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

#include <stdexcept>

#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)
    : User(std::move(ty), std::move(name)), opcode_(op) {}

Opcode Instruction::GetOpcode() const { return opcode_; }

bool Instruction::IsTerminator() const { return opcode_ == Opcode::Ret || opcode_ == Opcode::Br || opcode_ == Opcode::CondBr; }

BasicBlock* Instruction::GetParent() const { return parent_; }

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)) {
  if (op != Opcode::Add && op != Opcode::Sub && op != Opcode::Mul &&
      op != Opcode::Div && op != Opcode::Mod) {
    throw std::runtime_error(FormatError("ir", "BinaryInst 不支持的操作码"));
  }
  if (!lhs || !rhs) {
    throw std::runtime_error(FormatError("ir", "BinaryInst 缺少操作数"));
  }
  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()) {
    throw std::runtime_error(FormatError("ir", "BinaryInst 类型不匹配"));
  }
  if (!type_->IsInt32() && !type_->IsFloat()) {
    throw std::runtime_error(FormatError("ir", "BinaryInst 当前只支持 i32 或 float"));
  }
  AddOperand(lhs);
  AddOperand(rhs);
}

Value* BinaryInst::GetLhs() const { return GetOperand(0); }

Value* BinaryInst::GetRhs() const { return GetOperand(1); }

UnaryInst::UnaryInst(Opcode op, std::shared_ptr<Type> ty, Value* operand,
                     std::string name)
    : Instruction(op, std::move(ty), std::move(name)) {
  if (op != Opcode::Neg) {
    throw std::runtime_error(FormatError("ir", "UnaryInst 不支持的操作码"));
  }
  if (!operand) {
    throw std::runtime_error(FormatError("ir", "UnaryInst 缺少操作数"));
  }
  if (!type_ || !operand->GetType()) {
    throw std::runtime_error(FormatError("ir", "UnaryInst 缺少类型信息"));
  }
  if (type_->GetKind() != operand->GetType()->GetKind()) {
    throw std::runtime_error(FormatError("ir", "UnaryInst 类型不匹配"));
  }
  if (!type_->IsInt32() && !type_->IsFloat()) {
    throw std::runtime_error(FormatError("ir", "UnaryInst 当前只支持 i32 或 float"));
  }
  AddOperand(operand);
}

Value* UnaryInst::GetUnaryOperand() const { return GetOperand(0); }

ReturnInst::ReturnInst(std::shared_ptr<Type> void_ty, Value* val)
    : Instruction(Opcode::Ret, std::move(void_ty), "") {
  if (!type_ || !type_->IsVoid()) {
    throw std::runtime_error(FormatError("ir", "ReturnInst 返回类型必须为 void"));
  }
  if (val) {
    AddOperand(val);
  }
}

Value* ReturnInst::GetValue() const {
  return GetNumOperands() > 0 ? GetOperand(0) : nullptr;
}

AllocaInst::AllocaInst(std::shared_ptr<Type> ptr_ty, std::string name)
    : Instruction(Opcode::Alloca, std::move(ptr_ty), std::move(name)) {}

LoadInst::LoadInst(std::shared_ptr<Type> val_ty, Value* ptr, std::string name)
    : Instruction(Opcode::Load, std::move(val_ty), std::move(name)) {
  if (!ptr) {
    throw std::runtime_error(FormatError("ir", "LoadInst 缺少 ptr"));
  }
  if (!type_ || (!type_->IsInt32() && !type_->IsFloat() && !type_->IsInt1())) {
    // Note: IsInt1 is for Zext or comparisons
  }
  AddOperand(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), "") {
  if (!val) {
    throw std::runtime_error(FormatError("ir", "StoreInst 缺少 value"));
  }
  if (!ptr) {
    throw std::runtime_error(FormatError("ir", "StoreInst 缺少 ptr"));
  }
  if (!type_ || !type_->IsVoid()) {
    throw std::runtime_error(FormatError("ir", "StoreInst 返回类型必须为 void"));
  }
  if (!val->GetType() || (!val->GetType()->IsInt32() && !val->GetType()->IsFloat() && !val->GetType()->IsPointer())) {
    throw std::runtime_error(FormatError("ir", "StoreInst 当前只支持存储 i32、float 或指针类型"));
  }
  if (val->GetType()->IsInt32() || val->GetType()->IsPointer()) {
    if (!ptr->GetType() || !ptr->GetType()->IsPointer()) {
      throw std::runtime_error(
          FormatError("ir", "StoreInst 当前只支持写入指针类型槽位"));
    }
  } else if (val->GetType()->IsFloat()) {
    if (!ptr->GetType() || !ptr->GetType()->IsPtrFloat()) {
      throw std::runtime_error(
          FormatError("ir", "StoreInst 当前只支持写入 float*"));
    }
  }
  AddOperand(val);
  AddOperand(ptr);
}

Value* StoreInst::GetValue() const { return GetOperand(0); }

Value* StoreInst::GetPtr() const { return GetOperand(1); }

