%{ #include #include #include #include "param.h" #include "types.h" #include "pattern.h" #include #include #include "optim.h" /* * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". * * Author: Hans van Staveren */ #define op_CBO (op_plast+1) #define MAXNODES 1000 expr_t nodes[MAXNODES]; expr_p lastnode = nodes+1; int curind,prevind; int patlen,maxpatlen,rpllen; int lino = 1; int patno=1; #define MAX 100 int patmnem[MAX],rplmnem[MAX],rplexpr[MAX]; byte nparam[N_EX_OPS]; bool nonumlab[N_EX_OPS]; bool onlyconst[N_EX_OPS]; int nerrors=0; char patid[128]; /* fileno is not C89 and can be missing sometimes. */ #ifdef __LINUX__ int fileno(FILE *stream); #endif int CBO_instrs[] = { op_adi, op_adu, op_and, op_ior, op_xor /* don't add op_mli and op_mlu! */ }; int yylex(void); int lookup(int comm, int operator, int lnode, int rnode); void enter(char *name, int value); void yyerror(char *s); void printnodes(); void initio(); void outpat(int exprno, int instrno); void outbyte(int b); void outshort(int s); void out(int w); int patCBO; int rplCBO; %} %union { int y_int; } %left OR2 %left AND2 %left OR1 %left XOR1 %left AND1 %left CMPEQ CMPNE %left CMPLT CMPLE CMPGT CMPGE %left RSHIFT LSHIFT %left ARPLUS ARMINUS %left ARTIMES ARDIVIDE ARMOD %nonassoc NOT COMP UMINUS %nonassoc '$' %token SFIT UFIT NOTREG PSIZE WSIZE DEFINED SAMESIGN ROM ROTATE STRING %token MNEM %token NUMBER %type expr argno optexpr %start patternlist %% patternlist : /* empty */ | STRING '\n' | patternlist '\n' | patternlist pattern ; pattern : mnemlist optexpr ':' replacement '\n' { if (patCBO) { register int i; if (! rplCBO) { yyerror("No CBO in replacement"); } for (i=0; ipatlen) { YYERROR; } $$ = (int) $1; } ; %% extern char em_mnem[][4]; #define HASHSIZE (2*(sp_lmnem-sp_fmnem)) struct hashmnem { char h_name[3]; byte h_value; } hashmnem[HASHSIZE]; void inithash() { int i; enter("lab",op_lab); enter("LLP",op_LLP); enter("LEP",op_LEP); enter("SLP",op_SLP); enter("SEP",op_SEP); enter("CBO",op_CBO); for(i=0;i<=sp_lmnem-sp_fmnem;i++) enter(em_mnem[i],i+sp_fmnem); } unsigned int hashname(char *name) { unsigned int h; h = (*name++)&BMASK; h = (h<<4)^((*name++)&BMASK); h = (h<<4)^((*name++)&BMASK); return(h); } void enter(char *name, int value) { unsigned int h; h=hashname(name)%HASHSIZE; while (hashmnem[h].h_name[0] != 0) h = (h+1)%HASHSIZE; strncpy(hashmnem[h].h_name,name,3); hashmnem[h].h_value = value; } int mlookup(char *name) { unsigned int h; h = hashname(name)%HASHSIZE; while (strncmp(hashmnem[h].h_name,name,3) != 0 && hashmnem[h].h_name[0] != 0) h = (h+1)%HASHSIZE; return(hashmnem[h].h_value&BMASK); /* 0 if not found */ } int main(int argc, char *argv[]) { inithash(); initio(); yyparse(); if (nerrors==0) printnodes(); return nerrors; } void yyerror(char *s) { fprintf(stderr,"line %d: %s\n",lino,s); nerrors++; } int lookup(int comm, int operator, int lnode, int rnode) { expr_p p; for (p=nodes+1;pex_operator != operator) continue; if (!((p->ex_lnode == lnode && p->ex_rnode == rnode) || (comm && p->ex_lnode == rnode && p->ex_rnode == lnode))) continue; return(p-nodes); } if (lastnode >= &nodes[MAXNODES]) yyerror("node table overflow"); lastnode++; p->ex_operator = operator; p->ex_lnode = lnode; p->ex_rnode = rnode; return(p-nodes); } void printnodes() { expr_p p; printf("};\n\nshort lastind = %d;\n\nexpr_t enodes[] = {\n",prevind); for (p=nodes;pex_operator,p->ex_lnode,p->ex_rnode); printf("};\n\niarg_t iargs[%d];\n", (maxpatlen>0 ? maxpatlen : 1)); if (patid[0]) printf("/*rcsid: %s*/\n",patid); } void initio() { int i; printf("#include \"param.h\"\n#include \"types.h\"\n"); printf("#include \"pattern.h\"\n\n"); for(i=0;imaxpatlen) maxpatlen=patlen; } void outbyte(int b) { printf(",%3d",b); curind++; } void outshort(int s) { outbyte(s&0377); outbyte((s>>8)&0377); } void out(int w) { if (w<255) { outbyte(w); } else { outbyte(255); outshort(w); } } #include "scan.c"