ANSI C conversion and add procedure declarations.

This commit is contained in:
carl 2019-05-11 01:17:40 +08:00
parent 472654c366
commit 21e15965cc
29 changed files with 2510 additions and 1970 deletions

View file

@ -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 <assert.h>
#include <stdlib.h>
#include <stdio.h>
@ -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;size<MAXSHORT;size++)
for (p=freelist[size];p!=0;p = *(short **) p)
sum += size;
for (p=freelist[size];p!=0;p = *(short **) p)
sum += size;
fprintf(stderr,"Used core %u\n",(shortsasked-sum)*sizeof(short));
}
#endif
#ifdef SEPID
compactcore() {
compactcore()
{
register core_p corelist=0,tp,cl;
int size;
#ifdef COREDEBUG
fprintf(stderr,"Almost out of core\n");
#endif
for(size=SINC;size<MAXSHORT;size++) {
while ((tp = (core_p) freelist[size]) != (core_p) 0) {
for(size=SINC;size<MAXSHORT;size++)
{
while ((tp = (core_p) freelist[size]) != (core_p) 0)
{
freelist[size] = (short *) tp->co_next;
tp->co_size = size;
if (corelist==0 || tp<corelist) {
if (corelist==0 || tp<corelist)
{
tp->co_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

View file

@ -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_ */

View file

@ -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 <assert.h>
#include "param.h"
#include "types.h"
@ -15,13 +17,8 @@ static char rcsid[] = "$Id$";
#include <em_mnem.h>
#include <em_mes.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 "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;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)) {
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;
}

View file

@ -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 {

View file

@ -1,61 +1,58 @@
#ifndef NORCSID
static char rcsid[] = "$Id$";
#endif
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include "param.h"
#include "types.h"
#include <em_pseu.h>
#include <em_spec.h>
#include <em_mes.h>
#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 <assert.h>
#include <stdio.h>
#include "param.h"
#include "types.h"
#include <em_pseu.h>
#include <em_spec.h>
#include <em_mes.h>
#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);
}

View file

@ -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]<num>] [ argument ]
.SH DESCRIPTION
Em_opt reads a compact EM-program, argument or standard input,

View file

@ -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;

View file

@ -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;

View file

@ -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 <stdlib.h>
#include <stdio.h>
#include "param.h"
@ -16,18 +18,13 @@ static char rcsid[] = "$Id$";
#include <em_flag.h>
#include <em_mes.h>
#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;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");
error("unknown pseudo");
case ps_bss:
case ps_hol:
lnp = arglist(3);
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;
case ps_rom:
case ps_con:
lnp = arglist(0);
break;
}
process();
curpro.symbol = (sym_p) 0;
if (prodepth==1) {
prodepth=0;
#ifdef PSEUBETWEEN
pcount=0;
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
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;
}
}

17
util/opt/getline.h Normal file
View file

@ -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_ */

View file

@ -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 <stdlib.h>
#include <string.h>
#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);
}

View file

@ -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_ */

View file

@ -1,72 +1,99 @@
#ifndef NORCSID
static char rcsid[] = "$Id$";
#endif
#include <stdlib.h>
#include <stdio.h>
#include "param.h"
#include "types.h"
#include "tes.h"
#include "alloc.h"
#include <em_spec.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 <stdlib.h>
#include <stdio.h>
#include "param.h"
#include "types.h"
#include "tes.h"
#include "alloc.h"
#include "system.h"
#include <em_spec.h>
#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<num>] [name]\n",progname);
exit(-1);
if (argc > 1)
{
fprintf(stderr, "Usage: %s [-Ln] [-m<num>] [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);
}
}

View file

@ -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 <stdlib.h>
#include <stdio.h>
#include <string.h>
@ -13,12 +15,7 @@ static char rcsid[] = "$Id$";
#include <em_mnem.h>
#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;p<lastnode;p++) {
if (p->ex_operator != operator)
continue;
if (!(p->ex_lnode == lnode && p->ex_rnode == rnode ||
comm && p->ex_lnode == rnode && p->ex_rnode == lnode))
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;p<lastnode;p++)
printf("/* %3d */\t%3d,%6u,%6u,\n",
printf("/* %3d */\t{%3d,%6u,%6u},\n",
p-nodes,p->ex_operator,p->ex_lnode,p->ex_rnode);
printf("};\n\niarg_t iargs[%d];\n", (maxpatlen>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 {

View file

@ -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

File diff suppressed because it is too large Load diff

View file

@ -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 <assert.h>
#include "param.h"
#include "types.h"
@ -9,59 +11,23 @@ static char rcsid[] = "$Id$";
#include <em_spec.h>
#include <em_pseu.h>
#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
}

View file

@ -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 <assert.h>
#include <stdlib.h>
#include "param.h"
#include "types.h"
#include "tes.h"
@ -12,167 +15,179 @@ static char rcsid[] = "$Id$";
#include <em_flag.h>
#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;length<abp->ab_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++);
}
}

25
util/opt/putline.h Normal file
View file

@ -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_ */

View file

@ -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 <assert.h>
#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 <em_spec.h>
#include <em_pseu.h>
#include <em_mes.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
*/
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;
}

17
util/opt/reg.h Normal file
View file

@ -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_ */

View file

@ -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"

View file

@ -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);

View file

@ -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 <assert.h>
#include <stdio.h>
#include <em_spec.h>
@ -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;

View file

@ -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_ */

View file

@ -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_ */

View file

@ -1,46 +1,47 @@
#ifndef NORCSID
static char rcsid[] = "$Id$";
#endif
#include <stdlib.h>
#include <stdio.h>
#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 <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#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

15
util/opt/util.h Normal file
View file

@ -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_ */

View file

@ -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 <stdio.h>
#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;