refactor(dev): 统一 IR/MIR 接口命名风格

This commit is contained in:
Lane0218
2026-03-12 15:17:02 +08:00
parent f26551a896
commit b1155d8fa9
18 changed files with 170 additions and 167 deletions

View File

@@ -10,26 +10,26 @@ namespace ir {
BasicBlock::BasicBlock(std::string name) : name_(std::move(name)) {}
const std::string& BasicBlock::name() const { return name_; }
const std::string& BasicBlock::GetName() const { return name_; }
Function* BasicBlock::parent() const { return parent_; }
Function* BasicBlock::GetParent() const { return parent_; }
void BasicBlock::set_parent(Function* parent) { parent_ = parent; }
void BasicBlock::SetParent(Function* parent) { parent_ = parent; }
bool BasicBlock::HasTerminator() const {
return !instructions_.empty() && instructions_.back()->IsTerminator();
}
const std::vector<std::unique_ptr<Instruction>>& BasicBlock::instructions()
const std::vector<std::unique_ptr<Instruction>>& BasicBlock::GetInstructions()
const {
return instructions_;
}
const std::vector<BasicBlock*>& BasicBlock::predecessors() const {
const std::vector<BasicBlock*>& BasicBlock::GetPredecessors() const {
return predecessors_;
}
const std::vector<BasicBlock*>& BasicBlock::successors() const {
const std::vector<BasicBlock*>& BasicBlock::GetSuccessors() const {
return successors_;
}

View File

@@ -13,7 +13,7 @@ Function::Function(std::string name, std::shared_ptr<Type> ret_type)
BasicBlock* Function::CreateBlock(const std::string& name) {
auto block = std::make_unique<BasicBlock>(name);
auto* ptr = block.get();
ptr->set_parent(this);
ptr->SetParent(this);
blocks_.push_back(std::move(block));
if (!entry_) {
entry_ = ptr;
@@ -21,11 +21,11 @@ BasicBlock* Function::CreateBlock(const std::string& name) {
return ptr;
}
BasicBlock* Function::entry() { return entry_; }
BasicBlock* Function::GetEntry() { return entry_; }
const BasicBlock* Function::entry() const { return entry_; }
const BasicBlock* Function::GetEntry() const { return entry_; }
const std::vector<std::unique_ptr<BasicBlock>>& Function::blocks() const {
const std::vector<std::unique_ptr<BasicBlock>>& Function::GetBlocks() const {
return blocks_;
}

View File

@@ -10,11 +10,11 @@
namespace ir {
IRBuilder::IRBuilder(Context& ctx, BasicBlock* bb)
: ctx_(ctx), insertBlock_(bb) {}
: ctx_(ctx), insert_block_(bb) {}
void IRBuilder::SetInsertPoint(BasicBlock* bb) { insertBlock_ = bb; }
void IRBuilder::SetInsertPoint(BasicBlock* bb) { insert_block_ = bb; }
BasicBlock* IRBuilder::GetInsertBlock() const { return insertBlock_; }
BasicBlock* IRBuilder::GetInsertBlock() const { return insert_block_; }
ConstantInt* IRBuilder::CreateConstInt(int v) {
// 常量不需要挂在基本块里,由 Context 负责去重与生命周期。
@@ -23,7 +23,7 @@ ConstantInt* IRBuilder::CreateConstInt(int v) {
BinaryInst* IRBuilder::CreateBinary(Opcode op, Value* lhs, Value* rhs,
const std::string& name) {
if (!insertBlock_) {
if (!insert_block_) {
throw std::runtime_error(FormatError("ir", "IRBuilder 未设置插入点"));
}
if (!lhs) {
@@ -34,7 +34,7 @@ BinaryInst* IRBuilder::CreateBinary(Opcode op, Value* lhs, Value* rhs,
throw std::runtime_error(
FormatError("ir", "IRBuilder::CreateBinary 缺少 rhs"));
}
return insertBlock_->Append<BinaryInst>(op, lhs->type(), lhs, rhs, name);
return insert_block_->Append<BinaryInst>(op, lhs->GetType(), lhs, rhs, name);
}
BinaryInst* IRBuilder::CreateAdd(Value* lhs, Value* rhs,
@@ -43,25 +43,25 @@ BinaryInst* IRBuilder::CreateAdd(Value* lhs, Value* rhs,
}
AllocaInst* IRBuilder::CreateAllocaI32(const std::string& name) {
if (!insertBlock_) {
if (!insert_block_) {
throw std::runtime_error(FormatError("ir", "IRBuilder 未设置插入点"));
}
return insertBlock_->Append<AllocaInst>(ctx_.PtrInt32(), name);
return insert_block_->Append<AllocaInst>(ctx_.PtrInt32(), name);
}
LoadInst* IRBuilder::CreateLoad(Value* ptr, const std::string& name) {
if (!insertBlock_) {
if (!insert_block_) {
throw std::runtime_error(FormatError("ir", "IRBuilder 未设置插入点"));
}
if (!ptr) {
throw std::runtime_error(
FormatError("ir", "IRBuilder::CreateLoad 缺少 ptr"));
}
return insertBlock_->Append<LoadInst>(ctx_.Int32(), ptr, name);
return insert_block_->Append<LoadInst>(ctx_.Int32(), ptr, name);
}
StoreInst* IRBuilder::CreateStore(Value* val, Value* ptr) {
if (!insertBlock_) {
if (!insert_block_) {
throw std::runtime_error(FormatError("ir", "IRBuilder 未设置插入点"));
}
if (!val) {
@@ -72,18 +72,18 @@ StoreInst* IRBuilder::CreateStore(Value* val, Value* ptr) {
throw std::runtime_error(
FormatError("ir", "IRBuilder::CreateStore 缺少 ptr"));
}
return insertBlock_->Append<StoreInst>(ctx_.Void(), val, ptr);
return insert_block_->Append<StoreInst>(ctx_.Void(), val, ptr);
}
ReturnInst* IRBuilder::CreateRet(Value* v) {
if (!insertBlock_) {
if (!insert_block_) {
throw std::runtime_error(FormatError("ir", "IRBuilder 未设置插入点"));
}
if (!v) {
throw std::runtime_error(
FormatError("ir", "IRBuilder::CreateRet 缺少返回值"));
}
return insertBlock_->Append<ReturnInst>(ctx_.Void(), v);
return insert_block_->Append<ReturnInst>(ctx_.Void(), v);
}
} // namespace ir

View File

@@ -13,7 +13,7 @@
namespace ir {
static const char* TypeToString(const Type& ty) {
switch (ty.kind()) {
switch (ty.GetKind()) {
case Type::Kind::Void:
return "void";
case Type::Kind::Int32:
@@ -46,54 +46,55 @@ static const char* OpcodeToString(Opcode op) {
static std::string ValueToString(const Value* v) {
if (auto* ci = dynamic_cast<const ConstantInt*>(v)) {
return std::to_string(ci->value());
return std::to_string(ci->GetValue());
}
return v ? v->name() : "<null>";
return v ? v->GetName() : "<null>";
}
void IRPrinter::Print(const Module& module, std::ostream& os) {
for (const auto& func : module.functions()) {
os << "define " << TypeToString(*func->type()) << " @" << func->name()
for (const auto& func : module.GetFunctions()) {
os << "define " << TypeToString(*func->GetType()) << " @" << func->GetName()
<< "() {\n";
for (const auto& bb : func->blocks()) {
for (const auto& bb : func->GetBlocks()) {
if (!bb) {
continue;
}
os << bb->name() << ":\n";
for (const auto& instPtr : bb->instructions()) {
os << bb->GetName() << ":\n";
for (const auto& instPtr : bb->GetInstructions()) {
const auto* inst = instPtr.get();
switch (inst->opcode()) {
switch (inst->GetOpcode()) {
case Opcode::Add:
case Opcode::Sub:
case Opcode::Mul: {
auto* bin = static_cast<const BinaryInst*>(inst);
os << " " << bin->name() << " = " << OpcodeToString(bin->opcode())
<< " " << TypeToString(*bin->lhs()->type()) << " "
<< ValueToString(bin->lhs()) << ", "
<< ValueToString(bin->rhs()) << "\n";
os << " " << bin->GetName() << " = "
<< OpcodeToString(bin->GetOpcode()) << " "
<< TypeToString(*bin->GetLhs()->GetType()) << " "
<< ValueToString(bin->GetLhs()) << ", "
<< ValueToString(bin->GetRhs()) << "\n";
break;
}
case Opcode::Alloca: {
auto* alloca = static_cast<const AllocaInst*>(inst);
os << " " << alloca->name() << " = alloca i32\n";
os << " " << alloca->GetName() << " = alloca i32\n";
break;
}
case Opcode::Load: {
auto* load = static_cast<const LoadInst*>(inst);
os << " " << load->name() << " = load i32, i32* "
<< ValueToString(load->ptr()) << "\n";
os << " " << load->GetName() << " = load i32, i32* "
<< ValueToString(load->GetPtr()) << "\n";
break;
}
case Opcode::Store: {
auto* store = static_cast<const StoreInst*>(inst);
os << " store i32 " << ValueToString(store->value()) << ", i32* "
<< ValueToString(store->ptr()) << "\n";
os << " store i32 " << ValueToString(store->GetValue())
<< ", i32* " << ValueToString(store->GetPtr()) << "\n";
break;
}
case Opcode::Ret: {
auto* ret = static_cast<const ReturnInst*>(inst);
os << " ret " << TypeToString(*ret->value()->type()) << " "
<< ValueToString(ret->value()) << "\n";
os << " ret " << TypeToString(*ret->GetValue()->GetType()) << " "
<< ValueToString(ret->GetValue()) << "\n";
break;
}
}

View File

@@ -11,13 +11,13 @@ namespace ir {
Instruction::Instruction(Opcode op, std::shared_ptr<Type> ty, std::string name)
: Value(std::move(ty), std::move(name)), opcode_(op) {}
Opcode Instruction::opcode() const { return opcode_; }
Opcode Instruction::GetOpcode() const { return opcode_; }
bool Instruction::IsTerminator() const { return opcode_ == Opcode::Ret; }
BasicBlock* Instruction::parent() const { return parent_; }
BasicBlock* Instruction::GetParent() const { return parent_; }
void Instruction::set_parent(BasicBlock* parent) { parent_ = parent; }
void Instruction::SetParent(BasicBlock* parent) { parent_ = parent; }
BinaryInst::BinaryInst(Opcode op, std::shared_ptr<Type> ty, Value* lhs,
Value* rhs, std::string name)
@@ -28,11 +28,11 @@ BinaryInst::BinaryInst(Opcode op, std::shared_ptr<Type> ty, Value* lhs,
if (!lhs_ || !rhs_) {
throw std::runtime_error(FormatError("ir", "BinaryInst 缺少操作数"));
}
if (!type_ || !lhs_->type() || !rhs_->type()) {
if (!type_ || !lhs_->GetType() || !rhs_->GetType()) {
throw std::runtime_error(FormatError("ir", "BinaryInst 缺少类型信息"));
}
if (lhs_->type()->kind() != rhs_->type()->kind() ||
type_->kind() != lhs_->type()->kind()) {
if (lhs_->GetType()->GetKind() != rhs_->GetType()->GetKind() ||
type_->GetKind() != lhs_->GetType()->GetKind()) {
throw std::runtime_error(FormatError("ir", "BinaryInst 类型不匹配"));
}
if (!type_->IsInt32()) {
@@ -46,9 +46,9 @@ BinaryInst::BinaryInst(Opcode op, std::shared_ptr<Type> ty, Value* lhs,
}
}
Value* BinaryInst::lhs() const { return lhs_; }
Value* BinaryInst::GetLhs() const { return lhs_; }
Value* BinaryInst::rhs() const { return rhs_; }
Value* BinaryInst::GetRhs() const { return rhs_; }
ReturnInst::ReturnInst(std::shared_ptr<Type> void_ty, Value* val)
: Instruction(Opcode::Ret, std::move(void_ty), ""),
@@ -62,7 +62,7 @@ ReturnInst::ReturnInst(std::shared_ptr<Type> void_ty, Value* val)
value_->AddUser(this);
}
Value* ReturnInst::value() const { return value_; }
Value* ReturnInst::GetValue() const { return value_; }
AllocaInst::AllocaInst(std::shared_ptr<Type> ptr_ty, std::string name)
: Instruction(Opcode::Alloca, std::move(ptr_ty), std::move(name)) {
@@ -80,14 +80,14 @@ LoadInst::LoadInst(std::shared_ptr<Type> val_ty, Value* ptr, std::string name)
if (!type_ || !type_->IsInt32()) {
throw std::runtime_error(FormatError("ir", "LoadInst 当前只支持加载 i32"));
}
if (!ptr_->type() || !ptr_->type()->IsPtrInt32()) {
if (!ptr_->GetType() || !ptr_->GetType()->IsPtrInt32()) {
throw std::runtime_error(
FormatError("ir", "LoadInst 当前只支持从 i32* 加载"));
}
ptr_->AddUser(this);
}
Value* LoadInst::ptr() const { return ptr_; }
Value* LoadInst::GetPtr() const { return ptr_; }
StoreInst::StoreInst(std::shared_ptr<Type> void_ty, Value* val, Value* ptr)
: Instruction(Opcode::Store, std::move(void_ty), ""),
@@ -102,10 +102,10 @@ StoreInst::StoreInst(std::shared_ptr<Type> void_ty, Value* val, Value* ptr)
if (!type_ || !type_->IsVoid()) {
throw std::runtime_error(FormatError("ir", "StoreInst 返回类型必须为 void"));
}
if (!value_->type() || !value_->type()->IsInt32()) {
if (!value_->GetType() || !value_->GetType()->IsInt32()) {
throw std::runtime_error(FormatError("ir", "StoreInst 当前只支持存储 i32"));
}
if (!ptr_->type() || !ptr_->type()->IsPtrInt32()) {
if (!ptr_->GetType() || !ptr_->GetType()->IsPtrInt32()) {
throw std::runtime_error(
FormatError("ir", "StoreInst 当前只支持写入 i32*"));
}
@@ -113,8 +113,8 @@ StoreInst::StoreInst(std::shared_ptr<Type> void_ty, Value* val, Value* ptr)
ptr_->AddUser(this);
}
Value* StoreInst::value() const { return value_; }
Value* StoreInst::GetValue() const { return value_; }
Value* StoreInst::ptr() const { return ptr_; }
Value* StoreInst::GetPtr() const { return ptr_; }
} // namespace ir

View File

@@ -4,9 +4,9 @@
namespace ir {
Context& Module::context() { return context_; }
Context& Module::GetContext() { return context_; }
const Context& Module::context() const { return context_; }
const Context& Module::GetContext() const { return context_; }
Function* Module::CreateFunction(const std::string& name,
std::shared_ptr<Type> ret_type) {
@@ -14,7 +14,7 @@ Function* Module::CreateFunction(const std::string& name,
return functions_.back().get();
}
const std::vector<std::unique_ptr<Function>>& Module::functions() const {
const std::vector<std::unique_ptr<Function>>& Module::GetFunctions() const {
return functions_;
}

View File

@@ -5,7 +5,7 @@ namespace ir {
Type::Type(Kind k) : kind_(k) {}
Type::Kind Type::kind() const { return kind_; }
Type::Kind Type::GetKind() const { return kind_; }
bool Type::IsVoid() const { return kind_ == Kind::Void; }

View File

@@ -8,15 +8,15 @@ namespace ir {
Value::Value(std::shared_ptr<Type> ty, std::string name)
: type_(std::move(ty)), name_(std::move(name)) {}
const std::shared_ptr<Type>& Value::type() const { return type_; }
const std::shared_ptr<Type>& Value::GetType() const { return type_; }
const std::string& Value::name() const { return name_; }
const std::string& Value::GetName() const { return name_; }
void Value::set_name(std::string n) { name_ = std::move(n); }
void Value::SetName(std::string n) { name_ = std::move(n); }
void Value::AddUser(Instruction* user) { users_.push_back(user); }
const std::vector<Instruction*>& Value::users() const { return users_; }
const std::vector<Instruction*>& Value::GetUsers() const { return users_; }
ConstantInt::ConstantInt(std::shared_ptr<Type> ty, int v)
: Value(std::move(ty), ""), value_(v) {}