filename | src/tools/actparse.c |
changeset | 359:c588dce7ebde |
next | 420:e6f43dec3cf0 |
author | nkeynes |
date | Tue Sep 04 08:32:10 2007 +0000 (16 years ago) |
permissions | -rw-r--r-- |
last change | Change sh4x86 test to translate/disasm full basic blocks Add prelim sym-tab support |
file | annotate | diff | log | raw |
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +00001.2 +++ b/src/tools/actparse.c Tue Sep 04 08:32:10 2007 +00001.3 @@ -0,0 +1,123 @@1.4 +#include <stdlib.h>1.5 +#include <stdio.h>1.6 +#include <string.h>1.7 +#include <ctype.h>1.8 +#include <sys/stat.h>1.9 +#include <glib/gstrfuncs.h>1.10 +#include "tools/gendec.h"1.11 +1.12 +static int yyline;1.13 +1.14 +struct rule *new_action() {1.15 + struct action *action = malloc( sizeof( struct action ) );1.16 + memset( action, 0, sizeof( struct action ) );1.17 + return action;1.18 +}1.19 +1.20 +int add_action( struct actionset *actions, struct ruleset *rules, char *operation, char *action )1.21 +{1.22 + char *act = g_strchomp(action);1.23 +1.24 + char opclean[strlen(operation)];1.25 + char *p = operation, *q = opclean;1.26 + int i;1.27 +1.28 + // Strip c-style comments1.29 + while( *p ) {1.30 + if( *p == '/' && *(p+1) == '*' ) {1.31 + p+=2;1.32 + while( *p ) {1.33 + if( *p == '*' && *(p+1) == '/' ) {1.34 + p+=2;1.35 + break;1.36 + }1.37 + p++;1.38 + }1.39 + } else if( *p == '/' && *(p+1) == '/' ) {1.40 + p+=2;1.41 + while( *p && *p != '\n' ) {1.42 + p++;1.43 + }1.44 + } else {1.45 + *q++ = *p++;1.46 + }1.47 + }1.48 + *q = '\0';1.49 + strcpy( operation, g_strstrip(opclean) );1.50 +1.51 + for( i=0; i<rules->rule_count; i++ ) {1.52 + if( strcasecmp(rules->rules[i]->format, operation) == 0 ) {1.53 + if( actions->actions[i] != NULL ) {1.54 + fprintf( stderr, "Duplicate actions for operation '%s'\n", operation );1.55 + return -1;1.56 + }1.57 + actions->actions[i] = act;1.58 + return 0;1.59 + }1.60 + }1.61 + fprintf(stderr, "No operation found matching '%s'\n", operation );1.62 + return -1;1.63 +}1.64 +1.65 +1.66 +struct actionset *parse_action_file( struct ruleset *rules, FILE *f )1.67 +{1.68 + struct actionset *actions = malloc( sizeof(struct actionset ) );1.69 + struct stat st;1.70 + char *text;1.71 + int i, length;1.72 +1.73 + memset( actions, 0, sizeof( struct actionset ) );1.74 + /* Read whole file in (for convenience) */1.75 + fstat( fileno(f), &st );1.76 + length = st.st_size;1.77 + text = malloc( length+1 );1.78 + fread( text, length, 1, f );1.79 + text[length] = '\0';1.80 + yyline = 0;1.81 + actions->pretext = text;1.82 + for( i=0; i<length; i++ ) {1.83 + if( text[i] == '\n' ) {1.84 + yyline++;1.85 + if( i+3 < length && text[i+1] == '%' && text[i+2] == '%' ) {1.86 + text[i+1] = '\0';1.87 + i+=3;1.88 + break;1.89 + }1.90 + }1.91 + }1.92 +1.93 + char *operation = &text[i];1.94 + for( ; i<length; i++ ) {1.95 + if( text[i] == '\n' ) {1.96 + yyline++;1.97 + if( i+3 < length && text[i+1] == '%' && text[i+2] == '%' ) {1.98 + i+=3;1.99 + break;1.100 + }1.101 + }1.102 +1.103 + if( text[i] == '{' && text[i+1] == ':' ) {1.104 + text[i] = '\0';1.105 + i+=2;1.106 + char *action = &text[i];1.107 + for( ;i<length; i++ ) {1.108 + if( text[i] == ':' && text[i+1] == '}' ) {1.109 + text[i] = '\0';1.110 + i++;1.111 + if( add_action( actions, rules, operation, action ) != 0 ) {1.112 + free(actions);1.113 + free(text);1.114 + return NULL;1.115 + }1.116 + operation = &text[i+1];1.117 + break;1.118 + }1.119 + }1.120 + }1.121 + }1.122 +1.123 + actions->posttext = &text[i];1.124 +1.125 + return actions;1.126 +}
.