ack/lang/pc/comp/body.c

376 lines
7.2 KiB
C
Raw Normal View History

#include "parameters.h"
1988-10-26 15:21:11 +00:00
#include "debug.h"
#include <alloc.h>
#include <assert.h>
#include <em.h>
#include "LLlex.h"
#include "chk_expr.h"
#include "def.h"
#include "desig.h"
#include "idf.h"
#include "main.h"
1989-05-03 10:30:22 +00:00
#include "misc.h"
1988-10-26 15:21:11 +00:00
#include "node.h"
#include "scope.h"
#include "type.h"
#include "code.h"
#include "chk_expr.h"
#include "tmpvar.h"
#include "typequiv.h"
#include "error.h"
1988-10-26 15:21:11 +00:00
void MarkDef(register struct node *nd, unsigned short flags, int on)
1989-05-03 10:30:22 +00:00
{
while (nd && nd->nd_class != Def)
{
if ((nd->nd_class == Arrsel) || (nd->nd_class == LinkDef))
1989-05-03 10:30:22 +00:00
nd = nd->nd_left;
else if (nd->nd_class == Arrow)
1989-05-03 10:30:22 +00:00
nd = nd->nd_right;
else
break;
1989-05-03 10:30:22 +00:00
}
if (nd && (nd->nd_class == Def))
{
if ((flags & D_SET) && on && BlockScope != nd->nd_def->df_scope)
1989-05-03 10:30:22 +00:00
nd->nd_def->df_flags |= D_SETINHIGH;
if (on)
{
1994-11-01 11:45:23 +00:00
/*
if( (flags & D_SET) &&
(nd->nd_def->df_flags & D_WITH) )
node_warning(nd,
"variable \"%s\" already referenced in with",
nd->nd_def->df_idf->id_text);
*/
1989-05-03 10:30:22 +00:00
nd->nd_def->df_flags |= flags;
}
else
nd->nd_def->df_flags &= ~flags;
}
}
void AssertStat(register struct node *expp, unsigned short line)
1989-05-03 10:30:22 +00:00
{
struct desig dsr;
if (!ChkExpression(expp))
return;
1989-05-03 10:30:22 +00:00
if (expp->nd_type != bool_type)
{
1989-05-03 10:30:22 +00:00
node_error(expp, "type of assertion should be boolean");
return;
}
if (!options['a'] && !err_occurred)
{
1989-05-03 10:30:22 +00:00
dsr = InitDesig;
CodeExpr(expp, &dsr, NO_LABEL);
C_loc((arith) line);
1989-05-03 10:30:22 +00:00
C_cal("_ass");
}
}
1988-10-26 15:21:11 +00:00
void AssignStat(register struct node *left, register struct node *right)
1988-10-26 15:21:11 +00:00
{
register struct type *ltp, *rtp;
1989-05-03 10:30:22 +00:00
int retval = 0;
1988-10-26 15:21:11 +00:00
struct desig dsr;
1989-05-03 10:30:22 +00:00
retval = ChkExpression(right);
MarkUsed(right);
retval &= ChkLhs(left);
1988-10-26 15:21:11 +00:00
ltp = left->nd_type;
rtp = right->nd_type;
MarkDef(left, (unsigned short) D_SET, 1);
1989-05-03 10:30:22 +00:00
if (!retval)
return;
1989-05-03 10:30:22 +00:00
if (ltp == int_type && rtp == long_type)
{
1989-05-03 10:30:22 +00:00
right = MkNode(IntReduc, NULLNODE, right, &dot);
right->nd_type = int_type;
}
else if (ltp == long_type && rtp == int_type)
{
1989-05-03 10:30:22 +00:00
right = MkNode(IntCoerc, NULLNODE, right, &dot);
right->nd_type = long_type;
}
if (!TstAssCompat(ltp, rtp))
{
1988-10-26 15:21:11 +00:00
node_error(left, "type incompatibility in assignment");
return;
}
if (left->nd_class == Def && (left->nd_def->df_flags & D_INLOOP))
{
1989-05-03 10:30:22 +00:00
node_error(left, "assignment to a control variable");
return;
}
if (rtp == emptyset_type)
1988-10-26 15:21:11 +00:00
right->nd_type = ltp;
if (!err_occurred)
{
1988-10-26 15:21:11 +00:00
dsr = InitDesig;
CodeExpr(right, &dsr, NO_LABEL);
if (rtp->tp_fund & (T_ARRAY | T_RECORD))
1988-10-26 15:21:11 +00:00
CodeAddress(&dsr);
else
{
1988-10-26 15:21:11 +00:00
CodeValue(&dsr, rtp);
if (ltp == real_type && BaseType(rtp) == int_type)
1989-05-03 10:30:22 +00:00
Int2Real(rtp->tp_size);
1988-10-26 15:21:11 +00:00
RangeCheck(ltp, rtp);
}
CodeMove(&dsr, left, rtp);
}
FreeNode(left);
FreeNode(right);
}
void ProcStat(register struct node *nd)
1988-10-26 15:21:11 +00:00
{
if (!ChkCall(nd))
return;
1988-10-26 15:21:11 +00:00
if (nd->nd_type)
{
1988-10-26 15:21:11 +00:00
node_error(nd, "procedure call expected");
return;
}
}
void ChkForStat(register struct node *nd)
1988-10-26 15:21:11 +00:00
{
register struct def *df;
1989-05-03 10:30:22 +00:00
int retvar = 0;
retvar = ChkVariable(nd);
retvar &= ChkExpression(nd->nd_left);
MarkUsed(nd->nd_left);
retvar &= ChkExpression(nd->nd_right);
MarkUsed(nd->nd_right);
if (!retvar)
return;
1988-10-26 15:21:11 +00:00
assert(nd->nd_class == Def);
df = nd->nd_def;
if (df->df_scope != BlockScope)
{
1988-10-26 15:21:11 +00:00
node_error(nd, "for loop: control variable must be local");
return;
}
assert(df->df_kind == D_VARIABLE);
if (df->df_scope != GlobalScope && df->var_off >= 0)
{
node_error(nd, "for loop: control variable can't be a parameter");
MarkDef(nd, (unsigned short) (D_LOOPVAR | D_SET | D_USED), 1);
1989-05-03 10:30:22 +00:00
return;
1988-10-26 15:21:11 +00:00
}
if (!(df->df_type->tp_fund & T_ORDINAL))
{
1988-10-26 15:21:11 +00:00
node_error(nd, "for loop: control variable must be ordinal");
MarkDef(nd, (unsigned short) (D_LOOPVAR | D_SET | D_USED), 1);
1988-10-26 15:21:11 +00:00
return;
}
if (!TstCompat(df->df_type, nd->nd_left->nd_type))
1988-10-26 15:21:11 +00:00
node_error(nd,
"for loop: initial value incompatible with control variable");
1988-10-26 15:21:11 +00:00
if (!TstCompat(df->df_type, nd->nd_right->nd_type))
1988-10-26 15:21:11 +00:00
node_error(nd,
"for loop: final value incompatible with control variable");
if (df->df_type == long_type)
1989-05-03 10:30:22 +00:00
node_error(nd, "for loop: control variable can not be a long");
if (df->df_flags & D_INLOOP)
1989-05-03 10:30:22 +00:00
node_error(nd, "for loop: control variable already used");
if (df->df_flags & D_SETINHIGH)
node_error(nd, "for loop: control variable already set in block");
1989-05-03 10:30:22 +00:00
MarkDef(nd, (unsigned short) (D_LOOPVAR | D_INLOOP | D_SET | D_USED), 1);
1988-10-26 15:21:11 +00:00
return;
}
void EndForStat(register struct node *nd)
1989-05-03 10:30:22 +00:00
{
register struct def *df;
df = nd->nd_def;
if ((df->df_scope != BlockScope)
|| (df->df_scope != GlobalScope && df->var_off >= 0)
|| !(df->df_type->tp_fund & T_ORDINAL))
1989-05-03 10:30:22 +00:00
return;
MarkDef(nd, (unsigned short) (D_INLOOP | D_SET), 0);
1989-05-03 10:30:22 +00:00
}
arith CodeInitFor(register struct node *nd, int priority)
1988-10-26 15:21:11 +00:00
{
/* Push final-value, the value may only be evaluated
once, so generate a temporary for it, when not a constant.
*/
1988-10-26 15:21:11 +00:00
arith tmp;
CodePExpr(nd);
if (nd->nd_class != Value)
{
1988-10-26 15:21:11 +00:00
tmp = NewInt(priority);
1989-05-03 10:30:22 +00:00
1988-10-26 15:21:11 +00:00
C_dup(int_size);
C_stl(tmp);
1989-05-03 10:30:22 +00:00
1988-10-26 15:21:11 +00:00
return tmp;
}
return (arith) 0;
}
void CodeFor(struct node *nd, int stepsize, label l1, label l2)
1988-10-26 15:21:11 +00:00
{
/* Test if loop has to be done */
if (stepsize == 1) /* TO */
1988-10-26 15:21:11 +00:00
C_bgt(l2);
else
/* DOWNTO */
1988-10-26 15:21:11 +00:00
C_blt(l2);
/* Label at begin of the body */
C_df_ilb(l1);
RangeCheck(nd->nd_type, nd->nd_left->nd_type);
CodeDStore(nd);
}
void CodeEndFor(struct node *nd, int stepsize, label l1, label l2, arith tmp2)
1988-10-26 15:21:11 +00:00
{
/* Test if loop has to be done once more */
CodePExpr(nd);
C_dup(int_size);
if (tmp2)
1988-10-26 15:21:11 +00:00
C_lol(tmp2);
else
CodePExpr(nd->nd_right);
C_beq(l2);
/* Increment/decrement the control-variable */
if (stepsize == 1) /* TO */
1988-10-26 15:21:11 +00:00
C_inc();
else
/* DOWNTO */
1988-10-26 15:21:11 +00:00
C_dec();
C_bra(l1);
/* Exit label */
C_df_ilb(l2);
1989-10-12 11:10:48 +00:00
C_asp(int_size);
1988-10-26 15:21:11 +00:00
}
void WithStat(struct node *nd)
1988-10-26 15:21:11 +00:00
{
struct withdesig *wds;
struct desig ds;
struct scopelist *scl;
if (nd->nd_type->tp_fund != T_RECORD)
{
1988-10-26 15:21:11 +00:00
node_error(nd, "record variable expected");
return;
}
MarkDef(nd, (unsigned short) (D_USED | D_SET | D_WITH), 1);
1989-05-03 10:30:22 +00:00
/*
if( (nd->nd_class == Arrow) &&
(nd->nd_right->nd_type->tp_fund & T_FILE) ) {
nd->nd_right->nd_def->df_flags |= D_WITH;
}
*/
1989-05-03 10:30:22 +00:00
scl = new_scopelist();
scl->sc_scope = nd->nd_type->rec_scope;
scl->next = CurrVis;
CurrVis = scl;
if (err_occurred)
return;
1988-10-26 15:21:11 +00:00
/* Generate code */
CodeDAddress(nd);
wds = new_withdesig();
wds->w_next = WithDesigs;
WithDesigs = wds;
1989-05-03 10:30:22 +00:00
wds->w_scope = scl->sc_scope;
1988-10-26 15:21:11 +00:00
/* create a desig structure for the temporary */
ds.dsg_kind = DSG_FIXED;
ds.dsg_offset = NewPtr(1);
ds.dsg_name = 0;
/* need some pointertype to store pointer */
CodeStore(&ds, nil_type);
/* record is indirectly available */
ds.dsg_kind = DSG_PFIXED;
wds->w_desig = ds;
}
void EndWith(struct scopelist *saved_scl, struct node *nd)
1988-10-26 15:21:11 +00:00
{
/* restore scope, and release structures */
struct scopelist *scl;
struct withdesig *wds;
1989-05-03 10:30:22 +00:00
struct node *nd1;
1988-10-26 15:21:11 +00:00
while (CurrVis != saved_scl)
{
1988-10-26 15:21:11 +00:00
/* release scopelist */
scl = CurrVis;
CurrVis = CurrVis->next;
free_scopelist(scl);
if (WithDesigs == 0)
continue; /* we didn't generate any code */
1989-05-03 10:30:22 +00:00
1988-10-26 15:21:11 +00:00
/* release temporary */
FreePtr(WithDesigs->w_desig.dsg_offset);
/* release withdesig */
wds = WithDesigs;
WithDesigs = WithDesigs->w_next;
free_withdesig(wds);
}
1989-05-03 10:30:22 +00:00
for (nd1 = nd; nd1 != NULLNODE; nd1 = nd1->nd_right)
{
MarkDef(nd1->nd_left, (unsigned short) (D_WITH), 0);
1989-05-03 10:30:22 +00:00
}
1988-10-26 15:21:11 +00:00
FreeNode(nd);
}