%{ /* * (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 */ #include #include #include #include "param.h" #include "types.h" #include "pattern.h" #include #include #include "optim.h" #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]; int CBO_instrs[] = { op_adi, op_adu, op_and, op_ior, op_xor /* don't add op_mli and op_mlu! */ }; int patCBO; int rplCBO; /* Forward declarations */ void inithash(void); unsigned hashname(register char *name); void enter(char *name,int value); int mlookup(char* name); int lookup(int comm,int operator,int lnode,int rnode); void printnodes(void); void initio(void); void outpat(int exprno, int instrno); void outbyte(int b); void outshort(int s); void out(int w); int yylex(void); void yyerror(const char*); %} %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(void) { register 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 hashname(register char *name) { register unsigned h; h = (*name++)&BMASK; h = (h<<4)^((*name++)&BMASK); h = (h<<4)^((*name++)&BMASK); return(h); } void enter(char *name,int value) { register unsigned 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) { register unsigned 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(void) { inithash(); initio(); yyparse(); if (nerrors==0) printnodes(); return nerrors; } int yywrap(void) { return 1; } void yyerror(const char *s) { fprintf(stderr,"line %d: %s\n",lino,s); nerrors++; } int lookup(int comm,int operator,int lnode,int rnode) { register 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(void) { register 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("static char rcsid[] = %s;\n",patid); } void initio(void) { register 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); } }