diff --git a/src/IR.h b/src/IR.h index 856d49d..fbe421a 100644 --- a/src/IR.h +++ b/src/IR.h @@ -953,6 +953,7 @@ namespace sysy { return block == b.get(); }); } int allocateVariableID() { return variableID++; } + void resetVariableID() { variableID = 0; } int allocateblockID() { return blockID++; } public: diff --git a/src/IRBuilder.h b/src/IRBuilder.h index 8a7bf25..c7452f0 100644 --- a/src/IRBuilder.h +++ b/src/IRBuilder.h @@ -306,6 +306,7 @@ namespace sysy const std::vector &indices = {}, const std::string &name = "") { + block->getParent()->resetVariableID(); auto inst = new StoreInst(value, pointer, indices, block, name); assert(inst); block->getInstructions().emplace(position, inst); diff --git a/src/backend/codegen.cpp b/src/backend/codegen.cpp index 4fcfe08..b9fd846 100644 --- a/src/backend/codegen.cpp +++ b/src/backend/codegen.cpp @@ -333,6 +333,8 @@ namespace backend } else if (bInst->getKind() == Instruction::kDiv) code += space + "div\tr" + res + ", " + lname + ", " + rname + endl; + else if (lconst && rconst) + return {dstRegId, code}; else if (bInst->getKind() == Instruction::kICmpEQ) code += space + "cmp\t" + lname + ", " + rname + endl; else if (bInst->getKind() == Instruction::kICmpGE) @@ -469,8 +471,10 @@ namespace backend auto retval = retInst->getReturnValue(); if (isa(retval)) { - code += space + "mov\tr0, " + to_string(dynamic_cast(retval)->getInt()) + endl; + code += space + "mov\tr0, #" + to_string(dynamic_cast(retval)->getInt()) + endl; } + else if (isa(retval)) + ; else { code += space + "mov\tr0, r" + retval->getName() + endl; @@ -499,9 +503,50 @@ namespace backend if (isa(cond)) { BinaryInst *bInst = dynamic_cast(cond); - // auto lhs = bInst->getLhs(); - // auto rhs = bInst->getRhs(); - if (bInst->getKind() == Instruction::kICmpEQ) + auto lhs = bInst->getLhs(); + auto rhs = bInst->getRhs(); + if (isa(lhs) && isa(rhs)) + { + int lvalue = dynamic_cast(lhs)->getInt(); + int rvalue = dynamic_cast(rhs)->getInt(); + if (bInst->getKind() == Instruction::kICmpEQ) + if (lvalue == rvalue) + code += space + "b\t" + then_block->getName() + endl; + else + code += space + "b\t" + else_block->getName() + endl; + else if (bInst->getKind() == Instruction::kICmpGE) + if (lvalue >= rvalue) + code += space + "b\t" + then_block->getName() + endl; + else + code += space + "b\t" + else_block->getName() + endl; + else if (bInst->getKind() == Instruction::kICmpGT) + if (lvalue > rvalue) + code += space + "b\t" + then_block->getName() + endl; + else + code += space + "b\t" + else_block->getName() + endl; + else if (bInst->getKind() == Instruction::kICmpLE) + if (lvalue <= rvalue) + code += space + "b\t" + then_block->getName() + endl; + else + code += space + "b\t" + else_block->getName() + endl; + else if (bInst->getKind() == Instruction::kICmpLT) + if (lvalue < rvalue) + code += space + "b\t" + then_block->getName() + endl; + else + code += space + "b\t" + else_block->getName() + endl; + else if (bInst->getKind() == Instruction::kICmpNE) + if (lvalue != rvalue) + code += space + "b\t" + then_block->getName() + endl; + else + code += space + "b\t" + else_block->getName() + endl; + else + { + code += space + "cmp\tr" + bInst->getName() + ", #0" + endl; + code += space + "bne\t" + then_block->getName() + endl; + code += space + "b\t" + else_block->getName() + endl; + } + } + else if (bInst->getKind() == Instruction::kICmpEQ) { // code += binaryInst_gen(bInst, RegManager::RANY).second; code += space + "beq\t" + then_block->getName() + endl; @@ -584,8 +629,12 @@ namespace backend continue; } // int src_reg = stoi(arg->getName()) + 4; - int src_reg = stoi(arg->getName()); - code += space + "mov\tr" + to_string(arg_num - 1) + ", r" + to_string(src_reg) + endl; + string src_name; + if (isa(arg)) + src_name = "#" + to_string(dynamic_cast(arg)->getInt()); + else + src_name = "r" + arg->getName(); + code += space + "mov\tr" + to_string(arg_num - 1) + ", " + src_name + endl; // code += space + "mov\tr" + to_string(arg_num - 1) + ", r" + src + endl; } code += space + "bl\t" + callee_fuc->getName() + endl;