BSD 4_3 release
[unix-history] / usr / src / lib / c2 / c20.c
index bcb18dc..cd6cf44 100644 (file)
@@ -1,6 +1,7 @@
-#
-static char sccsid[] = "@(#)c20.c 4.5 7/5/81";
-/* char C20[] = {"@(#)c20.c 1.35 80/08/26 14:13:40"}; /* sccs ident */
+#ifndef lint
+static char sccsid[] = "@(#)c20.c      4.10 (Berkeley) 8/22/85";
+#endif
+
 /*
  *      C object code improver
  */
 /*
  *      C object code improver
  */
@@ -8,34 +9,59 @@ static        char sccsid[] = "@(#)c20.c 4.5 7/5/81";
 #include "c2.h"
 #include <stdio.h>
 #include <ctype.h>
 #include "c2.h"
 #include <stdio.h>
 #include <ctype.h>
+#include <sys/types.h>
+
+char *malloc();
+char firstr[sizeof (char *)];
+char *currentb;
+char *newa;
+char *lasta;
+char *lastr;
+int ncore;
 
 
-char _sibuf[BUFSIZ], _sobuf[BUFSIZ];
 int ioflag;
 int ioflag;
+int fflag;
+
 long   isn     = 2000000;
 long   isn     = 2000000;
+
 struct optab *oplook();
 struct optab *getline();
 struct optab *oplook();
 struct optab *getline();
+
 long lgensym[10] =
   {100000L,200000L,300000L,400000L,500000L,600000L,700000L,800000L,900000L,1000000L};
 
 long lgensym[10] =
   {100000L,200000L,300000L,400000L,500000L,600000L,700000L,800000L,900000L,1000000L};
 
-struct node *
-alloc(an)
+#define ALLOCSIZE      4096
+
+/*
+ * Cheapo space allocator.  Works much like the old one but uses malloc()
+ * and doesn't clash with stdio.  Assumes that no one requests more than
+ * ALLOCSIZE bytes at a time.
+ */
+char *
+xalloc(n)
+int n;
 {
 {
-       register int n;
-       register char *p;
-
-       n = an;
-       n+=sizeof(char *)-1;
-       n &= ~(sizeof(char *)-1);
-       if (lasta+n >= lastr) {
-               if (sbrk(2000) == -1) {
+       register char *nextb = * (char **) currentb;
+
+       if (n == 0) {   /* Free everything */
+               currentb = firstr;
+               nextb = * (char **) currentb;
+       }
+       if (nextb == NULL) {
+               if ((nextb = malloc(ALLOCSIZE)) == NULL) {
                        fprintf(stderr, "Optimizer: out of space\n");
                        exit(1);
                }
                        fprintf(stderr, "Optimizer: out of space\n");
                        exit(1);
                }
-               lastr += 2000;
+               ncore += (ALLOCSIZE/1024);
+               * (char **) currentb = nextb;
+               * (char **) nextb = NULL;
        }
        }
-       p = lasta;
-       lasta += n;
-       return(p);
+       lasta = nextb + sizeof nextb;
+       lastr = nextb + ALLOCSIZE;
+       currentb = nextb;
+       newa = lasta;
+       lasta += XALIGN(n);
+       return(newa);
 }
 
 main(argc, argv)
 }
 
 main(argc, argv)
