[project @ 1998-12-23 10:24:06 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 NewArrayOp       = ([wwPrim, wwLazy, wwPrim], False)
860 primOpStrictness WriteArrayOp     = ([wwPrim, wwPrim, wwLazy, wwPrim], False)
861
862 primOpStrictness NewMutVarOp      = ([wwLazy, wwPrim], False)
863 primOpStrictness WriteMutVarOp    = ([wwPrim, wwLazy, wwPrim], False)
864
865 primOpStrictness PutMVarOp        = ([wwPrim, wwLazy, wwPrim], False)
866
867 primOpStrictness CatchOp          = ([wwLazy, wwLazy], False)
868 primOpStrictness RaiseOp          = ([wwLazy], True)    -- NB: True => result is bottom
869
870 primOpStrictness MkWeakOp         = ([wwLazy, wwLazy, wwLazy, wwPrim], False)
871 primOpStrictness MakeStablePtrOp  = ([wwLazy, wwPrim], False)
872
873         -- The rest all have primitive-typed arguments
874 primOpStrictness other            = (repeat wwPrim, False)
875 \end{code}
876
877 %************************************************************************
878 %*                                                                      *
879 \subsubsection[PrimOp-comparison]{PrimOpInfo basic comparison ops}
880 %*                                                                      *
881 %************************************************************************
882
883 @primOpInfo@ gives all essential information (from which everything
884 else, notably a type, can be constructed) for each @PrimOp@.
885
886 \begin{code}
887 primOpInfo :: PrimOp -> PrimOpInfo
888 \end{code}
889
890 There's plenty of this stuff!
891
892 \begin{code}
893 primOpInfo CharGtOp   = mkCompare SLIT("gtChar#")   charPrimTy
894 primOpInfo CharGeOp   = mkCompare SLIT("geChar#")   charPrimTy
895 primOpInfo CharEqOp   = mkCompare SLIT("eqChar#")   charPrimTy
896 primOpInfo CharNeOp   = mkCompare SLIT("neChar#")   charPrimTy
897 primOpInfo CharLtOp   = mkCompare SLIT("ltChar#")   charPrimTy
898 primOpInfo CharLeOp   = mkCompare SLIT("leChar#")   charPrimTy
899
900 primOpInfo IntGtOp    = mkCompare SLIT(">#")       intPrimTy
901 primOpInfo IntGeOp    = mkCompare SLIT(">=#")      intPrimTy
902 primOpInfo IntEqOp    = mkCompare SLIT("==#")      intPrimTy
903 primOpInfo IntNeOp    = mkCompare SLIT("/=#")      intPrimTy
904 primOpInfo IntLtOp    = mkCompare SLIT("<#")       intPrimTy
905 primOpInfo IntLeOp    = mkCompare SLIT("<=#")      intPrimTy
906
907 primOpInfo WordGtOp   = mkCompare SLIT("gtWord#")   wordPrimTy
908 primOpInfo WordGeOp   = mkCompare SLIT("geWord#")   wordPrimTy
909 primOpInfo WordEqOp   = mkCompare SLIT("eqWord#")   wordPrimTy
910 primOpInfo WordNeOp   = mkCompare SLIT("neWord#")   wordPrimTy
911 primOpInfo WordLtOp   = mkCompare SLIT("ltWord#")   wordPrimTy
912 primOpInfo WordLeOp   = mkCompare SLIT("leWord#")   wordPrimTy
913
914 primOpInfo AddrGtOp   = mkCompare SLIT("gtAddr#")   addrPrimTy
915 primOpInfo AddrGeOp   = mkCompare SLIT("geAddr#")   addrPrimTy
916 primOpInfo AddrEqOp   = mkCompare SLIT("eqAddr#")   addrPrimTy
917 primOpInfo AddrNeOp   = mkCompare SLIT("neAddr#")   addrPrimTy
918 primOpInfo AddrLtOp   = mkCompare SLIT("ltAddr#")   addrPrimTy
919 primOpInfo AddrLeOp   = mkCompare SLIT("leAddr#")   addrPrimTy
920
921 primOpInfo FloatGtOp  = mkCompare SLIT("gtFloat#")  floatPrimTy
922 primOpInfo FloatGeOp  = mkCompare SLIT("geFloat#")  floatPrimTy
923 primOpInfo FloatEqOp  = mkCompare SLIT("eqFloat#")  floatPrimTy
924 primOpInfo FloatNeOp  = mkCompare SLIT("neFloat#")  floatPrimTy
925 primOpInfo FloatLtOp  = mkCompare SLIT("ltFloat#")  floatPrimTy
926 primOpInfo FloatLeOp  = mkCompare SLIT("leFloat#")  floatPrimTy
927
928 primOpInfo DoubleGtOp = mkCompare SLIT(">##") doublePrimTy
929 primOpInfo DoubleGeOp = mkCompare SLIT(">=##") doublePrimTy
930 primOpInfo DoubleEqOp = mkCompare SLIT("==##") doublePrimTy
931 primOpInfo DoubleNeOp = mkCompare SLIT("/=##") doublePrimTy
932 primOpInfo DoubleLtOp = mkCompare SLIT("<##") doublePrimTy
933 primOpInfo DoubleLeOp = mkCompare SLIT("<=##") doublePrimTy
934
935 \end{code}
936
937 %************************************************************************
938 %*                                                                      *
939 \subsubsection[PrimOp-Char]{PrimOpInfo for @Char#@s}
940 %*                                                                      *
941 %************************************************************************
942
943 \begin{code}
944 primOpInfo OrdOp = mkGenPrimOp SLIT("ord#") [] [charPrimTy] intPrimTy
945 primOpInfo ChrOp = mkGenPrimOp SLIT("chr#") [] [intPrimTy]  charPrimTy
946 \end{code}
947
948 %************************************************************************
949 %*                                                                      *
950 \subsubsection[PrimOp-Int]{PrimOpInfo for @Int#@s}
951 %*                                                                      *
952 %************************************************************************
953
954 \begin{code}
955 primOpInfo IntAddOp  = mkDyadic SLIT("+#")       intPrimTy
956 primOpInfo IntSubOp  = mkDyadic SLIT("-#") intPrimTy
957 primOpInfo IntMulOp  = mkDyadic SLIT("*#") intPrimTy
958 primOpInfo IntQuotOp = mkDyadic SLIT("quotInt#")         intPrimTy
959 primOpInfo IntRemOp  = mkDyadic SLIT("remInt#")  intPrimTy
960
961 primOpInfo IntNegOp  = mkMonadic SLIT("negateInt#") intPrimTy
962 primOpInfo IntAbsOp  = mkMonadic SLIT("absInt#") intPrimTy
963 \end{code}
964
965 %************************************************************************
966 %*                                                                      *
967 \subsubsection[PrimOp-Word]{PrimOpInfo for @Word#@s}
968 %*                                                                      *
969 %************************************************************************
970
971 A @Word#@ is an unsigned @Int#@.
972
973 \begin{code}
974 primOpInfo WordQuotOp = mkDyadic SLIT("quotWord#") wordPrimTy
975 primOpInfo WordRemOp  = mkDyadic SLIT("remWord#")        wordPrimTy
976
977 primOpInfo AndOp    = mkDyadic  SLIT("and#")    wordPrimTy
978 primOpInfo OrOp     = mkDyadic  SLIT("or#")     wordPrimTy
979 primOpInfo XorOp    = mkDyadic  SLIT("xor#")    wordPrimTy
980 primOpInfo NotOp    = mkMonadic SLIT("not#")    wordPrimTy
981
982 primOpInfo SllOp
983   = mkGenPrimOp SLIT("shiftL#")  [] [wordPrimTy, intPrimTy] wordPrimTy
984 primOpInfo SrlOp
985   = mkGenPrimOp SLIT("shiftRL#") [] [wordPrimTy, intPrimTy] wordPrimTy
986
987 primOpInfo ISllOp
988   = mkGenPrimOp SLIT("iShiftL#")  [] [intPrimTy, intPrimTy] intPrimTy
989 primOpInfo ISraOp
990   = mkGenPrimOp SLIT("iShiftRA#") [] [intPrimTy, intPrimTy] intPrimTy
991 primOpInfo ISrlOp
992   = mkGenPrimOp SLIT("iShiftRL#") [] [intPrimTy, intPrimTy] intPrimTy
993
994 primOpInfo Int2WordOp = mkGenPrimOp SLIT("int2Word#") [] [intPrimTy] wordPrimTy
995 primOpInfo Word2IntOp = mkGenPrimOp SLIT("word2Int#") [] [wordPrimTy] intPrimTy
996 \end{code}
997
998 %************************************************************************
999 %*                                                                      *
1000 \subsubsection[PrimOp-Addr]{PrimOpInfo for @Addr#@s}
1001 %*                                                                      *
1002 %************************************************************************
1003
1004 \begin{code}
1005 primOpInfo Int2AddrOp = mkGenPrimOp SLIT("int2Addr#") [] [intPrimTy] addrPrimTy
1006 primOpInfo Addr2IntOp = mkGenPrimOp SLIT("addr2Int#") [] [addrPrimTy] intPrimTy
1007 \end{code}
1008
1009
1010 %************************************************************************
1011 %*                                                                      *
1012 \subsubsection[PrimOp-Float]{PrimOpInfo for @Float#@s}
1013 %*                                                                      *
1014 %************************************************************************
1015
1016 @encodeFloat#@ and @decodeFloat#@ are given w/ Integer-stuff (it's
1017 similar).
1018
1019 \begin{code}
1020 primOpInfo FloatAddOp   = mkDyadic    SLIT("plusFloat#")           floatPrimTy
1021 primOpInfo FloatSubOp   = mkDyadic    SLIT("minusFloat#")   floatPrimTy
1022 primOpInfo FloatMulOp   = mkDyadic    SLIT("timesFloat#")   floatPrimTy
1023 primOpInfo FloatDivOp   = mkDyadic    SLIT("divideFloat#")  floatPrimTy
1024 primOpInfo FloatNegOp   = mkMonadic   SLIT("negateFloat#")  floatPrimTy
1025
1026 primOpInfo Float2IntOp  = mkGenPrimOp SLIT("float2Int#") [] [floatPrimTy] intPrimTy
1027 primOpInfo Int2FloatOp  = mkGenPrimOp SLIT("int2Float#") [] [intPrimTy] floatPrimTy
1028
1029 primOpInfo FloatExpOp   = mkMonadic   SLIT("expFloat#")    floatPrimTy
1030 primOpInfo FloatLogOp   = mkMonadic   SLIT("logFloat#")    floatPrimTy
1031 primOpInfo FloatSqrtOp  = mkMonadic   SLIT("sqrtFloat#")           floatPrimTy
1032 primOpInfo FloatSinOp   = mkMonadic   SLIT("sinFloat#")    floatPrimTy
1033 primOpInfo FloatCosOp   = mkMonadic   SLIT("cosFloat#")    floatPrimTy
1034 primOpInfo FloatTanOp   = mkMonadic   SLIT("tanFloat#")    floatPrimTy
1035 primOpInfo FloatAsinOp  = mkMonadic   SLIT("asinFloat#")           floatPrimTy
1036 primOpInfo FloatAcosOp  = mkMonadic   SLIT("acosFloat#")           floatPrimTy
1037 primOpInfo FloatAtanOp  = mkMonadic   SLIT("atanFloat#")           floatPrimTy
1038 primOpInfo FloatSinhOp  = mkMonadic   SLIT("sinhFloat#")           floatPrimTy
1039 primOpInfo FloatCoshOp  = mkMonadic   SLIT("coshFloat#")           floatPrimTy
1040 primOpInfo FloatTanhOp  = mkMonadic   SLIT("tanhFloat#")           floatPrimTy
1041 primOpInfo FloatPowerOp = mkDyadic    SLIT("powerFloat#")   floatPrimTy
1042 \end{code}
1043
1044 %************************************************************************
1045 %*                                                                      *
1046 \subsubsection[PrimOp-Double]{PrimOpInfo for @Double#@s}
1047 %*                                                                      *
1048 %************************************************************************
1049
1050 @encodeDouble#@ and @decodeDouble#@ are given w/ Integer-stuff (it's
1051 similar).
1052
1053 \begin{code}
1054 primOpInfo DoubleAddOp  = mkDyadic    SLIT("+##")   doublePrimTy
1055 primOpInfo DoubleSubOp  = mkDyadic    SLIT("-##")  doublePrimTy
1056 primOpInfo DoubleMulOp  = mkDyadic    SLIT("*##")  doublePrimTy
1057 primOpInfo DoubleDivOp  = mkDyadic    SLIT("/##") doublePrimTy
1058 primOpInfo DoubleNegOp  = mkMonadic   SLIT("negateDouble#") doublePrimTy
1059
1060 primOpInfo Double2IntOp     = mkGenPrimOp SLIT("double2Int#") [] [doublePrimTy] intPrimTy
1061 primOpInfo Int2DoubleOp     = mkGenPrimOp SLIT("int2Double#") [] [intPrimTy] doublePrimTy
1062
1063 primOpInfo Double2FloatOp   = mkGenPrimOp SLIT("double2Float#") [] [doublePrimTy] floatPrimTy
1064 primOpInfo Float2DoubleOp   = mkGenPrimOp SLIT("float2Double#") [] [floatPrimTy] doublePrimTy
1065
1066 primOpInfo DoubleExpOp  = mkMonadic   SLIT("expDouble#")           doublePrimTy
1067 primOpInfo DoubleLogOp  = mkMonadic   SLIT("logDouble#")           doublePrimTy
1068 primOpInfo DoubleSqrtOp = mkMonadic   SLIT("sqrtDouble#")   doublePrimTy
1069 primOpInfo DoubleSinOp  = mkMonadic   SLIT("sinDouble#")           doublePrimTy
1070 primOpInfo DoubleCosOp  = mkMonadic   SLIT("cosDouble#")           doublePrimTy
1071 primOpInfo DoubleTanOp  = mkMonadic   SLIT("tanDouble#")           doublePrimTy
1072 primOpInfo DoubleAsinOp = mkMonadic   SLIT("asinDouble#")   doublePrimTy
1073 primOpInfo DoubleAcosOp = mkMonadic   SLIT("acosDouble#")   doublePrimTy
1074 primOpInfo DoubleAtanOp = mkMonadic   SLIT("atanDouble#")   doublePrimTy
1075 primOpInfo DoubleSinhOp = mkMonadic   SLIT("sinhDouble#")   doublePrimTy
1076 primOpInfo DoubleCoshOp = mkMonadic   SLIT("coshDouble#")   doublePrimTy
1077 primOpInfo DoubleTanhOp = mkMonadic   SLIT("tanhDouble#")   doublePrimTy
1078 primOpInfo DoublePowerOp= mkDyadic    SLIT("**##")  doublePrimTy
1079 \end{code}
1080
1081 %************************************************************************
1082 %*                                                                      *
1083 \subsubsection[PrimOp-Integer]{PrimOpInfo for @Integer@ (and related!)}
1084 %*                                                                      *
1085 %************************************************************************
1086
1087 \begin{code}
1088 primOpInfo IntegerNegOp = integerMonadic SLIT("negateInteger#")
1089
1090 primOpInfo IntegerAddOp = integerDyadic SLIT("plusInteger#")
1091 primOpInfo IntegerSubOp = integerDyadic SLIT("minusInteger#")
1092 primOpInfo IntegerMulOp = integerDyadic SLIT("timesInteger#")
1093 primOpInfo IntegerGcdOp = integerDyadic SLIT("gcdInteger#")
1094
1095 primOpInfo IntegerCmpOp = integerCompare SLIT("cmpInteger#")
1096
1097 primOpInfo IntegerQuotRemOp = integerDyadic2Results SLIT("quotRemInteger#")
1098 primOpInfo IntegerDivModOp  = integerDyadic2Results SLIT("divModInteger#")
1099
1100 primOpInfo Integer2IntOp
1101   = mkGenPrimOp SLIT("integer2Int#") [] one_Integer_ty intPrimTy
1102
1103 primOpInfo Integer2WordOp
1104   = mkGenPrimOp SLIT("integer2Word#") [] one_Integer_ty wordPrimTy
1105
1106 primOpInfo Int2IntegerOp
1107   = mkGenPrimOp SLIT("int2Integer#") [] [intPrimTy] 
1108                         (unboxedTriple [intPrimTy, intPrimTy, byteArrayPrimTy])
1109
1110 primOpInfo Word2IntegerOp
1111   = mkGenPrimOp SLIT("word2Integer#") [] [wordPrimTy] 
1112                         (unboxedTriple [intPrimTy, intPrimTy, byteArrayPrimTy])
1113
1114 primOpInfo Addr2IntegerOp
1115   = mkGenPrimOp SLIT("addr2Integer#") [] [addrPrimTy] 
1116                         (unboxedTriple [intPrimTy, intPrimTy, byteArrayPrimTy])
1117
1118 primOpInfo IntegerToInt64Op
1119   = mkGenPrimOp SLIT("integerToInt64#") [] one_Integer_ty int64PrimTy
1120
1121 primOpInfo Int64ToIntegerOp
1122   = mkGenPrimOp SLIT("int64ToInteger#") [] [int64PrimTy]
1123                         (unboxedTriple [intPrimTy, intPrimTy, byteArrayPrimTy])
1124
1125 primOpInfo Word64ToIntegerOp
1126   = mkGenPrimOp SLIT("word64ToInteger#") [] [word64PrimTy] 
1127                         (unboxedTriple [intPrimTy, intPrimTy, byteArrayPrimTy])
1128
1129 primOpInfo IntegerToWord64Op
1130   = mkGenPrimOp SLIT("integerToWord64#") [] one_Integer_ty word64PrimTy
1131 \end{code}
1132
1133 Encoding and decoding of floating-point numbers is sorta
1134 Integer-related.
1135
1136 \begin{code}
1137 primOpInfo FloatEncodeOp
1138   = mkGenPrimOp SLIT("encodeFloat#") [] an_Integer_and_Int_tys floatPrimTy
1139
1140 primOpInfo DoubleEncodeOp
1141   = mkGenPrimOp SLIT("encodeDouble#") [] an_Integer_and_Int_tys doublePrimTy
1142
1143 primOpInfo FloatDecodeOp
1144   = mkGenPrimOp SLIT("decodeFloat#") [] [floatPrimTy] 
1145         (unboxedQuadruple [intPrimTy, intPrimTy, intPrimTy, byteArrayPrimTy])
1146 primOpInfo DoubleDecodeOp
1147   = mkGenPrimOp SLIT("decodeDouble#") [] [doublePrimTy] 
1148         (unboxedQuadruple [intPrimTy, intPrimTy, intPrimTy, byteArrayPrimTy])
1149 \end{code}
1150
1151 %************************************************************************
1152 %*                                                                      *
1153 \subsubsection[PrimOp-Arrays]{PrimOpInfo for primitive arrays}
1154 %*                                                                      *
1155 %************************************************************************
1156
1157 \begin{code}
1158 primOpInfo NewArrayOp
1159   = let {
1160         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1161         state = mkStatePrimTy s
1162     } in
1163     mkGenPrimOp SLIT("newArray#") [s_tv, elt_tv] 
1164         [intPrimTy, elt, state]
1165         (unboxedPair [state, mkMutableArrayPrimTy s elt])
1166
1167 primOpInfo (NewByteArrayOp kind)
1168   = let
1169         s = alphaTy; s_tv = alphaTyVar
1170
1171         op_str         = _PK_ ("new" ++ primRepString kind ++ "Array#")
1172         state = mkStatePrimTy s
1173     in
1174     mkGenPrimOp op_str [s_tv]
1175         [intPrimTy, state]
1176         (unboxedPair [state, mkMutableByteArrayPrimTy s])
1177
1178 ---------------------------------------------------------------------------
1179
1180 primOpInfo SameMutableArrayOp
1181   = let {
1182         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1183         mut_arr_ty = mkMutableArrayPrimTy s elt
1184     } in
1185     mkGenPrimOp SLIT("sameMutableArray#") [s_tv, elt_tv] [mut_arr_ty, mut_arr_ty]
1186                                    boolTy
1187
1188 primOpInfo SameMutableByteArrayOp
1189   = let {
1190         s = alphaTy; s_tv = alphaTyVar;
1191         mut_arr_ty = mkMutableByteArrayPrimTy s
1192     } in
1193     mkGenPrimOp SLIT("sameMutableByteArray#") [s_tv] [mut_arr_ty, mut_arr_ty]
1194                                    boolTy
1195
1196 ---------------------------------------------------------------------------
1197 -- Primitive arrays of Haskell pointers:
1198
1199 primOpInfo ReadArrayOp
1200   = let {
1201         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1202         state = mkStatePrimTy s
1203     } in
1204     mkGenPrimOp SLIT("readArray#") [s_tv, elt_tv]
1205         [mkMutableArrayPrimTy s elt, intPrimTy, state]
1206         (unboxedPair [state, elt])
1207
1208
1209 primOpInfo WriteArrayOp
1210   = let {
1211         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1212     } in
1213     mkGenPrimOp SLIT("writeArray#") [s_tv, elt_tv]
1214         [mkMutableArrayPrimTy s elt, intPrimTy, elt, mkStatePrimTy s]
1215         (mkStatePrimTy s)
1216
1217 primOpInfo IndexArrayOp
1218   = let { elt = alphaTy; elt_tv = alphaTyVar } in
1219     mkGenPrimOp SLIT("indexArray#") [elt_tv] [mkArrayPrimTy elt, intPrimTy]
1220         (unboxedPair [realWorldStatePrimTy, elt])
1221
1222 ---------------------------------------------------------------------------
1223 -- Primitive arrays full of unboxed bytes:
1224
1225 primOpInfo (ReadByteArrayOp kind)
1226   = let
1227         s = alphaTy; s_tv = alphaTyVar
1228
1229         op_str         = _PK_ ("read" ++ primRepString kind ++ "Array#")
1230         relevant_type  = assoc "primOpInfo{ReadByteArrayOp}" tbl kind
1231         state          = mkStatePrimTy s
1232
1233         tvs
1234           | kind == StablePtrRep = [s_tv, betaTyVar]
1235           | otherwise            = [s_tv]
1236     in
1237     mkGenPrimOp op_str tvs
1238         [mkMutableByteArrayPrimTy s, intPrimTy, state]
1239         (unboxedPair [state, relevant_type])
1240   where
1241     tbl = [ (CharRep,    charPrimTy),
1242             (IntRep,     intPrimTy),
1243             (WordRep,    wordPrimTy),
1244             (AddrRep,    addrPrimTy),
1245             (FloatRep,   floatPrimTy),
1246             (StablePtrRep, mkStablePtrPrimTy betaTy),
1247             (DoubleRep,  doublePrimTy) ]
1248
1249   -- How come there's no Word byte arrays? ADR
1250
1251 primOpInfo (WriteByteArrayOp kind)
1252   = let
1253         s = alphaTy; s_tv = alphaTyVar
1254         op_str = _PK_ ("write" ++ primRepString kind ++ "Array#")
1255         prim_ty = mkTyConApp (primRepTyCon kind) []
1256
1257         (the_prim_ty, tvs)
1258           | kind == StablePtrRep = (mkStablePtrPrimTy betaTy, [s_tv, betaTyVar])
1259           | otherwise            = (prim_ty, [s_tv])
1260
1261     in
1262     mkGenPrimOp op_str tvs
1263         [mkMutableByteArrayPrimTy s, intPrimTy, the_prim_ty, mkStatePrimTy s]
1264         (mkStatePrimTy s)
1265
1266 primOpInfo (IndexByteArrayOp kind)
1267   = let
1268         op_str = _PK_ ("index" ++ primRepString kind ++ "Array#")
1269
1270         (prim_tycon_args, tvs)
1271           | kind == StablePtrRep = ([alphaTy], [alphaTyVar])
1272           | otherwise            = ([],[])
1273     in
1274     mkGenPrimOp op_str tvs [byteArrayPrimTy, intPrimTy] 
1275         (mkTyConApp (primRepTyCon kind) prim_tycon_args)
1276
1277 primOpInfo (IndexOffForeignObjOp kind)
1278   = let
1279         op_str = _PK_ ("index" ++ primRepString kind ++ "OffForeignObj#")
1280
1281         (prim_tycon_args, tvs)
1282           | kind == StablePtrRep = ([alphaTy], [alphaTyVar])
1283           | otherwise            = ([], [])
1284     in
1285     mkGenPrimOp op_str tvs [foreignObjPrimTy, intPrimTy] 
1286         (mkTyConApp (primRepTyCon kind) prim_tycon_args)
1287
1288 primOpInfo (IndexOffAddrOp kind)
1289   = let
1290         op_str = _PK_ ("index" ++ primRepString kind ++ "OffAddr#")
1291
1292         (prim_tycon_args, tvs)
1293           | kind == StablePtrRep = ([alphaTy], [alphaTyVar])
1294           | otherwise            = ([], [])
1295     in
1296     mkGenPrimOp op_str tvs [addrPrimTy, intPrimTy] 
1297         (mkTyConApp (primRepTyCon kind) prim_tycon_args)
1298
1299 primOpInfo (WriteOffAddrOp kind)
1300   = let
1301         s = alphaTy; s_tv = alphaTyVar
1302         op_str = _PK_ ("write" ++ primRepString kind ++ "OffAddr#")
1303         prim_ty = mkTyConApp (primRepTyCon kind) []
1304     in
1305     mkGenPrimOp op_str [s_tv]
1306         [addrPrimTy, intPrimTy, prim_ty, mkStatePrimTy s]
1307         (mkStatePrimTy s)
1308
1309 ---------------------------------------------------------------------------
1310 primOpInfo UnsafeFreezeArrayOp
1311   = let {
1312         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1313         state = mkStatePrimTy s
1314     } in
1315     mkGenPrimOp SLIT("unsafeFreezeArray#") [s_tv, elt_tv]
1316         [mkMutableArrayPrimTy s elt, state]
1317         (unboxedPair [state, mkArrayPrimTy elt])
1318
1319 primOpInfo UnsafeFreezeByteArrayOp
1320   = let { 
1321         s = alphaTy; s_tv = alphaTyVar;
1322         state = mkStatePrimTy s
1323     } in
1324     mkGenPrimOp SLIT("unsafeFreezeByteArray#") [s_tv]
1325         [mkMutableByteArrayPrimTy s, state]
1326         (unboxedPair [state, byteArrayPrimTy])
1327
1328 ---------------------------------------------------------------------------
1329 primOpInfo SizeofByteArrayOp
1330   = mkGenPrimOp
1331         SLIT("sizeofByteArray#") []
1332         [byteArrayPrimTy]
1333         intPrimTy
1334
1335 primOpInfo SizeofMutableByteArrayOp
1336   = let { s = alphaTy; s_tv = alphaTyVar } in
1337     mkGenPrimOp
1338         SLIT("sizeofMutableByteArray#") [s_tv]
1339         [mkMutableByteArrayPrimTy s]
1340         intPrimTy
1341 \end{code}
1342
1343
1344 %************************************************************************
1345 %*                                                                      *
1346 \subsubsection[PrimOp-MutVars]{PrimOpInfo for mutable variable ops}
1347 %*                                                                      *
1348 %************************************************************************
1349
1350 \begin{code}
1351 primOpInfo NewMutVarOp
1352   = let {
1353         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1354         state = mkStatePrimTy s
1355     } in
1356     mkGenPrimOp SLIT("newMutVar#") [s_tv, elt_tv] 
1357         [elt, state]
1358         (unboxedPair [state, mkMutVarPrimTy s elt])
1359
1360 primOpInfo ReadMutVarOp
1361   = let {
1362         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1363         state = mkStatePrimTy s
1364     } in
1365     mkGenPrimOp SLIT("readMutVar#") [s_tv, elt_tv]
1366         [mkMutVarPrimTy s elt, state]
1367         (unboxedPair [state, elt])
1368
1369
1370 primOpInfo WriteMutVarOp
1371   = let {
1372         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1373     } in
1374     mkGenPrimOp SLIT("writeMutVar#") [s_tv, elt_tv]
1375         [mkMutVarPrimTy s elt, elt, mkStatePrimTy s]
1376         (mkStatePrimTy s)
1377
1378 primOpInfo SameMutVarOp
1379   = let {
1380         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar;
1381         mut_var_ty = mkMutVarPrimTy s elt
1382     } in
1383     mkGenPrimOp SLIT("sameMutVar#") [s_tv, elt_tv] [mut_var_ty, mut_var_ty]
1384                                    boolTy
1385 \end{code}
1386
1387 %************************************************************************
1388 %*                                                                      *
1389 \subsubsection[PrimOp-Exceptions]{PrimOpInfo for exceptions}
1390 %*                                                                      *
1391 %************************************************************************
1392
1393 catch :: IO a -> (IOError -> IO a) -> IO a
1394 catch :: a  -> (b -> a) -> a
1395
1396 \begin{code}
1397 primOpInfo CatchOp   
1398   = let
1399         a = alphaTy; a_tv = alphaTyVar;
1400         b = betaTy;  b_tv = betaTyVar;
1401     in
1402     mkGenPrimOp SLIT("catch#") [a_tv, b_tv] [a, mkFunTy b a] a
1403
1404 primOpInfo RaiseOp
1405   = let
1406         a = alphaTy; a_tv = alphaTyVar;
1407         b = betaTy;  b_tv = betaTyVar;
1408     in
1409     mkGenPrimOp SLIT("raise#") [a_tv, b_tv] [a] b
1410 \end{code}
1411
1412 %************************************************************************
1413 %*                                                                      *
1414 \subsubsection[PrimOp-MVars]{PrimOpInfo for synchronizing Variables}
1415 %*                                                                      *
1416 %************************************************************************
1417
1418 \begin{code}
1419 primOpInfo NewMVarOp
1420   = let
1421         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1422         state = mkStatePrimTy s
1423     in
1424     mkGenPrimOp SLIT("newMVar#") [s_tv, elt_tv] [state]
1425         (unboxedPair [state, mkMVarPrimTy s elt])
1426
1427 primOpInfo TakeMVarOp
1428   = let
1429         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1430         state = mkStatePrimTy s
1431     in
1432     mkGenPrimOp SLIT("takeMVar#") [s_tv, elt_tv]
1433         [mkMVarPrimTy s elt, state]
1434         (unboxedPair [state, elt])
1435
1436 primOpInfo PutMVarOp
1437   = let
1438         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1439     in
1440     mkGenPrimOp SLIT("putMVar#") [s_tv, elt_tv]
1441         [mkMVarPrimTy s elt, elt, mkStatePrimTy s]
1442         (mkStatePrimTy s)
1443
1444 primOpInfo SameMVarOp
1445   = let
1446         elt = alphaTy; elt_tv = alphaTyVar; s = betaTy; s_tv = betaTyVar
1447         mvar_ty = mkMVarPrimTy s elt
1448     in
1449     mkGenPrimOp SLIT("sameMVar#") [s_tv, elt_tv] [mvar_ty, mvar_ty] boolTy
1450 \end{code}
1451
1452 %************************************************************************
1453 %*                                                                      *
1454 \subsubsection[PrimOp-Wait]{PrimOpInfo for delay/wait operations}
1455 %*                                                                      *
1456 %************************************************************************
1457
1458 \begin{code}
1459
1460 primOpInfo DelayOp
1461   = let {
1462         s = alphaTy; s_tv = alphaTyVar
1463     } in
1464     mkGenPrimOp SLIT("delay#") [s_tv]
1465         [intPrimTy, mkStatePrimTy s] (mkStatePrimTy s)
1466
1467 primOpInfo WaitReadOp
1468   = let {
1469         s = alphaTy; s_tv = alphaTyVar
1470     } in
1471     mkGenPrimOp SLIT("waitRead#") [s_tv]
1472         [intPrimTy, mkStatePrimTy s] (mkStatePrimTy s)
1473
1474 primOpInfo WaitWriteOp
1475   = let {
1476         s = alphaTy; s_tv = alphaTyVar
1477     } in
1478     mkGenPrimOp SLIT("waitWrite#") [s_tv]
1479         [intPrimTy, mkStatePrimTy s] (mkStatePrimTy s)
1480 \end{code}
1481
1482 %************************************************************************
1483 %*                                                                      *
1484 \subsubsection[PrimOp-Concurrency]{Concurrency Primitives}
1485 %*                                                                      *
1486 %************************************************************************
1487
1488 \begin{code}
1489 -- fork# :: a -> State# RealWorld -> (# State# RealWorld, ThreadId# #)
1490 primOpInfo ForkOp       
1491   = mkGenPrimOp SLIT("fork#") [alphaTyVar] 
1492         [alphaTy, realWorldStatePrimTy]
1493         (unboxedPair [realWorldStatePrimTy, threadIdPrimTy])
1494
1495 -- killThread# :: ThreadId# -> State# RealWorld -> State# RealWorld
1496 primOpInfo KillThreadOp
1497   = mkGenPrimOp SLIT("killThread#") [] 
1498         [threadIdPrimTy, realWorldStatePrimTy]
1499         realWorldStatePrimTy
1500 \end{code}
1501
1502 ************************************************************************
1503 %*                                                                      *
1504 \subsubsection[PrimOps-Foreign]{PrimOpInfo for Foreign Objects}
1505 %*                                                                      *
1506 %************************************************************************
1507
1508 \begin{code}
1509 primOpInfo MakeForeignObjOp
1510   = mkGenPrimOp SLIT("makeForeignObj#") [] 
1511         [addrPrimTy, realWorldStatePrimTy] 
1512         (unboxedPair [realWorldStatePrimTy, foreignObjPrimTy])
1513
1514 primOpInfo WriteForeignObjOp
1515  = let {
1516         s = alphaTy; s_tv = alphaTyVar
1517     } in
1518    mkGenPrimOp SLIT("writeForeignObj#") [s_tv]
1519         [foreignObjPrimTy, addrPrimTy, mkStatePrimTy s] (mkStatePrimTy s)
1520 \end{code}
1521
1522 ************************************************************************
1523 %*                                                                      *
1524 \subsubsection[PrimOps-Weak]{PrimOpInfo for Weak Pointers}
1525 %*                                                                      *
1526 %************************************************************************
1527
1528 A @Weak@ Pointer is created by the @mkWeak#@ primitive:
1529
1530         mkWeak# :: k -> v -> f -> State# RealWorld 
1531                         -> (# State# RealWorld, Weak# v #)
1532
1533 In practice, you'll use the higher-level
1534
1535         data Weak v = Weak# v
1536         mkWeak :: k -> v -> IO () -> IO (Weak v)
1537
1538 \begin{code}
1539 primOpInfo MkWeakOp
1540   = mkGenPrimOp SLIT("mkWeak#") [alphaTyVar, betaTyVar, gammaTyVar] 
1541         [alphaTy, betaTy, gammaTy, realWorldStatePrimTy]
1542         (unboxedPair [realWorldStatePrimTy, mkWeakPrimTy betaTy])
1543 \end{code}
1544
1545 The following operation dereferences a weak pointer.  The weak pointer
1546 may have been finalised, so the operation returns a result code which
1547 must be inspected before looking at the dereferenced value.
1548
1549         deRefWeak# :: Weak# v -> State# RealWorld ->
1550                         (# State# RealWorld, v, Int# #)
1551
1552 Only look at v if the Int# returned is /= 0 !!
1553
1554 The higher-level op is
1555
1556         deRefWeak :: Weak v -> IO (Maybe v)
1557
1558 \begin{code}
1559 primOpInfo DeRefWeakOp
1560  = mkGenPrimOp SLIT("deRefWeak#") [alphaTyVar]
1561         [mkWeakPrimTy alphaTy, realWorldStatePrimTy]
1562         (unboxedTriple [realWorldStatePrimTy, intPrimTy, alphaTy])
1563 \end{code}
1564
1565 %************************************************************************
1566 %*                                                                      *
1567 \subsubsection[PrimOp-stable-pointers]{PrimOpInfo for ``stable pointers''}
1568 %*                                                                      *
1569 %************************************************************************
1570
1571 A {\em stable pointer} is an index into a table of pointers into the
1572 heap.  Since the garbage collector is told about stable pointers, it
1573 is safe to pass a stable pointer to external systems such as C
1574 routines.
1575
1576 Here's what the operations and types are supposed to be (from
1577 state-interface document).
1578
1579 \begin{verbatim}
1580 makeStablePtr#  :: a -> State# _RealWorld -> (# State# _RealWorld, a #)
1581 freeStablePtr#  :: StablePtr# a -> State# _RealWorld -> State# _RealWorld
1582 deRefStablePtr# :: StablePtr# a -> State# _RealWorld -> (# State# _RealWorld, a #)
1583 eqStablePtr#    :: StablePtr# a -> StablePtr# a -> Int#
1584 \end{verbatim}
1585
1586 It may seem a bit surprising that @makeStablePtr#@ is a @PrimIO@
1587 operation since it doesn't (directly) involve IO operations.  The
1588 reason is that if some optimisation pass decided to duplicate calls to
1589 @makeStablePtr#@ and we only pass one of the stable pointers over, a
1590 massive space leak can result.  Putting it into the PrimIO monad
1591 prevents this.  (Another reason for putting them in a monad is to
1592 ensure correct sequencing wrt the side-effecting @freeStablePtr#@
1593 operation.)
1594
1595 Note that we can implement @freeStablePtr#@ using @_ccall_@ (and,
1596 besides, it's not likely to be used from Haskell) so it's not a
1597 primop.
1598
1599 Question: Why @_RealWorld@ - won't any instance of @_ST@ do the job? [ADR]
1600
1601 \begin{code}
1602 primOpInfo MakeStablePtrOp
1603   = mkGenPrimOp SLIT("makeStablePtr#") [alphaTyVar]
1604         [alphaTy, realWorldStatePrimTy]
1605         (unboxedPair [realWorldStatePrimTy, 
1606                         mkTyConApp stablePtrPrimTyCon [alphaTy]])
1607
1608 primOpInfo DeRefStablePtrOp
1609   = mkGenPrimOp SLIT("deRefStablePtr#") [alphaTyVar]
1610         [mkStablePtrPrimTy alphaTy, realWorldStatePrimTy]
1611         (unboxedPair [realWorldStatePrimTy, alphaTy])
1612
1613 primOpInfo EqStablePtrOp
1614   = mkGenPrimOp SLIT("eqStablePtr#") [alphaTyVar, betaTyVar]
1615         [mkStablePtrPrimTy alphaTy, mkStablePtrPrimTy betaTy]
1616         intPrimTy
1617 \end{code}
1618
1619 %************************************************************************
1620 %*                                                                      *
1621 \subsubsection[PrimOp-unsafePointerEquality]{PrimOpInfo for Pointer Equality}
1622 %*                                                                      *
1623 %************************************************************************
1624
1625 [Alastair Reid is to blame for this!]
1626
1627 These days, (Glasgow) Haskell seems to have a bit of everything from
1628 other languages: strict operations, mutable variables, sequencing,
1629 pointers, etc.  About the only thing left is LISP's ability to test
1630 for pointer equality.  So, let's add it in!
1631
1632 \begin{verbatim}
1633 reallyUnsafePtrEquality :: a -> a -> Int#
1634 \end{verbatim}
1635
1636 which tests any two closures (of the same type) to see if they're the
1637 same.  (Returns $0$ for @False@, $\neq 0$ for @True@ - to avoid
1638 difficulties of trying to box up the result.)
1639
1640 NB This is {\em really unsafe\/} because even something as trivial as
1641 a garbage collection might change the answer by removing indirections.
1642 Still, no-one's forcing you to use it.  If you're worried about little
1643 things like loss of referential transparency, you might like to wrap
1644 it all up in a monad-like thing as John O'Donnell and John Hughes did
1645 for non-determinism (1989 (Fraserburgh) Glasgow FP Workshop
1646 Proceedings?)
1647
1648 I'm thinking of using it to speed up a critical equality test in some
1649 graphics stuff in a context where the possibility of saying that
1650 denotationally equal things aren't isn't a problem (as long as it
1651 doesn't happen too often.)  ADR
1652
1653 To Will: Jim said this was already in, but I can't see it so I'm
1654 adding it.  Up to you whether you add it.  (Note that this could have
1655 been readily implemented using a @veryDangerousCCall@ before they were
1656 removed...)
1657
1658 \begin{code}
1659 primOpInfo ReallyUnsafePtrEqualityOp
1660   = mkGenPrimOp SLIT("reallyUnsafePtrEquality#") [alphaTyVar]
1661         [alphaTy, alphaTy] intPrimTy
1662 \end{code}
1663
1664 %************************************************************************
1665 %*                                                                      *
1666 \subsubsection[PrimOp-parallel]{PrimOpInfo for parallelism op(s)}
1667 %*                                                                      *
1668 %************************************************************************
1669
1670 \begin{code}
1671 primOpInfo SeqOp        -- seq# :: a -> Int#
1672   = mkGenPrimOp SLIT("seq#")    [alphaTyVar] [alphaTy] intPrimTy
1673
1674 primOpInfo ParOp        -- par# :: a -> Int#
1675   = mkGenPrimOp SLIT("par#")    [alphaTyVar] [alphaTy] intPrimTy
1676 \end{code}
1677
1678 \begin{code}
1679 -- HWL: The first 4 Int# in all par... annotations denote:
1680 --   name, granularity info, size of result, degree of parallelism
1681 --      Same  structure as _seq_ i.e. returns Int#
1682
1683 primOpInfo ParGlobalOp  -- parGlobal# :: Int# -> Int# -> Int# -> Int# -> a -> b -> b
1684   = mkGenPrimOp SLIT("parGlobal#")      [alphaTyVar,betaTyVar] [alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,betaTy] intPrimTy
1685
1686 primOpInfo ParLocalOp   -- parLocal# :: Int# -> Int# -> Int# -> Int# -> a -> b -> b
1687   = mkGenPrimOp SLIT("parLocal#")       [alphaTyVar,betaTyVar] [alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,betaTy] intPrimTy
1688
1689 primOpInfo ParAtOp      -- parAt# :: Int# -> Int# -> Int# -> Int# -> a -> b -> c -> c
1690   = mkGenPrimOp SLIT("parAt#")  [alphaTyVar,betaTyVar,gammaTyVar] [betaTy,alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,gammaTy] intPrimTy
1691
1692 primOpInfo ParAtAbsOp   -- parAtAbs# :: Int# -> Int# -> Int# -> Int# -> Int# -> a -> b -> b
1693   = mkGenPrimOp SLIT("parAtAbs#")       [alphaTyVar,betaTyVar] [alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,betaTy] intPrimTy
1694
1695 primOpInfo ParAtRelOp   -- parAtRel# :: Int# -> Int# -> Int# -> Int# -> Int# -> a -> b -> b
1696   = mkGenPrimOp SLIT("parAtRel#")       [alphaTyVar,betaTyVar] [alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,betaTy] intPrimTy
1697
1698 primOpInfo ParAtForNowOp        -- parAtForNow# :: Int# -> Int# -> Int# -> Int# -> a -> b -> c -> c
1699   = mkGenPrimOp SLIT("parAtForNow#")    [alphaTyVar,betaTyVar,gammaTyVar] [betaTy,alphaTy,intPrimTy,intPrimTy,intPrimTy,intPrimTy,gammaTy] intPrimTy
1700
1701 primOpInfo CopyableOp   -- copyable# :: a -> a
1702   = mkGenPrimOp SLIT("copyable#")       [alphaTyVar] [alphaTy] intPrimTy
1703
1704 primOpInfo NoFollowOp   -- noFollow# :: a -> a
1705   = mkGenPrimOp SLIT("noFollow#")       [alphaTyVar] [alphaTy] intPrimTy
1706 \end{code}
1707
1708 %************************************************************************
1709 %*                                                                      *
1710 \subsubsection[PrimOp-IO-etc]{PrimOpInfo for C calls, and I/O-ish things}
1711 %*                                                                      *
1712 %************************************************************************
1713
1714 \begin{code}
1715 primOpInfo (CCallOp _ _ _ _)
1716      = mkGenPrimOp SLIT("ccall#") [alphaTyVar] [] alphaTy
1717
1718 {-
1719 primOpInfo (CCallOp _ _ _ _ arg_tys result_ty)
1720   = mkGenPrimOp SLIT("ccall#") [] arg_tys result_tycon tys_applied
1721   where
1722     (result_tycon, tys_applied, _) = splitAlgTyConApp result_ty
1723 -}
1724 #ifdef DEBUG
1725 primOpInfo op = panic ("primOpInfo:"++ show (I# (tagOf_PrimOp op)))
1726 #endif
1727 \end{code}
1728
1729 Some PrimOps need to be called out-of-line because they either need to
1730 perform a heap check or they block.
1731
1732 \begin{code}
1733 primOpOutOfLine op
1734   = case op of
1735         TakeMVarOp              -> True
1736         PutMVarOp               -> True
1737         DelayOp                 -> True
1738         WaitReadOp              -> True
1739         WaitWriteOp             -> True
1740         CatchOp                 -> True
1741         RaiseOp                 -> True
1742         NewArrayOp              -> True
1743         NewByteArrayOp _        -> True
1744         IntegerAddOp            -> True
1745         IntegerSubOp            -> True
1746         IntegerMulOp            -> True
1747         IntegerGcdOp            -> True
1748         IntegerQuotRemOp        -> True
1749         IntegerDivModOp         -> True
1750         Int2IntegerOp           -> True
1751         Word2IntegerOp          -> True
1752         Addr2IntegerOp          -> True
1753         Word64ToIntegerOp       -> True
1754         Int64ToIntegerOp        -> True
1755         FloatDecodeOp           -> True
1756         DoubleDecodeOp          -> True
1757         MkWeakOp                -> True
1758         DeRefWeakOp             -> True
1759         MakeForeignObjOp        -> True
1760         MakeStablePtrOp         -> True
1761         NewMutVarOp             -> True
1762         NewMVarOp               -> True
1763         ForkOp                  -> True
1764         KillThreadOp            -> True
1765         CCallOp _ _ may_gc@True _ -> True       -- _ccall_GC_
1766         _                       -> False
1767 \end{code}
1768
1769 Sometimes we may choose to execute a PrimOp even though it isn't
1770 certain that its result will be required; ie execute them
1771 ``speculatively''.  The same thing as ``cheap eagerness.'' Usually
1772 this is OK, because PrimOps are usually cheap, but it isn't OK for
1773 (a)~expensive PrimOps and (b)~PrimOps which can fail.
1774
1775 See also @primOpIsCheap@ (below).
1776
1777 PrimOps that have side effects also should not be executed speculatively
1778 or by data dependencies.
1779
1780 \begin{code}
1781 primOpOkForSpeculation :: PrimOp -> Bool
1782 primOpOkForSpeculation op 
1783   = not (primOpCanFail op || primOpHasSideEffects op || primOpOutOfLine op)
1784 \end{code}
1785
1786 @primOpIsCheap@, as used in \tr{SimplUtils.lhs}.  For now (HACK
1787 WARNING), we just borrow some other predicates for a
1788 what-should-be-good-enough test.  "Cheap" means willing to call it more
1789 than once.  Evaluation order is unaffected.
1790
1791 \begin{code}
1792 primOpIsCheap op = not (primOpHasSideEffects op || primOpOutOfLine op)
1793 \end{code}
1794
1795 \begin{code}
1796 primOpCanFail :: PrimOp -> Bool
1797 -- Int.
1798 primOpCanFail IntQuotOp = True          -- Divide by zero
1799 primOpCanFail IntRemOp          = True          -- Divide by zero
1800
1801 -- Integer
1802 primOpCanFail IntegerQuotRemOp = True           -- Divide by zero
1803 primOpCanFail IntegerDivModOp   = True          -- Divide by zero
1804
1805 -- Float.  ToDo: tan? tanh?
1806 primOpCanFail FloatDivOp        = True          -- Divide by zero
1807 primOpCanFail FloatLogOp        = True          -- Log of zero
1808 primOpCanFail FloatAsinOp       = True          -- Arg out of domain
1809 primOpCanFail FloatAcosOp       = True          -- Arg out of domain
1810
1811 -- Double.  ToDo: tan? tanh?
1812 primOpCanFail DoubleDivOp       = True          -- Divide by zero
1813 primOpCanFail DoubleLogOp       = True          -- Log of zero
1814 primOpCanFail DoubleAsinOp      = True          -- Arg out of domain
1815 primOpCanFail DoubleAcosOp      = True          -- Arg out of domain
1816
1817 primOpCanFail other_op          = False
1818 \end{code}
1819
1820 And some primops have side-effects and so, for example, must not be
1821 duplicated.
1822
1823 \begin{code}
1824 primOpHasSideEffects :: PrimOp -> Bool
1825
1826 primOpHasSideEffects TakeMVarOp        = True
1827 primOpHasSideEffects DelayOp           = True
1828 primOpHasSideEffects WaitReadOp        = True
1829 primOpHasSideEffects WaitWriteOp       = True
1830
1831 primOpHasSideEffects ParOp             = True
1832 primOpHasSideEffects ForkOp            = True
1833 primOpHasSideEffects KillThreadOp      = True
1834 primOpHasSideEffects SeqOp             = True
1835
1836 primOpHasSideEffects MakeForeignObjOp  = True
1837 primOpHasSideEffects WriteForeignObjOp = True
1838 primOpHasSideEffects MkWeakOp          = True
1839 primOpHasSideEffects DeRefWeakOp       = True
1840 primOpHasSideEffects MakeStablePtrOp   = True
1841 primOpHasSideEffects EqStablePtrOp     = True  -- SOF
1842 primOpHasSideEffects DeRefStablePtrOp  = True  -- ??? JSM & ADR
1843
1844 primOpHasSideEffects ParGlobalOp        = True
1845 primOpHasSideEffects ParLocalOp         = True
1846 primOpHasSideEffects ParAtOp            = True
1847 primOpHasSideEffects ParAtAbsOp         = True
1848 primOpHasSideEffects ParAtRelOp         = True
1849 primOpHasSideEffects ParAtForNowOp      = True
1850 primOpHasSideEffects CopyableOp         = True  -- Possibly not.  ASP 
1851 primOpHasSideEffects NoFollowOp         = True  -- Possibly not.  ASP
1852
1853 -- CCall
1854 primOpHasSideEffects (CCallOp   _ _ _ _) = True
1855
1856 primOpHasSideEffects other = False
1857 \end{code}
1858
1859 Inline primitive operations that perform calls need wrappers to save
1860 any live variables that are stored in caller-saves registers.
1861
1862 \begin{code}
1863 primOpNeedsWrapper :: PrimOp -> Bool
1864
1865 primOpNeedsWrapper (CCallOp _ _ _ _)    = True
1866
1867 primOpNeedsWrapper Integer2IntOp        = True
1868 primOpNeedsWrapper Integer2WordOp       = True
1869 primOpNeedsWrapper IntegerCmpOp         = True
1870
1871 primOpNeedsWrapper FloatExpOp           = True
1872 primOpNeedsWrapper FloatLogOp           = True
1873 primOpNeedsWrapper FloatSqrtOp          = True
1874 primOpNeedsWrapper FloatSinOp           = True
1875 primOpNeedsWrapper FloatCosOp           = True
1876 primOpNeedsWrapper FloatTanOp           = True
1877 primOpNeedsWrapper FloatAsinOp          = True
1878 primOpNeedsWrapper FloatAcosOp          = True
1879 primOpNeedsWrapper FloatAtanOp          = True
1880 primOpNeedsWrapper FloatSinhOp          = True
1881 primOpNeedsWrapper FloatCoshOp          = True
1882 primOpNeedsWrapper FloatTanhOp          = True
1883 primOpNeedsWrapper FloatPowerOp         = True
1884 primOpNeedsWrapper FloatEncodeOp        = True
1885
1886 primOpNeedsWrapper DoubleExpOp          = True
1887 primOpNeedsWrapper DoubleLogOp          = True
1888 primOpNeedsWrapper DoubleSqrtOp         = True
1889 primOpNeedsWrapper DoubleSinOp          = True
1890 primOpNeedsWrapper DoubleCosOp          = True
1891 primOpNeedsWrapper DoubleTanOp          = True
1892 primOpNeedsWrapper DoubleAsinOp         = True
1893 primOpNeedsWrapper DoubleAcosOp         = True
1894 primOpNeedsWrapper DoubleAtanOp         = True
1895 primOpNeedsWrapper DoubleSinhOp         = True
1896 primOpNeedsWrapper DoubleCoshOp         = True
1897 primOpNeedsWrapper DoubleTanhOp         = True
1898 primOpNeedsWrapper DoublePowerOp        = True
1899 primOpNeedsWrapper DoubleEncodeOp       = True
1900
1901 primOpNeedsWrapper MakeStablePtrOp      = True
1902 primOpNeedsWrapper DeRefStablePtrOp     = True
1903
1904 primOpNeedsWrapper DelayOp              = True
1905 primOpNeedsWrapper WaitReadOp           = True
1906 primOpNeedsWrapper WaitWriteOp          = True
1907
1908 primOpNeedsWrapper other_op             = False
1909 \end{code}
1910
1911 \begin{code}
1912 primOpOcc op
1913   = case (primOpInfo op) of
1914       Dyadic     occ _         -> occ
1915       Monadic    occ _         -> occ
1916       Compare    occ _         -> occ
1917       GenPrimOp  occ _ _ _     -> occ
1918 \end{code}
1919
1920 \begin{code}
1921 primOpUniq :: PrimOp -> Unique
1922 primOpUniq op = mkPrimOpIdUnique (IBOX(tagOf_PrimOp op))
1923
1924 primOpType :: PrimOp -> Type
1925 primOpType op
1926   = case (primOpInfo op) of
1927       Dyadic occ ty ->      dyadic_fun_ty ty
1928       Monadic occ ty ->     monadic_fun_ty ty
1929       Compare occ ty ->     compare_fun_ty ty
1930
1931       GenPrimOp occ tyvars arg_tys res_ty -> 
1932         mkForAllTys tyvars (mkFunTys arg_tys res_ty)
1933 \end{code}
1934
1935 \begin{code}
1936 data PrimOpResultInfo
1937   = ReturnsPrim     PrimRep
1938   | ReturnsAlg      TyCon
1939
1940 -- Some PrimOps need not return a manifest primitive or algebraic value
1941 -- (i.e. they might return a polymorphic value).  These PrimOps *must*
1942 -- be out of line, or the code generator won't work.
1943
1944 getPrimOpResultInfo :: PrimOp -> PrimOpResultInfo
1945
1946 getPrimOpResultInfo op
1947   = case (primOpInfo op) of
1948       Dyadic  _ ty               -> ReturnsPrim (typePrimRep ty)
1949       Monadic _ ty               -> ReturnsPrim (typePrimRep ty)
1950       Compare _ ty               -> ReturnsAlg  boolTyCon
1951       GenPrimOp _ _ _ ty         -> 
1952         let rep = typePrimRep ty in
1953         case rep of
1954            PtrRep -> case splitAlgTyConApp_maybe ty of
1955                         Nothing -> panic "getPrimOpResultInfo"
1956                         Just (tc,_,_) -> ReturnsAlg tc
1957            other -> ReturnsPrim other
1958
1959 isCompareOp :: PrimOp -> Bool
1960
1961 isCompareOp op
1962   = case primOpInfo op of
1963       Compare _ _ -> True
1964       _           -> False
1965 \end{code}
1966
1967 The commutable ops are those for which we will try to move constants
1968 to the right hand side for strength reduction.
1969
1970 \begin{code}
1971 commutableOp :: PrimOp -> Bool
1972
1973 commutableOp CharEqOp     = True
1974 commutableOp CharNeOp     = True
1975 commutableOp IntAddOp     = True
1976 commutableOp IntMulOp     = True
1977 commutableOp AndOp        = True
1978 commutableOp OrOp         = True
1979 commutableOp XorOp        = True
1980 commutableOp IntEqOp      = True
1981 commutableOp IntNeOp      = True
1982 commutableOp IntegerAddOp = True
1983 commutableOp IntegerMulOp = True
1984 commutableOp IntegerGcdOp = True
1985 commutableOp FloatAddOp   = True
1986 commutableOp FloatMulOp   = True
1987 commutableOp FloatEqOp    = True
1988 commutableOp FloatNeOp    = True
1989 commutableOp DoubleAddOp  = True
1990 commutableOp DoubleMulOp  = True
1991 commutableOp DoubleEqOp   = True
1992 commutableOp DoubleNeOp   = True
1993 commutableOp _            = False
1994 \end{code}
1995
1996 Utils:
1997 \begin{code}
1998 dyadic_fun_ty  ty = mkFunTys [ty, ty] ty
1999 monadic_fun_ty ty = mkFunTy  ty ty
2000 compare_fun_ty ty = mkFunTys [ty, ty] boolTy
2001 \end{code}
2002
2003 Output stuff:
2004 \begin{code}
2005 pprPrimOp  :: PrimOp -> SDoc
2006
2007 pprPrimOp (CCallOp fun is_casm may_gc cconv)
2008   = let
2009         callconv = text "{-" <> pprCallConv cconv <> text "-}"
2010
2011         before
2012           | is_casm && may_gc = "__casm_GC ``"
2013           | is_casm           = "__casm ``"
2014           | may_gc            = "__ccall_GC "
2015           | otherwise         = "__ccall "
2016
2017         after
2018           | is_casm   = text "''"
2019           | otherwise = empty
2020
2021         ppr_fun =
2022          case fun of
2023            Right _ -> ptext SLIT("<dynamic>")
2024            Left fn -> ptext fn
2025          
2026     in
2027     hcat [ ifPprDebug callconv
2028          , text before , ppr_fun , after]
2029
2030 pprPrimOp other_op
2031   = getPprStyle $ \ sty ->
2032    if ifaceStyle sty then       -- For interfaces Print it qualified with PrelGHC.
2033         ptext SLIT("PrelGHC.") <> pprOccName occ
2034    else
2035         pprOccName occ
2036   where
2037     occ = primOpOcc other_op
2038 \end{code}