[project @ 1996-04-05 08:26:04 by partain]
[ghc-hetmet.git] / ghc / compiler / basicTypes / UniqSupply.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[UniqSupply]{The @UniqueSupply@ data type and a (monadic) supply thereof}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module UniqSupply (
10
11         UniqSupply,             -- Abstractly
12
13         getUnique, getUniques,  -- basic ops
14
15         UniqSM(..),             -- type: unique supply monad
16         initUs, thenUs, returnUs,
17         mapUs, mapAndUnzipUs, mapAndUnzip3Us,
18         thenMaybeUs, mapAccumLUs,
19
20         mkSplitUniqSupply,
21         splitUniqSupply,
22
23         -- and the access functions for the `builtin' UniqueSupply
24         getBuiltinUniques, mkBuiltinUnique,
25         mkPseudoUnique1, mkPseudoUnique2, mkPseudoUnique3
26   ) where
27
28 import Ubiq{-uitous-}
29
30 import Unique
31 import Util
32
33 import PreludeGlaST
34
35 w2i x = word2Int# x
36 i2w x = int2Word# x
37 i2w_s x = (x :: Int#)
38 \end{code}
39
40
41 %************************************************************************
42 %*                                                                      *
43 \subsection{Splittable Unique supply: @UniqSupply@}
44 %*                                                                      *
45 %************************************************************************
46
47 %************************************************************************
48 %*                                                                      *
49 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
50 %*                                                                      *
51 %************************************************************************
52
53 A value of type @UniqSupply@ is unique, and it can
54 supply {\em one} distinct @Unique@.  Also, from the supply, one can
55 also manufacture an arbitrary number of further @UniqueSupplies@,
56 which will be distinct from the first and from all others.
57
58 \begin{code}
59 data UniqSupply
60   = MkSplitUniqSupply Int       -- make the Unique with this
61                    UniqSupply UniqSupply
62                                 -- when split => these two supplies
63 \end{code}
64
65 \begin{code}
66 mkSplitUniqSupply :: Char -> PrimIO UniqSupply
67
68 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
69 getUnique :: UniqSupply -> Unique
70 getUniques :: Int -> UniqSupply -> [Unique]
71 \end{code}
72
73 \begin{code}
74 mkSplitUniqSupply (MkChar c#)
75   = let
76         mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
77
78         -- here comes THE MAGIC:
79
80         mk_supply#
81           = unsafe_interleave (
82                 mk_unique   `thenPrimIO` \ uniq ->
83                 mk_supply#  `thenPrimIO` \ s1 ->
84                 mk_supply#  `thenPrimIO` \ s2 ->
85                 returnPrimIO (MkSplitUniqSupply uniq s1 s2)
86             )
87           where
88             -- inlined copy of unsafeInterleavePrimIO;
89             -- this is the single-most-hammered bit of code
90             -- in the compiler....
91             unsafe_interleave m s
92               = let
93                     (r, new_s) = m s
94                 in
95                 (r, s)
96
97         mk_unique = _ccall_ genSymZh            `thenPrimIO` \ (W# u#) ->
98                     returnPrimIO (MkInt (w2i (mask# `or#` u#)))
99     in
100     mk_supply#
101
102 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
103 \end{code}
104
105 \begin{code}
106 getUnique (MkSplitUniqSupply (MkInt n) _ _) = mkUniqueGrimily n
107
108 getUniques i@(MkInt i#) supply = i# `get_from` supply
109   where
110     get_from 0# _ = []
111     get_from n# (MkSplitUniqSupply (MkInt u#) _ s2)
112       = mkUniqueGrimily u# : get_from (n# `minusInt#` 1#) s2
113 \end{code}
114
115 %************************************************************************
116 %*                                                                      *
117 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
118 %*                                                                      *
119 %************************************************************************
120
121 \begin{code}
122 type UniqSM result = UniqSupply -> result
123
124 -- the initUs function also returns the final UniqSupply
125
126 initUs :: UniqSupply -> UniqSM a -> (UniqSupply, a)
127
128 initUs init_us m
129   = case (splitUniqSupply init_us) of { (s1, s2) ->
130     (s2, m s1) }
131
132 {-# INLINE thenUs #-}
133 {-# INLINE returnUs #-}
134 {-# INLINE splitUniqSupply #-}
135 \end{code}
136
137 @thenUs@ is where we split the @UniqSupply@.
138 \begin{code}
139 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
140
141 thenUs expr cont us
142   = case (splitUniqSupply us) of { (s1, s2) ->
143     case (expr s1)            of { result ->
144     cont result s2 }}
145 \end{code}
146
147 \begin{code}
148 returnUs :: a -> UniqSM a
149 returnUs result us = result
150
151 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
152
153 mapUs f []     = returnUs []
154 mapUs f (x:xs)
155   = f x         `thenUs` \ r  ->
156     mapUs f xs  `thenUs` \ rs ->
157     returnUs (r:rs)
158
159 mapAndUnzipUs  :: (a -> UniqSM (b,c))   -> [a] -> UniqSM ([b],[c])
160 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
161
162 mapAndUnzipUs f [] = returnUs ([],[])
163 mapAndUnzipUs f (x:xs)
164   = f x                 `thenUs` \ (r1,  r2)  ->
165     mapAndUnzipUs f xs  `thenUs` \ (rs1, rs2) ->
166     returnUs (r1:rs1, r2:rs2)
167
168 mapAndUnzip3Us f [] = returnUs ([],[],[])
169 mapAndUnzip3Us f (x:xs)
170   = f x                 `thenUs` \ (r1,  r2,  r3)  ->
171     mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
172     returnUs (r1:rs1, r2:rs2, r3:rs3)
173
174 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
175 thenMaybeUs m k
176   = m   `thenUs` \ result ->
177     case result of
178       Nothing -> returnUs Nothing
179       Just x  -> k x
180
181 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
182             -> acc
183             -> [x]
184             -> UniqSM (acc, [y])
185
186 mapAccumLUs f b []     = returnUs (b, [])
187 mapAccumLUs f b (x:xs)
188   = f b x                           `thenUs` \ (b__2, x__2) ->
189     mapAccumLUs f b__2 xs           `thenUs` \ (b__3, xs__2) ->
190     returnUs (b__3, x__2:xs__2)
191 \end{code}
192
193 %************************************************************************
194 %*                                                                      *
195 \subsubsection[UniqueSupplies-compiler]{@UniqueSupplies@ specific to the compiler}
196 %*                                                                      *
197 %************************************************************************
198
199 \begin{code}
200 mkPseudoUnique1, mkPseudoUnique2, mkPseudoUnique3,
201  mkBuiltinUnique :: Int -> Unique
202
203 mkBuiltinUnique i = mkUnique 'B' i
204 mkPseudoUnique1 i = mkUnique 'C' i -- used for getItsUnique on Regs
205 mkPseudoUnique2 i = mkUnique 'D' i -- ditto
206 mkPseudoUnique3 i = mkUnique 'E' i -- ditto
207
208 getBuiltinUniques :: Int -> [Unique]
209 getBuiltinUniques n = map (mkUnique 'B') [1 .. n]
210 \end{code}
211
212 The following runs a uniq monad expression, using builtin uniq values:
213 \begin{code}
214 --runBuiltinUs :: UniqSM a -> a
215 --runBuiltinUs m = snd (initUs uniqSupply_B m)
216 \end{code}