ack/lang/m2/comp/code.c

1028 lines
19 KiB
C
Raw Normal View History

1986-05-21 18:32:20 +00:00
/* C O D E G E N E R A T I O N R O U T I N E S */
/* Code generation for expressions and coercions
*/
#include "debug.h"
#include <em_arith.h>
#include <em_label.h>
1986-07-08 14:59:02 +00:00
#include <em_code.h>
1986-05-21 18:32:20 +00:00
#include <assert.h>
#include "type.h"
#include "def.h"
#include "scope.h"
#include "desig.h"
#include "LLlex.h"
#include "node.h"
#include "Lpars.h"
1986-05-30 18:48:00 +00:00
#include "standards.h"
1986-06-20 14:36:49 +00:00
#include "walk.h"
1986-05-21 18:32:20 +00:00
extern char *long2str();
extern char *symbol2str();
extern int proclevel;
1986-06-06 02:22:09 +00:00
int fp_used;
1986-05-21 18:32:20 +00:00
CodeConst(cst, size)
arith cst, size;
{
/* Generate code to push constant "cst" with size "size"
*/
if (size <= word_size) {
C_loc(cst);
}
else if (size == dword_size) {
C_ldc(cst);
}
else {
1986-10-06 20:36:30 +00:00
crash("(CodeConst)");
/*
label dlab = ++data_label;
C_df_dlb(dlab);
1986-06-06 02:22:09 +00:00
C_rom_icon(long2str((long) cst), size);
1986-05-23 09:46:31 +00:00
C_lae_dlb(dlab, (arith) 0);
1986-05-21 18:32:20 +00:00
C_loi(size);
1986-10-06 20:36:30 +00:00
*/
1986-05-21 18:32:20 +00:00
}
}
CodeString(nd)
1986-06-04 09:01:48 +00:00
register struct node *nd;
1986-05-21 18:32:20 +00:00
{
1986-09-25 19:39:06 +00:00
if (nd->nd_type->tp_fund != T_STRING) {
1986-05-23 09:46:31 +00:00
C_loc(nd->nd_INT);
}
1986-06-04 09:01:48 +00:00
else {
1986-10-06 20:36:30 +00:00
label lab = ++data_label;
C_df_dlb(lab);
1986-06-10 13:18:52 +00:00
C_rom_scon(nd->nd_STR, WA(nd->nd_SLE + 1));
1986-06-04 09:01:48 +00:00
C_lae_dlb(lab, (arith) 0);
}
}
CodePadString(nd, sz)
register struct node *nd;
arith sz;
{
/* Generate code to push the string indicated by "nd".
Make it null-padded to "sz" bytes
*/
1986-06-10 13:18:52 +00:00
register arith sizearg = WA(nd->nd_type->tp_size);
1986-06-04 09:01:48 +00:00
assert(nd->nd_type->tp_fund == T_STRING);
if (sizearg != sz) {
/* null padding required */
assert(sizearg < sz);
C_zer(sz - sizearg);
}
CodeString(nd); /* push address of string */
1986-06-06 02:22:09 +00:00
C_loi(sizearg);
1986-05-21 18:32:20 +00:00
}
CodeExpr(nd, ds, true_label, false_label)
1986-05-23 19:25:21 +00:00
register struct node *nd;
register struct desig *ds;
1986-05-21 18:32:20 +00:00
label true_label, false_label;
{
1986-06-06 02:22:09 +00:00
register struct type *tp = nd->nd_type;
1986-05-21 18:32:20 +00:00
1986-06-06 02:22:09 +00:00
if (tp->tp_fund == T_REAL) fp_used = 1;
1986-05-21 18:32:20 +00:00
switch(nd->nd_class) {
case Def:
1986-05-30 18:48:00 +00:00
if (nd->nd_def->df_kind == D_PROCEDURE) {
1986-06-04 09:01:48 +00:00
C_lpi(NameOfProc(nd->nd_def));
1986-05-30 18:48:00 +00:00
ds->dsg_kind = DSG_LOADED;
break;
}
1986-06-04 09:01:48 +00:00
/* Fall through */
case Link:
1986-06-20 14:36:49 +00:00
case LinkDef:
1986-06-10 13:18:52 +00:00
case Arrsel:
case Arrow:
1986-05-21 18:32:20 +00:00
CodeDesig(nd, ds);
break;
case Oper:
CodeOper(nd, true_label, false_label);
if (true_label == 0) ds->dsg_kind = DSG_LOADED;
1986-06-10 13:18:52 +00:00
else {
ds->dsg_kind = DSG_INIT;
true_label = 0;
}
1986-05-21 18:32:20 +00:00
break;
case Uoper:
CodeUoper(nd);
ds->dsg_kind = DSG_LOADED;
break;
case Value:
switch(nd->nd_symb) {
1986-10-06 20:36:30 +00:00
case REAL: {
label lab = ++data_label;
C_df_dlb(lab);
C_rom_fcon(nd->nd_REL, nd->nd_type->tp_size);
C_lae_dlb(lab, (arith) 0);
C_loi(nd->nd_type->tp_size);
}
1986-05-21 18:32:20 +00:00
break;
case STRING:
CodeString(nd);
break;
case INTEGER:
1986-06-06 02:22:09 +00:00
CodeConst(nd->nd_INT, tp->tp_size);
1986-05-21 18:32:20 +00:00
break;
default:
crash("Value error");
}
ds->dsg_kind = DSG_LOADED;
break;
case Call:
CodeCall(nd);
ds->dsg_kind = DSG_LOADED;
break;
1986-05-23 19:25:21 +00:00
case Set: {
1986-10-06 20:36:30 +00:00
register arith *st = nd->nd_set;
register int i;
1986-05-23 19:25:21 +00:00
st = nd->nd_set;
1986-05-28 18:36:51 +00:00
ds->dsg_kind = DSG_LOADED;
if (!st) {
1986-06-06 02:22:09 +00:00
C_zer(tp->tp_size);
1986-05-28 18:36:51 +00:00
break;
}
1986-06-06 02:22:09 +00:00
for (i = tp->tp_size / word_size, st += i; i > 0; i--) {
1986-05-23 19:25:21 +00:00
C_loc(*--st);
}
}
break;
1986-05-21 18:32:20 +00:00
case Xset:
1986-05-23 19:25:21 +00:00
CodeSet(nd);
1986-05-21 18:32:20 +00:00
ds->dsg_kind = DSG_LOADED;
break;
default:
crash("(CodeExpr) bad node type");
}
if (true_label != 0) {
1986-10-06 20:36:30 +00:00
/* Only for boolean expressions
*/
1986-06-06 02:22:09 +00:00
CodeValue(ds, tp->tp_size);
1986-05-21 18:32:20 +00:00
*ds = InitDesig;
C_zne(true_label);
C_bra(false_label);
}
}
CodeCoercion(t1, t2)
1986-05-28 18:36:51 +00:00
register struct type *t1, *t2;
1986-05-21 18:32:20 +00:00
{
1986-06-04 09:01:48 +00:00
register int fund1, fund2;
1986-05-28 18:36:51 +00:00
1986-06-26 09:39:36 +00:00
t1 = BaseType(t1);
t2 = BaseType(t2);
1986-06-17 12:04:05 +00:00
if (t1 == t2) return;
1986-05-28 18:36:51 +00:00
if ((fund1 = t1->tp_fund) == T_WORD) fund1 = T_INTEGER;
if ((fund2 = t2->tp_fund) == T_WORD) fund2 = T_INTEGER;
switch(fund1) {
case T_INTEGER:
switch(fund2) {
case T_INTEGER:
if (t2->tp_size != t1->tp_size) {
C_loc(t1->tp_size);
C_loc(t2->tp_size);
C_cii();
}
break;
case T_ENUMERATION:
case T_CHAR:
case T_CARDINAL:
if (t1->tp_size != word_size) {
C_loc(t1->tp_size);
C_loc(word_size);
C_ciu();
}
break;
case T_REAL:
C_loc(t1->tp_size);
C_loc(t2->tp_size);
C_cif();
break;
default:
crash("Funny integer conversion");
}
break;
case T_CHAR:
case T_ENUMERATION:
case T_CARDINAL:
1986-08-26 14:33:24 +00:00
case T_INTORCARD:
1986-05-28 18:36:51 +00:00
switch(fund2) {
case T_ENUMERATION:
case T_CHAR:
case T_CARDINAL:
case T_POINTER:
1986-09-25 19:39:06 +00:00
case T_EQUAL:
1986-08-26 14:33:24 +00:00
case T_INTORCARD:
1986-05-28 18:36:51 +00:00
if (t2->tp_size > word_size) {
C_loc(word_size);
C_loc(t2->tp_size);
C_cuu();
}
break;
case T_INTEGER:
1986-06-06 02:22:09 +00:00
C_loc(word_size);
1986-05-28 18:36:51 +00:00
C_loc(t2->tp_size);
C_cui();
break;
case T_REAL:
1986-06-06 02:22:09 +00:00
C_loc(word_size);
1986-05-28 18:36:51 +00:00
C_loc(t2->tp_size);
C_cuf();
break;
default:
crash("Funny cardinal conversion");
}
break;
case T_REAL:
switch(fund2) {
case T_REAL:
if (t2->tp_size != t1->tp_size) {
C_loc(t1->tp_size);
C_loc(t2->tp_size);
C_cff();
}
break;
case T_INTEGER:
C_loc(t1->tp_size);
C_loc(t2->tp_size);
C_cfi();
break;
case T_CARDINAL:
C_loc(t1->tp_size);
C_loc(t2->tp_size);
C_cfu();
break;
default:
crash("Funny REAL conversion");
}
break;
}
1986-05-21 18:32:20 +00:00
}
CodeCall(nd)
1986-05-23 19:25:21 +00:00
register struct node *nd;
1986-05-21 18:32:20 +00:00
{
/* Generate code for a procedure call. Checking of parameters
and result is already done.
*/
register struct node *left = nd->nd_left;
1986-10-06 20:36:30 +00:00
register struct node *right = nd->nd_right;
1986-06-20 14:36:49 +00:00
register struct type *result_tp;
1986-05-21 18:32:20 +00:00
if (left->nd_type == std_type) {
CodeStd(nd);
return;
}
1986-05-30 18:48:00 +00:00
if (IsCast(left)) {
1986-05-23 09:46:31 +00:00
/* it was just a cast. Simply ignore it
*/
1986-10-06 20:36:30 +00:00
CodePExpr(right->nd_left);
*nd = *(right->nd_left);
1986-05-23 09:46:31 +00:00
nd->nd_type = left->nd_def->df_type;
return;
}
1986-05-30 18:48:00 +00:00
assert(IsProcCall(left));
1986-05-21 18:32:20 +00:00
1986-10-06 20:36:30 +00:00
if (right) {
CodeParameters(ParamList(left->nd_type), right);
1986-05-21 18:32:20 +00:00
}
1986-08-26 14:33:24 +00:00
switch(left->nd_class) {
case Def: {
register struct def *df = left->nd_def;
if (df->df_kind == D_PROCEDURE) {
arith level = df->df_scope->sc_level;
if (level > 0) {
C_lxl((arith) proclevel - level);
}
C_cal(NameOfProc(df));
break;
1986-05-21 18:32:20 +00:00
}
1986-08-26 14:33:24 +00:00
else if (df->df_kind == D_PROCHEAD) {
C_cal(df->for_name);
break;
}}
/* Fall through */
default:
1986-05-30 18:48:00 +00:00
CodePExpr(left);
1986-05-21 18:32:20 +00:00
C_cai();
}
1986-06-10 13:18:52 +00:00
if (left->nd_type->prc_nbpar) C_asp(left->nd_type->prc_nbpar);
1986-06-20 14:36:49 +00:00
if (result_tp = ResultType(left->nd_type)) {
if (IsConstructed(result_tp)) {
C_lfr(pointer_size);
C_loi(result_tp->tp_size);
}
else C_lfr(WA(result_tp->tp_size));
1986-05-21 18:32:20 +00:00
}
}
1986-06-17 12:04:05 +00:00
CodeParameters(param, arg)
struct paramlist *param;
struct node *arg;
{
register struct type *tp;
register struct node *left;
1986-08-26 14:33:24 +00:00
register struct type *left_type;
1986-09-25 19:39:06 +00:00
1986-06-17 12:04:05 +00:00
assert(param != 0 && arg != 0);
if (param->next) {
CodeParameters(param->next, arg->nd_right);
}
tp = TypeOfParam(param);
left = arg->nd_left;
1986-08-26 14:33:24 +00:00
left_type = left->nd_type;
1986-06-17 12:04:05 +00:00
if (IsConformantArray(tp)) {
C_loc(tp->arr_elsize);
1986-08-26 14:33:24 +00:00
if (IsConformantArray(left_type)) {
1986-06-17 12:04:05 +00:00
DoHIGH(left);
1986-08-26 14:33:24 +00:00
if (tp->arr_elem->tp_size !=
left_type->arr_elem->tp_size) {
1986-06-17 12:04:05 +00:00
/* This can only happen if the formal type is
ARRAY OF WORD
*/
1986-08-26 14:33:24 +00:00
assert(tp->arr_elem == word_type);
C_loc(left_type->arr_elem->tp_size);
C_cal("_wa");
C_asp(dword_size);
C_lfr(word_size);
1986-06-17 12:04:05 +00:00
}
}
else if (left->nd_symb == STRING) {
1986-10-22 15:38:24 +00:00
C_loc(left->nd_SLE - 1);
1986-06-17 12:04:05 +00:00
}
else if (tp->arr_elem == word_type) {
1986-08-26 14:33:24 +00:00
C_loc((left_type->tp_size+word_size-1) / word_size - 1);
1986-06-17 12:04:05 +00:00
}
else {
1986-10-06 20:36:30 +00:00
arith lb, ub;
getbounds(IndexType(left_type), &lb, &ub);
C_loc(ub - lb);
1986-06-17 12:04:05 +00:00
}
C_loc((arith) 0);
if (left->nd_symb == STRING) {
CodeString(left);
}
else CodeDAddress(left);
}
else if (IsVarParam(param)) {
CodeDAddress(left);
}
else {
1986-08-26 14:33:24 +00:00
if (left_type->tp_fund == T_STRING) {
1986-06-17 12:04:05 +00:00
CodePadString(left, tp->tp_size);
}
else CodePExpr(left);
1986-09-25 19:39:06 +00:00
RangeCheck(left_type, tp);
1986-06-17 12:04:05 +00:00
}
}
1986-05-21 18:32:20 +00:00
CodeStd(nd)
struct node *nd;
{
1986-05-30 18:48:00 +00:00
register struct node *arg = nd->nd_right;
register struct node *left = 0;
register struct type *tp = 0;
1986-10-06 20:36:30 +00:00
int std = nd->nd_left->nd_def->df_value.df_stdname;
1986-05-30 18:48:00 +00:00
if (arg) {
left = arg->nd_left;
1986-06-26 09:39:36 +00:00
tp = BaseType(left->nd_type);
1986-05-30 18:48:00 +00:00
arg = arg->nd_right;
}
1986-10-06 20:36:30 +00:00
switch(std) {
1986-05-30 18:48:00 +00:00
case S_ABS:
CodePExpr(left);
if (tp->tp_fund == T_INTEGER) {
if (tp->tp_size == int_size) {
C_cal("_absi");
}
else C_cal("_absl");
}
else if (tp->tp_fund == T_REAL) {
if (tp->tp_size == float_size) {
C_cal("_absf");
}
else C_cal("_absd");
}
1986-08-26 14:33:24 +00:00
C_asp(tp->tp_size);
1986-05-30 18:48:00 +00:00
C_lfr(tp->tp_size);
break;
case S_CAP:
CodePExpr(left);
1986-10-06 20:36:30 +00:00
C_loc((arith) 0137); /* ASCII assumed */
1986-05-30 18:48:00 +00:00
C_and(word_size);
break;
case S_CHR:
CodePExpr(left);
1986-09-25 19:39:06 +00:00
RangeCheck(char_type, tp);
1986-05-30 18:48:00 +00:00
break;
case S_FLOAT:
CodePExpr(left);
CodeCoercion(tp, real_type);
break;
case S_HIGH:
assert(IsConformantArray(tp));
1986-06-04 09:01:48 +00:00
DoHIGH(left);
1986-05-30 18:48:00 +00:00
break;
case S_ODD:
1986-08-26 14:33:24 +00:00
CodePExpr(left);
1986-05-30 18:48:00 +00:00
if (tp->tp_size == word_size) {
C_loc((arith) 1);
C_and(word_size);
}
else {
assert(tp->tp_size == dword_size);
C_ldc((arith) 1);
C_and(dword_size);
C_ior(word_size);
}
break;
case S_ORD:
CodePExpr(left);
break;
case S_TRUNC:
CodePExpr(left);
CodeCoercion(tp, card_type);
break;
case S_VAL:
CodePExpr(left);
1986-09-25 19:39:06 +00:00
RangeCheck(nd->nd_type, tp);
1986-05-30 18:48:00 +00:00
break;
case S_ADR:
CodeDAddress(left);
break;
case S_DEC:
1986-10-06 20:36:30 +00:00
case S_INC: {
register arith size = tp->tp_size;
if (size < word_size) size = word_size;
1986-05-30 18:48:00 +00:00
CodePExpr(left);
if (arg) CodePExpr(arg->nd_left);
else C_loc((arith) 1);
1986-10-06 20:36:30 +00:00
if (std == S_DEC) {
if (tp->tp_fund == T_INTEGER) C_sbi(size);
else C_sbu(size);
1986-05-30 18:48:00 +00:00
}
else {
1986-10-06 20:36:30 +00:00
if (tp->tp_fund == T_INTEGER) C_adi(size);
else C_adu(size);
1986-05-30 18:48:00 +00:00
}
1986-10-06 20:36:30 +00:00
if (size == word_size) RangeCheck(tp, int_type);
1986-05-30 18:48:00 +00:00
CodeDStore(left);
break;
1986-10-06 20:36:30 +00:00
}
1986-05-30 18:48:00 +00:00
case S_HALT:
C_cal("_halt");
break;
case S_INCL:
case S_EXCL:
CodePExpr(left);
CodePExpr(arg->nd_left);
C_set(tp->tp_size);
if (std == S_INCL) {
C_ior(tp->tp_size);
}
else {
C_com(tp->tp_size);
C_and(tp->tp_size);
}
CodeDStore(left);
break;
default:
crash("(CodeStd)");
}
1986-05-21 18:32:20 +00:00
}
1986-05-28 18:36:51 +00:00
CodeAssign(nd, dss, dst)
1986-10-06 20:36:30 +00:00
register struct node *nd;
1986-05-23 09:46:31 +00:00
struct desig *dst, *dss;
1986-05-21 18:32:20 +00:00
{
/* Generate code for an assignment. Testing of type
compatibility and the like is already done.
*/
1986-06-04 09:01:48 +00:00
register struct type *tp = nd->nd_right->nd_type;
1986-10-06 20:36:30 +00:00
arith size = nd->nd_left->nd_type->tp_size;
1986-05-28 18:36:51 +00:00
if (dss->dsg_kind == DSG_LOADED) {
1986-06-04 09:01:48 +00:00
if (tp->tp_fund == T_STRING) {
CodeAddress(dst);
C_loc(tp->tp_size);
1986-10-06 20:36:30 +00:00
C_loc(size);
1986-06-04 09:01:48 +00:00
C_cal("_StringAssign");
C_asp((int_size << 1) + (pointer_size << 1));
return;
}
1986-10-06 20:36:30 +00:00
CodeStore(dst, size);
1986-06-04 09:01:48 +00:00
return;
1986-05-28 18:36:51 +00:00
}
1986-06-04 09:01:48 +00:00
CodeAddress(dss);
CodeAddress(dst);
1986-10-06 20:36:30 +00:00
C_blm(size);
1986-05-28 18:36:51 +00:00
}
1986-09-25 19:39:06 +00:00
RangeCheck(tpl, tpr)
1986-05-28 18:36:51 +00:00
register struct type *tpl, *tpr;
{
/* Generate a range check if neccessary
*/
arith llo, lhi, rlo, rhi;
if (bounded(tpl)) {
/* in this case we might need a range check */
if (!bounded(tpr)) {
/* yes, we need one */
1986-06-17 12:04:05 +00:00
genrck(tpl);
1986-05-28 18:36:51 +00:00
}
else {
/* both types are restricted. check the bounds
1986-10-06 20:36:30 +00:00
to see wether 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.
1986-05-28 18:36:51 +00:00
*/
getbounds(tpl, &llo, &lhi);
getbounds(tpr, &rlo, &rhi);
if (llo > rlo || lhi < rhi) {
1986-06-17 12:04:05 +00:00
genrck(tpl);
1986-05-28 18:36:51 +00:00
}
}
}
1986-05-21 18:32:20 +00:00
}
1986-08-26 14:33:24 +00:00
Operands(leftop, rightop, tp)
1986-05-23 19:25:21 +00:00
register struct node *leftop, *rightop;
1986-08-26 14:33:24 +00:00
struct type *tp;
1986-05-21 18:32:20 +00:00
{
1986-05-30 18:48:00 +00:00
CodePExpr(leftop);
1986-08-26 14:33:24 +00:00
CodeCoercion(leftop->nd_type, tp);
1986-05-30 18:48:00 +00:00
CodePExpr(rightop);
1986-08-26 14:33:24 +00:00
CodeCoercion(rightop->nd_type, tp);
1986-05-21 18:32:20 +00:00
}
CodeOper(expr, true_label, false_label)
1986-08-26 14:33:24 +00:00
register struct node *expr; /* the expression tree itself */
1986-05-21 18:32:20 +00:00
label true_label;
label false_label; /* labels to jump to in logical expr's */
{
register struct node *leftop = expr->nd_left;
register struct node *rightop = expr->nd_right;
register struct type *tp = expr->nd_type;
1986-08-26 14:33:24 +00:00
switch (expr->nd_symb) {
1986-05-21 18:32:20 +00:00
case '+':
1986-08-26 14:33:24 +00:00
Operands(leftop, rightop, tp);
1986-05-21 18:32:20 +00:00
switch (tp->tp_fund) {
case T_INTEGER:
C_adi(tp->tp_size);
break;
case T_REAL:
C_adf(tp->tp_size);
break;
1986-08-26 14:33:24 +00:00
case T_POINTER:
1986-09-25 19:39:06 +00:00
case T_EQUAL:
1986-05-21 18:32:20 +00:00
case T_CARDINAL:
1986-08-26 14:33:24 +00:00
case T_INTORCARD:
1986-05-21 18:32:20 +00:00
C_adu(tp->tp_size);
break;
case T_SET:
C_ior(tp->tp_size);
break;
default:
crash("bad type +");
}
break;
case '-':
1986-08-26 14:33:24 +00:00
Operands(leftop, rightop, tp);
1986-05-21 18:32:20 +00:00
switch (tp->tp_fund) {
case T_INTEGER:
C_sbi(tp->tp_size);
break;
case T_REAL:
C_sbf(tp->tp_size);
break;
1986-08-26 14:33:24 +00:00
case T_POINTER:
1986-09-25 19:39:06 +00:00
case T_EQUAL:
1986-05-21 18:32:20 +00:00
case T_CARDINAL:
1986-08-26 14:33:24 +00:00
case T_INTORCARD:
1986-05-21 18:32:20 +00:00
C_sbu(tp->tp_size);
break;
case T_SET:
C_com(tp->tp_size);
C_and(tp->tp_size);
break;
default:
crash("bad type -");
}
break;
case '*':
1986-08-26 14:33:24 +00:00
Operands(leftop, rightop, tp);
1986-05-21 18:32:20 +00:00
switch (tp->tp_fund) {
case T_INTEGER:
C_mli(tp->tp_size);
break;
case T_POINTER:
1986-09-25 19:39:06 +00:00
case T_EQUAL:
1986-05-21 18:32:20 +00:00
case T_CARDINAL:
1986-08-26 14:33:24 +00:00
case T_INTORCARD:
1986-05-21 18:32:20 +00:00
C_mlu(tp->tp_size);
break;
case T_REAL:
C_mlf(tp->tp_size);
break;
case T_SET:
C_and(tp->tp_size);
break;
default:
crash("bad type *");
}
break;
case '/':
1986-08-26 14:33:24 +00:00
Operands(leftop, rightop, tp);
1986-05-21 18:32:20 +00:00
switch (tp->tp_fund) {
case T_REAL:
C_dvf(tp->tp_size);
break;
case T_SET:
C_xor(tp->tp_size);
break;
default:
crash("bad type /");
}
break;
case DIV:
1986-08-26 14:33:24 +00:00
Operands(leftop, rightop, tp);
1986-05-21 18:32:20 +00:00
switch(tp->tp_fund) {
case T_INTEGER:
C_dvi(tp->tp_size);
break;
case T_POINTER:
1986-09-25 19:39:06 +00:00
case T_EQUAL:
1986-05-21 18:32:20 +00:00
case T_CARDINAL:
1986-08-26 14:33:24 +00:00
case T_INTORCARD:
1986-05-21 18:32:20 +00:00
C_dvu(tp->tp_size);
break;
default:
crash("bad type DIV");
}
break;
case MOD:
1986-08-26 14:33:24 +00:00
Operands(leftop, rightop, tp);
1986-05-21 18:32:20 +00:00
switch(tp->tp_fund) {
case T_INTEGER:
C_rmi(tp->tp_size);
break;
case T_POINTER:
1986-09-25 19:39:06 +00:00
case T_EQUAL:
1986-05-21 18:32:20 +00:00
case T_CARDINAL:
1986-08-26 14:33:24 +00:00
case T_INTORCARD:
1986-05-21 18:32:20 +00:00
C_rmu(tp->tp_size);
break;
default:
crash("bad type MOD");
}
break;
case '<':
case LESSEQUAL:
case '>':
case GREATEREQUAL:
case '=':
case '#':
1986-08-26 14:33:24 +00:00
tp = BaseType(leftop->nd_type);
if (tp == intorcard_type) tp = BaseType(rightop->nd_type);
Operands(leftop, rightop, tp);
1986-05-21 18:32:20 +00:00
switch (tp->tp_fund) {
case T_INTEGER:
1986-05-28 18:36:51 +00:00
C_cmi(tp->tp_size);
1986-05-21 18:32:20 +00:00
break;
case T_POINTER:
1986-09-25 19:39:06 +00:00
case T_EQUAL:
case T_HIDDEN:
1986-05-21 18:32:20 +00:00
case T_CARDINAL:
1986-08-26 14:33:24 +00:00
case T_INTORCARD:
1986-05-28 18:36:51 +00:00
C_cmu(tp->tp_size);
1986-05-21 18:32:20 +00:00
break;
case T_ENUMERATION:
case T_CHAR:
C_cmu(word_size);
break;
case T_REAL:
1986-05-28 18:36:51 +00:00
C_cmf(tp->tp_size);
1986-05-21 18:32:20 +00:00
break;
case T_SET:
1986-08-26 14:33:24 +00:00
if (expr->nd_symb == GREATEREQUAL) {
1986-05-28 18:36:51 +00:00
/* A >= B is the same as A equals A + B
*/
C_dup(2*tp->tp_size);
C_asp(tp->tp_size);
1986-08-26 14:33:24 +00:00
C_ior(tp->tp_size);
1986-05-28 18:36:51 +00:00
}
1986-08-26 14:33:24 +00:00
else if (expr->nd_symb == LESSEQUAL) {
1986-05-28 18:36:51 +00:00
/* A <= B is the same as A - B = {}
*/
C_com(tp->tp_size);
C_and(tp->tp_size);
1986-06-20 14:36:49 +00:00
C_zer(tp->tp_size);
1986-05-28 18:36:51 +00:00
}
C_cms(tp->tp_size);
1986-05-21 18:32:20 +00:00
break;
default:
crash("bad type COMPARE");
}
if (true_label != 0) {
1986-08-26 14:33:24 +00:00
compare(expr->nd_symb, true_label);
1986-05-21 18:32:20 +00:00
C_bra(false_label);
}
else {
1986-08-26 14:33:24 +00:00
truthvalue(expr->nd_symb);
1986-05-21 18:32:20 +00:00
}
break;
case IN:
1986-05-28 18:36:51 +00:00
/* In this case, evaluate right hand side first! The
INN instruction expects the bit number on top of the
stack
*/
1986-08-26 14:33:24 +00:00
CodePExpr(rightop);
CodePExpr(leftop);
1986-05-28 18:36:51 +00:00
CodeCoercion(leftop->nd_type, word_type);
C_inn(rightop->nd_type->tp_size);
1986-06-10 13:18:52 +00:00
if (true_label != 0) {
C_zne(true_label);
C_bra(false_label);
}
1986-05-21 18:32:20 +00:00
break;
1986-10-06 20:36:30 +00:00
case OR:
1986-05-21 18:32:20 +00:00
case AND:
1986-08-26 14:33:24 +00:00
case '&': {
label l_true, l_false, l_maybe = ++text_label, l_end;
struct desig Des;
1986-05-21 18:32:20 +00:00
if (true_label == 0) {
1986-08-26 14:33:24 +00:00
l_true = ++text_label;
l_false = ++text_label;
l_end = ++text_label;
}
else {
l_true = true_label;
l_false = false_label;
}
Des = InitDesig;
1986-10-06 20:36:30 +00:00
if (expr->nd_symb == OR) {
CodeExpr(leftop, &Des, l_true, l_maybe);
}
else CodeExpr(leftop, &Des, l_maybe, l_false);
1986-08-26 14:33:24 +00:00
C_df_ilb(l_maybe);
Des = InitDesig;
CodeExpr(rightop, &Des, l_true, l_false);
if (true_label == 0) {
1986-05-21 18:32:20 +00:00
C_df_ilb(l_true);
C_loc((arith)1);
C_bra(l_end);
C_df_ilb(l_false);
C_loc((arith)0);
C_df_ilb(l_end);
}
break;
1986-08-26 14:33:24 +00:00
}
1986-05-21 18:32:20 +00:00
default:
1986-08-26 14:33:24 +00:00
crash("(CodeOper) Bad operator %s\n",symbol2str(expr->nd_symb));
1986-05-21 18:32:20 +00:00
}
}
/* compare() serves as an auxiliary function of CodeOper */
compare(relop, lbl)
int relop;
1986-05-23 19:25:21 +00:00
register label lbl;
1986-05-21 18:32:20 +00:00
{
switch (relop) {
case '<':
C_zlt(lbl);
break;
case LESSEQUAL:
C_zle(lbl);
break;
case '>':
C_zgt(lbl);
break;
case GREATEREQUAL:
C_zge(lbl);
break;
case '=':
C_zeq(lbl);
break;
case '#':
C_zne(lbl);
break;
default:
crash("(compare)");
}
}
/* truthvalue() serves as an auxiliary function of CodeOper */
truthvalue(relop)
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 '#':
C_tne();
break;
default:
crash("(truthvalue)");
}
}
CodeUoper(nd)
register struct node *nd;
{
register struct type *tp = nd->nd_type;
1986-06-17 12:04:05 +00:00
CodePExpr(nd->nd_right);
1986-05-21 18:32:20 +00:00
switch(nd->nd_symb) {
case '~':
case NOT:
C_teq();
break;
case '-':
switch(tp->tp_fund) {
case T_INTEGER:
1986-08-26 14:33:24 +00:00
case T_INTORCARD:
1986-05-21 18:32:20 +00:00
C_ngi(tp->tp_size);
break;
case T_REAL:
C_ngf(tp->tp_size);
break;
default:
crash("Bad operand to unary -");
}
break;
default:
crash("Bad unary operator");
}
}
1986-05-23 19:25:21 +00:00
CodeSet(nd)
register struct node *nd;
{
1986-10-06 20:36:30 +00:00
register struct type *tp = nd->nd_type;
1986-05-23 19:25:21 +00:00
1986-10-06 20:36:30 +00:00
C_zer(tp->tp_size); /* empty set */
1986-05-23 19:25:21 +00:00
nd = nd->nd_right;
while (nd) {
assert(nd->nd_class == Link && nd->nd_symb == ',');
CodeEl(nd->nd_left, tp);
nd = nd->nd_right;
}
}
CodeEl(nd, tp)
register struct node *nd;
1986-06-04 09:01:48 +00:00
register struct type *tp;
1986-05-23 19:25:21 +00:00
{
1986-06-26 09:39:36 +00:00
register struct type *eltype = ElementType(tp);
1986-05-23 19:25:21 +00:00
if (nd->nd_class == Link && nd->nd_symb == UPTO) {
1986-06-06 02:22:09 +00:00
C_loc(tp->tp_size); /* push size */
1986-06-26 09:39:36 +00:00
if (eltype->tp_fund == T_SUBRANGE) {
C_loc(eltype->sub_ub);
1986-06-04 09:01:48 +00:00
}
1986-06-26 09:39:36 +00:00
else C_loc((arith) (eltype->enm_ncst - 1));
1986-08-26 14:33:24 +00:00
Operands(nd->nd_left, nd->nd_right, word_type);
1986-05-23 19:25:21 +00:00
C_cal("_LtoUset"); /* library routine to fill set */
1986-06-06 02:22:09 +00:00
C_asp(4 * word_size);
1986-05-23 19:25:21 +00:00
}
else {
1986-05-30 18:48:00 +00:00
CodePExpr(nd);
1986-05-23 19:25:21 +00:00
C_set(tp->tp_size);
1986-06-06 02:22:09 +00:00
C_ior(tp->tp_size);
1986-05-23 19:25:21 +00:00
}
}
1986-05-30 18:48:00 +00:00
CodePExpr(nd)
struct node *nd;
{
/* Generate code to push the value of the expression "nd"
on the stack.
*/
struct desig designator;
designator = InitDesig;
CodeExpr(nd, &designator, NO_LABEL, NO_LABEL);
CodeValue(&designator, nd->nd_type->tp_size);
}
CodeDAddress(nd)
struct node *nd;
{
/* Generate code to push the address of the designator "nd"
on the stack.
*/
struct desig designator;
designator = InitDesig;
CodeDesig(nd, &designator);
CodeAddress(&designator);
}
CodeDStore(nd)
register struct node *nd;
{
/* Generate code to store the expression on the stack into the
designator "nd".
*/
struct desig designator;
designator = InitDesig;
CodeDesig(nd, &designator);
CodeStore(&designator, nd->nd_type->tp_size);
}
1986-06-04 09:01:48 +00:00
DoHIGH(nd)
struct node *nd;
{
1986-08-26 14:33:24 +00:00
/* Get the high index of a conformant array, indicated by "nd".
The high index is the second field in the descriptor of
the array, so it is easily found.
*/
1986-06-20 14:36:49 +00:00
register struct def *df = nd->nd_def;
register arith highoff;
1986-06-04 09:01:48 +00:00
assert(nd->nd_class == Def);
assert(df->df_kind == D_VARIABLE);
1986-08-26 14:33:24 +00:00
assert(IsConformantArray(df->df_type));
1986-06-04 09:01:48 +00:00
1986-08-26 14:33:24 +00:00
highoff = df->var_off /* base address and descriptor */
+ pointer_size /* skip base address */
+ word_size; /* skip first field of descriptor */
1986-06-04 09:01:48 +00:00
if (df->df_scope->sc_level < proclevel) {
1986-06-10 13:18:52 +00:00
C_lxa((arith) (proclevel - df->df_scope->sc_level));
1986-06-04 09:01:48 +00:00
C_lof(highoff);
}
else C_lol(highoff);
}