summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ericson <John.Ericson@Obsidian.Systems>2021-03-14 03:31:21 +0000
committerJohn Ericson <John.Ericson@Obsidian.Systems>2021-03-17 22:37:11 +0000
commit9b91e63a67750e99f6915c65f428b7a613cc0005 (patch)
treefdec9a5a75aa3218fad6de984b2fb603d455a103
parent43a64744458dcec82102d6daf488c33b99f24745 (diff)
downloadhaskell-9b91e63a67750e99f6915c65f428b7a613cc0005.tar.gz
Fix literals for unregisterized backend of small typeswip/print-c-cast-not-suffix
All credit to @hsyl20, who in https://gitlab.haskell.org/ghc/ghc/-/merge_requests/4717#note_338560 figured out this was a problem. To fix this, we use casts in addition to the shrinking and suffixing that is already done. It might make for more verbose code, I don't think that matters too much. In the future, perhaps some of the shrinking and suffixing can be removed for being redundant. That proved less trivial than it sounds, so this wasn't done at this time. Progress towards #19026 Metric Increase: T12707 T13379 Co-authored-by: Sylvain Henry <hsyl20@gmail.com>
-rw-r--r--compiler/GHC/CmmToC.hs21
1 files changed, 16 insertions, 5 deletions
diff --git a/compiler/GHC/CmmToC.hs b/compiler/GHC/CmmToC.hs
index d7596f0aac..49f0567a81 100644
--- a/compiler/GHC/CmmToC.hs
+++ b/compiler/GHC/CmmToC.hs
@@ -1304,13 +1304,24 @@ wordShift platform = widthInLog (wordWidth platform)
commafy :: [SDoc] -> SDoc
commafy xs = hsep $ punctuate comma xs
--- Print in C hex format: 0x13fa
+-- | Print in C hex format
+--
+-- Examples:
+--
+-- 5114 :: W32 ===> ((StgWord32)0x13faU)
+-- (-5114) :: W32 ===> ((StgWord32)(-0x13faU))
+--
+-- We use casts to support types smaller than `unsigned int`; C literal
+-- suffixes support longer but not shorter types.
pprHexVal :: Platform -> Integer -> Width -> SDoc
-pprHexVal platform w rep
- | w < 0 = parens (char '-' <>
- text "0x" <> intToDoc (-w) <> repsuffix rep)
- | otherwise = text "0x" <> intToDoc w <> repsuffix rep
+pprHexVal platform w rep = parens ctype <> rawlit
where
+ rawlit
+ | w < 0 = parens (char '-' <>
+ text "0x" <> intToDoc (-w) <> repsuffix rep)
+ | otherwise = text "0x" <> intToDoc w <> repsuffix rep
+ ctype = machRep_U_CType platform rep
+
-- type suffix for literals:
-- Integer literals are unsigned in Cmm/C. We explicitly cast to
-- signed values for doing signed operations, but at all other