ack/util/opt/process.c

230 lines
5.1 KiB
C
Raw Permalink Normal View History

/*
* (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>
1984-05-17 13:42:36 +00:00
#include "param.h"
#include "types.h"
#include "tes.h"
#include <em_spec.h>
#include <em_pseu.h>
1984-05-17 13:42:36 +00:00
#include "alloc.h"
#include "util.h"
#include "putline.h"
1984-05-17 13:42:36 +00:00
#include "line.h"
#include "reg.h"
1984-05-17 13:42:36 +00:00
#include "lookup.h"
#include "proinf.h"
#include "ext.h"
/* External definitions */
extern void flow(void);
extern void backward(void);
extern int peephole(void);
1984-05-17 13:42:36 +00:00
static void relabel(void)
{
register num_p *npp, np, tp;
register num_p repl, ttp;
1984-05-17 13:42:36 +00:00
/*
* For each label find its final destination after crossjumping.
* Care has to be taken to prevent a loop in the program to
* cause same in the optimizer.
*/
for (npp = curpro.numhash; npp < &curpro.numhash[NNUMHASH]; npp++)
for (np = *npp; np != (num_p) 0; np = np->n_next)
{
assert(
! np->n_line || ((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)
1984-05-17 13:42:36 +00:00
tp->n_flags |= NUMMARK;
repl = tp->n_repl;
for (tp = np; tp->n_flags & NUMMARK; tp = ttp)
{
1984-05-17 13:42:36 +00:00
ttp = tp->n_repl;
tp->n_repl = repl;
tp->n_flags &= ~ NUMMARK;
tp->n_flags |= NUMKNOWN;
1984-05-17 13:42:36 +00:00
}
}
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);
np->n_jumps = 0;
}
1984-05-17 13:42:36 +00:00
}
static void symknown(void)
{
register sym_p *spp, sp;
1984-05-17 13:42:36 +00:00
for (spp = symhash; spp < &symhash[NSYMHASH]; spp++)
for (sp = *spp; sp != (sym_p) 0; sp = sp->s_next)
if (sp->s_flags & SYMSEEN)
sp->s_flags |= SYMKNOWN;
1984-05-17 13:42:36 +00:00
}
static void cleanlocals(void)
{
register num_p *npp, np, tp;
1984-05-17 13:42:36 +00:00
for (npp = curpro.numhash; npp < &curpro.numhash[NNUMHASH]; npp++)
{
1984-05-17 13:42:36 +00:00
np = *npp;
while (np != (num_p) 0)
{
1984-05-17 13:42:36 +00:00
tp = np->n_next;
oldnum(np);
np = tp;
}
*npp = (num_p) 0;
}
}
static void checklocs(void)
{
register num_p *npp, np;
1984-05-17 13:42:36 +00:00
for (npp = curpro.numhash; npp < &curpro.numhash[NNUMHASH]; npp++)
for (np = *npp; np != (num_p) 0; np = np->n_next)
1984-05-17 13:42:36 +00:00
if (np->n_line == (line_p) 0)
error("local label %u undefined", (unsigned) np->n_number);
1984-05-17 13:42:36 +00:00
}
static offset align(offset count, offset alignment)
{
1984-05-17 13:42:36 +00:00
assert(alignment == 1 || alignment == 2 || alignment == 4);
return ((count + alignment - 1) & ~(alignment - 1));
1984-05-17 13:42:36 +00:00
}
static void symvalue(void)
{
1984-05-17 13:42:36 +00:00
register line_p lp;
register sym_p sp;
register arg_p ap;
register argb_p abp;
short curfrag = 0;
offset count;
for (lp = pseudos; lp != (line_p) 0; lp = lp->l_next)
switch (lp->l_instr & BMASK)
{
default:
assert(FALSE);
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;
}
1984-05-17 13:42:36 +00:00
}
}
1990-07-18 14:33:07 +00:00
static void do_tes(void)
1990-07-18 14:33:07 +00:00
{
1991-02-01 10:21:39 +00:00
register line_p insptr = instrs, oldlin = NULL, oldlin2 = NULL;
1990-07-18 14:33:07 +00:00
init_state();
tes_pseudos();
while (insptr != NULL)
{
1991-02-01 10:21:39 +00:00
tes_instr(insptr, oldlin, oldlin2);
oldlin2 = oldlin;
oldlin = insptr;
insptr = insptr->l_next;
1990-07-18 14:33:07 +00:00
}
}
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
}