|
|
|
|
@ -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 {
|
|
|
|
|
|