2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[PrimOp]{Primitive operations (machine-level)}
8 PrimOp(..), allThePrimOps,
10 primOpTag, maxPrimOpTag, primOpOcc,
14 primOpOutOfLine, primOpNeedsWrapper,
15 primOpOkForSpeculation, primOpIsCheap, primOpIsDupable,
17 getPrimOpResultInfo, PrimOpResultInfo(..)
20 #include "HsVersions.h"
27 import OccName ( OccName, pprOccName, mkVarOccFS )
28 import TyCon ( TyCon, isPrimTyCon, tyConPrimRep, PrimRep(..) )
29 import Type ( Type, mkForAllTys, mkFunTy, mkFunTys, tyConAppTyCon,
31 import BasicTypes ( Arity, Boxity(..) )
32 import Unique ( Unique, mkPrimOpIdUnique )
37 %************************************************************************
39 \subsection[PrimOp-datatype]{Datatype for @PrimOp@ (an enumeration)}
41 %************************************************************************
43 These are in \tr{state-interface.verb} order.
49 #include "primop-data-decl.hs-incl"
52 Used for the Ord instance
55 primOpTag :: PrimOp -> Int
56 primOpTag op = iBox (tagOf_PrimOp op)
59 -- tagOf_PrimOp :: PrimOp -> FastInt
60 #include "primop-tag.hs-incl"
63 instance Eq PrimOp where
64 op1 == op2 = tagOf_PrimOp op1 ==# tagOf_PrimOp op2
66 instance Ord PrimOp where
67 op1 < op2 = tagOf_PrimOp op1 <# tagOf_PrimOp op2
68 op1 <= op2 = tagOf_PrimOp op1 <=# tagOf_PrimOp op2
69 op1 >= op2 = tagOf_PrimOp op1 >=# tagOf_PrimOp op2
70 op1 > op2 = tagOf_PrimOp op1 ># tagOf_PrimOp op2
71 op1 `compare` op2 | op1 < op2 = LT
75 instance Outputable PrimOp where
78 instance Show PrimOp where
79 showsPrec p op = showsPrecSDoc p (pprPrimOp op)
82 An @Enum@-derived list would be better; meanwhile... (ToDo)
85 allThePrimOps :: [PrimOp]
87 #include "primop-list.hs-incl"
91 tagToEnumKey :: Unique
92 tagToEnumKey = mkPrimOpIdUnique (primOpTag TagToEnumOp)
97 %************************************************************************
99 \subsection[PrimOp-info]{The essential info about each @PrimOp@}
101 %************************************************************************
103 The @String@ in the @PrimOpInfos@ is the ``base name'' by which the user may
104 refer to the primitive operation. The conventional \tr{#}-for-
105 unboxed ops is added on later.
107 The reason for the funny characters in the names is so we do not
108 interfere with the programmer's Haskell name spaces.
110 We use @PrimKinds@ for the ``type'' information, because they're
111 (slightly) more convenient to use than @TyCons@.
114 = Dyadic OccName -- string :: T -> T -> T
116 | Monadic OccName -- string :: T -> T
118 | Compare OccName -- string :: T -> T -> Bool
121 | GenPrimOp OccName -- string :: \/a1..an . T1 -> .. -> Tk -> T
126 mkDyadic str ty = Dyadic (mkVarOccFS str) ty
127 mkMonadic str ty = Monadic (mkVarOccFS str) ty
128 mkCompare str ty = Compare (mkVarOccFS str) ty
129 mkGenPrimOp str tvs tys ty = GenPrimOp (mkVarOccFS str) tvs tys ty
132 %************************************************************************
134 \subsubsection{Strictness}
136 %************************************************************************
138 Not all primops are strict!
141 primOpStrictness :: PrimOp -> Arity -> StrictSig
142 -- See Demand.StrictnessInfo for discussion of what the results
143 -- The arity should be the arity of the primop; that's why
144 -- this function isn't exported.
145 #include "primop-strictness.hs-incl"
148 %************************************************************************
150 \subsubsection[PrimOp-comparison]{PrimOpInfo basic comparison ops}
152 %************************************************************************
154 @primOpInfo@ gives all essential information (from which everything
155 else, notably a type, can be constructed) for each @PrimOp@.
158 primOpInfo :: PrimOp -> PrimOpInfo
159 #include "primop-primop-info.hs-incl"
162 Here are a load of comments from the old primOp info:
164 A @Word#@ is an unsigned @Int#@.
166 @decodeFloat#@ is given w/ Integer-stuff (it's similar).
168 @decodeDouble#@ is given w/ Integer-stuff (it's similar).
170 Decoding of floating-point numbers is sorta Integer-related. Encoding
171 is done with plain ccalls now (see PrelNumExtra.lhs).
173 A @Weak@ Pointer is created by the @mkWeak#@ primitive:
175 mkWeak# :: k -> v -> f -> State# RealWorld
176 -> (# State# RealWorld, Weak# v #)
178 In practice, you'll use the higher-level
180 data Weak v = Weak# v
181 mkWeak :: k -> v -> IO () -> IO (Weak v)
183 The following operation dereferences a weak pointer. The weak pointer
184 may have been finalized, so the operation returns a result code which
185 must be inspected before looking at the dereferenced value.
187 deRefWeak# :: Weak# v -> State# RealWorld ->
188 (# State# RealWorld, v, Int# #)
190 Only look at v if the Int# returned is /= 0 !!
192 The higher-level op is
194 deRefWeak :: Weak v -> IO (Maybe v)
196 Weak pointers can be finalized early by using the finalize# operation:
198 finalizeWeak# :: Weak# v -> State# RealWorld ->
199 (# State# RealWorld, Int#, IO () #)
201 The Int# returned is either
203 0 if the weak pointer has already been finalized, or it has no
204 finalizer (the third component is then invalid).
206 1 if the weak pointer is still alive, with the finalizer returned
207 as the third component.
209 A {\em stable name/pointer} is an index into a table of stable name
210 entries. Since the garbage collector is told about stable pointers,
211 it is safe to pass a stable pointer to external systems such as C
215 makeStablePtr# :: a -> State# RealWorld -> (# State# RealWorld, StablePtr# a #)
216 freeStablePtr :: StablePtr# a -> State# RealWorld -> State# RealWorld
217 deRefStablePtr# :: StablePtr# a -> State# RealWorld -> (# State# RealWorld, a #)
218 eqStablePtr# :: StablePtr# a -> StablePtr# a -> Int#
221 It may seem a bit surprising that @makeStablePtr#@ is a @IO@
222 operation since it doesn't (directly) involve IO operations. The
223 reason is that if some optimisation pass decided to duplicate calls to
224 @makeStablePtr#@ and we only pass one of the stable pointers over, a
225 massive space leak can result. Putting it into the IO monad
226 prevents this. (Another reason for putting them in a monad is to
227 ensure correct sequencing wrt the side-effecting @freeStablePtr@
230 An important property of stable pointers is that if you call
231 makeStablePtr# twice on the same object you get the same stable
234 Note that we can implement @freeStablePtr#@ using @_ccall_@ (and,
235 besides, it's not likely to be used from Haskell) so it's not a
238 Question: Why @RealWorld@ - won't any instance of @_ST@ do the job? [ADR]
243 A stable name is like a stable pointer, but with three important differences:
245 (a) You can't deRef one to get back to the original object.
246 (b) You can convert one to an Int.
247 (c) You don't need to 'freeStableName'
249 The existence of a stable name doesn't guarantee to keep the object it
250 points to alive (unlike a stable pointer), hence (a).
254 (a) makeStableName always returns the same value for a given
255 object (same as stable pointers).
257 (b) if two stable names are equal, it implies that the objects
258 from which they were created were the same.
260 (c) stableNameToInt always returns the same Int for a given
264 -- HWL: The first 4 Int# in all par... annotations denote:
265 -- name, granularity info, size of result, degree of parallelism
266 -- Same structure as _seq_ i.e. returns Int#
267 -- KSW: v, the second arg in parAt# and parAtForNow#, is used only to determine
268 -- `the processor containing the expression v'; it is not evaluated
270 These primops are pretty wierd.
272 dataToTag# :: a -> Int (arg must be an evaluated data type)
273 tagToEnum# :: Int -> a (result type must be an enumerated type)
275 The constraints aren't currently checked by the front end, but the
276 code generator will fall over if they aren't satisfied.
280 primOpInfo op = pprPanic "primOpInfo:" (ppr op)
284 %************************************************************************
286 \subsubsection[PrimOp-ool]{Which PrimOps are out-of-line}
288 %************************************************************************
290 Some PrimOps need to be called out-of-line because they either need to
291 perform a heap check or they block.
295 primOpOutOfLine :: PrimOp -> Bool
296 #include "primop-out-of-line.hs-incl"
300 primOpOkForSpeculation
301 ~~~~~~~~~~~~~~~~~~~~~~
302 Sometimes we may choose to execute a PrimOp even though it isn't
303 certain that its result will be required; ie execute them
304 ``speculatively''. The same thing as ``cheap eagerness.'' Usually
305 this is OK, because PrimOps are usually cheap, but it isn't OK for
306 (a)~expensive PrimOps and (b)~PrimOps which can fail.
308 PrimOps that have side effects also should not be executed speculatively.
310 Ok-for-speculation also means that it's ok *not* to execute the
314 Here the result is not used, so we can discard the primop. Anything
315 that has side effects mustn't be dicarded in this way, of course!
317 See also @primOpIsCheap@ (below).
321 primOpOkForSpeculation :: PrimOp -> Bool
322 -- See comments with CoreUtils.exprOkForSpeculation
323 primOpOkForSpeculation op
324 = not (primOpHasSideEffects op || primOpOutOfLine op || primOpCanFail op)
330 @primOpIsCheap@, as used in \tr{SimplUtils.lhs}. For now (HACK
331 WARNING), we just borrow some other predicates for a
332 what-should-be-good-enough test. "Cheap" means willing to call it more
333 than once, and/or push it inside a lambda. The latter could change the
334 behaviour of 'seq' for primops that can fail, so we don't treat them as cheap.
337 primOpIsCheap :: PrimOp -> Bool
338 primOpIsCheap op = primOpOkForSpeculation op
339 -- In March 2001, we changed this to
340 -- primOpIsCheap op = False
341 -- thereby making *no* primops seem cheap. But this killed eta
342 -- expansion on case (x ==# y) of True -> \s -> ...
343 -- which is bad. In particular a loop like
346 -- loop i | i == n = return ()
347 -- | otherwise = bar i >> loop (i+1)
348 -- allocated a closure every time round because it doesn't eta expand.
350 -- The problem that originally gave rise to the change was
351 -- let x = a +# b *# c in x +# x
352 -- were we don't want to inline x. But primopIsCheap doesn't control
353 -- that (it's exprIsDupable that does) so the problem doesn't occur
354 -- even if primOpIsCheap sometimes says 'True'.
359 primOpIsDupable means that the use of the primop is small enough to
360 duplicate into different case branches. See CoreUtils.exprIsDupable.
363 primOpIsDupable :: PrimOp -> Bool
364 -- See comments with CoreUtils.exprIsDupable
365 -- We say it's dupable it isn't implemented by a C call with a wrapper
366 primOpIsDupable op = not (primOpNeedsWrapper op)
371 primOpCanFail :: PrimOp -> Bool
372 #include "primop-can-fail.hs-incl"
375 And some primops have side-effects and so, for example, must not be
379 primOpHasSideEffects :: PrimOp -> Bool
380 #include "primop-has-side-effects.hs-incl"
383 Inline primitive operations that perform calls need wrappers to save
384 any live variables that are stored in caller-saves registers.
387 primOpNeedsWrapper :: PrimOp -> Bool
388 #include "primop-needs-wrapper.hs-incl"
392 primOpType :: PrimOp -> Type -- you may want to use primOpSig instead
394 = case (primOpInfo op) of
395 Dyadic occ ty -> dyadic_fun_ty ty
396 Monadic occ ty -> monadic_fun_ty ty
397 Compare occ ty -> compare_fun_ty ty
399 GenPrimOp occ tyvars arg_tys res_ty ->
400 mkForAllTys tyvars (mkFunTys arg_tys res_ty)
402 primOpOcc :: PrimOp -> OccName
403 primOpOcc op = case (primOpInfo op) of
407 GenPrimOp occ _ _ _ -> occ
409 -- primOpSig is like primOpType but gives the result split apart:
410 -- (type variables, argument types, result type)
411 -- It also gives arity, strictness info
413 primOpSig :: PrimOp -> ([TyVar], [Type], Type, Arity, StrictSig)
415 = (tyvars, arg_tys, res_ty, arity, primOpStrictness op arity)
417 arity = length arg_tys
418 (tyvars, arg_tys, res_ty)
419 = case (primOpInfo op) of
420 Monadic occ ty -> ([], [ty], ty )
421 Dyadic occ ty -> ([], [ty,ty], ty )
422 Compare occ ty -> ([], [ty,ty], boolTy)
423 GenPrimOp occ tyvars arg_tys res_ty
424 -> (tyvars, arg_tys, res_ty)
428 data PrimOpResultInfo
429 = ReturnsPrim PrimRep
432 -- Some PrimOps need not return a manifest primitive or algebraic value
433 -- (i.e. they might return a polymorphic value). These PrimOps *must*
434 -- be out of line, or the code generator won't work.
436 getPrimOpResultInfo :: PrimOp -> PrimOpResultInfo
437 getPrimOpResultInfo op
438 = case (primOpInfo op) of
439 Dyadic _ ty -> ReturnsPrim (typePrimRep ty)
440 Monadic _ ty -> ReturnsPrim (typePrimRep ty)
441 Compare _ ty -> ReturnsAlg boolTyCon
442 GenPrimOp _ _ _ ty | isPrimTyCon tc -> ReturnsPrim (tyConPrimRep tc)
443 | otherwise -> ReturnsAlg tc
445 tc = tyConAppTyCon ty
446 -- All primops return a tycon-app result
447 -- The tycon can be an unboxed tuple, though, which
448 -- gives rise to a ReturnAlg
451 The commutable ops are those for which we will try to move constants
452 to the right hand side for strength reduction.
455 commutableOp :: PrimOp -> Bool
456 #include "primop-commutable.hs-incl"
461 dyadic_fun_ty ty = mkFunTys [ty, ty] ty
462 monadic_fun_ty ty = mkFunTy ty ty
463 compare_fun_ty ty = mkFunTys [ty, ty] boolTy
468 pprPrimOp :: PrimOp -> SDoc
469 pprPrimOp other_op = pprOccName (primOpOcc other_op)