// 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

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);
  }
}

ReturnInst::ReturnInst(Value* val)
    : Instruction(Opcode::Ret, Type::Void(), ""), value_(val) {
  if (!value_) {
    throw std::runtime_error("ReturnInst 缺少返回值");
  }
  value_->AddUser(this);
}

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);
}

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);
}

}  // namespace ir
