ack/util/ncgg/expr.c

397 lines
8.2 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 <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
1985-01-08 09:59:28 +00:00
#include "param.h"
#include "set.h"
#include "reg.h"
#include "lookup.h"
#include "token.h"
#include "property.h"
#include "expr.h"
#include "regvar.h"
#include <cgg_cg.h>
#include <em_reg.h>
1985-01-08 09:59:28 +00:00
#include "extern.h"
extern set_t l_sets[];
int i_expr(expr_t e)
{
1985-01-08 09:59:28 +00:00
if (e.ex_typ != TYPINT)
error("Expression should be integer");
return (e.ex_index);
1985-01-08 09:59:28 +00:00
}
int b_expr(expr_t e)
{
1985-01-08 09:59:28 +00:00
if (e.ex_typ != TYPBOOL)
error("Expression should be boolean");
return (e.ex_index);
1985-01-08 09:59:28 +00:00
}
expr_t make_expr(int type, int operator, int op1, int op2)
{
1985-01-08 09:59:28 +00:00
expr_t result;
result.ex_typ = type;
result.ex_index = ex_lookup(operator, op1, op2);
return (result);
1985-01-08 09:59:28 +00:00
}
expr_t regno_expr(int regno)
{
1985-01-08 09:59:28 +00:00
expr_t result;
register int i;
1985-01-08 09:59:28 +00:00
result.ex_typ = TYPREG;
result.ex_index = ex_lookup(EX_REG, regno, 0);
for (i = 0; i < SZOFSET(MAXREGS); i++)
1985-01-08 09:59:28 +00:00
result.ex_regset[i] = 0;
BIS(result.ex_regset, regno);
return (result);
1985-01-08 09:59:28 +00:00
}
expr_t ident_expr(char *name)
{
1985-01-08 09:59:28 +00:00
register symbol *sy_p;
sy_p = lookup(name, symany, mustexist);
if (sy_p->sy_type == symconst)
return (make_expr(TYPINT, EX_CON,
(int) (sy_p->sy_value.syv_cstval & 0xFFFF),
(int) (sy_p->sy_value.syv_cstval >> 16)));
else if (sy_p->sy_type == symsconst)
return (make_expr(TYPADDR, EX_STRING, sy_p->sy_value.syv_stringno, 0));
else if (sy_p->sy_type != symreg)
error("Wrong type of identifier %s", name);
return (regno_expr(sy_p->sy_value.syv_regno));
1985-01-08 09:59:28 +00:00
}
expr_t subreg_expr(int tokarg, int subreg)
{
1985-01-08 09:59:28 +00:00
expr_t result;
result.ex_typ = TYPREG;
subregset(l_sets[tokpatset[tokarg > 0 ? tokarg - 1 : tokarg]].set_val,
subreg, result.ex_regset);
result.ex_index = ex_lookup(EX_SUBREG, tokarg, subreg);
return (result);
1985-01-08 09:59:28 +00:00
}
void subregset(register short *sp, int subreg, register short *regset)
{
register int i;
1985-01-08 09:59:28 +00:00
register reginfo *rp;
for (i = 0; i < SZOFSET(MAXREGS); i++)
regset[i] = 0;
for (i = 1; i < nregs; i++)
if (BIT(sp, i))
{
if (subreg)
{
rp = &l_regs[i];
if (rp->ri_memb[subreg - 1] == 0)
error("Register %s in set has no member %d", rp->ri_name,
subreg);
BIS(regset, rp->ri_memb[subreg - 1]);
}
else
BIS(regset, i);
}
for (; i < nregs + MAXTOKENS; i++)
if (BIT(sp, i))
error("Set contains %s, which is not a register",
l_tokens[i - nregs]->tk_name);
1985-01-08 09:59:28 +00:00
}
int membset(int setno, char *name, short *regset, char *appearance, int restyp, int *typp)
1985-01-08 09:59:28 +00:00
{
register short *sp;
register token_p tp;
register int i, j, k;
1985-01-08 09:59:28 +00:00
int thistyp;
int typesdiffer = 0;
int res_j = -1;
1985-01-08 09:59:28 +00:00
if (setno < 0)
return 0;
1985-01-08 09:59:28 +00:00
sp = l_sets[setno].set_val;
for (i = 1; i < nregs; i++)
if (BIT(sp, i))
{
error("Set in %s contains %s, which is not a token", appearance,
l_regs[i].ri_name);
break;
}
for (i = 0; i < SZOFSET(MAXREGS); i++)
1985-01-08 09:59:28 +00:00
regset[i] = 0;
for (i = nregs; i < nregs + MAXTOKENS; i++)
if (BIT(sp, i))
{
tp = l_tokens[i - nregs];
for (j = 0;
j < MAXATT
&& (tp->tk_att[j].ta_type == -3
|| strcmp(tp->tk_att[j].ta_name, name));
j++)
;
if (j == MAXATT)
error("Token %s does not contain %s", tp->tk_name, name);
else if (j != res_j && res_j != -1)
typesdiffer = 1;
else
{
res_j = j;
thistyp = tp->tk_att[j].ta_type;
if (thistyp == -2)
{
if (restyp != TYPADDR && restyp != 0)
typesdiffer = 1;
else
restyp = TYPADDR;
}
else if (thistyp == -1)
{
if (restyp != TYPINT && restyp != 0)
typesdiffer = 1;
else
restyp = TYPINT;
}
1985-01-08 09:59:28 +00:00
else
{
if (restyp != TYPREG && restyp != 0)
typesdiffer = 1;
else
{
restyp = TYPREG;
for (k = 0; k < SZOFSET(MAXREGS); k++)
regset[k] |=
l_props[tp->tk_att[j].ta_type].pr_regset[k];
}
1985-01-08 09:59:28 +00:00
}
}
}
if (typesdiffer)
error("%s is not a valid expression; types differ in the set",
appearance);
*typp = restyp == 0 ? TYPINT : restyp;
return (res_j == -1 ? 0 : res_j);
1985-01-08 09:59:28 +00:00
}
expr_t memb_expr(int setno, char *name, char *appearance, int tokarg)
{
1985-01-08 09:59:28 +00:00
expr_t result;
int res_j;
res_j = membset(setno, name, result.ex_regset, appearance, 0,
&result.ex_typ);
result.ex_index = ex_lookup(EX_TOKFIELD, tokarg, res_j + 1);
return (result);
1985-01-08 09:59:28 +00:00
}
expr_t tokm_expr(int tokarg, char *name)
{
1985-01-08 09:59:28 +00:00
char app[100];
1987-01-16 13:51:42 +00:00
int tokarg1 = tokarg > 0 ? tokarg : 1;
1985-01-08 09:59:28 +00:00
sprintf(app, "%%%d.%s", tokarg1, name);
return (memb_expr(tokpatset[tokarg1 - 1], name, app, tokarg));
1985-01-08 09:59:28 +00:00
}
expr_t perc_ident_expr(char *name)
{
1985-01-08 09:59:28 +00:00
char app[100];
sprintf(app, "%%%s", name);
return (memb_expr(cursetno, name, app, 0));
1985-01-08 09:59:28 +00:00
}
expr_t all_expr(int all_no, int subreg)
{
1985-01-08 09:59:28 +00:00
set_t localset;
register int i;
1985-01-08 09:59:28 +00:00
register short *sp;
expr_t result;
sp = l_props[allreg[all_no]].pr_regset;
for (i = 0; i < SETSIZE; i++)
localset.set_val[i] = i < SZOFSET(MAXREGS) ? sp[i] : 0;
subregset(localset.set_val, subreg, result.ex_regset);
1985-01-08 09:59:28 +00:00
result.ex_typ = TYPREG;
result.ex_index = ex_lookup(EX_ALLREG, all_no + 1, subreg);
return (result);
1985-01-08 09:59:28 +00:00
}
int eq2expr(expr_t e1, expr_t e2)
{
1985-01-08 09:59:28 +00:00
if (e1.ex_typ != e2.ex_typ)
error("Expressions around == should have equal type");
switch (e1.ex_typ)
{
default:
assert(0);
case TYPBOOL:
error("== on booleans not implemented");
case TYPINT:
return (EX_NCPEQ);
case TYPADDR:
return (EX_SCPEQ);
case TYPREG:
return (EX_RCPEQ);
1985-01-08 09:59:28 +00:00
}
}
int ne2expr(expr_t e1, expr_t e2)
{
1985-01-08 09:59:28 +00:00
if (e1.ex_typ != e2.ex_typ)
error("Expressions around != should have equal type");
switch (e1.ex_typ)
{
default:
assert(0);
case TYPBOOL:
error("!= on booleans not implemented");
case TYPINT:
return (EX_NCPNE);
case TYPADDR:
return (EX_SCPNE);
case TYPREG:
return (EX_RCPNE);
1985-01-08 09:59:28 +00:00
}
}
expr_t sum_expr(expr_t e1, expr_t e2)
{
int operator, op1, op2;
1985-01-08 09:59:28 +00:00
expr_t result;
operator = EX_CAT;
op1 = e1.ex_index;
op2 = e2.ex_index;
if (e1.ex_typ == e2.ex_typ)
{
1985-01-08 09:59:28 +00:00
result.ex_typ = e1.ex_typ;
if (e1.ex_typ == TYPINT)
operator = EX_PLUS;
else if (e1.ex_typ != TYPADDR)
error("+ is not implemented on this type");
}
else
{
1985-01-08 09:59:28 +00:00
result.ex_typ = TYPADDR;
if (e1.ex_typ != TYPADDR)
{
if (e1.ex_typ != TYPINT)
1985-01-08 09:59:28 +00:00
error("Wrong left operand of +");
op1 = ex_lookup(EX_TOSTRING, op1, 0);
1985-01-08 09:59:28 +00:00
}
if (e2.ex_typ != TYPADDR)
{
if (e2.ex_typ != TYPINT)
1985-01-08 09:59:28 +00:00
error("Wrong right operand of +");
op2 = ex_lookup(EX_TOSTRING, op2, 0);
1985-01-08 09:59:28 +00:00
}
}
result.ex_index = ex_lookup(operator, op1, op2);
return (result);
1985-01-08 09:59:28 +00:00
}
expr_t iextoaddr(expr_t e)
{
1985-01-08 09:59:28 +00:00
expr_t result;
result.ex_typ = TYPADDR;
result.ex_index = ex_lookup(EX_TOSTRING, e.ex_index, 0);
return (result);
1985-01-08 09:59:28 +00:00
}
expr_t regvar_expr(expr_t e, int regtyp, int regsiz)
{
1985-01-08 09:59:28 +00:00
expr_t result;
int i, regno, s, two_sizes;
two_sizes = (regtyp == reg_float && reglap != 0);
if (regsiz == -1)
{
/* reglap: Can't guess between 2 sizes of reg_float. */
if (two_sizes && regsiz == -1)
error("Must use regvar_w() or regvar_d()");
else
regsiz = rvsize[regtyp];
}
result = make_expr(TYPREG, EX_REGVAR, i_expr(e), regsiz);
for (i = 0; i < SZOFSET(MAXREGS); i++)
result.ex_regset[i] = 0;
/* s = the size of the registers in rvnumbers[regtyp] */
s = two_sizes ? reglap : rvsize[regtyp];
if (s == regsiz)
{
for (i = 0; i < nregvar[regtyp]; i++)
BIS(result.ex_regset, rvnumbers[regtyp][i]);
}
/* reglap: Also check the 2nd size of reg_float. */
if (two_sizes && rvsize[regtyp] == regsiz)
{
for (i = 0; i < nregvar[regtyp]; i++)
{
/* regno = first subregister */
regno = l_regs[rvnumbers[regtyp][i]].ri_memb[0];
if (regno != 0)
BIS(result.ex_regset, regno);
}
}
return (result);
1985-01-08 09:59:28 +00:00
}
1985-01-08 09:59:28 +00:00
/*
* Node table lookup part
*/
node_t nodes[MAXNODES];
int nnodes = 0;
1985-01-08 09:59:28 +00:00
void initnodes(void)
{
1985-01-08 09:59:28 +00:00
nodes[0].ex_operator = EX_CON;
nodes[0].ex_lnode = 0;
nodes[0].ex_rnode = 0;
nnodes++;
}
int ex_lookup(int operator, int lnode, int rnode)
{
1985-01-08 09:59:28 +00:00
register node_p p;
for (p = nodes + 1; p < &nodes[nnodes]; p++)
{
1985-01-08 09:59:28 +00:00
if (p->ex_operator != operator)
continue;
if (p->ex_lnode != lnode)
continue;
if (p->ex_rnode != rnode)
continue;
return (p - nodes);
1985-01-08 09:59:28 +00:00
}
NEXT(nnodes, MAXNODES, "Node");
1985-01-08 09:59:28 +00:00
p->ex_operator = operator;
p->ex_lnode = lnode;
p->ex_rnode = rnode;
return (p - nodes);
1985-01-08 09:59:28 +00:00
}