From 40b9920f8fe70e31ce2cd8378db3f2685fafe04e Mon Sep 17 00:00:00 2001 From: bruce Date: Mon, 13 Jul 1987 15:03:27 +0000 Subject: [PATCH] dfa.c now a routine for each state rather than nested switch statement --- modules/src/em_opt/Makefile | 6 +- modules/src/em_opt/nopt.c | 18 +++- modules/src/em_opt/outputdfa.c | 155 ++++++++++++--------------------- 3 files changed, 76 insertions(+), 103 deletions(-) diff --git a/modules/src/em_opt/Makefile b/modules/src/em_opt/Makefile index 14b2ccb7e..e6ffdc48a 100644 --- a/modules/src/em_opt/Makefile +++ b/modules/src/em_opt/Makefile @@ -41,7 +41,7 @@ POFILES = parser.o syntax.o outputdfa.o outcalls.o findworst.o\ initlex.o Lpars.o GENFILES = Lpars.h Lpars.c parserdummy parser.c syntax.c dfadummy\ - dfa.c dfa.c.new trans.h trans.h.new trans.c trans.c.new\ + dfa.c dfa.c.new trans.c trans.c.new\ incalls.d incalls.r incalls.r.new pseudo.d all: em_nopt $(LIBOPT) em_opt.3 @@ -49,7 +49,7 @@ all: em_nopt $(LIBOPT) em_opt.3 install: all $(INSTALL) lib/$(LIBOPT) $(INSTALL) man/em_opt.3 - cp em_nopt.1 $(EMHOME)/man + cp em_nopt.6 $(EMHOME)/man cp em_nopt $(BINDIR)/em_nopt cmp: all @@ -145,7 +145,7 @@ nopt.o: nopt.h mkstrct.o: nopt.h aux.o: nopt.h pseudo.d: nopt.h makefuns.awk -dfa.o: nopt.h trans.h +dfa.o: nopt.h trans.o: nopt.h incalls.d: nopt.h makefuns.awk diff --git a/modules/src/em_opt/nopt.c b/modules/src/em_opt/nopt.c index 39f1706bb..7e1ced1c1 100644 --- a/modules/src/em_opt/nopt.c +++ b/modules/src/em_opt/nopt.c @@ -3,6 +3,8 @@ static char rcsid2[] = "$Header$"; #endif #include "nopt.h" + +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 @@ -61,6 +63,17 @@ O_close() C_close(); } +OO_dfa(last) + register int last; +{ + for(;;) { + (*OO_fstate[OO_state])(last); + if (OO_nxtbackup==OO_bkupqueue) + return; + last = ((*OO_nxtpatt++ = *(--OO_nxtbackup))->em_opcode); + } +} + PRIVATE fatal(s,a) char *s; @@ -276,10 +289,12 @@ OO_backup(n) } } -OO_dodefault(numout, numcopy) +OO_dodefault(numout, numcopy,newstate) register int numout, numcopy; + int newstate; { register p_instr *p, *q; + OO_pushback(*--OO_nxtpatt); q = (p = OO_patternqueue) + numout; while(numcopy--) { if(numout) { @@ -290,6 +305,7 @@ OO_dodefault(numout, numcopy) } OO_nxtpatt = p; while(numout--) OO_out(*p++); + OO_state = newstate; } #ifdef DEBUG diff --git a/modules/src/em_opt/outputdfa.c b/modules/src/em_opt/outputdfa.c index 18fa6a6aa..ffd433ec0 100644 --- a/modules/src/em_opt/outputdfa.c +++ b/modules/src/em_opt/outputdfa.c @@ -10,13 +10,7 @@ FILE *ofile; outputnopt() { openofile("dfa.c"); - outheaders(); - outtables(); outdfa(); - outdodefault(); - installofile(); - openofile("trans.h"); - outtranshdr(); installofile(); openofile("trans.c"); outdotrans(); @@ -96,96 +90,71 @@ RENAME(x,y) unlink(x); } -PRIVATE -outheaders() -{ - fprintf(ofile,"#include \"nopt.h\"\n"); - fprintf(ofile,"#include \"trans.h\"\n"); - fprintf(ofile,"\n"); - fprintf(ofile,"int OO_maxpattern = %d;\n", longestpattern); - fprintf(ofile,"\n"); -} - -PRIVATE -outtables() -{ - int s; - int nout, ncpy, ngto; - fprintf(ofile,"static struct defact {\n"); - fprintf(ofile,"\tint numoutput;\n"); - fprintf(ofile,"\tint numcopy;\n"); - fprintf(ofile,"\tint nextstate;\n"); - fprintf(ofile,"\tint (*transstate)();\n"); - fprintf(ofile,"} defaultactions[] = {\n"); - for(s=0;s<=higheststate;s++) { - findfail(s,&nout,&ncpy,&ngto); - fprintf(ofile,"\t{%d, %d, %d, ",nout,ncpy,ngto); - if(actions[ngto]==NULL) - fprintf(ofile,"0"); - else - fprintf(ofile,"OO_%ddotrans",ngto); - fprintf(ofile,"},\n"); - } - fprintf(ofile,"};\n"); - fprintf(ofile,"\n"); -} - PRIVATE outdfa() { int s; struct idf *op; struct state *p; + int nout, ncpy, ngto; + int seenswitch; + fprintf(ofile,"#include \"nopt.h\"\n"); + fprintf(ofile,"\n"); + fprintf(ofile,"int OO_maxpattern = %d;\n", longestpattern); fprintf(ofile,"int OO_state = 0;\n"); fprintf(ofile,"\n"); - fprintf(ofile,"OO_dfa(last) register int last; {\n"); - fprintf(ofile,"\tfor(;;) {\n"); - fprintf(ofile,"\t\tswitch(last) {\n"); - for(op=ops;op!=(struct idf *)NULL;op=op->id_nextidf) { - if(!op->id_used) - continue; - fprintf(ofile,"\t\tcase op_%s:\n",op->id_text); - fprintf(ofile,"\t\t\tswitch(OO_state) {\n"); - if(!op->id_startpatt) { - fprintf(ofile,"\t\t\tcase 0: "); - fprintf(ofile,"OO_out(*--OO_nxtpatt); "); - fprintf(ofile,"break;\n"); - } - for(s=0;s<=higheststate;s++) - for(p=states[s];p!=(struct state *)NULL;p=p->next) { - if(p->op==op) { - fprintf(ofile,"\t\t\tcase %d: ",s); - if(actions[p->goto_state]==(struct action *)NULL) - fprintf(ofile,"OO_state = %d; ",p->goto_state); - else fprintf(ofile,"OO_%ddotrans(); ",p->goto_state); - fprintf(ofile,"break;\n"); - } - } - fprintf(ofile,"\t\t\tdefault: dodefaultaction(); break;\n"); - fprintf(ofile,"\t\t\t}\n"); - fprintf(ofile,"\t\t\tbreak;\n"); + for(s=0;s<=higheststate;s++) { + fprintf(ofile,"static dfa%d();\n",s); + } + fprintf(ofile,"\nint (*OO_fstate[])()=\n{\n"); + for(s=0;s<=higheststate;s++) { + fprintf(ofile,"\tdfa%d,\n",s); + } + fprintf(ofile,"};\n\n"); + for(s=0;s<=higheststate;s++) { + fprintf(ofile,"static dfa%d(opcode)\n",s); + fprintf(ofile,"\tint opcode;\n"); + fprintf(ofile,"{\n"); + seenswitch = 0; + for(p=states[s];p!=(struct state *)NULL;p=p->next) { + if(!seenswitch) { + seenswitch++; + fprintf(ofile,"\tswitch(opcode) {\n"); + } + fprintf(ofile,"\tcase op_%s: ",p->op->id_text); + if(actions[p->goto_state]==(struct action *)NULL) + fprintf(ofile,"OO_state = %d; ",p->goto_state); + else fprintf(ofile,"OO_%ddotrans(); ",p->goto_state); + fprintf(ofile,"break;\n"); + } + if(s==0) { + if(!seenswitch) { + seenswitch++; + fprintf(ofile,"\tswitch(opcode) {\n"); + } + 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); + if(actions[ngto]!=NULL) + fprintf(ofile,"\t\tOO_%ddotrans();\n",ngto); + if(seenswitch) fprintf(ofile,"\t\tbreak;\n"); + } + if(seenswitch) fprintf(ofile,"\t}\n"); + fprintf(ofile,"}\n"); + fprintf(ofile,"\n"); } - fprintf(ofile,"\t\tdefault:\n"); - fprintf(ofile,"\t\t\tif(OO_state) dodefaultaction();\n"); - fprintf(ofile,"\t\t\telse {\n"); - fprintf(ofile,"\t\t\t\tOO_flush();\n"); - fprintf(ofile,"\t\t\t\tEM_mkcalls(*--OO_nxtpatt);\n"); - fprintf(ofile,"\t\t\t\tOO_free(*OO_nxtpatt);\n"); - fprintf(ofile,"\t\t\t}\n"); - fprintf(ofile,"\t\t\tbreak;\n"); - fprintf(ofile,"\t\tcase OTHER:\n"); - fprintf(ofile,"\t\t\tif(OO_state) dodefaultaction();\n"); - fprintf(ofile,"\t\t\telse {\n"); - fprintf(ofile,"\t\t\t\tOO_flush();\n"); - fprintf(ofile,"\t\t\t\t--OO_nxtpatt;\n"); - fprintf(ofile,"\t\t\t}\n"); - fprintf(ofile,"\t\t\tbreak;\n"); - fprintf(ofile,"\t\t}\n"); - fprintf(ofile,"\t\tif (OO_nxtbackup==OO_bkupqueue)\n"); - fprintf(ofile,"\t\t\treturn;\n"); - fprintf(ofile,"\t\tlast = ((*OO_nxtpatt++ = *(--OO_nxtbackup))->em_opcode);\n"); - fprintf(ofile,"\t}\n"); - fprintf(ofile,"}\n"); } PRIVATE @@ -255,18 +224,6 @@ outdotrans() } } -PRIVATE -outdodefault() -{ - fprintf(ofile,"\nstatic dodefaultaction() {\n"); - fprintf(ofile,"\tregister struct defact *p = &defaultactions[OO_state];\n"); - fprintf(ofile,"\tOO_pushback(*--OO_nxtpatt);\n"); - fprintf(ofile,"\tOO_dodefault(p->numoutput,p->numcopy);\n"); - fprintf(ofile,"\tOO_state=p->nextstate;\n"); - fprintf(ofile,"\tif(p->transstate) (*(p->transstate))();\n"); - fprintf(ofile,"}\n"); -} - PRIVATE outoneaction(s,a) int s;