2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[PrimOp]{Primitive operations (machine-level)}
8 PrimOp(..), allThePrimOps,
10 primOpTag, maxPrimOpTag, primOpOcc,
12 primOpOutOfLine, primOpNeedsWrapper,
13 primOpOkForSpeculation, primOpIsCheap, primOpIsDupable,
15 getPrimOpResultInfo, PrimOpResultInfo(..)
18 #include "HsVersions.h"
25 import OccName ( OccName, pprOccName, mkVarOcc )
26 import TyCon ( TyCon, isPrimTyCon, tyConPrimRep, PrimRep(..) )
27 import Type ( Type, mkForAllTys, mkFunTy, mkFunTys, tyConAppTyCon,
29 import BasicTypes ( Arity, Boxity(..) )
34 %************************************************************************
36 \subsection[PrimOp-datatype]{Datatype for @PrimOp@ (an enumeration)}
38 %************************************************************************
40 These are in \tr{state-interface.verb} order.
46 #include "primop-data-decl.hs-incl"
49 Used for the Ord instance
52 primOpTag :: PrimOp -> Int
53 primOpTag op = iBox (tagOf_PrimOp op)
56 -- tagOf_PrimOp :: PrimOp -> FastInt
57 #include "primop-tag.hs-incl"
58 tagOf_PrimOp op = pprPanic# "tagOf_PrimOp: pattern-match" (ppr op)
61 instance Eq PrimOp where
62 op1 == op2 = tagOf_PrimOp op1 ==# tagOf_PrimOp op2
64 instance Ord PrimOp where
65 op1 < op2 = tagOf_PrimOp op1 <# tagOf_PrimOp op2
66 op1 <= op2 = tagOf_PrimOp op1 <=# tagOf_PrimOp op2
67 op1 >= op2 = tagOf_PrimOp op1 >=# tagOf_PrimOp op2
68 op1 > op2 = tagOf_PrimOp op1 ># tagOf_PrimOp op2
69 op1 `compare` op2 | op1 < op2 = LT
73 instance Outputable PrimOp where
76 instance Show PrimOp where
77 showsPrec p op = showsPrecSDoc p (pprPrimOp op)
80 An @Enum@-derived list would be better; meanwhile... (ToDo)
83 allThePrimOps :: [PrimOp]
85 #include "primop-list.hs-incl"
88 %************************************************************************
90 \subsection[PrimOp-info]{The essential info about each @PrimOp@}
92 %************************************************************************
94 The @String@ in the @PrimOpInfos@ is the ``base name'' by which the user may
95 refer to the primitive operation. The conventional \tr{#}-for-
96 unboxed ops is added on later.
98 The reason for the funny characters in the names is so we do not
99 interfere with the programmer's Haskell name spaces.
101 We use @PrimKinds@ for the ``type'' information, because they're
102 (slightly) more convenient to use than @TyCons@.
105 = Dyadic OccName -- string :: T -> T -> T
107 | Monadic OccName -- string :: T -> T
109 | Compare OccName -- string :: T -> T -> Bool
112 | GenPrimOp OccName -- string :: \/a1..an . T1 -> .. -> Tk -> T
117 mkDyadic str ty = Dyadic (mkVarOcc str) ty
118 mkMonadic str ty = Monadic (mkVarOcc str) ty
119 mkCompare str ty = Compare (mkVarOcc str) ty
120 mkGenPrimOp str tvs tys ty = GenPrimOp (mkVarOcc str) tvs tys ty
123 %************************************************************************
125 \subsubsection{Strictness}
127 %************************************************************************
129 Not all primops are strict!
132 primOpStrictness :: PrimOp -> Arity -> StrictSig
133 -- See Demand.StrictnessInfo for discussion of what the results
134 -- The arity should be the arity of the primop; that's why
135 -- this function isn't exported.
136 #include "primop-strictness.hs-incl"
139 %************************************************************************
141 \subsubsection[PrimOp-comparison]{PrimOpInfo basic comparison ops}
143 %************************************************************************
145 @primOpInfo@ gives all essential information (from which everything
146 else, notably a type, can be constructed) for each @PrimOp@.
149 primOpInfo :: PrimOp -> PrimOpInfo
150 #include "primop-primop-info.hs-incl"
153 Here are a load of comments from the old primOp info:
155 A @Word#@ is an unsigned @Int#@.
157 @decodeFloat#@ is given w/ Integer-stuff (it's similar).
159 @decodeDouble#@ is given w/ Integer-stuff (it's similar).
161 Decoding of floating-point numbers is sorta Integer-related. Encoding
162 is done with plain ccalls now (see PrelNumExtra.lhs).
164 A @Weak@ Pointer is created by the @mkWeak#@ primitive:
166 mkWeak# :: k -> v -> f -> State# RealWorld
167 -> (# State# RealWorld, Weak# v #)
169 In practice, you'll use the higher-level
171 data Weak v = Weak# v
172 mkWeak :: k -> v -> IO () -> IO (Weak v)
174 The following operation dereferences a weak pointer. The weak pointer
175 may have been finalized, so the operation returns a result code which
176 must be inspected before looking at the dereferenced value.
178 deRefWeak# :: Weak# v -> State# RealWorld ->
179 (# State# RealWorld, v, Int# #)
181 Only look at v if the Int# returned is /= 0 !!
183 The higher-level op is
185 deRefWeak :: Weak v -> IO (Maybe v)
187 Weak pointers can be finalized early by using the finalize# operation:
189 finalizeWeak# :: Weak# v -> State# RealWorld ->
190 (# State# RealWorld, Int#, IO () #)
192 The Int# returned is either
194 0 if the weak pointer has already been finalized, or it has no
195 finalizer (the third component is then invalid).
197 1 if the weak pointer is still alive, with the finalizer returned
198 as the third component.
200 A {\em stable name/pointer} is an index into a table of stable name
201 entries. Since the garbage collector is told about stable pointers,
202 it is safe to pass a stable pointer to external systems such as C
206 makeStablePtr# :: a -> State# RealWorld -> (# State# RealWorld, StablePtr# a #)
207 freeStablePtr :: StablePtr# a -> State# RealWorld -> State# RealWorld
208 deRefStablePtr# :: StablePtr# a -> State# RealWorld -> (# State# RealWorld, a #)
209 eqStablePtr# :: StablePtr# a -> StablePtr# a -> Int#
212 It may seem a bit surprising that @makeStablePtr#@ is a @IO@
213 operation since it doesn't (directly) involve IO operations. The
214 reason is that if some optimisation pass decided to duplicate calls to
215 @makeStablePtr#@ and we only pass one of the stable pointers over, a
216 massive space leak can result. Putting it into the IO monad
217 prevents this. (Another reason for putting them in a monad is to
218 ensure correct sequencing wrt the side-effecting @freeStablePtr@
221 An important property of stable pointers is that if you call
222 makeStablePtr# twice on the same object you get the same stable
225 Note that we can implement @freeStablePtr#@ using @_ccall_@ (and,
226 besides, it's not likely to be used from Haskell) so it's not a
229 Question: Why @RealWorld@ - won't any instance of @_ST@ do the job? [ADR]
234 A stable name is like a stable pointer, but with three important differences:
236 (a) You can't deRef one to get back to the original object.
237 (b) You can convert one to an Int.
238 (c) You don't need to 'freeStableName'
240 The existence of a stable name doesn't guarantee to keep the object it
241 points to alive (unlike a stable pointer), hence (a).
245 (a) makeStableName always returns the same value for a given
246 object (same as stable pointers).
248 (b) if two stable names are equal, it implies that the objects
249 from which they were created were the same.
251 (c) stableNameToInt always returns the same Int for a given
255 -- HWL: The first 4 Int# in all par... annotations denote:
256 -- name, granularity info, size of result, degree of parallelism
257 -- Same structure as _seq_ i.e. returns Int#
258 -- KSW: v, the second arg in parAt# and parAtForNow#, is used only to determine
259 -- `the processor containing the expression v'; it is not evaluated
261 These primops are pretty wierd.
263 dataToTag# :: a -> Int (arg must be an evaluated data type)
264 tagToEnum# :: Int -> a (result type must be an enumerated type)
266 The constraints aren't currently checked by the front end, but the
267 code generator will fall over if they aren't satisfied.
271 primOpInfo op = pprPanic "primOpInfo:" (ppr op)
275 %************************************************************************
277 \subsubsection[PrimOp-ool]{Which PrimOps are out-of-line}
279 %************************************************************************
281 Some PrimOps need to be called out-of-line because they either need to
282 perform a heap check or they block.
286 primOpOutOfLine :: PrimOp -> Bool
287 #include "primop-out-of-line.hs-incl"
291 primOpOkForSpeculation
292 ~~~~~~~~~~~~~~~~~~~~~~
293 Sometimes we may choose to execute a PrimOp even though it isn't
294 certain that its result will be required; ie execute them
295 ``speculatively''. The same thing as ``cheap eagerness.'' Usually
296 this is OK, because PrimOps are usually cheap, but it isn't OK for
297 (a)~expensive PrimOps and (b)~PrimOps which can fail.
299 PrimOps that have side effects also should not be executed speculatively.
301 Ok-for-speculation also means that it's ok *not* to execute the
305 Here the result is not used, so we can discard the primop. Anything
306 that has side effects mustn't be dicarded in this way, of course!
308 See also @primOpIsCheap@ (below).
312 primOpOkForSpeculation :: PrimOp -> Bool
313 -- See comments with CoreUtils.exprOkForSpeculation
314 primOpOkForSpeculation op
315 = not (primOpHasSideEffects op || primOpOutOfLine op || primOpCanFail op)
321 @primOpIsCheap@, as used in \tr{SimplUtils.lhs}. For now (HACK
322 WARNING), we just borrow some other predicates for a
323 what-should-be-good-enough test. "Cheap" means willing to call it more
324 than once, and/or push it inside a lambda. The latter could change the
325 behaviour of 'seq' for primops that can fail, so we don't treat them as cheap.
328 primOpIsCheap :: PrimOp -> Bool
329 primOpIsCheap op = primOpOkForSpeculation op
330 -- In March 2001, we changed this to
331 -- primOpIsCheap op = False
332 -- thereby making *no* primops seem cheap. But this killed eta
333 -- expansion on case (x ==# y) of True -> \s -> ...
334 -- which is bad. In particular a loop like
337 -- loop i | i == n = return ()
338 -- | otherwise = bar i >> loop (i+1)
339 -- allocated a closure every time round because it doesn't eta expand.
341 -- The problem that originally gave rise to the change was
342 -- let x = a +# b *# c in x +# x
343 -- were we don't want to inline x. But primopIsCheap doesn't control
344 -- that (it's exprIsDupable that does) so the problem doesn't occur
345 -- even if primOpIsCheap sometimes says 'True'.
350 primOpIsDupable means that the use of the primop is small enough to
351 duplicate into different case branches. See CoreUtils.exprIsDupable.
354 primOpIsDupable :: PrimOp -> Bool
355 -- See comments with CoreUtils.exprIsDupable
356 -- We say it's dupable it isn't implemented by a C call with a wrapper
357 primOpIsDupable op = not (primOpNeedsWrapper op)
362 primOpCanFail :: PrimOp -> Bool
363 #include "primop-can-fail.hs-incl"
366 And some primops have side-effects and so, for example, must not be
370 primOpHasSideEffects :: PrimOp -> Bool
371 #include "primop-has-side-effects.hs-incl"
374 Inline primitive operations that perform calls need wrappers to save
375 any live variables that are stored in caller-saves registers.
378 primOpNeedsWrapper :: PrimOp -> Bool
379 #include "primop-needs-wrapper.hs-incl"
383 primOpType :: PrimOp -> Type -- you may want to use primOpSig instead
385 = case (primOpInfo op) of
386 Dyadic occ ty -> dyadic_fun_ty ty
387 Monadic occ ty -> monadic_fun_ty ty
388 Compare occ ty -> compare_fun_ty ty
390 GenPrimOp occ tyvars arg_tys res_ty ->
391 mkForAllTys tyvars (mkFunTys arg_tys res_ty)
393 primOpOcc :: PrimOp -> OccName
394 primOpOcc op = case (primOpInfo op) of
398 GenPrimOp occ _ _ _ -> occ
400 -- primOpSig is like primOpType but gives the result split apart:
401 -- (type variables, argument types, result type)
402 -- It also gives arity, strictness info
404 primOpSig :: PrimOp -> ([TyVar], [Type], Type, Arity, StrictSig)
406 = (tyvars, arg_tys, res_ty, arity, primOpStrictness op arity)
408 arity = length arg_tys
409 (tyvars, arg_tys, res_ty)
410 = case (primOpInfo op) of
411 Monadic occ ty -> ([], [ty], ty )
412 Dyadic occ ty -> ([], [ty,ty], ty )
413 Compare occ ty -> ([], [ty,ty], boolTy)
414 GenPrimOp occ tyvars arg_tys res_ty
415 -> (tyvars, arg_tys, res_ty)
419 data PrimOpResultInfo
420 = ReturnsPrim PrimRep
423 -- Some PrimOps need not return a manifest primitive or algebraic value
424 -- (i.e. they might return a polymorphic value). These PrimOps *must*
425 -- be out of line, or the code generator won't work.
427 getPrimOpResultInfo :: PrimOp -> PrimOpResultInfo
428 getPrimOpResultInfo op
429 = case (primOpInfo op) of
430 Dyadic _ ty -> ReturnsPrim (typePrimRep ty)
431 Monadic _ ty -> ReturnsPrim (typePrimRep ty)
432 Compare _ ty -> ReturnsAlg boolTyCon
433 GenPrimOp _ _ _ ty | isPrimTyCon tc -> ReturnsPrim (tyConPrimRep tc)
434 | otherwise -> ReturnsAlg tc
436 tc = tyConAppTyCon ty
437 -- All primops return a tycon-app result
438 -- The tycon can be an unboxed tuple, though, which
439 -- gives rise to a ReturnAlg
442 The commutable ops are those for which we will try to move constants
443 to the right hand side for strength reduction.
446 commutableOp :: PrimOp -> Bool
447 #include "primop-commutable.hs-incl"
452 dyadic_fun_ty ty = mkFunTys [ty, ty] ty
453 monadic_fun_ty ty = mkFunTy ty ty
454 compare_fun_ty ty = mkFunTys [ty, ty] boolTy
459 pprPrimOp :: PrimOp -> SDoc
460 pprPrimOp other_op = pprOccName (primOpOcc other_op)