You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
686 lines
27 KiB
686 lines
27 KiB
// IR_try.h (修正版)
|
|
#pragma once
|
|
|
|
#include "utils.h"
|
|
#include <iostream>
|
|
#include <iosfwd>
|
|
#include <memory>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <utility>
|
|
#include <type_traits>
|
|
#include <vector>
|
|
#include <map>
|
|
|
|
namespace ir {
|
|
|
|
// 前向声明
|
|
class Value;
|
|
class User;
|
|
class BasicBlock;
|
|
class Function;
|
|
class Instruction;
|
|
class ConstantInt;
|
|
class ConstantFloat;
|
|
class ConstantI1;
|
|
class ConstantArrayValue;
|
|
class Type;
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Use
|
|
// -----------------------------------------------------------------------------
|
|
class Use {
|
|
public:
|
|
Use() = default;
|
|
Use(Value* value, User* user, size_t operand_index)
|
|
: value_(value), user_(user), operand_index_(operand_index) {}
|
|
|
|
Value* GetValue() const { return value_; }
|
|
User* GetUser() const { return user_; }
|
|
size_t GetOperandIndex() const { return operand_index_; }
|
|
|
|
void SetValue(Value* value) { value_ = value; }
|
|
void SetUser(User* user) { user_ = user; }
|
|
void SetOperandIndex(size_t operand_index) { operand_index_ = operand_index; }
|
|
|
|
private:
|
|
Value* value_ = nullptr;
|
|
User* user_ = nullptr;
|
|
size_t operand_index_ = 0;
|
|
};
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Context
|
|
// -----------------------------------------------------------------------------
|
|
class Context {
|
|
public:
|
|
Context() = default;
|
|
~Context();
|
|
|
|
ConstantInt* GetConstInt(int v);
|
|
std::string NextTemp();
|
|
|
|
private:
|
|
std::unordered_map<int, std::unique_ptr<ConstantInt>> const_ints_;
|
|
int temp_index_ = -1;
|
|
};
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Type
|
|
// -----------------------------------------------------------------------------
|
|
class Type {
|
|
public:
|
|
enum class Kind { Void, Int1, Int32, Float, Label, Function, PtrInt32, Array };
|
|
|
|
explicit Type(Kind k);
|
|
|
|
static const std::shared_ptr<Type>& GetVoidType();
|
|
static const std::shared_ptr<Type>& GetInt1Type();
|
|
static const std::shared_ptr<Type>& GetInt32Type();
|
|
static const std::shared_ptr<Type>& GetFloatType();
|
|
static const std::shared_ptr<Type>& GetLabelType();
|
|
static const std::shared_ptr<Type>& GetFunctionType();
|
|
static const std::shared_ptr<Type>& GetBoolType();
|
|
static const std::shared_ptr<Type>& GetPtrInt32Type();
|
|
static const std::shared_ptr<Type>& GetArrayType();
|
|
|
|
Kind GetKind() const { return kind_; }
|
|
bool IsVoid() const { return kind_ == Kind::Void; }
|
|
bool IsInt1() const { return kind_ == Kind::Int1; }
|
|
bool IsInt32() const { return kind_ == Kind::Int32; }
|
|
bool IsFloat() const { return kind_ == Kind::Float; }
|
|
bool IsLabel() const { return kind_ == Kind::Label; }
|
|
bool IsFunction() const { return kind_ == Kind::Function; }
|
|
bool IsBool() const { return kind_ == Kind::Int1; } // 通常与 Int1 相同
|
|
bool IsPtrInt32() const { return kind_ == Kind::PtrInt32; }
|
|
bool IsArray() const { return kind_ == Kind::Array; }
|
|
|
|
int GetSize() const;
|
|
void Print(std::ostream& os) const;
|
|
|
|
template <typename T>
|
|
std::enable_if_t<std::is_base_of_v<Type, T>, T*> As() const {
|
|
return dynamic_cast<T*>(const_cast<Type*>(this));
|
|
}
|
|
|
|
private:
|
|
Kind kind_;
|
|
};
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Value
|
|
// -----------------------------------------------------------------------------
|
|
class Value {
|
|
public:
|
|
Value(std::shared_ptr<Type> ty, std::string name);
|
|
virtual ~Value() = default;
|
|
|
|
const std::shared_ptr<Type>& GetType() const { return type_; }
|
|
const std::string& GetName() const { return name_; }
|
|
void SetName(std::string n) { name_ = std::move(n); }
|
|
|
|
bool IsVoid() const { return type_->IsVoid(); }
|
|
bool IsInt32() const { return type_->IsInt32(); }
|
|
bool IsPtrInt32() const { return type_->IsPtrInt32(); }
|
|
bool IsFloat() const { return type_->IsFloat(); }
|
|
bool IsBool() const { return type_->IsBool(); }
|
|
bool IsArray() const { return type_->IsArray(); }
|
|
bool IsLabel() const { return type_->IsLabel(); }
|
|
|
|
// 派生类身份判断(通过虚函数或枚举,这里使用虚函数)
|
|
virtual bool IsConstant() const { return false; }
|
|
virtual bool IsInstruction() const { return false; }
|
|
virtual bool IsUser() const { return false; }
|
|
virtual bool IsFunction() const { return false; }
|
|
|
|
void AddUse(User* user, size_t operand_index);
|
|
void RemoveUse(User* user, size_t operand_index);
|
|
const std::vector<Use>& GetUses() const { return uses_; }
|
|
void ReplaceAllUsesWith(Value* new_value);
|
|
void Print(std::ostream& os) const;
|
|
|
|
protected:
|
|
std::shared_ptr<Type> type_;
|
|
std::string name_;
|
|
std::vector<Use> uses_;
|
|
};
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// isa / dyncast 辅助函数
|
|
// -----------------------------------------------------------------------------
|
|
template <typename T>
|
|
inline std::enable_if_t<std::is_base_of_v<Value, T>, bool> isa(const Value* value) {
|
|
return T::classof(value);
|
|
}
|
|
|
|
template <typename T>
|
|
inline std::enable_if_t<std::is_base_of_v<Value, T>, T*> dyncast(Value* value) {
|
|
return isa<T>(value) ? static_cast<T*>(value) : nullptr;
|
|
}
|
|
|
|
template <typename T>
|
|
inline std::enable_if_t<std::is_base_of_v<Value, T>, const T*> dyncast(const Value* value) {
|
|
return isa<T>(value) ? static_cast<const T*>(value) : nullptr;
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// ConstantValue 系列
|
|
// -----------------------------------------------------------------------------
|
|
class ConstantValue : public Value {
|
|
public:
|
|
ConstantValue(std::shared_ptr<Type> ty, std::string name = "");
|
|
bool IsConstant() const override final { return true; }
|
|
};
|
|
|
|
class ConstantInt : public ConstantValue {
|
|
public:
|
|
ConstantInt(std::shared_ptr<Type> ty, int v);
|
|
int GetValue() const { return value_; }
|
|
static bool classof(const Value* v) { return v->IsConstant() && dynamic_cast<const ConstantInt*>(v) != nullptr; }
|
|
private:
|
|
int value_;
|
|
};
|
|
|
|
class ConstantFloat : public ConstantValue {
|
|
public:
|
|
ConstantFloat(std::shared_ptr<Type> ty, float v);
|
|
float GetValue() const { return value_; }
|
|
static bool classof(const Value* v) { return v->IsConstant() && dynamic_cast<const ConstantFloat*>(v) != nullptr; }
|
|
private:
|
|
float value_;
|
|
};
|
|
|
|
class ConstantI1 : public ConstantValue {
|
|
public:
|
|
ConstantI1(std::shared_ptr<Type> ty, bool v);
|
|
bool GetValue() const { return value_; }
|
|
static bool classof(const Value* v) { return v->IsConstant() && dynamic_cast<const ConstantI1*>(v) != nullptr; }
|
|
private:
|
|
bool value_;
|
|
};
|
|
|
|
class ConstantArrayValue : public Value {
|
|
public:
|
|
ConstantArrayValue(std::shared_ptr<Type> elemType,
|
|
const std::vector<Value*>& elements,
|
|
const std::vector<size_t>& dims,
|
|
const std::string& name = "");
|
|
const std::vector<Value*>& GetElements() const { return elements_; }
|
|
const std::vector<size_t>& GetDims() const { return dims_; }
|
|
void Print(std::ostream& os) const;
|
|
static bool classof(const Value* v) { return v->IsConstant() && dynamic_cast<const ConstantArrayValue*>(v) != nullptr; }
|
|
private:
|
|
std::vector<Value*> elements_;
|
|
std::vector<size_t> dims_;
|
|
};
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Opcode 枚举
|
|
// -----------------------------------------------------------------------------
|
|
enum class Opcode {
|
|
Add, Sub, Mul, Div, Rem, FAdd, FSub, FMul, FDiv, FRem,
|
|
And, Or, Xor, Shl, AShr, LShr,
|
|
ICmpEQ, ICmpNE, ICmpLT, ICmpGT, ICmpLE, ICmpGE,
|
|
FCmpEQ, FCmpNE, FCmpLT, FCmpGT, FCmpLE, FCmpGE,
|
|
Neg, Not, FNeg, FtoI, IToF,
|
|
Call, CondBr, Br, Return, Unreachable,
|
|
Alloca, Load, Store, Memset,
|
|
GetElementPtr, Phi, Zext
|
|
};
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// User
|
|
// -----------------------------------------------------------------------------
|
|
class User : public Value {
|
|
public:
|
|
User(std::shared_ptr<Type> ty, std::string name);
|
|
bool IsUser() const override final { return true; }
|
|
|
|
size_t GetNumOperands() const { return operands_.size(); }
|
|
Value* GetOperand(size_t index) const;
|
|
void SetOperand(size_t index, Value* value);
|
|
|
|
void AddOperand(Value* value);
|
|
void AddOperands(const std::vector<Value*>& values);
|
|
void RemoveOperand(size_t index);
|
|
void ClearAllOperands();
|
|
|
|
protected:
|
|
std::vector<Use> operands_;
|
|
};
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// GlobalValue
|
|
// -----------------------------------------------------------------------------
|
|
class GlobalValue : public User {
|
|
public:
|
|
GlobalValue(std::shared_ptr<Type> type, const std::string& name,
|
|
bool isConst = false, Value* init = nullptr);
|
|
GlobalValue(std::shared_ptr<Type> type, const std::string& name,
|
|
const std::vector<Value*>& dims,
|
|
bool isConst = false, Value* init = nullptr);
|
|
|
|
bool IsConstant() const { return isConst_; }
|
|
bool HasInitializer() const { return init_ != nullptr; }
|
|
Value* GetInitializer() const { return init_; }
|
|
size_t GetNumDims() const { return dims_.size(); }
|
|
Value* GetDim(size_t i) const { return dims_[i]; }
|
|
|
|
void SetConstant(bool c) { isConst_ = c; }
|
|
void SetInitializer(Value* v) { init_ = v; }
|
|
|
|
static bool classof(const Value* v) { return v->IsUser() && dynamic_cast<const GlobalValue*>(v) != nullptr; }
|
|
|
|
private:
|
|
bool isConst_;
|
|
Value* init_;
|
|
std::vector<Value*> dims_;
|
|
};
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Instruction 及其子类
|
|
// -----------------------------------------------------------------------------
|
|
class Instruction : public User {
|
|
public:
|
|
Instruction(Opcode op, std::shared_ptr<Type> ty,
|
|
BasicBlock* parent = nullptr, const std::string& name = "");
|
|
bool IsInstruction() const override final { return true; }
|
|
|
|
Opcode GetOpcode() const { return opcode_; }
|
|
bool IsTerminator() const {
|
|
return opcode_ == Opcode::Br || opcode_ == Opcode::CondBr ||
|
|
opcode_ == Opcode::Return || opcode_ == Opcode::Unreachable;
|
|
}
|
|
BasicBlock* GetParent() const { return parent_; }
|
|
void SetParent(BasicBlock* parent) { parent_ = parent; }
|
|
|
|
static bool classof(const Value* v) { return v->IsInstruction(); }
|
|
|
|
private:
|
|
Opcode opcode_;
|
|
BasicBlock* parent_;
|
|
};
|
|
|
|
class BinaryInst : public Instruction {
|
|
public:
|
|
BinaryInst(Opcode op, std::shared_ptr<Type> ty, Value* lhs, Value* rhs,
|
|
BasicBlock* parent = nullptr, const std::string& name = "");
|
|
Value* GetLhs() const { return GetOperand(0); }
|
|
Value* GetRhs() const { return GetOperand(1); }
|
|
static bool classof(const Value* v) {
|
|
return Instruction::classof(v) && static_cast<const Instruction*>(v)->GetOpcode() >= Opcode::Add &&
|
|
static_cast<const Instruction*>(v)->GetOpcode() <= Opcode::FCmpGE;
|
|
}
|
|
};
|
|
|
|
class UnaryInst : public Instruction {
|
|
public:
|
|
UnaryInst(Opcode op, std::shared_ptr<Type> ty, Value* operand,
|
|
BasicBlock* parent = nullptr, const std::string& name = "");
|
|
Value* GetOprd() const { return GetOperand(0); }
|
|
static bool classof(const Value* v) {
|
|
return Instruction::classof(v) && static_cast<const Instruction*>(v)->GetOpcode() >= Opcode::Neg &&
|
|
static_cast<const Instruction*>(v)->GetOpcode() <= Opcode::IToF;
|
|
}
|
|
};
|
|
|
|
class ReturnInst : public Instruction {
|
|
public:
|
|
ReturnInst(Value* val = nullptr, BasicBlock* parent = nullptr);
|
|
bool HasReturnValue() const { return GetNumOperands() > 0; }
|
|
Value* GetReturnValue() const { return HasReturnValue() ? GetOperand(0) : nullptr; }
|
|
static bool classof(const Value* v) {
|
|
return Instruction::classof(v) && static_cast<const Instruction*>(v)->GetOpcode() == Opcode::Return;
|
|
}
|
|
};
|
|
|
|
class AllocaInst : public Instruction {
|
|
public:
|
|
AllocaInst(std::shared_ptr<Type> elemType, BasicBlock* parent = nullptr,
|
|
const std::string& name = "");
|
|
AllocaInst(std::shared_ptr<Type> elemType, const std::vector<Value*>& dims,
|
|
BasicBlock* parent = nullptr, const std::string& name = "");
|
|
size_t GetNumDims() const { return dims_.size(); }
|
|
Value* GetDim(size_t i) const { return dims_[i]; }
|
|
static bool classof(const Value* v) {
|
|
return Instruction::classof(v) && static_cast<const Instruction*>(v)->GetOpcode() == Opcode::Alloca;
|
|
}
|
|
private:
|
|
std::vector<Value*> dims_;
|
|
};
|
|
|
|
class LoadInst : public Instruction {
|
|
public:
|
|
LoadInst(Value* ptr, BasicBlock* parent = nullptr, const std::string& name = "");
|
|
Value* GetPtr() const { return GetOperand(0); }
|
|
static bool classof(const Value* v) {
|
|
return Instruction::classof(v) && static_cast<const Instruction*>(v)->GetOpcode() == Opcode::Load;
|
|
}
|
|
};
|
|
|
|
class StoreInst : public Instruction {
|
|
public:
|
|
StoreInst(Value* val, Value* ptr, BasicBlock* parent = nullptr);
|
|
Value* GetValue() const { return GetOperand(0); }
|
|
Value* GetPtr() const { return GetOperand(1); }
|
|
static bool classof(const Value* v) {
|
|
return Instruction::classof(v) && static_cast<const Instruction*>(v)->GetOpcode() == Opcode::Store;
|
|
}
|
|
};
|
|
|
|
class UncondBrInst : public Instruction {
|
|
public:
|
|
UncondBrInst(BasicBlock* dest, const std::vector<Value*>& args = {},
|
|
BasicBlock* parent = nullptr);
|
|
BasicBlock* GetDest() const { return dyncast<BasicBlock>(GetOperand(0)); }
|
|
std::vector<Value*> GetArguments() const {
|
|
std::vector<Value*> result;
|
|
for (size_t i = 1; i < GetNumOperands(); ++i)
|
|
result.push_back(GetOperand(i));
|
|
return result;
|
|
}
|
|
static bool classof(const Value* v) {
|
|
return Instruction::classof(v) && static_cast<const Instruction*>(v)->GetOpcode() == Opcode::Br;
|
|
}
|
|
};
|
|
|
|
class CondBrInst : public Instruction {
|
|
public:
|
|
CondBrInst(Value* cond, BasicBlock* thenBlock, BasicBlock* elseBlock,
|
|
const std::vector<Value*>& thenArgs = {},
|
|
const std::vector<Value*>& elseArgs = {},
|
|
BasicBlock* parent = nullptr);
|
|
Value* GetCondition() const { return GetOperand(0); }
|
|
BasicBlock* GetThenBlock() const { return dyncast<BasicBlock>(GetOperand(1)); }
|
|
BasicBlock* GetElseBlock() const { return dyncast<BasicBlock>(GetOperand(2)); }
|
|
std::vector<Value*> GetThenArguments() const {
|
|
std::vector<Value*> result;
|
|
size_t num = GetThenBlock()->GetNumArguments();
|
|
for (size_t i = 0; i < num; ++i)
|
|
result.push_back(GetOperand(3 + i));
|
|
return result;
|
|
}
|
|
std::vector<Value*> GetElseArguments() const {
|
|
std::vector<Value*> result;
|
|
size_t num = GetThenBlock()->GetNumArguments();
|
|
size_t start = 3 + num;
|
|
for (size_t i = 0; i < GetElseBlock()->GetNumArguments(); ++i)
|
|
result.push_back(GetOperand(start + i));
|
|
return result;
|
|
}
|
|
static bool classof(const Value* v) {
|
|
return Instruction::classof(v) && static_cast<const Instruction*>(v)->GetOpcode() == Opcode::CondBr;
|
|
}
|
|
};
|
|
|
|
class UnreachableInst : public Instruction {
|
|
public:
|
|
explicit UnreachableInst(BasicBlock* parent = nullptr);
|
|
static bool classof(const Value* v) {
|
|
return Instruction::classof(v) && static_cast<const Instruction*>(v)->GetOpcode() == Opcode::Unreachable;
|
|
}
|
|
};
|
|
|
|
class CallInst : public Instruction {
|
|
public:
|
|
CallInst(Function* callee, const std::vector<Value*>& args = {},
|
|
BasicBlock* parent = nullptr, const std::string& name = "");
|
|
Function* GetCallee() const { return dyncast<Function>(GetOperand(0)); }
|
|
std::vector<Value*> GetArguments() const {
|
|
std::vector<Value*> result;
|
|
for (size_t i = 1; i < GetNumOperands(); ++i)
|
|
result.push_back(GetOperand(i));
|
|
return result;
|
|
}
|
|
static bool classof(const Value* v) {
|
|
return Instruction::classof(v) && static_cast<const Instruction*>(v)->GetOpcode() == Opcode::Call;
|
|
}
|
|
};
|
|
|
|
class GetElementPtrInst : public Instruction {
|
|
public:
|
|
GetElementPtrInst(Value* ptr, const std::vector<Value*>& indices,
|
|
const std::vector<size_t>& dims = {},
|
|
const std::vector<size_t>& curDims = {},
|
|
BasicBlock* parent = nullptr, const std::string& name = "");
|
|
Value* GetPointer() const { return GetOperand(0); }
|
|
size_t GetNumIndices() const { return GetNumOperands() - 1; }
|
|
Value* GetIndex(size_t i) const { return GetOperand(i + 1); }
|
|
const std::vector<size_t>& GetDims() const { return dims_; }
|
|
const std::vector<size_t>& GetCurDims() const { return curDims_; }
|
|
static bool classof(const Value* v) {
|
|
return Instruction::classof(v) && static_cast<const Instruction*>(v)->GetOpcode() == Opcode::GetElementPtr;
|
|
}
|
|
private:
|
|
std::vector<size_t> dims_;
|
|
std::vector<size_t> curDims_;
|
|
};
|
|
|
|
class PhiInst : public Instruction {
|
|
public:
|
|
PhiInst(std::shared_ptr<Type> type, BasicBlock* parent = nullptr,
|
|
const std::string& name = "");
|
|
void AddIncoming(Value* value, BasicBlock* block);
|
|
int GetNumIncomings() const { return static_cast<int>(GetNumOperands() / 2); }
|
|
Value* GetIncomingValue(int i) const { return GetOperand(2 * i); }
|
|
BasicBlock* GetIncomingBlock(int i) const { return dyncast<BasicBlock>(GetOperand(2 * i + 1)); }
|
|
static bool classof(const Value* v) {
|
|
return Instruction::classof(v) && static_cast<const Instruction*>(v)->GetOpcode() == Opcode::Phi;
|
|
}
|
|
};
|
|
|
|
class ZextInst : public Instruction {
|
|
public:
|
|
ZextInst(Value* val, std::shared_ptr<Type> targetType,
|
|
BasicBlock* parent = nullptr, const std::string& name = "");
|
|
Value* GetValue() const { return GetOperand(0); }
|
|
static bool classof(const Value* v) {
|
|
return Instruction::classof(v) && static_cast<const Instruction*>(v)->GetOpcode() == Opcode::Zext;
|
|
}
|
|
};
|
|
|
|
class MemsetInst : public Instruction {
|
|
public:
|
|
MemsetInst(Value* dst, Value* val, Value* len, Value* isVolatile,
|
|
BasicBlock* parent = nullptr);
|
|
Value* GetDest() const { return GetOperand(0); }
|
|
Value* GetValue() const { return GetOperand(1); }
|
|
Value* GetLength() const { return GetOperand(2); }
|
|
Value* GetIsVolatile() const { return GetOperand(3); }
|
|
static bool classof(const Value* v) {
|
|
return Instruction::classof(v) && static_cast<const Instruction*>(v)->GetOpcode() == Opcode::Memset;
|
|
}
|
|
};
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// BasicBlock
|
|
// -----------------------------------------------------------------------------
|
|
class BasicBlock : public Value {
|
|
public:
|
|
explicit BasicBlock(const std::string& name);
|
|
BasicBlock(Function* parent, const std::string& name);
|
|
|
|
Function* GetParent() const { return parent_; }
|
|
void SetParent(Function* parent) { parent_ = parent; }
|
|
|
|
bool HasTerminator() const;
|
|
const std::vector<std::unique_ptr<Instruction>>& GetInstructions() const { return instructions_; }
|
|
|
|
void AddPredecessor(BasicBlock* pred);
|
|
void AddSuccessor(BasicBlock* succ);
|
|
void RemovePredecessor(BasicBlock* pred);
|
|
void RemoveSuccessor(BasicBlock* succ);
|
|
const std::vector<BasicBlock*>& GetPredecessors() const { return predecessors_; }
|
|
const std::vector<BasicBlock*>& GetSuccessors() const { return successors_; }
|
|
|
|
template <typename T, typename... Args>
|
|
T* Append(Args&&... args) {
|
|
if (HasTerminator()) {
|
|
throw std::runtime_error("BasicBlock already has terminator");
|
|
}
|
|
auto inst = std::make_unique<T>(std::forward<Args>(args)...);
|
|
auto* ptr = inst.get();
|
|
ptr->SetParent(this);
|
|
instructions_.push_back(std::move(inst));
|
|
return ptr;
|
|
}
|
|
|
|
static bool classof(const Value* v) { return dynamic_cast<const BasicBlock*>(v) != nullptr; }
|
|
size_t GetNumArguments() const { return 0; } // 当前版本无参数
|
|
|
|
private:
|
|
Function* parent_;
|
|
std::vector<std::unique_ptr<Instruction>> instructions_;
|
|
std::vector<BasicBlock*> predecessors_;
|
|
std::vector<BasicBlock*> successors_;
|
|
};
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Function
|
|
// -----------------------------------------------------------------------------
|
|
class Function : public Value {
|
|
public:
|
|
Function(std::string name, std::shared_ptr<Type> ret_type);
|
|
Function(std::string name, std::shared_ptr<Type> ret_type,
|
|
const std::vector<std::shared_ptr<Type>>& param_types);
|
|
|
|
bool IsFunction() const override final { return true; }
|
|
|
|
std::shared_ptr<Type> GetReturnType() const { return GetType(); }
|
|
const std::vector<std::shared_ptr<Type>>& GetParamTypes() const { return param_types_; }
|
|
|
|
BasicBlock* GetEntryBlock() const { return entry_; }
|
|
void SetEntryBlock(BasicBlock* bb);
|
|
|
|
BasicBlock* CreateBlock(const std::string& name);
|
|
BasicBlock* AddBlock(std::unique_ptr<BasicBlock> block);
|
|
const std::vector<std::unique_ptr<BasicBlock>>& GetBlocks() const { return blocks_; }
|
|
|
|
static bool classof(const Value* v) { return v->IsFunction(); }
|
|
|
|
private:
|
|
std::vector<std::shared_ptr<Type>> param_types_;
|
|
BasicBlock* entry_;
|
|
std::vector<std::unique_ptr<BasicBlock>> blocks_;
|
|
};
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Module
|
|
// -----------------------------------------------------------------------------
|
|
class Module {
|
|
public:
|
|
Module() = default;
|
|
|
|
Context& GetContext() { return context_; }
|
|
const Context& GetContext() const { return context_; }
|
|
|
|
Function* CreateFunction(const std::string& name, std::shared_ptr<Type> ret_type);
|
|
Function* CreateFunction(const std::string& name, std::shared_ptr<Type> ret_type,
|
|
const std::vector<std::shared_ptr<Type>>& param_types);
|
|
Function* GetFunction(const std::string& name) const;
|
|
const std::vector<std::unique_ptr<Function>>& GetFunctions() const { return functions_; }
|
|
|
|
GlobalValue* CreateGlobalValue(const std::string& name, std::shared_ptr<Type> type,
|
|
bool isConst = false, Value* init = nullptr);
|
|
GlobalValue* GetGlobalValue(const std::string& name) const;
|
|
const std::vector<std::unique_ptr<GlobalValue>>& GetGlobalValues() const { return globals_; }
|
|
|
|
private:
|
|
Context context_;
|
|
std::vector<std::unique_ptr<Function>> functions_;
|
|
std::map<std::string, Function*> function_map_;
|
|
std::vector<std::unique_ptr<GlobalValue>> globals_;
|
|
std::map<std::string, GlobalValue*> global_map_;
|
|
};
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// IRBuilder
|
|
// -----------------------------------------------------------------------------
|
|
class IRBuilder {
|
|
public:
|
|
IRBuilder(Context& ctx, BasicBlock* bb);
|
|
|
|
void SetInsertPoint(BasicBlock* bb);
|
|
BasicBlock* GetInsertBlock() const { return insert_block_; }
|
|
|
|
ConstantInt* CreateConstInt(int v);
|
|
ConstantFloat* CreateConstFloat(float v);
|
|
ConstantI1* CreateConstBool(bool v);
|
|
ConstantArrayValue* CreateConstArray(std::shared_ptr<Type> elem_type,
|
|
const std::vector<Value*>& elements,
|
|
const std::vector<size_t>& dims,
|
|
const std::string& name = "");
|
|
|
|
BinaryInst* CreateBinary(Opcode op, Value* lhs, Value* rhs,
|
|
const std::string& name = "");
|
|
BinaryInst* CreateAdd(Value* lhs, Value* rhs, const std::string& name = "");
|
|
BinaryInst* CreateSub(Value* lhs, Value* rhs, const std::string& name = "");
|
|
BinaryInst* CreateMul(Value* lhs, Value* rhs, const std::string& name = "");
|
|
BinaryInst* CreateDiv(Value* lhs, Value* rhs, const std::string& name = "");
|
|
BinaryInst* CreateRem(Value* lhs, Value* rhs, const std::string& name = "");
|
|
BinaryInst* CreateAnd(Value* lhs, Value* rhs, const std::string& name = "");
|
|
BinaryInst* CreateOr(Value* lhs, Value* rhs, const std::string& name = "");
|
|
BinaryInst* CreateXor(Value* lhs, Value* rhs, const std::string& name = "");
|
|
BinaryInst* CreateShl(Value* lhs, Value* rhs, const std::string& name = "");
|
|
BinaryInst* CreateAShr(Value* lhs, Value* rhs, const std::string& name = "");
|
|
BinaryInst* CreateLShr(Value* lhs, Value* rhs, const std::string& name = "");
|
|
BinaryInst* CreateICmp(Opcode op, Value* lhs, Value* rhs, const std::string& name = "");
|
|
BinaryInst* CreateFCmp(Opcode op, Value* lhs, Value* rhs, const std::string& name = "");
|
|
|
|
UnaryInst* CreateNeg(Value* operand, const std::string& name = "");
|
|
UnaryInst* CreateNot(Value* operand, const std::string& name = "");
|
|
UnaryInst* CreateFNeg(Value* operand, const std::string& name = "");
|
|
UnaryInst* CreateFtoI(Value* operand, const std::string& name = "");
|
|
UnaryInst* CreateIToF(Value* operand, const std::string& name = "");
|
|
|
|
AllocaInst* CreateAlloca(std::shared_ptr<Type> elem_type, const std::string& name = "");
|
|
AllocaInst* CreateAllocaArray(std::shared_ptr<Type> elem_type, const std::vector<Value*>& dims,
|
|
const std::string& name = "");
|
|
LoadInst* CreateLoad(Value* ptr, const std::string& name = "");
|
|
StoreInst* CreateStore(Value* val, Value* ptr);
|
|
|
|
UncondBrInst* CreateBr(BasicBlock* dest, const std::vector<Value*>& args = {});
|
|
CondBrInst* CreateCondBr(Value* cond, BasicBlock* then_bb, BasicBlock* else_bb,
|
|
const std::vector<Value*>& then_args = {},
|
|
const std::vector<Value*>& else_args = {});
|
|
ReturnInst* CreateRet(Value* val = nullptr);
|
|
UnreachableInst* CreateUnreachable();
|
|
|
|
CallInst* CreateCall(Function* callee, const std::vector<Value*>& args,
|
|
const std::string& name = "");
|
|
|
|
GetElementPtrInst* CreateGEP(Value* ptr, const std::vector<Value*>& indices,
|
|
const std::vector<size_t>& dims = {},
|
|
const std::vector<size_t>& cur_dims = {},
|
|
const std::string& name = "");
|
|
PhiInst* CreatePhi(std::shared_ptr<Type> type, const std::string& name = "");
|
|
ZextInst* CreateZext(Value* val, std::shared_ptr<Type> target_type, const std::string& name = "");
|
|
MemsetInst* CreateMemset(Value* dst, Value* val, Value* len, Value* is_volatile);
|
|
|
|
private:
|
|
Context& ctx_;
|
|
BasicBlock* insert_block_;
|
|
};
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// IRPrinter
|
|
// -----------------------------------------------------------------------------
|
|
class IRPrinter {
|
|
public:
|
|
void Print(const Module& module, std::ostream& os);
|
|
};
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// 输出运算符重载
|
|
// -----------------------------------------------------------------------------
|
|
inline std::ostream& operator<<(std::ostream& os, const Type& type) {
|
|
type.Print(os);
|
|
return os;
|
|
}
|
|
inline std::ostream& operator<<(std::ostream& os, const Value& value) {
|
|
value.Print(os);
|
|
return os;
|
|
}
|
|
|
|
} // namespace ir
|