From 1c1eed4fd8fddf2105b9e318003a2dde46cc6cef Mon Sep 17 00:00:00 2001 From: bruce Date: Tue, 21 Jul 1987 13:23:09 +0000 Subject: [PATCH] Major change: All queues now in same buffer --- modules/src/em_opt/Makefile | 7 +- modules/src/em_opt/findworst.c | 5 +- modules/src/em_opt/main.c | 31 ++- modules/src/em_opt/mkstrct.c | 103 +++++----- modules/src/em_opt/nopt.c | 347 ++++++++++++++------------------- modules/src/em_opt/nopt.h | 36 ++-- modules/src/em_opt/outcalls.c | 18 +- modules/src/em_opt/outputdfa.c | 61 +++--- modules/src/em_opt/parser.g | 13 +- modules/src/em_opt/parser.h | 3 +- modules/src/em_opt/pseudo.r | 15 +- 11 files changed, 308 insertions(+), 331 deletions(-) diff --git a/modules/src/em_opt/Makefile b/modules/src/em_opt/Makefile index e6ffdc48a..667692d9a 100644 --- a/modules/src/em_opt/Makefile +++ b/modules/src/em_opt/Makefile @@ -1,5 +1,5 @@ # $Header$ -EMHOME = ../../.. +EMHOME = /proj/em/Work INSTALL = $(EMHOME)/modules/install COMPARE = $(EMHOME)/modules/compare LINT = lint @@ -35,7 +35,7 @@ CSRC = main.c nopt.c mkstrct.c aux.c outputdfa.c outcalls.c\ SRCS = Makefile nopt.h parser.h parser.g syntax.l pseudo.r\ patterns $(CSRC) -NOFILES = nopt.o dfa.o trans.o aux.o +NOFILES = nopt.o mkstrct.o dfa.o trans.o aux.o POFILES = parser.o syntax.o outputdfa.o outcalls.o findworst.o\ initlex.o Lpars.o @@ -77,13 +77,14 @@ NOPTLIB = $(EMHOME)/modules/lib/libread_emk.a\ $(EMHOME)/modules/lib/libemk.a\ $(EMHOME)/modules/lib/liballoc.a\ $(EMHOME)/modules/lib/malloc.o\ + $(EMHOME)/modules/lib/libstring.a\ $(EMHOME)/modules/lib/libsystem.a\ $(EMHOME)/lib/em_data.a em_nopt: dfadummy main.o $(NOFILES) $(CC) -o em_nopt main.o $(NOFILES) $(NOPTLIB) -OLINT = main.c nopt.c aux.c dfa.c trans.c +OLINT = main.c mkstrct.c nopt.c aux.c dfa.c trans.c OLINTLIB = $(EMHOME)/modules/lib/llib-lread_emkV.ln\ $(EMHOME)/modules/lib/llib-lemk.ln\ diff --git a/modules/src/em_opt/findworst.c b/modules/src/em_opt/findworst.c index 0057943ff..420285a81 100644 --- a/modules/src/em_opt/findworst.c +++ b/modules/src/em_opt/findworst.c @@ -30,7 +30,7 @@ findworst(repl) int s; int mostbackups = 0; if(n==0) { - fprintf(ofile,"\t\tOO_backup(%d);\n", longestpattern-1); + fprintf(ofile,"\t\tOO_backup(%d);\n", maxpattern-1); return; } for(s=1;s<=higheststate;s++) { @@ -56,8 +56,7 @@ findworst(repl) } } } - if(mostbackups) - fprintf(ofile,"\t\tOO_backup(%d);\n",mostbackups); + fprintf(ofile,"\t\tOO_backup(%d);\n",mostbackups); } findfail(state,resout,rescpy,resgto) diff --git a/modules/src/em_opt/main.c b/modules/src/em_opt/main.c index dafed33c1..8e555f4e3 100644 --- a/modules/src/em_opt/main.c +++ b/modules/src/em_opt/main.c @@ -18,7 +18,8 @@ int errors; /* Number of errors */ main(argc,argv) char **argv; { - register p_instr p = GETINSTR(); + static struct e_instr buff; + register p_instr p = &buff; if (argc >= 2) { filename = argv[1]; @@ -36,11 +37,13 @@ main(argc,argv) } else if (!O_open( (char *) 0)) fatal("O_open failed"); O_magic(); + EM_mkcalls(p); + for(;;) { + EM_getinstr(p=GETNXTPATT()); switch(p->em_type) { case EM_DEFILB: - *OO_nxtpatt++ = p; - OO_dfa(p->em_opcode=op_lab); + p->em_opcode=op_lab; break; case EM_MNEM: switch(p->em_argtype) { @@ -57,23 +60,31 @@ main(argc,argv) p->em_string = OO_freestr(p->em_string); break; } - *OO_nxtpatt++ = p; - OO_dfa(p->em_opcode); break; default: - FLUSHDFA(); - EM_mkcalls(p); - OO_free(p); + p->em_opcode = OTHER; + /* fall thru */ + if (OO_state) { + buff = *p; + OO_dfa(OTHER); + EM_mkcalls(&buff); + } + else { + OO_flush(); + EM_mkcalls(p); + } + continue; + case EM_PSEU: break; case EM_EOF: goto got_eof; case EM_ERROR: error("%s", EM_error); - break; + continue; case EM_FATAL: fatal("%s", EM_error); } - EM_getinstr(p=GETINSTR()); + OO_dfa(p->em_opcode); } got_eof: O_close(); diff --git a/modules/src/em_opt/mkstrct.c b/modules/src/em_opt/mkstrct.c index 7235a248c..989178246 100644 --- a/modules/src/em_opt/mkstrct.c +++ b/modules/src/em_opt/mkstrct.c @@ -4,87 +4,92 @@ static char rcsid3[] = "$Header$"; #include "nopt.h" -OO_inop(opcode) +EM_mkop(p,opcode) + register p_instr p; int opcode; { - register p_instr p = GETINSTR(); p->em_type = EM_MNEM; p->em_opcode = opcode; p->em_argtype = 0; - *OO_nxtpatt++ = p; } -OO_incst(opcode,cst) - int opcode,cst; +EM_mknarg(p,opcode) + register p_instr p; + int opcode; { - register p_instr p = GETINSTR(); p->em_type = EM_MNEM; p->em_opcode = opcode; - p->em_argtype = cst_ptyp; - p->em_cst = cst; - *OO_nxtpatt++ = p; + p->em_argtype = 0; + p->em_cst = 0; } -OO_inlab(opcode,lab) - int opcode,lab; +EM_mkilb(p,opcode,lab) + register p_instr p; + int opcode; + label lab; { - register p_instr p = GETINSTR(); p->em_type = EM_MNEM; - p->em_opcode = opcode; p->em_argtype = ilb_ptyp; + p->em_opcode = opcode; p->em_ilb = lab; - *OO_nxtpatt++ = p; } -OO_inpnam(opcode,pnam) +EM_mknof(p,opcode,lab,off) + register p_instr p; + int opcode; + label lab; + arith off; +{ + p->em_type = EM_MNEM; + p->em_argtype = nof_ptyp; + p->em_opcode = opcode; + p->em_dlb = lab; + p->em_off = off; +} + +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_off = off; +} + +EM_mkcst(p,opcode,cst) + register p_instr p; + int opcode; + arith cst; +{ + p->em_type = EM_MNEM; + p->em_argtype = cst_ptyp; + p->em_opcode = opcode; + p->em_cst = cst; +} + +EM_mkpro(p,opcode,pnam) + register p_instr p; int opcode; char *pnam; { - register p_instr p = GETINSTR(); p->em_type = EM_MNEM; - p->em_opcode = opcode; p->em_argtype = pro_ptyp; + p->em_opcode = opcode; p->em_pnam = OO_freestr(pnam); - *OO_nxtpatt++ = p; } -OO_indefilb(opcode,deflb) +EM_mkdefilb(p,opcode,deflb) + register p_instr p; int opcode; label deflb; { - register p_instr p = GETINSTR(); p->em_type = EM_DEFILB; p->em_opcode = opcode; p->em_argtype = 0; p->em_ilb = deflb; - *OO_nxtpatt++ = p; -} - -OO_indnam(opcode,name,off) - int opcode; - char *name; - int off; -{ - register p_instr p = GETINSTR(); - p->em_type = EM_MNEM; - p->em_opcode = opcode; - p->em_argtype = sof_ptyp; - p->em_dnam = OO_freestr(name); - p->em_off = off; - *OO_nxtpatt++ = p; -} - -OO_indlb(opcode,lab,off) - int opcode; - label lab; - int off; -{ - register p_instr p = GETINSTR(); - p->em_type = EM_MNEM; - p->em_opcode = opcode; - p->em_argtype = nof_ptyp; - p->em_dlb = lab; - p->em_off = off; - *OO_nxtpatt++ = p; } diff --git a/modules/src/em_opt/nopt.c b/modules/src/em_opt/nopt.c index 7e1ced1c1..a50d8e4f1 100644 --- a/modules/src/em_opt/nopt.c +++ b/modules/src/em_opt/nopt.c @@ -6,36 +6,39 @@ static char rcsid2[] = "$Header$"; extern int (*OO_fstate[])(); /* Initialized from patterns in dfa.c */ extern int OO_maxpattern; /* Initialized from patterns in dfa.c */ -#define MAXBACKUP 50 -#define MAXOUTPUT 200 -#define MAXSTRING 1000 +extern int OO_maxreplacement; /* Initialized from patterns in dfa.c */ extern char em_mnem[][4]; extern char em_pseu[][4]; -p_instr OO_freeq; -p_instr *OO_patternqueue; -p_instr *OO_nxtpatt; -p_instr *OO_bkupqueue; -p_instr *OO_nxtbackup; -p_instr OO_OTHER; +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 p_instr *lastbackup; -static p_instr *outputqueue; -static p_instr *nextoutput; -static p_instr *lastoutput; static char *strqueue; static char *nextstr; static char *laststr; -int OO_noutput; /* number of instructions in output queue */ arith OO_WSIZE; /* wordlength */ arith OO_PSIZE; /* pointer length */ #ifdef STATS int OO_wrstats = 1; /* pattern statistics output */ #endif +#ifdef DEBUG +#define printstate(s) dumpstate(s) +#else +#define printstate(s) +#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) for(pp=(p),qq=(q),i=(n);i--;*pp++ = *qq++) O_init(wsize,psize) arith wsize, psize; @@ -67,10 +70,12 @@ OO_dfa(last) register int last; { for(;;) { + printstate("OO_dfa"); (*OO_fstate[OO_state])(last); - if (OO_nxtbackup==OO_bkupqueue) - return; - last = ((*OO_nxtpatt++ = *(--OO_nxtbackup))->em_opcode); + if (!OO_endbackup) return; + last = (OO_nxtpatt++)->em_opcode; + if (OO_nxtpatt >= OO_endbackup) + OO_endbackup = 0; } } @@ -89,34 +94,15 @@ PRIVATE allocmem() { /* Allocate memory for queues on heap */ - OO_nxtpatt = OO_patternqueue = - (p_instr *)Malloc(OO_maxpattern*sizeof(p_instr)); - OO_nxtbackup = OO_bkupqueue = - (p_instr *)Malloc(MAXBACKUP*sizeof(p_instr)); - lastbackup = OO_bkupqueue + MAXBACKUP - 1; - nextoutput = outputqueue = - (p_instr *)Malloc(MAXOUTPUT*sizeof(p_instr)); - lastoutput = outputqueue + MAXOUTPUT - 1; - OO_noutput = 0; + 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_nxtrepl = OO_replqueue; nextstr = strqueue = (char *)Malloc(MAXSTRING*sizeof(char)); laststr = strqueue + MAXSTRING - 1; - /* allocate dummy OTHER data structure */ - OO_OTHER = (p_instr)Malloc(sizeof(struct e_instr)); - OO_OTHER->em_type = EM_MNEM; - OO_OTHER->em_opcode = OTHER; - OO_OTHER->em_argtype = 0; -} - -OO_nfree(n) - register int n; -{ - register p_instr *p = OO_nxtpatt = OO_patternqueue; - while(n--) { - OO_free(*p); /* OO_free is macro so don't use *p++ */ - p++; - } - OO_state = 0; } char * @@ -150,235 +136,196 @@ OO_flush() /* Output all instructions waiting in the output queue and free their /* storage including the saved strings. */ - register int n; - register p_instr *p; -#ifdef DEBUG + register p_instr p,q; + register int i,n; printstate("Flush"); -#endif - if (n = OO_noutput) { - for(p=outputqueue;n--;p++) { - EM_mkcalls(*p); - OO_free(*p); - } - nextoutput=outputqueue; - if(OO_nxtbackup==OO_bkupqueue) - nextstr = strqueue; - OO_noutput = 0; + for(p=OO_buffer;pem_opcode!=OTHER) + EM_mkcalls(p); + 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; + OO_patternqueue = OO_nxtpatt = OO_buffer; } -OO_out(p) - p_instr p; +p_instr +OO_halfflush() { - /* Put the instruction p on the output queue */ - if(nextoutput > lastoutput) { -#ifdef DEBUG - fprintf(stderr,"Warning: Overflow of outputqueue - output flushed\n"); -#endif - OO_flush(); - } - OO_noutput++; - *nextoutput++ = p; + /* + /* 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--;) + EM_mkcalls(p++); + /* now copy the rest of buffer and pattern back */ + BTSCPY(p,q,i,OO_buffer,OO_buffer+n,n+(OO_nxtpatt-OO_buffer)); + OO_patternqueue -= n; + OO_nxtpatt -= n; + printstate("after Half flush"); + return (OO_nxtpatt++); } -OO_outop(opcode) - int opcode; -{ - register p_instr p = GETINSTR(); - p->em_type = EM_MNEM; - p->em_opcode = opcode; - p->em_argtype = 0; - OO_out(p); -} - -OO_outcst(opcode,cst) - int opcode; - arith cst; -{ - register p_instr p = GETINSTR(); - p->em_type = EM_MNEM; - p->em_opcode = opcode; - p->em_argtype = cst_ptyp; - p->em_cst = cst; - OO_out(p); -} - -OO_outlab(opcode,lab) - int opcode; - label lab; -{ - register p_instr p = GETINSTR(); - p->em_type = EM_MNEM; - p->em_opcode = opcode; - p->em_argtype = ilb_ptyp; - p->em_ilb = lab; - OO_out(p); -} - -OO_outpnam(opcode,pnam) - int opcode; - char *pnam; -{ - register p_instr p = GETINSTR(); - p->em_type = EM_MNEM; - p->em_opcode = opcode; - p->em_argtype = pro_ptyp; - p->em_pnam = pnam; - OO_out(p); -} - -OO_outdefilb(opcode,deflb) - int opcode; - label deflb; -{ - register p_instr p = GETINSTR(); - p->em_type = EM_DEFILB; - p->em_opcode = opcode; - p->em_argtype = 0; - p->em_ilb = deflb; - OO_out(p); -} - -OO_outext(opcode,arg,off) +OO_mkext(p,opcode,arg,off) + register p_instr p; int opcode; p_instr arg; arith off; { - register p_instr p = GETINSTR(); - p->em_type = EM_MNEM; - p->em_opcode = opcode; - switch(p->em_argtype = arg->em_argtype) { + switch(arg->em_argtype) { case cst_ptyp: - p->em_cst = off; + EM_mkcst(p,opcode,off); break; case sof_ptyp: - p->em_dnam = arg->em_dnam; - p->em_off = off; + EM_mksof(p,opcode,arg->em_dnam,off); break; case nof_ptyp: - p->em_dlb = arg->em_dlb; - p->em_off = off; + EM_mknof(p,opcode,arg->em_dlb,off); break; default: fatal("Unexpected type %d in outext",arg->em_argtype); } - OO_out(p); -} - -OO_pushback(p) - p_instr p; -{ - /* push instr. p onto bkupqueue */ - if(OO_nxtbackup > lastbackup) { -#ifdef DEBUG - fprintf(stderr,"Warning: Overflow of bkupqueue-backup ignored\n"); - printstate("Backup overflow"); -#endif - return; - } - *OO_nxtbackup++ = p; } OO_backup(n) - register int n; + int n; { - /* copy (up to) n instructions from output to backup queues */ - while(n-- && nextoutput>outputqueue) { - OO_pushback(*(--nextoutput)); - OO_noutput--; + /* copy the replacement queue into the buffer queue */ + /* then move the pattern queue back n places */ + register p_instr p,q; + register int i,lrepl, diff; + printstate("Before backup"); + lrepl = OO_nxtrepl-OO_replqueue; + if(OO_endbackup) { + /* move the region between OO_nxtpatt and OO_endbackup */ + if ((diff = (OO_nxtpatt-OO_patternqueue) - lrepl) > 0) { + /* move left by diff */ + BTSCPY(p,q,i,OO_nxtpatt-diff,OO_nxtpatt,OO_endbackup-OO_nxtpatt); + OO_nxtpatt -= diff; + OO_endbackup -= diff; + } + else if (diff < 0) { + /* move right by diff */ + /* careful of overflowing buffer!! */ + 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_nxtpatt -= diff; + OO_endbackup -= diff; + } } + /* copy the replacement */ + 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 n instructions */ + if ((OO_patternqueue-OO_buffer) < n) + n = (OO_patternqueue-OO_buffer); + OO_nxtpatt = OO_patternqueue -= n; + if(!OO_endbackup && n) + OO_endbackup = OO_patternqueue+n; + OO_state = 0; + printstate("After backup"); } -OO_dodefault(numout, numcopy,newstate) - register int numout, numcopy; +OO_dodefault(numout, newstate) + int numout; int newstate; { - register p_instr *p, *q; - OO_pushback(*--OO_nxtpatt); - q = (p = OO_patternqueue) + numout; - while(numcopy--) { - if(numout) { - numout--; - OO_out(*p); - } - *p++ = *q++; - } - OO_nxtpatt = p; - while(numout--) OO_out(*p++); + printstate("Before dodefault"); + if(!OO_endbackup) OO_endbackup = OO_nxtpatt; + OO_nxtpatt--; + OO_patternqueue += numout; OO_state = newstate; + printstate("After dodefault"); } #ifdef DEBUG -PRIVATE -printstate(mess) +dumpstate(mess) char *mess; { - p_instr *p; - fprintf(stderr,"%s - state: ",mess); - p = outputqueue; - while(pem_type) { case EM_MNEM: - if(p->em_opcode == OTHER) - fprintf(stderr,"OTHER"); - else - fprintf(stderr,"%s",em_mnem[p->em_opcode-sp_fmnem]); + fprintf(stderr,"%s ",em_mnem[p->em_opcode-sp_fmnem]); break; case EM_PSEU: - case EM_STARTMES: - fprintf(stderr,"%s",em_pseu[p->em_opcode-sp_fpseu]); + fprintf(stderr,"%s ",em_pseu[p->em_opcode-sp_fpseu]); break; + case EM_STARTMES: case EM_MESARG: case EM_ENDMES: + fprintf(stderr,"MES "); break; case EM_DEFILB: - fprintf(stderr,"%ld", (long)p->em_ilb); - break; + fprintf(stderr,"%ld ", (long)p->em_ilb); + return; case EM_DEFDLB: - fprintf(stderr,"%ld", (long)p->em_dlb); - break; + fprintf(stderr,"%ld ", (long)p->em_dlb); + return; case EM_DEFDNAM: - fprintf(stderr,"%d", p->em_dnam); - break; + fprintf(stderr,"%d ", p->em_dnam); + return; case EM_ERROR: case EM_FATAL: case EM_EOF: - break; + return; } switch(p->em_argtype) { case 0: - fprintf(stderr," "); break; case cst_ptyp: - fprintf(stderr," %d ",p->em_cst); + fprintf(stderr,"%d ",p->em_cst); break; case nof_ptyp: - fprintf(stderr," .%d+%d ",p->em_dlb,p->em_off); + fprintf(stderr,".%d+%d ",p->em_dlb,p->em_off); break; case sof_ptyp: - fprintf(stderr," %s+%d ",p->em_dnam,p->em_off); + fprintf(stderr,"%s+%d ",p->em_dnam,p->em_off); break; case ilb_ptyp: - fprintf(stderr," *%d ",p->em_ilb); + fprintf(stderr,"*%d ",p->em_ilb); break; case pro_ptyp: - fprintf(stderr," $%s ",p->em_pnam); + fprintf(stderr,"$%s ",p->em_pnam); + break; + case str_ptyp: + case ico_ptyp: + case uco_ptyp: + fprintf(stderr,"\"%s\"",p->em_string); break; default: fatal(" prtinst - Unregognized arg %d ",p->em_argtype); diff --git a/modules/src/em_opt/nopt.h b/modules/src/em_opt/nopt.h index df71693ef..1bb873bf6 100644 --- a/modules/src/em_opt/nopt.h +++ b/modules/src/em_opt/nopt.h @@ -12,26 +12,29 @@ #include #include +#define MAXBUFFER 200 +#define MAXSTRING 1000 + #define OTHER 0 #define op_lab sp_fpseu typedef struct e_instr *p_instr; -#define FLUSHDFA() if(OO_state) {\ - *OO_nxtpatt++ = OO_OTHER; OO_dfa(OTHER);\ - } else if(OO_noutput) OO_flush(); +extern p_instr OO_buffer; +extern p_instr OO_patternqueue; +extern p_instr OO_nxtpatt; +extern p_instr OO_endbackup; +extern p_instr OO_nxtrepl; -#define GETINSTR() ((p_instr)st_alloc((char **)&OO_freeq,sizeof(struct e_instr),20)) -#define OO_free(p) st_free((p),&OO_freeq,sizeof(struct e_instr)) +p_instr OO_halfflush(); + +# define GETNXTPATT() (OO_nxtpatt>&OO_buffer[MAXBUFFER]?OO_halfflush():OO_nxtpatt++) +# define GETNXTREPL() (OO_nxtrepl++) + +# define FLUSHDFA() (GETNXTPATT())->em_opcode=OTHER;\ + if (OO_state) OO_dfa(OTHER); else OO_flush() -extern p_instr OO_freeq; -extern p_instr *OO_patternqueue; -extern p_instr *OO_nxtpatt; -extern p_instr *OO_bkupqueue; -extern p_instr *OO_nxtbackup; -extern p_instr OO_OTHER; extern int OO_state; -extern int OO_noutput; /* number of instructions in output queue */ extern arith OO_WSIZE; /* wordlength */ extern arith OO_PSIZE; /* pointer length */ #ifdef STATS @@ -42,8 +45,9 @@ extern char *OO_freestr(); extern arith OO_rotate(); extern arith OO_offset(); -#define CST(p) (p->em_cst) -#define PNAM(p) (p->em_pnam) -#define LAB(p) (p->em_ilb) -#define DEFILB(p) (p->em_ilb) +#define CST(p) (p.em_cst) +#define PNAM(p) (p.em_pnam) +#define LAB(p) (p.em_ilb) +#define DEFILB(p) (p.em_ilb) +#define DEFINED(p) (p.em_argtype) diff --git a/modules/src/em_opt/outcalls.c b/modules/src/em_opt/outcalls.c index fd36f0c63..29a7f4b9c 100644 --- a/modules/src/em_opt/outcalls.c +++ b/modules/src/em_opt/outcalls.c @@ -14,7 +14,7 @@ outputincalls() case NOARG: fprintf(ofile,"%s\t|\t|\n",s); if(op->id_used) { - fprintf(ofile,"\tOO_inop(op_%s);\n",s); + fprintf(ofile,"\tEM_mkop(GETNXTPATT(),op_%s);\n",s); fprintf(ofile,"\tOO_dfa(op_%s);\n",s); } else { @@ -25,7 +25,7 @@ outputincalls() case CSTOPT: fprintf(ofile,"%s_narg\t|\t|\n",s); if(op->id_used) { - fprintf(ofile,"\tOO_inop(op_%s);\n",s); + fprintf(ofile,"\tEM_mknarg(GETNXTPATT(),op_%s);\n",s); fprintf(ofile,"\tOO_dfa(op_%s);\n",s); } else { @@ -36,7 +36,7 @@ outputincalls() case CST: fprintf(ofile,"%s\t| int:n\t|\n",s); if(op->id_used) { - fprintf(ofile,"\tOO_incst(op_%s,n);\n",s); + fprintf(ofile,"\tEM_mkcst(GETNXTPATT(),op_%s,n);\n",s); fprintf(ofile,"\tOO_dfa(op_%s);\n",s); } else { @@ -47,7 +47,7 @@ outputincalls() case DEFILB: fprintf(ofile,"df_ilb\t| label:l\t|\n"); if(op->id_used) { - fprintf(ofile,"\tOO_indefilb(op_%s,l);\n",s); + fprintf(ofile,"\tEM_mkdefilb(GETNXTPATT(),op_%s,l);\n",s); fprintf(ofile,"\tOO_dfa(op_%s);\n",s); } else { @@ -58,7 +58,7 @@ outputincalls() case PNAM: fprintf(ofile,"%s\t| char *:s\t|\n",s); if(op->id_used) { - fprintf(ofile,"\tOO_inpnam(op_%s,s);\n",s); + fprintf(ofile,"\tEM_mkpro(GETNXTPATT(),op_%s,s);\n",s); fprintf(ofile,"\tOO_dfa(op_%s);\n",s); } else { @@ -69,7 +69,7 @@ outputincalls() case LAB: fprintf(ofile,"%s\t| label:l\t|\n",s); if(op->id_used) { - fprintf(ofile,"\tOO_inlab(op_%s,l);\n",s); + fprintf(ofile,"\tEM_mkilb(GETNXTPATT(),op_%s,l);\n",s); fprintf(ofile,"\tOO_dfa(op_%s);\n",s); } else { @@ -80,7 +80,7 @@ outputincalls() case EXT: fprintf(ofile,"%s\t| int:n\t|\n",s); if(op->id_used) { - fprintf(ofile,"\tOO_incst(op_%s,n);\n",s); + fprintf(ofile,"\tEM_mkcst(GETNXTPATT(),op_%s,n);\n",s); fprintf(ofile,"\tOO_dfa(op_%s);\n",s); } else { @@ -89,7 +89,7 @@ outputincalls() } fprintf(ofile,"%s_dnam\t| char *:s int:n\t|\n",s); if(op->id_used) { - fprintf(ofile,"\tOO_indnam(op_%s,s,n);\n",s); + fprintf(ofile,"\tEM_mksof(GETNXTPATT(),op_%s,s,n);\n",s); fprintf(ofile,"\tOO_dfa(op_%s);\n",s); } else { @@ -98,7 +98,7 @@ outputincalls() } fprintf(ofile,"%s_dlb\t| label:l int:n\t|\n",s); if(op->id_used) { - fprintf(ofile,"\tOO_indlb(op_%s,l,n);\n",s); + fprintf(ofile,"\tEM_mknof(GETNXTPATT(),op_%s,l,n);\n",s); fprintf(ofile,"\tOO_dfa(op_%s);\n",s); } else { diff --git a/modules/src/em_opt/outputdfa.c b/modules/src/em_opt/outputdfa.c index e59c94efd..fd22c5110 100644 --- a/modules/src/em_opt/outputdfa.c +++ b/modules/src/em_opt/outputdfa.c @@ -99,7 +99,7 @@ outdfa() int seenswitch; fprintf(ofile,"#include \"nopt.h\"\n"); fprintf(ofile,"\n"); - fprintf(ofile,"int OO_maxpattern = %d;\n", longestpattern); + fprintf(ofile,"int OO_maxreplacement = %d;\n", maxreplacement); fprintf(ofile,"int OO_state = 0;\n"); fprintf(ofile,"\n"); for(s=0;s<=higheststate;s++) { @@ -114,6 +114,9 @@ outdfa() fprintf(ofile,"static dfa%d(opcode)\n",s); fprintf(ofile,"\tint opcode;\n"); fprintf(ofile,"{\n"); + fprintf(ofile,"\t/* "); + outmnems(patterns[s]); + fprintf(ofile," */\n"); seenswitch = 0; for(p=states[s];p!=(struct state *)NULL;p=p->next) { if(!seenswitch) { @@ -133,19 +136,12 @@ outdfa() } fprintf(ofile,"\tdefault:\n"); fprintf(ofile,"\t\tOO_flush();\n"); - fprintf(ofile,"\t\tEM_mkcalls(*--OO_nxtpatt);\n"); - fprintf(ofile,"\t\tOO_free(*OO_nxtpatt);\n"); - fprintf(ofile,"\t\tbreak;\n"); - fprintf(ofile,"\tcase OTHER:\n"); - fprintf(ofile,"\t\tOO_flush();\n"); - fprintf(ofile,"\t\t--OO_nxtpatt;\n"); fprintf(ofile,"\t\tbreak;\n"); } else { if(seenswitch) fprintf(ofile,"\tdefault:\n"); findfail(s,&nout,&ncpy,&ngto); - fprintf(ofile,"\t\tOO_dodefault(%d,%d,%d);\n", - nout,ncpy,ngto); + fprintf(ofile,"\t\tOO_dodefault(%d,%d);\n",nout,ngto); if(actions[ngto]!=NULL) fprintf(ofile,"\t\tOO_%ddotrans();\n",ngto); if(seenswitch) fprintf(ofile,"\t\tbreak;\n"); @@ -171,24 +167,23 @@ outdotrans() int s; struct action *a; int seennontested; - int seentested; fprintf(ofile,"#include \"nopt.h\"\n\n"); for(s=0;s<=higheststate;s++) { if(actions[s]!=(struct action *)NULL) { fprintf(ofile,"\nOO_%ddotrans() {\n",s); - fprintf(ofile,"\tregister p_instr *patt = OO_patternqueue;\n"); + fprintf(ofile,"\tregister p_instr patt = OO_patternqueue;\n"); fprintf(ofile,"\t/* "); outmnems(patterns[s]); fprintf(ofile," */\n"); - seentested = seennontested=0; + seennontested=0; for(a=actions[s];a!=(struct action *)NULL;a=a->next) { if(a->test!=(struct exp_node *)NULL) { - seentested++; fprintf(ofile,"\tif("); outexp(a->test,s); fprintf(ofile,") {\n"); outoneaction(s,a); - fprintf(ofile,"\t\tgoto free;\n\t}\n"); + fprintf(ofile,"\t\treturn;\n"); + fprintf(ofile,"\t}\n"); } else { if(seennontested) { @@ -199,12 +194,8 @@ outdotrans() outoneaction(s,a); } } - if(!seennontested) { + if(!seennontested) fprintf(ofile,"\tOO_state=%d;\n",s); - fprintf(ofile,"\treturn;\n"); - } - if(seentested) fprintf(ofile,"free:"); - fprintf(ofile,"\tOO_nfree(%d);\n",patterns[s].m_len); fprintf(ofile,"}\n"); } /* @@ -243,32 +234,42 @@ outrepl(state,repl) char *mnem = ri->op_code->id_text; switch(ri->op_code->id_argfmt) { case NOARG: - fprintf(ofile,"\t\tOO_outop(op_%s);\n",mnem); + fprintf(ofile,"\t\tEM_mkop(GETNXTREPL(),op_%s);\n",mnem); break; case CST: - case CSTOPT: - fprintf(ofile,"\t\tOO_outcst(op_%s,",mnem); + fprintf(ofile,"\t\tEM_mkcst(GETNXTREPL(),op_%s,",mnem); fprintf(ofile,"(arith)"); outexp(ri->arg,state); fprintf(ofile,");\n"); break; + case CSTOPT: + if(ri->arg) { + fprintf(ofile,"\t\tEM_mkcst(GETNXTREPL(),op_%s,",mnem); + fprintf(ofile,"(arith)"); + outexp(ri->arg,state); + } + else { + fprintf(ofile,"\t\tEM_mknarg(GETNXTREPL(),op_%s);\n",mnem); + } + fprintf(ofile,");\n"); + break; case LAB: - fprintf(ofile,"\t\tOO_outlab(op_%s,",mnem); + fprintf(ofile,"\t\tEM_mkilb(GETNXTREPL(),op_%s,",mnem); outexp(ri->arg,state); fprintf(ofile,");\n"); break; case DEFILB: - fprintf(ofile,"\t\tOO_outdefilb(op_%s,",mnem); + fprintf(ofile,"\t\tEM_mkdefilb(GETNXTREPL(),op_%s,",mnem); outexp(ri->arg,state); fprintf(ofile,");\n"); break; case PNAM: - fprintf(ofile,"\t\tOO_outpnam(op_%s,",mnem); + fprintf(ofile,"\t\tEM_mkpro(GETNXTREPL(),op_%s,",mnem); outexp(ri->arg,state); fprintf(ofile,");\n"); break; case EXT: - fprintf(ofile,"\t\tOO_outext(op_%s,",mnem); + fprintf(ofile,"\t\tOO_mkext(GETNXTREPL(),op_%s,",mnem); outexp(ri->arg,state); fprintf(ofile,");\n"); break; @@ -316,10 +317,10 @@ outexp(e,state) fprintf(ofile,")"); break; case DEFINED: - fprintf(ofile,"(patt[%d]->em_argtype)",e->leaf_val-1); + fprintf(ofile,"DEFINED(patt[%d])",e->leaf_val-1); break; case UNDEFINED: - fprintf(ofile,"(patt[%d]->em_argtype==0)",e->leaf_val-1); + fprintf(ofile,"!DEFINED(patt[%d])",e->leaf_val-1); break; case COMMA: outext(e->exp_left); @@ -367,7 +368,7 @@ outexp(e,state) fprintf(ofile,"PNAM(patt[%d])",e->leaf_val-1); break; case EXT: - fprintf(ofile,"OO_offset(patt[%d])",e->leaf_val-1); + fprintf(ofile,"OO_offset(patt+%d)",e->leaf_val-1); break; } break; @@ -388,7 +389,7 @@ outext(e) 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 diff --git a/modules/src/em_opt/parser.g b/modules/src/em_opt/parser.g index 6f1afb30e..3edec5763 100644 --- a/modules/src/em_opt/parser.g +++ b/modules/src/em_opt/parser.g @@ -25,7 +25,8 @@ struct action *actions[MAXSTATES]; struct mnems patterns[MAXSTATES]; int higheststate = 0; /* Highest state yet allocated */ struct idf *ops; /* Chained list of all ops */ -int longestpattern = 0; +int maxpattern = 0; +int maxreplacement = 0; int nerrors = 0; static int lencurrpatt; @@ -77,8 +78,8 @@ patterns(struct exp_node **tests;) [ OPCODE { - if(++lencurrpatt>longestpattern) - longestpattern=lencurrpatt; + if(++lencurrpatt>maxpattern) + maxpattern=lencurrpatt; list = addelem(list,opval, (struct exp_node *)NULL); opval->id_used=1; if(lencurrpatt==1) @@ -169,7 +170,8 @@ action(struct mnem_list **list;) [ OPCODE { - lenthisrepl++; + if(++lenthisrepl>maxreplacement) + maxreplacement = lenthisrepl; test= (struct exp_node *)NULL; keepopval = opval; } @@ -347,7 +349,8 @@ constructlist(list,len) int len; { struct mnem_elem **p; - p = (struct mnem_elem **)Malloc(len*sizeof(struct mnem_elem *)); + p = (struct mnem_elem **) + Malloc((unsigned)(len*sizeof(struct mnem_elem *))); while(len--) { p[len] = list->elem; list = list->next; diff --git a/modules/src/em_opt/parser.h b/modules/src/em_opt/parser.h index d07d20483..412a5e69e 100644 --- a/modules/src/em_opt/parser.h +++ b/modules/src/em_opt/parser.h @@ -77,7 +77,8 @@ extern struct action *actions[MAXSTATES]; extern struct mnems patterns[MAXSTATES]; extern int higheststate; /* Highest state yet allocated */ extern struct idf *ops; /* Chained list of all ops */ -extern int longestpattern; +extern int maxpattern; +extern int maxreplacement; extern int nerrors; extern FILE *ofile; diff --git a/modules/src/em_opt/pseudo.r b/modules/src/em_opt/pseudo.r index fecb834f1..56fca96b6 100644 --- a/modules/src/em_opt/pseudo.r +++ b/modules/src/em_opt/pseudo.r @@ -1,17 +1,22 @@ df_dlb | label:l | - FLUSHDFA(); + register p_instr p = GETNXTPATT(); + FLUSHDFA(p); C_df_dlb(l); df_dnam | char *:s | - FLUSHDFA(); + register p_instr p = GETNXTPATT(); + FLUSHDFA(p); C_df_dnam(s); pro | char *:s arith:l | - FLUSHDFA(); + register p_instr p = GETNXTPATT(); + FLUSHDFA(p); C_pro(s,l); pro_narg | char *:s | - FLUSHDFA(); + register p_instr p = GETNXTPATT(); + FLUSHDFA(p); C_pro_narg(s); end | arith:l | - FLUSHDFA(); + register p_instr p = GETNXTPATT(); + FLUSHDFA(p); C_end(l); end_narg | | FLUSHDFA();