[IR]:增加默认添加ret指令逻辑
This commit is contained in:
@@ -204,6 +204,7 @@ std::any SysYIRGenerator::visitFuncType(SysYParser::FuncTypeContext *ctx) {
|
||||
std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){
|
||||
// 更新作用域
|
||||
module->enterNewScope();
|
||||
HasReturnInst = false;
|
||||
|
||||
auto name = ctx->Ident()->getText();
|
||||
std::vector<Type *> paramTypes;
|
||||
@@ -243,6 +244,18 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){
|
||||
visitBlockItem(item);
|
||||
}
|
||||
|
||||
if(HasReturnInst == false) {
|
||||
// 如果没有return语句,则默认返回0
|
||||
if (returnType != Type::getVoidType()) {
|
||||
Value* returnValue = ConstantValue::get(0);
|
||||
if (returnType == Type::getFloatType()) {
|
||||
returnValue = ConstantValue::get(0.0f);
|
||||
}
|
||||
builder.createReturnInst(returnValue);
|
||||
} else {
|
||||
builder.createReturnInst();
|
||||
}
|
||||
}
|
||||
module->leaveScope();
|
||||
|
||||
return std::any();
|
||||
@@ -478,6 +491,7 @@ std::any SysYIRGenerator::visitReturnStmt(SysYParser::ReturnStmtContext *ctx) {
|
||||
}
|
||||
}
|
||||
builder.createReturnInst(returnValue);
|
||||
HasReturnInst = true;
|
||||
return std::any();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user