2016-11-27 10:37:25 +00:00
|
|
|
#include "b.h"
|
|
|
|
|
|
|
|
/*
|
2016-11-27 19:28:19 +00:00
|
|
|
* Code generation (EM)
|
2016-11-27 10:37:25 +00:00
|
|
|
*/
|
|
|
|
|
2016-11-27 19:28:19 +00:00
|
|
|
static int
|
|
|
|
shiftsize(void)
|
|
|
|
{
|
|
|
|
switch (wordsize) {
|
|
|
|
case 1: return 0;
|
|
|
|
case 2: return 1;
|
|
|
|
case 4: return 2;
|
|
|
|
case 8: return 3;
|
|
|
|
default:
|
|
|
|
error("unsupported word size");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 10:37:25 +00:00
|
|
|
void
|
2016-11-27 19:28:19 +00:00
|
|
|
tonativeaddr(void)
|
2016-11-27 10:37:25 +00:00
|
|
|
{
|
2016-11-27 19:28:19 +00:00
|
|
|
C_loc(shiftsize());
|
|
|
|
C_slu(wordsize);
|
2016-11-27 10:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-27 19:28:19 +00:00
|
|
|
fromnativeaddr(void)
|
|
|
|
{
|
|
|
|
C_loc(shiftsize());
|
|
|
|
C_sru(wordsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
manglename(char* name, char prefix)
|
2016-11-27 10:37:25 +00:00
|
|
|
{
|
2016-11-27 19:28:19 +00:00
|
|
|
static char buffer[NCPS+3];
|
|
|
|
buffer[0] = prefix;
|
|
|
|
buffer[1] = '_';
|
|
|
|
strcpy(buffer+2, name);
|
|
|
|
return buffer;
|
2016-11-27 10:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
binary(struct tnode *tr)
|
|
|
|
{
|
|
|
|
rcexpr(tr->tr1);
|
|
|
|
rcexpr(tr->tr2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pushargs(struct tnode *tr)
|
|
|
|
{
|
|
|
|
int stk;
|
|
|
|
|
|
|
|
if (tr == NULL)
|
|
|
|
return 0;
|
|
|
|
if (tr->op == COMMA) {
|
|
|
|
rcexpr(tr->tr2);
|
|
|
|
stk = pushargs(tr->tr1);
|
2016-11-27 19:28:19 +00:00
|
|
|
return stk+wordsize;
|
2016-11-27 10:37:25 +00:00
|
|
|
}
|
|
|
|
rcexpr(tr);
|
2016-11-27 19:28:19 +00:00
|
|
|
return wordsize;
|
2016-11-27 10:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lvalexp(struct tnode *tr)
|
|
|
|
{
|
|
|
|
struct hshtab *bs;
|
|
|
|
char memloc[64];
|
|
|
|
|
|
|
|
switch (tr->op) {
|
|
|
|
|
|
|
|
case DECBEF:
|
|
|
|
case INCBEF:
|
|
|
|
case DECAFT:
|
|
|
|
case INCAFT:
|
|
|
|
if (tr->tr1->op == STAR) {
|
|
|
|
rcexpr(tr->tr1->tr1);
|
2016-11-27 19:28:19 +00:00
|
|
|
tonativeaddr();
|
2017-01-07 17:46:03 +00:00
|
|
|
|
|
|
|
if ((tr->op == DECBEF) || (tr->op == INCBEF)) {
|
|
|
|
C_dup(wordsize); /* ( addr addr -- ) */
|
|
|
|
C_loi(wordsize); /* ( addr val -- ) */
|
|
|
|
if (tr->op == DECBEF)
|
|
|
|
C_dec(); /* ( addr newval -- ) */
|
|
|
|
else
|
|
|
|
C_inc(); /* ( addr newval -- ) */
|
|
|
|
C_exg(wordsize); /* ( newval addr -- ) */
|
|
|
|
C_dup(wordsize*2); /* ( newval addr newval addr -- ) */
|
|
|
|
C_sti(wordsize); /* ( newval addr -- ) */
|
|
|
|
C_asp(wordsize); /* ( newval -- ) */
|
|
|
|
} else {
|
|
|
|
C_dup(wordsize); /* ( addr addr -- ) */
|
|
|
|
C_loi(wordsize); /* ( addr val -- ) */
|
|
|
|
C_dup(wordsize*2); /* ( addr val addr val -- ) */
|
|
|
|
if (tr->op == DECAFT)
|
|
|
|
C_dec(); /* ( addr val addr newval -- ) */
|
|
|
|
else
|
|
|
|
C_inc(); /* ( addr val addr newval -- ) */
|
|
|
|
C_exg(wordsize); /* ( addr val newval addr -- ) */
|
|
|
|
C_sti(wordsize); /* ( addr val -- ) */
|
|
|
|
C_exg(wordsize); /* ( val addr -- ) */
|
|
|
|
C_asp(wordsize); /* ( val -- ) */
|
|
|
|
}
|
2016-11-27 10:37:25 +00:00
|
|
|
} else { /* NAME, checked in "build" */
|
|
|
|
bs = (struct hshtab *) tr->tr1->tr1;
|
2017-01-07 17:46:03 +00:00
|
|
|
if (bs->class == EXTERN) {
|
|
|
|
switch (tr->op) {
|
|
|
|
case INCBEF:
|
|
|
|
C_ine_dnam(manglename(bs->name, 'b'), 0);
|
|
|
|
C_loe_dnam(manglename(bs->name, 'b'), 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DECBEF:
|
|
|
|
C_dee_dnam(manglename(bs->name, 'b'), 0);
|
|
|
|
C_loe_dnam(manglename(bs->name, 'b'), 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INCAFT:
|
|
|
|
C_loe_dnam(manglename(bs->name, 'b'), 0);
|
|
|
|
C_ine_dnam(manglename(bs->name, 'b'), 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DECAFT:
|
|
|
|
C_loe_dnam(manglename(bs->name, 'b'), 0);
|
|
|
|
C_dee_dnam(manglename(bs->name, 'b'), 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (bs->class == AUTO) {
|
|
|
|
switch (tr->op) {
|
|
|
|
case INCBEF:
|
|
|
|
C_inl(bs->offset);
|
|
|
|
C_lol(bs->offset);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DECBEF:
|
|
|
|
C_del(bs->offset);
|
|
|
|
C_lol(bs->offset);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INCAFT:
|
|
|
|
C_lol(bs->offset);
|
|
|
|
C_inl(bs->offset);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DECAFT:
|
|
|
|
C_lol(bs->offset);
|
|
|
|
C_del(bs->offset);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
2016-11-27 10:37:25 +00:00
|
|
|
goto classerror;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ASSIGN:
|
|
|
|
rcexpr(tr->tr2);
|
2016-11-27 19:28:19 +00:00
|
|
|
C_dup(wordsize);
|
2016-11-27 10:37:25 +00:00
|
|
|
if (tr->tr1->op == STAR) {
|
|
|
|
rcexpr(tr->tr1->tr1);
|
2016-11-27 19:28:19 +00:00
|
|
|
tonativeaddr();
|
|
|
|
C_sti(wordsize);
|
2016-11-27 10:37:25 +00:00
|
|
|
} else { /* NAME */
|
|
|
|
bs = (struct hshtab *) tr->tr1->tr1;
|
2016-11-27 19:28:19 +00:00
|
|
|
if (bs->class == EXTERN) {
|
2017-01-06 22:24:05 +00:00
|
|
|
C_ste_dnam(manglename(bs->name, 'b'), 0);
|
2016-11-27 19:28:19 +00:00
|
|
|
} else if (bs->class == AUTO) {
|
|
|
|
C_stl(bs->offset);
|
|
|
|
} else
|
2016-11-27 10:37:25 +00:00
|
|
|
goto classerror;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ASPLUS:
|
|
|
|
case ASMINUS:
|
|
|
|
case ASMOD:
|
|
|
|
case ASTIMES:
|
|
|
|
case ASDIV:
|
|
|
|
case ASOR:
|
|
|
|
case ASAND:
|
|
|
|
case ASLSH:
|
|
|
|
case ASRSH:
|
|
|
|
case ASEQUAL:
|
|
|
|
case ASNEQL:
|
|
|
|
case ASLEQ:
|
|
|
|
case ASLESS:
|
|
|
|
case ASGTQ:
|
|
|
|
case ASGREAT:
|
2016-12-31 17:36:12 +00:00
|
|
|
case ASEOR:
|
2016-11-27 10:37:25 +00:00
|
|
|
tr->op -= ASPLUS-PLUS;
|
|
|
|
rcexpr(block(ASSIGN,0,tr->tr1,tr));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
classerror:
|
|
|
|
error("Storage class");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rcexpr(struct tnode *tr)
|
|
|
|
{
|
|
|
|
int o1, o2;
|
|
|
|
int stk;
|
|
|
|
struct hshtab *bs;
|
|
|
|
|
|
|
|
if (tr == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (opdope[tr->op]&02) {
|
|
|
|
lvalexp(tr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (tr->op) {
|
|
|
|
|
|
|
|
case CON:
|
2016-11-27 19:28:19 +00:00
|
|
|
C_loc(tr->value);
|
2016-11-27 10:37:25 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case STRING:
|
2016-11-27 19:28:19 +00:00
|
|
|
C_lae_dlb(tr->value, 0);
|
|
|
|
fromnativeaddr();
|
2016-11-27 10:37:25 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case NAME: /* only rvalue */
|
|
|
|
bs = (struct hshtab *) tr->tr1;
|
|
|
|
if (bs->class == EXTERN)
|
2016-11-27 19:28:19 +00:00
|
|
|
C_loe_dnam(manglename(bs->name, 'b'), 0);
|
2016-11-27 10:37:25 +00:00
|
|
|
else if (bs->class == AUTO)
|
2016-11-27 19:28:19 +00:00
|
|
|
C_lol(bs->offset);
|
2016-11-27 10:37:25 +00:00
|
|
|
else
|
|
|
|
goto classerror;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CALL:
|
|
|
|
stk = pushargs(tr->tr2);
|
|
|
|
rcexpr(tr->tr1);
|
2016-11-27 19:28:19 +00:00
|
|
|
tonativeaddr();
|
|
|
|
C_cai();
|
2016-11-27 10:37:25 +00:00
|
|
|
if (stk)
|
2016-11-27 19:28:19 +00:00
|
|
|
C_asp(stk);
|
|
|
|
C_lfr(wordsize);
|
2016-11-27 10:37:25 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case AMPER:
|
|
|
|
bs = (struct hshtab *) tr->tr1->tr1;
|
|
|
|
if (bs->class == EXTERN) {
|
2016-11-27 19:28:19 +00:00
|
|
|
C_lae_dnam(manglename(bs->name, 'b'), 0);
|
2016-11-27 10:37:25 +00:00
|
|
|
} else if (bs->class == AUTO) {
|
2016-11-27 19:28:19 +00:00
|
|
|
C_lal(bs->offset);
|
2016-11-27 10:37:25 +00:00
|
|
|
} else
|
|
|
|
goto classerror;
|
2016-11-27 19:28:19 +00:00
|
|
|
fromnativeaddr();
|
2016-11-27 10:37:25 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case STAR: /* only rvalue */
|
|
|
|
rcexpr(tr->tr1);
|
2016-11-27 19:28:19 +00:00
|
|
|
tonativeaddr();
|
|
|
|
C_loi(wordsize);
|
2016-11-27 10:37:25 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case PLUS:
|
|
|
|
binary(tr);
|
2016-11-27 19:28:19 +00:00
|
|
|
C_adi(wordsize);
|
2016-11-27 10:37:25 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case MINUS:
|
|
|
|
binary(tr);
|
2016-11-27 19:28:19 +00:00
|
|
|
C_sbi(wordsize);
|
2016-11-27 10:37:25 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case TIMES:
|
|
|
|
binary(tr);
|
2016-11-27 19:28:19 +00:00
|
|
|
C_mli(wordsize);
|
2016-11-27 10:37:25 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case DIVIDE:
|
|
|
|
binary(tr);
|
2016-11-27 19:28:19 +00:00
|
|
|
C_dvi(wordsize);
|
2016-11-27 10:37:25 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case MOD:
|
|
|
|
binary(tr);
|
2016-11-27 19:28:19 +00:00
|
|
|
C_rmi(wordsize);
|
2016-11-27 10:37:25 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case AND:
|
|
|
|
binary(tr);
|
2016-11-27 19:28:19 +00:00
|
|
|
C_and(wordsize);
|
2016-11-27 10:37:25 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case OR:
|
|
|
|
binary(tr);
|
2016-11-27 19:28:19 +00:00
|
|
|
C_ior(wordsize);
|
2016-11-27 10:37:25 +00:00
|
|
|
return;
|
|
|
|
|
2016-12-31 17:36:12 +00:00
|
|
|
case EOR:
|
|
|
|
binary(tr);
|
|
|
|
C_xor(wordsize);
|
|
|
|
return;
|
|
|
|
|
2016-11-27 10:37:25 +00:00
|
|
|
case LSHIFT:
|
|
|
|
binary(tr);
|
2016-11-27 19:28:19 +00:00
|
|
|
C_sli(wordsize);
|
2016-11-27 10:37:25 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case RSHIFT:
|
|
|
|
binary(tr);
|
2016-11-27 19:28:19 +00:00
|
|
|
C_sri(wordsize);
|
2016-11-27 10:37:25 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case EQUAL:
|
|
|
|
case NEQUAL:
|
|
|
|
case LESS:
|
|
|
|
case LESSEQ:
|
|
|
|
case GREAT:
|
|
|
|
case GREATEQ:
|
|
|
|
binary(tr);
|
2016-11-27 19:28:19 +00:00
|
|
|
C_cmi(wordsize);
|
2016-11-27 10:37:25 +00:00
|
|
|
switch (tr->op) {
|
|
|
|
case EQUAL:
|
2016-11-27 19:28:19 +00:00
|
|
|
C_teq();
|
2016-11-27 10:37:25 +00:00
|
|
|
break;
|
|
|
|
case NEQUAL:
|
2016-11-27 19:28:19 +00:00
|
|
|
C_tne();
|
2016-11-27 10:37:25 +00:00
|
|
|
break;
|
|
|
|
case LESS:
|
2016-11-27 19:28:19 +00:00
|
|
|
C_tlt();
|
2016-11-27 10:37:25 +00:00
|
|
|
break;
|
|
|
|
case LESSEQ:
|
2016-11-27 19:28:19 +00:00
|
|
|
C_tle();
|
2016-11-27 10:37:25 +00:00
|
|
|
break;
|
|
|
|
case GREAT:
|
2016-11-27 19:28:19 +00:00
|
|
|
C_tgt();
|
2016-11-27 10:37:25 +00:00
|
|
|
break;
|
|
|
|
case GREATEQ:
|
2016-11-27 19:28:19 +00:00
|
|
|
C_tge();
|
2016-11-27 10:37:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case EXCLA:
|
|
|
|
rcexpr(tr->tr1);
|
2016-12-31 00:14:04 +00:00
|
|
|
C_teq();
|
2016-11-27 10:37:25 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case NEG:
|
|
|
|
rcexpr(tr->tr1);
|
2016-11-27 19:28:19 +00:00
|
|
|
C_ngi(wordsize);
|
2016-11-27 10:37:25 +00:00
|
|
|
return;
|
|
|
|
|
2017-01-01 17:40:06 +00:00
|
|
|
case NOT:
|
|
|
|
rcexpr(tr->tr1);
|
|
|
|
C_com(wordsize);
|
|
|
|
return;
|
|
|
|
|
2016-11-27 10:37:25 +00:00
|
|
|
case QUEST:
|
2016-11-27 19:28:19 +00:00
|
|
|
cbranch(tr->tr1, o1=isn++);
|
2016-11-27 10:37:25 +00:00
|
|
|
rcexpr(tr->tr2->tr1);
|
|
|
|
jump(o2 = isn++);
|
2016-11-27 19:28:19 +00:00
|
|
|
fnlabel(o1);
|
2016-11-27 10:37:25 +00:00
|
|
|
rcexpr(tr->tr2->tr2);
|
2016-11-27 19:28:19 +00:00
|
|
|
fnlabel(o2);
|
2016-11-27 10:37:25 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error("Can't print tree (op: %d)", tr->op);
|
|
|
|
}
|
|
|
|
|
|
|
|
classerror:
|
|
|
|
error("Storage class");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prints the tree in RPN, for debugging */
|
|
|
|
/*
|
|
|
|
void
|
|
|
|
rcexpr(struct tnode *tr)
|
|
|
|
{
|
|
|
|
struct hshtab *bs;
|
|
|
|
|
|
|
|
if (tr == NULL)
|
|
|
|
printf("(NULL) ");
|
|
|
|
else if (tr->op == CON)
|
|
|
|
printf("%d ", tr->value);
|
|
|
|
else if (tr->op == STRING)
|
|
|
|
printf("s(L%d) ", tr->value);
|
|
|
|
else if (tr->op == NAME) {
|
|
|
|
bs = (struct hshtab *)tr->tr1;
|
|
|
|
if (bs->class == AUTO)
|
|
|
|
printf("%s(%d) ", bs->name, bs->offset);
|
|
|
|
else
|
|
|
|
printf("%s ", bs->name);
|
|
|
|
} else {
|
|
|
|
rcexpr(tr->tr1);
|
|
|
|
if (opdope[tr->op]&01)
|
|
|
|
rcexpr(tr->tr2);
|
|
|
|
printtoken(tr->op, stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|