[project @ 2001-02-28 00:01:01 by qrczak]
[ghc-hetmet.git] / ghc / compiler / prelude / PrimOp.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[PrimOp]{Primitive operations (machine-level)}
5
6 \begin{code}
7 module PrimOp (
8         PrimOp(..), allThePrimOps,
9         primOpType, primOpSig, primOpUsg, primOpArity,
10         mkPrimOpIdName, primOpRdrName, primOpTag, primOpOcc,
11
12         commutableOp,
13
14         primOpOutOfLine, primOpNeedsWrapper, 
15         primOpOkForSpeculation, primOpIsCheap, primOpIsDupable,
16         primOpHasSideEffects,
17
18         getPrimOpResultInfo,  PrimOpResultInfo(..),
19
20         CCall(..), CCallTarget(..), ccallMayGC, ccallIsCasm, pprCCallOp,
21         isDynamicTarget, dynamicTarget, setCCallUnique
22     ) where
23
24 #include "HsVersions.h"
25
26 import PrimRep          -- most of it
27 import TysPrim
28 import TysWiredIn
29
30 import Demand           ( wwLazy, wwPrim, wwStrict, StrictnessInfo(..) )
31 import Var              ( TyVar )
32 import CallConv         ( CallConv, pprCallConv )
33 import Name             ( Name, mkWiredInName )
34 import RdrName          ( RdrName, mkRdrOrig )
35 import OccName          ( OccName, pprOccName, mkVarOcc )
36 import TyCon            ( TyCon, tyConArity )
37 import Type             ( Type, mkForAllTys, mkFunTy, mkFunTys, mkTyVarTys,
38                           mkTyConApp, typePrimRep,
39                           splitFunTy_maybe, splitAlgTyConApp_maybe, splitTyConApp,
40                           mkUTy, usOnce, usMany
41                         )
42 import Unique           ( Unique, mkPrimOpIdUnique )
43 import BasicTypes       ( Arity, Boxity(..) )
44 import CStrings         ( CLabelString, pprCLabelString )
45 import PrelNames        ( pREL_GHC, pREL_GHC_Name )
46 import Outputable
47 import Util             ( zipWithEqual )
48 import FastTypes
49 \end{code}
50
51 %************************************************************************
52 %*                                                                      *
53 \subsection[PrimOp-datatype]{Datatype for @PrimOp@ (an enumeration)}
54 %*                                                                      *
55 %************************************************************************
56
57 These are in \tr{state-interface.verb} order.
58
59 \begin{code}
60
61 -- supplies: 
62 -- data PrimOp = ...
63 #include "primop-data-decl.hs-incl"
64     | CCallOp CCall          -- and don't forget to add CCall
65 \end{code}
66
67 Used for the Ord instance
68
69 \begin{code}
70 primOpTag :: PrimOp -> Int
71 primOpTag op = iBox (tagOf_PrimOp op)
72
73 -- supplies   
74 -- tagOf_PrimOp :: PrimOp -> FastInt
75 #include "primop-tag.hs-incl"
76 tagOf_PrimOp op = pprPanic# "tagOf_PrimOp: pattern-match" (ppr op)
77
78
79 instance Eq PrimOp where
80     op1 == op2 = tagOf_PrimOp op1 ==# tagOf_PrimOp op2
81
82 instance Ord PrimOp where
83     op1 <  op2 =  tagOf_PrimOp op1 <# tagOf_PrimOp op2
84     op1 <= op2 =  tagOf_PrimOp op1 <=# tagOf_PrimOp op2
85     op1 >= op2 =  tagOf_PrimOp op1 >=# tagOf_PrimOp op2
86     op1 >  op2 =  tagOf_PrimOp op1 ># tagOf_PrimOp op2
87     op1 `compare` op2 | op1 < op2  = LT
88                       | op1 == op2 = EQ
89                       | otherwise  = GT
90
91 instance Outputable PrimOp where
92     ppr op = pprPrimOp op
93
94 instance Show PrimOp where
95     showsPrec p op = showsPrecSDoc p (pprPrimOp op)
96 \end{code}
97
98 An @Enum@-derived list would be better; meanwhile... (ToDo)
99 \begin{code}
100 allThePrimOps :: [PrimOp]
101 allThePrimOps =
102 #include "primop-list.hs-incl"
103 -- Doesn't include CCall, which is really a family of primops
104 \end{code}
105
106 %************************************************************************
107 %*                                                                      *
108 \subsection[PrimOp-info]{The essential info about each @PrimOp@}
109 %*                                                                      *
110 %************************************************************************
111
112 The @String@ in the @PrimOpInfos@ is the ``base name'' by which the user may
113 refer to the primitive operation.  The conventional \tr{#}-for-
114 unboxed ops is added on later.
115
116 The reason for the funny characters in the names is so we do not
117 interfere with the programmer's Haskell name spaces.
118
119 We use @PrimKinds@ for the ``type'' information, because they're
120 (slightly) more convenient to use than @TyCons@.
121 \begin{code}
122 data PrimOpInfo
123   = Dyadic      OccName         -- string :: T -> T -> T
124                 Type
125   | Monadic     OccName         -- string :: T -> T
126                 Type
127   | Compare     OccName         -- string :: T -> T -> Bool
128                 Type
129
130   | GenPrimOp   OccName         -- string :: \/a1..an . T1 -> .. -> Tk -> T
131                 [TyVar] 
132                 [Type] 
133                 Type 
134
135 mkDyadic str  ty = Dyadic  (mkVarOcc str) ty
136 mkMonadic str ty = Monadic (mkVarOcc str) ty
137 mkCompare str ty = Compare (mkVarOcc str) ty
138 mkGenPrimOp str tvs tys ty = GenPrimOp (mkVarOcc str) tvs tys ty
139 \end{code}
140
141 %************************************************************************
142 %*                                                                      *
143 \subsubsection{Strictness}
144 %*                                                                      *
145 %************************************************************************
146
147 Not all primops are strict!
148
149 \begin{code}
150 primOpStrictness :: PrimOp -> Arity -> StrictnessInfo
151         -- See Demand.StrictnessInfo for discussion of what the results
152         -- The arity should be the arity of the primop; that's why
153         -- this function isn't exported.
154 #include "primop-strictness.hs-incl"
155 \end{code}
156
157 %************************************************************************
158 %*                                                                      *
159 \subsubsection[PrimOp-comparison]{PrimOpInfo basic comparison ops}
160 %*                                                                      *
161 %************************************************************************
162
163 @primOpInfo@ gives all essential information (from which everything
164 else, notably a type, can be constructed) for each @PrimOp@.
165
166 \begin{code}
167 primOpInfo :: PrimOp -> PrimOpInfo
168 #include "primop-primop-info.hs-incl"
169 \end{code}
170
171 Here are a load of comments from the old primOp info:
172
173 A @Word#@ is an unsigned @Int#@.
174
175 @decodeFloat#@ is given w/ Integer-stuff (it's similar).
176
177 @decodeDouble#@ is given w/ Integer-stuff (it's similar).
178
179 Decoding of floating-point numbers is sorta Integer-related.  Encoding
180 is done with plain ccalls now (see PrelNumExtra.lhs).
181
182 A @Weak@ Pointer is created by the @mkWeak#@ primitive:
183
184         mkWeak# :: k -> v -> f -> State# RealWorld 
185                         -> (# State# RealWorld, Weak# v #)
186
187 In practice, you'll use the higher-level
188
189         data Weak v = Weak# v
190         mkWeak :: k -> v -> IO () -> IO (Weak v)
191
192 The following operation dereferences a weak pointer.  The weak pointer
193 may have been finalized, so the operation returns a result code which
194 must be inspected before looking at the dereferenced value.
195
196         deRefWeak# :: Weak# v -> State# RealWorld ->
197                         (# State# RealWorld, v, Int# #)
198
199 Only look at v if the Int# returned is /= 0 !!
200
201 The higher-level op is
202
203         deRefWeak :: Weak v -> IO (Maybe v)
204
205 Weak pointers can be finalized early by using the finalize# operation:
206         
207         finalizeWeak# :: Weak# v -> State# RealWorld -> 
208                            (# State# RealWorld, Int#, IO () #)
209
210 The Int# returned is either
211
212         0 if the weak pointer has already been finalized, or it has no
213           finalizer (the third component is then invalid).
214
215         1 if the weak pointer is still alive, with the finalizer returned
216           as the third component.
217
218 A {\em stable name/pointer} is an index into a table of stable name
219 entries.  Since the garbage collector is told about stable pointers,
220 it is safe to pass a stable pointer to external systems such as C
221 routines.
222
223 \begin{verbatim}
224 makeStablePtr#  :: a -> State# RealWorld -> (# State# RealWorld, StablePtr# a #)
225 freeStablePtr   :: StablePtr# a -> State# RealWorld -> State# RealWorld
226 deRefStablePtr# :: StablePtr# a -> State# RealWorld -> (# State# RealWorld, a #)
227 eqStablePtr#    :: StablePtr# a -> StablePtr# a -> Int#
228 \end{verbatim}
229
230 It may seem a bit surprising that @makeStablePtr#@ is a @IO@
231 operation since it doesn't (directly) involve IO operations.  The
232 reason is that if some optimisation pass decided to duplicate calls to
233 @makeStablePtr#@ and we only pass one of the stable pointers over, a
234 massive space leak can result.  Putting it into the IO monad
235 prevents this.  (Another reason for putting them in a monad is to
236 ensure correct sequencing wrt the side-effecting @freeStablePtr@
237 operation.)
238
239 An important property of stable pointers is that if you call
240 makeStablePtr# twice on the same object you get the same stable
241 pointer back.
242
243 Note that we can implement @freeStablePtr#@ using @_ccall_@ (and,
244 besides, it's not likely to be used from Haskell) so it's not a
245 primop.
246
247 Question: Why @RealWorld@ - won't any instance of @_ST@ do the job? [ADR]
248
249 Stable Names
250 ~~~~~~~~~~~~
251
252 A stable name is like a stable pointer, but with three important differences:
253
254         (a) You can't deRef one to get back to the original object.
255         (b) You can convert one to an Int.
256         (c) You don't need to 'freeStableName'
257
258 The existence of a stable name doesn't guarantee to keep the object it
259 points to alive (unlike a stable pointer), hence (a).
260
261 Invariants:
262         
263         (a) makeStableName always returns the same value for a given
264             object (same as stable pointers).
265
266         (b) if two stable names are equal, it implies that the objects
267             from which they were created were the same.
268
269         (c) stableNameToInt always returns the same Int for a given
270             stable name.
271
272
273 [Alastair Reid is to blame for this!]
274
275 These days, (Glasgow) Haskell seems to have a bit of everything from
276 other languages: strict operations, mutable variables, sequencing,
277 pointers, etc.  About the only thing left is LISP's ability to test
278 for pointer equality.  So, let's add it in!
279
280 \begin{verbatim}
281 reallyUnsafePtrEquality :: a -> a -> Int#
282 \end{verbatim}
283
284 which tests any two closures (of the same type) to see if they're the
285 same.  (Returns $0$ for @False@, $\neq 0$ for @True@ - to avoid
286 difficulties of trying to box up the result.)
287
288 NB This is {\em really unsafe\/} because even something as trivial as
289 a garbage collection might change the answer by removing indirections.
290 Still, no-one's forcing you to use it.  If you're worried about little
291 things like loss of referential transparency, you might like to wrap
292 it all up in a monad-like thing as John O'Donnell and John Hughes did
293 for non-determinism (1989 (Fraserburgh) Glasgow FP Workshop
294 Proceedings?)
295
296 I'm thinking of using it to speed up a critical equality test in some
297 graphics stuff in a context where the possibility of saying that
298 denotationally equal things aren't isn't a problem (as long as it
299 doesn't happen too often.)  ADR
300
301 To Will: Jim said this was already in, but I can't see it so I'm
302 adding it.  Up to you whether you add it.  (Note that this could have
303 been readily implemented using a @veryDangerousCCall@ before they were
304 removed...)
305
306
307 -- HWL: The first 4 Int# in all par... annotations denote:
308 --   name, granularity info, size of result, degree of parallelism
309 --      Same  structure as _seq_ i.e. returns Int#
310 -- KSW: v, the second arg in parAt# and parAtForNow#, is used only to determine
311 --   `the processor containing the expression v'; it is not evaluated
312
313 These primops are pretty wierd.
314
315         dataToTag# :: a -> Int    (arg must be an evaluated data type)
316         tagToEnum# :: Int -> a    (result type must be an enumerated type)
317
318 The constraints aren't currently checked by the front end, but the
319 code generator will fall over if they aren't satisfied.
320
321 \begin{code}
322 #ifdef DEBUG
323 primOpInfo op = pprPanic "primOpInfo:" (ppr op)
324 #endif
325 \end{code}
326
327 %************************************************************************
328 %*                                                                      *
329 \subsubsection[PrimOp-ool]{Which PrimOps are out-of-line}
330 %*                                                                      *
331 %************************************************************************
332
333 Some PrimOps need to be called out-of-line because they either need to
334 perform a heap check or they block.
335
336 \begin{code}
337 primOpOutOfLine (CCallOp c_call) = ccallMayGC c_call
338 #include "primop-out-of-line.hs-incl"
339 \end{code}
340
341
342 primOpOkForSpeculation
343 ~~~~~~~~~~~~~~~~~~~~~~
344 Sometimes we may choose to execute a PrimOp even though it isn't
345 certain that its result will be required; ie execute them
346 ``speculatively''.  The same thing as ``cheap eagerness.'' Usually
347 this is OK, because PrimOps are usually cheap, but it isn't OK for
348 (a)~expensive PrimOps and (b)~PrimOps which can fail.
349
350 PrimOps that have side effects also should not be executed speculatively.
351
352 Ok-for-speculation also means that it's ok *not* to execute the
353 primop. For example
354         case op a b of
355           r -> 3
356 Here the result is not used, so we can discard the primop.  Anything
357 that has side effects mustn't be dicarded in this way, of course!
358
359 See also @primOpIsCheap@ (below).
360
361
362 \begin{code}
363 primOpOkForSpeculation :: PrimOp -> Bool
364         -- See comments with CoreUtils.exprOkForSpeculation
365 primOpOkForSpeculation op 
366   = primOpIsCheap op && not (primOpCanFail op)
367 \end{code}
368
369
370 primOpIsCheap
371 ~~~~~~~~~~~~~
372 @primOpIsCheap@, as used in \tr{SimplUtils.lhs}.  For now (HACK
373 WARNING), we just borrow some other predicates for a
374 what-should-be-good-enough test.  "Cheap" means willing to call it more
375 than once.  Evaluation order is unaffected.
376
377 \begin{code}
378 primOpIsCheap :: PrimOp -> Bool
379         -- See comments with CoreUtils.exprOkForSpeculation
380 primOpIsCheap op = not (primOpHasSideEffects op || primOpOutOfLine op)
381 \end{code}
382
383 primOpIsDupable
384 ~~~~~~~~~~~~~~~
385 primOpIsDupable means that the use of the primop is small enough to
386 duplicate into different case branches.  See CoreUtils.exprIsDupable.
387
388 \begin{code}
389 primOpIsDupable :: PrimOp -> Bool
390         -- See comments with CoreUtils.exprIsDupable
391         -- We say it's dupable it isn't implemented by a C call with a wrapper
392 primOpIsDupable op = not (primOpNeedsWrapper op)
393 \end{code}
394
395
396 \begin{code}
397 primOpCanFail :: PrimOp -> Bool
398 #include "primop-can-fail.hs-incl"
399 \end{code}
400
401 And some primops have side-effects and so, for example, must not be
402 duplicated.
403
404 \begin{code}
405 primOpHasSideEffects :: PrimOp -> Bool
406 primOpHasSideEffects (CCallOp _)        = True
407 #include "primop-has-side-effects.hs-incl"
408 \end{code}
409
410 Inline primitive operations that perform calls need wrappers to save
411 any live variables that are stored in caller-saves registers.
412
413 \begin{code}
414 primOpNeedsWrapper :: PrimOp -> Bool
415 primOpNeedsWrapper (CCallOp _)          = True
416 #include "primop-needs-wrapper.hs-incl"
417 \end{code}
418
419 \begin{code}
420 primOpArity :: PrimOp -> Arity
421 primOpArity op 
422   = case (primOpInfo op) of
423       Monadic occ ty                      -> 1
424       Dyadic occ ty                       -> 2
425       Compare occ ty                      -> 2
426       GenPrimOp occ tyvars arg_tys res_ty -> length arg_tys
427                 
428 primOpType :: PrimOp -> Type  -- you may want to use primOpSig instead
429 primOpType op
430   = case (primOpInfo op) of
431       Dyadic occ ty ->      dyadic_fun_ty ty
432       Monadic occ ty ->     monadic_fun_ty ty
433       Compare occ ty ->     compare_fun_ty ty
434
435       GenPrimOp occ tyvars arg_tys res_ty -> 
436         mkForAllTys tyvars (mkFunTys arg_tys res_ty)
437
438 mkPrimOpIdName :: PrimOp -> Name
439         -- Make the name for the PrimOp's Id
440         -- We have to pass in the Id itself because it's a WiredInId
441         -- and hence recursive
442 mkPrimOpIdName op
443   = mkWiredInName pREL_GHC (primOpOcc op) (mkPrimOpIdUnique (primOpTag op))
444
445 primOpRdrName :: PrimOp -> RdrName 
446 primOpRdrName op = mkRdrOrig pREL_GHC_Name (primOpOcc op)
447
448 primOpOcc :: PrimOp -> OccName
449 primOpOcc op = case (primOpInfo op) of
450                               Dyadic    occ _     -> occ
451                               Monadic   occ _     -> occ
452                               Compare   occ _     -> occ
453                               GenPrimOp occ _ _ _ -> occ
454
455 -- primOpSig is like primOpType but gives the result split apart:
456 -- (type variables, argument types, result type)
457 -- It also gives arity, strictness info
458
459 primOpSig :: PrimOp -> ([TyVar], [Type], Type, Arity, StrictnessInfo)
460 primOpSig op
461   = (tyvars, arg_tys, res_ty, arity, primOpStrictness op arity)
462   where
463     arity = length arg_tys
464     (tyvars, arg_tys, res_ty)
465       = case (primOpInfo op) of
466           Monadic   occ ty -> ([],     [ty],    ty    )
467           Dyadic    occ ty -> ([],     [ty,ty], ty    )
468           Compare   occ ty -> ([],     [ty,ty], boolTy)
469           GenPrimOp occ tyvars arg_tys res_ty
470                            -> (tyvars, arg_tys, res_ty)
471
472 -- primOpUsg is like primOpSig but the types it yields are the
473 -- appropriate sigma (i.e., usage-annotated) types,
474 -- as required by the UsageSP inference.
475
476 primOpUsg :: PrimOp -> ([TyVar],[Type],Type)
477 primOpUsg p@(CCallOp _) = mangle p [] mkM
478 #include "primop-usage.hs-incl"
479
480 -- Things with no Haskell pointers inside: in actuality, usages are
481 -- irrelevant here (hence it doesn't matter that some of these
482 -- apparently permit duplication; since such arguments are never 
483 -- ENTERed anyway, the usage annotation they get is entirely irrelevant
484 -- except insofar as it propagates to infect other values that *are*
485 -- pointed.
486
487
488 -- Helper bits & pieces for usage info.
489                                     
490 mkZ          = mkUTy usOnce  -- pointed argument used zero
491 mkO          = mkUTy usOnce  -- pointed argument used once
492 mkM          = mkUTy usMany  -- pointed argument used multiply
493 mkP          = mkUTy usOnce  -- unpointed argument
494 mkR          = mkUTy usMany  -- unpointed result
495
496 nomangle op
497    = case primOpSig op of
498         (tyvars, arg_tys, res_ty, _, _)
499            -> (tyvars, map mkP arg_tys, mkR res_ty)
500
501 mangle op fs g  
502    = case primOpSig op of
503         (tyvars, arg_tys, res_ty, _, _)
504            -> (tyvars, zipWithEqual "primOpUsg" ($) fs arg_tys, g res_ty)
505
506 inFun op f g ty 
507    = case splitFunTy_maybe ty of
508         Just (a,b) -> mkFunTy (f a) (g b)
509         Nothing    -> pprPanic "primOpUsg:inFun" (ppr op <+> ppr ty)
510
511 inUB op fs ty
512    = case splitTyConApp ty of
513         (tc,tys) -> ASSERT( tc == tupleTyCon Unboxed (length fs) )
514                     mkTupleTy Unboxed (length fs) (zipWithEqual "primOpUsg" ($) fs tys)
515 \end{code}
516
517 \begin{code}
518 data PrimOpResultInfo
519   = ReturnsPrim     PrimRep
520   | ReturnsAlg      TyCon
521
522 -- Some PrimOps need not return a manifest primitive or algebraic value
523 -- (i.e. they might return a polymorphic value).  These PrimOps *must*
524 -- be out of line, or the code generator won't work.
525
526 getPrimOpResultInfo :: PrimOp -> PrimOpResultInfo
527 getPrimOpResultInfo (CCallOp _)
528   = ReturnsAlg unboxedPairTyCon
529 getPrimOpResultInfo op
530   = case (primOpInfo op) of
531       Dyadic  _ ty               -> ReturnsPrim (typePrimRep ty)
532       Monadic _ ty               -> ReturnsPrim (typePrimRep ty)
533       Compare _ ty               -> ReturnsAlg boolTyCon
534       GenPrimOp _ _ _ ty         -> 
535         let rep = typePrimRep ty in
536         case rep of
537            PtrRep -> case splitAlgTyConApp_maybe ty of
538                         Nothing -> pprPanic "getPrimOpResultInfo" 
539                                             (ppr ty <+> ppr op)
540                         Just (tc,_,_) -> ReturnsAlg tc
541            other -> ReturnsPrim other
542 \end{code}
543
544 The commutable ops are those for which we will try to move constants
545 to the right hand side for strength reduction.
546
547 \begin{code}
548 commutableOp :: PrimOp -> Bool
549 #include "primop-commutable.hs-incl"
550 \end{code}
551
552 Utils:
553 \begin{code}
554 mkPrimTyApp :: [TyVar] -> PrimRep -> ([TyVar], Type)
555         -- CharRep       -->  ([],  Char#)
556         -- StablePtrRep  -->  ([a], StablePtr# a)
557 mkPrimTyApp tvs kind
558   = (forall_tvs, mkTyConApp tycon (mkTyVarTys forall_tvs))
559   where
560     tycon      = primRepTyCon kind
561     forall_tvs = take (tyConArity tycon) tvs
562
563 dyadic_fun_ty  ty = mkFunTys [ty, ty] ty
564 monadic_fun_ty ty = mkFunTy  ty ty
565 compare_fun_ty ty = mkFunTys [ty, ty] boolTy
566 \end{code}
567
568 Output stuff:
569 \begin{code}
570 pprPrimOp  :: PrimOp -> SDoc
571
572 pprPrimOp (CCallOp c_call) = pprCCallOp c_call
573 pprPrimOp other_op
574   = getPprStyle $ \ sty ->
575     if ifaceStyle sty then      -- For interfaces Print it qualified with PrelGHC.
576         ptext SLIT("PrelGHC.") <> pprOccName occ
577     else
578         pprOccName occ
579   where
580     occ = primOpOcc other_op
581 \end{code}
582
583
584 %************************************************************************
585 %*                                                                      *
586 \subsubsection{CCalls}
587 %*                                                                      *
588 %************************************************************************
589
590 A special ``trap-door'' to use in making calls direct to C functions:
591 \begin{code}
592 data CCall
593   =  CCall      CCallTarget
594                 Bool            -- True <=> really a "casm"
595                 Bool            -- True <=> might invoke Haskell GC
596                 CallConv        -- calling convention to use.
597   deriving( Eq )
598
599 data CCallTarget
600   = StaticTarget  CLabelString  -- An "unboxed" ccall# to `fn'.
601   | DynamicTarget Unique        -- First argument (an Addr#) is the function pointer
602                                 --   (unique is used to generate a 'typedef' to cast
603                                 --    the function pointer if compiling the ccall# down to
604                                 --    .hc code - can't do this inline for tedious reasons.)
605
606 instance Eq CCallTarget where
607   (StaticTarget l1) == (StaticTarget l2) = l1 == l2
608   (DynamicTarget _) == (DynamicTarget _) = True 
609         -- Ignore the arbitrary unique; this is important when comparing
610         -- a dynamic ccall read from an interface file A.hi with the
611         -- one constructed from A.hs, when deciding whether the interface
612         -- has changed
613   t1 == t2 = False
614
615 ccallMayGC :: CCall -> Bool
616 ccallMayGC (CCall _ _ may_gc _) = may_gc
617
618 ccallIsCasm :: CCall -> Bool
619 ccallIsCasm (CCall _ c_asm _ _) = c_asm
620
621 isDynamicTarget (DynamicTarget _) = True
622 isDynamicTarget (StaticTarget _)  = False
623
624 dynamicTarget :: CCallTarget
625 dynamicTarget = DynamicTarget (panic "Unique in DynamicTarget not yet set")
626         -- The unique is really only to do with code generation, so it
627         -- is only set in CoreToStg; before then it's just an error message
628
629 setCCallUnique :: CCall -> Unique -> CCall
630 setCCallUnique (CCall (DynamicTarget _) is_asm may_gc cconv) uniq
631   = CCall (DynamicTarget uniq) is_asm may_gc cconv
632 setCCallUnique ccall uniq = ccall
633 \end{code}
634
635 \begin{code}
636 pprCCallOp (CCall fun is_casm may_gc cconv)
637   = hcat [ ifPprDebug callconv
638          , text "__", ppr_dyn
639          , text before , ppr_fun , after]
640   where
641         callconv = text "{-" <> pprCallConv cconv <> text "-}"
642
643         before
644           | is_casm && may_gc = "casm_GC ``"
645           | is_casm           = "casm ``"
646           | may_gc            = "ccall_GC "
647           | otherwise         = "ccall "
648
649         after
650           | is_casm   = text "''"
651           | otherwise = empty
652           
653         ppr_dyn = case fun of
654                     DynamicTarget _ -> text "dyn_"
655                     _               -> empty
656
657         ppr_fun = case fun of
658                      DynamicTarget _ -> text "\"\""
659                      StaticTarget fn -> pprCLabelString fn
660 \end{code}