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:35:32 +00:00
|
|
|
/*
|
|
|
|
* C O M P A C T A S S E M B L Y L A N G U A G E G E N E R A T I O N
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-07-22 00:46:16 +00:00
|
|
|
#include <stdlib.h>
|
1984-11-26 14:35:32 +00:00
|
|
|
#include <stdio.h>
|
2006-07-22 00:46:16 +00:00
|
|
|
#include <string.h>
|
1991-03-05 12:16:17 +00:00
|
|
|
#include <em_pseu.h>
|
|
|
|
#include <em_mes.h>
|
1984-11-26 14:35:32 +00:00
|
|
|
#include "../share/types.h"
|
|
|
|
#include "ca.h"
|
|
|
|
#include "../share/debug.h"
|
|
|
|
#include "../share/global.h"
|
|
|
|
#include "../share/lset.h"
|
|
|
|
#include "../share/files.h"
|
|
|
|
#include "../share/map.h"
|
|
|
|
#include "../share/alloc.h"
|
|
|
|
#include "../share/get.h"
|
|
|
|
#include "ca_put.h"
|
|
|
|
|
|
|
|
/* This phase transforms the Intermediate Code of the global optimizer
|
|
|
|
* to 'standard' compact assembly language, which will be processed
|
|
|
|
* by the code generator.
|
|
|
|
*/
|
|
|
|
|
2016-08-21 17:38:02 +00:00
|
|
|
short dlength;
|
|
|
|
dblock_p* dmap;
|
1984-11-26 14:35:32 +00:00
|
|
|
|
2016-08-21 17:38:02 +00:00
|
|
|
char** dnames, **pnames; /* Dynamically allocated arrays of strings.
|
1984-11-26 14:35:32 +00:00
|
|
|
* pnames[i] contains a pointer to the name
|
|
|
|
* of the procedure with proc_id i.
|
|
|
|
*/
|
|
|
|
|
2016-08-21 17:38:02 +00:00
|
|
|
STATIC line_p get_ca_lines(lf, p_out)
|
|
|
|
FILE* lf;
|
|
|
|
proc_p* p_out;
|
1984-11-29 10:31:44 +00:00
|
|
|
{
|
|
|
|
/* Read lines of EM text and link them.
|
|
|
|
* Register messages are outputted immediately after the PRO.
|
|
|
|
*/
|
|
|
|
|
|
|
|
line_p head, *pp, l;
|
|
|
|
line_p headm, *mp;
|
|
|
|
arg_p a;
|
|
|
|
|
|
|
|
curinp = lf; /* EM input file */
|
|
|
|
pp = &head;
|
|
|
|
mp = &headm;
|
2016-08-21 17:38:02 +00:00
|
|
|
headm = (line_p)0;
|
|
|
|
while (TRUE)
|
|
|
|
{
|
1984-11-29 10:31:44 +00:00
|
|
|
l = read_line(p_out);
|
2016-08-21 17:38:02 +00:00
|
|
|
if (feof(curinp))
|
|
|
|
break;
|
|
|
|
assert(l != (line_p)0);
|
|
|
|
if (INSTR(l) == ps_end && INSTR(head) != ps_pro)
|
|
|
|
{
|
1984-11-29 10:31:44 +00:00
|
|
|
/* Delete end pseudo after data-unit */
|
|
|
|
oldline(l);
|
|
|
|
break;
|
|
|
|
}
|
2016-08-21 17:38:02 +00:00
|
|
|
if (INSTR(l) == ps_mes && l->l_a.la_arg->a_a.a_offset == ms_reg)
|
|
|
|
{
|
1984-11-29 10:31:44 +00:00
|
|
|
/* l is a register message */
|
2016-08-21 17:38:02 +00:00
|
|
|
if (l->l_a.la_arg->a_next == (arg_p)0)
|
|
|
|
{
|
1984-11-29 10:31:44 +00:00
|
|
|
/* register message without arguments */
|
|
|
|
oldline(l);
|
2018-03-12 00:10:13 +00:00
|
|
|
continue;
|
2016-08-21 17:38:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1984-11-29 10:31:44 +00:00
|
|
|
*mp = l;
|
|
|
|
mp = &l->l_next;
|
|
|
|
}
|
2016-08-21 17:38:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1984-11-29 10:31:44 +00:00
|
|
|
*pp = l;
|
|
|
|
pp = &l->l_next;
|
|
|
|
}
|
2016-08-21 17:38:02 +00:00
|
|
|
if (INSTR(l) == ps_end)
|
|
|
|
{
|
1984-11-29 10:31:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-08-21 17:38:02 +00:00
|
|
|
*pp = (line_p)0;
|
|
|
|
if (head != (line_p)0 && INSTR(head) == ps_pro)
|
|
|
|
{
|
1984-11-29 10:31:44 +00:00
|
|
|
/* append register message without arguments to list */
|
|
|
|
l = newline(OPLIST);
|
|
|
|
l->l_instr = ps_mes;
|
|
|
|
a = ARG(l) = newarg(ARGOFF);
|
|
|
|
a->a_a.a_offset = ms_reg;
|
|
|
|
*mp = l;
|
|
|
|
l->l_next = head->l_next;
|
|
|
|
head->l_next = headm;
|
2016-08-21 17:38:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(headm == (line_p)0);
|
1984-11-29 10:31:44 +00:00
|
|
|
}
|
|
|
|
return head;
|
|
|
|
}
|
1984-11-26 14:35:32 +00:00
|
|
|
|
|
|
|
STATIC int makedmap(dbl)
|
2016-08-21 17:38:02 +00:00
|
|
|
dblock_p dbl;
|
1984-11-26 14:35:32 +00:00
|
|
|
{
|
|
|
|
/* construct the dmap table */
|
|
|
|
|
|
|
|
dblock_p d;
|
|
|
|
int cnt;
|
|
|
|
|
|
|
|
/* determine the length of the table */
|
|
|
|
|
|
|
|
cnt = 0;
|
2016-08-21 17:38:02 +00:00
|
|
|
for (d = dbl; d != (dblock_p)0; d = d->d_next)
|
|
|
|
cnt++;
|
|
|
|
dmap = (dblock_p*)newmap(cnt);
|
|
|
|
for (d = dbl; d != (dblock_p)0; d = d->d_next)
|
|
|
|
{
|
1988-09-02 13:52:11 +00:00
|
|
|
assert(d->d_id <= cnt);
|
1984-11-26 14:35:32 +00:00
|
|
|
dmap[d->d_id] = d;
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2016-11-10 21:04:18 +00:00
|
|
|
STATIC void getdnames(dumpd)
|
2016-08-21 17:38:02 +00:00
|
|
|
FILE* dumpd;
|
1984-11-26 14:35:32 +00:00
|
|
|
{
|
|
|
|
/* Read the names of the datalabels from
|
|
|
|
* the dump file.
|
|
|
|
*/
|
|
|
|
|
2016-08-21 17:38:02 +00:00
|
|
|
char str[IDL + 1];
|
1984-11-26 14:35:32 +00:00
|
|
|
int id;
|
|
|
|
|
2016-08-21 17:38:02 +00:00
|
|
|
dnames = (char**)newmap(dlength);
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (fscanf(dumpd, "%d %s", &id, str) == EOF)
|
|
|
|
return;
|
1984-11-26 14:35:32 +00:00
|
|
|
assert(id <= dlength);
|
2016-08-21 17:38:02 +00:00
|
|
|
dnames[id] = (char*)newcore(strlen(str) + 1);
|
1987-03-25 16:24:41 +00:00
|
|
|
strcpy(dnames[id], str);
|
1984-11-26 14:35:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 21:04:18 +00:00
|
|
|
STATIC void getpnames(dumpp)
|
2016-08-21 17:38:02 +00:00
|
|
|
FILE* dumpp;
|
1984-11-26 14:35:32 +00:00
|
|
|
{
|
|
|
|
/* Read the names of the procedures from
|
|
|
|
* the dump file.
|
|
|
|
*/
|
|
|
|
|
2016-08-21 17:38:02 +00:00
|
|
|
char str[IDL + 1];
|
1984-11-26 14:35:32 +00:00
|
|
|
int id;
|
|
|
|
|
2016-08-21 17:38:02 +00:00
|
|
|
pnames = (char**)newmap(plength);
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (fscanf(dumpp, "%d %s", &id, str) == EOF)
|
|
|
|
return;
|
1984-11-26 14:35:32 +00:00
|
|
|
assert(id <= plength);
|
2016-08-21 17:38:02 +00:00
|
|
|
pnames[id] = (char*)newcore(strlen(str) + 1);
|
1987-03-25 16:24:41 +00:00
|
|
|
strcpy(pnames[id], str);
|
1984-11-26 14:35:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-21 17:38:02 +00:00
|
|
|
STATIC new_name(s) char** s;
|
1984-11-26 14:35:32 +00:00
|
|
|
{
|
1990-10-08 11:44:15 +00:00
|
|
|
static int nn = 0;
|
1987-06-29 14:41:23 +00:00
|
|
|
char buf[20];
|
|
|
|
int len = strlen(*s);
|
|
|
|
|
2016-08-21 17:38:02 +00:00
|
|
|
oldcore(*s, len + 1);
|
1987-06-29 14:41:23 +00:00
|
|
|
buf[0] = '_';
|
|
|
|
buf[1] = 'I';
|
|
|
|
buf[2] = 'I';
|
2016-08-21 17:38:02 +00:00
|
|
|
sprintf(&buf[3], "%d", nn);
|
1984-11-26 14:35:32 +00:00
|
|
|
nn++;
|
2016-08-21 17:38:02 +00:00
|
|
|
*s = (char*)newcore(strlen(buf) + 1);
|
1987-06-29 14:41:23 +00:00
|
|
|
strcpy(*s, buf);
|
1984-11-26 14:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC uniq_names()
|
|
|
|
{
|
|
|
|
/* The names of all internal procedures and data blocks
|
|
|
|
* are made different. As the optimizer combines several
|
|
|
|
* modules into one, there may be name conflicts between
|
|
|
|
* procedures or data blocks that were internal in
|
|
|
|
* different source modules.
|
|
|
|
*/
|
|
|
|
|
|
|
|
proc_p p;
|
|
|
|
dblock_p d;
|
|
|
|
|
2016-08-21 17:38:02 +00:00
|
|
|
for (p = fproc; p != (proc_p)0; p = p->p_next)
|
|
|
|
{
|
|
|
|
if (!(p->p_flags1 & PF_EXTERNAL))
|
|
|
|
{
|
1987-06-29 14:41:23 +00:00
|
|
|
new_name(&(pnames[p->p_id]));
|
1984-11-26 14:35:32 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-21 17:38:02 +00:00
|
|
|
for (d = fdblock; d != (dblock_p)0; d = d->d_next)
|
|
|
|
{
|
|
|
|
if (!(d->d_flags1 & DF_EXTERNAL) && dnames[d->d_id])
|
|
|
|
{
|
1987-06-29 14:41:23 +00:00
|
|
|
new_name(&(dnames[d->d_id]));
|
1984-11-26 14:35:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1990-10-08 11:44:15 +00:00
|
|
|
|
2016-08-21 17:38:02 +00:00
|
|
|
main(argc, argv) int argc;
|
|
|
|
char* argv[];
|
1984-11-26 14:35:32 +00:00
|
|
|
{
|
|
|
|
/* CA does not output proctable etc. files. Instead, its
|
2016-08-21 20:01:19 +00:00
|
|
|
* pname_out and dname_out arguments contain the names of the
|
1984-11-26 14:35:32 +00:00
|
|
|
* dump files created by IC.
|
|
|
|
*/
|
2016-08-21 20:01:19 +00:00
|
|
|
struct files* files = findfiles(argc, argv);
|
|
|
|
|
2016-08-21 17:38:02 +00:00
|
|
|
FILE* f, *f2; /* The EM input and output. */
|
|
|
|
FILE* df, *pf; /* The dump files */
|
1984-11-26 14:35:32 +00:00
|
|
|
line_p lnp;
|
|
|
|
|
2016-08-21 20:01:19 +00:00
|
|
|
/* The names of the input files of every phase are passed as
|
|
|
|
* arguments to the phase. First come the input file names,
|
|
|
|
* then the output file names. We use a one-letter convention
|
|
|
|
* to denote the type of file:
|
|
|
|
* p: procedure table file
|
|
|
|
* d: data table file
|
|
|
|
* l: EM text file (lines of EM instructions)
|
|
|
|
* b: basic block file (Control Flow Graph file)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* The input file names */
|
|
|
|
|
|
|
|
char* pname_in = argv[1];
|
|
|
|
char* dname_in = argv[2];
|
|
|
|
char* lname_in = argv[3];
|
|
|
|
char* bname_in = argv[4];
|
|
|
|
|
|
|
|
/* The output file names */
|
|
|
|
|
|
|
|
char* pname_out = argv[5];
|
|
|
|
char* dname_out = argv[6];
|
|
|
|
char* lname_out = argv[7];
|
|
|
|
char* bname_out = argv[8];
|
|
|
|
|
|
|
|
fproc = getptable(pname_in); /* proc table */
|
|
|
|
fdblock = getdtable(dname_in); /* data block table */
|
1984-11-26 14:35:32 +00:00
|
|
|
dlength = makedmap(fdblock); /* allocate dmap table */
|
2016-08-21 20:01:19 +00:00
|
|
|
df = openfile(dname_out, "r");
|
1984-11-26 14:35:32 +00:00
|
|
|
getdnames(df);
|
|
|
|
fclose(df);
|
2016-08-21 20:01:19 +00:00
|
|
|
pf = openfile(pname_out, "r");
|
1984-11-26 14:35:32 +00:00
|
|
|
getpnames(pf);
|
|
|
|
fclose(pf);
|
|
|
|
uniq_names();
|
2016-08-21 20:01:19 +00:00
|
|
|
f = openfile(lname_in, "r");
|
1984-11-26 14:35:32 +00:00
|
|
|
f2 = stdout;
|
|
|
|
cputmagic(f2); /* write magic number */
|
2016-08-21 17:38:02 +00:00
|
|
|
while ((lnp = get_ca_lines(f, &curproc)) != (line_p)0)
|
|
|
|
{
|
|
|
|
cputlines(lnp, f2);
|
1984-11-26 14:35:32 +00:00
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
fclose(f2);
|
|
|
|
exit(0);
|
|
|
|
}
|