@@ -48,25 +74,25 @@ char **argv;
        while (argc>0) {/* get flags */
                if (**argv=='+') debug++;
                else if (**argv=='-') {
        while (argc>0) {/* get flags */
                if (**argv=='+') debug++;
                else if (**argv=='-') {
-                       if ((*argv)[1]=='i') ioflag++; else nflag++;
+                       if ((*argv)[1]=='i') ioflag++;
+                       else if ((*argv)[1]=='f') fflag++;
+                       else nflag++;
                } else if (infound==0) {
                        if (freopen(*argv, "r", stdin) ==NULL) {
                                fprintf(stderr,"C2: can't find %s\n", *argv);
                                exit(1);
                        }
                } else if (infound==0) {
                        if (freopen(*argv, "r", stdin) ==NULL) {
                                fprintf(stderr,"C2: can't find %s\n", *argv);
                                exit(1);
                        }
-                       setbuf(stdin,_sibuf); ++infound;
+                       ++infound;
                } else if (freopen(*argv, "w", stdout) ==NULL) {
                        fprintf(stderr,"C2: can't create %s\n", *argv);
                        exit(1);
                }
                } else if (freopen(*argv, "w", stdout) ==NULL) {
                        fprintf(stderr,"C2: can't create %s\n", *argv);
                        exit(1);
                }
-               setbuf(stdout,_sobuf);
                argc--; argv++;
        }
                argc--; argv++;
        }
-       lasta = lastr = sbrk(2);
        opsetup();
        opsetup();
-       lasta = firstr = lastr = alloc(0);
        maxiter = 0;
        do {
        maxiter = 0;
        do {
+               (void) xalloc(0);
                isend = input();
                niter = 0;
                bmove();
                isend = input();
                niter = 0;
                bmove();
@@ -84,7 +110,6 @@ char **argv;
                output();
                if (niter > maxiter)
                        maxiter = niter;
                output();
                if (niter > maxiter)
                        maxiter = niter;
-               lasta = firstr;
        } while (isend);
        if (nflag) {
                fprintf(stderr,"%d iterations\n", maxiter);
        } while (isend);
        if (nflag) {
                fprintf(stderr,"%d iterations\n", maxiter);
@@ -105,7 +130,7 @@ char **argv;
                fprintf(stderr,"%d redundant tst's\n", nrtst);
                fprintf(stderr,"%d jump on bit\n", nbj);
                fprintf(stderr,"%d field operations\n", nfield);
                fprintf(stderr,"%d redundant tst's\n", nrtst);
                fprintf(stderr,"%d jump on bit\n", nbj);
                fprintf(stderr,"%d field operations\n", nfield);
-               fprintf(stderr,"%dK core\n", ((unsigned)lastr+01777) >> 10);
+               fprintf(stderr,"%dK core\n", ncore);
        }
        putc('\n',stdout);
        fflush(stdout); exit(0);
        }
        putc('\n',stdout);
        fflush(stdout); exit(0);
