From 84d7b7dd2d91ec9cce154602669b97a084412cf2 Mon Sep 17 00:00:00 2001
From: Reid Barton <rwbarton@gmail.com>
Date: Fri, 6 Sep 2013 19:22:38 -0400
Subject: [PATCH] Make argument types in popcnt.c match declared primop types

On 64-bit Mac OS, gcc 4.2 (which comes with Xcode 4.6) generates code
that assumes that an argument that is smaller than the register
it is passed in has been sign- or zero-extended. But ghc thinks
the types of the PopCnt*Op primops are Word# -> Word#, so it passes
the entire argument word to the hs_popcnt* function as though it was
declared to have an argument of type StgWord. Segfaults ensue.

The easiest fix is to sidestep all this zero-extension business
by declaring the hs_popcnt* functions to take a whole StgWord (when their
argument would fit in a register), thereby matching the list of primops.

Fixes #7684.

(cherry picked from commit ad9bf96815cb5a9bb4acc51c99eff20be3e50da3)
---
 cbits/popcnt.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/cbits/popcnt.c b/cbits/popcnt.c
index b17b624..fc44ee7 100644
--- a/cbits/popcnt.c
+++ b/cbits/popcnt.c
@@ -12,24 +12,24 @@ static const unsigned char popcount_tab[] =
     3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
 };
 
-extern StgWord hs_popcnt8(StgWord8 x);
+extern StgWord hs_popcnt8(StgWord x);
 StgWord
-hs_popcnt8(StgWord8 x)
+hs_popcnt8(StgWord x)
 {
   return popcount_tab[(unsigned char)x];
 }
 
-extern StgWord hs_popcnt16(StgWord16 x);
+extern StgWord hs_popcnt16(StgWord x);
 StgWord
-hs_popcnt16(StgWord16 x)
+hs_popcnt16(StgWord x)
 {
   return popcount_tab[(unsigned char)x] +
       popcount_tab[(unsigned char)(x >> 8)];
 }
 
-extern StgWord hs_popcnt32(StgWord32 x);
+extern StgWord hs_popcnt32(StgWord x);
 StgWord
-hs_popcnt32(StgWord32 x)
+hs_popcnt32(StgWord x)
 {
   return popcount_tab[(unsigned char)x] +
       popcount_tab[(unsigned char)(x >> 8)] +
@@ -53,9 +53,9 @@ hs_popcnt64(StgWord64 x)
 
 #ifdef i386_HOST_ARCH
 
-extern StgWord hs_popcnt(StgWord32 x);
+extern StgWord hs_popcnt(StgWord x);
 StgWord
-hs_popcnt(StgWord32 x)
+hs_popcnt(StgWord x)
 {
   return popcount_tab[(unsigned char)x] +
       popcount_tab[(unsigned char)(x >> 8)] +
@@ -65,9 +65,9 @@ hs_popcnt(StgWord32 x)
 
 #else
 
-extern StgWord hs_popcnt(StgWord64 x);
+extern StgWord hs_popcnt(StgWord x);
 StgWord
-hs_popcnt(StgWord64 x)
+hs_popcnt(StgWord x)
 {
   return popcount_tab[(unsigned char)x] +
       popcount_tab[(unsigned char)(x >> 8)] +
-- 
GitLab