Fix some inconsistencies in the code and docs of primitives
[ghc-hetmet.git] / compiler / prelude / primops.txt.pp
1 -----------------------------------------------------------------------
2 -- $Id: primops.txt.pp,v 1.37 2005/11/25 09:46:19 simonmar Exp $
3 --
4 -- Primitive Operations and Types
5 --
6 -----------------------------------------------------------------------
7
8 -- This file is processed by the utility program genprimopcode to produce
9 -- a number of include files within the compiler and optionally to produce
10 -- human-readable documentation.
11 --
12 -- It should first be preprocessed.
13 --
14 -- To add a new primop, you currently need to update the following files:
15 --
16 --      - this file (ghc/compiler/prelude/primops.txt.pp), which includes
17 --        the type of the primop, and various other properties (its
18 --        strictness attributes, whether it is defined as a macro
19 --        or as out-of-line code, etc.)
20 --
21 --      - if the primop is inline (i.e. a macro), then:
22 --              ghc/compiler/AbsCUtils.lhs (dscCOpStmt)
23 --                defines the translation of the primop into simpler
24 --                abstract C operations.
25 --              
26 --      - or, for an out-of-line primop:
27 --              ghc/includes/StgMiscClosures.h (just add the declaration)
28 --              ghc/rts/PrimOps.cmm     (define it here)
29 --              ghc/rts/Linker.c       (declare the symbol for GHCi)
30 --
31 --      - the User's Guide 
32 --
33
34 -- This file is divided into named sections, each containing or more
35 -- primop entries. Section headers have the format:
36 --
37 --      section "section-name" {description}
38 --
39 -- This information is used solely when producing documentation; it is
40 -- otherwise ignored.  The description is optional.
41 --
42 -- The format of each primop entry is as follows:
43 --
44 --      primop internal-name "name-in-program-text" type category {description} attributes
45
46 -- The default attribute values which apply if you don't specify
47 -- other ones.  Attribute values can be True, False, or arbitrary
48 -- text between curly brackets.  This is a kludge to enable 
49 -- processors of this file to easily get hold of simple info
50 -- (eg, out_of_line), whilst avoiding parsing complex expressions
51 -- needed for strictness info.
52
53 defaults
54    has_side_effects = False
55    out_of_line      = False
56    commutable       = False
57    needs_wrapper    = False
58    can_fail         = False
59    strictness       = { \ arity -> mkStrictSig (mkTopDmdType (replicate arity lazyDmd) TopRes) }
60
61 -- Currently, documentation is produced using latex, so contents of
62 -- description fields should be legal latex. Descriptions can contain
63 -- matched pairs of embedded curly brackets.
64
65 #include "MachDeps.h"
66
67 -- We need platform defines (tests for mingw32 below).  However, we only
68 -- test the TARGET platform, which doesn't vary between stages, so the
69 -- stage1 platform defines are fine:
70 #include "../stage1/ghc_boot_platform.h"
71
72 section "The word size story."
73         {Haskell98 specifies that signed integers (type {\tt Int})
74          must contain at least 30 bits. GHC always implements {\tt
75          Int} using the primitive type {\tt Int\#}, whose size equals
76          the {\tt MachDeps.h} constant {\tt WORD\_SIZE\_IN\_BITS}.
77          This is normally set based on the {\tt config.h} parameter
78          {\tt SIZEOF\_HSWORD}, i.e., 32 bits on 32-bit machines, 64
79          bits on 64-bit machines.  However, it can also be explicitly
80          set to a smaller number, e.g., 31 bits, to allow the
81          possibility of using tag bits. Currently GHC itself has only
82          32-bit and 64-bit variants, but 30 or 31-bit code can be
83          exported as an external core file for use in other back ends.
84
85          GHC also implements a primitive unsigned integer type {\tt
86          Word\#} which always has the same number of bits as {\tt
87          Int\#}.
88         
89          In addition, GHC supports families of explicit-sized integers
90          and words at 8, 16, 32, and 64 bits, with the usual
91          arithmetic operations, comparisons, and a range of
92          conversions.  The 8-bit and 16-bit sizes are always
93          represented as {\tt Int\#} and {\tt Word\#}, and the
94          operations implemented in terms of the the primops on these
95          types, with suitable range restrictions on the results (using
96          the {\tt narrow$n$Int\#} and {\tt narrow$n$Word\#} families
97          of primops.  The 32-bit sizes are represented using {\tt
98          Int\#} and {\tt Word\#} when {\tt WORD\_SIZE\_IN\_BITS}
99          $\geq$ 32; otherwise, these are represented using distinct
100          primitive types {\tt Int32\#} and {\tt Word32\#}. These (when
101          needed) have a complete set of corresponding operations;
102          however, nearly all of these are implemented as external C
103          functions rather than as primops.  Exactly the same story
104          applies to the 64-bit sizes.  All of these details are hidden
105          under the {\tt PrelInt} and {\tt PrelWord} modules, which use
106          {\tt \#if}-defs to invoke the appropriate types and
107          operators.
108
109          Word size also matters for the families of primops for
110          indexing/reading/writing fixed-size quantities at offsets
111          from an array base, address, or foreign pointer.  Here, a
112          slightly different approach is taken.  The names of these
113          primops are fixed, but their {\it types} vary according to
114          the value of {\tt WORD\_SIZE\_IN\_BITS}. For example, if word
115          size is at least 32 bits then an operator like
116          \texttt{indexInt32Array\#} has type {\tt ByteArray\# -> Int\#
117          -> Int\#}; otherwise it has type {\tt ByteArray\# -> Int\# ->
118          Int32\#}.  This approach confines the necessary {\tt
119          \#if}-defs to this file; no conditional compilation is needed
120          in the files that expose these primops.
121
122          Finally, there are strongly deprecated primops for coercing
123          between {\tt Addr\#}, the primitive type of machine
124          addresses, and {\tt Int\#}.  These are pretty bogus anyway,
125          but will work on existing 32-bit and 64-bit GHC targets; they
126          are completely bogus when tag bits are used in {\tt Int\#},
127          so are not available in this case.  }
128         
129 -- Define synonyms for indexing ops. 
130
131 #if WORD_SIZE_IN_BITS < 32 
132 #define INT32 Int32#
133 #define WORD32 Word32#
134 #else
135 #define INT32 Int#
136 #define WORD32 Word#
137 #endif
138
139 #if WORD_SIZE_IN_BITS < 64
140 #define INT64 Int64#
141 #define WORD64 Word64#
142 #else
143 #define INT64 Int#
144 #define WORD64 Word#
145 #endif
146
147 ------------------------------------------------------------------------
148 section "Char#" 
149         {Operations on 31-bit characters.}
150 ------------------------------------------------------------------------
151
152 primtype Char#
153
154 primop   CharGtOp  "gtChar#"   Compare   Char# -> Char# -> Bool
155 primop   CharGeOp  "geChar#"   Compare   Char# -> Char# -> Bool
156
157 primop   CharEqOp  "eqChar#"   Compare
158    Char# -> Char# -> Bool
159    with commutable = True
160
161 primop   CharNeOp  "neChar#"   Compare
162    Char# -> Char# -> Bool
163    with commutable = True
164
165 primop   CharLtOp  "ltChar#"   Compare   Char# -> Char# -> Bool
166 primop   CharLeOp  "leChar#"   Compare   Char# -> Char# -> Bool
167
168 primop   OrdOp   "ord#"  GenPrimOp   Char# -> Int#
169
170 ------------------------------------------------------------------------
171 section "Int#"
172         {Operations on native-size integers (30+ bits).}
173 ------------------------------------------------------------------------
174
175 primtype Int#
176
177 primop   IntAddOp    "+#"    Dyadic
178    Int# -> Int# -> Int#
179    with commutable = True
180
181 primop   IntSubOp    "-#"    Dyadic   Int# -> Int# -> Int#
182
183 primop   IntMulOp    "*#" 
184    Dyadic   Int# -> Int# -> Int#
185    {Low word of signed integer multiply.}
186    with commutable = True
187
188 primop   IntMulMayOfloOp  "mulIntMayOflo#" 
189    Dyadic   Int# -> Int# -> Int#
190    {Return non-zero if there is any possibility that the upper word of a
191     signed integer multiply might contain useful information.  Return
192     zero only if you are completely sure that no overflow can occur.
193     On a 32-bit platform, the recommmended implementation is to do a 
194     32 x 32 -> 64 signed multiply, and subtract result[63:32] from
195     (result[31] >>signed 31).  If this is zero, meaning that the 
196     upper word is merely a sign extension of the lower one, no
197     overflow can occur.
198
199     On a 64-bit platform it is not always possible to 
200     acquire the top 64 bits of the result.  Therefore, a recommended 
201     implementation is to take the absolute value of both operands, and 
202     return 0 iff bits[63:31] of them are zero, since that means that their 
203     magnitudes fit within 31 bits, so the magnitude of the product must fit 
204     into 62 bits.
205
206     If in doubt, return non-zero, but do make an effort to create the
207     correct answer for small args, since otherwise the performance of
208     \texttt{(*) :: Integer -> Integer -> Integer} will be poor.
209    }
210    with commutable = True
211
212 primop   IntQuotOp    "quotInt#"    Dyadic
213    Int# -> Int# -> Int#
214    {Rounds towards zero.}
215    with can_fail = True
216
217 primop   IntRemOp    "remInt#"    Dyadic
218    Int# -> Int# -> Int#
219    {Satisfies \texttt{(quotInt\# x y) *\# y +\# (remInt\# x y) == x}.}
220    with can_fail = True
221
222 primop   IntGcdOp    "gcdInt#"    Dyadic   Int# -> Int# -> Int#
223    with out_of_line = True
224
225 primop   IntNegOp    "negateInt#"    Monadic   Int# -> Int#
226 primop   IntAddCOp   "addIntC#"    GenPrimOp   Int# -> Int# -> (# Int#, Int# #)
227          {Add with carry.  First member of result is (wrapped) sum; 
228           second member is 0 iff no overflow occured.}
229 primop   IntSubCOp   "subIntC#"    GenPrimOp   Int# -> Int# -> (# Int#, Int# #)
230          {Subtract with carry.  First member of result is (wrapped) difference; 
231           second member is 0 iff no overflow occured.}
232
233 primop   IntGtOp  ">#"   Compare   Int# -> Int# -> Bool
234 primop   IntGeOp  ">=#"   Compare   Int# -> Int# -> Bool
235
236 primop   IntEqOp  "==#"   Compare
237    Int# -> Int# -> Bool
238    with commutable = True
239
240 primop   IntNeOp  "/=#"   Compare
241    Int# -> Int# -> Bool
242    with commutable = True
243
244 primop   IntLtOp  "<#"   Compare   Int# -> Int# -> Bool
245 primop   IntLeOp  "<=#"   Compare   Int# -> Int# -> Bool
246
247 primop   ChrOp   "chr#"   GenPrimOp   Int# -> Char#
248
249 primop   Int2WordOp "int2Word#" GenPrimOp Int# -> Word#
250 primop   Int2FloatOp   "int2Float#"      GenPrimOp  Int# -> Float#
251 primop   Int2DoubleOp   "int2Double#"          GenPrimOp  Int# -> Double#
252
253 primop   Int2IntegerOp    "int2Integer#"
254    GenPrimOp Int# -> (# Int#, ByteArray# #)
255    with out_of_line = True
256
257 primop   ISllOp   "uncheckedIShiftL#" GenPrimOp  Int# -> Int# -> Int#
258          {Shift left.  Result undefined if shift amount is not
259           in the range 0 to word size - 1 inclusive.}
260 primop   ISraOp   "uncheckedIShiftRA#" GenPrimOp Int# -> Int# -> Int#
261          {Shift right arithmetic.  Result undefined if shift amount is not
262           in the range 0 to word size - 1 inclusive.}
263 primop   ISrlOp   "uncheckedIShiftRL#" GenPrimOp Int# -> Int# -> Int#
264          {Shift right logical.  Result undefined if shift amount is not
265           in the range 0 to word size - 1 inclusive.}
266
267 ------------------------------------------------------------------------
268 section "Word#"
269         {Operations on native-sized unsigned words (30+ bits).}
270 ------------------------------------------------------------------------
271
272 primtype Word#
273
274 primop   WordAddOp   "plusWord#"   Dyadic   Word# -> Word# -> Word#
275    with commutable = True
276
277 primop   WordSubOp   "minusWord#"   Dyadic   Word# -> Word# -> Word#
278
279 primop   WordMulOp   "timesWord#"   Dyadic   Word# -> Word# -> Word#
280    with commutable = True
281
282 primop   WordQuotOp   "quotWord#"   Dyadic   Word# -> Word# -> Word#
283    with can_fail = True
284
285 primop   WordRemOp   "remWord#"   Dyadic   Word# -> Word# -> Word#
286    with can_fail = True
287
288 primop   AndOp   "and#"   Dyadic   Word# -> Word# -> Word#
289    with commutable = True
290
291 primop   OrOp   "or#"   Dyadic   Word# -> Word# -> Word#
292    with commutable = True
293
294 primop   XorOp   "xor#"   Dyadic   Word# -> Word# -> Word#
295    with commutable = True
296
297 primop   NotOp   "not#"   Monadic   Word# -> Word#
298
299 primop   SllOp   "uncheckedShiftL#"   GenPrimOp   Word# -> Int# -> Word#
300          {Shift left logical.   Result undefined if shift amount is not
301           in the range 0 to word size - 1 inclusive.}
302 primop   SrlOp   "uncheckedShiftRL#"   GenPrimOp   Word# -> Int# -> Word#
303          {Shift right logical.   Result undefined if shift  amount is not
304           in the range 0 to word size - 1 inclusive.}
305
306 primop   Word2IntOp   "word2Int#"   GenPrimOp   Word# -> Int#
307
308 primop   Word2IntegerOp   "word2Integer#"   GenPrimOp 
309    Word# -> (# Int#, ByteArray# #)
310    with out_of_line = True
311
312 primop   WordGtOp   "gtWord#"   Compare   Word# -> Word# -> Bool
313 primop   WordGeOp   "geWord#"   Compare   Word# -> Word# -> Bool
314 primop   WordEqOp   "eqWord#"   Compare   Word# -> Word# -> Bool
315 primop   WordNeOp   "neWord#"   Compare   Word# -> Word# -> Bool
316 primop   WordLtOp   "ltWord#"   Compare   Word# -> Word# -> Bool
317 primop   WordLeOp   "leWord#"   Compare   Word# -> Word# -> Bool
318
319 ------------------------------------------------------------------------
320 section "Narrowings" 
321         {Explicit narrowing of native-sized ints or words.}
322 ------------------------------------------------------------------------
323
324 primop   Narrow8IntOp      "narrow8Int#"      Monadic   Int# -> Int#
325 primop   Narrow16IntOp     "narrow16Int#"     Monadic   Int# -> Int#
326 primop   Narrow32IntOp     "narrow32Int#"     Monadic   Int# -> Int#
327 primop   Narrow8WordOp     "narrow8Word#"     Monadic   Word# -> Word#
328 primop   Narrow16WordOp    "narrow16Word#"    Monadic   Word# -> Word#
329 primop   Narrow32WordOp    "narrow32Word#"    Monadic   Word# -> Word#
330
331
332 #if WORD_SIZE_IN_BITS < 32
333 ------------------------------------------------------------------------
334 section "Int32#"
335         {Operations on 32-bit integers ({\tt Int32\#}).  This type is only used
336          if plain {\tt Int\#} has less than 32 bits.  In any case, the operations
337          are not primops; they are implemented (if needed) as ccalls instead.}
338 ------------------------------------------------------------------------
339
340 primtype Int32#
341
342 primop   Int32ToIntegerOp   "int32ToInteger#" GenPrimOp 
343    Int32# -> (# Int#, ByteArray# #)
344    with out_of_line = True
345
346
347 ------------------------------------------------------------------------
348 section "Word32#"
349         {Operations on 32-bit unsigned words. This type is only used 
350          if plain {\tt Word\#} has less than 32 bits. In any case, the operations
351          are not primops; they are implemented (if needed) as ccalls instead.}
352 ------------------------------------------------------------------------
353
354 primtype Word32#
355
356 primop   Word32ToIntegerOp   "word32ToInteger#" GenPrimOp
357    Word32# -> (# Int#, ByteArray# #)
358    with out_of_line = True
359
360
361 #endif 
362
363
364 #if WORD_SIZE_IN_BITS < 64
365 ------------------------------------------------------------------------
366 section "Int64#"
367         {Operations on 64-bit unsigned words. This type is only used 
368          if plain {\tt Int\#} has less than 64 bits. In any case, the operations
369          are not primops; they are implemented (if needed) as ccalls instead.}
370 ------------------------------------------------------------------------
371
372 primtype Int64#
373
374 primop   Int64ToIntegerOp   "int64ToInteger#" GenPrimOp 
375    Int64# -> (# Int#, ByteArray# #)
376    with out_of_line = True
377
378 ------------------------------------------------------------------------
379 section "Word64#"
380         {Operations on 64-bit unsigned words. This type is only used 
381          if plain {\tt Word\#} has less than 64 bits. In any case, the operations
382          are not primops; they are implemented (if needed) as ccalls instead.}
383 ------------------------------------------------------------------------
384
385 primtype Word64#
386
387 primop   Word64ToIntegerOp   "word64ToInteger#" GenPrimOp
388    Word64# -> (# Int#, ByteArray# #)
389    with out_of_line = True
390
391 #endif
392
393 ------------------------------------------------------------------------
394 section "Integer#"
395         {Operations on arbitrary-precision integers. These operations are 
396 implemented via the GMP package. An integer is represented as a pair
397 consisting of an {\tt Int\#} representing the number of 'limbs' in use and
398 the sign, and a {\tt ByteArray\#} containing the 'limbs' themselves.  Such pairs
399 are returned as unboxed pairs, but must be passed as separate
400 components.
401
402 For .NET these operations are implemented by foreign imports, so the
403 primops are omitted.}
404 ------------------------------------------------------------------------
405
406 #ifndef ILX
407
408 primop   IntegerAddOp   "plusInteger#" GenPrimOp   
409    Int# -> ByteArray# -> Int# -> ByteArray# -> (# Int#, ByteArray# #)
410    with commutable = True
411         out_of_line = True
412
413 primop   IntegerSubOp   "minusInteger#" GenPrimOp  
414    Int# -> ByteArray# -> Int# -> ByteArray# -> (# Int#, ByteArray# #)
415    with out_of_line = True
416
417 primop   IntegerMulOp   "timesInteger#" GenPrimOp   
418    Int# -> ByteArray# -> Int# -> ByteArray# -> (# Int#, ByteArray# #)
419    with commutable = True
420         out_of_line = True
421
422 primop   IntegerGcdOp   "gcdInteger#" GenPrimOp    
423    Int# -> ByteArray# -> Int# -> ByteArray# -> (# Int#, ByteArray# #)
424    {Greatest common divisor.}
425    with commutable = True
426         out_of_line = True
427
428 primop   IntegerIntGcdOp   "gcdIntegerInt#" GenPrimOp
429    Int# -> ByteArray# -> Int# -> Int#
430    {Greatest common divisor, where second argument is an ordinary {\tt Int\#}.}
431    with out_of_line = True
432
433 primop   IntegerDivExactOp   "divExactInteger#" GenPrimOp
434    Int# -> ByteArray# -> Int# -> ByteArray# -> (# Int#, ByteArray# #)
435    {Divisor is guaranteed to be a factor of dividend.}
436    with out_of_line = True
437
438 primop   IntegerQuotOp   "quotInteger#" GenPrimOp
439    Int# -> ByteArray# -> Int# -> ByteArray# -> (# Int#, ByteArray# #)
440    {Rounds towards zero.}
441    with out_of_line = True
442
443 primop   IntegerRemOp   "remInteger#" GenPrimOp
444    Int# -> ByteArray# -> Int# -> ByteArray# -> (# Int#, ByteArray# #)
445    {Satisfies \texttt{plusInteger\# (timesInteger\# (quotInteger\# x y) y) (remInteger\# x y) == x}.}
446    with out_of_line = True
447
448 primop   IntegerCmpOp   "cmpInteger#"   GenPrimOp  
449    Int# -> ByteArray# -> Int# -> ByteArray# -> Int#
450    {Returns -1,0,1 according as first argument is less than, equal to, or greater than second argument.}
451    with needs_wrapper = True
452         out_of_line = True
453
454 primop   IntegerCmpIntOp   "cmpIntegerInt#" GenPrimOp
455    Int# -> ByteArray# -> Int# -> Int#
456    {Returns -1,0,1 according as first argument is less than, equal to, or greater than second argument, which
457    is an ordinary Int\#.}
458    with needs_wrapper = True
459         out_of_line = True
460
461 primop   IntegerQuotRemOp   "quotRemInteger#" GenPrimOp
462    Int# -> ByteArray# -> Int# -> ByteArray# -> (# Int#, ByteArray#, Int#, ByteArray# #)
463    {Compute quot and rem simulaneously.}
464    with can_fail = True
465         out_of_line = True
466
467 primop   IntegerDivModOp    "divModInteger#"  GenPrimOp
468    Int# -> ByteArray# -> Int# -> ByteArray# -> (# Int#, ByteArray#, Int#, ByteArray# #)
469    {Compute div and mod simultaneously, where div rounds towards negative infinity
470     and\texttt{(q,r) = divModInteger\#(x,y)} implies \texttt{plusInteger\# (timesInteger\# q y) r = x}.}
471    with can_fail = True
472         out_of_line = True
473
474 primop   Integer2IntOp   "integer2Int#"    GenPrimOp
475    Int# -> ByteArray# -> Int#
476    with needs_wrapper = True
477         out_of_line = True
478
479 primop   Integer2WordOp   "integer2Word#"   GenPrimOp
480    Int# -> ByteArray# -> Word#
481    with needs_wrapper = True
482         out_of_line = True
483
484 #if WORD_SIZE_IN_BITS < 32
485 primop   IntegerToInt32Op   "integerToInt32#" GenPrimOp
486    Int# -> ByteArray# -> Int32#
487
488 primop   IntegerToWord32Op   "integerToWord32#" GenPrimOp
489    Int# -> ByteArray# -> Word32#
490 #endif
491
492 primop   IntegerAndOp  "andInteger#" GenPrimOp
493    Int# -> ByteArray# -> Int# -> ByteArray# -> (# Int#, ByteArray# #)
494    with out_of_line = True
495
496 primop   IntegerOrOp  "orInteger#" GenPrimOp
497    Int# -> ByteArray# -> Int# -> ByteArray# -> (# Int#, ByteArray# #)
498    with out_of_line = True
499
500 primop   IntegerXorOp  "xorInteger#" GenPrimOp
501    Int# -> ByteArray# -> Int# -> ByteArray# -> (# Int#, ByteArray# #)
502    with out_of_line = True
503
504 primop   IntegerComplementOp  "complementInteger#" GenPrimOp
505    Int# -> ByteArray# -> (# Int#, ByteArray# #)
506    with out_of_line = True
507
508 #endif /* ndef ILX */
509
510 ------------------------------------------------------------------------
511 section "Double#"
512         {Operations on double-precision (64 bit) floating-point numbers.}
513 ------------------------------------------------------------------------
514
515 primtype Double#
516
517 primop   DoubleGtOp ">##"   Compare   Double# -> Double# -> Bool
518 primop   DoubleGeOp ">=##"   Compare   Double# -> Double# -> Bool
519
520 primop DoubleEqOp "==##"   Compare
521    Double# -> Double# -> Bool
522    with commutable = True
523
524 primop DoubleNeOp "/=##"   Compare
525    Double# -> Double# -> Bool
526    with commutable = True
527
528 primop   DoubleLtOp "<##"   Compare   Double# -> Double# -> Bool
529 primop   DoubleLeOp "<=##"   Compare   Double# -> Double# -> Bool
530
531 primop   DoubleAddOp   "+##"   Dyadic
532    Double# -> Double# -> Double#
533    with commutable = True
534
535 primop   DoubleSubOp   "-##"   Dyadic   Double# -> Double# -> Double#
536
537 primop   DoubleMulOp   "*##"   Dyadic
538    Double# -> Double# -> Double#
539    with commutable = True
540
541 primop   DoubleDivOp   "/##"   Dyadic
542    Double# -> Double# -> Double#
543    with can_fail = True
544
545 primop   DoubleNegOp   "negateDouble#"  Monadic   Double# -> Double#
546
547 primop   Double2IntOp   "double2Int#"          GenPrimOp  Double# -> Int#
548    {Truncates a {\tt Double#} value to the nearest {\tt Int#}.
549     Results are undefined if the truncation if truncation yields
550     a value outside the range of {\tt Int#}.}
551
552 primop   Double2FloatOp   "double2Float#" GenPrimOp Double# -> Float#
553
554 primop   DoubleExpOp   "expDouble#"      Monadic
555    Double# -> Double#
556    with needs_wrapper = True
557
558 primop   DoubleLogOp   "logDouble#"      Monadic         
559    Double# -> Double#
560    with
561    needs_wrapper = True
562    can_fail = True
563
564 primop   DoubleSqrtOp   "sqrtDouble#"      Monadic  
565    Double# -> Double#
566    with needs_wrapper = True
567
568 primop   DoubleSinOp   "sinDouble#"      Monadic          
569    Double# -> Double#
570    with needs_wrapper = True
571
572 primop   DoubleCosOp   "cosDouble#"      Monadic          
573    Double# -> Double#
574    with needs_wrapper = True
575
576 primop   DoubleTanOp   "tanDouble#"      Monadic          
577    Double# -> Double#
578    with needs_wrapper = True
579
580 primop   DoubleAsinOp   "asinDouble#"      Monadic 
581    Double# -> Double#
582    with
583    needs_wrapper = True
584    can_fail = True
585
586 primop   DoubleAcosOp   "acosDouble#"      Monadic  
587    Double# -> Double#
588    with
589    needs_wrapper = True
590    can_fail = True
591
592 primop   DoubleAtanOp   "atanDouble#"      Monadic  
593    Double# -> Double#
594    with
595    needs_wrapper = True
596
597 primop   DoubleSinhOp   "sinhDouble#"      Monadic  
598    Double# -> Double#
599    with needs_wrapper = True
600
601 primop   DoubleCoshOp   "coshDouble#"      Monadic  
602    Double# -> Double#
603    with needs_wrapper = True
604
605 primop   DoubleTanhOp   "tanhDouble#"      Monadic  
606    Double# -> Double#
607    with needs_wrapper = True
608
609 primop   DoublePowerOp   "**##" Dyadic  
610    Double# -> Double# -> Double#
611    {Exponentiation.}
612    with needs_wrapper = True
613
614 primop   DoubleDecodeOp   "decodeDouble#" GenPrimOp    
615    Double# -> (# Int#, Int#, ByteArray# #)
616    {Convert to arbitrary-precision integer.
617     First {\tt Int\#} in result is the exponent; second {\tt Int\#} and {\tt ByteArray\#}
618     represent an {\tt Integer\#} holding the mantissa.}
619    with out_of_line = True
620
621 primop   DoubleDecode_2IntOp   "decodeDouble_2Int#" GenPrimOp    
622    Double# -> (# Int#, Word#, Word#, Int# #)
623    {Convert to arbitrary-precision integer.
624     First component of the result is -1 or 1, indicating the sign of the
625     mantissa. The next two are the high and low 32 bits of the mantissa
626     respectively, and the last is the exponent.}
627    with out_of_line = True
628
629 ------------------------------------------------------------------------
630 section "Float#" 
631         {Operations on single-precision (32-bit) floating-point numbers.}
632 ------------------------------------------------------------------------
633
634 primtype Float#
635
636 primop   FloatGtOp  "gtFloat#"   Compare   Float# -> Float# -> Bool
637 primop   FloatGeOp  "geFloat#"   Compare   Float# -> Float# -> Bool
638
639 primop   FloatEqOp  "eqFloat#"   Compare
640    Float# -> Float# -> Bool
641    with commutable = True
642
643 primop   FloatNeOp  "neFloat#"   Compare
644    Float# -> Float# -> Bool
645    with commutable = True
646
647 primop   FloatLtOp  "ltFloat#"   Compare   Float# -> Float# -> Bool
648 primop   FloatLeOp  "leFloat#"   Compare   Float# -> Float# -> Bool
649
650 primop   FloatAddOp   "plusFloat#"      Dyadic            
651    Float# -> Float# -> Float#
652    with commutable = True
653
654 primop   FloatSubOp   "minusFloat#"      Dyadic      Float# -> Float# -> Float#
655
656 primop   FloatMulOp   "timesFloat#"      Dyadic    
657    Float# -> Float# -> Float#
658    with commutable = True
659
660 primop   FloatDivOp   "divideFloat#"      Dyadic  
661    Float# -> Float# -> Float#
662    with can_fail = True
663
664 primop   FloatNegOp   "negateFloat#"      Monadic    Float# -> Float#
665
666 primop   Float2IntOp   "float2Int#"      GenPrimOp  Float# -> Int#
667    {Truncates a {\tt Float#} value to the nearest {\tt Int#}.
668     Results are undefined if the truncation if truncation yields
669     a value outside the range of {\tt Int#}.}
670
671 primop   FloatExpOp   "expFloat#"      Monadic          
672    Float# -> Float#
673    with needs_wrapper = True
674
675 primop   FloatLogOp   "logFloat#"      Monadic          
676    Float# -> Float#
677    with needs_wrapper = True
678         can_fail = True
679
680 primop   FloatSqrtOp   "sqrtFloat#"      Monadic          
681    Float# -> Float#
682    with needs_wrapper = True
683
684 primop   FloatSinOp   "sinFloat#"      Monadic          
685    Float# -> Float#
686    with needs_wrapper = True
687
688 primop   FloatCosOp   "cosFloat#"      Monadic          
689    Float# -> Float#
690    with needs_wrapper = True
691
692 primop   FloatTanOp   "tanFloat#"      Monadic          
693    Float# -> Float#
694    with needs_wrapper = True
695
696 primop   FloatAsinOp   "asinFloat#"      Monadic          
697    Float# -> Float#
698    with needs_wrapper = True
699         can_fail = True
700
701 primop   FloatAcosOp   "acosFloat#"      Monadic          
702    Float# -> Float#
703    with needs_wrapper = True
704         can_fail = True
705
706 primop   FloatAtanOp   "atanFloat#"      Monadic          
707    Float# -> Float#
708    with needs_wrapper = True
709
710 primop   FloatSinhOp   "sinhFloat#"      Monadic          
711    Float# -> Float#
712    with needs_wrapper = True
713
714 primop   FloatCoshOp   "coshFloat#"      Monadic          
715    Float# -> Float#
716    with needs_wrapper = True
717
718 primop   FloatTanhOp   "tanhFloat#"      Monadic          
719    Float# -> Float#
720    with needs_wrapper = True
721
722 primop   FloatPowerOp   "powerFloat#"      Dyadic   
723    Float# -> Float# -> Float#
724    with needs_wrapper = True
725
726 primop   Float2DoubleOp   "float2Double#" GenPrimOp  Float# -> Double#
727
728 primop   FloatDecodeOp   "decodeFloat#" GenPrimOp
729    Float# -> (# Int#, Int#, ByteArray# #)
730    {Convert to arbitrary-precision integer.
731     First {\tt Int\#} in result is the exponent; second {\tt Int\#} and {\tt ByteArray\#}
732     represent an {\tt Integer\#} holding the mantissa.}
733    with out_of_line = True
734
735 primop   FloatDecode_IntOp   "decodeFloat_Int#" GenPrimOp
736    Float# -> (# Int#, Int# #)
737    {Convert to arbitrary-precision integer.
738     First {\tt Int\#} in result is the mantissa; second is the exponent.}
739    with out_of_line = True
740
741 ------------------------------------------------------------------------
742 section "Arrays"
743         {Operations on {\tt Array\#}.}
744 ------------------------------------------------------------------------
745
746 primtype Array# a
747
748 primtype MutableArray# s a
749
750 primop  NewArrayOp "newArray#" GenPrimOp
751    Int# -> a -> State# s -> (# State# s, MutableArray# s a #)
752    {Create a new mutable array of specified size (in bytes),
753     in the specified state thread,
754     with each element containing the specified initial value.}
755    with
756    out_of_line = True
757
758 primop  SameMutableArrayOp "sameMutableArray#" GenPrimOp
759    MutableArray# s a -> MutableArray# s a -> Bool
760
761 primop  ReadArrayOp "readArray#" GenPrimOp
762    MutableArray# s a -> Int# -> State# s -> (# State# s, a #)
763    {Read from specified index of mutable array. Result is not yet evaluated.}
764
765 primop  WriteArrayOp "writeArray#" GenPrimOp
766    MutableArray# s a -> Int# -> a -> State# s -> State# s
767    {Write to specified index of mutable array.}
768    with
769    has_side_effects = True
770
771 primop  IndexArrayOp "indexArray#" GenPrimOp
772    Array# a -> Int# -> (# a #)
773    {Read from specified index of immutable array. Result is packaged into
774     an unboxed singleton; the result itself is not yet evaluated.}
775
776 primop  UnsafeFreezeArrayOp "unsafeFreezeArray#" GenPrimOp
777    MutableArray# s a -> State# s -> (# State# s, Array# a #)
778    {Make a mutable array immutable, without copying.}
779    with
780    has_side_effects = True
781
782 primop  UnsafeThawArrayOp  "unsafeThawArray#" GenPrimOp
783    Array# a -> State# s -> (# State# s, MutableArray# s a #)
784    {Make an immutable array mutable, without copying.}
785    with
786    out_of_line = True
787
788 ------------------------------------------------------------------------
789 section "Byte Arrays"
790         {Operations on {\tt ByteArray\#}. A {\tt ByteArray\#} is a just a region of
791          raw memory in the garbage-collected heap, which is not scanned
792          for pointers. It carries its own size (in bytes). There are
793          three sets of operations for accessing byte array contents:
794          index for reading from immutable byte arrays, and read/write
795          for mutable byte arrays.  Each set contains operations for 
796          a range of useful primitive data types.  Each operation takes  
797          an offset measured in terms of the size fo the primitive type
798          being read or written.}
799
800 ------------------------------------------------------------------------
801
802 primtype ByteArray#
803
804 primtype MutableByteArray# s
805
806 primop  NewByteArrayOp_Char "newByteArray#" GenPrimOp
807    Int# -> State# s -> (# State# s, MutableByteArray# s #)
808    {Create a new mutable byte array of specified size (in bytes), in
809     the specified state thread.}
810    with out_of_line = True
811
812 primop  NewPinnedByteArrayOp_Char "newPinnedByteArray#" GenPrimOp
813    Int# -> State# s -> (# State# s, MutableByteArray# s #)
814    {Create a mutable byte array that the GC guarantees not to move.}
815    with out_of_line = True
816
817 primop  ByteArrayContents_Char "byteArrayContents#" GenPrimOp
818    ByteArray# -> Addr#
819    {Intended for use with pinned arrays; otherwise very unsafe!}
820
821 primop  SameMutableByteArrayOp "sameMutableByteArray#" GenPrimOp
822    MutableByteArray# s -> MutableByteArray# s -> Bool
823
824 primop  UnsafeFreezeByteArrayOp "unsafeFreezeByteArray#" GenPrimOp
825    MutableByteArray# s -> State# s -> (# State# s, ByteArray# #)
826    {Make a mutable byte array immutable, without copying.}
827    with
828    has_side_effects = True
829
830 primop  SizeofByteArrayOp "sizeofByteArray#" GenPrimOp  
831    ByteArray# -> Int#
832
833 primop  SizeofMutableByteArrayOp "sizeofMutableByteArray#" GenPrimOp
834    MutableByteArray# s -> Int#
835
836
837 primop IndexByteArrayOp_Char "indexCharArray#" GenPrimOp
838    ByteArray# -> Int# -> Char#
839    {Read 8-bit character; offset in bytes.}
840
841 primop IndexByteArrayOp_WideChar "indexWideCharArray#" GenPrimOp
842    ByteArray# -> Int# -> Char#
843    {Read 31-bit character; offset in 4-byte words.}
844
845 primop IndexByteArrayOp_Int "indexIntArray#" GenPrimOp
846    ByteArray# -> Int# -> Int#
847
848 primop IndexByteArrayOp_Word "indexWordArray#" GenPrimOp
849    ByteArray# -> Int# -> Word#
850
851 primop IndexByteArrayOp_Addr "indexAddrArray#" GenPrimOp
852    ByteArray# -> Int# -> Addr#
853
854 primop IndexByteArrayOp_Float "indexFloatArray#" GenPrimOp
855    ByteArray# -> Int# -> Float#
856
857 primop IndexByteArrayOp_Double "indexDoubleArray#" GenPrimOp
858    ByteArray# -> Int# -> Double#
859
860 primop IndexByteArrayOp_StablePtr "indexStablePtrArray#" GenPrimOp
861    ByteArray# -> Int# -> StablePtr# a
862
863 primop IndexByteArrayOp_Int8 "indexInt8Array#" GenPrimOp
864    ByteArray# -> Int# -> Int#
865
866 primop IndexByteArrayOp_Int16 "indexInt16Array#" GenPrimOp
867    ByteArray# -> Int# -> Int#
868
869 primop IndexByteArrayOp_Int32 "indexInt32Array#" GenPrimOp
870    ByteArray# -> Int# -> INT32
871
872 primop IndexByteArrayOp_Int64 "indexInt64Array#" GenPrimOp
873    ByteArray# -> Int# -> INT64
874
875 primop IndexByteArrayOp_Word8 "indexWord8Array#" GenPrimOp
876    ByteArray# -> Int# -> Word#
877
878 primop IndexByteArrayOp_Word16 "indexWord16Array#" GenPrimOp
879    ByteArray# -> Int# -> Word#
880
881 primop IndexByteArrayOp_Word32 "indexWord32Array#" GenPrimOp
882    ByteArray# -> Int# -> WORD32
883
884 primop IndexByteArrayOp_Word64 "indexWord64Array#" GenPrimOp
885    ByteArray# -> Int# -> WORD64
886
887 primop  ReadByteArrayOp_Char "readCharArray#" GenPrimOp
888    MutableByteArray# s -> Int# -> State# s -> (# State# s, Char# #)
889    {Read 8-bit character; offset in bytes.}
890
891 primop  ReadByteArrayOp_WideChar "readWideCharArray#" GenPrimOp
892    MutableByteArray# s -> Int# -> State# s -> (# State# s, Char# #)
893    {Read 31-bit character; offset in 4-byte words.}
894
895 primop  ReadByteArrayOp_Int "readIntArray#" GenPrimOp
896    MutableByteArray# s -> Int# -> State# s -> (# State# s, Int# #)
897
898 primop  ReadByteArrayOp_Word "readWordArray#" GenPrimOp
899    MutableByteArray# s -> Int# -> State# s -> (# State# s, Word# #)
900
901 primop  ReadByteArrayOp_Addr "readAddrArray#" GenPrimOp
902    MutableByteArray# s -> Int# -> State# s -> (# State# s, Addr# #)
903
904 primop  ReadByteArrayOp_Float "readFloatArray#" GenPrimOp
905    MutableByteArray# s -> Int# -> State# s -> (# State# s, Float# #)
906
907 primop  ReadByteArrayOp_Double "readDoubleArray#" GenPrimOp
908    MutableByteArray# s -> Int# -> State# s -> (# State# s, Double# #)
909
910 primop  ReadByteArrayOp_StablePtr "readStablePtrArray#" GenPrimOp
911    MutableByteArray# s -> Int# -> State# s -> (# State# s, StablePtr# a #)
912
913 primop  ReadByteArrayOp_Int8 "readInt8Array#" GenPrimOp
914    MutableByteArray# s -> Int# -> State# s -> (# State# s, Int# #)
915
916 primop  ReadByteArrayOp_Int16 "readInt16Array#" GenPrimOp
917    MutableByteArray# s -> Int# -> State# s -> (# State# s, Int# #)
918
919 primop  ReadByteArrayOp_Int32 "readInt32Array#" GenPrimOp
920    MutableByteArray# s -> Int# -> State# s -> (# State# s, INT32 #)
921
922 primop  ReadByteArrayOp_Int64 "readInt64Array#" GenPrimOp
923    MutableByteArray# s -> Int# -> State# s -> (# State# s, INT64 #)
924
925 primop  ReadByteArrayOp_Word8 "readWord8Array#" GenPrimOp
926    MutableByteArray# s -> Int# -> State# s -> (# State# s, Word# #)
927
928 primop  ReadByteArrayOp_Word16 "readWord16Array#" GenPrimOp
929    MutableByteArray# s -> Int# -> State# s -> (# State# s, Word# #)
930
931 primop  ReadByteArrayOp_Word32 "readWord32Array#" GenPrimOp
932    MutableByteArray# s -> Int# -> State# s -> (# State# s, WORD32 #)
933
934 primop  ReadByteArrayOp_Word64 "readWord64Array#" GenPrimOp
935    MutableByteArray# s -> Int# -> State# s -> (# State# s, WORD64 #)
936
937 primop  WriteByteArrayOp_Char "writeCharArray#" GenPrimOp
938    MutableByteArray# s -> Int# -> Char# -> State# s -> State# s
939    {Write 8-bit character; offset in bytes.}
940    with has_side_effects = True
941
942 primop  WriteByteArrayOp_WideChar "writeWideCharArray#" GenPrimOp
943    MutableByteArray# s -> Int# -> Char# -> State# s -> State# s
944    {Write 31-bit character; offset in 4-byte words.}
945    with has_side_effects = True
946
947 primop  WriteByteArrayOp_Int "writeIntArray#" GenPrimOp
948    MutableByteArray# s -> Int# -> Int# -> State# s -> State# s
949    with has_side_effects = True
950
951 primop  WriteByteArrayOp_Word "writeWordArray#" GenPrimOp
952    MutableByteArray# s -> Int# -> Word# -> State# s -> State# s
953    with has_side_effects = True
954
955 primop  WriteByteArrayOp_Addr "writeAddrArray#" GenPrimOp
956    MutableByteArray# s -> Int# -> Addr# -> State# s -> State# s
957    with has_side_effects = True
958
959 primop  WriteByteArrayOp_Float "writeFloatArray#" GenPrimOp
960    MutableByteArray# s -> Int# -> Float# -> State# s -> State# s
961    with has_side_effects = True
962
963 primop  WriteByteArrayOp_Double "writeDoubleArray#" GenPrimOp
964    MutableByteArray# s -> Int# -> Double# -> State# s -> State# s
965    with has_side_effects = True
966
967 primop  WriteByteArrayOp_StablePtr "writeStablePtrArray#" GenPrimOp
968    MutableByteArray# s -> Int# -> StablePtr# a -> State# s -> State# s
969    with has_side_effects = True
970
971 primop  WriteByteArrayOp_Int8 "writeInt8Array#" GenPrimOp
972    MutableByteArray# s -> Int# -> Int# -> State# s -> State# s
973    with has_side_effects = True
974
975 primop  WriteByteArrayOp_Int16 "writeInt16Array#" GenPrimOp
976    MutableByteArray# s -> Int# -> Int# -> State# s -> State# s
977    with has_side_effects = True
978
979 primop  WriteByteArrayOp_Int32 "writeInt32Array#" GenPrimOp
980    MutableByteArray# s -> Int# -> INT32 -> State# s -> State# s
981    with has_side_effects = True
982
983 primop  WriteByteArrayOp_Int64 "writeInt64Array#" GenPrimOp
984    MutableByteArray# s -> Int# -> INT64 -> State# s -> State# s
985    with has_side_effects = True
986
987 primop  WriteByteArrayOp_Word8 "writeWord8Array#" GenPrimOp
988    MutableByteArray# s -> Int# -> Word# -> State# s -> State# s
989    with has_side_effects = True
990
991 primop  WriteByteArrayOp_Word16 "writeWord16Array#" GenPrimOp
992    MutableByteArray# s -> Int# -> Word# -> State# s -> State# s
993    with has_side_effects = True
994
995 primop  WriteByteArrayOp_Word32 "writeWord32Array#" GenPrimOp
996    MutableByteArray# s -> Int# -> WORD32 -> State# s -> State# s
997    with has_side_effects = True
998
999 primop  WriteByteArrayOp_Word64 "writeWord64Array#" GenPrimOp
1000    MutableByteArray# s -> Int# -> WORD64 -> State# s -> State# s
1001    with has_side_effects = True
1002
1003 ------------------------------------------------------------------------
1004 section "Addr#"
1005 ------------------------------------------------------------------------
1006
1007 primtype Addr#
1008         { An arbitrary machine address assumed to point outside
1009          the garbage-collected heap. }
1010
1011 pseudoop "nullAddr#" Addr#
1012         { The null address. }
1013
1014 primop   AddrAddOp "plusAddr#" GenPrimOp Addr# -> Int# -> Addr#
1015 primop   AddrSubOp "minusAddr#" GenPrimOp Addr# -> Addr# -> Int#
1016          {Result is meaningless if two {\tt Addr\#}s are so far apart that their
1017          difference doesn't fit in an {\tt Int\#}.}
1018 primop   AddrRemOp "remAddr#" GenPrimOp Addr# -> Int# -> Int#
1019          {Return the remainder when the {\tt Addr\#} arg, treated like an {\tt Int\#},
1020           is divided by the {\tt Int\#} arg.}
1021 #if (WORD_SIZE_IN_BITS == 32 || WORD_SIZE_IN_BITS == 64)
1022 primop   Addr2IntOp  "addr2Int#"     GenPrimOp   Addr# -> Int#
1023         {Coerce directly from address to int. Strongly deprecated.}
1024 primop   Int2AddrOp   "int2Addr#"    GenPrimOp  Int# -> Addr#
1025         {Coerce directly from int to address. Strongly deprecated.}
1026 #endif
1027
1028 primop   AddrGtOp  "gtAddr#"   Compare   Addr# -> Addr# -> Bool
1029 primop   AddrGeOp  "geAddr#"   Compare   Addr# -> Addr# -> Bool
1030 primop   AddrEqOp  "eqAddr#"   Compare   Addr# -> Addr# -> Bool
1031 primop   AddrNeOp  "neAddr#"   Compare   Addr# -> Addr# -> Bool
1032 primop   AddrLtOp  "ltAddr#"   Compare   Addr# -> Addr# -> Bool
1033 primop   AddrLeOp  "leAddr#"   Compare   Addr# -> Addr# -> Bool
1034
1035 primop IndexOffAddrOp_Char "indexCharOffAddr#" GenPrimOp
1036    Addr# -> Int# -> Char#
1037    {Reads 8-bit character; offset in bytes.}
1038
1039 primop IndexOffAddrOp_WideChar "indexWideCharOffAddr#" GenPrimOp
1040    Addr# -> Int# -> Char#
1041    {Reads 31-bit character; offset in 4-byte words.}
1042
1043 primop IndexOffAddrOp_Int "indexIntOffAddr#" GenPrimOp
1044    Addr# -> Int# -> Int#
1045
1046 primop IndexOffAddrOp_Word "indexWordOffAddr#" GenPrimOp
1047    Addr# -> Int# -> Word#
1048
1049 primop IndexOffAddrOp_Addr "indexAddrOffAddr#" GenPrimOp
1050    Addr# -> Int# -> Addr#
1051
1052 primop IndexOffAddrOp_Float "indexFloatOffAddr#" GenPrimOp
1053    Addr# -> Int# -> Float#
1054
1055 primop IndexOffAddrOp_Double "indexDoubleOffAddr#" GenPrimOp
1056    Addr# -> Int# -> Double#
1057
1058 primop IndexOffAddrOp_StablePtr "indexStablePtrOffAddr#" GenPrimOp
1059    Addr# -> Int# -> StablePtr# a
1060
1061 primop IndexOffAddrOp_Int8 "indexInt8OffAddr#" GenPrimOp
1062    Addr# -> Int# -> Int#
1063
1064 primop IndexOffAddrOp_Int16 "indexInt16OffAddr#" GenPrimOp
1065    Addr# -> Int# -> Int#
1066
1067 primop IndexOffAddrOp_Int32 "indexInt32OffAddr#" GenPrimOp
1068    Addr# -> Int# -> INT32
1069
1070 primop IndexOffAddrOp_Int64 "indexInt64OffAddr#" GenPrimOp
1071    Addr# -> Int# -> INT64
1072
1073 primop IndexOffAddrOp_Word8 "indexWord8OffAddr#" GenPrimOp
1074    Addr# -> Int# -> Word#
1075
1076 primop IndexOffAddrOp_Word16 "indexWord16OffAddr#" GenPrimOp
1077    Addr# -> Int# -> Word#
1078
1079 primop IndexOffAddrOp_Word32 "indexWord32OffAddr#" GenPrimOp
1080    Addr# -> Int# -> WORD32
1081
1082 primop IndexOffAddrOp_Word64 "indexWord64OffAddr#" GenPrimOp
1083    Addr# -> Int# -> WORD64
1084
1085 primop ReadOffAddrOp_Char "readCharOffAddr#" GenPrimOp
1086    Addr# -> Int# -> State# s -> (# State# s, Char# #)
1087    {Reads 8-bit character; offset in bytes.}
1088
1089 primop ReadOffAddrOp_WideChar "readWideCharOffAddr#" GenPrimOp
1090    Addr# -> Int# -> State# s -> (# State# s, Char# #)
1091    {Reads 31-bit character; offset in 4-byte words.}
1092
1093 primop ReadOffAddrOp_Int "readIntOffAddr#" GenPrimOp
1094    Addr# -> Int# -> State# s -> (# State# s, Int# #)
1095
1096 primop ReadOffAddrOp_Word "readWordOffAddr#" GenPrimOp
1097    Addr# -> Int# -> State# s -> (# State# s, Word# #)
1098
1099 primop ReadOffAddrOp_Addr "readAddrOffAddr#" GenPrimOp
1100    Addr# -> Int# -> State# s -> (# State# s, Addr# #)
1101
1102 primop ReadOffAddrOp_Float "readFloatOffAddr#" GenPrimOp
1103    Addr# -> Int# -> State# s -> (# State# s, Float# #)
1104
1105 primop ReadOffAddrOp_Double "readDoubleOffAddr#" GenPrimOp
1106    Addr# -> Int# -> State# s -> (# State# s, Double# #)
1107
1108 primop ReadOffAddrOp_StablePtr "readStablePtrOffAddr#" GenPrimOp
1109    Addr# -> Int# -> State# s -> (# State# s, StablePtr# a #)
1110
1111 primop ReadOffAddrOp_Int8 "readInt8OffAddr#" GenPrimOp
1112    Addr# -> Int# -> State# s -> (# State# s, Int# #)
1113
1114 primop ReadOffAddrOp_Int16 "readInt16OffAddr#" GenPrimOp
1115    Addr# -> Int# -> State# s -> (# State# s, Int# #)
1116
1117 primop ReadOffAddrOp_Int32 "readInt32OffAddr#" GenPrimOp
1118    Addr# -> Int# -> State# s -> (# State# s, INT32 #)
1119
1120 primop ReadOffAddrOp_Int64 "readInt64OffAddr#" GenPrimOp
1121    Addr# -> Int# -> State# s -> (# State# s, INT64 #)
1122
1123 primop ReadOffAddrOp_Word8 "readWord8OffAddr#" GenPrimOp
1124    Addr# -> Int# -> State# s -> (# State# s, Word# #)
1125
1126 primop ReadOffAddrOp_Word16 "readWord16OffAddr#" GenPrimOp
1127    Addr# -> Int# -> State# s -> (# State# s, Word# #)
1128
1129 primop ReadOffAddrOp_Word32 "readWord32OffAddr#" GenPrimOp
1130    Addr# -> Int# -> State# s -> (# State# s, WORD32 #)
1131
1132 primop ReadOffAddrOp_Word64 "readWord64OffAddr#" GenPrimOp
1133    Addr# -> Int# -> State# s -> (# State# s, WORD64 #)
1134
1135
1136 primop  WriteOffAddrOp_Char "writeCharOffAddr#" GenPrimOp
1137    Addr# -> Int# -> Char# -> State# s -> State# s
1138    with has_side_effects = True
1139
1140 primop  WriteOffAddrOp_WideChar "writeWideCharOffAddr#" GenPrimOp
1141    Addr# -> Int# -> Char# -> State# s -> State# s
1142    with has_side_effects = True
1143
1144 primop  WriteOffAddrOp_Int "writeIntOffAddr#" GenPrimOp
1145    Addr# -> Int# -> Int# -> State# s -> State# s
1146    with has_side_effects = True
1147
1148 primop  WriteOffAddrOp_Word "writeWordOffAddr#" GenPrimOp
1149    Addr# -> Int# -> Word# -> State# s -> State# s
1150    with has_side_effects = True
1151
1152 primop  WriteOffAddrOp_Addr "writeAddrOffAddr#" GenPrimOp
1153    Addr# -> Int# -> Addr# -> State# s -> State# s
1154    with has_side_effects = True
1155
1156 primop  WriteOffAddrOp_Float "writeFloatOffAddr#" GenPrimOp
1157    Addr# -> Int# -> Float# -> State# s -> State# s
1158    with has_side_effects = True
1159
1160 primop  WriteOffAddrOp_Double "writeDoubleOffAddr#" GenPrimOp
1161    Addr# -> Int# -> Double# -> State# s -> State# s
1162    with has_side_effects = True
1163
1164 primop  WriteOffAddrOp_StablePtr "writeStablePtrOffAddr#" GenPrimOp
1165    Addr# -> Int# -> StablePtr# a -> State# s -> State# s
1166    with has_side_effects = True
1167
1168 primop  WriteOffAddrOp_Int8 "writeInt8OffAddr#" GenPrimOp
1169    Addr# -> Int# -> Int# -> State# s -> State# s
1170    with has_side_effects = True
1171
1172 primop  WriteOffAddrOp_Int16 "writeInt16OffAddr#" GenPrimOp
1173    Addr# -> Int# -> Int# -> State# s -> State# s
1174    with has_side_effects = True
1175
1176 primop  WriteOffAddrOp_Int32 "writeInt32OffAddr#" GenPrimOp
1177    Addr# -> Int# -> INT32 -> State# s -> State# s
1178    with has_side_effects = True
1179
1180 primop  WriteOffAddrOp_Int64 "writeInt64OffAddr#" GenPrimOp
1181    Addr# -> Int# -> INT64 -> State# s -> State# s
1182    with has_side_effects = True
1183
1184 primop  WriteOffAddrOp_Word8 "writeWord8OffAddr#" GenPrimOp
1185    Addr# -> Int# -> Word# -> State# s -> State# s
1186    with has_side_effects = True
1187
1188 primop  WriteOffAddrOp_Word16 "writeWord16OffAddr#" GenPrimOp
1189    Addr# -> Int# -> Word# -> State# s -> State# s
1190    with has_side_effects = True
1191
1192 primop  WriteOffAddrOp_Word32 "writeWord32OffAddr#" GenPrimOp
1193    Addr# -> Int# -> WORD32 -> State# s -> State# s
1194    with has_side_effects = True
1195
1196 primop  WriteOffAddrOp_Word64 "writeWord64OffAddr#" GenPrimOp
1197    Addr# -> Int# -> WORD64 -> State# s -> State# s
1198    with has_side_effects = True
1199
1200 ------------------------------------------------------------------------
1201 section "Mutable variables"
1202         {Operations on MutVar\#s.}
1203 ------------------------------------------------------------------------
1204
1205 primtype MutVar# s a
1206         {A {\tt MutVar\#} behaves like a single-element mutable array.}
1207
1208 primop  NewMutVarOp "newMutVar#" GenPrimOp
1209    a -> State# s -> (# State# s, MutVar# s a #)
1210    {Create {\tt MutVar\#} with specified initial value in specified state thread.}
1211    with
1212    out_of_line = True
1213
1214 primop  ReadMutVarOp "readMutVar#" GenPrimOp
1215    MutVar# s a -> State# s -> (# State# s, a #)
1216    {Read contents of {\tt MutVar\#}. Result is not yet evaluated.}
1217
1218 primop  WriteMutVarOp "writeMutVar#"  GenPrimOp
1219    MutVar# s a -> a -> State# s -> State# s
1220    {Write contents of {\tt MutVar\#}.}
1221    with
1222    has_side_effects = True
1223
1224 primop  SameMutVarOp "sameMutVar#" GenPrimOp
1225    MutVar# s a -> MutVar# s a -> Bool
1226
1227 -- not really the right type, but we don't know about pairs here.  The
1228 -- correct type is
1229 --
1230 --   MutVar# s a -> (a -> (a,b)) -> State# s -> (# State# s, b #)
1231 --
1232 primop  AtomicModifyMutVarOp "atomicModifyMutVar#" GenPrimOp
1233    MutVar# s a -> (a -> b) -> State# s -> (# State# s, c #)
1234    with
1235    has_side_effects = True
1236    out_of_line = True
1237
1238 ------------------------------------------------------------------------
1239 section "Exceptions"
1240 ------------------------------------------------------------------------
1241
1242 primop  CatchOp "catch#" GenPrimOp
1243           (State# RealWorld -> (# State# RealWorld, a #) )
1244        -> (b -> State# RealWorld -> (# State# RealWorld, a #) ) 
1245        -> State# RealWorld
1246        -> (# State# RealWorld, a #)
1247    with
1248         -- Catch is actually strict in its first argument
1249         -- but we don't want to tell the strictness
1250         -- analyser about that!
1251         -- might use caught action multiply
1252    out_of_line = True
1253
1254 primop  RaiseOp "raise#" GenPrimOp
1255    a -> b
1256    with
1257    strictness  = { \ _arity -> mkStrictSig (mkTopDmdType [lazyDmd] BotRes) }
1258       -- NB: result is bottom
1259    out_of_line = True
1260
1261 -- raiseIO# needs to be a primop, because exceptions in the IO monad
1262 -- must be *precise* - we don't want the strictness analyser turning
1263 -- one kind of bottom into another, as it is allowed to do in pure code.
1264
1265 primop  RaiseIOOp "raiseIO#" GenPrimOp
1266    a -> State# RealWorld -> (# State# RealWorld, b #)
1267    with
1268    out_of_line = True
1269
1270 primop  BlockAsyncExceptionsOp "blockAsyncExceptions#" GenPrimOp
1271         (State# RealWorld -> (# State# RealWorld, a #))
1272      -> (State# RealWorld -> (# State# RealWorld, a #))
1273    with
1274    out_of_line = True
1275
1276 primop  UnblockAsyncExceptionsOp "unblockAsyncExceptions#" GenPrimOp
1277         (State# RealWorld -> (# State# RealWorld, a #))
1278      -> (State# RealWorld -> (# State# RealWorld, a #))
1279    with
1280    out_of_line = True
1281
1282 ------------------------------------------------------------------------
1283 section "STM-accessible Mutable Variables"
1284 ------------------------------------------------------------------------
1285
1286 primtype TVar# s a
1287
1288 primop  AtomicallyOp "atomically#" GenPrimOp
1289       (State# RealWorld -> (# State# RealWorld, a #) )
1290    -> State# RealWorld -> (# State# RealWorld, a #)
1291    with
1292    out_of_line = True
1293    has_side_effects = True
1294
1295 primop  RetryOp "retry#" GenPrimOp
1296    State# RealWorld -> (# State# RealWorld, a #)
1297    with 
1298    out_of_line = True
1299    has_side_effects = True
1300
1301 primop  CatchRetryOp "catchRetry#" GenPrimOp
1302       (State# RealWorld -> (# State# RealWorld, a #) )
1303    -> (State# RealWorld -> (# State# RealWorld, a #) )
1304    -> (State# RealWorld -> (# State# RealWorld, a #) )
1305    with 
1306    out_of_line = True
1307    has_side_effects = True
1308
1309 primop  CatchSTMOp "catchSTM#" GenPrimOp
1310       (State# RealWorld -> (# State# RealWorld, a #) )
1311    -> (b -> State# RealWorld -> (# State# RealWorld, a #) )
1312    -> (State# RealWorld -> (# State# RealWorld, a #) )
1313    with 
1314    out_of_line = True
1315    has_side_effects = True
1316
1317 primop  Check "check#" GenPrimOp
1318       (State# RealWorld -> (# State# RealWorld, a #) )
1319    -> (State# RealWorld -> (# State# RealWorld, () #) )
1320    with 
1321    out_of_line = True
1322    has_side_effects = True
1323
1324 primop  NewTVarOp "newTVar#" GenPrimOp
1325        a
1326     -> State# s -> (# State# s, TVar# s a #)
1327    {Create a new {\tt TVar\#} holding a specified initial value.}
1328    with
1329    out_of_line  = True
1330
1331 primop  ReadTVarOp "readTVar#" GenPrimOp
1332        TVar# s a
1333     -> State# s -> (# State# s, a #)
1334    {Read contents of {\tt TVar\#}.  Result is not yet evaluated.}
1335    with
1336    out_of_line  = True
1337
1338 primop  WriteTVarOp "writeTVar#" GenPrimOp
1339        TVar# s a
1340     -> a
1341     -> State# s -> State# s
1342    {Write contents of {\tt TVar\#}.}
1343    with
1344    out_of_line      = True
1345    has_side_effects = True
1346
1347 primop  SameTVarOp "sameTVar#" GenPrimOp
1348    TVar# s a -> TVar# s a -> Bool
1349
1350
1351 ------------------------------------------------------------------------
1352 section "Synchronized Mutable Variables"
1353         {Operations on {\tt MVar\#}s. }
1354 ------------------------------------------------------------------------
1355
1356 primtype MVar# s a
1357         { A shared mutable variable ({\it not} the same as a {\tt MutVar\#}!).
1358         (Note: in a non-concurrent implementation, {\tt (MVar\# a)} can be
1359         represented by {\tt (MutVar\# (Maybe a))}.) }
1360
1361 primop  NewMVarOp "newMVar#"  GenPrimOp
1362    State# s -> (# State# s, MVar# s a #)
1363    {Create new {\tt MVar\#}; initially empty.}
1364    with
1365    out_of_line = True
1366
1367 primop  TakeMVarOp "takeMVar#" GenPrimOp
1368    MVar# s a -> State# s -> (# State# s, a #)
1369    {If {\tt MVar\#} is empty, block until it becomes full.
1370    Then remove and return its contents, and set it empty.}
1371    with
1372    has_side_effects = True
1373    out_of_line      = True
1374
1375 primop  TryTakeMVarOp "tryTakeMVar#" GenPrimOp
1376    MVar# s a -> State# s -> (# State# s, Int#, a #)
1377    {If {\tt MVar\#} is empty, immediately return with integer 0 and value undefined.
1378    Otherwise, return with integer 1 and contents of {\tt MVar\#}, and set {\tt MVar\#} empty.}
1379    with
1380    has_side_effects = True
1381    out_of_line      = True
1382
1383 primop  PutMVarOp "putMVar#" GenPrimOp
1384    MVar# s a -> a -> State# s -> State# s
1385    {If {\tt MVar\#} is full, block until it becomes empty.
1386    Then store value arg as its new contents.}
1387    with
1388    has_side_effects = True
1389    out_of_line      = True
1390
1391 primop  TryPutMVarOp "tryPutMVar#" GenPrimOp
1392    MVar# s a -> a -> State# s -> (# State# s, Int# #)
1393    {If {\tt MVar\#} is full, immediately return with integer 0.
1394     Otherwise, store value arg as {\tt MVar\#}'s new contents, and return with integer 1.}
1395    with
1396    has_side_effects = True
1397    out_of_line      = True
1398
1399 primop  SameMVarOp "sameMVar#" GenPrimOp
1400    MVar# s a -> MVar# s a -> Bool
1401
1402 primop  IsEmptyMVarOp "isEmptyMVar#" GenPrimOp
1403    MVar# s a -> State# s -> (# State# s, Int# #)
1404    {Return 1 if {\tt MVar\#} is empty; 0 otherwise.}
1405    with
1406    out_of_line = True
1407
1408 ------------------------------------------------------------------------
1409 section "Delay/wait operations"
1410 ------------------------------------------------------------------------
1411
1412 primop  DelayOp "delay#" GenPrimOp
1413    Int# -> State# s -> State# s
1414    {Sleep specified number of microseconds.}
1415    with
1416    needs_wrapper    = True
1417    has_side_effects = True
1418    out_of_line      = True
1419
1420 primop  WaitReadOp "waitRead#" GenPrimOp
1421    Int# -> State# s -> State# s
1422    {Block until input is available on specified file descriptor.}
1423    with
1424    needs_wrapper    = True
1425    has_side_effects = True
1426    out_of_line      = True
1427
1428 primop  WaitWriteOp "waitWrite#" GenPrimOp
1429    Int# -> State# s -> State# s
1430    {Block until output is possible on specified file descriptor.}
1431    with
1432    needs_wrapper    = True
1433    has_side_effects = True
1434    out_of_line      = True
1435
1436 #ifdef mingw32_TARGET_OS
1437 primop  AsyncReadOp "asyncRead#" GenPrimOp
1438    Int# -> Int# -> Int# -> Addr# -> State# RealWorld-> (# State# RealWorld, Int#, Int# #)
1439    {Asynchronously read bytes from specified file descriptor.}
1440    with
1441    needs_wrapper    = True
1442    has_side_effects = True
1443    out_of_line      = True
1444
1445 primop  AsyncWriteOp "asyncWrite#" GenPrimOp
1446    Int# -> Int# -> Int# -> Addr# -> State# RealWorld-> (# State# RealWorld, Int#, Int# #)
1447    {Asynchronously write bytes from specified file descriptor.}
1448    with
1449    needs_wrapper    = True
1450    has_side_effects = True
1451    out_of_line      = True
1452
1453 primop  AsyncDoProcOp "asyncDoProc#" GenPrimOp
1454    Addr# -> Addr# -> State# RealWorld-> (# State# RealWorld, Int#, Int# #)
1455    {Asynchronously perform procedure (first arg), passing it 2nd arg.}
1456    with
1457    needs_wrapper    = True
1458    has_side_effects = True
1459    out_of_line      = True
1460
1461 #endif
1462
1463 ------------------------------------------------------------------------
1464 section "Concurrency primitives"
1465 ------------------------------------------------------------------------
1466
1467 primtype State# s
1468         { {\tt State\#} is the primitive, unlifted type of states.  It has
1469         one type parameter, thus {\tt State\# RealWorld}, or {\tt State\# s},
1470         where s is a type variable. The only purpose of the type parameter
1471         is to keep different state threads separate.  It is represented by
1472         nothing at all. }
1473
1474 primtype RealWorld
1475         { {\tt RealWorld} is deeply magical.  It is {\it primitive}, but it is not
1476         {\it unlifted} (hence {\tt ptrArg}).  We never manipulate values of type
1477         {\tt RealWorld}; it's only used in the type system, to parameterise {\tt State\#}. }
1478
1479 primtype ThreadId#
1480         {(In a non-concurrent implementation, this can be a singleton
1481         type, whose (unique) value is returned by {\tt myThreadId\#}.  The 
1482         other operations can be omitted.)}
1483
1484 primop  ForkOp "fork#" GenPrimOp
1485    a -> State# RealWorld -> (# State# RealWorld, ThreadId# #)
1486    with
1487    has_side_effects = True
1488    out_of_line      = True
1489
1490 primop  ForkOnOp "forkOn#" GenPrimOp
1491    Int# -> a -> State# RealWorld -> (# State# RealWorld, ThreadId# #)
1492    with
1493    has_side_effects = True
1494    out_of_line      = True
1495
1496 primop  KillThreadOp "killThread#"  GenPrimOp
1497    ThreadId# -> a -> State# RealWorld -> State# RealWorld
1498    with
1499    has_side_effects = True
1500    out_of_line      = True
1501
1502 primop  YieldOp "yield#" GenPrimOp
1503    State# RealWorld -> State# RealWorld
1504    with
1505    has_side_effects = True
1506    out_of_line      = True
1507
1508 primop  MyThreadIdOp "myThreadId#" GenPrimOp
1509    State# RealWorld -> (# State# RealWorld, ThreadId# #)
1510    with
1511    out_of_line = True
1512
1513 primop LabelThreadOp "labelThread#" GenPrimOp
1514    ThreadId# -> Addr# -> State# RealWorld -> State# RealWorld
1515    with
1516    has_side_effects = True
1517    out_of_line      = True
1518    
1519 primop  IsCurrentThreadBoundOp "isCurrentThreadBound#" GenPrimOp
1520    State# RealWorld -> (# State# RealWorld, Int# #)
1521    with
1522    out_of_line = True
1523
1524 primop  NoDuplicateOp "noDuplicate#" GenPrimOp
1525    State# RealWorld -> State# RealWorld
1526    with
1527    out_of_line = True
1528
1529 ------------------------------------------------------------------------
1530 section "Weak pointers"
1531 ------------------------------------------------------------------------
1532
1533 primtype Weak# b
1534
1535 -- note that tyvar "o" denotes openAlphaTyVar
1536
1537 primop  MkWeakOp "mkWeak#" GenPrimOp
1538    o -> b -> c -> State# RealWorld -> (# State# RealWorld, Weak# b #)
1539    with
1540    has_side_effects = True
1541    out_of_line      = True
1542
1543 primop  DeRefWeakOp "deRefWeak#" GenPrimOp
1544    Weak# a -> State# RealWorld -> (# State# RealWorld, Int#, a #)
1545    with
1546    has_side_effects = True
1547    out_of_line      = True
1548
1549 primop  FinalizeWeakOp "finalizeWeak#" GenPrimOp
1550    Weak# a -> State# RealWorld -> (# State# RealWorld, Int#, 
1551               (State# RealWorld -> (# State# RealWorld, () #)) #)
1552    with
1553    has_side_effects = True
1554    out_of_line      = True
1555
1556 primop TouchOp "touch#" GenPrimOp
1557    o -> State# RealWorld -> State# RealWorld
1558    with
1559    has_side_effects = True
1560
1561 ------------------------------------------------------------------------
1562 section "Stable pointers and names"
1563 ------------------------------------------------------------------------
1564
1565 primtype StablePtr# a
1566
1567 primtype StableName# a
1568
1569 primop  MakeStablePtrOp "makeStablePtr#" GenPrimOp
1570    a -> State# RealWorld -> (# State# RealWorld, StablePtr# a #)
1571    with
1572    has_side_effects = True
1573    out_of_line      = True
1574
1575 primop  DeRefStablePtrOp "deRefStablePtr#" GenPrimOp
1576    StablePtr# a -> State# RealWorld -> (# State# RealWorld, a #)
1577    with
1578    needs_wrapper    = True
1579    has_side_effects = True
1580    out_of_line      = True
1581
1582 primop  EqStablePtrOp "eqStablePtr#" GenPrimOp
1583    StablePtr# a -> StablePtr# a -> Int#
1584    with
1585    has_side_effects = True
1586
1587 primop  MakeStableNameOp "makeStableName#" GenPrimOp
1588    a -> State# RealWorld -> (# State# RealWorld, StableName# a #)
1589    with
1590    needs_wrapper    = True
1591    has_side_effects = True
1592    out_of_line      = True
1593
1594 primop  EqStableNameOp "eqStableName#" GenPrimOp
1595    StableName# a -> StableName# a -> Int#
1596
1597 primop  StableNameToIntOp "stableNameToInt#" GenPrimOp
1598    StableName# a -> Int#
1599
1600 ------------------------------------------------------------------------
1601 section "Unsafe pointer equality"
1602 --  (#1 Bad Guy: Alistair Reid :)   
1603 ------------------------------------------------------------------------
1604
1605 primop  ReallyUnsafePtrEqualityOp "reallyUnsafePtrEquality#" GenPrimOp
1606    a -> a -> Int#
1607
1608 ------------------------------------------------------------------------
1609 section "Parallelism"
1610 ------------------------------------------------------------------------
1611
1612 primop  ParOp "par#" GenPrimOp
1613    a -> Int#
1614    with
1615       -- Note that Par is lazy to avoid that the sparked thing
1616       -- gets evaluted strictly, which it should *not* be
1617    has_side_effects = True
1618
1619 -- HWL: The first 4 Int# in all par... annotations denote:
1620 --   name, granularity info, size of result, degree of parallelism
1621 --      Same  structure as _seq_ i.e. returns Int#
1622 -- KSW: v, the second arg in parAt# and parAtForNow#, is used only to determine
1623 --   `the processor containing the expression v'; it is not evaluated
1624
1625 primop  ParGlobalOp  "parGlobal#"  GenPrimOp
1626    a -> Int# -> Int# -> Int# -> Int# -> b -> Int#
1627    with
1628    has_side_effects = True
1629
1630 primop  ParLocalOp  "parLocal#"  GenPrimOp
1631    a -> Int# -> Int# -> Int# -> Int# -> b -> Int#
1632    with
1633    has_side_effects = True
1634
1635 primop  ParAtOp  "parAt#"  GenPrimOp
1636    b -> a -> Int# -> Int# -> Int# -> Int# -> c -> Int#
1637    with
1638    has_side_effects = True
1639
1640 primop  ParAtAbsOp  "parAtAbs#"  GenPrimOp
1641    a -> Int# -> Int# -> Int# -> Int# -> Int# -> b -> Int#
1642    with
1643    has_side_effects = True
1644
1645 primop  ParAtRelOp  "parAtRel#" GenPrimOp
1646    a -> Int# -> Int# -> Int# -> Int# -> Int# -> b -> Int#
1647    with
1648    has_side_effects = True
1649
1650 primop  ParAtForNowOp  "parAtForNow#" GenPrimOp
1651    b -> a -> Int# -> Int# -> Int# -> Int# -> c -> Int#
1652    with
1653    has_side_effects = True
1654
1655 -- copyable# and noFollow# are yet to be implemented (for GpH)
1656 --
1657 --primop  CopyableOp  "copyable#" GenPrimOp
1658 --   a -> Int#
1659 --   with
1660 --   has_side_effects = True
1661 --
1662 --primop  NoFollowOp "noFollow#" GenPrimOp
1663 --   a -> Int#
1664 --   with
1665 --   has_side_effects = True
1666
1667
1668 ------------------------------------------------------------------------
1669 section "Tag to enum stuff"
1670         {Convert back and forth between values of enumerated types
1671         and small integers.}
1672 ------------------------------------------------------------------------
1673
1674 primop  DataToTagOp "dataToTag#" GenPrimOp
1675    a -> Int#
1676    with
1677    strictness  = { \ _arity -> mkStrictSig (mkTopDmdType [seqDmd] TopRes) }
1678         -- dataToTag# must have an evaluated argument
1679
1680 primop  TagToEnumOp "tagToEnum#" GenPrimOp     
1681    Int# -> a
1682
1683 ------------------------------------------------------------------------
1684 section "Bytecode operations" 
1685         {Support for the bytecode interpreter and linker.}
1686 ------------------------------------------------------------------------
1687
1688 primtype BCO#
1689    {Primitive bytecode type.}
1690
1691 primop   AddrToHValueOp "addrToHValue#" GenPrimOp
1692    Addr# -> (# a #)
1693    {Convert an {\tt Addr\#} to a followable type.}
1694
1695 primop   MkApUpd0_Op "mkApUpd0#" GenPrimOp
1696    BCO# -> (# a #)
1697    with
1698    out_of_line = True
1699
1700 primop  NewBCOOp "newBCO#" GenPrimOp
1701    ByteArray# -> ByteArray# -> Array# a -> Int# -> ByteArray# -> State# s -> (# State# s, BCO# #)
1702    with
1703    has_side_effects = True
1704    out_of_line      = True
1705
1706 primop  UnpackClosureOp "unpackClosure#" GenPrimOp
1707    a -> (# Addr#, Array# b, ByteArray# #)
1708    with
1709    out_of_line = True
1710
1711 primop  GetApStackValOp "getApStackVal#" GenPrimOp
1712    a -> Int# -> (# Int#, b #)
1713    with
1714    out_of_line = True
1715
1716 ------------------------------------------------------------------------
1717 section "Etc" 
1718         {Miscellaneous built-ins}
1719 ------------------------------------------------------------------------
1720
1721 pseudoop   "seq"
1722    a -> b -> b
1723    { Evaluates its first argument to head normal form, and then returns its second
1724         argument as the result. }
1725
1726 pseudoop   "inline"
1727    a -> a
1728    { The call {\tt (inline f)} arranges that f is inlined, regardless of its size.
1729         More precisely, the call {\tt (inline f)} rewrites to the right-hand side of
1730         {\tt f}'s definition. This allows the programmer to control inlining from a
1731         particular call site rather than the definition site of the function (c.f.
1732         {\tt INLINE} pragmas in User's Guide, Section 7.10.3, "INLINE and NOINLINE
1733         pragmas").
1734
1735         This inlining occurs regardless of the argument to the call or the size of
1736         {\tt f}'s definition; it is unconditional. The main caveat is that {\tt f}'s
1737         definition must be visible to the compiler. That is, {\tt f} must be
1738         {\tt let}-bound in the current scope. If no inlining takes place, the
1739         {\tt inline} function expands to the identity function in Phase zero; so its
1740         use imposes no overhead.
1741
1742         If the function is defined in another module, GHC only exposes its inlining
1743         in the interface file if the function is sufficiently small that it might be
1744         inlined by the automatic mechanism. There is currently no way to tell GHC to
1745         expose arbitrarily-large functions in the interface file. (This shortcoming
1746         is something that could be fixed, with some kind of pragma.) }
1747
1748 pseudoop   "lazy"
1749    a -> a
1750    { The {\tt lazy} function restrains strictness analysis a little. The call
1751         {\tt (lazy e)} means the same as {\tt e}, but {\tt lazy} has a magical
1752         property so far as strictness analysis is concerned: it is lazy in its first
1753         argument, even though its semantics is strict. After strictness analysis has
1754         run, calls to {\tt lazy} are inlined to be the identity function.
1755
1756         This behaviour is occasionally useful when controlling evaluation order.
1757         Notably, {\tt lazy} is used in the library definition of {\tt Control.Parallel.par}:
1758
1759         {\tt par :: a -> b -> b}
1760
1761         {\tt par x y = case (par\# x) of \_ -> lazy y}
1762
1763         If {\tt lazy} were not lazy, {\tt par} would look strict in {\tt y} which
1764         would defeat the whole purpose of {\tt par}.
1765
1766         Like {\tt seq}, the argument of {\tt lazy} can have an unboxed type. }
1767
1768 primtype Any a
1769         { The type constructor {\tt Any} is type to which you can unsafely coerce any
1770         lifted type, and back. 
1771
1772           * It is lifted, and hence represented by a pointer
1773
1774           * It does not claim to be a {\it data} type, and that's important for
1775             the code generator, because the code gen may {\it enter} a data value
1776             but never enters a function value.  
1777
1778         It's also used to instantiate un-constrained type variables after type
1779         checking.  For example
1780
1781         {\tt length Any []}
1782
1783         Annoyingly, we sometimes need {\tt Any}s of other kinds, such as {\tt (* -> *)} etc.
1784         This is a bit like tuples.   We define a couple of useful ones here,
1785         and make others up on the fly.  If any of these others end up being exported
1786         into interface files, we'll get a crash; at least until we add interface-file
1787         syntax to support them. }
1788
1789 pseudoop   "unsafeCoerce#"
1790    a -> b
1791    { The function {\tt unsafeCoerce\#} allows you to side-step the typechecker entirely. That
1792         is, it allows you to coerce any type into any other type. If you use this function,
1793         you had better get it right, otherwise segmentation faults await. It is generally
1794         used when you want to write a program that you know is well-typed, but where Haskell's
1795         type system is not expressive enough to prove that it is well typed.
1796
1797         The following uses of {\tt unsafeCoerce\#} are supposed to work (i.e. not lead to
1798         spurious compile-time or run-time crashes):
1799
1800          * Casting any lifted type to {\tt Any}
1801
1802          * Casting {\tt Any} back to the real type
1803
1804          * Casting an unboxed type to another unboxed type of the same size
1805            (but not coercions between floating-point and integral types)
1806
1807          * Casting between two types that have the same runtime representation.  One case is when
1808            the two types differ only in "phantom" type parameters, for example
1809            {\tt Ptr Int} to {\tt Ptr Float}, or {\tt [Int]} to {\tt [Float]} when the list is 
1810            known to be empty.  Also, a {\tt newtype} of a type {\tt T} has the same representation
1811            at runtime as {\tt T}.
1812
1813         Other uses of {\tt unsafeCoerce\#} are undefined.  In particular, you should not use
1814         {\tt unsafeCoerce\#} to cast a T to an algebraic data type D, unless T is also
1815         an algebraic data type.  For example, do not cast {\tt Int->Int} to {\tt Bool}, even if
1816         you later cast that {\tt Bool} back to {\tt Int->Int} before applying it.  The reasons
1817         have to do with GHC's internal representation details (for the congnoscenti, data values
1818         can be entered but function closures cannot).  If you want a safe type to cast things
1819         to, use {\tt Any}, which is not an algebraic data type.
1820         
1821         }
1822
1823 -- NB. It is tempting to think that casting a value to a type that it doesn't have is safe
1824 -- as long as you don't "do anything" with the value in its cast form, such as seq on it.  This
1825 -- isn't the case: the compiler can insert seqs itself, and if these happen at the wrong type,
1826 -- Bad Things Might Happen.  See bug #1616: in this case we cast a function of type (a,b) -> (a,b)
1827 -- to () -> () and back again.  The strictness analyser saw that the function was strict, but
1828 -- the wrapper had type () -> (), and hence the wrapper de-constructed the (), the worker re-constructed
1829 -- a new (), with the result that the code ended up with "case () of (a,b) -> ...".
1830
1831 ------------------------------------------------------------------------
1832 ---                                                                  ---
1833 ------------------------------------------------------------------------
1834
1835 thats_all_folks
1836
1837
1838