From 20173629aab24b4caad9b2aa533f870a92e96b23 Mon Sep 17 00:00:00 2001
From: Sergei Trofimovich <slyfox@gentoo.org>
Date: Wed, 21 Jul 2021 22:30:59 +0100
Subject: [PATCH] UNREG: implement 64-bit mach ops for 32-bit targets

Noticed build failures like
```
ghc-stage1: panic! (the 'impossible' happened)
  GHC version 9.3.20210721:
        pprCallishMachOp_for_C: MO_x64_Ne not supported!
```
on `--tagget=hppa2.0-unknown-linux-gnu`.

The change does not fix all 32-bit unreg target problems,
but at least allows linking final ghc binaries.

Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>
---
 compiler/GHC/CmmToC.hs | 58 +++++++++++++++++++++---------------------
 includes/stg/Prim.h    | 35 ++++++++++++++++++++++++-
 2 files changed, 63 insertions(+), 30 deletions(-)

diff --git a/compiler/GHC/CmmToC.hs b/compiler/GHC/CmmToC.hs
index f662f7d996f1..b49fccbd95be 100644
--- a/compiler/GHC/CmmToC.hs
+++ b/compiler/GHC/CmmToC.hs
@@ -863,35 +863,35 @@ pprCallishMachOp_for_C mop
         (MO_Prefetch_Data _ ) -> unsupported
         --- we could support prefetch via "__builtin_prefetch"
         --- Not adding it for now
-        MO_I64_ToI   -> unsupported
-        MO_I64_FromI -> unsupported
-        MO_W64_ToW   -> unsupported
-        MO_W64_FromW -> unsupported
-        MO_x64_Neg   -> unsupported
-        MO_x64_Add   -> unsupported
-        MO_x64_Sub   -> unsupported
-        MO_x64_Mul   -> unsupported
-        MO_I64_Quot  -> unsupported
-        MO_I64_Rem   -> unsupported
-        MO_W64_Quot  -> unsupported
-        MO_W64_Rem   -> unsupported
-        MO_x64_And   -> unsupported
-        MO_x64_Or    -> unsupported
-        MO_x64_Xor   -> unsupported
-        MO_x64_Not   -> unsupported
-        MO_x64_Shl   -> unsupported
-        MO_I64_Shr   -> unsupported
-        MO_W64_Shr   -> unsupported
-        MO_x64_Eq    -> unsupported
-        MO_x64_Ne    -> unsupported
-        MO_I64_Ge    -> unsupported
-        MO_I64_Gt    -> unsupported
-        MO_I64_Le    -> unsupported
-        MO_I64_Lt    -> unsupported
-        MO_W64_Ge    -> unsupported
-        MO_W64_Gt    -> unsupported
-        MO_W64_Le    -> unsupported
-        MO_W64_Lt    -> unsupported
+        MO_I64_ToI   -> text "hs_int64ToInt"
+        MO_I64_FromI -> text "hs_intToInt64"
+        MO_W64_ToW   -> text "hs_word64ToWord"
+        MO_W64_FromW -> text "hs_wordToWord64"
+        MO_x64_Neg   -> text "hs_neg64"
+        MO_x64_Add   -> text "hs_add64"
+        MO_x64_Sub   -> text "hs_sub64"
+        MO_x64_Mul   -> text "hs_mul64"
+        MO_I64_Quot  -> text "hs_quotInt64"
+        MO_I64_Rem   -> text "hs_remInt64"
+        MO_W64_Quot  -> text "hs_quotWord64"
+        MO_W64_Rem   -> text "hs_remWord64"
+        MO_x64_And   -> text "hs_and64"
+        MO_x64_Or    -> text "hs_xor64"
+        MO_x64_Xor   -> text "hs_xor64"
+        MO_x64_Not   -> text "hs_not64"
+        MO_x64_Shl   -> text "hs_uncheckedShiftL64"
+        MO_I64_Shr   -> text "hs_uncheckedIShiftRA64"
+        MO_W64_Shr   -> text "hs_uncheckedShiftRL64"
+        MO_x64_Eq    -> text "hs_eq64"
+        MO_x64_Ne    -> text "hs_ne64"
+        MO_I64_Ge    -> text "hs_geInt64"
+        MO_I64_Gt    -> text "hs_gtInt64"
+        MO_I64_Le    -> text "hs_leInt64"
+        MO_I64_Lt    -> text "hs_ltInt64"
+        MO_W64_Ge    -> text "hs_geWord64"
+        MO_W64_Gt    -> text "hs_gtWord64"
+        MO_W64_Le    -> text "hs_leWord64"
+        MO_W64_Lt    -> text "hs_ltWord64"
     where unsupported = panic ("pprCallishMachOp_for_C: " ++ show mop
                             ++ " not supported!")
 
