You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nudt-compiler-cpp/src/irgen/IRGenDecl.cpp

34 lines
783 B

#include "irgen/IRGen.h"
#include <stdexcept>
#include "SysYParser.h"
#include "ir/IR.h"
void IRGenImpl::GenBlock(SysYParser::BlockContext& block) {
for (auto* stmt : block.stmt()) {
if (stmt) {
if (GenStmt(*stmt)) {
break;
}
}
}
}
void IRGenImpl::GenVarDecl(SysYParser::VarDeclContext& decl) {
const std::string name = decl.Ident()->getText();
if (locals_.find(name) != locals_.end()) {
throw std::runtime_error("[irgen] 重复定义变量: " + name);
}
auto* slot = builder_.CreateAllocaI32(ir::DefaultContext().NextTemp());
locals_[name] = slot;
ir::Value* init = nullptr;
if (decl.exp()) {
init = GenExpr(*decl.exp());
} else {
init = ir::DefaultContext().GetConstInt(0);
}
builder_.CreateStore(init, slot);
}