2016-09-21 22:15:48 +00:00
|
|
|
#include "mcg.h"
|
|
|
|
|
2016-10-15 21:19:44 +00:00
|
|
|
struct procedure* current_proc;
|
2016-10-15 20:53:56 +00:00
|
|
|
|
2016-10-15 21:19:44 +00:00
|
|
|
static void print_blocks(char k)
|
2016-09-21 22:15:48 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-10-15 21:19:44 +00:00
|
|
|
tracef(k, "%c: procedure %s\n", k, current_proc->name);
|
2016-10-30 15:51:06 +00:00
|
|
|
for (i=0; i<current_proc->blocks.count; i++)
|
2016-09-21 22:15:48 +00:00
|
|
|
{
|
2016-10-15 21:19:44 +00:00
|
|
|
struct basicblock* bb = current_proc->blocks.item[i];
|
2016-09-21 22:15:48 +00:00
|
|
|
int j;
|
|
|
|
|
2016-09-22 21:19:29 +00:00
|
|
|
tracef(k, "%c:\n", k);
|
2016-09-23 23:04:00 +00:00
|
|
|
tracef(k, "%c: %sBLOCK: %s\n", k,
|
|
|
|
bb->is_fake ? "FAKE " : "",
|
|
|
|
bb->name);
|
2016-09-21 22:15:48 +00:00
|
|
|
|
2016-10-04 21:42:00 +00:00
|
|
|
if (bb->prevs.count > 0)
|
|
|
|
{
|
|
|
|
tracef(k, "%c: FROM:", k);
|
|
|
|
for (j=0; j<bb->prevs.count; j++)
|
|
|
|
tracef(k, " %s", bb->prevs.item[j]->name);
|
|
|
|
tracef(k, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bb->nexts.count > 0)
|
|
|
|
{
|
|
|
|
tracef(k, "%c: TO:", k);
|
|
|
|
for (j=0; j<bb->nexts.count; j++)
|
|
|
|
tracef(k, " %s", bb->nexts.item[j]->name);
|
|
|
|
tracef(k, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j=0; j<bb->irs.count; j++)
|
2016-09-26 20:48:58 +00:00
|
|
|
ir_print(k, bb->irs.item[j]);
|
2016-09-21 22:15:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 21:24:46 +00:00
|
|
|
static void print_vreg(char k, register_assignment_t* assignments, struct vreg* vreg)
|
|
|
|
{
|
|
|
|
struct hreg* hreg;
|
|
|
|
tracef(k, "%%%d", vreg->id);
|
|
|
|
if (assignments)
|
|
|
|
{
|
|
|
|
hreg = pmap_findright(assignments, vreg);
|
|
|
|
if (hreg)
|
|
|
|
tracef(k, "(%s)", hreg->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-15 21:19:44 +00:00
|
|
|
static void print_hops(char k)
|
2016-10-07 22:21:23 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-10-15 21:19:44 +00:00
|
|
|
tracef(k, "%c: procedure %s\n", k, current_proc->name);
|
2016-10-30 15:51:06 +00:00
|
|
|
for (i=0; i<dominance.preorder.count; i++)
|
2016-10-07 22:21:23 +00:00
|
|
|
{
|
|
|
|
struct basicblock* bb = dominance.preorder.item[i];
|
|
|
|
int j;
|
|
|
|
|
|
|
|
tracef(k, "%c:\n", k);
|
|
|
|
tracef(k, "%c: %sBLOCK: %s\n", k,
|
|
|
|
bb->is_fake ? "FAKE " : "",
|
|
|
|
bb->name);
|
|
|
|
|
|
|
|
if (bb->prevs.count > 0)
|
|
|
|
{
|
|
|
|
tracef(k, "%c: FROM:", k);
|
|
|
|
for (j=0; j<bb->prevs.count; j++)
|
|
|
|
tracef(k, " %s", bb->prevs.item[j]->name);
|
|
|
|
tracef(k, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bb->nexts.count > 0)
|
|
|
|
{
|
|
|
|
tracef(k, "%c: TO:", k);
|
|
|
|
for (j=0; j<bb->nexts.count; j++)
|
|
|
|
tracef(k, " %s", bb->nexts.item[j]->name);
|
|
|
|
tracef(k, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bb->liveins.count > 0)
|
|
|
|
{
|
|
|
|
tracef(k, "%c: INS:", k);
|
|
|
|
for (j=0; j<bb->liveins.count; j++)
|
2018-09-20 21:24:46 +00:00
|
|
|
{
|
|
|
|
tracef(k, " ");
|
|
|
|
print_vreg(k, &bb->regsin, bb->liveins.item[j]);
|
|
|
|
}
|
2016-10-07 22:21:23 +00:00
|
|
|
tracef(k, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bb->liveouts.count > 0)
|
|
|
|
{
|
|
|
|
tracef(k, "%c: OUTS:", k);
|
|
|
|
for (j=0; j<bb->liveouts.count; j++)
|
2018-09-20 21:24:46 +00:00
|
|
|
{
|
|
|
|
tracef(k, " ");
|
|
|
|
print_vreg(k, bb->regsout, bb->liveouts.item[j]);
|
|
|
|
}
|
2016-10-07 22:21:23 +00:00
|
|
|
tracef(k, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bb->phis.count > 0)
|
|
|
|
{
|
|
|
|
tracef(k, "%c: PHIS:", k);
|
|
|
|
for (j=0; j<bb->phis.count; j++)
|
|
|
|
{
|
|
|
|
struct vreg* vreg = bb->phis.item[j].left;
|
|
|
|
struct phi* phi = bb->phis.item[j].right;
|
|
|
|
|
2018-09-20 21:24:46 +00:00
|
|
|
tracef(k, " %%%d(via %s)=>",
|
2016-10-07 22:21:23 +00:00
|
|
|
phi->ir->result->id,
|
2018-09-20 21:24:46 +00:00
|
|
|
phi->prev->name);
|
|
|
|
print_vreg(k, &bb->regsin, vreg);
|
2016-10-07 22:21:23 +00:00
|
|
|
}
|
|
|
|
tracef(k, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j=0; j<bb->hops.count; j++)
|
|
|
|
hop_print(k, bb->hops.item[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-10 22:12:11 +00:00
|
|
|
static void emit_procedure(struct procedure* proc)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
2016-10-15 21:39:38 +00:00
|
|
|
fprintf(outputfile, "\n.sect .text\n");
|
2016-10-10 22:12:11 +00:00
|
|
|
for (i=0; i<dominance.preorder.count; i++)
|
|
|
|
{
|
|
|
|
struct basicblock* bb = dominance.preorder.item[i];
|
|
|
|
|
2016-10-27 21:17:16 +00:00
|
|
|
fprintf(outputfile, "%s:\n", platform_label(bb->name));
|
2016-10-10 22:12:11 +00:00
|
|
|
for (j=0; j<bb->hops.count; j++)
|
|
|
|
{
|
|
|
|
struct hop* hop = bb->hops.item[j];
|
|
|
|
fprintf(outputfile, "%s", hop_render(hop));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-07 22:21:23 +00:00
|
|
|
static void write_cfg_graph(const char* name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-10-27 21:17:16 +00:00
|
|
|
fprintf(cfg_dot_file, "subgraph \"%s\" {\n", name);
|
|
|
|
fprintf(cfg_dot_file, "\t\"%s\" [color=red];\n", cfg.entry->name);
|
2016-10-07 22:21:23 +00:00
|
|
|
|
|
|
|
for (i=0; i<cfg.graph.count; i++)
|
|
|
|
{
|
2016-10-27 21:17:16 +00:00
|
|
|
fprintf(cfg_dot_file, "\t\"%s\" -> \"%s\";\n",
|
2016-10-07 22:21:23 +00:00
|
|
|
cfg.graph.item[i].left->name,
|
|
|
|
cfg.graph.item[i].right->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(cfg_dot_file, "}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_dominance_graph(const char* name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-10-27 21:17:16 +00:00
|
|
|
fprintf(dominance_dot_file, "subgraph \"%s\" {\n", name);
|
|
|
|
fprintf(dominance_dot_file, "\t\"%s\" [color=green];\n", cfg.entry->name);
|
2016-10-07 22:21:23 +00:00
|
|
|
|
|
|
|
for (i=0; i<dominance.graph.count; i++)
|
|
|
|
{
|
2016-10-27 21:17:16 +00:00
|
|
|
fprintf(dominance_dot_file, "\t\"%s\" -> \"%s\";\n",
|
2016-10-07 22:21:23 +00:00
|
|
|
dominance.graph.item[i].right->name,
|
|
|
|
dominance.graph.item[i].left->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(dominance_dot_file, "}\n");
|
|
|
|
}
|
|
|
|
|
2016-09-23 23:04:00 +00:00
|
|
|
void procedure_compile(struct procedure* proc)
|
2016-09-21 22:15:48 +00:00
|
|
|
{
|
2016-10-15 21:19:44 +00:00
|
|
|
current_proc = proc;
|
|
|
|
|
|
|
|
pass_group_irs();
|
|
|
|
print_blocks('1');
|
2016-09-22 21:19:29 +00:00
|
|
|
|
2016-10-02 15:50:34 +00:00
|
|
|
/* Passes from here on must preserve IR grouping */
|
|
|
|
|
2016-10-15 21:19:44 +00:00
|
|
|
pass_eliminate_trivial_blocks();
|
|
|
|
pass_remove_dead_blocks();
|
2016-10-02 15:50:34 +00:00
|
|
|
|
2016-10-15 21:19:44 +00:00
|
|
|
print_blocks('2');
|
|
|
|
update_graph_data();
|
|
|
|
pass_split_critical_edges();
|
|
|
|
update_graph_data();
|
2016-10-05 21:55:38 +00:00
|
|
|
|
|
|
|
/* Passes from here on can't alter the BB graph without also updating prevs
|
2016-10-06 19:34:21 +00:00
|
|
|
* and nexts (and then calling update_graph_data()). */
|
2016-10-02 15:50:34 +00:00
|
|
|
|
2016-10-15 21:19:44 +00:00
|
|
|
print_blocks('3');
|
2016-10-29 09:57:56 +00:00
|
|
|
pass_wire_up_return_values();
|
2016-10-15 21:19:44 +00:00
|
|
|
pass_convert_stack_ops();
|
2018-09-22 09:19:00 +00:00
|
|
|
#if defined MCGG_OPTION_LOWER_PUSHES_TO_LOADS_AND_STORES
|
|
|
|
pass_lower_pushes();
|
|
|
|
#endif
|
2016-10-15 21:19:44 +00:00
|
|
|
print_blocks('4');
|
|
|
|
pass_convert_locals_to_ssa();
|
|
|
|
print_blocks('5');
|
2016-10-12 19:50:12 +00:00
|
|
|
pass_remove_dead_phis();
|
2016-10-22 21:04:13 +00:00
|
|
|
pass_infer_types();
|
2016-10-15 21:19:44 +00:00
|
|
|
print_blocks('6');
|
2016-09-22 21:19:29 +00:00
|
|
|
|
2016-10-07 22:21:23 +00:00
|
|
|
pass_instruction_selector();
|
2016-10-15 21:19:44 +00:00
|
|
|
print_hops('7');
|
2016-10-09 20:04:20 +00:00
|
|
|
pass_find_phi_congruence_groups();
|
2016-10-08 21:32:54 +00:00
|
|
|
pass_live_vreg_analysis();
|
2016-10-15 21:19:44 +00:00
|
|
|
print_hops('8');
|
|
|
|
pass_register_allocator();
|
|
|
|
pass_add_prologue_epilogue();
|
|
|
|
print_hops('9');
|
2016-10-06 19:34:21 +00:00
|
|
|
|
2016-10-10 22:12:11 +00:00
|
|
|
emit_procedure(proc);
|
|
|
|
|
2016-10-07 22:21:23 +00:00
|
|
|
if (cfg_dot_file)
|
|
|
|
write_cfg_graph(proc->name);
|
|
|
|
if (dominance_dot_file)
|
|
|
|
write_dominance_graph(proc->name);
|
2016-09-21 22:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set sw=4 ts=4 expandtab : */
|
|
|
|
|