|
|
|
|
@ -8,8 +8,47 @@
|
|
|
|
|
#include "utils/Log.h"
|
|
|
|
|
|
|
|
|
|
namespace ir {
|
|
|
|
|
User::User(std::shared_ptr<Type> ty, std::string name)
|
|
|
|
|
: Value(std::move(ty), std::move(name)) {}
|
|
|
|
|
|
|
|
|
|
size_t User::GetNumOperands() const { return operands_.size(); }
|
|
|
|
|
|
|
|
|
|
Value* User::GetOperand(size_t index) const {
|
|
|
|
|
if (index >= operands_.size()) {
|
|
|
|
|
throw std::out_of_range("User operand index out of range");
|
|
|
|
|
}
|
|
|
|
|
return operands_[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void User::SetOperand(size_t index, Value* value) {
|
|
|
|
|
if (index >= operands_.size()) {
|
|
|
|
|
throw std::out_of_range("User operand index out of range");
|
|
|
|
|
}
|
|
|
|
|
if (!value) {
|
|
|
|
|
throw std::runtime_error(FormatError("ir", "User operand 不能为空"));
|
|
|
|
|
}
|
|
|
|
|
auto* old = operands_[index];
|
|
|
|
|
if (old == value) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (old) {
|
|
|
|
|
old->RemoveUse(this, index);
|
|
|
|
|
}
|
|
|
|
|
operands_[index] = value;
|
|
|
|
|
value->AddUse(this, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void User::AddOperand(Value* value) {
|
|
|
|
|
if (!value) {
|
|
|
|
|
throw std::runtime_error(FormatError("ir", "User operand 不能为空"));
|
|
|
|
|
}
|
|
|
|
|
size_t operand_index = operands_.size();
|
|
|
|
|
operands_.push_back(value);
|
|
|
|
|
value->AddUse(this, operand_index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Instruction::Instruction(Opcode op, std::shared_ptr<Type> ty, std::string name)
|
|
|
|
|
: Value(std::move(ty), std::move(name)), opcode_(op) {}
|
|
|
|
|
: User(std::move(ty), std::move(name)), opcode_(op) {}
|
|
|
|
|
|
|
|
|
|
Opcode Instruction::GetOpcode() const { return opcode_; }
|
|
|
|
|
|
|
|
|
|
@ -21,48 +60,43 @@ void Instruction::SetParent(BasicBlock* parent) { parent_ = parent; }
|
|
|
|
|
|
|
|
|
|
BinaryInst::BinaryInst(Opcode op, std::shared_ptr<Type> ty, Value* lhs,
|
|
|
|
|
Value* rhs, std::string name)
|
|
|
|
|
: Instruction(op, std::move(ty), std::move(name)), lhs_(lhs), rhs_(rhs) {
|
|
|
|
|
: Instruction(op, std::move(ty), std::move(name)) {
|
|
|
|
|
if (op != Opcode::Add) {
|
|
|
|
|
throw std::runtime_error(FormatError("ir", "BinaryInst 当前只支持 Add"));
|
|
|
|
|
}
|
|
|
|
|
if (!lhs_ || !rhs_) {
|
|
|
|
|
if (!lhs || !rhs) {
|
|
|
|
|
throw std::runtime_error(FormatError("ir", "BinaryInst 缺少操作数"));
|
|
|
|
|
}
|
|
|
|
|
if (!type_ || !lhs_->GetType() || !rhs_->GetType()) {
|
|
|
|
|
if (!type_ || !lhs->GetType() || !rhs->GetType()) {
|
|
|
|
|
throw std::runtime_error(FormatError("ir", "BinaryInst 缺少类型信息"));
|
|
|
|
|
}
|
|
|
|
|
if (lhs_->GetType()->GetKind() != rhs_->GetType()->GetKind() ||
|
|
|
|
|
type_->GetKind() != lhs_->GetType()->GetKind()) {
|
|
|
|
|
if (lhs->GetType()->GetKind() != rhs->GetType()->GetKind() ||
|
|
|
|
|
type_->GetKind() != lhs->GetType()->GetKind()) {
|
|
|
|
|
throw std::runtime_error(FormatError("ir", "BinaryInst 类型不匹配"));
|
|
|
|
|
}
|
|
|
|
|
if (!type_->IsInt32()) {
|
|
|
|
|
throw std::runtime_error(FormatError("ir", "BinaryInst 当前只支持 i32"));
|
|
|
|
|
}
|
|
|
|
|
if (lhs_) {
|
|
|
|
|
lhs_->AddUser(this);
|
|
|
|
|
}
|
|
|
|
|
if (rhs_) {
|
|
|
|
|
rhs_->AddUser(this);
|
|
|
|
|
}
|
|
|
|
|
AddOperand(lhs);
|
|
|
|
|
AddOperand(rhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Value* BinaryInst::GetLhs() const { return lhs_; }
|
|
|
|
|
Value* BinaryInst::GetLhs() const { return GetOperand(0); }
|
|
|
|
|
|
|
|
|
|
Value* BinaryInst::GetRhs() const { return rhs_; }
|
|
|
|
|
Value* BinaryInst::GetRhs() const { return GetOperand(1); }
|
|
|
|
|
|
|
|
|
|
ReturnInst::ReturnInst(std::shared_ptr<Type> void_ty, Value* val)
|
|
|
|
|
: Instruction(Opcode::Ret, std::move(void_ty), ""),
|
|
|
|
|
value_(val) {
|
|
|
|
|
if (!value_) {
|
|
|
|
|
: Instruction(Opcode::Ret, std::move(void_ty), "") {
|
|
|
|
|
if (!val) {
|
|
|
|
|
throw std::runtime_error(FormatError("ir", "ReturnInst 缺少返回值"));
|
|
|
|
|
}
|
|
|
|
|
if (!type_ || !type_->IsVoid()) {
|
|
|
|
|
throw std::runtime_error(FormatError("ir", "ReturnInst 返回类型必须为 void"));
|
|
|
|
|
}
|
|
|
|
|
value_->AddUser(this);
|
|
|
|
|
AddOperand(val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Value* ReturnInst::GetValue() const { return value_; }
|
|
|
|
|
Value* ReturnInst::GetValue() const { return GetOperand(0); }
|
|
|
|
|
|
|
|
|
|
AllocaInst::AllocaInst(std::shared_ptr<Type> ptr_ty, std::string name)
|
|
|
|
|
: Instruction(Opcode::Alloca, std::move(ptr_ty), std::move(name)) {
|
|
|
|
|
@ -72,49 +106,46 @@ AllocaInst::AllocaInst(std::shared_ptr<Type> ptr_ty, std::string name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LoadInst::LoadInst(std::shared_ptr<Type> val_ty, Value* ptr, std::string name)
|
|
|
|
|
: Instruction(Opcode::Load, std::move(val_ty), std::move(name)),
|
|
|
|
|
ptr_(ptr) {
|
|
|
|
|
if (!ptr_) {
|
|
|
|
|
: Instruction(Opcode::Load, std::move(val_ty), std::move(name)) {
|
|
|
|
|
if (!ptr) {
|
|
|
|
|
throw std::runtime_error(FormatError("ir", "LoadInst 缺少 ptr"));
|
|
|
|
|
}
|
|
|
|
|
if (!type_ || !type_->IsInt32()) {
|
|
|
|
|
throw std::runtime_error(FormatError("ir", "LoadInst 当前只支持加载 i32"));
|
|
|
|
|
}
|
|
|
|
|
if (!ptr_->GetType() || !ptr_->GetType()->IsPtrInt32()) {
|
|
|
|
|
if (!ptr->GetType() || !ptr->GetType()->IsPtrInt32()) {
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
FormatError("ir", "LoadInst 当前只支持从 i32* 加载"));
|
|
|
|
|
}
|
|
|
|
|
ptr_->AddUser(this);
|
|
|
|
|
AddOperand(ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Value* LoadInst::GetPtr() const { return ptr_; }
|
|
|
|
|
Value* LoadInst::GetPtr() const { return GetOperand(0); }
|
|
|
|
|
|
|
|
|
|
StoreInst::StoreInst(std::shared_ptr<Type> void_ty, Value* val, Value* ptr)
|
|
|
|
|
: Instruction(Opcode::Store, std::move(void_ty), ""),
|
|
|
|
|
value_(val),
|
|
|
|
|
ptr_(ptr) {
|
|
|
|
|
if (!value_) {
|
|
|
|
|
: Instruction(Opcode::Store, std::move(void_ty), "") {
|
|
|
|
|
if (!val) {
|
|
|
|
|
throw std::runtime_error(FormatError("ir", "StoreInst 缺少 value"));
|
|
|
|
|
}
|
|
|
|
|
if (!ptr_) {
|
|
|
|
|
if (!ptr) {
|
|
|
|
|
throw std::runtime_error(FormatError("ir", "StoreInst 缺少 ptr"));
|
|
|
|
|
}
|
|
|
|
|
if (!type_ || !type_->IsVoid()) {
|
|
|
|
|
throw std::runtime_error(FormatError("ir", "StoreInst 返回类型必须为 void"));
|
|
|
|
|
}
|
|
|
|
|
if (!value_->GetType() || !value_->GetType()->IsInt32()) {
|
|
|
|
|
if (!val->GetType() || !val->GetType()->IsInt32()) {
|
|
|
|
|
throw std::runtime_error(FormatError("ir", "StoreInst 当前只支持存储 i32"));
|
|
|
|
|
}
|
|
|
|
|
if (!ptr_->GetType() || !ptr_->GetType()->IsPtrInt32()) {
|
|
|
|
|
if (!ptr->GetType() || !ptr->GetType()->IsPtrInt32()) {
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
FormatError("ir", "StoreInst 当前只支持写入 i32*"));
|
|
|
|
|
}
|
|
|
|
|
value_->AddUser(this);
|
|
|
|
|
ptr_->AddUser(this);
|
|
|
|
|
AddOperand(val);
|
|
|
|
|
AddOperand(ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Value* StoreInst::GetValue() const { return value_; }
|
|
|
|
|
Value* StoreInst::GetValue() const { return GetOperand(0); }
|
|
|
|
|
|
|
|
|
|
Value* StoreInst::GetPtr() const { return ptr_; }
|
|
|
|
|
Value* StoreInst::GetPtr() const { return GetOperand(1); }
|
|
|
|
|
|
|
|
|
|
} // namespace ir
|
|
|
|
|
|