2016-09-24 20:46:08 +00:00
|
|
|
#include "mcg.h"
|
2016-09-25 21:29:59 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
static const struct burm_emitter_data emitter_data;
|
2016-09-24 20:46:08 +00:00
|
|
|
|
|
|
|
void burm_trace(struct ir* 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
|
|
|
}
|
|
|
|
|
|
|
|
void burm_panic_cannot_match(struct ir* ir)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "could not find any patterns to match:\n");
|
|
|
|
ir_print(0, ir);
|
|
|
|
fprintf(stderr, "aborting!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-09-27 21:38:47 +00:00
|
|
|
static void emit_reg(struct ir* ir)
|
|
|
|
{
|
|
|
|
if (ir->hop)
|
|
|
|
hop_add_reg_insel(current_hop, ir->hop);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const struct burm_instruction_data* insndata = &burm_instruction_data[ir->insn_no];
|
|
|
|
if (insndata->emitter)
|
|
|
|
insndata->emitter(ir, &emitter_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void emit_string(const char* data)
|
|
|
|
{
|
|
|
|
hop_add_string_insel(current_hop, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void emit_value(struct ir* ir)
|
|
|
|
{
|
|
|
|
hop_add_value_insel(current_hop, ir);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void emit_resultreg(void)
|
|
|
|
{
|
|
|
|
hop_add_reg_insel(current_hop, current_hop);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void emit_eoi(void)
|
|
|
|
{
|
|
|
|
hop_add_eoi_insel(current_hop);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct burm_emitter_data emitter_data =
|
|
|
|
{
|
|
|
|
&emit_string,
|
|
|
|
&emit_reg,
|
|
|
|
&emit_value,
|
|
|
|
&emit_resultreg,
|
|
|
|
&emit_eoi
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void walk_instructions(struct ir* ir, int goal)
|
2016-09-25 20:17:14 +00:00
|
|
|
{
|
|
|
|
struct ir* children[10];
|
|
|
|
int insn_no = burm_rule(ir->state_label, goal);
|
|
|
|
const struct burm_instruction_data* insndata = &burm_instruction_data[insn_no];
|
|
|
|
const short* nts = burm_nts[insn_no];
|
2016-09-27 21:38:47 +00:00
|
|
|
struct hop* parent_hop = NULL;
|
2016-09-25 20:17:14 +00:00
|
|
|
int i;
|
2016-09-26 22:19:45 +00:00
|
|
|
|
2016-09-25 20:17:14 +00:00
|
|
|
ir->insn_no = insn_no;
|
2016-09-26 22:19:45 +00:00
|
|
|
if (!insndata->is_fragment)
|
2016-09-25 21:29:59 +00:00
|
|
|
{
|
2016-09-27 21:38:47 +00:00
|
|
|
parent_hop = current_hop;
|
|
|
|
current_hop = ir->hop = new_hop(insn_no, ir);
|
2016-09-25 21:29:59 +00:00
|
|
|
}
|
|
|
|
|
2016-09-25 20:17:14 +00:00
|
|
|
burm_kids(ir, insn_no, children);
|
|
|
|
for (i=0; nts[i]; i++)
|
2016-09-27 21:38:47 +00:00
|
|
|
walk_instructions(children[i], nts[i]);
|
2016-09-24 20:46:08 +00:00
|
|
|
|
2016-09-26 22:19:45 +00:00
|
|
|
ir->is_generated = true;
|
2016-09-25 21:29:59 +00:00
|
|
|
|
2016-09-25 20:17:14 +00:00
|
|
|
tracef('I', "I: $%d %s selected %s %d: %s\n",
|
2016-09-25 15:14:54 +00:00
|
|
|
ir->id,
|
2016-09-25 20:17:14 +00:00
|
|
|
ir->is_sequence ? "S" : " ",
|
2016-09-25 15:14:54 +00:00
|
|
|
insndata->is_fragment ? "fragment" : "instruction",
|
2016-09-25 20:17:14 +00:00
|
|
|
insn_no,
|
2016-09-25 15:14:54 +00:00
|
|
|
insndata->name);
|
2016-09-25 20:17:14 +00:00
|
|
|
|
2016-09-26 22:19:45 +00:00
|
|
|
if (!insndata->is_fragment)
|
|
|
|
{
|
2016-09-27 21:38:47 +00:00
|
|
|
if (insndata->emitter)
|
|
|
|
insndata->emitter(ir, &emitter_data);
|
|
|
|
|
|
|
|
hop_print('I', current_hop);
|
|
|
|
array_append(¤t_bb->hops, current_hop);
|
|
|
|
current_hop = parent_hop;
|
2016-09-26 22:19:45 +00:00
|
|
|
}
|
2016-09-24 20:46:08 +00:00
|
|
|
}
|
|
|
|
|
2016-09-25 20:17:14 +00:00
|
|
|
|
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
|
|
|
{
|
|
|
|
int insnno;
|
2016-09-26 22:19:45 +00:00
|
|
|
struct ir* ir = current_bb->irs.item[i];
|
2016-09-24 20:46:08 +00:00
|
|
|
burm_label(ir);
|
|
|
|
|
|
|
|
insnno = burm_rule(ir->state_label, 1);
|
|
|
|
if (!insnno)
|
|
|
|
burm_panic_cannot_match(ir);
|
|
|
|
|
2016-09-26 22:19:45 +00:00
|
|
|
ir_print('I', ir);
|
2016-09-27 21:38:47 +00:00
|
|
|
walk_instructions(ir, 1);
|
2016-09-24 20:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pass_instruction_selector(struct procedure* proc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-09-26 20:48:58 +00:00
|
|
|
for (i=0; i<proc->blocks.count; i++)
|
2016-09-24 20:46:08 +00:00
|
|
|
{
|
2016-09-26 22:19:45 +00:00
|
|
|
current_bb = proc->blocks.item[i];
|
|
|
|
select_instructions();
|
2016-09-24 20:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set sw=4 ts=4 expandtab : */
|
|
|
|
|