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
33 data BreakArray = BA (MutableByteArray# RealWorld)
35 breakOff, breakOn :: Word
40 showBreakArray :: BreakArray -> IO ()
41 showBreakArray array = do
43 | count == sz = return ()
45 val <- readBreakArray array count
46 putStr $ " " ++ show val
51 setBreakOn :: BreakArray -> Int -> IO Bool
52 setBreakOn array index
53 | safeIndex array index = do
54 writeBreakArray array index breakOn
56 | otherwise = return False
58 setBreakOff :: BreakArray -> Int -> IO Bool
59 setBreakOff array index
60 | safeIndex array index = do
61 writeBreakArray array index breakOff
63 | otherwise = return False
65 getBreak :: BreakArray -> Int -> IO (Maybe Word)
67 | safeIndex array index = do
68 val <- readBreakArray array index
70 | otherwise = return Nothing
72 safeIndex :: BreakArray -> Int -> Bool
73 safeIndex array index = index < size array && index >= 0
75 size :: BreakArray -> Int
76 size (BA array) = (I# (sizeofMutableByteArray# array)) `div` wORD_SIZE
78 allocBA :: Int -> IO BreakArray
79 allocBA (I# sz) = IO $ \s1 ->
80 case newByteArray# sz s1 of { (# s2, array #) -> (# s2, BA array #) }
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)
87 W# off -> do -- Todo: there must be a better way to write zero as a Word!
89 | n ==# sz = return ()
96 writeBA# :: MutableByteArray# RealWorld -> Int# -> Word# -> IO ()
97 writeBA# array i word = IO $ \s ->
98 case writeWordArray# array i word s of { s -> (# s, () #) }
100 writeBreakArray :: BreakArray -> Int -> Word -> IO ()
101 writeBreakArray (BA array) (I# i) (W# word) = writeBA# array i word
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 #) }
107 readBreakArray :: BreakArray -> Int -> IO Word
108 readBreakArray (BA array) (I# i) = readBA# array i
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