Search
lxdream.org :: lxdream/src/tools/gendec.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/tools/gendec.c
changeset 948:545c85cc56f1
prev824:016cda9d0518
next969:3f178ca1398c
author nkeynes
date Wed Jan 07 04:39:04 2009 +0000 (13 years ago)
branchlxdream-mem
permissions -rw-r--r--
last change Introduce sh4_finalize_instruction to clean-up on instruction exits
Remove the sh4_flush_icache special cases, now works through the
general case.
view annotate diff log raw
     1 /**
     2  * $Id$
     3  * 
     4  * Parse the instruction and action files and generate an appropriate
     5  * instruction decoder.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    20 #include <stdio.h>
    21 #include <stdlib.h>
    22 #include <string.h>
    23 #include <getopt.h>
    24 #include <errno.h>
    25 #include <ctype.h>
    26 #include <glib/gstrfuncs.h>
    27 #include <assert.h>
    28 #include "tools/gendec.h"
    30 #define DEFAULT_OUT_EXT ".c"
    32 const char *ins_filename = NULL;
    33 const char *act_filename = NULL;
    34 const char *out_filename = NULL;
    36 #define GEN_SOURCE 1
    37 #define GEN_TEMPLATE 2
    39 FILE *ins_file, *act_file, *out_file;
    41 char *option_list = "tmho:";
    42 int gen_mode = GEN_SOURCE;
    43 struct option longopts[1] = { { NULL, 0, 0, 0 } };
    45 void usage() {
    46     printf( "gendec <instruction-file> <action-file> [ -o <output-file> ]\n" );
    47 }
    49 /**
    50  * Find a mask that can be used to split up the given rules
    51  */
    52 uint32_t find_mask( struct ruleset *rules, int ruleidx[], int rule_count, 
    53                     uint32_t input_mask )
    54 {
    55     int i;
    56     uint32_t mask = rules->rules[ruleidx[0]]->mask;
    58     for( i=1; i<rule_count; i++ ) {
    59         mask = mask & rules->rules[ruleidx[i]]->mask;
    60     }
    62     assert( (mask & input_mask) == input_mask ); /* input_mask should always be included in the mask */
    64     return mask & (~input_mask); /* but we don't want to see the input mask again */
    65 }
    67 int get_option_count_for_mask( uint32_t mask ) {
    68     int count = 0;
    70     while( mask ) {
    71         if( mask&1 ) 
    72             count++;
    73         mask >>= 1;
    74     }
    75     return 1<<count;
    76 }
    78 int get_bitshift_for_mask( uint32_t mask ) {
    79     int shift = 0;
    80     while( mask && !(mask&1) ) {
    81         shift++;
    82         mask >>= 1;
    83     }
    84     return shift;
    85 }
    87 void get_option_values_for_mask( uint32_t *options, 
    88                                  uint32_t mask ) 
    89 {
    90     /* This could be a lot smarter. But it's not */
    91     int i;
    92     *options = 0;
    93     for( i=1; i<=mask; i++ ) {
    94         if( (i & mask) > *options ) {
    95             options++;
    96             *options = (i&mask);
    97         }
    98     }
    99 }
   101 void fprint_indent( char *action, int depth, FILE *f )
   102 {
   103     int spaces = 0, needed = depth*8, i;
   104     char *text = action;
   106     /* Determine number of spaces in first line of input */
   107     for( i=0; isspace(action[i]); i++ ) {
   108         if( action[i] == '\n' ) {
   109             spaces = 0;
   110             text = &action[i+1];
   111         } else {
   112             spaces++;
   113         }
   114     }
   116     needed -= spaces;
   117     fprintf( f, "%*c", needed, ' ' );
   118     for( i=0; text[i] != '\0'; i++ ) {
   119         fputc( text[i], f );
   120         if( text[i] == '\n' && text[i+1] != '\0' ) {
   121             fprintf( f, "%*c", needed, ' ' );
   122         }
   123     }
   124     if( text[i-1] != '\n' ) {
   125         fprintf( f, "\n" );
   126     }
   127 }
   129 void fprint_action( struct rule *rule, char *action, int depth, FILE *f ) 
   130 {
   131     int i;
   132     if( action == NULL ) {
   133         fprintf( f, "%*cUNIMP(ir); /* %s */\n", depth*8, ' ', rule->format );
   134     } else {
   135         fprintf( f, "%*c{ /* %s */", depth*8, ' ', rule->format );
   136         if( rule->operand_count != 0 ) {
   137             fprintf( f, "\n%*c", depth*8, ' ' );
   138             for( i=0; i<rule->operand_count; i++ ) {
   139                 if( rule->operands[i].is_signed ) {
   140                     fprintf( f, "int32_t %s = SIGNEXT%d", rule->operands[i].name, rule->operands[i].bit_count );
   141                 } else {
   142                     fprintf( f, "uint32_t %s = ", rule->operands[i].name );
   143                 }
   144                 if( rule->operands[i].bit_shift == 0 ) {
   145                     fprintf( f, "(ir&0x%X)", (1<<(rule->operands[i].bit_count))-1 );
   146                 } else {
   147                     fprintf( f, "((ir>>%d)&0x%X)", rule->operands[i].bit_shift,
   148                             (1<<(rule->operands[i].bit_count))-1 );
   149                 }
   150                 if( rule->operands[i].left_shift != 0 ) {
   151                     fprintf( f, "<<%d", rule->operands[i].left_shift );
   152                 }
   153                 fprintf( f, "; " );
   154             }
   155         }
   156         fputs( "\n", f );
   157         if( action[0] != '\0' ) {
   158             fprint_indent( action, depth, f );
   159         }
   160         fprintf( f, "%*c}\n", depth*8, ' ' );
   161     }
   162 }
   164 void split_and_generate( struct ruleset *rules, char **actions, 
   165                          int ruleidx[], int rule_count, int input_mask, 
   166                          int depth, FILE *f ) {
   167     uint32_t mask;
   168     int i,j;
   170     if( rule_count == 0 ) {
   171         fprintf( f, "%*cUNDEF(ir);\n", depth*8, ' ' );
   172     } else if( rule_count == 1 ) {
   173         fprint_action( rules->rules[ruleidx[0]], actions[ruleidx[0]], depth, f );
   174     } else {
   176         mask = find_mask(rules, ruleidx, rule_count, input_mask);
   177         if( mask == 0 ) { /* No matching mask? */
   178             fprintf( stderr, "Error: unable to find a valid bitmask (%d rules, %08X input mask)\n", rule_count, input_mask );
   179             dump_rulesubset( rules, ruleidx, rule_count, stderr );
   180             return;
   181         }
   183         /* break up the rules into sub-sets, and process each sub-set.
   184          * NB: We could do this in one pass at the cost of more complex
   185          * data structures. For now though, this keeps it simple
   186          */
   187         int option_count = get_option_count_for_mask( mask );
   188         uint32_t options[option_count];
   189         int subruleidx[rule_count];
   190         int subrule_count;
   191         int mask_shift = get_bitshift_for_mask( mask );
   192         int has_empty_options = 0;
   193         get_option_values_for_mask( options, mask );
   195         if( mask_shift == 0 ) {
   196             fprintf( f, "%*cswitch( ir&0x%X ) {\n", depth*8, ' ', mask );
   197         } else {
   198             fprintf( f, "%*cswitch( (ir&0x%X) >> %d ) {\n", depth*8, ' ',
   199                     mask, mask_shift);
   200         }
   201         for( i=0; i<option_count; i++ ) {
   202             subrule_count = 0;
   203             for( j=0; j<rule_count; j++ ) {
   204                 int match = rules->rules[ruleidx[j]]->bits & mask;
   205                 if( match == options[i] ) {
   206                     subruleidx[subrule_count++] = ruleidx[j];
   207                 }
   208             }
   209             if( subrule_count == 0 ) {
   210                 has_empty_options = 1;
   211             } else {
   212                 fprintf( f, "%*ccase 0x%X:\n", depth*8+4, ' ', options[i]>>mask_shift );
   213                 split_and_generate( rules, actions, subruleidx, subrule_count,
   214                                     mask|input_mask, depth+1, f );
   215                 fprintf( f, "%*cbreak;\n", depth*8+8, ' ' );
   216             }
   217         }
   218         if( has_empty_options ) {
   219             fprintf( f, "%*cdefault:\n%*cUNDEF(ir);\n%*cbreak;\n",
   220                     depth*8+4, ' ', depth*8+8, ' ', depth*8 + 8, ' ' );
   221         }
   222         fprintf( f, "%*c}\n", depth*8, ' ' );
   223     }
   224 }
   226 int generate_decoder( struct ruleset *rules, actionfile_t af, FILE *out )
   227 {
   228     int ruleidx[rules->rule_count];
   229     int i;
   231     for( i=0; i<rules->rule_count; i++ ) {
   232         ruleidx[i] = i;
   233     }
   235     actiontoken_t token = action_file_next(af);
   236     while( token->symbol != END ) {
   237         if( token->symbol == TEXT ) {
   238             fputs( token->text, out );
   239         } else if( token->symbol == ERROR ) {
   240             fprintf( stderr, "Error parsing action file" );
   241             return -1;
   242         } else {
   243             split_and_generate( rules, token->actions, ruleidx, rules->rule_count, 0, 1, out );
   244         }
   245         token = action_file_next(af);
   246     }
   247     return 0;
   248 }
   250 int generate_template( struct ruleset *rules, actionfile_t af, FILE *out )
   251 {
   252     int i;
   254     actiontoken_t token = action_file_next(af);
   255     while( token->symbol != END ) {
   256         if( token->symbol == TEXT ) {
   257             fputs( token->text, out );
   258         } else if( token->symbol == ERROR ) {
   259             fprintf( stderr, "Error parsing action file" );
   260             return -1;
   261         } else {
   262             fputs( "%%\n", out );
   263             for( i=0; i<rules->rule_count; i++ ) {
   264                 fprintf( out, "%s {: %s :}\n", rules->rules[i]->format,
   265                         token->actions[i] == NULL ? "" : token->actions[i] );
   266             }
   267             fputs( "%%\n", out );
   268         }
   269         token = action_file_next(af);
   270     }
   272     return 0;
   273 }
   276 int main( int argc, char *argv[] )
   277 {
   278     int opt;
   280     /* Parse the command line */
   281     while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
   282         switch( opt ) {
   283         case 't':
   284             gen_mode = GEN_TEMPLATE;
   285             break;
   286         case 'o':
   287             out_filename = optarg;
   288             break;
   289         case 'h':
   290             usage();
   291             exit(0);
   292         }
   293     }
   294     if( optind < argc ) {
   295         ins_filename = argv[optind++];
   296     }
   297     if( optind < argc ) {
   298         act_filename = argv[optind++];
   299     }
   301     if( optind < argc || ins_filename == NULL || act_filename == NULL ) {
   302         usage();
   303         exit(1);
   304     }
   306     if( out_filename == NULL ) {
   307         if( gen_mode == GEN_TEMPLATE ) {
   308             out_filename = act_filename;
   309         } else {
   310             char tmp[strlen(act_filename)+1];
   311             strcpy( tmp, act_filename);
   312             char *c = strrchr( tmp, '.' );
   313             if( c != NULL ) {
   314                 *c = '\0';
   315             }
   316             out_filename = g_strconcat( tmp, DEFAULT_OUT_EXT, NULL );
   317         }
   318     }
   320     /* Open the files */
   321     ins_file = fopen( ins_filename, "ro" );
   322     if( ins_file == NULL ) {
   323         fprintf( stderr, "Unable to open '%s' for reading (%s)\n", ins_filename, strerror(errno) );
   324         exit(2);
   325     }
   327     /* Parse the input */
   328     struct ruleset *rules = parse_ruleset_file( ins_file );
   329     fclose( ins_file );
   330     if( rules == NULL ) {
   331         exit(5);
   332     }
   334     actionfile_t af = action_file_open( act_filename, rules );
   335     if( af == NULL ) {
   336         fprintf( stderr, "Unable to open '%s' for reading (%s)\n", act_filename, strerror(errno) );
   337         exit(3);
   338     }
   341     /* Open the output file */
   342     out_file = fopen( out_filename, "wo" );
   343     if( out_file == NULL ) {
   344         fprintf( stderr, "Unable to open '%s' for writing (%s)\n", out_filename, strerror(errno) );
   345         exit(4);
   346     }
   348     switch( gen_mode ) {
   349     case GEN_SOURCE:
   350         if( generate_decoder( rules, af, out_file ) != 0 ) {
   351             exit(7);
   352         }
   353         break;
   354     case GEN_TEMPLATE:
   355         if( generate_template( rules, af, out_file ) != 0 ) {
   356             exit(7);
   357         }
   358         break;
   359     }
   361     action_file_close(af);
   362     fclose( out_file );
   363     return 0;
   364 }
.