// IR 指令体系：
// - 二元运算/比较、load/store、call、br/condbr、ret、phi、alloca 等
// - 指令操作数与结果类型管理，支持打印与优化
#include "ir/IR.h"

#include <stdexcept>

namespace ir {
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 (!type_->IsInt32()) {
    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(std::shared_ptr<Type> void_ty, Value* val)
    : Instruction(Opcode::Ret, std::move(void_ty), ""),
      value_(val) {
  if (!value_) {
    throw std::runtime_error("ReturnInst 缺少返回值");
  }
  if (!type_ || !type_->IsVoid()) {
    throw std::runtime_error("ReturnInst 返回类型必须为 void");
  }
  value_->AddUser(this);
}

Value* ReturnInst::value() const { return value_; }

AllocaInst::AllocaInst(std::shared_ptr<Type> ptr_ty, std::string name)
    : Instruction(Opcode::Alloca, std::move(ptr_ty), std::move(name)) {
  if (!type_ || !type_->IsPtrInt32()) {
    throw std::runtime_error("AllocaInst 当前只支持 i32*");
  }
}

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_) {
    throw std::runtime_error("LoadInst 缺少 ptr");
  }
  if (!type_ || !type_->IsInt32()) {
    throw std::runtime_error("LoadInst 当前只支持加载 i32");
  }
  if (!ptr_->type() || !ptr_->type()->IsPtrInt32()) {
    throw std::runtime_error("LoadInst 当前只支持从 i32* 加载");
  }
  ptr_->AddUser(this);
}

Value* LoadInst::ptr() const { return ptr_; }

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_) {
    throw std::runtime_error("StoreInst 缺少 value");
  }
  if (!ptr_) {
    throw std::runtime_error("StoreInst 缺少 ptr");
  }
  if (!type_ || !type_->IsVoid()) {
    throw std::runtime_error("StoreInst 返回类型必须为 void");
  }
  if (!value_->type() || !value_->type()->IsInt32()) {
    throw std::runtime_error("StoreInst 当前只支持存储 i32");
  }
  if (!ptr_->type() || !ptr_->type()->IsPtrInt32()) {
    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
