diff --git a/src/IR.cpp b/src/IR.cpp index acd8455..3335598 100644 --- a/src/IR.cpp +++ b/src/IR.cpp @@ -510,7 +510,7 @@ void User::replaceOperand(int index, Value *value) { use.setValue(value); } -CallInst::CallInst(Function *callee, const std::vector args, +CallInst::CallInst(Function *callee, const std::vector &args, BasicBlock *parent, const std::string &name) : Instruction(kCall, callee->getReturnType(), parent, name) { addOperand(callee); diff --git a/src/IR.h b/src/IR.h index 9591c49..ae16ac9 100644 --- a/src/IR.h +++ b/src/IR.h @@ -183,7 +183,7 @@ protected: protected: Value(Type *type, const std::string &name = "") : type(type), name(name), uses() {} - virtual ~Value() {} + virtual ~Value() = default; public: Type *getType() const { return type; } @@ -258,6 +258,7 @@ protected: public: Argument(Type *type, BasicBlock *block, int index, const std::string &name = ""); + virtual ~Argument() = default; public: BasicBlock *getParent() const { return block; } @@ -328,6 +329,8 @@ protected: User(Type *type, const std::string &name = "") : Value(type, name), operands() {} +public: + virtual ~User() = default; public: using use_iterator = std::vector::const_iterator; struct operand_iterator : public std::vector::const_iterator { @@ -473,7 +476,7 @@ class CallInst : public Instruction { friend class IRBuilder; protected: - CallInst(Function *callee, const std::vector args = {}, + CallInst(Function *callee, const std::vector &args = {}, BasicBlock *parent = nullptr, const std::string &name = ""); public: @@ -627,6 +630,7 @@ public: int getNumDims() const { return getNumOperands(); } auto getDims() const { return getOperands(); } Value *getDim(int index) { return getOperand(index); } + public: virtual void print(std::ostream &os) const override; }; // class AllocaInst @@ -651,6 +655,7 @@ public: return make_range(std::next(operand_begin()), operand_end()); } Value *getIndex(int index) const { return getOperand(index + 1); } + public: virtual void print(std::ostream &os) const override; }; // class LoadInst @@ -677,6 +682,7 @@ public: return make_range(std::next(operand_begin(), 2), operand_end()); } Value *getIndex(int index) const { return getOperand(index + 2); } + public: virtual void print(std::ostream &os) const override; }; // class StoreInst @@ -721,6 +727,7 @@ public: } int allocateVariableID() { return variableID++; } int allocateblockID() { return blockID++; } + public: virtual void print(std::ostream &os) const override; }; // class Function @@ -739,8 +746,9 @@ public: public: auto getValues() const { return getOperands(); } + public: - virtual void print(std::ostream &os) const override {}; + virtual void print(std::ostream &os) const override{}; }; // class ConstantArray //! Global value declared at file scope @@ -762,12 +770,16 @@ protected: addOperand(init); } +public: + virtual ~GlobalValue() = default; + public: Value *init() const { return hasInit ? operands.back().getValue() : nullptr; } int getNumDims() const { return getNumOperands() - (hasInit ? 1 : 0); } Value *getDim(int index) { return getOperand(index); } + public: - virtual void print(std::ostream &os) const override {}; + virtual void print(std::ostream &os) const override{}; }; // class GlobalValue //! IR unit for representing a SysY compile unit @@ -807,6 +819,7 @@ public: return nullptr; return result->second.get(); } + public: void print(std::ostream &os) const; }; // class Module diff --git a/src/IRBuilder.h b/src/IRBuilder.h index 2aec108..60cb092 100644 --- a/src/IRBuilder.h +++ b/src/IRBuilder.h @@ -27,6 +27,14 @@ public: void setPosition(BasicBlock::iterator position) { this->position = position; } public: + CallInst *createCallInst(Function *callee, + const std::vector &args = {}, + const std::string &name = "") { + auto inst = new CallInst(callee, args, block, name); + assert(inst); + block->getInstructions().emplace(position, inst); + return inst; + } UnaryInst *createUnaryInst(Instruction::Kind kind, Type *type, Value *operand, const std::string &name = "") { diff --git a/src/SysYIRGenerator.cpp b/src/SysYIRGenerator.cpp index 3d76d67..313fe34 100644 --- a/src/SysYIRGenerator.cpp +++ b/src/SysYIRGenerator.cpp @@ -146,10 +146,14 @@ any SysYIRGenerator::visitNumberExp(SysYParser::NumberExpContext *ctx) { any SysYIRGenerator::visitLValueExp(SysYParser::LValueExpContext *ctx) { auto name = ctx->lValue()->ID()->getText(); - auto ptr = symbols.lookup(name); - if (not ptr) + Value *value = symbols.lookup(name); + if (not value) error(ctx, "undefined variable"); - Value *value = builder.createLoadInst(ptr); + auto a = dynamic_cast(value); + if (dynamic_cast(value) or + (dynamic_cast(value) and + dynamic_cast(value))) + value = builder.createLoadInst(value); return value; } @@ -211,4 +215,18 @@ any SysYIRGenerator::visitReturnStmt(SysYParser::ReturnStmtContext *ctx) { return result; } +any SysYIRGenerator::visitCall(SysYParser::CallContext *ctx) { + auto funcName = ctx->ID()->getText(); + auto func = dynamic_cast(symbols.lookup(funcName)); + assert(func); + vector args; + if (auto rArgs = ctx->funcRParams()) { + for (auto exp : rArgs->exp()) { + args.push_back(any_cast(exp->accept(this))); + } + } + Value *call = builder.createCallInst(func, args); + return call; +} + } // namespace sysy \ No newline at end of file diff --git a/src/SysYIRGenerator.h b/src/SysYIRGenerator.h index ed87323..6b9db90 100644 --- a/src/SysYIRGenerator.h +++ b/src/SysYIRGenerator.h @@ -199,9 +199,7 @@ public: return visitChildren(ctx); } - virtual std::any visitCall(SysYParser::CallContext *ctx) override { - return visitChildren(ctx); - } + virtual std::any visitCall(SysYParser::CallContext *ctx) override; virtual std::any visitLValue(SysYParser::LValueContext *ctx) override { return visitChildren(ctx); diff --git a/test/11_add2.sy b/test/11_add2.sy index ff966fa..c783a32 100644 --- a/test/11_add2.sy +++ b/test/11_add2.sy @@ -1,7 +1,13 @@ //test add + +int mul(int x, int y) { + return x * y; +} + int main(){ int a, b; a = 10; b = 0; + a = mul(a, b); return a + b; }