2d1a4d491fdd88166c6f123f1ec3b018bef6de76
[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 #if __GLASGOW_HASKELL__ >= 611
30 import GHC.IO ( IO(..) )
31 #else
32 import GHC.IOBase ( IO(..) )
33 #endif
34 import Constants
35
36 data BreakArray = BA (MutableByteArray# RealWorld)
37
38 breakOff, breakOn :: Word
39 breakOn  = 1
40 breakOff = 0
41
42 -- XXX crude
43 showBreakArray :: BreakArray -> IO ()
44 showBreakArray array = do
45    let loop count sz
46           | count == sz = return ()
47           | otherwise = do
48                val <- readBreakArray array count 
49                putStr $ " " ++ show val
50                loop (count + 1) sz
51    loop 0 (size array) 
52    putStr "\n"
53
54 setBreakOn :: BreakArray -> Int -> IO Bool 
55 setBreakOn array index
56    | safeIndex array index = do 
57         writeBreakArray array index breakOn 
58         return True
59    | otherwise = return False 
60
61 setBreakOff :: BreakArray -> Int -> IO Bool 
62 setBreakOff array index
63    | safeIndex array index = do
64         writeBreakArray array index breakOff
65         return True
66    | otherwise = return False 
67
68 getBreak :: BreakArray -> Int -> IO (Maybe Word)
69 getBreak array index 
70    | safeIndex array index = do
71         val <- readBreakArray array index 
72         return $ Just val 
73    | otherwise = return Nothing
74
75 safeIndex :: BreakArray -> Int -> Bool
76 safeIndex array index = index < size array && index >= 0
77
78 size :: BreakArray -> Int
79 size (BA array) = (I# (sizeofMutableByteArray# array)) `div` wORD_SIZE
80
81 allocBA :: Int -> IO BreakArray 
82 allocBA (I# sz) = IO $ \s1 ->
83   case newByteArray# sz s1 of { (# s2, array #) -> (# s2, BA array #) }
84
85 -- create a new break array and initialise elements to zero
86 newBreakArray :: Int -> IO BreakArray
87 newBreakArray entries@(I# sz) = do
88    BA array <- allocBA (entries * wORD_SIZE) 
89    case breakOff of 
90       W# off -> do    -- Todo: there must be a better way to write zero as a Word!
91          let loop n
92                 | n ==# sz = return ()
93                 | otherwise = do
94                      writeBA# array n off 
95                      loop (n +# 1#)
96          loop 0#
97    return $ BA array
98
99 writeBA# :: MutableByteArray# RealWorld -> Int# -> Word# -> IO ()
100 writeBA# array i word = IO $ \s ->
101   case writeWordArray# array i word s of { s -> (# s, () #) }
102
103 writeBreakArray :: BreakArray -> Int -> Word -> IO ()
104 writeBreakArray (BA array) (I# i) (W# word) = writeBA# array i word 
105
106 readBA# :: MutableByteArray# RealWorld -> Int# -> IO Word 
107 readBA# array i = IO $ \s -> 
108    case readWordArray# array i s of { (# s, c #) -> (# s, W# c #) }
109
110 readBreakArray :: BreakArray -> Int -> IO Word 
111 readBreakArray (BA array) (I# i) = readBA# array i
112
113 #else /* GHCI */
114 --stub implementation to make main/, etc., code happier.
115 --IOArray and IOUArray are increasingly non-portable,
116 --still don't have quite the same interface, and (for GHCI)
117 --presumably have a different representation.
118 data BreakArray = Unspecified
119 newBreakArray :: Int -> IO BreakArray
120 newBreakArray _ = return Unspecified
121 #endif /* GHCI */
122