4 * gendec instruction definitions parser
6 * Copyright (c) 2005 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
23 #include "tools/gendec.h"
25 #define CONSUME_CHAR(x) if( **str != x ) { fprintf( stderr, "Unexpected input character '%c', expected '%c' at line %d\n", **str, x, yyline ); return -1; } else { (*str)++; }
28 struct rule *new_rule() {
29 struct rule *rule = malloc( sizeof( struct rule ) );
30 memset( rule, 0, sizeof( struct rule ) );
34 int parse_registers_block( char *buf, int buflen, FILE *f );
35 int parse_rule( char **str, struct rule *rule );
36 int parse_bitstring( char **str, struct rule *rule );
37 int parse_bitoperand( char **str, struct rule *rule );
38 int parse_integer( char **str );
39 int parse_rule_format( char **str, struct rule *rule );
40 int parse_operand_uses( char **str, struct rule *rule );
44 struct ruleset *parse_ruleset_file( FILE *f )
46 struct ruleset *rules = malloc( sizeof(struct ruleset ) );
49 rules->rule_count = 0;
51 while( fgets( buf, sizeof(buf), f ) != NULL ) {
53 if( strncasecmp(buf, "registers", 9) == 0 ) {
54 parse_registers_block(buf, sizeof(buf), f);
55 } else if( buf[0] != '\0' && buf[0] != '#' && buf[0] != '\n' ) {
59 if( parse_rule( &p, rule ) != 0 ) {
62 rules->rules[rules->rule_count++] = rule;
69 int parse_registers_block( char *buf, int buflen, FILE *f ) {
71 if( strchr(buf, '}') != NULL ) {
74 } while( fgets( buf, buflen, f ) != NULL );
79 * Parse a single complete rule
80 * @return 0 on success, non-zero on failure
82 int parse_rule( char **str, struct rule *rule )
84 if( parse_bitstring( str, rule ) != 0 ) {
88 /* consume whitespace in between */
89 while( isspace(**str) ) (*str)++;
91 fprintf( stderr, "Unexpected end of file in rule on line %d\n", yyline );
95 int result = parse_rule_format( str, rule );
97 /* Reverse operand bit shifts */
99 for( j=0; j<rule->operand_count; j++ ) {
100 rule->operands[j].bit_shift =
101 rule->bit_count - rule->operands[j].bit_shift - rule->operands[j].bit_count;
105 result = parse_operand_uses( str, rule );
112 int parse_bitstring( char **str, struct rule *rule )
114 while( !isspace(**str) ) {
119 rule->bits = rule->bits << 1;
120 rule->mask = (rule->mask << 1) | 1;
124 rule->bits = (rule->bits << 1) | 1;
125 rule->mask = (rule->mask << 1) | 1;
129 if( parse_bitoperand( str, rule ) != 0 ) {
135 fprintf( stderr, "Unexpected character '%c' in bitstring at line %d\n", ch, yyline );
142 int parse_bitoperand( char **str, struct rule *rule )
144 char *p = rule->operands[rule->operand_count].name;
146 if( rule->operand_count == MAX_OPERANDS ) {
147 fprintf( stderr, "Maximum operands/rule exceeded (%d) at line %d\n", MAX_OPERANDS, yyline );
151 while( isalnum(**str) || **str == '_' ) {
157 int size = parse_integer( str );
161 rule->operands[rule->operand_count].bit_count = size;
162 if( **str == 's' || **str == 'S' ) {
164 rule->operands[rule->operand_count].is_signed = 1;
165 } else if( **str == 'u' || **str == 'U' ) {
167 rule->operands[rule->operand_count].is_signed = 0;
172 int lsl = parse_integer(str);
176 rule->operands[rule->operand_count].left_shift = lsl;
180 rule->operands[rule->operand_count].bit_shift = rule->bit_count;
181 rule->bit_count += size;
182 rule->bits = rule->bits << size;
183 rule->mask = rule->mask << size;
184 rule->operand_count++;
188 int parse_integer( char **str )
191 if( !isdigit(**str) ) {
192 fprintf(stderr, "Expected digit (0-9) but was '%c' at line %d\n", **str, yyline );
196 val = val * 10 + (**str - '0');
198 } while( isdigit(**str) );
202 int parse_rule_format( char **str, struct rule *rule )
207 while( **str != '\n' && **str != '\0' && **str != '!' ) {
211 strcpy( rule->format, tmp );
216 int parse_operand_uses( char **str, struct rule *rule )
221 void dump_ruleset( struct ruleset *rules, FILE *f )
224 fprintf( f, "Rulset: %d rules\n", rules->rule_count );
225 for( i=0; i<rules->rule_count; i++ ) {
226 struct rule *rule = rules->rules[i];
227 fprintf( f, "Match: %08X/%08X %s: ", rule->bits, rule->mask, rule->format );
228 for( j=0; j<rule->operand_count; j++ ) {
229 fprintf( f, "%s = (%s)(ir>>%d)&0x%X ", rule->operands[j].name,
230 rule->operands[j].is_signed ? "signed" : "unsigned",
231 rule->operands[j].bit_shift,
232 (1<<rule->operands[j].bit_count)-1 );
238 void dump_rulesubset( struct ruleset *rules, int ruleidx[], int rule_count, FILE *f )
241 for( i=0; i<rule_count; i++ ) {
242 struct rule *rule = rules->rules[ruleidx[i]];
243 fprintf( f, "Match: %08X/%08X %s: ", rule->bits, rule->mask, rule->format );
244 for( j=0; j<rule->operand_count; j++ ) {
245 fprintf( f, "%s = (%s)(ir>>%d)&0x%X ", rule->operands[j].name,
246 rule->operands[j].is_signed ? "signed" : "unsigned",
247 rule->operands[j].bit_shift,
248 (1<<rule->operands[j].bit_count)-1 );
.