Fix a bug in stack layout with safe foreign calls (#8083)
[ghc.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 castFloatToWord8Array,
11 castDoubleToWord8Array,
12 floatToBytes,
13 doubleToBytes
14 )
15
16 where
17
18 import Data.Array.Unsafe( castSTUArray )
19 import Data.Array.ST hiding( castSTUArray )
20
21 import Control.Monad.ST
22
23 import Data.Word
24
25
26
27 -- -----------------------------------------------------------------------------
28 -- Converting floating-point literals to integrals for printing
29
30 castFloatToWord8Array :: STUArray s Int Float -> ST s (STUArray s Int Word8)
31 castFloatToWord8Array = castSTUArray
32
33 castDoubleToWord8Array :: STUArray s Int Double -> ST s (STUArray s Int Word8)
34 castDoubleToWord8Array = castSTUArray
35
36 -- floatToBytes and doubleToBytes convert to the host's byte
37 -- order. Providing that we're not cross-compiling for a
38 -- target with the opposite endianness, this should work ok
39 -- on all targets.
40
41 -- ToDo: this stuff is very similar to the shenanigans in PprAbs,
42 -- could they be merged?
43
44 floatToBytes :: Float -> [Int]
45 floatToBytes f
46 = runST (do
47 arr <- newArray_ ((0::Int),3)
48 writeArray arr 0 f
49 arr <- castFloatToWord8Array arr
50 i0 <- readArray arr 0
51 i1 <- readArray arr 1
52 i2 <- readArray arr 2
53 i3 <- readArray arr 3
54 return (map fromIntegral [i0,i1,i2,i3])
55 )
56
57 doubleToBytes :: Double -> [Int]
58 doubleToBytes d
59 = runST (do
60 arr <- newArray_ ((0::Int),7)
61 writeArray arr 0 d
62 arr <- castDoubleToWord8Array arr
63 i0 <- readArray arr 0
64 i1 <- readArray arr 1
65 i2 <- readArray arr 2
66 i3 <- readArray arr 3
67 i4 <- readArray arr 4
68 i5 <- readArray arr 5
69 i6 <- readArray arr 6
70 i7 <- readArray arr 7
71 return (map fromIntegral [i0,i1,i2,i3,i4,i5,i6,i7])
72 )