From 21e15965cce430e194da4089e7c42b5abf7d6574 Mon Sep 17 00:00:00 2001 From: carl Date: Sat, 11 May 2019 01:17:40 +0800 Subject: [PATCH] ANSI C conversion and add procedure declarations. --- util/opt/alloc.c | 266 ++++++---- util/opt/alloc.h | 16 +- util/opt/backward.c | 285 +++++----- util/opt/build.lua | 6 +- util/opt/cleanup.c | 69 ++- util/opt/em_opt.6 | 2 +- util/opt/ext.h | 2 +- util/opt/flow.c | 138 ++--- util/opt/getline.c | 867 ++++++++++++++++-------------- util/opt/getline.h | 17 + util/opt/lookup.c | 82 +-- util/opt/lookup.h | 16 +- util/opt/main.c | 127 +++-- util/opt/mktab.y | 89 ++-- util/opt/patterns | 2 +- util/opt/peephole.c | 1217 ++++++++++++++++++++++++------------------- util/opt/process.c | 277 +++++----- util/opt/putline.c | 508 +++++++++--------- util/opt/putline.h | 25 + util/opt/reg.c | 114 ++-- util/opt/reg.h | 17 + util/opt/scan.l | 6 +- util/opt/special.c | 15 +- util/opt/tes.c | 214 +++++--- util/opt/tes.h | 11 +- util/opt/types.h | 7 +- util/opt/util.c | 51 +- util/opt/util.h | 15 + util/opt/var.c | 19 +- 29 files changed, 2510 insertions(+), 1970 deletions(-) create mode 100644 util/opt/getline.h create mode 100644 util/opt/putline.h create mode 100644 util/opt/reg.h create mode 100644 util/opt/util.h diff --git a/util/opt/alloc.c b/util/opt/alloc.c index cc6dc6607..9bdb77caa 100644 --- a/util/opt/alloc.c +++ b/util/opt/alloc.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include #include #include @@ -9,20 +11,14 @@ static char rcsid[] = "$Id$"; #include "types.h" #include "tes.h" #include "alloc.h" +#include "util.h" #include "line.h" #include "lookup.h" #include "proinf.h" -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ - #ifdef USEMALLOC -short * myalloc(); +short *myalloc(register unsigned int); #define newcore(size) myalloc(size) #define oldcore(p,size) free(p) @@ -94,101 +90,111 @@ int asizetab[] = { * PART 1 */ -line_p newline(optyp) int optyp; { +line_p newline(int optyp) +{ register line_p lnp; - register kind=optyp; + register int kind = optyp; - if (kind>OPMINI) + if (kind > OPMINI) kind = OPMINI; lnp = (line_p) newcore(lsizetab[kind]); lnp->l_optyp = optyp; - return(lnp); + return (lnp); } -oldline(lnp) register line_p lnp; { - register kind=lnp->l_optyp&BMASK; +void oldline(register line_p lnp) +{ + register int kind = lnp->l_optyp & BMASK; - if (kind>OPMINI) + if (kind > OPMINI) kind = OPMINI; if (kind == OPLIST) oldargs(lnp->l_a.la_arg); - oldcore((short *) lnp,lsizetab[kind]); + oldcore((short * ) lnp, lsizetab[kind]); } -arg_p newarg(kind) int kind; { +arg_p newarg(int kind) +{ register arg_p ap; ap = (arg_p) newcore(asizetab[kind]); ap->a_typ = kind; - return(ap); + return (ap); } -oldargs(ap) register arg_p ap; { - register arg_p next; +void oldargs(register arg_p ap) +{ + register arg_p next; - while (ap != (arg_p) 0) { + 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; + 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]); + oldcore((short * ) ap, asizetab[ap->a_typ]); ap = next; } } -oldargb(abp) register argb_p abp; { +void oldargb(register argb_p abp) +{ register argb_p next; - while (abp != (argb_p) 0) { + while (abp != (argb_p) 0) + { next = abp->ab_next; - oldcore((short *) abp,sizeof (argb_t)); + oldcore((short * ) abp, sizeof (argb_t)); abp = next; } } -reg_p newreg() { - - return((reg_p) newcore(sizeof(reg_t))); +reg_p newreg(void) +{ + return ((reg_p) newcore(sizeof(reg_t))); } -oldreg(rp) reg_p rp; { +void oldreg(reg_p rp) +{ - oldcore((short *) rp,sizeof(reg_t)); + oldcore((short * ) rp, sizeof(reg_t)); } -num_p newnum() { - - return((num_p) newcore(sizeof(num_t))); +num_p newnum(void) +{ + return ((num_p) newcore(sizeof(num_t))); } -oldnum(lp) num_p lp; { - - oldcore((short *) lp,sizeof(num_t)); +void oldnum(num_p lp) +{ + oldcore((short * ) lp, sizeof(num_t)); } -offset *newrom() { - - return((offset *) newcore(MAXROM*sizeof(offset))); +offset *newrom(void) +{ + return ((offset *) newcore(MAXROM*sizeof(offset))); } -sym_p newsym(len) int len; { +sym_p newsym(int len) +{ /* * sym_t includes a 2 character s_name at the end * extend this structure with len-2 characters */ - return((sym_p) newcore(sizeof(sym_t) - 2 + len)); + return ((sym_p) newcore(sizeof(sym_t) - 2 + len)); } -argb_p newargb() { - - return((argb_p) newcore(sizeof(argb_t))); +argb_p newargb(void) +{ + return ((argb_p) newcore(sizeof(argb_t))); } #ifndef USEMALLOC @@ -201,68 +207,80 @@ argb_p newargb() { short *freelist[MAXSHORT]; -typedef struct coreblock { +typedef struct coreblock +{ struct coreblock *co_next; short co_size; -} core_t,*core_p; +}core_t,*core_p; #define SINC (sizeof(core_t)/sizeof(short)) #ifdef COREDEBUG -coreverbose() { +coreverbose() +{ register size; register short *p; register sum; sum = 0; for(size=1;sizeco_next; tp->co_size = size; - if (corelist==0 || tpco_next = corelist; corelist = tp; - } else { + } + else + { for(cl=corelist;cl->co_next != 0 && tp>cl->co_next; - cl = cl->co_next) - ; + cl = cl->co_next) + ; tp->co_next = cl->co_next; cl->co_next = tp; } } } - while (corelist != 0) { + while (corelist != 0) + { while ((short *) corelist->co_next == - (short *) corelist + corelist->co_size) { + (short *) corelist + corelist->co_size) + { corelist->co_size += corelist->co_next->co_size; - corelist->co_next = corelist->co_next->co_next; + 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) { + (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)); + sizeof(short)*(MAXSHORT-1)); corelist->co_size -= MAXSHORT; } - if (corelist->co_size >= MAXSHORT) { + if (corelist->co_size >= MAXSHORT) + { oldcore((short *) corelist + corelist->co_size-SINC, - sizeof(short)*SINC); + sizeof(short)*SINC); corelist->co_size -= SINC; } cl = corelist->co_next; @@ -271,7 +289,8 @@ compactcore() { } } -short *grabcore(size) int size; { +short *grabcore(size) int size; +{ register short *p; register trysize; @@ -283,9 +302,11 @@ short *grabcore(size) int size; { */ assert(size<2*MAXSHORT); - for(trysize=2*MAXSHORT-2; trysize>size; trysize -= 2) { + for(trysize=2*MAXSHORT-2; trysize>size; trysize -= 2) + { p = freelist[trysize/sizeof(short)]; - if ( p != (short *) 0) { + if ( p != (short *) 0) + { freelist[trysize/sizeof(short)] = *(short **) p; oldcore(p+size/sizeof(short),trysize-size); return(p); @@ -299,13 +320,16 @@ short *grabcore(size) int size; { */ compactcore(); - if ((p=freelist[size/sizeof(short)]) != 0) { + if ((p=freelist[size/sizeof(short)]) != 0) + { freelist[size/sizeof(short)] = * (short **) p; return(p); } - for(trysize=2*MAXSHORT-2; trysize>size; trysize -= 2) { + for(trysize=2*MAXSHORT-2; trysize>size; trysize -= 2) + { p = freelist[trysize/sizeof(short)]; - if ( p != (short *) 0) { + if ( p != (short *) 0) + { freelist[trysize/sizeof(short)] = *(short **) p; oldcore(p+size/sizeof(short),trysize-size); return(p); @@ -320,26 +344,30 @@ short *grabcore(size) int size; { } #endif /* SEPID */ -short *newcore(size) int size; { +short *newcore(size) int size; +{ register short *p,*q; size = (size + sizeof(int) - 1) & ~(sizeof(int) - 1); - if( size < 2*MAXSHORT ) { + if( size < 2*MAXSHORT ) + { if ((p=freelist[size/sizeof(short)]) != (short *) 0) - freelist[size/sizeof(short)] = *(short **) p; - else { + freelist[size/sizeof(short)] = *(short **) p; + else + { p = freshcore(size); #ifdef SEPID if (p == (short *) 0) - p = grabcore(size); + p = grabcore(size); #endif } - } else - p = freshcore(size); + } + else + p = freshcore(size); if (p == 0) - error("out of memory"); - for (q=p; size > 0 ; size -= sizeof(short)) - *q++ = 0; + error("out of memory"); + for (q=p; size > 0; size -= sizeof(short)) + *q++ = 0; return(p); } @@ -350,7 +378,8 @@ short *newcore(size) int size; { * you can use these as substitutes */ -char *malloc(size) int size; { +char *malloc(size) int size; +{ /* * malloc(III) is called by stdio, @@ -360,12 +389,14 @@ char *malloc(size) int size; { return( (char *) newcore(size)); } -free() { +free() +{ } #endif -oldcore(p,size) short *p; int size; { +oldcore(p,size) short *p; int size; +{ #ifdef CORECHECK register short *cp; #endif @@ -373,8 +404,8 @@ oldcore(p,size) short *p; int size; { assert(size<2*MAXSHORT); #ifdef CORECHECK for (cp=freelist[size/sizeof(short)]; cp != (short *) 0; - cp = *(short **) cp) - assert(cp != p); + cp = *(short **) cp) + assert(cp != p); #endif *(short **) p = freelist[size/sizeof(short)]; freelist[size/sizeof(short)] = p; @@ -382,7 +413,8 @@ oldcore(p,size) short *p; int size; { short *ccur,*cend; -coreinit(p1,p2) short *p1,*p2; { +coreinit(p1,p2) short *p1,*p2; +{ /* * coreinit is called with the boundaries of a piece of @@ -393,25 +425,28 @@ coreinit(p1,p2) short *p1,*p2; { cend = p2; } -short *freshcore(size) int size; { +short *freshcore(size) int size; +{ register short *temp; static int cchunk=CCHUNK; - - while(&ccur[size/sizeof(short)] >= cend && cchunk>0) { - do { + + while(&ccur[size/sizeof(short)] >= cend && cchunk>0) + { + do + { temp = (short *) sbrk(cchunk*sizeof(short)); if (temp == (short *) -1) - cchunk >>= 1; + cchunk >>= 1; else if (temp != cend) - ccur = cend = temp; - } while (temp == (short *) -1 && cchunk>0); + ccur = cend = temp; + }while (temp == (short *) -1 && cchunk>0); cend += cchunk; #ifdef COREDEBUG shortsasked += cchunk; #endif } if (cchunk==0) - return(0); + return(0); temp = ccur; ccur = &ccur[size/sizeof(short)]; return(temp); @@ -419,21 +454,22 @@ short *freshcore(size) int size; { #else /* USEMALLOC */ -coreinit() { - +void coreinit(void) +{ /* * Empty function, no initialization needed */ } -short *myalloc(size) register size; { - register short *p,*q; +short *myalloc(register unsigned int size) +{ + register short *p, *q; - p = (short *)malloc(size); + p = (short *) malloc(size); if (p == 0) error("out of memory"); - for(q=p;size>0;size -= sizeof(short)) + for (q = p; size > 0; size -= sizeof(short)) *q++ = 0; - return(p); + return (p); } #endif diff --git a/util/opt/alloc.h b/util/opt/alloc.h index 2c1de3e19..5af71fe5f 100644 --- a/util/opt/alloc.h +++ b/util/opt/alloc.h @@ -2,7 +2,10 @@ * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". */ -/* $Id$ */ +#ifndef ALLOC_H_ +#define ALLOC_H_ + +#include "types.h" extern line_p newline(); extern offset *newrom(); @@ -12,9 +15,12 @@ extern arg_p newarg(); extern argb_p newargb(); extern reg_p newreg(); -extern oldline(); -extern oldloc(); -extern oldreg(); +void oldline(register line_p lnp); +void oldreg(reg_p rp); +void oldargs(register arg_p ap); +void oldargb(register argb_p abp); +void oldnum(num_p lp); +void coreinit(void); #define USEMALLOC /* if defined malloc() and free() are used */ @@ -47,3 +53,5 @@ extern oldreg(); #define STACKROOM 1 /* 0 gives problems */ #endif /* USEMALLOC */ + +#endif /* ALLOC_H_ */ diff --git a/util/opt/backward.c b/util/opt/backward.c index 83304ce55..d20cb878e 100644 --- a/util/opt/backward.c +++ b/util/opt/backward.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include #include "param.h" #include "types.h" @@ -15,13 +17,8 @@ static char rcsid[] = "$Id$"; #include #include #include "ext.h" - -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ +#include "getline.h" +#include "reg.h" #define local(x) ((((x)->s_flags&SYMKNOWN) == 0 && \ ((x)->s_flags &= ~ SYMGLOBAL)),\ @@ -34,49 +31,52 @@ static char rcsid[] = "$Id$"; #define DTYPBSS 2 #define DTYPCON 3 #define DTYPROM 4 -byte curdtyp; -bool goodrom; -short curfrag = 3; /* see also peephole.c */ +byte curdtyp; +bool goodrom; +short curfrag = 3; /* see also peephole.c */ offset rombuf[MAXROM]; -int rc; +int rc; -extern offset aoff(); - -backward() { +void backward(void) +{ register line_p lnp; - line_p next; + line_p next; register arg_p ap; - line_p i,p; + 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) { + 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); + 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; } - ap = ap->a_next; - } - break; + break; } /* @@ -84,107 +84,118 @@ backward() { * 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; + 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: - 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;ns_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: - if (prodepth != 0 && - ((int) aoff(lnp->l_a.la_arg, 0) == ms_std || - (int) aoff(lnp->l_a.la_arg, 0) == ms_stb || - (int) aoff(lnp->l_a.la_arg, 0) == ms_ego)) { + assert((lnp->l_instr&BMASK)<=op_last); lnp->l_next = i; i = lnp; continue; - } - 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; + 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: + if (prodepth != 0 + && ((int) aoff(lnp->l_a.la_arg, 0) == ms_std + || (int) aoff(lnp->l_a.la_arg, 0) == ms_stb + || (int) aoff(lnp->l_a.la_arg, 0) == ms_ego)) + { + lnp->l_next = i; + i = lnp; + continue; + } + 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; + instrs = i; + pseudos = p; + curpro.lastline = (line_p) 0; } diff --git a/util/opt/build.lua b/util/opt/build.lua index cb86b3fff..a503a6f35 100644 --- a/util/opt/build.lua +++ b/util/opt/build.lua @@ -11,9 +11,9 @@ flex { } local headers = { - "./alloc.h", "./ext.h", "./line.h", "./lookup.h", "./optim.h", - "./param.h", "./pattern.h", "./pop_push.h", "./proinf.h", - "./tes.h", "./types.h", + "./alloc.h", "./ext.h", "./getline.h", "./line.h", "./lookup.h", "./optim.h", + "./param.h", "./pattern.h", "./pop_push.h", "./proinf.h","./putline.h", + "./reg.h","./tes.h", "./types.h","./util.h" } cprogram { diff --git a/util/opt/cleanup.c b/util/opt/cleanup.c index 504e712f4..3ac39c53d 100644 --- a/util/opt/cleanup.c +++ b/util/opt/cleanup.c @@ -1,61 +1,58 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - -#include -#include -#include -#include "param.h" -#include "types.h" -#include -#include -#include -#include "lookup.h" -#include "ext.h" - /* * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". * * Author: Hans van Staveren */ +#include +#include +#include "param.h" +#include "types.h" +#include +#include +#include +#include "lookup.h" +#include "putline.h" +#include "util.h" +#include "ext.h" - -void -cleanup() { +void cleanup(void) +{ FILE *infile; - register c; - register sym_p *spp,sp; + register int 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) + 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); + c = fclose(outfile); assert(c != EOF); outfile = stdout; - infile = fopen(template,"r"); + infile = fopen(tempname, "r"); if (infile == NULL) error("temp file disappeared"); outshort(sp_magic); /* Attempt to first output the word_size message */ - while ((c = getc(infile)) != sp_cend && c != EOF) { + while ((c = getc(infile)) != sp_cend && c != EOF) + { putc(c, outfile); } - if (c == sp_cend) putc(c, outfile); + if (c == sp_cend) + putc(c, outfile); outinst(ps_mes); outint(ms_ext); - for (spp=symhash;spp< &symhash[NSYMHASH];spp++) + 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)) + 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); + putc(sp_cend, outfile); + while ((c = getc(infile)) != EOF) + putc(c, outfile); + c = fclose(infile); assert(c != EOF); - c=unlink(template); + c = remove(tempname); assert(c == 0); } diff --git a/util/opt/em_opt.6 b/util/opt/em_opt.6 index 267bd9c69..f42ab1d56 100644 --- a/util/opt/em_opt.6 +++ b/util/opt/em_opt.6 @@ -3,7 +3,7 @@ .SH NAME em_opt \- EM peephole optimizer .SH SYNOPSIS -.B ~em/lib.bin/em_opt +.B em_opt [\-Ln] [\-m[l]] [ argument ] .SH DESCRIPTION Em_opt reads a compact EM-program, argument or standard input, diff --git a/util/opt/ext.h b/util/opt/ext.h index 4adeca38b..342ab17c0 100644 --- a/util/opt/ext.h +++ b/util/opt/ext.h @@ -16,7 +16,7 @@ extern bool repl_longmuls; extern byte em_flag[]; extern line_p instrs,pseudos; extern FILE *outfile; -extern char template[]; +extern char tempname[]; extern offset wordsize; extern offset pointersize; extern char *progname; diff --git a/util/opt/flow.c b/util/opt/flow.c index 73ae715af..08a04cf26 100644 --- a/util/opt/flow.c +++ b/util/opt/flow.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include "param.h" #include "types.h" #include "tes.h" @@ -14,97 +16,110 @@ static char rcsid[] = "$Id$"; #include "optim.h" #include "ext.h" -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ +/* Forward declarations */ +static void reach(register line_p); +static void findreach(void); +static void cleaninstrs(void); -void reach(); - -flow() { - - findreach(); /* determine reachable labels */ - cleaninstrs(); /* throw away unreachable code */ +void flow(void) +{ + findreach(); /* determine reachable labels */ + cleaninstrs(); /* throw away unreachable code */ } -findreach() { - register num_p *npp,np; +static void findreach(void) +{ + 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) { + 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)) { + if (!(np->n_flags & NUMSCAN)) + { np->n_flags |= NUMSCAN; - if (np->n_line) { + if (np->n_line) + { reach(np->n_line->l_next); continue; } - if (!(np->n_repl->n_flags&NUMSCAN)) { + if (!(np->n_repl->n_flags & NUMSCAN)) + { np->n_repl->n_flags |= NUMSCAN; if (np->n_repl->n_line) - reach(np->n_repl->n_line->l_next); + reach(np->n_repl->n_line->l_next); } } } } -void -reach(lnp) register line_p lnp; { +static void reach(register line_p lnp) +{ register num_p np; - for (;lnp != (line_p) 0; lnp = lnp->l_next) { - if(lnp->l_optyp == OPNUMLAB) { + 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) + if ((lnp->l_instr & BMASK) != op_lab) lnp->l_a.la_np = np = np->n_repl; np->n_flags |= NUMREACH; - if (!(np->n_flags&NUMSCAN)) { + if (!(np->n_flags & NUMSCAN)) + { np->n_flags |= NUMSCAN; if (np->n_line) reach(np->n_line->l_next); - else { + else + { np = np->n_repl; np->n_flags |= NUMREACH; - if (!(np->n_flags & NUMSCAN)) { + if (!(np->n_flags & NUMSCAN)) + { np->n_flags |= NUMSCAN; if (np->n_line) reach(np->n_line->l_next); } } } - if ((lnp->l_instr&BMASK) == op_lab) + if ((lnp->l_instr & BMASK) == op_lab) return; else np->n_jumps++; } - if ((lnp->l_instr & BMASK) > sp_lmnem) continue; - if ((em_flag[(lnp->l_instr&BMASK)-sp_fmnem]&EM_FLO)==FLO_T) + if ((lnp->l_instr & BMASK) > sp_lmnem) + continue; + if ((em_flag[(lnp->l_instr & BMASK) - sp_fmnem] & EM_FLO) == FLO_T) return; } } -cleaninstrs() { - register line_p *lpp,lp,*lastbra; - bool reachable,superfluous; +static void cleaninstrs(void) +{ + 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) { + 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) { + 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; @@ -113,19 +128,23 @@ cleaninstrs() { 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)) + 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 + } + else superfluous = FALSE; - if ( (!reachable) || superfluous) { - if (instr == op_lab) { + if ((!reachable) || superfluous) + { + if (instr == op_lab) + { lp->l_a.la_np->n_line = 0; } - else if (instr > sp_lmnem) { + else if (instr > sp_lmnem) + { /* leave pseudo's */ lpp = &lp->l_next; continue; @@ -134,11 +153,14 @@ cleaninstrs() { oldline(*lpp); OPTIM(O_UNREACH); *lpp = lp; - } else { - if ( instr <= sp_lmnem && - (em_flag[instr-sp_fmnem]&EM_FLO)==FLO_T) { + } + else + { + if (instr <= sp_lmnem + && (em_flag[instr - sp_fmnem] & EM_FLO) == FLO_T) + { reachable = FALSE; - if ((lp->l_instr&BMASK) == op_bra) + 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 index 9ecdf08fd..4fb4c4917 100644 --- a/util/opt/getline.c +++ b/util/opt/getline.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include #include #include "param.h" @@ -16,18 +18,13 @@ static char rcsid[] = "$Id$"; #include #include #include "ext.h" +#include "reg.h" +#include "getline.h" +#include "util.h" -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * 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 */ +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 @@ -44,275 +41,220 @@ static char string[IDL+1]; /* temp store for names */ #define readbyte getchar -short readshort() { +/* Other external declarations */ +extern void process(void); + +/* Forward declarations */ +static int table2(void); + +static void tstinpro(void) +{ + if (prodepth == 0) + error("This is not allowed outside a procedure"); +} + +short readshort(void) +{ 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) ; + if (h_byte >= 128) + h_byte -= 256; + return l_byte | (h_byte * 256); } #ifdef LONGOFF -offset readoffset() { +static offset readoffset(void) +{ 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) ; + 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() { - +static void draininput(void) +{ /* * called when MES ERR is encountered. * Drain input in case it is a pipe. */ - while (getchar() != EOF) ; } -short getint() { +static short getint(void) +{ - switch(table2()) { - default: error("int expected"); - case CSTX1: - return(tabval); + 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)); +static sym_p getsym(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); +static offset getoff(void) +{ + switch (table2()) + { + default: + error("offset expected"); + case CSTX1: + return ((offset) tabval); #ifdef LONGOFF - case CSTX2: - return(tabval2); + case CSTX2: + return (tabval2); #endif } } -make_string(n) int n; { - - sprintf(string,".%u",n); +static void make_string(int n) +{ + sprintf(string, ".%u", n); } -inident() { - register n; +static void inident(void) +{ + register int n; register char *p = string; - register c; + register int c; n = getint(); - while (n--) { + while (n--) + { c = readbyte(); - if (p<&string[IDL]) + 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); +static int table3(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); + 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); + 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); + case CSTX2: + return (VALX2); #endif } - default: return(n); + default: + return (n); } } -int table1() { - register n; +static int table1(void) +{ + register int n; n = readbyte(); if (n == EOF) - return(ATEOF); - if ((n <= sp_lmnem) && (n >= sp_fmnem)) { + return (ATEOF); + if ((n <= sp_lmnem) && (n >= sp_fmnem)) + { tabval = n; - return(INST); + return (INST); } - if ((n <= sp_lpseu) && (n >= sp_fpseu)) { + if ((n <= sp_lpseu) && (n >= sp_fpseu)) + { tabval = n; - return(PSEU); + return (PSEU); } - if ((n < sp_filb0 + sp_nilb0) && (n >= sp_filb0)) { + if ((n < sp_filb0 + sp_nilb0) && (n >= sp_filb0)) + { tabval = n - sp_filb0; - return(ILBX); + return (ILBX); } - return(table3(n)); + return (table3(n)); } -int table2() { - register n; +static int table2(void) +{ + register int n; n = readbyte(); - if ((n < sp_fcst0 + sp_ncst0) && (n >= sp_fcst0)) { + if ((n < sp_fcst0 + sp_ncst0) && (n >= sp_fcst0)) + { tabval = n - sp_zcst0; - return(CSTX1); + return (CSTX1); } - return(table3(n)); + return (table3(n)); } -void -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--) { +static void argstring(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; +static line_p arglist(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 @@ -321,74 +263,77 @@ line_p arglist(n) int n; { 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; + 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); + return (lnp); } -offset aoff(ap,n) register arg_p ap; { +offset aoff(register arg_p ap, int n) +{ - while (n>0) { + while (n > 0) + { if (ap != (arg_p) 0) ap = ap->a_next; n--; @@ -397,154 +342,284 @@ offset aoff(ap,n) register arg_p ap; { error("too few parameters"); if (ap->a_typ != ARGOFF) error("offset expected"); - return(ap->a_a.a_offset); + return (ap->a_a.a_offset); } -int inpseudo(n) short n; { - register line_p lnp,head,tail; - short n1,n2; +static int inpseudo(short n) +{ + register line_p lnp, head, tail; + short n1, n2; proinf savearea; #ifdef PSEUBETWEEN - static int pcount=0; + static int pcount = 0; - if (pcount++ >= PSEUBETWEEN && prodepth==0) { + if (pcount++ >= PSEUBETWEEN && prodepth == 0) + { process(); - pcount=0; + 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_gto: - curpro.gtoproc=1; - /* Treat as empty mes ms_reg */ - case ms_reg: - tstinpro(); - regvar(lnp->l_a.la_arg->a_next); - oldline(lnp); - lnp=newline(OPNO); - n=ps_exc; /* kludge to force out this line */ - break; - case ms_tes: - tstinpro(); - 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; + switch (n) + { default: - error("bad second arg of PRO"); - } - prodepth++; - curpro.gtoproc=0; - if (prodepth>1) { - register i; - - curpro.lastline = (line_p) 0; - curpro.freg = (reg_p) 0; - for(i=0;il_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 - return(0); - } else - return(1); + break; + case ms_gto: + curpro.gtoproc = 1; + /* Treat as empty mes ms_reg */ + case ms_reg: + tstinpro(); + regvar(lnp->l_a.la_arg->a_next); + oldline(lnp); + lnp = newline(OPNO); + n = ps_exc; /* kludge to force out this line */ + break; + case ms_tes: + tstinpro(); + 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++; + curpro.gtoproc = 0; + if (prodepth > 1) + { + register int 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); + return (0); } -tstinpro() { +void getlines(void) +{ + register line_p lnp; + register int instr; - if (prodepth==0) - error("This is not allowed outside a procedure"); + 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; + } } + diff --git a/util/opt/getline.h b/util/opt/getline.h new file mode 100644 index 000000000..6491e0f6c --- /dev/null +++ b/util/opt/getline.h @@ -0,0 +1,17 @@ +/* Copyright (c) 2019 ACK Project. + * See the copyright notice in the ACK home directory, + * in the file "Copyright". + * + * Created on: 2019-04-07 + * + */ +#ifndef GETLINE_H_ +#define GETLINE_H_ + +#include "types.h" + +void getlines(void); +offset aoff(register arg_p ap, int n); +short readshort(void); + +#endif /* GETLINE_H_ */ diff --git a/util/opt/lookup.c b/util/opt/lookup.c index 8cda8e518..434e1d198 100644 --- a/util/opt/lookup.c +++ b/util/opt/lookup.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include #include #include "param.h" @@ -10,68 +12,68 @@ static char rcsid[] = "$Id$"; #include "lookup.h" #include "alloc.h" #include "proinf.h" +#include "util.h" -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ - -unsigned hash(string) char *string; { +unsigned hash(char *string) +{ register char *p; - register unsigned i,sum; + register unsigned i, sum; - for (sum=i=0,p=string;*p;i += 3) - sum ^= (*p++)<<(i&07); - return(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; - register i; +sym_p symlookup(char *name, int status, int flags) +{ + register sym_p *spp, sp; + register int i; static short genfrag = 32767; - spp = &symhash[hash(name)%NSYMHASH]; + spp = &symhash[hash(name) % NSYMHASH]; while (*spp != (sym_p) 0) - if (strncmp((*spp)->s_name,name,IDL)==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); + 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 + return (sp); + } + else spp = &(*spp)->s_next; /* * symbol not found, enter in table */ - i = strlen(name) + 1; - if (i & 1) - i++; - if (i > IDL) - i = IDL; - *spp = sp = newsym(i); - strncpy(sp->s_name,name,i); + i = strlen(name) + 1; + if (i & 1) + i++; + if (i > IDL) + i = IDL; + *spp = sp = newsym(i); + strncpy(sp->s_name, name, i); sp->s_flags = flags; if (status == DEFINING) sp->s_flags |= SYMDEF; sp->s_frag = genfrag--; - return(sp); + return (sp); } -num_p numlookup(number) unsigned number; { +num_p numlookup(unsigned number) +{ register num_p *npp, np; - npp = &curpro.numhash[number%NNUMHASH]; + npp = &curpro.numhash[number % NNUMHASH]; while (*npp != (num_p) 0) if ((*npp)->n_number == number) - return(*npp); + return (*npp); else npp = &(*npp)->n_next; @@ -82,5 +84,5 @@ num_p numlookup(number) unsigned number; { *npp = np = newnum(); np->n_number = number; np->n_repl = np; - return(np); + return (np); } diff --git a/util/opt/lookup.h b/util/opt/lookup.h index 194c4501d..e37efa8e5 100644 --- a/util/opt/lookup.h +++ b/util/opt/lookup.h @@ -2,7 +2,10 @@ * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". */ -/* $Id$ */ +#ifndef LOOKUP_H_ +#define LOOKUP_H_ + +#include "types.h" #define IDL 100 @@ -28,3 +31,14 @@ extern sym_p symhash[NSYMHASH],symlookup(); #define OCCURRING 0 #define DEFINING 1 #define NOTHING 2 + +/** Return the hash value of the specified string. */ +unsigned hash(char *string); +num_p numlookup(unsigned number); +/** Search the hash table for the specified name + * and symbol type specified in `flags`. + */ +sym_p symlookup(char *name, int status, int flags); + + +#endif /* LOOKUP_H_ */ diff --git a/util/opt/main.c b/util/opt/main.c index a3df98a73..2856ffd33 100644 --- a/util/opt/main.c +++ b/util/opt/main.c @@ -1,72 +1,99 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - -#include -#include -#include "param.h" -#include "types.h" -#include "tes.h" -#include "alloc.h" -#include -#include "ext.h" - /* * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". * * Author: Hans van Staveren */ +#include +#include +#include "param.h" +#include "types.h" +#include "tes.h" +#include "alloc.h" +#include "system.h" +#include +#include "ext.h" +#include "util.h" +#include "getline.h" +#include "putline.h" + +/* Other external definitions */ +extern void cleanup(void); + +void flags(register char *s) +{ + for (s++; *s; s++) + switch (*s) + { + case 'L': + Lflag = TRUE; + break; + case 'n': + nflag = TRUE; + break; + case 'm': + if (*(s + 1) == 'l') + { + s++; + repl_longmuls = TRUE; + } + repl_muls = atoi(s + 1); + break; + } +} + +void fileinit(void) +{ + if (readshort() != (short) sp_magic) + error("wrong input file"); + if (Lflag) + { + + if (sys_tmpnam(tempname)==NULL) + { + error("can't create temporary file."); + } + outfile = fopen(tempname, "wb"); + if (outfile == NULL) + error("can't create %s", tempname); + } + else + { + outfile = stdout; + outshort(sp_magic); + } +} + /* * Main program for EM optimizer */ -main(argc,argv) int argc; char *argv[]; { +int main(int argc, char* argv[]) +{ +#ifndef USEMALLOC int somespace[STACKROOM]; +#endif progname = argv[0]; - while (argc-->1 && **++argv == '-') + while (argc-- > 1 && **++argv == '-') flags(*argv); - if (argc>1) { - fprintf(stderr,"Usage: %s [-Ln] [-m] [name]\n",progname); - exit(-1); + if (argc > 1) + { + fprintf(stderr, "Usage: %s [-Ln] [-m] [name]\n", progname); + exit(EXIT_FAILURE); } if (argc) - if (freopen(*argv,"r",stdin) == NULL) - error("Cannot open %s",*argv); + if (freopen(*argv, "r", stdin) == NULL) + error("Cannot open %s", *argv); fileinit(); - coreinit((short *)somespace,(short *)(somespace+STACKROOM)); +#ifdef USEMALLOC + coreinit(); +#else + coreinit((short *) somespace, (short *) (somespace + STACKROOM)); +#endif getlines(); cleanup(); - return(0); + return (EXIT_SUCCESS); } -flags(s) register char *s; { - - for (s++;*s;s++) - switch(*s) { - case 'L': Lflag = TRUE; break; - case 'n': nflag = TRUE; break; - case 'm': if (*(s+1) == 'l') { - s++; - repl_longmuls = TRUE; - } - repl_muls = atoi(s+1); break; - } -} - -fileinit() { - short readshort(); - - if (readshort() != (short) sp_magic) - error("wrong input file"); - if (Lflag) { - outfile = fdopen(mkstemp(template),"w"); - if (outfile == NULL) - error("can't create %s",template); - } else { - outfile = stdout; - outshort(sp_magic); - } -} diff --git a/util/opt/mktab.y b/util/opt/mktab.y index 7208919c5..d2e39d5f2 100644 --- a/util/opt/mktab.y +++ b/util/opt/mktab.y @@ -1,8 +1,10 @@ %{ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include #include #include @@ -13,12 +15,7 @@ static char rcsid[] = "$Id$"; #include #include "optim.h" -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ + #define op_CBO (op_plast+1) @@ -48,6 +45,22 @@ int CBO_instrs[] = { int patCBO; int rplCBO; + +/* Forward declarations */ +void inithash(void); +unsigned hashname(register char *name); +void enter(char *name,int value); +int mlookup(char* name); +int lookup(int comm,int operator,int lnode,int rnode); +void printnodes(void); +void initio(void); +void outpat(int exprno, int instrno); +void outbyte(int b); +void outshort(int s); +void out(int w); +int yylex(void); +void yyerror(const char*); + %} %union { @@ -241,8 +254,9 @@ struct hashmnem { byte h_value; } hashmnem[HASHSIZE]; -inithash() { - register i; +void inithash(void) +{ + register int i; enter("lab",op_lab); enter("LLP",op_LLP); @@ -254,7 +268,8 @@ inithash() { enter(em_mnem[i],i+sp_fmnem); } -unsigned hashname(name) register char *name; { +unsigned hashname(register char *name) +{ register unsigned h; h = (*name++)&BMASK; @@ -263,7 +278,8 @@ unsigned hashname(name) register char *name; { return(h); } -enter(name,value) char *name; { +void enter(char *name,int value) +{ register unsigned h; h=hashname(name)%HASHSIZE; @@ -273,7 +289,8 @@ enter(name,value) char *name; { hashmnem[h].h_value = value; } -int mlookup(name) char *name; { +int mlookup(char* name) +{ register unsigned h; h = hashname(name)%HASHSIZE; @@ -283,8 +300,8 @@ int mlookup(name) char *name; { return(hashmnem[h].h_value&BMASK); /* 0 if not found */ } -main() { - +int main(void) +{ inithash(); initio(); yyparse(); @@ -293,24 +310,26 @@ main() { return nerrors; } -int yywrap(void) { +int yywrap(void) +{ return 1; } -yyerror(s) char *s; { - +void yyerror(const char *s) +{ fprintf(stderr,"line %d: %s\n",lino,s); nerrors++; } -lookup(comm,operator,lnode,rnode) { +int lookup(int comm,int operator,int lnode,int rnode) { + register expr_p p; for (p=nodes+1;pex_operator != operator) continue; - if (!(p->ex_lnode == lnode && p->ex_rnode == rnode || - comm && p->ex_lnode == rnode && p->ex_rnode == lnode)) + if (!((p->ex_lnode == lnode && p->ex_rnode == rnode) || + (comm && p->ex_lnode == rnode && p->ex_rnode == lnode))) continue; return(p-nodes); } @@ -323,20 +342,22 @@ lookup(comm,operator,lnode,rnode) { return(p-nodes); } -printnodes() { +void printnodes(void) +{ register expr_p p; printf("};\n\nshort lastind = %d;\n\nexpr_t enodes[] = {\n",prevind); for (p=nodes;pex_operator,p->ex_lnode,p->ex_rnode); printf("};\n\niarg_t iargs[%d];\n", (maxpatlen>0 ? maxpatlen : 1)); if (patid[0]) printf("static char rcsid[] = %s;\n",patid); } -initio() { - register i; +void initio(void) +{ + register int i; printf("#include \"param.h\"\n#include \"types.h\"\n"); printf("#include \"pattern.h\"\n\n"); @@ -377,7 +398,7 @@ initio() { curind = 1; } -outpat(exprno, instrno) +void outpat(int exprno, int instrno) { register int i; @@ -403,20 +424,20 @@ outpat(exprno, instrno) if (patlen>maxpatlen) maxpatlen=patlen; } -outbyte(b) { - +void outbyte(int b) +{ printf(",%3d",b); curind++; } -outshort(s) { - +void outshort(int s) +{ outbyte(s&0377); outbyte((s>>8)&0377); } -out(w) { - +void out(int w) +{ if (w<255) { outbyte(w); } else { diff --git a/util/opt/patterns b/util/opt/patterns index 34bcc760a..58f5204f9 100644 --- a/util/opt/patterns +++ b/util/opt/patterns @@ -1,4 +1,4 @@ -"$Id$" + inc dec: inc loc adi $3==w: loc $2+1 adi w inc loc sbi $3==w: loc $2-1 sbi w diff --git a/util/opt/peephole.c b/util/opt/peephole.c index 9ad466ad2..ba310388f 100644 --- a/util/opt/peephole.c +++ b/util/opt/peephole.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include #include "param.h" #include "types.h" @@ -10,39 +12,42 @@ static char rcsid[] = "$Id$"; #include "lookup.h" #include "proinf.h" #include "alloc.h" +#include "reg.h" #include "pattern.h" #include #include #include "optim.h" #include "ext.h" -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ - #undef CHK_HASH /* print numbers patterns are hashed to */ #ifdef CHK_HASH #include #endif #define ILLHASH 0177777 -short pathash[256]; /* table of indices into pattern[] */ +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 } -}; +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; { +/* Forward declarations */ +static int repl_mul(register line_p, line_p *, line_p *); +static int optimize(void); +static int basicblock(line_p *); - if (((*bp)&BMASK) >= op_pfirst) - *bp = transl[((*bp)&BMASK)-op_pfirst][opind]; +static void opcheck(register byte *bp) +{ + if (((*bp) & BMASK) >= op_pfirst) + *bp = transl[((*bp) & BMASK) - op_pfirst][opind]; } /* @@ -54,41 +59,46 @@ opcheck(bp) register byte *bp; { * Estimated improvement possible: about 2% */ -hashpatterns() { +static void hashpatterns(void) +{ short index; - register byte *bp,*tp; + 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) { + 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; + 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) + if ((*tp++ & BMASK) == BMASK) tp += 2; - i = *tp++&BMASK; - if (i==BMASK) { - i = *tp++&BMASK; - i |= (*tp++&BMASK)<<8; + i = *tp++ & BMASK; + if (i == BMASK) + { + i = *tp++ & BMASK; + i |= (*tp++ & BMASK) << 8; } - while (i--) { + while (i--) + { opcheck(tp++); - if ((*tp++&BMASK)==BMASK) + if ((*tp++ & BMASK) == BMASK) tp += 2; } @@ -99,21 +109,22 @@ hashpatterns() { 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); + 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; + 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; + bp[PO_NEXT] = pathash[hashvalue] & BMASK; + bp[PO_NEXT + 1] = pathash[hashvalue] >> 8; pathash[hashvalue] = i; #ifdef CHK_HASH fprintf(stderr,"%d\n",hashvalue); @@ -121,352 +132,386 @@ hashpatterns() { } } -peephole() { +int peephole(void) +{ static bool phashed = FALSE; - if (!phashed) { + if (!phashed) + { hashpatterns(); - phashed=TRUE; + phashed = TRUE; } return optimize(); } -optimize() { - register num_p *npp,np; - register instr; +static int optimize(void) +{ + register num_p *npp, np; + register int instr; bool madeopt; madeopt = 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) continue; - if(np->n_line->l_next == (line_p) 0) + for (npp = curpro.numhash; npp < &curpro.numhash[NNUMHASH]; npp++) + for (np = *npp; np != (num_p) 0; np = np->n_next) + { + if (!np->n_line) continue; - instr = np->n_line->l_next->l_instr&BMASK; + 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 - if (basicblock(&np->n_line->l_next)) - madeopt = TRUE; + else if (basicblock(&np->n_line->l_next)) + madeopt = TRUE; } return madeopt; } -offset oabs(off) offset off; { +static offset oabs(offset off) +{ - return(off >= 0 ? off : -off); + return (off >= 0 ? off : -off); } -line_p repline(ev,patlen) eval_t ev; { +static line_p repline(eval_t ev, int patlen) +{ register line_p lp; register iarg_p iap; register sym_p sp; - offset diff,newdiff; + 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; + 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; } } - 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; + else + { + lp = newline(OPOFFSET); + lp->l_a.la_offset = ev.e_v.e_con; } - } - return(lp); + 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; +static offset rotate(offset w, offset amount) +{ + offset highmask, lowmask; #ifndef LONGOFF assert(wordsize<=4); #endif - highmask = (offset)(-1) << amount; + highmask = (offset) (-1) << amount; lowmask = ~highmask; if (wordsize != 4) - highmask &= wordsize==2 ? 0xFFFF : 0xFF; - return(((w<>(8*wordsize-amount))&lowmask)); + highmask &= wordsize == 2 ? 0xFFFF : 0xFF; + return (((w << amount) & highmask) + | ((w >> (8 * wordsize - amount)) & lowmask)); } -eval_t undefres = { EV_UNDEF }; +eval_t undefres = +{ EV_UNDEF }; -eval_t compute(pexp) register expr_p pexp; { - eval_t leaf1,leaf2,res; - register i; +static eval_t compute(register expr_p pexp) +{ + eval_t leaf1, leaf2, res; + register int 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; + 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) + switch (pexp->ex_operator) + { + default: + assert(FALSE); + break; + 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; - } - 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) + 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; - } - 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) { + 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 || - leaf2.e_v.e_con < 0 || - leaf2.e_v.e_con >= MAXROM) - return(undefres); - res.e_v.e_con = sp->s_rom[(int)(leaf2.e_v.e_con)]; + 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[(int) (leaf2.e_v.e_con)]; + break; + } + else + return (undefres); + case EX_SFIT: + mask = 0; + for (i = leaf2.e_v.e_con - 1; i < (int)(8 * sizeof(offset)); i++) + mask |= ((offset) 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 < (int)(8 * sizeof(offset)); i++) + mask |= ((offset) 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; - } else - return(undefres); - case EX_SFIT: - mask = 0; - for (i=leaf2.e_v.e_con - 1;i < 8*sizeof(offset); i++) - mask |= ((offset)1)<0;patlen--,tp=lp,lp=lp->l_next) + 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 ) { + while (lp != (line_p) 0) + { tp = lp->l_next; oldline(lp); lp = tp; } - return(TRUE); + return (TRUE); } -bool trypat(lpp,bp,len) -line_p *lpp; -register byte *bp; -int len; +static bool trypat(line_p *lpp, register byte *bp, int len) { register iarg_p iap; - int i,patlen; + int i, patlen; register line_p lp; eval_t result; - patlen = *bp++&BMASK; - if (patlen == BMASK) { - patlen = *bp++&BMASK; - patlen |= (*bp++&BMASK)<<8; + patlen = *bp++ & BMASK; + if (patlen == BMASK) + { + patlen = *bp++ & BMASK; + patlen |= (*bp++ & BMASK) << 8; } - if (len == 3) { - if (patlen<3) - return(FALSE); - } else { + if (len == 3) + { + if (patlen < 3) + return (FALSE); + } + else + { if (patlen != len) - return(FALSE); + return (FALSE); } /* * Length is ok, now check opcodes */ - for (i=0,lp= *lpp;il_next) + for (i = 0, lp = *lpp; i < patlen && lp != (line_p) 0; i++, lp = lp->l_next) if (lp->l_instr != *bp++) - return(FALSE); + return (FALSE); if (i != patlen) - return(FALSE); + return (FALSE); /* * opcodes are also correct, now comes the hard part */ - for(i=0,lp= *lpp,iap= iargs; il_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; + 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; + 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; + 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; + 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; + i = *bp++ & BMASK; + if (i == BMASK) + { + i = *bp++ & BMASK; + i |= (*bp++ & BMASK) << 8; } - if ( i != 0) { + 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 (FALSE); } - return(tryrepl(lpp,bp,patlen)); + return (tryrepl(lpp, bp, patlen)); } -int -basicblock(alpp) line_p *alpp; { - register line_p *lpp,lp; +static int basicblock(line_p *alpp) +{ + register line_p *lpp, lp; unsigned short hash[3]; line_p *next; register byte *bp; @@ -606,20 +658,25 @@ basicblock(alpp) line_p *alpp; { bool madeopt; int count = 0; - lpp = alpp; madeopt = FALSE; - while ((*lpp) != (line_p) 0 && ((*lpp)->l_instr&BMASK) != op_lab) { + 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; + 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); + hash[2] = (hash[1] << 4) ^ (lp->l_instr & BMASK); else hash[2] = ILLHASH; - } else { + } + else + { hash[1] = ILLHASH; hash[2] = ILLHASH; } @@ -628,195 +685,259 @@ basicblock(alpp) line_p *alpp; { * 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); - } + 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); + } } - if (lpp == next) { + if (lpp == next) + { count++; - if (count > 1000) { + if (count > 1000) + { /* probably loop in table */ - fprintf(stderr, "Warning: possible loop in patterns; call an expert\n"); + fprintf(stderr, + "Warning: possible loop in patterns; call an expert\n"); next = &((*lpp)->l_next); count = 0; } } - else count = 0; + else + count = 0; lpp = next; } lpp = alpp; - if (repl_muls) { - while ((lp = *lpp) != (line_p) 0 && (lp->l_instr&BMASK) != op_lab) { - line_p b_repl, e_repl; - int cnt; - - if ((cnt = (lp->l_instr & BMASK)) != op_loc && cnt != op_ldc) { - lpp = &lp->l_next; - continue; - } + if (repl_muls) + { + while ((lp = *lpp) != (line_p) 0 && (lp->l_instr & BMASK) != op_lab) + { + line_p b_repl, e_repl; + int cnt; - cnt = repl_mul(lp, &b_repl, &e_repl); - - lp = *lpp; - if (cnt > 0 && cnt <= repl_muls) { - *lpp = b_repl; - e_repl->l_next = lp->l_next->l_next; - oldline(lp->l_next); - oldline(lp); - lpp = &e_repl->l_next; - madeopt = TRUE; - } - else { - while (b_repl != (line_p) 0) { - line_p n = b_repl->l_next; - - oldline(b_repl); - b_repl = n; + if ((cnt = (lp->l_instr & BMASK)) != op_loc && cnt != op_ldc) + { + lpp = &lp->l_next; + continue; + } + + cnt = repl_mul(lp, &b_repl, &e_repl); + + lp = *lpp; + if (cnt > 0 && cnt <= repl_muls) + { + *lpp = b_repl; + e_repl->l_next = lp->l_next->l_next; + oldline(lp->l_next); + oldline(lp); + lpp = &e_repl->l_next; + madeopt = TRUE; + } + else + { + while (b_repl != (line_p) 0) + { + line_p n = b_repl->l_next; + + oldline(b_repl); + b_repl = n; + } + lpp = &lp->l_next; } - lpp = &lp->l_next; } - } } return madeopt; } -repl_mul(lp, b, e) - register line_p lp; - line_p *b, *e; +static int repl_mul(register line_p lp, line_p *b, line_p *e) { register line_p next = lp->l_next; - int ins; - int sz; - unsigned long n; - int n0, n1; - int virgin = 1; - int retval = 0; + int ins; + int sz; + unsigned long n; + int n0, n1; + int virgin = 1; + int retval = 0; *b = 0; - if (! next) return 0; - if ((ins = (next->l_instr & BMASK)) != op_mli && ins != op_mlu) { + if (!next) + return 0; + if ((ins = (next->l_instr & BMASK)) != op_mli && ins != op_mlu) + { return 0; } - switch(next->l_optyp) { - case OPNO: - return 0; - case OPSHORT: - sz = next->l_a.la_short; - break; + switch (next->l_optyp) + { + case OPNO: + return 0; + case OPSHORT: + sz = next->l_a.la_short; + break; #ifdef LONGOFF - case OPOFFSET: - sz = next->l_a.la_offset; - break; + case OPOFFSET: + sz = next->l_a.la_offset; + break; #endif - default: - sz = (next->l_optyp & BMASK) - Z_OPMINI; - break; + default: + sz = (next->l_optyp & BMASK) - Z_OPMINI; + break; } - if (ins == op_loc && sz != wordsize) return 0; - if (ins == op_ldc && sz != 2*wordsize) return 0; - if (! repl_longmuls && sz != wordsize) return 0; - switch(lp->l_optyp) { - case OPSHORT: - n = (long) lp->l_a.la_short; - break; + if (ins == op_loc && sz != wordsize) + return 0; + if (ins == op_ldc && sz != 2 * wordsize) + return 0; + if (!repl_longmuls && sz != wordsize) + return 0; + switch (lp->l_optyp) + { + case OPSHORT: + n = (long) lp->l_a.la_short; + break; #ifdef LONGOFF - case OPOFFSET: - n = lp->l_a.la_offset; - break; + case OPOFFSET: + n = lp->l_a.la_offset; + break; #endif - default: - n = (long)((lp->l_optyp & BMASK) - Z_OPMINI); - break; + default: + n = (long) ((lp->l_optyp & BMASK) - Z_OPMINI); + break; } #define newinstr(res, opcode, val) (*(res) = newline((short)(val)+Z_OPMINI), (*(res))->l_instr = (opcode)) - while (n) { + while (n) + { /* first find "0*1*$" in n */ - for (n1 = 0; n & 1; n>>=1) ++n1; /* count "1" bits */ + for (n1 = 0; n & 1; n >>= 1) + ++n1; /* count "1" bits */ if (n) - for (n0 = 0; !(n & 1); n>>=1) /* count "0" bits */ + for (n0 = 0; !(n & 1); n >>= 1) /* count "0" bits */ ++n0; else n0 = 0; - if (n1 == 0) { - if (n0) { - newinstr(b, op_loc, n0); b = &((*b)->l_next); - newinstr(b, op_slu, sz); b = &((*b)->l_next); - retval++; - } - } else if (n1 == 1) { - if (virgin) { - newinstr(b, op_dup, sz); b = &((*b)->l_next); - virgin = 0; - } - else { - newinstr(b, op_exg, sz); b = &((*b)->l_next); - newinstr(b, op_dup, 2*sz); b = &((*b)->l_next); - newinstr(b, op_asp, sz); b = &((*b)->l_next); - newinstr(b, op_adu, sz); b = &((*b)->l_next); - newinstr(b, op_exg, sz); b = &((*b)->l_next); - retval++; - } - if (n) { - newinstr(b, op_loc, n0+n1); b = &((*b)->l_next); - newinstr(b, op_slu, sz); b = &((*b)->l_next); - retval++; - } - } else { - if (virgin) { - newinstr(b, op_dup, sz); b = &((*b)->l_next); - if (sz == wordsize) { - newinstr(b, op_loc, 0); b = &((*b)->l_next); - } - else { - newinstr(b, op_ldc, 0); b = &((*b)->l_next); - } - newinstr(b, op_exg, sz); b = &((*b)->l_next); - virgin = 0; - } - else { - newinstr(b, op_exg, sz); b = &((*b)->l_next); - newinstr(b, op_dup, 2*sz); b = &((*b)->l_next); - newinstr(b, op_asp, sz); b = &((*b)->l_next); - } - newinstr(b, op_sbu, sz); b = &((*b)->l_next); - newinstr(b, op_exg, sz); b = &((*b)->l_next); - retval++; - if (n1 != 8*sz) { - newinstr(b, op_loc, n1); b = &((*b)->l_next); - newinstr(b, op_slu, sz); b = &((*b)->l_next); - retval++; - newinstr(b, op_exg, sz); b = &((*b)->l_next); - newinstr(b, op_dup, 2*sz); b = &((*b)->l_next); - newinstr(b, op_asp, sz); b = &((*b)->l_next); - newinstr(b, op_adu, sz); b = &((*b)->l_next); - newinstr(b, op_exg, sz); b = &((*b)->l_next); - retval++; - } - if (n0) { - newinstr(b, op_loc, n0); b = &((*b)->l_next); - newinstr(b, op_slu, sz); b = &((*b)->l_next); + if (n1 == 0) + { + if (n0) + { + newinstr(b, op_loc, n0); + b = &((*b)->l_next); + newinstr(b, op_slu, sz); + b = &((*b)->l_next); retval++; } } - } + else if (n1 == 1) + { + if (virgin) + { + newinstr(b, op_dup, sz); + b = &((*b)->l_next); + virgin = 0; + } + else + { + newinstr(b, op_exg, sz); + b = &((*b)->l_next); + newinstr(b, op_dup, 2 * sz); + b = &((*b)->l_next); + newinstr(b, op_asp, sz); + b = &((*b)->l_next); + newinstr(b, op_adu, sz); + b = &((*b)->l_next); + newinstr(b, op_exg, sz); + b = &((*b)->l_next); + retval++; + } + if (n) + { + newinstr(b, op_loc, n0 + n1); + b = &((*b)->l_next); + newinstr(b, op_slu, sz); + b = &((*b)->l_next); + retval++; + } + } + else + { + if (virgin) + { + newinstr(b, op_dup, sz); + b = &((*b)->l_next); + if (sz == wordsize) + { + newinstr(b, op_loc, 0); + b = &((*b)->l_next); + } + else + { + newinstr(b, op_ldc, 0); + b = &((*b)->l_next); + } + newinstr(b, op_exg, sz); + b = &((*b)->l_next); + virgin = 0; + } + else + { + newinstr(b, op_exg, sz); + b = &((*b)->l_next); + newinstr(b, op_dup, 2 * sz); + b = &((*b)->l_next); + newinstr(b, op_asp, sz); + b = &((*b)->l_next); + } + newinstr(b, op_sbu, sz); + b = &((*b)->l_next); + newinstr(b, op_exg, sz); + b = &((*b)->l_next); + retval++; + if (n1 != 8 * sz) + { + newinstr(b, op_loc, n1); + b = &((*b)->l_next); + newinstr(b, op_slu, sz); + b = &((*b)->l_next); + retval++; + newinstr(b, op_exg, sz); + b = &((*b)->l_next); + newinstr(b, op_dup, 2 * sz); + b = &((*b)->l_next); + newinstr(b, op_asp, sz); + b = &((*b)->l_next); + newinstr(b, op_adu, sz); + b = &((*b)->l_next); + newinstr(b, op_exg, sz); + b = &((*b)->l_next); + retval++; + } + if (n0) + { + newinstr(b, op_loc, n0); + b = &((*b)->l_next); + newinstr(b, op_slu, sz); + b = &((*b)->l_next); + retval++; + } + } + } newinstr(b, op_asp, sz); - if (virgin) { + if (virgin) + { b = &((*b)->l_next); newinstr(b, sz == wordsize ? op_loc : op_ldc, 0); - } + } *e = *b; return retval == 0 ? 1 : retval; #undef newinstr diff --git a/util/opt/process.c b/util/opt/process.c index c1ab7d238..631805b67 100644 --- a/util/opt/process.c +++ b/util/opt/process.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include #include "param.h" #include "types.h" @@ -9,59 +11,23 @@ static char rcsid[] = "$Id$"; #include #include #include "alloc.h" +#include "util.h" +#include "putline.h" #include "line.h" +#include "reg.h" #include "lookup.h" #include "proinf.h" #include "ext.h" -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ +/* External definitions */ +extern void flow(void); +extern void backward(void); +extern int peephole(void); -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) { - int npasses = 0; - bool madeopt; - - checklocs(); /* check definition of locals */ - do { - madeopt = peephole(); /* local optimization */ - relabel(); /* relabel local labels */ - flow(); /* throw away unreachable code */ - } while (madeopt && ++npasses < 5000); - assert(!madeopt); - } - do_tes(); /* top elt. size computation phase */ - outpro(); /* generate PRO pseudo */ - outregs(); /* generate MES ms_reg pseudos */ - outtes(); /* generate MES ms_tes 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; +static void relabel(void) +{ + register num_p *npp, np, tp; + register num_p repl, ttp; /* * For each label find its final destination after crossjumping. @@ -70,30 +36,33 @@ relabel() { */ for (npp = curpro.numhash; npp < &curpro.numhash[NNUMHASH]; npp++) - for (np = *npp; np != (num_p) 0; np = np->n_next) { - assert(! np->n_line || - ((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) + for (np = *npp; np != (num_p) 0; np = np->n_next) + { + assert( + ! np->n_line || ((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) { + 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; + tp->n_flags |= NUMKNOWN; } } for (npp = curpro.numhash; npp < &curpro.numhash[NNUMHASH]; npp++) - for (np = *npp; np != (num_p) 0; np = np->n_next) { - np->n_flags &= ~(NUMKNOWN|NUMSCAN|NUMREACH); + for (np = *npp; np != (num_p) 0; np = np->n_next) + { + np->n_flags &= ~(NUMKNOWN | NUMSCAN | NUMREACH); np->n_jumps = 0; } } -symknown() { - register sym_p *spp,sp; +static void symknown(void) +{ + register sym_p *spp, sp; for (spp = symhash; spp < &symhash[NSYMHASH]; spp++) for (sp = *spp; sp != (sym_p) 0; sp = sp->s_next) @@ -101,12 +70,15 @@ symknown() { sp->s_flags |= SYMKNOWN; } -cleanlocals() { - register num_p *npp,np,tp; +static void cleanlocals(void) +{ + register num_p *npp, np, tp; - for (npp = curpro.numhash; npp < &curpro.numhash[NNUMHASH]; npp++) { + for (npp = curpro.numhash; npp < &curpro.numhash[NNUMHASH]; npp++) + { np = *npp; - while (np != (num_p) 0) { + while (np != (num_p) 0) + { tp = np->n_next; oldnum(np); np = tp; @@ -115,23 +87,25 @@ cleanlocals() { } } -checklocs() { - register num_p *npp,np; +static void checklocs(void) +{ + 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) + 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); + error("local label %u undefined", (unsigned) np->n_number); } -offset align(count,alignment) offset count,alignment; { +static offset align(offset count, offset alignment) +{ - assert(alignment==1||alignment==2||alignment==4); - return((count+alignment-1)&~(alignment-1)); + assert(alignment == 1 || alignment == 2 || alignment == 4); + return ((count + alignment - 1) & ~(alignment - 1)); } -symvalue() { +static void symvalue(void) +{ register line_p lp; register sym_p sp; register arg_p ap; @@ -139,66 +113,117 @@ symvalue() { 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; + for (lp = pseudos; lp != (line_p) 0; lp = lp->l_next) + switch (lp->l_instr & BMASK) + { + default: + assert(FALSE); + break; + 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; + } } - 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; - } - } } -do_tes() +static void do_tes(void) { register line_p insptr = instrs, oldlin = NULL, oldlin2 = NULL; init_state(); tes_pseudos(); - while (insptr != NULL) { + while (insptr != NULL) + { tes_instr(insptr, oldlin, oldlin2); oldlin2 = oldlin; oldlin = insptr; insptr = insptr->l_next; } } + +void process(void) +{ + + 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) + { + int npasses = 0; + bool madeopt; + + checklocs(); /* check definition of locals */ + do + { + madeopt = peephole(); /* local optimization */ + relabel(); /* relabel local labels */ + flow(); /* throw away unreachable code */ + } while (madeopt && ++npasses < 5000); + assert(!madeopt); + } + do_tes(); /* top elt. size computation phase */ + outpro(); /* generate PRO pseudo */ + outregs(); /* generate MES ms_reg pseudos */ + outtes(); /* generate MES ms_tes 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 +} + diff --git a/util/opt/putline.c b/util/opt/putline.c index 50a8a6697..60c40ebf3 100644 --- a/util/opt/putline.c +++ b/util/opt/putline.c @@ -1,8 +1,11 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include +#include #include "param.h" #include "types.h" #include "tes.h" @@ -12,167 +15,179 @@ static char rcsid[] = "$Id$"; #include #include "alloc.h" #include "line.h" +#include "putline.h" +#include "util.h" #include "lookup.h" #include "proinf.h" #include "optim.h" #include "ext.h" -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ - #define outbyte(b) putc(b,outfile) -putlines(lnp) register line_p lnp; { +/* Forward declarations. */ +static void numlab(register num_p); +static void putargs(register arg_p); +static void putstr(register argb_p); + +void putlines(register line_p lnp) +{ register arg_p ap; line_p temp; - register instr; - short curlin= -2; + register int 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; + 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; + 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; + 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; } - ap = ap->a_next; - } - break; + 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: + switch (instr) + { + case ps_sym: + break; + case op_lni: + if (curlin != -2) + curlin++; outinst(instr); - goto processoperand; - case OPSHORT: - thislin = lnp->l_a.la_short; + 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: - 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; + if ((em_flag[instr - sp_fmnem] & EM_FLO) == FLO_P) + curlin = -2; 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: - if ((em_flag[instr-sp_fmnem]&EM_FLO)==FLO_P) - curlin = -2; - 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; + 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; + 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; + 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; + 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) ; - } + 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. @@ -187,185 +202,212 @@ processoperand: } } -putargs(ap) register arg_p ap; { +static void putargs(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; + while (ap != (arg_p) 0) + { + switch (ap->a_typ) + { + default: + assert(FALSE); + break; + 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; { +static void putstr(register argb_p abp) +{ register argb_p tbp; - register length; + register int length; length = 0; tbp = abp; - while (tbp!= (argb_p) 0) { + while (tbp != (argb_p) 0) + { length += tbp->ab_index; tbp = tbp->ab_next; } outint(length); - while (abp != (argb_p) 0) { - for (length=0;lengthab_index;length++) - outbyte( (byte) abp->ab_contents[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; { +void outdef(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) { + 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); - } + 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; { +void outocc(register sym_p sp) +{ - if ((sp->s_flags&SYMOUT)==0) { + 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); + if ((sp->s_flags & SYMGLOBAL) == 0) + { + outinst(sp->s_flags & SYMPRO ? ps_inp : ps_ina); outsym(sp); } } } -outpro() { - +void outpro(void) +{ outdef(curpro.symbol); outinst(ps_pro); outsym(curpro.symbol); outoff(curpro.localbytes); } -outend() { - +void outend(void) +{ outinst(ps_end); outoff(curpro.localbytes); } -outinst(m) { - - outbyte( (byte) m ); +void outinst(int m) +{ + outbyte((byte ) m); } -outoff(off) offset off; { - +void outoff(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) ); + else + { + outbyte((byte) sp_cst4); + outshort((short) (off & 0177777L)); + outshort((short) (off >> 16)); } #endif } -outint(i) short i; { +void outint(short i) +{ - if (i>= -sp_zcst0 && i< sp_ncst0-sp_zcst0) - outbyte( (byte) (i+sp_zcst0+sp_fcst0) ); - else { - outbyte( (byte) sp_cst2) ; + 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) ); +void outshort(short i) +{ + outbyte((byte) (i&BMASK)); + outbyte((byte ) (i >> 8)); } -numlab(np) register num_p np; { - +static void numlab(register num_p np) +{ if (np->n_number < sp_nilb0) - outbyte( (byte) (np->n_number + sp_filb0) ); + 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) ; +void outnum(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; +void outsym(register sym_p sp) +{ + register char *p; register unsigned num; - if (sp->s_name[0] == '.') { + 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) ; + if (num < 256) + { + outbyte((byte) sp_dlb1); + outbyte((byte ) (num)); + } + else + { + outbyte((byte) sp_dlb2); outshort((short) num); } - } else { - p= sp->s_name; + } + else + { + p = sp->s_name; + /* This is not a real warning, because s_name is dynamically + * allocated as necessary. + */ while (*p && p < &sp->s_name[IDL]) p++; num = p - sp->s_name; - outbyte( (byte) (sp->s_flags&SYMPRO ? sp_pnam : sp_dnam) ); + outbyte((byte) (sp->s_flags&SYMPRO ? sp_pnam : sp_dnam)); outint((short) num); p = sp->s_name; while (num--) - outbyte( (byte) *p++ ); + outbyte((byte ) *p++); } } diff --git a/util/opt/putline.h b/util/opt/putline.h new file mode 100644 index 000000000..2b798901f --- /dev/null +++ b/util/opt/putline.h @@ -0,0 +1,25 @@ +/* Copyright (c) 2019 ACK Project. + * See the copyright notice in the ACK home directory, + * in the file "Copyright". + * + * Created on: 2019-04-06 + * + */ +#ifndef PUTLINE_H_ +#define PUTLINE_H_ + +#include "types.h" + +void putlines(register line_p lnp); +void outdef(register sym_p sp); +void outocc(register sym_p sp); +void outpro(void); +void outend(void); +void outinst(int m); +void outoff(offset off); +void outint(short i); +void outshort(short i); +void outnum(register num_p np); +void outsym(register sym_p sp); + +#endif /* PUTLINE_H_ */ diff --git a/util/opt/reg.c b/util/opt/reg.c index c00dcd11e..62385630d 100644 --- a/util/opt/reg.c +++ b/util/opt/reg.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include #include "param.h" #include "types.h" @@ -9,62 +11,69 @@ static char rcsid[] = "$Id$"; #include "tes.h" #include "proinf.h" #include "alloc.h" +#include "putline.h" +#include "reg.h" #include #include #include #include "ext.h" -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ - -void -regvar(ap) register arg_p ap; { +void regvar(register arg_p ap) +{ register reg_p rp; - register i; + register int 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; + 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; + switch (i) + { + default: + assert(FALSE); + break; + 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; { +int inreg(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); + 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; +void outregs(void) +{ + register reg_p rp, tp; + register int i; - for(rp=curpro.freg; rp != (reg_p) 0; rp = tp) { + for (rp = curpro.freg; rp != (reg_p) 0; rp = tp) + { tp = rp->r_next; outinst(ps_mes); - outoff((offset)ms_reg); - for(i=0;i<4;i++) + outoff((offset) ms_reg); + for (i = 0; i < 4; i++) outoff(rp->r_par[i]); outinst(sp_cend); oldreg(rp); @@ -74,39 +83,44 @@ outregs() { * will be output. Kludgy. */ outinst(ps_mes); - outoff((offset)(curpro.gtoproc? ms_gto : ms_reg)); + outoff((offset) (curpro.gtoproc ? ms_gto : ms_reg)); outinst(sp_cend); curpro.freg = (reg_p) 0; } /* outtes() handles the output of the top elt. messages */ -outtes() { +void outtes(void) +{ 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_flags & NUMSET) || np->n_size == 0 || - (np->n_flags & NUMCOND)) continue; + for (npp = curpro.numhash; npp < &curpro.numhash[NNUMHASH]; npp++) + { + for (np = *npp; np != (num_p) 0; np = np->n_next) + { + if (!(np->n_flags & NUMSET) || np->n_size == 0 + || (np->n_flags & NUMCOND)) + continue; outinst(ps_mes); - outoff((offset)ms_tes); - outoff((offset)np->n_number); - outoff((offset)np->n_size); - outoff((offset)((np->n_flags & NUMFALLTHROUGH) ? 1 : 0)); + outoff((offset) ms_tes); + outoff((offset) np->n_number); + outoff((offset) np->n_size); + outoff((offset) ((np->n_flags & NUMFALLTHROUGH) ? 1 : 0)); outinst(sp_cend); } } } -void -incregusage(off) offset off; { +void incregusage(offset off) +{ register reg_p rp; #ifndef GLOBAL_OPT /* If we're optimizing the output of the global optimizer * we must not change the count fields of the register messages. */ - for(rp=curpro.freg; rp != (reg_p) 0; rp=rp->r_next) - if (rp->r_par[0]==off) { + 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/reg.h b/util/opt/reg.h new file mode 100644 index 000000000..144c224df --- /dev/null +++ b/util/opt/reg.h @@ -0,0 +1,17 @@ +/* Copyright (c) 2019 ACK Project. + * See the copyright notice in the ACK home directory, + * in the file "Copyright". + * + */ +#ifndef REG_H_ +#define REG_H_ + +#include "types.h" + +void regvar(register arg_p ap); +void outregs(void); +int inreg(offset off); +void outtes(void); +void incregusage(offset off); + +#endif /* REG_H_ */ diff --git a/util/opt/scan.l b/util/opt/scan.l index 1c26f1857..a14690253 100644 --- a/util/opt/scan.l +++ b/util/opt/scan.l @@ -1,8 +1,4 @@ %{ -#ifndef NORCSID -static char rcsid2[] = "$Id$"; -#endif - /* * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". @@ -13,6 +9,8 @@ static char rcsid2[] = "$Id$"; extern long atol(); extern char patid[128]; extern int lino; +extern int mlookup(char* name); +extern void yyerror(const char *); #include "y.tab.h" diff --git a/util/opt/special.c b/util/opt/special.c index 132e83344..3679e00b1 100644 --- a/util/opt/special.c +++ b/util/opt/special.c @@ -1,22 +1,15 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - -#include "param.h" -#include "types.h" - /* * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". * * Author: Hans van Staveren */ +#include "param.h" +#include "types.h" + #ifdef ALLOWSPECIAL -bool special(lpp,bp,patlen) -line_p *lpp; -byte *bp; -int patlen; +bool special(line_p *lpp,byte *bp,int patlen) { return(FALSE); diff --git a/util/opt/tes.c b/util/opt/tes.c index 028bf845f..3f2803217 100644 --- a/util/opt/tes.c +++ b/util/opt/tes.c @@ -1,12 +1,8 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif /* * This file contains the main part of the top element size computation phase. * * Author: Hans van Eck. */ - #include #include #include @@ -39,152 +35,206 @@ extern char flow_tab[]; int state; static int stacktop = 0; -init_state() +/* Forward declarations */ +static void do_inst_label(line_p); +static void assign_label(register num_p); + +void init_state(void) { stacktop = 0; state = KNOWN; } -tes_pseudos() +void tes_pseudos(void) { register line_p lp; - for (lp = pseudos; lp != (line_p)0; lp = lp->l_next) { - switch(INSTR(lp)) { - case ps_con: - case ps_rom: - if (lp->l_optyp == OPLIST) { - register arg_p ap = lp->l_a.la_arg; + for (lp = pseudos; lp != (line_p) 0; lp = lp->l_next) + { + switch (INSTR(lp)) + { + case ps_con: + case ps_rom: + if (lp->l_optyp == OPLIST) + { + register arg_p ap = lp->l_a.la_arg; - while (ap != (arg_p) 0) { - if (ap->a_typ == ARGNUM) { - assign_label(ap->a_a.a_np->n_repl); + while (ap != (arg_p) 0) + { + if (ap->a_typ == ARGNUM) + { + assign_label(ap->a_a.a_np->n_repl); + } + ap = ap->a_next; } - ap = ap->a_next; } - } else if (lp->l_optyp == OPNUMLAB) - assign_label(lp->l_a.la_np->n_repl); + else if (lp->l_optyp == OPNUMLAB) + assign_label(lp->l_a.la_np->n_repl); } } } -void -tes_instr(lnp, x, y) -line_p lnp, x, y; +void tes_instr(line_p lnp, line_p x, line_p y) { char *s; - register instr = INSTR(lnp); + register int instr = INSTR(lnp); register int arg, argdef; int neg = 0; - if (instr == op_lab) { + if (instr == op_lab) + { do_inst_label(lnp); return; } - if (instr < sp_fmnem || instr > sp_lmnem) { + if (instr < sp_fmnem || instr > sp_lmnem) + { return; } - if (state == NOTREACHED) return; /* What else ? */ + if (state == NOTREACHED) + return; /* What else ? */ s = pop_push[instr]; - if (*s != '0') { - while (*s != '\0') { + if (*s != '0') + { + while (*s != '\0') + { neg = (*s++ == '-'); - if (TYPE(lnp) == OPSHORT) { + if (TYPE(lnp) == OPSHORT) + { arg = SHORT(lnp); - if (arg < wordsize) arg = wordsize; + if (arg < wordsize) + arg = wordsize; argdef = TRUE; - } else if (IS_MINI(lnp)) { + } + else if (IS_MINI(lnp)) + { arg = MINI(lnp); - if (arg > 0 && arg < wordsize) arg = wordsize; - if (arg < 0 && -arg < wordsize) arg = -wordsize; + if (arg > 0 && arg < wordsize) + arg = wordsize; + if (arg < 0 && -arg < wordsize) + arg = -wordsize; argdef = TRUE; - } else { + } + else + { argdef = FALSE; } - switch (*s++) { - case 'w': stacktop = wordsize; break; - case 'd': stacktop = wordsize * 2; break; - case 'p': stacktop = pointersize; break; - case 'a': - if (argdef == FALSE || instr == op_ass) { + switch (*s++) + { + case 'w': + stacktop = wordsize; + break; + case 'd': + stacktop = wordsize * 2; + break; + case 'p': + stacktop = pointersize; + break; + case 'a': + if (argdef == FALSE || instr == op_ass) + { + stacktop = 0; + } + else + { + stacktop = arg; + } + break; + case 'x': + if (IS_LOC(x)) + { + arg = MINI(x); + if (arg < wordsize) + arg = wordsize; + stacktop = arg; + } + else + { + stacktop = 0; + } + break; + case 'y': + if (IS_LOC(y)) + { + arg = MINI(y); + if (arg < wordsize) + arg = wordsize; + stacktop = arg; + } + else + { + stacktop = 0; + } + break; + case '?': stacktop = 0; - } else { - stacktop = arg; - } - break; - case 'x': - if (IS_LOC(x)) { - arg = MINI(x); - if (arg < wordsize) arg = wordsize; - stacktop = arg; - } else { - stacktop = 0; - } - break; - case 'y': - if (IS_LOC(y)) { - arg = MINI(y); - if (arg < wordsize) arg = wordsize; - stacktop = arg; - } else { - stacktop = 0; - } - break; - case '?': - stacktop = 0; - break; - default: - assert(FALSE); + break; + default: + assert(FALSE); } } /* * When the last argument was negative, the element size * must be negated. This is to catch 'asp -4'. */ - if (neg) stacktop = -stacktop; + if (neg) + stacktop = -stacktop; } - if (stacktop < 0) stacktop = 0; + if (stacktop < 0) + stacktop = 0; - if (ISABRANCH(instr)) do_inst_label(lnp); - if (NON_CONTINUABLE(instr)) { + if (ISABRANCH(instr)) + do_inst_label(lnp); + if (NON_CONTINUABLE(instr)) + { state = NOTREACHED; stacktop = 0; } } -assign_label(label) -register num_p label; +static void assign_label(register num_p label) { - if (label->n_flags & NUMSET) { - if (state == NOTREACHED || stacktop > label->n_size) { + if (label->n_flags & NUMSET) + { + if (state == NOTREACHED || stacktop > label->n_size) + { stacktop = label->n_size; - } else if ( stacktop < label->n_size) { + } + else if (stacktop < label->n_size) + { label->n_size = stacktop; } - } else { + } + else + { label->n_size = stacktop; label->n_flags |= NUMSET; } } -do_inst_label(lnp) /* (re-)install a label */ -line_p lnp; +/* (re-)install a label */ +static void do_inst_label(line_p lnp) { num_p label = lnp->l_a.la_np->n_repl; int instr = INSTR(lnp); assign_label(label); - if (instr == op_lab) { - if (state == NOTREACHED) { - } else { + if (instr == op_lab) + { + if (state == NOTREACHED) + { + } + else + { label->n_flags |= NUMFALLTHROUGH; } - } else if (ISCONDBRANCH(instr)) { /* conditional branch */ + } + else if (ISCONDBRANCH(instr)) + { /* conditional branch */ label->n_flags |= NUMCOND; } state = KNOWN; diff --git a/util/opt/tes.h b/util/opt/tes.h index 770512166..4fb4f76ff 100644 --- a/util/opt/tes.h +++ b/util/opt/tes.h @@ -1,8 +1,17 @@ /* * Author: Hans van Eck. */ -/* $Id$ */ +#ifndef TES_H_ +#define TES_H_ + +#include "types.h" extern int state; #define KNOWN 1 #define NOTREACHED 2 + +void init_state(void); +void tes_pseudos(void); +void tes_instr(line_p lnp, line_p x, line_p y); + +#endif /* TES_H_ */ diff --git a/util/opt/types.h b/util/opt/types.h index c9ff1824d..4c73c3787 100644 --- a/util/opt/types.h +++ b/util/opt/types.h @@ -2,9 +2,10 @@ * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". */ -/* $Id$ */ +#ifndef TYPES_H_ +#define TYPES_H_ -typedef char byte; +typedef unsigned char byte; typedef char bool; typedef struct line line_t; typedef struct line *line_p; @@ -23,3 +24,5 @@ typedef long offset; #else typedef short offset; #endif + +#endif /* TYPES_H_ */ diff --git a/util/opt/util.c b/util/opt/util.c index b38796507..f62542a34 100644 --- a/util/opt/util.c +++ b/util/opt/util.c @@ -1,46 +1,47 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - -#include -#include -#include "param.h" -#include "types.h" -#include "tes.h" -#include "lookup.h" -#include "proinf.h" -#include "optim.h" -#include "ext.h" - /* * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". * * Author: Hans van Staveren */ - +#include +#include +#include +#include "param.h" +#include "types.h" +#include "tes.h" +#include "lookup.h" +#include "proinf.h" +#include "optim.h" +#include "util.h" +#include "ext.h" /* VARARGS1 */ -error(s,a) char *s,*a; { +void error(char *s, ...) +{ + va_list ap; + va_start(ap, s); - fprintf(stderr,"%s: error on line %u",progname,linecount); + 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"); + fprintf(stderr, "(%.*s)", IDL, curpro.symbol->s_name); + fprintf(stderr, ": "); + vfprintf(stderr, s, ap); + fprintf(stderr, "\n"); + + va_end(ap); #ifndef NDEBUG abort(); #endif - exit(-1); + exit(EXIT_FAILURE); } #ifdef DIAGOPT -optim(n) { - +void optim(int n) +{ fprintf(stderr,"Made optimization %d",n); if (prodepth) - fprintf(stderr," (%.*s)",IDL,curpro.symbol->s_name); + fprintf(stderr," (%.*s)",IDL,curpro.symbol->s_name); fprintf(stderr,"\n"); } #endif diff --git a/util/opt/util.h b/util/opt/util.h new file mode 100644 index 000000000..c5f56f4cc --- /dev/null +++ b/util/opt/util.h @@ -0,0 +1,15 @@ +/* Copyright (c) 2019 ACK Project. + * See the copyright notice in the ACK home directory, + * in the file "Copyright". + * + * + */ +#ifndef UTIL_H_ +#define UTIL_H_ + +void error(char *s, ...); +#ifdef DIAGOPT +void optim(int n); +#endif + +#endif /* UTIL_H_ */ diff --git a/util/opt/var.c b/util/opt/var.c index 58c803f03..3e51a9a70 100644 --- a/util/opt/var.c +++ b/util/opt/var.c @@ -1,7 +1,9 @@ -#ifndef NORCSID -static char rcsid[] = "$Id$"; -#endif - +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * See the copyright notice in the ACK home directory, in the file "Copyright". + * + * Author: Hans van Staveren + */ #include #include "param.h" #include "types.h" @@ -9,12 +11,7 @@ static char rcsid[] = "$Id$"; #include "lookup.h" #include "proinf.h" -/* - * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. - * See the copyright notice in the ACK home directory, in the file "Copyright". - * - * Author: Hans van Staveren - */ + unsigned linecount = 0; /* "line"number for errormessages */ int prodepth = 0; /* Level of nesting */ @@ -25,7 +22,7 @@ bool repl_longmuls = 0; /* replacing longmuls as well? */ line_p instrs,pseudos; /* pointers to chains */ sym_p symhash[NSYMHASH]; /* array of pointers to chains */ FILE *outfile; -char template[] = "/tmp/emoptXXXXXX"; +char tempname[L_tmpnam]; offset wordsize = 0; offset pointersize = 0; char *progname;