%{ #ifndef NORCSID static char rcsid[] = "$Header$"; #endif #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 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]; %} %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' { register i; outbyte(0); outshort(prevind); prevind=curind-3; out(patlen); for (i=0;imaxpatlen) maxpatlen=patlen; } | error '\n' { yyerrok; } ; replacement : expr /* special optimization */ { #ifdef ALLOWSPECIAL rpllen=1; rplmnem[0]=0; rplexpr[0]=$1; #else yyerror("No specials allowed"); #endif } | repllist ; repllist: /* empty */ { rpllen=0; } | repllist repl ; repl : MNEM optexpr { rplmnem[rpllen] = $1; rplexpr[rpllen++] = $2; } ; mnemlist: MNEM { patlen=0; patmnem[patlen++] = $1; } | mnemlist MNEM { patmnem[patlen++] = $2; } ; optexpr : /* empty */ { $$ = 0; } | expr ; expr : '$' argno { $$ = lookup(0,EX_ARG,$2,0); } | NUMBER { $$ = lookup(0,EX_CON,(int)(short)$1,0); } | PSIZE { $$ = lookup(0,EX_POINTERSIZE,0,0); } | WSIZE { $$ = lookup(0,EX_WORDSIZE,0,0); } | DEFINED '(' expr ')' { $$ = lookup(0,EX_DEFINED,$3,0); } | SAMESIGN '(' expr ',' expr ')' { $$ = lookup(1,EX_SAMESIGN,$3,$5); } | SFIT '(' expr ',' expr ')' { $$ = lookup(0,EX_SFIT,$3,$5); } | UFIT '(' expr ',' expr ')' { $$ = lookup(0,EX_UFIT,$3,$5); } | ROTATE '(' expr ',' expr ')' { $$ = lookup(0,EX_ROTATE,$3,$5); } | NOTREG '(' expr ')' { $$ = lookup(0,EX_NOTREG,$3,0); } | ROM '(' argno ',' expr ')' { $$ = lookup(0,EX_ROM,$3,$5); } | '(' expr ')' { $$ = $2; } | expr CMPEQ expr { $$ = lookup(1,EX_CMPEQ,$1,$3); } | expr CMPNE expr { $$ = lookup(1,EX_CMPNE,$1,$3); } | expr CMPGT expr { $$ = lookup(0,EX_CMPGT,$1,$3); } | expr CMPGE expr { $$ = lookup(0,EX_CMPGE,$1,$3); } | expr CMPLT expr { $$ = lookup(0,EX_CMPLT,$1,$3); } | expr CMPLE expr { $$ = lookup(0,EX_CMPLE,$1,$3); } | expr OR2 expr { $$ = lookup(0,EX_OR2,$1,$3); } | expr AND2 expr { $$ = lookup(0,EX_AND2,$1,$3); } | expr OR1 expr { $$ = lookup(1,EX_OR1,$1,$3); } | expr XOR1 expr { $$ = lookup(1,EX_XOR1,$1,$3); } | expr AND1 expr { $$ = lookup(1,EX_AND1,$1,$3); } | expr ARPLUS expr { $$ = lookup(1,EX_PLUS,$1,$3); } | expr ARMINUS expr { $$ = lookup(0,EX_MINUS,$1,$3); } | expr ARTIMES expr { $$ = lookup(1,EX_TIMES,$1,$3); } | expr ARDIVIDE expr { $$ = lookup(0,EX_DIVIDE,$1,$3); } | expr ARMOD expr { $$ = lookup(0,EX_MOD,$1,$3); } | expr LSHIFT expr { $$ = lookup(0,EX_LSHIFT,$1,$3); } | expr RSHIFT expr { $$ = lookup(0,EX_RSHIFT,$1,$3); } | ARPLUS expr %prec UMINUS { $$ = $2; } | ARMINUS expr %prec UMINUS { $$ = lookup(0,EX_UMINUS,$2,0); } | NOT expr { $$ = lookup(0,EX_NOT,$2,0); } | COMP expr { $$ = lookup(0,EX_COMP,$2,0); } ; argno : NUMBER { if ($1<1 || $1>patlen) { 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]; inithash() { register i; enter("lab",op_lab); enter("LLP",op_LLP); enter("LEP",op_LEP); enter("SLP",op_SLP); enter("SEP",op_SEP); for(i=0;i<=sp_lmnem-sp_fmnem;i++) enter(em_mnem[i],i+sp_fmnem); } unsigned hashname(name) register char *name; { register unsigned h; h = (*name++)&BMASK; h = (h<<4)^((*name++)&BMASK); h = (h<<4)^((*name++)&BMASK); return(h); } enter(name,value) char *name; { 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(name) 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 */ } main() { inithash(); initio(); yyparse(); if (nerrors==0) printnodes(); return nerrors; } yyerror(s) char *s; { fprintf(stderr,"line %d: %s\n",lino,s); nerrors++; } lookup(comm,operator,lnode,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); } printnodes() { 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); if (patid[0]) printf("static char rcsid[] = %s;\n",patid); } initio() { register i; printf("#include \"param.h\"\n#include \"types.h\"\n"); printf("#include \"pattern.h\"\n\n"); for(i=0;i>8)&0377); } out(w) { if (w<255) { outbyte(w); } else { outbyte(255); outshort(w); } } #include "scan.c"