Changed trans.c from switch statement into series of small procedures. Output files from parser now only written if changed

This commit is contained in:
bruce 1987-07-09 09:02:41 +00:00
parent 74ea5e0bd7
commit 64578a3afd
6 changed files with 153 additions and 63 deletions

View file

@ -26,7 +26,7 @@ PREFLAGS= $(INCLDIR) -DPRIVATE=static# -DDEBUG
# from the patterns file each time an optimization is performed.
#PREFLAGS= $(PREFLAGS) -DSTATS
PROFFLAG= -O
CFLAGS = $(PREFLAGS) $(PROFFLAG) -J
CFLAGS = $(PREFLAGS) $(PROFFLAG)
LLOPT =
CMD = '$(CC) -c $(CFLAGS)'
@ -45,8 +45,8 @@ NOFILES = nopt.o dfa.o trans.o aux.o
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.save trans.c trans.c.save incalls.d incalls.r\
incalls.r.save pseudo.d
dfa.c dfa.c.new trans.h trans.h.new trans.c trans.c.new\
incalls.d incalls.r incalls.r.new pseudo.d
all: em_nopt $(LIBOPT)
@ -78,15 +78,9 @@ $(LIBOPT): $(NOFILES) mkstrct.o pseudo.d incalls.d
-sh -c 'ranlib $(LIBOPT)'
dfadummy: patterns parser
-mv dfa.c dfa.c.save
-mv trans.c trans.c.save
-mv incalls.r incalls.r.save
-/lib/cpp patterns | $(HOWMUCH) >/tmp/patts
parser </tmp/patts
-rm /tmp/patts
-if cmp -s dfa.c dfa.c.save; then mv dfa.c.save dfa.c; else exit 0; fi
-if cmp -s trans.c trans.c.save; then mv trans.c.save trans.c; else exit 0; fi
-if cmp -s incalls.r incalls.r.save; then mv incalls.r.save incalls.r; else exit 0; fi
touch dfadummy
# How to build program to parse patterns table and build c files.
@ -108,7 +102,7 @@ nopt.o: nopt.h
mkstrct.o: nopt.h
aux.o: nopt.h
pseudo.d: nopt.h makefuns.awk
dfa.o: nopt.h dfadummy
dfa.o: nopt.h trans.h dfadummy
trans.o: nopt.h dfadummy
incalls.d: nopt.h makefuns.awk
incalls.r: dfadummy

View file

