2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[UniqSupply]{The @UniqueSupply@ data type and a (monadic) supply thereof}
7 #include "HsVersions.h"
11 UniqSupply, -- Abstractly
13 getUnique, getUniques, -- basic ops
15 UniqSM(..), -- type: unique supply monad
16 initUs, thenUs, returnUs,
22 -- and the access functions for the `builtin' UniqueSupply
23 getBuiltinUniques, mkBuiltinUnique,
24 mkPseudoUnique1, mkPseudoUnique2, mkPseudoUnique3
40 %************************************************************************
42 \subsection{Splittable Unique supply: @UniqSupply@}
44 %************************************************************************
46 %************************************************************************
48 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
50 %************************************************************************
52 A value of type @UniqSupply@ is unique, and it can
53 supply {\em one} distinct @Unique@. Also, from the supply, one can
54 also manufacture an arbitrary number of further @UniqueSupplies@,
55 which will be distinct from the first and from all others.
59 = MkSplitUniqSupply Int -- make the Unique with this
61 -- when split => these two supplies
65 mkSplitUniqSupply :: Char -> PrimIO UniqSupply
67 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
68 getUnique :: UniqSupply -> Unique
69 getUniques :: Int -> UniqSupply -> [Unique]
73 mkSplitUniqSupply (MkChar c#)
75 mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
77 -- here comes THE MAGIC:
81 mk_unique `thenPrimIO` \ uniq ->
82 mk_supply# `thenPrimIO` \ s1 ->
83 mk_supply# `thenPrimIO` \ s2 ->
84 returnPrimIO (MkSplitUniqSupply uniq s1 s2)
87 -- inlined copy of unsafeInterleavePrimIO;
88 -- this is the single-most-hammered bit of code
89 -- in the compiler....
96 mk_unique = _ccall_ genSymZh `thenPrimIO` \ (W# u#) ->
97 returnPrimIO (MkInt (w2i (mask# `or#` u#)))
101 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
105 getUnique (MkSplitUniqSupply (MkInt n) _ _) = mkUniqueGrimily n
107 getUniques i@(MkInt i#) supply = i# `get_from` supply
110 get_from n# (MkSplitUniqSupply (MkInt u#) _ s2)
111 = mkUniqueGrimily u# : get_from (n# `minusInt#` 1#) s2
114 %************************************************************************
116 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
118 %************************************************************************
121 type UniqSM result = UniqSupply -> result
123 -- the initUs function also returns the final UniqSupply
125 initUs :: UniqSupply -> UniqSM a -> (UniqSupply, a)
128 = case (splitUniqSupply init_us) of { (s1, s2) ->
131 {-# INLINE thenUs #-}
132 {-# INLINE returnUs #-}
133 {-# INLINE splitUniqSupply #-}
136 @thenUs@ is where we split the @UniqSupply@.
138 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
141 = case (splitUniqSupply us) of { (s1, s2) ->
142 case (expr s1) of { result ->
147 returnUs :: a -> UniqSM a
148 returnUs result us = result
150 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
152 mapUs f [] = returnUs []
154 = f x `thenUs` \ r ->
155 mapUs f xs `thenUs` \ rs ->
158 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
160 mapAndUnzipUs f [] = returnUs ([],[])
161 mapAndUnzipUs f (x:xs)
162 = f x `thenUs` \ (r1, r2) ->
163 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
164 returnUs (r1:rs1, r2:rs2)
167 %************************************************************************
169 \subsubsection[UniqueSupplies-compiler]{@UniqueSupplies@ specific to the compiler}
171 %************************************************************************
174 mkPseudoUnique1, mkPseudoUnique2, mkPseudoUnique3,
175 mkBuiltinUnique :: Int -> Unique
177 mkBuiltinUnique i = mkUnique 'B' i
178 mkPseudoUnique1 i = mkUnique 'C' i -- used for getItsUnique on Regs
179 mkPseudoUnique2 i = mkUnique 'D' i -- ditto
180 mkPseudoUnique3 i = mkUnique 'E' i -- ditto
182 getBuiltinUniques :: Int -> [Unique]
183 getBuiltinUniques n = map (mkUnique 'B') [1 .. n]
186 The following runs a uniq monad expression, using builtin uniq values:
188 --runBuiltinUs :: UniqSM a -> a
189 --runBuiltinUs m = snd (initUs uniqSupply_B m)