[project @ 1999-04-23 13:53:28 by simonm]
[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         tagOf_PrimOp, -- ToDo: rm
10         primOpType,
11         primOpUniq, primOpOcc,
12
13         commutableOp,
14
15         primOpOutOfLine, primOpNeedsWrapper, primOpStrictness,
16         primOpOkForSpeculation, primOpIsCheap,
17         primOpHasSideEffects,
18
19         getPrimOpResultInfo,  PrimOpResultInfo(..),
20
21         pprPrimOp
22     ) where
23
24 #include "HsVersions.h"
25
26 import PrimRep          -- most of it
27 import TysPrim
28 import TysWiredIn
29
30 import Demand           ( Demand, wwLazy, wwPrim, wwStrict )
31 import Var              ( TyVar )
32 import CallConv         ( CallConv, pprCallConv )
33 import PprType          ( pprParendType )
34 import OccName          ( OccName, pprOccName, mkSrcVarOcc )
35 import TyCon            ( TyCon, tyConArity )
36 import Type             ( mkForAllTys, mkForAllTy, mkFunTy, mkFunTys, mkTyVarTys,
37                           mkTyConTy, mkTyConApp, typePrimRep,
38                           splitAlgTyConApp, Type, isUnboxedTupleType, 
39                           splitAlgTyConApp_maybe
40                         )
41 import Unique           ( Unique, mkPrimOpIdUnique )
42 import Outputable
43 import Util             ( assoc )
44 import GlaExts          ( Int(..), Int#, (==#) )
45 \end{code}
46
47 %************************************************************************
48 %*                                                                      *
49 \subsection[PrimOp-datatype]{Datatype for @PrimOp@ (an enumeration)}
50 %*                                                                      *
51 %************************************************************************
52
53 These are in \tr{state-interface.verb} order.
54
55 \begin{code}
56 data PrimOp
57     -- dig the FORTRAN/C influence on the names...
58
59     -- comparisons:
60
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
67
68     -- Char#-related ops:
69     | OrdOp | ChrOp
70
71     -- Int#-related ops:
72    -- IntAbsOp unused?? ADR
73     | IntAddOp | IntSubOp | IntMulOp | IntQuotOp
74     | IntRemOp | IntNegOp | IntAbsOp
75     | ISllOp | ISraOp | ISrlOp -- shift {left,right} {arithmetic,logical}
76     | IntAddCOp
77     | IntSubCOp
78     | IntMulCOp
79
80     -- Word#-related ops:
81     | WordQuotOp | WordRemOp
82     | AndOp  | OrOp   | NotOp | XorOp
83     | SllOp  | SrlOp  -- shift {left,right} {logical}
84     | Int2WordOp | Word2IntOp -- casts
85
86     -- Addr#-related ops:
87     | Int2AddrOp | Addr2IntOp -- casts
88
89     -- Float#-related ops:
90     | FloatAddOp | FloatSubOp | FloatMulOp | FloatDivOp | FloatNegOp
91     | Float2IntOp | Int2FloatOp
92
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
100
101     -- Double#-related ops:
102     | DoubleAddOp | DoubleSubOp | DoubleMulOp | DoubleDivOp | DoubleNegOp
103     | Double2IntOp | Int2DoubleOp
104     | Double2FloatOp | Float2DoubleOp
105
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
113
114     -- Integer (and related...) ops:
115     -- slightly weird -- to match GMP package.
116     | IntegerAddOp | IntegerSubOp | IntegerMulOp | IntegerGcdOp
117     | IntegerQuotRemOp | IntegerDivModOp | IntegerNegOp
118
119     | IntegerCmpOp
120     | IntegerCmpIntOp
121
122     | Integer2IntOp  | Integer2WordOp  
123     | Int2IntegerOp  | Word2IntegerOp
124     | Addr2IntegerOp
125      -- casting to/from Integer and 64-bit (un)signed quantities.
126     | IntegerToInt64Op | Int64ToIntegerOp
127     | IntegerToWord64Op | Word64ToIntegerOp
128     -- ?? gcd, etc?
129
130     | FloatDecodeOp
131     | DoubleDecodeOp
132
133     -- primitive ops for primitive arrays
134
135     | NewArrayOp
136     | NewByteArrayOp PrimRep
137
138     | SameMutableArrayOp
139     | SameMutableByteArrayOp
140
141     | ReadArrayOp | WriteArrayOp | IndexArrayOp -- for arrays of Haskell ptrs
142
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
153
154     | UnsafeFreezeArrayOp | UnsafeFreezeByteArrayOp
155     | UnsafeThawArrayOp   | UnsafeThawByteArrayOp
156     | SizeofByteArrayOp   | SizeofMutableByteArrayOp
157
158     -- Mutable variables
159     | NewMutVarOp
160     | ReadMutVarOp
161     | WriteMutVarOp
162     | SameMutVarOp
163
164     -- for MVars
165     | NewMVarOp
166     | TakeMVarOp 
167     | PutMVarOp
168     | SameMVarOp
169     | IsEmptyMVarOp
170
171     -- exceptions
172     | CatchOp
173     | RaiseOp
174
175     -- foreign objects
176     | MakeForeignObjOp
177     | WriteForeignObjOp
178
179     -- weak pointers
180     | MkWeakOp
181     | DeRefWeakOp
182     | FinalizeWeakOp
183
184     -- stable names
185     | MakeStableNameOp
186     | EqStableNameOp
187     | StableNameToIntOp
188
189     -- stable pointers
190     | MakeStablePtrOp
191     | DeRefStablePtrOp
192     | EqStablePtrOp
193 \end{code}
194
195 A special ``trap-door'' to use in making calls direct to C functions:
196 \begin{code}
197     | CCallOp   (Either 
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.)
203                                     
204                 Bool                -- True <=> really a "casm"
205                 Bool                -- True <=> might invoke Haskell GC
206                 CallConv            -- calling convention to use.
207
208     -- (... to be continued ... )
209 \end{code}
210
211 The ``type'' of @CCallOp foo [t1, ... tm] r@ is @t1 -> ... tm -> r@.
212 (See @primOpInfo@ for details.)
213
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.
217
218 For example, we represent
219 \begin{pseudocode}
220 ((ccall# foo [StablePtr# a, Int] Float) sp# i#) :: (Float, IoWorld)
221 \end{pseudocode}
222 by
223 \begin{pseudocode}
224 Case
225   ( Prim
226       (CCallOp "foo" [Universe#, StablePtr# a, Int#] FloatPrimAndUniverse False)
227        -- :: Universe# -> StablePtr# a -> Int# -> FloatPrimAndUniverse
228       []
229       [w#, sp# i#]
230   )
231   (AlgAlts [ ( FloatPrimAndIoWorld,
232                  [f#, w#],
233                  Con (TupleCon 2) [Float, IoWorld] [F# f#, World w#]
234                ) ]
235              NoDefault
236   )
237 \end{pseudocode}
238
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
241 \begin{pseudocode}
242 Case
243   ( Prim
244       (CCallOp "foo" [alpha1, alpha2, alpha3] alpha4 False)
245        -- :: /\ alpha1, alpha2 alpha3, alpha4.
246        --       alpha1 -> alpha2 -> alpha3 -> alpha4
247       [Universe#, StablePtr# a, Int#, FloatPrimAndIoWorld]
248       [w#, sp# i#]
249   )
250   (AlgAlts [ ( FloatPrimAndIoWorld,
251                  [f#, w#],
252                  Con (TupleCon 2) [Float, IoWorld] [F# f#, World w#]
253                ) ]
254              NoDefault
255   )
256 \end{pseudocode}
257
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)
266
267 \begin{code}
268     -- (... continued from above ... )
269
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
273
274     -- parallel stuff
275     | SeqOp
276     | ParOp
277
278     -- concurrency
279     | ForkOp
280     | KillThreadOp
281     | YieldOp
282     | MyThreadIdOp
283     | DelayOp
284     | WaitReadOp
285     | WaitWriteOp
286
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
296
297     -- tag-related
298     | DataToTagOp
299     | TagToEnumOp
300 \end{code}
301
302 Used for the Ord instance
303
304 \begin{code}
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)
368
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)
389
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)
412
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)
433
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)
442
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)
448
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)
458
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)
468
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)
478
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)
488
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)
498
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)
509
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)
516
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)
560
561 tagOf_PrimOp op = pprPanic# "tagOf_PrimOp: pattern-match" (ppr op)
562 --panic# "tagOf_PrimOp: pattern-match"
563
564 instance Eq PrimOp where
565     op1 == op2 = tagOf_PrimOp op1 _EQ_ tagOf_PrimOp op2
566
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
573                       | op1 == op2 = EQ
574                       | otherwise  = GT
575
576 instance Outputable PrimOp where
577     ppr op = pprPrimOp op
578
579 instance Show PrimOp where
580     showsPrec p op = showsPrecSDoc p (pprPrimOp op)
581 \end{code}
582
583 An @Enum@-derived list would be better; meanwhile... (ToDo)
584 \begin{code}
585 allThePrimOps
586   = [   CharGtOp,
587         CharGeOp,
588         CharEqOp,
589         CharNeOp,
590         CharLtOp,
591         CharLeOp,
592         IntGtOp,
593         IntGeOp,
594         IntEqOp,
595         IntNeOp,
596         IntLtOp,
597         IntLeOp,
598         WordGtOp,
599         WordGeOp,
600         WordEqOp,
601         WordNeOp,
602         WordLtOp,
603         WordLeOp,
604         AddrGtOp,
605         AddrGeOp,
606         AddrEqOp,
607         AddrNeOp,
608         AddrLtOp,
609         AddrLeOp,
610         FloatGtOp,
611         FloatGeOp,
612         FloatEqOp,
613         FloatNeOp,
614         FloatLtOp,
615         FloatLeOp,
616         DoubleGtOp,
617         DoubleGeOp,
618         DoubleEqOp,
619         DoubleNeOp,
620         DoubleLtOp,
621         DoubleLeOp,
622         OrdOp,
623         ChrOp,
624         IntAddOp,
625         IntSubOp,
626         IntMulOp,
627         IntQuotOp,
628         IntRemOp,
629         IntNegOp,
630         WordQuotOp,
631         WordRemOp,
632         AndOp,
633         OrOp,
634         NotOp,
635         XorOp,
636         SllOp,
637         SrlOp,
638         ISllOp,
639         ISraOp,
640         ISrlOp,
641         IntAddCOp,
642         IntSubCOp,
643         IntMulCOp,
644         Int2WordOp,
645         Word2IntOp,
646         Int2AddrOp,
647         Addr2IntOp,
648
649         FloatAddOp,
650         FloatSubOp,
651         FloatMulOp,
652         FloatDivOp,
653         FloatNegOp,
654         Float2IntOp,
655         Int2FloatOp,
656         FloatExpOp,
657         FloatLogOp,
658         FloatSqrtOp,
659         FloatSinOp,
660         FloatCosOp,
661         FloatTanOp,
662         FloatAsinOp,
663         FloatAcosOp,
664         FloatAtanOp,
665         FloatSinhOp,
666         FloatCoshOp,
667         FloatTanhOp,
668         FloatPowerOp,
669         DoubleAddOp,
670         DoubleSubOp,
671         DoubleMulOp,
672         DoubleDivOp,
673         DoubleNegOp,
674         Double2IntOp,
675         Int2DoubleOp,
676         Double2FloatOp,
677         Float2DoubleOp,
678         DoubleExpOp,
679         DoubleLogOp,
680         DoubleSqrtOp,
681         DoubleSinOp,
682         DoubleCosOp,
683         DoubleTanOp,
684         DoubleAsinOp,
685         DoubleAcosOp,
686         DoubleAtanOp,
687         DoubleSinhOp,
688         DoubleCoshOp,
689         DoubleTanhOp,
690         DoublePowerOp,
691         IntegerAddOp,
692         IntegerSubOp,
693         IntegerMulOp,
694         IntegerGcdOp,
695         IntegerQuotRemOp,
696         IntegerDivModOp,
697         IntegerNegOp,
698         IntegerCmpOp,
699         IntegerCmpIntOp,
700         Integer2IntOp,
701         Integer2WordOp,
702         Int2IntegerOp,
703         Word2IntegerOp,
704         Addr2IntegerOp,
705         IntegerToInt64Op,
706         Int64ToIntegerOp,
707         IntegerToWord64Op,
708         Word64ToIntegerOp,
709         FloatDecodeOp,
710         DoubleDecodeOp,
711         NewArrayOp,
712         NewByteArrayOp CharRep,
713         NewByteArrayOp IntRep,
714         NewByteArrayOp WordRep,
715         NewByteArrayOp AddrRep,
716         NewByteArrayOp FloatRep,
717         NewByteArrayOp DoubleRep,
718         NewByteArrayOp StablePtrRep,
719         SameMutableArrayOp,
720         SameMutableByteArrayOp,
721         ReadArrayOp,
722         WriteArrayOp,
723         IndexArrayOp,
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,
779         UnsafeFreezeArrayOp,
780         UnsafeFreezeByteArrayOp,
781         UnsafeThawArrayOp,
782         UnsafeThawByteArrayOp,
783         SizeofByteArrayOp,
784         SizeofMutableByteArrayOp,
785         NewMutVarOp,
786         ReadMutVarOp,
787         WriteMutVarOp,
788         SameMutVarOp,
789         CatchOp,
790         RaiseOp,
791         NewMVarOp,
792         TakeMVarOp,
793         PutMVarOp,
794         SameMVarOp,
795         IsEmptyMVarOp,
796         MakeForeignObjOp,
797         WriteForeignObjOp,
798         MkWeakOp,
799         DeRefWeakOp,
800         FinalizeWeakOp,
801         MakeStableNameOp,
802         EqStableNameOp,
803         StableNameToIntOp,
804         MakeStablePtrOp,
805         DeRefStablePtrOp,
806         EqStablePtrOp,
807         ReallyUnsafePtrEqualityOp,
808         ParGlobalOp,
809         ParLocalOp,
810         ParAtOp,
811         ParAtAbsOp,
812         ParAtRelOp,
813         ParAtForNowOp,
814         CopyableOp,
815         NoFollowOp,
816         SeqOp,
817         ParOp,
818         ForkOp,
819         KillThreadOp,
820         YieldOp,
821         MyThreadIdOp,
822         DelayOp,
823         WaitReadOp,
824         WaitWriteOp,
825         DataToTagOp,
826         TagToEnumOp
827     ]
828 \end{code}
829
830 %************************************************************************
831 %*                                                                      *
832 \subsection[PrimOp-info]{The essential info about each @PrimOp@}
833 %*                                                                      *
834 %************************************************************************
835
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.
839
840 The reason for the funny characters in the names is so we do not
841 interfere with the programmer's Haskell name spaces.
842
843 We use @PrimKinds@ for the ``type'' information, because they're
844 (slightly) more convenient to use than @TyCons@.
845 \begin{code}
846 data PrimOpInfo
847   = Dyadic      OccName         -- string :: T -> T -> T
848                 Type
849   | Monadic     OccName         -- string :: T -> T
850                 Type
851   | Compare     OccName         -- string :: T -> T -> Bool
852                 Type
853
854   | GenPrimOp   OccName         -- string :: \/a1..an . T1 -> .. -> Tk -> T
855                 [TyVar] 
856                 [Type] 
857                 Type 
858
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
863 \end{code}
864
865 Utility bits:
866 \begin{code}
867 one_Integer_ty = [intPrimTy, byteArrayPrimTy]
868 two_Integer_tys
869   = [intPrimTy, byteArrayPrimTy, -- first Integer pieces
870      intPrimTy, byteArrayPrimTy] -- second '' pieces
871 an_Integer_and_Int_tys
872   = [intPrimTy, byteArrayPrimTy, -- Integer
873      intPrimTy]
874
875 unboxedPair      = mkUnboxedTupleTy 2
876 unboxedTriple    = mkUnboxedTupleTy 3
877 unboxedQuadruple = mkUnboxedTupleTy 4
878
879 integerMonadic name = mkGenPrimOp name [] one_Integer_ty 
880                         (unboxedPair one_Integer_ty)
881
882 integerDyadic name = mkGenPrimOp name [] two_Integer_tys 
883                         (unboxedPair one_Integer_ty)
884
885 integerDyadic2Results name = mkGenPrimOp name [] two_Integer_tys 
886     (unboxedQuadruple two_Integer_tys)
887
888 integerCompare name = mkGenPrimOp name [] two_Integer_tys intPrimTy
889 \end{code}
890
891 %************************************************************************
892 %*                                                                      *
893 \subsubsection{Strictness}
894 %*                                                                      *
895 %************************************************************************
896
897 Not all primops are strict!
898
899 \begin{code}
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.
905
906 primOpStrictness SeqOp            = ([wwLazy], False)
907 primOpStrictness ParOp            = ([wwLazy], False)
908 primOpStrictness ForkOp           = ([wwLazy, wwPrim], False)
909
910 primOpStrictness NewArrayOp       = ([wwPrim, wwLazy, wwPrim], False)
911 primOpStrictness WriteArrayOp     = ([wwPrim, wwPrim, wwLazy, wwPrim], False)
912
913 primOpStrictness NewMutVarOp      = ([wwLazy, wwPrim], False)
914 primOpStrictness WriteMutVarOp    = ([wwPrim, wwLazy, wwPrim], False)
915
916 primOpStrictness PutMVarOp        = ([wwPrim, wwLazy, wwPrim], False)
917
918 primOpStrictness CatchOp          = ([wwLazy, wwLazy], False)
919 primOpStrictness RaiseOp          = ([wwLazy], True)    -- NB: True => result is bottom
920
921 primOpStrictness MkWeakOp         = ([wwLazy, wwLazy, wwLazy, wwPrim], False)
922 primOpStrictness MakeStableNameOp = ([wwLazy, wwPrim], False)
923 primOpStrictness MakeStablePtrOp  = ([wwLazy, wwPrim], False)
924
925 primOpStrictness DataToTagOp      = ([wwLazy], False)
926
927         -- The rest all have primitive-typed arguments
928 primOpStrictness other            = (repeat wwPrim, False)
929 \end{code}
930
931 %************************************************************************
932 %*                                                                      *
933 \subsubsection[PrimOp-comparison]{PrimOpInfo basic comparison ops}
934 %*                                                                      *
935 %************************************************************************
936
937 @primOpInfo@ gives all essential information (from which everything
938 else, notably a type, can be constructed) for each @PrimOp@.
939
940 \begin{code}
941 primOpInfo :: PrimOp -> PrimOpInfo
942 \end{code}
943
944 There's plenty of this stuff!
945
946 \begin{code}
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
953
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
960
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
967
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
974
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
981
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
988
989 \end{code}
990
991 %************************************************************************
992 %*                                                                      *
993 \subsubsection[PrimOp-Char]{PrimOpInfo for @Char#@s}
994 %*                                                                      *
995 %************************************************************************
996
997 \begin{code}
998 primOpInfo OrdOp = mkGenPrimOp SLIT("ord#") [] [charPrimTy] intPrimTy
999 primOpInfo ChrOp = mkGenPrimOp SLIT("chr#") [] [intPrimTy]  charPrimTy
1000 \end{code}
1001
1002 %************************************************************************
1003 %*                                                                      *
1004 \subsubsection[PrimOp-Int]{PrimOpInfo for @Int#@s}
1005 %*                                                                      *
1006 %************************************************************************
1007
1008 \begin{code}
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
1014
1015 primOpInfo IntNegOp  = mkMonadic SLIT("negateInt#") intPrimTy
1016 primOpInfo IntAbsOp  = mkMonadic SLIT("absInt#") intPrimTy
1017
1018 primOpInfo IntAddCOp = 
1019         mkGenPrimOp SLIT("addIntC#")  [] [intPrimTy, intPrimTy] 
1020                 (unboxedPair [intPrimTy, intPrimTy])
1021
1022 primOpInfo IntSubCOp = 
1023         mkGenPrimOp SLIT("subIntC#")  [] [intPrimTy, intPrimTy] 
1024                 (unboxedPair [intPrimTy, intPrimTy])
1025
1026 primOpInfo IntMulCOp = 
1027         mkGenPrimOp SLIT("mulIntC#")  [] [intPrimTy, intPrimTy] 
1028                 (unboxedPair [intPrimTy, intPrimTy])
1029 \end{code}
1030
1031 %************************************************************************
1032 %*                                                                      *
1033 \subsubsection[PrimOp-Word]{PrimOpInfo for @Word#@s}
1034 %*                                                                      *
1035 %************************************************************************
1036
1037 A @Word#@ is an unsigned @Int#@.
1038
1039 \begin{code}
1040 primOpInfo WordQuotOp = mkDyadic SLIT("quotWord#") wordPrimTy
1041 primOpInfo WordRemOp  = mkDyadic SLIT("remWord#")        wordPrimTy
1042
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
1047
1048 primOpInfo SllOp
1049   = mkGenPrimOp SLIT("shiftL#")  [] [wordPrimTy, intPrimTy] wordPrimTy
1050 primOpInfo SrlOp
1051   = mkGenPrimOp SLIT("shiftRL#") [] [wordPrimTy, intPrimTy] wordPrimTy
1052
1053 primOpInfo ISllOp
1054   = mkGenPrimOp SLIT("iShiftL#")  [] [intPrimTy, intPrimTy] intPrimTy
1055 primOpInfo ISraOp
1056   = mkGenPrimOp SLIT("iShiftRA#") [] [intPrimTy, intPrimTy] intPrimTy
1057 primOpInfo ISrlOp
1058   = mkGenPrimOp SLIT("iShiftRL#") [] [intPrimTy, intPrimTy] intPrimTy
1059
1060 primOpInfo Int2WordOp = mkGenPrimOp SLIT("int2Word#") [] [intPrimTy] wordPrimTy
1061 primOpInfo Word2IntOp = mkGenPrimOp SLIT("word2Int#") [] [wordPrimTy] intPrimTy
1062 \end{code}
1063
1064 %************************************************************************
1065 %*                                                                      *
1066 \subsubsection[PrimOp-Addr]{PrimOpInfo for @Addr#@s}
1067 %*                                                                      *
1068 %************************************************************************
1069
1070 \begin{code}
1071 primOpInfo Int2AddrOp = mkGenPrimOp SLIT("int2Addr#") [] [intPrimTy] addrPrimTy
1072 primOpInfo Addr2IntOp = mkGenPrimOp SLIT("addr2Int#") [] [addrPrimTy] intPrimTy
1073 \end{code}
1074
1075
1076 %************************************************************************
1077 %*                                                                      *
1078 \subsubsection[PrimOp-Float]{PrimOpInfo for @Float#@s}
1079 %*                                                                      *
1080 %************************************************************************
1081
1082 @decodeFloat#@ is given w/ Integer-stuff (it's similar).
1083
1084 \begin{code}
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
1090
1091 primOpInfo Float2IntOp  = mkGenPrimOp SLIT("float2Int#") [] [floatPrimTy] intPrimTy
1092 primOpInfo Int2FloatOp  = mkGenPrimOp SLIT("int2Float#") [] [intPrimTy] floatPrimTy
1093
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
1107 \end{code}
1108
1109 %************************************************************************
1110 %*                                                                      *
1111 \subsubsection[PrimOp-Double]{PrimOpInfo for @Double#@s}
1112 %*                                                                      *
1113 %************************************************************************
1114
1115 @decodeDouble#@ is given w/ Integer-stuff (it's similar).
1116
1117 \begin{code}
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
1123
1124 primOpInfo Double2IntOp     = mkGenPrimOp SLIT("double2Int#") [] [doublePrimTy] intPrimTy
1125 primOpInfo Int2DoubleOp     = mkGenPrimOp SLIT("int2Double#") [] [intPrimTy] doublePrimTy
1126
1127 primOpInfo Double2FloatOp   = mkGenPrimOp SLIT("double2Float#") [] [doublePrimTy] floatPrimTy
1128 primOpInfo Float2DoubleOp   = mkGenPrimOp SLIT("float2Double#") [] [floatPrimTy] doublePrimTy
1129
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
1143 \end{code}
1144
1145 %************************************************************************
1146 %*                                                                      *
1147 \subsubsection[PrimOp-Integer]{PrimOpInfo for @Integer@ (and related!)}
1148 %*                                                                      *
1149 %************************************************************************
1150
1151 \begin{code}
1152 primOpInfo IntegerNegOp = integerMonadic SLIT("negateInteger#")
1153
1154 primOpInfo IntegerAddOp = integerDyadic SLIT("plusInteger#")
1155 primOpInfo IntegerSubOp = integerDyadic SLIT("minusInteger#")
1156 primOpInfo IntegerMulOp = integerDyadic SLIT("timesInteger#")
1157 primOpInfo IntegerGcdOp = integerDyadic SLIT("gcdInteger#")
1158
1159 primOpInfo IntegerCmpOp = integerCompare SLIT("cmpInteger#")
1160 primOpInfo IntegerCmpIntOp 
1161   = mkGenPrimOp SLIT("cmpIntegerInt#") [] an_Integer_and_Int_tys intPrimTy
1162
1163 primOpInfo IntegerQuotRemOp = integerDyadic2Results SLIT("quotRemInteger#")
1164 primOpInfo IntegerDivModOp  = integerDyadic2Results SLIT("divModInteger#")
1165
1166 primOpInfo Integer2IntOp
1167   = mkGenPrimOp SLIT("integer2Int#") [] one_Integer_ty intPrimTy
1168
1169 primOpInfo Integer2WordOp
1170   = mkGenPrimOp SLIT("integer2Word#") [] one_Integer_ty wordPrimTy
1171
1172 primOpInfo Int2IntegerOp
1173   = mkGenPrimOp SLIT("int2Integer#") [] [intPrimTy] 
1174         (unboxedPair one_Integer_ty)
1175
1176 primOpInfo Word2IntegerOp
1177   = mkGenPrimOp SLIT("word2Integer#") [] [wordPrimTy] 
1178         (unboxedPair one_Integer_ty)
1179
1180 primOpInfo Addr2IntegerOp
1181   = mkGenPrimOp SLIT("addr2Integer#") [] [addrPrimTy] 
1182         (unboxedPair one_Integer_ty)
1183
1184 primOpInfo IntegerToInt64Op
1185   = mkGenPrimOp SLIT("integerToInt64#") [] one_Integer_ty int64PrimTy
1186
1187 primOpInfo Int64ToIntegerOp
1188   = mkGenPrimOp SLIT("int64ToInteger#") [] [int64PrimTy]
1189         (unboxedPair one_Integer_ty)
1190
1191 primOpInfo Word64ToIntegerOp
1192   = mkGenPrimOp SLIT("word64ToInteger#") [] [word64PrimTy] 
1193         (unboxedPair one_Integer_ty)
1194
1195 primOpInfo IntegerToWord64Op
1196   = mkGenPrimOp SLIT("integerToWord64#") [] one_Integer_ty word64PrimTy
1197 \end{code}
1198
1199 Decoding of floating-point numbers is sorta Integer-related.  Encoding
1200 is done with plain ccalls now (see PrelNumExtra.lhs).
1201
1202 \begin{code}
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])
1209 \end{code}
1210
1211 %************************************************************************
1212 %*                                                                      *
1213 \subsubsection[PrimOp-Arrays]{PrimOpInfo for primitive arrays}
1214 %*                                                                      *
1215 %************************************************************************
1216
1217 \begin{code}
1218 primOpInfo NewArrayOp
1219   = let {
1220         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1221         state = mkStatePrimTy s
1222     } in
1223     mkGenPrimOp SLIT("newArray#") [s_tv, elt_tv] 
1224         [intPrimTy, elt, state]
1225         (unboxedPair [state, mkMutableArrayPrimTy s elt])
1226
1227 primOpInfo (NewByteArrayOp kind)
1228   = let
1229         s = alphaTy; s_tv = alphaTyVar
1230
1231         op_str         = _PK_ ("new" ++ primRepString kind ++ "Array#")
1232         state = mkStatePrimTy s
1233     in
1234     mkGenPrimOp op_str [s_tv]
1235         [intPrimTy, state]
1236         (unboxedPair [state, mkMutableByteArrayPrimTy s])
1237
1238 ---------------------------------------------------------------------------
1239
1240 primOpInfo SameMutableArrayOp
1241   = let {
1242         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1243         mut_arr_ty = mkMutableArrayPrimTy s elt
1244     } in
1245     mkGenPrimOp SLIT("sameMutableArray#") [s_tv, elt_tv] [mut_arr_ty, mut_arr_ty]
1246                                    boolTy
1247
1248 primOpInfo SameMutableByteArrayOp
1249   = let {
1250         s = alphaTy; s_tv = alphaTyVar;
1251         mut_arr_ty = mkMutableByteArrayPrimTy s
1252     } in
1253     mkGenPrimOp SLIT("sameMutableByteArray#") [s_tv] [mut_arr_ty, mut_arr_ty]
1254                                    boolTy
1255
1256 ---------------------------------------------------------------------------
1257 -- Primitive arrays of Haskell pointers:
1258
1259 primOpInfo ReadArrayOp
1260   = let {
1261         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1262         state = mkStatePrimTy s
1263     } in
1264     mkGenPrimOp SLIT("readArray#") [s_tv, elt_tv]
1265         [mkMutableArrayPrimTy s elt, intPrimTy, state]
1266         (unboxedPair [state, elt])
1267
1268
1269 primOpInfo WriteArrayOp
1270   = let {
1271         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1272     } in
1273     mkGenPrimOp SLIT("writeArray#") [s_tv, elt_tv]
1274         [mkMutableArrayPrimTy s elt, intPrimTy, elt, mkStatePrimTy s]
1275         (mkStatePrimTy s)
1276
1277 primOpInfo IndexArrayOp
1278   = let { elt = alphaTy; elt_tv = alphaTyVar } in
1279     mkGenPrimOp SLIT("indexArray#") [elt_tv] [mkArrayPrimTy elt, intPrimTy]
1280         (unboxedPair [realWorldStatePrimTy, elt])
1281
1282 ---------------------------------------------------------------------------
1283 -- Primitive arrays full of unboxed bytes:
1284
1285 primOpInfo (ReadByteArrayOp kind)
1286   = let
1287         s = alphaTy; s_tv = alphaTyVar
1288
1289         op_str         = _PK_ ("read" ++ primRepString kind ++ "Array#")
1290         (tvs, prim_ty) = mkPrimTyApp betaTyVars kind
1291         state          = mkStatePrimTy s
1292     in
1293     mkGenPrimOp op_str (s_tv:tvs)
1294         [mkMutableByteArrayPrimTy s, intPrimTy, state]
1295         (unboxedPair [state, prim_ty])
1296
1297 primOpInfo (WriteByteArrayOp kind)
1298   = let
1299         s = alphaTy; s_tv = alphaTyVar
1300         op_str = _PK_ ("write" ++ primRepString kind ++ "Array#")
1301         (tvs, prim_ty) = mkPrimTyApp betaTyVars kind
1302     in
1303     mkGenPrimOp op_str (s_tv:tvs)
1304         [mkMutableByteArrayPrimTy s, intPrimTy, prim_ty, mkStatePrimTy s]
1305         (mkStatePrimTy s)
1306
1307 primOpInfo (IndexByteArrayOp kind)
1308   = let
1309         op_str = _PK_ ("index" ++ primRepString kind ++ "Array#")
1310         (tvs, prim_ty) = mkPrimTyApp alphaTyVars kind
1311     in
1312     mkGenPrimOp op_str tvs [byteArrayPrimTy, intPrimTy] prim_ty
1313
1314 primOpInfo (IndexOffForeignObjOp kind)
1315   = let
1316         op_str = _PK_ ("index" ++ primRepString kind ++ "OffForeignObj#")
1317         (tvs, prim_ty) = mkPrimTyApp alphaTyVars kind
1318     in
1319     mkGenPrimOp op_str tvs [foreignObjPrimTy, intPrimTy] prim_ty
1320
1321 primOpInfo (IndexOffAddrOp kind)
1322   = let
1323         op_str = _PK_ ("index" ++ primRepString kind ++ "OffAddr#")
1324         (tvs, prim_ty) = mkPrimTyApp alphaTyVars kind
1325     in
1326     mkGenPrimOp op_str tvs [addrPrimTy, intPrimTy] prim_ty
1327
1328 primOpInfo (WriteOffAddrOp kind)
1329   = let
1330         s = alphaTy; s_tv = alphaTyVar
1331         op_str = _PK_ ("write" ++ primRepString kind ++ "OffAddr#")
1332         (tvs, prim_ty) = mkPrimTyApp betaTyVars kind
1333     in
1334     mkGenPrimOp op_str (s_tv:tvs)
1335         [addrPrimTy, intPrimTy, prim_ty, mkStatePrimTy s]
1336         (mkStatePrimTy s)
1337
1338 ---------------------------------------------------------------------------
1339 primOpInfo UnsafeFreezeArrayOp
1340   = let {
1341         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1342         state = mkStatePrimTy s
1343     } in
1344     mkGenPrimOp SLIT("unsafeFreezeArray#") [s_tv, elt_tv]
1345         [mkMutableArrayPrimTy s elt, state]
1346         (unboxedPair [state, mkArrayPrimTy elt])
1347
1348 primOpInfo UnsafeFreezeByteArrayOp
1349   = let { 
1350         s = alphaTy; s_tv = alphaTyVar;
1351         state = mkStatePrimTy s
1352     } in
1353     mkGenPrimOp SLIT("unsafeFreezeByteArray#") [s_tv]
1354         [mkMutableByteArrayPrimTy s, state]
1355         (unboxedPair [state, byteArrayPrimTy])
1356
1357 primOpInfo UnsafeThawArrayOp
1358   = let {
1359         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1360         state = mkStatePrimTy s
1361     } in
1362     mkGenPrimOp SLIT("unsafeThawArray#") [s_tv, elt_tv]
1363         [mkArrayPrimTy elt, state]
1364         (unboxedPair [state, mkMutableArrayPrimTy s elt])
1365
1366 primOpInfo UnsafeThawByteArrayOp
1367   = let { 
1368         s = alphaTy; s_tv = alphaTyVar;
1369         state = mkStatePrimTy s
1370     } in
1371     mkGenPrimOp SLIT("unsafeThawByteArray#") [s_tv]
1372         [byteArrayPrimTy, state]
1373         (unboxedPair [state, mkMutableByteArrayPrimTy s])
1374
1375 ---------------------------------------------------------------------------
1376 primOpInfo SizeofByteArrayOp
1377   = mkGenPrimOp
1378         SLIT("sizeofByteArray#") []
1379         [byteArrayPrimTy]
1380         intPrimTy
1381
1382 primOpInfo SizeofMutableByteArrayOp
1383   = let { s = alphaTy; s_tv = alphaTyVar } in
1384     mkGenPrimOp
1385         SLIT("sizeofMutableByteArray#") [s_tv]
1386         [mkMutableByteArrayPrimTy s]
1387         intPrimTy
1388 \end{code}
1389
1390
1391 %************************************************************************
1392 %*                                                                      *
1393 \subsubsection[PrimOp-MutVars]{PrimOpInfo for mutable variable ops}
1394 %*                                                                      *
1395 %************************************************************************
1396
1397 \begin{code}
1398 primOpInfo NewMutVarOp
1399   = let {
1400         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1401         state = mkStatePrimTy s
1402     } in
1403     mkGenPrimOp SLIT("newMutVar#") [s_tv, elt_tv] 
1404         [elt, state]
1405         (unboxedPair [state, mkMutVarPrimTy s elt])
1406
1407 primOpInfo ReadMutVarOp
1408   = let {
1409         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1410         state = mkStatePrimTy s
1411     } in
1412     mkGenPrimOp SLIT("readMutVar#") [s_tv, elt_tv]
1413         [mkMutVarPrimTy s elt, state]
1414         (unboxedPair [state, elt])
1415
1416
1417 primOpInfo WriteMutVarOp
1418   = let {
1419         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1420     } in
1421     mkGenPrimOp SLIT("writeMutVar#") [s_tv, elt_tv]
1422         [mkMutVarPrimTy s elt, elt, mkStatePrimTy s]
1423         (mkStatePrimTy s)
1424
1425 primOpInfo SameMutVarOp
1426   = let {
1427         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1428         mut_var_ty = mkMutVarPrimTy s elt
1429     } in
1430     mkGenPrimOp SLIT("sameMutVar#") [s_tv, elt_tv] [mut_var_ty, mut_var_ty]
1431                                    boolTy
1432 \end{code}
1433
1434 %************************************************************************
1435 %*                                                                      *
1436 \subsubsection[PrimOp-Exceptions]{PrimOpInfo for exceptions}
1437 %*                                                                      *
1438 %************************************************************************
1439
1440 catch :: IO a -> (IOError -> IO a) -> IO a
1441 catch :: a  -> (b -> a) -> a
1442
1443 \begin{code}
1444 primOpInfo CatchOp   
1445   = let
1446         a = alphaTy; a_tv = alphaTyVar
1447         b = betaTy;  b_tv = betaTyVar;
1448     in
1449     mkGenPrimOp SLIT("catch#") [a_tv, b_tv] [a, mkFunTy b a] a
1450
1451 primOpInfo RaiseOp
1452   = let
1453         a = alphaTy; a_tv = alphaTyVar
1454         b = betaTy;  b_tv = betaTyVar;
1455     in
1456     mkGenPrimOp SLIT("raise#") [a_tv, b_tv] [a] b
1457 \end{code}
1458
1459 %************************************************************************
1460 %*                                                                      *
1461 \subsubsection[PrimOp-MVars]{PrimOpInfo for synchronizing Variables}
1462 %*                                                                      *
1463 %************************************************************************
1464
1465 \begin{code}
1466 primOpInfo NewMVarOp
1467   = let
1468         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1469         state = mkStatePrimTy s
1470     in
1471     mkGenPrimOp SLIT("newMVar#") [s_tv, elt_tv] [state]
1472         (unboxedPair [state, mkMVarPrimTy s elt])
1473
1474 primOpInfo TakeMVarOp
1475   = let
1476         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1477         state = mkStatePrimTy s
1478     in
1479     mkGenPrimOp SLIT("takeMVar#") [s_tv, elt_tv]
1480         [mkMVarPrimTy s elt, state]
1481         (unboxedPair [state, elt])
1482
1483 primOpInfo PutMVarOp
1484   = let
1485         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1486     in
1487     mkGenPrimOp SLIT("putMVar#") [s_tv, elt_tv]
1488         [mkMVarPrimTy s elt, elt, mkStatePrimTy s]
1489         (mkStatePrimTy s)
1490
1491 primOpInfo SameMVarOp
1492   = let
1493         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1494         mvar_ty = mkMVarPrimTy s elt
1495     in
1496     mkGenPrimOp SLIT("sameMVar#") [s_tv, elt_tv] [mvar_ty, mvar_ty] boolTy
1497
1498 primOpInfo IsEmptyMVarOp
1499   = let
1500         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1501         state = mkStatePrimTy s
1502     in
1503     mkGenPrimOp SLIT("isEmptyMVar#") [s_tv, elt_tv]
1504         [mkMVarPrimTy s elt, mkStatePrimTy s]
1505         (unboxedPair [state, intPrimTy])
1506
1507 \end{code}
1508
1509 %************************************************************************
1510 %*                                                                      *
1511 \subsubsection[PrimOp-Wait]{PrimOpInfo for delay/wait operations}
1512 %*                                                                      *
1513 %************************************************************************
1514
1515 \begin{code}
1516
1517 primOpInfo DelayOp
1518   = let {
1519         s = alphaTy; s_tv = alphaTyVar
1520     } in
1521     mkGenPrimOp SLIT("delay#") [s_tv]
1522         [intPrimTy, mkStatePrimTy s] (mkStatePrimTy s)
1523
1524 primOpInfo WaitReadOp
1525   = let {
1526         s = alphaTy; s_tv = alphaTyVar
1527     } in
1528     mkGenPrimOp SLIT("waitRead#") [s_tv]
1529         [intPrimTy, mkStatePrimTy s] (mkStatePrimTy s)
1530
1531 primOpInfo WaitWriteOp
1532   = let {
1533         s = alphaTy; s_tv = alphaTyVar
1534     } in
1535     mkGenPrimOp SLIT("waitWrite#") [s_tv]
1536         [intPrimTy, mkStatePrimTy s] (mkStatePrimTy s)
1537 \end{code}
1538
1539 %************************************************************************
1540 %*                                                                      *
1541 \subsubsection[PrimOp-Concurrency]{Concurrency Primitives}
1542 %*                                                                      *
1543 %************************************************************************
1544
1545 \begin{code}
1546 -- fork# :: a -> State# RealWorld -> (# State# RealWorld, ThreadId# #)
1547 primOpInfo ForkOp       
1548   = mkGenPrimOp SLIT("fork#") [alphaTyVar] 
1549         [alphaTy, realWorldStatePrimTy]
1550         (unboxedPair [realWorldStatePrimTy, threadIdPrimTy])
1551
1552 -- killThread# :: ThreadId# -> State# RealWorld -> State# RealWorld
1553 primOpInfo KillThreadOp
1554   = mkGenPrimOp SLIT("killThread#") [alphaTyVar] 
1555         [threadIdPrimTy, alphaTy, realWorldStatePrimTy]
1556         realWorldStatePrimTy
1557
1558 -- yield# :: State# RealWorld -> State# RealWorld
1559 primOpInfo YieldOp
1560   = mkGenPrimOp SLIT("yield#") [] 
1561         [realWorldStatePrimTy]
1562         realWorldStatePrimTy
1563
1564 -- myThreadId# :: State# RealWorld -> (# State# RealWorld, ThreadId# #)
1565 primOpInfo MyThreadIdOp
1566   = mkGenPrimOp SLIT("myThreadId#") [] 
1567         [realWorldStatePrimTy]
1568         (unboxedPair [realWorldStatePrimTy, threadIdPrimTy])
1569 \end{code}
1570
1571 ************************************************************************
1572 %*                                                                      *
1573 \subsubsection[PrimOps-Foreign]{PrimOpInfo for Foreign Objects}
1574 %*                                                                      *
1575 %************************************************************************
1576
1577 \begin{code}
1578 primOpInfo MakeForeignObjOp
1579   = mkGenPrimOp SLIT("makeForeignObj#") [] 
1580         [addrPrimTy, realWorldStatePrimTy] 
1581         (unboxedPair [realWorldStatePrimTy, foreignObjPrimTy])
1582
1583 primOpInfo WriteForeignObjOp
1584  = let {
1585         s = alphaTy; s_tv = alphaTyVar
1586     } in
1587    mkGenPrimOp SLIT("writeForeignObj#") [s_tv]
1588         [foreignObjPrimTy, addrPrimTy, mkStatePrimTy s] (mkStatePrimTy s)
1589 \end{code}
1590
1591 ************************************************************************
1592 %*                                                                      *
1593 \subsubsection[PrimOps-Weak]{PrimOpInfo for Weak Pointers}
1594 %*                                                                      *
1595 %************************************************************************
1596
1597 A @Weak@ Pointer is created by the @mkWeak#@ primitive:
1598
1599         mkWeak# :: k -> v -> f -> State# RealWorld 
1600                         -> (# State# RealWorld, Weak# v #)
1601
1602 In practice, you'll use the higher-level
1603
1604         data Weak v = Weak# v
1605         mkWeak :: k -> v -> IO () -> IO (Weak v)
1606
1607 \begin{code}
1608 primOpInfo MkWeakOp
1609   = mkGenPrimOp SLIT("mkWeak#") [alphaTyVar, betaTyVar, gammaTyVar] 
1610         [alphaTy, betaTy, gammaTy, realWorldStatePrimTy]
1611         (unboxedPair [realWorldStatePrimTy, mkWeakPrimTy betaTy])
1612 \end{code}
1613
1614 The following operation dereferences a weak pointer.  The weak pointer
1615 may have been finalized, so the operation returns a result code which
1616 must be inspected before looking at the dereferenced value.
1617
1618         deRefWeak# :: Weak# v -> State# RealWorld ->
1619                         (# State# RealWorld, v, Int# #)
1620
1621 Only look at v if the Int# returned is /= 0 !!
1622
1623 The higher-level op is
1624
1625         deRefWeak :: Weak v -> IO (Maybe v)
1626
1627 \begin{code}
1628 primOpInfo DeRefWeakOp
1629  = mkGenPrimOp SLIT("deRefWeak#") [alphaTyVar]
1630         [mkWeakPrimTy alphaTy, realWorldStatePrimTy]
1631         (unboxedTriple [realWorldStatePrimTy, intPrimTy, alphaTy])
1632 \end{code}
1633
1634 Weak pointers can be finalized early by using the finalize# operation:
1635         
1636         finalizeWeak# :: Weak# v -> State# RealWorld -> 
1637                            (# State# RealWorld, Int#, IO () #)
1638
1639 The Int# returned is either
1640
1641         0 if the weak pointer has already been finalized, or it has no
1642           finalizer (the third component is then invalid).
1643
1644         1 if the weak pointer is still alive, with the finalizer returned
1645           as the third component.
1646
1647 \begin{code}
1648 primOpInfo FinalizeWeakOp
1649  = mkGenPrimOp SLIT("finalizeWeak#") [alphaTyVar]
1650         [mkWeakPrimTy alphaTy, realWorldStatePrimTy]
1651         (unboxedTriple [realWorldStatePrimTy, intPrimTy,
1652                         mkFunTy realWorldStatePrimTy 
1653                           (unboxedPair [realWorldStatePrimTy,unitTy])])
1654 \end{code}
1655
1656 %************************************************************************
1657 %*                                                                      *
1658 \subsubsection[PrimOp-stable-pointers]{PrimOpInfo for stable pointers and stable names}
1659 %*                                                                      *
1660 %************************************************************************
1661
1662 A {\em stable name/pointer} is an index into a table of stable name
1663 entries.  Since the garbage collector is told about stable pointers,
1664 it is safe to pass a stable pointer to external systems such as C
1665 routines.
1666
1667 \begin{verbatim}
1668 makeStablePtr#  :: a -> State# RealWorld -> (# State# RealWorld, a #)
1669 freeStablePtr   :: StablePtr# a -> State# RealWorld -> State# RealWorld
1670 deRefStablePtr# :: StablePtr# a -> State# RealWorld -> (# State# RealWorld, a #)
1671 eqStablePtr#    :: StablePtr# a -> StablePtr# a -> Int#
1672 \end{verbatim}
1673
1674 It may seem a bit surprising that @makeStablePtr#@ is a @IO@
1675 operation since it doesn't (directly) involve IO operations.  The
1676 reason is that if some optimisation pass decided to duplicate calls to
1677 @makeStablePtr#@ and we only pass one of the stable pointers over, a
1678 massive space leak can result.  Putting it into the IO monad
1679 prevents this.  (Another reason for putting them in a monad is to
1680 ensure correct sequencing wrt the side-effecting @freeStablePtr@
1681 operation.)
1682
1683 An important property of stable pointers is that if you call
1684 makeStablePtr# twice on the same object you get the same stable
1685 pointer back.
1686
1687 Note that we can implement @freeStablePtr#@ using @_ccall_@ (and,
1688 besides, it's not likely to be used from Haskell) so it's not a
1689 primop.
1690
1691 Question: Why @RealWorld@ - won't any instance of @_ST@ do the job? [ADR]
1692
1693 Stable Names
1694 ~~~~~~~~~~~~
1695
1696 A stable name is like a stable pointer, but with three important differences:
1697
1698         (a) You can't deRef one to get back to the original object.
1699         (b) You can convert one to an Int.
1700         (c) You don't need to 'freeStableName'
1701
1702 The existence of a stable name doesn't guarantee to keep the object it
1703 points to alive (unlike a stable pointer), hence (a).
1704
1705 Invariants:
1706         
1707         (a) makeStableName always returns the same value for a given
1708             object (same as stable pointers).
1709
1710         (b) if two stable names are equal, it implies that the objects
1711             from which they were created were the same.
1712
1713         (c) stableNameToInt always returns the same Int for a given
1714             stable name.
1715
1716 \begin{code}
1717 primOpInfo MakeStablePtrOp
1718   = mkGenPrimOp SLIT("makeStablePtr#") [alphaTyVar]
1719         [alphaTy, realWorldStatePrimTy]
1720         (unboxedPair [realWorldStatePrimTy, 
1721                         mkTyConApp stablePtrPrimTyCon [alphaTy]])
1722
1723 primOpInfo DeRefStablePtrOp
1724   = mkGenPrimOp SLIT("deRefStablePtr#") [alphaTyVar]
1725         [mkStablePtrPrimTy alphaTy, realWorldStatePrimTy]
1726         (unboxedPair [realWorldStatePrimTy, alphaTy])
1727
1728 primOpInfo EqStablePtrOp
1729   = mkGenPrimOp SLIT("eqStablePtr#") [alphaTyVar, betaTyVar]
1730         [mkStablePtrPrimTy alphaTy, mkStablePtrPrimTy betaTy]
1731         intPrimTy
1732
1733 primOpInfo MakeStableNameOp
1734   = mkGenPrimOp SLIT("makeStableName#") [alphaTyVar]
1735         [alphaTy, realWorldStatePrimTy]
1736         (unboxedPair [realWorldStatePrimTy, 
1737                         mkTyConApp stableNamePrimTyCon [alphaTy]])
1738
1739 primOpInfo EqStableNameOp
1740   = mkGenPrimOp SLIT("eqStableName#") [alphaTyVar, betaTyVar]
1741         [mkStableNamePrimTy alphaTy, mkStableNamePrimTy betaTy]
1742         intPrimTy
1743
1744 primOpInfo StableNameToIntOp
1745   = mkGenPrimOp SLIT("stableNameToInt#") [alphaTyVar]
1746         [mkStableNamePrimTy alphaTy]
1747         intPrimTy
1748 \end{code}
1749
1750 %************************************************************************
1751 %*                                                                      *
1752 \subsubsection[PrimOp-unsafePointerEquality]{PrimOpInfo for Pointer Equality}
1753 %*                                                                      *
1754 %************************************************************************
1755
1756 [Alastair Reid is to blame for this!]
1757
1758 These days, (Glasgow) Haskell seems to have a bit of everything from
1759 other languages: strict operations, mutable variables, sequencing,
1760 pointers, etc.  About the only thing left is LISP's ability to test
1761 for pointer equality.  So, let's add it in!
1762
1763 \begin{verbatim}
1764 reallyUnsafePtrEquality :: a -> a -> Int#
1765 \end{verbatim}
1766
1767 which tests any two closures (of the same type) to see if they're the
1768 same.  (Returns $0$ for @False@, $\neq 0$ for @True@ - to avoid
1769 difficulties of trying to box up the result.)
1770
1771 NB This is {\em really unsafe\/} because even something as trivial as
1772 a garbage collection might change the answer by removing indirections.
1773 Still, no-one's forcing you to use it.  If you're worried about little
1774 things like loss of referential transparency, you might like to wrap
1775 it all up in a monad-like thing as John O'Donnell and John Hughes did
1776 for non-determinism (1989 (Fraserburgh) Glasgow FP Workshop
1777 Proceedings?)
1778
1779 I'm thinking of using it to speed up a critical equality test in some
1780 graphics stuff in a context where the possibility of saying that
1781 denotationally equal things aren't isn't a problem (as long as it
1782 doesn't happen too often.)  ADR
1783
1784 To Will: Jim said this was already in, but I can't see it so I'm
1785 adding it.  Up to you whether you add it.  (Note that this could have
1786 been readily implemented using a @veryDangerousCCall@ before they were
1787 removed...)
1788
1789 \begin{code}
1790 primOpInfo ReallyUnsafePtrEqualityOp
1791   = mkGenPrimOp SLIT("reallyUnsafePtrEquality#") [alphaTyVar]
1792         [alphaTy, alphaTy] intPrimTy
1793 \end{code}
1794
1795 %************************************************************************
1796 %*                                                                      *
1797 \subsubsection[PrimOp-parallel]{PrimOpInfo for parallelism op(s)}
1798 %*                                                                      *
1799 %************************************************************************
1800
1801 \begin{code}
1802 primOpInfo SeqOp        -- seq# :: a -> Int#
1803   = mkGenPrimOp SLIT("seq#")    [alphaTyVar] [alphaTy] intPrimTy
1804
1805 primOpInfo ParOp        -- par# :: a -> Int#
1806   = mkGenPrimOp SLIT("par#")    [alphaTyVar] [alphaTy] intPrimTy
1807 \end{code}
1808
1809 \begin{code}
1810 -- HWL: The first 4 Int# in all par... annotations denote:
1811 --   name, granularity info, size of result, degree of parallelism
1812 --      Same  structure as _seq_ i.e. returns Int#
1813
1814 primOpInfo ParGlobalOp  -- parGlobal# :: Int# -> Int# -> Int# -> Int# -> a -> b -> b
1815   = mkGenPrimOp SLIT("parGlobal#")      [alphaTyVar,betaTyVar] [alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,betaTy] intPrimTy
1816
1817 primOpInfo ParLocalOp   -- parLocal# :: Int# -> Int# -> Int# -> Int# -> a -> b -> b
1818   = mkGenPrimOp SLIT("parLocal#")       [alphaTyVar,betaTyVar] [alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,betaTy] intPrimTy
1819
1820 primOpInfo ParAtOp      -- parAt# :: Int# -> Int# -> Int# -> Int# -> a -> b -> c -> c
1821   = mkGenPrimOp SLIT("parAt#")  [alphaTyVar,betaTyVar,gammaTyVar] [betaTy,alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,gammaTy] intPrimTy
1822
1823 primOpInfo ParAtAbsOp   -- parAtAbs# :: Int# -> Int# -> Int# -> Int# -> Int# -> a -> b -> b
1824   = mkGenPrimOp SLIT("parAtAbs#")       [alphaTyVar,betaTyVar] [alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,betaTy] intPrimTy
1825
1826 primOpInfo ParAtRelOp   -- parAtRel# :: Int# -> Int# -> Int# -> Int# -> Int# -> a -> b -> b
1827   = mkGenPrimOp SLIT("parAtRel#")       [alphaTyVar,betaTyVar] [alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,betaTy] intPrimTy
1828
1829 primOpInfo ParAtForNowOp        -- parAtForNow# :: Int# -> Int# -> Int# -> Int# -> a -> b -> c -> c
1830   = mkGenPrimOp SLIT("parAtForNow#")    [alphaTyVar,betaTyVar,gammaTyVar] [betaTy,alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,gammaTy] intPrimTy
1831
1832 primOpInfo CopyableOp   -- copyable# :: a -> a
1833   = mkGenPrimOp SLIT("copyable#")       [alphaTyVar] [alphaTy] intPrimTy
1834
1835 primOpInfo NoFollowOp   -- noFollow# :: a -> a
1836   = mkGenPrimOp SLIT("noFollow#")       [alphaTyVar] [alphaTy] intPrimTy
1837 \end{code}
1838
1839 %************************************************************************
1840 %*                                                                      *
1841 \subsubsection[PrimOp-IO-etc]{PrimOpInfo for C calls, and I/O-ish things}
1842 %*                                                                      *
1843 %************************************************************************
1844
1845 \begin{code}
1846 primOpInfo (CCallOp _ _ _ _)
1847      = mkGenPrimOp SLIT("ccall#") [alphaTyVar] [] alphaTy
1848
1849 {-
1850 primOpInfo (CCallOp _ _ _ _ arg_tys result_ty)
1851   = mkGenPrimOp SLIT("ccall#") [] arg_tys result_tycon tys_applied
1852   where
1853     (result_tycon, tys_applied, _) = splitAlgTyConApp result_ty
1854 -}
1855 \end{code}
1856
1857 %************************************************************************
1858 %*                                                                      *
1859 \subsubsection[PrimOp-tag]{PrimOpInfo for @dataToTag#@ and @tagToEnum#@}
1860 %*                                                                      *
1861 %************************************************************************
1862
1863 These primops are pretty wierd.
1864
1865         dataToTag# :: a -> Int    (arg must be an evaluated data type)
1866         tagToEnum# :: Int -> a    (result type must be an enumerated type)
1867
1868 The constraints aren't currently checked by the front end, but the
1869 code generator will fall over if they aren't satisfied.
1870
1871 \begin{code}
1872 primOpInfo DataToTagOp
1873   = mkGenPrimOp SLIT("dataToTag#") [alphaTyVar] [alphaTy] intPrimTy
1874
1875 primOpInfo TagToEnumOp
1876   = mkGenPrimOp SLIT("tagToEnum#") [alphaTyVar] [intPrimTy] alphaTy
1877
1878 #ifdef DEBUG
1879 primOpInfo op = panic ("primOpInfo:"++ show (I# (tagOf_PrimOp op)))
1880 #endif
1881 \end{code}
1882
1883 %************************************************************************
1884 %*                                                                      *
1885 \subsubsection[PrimOp-ool]{Which PrimOps are out-of-line}
1886 %*                                                                      *
1887 %************************************************************************
1888
1889 Some PrimOps need to be called out-of-line because they either need to
1890 perform a heap check or they block.
1891
1892 \begin{code}
1893 primOpOutOfLine op
1894   = case op of
1895         TakeMVarOp              -> True
1896         PutMVarOp               -> True
1897         DelayOp                 -> True
1898         WaitReadOp              -> True
1899         WaitWriteOp             -> True
1900         CatchOp                 -> True
1901         RaiseOp                 -> True
1902         NewArrayOp              -> True
1903         NewByteArrayOp _        -> True
1904         IntegerAddOp            -> True
1905         IntegerSubOp            -> True
1906         IntegerMulOp            -> True
1907         IntegerGcdOp            -> True
1908         IntegerQuotRemOp        -> True
1909         IntegerDivModOp         -> True
1910         Int2IntegerOp           -> True
1911         Word2IntegerOp          -> True
1912         Addr2IntegerOp          -> True
1913         Word64ToIntegerOp       -> True
1914         Int64ToIntegerOp        -> True
1915         FloatDecodeOp           -> True
1916         DoubleDecodeOp          -> True
1917         MkWeakOp                -> True
1918         FinalizeWeakOp          -> True
1919         MakeStableNameOp        -> True
1920         MakeForeignObjOp        -> True
1921         NewMutVarOp             -> True
1922         NewMVarOp               -> True
1923         ForkOp                  -> True
1924         KillThreadOp            -> True
1925         YieldOp                 -> True
1926         CCallOp _ _ may_gc@True _ -> True       -- _ccall_GC_
1927           -- the next one doesn't perform any heap checks,
1928           -- but it is of such an esoteric nature that
1929           -- it is done out-of-line rather than require
1930           -- the NCG to implement it.
1931         UnsafeThawArrayOp       -> True
1932         _                       -> False
1933 \end{code}
1934
1935 Sometimes we may choose to execute a PrimOp even though it isn't
1936 certain that its result will be required; ie execute them
1937 ``speculatively''.  The same thing as ``cheap eagerness.'' Usually
1938 this is OK, because PrimOps are usually cheap, but it isn't OK for
1939 (a)~expensive PrimOps and (b)~PrimOps which can fail.
1940
1941 See also @primOpIsCheap@ (below).
1942
1943 PrimOps that have side effects also should not be executed speculatively
1944 or by data dependencies.
1945
1946 \begin{code}
1947 primOpOkForSpeculation :: PrimOp -> Bool
1948 primOpOkForSpeculation op 
1949   = not (primOpCanFail op || primOpHasSideEffects op || primOpOutOfLine op)
1950 \end{code}
1951
1952 @primOpIsCheap@, as used in \tr{SimplUtils.lhs}.  For now (HACK
1953 WARNING), we just borrow some other predicates for a
1954 what-should-be-good-enough test.  "Cheap" means willing to call it more
1955 than once.  Evaluation order is unaffected.
1956
1957 \begin{code}
1958 primOpIsCheap op = not (primOpHasSideEffects op || primOpOutOfLine op)
1959 \end{code}
1960
1961 \begin{code}
1962 primOpCanFail :: PrimOp -> Bool
1963 -- Int.
1964 primOpCanFail IntQuotOp = True          -- Divide by zero
1965 primOpCanFail IntRemOp          = True          -- Divide by zero
1966
1967 -- Integer
1968 primOpCanFail IntegerQuotRemOp = True           -- Divide by zero
1969 primOpCanFail IntegerDivModOp   = True          -- Divide by zero
1970
1971 -- Float.  ToDo: tan? tanh?
1972 primOpCanFail FloatDivOp        = True          -- Divide by zero
1973 primOpCanFail FloatLogOp        = True          -- Log of zero
1974 primOpCanFail FloatAsinOp       = True          -- Arg out of domain
1975 primOpCanFail FloatAcosOp       = True          -- Arg out of domain
1976
1977 -- Double.  ToDo: tan? tanh?
1978 primOpCanFail DoubleDivOp       = True          -- Divide by zero
1979 primOpCanFail DoubleLogOp       = True          -- Log of zero
1980 primOpCanFail DoubleAsinOp      = True          -- Arg out of domain
1981 primOpCanFail DoubleAcosOp      = True          -- Arg out of domain
1982
1983 primOpCanFail other_op          = False
1984 \end{code}
1985
1986 And some primops have side-effects and so, for example, must not be
1987 duplicated.
1988
1989 \begin{code}
1990 primOpHasSideEffects :: PrimOp -> Bool
1991
1992 primOpHasSideEffects TakeMVarOp        = True
1993 primOpHasSideEffects DelayOp           = True
1994 primOpHasSideEffects WaitReadOp        = True
1995 primOpHasSideEffects WaitWriteOp       = True
1996
1997 primOpHasSideEffects ParOp             = True
1998 primOpHasSideEffects ForkOp            = True
1999 primOpHasSideEffects KillThreadOp      = True
2000 primOpHasSideEffects YieldOp           = True
2001 primOpHasSideEffects SeqOp             = True
2002
2003 primOpHasSideEffects MakeForeignObjOp  = True
2004 primOpHasSideEffects WriteForeignObjOp = True
2005 primOpHasSideEffects MkWeakOp          = True
2006 primOpHasSideEffects DeRefWeakOp       = True
2007 primOpHasSideEffects FinalizeWeakOp    = True
2008 primOpHasSideEffects MakeStablePtrOp   = True
2009 primOpHasSideEffects MakeStableNameOp  = True
2010 primOpHasSideEffects EqStablePtrOp     = True  -- SOF
2011 primOpHasSideEffects DeRefStablePtrOp  = True  -- ??? JSM & ADR
2012
2013 primOpHasSideEffects ParGlobalOp        = True
2014 primOpHasSideEffects ParLocalOp         = True
2015 primOpHasSideEffects ParAtOp            = True
2016 primOpHasSideEffects ParAtAbsOp         = True
2017 primOpHasSideEffects ParAtRelOp         = True
2018 primOpHasSideEffects ParAtForNowOp      = True
2019 primOpHasSideEffects CopyableOp         = True  -- Possibly not.  ASP 
2020 primOpHasSideEffects NoFollowOp         = True  -- Possibly not.  ASP
2021
2022 -- CCall
2023 primOpHasSideEffects (CCallOp   _ _ _ _) = True
2024
2025 primOpHasSideEffects other = False
2026 \end{code}
2027
2028 Inline primitive operations that perform calls need wrappers to save
2029 any live variables that are stored in caller-saves registers.
2030
2031 \begin{code}
2032 primOpNeedsWrapper :: PrimOp -> Bool
2033
2034 primOpNeedsWrapper (CCallOp _ _ _ _)    = True
2035
2036 primOpNeedsWrapper Integer2IntOp        = True
2037 primOpNeedsWrapper Integer2WordOp       = True
2038 primOpNeedsWrapper IntegerCmpOp         = True
2039 primOpNeedsWrapper IntegerCmpIntOp      = True
2040
2041 primOpNeedsWrapper FloatExpOp           = True
2042 primOpNeedsWrapper FloatLogOp           = True
2043 primOpNeedsWrapper FloatSqrtOp          = True
2044 primOpNeedsWrapper FloatSinOp           = True
2045 primOpNeedsWrapper FloatCosOp           = True
2046 primOpNeedsWrapper FloatTanOp           = True
2047 primOpNeedsWrapper FloatAsinOp          = True
2048 primOpNeedsWrapper FloatAcosOp          = True
2049 primOpNeedsWrapper FloatAtanOp          = True
2050 primOpNeedsWrapper FloatSinhOp          = True
2051 primOpNeedsWrapper FloatCoshOp          = True
2052 primOpNeedsWrapper FloatTanhOp          = True
2053 primOpNeedsWrapper FloatPowerOp         = True
2054
2055 primOpNeedsWrapper DoubleExpOp          = True
2056 primOpNeedsWrapper DoubleLogOp          = True
2057 primOpNeedsWrapper DoubleSqrtOp         = True
2058 primOpNeedsWrapper DoubleSinOp          = True
2059 primOpNeedsWrapper DoubleCosOp          = True
2060 primOpNeedsWrapper DoubleTanOp          = True
2061 primOpNeedsWrapper DoubleAsinOp         = True
2062 primOpNeedsWrapper DoubleAcosOp         = True
2063 primOpNeedsWrapper DoubleAtanOp         = True
2064 primOpNeedsWrapper DoubleSinhOp         = True
2065 primOpNeedsWrapper DoubleCoshOp         = True
2066 primOpNeedsWrapper DoubleTanhOp         = True
2067 primOpNeedsWrapper DoublePowerOp        = True
2068
2069 primOpNeedsWrapper MakeStableNameOp     = True
2070 primOpNeedsWrapper DeRefStablePtrOp     = True
2071
2072 primOpNeedsWrapper DelayOp              = True
2073 primOpNeedsWrapper WaitReadOp           = True
2074 primOpNeedsWrapper WaitWriteOp          = True
2075
2076 primOpNeedsWrapper other_op             = False
2077 \end{code}
2078
2079 \begin{code}
2080 primOpOcc op
2081   = case (primOpInfo op) of
2082       Dyadic     occ _         -> occ
2083       Monadic    occ _         -> occ
2084       Compare    occ _         -> occ
2085       GenPrimOp  occ _ _ _     -> occ
2086 \end{code}
2087
2088 \begin{code}
2089 primOpUniq :: PrimOp -> Unique
2090 primOpUniq op = mkPrimOpIdUnique (IBOX(tagOf_PrimOp op))
2091
2092 primOpType :: PrimOp -> Type
2093 primOpType op
2094   = case (primOpInfo op) of
2095       Dyadic occ ty ->      dyadic_fun_ty ty
2096       Monadic occ ty ->     monadic_fun_ty ty
2097       Compare occ ty ->     compare_fun_ty ty
2098
2099       GenPrimOp occ tyvars arg_tys res_ty -> 
2100         mkForAllTys tyvars (mkFunTys arg_tys res_ty)
2101 \end{code}
2102
2103 \begin{code}
2104 data PrimOpResultInfo
2105   = ReturnsPrim     PrimRep
2106   | ReturnsAlg      TyCon
2107
2108 -- Some PrimOps need not return a manifest primitive or algebraic value
2109 -- (i.e. they might return a polymorphic value).  These PrimOps *must*
2110 -- be out of line, or the code generator won't work.
2111
2112 getPrimOpResultInfo :: PrimOp -> PrimOpResultInfo
2113 getPrimOpResultInfo op
2114   = case (primOpInfo op) of
2115       Dyadic  _ ty               -> ReturnsPrim (typePrimRep ty)
2116       Monadic _ ty               -> ReturnsPrim (typePrimRep ty)
2117       Compare _ ty               -> ReturnsAlg boolTyCon
2118       GenPrimOp _ _ _ ty         -> 
2119         let rep = typePrimRep ty in
2120         case rep of
2121            PtrRep -> case splitAlgTyConApp_maybe ty of
2122                         Nothing -> panic "getPrimOpResultInfo"
2123                         Just (tc,_,_) -> ReturnsAlg tc
2124            other -> ReturnsPrim other
2125
2126 isCompareOp :: PrimOp -> Bool
2127 isCompareOp op
2128   = case primOpInfo op of
2129       Compare _ _ -> True
2130       _           -> False
2131 \end{code}
2132
2133 The commutable ops are those for which we will try to move constants
2134 to the right hand side for strength reduction.
2135
2136 \begin{code}
2137 commutableOp :: PrimOp -> Bool
2138
2139 commutableOp CharEqOp     = True
2140 commutableOp CharNeOp     = True
2141 commutableOp IntAddOp     = True
2142 commutableOp IntMulOp     = True
2143 commutableOp AndOp        = True
2144 commutableOp OrOp         = True
2145 commutableOp XorOp        = True
2146 commutableOp IntEqOp      = True
2147 commutableOp IntNeOp      = True
2148 commutableOp IntegerAddOp = True
2149 commutableOp IntegerMulOp = True
2150 commutableOp IntegerGcdOp = True
2151 commutableOp FloatAddOp   = True
2152 commutableOp FloatMulOp   = True
2153 commutableOp FloatEqOp    = True
2154 commutableOp FloatNeOp    = True
2155 commutableOp DoubleAddOp  = True
2156 commutableOp DoubleMulOp  = True
2157 commutableOp DoubleEqOp   = True
2158 commutableOp DoubleNeOp   = True
2159 commutableOp _            = False
2160 \end{code}
2161
2162 Utils:
2163 \begin{code}
2164 mkPrimTyApp :: [TyVar] -> PrimRep -> ([TyVar], Type)
2165         -- CharRep       -->  ([],  Char#)
2166         -- StablePtrRep  -->  ([a], StablePtr# a)
2167 mkPrimTyApp tvs kind
2168   = (forall_tvs, mkTyConApp tycon (mkTyVarTys forall_tvs))
2169   where
2170     tycon      = primRepTyCon kind
2171     forall_tvs = take (tyConArity tycon) tvs
2172
2173 dyadic_fun_ty  ty = mkFunTys [ty, ty] ty
2174 monadic_fun_ty ty = mkFunTy  ty ty
2175 compare_fun_ty ty = mkFunTys [ty, ty] boolTy
2176 \end{code}
2177
2178 Output stuff:
2179 \begin{code}
2180 pprPrimOp  :: PrimOp -> SDoc
2181
2182 pprPrimOp (CCallOp fun is_casm may_gc cconv)
2183   = let
2184         callconv = text "{-" <> pprCallConv cconv <> text "-}"
2185
2186         before
2187           | is_casm && may_gc = "casm_GC ``"
2188           | is_casm           = "casm ``"
2189           | may_gc            = "ccall_GC "
2190           | otherwise         = "ccall "
2191
2192         after
2193           | is_casm   = text "''"
2194           | otherwise = empty
2195           
2196         ppr_dyn =
2197           case fun of
2198             Right _ -> text "dyn_"
2199             _       -> empty
2200
2201         ppr_fun =
2202          case fun of
2203            Right _ -> text "\"\""
2204            Left fn -> ptext fn
2205          
2206     in
2207     hcat [ ifPprDebug callconv
2208          , text "__", ppr_dyn
2209          , text before , ppr_fun , after]
2210
2211 pprPrimOp other_op
2212   = getPprStyle $ \ sty ->
2213    if ifaceStyle sty then       -- For interfaces Print it qualified with PrelGHC.
2214         ptext SLIT("PrelGHC.") <> pprOccName occ
2215    else
2216         pprOccName occ
2217   where
2218     occ = primOpOcc other_op
2219 \end{code}