2016-09-18 21:24:54 +00:00
|
|
|
#include "mcg.h"
|
|
|
|
|
|
|
|
static int next_id = 0;
|
|
|
|
|
|
|
|
struct ir* new_ir0(int opcode, int size)
|
|
|
|
{
|
|
|
|
struct ir* ir = calloc(sizeof(struct ir), 1);
|
|
|
|
ir->id = next_id++;
|
|
|
|
ir->opcode = opcode;
|
|
|
|
ir->size = size;
|
|
|
|
return ir;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ir* new_ir1(int opcode, int size,
|
2016-09-19 21:06:59 +00:00
|
|
|
struct ir* left)
|
2016-09-18 21:24:54 +00:00
|
|
|
{
|
|
|
|
struct ir* ir = new_ir0(opcode, size);
|
2016-09-19 21:06:59 +00:00
|
|
|
ir->left = left;
|
2016-09-18 21:24:54 +00:00
|
|
|
return ir;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ir* new_ir2(int opcode, int size,
|
2016-09-19 21:06:59 +00:00
|
|
|
struct ir* left, struct ir* right)
|
2016-09-18 21:24:54 +00:00
|
|
|
{
|
|
|
|
struct ir* ir = new_ir0(opcode, size);
|
2016-09-19 21:06:59 +00:00
|
|
|
ir->left = left;
|
|
|
|
ir->right = right;
|
2016-09-18 21:24:54 +00:00
|
|
|
return ir;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ir* new_labelir(const char* label)
|
|
|
|
{
|
|
|
|
struct ir* ir = new_ir0(IR_LABEL, EM_pointersize);
|
|
|
|
ir->u.lvalue = label;
|
|
|
|
return ir;
|
|
|
|
}
|
|
|
|
|
2016-09-21 22:15:48 +00:00
|
|
|
struct ir* new_constir(int size, arith value)
|
2016-09-18 21:24:54 +00:00
|
|
|
{
|
2016-09-21 22:15:48 +00:00
|
|
|
struct ir* ir = new_ir0(IR_CONST, size);
|
2016-09-18 21:24:54 +00:00
|
|
|
ir->u.ivalue = value;
|
|
|
|
return ir;
|
|
|
|
}
|
|
|
|
|
2016-09-21 22:15:48 +00:00
|
|
|
struct ir* new_wordir(arith value)
|
2016-09-18 21:24:54 +00:00
|
|
|
{
|
2016-09-21 22:15:48 +00:00
|
|
|
return new_constir(EM_wordsize, value);
|
2016-09-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ir* new_bbir(struct basicblock* bb)
|
|
|
|
{
|
|
|
|
struct ir* ir = new_ir0(IR_BLOCK, EM_pointersize);
|
|
|
|
ir->u.bvalue = bb;
|
|
|
|
return ir;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ir* new_anyir(int size)
|
|
|
|
{
|
|
|
|
return new_ir0(IR_ANY, size);
|
|
|
|
}
|
|
|
|
|
2016-09-21 22:15:48 +00:00
|
|
|
struct ir* new_localir(int offset)
|
2016-09-18 21:24:54 +00:00
|
|
|
{
|
2016-09-21 22:15:48 +00:00
|
|
|
struct ir* ir = new_ir0(IR_LOCAL, EM_pointersize);
|
|
|
|
ir->u.ivalue = offset;
|
|
|
|
return ir;
|
2016-09-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 22:15:48 +00:00
|
|
|
void ir_print(char k, const struct ir* ir)
|
2016-09-18 21:24:54 +00:00
|
|
|
{
|
2016-09-21 22:15:48 +00:00
|
|
|
if (ir->left && !ir->left->is_sequence)
|
|
|
|
ir_print(k, ir->left);
|
|
|
|
if (ir->right && !ir->right->is_sequence)
|
|
|
|
ir_print(k, ir->right);
|
|
|
|
|
|
|
|
tracef(k, "%c: %c ", k, ir->is_sequence ? 'S' : ' ');
|
|
|
|
tracef(k, "$%d = ", ir->id);
|
|
|
|
tracef(k, "%s", ir_names[ir->opcode]);
|
|
|
|
if (ir->size)
|
|
|
|
tracef(k, "%d", ir->size);
|
|
|
|
tracef(k, "(");
|
2016-09-18 21:24:54 +00:00
|
|
|
|
|
|
|
switch (ir->opcode)
|
|
|
|
{
|
2016-09-21 22:15:48 +00:00
|
|
|
case IR_CONST:
|
|
|
|
case IR_LOCAL:
|
|
|
|
tracef(k, "%d", ir->u.ivalue);
|
2016-09-18 21:24:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IR_LABEL:
|
2016-09-21 22:15:48 +00:00
|
|
|
tracef(k, "%s", ir->u.lvalue);
|
2016-09-18 21:24:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IR_BLOCK:
|
2016-09-21 22:15:48 +00:00
|
|
|
tracef(k, "%s", ir->u.bvalue->name);
|
2016-09-18 21:24:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-09-19 21:06:59 +00:00
|
|
|
if (ir->left)
|
2016-09-21 22:15:48 +00:00
|
|
|
tracef(k, "$%d", ir->left->id);
|
2016-09-19 21:06:59 +00:00
|
|
|
if (ir->right)
|
2016-09-21 22:15:48 +00:00
|
|
|
tracef(k, ", $%d", ir->right->id);
|
2016-09-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 22:15:48 +00:00
|
|
|
tracef(k, ")\n");
|
2016-09-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set sw=4 ts=4 expandtab : */
|