@ -30,7 +30,7 @@ findworst(repl)
int s;
int mostbackups = 0;
if(n==0) {
fprintf(ofile,"\t\t\tOO_backup(%d);\n", longestpattern-1);
fprintf(ofile,"\t\tOO_backup(%d);\n", longestpattern-1);
return;
}
for(s=1;s<=higheststate;s++) {
@ -57,11 +57,12 @@ findworst(repl)
}
}
if(mostbackups)
fprintf(ofile,"\t\t\tOO_backup(%d);\n",mostbackups);
fprintf(ofile,"\t\tOO_backup(%d);\n",mostbackups);
}
findfail(state)
findfail(state,resout,rescpy,resgto)
int state;
int *resout, *rescpy, *resgto;
{
/*
/* If pattern matching fails in 'state', how many outputs and how many
@ -85,12 +86,16 @@ findfail(state)
continue;
if((leftmatch(patterns[s],patterns[state],i,n)==1)&&
patterns[s].m_len==(n-i+1)) {
fprintf(ofile,"\t{%d,%d,%d},",i-1,n-i+1,s);
return;
}
*resout = i-1;
*rescpy = n-i+1;
*resgto = s;
return;
}
}
}
fprintf(ofile,"\t{%d,0,0},",n);
*resout = n;
*rescpy = 0;
*resgto = 0;
}
PRIVATE int

View file

@ -117,6 +117,15 @@ OO_free(p)
*OO_nxtifree++ = p;
}
OO_nfree(n)
register int n;
{
while(n--) {
OO_free(*--OO_nxtpatt);
}
OO_state = 0;
}
char *
OO_freestr(str)
char *str;

View file

@ -11,7 +11,7 @@
#include <em_comp.h>
#include <system.h>
#include <idf_pkg.spec>
#include "emO_code.h"
#include <emO_code.h>
#define OTHER 255

View file

@ -9,10 +9,6 @@ outputincalls()
struct idf *op;
int opcode;
char *s;
if((ofile=fopen("incalls.r","w"))==NULL) {
fprintf(stderr,"Fatal Error: cannot open output file incalls.r\n");
sys_stop(S_EXIT);
}
for(op=ops;op!=(struct idf *)NULL;op=op->id_nextidf) {
opcode = op->id_opcode;
s = op->id_text;

View file

@ -9,22 +9,97 @@ FILE *ofile;
outputnopt()
{
if((ofile=fopen("dfa.c","w"))==NULL) {
fprintf(stderr,"Couldn't open dfa.c for output\n");
sys_stop(S_EXIT);
}
openofile("dfa.c");
outheaders();
outtables();
outdfa();
outdodefault();
installofile();
openofile("trans.h");
outtranshdr();
installofile();
openofile("trans.c");
outdotrans();
installofile();
openofile("incalls.r");
outputincalls();
installofile();
}
static char ofilename[80];
static char ofiletemp[80];
PRIVATE
openofile(filename)
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);
sys_stop(S_EXIT);
}
}
PRIVATE
installofile()
{
/*
* 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;
fclose(ofile);
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) {
fclose(f1);
RENAME(ofiletemp,ofilename);
return;
}
do {
c1 = getc(f1);
c2 = getc(f2);
} while (c1 == c2 && c1 != EOF);
fclose(f1);
fclose(f2);
if (c1 != c2) {
RENAME(ofiletemp,ofilename);
}
else UNLINK(ofiletemp);
}
PRIVATE
UNLINK(x)
char *x;
{
/* Must remove the file "x" */
unlink(x); /* systemcall to remove file */
}
PRIVATE
RENAME(x,y)
char *x, *y;
{
/* Must move the file "x" to the file "y" */
unlink(y);
if(link(x,y)!=0) {
fprintf(stderr,"Cannot link to %s",y);
sys_stop(S_EXIT);
}
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");
@ -34,15 +109,21 @@ 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);
if(s%4==3)
fprintf(ofile,"\n");
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");
@ -75,7 +156,7 @@ outdfa()
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_dotrans(%d); ",p->goto_state);
else fprintf(ofile,"OO_%ddotrans(); ",p->goto_state);
fprintf(ofile,"break;\n");
}
}
@ -115,6 +196,16 @@ outmnems(l)
fprintf(ofile,"%s ",l.m_elems[i-1]->op_code->id_text);
}
PRIVATE
outtranshdr()
{
register int s;
for(s=0;s<=higheststate;s++) {
if(actions[s]!=NULL)
fprintf(ofile,"extern OO_%ddotrans();\n",s);
}
}
PRIVATE
outdotrans()
{
@ -122,30 +213,25 @@ outdotrans()
int i;
struct state *p;
struct action *a;
int firsttest;
int seennontested;
if((ofile=fopen("trans.c","w"))==NULL) {
fprintf(stderr,"Fatal Error: cannot open output file trans.c\n");
sys_stop(S_EXIT);
}
fprintf(ofile,"#include \"nopt.h\"\n\n");
fprintf(ofile,"\nOO_dotrans(s) int s; {\n");
fprintf(ofile,"\tregister p_instr *patt = OO_patternqueue;\n");
fprintf(ofile,"\tswitch(OO_state=s) {\n");
fprintf(ofile,"\tdefault: return;\n");
for(s=0;s<=higheststate;s++)
for(s=0;s<=higheststate;s++) {
if(actions[s]!=(struct action *)NULL) {
fprintf(ofile,"\tcase %d: /*",s);
fprintf(ofile,"\nOO_%ddotrans() {\n",s);
fprintf(ofile,"\t/* ");
outmnems(patterns[s]);
fprintf(ofile," */\n");
fprintf(ofile,"\tregister p_instr *patt = OO_patternqueue;\n");
seennontested=0;
firsttest=1;
for(a=actions[s];a!=(struct action *)NULL;a=a->next) {
if(a->test!=(struct exp_node *)NULL) {
fprintf(ofile,"\t\tif(");
fprintf(ofile,"\tif(");
outexp(a->test,s);
fprintf(ofile,") {\n");
outoneaction(s,a);
fprintf(ofile,"\t\t\tgoto free%d;\n",patterns[s].m_len);
fprintf(ofile,"\t\t}\n");
fprintf(ofile,"\t\tgoto free;\n\t}\n");
}
else {
if(seennontested) {
@ -154,19 +240,19 @@ outdotrans()
}
seennontested++;
outoneaction(s,a);
fprintf(ofile,"\t\t\tgoto free%d;\n",patterns[s].m_len);
}
}
if(!seennontested)
fprintf(ofile,"\t\treturn;\n");
if(!seennontested) {
fprintf(ofile,"\tOO_state=%d;\n",s);
fprintf(ofile,"\treturn;\n");
}
fprintf(ofile,"free:\tOO_nfree(%d);\n",patterns[s].m_len);
fprintf(ofile,"}\n");
}
fprintf(ofile,"\t}\n");
for(i=longestpattern;i>0;i--)
fprintf(ofile," free%d: OO_free(*--OO_nxtpatt);\n",i);
fprintf(ofile," free0: ;\n");
fprintf(ofile,"\tOO_state=0;\n");
fprintf(ofile,"}\n");
fprintf(ofile,"\n");
/*
* else fprintf(ofile,"\nOO_%ddotrans() {\n\tOO_state=%d;\n}\n",s,s);
*/
}
}
PRIVATE
@ -176,7 +262,8 @@ outdodefault()
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_dotrans(p->nextstate);\n");
fprintf(ofile,"\tOO_state=p->nextstate;\n");
fprintf(ofile,"\tif(p->transstate) (*(p->transstate))();\n");
fprintf(ofile,"}\n");
}
@ -185,12 +272,11 @@ outoneaction(s,a)
int s;
struct action *a;
{
fprintf(ofile,"\t\t/* ");
fprintf(ofile," -> ");
fprintf(ofile,"\t\t/* -> ");
outmnems(a->replacement);
fprintf(ofile," */\n");
fprintf(ofile,"#ifdef STATS\n");
fprintf(ofile,"\t\t\tif(OO_wrstats) fprintf(stderr,\"%d\\n\");\n",a->linenum);
fprintf(ofile,"\t\tif(OO_wrstats) fprintf(stderr,\"%d\\n\");\n",a->linenum);
fprintf(ofile,"#endif\n");
outrepl(s,patterns[s],a->replacement);
findworst(a->replacement);
@ -212,31 +298,31 @@ outrepl(state,patt,repl)
char *mnem = ri->op_code->id_text;
switch(ri->op_code->id_argfmt) {
case NOARG:
fprintf(ofile,"\t\t\tOO_outop(op_%s);\n",mnem);
fprintf(ofile,"\t\tOO_outop(op_%s);\n",mnem);
break;
case CST:
case CSTOPT:
fprintf(ofile,"\t\t\tOO_outcst(op_%s,",mnem);
fprintf(ofile,"\t\tOO_outcst(op_%s,",mnem);
outexp(ri->arg,state);
fprintf(ofile,");\n");
break;
case LAB:
fprintf(ofile,"\t\t\tOO_outlab(op_%s,",mnem);
fprintf(ofile,"\t\tOO_outlab(op_%s,",mnem);
outexp(ri->arg,state);
fprintf(ofile,");\n");
break;
case DEFILB:
fprintf(ofile,"\t\t\tOO_outdefilb(op_%s,",mnem);
fprintf(ofile,"\t\tOO_outdefilb(op_%s,",mnem);
outexp(ri->arg,state);
fprintf(ofile,");\n");
break;
case PNAM:
fprintf(ofile,"\t\t\tOO_outpnam(op_%s,",mnem);
fprintf(ofile,"\t\tOO_outpnam(op_%s,",mnem);
outexp(ri->arg,state);
fprintf(ofile,");\n");
break;
case EXT:
fprintf(ofile,"\t\t\tOO_outext(op_%s,",mnem);
fprintf(ofile,"\t\tOO_outext(op_%s,",mnem);
outexp(ri->arg,state);
fprintf(ofile,");\n");
break;