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