2016-09-24 20:46:08 +00:00
|
|
|
#include "mcg.h"
|
2016-09-25 21:29:59 +00:00
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
#define MAX_CHILDREN 10
|
|
|
|
|
|
|
|
struct insn
|
|
|
|
{
|
|
|
|
struct ir* ir;
|
|
|
|
struct hop* hop;
|
|
|
|
const struct burm_instruction_data* insndata;
|
|
|
|
int num_children;
|
|
|
|
struct insn* children[MAX_CHILDREN];
|
|
|
|
};
|
|
|
|
|
2016-09-26 22:19:45 +00:00
|
|
|
static struct basicblock* current_bb;
|
2016-09-27 21:38:47 +00:00
|
|
|
static struct hop* current_hop;
|
2016-10-01 20:58:29 +00:00
|
|
|
static struct ir* current_ir;
|
2016-10-03 22:16:06 +00:00
|
|
|
static struct insn* current_insn;
|
2016-09-27 21:38:47 +00:00
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
static void emit(struct insn* insn);
|
2016-09-24 20:46:08 +00:00
|
|
|
|
2016-10-03 18:52:36 +00:00
|
|
|
void burm_trace(struct burm_node* p, int ruleno, int cost, int bestcost) {
|
2016-09-25 15:14:54 +00:00
|
|
|
const struct burm_instruction_data* insndata = &burm_instruction_data[ruleno];
|
2016-09-25 21:29:59 +00:00
|
|
|
//tracef('I', "I: 0x%p matched %s with cost %d vs. %d\n", p,
|
|
|
|
// insndata->name, cost, bestcost);
|
2016-09-24 20:46:08 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 18:52:36 +00:00
|
|
|
void burm_panic_cannot_match(struct burm_node* node)
|
2016-09-24 20:46:08 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "could not find any patterns to match:\n");
|
2016-10-19 21:29:05 +00:00
|
|
|
ir_print('!', node->ir);
|
2016-09-24 20:46:08 +00:00
|
|
|
fprintf(stderr, "aborting!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-10-21 21:31:00 +00:00
|
|
|
static void emit_return_reg(int index)
|
2016-10-03 22:16:06 +00:00
|
|
|
{
|
2016-10-21 21:31:00 +00:00
|
|
|
hop_add_vreg_insel(current_hop, current_hop->output, index);
|
2016-10-03 22:16:06 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 20:56:25 +00:00
|
|
|
static struct vreg* find_vreg_of_child(int child)
|
2016-09-27 21:38:47 +00:00
|
|
|
{
|
2016-10-03 22:16:06 +00:00
|
|
|
struct insn* insn = current_insn->children[child];
|
2016-10-02 21:25:54 +00:00
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
if (insn->hop)
|
2016-10-05 20:56:25 +00:00
|
|
|
return insn->hop->output;
|
2016-10-03 22:16:06 +00:00
|
|
|
else
|
2016-10-05 20:56:25 +00:00
|
|
|
return insn->ir->result;
|
|
|
|
}
|
|
|
|
|
2016-10-21 21:31:00 +00:00
|
|
|
static void emit_reg(int child, int index)
|
2016-10-05 20:56:25 +00:00
|
|
|
{
|
|
|
|
struct vreg* vreg = find_vreg_of_child(child);
|
2016-10-03 22:16:06 +00:00
|
|
|
|
|
|
|
if (vreg)
|
2016-10-07 22:21:23 +00:00
|
|
|
{
|
2016-10-21 21:31:00 +00:00
|
|
|
hop_add_vreg_insel(current_hop, vreg, index);
|
2016-10-07 22:21:23 +00:00
|
|
|
array_appendu(&vreg->used, current_hop);
|
|
|
|
}
|
2016-09-27 21:38:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void emit_string(const char* data)
|
|
|
|
{
|
|
|
|
hop_add_string_insel(current_hop, data);
|
|
|
|
}
|
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
static void emit_fragment(int child)
|
2016-09-29 20:06:04 +00:00
|
|
|
{
|
2016-10-03 22:16:06 +00:00
|
|
|
emit(current_insn->children[child]);
|
2016-09-29 20:06:04 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
static void emit_value(int child)
|
2016-09-27 21:38:47 +00:00
|
|
|
{
|
2016-10-03 22:16:06 +00:00
|
|
|
hop_add_value_insel(current_hop, current_insn->children[child]->ir);
|
2016-09-27 21:38:47 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 21:25:54 +00:00
|
|
|
static void emit_eoi(void)
|
2016-09-27 21:38:47 +00:00
|
|
|
{
|
2016-10-02 21:25:54 +00:00
|
|
|
hop_add_eoi_insel(current_hop);
|
2016-09-27 21:38:47 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 13:09:34 +00:00
|
|
|
static struct constraint* get_constraint(struct vreg* vreg)
|
|
|
|
{
|
|
|
|
struct constraint* c = pmap_findleft(¤t_hop->constraints, vreg);
|
|
|
|
if (!c)
|
|
|
|
{
|
|
|
|
c = calloc(1, sizeof(*c));
|
|
|
|
pmap_put(¤t_hop->constraints, vreg, c);
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void constrain_input_reg(int child, uint32_t attr)
|
2016-10-05 20:56:25 +00:00
|
|
|
{
|
|
|
|
struct vreg* vreg = find_vreg_of_child(child);
|
2016-10-09 13:09:34 +00:00
|
|
|
struct constraint* c;
|
2016-10-05 20:56:25 +00:00
|
|
|
|
2016-10-25 21:04:20 +00:00
|
|
|
assert(vreg);
|
2016-10-09 13:09:34 +00:00
|
|
|
|
2016-10-25 21:04:20 +00:00
|
|
|
array_appendu(¤t_hop->ins, vreg);
|
2016-10-09 13:09:34 +00:00
|
|
|
get_constraint(vreg)->attrs = attr;
|
2016-10-05 20:56:25 +00:00
|
|
|
}
|
|
|
|
|
2016-10-29 18:22:44 +00:00
|
|
|
static void constrain_input_reg_preserved(int child)
|
|
|
|
{
|
|
|
|
struct vreg* vreg = find_vreg_of_child(child);
|
|
|
|
struct constraint* c;
|
|
|
|
|
2018-09-22 09:49:13 +00:00
|
|
|
if (!vreg)
|
|
|
|
fatal("child %d of instruction is not a register and cannot be constrained", child);
|
|
|
|
|
2016-10-29 18:22:44 +00:00
|
|
|
array_appendu(¤t_hop->throughs, vreg);
|
|
|
|
get_constraint(vreg)->preserved = true;
|
|
|
|
}
|
|
|
|
|
2016-10-15 20:53:56 +00:00
|
|
|
static uint32_t find_type_from_constraint(uint32_t attr)
|
|
|
|
{
|
|
|
|
/* Looks through the registers and finds a concrete register implementing
|
|
|
|
* that attribute, and returns the type. We assume that all registers
|
|
|
|
* implementing an attribute (which anyone is going to ask for, 'volatile'
|
|
|
|
* doesn't count) will have the same type. TODO: mcgg should check for
|
|
|
|
* this. */
|
|
|
|
|
|
|
|
const struct burm_register_data* brd = burm_register_data;
|
2016-10-21 21:31:00 +00:00
|
|
|
while (brd->id)
|
2016-10-15 20:53:56 +00:00
|
|
|
{
|
|
|
|
if (brd->attrs & attr)
|
2016-10-25 21:04:20 +00:00
|
|
|
{
|
|
|
|
const uint32_t type_attrs =
|
|
|
|
(burm_int_ATTR | burm_float_ATTR |
|
|
|
|
burm_long_ATTR | burm_double_ATTR);
|
|
|
|
|
|
|
|
if (brd->attrs & type_attrs)
|
|
|
|
return brd->attrs & type_attrs;
|
|
|
|
return attr;
|
|
|
|
}
|
2016-10-15 20:53:56 +00:00
|
|
|
brd++;
|
|
|
|
}
|
|
|
|
|
|
|
|
fatal("unable to find a register matching attribute 0x%x", attr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-09 13:09:34 +00:00
|
|
|
static void constrain_output_reg(uint32_t attr)
|
2016-10-05 20:56:25 +00:00
|
|
|
{
|
2016-10-08 10:15:21 +00:00
|
|
|
struct vreg* vreg = current_hop->output;
|
|
|
|
|
|
|
|
if (!vreg)
|
|
|
|
current_hop->output = vreg = new_vreg();
|
|
|
|
|
|
|
|
array_appendu(¤t_hop->outs, vreg);
|
|
|
|
vreg->defined = current_hop;
|
2016-10-15 20:53:56 +00:00
|
|
|
vreg->type = find_type_from_constraint(attr);
|
2016-10-09 13:09:34 +00:00
|
|
|
|
|
|
|
get_constraint(vreg)->attrs = attr;
|
2016-10-05 20:56:25 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 20:17:02 +00:00
|
|
|
static void constrain_output_reg_equal_to(int child)
|
|
|
|
{
|
|
|
|
struct vreg* vreg = find_vreg_of_child(child);
|
|
|
|
|
|
|
|
get_constraint(current_hop->output)->equals_to = vreg;
|
|
|
|
}
|
|
|
|
|
2016-09-27 21:38:47 +00:00
|
|
|
static const struct burm_emitter_data emitter_data =
|
|
|
|
{
|
|
|
|
&emit_string,
|
2016-09-29 20:06:04 +00:00
|
|
|
&emit_fragment,
|
2016-10-03 22:16:06 +00:00
|
|
|
&emit_return_reg,
|
2016-09-27 21:38:47 +00:00
|
|
|
&emit_reg,
|
|
|
|
&emit_value,
|
2016-10-02 21:25:54 +00:00
|
|
|
&emit_eoi,
|
2016-10-05 20:56:25 +00:00
|
|
|
&constrain_input_reg,
|
2016-10-29 18:22:44 +00:00
|
|
|
&constrain_input_reg_preserved,
|
2016-10-14 20:17:02 +00:00
|
|
|
&constrain_output_reg,
|
|
|
|
&constrain_output_reg_equal_to,
|
2016-09-27 21:38:47 +00:00
|
|
|
};
|
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
static void emit(struct insn* insn)
|
|
|
|
{
|
|
|
|
struct insn* old = current_insn;
|
|
|
|
current_insn = insn;
|
|
|
|
|
|
|
|
insn->insndata->emitter(&emitter_data);
|
2016-09-27 21:38:47 +00:00
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
current_insn = old;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct insn* walk_instructions(struct burm_node* node, int goal)
|
2016-09-25 20:17:14 +00:00
|
|
|
{
|
2016-10-03 22:16:06 +00:00
|
|
|
struct insn* insn = calloc(1, sizeof(*insn));
|
2016-09-25 20:17:14 +00:00
|
|
|
int i;
|
2016-10-03 22:16:06 +00:00
|
|
|
|
|
|
|
insn->ir = node->ir;
|
|
|
|
insn->num_children = 0;
|
|
|
|
|
|
|
|
if (goal)
|
2016-09-25 21:29:59 +00:00
|
|
|
{
|
2016-10-03 22:16:06 +00:00
|
|
|
int insn_no = burm_rule(node->state_label, goal);
|
|
|
|
const short* nts = burm_nts[insn_no];
|
|
|
|
struct burm_node* children[MAX_CHILDREN] = {0};
|
2016-09-25 21:29:59 +00:00
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
insn->insndata = &burm_instruction_data[insn_no];
|
2016-09-24 20:46:08 +00:00
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
burm_kids(node, insn_no, children);
|
2016-09-25 20:17:14 +00:00
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
i = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (!children[i])
|
|
|
|
break;
|
|
|
|
|
|
|
|
insn->children[i] = walk_instructions(children[i], nts[i]);
|
|
|
|
insn->num_children++;
|
|
|
|
i++;
|
|
|
|
}
|
2016-10-02 21:25:54 +00:00
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
tracef('I', "I: $%d goal %d %s selected %d: %s\n",
|
|
|
|
node->ir->id,
|
|
|
|
goal,
|
|
|
|
insn->insndata->is_fragment ? "fragment" : "instruction",
|
|
|
|
insn_no,
|
|
|
|
insn->insndata->name);
|
2016-09-27 21:38:47 +00:00
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
if (!insn->insndata->is_fragment)
|
|
|
|
{
|
2016-10-14 21:12:29 +00:00
|
|
|
insn->hop = current_hop = new_hop(current_bb, insn->ir);
|
2016-10-14 23:15:08 +00:00
|
|
|
current_hop->insndata = insn->insndata;
|
2016-10-14 21:12:29 +00:00
|
|
|
emit(insn);
|
2016-10-04 19:29:03 +00:00
|
|
|
|
2016-10-14 21:12:29 +00:00
|
|
|
if (!current_hop->output)
|
2016-10-04 19:29:03 +00:00
|
|
|
{
|
2016-10-14 21:12:29 +00:00
|
|
|
switch (node->label)
|
|
|
|
{
|
2016-10-23 19:54:14 +00:00
|
|
|
case ir_to_esn(IR_REG, 0):
|
2016-10-14 21:12:29 +00:00
|
|
|
current_hop->output = node->ir->result;
|
2016-10-27 21:17:16 +00:00
|
|
|
assert(current_hop->output != NULL);
|
2016-10-14 21:12:29 +00:00
|
|
|
break;
|
|
|
|
|
2016-10-23 19:54:14 +00:00
|
|
|
case ir_to_esn(IR_NOP, 'I'):
|
|
|
|
case ir_to_esn(IR_NOP, 'F'):
|
|
|
|
case ir_to_esn(IR_NOP, 'L'):
|
|
|
|
case ir_to_esn(IR_NOP, 'D'):
|
2016-10-14 21:12:29 +00:00
|
|
|
current_hop->output = node->left->ir->result;
|
2016-10-27 21:17:16 +00:00
|
|
|
assert(current_hop->output != NULL);
|
2016-10-14 21:12:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-10-04 19:29:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
hop_print('I', current_hop);
|
2016-10-07 22:21:23 +00:00
|
|
|
array_append(¤t_bb->hops, current_hop);
|
2016-10-03 22:16:06 +00:00
|
|
|
|
2016-10-15 17:14:25 +00:00
|
|
|
if ((goal != burm_stmt_NT) && !insn->ir->result)
|
2016-10-03 22:16:06 +00:00
|
|
|
insn->ir->result = insn->hop->output;
|
|
|
|
}
|
2016-09-26 22:19:45 +00:00
|
|
|
}
|
2016-10-03 22:16:06 +00:00
|
|
|
|
|
|
|
return insn;
|
2016-09-24 20:46:08 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 18:52:36 +00:00
|
|
|
static struct burm_node* build_shadow_tree(struct ir* root, struct ir* ir)
|
|
|
|
{
|
|
|
|
struct burm_node* node = calloc(1, sizeof(*node));
|
|
|
|
node->ir = ir;
|
|
|
|
|
|
|
|
if (ir->root == root)
|
|
|
|
{
|
2016-10-23 19:54:14 +00:00
|
|
|
node->label = ir_to_esn(ir->opcode, ir->type);
|
2016-10-03 18:52:36 +00:00
|
|
|
|
|
|
|
if (ir->left)
|
|
|
|
node->left = build_shadow_tree(root, ir->left);
|
|
|
|
|
|
|
|
if (ir->right)
|
|
|
|
node->right = build_shadow_tree(root, ir->right);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
node->label = ir_to_esn(IR_REG, 0);
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2016-09-26 22:19:45 +00:00
|
|
|
static void select_instructions(void)
|
2016-09-24 20:46:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-09-26 22:19:45 +00:00
|
|
|
tracef('I', "I: BLOCK: %s\n", current_bb->name);
|
2016-09-24 20:46:08 +00:00
|
|
|
|
2016-09-26 22:19:45 +00:00
|
|
|
for (i=0; i<current_bb->irs.count; i++)
|
2016-09-24 20:46:08 +00:00
|
|
|
{
|
2016-10-03 18:52:36 +00:00
|
|
|
struct burm_node* shadow;
|
2016-09-24 20:46:08 +00:00
|
|
|
int insnno;
|
2016-10-03 18:52:36 +00:00
|
|
|
|
2016-10-01 20:58:29 +00:00
|
|
|
current_ir = current_bb->irs.item[i];
|
2016-09-24 20:46:08 +00:00
|
|
|
|
2016-10-04 19:58:31 +00:00
|
|
|
if (current_ir->opcode == IR_PHI)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
|
|
|
|
current_ir->result = new_vreg();
|
2016-10-07 22:21:23 +00:00
|
|
|
tracef('I', "I: $%d is phi:", current_ir->result->id);
|
2016-10-04 19:58:31 +00:00
|
|
|
for (j=0; j<current_ir->u.phivalue.count; j++)
|
2016-10-07 22:21:23 +00:00
|
|
|
{
|
|
|
|
struct basicblock* parentbb = current_ir->u.phivalue.item[j].left;
|
|
|
|
struct ir* parentir = current_ir->u.phivalue.item[j].right;
|
|
|
|
struct phi* phi = calloc(1, sizeof(*phi));
|
|
|
|
tracef('I', " %s=>$%d", parentbb->name, parentir->id);
|
|
|
|
|
|
|
|
phi->prev = parentbb;
|
|
|
|
phi->ir = parentir;
|
|
|
|
pmap_add(¤t_bb->phis, current_ir->result, phi);
|
|
|
|
}
|
2016-10-04 19:58:31 +00:00
|
|
|
tracef('I', "\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-08 09:07:28 +00:00
|
|
|
ir_print('I', current_ir);
|
2016-10-04 19:58:31 +00:00
|
|
|
shadow = build_shadow_tree(current_ir, current_ir);
|
|
|
|
burm_label(shadow);
|
2016-09-24 20:46:08 +00:00
|
|
|
|
2016-10-04 19:58:31 +00:00
|
|
|
insnno = burm_rule(shadow->state_label, 1);
|
|
|
|
if (!insnno)
|
|
|
|
burm_panic_cannot_match(shadow);
|
|
|
|
|
2016-10-15 16:38:46 +00:00
|
|
|
walk_instructions(shadow, burm_stmt_NT);
|
2016-10-04 19:58:31 +00:00
|
|
|
}
|
2016-09-24 20:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-07 22:21:23 +00:00
|
|
|
void pass_instruction_selector(void)
|
2016-09-24 20:46:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-10-27 21:17:16 +00:00
|
|
|
for (i=0; i<dominance.preorder.count; i++)
|
2016-09-24 20:46:08 +00:00
|
|
|
{
|
2016-10-27 21:17:16 +00:00
|
|
|
current_bb = dominance.preorder.item[i];
|
2016-09-26 22:19:45 +00:00
|
|
|
select_instructions();
|
2016-09-24 20:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set sw=4 ts=4 expandtab : */
|
|
|
|
|