- MachChar ch | code_style -> hcat [ptext SLIT("(C_)"), char '\'', text (charToC ch), char '\'']
- | ifaceStyle sty -> char '\'' <> text (charToEasyHaskell ch) <> char '\''
- | otherwise -> text ['\'', ch, '\'']
-
- MachStr s | code_style -> doubleQuotes (text (stringToC (_UNPK_ s)))
- | otherwise -> text (show (_UNPK_ s))
-
- NoRepStr s | code_style -> pprPanic "NoRep in code style" (ppr lit)
- | otherwise -> ptext SLIT("_string_") <+> text (show (_UNPK_ s))
-
- MachInt i signed | code_style && out_of_range
- -> pprPanic "" (hsep [text "ERROR: Int ", text (show i), text "out of range",
- brackets (ppr range_min <+> text ".." <+> ppr range_max)])
- | otherwise -> integer i
-
- where
- range_min = if signed then minInt else 0
- range_max = maxInt
- out_of_range = not (i >= toInteger range_min && i <= toInteger range_max)
-
- MachFloat f | code_style -> ptext SLIT("(StgFloat)") <> rational f
- | otherwise -> ptext SLIT("_float_") <+> rational f
-
- MachDouble d -> rational d
-
- MachAddr p | code_style -> ptext SLIT("(void*)") <> integer p
- | otherwise -> ptext SLIT("_addr_") <+> integer p
+ MachChar ch | code_style -> hcat [ptext SLIT("(C_)"), text (show (ord ch))]
+ | otherwise -> pprHsChar ch
+
+ MachStr s | code_style -> pprFSInCStyle s
+ | otherwise -> pprHsString s
+ -- Warning: printing MachStr in code_style assumes it contains
+ -- only characters '\0'..'\xFF'!
+
+ MachInt i | code_style && i == tARGET_MIN_INT -> parens (integer (i+1) <> text "-1")
+ -- Avoid a problem whereby gcc interprets
+ -- the constant minInt as unsigned.
+ | otherwise -> pprIntVal i
+
+ MachInt64 i | code_style -> pprIntVal i -- Same problem with gcc???
+ | otherwise -> ptext SLIT("__int64") <+> integer i
+
+ MachWord w | code_style -> pprHexVal w
+ | otherwise -> ptext SLIT("__word") <+> integer w
+
+ MachWord64 w | code_style -> pprHexVal w
+ | otherwise -> ptext SLIT("__word64") <+> integer w
+
+ MachFloat f | code_style -> ptext SLIT("(StgFloat)") <> code_rational f
+ | otherwise -> ptext SLIT("__float") <+> rational f
+
+ MachDouble d | code_style -> code_rational d
+ | otherwise -> rational d
+
+ MachNullAddr | code_style -> ptext SLIT("(void*)0")
+ | otherwise -> ptext SLIT("__NULL")
+
+ MachLabel l mb
+ | code_style -> ptext SLIT("(&") <> ftext l <> char ')'
+ | otherwise -> ptext SLIT("__label") <+>
+ case mb of
+ Nothing -> pprHsString l
+ Just x -> doubleQuotes (text (unpackFS l ++ '@':show x))
+
+-- negative floating literals in code style need parentheses to avoid
+-- interacting with surrounding syntax.
+code_rational d | d < 0 = parens (rational d)
+ | otherwise = rational d
+
+pprIntVal :: Integer -> SDoc
+-- Print negative integers with parens to be sure it's unambiguous
+pprIntVal i | i < 0 = parens (integer i)
+ | otherwise = integer i
+
+pprHexVal :: Integer -> SDoc
+-- Print in C hex format: 0x13fa
+pprHexVal 0 = ptext SLIT("0x0")
+pprHexVal w = ptext SLIT("0x") <> go w
+ where
+ go 0 = empty
+ go w = go quot <> dig
+ where
+ (quot,rem) = w `quotRem` 16
+ dig | rem < 10 = char (chr (fromInteger rem + ord '0'))
+ | otherwise = char (chr (fromInteger rem - 10 + ord 'a'))
+\end{code}