SRCS = Makefile nopt.h parser.h parser.g syntax.l patterns $(CSRC)
-NOFILES=nopt.o dfa.o trans.o incalls.o pseudo.o aux.o mkcalls.o
+NOFILES = nopt.o dfa.o trans.o incalls.o pseudo.o aux.o mkcalls.o
-POFILES=parser.o syntax.o outputdfa.o outcalls.o findworst.o initlex.o Lpars.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\
+GENFILES = Lpars.h Lpars.c parserdummy parser.c syntax.c dfadummy\
dfa.c dfa.c.save trans.c trans.c.save incalls.c incalls.c.save
all: $(LIBOPT)
rm -f $(NOFILES) $(POFILES) $(GENFILES) parser core makerror
# set HOWMUCH to head -20 to limit number of patterns used
-#HOWMUCH=head -20
-HOWMUCH=cat
-
-LEXLIB=-ll
-INCLDIR=-I$(EMHOME)/h -I$(EMHOME)/modules/h -I$(EMHOME)/modules/pkg
-PREFLAGS=$(INCLDIR) -DPRIVATE=static
-PROFFLAG=-O
-CFLAGS=$(PREFLAGS) $(PROFFLAG)
-LLOPT=
+#HOWMUCH = head -20
+HOWMUCH = cat
+
+LEXLIB = -ll
+INCLDIR = -I$(EMHOME)/h -I$(EMHOME)/modules/h -I$(EMHOME)/modules/pkg
+PREFLAGS = $(INCLDIR) -DPRIVATE=static
+# Enable the next line to produce a version that output's the line number
+# from the patterns file each time an optimization is performed.
+#PREFLAGS = $(PREFLAGS) -DSTATS
+PROFFLAG = -O
+CFLAGS = $(PREFLAGS) $(PROFFLAG)
+LLOPT =
$(LIBOPT): dfadummy $(NOFILES)
rm -f $(LIBOPT)
# How to build program to parse patterns table and build c files.
-PARSERLIB=$(EMHOME)/lib/em_data.a\
+PARSERLIB = $(EMHOME)/lib/em_data.a\
$(EMHOME)/modules/lib/libprint.a\
$(EMHOME)/modules/lib/liballoc.a\
$(EMHOME)/modules/lib/libstring.a\
#endif
#include "nopt.h"
-#define MAXPATTERN 15
+extern int maxpattern; /* Initialized from patterns in dfa.c */
#define MAXBACKUP 50
#define MAXOUTPUT 200
#define MAXFREEI 200
int OO_PSIZE; /* pointer length */
#ifdef STATS
-int sflag = 1; /* pattern statistics output */
-#endif
-#ifdef COLLECT
-int cflag = 0; /* internal statistics output */
-#define UPDATEMAX(oldmax,n) if(cflag&&n>oldmax) oldmax=n
-static int numwrites = 0;
-static int numpushs = 0;
-static int numbackups = 0;
-static int numflushes = 0;
-static int numfrees = 0;
-static int numdefaults = 0;
-static int highestbackup = 0, totalbackup = 0;
-static int highestoutput = 0, totaloutput = 0;
-static int highestfreei = 0, totalfreei = 0;
-static int higheststr = 0, totalstr = 0;
+int OO_wrstats = 1; /* pattern statistics output */
#endif
C_init(wsize,psize)
C_close()
{
-#ifdef COLLECT
- if(cflag)
- outputstats();
-#endif
O_close();
}
{
/* Allocate memory for queues on heap */
OO_nxtpatt = OO_patternqueue =
- (struct instr **)Malloc(MAXPATTERN*sizeof(struct instr *));
+ (struct instr **)Malloc(maxpattern*sizeof(struct instr *));
OO_nxtbackup = OO_bkupqueue =
(struct instr **)Malloc(MAXBACKUP*sizeof(struct instr *));
lastbackup = OO_bkupqueue + MAXBACKUP - 1;
laststr = strqueue + MAXSTRING - 1;
}
-#ifdef COLLECT
-PRIVATE
-outputstats()
-{
- int i;
- fprint(STDERR,"Total of %d instructions read, %d written\n",numreads,numwrites);
- fprint(STDERR,"Total of %d calls to flush\n",numflushes);
- fprint(STDERR,"Total of %d calls to myfree\n",numfrees);
- fprint(STDERR,"Total of %d instructions pushed back\n",numpushs-numbackups);
- fprint(STDERR,"Total of %d instructions backed up\n",numbackups);
- fprint(STDERR,"Total of %d calls to dodefault\n",numdefaults);
- fprint(STDERR,"Max of highestbackup\t%d\t(%d)\t",highestbackup,MAXBACKUP);
- printav(totalbackup);
- fprint(STDERR,"Max of highestoutput\t%d\t(%d)\t",highestoutput,MAXOUTPUT);
- printav(totaloutput);
- fprint(STDERR,"Max of highestfreei\t%d\t(%d)\t",highestfreei,MAXFREEI);
- printav(totalfreei);
- fprint(STDERR,"Max of higheststr\t%d\t(%d)\t",higheststr,MAXSTRING);
- printav(totalstr);
-}
-
-PRIVATE
-printav(n)
- int n;
-{
- fprint(STDERR,"Av.\t%d.%d\n",n/numflushes,(n*10)%numflushes);
-}
-#endif
-
OO_free(p)
struct instr *p;
{
-#ifdef DEBUG
- fprint(STDERR,"about to free ");
- prtinst(p);
- fprint(STDERR,"\n");
-#endif
-#ifdef COLLECT
- if(cflag)
- numfrees++;
-#endif
if(nextifree > lastifree) {
#ifdef DEBUG
- fprint(STDERR,"Warning: Overflow of freeiqueue-free arg ignored\n");
+ fprint(STDERR,"Warning: Overflow of free intr. queue.\n");
+ fprint(STDERR,"Ignored free of ");
+ prtinst(p);
+ fprint(STDERR,"\n");
printstate("Freea overflow");
#endif
return;
}
*nextifree++ = p;
-#ifdef COLLECT
- UPDATEMAX(highestfreei,nextifree-freeiqueue);
-#endif
}
PRIVATE char *
fprint(STDERR,"string space overflowed!\n");
sys_stop(S_EXIT);
}
-#ifdef COLLECT
- UPDATEMAX(higheststr,nextstr-strqueue);
-#endif
return(res);
}
struct instr **p;
#ifdef DEBUG
printstate("Flush");
-#endif
-#ifdef COLLECT
- if(cflag) {
- numflushes++;
- totaloutput += nextoutput-outputqueue;
- totalbackup += OO_nxtbackup-OO_bkupqueue;
- totalfreei += nextifree-freeiqueue;
- totalstr += nextstr-strqueue;
- }
#endif
if(OO_noutput) {
for(p=outputqueue;p<nextoutput;p++) {
-#ifdef COLLECT
- if(cflag)
- numwrites++;
-#endif
OO_mkcalls(*p);
OO_free(*p);
}
register struct instr *p = GETINSTR();
p->opcode = opcode;
p->argtype = none_ptyp;
- OO_output(p);
+ OO_out(p);
}
OO_inop(opcode)
p->opcode = opcode;
p->argtype = cst_ptyp;
p->acst = cst;
- OO_output(p);
+ OO_out(p);
}
OO_incst(opcode,cst)
p->opcode = opcode;
p->argtype = cst_ptyp;
p->acst = lab;
- OO_output(p);
+ OO_out(p);
}
OO_inlab(opcode,lab)
p->opcode = opcode;
p->argtype = pro_ptyp;
p->apnam = pnam;
- OO_output(p);
+ OO_out(p);
}
OO_inpnam(opcode,pnam)
p->opcode = opcode;
p->argtype = lab_ptyp;
p->alab = deflb;
- OO_output(p);
+ OO_out(p);
}
OO_indefilb(opcode,deflb)
default:
fatal("Unexpected type %d in outext",arg->argtype);
}
- OO_output(p);
+ OO_out(p);
}
OO_indnam(opcode,name,off)
*OO_nxtpatt++ = p;
}
-OO_output(p)
+OO_out(p)
struct instr *p;
{
/* Put the instruction p on the output queue */
if(nextoutput > lastoutput) {
#ifdef DEBUG
- fprint(STDERR,"Overflow of outputqueue - output flushed\n");
- printstate("Output overflow");
+ fprint(STDERR,"Warning: Overflow of outputqueue - output flushed\n");
#endif
OO_flush();
}
OO_noutput++;
*nextoutput++ = p;
-#ifdef COLLECT
- UPDATEMAX(highestoutput,nextoutput-outputqueue);
-#endif
}
OO_pushback(p)
return;
}
*OO_nxtbackup++ = p;
-#ifdef COLLECT
- UPDATEMAX(highestbackup,OO_nxtbackup-OO_bkupqueue);
- numpushs++;
-#endif
}
OO_backup(n)
{
/* copy (up to) n instructions from output to backup queues */
while(n-- && nextoutput>outputqueue) {
-#ifdef COLLECT
- if(cflag)
- numbackups++;
-#endif
OO_pushback(*(--nextoutput));
OO_noutput--;
}
while(numcopy--) {
if(numout) {
numout--;
- OO_output(*p);
+ OO_out(*p);
}
*p++ = *q++;
}
OO_nxtpatt = p;
- while(numout--) OO_output(*p++);
-#ifdef COLLECT
- if(cflag)
- numdefaults++;
-#endif
+ while(numout--) OO_out(*p++);
}
#ifdef DEBUG
fprint(STDERR,"Couldn't open dfa.c for output\n");
sys_stop(S_EXIT);
}
- outputheaders();
- outputtables();
- outputdfa();
- outputdodefault();
- outputdotrans();
+ outheaders();
+ outtables();
+ outdfa();
+ outdodefault();
+ outdotrans();
outputincalls();
}
PRIVATE
-outputheaders()
+outheaders()
{
fprint(ofile,"#include \"nopt.h\"\n");
fprint(ofile,"\n");
+ fprint(ofile,"int maxpattern = %d;\n", longestpattern);
+ fprint(ofile,"\n");
}
PRIVATE
-outputtables()
+outtables()
{
int s;
fprint(ofile,"static struct defact {\n");
}
PRIVATE
-outputdfa()
+outdfa()
{
int s;
struct idf *op;
fprint(ofile,"\t\t\tswitch(OO_state) {\n");
if(!op->id_startpatt) {
fprint(ofile,"\t\t\tcase 0: ");
- fprint(ofile,"OO_output(*--OO_nxtpatt); ");
+ fprint(ofile,"OO_out(*--OO_nxtpatt); ");
fprint(ofile,"break;\n");
}
for(s=0;s<=higheststate;s++)
fprint(ofile,"break;\n");
}
}
- fprint(ofile,"\t\t\tdefault: defaultaction(); break;\n");
+ fprint(ofile,"\t\t\tdefault: dodefaultaction(); break;\n");
fprint(ofile,"\t\t\t}\n");
fprint(ofile,"\t\t\tbreak;\n");
}
fprint(ofile,"\t\tdefault:\n");
- fprint(ofile,"\t\t\tif(OO_state) defaultaction();\n");
+ fprint(ofile,"\t\t\tif(OO_state) dodefaultaction();\n");
fprint(ofile,"\t\t\telse {\n");
fprint(ofile,"\t\t\t\tOO_flush();\n");
fprint(ofile,"\t\t\t\tOO_mkcalls(*--OO_nxtpatt);\n");
fprint(ofile,"\t\t\t}\n");
fprint(ofile,"\t\t\tbreak;\n");
fprint(ofile,"\t\tcase OTHER:\n");
- fprint(ofile,"\t\t\tif(OO_state) defaultaction();\n");
+ fprint(ofile,"\t\t\tif(OO_state) dodefaultaction();\n");
fprint(ofile,"\t\t\telse {\n");
fprint(ofile,"\t\t\t\tOO_flush();\n");
fprint(ofile,"\t\t\t\tOO_free(*--OO_nxtpatt);\n");
}
PRIVATE
-outputmnems(l)
+outmnems(l)
struct mnems l;
{
int i;
}
PRIVATE
-outputdotrans()
+outdotrans()
{
int s;
int i;
for(s=0;s<=higheststate;s++)
if(actions[s]!=(struct action *)NULL) {
fprint(ofile,"\tcase %d: /*",s);
- outputmnems(patterns[s]);
+ outmnems(patterns[s]);
fprint(ofile," */\n");
seennontested=0;
for(a=actions[s];a!=(struct action *)NULL;a=a->next) {
if(a->test!=(struct exp_node *)NULL) {
fprint(ofile,"\t\tif(");
- outputexp(a->test,s);
+ outexp(a->test,s);
fprint(ofile,") {\n");
- outputoneaction(s,a);
+ outoneaction(s,a);
fprint(ofile,"\t\t\tgoto free%d;\n",patterns[s].m_len);
fprint(ofile,"\t\t}\n");
}
nerrors++;
}
seennontested++;
- outputoneaction(s,a);
+ outoneaction(s,a);
fprint(ofile,"\t\t\tgoto free%d;\n",patterns[s].m_len);
}
}
}
PRIVATE
-outputdodefault()
+outdodefault()
{
- fprint(ofile,"\nstatic defaultaction() {\n");
+ fprint(ofile,"\nstatic dodefaultaction() {\n");
fprint(ofile,"\tregister struct defact *p = &defaultactions[OO_state];\n");
fprint(ofile,"\tOO_pushback(*--OO_nxtpatt);\n");
fprint(ofile,"\tOO_dodefault(p->numoutput,p->numcopy);\n");
}
PRIVATE
-outputoneaction(s,a)
+outoneaction(s,a)
int s;
struct action *a;
{
fprint(ofile,"\t\t/* ");
fprint(ofile," -> ");
- outputmnems(a->replacement);
+ outmnems(a->replacement);
fprint(ofile," */\n");
- outputrepl(s,patterns[s],a->replacement);
+ fprint(ofile,"#ifdef STATS\n");
+ fprint(ofile,"\t\t\tif(OO_wrstats) fprint(STDERR,\"%d\\n\");\n",a->linenum);
+ fprint(ofile,"#endif\n");
+ outrepl(s,patterns[s],a->replacement);
findworst(a->replacement);
}
PRIVATE
-outputrepl(state,patt,repl)
+outrepl(state,patt,repl)
int state;
struct mnems patt,repl;
{
case CST:
case CSTOPT:
fprint(ofile,"\t\t\tOO_outcst(op_%s,",mnem);
- outputexp(ri->arg,state);
+ outexp(ri->arg,state);
fprint(ofile,");\n");
break;
case LAB:
fprint(ofile,"\t\t\tOO_outlab(op_%s,",mnem);
- outputexp(ri->arg,state);
+ outexp(ri->arg,state);
fprint(ofile,");\n");
break;
case DEFILB:
fprint(ofile,"\t\t\tOO_outdefilb(op_%s,",mnem);
- outputexp(ri->arg,state);
+ outexp(ri->arg,state);
fprint(ofile,");\n");
break;
case PNAM:
fprint(ofile,"\t\t\tOO_outpnam(op_%s,",mnem);
- outputexp(ri->arg,state);
+ outexp(ri->arg,state);
fprint(ofile,");\n");
break;
case EXT:
fprint(ofile,"\t\t\tOO_outext(op_%s,",mnem);
- outputexp(ri->arg,state);
+ outexp(ri->arg,state);
fprint(ofile,");\n");
break;
}
}
PRIVATE
-outputexp(e,state)
+outexp(e,state)
struct exp_node *e;
int state;
{
case LSHIFT:
case RSHIFT:
fprint(ofile,"(");
- outputexp(e->exp_left,state);
- outputop(e->node_type);
- outputexp(e->exp_right,state);
+ outexp(e->exp_left,state);
+ outop(e->node_type);
+ outexp(e->exp_right,state);
fprint(ofile,")");
break;
case NOT:
case UPLUS:
case UMINUS:
fprint(ofile,"(");
- outputop(e->node_type);
- outputexp(e->exp_left,state);
+ outop(e->node_type);
+ outexp(e->exp_left,state);
fprint(ofile,")");
break;
case DEFINED:
break;
case COMMA:
outext(e->exp_left);
- fprint(ofile,","); outputexp(e->exp_right,state);
+ fprint(ofile,","); outexp(e->exp_right,state);
break;
case SAMESIGN:
case SFIT:
case UFIT:
case ROTATE:
- outputop(e->node_type);
- outputexp(e->exp_left,state);
+ outop(e->node_type);
+ outexp(e->exp_left,state);
fprint(ofile,",");
- outputexp(e->exp_right,state);
+ outexp(e->exp_right,state);
fprint(ofile,")");
break;
case SAMEEXT:
case SAMENAM:
- outputop(e->node_type);
+ outop(e->node_type);
outext(e->exp_left);
fprint(ofile,",");
outext(e->exp_right,state);
}
PRIVATE
-outputop(op)
+outop(op)
int op;
{
switch(op) {
case COMP: fprint(ofile,"~"); break;
case UPLUS: fprint(ofile,"+"); break;
case UMINUS: fprint(ofile,"-"); break;
- case SAMESIGN: fprint(ofile,"OO_samesign("); break;
+ case SAMESIGN: fprint(ofile,"OO_signsame("); break;
case SFIT: fprint(ofile,"OO_sfit("); break;
case UFIT: fprint(ofile,"OO_ufit("); break;
case ROTATE: fprint(ofile,"OO_rotate("); break;
- case SAMEEXT: fprint(ofile,"OO_sameext("); break;
- case SAMENAM: fprint(ofile,"OO_samenam("); break;
+ case SAMEEXT: fprint(ofile,"OO_extsame("); break;
+ case SAMENAM: fprint(ofile,"OO_namsame("); break;
}
}