Initial revision
authorsater <none@none>
Thu, 17 May 1984 13:42:36 +0000 (13:42 +0000)
committersater <none@none>
Thu, 17 May 1984 13:42:36 +0000 (13:42 +0000)
30 files changed:
util/opt/Makefile [new file with mode: 0644]
util/opt/alloc.c [new file with mode: 0644]
util/opt/alloc.h [new file with mode: 0644]
util/opt/assert.h [new file with mode: 0644]
util/opt/backward.c [new file with mode: 0644]
util/opt/cleanup.c [new file with mode: 0644]
util/opt/ext.h [new file with mode: 0644]
util/opt/flow.c [new file with mode: 0644]
util/opt/getline.c [new file with mode: 0644]
util/opt/line.h [new file with mode: 0644]
util/opt/lookup.c [new file with mode: 0644]
util/opt/lookup.h [new file with mode: 0644]
util/opt/main.c [new file with mode: 0644]
util/opt/makedepend [new file with mode: 0755]
util/opt/mktab.y [new file with mode: 0644]
util/opt/optim.h [new file with mode: 0644]
util/opt/param.h [new file with mode: 0644]
util/opt/pattern.h [new file with mode: 0644]
util/opt/patterns [new file with mode: 0644]
util/opt/peephole.c [new file with mode: 0644]
util/opt/process.c [new file with mode: 0644]
util/opt/proinf.h [new file with mode: 0644]
util/opt/putline.c [new file with mode: 0644]
util/opt/reg.c [new file with mode: 0644]
util/opt/scan.l [new file with mode: 0644]
util/opt/special.c [new file with mode: 0644]
util/opt/testopt [new file with mode: 0755]
util/opt/types.h [new file with mode: 0644]
util/opt/util.c [new file with mode: 0644]
util/opt/var.c [new file with mode: 0644]

