From c893a666d247f6481f8509233f6dfd817371e088 Mon Sep 17 00:00:00 2001 From: wqz <1197460504@qq.com> Date: Sun, 14 May 2023 17:12:21 +0800 Subject: [PATCH] =?UTF-8?q?alloca=E6=8C=87=E4=BB=A4=E5=92=8Cstore=E6=8C=87?= =?UTF-8?q?=E4=BB=A4=EF=BC=8C=E5=AE=9E=E7=8E=B0=E4=BA=86=E5=A4=A7=E6=A6=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/codegen.cpp | 27 +++++++++++++++++++++------ src/backend/codegen.hpp | 1 + 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/backend/codegen.cpp b/src/backend/codegen.cpp index 4e935e5..ecb77f1 100644 --- a/src/backend/codegen.cpp +++ b/src/backend/codegen.cpp @@ -105,6 +105,7 @@ namespace backend code += space + "push\t{fp,lr}" + endl; // set fp code += space + "add\tfp, sp, #4" + endl; + // top_offset = -8; } else { @@ -112,6 +113,7 @@ namespace backend code += space + "push\t{fp}" + endl; // set fp code += space + "add\tfp, sp, #0" + endl; + // top_offset = -4; } return code; } @@ -125,6 +127,7 @@ namespace backend string CodeGen::epilogueCode_gen(Function *func) { string code; + // if there is callinst in function bool haveCall = false; auto bbs = func->getBasicBlocks(); for (auto iter = bbs.begin(); iter != bbs.end(); ++iter) @@ -154,6 +157,7 @@ namespace backend clearFunctionRecord(func); string bbCode; auto bbs = func->getBasicBlocks(); + top_offset = -8; for (auto iter = bbs.begin(); iter != bbs.end(); ++iter) { auto bb = iter->get(); @@ -208,18 +212,29 @@ namespace backend CodeGen::allocaInst_gen(AllocaInst *aInst, RegManager::RegId dstRegId) { string code; - /** - *code in here - */ + localVarStOffset.emplace(aInst, top_offset); + top_offset -= 4; + return {dstRegId, code}; } string CodeGen::storeInst_gen(StoreInst *stInst) { string code; - /** - *code in here - */ + auto value = dyncast(stInst->getValue()); + auto pointer = dynamic_cast(stInst->getPointer()); + int offset = localVarStOffset[pointer]; + std::stringstream ss1; + std::stringstream ss2; + if (value) + { + int constant_value = value->getInt(); + ss1 << constant_value; + code += space + "mov\tr3, #" + ss1.str() + endl; + } + ss2 << offset; + code += space + "str\tr3, [fp, #" + ss2.str() + "]" + endl; + return code; } pair diff --git a/src/backend/codegen.hpp b/src/backend/codegen.hpp index e2b43e3..837840c 100644 --- a/src/backend/codegen.hpp +++ b/src/backend/codegen.hpp @@ -182,6 +182,7 @@ namespace backend // label manager map bb_labels; uint64_t label_no = 0; + int top_offset = 0; public: CodeGen(Module *module) : module(module) {}