Partial support for function call IR genenration

main
Su Xing 3 years ago
parent 1f0928a443
commit 9f5ea0d3ca

@ -510,7 +510,7 @@ void User::replaceOperand(int index, Value *value) {
use.setValue(value);
}
CallInst::CallInst(Function *callee, const std::vector<Value *> args,
CallInst::CallInst(Function *callee, const std::vector<Value *> &args,
BasicBlock *parent, const std::string &name)
: Instruction(kCall, callee->getReturnType(), parent, name) {
addOperand(callee);

@ -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<Use>::const_iterator;
struct operand_iterator : public std::vector<Use>::const_iterator {
@ -473,7 +476,7 @@ class CallInst : public Instruction {
friend class IRBuilder;
protected:
CallInst(Function *callee, const std::vector<Value *> args = {},
CallInst(Function *callee, const std::vector<Value *> &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

@ -27,6 +27,14 @@ public:
void setPosition(BasicBlock::iterator position) { this->position = position; }
public:
CallInst *createCallInst(Function *callee,
const std::vector<Value *> &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 = "") {

@ -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<Argument *>(value);
if (dynamic_cast<GlobalValue *>(value) or
(dynamic_cast<Instruction *>(value) and
dynamic_cast<AllocaInst *>(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<Function *>(symbols.lookup(funcName));
assert(func);
vector<Value *> args;
if (auto rArgs = ctx->funcRParams()) {
for (auto exp : rArgs->exp()) {
args.push_back(any_cast<Value *>(exp->accept(this)));
}
}
Value *call = builder.createCallInst(func, args);
return call;
}
} // namespace sysy

@ -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);

@ -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;
}

Loading…
Cancel
Save