forked from NUDT-compiler/nudt-compiler-cpp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
3.4 KiB
148 lines
3.4 KiB
#include "mir/MIR.h"
|
|
|
|
#include <stdexcept>
|
|
#include <utility>
|
|
|
|
#include "utils/Log.h"
|
|
|
|
namespace mir
|
|
{
|
|
|
|
MachineFunction::MachineFunction(std::string name)
|
|
: name_(std::move(name))
|
|
{
|
|
entry_ = &CreateBlock("entry");
|
|
}
|
|
|
|
MachineBasicBlock &MachineFunction::CreateBlock(std::string name)
|
|
{
|
|
blocks_.push_back(
|
|
std::make_unique<MachineBasicBlock>(std::move(name), CreateLabel()));
|
|
return *blocks_.back();
|
|
}
|
|
|
|
MachineBasicBlock *MachineFunction::FindBlock(const std::string &name)
|
|
{
|
|
for (auto &block : blocks_)
|
|
{
|
|
if (block && block->GetName() == name)
|
|
{
|
|
return block.get();
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
const MachineBasicBlock *MachineFunction::FindBlock(
|
|
const std::string &name) const
|
|
{
|
|
for (const auto &block : blocks_)
|
|
{
|
|
if (block && block->GetName() == name)
|
|
{
|
|
return block.get();
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
int MachineFunction::CreateLabel() { return next_label_id_++; }
|
|
|
|
int MachineFunction::CreateFrameIndex(int size)
|
|
{
|
|
int index = static_cast<int>(frame_slots_.size());
|
|
frame_slots_.push_back(FrameSlot{index, size, 0, false, false});
|
|
return index;
|
|
}
|
|
|
|
int MachineFunction::CreateStackArgFrameIndex(int size)
|
|
{
|
|
int index = static_cast<int>(frame_slots_.size());
|
|
frame_slots_.push_back(FrameSlot{index, size, 0, true, false});
|
|
return index;
|
|
}
|
|
|
|
int MachineFunction::CreateCalleeStackArgFrameIndex(int size)
|
|
{
|
|
int index = static_cast<int>(frame_slots_.size());
|
|
frame_slots_.push_back(FrameSlot{index, size, 0, true, true});
|
|
return index;
|
|
}
|
|
|
|
FrameSlot &MachineFunction::GetFrameSlot(int index)
|
|
{
|
|
if (index < 0 || index >= static_cast<int>(frame_slots_.size()))
|
|
{
|
|
throw std::runtime_error(FormatError("mir", "非法 FrameIndex"));
|
|
}
|
|
return frame_slots_[index];
|
|
}
|
|
|
|
const FrameSlot &MachineFunction::GetFrameSlot(int index) const
|
|
{
|
|
if (index < 0 || index >= static_cast<int>(frame_slots_.size()))
|
|
{
|
|
throw std::runtime_error(FormatError("mir", "非法 FrameIndex"));
|
|
}
|
|
return frame_slots_[index];
|
|
}
|
|
|
|
int MachineFunction::CreateVReg(VRegClass vreg_class)
|
|
{
|
|
int id = static_cast<int>(vreg_classes_.size());
|
|
vreg_classes_.push_back(vreg_class);
|
|
return id;
|
|
}
|
|
|
|
VRegClass MachineFunction::GetVRegClass(int vreg_id) const
|
|
{
|
|
if (vreg_id < 0 || vreg_id >= static_cast<int>(vreg_classes_.size()))
|
|
{
|
|
throw std::runtime_error(FormatError("mir", "非法 VReg id"));
|
|
}
|
|
return vreg_classes_[vreg_id];
|
|
}
|
|
|
|
void MachineFunction::AddCalleeSavedReg(PhysReg reg)
|
|
{
|
|
for (auto r : callee_saved_regs_)
|
|
{
|
|
if (r == reg)
|
|
return;
|
|
}
|
|
callee_saved_regs_.push_back(reg);
|
|
}
|
|
|
|
MachineFunction &MachineModule::CreateFunction(std::string name)
|
|
{
|
|
functions_.push_back(std::make_unique<MachineFunction>(std::move(name)));
|
|
return *functions_.back();
|
|
}
|
|
|
|
MachineFunction *MachineModule::GetFunction(const std::string &name)
|
|
{
|
|
for (auto &function : functions_)
|
|
{
|
|
if (function && function->GetName() == name)
|
|
{
|
|
return function.get();
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
const MachineFunction *MachineModule::GetFunction(
|
|
const std::string &name) const
|
|
{
|
|
for (const auto &function : functions_)
|
|
{
|
|
if (function && function->GetName() == name)
|
|
{
|
|
return function.get();
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
} // namespace mir
|