2 /* --------------------------------------------------------------------------
5 * Copyright (c) 1994-1998.
7 * $RCSfile: Assembler.c,v $
9 * $Date: 1999/12/07 11:22:56 $
11 * This module provides functions to construct BCOs and other closures
12 * required by the bytecode compiler.
14 * It is supposed to shield the compiler from platform dependent information
20 * and from details of how the abstract machine is implemented such as:
22 * o what does a BCO look like?
23 * o how many bytes does the "Push InfoTable" instruction require?
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.
41 * ------------------------------------------------------------------------*/
49 #include "Bytecodes.h"
51 #include "Disassembler.h"
52 #include "StgMiscClosures.h"
55 #include "Evaluator.h"
57 #define INSIDE_ASSEMBLER_C
58 #include "Assembler.h"
59 #undef INSIDE_ASSEMBLER_C
61 /* --------------------------------------------------------------------------
62 * References between BCOs
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.
68 * ToDo: generalise to allow references between any objects
69 * ------------------------------------------------------------------------*/
72 AsmObject ref; /* who refers to it */
73 AsmNat i; /* index into some table held by referer */
76 /* --------------------------------------------------------------------------
77 * Queues (of instructions, ptrs, nonptrs)
78 * ------------------------------------------------------------------------*/
83 #include "QueueTemplate.h"
89 #define Type AsmObject
91 #include "QueueTemplate.h"
99 #include "QueueTemplate.h"
104 #define Queue NonPtrs
106 #define MAKE_findIn 1
107 #include "QueueTemplate.h"
112 /* --------------------------------------------------------------------------
113 * AsmObjects are used to build heap objects.
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
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.
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 ... */
132 AsmNat num_unresolved; /* number of unfilled references */
133 StgClosure* closure; /* where object was allocated */
137 struct AsmObject_ object; /* must be first in struct */
143 struct AsmObject_ object; /* must be first in struct */
147 struct AsmObject_ object; /* must be first in struct */
152 int /*StgExpr*/ stgexpr;
154 /* abstract machine ("executed" during compilation) */
155 AsmSp sp; /* stack ptr */
160 static void asmResolveRef( AsmObject obj, AsmNat i, AsmClosure reference )
162 ASSERT(obj->closure);
163 switch (get_itbl(obj->closure)->type) {
166 StgBCO* bco = stgCast(StgBCO*,obj->closure);
167 ASSERT(i < bco->n_ptrs && bcoConstPtr(bco,i) == NULL);
168 bcoConstCPtr(bco,i) = reference;
173 StgCAF* caf = stgCast(StgCAF*,obj->closure);
174 ASSERT(i == 0 && caf->body == NULL);
175 caf->body = reference;
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;
187 StgAP_UPD* ap = stgCast(StgAP_UPD*,obj->closure);
188 ASSERT(i < 1+ap->n_args);
190 ASSERT(ap->fun == NULL);
193 ASSERT(payloadCPtr(ap,i-1) == NULL);
194 payloadCPtr(ap,i-1) = reference;
199 barf("asmResolveRef");
201 obj->num_unresolved -= 1;
204 static void asmAddRef( AsmObject referent, AsmObject referer, AsmNat i )
206 if (referent->closure) {
207 asmResolveRef(referer,i,(AsmClosure)referent->closure);
209 insertRefs(&(referent->refs),(AsmRef){referer,i});
213 void asmAddPtr( AsmObject obj, AsmObject arg )
215 ASSERT(obj->closure == 0); /* can't extend an object once it's allocated */
216 insertPtrs( &obj->ptrs, arg );
219 static void asmBeginObject( AsmObject obj )
222 obj->num_unresolved = 0;
223 initRefs(&obj->refs);
224 initPtrs(&obj->ptrs);
227 static void asmEndObject( AsmObject obj, StgClosure* c )
229 obj->num_unresolved = obj->ptrs.len;
231 mapQueue(Ptrs, AsmObject, obj->ptrs, asmAddRef(x,obj,i));
232 mapQueue(Refs, AsmRef, obj->refs, asmResolveRef(x.ref,x.i,c));
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));
242 int asmObjectHasClosure ( AsmObject obj )
244 return (obj->num_unresolved == 0 && obj->closure);
247 AsmClosure asmClosureOfObject ( AsmObject obj )
249 ASSERT(asmObjectHasClosure(obj));
253 void asmMarkObject ( AsmObject obj )
255 ASSERT(obj->num_unresolved == 0 && obj->closure);
256 obj->closure = MarkRoot(obj->closure);
259 /* --------------------------------------------------------------------------
261 * ------------------------------------------------------------------------*/
263 static StgClosure* asmAlloc( nat size )
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); */
271 static void setSp( AsmBCO bco, AsmSp sp )
273 bco->max_sp = stg_max(bco->sp,bco->max_sp);
275 bco->max_sp = stg_max(bco->sp,bco->max_sp);
278 static void incSp ( AsmBCO bco, int sp_delta )
280 bco->max_sp = stg_max(bco->sp,bco->max_sp);
282 bco->max_sp = stg_max(bco->sp,bco->max_sp);
285 static void decSp ( AsmBCO bco, int sp_delta )
287 bco->max_sp = stg_max(bco->sp,bco->max_sp);
289 bco->max_sp = stg_max(bco->sp,bco->max_sp);
292 /* --------------------------------------------------------------------------
294 * ------------------------------------------------------------------------*/
296 AsmObject asmMkObject( AsmClosure c )
298 AsmObject obj = malloc(sizeof(struct AsmObject_));
300 barf("Can't allocate AsmObject");
307 AsmCon asmBeginCon( AsmInfo info )
309 AsmCon con = malloc(sizeof(struct AsmCon_));
311 barf("Can't allocate AsmCon");
313 asmBeginObject(&con->object);
318 void asmEndCon( AsmCon con )
320 nat p = con->object.ptrs.len;
321 nat np = stg_max(0,MIN_NONUPD_SIZE-p);
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);
331 AsmCAF asmBeginCAF( void )
333 AsmCAF caf = malloc(sizeof(struct AsmCAF_));
335 barf("Can't allocate AsmCAF");
337 asmBeginObject(&caf->object);
341 void asmEndCAF( AsmCAF caf, AsmBCO body )
343 StgClosure* c = asmAlloc(CAF_sizeW());
344 StgCAF* o = stgCast(StgCAF*,c);
345 SET_HDR(o,&CAF_UNENTERED_info,??);
347 o->value = stgCast(StgClosure*,0xdeadbeef);
348 o->link = stgCast(StgCAF*,0xdeadbeef);
350 asmAddPtr(&caf->object,&body->object);
351 asmEndObject(&caf->object,c);
354 AsmBCO asmBeginBCO( int /*StgExpr*/ e )
356 AsmBCO bco = malloc(sizeof(struct AsmBCO_));
358 barf("Can't allocate AsmBCO");
360 asmBeginObject(&bco->object);
361 initInstrs(&bco->is);
362 initNonPtrs(&bco->nps);
365 bco->max_sp = bco->sp = 0;
366 bco->lastOpc = i_INTERNAL_ERROR;
370 void asmEndBCO( AsmBCO bco )
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 */
376 StgClosure* c = asmAlloc(BCO_sizeW(p,np,is));
377 StgBCO* o = stgCast(StgBCO*,c);
378 SET_HDR(o,&BCO_info,??);
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);
387 bco->max_sp = stg_max(bco->sp,bco->max_sp);
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;
394 bcoInstr(o,j++) = i_STK_CHECK_big;
395 bcoInstr(o,j++) = bco->max_sp / 256;
396 bcoInstr(o,j++) = bco->max_sp % 256;
399 mapQueue(Instrs, StgWord8, bco->is, bcoInstr(o,j++) = x);
402 freeInstrs(&bco->is);
403 freeNonPtrs(&bco->nps);
404 asmEndObject(&bco->object,c);
407 /* --------------------------------------------------------------------------
409 * ------------------------------------------------------------------------*/
411 static void asmInstrOp ( AsmBCO bco, StgWord i )
413 ASSERT(i <= BIGGEST_OPCODE); /* must be a valid opcode */
415 insertInstrs(&(bco->is),i);
418 static void asmInstr8 ( AsmBCO bco, StgWord i )
421 ASSERT(i < 256); /* must be a byte */
423 insertInstrs(&(bco->is),i);
426 static void asmInstr16 ( AsmBCO bco, StgWord i )
428 ASSERT(i < 65536); /* must be a short */
429 insertInstrs(&(bco->is),i / 256);
430 insertInstrs(&(bco->is),i % 256);
433 static Instr asmInstrBack ( AsmBCO bco, StgWord n )
435 return bco->is.elems[bco->is.len - n];
438 static void asmInstrRecede ( AsmBCO bco, StgWord n )
440 if (bco->is.len < n) barf("asmInstrRecede");
444 static void asmPtr( AsmBCO bco, AsmObject x )
446 insertPtrs( &bco->object.ptrs, x );
449 static void asmWord( AsmBCO bco, StgWord i )
451 insertNonPtrs( &bco->nps, i );
454 static int asmFindInNonPtrs ( AsmBCO bco, StgWord i )
456 return findInNonPtrs ( &bco->nps, i );
459 #define asmWords(bco,ty,x) \
461 union { ty a; AsmWord b[sizeofW(ty)]; } p; \
463 if (sizeof(ty) < sizeof(AsmWord)) p.b[0]=0; \
465 for( i = 0; i < sizeofW(ty); i++ ) { \
466 asmWord(bco,p.b[i]); \
470 static StgWord repSizeW( AsmRep rep )
473 case CHAR_REP: return sizeofW(StgWord) + sizeofW(StgChar);
476 case INT_REP: return sizeofW(StgWord) + sizeofW(StgInt);
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);
488 #ifdef PROVIDE_FOREIGN
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);
505 case VOID_REP: return sizeofW(StgWord);
506 default: barf("repSizeW %d",rep);
511 int asmRepSizeW ( AsmRep rep )
513 return repSizeW ( rep );
517 /* --------------------------------------------------------------------------
518 * Instruction emission. All instructions should be routed through here
519 * so that the peephole optimiser gets to see what's happening.
520 * ------------------------------------------------------------------------*/
522 static void emiti_ ( AsmBCO bco, Instr opcode )
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);
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);
543 asmInstrOp(bco,opcode);
547 static void emiti_8 ( AsmBCO bco, Instr opcode, int arg1 )
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);
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);
564 asmInstrOp(bco,opcode);
569 static void emiti_16 ( AsmBCO bco, Instr opcode, int arg1 )
571 asmInstrOp(bco,opcode);
572 asmInstr16(bco,arg1);
575 static void emiti_8_8 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
577 asmInstrOp(bco,opcode);
582 static void emiti_8_16 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
584 asmInstrOp(bco,opcode);
586 asmInstr16(bco,arg2);
589 static void emiti_16_16 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
591 asmInstrOp(bco,opcode);
592 asmInstr16(bco,arg1);
593 asmInstr16(bco,arg2);
597 /* --------------------------------------------------------------------------
598 * Wrappers around the above fns
599 * ------------------------------------------------------------------------*/
601 static void emit_i_VAR_INT ( AsmBCO bco, int arg1 )
605 emiti_8 (bco,i_VAR_INT, arg1); else
606 emiti_16(bco,i_VAR_INT_big,arg1);
609 static void emit_i_VAR_WORD ( AsmBCO bco, int arg1 )
613 emiti_8 (bco,i_VAR_WORD, arg1); else
614 emiti_16(bco,i_VAR_WORD_big,arg1);
617 static void emit_i_VAR_ADDR ( AsmBCO bco, int arg1 )
621 emiti_8 (bco,i_VAR_ADDR, arg1); else
622 emiti_16(bco,i_VAR_ADDR_big,arg1);
625 static void emit_i_VAR_CHAR ( AsmBCO bco, int arg1 )
629 emiti_8 (bco,i_VAR_CHAR, arg1); else
630 emiti_16(bco,i_VAR_CHAR_big,arg1);
633 static void emit_i_VAR_FLOAT ( AsmBCO bco, int arg1 )
637 emiti_8 (bco,i_VAR_FLOAT, arg1); else
638 emiti_16(bco,i_VAR_FLOAT_big,arg1);
641 static void emit_i_VAR_DOUBLE ( AsmBCO bco, int arg1 )
645 emiti_8 (bco,i_VAR_DOUBLE, arg1); else
646 emiti_16(bco,i_VAR_DOUBLE_big,arg1);
649 static void emit_i_VAR_STABLE ( AsmBCO bco, int arg1 )
653 emiti_8 (bco,i_VAR_STABLE, arg1); else
654 emiti_16(bco,i_VAR_STABLE_big,arg1);
657 static void emit_i_VAR ( AsmBCO bco, int arg1 )
661 emiti_8 (bco,i_VAR, arg1); else
662 emiti_16(bco,i_VAR_big,arg1);
665 static void emit_i_PACK ( AsmBCO bco, int arg1 )
669 emiti_8 (bco,i_PACK, arg1); else
670 emiti_16(bco,i_PACK_big,arg1);
673 static void emit_i_SLIDE ( AsmBCO bco, int arg1, int arg2 )
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);
682 static void emit_i_MKAP ( AsmBCO bco, int arg1, int arg2 )
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);
692 static void emit_i_CONST_INT ( AsmBCO bco, int arg1 )
696 emiti_8 (bco,i_CONST_INT, arg1); else
697 emiti_16(bco,i_CONST_INT_big,arg1);
700 static void emit_i_CONST_INTEGER ( AsmBCO bco, int arg1 )
704 emiti_8 (bco,i_CONST_INTEGER, arg1); else
705 emiti_16(bco,i_CONST_INTEGER_big,arg1);
708 static void emit_i_CONST_ADDR ( AsmBCO bco, int arg1 )
712 emiti_8 (bco,i_CONST_ADDR, arg1); else
713 emiti_16(bco,i_CONST_ADDR_big,arg1);
716 static void emit_i_CONST_CHAR ( AsmBCO bco, int arg1 )
720 emiti_8 (bco,i_CONST_CHAR, arg1); else
721 emiti_16(bco,i_CONST_CHAR_big,arg1);
724 static void emit_i_CONST_FLOAT ( AsmBCO bco, int arg1 )
728 emiti_8 (bco,i_CONST_FLOAT, arg1); else
729 emiti_16(bco,i_CONST_FLOAT_big,arg1);
732 static void emit_i_CONST_DOUBLE ( AsmBCO bco, int arg1 )
736 emiti_8 (bco,i_CONST_DOUBLE, arg1); else
737 emiti_16(bco,i_CONST_DOUBLE_big,arg1);
740 static void emit_i_CONST ( AsmBCO bco, int arg1 )
744 emiti_8 (bco,i_CONST, arg1); else
745 emiti_16(bco,i_CONST_big,arg1);
748 static void emit_i_RETADDR ( AsmBCO bco, int arg1 )
752 emiti_8 (bco,i_RETADDR, arg1); else
753 emiti_16(bco,i_RETADDR_big,arg1);
757 /* --------------------------------------------------------------------------
759 * ------------------------------------------------------------------------*/
761 AsmSp asmBeginArgCheck ( AsmBCO bco )
763 ASSERT(bco->sp == 0);
767 void asmEndArgCheck ( AsmBCO bco, AsmSp last_arg )
769 nat args = bco->sp - last_arg;
770 if (args != 0) { /* optimisation */
771 emiti_8(bco,i_ARG_CHECK,args);
775 /* --------------------------------------------------------------------------
776 * Creating and using "variables"
777 * ------------------------------------------------------------------------*/
779 AsmVar asmBind ( AsmBCO bco, AsmRep rep )
781 incSp(bco,repSizeW(rep));
785 void asmVar ( AsmBCO bco, AsmVar v, AsmRep rep )
789 if (rep == VOID_REP) {
791 incSp(bco,repSizeW(rep));
795 offset = bco->sp - v;
799 emit_i_VAR_INT(bco,offset);
803 emit_i_VAR_WORD(bco,offset);
806 emit_i_VAR_ADDR(bco,offset);
809 emit_i_VAR_CHAR(bco,offset);
812 emit_i_VAR_FLOAT(bco,offset);
815 emit_i_VAR_DOUBLE(bco,offset);
818 emit_i_VAR_STABLE(bco,offset);
825 #ifdef PROVIDE_FOREIGN
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 */
841 emit_i_VAR(bco,offset);
844 barf("asmVar %d",rep);
846 incSp(bco,repSizeW(rep));
849 /* --------------------------------------------------------------------------
851 * ------------------------------------------------------------------------*/
853 AsmSp asmBeginEnter( AsmBCO bco )
858 void asmEndEnter( AsmBCO bco, AsmSp sp1, AsmSp sp2 )
860 int x = bco->sp - sp1;
862 ASSERT(x >= 0 && y >= 0);
864 emit_i_SLIDE(bco,x,y);
865 decSp(bco,sp1 - sp2);
868 decSp(bco,sizeofW(StgPtr));
871 /* --------------------------------------------------------------------------
872 * Build boxed Ints, Floats, etc
873 * ------------------------------------------------------------------------*/
875 AsmVar asmBox( AsmBCO bco, AsmRep rep )
879 emiti_(bco,i_PACK_CHAR);
882 emiti_(bco,i_PACK_INT);
886 emiti_(bco,i_PACK_WORD);
889 emiti_(bco,i_PACK_ADDR);
892 emiti_(bco,i_PACK_FLOAT);
895 emiti_(bco,i_PACK_DOUBLE);
898 emiti_(bco,i_PACK_STABLE);
902 barf("asmBox %d",rep);
904 /* NB: these operations DO pop their arg */
905 decSp(bco, repSizeW(rep)); /* pop unboxed arg */
906 incSp(bco, sizeofW(StgPtr)); /* push box */
910 /* --------------------------------------------------------------------------
911 * Unbox Ints, Floats, etc
912 * ------------------------------------------------------------------------*/
914 AsmVar asmUnbox( AsmBCO bco, AsmRep rep )
918 emiti_(bco,i_UNPACK_INT);
922 emiti_(bco,i_UNPACK_WORD);
925 emiti_(bco,i_UNPACK_ADDR);
928 emiti_(bco,i_UNPACK_CHAR);
931 emiti_(bco,i_UNPACK_FLOAT);
934 emiti_(bco,i_UNPACK_DOUBLE);
937 emiti_(bco,i_UNPACK_STABLE);
940 barf("asmUnbox %d",rep);
942 /* NB: these operations DO NOT pop their arg */
943 incSp(bco, repSizeW(rep)); /* push unboxed arg */
948 /* --------------------------------------------------------------------------
949 * Push unboxed Ints, Floats, etc
950 * ------------------------------------------------------------------------*/
952 void asmConstInt( AsmBCO bco, AsmInt x )
954 emit_i_CONST_INT(bco,bco->nps.len);
955 asmWords(bco,AsmInt,x);
956 incSp(bco, repSizeW(INT_REP));
959 void asmConstInteger( AsmBCO bco, AsmString x )
961 emit_i_CONST_INTEGER(bco,bco->nps.len);
962 asmWords(bco,AsmString,x);
963 incSp(bco, repSizeW(INTEGER_REP));
966 void asmConstAddr( AsmBCO bco, AsmAddr x )
968 emit_i_CONST_ADDR(bco,bco->nps.len);
969 asmWords(bco,AsmAddr,x);
970 incSp(bco, repSizeW(ADDR_REP));
973 void asmConstWord( AsmBCO bco, AsmWord x )
975 emit_i_CONST_INT(bco,bco->nps.len);
976 asmWords(bco,AsmWord,(AsmInt)x);
977 incSp(bco, repSizeW(WORD_REP));
980 void asmConstChar( AsmBCO bco, AsmChar x )
982 emit_i_CONST_CHAR(bco,bco->nps.len);
983 asmWords(bco,AsmChar,x);
984 incSp(bco, repSizeW(CHAR_REP));
987 void asmConstFloat( AsmBCO bco, AsmFloat x )
989 emit_i_CONST_FLOAT(bco,bco->nps.len);
990 asmWords(bco,AsmFloat,x);
991 incSp(bco, repSizeW(FLOAT_REP));
994 void asmConstDouble( AsmBCO bco, AsmDouble x )
996 emit_i_CONST_DOUBLE(bco,bco->nps.len);
997 asmWords(bco,AsmDouble,x);
998 incSp(bco, repSizeW(DOUBLE_REP));
1001 /* --------------------------------------------------------------------------
1002 * Algebraic case helpers
1003 * ------------------------------------------------------------------------*/
1005 /* a mildly bogus pair of functions... */
1006 AsmSp asmBeginCase( AsmBCO bco )
1011 void asmEndCase( AsmBCO bco )
1015 AsmSp asmContinuation( AsmBCO bco, AsmBCO ret_addr )
1017 emit_i_RETADDR(bco,bco->object.ptrs.len);
1018 asmPtr(bco,&(ret_addr->object));
1019 incSp(bco, 2 * sizeofW(StgPtr));
1023 AsmBCO asmBeginContinuation ( AsmSp sp, int /*List*/ alts )
1025 AsmBCO bco = asmBeginBCO(alts);
1030 void asmEndContinuation ( AsmBCO bco )
1036 /* --------------------------------------------------------------------------
1038 * ------------------------------------------------------------------------*/
1040 AsmSp asmBeginAlt( AsmBCO bco )
1045 void asmEndAlt( AsmBCO bco, AsmSp sp )
1050 AsmPc asmTest( AsmBCO bco, AsmWord tag )
1052 emiti_8_16(bco,i_TEST,tag,0);
1056 AsmPc asmTestInt( AsmBCO bco, AsmVar v, AsmInt x )
1058 asmVar(bco,v,INT_REP);
1060 emiti_16(bco,i_TEST_INT,0);
1061 decSp(bco, 2*repSizeW(INT_REP));
1065 void asmFixBranch( AsmBCO bco, AsmPc from )
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);
1074 void asmPanic( AsmBCO bco )
1076 emiti_(bco,i_PANIC); /* "irrefutable" pattern failed - oops! */
1079 /* --------------------------------------------------------------------------
1081 * ------------------------------------------------------------------------*/
1083 AsmSp asmBeginPrim( AsmBCO bco )
1088 void asmEndPrim( AsmBCO bco, const AsmPrim* prim, AsmSp base )
1090 emiti_8(bco,prim->prefix,prim->opcode);
1094 char* asmGetPrimopName ( AsmPrim* p )
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.
1104 AsmPrim asmPrimOps[] = {
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 }
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 }
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 }
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 }
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 }
1166 , { "primIntToWord", "I", "W", MONAD_Id, i_PRIMOP1, i_intToWord }
1167 , { "primWordToInt", "W", "I", MONAD_Id, i_PRIMOP1, i_wordToInt }
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 }
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 }
1187 /* Stable# operations */
1188 , { "primIntToStablePtr", "I", "s", MONAD_Id, i_PRIMOP1, i_intToStable }
1189 , { "primStablePtrToInt", "s", "I", MONAD_Id, i_PRIMOP1, i_stableToInt }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
1330 , { "primNewByteArray", "I", "m", MONAD_ST, i_PRIMOP2, i_newByteArray }
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 }
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 }
1340 /* {new,write,read,index}IntegerArray not provided */
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 }
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 }
1362 /* {new,write,read,index}ForeignObjArray not provided */
1365 #ifdef PROVIDE_FOREIGN
1366 /* ForeignObj# operations */
1367 , { "primMakeForeignObj", "A", "f", MONAD_IO, i_PRIMOP2, i_makeForeignObj }
1370 /* WeakPair# operations */
1371 , { "primMakeWeak", "bac", "w", MONAD_IO, i_PRIMOP2, i_makeWeak }
1372 , { "primDeRefWeak", "w", "Ia", MONAD_IO, i_PRIMOP2, i_deRefWeak }
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 }
1379 /* foreign export dynamic support */
1380 , { "primCreateAdjThunkARCH", "sAC","A", MONAD_IO, i_PRIMOP2, i_createAdjThunkARCH }
1382 /* misc handy hacks */
1383 , { "primGetArgc", "", "I", MONAD_IO, i_PRIMOP2, i_getArgc }
1384 , { "primGetArgv", "I", "A", MONAD_IO, i_PRIMOP2, i_getArgv }
1386 #ifdef PROVIDE_PTREQUALITY
1387 , { "primReallyUnsafePtrEquality", "aa", "B",MONAD_Id, i_PRIMOP2, i_reallyUnsafePtrEquality }
1389 #ifdef PROVIDE_COERCE
1390 , { "primUnsafeCoerce", "a", "b", MONAD_Id, i_PRIMOP2, i_unsafeCoerce }
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 }
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 }
1408 /* Ccall is polyadic - so it's excluded from this table */
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 };
1423 AsmPrim* asmFindPrim( char* s )
1426 for (i=0; asmPrimOps[i].name; ++i) {
1427 if (strcmp(s,asmPrimOps[i].name)==0) {
1428 return &asmPrimOps[i];
1434 AsmPrim* asmFindPrimop( AsmInstr prefix, AsmInstr op )
1437 for (i=0; asmPrimOps[i].name; ++i) {
1438 if (asmPrimOps[i].prefix == prefix && asmPrimOps[i].opcode == op) {
1439 return &asmPrimOps[i];
1445 /* --------------------------------------------------------------------------
1446 * Handwritten primops
1447 * ------------------------------------------------------------------------*/
1449 AsmBCO asm_BCO_catch ( void )
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));
1461 AsmBCO asm_BCO_raise ( void )
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));
1471 AsmBCO asm_BCO_seq ( void )
1475 cont = asmBeginBCO(0 /*NIL*/);
1476 emiti_8(cont,i_ARG_CHECK,2); /* should never fail */
1478 emit_i_SLIDE(cont,1,2);
1479 emiti_(cont,i_ENTER);
1480 incSp(cont, 3*sizeofW(StgPtr));
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));
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));
1497 AsmBCO asm_BCO_takeMVar ( void )
1499 AsmBCO kase, casecont, take;
1501 take = asmBeginBCO(0 /*NIL*/);
1503 emiti_8(take,i_PRIMOP2,i_takeMVar);
1507 emit_i_SLIDE(take,3,4);
1508 emiti_(take,i_ENTER);
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);
1522 asmEndBCO(casecont);
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));
1529 emiti_(kase,i_ENTER);
1537 /* --------------------------------------------------------------------------
1539 * ------------------------------------------------------------------------*/
1541 AsmVar asmAllocCONSTR ( AsmBCO bco, AsmInfo info )
1544 ASSERT( sizeW_fromITBL(info) >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
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 );
1551 emiti_8(bco,i_ALLOC_CONSTR,bco->nps.len);
1552 asmWords(bco,AsmInfo,info);
1554 emiti_8(bco,i_ALLOC_CONSTR,i);
1557 incSp(bco, sizeofW(StgClosurePtr));
1561 AsmSp asmBeginPack( AsmBCO bco )
1566 void asmEndPack( AsmBCO bco, AsmVar v, AsmSp start, AsmInfo info )
1568 nat size = bco->sp - start;
1569 assert(bco->sp >= start);
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);
1577 void asmBeginUnpack( AsmBCO bco )
1579 /* dummy to make it look prettier */
1582 void asmEndUnpack( AsmBCO bco )
1584 emiti_(bco,i_UNPACK);
1587 AsmVar asmAllocAP( AsmBCO bco, AsmNat words )
1589 emiti_8(bco,i_ALLOC_AP,words);
1590 incSp(bco, sizeofW(StgPtr));
1594 AsmSp asmBeginMkAP( AsmBCO bco )
1599 void asmEndMkAP( AsmBCO bco, AsmVar v, AsmSp start )
1601 emit_i_MKAP(bco,bco->sp-v,bco->sp-start-1);
1602 /* -1 because fun isn't counted */
1606 AsmVar asmAllocPAP( AsmBCO bco, AsmNat size )
1608 emiti_8(bco,i_ALLOC_PAP,size);
1609 incSp(bco, sizeofW(StgPtr));
1613 AsmSp asmBeginMkPAP( AsmBCO bco )
1618 void asmEndMkPAP( AsmBCO bco, AsmVar v, AsmSp start )
1620 emiti_8_8(bco,i_MKPAP,bco->sp-v,bco->sp-start-1);
1621 /* -1 because fun isn't counted */
1625 AsmVar asmClosure( AsmBCO bco, AsmObject p )
1627 emit_i_CONST(bco,bco->object.ptrs.len);
1629 incSp(bco, sizeofW(StgPtr));
1633 AsmVar asmGHCClosure( AsmBCO bco, AsmObject p )
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);
1643 /* --------------------------------------------------------------------------
1644 * Building InfoTables
1645 * ------------------------------------------------------------------------*/
1647 AsmInfo asmMkInfo( AsmNat tag, AsmNat ptrs )
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.
1653 AsmNat nptrs = stg_max(0,MIN_NONUPD_SIZE-ptrs);
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);
1663 #warning asmMkInfo: Need to insert entry code in some cunning way
1665 ASSERT( sizeW_fromITBL(info) >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
1669 /*-------------------------------------------------------------------------*/
1671 #endif /* INTERPRETER */