@@ -114,15 +139,15 @@ char **argv;
 input()
 {
        register struct node *p, *lastp;
 input()
 {
        register struct node *p, *lastp;
-       struct optab *op; register char *cp1;
+       struct optab *opp; register char *cp1;
        static struct optab F77JSW = {".long", T(JSW,1)};
 
        lastp = &first;
        for (;;) {
          top:
        static struct optab F77JSW = {".long", T(JSW,1)};
 
        lastp = &first;
        for (;;) {
          top:
-               op = getline();
-               if (debug && op==0) fprintf(stderr,"? %s\n",line);
-               switch (op->opcode&0377) {
+               opp = getline();
+               if (debug && opp==0) fprintf(stderr,"? %s\n",line);
+               switch (opp->opcode&0377) {
        
                case LABEL:
                        p = alloc(sizeof first);
        
                case LABEL:
                        p = alloc(sizeof first);
@@ -140,15 +165,15 @@ input()
        
                case LGEN:
                        if (*curlp!='L' && !locuse(curlp)) goto std;
        
                case LGEN:
                        if (*curlp!='L' && !locuse(curlp)) goto std;
-                       op= &F77JSW;
+                       opp= &F77JSW;
                case JBR:
                case JBR:
-                       if (op->opcode==T(JBR,RET) || op->opcode==T(JBR,RSB)) goto std;
+                       if (opp->opcode==T(JBR,RET) || opp->opcode==T(JBR,RSB)) goto std;
                case CBR:
                case JMP:
                case JSW:
                case SOBGEQ: case SOBGTR: case AOBLEQ: case AOBLSS: case ACB:
                        p = alloc(sizeof first);
                case CBR:
                case JMP:
                case JSW:
                case SOBGEQ: case SOBGTR: case AOBLEQ: case AOBLSS: case ACB:
                        p = alloc(sizeof first);
-                       p->combop = op->opcode; p->code=0; cp1=curlp;
+                       p->combop = opp->opcode; p->code=0; cp1=curlp;
                        if ((!isdigit(*cp1) || 0==(p->labno=locuse(cp1))) &&
                          (*cp1!='L' || 0==(p->labno = getnum(cp1+1)))) {/* jbs, etc.? */
                                while (*cp1++); while (*--cp1!=',' && cp1!=curlp);
                        if ((!isdigit(*cp1) || 0==(p->labno=locuse(cp1))) &&
                          (*cp1!='L' || 0==(p->labno = getnum(cp1+1)))) {/* jbs, etc.? */
                                while (*cp1++); while (*--cp1!=',' && cp1!=curlp);
@@ -164,7 +189,7 @@ input()
 
                case MOVA:
                        p=alloc(sizeof first);
 
                case MOVA:
                        p=alloc(sizeof first);
-                       p->combop=op->opcode; p->code=0; cp1=curlp+1;
+                       p->combop=opp->opcode; p->code=0; cp1=curlp+1;
                        if (cp1[-1]=='L' || isdigit(cp1[-1])) {
                                while (*cp1++!=','); *--cp1=0;
                                if (0!=(p->labno=locuse(curlp)) ||
                        if (cp1[-1]=='L' || isdigit(cp1[-1])) {
                                while (*cp1++!=','); *--cp1=0;
                                if (0!=(p->labno=locuse(curlp)) ||
@@ -181,9 +206,9 @@ input()
                case BSS:
                case DATA:
                        for (;;) {
                case BSS:
                case DATA:
                        for (;;) {
-                               printf("%s%c",line,(op->opcode==LABEL ? ':' : '\n'));
-                               if (op->opcode==TEXT) goto top;
-                               if (END==(op=getline())->opcode) {/* dangling .data is bad for you */
+                               printf("%s%c",line,(opp->opcode==LABEL ? ':' : '\n'));
+                               if (opp->opcode==TEXT) goto top;
+                               if (END==(opp=getline())->opcode) {/* dangling .data is bad for you */
                                        printf(".text\n");
                                        break;
                                }
                                        printf(".text\n");
                                        break;
                                }
@@ -192,7 +217,7 @@ input()
                std:
                default:
                        p = alloc(sizeof first);
                std:
                default:
                        p = alloc(sizeof first);
-                       p->combop = op->opcode;
+                       p->combop = opp->opcode;
                        p->labno = 0;
                        p->code = copy(curlp);
                        break;
                        p->labno = 0;
                        p->code = copy(curlp);
                        break;
@@ -200,16 +225,22 @@ input()
                }
                p->forw = 0;
                p->back = lastp;
                }
                p->forw = 0;
                p->back = lastp;
-               p->pop = op;
+               p->pop = opp;
                lastp->forw = p;
                lastp = p;
                p->ref = 0;
                if (p->op==CASE) {
                        char *lp; int ncase;
                        lp=curlp; while (*lp++); while (*--lp!='$'); ncase=getnum(lp+1);
                lastp->forw = p;
                lastp = p;
                p->ref = 0;
                if (p->op==CASE) {
                        char *lp; int ncase;
                        lp=curlp; while (*lp++); while (*--lp!='$'); ncase=getnum(lp+1);
-                       if (LABEL!=(getline())->opcode) abort(-2);
+                       if (LABEL!=(getline())->opcode) {
+                               fprintf(stderr, "c2: garbled 'case' instruction\n");
+                               exit(-2);
+                       }
                        do {
                        do {
-                               if (WGEN!=(getline())->opcode) abort(-3);
+                               if (WGEN!=(getline())->opcode) {
+                                       fprintf(stderr, "c2: garbled 'case' instruction\n");
+                                       exit(-3);
+                               }
                                p = alloc(sizeof first); p->combop = JSW; p->code = 0;
                                lp=curlp; while(*lp++!='-'); *--lp=0; p->labno=getnum(curlp+1);
                                if (isn<=p->labno) isn=1+p->labno;
                                p = alloc(sizeof first); p->combop = JSW; p->code = 0;
                                lp=curlp; while(*lp++!='-'); *--lp=0; p->labno=getnum(curlp+1);
                                if (isn<=p->labno) isn=1+p->labno;
@@ -217,9 +248,9 @@ input()
                                p->ref = 0; p->pop=0;
                        } while (--ncase>=0);
                }
                                p->ref = 0; p->pop=0;
                        } while (--ncase>=0);
                }
-               if (op->opcode==EROU)
+               if (opp->opcode==EROU)
                        return(1);
                        return(1);
-               if (op->opcode==END)
+               if (opp->opcode==END)
                        return(0);
        }
 }
                        return(0);
        }
 }
@@ -268,8 +299,6 @@ register char *p;
 locuse(p)
 register char *p;
 {
 locuse(p)
 register char *p;
 {
-       register c; int neg; register long n;
-
        if (!isdigit(p[0]) || p[1] != 'f' && p[1] != 'b' || p[2]) return(0);
        return (lgensym[p[0] - '0'] - (p[1] == 'b'));
 }
        if (!isdigit(p[0]) || p[1] != 'f' && p[1] != 'b' || p[2]) return(0);
        return (lgensym[p[0] - '0'] - (p[1] == 'b'));
 }
@@ -331,6 +360,26 @@ output()
                if (casebas==0) printf("L%d:\n",casebas=isn++);
                printf(".word   L%d-L%d\n", t->labno, casebas);
                continue;
                if (casebas==0) printf("L%d:\n",casebas=isn++);
                printf(".word   L%d-L%d\n", t->labno, casebas);
                continue;
+       case MOV:
+               if (!fflag) goto std;
+               if (t->forw) if(t->forw->op == CBR) goto std;
+               if (*t->code == '$') goto std;
+               if (t->subop == FFLOAT)
+                       {
+                       printf("movl\t%s\n", t->code);
+                       continue;
+                       }
+               if (t->subop == DFLOAT || t->subop == GFLOAT)
+                       {
+                       printf("movq\t%s\n", t->code);
+                       continue;
+                       }
+               if (t->subop == HFLOAT)
+                       {
+                       printf("movo\t%s\n", t->code);
+                       continue;
+                       }
+               goto std;
 
        }
 }
 
        }
 }
@@ -357,7 +406,7 @@ char *ap;
                while (*p++)
                        n++;
        }
                while (*p++)
                        n++;
        }
-       onp = np = alloc(n);
+       onp = np = (char *) alloc(n);
        p = ap;
        while (*np++ = *p++);
        if (na>1) {
        p = ap;
        while (*np++ = *p++);
        if (na>1) {
@@ -376,7 +425,7 @@ opsetup()
        register struct optab *optp, **ophp;
        register int i,t;
 
        register struct optab *optp, **ophp;
        register int i,t;
 
-       for(i=NREG+5;--i>=0;) regs[i]=alloc(C2_ASIZE);
+       for(i=NREG+5;--i>=0;) regs[i] = malloc(C2_ASIZE);
        for (optp = optab; optp->opstring[0]; optp++) {
                t=7; i=0; while (--t>=0) i+= i+optp->opstring[t];
                ophp = &ophash[i % OPHS];
        for (optp = optab; optp->opstring[0]; optp++) {
                t=7; i=0; while (--t>=0) i+= i+optp->opstring[t];
                ophp = &ophash[i % OPHS];
@@ -384,7 +433,7 @@ opsetup()
 /*                     fprintf(stderr,"\ncollision: %d %s %s",
 /*                             ophp-1-ophash,optp->opstring,(*(ophp-1))->opstring);
 */
 /*                     fprintf(stderr,"\ncollision: %d %s %s",
 /*                             ophp-1-ophash,optp->opstring,(*(ophp-1))->opstring);
 */
-                       if (ophp > &ophash[OPHS])
+                       if (ophp >= &ophash[OPHS])
                                ophp = ophash;
                }
                *--ophp = optp;
                                ophp = ophash;
                }
                *--ophp = optp;
@@ -413,7 +462,7 @@ oplook()
 
 refcount()
 {
 
 refcount()
 {
-       register struct node *p, *lp;
+       register struct node *p, *lp, *tp;
        struct node *labhash[LABHS];
        register struct node **hp;
 
        struct node *labhash[LABHS];
        register struct node **hp;
 
@@ -436,10 +485,10 @@ refcount()
                                        break;
                        }
                        if (lp) {
                                        break;
                        }
                        if (lp) {
-                               hp = nonlab(lp)->back;
-                               if (hp!=lp) {
-                                       p->labno = hp->labno;
-                                       lp = hp;
+                               tp = nonlab(lp)->back;
+                               if (tp!=lp) {
+                                       p->labno = tp->labno;
+                                       lp = tp;
                                }
                                p->ref = lp;
                                lp->refc++;
                                }
                                p->ref = lp;
                                lp->refc++;
@@ -530,42 +579,39 @@ xjump(p1)
 register struct node *p1;
 {
        register struct node *p2, *p3;
 register struct node *p1;
 {
        register struct node *p2, *p3;
-       int nxj;
 
 
-       nxj = 0;
        if ((p2 = p1->ref)==0)
        if ((p2 = p1->ref)==0)
-               return(0);
+               return;
        for (;;) {
                while ((p1 = p1->back) && p1->op==LABEL);
                while ((p2 = p2->back) && p2->op==LABEL);
                if (!equop(p1, p2) || p1==p2)
        for (;;) {
                while ((p1 = p1->back) && p1->op==LABEL);
                while ((p2 = p2->back) && p2->op==LABEL);
                if (!equop(p1, p2) || p1==p2)
-                       return(nxj);
+                       return;
                p3 = insertl(p2);
                p1->combop = JBR;
                p1->pop=0;
                p1->ref = p3;
                p1->labno = p3->labno;
                p1->code = 0;
                p3 = insertl(p2);
                p1->combop = JBR;
                p1->pop=0;
                p1->ref = p3;
                p1->labno = p3->labno;
                p1->code = 0;
-               nxj++;
                nxjump++;
                nchange++;
        }
 }
 
 struct node *
                nxjump++;
                nchange++;
        }
 }
 
 struct node *
-insertl(op)
-register struct node *op;
+insertl(np)
+register struct node *np;
 {
        register struct node *lp;
 
 {
        register struct node *lp;
 
-       if (op->op == LABEL) {
-               op->refc++;
-               return(op);
+       if (np->op == LABEL) {
+               np->refc++;
+               return(np);
        }
        }
-       if (op->back->op == LABEL) {
-               op = op->back;
-               op->refc++;
-               return(op);
+       if (np->back->op == LABEL) {
+               np = np->back;
+               np->refc++;
+               return(np);
        }
        lp = alloc(sizeof first);
        lp->combop = LABEL;
        }
        lp = alloc(sizeof first);
        lp->combop = LABEL;
@@ -573,10 +619,10 @@ register struct node *op;
        lp->ref = 0;
        lp->code = 0;
        lp->refc = 1;
        lp->ref = 0;
        lp->code = 0;
        lp->refc = 1;
-       lp->back = op->back;
-       lp->forw = op;
-       op->back->forw = lp;
-       op->back = lp;
+       lp->back = np->back;
+       lp->forw = np;
+       np->back->forw = lp;
+       np->back = lp;
        return(lp);
 }
 
        return(lp);
 }