fix(frontend): 规范一下前端实现
This commit is contained in:
@@ -23,7 +23,7 @@ class Context {
|
||||
const std::shared_ptr<Type>& PtrInt32();
|
||||
// 去重创建 i32 常量。
|
||||
ConstantInt* GetConstInt(int v);
|
||||
// 生成临时名称,如 %t0、%t1 ...
|
||||
|
||||
std::string NextTemp();
|
||||
|
||||
private:
|
||||
|
||||
@@ -6,6 +6,13 @@
|
||||
#include <stdexcept>
|
||||
|
||||
namespace ir {
|
||||
namespace {
|
||||
|
||||
bool IsArithmeticType(const std::shared_ptr<Type>& ty) {
|
||||
return ty && ty->kind() == Type::Kind::Int32;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ConstantInt* IRBuilder::CreateConstInt(int v) {
|
||||
// 常量不需要挂在基本块里,由 Context 负责去重与生命周期。
|
||||
@@ -17,7 +24,23 @@ BinaryInst* IRBuilder::CreateBinary(Opcode op, Value* lhs, Value* rhs,
|
||||
if (!insertBlock_) {
|
||||
throw std::runtime_error("IRBuilder 未设置插入点");
|
||||
}
|
||||
return insertBlock_->Append<BinaryInst>(op, Type::Int32(), lhs, rhs, name);
|
||||
if (!lhs) {
|
||||
throw std::runtime_error("IRBuilder::CreateBinary 缺少 lhs");
|
||||
}
|
||||
if (!rhs) {
|
||||
throw std::runtime_error("IRBuilder::CreateBinary 缺少 rhs");
|
||||
}
|
||||
if (op != Opcode::Add) {
|
||||
throw std::runtime_error("IRBuilder::CreateBinary 当前只支持 Add");
|
||||
}
|
||||
if (!lhs->type() || !rhs->type() ||
|
||||
lhs->type()->kind() != rhs->type()->kind()) {
|
||||
throw std::runtime_error("IRBuilder::CreateBinary 操作数类型不匹配");
|
||||
}
|
||||
if (!IsArithmeticType(lhs->type())) {
|
||||
throw std::runtime_error("IRBuilder::CreateBinary 当前只支持 i32 二元运算");
|
||||
}
|
||||
return insertBlock_->Append<BinaryInst>(op, lhs->type(), lhs, rhs, name);
|
||||
}
|
||||
|
||||
AllocaInst* IRBuilder::CreateAllocaI32(const std::string& name) {
|
||||
|
||||
Reference in New Issue
Block a user