refactor(irgen): IR改成alloca和store形式
This commit is contained in:
35
src/ir/IR.h
35
src/ir/IR.h
@@ -19,6 +19,7 @@ class Context {
|
||||
~Context();
|
||||
const std::shared_ptr<Type>& Void();
|
||||
const std::shared_ptr<Type>& Int32();
|
||||
const std::shared_ptr<Type>& PtrInt32();
|
||||
// 去重创建 i32 常量。
|
||||
ConstantInt* GetConstInt(int v);
|
||||
// 生成临时名称,如 %t0、%t1 ...
|
||||
@@ -27,6 +28,7 @@ class Context {
|
||||
private:
|
||||
std::shared_ptr<Type> void_;
|
||||
std::shared_ptr<Type> int32_;
|
||||
std::shared_ptr<Type> ptr_i32_;
|
||||
std::unordered_map<int, std::unique_ptr<ConstantInt>> const_ints_;
|
||||
int temp_index_ = 0;
|
||||
};
|
||||
@@ -35,11 +37,12 @@ Context& DefaultContext();
|
||||
|
||||
class Type {
|
||||
public:
|
||||
enum class Kind { Void, Int32 };
|
||||
enum class Kind { Void, Int32, PtrInt32 };
|
||||
explicit Type(Kind k) : kind_(k) {}
|
||||
Kind kind() const { return kind_; }
|
||||
static std::shared_ptr<Type> Void();
|
||||
static std::shared_ptr<Type> Int32();
|
||||
static std::shared_ptr<Type> PtrInt32();
|
||||
|
||||
private:
|
||||
Kind kind_;
|
||||
@@ -68,7 +71,7 @@ class ConstantInt : public Value {
|
||||
int value_{};
|
||||
};
|
||||
|
||||
enum class Opcode { Add, Sub, Mul, Ret };
|
||||
enum class Opcode { Add, Sub, Mul, Alloca, Load, Store, Ret };
|
||||
|
||||
class Instruction : public Value {
|
||||
public:
|
||||
@@ -101,6 +104,31 @@ class ReturnInst : public Instruction {
|
||||
Value* value_;
|
||||
};
|
||||
|
||||
class AllocaInst : public Instruction {
|
||||
public:
|
||||
explicit AllocaInst(std::string name);
|
||||
};
|
||||
|
||||
class LoadInst : public Instruction {
|
||||
public:
|
||||
LoadInst(Value* ptr, std::string name);
|
||||
Value* ptr() const { return ptr_; }
|
||||
|
||||
private:
|
||||
Value* ptr_;
|
||||
};
|
||||
|
||||
class StoreInst : public Instruction {
|
||||
public:
|
||||
StoreInst(Value* val, Value* ptr);
|
||||
Value* value() const { return value_; }
|
||||
Value* ptr() const { return ptr_; }
|
||||
|
||||
private:
|
||||
Value* value_;
|
||||
Value* ptr_;
|
||||
};
|
||||
|
||||
class BasicBlock {
|
||||
public:
|
||||
explicit BasicBlock(std::string name) : name_(std::move(name)) {}
|
||||
@@ -158,6 +186,9 @@ class IRBuilder {
|
||||
BinaryInst* CreateAdd(Value* lhs, Value* rhs, const std::string& name) {
|
||||
return CreateBinary(Opcode::Add, lhs, rhs, name);
|
||||
}
|
||||
AllocaInst* CreateAllocaI32(const std::string& name);
|
||||
LoadInst* CreateLoad(Value* ptr, const std::string& name);
|
||||
StoreInst* CreateStore(Value* val, Value* ptr);
|
||||
ReturnInst* CreateRet(Value* v);
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user