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 (11 years ago)
permissions -rw-r--r--
last change Re-indent everything consistently
Fix include guards for consistency as well
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@736
    52
        yyline++;
nkeynes@736
    53
        if( strncasecmp(buf, "registers", 9) == 0 ) {
nkeynes@736
    54
            parse_registers_block(buf, sizeof(buf), f);
nkeynes@736
    55
        } else if( buf[0] != '\0' && buf[0] != '#' && buf[0] != '\n' ) {
nkeynes@736
    56
            struct rule *rule;
nkeynes@736
    57
            char *p = buf;
nkeynes@736
    58
            rule = new_rule();
nkeynes@736
    59
            if( parse_rule( &p, rule ) != 0 ) {
nkeynes@736
    60
                free( rule );
nkeynes@736
    61
            } else {
nkeynes@736
    62
                rules->rules[rules->rule_count++] = rule;
nkeynes@736
    63
            }
nkeynes@736
    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@736
    71
        if( strchr(buf, '}') != NULL ) {
nkeynes@736
    72
            break;
nkeynes@736
    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@736
    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@736
    91
        fprintf( stderr, "Unexpected end of file in rule on line %d\n", yyline );
nkeynes@736
    92
        return -1;
nkeynes@359
    93
    }
nkeynes@736
    94
nkeynes@359
    95
    int result = parse_rule_format( str, rule );
nkeynes@359
    96
    if( result == 0 ) {
nkeynes@736
    97
        /* Reverse operand bit shifts */
nkeynes@736
    98
        int j;
nkeynes@736
    99
        for( j=0; j<rule->operand_count; j++ ) {
nkeynes@736
   100
            rule->operands[j].bit_shift = 
nkeynes@736
   101
                rule->bit_count - rule->operands[j].bit_shift - rule->operands[j].bit_count;
nkeynes@736
   102
        }
nkeynes@736
   103
        if( **str == '!' ) {
nkeynes@736
   104
            (*str)++;
nkeynes@736
   105
            result = parse_operand_uses( str, rule );
nkeynes@736
   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@736
   115
        int ch = **str;
nkeynes@736
   116
        (*str)++;
nkeynes@736
   117
        switch( ch ) {
nkeynes@736
   118
        case '0':
nkeynes@736
   119
            rule->bits = rule->bits << 1;
nkeynes@736
   120
            rule->mask = (rule->mask << 1) | 1;
nkeynes@736
   121
            rule->bit_count++;
nkeynes@736
   122
            break;
nkeynes@736
   123
        case '1':
nkeynes@736
   124
            rule->bits = (rule->bits << 1) | 1;
nkeynes@736
   125
            rule->mask = (rule->mask << 1) | 1;
nkeynes@736
   126
            rule->bit_count++;
nkeynes@736
   127
            break;
nkeynes@736
   128
        case '(':
nkeynes@736
   129
            if( parse_bitoperand( str, rule ) != 0 ) {
nkeynes@736
   130
                return -1 ;
nkeynes@736
   131
            }
nkeynes@736
   132
            break;
nkeynes@736
   133
        default:
nkeynes@736
   134
            (*str)--;
nkeynes@736
   135
            fprintf( stderr, "Unexpected character '%c' in bitstring at line %d\n", ch, yyline );
nkeynes@736
   136
            return -1;
nkeynes@736
   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@736
   147
        fprintf( stderr, "Maximum operands/rule exceeded (%d) at line %d\n", MAX_OPERANDS, yyline );
nkeynes@736
   148
        return -1;
nkeynes@359
   149
    }
nkeynes@359
   150
nkeynes@359
   151
    while( isalnum(**str) || **str == '_' ) {
nkeynes@736
   152
        *p++ = *(*str)++;
nkeynes@359
   153
    }
nkeynes@359
   154
    *p = '\0';
nkeynes@359
   155
    CONSUME_CHAR(':');
nkeynes@736
   156
nkeynes@359
   157
    int size = parse_integer( str );
nkeynes@359
   158
    if( size == -1 ) {
nkeynes@736
   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@736
   163
        (*str)++;
nkeynes@736
   164
        rule->operands[rule->operand_count].is_signed = 1;
nkeynes@359
   165
    } else if( **str == 'u' || **str == 'U' ) {
nkeynes@736
   166
        (*str)++;
nkeynes@736
   167
        rule->operands[rule->operand_count].is_signed = 0;
nkeynes@359
   168
    }
nkeynes@359
   169
    if( **str == '<' ) {
nkeynes@736
   170
        (*str)++;
nkeynes@736
   171
        CONSUME_CHAR('<');
nkeynes@736
   172
        int lsl = parse_integer(str);
nkeynes@736
   173
        if( lsl == -1 ) {
nkeynes@736
   174
            return -1;
nkeynes@736
   175
        }
nkeynes@736
   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@736
   192
        fprintf(stderr, "Expected digit (0-9) but was '%c' at line %d\n", **str, yyline );
nkeynes@736
   193
        return -1;
nkeynes@359
   194
    }
nkeynes@359
   195
    do {
nkeynes@736
   196
        val = val * 10 + (**str - '0');
nkeynes@736
   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@736
   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@736
   226
        struct rule *rule = rules->rules[i];
nkeynes@736
   227
        fprintf( f, "Match: %08X/%08X %s: ", rule->bits, rule->mask, rule->format );
nkeynes@736
   228
        for( j=0; j<rule->operand_count; j++ ) {
nkeynes@736
   229
            fprintf( f, "%s = (%s)(ir>>%d)&0x%X ", rule->operands[j].name,
nkeynes@736
   230
                    rule->operands[j].is_signed ? "signed" : "unsigned", 
nkeynes@736
   231
                            rule->operands[j].bit_shift,
nkeynes@736
   232
                            (1<<rule->operands[j].bit_count)-1 );
nkeynes@736
   233
        }
nkeynes@736
   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@736
   242
        struct rule *rule = rules->rules[ruleidx[i]];
nkeynes@736
   243
        fprintf( f, "Match: %08X/%08X %s: ", rule->bits, rule->mask, rule->format );
nkeynes@736
   244
        for( j=0; j<rule->operand_count; j++ ) {
nkeynes@736
   245
            fprintf( f, "%s = (%s)(ir>>%d)&0x%X ", rule->operands[j].name,
nkeynes@736
   246
                    rule->operands[j].is_signed ? "signed" : "unsigned", 
nkeynes@736
   247
                            rule->operands[j].bit_shift,
nkeynes@736
   248
                            (1<<rule->operands[j].bit_count)-1 );
nkeynes@736
   249
        }
nkeynes@736
   250
        fprintf( f, "\n" );
nkeynes@359
   251
    }
nkeynes@359
   252
}
.