From ece0bd090dbe6764665ec5be5bd8f1a64df3c76a Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Fri, 22 Jul 2022 19:23:46 -0600
Subject: [PATCH] Fix x86 setjmp/longjmp

---
 src/header/setjmp/impl/i386/longjmp.s | 24 ++++++++++-------------
 src/header/setjmp/impl/i386/setjmp.s  | 20 +++++++++----------
 src/header/setjmp/mod.rs              | 28 ++++++++-------------------
 3 files changed, 28 insertions(+), 44 deletions(-)

diff --git a/src/header/setjmp/impl/i386/longjmp.s b/src/header/setjmp/impl/i386/longjmp.s
index 772d28dd..a2378b9f 100644
--- a/src/header/setjmp/impl/i386/longjmp.s
+++ b/src/header/setjmp/impl/i386/longjmp.s
@@ -4,17 +4,13 @@
 .type longjmp,@function
 _longjmp:
 longjmp:
-	mov  4(%esp),%edx
-	mov  8(%esp),%eax
-	test    %eax,%eax
-	jnz 1f
-	inc     %eax
-1:
-	mov   (%edx),%ebx
-	mov  4(%edx),%esi
-	mov  8(%edx),%edi
-	mov 12(%edx),%ebp
-	mov 16(%edx),%ecx
-	mov     %ecx,%esp
-	mov 20(%edx),%ecx
-	jmp *%ecx
+	mov edx, [esp + 4]
+	mov eax, [esp + 8]
+	cmp eax, 1
+	adc al, 0
+	mov ebx, [edx]
+	mov esi, [edx + 4]
+	mov edi, [edx + 8]
+	mov ebp, [edx + 12]
+	mov esp, [edx + 16]
+	jmp [edx + 20]
diff --git a/src/header/setjmp/impl/i386/setjmp.s b/src/header/setjmp/impl/i386/setjmp.s
index 4d19cf87..03678fdf 100644
--- a/src/header/setjmp/impl/i386/setjmp.s
+++ b/src/header/setjmp/impl/i386/setjmp.s
@@ -10,14 +10,14 @@ ___setjmp:
 __setjmp:
 _setjmp:
 setjmp:
-	mov 4(%esp), %eax
-	mov    %ebx, (%eax)
-	mov    %esi, 4(%eax)
-	mov    %edi, 8(%eax)
-	mov    %ebp, 12(%eax)
-	lea 4(%esp), %ecx
-	mov    %ecx, 16(%eax)
-	mov  (%esp), %ecx
-	mov    %ecx, 20(%eax)
-	xor    %eax, %eax
+	mov eax, [esp + 4]
+	mov [eax], ebx
+	mov [eax + 4], esi
+	mov [eax + 8], edi
+	mov [eax + 12], ebp
+	lea ecx, [esp + 4]
+	mov [eax + 16], ecx
+	mov ecx, [esp]
+	mov [eax + 20], ecx
+	xor eax, eax
 	ret
diff --git a/src/header/setjmp/mod.rs b/src/header/setjmp/mod.rs
index 372e5676..5ed23a51 100644
--- a/src/header/setjmp/mod.rs
+++ b/src/header/setjmp/mod.rs
@@ -3,30 +3,18 @@
 use core::arch::global_asm;
 
 macro_rules! platform_specific {
-    ($($arch:expr,$ext:expr;)+) => {
+    ($($rust_arch:expr,$c_arch:expr,$ext:expr;)+) => {
         $(
-            #[cfg(target_arch = $arch)]
-            global_asm!(include_str!(concat!("impl/", $arch, "/setjmp.", $ext)));
-            #[cfg(target_arch = $arch)]
-            global_asm!(include_str!(concat!("impl/", $arch, "/longjmp.", $ext)));
+            #[cfg(target_arch = $rust_arch)]
+            global_asm!(include_str!(concat!("impl/", $c_arch, "/setjmp.", $ext)));
+            #[cfg(target_arch = $rust_arch)]
+            global_asm!(include_str!(concat!("impl/", $c_arch, "/longjmp.", $ext)));
         )+
     }
 }
 
 platform_specific! {
-    "aarch64","s";
-    "arm","s";
-    "i386","s";
-    "m68k","s";
-    "microblaze","s";
-    "mips","S";
-    "mips64","S";
-    "mipsn32","S";
-    "or1k","s";
-    "powerpc","S";
-    "powerpc64","s";
-    "s390x","s";
-    "sh","S";
-    "x32","s";
-    "x86_64","s";
+    "aarch64","aarch64", "s";
+    "x86","i386","s";
+    "x86_64","x86_64","s";
 }
-- 
GitLab