1 -----------------------------------------------------------------------------
3 -- Break Arrays in the IO monad
4 -- Entries in the array are Word sized
6 -- (c) The University of Glasgow 2007
8 -----------------------------------------------------------------------------
12 -- constructor is exported only for ByteCodeGen
26 data BreakArray = BA (MutableByteArray# RealWorld)
28 breakOff, breakOn :: Word
29 breakOn = fromIntegral 1
30 breakOff = fromIntegral 0
33 showBreakArray :: BreakArray -> IO ()
34 showBreakArray array = do
36 | count == sz = return ()
38 val <- readBreakArray array count
39 putStr $ " " ++ show val
44 setBreakOn :: BreakArray -> Int -> IO Bool
45 setBreakOn array index
46 | safeIndex array index = do
47 writeBreakArray array index breakOn
49 | otherwise = return False
51 setBreakOff :: BreakArray -> Int -> IO Bool
52 setBreakOff array index
53 | safeIndex array index = do
54 writeBreakArray array index breakOff
56 | otherwise = return False
58 getBreak :: BreakArray -> Int -> IO (Maybe Word)
60 | safeIndex array index = do
61 val <- readBreakArray array index
63 | otherwise = return Nothing
65 safeIndex :: BreakArray -> Int -> Bool
66 safeIndex array index = index < size array && index >= 0
68 size :: BreakArray -> Int
69 size (BA array) = (I# (sizeofMutableByteArray# array)) `div` wORD_SIZE
71 allocBA :: Int -> IO BreakArray
72 allocBA (I# sz) = IO $ \s1 ->
73 case newByteArray# sz s1 of { (# s2, array #) -> (# s2, BA array #) }
75 -- create a new break array and initialise elements to zero
76 newBreakArray :: Int -> IO BreakArray
77 newBreakArray entries@(I# sz) = do
78 BA array <- allocBA (entries * wORD_SIZE)
80 W# off -> do -- Todo: there must be a better way to write zero as a Word!
82 | n ==# sz = return ()
89 writeBA# :: MutableByteArray# RealWorld -> Int# -> Word# -> IO ()
90 writeBA# array i word = IO $ \s ->
91 case writeWordArray# array i word s of { s -> (# s, () #) }
93 writeBreakArray :: BreakArray -> Int -> Word -> IO ()
94 writeBreakArray (BA array) (I# i) (W# word) = writeBA# array i word
96 readBA# :: MutableByteArray# RealWorld -> Int# -> IO Word
97 readBA# array i = IO $ \s ->
98 case readWordArray# array i s of { (# s, c #) -> (# s, W# c #) }
100 readBreakArray :: BreakArray -> Int -> IO Word
101 readBreakArray (BA array) (I# i) = readBA# array i