diff --git a/include/mc_inst.h b/include/mc_inst.h index 8dce4aa..b1b2b03 100644 --- a/include/mc_inst.h +++ b/include/mc_inst.h @@ -182,6 +182,8 @@ public: class MBasicBlock { public: + int id; + std::list::iterator itr; sptr(BasicBlock) ir_bb; sptr(MFunction) parent_func; std::list inst_list; diff --git a/src/mc_asmgen.cpp b/src/mc_asmgen.cpp index 8387a69..1c7b370 100644 --- a/src/mc_asmgen.cpp +++ b/src/mc_asmgen.cpp @@ -3,13 +3,79 @@ namespace CompSysY { +using std::endl; + +static void bb_debug(std::ostream &ostr, sptr(MBasicBlock) bb) { + ostr << "pred:"; + for (auto item : bb->pred_list) { + ostr << "L_BB_" << item->id << ","; + } + ostr << "\n"; + ostr << "succ:"; + for (auto item : bb->succ_list) { + ostr << "L_BB_" << item->id << ","; + } + ostr << "\n"; + ostr << "livein:"; + for (auto item : bb->livein) { + ostr << item.to_string() << ","; + } + ostr << "\n"; + ostr << "liveout:"; + for (auto item : bb->liveout) { + ostr << item.to_string() << ","; + } + ostr << "\n"; +} void MCModule::MC2ASM(std::ostream &ostr) { // header, specifying align and arch ostr << "\t.text\n" << "\t.attribute\t4,\t16\n" << "\t.attribute\t5\t\"rv64i2p0_m2p0_f2p0_d2p0\"\n"; - ostr << "\t.file\t\"" << this->file_name << "\"\n" << std::endl; - + ostr << "\t.file\t\"" << this->file_name << "\"\n" << endl; + for (auto func : this->function_list) { + // function header + ostr << "\n\n"; + ostr << ".global\t" << func->ir_func->name << endl; + ostr << "\t.type\t" << func->ir_func->name << "\tSTT_FUNC" << endl; + ostr << func->ir_func->name << ":" << endl; + //TODO entry code + for (auto bb : func->bb_list) { + ostr << "L_BB_" << bb->id << ":" << endl; + ostr << "/*\n"; + bb_debug(ostr, bb); + ostr << "\n*/" << endl; + for (auto inst : bb->inst_list) { + switch (inst->inst_tag) { + case MInstTag::Add: break; + case MInstTag::Sub: break; + case MInstTag::Mul: break; + case MInstTag::Div: break; + case MInstTag::Mod: break; + case MInstTag::Lt: break; + case MInstTag::Le: break; + case MInstTag::Ge: break; + case MInstTag::Gt: break; + case MInstTag::Eq: break; + case MInstTag::Ne: break; + case MInstTag::And: break; + case MInstTag::Or: break; + case MInstTag::Lsh: break; + case MInstTag::Rsh: break; + case MInstTag::Move: break; + case MInstTag::Branch: break; + case MInstTag::Jmp: break; + case MInstTag::Ret: break; + case MInstTag::Load: break; + case MInstTag::Store: break; + case MInstTag::Compare: break; + case MInstTag::Call: break; + case MInstTag::Globsym: break; + case MInstTag::Comment: break; + } + } + } + } } } \ No newline at end of file diff --git a/src/mc_codegen.cpp b/src/mc_codegen.cpp index c9f9de0..a6f9be2 100644 --- a/src/mc_codegen.cpp +++ b/src/mc_codegen.cpp @@ -93,10 +93,11 @@ static MOperand value2moperand( void MCModule::IR2MC(const Module &ir_module) { // Simply copy globals, since they don't need any translation + int bb_id_cnt = 0; for (auto glob : ir_module.global_var_list) { this->global_list.push_back(glob); } - + for (auto func : ir_module.function_list) { if (func->is_libfunc()) continue; auto mc_func = make_shared(); @@ -107,7 +108,8 @@ void MCModule::IR2MC(const Module &ir_module) { std::unordered_map bb_ir2mc; for (auto bb : func->bb_list) { auto mc_bb = make_shared(); - mc_func->bb_list.push_back(mc_bb); + mc_bb->id = bb_id_cnt ++; + mc_bb->itr = mc_func->bb_list.insert(mc_func->bb_list.end(), mc_bb); mc_bb->ir_bb = bb; mc_bb->parent_func = mc_func; bb_ir2mc.insert({bb, mc_bb});