can rounghly codegen
This commit is contained in:
@ -46,10 +46,11 @@ int main(int argc, char **argv) {
|
||||
GenerateNaiveASM(fout, IR);
|
||||
} else {
|
||||
auto IR_with_out_allocas = Mem2Reg(IR);
|
||||
IR_with_out_allocas->RecursivePrint(fout);
|
||||
// IR_with_out_allocas->RecursivePrint(fout);
|
||||
auto IR_with_out_phis = PhiEliminate(IR_with_out_allocas);
|
||||
// IR_with_out_phis->RecursivePrint(fout);
|
||||
auto alloced_code = RegAlloc(IR_with_out_phis);
|
||||
OptBackend::GenerateOptASM(fout, alloced_code);
|
||||
}
|
||||
} catch (const SemanticError &err) {
|
||||
std::cout << err.what() << std::endl;
|
||||
|
95
src/opt/optbackend.cpp
Normal file
95
src/opt/optbackend.cpp
Normal file
@ -0,0 +1,95 @@
|
||||
#include "opt/gen.h"
|
||||
namespace OptBackend {
|
||||
std::string cur_block_label_for_phi;
|
||||
void GenerateOptASM(std::ostream &os, std::shared_ptr<ModuleItem> prog) {
|
||||
auto riscv = std::make_shared<RISCVProgItem>();
|
||||
|
||||
for (auto conststr : prog->const_strs) {
|
||||
auto asm_item = std::make_shared<RISCVConstStrItem>();
|
||||
riscv->const_strs.push_back(asm_item);
|
||||
asm_item->content = conststr->string_raw;
|
||||
asm_item->full_label = ".str." + std::to_string(conststr->const_str_id);
|
||||
}
|
||||
for (auto global_var : prog->global_var_defs) {
|
||||
auto asm_item = std::make_shared<RISCVGlobalVarItem>();
|
||||
riscv->global_vars.push_back(asm_item);
|
||||
asm_item->full_label = ".var.global." + global_var->name_raw + ".addrkp";
|
||||
}
|
||||
std::unordered_map<std::string, FuncLayout> func_layouts;
|
||||
for (auto func_def : prog->function_defs) {
|
||||
// if (func_def->init_block) {
|
||||
// for (auto act : func_def->init_block->actions) {
|
||||
// ScanForVar(func_layouts[func_def->func_name_raw], act, prog->low_level_class_info);
|
||||
// }
|
||||
// }
|
||||
// for (auto block : func_def->basic_blocks) {
|
||||
// for (auto act : block->actions) {
|
||||
// ScanForVar(func_layouts[func_def->func_name_raw], act, prog->low_level_class_info);
|
||||
// }
|
||||
// }
|
||||
FuncLayout &layout = func_layouts[func_def->func_name_raw];
|
||||
// for (size_t i = 0; i < func_def->args_full_name.size(); i++) {
|
||||
// layout.arg_offset[func_def->args_full_name[i]] = i;
|
||||
// }
|
||||
for (size_t i = 0; i < func_def->spilled_vars; i++) {
|
||||
layout.AllocateItem("#" + std::to_string(i), 4, 1);
|
||||
}
|
||||
// debug:
|
||||
|
||||
// std::cerr << "layout info of function " << func_def->func_name_raw << std::endl;
|
||||
// std::cerr << "\tcur_pos=" << layout.cur_pos << std::endl;
|
||||
// std::cerr << "\ttotal_frame_size=" << layout.total_frame_size << std::endl;
|
||||
// for (const auto &item : layout.local_items) {
|
||||
// std::cerr << "\t" << item.first << " " << item.second << std::endl;
|
||||
// }
|
||||
}
|
||||
|
||||
for (auto func_def : prog->function_defs) {
|
||||
std::cerr << "generating asm for function " << func_def->func_name_raw << std::endl;
|
||||
auto func_asm = std::make_shared<RISCVFuncItem>();
|
||||
riscv->funcs.push_back(func_asm);
|
||||
func_asm->full_label = func_def->func_name_raw;
|
||||
FuncLayout &layout = func_layouts[func_def->func_name_raw];
|
||||
if (layout.total_frame_size < 2048) {
|
||||
func_asm->code_lines.push_back("addi sp, sp, -" + std::to_string(layout.total_frame_size));
|
||||
func_asm->code_lines.push_back("sw ra, " + std::to_string(layout.total_frame_size - 4) + "(sp)");
|
||||
func_asm->code_lines.push_back("sw s0, " + std::to_string(layout.total_frame_size - 8) + "(sp)");
|
||||
func_asm->code_lines.push_back("addi s0, sp, " + std::to_string(layout.total_frame_size));
|
||||
func_asm->code_lines.push_back("sw s0, " + std::to_string(layout.total_frame_size - 12) + "(sp)");
|
||||
} else {
|
||||
func_asm->code_lines.push_back("li x31, " + std::to_string(layout.total_frame_size));
|
||||
func_asm->code_lines.push_back("sub sp, sp, x31");
|
||||
func_asm->code_lines.push_back("add x31, x31, sp");
|
||||
func_asm->code_lines.push_back("sw ra, -4(x31)");
|
||||
func_asm->code_lines.push_back("sw s0, -8(x31)");
|
||||
func_asm->code_lines.push_back("sw x31, -12(x31)");
|
||||
func_asm->code_lines.push_back("mv s0, t0");
|
||||
}
|
||||
if (func_def->init_block) {
|
||||
func_asm->code_lines.push_back(".entrylabel." + func_def->init_block->label_full + ":");
|
||||
for (auto act : func_def->init_block->actions) {
|
||||
OptBackend::GenerateASM(act, func_asm->code_lines, func_layouts[func_def->func_name_raw],
|
||||
prog->low_level_class_info);
|
||||
}
|
||||
if (func_def->init_block->exit_action->corresponding_phi) {
|
||||
OptBackend::cur_block_label_for_phi = func_def->init_block->label_full;
|
||||
OptBackend::GenerateASM(func_def->init_block->exit_action->corresponding_phi, func_asm->code_lines,
|
||||
func_layouts[func_def->func_name_raw], prog->low_level_class_info);
|
||||
}
|
||||
OptBackend::GenerateASM(func_def->init_block->exit_action, func_asm->code_lines,
|
||||
func_layouts[func_def->func_name_raw], prog->low_level_class_info);
|
||||
}
|
||||
for (auto block : func_def->basic_blocks) {
|
||||
func_asm->code_lines.push_back(".entrylabel." + block->label_full + ":");
|
||||
for (auto act : block->actions) {
|
||||
OptBackend::GenerateASM(act, func_asm->code_lines, func_layouts[func_def->func_name_raw],
|
||||
prog->low_level_class_info);
|
||||
}
|
||||
OptBackend::GenerateASM(block->exit_action, func_asm->code_lines, func_layouts[func_def->func_name_raw],
|
||||
prog->low_level_class_info);
|
||||
}
|
||||
}
|
||||
|
||||
riscv->RecursivePrint(os);
|
||||
}
|
||||
} // namespace OptBackend
|
@ -154,12 +154,12 @@ void TranslateColorResult(std::shared_ptr<FunctionDefItem> func, CFGType &cfg, C
|
||||
var = "$reg." + std::to_string(confnode->color);
|
||||
}
|
||||
};
|
||||
func->spilled_vars = 0;
|
||||
for (auto node : cfg.nodes) {
|
||||
auto block = node->corresponding_block;
|
||||
std::vector<size_t> cur_node_use;
|
||||
std::vector<size_t> cur_node_def;
|
||||
bool use_def_init = false;
|
||||
func->spilled_vars = 0;
|
||||
for (auto act : block->actions) {
|
||||
if (auto br_act = std::dynamic_pointer_cast<BRAction>(act)) {
|
||||
if (var_to_id.find(br_act->cond) != var_to_id.end()) {
|
||||
@ -284,11 +284,12 @@ void TranslateColorResult(std::shared_ptr<FunctionDefItem> func, CFGType &cfg, C
|
||||
if (move_act->src_full == move_act->dest_full) {
|
||||
need_remove = true;
|
||||
}
|
||||
} else if (auto force_def_act = std::dynamic_pointer_cast<ForceDef>(*act_it)) {
|
||||
need_remove = true;
|
||||
} else if (auto force_use_act = std::dynamic_pointer_cast<ForceUse>(*act_it)) {
|
||||
need_remove = true;
|
||||
}
|
||||
// else if (auto force_def_act = std::dynamic_pointer_cast<ForceDef>(*act_it)) {
|
||||
// need_remove = true;
|
||||
// } else if (auto force_use_act = std::dynamic_pointer_cast<ForceUse>(*act_it)) {
|
||||
// need_remove = true;
|
||||
// }
|
||||
if (need_remove) {
|
||||
auto it_next = act_it;
|
||||
++it_next;
|
||||
@ -327,6 +328,22 @@ void PairMoveEliminate(std::shared_ptr<FunctionDefItem> func, CFGType &cfg, Conf
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RemoveCallingConventionKeeper(std::shared_ptr<FunctionDefItem> func, CFGType &cfg, ConfGraph &confgraph) {
|
||||
for (auto node : cfg.nodes) {
|
||||
auto block = node->corresponding_block;
|
||||
std::vector<std::list<std::shared_ptr<ActionItem>>::iterator> act_to_move;
|
||||
for (auto it = block->actions.begin(); it != block->actions.end(); ++it) {
|
||||
if (std::dynamic_pointer_cast<opt::ForceDef>(*it) || std::dynamic_pointer_cast<opt::ForceUse>(*it)) {
|
||||
act_to_move.push_back(it);
|
||||
}
|
||||
}
|
||||
for (auto it : act_to_move) {
|
||||
block->actions.erase(it);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ConductRegAllocForFunction(std::shared_ptr<FunctionDefItem> func) {
|
||||
std::cerr << "processing function " << func->func_name_raw << std::endl;
|
||||
CFGType cfg;
|
||||
@ -342,7 +359,7 @@ void ConductRegAllocForFunction(std::shared_ptr<FunctionDefItem> func) {
|
||||
confgraph = BuildConfGraph(cfg);
|
||||
} while (ConductColoring(func, cfg, confgraph));
|
||||
TranslateColorResult(func, cfg, confgraph);
|
||||
// PairMoveEliminate(func, cfg, confgraph);
|
||||
RemoveCallingConventionKeeper(func, cfg, confgraph);
|
||||
func->RecursivePrint(std::cerr);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user