Search
lxdream.org :: lxdream/src/tools/insparse.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/tools/insparse.c
changeset 561:533f6b478071
prev420:e6f43dec3cf0
next736:a02d1475ccfd
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
file annotate diff log raw
nkeynes@561
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@561
     3
 *
nkeynes@561
     4
 * gendec instruction definitions parser
nkeynes@561
     5
 *
nkeynes@561
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@561
     7
 *
nkeynes@561
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@561
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@561
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@561
    11
 * (at your option) any later version.
nkeynes@561
    12
 *
nkeynes@561
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@561
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@561
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@561
    16
 * GNU General Public License for more details.
nkeynes@561
    17
 */
nkeynes@561
    18
nkeynes@359
    19
#include <stdlib.h>
nkeynes@359
    20
#include <stdio.h>
nkeynes@359
    21
#include <string.h>
nkeynes@359
    22
#include <ctype.h>
nkeynes@359
    23
#include "tools/gendec.h"
nkeynes@359
    24
nkeynes@359
    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)++; }
nkeynes@359
    26
static int yyline;
nkeynes@359
    27
nkeynes@359
    28
struct rule *new_rule() {
nkeynes@359
    29
    struct rule *rule = malloc( sizeof( struct rule ) );
nkeynes@359
    30
    memset( rule, 0, sizeof( struct rule ) );
nkeynes@359
    31
    return rule;
nkeynes@359
    32
}
nkeynes@359
    33
nkeynes@420
    34
int parse_registers_block( char *buf, int buflen, FILE *f );
nkeynes@420
    35
int parse_rule( char **str, struct rule *rule );
nkeynes@420
    36
int parse_bitstring( char **str, struct rule *rule );
nkeynes@420
    37
int parse_bitoperand( char **str, struct rule *rule );
nkeynes@420
    38
int parse_integer( char **str );
nkeynes@420
    39
int parse_rule_format( char **str, struct rule *rule );
nkeynes@420
    40
int parse_operand_uses( char **str, struct rule *rule );
nkeynes@420
    41
nkeynes@420
    42
nkeynes@420
    43
nkeynes@359
    44
struct ruleset *parse_ruleset_file( FILE *f ) 
nkeynes@359
    45
{
nkeynes@359
    46
    struct ruleset *rules = malloc( sizeof(struct ruleset ) );
nkeynes@359
    47
    char buf[512];
nkeynes@359
    48
nkeynes@359
    49
    rules->rule_count = 0;
nkeynes@359
    50
    yyline = 0;
nkeynes@359
    51
    while( fgets( buf, sizeof(buf), f ) != NULL ) {
nkeynes@359
    52
	yyline++;
nkeynes@359
    53
	if( strncasecmp(buf, "registers", 9) == 0 ) {
nkeynes@359
    54
	    parse_registers_block(buf, sizeof(buf), f);
nkeynes@359
    55
	} else if( buf[0] != '\0' && buf[0] != '#' && buf[0] != '\n' ) {
nkeynes@359
    56
	    struct rule *rule;
nkeynes@359
    57
	    char *p = buf;
nkeynes@359
    58
	    rule = new_rule();
nkeynes@359
    59
	    if( parse_rule( &p, rule ) != 0 ) {
nkeynes@359
    60
		free( rule );
nkeynes@359
    61
	    } else {
nkeynes@359
    62
		rules->rules[rules->rule_count++] = rule;
nkeynes@359
    63
	    }
nkeynes@359
    64
	}
nkeynes@359
    65
    }
nkeynes@359
    66
    return rules;
nkeynes@359
    67
}
nkeynes@359
    68
nkeynes@359
    69
int parse_registers_block( char *buf, int buflen, FILE *f ) {
nkeynes@359
    70
    do {
nkeynes@359
    71
	if( strchr(buf, '}') != NULL ) {
nkeynes@359
    72
	    break;
nkeynes@359
    73
	}
nkeynes@359
    74
    } while( fgets( buf, buflen, f ) != NULL );
nkeynes@420
    75
    return 0;
nkeynes@359
    76
}
nkeynes@359
    77
nkeynes@359
    78
/**
nkeynes@359
    79
 * Parse a single complete rule
nkeynes@359
    80
 * @return 0 on success, non-zero on failure
nkeynes@359
    81
 */
nkeynes@359
    82
