[project @ 1999-11-16 17:38:54 by sewardj]
[ghc-hetmet.git] / ghc / rts / Assembler.c
1
2 /* --------------------------------------------------------------------------
3  * Bytecode assembler
4  *
5  * Copyright (c) 1994-1998.
6  *
7  * $RCSfile: Assembler.c,v $
8  * $Revision: 1.15 $
9  * $Date: 1999/11/16 17:39:07 $
10  *
11  * This module provides functions to construct BCOs and other closures
12  * required by the bytecode compiler.
13  *
14  * It is supposed to shield the compiler from platform dependent information
15  * such as:
16  *
17  * o sizeof(StgFloat)
18  * o sizeof(I#)
19  *
20  * and from details of how the abstract machine is implemented such as:
21  *
22  * o what does a BCO look like?
23  * o how many bytes does the "Push InfoTable" instruction require?
24  *
25  * Details of design:
26  * o (To handle letrecs) We allocate Aps, Paps and Cons using number of
27  *   heap allocated args to determine size.
28  *   We can't handle unboxed args :-(
29  * o All stack offsets are relative to position of Sp at start of
30  *   function or thunk (not BCO - consider continuations)
31  * o Active thunks must be roots during GC - how to achieve this?
32  * o Each BCO contains its own stack and heap check
33  *   We don't try to exploit the Hp check optimisation - easier to make
34  *   each thunk stand on its own.
35  * o asBind returns a "varid" (which is, in fact, a stack offset)
36  *   asVar acts on a "varid" - combining it with the current stack size to
37  *   determine actual position
38  * o Assembler.h uses totally neutral types: strings, floats, ints, etc
39  *   to minimise conflicts with other parts of the system.
40  * Simulated Stack
41  * ------------------------------------------------------------------------*/
42
43 #include "Rts.h"
44
45 #ifdef INTERPRETER
46
47 #include "RtsFlags.h"
48 #include "RtsUtils.h"
49 #include "Bytecodes.h"
50 #include "Printer.h"
51 #include "Disassembler.h"
52 #include "StgMiscClosures.h"
53 #include "Storage.h"
54 #include "Schedule.h"
55 #include "Evaluator.h"
56
57 #define INSIDE_ASSEMBLER_C
58 #include "Assembler.h"
59 #undef INSIDE_ASSEMBLER_C
60
61 /* --------------------------------------------------------------------------
62  * References between BCOs
63  *
64  * These are necessary because there can be circular references between 
65  * BCOs so we have to keep track of all the references to each object
66  * and fill in all the references once we're done.
67  *
68  * ToDo: generalise to allow references between any objects
69  * ------------------------------------------------------------------------*/
70
71 typedef struct {
72     AsmObject ref;  /* who refers to it                       */
73     AsmNat i;       /* index into some table held by referer  */
74 } AsmRef;
75
76 /* --------------------------------------------------------------------------
77  * Queues (of instructions, ptrs, nonptrs)
78  * ------------------------------------------------------------------------*/
79
80 #define Queue Instrs
81 #define Type  StgWord8
82 #include "QueueTemplate.h"
83 #undef Type
84 #undef Queue
85
86 #define Queue Ptrs
87 #define Type  AsmObject
88 #include "QueueTemplate.h"
89 #undef Type
90 #undef Queue
91
92 #define Queue Refs
93 #define Type  AsmRef
94 #include "QueueTemplate.h"
95 #undef Type
96 #undef Queue
97
98 #define Queue NonPtrs
99 #define Type  StgWord
100 #include "QueueTemplate.h"
101 #undef Type
102 #undef Queue
103
104 /* --------------------------------------------------------------------------
105  * AsmObjects are used to build heap objects.
106  *
107  * AsmObjects can contain circular references to each other
108  * so we have to keep track of all the references which can't be filled
109  * in yet.
110  *
111  * When we finish building an AsmObject, we allocate an actual heap object and
112  * fill in all the references to the asmObject with pointers to the heap object.
113  *
114  * We obtain a limited form of polymorphism through inheritance by putting 
115  * the AsmObject first in every structure (as in C++ implementations).
116  * We use the closure type of the allocated object to figure out
117  * where the payload lives in the closure.
118  * ------------------------------------------------------------------------*/
119 /* ToDo: clean up terminology: is Closure right or should it be object or ... */
120
121 struct AsmObject_ {
122     Refs           refs;
123     Ptrs           ptrs;
124     AsmNat         num_unresolved; /* number of unfilled references */
125     StgClosure*    closure;        /* where object was allocated    */
126 };
127     
128 struct AsmCon_ {
129     struct AsmObject_ object;  /* must be first in struct */
130
131     AsmInfo info;
132 };
133   
134 struct AsmCAF_ {
135     struct AsmObject_ object;  /* must be first in struct */
136 };
137
138 struct AsmBCO_ {
139     struct AsmObject_ object;  /* must be first in struct */
140
141     Instrs   is;          
142     NonPtrs  nps;
143
144     int /*StgExpr*/  stgexpr;    
145
146     /* abstract machine ("executed" during compilation) */
147     AsmSp    sp;          /* stack ptr */
148     AsmSp    max_sp;
149     StgWord  hp;          /* heap ptr  */
150     StgWord  max_hp;
151     Instr    lastOpc;
152 };
153
154 static void asmResolveRef( AsmObject obj, AsmNat i, AsmClosure reference )
155 {
156     ASSERT(obj->closure);
157     switch (get_itbl(obj->closure)->type) {
158     case BCO:
159         {
160             StgBCO* bco = stgCast(StgBCO*,obj->closure);
161             ASSERT(i < bco->n_ptrs && bcoConstPtr(bco,i) == NULL);
162             bcoConstCPtr(bco,i) = reference;
163             break;
164         }
165     case CAF_UNENTERED:
166         {
167             StgCAF* caf = stgCast(StgCAF*,obj->closure);
168             ASSERT(i == 0 && caf->body == NULL);
169             caf->body = reference;
170             break;
171         }
172     case CONSTR:
173         {
174             StgClosure* con = stgCast(StgClosure*,obj->closure);
175             ASSERT(i < get_itbl(con)->layout.payload.nptrs && payloadCPtr(con,i) == NULL);
176             payloadCPtr(con,i) = reference;
177             break;
178         }
179     case AP_UPD:
180         {
181             StgAP_UPD* ap = stgCast(StgAP_UPD*,obj->closure);
182             ASSERT(i < 1+ap->n_args);
183             if (i==0) {
184                 ASSERT(ap->fun == NULL);
185                 ap->fun = reference;
186             } else {
187                 ASSERT(payloadCPtr(ap,i-1) == NULL);
188                 payloadCPtr(ap,i-1) = reference;
189             }
190             break;
191         }
192     default:
193             barf("asmResolveRef");
194     }
195     obj->num_unresolved -= 1;
196 }
197
198 static void asmAddRef( AsmObject referent, AsmObject referer, AsmNat i )
199 {
200     if (referent->closure) {
201         asmResolveRef(referer,i,(AsmClosure)referent->closure);
202     } else {
203         insertRefs(&(referent->refs),(AsmRef){referer,i});
204     }
205 }
206
207 void asmAddPtr( AsmObject obj, AsmObject arg )
208 {
209     ASSERT(obj->closure == 0); /* can't extend an object once it's allocated */
210     insertPtrs( &obj->ptrs, arg );
211 }
212
213 static void asmBeginObject( AsmObject obj )
214 {
215     obj->closure = NULL;
216     obj->num_unresolved = 0;
217     initRefs(&obj->refs);
218     initPtrs(&obj->ptrs);
219 }
220
221 static void asmEndObject( AsmObject obj, StgClosure* c )
222 {
223     obj->num_unresolved = obj->ptrs.len;
224     obj->closure = c;
225     mapQueue(Ptrs,    AsmObject, obj->ptrs, asmAddRef(x,obj,i));
226     mapQueue(Refs,    AsmRef,    obj->refs, asmResolveRef(x.ref,x.i,c));
227
228     if (obj->num_unresolved == 0) {
229         freePtrs(&obj->ptrs);
230         freeRefs(&obj->refs);
231         /* we don't print until all ptrs are resolved */
232         IF_DEBUG(codegen,printObj(obj->closure));
233     }
234 }
235
236 int asmObjectHasClosure ( AsmObject obj )
237 {
238     return (obj->num_unresolved == 0 && obj->closure);
239 }
240
241 AsmClosure asmClosureOfObject ( AsmObject obj )
242 {
243     ASSERT(asmObjectHasClosure(obj));
244     return obj->closure;
245 }
246
247 void asmMarkObject ( AsmObject obj )
248 {
249     ASSERT(obj->num_unresolved == 0 && obj->closure);
250     obj->closure = MarkRoot(obj->closure);
251 }
252
253 /* --------------------------------------------------------------------------
254  * Heap allocation
255  * ------------------------------------------------------------------------*/
256
257 static StgClosure* asmAlloc( nat size )
258 {
259     StgClosure* o = stgCast(StgClosure*,allocate(size));
260     ASSERT( size >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
261     /* printf("Allocated %p .. %p\n", o, o+size-1); */
262     return o;
263 }
264
265 static void grabHpUpd( AsmBCO bco, nat size )
266 {
267     /* ToDo: sometimes we should test for MIN_UPD_SIZE instead */
268     ASSERT( size >= MIN_UPD_SIZE + sizeofW(StgHeader) );
269     bco->hp += size;
270 }
271
272 static void grabHpNonUpd( AsmBCO bco, nat size )
273 {
274     /* ToDo: sometimes we should test for MIN_UPD_SIZE instead */
275     ASSERT( size >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
276     bco->hp += size;
277 }
278
279 static void resetHp( AsmBCO bco, nat hp )
280 {
281     bco->max_hp = stg_max(bco->hp,bco->max_hp);
282     bco->hp     = hp;
283 }
284
285 static void setSp( AsmBCO bco, AsmSp sp )
286 {
287     bco->max_sp = stg_max(bco->sp,bco->max_sp);
288     bco->sp     = sp;
289     bco->max_sp = stg_max(bco->sp,bco->max_sp);
290 }
291
292 static void incSp ( AsmBCO bco, int sp_delta )
293 {
294     bco->max_sp  = stg_max(bco->sp,bco->max_sp);
295     bco->sp     += sp_delta;
296     bco->max_sp  = stg_max(bco->sp,bco->max_sp);
297 }
298
299 static void decSp ( AsmBCO bco, int sp_delta )
300 {
301     bco->max_sp  = stg_max(bco->sp,bco->max_sp);
302     bco->sp     -= sp_delta;
303     bco->max_sp  = stg_max(bco->sp,bco->max_sp);
304 }
305
306 /* --------------------------------------------------------------------------
307  * 
308  * ------------------------------------------------------------------------*/
309
310 AsmObject asmMkObject( AsmClosure c )
311 {
312     AsmObject obj = malloc(sizeof(struct AsmObject_));
313     if (obj == NULL) {
314         barf("Can't allocate AsmObject");
315     }
316     asmBeginObject(obj);
317     asmEndObject(obj,c);
318     return obj;
319 }
320
321 AsmCon asmBeginCon( AsmInfo info )
322 {
323     AsmCon con = malloc(sizeof(struct AsmCon_));
324     if (con == NULL) {
325         barf("Can't allocate AsmCon");
326     }
327     asmBeginObject(&con->object);
328     con->info = info;
329     return con;
330 }
331
332 void asmEndCon( AsmCon con )
333 {
334     nat p  = con->object.ptrs.len;
335     nat np = stg_max(0,MIN_NONUPD_SIZE-p);
336
337     StgClosure* c = asmAlloc(CONSTR_sizeW(p,np));
338     StgClosure* o = stgCast(StgClosure*,c);
339     SET_HDR(o,con->info,??);
340     mapQueue(Ptrs,    AsmObject, con->object.ptrs, payloadCPtr(o,i) = NULL);
341     { nat i; for( i=0; i<np; ++i ) { payloadWord(o,p+i) = 0xdeadbeef; } }
342     asmEndObject(&con->object,c);
343 }
344
345 AsmCAF asmBeginCAF( void )
346 {
347     AsmCAF caf = malloc(sizeof(struct AsmCAF_));
348     if (caf == NULL) {
349         barf("Can't allocate AsmCAF");
350     }
351     asmBeginObject(&caf->object);
352     return caf;
353 }
354
355 void asmEndCAF( AsmCAF caf, AsmBCO body )
356 {
357     StgClosure* c = asmAlloc(CAF_sizeW());
358     StgCAF*     o = stgCast(StgCAF*,c);
359     SET_HDR(o,&CAF_UNENTERED_info,??);
360     o->body  = NULL;
361     o->value = stgCast(StgClosure*,0xdeadbeef);
362     o->link  = stgCast(StgCAF*,0xdeadbeef);
363     o->mut_link = NULL;
364     asmAddPtr(&caf->object,&body->object);
365     asmEndObject(&caf->object,c);
366 }
367
368 AsmBCO asmBeginBCO( int /*StgExpr*/ e )
369 {
370     AsmBCO bco = malloc(sizeof(struct AsmBCO_));
371     if (bco == NULL) {
372         barf("Can't allocate AsmBCO");
373     }
374     asmBeginObject(&bco->object);
375     initInstrs(&bco->is);
376     initNonPtrs(&bco->nps);
377
378     bco->stgexpr = e;
379     bco->max_sp = bco->sp = 0;
380     bco->max_hp = bco->hp = 0;
381     bco->lastOpc = i_INTERNAL_ERROR;
382     return bco;
383 }
384
385 void asmEndBCO( AsmBCO bco )
386 {
387     nat p  = bco->object.ptrs.len;
388     nat np = bco->nps.len;
389     nat is = bco->is.len + (bco->max_sp <= 255 ? 2 : 3);  /* 2 or 3 for stack check */
390
391     StgClosure* c = asmAlloc(BCO_sizeW(p,np,is));
392     StgBCO*     o = stgCast(StgBCO*,c);
393     SET_HDR(o,&BCO_info,??);
394     o->n_ptrs   = p;
395     o->n_words  = np;
396     o->n_instrs = is;
397     o->stgexpr  = bco->stgexpr;
398     mapQueue(Ptrs,    AsmObject, bco->object.ptrs, bcoConstCPtr(o,i) = NULL);
399     mapQueue(NonPtrs, StgWord,   bco->nps,  bcoConstWord(o,i) = x);
400     {
401         nat j = 0;
402         bco->max_sp = stg_max(bco->sp,bco->max_sp);
403         bco->max_hp = stg_max(bco->hp,bco->max_hp);
404
405         ASSERT(bco->max_sp <= 65535);
406         if (bco->max_sp <= 255) {
407            bcoInstr(o,j++) = i_STK_CHECK;
408            bcoInstr(o,j++) = bco->max_sp;
409         } else {
410            bcoInstr(o,j++) = i_STK_CHECK_big;
411            bcoInstr(o,j++) = bco->max_sp / 256;
412            bcoInstr(o,j++) = bco->max_sp % 256;
413         }
414
415         mapQueue(Instrs,  StgWord8,   bco->is,   bcoInstr(o,j++) = x);
416         ASSERT(j == is);
417     }
418     freeInstrs(&bco->is);
419     freeNonPtrs(&bco->nps);
420     asmEndObject(&bco->object,c);
421 }
422
423 /* --------------------------------------------------------------------------
424  * 
425  * ------------------------------------------------------------------------*/
426
427 static void asmInstrOp ( AsmBCO bco, StgWord i )
428 {
429     ASSERT(i <= BIGGEST_OPCODE); /* must be a valid opcode */
430     bco->lastOpc = i;
431     insertInstrs(&(bco->is),i);
432 }
433
434 static void asmInstr8 ( AsmBCO bco, StgWord i )
435 {
436   if (i >= 256) {
437     ASSERT(i < 256); /* must be a byte */
438   }
439     insertInstrs(&(bco->is),i);
440 }
441
442 static void asmInstr16 ( AsmBCO bco, StgWord i )
443 {
444     ASSERT(i < 65536); /* must be a short */
445     insertInstrs(&(bco->is),i / 256);
446     insertInstrs(&(bco->is),i % 256);
447 }
448
449 static Instr asmInstrBack ( AsmBCO bco, StgWord n )
450 {
451    return bco->is.elems[bco->is.len - n];
452 }
453
454 static void asmInstrRecede ( AsmBCO bco, StgWord n )
455 {
456    if (bco->is.len < n) barf("asmInstrRecede");
457    bco->is.len -= n;
458 }
459
460 static void asmPtr( AsmBCO bco, AsmObject x )
461 {
462     insertPtrs( &bco->object.ptrs, x );
463 }
464
465 static void asmWord( AsmBCO bco, StgWord i )
466 {
467     insertNonPtrs( &bco->nps, i );
468 }
469
470 #define asmWords(bco,ty,x)                               \
471     {                                                    \
472         union { ty a; AsmWord b[sizeofW(ty)]; } p;       \
473         nat i;                                           \
474         if (sizeof(ty) < sizeof(AsmWord)) p.b[0]=0;      \
475         p.a = x;                                         \
476         for( i = 0; i < sizeofW(ty); i++ ) {             \
477             asmWord(bco,p.b[i]);                         \
478         }                                                \
479     }
480
481 static StgWord repSizeW( AsmRep rep )
482 {
483     switch (rep) {
484     case CHAR_REP:    return sizeofW(StgWord) + sizeofW(StgChar);
485
486     case BOOL_REP:
487     case INT_REP:     return sizeofW(StgWord) + sizeofW(StgInt);
488     case WORD_REP:    return sizeofW(StgWord) + sizeofW(StgWord);
489     case ADDR_REP:    return sizeofW(StgWord) + sizeofW(StgAddr);
490     case FLOAT_REP:   return sizeofW(StgWord) + sizeofW(StgFloat);
491     case DOUBLE_REP:  return sizeofW(StgWord) + sizeofW(StgDouble);
492     case STABLE_REP:  return sizeofW(StgWord) + sizeofW(StgWord);
493
494     case INTEGER_REP: 
495 #ifdef PROVIDE_WEAK
496     case WEAK_REP: 
497 #endif
498 #ifdef PROVIDE_FOREIGN
499     case FOREIGN_REP: 
500 #endif
501     case ALPHA_REP:    /* a                        */ 
502     case BETA_REP:     /* b                        */ 
503     case GAMMA_REP:    /* c                        */ 
504     case DELTA_REP:    /* d                        */ 
505     case HANDLER_REP:  /* IOError -> IO a          */ 
506     case ERROR_REP:    /* IOError                  */ 
507     case ARR_REP    :  /* PrimArray              a */ 
508     case BARR_REP   :  /* PrimByteArray          a */ 
509     case REF_REP    :  /* Ref                  s a */ 
510     case MUTARR_REP :  /* PrimMutableArray     s a */ 
511     case MUTBARR_REP:  /* PrimMutableByteArray s a */ 
512     case THREADID_REP: /* ThreadId                 */ 
513     case MVAR_REP:     /* MVar a                   */ 
514     case PTR_REP:     return sizeofW(StgPtr);
515
516     case VOID_REP:    return sizeofW(StgWord);
517     default:          barf("repSizeW %d",rep);
518     }
519 }
520
521
522 int asmRepSizeW ( AsmRep rep )
523 {
524    return repSizeW ( rep );
525 }
526
527
528 /* --------------------------------------------------------------------------
529  * Instruction emission.  All instructions should be routed through here
530  * so that the peephole optimiser gets to see what's happening.
531  * ------------------------------------------------------------------------*/
532
533 static void emiti_ ( AsmBCO bco, Instr opcode )
534 {
535    StgInt x, y;
536    if (bco->lastOpc == i_SLIDE && opcode == i_ENTER) {
537       /* SLIDE x y ; ENTER   ===>  SE x y */
538       x = asmInstrBack(bco,2);
539       y = asmInstrBack(bco,1); 
540       asmInstrRecede(bco,3);
541       asmInstrOp(bco,i_SE); asmInstr8(bco,x); asmInstr8(bco,y);
542    }
543    else
544    if (bco->lastOpc == i_RV && opcode == i_ENTER) {
545       /* RV x y ; ENTER ===> RVE x (y-2)
546          Because RETADDR pushes 2 words on the stack, y must be at least 2. */
547       x = asmInstrBack(bco,2);
548       y = asmInstrBack(bco,1);
549       if (y < 2) barf("emiti_: RVE: impossible y value");
550       asmInstrRecede(bco,3);
551       asmInstrOp(bco, i_RVE); asmInstr8(bco,x); asmInstr8(bco,y-2);
552    }
553    else {
554       asmInstrOp(bco,opcode);
555    }
556 }
557
558 static void emiti_8 ( AsmBCO bco, Instr opcode, int arg1 )
559 {
560    StgInt x;
561    if (bco->lastOpc == i_VAR && opcode == i_VAR) {
562       /* VAR x ; VAR y ===>  VV x y */
563       x = asmInstrBack(bco,1);
564       asmInstrRecede(bco,2);
565       asmInstrOp(bco,i_VV); asmInstr8(bco,x); asmInstr8(bco,arg1);
566    } 
567    else 
568    if (bco->lastOpc == i_RETADDR && opcode == i_VAR) {
569       /* RETADDR x ; VAR y ===> RV x y */
570       x = asmInstrBack(bco,1);
571       asmInstrRecede(bco,2);
572       asmInstrOp(bco, i_RV); asmInstr8(bco,x); asmInstr8(bco,arg1);
573    }
574    else {
575       asmInstrOp(bco,opcode);
576       asmInstr8(bco,arg1);
577    }
578 }
579
580 static void emiti_16 ( AsmBCO bco, Instr opcode, int arg1 )
581 {
582    asmInstrOp(bco,opcode);
583    asmInstr16(bco,arg1);
584 }
585
586 static void emiti_8_8 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
587 {
588    asmInstrOp(bco,opcode);
589    asmInstr8(bco,arg1);
590    asmInstr8(bco,arg2);
591 }
592
593 static void emiti_8_16 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
594 {
595    asmInstrOp(bco,opcode);
596    asmInstr8(bco,arg1);
597    asmInstr16(bco,arg2);
598 }
599
600 static void emiti_16_16 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
601 {
602    asmInstrOp(bco,opcode);
603    asmInstr16(bco,arg1);
604    asmInstr16(bco,arg2);
605 }
606
607
608 /* --------------------------------------------------------------------------
609  * Wrappers around the above fns
610  * ------------------------------------------------------------------------*/
611
612 static void emit_i_VAR_INT ( AsmBCO bco, int arg1 )
613 {
614    ASSERT(arg1 >= 0);
615    if (arg1 < 256)
616       emiti_8 (bco,i_VAR_INT,    arg1); else
617       emiti_16(bco,i_VAR_INT_big,arg1);
618 }
619
620 static void emit_i_VAR_WORD ( AsmBCO bco, int arg1 )
621 {
622    ASSERT(arg1 >= 0);
623    if (arg1 < 256)
624       emiti_8 (bco,i_VAR_WORD,    arg1); else
625       emiti_16(bco,i_VAR_WORD_big,arg1);
626 }
627
628 static void emit_i_VAR_ADDR ( AsmBCO bco, int arg1 )
629 {
630    ASSERT(arg1 >= 0);
631    if (arg1 < 256)
632       emiti_8 (bco,i_VAR_ADDR,    arg1); else
633       emiti_16(bco,i_VAR_ADDR_big,arg1);
634 }
635
636 static void emit_i_VAR_CHAR ( AsmBCO bco, int arg1 )
637 {
638    ASSERT(arg1 >= 0);
639    if (arg1 < 256)
640       emiti_8 (bco,i_VAR_CHAR,    arg1); else
641       emiti_16(bco,i_VAR_CHAR_big,arg1);
642 }
643
644 static void emit_i_VAR_FLOAT ( AsmBCO bco, int arg1 )
645 {
646    ASSERT(arg1 >= 0);
647    if (arg1 < 256)
648       emiti_8 (bco,i_VAR_FLOAT,    arg1); else
649       emiti_16(bco,i_VAR_FLOAT_big,arg1);
650 }
651
652 static void emit_i_VAR_DOUBLE ( AsmBCO bco, int arg1 )
653 {
654    ASSERT(arg1 >= 0);
655    if (arg1 < 256)
656       emiti_8 (bco,i_VAR_DOUBLE,    arg1); else
657       emiti_16(bco,i_VAR_DOUBLE_big,arg1);
658 }
659
660 static void emit_i_VAR_STABLE ( AsmBCO bco, int arg1 )
661 {
662    ASSERT(arg1 >= 0);
663    if (arg1 < 256)
664       emiti_8 (bco,i_VAR_STABLE,    arg1); else
665       emiti_16(bco,i_VAR_STABLE_big,arg1);
666 }
667
668 static void emit_i_VAR ( AsmBCO bco, int arg1 )
669 {
670    ASSERT(arg1 >= 0);
671    if (arg1 < 256)
672       emiti_8 (bco,i_VAR,    arg1); else
673       emiti_16(bco,i_VAR_big,arg1);
674 }
675
676 static void emit_i_PACK ( AsmBCO bco, int arg1 )
677 {
678    ASSERT(arg1 >= 0);
679    if (arg1 < 256)
680       emiti_8 (bco,i_PACK,    arg1); else
681       emiti_16(bco,i_PACK_big,arg1);
682 }
683
684 static void emit_i_SLIDE ( AsmBCO bco, int arg1, int arg2 )
685 {
686    ASSERT(arg1 >= 0);
687    ASSERT(arg2 >= 0);
688    if (arg1 < 256 && arg2 < 256)
689       emiti_8_8  (bco,i_SLIDE,    arg1,arg2); else
690       emiti_16_16(bco,i_SLIDE_big,arg1,arg2);
691 }
692
693 static void emit_i_MKAP ( AsmBCO bco, int arg1, int arg2 )
694 {
695    ASSERT(arg1 >= 0);
696    ASSERT(arg2 >= 0);
697    if (arg1 < 256 && arg2 < 256)
698       emiti_8_8  (bco,i_MKAP,    arg1,arg2); else
699       emiti_16_16(bco,i_MKAP_big,arg1,arg2);
700 }
701
702
703 static void emit_i_CONST_INT ( AsmBCO bco, int arg1 )
704 {
705    ASSERT(arg1 >= 0);
706    if (arg1 < 256)
707       emiti_8 (bco,i_CONST_INT,    arg1); else
708       emiti_16(bco,i_CONST_INT_big,arg1);
709 }
710
711 static void emit_i_CONST_INTEGER ( AsmBCO bco, int arg1 )
712 {
713    ASSERT(arg1 >= 0);
714    if (arg1 < 256)
715       emiti_8 (bco,i_CONST_INTEGER,    arg1); else
716       emiti_16(bco,i_CONST_INTEGER_big,arg1);
717 }
718
719 static void emit_i_CONST_ADDR ( AsmBCO bco, int arg1 )
720 {
721    ASSERT(arg1 >= 0);
722    if (arg1 < 256)
723       emiti_8 (bco,i_CONST_ADDR,    arg1); else
724       emiti_16(bco,i_CONST_ADDR_big,arg1);
725 }
726
727 static void emit_i_CONST_CHAR ( AsmBCO bco, int arg1 )
728 {
729    ASSERT(arg1 >= 0);
730    if (arg1 < 256)
731       emiti_8 (bco,i_CONST_CHAR,    arg1); else
732       emiti_16(bco,i_CONST_CHAR_big,arg1);
733 }
734
735 static void emit_i_CONST_FLOAT ( AsmBCO bco, int arg1 )
736 {
737    ASSERT(arg1 >= 0);
738    if (arg1 < 256)
739       emiti_8 (bco,i_CONST_FLOAT,    arg1); else
740       emiti_16(bco,i_CONST_FLOAT_big,arg1);
741 }
742
743 static void emit_i_CONST_DOUBLE ( AsmBCO bco, int arg1 )
744 {
745    ASSERT(arg1 >= 0);
746    if (arg1 < 256)
747       emiti_8 (bco,i_CONST_DOUBLE,    arg1); else
748       emiti_16(bco,i_CONST_DOUBLE_big,arg1);
749 }
750
751 static void emit_i_CONST ( AsmBCO bco, int arg1 )
752 {
753    ASSERT(arg1 >= 0);
754    if (arg1 < 256)
755       emiti_8 (bco,i_CONST,    arg1); else
756       emiti_16(bco,i_CONST_big,arg1);
757 }
758
759 static void emit_i_RETADDR ( AsmBCO bco, int arg1 )
760 {
761    ASSERT(arg1 >= 0);
762    if (arg1 < 256)
763       emiti_8 (bco,i_RETADDR,    arg1); else
764       emiti_16(bco,i_RETADDR_big,arg1);
765 }
766
767
768 /* --------------------------------------------------------------------------
769  * Arg checks.
770  * ------------------------------------------------------------------------*/
771
772 AsmSp  asmBeginArgCheck ( AsmBCO bco )
773 {
774     ASSERT(bco->sp == 0);
775     return bco->sp;
776 }
777
778 void   asmEndArgCheck   ( AsmBCO bco, AsmSp last_arg )
779 {
780     nat args = bco->sp - last_arg;
781     if (args != 0) { /* optimisation */
782         emiti_8(bco,i_ARG_CHECK,args);
783         grabHpNonUpd(bco,PAP_sizeW(args-1));
784         resetHp(bco,0);
785     }
786 }
787
788 /* --------------------------------------------------------------------------
789  * Creating and using "variables"
790  * ------------------------------------------------------------------------*/
791
792 AsmVar asmBind          ( AsmBCO bco, AsmRep rep )
793 {
794     incSp(bco,repSizeW(rep));
795     return bco->sp;
796 }
797
798 void   asmVar           ( AsmBCO bco, AsmVar v, AsmRep rep )
799 {
800     int offset;
801
802     if (rep == VOID_REP) {
803         emiti_(bco,i_VOID);
804         incSp(bco,repSizeW(rep));
805         return;
806     }
807
808     offset = bco->sp - v;
809     switch (rep) {
810     case BOOL_REP:
811     case INT_REP:
812             emit_i_VAR_INT(bco,offset);
813             break;
814     case WORD_REP:
815             emit_i_VAR_WORD(bco,offset);
816             break;
817     case ADDR_REP:
818             emit_i_VAR_ADDR(bco,offset);
819             break;
820     case CHAR_REP:
821             emit_i_VAR_CHAR(bco,offset);
822             break;
823     case FLOAT_REP:
824             emit_i_VAR_FLOAT(bco,offset);
825             break;
826     case DOUBLE_REP:
827             emit_i_VAR_DOUBLE(bco,offset);
828             break;
829     case STABLE_REP:
830             emit_i_VAR_STABLE(bco,offset);
831             break;
832
833     case INTEGER_REP:
834 #ifdef PROVIDE_WEAK
835     case WEAK_REP: 
836 #endif
837 #ifdef PROVIDE_FOREIGN
838     case FOREIGN_REP:
839 #endif
840     case ALPHA_REP:    /* a                        */ 
841     case BETA_REP:     /* b                        */
842     case GAMMA_REP:    /* c                        */ 
843     case DELTA_REP:    /* d                        */ 
844     case HANDLER_REP:  /* IOError -> IO a          */
845     case ERROR_REP:    /* IOError                  */
846     case ARR_REP    :  /* PrimArray              a */
847     case BARR_REP   :  /* PrimByteArray          a */
848     case REF_REP    :  /* Ref                  s a */
849     case MUTARR_REP :  /* PrimMutableArray     s a */
850     case MUTBARR_REP:  /* PrimMutableByteArray s a */
851     case THREADID_REP: /* ThreadId                 */
852     case MVAR_REP:     /* MVar a                   */
853     case PTR_REP:
854             emit_i_VAR(bco,offset);
855             break;
856     default:
857             barf("asmVar %d",rep);
858     }
859     incSp(bco,repSizeW(rep));
860 }
861
862 /* --------------------------------------------------------------------------
863  * Tail calls
864  * ------------------------------------------------------------------------*/
865
866 AsmSp asmBeginEnter( AsmBCO bco )
867 {
868     return bco->sp;
869 }
870
871 void asmEndEnter( AsmBCO bco, AsmSp sp1, AsmSp sp2 )
872 {
873     int x = bco->sp - sp1;
874     int y = sp1 - sp2;
875     ASSERT(x >= 0 && y >= 0);
876     if (y != 0) {
877         emit_i_SLIDE(bco,x,y);
878         decSp(bco,sp1 - sp2);
879     }
880     emiti_(bco,i_ENTER);
881     decSp(bco,sizeofW(StgPtr));
882 }
883
884 /* --------------------------------------------------------------------------
885  * Build boxed Ints, Floats, etc
886  * ------------------------------------------------------------------------*/
887
888 AsmVar asmBox( AsmBCO bco, AsmRep rep )
889 {
890     switch (rep) {
891     case CHAR_REP:
892             emiti_(bco,i_PACK_CHAR);
893             grabHpNonUpd(bco,Czh_sizeW);
894             break;
895     case INT_REP:
896             emiti_(bco,i_PACK_INT);
897             grabHpNonUpd(bco,Izh_sizeW);
898             break;
899     case WORD_REP:
900             emiti_(bco,i_PACK_WORD);
901             grabHpNonUpd(bco,Wzh_sizeW);
902             break;
903     case ADDR_REP:
904             emiti_(bco,i_PACK_ADDR);
905             grabHpNonUpd(bco,Azh_sizeW);
906             break;
907     case FLOAT_REP:
908             emiti_(bco,i_PACK_FLOAT);
909             grabHpNonUpd(bco,Fzh_sizeW);
910             break;
911     case DOUBLE_REP:
912             emiti_(bco,i_PACK_DOUBLE);
913             grabHpNonUpd(bco,Dzh_sizeW);
914             break;
915     case STABLE_REP:
916             emiti_(bco,i_PACK_STABLE);
917             grabHpNonUpd(bco,Stablezh_sizeW);
918             break;
919
920     default:
921             barf("asmBox %d",rep);
922     }
923     /* NB: these operations DO pop their arg       */
924     decSp(bco, repSizeW(rep));   /* pop unboxed arg */
925     incSp(bco, sizeofW(StgPtr)); /* push box        */
926     return bco->sp;
927 }
928
929 /* --------------------------------------------------------------------------
930  * Unbox Ints, Floats, etc
931  * ------------------------------------------------------------------------*/
932
933 AsmVar asmUnbox( AsmBCO bco, AsmRep rep )
934 {
935     switch (rep) {
936     case INT_REP:
937             emiti_(bco,i_UNPACK_INT);
938             break;
939     case WORD_REP:
940             emiti_(bco,i_UNPACK_WORD);
941             break;
942     case ADDR_REP:
943             emiti_(bco,i_UNPACK_ADDR);
944             break;
945     case CHAR_REP:
946             emiti_(bco,i_UNPACK_CHAR);
947             break;
948     case FLOAT_REP:
949             emiti_(bco,i_UNPACK_FLOAT);
950             break;
951     case DOUBLE_REP:
952             emiti_(bco,i_UNPACK_DOUBLE);
953             break;
954     case STABLE_REP:
955             emiti_(bco,i_UNPACK_STABLE);
956             break;
957     default:
958             barf("asmUnbox %d",rep);
959     }
960     /* NB: these operations DO NOT pop their arg  */
961     incSp(bco, repSizeW(rep)); /* push unboxed arg */
962     return bco->sp;
963 }
964
965
966 /* --------------------------------------------------------------------------
967  * Push unboxed Ints, Floats, etc
968  * ------------------------------------------------------------------------*/
969
970 void asmConstInt( AsmBCO bco, AsmInt x )
971 {
972     emit_i_CONST_INT(bco,bco->nps.len);
973     asmWords(bco,AsmInt,x);
974     incSp(bco, repSizeW(INT_REP));
975 }
976
977 void asmConstInteger( AsmBCO bco, AsmString x )
978 {
979     emit_i_CONST_INTEGER(bco,bco->nps.len);
980     asmWords(bco,AsmString,x);
981     incSp(bco, repSizeW(INTEGER_REP));
982 }
983
984 void asmConstAddr( AsmBCO bco, AsmAddr x )
985 {
986     emit_i_CONST_ADDR(bco,bco->nps.len);
987     asmWords(bco,AsmAddr,x);
988     incSp(bco, repSizeW(ADDR_REP));
989 }
990
991 void asmConstWord( AsmBCO bco, AsmWord x )
992 {
993     emit_i_CONST_INT(bco,bco->nps.len);
994     asmWords(bco,AsmWord,(AsmInt)x);
995     incSp(bco, repSizeW(WORD_REP));
996 }
997
998 void asmConstChar( AsmBCO bco, AsmChar x )
999 {
1000     emit_i_CONST_CHAR(bco,bco->nps.len);
1001     asmWords(bco,AsmChar,x);
1002     incSp(bco, repSizeW(CHAR_REP));
1003 }
1004
1005 void asmConstFloat( AsmBCO bco, AsmFloat x )
1006 {
1007     emit_i_CONST_FLOAT(bco,bco->nps.len);
1008     asmWords(bco,AsmFloat,x);
1009     incSp(bco, repSizeW(FLOAT_REP));
1010 }
1011
1012 void asmConstDouble( AsmBCO bco, AsmDouble x )
1013 {
1014     emit_i_CONST_DOUBLE(bco,bco->nps.len);
1015     asmWords(bco,AsmDouble,x);
1016     incSp(bco, repSizeW(DOUBLE_REP));
1017 }
1018
1019 /* --------------------------------------------------------------------------
1020  * Algebraic case helpers
1021  * ------------------------------------------------------------------------*/
1022
1023 /* a mildly bogus pair of functions... */
1024 AsmSp asmBeginCase( AsmBCO bco )
1025 {
1026     return bco->sp;
1027 }
1028
1029 void asmEndCase( AsmBCO bco )
1030 {
1031 }
1032
1033 AsmSp asmContinuation( AsmBCO bco, AsmBCO ret_addr )
1034 {
1035     emit_i_RETADDR(bco,bco->object.ptrs.len);
1036     asmPtr(bco,&(ret_addr->object));
1037     incSp(bco, 2 * sizeofW(StgPtr));
1038     return bco->sp;
1039 }
1040
1041 AsmBCO asmBeginContinuation ( AsmSp sp, int /*List*/ alts )
1042 {
1043     AsmBCO bco = asmBeginBCO(alts);
1044     setSp(bco, sp);
1045     return bco;
1046 }
1047
1048 void asmEndContinuation ( AsmBCO bco )
1049 {
1050     asmEndBCO(bco);
1051 }
1052
1053
1054 /* --------------------------------------------------------------------------
1055  * Branches
1056  * ------------------------------------------------------------------------*/
1057
1058 AsmSp asmBeginAlt( AsmBCO bco )
1059 {
1060     return bco->sp;
1061 }
1062
1063 void asmEndAlt( AsmBCO bco, AsmSp  sp )
1064 {
1065     setSp(bco,sp);
1066 }
1067
1068 AsmPc asmTest( AsmBCO bco, AsmWord tag )
1069 {
1070     emiti_8_16(bco,i_TEST,tag,0);
1071     return bco->is.len;
1072 }
1073
1074 AsmPc asmTestInt( AsmBCO bco, AsmVar v, AsmInt x )
1075 {
1076     asmVar(bco,v,INT_REP);
1077     asmConstInt(bco,x);
1078     emiti_16(bco,i_TEST_INT,0);
1079     decSp(bco, 2*repSizeW(INT_REP));
1080     return bco->is.len;
1081 }
1082
1083 void asmFixBranch( AsmBCO bco, AsmPc from )
1084 {
1085     int distance = bco->is.len - from;
1086     ASSERT(distance >= 0);
1087     ASSERT(distance < 65536);
1088     setInstrs(&(bco->is),from-2,distance/256);
1089     setInstrs(&(bco->is),from-1,distance%256);
1090 }
1091
1092 void asmPanic( AsmBCO bco )
1093 {
1094     emiti_(bco,i_PANIC); /* "irrefutable" pattern failed - oops! */
1095 }
1096
1097 /* --------------------------------------------------------------------------
1098  * Primops
1099  * ------------------------------------------------------------------------*/
1100
1101 AsmSp asmBeginPrim( AsmBCO bco )
1102 {
1103     return bco->sp;
1104 }
1105
1106 void   asmEndPrim( AsmBCO bco, const AsmPrim* prim, AsmSp base )
1107 {
1108     emiti_8(bco,prim->prefix,prim->opcode);
1109     setSp(bco, base);
1110 }
1111
1112 /* Hugs used to let you add arbitrary primops with arbitrary types
1113  * just by editing Prelude.hs or any other file you wanted.
1114  * We deliberately avoided that approach because we wanted more
1115  * control over which primops are provided.
1116  */
1117 const AsmPrim asmPrimOps[] = {
1118
1119     /* Char# operations */
1120       { "primGtChar",                "CC", "B",  MONAD_Id, i_PRIMOP1, i_gtChar }
1121     , { "primGeChar",                "CC", "B",  MONAD_Id, i_PRIMOP1, i_geChar }
1122     , { "primEqChar",                "CC", "B",  MONAD_Id, i_PRIMOP1, i_eqChar }
1123     , { "primNeChar",                "CC", "B",  MONAD_Id, i_PRIMOP1, i_neChar }
1124     , { "primLtChar",                "CC", "B",  MONAD_Id, i_PRIMOP1, i_ltChar }
1125     , { "primLeChar",                "CC", "B",  MONAD_Id, i_PRIMOP1, i_leChar }
1126     , { "primCharToInt",             "C",  "I",  MONAD_Id, i_PRIMOP1, i_charToInt }
1127     , { "primIntToChar",             "I",  "C",  MONAD_Id, i_PRIMOP1, i_intToChar }
1128
1129     /* Int# operations */
1130     , { "primGtInt",                 "II", "B",  MONAD_Id, i_PRIMOP1, i_gtInt }
1131     , { "primGeInt",                 "II", "B",  MONAD_Id, i_PRIMOP1, i_geInt }
1132     , { "primEqInt",                 "II", "B",  MONAD_Id, i_PRIMOP1, i_eqInt }
1133     , { "primNeInt",                 "II", "B",  MONAD_Id, i_PRIMOP1, i_neInt }
1134     , { "primLtInt",                 "II", "B",  MONAD_Id, i_PRIMOP1, i_ltInt }
1135     , { "primLeInt",                 "II", "B",  MONAD_Id, i_PRIMOP1, i_leInt }
1136     , { "primMinInt",                "",   "I",  MONAD_Id, i_PRIMOP1, i_minInt }
1137     , { "primMaxInt",                "",   "I",  MONAD_Id, i_PRIMOP1, i_maxInt }
1138     , { "primPlusInt",               "II", "I",  MONAD_Id, i_PRIMOP1, i_plusInt }
1139     , { "primMinusInt",              "II", "I",  MONAD_Id, i_PRIMOP1, i_minusInt }
1140     , { "primTimesInt",              "II", "I",  MONAD_Id, i_PRIMOP1, i_timesInt }
1141     , { "primQuotInt",               "II", "I",  MONAD_Id, i_PRIMOP1, i_quotInt }
1142     , { "primRemInt",                "II", "I",  MONAD_Id, i_PRIMOP1, i_remInt }
1143     , { "primQuotRemInt",            "II", "II", MONAD_Id, i_PRIMOP1, i_quotRemInt }
1144     , { "primNegateInt",             "I",  "I",  MONAD_Id, i_PRIMOP1, i_negateInt }
1145
1146     , { "primAndInt",                "II", "I",  MONAD_Id, i_PRIMOP1, i_andInt }
1147     , { "primOrInt",                 "II", "I",  MONAD_Id, i_PRIMOP1, i_orInt }
1148     , { "primXorInt",                "II", "I",  MONAD_Id, i_PRIMOP1, i_xorInt }
1149     , { "primNotInt",                "I",  "I",  MONAD_Id, i_PRIMOP1, i_notInt }
1150     , { "primShiftLInt",             "II", "I",  MONAD_Id, i_PRIMOP1, i_shiftLInt }
1151     , { "primShiftRAInt",            "II", "I",  MONAD_Id, i_PRIMOP1, i_shiftRAInt }
1152     , { "primShiftRLInt",            "II", "I",  MONAD_Id, i_PRIMOP1, i_shiftRLInt }
1153
1154     /* Word# operations */
1155     , { "primGtWord",                "WW", "B",  MONAD_Id, i_PRIMOP1, i_gtWord }
1156     , { "primGeWord",                "WW", "B",  MONAD_Id, i_PRIMOP1, i_geWord }
1157     , { "primEqWord",                "WW", "B",  MONAD_Id, i_PRIMOP1, i_eqWord }
1158     , { "primNeWord",                "WW", "B",  MONAD_Id, i_PRIMOP1, i_neWord }
1159     , { "primLtWord",                "WW", "B",  MONAD_Id, i_PRIMOP1, i_ltWord }
1160     , { "primLeWord",                "WW", "B",  MONAD_Id, i_PRIMOP1, i_leWord }
1161     , { "primMinWord",               "",   "W",  MONAD_Id, i_PRIMOP1, i_minWord }
1162     , { "primMaxWord",               "",   "W",  MONAD_Id, i_PRIMOP1, i_maxWord }
1163     , { "primPlusWord",              "WW", "W",  MONAD_Id, i_PRIMOP1, i_plusWord }
1164     , { "primMinusWord",             "WW", "W",  MONAD_Id, i_PRIMOP1, i_minusWord }
1165     , { "primTimesWord",             "WW", "W",  MONAD_Id, i_PRIMOP1, i_timesWord }
1166     , { "primQuotWord",              "WW", "W",  MONAD_Id, i_PRIMOP1, i_quotWord }
1167     , { "primRemWord",               "WW", "W",  MONAD_Id, i_PRIMOP1, i_remWord }
1168     , { "primQuotRemWord",           "WW", "WW", MONAD_Id, i_PRIMOP1, i_quotRemWord }
1169     , { "primNegateWord",            "W",  "W",  MONAD_Id, i_PRIMOP1, i_negateWord }
1170
1171     , { "primAndWord",               "WW", "W",  MONAD_Id, i_PRIMOP1, i_andWord }
1172     , { "primOrWord",                "WW", "W",  MONAD_Id, i_PRIMOP1, i_orWord }
1173     , { "primXorWord",               "WW", "W",  MONAD_Id, i_PRIMOP1, i_xorWord }
1174     , { "primNotWord",               "W",  "W",  MONAD_Id, i_PRIMOP1, i_notWord }
1175     , { "primShiftLWord",            "WW", "W",  MONAD_Id, i_PRIMOP1, i_shiftLWord }
1176     , { "primShiftRAWord",           "WW", "W",  MONAD_Id, i_PRIMOP1, i_shiftRAWord }
1177     , { "primShiftRLWord",           "WW", "W",  MONAD_Id, i_PRIMOP1, i_shiftRLWord }
1178
1179     , { "primIntToWord",             "I",  "W",  MONAD_Id, i_PRIMOP1, i_intToWord }
1180     , { "primWordToInt",             "W",  "I",  MONAD_Id, i_PRIMOP1, i_wordToInt }
1181
1182     /* Addr# operations */
1183     , { "primGtAddr",                "AA", "B",  MONAD_Id, i_PRIMOP1, i_gtAddr }
1184     , { "primGeAddr",                "AA", "B",  MONAD_Id, i_PRIMOP1, i_geAddr }
1185     , { "primEqAddr",                "AA", "B",  MONAD_Id, i_PRIMOP1, i_eqAddr }
1186     , { "primNeAddr",                "AA", "B",  MONAD_Id, i_PRIMOP1, i_neAddr }
1187     , { "primLtAddr",                "AA", "B",  MONAD_Id, i_PRIMOP1, i_ltAddr }
1188     , { "primLeAddr",                "AA", "B",  MONAD_Id, i_PRIMOP1, i_leAddr }
1189     , { "primIntToAddr",             "I",  "A",  MONAD_Id, i_PRIMOP1, i_intToAddr }
1190     , { "primAddrToInt",             "A",  "I",  MONAD_Id, i_PRIMOP1, i_addrToInt }
1191
1192     , { "primIndexCharOffAddr",      "AI", "C",  MONAD_Id, i_PRIMOP1, i_indexCharOffAddr }
1193     , { "primIndexIntOffAddr",       "AI", "I",  MONAD_Id, i_PRIMOP1, i_indexIntOffAddr }
1194     , { "primIndexWordOffAddr",      "AI", "W",  MONAD_Id, i_PRIMOP1, i_indexWordOffAddr }
1195     , { "primIndexAddrOffAddr",      "AI", "A",  MONAD_Id, i_PRIMOP1, i_indexAddrOffAddr }
1196     , { "primIndexFloatOffAddr",     "AI", "F",  MONAD_Id, i_PRIMOP1, i_indexFloatOffAddr }
1197     , { "primIndexDoubleOffAddr",    "AI", "D",  MONAD_Id, i_PRIMOP1, i_indexDoubleOffAddr }
1198     , { "primIndexStableOffAddr",    "AI", "s",  MONAD_Id, i_PRIMOP1, i_indexStableOffAddr }
1199
1200     /* Stable# operations */
1201     , { "primIntToStablePtr",        "I",  "s",  MONAD_Id, i_PRIMOP1, i_intToStable }
1202     , { "primStablePtrToInt",        "s",  "I",  MONAD_Id, i_PRIMOP1, i_stableToInt }
1203
1204     /* These ops really ought to be in the IO monad */
1205     , { "primReadCharOffAddr",       "AI", "C",  MONAD_ST, i_PRIMOP1, i_readCharOffAddr }
1206     , { "primReadIntOffAddr",        "AI", "I",  MONAD_ST, i_PRIMOP1, i_readIntOffAddr }
1207     , { "primReadWordOffAddr",       "AI", "W",  MONAD_ST, i_PRIMOP1, i_readWordOffAddr }
1208     , { "primReadAddrOffAddr",       "AI", "A",  MONAD_ST, i_PRIMOP1, i_readAddrOffAddr }
1209     , { "primReadFloatOffAddr",      "AI", "F",  MONAD_ST, i_PRIMOP1, i_readFloatOffAddr }
1210     , { "primReadDoubleOffAddr",     "AI", "D",  MONAD_ST, i_PRIMOP1, i_readDoubleOffAddr }
1211     , { "primReadStableOffAddr",     "AI", "s",  MONAD_ST, i_PRIMOP1, i_readStableOffAddr }
1212
1213     /* These ops really ought to be in the IO monad */
1214     , { "primWriteCharOffAddr",      "AIC", "",  MONAD_ST, i_PRIMOP1, i_writeCharOffAddr }
1215     , { "primWriteIntOffAddr",       "AII", "",  MONAD_ST, i_PRIMOP1, i_writeIntOffAddr }
1216     , { "primWriteWordOffAddr",      "AIW", "",  MONAD_ST, i_PRIMOP1, i_writeWordOffAddr }
1217     , { "primWriteAddrOffAddr",      "AIA", "",  MONAD_ST, i_PRIMOP1, i_writeAddrOffAddr }
1218     , { "primWriteFloatOffAddr",     "AIF", "",  MONAD_ST, i_PRIMOP1, i_writeFloatOffAddr }
1219     , { "primWriteDoubleOffAddr",    "AID", "",  MONAD_ST, i_PRIMOP1, i_writeDoubleOffAddr }
1220     , { "primWriteStableOffAddr",    "AIs", "",  MONAD_ST, i_PRIMOP1, i_writeStableOffAddr }
1221
1222     /* Integer operations */
1223     , { "primCompareInteger",        "ZZ", "I",  MONAD_Id, i_PRIMOP1, i_compareInteger }
1224     , { "primNegateInteger",         "Z",  "Z",  MONAD_Id, i_PRIMOP1, i_negateInteger }
1225     , { "primPlusInteger",           "ZZ", "Z",  MONAD_Id, i_PRIMOP1, i_plusInteger }
1226     , { "primMinusInteger",          "ZZ", "Z",  MONAD_Id, i_PRIMOP1, i_minusInteger }
1227     , { "primTimesInteger",          "ZZ", "Z",  MONAD_Id, i_PRIMOP1, i_timesInteger }
1228     , { "primQuotRemInteger",        "ZZ", "ZZ", MONAD_Id, i_PRIMOP1, i_quotRemInteger }
1229     , { "primDivModInteger",         "ZZ", "ZZ", MONAD_Id, i_PRIMOP1, i_divModInteger }
1230     , { "primIntegerToInt",          "Z",  "I",  MONAD_Id, i_PRIMOP1, i_integerToInt }
1231     , { "primIntToInteger",          "I",  "Z",  MONAD_Id, i_PRIMOP1, i_intToInteger }
1232     , { "primIntegerToWord",         "Z",  "W",  MONAD_Id, i_PRIMOP1, i_integerToWord }
1233     , { "primWordToInteger",         "W",  "Z",  MONAD_Id, i_PRIMOP1, i_wordToInteger }
1234     , { "primIntegerToFloat",        "Z",  "F",  MONAD_Id, i_PRIMOP1, i_integerToFloat }
1235     , { "primFloatToInteger",        "F",  "Z",  MONAD_Id, i_PRIMOP1, i_floatToInteger }
1236     , { "primIntegerToDouble",       "Z",  "D",  MONAD_Id, i_PRIMOP1, i_integerToDouble }
1237     , { "primDoubleToInteger",       "D",  "Z",  MONAD_Id, i_PRIMOP1, i_doubleToInteger }
1238
1239     /* Float# operations */
1240     , { "primGtFloat",               "FF", "B",  MONAD_Id, i_PRIMOP1, i_gtFloat }
1241     , { "primGeFloat",               "FF", "B",  MONAD_Id, i_PRIMOP1, i_geFloat }
1242     , { "primEqFloat",               "FF", "B",  MONAD_Id, i_PRIMOP1, i_eqFloat }
1243     , { "primNeFloat",               "FF", "B",  MONAD_Id, i_PRIMOP1, i_neFloat }
1244     , { "primLtFloat",               "FF", "B",  MONAD_Id, i_PRIMOP1, i_ltFloat }
1245     , { "primLeFloat",               "FF", "B",  MONAD_Id, i_PRIMOP1, i_leFloat }
1246     , { "primMinFloat",              "",   "F",  MONAD_Id, i_PRIMOP1, i_minFloat }
1247     , { "primMaxFloat",              "",   "F",  MONAD_Id, i_PRIMOP1, i_maxFloat }
1248     , { "primRadixFloat",            "",   "I",  MONAD_Id, i_PRIMOP1, i_radixFloat }
1249     , { "primDigitsFloat",           "",   "I",  MONAD_Id, i_PRIMOP1, i_digitsFloat }
1250     , { "primMinExpFloat",           "",   "I",  MONAD_Id, i_PRIMOP1, i_minExpFloat }
1251     , { "primMaxExpFloat",           "",   "I",  MONAD_Id, i_PRIMOP1, i_maxExpFloat }
1252     , { "primPlusFloat",             "FF", "F",  MONAD_Id, i_PRIMOP1, i_plusFloat }
1253     , { "primMinusFloat",            "FF", "F",  MONAD_Id, i_PRIMOP1, i_minusFloat }
1254     , { "primTimesFloat",            "FF", "F",  MONAD_Id, i_PRIMOP1, i_timesFloat }
1255     , { "primDivideFloat",           "FF", "F",  MONAD_Id, i_PRIMOP1, i_divideFloat }
1256     , { "primNegateFloat",           "F",  "F",  MONAD_Id, i_PRIMOP1, i_negateFloat }
1257     , { "primFloatToInt",            "F",  "I",  MONAD_Id, i_PRIMOP1, i_floatToInt }
1258     , { "primIntToFloat",            "I",  "F",  MONAD_Id, i_PRIMOP1, i_intToFloat }
1259     , { "primExpFloat",              "F",  "F",  MONAD_Id, i_PRIMOP1, i_expFloat }
1260     , { "primLogFloat",              "F",  "F",  MONAD_Id, i_PRIMOP1, i_logFloat }
1261     , { "primSqrtFloat",             "F",  "F",  MONAD_Id, i_PRIMOP1, i_sqrtFloat }
1262     , { "primSinFloat",              "F",  "F",  MONAD_Id, i_PRIMOP1, i_sinFloat }
1263     , { "primCosFloat",              "F",  "F",  MONAD_Id, i_PRIMOP1, i_cosFloat }
1264     , { "primTanFloat",              "F",  "F",  MONAD_Id, i_PRIMOP1, i_tanFloat }
1265     , { "primAsinFloat",             "F",  "F",  MONAD_Id, i_PRIMOP1, i_asinFloat }
1266     , { "primAcosFloat",             "F",  "F",  MONAD_Id, i_PRIMOP1, i_acosFloat }
1267     , { "primAtanFloat",             "F",  "F",  MONAD_Id, i_PRIMOP1, i_atanFloat }
1268     , { "primSinhFloat",             "F",  "F",  MONAD_Id, i_PRIMOP1, i_sinhFloat }
1269     , { "primCoshFloat",             "F",  "F",  MONAD_Id, i_PRIMOP1, i_coshFloat }
1270     , { "primTanhFloat",             "F",  "F",  MONAD_Id, i_PRIMOP1, i_tanhFloat }
1271     , { "primPowerFloat",            "FF", "F",  MONAD_Id, i_PRIMOP1, i_powerFloat }
1272     , { "primDecodeFloatZ",          "F",  "ZI", MONAD_Id, i_PRIMOP1, i_decodeFloatZ }
1273     , { "primEncodeFloatZ",          "ZI", "F",  MONAD_Id, i_PRIMOP1, i_encodeFloatZ }
1274     , { "primIsNaNFloat",            "F",  "B",  MONAD_Id, i_PRIMOP1, i_isNaNFloat }
1275     , { "primIsInfiniteFloat",       "F",  "B",  MONAD_Id, i_PRIMOP1, i_isInfiniteFloat }
1276     , { "primIsDenormalizedFloat",   "F",  "B",  MONAD_Id, i_PRIMOP1, i_isDenormalizedFloat }
1277     , { "primIsNegativeZeroFloat",   "F",  "B",  MONAD_Id, i_PRIMOP1, i_isNegativeZeroFloat }
1278     , { "primIsIEEEFloat",           "",   "B",  MONAD_Id, i_PRIMOP1, i_isIEEEFloat }
1279
1280     /* Double# operations */
1281     , { "primGtDouble",              "DD", "B",  MONAD_Id, i_PRIMOP1, i_gtDouble }
1282     , { "primGeDouble",              "DD", "B",  MONAD_Id, i_PRIMOP1, i_geDouble }
1283     , { "primEqDouble",              "DD", "B",  MONAD_Id, i_PRIMOP1, i_eqDouble }
1284     , { "primNeDouble",              "DD", "B",  MONAD_Id, i_PRIMOP1, i_neDouble }
1285     , { "primLtDouble",              "DD", "B",  MONAD_Id, i_PRIMOP1, i_ltDouble }
1286     , { "primLeDouble",              "DD", "B",  MONAD_Id, i_PRIMOP1, i_leDouble }
1287     , { "primMinDouble",             "",   "D",  MONAD_Id, i_PRIMOP1, i_minDouble }
1288     , { "primMaxDouble",             "",   "D",  MONAD_Id, i_PRIMOP1, i_maxDouble }
1289     , { "primRadixDouble",           "",   "I",  MONAD_Id, i_PRIMOP1, i_radixDouble }
1290     , { "primDigitsDouble",          "",   "I",  MONAD_Id, i_PRIMOP1, i_digitsDouble }
1291     , { "primMinExpDouble",          "",   "I",  MONAD_Id, i_PRIMOP1, i_minExpDouble }
1292     , { "primMaxExpDouble",          "",   "I",  MONAD_Id, i_PRIMOP1, i_maxExpDouble }
1293     , { "primPlusDouble",            "DD", "D",  MONAD_Id, i_PRIMOP1, i_plusDouble }
1294     , { "primMinusDouble",           "DD", "D",  MONAD_Id, i_PRIMOP1, i_minusDouble }
1295     , { "primTimesDouble",           "DD", "D",  MONAD_Id, i_PRIMOP1, i_timesDouble }
1296     , { "primDivideDouble",          "DD", "D",  MONAD_Id, i_PRIMOP1, i_divideDouble }
1297     , { "primNegateDouble",          "D",  "D",  MONAD_Id, i_PRIMOP1, i_negateDouble }
1298     , { "primDoubleToInt",           "D",  "I",  MONAD_Id, i_PRIMOP1, i_doubleToInt }
1299     , { "primIntToDouble",           "I",  "D",  MONAD_Id, i_PRIMOP1, i_intToDouble }
1300     , { "primDoubleToFloat",         "D",  "F",  MONAD_Id, i_PRIMOP1, i_doubleToFloat }
1301     , { "primFloatToDouble",         "F",  "D",  MONAD_Id, i_PRIMOP1, i_floatToDouble }
1302     , { "primExpDouble",             "D",  "D",  MONAD_Id, i_PRIMOP1, i_expDouble }
1303     , { "primLogDouble",             "D",  "D",  MONAD_Id, i_PRIMOP1, i_logDouble }
1304     , { "primSqrtDouble",            "D",  "D",  MONAD_Id, i_PRIMOP1, i_sqrtDouble }
1305     , { "primSinDouble",             "D",  "D",  MONAD_Id, i_PRIMOP1, i_sinDouble }
1306     , { "primCosDouble",             "D",  "D",  MONAD_Id, i_PRIMOP1, i_cosDouble }
1307     , { "primTanDouble",             "D",  "D",  MONAD_Id, i_PRIMOP1, i_tanDouble }
1308     , { "primAsinDouble",            "D",  "D",  MONAD_Id, i_PRIMOP1, i_asinDouble }
1309     , { "primAcosDouble",            "D",  "D",  MONAD_Id, i_PRIMOP1, i_acosDouble }
1310     , { "primAtanDouble",            "D",  "D",  MONAD_Id, i_PRIMOP1, i_atanDouble }
1311     , { "primSinhDouble",            "D",  "D",  MONAD_Id, i_PRIMOP1, i_sinhDouble }
1312     , { "primCoshDouble",            "D",  "D",  MONAD_Id, i_PRIMOP1, i_coshDouble }
1313     , { "primTanhDouble",            "D",  "D",  MONAD_Id, i_PRIMOP1, i_tanhDouble }
1314     , { "primPowerDouble",           "DD", "D",  MONAD_Id, i_PRIMOP1, i_powerDouble }
1315     , { "primDecodeDoubleZ",         "D",  "ZI", MONAD_Id, i_PRIMOP1, i_decodeDoubleZ }
1316     , { "primEncodeDoubleZ",         "ZI",  "D", MONAD_Id, i_PRIMOP1, i_encodeDoubleZ }
1317     , { "primIsNaNDouble",           "D",  "B",  MONAD_Id, i_PRIMOP1, i_isNaNDouble }
1318     , { "primIsInfiniteDouble",      "D",  "B",  MONAD_Id, i_PRIMOP1, i_isInfiniteDouble }
1319     , { "primIsDenormalizedDouble",  "D",  "B",  MONAD_Id, i_PRIMOP1, i_isDenormalizedDouble }
1320     , { "primIsNegativeZeroDouble",  "D",  "B",  MONAD_Id, i_PRIMOP1, i_isNegativeZeroDouble }
1321     , { "primIsIEEEDouble",          "",   "B",  MONAD_Id, i_PRIMOP1, i_isIEEEDouble }
1322
1323     /* Ref operations */
1324     , { "primNewRef",                "a",  "R",  MONAD_ST, i_PRIMOP2, i_newRef }
1325     , { "primWriteRef",              "Ra", "",   MONAD_ST, i_PRIMOP2, i_writeRef }
1326     , { "primReadRef",               "R",  "a",  MONAD_ST, i_PRIMOP2, i_readRef }
1327     , { "primSameRef",               "RR", "B",  MONAD_Id, i_PRIMOP2, i_sameRef }
1328
1329     /* PrimArray operations */
1330     , { "primSameMutableArray",      "MM",  "B", MONAD_Id, i_PRIMOP2, i_sameMutableArray }
1331     , { "primUnsafeFreezeArray",     "M",   "X", MONAD_ST, i_PRIMOP2, i_unsafeFreezeArray }
1332     , { "primNewArray",              "Ia",  "M", MONAD_ST, i_PRIMOP2, i_newArray }
1333     , { "primWriteArray",            "MIa", "",  MONAD_ST, i_PRIMOP2, i_writeArray }
1334     , { "primReadArray",             "MI",  "a", MONAD_ST, i_PRIMOP2, i_readArray }
1335     , { "primIndexArray",            "XI",  "a", MONAD_Id, i_PRIMOP2, i_indexArray }
1336     , { "primSizeArray",             "X",   "I", MONAD_Id, i_PRIMOP2, i_sizeArray }
1337     , { "primSizeMutableArray",      "M",   "I", MONAD_Id, i_PRIMOP2, i_sizeMutableArray }
1338
1339     /* Prim[Mutable]ByteArray operations */
1340     , { "primSameMutableByteArray",  "mm", "B", MONAD_Id, i_PRIMOP2, i_sameMutableByteArray }
1341     , { "primUnsafeFreezeByteArray", "m",  "x", MONAD_ST, i_PRIMOP2, i_unsafeFreezeByteArray }
1342     
1343     , { "primNewByteArray",          "I",  "m", MONAD_ST, i_PRIMOP2, i_newByteArray }
1344
1345     , { "primWriteCharArray",        "mIC", "", MONAD_ST, i_PRIMOP2, i_writeCharArray }
1346     , { "primReadCharArray",         "mI", "C", MONAD_ST, i_PRIMOP2, i_readCharArray }
1347     , { "primIndexCharArray",        "xI", "C", MONAD_Id, i_PRIMOP2, i_indexCharArray }
1348     
1349     , { "primWriteIntArray",         "mII", "",  MONAD_ST, i_PRIMOP2, i_writeIntArray }
1350     , { "primReadIntArray",          "mI",  "I", MONAD_ST, i_PRIMOP2, i_readIntArray }
1351     , { "primIndexIntArray",         "xI",  "I", MONAD_Id, i_PRIMOP2, i_indexIntArray }
1352
1353     /* {new,write,read,index}IntegerArray not provided */
1354
1355     , { "primWriteWordArray",        "mIW", "",  MONAD_ST, i_PRIMOP2, i_writeWordArray }
1356     , { "primReadWordArray",         "mI",  "W", MONAD_ST, i_PRIMOP2, i_readWordArray }
1357     , { "primIndexWordArray",        "xI",  "W", MONAD_Id, i_PRIMOP2, i_indexWordArray }
1358     , { "primWriteAddrArray",        "mIA", "",  MONAD_ST, i_PRIMOP2, i_writeAddrArray }
1359     , { "primReadAddrArray",         "mI",  "A", MONAD_ST, i_PRIMOP2, i_readAddrArray }
1360     , { "primIndexAddrArray",        "xI",  "A", MONAD_Id, i_PRIMOP2, i_indexAddrArray }
1361     , { "primWriteFloatArray",       "mIF", "",  MONAD_ST, i_PRIMOP2, i_writeFloatArray }
1362     , { "primReadFloatArray",        "mI",  "F", MONAD_ST, i_PRIMOP2, i_readFloatArray }
1363     , { "primIndexFloatArray",       "xI",  "F", MONAD_Id, i_PRIMOP2, i_indexFloatArray }
1364     , { "primWriteDoubleArray" ,     "mID", "",  MONAD_ST, i_PRIMOP2, i_writeDoubleArray }
1365     , { "primReadDoubleArray",       "mI",  "D", MONAD_ST, i_PRIMOP2, i_readDoubleArray }
1366     , { "primIndexDoubleArray",      "xI",  "D", MONAD_Id, i_PRIMOP2, i_indexDoubleArray }
1367
1368 #if 0
1369 #ifdef PROVIDE_STABLE
1370     , { "primWriteStableArray",      "mIs", "",  MONAD_ST, i_PRIMOP2, i_writeStableArray }
1371     , { "primReadStableArray",       "mI",  "s", MONAD_ST, i_PRIMOP2, i_readStableArray }
1372     , { "primIndexStableArray",      "xI",  "s", MONAD_Id, i_PRIMOP2, i_indexStableArray }
1373 #endif
1374 #endif
1375     /* {new,write,read,index}ForeignObjArray not provided */
1376
1377
1378 #ifdef PROVIDE_FOREIGN
1379     /* ForeignObj# operations */
1380     , { "primMakeForeignObj",        "A",  "f",  MONAD_IO, i_PRIMOP2, i_makeForeignObj }
1381 #endif
1382 #ifdef PROVIDE_WEAK
1383     /* WeakPair# operations */
1384     , { "primMakeWeak",              "bac", "w",  MONAD_IO, i_PRIMOP2, i_makeWeak }
1385     , { "primDeRefWeak",             "w",   "Ia", MONAD_IO, i_PRIMOP2, i_deRefWeak }
1386 #endif
1387     /* StablePtr# operations */
1388     , { "primMakeStablePtr",         "a", "s",   MONAD_IO, i_PRIMOP2, i_makeStablePtr }
1389     , { "primDeRefStablePtr",        "s", "a",   MONAD_IO, i_PRIMOP2, i_deRefStablePtr }
1390     , { "primFreeStablePtr",         "s", "",    MONAD_IO, i_PRIMOP2, i_freeStablePtr }
1391
1392     /* foreign export dynamic support */
1393     , { "primCreateAdjThunkARCH",    "sAC","A",  MONAD_IO, i_PRIMOP2, i_createAdjThunkARCH }
1394
1395     /* misc handy hacks */
1396     , { "primGetArgc",               "",   "I",  MONAD_IO, i_PRIMOP2, i_getArgc }
1397     , { "primGetArgv",               "I",  "A",  MONAD_IO, i_PRIMOP2, i_getArgv }
1398
1399 #ifdef PROVIDE_PTREQUALITY
1400     , { "primReallyUnsafePtrEquality", "aa", "B",MONAD_Id, i_PRIMOP2, i_reallyUnsafePtrEquality }
1401 #endif
1402 #ifdef PROVIDE_COERCE
1403     , { "primUnsafeCoerce",          "a", "b",   MONAD_Id, i_PRIMOP2, i_unsafeCoerce }
1404 #endif
1405 #ifdef PROVIDE_CONCURRENT
1406     /* Concurrency operations */
1407     , { "primFork",                  "a", "T",   MONAD_IO, i_PRIMOP2, i_fork }
1408     , { "primKillThread",            "T", "",    MONAD_IO, i_PRIMOP2, i_killThread }
1409     , { "primSameMVar",              "rr", "B",  MONAD_Id, i_PRIMOP2, i_sameMVar }
1410     , { "primDelay",                 "I", "",    MONAD_IO, i_PRIMOP2, i_delay }
1411     , { "primWaitRead",              "I", "",    MONAD_IO, i_PRIMOP2, i_waitRead }
1412     , { "primWaitWrite",             "I", "",    MONAD_IO, i_PRIMOP2, i_waitWrite }
1413 #endif
1414     , { "primNewMVar",               "",  "r",   MONAD_IO, i_PRIMOP2, i_newMVar }
1415       /* primTakeMVar is handwritten bytecode */
1416     , { "primPutMVar",               "ra", "",   MONAD_IO, i_PRIMOP2, i_putMVar } 
1417
1418     /* Ccall is polyadic - so it's excluded from this table */
1419
1420     , { 0,0,0,0,0,0 }
1421 };
1422
1423 const AsmPrim ccall_ccall_Id
1424    = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_ccall_Id };
1425 const AsmPrim ccall_ccall_IO
1426    = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_ccall_IO };
1427 const AsmPrim ccall_stdcall_Id 
1428    = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_stdcall_Id };
1429 const AsmPrim ccall_stdcall_IO 
1430    = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_stdcall_IO };
1431
1432
1433 const AsmPrim* asmFindPrim( char* s )
1434 {
1435     int i;
1436     for (i=0; asmPrimOps[i].name; ++i) {
1437         if (strcmp(s,asmPrimOps[i].name)==0) {
1438             return &asmPrimOps[i];
1439         }
1440     }
1441     return 0;
1442 }
1443
1444 const AsmPrim* asmFindPrimop( AsmInstr prefix, AsmInstr op )
1445 {
1446     nat i;
1447     for (i=0; asmPrimOps[i].name; ++i) {
1448         if (asmPrimOps[i].prefix == prefix && asmPrimOps[i].opcode == op) {
1449             return &asmPrimOps[i];
1450         }
1451     }
1452     return 0;
1453 }
1454
1455 /* --------------------------------------------------------------------------
1456  * Handwritten primops
1457  * ------------------------------------------------------------------------*/
1458
1459 AsmBCO asm_BCO_catch ( void )
1460 {
1461    AsmBCO bco = asmBeginBCO(0 /*NIL*/);
1462    emiti_8(bco,i_ARG_CHECK,2);
1463    emiti_8(bco,i_PRIMOP1,i_pushcatchframe);
1464    incSp(bco, (1-2)*sizeofW(StgPtr) + sizeofW(StgCatchFrame));
1465    emiti_(bco,i_ENTER);
1466    decSp(bco, sizeofW(StgPtr));
1467    asmEndBCO(bco);
1468    return bco;
1469 }
1470
1471 AsmBCO asm_BCO_raise ( void )
1472 {
1473    AsmBCO bco = asmBeginBCO(0 /*NIL*/);
1474    emiti_8(bco,i_ARG_CHECK,1);
1475    emiti_8(bco,i_PRIMOP2,i_raise);
1476    decSp(bco,sizeofW(StgPtr));
1477    asmEndBCO(bco);
1478    return bco;
1479 }
1480
1481 AsmBCO asm_BCO_seq ( void )
1482 {
1483    AsmBCO eval, cont;
1484
1485    cont = asmBeginBCO(0 /*NIL*/);
1486    emiti_8(cont,i_ARG_CHECK,2);   /* should never fail */
1487    emit_i_VAR(cont,1);
1488    emit_i_SLIDE(cont,1,2);
1489    emiti_(cont,i_ENTER);
1490    incSp(cont, 3*sizeofW(StgPtr));
1491    asmEndBCO(cont);
1492
1493    eval = asmBeginBCO(0 /*NIL*/);
1494    emiti_8(eval,i_ARG_CHECK,2);
1495    emit_i_RETADDR(eval,eval->object.ptrs.len);
1496    asmPtr(eval,&(cont->object));
1497    emit_i_VAR(eval,2);
1498    emit_i_SLIDE(eval,3,1);
1499    emiti_8(eval,i_PRIMOP1,i_pushseqframe);
1500    emiti_(eval,i_ENTER);
1501    incSp(eval, sizeofW(StgSeqFrame) + 4*sizeofW(StgPtr));
1502    asmEndBCO(eval);
1503
1504    return eval;
1505 }
1506
1507 AsmBCO asm_BCO_takeMVar ( void )
1508 {
1509    AsmBCO kase, casecont, take;
1510
1511    take = asmBeginBCO(0 /*NIL*/);
1512    emit_i_VAR(take,0);
1513    emiti_8(take,i_PRIMOP2,i_takeMVar);
1514    emit_i_VAR(take,3);
1515    emit_i_VAR(take,1);
1516    emit_i_VAR(take,4);
1517    emit_i_SLIDE(take,3,4);
1518    emiti_(take,i_ENTER);
1519    incSp(take,20);
1520    asmEndBCO(take);
1521
1522    casecont = asmBeginBCO(0 /*NIL*/);
1523    emiti_(casecont,i_UNPACK);
1524    emit_i_VAR(casecont,4);
1525    emit_i_VAR(casecont,4);
1526    emit_i_VAR(casecont,2);
1527    emit_i_CONST(casecont,casecont->object.ptrs.len);
1528    asmPtr(casecont,&(take->object));
1529    emit_i_SLIDE(casecont,4,5);
1530    emiti_(casecont,i_ENTER);
1531    incSp(casecont,20);
1532    asmEndBCO(casecont);
1533
1534    kase = asmBeginBCO(0 /*NIL*/);
1535    emiti_8(kase,i_ARG_CHECK,3);
1536    emit_i_RETADDR(kase,kase->object.ptrs.len);
1537    asmPtr(kase,&(casecont->object));
1538    emit_i_VAR(kase,2);
1539    emiti_(kase,i_ENTER);
1540    incSp(kase,20);
1541    asmEndBCO(kase);
1542
1543    return kase;
1544 }
1545
1546
1547 /* --------------------------------------------------------------------------
1548  * Heap manipulation
1549  * ------------------------------------------------------------------------*/
1550
1551 AsmVar asmAllocCONSTR   ( AsmBCO bco, AsmInfo info )
1552 {
1553     ASSERT( sizeW_fromITBL(info) >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
1554     emiti_8(bco,i_ALLOC_CONSTR,bco->nps.len);
1555     asmWords(bco,AsmInfo,info);
1556     incSp(bco, sizeofW(StgClosurePtr));
1557     grabHpNonUpd(bco,sizeW_fromITBL(info));
1558     return bco->sp;
1559 }
1560
1561 AsmSp asmBeginPack( AsmBCO bco )
1562 {
1563     return bco->sp;
1564 }
1565
1566 void asmEndPack( AsmBCO bco, AsmVar v, AsmSp start, AsmInfo info )
1567 {
1568     nat size = bco->sp - start;
1569     assert(bco->sp >= start);
1570     assert(start >= v);
1571     /* only reason to include info is for this assertion */
1572     assert(info->layout.payload.ptrs == size);
1573     emit_i_PACK(bco, bco->sp - v);
1574     setSp(bco, start);
1575 }
1576
1577 void asmBeginUnpack( AsmBCO bco )
1578 {
1579     /* dummy to make it look prettier */
1580 }
1581
1582 void asmEndUnpack( AsmBCO bco )
1583 {
1584     emiti_(bco,i_UNPACK);
1585 }
1586
1587 AsmVar asmAllocAP( AsmBCO bco, AsmNat words )
1588 {
1589     emiti_8(bco,i_ALLOC_AP,words);
1590     incSp(bco, sizeofW(StgPtr));
1591     grabHpUpd(bco,AP_sizeW(words));
1592     return bco->sp;
1593 }
1594
1595 AsmSp asmBeginMkAP( AsmBCO bco )
1596 {
1597     return bco->sp;
1598 }
1599
1600 void asmEndMkAP( AsmBCO bco, AsmVar v, AsmSp start )
1601 {
1602     emit_i_MKAP(bco,bco->sp-v,bco->sp-start-1);
1603             /* -1 because fun isn't counted */
1604     setSp(bco, start);
1605 }
1606
1607 AsmVar asmAllocPAP( AsmBCO bco, AsmNat size )
1608 {
1609     emiti_8(bco,i_ALLOC_PAP,size);
1610     incSp(bco, sizeofW(StgPtr));
1611     return bco->sp;
1612 }
1613
1614 AsmSp asmBeginMkPAP( AsmBCO bco )
1615 {
1616     return bco->sp;
1617 }
1618
1619 void asmEndMkPAP( AsmBCO bco, AsmVar v, AsmSp start )
1620 {
1621     emiti_8_8(bco,i_MKPAP,bco->sp-v,bco->sp-start-1);
1622             /* -1 because fun isn't counted */
1623     setSp(bco, start);
1624 }
1625
1626 AsmVar asmClosure( AsmBCO bco, AsmObject p )
1627 {
1628     emit_i_CONST(bco,bco->object.ptrs.len);
1629     asmPtr(bco,p);
1630     incSp(bco, sizeofW(StgPtr));
1631     return bco->sp;
1632 }
1633
1634 AsmVar asmGHCClosure( AsmBCO bco, AsmObject p )
1635 {
1636     // A complete hack.  Pushes the address as a tagged int
1637     // and then uses SLIDE to get rid of the tag.  Appalling.
1638     asmConstInt(bco, (AsmInt)p);
1639     emit_i_SLIDE(bco,0,1); decSp(bco,1);
1640     return bco->sp;
1641 }
1642
1643
1644 /* --------------------------------------------------------------------------
1645  * Building InfoTables
1646  * ------------------------------------------------------------------------*/
1647
1648 AsmInfo asmMkInfo( AsmNat tag, AsmNat ptrs )
1649 {
1650     StgInfoTable* info = stgMallocBytes( sizeof(StgInfoTable),"asmMkInfo");
1651     /* Note: the evaluator automatically pads objects with the right number
1652      * of non-ptrs to satisfy MIN_NONUPD_SIZE restrictions.
1653      */
1654     AsmNat nptrs = stg_max(0,MIN_NONUPD_SIZE-ptrs);
1655
1656     /* initialisation code based on INFO_TABLE_CONSTR */
1657     info->layout.payload.ptrs  = ptrs;
1658     info->layout.payload.nptrs = nptrs;
1659     info->srt_len = tag;
1660     info->type    = CONSTR;
1661 #ifdef USE_MINIINTERPRETER
1662     info->entry   = stgCast(StgFunPtr,&Hugs_CONSTR_entry);
1663 #else
1664 #warning asmMkInfo: Need to insert entry code in some cunning way
1665 #endif
1666     ASSERT( sizeW_fromITBL(info) >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
1667     return info;
1668 }
1669
1670 /*-------------------------------------------------------------------------*/
1671
1672 #endif /* INTERPRETER */
1673