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.

112 lines
3.2 KiB

// IR 指令体系:
// - 二元运算/比较、load/store、call、br/condbr、ret、phi、alloca 等
// - 指令操作数与结果类型管理,支持打印与优化
#include "ir/IR.h"
#include <stdexcept>
namespace ir {
namespace {
bool IsArithmeticType(const std::shared_ptr<Type>& ty) {
return ty && ty->kind() == Type::Kind::Int32;
}
bool IsPtrInt32Type(const std::shared_ptr<Type>& ty) {
return ty && ty->kind() == Type::Kind::PtrInt32;
}
} // namespace
Instruction::Instruction(Opcode op, std::shared_ptr<Type> ty, std::string name)
: Value(std::move(ty), std::move(name)), opcode_(op) {}
Opcode Instruction::opcode() const { return opcode_; }
bool Instruction::IsTerminator() const { return opcode_ == Opcode::Ret; }
BasicBlock* Instruction::parent() const { return parent_; }
void Instruction::set_parent(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) {
if (op != Opcode::Add) {
throw std::runtime_error("BinaryInst 当前只支持 Add");
}
if (!lhs_ || !rhs_) {
throw std::runtime_error("BinaryInst 缺少操作数");
}
if (!type_ || !lhs_->type() || !rhs_->type()) {
throw std::runtime_error("BinaryInst 缺少类型信息");
}
if (lhs_->type()->kind() != rhs_->type()->kind() ||
type_->kind() != lhs_->type()->kind()) {
throw std::runtime_error("BinaryInst 类型不匹配");
}
if (!IsArithmeticType(type_)) {
throw std::runtime_error("BinaryInst 当前只支持 i32");
}
if (lhs_) {
lhs_->AddUser(this);
}
if (rhs_) {
rhs_->AddUser(this);
}
}
Value* BinaryInst::lhs() const { return lhs_; }
Value* BinaryInst::rhs() const { return rhs_; }
ReturnInst::ReturnInst(Value* val)
: Instruction(Opcode::Ret, Type::Void(), ""), value_(val) {
if (!value_) {
throw std::runtime_error("ReturnInst 缺少返回值");
}
value_->AddUser(this);
}
Value* ReturnInst::value() const { return value_; }
AllocaInst::AllocaInst(std::string name)
: Instruction(Opcode::Alloca, Type::PtrInt32(), std::move(name)) {}
LoadInst::LoadInst(Value* ptr, std::string name)
: Instruction(Opcode::Load, Type::Int32(), std::move(name)), ptr_(ptr) {
if (!ptr_) {
throw std::runtime_error("LoadInst 缺少 ptr");
}
if (!IsPtrInt32Type(ptr_->type())) {
throw std::runtime_error("LoadInst 当前只支持从 i32* 加载");
}
ptr_->AddUser(this);
}
Value* LoadInst::ptr() const { return ptr_; }
StoreInst::StoreInst(Value* val, Value* ptr)
: Instruction(Opcode::Store, Type::Void(), ""), value_(val), ptr_(ptr) {
if (!value_) {
throw std::runtime_error("StoreInst 缺少 value");
}
if (!ptr_) {
throw std::runtime_error("StoreInst 缺少 ptr");
}
if (!IsArithmeticType(value_->type())) {
throw std::runtime_error("StoreInst 当前只支持存储 i32");
}
if (!IsPtrInt32Type(ptr_->type())) {
throw std::runtime_error("StoreInst 当前只支持写入 i32*");
}
value_->AddUser(this);
ptr_->AddUser(this);
}
Value* StoreInst::value() const { return value_; }
Value* StoreInst::ptr() const { return ptr_; }
} // namespace ir