Search
lxdream.org :: lxdream/src/tools/gendec.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/tools/gendec.c
changeset 420:e6f43dec3cf0
prev359:c588dce7ebde
next561:533f6b478071
author nkeynes
date Sat Oct 06 08:48:47 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Fix compile warnings
file annotate diff log raw
1.1 --- a/src/tools/gendec.c Thu Aug 23 12:33:27 2007 +0000
1.2 +++ b/src/tools/gendec.c Sat Oct 06 08:48:47 2007 +0000
1.3 @@ -1,5 +1,5 @@
1.4 /**
1.5 - * $Id: gendec.c,v 1.1 2007-08-23 12:33:27 nkeynes Exp $
1.6 + * $Id: gendec.c,v 1.2 2007-10-06 08:48:47 nkeynes Exp $
1.7 *
1.8 * Parse the instruction and action files and generate an appropriate
1.9 * instruction decoder.
1.10 @@ -46,99 +46,6 @@
1.11 printf( "gendec <instruction-file> <action-file> [ -o <output-file> ]\n" );
1.12 }
1.13
1.14 -int main( int argc, char *argv[] )
1.15 -{
1.16 - int opt, i;
1.17 -
1.18 - /* Parse the command line */
1.19 - while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
1.20 - switch( opt ) {
1.21 - case 't':
1.22 - gen_mode = GEN_TEMPLATE;
1.23 - break;
1.24 - case 'o':
1.25 - out_filename = optarg;
1.26 - break;
1.27 - case 'h':
1.28 - usage();
1.29 - exit(0);
1.30 - }
1.31 - }
1.32 - if( optind < argc ) {
1.33 - ins_filename = argv[optind++];
1.34 - }
1.35 - if( optind < argc ) {
1.36 - act_filename = argv[optind++];
1.37 - }
1.38 -
1.39 - if( optind < argc || ins_filename == NULL || act_filename == NULL ) {
1.40 - usage();
1.41 - exit(1);
1.42 - }
1.43 -
1.44 - if( out_filename == NULL ) {
1.45 - if( gen_mode == GEN_TEMPLATE ) {
1.46 - out_filename = act_filename;
1.47 - } else {
1.48 - char tmp[strlen(act_filename)+1];
1.49 - strcpy( tmp, act_filename);
1.50 - char *c = strrchr( tmp, '.' );
1.51 - if( c != NULL ) {
1.52 - *c = '\0';
1.53 - }
1.54 - out_filename = g_strconcat( tmp, DEFAULT_OUT_EXT );
1.55 - }
1.56 - }
1.57 -
1.58 - /* Open the files */
1.59 - ins_file = fopen( ins_filename, "ro" );
1.60 - if( ins_file == NULL ) {
1.61 - fprintf( stderr, "Unable to open '%s' for reading (%s)\n", ins_filename, strerror(errno) );
1.62 - exit(2);
1.63 - }
1.64 -
1.65 - act_file = fopen( act_filename, "ro" );
1.66 - if( act_file == NULL ) {
1.67 - fprintf( stderr, "Unable to open '%s' for reading (%s)\n", act_filename, strerror(errno) );
1.68 - exit(3);
1.69 - }
1.70 -
1.71 - /* Parse the input */
1.72 - struct ruleset *rules = parse_ruleset_file( ins_file );
1.73 - fclose( ins_file );
1.74 - if( rules == NULL ) {
1.75 - exit(5);
1.76 - }
1.77 -
1.78 - struct actionset *actions = parse_action_file( rules, act_file );
1.79 - fclose( act_file );
1.80 - if( actions == NULL ) {
1.81 - exit(6);
1.82 - }
1.83 -
1.84 - /* Finally write out the results */
1.85 - out_file = fopen( out_filename, "wo" );
1.86 - if( out_file == NULL ) {
1.87 - fprintf( stderr, "Unable to open '%s' for writing (%s)\n", out_filename, strerror(errno) );
1.88 - exit(4);
1.89 - }
1.90 -
1.91 - switch( gen_mode ) {
1.92 - case GEN_SOURCE:
1.93 - if( generate_decoder( rules, actions, out_file ) != 0 ) {
1.94 - exit(7);
1.95 - }
1.96 - break;
1.97 - case GEN_TEMPLATE:
1.98 - if( generate_template( rules, actions, out_file ) != 0 ) {
1.99 - exit(7);
1.100 - }
1.101 - break;
1.102 - }
1.103 - fclose( out_file );
1.104 - return 0;
1.105 -}
1.106 -
1.107 /**
1.108 * Find a mask that can be used to split up the given rules
1.109 */
1.110 @@ -191,7 +98,7 @@
1.111 }
1.112 }
1.113
1.114 -fprint_indent( char *action, int depth, FILE *f )
1.115 +void fprint_indent( char *action, int depth, FILE *f )
1.116 {
1.117 int spaces = 0, needed = depth*8, i;
1.118 char *text = action;
1.119 @@ -219,10 +126,9 @@
1.120 }
1.121 }
1.122
1.123 -fprint_action( struct rule *rule, char *action, int depth, FILE *f )
1.124 +void fprint_action( struct rule *rule, char *action, int depth, FILE *f )
1.125 {
1.126 int i;
1.127 - char tmp[64];
1.128 if( action == NULL ) {
1.129 fprintf( f, "%*cUNIMP(ir); /* %s */\n", depth*8, ' ', rule->format );
1.130 } else {
1.131 @@ -255,10 +161,9 @@
1.132 }
1.133 }
1.134
1.135 -int split_and_generate( struct ruleset *rules, struct actionset *actions,
1.136 +void split_and_generate( struct ruleset *rules, struct actionset *actions,
1.137 int ruleidx[], int rule_count, int input_mask,
1.138 int depth, FILE *f ) {
1.139 - char tmp[64];
1.140 uint32_t mask;
1.141 int i,j;
1.142
1.143 @@ -272,7 +177,7 @@
1.144 if( mask == 0 ) { /* No matching mask? */
1.145 fprintf( stderr, "Error: unable to find a valid bitmask (%d rules, %08X input mask)\n", rule_count, input_mask );
1.146 dump_rulesubset( rules, ruleidx, rule_count, stderr );
1.147 - return -1;
1.148 + return;
1.149 }
1.150
1.151 /* break up the rules into sub-sets, and process each sub-set.
1.152 @@ -351,3 +256,97 @@
1.153
1.154 return 0;
1.155 }
1.156 +
1.157 +
1.158 +int main( int argc, char *argv[] )
1.159 +{
1.160 + int opt;
1.161 +
1.162 + /* Parse the command line */
1.163 + while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
1.164 + switch( opt ) {
1.165 + case 't':
1.166 + gen_mode = GEN_TEMPLATE;
1.167 + break;
1.168 + case 'o':
1.169 + out_filename = optarg;
1.170 + break;
1.171 + case 'h':
1.172 + usage();
1.173 + exit(0);
1.174 + }
1.175 + }
1.176 + if( optind < argc ) {
1.177 + ins_filename = argv[optind++];
1.178 + }
1.179 + if( optind < argc ) {
1.180 + act_filename = argv[optind++];
1.181 + }
1.182 +
1.183 + if( optind < argc || ins_filename == NULL || act_filename == NULL ) {
1.184 + usage();
1.185 + exit(1);
1.186 + }
1.187 +
1.188 + if( out_filename == NULL ) {
1.189 + if( gen_mode == GEN_TEMPLATE ) {
1.190 + out_filename = act_filename;
1.191 + } else {
1.192 + char tmp[strlen(act_filename)+1];
1.193 + strcpy( tmp, act_filename);
1.194 + char *c = strrchr( tmp, '.' );
1.195 + if( c != NULL ) {
1.196 + *c = '\0';
1.197 + }
1.198 + out_filename = g_strconcat( tmp, DEFAULT_OUT_EXT, NULL );
1.199 + }
1.200 + }
1.201 +
1.202 + /* Open the files */
1.203 + ins_file = fopen( ins_filename, "ro" );
1.204 + if( ins_file == NULL ) {
1.205 + fprintf( stderr, "Unable to open '%s' for reading (%s)\n", ins_filename, strerror(errno) );
1.206 + exit(2);
1.207 + }
1.208 +
1.209 + act_file = fopen( act_filename, "ro" );
1.210 + if( act_file == NULL ) {
1.211 + fprintf( stderr, "Unable to open '%s' for reading (%s)\n", act_filename, strerror(errno) );
1.212 + exit(3);
1.213 + }
1.214 +
1.215 + /* Parse the input */
1.216 + struct ruleset *rules = parse_ruleset_file( ins_file );
1.217 + fclose( ins_file );
1.218 + if( rules == NULL ) {
1.219 + exit(5);
1.220 + }
1.221 +
1.222 + struct actionset *actions = parse_action_file( rules, act_file );
1.223 + fclose( act_file );
1.224 + if( actions == NULL ) {
1.225 + exit(6);
1.226 + }
1.227 +
1.228 + /* Finally write out the results */
1.229 + out_file = fopen( out_filename, "wo" );
1.230 + if( out_file == NULL ) {
1.231 + fprintf( stderr, "Unable to open '%s' for writing (%s)\n", out_filename, strerror(errno) );
1.232 + exit(4);
1.233 + }
1.234 +
1.235 + switch( gen_mode ) {
1.236 + case GEN_SOURCE:
1.237 + if( generate_decoder( rules, actions, out_file ) != 0 ) {
1.238 + exit(7);
1.239 + }
1.240 + break;
1.241 + case GEN_TEMPLATE:
1.242 + if( generate_template( rules, actions, out_file ) != 0 ) {
1.243 + exit(7);
1.244 + }
1.245 + break;
1.246 + }
1.247 + fclose( out_file );
1.248 + return 0;
1.249 +}
.