#include "mir/MIR.h"

#include <stdexcept>
#include <utility>

namespace mir {

MachineFunction::MachineFunction(std::string name)
    : name_(std::move(name)), entry_("entry") {}

int MachineFunction::CreateFrameIndex(int size) {
  int index = static_cast<int>(frame_slots_.size());
  frame_slots_.push_back(FrameSlot{index, size, 0});
  return index;
}

FrameSlot& MachineFunction::frame_slot(int index) {
  if (index < 0 || index >= static_cast<int>(frame_slots_.size())) {
    throw std::runtime_error("非法 FrameIndex");
  }
  return frame_slots_[index];
}

const FrameSlot& MachineFunction::frame_slot(int index) const {
  if (index < 0 || index >= static_cast<int>(frame_slots_.size())) {
    throw std::runtime_error("非法 FrameIndex");
  }
  return frame_slots_[index];
}

}  // namespace mir
