1988-10-26 15:21:11 +00:00
|
|
|
/* C O D E G E N E R A T I O N R O U T I N E S */
|
|
|
|
|
2006-07-22 21:01:15 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-05-14 19:47:04 +00:00
|
|
|
#include "parameters.h"
|
1988-10-26 15:21:11 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include <assert.h>
|
|
|
|
#include <em.h>
|
|
|
|
#include <em_reg.h>
|
1989-05-03 10:30:22 +00:00
|
|
|
#include <em_abs.h>
|
1988-10-26 15:21:11 +00:00
|
|
|
|
|
|
|
#include "LLlex.h"
|
|
|
|
#include "Lpars.h"
|
|
|
|
#include "def.h"
|
|
|
|
#include "desig.h"
|
1989-05-03 10:30:22 +00:00
|
|
|
#include "f_info.h"
|
|
|
|
#include "idf.h"
|
1988-10-26 15:21:11 +00:00
|
|
|
#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 "required.h"
|
|
|
|
#include "scope.h"
|
|
|
|
#include "type.h"
|
2019-02-23 16:44:50 +00:00
|
|
|
#include "code.h"
|
|
|
|
#include "tmpvar.h"
|
|
|
|
#include "typequiv.h"
|
|
|
|
#include "error.h"
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
int fp_used;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
static void CodeUoper(register struct node *);
|
|
|
|
static void CodeBoper(register struct node *, /* the expression tree itself */
|
|
|
|
label);
|
|
|
|
static void CodeSet(register struct node *);
|
|
|
|
static void CodeEl(register struct node *, register struct type *);
|
|
|
|
static void CodePString(struct node *, struct type *);
|
|
|
|
/* General internal system API calls */
|
|
|
|
static void CodeStd(struct node *);
|
2016-11-10 21:04:18 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
static void genrck(register struct type *);
|
|
|
|
static void RegisterMessages(register struct def *);
|
|
|
|
static void CodeConfDescr(register struct type *, register struct type *);
|
|
|
|
|
|
|
|
extern void call_ini(void);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void CodeFil(void)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
2019-02-23 16:44:50 +00:00
|
|
|
if (!options['L'])
|
|
|
|
C_fil_dlb((label ) 1, (arith) 0);
|
1988-10-26 15:21:11 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
void routine_label(register struct def * df)
|
1989-05-03 10:30:22 +00:00
|
|
|
{
|
|
|
|
df->prc_label = ++data_label;
|
|
|
|
C_df_dlb(df->prc_label);
|
1991-02-18 11:11:04 +00:00
|
|
|
C_rom_scon(df->df_idf->id_text, (arith)(strlen(df->df_idf->id_text) + 1));
|
1989-05-03 10:30:22 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
void RomString(register struct node *nd)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
C_df_dlb(++data_label);
|
1989-05-03 10:30:22 +00:00
|
|
|
|
|
|
|
/* A string of the string_type is null-terminated. */
|
2019-02-23 16:44:50 +00:00
|
|
|
if (nd->nd_type == string_type)
|
|
|
|
C_rom_scon(nd->nd_STR, nd->nd_SLE + 1); /* with trailing '\0' */
|
1989-05-03 10:30:22 +00:00
|
|
|
else
|
2019-02-23 16:44:50 +00:00
|
|
|
C_rom_scon(nd->nd_STR, nd->nd_SLE); /* no trailing '\0' */
|
1989-05-03 10:30:22 +00:00
|
|
|
|
1988-10-26 15:21:11 +00:00
|
|
|
nd->nd_SLA = data_label;
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
void RomReal(register struct node *nd)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
2019-02-23 16:44:50 +00:00
|
|
|
if (!nd->nd_RLA)
|
|
|
|
{
|
1991-11-01 10:15:15 +00:00
|
|
|
C_df_dlb(++data_label);
|
|
|
|
nd->nd_RLA = data_label;
|
|
|
|
C_rom_fcon(nd->nd_REL, nd->nd_type->tp_size);
|
|
|
|
}
|
1988-10-26 15:21:11 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
void BssVar(void)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
/* generate bss segments for global variables */
|
|
|
|
register struct def *df = GlobalScope->sc_def;
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
while (df)
|
|
|
|
{
|
|
|
|
if (df->df_kind == D_VARIABLE)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
C_df_dnam(df->var_name);
|
|
|
|
|
|
|
|
/* ??? undefined value ??? */
|
|
|
|
C_bss_cst(df->df_type->tp_size, (arith) 0, 0);
|
|
|
|
}
|
|
|
|
df = df->df_nextinscope;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
static arith CodeGtoDescr(register struct scope *sc)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
/* Create code for goto descriptors
|
2019-02-23 16:44:50 +00:00
|
|
|
*/
|
1988-10-26 15:21:11 +00:00
|
|
|
|
|
|
|
register struct node *lb = sc->sc_lablist;
|
|
|
|
int first = 1;
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
while (lb)
|
|
|
|
{
|
|
|
|
if (lb->nd_def->lab_descr)
|
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
/* create local for target SP */
|
|
|
|
sc->sc_off = -WA(pointer_size - sc->sc_off);
|
|
|
|
C_ms_gto();
|
|
|
|
first = 0;
|
|
|
|
}
|
|
|
|
C_df_dlb(lb->nd_def->lab_descr);
|
|
|
|
C_rom_ilb(lb->nd_def->lab_no);
|
|
|
|
C_rom_cst(sc->sc_off);
|
|
|
|
}
|
|
|
|
lb = lb->nd_next;
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
if (!first)
|
1988-10-26 15:21:11 +00:00
|
|
|
return sc->sc_off;
|
|
|
|
else
|
|
|
|
return (arith) 0;
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
arith CodeBeginBlock(register struct def *df)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
/* Generate code at the beginning of the main program,
|
2019-02-23 16:44:50 +00:00
|
|
|
procedure or function.
|
|
|
|
*/
|
1988-10-26 15:21:11 +00:00
|
|
|
|
|
|
|
arith StackAdjustment = 0;
|
2019-02-23 16:44:50 +00:00
|
|
|
arith offset = 0; /* offset to save StackPointer */
|
1988-10-26 15:21:11 +00:00
|
|
|
|
|
|
|
TmpOpen(df->prc_vis->sc_scope);
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (df->df_kind == D_MODULE) /* nothing */
|
|
|
|
;
|
|
|
|
else if (df->df_kind == D_PROGRAM)
|
|
|
|
{
|
1991-03-06 14:26:16 +00:00
|
|
|
C_exp("_m_a_i_n");
|
|
|
|
C_pro_narg("_m_a_i_n");
|
1988-10-26 15:21:11 +00:00
|
|
|
C_ms_par((arith) 0);
|
|
|
|
offset = CodeGtoDescr(df->prc_vis->sc_scope);
|
|
|
|
CodeFil();
|
|
|
|
|
1988-11-16 15:18:21 +00:00
|
|
|
/* initialize external files */
|
|
|
|
call_ini();
|
2019-02-23 16:44:50 +00:00
|
|
|
/* ignore floating point underflow */C_lim();
|
|
|
|
C_loc((arith)(1 << EFUNFL));
|
1989-05-03 10:30:22 +00:00
|
|
|
C_ior(int_size);
|
|
|
|
C_sim();
|
1991-02-14 18:15:22 +00:00
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
else if (df->df_kind & (D_PROCEDURE | D_FUNCTION))
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
struct type *tp;
|
|
|
|
register struct paramlist *param;
|
|
|
|
|
|
|
|
C_pro_narg(df->prc_name);
|
|
|
|
C_ms_par(df->df_type->prc_nbpar);
|
|
|
|
|
|
|
|
offset = CodeGtoDescr(df->prc_vis->sc_scope);
|
|
|
|
CodeFil();
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (options['t'])
|
|
|
|
{
|
|
|
|
C_lae_dlb(df->prc_label, (arith) 0);
|
1989-05-03 10:30:22 +00:00
|
|
|
C_cal("procentry");
|
|
|
|
C_asp(pointer_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prc_bool is the local variable that indicates if the
|
|
|
|
* function result is assigned. This and can be disabled
|
|
|
|
* with the -R option. The variable, however, is always
|
|
|
|
* allocated and initialized.
|
|
|
|
*/
|
2019-02-23 16:44:50 +00:00
|
|
|
if (df->prc_res)
|
|
|
|
{
|
1989-05-03 10:30:22 +00:00
|
|
|
C_zer((arith) int_size);
|
|
|
|
C_stl(df->prc_bool);
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
for (param = ParamList(df->df_type) ; param; param = param->next)
|
|
|
|
{
|
|
|
|
if (!IsVarParam(param))
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
tp = TypeOfParam(param);
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (IsConformantArray(tp))
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
/* Here, we have to make a copy of the
|
2019-02-23 16:44:50 +00:00
|
|
|
array. We must also remember how much
|
|
|
|
room is reserved for copies, because
|
|
|
|
we have to adjust the stack pointer
|
|
|
|
before we return.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!StackAdjustment)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
/* First time we get here
|
2019-02-23 16:44:50 +00:00
|
|
|
*/
|
1988-10-26 15:21:11 +00:00
|
|
|
StackAdjustment = NewInt(0);
|
|
|
|
C_loc((arith) 0);
|
|
|
|
C_stl(StackAdjustment);
|
|
|
|
}
|
|
|
|
/* Address of array */
|
|
|
|
C_lol(param->par_def->var_off);
|
|
|
|
|
|
|
|
/* First compute size of the array */
|
|
|
|
C_lol(tp->arr_cfdescr + word_size);
|
|
|
|
C_inc();
|
2019-02-23 16:44:50 +00:00
|
|
|
/* gives number of elements */
|
1988-10-26 15:21:11 +00:00
|
|
|
C_lol(tp->arr_cfdescr + 2 * word_size);
|
2019-02-23 16:44:50 +00:00
|
|
|
/* size of elements */
|
1988-10-26 15:21:11 +00:00
|
|
|
C_mli(word_size);
|
|
|
|
C_loc(word_size - 1);
|
|
|
|
C_adi(word_size);
|
1991-09-24 10:45:43 +00:00
|
|
|
C_loc(word_size - 1);
|
|
|
|
C_com(word_size);
|
|
|
|
C_and(word_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
C_dup(word_size);
|
|
|
|
C_lol(StackAdjustment);
|
|
|
|
C_adi(word_size);
|
|
|
|
C_stl(StackAdjustment);
|
2019-02-23 16:44:50 +00:00
|
|
|
/* remember stack adjustments */
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
C_los(word_size); /* copy */
|
|
|
|
C_lor((arith) 1);
|
|
|
|
/* push new address of array
|
|
|
|
... downwards ... ???
|
|
|
|
*/
|
1988-10-26 15:21:11 +00:00
|
|
|
C_stl(param->par_def->var_off);
|
|
|
|
}
|
|
|
|
}
|
1991-02-14 18:15:22 +00:00
|
|
|
}
|
1988-10-26 15:21:11 +00:00
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
else
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
crash("(CodeBeginBlock)");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (offset)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
/* save SP for non-local jump */
|
|
|
|
C_lor((arith) 1);
|
|
|
|
C_stl(offset);
|
|
|
|
}
|
|
|
|
return StackAdjustment;
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
void CodeEndBlock(register struct def *df, arith StackAdjustment)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
2019-02-23 16:44:50 +00:00
|
|
|
if (df->df_kind == D_PROGRAM)
|
|
|
|
{
|
1991-02-14 18:15:22 +00:00
|
|
|
C_loc((arith) 0);
|
|
|
|
C_cal("_hlt");
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
else if (df->df_kind & (D_PROCEDURE | D_FUNCTION))
|
|
|
|
{
|
1991-02-14 18:15:22 +00:00
|
|
|
struct type *tp;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (StackAdjustment)
|
|
|
|
{
|
1991-02-14 18:15:22 +00:00
|
|
|
/* remove copies of conformant arrays */
|
|
|
|
C_lol(StackAdjustment);
|
|
|
|
C_ass(word_size);
|
|
|
|
FreeInt(StackAdjustment);
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
if (!options['n'])
|
1991-02-14 18:15:22 +00:00
|
|
|
RegisterMessages(df->prc_vis->sc_scope->sc_def);
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (options['t'])
|
|
|
|
{
|
|
|
|
C_lae_dlb(df->prc_label, (arith) 0);
|
1991-02-14 18:15:22 +00:00
|
|
|
C_cal("procexit");
|
|
|
|
C_asp(pointer_size);
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
if ( (tp = ResultType(df->df_type)) )
|
|
|
|
{
|
|
|
|
if (!options['R'])
|
|
|
|
{
|
|
|
|
C_lin((arith) LineNumber);
|
1991-02-14 18:15:22 +00:00
|
|
|
C_lol(df->prc_bool);
|
|
|
|
C_cal("_nfa");
|
|
|
|
C_asp(word_size);
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
if (tp->tp_size == word_size)
|
1991-02-14 18:15:22 +00:00
|
|
|
C_lol(-tp->tp_size);
|
2019-02-23 16:44:50 +00:00
|
|
|
else if (tp->tp_size == 2 * word_size)
|
1991-02-14 18:15:22 +00:00
|
|
|
C_ldl(-tp->tp_size);
|
2019-02-23 16:44:50 +00:00
|
|
|
else
|
|
|
|
{
|
1991-02-14 18:15:22 +00:00
|
|
|
C_lal(-tp->tp_size);
|
|
|
|
C_loi(tp->tp_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
}
|
|
|
|
|
1991-02-14 18:15:22 +00:00
|
|
|
C_ret(tp->tp_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
}
|
1991-02-14 18:15:22 +00:00
|
|
|
else
|
|
|
|
C_ret((arith) 0);
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
else
|
|
|
|
{
|
1991-02-14 18:15:22 +00:00
|
|
|
crash("(CodeEndBlock)");
|
|
|
|
/*NOTREACHED*/
|
1988-10-26 15:21:11 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
C_end(-df->prc_vis->sc_scope->sc_off);
|
1988-10-26 15:21:11 +00:00
|
|
|
TmpClose();
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
void CodeExpr(register struct node *nd, register struct desig *ds,
|
|
|
|
label true_label)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
register struct type *tp = nd->nd_type;
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (tp->tp_fund == T_REAL)
|
|
|
|
fp_used = 1;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
switch (nd->nd_class)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
case Value:
|
2019-02-23 16:44:50 +00:00
|
|
|
switch (nd->nd_symb)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
case INTEGER:
|
|
|
|
C_loc(nd->nd_INT);
|
|
|
|
break;
|
|
|
|
case REAL:
|
1991-11-01 10:15:15 +00:00
|
|
|
RomReal(nd);
|
1988-10-26 15:21:11 +00:00
|
|
|
C_lae_dlb(nd->nd_RLA, (arith) 0);
|
|
|
|
C_loi(tp->tp_size);
|
|
|
|
break;
|
|
|
|
case STRING:
|
2019-02-23 16:44:50 +00:00
|
|
|
if (tp->tp_fund == T_CHAR)
|
1988-10-26 15:21:11 +00:00
|
|
|
C_loc(nd->nd_INT);
|
|
|
|
else
|
|
|
|
C_lae_dlb(nd->nd_SLA, (arith) 0);
|
|
|
|
break;
|
|
|
|
case NIL:
|
|
|
|
C_zer(pointer_size);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
crash("(CodeExpr Value)");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
ds->dsg_kind = DSG_LOADED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Uoper:
|
|
|
|
CodeUoper(nd);
|
|
|
|
ds->dsg_kind = DSG_LOADED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Boper:
|
|
|
|
CodeBoper(nd, true_label);
|
|
|
|
ds->dsg_kind = DSG_LOADED;
|
|
|
|
true_label = NO_LABEL;
|
|
|
|
break;
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case Set:
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
register arith *st = nd->nd_set;
|
|
|
|
register int i;
|
|
|
|
|
|
|
|
ds->dsg_kind = DSG_LOADED;
|
2019-02-23 16:44:50 +00:00
|
|
|
if (!st)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
C_zer(tp->tp_size);
|
|
|
|
break;
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
for (i = tp->tp_size / word_size, st += i; i > 0; i--)
|
1988-10-26 15:21:11 +00:00
|
|
|
C_loc(*--st);
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
}
|
1988-10-26 15:21:11 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Xset:
|
|
|
|
CodeSet(nd);
|
|
|
|
ds->dsg_kind = DSG_LOADED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Call:
|
|
|
|
CodeCall(nd);
|
|
|
|
ds->dsg_kind = DSG_LOADED;
|
|
|
|
break;
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case NameOrCall:
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
/* actual procedure/function parameter */
|
|
|
|
struct node *left = nd->nd_left;
|
|
|
|
struct def *df = left->nd_def;
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (df->df_kind & D_ROUTINE)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
int level = df->df_scope->sc_level;
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (level <= 0 || (df->df_flags & D_EXTERNAL))
|
1988-10-26 15:21:11 +00:00
|
|
|
C_zer(pointer_size);
|
|
|
|
else
|
2019-02-23 16:44:50 +00:00
|
|
|
C_lxl((arith)(proclevel - level));
|
1988-10-26 15:21:11 +00:00
|
|
|
|
|
|
|
C_lpi(df->prc_name);
|
|
|
|
ds->dsg_kind = DSG_LOADED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(df->df_kind == D_VARIABLE);
|
|
|
|
assert(df->df_type->tp_fund & T_ROUTINE);
|
|
|
|
|
|
|
|
CodeDesig(left, ds);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Arrow:
|
|
|
|
case Arrsel:
|
|
|
|
case Def:
|
|
|
|
case LinkDef:
|
|
|
|
CodeDesig(nd, ds);
|
|
|
|
break;
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case Cast:
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
/* convert integer to real */
|
|
|
|
struct node *right = nd->nd_right;
|
|
|
|
|
|
|
|
CodePExpr(right);
|
1989-05-03 10:30:22 +00:00
|
|
|
Int2Real(right->nd_type->tp_size);
|
|
|
|
ds->dsg_kind = DSG_LOADED;
|
|
|
|
break;
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
case IntCoerc:
|
|
|
|
{
|
1989-05-03 10:30:22 +00:00
|
|
|
/* convert integer to long integer */
|
|
|
|
struct node *right = nd->nd_right;
|
|
|
|
|
|
|
|
CodePExpr(right);
|
|
|
|
Int2Long();
|
1988-10-26 15:21:11 +00:00
|
|
|
ds->dsg_kind = DSG_LOADED;
|
|
|
|
break;
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
case IntReduc:
|
|
|
|
{
|
1989-05-03 10:30:22 +00:00
|
|
|
/* convert a long to an integer */
|
|
|
|
struct node *right = nd->nd_right;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
1989-05-03 10:30:22 +00:00
|
|
|
CodePExpr(right);
|
|
|
|
Long2Int();
|
|
|
|
ds->dsg_kind = DSG_LOADED;
|
|
|
|
break;
|
|
|
|
}
|
1988-10-26 15:21:11 +00:00
|
|
|
default:
|
|
|
|
crash("(CodeExpr : bad node type)");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
} /* switch class */
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (true_label)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
/* Only for boolean expressions
|
2019-02-23 16:44:50 +00:00
|
|
|
*/
|
1988-10-26 15:21:11 +00:00
|
|
|
CodeValue(ds, tp);
|
|
|
|
C_zeq(true_label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
static void CodeUoper(register struct node *nd)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
register struct type *tp = nd->nd_type;
|
|
|
|
|
|
|
|
CodePExpr(nd->nd_right);
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
switch (nd->nd_symb)
|
|
|
|
{
|
|
|
|
case '-':
|
|
|
|
assert(tp->tp_fund & T_NUMERIC);
|
|
|
|
if (tp->tp_fund == T_INTEGER || tp->tp_fund == T_LONG)
|
|
|
|
C_ngi(tp->tp_size);
|
|
|
|
else
|
|
|
|
C_ngf(tp->tp_size);
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case NOT:
|
|
|
|
C_teq();
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case '(':
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
default:
|
|
|
|
crash("(CodeUoper)");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* truthvalue() serves as an auxiliary function of CodeBoper */
|
|
|
|
static void truthvalue(int relop)
|
|
|
|
{
|
|
|
|
switch (relop)
|
|
|
|
{
|
|
|
|
case '<':
|
|
|
|
C_tlt();
|
|
|
|
break;
|
|
|
|
case LESSEQUAL:
|
|
|
|
C_tle();
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
C_tgt();
|
|
|
|
break;
|
|
|
|
case GREATEREQUAL:
|
|
|
|
C_tge();
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
C_teq();
|
|
|
|
break;
|
|
|
|
case NOTEQUAL:
|
|
|
|
C_tne();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
crash("(truthvalue)");
|
|
|
|
/*NOTREACHED*/
|
1988-10-26 15:21:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void Operands(register struct node *leftop, register struct node *rightop)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
CodePExpr(leftop);
|
|
|
|
CodePExpr(rightop);
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
static void CodeBoper(register struct node *expr, /* the expression tree itself */
|
|
|
|
label true_label) /* label to jump to in logical exprs */
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
register struct node *leftop = expr->nd_left;
|
|
|
|
register struct node *rightop = expr->nd_right;
|
|
|
|
register struct type *tp = expr->nd_type;
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
switch (expr->nd_symb)
|
|
|
|
{
|
|
|
|
case '+':
|
|
|
|
Operands(leftop, rightop);
|
|
|
|
switch (tp->tp_fund)
|
|
|
|
{
|
|
|
|
case T_INTEGER:
|
|
|
|
case T_LONG:
|
|
|
|
C_adi(tp->tp_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
break;
|
2019-02-23 16:44:50 +00:00
|
|
|
case T_REAL:
|
|
|
|
C_adf(tp->tp_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
break;
|
2019-02-23 16:44:50 +00:00
|
|
|
case T_SET:
|
|
|
|
C_ior(tp->tp_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
break;
|
2019-02-23 16:44:50 +00:00
|
|
|
default:
|
|
|
|
crash("(CodeBoper: bad type +)");
|
|
|
|
}
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case '-':
|
|
|
|
Operands(leftop, rightop);
|
|
|
|
switch (tp->tp_fund)
|
|
|
|
{
|
|
|
|
case T_INTEGER:
|
|
|
|
case T_LONG:
|
|
|
|
C_sbi(tp->tp_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
break;
|
2019-02-23 16:44:50 +00:00
|
|
|
case T_REAL:
|
|
|
|
C_sbf(tp->tp_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
break;
|
2019-02-23 16:44:50 +00:00
|
|
|
case T_SET:
|
|
|
|
C_com(tp->tp_size);
|
|
|
|
C_and(tp->tp_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
break;
|
2019-02-23 16:44:50 +00:00
|
|
|
default:
|
|
|
|
crash("(CodeBoper: bad type -)");
|
|
|
|
}
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case '*':
|
|
|
|
Operands(leftop, rightop);
|
|
|
|
switch (tp->tp_fund)
|
|
|
|
{
|
|
|
|
case T_INTEGER:
|
|
|
|
case T_LONG:
|
|
|
|
C_mli(tp->tp_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
break;
|
2019-02-23 16:44:50 +00:00
|
|
|
case T_REAL:
|
|
|
|
C_mlf(tp->tp_size);
|
|
|
|
break;
|
|
|
|
case T_SET:
|
|
|
|
C_and(tp->tp_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
break;
|
|
|
|
default:
|
2019-02-23 16:44:50 +00:00
|
|
|
crash("(CodeBoper: bad type *)");
|
|
|
|
}
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case '/':
|
|
|
|
Operands(leftop, rightop);
|
|
|
|
if (tp->tp_fund == T_REAL)
|
|
|
|
C_dvf(tp->tp_size);
|
|
|
|
else
|
|
|
|
crash("(CodeBoper: bad type /)");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIV:
|
|
|
|
case MOD:
|
|
|
|
Operands(leftop, rightop);
|
|
|
|
if (tp->tp_fund == T_INTEGER)
|
|
|
|
{
|
|
|
|
C_cal(expr->nd_symb == MOD ? "_mdi" : "_dvi");
|
|
|
|
C_asp(2 * tp->tp_size);
|
|
|
|
C_lfr(tp->tp_size);
|
|
|
|
}
|
|
|
|
else if (tp->tp_fund == T_LONG)
|
|
|
|
{
|
|
|
|
C_cal(expr->nd_symb == MOD ? "_mdil" : "_dvil");
|
|
|
|
C_asp(2 * tp->tp_size);
|
|
|
|
C_lfr(tp->tp_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
crash("(CodeBoper: bad type MOD)");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '<':
|
|
|
|
case LESSEQUAL:
|
|
|
|
case '>':
|
|
|
|
case GREATEREQUAL:
|
|
|
|
case '=':
|
|
|
|
case NOTEQUAL:
|
|
|
|
CodePExpr(leftop);
|
|
|
|
CodePExpr(rightop);
|
|
|
|
tp = BaseType(rightop->nd_type);
|
|
|
|
|
|
|
|
switch (tp->tp_fund)
|
|
|
|
{
|
|
|
|
case T_INTEGER:
|
|
|
|
case T_LONG:
|
|
|
|
C_cmi(tp->tp_size);
|
|
|
|
break;
|
|
|
|
case T_REAL:
|
|
|
|
C_cmf(tp->tp_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
break;
|
2019-02-23 16:44:50 +00:00
|
|
|
case T_ENUMERATION:
|
|
|
|
case T_CHAR:
|
|
|
|
C_cmu(word_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
break;
|
2019-02-23 16:44:50 +00:00
|
|
|
case T_POINTER:
|
|
|
|
C_cmp();
|
1988-10-26 15:21:11 +00:00
|
|
|
break;
|
2019-02-23 16:44:50 +00:00
|
|
|
|
|
|
|
case T_SET:
|
|
|
|
if (expr->nd_symb == GREATEREQUAL)
|
|
|
|
{
|
|
|
|
/* A >= B is the same as A equals A + B
|
|
|
|
*/
|
|
|
|
C_dup(2 * tp->tp_size);
|
|
|
|
C_asp(tp->tp_size);
|
|
|
|
C_ior(tp->tp_size);
|
|
|
|
expr->nd_symb = '=';
|
|
|
|
}
|
|
|
|
else if (expr->nd_symb == LESSEQUAL)
|
|
|
|
{
|
|
|
|
/* A <= B is the same as A - B = []
|
|
|
|
*/
|
|
|
|
C_com(tp->tp_size);
|
|
|
|
C_and(tp->tp_size);
|
|
|
|
C_zer(tp->tp_size);
|
|
|
|
expr->nd_symb = '=';
|
|
|
|
}
|
|
|
|
C_cms(tp->tp_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
break;
|
2019-02-23 16:44:50 +00:00
|
|
|
|
|
|
|
case T_STRINGCONST:
|
|
|
|
case T_ARRAY:
|
|
|
|
C_loc((arith) IsString(tp));
|
|
|
|
C_cal("_bcp");
|
|
|
|
C_asp(2 * pointer_size + word_size);
|
|
|
|
C_lfr(word_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
break;
|
2019-02-23 16:44:50 +00:00
|
|
|
|
|
|
|
case T_STRING:
|
|
|
|
C_cmp();
|
1988-10-26 15:21:11 +00:00
|
|
|
break;
|
2019-02-23 16:44:50 +00:00
|
|
|
|
1988-10-26 15:21:11 +00:00
|
|
|
default:
|
2019-02-23 16:44:50 +00:00
|
|
|
crash("(CodeBoper : bad type COMPARE)");
|
|
|
|
}
|
|
|
|
truthvalue(expr->nd_symb);
|
|
|
|
if (true_label != NO_LABEL )
|
|
|
|
C_zeq(true_label);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IN:
|
|
|
|
/* In this case, evaluate right hand side first! The INN
|
|
|
|
instruction expects the bit number on top of the stack
|
|
|
|
*/
|
|
|
|
CodePExpr(rightop);
|
|
|
|
CodePExpr(leftop);
|
|
|
|
if (rightop->nd_type == emptyset_type)
|
|
|
|
C_and(rightop->nd_type->tp_size);
|
|
|
|
else
|
|
|
|
C_inn(rightop->nd_type->tp_size);
|
|
|
|
|
|
|
|
if (true_label != NO_LABEL )
|
|
|
|
C_zeq(true_label);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AND:
|
|
|
|
case OR:
|
|
|
|
Operands(leftop, rightop);
|
|
|
|
if (expr->nd_symb == AND)
|
|
|
|
C_and(tp->tp_size);
|
|
|
|
else
|
|
|
|
C_ior(tp->tp_size);
|
|
|
|
if (true_label != NO_LABEL )
|
|
|
|
C_zeq(true_label);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
crash("(CodeBoper Bad operator %s\n)", symbol2str(expr->nd_symb));
|
1988-10-26 15:21:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
|
|
|
|
static void CodeSet(register struct node *nd)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
register struct type *tp = nd->nd_type;
|
|
|
|
|
|
|
|
C_zer(tp->tp_size);
|
|
|
|
nd = nd->nd_right;
|
2019-02-23 16:44:50 +00:00
|
|
|
while (nd)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
assert(nd->nd_class == Link && nd->nd_symb == ',');
|
|
|
|
|
|
|
|
CodeEl(nd->nd_left, tp);
|
|
|
|
nd = nd->nd_right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
static void CodeEl(register struct node *nd, register struct type *tp)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
2019-02-23 16:44:50 +00:00
|
|
|
if (nd->nd_class == Link && nd->nd_symb == UPTO)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
Operands(nd->nd_left, nd->nd_right);
|
2019-02-23 16:44:50 +00:00
|
|
|
C_loc(tp->tp_size); /* push size */
|
|
|
|
C_cal("_bts"); /* library routine to fill set */
|
1988-10-26 15:21:11 +00:00
|
|
|
C_asp(3 * word_size);
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
else
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
CodePExpr(nd);
|
|
|
|
C_set(tp->tp_size);
|
|
|
|
C_ior(tp->tp_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
static struct type * CodeParameters(struct paramlist *param, struct node *arg)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
1989-05-03 10:30:22 +00:00
|
|
|
register struct type *tp, *left_tp, *last_tp = (struct type *) 0;
|
1988-10-26 15:21:11 +00:00
|
|
|
struct node *left;
|
|
|
|
struct desig ds;
|
|
|
|
|
|
|
|
assert(param && arg);
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (param->next)
|
1988-10-26 15:21:11 +00:00
|
|
|
last_tp = CodeParameters(param->next, arg->nd_right);
|
|
|
|
|
|
|
|
tp = TypeOfParam(param);
|
|
|
|
left = arg->nd_left;
|
|
|
|
left_tp = left->nd_type;
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (IsConformantArray(tp))
|
|
|
|
{
|
|
|
|
if (last_tp != tp)
|
1988-10-26 15:21:11 +00:00
|
|
|
/* push descriptors only once */
|
|
|
|
CodeConfDescr(tp, left_tp);
|
|
|
|
|
|
|
|
CodeDAddress(left);
|
|
|
|
return tp;
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
if (IsVarParam(param))
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
CodeDAddress(left);
|
|
|
|
return tp;
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
if (left_tp->tp_fund == T_STRINGCONST)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
CodePString(left, tp);
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
|
|
|
|
ds = InitDesig;
|
2019-02-23 16:44:50 +00:00
|
|
|
CodeExpr(left, &ds, NO_LABEL );
|
1988-10-26 15:21:11 +00:00
|
|
|
CodeValue(&ds, left_tp);
|
|
|
|
|
|
|
|
RangeCheck(tp, left_tp);
|
2019-02-23 16:44:50 +00:00
|
|
|
if (tp == real_type && BaseType(left_tp) == int_type)
|
1989-05-03 10:30:22 +00:00
|
|
|
Int2Real(int_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
static void CodeConfDescr(register struct type *ftp, register struct type *atp)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
struct type *elemtp = ftp->arr_elem;
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (IsConformantArray(elemtp))
|
1988-10-26 15:21:11 +00:00
|
|
|
CodeConfDescr(elemtp, atp->arr_elem);
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (atp->tp_fund == T_STRINGCONST)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
C_loc((arith) 1);
|
|
|
|
C_loc(atp->tp_psize - 1);
|
|
|
|
C_loc((arith) 1);
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
else if (IsConformantArray(atp))
|
|
|
|
{
|
|
|
|
if (atp->arr_sclevel < proclevel)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
C_lxa((arith) proclevel - atp->arr_sclevel);
|
|
|
|
C_adp(atp->arr_cfdescr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
C_lal(atp->arr_cfdescr);
|
|
|
|
|
|
|
|
C_loi(3 * word_size);
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
else
|
|
|
|
{ /* normal array */
|
1988-10-26 15:21:11 +00:00
|
|
|
assert(atp->tp_fund == T_ARRAY);
|
|
|
|
assert(!IsConformantArray(atp));
|
|
|
|
C_lae_dlb(atp->arr_ardescr, (arith) 0);
|
2019-02-23 16:44:50 +00:00
|
|
|
C_loi(3 * word_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
static void CodePString(struct node *nd, struct type *tp)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
/* no null padding */
|
|
|
|
C_lae_dlb(nd->nd_SLA, (arith) 0);
|
|
|
|
C_loi(tp->tp_size);
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
void CodeCall(register struct node *nd)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
/* Generate code for a procedure call. Checking of parameters
|
2019-02-23 16:44:50 +00:00
|
|
|
and result is already done.
|
|
|
|
*/
|
1988-10-26 15:21:11 +00:00
|
|
|
register struct node *left = nd->nd_left;
|
|
|
|
register struct node *right = nd->nd_right;
|
|
|
|
register struct def *df = left->nd_def;
|
|
|
|
register struct type *result_tp;
|
|
|
|
|
|
|
|
assert(IsProcCall(left));
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (left->nd_type == std_type)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
CodeStd(nd);
|
|
|
|
return;
|
2019-02-23 16:44:50 +00:00
|
|
|
}
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (right)
|
1988-10-26 15:21:11 +00:00
|
|
|
(void) CodeParameters(ParamList(left->nd_type), right);
|
|
|
|
|
|
|
|
assert(left->nd_class == Def);
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (df->df_kind & D_ROUTINE)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
int level = df->df_scope->sc_level;
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (level > 0 && !(df->df_flags & D_EXTERNAL))
|
|
|
|
C_lxl((arith)(proclevel - level));
|
1988-10-26 15:21:11 +00:00
|
|
|
C_cal(df->prc_name);
|
|
|
|
C_asp(left->nd_type->prc_nbpar);
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
else
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
label l1 = ++text_label;
|
|
|
|
label l2 = ++text_label;
|
|
|
|
|
|
|
|
assert(df->df_kind == D_VARIABLE);
|
|
|
|
|
|
|
|
/* Push value of procedure/function parameter */
|
|
|
|
CodePExpr(left);
|
|
|
|
|
|
|
|
/* Test if value is a global or local procedure/function */
|
|
|
|
C_exg(pointer_size);
|
|
|
|
C_dup(pointer_size);
|
|
|
|
C_zer(pointer_size);
|
|
|
|
C_cmp();
|
|
|
|
|
|
|
|
C_zeq(l1);
|
2019-02-23 16:44:50 +00:00
|
|
|
/* At this point, on top of the stack the LB */
|
1988-10-26 15:21:11 +00:00
|
|
|
C_exg(pointer_size);
|
2019-02-23 16:44:50 +00:00
|
|
|
/* Now, the name of the procedure/function */C_cai();
|
1988-10-26 15:21:11 +00:00
|
|
|
C_asp(pointer_size + left->nd_type->prc_nbpar);
|
|
|
|
C_bra(l2);
|
|
|
|
|
|
|
|
/* value is a global procedure/function */
|
|
|
|
C_df_ilb(l1);
|
2019-02-23 16:44:50 +00:00
|
|
|
C_asp(pointer_size); /* no LB needed */
|
1988-10-26 15:21:11 +00:00
|
|
|
C_cai();
|
|
|
|
C_asp(left->nd_type->prc_nbpar);
|
|
|
|
C_df_ilb(l2);
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if ( (result_tp = ResultType(left->nd_type)) )
|
1988-10-26 15:21:11 +00:00
|
|
|
C_lfr(result_tp->tp_size);
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
static void CodeStd(struct node *nd)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
register struct node *arg = nd->nd_right;
|
|
|
|
register struct node *left = arg->nd_left;
|
|
|
|
register struct type *tp = BaseType(left->nd_type);
|
|
|
|
int req = nd->nd_left->nd_def->df_value.df_reqname;
|
|
|
|
|
|
|
|
assert(arg->nd_class == Link && arg->nd_symb == ',');
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
switch (req)
|
|
|
|
{
|
|
|
|
case R_ABS:
|
|
|
|
CodePExpr(left);
|
|
|
|
if (tp == int_type)
|
|
|
|
C_cal("_abi");
|
|
|
|
else if (tp == long_type)
|
|
|
|
C_cal("_abl");
|
|
|
|
else
|
|
|
|
C_cal("_abr");
|
|
|
|
C_asp(tp->tp_size);
|
|
|
|
C_lfr(tp->tp_size);
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case R_SQR:
|
|
|
|
CodePExpr(left);
|
|
|
|
C_dup(tp->tp_size);
|
|
|
|
if (tp == int_type || tp == long_type)
|
|
|
|
C_mli(tp->tp_size);
|
|
|
|
else
|
|
|
|
C_mlf(real_size);
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case R_SIN:
|
|
|
|
case R_COS:
|
|
|
|
case R_EXP:
|
|
|
|
case R_LN:
|
|
|
|
case R_SQRT:
|
|
|
|
case R_ARCTAN:
|
|
|
|
assert(tp == real_type);
|
|
|
|
CodePExpr(left);
|
|
|
|
switch (req)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
case R_SIN:
|
2019-02-23 16:44:50 +00:00
|
|
|
C_cal("_sin");
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
case R_COS:
|
2019-02-23 16:44:50 +00:00
|
|
|
C_cal("_cos");
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
case R_EXP:
|
2019-02-23 16:44:50 +00:00
|
|
|
C_cal("_exp");
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
case R_LN:
|
2019-02-23 16:44:50 +00:00
|
|
|
C_cal("_log");
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
case R_SQRT:
|
2019-02-23 16:44:50 +00:00
|
|
|
C_cal("_sqt");
|
1988-10-26 15:21:11 +00:00
|
|
|
break;
|
2019-02-23 16:44:50 +00:00
|
|
|
case R_ARCTAN:
|
|
|
|
C_cal("_atn");
|
1988-10-26 15:21:11 +00:00
|
|
|
break;
|
2019-02-23 16:44:50 +00:00
|
|
|
default:
|
|
|
|
crash("(CodeStd)");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
C_asp(real_size);
|
|
|
|
C_lfr(real_size);
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case R_TRUNC:
|
|
|
|
assert(tp == real_type);
|
|
|
|
CodePExpr(left);
|
|
|
|
Real2Int();
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case R_ROUND:
|
|
|
|
assert(tp == real_type);
|
|
|
|
CodePExpr(left);
|
|
|
|
C_cal("_rnd");
|
|
|
|
C_asp(real_size);
|
|
|
|
C_lfr(real_size);
|
|
|
|
Real2Int();
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case R_ORD:
|
|
|
|
CodePExpr(left);
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case R_CHR:
|
|
|
|
CodePExpr(left);
|
|
|
|
genrck(char_type);
|
|
|
|
break;
|
1989-05-03 10:30:22 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case R_SUCC:
|
|
|
|
case R_PRED:
|
|
|
|
CodePExpr(left);
|
|
|
|
C_loc((arith) 1);
|
|
|
|
if (tp == long_type)
|
|
|
|
Int2Long();
|
1989-05-03 10:30:22 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (req == R_SUCC)
|
|
|
|
C_adi(tp->tp_size);
|
|
|
|
else
|
|
|
|
C_sbi(tp->tp_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (bounded(left->nd_type))
|
|
|
|
genrck(left->nd_type);
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case R_ODD:
|
|
|
|
CodePExpr(left);
|
|
|
|
C_loc((arith) 1);
|
|
|
|
if (tp == long_type)
|
|
|
|
Int2Long();
|
|
|
|
C_and(tp->tp_size);
|
|
|
|
if (tp == long_type)
|
|
|
|
Long2Int(); /* bool_size == int_size */
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case R_EOF:
|
|
|
|
case R_EOLN:
|
|
|
|
CodeDAddress(left);
|
|
|
|
if (req == R_EOF)
|
|
|
|
C_cal("_efl");
|
|
|
|
else
|
|
|
|
C_cal("_eln");
|
|
|
|
C_asp(pointer_size);
|
|
|
|
C_lfr(word_size);
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case R_REWRITE:
|
|
|
|
case R_RESET:
|
|
|
|
CodeDAddress(left);
|
|
|
|
if (tp == text_type)
|
|
|
|
C_loc((arith) 0);
|
|
|
|
else
|
|
|
|
C_loc(tp->next->tp_psize);
|
|
|
|
/* ??? elements of packed size ??? */
|
|
|
|
if (req == R_REWRITE)
|
|
|
|
C_cal("_cre");
|
|
|
|
else
|
|
|
|
C_cal("_opn");
|
|
|
|
C_asp(pointer_size + word_size);
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case R_PUT:
|
|
|
|
case R_GET:
|
|
|
|
CodeDAddress(left);
|
|
|
|
if (req == R_PUT)
|
|
|
|
C_cal("_put");
|
|
|
|
else
|
|
|
|
C_cal("_get");
|
|
|
|
C_asp(pointer_size);
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case R_PAGE:
|
|
|
|
CodeDAddress(left);
|
|
|
|
C_cal("_pag");
|
|
|
|
C_asp(pointer_size);
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case R_PACK:
|
|
|
|
{
|
|
|
|
label lba = tp->arr_ardescr;
|
1991-02-19 12:41:27 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
CodeDAddress(left);
|
|
|
|
arg = arg->nd_right;
|
|
|
|
left = arg->nd_left;
|
|
|
|
CodePExpr(left);
|
|
|
|
arg = arg->nd_right;
|
|
|
|
left = arg->nd_left;
|
|
|
|
CodeDAddress(left);
|
|
|
|
C_lae_dlb(left->nd_type->arr_ardescr, (arith) 0);
|
|
|
|
C_lae_dlb(lba, (arith) 0);
|
|
|
|
C_cal("_pac");
|
|
|
|
C_asp(4 * pointer_size + word_size);
|
|
|
|
break;
|
|
|
|
}
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case R_UNPACK:
|
|
|
|
{
|
|
|
|
/* change sequence of arguments of the library routine
|
|
|
|
_unp to merge code of R_PACK and R_UNPACK.
|
|
|
|
*/
|
|
|
|
label lba, lbz = tp->arr_ardescr;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
tp = tp->arr_elem;
|
|
|
|
if (tp->tp_fund == T_SUBRANGE && tp->sub_lb >= 0)
|
|
|
|
{
|
|
|
|
C_loc((arith) 1);
|
1988-10-26 15:21:11 +00:00
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
else
|
|
|
|
C_loc((arith) 0);
|
|
|
|
CodeDAddress(left);
|
|
|
|
arg = arg->nd_right;
|
|
|
|
left = arg->nd_left;
|
|
|
|
CodeDAddress(left);
|
|
|
|
lba = left->nd_type->arr_ardescr;
|
|
|
|
arg = arg->nd_right;
|
|
|
|
left = arg->nd_left;
|
|
|
|
CodePExpr(left);
|
|
|
|
C_lae_dlb(lbz, (arith) 0);
|
|
|
|
C_lae_dlb(lba, (arith) 0);
|
|
|
|
C_cal("_unp");
|
|
|
|
C_asp(4 * pointer_size + 2 * word_size);
|
|
|
|
break;
|
|
|
|
}
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case R_NEW:
|
|
|
|
case R_DISPOSE:
|
|
|
|
CodeDAddress(left);
|
|
|
|
C_loc(PointedtoType(tp)->tp_size);
|
|
|
|
if (req == R_NEW)
|
|
|
|
C_cal("_new");
|
|
|
|
else
|
|
|
|
C_cal("_dis");
|
|
|
|
C_asp(pointer_size + word_size);
|
|
|
|
break;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
case R_HALT:
|
|
|
|
if (left)
|
|
|
|
CodePExpr(left);
|
|
|
|
else
|
|
|
|
C_zer(int_size);
|
|
|
|
C_cal("_hlt"); /* can't return */
|
|
|
|
C_asp(int_size); /* help the optimizer(s) */
|
|
|
|
break;
|
1989-05-03 10:30:22 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
default:
|
|
|
|
crash("(CodeStd)");
|
|
|
|
/*NOTREACHED*/
|
1988-10-26 15:21:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
void Long2Int(void)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
1989-05-03 10:30:22 +00:00
|
|
|
/* convert a long to integer */
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (int_size == long_size)
|
|
|
|
return;
|
1989-05-03 10:30:22 +00:00
|
|
|
|
|
|
|
C_loc(long_size);
|
1988-10-26 15:21:11 +00:00
|
|
|
C_loc(int_size);
|
1989-05-03 10:30:22 +00:00
|
|
|
C_cii();
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
void Int2Long(void)
|
1989-05-03 10:30:22 +00:00
|
|
|
{
|
|
|
|
/* convert integer to long */
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (int_size == long_size)
|
|
|
|
return;
|
1989-05-03 10:30:22 +00:00
|
|
|
C_loc(int_size);
|
|
|
|
C_loc(long_size);
|
|
|
|
C_cii();
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
void Int2Real(arith size)
|
|
|
|
/* size is different for integers and longs */
|
1989-05-03 10:30:22 +00:00
|
|
|
{
|
|
|
|
/* convert integer to real */
|
|
|
|
C_loc(size);
|
1988-10-26 15:21:11 +00:00
|
|
|
C_loc(real_size);
|
|
|
|
C_cif();
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
void Real2Int(void)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
/* convert real to integer */
|
|
|
|
C_loc(real_size);
|
|
|
|
C_loc(int_size);
|
|
|
|
C_cfi();
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
void RangeCheck(register struct type *tpl, register struct type *tpr)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
/* Generate a range check if neccessary
|
2019-02-23 16:44:50 +00:00
|
|
|
*/
|
1988-10-26 15:21:11 +00:00
|
|
|
|
|
|
|
arith llo, lhi, rlo, rhi;
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (bounded(tpl))
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
/* in this case we might need a range check */
|
2019-02-23 16:44:50 +00:00
|
|
|
if (!bounded(tpr))
|
1988-10-26 15:21:11 +00:00
|
|
|
/* yes, we need one */
|
|
|
|
genrck(tpl);
|
2019-02-23 16:44:50 +00:00
|
|
|
else
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
/* both types are restricted. check the bounds to see
|
2019-02-23 16:44:50 +00:00
|
|
|
whether we need a range check. We don't need one
|
|
|
|
if the range of values of the right hand side is a
|
|
|
|
subset of the range of values of the left hand side.
|
|
|
|
*/
|
1988-10-26 15:21:11 +00:00
|
|
|
getbounds(tpl, &llo, &lhi);
|
|
|
|
getbounds(tpr, &rlo, &rhi);
|
2019-02-23 16:44:50 +00:00
|
|
|
if (llo > rlo || lhi < rhi)
|
1988-10-26 15:21:11 +00:00
|
|
|
genrck(tpl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
static void genrck(register struct type *tp)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
/* Generate a range check descriptor for type "tp" when
|
2019-02-23 16:44:50 +00:00
|
|
|
necessary. Return its label.
|
|
|
|
*/
|
1988-10-26 15:21:11 +00:00
|
|
|
|
|
|
|
arith lb, ub;
|
|
|
|
register label o1;
|
|
|
|
int newlabel = 0;
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (options['R'])
|
|
|
|
return;
|
1988-10-26 15:21:11 +00:00
|
|
|
|
|
|
|
getbounds(tp, &lb, &ub);
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
if (tp->tp_fund == T_SUBRANGE)
|
|
|
|
{
|
|
|
|
if (!(o1 = tp->sub_rck))
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
tp->sub_rck = o1 = ++data_label;
|
|
|
|
newlabel = 1;
|
|
|
|
}
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
else if (!(o1 = tp->enm_rck))
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
tp->enm_rck = o1 = ++data_label;
|
|
|
|
newlabel = 1;
|
|
|
|
}
|
2019-02-23 16:44:50 +00:00
|
|
|
if (newlabel)
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
C_df_dlb(o1);
|
|
|
|
C_rom_cst(lb);
|
|
|
|
C_rom_cst(ub);
|
|
|
|
}
|
|
|
|
C_lae_dlb(o1, (arith) 0);
|
|
|
|
C_rck(word_size);
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
void CodePExpr(register struct node *nd)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
/* Generate code to push the value of the expression "nd"
|
2019-02-23 16:44:50 +00:00
|
|
|
on the stack.
|
|
|
|
*/
|
1988-10-26 15:21:11 +00:00
|
|
|
|
|
|
|
struct desig designator;
|
|
|
|
struct type *tp = BaseType(nd->nd_type);
|
2019-02-23 16:44:50 +00:00
|
|
|
|
1988-10-26 15:21:11 +00:00
|
|
|
designator = InitDesig;
|
2019-02-23 16:44:50 +00:00
|
|
|
CodeExpr(nd, &designator, NO_LABEL );
|
|
|
|
if (tp->tp_fund & (T_ARRAY | T_RECORD))
|
1988-10-26 15:21:11 +00:00
|
|
|
CodeAddress(&designator);
|
|
|
|
else
|
|
|
|
CodeValue(&designator, nd->nd_type);
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
void CodeDAddress(struct node *nd)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
/* Generate code to push the address of the designator "nd"
|
2019-02-23 16:44:50 +00:00
|
|
|
on the stack.
|
|
|
|
*/
|
1988-10-26 15:21:11 +00:00
|
|
|
|
|
|
|
struct desig designator;
|
2019-02-23 16:44:50 +00:00
|
|
|
|
1988-10-26 15:21:11 +00:00
|
|
|
designator = InitDesig;
|
|
|
|
CodeDesig(nd, &designator);
|
|
|
|
CodeAddress(&designator);
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
void CodeDStore(register struct node *nd)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
/* Generate code to store the expression on the stack
|
2019-02-23 16:44:50 +00:00
|
|
|
into the designator "nd".
|
|
|
|
*/
|
1988-10-26 15:21:11 +00:00
|
|
|
|
|
|
|
struct desig designator;
|
2019-02-23 16:44:50 +00:00
|
|
|
|
1988-10-26 15:21:11 +00:00
|
|
|
designator = InitDesig;
|
|
|
|
CodeDesig(nd, &designator);
|
|
|
|
CodeStore(&designator, nd->nd_type);
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
static void RegisterMessages(register struct def *df)
|
1988-10-26 15:21:11 +00:00
|
|
|
{
|
|
|
|
register struct type *tp;
|
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
for (; df; df = df->df_nextinscope)
|
|
|
|
{
|
|
|
|
if (df->df_kind == D_VARIABLE && !(df->df_flags & D_NOREG))
|
|
|
|
{
|
1988-10-26 15:21:11 +00:00
|
|
|
/* Examine type and size
|
2019-02-23 16:44:50 +00:00
|
|
|
*/
|
1988-10-26 15:21:11 +00:00
|
|
|
tp = BaseType(df->df_type);
|
2019-02-23 16:44:50 +00:00
|
|
|
if (df->df_flags & D_VARPAR || tp->tp_fund & T_POINTER)
|
|
|
|
C_ms_reg(df->var_off, pointer_size, reg_pointer, 0);
|
1988-10-26 15:21:11 +00:00
|
|
|
|
2019-02-23 16:44:50 +00:00
|
|
|
else if (df->df_flags & D_LOOPVAR)
|
|
|
|
C_ms_reg(df->var_off, tp->tp_size, reg_loop, 2);
|
|
|
|
else if (tp->tp_fund & T_NUMERIC)
|
1988-10-26 15:21:11 +00:00
|
|
|
C_ms_reg(df->var_off, tp->tp_size,
|
2019-02-23 16:44:50 +00:00
|
|
|
tp->tp_fund == T_REAL ? reg_float : reg_any, 0);
|
1988-10-26 15:21:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|