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 -----------------------------------------------------------------------------
15 -- The above warning supression flag is a temporary kludge.
16 -- While working on this module you are encouraged to remove it and fix
17 -- any warnings in the module. See
18 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
24 (BA) -- constructor is exported only for ByteCodeGen
40 data BreakArray = BA (MutableByteArray# RealWorld)
42 breakOff, breakOn :: Word
47 showBreakArray :: BreakArray -> IO ()
48 showBreakArray array = do
50 | count == sz = return ()
52 val <- readBreakArray array count
53 putStr $ " " ++ show val
58 setBreakOn :: BreakArray -> Int -> IO Bool
59 setBreakOn array index
60 | safeIndex array index = do
61 writeBreakArray array index breakOn
63 | otherwise = return False
65 setBreakOff :: BreakArray -> Int -> IO Bool
66 setBreakOff array index
67 | safeIndex array index = do
68 writeBreakArray array index breakOff
70 | otherwise = return False
72 getBreak :: BreakArray -> Int -> IO (Maybe Word)
74 | safeIndex array index = do
75 val <- readBreakArray array index
77 | otherwise = return Nothing
79 safeIndex :: BreakArray -> Int -> Bool
80 safeIndex array index = index < size array && index >= 0
82 size :: BreakArray -> Int
83 size (BA array) = (I# (sizeofMutableByteArray# array)) `div` wORD_SIZE
85 allocBA :: Int -> IO BreakArray
86 allocBA (I# sz) = IO $ \s1 ->
87 case newByteArray# sz s1 of { (# s2, array #) -> (# s2, BA array #) }
89 -- create a new break array and initialise elements to zero
90 newBreakArray :: Int -> IO BreakArray
91 newBreakArray entries@(I# sz) = do
92 BA array <- allocBA (entries * wORD_SIZE)
94 W# off -> do -- Todo: there must be a better way to write zero as a Word!
96 | n ==# sz = return ()
103 writeBA# :: MutableByteArray# RealWorld -> Int# -> Word# -> IO ()
104 writeBA# array i word = IO $ \s ->
105 case writeWordArray# array i word s of { s -> (# s, () #) }
107 writeBreakArray :: BreakArray -> Int -> Word -> IO ()
108 writeBreakArray (BA array) (I# i) (W# word) = writeBA# array i word
110 readBA# :: MutableByteArray# RealWorld -> Int# -> IO Word
111 readBA# array i = IO $ \s ->
112 case readWordArray# array i s of { (# s, c #) -> (# s, W# c #) }
114 readBreakArray :: BreakArray -> Int -> IO Word
115 readBreakArray (BA array) (I# i) = readBA# array i
118 --stub implementation to make main/, etc., code happier.
119 --IOArray and IOUArray are increasingly non-portable,
120 --still don't have quite the same interface, and (for GHCI)
121 --presumably have a different representation.
122 data BreakArray = Unspecified
123 newBreakArray :: Int -> IO BreakArray
124 newBreakArray _ = return Unspecified