dfa.c now a routine for each state rather than nested switch statement

This commit is contained in:
bruce 1987-07-13 15:03:27 +00:00
parent b93dc94cdb
commit 40b9920f8f
3 changed files with 76 additions and 103 deletions

View file

@ -41,7 +41,7 @@ POFILES = parser.o syntax.o outputdfa.o outcalls.o findworst.o\
initlex.o Lpars.o initlex.o Lpars.o
GENFILES = Lpars.h Lpars.c parserdummy parser.c syntax.c dfadummy\ 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 incalls.d incalls.r incalls.r.new pseudo.d
all: em_nopt $(LIBOPT) em_opt.3 all: em_nopt $(LIBOPT) em_opt.3
@ -49,7 +49,7 @@ all: em_nopt $(LIBOPT) em_opt.3
install: all install: all
$(INSTALL) lib/$(LIBOPT) $(INSTALL) lib/$(LIBOPT)
$(INSTALL) man/em_opt.3 $(INSTALL) man/em_opt.3
cp em_nopt.1 $(EMHOME)/man cp em_nopt.6 $(EMHOME)/man
cp em_nopt $(BINDIR)/em_nopt cp em_nopt $(BINDIR)/em_nopt
cmp: all cmp: all
@ -145,7 +145,7 @@ nopt.o: nopt.h
mkstrct.o: nopt.h mkstrct.o: nopt.h
aux.o: nopt.h aux.o: nopt.h
pseudo.d: nopt.h makefuns.awk pseudo.d: nopt.h makefuns.awk
dfa.o: nopt.h trans.h dfa.o: nopt.h
trans.o: nopt.h trans.o: nopt.h
incalls.d: nopt.h makefuns.awk incalls.d: nopt.h makefuns.awk

View file

@ -3,6 +3,8 @@ static char rcsid2[] = "$Header$";
#endif #endif
#include "nopt.h" #include "nopt.h"
extern int (*OO_fstate[])(); /* Initialized from patterns in dfa.c */
extern int OO_maxpattern; /* Initialized from patterns in dfa.c */ extern int OO_maxpattern; /* Initialized from patterns in dfa.c */
#define MAXBACKUP 50 #define MAXBACKUP 50
#define MAXOUTPUT 200 #define MAXOUTPUT 200
@ -61,6 +63,17 @@ O_close()
C_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 PRIVATE
fatal(s,a) fatal(s,a)
char *s; char *s;
@ -276,10 +289,12 @@ OO_backup(n)
} }
} }
OO_dodefault(numout, numcopy) OO_dodefault(numout, numcopy,newstate)
register int numout, numcopy; register int numout, numcopy;
int newstate;
{ {
register p_instr *p, *q; register p_instr *p, *q;
OO_pushback(*--OO_nxtpatt);
q = (p = OO_patternqueue) + numout; q = (p = OO_patternqueue) + numout;
while(numcopy--) { while(numcopy--) {
if(numout) { if(numout) {
@ -290,6 +305,7 @@ OO_dodefault(numout, numcopy)
} }
OO_nxtpatt = p; OO_nxtpatt = p;
while(numout--) OO_out(*p++); while(numout--) OO_out(*p++);
OO_state = newstate;
} }
#ifdef DEBUG #ifdef DEBUG

View file

@ -10,13 +10,7 @@ FILE *ofile;
outputnopt() outputnopt()
{ {
openofile("dfa.c"); openofile("dfa.c");
outheaders();
outtables();
outdfa(); outdfa();
outdodefault();
installofile();
openofile("trans.h");
outtranshdr();
installofile(); installofile();
openofile("trans.c"); openofile("trans.c");
outdotrans(); outdotrans();
@ -96,96 +90,71 @@ RENAME(x,y)
unlink(x); 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 PRIVATE
outdfa() outdfa()
{ {
int s; int s;
struct idf *op; struct idf *op;
struct state *p; 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,"int OO_state = 0;\n");
fprintf(ofile,"\n"); fprintf(ofile,"\n");
fprintf(ofile,"OO_dfa(last) register int last; {\n"); for(s=0;s<=higheststate;s++) {
fprintf(ofile,"\tfor(;;) {\n"); fprintf(ofile,"static dfa%d();\n",s);
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++) 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) { for(p=states[s];p!=(struct state *)NULL;p=p->next) {
if(p->op==op) { if(!seenswitch) {
fprintf(ofile,"\t\t\tcase %d: ",s); seenswitch++;
fprintf(ofile,"\tswitch(opcode) {\n");
}
fprintf(ofile,"\tcase op_%s: ",p->op->id_text);
if(actions[p->goto_state]==(struct action *)NULL) if(actions[p->goto_state]==(struct action *)NULL)
fprintf(ofile,"OO_state = %d; ",p->goto_state); fprintf(ofile,"OO_state = %d; ",p->goto_state);
else fprintf(ofile,"OO_%ddotrans(); ",p->goto_state); else fprintf(ofile,"OO_%ddotrans(); ",p->goto_state);
fprintf(ofile,"break;\n"); fprintf(ofile,"break;\n");
} }
if(s==0) {
if(!seenswitch) {
seenswitch++;
fprintf(ofile,"\tswitch(opcode) {\n");
} }
fprintf(ofile,"\t\t\tdefault: dodefaultaction(); break;\n"); fprintf(ofile,"\tdefault:\n");
fprintf(ofile,"\t\t\t}\n"); fprintf(ofile,"\t\tOO_flush();\n");
fprintf(ofile,"\t\t\tbreak;\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");
} }
fprintf(ofile,"\t\tdefault:\n"); else {
fprintf(ofile,"\t\t\tif(OO_state) dodefaultaction();\n"); if(seenswitch) fprintf(ofile,"\tdefault:\n");
fprintf(ofile,"\t\t\telse {\n"); findfail(s,&nout,&ncpy,&ngto);
fprintf(ofile,"\t\t\t\tOO_flush();\n"); fprintf(ofile,"\t\tOO_dodefault(%d,%d,%d);\n",
fprintf(ofile,"\t\t\t\tEM_mkcalls(*--OO_nxtpatt);\n"); nout,ncpy,ngto);
fprintf(ofile,"\t\t\t\tOO_free(*OO_nxtpatt);\n"); if(actions[ngto]!=NULL)
fprintf(ofile,"\t\t\t}\n"); fprintf(ofile,"\t\tOO_%ddotrans();\n",ngto);
fprintf(ofile,"\t\t\tbreak;\n"); if(seenswitch) fprintf(ofile,"\t\tbreak;\n");
fprintf(ofile,"\t\tcase OTHER:\n"); }
fprintf(ofile,"\t\t\tif(OO_state) dodefaultaction();\n"); if(seenswitch) fprintf(ofile,"\t}\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"); fprintf(ofile,"}\n");
fprintf(ofile,"\n");
}
} }
PRIVATE 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 PRIVATE
outoneaction(s,a) outoneaction(s,a)
int s; int s;