[libunwind] [libunwind][Mips] Fix the o32 register save/restore code for MIPS I (PR #246542)

Brad Smith via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 26 19:49:03 PST 2026


https://github.com/brad0 updated https://github.com/llvm/llvm-project/pull/246542

>From 9aefa8eb7754cff672b16bfb24d7b0ca0db0d7c2 Mon Sep 17 00:00:00 2001
From: Brad Smith <brad at comstyle.com>
Date: Thu, 26 Nov 2026 20:45:17 -0500
Subject: [PATCH] [libunwind][Mips] Fix the o32 register save/restore code for
 MIPS I

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.

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>
---
 libunwind/src/UnwindRegistersRestore.S | 47 +++++++++++++++++++++++---
 libunwind/src/UnwindRegistersSave.S    | 35 +++++++++++++++++++
 2 files changed, 78 insertions(+), 4 deletions(-)

diff --git a/libunwind/src/UnwindRegistersRestore.S b/libunwind/src/UnwindRegistersRestore.S
index b1b075ba556f9..2a4df338fdfc7 100644
--- a/libunwind/src/UnwindRegistersRestore.S
+++ b/libunwind/src/UnwindRegistersRestore.S
@@ -1052,6 +1052,41 @@ 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 == 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.
+  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 != 64
   ldc1  $f0, (4 * 36 + 8 * 0)($4)
   ldc1  $f2, (4 * 36 + 8 * 2)($4)
@@ -1106,10 +1141,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 +1181,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 1dbc1f697bc14..5cfed47751350 100644
--- a/libunwind/src/UnwindRegistersSave.S
+++ b/libunwind/src/UnwindRegistersSave.S
@@ -313,6 +313,41 @@ 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 == 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.
+  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 != 64
   sdc1  $f0, (4 * 36 + 8 * 0)($4)
   sdc1  $f2, (4 * 36 + 8 * 2)($4)



More information about the cfe-commits mailing list