2019-05-10 17:17:40 +00:00
|
|
|
/*
|
|
|
|
* (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
|
|
|
|
*/
|
1984-05-17 13:42:36 +00:00
|
|
|
#include "param.h"
|
|
|
|
#include "types.h"
|
1990-09-04 16:42:43 +00:00
|
|
|
#include "tes.h"
|
1988-09-12 09:13:49 +00:00
|
|
|
#include <em_flag.h>
|
|
|
|
#include <em_spec.h>
|
|
|
|
#include <em_mnem.h>
|
1984-05-17 13:42:36 +00:00
|
|
|
#include "alloc.h"
|
|
|
|
#include "line.h"
|
|
|
|
#include "proinf.h"
|
|
|
|
#include "optim.h"
|
|
|
|
#include "ext.h"
|
|
|
|
|
2019-05-10 17:17:40 +00:00
|
|
|
/* Forward declarations */
|
|
|
|
static void reach(register line_p);
|
|
|
|
static void findreach(void);
|
|
|
|
static void cleaninstrs(void);
|
1984-05-17 13:42:36 +00:00
|
|
|
|
2019-05-10 17:17:40 +00:00
|
|
|
void flow(void)
|
|
|
|
{
|
|
|
|
findreach(); /* determine reachable labels */
|
|
|
|
cleaninstrs(); /* throw away unreachable code */
|
1984-05-17 13:42:36 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 17:17:40 +00:00
|
|
|
static void findreach(void)
|
|
|
|
{
|
|
|
|
register num_p *npp, np;
|
1984-05-17 13:42:36 +00:00
|
|
|
|
|
|
|
reach(instrs);
|
2019-05-10 17:17:40 +00:00
|
|
|
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)
|
|
|
|
{
|
1984-05-17 13:42:36 +00:00
|
|
|
np->n_repl->n_flags |= NUMREACH;
|
|
|
|
np->n_repl->n_jumps++;
|
2019-05-10 17:17:40 +00:00
|
|
|
if (!(np->n_flags & NUMSCAN))
|
|
|
|
{
|
1984-05-17 13:42:36 +00:00
|
|
|
np->n_flags |= NUMSCAN;
|
2019-05-10 17:17:40 +00:00
|
|
|
if (np->n_line)
|
|
|
|
{
|
1989-02-06 14:39:08 +00:00
|
|
|
reach(np->n_line->l_next);
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-10 17:17:40 +00:00
|
|
|
if (!(np->n_repl->n_flags & NUMSCAN))
|
|
|
|
{
|
1989-02-06 14:39:08 +00:00
|
|
|
np->n_repl->n_flags |= NUMSCAN;
|
|
|
|
if (np->n_repl->n_line)
|
2019-05-10 17:17:40 +00:00
|
|
|
reach(np->n_repl->n_line->l_next);
|
1989-02-06 14:39:08 +00:00
|
|
|
}
|
1984-05-17 13:42:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 17:17:40 +00:00
|
|
|
static void reach(register line_p lnp)
|
|
|
|
{
|
1984-05-17 13:42:36 +00:00
|
|
|
register num_p np;
|
|
|
|
|
2019-05-10 17:17:40 +00:00
|
|
|
for (; lnp != (line_p) 0; lnp = lnp->l_next)
|
|
|
|
{
|
|
|
|
if (lnp->l_optyp == OPNUMLAB)
|
|
|
|
{
|
1984-05-17 13:42:36 +00:00
|
|
|
/*
|
|
|
|
* Branch instruction or label
|
|
|
|
*/
|
|
|
|
np = lnp->l_a.la_np;
|
2019-05-10 17:17:40 +00:00
|
|
|
if ((lnp->l_instr & BMASK) != op_lab)
|
1989-02-06 14:39:08 +00:00
|
|
|
lnp->l_a.la_np = np = np->n_repl;
|
1984-05-17 13:42:36 +00:00
|
|
|
np->n_flags |= NUMREACH;
|
2019-05-10 17:17:40 +00:00
|
|
|
if (!(np->n_flags & NUMSCAN))
|
|
|
|
{
|
1984-05-17 13:42:36 +00:00
|
|
|
np->n_flags |= NUMSCAN;
|
1989-02-06 14:39:08 +00:00
|
|
|
if (np->n_line)
|
|
|
|
reach(np->n_line->l_next);
|
2019-05-10 17:17:40 +00:00
|
|
|
else
|
|
|
|
{
|
1989-02-06 14:39:08 +00:00
|
|
|
np = np->n_repl;
|
|
|
|
np->n_flags |= NUMREACH;
|
2019-05-10 17:17:40 +00:00
|
|
|
if (!(np->n_flags & NUMSCAN))
|
|
|
|
{
|
1989-02-06 14:39:08 +00:00
|
|
|
np->n_flags |= NUMSCAN;
|
|
|
|
if (np->n_line)
|
|
|
|
reach(np->n_line->l_next);
|
|
|
|
}
|
|
|
|
}
|
1984-05-17 13:42:36 +00:00
|
|
|
}
|
2019-05-10 17:17:40 +00:00
|
|
|
if ((lnp->l_instr & BMASK) == op_lab)
|
1984-05-17 13:42:36 +00:00
|
|
|
return;
|
|
|
|
else
|
|
|
|
np->n_jumps++;
|
|
|
|
}
|
2019-05-10 17:17:40 +00:00
|
|
|
if ((lnp->l_instr & BMASK) > sp_lmnem)
|
|
|
|
continue;
|
|
|
|
if ((em_flag[(lnp->l_instr & BMASK) - sp_fmnem] & EM_FLO) == FLO_T)
|
1984-05-17 13:42:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 17:17:40 +00:00
|
|
|
static void cleaninstrs(void)
|
|
|
|
{
|
|
|
|
register line_p *lpp, lp, *lastbra;
|
|
|
|
bool reachable, superfluous;
|
1984-05-17 13:42:36 +00:00
|
|
|
int instr;
|
|
|
|
|
2019-05-10 17:17:40 +00:00
|
|
|
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)
|
|
|
|
{
|
1984-05-17 13:42:36 +00:00
|
|
|
reachable = TRUE;
|
2019-05-10 17:17:40 +00:00
|
|
|
if (lastbra != (line_p *) 0 && (*lastbra)->l_next == lp
|
|
|
|
&& (*lastbra)->l_a.la_np->n_repl == lp->l_a.la_np)
|
|
|
|
{
|
1984-05-17 13:42:36 +00:00
|
|
|
oldline(*lastbra);
|
|
|
|
OPTIM(O_BRALAB);
|
|
|
|
lpp = lastbra;
|
|
|
|
*lpp = lp;
|
1984-06-28 10:40:09 +00:00
|
|
|
lastbra = (line_p *) 0;
|
1984-05-17 13:42:36 +00:00
|
|
|
lp->l_a.la_np->n_jumps--;
|
|
|
|
}
|
|
|
|
}
|
2019-05-10 17:17:40 +00:00
|
|
|
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))
|
1984-05-17 13:42:36 +00:00
|
|
|
superfluous = TRUE;
|
|
|
|
else
|
|
|
|
superfluous = FALSE;
|
2019-05-10 17:17:40 +00:00
|
|
|
}
|
|
|
|
else
|
1984-05-17 13:42:36 +00:00
|
|
|
superfluous = FALSE;
|
2019-05-10 17:17:40 +00:00
|
|
|
if ((!reachable) || superfluous)
|
|
|
|
{
|
|
|
|
if (instr == op_lab)
|
|
|
|
{
|
1989-02-06 14:39:08 +00:00
|
|
|
lp->l_a.la_np->n_line = 0;
|
|
|
|
}
|
2019-05-10 17:17:40 +00:00
|
|
|
else if (instr > sp_lmnem)
|
|
|
|
{
|
1990-08-02 16:01:21 +00:00
|
|
|
/* leave pseudo's */
|
|
|
|
lpp = &lp->l_next;
|
|
|
|
continue;
|
|
|
|
}
|
1984-05-17 13:42:36 +00:00
|
|
|
lp = lp->l_next;
|
|
|
|
oldline(*lpp);
|
|
|
|
OPTIM(O_UNREACH);
|
|
|
|
*lpp = lp;
|
2019-05-10 17:17:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (instr <= sp_lmnem
|
|
|
|
&& (em_flag[instr - sp_fmnem] & EM_FLO) == FLO_T)
|
|
|
|
{
|
1984-05-17 13:42:36 +00:00
|
|
|
reachable = FALSE;
|
2019-05-10 17:17:40 +00:00
|
|
|
if ((lp->l_instr & BMASK) == op_bra)
|
1984-05-17 13:42:36 +00:00
|
|
|
lastbra = lpp;
|
|
|
|
}
|
|
|
|
lpp = &lp->l_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|