5 #include "tools/gendec.h"
7 #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)++; }
10 struct rule *new_rule() {
11 struct rule *rule = malloc( sizeof( struct rule ) );
12 memset( rule, 0, sizeof( struct rule ) );
16 struct ruleset *parse_ruleset_file( FILE *f )
18 struct ruleset *rules = malloc( sizeof(struct ruleset ) );
21 rules->rule_count = 0;
23 while( fgets( buf, sizeof(buf), f ) != NULL ) {
25 if( strncasecmp(buf, "registers", 9) == 0 ) {
26 parse_registers_block(buf, sizeof(buf), f);
27 } else if( buf[0] != '\0' && buf[0] != '#' && buf[0] != '\n' ) {
31 if( parse_rule( &p, rule ) != 0 ) {
34 rules->rules[rules->rule_count++] = rule;
41 int parse_registers_block( char *buf, int buflen, FILE *f ) {
43 if( strchr(buf, '}') != NULL ) {
46 } while( fgets( buf, buflen, f ) != NULL );
50 * Parse a single complete rule
51 * @return 0 on success, non-zero on failure
53 int parse_rule( char **str, struct rule *rule )
55 if( parse_bitstring( str, rule ) != 0 ) {
59 /* consume whitespace in between */
60 while( isspace(**str) ) (*str)++;
62 fprintf( stderr, "Unexpected end of file in rule on line %d\n", yyline );
66 int result = parse_rule_format( str, rule );
68 /* Reverse operand bit shifts */
70 for( j=0; j<rule->operand_count; j++ ) {
71 rule->operands[j].bit_shift =
72 rule->bit_count - rule->operands[j].bit_shift - rule->operands[j].bit_count;
76 result = parse_operand_uses( str, rule );
83 int parse_bitstring( char **str, struct rule *rule )
85 while( !isspace(**str) ) {
90 rule->bits = rule->bits << 1;
91 rule->mask = (rule->mask << 1) | 1;
95 rule->bits = (rule->bits << 1) | 1;
96 rule->mask = (rule->mask << 1) | 1;
100 if( parse_bitoperand( str, rule ) != 0 ) {
106 fprintf( stderr, "Unexpected character '%c' in bitstring at line %d\n", ch, yyline );
113 int parse_bitoperand( char **str, struct rule *rule )
115 char *p = rule->operands[rule->operand_count].name;
118 if( rule->operand_count == MAX_OPERANDS ) {
119 fprintf( stderr, "Maximum operands/rule exceeded (%d) at line %d\n", MAX_OPERANDS, yyline );
123 while( isalnum(**str) || **str == '_' ) {
129 int size = parse_integer( str );
133 rule->operands[rule->operand_count].bit_count = size;
134 if( **str == 's' || **str == 'S' ) {
136 rule->operands[rule->operand_count].is_signed = 1;
137 } else if( **str == 'u' || **str == 'U' ) {
139 rule->operands[rule->operand_count].is_signed = 0;
144 int lsl = parse_integer(str);
148 rule->operands[rule->operand_count].left_shift = lsl;
152 rule->operands[rule->operand_count].bit_shift = rule->bit_count;
153 rule->bit_count += size;
154 rule->bits = rule->bits << size;
155 rule->mask = rule->mask << size;
156 rule->operand_count++;
160 int parse_integer( char **str )
163 if( !isdigit(**str) ) {
164 fprintf(stderr, "Expected digit (0-9) but was '%c' at line %d\n", **str, yyline );
168 val = val * 10 + (**str - '0');
170 } while( isdigit(**str) );
174 int parse_rule_format( char **str, struct rule *rule )
179 while( **str != '\n' && **str != '\0' && **str != '!' ) {
183 strcpy( rule->format, tmp );
188 int parse_operand_uses( char **str, struct rule *rule )
193 void dump_ruleset( struct ruleset *rules, FILE *f )
196 fprintf( f, "Rulset: %d rules\n", rules->rule_count );
197 for( i=0; i<rules->rule_count; i++ ) {
198 struct rule *rule = rules->rules[i];
199 fprintf( f, "Match: %08X/%08X %s: ", rule->bits, rule->mask, rule->format );
200 for( j=0; j<rule->operand_count; j++ ) {
201 fprintf( f, "%s = (%s)(ir>>%d)&0x%X ", rule->operands[j].name,
202 rule->operands[j].is_signed ? "signed" : "unsigned",
203 rule->operands[j].bit_shift,
204 (1<<rule->operands[j].bit_count)-1 );
210 void dump_rulesubset( struct ruleset *rules, int ruleidx[], int rule_count, FILE *f )
213 for( i=0; i<rule_count; i++ ) {
214 struct rule *rule = rules->rules[ruleidx[i]];
215 fprintf( f, "Match: %08X/%08X %s: ", rule->bits, rule->mask, rule->format );
216 for( j=0; j<rule->operand_count; j++ ) {
217 fprintf( f, "%s = (%s)(ir>>%d)&0x%X ", rule->operands[j].name,
218 rule->operands[j].is_signed ? "signed" : "unsigned",
219 rule->operands[j].bit_shift,
220 (1<<rule->operands[j].bit_count)-1 );
.