diff --git a/modules/src/em_opt/findworst.c b/modules/src/em_opt/findworst.c index 541a1297d..10b945120 100644 --- a/modules/src/em_opt/findworst.c +++ b/modules/src/em_opt/findworst.c @@ -6,30 +6,30 @@ static char rcsidp3[] = "$Id$"; #define UPDATEWORST(backups) if(backups>mostbackups) mostbackups = backups; -PRIVATE int leftmatch(); -PRIVATE int rightmatch(); +PRIVATE int rightmatch(struct mnems,struct mnems,int, int); +PRIVATE int leftmatch(struct mnems, struct mnems, int, int); -findworst(patt,repl) - struct mnems patt,repl; + +void findworst(struct mnems patt,struct mnems repl) { /* - /* Find the pattern that requires the most backup of output queue. - /* Let repl be r1 r2 ... rn. All these are already on the output queue. - /* Possibilities in order of most backup first are: - /* a) pattern of form: p1 .... pb r1 r2 .... rn pc ... pd - /* i.e. completely in pattern. - /* requires a backup of b+n instructions - /* and a goto to state 0. - /* b) pattern of form: p1 .... pb r1 r2 .... ri - /* i.e. a prefix of ends a pattern. - /* requires a backup of b+n instructions - /* and a goto to state 0. - /* c) pattern of form: ri ri+1 ... rn pc ... pd - /* i.e. a suffix of starts a pattern. - /* requires a backup of j-i+1 instructions and a goto to state 0. - /* d) pattern of the form: ri ri+1 ... rj - /* i.e. a substring of is a complete pattern - /* requires a backup of j-i+1 instructions and a goto to state 0. + / Find the pattern that requires the most backup of output queue. + / Let repl be r1 r2 ... rn. All these are already on the output queue. + / Possibilities in order of most backup first are: + / a) pattern of form: p1 .... pb r1 r2 .... rn pc ... pd + / i.e. completely in pattern. + / requires a backup of b+n instructions + / and a goto to state 0. + / b) pattern of form: p1 .... pb r1 r2 .... ri + / i.e. a prefix of ends a pattern. + / requires a backup of b+n instructions + / and a goto to state 0. + / c) pattern of form: ri ri+1 ... rn pc ... pd + / i.e. a suffix of starts a pattern. + / requires a backup of j-i+1 instructions and a goto to state 0. + / d) pattern of the form: ri ri+1 ... rj + / i.e. a substring of is a complete pattern + / requires a backup of j-i+1 instructions and a goto to state 0. */ int n = repl.m_len; int diff = patt.m_len - repl.m_len; @@ -45,7 +45,7 @@ findworst(patt,repl) if(actions[s]==(struct action *)NULL) continue; /* look for case a */ - if(first=rightmatch(patterns[s],repl,1,n)) { + if( (first=rightmatch(patterns[s],repl,1,n))) { UPDATEWORST(first-1+n); } /* look for case b */ @@ -76,16 +76,14 @@ findworst(patt,repl) fprintf(ofile,"\t\tOO_mkrepl(%d,%d,%d);\n",n,diff,mostbackups); } -findfail(state,resout,rescpy,resgto) - int state; - int *resout, *rescpy, *resgto; +void findfail(int state, int *resout, int *rescpy, int *resgto) { /* - /* If pattern matching fails in 'state', how many outputs and how many - /* push backs are requires. If pattern is of the form p1 p2 .... pn - /* look for patterns of the form p2 p3 ... pn; then p3 p4 ... pn; etc. - /* The first such match of the form pi pi+1 ... pn requires an output - /* of p1 p2 ... pi-1 and a push back of pn pn-1 ... pi. + / If pattern matching fails in 'state', how many outputs and how many + / push backs are requires. If pattern is of the form p1 p2 .... pn + / look for patterns of the form p2 p3 ... pn; then p3 p4 ... pn; etc. + / The first such match of the form pi pi+1 ... pn requires an output + / of p1 p2 ... pi-1 and a push back of pn pn-1 ... pi. */ int s,i; struct state *p; @@ -114,15 +112,12 @@ findfail(state,resout,rescpy,resgto) *resgto = 0; } -PRIVATE int -leftmatch(patt,repl,i,j) - struct mnems patt,repl; - int i,j; +PRIVATE int leftmatch(struct mnems patt,struct mnems repl,int i,int j) { /* - /* Return the first complete match of the mnems of - /* 'repl' in the mnems of 'patt'. Find the leftmost match. - /* Return 0 if fails. + / Return the first complete match of the mnems of + / 'repl' in the mnems of 'patt'. Find the leftmost match. + / Return 0 if fails. */ int lenrij = j-i+1; int lastpos = patt.m_len - lenrij + 1; @@ -139,15 +134,12 @@ leftmatch(patt,repl,i,j) return(0); } -PRIVATE int -rightmatch(patt,repl,i,j) - struct mnems patt,repl; - int i,j; +PRIVATE int rightmatch(struct mnems patt,struct mnems repl,int i,int j) { /* - /* Return the first complete match of the mnems of - /* 'repl' in the mnems of 'patt'. Find the rightmost match. - /* Return 0 if fails. + / Return the first complete match of the mnems of + / 'repl' in the mnems of 'patt'. Find the rightmost match. + / Return 0 if fails. */ int lenrij = j-i+1; int lastpos = patt.m_len - lenrij + 1; diff --git a/modules/src/em_opt/initlex.c b/modules/src/em_opt/initlex.c index 0233ac702..b5431bd9e 100644 --- a/modules/src/em_opt/initlex.c +++ b/modules/src/em_opt/initlex.c @@ -13,9 +13,9 @@ static char rcsidp2[] = "$Id$"; extern char em_flag[]; extern char em_mnem[][4]; -PRIVATE idinit(); +PRIVATE void idinit(char *, int, int); -initlex() +void initlex(void) { register int i,j; init_idf(); @@ -47,11 +47,8 @@ initlex() } } -PRIVATE -idinit(tag,opcode,argfmt) - char *tag; - int opcode; - int argfmt; + +PRIVATE void idinit(char *tag, int opcode, int argfmt) { struct idf *p; p = str2idf(tag,0); diff --git a/modules/src/em_opt/makefuns.awk b/modules/src/em_opt/makefuns.awk index 31d25f516..005026021 100644 --- a/modules/src/em_opt/makefuns.awk +++ b/modules/src/em_opt/makefuns.awk @@ -3,56 +3,73 @@ BEGIN { seenproc = 0; CC="${CMD}" if (prototypes == "") prototypes = "prototypes" + print "#include \"nopt.h\"" } /^%/ {} /^$/ {} /^[a-z]/ && $3 !~ /.*NOTIMPLEMENTED.*/ { if(seenproc) { print "}" - print "--EOF--" - printf "if %s O_%s.c\n",CC,nam - printf "then :\nelse exit 1\nfi\n" - printf "rm -f O_%s.c\n",nam - } + } seenproc = 1 $1 = substr($1,1,index($1,"\t")-1); nam = $1 - printf "cat > O_%s.c << '--EOF--'\n",$1 - print "#include \"nopt.h\"" + print "" printf "void O_%s(",$1 prototype = "_PROTOTYPE(void O_" $1 ", (" - nparms = split($2,parms,":"); - for(p=1;p 1) { - prototype = prototype parms[1] - printf "\t%s",parms[1] - } - else { - prototype = prototype "void" - } - for(p=1;p 0) + { + nparms = split(p2[i],params,":"); + if (nparms >0 ) + { + gsub(/^[ \t]+/,"",params[1]) + gsub(/[ \t]+$/,"",params[1]) + gsub(/^[ \t]+/,"",params[2]) + gsub(/[ \t]+$/,"",params[2]) + + paramstr = paramstr params[1] " " params[2] + printf "%s %s",params[1],params[2] + if(count!=i) + { + paramstr = paramstr ", " + printf ", " + } + } else + { + printf "void" + paramstr = paramstr "void" + } + } } } + prototype = prototype paramstr + print ")" print prototype "));" >> prototypes if($3) { printf "{\n\t%s\n",$3 } else { printf "{\n" - } + } } /^ / { print @@ -60,9 +77,6 @@ BEGIN { END { if(seenproc) { print "}" - print "--EOF--" - printf "if %s O_%s.c\n",CC,nam - printf "then :\nelse exit 1\nfi\n" - printf "rm -f O_%s.c\n",nam + print "" } } diff --git a/modules/src/em_opt/mkstrct.c b/modules/src/em_opt/mkstrct.c index 7d50b29eb..fd15948e1 100644 --- a/modules/src/em_opt/mkstrct.c +++ b/modules/src/em_opt/mkstrct.c @@ -4,20 +4,16 @@ static char rcsid3[] = "$Id$"; #include "nopt.h" -void -EM_mkop(p,opcode) - register p_instr p; - int opcode; +void EM_mkop(p, opcode) + register p_instr p;int opcode; { p->em_type = EM_MNEM; p->em_opcode = opcode; p->em_argtype = 0; } -void -EM_mknarg(p,opcode) - register p_instr p; - int opcode; +void EM_mknarg(p, opcode) + register p_instr p;int opcode; { p->em_type = EM_MNEM; p->em_opcode = opcode; @@ -25,11 +21,8 @@ EM_mknarg(p,opcode) p->em_cst = 0; } -void -EM_mkilb(p,opcode,lab) - register p_instr p; - int opcode; - label lab; +void EM_mkilb(p, opcode, lab) + register p_instr p;int opcode;label lab; { p->em_type = EM_MNEM; p->em_argtype = ilb_ptyp; @@ -37,11 +30,8 @@ EM_mkilb(p,opcode,lab) p->em_ilb = lab; } -void -EM_mknof(p,opcode,lab,off) - register p_instr p; - int opcode; - label lab; +void EM_mknof(p, opcode, lab, off) + register p_instr p;int opcode;label lab; arith off; { p->em_type = EM_MNEM; @@ -51,24 +41,19 @@ EM_mknof(p,opcode,lab,off) p->em_off = off; } -void -EM_mksof(p,opcode,name,off) - register p_instr p; - int opcode; - char *name; +void EM_mksof(p, opcode, name, off) + register p_instr p;int opcode;char *name; arith off; { p->em_type = EM_MNEM; p->em_argtype = sof_ptyp; p->em_opcode = opcode; - p->em_dnam = OO_freestr(name); + p->em_dnam = OO_freestr(name); p->em_off = off; } -void -EM_mkcst(p,opcode,cst) - register p_instr p; - int opcode; +void EM_mkcst(p, opcode, cst) + register p_instr p;int opcode; arith cst; { p->em_type = EM_MNEM; @@ -77,11 +62,8 @@ EM_mkcst(p,opcode,cst) p->em_cst = cst; } -void -EM_mkpro(p,opcode,pnam) - register p_instr p; - int opcode; - char *pnam; +void EM_mkpro(p, opcode, pnam) + register p_instr p;int opcode;char *pnam; { p->em_type = EM_MNEM; p->em_argtype = pro_ptyp; @@ -89,11 +71,8 @@ EM_mkpro(p,opcode,pnam) p->em_pnam = OO_freestr(pnam); } -void -EM_mkdefilb(p,opcode,deflb) - register p_instr p; - int opcode; - label deflb; +void EM_mkdefilb(p, opcode, deflb) + register p_instr p;int opcode;label deflb; { p->em_type = EM_DEFILB; p->em_opcode = opcode; @@ -101,8 +80,7 @@ EM_mkdefilb(p,opcode,deflb) p->em_ilb = deflb; } -void -EM_Nop(opcode) +void EM_Nop(opcode) int opcode; { register p_instr p = GETNXTPATT(); @@ -111,8 +89,7 @@ EM_Nop(opcode) p->em_argtype = 0; } -void -EM_Nnarg(opcode) +void EM_Nnarg(opcode) int opcode; { register p_instr p = GETNXTPATT(); @@ -122,10 +99,8 @@ EM_Nnarg(opcode) p->em_cst = 0; } -void -EM_Nilb(opcode,lab) - int opcode; - label lab; +void EM_Nilb(opcode, lab) + int opcode;label lab; { register p_instr p = GETNXTPATT(); p->em_type = EM_MNEM; @@ -134,10 +109,8 @@ EM_Nilb(opcode,lab) p->em_ilb = lab; } -void -EM_Nnof(opcode,lab,off) - int opcode; - label lab; +void EM_Nnof(opcode, lab, off) + int opcode;label lab; arith off; { register p_instr p = GETNXTPATT(); @@ -148,22 +121,19 @@ EM_Nnof(opcode,lab,off) p->em_off = off; } -void -EM_Nsof(opcode,name,off) - int opcode; - char *name; +void EM_Nsof(opcode, name, off) + int opcode;char *name; arith off; { register p_instr p = GETNXTPATT(); p->em_type = EM_MNEM; p->em_argtype = sof_ptyp; p->em_opcode = opcode; - p->em_dnam = OO_freestr(name); + p->em_dnam = OO_freestr(name); p->em_off = off; } -void -EM_Ncst(opcode,cst) +void EM_Ncst(opcode, cst) int opcode; arith cst; { @@ -174,10 +144,8 @@ EM_Ncst(opcode,cst) p->em_cst = cst; } -void -EM_Npro(opcode,pnam) - int opcode; - char *pnam; +void EM_Npro(opcode, pnam) + int opcode;char *pnam; { register p_instr p = GETNXTPATT(); p->em_type = EM_MNEM; @@ -186,10 +154,8 @@ EM_Npro(opcode,pnam) p->em_pnam = OO_freestr(pnam); } -void -EM_Ndefilb(opcode,deflb) - int opcode; - label deflb; +void EM_Ndefilb(opcode, deflb) + int opcode;label deflb; { register p_instr p = GETNXTPATT(); p->em_type = EM_DEFILB; @@ -198,8 +164,7 @@ EM_Ndefilb(opcode,deflb) p->em_ilb = deflb; } -void -EM_Rop(opcode) +void EM_Rop(opcode) int opcode; { register p_instr p = GETNXTREPL(); @@ -208,8 +173,7 @@ EM_Rop(opcode) p->em_argtype = 0; } -void -EM_Rnarg(opcode) +void EM_Rnarg(opcode) int opcode; { register p_instr p = GETNXTREPL(); @@ -219,10 +183,8 @@ EM_Rnarg(opcode) p->em_cst = 0; } -void -EM_Rilb(opcode,lab) - int opcode; - label lab; +void EM_Rilb(opcode, lab) + int opcode;label lab; { register p_instr p = GETNXTREPL(); p->em_type = EM_MNEM; @@ -231,10 +193,8 @@ EM_Rilb(opcode,lab) p->em_ilb = lab; } -void -EM_Rnof(opcode,lab,off) - int opcode; - label lab; +void EM_Rnof(opcode, lab, off) + int opcode;label lab; arith off; { register p_instr p = GETNXTREPL(); @@ -245,22 +205,19 @@ EM_Rnof(opcode,lab,off) p->em_off = off; } -void -EM_Rsof(opcode,name,off) - int opcode; - char *name; +void EM_Rsof(opcode, name, off) + int opcode;char *name; arith off; { register p_instr p = GETNXTREPL(); p->em_type = EM_MNEM; p->em_argtype = sof_ptyp; p->em_opcode = opcode; - p->em_dnam = OO_freestr(name); + p->em_dnam = OO_freestr(name); p->em_off = off; } -void -EM_Rcst(opcode,cst) +void EM_Rcst(opcode, cst) int opcode; arith cst; { @@ -271,10 +228,8 @@ EM_Rcst(opcode,cst) p->em_cst = cst; } -void -EM_Rpro(opcode,pnam) - int opcode; - char *pnam; +void EM_Rpro(opcode, pnam) + int opcode;char *pnam; { register p_instr p = GETNXTREPL(); p->em_type = EM_MNEM; @@ -283,10 +238,8 @@ EM_Rpro(opcode,pnam) p->em_pnam = OO_freestr(pnam); } -void -EM_Rdefilb(opcode,deflb) - int opcode; - label deflb; +void EM_Rdefilb(opcode, deflb) + int opcode;label deflb; { register p_instr p = GETNXTREPL(); p->em_type = EM_DEFILB; diff --git a/modules/src/em_opt/nopt.c b/modules/src/em_opt/nopt.c index f2e071f86..d10fd18cb 100644 --- a/modules/src/em_opt/nopt.c +++ b/modules/src/em_opt/nopt.c @@ -4,36 +4,36 @@ static char rcsid2[] = "$Id$"; #include "nopt.h" -extern struct dfa OO_checknext[]; /* Initialized in dfa.c */ -extern struct dfa *OO_base[]; /* Initialized in dfa.c */ -extern struct dodefault OO_default[]; /* Initialized in dfa.c */ -extern int OO_maxpattern; /* Initialized in dfa.c */ -extern int OO_maxreplacement; /* Initialized in dfa.c */ -extern int (*OO_ftrans[])(); /* Initialized in trans.c */ +extern struct dfa OO_checknext[]; /* Initialized in dfa.c */ +extern struct dfa *OO_base[]; /* Initialized in dfa.c */ +extern struct dodefault OO_default[]; /* Initialized in dfa.c */ +extern int OO_maxpattern; /* Initialized in dfa.c */ +extern int OO_maxreplacement; /* Initialized in dfa.c */ +extern int (*OO_ftrans[])(); /* Initialized in trans.c */ -extern char em_mnem[][4]; -extern char em_pseu[][4]; +extern char em_mnem[][4]; +extern char em_pseu[][4]; int OO_state = 0; -p_instr OO_buffer; -p_instr OO_patternqueue; -p_instr OO_nxtpatt; -p_instr OO_endbackup; -p_instr OO_nxtrepl; -static p_instr OO_replqueue; +p_instr OO_buffer; +p_instr OO_patternqueue; +p_instr OO_nxtpatt; +p_instr OO_endbackup; +p_instr OO_nxtrepl; +static p_instr OO_replqueue; -static char *filename; -static char *strqueue; -static char *nextstr; -static char *laststr; +static char *filename; +static char *strqueue; +static char *nextstr; +static char *laststr; -arith OO_WSIZE; /* wordlength */ -arith OO_DWSIZE; /* 2*wordlength */ -arith OO_PSIZE; /* pointer length */ +arith OO_WSIZE; /* wordlength */ +arith OO_DWSIZE; /* 2*wordlength */ +arith OO_PSIZE; /* pointer length */ #ifdef STATS -int OO_wrstats = 1; /* pattern statistics output */ +int OO_wrstats = 1; /* pattern statistics output */ #endif #ifdef DEBUG #define printstate(s) dumpstate(s) @@ -42,108 +42,103 @@ int OO_wrstats = 1; /* pattern statistics output */ #endif /* DEBUG */ /**** WHICH IS FASTER? **** -#define BTSCPY(pp,qq,i,p,q,n) btscpy(p,q,(n)*sizeof(struct e_instr)) + #define BTSCPY(pp,qq,i,p,q,n) btscpy(p,q,(n)*sizeof(struct e_instr)) **************************/ #define BTSCPY(pp,qq,i,p,q,n) for(pp=(p),qq=(q),i=(n);i--;*pp++ = *qq++) -PRIVATE void allocmem(); +PRIVATE void allocmem(void); -void -O_init(wsize,psize) - arith wsize, psize; +void O_init(arith wsize, arith psize) { allocmem(); - C_init(wsize,psize); + C_init(wsize, psize); OO_WSIZE = wsize; - OO_DWSIZE = 2*wsize; + OO_DWSIZE = 2 * wsize; OO_PSIZE = psize; } -int -O_open(fname) - char *fname; +int O_open(char *fname) { filename = fname; - return(C_open(fname)); + return (C_open(fname)); } -void -O_magic() +void O_magic(void) { C_magic(); } -void -O_close() +void O_close(void) { C_close(); } -void -OO_dfa(last) - register int last; +void OO_dfa(register int last) { register struct dfa *b; register struct dodefault *d; register int (*f)(); - for(;;) { + for (;;) + { printstate("OO_dfa"); - if((b=OO_base[OO_state]) && ((b += last)->check==OO_state)) { - if(f=OO_ftrans[OO_state = b->next]) (*f)(); + if ((b = OO_base[OO_state]) && ((b += last)->check == OO_state)) + { + if ((f = OO_ftrans[OO_state = b->next])) + (*f)(); } - else if (OO_state) { + else if (OO_state) + { /* consult default entry */ d = &OO_default[OO_state]; - if(!OO_endbackup) OO_endbackup = OO_nxtpatt; + if (!OO_endbackup) + OO_endbackup = OO_nxtpatt; OO_nxtpatt--; OO_patternqueue += d->numout; - if(f=OO_ftrans[OO_state = d->next]) (*f)(); + if ((f = OO_ftrans[OO_state = d->next])) + (*f)(); } - else OO_flush(); - if (!OO_endbackup) return; + else + OO_flush(); + if (!OO_endbackup) + return; last = (OO_nxtpatt++)->em_opcode; if (OO_nxtpatt >= OO_endbackup) OO_endbackup = 0; } } -PRIVATE void -fatal(s,a) - char *s; - int a; +PRIVATE void fatal(s, a) + char *s;int a; { fprint(STDERR, "%s: ", filename ? filename : "standard input"); - fprint(STDERR,s,a); - fprint(STDERR,"\n"); + fprint(STDERR, s, a); + fprint(STDERR, "\n"); sys_stop(S_EXIT); } -PRIVATE void -allocmem() +PRIVATE void allocmem(void) { /* Allocate memory for queues on heap */ - OO_buffer = (p_instr) - Malloc((unsigned)(MAXBUFFER*sizeof(struct e_instr))); + OO_buffer = (p_instr) Malloc( + (unsigned) (MAXBUFFER * sizeof(struct e_instr))); OO_patternqueue = OO_nxtpatt = OO_buffer; - OO_replqueue = (p_instr) - Malloc((unsigned)OO_maxreplacement*sizeof(struct e_instr)); + OO_replqueue = (p_instr) Malloc( + (unsigned) OO_maxreplacement * sizeof(struct e_instr)); OO_nxtrepl = OO_replqueue; - nextstr = strqueue = - (char *)Malloc(MAXSTRING*sizeof(char)); + nextstr = strqueue = (char *) Malloc(MAXSTRING * sizeof(char)); laststr = strqueue + MAXSTRING - 1; } -char * -OO_freestr(str) - char *str; +char * OO_freestr(char *str) { register char *s = str; register char *res; - while (*s++); - again: - if ((s-str) > (laststr-nextstr)) { - unsigned newsize = (laststr - strqueue + 1)*2; - res = Realloc(strqueue,newsize); + while (*s++) + ; + again: if ((s - str) > (laststr - nextstr)) + { + unsigned newsize = (laststr - strqueue + 1) * 2; + res = Realloc(strqueue, newsize); laststr = res + newsize - 1; nextstr = res + (nextstr - strqueue); strqueue = res; @@ -153,201 +148,202 @@ OO_freestr(str) #endif goto again; } - res=nextstr; - for(s=str;*nextstr++ = *s++;); - return(res); + res = nextstr; + for (s = str; (*nextstr++ = *s++);) + ; + return (res); } -void -OO_flush() +void OO_flush(void) { /* - /* Output all instructions waiting in the output queue and free their - /* storage including the saved strings. - */ - register p_instr p,q; - register int i,n; + Output all instructions waiting in the output queue and free their + storage including the saved strings. + */ + register p_instr p, q; + register int i, n; printstate("Flush"); - for(p=OO_buffer;pem_opcode!=OTHER) + if (p->em_opcode != OTHER) C_out(p); - if(OO_endbackup) { - n = OO_endbackup-OO_nxtpatt; - BTSCPY(p,q,i,OO_buffer,OO_nxtpatt,n); + if (OO_endbackup) + { + n = OO_endbackup - OO_nxtpatt; + BTSCPY(p, q, i, OO_buffer, OO_nxtpatt, n); OO_endbackup = OO_buffer + n; } - else nextstr = strqueue; + else + nextstr = strqueue; OO_patternqueue = OO_nxtpatt = OO_buffer; } -p_instr -OO_halfflush() +p_instr OO_halfflush(void) { /* - /* Called when buffer full, flush half the buffer and move the - /* the pattern pointers to the new positions. Return a pointer - /* to the new nxtpatt position and increment it. - /* Note that OO_endbackup is always NIL (i.e. there are no - /* instructions on the backup queue) when this is invoked. - */ - register int i,n; - register p_instr p,q; + Called when buffer full, flush half the buffer and move the + the pattern pointers to the new positions. Return a pointer + to the new nxtpatt position and increment it. + Note that OO_endbackup is always NIL (i.e. there are no + instructions on the backup queue) when this is invoked. + */ + register int i, n; + register p_instr p, q; printstate("Half flush"); n = MAXBUFFER / 2; - for(p=OO_buffer,i=n;i--;) + for (p = OO_buffer, i = n; i--;) C_out(p++); /* now copy the rest of buffer and pattern back */ - BTSCPY(p,q,i,OO_buffer,OO_buffer+n,(OO_nxtpatt-OO_buffer)-n); + BTSCPY(p, q, i, OO_buffer, OO_buffer + n, (OO_nxtpatt - OO_buffer) - n); OO_patternqueue -= n; OO_nxtpatt -= n; printstate("after Half flush"); return (OO_nxtpatt++); } -void -OO_mkext(p,opcode,arg,off) - register p_instr p; - int opcode; - p_instr arg; - arith off; +void OO_mkext(register p_instr p, int opcode, p_instr arg, arith off) { - switch(arg->em_argtype) { - case cst_ptyp: - EM_mkcst(p,opcode,off); - break; - case sof_ptyp: - EM_mksof(p,opcode,arg->em_dnam,off); - break; - case nof_ptyp: - EM_mknof(p,opcode,arg->em_dlb,off); - break; - default: - fatal("Unexpected type %d in outext",arg->em_argtype); + switch (arg->em_argtype) + { + case cst_ptyp: + EM_mkcst(p, opcode, off); + break; + case sof_ptyp: + EM_mksof(p, opcode, arg->em_dnam, off); + break; + case nof_ptyp: + EM_mknof(p, opcode, arg->em_dlb, off); + break; + default: + fatal("Unexpected type %d in outext", arg->em_argtype); } } -void -OO_mkrepl(lrepl,diff,numbkup) - int lrepl,diff,numbkup; +void OO_mkrepl(int lrepl, int diff, int numbkup) { /* copy the replacement queue into the buffer queue */ /* then move the pattern queue back n places */ - register p_instr p,q; + register p_instr p, q; register int i; printstate("Before backup"); - if(OO_endbackup) { + if (OO_endbackup) + { /* move the region between OO_nxtpatt and OO_endbackup */ - if (diff > 0) { + if (diff > 0) + { /* move left by diff */ - BTSCPY(p,q,i,OO_nxtpatt-diff,OO_nxtpatt,OO_endbackup-OO_nxtpatt); + BTSCPY(p, q, i, OO_nxtpatt - diff, OO_nxtpatt, + OO_endbackup - OO_nxtpatt); OO_nxtpatt -= diff; OO_endbackup -= diff; } - else if (diff < 0) { + else if (diff < 0) + { /* move right by diff */ /* careful of overflowing buffer!! */ - if ((OO_nxtpatt-diff)> (OO_buffer+MAXBUFFER) ) + if ((OO_nxtpatt - diff) > (OO_buffer + MAXBUFFER)) OO_halfflush(); /* cannot use btscpy as strings may overlap */ - p = (q=OO_endbackup-1) - diff; - while(q>=OO_nxtpatt) + p = (q = OO_endbackup - 1) - diff; + while (q >= OO_nxtpatt) *p-- = *q--; OO_nxtpatt -= diff; OO_endbackup -= diff; } } /* copy the replacement */ - if (lrepl) { - BTSCPY(p,q,i,OO_patternqueue,OO_replqueue,lrepl); + if (lrepl) + { + BTSCPY(p, q, i, OO_patternqueue, OO_replqueue, lrepl); OO_nxtrepl = OO_replqueue; OO_patternqueue += lrepl; } /* now move the position of interest back nunbkup instructions */ - if ((OO_patternqueue-OO_buffer) < numbkup) - numbkup = (OO_patternqueue-OO_buffer); + if ((OO_patternqueue - OO_buffer) < numbkup) + numbkup = (OO_patternqueue - OO_buffer); OO_nxtpatt = OO_patternqueue -= numbkup; - if(!OO_endbackup && numbkup) - OO_endbackup = OO_patternqueue+numbkup; + if (!OO_endbackup && numbkup) + OO_endbackup = OO_patternqueue + numbkup; OO_state = 0; printstate("After backup"); } #ifdef DEBUG void -dumpstate(mess) - char *mess; +dumpstate(char *mess) { - p_instr p; + p_instr p; fprintf(stderr,"%s - state(%d): ",mess,OO_state); p = OO_buffer; while(pem_type) { - case EM_MNEM: + switch(p->em_type) + { + case EM_MNEM: fprintf(stderr,"%s ",em_mnem[p->em_opcode-sp_fmnem]); break; - case EM_PSEU: + case EM_PSEU: fprintf(stderr,"%s ",em_pseu[p->em_opcode-sp_fpseu]); break; - case EM_STARTMES: - case EM_MESARG: - case EM_ENDMES: + case EM_STARTMES: + case EM_MESARG: + case EM_ENDMES: fprintf(stderr,"MES "); break; - case EM_DEFILB: + case EM_DEFILB: fprintf(stderr,"%ld ", (long)p->em_ilb); return; - case EM_DEFDLB: + case EM_DEFDLB: fprintf(stderr,"%ld ", (long)p->em_dlb); return; - case EM_DEFDNAM: + case EM_DEFDNAM: fprintf(stderr,"%d ", p->em_dnam); return; - case EM_ERROR: - case EM_FATAL: - case EM_EOF: + case EM_ERROR: + case EM_FATAL: + case EM_EOF: return; } - switch(p->em_argtype) { - case 0: + switch(p->em_argtype) + { + case 0: break; - case cst_ptyp: + case cst_ptyp: fprintf(stderr,"%d ",p->em_cst); break; - case nof_ptyp: + case nof_ptyp: fprintf(stderr,".%d+%d ",p->em_dlb,p->em_off); break; - case sof_ptyp: + case sof_ptyp: fprintf(stderr,"%s+%d ",p->em_dnam,p->em_off); break; - case ilb_ptyp: + case ilb_ptyp: fprintf(stderr,"*%d ",p->em_ilb); break; - case pro_ptyp: + case pro_ptyp: fprintf(stderr,"$%s ",p->em_pnam); break; - case str_ptyp: - case ico_ptyp: - case uco_ptyp: + case str_ptyp: + case ico_ptyp: + case uco_ptyp: fprintf(stderr,"\"%s\"",p->em_string); break; - default: + default: fatal(" prtinst - Unregognized arg %d ",p->em_argtype); } } diff --git a/modules/src/em_opt/outcalls.c b/modules/src/em_opt/outcalls.c index 97281a0b7..cb86ff6fb 100644 --- a/modules/src/em_opt/outcalls.c +++ b/modules/src/em_opt/outcalls.c @@ -4,108 +4,128 @@ static char rcsidp4[] = "$Id$"; #include "parser.h" -outputincalls() +void outputincalls(void) { struct idf *op; char *s; - for(op=ops;op!=(struct idf *)NULL;op=op->id_nextidf) { + for (op = ops; op != (struct idf *) NULL; op = op->id_nextidf) + { s = op->id_text; - switch(op->id_argfmt) { - case NOARG: - fprintf(ofile,"%s\t|\t|\n",s); - if(op->id_used) { - fprintf(ofile,"\tEM_Nop(op_%s);\n",s); - fprintf(ofile,"\tOO_dfa(op_%s);\n",s); - } - else { - fprintf(ofile,"\tFLUSHDFA();\n"); - fprintf(ofile,"\tC_%s();\n",s); - } - break; - case CSTOPT: - fprintf(ofile,"%s_narg\t|\t|\n",s); - if(op->id_used) { - fprintf(ofile,"\tEM_Nnarg(op_%s);\n",s); - fprintf(ofile,"\tOO_dfa(op_%s);\n",s); - } - else { - fprintf(ofile,"\tFLUSHDFA();\n"); - fprintf(ofile,"\tC_%s_narg();\n",s); - } - /* fall thru */ - case CST: - fprintf(ofile,"%s\t| arith:n\t|\n",s); - if(op->id_used) { - fprintf(ofile,"\tEM_Ncst(op_%s,n);\n",s); - fprintf(ofile,"\tOO_dfa(op_%s);\n",s); - } - else { - fprintf(ofile,"\tFLUSHDFA();\n"); - fprintf(ofile,"\tC_%s(n);\n",s); - } - break; - case DEFILB: - fprintf(ofile,"df_ilb\t| label:l\t|\n"); - if(op->id_used) { - fprintf(ofile,"\tEM_Ndefilb(op_%s,l);\n",s); - fprintf(ofile,"\tOO_dfa(op_%s);\n",s); - } - else { - fprintf(ofile,"\tFLUSHDFA();\n"); - fprintf(ofile,"\tC_df_ilb(l);\n",s); - } - break; - case PNAM: - fprintf(ofile,"%s\t| char *:s\t|\n",s); - if(op->id_used) { - fprintf(ofile,"\tEM_Npro(op_%s,s);\n",s); - fprintf(ofile,"\tOO_dfa(op_%s);\n",s); - } - else { - fprintf(ofile,"\tFLUSHDFA();\n"); - fprintf(ofile,"\tC_%s(s);\n",s); - } - break; - case LAB: - fprintf(ofile,"%s\t| label:l\t|\n",s); - if(op->id_used) { - fprintf(ofile,"\tEM_Nilb(op_%s,l);\n",s); - fprintf(ofile,"\tOO_dfa(op_%s);\n",s); - } - else { - fprintf(ofile,"\tFLUSHDFA();\n"); - fprintf(ofile,"\tC_%s(l);\n",s); - } - break; - case EXT: - fprintf(ofile,"%s\t| arith:n\t|\n",s); - if(op->id_used) { - fprintf(ofile,"\tEM_Ncst(op_%s,n);\n",s); - fprintf(ofile,"\tOO_dfa(op_%s);\n",s); - } - else { - fprintf(ofile,"\tFLUSHDFA();\n"); - fprintf(ofile,"\tC_%s(n);\n",s); - } - fprintf(ofile,"%s_dnam\t| char *:s arith:n\t|\n",s); - if(op->id_used) { - fprintf(ofile,"\tEM_Nsof(op_%s,s,n);\n",s); - fprintf(ofile,"\tOO_dfa(op_%s);\n",s); - } - else { - fprintf(ofile,"\tFLUSHDFA();\n"); - fprintf(ofile,"\tC_%s_dnam(s,n);\n",s); - } - fprintf(ofile,"%s_dlb\t| label:l arith:n\t|\n",s); - if(op->id_used) { - fprintf(ofile,"\tEM_Nnof(op_%s,l,n);\n",s); - fprintf(ofile,"\tOO_dfa(op_%s);\n",s); - } - else { - fprintf(ofile,"\tFLUSHDFA();\n"); - fprintf(ofile,"\tC_%s_dlb(l,n);\n",s); - } - break; + switch (op->id_argfmt) + { + case NOARG: + fprintf(ofile, "%s\t|\t|\n", s); + if (op->id_used) + { + fprintf(ofile, "\tEM_Nop(op_%s);\n", s); + fprintf(ofile, "\tOO_dfa(op_%s);\n", s); + } + else + { + fprintf(ofile, "\tFLUSHDFA();\n"); + fprintf(ofile, "\tC_%s();\n", s); + } + break; + case CSTOPT: + fprintf(ofile, "%s_narg\t|\t|\n", s); + if (op->id_used) + { + fprintf(ofile, "\tEM_Nnarg(op_%s);\n", s); + fprintf(ofile, "\tOO_dfa(op_%s);\n", s); + } + else + { + fprintf(ofile, "\tFLUSHDFA();\n"); + fprintf(ofile, "\tC_%s_narg();\n", s); + } + /* fall thru */ + case CST: + fprintf(ofile, "%s\t| arith:n\t|\n", s); + if (op->id_used) + { + fprintf(ofile, "\tEM_Ncst(op_%s,n);\n", s); + fprintf(ofile, "\tOO_dfa(op_%s);\n", s); + } + else + { + fprintf(ofile, "\tFLUSHDFA();\n"); + fprintf(ofile, "\tC_%s(n);\n", s); + } + break; + case DEFILB: + fprintf(ofile, "df_ilb\t| label:l\t|\n"); + if (op->id_used) + { + fprintf(ofile, "\tEM_Ndefilb(op_%s,l);\n", s); + fprintf(ofile, "\tOO_dfa(op_%s);\n", s); + } + else + { + fprintf(ofile, "\tFLUSHDFA();\n"); + fprintf(ofile, "\tC_df_ilb(l);\n", s); + } + break; + case PNAM: + fprintf(ofile, "%s\t| char *:s\t|\n", s); + if (op->id_used) + { + fprintf(ofile, "\tEM_Npro(op_%s,s);\n", s); + fprintf(ofile, "\tOO_dfa(op_%s);\n", s); + } + else + { + fprintf(ofile, "\tFLUSHDFA();\n"); + fprintf(ofile, "\tC_%s(s);\n", s); + } + break; + case LAB: + fprintf(ofile, "%s\t| label:l\t|\n", s); + if (op->id_used) + { + fprintf(ofile, "\tEM_Nilb(op_%s,l);\n", s); + fprintf(ofile, "\tOO_dfa(op_%s);\n", s); + } + else + { + fprintf(ofile, "\tFLUSHDFA();\n"); + fprintf(ofile, "\tC_%s(l);\n", s); + } + break; + case EXT: + fprintf(ofile, "%s\t| arith:n\t|\n", s); + if (op->id_used) + { + fprintf(ofile, "\tEM_Ncst(op_%s,n);\n", s); + fprintf(ofile, "\tOO_dfa(op_%s);\n", s); + } + else + { + fprintf(ofile, "\tFLUSHDFA();\n"); + fprintf(ofile, "\tC_%s(n);\n", s); + } + fprintf(ofile, "%s_dnam\t| char *:s arith:n\t|\n", s); + if (op->id_used) + { + fprintf(ofile, "\tEM_Nsof(op_%s,s,n);\n", s); + fprintf(ofile, "\tOO_dfa(op_%s);\n", s); + } + else + { + fprintf(ofile, "\tFLUSHDFA();\n"); + fprintf(ofile, "\tC_%s_dnam(s,n);\n", s); + } + fprintf(ofile, "%s_dlb\t| label:l arith:n\t|\n", s); + if (op->id_used) + { + fprintf(ofile, "\tEM_Nnof(op_%s,l,n);\n", s); + fprintf(ofile, "\tOO_dfa(op_%s);\n", s); + } + else + { + fprintf(ofile, "\tFLUSHDFA();\n"); + fprintf(ofile, "\tC_%s_dlb(l,n);\n", s); + } + break; } } } diff --git a/modules/src/em_opt/outputdfa.c b/modules/src/em_opt/outputdfa.c index c58c966b9..110bdea2f 100644 --- a/modules/src/em_opt/outputdfa.c +++ b/modules/src/em_opt/outputdfa.c @@ -3,25 +3,31 @@ static char rcsidp5[] = "$Id$"; #endif #include +#include "alloc.h" #include "parser.h" #include "Lpars.h" FILE *ofile; -PRIVATE openofile(); -PRIVATE installofile(); -PRIVATE UNLINK(); -PRIVATE RENAME(); -PRIVATE outdfa(); -PRIVATE outmnems(); -PRIVATE outdotrans(); -PRIVATE outoneaction(); -PRIVATE outrepl(); -PRIVATE outexp(); -PRIVATE outext(); -PRIVATE outop(); +PRIVATE void openofile(char *); +PRIVATE void installofile(void); +PRIVATE void UNLINK(char *); +PRIVATE void RENAME(char *, char *); +PRIVATE void increase_next(unsigned int); +PRIVATE void store_row(int, register int *); +PRIVATE void outdfa(void); +PRIVATE void outmnems(struct mnems); +PRIVATE int sametest(int, int, struct exp_node *, struct exp_node *); +PRIVATE int samerepl(int, int, struct mnems, struct mnems); +PRIVATE int samecode(int, int); +PRIVATE void outdotrans(void); +PRIVATE void outoneaction(int, struct action *); +PRIVATE void outrepl(int, struct mnems); +PRIVATE void outexp(struct exp_node *, int); +PRIVATE void outext(struct exp_node *); +PRIVATE void outop(int op); -outputnopt() +void outputnopt(void) { openofile("dfa.c"); outdfa(); @@ -37,170 +43,173 @@ outputnopt() static char ofilename[80]; static char ofiletemp[80]; -PRIVATE -openofile(filename) - char *filename; +PRIVATE void openofile(char *filename) { - strcpy(ofilename,filename); - strcpy(ofiletemp,filename); - strcat(ofiletemp,".new"); - if((ofile=fopen(ofiletemp,"w"))==NULL) { - fprintf(stderr,"Fatal Error: cannot open output file %s\n",ofiletemp); + strcpy(ofilename, filename); + strcpy(ofiletemp, filename); + strcat(ofiletemp, ".new"); + if ((ofile = fopen(ofiletemp, "w")) == NULL) + { + fprintf(stderr, "Fatal Error: cannot open output file %s\n", ofiletemp); sys_stop(S_EXIT); } } -PRIVATE -installofile() +PRIVATE void installofile(void) { /* * if contents of newly generated ofiletemp is different * from that of ofilename then copy over old file else * delete newly generated file */ - register FILE *f1, *f2; - register int c1, c2; + register FILE *f1, *f2; + register int c1, c2; fclose(ofile); - if((f1 = fopen(ofiletemp,"r")) == NULL) { - fprintf(stderr,"Fatal Error: cannont reopen file %s\n",ofiletemp); + if ((f1 = fopen(ofiletemp, "r")) == NULL) + { + fprintf(stderr, "Fatal Error: cannont reopen file %s\n", ofiletemp); sys_stop(S_EXIT); } - if((f2 = fopen(ofilename,"r")) == NULL) { + if ((f2 = fopen(ofilename, "r")) == NULL) + { fclose(f1); - RENAME(ofiletemp,ofilename); + RENAME(ofiletemp, ofilename); return; } - do { + do + { c1 = getc(f1); c2 = getc(f2); } while (c1 == c2 && c1 != EOF); fclose(f1); fclose(f2); - if (c1 != c2) { - RENAME(ofiletemp,ofilename); + if (c1 != c2) + { + RENAME(ofiletemp, ofilename); } - else UNLINK(ofiletemp); + else + UNLINK(ofiletemp); } -PRIVATE -UNLINK(x) - char *x; +PRIVATE void UNLINK(char *x) { /* Must remove the file "x" */ - unlink(x); /* systemcall to remove file */ + remove(x); /* systemcall to remove file */ } -PRIVATE -RENAME(x,y) - char *x, *y; +PRIVATE void RENAME(char *x, char*y) { /* Must move the file "x" to the file "y" */ - unlink(y); - if(link(x,y)!=0) { - fprintf(stderr,"Cannot link to %s",y); + if (rename(x, y) != 0) + { + fprintf(stderr, "Cannot rename to %s", y); sys_stop(S_EXIT); } - unlink(x); } # define MAXOPCODE 255 # define EMPTY -1 int *next, *check, *base; -unsigned currsize; /* current size of next and check arrays */ -int maxpos = 0; /* highest used position in these arrayes */ +unsigned currsize; /* current size of next and check arrays */ +int maxpos = 0; /* highest used position in these arrayes */ -PRIVATE -increase_next(size) - int size; +PRIVATE void increase_next(unsigned int size) { /* realloc arrays next and check so they are at least * of size 'size' */ - char *Realloc(); unsigned newsize = currsize; - register int i; - do { + register unsigned int i; + do + { newsize *= 2; - } while (newsizecurrsize) increase_next(o); - if(n[o]!=EMPTY) goto nextpos; + for (i = 0; i < MAXOPCODE; i++) + { + if (row[i]) + { + if ((o = b + i) > currsize) + increase_next(o); + if (n[o] != EMPTY) + goto nextpos; } } /* found a place */ - base[state]=b; - for(i=0;imaxpos) maxpos = o; + base[state] = b; + for (i = 0; i < MAXOPCODE; i++) + if (row[i]) + { + if ((o = b + i) > maxpos) + maxpos = o; next[o] = row[i]; check[o] = state; } return; - nextpos: - ++b; + nextpos: ++b; } } -PRIVATE -outdfa() +PRIVATE void outdfa(void) { - register int s,i; + register int s, i; register struct state *p; int nout, ncpy, ngto; int row[MAXOPCODE]; int numinrow; int numentries = 0; - - fprintf(ofile,"#include \"nopt.h\"\n"); - fprintf(ofile,"\n"); - fprintf(ofile,"int OO_maxreplacement = %d;\n", maxreplacement); - fprintf(ofile,"\n"); + + fprintf(ofile, "#include \"nopt.h\"\n"); + fprintf(ofile, "\n"); + fprintf(ofile, "int OO_maxreplacement = %d;\n", maxreplacement); + fprintf(ofile, "\n"); /* find how many entries in dfa */ - for(s=0;s<=higheststate;s++) - for(p=states[s];p!=(struct state *)NULL;p=p->next) + for (s = 0; s <= higheststate; s++) + for (p = states[s]; p != (struct state *) NULL; p = p->next) numentries++; /* start with next and check arrays twice this size */ currsize = 2 * numentries; - next = (int *)Malloc(currsize*sizeof(int)); - check = (int *)Malloc(currsize*sizeof(int)); - base = (int *)Malloc(((unsigned)(higheststate+1))*sizeof(int)); + next = (int *) Malloc(currsize * sizeof(int)); + check = (int *) Malloc(currsize * sizeof(int)); + base = (int *) Malloc(((unsigned) (higheststate + 1)) * sizeof(int)); /* fill next array with EMPTY */ - for(i=0;inext) { + for (p = states[s]; p != (struct state *) NULL; p = p->next) + { numinrow++; row[p->op->id_opcode] = p->goto_state; } /* look for a place to store row */ - if(numinrow) - store_row(s,row); + if (numinrow) + store_row(s, row); else base[s] = EMPTY; } @@ -209,56 +218,304 @@ outdfa() printf("Longest pattern: %d\n", maxpattern); printf("Longest replacement: %d\n", maxreplacement); printf("Dfa contains %d states and %d distinct state/opcode pairs\n", - higheststate+1,numentries); - printf("Compacted using row displacement into %d entries\n",maxpos); + higheststate + 1, numentries); + printf("Compacted using row displacement into %d entries\n", maxpos); /* output the arrays */ - fprintf(ofile,"struct dfa OO_checknext[] = {\n"); - for(i=0;i<=maxpos;i++) { - fprintf(ofile,"\t/* %4d */\t",i); - fprintf(ofile,"{%4d,%4d},\n", check[i], next[i]); + fprintf(ofile, "struct dfa OO_checknext[] = {\n"); + for (i = 0; i <= maxpos; i++) + { + fprintf(ofile, "\t/* %4d */\t", i); + fprintf(ofile, "{%4d,%4d},\n", check[i], next[i]); } - fprintf(ofile,"};\n\n"); - fprintf(ofile,"struct dfa *OO_base[] = {\n"); - for(s=0;s<=higheststate;s++) { - fprintf(ofile,"\t/* %4d: ",s); + fprintf(ofile, "};\n\n"); + fprintf(ofile, "struct dfa *OO_base[] = {\n"); + for (s = 0; s <= higheststate; s++) + { + fprintf(ofile, "\t/* %4d: ", s); outmnems(patterns[s]); - fprintf(ofile,"*/\t"); - if(base[s]==EMPTY) - fprintf(ofile,"0,\n"); + fprintf(ofile, "*/\t"); + if (base[s] == EMPTY) + fprintf(ofile, "0,\n"); else - fprintf(ofile,"&OO_checknext[%4d],\n", base[s]); + fprintf(ofile, "&OO_checknext[%4d],\n", base[s]); } - fprintf(ofile,"};\n\n"); - fprintf(ofile,"struct dodefault OO_default[] = {\n"); - for(s=0;s<=higheststate;s++) { - fprintf(ofile,"\t/* %4d: ",s); + fprintf(ofile, "};\n\n"); + fprintf(ofile, "struct dodefault OO_default[] = {\n"); + for (s = 0; s <= higheststate; s++) + { + fprintf(ofile, "\t/* %4d: ", s); outmnems(patterns[s]); - fprintf(ofile,"*/\t"); - findfail(s,&nout,&ncpy,&ngto); - fprintf(ofile,"{%4d,%4d},\n", nout,ngto); + fprintf(ofile, "*/\t"); + findfail(s, &nout, &ncpy, &ngto); + fprintf(ofile, "{%4d,%4d},\n", nout, ngto); } - fprintf(ofile,"};\n\n"); + fprintf(ofile, "};\n\n"); } -PRIVATE -outmnems(l) - struct mnems l; +PRIVATE void outmnems(struct mnems l) { int i; - for(i=1;i<=l.m_len;i++) - fprintf(ofile,"%s ",l.m_elems[i-1]->op_code->id_text); + for (i = 1; i <= l.m_len; i++) + fprintf(ofile, "%s ", l.m_elems[i - 1]->op_code->id_text); } -PRIVATE int -sametest(s1,s2,e1,e2) - int s1,s2; - struct exp_node *e1,*e2; +PRIVATE int sametest(int s1, int s2, struct exp_node *e1, struct exp_node *e2) { /* return 1 if tests are identical */ - if(e1) { - if(!e2) return 0; - if(e1->node_type!=e2->node_type) return 0; - switch(e1->node_type) { + if (e1) + { + if (!e2) + return 0; + if (e1->node_type != e2->node_type) + return 0; + switch (e1->node_type) + { + case LOGAND: + case LOGOR: + case BITAND: + case BITOR: + case XOR: + case MINUS: + case PLUS: + case TIMES: + case DIV: + case MOD: + case EQ: + case NE: + case LT: + case LE: + case GT: + case GE: + case LSHIFT: + case RSHIFT: + case COMMA: + case SAMESIGN: + case SFIT: + case UFIT: + case ROTATE: + case SAMEEXT: + case SAMENAM: + return (sametest(s1, s2, e1->exp_left, e2->exp_left) + && sametest(s1, s2, e1->exp_right, e2->exp_right)); + case NOT: + case COMP: + case UPLUS: + case UMINUS: + return sametest(s1, s2, e1->exp_left, e2->exp_left); + case DEFINED: + case UNDEFINED: + case INT: + return (e1->leaf_val == e2->leaf_val); + case PATARG: + /* depends on pattern !! */ + if (e1->leaf_val != e2->leaf_val) + return 0; + return (patterns[s1].m_elems[e1->leaf_val - 1]->op_code->id_argfmt + == patterns[s2].m_elems[e2->leaf_val - 1]->op_code->id_argfmt); + case PSIZE: + case WSIZE: + case DWSIZE: + return 1; + + } + /*NOTREACHED*/ + } + else + return (e2 == 0); +} + +PRIVATE int samerepl(int s1, int s2, struct mnems r1, struct mnems r2) +{ + /* return 1 if replacements are identical */ + register int i; + register struct mnem_elem *m1, *m2; + if (r1.m_len != r2.m_len) + return 0; /* different length */ + for (i = 0; i < r1.m_len; i++) + { + m1 = r1.m_elems[i]; + m2 = r2.m_elems[i]; + if (m1->op_code != m2->op_code) + return 0; + if (!sametest(s1, s2, m1->arg, m2->arg)) + return 0; + } + return 1; +} + +PRIVATE int samecode(int s1, int s2) +{ + /* return 1 if replacement code of state s1 and s2 are identical */ + register struct action *a1, *a2; + if (patterns[s1].m_len != patterns[s2].m_len) + return 0; + a1 = actions[s1]; + a2 = actions[s2]; + while (a1) + { + if (!a2) + return 0; /* a2 is shorter */ + if (!samerepl(s1, s2, a1->replacement, a2->replacement)) + return 0; + if (!sametest(s1, s2, a1->test, a2->test)) + return 0; + a1 = a1->next; + a2 = a2->next; + } + if (a2) + return 0; /* a2 is longer */ + return 1; +} + +PRIVATE void outdotrans(void) +{ + register int s, t; + struct action *a; + int seennontested; + int *farray; + fprintf(ofile, "#include \"nopt.h\"\n\n"); + /* keep track of which procedure used for each state */ + farray = (int *) Malloc((unsigned) (higheststate + 1) * sizeof(int)); + for (s = 0; s <= higheststate; s++) + farray[s] = EMPTY; + /* output the functions avoiding duplicates */ + for (s = 0; s <= higheststate; s++) + { + if (actions[s] != (struct action *) NULL) + { + /* first look for a previous identical function */ + for (t = 0; t < s; t++) + { + if (actions[t] != (struct action *) NULL && samecode(s, t)) + { + /* use state 't' instead */ + farray[s] = t; + goto next_func; + } + } + /* no identical function so make new one */ + farray[s] = s; + fprintf(ofile, "\nstatic void do%dtrans(void) {\n", s); + fprintf(ofile, "\tregister p_instr patt = OO_patternqueue;\n"); + fprintf(ofile, "\t/* "); + outmnems(patterns[s]); + fprintf(ofile, " */\n"); + seennontested = 0; + for (a = actions[s]; a != (struct action *) NULL; a = a->next) + { + if (a->test != (struct exp_node *) NULL) + { + fprintf(ofile, "\tif("); + outexp(a->test, s); + fprintf(ofile, ") {\n"); + outoneaction(s, a); + fprintf(ofile, "\t\treturn;\n"); + fprintf(ofile, "\t}\n"); + } + else + { + if (seennontested) + { + fprintf(stderr, + "parser: more than one untested action on state %d\n", + s); + nerrors++; + } + seennontested++; + outoneaction(s, a); + } + } + fprintf(ofile, "}\n"); + } + next_func: continue; + } + /* output the array itself */ + fprintf(ofile, "\n\nvoid (*OO_ftrans[])(void)=\n{\n"); + for (s = 0; s <= higheststate; s++) + { + if (farray[s] != EMPTY) + fprintf(ofile, "\tdo%dtrans,\n", farray[s]); + else + fprintf(ofile, "\t0,\n"); + } + fprintf(ofile, "};\n"); +} + +PRIVATE void outoneaction(int s, struct action *a) +{ + fprintf(ofile, "\t\t/* -> "); + outmnems(a->replacement); + fprintf(ofile, " */\n"); + fprintf(ofile, "#ifdef STATS\n"); + fprintf(ofile, "\t\tif(OO_wrstats) fprintf(stderr,\"%d\\n\");\n", + a->linenum); + fprintf(ofile, "#endif\n"); + outrepl(s, a->replacement); + findworst(patterns[s], a->replacement); +} + +PRIVATE void outrepl(int state, struct mnems repl) +{ + /* Contruct =r1 r2 ... rn and put on output queue. + */ + int n = repl.m_len; + int i; + for (i = 1; i <= n; i++) + { + struct mnem_elem *ri = repl.m_elems[i - 1]; + char *mnem = ri->op_code->id_text; + switch (ri->op_code->id_argfmt) + { + case NOARG: + fprintf(ofile, "\t\tEM_Rop(op_%s);\n", mnem); + break; + case CST: + fprintf(ofile, "\t\tEM_Rcst(op_%s,", mnem); + fprintf(ofile, "(arith)"); + outexp(ri->arg, state); + fprintf(ofile, ");\n"); + break; + case CSTOPT: + if (ri->arg) + { + fprintf(ofile, "\t\tEM_Rcst(op_%s,", mnem); + fprintf(ofile, "(arith)"); + outexp(ri->arg, state); + } + else + { + fprintf(ofile, "\t\tEM_Rnarg(op_%s);\n", mnem); + } + fprintf(ofile, ");\n"); + break; + case LAB: + fprintf(ofile, "\t\tEM_Rilb(op_%s,", mnem); + outexp(ri->arg, state); + fprintf(ofile, ");\n"); + break; + case DEFILB: + fprintf(ofile, "\t\tEM_Rdefilb(op_%s,", mnem); + outexp(ri->arg, state); + fprintf(ofile, ");\n"); + break; + case PNAM: + fprintf(ofile, "\t\tEM_Rpro(op_%s,", mnem); + outexp(ri->arg, state); + fprintf(ofile, ");\n"); + break; + case EXT: + fprintf(ofile, "\t\tOO_mkext(GETNXTREPL(), op_%s,", mnem); + outexp(ri->arg, state); + fprintf(ofile, ");\n"); + break; + } + } +} + +PRIVATE void outexp(struct exp_node *e, int state) +{ + switch (e->node_type) + { case LOGAND: case LOGOR: case BITAND: @@ -277,367 +534,191 @@ sametest(s1,s2,e1,e2) case GE: case LSHIFT: case RSHIFT: - case COMMA: - case SAMESIGN: - case SFIT: - case UFIT: - case ROTATE: - case SAMEEXT: - case SAMENAM: - return (sametest(s1,s2,e1->exp_left,e2->exp_left) && - sametest(s1,s2,e1->exp_right,e2->exp_right)); + fprintf(ofile, "("); + outexp(e->exp_left, state); + outop(e->node_type); + outexp(e->exp_right, state); + fprintf(ofile, ")"); + break; case NOT: case COMP: case UPLUS: case UMINUS: - return sametest(s1,s2,e1->exp_left,e2->exp_left); + fprintf(ofile, "("); + outop(e->node_type); + outexp(e->exp_left, state); + fprintf(ofile, ")"); + break; case DEFINED: + fprintf(ofile, "DEFINED(patt[%d])", e->leaf_val - 1); + break; case UNDEFINED: - case INT: - return (e1->leaf_val == e2->leaf_val); + fprintf(ofile, "!DEFINED(patt[%d])", e->leaf_val - 1); + break; + case COMMA: + outext(e->exp_left); + fprintf(ofile, ","); + fprintf(ofile, "(arith)"); + outexp(e->exp_right, state); + break; + case SAMESIGN: + case SFIT: + case UFIT: + case ROTATE: + outop(e->node_type); + fprintf(ofile, "(arith)"); + outexp(e->exp_left, state); + fprintf(ofile, ","); + fprintf(ofile, "(arith)"); + outexp(e->exp_right, state); + fprintf(ofile, ")"); + break; + case SAMEEXT: + case SAMENAM: + outop(e->node_type); + outext(e->exp_left); + fprintf(ofile, ","); + outext(e->exp_right); + fprintf(ofile, ")"); + break; case PATARG: - /* depends on pattern !! */ - if (e1->leaf_val != e2->leaf_val) return 0; - return (patterns[s1].m_elems[e1->leaf_val-1]-> - op_code->id_argfmt - == patterns[s2].m_elems[e2->leaf_val-1]-> - op_code->id_argfmt); + switch (patterns[state].m_elems[e->leaf_val - 1]->op_code->id_argfmt) + { + case NOARG: + fprintf(stderr, "error: mnem %d has no argument\n", + e->leaf_val); + nerrors++; + break; + case CST: + case CSTOPT: + fprintf(ofile, "CST(patt[%d])", e->leaf_val - 1); + break; + case LAB: + fprintf(ofile, "LAB(patt[%d])", e->leaf_val - 1); + break; + case DEFILB: + fprintf(ofile, "DEFILB(patt[%d])", e->leaf_val - 1); + break; + case PNAM: + fprintf(ofile, "PNAM(patt[%d])", e->leaf_val - 1); + break; + case EXT: + fprintf(ofile, "OO_offset(patt+%d)", e->leaf_val - 1); + break; + } + break; case PSIZE: + fprintf(ofile, "OO_PSIZE"); + break; case WSIZE: + fprintf(ofile, "OO_WSIZE"); + break; case DWSIZE: - return 1; - - } - /*NOTREACHED*/ - } - else return (e2==0); -} - -PRIVATE int -samerepl(s1,s2,r1,r2) - int s1,s2; - struct mnems r1,r2; -{ - /* return 1 if replacements are identical */ - register int i; - register struct mnem_elem *m1,*m2; - if (r1.m_len != r2.m_len) return 0; /* different length */ - for(i=0;iop_code!=m2->op_code) return 0; - if(!sametest(s1,s2,m1->arg,m2->arg)) return 0; - } - return 1; -} - -PRIVATE int -samecode(s1,s2) - int s1,s2; -{ - /* return 1 if replacement code of state s1 and s2 are identical */ - register struct action *a1,*a2; - if (patterns[s1].m_len != patterns[s2].m_len) return 0; - a1 = actions[s1]; - a2 = actions[s2]; - while(a1) { - if(!a2) return 0; /* a2 is shorter */ - if(!samerepl(s1,s2,a1->replacement,a2->replacement)) return 0; - if(!sametest(s1,s2,a1->test,a2->test)) return 0; - a1 = a1->next; - a2 = a2->next; - } - if(a2) return 0; /* a2 is longer */ - return 1; -} - -PRIVATE -outdotrans() -{ - register int s,t; - struct action *a; - int seennontested; - int *farray; - fprintf(ofile,"#include \"nopt.h\"\n\n"); - /* keep track of which procedure used for each state */ - farray = (int *)Malloc((unsigned)(higheststate+1)*sizeof(int)); - for(s=0;s<=higheststate;s++) farray[s] = EMPTY; - /* output the functions avoiding duplicates */ - for(s=0;s<=higheststate;s++) { - if(actions[s]!=(struct action *)NULL) { - /* first look for a previous identical function */ - for(t=0;tnext) { - if(a->test!=(struct exp_node *)NULL) { - fprintf(ofile,"\tif("); - outexp(a->test,s); - fprintf(ofile,") {\n"); - outoneaction(s,a); - fprintf(ofile,"\t\treturn;\n"); - fprintf(ofile,"\t}\n"); - } - else { - if(seennontested) { - fprintf(stderr,"parser: more than one untested action on state %d\n",s); - nerrors++; - } - seennontested++; - outoneaction(s,a); - } - } - fprintf(ofile,"}\n"); - } - next_func: - continue; - } - /* output the array itself */ - fprintf(ofile,"\n\nint (*OO_ftrans[])()=\n{\n"); - for(s=0;s<=higheststate;s++) { - if(farray[s]!=EMPTY) - fprintf(ofile,"\tdo%dtrans,\n",farray[s]); - else - fprintf(ofile,"\t0,\n"); - } - fprintf(ofile,"};\n"); -} - -PRIVATE -outoneaction(s,a) - int s; - struct action *a; -{ - fprintf(ofile,"\t\t/* -> "); - outmnems(a->replacement); - fprintf(ofile," */\n"); - fprintf(ofile,"#ifdef STATS\n"); - fprintf(ofile,"\t\tif(OO_wrstats) fprintf(stderr,\"%d\\n\");\n",a->linenum); - fprintf(ofile,"#endif\n"); - outrepl(s,a->replacement); - findworst(patterns[s],a->replacement); -} - -PRIVATE -outrepl(state,repl) - int state; - struct mnems repl; -{ - /* - /* Contruct =r1 r2 ... rn and put on output queue. - */ - int n = repl.m_len; - int i; - for(i=1;i<=n;i++) { - struct mnem_elem *ri = repl.m_elems[i-1]; - char *mnem = ri->op_code->id_text; - switch(ri->op_code->id_argfmt) { - case NOARG: - fprintf(ofile,"\t\tEM_Rop(op_%s);\n",mnem); + fprintf(ofile, "OO_DWSIZE"); break; - case CST: - fprintf(ofile,"\t\tEM_Rcst(op_%s,",mnem); - fprintf(ofile,"(arith)"); - outexp(ri->arg,state); - fprintf(ofile,");\n"); + case INT: + fprintf(ofile, "%d", e->leaf_val); break; - case CSTOPT: - if(ri->arg) { - fprintf(ofile,"\t\tEM_Rcst(op_%s,",mnem); - fprintf(ofile,"(arith)"); - outexp(ri->arg,state); - } - else { - fprintf(ofile,"\t\tEM_Rnarg(op_%s);\n",mnem); - } - fprintf(ofile,");\n"); - break; - case LAB: - fprintf(ofile,"\t\tEM_Rilb(op_%s,",mnem); - outexp(ri->arg,state); - fprintf(ofile,");\n"); - break; - case DEFILB: - fprintf(ofile,"\t\tEM_Rdefilb(op_%s,",mnem); - outexp(ri->arg,state); - fprintf(ofile,");\n"); - break; - case PNAM: - fprintf(ofile,"\t\tEM_Rpro(op_%s,",mnem); - outexp(ri->arg,state); - fprintf(ofile,");\n"); - break; - case EXT: - fprintf(ofile,"\t\tOO_mkext(GETNXTREPL(), op_%s,",mnem); - outexp(ri->arg,state); - fprintf(ofile,");\n"); - break; - } } } -PRIVATE -outexp(e,state) - struct exp_node *e; - int state; +PRIVATE void outext(struct exp_node *e) { - switch(e->node_type) { - case LOGAND: - case LOGOR: - case BITAND: - case BITOR: - case XOR: - case MINUS: - case PLUS: - case TIMES: - case DIV: - case MOD: - case EQ: - case NE: - case LT: - case LE: - case GT: - case GE: - case LSHIFT: - case RSHIFT: - fprintf(ofile,"("); - outexp(e->exp_left,state); - outop(e->node_type); - outexp(e->exp_right,state); - fprintf(ofile,")"); - break; - case NOT: - case COMP: - case UPLUS: - case UMINUS: - fprintf(ofile,"("); - outop(e->node_type); - outexp(e->exp_left,state); - fprintf(ofile,")"); - break; - case DEFINED: - fprintf(ofile,"DEFINED(patt[%d])",e->leaf_val-1); - break; - case UNDEFINED: - fprintf(ofile,"!DEFINED(patt[%d])",e->leaf_val-1); - break; - case COMMA: - outext(e->exp_left); - fprintf(ofile,","); - fprintf(ofile,"(arith)"); - outexp(e->exp_right,state); - break; - case SAMESIGN: - case SFIT: - case UFIT: - case ROTATE: - outop(e->node_type); - fprintf(ofile,"(arith)"); - outexp(e->exp_left,state); - fprintf(ofile,","); - fprintf(ofile,"(arith)"); - outexp(e->exp_right,state); - fprintf(ofile,")"); - break; - case SAMEEXT: - case SAMENAM: - outop(e->node_type); - outext(e->exp_left); - fprintf(ofile,","); - outext(e->exp_right); - fprintf(ofile,")"); - break; - case PATARG: - switch(patterns[state].m_elems[e->leaf_val-1]->op_code->id_argfmt) { - case NOARG: - fprintf(stderr,"error: mnem %d has no argument\n",e->leaf_val); - nerrors++; - break; - case CST: - case CSTOPT: - fprintf(ofile,"CST(patt[%d])",e->leaf_val-1); - break; - case LAB: - fprintf(ofile,"LAB(patt[%d])",e->leaf_val-1); - break; - case DEFILB: - fprintf(ofile,"DEFILB(patt[%d])",e->leaf_val-1); - break; - case PNAM: - fprintf(ofile,"PNAM(patt[%d])",e->leaf_val-1); - break; - case EXT: - fprintf(ofile,"OO_offset(patt+%d)",e->leaf_val-1); - break; - } - break; - case PSIZE: - fprintf(ofile,"OO_PSIZE"); break; - case WSIZE: - fprintf(ofile,"OO_WSIZE"); break; - case DWSIZE: - fprintf(ofile,"OO_DWSIZE"); break; - case INT: - fprintf(ofile,"%d",e->leaf_val); break; - } -} - -PRIVATE -outext(e) - struct exp_node *e; -{ - if(e->node_type!=PATARG) { - fprintf(stderr,"Internal error in outext of parser\n"); + if (e->node_type != PATARG) + { + fprintf(stderr, "Internal error in outext of parser\n"); nerrors++; } - fprintf(ofile,"patt+%d",e->leaf_val-1); + fprintf(ofile, "patt+%d", e->leaf_val - 1); } -PRIVATE -outop(op) - int op; +PRIVATE void outop(int op) { - switch(op) { - case LOGAND: fprintf(ofile,"&&"); break; - case LOGOR: fprintf(ofile,"||"); break; - case BITAND: fprintf(ofile,"&"); break; - case BITOR: fprintf(ofile,"|"); break; - case XOR: fprintf(ofile,"^"); break; - case MINUS: fprintf(ofile,"-"); break; - case PLUS: fprintf(ofile,"+"); break; - case TIMES: fprintf(ofile,"*"); break; - case DIV: fprintf(ofile,"/"); break; - case MOD: fprintf(ofile,"%%"); break; - case EQ: fprintf(ofile,"=="); break; - case NE: fprintf(ofile,"!="); break; - case LT: fprintf(ofile,"<"); break; - case LE: fprintf(ofile,"<="); break; - case GT: fprintf(ofile,">"); break; - case GE: fprintf(ofile,">="); break; - case LSHIFT: fprintf(ofile,"<<"); break; - case RSHIFT: fprintf(ofile,">>"); break; - case NOT: fprintf(ofile,"!"); break; - case COMP: fprintf(ofile,"~"); break; - case UPLUS: fprintf(ofile,"+"); break; - case UMINUS: fprintf(ofile,"-"); break; - case SAMESIGN: fprintf(ofile,"OO_signsame("); break; - case SFIT: fprintf(ofile,"OO_sfit("); break; - case UFIT: fprintf(ofile,"OO_ufit("); break; - case ROTATE: fprintf(ofile,"OO_rotate("); break; - case SAMEEXT: fprintf(ofile,"OO_extsame("); break; - case SAMENAM: fprintf(ofile,"OO_namsame("); break; + switch (op) + { + case LOGAND: + fprintf(ofile, "&&"); + break; + case LOGOR: + fprintf(ofile, "||"); + break; + case BITAND: + fprintf(ofile, "&"); + break; + case BITOR: + fprintf(ofile, "|"); + break; + case XOR: + fprintf(ofile, "^"); + break; + case MINUS: + fprintf(ofile, "-"); + break; + case PLUS: + fprintf(ofile, "+"); + break; + case TIMES: + fprintf(ofile, "*"); + break; + case DIV: + fprintf(ofile, "/"); + break; + case MOD: + fprintf(ofile, "%%"); + break; + case EQ: + fprintf(ofile, "=="); + break; + case NE: + fprintf(ofile, "!="); + break; + case LT: + fprintf(ofile, "<"); + break; + case LE: + fprintf(ofile, "<="); + break; + case GT: + fprintf(ofile, ">"); + break; + case GE: + fprintf(ofile, ">="); + break; + case LSHIFT: + fprintf(ofile, "<<"); + break; + case RSHIFT: + fprintf(ofile, ">>"); + break; + case NOT: + fprintf(ofile, "!"); + break; + case COMP: + fprintf(ofile, "~"); + break; + case UPLUS: + fprintf(ofile, "+"); + break; + case UMINUS: + fprintf(ofile, "-"); + break; + case SAMESIGN: + fprintf(ofile, "OO_signsame("); + break; + case SFIT: + fprintf(ofile, "OO_sfit("); + break; + case UFIT: + fprintf(ofile, "OO_ufit("); + break; + case ROTATE: + fprintf(ofile, "OO_rotate("); + break; + case SAMEEXT: + fprintf(ofile, "OO_extsame("); + break; + case SAMENAM: + fprintf(ofile, "OO_namsame("); + break; } } diff --git a/modules/src/em_opt/parser.g b/modules/src/em_opt/parser.g index 2c3041ba8..719fec156 100644 --- a/modules/src/em_opt/parser.g +++ b/modules/src/em_opt/parser.g @@ -17,6 +17,7 @@ static char rcsidp1[] = "$Id$"; #endif #include "parser.h" +#include "alloc.h" #define MAXPRIO 11 @@ -33,6 +34,20 @@ int nerrors = 0; static int lencurrpatt; static int lenthisrepl; static int currentstate; /* Current state of dfa */ + +extern void back_token(void); +extern void parser(void); + +/* Forward declarations */ +void addaction(int, int, struct exp_node *,struct exp_node *, + struct mnem_list *); +struct mnem_elem **constructlist(struct mnem_list *, int); +struct mnem_list *addelem(struct mnem_list *, struct idf *, + struct exp_node *); +int dotransition(int, struct idf *, struct mnem_list *, int); +int priority(int); +void LLmessage(int); + } input : /* empty */ @@ -326,11 +341,8 @@ binop : LOGAND %lexical yylex; { -addaction(startline, state, restrictions, finaltest, repllist) - int startline; - int state; - struct exp_node *restrictions, *finaltest; - struct mnem_list *repllist; +void addaction(int startline, int state, struct exp_node *restrictions, + struct exp_node *finaltest, struct mnem_list *repllist) { struct action *p, *q; p=(struct action *)Malloc(sizeof(struct action)); @@ -349,10 +361,7 @@ addaction(startline, state, restrictions, finaltest, repllist) } } -struct mnem_elem ** -constructlist(list,len) - struct mnem_list *list; - int len; +struct mnem_elem **constructlist(struct mnem_list *list, int len) { struct mnem_elem **p; p = (struct mnem_elem **) @@ -364,11 +373,9 @@ constructlist(list,len) return(p); } -struct mnem_list * -addelem(oldlist, mnem, test) - struct mnem_list *oldlist; - struct idf *mnem; - struct exp_node *test; +struct mnem_list *addelem(struct mnem_list *oldlist, + struct idf *mnem, + struct exp_node *test) { struct mnem_list *reslist; struct mnem_elem *element; @@ -381,12 +388,11 @@ addelem(oldlist, mnem, test) return(reslist); } -int -dotransition(state, mnem, mnem_list, lenlist) - int state; - struct idf *mnem; - struct mnem_list *mnem_list; - int lenlist; +int dotransition( + int state, + struct idf *mnem, + struct mnem_list *mnem_list, + int lenlist) { struct state *p; /* look for existing transition */ @@ -399,7 +405,7 @@ dotransition(state, mnem, mnem_list, lenlist) p=(struct state *)Malloc(sizeof(struct state)); p->op=mnem; if(++higheststate>MAXSTATES) { - fprintf(stderr,"Parser: More than %s states\n",MAXSTATES); + fprintf(stderr,"Parser: More than %d states\n",MAXSTATES); sys_stop(S_EXIT); } p->goto_state= higheststate; @@ -427,7 +433,7 @@ combinetests(test1, test2) return(mknode(LOGAND,test1,test2)); } -priority(op) int op; { +int priority(int op) { switch (op) { case LOGOR: return(1); case LOGAND: return(2); @@ -456,10 +462,8 @@ priority(op) int op; { return(0); } -struct exp_node * -mknode(op,left,right) - int op; - struct exp_node *left,*right; +struct exp_node *mknode(int op, struct exp_node *left, + struct exp_node *right) { struct exp_node *p; p = (struct exp_node *)Malloc(sizeof(struct exp_node)); @@ -469,9 +473,7 @@ mknode(op,left,right) return(p); } -struct exp_node * -mkleaf(op,val) - int op,val; +struct exp_node *mkleaf(int op, int val) { struct exp_node *p; p = (struct exp_node *)Malloc(sizeof(struct exp_node)); @@ -480,8 +482,7 @@ mkleaf(op,val) return(p); } -LLmessage(insertedtok) - int insertedtok; +void LLmessage(int insertedtok) { nerrors++; fprintf(stderr,"parser: syntax error on line %d: ",linenum); @@ -492,7 +493,8 @@ LLmessage(insertedtok) else fprintf(stderr,"Deleted token %d\n",LLsymb); } -main() { +int main(int argc, char **argv) +{ initlex(); states[0] = (struct state *)NULL; patterns[0].m_len = 0; diff --git a/modules/src/em_opt/parser.h b/modules/src/em_opt/parser.h index aedaec153..32e9c481b 100644 --- a/modules/src/em_opt/parser.h +++ b/modules/src/em_opt/parser.h @@ -1,4 +1,7 @@ /* $Id$ */ +#ifndef PARSER_H_ +#define PARSER_H_ + #include #include @@ -88,10 +91,17 @@ extern struct idf *opval; /* opcode of returned OPCODE*/ extern int lastintval; /* value of last integer seen */ extern int linenum; /*line number of input file*/ -/* Functions not returning int */ -char *Malloc(); -struct exp_node *mknode(); -struct exp_node *mkleaf(); -struct exp_node *combinetests(); -struct mnem_list *addelem(); -struct mnem_elem **constructlist(); +struct exp_node *mknode(int, struct exp_node *, struct exp_node *); +struct exp_node *mkleaf(int, int); +struct exp_node *combinetests(struct exp_node *, struct exp_node *); +struct mnem_list *addelem(struct mnem_list *, struct idf *, struct exp_node *); +struct mnem_elem **constructlist(struct mnem_list *, int); +void findworst(struct mnems,struct mnems); + +void outputincalls(void); +void outputnopt(void); +void initlex(void); +void findfail(int, int *, int *, int *); + + +#endif /* PARSER_H_ */ diff --git a/modules/src/em_opt/syntax.l b/modules/src/em_opt/syntax.l index a9d30069d..a4c55522b 100644 --- a/modules/src/em_opt/syntax.l +++ b/modules/src/em_opt/syntax.l @@ -3,6 +3,7 @@ #include "Lpars.h" #include "parser.h" + struct idf *opval; /* opcode of returned OPCODE*/ int lastintval; /* value of last integer seen */ int linenum = 1; /*current line number of input file*/ @@ -59,7 +60,12 @@ offset return(OFFSET); . return(yytext[0]); %% -back_token() +int yywrap(void) +{ + return 1; +} + +void back_token(void) { yyless(0); } diff --git a/modules/src/em_opt/utils.c b/modules/src/em_opt/utils.c index 4ba33c43d..9ade89016 100644 --- a/modules/src/em_opt/utils.c +++ b/modules/src/em_opt/utils.c @@ -2,97 +2,88 @@ static char rcsid4[] = "$Id$"; #endif +#include #include "nopt.h" -arith -OO_rotate(w,amount) - arith w, amount; +arith OO_rotate(arith w, arith amount) { long highmask, lowmask; - highmask = (long)(-1) << amount; + highmask = (long) (-1) << amount; lowmask = ~highmask; - if(OO_WSIZE!=4) - highmask &= (OO_WSIZE==2)?0xFFFF:0xFF; - return(((w<> (8*OO_WSIZE-amount))&lowmask)); + if (OO_WSIZE != 4) + highmask &= (OO_WSIZE == 2) ? 0xFFFF : 0xFF; + return (((w << amount) & highmask) + | ((w >> (8 * OO_WSIZE - amount)) & lowmask)); } -int -OO_signsame(a,b) - arith a, b; +int OO_signsame(arith a, arith b) { - return( (a ^ b) >= 0); + return ((a ^ b) >= 0); } -int -OO_sfit(val,nbits) - arith val, nbits; +int OO_sfit(arith val, arith nbits) { register long mask = ~((1L << (nbits - 1)) - 1); - return(((val&mask) == 0) | (val&mask)==mask); + return (((val & mask) == 0) | ((val & mask) == mask)); } -int -OO_ufit(val, nbits) - arith val, nbits; +int OO_ufit(arith val, arith nbits) { - return((val&(~((1L << (nbits - 1)) - 1))) == 0); + return ((val & (~((1L << (nbits - 1)) - 1))) == 0); } -int -OO_extsame(a1,a2) - register p_instr a1, a2; +int OO_extsame(register p_instr a1, register p_instr a2) { if (a1->em_argtype != a2->em_argtype) - return(0); - switch(a1->em_argtype) { - case cst_ptyp: - return (a1->em_cst == a2->em_cst); - case sof_ptyp: - if(a1->em_off != a2->em_off) - return(0); - return (strcmp(a1->em_dnam,a2->em_dnam)==0); - case nof_ptyp: - if (a1->em_off != a2->em_off) - return(0); - return (a1->em_dlb == a2->em_dlb); - default: - fatal("illegal type (%d) to sameext!",a1->em_argtype); - /*NOTREACHED*/ + return (0); + switch (a1->em_argtype) + { + case cst_ptyp: + return (a1->em_cst == a2->em_cst); + case sof_ptyp: + if (a1->em_off != a2->em_off) + return (0); + return (strcmp(a1->em_dnam, a2->em_dnam) == 0); + case nof_ptyp: + if (a1->em_off != a2->em_off) + return (0); + return (a1->em_dlb == a2->em_dlb); + default: + fatal("illegal type (%d) to sameext!", a1->em_argtype); + /*NOTREACHED*/ } } -int -OO_namsame(a1,a2) - register p_instr a1, a2; +int OO_namsame(register p_instr a1, register p_instr a2) { if (a1->em_argtype != a2->em_argtype) - return(0); - switch(a1->em_argtype) { - case cst_ptyp: - return 1; - case sof_ptyp: - return (strcmp(a1->em_dnam,a2->em_dnam)==0); - case nof_ptyp: - return (a1->em_dlb == a2->em_dlb); - default: - fatal("illegal type (%d) to samenam!",a1->em_argtype); - /*NOTREACHED*/ + return (0); + switch (a1->em_argtype) + { + case cst_ptyp: + return 1; + case sof_ptyp: + return (strcmp(a1->em_dnam, a2->em_dnam) == 0); + case nof_ptyp: + return (a1->em_dlb == a2->em_dlb); + default: + fatal("illegal type (%d) to samenam!", a1->em_argtype); + /*NOTREACHED*/ } } -arith -OO_offset(a) - register p_instr a; +arith OO_offset(register p_instr a) { - switch(a->em_argtype) { - case cst_ptyp: - return a->em_cst; - case sof_ptyp: - return a->em_off; - case nof_ptyp: - return a->em_off; - default: - fatal("illegal type (%d) to offset!",a->em_argtype); - /*NOTREACHED*/ + switch (a->em_argtype) + { + case cst_ptyp: + return a->em_cst; + case sof_ptyp: + return a->em_off; + case nof_ptyp: + return a->em_off; + default: + fatal("illegal type (%d) to offset!", a->em_argtype); + /*NOTREACHED*/ } }