From bf21e9c437df81f52d3c034f9f2189a9b6b4a36d Mon Sep 17 00:00:00 2001 From: ftt <> Date: Thu, 9 Apr 2026 16:29:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E6=B5=AE=E7=82=B9=E5=B8=B8?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mir/Lowering.cpp | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/mir/Lowering.cpp b/src/mir/Lowering.cpp index 4d6f513..73b37e9 100644 --- a/src/mir/Lowering.cpp +++ b/src/mir/Lowering.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "ir/IR.h" #include "utils/Log.h" @@ -20,6 +21,12 @@ namespace { using ValueSlotMap = std::unordered_map; +static uint32_t FloatToBits(float f) { + uint32_t bits; + memcpy(&bits, &f, sizeof(bits)); + return bits; +} + // 获取类型大小(字节) int GetTypeSize(const ir::Type* type) { if (!type) return 4; @@ -109,15 +116,23 @@ void EmitValueToReg(const ir::Value* value, PhysReg target, } // 处理浮点常量 if (auto* fconstant = dynamic_cast(value)) { - // 浮点常量需要先存储到栈槽,再加载到寄存器 - // 因为 ARMv8 没有直接加载浮点立即数的指令 - int slot = -1; - // 注意:这里需要找到或创建该浮点常量的栈槽 - // 简单起见,可以每次都分配新栈槽 - // 更好的做法是:在 Module 级别缓存浮点常量 - throw std::runtime_error( - FormatError("mir", "浮点常量暂未实现")); - return; + // 浮点常量需要存储到栈槽,然后加载到寄存器 + // 检查是否已经为这个常量分配了栈槽 + auto it = slots.find(value); + int slot; + if (it == slots.end()) { + // 分配新的栈槽 + slot = function.CreateFrameIndex(4); + // 将浮点常量存储到栈槽 + float fval = fconstant->GetValue(); + // 将浮点数作为立即数加载(使用整数表示) + uint32_t int_val = *reinterpret_cast(&fval); + block.Append(Opcode::MovImm, {Operand::Reg(PhysReg::W8), Operand::Imm(static_cast(int_val))}); + block.Append(Opcode::StoreStack, {Operand::Reg(PhysReg::W8), Operand::FrameIndex(slot)}); + const_cast(slots).emplace(value, slot); + } else { + slot = it->second; + } } // 处理零常量 if (dynamic_cast(value) ||