refactor(dev): unify user-facing diagnostics
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
#include "utils/Log.h"
|
||||
|
||||
namespace mir {
|
||||
namespace {
|
||||
|
||||
@@ -17,8 +19,7 @@ void RunFrameLowering(MachineFunction& function) {
|
||||
for (const auto& slot : function.frame_slots()) {
|
||||
cursor += slot.size;
|
||||
if (-cursor < -256) {
|
||||
throw std::runtime_error(
|
||||
"Lab3 MVP 后端暂不支持超过 64 个 i32 栈槽的函数");
|
||||
throw std::runtime_error(FormatError("mir", "暂不支持过大的栈帧"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <unordered_map>
|
||||
|
||||
#include "ir/IR.h"
|
||||
#include "utils/Log.h"
|
||||
|
||||
namespace mir {
|
||||
namespace {
|
||||
@@ -20,8 +21,8 @@ void EmitValueToReg(const ir::Value* value, PhysReg target,
|
||||
|
||||
auto it = slots.find(value);
|
||||
if (it == slots.end()) {
|
||||
throw std::runtime_error("Lab3 MVP 后端找不到值对应的栈槽: " +
|
||||
value->name());
|
||||
throw std::runtime_error(
|
||||
FormatError("mir", "找不到值对应的栈槽: " + value->name()));
|
||||
}
|
||||
|
||||
block.Append(Opcode::LoadStack,
|
||||
@@ -41,7 +42,8 @@ void LowerInstruction(const ir::Instruction& inst, MachineFunction& function,
|
||||
auto& store = static_cast<const ir::StoreInst&>(inst);
|
||||
auto dst = slots.find(store.ptr());
|
||||
if (dst == slots.end()) {
|
||||
throw std::runtime_error("Lab3 MVP 后端要求 store 目标必须来自 alloca");
|
||||
throw std::runtime_error(
|
||||
FormatError("mir", "暂不支持对非栈变量地址进行写入"));
|
||||
}
|
||||
EmitValueToReg(store.value(), PhysReg::W8, slots, block);
|
||||
block.Append(Opcode::StoreStack,
|
||||
@@ -52,7 +54,8 @@ void LowerInstruction(const ir::Instruction& inst, MachineFunction& function,
|
||||
auto& load = static_cast<const ir::LoadInst&>(inst);
|
||||
auto src = slots.find(load.ptr());
|
||||
if (src == slots.end()) {
|
||||
throw std::runtime_error("Lab3 MVP 后端要求 load 源必须来自 alloca");
|
||||
throw std::runtime_error(
|
||||
FormatError("mir", "暂不支持对非栈变量地址进行读取"));
|
||||
}
|
||||
int dst_slot = function.CreateFrameIndex();
|
||||
block.Append(Opcode::LoadStack,
|
||||
@@ -83,10 +86,10 @@ void LowerInstruction(const ir::Instruction& inst, MachineFunction& function,
|
||||
}
|
||||
case ir::Opcode::Sub:
|
||||
case ir::Opcode::Mul:
|
||||
throw std::runtime_error("Lab3 MVP 后端暂不支持 add 以外的二元运算");
|
||||
throw std::runtime_error(FormatError("mir", "暂不支持该二元运算"));
|
||||
}
|
||||
|
||||
throw std::runtime_error("Lab3 MVP 后端遇到未知 IR 指令");
|
||||
throw std::runtime_error(FormatError("mir", "暂不支持该 IR 指令"));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -95,19 +98,19 @@ std::unique_ptr<MachineFunction> LowerToMIR(const ir::Module& module) {
|
||||
DefaultContext();
|
||||
|
||||
if (module.functions().size() != 1) {
|
||||
throw std::runtime_error("Lab3 MVP 后端只支持单函数 main");
|
||||
throw std::runtime_error(FormatError("mir", "暂不支持多个函数"));
|
||||
}
|
||||
|
||||
const auto& func = *module.functions().front();
|
||||
if (func.name() != "main") {
|
||||
throw std::runtime_error("Lab3 MVP 后端只支持 main 函数");
|
||||
throw std::runtime_error(FormatError("mir", "暂不支持非 main 函数"));
|
||||
}
|
||||
|
||||
auto machine_func = std::make_unique<MachineFunction>(func.name());
|
||||
ValueSlotMap slots;
|
||||
const auto* entry = func.entry();
|
||||
if (!entry) {
|
||||
throw std::runtime_error("IR 函数缺少入口基本块");
|
||||
throw std::runtime_error(FormatError("mir", "IR 函数缺少入口基本块"));
|
||||
}
|
||||
|
||||
for (const auto& inst : entry->instructions()) {
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "utils/Log.h"
|
||||
|
||||
namespace mir {
|
||||
namespace {
|
||||
|
||||
@@ -24,7 +26,7 @@ void RunRegAlloc(MachineFunction& function) {
|
||||
for (const auto& inst : function.entry().instructions()) {
|
||||
for (const auto& operand : inst.operands()) {
|
||||
if (operand.kind() == Operand::Kind::Reg && !IsAllowedReg(operand.reg())) {
|
||||
throw std::runtime_error("Lab3 MVP 后端发现未预着色的寄存器");
|
||||
throw std::runtime_error(FormatError("mir", "寄存器分配失败"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user