29 lines
669 B
C++
29 lines
669 B
C++
#include "irgen/IRGen.h"
|
|
|
|
#include <stdexcept>
|
|
|
|
#include "SysYParser.h"
|
|
#include "ir/IR.h"
|
|
|
|
IRGenImpl::IRGenImpl(ir::Module& module)
|
|
: module_(module), func_(nullptr), builder_(nullptr) {}
|
|
|
|
void IRGenImpl::Gen(SysYParser::CompUnitContext& cu) {
|
|
if (!cu.funcDef()) {
|
|
throw std::runtime_error("[irgen] 缺少 main 定义");
|
|
}
|
|
GenFuncDef(*cu.funcDef());
|
|
}
|
|
|
|
void IRGenImpl::GenFuncDef(SysYParser::FuncDefContext& func) {
|
|
if (!func.block()) {
|
|
throw std::runtime_error("[irgen] 函数体为空");
|
|
}
|
|
|
|
func_ = module_.CreateFunction("main", ir::Type::Int32());
|
|
builder_.SetInsertPoint(func_->entry());
|
|
locals_.clear();
|
|
|
|
GenBlock(*func.block());
|
|
}
|