merge GHC HEAD
[ghc-hetmet.git] / compiler / nativeGen / PprBase.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Pretty-printing assembly language
4 --
5 -- (c) The University of Glasgow 1993-2005
6 --
7 -----------------------------------------------------------------------------
8
9 module PprBase (
10         asmSDoc,
11         pprCLabel_asm,
12         castFloatToWord8Array,
13         castDoubleToWord8Array,
14         floatToBytes,
15         doubleToBytes
16 )
17
18 where
19
20 import qualified Outputable
21 import CLabel
22 import Pretty
23
24 import Data.Array.ST
25 import Control.Monad.ST
26
27 import Data.Word
28
29
30
31 asmSDoc :: Outputable.SDoc -> Doc
32 asmSDoc d 
33         = Outputable.withPprStyleDoc (Outputable.mkCodeStyle Outputable.AsmStyle) d
34
35
36 pprCLabel_asm :: CLabel -> Doc
37 pprCLabel_asm l 
38         = asmSDoc (pprCLabel l)
39
40
41 -- -----------------------------------------------------------------------------
42 -- Converting floating-point literals to integrals for printing
43
44 castFloatToWord8Array :: STUArray s Int Float -> ST s (STUArray s Int Word8)
45 castFloatToWord8Array = castSTUArray
46
47 castDoubleToWord8Array :: STUArray s Int Double -> ST s (STUArray s Int Word8)
48 castDoubleToWord8Array = castSTUArray
49
50 -- floatToBytes and doubleToBytes convert to the host's byte
51 -- order.  Providing that we're not cross-compiling for a 
52 -- target with the opposite endianness, this should work ok
53 -- on all targets.
54
55 -- ToDo: this stuff is very similar to the shenanigans in PprAbs,
56 -- could they be merged?
57
58 floatToBytes :: Float -> [Int]
59 floatToBytes f
60    = runST (do
61         arr <- newArray_ ((0::Int),3)
62         writeArray arr 0 f
63         arr <- castFloatToWord8Array arr
64         i0 <- readArray arr 0
65         i1 <- readArray arr 1
66         i2 <- readArray arr 2
67         i3 <- readArray arr 3
68         return (map fromIntegral [i0,i1,i2,i3])
69      )
70
71 doubleToBytes :: Double -> [Int]
72 doubleToBytes d
73    = runST (do
74         arr <- newArray_ ((0::Int),7)
75         writeArray arr 0 d
76         arr <- castDoubleToWord8Array arr
77         i0 <- readArray arr 0
78         i1 <- readArray arr 1
79         i2 <- readArray arr 2
80         i3 <- readArray arr 3
81         i4 <- readArray arr 4
82         i5 <- readArray arr 5
83         i6 <- readArray arr 6
84         i7 <- readArray arr 7
85         return (map fromIntegral [i0,i1,i2,i3,i4,i5,i6,i7])
86      )