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

@@ -43,7 +43,7 @@ class Type {
public:
enum class Kind { Void, Int32, PtrInt32 };
explicit Type(Kind k);
Kind kind() const;
Kind GetKind() const;
bool IsVoid() const;
bool IsInt32() const;
bool IsPtrInt32() const;
@@ -56,11 +56,11 @@ class Value {
public:
Value(std::shared_ptr<Type> ty, std::string name);
virtual ~Value() = default;
const std::shared_ptr<Type>& type() const;
const std::string& name() const;
void set_name(std::string n);
const std::shared_ptr<Type>& GetType() const;
const std::string& GetName() const;
void SetName(std::string n);
void AddUser(Instruction* user);
const std::vector<Instruction*>& users() const;
const std::vector<Instruction*>& GetUsers() const;
protected:
std::shared_ptr<Type> type_;
@@ -71,7 +71,7 @@ class Value {
class ConstantInt : public Value {
public:
ConstantInt(std::shared_ptr<Type> ty, int v);
int value() const { return value_; }
int GetValue() const { return value_; }
private:
int value_{};
@@ -83,10 +83,10 @@ enum class Opcode { Add, Sub, Mul, Alloca, Load, Store, Ret };
class Instruction : public Value {
public:
Instruction(Opcode op, std::shared_ptr<Type> ty, std::string name = "");
Opcode opcode() const;
Opcode GetOpcode() const;
bool IsTerminator() const;
BasicBlock* parent() const;
void set_parent(BasicBlock* parent);
BasicBlock* GetParent() const;
void SetParent(BasicBlock* parent);
private:
Opcode opcode_;
@@ -97,8 +97,8 @@ class BinaryInst : public Instruction {
public:
BinaryInst(Opcode op, std::shared_ptr<Type> ty, Value* lhs, Value* rhs,
std::string name);
Value* lhs() const;
Value* rhs() const;
Value* GetLhs() const;
Value* GetRhs() const;
private:
Value* lhs_;
@@ -108,7 +108,7 @@ class BinaryInst : public Instruction {
class ReturnInst : public Instruction {
public:
ReturnInst(std::shared_ptr<Type> void_ty, Value* val);
Value* value() const;
Value* GetValue() const;
private:
Value* value_;
@@ -122,7 +122,7 @@ class AllocaInst : public Instruction {
class LoadInst : public Instruction {
public:
LoadInst(std::shared_ptr<Type> val_ty, Value* ptr, std::string name);
Value* ptr() const;
Value* GetPtr() const;
private:
Value* ptr_;
@@ -131,8 +131,8 @@ class LoadInst : public Instruction {
class StoreInst : public Instruction {
public:
StoreInst(std::shared_ptr<Type> void_ty, Value* val, Value* ptr);
Value* value() const;
Value* ptr() const;
Value* GetValue() const;
Value* GetPtr() const;
private:
Value* value_;
@@ -142,13 +142,13 @@ class StoreInst : public Instruction {
class BasicBlock {
public:
explicit BasicBlock(std::string name);
const std::string& name() const;
Function* parent() const;
void set_parent(Function* parent);
const std::string& GetName() const;
Function* GetParent() const;
void SetParent(Function* parent);
bool HasTerminator() const;
const std::vector<std::unique_ptr<Instruction>>& instructions() const;
const std::vector<BasicBlock*>& predecessors() const;
const std::vector<BasicBlock*>& successors() const;
const std::vector<std::unique_ptr<Instruction>>& GetInstructions() const;
const std::vector<BasicBlock*>& GetPredecessors() const;
const std::vector<BasicBlock*>& GetSuccessors() const;
template <typename T, typename... Args>
T* Append(Args&&... args) {
if (HasTerminator()) {
@@ -157,7 +157,7 @@ class BasicBlock {
}
auto inst = std::make_unique<T>(std::forward<Args>(args)...);
auto* ptr = inst.get();
ptr->set_parent(this);
ptr->SetParent(this);
instructions_.push_back(std::move(inst));
return ptr;
}
@@ -175,9 +175,9 @@ class Function : public Value {
// 允许显式指定返回类型,便于后续扩展多种函数签名。
Function(std::string name, std::shared_ptr<Type> ret_type);
BasicBlock* CreateBlock(const std::string& name);
BasicBlock* entry();
const BasicBlock* entry() const;
const std::vector<std::unique_ptr<BasicBlock>>& blocks() const;
BasicBlock* GetEntry();
const BasicBlock* GetEntry() const;
const std::vector<std::unique_ptr<BasicBlock>>& GetBlocks() const;
private:
BasicBlock* entry_ = nullptr;
@@ -187,12 +187,12 @@ class Function : public Value {
class Module {
public:
Module() = default;
Context& context();
const Context& context() const;
Context& GetContext();
const Context& GetContext() const;
// 创建函数时显式传入返回类型,便于在 IRGen 中根据语法树信息选择类型。
Function* CreateFunction(const std::string& name,
std::shared_ptr<Type> ret_type);
const std::vector<std::unique_ptr<Function>>& functions() const;
const std::vector<std::unique_ptr<Function>>& GetFunctions() const;
private:
Context context_;
@@ -217,7 +217,7 @@ class IRBuilder {
private:
Context& ctx_;
BasicBlock* insertBlock_;
BasicBlock* insert_block_;
};
class IRPrinter {

View File

@@ -41,10 +41,10 @@ class Operand {
static Operand Imm(int value);
static Operand FrameIndex(int index);
Kind kind() const { return kind_; }
PhysReg reg() const { return reg_; }
int imm() const { return imm_; }
int frame_index() const { return imm_; }
Kind GetKind() const { return kind_; }
PhysReg GetReg() const { return reg_; }
int GetImm() const { return imm_; }
int GetFrameIndex() const { return imm_; }
private:
Operand(Kind kind, PhysReg reg, int imm);
@@ -58,8 +58,8 @@ class MachineInstr {
public:
MachineInstr(Opcode opcode, std::vector<Operand> operands = {});
Opcode opcode() const { return opcode_; }
const std::vector<Operand>& operands() const { return operands_; }
Opcode GetOpcode() const { return opcode_; }
const std::vector<Operand>& GetOperands() const { return operands_; }
private:
Opcode opcode_;
@@ -76,9 +76,9 @@ class MachineBasicBlock {
public:
explicit MachineBasicBlock(std::string name);
const std::string& name() const { return name_; }
std::vector<MachineInstr>& instructions() { return instructions_; }
const std::vector<MachineInstr>& instructions() const { return instructions_; }
const std::string& GetName() const { return name_; }
std::vector<MachineInstr>& GetInstructions() { return instructions_; }
const std::vector<MachineInstr>& GetInstructions() const { return instructions_; }
MachineInstr& Append(Opcode opcode,
std::initializer_list<Operand> operands = {});
@@ -92,17 +92,17 @@ class MachineFunction {
public:
explicit MachineFunction(std::string name);
const std::string& name() const { return name_; }
MachineBasicBlock& entry() { return entry_; }
const MachineBasicBlock& entry() const { return entry_; }
const std::string& GetName() const { return name_; }
MachineBasicBlock& GetEntry() { return entry_; }
const MachineBasicBlock& GetEntry() const { return entry_; }
int CreateFrameIndex(int size = 4);
FrameSlot& frame_slot(int index);
const FrameSlot& frame_slot(int index) const;
const std::vector<FrameSlot>& frame_slots() const { return frame_slots_; }
FrameSlot& GetFrameSlot(int index);
const FrameSlot& GetFrameSlot(int index) const;
const std::vector<FrameSlot>& GetFrameSlots() const { return frame_slots_; }
int frame_size() const { return frame_size_; }
void set_frame_size(int size) { frame_size_ = size; }
int GetFrameSize() const { return frame_size_; }
void SetFrameSize(int size) { frame_size_ = size; }
private:
std::string name_;