2016-09-17 22:02:16 +00:00
|
|
|
#include "mcg.h"
|
|
|
|
|
2016-09-18 21:24:54 +00:00
|
|
|
static struct symbol* currentproc;
|
2016-09-19 21:06:59 +00:00
|
|
|
static struct basicblock* current_bb;
|
2016-09-18 21:24:54 +00:00
|
|
|
|
|
|
|
static int stackptr;
|
|
|
|
static struct ir* stack[64];
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
static struct ir* convert(struct ir* src, int destsize, int opcode);
|
|
|
|
static struct ir* appendir(struct ir* ir);
|
|
|
|
|
|
|
|
static void reset_stack(void)
|
2016-09-17 22:02:16 +00:00
|
|
|
{
|
2016-09-18 21:24:54 +00:00
|
|
|
stackptr = 0;
|
2016-09-17 22:02:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 21:24:54 +00:00
|
|
|
static void push(struct ir* ir)
|
2016-09-17 22:02:16 +00:00
|
|
|
{
|
2016-09-18 21:24:54 +00:00
|
|
|
if (stackptr == sizeof(stack)/sizeof(*stack))
|
|
|
|
fatal("stack overflow");
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
/* If we try to push something which is too small, convert it to a word
|
|
|
|
* first. */
|
2016-09-17 22:02:16 +00:00
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
if (ir->size < EM_wordsize)
|
2016-09-19 21:30:41 +00:00
|
|
|
ir = convert(ir, EM_wordsize, IR_CIU1);
|
2016-09-18 21:24:54 +00:00
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
stack[stackptr++] = ir;
|
2016-09-17 22:02:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
static struct ir* pop(int size)
|
2016-09-17 22:02:16 +00:00
|
|
|
{
|
2016-09-19 21:06:59 +00:00
|
|
|
if (stackptr == 0)
|
|
|
|
{
|
|
|
|
/* Nothing in our fake stack, so we have to read from the real stack. */
|
2016-09-18 21:24:54 +00:00
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
if (size < EM_wordsize)
|
|
|
|
size = EM_wordsize;
|
|
|
|
return
|
|
|
|
new_ir0(
|
|
|
|
IR_POP, size
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct ir* ir = stack[--stackptr];
|
2016-09-17 22:02:16 +00:00
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
/* If we try to pop something which is smaller than a word, convert it first. */
|
|
|
|
|
|
|
|
if (size < EM_wordsize)
|
2016-09-19 21:30:41 +00:00
|
|
|
ir = convert(ir, size, IR_CIU1);
|
2016-09-18 21:24:54 +00:00
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
if (ir->size != size)
|
|
|
|
fatal("expected an item on stack of size %d, but got %d\n", size, ir->size);
|
|
|
|
return ir;
|
|
|
|
}
|
2016-09-17 22:02:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
static void print_stack(void)
|
2016-09-17 22:02:16 +00:00
|
|
|
{
|
2016-09-18 21:24:54 +00:00
|
|
|
int i;
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
printf("\t; stack:");
|
2016-09-18 21:24:54 +00:00
|
|
|
for (i=0; i<stackptr; i++)
|
2016-09-19 21:06:59 +00:00
|
|
|
{
|
|
|
|
struct ir* ir = stack[i];
|
|
|
|
printf(" $%d.%d", ir->id, ir->size);
|
|
|
|
}
|
|
|
|
printf(" (top)\n");
|
2016-09-17 22:02:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
static struct ir* appendir(struct ir* ir)
|
2016-09-17 22:02:16 +00:00
|
|
|
{
|
2016-09-18 21:24:54 +00:00
|
|
|
int i;
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
assert(current_bb != NULL);
|
|
|
|
ir->is_sequence = true;
|
|
|
|
APPEND(current_bb->irs, ir);
|
2016-09-18 21:24:54 +00:00
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
ir_print(ir);
|
|
|
|
return ir;
|
2016-09-17 22:02:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
static void materialise_stack(void)
|
2016-09-17 22:02:16 +00:00
|
|
|
{
|
2016-09-19 21:06:59 +00:00
|
|
|
int i;
|
2016-09-18 21:24:54 +00:00
|
|
|
|
2016-09-19 22:19:39 +00:00
|
|
|
for (i=0; i<stackptr; i++)
|
2016-09-18 21:24:54 +00:00
|
|
|
{
|
2016-09-19 21:06:59 +00:00
|
|
|
struct ir* ir = stack[i];
|
|
|
|
appendir(
|
|
|
|
new_ir1(
|
|
|
|
IR_PUSH, ir->size,
|
|
|
|
ir
|
|
|
|
)
|
|
|
|
);
|
2016-09-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
reset_stack();
|
2016-09-17 22:02:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
void tb_filestart(void)
|
2016-09-17 22:02:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
void tb_fileend(void)
|
2016-09-17 22:02:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void tb_regvar(arith offset, int size, int type, int priority)
|
|
|
|
{
|
2016-09-18 21:24:54 +00:00
|
|
|
/* ignored */
|
2016-09-17 22:02:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 21:24:54 +00:00
|
|
|
static struct ir* address_of_local(int index)
|
2016-09-17 22:02:16 +00:00
|
|
|
{
|
2016-09-18 21:24:54 +00:00
|
|
|
return
|
|
|
|
new_ir2(
|
|
|
|
IR_ADD, EM_pointersize,
|
|
|
|
new_regir((index < 0) ? IRR_LB : IRR_AB),
|
|
|
|
new_wordir(index)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-09-19 22:19:39 +00:00
|
|
|
static struct ir* address_of_external(const char* label, arith offset)
|
|
|
|
{
|
|
|
|
if (offset != 0)
|
|
|
|
return
|
|
|
|
new_ir2(
|
|
|
|
IR_ADD, EM_pointersize,
|
|
|
|
new_labelir(label),
|
|
|
|
new_wordir(offset)
|
|
|
|
);
|
|
|
|
else
|
|
|
|
return
|
|
|
|
new_labelir(label);
|
|
|
|
}
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
static struct ir* convert(struct ir* src, int destsize, int opcode)
|
2016-09-18 21:24:54 +00:00
|
|
|
{
|
2016-09-19 21:06:59 +00:00
|
|
|
switch (src->size)
|
2016-09-18 21:24:54 +00:00
|
|
|
{
|
|
|
|
case 1: opcode += 0; break;
|
|
|
|
case 2: opcode += 1; break;
|
|
|
|
case 4: opcode += 2; break;
|
|
|
|
case 8: opcode += 3; break;
|
|
|
|
default:
|
2016-09-19 21:06:59 +00:00
|
|
|
fatal("can't convert from things of size %d", src->size);
|
2016-09-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
new_ir1(
|
|
|
|
opcode, destsize,
|
2016-09-19 21:06:59 +00:00
|
|
|
src
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ir* tristate_compare(int size, int opcode)
|
|
|
|
{
|
|
|
|
struct ir* right = pop(size);
|
|
|
|
struct ir* left = pop(size);
|
|
|
|
|
|
|
|
return
|
|
|
|
new_ir2(
|
|
|
|
opcode, size,
|
|
|
|
left, right
|
2016-09-18 21:24:54 +00:00
|
|
|
);
|
2016-09-17 22:02:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 22:19:39 +00:00
|
|
|
static void simple_convert(int opcode)
|
2016-09-19 21:30:41 +00:00
|
|
|
{
|
|
|
|
struct ir* destsize = pop(EM_wordsize);
|
|
|
|
struct ir* srcsize = pop(EM_wordsize);
|
|
|
|
struct ir* value;
|
|
|
|
|
|
|
|
assert(srcsize->opcode == IR_ICONST);
|
|
|
|
assert(destsize->opcode == IR_ICONST);
|
|
|
|
|
|
|
|
value = pop(srcsize->u.ivalue);
|
|
|
|
push(
|
|
|
|
convert(value, destsize->u.ivalue, opcode)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
static void insn_simple(int opcode)
|
2016-09-17 22:02:16 +00:00
|
|
|
{
|
2016-09-18 21:24:54 +00:00
|
|
|
switch (opcode)
|
|
|
|
{
|
2016-09-19 21:30:41 +00:00
|
|
|
case op_bra:
|
2016-09-18 21:24:54 +00:00
|
|
|
{
|
2016-09-19 21:30:41 +00:00
|
|
|
struct ir* dest = pop(EM_pointersize);
|
2016-09-18 21:24:54 +00:00
|
|
|
|
2016-09-19 21:30:41 +00:00
|
|
|
materialise_stack();
|
|
|
|
appendir(
|
|
|
|
new_ir1(
|
|
|
|
IR_JUMP, 0,
|
|
|
|
dest
|
|
|
|
)
|
2016-09-18 21:24:54 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2016-09-19 21:30:41 +00:00
|
|
|
|
|
|
|
case op_cii: simple_convert(IR_CII1); break;
|
|
|
|
case op_ciu: simple_convert(IR_CIU1); break;
|
2016-09-18 21:24:54 +00:00
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
case op_cmp:
|
|
|
|
push(
|
|
|
|
tristate_compare(EM_pointersize, IR_COMPAREU)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
2016-09-19 21:30:41 +00:00
|
|
|
case op_cai:
|
|
|
|
{
|
|
|
|
struct ir* dest = pop(EM_pointersize);
|
|
|
|
|
|
|
|
materialise_stack();
|
|
|
|
appendir(
|
|
|
|
new_ir1(
|
|
|
|
IR_CALL, 0,
|
|
|
|
dest
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-18 21:24:54 +00:00
|
|
|
default:
|
2016-09-19 21:06:59 +00:00
|
|
|
fatal("treebuilder: unknown simple instruction '%s'",
|
2016-09-18 21:24:54 +00:00
|
|
|
em_mnem[opcode - sp_fmnem]);
|
|
|
|
}
|
2016-09-17 22:02:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
static void simple_branch2(int opcode, int size,
|
|
|
|
struct basicblock* truebb, struct basicblock* falsebb,
|
|
|
|
int irop)
|
2016-09-17 22:02:16 +00:00
|
|
|
{
|
2016-09-19 21:06:59 +00:00
|
|
|
struct ir* right = pop(size);
|
|
|
|
struct ir* left = pop(size);
|
2016-09-18 21:24:54 +00:00
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
materialise_stack();
|
|
|
|
appendir(
|
|
|
|
new_ir2(
|
|
|
|
IR_CJUMP, 0,
|
|
|
|
new_ir2(
|
|
|
|
irop, size,
|
|
|
|
left, right
|
|
|
|
),
|
|
|
|
new_ir2(
|
|
|
|
IR_PAIR, 0,
|
|
|
|
new_bbir(truebb),
|
|
|
|
new_bbir(falsebb)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2016-09-18 21:24:54 +00:00
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
static void compare0_branch2(int opcode,
|
|
|
|
struct basicblock* truebb, struct basicblock* falsebb,
|
|
|
|
int irop)
|
|
|
|
{
|
|
|
|
push(
|
|
|
|
new_wordir(0)
|
|
|
|
);
|
2016-09-18 21:24:54 +00:00
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
simple_branch2(opcode, EM_wordsize, truebb, falsebb, irop);
|
|
|
|
}
|
2016-09-18 21:24:54 +00:00
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
static void insn_bvalue(int opcode, struct basicblock* leftbb, struct basicblock* rightbb)
|
|
|
|
{
|
|
|
|
switch (opcode)
|
|
|
|
{
|
|
|
|
case op_zeq: compare0_branch2(opcode, leftbb, rightbb, IR_IFEQ); break;
|
|
|
|
case op_zlt: compare0_branch2(opcode, leftbb, rightbb, IR_IFLT); break;
|
|
|
|
case op_zle: compare0_branch2(opcode, leftbb, rightbb, IR_IFLE); break;
|
|
|
|
|
|
|
|
case op_zne: compare0_branch2(opcode, rightbb, leftbb, IR_IFEQ); break;
|
|
|
|
case op_zge: compare0_branch2(opcode, rightbb, leftbb, IR_IFLT); break;
|
|
|
|
case op_zgt: compare0_branch2(opcode, rightbb, leftbb, IR_IFLE); break;
|
2016-09-18 21:24:54 +00:00
|
|
|
|
|
|
|
case op_bra:
|
|
|
|
{
|
2016-09-19 21:06:59 +00:00
|
|
|
materialise_stack();
|
2016-09-18 21:24:54 +00:00
|
|
|
|
|
|
|
appendir(
|
|
|
|
new_ir1(
|
|
|
|
IR_JUMP, 0,
|
2016-09-19 21:06:59 +00:00
|
|
|
new_bbir(leftbb)
|
2016-09-18 21:24:54 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2016-09-19 21:06:59 +00:00
|
|
|
fatal("treebuilder: unknown bvalue instruction '%s'",
|
2016-09-18 21:24:54 +00:00
|
|
|
em_mnem[opcode - sp_fmnem]);
|
|
|
|
}
|
2016-09-17 22:02:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
static void simple_alu1(int opcode, int size, int irop)
|
|
|
|
{
|
|
|
|
struct ir* val = pop(size);
|
|
|
|
|
|
|
|
push(
|
|
|
|
new_ir1(
|
|
|
|
irop, size,
|
|
|
|
val
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void simple_alu2(int opcode, int size, int irop)
|
2016-09-17 22:02:16 +00:00
|
|
|
{
|
2016-09-19 21:06:59 +00:00
|
|
|
struct ir* right = pop(size);
|
|
|
|
struct ir* left = pop(size);
|
|
|
|
|
|
|
|
push(
|
|
|
|
new_ir2(
|
|
|
|
irop, size,
|
|
|
|
left, right
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2016-09-18 21:24:54 +00:00
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
static void insn_ivalue(int opcode, arith value)
|
|
|
|
{
|
2016-09-18 21:24:54 +00:00
|
|
|
switch (opcode)
|
|
|
|
{
|
2016-09-19 21:06:59 +00:00
|
|
|
case op_adi: simple_alu2(opcode, value, IR_ADD); break;
|
|
|
|
case op_sbi: simple_alu2(opcode, value, IR_SUB); break;
|
|
|
|
case op_mli: simple_alu2(opcode, value, IR_MUL); break;
|
|
|
|
case op_dvi: simple_alu2(opcode, value, IR_DIV); break;
|
|
|
|
case op_rmi: simple_alu2(opcode, value, IR_MOD); break;
|
|
|
|
case op_ngi: simple_alu1(opcode, value, IR_NEG); break;
|
|
|
|
|
2016-09-19 21:30:41 +00:00
|
|
|
case op_and: simple_alu2(opcode, value, IR_AND); break;
|
|
|
|
case op_ior: simple_alu2(opcode, value, IR_OR); break;
|
|
|
|
case op_xor: simple_alu2(opcode, value, IR_EOR); break;
|
|
|
|
case op_com: simple_alu1(opcode, value, IR_NOT); break;
|
|
|
|
|
2016-09-18 21:24:54 +00:00
|
|
|
case op_lol:
|
|
|
|
push(
|
|
|
|
new_ir1(
|
|
|
|
IR_LOAD, EM_wordsize,
|
|
|
|
address_of_local(value)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case op_stl:
|
|
|
|
appendir(
|
|
|
|
new_ir2(
|
|
|
|
IR_STORE, EM_wordsize,
|
|
|
|
address_of_local(value),
|
2016-09-19 21:06:59 +00:00
|
|
|
pop(EM_wordsize)
|
2016-09-18 21:24:54 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
case op_lal:
|
|
|
|
push(
|
|
|
|
address_of_local(value)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
2016-09-18 21:24:54 +00:00
|
|
|
case op_loc:
|
|
|
|
push(
|
|
|
|
new_wordir(value)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case op_loi:
|
|
|
|
push(
|
|
|
|
new_ir1(
|
|
|
|
IR_LOAD, value,
|
2016-09-19 21:06:59 +00:00
|
|
|
pop(EM_pointersize)
|
2016-09-18 21:24:54 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case op_sti:
|
2016-09-19 21:06:59 +00:00
|
|
|
{
|
|
|
|
struct ir* ptr = pop(EM_pointersize);
|
|
|
|
struct ir* val = pop(value);
|
2016-09-18 21:24:54 +00:00
|
|
|
|
|
|
|
appendir(
|
|
|
|
new_ir2(
|
|
|
|
IR_STORE, value,
|
2016-09-19 21:06:59 +00:00
|
|
|
ptr, val
|
2016-09-18 21:24:54 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
2016-09-19 21:06:59 +00:00
|
|
|
}
|
2016-09-18 21:24:54 +00:00
|
|
|
|
|
|
|
case op_cmi:
|
|
|
|
push(
|
|
|
|
tristate_compare(value, IR_COMPARES)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case op_cmu:
|
|
|
|
push(
|
|
|
|
tristate_compare(value, IR_COMPAREU)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case op_ads:
|
2016-09-19 21:06:59 +00:00
|
|
|
{
|
|
|
|
struct ir* off = pop(value);
|
|
|
|
struct ir* ptr = pop(EM_pointersize);
|
2016-09-18 21:24:54 +00:00
|
|
|
|
|
|
|
if (value != EM_pointersize)
|
2016-09-19 21:30:41 +00:00
|
|
|
off = convert(off, EM_pointersize, IR_CII1);
|
2016-09-18 21:24:54 +00:00
|
|
|
|
|
|
|
push(
|
|
|
|
new_ir2(
|
2016-09-19 21:06:59 +00:00
|
|
|
IR_ADD, EM_pointersize,
|
|
|
|
ptr, off
|
2016-09-18 21:24:54 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
2016-09-19 21:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case op_adp:
|
|
|
|
{
|
|
|
|
struct ir* ptr = pop(EM_pointersize);
|
2016-09-18 21:24:54 +00:00
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
push(
|
|
|
|
new_ir2(
|
|
|
|
IR_ADD, EM_pointersize,
|
|
|
|
ptr,
|
|
|
|
new_wordir(value)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case op_sbs:
|
|
|
|
{
|
|
|
|
struct ir* right = pop(EM_pointersize);
|
|
|
|
struct ir* left = pop(EM_pointersize);
|
|
|
|
|
|
|
|
struct ir* delta =
|
|
|
|
new_ir2(
|
|
|
|
IR_SUB, EM_pointersize,
|
|
|
|
left, right
|
|
|
|
);
|
|
|
|
|
|
|
|
if (value != EM_pointersize)
|
2016-09-19 21:30:41 +00:00
|
|
|
delta = convert(delta, value, IR_CII1);
|
2016-09-19 21:06:59 +00:00
|
|
|
|
|
|
|
push(delta);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-18 21:24:54 +00:00
|
|
|
case op_dup:
|
|
|
|
{
|
2016-09-19 21:06:59 +00:00
|
|
|
struct ir* v = pop(value);
|
|
|
|
if (!v->is_sequence)
|
|
|
|
appendir(v);
|
2016-09-18 21:24:54 +00:00
|
|
|
push(v);
|
|
|
|
push(v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case op_asp:
|
|
|
|
{
|
|
|
|
switch (value)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
case -2:
|
|
|
|
case -4:
|
|
|
|
case -8:
|
|
|
|
push(new_anyir(-value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
appendir(
|
|
|
|
new_ir2(
|
|
|
|
IR_SETREG, EM_pointersize,
|
|
|
|
new_regir(IRR_SP),
|
|
|
|
new_ir2(
|
|
|
|
IR_ADD, EM_pointersize,
|
|
|
|
new_regir(IRR_SP),
|
|
|
|
new_wordir(value)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case op_ret:
|
|
|
|
{
|
|
|
|
if (value > 0)
|
|
|
|
{
|
2016-09-19 21:06:59 +00:00
|
|
|
struct ir* retval = pop(value);
|
2016-09-18 21:24:54 +00:00
|
|
|
appendir(
|
|
|
|
new_ir2(
|
|
|
|
IR_SETREG, value,
|
|
|
|
new_regir(IRR_RR),
|
2016-09-19 21:06:59 +00:00
|
|
|
retval
|
2016-09-18 21:24:54 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
appendir(
|
|
|
|
new_ir0(
|
|
|
|
IR_RET, 0
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
case op_lfr:
|
|
|
|
{
|
|
|
|
push(
|
|
|
|
appendir(
|
|
|
|
new_ir1(
|
|
|
|
IR_GETREG, value,
|
|
|
|
new_regir(IRR_RR)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-19 22:19:39 +00:00
|
|
|
case op_csa:
|
|
|
|
case op_csb:
|
|
|
|
{
|
|
|
|
struct basicblock* data_bb;
|
|
|
|
int i;
|
|
|
|
const char* helper = aprintf(".%s%d",
|
|
|
|
(opcode == op_csa) ? "csa" : "csb",
|
|
|
|
value);
|
|
|
|
struct ir* descriptor = pop(EM_pointersize);
|
|
|
|
|
|
|
|
if (descriptor->opcode != IR_LABEL)
|
|
|
|
fatal("csa/csb are only supported if they refer "
|
|
|
|
"directly to a descriptor block");
|
|
|
|
|
|
|
|
/* Splice the outgoing bbs in the data block into our own. */
|
|
|
|
|
|
|
|
data_bb = bb_get(descriptor->u.lvalue);
|
|
|
|
for (i=0; i<data_bb->outblocks_count; i++)
|
|
|
|
{
|
|
|
|
struct basicblock* bb = data_bb->outblocks[i];
|
|
|
|
printf("\t; may jump to %s\n", bb->name);
|
|
|
|
APPENDU(current_bb->outblocks, bb);
|
|
|
|
APPENDU(bb->inblocks, current_bb);
|
|
|
|
}
|
|
|
|
|
|
|
|
push(descriptor);
|
|
|
|
materialise_stack();
|
|
|
|
appendir(
|
|
|
|
new_ir1(
|
|
|
|
IR_JUMP, 0,
|
|
|
|
new_labelir(helper)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-18 21:24:54 +00:00
|
|
|
default:
|
2016-09-19 21:06:59 +00:00
|
|
|
fatal("treebuilder: unknown ivalue instruction '%s'",
|
2016-09-18 21:24:54 +00:00
|
|
|
em_mnem[opcode - sp_fmnem]);
|
|
|
|
}
|
2016-09-17 22:02:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
static void insn_lvalue(int opcode, const char* label, arith offset)
|
|
|
|
{
|
|
|
|
switch (opcode)
|
|
|
|
{
|
|
|
|
case op_lae:
|
|
|
|
push(
|
2016-09-19 22:19:39 +00:00
|
|
|
address_of_external(label, offset)
|
2016-09-19 21:06:59 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case op_loe:
|
|
|
|
push(
|
|
|
|
new_ir1(
|
|
|
|
IR_LOAD, EM_wordsize,
|
2016-09-19 22:19:39 +00:00
|
|
|
address_of_external(label, offset)
|
2016-09-19 21:06:59 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case op_ste:
|
|
|
|
appendir(
|
|
|
|
new_ir2(
|
|
|
|
IR_STORE, EM_wordsize,
|
2016-09-19 22:19:39 +00:00
|
|
|
address_of_external(label, offset),
|
2016-09-19 21:06:59 +00:00
|
|
|
pop(EM_wordsize)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case op_cal:
|
|
|
|
assert(offset == 0);
|
|
|
|
materialise_stack();
|
|
|
|
appendir(
|
|
|
|
new_ir1(
|
|
|
|
IR_CALL, 0,
|
|
|
|
new_labelir(label)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
2016-09-19 22:19:39 +00:00
|
|
|
|
|
|
|
case op_bra:
|
|
|
|
assert(offset == 0);
|
|
|
|
materialise_stack();
|
|
|
|
appendir(
|
|
|
|
new_ir1(
|
|
|
|
IR_JUMP, 0,
|
|
|
|
new_labelir(label)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
2016-09-19 21:06:59 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
fatal("treebuilder: unknown lvalue instruction '%s'",
|
|
|
|
em_mnem[opcode - sp_fmnem]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void generate_tree(struct basicblock* bb)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("; BLOCK %s\n", bb->name);
|
2016-09-19 22:19:39 +00:00
|
|
|
if (bb->inblocks_count > 0)
|
|
|
|
{
|
|
|
|
printf("; Entered from:\n");
|
|
|
|
for (i=0; i<bb->inblocks_count; i++)
|
|
|
|
printf("; %s\n", bb->inblocks[i]->name);
|
|
|
|
}
|
|
|
|
|
2016-09-19 21:06:59 +00:00
|
|
|
current_bb = bb;
|
|
|
|
reset_stack();
|
|
|
|
|
|
|
|
for (i=0; i<bb->insns_count; i++)
|
|
|
|
{
|
|
|
|
struct insn* insn = bb->insns[i];
|
|
|
|
printf("\t; EM: %s ", em_mnem[insn->opcode - sp_fmnem]);
|
|
|
|
switch (insn->paramtype)
|
|
|
|
{
|
|
|
|
case PARAM_NONE:
|
|
|
|
printf("\n");
|
|
|
|
insn_simple(insn->opcode);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PARAM_IVALUE:
|
|
|
|
printf("value=%d\n", insn->u.ivalue);
|
|
|
|
insn_ivalue(insn->opcode, insn->u.ivalue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PARAM_LVALUE:
|
|
|
|
printf("label=%s offset=%d\n",
|
|
|
|
insn->u.lvalue.label, insn->u.lvalue.offset);
|
|
|
|
insn_lvalue(insn->opcode, insn->u.lvalue.label, insn->u.lvalue.offset);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PARAM_BVALUE:
|
|
|
|
printf("true=%s", insn->u.bvalue.left->name);
|
|
|
|
if (insn->u.bvalue.right)
|
|
|
|
printf(" false=%s", insn->u.bvalue.right->name);
|
|
|
|
printf("\n");
|
|
|
|
insn_bvalue(insn->opcode, insn->u.bvalue.left, insn->u.bvalue.right);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
print_stack();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(stackptr == 0);
|
2016-09-19 22:19:39 +00:00
|
|
|
|
|
|
|
if (bb->outblocks_count > 0)
|
|
|
|
{
|
|
|
|
printf("; Exiting to:\n");
|
|
|
|
for (i=0; i<bb->outblocks_count; i++)
|
|
|
|
printf("; %s\n", bb->outblocks[i]->name);
|
|
|
|
}
|
|
|
|
printf("\n");
|
2016-09-19 21:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tb_procedure(struct procedure* current_proc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<current_proc->blocks_count; i++)
|
|
|
|
generate_tree(current_proc->blocks[i]);
|
|
|
|
}
|
|
|
|
|
2016-09-17 22:02:16 +00:00
|
|
|
/* vim: set sw=4 ts=4 expandtab : */
|
|
|
|
|