|
|
|
|
@ -2,6 +2,7 @@
|
|
|
|
|
// 可在此基础上扩展更多类型/指令
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <iosfwd>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
#include <string>
|
|
|
|
|
@ -15,10 +16,12 @@ class Type;
|
|
|
|
|
class ConstantInt;
|
|
|
|
|
class Instruction;
|
|
|
|
|
class BasicBlock;
|
|
|
|
|
class Function;
|
|
|
|
|
|
|
|
|
|
// IR 上下文:集中管理类型、常量等共享资源,便于复用与扩展。
|
|
|
|
|
class Context {
|
|
|
|
|
public:
|
|
|
|
|
Context() = default;
|
|
|
|
|
~Context();
|
|
|
|
|
const std::shared_ptr<Type>& Void();
|
|
|
|
|
const std::shared_ptr<Type>& Int32();
|
|
|
|
|
@ -36,16 +39,14 @@ class Context {
|
|
|
|
|
int temp_index_ = -1;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Context& DefaultContext();
|
|
|
|
|
|
|
|
|
|
class Type {
|
|
|
|
|
public:
|
|
|
|
|
enum class Kind { Void, Int32, PtrInt32 };
|
|
|
|
|
explicit Type(Kind k);
|
|
|
|
|
Kind kind() const;
|
|
|
|
|
static std::shared_ptr<Type> Void();
|
|
|
|
|
static std::shared_ptr<Type> Int32();
|
|
|
|
|
static std::shared_ptr<Type> PtrInt32();
|
|
|
|
|
bool IsVoid() const;
|
|
|
|
|
bool IsInt32() const;
|
|
|
|
|
bool IsPtrInt32() const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Kind kind_;
|
|
|
|
|
@ -69,7 +70,7 @@ class Value {
|
|
|
|
|
|
|
|
|
|
class ConstantInt : public Value {
|
|
|
|
|
public:
|
|
|
|
|
explicit ConstantInt(int v);
|
|
|
|
|
ConstantInt(std::shared_ptr<Type> ty, int v);
|
|
|
|
|
int value() const { return value_; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
@ -106,7 +107,7 @@ class BinaryInst : public Instruction {
|
|
|
|
|
|
|
|
|
|
class ReturnInst : public Instruction {
|
|
|
|
|
public:
|
|
|
|
|
explicit ReturnInst(Value* val);
|
|
|
|
|
ReturnInst(std::shared_ptr<Type> void_ty, Value* val);
|
|
|
|
|
Value* value() const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
@ -115,12 +116,12 @@ class ReturnInst : public Instruction {
|
|
|
|
|
|
|
|
|
|
class AllocaInst : public Instruction {
|
|
|
|
|
public:
|
|
|
|
|
explicit AllocaInst(std::string name);
|
|
|
|
|
AllocaInst(std::shared_ptr<Type> ptr_ty, std::string name);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class LoadInst : public Instruction {
|
|
|
|
|
public:
|
|
|
|
|
LoadInst(Value* ptr, std::string name);
|
|
|
|
|
LoadInst(std::shared_ptr<Type> val_ty, Value* ptr, std::string name);
|
|
|
|
|
Value* ptr() const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
@ -129,7 +130,7 @@ class LoadInst : public Instruction {
|
|
|
|
|
|
|
|
|
|
class StoreInst : public Instruction {
|
|
|
|
|
public:
|
|
|
|
|
StoreInst(Value* val, Value* ptr);
|
|
|
|
|
StoreInst(std::shared_ptr<Type> void_ty, Value* val, Value* ptr);
|
|
|
|
|
Value* value() const;
|
|
|
|
|
Value* ptr() const;
|
|
|
|
|
|
|
|
|
|
@ -142,8 +143,12 @@ class BasicBlock {
|
|
|
|
|
public:
|
|
|
|
|
explicit BasicBlock(std::string name);
|
|
|
|
|
const std::string& name() const;
|
|
|
|
|
Function* parent() const;
|
|
|
|
|
void set_parent(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;
|
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
|
T* Append(Args&&... args) {
|
|
|
|
|
if (HasTerminator()) {
|
|
|
|
|
@ -159,7 +164,10 @@ class BasicBlock {
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::string name_;
|
|
|
|
|
Function* parent_ = nullptr;
|
|
|
|
|
std::vector<std::unique_ptr<Instruction>> instructions_;
|
|
|
|
|
std::vector<BasicBlock*> predecessors_;
|
|
|
|
|
std::vector<BasicBlock*> successors_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Function : public Value {
|
|
|
|
|
@ -178,18 +186,22 @@ class Function : public Value {
|
|
|
|
|
|
|
|
|
|
class Module {
|
|
|
|
|
public:
|
|
|
|
|
Module() = default;
|
|
|
|
|
Context& context();
|
|
|
|
|
const Context& context() const;
|
|
|
|
|
// 创建函数时显式传入返回类型,便于在 IRGen 中根据语法树信息选择类型。
|
|
|
|
|
Function* CreateFunction(const std::string& name,
|
|
|
|
|
std::shared_ptr<Type> ret_type);
|
|
|
|
|
const std::vector<std::unique_ptr<Function>>& functions() const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Context context_;
|
|
|
|
|
std::vector<std::unique_ptr<Function>> functions_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class IRBuilder {
|
|
|
|
|
public:
|
|
|
|
|
explicit IRBuilder(BasicBlock* bb);
|
|
|
|
|
IRBuilder(Context& ctx, BasicBlock* bb);
|
|
|
|
|
void SetInsertPoint(BasicBlock* bb);
|
|
|
|
|
BasicBlock* GetInsertBlock() const;
|
|
|
|
|
|
|
|
|
|
@ -204,12 +216,13 @@ class IRBuilder {
|
|
|
|
|
ReturnInst* CreateRet(Value* v);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Context& ctx_;
|
|
|
|
|
BasicBlock* insertBlock_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class IRPrinter {
|
|
|
|
|
public:
|
|
|
|
|
void Print(const Module& module);
|
|
|
|
|
void Print(const Module& module, std::ostream& os);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace ir
|
|
|
|
|
|