ack/util/ego/il/il.c

376 lines
8.7 KiB
C
Raw Normal View History

1994-06-24 11:31:16 +00:00
/* $Id$ */
1987-03-09 19:15:41 +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".
*/
1984-11-26 14:14:55 +00:00
/* I N L I N E S U B S T I T U T I O N */
#include <stdlib.h>
1984-11-26 14:14:55 +00:00
#include <stdio.h>
#include <string.h>
#include <unistd.h>
1991-03-05 12:44:05 +00:00
#include <em_path.h>
#include <em_mnem.h>
#include <em_pseu.h>
1984-11-26 14:14:55 +00:00
#include "../share/types.h"
#include "il.h"
#include "../share/debug.h"
#include "../share/alloc.h"
#include "../share/global.h"
#include "../share/lset.h"
#include "../share/files.h"
#include "../share/map.h"
#include "il_aux.h"
1984-11-26 14:14:55 +00:00
#include "il1_anal.h"
#include "il1_aux.h"
1984-11-26 14:14:55 +00:00
#include "il2_aux.h"
#include "il3_change.h"
1984-11-26 14:14:55 +00:00
#include "il3_subst.h"
#include "../share/get.h"
#include "../share/put.h"
#include "../share/go.h"
#include <limits.h>
1984-11-26 14:14:55 +00:00
int calnr;
int complete_program;
2016-08-21 17:38:54 +00:00
calcnt_p cchead; /* call-count info of current proc */
STATIC long space = 0;
STATIC long total_size = 0;
1984-11-26 14:14:55 +00:00
2022-07-16 21:27:57 +00:00
STATIC char cname[PATH_MAX];
STATIC char ccname[PATH_MAX];
STATIC char cname2[PATH_MAX];
1984-11-26 14:14:55 +00:00
/* For debugging only */
2022-07-16 21:27:57 +00:00
STATIC char sname[PATH_MAX];
1991-03-05 12:44:05 +00:00
STATIC int kp_temps = 0;
1984-11-26 14:14:55 +00:00
int Ssubst;
#ifdef VERBOSE
2016-08-21 17:38:54 +00:00
int Senv, Srecursive, Slocals, Sinstrlab, Sparsefails, Spremoved, Scals;
int Sbig_caller, Sdispensable, Schangedcallee, Sbigcallee, Sspace, Szeroratio;
1984-11-26 14:14:55 +00:00
#endif
/* P A S S 1
*
* Pass 1 reads and analyses the EM text and the CFG.
* It determines for every procedure if it may be expanded
* in line and how it uses its formal parameters.
* It also collects all calls appearing in the program and
* recognizes the actual parameters of every call.
* The call descriptors are put in a file (calfile).
*/
STATIC void pass1(const char *lnam, const char *bnam, const char *cnam)
1984-11-26 14:14:55 +00:00
{
2016-08-21 17:38:54 +00:00
FILE* f, *gf, *cf, *ccf; /* The EM input, the basic block graph,
1984-11-26 14:14:55 +00:00
* the call-list file and the calcnt file.
*/
long laddr;
bblock_p g;
short kind;
line_p l;
2016-08-21 17:38:54 +00:00
f = openfile(lnam, "r");
gf = openfile(bnam, "r");
cf = openfile(cnam, "w");
ccf = openfile(ccname, "w");
1984-11-26 14:14:55 +00:00
mesregs = Lempty_set();
apriori(fproc);
/* use information from the procedure table to
* see which calls certainly cannot be expanded.
*/
2016-08-21 17:38:54 +00:00
while (TRUE)
{
1984-11-26 14:14:55 +00:00
laddr = ftell(f);
2016-08-21 17:38:54 +00:00
if (!getunit(gf, f, &kind, &g, &l, &curproc, TRUE))
break;
1984-11-26 14:14:55 +00:00
/* Read the control flow graph and EM text of
* one procedure and analyze it.
*/
2016-08-21 17:38:54 +00:00
if (kind == LDATA)
{
remunit(LDATA, (proc_p)0, l);
1984-11-26 14:14:55 +00:00
continue;
}
2016-08-21 17:38:54 +00:00
OUTTRACE("flow graph of proc %d read", curproc->p_id);
1984-11-26 14:14:55 +00:00
assert(INSTR(g->b_start) == ps_pro);
curproc->p_start = g;
curproc->P_LADDR = laddr;
/* address of em text in em-file */
/* address of graph in basic block file */
curproc->P_SIZE = proclength(curproc); /* #instructions */
total_size += curproc->P_SIZE;
2016-08-21 17:38:54 +00:00
if (BIG_PROC(curproc))
{
1984-11-26 14:14:55 +00:00
/* curproc is too large to be expanded in line */
UNSUITABLE(curproc);
}
calnr = 0;
2016-08-21 17:38:54 +00:00
anal_proc(curproc, cf, ccf);
OUTTRACE("proc %d processed", curproc->p_id);
remunit(LTEXT, curproc, (line_p)0);
1984-11-26 14:14:55 +00:00
/* remove control flow graph + text */
2016-08-21 17:38:54 +00:00
OUTTRACE("graph of proc %d removed", curproc->p_id);
1984-11-26 14:14:55 +00:00
Ldeleteset(mesregs);
mesregs = Lempty_set();
}
fclose(f);
fclose(gf);
fclose(cf);
2016-08-21 17:38:54 +00:00
fclose(ccf);
1984-11-26 14:14:55 +00:00
}
/* P A S S 2
*
* Pass 2 reads the calfile and determines which calls should
* be expanded in line. It does not use the EM text.
*/
STATIC void pass2(const char *cnam, long space)
1984-11-26 14:14:55 +00:00
{
2016-08-21 17:38:54 +00:00
FILE* cf, *cf2, *ccf;
call_p c, a;
cf = openfile(cnam, "r");
cf2 = openfile(cname2, "w");
ccf = openfile(ccname, "r");
while ((c = getcall(cf)) != (call_p)0)
{
1984-11-26 14:14:55 +00:00
/* process all calls */
2016-08-21 17:38:54 +00:00
if (SUITABLE(c->cl_proc) && anal_params(c))
{
1984-11-26 14:14:55 +00:00
/* called proc. may be put in line */
/* see which parameters may be put in line */
assign_ratio(c); /* assign a rank */
a = abstract(c); /* abstract essential info */
2016-08-21 17:38:54 +00:00
append_abstract(a, a->cl_caller);
1984-11-26 14:14:55 +00:00
/* put it in call-list of calling proc. */
2016-08-21 17:38:54 +00:00
putcall(c, cf2, (short)0);
}
else
{
1984-11-26 14:14:55 +00:00
rem_call(c);
}
}
2016-08-21 17:38:54 +00:00
select_calls(fproc, ccf, space);
fclose(cf);
if (!kp_temps)
unlink(cnam);
1984-11-26 14:14:55 +00:00
fclose(cf2);
2016-08-21 17:38:54 +00:00
fclose(ccf);
if (!kp_temps)
unlink(ccname);
cf2 = openfile(cname2, "r");
add_actuals(fproc, cf2);
1984-11-26 14:14:55 +00:00
cleancals(fproc); /* remove calls that were not selected */
/* add actual parameters to each selected call */
2016-08-21 17:38:54 +00:00
fclose(cf2);
if (!kp_temps)
unlink(cname2);
1984-11-26 14:14:55 +00:00
}
/* P A S S 3
*
* pass 3 reads the substitution file and performs all
* substitutions described in that file. It reads the
* original EM text and produced a new (optimized)
* EM textfile.
*/
void pass3(const char *lnam, const char *lnam2)
1984-11-26 14:14:55 +00:00
{
bool verbose = TRUE;
2016-08-21 17:38:54 +00:00
FILE* lfile, *lfilerand, *lfile2, *sfile;
call_p c, next;
line_p l, startscan, cal;
1984-11-26 14:14:55 +00:00
short lastcid; /* last call-id seen */
lfile = openfile(lnam, "r");
lfilerand = openfile(lnam, "r");
2016-08-21 17:38:54 +00:00
lfile2 = openfile(lnam2, "w");
if (verbose)
{
sfile = openfile(sname, "w");
1984-11-26 14:14:55 +00:00
}
mesregs = Lempty_set();
2016-08-21 17:38:54 +00:00
while ((l = get_text(lfile, &curproc)) != (line_p)0)
{
if (curproc == (proc_p)0)
{
1984-11-26 14:14:55 +00:00
/* Just a data-unit; no real instructions */
2016-08-21 17:38:54 +00:00
putlines(l->l_next, lfile2);
1984-11-26 14:14:55 +00:00
oldline(l);
continue;
}
2016-08-21 17:38:54 +00:00
if (IS_DISPENSABLE(curproc))
{
liquidate(curproc, l->l_next);
}
else
{
1984-11-26 14:14:55 +00:00
startscan = l->l_next;
lastcid = 0;
2016-08-21 17:38:54 +00:00
for (c = curproc->P_CALS; c != (call_p)0; c = next)
{
1984-11-26 14:14:55 +00:00
next = c->cl_cdr;
2016-08-21 17:38:54 +00:00
cal = scan_to_cal(startscan, c->cl_id - lastcid);
assert(cal != (line_p)0);
startscan = scan_to_cal(cal->l_next, 1);
1984-11-26 14:14:55 +00:00
/* next CAL */
lastcid = c->cl_id;
/* next CAL after current one */
2016-08-21 17:38:54 +00:00
substitute(lfilerand, c, cal, l->l_next);
if (verbose)
{
putcall(c, sfile, 0);
}
else
{
1984-11-26 14:14:55 +00:00
rem_call(c);
}
}
}
2016-08-21 17:38:54 +00:00
putlines(l->l_next, lfile2);
1984-11-26 14:14:55 +00:00
Ldeleteset(mesregs);
mesregs = Lempty_set();
oldline(l);
}
fclose(lfile);
fclose(lfile2);
2016-08-21 17:38:54 +00:00
if (verbose)
{
1984-11-26 14:14:55 +00:00
fclose(sfile);
2016-08-21 17:38:54 +00:00
if (!kp_temps)
unlink(sname);
1984-11-26 14:14:55 +00:00
}
}
STATIC void il_extptab(ptab)
2016-08-21 17:38:54 +00:00
proc_p ptab;
1984-11-26 14:14:55 +00:00
{
/* Allocate space for extension of proctable entries.
* Also, initialise some of the fields just allocated.
*/
register proc_p p;
2016-08-21 17:38:54 +00:00
for (p = ptab; p != (proc_p)0; p = p->p_next)
{
1984-11-26 14:14:55 +00:00
p->p_extend = newilpx();
p->P_ORGLABELS = p->p_nrlabels;
p->P_ORGLOCALS = p->p_localbytes;
}
}
STATIC void il_cleanptab(ptab)
2016-08-21 17:38:54 +00:00
proc_p ptab;
1984-11-26 14:14:55 +00:00
{
/* De-allocate space for extensions */
register proc_p p;
2016-08-21 17:38:54 +00:00
for (p = ptab; p != (proc_p)0; p = p->p_next)
{
1984-11-26 14:14:55 +00:00
oldilpx(p->p_extend);
}
}
#ifdef VERBOSE
STATIC void Sdiagnostics()
1984-11-26 14:14:55 +00:00
{
/* print statictical information */
2016-08-21 17:38:54 +00:00
fprintf(stderr, "STATISTICS:\n");
fprintf(stderr, "Info about procedures:\n");
fprintf(stderr, "environment accessed: %d\n", Senv);
fprintf(stderr, "recursive: %d\n", Srecursive);
fprintf(stderr, "too many locals: %d\n", Slocals);
fprintf(stderr, "instr. lab in data block: %d\n", Sinstrlab);
fprintf(stderr, "procedures removed: %d\n", Spremoved);
fprintf(stderr, "\nInfo about calls:\n");
fprintf(stderr, "total number of calls: %d\n", Scals);
fprintf(stderr, "total number of calls substituted: %d\n", Ssubst);
fprintf(stderr, "parser failed: %d\n", Sparsefails);
fprintf(stderr, "caller too big: %d\n", Sbig_caller);
fprintf(stderr, "caller dispensable: %d\n", Sdispensable);
fprintf(stderr, "callee is changed: %d\n", Schangedcallee);
fprintf(stderr, "callee too big: %d\n", Sbigcallee);
fprintf(stderr, "no space available: %d\n", Sspace);
fprintf(stderr, "zero ratio: %d\n", Szeroratio);
1984-11-26 14:14:55 +00:00
}
#endif
void il_flags(void *vp)
1984-11-26 14:14:55 +00:00
{
char *p = vp;
2016-08-21 17:38:54 +00:00
switch (*p++)
{
case 's':
while (*p != '\0')
{
space = 10 * space + *p++ - '0';
}
break;
case 'a':
complete_program = 1;
break;
case 't':
strcpy(cname, ".");
strcpy(ccname, ".");
strcpy(sname, ".");
strcpy(cname2, ".");
kp_temps = 1;
break;
1984-11-26 14:14:55 +00:00
}
}
int main(argc, argv) int argc;
2016-08-21 17:38:54 +00:00
char* argv[];
1984-11-26 14:14:55 +00:00
{
struct files* files = findfiles(argc, argv);
2016-08-21 17:38:54 +00:00
FILE* f;
char* tmpdir = getenv("TMPDIR");
2016-08-21 17:38:54 +00:00
go(argc, argv, no_action, no_action, no_action, il_flags);
1984-11-26 14:14:55 +00:00
il_extptab(fproc); /* add extended data structures */
if (!tmpdir)
tmpdir = "/tmp";
strcpy(cname, tmpdir);
strcpy(ccname, tmpdir);
strcpy(sname, tmpdir);
strcpy(cname2, tmpdir);
1991-03-05 12:44:05 +00:00
strcat(cname, "/ego.i1.XXXXXX");
strcat(ccname, "/ego.i2.XXXXXX");
strcat(sname, "/ego.i3.XXXXXX");
strcat(cname2, "/ego.i4.XXXXXX");
close(mkstemp(cname));
close(mkstemp(ccname));
close(mkstemp(sname));
close(mkstemp(cname2));
pass1(files->lname_in, files->bname_in, cname); /* grep calls, analyse procedures */
2016-08-21 17:38:54 +00:00
space = total_size * space / 100;
pass2(cname, space); /* select calls to be expanded */
pass3(files->lname_in, files->lname_out); /* do substitutions */
f = openfile(files->dname_out, "w");
1984-11-26 14:14:55 +00:00
il_cleanptab(fproc); /* remove extended data structures */
2016-08-21 17:38:54 +00:00
putdtable(fdblock, f);
f = openfile(files->pname_out, "w");
2016-08-21 17:38:54 +00:00
putptable(fproc, f, FALSE);
report("inline substitutions", Ssubst);
1984-11-26 14:14:55 +00:00
#ifdef VERBOSE
2016-08-21 17:38:54 +00:00
if (verbose_flag)
{
1984-11-26 14:14:55 +00:00
Sdiagnostics();
}
#endif
#ifdef DEBUG
core_usage();
#endif
exit(0);
}