Browse Source

Ported code to Mantra and general clean up.

bruce 37 years ago
parent
commit
ee89196671

+ 16 - 13
modules/src/em_opt/Makefile

@@ -9,11 +9,11 @@ CSRC = nopt.c aux.c mkcalls.c pseudo.c outputdfa.c outcalls.c\
 
 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)
@@ -34,15 +34,18 @@ clean:
 		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)
@@ -63,7 +66,7 @@ dfadummy:	patterns parser
 
 # 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\

+ 3 - 3
modules/src/em_opt/aux.c

@@ -12,7 +12,7 @@ OO_rotate(w,amount)
 	return(((w<<amount)&highmask) | ((w >> (8*OO_WSIZE-amount))&lowmask));
 }
 
-OO_samesign(a,b)
+OO_signsame(a,b)
 	int a, b;
 {
 	return( (a ^ b) >= 0);
@@ -38,7 +38,7 @@ OO_ufit(val, nbits)
 	return((val&mask) == 0);
 }
 
-OO_sameext(a1,a2)
+OO_extsame(a1,a2)
 	struct instr *a1, *a2;
 {
 	if(a1->argtype != a2->argtype) return(0);
@@ -56,7 +56,7 @@ OO_sameext(a1,a2)
 	}
 }
 
-OO_samenam(a1,a2)
+OO_namsame(a1,a2)
 	struct instr *a1, *a2;
 {
 	if(a1->argtype != a2->argtype) return(0);

+ 17 - 105
modules/src/em_opt/nopt.c

@@ -3,7 +3,7 @@ static char rcsid[] = "$Header$";
 #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
@@ -36,21 +36,7 @@ int OO_WSIZE;	/* wordlength */
 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)
@@ -76,10 +62,6 @@ C_magic()
 
 C_close()
 {
-#ifdef COLLECT
-	if(cflag)
-		outputstats();
-#endif
 	O_close();
 }
 
@@ -99,7 +81,7 @@ allocmem()
 {
 	/* 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;
@@ -115,58 +97,20 @@ allocmem()
 	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 *
@@ -179,9 +123,6 @@ freestr(s)
 		fprint(STDERR,"string space overflowed!\n");
 		sys_stop(S_EXIT);
 	}
-#ifdef COLLECT
-	UPDATEMAX(higheststr,nextstr-strqueue);
-#endif
 	return(res);
 }
 
@@ -194,22 +135,9 @@ OO_flush()
 	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);
 		}
@@ -226,7 +154,7 @@ OO_outop(opcode)
 	register struct instr *p = GETINSTR();
 	p->opcode = opcode;
 	p->argtype = none_ptyp;
-	OO_output(p);
+	OO_out(p);
 }
 
 OO_inop(opcode)
@@ -245,7 +173,7 @@ OO_outcst(opcode,cst)
 	p->opcode = opcode;
 	p->argtype = cst_ptyp;
 	p->acst = cst;
-	OO_output(p);
+	OO_out(p);
 }
 
 OO_incst(opcode,cst)
@@ -265,7 +193,7 @@ OO_outlab(opcode,lab)
 	p->opcode = opcode;
 	p->argtype = cst_ptyp;
 	p->acst = lab;
-	OO_output(p);
+	OO_out(p);
 }
 
 OO_inlab(opcode,lab)
@@ -286,7 +214,7 @@ OO_outpnam(opcode,pnam)
 	p->opcode = opcode;
 	p->argtype = pro_ptyp;
 	p->apnam = pnam;
-	OO_output(p);
+	OO_out(p);
 }
 
 OO_inpnam(opcode,pnam)
@@ -308,7 +236,7 @@ OO_outdefilb(opcode,deflb)
 	p->opcode = opcode;
 	p->argtype = lab_ptyp;
 	p->alab = deflb;
-	OO_output(p);
+	OO_out(p);
 }
 
 OO_indefilb(opcode,deflb)
@@ -344,7 +272,7 @@ OO_outext(opcode,arg,soff)
 	default:
 		fatal("Unexpected type %d in outext",arg->argtype);
 	}
-	OO_output(p);
+	OO_out(p);
 }
 
 OO_indnam(opcode,name,off)
@@ -373,22 +301,18 @@ OO_indlb(opcode,lab,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)
@@ -403,10 +327,6 @@ OO_pushback(p)
 		return;
 	}
 	*OO_nxtbackup++ = p;
-#ifdef COLLECT
-	UPDATEMAX(highestbackup,OO_nxtbackup-OO_bkupqueue);
-	numpushs++;
-#endif
 }
 
 OO_backup(n)
@@ -414,10 +334,6 @@ 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--;
 	}
@@ -431,16 +347,12 @@ OO_dodefault(numout, numcopy)
 	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

+ 1 - 4
modules/src/em_opt/nopt.h

@@ -59,11 +59,8 @@ extern int OO_state;
 extern int OO_noutput;	/* number of instructions in output queue */
 extern int OO_WSIZE;	/* wordlength */
 extern int OO_PSIZE;	/* pointer length */
-#ifdef DEBUG
-extern int dflag;			/* debugging output */
-#endif
 #ifdef STATS
-extern int sflag;			/* statistics output */
+extern int OO_wrstats;			/* statistics output */
 #endif
 
 #define CST(p)		(p->acst)

+ 49 - 44
modules/src/em_opt/outputdfa.c

@@ -13,23 +13,25 @@ outputnopt()
 		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");
@@ -47,7 +49,7 @@ outputtables()
 }
 
 PRIVATE
-outputdfa()
+outdfa()
 {
 	int s;
 	struct idf *op;
@@ -64,7 +66,7 @@ outputdfa()
 		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++)
@@ -77,12 +79,12 @@ outputdfa()
 					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");
@@ -90,7 +92,7 @@ outputdfa()
 	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");
@@ -103,7 +105,7 @@ outputdfa()
 }
 
 PRIVATE
-outputmnems(l)
+outmnems(l)
 	struct mnems l;
 {
 	int i;
@@ -112,7 +114,7 @@ outputmnems(l)
 }
 
 PRIVATE
-outputdotrans()
+outdotrans()
 {
 	int s;
 	int i;
@@ -131,15 +133,15 @@ outputdotrans()
 	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");
 				}
@@ -149,7 +151,7 @@ outputdotrans()
 						nerrors++;
 					}
 					seennontested++;
-					outputoneaction(s,a);
+					outoneaction(s,a);
 					fprint(ofile,"\t\t\tgoto free%d;\n",patterns[s].m_len);
 				}
 			}
@@ -166,9 +168,9 @@ outputdotrans()
 }
 
 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");
@@ -177,20 +179,23 @@ outputdodefault()
 }
 
 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;
 {
@@ -210,27 +215,27 @@ outputrepl(state,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;
 		}
@@ -238,7 +243,7 @@ outputrepl(state,patt,repl)
 }
 
 PRIVATE
-outputexp(e,state)
+outexp(e,state)
 	struct exp_node *e;
 	int state;
 {
@@ -262,9 +267,9 @@ outputexp(e,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:
@@ -272,8 +277,8 @@ outputexp(e,state)
 	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:
@@ -284,21 +289,21 @@ outputexp(e,state)
 		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);
@@ -349,7 +354,7 @@ outext(e)
 }
 
 PRIVATE
-outputop(op)
+outop(op)
 	int op;
 {
 	switch(op) {
@@ -375,11 +380,11 @@ outputop(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;
 	}
 }