[project @ 1998-12-22 16:31:28 by simonpj]
[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, varOcc )
35 import TyCon            ( TyCon )
36 import Type             ( mkForAllTys, mkForAllTy, mkFunTy, mkFunTys, 
37                           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
77     -- Word#-related ops:
78     | WordQuotOp | WordRemOp
79     | AndOp  | OrOp   | NotOp | XorOp
80     | SllOp  | SrlOp  -- shift {left,right} {logical}
81     | Int2WordOp | Word2IntOp -- casts
82
83     -- Addr#-related ops:
84     | Int2AddrOp | Addr2IntOp -- casts
85
86     -- Float#-related ops:
87     | FloatAddOp | FloatSubOp | FloatMulOp | FloatDivOp | FloatNegOp
88     | Float2IntOp | Int2FloatOp
89
90     | FloatExpOp   | FloatLogOp   | FloatSqrtOp
91     | FloatSinOp   | FloatCosOp   | FloatTanOp
92     | FloatAsinOp  | FloatAcosOp  | FloatAtanOp
93     | FloatSinhOp  | FloatCoshOp  | FloatTanhOp
94     -- not all machines have these available conveniently:
95     -- | FloatAsinhOp | FloatAcoshOp | FloatAtanhOp
96     | FloatPowerOp -- ** op
97
98     -- Double#-related ops:
99     | DoubleAddOp | DoubleSubOp | DoubleMulOp | DoubleDivOp | DoubleNegOp
100     | Double2IntOp | Int2DoubleOp
101     | Double2FloatOp | Float2DoubleOp
102
103     | DoubleExpOp   | DoubleLogOp   | DoubleSqrtOp
104     | DoubleSinOp   | DoubleCosOp   | DoubleTanOp
105     | DoubleAsinOp  | DoubleAcosOp  | DoubleAtanOp
106     | DoubleSinhOp  | DoubleCoshOp  | DoubleTanhOp
107     -- not all machines have these available conveniently:
108     -- | DoubleAsinhOp | DoubleAcoshOp | DoubleAtanhOp
109     | DoublePowerOp -- ** op
110
111     -- Integer (and related...) ops:
112     -- slightly weird -- to match GMP package.
113     | IntegerAddOp | IntegerSubOp | IntegerMulOp | IntegerGcdOp
114     | IntegerQuotRemOp | IntegerDivModOp | IntegerNegOp
115
116     | IntegerCmpOp
117
118     | Integer2IntOp  | Integer2WordOp  
119     | Int2IntegerOp  | Word2IntegerOp
120     | Addr2IntegerOp
121      -- casting to/from Integer and 64-bit (un)signed quantities.
122     | IntegerToInt64Op | Int64ToIntegerOp
123     | IntegerToWord64Op | Word64ToIntegerOp
124     -- ?? gcd, etc?
125
126     | FloatEncodeOp  | FloatDecodeOp
127     | DoubleEncodeOp | DoubleDecodeOp
128
129     -- primitive ops for primitive arrays
130
131     | NewArrayOp
132     | NewByteArrayOp PrimRep
133
134     | SameMutableArrayOp
135     | SameMutableByteArrayOp
136
137     | ReadArrayOp | WriteArrayOp | IndexArrayOp -- for arrays of Haskell ptrs
138
139     | ReadByteArrayOp   PrimRep
140     | WriteByteArrayOp  PrimRep
141     | IndexByteArrayOp  PrimRep
142     | IndexOffAddrOp    PrimRep
143     | WriteOffAddrOp    PrimRep
144         -- PrimRep can be one of {Char,Int,Addr,Float,Double}Kind.
145         -- This is just a cheesy encoding of a bunch of ops.
146         -- Note that ForeignObjRep is not included -- the only way of
147         -- creating a ForeignObj is with a ccall or casm.
148     | IndexOffForeignObjOp PrimRep
149
150     | UnsafeFreezeArrayOp | UnsafeFreezeByteArrayOp
151     | SizeofByteArrayOp   | SizeofMutableByteArrayOp
152
153     -- Mutable variables
154     | NewMutVarOp
155     | ReadMutVarOp
156     | WriteMutVarOp
157     | SameMutVarOp
158
159     -- for MVars
160     | NewMVarOp
161     | TakeMVarOp 
162     | PutMVarOp
163     | SameMVarOp
164
165     -- exceptions
166     | CatchOp
167     | RaiseOp
168
169     | MakeForeignObjOp
170     | WriteForeignObjOp
171
172     | MkWeakOp
173     | DeRefWeakOp
174
175     | MakeStablePtrOp
176     | DeRefStablePtrOp
177     | EqStablePtrOp
178 \end{code}
179
180 A special ``trap-door'' to use in making calls direct to C functions:
181 \begin{code}
182     | CCallOp   (Either 
183                     FAST_STRING    -- Left fn => An "unboxed" ccall# to `fn'.
184                     Unique)        -- Right u => first argument (an Addr#) is the function pointer
185                                    --   (unique is used to generate a 'typedef' to cast
186                                    --    the function pointer if compiling the ccall# down to
187                                    --    .hc code - can't do this inline for tedious reasons.)
188                                     
189                 Bool                -- True <=> really a "casm"
190                 Bool                -- True <=> might invoke Haskell GC
191                 CallConv            -- calling convention to use.
192
193     -- (... to be continued ... )
194 \end{code}
195
196 The ``type'' of @CCallOp foo [t1, ... tm] r@ is @t1 -> ... tm -> r@.
197 (See @primOpInfo@ for details.)
198
199 Note: that first arg and part of the result should be the system state
200 token (which we carry around to fool over-zealous optimisers) but
201 which isn't actually passed.
202
203 For example, we represent
204 \begin{pseudocode}
205 ((ccall# foo [StablePtr# a, Int] Float) sp# i#) :: (Float, IoWorld)
206 \end{pseudocode}
207 by
208 \begin{pseudocode}
209 Case
210   ( Prim
211       (CCallOp "foo" [Universe#, StablePtr# a, Int#] FloatPrimAndUniverse False)
212        -- :: Universe# -> StablePtr# a -> Int# -> FloatPrimAndUniverse
213       []
214       [w#, sp# i#]
215   )
216   (AlgAlts [ ( FloatPrimAndIoWorld,
217                  [f#, w#],
218                  Con (TupleCon 2) [Float, IoWorld] [F# f#, World w#]
219                ) ]
220              NoDefault
221   )
222 \end{pseudocode}
223
224 Nota Bene: there are some people who find the empty list of types in
225 the @Prim@ somewhat puzzling and would represent the above by
226 \begin{pseudocode}
227 Case
228   ( Prim
229       (CCallOp "foo" [alpha1, alpha2, alpha3] alpha4 False)
230        -- :: /\ alpha1, alpha2 alpha3, alpha4.
231        --       alpha1 -> alpha2 -> alpha3 -> alpha4
232       [Universe#, StablePtr# a, Int#, FloatPrimAndIoWorld]
233       [w#, sp# i#]
234   )
235   (AlgAlts [ ( FloatPrimAndIoWorld,
236                  [f#, w#],
237                  Con (TupleCon 2) [Float, IoWorld] [F# f#, World w#]
238                ) ]
239              NoDefault
240   )
241 \end{pseudocode}
242
243 But, this is a completely different way of using @CCallOp@.  The most
244 major changes required if we switch to this are in @primOpInfo@, and
245 the desugarer. The major difficulty is in moving the HeapRequirement
246 stuff somewhere appropriate.  (The advantage is that we could simplify
247 @CCallOp@ and record just the number of arguments with corresponding
248 simplifications in reading pragma unfoldings, the simplifier,
249 instantiation (etc) of core expressions, ... .  Maybe we should think
250 about using it this way?? ADR)
251
252 \begin{code}
253     -- (... continued from above ... )
254
255     -- Operation to test two closure addresses for equality (yes really!)
256     -- BLAME ALASTAIR REID FOR THIS!  THE REST OF US ARE INNOCENT!
257     | ReallyUnsafePtrEqualityOp
258
259     -- parallel stuff
260     | SeqOp
261     | ParOp
262
263     -- concurrency
264     | ForkOp
265     | KillThreadOp
266     | DelayOp
267     | WaitReadOp
268     | WaitWriteOp
269
270     | ParGlobalOp       -- named global par
271     | ParLocalOp        -- named local par
272     | ParAtOp           -- specifies destination of local par
273     | ParAtAbsOp        -- specifies destination of local par (abs processor)
274     | ParAtRelOp        -- specifies destination of local par (rel processor)
275     | ParAtForNowOp     -- specifies initial destination of global par
276     | CopyableOp        -- marks copyable code
277     | NoFollowOp        -- marks non-followup expression
278 \end{code}
279
280 Used for the Ord instance
281
282 \begin{code}
283 tagOf_PrimOp CharGtOp                         = (ILIT( 1) :: FAST_INT)
284 tagOf_PrimOp CharGeOp                         = ILIT(  2)
285 tagOf_PrimOp CharEqOp                         = ILIT(  3)
286 tagOf_PrimOp CharNeOp                         = ILIT(  4)
287 tagOf_PrimOp CharLtOp                         = ILIT(  5)
288 tagOf_PrimOp CharLeOp                         = ILIT(  6)
289 tagOf_PrimOp IntGtOp                          = ILIT(  7)
290 tagOf_PrimOp IntGeOp                          = ILIT(  8)
291 tagOf_PrimOp IntEqOp                          = ILIT(  9)
292 tagOf_PrimOp IntNeOp                          = ILIT( 10)
293 tagOf_PrimOp IntLtOp                          = ILIT( 11)
294 tagOf_PrimOp IntLeOp                          = ILIT( 12)
295 tagOf_PrimOp WordGtOp                         = ILIT( 13)
296 tagOf_PrimOp WordGeOp                         = ILIT( 14)
297 tagOf_PrimOp WordEqOp                         = ILIT( 15)
298 tagOf_PrimOp WordNeOp                         = ILIT( 16)
299 tagOf_PrimOp WordLtOp                         = ILIT( 17)
300 tagOf_PrimOp WordLeOp                         = ILIT( 18)
301 tagOf_PrimOp AddrGtOp                         = ILIT( 19)
302 tagOf_PrimOp AddrGeOp                         = ILIT( 20)
303 tagOf_PrimOp AddrEqOp                         = ILIT( 21)
304 tagOf_PrimOp AddrNeOp                         = ILIT( 22)
305 tagOf_PrimOp AddrLtOp                         = ILIT( 23)
306 tagOf_PrimOp AddrLeOp                         = ILIT( 24)
307 tagOf_PrimOp FloatGtOp                        = ILIT( 25)
308 tagOf_PrimOp FloatGeOp                        = ILIT( 26)
309 tagOf_PrimOp FloatEqOp                        = ILIT( 27)
310 tagOf_PrimOp FloatNeOp                        = ILIT( 28)
311 tagOf_PrimOp FloatLtOp                        = ILIT( 29)
312 tagOf_PrimOp FloatLeOp                        = ILIT( 30)
313 tagOf_PrimOp DoubleGtOp                       = ILIT( 31)
314 tagOf_PrimOp DoubleGeOp                       = ILIT( 32)
315 tagOf_PrimOp DoubleEqOp                       = ILIT( 33)
316 tagOf_PrimOp DoubleNeOp                       = ILIT( 34)
317 tagOf_PrimOp DoubleLtOp                       = ILIT( 35)
318 tagOf_PrimOp DoubleLeOp                       = ILIT( 36)
319 tagOf_PrimOp OrdOp                            = ILIT( 37)
320 tagOf_PrimOp ChrOp                            = ILIT( 38)
321 tagOf_PrimOp IntAddOp                         = ILIT( 39)
322 tagOf_PrimOp IntSubOp                         = ILIT( 40)
323 tagOf_PrimOp IntMulOp                         = ILIT( 41)
324 tagOf_PrimOp IntQuotOp                        = ILIT( 42)
325 tagOf_PrimOp IntRemOp                         = ILIT( 43)
326 tagOf_PrimOp IntNegOp                         = ILIT( 44)
327 tagOf_PrimOp IntAbsOp                         = ILIT( 45)
328 tagOf_PrimOp WordQuotOp                       = ILIT( 46)
329 tagOf_PrimOp WordRemOp                        = ILIT( 47)
330 tagOf_PrimOp AndOp                            = ILIT( 48)
331 tagOf_PrimOp OrOp                             = ILIT( 49)
332 tagOf_PrimOp NotOp                            = ILIT( 50)
333 tagOf_PrimOp XorOp                            = ILIT( 51)
334 tagOf_PrimOp SllOp                            = ILIT( 52)
335 tagOf_PrimOp SrlOp                            = ILIT( 53)
336 tagOf_PrimOp ISllOp                           = ILIT( 54)
337 tagOf_PrimOp ISraOp                           = ILIT( 55)
338 tagOf_PrimOp ISrlOp                           = ILIT( 56)
339 tagOf_PrimOp Int2WordOp                       = ILIT( 57)
340 tagOf_PrimOp Word2IntOp                       = ILIT( 58)
341 tagOf_PrimOp Int2AddrOp                       = ILIT( 59)
342 tagOf_PrimOp Addr2IntOp                       = ILIT( 60)
343
344 tagOf_PrimOp FloatAddOp                       = ILIT( 61)
345 tagOf_PrimOp FloatSubOp                       = ILIT( 62)
346 tagOf_PrimOp FloatMulOp                       = ILIT( 63)
347 tagOf_PrimOp FloatDivOp                       = ILIT( 64)
348 tagOf_PrimOp FloatNegOp                       = ILIT( 65)
349 tagOf_PrimOp Float2IntOp                      = ILIT( 66)
350 tagOf_PrimOp Int2FloatOp                      = ILIT( 67)
351 tagOf_PrimOp FloatExpOp                       = ILIT( 68)
352 tagOf_PrimOp FloatLogOp                       = ILIT( 69)
353 tagOf_PrimOp FloatSqrtOp                      = ILIT( 70)
354 tagOf_PrimOp FloatSinOp                       = ILIT( 71)
355 tagOf_PrimOp FloatCosOp                       = ILIT( 72)
356 tagOf_PrimOp FloatTanOp                       = ILIT( 73)
357 tagOf_PrimOp FloatAsinOp                      = ILIT( 74)
358 tagOf_PrimOp FloatAcosOp                      = ILIT( 75)
359 tagOf_PrimOp FloatAtanOp                      = ILIT( 76)
360 tagOf_PrimOp FloatSinhOp                      = ILIT( 77)
361 tagOf_PrimOp FloatCoshOp                      = ILIT( 78)
362 tagOf_PrimOp FloatTanhOp                      = ILIT( 79)
363 tagOf_PrimOp FloatPowerOp                     = ILIT( 80)
364
365 tagOf_PrimOp DoubleAddOp                      = ILIT( 81)
366 tagOf_PrimOp DoubleSubOp                      = ILIT( 82)
367 tagOf_PrimOp DoubleMulOp                      = ILIT( 83)
368 tagOf_PrimOp DoubleDivOp                      = ILIT( 84)
369 tagOf_PrimOp DoubleNegOp                      = ILIT( 85)
370 tagOf_PrimOp Double2IntOp                     = ILIT( 86)
371 tagOf_PrimOp Int2DoubleOp                     = ILIT( 87)
372 tagOf_PrimOp Double2FloatOp                   = ILIT( 88)
373 tagOf_PrimOp Float2DoubleOp                   = ILIT( 89)
374 tagOf_PrimOp DoubleExpOp                      = ILIT( 90)
375 tagOf_PrimOp DoubleLogOp                      = ILIT( 91)
376 tagOf_PrimOp DoubleSqrtOp                     = ILIT( 92)
377 tagOf_PrimOp DoubleSinOp                      = ILIT( 93)
378 tagOf_PrimOp DoubleCosOp                      = ILIT( 94)
379 tagOf_PrimOp DoubleTanOp                      = ILIT( 95)
380 tagOf_PrimOp DoubleAsinOp                     = ILIT( 96)
381 tagOf_PrimOp DoubleAcosOp                     = ILIT( 97)
382 tagOf_PrimOp DoubleAtanOp                     = ILIT( 98)
383 tagOf_PrimOp DoubleSinhOp                     = ILIT( 99)
384 tagOf_PrimOp DoubleCoshOp                     = ILIT(100)
385 tagOf_PrimOp DoubleTanhOp                     = ILIT(101)
386 tagOf_PrimOp DoublePowerOp                    = ILIT(102)
387
388 tagOf_PrimOp IntegerAddOp                     = ILIT(103)
389 tagOf_PrimOp IntegerSubOp                     = ILIT(104)
390 tagOf_PrimOp IntegerMulOp                     = ILIT(105)
391 tagOf_PrimOp IntegerGcdOp                     = ILIT(106)
392 tagOf_PrimOp IntegerQuotRemOp                 = ILIT(107)
393 tagOf_PrimOp IntegerDivModOp                  = ILIT(108)
394 tagOf_PrimOp IntegerNegOp                     = ILIT(109)
395 tagOf_PrimOp IntegerCmpOp                     = ILIT(110)
396 tagOf_PrimOp Integer2IntOp                    = ILIT(111)
397 tagOf_PrimOp Integer2WordOp                   = ILIT(112)
398 tagOf_PrimOp Int2IntegerOp                    = ILIT(113)
399 tagOf_PrimOp Word2IntegerOp                   = ILIT(114)
400 tagOf_PrimOp Addr2IntegerOp                   = ILIT(115)
401 tagOf_PrimOp IntegerToInt64Op                 = ILIT(116)
402 tagOf_PrimOp Int64ToIntegerOp                 = ILIT(117)
403 tagOf_PrimOp IntegerToWord64Op                = ILIT(118)
404 tagOf_PrimOp Word64ToIntegerOp                = ILIT(119)
405
406 tagOf_PrimOp FloatEncodeOp                    = ILIT(120)
407 tagOf_PrimOp FloatDecodeOp                    = ILIT(121)
408 tagOf_PrimOp DoubleEncodeOp                   = ILIT(122)
409 tagOf_PrimOp DoubleDecodeOp                   = ILIT(123)
410
411 tagOf_PrimOp NewArrayOp                       = ILIT(124)
412 tagOf_PrimOp (NewByteArrayOp CharRep)         = ILIT(125)
413 tagOf_PrimOp (NewByteArrayOp IntRep)          = ILIT(126)
414 tagOf_PrimOp (NewByteArrayOp WordRep)         = ILIT(127)
415 tagOf_PrimOp (NewByteArrayOp AddrRep)         = ILIT(128)
416 tagOf_PrimOp (NewByteArrayOp FloatRep)        = ILIT(129)
417 tagOf_PrimOp (NewByteArrayOp DoubleRep)       = ILIT(130)
418 tagOf_PrimOp (NewByteArrayOp StablePtrRep)    = ILIT(131)
419 tagOf_PrimOp SameMutableArrayOp               = ILIT(132)
420 tagOf_PrimOp SameMutableByteArrayOp           = ILIT(133)
421 tagOf_PrimOp ReadArrayOp                      = ILIT(134)
422 tagOf_PrimOp WriteArrayOp                     = ILIT(135)
423 tagOf_PrimOp IndexArrayOp                     = ILIT(136)
424
425 tagOf_PrimOp (ReadByteArrayOp CharRep)        = ILIT(137)
426 tagOf_PrimOp (ReadByteArrayOp IntRep)         = ILIT(138)
427 tagOf_PrimOp (ReadByteArrayOp WordRep)        = ILIT(139)
428 tagOf_PrimOp (ReadByteArrayOp AddrRep)        = ILIT(140)
429 tagOf_PrimOp (ReadByteArrayOp FloatRep)       = ILIT(141)
430 tagOf_PrimOp (ReadByteArrayOp DoubleRep)      = ILIT(142)
431 tagOf_PrimOp (ReadByteArrayOp StablePtrRep)   = ILIT(143)
432 tagOf_PrimOp (ReadByteArrayOp Int64Rep)       = ILIT(144)
433 tagOf_PrimOp (ReadByteArrayOp Word64Rep)      = ILIT(145)
434
435 tagOf_PrimOp (WriteByteArrayOp CharRep)       = ILIT(146)
436 tagOf_PrimOp (WriteByteArrayOp IntRep)        = ILIT(147)
437 tagOf_PrimOp (WriteByteArrayOp WordRep)       = ILIT(148)
438 tagOf_PrimOp (WriteByteArrayOp AddrRep)       = ILIT(149)
439 tagOf_PrimOp (WriteByteArrayOp FloatRep)      = ILIT(150)
440 tagOf_PrimOp (WriteByteArrayOp DoubleRep)     = ILIT(151)
441 tagOf_PrimOp (WriteByteArrayOp StablePtrRep)  = ILIT(152)
442 tagOf_PrimOp (WriteByteArrayOp Int64Rep)      = ILIT(153)
443 tagOf_PrimOp (WriteByteArrayOp Word64Rep)     = ILIT(154)
444
445 tagOf_PrimOp (IndexByteArrayOp CharRep)       = ILIT(155)
446 tagOf_PrimOp (IndexByteArrayOp IntRep)        = ILIT(156)
447 tagOf_PrimOp (IndexByteArrayOp WordRep)       = ILIT(157)
448 tagOf_PrimOp (IndexByteArrayOp AddrRep)       = ILIT(158)
449 tagOf_PrimOp (IndexByteArrayOp FloatRep)      = ILIT(159)
450 tagOf_PrimOp (IndexByteArrayOp DoubleRep)     = ILIT(160)
451 tagOf_PrimOp (IndexByteArrayOp StablePtrRep)  = ILIT(161)
452 tagOf_PrimOp (IndexByteArrayOp Int64Rep)      = ILIT(162)
453 tagOf_PrimOp (IndexByteArrayOp Word64Rep)     = ILIT(163)
454
455 tagOf_PrimOp (IndexOffAddrOp CharRep)         = ILIT(164)
456 tagOf_PrimOp (IndexOffAddrOp IntRep)          = ILIT(165)
457 tagOf_PrimOp (IndexOffAddrOp WordRep)         = ILIT(166)
458 tagOf_PrimOp (IndexOffAddrOp AddrRep)         = ILIT(167)
459 tagOf_PrimOp (IndexOffAddrOp FloatRep)        = ILIT(168)
460 tagOf_PrimOp (IndexOffAddrOp DoubleRep)       = ILIT(169)
461 tagOf_PrimOp (IndexOffAddrOp StablePtrRep)    = ILIT(170)
462 tagOf_PrimOp (IndexOffAddrOp Int64Rep)        = ILIT(171)
463 tagOf_PrimOp (IndexOffAddrOp Word64Rep)       = ILIT(172)
464 tagOf_PrimOp (IndexOffForeignObjOp CharRep)   = ILIT(173)
465 tagOf_PrimOp (IndexOffForeignObjOp IntRep)    = ILIT(174)
466 tagOf_PrimOp (IndexOffForeignObjOp WordRep)   = ILIT(175)
467 tagOf_PrimOp (IndexOffForeignObjOp AddrRep)   = ILIT(176)
468 tagOf_PrimOp (IndexOffForeignObjOp FloatRep)  = ILIT(177)
469 tagOf_PrimOp (IndexOffForeignObjOp DoubleRep) = ILIT(178)
470 tagOf_PrimOp (IndexOffForeignObjOp StablePtrRep) = ILIT(179)
471 tagOf_PrimOp (IndexOffForeignObjOp Int64Rep)  = ILIT(180)
472 tagOf_PrimOp (IndexOffForeignObjOp Word64Rep) = ILIT(181)
473
474 tagOf_PrimOp (WriteOffAddrOp CharRep)         = ILIT(182)
475 tagOf_PrimOp (WriteOffAddrOp IntRep)          = ILIT(183)
476 tagOf_PrimOp (WriteOffAddrOp WordRep)         = ILIT(184)
477 tagOf_PrimOp (WriteOffAddrOp AddrRep)         = ILIT(185)
478 tagOf_PrimOp (WriteOffAddrOp FloatRep)        = ILIT(186)
479 tagOf_PrimOp (WriteOffAddrOp DoubleRep)       = ILIT(187)
480 tagOf_PrimOp (WriteOffAddrOp StablePtrRep)    = ILIT(188)
481 tagOf_PrimOp (WriteOffAddrOp ForeignObjRep)   = ILIT(189)
482 tagOf_PrimOp (WriteOffAddrOp Int64Rep)        = ILIT(190)
483 tagOf_PrimOp (WriteOffAddrOp Word64Rep)       = ILIT(191)
484
485 tagOf_PrimOp UnsafeFreezeArrayOp              = ILIT(192)
486 tagOf_PrimOp UnsafeFreezeByteArrayOp          = ILIT(193)
487 tagOf_PrimOp SizeofByteArrayOp                = ILIT(194)
488 tagOf_PrimOp SizeofMutableByteArrayOp         = ILIT(195)
489 tagOf_PrimOp NewMVarOp                        = ILIT(196)
490 tagOf_PrimOp TakeMVarOp                       = ILIT(197)
491 tagOf_PrimOp PutMVarOp                        = ILIT(198)
492 tagOf_PrimOp SameMVarOp                       = ILIT(199)
493 tagOf_PrimOp MakeForeignObjOp                 = ILIT(200)
494 tagOf_PrimOp WriteForeignObjOp                = ILIT(201)
495 tagOf_PrimOp MkWeakOp                         = ILIT(202)
496 tagOf_PrimOp DeRefWeakOp                      = ILIT(203)
497 tagOf_PrimOp MakeStablePtrOp                  = ILIT(204)
498 tagOf_PrimOp DeRefStablePtrOp                 = ILIT(205)
499 tagOf_PrimOp EqStablePtrOp                    = ILIT(206)
500 tagOf_PrimOp (CCallOp _ _ _ _)                = ILIT(207)
501 tagOf_PrimOp ReallyUnsafePtrEqualityOp        = ILIT(208)
502 tagOf_PrimOp SeqOp                            = ILIT(209)
503 tagOf_PrimOp ParOp                            = ILIT(210)
504 tagOf_PrimOp ForkOp                           = ILIT(211)
505 tagOf_PrimOp KillThreadOp                     = ILIT(212)
506 tagOf_PrimOp DelayOp                          = ILIT(213)
507 tagOf_PrimOp WaitReadOp                       = ILIT(214)
508 tagOf_PrimOp WaitWriteOp                      = ILIT(215)
509 tagOf_PrimOp ParGlobalOp                      = ILIT(216)
510 tagOf_PrimOp ParLocalOp                       = ILIT(217)
511 tagOf_PrimOp ParAtOp                          = ILIT(218)
512 tagOf_PrimOp ParAtAbsOp                       = ILIT(219)
513 tagOf_PrimOp ParAtRelOp                       = ILIT(220)
514 tagOf_PrimOp ParAtForNowOp                    = ILIT(221)
515 tagOf_PrimOp CopyableOp                       = ILIT(222)
516 tagOf_PrimOp NoFollowOp                       = ILIT(223)
517 tagOf_PrimOp NewMutVarOp                      = ILIT(224)
518 tagOf_PrimOp ReadMutVarOp                     = ILIT(225)
519 tagOf_PrimOp WriteMutVarOp                    = ILIT(226)
520 tagOf_PrimOp SameMutVarOp                     = ILIT(227)
521 tagOf_PrimOp CatchOp                          = ILIT(228)
522 tagOf_PrimOp RaiseOp                          = ILIT(229)
523
524 tagOf_PrimOp op = pprPanic# "tagOf_PrimOp: pattern-match" (ppr op)
525 --panic# "tagOf_PrimOp: pattern-match"
526
527 instance Eq PrimOp where
528     op1 == op2 = tagOf_PrimOp op1 _EQ_ tagOf_PrimOp op2
529
530 instance Ord PrimOp where
531     op1 <  op2 =  tagOf_PrimOp op1 _LT_ tagOf_PrimOp op2
532     op1 <= op2 =  tagOf_PrimOp op1 _LE_ tagOf_PrimOp op2
533     op1 >= op2 =  tagOf_PrimOp op1 _GE_ tagOf_PrimOp op2
534     op1 >  op2 =  tagOf_PrimOp op1 _GT_ tagOf_PrimOp op2
535     op1 `compare` op2 | op1 < op2  = LT
536                       | op1 == op2 = EQ
537                       | otherwise  = GT
538
539 instance Outputable PrimOp where
540     ppr op = pprPrimOp op
541
542 instance Show PrimOp where
543     showsPrec p op = showsPrecSDoc p (pprPrimOp op)
544 \end{code}
545
546 An @Enum@-derived list would be better; meanwhile... (ToDo)
547 \begin{code}
548 allThePrimOps
549   = [   CharGtOp,
550         CharGeOp,
551         CharEqOp,
552         CharNeOp,
553         CharLtOp,
554         CharLeOp,
555         IntGtOp,
556         IntGeOp,
557         IntEqOp,
558         IntNeOp,
559         IntLtOp,
560         IntLeOp,
561         WordGtOp,
562         WordGeOp,
563         WordEqOp,
564         WordNeOp,
565         WordLtOp,
566         WordLeOp,
567         AddrGtOp,
568         AddrGeOp,
569         AddrEqOp,
570         AddrNeOp,
571         AddrLtOp,
572         AddrLeOp,
573         FloatGtOp,
574         FloatGeOp,
575         FloatEqOp,
576         FloatNeOp,
577         FloatLtOp,
578         FloatLeOp,
579         DoubleGtOp,
580         DoubleGeOp,
581         DoubleEqOp,
582         DoubleNeOp,
583         DoubleLtOp,
584         DoubleLeOp,
585         OrdOp,
586         ChrOp,
587         IntAddOp,
588         IntSubOp,
589         IntMulOp,
590         IntQuotOp,
591         IntRemOp,
592         IntNegOp,
593         WordQuotOp,
594         WordRemOp,
595         AndOp,
596         OrOp,
597         NotOp,
598         XorOp,
599         SllOp,
600         SrlOp,
601         ISllOp,
602         ISraOp,
603         ISrlOp,
604         Int2WordOp,
605         Word2IntOp,
606         Int2AddrOp,
607         Addr2IntOp,
608
609         FloatAddOp,
610         FloatSubOp,
611         FloatMulOp,
612         FloatDivOp,
613         FloatNegOp,
614         Float2IntOp,
615         Int2FloatOp,
616         FloatExpOp,
617         FloatLogOp,
618         FloatSqrtOp,
619         FloatSinOp,
620         FloatCosOp,
621         FloatTanOp,
622         FloatAsinOp,
623         FloatAcosOp,
624         FloatAtanOp,
625         FloatSinhOp,
626         FloatCoshOp,
627         FloatTanhOp,
628         FloatPowerOp,
629         DoubleAddOp,
630         DoubleSubOp,
631         DoubleMulOp,
632         DoubleDivOp,
633         DoubleNegOp,
634         Double2IntOp,
635         Int2DoubleOp,
636         Double2FloatOp,
637         Float2DoubleOp,
638         DoubleExpOp,
639         DoubleLogOp,
640         DoubleSqrtOp,
641         DoubleSinOp,
642         DoubleCosOp,
643         DoubleTanOp,
644         DoubleAsinOp,
645         DoubleAcosOp,
646         DoubleAtanOp,
647         DoubleSinhOp,
648         DoubleCoshOp,
649         DoubleTanhOp,
650         DoublePowerOp,
651         IntegerAddOp,
652         IntegerSubOp,
653         IntegerMulOp,
654         IntegerGcdOp,
655         IntegerQuotRemOp,
656         IntegerDivModOp,
657         IntegerNegOp,
658         IntegerCmpOp,
659         Integer2IntOp,
660         Integer2WordOp,
661         Int2IntegerOp,
662         Word2IntegerOp,
663         Addr2IntegerOp,
664         IntegerToInt64Op,
665         Int64ToIntegerOp,
666         IntegerToWord64Op,
667         Word64ToIntegerOp,
668         FloatEncodeOp,
669         FloatDecodeOp,
670         DoubleEncodeOp,
671         DoubleDecodeOp,
672         NewArrayOp,
673         NewByteArrayOp CharRep,
674         NewByteArrayOp IntRep,
675         NewByteArrayOp WordRep,
676         NewByteArrayOp AddrRep,
677         NewByteArrayOp FloatRep,
678         NewByteArrayOp DoubleRep,
679         NewByteArrayOp StablePtrRep,
680         SameMutableArrayOp,
681         SameMutableByteArrayOp,
682         ReadArrayOp,
683         WriteArrayOp,
684         IndexArrayOp,
685         ReadByteArrayOp CharRep,
686         ReadByteArrayOp IntRep,
687         ReadByteArrayOp WordRep,
688         ReadByteArrayOp AddrRep,
689         ReadByteArrayOp FloatRep,
690         ReadByteArrayOp DoubleRep,
691         ReadByteArrayOp StablePtrRep,
692         ReadByteArrayOp Int64Rep,
693         ReadByteArrayOp Word64Rep,
694         WriteByteArrayOp CharRep,
695         WriteByteArrayOp IntRep,
696         WriteByteArrayOp WordRep,
697         WriteByteArrayOp AddrRep,
698         WriteByteArrayOp FloatRep,
699         WriteByteArrayOp DoubleRep,
700         WriteByteArrayOp StablePtrRep,
701         WriteByteArrayOp Int64Rep,
702         WriteByteArrayOp Word64Rep,
703         IndexByteArrayOp CharRep,
704         IndexByteArrayOp IntRep,
705         IndexByteArrayOp WordRep,
706         IndexByteArrayOp AddrRep,
707         IndexByteArrayOp FloatRep,
708         IndexByteArrayOp DoubleRep,
709         IndexByteArrayOp StablePtrRep,
710         IndexByteArrayOp Int64Rep,
711         IndexByteArrayOp Word64Rep,
712         IndexOffForeignObjOp CharRep,
713         IndexOffForeignObjOp AddrRep,
714         IndexOffForeignObjOp IntRep,
715         IndexOffForeignObjOp WordRep,
716         IndexOffForeignObjOp FloatRep,
717         IndexOffForeignObjOp DoubleRep,
718         IndexOffForeignObjOp StablePtrRep,
719         IndexOffForeignObjOp Int64Rep,
720         IndexOffForeignObjOp Word64Rep,
721         IndexOffAddrOp CharRep,
722         IndexOffAddrOp IntRep,
723         IndexOffAddrOp WordRep,
724         IndexOffAddrOp AddrRep,
725         IndexOffAddrOp FloatRep,
726         IndexOffAddrOp DoubleRep,
727         IndexOffAddrOp StablePtrRep,
728         IndexOffAddrOp Int64Rep,
729         IndexOffAddrOp Word64Rep,
730         WriteOffAddrOp CharRep,
731         WriteOffAddrOp IntRep,
732         WriteOffAddrOp WordRep,
733         WriteOffAddrOp AddrRep,
734         WriteOffAddrOp FloatRep,
735         WriteOffAddrOp DoubleRep,
736         WriteOffAddrOp ForeignObjRep,
737         WriteOffAddrOp StablePtrRep,
738         WriteOffAddrOp Int64Rep,
739         WriteOffAddrOp Word64Rep,
740         UnsafeFreezeArrayOp,
741         UnsafeFreezeByteArrayOp,
742         SizeofByteArrayOp,
743         SizeofMutableByteArrayOp,
744         NewMutVarOp,
745         ReadMutVarOp,
746         WriteMutVarOp,
747         SameMutVarOp,
748         CatchOp,
749         RaiseOp,
750         NewMVarOp,
751         TakeMVarOp,
752         PutMVarOp,
753         SameMVarOp,
754         MakeForeignObjOp,
755         WriteForeignObjOp,
756         MkWeakOp,
757         DeRefWeakOp,
758         MakeStablePtrOp,
759         DeRefStablePtrOp,
760         EqStablePtrOp,
761         ReallyUnsafePtrEqualityOp,
762         ParGlobalOp,
763         ParLocalOp,
764         ParAtOp,
765         ParAtAbsOp,
766         ParAtRelOp,
767         ParAtForNowOp,
768         CopyableOp,
769         NoFollowOp,
770         SeqOp,
771         ParOp,
772         ForkOp,
773         KillThreadOp,
774         DelayOp,
775         WaitReadOp,
776         WaitWriteOp
777     ]
778 \end{code}
779
780 %************************************************************************
781 %*                                                                      *
782 \subsection[PrimOp-info]{The essential info about each @PrimOp@}
783 %*                                                                      *
784 %************************************************************************
785
786 The @String@ in the @PrimOpInfos@ is the ``base name'' by which the user may
787 refer to the primitive operation.  The conventional \tr{#}-for-
788 unboxed ops is added on later.
789
790 The reason for the funny characters in the names is so we do not
791 interfere with the programmer's Haskell name spaces.
792
793 We use @PrimKinds@ for the ``type'' information, because they're
794 (slightly) more convenient to use than @TyCons@.
795 \begin{code}
796 data PrimOpInfo
797   = Dyadic      OccName         -- string :: T -> T -> T
798                 Type
799   | Monadic     OccName         -- string :: T -> T
800                 Type
801   | Compare     OccName         -- string :: T -> T -> Bool
802                 Type
803
804   | GenPrimOp   OccName         -- string :: \/a1..an . T1 -> .. -> Tk -> T
805                 [TyVar] 
806                 [Type] 
807                 Type 
808
809 mkDyadic str  ty = Dyadic  (varOcc str) ty
810 mkMonadic str ty = Monadic (varOcc str) ty
811 mkCompare str ty = Compare (varOcc str) ty
812 mkGenPrimOp str tvs tys ty = GenPrimOp (varOcc str) tvs tys ty
813 \end{code}
814
815 Utility bits:
816 \begin{code}
817 one_Integer_ty = [intPrimTy, intPrimTy, byteArrayPrimTy]
818 two_Integer_tys
819   = [intPrimTy, intPrimTy, byteArrayPrimTy, -- first Integer pieces
820      intPrimTy, intPrimTy, byteArrayPrimTy] -- second '' pieces
821 an_Integer_and_Int_tys
822   = [intPrimTy, intPrimTy, byteArrayPrimTy, -- Integer
823      intPrimTy]
824
825 unboxedPair      = mkUnboxedTupleTy 2
826 unboxedTriple    = mkUnboxedTupleTy 3
827 unboxedQuadruple = mkUnboxedTupleTy 4
828 unboxedSexTuple  = mkUnboxedTupleTy 6
829
830 integerMonadic name = mkGenPrimOp name [] one_Integer_ty 
831                         (unboxedTriple [intPrimTy, intPrimTy, byteArrayPrimTy])
832
833 integerDyadic name = mkGenPrimOp name [] two_Integer_tys 
834                         (unboxedTriple [intPrimTy, intPrimTy, byteArrayPrimTy])
835
836 integerDyadic2Results name = mkGenPrimOp name [] two_Integer_tys 
837     (unboxedSexTuple [intPrimTy, intPrimTy, byteArrayPrimTy, 
838                       intPrimTy, intPrimTy, byteArrayPrimTy])
839
840 integerCompare name = mkGenPrimOp name [] two_Integer_tys intPrimTy
841 \end{code}
842
843 %************************************************************************
844 %*                                                                      *
845 \subsubsection{Strictness}
846 %*                                                                      *
847 %************************************************************************
848
849 Not all primops are strict!
850
851 \begin{code}
852 primOpStrictness :: PrimOp -> ([Demand], Bool)
853         -- See IdInfo.StrictnessInfo for discussion of what the results
854         -- **NB** as a cheap hack, to avoid having to look up the PrimOp's arity,
855         -- the list of demands may be infinite!
856         -- Use only the ones you ned.
857
858 primOpStrictness SeqOp            = ([wwLazy], False)
859 primOpStrictness WriteArrayOp     = ([wwPrim, wwPrim, wwLazy, wwPrim], False)
860 primOpStrictness WriteMutVarOp    = ([wwPrim, wwLazy, wwPrim], False)
861 primOpStrictness PutMVarOp        = ([wwPrim, wwLazy, wwPrim], False)
862 primOpStrictness CatchOp          = ([wwLazy, wwLazy], False)
863 primOpStrictness RaiseOp          = ([wwLazy], True)    -- NB: True => result is bottom
864 primOpStrictness MkWeakOp         = ([wwLazy, wwLazy, wwLazy, wwPrim], False)
865 primOpStrictness MakeStablePtrOp  = ([wwLazy, wwPrim], False)
866 primOpStrictness other            = (repeat wwPrim, False)
867 \end{code}
868
869 %************************************************************************
870 %*                                                                      *
871 \subsubsection[PrimOp-comparison]{PrimOpInfo basic comparison ops}
872 %*                                                                      *
873 %************************************************************************
874
875 @primOpInfo@ gives all essential information (from which everything
876 else, notably a type, can be constructed) for each @PrimOp@.
877
878 \begin{code}
879 primOpInfo :: PrimOp -> PrimOpInfo
880 \end{code}
881
882 There's plenty of this stuff!
883
884 \begin{code}
885 primOpInfo CharGtOp   = mkCompare SLIT("gtChar#")   charPrimTy
886 primOpInfo CharGeOp   = mkCompare SLIT("geChar#")   charPrimTy
887 primOpInfo CharEqOp   = mkCompare SLIT("eqChar#")   charPrimTy
888 primOpInfo CharNeOp   = mkCompare SLIT("neChar#")   charPrimTy
889 primOpInfo CharLtOp   = mkCompare SLIT("ltChar#")   charPrimTy
890 primOpInfo CharLeOp   = mkCompare SLIT("leChar#")   charPrimTy
891
892 primOpInfo IntGtOp    = mkCompare SLIT(">#")       intPrimTy
893 primOpInfo IntGeOp    = mkCompare SLIT(">=#")      intPrimTy
894 primOpInfo IntEqOp    = mkCompare SLIT("==#")      intPrimTy
895 primOpInfo IntNeOp    = mkCompare SLIT("/=#")      intPrimTy
896 primOpInfo IntLtOp    = mkCompare SLIT("<#")       intPrimTy
897 primOpInfo IntLeOp    = mkCompare SLIT("<=#")      intPrimTy
898
899 primOpInfo WordGtOp   = mkCompare SLIT("gtWord#")   wordPrimTy
900 primOpInfo WordGeOp   = mkCompare SLIT("geWord#")   wordPrimTy
901 primOpInfo WordEqOp   = mkCompare SLIT("eqWord#")   wordPrimTy
902 primOpInfo WordNeOp   = mkCompare SLIT("neWord#")   wordPrimTy
903 primOpInfo WordLtOp   = mkCompare SLIT("ltWord#")   wordPrimTy
904 primOpInfo WordLeOp   = mkCompare SLIT("leWord#")   wordPrimTy
905
906 primOpInfo AddrGtOp   = mkCompare SLIT("gtAddr#")   addrPrimTy
907 primOpInfo AddrGeOp   = mkCompare SLIT("geAddr#")   addrPrimTy
908 primOpInfo AddrEqOp   = mkCompare SLIT("eqAddr#")   addrPrimTy
909 primOpInfo AddrNeOp   = mkCompare SLIT("neAddr#")   addrPrimTy
910 primOpInfo AddrLtOp   = mkCompare SLIT("ltAddr#")   addrPrimTy
911 primOpInfo AddrLeOp   = mkCompare SLIT("leAddr#")   addrPrimTy
912
913 primOpInfo FloatGtOp  = mkCompare SLIT("gtFloat#")  floatPrimTy
914 primOpInfo FloatGeOp  = mkCompare SLIT("geFloat#")  floatPrimTy
915 primOpInfo FloatEqOp  = mkCompare SLIT("eqFloat#")  floatPrimTy
916 primOpInfo FloatNeOp  = mkCompare SLIT("neFloat#")  floatPrimTy
917 primOpInfo FloatLtOp  = mkCompare SLIT("ltFloat#")  floatPrimTy
918 primOpInfo FloatLeOp  = mkCompare SLIT("leFloat#")  floatPrimTy
919
920 primOpInfo DoubleGtOp = mkCompare SLIT(">##") doublePrimTy
921 primOpInfo DoubleGeOp = mkCompare SLIT(">=##") doublePrimTy
922 primOpInfo DoubleEqOp = mkCompare SLIT("==##") doublePrimTy
923 primOpInfo DoubleNeOp = mkCompare SLIT("/=##") doublePrimTy
924 primOpInfo DoubleLtOp = mkCompare SLIT("<##") doublePrimTy
925 primOpInfo DoubleLeOp = mkCompare SLIT("<=##") doublePrimTy
926
927 \end{code}
928
929 %************************************************************************
930 %*                                                                      *
931 \subsubsection[PrimOp-Char]{PrimOpInfo for @Char#@s}
932 %*                                                                      *
933 %************************************************************************
934
935 \begin{code}
936 primOpInfo OrdOp = mkGenPrimOp SLIT("ord#") [] [charPrimTy] intPrimTy
937 primOpInfo ChrOp = mkGenPrimOp SLIT("chr#") [] [intPrimTy]  charPrimTy
938 \end{code}
939
940 %************************************************************************
941 %*                                                                      *
942 \subsubsection[PrimOp-Int]{PrimOpInfo for @Int#@s}
943 %*                                                                      *
944 %************************************************************************
945
946 \begin{code}
947 primOpInfo IntAddOp  = mkDyadic SLIT("+#")       intPrimTy
948 primOpInfo IntSubOp  = mkDyadic SLIT("-#") intPrimTy
949 primOpInfo IntMulOp  = mkDyadic SLIT("*#") intPrimTy
950 primOpInfo IntQuotOp = mkDyadic SLIT("quotInt#")         intPrimTy
951 primOpInfo IntRemOp  = mkDyadic SLIT("remInt#")  intPrimTy
952
953 primOpInfo IntNegOp  = mkMonadic SLIT("negateInt#") intPrimTy
954 primOpInfo IntAbsOp  = mkMonadic SLIT("absInt#") intPrimTy
955 \end{code}
956
957 %************************************************************************
958 %*                                                                      *
959 \subsubsection[PrimOp-Word]{PrimOpInfo for @Word#@s}
960 %*                                                                      *
961 %************************************************************************
962
963 A @Word#@ is an unsigned @Int#@.
964
965 \begin{code}
966 primOpInfo WordQuotOp = mkDyadic SLIT("quotWord#") wordPrimTy
967 primOpInfo WordRemOp  = mkDyadic SLIT("remWord#")        wordPrimTy
968
969 primOpInfo AndOp    = mkDyadic  SLIT("and#")    wordPrimTy
970 primOpInfo OrOp     = mkDyadic  SLIT("or#")     wordPrimTy
971 primOpInfo XorOp    = mkDyadic  SLIT("xor#")    wordPrimTy
972 primOpInfo NotOp    = mkMonadic SLIT("not#")    wordPrimTy
973
974 primOpInfo SllOp
975   = mkGenPrimOp SLIT("shiftL#")  [] [wordPrimTy, intPrimTy] wordPrimTy
976 primOpInfo SrlOp
977   = mkGenPrimOp SLIT("shiftRL#") [] [wordPrimTy, intPrimTy] wordPrimTy
978
979 primOpInfo ISllOp
980   = mkGenPrimOp SLIT("iShiftL#")  [] [intPrimTy, intPrimTy] intPrimTy
981 primOpInfo ISraOp
982   = mkGenPrimOp SLIT("iShiftRA#") [] [intPrimTy, intPrimTy] intPrimTy
983 primOpInfo ISrlOp
984   = mkGenPrimOp SLIT("iShiftRL#") [] [intPrimTy, intPrimTy] intPrimTy
985
986 primOpInfo Int2WordOp = mkGenPrimOp SLIT("int2Word#") [] [intPrimTy] wordPrimTy
987 primOpInfo Word2IntOp = mkGenPrimOp SLIT("word2Int#") [] [wordPrimTy] intPrimTy
988 \end{code}
989
990 %************************************************************************
991 %*                                                                      *
992 \subsubsection[PrimOp-Addr]{PrimOpInfo for @Addr#@s}
993 %*                                                                      *
994 %************************************************************************
995
996 \begin{code}
997 primOpInfo Int2AddrOp = mkGenPrimOp SLIT("int2Addr#") [] [intPrimTy] addrPrimTy
998 primOpInfo Addr2IntOp = mkGenPrimOp SLIT("addr2Int#") [] [addrPrimTy] intPrimTy
999 \end{code}
1000
1001
1002 %************************************************************************
1003 %*                                                                      *
1004 \subsubsection[PrimOp-Float]{PrimOpInfo for @Float#@s}
1005 %*                                                                      *
1006 %************************************************************************
1007
1008 @encodeFloat#@ and @decodeFloat#@ are given w/ Integer-stuff (it's
1009 similar).
1010
1011 \begin{code}
1012 primOpInfo FloatAddOp   = mkDyadic    SLIT("plusFloat#")           floatPrimTy
1013 primOpInfo FloatSubOp   = mkDyadic    SLIT("minusFloat#")   floatPrimTy
1014 primOpInfo FloatMulOp   = mkDyadic    SLIT("timesFloat#")   floatPrimTy
1015 primOpInfo FloatDivOp   = mkDyadic    SLIT("divideFloat#")  floatPrimTy
1016 primOpInfo FloatNegOp   = mkMonadic   SLIT("negateFloat#")  floatPrimTy
1017
1018 primOpInfo Float2IntOp  = mkGenPrimOp SLIT("float2Int#") [] [floatPrimTy] intPrimTy
1019 primOpInfo Int2FloatOp  = mkGenPrimOp SLIT("int2Float#") [] [intPrimTy] floatPrimTy
1020
1021 primOpInfo FloatExpOp   = mkMonadic   SLIT("expFloat#")    floatPrimTy
1022 primOpInfo FloatLogOp   = mkMonadic   SLIT("logFloat#")    floatPrimTy
1023 primOpInfo FloatSqrtOp  = mkMonadic   SLIT("sqrtFloat#")           floatPrimTy
1024 primOpInfo FloatSinOp   = mkMonadic   SLIT("sinFloat#")    floatPrimTy
1025 primOpInfo FloatCosOp   = mkMonadic   SLIT("cosFloat#")    floatPrimTy
1026 primOpInfo FloatTanOp   = mkMonadic   SLIT("tanFloat#")    floatPrimTy
1027 primOpInfo FloatAsinOp  = mkMonadic   SLIT("asinFloat#")           floatPrimTy
1028 primOpInfo FloatAcosOp  = mkMonadic   SLIT("acosFloat#")           floatPrimTy
1029 primOpInfo FloatAtanOp  = mkMonadic   SLIT("atanFloat#")           floatPrimTy
1030 primOpInfo FloatSinhOp  = mkMonadic   SLIT("sinhFloat#")           floatPrimTy
1031 primOpInfo FloatCoshOp  = mkMonadic   SLIT("coshFloat#")           floatPrimTy
1032 primOpInfo FloatTanhOp  = mkMonadic   SLIT("tanhFloat#")           floatPrimTy
1033 primOpInfo FloatPowerOp = mkDyadic    SLIT("powerFloat#")   floatPrimTy
1034 \end{code}
1035
1036 %************************************************************************
1037 %*                                                                      *
1038 \subsubsection[PrimOp-Double]{PrimOpInfo for @Double#@s}
1039 %*                                                                      *
1040 %************************************************************************
1041
1042 @encodeDouble#@ and @decodeDouble#@ are given w/ Integer-stuff (it's
1043 similar).
1044
1045 \begin{code}
1046 primOpInfo DoubleAddOp  = mkDyadic    SLIT("+##")   doublePrimTy
1047 primOpInfo DoubleSubOp  = mkDyadic    SLIT("-##")  doublePrimTy
1048 primOpInfo DoubleMulOp  = mkDyadic    SLIT("*##")  doublePrimTy
1049 primOpInfo DoubleDivOp  = mkDyadic    SLIT("/##") doublePrimTy
1050 primOpInfo DoubleNegOp  = mkMonadic   SLIT("negateDouble#") doublePrimTy
1051
1052 primOpInfo Double2IntOp     = mkGenPrimOp SLIT("double2Int#") [] [doublePrimTy] intPrimTy
1053 primOpInfo Int2DoubleOp     = mkGenPrimOp SLIT("int2Double#") [] [intPrimTy] doublePrimTy
1054
1055 primOpInfo Double2FloatOp   = mkGenPrimOp SLIT("double2Float#") [] [doublePrimTy] floatPrimTy
1056 primOpInfo Float2DoubleOp   = mkGenPrimOp SLIT("float2Double#") [] [floatPrimTy] doublePrimTy
1057
1058 primOpInfo DoubleExpOp  = mkMonadic   SLIT("expDouble#")           doublePrimTy
1059 primOpInfo DoubleLogOp  = mkMonadic   SLIT("logDouble#")           doublePrimTy
1060 primOpInfo DoubleSqrtOp = mkMonadic   SLIT("sqrtDouble#")   doublePrimTy
1061 primOpInfo DoubleSinOp  = mkMonadic   SLIT("sinDouble#")           doublePrimTy
1062 primOpInfo DoubleCosOp  = mkMonadic   SLIT("cosDouble#")           doublePrimTy
1063 primOpInfo DoubleTanOp  = mkMonadic   SLIT("tanDouble#")           doublePrimTy
1064 primOpInfo DoubleAsinOp = mkMonadic   SLIT("asinDouble#")   doublePrimTy
1065 primOpInfo DoubleAcosOp = mkMonadic   SLIT("acosDouble#")   doublePrimTy
1066 primOpInfo DoubleAtanOp = mkMonadic   SLIT("atanDouble#")   doublePrimTy
1067 primOpInfo DoubleSinhOp = mkMonadic   SLIT("sinhDouble#")   doublePrimTy
1068 primOpInfo DoubleCoshOp = mkMonadic   SLIT("coshDouble#")   doublePrimTy
1069 primOpInfo DoubleTanhOp = mkMonadic   SLIT("tanhDouble#")   doublePrimTy
1070 primOpInfo DoublePowerOp= mkDyadic    SLIT("**##")  doublePrimTy
1071 \end{code}
1072
1073 %************************************************************************
1074 %*                                                                      *
1075 \subsubsection[PrimOp-Integer]{PrimOpInfo for @Integer@ (and related!)}
1076 %*                                                                      *
1077 %************************************************************************
1078
1079 \begin{code}
1080 primOpInfo IntegerNegOp = integerMonadic SLIT("negateInteger#")
1081
1082 primOpInfo IntegerAddOp = integerDyadic SLIT("plusInteger#")
1083 primOpInfo IntegerSubOp = integerDyadic SLIT("minusInteger#")
1084 primOpInfo IntegerMulOp = integerDyadic SLIT("timesInteger#")
1085 primOpInfo IntegerGcdOp = integerDyadic SLIT("gcdInteger#")
1086
1087 primOpInfo IntegerCmpOp = integerCompare SLIT("cmpInteger#")
1088
1089 primOpInfo IntegerQuotRemOp = integerDyadic2Results SLIT("quotRemInteger#")
1090 primOpInfo IntegerDivModOp  = integerDyadic2Results SLIT("divModInteger#")
1091
1092 primOpInfo Integer2IntOp
1093   = mkGenPrimOp SLIT("integer2Int#") [] one_Integer_ty intPrimTy
1094
1095 primOpInfo Integer2WordOp
1096   = mkGenPrimOp SLIT("integer2Word#") [] one_Integer_ty wordPrimTy
1097
1098 primOpInfo Int2IntegerOp
1099   = mkGenPrimOp SLIT("int2Integer#") [] [intPrimTy] 
1100                         (unboxedTriple [intPrimTy, intPrimTy, byteArrayPrimTy])
1101
1102 primOpInfo Word2IntegerOp
1103   = mkGenPrimOp SLIT("word2Integer#") [] [wordPrimTy] 
1104                         (unboxedTriple [intPrimTy, intPrimTy, byteArrayPrimTy])
1105
1106 primOpInfo Addr2IntegerOp
1107   = mkGenPrimOp SLIT("addr2Integer#") [] [addrPrimTy] 
1108                         (unboxedTriple [intPrimTy, intPrimTy, byteArrayPrimTy])
1109
1110 primOpInfo IntegerToInt64Op
1111   = mkGenPrimOp SLIT("integerToInt64#") [] one_Integer_ty int64PrimTy
1112
1113 primOpInfo Int64ToIntegerOp
1114   = mkGenPrimOp SLIT("int64ToInteger#") [] [int64PrimTy]
1115                         (unboxedTriple [intPrimTy, intPrimTy, byteArrayPrimTy])
1116
1117 primOpInfo Word64ToIntegerOp
1118   = mkGenPrimOp SLIT("word64ToInteger#") [] [word64PrimTy] 
1119                         (unboxedTriple [intPrimTy, intPrimTy, byteArrayPrimTy])
1120
1121 primOpInfo IntegerToWord64Op
1122   = mkGenPrimOp SLIT("integerToWord64#") [] one_Integer_ty word64PrimTy
1123 \end{code}
1124
1125 Encoding and decoding of floating-point numbers is sorta
1126 Integer-related.
1127
1128 \begin{code}
1129 primOpInfo FloatEncodeOp
1130   = mkGenPrimOp SLIT("encodeFloat#") [] an_Integer_and_Int_tys floatPrimTy
1131
1132 primOpInfo DoubleEncodeOp
1133   = mkGenPrimOp SLIT("encodeDouble#") [] an_Integer_and_Int_tys doublePrimTy
1134
1135 primOpInfo FloatDecodeOp
1136   = mkGenPrimOp SLIT("decodeFloat#") [] [floatPrimTy] 
1137         (unboxedQuadruple [intPrimTy, intPrimTy, intPrimTy, byteArrayPrimTy])
1138 primOpInfo DoubleDecodeOp
1139   = mkGenPrimOp SLIT("decodeDouble#") [] [doublePrimTy] 
1140         (unboxedQuadruple [intPrimTy, intPrimTy, intPrimTy, byteArrayPrimTy])
1141 \end{code}
1142
1143 %************************************************************************
1144 %*                                                                      *
1145 \subsubsection[PrimOp-Arrays]{PrimOpInfo for primitive arrays}
1146 %*                                                                      *
1147 %************************************************************************
1148
1149 \begin{code}
1150 primOpInfo NewArrayOp
1151   = let {
1152         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1153         state = mkStatePrimTy s
1154     } in
1155     mkGenPrimOp SLIT("newArray#") [s_tv, elt_tv] 
1156         [intPrimTy, elt, state]
1157         (unboxedPair [state, mkMutableArrayPrimTy s elt])
1158
1159 primOpInfo (NewByteArrayOp kind)
1160   = let
1161         s = alphaTy; s_tv = alphaTyVar
1162
1163         op_str         = _PK_ ("new" ++ primRepString kind ++ "Array#")
1164         state = mkStatePrimTy s
1165     in
1166     mkGenPrimOp op_str [s_tv]
1167         [intPrimTy, state]
1168         (unboxedPair [state, mkMutableByteArrayPrimTy s])
1169
1170 ---------------------------------------------------------------------------
1171
1172 primOpInfo SameMutableArrayOp
1173   = let {
1174         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1175         mut_arr_ty = mkMutableArrayPrimTy s elt
1176     } in
1177     mkGenPrimOp SLIT("sameMutableArray#") [s_tv, elt_tv] [mut_arr_ty, mut_arr_ty]
1178                                    boolTy
1179
1180 primOpInfo SameMutableByteArrayOp
1181   = let {
1182         s = alphaTy; s_tv = alphaTyVar;
1183         mut_arr_ty = mkMutableByteArrayPrimTy s
1184     } in
1185     mkGenPrimOp SLIT("sameMutableByteArray#") [s_tv] [mut_arr_ty, mut_arr_ty]
1186                                    boolTy
1187
1188 ---------------------------------------------------------------------------
1189 -- Primitive arrays of Haskell pointers:
1190
1191 primOpInfo ReadArrayOp
1192   = let {
1193         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1194         state = mkStatePrimTy s
1195     } in
1196     mkGenPrimOp SLIT("readArray#") [s_tv, elt_tv]
1197         [mkMutableArrayPrimTy s elt, intPrimTy, state]
1198         (unboxedPair [state, elt])
1199
1200
1201 primOpInfo WriteArrayOp
1202   = let {
1203         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1204     } in
1205     mkGenPrimOp SLIT("writeArray#") [s_tv, elt_tv]
1206         [mkMutableArrayPrimTy s elt, intPrimTy, elt, mkStatePrimTy s]
1207         (mkStatePrimTy s)
1208
1209 primOpInfo IndexArrayOp
1210   = let { elt = alphaTy; elt_tv = alphaTyVar } in
1211     mkGenPrimOp SLIT("indexArray#") [elt_tv] [mkArrayPrimTy elt, intPrimTy]
1212         (unboxedPair [realWorldStatePrimTy, elt])
1213
1214 ---------------------------------------------------------------------------
1215 -- Primitive arrays full of unboxed bytes:
1216
1217 primOpInfo (ReadByteArrayOp kind)
1218   = let
1219         s = alphaTy; s_tv = alphaTyVar
1220
1221         op_str         = _PK_ ("read" ++ primRepString kind ++ "Array#")
1222         relevant_type  = assoc "primOpInfo{ReadByteArrayOp}" tbl kind
1223         state          = mkStatePrimTy s
1224
1225         tvs
1226           | kind == StablePtrRep = [s_tv, betaTyVar]
1227           | otherwise            = [s_tv]
1228     in
1229     mkGenPrimOp op_str tvs
1230         [mkMutableByteArrayPrimTy s, intPrimTy, state]
1231         (unboxedPair [state, relevant_type])
1232   where
1233     tbl = [ (CharRep,    charPrimTy),
1234             (IntRep,     intPrimTy),
1235             (WordRep,    wordPrimTy),
1236             (AddrRep,    addrPrimTy),
1237             (FloatRep,   floatPrimTy),
1238             (StablePtrRep, mkStablePtrPrimTy betaTy),
1239             (DoubleRep,  doublePrimTy) ]
1240
1241   -- How come there's no Word byte arrays? ADR
1242
1243 primOpInfo (WriteByteArrayOp kind)
1244   = let
1245         s = alphaTy; s_tv = alphaTyVar
1246         op_str = _PK_ ("write" ++ primRepString kind ++ "Array#")
1247         prim_ty = mkTyConApp (primRepTyCon kind) []
1248
1249         (the_prim_ty, tvs)
1250           | kind == StablePtrRep = (mkStablePtrPrimTy betaTy, [s_tv, betaTyVar])
1251           | otherwise            = (prim_ty, [s_tv])
1252
1253     in
1254     mkGenPrimOp op_str tvs
1255         [mkMutableByteArrayPrimTy s, intPrimTy, the_prim_ty, mkStatePrimTy s]
1256         (mkStatePrimTy s)
1257
1258 primOpInfo (IndexByteArrayOp kind)
1259   = let
1260         op_str = _PK_ ("index" ++ primRepString kind ++ "Array#")
1261
1262         (prim_tycon_args, tvs)
1263           | kind == StablePtrRep = ([alphaTy], [alphaTyVar])
1264           | otherwise            = ([],[])
1265     in
1266     mkGenPrimOp op_str tvs [byteArrayPrimTy, intPrimTy] 
1267         (mkTyConApp (primRepTyCon kind) prim_tycon_args)
1268
1269 primOpInfo (IndexOffForeignObjOp kind)
1270   = let
1271         op_str = _PK_ ("index" ++ primRepString kind ++ "OffForeignObj#")
1272
1273         (prim_tycon_args, tvs)
1274           | kind == StablePtrRep = ([alphaTy], [alphaTyVar])
1275           | otherwise            = ([], [])
1276     in
1277     mkGenPrimOp op_str tvs [foreignObjPrimTy, intPrimTy] 
1278         (mkTyConApp (primRepTyCon kind) prim_tycon_args)
1279
1280 primOpInfo (IndexOffAddrOp kind)
1281   = let
1282         op_str = _PK_ ("index" ++ primRepString kind ++ "OffAddr#")
1283
1284         (prim_tycon_args, tvs)
1285           | kind == StablePtrRep = ([alphaTy], [alphaTyVar])
1286           | otherwise            = ([], [])
1287     in
1288     mkGenPrimOp op_str tvs [addrPrimTy, intPrimTy] 
1289         (mkTyConApp (primRepTyCon kind) prim_tycon_args)
1290
1291 primOpInfo (WriteOffAddrOp kind)
1292   = let
1293         s = alphaTy; s_tv = alphaTyVar
1294         op_str = _PK_ ("write" ++ primRepString kind ++ "OffAddr#")
1295         prim_ty = mkTyConApp (primRepTyCon kind) []
1296     in
1297     mkGenPrimOp op_str [s_tv]
1298         [addrPrimTy, intPrimTy, prim_ty, mkStatePrimTy s]
1299         (mkStatePrimTy s)
1300
1301 ---------------------------------------------------------------------------
1302 primOpInfo UnsafeFreezeArrayOp
1303   = let {
1304         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1305         state = mkStatePrimTy s
1306     } in
1307     mkGenPrimOp SLIT("unsafeFreezeArray#") [s_tv, elt_tv]
1308         [mkMutableArrayPrimTy s elt, state]
1309         (unboxedPair [state, mkArrayPrimTy elt])
1310
1311 primOpInfo UnsafeFreezeByteArrayOp
1312   = let { 
1313         s = alphaTy; s_tv = alphaTyVar;
1314         state = mkStatePrimTy s
1315     } in
1316     mkGenPrimOp SLIT("unsafeFreezeByteArray#") [s_tv]
1317         [mkMutableByteArrayPrimTy s, state]
1318         (unboxedPair [state, byteArrayPrimTy])
1319
1320 ---------------------------------------------------------------------------
1321 primOpInfo SizeofByteArrayOp
1322   = mkGenPrimOp
1323         SLIT("sizeofByteArray#") []
1324         [byteArrayPrimTy]
1325         intPrimTy
1326
1327 primOpInfo SizeofMutableByteArrayOp
1328   = let { s = alphaTy; s_tv = alphaTyVar } in
1329     mkGenPrimOp
1330         SLIT("sizeofMutableByteArray#") [s_tv]
1331         [mkMutableByteArrayPrimTy s]
1332         intPrimTy
1333 \end{code}
1334
1335
1336 %************************************************************************
1337 %*                                                                      *
1338 \subsubsection[PrimOp-MutVars]{PrimOpInfo for mutable variable ops}
1339 %*                                                                      *
1340 %************************************************************************
1341
1342 \begin{code}
1343 primOpInfo NewMutVarOp
1344   = let {
1345         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1346         state = mkStatePrimTy s
1347     } in
1348     mkGenPrimOp SLIT("newMutVar#") [s_tv, elt_tv] 
1349         [elt, state]
1350         (unboxedPair [state, mkMutVarPrimTy s elt])
1351
1352 primOpInfo ReadMutVarOp
1353   = let {
1354         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1355         state = mkStatePrimTy s
1356     } in
1357     mkGenPrimOp SLIT("readMutVar#") [s_tv, elt_tv]
1358         [mkMutVarPrimTy s elt, state]
1359         (unboxedPair [state, elt])
1360
1361
1362 primOpInfo WriteMutVarOp
1363   = let {
1364         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1365     } in
1366     mkGenPrimOp SLIT("writeMutVar#") [s_tv, elt_tv]
1367         [mkMutVarPrimTy s elt, elt, mkStatePrimTy s]
1368         (mkStatePrimTy s)
1369
1370 primOpInfo SameMutVarOp
1371   = let {
1372         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1373         mut_var_ty = mkMutVarPrimTy s elt
1374     } in
1375     mkGenPrimOp SLIT("sameMutVar#") [s_tv, elt_tv] [mut_var_ty, mut_var_ty]
1376                                    boolTy
1377 \end{code}
1378
1379 %************************************************************************
1380 %*                                                                      *
1381 \subsubsection[PrimOp-Exceptions]{PrimOpInfo for exceptions}
1382 %*                                                                      *
1383 %************************************************************************
1384
1385 catch :: IO a -> (IOError -> IO a) -> IO a
1386 catch :: a  -> (b -> a) -> a
1387
1388 \begin{code}
1389 primOpInfo CatchOp   
1390   = let
1391         a = alphaTy; a_tv = alphaTyVar;
1392         b = betaTy;  b_tv = betaTyVar;
1393     in
1394     mkGenPrimOp SLIT("catch#") [a_tv, b_tv] [a, mkFunTy b a] a
1395
1396 primOpInfo RaiseOp
1397   = let
1398         a = alphaTy; a_tv = alphaTyVar;
1399         b = betaTy;  b_tv = betaTyVar;
1400     in
1401     mkGenPrimOp SLIT("raise#") [a_tv, b_tv] [a] b
1402 \end{code}
1403
1404 %************************************************************************
1405 %*                                                                      *
1406 \subsubsection[PrimOp-MVars]{PrimOpInfo for synchronizing Variables}
1407 %*                                                                      *
1408 %************************************************************************
1409
1410 \begin{code}
1411 primOpInfo NewMVarOp
1412   = let
1413         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1414         state = mkStatePrimTy s
1415     in
1416     mkGenPrimOp SLIT("newMVar#") [s_tv, elt_tv] [state]
1417         (unboxedPair [state, mkMVarPrimTy s elt])
1418
1419 primOpInfo TakeMVarOp
1420   = let
1421         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1422         state = mkStatePrimTy s
1423     in
1424     mkGenPrimOp SLIT("takeMVar#") [s_tv, elt_tv]
1425         [mkMVarPrimTy s elt, state]
1426         (unboxedPair [state, elt])
1427
1428 primOpInfo PutMVarOp
1429   = let
1430         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1431     in
1432     mkGenPrimOp SLIT("putMVar#") [s_tv, elt_tv]
1433         [mkMVarPrimTy s elt, elt, mkStatePrimTy s]
1434         (mkStatePrimTy s)
1435
1436 primOpInfo SameMVarOp
1437   = let
1438         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1439         mvar_ty = mkMVarPrimTy s elt
1440     in
1441     mkGenPrimOp SLIT("sameMVar#") [s_tv, elt_tv] [mvar_ty, mvar_ty] boolTy
1442 \end{code}
1443
1444 %************************************************************************
1445 %*                                                                      *
1446 \subsubsection[PrimOp-Wait]{PrimOpInfo for delay/wait operations}
1447 %*                                                                      *
1448 %************************************************************************
1449
1450 \begin{code}
1451
1452 primOpInfo DelayOp
1453   = let {
1454         s = alphaTy; s_tv = alphaTyVar
1455     } in
1456     mkGenPrimOp SLIT("delay#") [s_tv]
1457         [intPrimTy, mkStatePrimTy s] (mkStatePrimTy s)
1458
1459 primOpInfo WaitReadOp
1460   = let {
1461         s = alphaTy; s_tv = alphaTyVar
1462     } in
1463     mkGenPrimOp SLIT("waitRead#") [s_tv]
1464         [intPrimTy, mkStatePrimTy s] (mkStatePrimTy s)
1465
1466 primOpInfo WaitWriteOp
1467   = let {
1468         s = alphaTy; s_tv = alphaTyVar
1469     } in
1470     mkGenPrimOp SLIT("waitWrite#") [s_tv]
1471         [intPrimTy, mkStatePrimTy s] (mkStatePrimTy s)
1472 \end{code}
1473
1474 %************************************************************************
1475 %*                                                                      *
1476 \subsubsection[PrimOp-Concurrency]{Concurrency Primitives}
1477 %*                                                                      *
1478 %************************************************************************
1479
1480 \begin{code}
1481 -- fork# :: a -> State# RealWorld -> (# State# RealWorld, ThreadId# #)
1482 primOpInfo ForkOp       
1483   = mkGenPrimOp SLIT("fork#") [alphaTyVar] 
1484         [alphaTy, realWorldStatePrimTy]
1485         (unboxedPair [realWorldStatePrimTy, threadIdPrimTy])
1486
1487 -- killThread# :: ThreadId# -> State# RealWorld -> State# RealWorld
1488 primOpInfo KillThreadOp
1489   = mkGenPrimOp SLIT("killThread#") [] 
1490         [threadIdPrimTy, realWorldStatePrimTy]
1491         realWorldStatePrimTy
1492 \end{code}
1493
1494 ************************************************************************
1495 %*                                                                      *
1496 \subsubsection[PrimOps-Foreign]{PrimOpInfo for Foreign Objects}
1497 %*                                                                      *
1498 %************************************************************************
1499
1500 \begin{code}
1501 primOpInfo MakeForeignObjOp
1502   = mkGenPrimOp SLIT("makeForeignObj#") [] 
1503         [addrPrimTy, realWorldStatePrimTy] 
1504         (unboxedPair [realWorldStatePrimTy, foreignObjPrimTy])
1505
1506 primOpInfo WriteForeignObjOp
1507  = let {
1508         s = alphaTy; s_tv = alphaTyVar
1509     } in
1510    mkGenPrimOp SLIT("writeForeignObj#") [s_tv]
1511         [foreignObjPrimTy, addrPrimTy, mkStatePrimTy s] (mkStatePrimTy s)
1512 \end{code}
1513
1514 ************************************************************************
1515 %*                                                                      *
1516 \subsubsection[PrimOps-Weak]{PrimOpInfo for Weak Pointers}
1517 %*                                                                      *
1518 %************************************************************************
1519
1520 A @Weak@ Pointer is created by the @mkWeak#@ primitive:
1521
1522         mkWeak# :: k -> v -> f -> State# RealWorld 
1523                         -> (# State# RealWorld, Weak# v #)
1524
1525 In practice, you'll use the higher-level
1526
1527         data Weak v = Weak# v
1528         mkWeak :: k -> v -> IO () -> IO (Weak v)
1529
1530 \begin{code}
1531 primOpInfo MkWeakOp
1532   = mkGenPrimOp SLIT("mkWeak#") [alphaTyVar, betaTyVar, gammaTyVar] 
1533         [alphaTy, betaTy, gammaTy, realWorldStatePrimTy]
1534         (unboxedPair [realWorldStatePrimTy, mkWeakPrimTy betaTy])
1535 \end{code}
1536
1537 The following operation dereferences a weak pointer.  The weak pointer
1538 may have been finalised, so the operation returns a result code which
1539 must be inspected before looking at the dereferenced value.
1540
1541         deRefWeak# :: Weak# v -> State# RealWorld ->
1542                         (# State# RealWorld, v, Int# #)
1543
1544 Only look at v if the Int# returned is /= 0 !!
1545
1546 The higher-level op is
1547
1548         deRefWeak :: Weak v -> IO (Maybe v)
1549
1550 \begin{code}
1551 primOpInfo DeRefWeakOp
1552  = mkGenPrimOp SLIT("deRefWeak#") [alphaTyVar]
1553         [mkWeakPrimTy alphaTy, realWorldStatePrimTy]
1554         (unboxedTriple [realWorldStatePrimTy, intPrimTy, alphaTy])
1555 \end{code}
1556
1557 %************************************************************************
1558 %*                                                                      *
1559 \subsubsection[PrimOp-stable-pointers]{PrimOpInfo for ``stable pointers''}
1560 %*                                                                      *
1561 %************************************************************************
1562
1563 A {\em stable pointer} is an index into a table of pointers into the
1564 heap.  Since the garbage collector is told about stable pointers, it
1565 is safe to pass a stable pointer to external systems such as C
1566 routines.
1567
1568 Here's what the operations and types are supposed to be (from
1569 state-interface document).
1570
1571 \begin{verbatim}
1572 makeStablePtr#  :: a -> State# _RealWorld -> (# State# _RealWorld, a #)
1573 freeStablePtr#  :: StablePtr# a -> State# _RealWorld -> State# _RealWorld
1574 deRefStablePtr# :: StablePtr# a -> State# _RealWorld -> (# State# _RealWorld, a #)
1575 eqStablePtr#    :: StablePtr# a -> StablePtr# a -> Int#
1576 \end{verbatim}
1577
1578 It may seem a bit surprising that @makeStablePtr#@ is a @PrimIO@
1579 operation since it doesn't (directly) involve IO operations.  The
1580 reason is that if some optimisation pass decided to duplicate calls to
1581 @makeStablePtr#@ and we only pass one of the stable pointers over, a
1582 massive space leak can result.  Putting it into the PrimIO monad
1583 prevents this.  (Another reason for putting them in a monad is to
1584 ensure correct sequencing wrt the side-effecting @freeStablePtr#@
1585 operation.)
1586
1587 Note that we can implement @freeStablePtr#@ using @_ccall_@ (and,
1588 besides, it's not likely to be used from Haskell) so it's not a
1589 primop.
1590
1591 Question: Why @_RealWorld@ - won't any instance of @_ST@ do the job? [ADR]
1592
1593 \begin{code}
1594 primOpInfo MakeStablePtrOp
1595   = mkGenPrimOp SLIT("makeStablePtr#") [alphaTyVar]
1596         [alphaTy, realWorldStatePrimTy]
1597         (unboxedPair [realWorldStatePrimTy, 
1598                         mkTyConApp stablePtrPrimTyCon [alphaTy]])
1599
1600 primOpInfo DeRefStablePtrOp
1601   = mkGenPrimOp SLIT("deRefStablePtr#") [alphaTyVar]
1602         [mkStablePtrPrimTy alphaTy, realWorldStatePrimTy]
1603         (unboxedPair [realWorldStatePrimTy, alphaTy])
1604
1605 primOpInfo EqStablePtrOp
1606   = mkGenPrimOp SLIT("eqStablePtr#") [alphaTyVar, betaTyVar]
1607         [mkStablePtrPrimTy alphaTy, mkStablePtrPrimTy betaTy]
1608         intPrimTy
1609 \end{code}
1610
1611 %************************************************************************
1612 %*                                                                      *
1613 \subsubsection[PrimOp-unsafePointerEquality]{PrimOpInfo for Pointer Equality}
1614 %*                                                                      *
1615 %************************************************************************
1616
1617 [Alastair Reid is to blame for this!]
1618
1619 These days, (Glasgow) Haskell seems to have a bit of everything from
1620 other languages: strict operations, mutable variables, sequencing,
1621 pointers, etc.  About the only thing left is LISP's ability to test
1622 for pointer equality.  So, let's add it in!
1623
1624 \begin{verbatim}
1625 reallyUnsafePtrEquality :: a -> a -> Int#
1626 \end{verbatim}
1627
1628 which tests any two closures (of the same type) to see if they're the
1629 same.  (Returns $0$ for @False@, $\neq 0$ for @True@ - to avoid
1630 difficulties of trying to box up the result.)
1631
1632 NB This is {\em really unsafe\/} because even something as trivial as
1633 a garbage collection might change the answer by removing indirections.
1634 Still, no-one's forcing you to use it.  If you're worried about little
1635 things like loss of referential transparency, you might like to wrap
1636 it all up in a monad-like thing as John O'Donnell and John Hughes did
1637 for non-determinism (1989 (Fraserburgh) Glasgow FP Workshop
1638 Proceedings?)
1639
1640 I'm thinking of using it to speed up a critical equality test in some
1641 graphics stuff in a context where the possibility of saying that
1642 denotationally equal things aren't isn't a problem (as long as it
1643 doesn't happen too often.)  ADR
1644
1645 To Will: Jim said this was already in, but I can't see it so I'm
1646 adding it.  Up to you whether you add it.  (Note that this could have
1647 been readily implemented using a @veryDangerousCCall@ before they were
1648 removed...)
1649
1650 \begin{code}
1651 primOpInfo ReallyUnsafePtrEqualityOp
1652   = mkGenPrimOp SLIT("reallyUnsafePtrEquality#") [alphaTyVar]
1653         [alphaTy, alphaTy] intPrimTy
1654 \end{code}
1655
1656 %************************************************************************
1657 %*                                                                      *
1658 \subsubsection[PrimOp-parallel]{PrimOpInfo for parallelism op(s)}
1659 %*                                                                      *
1660 %************************************************************************
1661
1662 \begin{code}
1663 primOpInfo SeqOp        -- seq# :: a -> Int#
1664   = mkGenPrimOp SLIT("seq#")    [alphaTyVar] [alphaTy] intPrimTy
1665
1666 primOpInfo ParOp        -- par# :: a -> Int#
1667   = mkGenPrimOp SLIT("par#")    [alphaTyVar] [alphaTy] intPrimTy
1668 \end{code}
1669
1670 \begin{code}
1671 -- HWL: The first 4 Int# in all par... annotations denote:
1672 --   name, granularity info, size of result, degree of parallelism
1673 --      Same  structure as _seq_ i.e. returns Int#
1674
1675 primOpInfo ParGlobalOp  -- parGlobal# :: Int# -> Int# -> Int# -> Int# -> a -> b -> b
1676   = mkGenPrimOp SLIT("parGlobal#")      [alphaTyVar,betaTyVar] [alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,betaTy] intPrimTy
1677
1678 primOpInfo ParLocalOp   -- parLocal# :: Int# -> Int# -> Int# -> Int# -> a -> b -> b
1679   = mkGenPrimOp SLIT("parLocal#")       [alphaTyVar,betaTyVar] [alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,betaTy] intPrimTy
1680
1681 primOpInfo ParAtOp      -- parAt# :: Int# -> Int# -> Int# -> Int# -> a -> b -> c -> c
1682   = mkGenPrimOp SLIT("parAt#")  [alphaTyVar,betaTyVar,gammaTyVar] [betaTy,alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,gammaTy] intPrimTy
1683
1684 primOpInfo ParAtAbsOp   -- parAtAbs# :: Int# -> Int# -> Int# -> Int# -> Int# -> a -> b -> b
1685   = mkGenPrimOp SLIT("parAtAbs#")       [alphaTyVar,betaTyVar] [alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,betaTy] intPrimTy
1686
1687 primOpInfo ParAtRelOp   -- parAtRel# :: Int# -> Int# -> Int# -> Int# -> Int# -> a -> b -> b
1688   = mkGenPrimOp SLIT("parAtRel#")       [alphaTyVar,betaTyVar] [alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,betaTy] intPrimTy
1689
1690 primOpInfo ParAtForNowOp        -- parAtForNow# :: Int# -> Int# -> Int# -> Int# -> a -> b -> c -> c
1691   = mkGenPrimOp SLIT("parAtForNow#")    [alphaTyVar,betaTyVar,gammaTyVar] [betaTy,alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,gammaTy] intPrimTy
1692
1693 primOpInfo CopyableOp   -- copyable# :: a -> a
1694   = mkGenPrimOp SLIT("copyable#")       [alphaTyVar] [alphaTy] intPrimTy
1695
1696 primOpInfo NoFollowOp   -- noFollow# :: a -> a
1697   = mkGenPrimOp SLIT("noFollow#")       [alphaTyVar] [alphaTy] intPrimTy
1698 \end{code}
1699
1700 %************************************************************************
1701 %*                                                                      *
1702 \subsubsection[PrimOp-IO-etc]{PrimOpInfo for C calls, and I/O-ish things}
1703 %*                                                                      *
1704 %************************************************************************
1705
1706 \begin{code}
1707 primOpInfo (CCallOp _ _ _ _)
1708      = mkGenPrimOp SLIT("ccall#") [alphaTyVar] [] alphaTy
1709
1710 {-
1711 primOpInfo (CCallOp _ _ _ _ arg_tys result_ty)
1712   = mkGenPrimOp SLIT("ccall#") [] arg_tys result_tycon tys_applied
1713   where
1714     (result_tycon, tys_applied, _) = splitAlgTyConApp result_ty
1715 -}
1716 #ifdef DEBUG
1717 primOpInfo op = panic ("primOpInfo:"++ show (I# (tagOf_PrimOp op)))
1718 #endif
1719 \end{code}
1720
1721 Some PrimOps need to be called out-of-line because they either need to
1722 perform a heap check or they block.
1723
1724 \begin{code}
1725 primOpOutOfLine op
1726   = case op of
1727         TakeMVarOp              -> True
1728         PutMVarOp               -> True
1729         DelayOp                 -> True
1730         WaitReadOp              -> True
1731         WaitWriteOp             -> True
1732         CatchOp                 -> True
1733         RaiseOp                 -> True
1734         NewArrayOp              -> True
1735         NewByteArrayOp _        -> True
1736         IntegerAddOp            -> True
1737         IntegerSubOp            -> True
1738         IntegerMulOp            -> True
1739         IntegerGcdOp            -> True
1740         IntegerQuotRemOp        -> True
1741         IntegerDivModOp         -> True
1742         Int2IntegerOp           -> True
1743         Word2IntegerOp          -> True
1744         Addr2IntegerOp          -> True
1745         Word64ToIntegerOp       -> True
1746         Int64ToIntegerOp        -> True
1747         FloatDecodeOp           -> True
1748         DoubleDecodeOp          -> True
1749         MkWeakOp                -> True
1750         DeRefWeakOp             -> True
1751         MakeForeignObjOp        -> True
1752         MakeStablePtrOp         -> True
1753         NewMutVarOp             -> True
1754         NewMVarOp               -> True
1755         ForkOp                  -> True
1756         KillThreadOp            -> True
1757         CCallOp _ _ may_gc@True _ -> True       -- _ccall_GC_
1758         _                       -> False
1759 \end{code}
1760
1761 Sometimes we may choose to execute a PrimOp even though it isn't
1762 certain that its result will be required; ie execute them
1763 ``speculatively''.  The same thing as ``cheap eagerness.'' Usually
1764 this is OK, because PrimOps are usually cheap, but it isn't OK for
1765 (a)~expensive PrimOps and (b)~PrimOps which can fail.
1766
1767 See also @primOpIsCheap@ (below).
1768
1769 PrimOps that have side effects also should not be executed speculatively
1770 or by data dependencies.
1771
1772 \begin{code}
1773 primOpOkForSpeculation :: PrimOp -> Bool
1774 primOpOkForSpeculation op 
1775   = not (primOpCanFail op || primOpHasSideEffects op || primOpOutOfLine op)
1776 \end{code}
1777
1778 @primOpIsCheap@, as used in \tr{SimplUtils.lhs}.  For now (HACK
1779 WARNING), we just borrow some other predicates for a
1780 what-should-be-good-enough test.  "Cheap" means willing to call it more
1781 than once.  Evaluation order is unaffected.
1782
1783 \begin{code}
1784 primOpIsCheap op = not (primOpHasSideEffects op || primOpOutOfLine op)
1785 \end{code}
1786
1787 \begin{code}
1788 primOpCanFail :: PrimOp -> Bool
1789 -- Int.
1790 primOpCanFail IntQuotOp = True          -- Divide by zero
1791 primOpCanFail IntRemOp          = True          -- Divide by zero
1792
1793 -- Integer
1794 primOpCanFail IntegerQuotRemOp = True           -- Divide by zero
1795 primOpCanFail IntegerDivModOp   = True          -- Divide by zero
1796
1797 -- Float.  ToDo: tan? tanh?
1798 primOpCanFail FloatDivOp        = True          -- Divide by zero
1799 primOpCanFail FloatLogOp        = True          -- Log of zero
1800 primOpCanFail FloatAsinOp       = True          -- Arg out of domain
1801 primOpCanFail FloatAcosOp       = True          -- Arg out of domain
1802
1803 -- Double.  ToDo: tan? tanh?
1804 primOpCanFail DoubleDivOp       = True          -- Divide by zero
1805 primOpCanFail DoubleLogOp       = True          -- Log of zero
1806 primOpCanFail DoubleAsinOp      = True          -- Arg out of domain
1807 primOpCanFail DoubleAcosOp      = True          -- Arg out of domain
1808
1809 primOpCanFail other_op          = False
1810 \end{code}
1811
1812 And some primops have side-effects and so, for example, must not be
1813 duplicated.
1814
1815 \begin{code}
1816 primOpHasSideEffects :: PrimOp -> Bool
1817
1818 primOpHasSideEffects TakeMVarOp        = True
1819 primOpHasSideEffects DelayOp           = True
1820 primOpHasSideEffects WaitReadOp        = True
1821 primOpHasSideEffects WaitWriteOp       = True
1822
1823 primOpHasSideEffects ParOp             = True
1824 primOpHasSideEffects ForkOp            = True
1825 primOpHasSideEffects KillThreadOp      = True
1826 primOpHasSideEffects SeqOp             = True
1827
1828 primOpHasSideEffects MakeForeignObjOp  = True
1829 primOpHasSideEffects WriteForeignObjOp = True
1830 primOpHasSideEffects MkWeakOp          = True
1831 primOpHasSideEffects DeRefWeakOp       = True
1832 primOpHasSideEffects MakeStablePtrOp   = True
1833 primOpHasSideEffects EqStablePtrOp     = True  -- SOF
1834 primOpHasSideEffects DeRefStablePtrOp  = True  -- ??? JSM & ADR
1835
1836 primOpHasSideEffects ParGlobalOp        = True
1837 primOpHasSideEffects ParLocalOp         = True
1838 primOpHasSideEffects ParAtOp            = True
1839 primOpHasSideEffects ParAtAbsOp         = True
1840 primOpHasSideEffects ParAtRelOp         = True
1841 primOpHasSideEffects ParAtForNowOp      = True
1842 primOpHasSideEffects CopyableOp         = True  -- Possibly not.  ASP 
1843 primOpHasSideEffects NoFollowOp         = True  -- Possibly not.  ASP
1844
1845 -- CCall
1846 primOpHasSideEffects (CCallOp   _ _ _ _) = True
1847
1848 primOpHasSideEffects other = False
1849 \end{code}
1850
1851 Inline primitive operations that perform calls need wrappers to save
1852 any live variables that are stored in caller-saves registers.
1853
1854 \begin{code}
1855 primOpNeedsWrapper :: PrimOp -> Bool
1856
1857 primOpNeedsWrapper (CCallOp _ _ _ _)    = True
1858
1859 primOpNeedsWrapper Integer2IntOp        = True
1860 primOpNeedsWrapper Integer2WordOp       = True
1861 primOpNeedsWrapper IntegerCmpOp         = True
1862
1863 primOpNeedsWrapper FloatExpOp           = True
1864 primOpNeedsWrapper FloatLogOp           = True
1865 primOpNeedsWrapper FloatSqrtOp          = True
1866 primOpNeedsWrapper FloatSinOp           = True
1867 primOpNeedsWrapper FloatCosOp           = True
1868 primOpNeedsWrapper FloatTanOp           = True
1869 primOpNeedsWrapper FloatAsinOp          = True
1870 primOpNeedsWrapper FloatAcosOp          = True
1871 primOpNeedsWrapper FloatAtanOp          = True
1872 primOpNeedsWrapper FloatSinhOp          = True
1873 primOpNeedsWrapper FloatCoshOp          = True
1874 primOpNeedsWrapper FloatTanhOp          = True
1875 primOpNeedsWrapper FloatPowerOp         = True
1876 primOpNeedsWrapper FloatEncodeOp        = True
1877
1878 primOpNeedsWrapper DoubleExpOp          = True
1879 primOpNeedsWrapper DoubleLogOp          = True
1880 primOpNeedsWrapper DoubleSqrtOp         = True
1881 primOpNeedsWrapper DoubleSinOp          = True
1882 primOpNeedsWrapper DoubleCosOp          = True
1883 primOpNeedsWrapper DoubleTanOp          = True
1884 primOpNeedsWrapper DoubleAsinOp         = True
1885 primOpNeedsWrapper DoubleAcosOp         = True
1886 primOpNeedsWrapper DoubleAtanOp         = True
1887 primOpNeedsWrapper DoubleSinhOp         = True
1888 primOpNeedsWrapper DoubleCoshOp         = True
1889 primOpNeedsWrapper DoubleTanhOp         = True
1890 primOpNeedsWrapper DoublePowerOp        = True
1891 primOpNeedsWrapper DoubleEncodeOp       = True
1892
1893 primOpNeedsWrapper MakeStablePtrOp      = True
1894 primOpNeedsWrapper DeRefStablePtrOp     = True
1895
1896 primOpNeedsWrapper DelayOp              = True
1897 primOpNeedsWrapper WaitReadOp           = True
1898 primOpNeedsWrapper WaitWriteOp          = True
1899
1900 primOpNeedsWrapper other_op             = False
1901 \end{code}
1902
1903 \begin{code}
1904 primOpOcc op
1905   = case (primOpInfo op) of
1906       Dyadic     occ _         -> occ
1907       Monadic    occ _         -> occ
1908       Compare    occ _         -> occ
1909       GenPrimOp  occ _ _ _     -> occ
1910 \end{code}
1911
1912 \begin{code}
1913 primOpUniq :: PrimOp -> Unique
1914 primOpUniq op = mkPrimOpIdUnique (IBOX(tagOf_PrimOp op))
1915
1916 primOpType :: PrimOp -> Type
1917 primOpType op
1918   = case (primOpInfo op) of
1919       Dyadic occ ty ->      dyadic_fun_ty ty
1920       Monadic occ ty ->     monadic_fun_ty ty
1921       Compare occ ty ->     compare_fun_ty ty
1922
1923       GenPrimOp occ tyvars arg_tys res_ty -> 
1924         mkForAllTys tyvars (mkFunTys arg_tys res_ty)
1925 \end{code}
1926
1927 \begin{code}
1928 data PrimOpResultInfo
1929   = ReturnsPrim     PrimRep
1930   | ReturnsAlg      TyCon
1931
1932 -- Some PrimOps need not return a manifest primitive or algebraic value
1933 -- (i.e. they might return a polymorphic value).  These PrimOps *must*
1934 -- be out of line, or the code generator won't work.
1935
1936 getPrimOpResultInfo :: PrimOp -> PrimOpResultInfo
1937
1938 getPrimOpResultInfo op
1939   = case (primOpInfo op) of
1940       Dyadic  _ ty               -> ReturnsPrim (typePrimRep ty)
1941       Monadic _ ty               -> ReturnsPrim (typePrimRep ty)
1942       Compare _ ty               -> ReturnsAlg  boolTyCon
1943       GenPrimOp _ _ _ ty         -> 
1944         let rep = typePrimRep ty in
1945         case rep of
1946            PtrRep -> case splitAlgTyConApp_maybe ty of
1947                         Nothing -> panic "getPrimOpResultInfo"
1948                         Just (tc,_,_) -> ReturnsAlg tc
1949            other -> ReturnsPrim other
1950
1951 isCompareOp :: PrimOp -> Bool
1952
1953 isCompareOp op
1954   = case primOpInfo op of
1955       Compare _ _ -> True
1956       _           -> False
1957 \end{code}
1958
1959 The commutable ops are those for which we will try to move constants
1960 to the right hand side for strength reduction.
1961
1962 \begin{code}
1963 commutableOp :: PrimOp -> Bool
1964
1965 commutableOp CharEqOp     = True
1966 commutableOp CharNeOp     = True
1967 commutableOp IntAddOp     = True
1968 commutableOp IntMulOp     = True
1969 commutableOp AndOp        = True
1970 commutableOp OrOp         = True
1971 commutableOp XorOp        = True
1972 commutableOp IntEqOp      = True
1973 commutableOp IntNeOp      = True
1974 commutableOp IntegerAddOp = True
1975 commutableOp IntegerMulOp = True
1976 commutableOp IntegerGcdOp = True
1977 commutableOp FloatAddOp   = True
1978 commutableOp FloatMulOp   = True
1979 commutableOp FloatEqOp    = True
1980 commutableOp FloatNeOp    = True
1981 commutableOp DoubleAddOp  = True
1982 commutableOp DoubleMulOp  = True
1983 commutableOp DoubleEqOp   = True
1984 commutableOp DoubleNeOp   = True
1985 commutableOp _            = False
1986 \end{code}
1987
1988 Utils:
1989 \begin{code}
1990 dyadic_fun_ty  ty = mkFunTys [ty, ty] ty
1991 monadic_fun_ty ty = mkFunTy  ty ty
1992 compare_fun_ty ty = mkFunTys [ty, ty] boolTy
1993 \end{code}
1994
1995 Output stuff:
1996 \begin{code}
1997 pprPrimOp  :: PrimOp -> SDoc
1998
1999 pprPrimOp (CCallOp fun is_casm may_gc cconv)
2000   = let
2001         callconv = text "{-" <> pprCallConv cconv <> text "-}"
2002
2003         before
2004           | is_casm && may_gc = "__casm_GC ``"
2005           | is_casm           = "__casm ``"
2006           | may_gc            = "__ccall_GC "
2007           | otherwise         = "__ccall "
2008
2009         after
2010           | is_casm   = text "''"
2011           | otherwise = empty
2012
2013         ppr_fun =
2014          case fun of
2015            Right _ -> ptext SLIT("<dynamic>")
2016            Left fn -> ptext fn
2017          
2018     in
2019     hcat [ ifPprDebug callconv
2020          , text before , ppr_fun , after]
2021
2022 pprPrimOp other_op
2023   = getPprStyle $ \ sty ->
2024    if ifaceStyle sty then       -- For interfaces Print it qualified with PrelGHC.
2025         ptext SLIT("PrelGHC.") <> pprOccName occ
2026    else
2027         pprOccName occ
2028   where
2029     occ = primOpOcc other_op
2030 \end{code}