ack/util/ncgg/output.c

1054 lines
21 KiB
C
Raw Permalink Normal View History

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".
*/
/* #define CODEDEBUG *//* print readable code */
1985-01-08 09:59:28 +00:00
#ifdef CODEDEBUG
int code_in_c=0; /* put readable code in "code" */
int tabledebug=1; /* generate code for table debugging */
1985-01-08 09:59:28 +00:00
#else
int code_in_c = 1; /* put code in "tables.c" */
int tabledebug = 0; /* do not generate code for table debugging */
1985-01-08 09:59:28 +00:00
#endif
int verbose = 0; /* print all statistics */
int use_tes; /* use top element size information */
char *c_file = "tables.c";
char *h_file = "tables.H";
char *cd_file = "code";
1985-01-08 09:59:28 +00:00
#ifndef NORCSID
static char rcsid[] = "$Id$";
1985-01-08 09:59:28 +00:00
#endif
#include <assert.h>
1985-01-08 09:59:28 +00:00
#include <stdio.h>
#include <ctype.h>
#include "varinfo.h"
#include "param.h"
#include "reg.h"
#include "property.h"
#include "token.h"
#include "set.h"
#include "subr.h"
1985-01-08 09:59:28 +00:00
#include "instruct.h"
#include "lookup.h"
#include <cgg_cg.h>
#include "pseudo.h"
#include "regvar.h"
#include "extern.h"
#define BMASK 0xFF
#define BSHIFT 8
FILE *ctable, *htable;
1985-01-08 09:59:28 +00:00
FILE *code;
short *lineset;
int maxline;
extern int nstrings;
extern char *l_strings[];
extern int ninstances;
extern inst_t l_instances[];
extern int nmoves;
extern move_t l_moves[];
extern int ntests;
extern test_t l_tests[];
extern int nstacks;
extern c1_t l_stacks[];
extern int ncoercs;
extern c3_t l_coercs[];
extern int nsplit, maxsplit;
1985-01-08 09:59:28 +00:00
extern c2_t l_split[];
extern set_t l_sets[];
int maxallreg = 0;
int maxregvars = 0;
1985-01-08 09:59:28 +00:00
int setsize;
int prevind = 0;
int npatbytes = -1;
char pattern[MAXPATBYTES];
int pathash[256];
1985-01-08 09:59:28 +00:00
extern int npatterns;
extern int patindex[];
extern int empatlen;
extern int emmnem[];
extern int empatexpr;
extern node_t nodes[];
extern int nnodes;
extern char * filename;
/* Forward declarations */
static void patbyte(int);
static void patshort(int);
static void pat(int);
void opnfile(FILE **f, char *s)
{
1985-01-08 09:59:28 +00:00
if ((*f = fopen(s, "w")) == NULL)
fatal("Can't create %s", s);
}
1985-01-08 09:59:28 +00:00
void unlfile(FILE *f, char *s)
{
if (f)
fclose(f);
if (remove(s) < 0)
error("%s incorrect, must be removed!!", s);
1985-01-08 09:59:28 +00:00
}
void initio(void)
{
1985-01-08 09:59:28 +00:00
opnfile(&ctable, c_file);
opnfile(&htable, h_file);
1985-01-08 09:59:28 +00:00
if (code_in_c)
fprintf(ctable, "char coderules[] = {");
1985-01-08 09:59:28 +00:00
else
opnfile(&code, cd_file);
1985-01-08 09:59:28 +00:00
patbyte(0);
if (tabledebug)
lineset = (short *) myalloc(SZOFSET(MAXSOURCELINES) * sizeof(short));
1985-01-08 09:59:28 +00:00
}
void finishcode(void)
{
1985-01-08 09:59:28 +00:00
if (code_in_c)
fprintf(ctable, "\n};\n\n");
fprintf(ctable, "int allsetno = %d;\n", allsetno);
if (tabledebug)
{
FILE *fd;
1985-01-08 09:59:28 +00:00
int sz;
fd = fopen("lineset","wb");
if (fd != NULL)
{
sz = SZOFSET(maxline) * 2;
fwrite(&sz,1,sizeof(int),fd);
fwrite(lineset,1,sz,fd);
fclose(fd);
}
else
1985-01-08 09:59:28 +00:00
error("Can't create lineset");
}
}
void errorexit(void)
{
unlfile(ctable, c_file);
unlfile(htable, h_file);
1985-01-08 09:59:28 +00:00
if (!code_in_c)
unlfile(code, cd_file);
1985-01-08 09:59:28 +00:00
}
#ifdef CODEDEBUG
#define code8(x) fprintf(code,"%s","x")
#define code8nl(x) fprintf(code,"%s\n","x")
#define code53(x,y) fprintf(code,"%s-%d","x",y)
#define codeint(x) fprintf(code," %d",x)
#define codenl() fprintf(code,"\n")
#else
1987-02-05 19:20:54 +00:00
#define codenl()
#define code8nl(x) code8(x)
void code8(int x)
{
1985-01-08 09:59:28 +00:00
codeindex++;
if (code_in_c)
fprintf(ctable, "%d,", x & 0377);
1985-01-08 09:59:28 +00:00
else
putc(x, code);
1985-01-08 09:59:28 +00:00
}
void code53(int x, int y)
{
code8(x + (y << 5));
1985-01-08 09:59:28 +00:00
}
void codeint(int x)
{
assert(x >= 0 && x <= 32767);
if (x < 128)
{
1985-01-08 09:59:28 +00:00
code8(x);
}
else
{
code8(x / 256 + 128);
code8(x % 256);
1985-01-08 09:59:28 +00:00
}
}
#endif
void outpatterns(void)
{
register int i;
1985-01-08 09:59:28 +00:00
if (!inproc)
{
1985-01-08 09:59:28 +00:00
patbyte(0);
patshort(prevind);
prevind = npatbytes - 2;
1985-01-08 09:59:28 +00:00
patbyte(empatlen);
for (i = 0; i < empatlen; i++)
1985-01-08 09:59:28 +00:00
patbyte(emmnem[i]);
pat(empatexpr);
}
if (callproc == 0)
{
1985-01-08 09:59:28 +00:00
patbyte(npatterns);
for (i = 0; i < npatterns; i++)
1985-01-08 09:59:28 +00:00
pat(patindex[i]);
}
else
{
1985-01-08 09:59:28 +00:00
patbyte(0);
pat(callproc);
1989-11-09 11:04:18 +00:00
pat(nprocargs);
for (i = 0; i < nprocargs; i++)
pat(procarg[i]);
1985-01-08 09:59:28 +00:00
}
}
static void pat(int n)
{
1985-01-08 09:59:28 +00:00
assert(n >= 0);
if (n < 128)
1985-01-08 09:59:28 +00:00
patbyte(n);
else
{
patbyte(n / 256 + 128);
patbyte(n % 256);
1985-01-08 09:59:28 +00:00
}
}
static void patshort(int n)
{
1985-01-08 09:59:28 +00:00
patbyte(n % 256);
patbyte(n / 256);
1985-01-08 09:59:28 +00:00
}
static void patbyte(int n)
{
1985-01-08 09:59:28 +00:00
1987-01-16 13:51:42 +00:00
NEXT(npatbytes, MAXPATBYTES, "Pattern bytes");
pattern[npatbytes] = n;
1985-01-08 09:59:28 +00:00
}
void hashpatterns(void)
{
1985-01-08 09:59:28 +00:00
short index;
register char *bp, *tp;
1985-01-08 09:59:28 +00:00
register short i;
unsigned short hashvalue;
int patlen;
index = prevind;
while (index != 0)
{
1985-01-08 09:59:28 +00:00
bp = &pattern[index];
tp = &bp[PO_MATCH];
i = *tp++ & BMASK;
if (i == BMASK)
{
i = *tp++ & BMASK;
i |= (*tp++ & BMASK) << BSHIFT;
1985-01-08 09:59:28 +00:00
}
patlen = i;
hashvalue = 0;
switch (patlen)
{
default: /* 3 or more */
hashvalue = (hashvalue << 4) ^ (*tp++ & BMASK);
case 2:
hashvalue = (hashvalue << 4) ^ (*tp++ & BMASK);
case 1:
hashvalue = (hashvalue << 4) ^ (*tp++ & BMASK);
1985-01-08 09:59:28 +00:00
}
assert(hashvalue!= ILLHASH);
i = index;
index = (bp[PO_NEXT] & BMASK) | (bp[PO_NEXT + 1] << BSHIFT);
bp[PO_HASH] = hashvalue >> BSHIFT;
1985-01-08 09:59:28 +00:00
hashvalue &= BMASK;
bp[PO_NEXT] = pathash[hashvalue] & BMASK;
bp[PO_NEXT + 1] = pathash[hashvalue] >> BSHIFT;
1985-01-08 09:59:28 +00:00
pathash[hashvalue] = i;
}
}
void outincludes(void)
{
1985-01-08 09:59:28 +00:00
fprintf(ctable, "#include \"param.h\"\n");
fprintf(ctable, "#include \"tables.h\"\n");
fprintf(ctable, "#include \"types.h\"\n");
fprintf(ctable, "#include <cgg_cg.h>\n");
fprintf(ctable, "#include \"data.h\"\n");
1985-01-08 09:59:28 +00:00
}
void outregs(void)
{
register int i, j, k;
1985-01-08 09:59:28 +00:00
short rset[SZOFSET(MAXREGS)];
short clashlist[MAXREGS * MAXREGS];
int iclashlist = 0;
int t, ready;
fprintf(ctable, "char stregclass[] = {\n");
for (i = 0; i < nregs; i++)
fprintf(ctable, "\t%d,\n", l_regs[i].ri_class);
fprintf(ctable, "};\n\nstruct reginfo machregs[] = {\n{0},\n");
for (i = 1; i < nregs; i++)
{
fprintf(ctable, "{%d,%d", strlookup(l_regs[i].ri_repr),
l_regs[i].ri_size);
if (maxmembers != 0)
{
fprintf(ctable, ",{");
for (j = 0; j < maxmembers; j++)
fprintf(ctable, "%d,", l_regs[i].ri_memb[j]);
1985-01-08 09:59:28 +00:00
/* now compute and print set of registers
* that clashes with this register.
* A register clashes with al its children (and theirs)
* and with all their parents.
*/
for (j = 0; j < SZOFSET(MAXREGS); j++)
rset[j] = 0;
BIS(rset, i);
do
{
ready = 1;
for (j = 1; j < nregs; j++)
if (BIT(rset, j))
for (k = 0; k < 2; k++)
if ((t = l_regs[j].ri_memb[k]) != 0)
{
if (BIT(rset,t) == 0)
ready = 0;
BIS(rset, t);
}
1985-01-08 09:59:28 +00:00
} while (!ready);
do
{
ready = 1;
for (j = 1; j < nregs; j++)
for (k = 0; k < 2; k++)
if ((t = l_regs[j].ri_memb[k]) != 0)
if (BIT(rset, t))
{
if (BIT(rset,j) == 0)
ready = 0;
BIS(rset, j);
}
1985-01-08 09:59:28 +00:00
} while (!ready);
fprintf(ctable, "},{");
for (j = 0; j < SZOFSET(nregs); j++)
fprintf(ctable, "0%o,", rset[j] & 0xFFFF);
fprintf(ctable, "}, %d", iclashlist);
for (j = 1; j < nregs; j++)
{
if (BIT(rset, j))
clashlist[iclashlist++] = j;
}
clashlist[iclashlist++] = 0;
1985-01-08 09:59:28 +00:00
}
/*
* Write .r_refcount = 1 for register variables or 0
* for other registers.
*
* reglap: Write .r_refcount = 0 if the register
* variable has a subregister.
*/
fprintf(ctable, ",%d",
l_regs[i].ri_rregvar >= 0 && l_regs[i].ri_memb[0] == 0);
fprintf(ctable, "},\n");
1985-01-08 09:59:28 +00:00
}
fprintf(ctable, "};\n\n short clashlist[] = {\n\t");
for (i = 0; i < iclashlist; i++)
{
fprintf(ctable, "%d, ", clashlist[i]);
if (clashlist[i] == 0)
fprintf(ctable, "\n\t");
}
fprintf(ctable, "0};\n\n");
1985-01-08 09:59:28 +00:00
}
void outregvars(void)
{
register int i, j;
fprintf(htable, "#define REGVARS\n");
if (reglap != 0)
fprintf(htable, "#define REGLAP\n");
fprintf(ctable, "#include \"regvar.h\"\n");
fprintf(ctable, "int nregvar[4] = { ");
for (i = 0; i < 4; i++)
{
fprintf(ctable, "%d, ", nregvar[i]);
if (nregvar[i] > maxregvars)
1985-01-08 09:59:28 +00:00
maxregvars = nregvar[i];
}
fprintf(ctable, "};\n");
for (i = 0; i < 4; i++)
if (nregvar[i] > 0)
fprintf(ctable, "struct regassigned ratar%d[%d];\n", i, nregvar[i]);
for (i = 0; i < 4; i++)
if (nregvar[i] > 0)
{
fprintf(ctable, "int rvtar%d[] = {", i);
for (j = 0; j < nregvar[i]; j++)
fprintf(ctable, "%d,", rvnumbers[i][j]);
fprintf(ctable, "};\n");
}
fprintf(ctable, "\nint *rvnumbers[] = {\n");
for (i = 0; i < 4; i++)
if (nregvar[i] > 0)
fprintf(ctable, "\trvtar%d,\n", i);
1985-01-08 09:59:28 +00:00
else
fprintf(ctable, "\t0,\n");
fprintf(ctable, "};\n\nstruct regassigned *regassigned[] = {\n");
for (i = 0; i < 4; i++)
if (nregvar[i] > 0)
fprintf(ctable, "\tratar%d,\n", i);
1985-01-08 09:59:28 +00:00
else
fprintf(ctable, "\t0,\n");
fprintf(ctable, "};\n");
1985-01-08 09:59:28 +00:00
}
int typeconv(int n)
{
if (n >= 0)
return (2);
if (n == -1)
return (1);
if (n == -2)
return (3);
assert(n == -3);
return (0);
}
1985-01-08 09:59:28 +00:00
static int is_ascii(int c)
{
if ((c >= 0) && (c <= 127))
return 1;
return 0;
1985-01-08 09:59:28 +00:00
}
void outfmt(register char *p)
1990-12-17 12:52:41 +00:00
{
register int c;
fprintf(ctable, "\"");
while ((c = (*p++ & 0377)) != 0)
{
if (!is_ascii(c) || iscntrl(c))
{
fprintf(ctable, "\\%c%c%c", ((c & ~0300) >> 6) + '0',
((c & 070) >> 3) + '0', (c & 07) + '0');
1990-12-17 12:52:41 +00:00
}
else
fprintf(ctable, "%c", c);
1990-12-17 12:52:41 +00:00
}
fprintf(ctable, "\"");
1990-12-17 12:52:41 +00:00
}
void outtokens(void)
{
register int tokno, i;
1985-01-08 09:59:28 +00:00
register token_p tp;
fprintf(ctable, "tkdef_t tokens[] = {{0},\n");
for (tokno = 1; tokno < ntokens; tokno++)
{
1985-01-08 09:59:28 +00:00
tp = l_tokens[tokno];
fprintf(ctable, "/* %3d */{%d,{%d,%d},{", tokno, tp->tk_size,
tp->tk_cost.ct_space, tp->tk_cost.ct_time);
for (i = 0; i < maxtokensize; i++)
fprintf(ctable, "%d,", typeconv(tp->tk_att[i].ta_type));
fprintf(ctable, "},%d},\t/* ", tp->tk_format);
if (tp->tk_format >= 0)
outfmt(l_strings[tp->tk_format]);
else
fprintf(ctable, "(no format)");
fprintf(ctable, " */\n");
1985-01-08 09:59:28 +00:00
}
fprintf(ctable, "{0}};\n\n");
1985-01-08 09:59:28 +00:00
}
void outenodes(void)
{
1985-01-08 09:59:28 +00:00
register node_p np;
fprintf(ctable, "node_t enodes[] = {\n");
for (np = nodes; np < &nodes[nnodes]; np++)
fprintf(ctable, "{%d,%d,%d},\n", np->ex_operator, np->ex_lnode,
np->ex_rnode);
fprintf(ctable, "};\n\n");
1985-01-08 09:59:28 +00:00
}
void outstrings(void)
{
register int i;
#if 0
1987-02-09 17:41:25 +00:00
register char *p;
register int c;
#endif
1985-01-08 09:59:28 +00:00
if (tabledebug)
fprintf(ctable, "char *tablename = \"%s\";\n", filename);
fprintf(ctable, "string codestrings[] = {\n");
for (i = 0; i < nstrings; i++)
{
fprintf(ctable, "\t");
1990-12-17 12:52:41 +00:00
outfmt(l_strings[i]);
#if 0
while ((c= (*p++&0377))!=0)
{
if (! isascii(c) || iscntrl(c))
{
1987-02-09 17:41:25 +00:00
fprintf(ctable,"\\%c%c%c",
((c&~0300)>>6) + '0', ((c&070)>>3)+'0',
(c&07)+'0');
1987-02-09 17:41:25 +00:00
}
else fprintf(ctable, "%c",c);
}
1985-01-08 09:59:28 +00:00
fprintf(ctable,"\",\n");
1990-12-17 12:52:41 +00:00
#endif
fprintf(ctable, ",\n");
1985-01-08 09:59:28 +00:00
}
fprintf(ctable, "};\n\n");
1985-01-08 09:59:28 +00:00
}
extern set_t unstackset;
void outsets(void)
{
register int i;
1985-01-08 09:59:28 +00:00
register set_p sp;
fprintf(ctable, "set_t machsets[] = {\n");
for (sp = l_sets; sp < &l_sets[nsets]; sp++)
{
fprintf(ctable, "/* %3ld */ {%3d,{", (long) (sp - l_sets),
sp->set_size);
for (i = 0; i < setsize; i++)
fprintf(ctable, "0x%x,", sp->set_val[i] & 0xFFFF);
fprintf(ctable, "}},\n");
1985-01-08 09:59:28 +00:00
}
fprintf(ctable, "};\n\n");
fprintf(ctable, "set_t unstackset = { %3d,{\n", unstackset.set_size);
for (i = 0; i < setsize; i++)
fprintf(ctable, "0x%x,", unstackset.set_val[i] & 0xFFFF);
fprintf(ctable, "}};\n\n");
1985-01-08 09:59:28 +00:00
}
void outinstances(void)
{
1985-01-08 09:59:28 +00:00
register inst_p ip;
register int i;
fprintf(ctable, "inst_t tokeninstances[] = {\n");
for (ip = l_instances; ip < &l_instances[ninstances]; ip++)
{
fprintf(ctable, "{ %d, {", ip->in_which);
for (i = 0; i <= maxtokensize; i++)
fprintf(ctable, "%d,", ip->in_info[i]);
fprintf(ctable, "}},\n");
1985-01-08 09:59:28 +00:00
}
fprintf(ctable, "};\n\n");
1985-01-08 09:59:28 +00:00
}
void outmoves(void)
{
1985-01-08 09:59:28 +00:00
register move_p mp;
fprintf(ctable, "move_t moves[] = {\n");
for (mp = l_moves; mp < &l_moves[nmoves]; mp++)
fprintf(ctable, "{%d,%d,%d,%d,%d},\n", mp->m_set1, mp->m_expr1,
mp->m_set2, mp->m_expr2, mp->m_cindex);
fprintf(ctable, "{-1}\n};\n\n");
1985-01-08 09:59:28 +00:00
}
void outtests(void)
{
1985-01-08 09:59:28 +00:00
register test_p tp;
fprintf(ctable, "test_t tests[] = {\n");
for (tp = l_tests; tp < &l_tests[ntests]; tp++)
fprintf(ctable, "{%d,%d,%d},\n", tp->t_set, tp->t_expr, tp->t_cindex);
fprintf(ctable, "{-1}\n};\n\n");
1985-01-08 09:59:28 +00:00
}
void outstacks(void)
{
1985-01-08 09:59:28 +00:00
register c1_p cp;
fprintf(ctable, "c1_t c1coercs[] = {\n");
for (cp = l_stacks; cp < &l_stacks[nstacks]; cp++)
fprintf(ctable, "{%d,%d,%d,%d},\n", cp->c1_texpno, cp->c1_expr,
cp->c1_prop, cp->c1_codep);
fprintf(ctable, "{-1}\n};\n\n");
1985-01-08 09:59:28 +00:00
}
void outsplits(void)
{
1985-01-08 09:59:28 +00:00
register c2_p cp;
register int i;
fprintf(ctable, "c2_t c2coercs[] = {\n");
for (cp = l_split; cp < &l_split[nsplit]; cp++)
{
fprintf(ctable, "{%d,%d,%d,{", cp->c2_texpno, cp->c2_expr,
cp->c2_nsplit);
for (i = 0; i < maxsplit; i++)
fprintf(ctable, "%d,", cp->c2_repl[i]);
fprintf(ctable, "},%d},\n", cp->c2_codep);
1985-01-08 09:59:28 +00:00
}
fprintf(ctable, "{-1}\n};\n\n");
1985-01-08 09:59:28 +00:00
}
void outcoercs(void)
{
1985-01-08 09:59:28 +00:00
register c3_p cp;
fprintf(ctable, "c3_t c3coercs[] = {\n");
for (cp = l_coercs; cp < &l_coercs[ncoercs]; cp++)
fprintf(ctable, "{%d,%d,%d,%d,%d},\n", cp->c3_texpno, cp->c3_expr,
cp->c3_prop, cp->c3_repl, cp->c3_codep);
fprintf(ctable, "{-1}\n};\n\n");
1985-01-08 09:59:28 +00:00
}
void outproplists(void)
{
register int propno;
register int regno;
for (propno = 0; propno < nprops; propno++)
{
fprintf(ctable, "struct reginfo *rlist%d[] = {\n", propno);
for (regno = 1; regno < nregs; regno++)
if (BIT(l_props[propno].pr_regset, regno))
fprintf(ctable, "&machregs[%d],\n", regno);
fprintf(ctable, "0\n};\n");
1985-01-08 09:59:28 +00:00
}
fprintf(ctable, "struct reginfo **reglist[] = {\n");
for (propno = 0; propno < nprops; propno++)
fprintf(ctable, "rlist%d,\n", propno);
fprintf(ctable, "};\n\n");
1985-01-08 09:59:28 +00:00
}
void outconsts(void)
{
1985-01-08 09:59:28 +00:00
fprintf(ctable, "unsigned cc1 = %u;\n", fc1);
fprintf(ctable, "unsigned cc2 = %u;\n", fc2);
fprintf(ctable, "unsigned cc3 = %u;\n", fc3);
fprintf(ctable, "unsigned cc4 = %u;\n", fc4);
1985-01-08 09:59:28 +00:00
}
void cdef(char *s, int n)
{
1985-01-08 09:59:28 +00:00
fprintf(htable, "#define %s %d\n", s, n);
1985-01-08 09:59:28 +00:00
}
void passon(char *s)
{
1985-01-08 09:59:28 +00:00
char buf[32];
sprintf(buf, "T%s", s);
cdef(buf, cmustbeset(s));
1985-01-08 09:59:28 +00:00
}
void outdefs(void)
{
1985-01-08 09:59:28 +00:00
register symbol *sy_p;
extern int maxempatlen, maxrule;
1985-01-08 09:59:28 +00:00
char *wrdfmt;
passon("EM_WSIZE");
passon("EM_PSIZE");
passon("EM_BSIZE");
if ((sy_p = lookup("FORMAT", symsconst, justlooking)) != 0)
{
1985-01-08 09:59:28 +00:00
wrdfmt = l_strings[sy_p->sy_value.syv_stringno];
fprintf(htable, "#define WRD_FMT \"%s\"\n", wrdfmt);
}
cdef("MAXALLREG", maxallreg);
cdef("SETSIZE", setsize);
cdef("NREGS", nregs);
cdef("REGSETSIZE", SZOFSET(nregs));
cdef("TOKENSIZE", maxtokensize);
cdef("MAXMEMBERS", maxmembers);
cdef("LONGESTPATTERN", maxempatlen);
cdef("MAXPATLEN", maxtokpatlen);
cdef("MAXREPLLEN", maxtokrepllen);
cdef("MAXEMREPLLEN", maxemrepllen);
cdef("MAXPROCARG", maxprocargs);
cdef("MAXRULE", maxrule < 16 ? 16 : maxrule);
if (nsplit > 0)
{
cdef("MAXSPLIT", maxsplit);
1985-01-08 09:59:28 +00:00
}
if (tabledebug)
cdef("TABLEDEBUG", 1);
if (use_tes)
cdef("USE_TES", 1);
1985-01-08 09:59:28 +00:00
}
void outars(void)
{
register int i;
1985-01-08 09:59:28 +00:00
if (code_in_c)
fprintf(htable, "#define CODEINC 1\n");
else
{
fprintf(ctable, "char coderules[%d];\n", codeindex);
fprintf(ctable, "int ncodebytes=%d;\n", codeindex);
1985-01-08 09:59:28 +00:00
}
fprintf(ctable, "char pattern[%d]={\n", npatbytes + 1);
for (i = 0; i <= npatbytes; i++)
{
fprintf(ctable, "%d,%c", pattern[i] & BMASK, i % 16 == 15 ? '\n' : ' ');
1985-01-08 09:59:28 +00:00
}
fprintf(ctable, "};\n\n");
fprintf(ctable, "int pathash[256]={\n");
for (i = 0; i < 256; i++)
{
fprintf(ctable, "%d,%c", pathash[i] & 0xFFFF, i % 10 == 9 ? '\n' : ' ');
1985-01-08 09:59:28 +00:00
}
fprintf(ctable, "};\n");
1985-01-08 09:59:28 +00:00
}
void finishio(void)
{
1985-01-08 09:59:28 +00:00
extern int nregs;
finishcode();
hashpatterns();
setsize = SZOFSET(nregs + ntokens);
1985-01-08 09:59:28 +00:00
outdefs();
outincludes();
outregs();
outtokens();
outenodes();
outstrings();
outsets();
outinstances();
outmoves();
outtests();
outstacks();
if (nsplit > 0)
1985-01-08 09:59:28 +00:00
outsplits();
outcoercs();
outproplists();
outconsts();
if (rvused)
outregvars();
outars();
}
void codecoco(int cocono)
{
1985-01-08 09:59:28 +00:00
if (cocono == -1)
1985-01-08 09:59:28 +00:00
return;
code8(DO_SETCC);
codeint(cocono);
codenl();
}
void dopattern(int stackcoerc, varinfo *kills, varinfo *allocates,
varinfo *generates, varinfo *yields, varinfo *leaving)
1985-01-08 09:59:28 +00:00
{
register int i;
int n, nops;
register struct varinfo *vp, *vivp;
1985-01-08 09:59:28 +00:00
register instr_p instp;
int al, deal;
1985-01-08 09:59:28 +00:00
int vil;
int cocono = -1;
1985-01-08 09:59:28 +00:00
cost_t totcost;
1985-01-11 09:41:47 +00:00
int nremoves;
int removelist[100];
1985-01-08 09:59:28 +00:00
static char tlab[] = "0:";
extern int optexact, optstack, startline;
1985-01-08 09:59:28 +00:00
extern char *filename;
extern int lineno;
#ifdef CODEDEBUG
fprintf(code,"Code(%d) at \"%s\", line %d\n",stackcoerc,filename,lineno);
#endif
if (code_in_c)
fprintf(ctable, "\n/* \"%s\", line %d */ ", filename, lineno);
if (tabledebug)
{
1985-01-08 09:59:28 +00:00
code8(DO_DLINE);
codeint(startline);
1985-01-08 09:59:28 +00:00
codenl();
if (startline < MAXSOURCELINES)
{
if (startline > maxline)
maxline = startline;
BIS(lineset, startline);
}
else
{
static int beenhere = 0;
if (!beenhere)
{
1985-01-08 09:59:28 +00:00
beenhere++;
error("Too many source lines for table debug");
}
}
}
/* MATCH part */
if (tokpatlen)
{
1985-01-08 09:59:28 +00:00
if (optexact)
if (optstack)
code53(DO_XXMATCH, tokpatlen);
1985-01-08 09:59:28 +00:00
else
code53(DO_XMATCH, tokpatlen);
1985-01-08 09:59:28 +00:00
else
code53(DO_MATCH, tokpatlen);
for (i = 0; i < tokpatlen; i++)
1985-01-08 09:59:28 +00:00
codeint(tokpatset[i]);
codenl();
}
else if (stackcoerc)
1985-01-08 09:59:28 +00:00
code8nl(DO_COERC);
if (optstack)
{
code53(DO_TOSTACK, 0);
1985-01-08 09:59:28 +00:00
codeint(allsetno);
codenl();
}
/* The kills */
for (vp = kills; vp != 0; vp = vp->vi_next)
{
if (vp->vi_int[1] != 0)
{
code53(DO_REMOVE, 1);
1985-01-08 09:59:28 +00:00
codeint(vp->vi_int[0]);
codeint(vp->vi_int[1]);
}
else if (vp->vi_int[0] >= 0)
{
code53(DO_REMOVE, 0);
1985-01-08 09:59:28 +00:00
codeint(vp->vi_int[0]);
}
else
{
1989-10-26 15:41:38 +00:00
code8(DO_KILLREG);
1987-02-23 15:48:27 +00:00
codeint(-vp->vi_int[0] - 1);
} codenl();
1985-01-08 09:59:28 +00:00
}
nremoves = 0;
for (vp = generates; vp != 0; vp = vp->vi_next)
{
1985-01-11 09:41:47 +00:00
if (vp->vi_int[0] != INSREMOVE)
continue;
for (i = 0; i < nremoves; i++)
if (vp->vi_int[1] == removelist[i])
1985-01-11 09:41:47 +00:00
break;
if (i == nremoves)
{
assert(nremoves < (sizeof(removelist) / sizeof(int)));
1985-01-11 09:41:47 +00:00
removelist[nremoves++] = vp->vi_int[1];
}
}
for (i = 0; i < nremoves; i++)
{
1985-01-11 09:41:47 +00:00
code8(DO_RREMOVE);
codeint(removelist[i]);
codenl();
}
1985-01-08 09:59:28 +00:00
/* allocate part */
deal = 0;
al = 0;
for (vp = allocates; vp != 0; vp = vp->vi_next)
{
if (vp->vi_int[0] == -1)
{ /* Deallocate */
1985-01-08 09:59:28 +00:00
deal++;
code8(DO_DEALLOCATE);
codeint(vp->vi_int[1]);
codenl();
}
else
{
if (vp->vi_int[1] == 0)
{
code53(DO_ALLOCATE, 0);
1985-01-08 09:59:28 +00:00
codeint(vp->vi_int[0]);
codenl();
}
else
{
code53(DO_ALLOCATE, 1);
1985-01-08 09:59:28 +00:00
codeint(vp->vi_int[0]);
codeint(vp->vi_int[1]);
codenl();
}
al++;
}
}
if (deal)
code8nl(DO_REALLOCATE);
if (al > maxallreg)
maxallreg = al;
1985-01-08 09:59:28 +00:00
totcost.ct_space = 0;
totcost.ct_time = 0;
for (vp = generates; vp != 0; vp = vp->vi_next)
{
n = vp->vi_int[0];
switch (n)
{
default:
assert(n >= 0);
instp = &l_instr[n];
nops = instp->i_nops;
code53(DO_INSTR, nops);
if (vp->vi_int[1] == 0)
{
codeint(instp->i_asname);
}
else
{
codeint(10000 + vp->vi_int[1]);
}
vivp = vp->vi_vi;
for (i = 0; i < nops; i++)
{
codeint(vivp->vi_int[0]);
vivp = vivp->vi_vi;
}
codenl();
totcost.ct_space += instp->i_cost.ct_space;
totcost.ct_time += instp->i_cost.ct_time;
break;
case INSREMOVE:
break;
case INSMOVE:
codecoco(cocono);
code8(DO_MOVE);
codeint(vp->vi_int[1]);
codeint(vp->vi_int[2]);
codenl();
break;
case INSTEST:
codecoco(cocono);
code8(DO_TEST);
codeint(vp->vi_int[1]);
codenl();
break;
case INSPRETURN:
code8(DO_PRETURN);
codenl();
break;
case INSTLAB:
cocono = 0;
tlab[0] = vp->vi_int[1] + '0';
code53(DO_INSTR, 0);
codeint(strlookup(tlab));
codenl();
break;
case INSSETCC:
cocono = vp->vi_int[1];
break;
case INSERASE:
code8(DO_ERASE);
codeint(vp->vi_int[1]);
codenl();
break;
case INSLABDEF:
cocono = 0;
code8(DO_LABDEF);
codeint(vp->vi_int[1]);
codenl();
break;
1985-01-08 09:59:28 +00:00
}
}
codecoco(cocono);
vil = vilength(yields);
if (vil != 0 || tokpatlen != 0 || allocates != 0)
{
code53(DO_TOKREPLACE, vilength(yields));
for (vp = yields; vp != 0; vp = vp->vi_next)
{
1985-01-08 09:59:28 +00:00
codeint(vp->vi_int[0]);
} codenl();
1985-01-08 09:59:28 +00:00
}
if (leaving != 0)
{
code53(DO_EMREPLACE, vilength(leaving));
while (leaving != 0)
{
1985-01-08 09:59:28 +00:00
codeint(leaving->vi_int[0]);
codeint(leaving->vi_int[1]);
leaving = leaving->vi_next;
} codenl();
1985-01-08 09:59:28 +00:00
}
if (totcost.ct_space != 0 || totcost.ct_time != 0)
{
1985-01-08 09:59:28 +00:00
code8(DO_COST);
codeint(totcost.ct_space);
codeint(totcost.ct_time);
codenl();
}
if (empatlen == 0 && !inproc)
1985-01-08 09:59:28 +00:00
code8nl(DO_RETURN);
else
code8nl(DO_NEXTEM);
}
void used(char *resource, int use, int max)
{
if (verbose || 4 * use > 3 * max)
fprintf(stderr, "%s %d(%d)\n", resource, use, max);
}
void statistics(void)
{
extern int nnodes, maxempatlen, maxrule;
used("Registers", nregs, MAXREGS);
used("Properties", nprops, MAXPROPS);
used("Tokens", ntokens, MAXTOKENS);
used("Tokensize", maxtokensize, MAXATT);
used("Sets", nsets, MAXSETS);
used("Instructions", ninstr, MAXINSTR);
used("Strings", nstrings, MAXSTRINGS);
used("Exp-nodes", nnodes, MAXNODES);
used("EM-pat length", maxempatlen, EMPATMAX);
used("rules/EM-pattern", maxrule, MAXPATTERNS);
used("Allocates/rule", maxallreg, MAXALLREG);
used("Instances", ninstances, MAXINSTANCES);
used("Moves", nmoves, MAXMOVES);
used("Tests", ntests, MAXTESTS);
used("Stacks", nstacks, MAXSTACKS);
used("1->1 Coercions", ncoercs, MAXCOERCS);
used("Splitting coercions", nsplit, MAXSPLCOERC);
used("Register variables", maxregvars, MAXREGVAR);
used("Pat bytes", npatbytes + 1, MAXPATBYTES);
1985-01-08 09:59:28 +00:00
if (tabledebug)
used("Source lines", maxline, MAXSOURCELINES);
1985-01-08 09:59:28 +00:00
}