Search
lxdream.org :: lxdream/src/tools/insparse.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/tools/insparse.c
changeset 736:a02d1475ccfd
prev561:533f6b478071
author nkeynes
date Mon Jul 14 07:44:42 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Re-indent everything consistently
Fix include guards for consistency as well
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * gendec instruction definitions parser
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     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.
    12  *
    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.
    17  */
    19 #include <stdlib.h>
    20 #include <stdio.h>
    21 #include <string.h>
    22 #include <ctype.h>
    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)++; }
    26 static int yyline;
    28 struct rule *new_rule() {
    29     struct rule *rule = malloc( sizeof( struct rule ) );
    30     memset( rule, 0, sizeof( struct rule ) );
    31     return rule;
    32 }
    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 ) 
    45 {
    46     struct ruleset *rules = malloc( sizeof(struct ruleset ) );
    47     char buf[512];
    49     rules->rule_count = 0;
    50     yyline = 0;
    51     while( fgets( buf, sizeof(buf), f ) != NULL ) {
    52         yyline++;
    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' ) {
    56             struct rule *rule;
    57             char *p = buf;
    58             rule = new_rule();
    59             if( parse_rule( &p, rule ) != 0 ) {
    60                 free( rule );
    61             } else {
    62                 rules->rules[rules->rule_count++] = rule;
    63             }
    64         }
    65     }
    66     return rules;
    67 }
    69 int parse_registers_block( char *buf, int buflen, FILE *f ) {
    70     do {
    71         if( strchr(buf, '}') != NULL ) {
    72             break;
    73         }
    74     } while( fgets( buf, buflen, f ) != NULL );
    75     return 0;
    76 }
    78 /**
    79  * Parse a single complete rule
    80  * @return 0 on success, non-zero on failure
    81  */
    82 int parse_rule( char **str, struct rule *rule ) 
    83 {
    84     if( parse_bitstring( str, rule ) != 0 ) {
    85         return -1;
    86     }
    88     /* consume whitespace in between */
    89     while( isspace(**str) ) (*str)++;
    90     if( **str == '\0' ) {
    91         fprintf( stderr, "Unexpected end of file in rule on line %d\n", yyline );
    92         return -1;
    93     }
    95     int result = parse_rule_format( str, rule );
    96     if( result == 0 ) {
    97         /* Reverse operand bit shifts */
    98         int j;
    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;
   102         }
   103         if( **str == '!' ) {
   104             (*str)++;
   105             result = parse_operand_uses( str, rule );
   106         }
   107     }
   109     return 0;
   110 }
   112 int parse_bitstring( char **str, struct rule *rule )
   113 {
   114     while( !isspace(**str) ) {
   115         int ch = **str;
   116         (*str)++;
   117         switch( ch ) {
   118         case '0':
   119             rule->bits = rule->bits << 1;
   120             rule->mask = (rule->mask << 1) | 1;
   121             rule->bit_count++;
   122             break;
   123         case '1':
   124             rule->bits = (rule->bits << 1) | 1;
   125             rule->mask = (rule->mask << 1) | 1;
   126             rule->bit_count++;
   127             break;
   128         case '(':
   129             if( parse_bitoperand( str, rule ) != 0 ) {
   130                 return -1 ;
   131             }
   132             break;
   133         default:
   134             (*str)--;
   135             fprintf( stderr, "Unexpected character '%c' in bitstring at line %d\n", ch, yyline );
   136             return -1;
   137         }
   138     }
   139     return 0;
   140 }
   142 int parse_bitoperand( char **str, struct rule *rule )
   143 {
   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 );
   148         return -1;
   149     }
   151     while( isalnum(**str) || **str == '_' ) {
   152         *p++ = *(*str)++;
   153     }
   154     *p = '\0';
   155     CONSUME_CHAR(':');
   157     int size = parse_integer( str );
   158     if( size == -1 ) {
   159         return -1; 
   160     }
   161     rule->operands[rule->operand_count].bit_count = size;
   162     if( **str == 's' || **str == 'S' ) {
   163         (*str)++;
   164         rule->operands[rule->operand_count].is_signed = 1;
   165     } else if( **str == 'u' || **str == 'U' ) {
   166         (*str)++;
   167         rule->operands[rule->operand_count].is_signed = 0;
   168     }
   169     if( **str == '<' ) {
   170         (*str)++;
   171         CONSUME_CHAR('<');
   172         int lsl = parse_integer(str);
   173         if( lsl == -1 ) {
   174             return -1;
   175         }
   176         rule->operands[rule->operand_count].left_shift = lsl;
   177     }
   178     CONSUME_CHAR(')');
   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++;
   185     return 0;
   186 }
   188 int parse_integer( char **str )
   189 {
   190     uint32_t val = 0;
   191     if( !isdigit(**str) ) {
   192         fprintf(stderr, "Expected digit (0-9) but was '%c' at line %d\n", **str, yyline );
   193         return -1;
   194     }
   195     do {
   196         val = val * 10 + (**str - '0');
   197         (*str)++;
   198     } while( isdigit(**str) );
   199     return val;
   200 }
   202 int parse_rule_format( char **str, struct rule *rule )
   203 {
   205     char tmp[64];
   206     char *p = tmp;
   207     while( **str != '\n' && **str != '\0' && **str != '!' ) {
   208         *p++ = *(*str)++;
   209     }
   210     *p = '\0';
   211     strcpy( rule->format, tmp );
   213     return 0;
   214 }
   216 int parse_operand_uses( char **str, struct rule *rule )
   217 {
   218     return 0;
   219 }
   221 void dump_ruleset( struct ruleset *rules, FILE *f ) 
   222 {
   223     int i, j;
   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 );
   233         }
   234         fprintf( f, "\n" );
   235     }
   236 }
   238 void dump_rulesubset( struct ruleset *rules, int ruleidx[], int rule_count, FILE *f )
   239 {
   240     int i,j;
   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 );
   249         }
   250         fprintf( f, "\n" );
   251     }
   252 }
.