CmpInst::CmpInst(CmpOp cmp_op, Value* lhs, Value* rhs, std::string name)
    : Instruction(Opcode::Cmp, Type::GetInt1Type(), std::move(name)), cmp_op_(cmp_op) {
  if (!lhs || !rhs) {
    throw std::runtime_error(FormatError("ir", "CmpInst 缺少操作数"));
  }
  if (!lhs->GetType() || !rhs->GetType()) {
    throw std::runtime_error(FormatError("ir", "CmpInst 缺少操作数类型信息"));
  }
  if (lhs->GetType()->GetKind() != rhs->GetType()->GetKind()) {
    throw std::runtime_error(FormatError("ir", "CmpInst 操作数类型不匹配"));
  }
  AddOperand(lhs);
  AddOperand(rhs);
}

CmpOp CmpInst::GetCmpOp() const { return cmp_op_; }
Value* CmpInst::GetLhs() const { return GetOperand(0); }
Value* CmpInst::GetRhs() const { return GetOperand(1); }

FCmpInst::FCmpInst(CmpOp cmp_op, Value* lhs, Value* rhs, std::string name)
    : Instruction(Opcode::FCmp, Type::GetInt1Type(), std::move(name)), cmp_op_(cmp_op) {
  if (!lhs || !rhs) {
    throw std::runtime_error(FormatError("ir", "FCmpInst 缺少操作数"));
  }
  if (!lhs->GetType() || !rhs->GetType()) {
    throw std::runtime_error(FormatError("ir", "FCmpInst 缺少操作数类型信息"));
  }
  if (lhs->GetType()->GetKind() != rhs->GetType()->GetKind()) {
    throw std::runtime_error(FormatError("ir", "FCmpInst 操作数类型不匹配"));
  }
  AddOperand(lhs);
  AddOperand(rhs);
}

CmpOp FCmpInst::GetCmpOp() const { return cmp_op_; }
Value* FCmpInst::GetLhs() const { return GetOperand(0); }
Value* FCmpInst::GetRhs() const { return GetOperand(1); }

ZextInst::ZextInst(std::shared_ptr<Type> dest_ty, Value* val, std::string name)
    : Instruction(Opcode::Zext, std::move(dest_ty), std::move(name)) {
  if (!val) {
    throw std::runtime_error(FormatError("ir", "ZextInst 缺少操作数"));
  }
  if (!type_->IsInt32() || !val->GetType()->IsInt1()) {
    throw std::runtime_error(FormatError("ir", "ZextInst 当前只支持 i1 到 i32"));
  }
  AddOperand(val);
}

Value* ZextInst::GetValue() const { return GetOperand(0); }

BranchInst::BranchInst(BasicBlock* dest)
    : Instruction(Opcode::Br, Type::GetVoidType(), "") {
  if (!dest) {
    throw std::runtime_error(FormatError("ir", "BranchInst 缺少目的块"));
  }
  AddOperand(dest);
}

BasicBlock* BranchInst::GetDest() const { return static_cast<BasicBlock*>(GetOperand(0)); }

CondBranchInst::CondBranchInst(Value* cond, BasicBlock* true_bb, BasicBlock* false_bb)
    : Instruction(Opcode::CondBr, Type::GetVoidType(), "") {
  if (!cond || !true_bb || !false_bb) {
    throw std::runtime_error(FormatError("ir", "CondBranchInst 缺少连边操作数"));
  }
  if (!cond->GetType()->IsInt1()) {
    throw std::runtime_error(FormatError("ir", "CondBranchInst 必须使用 i1 作为条件"));
  }
  AddOperand(cond);
  AddOperand(true_bb);
  AddOperand(false_bb);
}

Value* CondBranchInst::GetCond() const { return GetOperand(0); }
BasicBlock* CondBranchInst::GetTrueBlock() const { return static_cast<BasicBlock*>(GetOperand(1)); }
BasicBlock* CondBranchInst::GetFalseBlock() const { return static_cast<BasicBlock*>(GetOperand(2)); }

CallInst::CallInst(Function* func, std::vector<Value*> args, std::string name)
    : Instruction(Opcode::Call, func->GetType(), std::move(name)), func_(func), args_(std::move(args)) {
  if (!func) {
    throw std::runtime_error(FormatError("ir", "CallInst 缺少目标函数"));
  }
  AddOperand(func);
  for (auto* arg : args_) {
    AddOperand(arg);
  }
}

Function* CallInst::GetFunc() const { return func_; }
const std::vector<Value*>& CallInst::GetArgs() const { return args_; }

GEPInst::GEPInst(std::shared_ptr<Type> ty, Value* ptr, std::vector<Value*> indices, std::string name)
    : Instruction(Opcode::GEP, std::move(ty), std::move(name)), indices_(std::move(indices)) {
  AddOperand(ptr);
  for (auto* idx : indices_) {
    AddOperand(idx);
  }
}

Value* GEPInst::GetPtr() const { return GetOperand(0); }
const std::vector<Value*>& GEPInst::GetIndices() const { return indices_; }

SIToFPInst::SIToFPInst(std::shared_ptr<Type> ty, Value* val, std::string name)
    : Instruction(Opcode::SIToFP, std::move(ty), std::move(name)) {
  AddOperand(val);
}

FPToSIInst::FPToSIInst(std::shared_ptr<Type> ty, Value* val, std::string name)
    : Instruction(Opcode::FPToSI, std::move(ty), std::move(name)) {
  AddOperand(val);
}

}  // namespace ir
