From 69e443ae9e369bc120dbb356c8517b9c392d8843 Mon Sep 17 00:00:00 2001 From: wqz <1197460504@qq.com> Date: Wed, 10 May 2023 13:39:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E5=A4=B4=E9=83=A8=E5=92=8C=E5=B0=BE=E9=83=A8=E7=9A=84=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/codegen.cpp | 60 +++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/src/backend/codegen.cpp b/src/backend/codegen.cpp index 5bf4295..4e935e5 100644 --- a/src/backend/codegen.cpp +++ b/src/backend/codegen.cpp @@ -83,9 +83,36 @@ namespace backend string CodeGen::prologueCode_gen(Function *func) { string code; - /** - *code in here - */ + // if there is callinst in function + bool haveCall = false; + auto bbs = func->getBasicBlocks(); + for (auto iter = bbs.begin(); iter != bbs.end(); ++iter) + { + auto bb = iter->get(); + for (auto &instr : bb->getInstructions()) + { + auto instrType = instr->getKind(); + if (instrType == Value::Kind::kCall) + { + haveCall = true; + break; + } + } + } + if (haveCall) + { + // push fp,lr + code += space + "push\t{fp,lr}" + endl; + // set fp + code += space + "add\tfp, sp, #4" + endl; + } + else + { + // push fp + code += space + "push\t{fp}" + endl; + // set fp + code += space + "add\tfp, sp, #0" + endl; + } return code; } @@ -98,9 +125,26 @@ namespace backend string CodeGen::epilogueCode_gen(Function *func) { string code; - /** - *code in here - */ + bool haveCall = false; + auto bbs = func->getBasicBlocks(); + for (auto iter = bbs.begin(); iter != bbs.end(); ++iter) + { + auto bb = iter->get(); + for (auto &instr : bb->getInstructions()) + { + auto instrType = instr->getKind(); + if (instrType == Value::Kind::kCall) + { + haveCall = true; + break; + } + } + } + if (haveCall) + code += space + "pop\t{fp,lr}" + endl; + else + code += space + "pop\t{fp}" + endl; + code += space + "bx\t" + endl; return code; } @@ -327,7 +371,7 @@ namespace backend { asmCode += space + ".data" + endl; //******************Revised by lyq BEGIN*************************************** - if(glbvl->isconst()) + if (glbvl->isconst()) asmCode += space + ".section" + space + "rodata" + endl; //******************Revised by lyq END***************************************** { @@ -349,7 +393,7 @@ namespace backend { asmCode += space + ".bss" + endl; //******************Revised by lyq BEGIN*************************************** - if(glbvl->isconst()) + if (glbvl->isconst()) asmCode += space + ".section" + space + "rodata" + endl; //******************Revised by lyq END***************************************** {