ack/util/ncgg/subr.c

518 lines
9.9 KiB
C
Raw 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".
*/
1985-01-08 09:59:28 +00:00
#ifndef NORCSID
static char rcsid[] = "$Id$";
1985-01-08 09:59:28 +00:00
#endif
#include <stdlib.h>
#include <string.h>
1985-01-08 09:59:28 +00:00
#include "param.h"
#include "reg.h"
#include "lookup.h"
#include "property.h"
#include "expr.h"
#include "set.h"
#include "varinfo.h"
#include "instruct.h"
#include "token.h"
#include "regvar.h"
#include <cgg_cg.h>
#include <em_reg.h>
#include "subr.h"
#include "hall.h"
1985-01-08 09:59:28 +00:00
#include "extern.h"
extern int npatbytes;
extern set_t l_sets[];
void n_proc(char *name)
{
1985-01-08 09:59:28 +00:00
register symbol *sy_p;
sy_p = lookup(name, symproc, newsymbol);
1987-01-16 13:51:42 +00:00
sy_p->sy_value.syv_procoff = npatbytes + 1;
1985-01-08 09:59:28 +00:00
}
struct varinfo * make_erase(char *name)
{
expr_t e;
1985-01-08 09:59:28 +00:00
struct varinfo *result;
e = ident_expr(name);
if (e.ex_typ != TYPREG)
error("Register name required here");
NEW(result, struct varinfo);
1985-01-08 09:59:28 +00:00
result->vi_next = VI_NULL;
result->vi_int[0] = e.ex_index;
return (result);
1985-01-08 09:59:28 +00:00
}
void n_instr(char *name, char *asname, operand *oplist, struct varinfo *eraselist,
struct varinfo *cost)
1985-01-08 09:59:28 +00:00
{
register int instrno;
register int cc_count;
1985-01-08 09:59:28 +00:00
register instr_p ip;
instrno = NEXT(ninstr, MAXINSTR, "Instructions");
1985-01-08 09:59:28 +00:00
ip = &l_instr[instrno];
ip->i_name = name;
ip->i_asname = strlookup(asname != 0 ? asname : name);
1985-01-08 09:59:28 +00:00
ip->i_nops = 0;
ip->i_oplist = oplist;
ip->i_erases = eraselist;
if (cost == 0)
{
1985-01-08 09:59:28 +00:00
ip->i_cost.ct_space = 0;
ip->i_cost.ct_time = 0;
}
else
{
1985-01-08 09:59:28 +00:00
ip->i_cost.ct_space = cost->vi_int[0];
ip->i_cost.ct_space = cost->vi_int[1];
}
for (cc_count = 0; oplist != 0; oplist = oplist->o_next)
{
1985-01-08 09:59:28 +00:00
ip->i_nops++;
if (oplist->o_adorn & AD_CC)
1985-01-08 09:59:28 +00:00
cc_count++;
}
while (eraselist != VI_NULL )
{
1985-01-08 09:59:28 +00:00
if (eraselist->vi_int[0] == -1 && cc_count)
error("Instruction can't both set and break the condition codes");
eraselist = eraselist->vi_next;
1985-01-08 09:59:28 +00:00
}
if (cc_count > 1)
1985-01-08 09:59:28 +00:00
error("No instruction can set condition codes more than once");
}
void n_set(char *name, int number)
{
1985-01-08 09:59:28 +00:00
register symbol *sy_p;
sy_p = lookup(name, symset, newsymbol);
1985-01-08 09:59:28 +00:00
sy_p->sy_value.syv_setno = number;
}
void n_tok(char *name, struct varinfo *atts, int size,
struct varinfo *cost, struct varinfo *format)
1985-01-08 09:59:28 +00:00
{
register symbol *sy_p;
register token_p tp;
register struct varinfo *vip;
int i;
int tokno;
int thistokensize;
char formstr[50], smallstr[2];
1985-01-08 09:59:28 +00:00
sy_p = lookup(name, symtok, newsymbol);
NEW(tp, token_t);
tokno = NEXT(ntokens, MAXTOKENS, "Tokens");
1985-01-08 09:59:28 +00:00
sy_p->sy_value.syv_tokno = tokno;
l_tokens[tokno] = tp;
tp->tk_name = sy_p->sy_name;
tp->tk_size = size;
if (cost != 0)
{
1985-01-08 09:59:28 +00:00
tp->tk_cost.ct_space = cost->vi_int[0];
tp->tk_cost.ct_time = cost->vi_int[1];
}
else
{
1985-01-08 09:59:28 +00:00
tp->tk_cost.ct_space = 0;
tp->tk_cost.ct_time = 0;
1985-01-08 09:59:28 +00:00
}
for (i = 0, vip = atts; i < MAXATT && vip != 0; i++, vip = vip->vi_next)
{
1985-01-08 09:59:28 +00:00
tp->tk_att[i].ta_type = vip->vi_int[0];
tp->tk_att[i].ta_name = vip->vi_str[0];
vip->vi_str[0] = 0;
1985-01-08 09:59:28 +00:00
}
thistokensize = i;
if (i > maxtokensize)
maxtokensize = i;
if (vip != 0)
error("More then %d attributes, rest discarded", MAXATT);
for (; i < MAXATT; i++)
tp->tk_att[i].ta_type = -3;
if (format != 0)
{
1985-01-08 09:59:28 +00:00
formstr[0] = 0;
for (vip = format; vip != 0; vip = vip->vi_next)
{
if (vip->vi_int[0] == 0)
strcat(formstr, vip->vi_str[0]);
else
{
for (i = 0; i < thistokensize; i++)
{
if (strcmp(vip->vi_str[0], tp->tk_att[i].ta_name) == 0)
{
smallstr[0] = i + 1;
1985-01-08 09:59:28 +00:00
smallstr[1] = 0;
strcat(formstr, smallstr);
1985-01-08 09:59:28 +00:00
break;
}
}
if (i == thistokensize)
error("%s not a known attribute", vip->vi_str[0]);
1985-01-08 09:59:28 +00:00
}
}
tp->tk_format = strlookup(formstr);
}
else
1985-01-08 09:59:28 +00:00
tp->tk_format = -1;
}
void checkprintformat(int n)
{
1985-01-08 09:59:28 +00:00
register short *s;
register int i;
s = l_sets[n].set_val;
for (i = nregs; i < nregs + ntokens; i++)
if (BIT(s,i) && l_tokens[i - nregs]->tk_format < 0)
1985-01-08 09:59:28 +00:00
error("Token %s in set does not have printformat",
l_tokens[i - nregs]->tk_name);
1985-01-08 09:59:28 +00:00
}
void n_prop(char *name, int size)
{
1985-01-08 09:59:28 +00:00
int propno;
register symbol *sp;
propno = NEXT(nprops, MAXPROPS, "Properties");
sp = lookup(name, symprop, newsymbol);
1985-01-08 09:59:28 +00:00
sp->sy_value.syv_propno = propno;
if (size <= 0)
{
1985-01-08 09:59:28 +00:00
error("Size of property must be >0");
size = wordsize;
}
l_props[propno].pr_size = size;
}
void prophall(int n)
{
register int i;
1985-01-08 09:59:28 +00:00
short hallset[SETSIZE];
if (n < 0)
return;
for (i = 0; i < SETSIZE; i++)
hallset[i] = i < SZOFSET(MAXREGS) ? l_props[n].pr_regset[i] : 0;
1985-01-08 09:59:28 +00:00
nexthall(hallset);
}
int n_reg(char *name, char *printstring, int nmemb, int member1, int member2)
{
1985-01-08 09:59:28 +00:00
register symbol *sy_p;
register reginfo *ri_p;
int regno;
sy_p = lookup(name, symreg, newsymbol);
sy_p->sy_value.syv_regno = regno = NEXT(nregs, MAXREGS,
"Number of registers");
1985-01-08 09:59:28 +00:00
ri_p = &l_regs[regno];
ri_p->ri_name = mystrcpy(name);
ri_p->ri_repr = printstring != 0 ? mystrcpy(printstring) : ri_p->ri_name;
1985-01-08 09:59:28 +00:00
ri_p->ri_memb[0] = member1;
ri_p->ri_memb[1] = member2;
if (nmemb > maxmembers)
maxmembers = nmemb;
return (regno);
1985-01-08 09:59:28 +00:00
}
void make_const(void)
{
1985-01-08 09:59:28 +00:00
wordsize = cmustbeset("EM_WSIZE");
pointersize = cmustbeset("EM_PSIZE");
}
int cmustbeset(char *ident)
{
1985-01-08 09:59:28 +00:00
return (lookup(ident, symconst, mustexist)->sy_value.syv_cstval);
1985-01-08 09:59:28 +00:00
}
void n_const(char *ident, int val)
{
1985-01-08 09:59:28 +00:00
register symbol *sy_p;
sy_p = lookup(ident, symconst, newsymbol);
1985-01-08 09:59:28 +00:00
sy_p->sy_value.syv_cstval = val;
}
void n_sconst(char *ident, char *val)
{
1985-01-08 09:59:28 +00:00
register symbol *sy_p;
sy_p = lookup(ident, symsconst, newsymbol);
1985-01-08 09:59:28 +00:00
sy_p->sy_value.syv_stringno = strlookup(val);
}
static void add_regvar(int rvnum, reginfo *regp, int rv)
{
int overlap, wrong;
overlap = wrong = 0;
if (regp->ri_memb[0] != 0)
{
/* reglap: float may overlap with one subregister */
if (rv == reg_float && regp->ri_memb[1] == 0)
overlap = 1;
else
wrong = 1;
}
if (wrong)
error("Register variables may not have subregisters");
rvused |= ANY_REGVAR;
if (regp->ri_size == wordsize)
rvused |= SL_REGVAR;
else if (regp->ri_size == 2 * wordsize)
rvused |= DL_REGVAR;
wrong = 0;
if (overlap)
{
/* reglap = size of overlap float */
if (reglap == 0)
{
reglap = regp->ri_size;
if (reglap == rvsize[reg_float])
error("Two sizes of reg_float can't be same size");
}
else if (reglap != regp->ri_size)
wrong = 1;
}
else
{
if (nregvar[rv] == 0)
rvsize[rv] = regp->ri_size;
else if (rvsize[rv] != regp->ri_size)
wrong = 1;
}
if (wrong)
error("All register variables of one type must have the same size");
if (overlap)
{
reginfo *member_p = &l_regs[regp->ri_memb[0]];
int i;
/*
* reglap: Remove member_p from rvnumbers.
* Add reg_p in its place.
*/
wrong = 1;
for (i = 0; i < nregvar[rv]; i++)
{
if (rvnumbers[rv][i] == regp->ri_memb[0])
{
rvnumbers[rv][i] = rvnum;
wrong = 0;
break;
}
}
if (wrong)
error("Register variable %s can't overlap %s", regp->ri_name,
member_p->ri_name);
}
else
{
NEXT(nregvar[rv], MAXREGVAR, "Register variable");
rvnumbers[rv][nregvar[rv] - 1] = rvnum;
}
}
void regline(varinfo *rl, varinfo *pl, int rv)
{
register varinfo *rrl, *rpl;
1985-01-08 09:59:28 +00:00
register short *sp;
register reginfo *regp;
int thissize;
int propno;
for (rrl = rl; rrl != 0; rrl = rrl->vi_next)
{
1985-01-08 09:59:28 +00:00
regp = &l_regs[rrl->vi_int[0]];
thissize = 0;
for (rpl = pl; rpl != 0; rpl = rpl->vi_next)
{
1985-01-08 09:59:28 +00:00
propno = rpl->vi_int[0];
sp = l_props[propno].pr_regset;
BIS(sp, rrl->vi_int[0]);
if (thissize == 0)
1985-01-08 09:59:28 +00:00
thissize = l_props[propno].pr_size;
else if (thissize != -1 && thissize != l_props[propno].pr_size)
error("Register %s has no clear size", regp->ri_name);
1985-01-08 09:59:28 +00:00
}
regp->ri_size = thissize;
regp->ri_class = regclass;
regp->ri_rregvar = rv;
if (rv >= 0)
add_regvar(rrl->vi_int[0], regp, rv);
1985-01-08 09:59:28 +00:00
}
regclass++;
}
void check_reglap(void)
{
reginfo *regp;
int i;
if (reglap == 0)
return;
/* reglap: Check that every reg_float has size == reglap. */
for (i = 0; i < nregvar[reg_float]; i++)
{
regp = &l_regs[rvnumbers[reg_float][i]];
if (regp->ri_size != reglap)
error("Missing reg_float of size %d to contain %s", reglap,
regp->ri_name);
}
}
void setallreg(struct varinfo *vi)
{
1985-01-08 09:59:28 +00:00
nallreg = 0;
for (; vi != 0; vi = vi->vi_next)
{
if (vi->vi_int[0] < 0)
1985-01-08 09:59:28 +00:00
continue;
allreg[nallreg++] = vi->vi_int[0];
}
}
void freevi(register struct varinfo *vip)
{
register int i;
1985-01-08 09:59:28 +00:00
if (vip == 0)
1985-01-08 09:59:28 +00:00
return;
freevi(vip->vi_next);
freevi(vip->vi_vi);
for (i = 0; i < VI_NSTR; i++)
free((char *) vip->vi_str[i]);
1985-01-08 09:59:28 +00:00
free(vip);
}
int myatoi(register char *s)
{
register int base = 10;
register int sum = 0;
1985-01-08 09:59:28 +00:00
if (*s == '0')
{
1985-01-08 09:59:28 +00:00
base = 8;
s++;
if (*s == 'x')
{
base = 16;
1985-01-08 09:59:28 +00:00
s++;
}
}
for (;;)
{
switch (*s)
{
default:
return (sum);
case '8':
case '9':
if (base == 8)
error("Bad digit in octal number");
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
sum = sum * base + *s++ - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
if (base != 16)
error("Hexletter in number not expected");
sum = sum * base + 10 + *s++ - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
if (base != 16)
error("Hexletter in number not expected");
sum = sum * base + 10 + *s++ - 'A';
break;
1985-01-08 09:59:28 +00:00
}
}
}
char *mystrcpy(char *s)
{
1985-01-08 09:59:28 +00:00
register char *p;
p = myalloc(strlen(s) + 1);
strcpy(p, s);
return (p);
1985-01-08 09:59:28 +00:00
}
char *myalloc(register int n)
{
register char *p, *result;
1985-01-08 09:59:28 +00:00
result = p = malloc(n);
if (p == (char *) 0)
1985-01-08 09:59:28 +00:00
fatal("Out of memory");
do
*p++ = 0;
while (--n);
return (result);
1985-01-08 09:59:28 +00:00
}
int chkincl(int value, int lwb, int upb)
{
1985-01-08 09:59:28 +00:00
if (value < lwb || value > upb)
error("Number %d should have been between %d and %d", value, lwb, upb);
return (value);
1985-01-08 09:59:28 +00:00
}
int subset(short *sp1, short *sp2, int setsize)
{
register int i;
1985-01-08 09:59:28 +00:00
for (i = 0; i < setsize; i++)
if ((sp1[i] | sp2[i]) != sp2[i])
return (0);
return (1);
1985-01-08 09:59:28 +00:00
}
int vilength(register struct varinfo *vip)
{
register int l = 0;
1985-01-08 09:59:28 +00:00
while (vip != 0)
{
vip = vip->vi_next;
1985-01-08 09:59:28 +00:00
l++;
}
return (l);
1985-01-08 09:59:28 +00:00
}