int parse_rule( char **str, struct rule *rule ) 
nkeynes@359
    83
{
nkeynes@359
    84
    if( parse_bitstring( str, rule ) != 0 ) {
nkeynes@359
    85
	return -1;
nkeynes@359
    86
    }
nkeynes@359
    87
nkeynes@359
    88
    /* consume whitespace in between */
nkeynes@359
    89
    while( isspace(**str) ) (*str)++;
nkeynes@359
    90
    if( **str == '\0' ) {
nkeynes@359
    91
	fprintf( stderr, "Unexpected end of file in rule on line %d\n", yyline );
nkeynes@359
    92
	return -1;
nkeynes@359
    93
    }
nkeynes@359
    94
    
nkeynes@359
    95
    int result = parse_rule_format( str, rule );
nkeynes@359
    96
    if( result == 0 ) {
nkeynes@359
    97
	/* Reverse operand bit shifts */
nkeynes@359
    98
	int j;
nkeynes@359
    99
	for( j=0; j<rule->operand_count; j++ ) {
nkeynes@359
   100
	    rule->operands[j].bit_shift = 
nkeynes@359
   101
		rule->bit_count - rule->operands[j].bit_shift - rule->operands[j].bit_count;
nkeynes@359
   102
	}
nkeynes@359
   103
	if( **str == '!' ) {
nkeynes@359
   104
	    (*str)++;
nkeynes@359
   105
	    result = parse_operand_uses( str, rule );
nkeynes@359
   106
	}
nkeynes@359
   107
    }
nkeynes@359
   108
nkeynes@359
   109
    return 0;
nkeynes@359
   110
}
nkeynes@359
   111
nkeynes@359
   112
int parse_bitstring( char **str, struct rule *rule )
nkeynes@359
   113
{
nkeynes@359
   114
    while( !isspace(**str) ) {
nkeynes@359
   115
	int ch = **str;
nkeynes@359
   116
	(*str)++;
nkeynes@359
   117
	switch( ch ) {
nkeynes@359
   118
	case '0':
nkeynes@359
   119
	    rule->bits = rule->bits << 1;
nkeynes@359
   120
	    rule->mask = (rule->mask << 1) | 1;
nkeynes@359
   121
	    rule->bit_count++;
nkeynes@359
   122
	    break;
nkeynes@359
   123
	case '1':
nkeynes@359
   124
	    rule->bits = (rule->bits << 1) | 1;
nkeynes@359
   125
	    rule->mask = (rule->mask << 1) | 1;
nkeynes@359
   126
	    rule->bit_count++;
nkeynes@359
   127
	    break;
nkeynes@359
   128
	case '(':
nkeynes@359
   129
	    if( parse_bitoperand( str, rule ) != 0 ) {
nkeynes@359
   130
		return -1 ;
nkeynes@359
   131
	    }
nkeynes@359
   132
	    break;
nkeynes@359
   133
	default:
nkeynes@359
   134
	    (*str)--;
nkeynes@359
   135
	    fprintf( stderr, "Unexpected character '%c' in bitstring at line %d\n", ch, yyline );
nkeynes@359
   136
	    return -1;
nkeynes@359
   137
	}
nkeynes@359
   138
    }
nkeynes@359
   139
    return 0;
nkeynes@359
   140
}
nkeynes@359
   141
nkeynes@359
   142
int parse_bitoperand( char **str, struct rule *rule )
nkeynes@359
   143
{
nkeynes@359
   144
    char *p = rule->operands[rule->operand_count].name;
nkeynes@359
   145
nkeynes@359
   146
    if( rule->operand_count == MAX_OPERANDS ) {
nkeynes@359
   147
	fprintf( stderr, "Maximum operands/rule exceeded (%d) at line %d\n", MAX_OPERANDS, yyline );
nkeynes@359
   148
	return -1;
nkeynes@359
   149
    }
nkeynes@359
   150
nkeynes@359
   151
    while( isalnum(**str) || **str == '_' ) {
nkeynes@359
   152
	*p++ = *(*str)++;
nkeynes@359
   153
    }
nkeynes@359
   154
    *p = '\0';
nkeynes@359
   155
    CONSUME_CHAR(':');
nkeynes@359
   156
    
nkeynes@359
   157
    int size = parse_integer( str );
nkeynes@359
   158
    if( size == -1 ) {
nkeynes@359
   159
	return -1; 
nkeynes@359
   160
    }
nkeynes@359
   161
    rule->operands[rule->operand_count].bit_count = size;
nkeynes@359
   162
    if( **str == 's' || **str == 'S' ) {
nkeynes@359
   163
	(*str)++;
nkeynes@359
   164
	rule->operands[rule->operand_count].is_signed = 1;
nkeynes@359
   165
    } else if( **str == 'u' || **str == 'U' ) {
nkeynes@359
   166
	(*str)++;
nkeynes@359
   167
	rule->operands[rule->operand_count].is_signed = 0;
nkeynes@359
   168
    }
nkeynes@359
   169
    if( **str == '<' ) {
nkeynes@359
   170
	(*str)++;
nkeynes@359
   171
	CONSUME_CHAR('<');
nkeynes@359
   172
	int lsl = parse_integer(str);
nkeynes@359
   173
	if( lsl == -1 ) {
nkeynes@359
   174
	    return -1;
nkeynes@359
   175
	}
nkeynes@359
   176
	rule->operands[rule->operand_count].left_shift = lsl;
nkeynes@359
   177
    }
nkeynes@359
   178
    CONSUME_CHAR(')');
nkeynes@359
   179
nkeynes@359
   180
    rule->operands[rule->operand_count].bit_shift = rule->bit_count;
nkeynes@359
   181
    rule->bit_count += size;
nkeynes@359
   182
    rule->bits = rule->bits << size;
nkeynes@359
   183
    rule->mask = rule->mask << size;
nkeynes@359
   184
    rule->operand_count++;
nkeynes@359
   185
    return 0;
nkeynes@359
   186
}
nkeynes@359
   187
