[libunwind] eb737cc - [libunwind][Mips] Fix the o32 register save/restore code for MIPS I (#246542)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 1 02:27:55 PST 2026
Author: Brad Smith
Date: 2026-12-01T05:27:49-05:00
New Revision: eb737ccf64fa8f6db9cdbd8c0ba3570f87d178b7
URL: https://github.com/llvm/llvm-project/commit/eb737ccf64fa8f6db9cdbd8c0ba3570f87d178b7
DIFF: https://github.com/llvm/llvm-project/commit/eb737ccf64fa8f6db9cdbd8c0ba3570f87d178b7.diff
LOG: [libunwind][Mips] Fix the o32 register save/restore code for MIPS I (#246542)
MIPS I has no ldc1/sdc1, so UnwindRegistersSave.S and
UnwindRegistersRestore.S do not assemble for hard-float o32 on MIPS I.
For FP32, save and restore each register with swc1/lwc1 instead. Each
even/odd register pair is in one 8-byte slot, with the even register in
the first half, which is where getRegister() expects it. Use .set
oddspreg so this also assembles with -mno-odd-spreg.
jumpto() also loads $8 and moves it to HI right away with mthi, then
does the same for LO. The code is in .set noreorder, so on MIPS I mthi
and mtlo read $8 in the load delay slot and get the old value. Load HI
into $8 and LO into $9 first, then do the mthi and mtlo. Both registers
are restored from the context afterwards.
jumpto() then loads $4 in the delay slot of the jr. On MIPS I the first
instruction at the new pc cannot use $4 yet, and a landing pad gets the
exception object in $4. Load $4 before the jr and put a nop in the
delay slot.
Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
Added:
Modified:
libunwind/src/UnwindRegistersRestore.S
libunwind/src/UnwindRegistersSave.S
Removed:
################################################################################
diff --git a/libunwind/src/UnwindRegistersRestore.S b/libunwind/src/UnwindRegistersRestore.S
index b1b075ba556f91..36f2d4b0cffbfc 100644
--- a/libunwind/src/UnwindRegistersRestore.S
+++ b/libunwind/src/UnwindRegistersRestore.S
@@ -1052,7 +1052,44 @@ DEFINE_LIBUNWIND_FUNCTION(_ZN9libunwind18Registers_mips_o326jumptoEv)
lwc1 $f29, (4 * 36 + 8 * 29)($4)
lwc1 $f30, (4 * 36 + 8 * 30)($4)
lwc1 $f31, (4 * 36 + 8 * 31)($4)
-#elif __mips_fpr != 64
+#elif __mips_fpr == 32
+ // MIPS I has no ldc1. Each even/odd register pair is in one 8-byte slot,
+ // with the even register in the first half, as getRegister() expects.
+ // The odd registers are used, so allow that even with -mno-odd-spreg.
+ .set oddspreg
+ lwc1 $f0, (4 * 36 + 8 * 0)($4)
+ lwc1 $f1, (4 * 36 + 8 * 0 + 4)($4)
+ lwc1 $f2, (4 * 36 + 8 * 2)($4)
+ lwc1 $f3, (4 * 36 + 8 * 2 + 4)($4)
+ lwc1 $f4, (4 * 36 + 8 * 4)($4)
+ lwc1 $f5, (4 * 36 + 8 * 4 + 4)($4)
+ lwc1 $f6, (4 * 36 + 8 * 6)($4)
+ lwc1 $f7, (4 * 36 + 8 * 6 + 4)($4)
+ lwc1 $f8, (4 * 36 + 8 * 8)($4)
+ lwc1 $f9, (4 * 36 + 8 * 8 + 4)($4)
+ lwc1 $f10, (4 * 36 + 8 * 10)($4)
+ lwc1 $f11, (4 * 36 + 8 * 10 + 4)($4)
+ lwc1 $f12, (4 * 36 + 8 * 12)($4)
+ lwc1 $f13, (4 * 36 + 8 * 12 + 4)($4)
+ lwc1 $f14, (4 * 36 + 8 * 14)($4)
+ lwc1 $f15, (4 * 36 + 8 * 14 + 4)($4)
+ lwc1 $f16, (4 * 36 + 8 * 16)($4)
+ lwc1 $f17, (4 * 36 + 8 * 16 + 4)($4)
+ lwc1 $f18, (4 * 36 + 8 * 18)($4)
+ lwc1 $f19, (4 * 36 + 8 * 18 + 4)($4)
+ lwc1 $f20, (4 * 36 + 8 * 20)($4)
+ lwc1 $f21, (4 * 36 + 8 * 20 + 4)($4)
+ lwc1 $f22, (4 * 36 + 8 * 22)($4)
+ lwc1 $f23, (4 * 36 + 8 * 22 + 4)($4)
+ lwc1 $f24, (4 * 36 + 8 * 24)($4)
+ lwc1 $f25, (4 * 36 + 8 * 24 + 4)($4)
+ lwc1 $f26, (4 * 36 + 8 * 26)($4)
+ lwc1 $f27, (4 * 36 + 8 * 26 + 4)($4)
+ lwc1 $f28, (4 * 36 + 8 * 28)($4)
+ lwc1 $f29, (4 * 36 + 8 * 28 + 4)($4)
+ lwc1 $f30, (4 * 36 + 8 * 30)($4)
+ lwc1 $f31, (4 * 36 + 8 * 30 + 4)($4)
+#elif __mips_fpr == 0
ldc1 $f0, (4 * 36 + 8 * 0)($4)
ldc1 $f2, (4 * 36 + 8 * 2)($4)
ldc1 $f4, (4 * 36 + 8 * 4)($4)
@@ -1106,10 +1143,11 @@ DEFINE_LIBUNWIND_FUNCTION(_ZN9libunwind18Registers_mips_o326jumptoEv)
#endif
#if __mips_isa_rev < 6
// restore hi and lo
+ // MIPS 1 has load delay slot. Load both values before using them.
lw $8, (4 * 33)($4)
+ lw $9, (4 * 34)($4)
mthi $8
- lw $8, (4 * 34)($4)
- mtlo $8
+ mtlo $9
#endif
// r0 is zero
lw $1, (4 * 1)($4)
@@ -1145,9 +1183,12 @@ DEFINE_LIBUNWIND_FUNCTION(_ZN9libunwind18Registers_mips_o326jumptoEv)
lw $31, (4 * 32)($4)
// MIPS 1 has load delay slot. Ensure lw $31 and jr are separated by an instruction.
lw $30, (4 * 30)($4)
- // jump to ra, load a0 in the delay slot
- jr $31
+ // Load a0 before the jump, not in the delay slot, as on MIPS 1 the first
+ // instruction at ra could not use it yet.
lw $4, (4 * 4)($4)
+ // jump to ra
+ jr $31
+ nop
.set pop
#elif defined(__mips64)
diff --git a/libunwind/src/UnwindRegistersSave.S b/libunwind/src/UnwindRegistersSave.S
index 1dbc1f697bc14a..ae5c59b723c486 100644
--- a/libunwind/src/UnwindRegistersSave.S
+++ b/libunwind/src/UnwindRegistersSave.S
@@ -313,7 +313,44 @@ DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
swc1 $f29, (4 * 36 + 8 * 29)($4)
swc1 $f30, (4 * 36 + 8 * 30)($4)
swc1 $f31, (4 * 36 + 8 * 31)($4)
-#elif __mips_fpr != 64
+#elif __mips_fpr == 32
+ // MIPS I has no sdc1. Each even/odd register pair is in one 8-byte slot,
+ // with the even register in the first half, as getRegister() expects.
+ // The odd registers are used, so allow that even with -mno-odd-spreg.
+ .set oddspreg
+ swc1 $f0, (4 * 36 + 8 * 0)($4)
+ swc1 $f1, (4 * 36 + 8 * 0 + 4)($4)
+ swc1 $f2, (4 * 36 + 8 * 2)($4)
+ swc1 $f3, (4 * 36 + 8 * 2 + 4)($4)
+ swc1 $f4, (4 * 36 + 8 * 4)($4)
+ swc1 $f5, (4 * 36 + 8 * 4 + 4)($4)
+ swc1 $f6, (4 * 36 + 8 * 6)($4)
+ swc1 $f7, (4 * 36 + 8 * 6 + 4)($4)
+ swc1 $f8, (4 * 36 + 8 * 8)($4)
+ swc1 $f9, (4 * 36 + 8 * 8 + 4)($4)
+ swc1 $f10, (4 * 36 + 8 * 10)($4)
+ swc1 $f11, (4 * 36 + 8 * 10 + 4)($4)
+ swc1 $f12, (4 * 36 + 8 * 12)($4)
+ swc1 $f13, (4 * 36 + 8 * 12 + 4)($4)
+ swc1 $f14, (4 * 36 + 8 * 14)($4)
+ swc1 $f15, (4 * 36 + 8 * 14 + 4)($4)
+ swc1 $f16, (4 * 36 + 8 * 16)($4)
+ swc1 $f17, (4 * 36 + 8 * 16 + 4)($4)
+ swc1 $f18, (4 * 36 + 8 * 18)($4)
+ swc1 $f19, (4 * 36 + 8 * 18 + 4)($4)
+ swc1 $f20, (4 * 36 + 8 * 20)($4)
+ swc1 $f21, (4 * 36 + 8 * 20 + 4)($4)
+ swc1 $f22, (4 * 36 + 8 * 22)($4)
+ swc1 $f23, (4 * 36 + 8 * 22 + 4)($4)
+ swc1 $f24, (4 * 36 + 8 * 24)($4)
+ swc1 $f25, (4 * 36 + 8 * 24 + 4)($4)
+ swc1 $f26, (4 * 36 + 8 * 26)($4)
+ swc1 $f27, (4 * 36 + 8 * 26 + 4)($4)
+ swc1 $f28, (4 * 36 + 8 * 28)($4)
+ swc1 $f29, (4 * 36 + 8 * 28 + 4)($4)
+ swc1 $f30, (4 * 36 + 8 * 30)($4)
+ swc1 $f31, (4 * 36 + 8 * 30 + 4)($4)
+#elif __mips_fpr == 0
sdc1 $f0, (4 * 36 + 8 * 0)($4)
sdc1 $f2, (4 * 36 + 8 * 2)($4)
sdc1 $f4, (4 * 36 + 8 * 4)($4)
More information about the cfe-commits
mailing list