diff --git a/util/opt/Makefile b/util/opt/Makefile
new file mode 100644 (file)
index 0000000..d965e66
--- /dev/null
@@ -0,0 +1,197 @@
+CFILES=main.c getline.c lookup.c var.c process.c backward.c util.c\
+       alloc.c putline.c cleanup.c peephole.c flow.c reg.c
+OFILES=main.o getline.o lookup.o var.o process.o backward.o util.o\
+       alloc.o putline.o cleanup.o peephole.o flow.o reg.o
+KFILES=main.k getline.k lookup.k var.k process.k backward.k util.k\
+       alloc.k putline.k cleanup.k peephole.k flow.k reg.k
+LIBS=../../lib/em_data.a
+CFLAGS=-O -DNDEBUG
+LDFLAGS=-i
+LINT=lint
+OPR=wide|opr
+XREF=xref -c -w80
+PROPTS=
+# LEXLIB is implementation dependent, try -ll or -lln first
+LEXLIB=-lln
+
+opt:    $(OFILES) pattern.o $(LIBS)
+       cc $(LDFLAGS) $(CFLAGS) $(OFILES) pattern.o $(LIBS) -o opt
+
+test:   opt
+       testopt
+
+cmp :   opt
+       cmp opt ../../lib/em_opt
+
+install:opt
+       -size opt ../../lib/em_opt
+       cp opt ../../lib/em_opt
+
+pattern.c:      patterns mktab
+       ../../lib/cpp patterns | mktab > pattern.c
+
+mktab:  mktab.o $(LIBS)
+       cc $(CFLAGS) mktab.o $(LIBS) $(LEXLIB) -o mktab
+
+depend:
+       makedepend
+
+lint:   $(CFILES) pattern.c
+       $(LINT) $(CFILES) pattern.c>lint 2>&1
+
+printall:
+       -pr $(PROPTS) Makefile -n *.h `ls $(CFILES)` mktab.y scan.l patterns|$(OPR)
+       touch print
+
+print:  Makefile *.h $(CFILES) mktab.y scan.l patterns
+       -pr $(PROPTS) $? | $(OPR)
+
+opr:
+       make pr ^ $(OPR)
+
+pr:
+       @pr $(PROPTS) Makefile *.h $(CFILES) mktab.y scan.l patterns
+
+xref:
+       $(XREF) *.h $(CFILES) | pr $(PROPTS) -h "XREF EMOPT"|$(OPR)&
+
+sizes:  opt
+       -nm opt | sort -n| /usr/plain/bin/map
+
+clean:
+       rm -f *.o opt mktab mktab.c scan.c pattern.c
+
+kfiles: $(KFILES)
+
+.SUFFIXES: .k
+.c.k: ; cem -c $*.c
+
+# the next lines are generated automatically
+# AUTOAUTOAUTOAUTOAUTOAUTOAUTOAUTOAUTOAUTOAUTOAUTO
+alloc.o:       alloc.h
+alloc.o:       assert.h
+alloc.o:       line.h
+alloc.o:       lookup.h
+alloc.o:       param.h
+alloc.o:       proinf.h
+alloc.o:       types.h
+backward.o:    ../../h/em_mnem.h
+backward.o:    ../../h/em_pseu.h
+backward.o:    ../../h/em_spec.h
+backward.o:    alloc.h
+backward.o:    assert.h
+backward.o:    ext.h
+backward.o:    line.h
+backward.o:    lookup.h
+backward.o:    param.h
+backward.o:    proinf.h
+backward.o:    types.h
+cleanup.o:     ../../h/em_mes.h
+cleanup.o:     ../../h/em_pseu.h
+cleanup.o:     ../../h/em_spec.h
+cleanup.o:     assert.h
+cleanup.o:     ext.h
+cleanup.o:     lookup.h
+cleanup.o:     param.h
+cleanup.o:     types.h
+flow.o:        ../../h/em_flag.h
+flow.o:        ../../h/em_mnem.h
+flow.o:        ../../h/em_spec.h
+flow.o:        alloc.h
+flow.o:        ext.h
+flow.o:        line.h
+flow.o:        optim.h
+flow.o:        param.h
+flow.o:        proinf.h
+flow.o:        types.h
+getline.o:     ../../h/em_flag.h
+getline.o:     ../../h/em_mes.h
+getline.o:     ../../h/em_pseu.h
+getline.o:     ../../h/em_spec.h
+getline.o:     alloc.h
+getline.o:     assert.h
+getline.o:     ext.h
+getline.o:     line.h
+getline.o:     lookup.h
+getline.o:     param.h
+getline.o:     proinf.h
+getline.o:     types.h
+lookup.o:      alloc.h
+lookup.o:      lookup.h
+lookup.o:      param.h
+lookup.o:      proinf.h
+lookup.o:      types.h
+main.o:        ../../h/em_spec.h
+main.o:        alloc.h
+main.o:        ext.h
+main.o:        param.h
+main.o:        types.h
+mktab.o:       ../../h/em_mnem.h
+mktab.o:       ../../h/em_spec.h
+mktab.o:       optim.h
+mktab.o:       param.h
+mktab.o:       pattern.h
+mktab.o:       scan.c
+mktab.o:       types.h
+pattern.o:     param.h
+pattern.o:     pattern.h
+pattern.o:     types.h
+peephole.o:    ../../h/em_mnem.h
+peephole.o:    ../../h/em_spec.h
+peephole.o:    alloc.h
+peephole.o:    assert.h
+peephole.o:    ext.h
+peephole.o:    line.h
+peephole.o:    lookup.h
+peephole.o:    optim.h
+peephole.o:    param.h
+peephole.o:    pattern.h
+peephole.o:    proinf.h
+peephole.o:    types.h
+process.o:     ../../h/em_pseu.h
+process.o:     ../../h/em_spec.h
+process.o:     alloc.h
+process.o:     assert.h
+process.o:     ext.h
+process.o:     line.h
+process.o:     lookup.h
+process.o:     param.h
+process.o:     proinf.h
+process.o:     types.h
+putline.o:     ../../h/em_flag.h
+putline.o:     ../../h/em_mnem.h
+putline.o:     ../../h/em_pseu.h
+putline.o:     ../../h/em_spec.h
+putline.o:     alloc.h
+putline.o:     assert.h
+putline.o:     ext.h
+putline.o:     line.h
+putline.o:     lookup.h
+putline.o:     optim.h
+putline.o:     param.h
+putline.o:     proinf.h
+putline.o:     types.h
+reg.o: ../../h/em_mes.h
+reg.o: ../../h/em_pseu.h
+reg.o: ../../h/em_spec.h
+reg.o: alloc.h
+reg.o: assert.h
+reg.o: ext.h
+reg.o: line.h
+reg.o: param.h
+reg.o: proinf.h
+reg.o: types.h
+scan.o:        stdio.h
+special.o:     param.h
+special.o:     types.h
+util.o:        assert.h
+util.o:        ext.h
+util.o:        lookup.h
+util.o:        optim.h
+util.o:        param.h
+util.o:        proinf.h
+util.o:        types.h
+var.o: lookup.h
+var.o: param.h
+var.o: proinf.h
+var.o: types.h
diff --git a/util/opt/alloc.c b/util/opt/alloc.c
new file mode 100644 (file)
index 0000000..df82cfe
--- /dev/null
@@ -0,0 +1,441 @@
+#include <stdio.h>
+#include "param.h"
+#include "types.h"
+#include "assert.h"
+#include "alloc.h"
+#include "line.h"
+#include "lookup.h"
+#include "proinf.h"
+
+/*
+ * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ *
+ *          This product is part of the Amsterdam Compiler Kit.
+ *
+ * Permission to use, sell, duplicate or disclose this software must be
+ * obtained in writing. Requests for such permissions may be sent to
+ *
+ *      Dr. Andrew S. Tanenbaum
+ *      Wiskundig Seminarium
+ *      Vrije Universiteit
+ *      Postbox 7161
+ *      1007 MC Amsterdam
+ *      The Netherlands
+ *
+ * Author: Hans van Staveren
+ */
+
+#ifdef USEMALLOC
+
+short * myalloc();
+
+#define newcore(size) myalloc(size)
+#define oldcore(p,size) free(p)
+
+#else
+
+/* #define CORECHECK   /* if defined tests are made to insure
+                          each block occurs at most once */
+
+#define CCHUNK 1024    /* number of shorts asked from system */
+
+short *newcore(),*freshcore();
+extern char *sbrk();
+
+#ifdef COREDEBUG
+int shortsasked=0;
+#endif
+
+#endif
+
+/*
+ * The following two sizetables contain the sizes of the various kinds
+ * of line and argument structures.
+ * Care has been taken to make this table implementation independent,
+ * but if you think very hard you might find a compiler failing the
+ * assumptions made.
+ * A wasteful but safe approach is to replace every line of them by
+ *  sizeof(line_t)
+ * and
+ *  sizeof(arg_t)
+ * respectively.
+ */
+
+#define LBASE (sizeof(line_t)-sizeof(un_l_a))
+
+int lsizetab[] = {
+       LBASE,
+       LBASE+sizeof(short),
+       LBASE+sizeof(offset),
+       LBASE+sizeof(num_p),
+       LBASE+sizeof(sym_p),
+       LBASE+sizeof(s_la_sval),
+       LBASE+sizeof(s_la_lval),
+       LBASE+sizeof(arg_p),
+       LBASE
+};
+
+#define ABASE (sizeof(arg_t)-sizeof(un_a_a))
+
+int asizetab[] = {
+       ABASE+sizeof(offset),
+       ABASE+sizeof(num_p),
+       ABASE+sizeof(sym_p),
+       ABASE+sizeof(s_a_val),
+       ABASE+sizeof(argb_t),
+       ABASE+sizeof(s_a_con),
+       ABASE+sizeof(s_a_con),
+       ABASE+sizeof(s_a_con),
+};
+
+/*
+ * alloc routines:
+ * Two parts:
+ *   1)        typed alloc and free routines
+ *   2) untyped raw core allocation
+ */
+
+/*
+ * PART 1
+ */
+
+line_p newline(optyp) int optyp; {
+       register line_p lnp;
+       register kind=optyp;
+
+       if (kind>OPMINI)
+               kind = OPMINI;
+       lnp = (line_p) newcore(lsizetab[kind]);
+       lnp->l_optyp = optyp;
+       return(lnp);
+}
+
+oldline(lnp) register line_p lnp; {
+       register kind=lnp->l_optyp&BMASK;
+
+       if (kind>OPMINI)
+               kind = OPMINI;
+       if (kind == OPLIST)
+               oldargs(lnp->l_a.la_arg);
+       oldcore((short *) lnp,lsizetab[kind]);
+}
+
+arg_p newarg(kind) int kind; {
+       register arg_p ap;
+
+       ap = (arg_p) newcore(asizetab[kind]);
+       ap->a_typ = kind;
+       return(ap);
+}
+
+oldargs(ap) register arg_p ap; {
+       register arg_p  next;
+
+       while (ap != (arg_p) 0) {
+               next = ap->a_next;
+               switch(ap->a_typ) {
+               case ARGSTR:
+                       oldargb(ap->a_a.a_string.ab_next);
+                       break;
+               case ARGICN:
+               case ARGUCN:
+               case ARGFCN:
+                       oldargb(ap->a_a.a_con.ac_con.ab_next);
+                       break;
+               }
+               oldcore((short *) ap,asizetab[ap->a_typ]);
+               ap = next;
+       }
+}
+
+oldargb(abp) register argb_p abp; {
+       register argb_p next;
+
+       while (abp != (argb_p) 0) {
+               next = abp->ab_next;
+               oldcore((short *) abp,sizeof (argb_t));
+               abp = next;
+       }
+}
+
+reg_p newreg() {
+
+       return((reg_p) newcore(sizeof(reg_t)));
+}
+
+oldreg(rp) reg_p rp; {
+
+       oldcore((short *) rp,sizeof(reg_t));
+}
+
+num_p newnum() {
+
+       return((num_p) newcore(sizeof(num_t)));
+}
+
+oldnum(lp) num_p lp; {
+
+       oldcore((short *) lp,sizeof(num_t));
+}
+
+offset *newrom() {
+
+       return((offset *) newcore(MAXROM*sizeof(offset)));
+}
+
+sym_p newsym() {
+
+       return((sym_p) newcore(sizeof(sym_t)));
+}
+
+argb_p newargb() {
+
+       return((argb_p) newcore(sizeof(argb_t)));
+}
+
+#ifndef USEMALLOC
+
+/******************************************************************/
+/******   Start of raw core management package    *****************/
+/******************************************************************/
+
+#define MAXSHORT 30    /* Maximum number of shorts one can ask for */
+
+short *freelist[MAXSHORT];
+
+typedef struct coreblock {
+       struct coreblock *co_next;
+       short co_size;
+} core_t,*core_p;
+
+#define SINC   (sizeof(core_t)/sizeof(short))
+#ifdef COREDEBUG
+coreverbose() {
+       register size;
+       register short *p;
+       register sum;
+
+       sum = 0;
+       for(size=1;size<MAXSHORT;size++)
+               for (p=freelist[size];p!=0;p = *(short **) p)
+                       sum += size;
+       fprintf(stderr,"Used core %u\n",(shortsasked-sum)*sizeof(short));
+}
+#endif
+
+#ifdef SEPID
+
+compactcore() {
+       register core_p corelist=0,tp,cl;
+       int size;
+
+#ifdef COREDEBUG
+       fprintf(stderr,"Almost out of core\n");
+#endif
+       for(size=SINC;size<MAXSHORT;size++) {
+               while ((tp = (core_p) freelist[size]) != (core_p) 0) {
+                       freelist[size] = (short *) tp->co_next;
+                       tp->co_size = size;
+                       if (corelist==0 || tp<corelist) {
+                               tp->co_next = corelist;
+                               corelist = tp;
+                       } else {
+                               for(cl=corelist;cl->co_next != 0 && tp>cl->co_next;
+                                                       cl = cl->co_next)
+                                       ;
+                               tp->co_next = cl->co_next;
+                               cl->co_next = tp;
+                       }
+               }
+       }
+       while (corelist != 0) {
+               while ((short *) corelist->co_next ==
+                   (short *) corelist + corelist->co_size) {
+                       corelist->co_size += corelist->co_next->co_size;
+                       corelist->co_next =  corelist->co_next->co_next;
+               }
+               assert(corelist->co_next==0 ||
+                       (short *) corelist->co_next >
+                           (short *) corelist + corelist->co_size);
+               while (corelist->co_size >= MAXSHORT+SINC) {
+                       oldcore((short *) corelist + corelist->co_size-(MAXSHORT-1),
+                               sizeof(short)*(MAXSHORT-1));
+                       corelist->co_size -= MAXSHORT;
+               }
+               if (corelist->co_size >= MAXSHORT) {
+                       oldcore((short *) corelist + corelist->co_size-SINC,
+                               sizeof(short)*SINC);
+                       corelist->co_size -= SINC;
+               }
+               cl = corelist->co_next;
+               oldcore((short *) corelist, sizeof(short)*corelist->co_size);
+               corelist = cl;
+       }
+}
+
+short *grabcore(size) int size; {
+       register short *p;
+       register trysize;
+
+       /*
+        * Desperate situation, can't get more core from system.
+        * Postpone giving up just a little bit by splitting up
+        * larger free blocks if possible.
+        * Algorithm is worst fit.
+        */
+
+       assert(size<2*MAXSHORT);
+       for(trysize=2*MAXSHORT-2; trysize>size; trysize -= 2) {
+               p = freelist[trysize/sizeof(short)];
+               if ( p != (short *) 0) {
+                       freelist[trysize/sizeof(short)] = *(short **) p;
+                       oldcore(p+size/sizeof(short),trysize-size);
+                       return(p);
+               }
+       }
+
+       /*
+        * Can't get more core from the biggies, try to combine the
+        * little ones. This is expensive but probably better than
+        * giving up.
+        */
+
+       compactcore();
+       if ((p=freelist[size/sizeof(short)]) != 0) {
+               freelist[size/sizeof(short)] = * (short **) p;
+               return(p);
+       }
+       for(trysize=2*MAXSHORT-2; trysize>size; trysize -= 2) {
+               p = freelist[trysize/sizeof(short)];
+               if ( p != (short *) 0) {
+                       freelist[trysize/sizeof(short)] = *(short **) p;
+                       oldcore(p+size/sizeof(short),trysize-size);
+                       return(p);
+               }
+       }
+
+       /*
+        * That's it then. Finished.
+        */
+
+       return(0);
+}
+#endif /* SEPID */
+
+short *newcore(size) int size; {
+       register short *p,*q;
+
+       if( size < 2*MAXSHORT ) {
+               if ((p=freelist[size/sizeof(short)]) != (short *) 0)
+                       freelist[size/sizeof(short)] = *(short **) p;
+               else {
+                       p = freshcore(size);
+#ifdef SEPID
+                       if (p == (short *) 0)
+                               p = grabcore(size);
+#endif
+               }
+       } else
+               p = freshcore(size);
+       if (p == 0)
+               error("out of memory");
+       for (q=p; size > 0 ; size -= sizeof(short))
+               *q++ = 0;
+       return(p);
+}
+
+#ifdef NOMALLOC
+
+/*
+ * stdio uses malloc and free.
+ * you can use these as substitutes
+ */
+
+char *malloc(size) int size; {
+
+       /*
+        * malloc(III) is called by stdio,
+        * this routine is a substitute.
+        */
+
+       return( (char *) newcore(size));
+}
+
+free() {
+
+}
+#endif
+
+oldcore(p,size) short *p; int size; {
+#ifdef CORECHECK
+       register short *cp;
+#endif
+
+       assert(size<2*MAXSHORT);
+#ifdef CORECHECK
+       for (cp=freelist[size/sizeof(short)]; cp != (short *) 0;
+           cp = (short *) *cp)
+               assert(cp != p);
+#endif
+       *(short **) p = freelist[size/sizeof(short)];
+       freelist[size/sizeof(short)] = p;
+}
+
+short *ccur,*cend;
+
+coreinit(p1,p2) short *p1,*p2; {
+
+       /*
+        * coreinit is called with the boundaries of a piece of
+        * memory that can be used for starters.
+        */
+
+       ccur = p1;
+       cend = p2;
+}
+
+short *freshcore(size) int size; {
+       register short *temp;
+       static int cchunk=CCHUNK;
+       
+       while(&ccur[size/sizeof(short)] >= cend && cchunk>0) {
+               do {
+                       temp = (short *) sbrk(cchunk*sizeof(short));
+                       if (temp == (short *) -1)
+                               cchunk >>= 1;
+                       else if (temp != cend)
+                               ccur = cend = temp;
+               } while (temp == (short *) -1 && cchunk>0);
+               cend += cchunk;
+#ifdef COREDEBUG
+               shortsasked += cchunk;
+#endif
+       }
+       if (cchunk==0)
+               return(0);
+       temp = ccur;
+       ccur = &ccur[size/sizeof(short)];
+       return(temp);
+}
+
+#else  /* USEMALLOC */
+
+coreinit() {
+
+       /*
+        * Empty function, no initialization needed
+        */
+}
+
+short *myalloc(size) register size; {
+       register short *p,*q;
+       extern char *malloc();
+
+       p = (short *)malloc(size);
+       if (p == 0)
+               error("out of memory");
+       for(q=p;size>0;size -= sizeof(short))
+               *q++ = 0;
+       return(p);
+}
+#endif
diff --git a/util/opt/alloc.h b/util/opt/alloc.h
new file mode 100644 (file)
index 0000000..edfdaea
--- /dev/null
@@ -0,0 +1,53 @@
+extern line_p  newline();
+extern offset  *newrom();
+extern sym_p   newsym();
+extern num_p   newnum();
+extern arg_p   newarg();
+extern argb_p  newargb();
+extern reg_p   newreg();
+
+extern         oldline();
+extern         oldloc();
+extern         oldreg();
+
+/* #define USEMALLOC   /* if defined malloc() and free() are used */
+
+/* #define COREDEBUG   /* keep records and print statistics */
+
+/*
+ * The next define gives if defined the number of pseudo's outside
+ * procedures that are collected without processing.
+ * If undefined all pseudo's will be collected but that may
+ * give trouble on small machines, because of lack of room.
+ */
+#define PSEUBETWEEN 200 
+
+#ifndef USEMALLOC
+/*
+ * Now the real bitsqueezing starts.
+ * When running on a machine where code and data live in
+ * separate address-spaces it is worth putting in some extra
+ * code to save on probably less data.
+ */
+#define SEPID          /* code and data in separate spaces */
+/*
+ * If the stack segment and the data are separate as on a PDP11 under UNIX
+ * it is worth squeezing some shorts out of the stack page.
+ */
+#ifndef EM_WSIZE
+/*
+ * Compiled with 'standard' C compiler
+ */
+#define STACKROOM 3200 /* number of shorts space in stack */
+#else
+/*
+ * Compiled with pcc, has trouble with lots of variables
+ */
+#define STACKROOM 2000
+#endif
+
+#else
+
+#define STACKROOM 1    /* 0 gives problems */
+
+#endif /* USEMALLOC */
diff --git a/util/opt/assert.h b/util/opt/assert.h
new file mode 100644 (file)
index 0000000..7617f9b
--- /dev/null
@@ -0,0 +1,5 @@
+#ifndef NDEBUG
+#define assert(x) if(!(x)) badassertion(__FILE__,__LINE__)
+#else
+#define assert(x)      /* nothing */
+#endif
diff --git a/util/opt/backward.c b/util/opt/backward.c
new file mode 100644 (file)
index 0000000..1fef284
--- /dev/null
@@ -0,0 +1,183 @@
+#include "param.h"
+#include "types.h"
+#include "assert.h"
+#include "line.h"
+#include "lookup.h"
+#include "alloc.h"
+#include "proinf.h"
+#include "../../h/em_spec.h"
+#include "../../h/em_pseu.h"
+#include "../../h/em_mnem.h"
+#include "ext.h"
+
+/*
+ * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ *
+ *          This product is part of the Amsterdam Compiler Kit.
+ *
+ * Permission to use, sell, duplicate or disclose this software must be
+ * obtained in writing. Requests for such permissions may be sent to
+ *
+ *      Dr. Andrew S. Tanenbaum
+ *      Wiskundig Seminarium
+ *      Vrije Universiteit
+ *      Postbox 7161
+ *      1007 MC Amsterdam
+ *      The Netherlands
+ *
+ * Author: Hans van Staveren
+ */
+
+#define local(x)       if (((x)->s_flags&SYMKNOWN) == 0)\
+                               x->s_flags &= ~ SYMGLOBAL
+#define global(x)      if(((x)->s_flags&SYMKNOWN) == 0)\
+                               x->s_flags |= SYMGLOBAL
+
+#define DTYPHOL        1
+#define DTYPBSS 2
+#define DTYPCON 3
+#define DTYPROM 4
+byte   curdtyp;
+bool   goodrom;
+short  curfrag = 3;    /* see also peephole.c */
+offset rombuf[MAXROM];
+int    rc;
+
+backward() {
+       register line_p lnp;
+       line_p  next;
+       register arg_p ap;
+       line_p i,p;
+       int n;
+       register sym_p sp;
+
+       i = p = (line_p) 0;
+       curdtyp=0;
+       for (lnp = curpro.lastline; lnp != (line_p) 0; lnp = next) {
+               next = lnp->l_next;
+               switch(lnp->l_optyp) {
+               case OPSYMBOL:
+                       global(lnp->l_a.la_sp);
+                       break;
+               case OPSVAL:
+                       global(lnp->l_a.la_sval.lasv_sp);
+                       break;
+               case OPLVAL:
+                       global(lnp->l_a.la_lval.lalv_sp);
+                       break;
+               case OPLIST:
+                       ap = lnp->l_a.la_arg;
+                       while (ap != (arg_p) 0 ) {
+                               switch(ap->a_typ) {
+                               case ARGSYM:
+                                       global(ap->a_a.a_sp);
+                                       break;
+                               case ARGVAL:
+                                       global(ap->a_a.a_val.av_sp);
+                               }
+                               ap = ap->a_next;
+                       }
+                       break;
+               }
+
+               /*
+                * references to symbols are processed now.
+                * for plain instructions nothing else is needed
+                */
+
+               switch(lnp->l_instr&BMASK) {
+               /*
+                * count all local occurences for register counts;
+                * op_lal is omitted and not by accident.
+                */
+               case op_del:
+               case op_inl:
+               case op_ldl:
+               case op_lil:
+               case op_lol:
+               case op_sdl:
+               case op_sil:
+               case op_stl:
+               case op_zrl:
+                       switch(lnp->l_optyp) {
+                       case OPNO:
+                       case OPNUMLAB:
+                       case OPSYMBOL:
+                       case OPSVAL:
+                       case OPLVAL:
+                       case OPLIST:
+                               break;
+                       case OPOFFSET:
+                               incregusage(lnp->l_a.la_offset);
+                               break;
+                       case OPSHORT:
+                               incregusage((offset)lnp->l_a.la_short);
+                               break;
+                       default:
+                               incregusage((offset)(lnp->l_optyp&BMASK)-Z_OPMINI);
+                               break;
+                       }
+                       /* fall through !! */
+               default:
+                       assert((lnp->l_instr&BMASK)<=op_last);
+                       lnp->l_next = i;
+                       i = lnp;
+                       continue;
+               case ps_sym:
+                       sp = lnp->l_a.la_sp;
+                       local(sp);
+                       if (curdtyp == DTYPROM && goodrom) {
+                               sp->s_rom = newrom();
+                               for (n=0;n<rc;n++)
+                                       sp->s_rom[n] = rombuf[n];
+                       }
+                       sp->s_frag = curfrag;
+                       break;
+               case ps_hol:
+                       curdtyp = DTYPHOL;
+                       curfrag++;
+                       break;
+               case ps_bss:
+                       curdtyp = DTYPBSS;
+                       curfrag++;
+                       break;
+               case ps_con:
+                       if (curdtyp != DTYPCON) {
+                               curdtyp = DTYPCON;
+                               curfrag++;
+                       }
+                       break;
+               case ps_rom:
+                       if (curdtyp != DTYPROM) {
+                               curdtyp = DTYPROM;
+                               curfrag++;
+                       }
+                       ap = lnp->l_a.la_arg;
+                       rc = 0;
+                       while (ap != (arg_p) 0 && rc < MAXROM) {
+                               if (ap->a_typ == ARGOFF) {
+                                       rombuf[rc++] = ap->a_a.a_offset;
+                                       ap = ap->a_next;
+                               } else
+                                       ap = (arg_p) 0;
+                       }
+                       goodrom = (rc >= 2);
+                       break;
+               case ps_mes:
+                       break;
+               case ps_inp:
+               case ps_ina:
+                       local(lnp->l_a.la_sp);
+               case ps_exp:
+               case ps_exa:
+               case ps_exc:
+                       oldline(lnp);
+                       continue;
+               }
+               lnp->l_next = p;
+               p = lnp;
+       }
+       if (prodepth != 0)
+               local(curpro.symbol);
+       instrs = i; pseudos = p; curpro.lastline = (line_p) 0;
+}
diff --git a/util/opt/cleanup.c b/util/opt/cleanup.c
new file mode 100644 (file)
index 0000000..e0505e2
--- /dev/null
@@ -0,0 +1,61 @@
+#include <stdio.h>
+#include "param.h"
+#include "types.h"
+#include "assert.h"
+#include "../../h/em_pseu.h"
+#include "../../h/em_spec.h"
+#include "../../h/em_mes.h"
+#include "lookup.h"
+#include "ext.h"
+
+/*
+ * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ *
+ *          This product is part of the Amsterdam Compiler Kit.
+ *
+ * Permission to use, sell, duplicate or disclose this software must be
+ * obtained in writing. Requests for such permissions may be sent to
+ *
+ *      Dr. Andrew S. Tanenbaum
+ *      Wiskundig Seminarium
+ *      Vrije Universiteit
+ *      Postbox 7161
+ *      1007 MC Amsterdam
+ *      The Netherlands
+ *
+ * Author: Hans van Staveren
+ */
+
+
+cleanup() {
+       FILE *infile;
+       register c;
+       register sym_p *spp,sp;
+
+       for (spp=symhash;spp< &symhash[NSYMHASH];spp++)
+               for (sp = *spp; sp != (sym_p) 0; sp = sp->s_next)
+                       if ((sp->s_flags & SYMOUT) == 0)
+                               outdef(sp);
+       if(!Lflag)
+               return;
+       c=fclose(outfile);
+       assert(c != EOF);
+       outfile = stdout;
+       infile = fopen(template,"r");
+       if (infile == NULL)
+               error("temp file disappeared");
+       outshort(sp_magic);
+       outinst(ps_mes);
+       outint(ms_ext);
+       for (spp=symhash;spp< &symhash[NSYMHASH];spp++)
+               for (sp = *spp; sp != (sym_p) 0; sp = sp->s_next)
+                       if ((sp->s_flags&(SYMDEF|SYMGLOBAL)) == (SYMDEF|SYMGLOBAL))
+                               outsym(sp);
+       putc(sp_cend,outfile);
+       while ( (c=getc(infile)) != EOF)
+               putc(c,outfile);
+       c=fclose(infile);
+       assert(c != EOF);
+       c=unlink(template);
+       assert(c == 0);
+}
diff --git a/util/opt/ext.h b/util/opt/ext.h
new file mode 100644 (file)
index 0000000..10d6f0d
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef FILE
+#include <stdio.h>
+#endif
+extern unsigned linecount;
+extern int     prodepth;
+extern bool    Lflag;
+extern bool    nflag;
+extern byte    em_flag[];
+extern line_p  instrs,pseudos;
+extern FILE    *outfile;
+extern char    template[];
+extern offset  wordsize;
+extern offset  pointersize;
+extern char    *progname;
diff --git a/util/opt/flow.c b/util/opt/flow.c
new file mode 100644 (file)
index 0000000..3df6c4e
--- /dev/null
@@ -0,0 +1,122 @@
+#include "param.h"
+#include "types.h"
+#include "../../h/em_flag.h"
+#include "../../h/em_spec.h"
+#include "../../h/em_mnem.h"
+#include "alloc.h"
+#include "line.h"
+#include "proinf.h"
+#include "optim.h"
+#include "ext.h"
+
+/*
+ * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ *
+ *          This product is part of the Amsterdam Compiler Kit.
+ *
+ * Permission to use, sell, duplicate or disclose this software must be
+ * obtained in writing. Requests for such permissions may be sent to
+ *
+ *      Dr. Andrew S. Tanenbaum
+ *      Wiskundig Seminarium
+ *      Vrije Universiteit
+ *      Postbox 7161
+ *      1007 MC Amsterdam
+ *      The Netherlands
+ *
+ * Author: Hans van Staveren
+ */
+
+flow() {
+
+       findreach();    /* determine reachable labels */
+       cleaninstrs();  /* throw away unreachable code */
+}
+
+findreach() {
+       register num_p  *npp,np;
+
+       reach(instrs);
+       for(npp=curpro.numhash;npp< &curpro.numhash[NNUMHASH]; npp++)
+               for(np= *npp; np != (num_p) 0 ; np = np->n_next)
+                       if (np->n_flags&NUMDATA) {
+                               np->n_repl->n_flags |= NUMREACH;
+                               np->n_repl->n_jumps++;
+                               if (!(np->n_flags&NUMSCAN)) {
+                                       np->n_flags |= NUMSCAN;
+                                       reach(np->n_line->l_next);
+                               }
+                       }
+}
+
+reach(lnp) register line_p lnp; {
+       register num_p np;
+
+       for (;lnp != (line_p) 0; lnp = lnp->l_next) {
+               if(lnp->l_optyp == OPNUMLAB) {
+                       /*
+                        * Branch instruction or label
+                        */
+                       np = lnp->l_a.la_np;
+                       if ((lnp->l_instr&BMASK) != op_lab)
+                               np = np->n_repl;
+                       np->n_flags |= NUMREACH;
+                       if (!(np->n_flags&NUMSCAN)) {
+                               np->n_flags |= NUMSCAN;
+                               reach(np->n_line->l_next);
+                       }
+                       if ((lnp->l_instr&BMASK) == op_lab)
+                               return;
+                       else
+                               np->n_jumps++;
+               }
+               if ((em_flag[(lnp->l_instr&BMASK)-sp_fmnem]&EM_FLO)==FLO_T)
+                       return;
+       }
+}
+
+cleaninstrs() {
+       register line_p *lpp,lp,*lastbra;
+       bool reachable,superfluous;
+       int instr;
+
+       lpp = &instrs; lastbra = (line_p *) 0; reachable = TRUE;
+       while ((lp = *lpp) != (line_p) 0) {
+               instr = lp->l_instr&BMASK;
+               if (instr == op_lab) {
+                       if ((lp->l_a.la_np->n_flags&NUMREACH) != 0) {
+                               reachable = TRUE;
+                               if (lastbra != (line_p *) 0
+                                   && (*lastbra)->l_next == lp
+                                   && (*lastbra)->l_a.la_np->n_repl==lp->l_a.la_np) {
+                                       oldline(*lastbra);
+                                       OPTIM(O_BRALAB);
+                                       lpp = lastbra;
+                                       *lpp = lp;
+                                       lp->l_a.la_np->n_jumps--;
+                               }
+                       }
+                       if ( lp->l_a.la_np->n_repl != lp->l_a.la_np ||
+                            ((lp->l_a.la_np->n_flags&NUMDATA)==0 &&
+                             lp->l_a.la_np->n_jumps == 0))
+                               superfluous = TRUE;
+                       else
+                               superfluous = FALSE;
+               } else
+                       superfluous = FALSE;
+               if ( (!reachable) || superfluous) {
+                       lp = lp->l_next;
+                       oldline(*lpp);
+                       OPTIM(O_UNREACH);
+                       *lpp = lp;
+               } else {
+                       if ( instr <= sp_lmnem &&
+                           (em_flag[instr-sp_fmnem]&EM_FLO)==FLO_T) {
+                               reachable = FALSE;
+                               if ((lp->l_instr&BMASK) == op_bra)
+                                       lastbra = lpp;
+                       }
+                       lpp = &lp->l_next;
+               }
+       }
+}
diff --git a/util/opt/getline.c b/util/opt/getline.c
new file mode 100644 (file)
index 0000000..49b0809
--- /dev/null
@@ -0,0 +1,549 @@
+#include <stdio.h>
+#include "param.h"
+#include "types.h"
+#include "assert.h"
+#include "line.h"
+#include "lookup.h"
+#include "alloc.h"
+#include "proinf.h"
+#include "../../h/em_spec.h"
+#include "../../h/em_pseu.h"
+#include "../../h/em_flag.h"
+#include "../../h/em_mes.h"
+#include "ext.h"
+
+/*
+ * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ *
+ *          This product is part of the Amsterdam Compiler Kit.
+ *
+ * Permission to use, sell, duplicate or disclose this software must be
+ * obtained in writing. Requests for such permissions may be sent to
+ *
+ *      Dr. Andrew S. Tanenbaum
+ *      Wiskundig Seminarium
+ *      Vrije Universiteit
+ *      Postbox 7161
+ *      1007 MC Amsterdam
+ *      The Netherlands
+ *
+ * Author: Hans van Staveren
+ */
+
+
+static  short   tabval;         /* temp store for shorts */
+static  offset  tabval2;        /* temp store for offsets */
+static  char    string[IDL+1];  /* temp store for names */
+
+/*
+ * The next constants are close to sp_cend for fast switches
+ */
+#define INST    256     /* instruction:         number in tabval */
+#define PSEU    257     /* pseudo:              number in tabval */
+#define ILBX    258     /* label:               number in tabval */
+#define DLBX    259     /* symbol:              name in string[] */
+#define CSTX1   260     /* short constant:      stored in tabval */
+#define CSTX2   261     /* offset:              value in tabval2 */
+#define VALX1   262     /* symbol+short:        in string[] and tabval */
+#define VALX2   263     /* symbol+offset:       in string[] and tabval2 */
+#define ATEOF   264     /* bumped into end of file */
+
+#define readbyte getchar
+
+short readshort() {
+       register int l_byte, h_byte;
+
+       l_byte = readbyte();
+       h_byte = readbyte();
+       if ( h_byte>=128 ) h_byte -= 256 ;
+       return l_byte | (h_byte*256) ;
+}
+
+#ifdef LONGOFF
+offset readoffset() {
+       register long l;
+       register int h_byte;
+
+       l = readbyte();
+       l |= ((unsigned) readbyte())*256 ;
+       l |= readbyte()*256L*256L ;
+       h_byte = readbyte() ;
+       if ( h_byte>=128 ) h_byte -= 256 ;
+       return l | (h_byte*256L*256*256L) ;
+}
+#endif
+
+draininput() {
+
+       /*
+        * called when MES ERR is encountered.
+        * Drain input in case it is a pipe.
+        */
+
+       while (getchar() != EOF)
+               ;
+}
+
+short getint() {
+
+       switch(table2()) {
+       default: error("int expected");
+       case CSTX1:
+               return(tabval);
+       }
+}
+
+sym_p getsym(status) int status; {
+
+       switch(table2()) {
+       default:
+               error("symbol expected");
+       case DLBX:
+               return(symlookup(string,status,0));
+       case sp_pnam:
+               return(symlookup(string,status,SYMPRO));
+       }
+}
+
+offset getoff() {
+
+       switch (table2()) {
+       default: error("offset expected");
+       case CSTX1:
+               return((offset) tabval);
+#ifdef LONGOFF
+       case CSTX2:
+               return(tabval2);
+#endif
+       }
+}
+
+make_string(n) int n; {
+       register char *s;
+       extern char *sprintf();
+
+       s=sprintf(string,".%u",n);
+       assert(s == string);
+}
+
+inident() {
+       register n;
+       register char *p = string;
+       register c;
+
+       n = getint();
+       while (n--) {
+               c = readbyte();
+               if (p<&string[IDL])
+                       *p++ = c;
+       }
+       *p++ = 0;
+}
+
+int table3(n) int n; {
+
+       switch (n) {
+       case sp_ilb1:   tabval = readbyte(); return(ILBX);
+       case sp_ilb2:   tabval = readshort(); return(ILBX);
+       case sp_dlb1:   make_string(readbyte()); return(DLBX);
+       case sp_dlb2:   make_string(readshort()); return(DLBX);
+       case sp_dnam:   inident(); return(DLBX);
+       case sp_pnam:   inident(); return(n);
+       case sp_cst2:   tabval = readshort(); return(CSTX1);
+#ifdef LONGOFF
+       case sp_cst4:   tabval2 = readoffset(); return(CSTX2);
+#endif
+       case sp_doff:   if (table2()!=DLBX) error("symbol expected");
+                       switch(table2()) {
+                       default:        error("offset expected");
+                       case CSTX1:             return(VALX1);
+#ifdef LONGOFF
+                       case CSTX2:             return(VALX2);
+#endif
+                       }
+       default:        return(n);
+       }
+}
+
+int table1() {
+       register n;
+
+       n = readbyte();
+       if (n == EOF)
+               return(ATEOF);
+       if ((n <= sp_lmnem) && (n >= sp_fmnem)) {
+               tabval = n;
+               return(INST);
+       }
+       if ((n <= sp_lpseu) && (n >= sp_fpseu)) {
+               tabval = n;
+               return(PSEU);
+       }
+       if ((n < sp_filb0 + sp_nilb0) && (n >= sp_filb0)) {
+               tabval = n - sp_filb0;
+               return(ILBX);
+       }
+       return(table3(n));
+}
+
+int table2() {
+       register n;
+
+       n = readbyte();
+       if ((n < sp_fcst0 + sp_ncst0) && (n >= sp_fcst0)) {
+               tabval = n - sp_zcst0;
+               return(CSTX1);
+       }
+       return(table3(n));
+}
+
+getlines() {
+       register line_p lnp;
+       register instr;
+
+    for(;;) {
+       linecount++;
+       switch(table1()) {
+       default:
+               error("unknown instruction byte");
+               /* NOTREACHED */
+
+       case ATEOF:
+               if (prodepth!=0)
+                       error("procedure unterminated at eof");
+               process();
+               return;
+       case INST:
+               tstinpro();
+               instr = tabval;
+               break;
+       case DLBX:
+               lnp = newline(OPSYMBOL);
+               lnp->l_instr = ps_sym;
+               lnp->l_a.la_sp= symlookup(string,DEFINING,0);
+               lnp->l_next = curpro.lastline;
+               curpro.lastline = lnp;
+               continue;
+       case ILBX:
+               tstinpro();
+               lnp = newline(OPNUMLAB);
+               lnp->l_instr = op_lab;
+               lnp->l_a.la_np = numlookup((unsigned) tabval);
+               if (lnp->l_a.la_np->n_line != (line_p) 0)
+                       error("label %u multiple defined",(unsigned) tabval);
+               lnp->l_a.la_np->n_line = lnp;
+               lnp->l_next = curpro.lastline;
+               curpro.lastline = lnp;
+               continue;
+       case PSEU:
+               if(inpseudo(tabval))
+                       return;
+               continue;
+       }
+
+       /*
+        * Now we have an instruction number in instr
+        * There might be an operand, look for it
+        */
+
+       if ((em_flag[instr-sp_fmnem]&EM_PAR)==PAR_NO) {
+               lnp = newline(OPNO);
+       } else switch(table2()) {
+       default:
+               error("unknown offset byte");
+       case sp_cend:
+               lnp = newline(OPNO);
+               break;
+       case CSTX1:
+               if ((em_flag[instr-sp_fmnem]&EM_PAR)!= PAR_B) {
+                       if (CANMINI(tabval))
+                               lnp = newline(tabval+Z_OPMINI);
+                       else {
+                               lnp = newline(OPSHORT);
+                               lnp->l_a.la_short = tabval;
+                       }
+               } else {
+                       lnp = newline(OPNUMLAB);
+                       lnp->l_a.la_np = numlookup((unsigned) tabval);
+               }
+               break;
+#ifdef LONGOFF
+       case CSTX2:
+               lnp = newline(OPOFFSET);
+               lnp->l_a.la_offset = tabval2;
+               break;
+#endif
+       case ILBX:
+               tstinpro();
+               lnp = newline(OPNUMLAB);
+               lnp->l_a.la_np = numlookup((unsigned) tabval);
+               break;
+       case DLBX:
+               lnp = newline(OPSYMBOL);
+               lnp->l_a.la_sp = symlookup(string,OCCURRING,0);
+               break;
+       case sp_pnam:
+               lnp = newline(OPSYMBOL);
+               lnp->l_a.la_sp = symlookup(string,OCCURRING,SYMPRO);
+               break;
+       case VALX1:
+               lnp = newline(OPSVAL);
+               lnp->l_a.la_sval.lasv_sp = symlookup(string,OCCURRING,0);
+               lnp->l_a.la_sval.lasv_short = tabval;
+               break;
+#ifdef LONGOFF
+       case VALX2:
+               lnp = newline(OPLVAL);
+               lnp->l_a.la_lval.lalv_sp = symlookup(string,OCCURRING,0);
+               lnp->l_a.la_lval.lalv_offset = tabval2;
+               break;
+#endif
+       }
+       lnp->l_instr = instr;
+       lnp->l_next = curpro.lastline;
+       curpro.lastline = lnp;
+    }
+}
+
+argstring(length,abp) offset length; register argb_p abp; {
+
+       while (length--) {
+               if (abp->ab_index == NARGBYTES)
+                       abp = abp->ab_next = newargb();
+               abp->ab_contents[abp->ab_index++] = readbyte();
+       }
+}
+
+line_p  arglist(n) int n; {
+       line_p  lnp;
+       register arg_p ap,*app;
+       bool moretocome;
+       offset length;
+
+
+       /*
+        * creates an arglist with n elements
+        * if n == 0 the arglist is variable and terminated by sp_cend
+        */
+
+       lnp = newline(OPLIST);
+       app = &lnp->l_a.la_arg;
+       moretocome = TRUE;
+       do {
+               switch(table2()) {
+               default:
+                       error("unknown byte in arglist");
+               case CSTX1:
+                       tabval2 = (offset) tabval;
+               case CSTX2:
+                       *app = ap = newarg(ARGOFF);
+                       ap->a_a.a_offset = tabval2;
+                       app = &ap->a_next;
+                       break;
+               case ILBX:
+                       tstinpro();
+                       *app = ap = newarg(ARGNUM);
+                       ap->a_a.a_np = numlookup((unsigned) tabval);
+                       ap->a_a.a_np->n_flags |= NUMDATA;
+                       app = &ap->a_next;
+                       break;
+               case DLBX:
+                       *app = ap = newarg(ARGSYM);
+                       ap->a_a.a_sp = symlookup(string,OCCURRING,0);
+                       app = &ap->a_next;
+                       break;
+               case sp_pnam:
+                       *app = ap = newarg(ARGSYM);
+                       ap->a_a.a_sp = symlookup(string,OCCURRING,SYMPRO);
+                       app = &ap->a_next;
+                       break;
+               case VALX1:
+                       tabval2 = (offset) tabval;
+               case VALX2:
+                       *app = ap = newarg(ARGVAL);
+                       ap->a_a.a_val.av_sp = symlookup(string,OCCURRING,0);
+                       ap->a_a.a_val.av_offset = tabval2;
+                       app = &ap->a_next;
+                       break;
+               case sp_scon:
+                       *app = ap = newarg(ARGSTR);
+                       length = getoff();
+                       argstring(length,&ap->a_a.a_string);
+                       app = &ap->a_next;
+                       break;
+               case sp_icon:
+                       *app = ap = newarg(ARGICN);
+                       goto casecon;
+               case sp_ucon:
+                       *app = ap = newarg(ARGUCN);
+                       goto casecon;
+               case sp_fcon:
+                       *app = ap = newarg(ARGFCN);
+               casecon:
+                       length = getint();
+                       ap->a_a.a_con.ac_length = (short) length;
+                       argstring(getoff(),&ap->a_a.a_con.ac_con);
+                       app = &ap->a_next;
+                       break;
+               case sp_cend:
+                       moretocome = FALSE;
+               }
+               if (n && (--n) == 0)
+                       moretocome = FALSE;
+       } while (moretocome);
+       return(lnp);
+}
+
+offset aoff(ap,n) register arg_p ap; {
+
+       while (n>0) {
+               if (ap != (arg_p) 0)
+                       ap = ap->a_next;
+               n--;
+       }
+       if (ap == (arg_p) 0)
+               error("too few parameters");
+       if (ap->a_typ != ARGOFF)
+               error("offset expected");
+       return(ap->a_a.a_offset);
+}
+
+int inpseudo(n) short n; {
+       register line_p lnp,head,tail;
+       short           n1,n2;
+       proinf savearea;
+#ifdef PSEUBETWEEN
+       static int pcount=0;
+
+       if (pcount++ >= PSEUBETWEEN && prodepth==0) {
+               process();
+               pcount=0;
+       }
+#endif
+
+       switch(n) {
+       default:
+               error("unknown pseudo");
+       case ps_bss:
+       case ps_hol:
+               lnp = arglist(3);
+               break;
+       case ps_rom:
+       case ps_con:
+               lnp = arglist(0);
+               break;
+       case ps_ina:
+       case ps_inp:
+       case ps_exa:
+       case ps_exp:
+               lnp = newline(OPSYMBOL);
+               lnp->l_a.la_sp = getsym(NOTHING);
+               break;
+       case ps_exc:
+               n1 = getint(); n2 = getint();
+               if (n1 != 0 && n2 != 0) {
+                       tail = curpro.lastline;
+                       while (--n2) tail = tail->l_next;
+                       head = tail;
+                       while (n1--) head = head->l_next;
+                       lnp = tail->l_next;
+                       tail->l_next = head->l_next;
+                       head->l_next = curpro.lastline;
+                       curpro.lastline = lnp;
+               }
+               lnp = newline(OPNO);
+               break;
+       case ps_mes:
+               lnp = arglist(0);
+               switch((int) aoff(lnp->l_a.la_arg,0)) {
+               case ms_err:
+                       draininput(); exit(-1);
+               case ms_opt:
+                       nflag = TRUE; break;
+               case ms_emx:
+                       wordsize = aoff(lnp->l_a.la_arg,1);
+                       pointersize = aoff(lnp->l_a.la_arg,2);
+#ifndef LONGOFF
+                       if (wordsize>2)
+                               error("This optimizer cannot handle wordsize>2");
+#endif
+                       break;
+               case ms_reg:
+                       if (prodepth==0)
+                               error("MES REG outside procedure");
+                       regvar(lnp->l_a.la_arg->a_next);
+                       oldline(lnp);
+                       lnp=newline(OPNO);
+                       n=ps_exc;       /* kludge to force out this line */
+                       break;
+               }
+               break;
+       case ps_pro:
+               if (prodepth>0)
+                       savearea = curpro;
+               else
+                       process();
+               curpro.symbol = getsym(DEFINING);
+               switch(table2()) {
+               case sp_cend:
+                       curpro.localbytes = (offset) -1;
+                       break;
+               case CSTX1:
+                       tabval2 = (offset) tabval;
+               case CSTX2:
+                       curpro.localbytes = tabval2;
+                       break;
+               default:
+                       error("bad second arg of PRO");
+               }
+               prodepth++;
+               if (prodepth>1) {
+                       register i;
+
+                       curpro.lastline = (line_p) 0;
+                       curpro.freg = (reg_p) 0;
+                       for(i=0;i<NNUMHASH;i++)
+                               curpro.numhash[i] = (num_p) 0;
+                       getlines();
+                       curpro = savearea;
+                       prodepth--;
+               }
+               return(0);
+       case ps_end:
+               if (prodepth==0)
+                       error("END misplaced");
+               switch(table2()) {
+               case sp_cend:
+                       if (curpro.localbytes == (offset) -1)
+                               error("bytes for locals still unknown");
+                       break;
+               case CSTX1:
+                       tabval2 = (offset) tabval;
+               case CSTX2:
+                       if (curpro.localbytes != (offset) -1 && curpro.localbytes != tabval2)
+                               error("inconsistency in number of bytes for locals");
+                       curpro.localbytes = tabval2;
+                       break;
+               }
+               process();
+               curpro.symbol = (sym_p) 0;
+               if (prodepth==1) {
+                       prodepth=0;
+#ifdef PSEUBETWEEN
+                       pcount=0;
+#endif
+                       return(0);
+               } else
+                       return(1);
+       }
+       lnp->l_instr = n;
+       lnp->l_next = curpro.lastline;
+       curpro.lastline = lnp;
+       return(0);
+}
+
+tstinpro() {
+
+       if (prodepth==0)
+               error("Instruction or label not allowed outside procedure");
+}
diff --git a/util/opt/line.h b/util/opt/line.h
new file mode 100644 (file)
index 0000000..8f20bf3
--- /dev/null
@@ -0,0 +1,86 @@
+#define NARGBYTES      14
+struct argbytes {
+       argb_p  ab_next;
+       short   ab_index;
+       char    ab_contents[NARGBYTES];
+};
+
+typedef struct {
+       sym_p   av_sp;
+       offset  av_offset;
+} s_a_val;
+
+typedef struct {
+       short   ac_length;
+       argb_t  ac_con;
+} s_a_con;
+
+typedef union {
+       offset  a_offset;
+       num_p   a_np;
+       sym_p   a_sp;
+       s_a_val a_val;
+       argb_t  a_string;
+       s_a_con a_con;
+} un_a_a;
+
+struct arg {
+       arg_p   a_next;
+       short   a_typ;
+       un_a_a  a_a;
+};
+
+/* possible values for .a_typ
+ */
+
+#define ARGOFF 0
+#define ARGNUM 1
+#define ARGSYM 2
+#define ARGVAL 3
+#define ARGSTR 4
+#define ARGICN 5
+#define ARGUCN 6
+#define ARGFCN 7
+
+typedef struct {
+       sym_p   lasv_sp;
+       short   lasv_short;
+} s_la_sval;
+
+typedef struct {
+       sym_p   lalv_sp;
+       offset  lalv_offset;
+} s_la_lval;
+
+typedef union {
+       short   la_short;
+       offset  la_offset;
+       num_p   la_np;
+       sym_p   la_sp;
+       s_la_sval la_sval;
+       s_la_lval la_lval;
+       arg_p   la_arg;
+} un_l_a;
+
+struct line {
+       line_p          l_next;         /* maintains linked list */
+       byte            l_instr;        /* instruction number */
+       byte            l_optyp;        /* specifies what follows */
+       un_l_a          l_a;
+};
+
+/* Possible values for .l_optyp */
+
+#define OPNO           0       /* no operand */
+#define OPSHORT                1       /* 16 bit number */
+#define OPOFFSET       2       /* 16 or 32 bit number */
+#define OPNUMLAB       3       /* local label for branches */
+#define OPSYMBOL       4       /* global label or procedurename */
+#define OPSVAL         5       /* symbol + 16 bit constant */
+#define OPLVAL         6       /* symbol + 16 or 32 bit constant */
+#define OPLIST         7       /* operand list for some pseudos */
+#define OPMINI         8       /* start of minis */
+
+#define Z_OPMINI       (OPMINI+100)    /* tunable */
+
+#define CANMINI(x) ((x)>=OPMINI-Z_OPMINI && (x)<256-Z_OPMINI)
diff --git a/util/opt/lookup.c b/util/opt/lookup.c
new file mode 100644 (file)
index 0000000..567baa8
--- /dev/null
@@ -0,0 +1,84 @@
+#include "param.h"
+#include "types.h"
+#include "lookup.h"
+#include "alloc.h"
+#include "proinf.h"
+
+/*
+ * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ *
+ *          This product is part of the Amsterdam Compiler Kit.
+ *
+ * Permission to use, sell, duplicate or disclose this software must be
+ * obtained in writing. Requests for such permissions may be sent to
+ *
+ *      Dr. Andrew S. Tanenbaum
+ *      Wiskundig Seminarium
+ *      Vrije Universiteit
+ *      Postbox 7161
+ *      1007 MC Amsterdam
+ *      The Netherlands
+ *
+ * Author: Hans van Staveren
+ */
+
+unsigned hash(string) char *string; {
+       register char *p;
+       register unsigned i,sum;
+
+       for (sum=i=0,p=string;*p;i += 3)
+               sum ^= (*p++)<<(i&07);
+       return(sum);
+}
+
+sym_p symlookup(name,status,flags) char *name; int status,flags; {
+       register sym_p *spp,sp;
+       static short genfrag = 32767;
+
+       spp = &symhash[hash(name)%NSYMHASH];
+       while (*spp != (sym_p) 0)
+               if (strncmp((*spp)->s_name,name,IDL)==0) {
+                       sp = *spp;
+                       if ((sp->s_flags^flags)&SYMPRO)
+                               error("%s is both proc and datalabel",name);
+                       if (status == DEFINING) {
+                               if (sp->s_flags&SYMDEF)
+                                       error("redefined symbol %s",name);
+                               sp->s_flags |= SYMDEF;
+                       }
+                       return(sp);
+               } else
+                       spp = &(*spp)->s_next;
+
+       /*
+        * symbol not found, enter in table
+        */
+
+       *spp = sp = newsym();
+       strncpy(sp->s_name,name,IDL);
+       sp->s_flags = flags;
+       if (status == DEFINING)
+               sp->s_flags |= SYMDEF;
+       sp->s_frag = genfrag--;
+       return(sp);
+}
+
+num_p numlookup(number) unsigned number; {
+       register num_p *npp, np;
+
+       npp = &curpro.numhash[number%NNUMHASH];
+       while (*npp != (num_p) 0)
+               if ((*npp)->n_number == number)
+                       return(*npp);
+               else
+                       npp = &(*npp)->n_next;
+
+       /*
+        * local label not found, enter in tabel
+        */
+
+       *npp = np = newnum();
+       np->n_number = number;
+       np->n_repl = np;
+       return(np);
+}
diff --git a/util/opt/lookup.h b/util/opt/lookup.h
new file mode 100644 (file)
index 0000000..a8dd4ff
--- /dev/null
@@ -0,0 +1,23 @@
+#define IDL    8
+
+struct sym {
+       sym_p   s_next;
+       char    s_name[IDL];
+       offset  *s_rom;
+       short   s_flags;
+       short   s_frag;
+       offset  s_value;
+};
+
+/* contents of .s_flags */
+#define SYMPRO         000001
+#define SYMGLOBAL      000002
+#define SYMKNOWN       000004
+#define SYMOUT         000010
+#define SYMDEF         000020
+
+#define NSYMHASH       127
+extern sym_p symhash[NSYMHASH],symlookup();
+#define OCCURRING      0
+#define DEFINING       1
+#define NOTHING                2
diff --git a/util/opt/main.c b/util/opt/main.c
new file mode 100644 (file)
index 0000000..185bd4e
--- /dev/null
@@ -0,0 +1,73 @@
+#include <stdio.h>
+#include "param.h"
+#include "types.h"
+#include "alloc.h"
+#include "../../h/em_spec.h"
+#include "ext.h"
+
+/*
+ * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ *
+ *          This product is part of the Amsterdam Compiler Kit.
+ *
+ * Permission to use, sell, duplicate or disclose this software must be
+ * obtained in writing. Requests for such permissions may be sent to
+ *
+ *      Dr. Andrew S. Tanenbaum
+ *      Wiskundig Seminarium
+ *      Vrije Universiteit
+ *      Postbox 7161
+ *      1007 MC Amsterdam
+ *      The Netherlands
+ *
+ * Author: Hans van Staveren
+ */
+
+/*
+ * Main program for EM optimizer
+ */
+
+main(argc,argv) int argc; char *argv[]; {
+       short somespace[STACKROOM];
+
+       progname = argv[0];
+       while (argc-->1 && **++argv == '-')
+               flags(*argv);
+       if (argc>1) {
+               fprintf(stderr,"Usage: %s [-Ln] [name]\n",progname);
+               exit(-1);
+       }
+       if (argc)
+               if (freopen(*argv,"r",stdin) == NULL)
+                       error("Cannot open %s",*argv);
+       fileinit();
+       coreinit(somespace,somespace+STACKROOM);
+       getlines();
+       cleanup();
+       return(0);
+}
+
+flags(s) register char *s; {
+
+       for (s++;*s;s++)
+               switch(*s) {
+               case 'L':       Lflag = TRUE; break;
+               case 'n':       nflag = TRUE; break;
+               }
+}
+
+fileinit() {
+       char *mktemp();
+       short readshort();
+
+       if (readshort() != (short) sp_magic)
+               error("wrong input file");
+       if (Lflag) {
+               outfile = fopen(mktemp(template),"w");
+               if (outfile == NULL)
+                       error("can't create %s",template);
+       } else {
+               outfile = stdout;
+               outshort(sp_magic);
+       }
+}
diff --git a/util/opt/makedepend b/util/opt/makedepend
new file mode 100755 (executable)
index 0000000..a1af4cc
--- /dev/null
@@ -0,0 +1,14 @@
+for extension in c y
+do
+    for file in *.$extension
+    do ofile=`basename $file .$extension`.o
+    grep '^# *include.*"' $file|sed "s/.*\"\(.*\)\".*/$ofile:  \1/"
+    done
+done | sort -u >depend
+ed - Makefile <<'!'
+/AUTOAUTOAUTO/+,$d
+$r depend
+w
+q
+!
+rm -f depend
diff --git a/util/opt/mktab.y b/util/opt/mktab.y
new file mode 100644 (file)
index 0000000..a3daa27
--- /dev/null
@@ -0,0 +1,362 @@
+%{
+#include <stdio.h>
+#include "param.h"
+#include "types.h"
+#include "pattern.h"
+#include "../../h/em_spec.h"
+#include "../../h/em_mnem.h"
+#include "optim.h"
+
+/*
+ * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ *
+ *          This product is part of the Amsterdam Compiler Kit.
+ *
+ * Permission to use, sell, duplicate or disclose this software must be
+ * obtained in writing. Requests for such permissions may be sent to
+ *
+ *      Dr. Andrew S. Tanenbaum
+ *      Wiskundig Seminarium
+ *      Vrije Universiteit
+ *      Postbox 7161
+ *      1007 MC Amsterdam
+ *      The Netherlands
+ *
+ * Author: Hans van Staveren
+ */
+
+#define MAXNODES 1000
+expr_t nodes[MAXNODES];
+expr_p lastnode = nodes+1;
+int    curind,prevind;
+int    patlen,maxpatlen,rpllen;
+int    lino = 1;
+int    patno=1;
+#define MAX    100
+int patmnem[MAX],rplmnem[MAX],rplexpr[MAX];
+byte   nparam[N_EX_OPS];
+bool   nonumlab[N_EX_OPS];
+bool   onlyconst[N_EX_OPS];
+int    nerrors=0;
+%}
+
+%union {
+       int     y_int;
+}
+
+%left OR2
+%left AND2
+%left OR1
+%left XOR1
+%left AND1
+%left CMPEQ,CMPNE
+%left CMPLT,CMPLE,CMPGT,CMPGE
+%left RSHIFT,LSHIFT
+%left ARPLUS,ARMINUS
+%left ARTIMES,ARDIVIDE,ARMOD
+%nonassoc NOT,COMP,UMINUS
+%nonassoc '$'
+
+%token SFIT,UFIT,NOTREG,PSIZE,WSIZE,DEFINED,SAMESIGN,ROM,ROTATE
+%token <y_int> MNEM
+%token <y_int> NUMBER
+%type <y_int> expr,argno,optexpr
+
+%start patternlist
+
+%%
+patternlist
+       :       /* empty */
+       |       patternlist '\n'
+       |       patternlist pattern
+       ;
+pattern        :
+               mnemlist optexpr ':' replacement '\n'
+                       { register i;
+                         outbyte(0); outshort(prevind); prevind=curind-3;
+                         out(patlen);
+                         for (i=0;i<patlen;i++) outbyte(patmnem[i]);
+                         out($2);
+                         out(rpllen);
+                         for (i=0;i<rpllen;i++) {
+                               outbyte(rplmnem[i]);
+                               out(rplexpr[i]);
+                         }
+#ifdef DIAGOPT
+                         outshort(patno);
+#endif
+                         patno++;
+                         printf("\n");
+                         if (patlen>maxpatlen) maxpatlen=patlen;
+                       }
+       |       error '\n'
+                       { yyerrok; }
+       ;
+replacement
+       :       expr    /* special optimization */
+                       {
+#ifdef ALLOWSPECIAL
+                         rpllen=1; rplmnem[0]=0; rplexpr[0]=$1;
+#else
+                         yyerror("No specials allowed");
+#endif
+                       }
+       |       repllist
+       ;
+repllist:      /* empty */
+                       { rpllen=0; }
+       |       repllist repl
+       ;
+repl   :       MNEM    optexpr
+                       { rplmnem[rpllen] = $1; rplexpr[rpllen++] = $2; }
+       ;
+mnemlist:      MNEM
+                       { patlen=0; patmnem[patlen++] = $1; }
+       |       mnemlist MNEM
+                       { patmnem[patlen++] = $2; }
+       ;
+optexpr        :       /* empty */
+                       { $$ = 0; }
+       |       expr
+       ;
+expr   
+       :       '$' argno
+                       { $$ = lookup(0,EX_ARG,$2,0); }
+       |       NUMBER
+                       { $$ = lookup(0,EX_CON,(int)(short)$1,0); }
+       |       PSIZE
+                       { $$ = lookup(0,EX_POINTERSIZE,0,0); }
+       |       WSIZE
+                       { $$ = lookup(0,EX_WORDSIZE,0,0); }
+       |       DEFINED '(' expr ')'
+                       { $$ = lookup(0,EX_DEFINED,$3,0); }
+       |       SAMESIGN '(' expr ',' expr ')'
+                       { $$ = lookup(1,EX_SAMESIGN,$3,$5); }
+       |       SFIT '(' expr ',' expr ')'
+                       { $$ = lookup(0,EX_SFIT,$3,$5); }
+       |       UFIT '(' expr ',' expr ')'
+                       { $$ = lookup(0,EX_UFIT,$3,$5); }
+       |       ROTATE '(' expr ',' expr ')'
+                       { $$ = lookup(0,EX_ROTATE,$3,$5); }
+       |       NOTREG '(' expr ')'
+                       { $$ = lookup(0,EX_NOTREG,$3,0); }
+       |       ROM '(' argno ',' expr ')'
+                       { $$ = lookup(0,EX_ROM,$3,$5); }
+       |       '(' expr ')'
+                       { $$ = $2; }
+       |       expr CMPEQ expr
+                       { $$ = lookup(1,EX_CMPEQ,$1,$3); }
+       |       expr CMPNE expr
+                       { $$ = lookup(1,EX_CMPNE,$1,$3); }
+       |       expr CMPGT expr
+                       { $$ = lookup(0,EX_CMPGT,$1,$3); }
+       |       expr CMPGE expr
+                       { $$ = lookup(0,EX_CMPGE,$1,$3); }
+       |       expr CMPLT expr
+                       { $$ = lookup(0,EX_CMPLT,$1,$3); }
+       |       expr CMPLE expr
+                       { $$ = lookup(0,EX_CMPLE,$1,$3); }
+       |       expr OR2 expr
+                       { $$ = lookup(0,EX_OR2,$1,$3); }
+       |       expr AND2 expr
+                       { $$ = lookup(0,EX_AND2,$1,$3); }
+       |       expr OR1 expr
+                       { $$ = lookup(1,EX_OR1,$1,$3); }
+       |       expr XOR1 expr
+                       { $$ = lookup(1,EX_XOR1,$1,$3); }
+       |       expr AND1 expr
+                       { $$ = lookup(1,EX_AND1,$1,$3); }
+       |       expr ARPLUS expr
+                       { $$ = lookup(1,EX_PLUS,$1,$3); }
+       |       expr ARMINUS expr
+                       { $$ = lookup(0,EX_MINUS,$1,$3); }
+       |       expr ARTIMES expr
+                       { $$ = lookup(1,EX_TIMES,$1,$3); }
+       |       expr ARDIVIDE expr
+                       { $$ = lookup(0,EX_DIVIDE,$1,$3); }
+       |       expr ARMOD expr
+                       { $$ = lookup(0,EX_MOD,$1,$3); }
+       |       expr LSHIFT expr
+                       { $$ = lookup(0,EX_LSHIFT,$1,$3); }
+       |       expr RSHIFT expr
+                       { $$ = lookup(0,EX_RSHIFT,$1,$3); }
+       |       ARPLUS expr %prec UMINUS
+                       { $$ = $2; }
+       |       ARMINUS expr %prec UMINUS
+                       { $$ = lookup(0,EX_UMINUS,$2,0); }
+       |       NOT expr
+                       { $$ = lookup(0,EX_NOT,$2,0); }
+       |       COMP expr
+                       { $$ = lookup(0,EX_COMP,$2,0); }
+       ;
+argno  :       NUMBER
+                       { if ($1<1 || $1>patlen) {
+                               YYERROR;
+                         }
+                         $$ = (int) $1;
+                       }
+       ;
+
+%%
+
+extern char em_mnem[][4];
+
+#define HASHSIZE       (2*(sp_lmnem-sp_fmnem))
+
+struct hashmnem {
+       char h_name[3];
+       byte h_value;
+} hashmnem[HASHSIZE];
+
+inithash() {
+       register i;
+
+       enter("lab",op_lab);
+       enter("LLP",op_LLP);
+       enter("LEP",op_LEP);
+       enter("SLP",op_SLP);
+       enter("SEP",op_SEP);
+       for(i=0;i<=sp_lmnem-sp_fmnem;i++)
+               enter(em_mnem[i],i+sp_fmnem);
+}
+
+unsigned hashname(name) register char *name; {
+       register unsigned h;
+
+       h = (*name++)&BMASK;
+       h = (h<<4)^((*name++)&BMASK);
+       h = (h<<4)^((*name++)&BMASK);
+       return(h);
+}
+
+enter(name,value) char *name; {
+       register unsigned h;
+
+       h=hashname(name)%HASHSIZE;
+       while (hashmnem[h].h_name[0] != 0)
+               h = (h+1)%HASHSIZE;
+       strncpy(hashmnem[h].h_name,name,3);
+       hashmnem[h].h_value = value;
+}
+
+int mlookup(name) char *name; {
+       register unsigned h;
+
+       h = hashname(name)%HASHSIZE;
+       while (strncmp(hashmnem[h].h_name,name,3) != 0 &&
+              hashmnem[h].h_name[0] != 0)
+               h = (h+1)%HASHSIZE;
+       return(hashmnem[h].h_value&BMASK);      /* 0 if not found */
+}
+
+main() {
+
+       inithash();
+       initio();
+       yyparse();
+       if (nerrors==0)
+               printnodes();
+       return nerrors;
+}
+
+yyerror(s) char *s; {
+
+       fprintf(stderr,"line %d: %s\n",lino,s);
+       nerrors++;
+}
+
+lookup(comm,operator,lnode,rnode) {
+       register expr_p p;
+
+       for (p=nodes+1;p<lastnode;p++) {
+               if (p->ex_operator != operator)
+                       continue;
+               if (!(p->ex_lnode == lnode && p->ex_rnode == rnode ||
+                   comm && p->ex_lnode == rnode && p->ex_rnode == lnode))
+                       continue;
+               return(p-nodes);
+       }
+       if (lastnode >= &nodes[MAXNODES])
+               yyerror("node table overflow");
+       lastnode++;
+       p->ex_operator = operator;
+       p->ex_lnode = lnode;
+       p->ex_rnode = rnode;
+       return(p-nodes);
+}
+
+printnodes() {
+       register expr_p p;
+
+       printf("};\n\nshort lastind = %d;\n\nexpr_t enodes[] = {\n",prevind);
+       for (p=nodes;p<lastnode;p++)
+               printf("/* %3d */\t%3d,%6u,%6u,\n",
+                       p-nodes,p->ex_operator,p->ex_lnode,p->ex_rnode);
+       printf("};\n\niarg_t iargs[%d];\n",maxpatlen);
+}
+
+initio() {
+       register i;
+
+       printf("#include \"param.h\"\n#include \"types.h\"\n");
+       printf("#include \"pattern.h\"\n\n");
+       for(i=0;i<N_EX_OPS;i++) {
+               nparam[i]=2;
+               nonumlab[i]=TRUE;
+               onlyconst[i]=TRUE;
+       }
+       nparam[EX_POINTERSIZE] = 0;
+       nparam[EX_WORDSIZE] = 0;
+       nparam[EX_CON] = 0;
+       nparam[EX_ROM] = 0;
+       nparam[EX_ARG] = 0;
+       nparam[EX_DEFINED] = 0;
+       nparam[EX_OR2] = 1;
+       nparam[EX_AND2] = 1;
+       nparam[EX_UMINUS] = 1;
+       nparam[EX_NOT] = 1;
+       nparam[EX_COMP] = 1;
+       nparam[EX_NOTREG] = 1;
+       nonumlab[EX_CMPEQ] = FALSE;
+       nonumlab[EX_CMPNE] = FALSE;
+       onlyconst[EX_CMPEQ] = FALSE;
+       onlyconst[EX_CMPNE] = FALSE;
+       onlyconst[EX_CMPLE] = FALSE;
+       onlyconst[EX_CMPLT] = FALSE;
+       onlyconst[EX_CMPGE] = FALSE;
+       onlyconst[EX_CMPGT] = FALSE;
+       onlyconst[EX_PLUS] = FALSE;
+       onlyconst[EX_MINUS] = FALSE;
+       printf("byte nparam[] = {");
+       for (i=0;i<N_EX_OPS;i++) printf("%d,",nparam[i]);
+       printf("};\nbool nonumlab[] = {");
+       for (i=0;i<N_EX_OPS;i++) printf("%d,",nonumlab[i]);
+       printf("};\nbool onlyconst[] = {");
+       for (i=0;i<N_EX_OPS;i++) printf("%d,",onlyconst[i]);
+       printf("};\n\nbyte pattern[] = { 0\n");
+       curind = 1;
+}
+
+outbyte(b) {
+
+       printf(",%3d",b);
+       curind++;
+}
+
+outshort(s) {
+
+       outbyte(s&0377);
+       outbyte((s>>8)&0377);
+}
+
+out(w) {
+
+       if (w<255) {
+               outbyte(w);
+       } else {
+               outbyte(255);
+               outshort(w);
+       }
+}
+
+#include "scan.c"
diff --git a/util/opt/optim.h b/util/opt/optim.h
new file mode 100644 (file)
index 0000000..4dfea7c
--- /dev/null
@@ -0,0 +1,10 @@
+/* #define DIAGOPT /* if defined diagnostics are produced */
+#ifdef DIAGOPT
+#define OPTIM(x) optim(x)
+#define O_UNREACH 1001
+#define O_BRALAB  1002
+#define O_LINLNI  1003
+#define O_LINGONE 1004
+#else
+#define OPTIM(x)       /* NOTHING */
+#endif
diff --git a/util/opt/param.h b/util/opt/param.h
new file mode 100644 (file)
index 0000000..c87d32e
--- /dev/null
@@ -0,0 +1,13 @@
+#define LONGOFF                /* if defined long offsets are used */
+
+#define TRUE   1
+#define FALSE  0
+
+#define MAXROM 3
+
+#define op_lab (sp_lmnem+1)
+#define op_last        op_lab
+#define ps_sym (sp_lpseu+1)
+#define ps_last        ps_sym
+
+#define BMASK  0377
diff --git a/util/opt/pattern.h b/util/opt/pattern.h
new file mode 100644 (file)
index 0000000..4066fa7
--- /dev/null
@@ -0,0 +1,124 @@
+/*
+ * pattern contains the optimization patterns in an apparently
+ * unordered fashion. All patterns follow each other unaligned.
+ * Each pattern looks as follows:
+ *   Byte 0:   high byte of hash value associated with this pattern.
+ *   Byte 1-2: index of next pattern with same low byte of hash value.
+ *   Byte 3- : pattern and replacement.
+ *                First comes the pattern length
+ *                then the pattern opcodes,
+ *               then a boolean expression,
+ *               then the one-byte replacement length
+ *               then the intermixed pattern opcodes and operands or
+ *               0 followed by the one-byte special optimization expression.
+ *   If the DIAGOPT option is set, the optimization is followed
+ *   by the line number in the tables.
+ */
+
+/* #define ALLOWSPECIAL /* Special optimizations allowed */
+
+#define PO_HASH                0
+#define PO_NEXT                1
+#define PO_MATCH       3
+
+struct exprnode {
+       short ex_operator;
+       short ex_lnode;
+       short ex_rnode;
+};
+typedef struct exprnode expr_t;
+typedef struct exprnode *expr_p;
+
+/*
+ * contents of .ex_operator
+ */
+
+#define EX_CON         0
+#define EX_ARG         1
+#define EX_CMPEQ       2
+#define EX_CMPNE       3
+#define EX_CMPGT       4
+#define EX_CMPGE       5
+#define EX_CMPLT       6
+#define EX_CMPLE       7
+#define EX_OR2         8
+#define EX_AND2                9
+#define EX_OR1         10
+#define EX_XOR1                11
+#define EX_AND1                12
+#define EX_PLUS                13
+#define EX_MINUS       14
+#define EX_TIMES       15
+#define EX_DIVIDE      16
+#define EX_MOD         17
+#define EX_LSHIFT      18
+#define EX_RSHIFT      19
+#define EX_UMINUS      20
+#define EX_NOT         21
+#define EX_COMP                22
+#define EX_ROM         23
+#define EX_NOTREG      24
+#define EX_POINTERSIZE 25
+#define EX_WORDSIZE    26
+#define EX_DEFINED     27
+#define EX_SAMESIGN    28
+#define EX_SFIT                29
+#define EX_UFIT                30
+#define EX_ROTATE      31
+#define N_EX_OPS       32      /* must be one higher then previous */
+
+
+/*
+ * Definition of special opcodes used in patterns
+ */
+
+#define op_pfirst op_LLP
+#define op_LLP (op_last+1)
+#define op_LEP (op_last+2)
+#define op_SLP (op_last+3)
+#define op_SEP (op_last+4)
+#define op_plast op_SEP
+
+/*
+ * Definition of the structure in which instruction operands
+ * are kept during pattern matching.
+ */
+
+typedef struct eval eval_t;
+typedef struct eval *eval_p;
+
+struct eval {
+       short   e_typ;
+       union {
+               offset  e_con;
+               num_p   e_np;
+       } e_v;
+};
+
+/*
+ * contents of .e_typ
+ */
+#define EV_UNDEF       0
+#define EV_CONST       1
+#define EV_NUMLAB      2
+#define EV_FRAG                3       /* and all higher numbers */
+
+typedef struct iarg iarg_t;
+typedef struct iarg *iarg_p;
+
+struct iarg {
+       eval_t  ia_ev;
+       sym_p   ia_sp;
+};
+
+/*
+ * The next extern declarations refer to data generated by mktab
+ */
+
+extern byte pattern[];
+extern short  lastind;
+extern iarg_t iargs[];
+extern byte nparam[];
+extern bool nonumlab[];
+extern bool onlyconst[];
+extern expr_t enodes[];
diff --git a/util/opt/patterns b/util/opt/patterns
new file mode 100644 (file)
index 0000000..b993e8f
--- /dev/null
@@ -0,0 +1,474 @@
+loc adi loc sbi $2==w && $4==w: loc $1-$3 adi w
+ldc adi ldc sbi $2==2*w && $4==2*w:     ldc $1-$3 adi 2*w
+loc adi loc adi $2==w && $4==w: loc $1+$3 adi w
+ldc adi ldc adi $2==2*w && $4==2*w:     ldc $1+$3 adi 2*w
+adp $1==0:
+adp adp :       adp $1+$2
+adp lof :       lof $1+$2
+adp ldf :       ldf $1+$2
+adp loi $1!=0 && $2==w: lof $1
+adp loi $1!=0 && $2==2*w:       ldf $1
+adp stf :       stf $1+$2
+adp sdf :       sdf $1+$2
+adp sti $1!=0 && $2==w: stf $1
+adp sti $1!=0 && $2==2*w:       sdf $1
+asp $1==0:
+asp asp :       asp $1+$2
+blm $1==0 : asp 2*p
+cmi zeq $1==w:  beq $2
+cmi zge $1==w:  bge $2
+cmi zgt $1==w:  bgt $2
+cmi zle $1==w:  ble $2
+cmi zlt $1==w:  blt $2
+cmi zne $1==w:  bne $2
+dvi ngi $1==$2: ngi $1  dvi $1
+lae adp :       lae $1+$2
+lae blm $2==w:  loi w   ste $1
+lae blm $2==2*w:        loi 2*w sde $1
+lae ldf :       lde $1+$2
+lae lof :       loe $1+$2
+lae loi $2==w:  loe $1
+lae loi $2==2*w:        lde $1
+#ifdef INT
+lae loi loe $3==$1-w && $2%w==0:        lae $3  loi $2+w
+lae loi lde $3==$1-2*w && $2%w==0:      lae $3  loi $2+2*w
+lae loi lae loi $1==$3+$4 && $2%w==0 && $4%w==0:        lae $3  loi $2+$4
+lae sti ste $3==$1+$2:  lae $1  sti $2+w
+lae sti sde $3==$1+$2:  lae $1  sti $2+2*w
+lae sti loc ste $4==$1-w:       loc $3  lae $4  sti $2+w
+lae sti lol ste $4==$1-w:       lol $3  lae $4  sti $2+w
+#endif
+lae lae blm loe ste $4==$1+$3 && $5==$2+$3:     lae $1  lae $2  blm $3+w
+lae lae blm lde sde $4==$1+$3 && $5==$2+$3:     lae $1  lae $2  blm $3+2*w
+lae lae blm lae lae blm $4==$1+$3 && $5==$2+$3: lae $1  lae $2  blm $3+$6
+lae lal blm lae lal blm $4==$1+$3 && $5==$2+$3 && samesign($2,$5):
+       lae $1  lal $2  blm $3+$6
+lal lae blm lal lae blm $4==$1+$3 && $5==$2+$3 && samesign($1,$4):
+       lal $1  lae $2  blm $3+$6
+lal lal blm lal lal blm $4==$1+$3 && $5==$2+$3 && samesign($1,$4) && samesign($2,$5):
+       lal $1  lal $2  blm $3+$6
+lal lal sbs $3==w && samesign($1,$2): loc $1-$2
+lae sdf :       sde $1+$2
+lae stf :       ste $1+$2
+lae sti $2==w:  ste $1
+lae sti $2==2*w:        sde $1
+lal adp samesign($1,$1+$2):     lal $1+$2
+lal blm $2==w:  loi w   stl $1
+lal blm $2==2*w:        loi 2*w sdl $1
+#ifdef INT
+lal sti loc stl notreg($4) && $4==$1-w && samesign($1,$4):
+       loc $3  lal $4  sti $2+w
+lal sti loe stl notreg($4) && $4==$1-w && samesign($1,$4):
+       loe $3  lal $4  sti $2+w
+#endif
+lal ldf samesign($1,$1+$2):     ldl $1+$2
+lal lof samesign($1,$1+$2):     lol $1+$2
+lal loi $2==w:  lol $1
+lal loi $2==2*w:        ldl $1
+#ifdef INT
+lal loi lol notreg($3) && $3==$1-w && samesign($1,$3) && $2%w==0:
+       lal $3  loi $2+w
+lal loi ldl notreg($3) && $3==$1-2*w && samesign($1,$3) && $2%w==0:
+       lal $3  loi $2+2*w
+lal loi lal loi $1==$3+$4 && samesign($1,$3) && $2%w==0 && $4%w==0:
+       lal $3  loi $2+$4
+lal sti stl notreg($3) && $3==$1+$2 && samesign($1,$3): lal $1  sti $2+w
+lal sti sdl notreg($3) && $3==$1+$2 && samesign($1,$3): lal $1  sti $2+2*w
+#endif
+lal sdf samesign($1,$1+$2):     sdl $1+$2
+lal stf samesign($1,$1+$2):     stl $1+$2
+lal sti $2==w:  stl $1
+lal sti $2==2*w:        sdl $1
+#ifdef INT
+lde lde $2==$1-2*w:     lae $2  loi 4*w
+lde loe $2==$1-w:       lae $2  loi 3*w
+#endif
+lde sde $2==$1:
+lde sde lde sde $3==$1+2*w && $4==$2+2*w:       lae $1  lae $2  blm 4*w
+#ifdef INT
+ldl ldl $2==$1-2*w && notreg($1) && notreg($2) && samesign($1,$2):
+       lal $2  loi 4*w
+ldl lol $2==$1-w && notreg($1) && notreg($2) && samesign($1,$2):
+       lal $2  loi 3*w
+#endif
+ldl sdl $1==$2:
+lxa loi lxa sti $3==$1 && $4==$2:
+lxa lof lxa stf $3==$1 && $4==$2:
+lxa ldf lxa sdf $3==$1 && $4==$2:
+lxa stf lxa lof $3==$1 && $4==$2:       dup w   lxa $1  stf $2
+lxa sdf lxa ldf $3==$1 && $4==$2:       dup 2*w lxa $1  sdf $2
+lxl lof lxl stf $3==$1 && $4==$2:
+lxl ldf lxl sdf $3==$1 && $4==$2:
+lxl stf lxl lof $3==$1 && $4==$2:       dup w   lxl $1  stf $2
+lxl sdf lxl ldf $3==$1 && $4==$2:       dup 2*w lxl $1  sdf $2
+lxa sti lxa loi $3==$1 && $4==$2 && $2%w==0:    dup $2  lxa $1  sti $2
+loc adi $1==-1 && $2==w:        dec
+loc dec sfit($1-1,8*w) :       loc $1-1
+loc bgt $1==-1: zge $2
+loc ble $1==-1: zlt $2
+loc dvi $1==-1 && $2==w:        ngi w
+ldc dvi $1==-1 && $2==2*w:      ngi 2*w
+loc loe adi $1==-1 && $3==w:    loe $2  dec
+loc loe mli $1==-1 && $3==w:    loe $2  ngi w
+loc lol adi $1==-1 && $3==w:    lol $2  dec
+loc mli $1==-1 && $2==w:        ngi w
+ldc mli $1==-1 && $2==2*w:      ngi 2*w
+loc sbi $1==-1 && $2==w:        inc
+loc inc sfit($1+1,8*w) :       loc $1+1
+loc adi $1==0 && $2==w:
+ldc adi $1==0 && $2==2*w:
+zer adi $1==$2:
+loc beq $1==0:  zeq $2
+loc bge $1==0:  zge $2
+loc bgt $1==0:  zgt $2
+loc ble $1==0:  zle $2
+loc blt $1==0:  zlt $2
+loc bne $1==0:  zne $2
+loc cmi teq $1==0 && $2==w:     teq
+loc cmi tge $1==0 && $2==w:     tge
+loc cmi tgt $1==0 && $2==w:     tgt
+loc cmi tle $1==0 && $2==w:     tle
+loc cmi tlt $1==0 && $2==w:     tlt
+loc cmi tne $1==0 && $2==w:     tne
+loc ior $1==0 && $2==w:
+ldc ior $1==0 && $2==2*w:
+zer ior $1==$2:
+loc ste $1==0:  zre $2
+loc stl $1==0:  zrl $2
+loc sbi $1==0 && $2==w:
+ldc sbi $1==0 && $2==2*w:
+zer sbi $1==$2:
+loc xor $1==0 && $2==w:
+ldc xor $1==0 && $2==2*w:
+zer xor $1==$2:
+loc adi $1==1 && $2==w: inc
+loc bge $1==1:  zgt $2
+loc blt $1==1:  zle $2
+loc dvi $1==1 && $2==w:
+ldc dvi $1==1 && $2==2*w:
+loc loe adi $1==1 && $3==w:     loe $2  inc
+loc loe mli $1==1 && $3==w:     loe $2
+loc lol adi $1==1 && $3==w:     lol $2  inc
+loc lol mli $1==1 && $3==w:     lol $2
+loc mli $1==1 && $2==w:
+loc sbi $1==1 && $2==w: dec
+loc loe mli $3==w:      loe $2  loc $1  mli w
+loc lol mli $3==w:      lol $2  loc $1  mli w
+ldc lde mli $3==2*w:    lde $2  ldc $1  mli 2*w
+ldc lde adi $3==2*w:    lde $2  ldc $1  adi 2*w
+ldc ldl mli $3==2*w:    ldl $2  ldc $1  mli 2*w
+ldc ldl adi $3==2*w:    ldl $2  ldc $1  adi 2*w
+loc mli $1==2 && $2==w:        loc 1   sli w
+loc mli $1==4 && $2==w:        loc 2   sli w
+loc mli $1==8 && $2==w:        loc 3   sli w
+loc mli $1==16 && $2==w:        loc 4   sli w
+loc mli $1==32 && $2==w:        loc 5   sli w
+loc mli $1==64 && $2==w:        loc 6   sli w
+loc mli $1==128 && $2==w:       loc 7   sli w
+loc mli $1==256 && $2==w:       loc 8   sli w
+loc adi !defined($2):   adi $1
+loc sbi !defined($2):   sbi $1
+loc mli !defined($2):   mli $1
+loc dvi !defined($2):   dvi $1
+loc rmi !defined($2):   rmi $1
+loc ngi !defined($2):   ngi $1
+loc sli !defined($2):   sli $1
+loc sri !defined($2):   sri $1
+loc adu !defined($2):   adu $1
+loc sbu !defined($2):   sbu $1
+loc mlu !defined($2):   mlu $1
+loc dvu !defined($2):   dvu $1
+loc rmu !defined($2):   rmu $1
+loc slu !defined($2):   slu $1
+loc sru !defined($2):   sru $1
+loc adf !defined($2):   adf $1
+loc sbf !defined($2):   sbf $1
+loc mlf !defined($2):   mlf $1
+loc dvf !defined($2):   dvf $1
+loc ngf !defined($2):   ngf $1
+loc fif !defined($2):   fif $1
+loc fef !defined($2):   fef $1
+loc zer !defined($2):   zer $1
+loc zrf !defined($2):   zrf $1
+loc los $2==w: loi $1
+loc sts $2==w: sti $1
+loc ads $2==w: adp $1
+loc ass $2==w: asp $1
+loc bls $2==w: blm $1
+loc dus $2==w: dup $1
+loc loc cii $1==$2:
+loc loc cuu $1==$2:
+loc loc cff $1==$2:
+loc and !defined($2):   and $1
+loc ior !defined($2):   ior $1
+loc xor !defined($2):   xor $1
+loc com !defined($2):   com $1
+loc rol !defined($2):   rol $1
+loc rol $1==0:
+loc ror !defined($2):   ror $1
+loc ror $1==0:
+loc inn !defined($2):   inn $1
+loc set !defined($2):   set $1
+loc cmi !defined($2):   cmi $1
+loc cmu !defined($2):   cmu $1
+loc cmf !defined($2):   cmf $1
+loe dec ste $1==$3:     dee $1
+loe inc ste $1==$3:     ine $1
+loe loc mli $2==0 && $3==w:     loc 0
+#ifdef INT
+loe loe $2==$1-w:       lde $2
+loe loe beq $2==$1+w:   lde $1  beq $3
+loe loe bge $2==$1+w:   lde $1  ble $3
+loe loe bgt $2==$1+w:   lde $1  blt $3
+loe loe ble $2==$1+w:   lde $1  bge $3
+loe loe blt $2==$1+w:   lde $1  bgt $3
+loe loe bne $2==$1+w:   lde $1  bne $3
+loe loe cmi $2==$1+w && $3==w:  lde $1  cmi w   ngi w
+#endif
+ngi teq $1==w:  teq
+ngi tge $1==w:  tle
+ngi tgt $1==w:  tlt
+ngi tle $1==w:  tge
+ngi tlt $1==w:  tgt
+ngi tne $1==w:  tne
+#ifdef INT
+loe loe mli $2==$1+w && $3==w:  lde $1  mli w
+loe loe adi $2==$1+w && $3==w:  lde $1  adi w
+loe loe $1==$2: loe $1  dup w
+#endif
+loe ste $1==$2:
+LLP blm $2==w:  loi w   sil $1
+lol dec stl $1==$3:     del $1
+lol inc stl $1==$3:     inl $1
+lol loc mli $2==0 && $3==w:     loc 0
+LLP loi $2==w:  lil $1
+#ifdef INT
+lol lol $2==$1-w && notreg($1) && notreg($2) && samesign($1,$2):
+       ldl $2
+lol lol beq $2==$1+w && notreg($1) && notreg($2) && samesign($1,$2):
+       ldl $1  beq $3
+lol lol bge $2==$1+w && notreg($1) && notreg($2) && samesign($1,$2):
+       ldl $1  ble $3
+lol lol bgt $2==$1+w && notreg($1) && notreg($2) && samesign($1,$2):
+       ldl $1  blt $3
+lol lol ble $2==$1+w && notreg($1) && notreg($2) && samesign($1,$2):
+       ldl $1  bge $3
+lol lol blt $2==$1+w && notreg($1) && notreg($2) && samesign($1,$2):
+       ldl $1  bgt $3
+lol lol bne $2==$1+w && notreg($1) && notreg($2) && samesign($1,$2):
+       ldl $1  bne $3
+lol lol cmi $3==w && $2==$1+w && notreg($1) && notreg($2) && samesign($1,$2):
+       ldl $1  cmi w   ngi w
+lol lol mli $3==w && $2==$1+w && notreg($1) && notreg($2) && samesign($1,$2):
+       ldl $1  mli w
+lol lol adi $3==w && $2==$1+w && notreg($1) && notreg($2) && samesign($1,$2):
+       ldl $1  adi w
+lol lol $1==$2: lol $1  dup w
+#endif
+lol stl $1==$2:
+LLP sti $2==w:  sil $1
+mli ngi $1==$2: ngi $1  mli $1
+ngi adi $1==$2: sbi $1
+ngf adf $1==$2: sbf $1
+ngi sbi $1==$2: adi $1
+ngf sbf $1==$2: adf $1
+ngi ngi $1==$2:
+ngf ngf $1==$2:
+#ifdef INT
+sde sde $2==$1+2*w:     lae $1  sti 4*w
+sde ste $2==$1+2*w:     lae $1  sti 3*w
+sde loc ste $3==$1-w:   loc $2  lae $3  sti 3*w
+sde lol ste $3==$1-w:   lol $2  lae $3  sti 3*w
+sde lde $1==$2: dup 2*w sde $1
+#endif
+sdf $1==0:      sti 2*w
+#ifdef INT
+sdl sdl $2==$1+2*w && notreg($1) && notreg($2) && samesign($1,$2):
+       lal $1  sti 4*w
+sdl stl $2==$1+2*w && notreg($1) && notreg($2) && samesign($1,$2):
+       lal $1  sti 3*w
+sdl loc stl $3==$1-w && notreg($1) && notreg($3) && samesign($1,$3):
+       loc $2  lal $3  sti 3*w
+sdl loe stl $3==$1-w && notreg($1) && notreg($3) && samesign($1,$3):
+       loe $2  lal $3  sti 3*w
+sdl ldl $1==$2: dup 2*w sdl $1
+ste loe $1==$2: dup w   ste $1
+ste ste $2==$1-w:       sde $2
+ste loc ste $3==$1-w:   loc $2  sde $3
+ste lol ste $3==$1-w:   lol $2  sde $3
+stl lol $1==$2: dup w   stl $1
+#endif
+stf $1==0: sti w
+sdl ldl ret $1==$2 && $3==2*w:  ret 2*w
+#ifdef INT
+stl stl $2==$1+w && notreg($1) && notreg($2) && samesign($1,$2):        sdl $1
+stl loc stl $3==$1-w && notreg($1) && notreg($3) && samesign($1,$3):
+       loc $2  sdl $3
+stl loe stl $3==$1-w && notreg($1) && notreg($3) && samesign($1,$3):
+       loe $2  sdl $3
+#endif
+stl lol ret $1==$2 && $3==w:    ret w
+lal sti lal loi ret $1==$3 && $2==$4 && $2==$5:        ret $2
+loc sbi loc sbi $2==w && $4==w: loc $1+$3 sbi w
+ldc sbi ldc sbi $2==2*w && $4==2*w:     ldc $1+$3 sbi 2*w
+loc sbi loc adi $2==w && $4==w: loc $1-$3 sbi w
+ldc sbi ldc adi $2==2*w && $4==2*w:     ldc $1-$3 sbi 2*w
+teq teq :       tne
+teq tne :       teq
+teq zne :       zeq $2
+teq zeq :       zne $2
+tge teq :       tlt
+tge tne :       tge
+tge zeq :       zlt $2
+tge zne :       zge $2
+tgt teq :       tle
+tgt tne :       tgt
+tgt zeq :       zle $2
+tgt zne :       zgt $2
+tle teq :       tgt
+tle tne :       tle
+tle zeq :       zgt $2
+tle zne :       zle $2
+tlt teq :       tge
+tlt tne :       tlt
+tlt zeq :       zge $2
+tlt zne :       zlt $2
+tne teq :       teq
+tne tne :       tne
+tne zeq :       zeq $2
+tne zne :       zne $2
+#ifdef INT
+loc loc loc $1==0 && $2==0 && $3==0 :    zer 6
+zer loc defined($1) && $2==0:   zer $1+w
+#endif
+loi loc and $1==1 && $3==w && ($2&255)==255:    loi 1
+loi loc loc cii $1<w && $2==w: loi $1 loc $2 loc $3 cuu
+cmp teq :       cms p   teq
+cmp tne :       cms p   tne
+cmu teq defined($1):    cms $1  teq
+cmu tne defined($1):    cms $1  tne
+cms zeq $1==w:  beq $2
+cms zne $1==w:  bne $2
+lol lae aar adp $3==w:  adp $4  lol $1  lae $2  aar w
+loe lae aar adp $3==w:  adp $4  loe $1  lae $2  aar w
+cmi zeq defined($1):    cms $1  zeq $2
+cmi zne defined($1):    cms $1  zne $2
+loe inc dup ste $1==$4 && $3==w:        ine $1  loe $1
+loe dec dup ste $1==$4 && $3==w:        dee $1  loe $1
+lol inc dup stl $1==$4 && $3==w:        inl $1  lol $1
+lol dec dup stl $1==$4 && $3==w:        del $1  lol $1
+adp dup SEP adp $1==-$4 && $2==p:       dup p   adp $1   SEP $3
+adp dup SLP adp $1==-$4 && $2==p:       dup p   adp $1   SLP $3
+inc dup ste dec $2==w:  dup w   inc     ste $3
+inc dup stl dec $2==w:  dup w   inc     stl $3
+zeq bra lab $1==$3:     zne $2  lab $1
+zge bra lab $1==$3:     zlt $2  lab $1
+zgt bra lab $1==$3:     zle $2  lab $1
+zlt bra lab $1==$3:     zge $2  lab $1
+zle bra lab $1==$3:     zgt $2  lab $1
+zne bra lab $1==$3:     zeq $2  lab $1
+beq bra lab $1==$3:     bne $2  lab $1
+bge bra lab $1==$3:     blt $2  lab $1
+bgt bra lab $1==$3:     ble $2  lab $1
+blt bra lab $1==$3:     bge $2  lab $1
+ble bra lab $1==$3:     bgt $2  lab $1
+bne bra lab $1==$3:     beq $2  lab $1
+lin lin :       lin $2
+lin lab lin :   lab $2  lin $3
+lin ret :       ret $2
+lin bra :       bra $2
+dup SLP loi $1==p && $3==w:     SLP $2  lil $2
+dup SLP sti $1==p && $3==w:     SLP $2  sil $2
+loc cms $1==0 && $2==w: tne
+zer $1==w: loc 0
+loc loc adi $3==w && sfit($1+$2,8*w) : loc $1+$2
+loc loc sbi $3==w && sfit($1-$2,8*w) : loc $1-$2
+loc loc mli $3==w && sfit($1*$2,8*w) : loc $1*$2
+loc loc dvi $3==w && $2!=0 : loc $1/$2
+loc loc and $3==w :    loc $1&$2
+loc loc ior $3==w :    loc $1|$2
+loc loc ior $1==0 && $2==0 && $3==2*w :        
+loc loc xor $3==w :    loc $1^$2
+loc loc xor $1==0 && $2==0 && $3==2*w :        
+loc loc rol $3==w :    loc rotate($1,$2)
+loc loc ror $3==w :    loc rotate($1,8*w-$2)
+loc ngi $2==w && sfit(-$1,8*w) : loc -$1
+loc com $2==w :        loc ~$1
+ldc ngi $2==2*w : ldc -$1
+loc lae aar $3==w && $1>=rom(2,0) && $1 <= rom(2,0)+rom(2,1) :
+       adp ($1-rom(2,0))*rom(2,2)
+loc lae lar $3==w && $1>=rom(2,0) && $1 <= rom(2,0)+rom(2,1) :
+       adp ($1-rom(2,0))*rom(2,2) loi rom(2,2)
+loc lae sar $3==w && $1>=rom(2,0) && $1 <= rom(2,0)+rom(2,1) :
+       adp ($1-rom(2,0))*rom(2,2) sti rom(2,2)
+loc teq : loc $1==0
+loc tne : loc $1!=0
+loc tge : loc $1>=0
+loc tle : loc $1<=0
+loc tgt : loc $1>0
+loc tlt : loc $1<0
+loc zeq $1==0 : bra $2
+loc zeq :
+loc zne $1!=0 : bra $2
+loc zne :
+loc zge $1>=0 : bra $2
+loc zge :
+loc zle $1<=0 : bra $2
+loc zle :
+loc zgt $1>0 : bra $2
+loc zgt :
+loc zlt $1<0 : bra $2
+loc zlt :
+loc loc beq $1==$2 : bra $3
+loc loc beq :
+loc loc bne $1!=$2 : bra $3
+loc loc bne :
+loc loc bge $1>=$2 : bra $3
+loc loc bge :
+loc loc ble $1<=$2 : bra $3
+loc loc ble :
+loc loc bgt $1>$2 : bra $3
+loc loc bgt :
+loc loc blt $1<$2 : bra $3
+loc loc blt :
+lae loi lal sti $2==$4 && $2>4*w : lae $1 lal $3 blm $2
+lal loi lae sti $2==$4 && $2>4*w : lal $1 lae $3 blm $2
+lal loi lal sti $2==$4 && $2>4*w && ( $3<=$1-$2 || $3>=$1+$2 ) :
+       lal $1 lal $3 blm $2
+lae loi lae sti $2==$4 && $2>4*w && ( !defined($1==$3) || $3<=$1-$2 || $3>=$1+$2 ) :
+       lae $1 lae $3 blm $2
+loc loc loc cif $1==0 && $2==w :       zrf $3
+loc loc loc cii $2==w && $3==2*w : ldc $1
+loc loc loc ciu $1>=0 && $2==w && $3==2*w : ldc $1
+loi loc inn $1==$3 && $2>=0 && $2<$1*8 : 
+       lof ($2/(8*w))*w loc $2&(8*w-1) inn w
+ldl loc inn $3==2*w && $2>=0 && $2<16*w : 
+       lol $1+($2/(8*w))*w loc $2&(8*w-1) inn w
+lde loc inn $3==2*w && $2>=0 && $2<16*w : 
+       loe $1+($2/(8*w))*w loc $2&(8*w-1) inn w
+ldf loc inn $3==2*w && $2>=0 && $2<16*w : 
+       lof $1+($2/(8*w))*w loc $2&(8*w-1) inn w
+loc inn $1<0 || $1>=8*$2 : asp $2 loc 0
+lol loc adi stl $3==w && $1==$4 : loc $2 lol $1 adi w stl $4
+lol loe adi stl $3==w && $1==$4 : loe $2 lol $1 adi w stl $4
+lol lol adi stl $3==w && $1==$4 && $1!=$2 : lol $2 lol $1 adi w stl $4
+loe loc adi ste $3==w && $1==$4 : loc $2 loe $1 adi w ste $4
+loe loe adi ste $3==w && $1==$4 && $1!=$2 : loe $2 loe $1 adi w ste $4
+loe lol adi ste $3==w && $1==$4 : lol $2 loe $1 adi w ste $4
+lol loc ior stl $3==w && $1==$4 : loc $2 lol $1 ior w stl $4
+lol loe ior stl $3==w && $1==$4 : loe $2 lol $1 ior w stl $4
+lol lol ior stl $3==w && $1==$4 && $1!=$2 : lol $2 lol $1 ior w stl $4
+loe loc ior ste $3==w && $1==$4 : loc $2 loe $1 ior w ste $4
+loe loe ior ste $3==w && $1==$4 && $1!=$2 : loe $2 loe $1 ior w ste $4
+loe lol ior ste $3==w && $1==$4 : lol $2 loe $1 ior w ste $4
+lol loc and stl $3==w && $1==$4 : loc $2 lol $1 and w stl $4
+lol loe and stl $3==w && $1==$4 : loe $2 lol $1 and w stl $4
+lol lol and stl $3==w && $1==$4 && $1!=$2 : lol $2 lol $1 and w stl $4
+loe loc and ste $3==w && $1==$4 : loc $2 loe $1 and w ste $4
+loe loe and ste $3==w && $1==$4 && $1!=$2 : loe $2 loe $1 and w ste $4
+loe lol and ste $3==w && $1==$4 : lol $2 loe $1 and w ste $4
+loi asp $1==$2 : asp p
+lal loi loc loc loc loc ior $2==4*w && $7==4*w && ($3==0)+($4==0)+($5==0)+($6==0)>2 :
+       lol $1+3*w loc $3 ior w lol $1+2*w loc $4 ior w lol $1+w loc $5 ior w lol $1 loc $6 ior w
+loc dup stl loc dup stl $2==2 && $5==2:
+       loc $1 stl $3 loc $4 stl $6 loc $1 loc $4
diff --git a/util/opt/peephole.c b/util/opt/peephole.c
new file mode 100644 (file)
index 0000000..8696cb8
--- /dev/null
@@ -0,0 +1,648 @@
+#include "param.h"
+#include "types.h"
+#include "assert.h"
+#include "line.h"
+#include "lookup.h"
+#include "proinf.h"
+#include "alloc.h"
+#include "pattern.h"
+#include "../../h/em_spec.h"
+#include "../../h/em_mnem.h"
+#include "optim.h"
+#include "ext.h"
+
+/*
+ * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ *
+ *          This product is part of the Amsterdam Compiler Kit.
+ *
+ * Permission to use, sell, duplicate or disclose this software must be
+ * obtained in writing. Requests for such permissions may be sent to
+ *
+ *      Dr. Andrew S. Tanenbaum
+ *      Wiskundig Seminarium
+ *      Vrije Universiteit
+ *      Postbox 7161
+ *      1007 MC Amsterdam
+ *      The Netherlands
+ *
+ * Author: Hans van Staveren
+ */
+
+/* #define CHK_HASH    /* print numbers patterns are hashed to */
+#ifdef CHK_HASH
+#include <stdio.h>
+#endif
+
+#define ILLHASH 0177777
+short pathash[256];    /* table of indices into pattern[] */
+
+int opind = 0;         /* second index of next matrix */
+byte transl[op_plast-op_pfirst+1][3] = {
+       /* LLP */       { op_LLP, op_lol, op_ldl },
+       /* LEP */       { op_LEP, op_loe, op_lde },
+       /* SLP */       { op_SLP, op_stl, op_sdl },
+       /* SEP */       { op_SEP, op_ste, op_sde }
+};
+
+opcheck(bp) register byte *bp; {
+
+       if (((*bp)&BMASK) >= op_pfirst)
+               *bp = transl[((*bp)&BMASK)-op_pfirst][opind];
+}
+
+/*
+ * The hashing method used is believed to be reasonably efficient.
+ * A minor speed improvement could be obtained by keeping a boolean
+ * array telling which opcode has any patterns starting with it.
+ * Currently only about one third of the opcodes actually have a
+ * pattern starting with it, but they are the most common ones.
+ * Estimated improvement possible: about 2%
+ */
+
+hashpatterns() {
+       short index;
+       register byte *bp,*tp;
+       register short i;
+       unsigned short hashvalue;
+       byte *save;
+       int patlen;
+
+       if (pointersize == wordsize)
+               opind=1;
+       else if (pointersize == 2*wordsize)
+               opind=2;
+       index = lastind;        /* set by mktab */
+       while (index != 0) {
+               bp = &pattern[index];
+               tp = &bp[PO_MATCH];
+               i = *tp++&BMASK;
+               if (i==BMASK) {
+                       i = *tp++&BMASK;
+                       i |= (*tp++&BMASK)<<8;
+               }
+               save = tp;
+               patlen = i;
+               while (i--)
+                       opcheck(tp++);
+               if ((*tp++&BMASK)==BMASK)
+                       tp += 2;
+               i = *tp++&BMASK;
+               if (i==BMASK) {
+                       i = *tp++&BMASK;
+                       i |= (*tp++&BMASK)<<8;
+               }
+               while (i--) {
+                       opcheck(tp++);
+                       if ((*tp++&BMASK)==BMASK)
+                               tp += 2;
+               }
+
+               /*
+                * Now the special opcodes are filled
+                * in properly, we can hash the pattern
+                */
+
+               hashvalue = 0;
+               tp = save;
+               switch(patlen) {
+               default:        /* 3 or more */
+                       hashvalue = (hashvalue<<4)^(*tp++&BMASK);
+               case 2:
+                       hashvalue = (hashvalue<<4)^(*tp++&BMASK);
+               case 1:
+                       hashvalue = (hashvalue<<4)^(*tp++&BMASK);
+               }
+               assert(hashvalue!= ILLHASH);
+               i=index;
+               index = (bp[PO_NEXT]&BMASK)|(bp[PO_NEXT+1]<<8);
+               bp[PO_HASH] = hashvalue>>8;
+               hashvalue &= BMASK;
+               bp[PO_NEXT] = pathash[hashvalue]&BMASK;
+               bp[PO_NEXT+1] = pathash[hashvalue]>>8;
+               pathash[hashvalue] = i;
+#ifdef CHK_HASH
+               fprintf(stderr,"%d\n",hashvalue);
+#endif
+       }
+}
+
+peephole() {
+       static bool phashed = FALSE;
+
+       if (!phashed) {
+               hashpatterns();
+               phashed=TRUE;
+       }
+       optimize();
+}
+
+optimize() {
+       register num_p *npp,np;
+       register instr;
+
+       basicblock(&instrs);
+       for (npp=curpro.numhash;npp< &curpro.numhash[NNUMHASH]; npp++)
+               for (np = *npp; np != (num_p) 0; np=np->n_next) {
+                       if(np->n_line->l_next == (line_p) 0)
+                               continue;
+                       instr = np->n_line->l_next->l_instr&BMASK;
+                       if (instr == op_lab || instr == op_bra)
+                               np->n_repl = np->n_line->l_next->l_a.la_np;
+                       else
+                               basicblock(&np->n_line->l_next);
+               }
+}
+
+offset oabs(off) offset off; {
+
+       return(off >= 0 ? off : -off);
+}
+
+line_p repline(ev,patlen) eval_t ev; {
+       register line_p lp;
+       register iarg_p iap;
+       register sym_p sp;
+       offset diff,newdiff;
+
+       assert(ev.e_typ != EV_UNDEF);
+       switch(ev.e_typ) {
+       case EV_CONST:
+               if ((short) ev.e_v.e_con == ev.e_v.e_con) {
+                       if (CANMINI((short) ev.e_v.e_con))
+                               lp = newline((short) (ev.e_v.e_con)+Z_OPMINI);
+                       else {
+                               lp = newline(OPSHORT);
+                               lp->l_a.la_short = (short) ev.e_v.e_con;
+                       }
+               } else {
+                       lp = newline(OPOFFSET);
+                       lp->l_a.la_offset = ev.e_v.e_con;
+               }
+               return(lp);
+       case EV_NUMLAB:
+               lp = newline(OPNUMLAB);
+               lp->l_a.la_np = ev.e_v.e_np;
+               return(lp);
+       default:        /* fragment + offset */
+               /*
+                * There is a slight problem here, because we have to
+                * map fragment+offset to symbol+offset.
+                * Fortunately the fragment we have must be the fragment
+                * of one of the symbols in the matchpattern.
+                * So a short search should do the job.
+                */
+               sp = (sym_p) 0;
+               for (iap= &iargs[patlen-1]; iap >= iargs; iap--)
+                       if (iap->ia_ev.e_typ == ev.e_typ) {
+                               /*
+                                * Although lint complains, diff is not used
+                                * before set.
+                                *
+                                * The proof is left as an exercise to the
+                                * reader.
+                                */
+                               newdiff = oabs(iap->ia_sp->s_value-ev.e_v.e_con);
+                               if (sp==(sym_p) 0 || newdiff < diff) {
+                                       sp = iap->ia_sp;
+                                       diff = newdiff;
+                               }
+                       }
+               assert(sp != (sym_p) 0);
+               if (diff == 0) {
+                       lp = newline(OPSYMBOL);
+                       lp->l_a.la_sp = sp;
+               } else {
+                       diff = ev.e_v.e_con - sp->s_value;
+                       if ((short) diff == diff) {
+                               lp = newline(OPSVAL);
+                               lp->l_a.la_sval.lasv_short = (short) diff;
+                               lp->l_a.la_sval.lasv_sp = sp;
+                       } else {
+                               lp = newline(OPLVAL);
+                               lp->l_a.la_lval.lalv_offset = diff;
+                               lp->l_a.la_lval.lalv_sp = sp;
+                       }
+               }
+               return(lp);
+       }
+}
+
+offset rotate(w,amount) offset w,amount; {
+       offset highmask,lowmask;
+
+#ifndef LONGOFF
+       assert(wordsize<=4);
+#endif
+       highmask = (offset)(-1) << amount;
+       lowmask = ~highmask;
+       if (wordsize != 4)
+               highmask &= wordsize==2 ? 0xFFFF : 0xFF;
+       return(((w<<amount)&highmask)|((w>>(8*wordsize-amount))&lowmask));
+}
+
+eval_t undefres = { EV_UNDEF };
+
+eval_t compute(pexp) register expr_p pexp; {
+       eval_t leaf1,leaf2,res;
+       register i;
+       register sym_p sp;
+       offset mask;
+
+       switch(nparam[pexp->ex_operator]) {
+       default:
+               assert(FALSE);
+       case 2:
+               leaf2 = compute(&enodes[pexp->ex_rnode]);
+               if (leaf2.e_typ == EV_UNDEF ||
+                   nonumlab[pexp->ex_operator] && leaf2.e_typ == EV_NUMLAB ||
+                   onlyconst[pexp->ex_operator] && leaf2.e_typ != EV_CONST)
+                       return(undefres);
+       case 1:
+               leaf1 = compute(&enodes[pexp->ex_lnode]);
+               if (leaf1.e_typ == EV_UNDEF ||
+                   nonumlab[pexp->ex_operator] && leaf1.e_typ == EV_NUMLAB ||
+                   onlyconst[pexp->ex_operator] && leaf1.e_typ != EV_CONST)
+                       return(undefres);
+       case 0:
+               break;
+       }
+
+       res.e_typ = EV_CONST;
+       res.e_v.e_con = 0;
+       switch(pexp->ex_operator) {
+       default:
+               assert(FALSE);
+       case EX_CON:
+               res.e_v.e_con = (offset) pexp->ex_lnode;
+               break;
+       case EX_ARG:
+               return(iargs[pexp->ex_lnode - 1].ia_ev);
+       case EX_CMPEQ:
+               if (leaf1.e_typ != leaf2.e_typ)
+                       return(undefres);
+               if (leaf1.e_typ == EV_NUMLAB) {
+                       if (leaf1.e_v.e_np == leaf2.e_v.e_np)
+                               res.e_v.e_con = 1;
+                       break;
+               }
+               if (leaf1.e_v.e_con == leaf2.e_v.e_con)
+                       res.e_v.e_con = 1;
+               break;
+       case EX_CMPNE:
+               if (leaf1.e_typ != leaf2.e_typ) {
+                       res.e_v.e_con = 1;
+                       break;
+               }
+               if (leaf1.e_typ == EV_NUMLAB) {
+                       if (leaf1.e_v.e_np != leaf2.e_v.e_np)
+                               res.e_v.e_con = 1;
+                       break;
+               }
+               if (leaf1.e_v.e_con != leaf2.e_v.e_con)
+                       res.e_v.e_con = 1;
+               break;
+       case EX_CMPGT:
+               if (leaf1.e_typ != leaf2.e_typ)
+                       return(undefres);
+               res.e_v.e_con = leaf1.e_v.e_con > leaf2.e_v.e_con;
+               break;
+       case EX_CMPGE:
+               if (leaf1.e_typ != leaf2.e_typ)
+                       return(undefres);
+               res.e_v.e_con = leaf1.e_v.e_con >= leaf2.e_v.e_con;
+               break;
+       case EX_CMPLT:
+               if (leaf1.e_typ != leaf2.e_typ)
+                       return(undefres);
+               res.e_v.e_con = leaf1.e_v.e_con < leaf2.e_v.e_con;
+               break;
+       case EX_CMPLE:
+               if (leaf1.e_typ != leaf2.e_typ)
+                       return(undefres);
+               res.e_v.e_con = leaf1.e_v.e_con <= leaf2.e_v.e_con;
+               break;
+       case EX_OR2:
+               if (leaf1.e_v.e_con != 0)
+                       return(leaf1);
+               leaf2 = compute(&enodes[pexp->ex_rnode]);
+               if (leaf2.e_typ != EV_CONST)
+                       return(undefres);
+               return(leaf2);
+       case EX_AND2:
+               if (leaf1.e_v.e_con == 0)
+                       return(leaf1);
+               leaf2 = compute(&enodes[pexp->ex_rnode]);
+               if (leaf2.e_typ != EV_CONST)
+                       return(undefres);
+               return(leaf2);
+       case EX_OR1:
+               res.e_v.e_con = leaf1.e_v.e_con | leaf2.e_v.e_con;
+               break;
+       case EX_XOR1:
+               res.e_v.e_con = leaf1.e_v.e_con ^ leaf2.e_v.e_con;
+               break;
+       case EX_AND1:
+               res.e_v.e_con = leaf1.e_v.e_con & leaf2.e_v.e_con;
+               break;
+       case EX_TIMES:
+               res.e_v.e_con = leaf1.e_v.e_con * leaf2.e_v.e_con;
+               break;
+       case EX_DIVIDE:
+               res.e_v.e_con = leaf1.e_v.e_con / leaf2.e_v.e_con;
+               break;
+       case EX_MOD:
+               res.e_v.e_con = leaf1.e_v.e_con % leaf2.e_v.e_con;
+               break;
+       case EX_LSHIFT:
+               res.e_v.e_con = leaf1.e_v.e_con << leaf2.e_v.e_con;
+               break;
+       case EX_RSHIFT:
+               res.e_v.e_con = leaf1.e_v.e_con >> leaf2.e_v.e_con;
+               break;
+       case EX_UMINUS:
+               res.e_v.e_con = -leaf1.e_v.e_con;
+               break;
+       case EX_NOT:
+               res.e_v.e_con = !leaf1.e_v.e_con;
+               break;
+       case EX_COMP:
+               res.e_v.e_con = ~leaf1.e_v.e_con;
+               break;
+       case EX_PLUS:
+               if (leaf1.e_typ >= EV_FRAG) {
+                       if (leaf2.e_typ >= EV_FRAG)
+                               return(undefres);
+                       res.e_typ = leaf1.e_typ;
+               } else
+                       res.e_typ = leaf2.e_typ;
+               res.e_v.e_con = leaf1.e_v.e_con + leaf2.e_v.e_con;
+               break;
+       case EX_MINUS:
+               if (leaf1.e_typ >= EV_FRAG) {
+                       if (leaf2.e_typ == EV_CONST)
+                               res.e_typ = leaf1.e_typ;
+                       else if (leaf2.e_typ != leaf1.e_typ)
+                               return(undefres);
+               } else if (leaf2.e_typ >= EV_FRAG)
+                       return(undefres);
+               res.e_v.e_con = leaf1.e_v.e_con - leaf2.e_v.e_con;
+               break;
+       case EX_POINTERSIZE:
+               res.e_v.e_con = pointersize;
+               break;
+       case EX_WORDSIZE:
+               res.e_v.e_con = wordsize;
+               break;
+       case EX_NOTREG:
+               res.e_v.e_con = !inreg(leaf1.e_v.e_con);
+               break;
+       case EX_DEFINED:
+               leaf1 = compute(&enodes[pexp->ex_lnode]);
+               res.e_v.e_con = leaf1.e_typ != EV_UNDEF;
+               break;
+       case EX_SAMESIGN:
+               res.e_v.e_con = (leaf1.e_v.e_con ^ leaf2.e_v.e_con) >= 0;
+               break;
+       case EX_ROM:
+               if ((sp = iargs[pexp->ex_lnode - 1].ia_sp) != (sym_p) 0 &&
+                   sp->s_rom != (offset *) 0) {
+                       leaf2 = compute(&enodes[pexp->ex_rnode]);
+                       if (leaf2.e_typ != EV_CONST ||
+                           leaf2.e_v.e_con < 0 ||
+                           leaf2.e_v.e_con >= MAXROM)
+                               return(undefres);
+                       res.e_v.e_con = sp->s_rom[leaf2.e_v.e_con];
+                       break;
+               } else
+                       return(undefres);
+       case EX_SFIT:
+               mask = 0;
+               for (i=leaf2.e_v.e_con - 1;i < 8*sizeof(offset); i++)
+                       mask |= 1<<i;
+               res.e_v.e_con = (leaf1.e_v.e_con&mask) == 0 ||
+                                      (leaf1.e_v.e_con&mask) == mask;
+               break;
+       case EX_UFIT:
+               mask = 0;
+               for (i=leaf2.e_v.e_con;i < 8*sizeof(offset); i++)
+                       mask |= 1<<i;
+               res.e_v.e_con = (leaf1.e_v.e_con&mask) == 0;
+               break;
+       case EX_ROTATE:
+               res.e_v.e_con = rotate(leaf1.e_v.e_con,leaf2.e_v.e_con);
+               break;
+       }
+       return(res);
+}
+
+#ifdef ALLOWSPECIAL
+extern bool special();
+#endif
+
+bool tryrepl(lpp,bp,patlen)
+line_p *lpp;
+register byte *bp;
+int patlen;
+{
+       int rpllen,instr,rplval;
+       register line_p lp;
+       line_p replacement,*rlpp,tp;
+
+       rpllen = *bp++&BMASK;
+       if (rpllen == BMASK) {
+               rpllen = *bp++&BMASK;
+               rpllen |= (*bp++&BMASK)<<8;
+       }
+#ifdef ALLOWSPECIAL
+       if (rpllen == 1 && *bp == 0)
+               return(special(lpp,bp+1,patlen));
+#endif
+       replacement = (line_p) 0;
+       rlpp = &replacement;
+       while (rpllen--) {
+               instr = *bp++&BMASK;
+               rplval = *bp++&BMASK;
+               if (rplval == BMASK) {
+                       rplval = (*bp++&BMASK);
+                       rplval |= (*bp++&BMASK)<<8;
+               }
+               if (rplval)
+                       lp = repline(compute(&enodes[rplval]),patlen);
+               else
+                       lp = newline(OPNO);
+
+               /*
+                * One replacement instruction is generated,
+                * link in list and proceed with the next one.
+                */
+
+               if (instr == op_lab)
+                       lp->l_a.la_np->n_line = lp;
+               *rlpp = lp;
+               rlpp = &lp->l_next;
+               lp->l_instr = instr;
+       }
+
+       /*
+        * Replace instructions matched by the created replacement
+        */
+
+
+       OPTIM((bp[0]&BMASK)|(bp[1]&BMASK)<<8);
+       for (lp= *lpp;patlen>0;patlen--,tp=lp,lp=lp->l_next)
+               ;
+       tp->l_next = (line_p) 0;
+       *rlpp = lp;
+       lp = *lpp;
+       *lpp = replacement;
+       while ( lp != (line_p) 0 ) {
+               tp = lp->l_next;
+               oldline(lp);
+               lp = tp;
+       }
+       return(TRUE);
+}
+
+bool trypat(lpp,bp,len)
+line_p *lpp;
+register byte *bp;
+int len;
+{
+       register iarg_p iap;
+       int i,patlen;
+       register line_p lp;
+       eval_t result;
+
+       patlen = *bp++&BMASK;
+       if (patlen == BMASK) {
+               patlen = *bp++&BMASK;
+               patlen |= (*bp++&BMASK)<<8;
+       }
+       if (len == 3) {
+               if (patlen<3)
+                       return(FALSE);
+       } else {
+               if (patlen != len)
+                       return(FALSE);
+       }
+
+       /*
+        * Length is ok, now check opcodes
+        */
+
+       for (i=0,lp= *lpp;i<patlen && lp != (line_p) 0;i++,lp=lp->l_next)
+               if (lp->l_instr != *bp++)
+                       return(FALSE);
+       if (i != patlen)
+               return(FALSE);
+
+       /*
+        * opcodes are also correct, now comes the hard part
+        */
+
+       for(i=0,lp= *lpp,iap= iargs; i<patlen;i++,iap++,lp=lp->l_next) {
+               switch(lp->l_optyp) {
+               case OPNO:
+                       iap->ia_ev.e_typ = EV_UNDEF;
+                       break;
+               default:
+                       iap->ia_ev.e_typ = EV_CONST;
+                       iap->ia_ev.e_v.e_con = (lp->l_optyp&BMASK)-Z_OPMINI;
+                       break;
+               case OPSHORT:
+                       iap->ia_ev.e_typ = EV_CONST;
+                       iap->ia_ev.e_v.e_con = lp->l_a.la_short;
+                       break;
+#ifdef LONGOFF
+               case OPOFFSET:
+                       iap->ia_ev.e_typ = EV_CONST;
+                       iap->ia_ev.e_v.e_con = lp->l_a.la_offset;
+                       break;
+#endif
+               case OPNUMLAB:
+                       iap->ia_ev.e_typ = EV_NUMLAB;
+                       iap->ia_ev.e_v.e_np = lp->l_a.la_np;
+                       break;
+               case OPSYMBOL:
+                       iap->ia_ev.e_typ = lp->l_a.la_sp->s_frag;
+                       iap->ia_sp = lp->l_a.la_sp;
+                       iap->ia_ev.e_v.e_con = lp->l_a.la_sp->s_value;
+                       break;
+               case OPSVAL:
+                       iap->ia_ev.e_typ = lp->l_a.la_sval.lasv_sp->s_frag;
+                       iap->ia_sp = lp->l_a.la_sval.lasv_sp;
+                       iap->ia_ev.e_v.e_con = lp->l_a.la_sval.lasv_sp->s_value + lp->l_a.la_sval.lasv_short;
+                       break;
+#ifdef LONGOFF
+               case OPLVAL:
+                       iap->ia_ev.e_typ = lp->l_a.la_lval.lalv_sp->s_frag;
+                       iap->ia_sp = lp->l_a.la_lval.lalv_sp;
+                       iap->ia_ev.e_v.e_con = lp->l_a.la_lval.lalv_sp->s_value + lp->l_a.la_lval.lalv_offset;
+                       break;
+#endif
+               }
+       }
+       i = *bp++&BMASK;
+       if ( i==BMASK ) {
+               i = *bp++&BMASK;
+               i |= (*bp++&BMASK)<<8;
+       }
+       if ( i != 0) {
+               /* there is a condition */
+               result = compute(&enodes[i]);
+               if (result.e_typ != EV_CONST || result.e_v.e_con == 0)
+                       return(FALSE);
+       }
+       return(tryrepl(lpp,bp,patlen));
+}
+
+basicblock(alpp) line_p *alpp; {
+       register line_p *lpp,lp;
+       bool madeopt;
+       unsigned short hash[3];
+       line_p *next;
+       register byte *bp;
+       int i;
+       short index;
+
+       do {    /* make pass over basicblock */
+           lpp = alpp; madeopt = FALSE;
+           while ((*lpp) != (line_p) 0 && ((*lpp)->l_instr&BMASK) != op_lab) {
+               lp = *lpp; next = &lp->l_next;
+               hash[0] = lp->l_instr&BMASK;
+               lp=lp->l_next;
+               if (lp != (line_p) 0) {
+                       hash[1] = (hash[0]<<4)^(lp->l_instr&BMASK);
+                       lp=lp->l_next;
+                       if (lp != (line_p) 0)
+                               hash[2] = (hash[1]<<4)^(lp->l_instr&BMASK);
+                       else
+                               hash[2] = ILLHASH;
+               } else {
+                       hash[1] = ILLHASH;
+                       hash[2] = ILLHASH;
+               }
+
+               /*
+                * hashvalues computed. Try for longest pattern first
+                */
+
+               for (i=2;i>=0;i--) {
+                   index = pathash[hash[i]&BMASK];
+                   while (index != 0) {
+                       bp = &pattern[index];
+                       if((bp[PO_HASH]&BMASK) == (hash[i]>>8))
+                           if(trypat(lpp,&bp[PO_MATCH],i+1)) {
+                               madeopt = TRUE;
+                               next = lpp;
+                               i = 0;  /* dirty way of double break */
+                               break;
+                           }
+                       index=(bp[PO_NEXT]&BMASK)|(bp[PO_NEXT+1]<<8);
+                   }
+               }
+               lpp = next;
+           }
+       } while(madeopt);       /* as long as there is progress */
+}
diff --git a/util/opt/process.c b/util/opt/process.c
new file mode 100644 (file)
index 0000000..12ca07f
--- /dev/null
@@ -0,0 +1,181 @@
+#include "param.h"
+#include "types.h"
+#include "assert.h"
+#include "../../h/em_spec.h"
+#include "../../h/em_pseu.h"
+#include "alloc.h"
+#include "line.h"
+#include "lookup.h"
+#include "proinf.h"
+#include "ext.h"
+
+/*
+ * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ *
+ *          This product is part of the Amsterdam Compiler Kit.
+ *
+ * Permission to use, sell, duplicate or disclose this software must be
+ * obtained in writing. Requests for such permissions may be sent to
+ *
+ *      Dr. Andrew S. Tanenbaum
+ *      Wiskundig Seminarium
+ *      Vrije Universiteit
+ *      Postbox 7161
+ *      1007 MC Amsterdam
+ *      The Netherlands
+ *
+ * Author: Hans van Staveren
+ */
+
+process() {
+
+       if (wordsize == 0 || pointersize == 0)
+               error("No MES EMX encountered");
+       backward();                     /* reverse and cleanup list */
+       symknown();                     /* symbol scope is now known */
+       if (!nflag)
+               symvalue();             /* give symbols value */
+       if (prodepth != 0) {
+               if (!nflag) {
+                       checklocs();    /* check definition of locals */
+                       peephole();     /* local optimization */
+                       relabel();      /* relabel local labels */
+                       flow();         /* throw away unreachable code */
+               }
+               outpro();               /* generate PRO pseudo */
+               outregs();              /* generate MES ms_reg pseudos */
+       }
+       putlines(pseudos);              /* pseudos first */
+       if (prodepth != 0) {
+               putlines(instrs);       /* instructions next */
+               outend();               /* generate END pseudo */
+               cleanlocals();          /* forget instruction labels */
+       } else if(instrs != (line_p) 0)
+               error("instructions outside procedure");
+#ifdef COREDEBUG
+       coreverbose();
+#endif
+}
+
+relabel() {
+       register num_p *npp,np,tp;
+       register num_p repl,ttp;
+
+       /*
+        * For each label find its final destination after crossjumping.
+        * Care has to be taken to prevent a loop in the program to
+        * cause same in the optimizer.
+        */
+
+       for (npp = curpro.numhash; npp < &curpro.numhash[NNUMHASH]; npp++)
+               for (np = *npp; np != (num_p) 0; np = np->n_next) {
+                       assert((np->n_line->l_instr&BMASK) == op_lab
+                           && np->n_line->l_a.la_np == np);
+                       for(tp=np; (tp->n_flags&(NUMKNOWN|NUMMARK))==0;
+                                  tp = tp->n_repl)
+                               tp->n_flags |= NUMMARK;
+                       repl = tp->n_repl;
+                       for(tp=np; tp->n_flags&NUMMARK; tp = ttp) {
+                               ttp = tp->n_repl;
+                               tp->n_repl = repl;
+                               tp->n_flags &= ~ NUMMARK;
+                               tp->n_flags |=   NUMKNOWN;
+                       }
+               }
+}
+
+symknown() {
+       register sym_p *spp,sp;
+
+       for (spp = symhash; spp < &symhash[NSYMHASH]; spp++)
+               for (sp = *spp; sp != (sym_p) 0; sp = sp->s_next)
+                       sp->s_flags |= SYMKNOWN;
+}
+
+cleanlocals() {
+       register num_p *npp,np,tp;
+
+       for (npp = curpro.numhash; npp < &curpro.numhash[NNUMHASH]; npp++) {
+               np = *npp;
+               while (np != (num_p) 0) {
+                       tp = np->n_next;
+                       oldnum(np);
+                       np = tp;
+               }
+               *npp = (num_p) 0;
+       }
+}
+
+checklocs() {
+       register num_p *npp,np;
+
+       for (npp=curpro.numhash; npp < & curpro.numhash[NNUMHASH]; npp++)
+               for (np = *npp; np != (num_p) 0; np=np->n_next)
+                       if (np->n_line == (line_p) 0)
+                               error("local label %u undefined",
+                                       (unsigned) np->n_number);
+}
+
+offset align(count,alignment) offset count,alignment; {
+
+       assert(alignment==1||alignment==2||alignment==4);
+       return((count+alignment-1)&~(alignment-1));
+}
+
+symvalue() {
+       register line_p lp;
+       register sym_p sp;
+       register arg_p ap;
+       register argb_p abp;
+       short curfrag = 0;
+       offset count;
+
+       for (lp=pseudos; lp != (line_p) 0; lp = lp->l_next)
+       switch(lp->l_instr&BMASK) {
+       default:
+               assert(FALSE);
+       case ps_sym:
+               sp = lp->l_a.la_sp;
+               if (sp->s_frag != curfrag) {
+                       count = 0;
+                       curfrag = sp->s_frag;
+               }
+               count = align(count,wordsize);
+               sp->s_value = count;
+               break;
+       case ps_bss:
+       case ps_hol:
+               /* nothing to do, all bss pseudos are in diff frags */
+       case ps_mes:
+               break;
+       case ps_con:
+       case ps_rom:
+               for (ap=lp->l_a.la_arg; ap  != (arg_p) 0; ap = ap->a_next)
+               switch(ap->a_typ) {
+               default:
+                       assert(FALSE);
+               case ARGOFF:
+                       count = align(count,wordsize)+wordsize;
+                       break;
+               case ARGNUM:
+               case ARGSYM:
+               case ARGVAL:
+                       count = align(count,wordsize)+pointersize;
+                       break;
+               case ARGICN:
+               case ARGUCN:
+               case ARGFCN:
+                       if (ap->a_a.a_con.ac_length < wordsize)
+                               count = align(count,(offset)ap->a_a.a_con.ac_length);
+                       else
+                               count = align(count,wordsize);
+                       count += ap->a_a.a_con.ac_length;
+                       break;
+               case ARGSTR:
+                       for (abp = &ap->a_a.a_string; abp != (argb_p) 0;
+                            abp = abp->ab_next)
+                               count += abp->ab_index;
+                       break;
+               }
+       }
+}
diff --git a/util/opt/proinf.h b/util/opt/proinf.h
new file mode 100644 (file)
index 0000000..4ab6851
--- /dev/null
@@ -0,0 +1,33 @@
+struct num {
+       num_p   n_next;
+       unsigned n_number;
+       unsigned n_jumps;
+       num_p   n_repl;
+       short   n_flags;
+       line_p  n_line;
+};
+
+/* contents of .n_flags */
+#define NUMDATA                000001
+#define NUMREACH       000002
+#define NUMKNOWN       000004
+#define NUMMARK                000010
+#define NUMSCAN                000020
+
+#define NNUMHASH       37
+extern num_p   numlookup();
+
+struct regs {
+       reg_p   r_next;
+       offset  r_par[4];
+};
+
+typedef struct proinf {
+       offset  localbytes;
+       line_p  lastline;
+       sym_p   symbol;
+       reg_p   freg;
+       num_p   numhash[NNUMHASH];
+} proinf;
+
+extern proinf curpro;
diff --git a/util/opt/putline.c b/util/opt/putline.c
new file mode 100644 (file)
index 0000000..627a999
--- /dev/null
@@ -0,0 +1,375 @@
+#include "param.h"
+#include "types.h"
+#include "assert.h"
+#include "../../h/em_spec.h"
+#include "../../h/em_pseu.h"
+#include "../../h/em_mnem.h"
+#include "../../h/em_flag.h"
+#include "alloc.h"
+#include "line.h"
+#include "lookup.h"
+#include "proinf.h"
+#include "optim.h"
+#include "ext.h"
+
+/*
+ * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ *
+ *          This product is part of the Amsterdam Compiler Kit.
+ *
+ * Permission to use, sell, duplicate or disclose this software must be
+ * obtained in writing. Requests for such permissions may be sent to
+ *
+ *      Dr. Andrew S. Tanenbaum
+ *      Wiskundig Seminarium
+ *      Vrije Universiteit
+ *      Postbox 7161
+ *      1007 MC Amsterdam
+ *      The Netherlands
+ *
+ * Author: Hans van Staveren
+ */
+
+#define outbyte(b) putc(b,outfile)
+
+putlines(lnp) register line_p lnp; {
+       register arg_p ap;
+       line_p temp;
+       register instr;
+       short curlin= -2;
+       short thislin;
+
+       while ( lnp != (line_p) 0) {
+               instr = lnp->l_instr&BMASK;
+               switch(lnp->l_optyp) {
+               case OPSYMBOL:
+                       if ((lnp->l_instr&BMASK) == ps_sym)
+                               outdef(lnp->l_a.la_sp);
+                       else
+                               outocc(lnp->l_a.la_sp);
+                       break;
+               case OPSVAL:
+                       outocc(lnp->l_a.la_sval.lasv_sp);
+                       break;
+#ifdef LONGOFF
+               case OPLVAL:
+                       outocc(lnp->l_a.la_lval.lalv_sp);
+                       break;
+#endif
+               case OPLIST:
+                       ap = lnp->l_a.la_arg;
+                       while (ap != (arg_p) 0) {
+                               switch(ap->a_typ) {
+                               case ARGSYM:
+                                       outocc(ap->a_a.a_sp);
+                                       break;
+                               case ARGVAL:
+                                       outocc(ap->a_a.a_val.av_sp);
+                                       break;
+                               }
+                               ap = ap->a_next;
+                       }
+                       break;
+               }
+
+               /*
+                * global symbols now taken care of
+                */
+
+
+               switch(instr) {
+               case ps_sym:
+                       break;
+               case op_lni:
+                       if (curlin != -2)
+                               curlin++;
+                       outinst(instr);
+                       break;
+               case op_lin:
+                       switch(lnp->l_optyp) {
+                       case OPNO:
+                       case OPOFFSET:
+                       case OPNUMLAB:
+                       case OPSYMBOL:
+                       case OPSVAL:
+                       case OPLVAL:
+                       case OPLIST:
+                               outinst(instr);
+                               goto processoperand;
+                       case OPSHORT:
+                               thislin = lnp->l_a.la_short;
+                               break;
+                       default:
+                               thislin = (lnp->l_optyp&BMASK)-Z_OPMINI;
+                               break;
+                       }
+                       if (thislin == curlin && !nflag) {
+                               temp = lnp->l_next;
+                               oldline(lnp);
+                               lnp = temp;
+                               OPTIM(O_LINGONE);
+                               continue;
+                       } else if (thislin == curlin+1 && !nflag) {
+                               instr = op_lni;
+                               outinst(instr);
+                               temp = lnp->l_next;
+                               oldline(lnp);
+                               OPTIM(O_LINLNI);
+                               lnp = newline(OPNO);
+                               lnp->l_next = temp;
+                               lnp->l_instr = instr;
+                       } else {
+                               outinst(instr);
+                       }
+                       curlin = thislin;
+                       break;
+               case op_lab:
+                       curlin = -2;
+                       break;
+               default:
+                       outinst(instr);
+               }
+processoperand:
+               switch(lnp->l_optyp) {
+               case OPNO:
+                       if ((em_flag[instr-sp_fmnem]&EM_PAR)!=PAR_NO)
+                               outbyte( (byte) sp_cend) ;
+                       break;
+               default:
+                       outint((lnp->l_optyp&BMASK)-Z_OPMINI);
+                       break;
+               case OPSHORT:
+                       outint(lnp->l_a.la_short);
+                       break;
+#ifdef LONGOFF
+               case OPOFFSET:
+                       outoff(lnp->l_a.la_offset);
+                       break;
+#endif
+               case OPNUMLAB:
+                       if (instr == op_lab)
+                               numlab(lnp->l_a.la_np->n_repl);
+                       else if (instr < sp_fpseu) /* plain instruction */
+                               outint((short) lnp->l_a.la_np->n_repl->n_number);
+                       else
+                               outnum(lnp->l_a.la_np->n_repl);
+                       break;
+               case OPSYMBOL:
+                       outsym(lnp->l_a.la_sp);
+                       break;
+               case OPSVAL:
+                       outbyte( (byte) sp_doff) ;
+                       outsym(lnp->l_a.la_sval.lasv_sp);
+                       outint(lnp->l_a.la_sval.lasv_short);
+                       break;
+#ifdef LONGOFF
+               case OPLVAL:
+                       outbyte( (byte) sp_doff) ;
+                       outsym(lnp->l_a.la_lval.lalv_sp);
+                       outoff(lnp->l_a.la_lval.lalv_offset);
+                       break;
+#endif
+               case OPLIST:
+                       putargs(lnp->l_a.la_arg);
+                       switch(instr) {
+                       case ps_con:
+                       case ps_rom:
+                       case ps_mes:
+                               outbyte( (byte) sp_cend) ;
+                       }
+               }
+               /*
+                * instruction is output now.
+                * remove its useless body
+                */
+
+               temp = lnp->l_next;
+               oldline(lnp);
+               lnp = temp;
+               if (ferror(outfile))
+                       error("write error");
+       }
+}
+
+putargs(ap) register arg_p ap; {
+
+       while (ap != (arg_p) 0) {
+               switch(ap->a_typ) {
+               default:
+                       assert(FALSE);
+               case ARGOFF:
+                       outoff(ap->a_a.a_offset);
+                       break;
+               case ARGNUM:
+                       outnum(ap->a_a.a_np->n_repl);
+                       break;
+               case ARGSYM:
+                       outsym(ap->a_a.a_sp);
+                       break;
+               case ARGVAL:
+                       outbyte( (byte) sp_doff) ;
+                       outsym(ap->a_a.a_val.av_sp);
+                       outoff(ap->a_a.a_val.av_offset);
+                       break;
+               case ARGSTR:
+                       outbyte( (byte) sp_scon) ;
+                       putstr(&ap->a_a.a_string);
+                       break;
+               case ARGICN:
+                       outbyte( (byte) sp_icon) ;
+                       goto casecon;
+               case ARGUCN:
+                       outbyte( (byte) sp_ucon) ;
+                       goto casecon;
+               case ARGFCN:
+                       outbyte( (byte) sp_fcon) ;
+               casecon:
+                       outint(ap->a_a.a_con.ac_length);
+                       putstr(&ap->a_a.a_con.ac_con);
+                       break;
+               }
+               ap = ap->a_next;
+       }
+}
+
+putstr(abp) register argb_p abp; {
+       register argb_p tbp;
+       register length;
+
+       length = 0;
+       tbp = abp;
+       while (tbp!= (argb_p) 0) {
+               length += tbp->ab_index;
+               tbp = tbp->ab_next;
+       }
+       outint(length);
+       while (abp != (argb_p) 0) {
+               for (length=0;length<abp->ab_index;length++)
+                       outbyte( (byte) abp->ab_contents[length] );
+               abp = abp->ab_next;
+       }
+}
+
+outdef(sp) register sym_p sp; {
+
+       /*
+        * The surrounding If statement is removed to be friendly
+        * to Backend writers having to deal with assemblers
+        * not following our conventions.
+       if ((sp->s_flags&SYMOUT)==0) {
+        */
+               sp->s_flags |= SYMOUT;
+               if (sp->s_flags&SYMGLOBAL) {
+                       outinst(sp->s_flags&SYMPRO ? ps_exp : ps_exa);
+                       outsym(sp);
+               }
+       /*
+       }
+        */
+}
+
+outocc(sp) register sym_p sp; {
+
+       if ((sp->s_flags&SYMOUT)==0) {
+               sp->s_flags |= SYMOUT;
+               if ((sp->s_flags&SYMGLOBAL)==0) {
+                       outinst(sp->s_flags&SYMPRO ? ps_inp : ps_ina);
+                       outsym(sp);
+               }
+       }
+}
+
+outpro() {
+
+       outdef(curpro.symbol);
+       outinst(ps_pro);
+       outsym(curpro.symbol);
+       outoff(curpro.localbytes);
+}
+
+outend() {
+
+       outinst(ps_end);
+       outoff(curpro.localbytes);
+}
+
+outinst(m) {
+
+       outbyte( (byte) m );
+}
+
+outoff(off) offset off; {
+
+#ifdef LONGOFF
+       if ((short) off == off)
+#endif
+               outint((short) off);
+#ifdef LONGOFF
+       else {
+               outbyte( (byte) sp_cst4) ;
+               outshort( (short) (off&0177777L) );
+               outshort( (short) (off>>16) );
+       }
+#endif
+}
+
+outint(i) short i; {
+
+       if (i>= -sp_zcst0 && i< sp_ncst0-sp_zcst0)
+               outbyte( (byte) (i+sp_zcst0+sp_fcst0) );
+       else {
+               outbyte( (byte) sp_cst2) ;
+               outshort(i);
+       }
+}
+
+outshort(i) short i; {
+
+       outbyte( (byte) (i&BMASK) );
+       outbyte( (byte) (i>>8) );
+}
+
+numlab(np) register num_p np; {
+
+       if (np->n_number < sp_nilb0)
+               outbyte( (byte) (np->n_number + sp_filb0) );
+       else
+               outnum(np);
+}
+
+outnum(np) register num_p np; {
+
+       if(np->n_number<256) {
+               outbyte( (byte) sp_ilb1) ;
+               outbyte( (byte) (np->n_number) );
+       } else {
+               outbyte( (byte) sp_ilb2) ;
+               outshort((short) np->n_number);
+       }
+}
+
+outsym(sp) register sym_p sp; {
+       register byte *p;
+       register unsigned num;
+
+       if (sp->s_name[0] == '.') {
+               num = atoi(&sp->s_name[1]);
+               if (num < 256) {
+                       outbyte( (byte) sp_dlb1) ;
+                       outbyte( (byte) (num) );
+               } else {
+                       outbyte( (byte) sp_dlb2) ;
+                       outshort((short) num);
+               }
+       } else {
+               p= sp->s_name;
+               while (*p && p < &sp->s_name[IDL])
+                       p++;
+               num = p - sp->s_name;
+               outbyte( (byte) (sp->s_flags&SYMPRO ? sp_pnam : sp_dnam) );
+               outint((short) num);
+               p = sp->s_name;
+               while (num--)
+                       outbyte( (byte) *p++ );
+       }
+}
diff --git a/util/opt/reg.c b/util/opt/reg.c
new file mode 100644 (file)
index 0000000..209e1d6
--- /dev/null
@@ -0,0 +1,94 @@
+#include "assert.h"
+#include "param.h"
+#include "types.h"
+#include "line.h"
+#include "proinf.h"
+#include "alloc.h"
+#include "../../h/em_spec.h"
+#include "../../h/em_pseu.h"
+#include "../../h/em_mes.h"
+#include "ext.h"
+
+/*
+ * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ *
+ *          This product is part of the Amsterdam Compiler Kit.
+ *
+ * Permission to use, sell, duplicate or disclose this software must be
+ * obtained in writing. Requests for such permissions may be sent to
+ *
+ *      Dr. Andrew S. Tanenbaum
+ *      Wiskundig Seminarium
+ *      Vrije Universiteit
+ *      Postbox 7161
+ *      1007 MC Amsterdam
+ *      The Netherlands
+ *
+ * Author: Hans van Staveren
+ */
+
+regvar(ap) register arg_p ap; {
+       register reg_p rp;
+       register i;
+
+       rp = newreg();
+       i=0;
+       while (ap!=(arg_p)0 && ap->a_typ==ARGOFF && i<4) {
+               rp->r_par[i++]=ap->a_a.a_offset;
+               ap=ap->a_next;
+       }
+       /*
+        * Omit incomplete messages
+        */
+       switch(i) {
+       default:assert(FALSE);
+       case 0:
+       case 1:
+       case 2: oldreg(rp); return;
+       case 3: rp->r_par[3]= (offset) 0; break;
+       case 4: break;
+       }
+       rp->r_next = curpro.freg;
+       curpro.freg = rp;
+}
+
+inreg(off) offset off; {
+       register reg_p rp;
+
+       for (rp=curpro.freg; rp != (reg_p) 0; rp=rp->r_next)
+               if( rp->r_par[0] == off)
+                       return(TRUE);
+       return(FALSE);
+}
+
+outregs() {
+       register reg_p rp,tp;
+       register i;
+
+       for(rp=curpro.freg; rp != (reg_p) 0; rp = tp) {
+               tp = rp->r_next;
+               if (rp->r_par[3] != 0) {
+                       outinst(ps_mes);
+                       outoff((offset)ms_reg);
+                       for(i=0;i<4;i++)
+                               outoff(rp->r_par[i]);
+                       outinst(sp_cend);
+               }
+               oldreg(rp);
+       }
+       /* Now an empty mes 3 to signal the end. */
+       outinst(ps_mes);
+       outoff((offset)ms_reg);
+       outinst(sp_cend);
+       curpro.freg = (reg_p) 0;
+}
+
+incregusage(off) offset off; {
+       register reg_p rp;
+
+       for(rp=curpro.freg; rp != (reg_p) 0; rp=rp->r_next)
+               if (rp->r_par[0]==off) {
+                       rp->r_par[3]++;
+                       return;
+               }
+}
diff --git a/util/opt/scan.l b/util/opt/scan.l
new file mode 100644 (file)
index 0000000..f42b710
--- /dev/null
@@ -0,0 +1,71 @@
+%{
+/*
+ * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ *
+ *          This product is part of the Amsterdam Compiler Kit.
+ *
+ * Permission to use, sell, duplicate or disclose this software must be
+ * obtained in writing. Requests for such permissions may be sent to
+ *
+ *      Dr. Andrew S. Tanenbaum
+ *      Wiskundig Seminarium
+ *      Vrije Universiteit
+ *      Postbox 7161
+ *      1007 MC Amsterdam
+ *      The Netherlands
+ *
+ * Author: Hans van Staveren
+ */
+
+extern long atol();
+%}
+%%
+notreg         return(NOTREG);
+sfit           return(SFIT);
+ufit           return(UFIT);
+rotate         return(ROTATE);
+p              return(PSIZE);
+w              return(WSIZE);
+defined                return(DEFINED);
+samesign       return(SAMESIGN);
+rom            return(ROM);
+[a-zA-Z]{3}    {
+               int m;
+               m = mlookup(yytext);
+               if (m==0) {
+                       REJECT;
+               } else {
+                       yylval.y_int = m;
+                       return(MNEM);
+               }
+               }
+"&&"           return(AND2);
+"||"           return(OR2);
+"&"            return(AND1);
+"|"            return(OR1);
+"^"            return(XOR1);
+"+"            return(ARPLUS);
+"-"            return(ARMINUS);
+"*"            return(ARTIMES);
+"/"            return(ARDIVIDE);
+"%"            return(ARMOD);
+"=="           return(CMPEQ);
+"!="           return(CMPNE);
+"<"            return(CMPLT);
+"<="           return(CMPLE);
+">"            return(CMPGT);
+">="           return(CMPGE);
+"!"            return(NOT);
+"~"            return(COMP);
+"<<"           return(LSHIFT);
+">>"           return(RSHIFT);
+[0-9]+         { long l= atol(yytext);
+                 if (l>32767) yyerror("Number too big");
+                 yylval.y_int= (int) l;
+                 return(NUMBER);
+               }
+[ \t]          ;
+.              return(yytext[0]);
+\n             { lino++; return(yytext[0]); }
+:[ \t]*\n[ \t]+        { lino++; return(':'); }
+^"# "[0-9]+.*\n        { lino=atoi(yytext+2); }
diff --git a/util/opt/special.c b/util/opt/special.c
new file mode 100644 (file)
index 0000000..ffa5fb4
--- /dev/null
@@ -0,0 +1,29 @@
+#include "param.h"
+#include "types.h"
+
+/*
+ * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ *
+ *          This product is part of the Amsterdam Compiler Kit.
+ *
+ * Permission to use, sell, duplicate or disclose this software must be
+ * obtained in writing. Requests for such permissions may be sent to
+ *
+ *      Dr. Andrew S. Tanenbaum
+ *      Wiskundig Seminarium
+ *      Vrije Universiteit
+ *      Postbox 7161
+ *      1007 MC Amsterdam
+ *      The Netherlands
+ *
+ * Author: Hans van Staveren
+ */
+
+bool special(lpp,bp,patlen)
+line_p *lpp;
+byte *bp;
+int patlen;
+{
+
+       return(FALSE);
+}
diff --git a/util/opt/testopt b/util/opt/testopt
new file mode 100755 (executable)
index 0000000..4fb8f3e
--- /dev/null
@@ -0,0 +1,7 @@
+while true
+do
+  (echo ' mes 2,2,2
+ pro $foo,0';cat;echo ' end') >t.e
+  npc -2=${1-opt} -O -2 t.e;npc -D t.m
+  cat t.e
+done
diff --git a/util/opt/types.h b/util/opt/types.h
new file mode 100644 (file)
index 0000000..de4df5b
--- /dev/null
@@ -0,0 +1,19 @@
+typedef char byte;
+typedef char bool;
+typedef struct line line_t;
+typedef struct line *line_p;
+typedef struct sym sym_t;
+typedef struct sym *sym_p;
+typedef struct num num_t;
+typedef struct num *num_p;
+typedef struct arg arg_t;
+typedef struct arg *arg_p;
+typedef struct argbytes argb_t;
+typedef struct argbytes *argb_p;
+typedef struct regs reg_t;
+typedef struct regs *reg_p;
+#ifdef LONGOFF
+typedef long offset;
+#else
+typedef short offset;
+#endif
diff --git a/util/opt/util.c b/util/opt/util.c
new file mode 100644 (file)
index 0000000..b44f20a
--- /dev/null
@@ -0,0 +1,58 @@
+#include <stdio.h>
+#include "param.h"
+#include "types.h"
+#include "assert.h"
+#include "lookup.h"
+#include "proinf.h"
+#include "optim.h"
+#include "ext.h"
+
+/*
+ * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ *
+ *          This product is part of the Amsterdam Compiler Kit.
+ *
+ * Permission to use, sell, duplicate or disclose this software must be
+ * obtained in writing. Requests for such permissions may be sent to
+ *
+ *      Dr. Andrew S. Tanenbaum
+ *      Wiskundig Seminarium
+ *      Vrije Universiteit
+ *      Postbox 7161
+ *      1007 MC Amsterdam
+ *      The Netherlands
+ *
+ * Author: Hans van Staveren
+ */
+
+
+/* VARARGS1 */
+error(s,a) char *s,*a; {
+
+       fprintf(stderr,"%s: error on line %u",progname,linecount);
+       if (prodepth != 0)
+               fprintf(stderr,"(%.*s)",IDL,curpro.symbol->s_name);
+       fprintf(stderr,": ");
+       fprintf(stderr,s,a);
+       fprintf(stderr,"\n");
+       abort();
+       exit(-1);
+}
+
+#ifndef NDEBUG
+badassertion(file,line) char *file; unsigned line; {
+
+       fprintf(stderr,"assertion failed file %s, line %u\n",file,line);
+       error("assertion");
+}
+#endif
+
+#ifdef DIAGOPT
+optim(n) {
+
+       fprintf(stderr,"Made optimization %d",n);
+       if (inpro)
+               fprintf(stderr," (%.*s)",IDL,curpro.symbol->s_name);
+       fprintf(stderr,"\n");
+}
+#endif
diff --git a/util/opt/var.c b/util/opt/var.c
new file mode 100644 (file)
index 0000000..d7f9a33
--- /dev/null
@@ -0,0 +1,36 @@
+#include <stdio.h>
+#include "param.h"
+#include "types.h"
+#include "lookup.h"
+#include "proinf.h"
+
+/*
+ * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ *
+ *          This product is part of the Amsterdam Compiler Kit.
+ *
+ * Permission to use, sell, duplicate or disclose this software must be
+ * obtained in writing. Requests for such permissions may be sent to
+ *
+ *      Dr. Andrew S. Tanenbaum
+ *      Wiskundig Seminarium
+ *      Vrije Universiteit
+ *      Postbox 7161
+ *      1007 MC Amsterdam
+ *      The Netherlands
+ *
+ * Author: Hans van Staveren
+ */
+
+unsigned linecount = 0;                /* "line"number for errormessages */
+int    prodepth  = 0;          /* Level of nesting */
+bool   Lflag     = 0;          /* make library module */
+bool   nflag     = 0;          /* do not optimize */
+line_p instrs,pseudos;         /* pointers to chains */
+sym_p  symhash[NSYMHASH];      /* array of pointers to chains */
+FILE   *outfile;
+char   template[] = "/usr/tmp/emoptXXXXXX";
+offset wordsize = 0;
+offset pointersize = 0;
+char   *progname;
+proinf curpro;                 /* collected information about current pro */