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

void User::ClearOperands() {
  for (size_t i = 0; i < operands_.size(); ++i) {
    auto* old = operands_[i];
    if (old) {
      old->RemoveUse(this, i);
    }
  }
  operands_.clear();
}

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

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 (!lhs || !rhs) {
    throw std::runtime_error(FormatError("ir", "BinaryInst 缺少操作数"));
  }
  AddOperand(lhs);
  AddOperand(rhs);
}

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

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

BranchInst::BranchInst(BasicBlock* dest)
    : Instruction(Opcode::Br, Type::GetVoidType(), "") {
  AddOperand(dest);
}

BranchInst::BranchInst(Value* cond, BasicBlock* if_true, BasicBlock* if_false)
    : Instruction(Opcode::Br, Type::GetVoidType(), "") {
  AddOperand(cond);
  AddOperand(if_true);
  AddOperand(if_false);
}

bool BranchInst::IsConditional() const { return GetNumOperands() == 3; }

Value* BranchInst::GetCondition() const {
  return IsConditional() ? GetOperand(0) : nullptr;
}

BasicBlock* BranchInst::GetIfTrue() const {
  return IsConditional() ? static_cast<BasicBlock*>(GetOperand(1)) : nullptr;
}

BasicBlock* BranchInst::GetIfFalse() const {
  return IsConditional() ? static_cast<BasicBlock*>(GetOperand(2)) : nullptr;
}

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

CallInst::CallInst(Function* func, const std::vector<Value*>& args,
                   std::string name)
    : Instruction(Opcode::Call, func->GetType(), std::move(name)) {
  AddOperand(func);
  for (auto* arg : args) {
    AddOperand(arg);
  }
}

Function* CallInst::GetFunction() const {
  return static_cast<Function*>(GetOperand(0));
}

GetElementPtrInst::GetElementPtrInst(std::shared_ptr<Type> ptr_ty, Value* ptr,
                                     const std::vector<Value*>& indices,
                                     std::string name)
    : Instruction(Opcode::GEP, std::move(ptr_ty), std::move(name)) {
  AddOperand(ptr);
  for (auto* idx : indices) {
    AddOperand(idx);
  }
}

Value* GetElementPtrInst::GetPtr() const { return GetOperand(0); }

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

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

ReturnInst::ReturnInst(std::shared_ptr<Type> void_ty, Value* val)
    : Instruction(Opcode::Ret, std::move(void_ty), "") {
  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)) {
  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), "") {
  AddOperand(val);
  AddOperand(ptr);
}

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

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

PhiInst::PhiInst(std::shared_ptr<Type> ty, std::string name)
    : Instruction(Opcode::Phi, std::move(ty), std::move(name)) {}

void PhiInst::AddIncoming(Value* val, BasicBlock* bb) {
  AddOperand(val);
  AddOperand(bb);
}

size_t PhiInst::GetNumIncoming() const {
  return GetNumOperands() / 2;
}

Value* PhiInst::GetIncomingValue(size_t i) const {
  return GetOperand(2 * i);
}

BasicBlock* PhiInst::GetIncomingBlock(size_t i) const {
  return static_cast<BasicBlock*>(GetOperand(2 * i + 1));
}

void PhiInst::SetIncomingValue(size_t i, Value* val) {
  SetOperand(2 * i, val);
}

void PhiInst::SetIncomingBlock(size_t i, BasicBlock* bb) {
  SetOperand(2 * i + 1, bb);
}

void PhiInst::RemoveIncomingBlock(BasicBlock* bb) {
  std::vector<Value*> new_ops;
  for (size_t i = 0; i < GetNumIncoming(); ++i) {
    if (GetIncomingBlock(i) != bb) {
      new_ops.push_back(GetIncomingValue(i));
      new_ops.push_back(GetIncomingBlock(i));
    }
  }
  ClearOperands();
  for (auto* op : new_ops) {
    AddOperand(op);
  }
}

}  // namespace ir
