diff --git a/include/naivebackend/build_layout.hpp b/include/naivebackend/build_layout.hpp index 2982fdc..6be2efd 100644 --- a/include/naivebackend/build_layout.hpp +++ b/include/naivebackend/build_layout.hpp @@ -32,7 +32,7 @@ inline void ScanForVar(FuncLayout &layout, std::shared_ptr action, throw std::runtime_error("GetElementPtrAction should have a result_full"); } if (get_element_act->indices.size() == 1) { - layout.AllocateItem(get_element_act->result_full, CalcSize(get_element_act->ty)); + layout.AllocateItem(get_element_act->result_full, 4); } else if (get_element_act->indices.size() == 2) { if (get_element_act->indices[0] != "0") throw std::runtime_error("GetElementPtrAction with non-zero base index is not supported"); diff --git a/include/naivebackend/codegen.hpp b/include/naivebackend/codegen.hpp index 0e63a57..574b878 100644 --- a/include/naivebackend/codegen.hpp +++ b/include/naivebackend/codegen.hpp @@ -88,7 +88,7 @@ inline void GenerateASM(std::shared_ptr act, std::vectorty); code_lines.push_back("slli t1, t1, " + std::to_string(std::countr_zero(element_sz))); code_lines.push_back("add t2, t0, t1"); - GenerateWriteAccess(get_element_act->result_full, element_sz, "t2", layout, code_lines); + GenerateWriteAccess(get_element_act->result_full, 4, "t2", layout, code_lines); } else if (get_element_act->indices.size() == 2) { // struct access if (get_element_act->indices[0] != "0") { @@ -138,6 +138,8 @@ inline void GenerateASM(std::shared_ptr act, std::vectorargs_ty.size() != call_act->args_val_full.size()) { throw std::runtime_error("args_ty and args_full_name should have the same size"); } + code_lines.push_back("addi sp, sp, -16"); + code_lines.push_back("sw a0, 0(sp)"); for (size_t i = 0; i < num_of_args && i < 8; i++) { IRVar2RISCVReg(call_act->args_val_full[i], CalcSize(call_act->args_ty[i]), "a" + std::to_string(i), layout, code_lines); @@ -160,6 +162,8 @@ inline void GenerateASM(std::shared_ptr act, std::vectorreturn_type); GenerateWriteAccess(call_act->result_full, ret_sz, "a0", layout, code_lines); } + code_lines.push_back("lw a0, 0(sp)"); + code_lines.push_back("addi sp, sp, 16"); } else if (auto phi_act = std::dynamic_pointer_cast(act)) { if (!process_phi) { return; // for efficiency, phi actions are implemented as store action in the previous block diff --git a/include/naivebackend/naivebackend.h b/include/naivebackend/naivebackend.h index d1a0f94..3723554 100644 --- a/include/naivebackend/naivebackend.h +++ b/include/naivebackend/naivebackend.h @@ -32,6 +32,8 @@ class RISCVConstStrItem : public RISCVAsmItemBase { os << "\\t"; } else if (c == '\"') { os << "\\\""; + } else if (c == '\\') { + os << "\\\\"; } else { os << c; } diff --git a/src/IR/IRBuilder.cpp b/src/IR/IRBuilder.cpp index b42c582..724c16c 100644 --- a/src/IR/IRBuilder.cpp +++ b/src/IR/IRBuilder.cpp @@ -47,11 +47,11 @@ void IRBuilder::ActuralVisit(FuncDef_ASTNode *node) { size_t block_id = block_counter++; current_block->label_full = "label_" + std::to_string(block_id); - if (node->func_name == "main") { + if (declare->func_name_raw == "main") { func_def->init_block = main_init_block; } else { func_def->init_block = std::make_shared(); - func_def->init_block->label_full = "label_init_" + node->func_name; + func_def->init_block->label_full = "label_init_" + declare->func_name_raw; for (auto &arg : node->params) { std::string &arg_name_raw = arg.first; std::string rvalue_full_name = "%.var.local." + std::to_string(scope_id) + "." + arg_name_raw + ".val";