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(..) )
37 data BreakArray = BA (MutableByteArray# RealWorld)
39 breakOff, breakOn :: Word
44 showBreakArray :: BreakArray -> IO ()
45 showBreakArray array = do
47 | count == sz = return ()
49 val <- readBreakArray array count
50 putStr $ " " ++ show val
55 setBreakOn :: BreakArray -> Int -> IO Bool
56 setBreakOn array index
57 | safeIndex array index = do
58 writeBreakArray array index breakOn
60 | otherwise = return False
62 setBreakOff :: BreakArray -> Int -> IO Bool
63 setBreakOff array index
64 | safeIndex array index = do
65 writeBreakArray array index breakOff
67 | otherwise = return False
69 getBreak :: BreakArray -> Int -> IO (Maybe Word)
71 | safeIndex array index = do
72 val <- readBreakArray array index
74 | otherwise = return Nothing
76 safeIndex :: BreakArray -> Int -> Bool
77 safeIndex array index = index < size array && index >= 0
79 size :: BreakArray -> Int
80 size (BA array) = (I# (sizeofMutableByteArray# array)) `div` wORD_SIZE
82 allocBA :: Int -> IO BreakArray
83 allocBA (I# sz) = IO $ \s1 ->
84 case newByteArray# sz s1 of { (# s2, array #) -> (# s2, BA array #) }
86 -- create a new break array and initialise elements to zero
87 newBreakArray :: Int -> IO BreakArray
88 newBreakArray entries@(I# sz) = do
89 BA array <- allocBA (entries * wORD_SIZE)
91 W# off -> do -- Todo: there must be a better way to write zero as a Word!
93 | n ==# sz = return ()
100 writeBA# :: MutableByteArray# RealWorld -> Int# -> Word# -> IO ()
101 writeBA# array i word = IO $ \s ->
102 case writeWordArray# array i word s of { s -> (# s, () #) }
104 writeBreakArray :: BreakArray -> Int -> Word -> IO ()
105 writeBreakArray (BA array) (I# i) (W# word) = writeBA# array i word
107 readBA# :: MutableByteArray# RealWorld -> Int# -> IO Word
108 readBA# array i = IO $ \s ->
109 case readWordArray# array i s of { (# s, c #) -> (# s, W# c #) }
111 readBreakArray :: BreakArray -> Int -> IO Word
112 readBreakArray (BA array) (I# i) = readBA# array i
115 --stub implementation to make main/, etc., code happier.
116 --IOArray and IOUArray are increasingly non-portable,
117 --still don't have quite the same interface, and (for GHCI)
118 --presumably have a different representation.
119 data BreakArray = Unspecified
120 newBreakArray :: Int -> IO BreakArray
121 newBreakArray _ = return Unspecified