Make dumpIfSet_dyn_or use dumpSDoc
[ghc-hetmet.git] / compiler / main / BreakArray.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Break Arrays in the IO monad
4 -- Entries in the array are Word sized 
5 --
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.
9 --
10 -- (c) The University of Glasgow 2007
11 --
12 -----------------------------------------------------------------------------
13
14 module BreakArray
15   ( BreakArray
16 #ifdef GHCI
17       (BA)  -- constructor is exported only for ByteCodeGen
18 #endif
19   , newBreakArray
20 #ifdef GHCI
21   , getBreak 
22   , setBreakOn 
23   , setBreakOff
24   , showBreakArray
25 #endif
26   ) where
27 #ifdef GHCI
28 import GHC.Exts
29 import GHC.IO ( IO(..) )
30 import Constants
31
32 data BreakArray = BA (MutableByteArray# RealWorld)
33
34 breakOff, breakOn :: Word
35 breakOn  = 1
36 breakOff = 0
37
38 -- XXX crude
39 showBreakArray :: BreakArray -> IO ()
40 showBreakArray array = do
41    let loop count sz
42           | count == sz = return ()
43           | otherwise = do
44                val <- readBreakArray array count 
45                putStr $ " " ++ show val
46                loop (count + 1) sz
47    loop 0 (size array) 
48    putStr "\n"
49
50 setBreakOn :: BreakArray -> Int -> IO Bool 
51 setBreakOn array index
52    | safeIndex array index = do 
53         writeBreakArray array index breakOn 
54         return True
55    | otherwise = return False 
56
57 setBreakOff :: BreakArray -> Int -> IO Bool 
58 setBreakOff array index
59    | safeIndex array index = do
60         writeBreakArray array index breakOff
61         return True
62    | otherwise = return False 
63
64 getBreak :: BreakArray -> Int -> IO (Maybe Word)
65 getBreak array index 
66    | safeIndex array index = do
67         val <- readBreakArray array index 
68         return $ Just val 
69    | otherwise = return Nothing
70
71 safeIndex :: BreakArray -> Int -> Bool
72 safeIndex array index = index < size array && index >= 0
73
74 size :: BreakArray -> Int
75 size (BA array) = (I# (sizeofMutableByteArray# array)) `div` wORD_SIZE
76
77 allocBA :: Int -> IO BreakArray 
78 allocBA (I# sz) = IO $ \s1 ->
79   case newByteArray# sz s1 of { (# s2, array #) -> (# s2, BA array #) }
80
81 -- create a new break array and initialise elements to zero
82 newBreakArray :: Int -> IO BreakArray
83 newBreakArray entries@(I# sz) = do
84    BA array <- allocBA (entries * wORD_SIZE) 
85    case breakOff of 
86       W# off -> do    -- Todo: there must be a better way to write zero as a Word!
87          let loop n
88                 | n ==# sz = return ()
89                 | otherwise = do
90                      writeBA# array n off 
91                      loop (n +# 1#)
92          loop 0#
93    return $ BA array
94
95 writeBA# :: MutableByteArray# RealWorld -> Int# -> Word# -> IO ()
96 writeBA# array i word = IO $ \s ->
97   case writeWordArray# array i word s of { s -> (# s, () #) }
98
99 writeBreakArray :: BreakArray -> Int -> Word -> IO ()
100 writeBreakArray (BA array) (I# i) (W# word) = writeBA# array i word 
101
102 readBA# :: MutableByteArray# RealWorld -> Int# -> IO Word 
103 readBA# array i = IO $ \s -> 
104    case readWordArray# array i s of { (# s, c #) -> (# s, W# c #) }
105
106 readBreakArray :: BreakArray -> Int -> IO Word 
107 readBreakArray (BA array) (I# i) = readBA# array i
108
109 #else /* GHCI */
110 --stub implementation to make main/, etc., code happier.
111 --IOArray and IOUArray are increasingly non-portable,
112 --still don't have quite the same interface, and (for GHCI)
113 --presumably have a different representation.
114 data BreakArray = Unspecified
115 newBreakArray :: Int -> IO BreakArray
116 newBreakArray _ = return Unspecified
117 #endif /* GHCI */
118