2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[PrimOp]{Primitive operations (machine-level)}
8 PrimOp(..), allThePrimOps,
9 tagOf_PrimOp, -- ToDo: rm
10 primOpType, primOpSig, primOpUsg,
11 primOpUniq, primOpOcc,
15 primOpOutOfLine, primOpNeedsWrapper, primOpStrictness,
16 primOpOkForSpeculation, primOpIsCheap,
19 getPrimOpResultInfo, PrimOpResultInfo(..),
24 #include "HsVersions.h"
26 import PrimRep -- most of it
30 import Demand ( Demand, wwLazy, wwPrim, wwStrict )
32 import CallConv ( CallConv, pprCallConv )
33 import PprType ( pprParendType )
34 import OccName ( OccName, pprOccName, mkSrcVarOcc )
35 import TyCon ( TyCon, tyConArity )
36 import Type ( Type, mkForAllTys, mkForAllTy, mkFunTy, mkFunTys, mkTyVarTys,
37 mkTyConTy, mkTyConApp, typePrimRep,
38 splitFunTy_maybe, splitAlgTyConApp_maybe, splitTyConApp_maybe,
41 import Unique ( Unique, mkPrimOpIdUnique )
43 import Util ( assoc, zipWithEqual )
44 import GlaExts ( Int(..), Int#, (==#) )
47 %************************************************************************
49 \subsection[PrimOp-datatype]{Datatype for @PrimOp@ (an enumeration)}
51 %************************************************************************
53 These are in \tr{state-interface.verb} order.
57 -- dig the FORTRAN/C influence on the names...
61 = CharGtOp | CharGeOp | CharEqOp | CharNeOp | CharLtOp | CharLeOp
62 | IntGtOp | IntGeOp | IntEqOp | IntNeOp | IntLtOp | IntLeOp
63 | WordGtOp | WordGeOp | WordEqOp | WordNeOp | WordLtOp | WordLeOp
64 | AddrGtOp | AddrGeOp | AddrEqOp | AddrNeOp | AddrLtOp | AddrLeOp
65 | FloatGtOp | FloatGeOp | FloatEqOp | FloatNeOp | FloatLtOp | FloatLeOp
66 | DoubleGtOp | DoubleGeOp | DoubleEqOp | DoubleNeOp | DoubleLtOp | DoubleLeOp
72 -- IntAbsOp unused?? ADR
73 | IntAddOp | IntSubOp | IntMulOp | IntQuotOp
74 | IntRemOp | IntNegOp | IntAbsOp
75 | ISllOp | ISraOp | ISrlOp -- shift {left,right} {arithmetic,logical}
81 | WordQuotOp | WordRemOp
82 | AndOp | OrOp | NotOp | XorOp
83 | SllOp | SrlOp -- shift {left,right} {logical}
84 | Int2WordOp | Word2IntOp -- casts
87 | Int2AddrOp | Addr2IntOp -- casts
89 -- Float#-related ops:
90 | FloatAddOp | FloatSubOp | FloatMulOp | FloatDivOp | FloatNegOp
91 | Float2IntOp | Int2FloatOp
93 | FloatExpOp | FloatLogOp | FloatSqrtOp
94 | FloatSinOp | FloatCosOp | FloatTanOp
95 | FloatAsinOp | FloatAcosOp | FloatAtanOp
96 | FloatSinhOp | FloatCoshOp | FloatTanhOp
97 -- not all machines have these available conveniently:
98 -- | FloatAsinhOp | FloatAcoshOp | FloatAtanhOp
99 | FloatPowerOp -- ** op
101 -- Double#-related ops:
102 | DoubleAddOp | DoubleSubOp | DoubleMulOp | DoubleDivOp | DoubleNegOp
103 | Double2IntOp | Int2DoubleOp
104 | Double2FloatOp | Float2DoubleOp
106 | DoubleExpOp | DoubleLogOp | DoubleSqrtOp
107 | DoubleSinOp | DoubleCosOp | DoubleTanOp
108 | DoubleAsinOp | DoubleAcosOp | DoubleAtanOp
109 | DoubleSinhOp | DoubleCoshOp | DoubleTanhOp
110 -- not all machines have these available conveniently:
111 -- | DoubleAsinhOp | DoubleAcoshOp | DoubleAtanhOp
112 | DoublePowerOp -- ** op
114 -- Integer (and related...) ops:
115 -- slightly weird -- to match GMP package.
116 | IntegerAddOp | IntegerSubOp | IntegerMulOp | IntegerGcdOp
117 | IntegerQuotRemOp | IntegerDivModOp | IntegerNegOp
122 | Integer2IntOp | Integer2WordOp
123 | Int2IntegerOp | Word2IntegerOp
125 -- casting to/from Integer and 64-bit (un)signed quantities.
126 | IntegerToInt64Op | Int64ToIntegerOp
127 | IntegerToWord64Op | Word64ToIntegerOp
133 -- primitive ops for primitive arrays
136 | NewByteArrayOp PrimRep
139 | SameMutableByteArrayOp
141 | ReadArrayOp | WriteArrayOp | IndexArrayOp -- for arrays of Haskell ptrs
143 | ReadByteArrayOp PrimRep
144 | WriteByteArrayOp PrimRep
145 | IndexByteArrayOp PrimRep
146 | IndexOffAddrOp PrimRep
147 | WriteOffAddrOp PrimRep
148 -- PrimRep can be one of {Char,Int,Addr,Float,Double}Kind.
149 -- This is just a cheesy encoding of a bunch of ops.
150 -- Note that ForeignObjRep is not included -- the only way of
151 -- creating a ForeignObj is with a ccall or casm.
152 | IndexOffForeignObjOp PrimRep
154 | UnsafeFreezeArrayOp | UnsafeFreezeByteArrayOp
155 | UnsafeThawArrayOp | UnsafeThawByteArrayOp
156 | SizeofByteArrayOp | SizeofMutableByteArrayOp
195 A special ``trap-door'' to use in making calls direct to C functions:
198 FAST_STRING -- Left fn => An "unboxed" ccall# to `fn'.
199 Unique) -- Right u => first argument (an Addr#) is the function pointer
200 -- (unique is used to generate a 'typedef' to cast
201 -- the function pointer if compiling the ccall# down to
202 -- .hc code - can't do this inline for tedious reasons.)
204 Bool -- True <=> really a "casm"
205 Bool -- True <=> might invoke Haskell GC
206 CallConv -- calling convention to use.
208 -- (... to be continued ... )
211 The ``type'' of @CCallOp foo [t1, ... tm] r@ is @t1 -> ... tm -> r@.
212 (See @primOpInfo@ for details.)
214 Note: that first arg and part of the result should be the system state
215 token (which we carry around to fool over-zealous optimisers) but
216 which isn't actually passed.
218 For example, we represent
220 ((ccall# foo [StablePtr# a, Int] Float) sp# i#) :: (Float, IoWorld)
226 (CCallOp "foo" [Universe#, StablePtr# a, Int#] FloatPrimAndUniverse False)
227 -- :: Universe# -> StablePtr# a -> Int# -> FloatPrimAndUniverse
231 (AlgAlts [ ( FloatPrimAndIoWorld,
233 Con (TupleCon 2) [Float, IoWorld] [F# f#, World w#]
239 Nota Bene: there are some people who find the empty list of types in
240 the @Prim@ somewhat puzzling and would represent the above by
244 (CCallOp "foo" [alpha1, alpha2, alpha3] alpha4 False)
245 -- :: /\ alpha1, alpha2 alpha3, alpha4.
246 -- alpha1 -> alpha2 -> alpha3 -> alpha4
247 [Universe#, StablePtr# a, Int#, FloatPrimAndIoWorld]
250 (AlgAlts [ ( FloatPrimAndIoWorld,
252 Con (TupleCon 2) [Float, IoWorld] [F# f#, World w#]
258 But, this is a completely different way of using @CCallOp@. The most
259 major changes required if we switch to this are in @primOpInfo@, and
260 the desugarer. The major difficulty is in moving the HeapRequirement
261 stuff somewhere appropriate. (The advantage is that we could simplify
262 @CCallOp@ and record just the number of arguments with corresponding
263 simplifications in reading pragma unfoldings, the simplifier,
264 instantiation (etc) of core expressions, ... . Maybe we should think
265 about using it this way?? ADR)
268 -- (... continued from above ... )
270 -- Operation to test two closure addresses for equality (yes really!)
271 -- BLAME ALASTAIR REID FOR THIS! THE REST OF US ARE INNOCENT!
272 | ReallyUnsafePtrEqualityOp
287 -- more parallel stuff
288 | ParGlobalOp -- named global par
289 | ParLocalOp -- named local par
290 | ParAtOp -- specifies destination of local par
291 | ParAtAbsOp -- specifies destination of local par (abs processor)
292 | ParAtRelOp -- specifies destination of local par (rel processor)
293 | ParAtForNowOp -- specifies initial destination of global par
294 | CopyableOp -- marks copyable code
295 | NoFollowOp -- marks non-followup expression
302 Used for the Ord instance
305 tagOf_PrimOp CharGtOp = (ILIT( 1) :: FAST_INT)
306 tagOf_PrimOp CharGeOp = ILIT( 2)
307 tagOf_PrimOp CharEqOp = ILIT( 3)
308 tagOf_PrimOp CharNeOp = ILIT( 4)
309 tagOf_PrimOp CharLtOp = ILIT( 5)
310 tagOf_PrimOp CharLeOp = ILIT( 6)
311 tagOf_PrimOp IntGtOp = ILIT( 7)
312 tagOf_PrimOp IntGeOp = ILIT( 8)
313 tagOf_PrimOp IntEqOp = ILIT( 9)
314 tagOf_PrimOp IntNeOp = ILIT( 10)
315 tagOf_PrimOp IntLtOp = ILIT( 11)
316 tagOf_PrimOp IntLeOp = ILIT( 12)
317 tagOf_PrimOp WordGtOp = ILIT( 13)
318 tagOf_PrimOp WordGeOp = ILIT( 14)
319 tagOf_PrimOp WordEqOp = ILIT( 15)
320 tagOf_PrimOp WordNeOp = ILIT( 16)
321 tagOf_PrimOp WordLtOp = ILIT( 17)
322 tagOf_PrimOp WordLeOp = ILIT( 18)
323 tagOf_PrimOp AddrGtOp = ILIT( 19)
324 tagOf_PrimOp AddrGeOp = ILIT( 20)
325 tagOf_PrimOp AddrEqOp = ILIT( 21)
326 tagOf_PrimOp AddrNeOp = ILIT( 22)
327 tagOf_PrimOp AddrLtOp = ILIT( 23)
328 tagOf_PrimOp AddrLeOp = ILIT( 24)
329 tagOf_PrimOp FloatGtOp = ILIT( 25)
330 tagOf_PrimOp FloatGeOp = ILIT( 26)
331 tagOf_PrimOp FloatEqOp = ILIT( 27)
332 tagOf_PrimOp FloatNeOp = ILIT( 28)
333 tagOf_PrimOp FloatLtOp = ILIT( 29)
334 tagOf_PrimOp FloatLeOp = ILIT( 30)
335 tagOf_PrimOp DoubleGtOp = ILIT( 31)
336 tagOf_PrimOp DoubleGeOp = ILIT( 32)
337 tagOf_PrimOp DoubleEqOp = ILIT( 33)
338 tagOf_PrimOp DoubleNeOp = ILIT( 34)
339 tagOf_PrimOp DoubleLtOp = ILIT( 35)
340 tagOf_PrimOp DoubleLeOp = ILIT( 36)
341 tagOf_PrimOp OrdOp = ILIT( 37)
342 tagOf_PrimOp ChrOp = ILIT( 38)
343 tagOf_PrimOp IntAddOp = ILIT( 39)
344 tagOf_PrimOp IntSubOp = ILIT( 40)
345 tagOf_PrimOp IntMulOp = ILIT( 41)
346 tagOf_PrimOp IntQuotOp = ILIT( 42)
347 tagOf_PrimOp IntRemOp = ILIT( 43)
348 tagOf_PrimOp IntNegOp = ILIT( 44)
349 tagOf_PrimOp IntAbsOp = ILIT( 45)
350 tagOf_PrimOp WordQuotOp = ILIT( 46)
351 tagOf_PrimOp WordRemOp = ILIT( 47)
352 tagOf_PrimOp AndOp = ILIT( 48)
353 tagOf_PrimOp OrOp = ILIT( 49)
354 tagOf_PrimOp NotOp = ILIT( 50)
355 tagOf_PrimOp XorOp = ILIT( 51)
356 tagOf_PrimOp SllOp = ILIT( 52)
357 tagOf_PrimOp SrlOp = ILIT( 53)
358 tagOf_PrimOp ISllOp = ILIT( 54)
359 tagOf_PrimOp ISraOp = ILIT( 55)
360 tagOf_PrimOp ISrlOp = ILIT( 56)
361 tagOf_PrimOp IntAddCOp = ILIT( 57)
362 tagOf_PrimOp IntSubCOp = ILIT( 58)
363 tagOf_PrimOp IntMulCOp = ILIT( 59)
364 tagOf_PrimOp Int2WordOp = ILIT( 60)
365 tagOf_PrimOp Word2IntOp = ILIT( 61)
366 tagOf_PrimOp Int2AddrOp = ILIT( 62)
367 tagOf_PrimOp Addr2IntOp = ILIT( 63)
369 tagOf_PrimOp FloatAddOp = ILIT( 64)
370 tagOf_PrimOp FloatSubOp = ILIT( 65)
371 tagOf_PrimOp FloatMulOp = ILIT( 66)
372 tagOf_PrimOp FloatDivOp = ILIT( 67)
373 tagOf_PrimOp FloatNegOp = ILIT( 68)
374 tagOf_PrimOp Float2IntOp = ILIT( 69)
375 tagOf_PrimOp Int2FloatOp = ILIT( 70)
376 tagOf_PrimOp FloatExpOp = ILIT( 71)
377 tagOf_PrimOp FloatLogOp = ILIT( 72)
378 tagOf_PrimOp FloatSqrtOp = ILIT( 73)
379 tagOf_PrimOp FloatSinOp = ILIT( 74)
380 tagOf_PrimOp FloatCosOp = ILIT( 75)
381 tagOf_PrimOp FloatTanOp = ILIT( 76)
382 tagOf_PrimOp FloatAsinOp = ILIT( 77)
383 tagOf_PrimOp FloatAcosOp = ILIT( 78)
384 tagOf_PrimOp FloatAtanOp = ILIT( 79)
385 tagOf_PrimOp FloatSinhOp = ILIT( 80)
386 tagOf_PrimOp FloatCoshOp = ILIT( 81)
387 tagOf_PrimOp FloatTanhOp = ILIT( 82)
388 tagOf_PrimOp FloatPowerOp = ILIT( 83)
390 tagOf_PrimOp DoubleAddOp = ILIT( 84)
391 tagOf_PrimOp DoubleSubOp = ILIT( 85)
392 tagOf_PrimOp DoubleMulOp = ILIT( 86)
393 tagOf_PrimOp DoubleDivOp = ILIT( 87)
394 tagOf_PrimOp DoubleNegOp = ILIT( 88)
395 tagOf_PrimOp Double2IntOp = ILIT( 89)
396 tagOf_PrimOp Int2DoubleOp = ILIT( 90)
397 tagOf_PrimOp Double2FloatOp = ILIT( 91)
398 tagOf_PrimOp Float2DoubleOp = ILIT( 92)
399 tagOf_PrimOp DoubleExpOp = ILIT( 93)
400 tagOf_PrimOp DoubleLogOp = ILIT( 94)
401 tagOf_PrimOp DoubleSqrtOp = ILIT( 95)
402 tagOf_PrimOp DoubleSinOp = ILIT( 96)
403 tagOf_PrimOp DoubleCosOp = ILIT( 97)
404 tagOf_PrimOp DoubleTanOp = ILIT( 98)
405 tagOf_PrimOp DoubleAsinOp = ILIT( 99)
406 tagOf_PrimOp DoubleAcosOp = ILIT(100)
407 tagOf_PrimOp DoubleAtanOp = ILIT(101)
408 tagOf_PrimOp DoubleSinhOp = ILIT(102)
409 tagOf_PrimOp DoubleCoshOp = ILIT(103)
410 tagOf_PrimOp DoubleTanhOp = ILIT(104)
411 tagOf_PrimOp DoublePowerOp = ILIT(105)
413 tagOf_PrimOp IntegerAddOp = ILIT(106)
414 tagOf_PrimOp IntegerSubOp = ILIT(107)
415 tagOf_PrimOp IntegerMulOp = ILIT(108)
416 tagOf_PrimOp IntegerGcdOp = ILIT(109)
417 tagOf_PrimOp IntegerQuotRemOp = ILIT(110)
418 tagOf_PrimOp IntegerDivModOp = ILIT(111)
419 tagOf_PrimOp IntegerNegOp = ILIT(112)
420 tagOf_PrimOp IntegerCmpOp = ILIT(113)
421 tagOf_PrimOp IntegerCmpIntOp = ILIT(114)
422 tagOf_PrimOp Integer2IntOp = ILIT(115)
423 tagOf_PrimOp Integer2WordOp = ILIT(116)
424 tagOf_PrimOp Int2IntegerOp = ILIT(117)
425 tagOf_PrimOp Word2IntegerOp = ILIT(118)
426 tagOf_PrimOp Addr2IntegerOp = ILIT(119)
427 tagOf_PrimOp IntegerToInt64Op = ILIT(120)
428 tagOf_PrimOp Int64ToIntegerOp = ILIT(121)
429 tagOf_PrimOp IntegerToWord64Op = ILIT(122)
430 tagOf_PrimOp Word64ToIntegerOp = ILIT(123)
431 tagOf_PrimOp FloatDecodeOp = ILIT(125)
432 tagOf_PrimOp DoubleDecodeOp = ILIT(127)
434 tagOf_PrimOp NewArrayOp = ILIT(128)
435 tagOf_PrimOp (NewByteArrayOp CharRep) = ILIT(129)
436 tagOf_PrimOp (NewByteArrayOp IntRep) = ILIT(130)
437 tagOf_PrimOp (NewByteArrayOp WordRep) = ILIT(131)
438 tagOf_PrimOp (NewByteArrayOp AddrRep) = ILIT(132)
439 tagOf_PrimOp (NewByteArrayOp FloatRep) = ILIT(133)
440 tagOf_PrimOp (NewByteArrayOp DoubleRep) = ILIT(134)
441 tagOf_PrimOp (NewByteArrayOp StablePtrRep) = ILIT(135)
443 tagOf_PrimOp SameMutableArrayOp = ILIT(136)
444 tagOf_PrimOp SameMutableByteArrayOp = ILIT(137)
445 tagOf_PrimOp ReadArrayOp = ILIT(138)
446 tagOf_PrimOp WriteArrayOp = ILIT(139)
447 tagOf_PrimOp IndexArrayOp = ILIT(140)
449 tagOf_PrimOp (ReadByteArrayOp CharRep) = ILIT(141)
450 tagOf_PrimOp (ReadByteArrayOp IntRep) = ILIT(142)
451 tagOf_PrimOp (ReadByteArrayOp WordRep) = ILIT(143)
452 tagOf_PrimOp (ReadByteArrayOp AddrRep) = ILIT(144)
453 tagOf_PrimOp (ReadByteArrayOp FloatRep) = ILIT(145)
454 tagOf_PrimOp (ReadByteArrayOp DoubleRep) = ILIT(146)
455 tagOf_PrimOp (ReadByteArrayOp StablePtrRep) = ILIT(147)
456 tagOf_PrimOp (ReadByteArrayOp Int64Rep) = ILIT(148)
457 tagOf_PrimOp (ReadByteArrayOp Word64Rep) = ILIT(149)
459 tagOf_PrimOp (WriteByteArrayOp CharRep) = ILIT(150)
460 tagOf_PrimOp (WriteByteArrayOp IntRep) = ILIT(151)
461 tagOf_PrimOp (WriteByteArrayOp WordRep) = ILIT(152)
462 tagOf_PrimOp (WriteByteArrayOp AddrRep) = ILIT(153)
463 tagOf_PrimOp (WriteByteArrayOp FloatRep) = ILIT(154)
464 tagOf_PrimOp (WriteByteArrayOp DoubleRep) = ILIT(155)
465 tagOf_PrimOp (WriteByteArrayOp StablePtrRep) = ILIT(156)
466 tagOf_PrimOp (WriteByteArrayOp Int64Rep) = ILIT(157)
467 tagOf_PrimOp (WriteByteArrayOp Word64Rep) = ILIT(158)
469 tagOf_PrimOp (IndexByteArrayOp CharRep) = ILIT(159)
470 tagOf_PrimOp (IndexByteArrayOp IntRep) = ILIT(160)
471 tagOf_PrimOp (IndexByteArrayOp WordRep) = ILIT(161)
472 tagOf_PrimOp (IndexByteArrayOp AddrRep) = ILIT(162)
473 tagOf_PrimOp (IndexByteArrayOp FloatRep) = ILIT(163)
474 tagOf_PrimOp (IndexByteArrayOp DoubleRep) = ILIT(164)
475 tagOf_PrimOp (IndexByteArrayOp StablePtrRep) = ILIT(165)
476 tagOf_PrimOp (IndexByteArrayOp Int64Rep) = ILIT(166)
477 tagOf_PrimOp (IndexByteArrayOp Word64Rep) = ILIT(167)
479 tagOf_PrimOp (IndexOffAddrOp CharRep) = ILIT(168)
480 tagOf_PrimOp (IndexOffAddrOp IntRep) = ILIT(169)
481 tagOf_PrimOp (IndexOffAddrOp WordRep) = ILIT(170)
482 tagOf_PrimOp (IndexOffAddrOp AddrRep) = ILIT(171)
483 tagOf_PrimOp (IndexOffAddrOp FloatRep) = ILIT(172)
484 tagOf_PrimOp (IndexOffAddrOp DoubleRep) = ILIT(173)
485 tagOf_PrimOp (IndexOffAddrOp StablePtrRep) = ILIT(174)
486 tagOf_PrimOp (IndexOffAddrOp Int64Rep) = ILIT(175)
487 tagOf_PrimOp (IndexOffAddrOp Word64Rep) = ILIT(176)
489 tagOf_PrimOp (IndexOffForeignObjOp CharRep) = ILIT(177)
490 tagOf_PrimOp (IndexOffForeignObjOp IntRep) = ILIT(178)
491 tagOf_PrimOp (IndexOffForeignObjOp WordRep) = ILIT(179)
492 tagOf_PrimOp (IndexOffForeignObjOp AddrRep) = ILIT(180)
493 tagOf_PrimOp (IndexOffForeignObjOp FloatRep) = ILIT(181)
494 tagOf_PrimOp (IndexOffForeignObjOp DoubleRep) = ILIT(182)
495 tagOf_PrimOp (IndexOffForeignObjOp StablePtrRep) = ILIT(183)
496 tagOf_PrimOp (IndexOffForeignObjOp Int64Rep) = ILIT(184)
497 tagOf_PrimOp (IndexOffForeignObjOp Word64Rep) = ILIT(185)
499 tagOf_PrimOp (WriteOffAddrOp CharRep) = ILIT(186)
500 tagOf_PrimOp (WriteOffAddrOp IntRep) = ILIT(187)
501 tagOf_PrimOp (WriteOffAddrOp WordRep) = ILIT(188)
502 tagOf_PrimOp (WriteOffAddrOp AddrRep) = ILIT(189)
503 tagOf_PrimOp (WriteOffAddrOp FloatRep) = ILIT(190)
504 tagOf_PrimOp (WriteOffAddrOp DoubleRep) = ILIT(191)
505 tagOf_PrimOp (WriteOffAddrOp StablePtrRep) = ILIT(192)
506 tagOf_PrimOp (WriteOffAddrOp ForeignObjRep) = ILIT(193)
507 tagOf_PrimOp (WriteOffAddrOp Int64Rep) = ILIT(194)
508 tagOf_PrimOp (WriteOffAddrOp Word64Rep) = ILIT(195)
510 tagOf_PrimOp UnsafeFreezeArrayOp = ILIT(196)
511 tagOf_PrimOp UnsafeFreezeByteArrayOp = ILIT(197)
512 tagOf_PrimOp UnsafeThawArrayOp = ILIT(198)
513 tagOf_PrimOp UnsafeThawByteArrayOp = ILIT(199)
514 tagOf_PrimOp SizeofByteArrayOp = ILIT(200)
515 tagOf_PrimOp SizeofMutableByteArrayOp = ILIT(201)
517 tagOf_PrimOp NewMVarOp = ILIT(202)
518 tagOf_PrimOp TakeMVarOp = ILIT(203)
519 tagOf_PrimOp PutMVarOp = ILIT(204)
520 tagOf_PrimOp SameMVarOp = ILIT(205)
521 tagOf_PrimOp IsEmptyMVarOp = ILIT(206)
522 tagOf_PrimOp MakeForeignObjOp = ILIT(207)
523 tagOf_PrimOp WriteForeignObjOp = ILIT(208)
524 tagOf_PrimOp MkWeakOp = ILIT(209)
525 tagOf_PrimOp DeRefWeakOp = ILIT(210)
526 tagOf_PrimOp FinalizeWeakOp = ILIT(211)
527 tagOf_PrimOp MakeStableNameOp = ILIT(212)
528 tagOf_PrimOp EqStableNameOp = ILIT(213)
529 tagOf_PrimOp StableNameToIntOp = ILIT(214)
530 tagOf_PrimOp MakeStablePtrOp = ILIT(215)
531 tagOf_PrimOp DeRefStablePtrOp = ILIT(216)
532 tagOf_PrimOp EqStablePtrOp = ILIT(217)
533 tagOf_PrimOp (CCallOp _ _ _ _) = ILIT(218)
534 tagOf_PrimOp ReallyUnsafePtrEqualityOp = ILIT(219)
535 tagOf_PrimOp SeqOp = ILIT(220)
536 tagOf_PrimOp ParOp = ILIT(221)
537 tagOf_PrimOp ForkOp = ILIT(222)
538 tagOf_PrimOp KillThreadOp = ILIT(223)
539 tagOf_PrimOp YieldOp = ILIT(224)
540 tagOf_PrimOp MyThreadIdOp = ILIT(225)
541 tagOf_PrimOp DelayOp = ILIT(226)
542 tagOf_PrimOp WaitReadOp = ILIT(227)
543 tagOf_PrimOp WaitWriteOp = ILIT(228)
544 tagOf_PrimOp ParGlobalOp = ILIT(229)
545 tagOf_PrimOp ParLocalOp = ILIT(230)
546 tagOf_PrimOp ParAtOp = ILIT(231)
547 tagOf_PrimOp ParAtAbsOp = ILIT(232)
548 tagOf_PrimOp ParAtRelOp = ILIT(233)
549 tagOf_PrimOp ParAtForNowOp = ILIT(234)
550 tagOf_PrimOp CopyableOp = ILIT(235)
551 tagOf_PrimOp NoFollowOp = ILIT(236)
552 tagOf_PrimOp NewMutVarOp = ILIT(237)
553 tagOf_PrimOp ReadMutVarOp = ILIT(238)
554 tagOf_PrimOp WriteMutVarOp = ILIT(239)
555 tagOf_PrimOp SameMutVarOp = ILIT(240)
556 tagOf_PrimOp CatchOp = ILIT(241)
557 tagOf_PrimOp RaiseOp = ILIT(242)
558 tagOf_PrimOp DataToTagOp = ILIT(243)
559 tagOf_PrimOp TagToEnumOp = ILIT(244)
561 tagOf_PrimOp op = pprPanic# "tagOf_PrimOp: pattern-match" (ppr op)
562 --panic# "tagOf_PrimOp: pattern-match"
564 instance Eq PrimOp where
565 op1 == op2 = tagOf_PrimOp op1 _EQ_ tagOf_PrimOp op2
567 instance Ord PrimOp where
568 op1 < op2 = tagOf_PrimOp op1 _LT_ tagOf_PrimOp op2
569 op1 <= op2 = tagOf_PrimOp op1 _LE_ tagOf_PrimOp op2
570 op1 >= op2 = tagOf_PrimOp op1 _GE_ tagOf_PrimOp op2
571 op1 > op2 = tagOf_PrimOp op1 _GT_ tagOf_PrimOp op2
572 op1 `compare` op2 | op1 < op2 = LT
576 instance Outputable PrimOp where
577 ppr op = pprPrimOp op
579 instance Show PrimOp where
580 showsPrec p op = showsPrecSDoc p (pprPrimOp op)
583 An @Enum@-derived list would be better; meanwhile... (ToDo)
712 NewByteArrayOp CharRep,
713 NewByteArrayOp IntRep,
714 NewByteArrayOp WordRep,
715 NewByteArrayOp AddrRep,
716 NewByteArrayOp FloatRep,
717 NewByteArrayOp DoubleRep,
718 NewByteArrayOp StablePtrRep,
720 SameMutableByteArrayOp,
724 ReadByteArrayOp CharRep,
725 ReadByteArrayOp IntRep,
726 ReadByteArrayOp WordRep,
727 ReadByteArrayOp AddrRep,
728 ReadByteArrayOp FloatRep,
729 ReadByteArrayOp DoubleRep,
730 ReadByteArrayOp StablePtrRep,
731 ReadByteArrayOp Int64Rep,
732 ReadByteArrayOp Word64Rep,
733 WriteByteArrayOp CharRep,
734 WriteByteArrayOp IntRep,
735 WriteByteArrayOp WordRep,
736 WriteByteArrayOp AddrRep,
737 WriteByteArrayOp FloatRep,
738 WriteByteArrayOp DoubleRep,
739 WriteByteArrayOp StablePtrRep,
740 WriteByteArrayOp Int64Rep,
741 WriteByteArrayOp Word64Rep,
742 IndexByteArrayOp CharRep,
743 IndexByteArrayOp IntRep,
744 IndexByteArrayOp WordRep,
745 IndexByteArrayOp AddrRep,
746 IndexByteArrayOp FloatRep,
747 IndexByteArrayOp DoubleRep,
748 IndexByteArrayOp StablePtrRep,
749 IndexByteArrayOp Int64Rep,
750 IndexByteArrayOp Word64Rep,
751 IndexOffForeignObjOp CharRep,
752 IndexOffForeignObjOp AddrRep,
753 IndexOffForeignObjOp IntRep,
754 IndexOffForeignObjOp WordRep,
755 IndexOffForeignObjOp FloatRep,
756 IndexOffForeignObjOp DoubleRep,
757 IndexOffForeignObjOp StablePtrRep,
758 IndexOffForeignObjOp Int64Rep,
759 IndexOffForeignObjOp Word64Rep,
760 IndexOffAddrOp CharRep,
761 IndexOffAddrOp IntRep,
762 IndexOffAddrOp WordRep,
763 IndexOffAddrOp AddrRep,
764 IndexOffAddrOp FloatRep,
765 IndexOffAddrOp DoubleRep,
766 IndexOffAddrOp StablePtrRep,
767 IndexOffAddrOp Int64Rep,
768 IndexOffAddrOp Word64Rep,
769 WriteOffAddrOp CharRep,
770 WriteOffAddrOp IntRep,
771 WriteOffAddrOp WordRep,
772 WriteOffAddrOp AddrRep,
773 WriteOffAddrOp FloatRep,
774 WriteOffAddrOp DoubleRep,
775 WriteOffAddrOp ForeignObjRep,
776 WriteOffAddrOp StablePtrRep,
777 WriteOffAddrOp Int64Rep,
778 WriteOffAddrOp Word64Rep,
780 UnsafeFreezeByteArrayOp,
782 UnsafeThawByteArrayOp,
784 SizeofMutableByteArrayOp,
807 ReallyUnsafePtrEqualityOp,
830 %************************************************************************
832 \subsection[PrimOp-info]{The essential info about each @PrimOp@}
834 %************************************************************************
836 The @String@ in the @PrimOpInfos@ is the ``base name'' by which the user may
837 refer to the primitive operation. The conventional \tr{#}-for-
838 unboxed ops is added on later.
840 The reason for the funny characters in the names is so we do not
841 interfere with the programmer's Haskell name spaces.
843 We use @PrimKinds@ for the ``type'' information, because they're
844 (slightly) more convenient to use than @TyCons@.
847 = Dyadic OccName -- string :: T -> T -> T
849 | Monadic OccName -- string :: T -> T
851 | Compare OccName -- string :: T -> T -> Bool
854 | GenPrimOp OccName -- string :: \/a1..an . T1 -> .. -> Tk -> T
859 mkDyadic str ty = Dyadic (mkSrcVarOcc str) ty
860 mkMonadic str ty = Monadic (mkSrcVarOcc str) ty
861 mkCompare str ty = Compare (mkSrcVarOcc str) ty
862 mkGenPrimOp str tvs tys ty = GenPrimOp (mkSrcVarOcc str) tvs tys ty
867 one_Integer_ty = [intPrimTy, byteArrayPrimTy]
869 = [intPrimTy, byteArrayPrimTy, -- first Integer pieces
870 intPrimTy, byteArrayPrimTy] -- second '' pieces
871 an_Integer_and_Int_tys
872 = [intPrimTy, byteArrayPrimTy, -- Integer
875 unboxedPair = mkUnboxedTupleTy 2
876 unboxedTriple = mkUnboxedTupleTy 3
877 unboxedQuadruple = mkUnboxedTupleTy 4
879 integerMonadic name = mkGenPrimOp name [] one_Integer_ty
880 (unboxedPair one_Integer_ty)
882 integerDyadic name = mkGenPrimOp name [] two_Integer_tys
883 (unboxedPair one_Integer_ty)
885 integerDyadic2Results name = mkGenPrimOp name [] two_Integer_tys
886 (unboxedQuadruple two_Integer_tys)
888 integerCompare name = mkGenPrimOp name [] two_Integer_tys intPrimTy
891 %************************************************************************
893 \subsubsection{Strictness}
895 %************************************************************************
897 Not all primops are strict!
900 primOpStrictness :: PrimOp -> ([Demand], Bool)
901 -- See IdInfo.StrictnessInfo for discussion of what the results
902 -- **NB** as a cheap hack, to avoid having to look up the PrimOp's arity,
903 -- the list of demands may be infinite!
904 -- Use only the ones you ned.
906 primOpStrictness SeqOp = ([wwLazy], False)
907 primOpStrictness ParOp = ([wwLazy], False)
908 primOpStrictness ForkOp = ([wwLazy, wwPrim], False)
910 primOpStrictness NewArrayOp = ([wwPrim, wwLazy, wwPrim], False)
911 primOpStrictness WriteArrayOp = ([wwPrim, wwPrim, wwLazy, wwPrim], False)
913 primOpStrictness NewMutVarOp = ([wwLazy, wwPrim], False)
914 primOpStrictness WriteMutVarOp = ([wwPrim, wwLazy, wwPrim], False)
916 primOpStrictness PutMVarOp = ([wwPrim, wwLazy, wwPrim], False)
918 primOpStrictness CatchOp = ([wwLazy, wwLazy], False)
919 primOpStrictness RaiseOp = ([wwLazy], True) -- NB: True => result is bottom
921 primOpStrictness MkWeakOp = ([wwLazy, wwLazy, wwLazy, wwPrim], False)
922 primOpStrictness MakeStableNameOp = ([wwLazy, wwPrim], False)
923 primOpStrictness MakeStablePtrOp = ([wwLazy, wwPrim], False)
925 primOpStrictness DataToTagOp = ([wwLazy], False)
927 -- The rest all have primitive-typed arguments
928 primOpStrictness other = (repeat wwPrim, False)
931 %************************************************************************
933 \subsubsection[PrimOp-comparison]{PrimOpInfo basic comparison ops}
935 %************************************************************************
937 @primOpInfo@ gives all essential information (from which everything
938 else, notably a type, can be constructed) for each @PrimOp@.
941 primOpInfo :: PrimOp -> PrimOpInfo
944 There's plenty of this stuff!
947 primOpInfo CharGtOp = mkCompare SLIT("gtChar#") charPrimTy
948 primOpInfo CharGeOp = mkCompare SLIT("geChar#") charPrimTy
949 primOpInfo CharEqOp = mkCompare SLIT("eqChar#") charPrimTy
950 primOpInfo CharNeOp = mkCompare SLIT("neChar#") charPrimTy
951 primOpInfo CharLtOp = mkCompare SLIT("ltChar#") charPrimTy
952 primOpInfo CharLeOp = mkCompare SLIT("leChar#") charPrimTy
954 primOpInfo IntGtOp = mkCompare SLIT(">#") intPrimTy
955 primOpInfo IntGeOp = mkCompare SLIT(">=#") intPrimTy
956 primOpInfo IntEqOp = mkCompare SLIT("==#") intPrimTy
957 primOpInfo IntNeOp = mkCompare SLIT("/=#") intPrimTy
958 primOpInfo IntLtOp = mkCompare SLIT("<#") intPrimTy
959 primOpInfo IntLeOp = mkCompare SLIT("<=#") intPrimTy
961 primOpInfo WordGtOp = mkCompare SLIT("gtWord#") wordPrimTy
962 primOpInfo WordGeOp = mkCompare SLIT("geWord#") wordPrimTy
963 primOpInfo WordEqOp = mkCompare SLIT("eqWord#") wordPrimTy
964 primOpInfo WordNeOp = mkCompare SLIT("neWord#") wordPrimTy
965 primOpInfo WordLtOp = mkCompare SLIT("ltWord#") wordPrimTy
966 primOpInfo WordLeOp = mkCompare SLIT("leWord#") wordPrimTy
968 primOpInfo AddrGtOp = mkCompare SLIT("gtAddr#") addrPrimTy
969 primOpInfo AddrGeOp = mkCompare SLIT("geAddr#") addrPrimTy
970 primOpInfo AddrEqOp = mkCompare SLIT("eqAddr#") addrPrimTy
971 primOpInfo AddrNeOp = mkCompare SLIT("neAddr#") addrPrimTy
972 primOpInfo AddrLtOp = mkCompare SLIT("ltAddr#") addrPrimTy
973 primOpInfo AddrLeOp = mkCompare SLIT("leAddr#") addrPrimTy
975 primOpInfo FloatGtOp = mkCompare SLIT("gtFloat#") floatPrimTy
976 primOpInfo FloatGeOp = mkCompare SLIT("geFloat#") floatPrimTy
977 primOpInfo FloatEqOp = mkCompare SLIT("eqFloat#") floatPrimTy
978 primOpInfo FloatNeOp = mkCompare SLIT("neFloat#") floatPrimTy
979 primOpInfo FloatLtOp = mkCompare SLIT("ltFloat#") floatPrimTy
980 primOpInfo FloatLeOp = mkCompare SLIT("leFloat#") floatPrimTy
982 primOpInfo DoubleGtOp = mkCompare SLIT(">##") doublePrimTy
983 primOpInfo DoubleGeOp = mkCompare SLIT(">=##") doublePrimTy
984 primOpInfo DoubleEqOp = mkCompare SLIT("==##") doublePrimTy
985 primOpInfo DoubleNeOp = mkCompare SLIT("/=##") doublePrimTy
986 primOpInfo DoubleLtOp = mkCompare SLIT("<##") doublePrimTy
987 primOpInfo DoubleLeOp = mkCompare SLIT("<=##") doublePrimTy
991 %************************************************************************
993 \subsubsection[PrimOp-Char]{PrimOpInfo for @Char#@s}
995 %************************************************************************
998 primOpInfo OrdOp = mkGenPrimOp SLIT("ord#") [] [charPrimTy] intPrimTy
999 primOpInfo ChrOp = mkGenPrimOp SLIT("chr#") [] [intPrimTy] charPrimTy
1002 %************************************************************************
1004 \subsubsection[PrimOp-Int]{PrimOpInfo for @Int#@s}
1006 %************************************************************************
1009 primOpInfo IntAddOp = mkDyadic SLIT("+#") intPrimTy
1010 primOpInfo IntSubOp = mkDyadic SLIT("-#") intPrimTy
1011 primOpInfo IntMulOp = mkDyadic SLIT("*#") intPrimTy
1012 primOpInfo IntQuotOp = mkDyadic SLIT("quotInt#") intPrimTy
1013 primOpInfo IntRemOp = mkDyadic SLIT("remInt#") intPrimTy
1015 primOpInfo IntNegOp = mkMonadic SLIT("negateInt#") intPrimTy
1016 primOpInfo IntAbsOp = mkMonadic SLIT("absInt#") intPrimTy
1018 primOpInfo IntAddCOp =
1019 mkGenPrimOp SLIT("addIntC#") [] [intPrimTy, intPrimTy]
1020 (unboxedPair [intPrimTy, intPrimTy])
1022 primOpInfo IntSubCOp =
1023 mkGenPrimOp SLIT("subIntC#") [] [intPrimTy, intPrimTy]
1024 (unboxedPair [intPrimTy, intPrimTy])
1026 primOpInfo IntMulCOp =
1027 mkGenPrimOp SLIT("mulIntC#") [] [intPrimTy, intPrimTy]
1028 (unboxedPair [intPrimTy, intPrimTy])
1031 %************************************************************************
1033 \subsubsection[PrimOp-Word]{PrimOpInfo for @Word#@s}
1035 %************************************************************************
1037 A @Word#@ is an unsigned @Int#@.
1040 primOpInfo WordQuotOp = mkDyadic SLIT("quotWord#") wordPrimTy
1041 primOpInfo WordRemOp = mkDyadic SLIT("remWord#") wordPrimTy
1043 primOpInfo AndOp = mkDyadic SLIT("and#") wordPrimTy
1044 primOpInfo OrOp = mkDyadic SLIT("or#") wordPrimTy
1045 primOpInfo XorOp = mkDyadic SLIT("xor#") wordPrimTy
1046 primOpInfo NotOp = mkMonadic SLIT("not#") wordPrimTy
1049 = mkGenPrimOp SLIT("shiftL#") [] [wordPrimTy, intPrimTy] wordPrimTy
1051 = mkGenPrimOp SLIT("shiftRL#") [] [wordPrimTy, intPrimTy] wordPrimTy
1054 = mkGenPrimOp SLIT("iShiftL#") [] [intPrimTy, intPrimTy] intPrimTy
1056 = mkGenPrimOp SLIT("iShiftRA#") [] [intPrimTy, intPrimTy] intPrimTy
1058 = mkGenPrimOp SLIT("iShiftRL#") [] [intPrimTy, intPrimTy] intPrimTy
1060 primOpInfo Int2WordOp = mkGenPrimOp SLIT("int2Word#") [] [intPrimTy] wordPrimTy
1061 primOpInfo Word2IntOp = mkGenPrimOp SLIT("word2Int#") [] [wordPrimTy] intPrimTy
1064 %************************************************************************
1066 \subsubsection[PrimOp-Addr]{PrimOpInfo for @Addr#@s}
1068 %************************************************************************
1071 primOpInfo Int2AddrOp = mkGenPrimOp SLIT("int2Addr#") [] [intPrimTy] addrPrimTy
1072 primOpInfo Addr2IntOp = mkGenPrimOp SLIT("addr2Int#") [] [addrPrimTy] intPrimTy
1076 %************************************************************************
1078 \subsubsection[PrimOp-Float]{PrimOpInfo for @Float#@s}
1080 %************************************************************************
1082 @decodeFloat#@ is given w/ Integer-stuff (it's similar).
1085 primOpInfo FloatAddOp = mkDyadic SLIT("plusFloat#") floatPrimTy
1086 primOpInfo FloatSubOp = mkDyadic SLIT("minusFloat#") floatPrimTy
1087 primOpInfo FloatMulOp = mkDyadic SLIT("timesFloat#") floatPrimTy
1088 primOpInfo FloatDivOp = mkDyadic SLIT("divideFloat#") floatPrimTy
1089 primOpInfo FloatNegOp = mkMonadic SLIT("negateFloat#") floatPrimTy
1091 primOpInfo Float2IntOp = mkGenPrimOp SLIT("float2Int#") [] [floatPrimTy] intPrimTy
1092 primOpInfo Int2FloatOp = mkGenPrimOp SLIT("int2Float#") [] [intPrimTy] floatPrimTy
1094 primOpInfo FloatExpOp = mkMonadic SLIT("expFloat#") floatPrimTy
1095 primOpInfo FloatLogOp = mkMonadic SLIT("logFloat#") floatPrimTy
1096 primOpInfo FloatSqrtOp = mkMonadic SLIT("sqrtFloat#") floatPrimTy
1097 primOpInfo FloatSinOp = mkMonadic SLIT("sinFloat#") floatPrimTy
1098 primOpInfo FloatCosOp = mkMonadic SLIT("cosFloat#") floatPrimTy
1099 primOpInfo FloatTanOp = mkMonadic SLIT("tanFloat#") floatPrimTy
1100 primOpInfo FloatAsinOp = mkMonadic SLIT("asinFloat#") floatPrimTy
1101 primOpInfo FloatAcosOp = mkMonadic SLIT("acosFloat#") floatPrimTy
1102 primOpInfo FloatAtanOp = mkMonadic SLIT("atanFloat#") floatPrimTy
1103 primOpInfo FloatSinhOp = mkMonadic SLIT("sinhFloat#") floatPrimTy
1104 primOpInfo FloatCoshOp = mkMonadic SLIT("coshFloat#") floatPrimTy
1105 primOpInfo FloatTanhOp = mkMonadic SLIT("tanhFloat#") floatPrimTy
1106 primOpInfo FloatPowerOp = mkDyadic SLIT("powerFloat#") floatPrimTy
1109 %************************************************************************
1111 \subsubsection[PrimOp-Double]{PrimOpInfo for @Double#@s}
1113 %************************************************************************
1115 @decodeDouble#@ is given w/ Integer-stuff (it's similar).
1118 primOpInfo DoubleAddOp = mkDyadic SLIT("+##") doublePrimTy
1119 primOpInfo DoubleSubOp = mkDyadic SLIT("-##") doublePrimTy
1120 primOpInfo DoubleMulOp = mkDyadic SLIT("*##") doublePrimTy
1121 primOpInfo DoubleDivOp = mkDyadic SLIT("/##") doublePrimTy
1122 primOpInfo DoubleNegOp = mkMonadic SLIT("negateDouble#") doublePrimTy
1124 primOpInfo Double2IntOp = mkGenPrimOp SLIT("double2Int#") [] [doublePrimTy] intPrimTy
1125 primOpInfo Int2DoubleOp = mkGenPrimOp SLIT("int2Double#") [] [intPrimTy] doublePrimTy
1127 primOpInfo Double2FloatOp = mkGenPrimOp SLIT("double2Float#") [] [doublePrimTy] floatPrimTy
1128 primOpInfo Float2DoubleOp = mkGenPrimOp SLIT("float2Double#") [] [floatPrimTy] doublePrimTy
1130 primOpInfo DoubleExpOp = mkMonadic SLIT("expDouble#") doublePrimTy
1131 primOpInfo DoubleLogOp = mkMonadic SLIT("logDouble#") doublePrimTy
1132 primOpInfo DoubleSqrtOp = mkMonadic SLIT("sqrtDouble#") doublePrimTy
1133 primOpInfo DoubleSinOp = mkMonadic SLIT("sinDouble#") doublePrimTy
1134 primOpInfo DoubleCosOp = mkMonadic SLIT("cosDouble#") doublePrimTy
1135 primOpInfo DoubleTanOp = mkMonadic SLIT("tanDouble#") doublePrimTy
1136 primOpInfo DoubleAsinOp = mkMonadic SLIT("asinDouble#") doublePrimTy
1137 primOpInfo DoubleAcosOp = mkMonadic SLIT("acosDouble#") doublePrimTy
1138 primOpInfo DoubleAtanOp = mkMonadic SLIT("atanDouble#") doublePrimTy
1139 primOpInfo DoubleSinhOp = mkMonadic SLIT("sinhDouble#") doublePrimTy
1140 primOpInfo DoubleCoshOp = mkMonadic SLIT("coshDouble#") doublePrimTy
1141 primOpInfo DoubleTanhOp = mkMonadic SLIT("tanhDouble#") doublePrimTy
1142 primOpInfo DoublePowerOp= mkDyadic SLIT("**##") doublePrimTy
1145 %************************************************************************
1147 \subsubsection[PrimOp-Integer]{PrimOpInfo for @Integer@ (and related!)}
1149 %************************************************************************
1152 primOpInfo IntegerNegOp = integerMonadic SLIT("negateInteger#")
1154 primOpInfo IntegerAddOp = integerDyadic SLIT("plusInteger#")
1155 primOpInfo IntegerSubOp = integerDyadic SLIT("minusInteger#")
1156 primOpInfo IntegerMulOp = integerDyadic SLIT("timesInteger#")
1157 primOpInfo IntegerGcdOp = integerDyadic SLIT("gcdInteger#")
1159 primOpInfo IntegerCmpOp = integerCompare SLIT("cmpInteger#")
1160 primOpInfo IntegerCmpIntOp
1161 = mkGenPrimOp SLIT("cmpIntegerInt#") [] an_Integer_and_Int_tys intPrimTy
1163 primOpInfo IntegerQuotRemOp = integerDyadic2Results SLIT("quotRemInteger#")
1164 primOpInfo IntegerDivModOp = integerDyadic2Results SLIT("divModInteger#")
1166 primOpInfo Integer2IntOp
1167 = mkGenPrimOp SLIT("integer2Int#") [] one_Integer_ty intPrimTy
1169 primOpInfo Integer2WordOp
1170 = mkGenPrimOp SLIT("integer2Word#") [] one_Integer_ty wordPrimTy
1172 primOpInfo Int2IntegerOp
1173 = mkGenPrimOp SLIT("int2Integer#") [] [intPrimTy]
1174 (unboxedPair one_Integer_ty)
1176 primOpInfo Word2IntegerOp
1177 = mkGenPrimOp SLIT("word2Integer#") [] [wordPrimTy]
1178 (unboxedPair one_Integer_ty)
1180 primOpInfo Addr2IntegerOp
1181 = mkGenPrimOp SLIT("addr2Integer#") [] [addrPrimTy]
1182 (unboxedPair one_Integer_ty)
1184 primOpInfo IntegerToInt64Op
1185 = mkGenPrimOp SLIT("integerToInt64#") [] one_Integer_ty int64PrimTy
1187 primOpInfo Int64ToIntegerOp
1188 = mkGenPrimOp SLIT("int64ToInteger#") [] [int64PrimTy]
1189 (unboxedPair one_Integer_ty)
1191 primOpInfo Word64ToIntegerOp
1192 = mkGenPrimOp SLIT("word64ToInteger#") [] [word64PrimTy]
1193 (unboxedPair one_Integer_ty)
1195 primOpInfo IntegerToWord64Op
1196 = mkGenPrimOp SLIT("integerToWord64#") [] one_Integer_ty word64PrimTy
1199 Decoding of floating-point numbers is sorta Integer-related. Encoding
1200 is done with plain ccalls now (see PrelNumExtra.lhs).
1203 primOpInfo FloatDecodeOp
1204 = mkGenPrimOp SLIT("decodeFloat#") [] [floatPrimTy]
1205 (unboxedTriple [intPrimTy, intPrimTy, byteArrayPrimTy])
1206 primOpInfo DoubleDecodeOp
1207 = mkGenPrimOp SLIT("decodeDouble#") [] [doublePrimTy]
1208 (unboxedTriple [intPrimTy, intPrimTy, byteArrayPrimTy])
1211 %************************************************************************
1213 \subsubsection[PrimOp-Arrays]{PrimOpInfo for primitive arrays}
1215 %************************************************************************
1218 newArray# :: Int# -> a -> State# s -> (# State# s, MutArr# s a #)
1219 newFooArray# :: Int# -> State# s -> (# State# s, MutByteArr# s #)
1223 primOpInfo NewArrayOp
1225 elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1226 state = mkStatePrimTy s
1228 mkGenPrimOp SLIT("newArray#") [s_tv, elt_tv]
1229 [intPrimTy, elt, state]
1230 (unboxedPair [state, mkMutableArrayPrimTy s elt])
1232 primOpInfo (NewByteArrayOp kind)
1234 s = alphaTy; s_tv = alphaTyVar
1236 op_str = _PK_ ("new" ++ primRepString kind ++ "Array#")
1237 state = mkStatePrimTy s
1239 mkGenPrimOp op_str [s_tv]
1241 (unboxedPair [state, mkMutableByteArrayPrimTy s])
1243 ---------------------------------------------------------------------------
1246 sameMutableArray# :: MutArr# s a -> MutArr# s a -> Bool
1247 sameMutableByteArray# :: MutByteArr# s -> MutByteArr# s -> Bool
1250 primOpInfo SameMutableArrayOp
1252 elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1253 mut_arr_ty = mkMutableArrayPrimTy s elt
1255 mkGenPrimOp SLIT("sameMutableArray#") [s_tv, elt_tv] [mut_arr_ty, mut_arr_ty]
1258 primOpInfo SameMutableByteArrayOp
1260 s = alphaTy; s_tv = alphaTyVar;
1261 mut_arr_ty = mkMutableByteArrayPrimTy s
1263 mkGenPrimOp SLIT("sameMutableByteArray#") [s_tv] [mut_arr_ty, mut_arr_ty]
1266 ---------------------------------------------------------------------------
1267 -- Primitive arrays of Haskell pointers:
1270 readArray# :: MutArr# s a -> Int# -> State# s -> (# State# s, a #)
1271 writeArray# :: MutArr# s a -> Int# -> a -> State# s -> State# s
1272 indexArray# :: Array# a -> Int# -> (# a #)
1275 primOpInfo ReadArrayOp
1277 elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1278 state = mkStatePrimTy s
1280 mkGenPrimOp SLIT("readArray#") [s_tv, elt_tv]
1281 [mkMutableArrayPrimTy s elt, intPrimTy, state]
1282 (unboxedPair [state, elt])
1285 primOpInfo WriteArrayOp
1287 elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1289 mkGenPrimOp SLIT("writeArray#") [s_tv, elt_tv]
1290 [mkMutableArrayPrimTy s elt, intPrimTy, elt, mkStatePrimTy s]
1293 primOpInfo IndexArrayOp
1294 = let { elt = alphaTy; elt_tv = alphaTyVar } in
1295 mkGenPrimOp SLIT("indexArray#") [elt_tv] [mkArrayPrimTy elt, intPrimTy]
1296 (mkUnboxedTupleTy 1 [elt])
1298 ---------------------------------------------------------------------------
1299 -- Primitive arrays full of unboxed bytes:
1301 primOpInfo (ReadByteArrayOp kind)
1303 s = alphaTy; s_tv = alphaTyVar
1305 op_str = _PK_ ("read" ++ primRepString kind ++ "Array#")
1306 (tvs, prim_ty) = mkPrimTyApp betaTyVars kind
1307 state = mkStatePrimTy s
1309 mkGenPrimOp op_str (s_tv:tvs)
1310 [mkMutableByteArrayPrimTy s, intPrimTy, state]
1311 (unboxedPair [state, prim_ty])
1313 primOpInfo (WriteByteArrayOp kind)
1315 s = alphaTy; s_tv = alphaTyVar
1316 op_str = _PK_ ("write" ++ primRepString kind ++ "Array#")
1317 (tvs, prim_ty) = mkPrimTyApp betaTyVars kind
1319 mkGenPrimOp op_str (s_tv:tvs)
1320 [mkMutableByteArrayPrimTy s, intPrimTy, prim_ty, mkStatePrimTy s]
1323 primOpInfo (IndexByteArrayOp kind)
1325 op_str = _PK_ ("index" ++ primRepString kind ++ "Array#")
1326 (tvs, prim_ty) = mkPrimTyApp alphaTyVars kind
1328 mkGenPrimOp op_str tvs [byteArrayPrimTy, intPrimTy] prim_ty
1330 primOpInfo (IndexOffForeignObjOp kind)
1332 op_str = _PK_ ("index" ++ primRepString kind ++ "OffForeignObj#")
1333 (tvs, prim_ty) = mkPrimTyApp alphaTyVars kind
1335 mkGenPrimOp op_str tvs [foreignObjPrimTy, intPrimTy] prim_ty
1337 primOpInfo (IndexOffAddrOp kind)
1339 op_str = _PK_ ("index" ++ primRepString kind ++ "OffAddr#")
1340 (tvs, prim_ty) = mkPrimTyApp alphaTyVars kind
1342 mkGenPrimOp op_str tvs [addrPrimTy, intPrimTy] prim_ty
1344 primOpInfo (WriteOffAddrOp kind)
1346 s = alphaTy; s_tv = alphaTyVar
1347 op_str = _PK_ ("write" ++ primRepString kind ++ "OffAddr#")
1348 (tvs, prim_ty) = mkPrimTyApp betaTyVars kind
1350 mkGenPrimOp op_str (s_tv:tvs)
1351 [addrPrimTy, intPrimTy, prim_ty, mkStatePrimTy s]
1354 ---------------------------------------------------------------------------
1356 unsafeFreezeArray# :: MutArr# s a -> State# s -> (# State# s, Array# a #)
1357 unsafeFreezeByteArray# :: MutByteArr# s -> State# s -> (# State# s, ByteArray# #)
1358 unsafeThawArray# :: Array# a -> State# s -> (# State# s, MutArr# s a #)
1359 unsafeThawByteArray# :: ByteArray# -> State# s -> (# State# s, MutByteArr# s #)
1362 primOpInfo UnsafeFreezeArrayOp
1364 elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1365 state = mkStatePrimTy s
1367 mkGenPrimOp SLIT("unsafeFreezeArray#") [s_tv, elt_tv]
1368 [mkMutableArrayPrimTy s elt, state]
1369 (unboxedPair [state, mkArrayPrimTy elt])
1371 primOpInfo UnsafeFreezeByteArrayOp
1373 s = alphaTy; s_tv = alphaTyVar;
1374 state = mkStatePrimTy s
1376 mkGenPrimOp SLIT("unsafeFreezeByteArray#") [s_tv]
1377 [mkMutableByteArrayPrimTy s, state]
1378 (unboxedPair [state, byteArrayPrimTy])
1380 primOpInfo UnsafeThawArrayOp
1382 elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1383 state = mkStatePrimTy s
1385 mkGenPrimOp SLIT("unsafeThawArray#") [s_tv, elt_tv]
1386 [mkArrayPrimTy elt, state]
1387 (unboxedPair [state, mkMutableArrayPrimTy s elt])
1389 primOpInfo UnsafeThawByteArrayOp
1391 s = alphaTy; s_tv = alphaTyVar;
1392 state = mkStatePrimTy s
1394 mkGenPrimOp SLIT("unsafeThawByteArray#") [s_tv]
1395 [byteArrayPrimTy, state]
1396 (unboxedPair [state, mkMutableByteArrayPrimTy s])
1398 ---------------------------------------------------------------------------
1399 primOpInfo SizeofByteArrayOp
1401 SLIT("sizeofByteArray#") []
1405 primOpInfo SizeofMutableByteArrayOp
1406 = let { s = alphaTy; s_tv = alphaTyVar } in
1408 SLIT("sizeofMutableByteArray#") [s_tv]
1409 [mkMutableByteArrayPrimTy s]
1414 %************************************************************************
1416 \subsubsection[PrimOp-MutVars]{PrimOpInfo for mutable variable ops}
1418 %************************************************************************
1421 primOpInfo NewMutVarOp
1423 elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1424 state = mkStatePrimTy s
1426 mkGenPrimOp SLIT("newMutVar#") [s_tv, elt_tv]
1428 (unboxedPair [state, mkMutVarPrimTy s elt])
1430 primOpInfo ReadMutVarOp
1432 elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1433 state = mkStatePrimTy s
1435 mkGenPrimOp SLIT("readMutVar#") [s_tv, elt_tv]
1436 [mkMutVarPrimTy s elt, state]
1437 (unboxedPair [state, elt])
1440 primOpInfo WriteMutVarOp
1442 elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1444 mkGenPrimOp SLIT("writeMutVar#") [s_tv, elt_tv]
1445 [mkMutVarPrimTy s elt, elt, mkStatePrimTy s]
1448 primOpInfo SameMutVarOp
1450 elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1451 mut_var_ty = mkMutVarPrimTy s elt
1453 mkGenPrimOp SLIT("sameMutVar#") [s_tv, elt_tv] [mut_var_ty, mut_var_ty]
1457 %************************************************************************
1459 \subsubsection[PrimOp-Exceptions]{PrimOpInfo for exceptions}
1461 %************************************************************************
1463 catch :: IO a -> (IOError -> IO a) -> IO a
1464 catch# :: a -> (b -> a) -> a
1469 a = alphaTy; a_tv = alphaTyVar
1470 b = betaTy; b_tv = betaTyVar;
1472 mkGenPrimOp SLIT("catch#") [a_tv, b_tv] [a, mkFunTy b a] a
1476 a = alphaTy; a_tv = alphaTyVar
1477 b = betaTy; b_tv = betaTyVar;
1479 mkGenPrimOp SLIT("raise#") [a_tv, b_tv] [a] b
1482 %************************************************************************
1484 \subsubsection[PrimOp-MVars]{PrimOpInfo for synchronizing Variables}
1486 %************************************************************************
1489 primOpInfo NewMVarOp
1491 elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1492 state = mkStatePrimTy s
1494 mkGenPrimOp SLIT("newMVar#") [s_tv, elt_tv] [state]
1495 (unboxedPair [state, mkMVarPrimTy s elt])
1497 primOpInfo TakeMVarOp
1499 elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1500 state = mkStatePrimTy s
1502 mkGenPrimOp SLIT("takeMVar#") [s_tv, elt_tv]
1503 [mkMVarPrimTy s elt, state]
1504 (unboxedPair [state, elt])
1506 primOpInfo PutMVarOp
1508 elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1510 mkGenPrimOp SLIT("putMVar#") [s_tv, elt_tv]
1511 [mkMVarPrimTy s elt, elt, mkStatePrimTy s]
1514 primOpInfo SameMVarOp
1516 elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1517 mvar_ty = mkMVarPrimTy s elt
1519 mkGenPrimOp SLIT("sameMVar#") [s_tv, elt_tv] [mvar_ty, mvar_ty] boolTy
1521 primOpInfo IsEmptyMVarOp
1523 elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1524 state = mkStatePrimTy s
1526 mkGenPrimOp SLIT("isEmptyMVar#") [s_tv, elt_tv]
1527 [mkMVarPrimTy s elt, mkStatePrimTy s]
1528 (unboxedPair [state, intPrimTy])
1532 %************************************************************************
1534 \subsubsection[PrimOp-Wait]{PrimOpInfo for delay/wait operations}
1536 %************************************************************************
1542 s = alphaTy; s_tv = alphaTyVar
1544 mkGenPrimOp SLIT("delay#") [s_tv]
1545 [intPrimTy, mkStatePrimTy s] (mkStatePrimTy s)
1547 primOpInfo WaitReadOp
1549 s = alphaTy; s_tv = alphaTyVar
1551 mkGenPrimOp SLIT("waitRead#") [s_tv]
1552 [intPrimTy, mkStatePrimTy s] (mkStatePrimTy s)
1554 primOpInfo WaitWriteOp
1556 s = alphaTy; s_tv = alphaTyVar
1558 mkGenPrimOp SLIT("waitWrite#") [s_tv]
1559 [intPrimTy, mkStatePrimTy s] (mkStatePrimTy s)
1562 %************************************************************************
1564 \subsubsection[PrimOp-Concurrency]{Concurrency Primitives}
1566 %************************************************************************
1569 -- fork# :: a -> State# RealWorld -> (# State# RealWorld, ThreadId# #)
1571 = mkGenPrimOp SLIT("fork#") [alphaTyVar]
1572 [alphaTy, realWorldStatePrimTy]
1573 (unboxedPair [realWorldStatePrimTy, threadIdPrimTy])
1575 -- killThread# :: ThreadId# -> exception -> State# RealWorld -> State# RealWorld
1576 primOpInfo KillThreadOp
1577 = mkGenPrimOp SLIT("killThread#") [alphaTyVar]
1578 [threadIdPrimTy, alphaTy, realWorldStatePrimTy]
1579 realWorldStatePrimTy
1581 -- yield# :: State# RealWorld -> State# RealWorld
1583 = mkGenPrimOp SLIT("yield#") []
1584 [realWorldStatePrimTy]
1585 realWorldStatePrimTy
1587 -- myThreadId# :: State# RealWorld -> (# State# RealWorld, ThreadId# #)
1588 primOpInfo MyThreadIdOp
1589 = mkGenPrimOp SLIT("myThreadId#") []
1590 [realWorldStatePrimTy]
1591 (unboxedPair [realWorldStatePrimTy, threadIdPrimTy])
1594 ************************************************************************
1596 \subsubsection[PrimOps-Foreign]{PrimOpInfo for Foreign Objects}
1598 %************************************************************************
1601 primOpInfo MakeForeignObjOp
1602 = mkGenPrimOp SLIT("makeForeignObj#") []
1603 [addrPrimTy, realWorldStatePrimTy]
1604 (unboxedPair [realWorldStatePrimTy, foreignObjPrimTy])
1606 primOpInfo WriteForeignObjOp
1608 s = alphaTy; s_tv = alphaTyVar
1610 mkGenPrimOp SLIT("writeForeignObj#") [s_tv]
1611 [foreignObjPrimTy, addrPrimTy, mkStatePrimTy s] (mkStatePrimTy s)
1614 ************************************************************************
1616 \subsubsection[PrimOps-Weak]{PrimOpInfo for Weak Pointers}
1618 %************************************************************************
1620 A @Weak@ Pointer is created by the @mkWeak#@ primitive:
1622 mkWeak# :: k -> v -> f -> State# RealWorld
1623 -> (# State# RealWorld, Weak# v #)
1625 In practice, you'll use the higher-level
1627 data Weak v = Weak# v
1628 mkWeak :: k -> v -> IO () -> IO (Weak v)
1632 = mkGenPrimOp SLIT("mkWeak#") [alphaTyVar, betaTyVar, gammaTyVar]
1633 [alphaTy, betaTy, gammaTy, realWorldStatePrimTy]
1634 (unboxedPair [realWorldStatePrimTy, mkWeakPrimTy betaTy])
1637 The following operation dereferences a weak pointer. The weak pointer
1638 may have been finalized, so the operation returns a result code which
1639 must be inspected before looking at the dereferenced value.
1641 deRefWeak# :: Weak# v -> State# RealWorld ->
1642 (# State# RealWorld, v, Int# #)
1644 Only look at v if the Int# returned is /= 0 !!
1646 The higher-level op is
1648 deRefWeak :: Weak v -> IO (Maybe v)
1651 primOpInfo DeRefWeakOp
1652 = mkGenPrimOp SLIT("deRefWeak#") [alphaTyVar]
1653 [mkWeakPrimTy alphaTy, realWorldStatePrimTy]
1654 (unboxedTriple [realWorldStatePrimTy, intPrimTy, alphaTy])
1657 Weak pointers can be finalized early by using the finalize# operation:
1659 finalizeWeak# :: Weak# v -> State# RealWorld ->
1660 (# State# RealWorld, Int#, IO () #)
1662 The Int# returned is either
1664 0 if the weak pointer has already been finalized, or it has no
1665 finalizer (the third component is then invalid).
1667 1 if the weak pointer is still alive, with the finalizer returned
1668 as the third component.
1671 primOpInfo FinalizeWeakOp
1672 = mkGenPrimOp SLIT("finalizeWeak#") [alphaTyVar]
1673 [mkWeakPrimTy alphaTy, realWorldStatePrimTy]
1674 (unboxedTriple [realWorldStatePrimTy, intPrimTy,
1675 mkFunTy realWorldStatePrimTy
1676 (unboxedPair [realWorldStatePrimTy,unitTy])])
1679 %************************************************************************
1681 \subsubsection[PrimOp-stable-pointers]{PrimOpInfo for stable pointers and stable names}
1683 %************************************************************************
1685 A {\em stable name/pointer} is an index into a table of stable name
1686 entries. Since the garbage collector is told about stable pointers,
1687 it is safe to pass a stable pointer to external systems such as C
1691 makeStablePtr# :: a -> State# RealWorld -> (# State# RealWorld, StablePtr# a #)
1692 freeStablePtr :: StablePtr# a -> State# RealWorld -> State# RealWorld
1693 deRefStablePtr# :: StablePtr# a -> State# RealWorld -> (# State# RealWorld, a #)
1694 eqStablePtr# :: StablePtr# a -> StablePtr# a -> Int#
1697 It may seem a bit surprising that @makeStablePtr#@ is a @IO@
1698 operation since it doesn't (directly) involve IO operations. The
1699 reason is that if some optimisation pass decided to duplicate calls to
1700 @makeStablePtr#@ and we only pass one of the stable pointers over, a
1701 massive space leak can result. Putting it into the IO monad
1702 prevents this. (Another reason for putting them in a monad is to
1703 ensure correct sequencing wrt the side-effecting @freeStablePtr@
1706 An important property of stable pointers is that if you call
1707 makeStablePtr# twice on the same object you get the same stable
1710 Note that we can implement @freeStablePtr#@ using @_ccall_@ (and,
1711 besides, it's not likely to be used from Haskell) so it's not a
1714 Question: Why @RealWorld@ - won't any instance of @_ST@ do the job? [ADR]
1719 A stable name is like a stable pointer, but with three important differences:
1721 (a) You can't deRef one to get back to the original object.
1722 (b) You can convert one to an Int.
1723 (c) You don't need to 'freeStableName'
1725 The existence of a stable name doesn't guarantee to keep the object it
1726 points to alive (unlike a stable pointer), hence (a).
1730 (a) makeStableName always returns the same value for a given
1731 object (same as stable pointers).
1733 (b) if two stable names are equal, it implies that the objects
1734 from which they were created were the same.
1736 (c) stableNameToInt always returns the same Int for a given
1740 primOpInfo MakeStablePtrOp
1741 = mkGenPrimOp SLIT("makeStablePtr#") [alphaTyVar]
1742 [alphaTy, realWorldStatePrimTy]
1743 (unboxedPair [realWorldStatePrimTy,
1744 mkTyConApp stablePtrPrimTyCon [alphaTy]])
1746 primOpInfo DeRefStablePtrOp
1747 = mkGenPrimOp SLIT("deRefStablePtr#") [alphaTyVar]
1748 [mkStablePtrPrimTy alphaTy, realWorldStatePrimTy]
1749 (unboxedPair [realWorldStatePrimTy, alphaTy])
1751 primOpInfo EqStablePtrOp
1752 = mkGenPrimOp SLIT("eqStablePtr#") [alphaTyVar, betaTyVar]
1753 [mkStablePtrPrimTy alphaTy, mkStablePtrPrimTy betaTy]
1756 primOpInfo MakeStableNameOp
1757 = mkGenPrimOp SLIT("makeStableName#") [alphaTyVar]
1758 [alphaTy, realWorldStatePrimTy]
1759 (unboxedPair [realWorldStatePrimTy,
1760 mkTyConApp stableNamePrimTyCon [alphaTy]])
1762 primOpInfo EqStableNameOp
1763 = mkGenPrimOp SLIT("eqStableName#") [alphaTyVar, betaTyVar]
1764 [mkStableNamePrimTy alphaTy, mkStableNamePrimTy betaTy]
1767 primOpInfo StableNameToIntOp
1768 = mkGenPrimOp SLIT("stableNameToInt#") [alphaTyVar]
1769 [mkStableNamePrimTy alphaTy]
1773 %************************************************************************
1775 \subsubsection[PrimOp-unsafePointerEquality]{PrimOpInfo for Pointer Equality}
1777 %************************************************************************
1779 [Alastair Reid is to blame for this!]
1781 These days, (Glasgow) Haskell seems to have a bit of everything from
1782 other languages: strict operations, mutable variables, sequencing,
1783 pointers, etc. About the only thing left is LISP's ability to test
1784 for pointer equality. So, let's add it in!
1787 reallyUnsafePtrEquality :: a -> a -> Int#
1790 which tests any two closures (of the same type) to see if they're the
1791 same. (Returns $0$ for @False@, $\neq 0$ for @True@ - to avoid
1792 difficulties of trying to box up the result.)
1794 NB This is {\em really unsafe\/} because even something as trivial as
1795 a garbage collection might change the answer by removing indirections.
1796 Still, no-one's forcing you to use it. If you're worried about little
1797 things like loss of referential transparency, you might like to wrap
1798 it all up in a monad-like thing as John O'Donnell and John Hughes did
1799 for non-determinism (1989 (Fraserburgh) Glasgow FP Workshop
1802 I'm thinking of using it to speed up a critical equality test in some
1803 graphics stuff in a context where the possibility of saying that
1804 denotationally equal things aren't isn't a problem (as long as it
1805 doesn't happen too often.) ADR
1807 To Will: Jim said this was already in, but I can't see it so I'm
1808 adding it. Up to you whether you add it. (Note that this could have
1809 been readily implemented using a @veryDangerousCCall@ before they were
1813 primOpInfo ReallyUnsafePtrEqualityOp
1814 = mkGenPrimOp SLIT("reallyUnsafePtrEquality#") [alphaTyVar]
1815 [alphaTy, alphaTy] intPrimTy
1818 %************************************************************************
1820 \subsubsection[PrimOp-parallel]{PrimOpInfo for parallelism op(s)}
1822 %************************************************************************
1825 primOpInfo SeqOp -- seq# :: a -> Int#
1826 = mkGenPrimOp SLIT("seq#") [alphaTyVar] [alphaTy] intPrimTy
1828 primOpInfo ParOp -- par# :: a -> Int#
1829 = mkGenPrimOp SLIT("par#") [alphaTyVar] [alphaTy] intPrimTy
1833 -- HWL: The first 4 Int# in all par... annotations denote:
1834 -- name, granularity info, size of result, degree of parallelism
1835 -- Same structure as _seq_ i.e. returns Int#
1836 -- KSW: v, the second arg in parAt# and parAtForNow#, is used only to determine
1837 -- `the processor containing the expression v'; it is not evaluated
1839 primOpInfo ParGlobalOp -- parGlobal# :: a -> Int# -> Int# -> Int# -> Int# -> b -> Int#
1840 = mkGenPrimOp SLIT("parGlobal#") [alphaTyVar,betaTyVar] [alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,betaTy] intPrimTy
1842 primOpInfo ParLocalOp -- parLocal# :: a -> Int# -> Int# -> Int# -> Int# -> b -> Int#
1843 = mkGenPrimOp SLIT("parLocal#") [alphaTyVar,betaTyVar] [alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,betaTy] intPrimTy
1845 primOpInfo ParAtOp -- parAt# :: a -> v -> Int# -> Int# -> Int# -> Int# -> b -> Int#
1846 = mkGenPrimOp SLIT("parAt#") [alphaTyVar,betaTyVar,gammaTyVar] [betaTy,alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,gammaTy] intPrimTy
1848 primOpInfo ParAtAbsOp -- parAtAbs# :: a -> Int# -> Int# -> Int# -> Int# -> Int# -> b -> Int#
1849 = mkGenPrimOp SLIT("parAtAbs#") [alphaTyVar,betaTyVar] [alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,betaTy] intPrimTy
1851 primOpInfo ParAtRelOp -- parAtRel# :: a -> Int# -> Int# -> Int# -> Int# -> Int# -> b -> Int#
1852 = mkGenPrimOp SLIT("parAtRel#") [alphaTyVar,betaTyVar] [alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,betaTy] intPrimTy
1854 primOpInfo ParAtForNowOp -- parAtForNow# :: a -> v -> Int# -> Int# -> Int# -> Int# -> b -> Int#
1855 = mkGenPrimOp SLIT("parAtForNow#") [alphaTyVar,betaTyVar,gammaTyVar] [betaTy,alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,gammaTy] intPrimTy
1857 primOpInfo CopyableOp -- copyable# :: a -> Int#
1858 = mkGenPrimOp SLIT("copyable#") [alphaTyVar] [alphaTy] intPrimTy
1860 primOpInfo NoFollowOp -- noFollow# :: a -> Int#
1861 = mkGenPrimOp SLIT("noFollow#") [alphaTyVar] [alphaTy] intPrimTy
1864 %************************************************************************
1866 \subsubsection[PrimOp-IO-etc]{PrimOpInfo for C calls, and I/O-ish things}
1868 %************************************************************************
1871 primOpInfo (CCallOp _ _ _ _)
1872 = mkGenPrimOp SLIT("ccall#") [alphaTyVar] [] alphaTy
1875 primOpInfo (CCallOp _ _ _ _ arg_tys result_ty)
1876 = mkGenPrimOp SLIT("ccall#") [] arg_tys result_tycon tys_applied
1878 (result_tycon, tys_applied, _) = splitAlgTyConApp result_ty
1882 %************************************************************************
1884 \subsubsection[PrimOp-tag]{PrimOpInfo for @dataToTag#@ and @tagToEnum#@}
1886 %************************************************************************
1888 These primops are pretty wierd.
1890 dataToTag# :: a -> Int (arg must be an evaluated data type)
1891 tagToEnum# :: Int -> a (result type must be an enumerated type)
1893 The constraints aren't currently checked by the front end, but the
1894 code generator will fall over if they aren't satisfied.
1897 primOpInfo DataToTagOp
1898 = mkGenPrimOp SLIT("dataToTag#") [alphaTyVar] [alphaTy] intPrimTy
1900 primOpInfo TagToEnumOp
1901 = mkGenPrimOp SLIT("tagToEnum#") [alphaTyVar] [intPrimTy] alphaTy
1904 primOpInfo op = panic ("primOpInfo:"++ show (I# (tagOf_PrimOp op)))
1908 %************************************************************************
1910 \subsubsection[PrimOp-ool]{Which PrimOps are out-of-line}
1912 %************************************************************************
1914 Some PrimOps need to be called out-of-line because they either need to
1915 perform a heap check or they block.
1928 NewByteArrayOp _ -> True
1929 IntegerAddOp -> True
1930 IntegerSubOp -> True
1931 IntegerMulOp -> True
1932 IntegerGcdOp -> True
1933 IntegerQuotRemOp -> True
1934 IntegerDivModOp -> True
1935 Int2IntegerOp -> True
1936 Word2IntegerOp -> True
1937 Addr2IntegerOp -> True
1938 Word64ToIntegerOp -> True
1939 Int64ToIntegerOp -> True
1940 FloatDecodeOp -> True
1941 DoubleDecodeOp -> True
1943 FinalizeWeakOp -> True
1944 MakeStableNameOp -> True
1945 MakeForeignObjOp -> True
1949 KillThreadOp -> True
1951 CCallOp _ _ may_gc@True _ -> True -- _ccall_GC_
1952 -- the next one doesn't perform any heap checks,
1953 -- but it is of such an esoteric nature that
1954 -- it is done out-of-line rather than require
1955 -- the NCG to implement it.
1956 UnsafeThawArrayOp -> True
1960 Sometimes we may choose to execute a PrimOp even though it isn't
1961 certain that its result will be required; ie execute them
1962 ``speculatively''. The same thing as ``cheap eagerness.'' Usually
1963 this is OK, because PrimOps are usually cheap, but it isn't OK for
1964 (a)~expensive PrimOps and (b)~PrimOps which can fail.
1966 See also @primOpIsCheap@ (below).
1968 PrimOps that have side effects also should not be executed speculatively
1969 or by data dependencies.
1972 primOpOkForSpeculation :: PrimOp -> Bool
1973 primOpOkForSpeculation op
1974 = not (primOpCanFail op || primOpHasSideEffects op || primOpOutOfLine op)
1977 @primOpIsCheap@, as used in \tr{SimplUtils.lhs}. For now (HACK
1978 WARNING), we just borrow some other predicates for a
1979 what-should-be-good-enough test. "Cheap" means willing to call it more
1980 than once. Evaluation order is unaffected.
1983 primOpIsCheap op = not (primOpHasSideEffects op || primOpOutOfLine op)
1987 primOpCanFail :: PrimOp -> Bool
1989 primOpCanFail IntQuotOp = True -- Divide by zero
1990 primOpCanFail IntRemOp = True -- Divide by zero
1993 primOpCanFail IntegerQuotRemOp = True -- Divide by zero
1994 primOpCanFail IntegerDivModOp = True -- Divide by zero
1996 -- Float. ToDo: tan? tanh?
1997 primOpCanFail FloatDivOp = True -- Divide by zero
1998 primOpCanFail FloatLogOp = True -- Log of zero
1999 primOpCanFail FloatAsinOp = True -- Arg out of domain
2000 primOpCanFail FloatAcosOp = True -- Arg out of domain
2002 -- Double. ToDo: tan? tanh?
2003 primOpCanFail DoubleDivOp = True -- Divide by zero
2004 primOpCanFail DoubleLogOp = True -- Log of zero
2005 primOpCanFail DoubleAsinOp = True -- Arg out of domain
2006 primOpCanFail DoubleAcosOp = True -- Arg out of domain
2008 primOpCanFail other_op = False
2011 And some primops have side-effects and so, for example, must not be
2015 primOpHasSideEffects :: PrimOp -> Bool
2017 primOpHasSideEffects TakeMVarOp = True
2018 primOpHasSideEffects DelayOp = True
2019 primOpHasSideEffects WaitReadOp = True
2020 primOpHasSideEffects WaitWriteOp = True
2022 primOpHasSideEffects ParOp = True
2023 primOpHasSideEffects ForkOp = True
2024 primOpHasSideEffects KillThreadOp = True
2025 primOpHasSideEffects YieldOp = True
2026 primOpHasSideEffects SeqOp = True
2028 primOpHasSideEffects MakeForeignObjOp = True
2029 primOpHasSideEffects WriteForeignObjOp = True
2030 primOpHasSideEffects MkWeakOp = True
2031 primOpHasSideEffects DeRefWeakOp = True
2032 primOpHasSideEffects FinalizeWeakOp = True
2033 primOpHasSideEffects MakeStablePtrOp = True
2034 primOpHasSideEffects MakeStableNameOp = True
2035 primOpHasSideEffects EqStablePtrOp = True -- SOF
2036 primOpHasSideEffects DeRefStablePtrOp = True -- ??? JSM & ADR
2038 primOpHasSideEffects ParGlobalOp = True
2039 primOpHasSideEffects ParLocalOp = True
2040 primOpHasSideEffects ParAtOp = True
2041 primOpHasSideEffects ParAtAbsOp = True
2042 primOpHasSideEffects ParAtRelOp = True
2043 primOpHasSideEffects ParAtForNowOp = True
2044 primOpHasSideEffects CopyableOp = True -- Possibly not. ASP
2045 primOpHasSideEffects NoFollowOp = True -- Possibly not. ASP
2048 primOpHasSideEffects (CCallOp _ _ _ _) = True
2050 primOpHasSideEffects other = False
2053 Inline primitive operations that perform calls need wrappers to save
2054 any live variables that are stored in caller-saves registers.
2057 primOpNeedsWrapper :: PrimOp -> Bool
2059 primOpNeedsWrapper (CCallOp _ _ _ _) = True
2061 primOpNeedsWrapper Integer2IntOp = True
2062 primOpNeedsWrapper Integer2WordOp = True
2063 primOpNeedsWrapper IntegerCmpOp = True
2064 primOpNeedsWrapper IntegerCmpIntOp = True
2066 primOpNeedsWrapper FloatExpOp = True
2067 primOpNeedsWrapper FloatLogOp = True
2068 primOpNeedsWrapper FloatSqrtOp = True
2069 primOpNeedsWrapper FloatSinOp = True
2070 primOpNeedsWrapper FloatCosOp = True
2071 primOpNeedsWrapper FloatTanOp = True
2072 primOpNeedsWrapper FloatAsinOp = True
2073 primOpNeedsWrapper FloatAcosOp = True
2074 primOpNeedsWrapper FloatAtanOp = True
2075 primOpNeedsWrapper FloatSinhOp = True
2076 primOpNeedsWrapper FloatCoshOp = True
2077 primOpNeedsWrapper FloatTanhOp = True
2078 primOpNeedsWrapper FloatPowerOp = True
2080 primOpNeedsWrapper DoubleExpOp = True
2081 primOpNeedsWrapper DoubleLogOp = True
2082 primOpNeedsWrapper DoubleSqrtOp = True
2083 primOpNeedsWrapper DoubleSinOp = True
2084 primOpNeedsWrapper DoubleCosOp = True
2085 primOpNeedsWrapper DoubleTanOp = True
2086 primOpNeedsWrapper DoubleAsinOp = True
2087 primOpNeedsWrapper DoubleAcosOp = True
2088 primOpNeedsWrapper DoubleAtanOp = True
2089 primOpNeedsWrapper DoubleSinhOp = True
2090 primOpNeedsWrapper DoubleCoshOp = True
2091 primOpNeedsWrapper DoubleTanhOp = True
2092 primOpNeedsWrapper DoublePowerOp = True
2094 primOpNeedsWrapper MakeStableNameOp = True
2095 primOpNeedsWrapper DeRefStablePtrOp = True
2097 primOpNeedsWrapper DelayOp = True
2098 primOpNeedsWrapper WaitReadOp = True
2099 primOpNeedsWrapper WaitWriteOp = True
2101 primOpNeedsWrapper other_op = False
2106 = case (primOpInfo op) of
2108 Monadic occ _ -> occ
2109 Compare occ _ -> occ
2110 GenPrimOp occ _ _ _ -> occ
2114 primOpUniq :: PrimOp -> Unique
2115 primOpUniq op = mkPrimOpIdUnique (IBOX(tagOf_PrimOp op))
2117 primOpType :: PrimOp -> Type -- you may want to use primOpSig instead
2119 = case (primOpInfo op) of
2120 Dyadic occ ty -> dyadic_fun_ty ty
2121 Monadic occ ty -> monadic_fun_ty ty
2122 Compare occ ty -> compare_fun_ty ty
2124 GenPrimOp occ tyvars arg_tys res_ty ->
2125 mkForAllTys tyvars (mkFunTys arg_tys res_ty)
2127 -- primOpSig is like primOpType but gives the result split apart:
2128 -- (type variables, argument types, result type)
2130 primOpSig :: PrimOp -> ([TyVar],[Type],Type)
2132 = case (primOpInfo op) of
2133 Monadic occ ty -> ([], [ty], ty )
2134 Dyadic occ ty -> ([], [ty,ty], ty )
2135 Compare occ ty -> ([], [ty,ty], boolTy)
2136 GenPrimOp occ tyvars arg_tys res_ty
2137 -> (tyvars, arg_tys, res_ty)
2139 -- primOpUsg is like primOpSig but the types it yields are the
2140 -- appropriate sigma (i.e., usage-annotated) types,
2141 -- as required by the UsageSP inference.
2143 primOpUsg :: PrimOp -> ([TyVar],[Type],Type)
2147 -- Refer to comment by `otherwise' clause; we need consider here
2148 -- *only* primops that have arguments or results containing Haskell
2149 -- pointers (things that are pointed). Unpointed values are
2150 -- irrelevant to the usage analysis. The issue is whether pointed
2151 -- values may be entered or duplicated by the primop.
2153 -- Remember that primops are *never* partially applied.
2155 NewArrayOp -> mangle [mkP, mkM, mkP ] mkM
2156 SameMutableArrayOp -> mangle [mkP, mkP ] mkM
2157 ReadArrayOp -> mangle [mkM, mkP, mkP ] mkM
2158 WriteArrayOp -> mangle [mkM, mkP, mkM, mkP] mkR
2159 IndexArrayOp -> mangle [mkM, mkP ] mkM
2160 UnsafeFreezeArrayOp -> mangle [mkM, mkP ] mkM
2161 UnsafeThawArrayOp -> mangle [mkM, mkP ] mkM
2163 NewMutVarOp -> mangle [mkM, mkP ] mkM
2164 ReadMutVarOp -> mangle [mkM, mkP ] mkM
2165 WriteMutVarOp -> mangle [mkM, mkM, mkP ] mkR
2166 SameMutVarOp -> mangle [mkP, mkP ] mkM
2168 CatchOp -> -- [mkO, mkO . (inFun mkM mkO)] mkO
2169 mangle [mkM, mkM . (inFun mkM mkM)] mkM
2170 -- might use caught action multiply
2171 RaiseOp -> mangle [mkM ] mkM
2173 NewMVarOp -> mangle [mkP ] mkR
2174 TakeMVarOp -> mangle [mkM, mkP ] mkM
2175 PutMVarOp -> mangle [mkM, mkM, mkP ] mkR
2176 SameMVarOp -> mangle [mkP, mkP ] mkM
2177 IsEmptyMVarOp -> mangle [mkP, mkP ] mkM
2179 ForkOp -> mangle [mkO, mkP ] mkR
2180 KillThreadOp -> mangle [mkP, mkM, mkP ] mkR
2182 MkWeakOp -> mangle [mkZ, mkM, mkM, mkP] mkM
2183 DeRefWeakOp -> mangle [mkM, mkP ] mkM
2184 FinalizeWeakOp -> mangle [mkM, mkP ] (mkR . (inUB [id,id,inFun mkR mkM]))
2186 MakeStablePtrOp -> mangle [mkM, mkP ] mkM
2187 DeRefStablePtrOp -> mangle [mkM, mkP ] mkM
2188 EqStablePtrOp -> mangle [mkP, mkP ] mkR
2189 MakeStableNameOp -> mangle [mkZ, mkP ] mkR
2190 EqStableNameOp -> mangle [mkP, mkP ] mkR
2191 StableNameToIntOp -> mangle [mkP ] mkR
2193 ReallyUnsafePtrEqualityOp -> mangle [mkZ, mkZ ] mkR
2195 SeqOp -> mangle [mkO ] mkR
2196 ParOp -> mangle [mkO ] mkR
2197 ParGlobalOp -> mangle [mkO, mkP, mkP, mkP, mkP, mkM] mkM
2198 ParLocalOp -> mangle [mkO, mkP, mkP, mkP, mkP, mkM] mkM
2199 ParAtOp -> mangle [mkO, mkZ, mkP, mkP, mkP, mkP, mkM] mkM
2200 ParAtAbsOp -> mangle [mkO, mkP, mkP, mkP, mkP, mkM] mkM
2201 ParAtRelOp -> mangle [mkO, mkP, mkP, mkP, mkP, mkM] mkM
2202 ParAtForNowOp -> mangle [mkO, mkZ, mkP, mkP, mkP, mkP, mkM] mkM
2203 CopyableOp -> mangle [mkZ ] mkR
2204 NoFollowOp -> mangle [mkZ ] mkR
2206 CCallOp _ _ _ _ -> mangle [ ] mkM
2208 -- Things with no Haskell pointers inside: in actuality, usages are
2209 -- irrelevant here (hence it doesn't matter that some of these
2210 -- apparently permit duplication; since such arguments are never
2211 -- ENTERed anyway, the usage annotation they get is entirely irrelevant
2212 -- except insofar as it propagates to infect other values that *are*
2215 otherwise -> nomangle
2217 where mkZ = mkUsgTy UsOnce -- pointed argument used zero
2218 mkO = mkUsgTy UsOnce -- pointed argument used once
2219 mkM = mkUsgTy UsMany -- pointed argument used multiply
2220 mkP = mkUsgTy UsOnce -- unpointed argument
2221 mkR = mkUsgTy UsMany -- unpointed result
2223 (tyvars, arg_tys, res_ty)
2226 nomangle = (tyvars, map mkP arg_tys, mkR res_ty)
2228 mangle fs g = (tyvars, zipWithEqual "primOpUsg" ($) fs arg_tys, g res_ty)
2230 inFun f g ty = case splitFunTy_maybe ty of
2231 Just (a,b) -> mkFunTy (f a) (g b)
2232 Nothing -> pprPanic "primOpUsg:inFun" (ppr op <+> ppr ty)
2234 inUB fs ty = case splitTyConApp_maybe ty of
2235 Just (tc,tys) -> ASSERT( tc == unboxedTupleTyCon (length fs) )
2236 mkUnboxedTupleTy (length fs) (zipWithEqual "primOpUsg"
2238 Nothing -> pprPanic "primOpUsg:inUB" (ppr op <+> ppr ty)
2242 data PrimOpResultInfo
2243 = ReturnsPrim PrimRep
2246 -- Some PrimOps need not return a manifest primitive or algebraic value
2247 -- (i.e. they might return a polymorphic value). These PrimOps *must*
2248 -- be out of line, or the code generator won't work.
2250 getPrimOpResultInfo :: PrimOp -> PrimOpResultInfo
2251 getPrimOpResultInfo op
2252 = case (primOpInfo op) of
2253 Dyadic _ ty -> ReturnsPrim (typePrimRep ty)
2254 Monadic _ ty -> ReturnsPrim (typePrimRep ty)
2255 Compare _ ty -> ReturnsAlg boolTyCon
2256 GenPrimOp _ _ _ ty ->
2257 let rep = typePrimRep ty in
2259 PtrRep -> case splitAlgTyConApp_maybe ty of
2260 Nothing -> panic "getPrimOpResultInfo"
2261 Just (tc,_,_) -> ReturnsAlg tc
2262 other -> ReturnsPrim other
2264 isCompareOp :: PrimOp -> Bool
2266 = case primOpInfo op of
2271 The commutable ops are those for which we will try to move constants
2272 to the right hand side for strength reduction.
2275 commutableOp :: PrimOp -> Bool
2277 commutableOp CharEqOp = True
2278 commutableOp CharNeOp = True
2279 commutableOp IntAddOp = True
2280 commutableOp IntMulOp = True
2281 commutableOp AndOp = True
2282 commutableOp OrOp = True
2283 commutableOp XorOp = True
2284 commutableOp IntEqOp = True
2285 commutableOp IntNeOp = True
2286 commutableOp IntegerAddOp = True
2287 commutableOp IntegerMulOp = True
2288 commutableOp IntegerGcdOp = True
2289 commutableOp FloatAddOp = True
2290 commutableOp FloatMulOp = True
2291 commutableOp FloatEqOp = True
2292 commutableOp FloatNeOp = True
2293 commutableOp DoubleAddOp = True
2294 commutableOp DoubleMulOp = True
2295 commutableOp DoubleEqOp = True
2296 commutableOp DoubleNeOp = True
2297 commutableOp _ = False
2302 mkPrimTyApp :: [TyVar] -> PrimRep -> ([TyVar], Type)
2303 -- CharRep --> ([], Char#)
2304 -- StablePtrRep --> ([a], StablePtr# a)
2305 mkPrimTyApp tvs kind
2306 = (forall_tvs, mkTyConApp tycon (mkTyVarTys forall_tvs))
2308 tycon = primRepTyCon kind
2309 forall_tvs = take (tyConArity tycon) tvs
2311 dyadic_fun_ty ty = mkFunTys [ty, ty] ty
2312 monadic_fun_ty ty = mkFunTy ty ty
2313 compare_fun_ty ty = mkFunTys [ty, ty] boolTy
2318 pprPrimOp :: PrimOp -> SDoc
2320 pprPrimOp (CCallOp fun is_casm may_gc cconv)
2322 callconv = text "{-" <> pprCallConv cconv <> text "-}"
2325 | is_casm && may_gc = "casm_GC ``"
2326 | is_casm = "casm ``"
2327 | may_gc = "ccall_GC "
2328 | otherwise = "ccall "
2331 | is_casm = text "''"
2336 Right _ -> text "dyn_"
2341 Right _ -> text "\"\""
2345 hcat [ ifPprDebug callconv
2346 , text "__", ppr_dyn
2347 , text before , ppr_fun , after]
2350 = getPprStyle $ \ sty ->
2351 if ifaceStyle sty then -- For interfaces Print it qualified with PrelGHC.
2352 ptext SLIT("PrelGHC.") <> pprOccName occ
2356 occ = primOpOcc other_op