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.
46 lines
1.0 KiB
46 lines
1.0 KiB
#include "mir/MIR.h"
|
|
|
|
#include <stdexcept>
|
|
#include <vector>
|
|
|
|
#include "utils/Log.h"
|
|
|
|
namespace mir {
|
|
namespace {
|
|
|
|
int AlignTo(int value, int align) {
|
|
return ((value + align - 1) / align) * align;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void RunFrameLowering(MachineFunction& function) {
|
|
int cursor = 0;
|
|
for (const auto& slot : function.frame_slots()) {
|
|
cursor += slot.size;
|
|
if (-cursor < -256) {
|
|
throw std::runtime_error(FormatError("mir", "暂不支持过大的栈帧"));
|
|
}
|
|
}
|
|
|
|
cursor = 0;
|
|
for (const auto& slot : function.frame_slots()) {
|
|
cursor += slot.size;
|
|
function.frame_slot(slot.index).offset = -cursor;
|
|
}
|
|
function.set_frame_size(AlignTo(cursor, 16));
|
|
|
|
auto& insts = function.entry().instructions();
|
|
std::vector<MachineInstr> lowered;
|
|
lowered.emplace_back(Opcode::Prologue);
|
|
for (const auto& inst : insts) {
|
|
if (inst.opcode() == Opcode::Ret) {
|
|
lowered.emplace_back(Opcode::Epilogue);
|
|
}
|
|
lowered.push_back(inst);
|
|
}
|
|
insts = std::move(lowered);
|
|
}
|
|
|
|
} // namespace mir
|