This commit is contained in:
ridethepig 2023-06-05 18:33:50 +08:00
parent 8d23769410
commit 571ab10f62
3 changed files with 74 additions and 4 deletions

View File

@ -182,6 +182,8 @@ public:
class MBasicBlock {
public:
int id;
std::list<sptr(MBasicBlock)>::iterator itr;
sptr(BasicBlock) ir_bb;
sptr(MFunction) parent_func;
std::list<sptr(MInst)> inst_list;

View File

@ -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;
}
}
}
}
}
}

View File

@ -93,6 +93,7 @@ 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);
}
@ -107,7 +108,8 @@ void MCModule::IR2MC(const Module &ir_module) {
std::unordered_map<sptr(BasicBlock), sptr(MBasicBlock)> bb_ir2mc;
for (auto bb : func->bb_list) {
auto mc_bb = make_shared<MBasicBlock>();
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});