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