Another round of External Core fixes
[ghc-hetmet.git] / compiler / main / BreakArray.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Break Arrays in the IO monad
4 -- Entries in the array are Word sized 
5 --
6 -- Conceptually, a zero-indexed IOArray of Bools, initially False.
7 -- They're represented as Words with 0==False, 1==True.
8 -- They're used to determine whether GHCI breakpoints are on or off.
9 --
10 -- (c) The University of Glasgow 2007
11 --
12 -----------------------------------------------------------------------------
13
14 module BreakArray
15   ( BreakArray
16 #ifdef GHCI
17       (BA)  -- constructor is exported only for ByteCodeGen
18 #endif
19   , newBreakArray
20 #ifdef GHCI
21   , getBreak 
22   , setBreakOn 
23   , setBreakOff
24   , showBreakArray
25 #endif
26   ) where
27 #ifdef GHCI
28 import GHC.Exts
29 import GHC.IOBase
30 import GHC.Word
31 import Constants
32
33 data BreakArray = BA (MutableByteArray# RealWorld)
34
35 breakOff, breakOn :: Word
36 breakOn  = 1
37 breakOff = 0
38
39 -- XXX crude
40 showBreakArray :: BreakArray -> IO ()
41 showBreakArray array = do
42    let loop count sz
43           | count == sz = return ()
44           | otherwise = do
45                val <- readBreakArray array count 
46                putStr $ " " ++ show val
47                loop (count + 1) sz
48    loop 0 (size array) 
49    putStr "\n"
50
51 setBreakOn :: BreakArray -> Int -> IO Bool 
52 setBreakOn array index
53    | safeIndex array index = do 
54         writeBreakArray array index breakOn 
55         return True
56    | otherwise = return False 
57
58 setBreakOff :: BreakArray -> Int -> IO Bool 
59 setBreakOff array index
60    | safeIndex array index = do
61         writeBreakArray array index breakOff
62         return True
63    | otherwise = return False 
64
65 getBreak :: BreakArray -> Int -> IO (Maybe Word)
66 getBreak array index 
67    | safeIndex array index = do
68         val <- readBreakArray array index 
69         return $ Just val 
70    | otherwise = return Nothing
71
72 safeIndex :: BreakArray -> Int -> Bool
73 safeIndex array index = index < size array && index >= 0
74
75 size :: BreakArray -> Int
76 size (BA array) = (I# (sizeofMutableByteArray# array)) `div` wORD_SIZE
77
78 allocBA :: Int -> IO BreakArray 
79 allocBA (I# sz) = IO $ \s1 ->
80   case newByteArray# sz s1 of { (# s2, array #) -> (# s2, BA array #) }
81
82 -- create a new break array and initialise elements to zero
83 newBreakArray :: Int -> IO BreakArray
84 newBreakArray entries@(I# sz) = do
85    BA array <- allocBA (entries * wORD_SIZE) 
86    case breakOff of 
87       W# off -> do    -- Todo: there must be a better way to write zero as a Word!
88          let loop n
89                 | n ==# sz = return ()
90                 | otherwise = do
91                      writeBA# array n off 
92                      loop (n +# 1#)
93          loop 0#
94    return $ BA array
95
96 writeBA# :: MutableByteArray# RealWorld -> Int# -> Word# -> IO ()
97 writeBA# array i word = IO $ \s ->
98   case writeWordArray# array i word s of { s -> (# s, () #) }
99
100 writeBreakArray :: BreakArray -> Int -> Word -> IO ()
101 writeBreakArray (BA array) (I# i) (W# word) = writeBA# array i word 
102
103 readBA# :: MutableByteArray# RealWorld -> Int# -> IO Word 
104 readBA# array i = IO $ \s -> 
105    case readWordArray# array i s of { (# s, c #) -> (# s, W# c #) }
106
107 readBreakArray :: BreakArray -> Int -> IO Word 
108 readBreakArray (BA array) (I# i) = readBA# array i
109
110 #else /* GHCI */
111 --stub implementation to make main/, etc., code happier.
112 --IOArray and IOUArray are increasingly non-portable,
113 --still don't have quite the same interface, and (for GHCI)
114 --presumably have a different representation.
115 data BreakArray = Unspecified
116 newBreakArray :: Int -> IO BreakArray
117 newBreakArray _ = return Unspecified
118 #endif /* GHCI */
119