diff --git a/includes/stg/Prim.h b/includes/stg/Prim.h
index 5f432b6f9bd1..20b3d45bee29 100644
--- a/includes/stg/Prim.h
+++ b/includes/stg/Prim.h
@@ -68,7 +68,40 @@ StgWord16 hs_bitrev16(StgWord16 x);
 StgWord32 hs_bitrev32(StgWord32 x);
 StgWord64 hs_bitrev64(StgWord64 x);
 
-/* TODO: longlong.c */
+/* libraries/ghc-prim/cbits/longlong.c */
+#if WORD_SIZE_IN_BITS < 64
+StgInt hs_eq64 (StgWord64 a, StgWord64 b);
+StgInt hs_ne64 (StgWord64 a, StgWord64 b);
+StgInt hs_gtWord64 (StgWord64 a, StgWord64 b);
+StgInt hs_geWord64 (StgWord64 a, StgWord64 b);
+StgInt hs_ltWord64 (StgWord64 a, StgWord64 b);
+StgInt hs_leWord64 (StgWord64 a, StgWord64 b);
+StgInt hs_gtInt64 (StgInt64 a, StgInt64 b);
+StgInt hs_geInt64 (StgInt64 a, StgInt64 b);
+StgInt hs_ltInt64 (StgInt64 a, StgInt64 b);
+StgInt hs_leInt64 (StgInt64 a, StgInt64 b);
+StgInt64 hs_neg64       (StgInt64 a);
+StgWord64 hs_add64      (StgWord64 a, StgWord64 b);
+StgWord64 hs_sub64      (StgWord64 a, StgWord64 b);
+StgWord64 hs_mul64      (StgWord64 a, StgWord64 b);
+StgWord64 hs_remWord64  (StgWord64 a, StgWord64 b);
+StgWord64 hs_quotWord64 (StgWord64 a, StgWord64 b);
+StgInt64 hs_remInt64    (StgInt64 a, StgInt64 b);
+StgInt64 hs_quotInt64   (StgInt64 a, StgInt64 b);
+StgWord64 hs_and64      (StgWord64 a, StgWord64 b);
+StgWord64 hs_or64       (StgWord64 a, StgWord64 b);
+StgWord64 hs_xor64      (StgWord64 a, StgWord64 b);
+StgWord64 hs_not64      (StgWord64 a);
+StgWord64 hs_uncheckedShiftL64   (StgWord64 a, StgInt b);
+StgWord64 hs_uncheckedShiftRL64  (StgWord64 a, StgInt b);
+StgInt64  hs_uncheckedIShiftRA64 (StgInt64 a,  StgInt b);
+StgInt64  hs_intToInt64    (StgInt    i);
+StgInt    hs_int64ToInt    (StgInt64  i);
+StgWord64 hs_int64ToWord64 (StgInt64  i);
+StgWord64 hs_wordToWord64  (StgWord   w);
+StgWord   hs_word64ToWord  (StgWord64 w);
+StgInt64  hs_word64ToInt64 (StgWord64 w);
+#endif
 
 /* libraries/ghc-prim/cbits/pdep.c */
 StgWord64 hs_pdep64(StgWord64 src, StgWord64 mask);
-- 
GitLab