nkeynes@359
   188
int parse_integer( char **str )
nkeynes@359
   189
{
nkeynes@359
   190
    uint32_t val = 0;
nkeynes@359
   191
    if( !isdigit(**str) ) {
nkeynes@359
   192
	fprintf(stderr, "Expected digit (0-9) but was '%c' at line %d\n", **str, yyline );
nkeynes@359
   193
	return -1;
nkeynes@359
   194
    }
nkeynes@359
   195
    do {
nkeynes@359
   196
	val = val * 10 + (**str - '0');
nkeynes@359
   197
	(*str)++;
nkeynes@359
   198
    } while( isdigit(**str) );
nkeynes@359
   199
    return val;
nkeynes@359
   200
}
nkeynes@359
   201
nkeynes@359
   202
int parse_rule_format( char **str, struct rule *rule )
nkeynes@359
   203
{
nkeynes@359
   204
nkeynes@359
   205
    char tmp[64];
nkeynes@359
   206
    char *p = tmp;
nkeynes@359
   207
    while( **str != '\n' && **str != '\0' && **str != '!' ) {
nkeynes@359
   208
	*p++ = *(*str)++;
nkeynes@359
   209
    }
nkeynes@359
   210
    *p = '\0';
nkeynes@359
   211
    strcpy( rule->format, tmp );
nkeynes@359
   212
nkeynes@359
   213
    return 0;
nkeynes@359
   214
}
nkeynes@359
   215
nkeynes@359
   216
int parse_operand_uses( char **str, struct rule *rule )
nkeynes@359
   217
{
nkeynes@420
   218
    return 0;
nkeynes@359
   219
}
nkeynes@359
   220
nkeynes@359
   221
void dump_ruleset( struct ruleset *rules, FILE *f ) 
nkeynes@359
   222
{
nkeynes@359
   223
    int i, j;
nkeynes@359
   224
    fprintf( f, "Rulset: %d rules\n", rules->rule_count );
nkeynes@359
   225
    for( i=0; i<rules->rule_count; i++ ) {
nkeynes@359
   226
	struct rule *rule = rules->rules[i];
nkeynes@359
   227
	fprintf( f, "Match: %08X/%08X %s: ", rule->bits, rule->mask, rule->format );
nkeynes@359
   228
	for( j=0; j<rule->operand_count; j++ ) {
nkeynes@359
   229
	    fprintf( f, "%s = (%s)(ir>>%d)&0x%X ", rule->operands[j].name,
nkeynes@359
   230
		     rule->operands[j].is_signed ? "signed" : "unsigned", 
nkeynes@359
   231
		     rule->operands[j].bit_shift,
nkeynes@359
   232
		     (1<<rule->operands[j].bit_count)-1 );
nkeynes@359
   233
	}
nkeynes@359
   234
	fprintf( f, "\n" );
nkeynes@359
   235
    }
nkeynes@359
   236
}
nkeynes@359
   237
nkeynes@359
   238
void dump_rulesubset( struct ruleset *rules, int ruleidx[], int rule_count, FILE *f )
nkeynes@359
   239
{
nkeynes@359
   240
    int i,j;
nkeynes@359
   241
    for( i=0; i<rule_count; i++ ) {
nkeynes@359
   242
	struct rule *rule = rules->rules[ruleidx[i]];
nkeynes@359
   243
	fprintf( f, "Match: %08X/%08X %s: ", rule->bits, rule->mask, rule->format );
nkeynes@359
   244
	for( j=0; j<rule->operand_count; j++ ) {
nkeynes@359
   245
	    fprintf( f, "%s = (%s)(ir>>%d)&0x%X ", rule->operands[j].name,
nkeynes@359
   246
		     rule->operands[j].is_signed ? "signed" : "unsigned", 
nkeynes@359
   247
		     rule->operands[j].bit_shift,
nkeynes@359
   248
		     (1<<rule->operands[j].bit_count)-1 );
nkeynes@359
   249
	}
nkeynes@359
   250
	fprintf( f, "\n" );
nkeynes@359
   251
    }
nkeynes@359
   252
}
.