1 -----------------------------------------------------------------------------
3 -- Break Arrays in the IO monad
4 -- Entries in the array are Word sized
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.
10 -- (c) The University of Glasgow 2007
12 -----------------------------------------------------------------------------
17 (BA) -- constructor is exported only for ByteCodeGen
29 #if __GLASGOW_HASKELL__ >= 611
30 import GHC.IO ( IO(..) )
32 import GHC.IOBase ( IO(..) )
36 data BreakArray = BA (MutableByteArray# RealWorld)
38 breakOff, breakOn :: Word
43 showBreakArray :: BreakArray -> IO ()
44 showBreakArray array = do
46 | count == sz = return ()
48 val <- readBreakArray array count
49 putStr $ " " ++ show val
54 setBreakOn :: BreakArray -> Int -> IO Bool
55 setBreakOn array index
56 | safeIndex array index = do
57 writeBreakArray array index breakOn
59 | otherwise = return False
61 setBreakOff :: BreakArray -> Int -> IO Bool
62 setBreakOff array index
63 | safeIndex array index = do
64 writeBreakArray array index breakOff
66 | otherwise = return False
68 getBreak :: BreakArray -> Int -> IO (Maybe Word)
70 | safeIndex array index = do
71 val <- readBreakArray array index
73 | otherwise = return Nothing
75 safeIndex :: BreakArray -> Int -> Bool
76 safeIndex array index = index < size array && index >= 0
78 size :: BreakArray -> Int
79 size (BA array) = (I# (sizeofMutableByteArray# array)) `div` wORD_SIZE
81 allocBA :: Int -> IO BreakArray
82 allocBA (I# sz) = IO $ \s1 ->
83 case newByteArray# sz s1 of { (# s2, array #) -> (# s2, BA array #) }
85 -- create a new break array and initialise elements to zero
86 newBreakArray :: Int -> IO BreakArray
87 newBreakArray entries@(I# sz) = do
88 BA array <- allocBA (entries * wORD_SIZE)
90 W# off -> do -- Todo: there must be a better way to write zero as a Word!
92 | n ==# sz = return ()
99 writeBA# :: MutableByteArray# RealWorld -> Int# -> Word# -> IO ()
100 writeBA# array i word = IO $ \s ->
101 case writeWordArray# array i word s of { s -> (# s, () #) }
103 writeBreakArray :: BreakArray -> Int -> Word -> IO ()
104 writeBreakArray (BA array) (I# i) (W# word) = writeBA# array i word
106 readBA# :: MutableByteArray# RealWorld -> Int# -> IO Word
107 readBA# array i = IO $ \s ->
108 case readWordArray# array i s of { (# s, c #) -> (# s, W# c #) }
110 readBreakArray :: BreakArray -> Int -> IO Word
111 readBreakArray (BA array) (I# i) = readBA# array i
114 --stub implementation to make main/, etc., code happier.
115 --IOArray and IOUArray are increasingly non-portable,
116 --still don't have quite the same interface, and (for GHCI)
117 --presumably have a different representation.
118 data BreakArray = Unspecified
119 newBreakArray :: Int -> IO BreakArray
120 newBreakArray _ = return Unspecified