2 /* --------------------------------------------------------------------------
5 * Copyright (c) 1994-1998.
7 * $RCSfile: Assembler.c,v $
9 * $Date: 2000/04/27 16:35:30 $
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 static StgClosure* asmAlloc ( nat size );
62 extern void* getNameOrTupleClosureCPtr ( int /*Cell*/ c );
65 /* --------------------------------------------------------------------------
66 * Initialising and managing objects and entities
67 * ------------------------------------------------------------------------*/
69 static struct AsmObject_* objects;
71 #define INITIALISE_TABLE(Type,table,size,used) \
75 #define ENSURE_SPACE_IN_TABLE(Type,table,size,used) \
78 size = (size ? 2*size : 1); \
79 new = malloc ( size * sizeof(Type)); \
81 barf("bytecode assembler: can't expand table of type " \
83 memcpy ( new, table, used * sizeof(Type) ); \
84 if (table) free(table); \
88 void asmInitialise ( void )
94 AsmObject asmNewObject ( void )
96 AsmObject obj = malloc(sizeof(struct AsmObject_));
98 barf("bytecode assembler: can't malloc in asmNewObject");
101 obj->n_refs = obj->n_words = obj->n_insns = 0;
103 obj->magic = 0x31415927;
104 INITIALISE_TABLE(AsmEntity,obj->entities,
111 void asmAddEntity ( AsmObject obj,
115 ENSURE_SPACE_IN_TABLE(
116 Asm_Entity,obj->entities,
117 obj->sizeEntities,obj->usedEntities);
118 obj->entities[obj->usedEntities].kind = kind;
119 obj->entities[obj->usedEntities].val = val;
122 case Asm_RefNoOp: case Asm_RefObject: case Asm_RefHugs:
123 obj->n_refs++; break;
125 obj->n_words++; break;
127 obj->n_insns++; break;
129 barf("asmAddEntity");
133 static int asmFindInNonPtrs ( AsmBCO bco, StgWord w )
136 for (i = 0; i < bco->usedEntities; i++) {
137 if (bco->entities[i].kind == Asm_NonPtrWord) {
138 if (bco->entities[i].val == w) return j;
145 static void setInstrs ( AsmBCO bco, int instr_no, StgWord new_instr_byte )
148 for (i = 0; i < bco->usedEntities; i++) {
149 if (bco->entities[i].kind == Asm_Insn8) {
151 bco->entities[i].val = new_instr_byte;
160 void* asmGetClosureOfObject ( AsmObject obj )
166 /* --------------------------------------------------------------------------
167 * Top level assembler/BCO linker functions
168 * ------------------------------------------------------------------------*/
170 int asmCalcHeapSizeW ( AsmObject obj )
177 is = obj->n_insns + (obj->max_sp <= 255 ? 2 : 3);
178 ws = BCO_sizeW ( p, np, is );
186 ws = CONSTR_sizeW ( p, np );
189 barf("asmCalcHeapSizeW");
191 if (ws - sizeofW(StgHeader) < MIN_NONUPD_SIZE)
192 ws = sizeofW(StgHeader) + MIN_NONUPD_SIZE;
197 void asmAllocateHeapSpace ( void )
200 for (obj = objects; obj; obj = obj->next) {
201 StgClosure* c = asmAlloc ( asmCalcHeapSizeW ( obj ) );
206 void asmShutdown ( void )
209 AsmObject next = NULL;
210 for (obj = objects; obj; obj = next) {
212 obj->magic = 0x27180828;
213 if ( /*paranoia*/ obj->entities)
220 StgClosure* asmDerefEntity ( Asm_Entity entity )
222 switch (entity.kind) {
224 return (StgClosure*)entity.val;
227 ASSERT( ((AsmObject)(entity.val))->magic == 0x31415927 );
228 return ((AsmObject)(entity.val))->closure;
230 return getNameOrTupleClosureCPtr(entity.val);
232 barf("asmDerefEntity");
234 return NULL; /*notreached*/
237 void asmCopyAndLink ( void )
242 for (obj = objects; obj; obj = obj->next) {
243 StgClosure** p = (StgClosure**)(obj->closure);
249 AsmBCO abco = (AsmBCO)obj;
250 StgBCO* bco = (StgBCO*)p;
251 SET_HDR(bco,&BCO_info,??);
252 bco->n_ptrs = abco->n_refs;
253 bco->n_words = abco->n_words;
254 bco->n_instrs = abco->n_insns + (obj->max_sp <= 255 ? 2 : 3);
255 bco->stgexpr = abco->stgexpr;
257 /* First copy in the ptrs. */
259 for (j = 0; j < obj->usedEntities; j++) {
260 switch (obj->entities[j].kind) {
264 bcoConstCPtr(bco,k++)
265 = (StgClosure*)asmDerefEntity(obj->entities[j]); break;
271 /* Now the non-ptrs. */
273 for (j = 0; j < obj->usedEntities; j++) {
274 switch (obj->entities[j].kind) {
276 bcoConstWord(bco,k++) = obj->entities[j].val; break;
282 /* Finally the insns, adding a stack check at the start. */
284 abco->max_sp = stg_max(abco->sp,abco->max_sp);
286 ASSERT(abco->max_sp <= 65535);
287 if (abco->max_sp <= 255) {
288 bcoInstr(bco,k++) = i_STK_CHECK;
289 bcoInstr(bco,k++) = abco->max_sp;
291 bcoInstr(bco,k++) = i_STK_CHECK_big;
292 bcoInstr(bco,k++) = abco->max_sp / 256;
293 bcoInstr(bco,k++) = abco->max_sp % 256;
295 for (j = 0; j < obj->usedEntities; j++) {
296 switch (obj->entities[j].kind) {
298 bcoInstr(bco,k++) = obj->entities[j].val; break;
305 barf("asmCopyAndLink: strange stuff in AsmBCO");
309 ASSERT(k == bco->n_instrs);
314 StgCAF* caf = (StgCAF*)p;
315 SET_HDR(caf,&CAF_UNENTERED_info,??);
317 caf->mut_link = NULL;
318 caf->value = (StgClosure*)0xdeadbeef;
319 ASSERT(obj->usedEntities == 1);
320 switch (obj->entities[0].kind) {
324 caf->body = (StgClosure*)asmDerefEntity(obj->entities[0]);
327 barf("asmCopyAndLink: strange stuff in AsmCAF");
334 SET_HDR((StgClosure*)p,obj->itbl,??);
336 /* First put in the pointers, then the non-pointers. */
337 for (j = 0; j < obj->usedEntities; j++) {
338 switch (obj->entities[j].kind) {
342 *p++ = asmDerefEntity(obj->entities[j]); break;
347 for (j = 0; j < obj->usedEntities; j++) {
348 switch (obj->entities[j].kind) {
350 *p++ = (StgClosure*)(obj->entities[j].val); break;
352 barf("asmCopyAndLink: strange stuff in AsmCon");
359 barf("asmCopyAndLink");
366 void asmMarkObject ( AsmObject obj )
368 ASSERT(obj->num_unresolved == 0 && obj->closure);
369 obj->closure = MarkRoot(obj->closure);
373 /* --------------------------------------------------------------------------
374 * Keeping track of the simulated stack pointer
375 * ------------------------------------------------------------------------*/
377 static StgClosure* asmAlloc( nat size )
379 StgClosure* o = stgCast(StgClosure*,allocate(size));
380 ASSERT( size >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
381 /* printf("Allocated %p .. %p\n", o, o+size-1); */
385 static void setSp( AsmBCO bco, AsmSp sp )
387 bco->max_sp = stg_max(bco->sp,bco->max_sp);
389 bco->max_sp = stg_max(bco->sp,bco->max_sp);
392 static void incSp ( AsmBCO bco, int sp_delta )
394 bco->max_sp = stg_max(bco->sp,bco->max_sp);
396 bco->max_sp = stg_max(bco->sp,bco->max_sp);
399 static void decSp ( AsmBCO bco, int sp_delta )
401 bco->max_sp = stg_max(bco->sp,bco->max_sp);
403 bco->max_sp = stg_max(bco->sp,bco->max_sp);
406 /* --------------------------------------------------------------------------
408 * ------------------------------------------------------------------------*/
410 AsmCon asmBeginCon( AsmInfo info )
412 AsmCon con = asmNewObject();
418 void asmEndCon( AsmCon con )
422 AsmCAF asmBeginCAF( void )
424 AsmCAF caf = asmNewObject();
429 void asmEndCAF( AsmCAF caf )
433 AsmBCO asmBeginBCO( int /*StgExpr*/ e )
435 AsmBCO bco = asmNewObject();
440 bco->lastOpc = i_INTERNAL_ERROR;
444 void asmEndBCO( AsmBCO bco )
448 /* --------------------------------------------------------------------------
450 * ------------------------------------------------------------------------*/
452 static void asmAddInstr ( AsmBCO bco, StgWord i )
454 asmAddEntity ( bco, Asm_Insn8, i );
457 static void asmAddNonPtrWord ( AsmObject obj, StgWord i )
459 asmAddEntity ( obj, Asm_NonPtrWord, i );
462 void asmAddRefHugs ( AsmObject obj,int /*Name*/ n )
464 asmAddEntity ( obj, Asm_RefHugs, n );
467 void asmAddRefObject ( AsmObject obj, AsmObject p )
469 ASSERT(p->magic == 0x31415927);
470 asmAddEntity ( obj, Asm_RefObject, (StgWord)p );
473 void asmAddRefNoOp ( AsmObject obj, StgPtr p )
475 asmAddEntity ( obj, Asm_RefNoOp, (StgWord)p );
480 static void asmInstrOp ( AsmBCO bco, StgWord i )
482 ASSERT(i <= BIGGEST_OPCODE); /* must be a valid opcode */
487 static void asmInstr8 ( AsmBCO bco, StgWord i )
490 ASSERT(i < 256); /* must be a byte */
495 static void asmInstr16 ( AsmBCO bco, StgWord i )
497 ASSERT(i < 65536); /* must be a short */
498 asmAddInstr(bco,i / 256);
499 asmAddInstr(bco,i % 256);
503 static Instr asmInstrBack ( AsmBCO bco, StgWord n )
505 return bco->is.elems[bco->is.len - n];
508 static void asmInstrRecede ( AsmBCO bco, StgWord n )
510 if (bco->is.len < n) barf("asmInstrRecede");
515 #define asmAddNonPtrWords(bco,ty,x) \
517 union { ty a; AsmWord b[sizeofW(ty)]; } p; \
519 if (sizeof(ty) < sizeof(AsmWord)) p.b[0]=0; \
521 for( i = 0; i < sizeofW(ty); i++ ) { \
522 asmAddNonPtrWord(bco,p.b[i]); \
526 static StgWord repSizeW( AsmRep rep )
529 case CHAR_REP: return sizeofW(StgWord) + sizeofW(StgChar);
532 case INT_REP: return sizeofW(StgWord) + sizeofW(StgInt);
534 case WORD_REP: return sizeofW(StgWord) + sizeofW(StgWord);
535 case ADDR_REP: return sizeofW(StgWord) + sizeofW(StgAddr);
536 case FLOAT_REP: return sizeofW(StgWord) + sizeofW(StgFloat);
537 case DOUBLE_REP: return sizeofW(StgWord) + sizeofW(StgDouble);
538 case STABLE_REP: return sizeofW(StgWord) + sizeofW(StgWord);
544 #ifdef PROVIDE_FOREIGN
547 case ALPHA_REP: /* a */
548 case BETA_REP: /* b */
549 case GAMMA_REP: /* c */
550 case DELTA_REP: /* d */
551 case HANDLER_REP: /* IOError -> IO a */
552 case ERROR_REP: /* IOError */
553 case ARR_REP : /* PrimArray a */
554 case BARR_REP : /* PrimByteArray a */
555 case REF_REP : /* Ref s a */
556 case MUTARR_REP : /* PrimMutableArray s a */
557 case MUTBARR_REP: /* PrimMutableByteArray s a */
558 case MVAR_REP: /* MVar a */
559 case PTR_REP: return sizeofW(StgPtr);
561 case VOID_REP: return sizeofW(StgWord);
562 default: barf("repSizeW %d",rep);
567 int asmRepSizeW ( AsmRep rep )
569 return repSizeW ( rep );
573 /* --------------------------------------------------------------------------
574 * Instruction emission. All instructions should be routed through here
575 * so that the peephole optimiser gets to see what's happening.
576 * ------------------------------------------------------------------------*/
578 static void emiti_ ( AsmBCO bco, Instr opcode )
582 if (bco->lastOpc == i_SLIDE && opcode == i_ENTER) {
583 /* SLIDE x y ; ENTER ===> SE x y */
584 x = asmInstrBack(bco,2);
585 y = asmInstrBack(bco,1);
586 asmInstrRecede(bco,3);
587 asmInstrOp(bco,i_SE); asmInstr8(bco,x); asmInstr8(bco,y);
590 if (bco->lastOpc == i_RV && opcode == i_ENTER) {
591 /* RV x y ; ENTER ===> RVE x (y-2)
592 Because RETADDR pushes 2 words on the stack, y must be at least 2. */
593 x = asmInstrBack(bco,2);
594 y = asmInstrBack(bco,1);
595 if (y < 2) barf("emiti_: RVE: impossible y value");
596 asmInstrRecede(bco,3);
597 asmInstrOp(bco, i_RVE); asmInstr8(bco,x); asmInstr8(bco,y-2);
600 asmInstrOp(bco,opcode);
603 asmInstrOp(bco,opcode);
607 static void emiti_8 ( AsmBCO bco, Instr opcode, int arg1 )
611 if (bco->lastOpc == i_VAR && opcode == i_VAR) {
612 /* VAR x ; VAR y ===> VV x y */
613 x = asmInstrBack(bco,1);
614 asmInstrRecede(bco,2);
615 asmInstrOp(bco,i_VV); asmInstr8(bco,x); asmInstr8(bco,arg1);
618 if (bco->lastOpc == i_RETADDR && opcode == i_VAR) {
619 /* RETADDR x ; VAR y ===> RV x y */
620 x = asmInstrBack(bco,1);
621 asmInstrRecede(bco,2);
622 asmInstrOp(bco, i_RV); asmInstr8(bco,x); asmInstr8(bco,arg1);
625 asmInstrOp(bco,opcode);
629 asmInstrOp(bco,opcode);
634 static void emiti_16 ( AsmBCO bco, Instr opcode, int arg1 )
636 asmInstrOp(bco,opcode);
637 asmInstr16(bco,arg1);
640 static void emiti_8_8 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
642 asmInstrOp(bco,opcode);
647 static void emiti_8_16 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
649 asmInstrOp(bco,opcode);
651 asmInstr16(bco,arg2);
654 static void emiti_16_16 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
656 asmInstrOp(bco,opcode);
657 asmInstr16(bco,arg1);
658 asmInstr16(bco,arg2);
662 /* --------------------------------------------------------------------------
663 * Wrappers around the above fns
664 * ------------------------------------------------------------------------*/
666 static void emit_i_VAR_INT ( AsmBCO bco, int arg1 )
670 emiti_8 (bco,i_VAR_INT, arg1); else
671 emiti_16(bco,i_VAR_INT_big,arg1);
674 static void emit_i_VAR_WORD ( AsmBCO bco, int arg1 )
678 emiti_8 (bco,i_VAR_WORD, arg1); else
679 emiti_16(bco,i_VAR_WORD_big,arg1);
682 static void emit_i_VAR_ADDR ( AsmBCO bco, int arg1 )
686 emiti_8 (bco,i_VAR_ADDR, arg1); else
687 emiti_16(bco,i_VAR_ADDR_big,arg1);
690 static void emit_i_VAR_CHAR ( AsmBCO bco, int arg1 )
694 emiti_8 (bco,i_VAR_CHAR, arg1); else
695 emiti_16(bco,i_VAR_CHAR_big,arg1);
698 static void emit_i_VAR_FLOAT ( AsmBCO bco, int arg1 )
702 emiti_8 (bco,i_VAR_FLOAT, arg1); else
703 emiti_16(bco,i_VAR_FLOAT_big,arg1);
706 static void emit_i_VAR_DOUBLE ( AsmBCO bco, int arg1 )
710 emiti_8 (bco,i_VAR_DOUBLE, arg1); else
711 emiti_16(bco,i_VAR_DOUBLE_big,arg1);
714 static void emit_i_VAR_STABLE ( AsmBCO bco, int arg1 )
718 emiti_8 (bco,i_VAR_STABLE, arg1); else
719 emiti_16(bco,i_VAR_STABLE_big,arg1);
722 static void emit_i_VAR ( AsmBCO bco, int arg1 )
726 emiti_8 (bco,i_VAR, arg1); else
727 emiti_16(bco,i_VAR_big,arg1);
730 static void emit_i_PACK ( AsmBCO bco, int arg1 )
734 emiti_8 (bco,i_PACK, arg1); else
735 emiti_16(bco,i_PACK_big,arg1);
738 static void emit_i_SLIDE ( AsmBCO bco, int arg1, int arg2 )
742 if (arg1 < 256 && arg2 < 256)
743 emiti_8_8 (bco,i_SLIDE, arg1,arg2); else
744 emiti_16_16(bco,i_SLIDE_big,arg1,arg2);
747 static void emit_i_MKAP ( AsmBCO bco, int arg1, int arg2 )
751 if (arg1 < 256 && arg2 < 256)
752 emiti_8_8 (bco,i_MKAP, arg1,arg2); else
753 emiti_16_16(bco,i_MKAP_big,arg1,arg2);
757 static void emit_i_CONST_INT ( AsmBCO bco, int arg1 )
761 emiti_8 (bco,i_CONST_INT, arg1); else
762 emiti_16(bco,i_CONST_INT_big,arg1);
765 static void emit_i_CONST_INTEGER ( AsmBCO bco, int arg1 )
769 emiti_8 (bco,i_CONST_INTEGER, arg1); else
770 emiti_16(bco,i_CONST_INTEGER_big,arg1);
773 static void emit_i_CONST_ADDR ( AsmBCO bco, int arg1 )
777 emiti_8 (bco,i_CONST_ADDR, arg1); else
778 emiti_16(bco,i_CONST_ADDR_big,arg1);
781 static void emit_i_CONST_CHAR ( AsmBCO bco, int arg1 )
785 emiti_8 (bco,i_CONST_CHAR, arg1); else
786 emiti_16(bco,i_CONST_CHAR_big,arg1);
789 static void emit_i_CONST_FLOAT ( AsmBCO bco, int arg1 )
793 emiti_8 (bco,i_CONST_FLOAT, arg1); else
794 emiti_16(bco,i_CONST_FLOAT_big,arg1);
797 static void emit_i_CONST_DOUBLE ( AsmBCO bco, int arg1 )
801 emiti_8 (bco,i_CONST_DOUBLE, arg1); else
802 emiti_16(bco,i_CONST_DOUBLE_big,arg1);
805 static void emit_i_CONST ( AsmBCO bco, int arg1 )
809 emiti_8 (bco,i_CONST, arg1); else
810 emiti_16(bco,i_CONST_big,arg1);
813 static void emit_i_RETADDR ( AsmBCO bco, int arg1 )
817 emiti_8 (bco,i_RETADDR, arg1); else
818 emiti_16(bco,i_RETADDR_big,arg1);
821 static void emit_i_ALLOC_CONSTR ( AsmBCO bco, int arg1 )
825 emiti_8 (bco,i_ALLOC_CONSTR, arg1); else
826 emiti_16(bco,i_ALLOC_CONSTR_big,arg1);
829 /* --------------------------------------------------------------------------
831 * ------------------------------------------------------------------------*/
833 AsmSp asmBeginArgCheck ( AsmBCO bco )
835 ASSERT(bco->sp == 0);
839 void asmEndArgCheck ( AsmBCO bco, AsmSp last_arg )
841 nat args = bco->sp - last_arg;
842 if (args != 0) { /* optimisation */
843 emiti_8(bco,i_ARG_CHECK,args);
847 /* --------------------------------------------------------------------------
848 * Creating and using "variables"
849 * ------------------------------------------------------------------------*/
851 AsmVar asmBind ( AsmBCO bco, AsmRep rep )
853 incSp(bco,repSizeW(rep));
857 void asmVar ( AsmBCO bco, AsmVar v, AsmRep rep )
861 if (rep == VOID_REP) {
863 incSp(bco,repSizeW(rep));
867 offset = bco->sp - v;
871 emit_i_VAR_INT(bco,offset);
875 emit_i_VAR_WORD(bco,offset);
878 emit_i_VAR_ADDR(bco,offset);
881 emit_i_VAR_CHAR(bco,offset);
884 emit_i_VAR_FLOAT(bco,offset);
887 emit_i_VAR_DOUBLE(bco,offset);
890 emit_i_VAR_STABLE(bco,offset);
897 #ifdef PROVIDE_FOREIGN
900 case ALPHA_REP: /* a */
901 case BETA_REP: /* b */
902 case GAMMA_REP: /* c */
903 case DELTA_REP: /* d */
904 case HANDLER_REP: /* IOError -> IO a */
905 case ERROR_REP: /* IOError */
906 case ARR_REP : /* PrimArray a */
907 case BARR_REP : /* PrimByteArray a */
908 case REF_REP : /* Ref s a */
909 case MUTARR_REP : /* PrimMutableArray s a */
910 case MUTBARR_REP: /* PrimMutableByteArray s a */
911 case MVAR_REP: /* MVar a */
913 emit_i_VAR(bco,offset);
916 barf("asmVar %d",rep);
918 incSp(bco,repSizeW(rep));
921 /* --------------------------------------------------------------------------
923 * ------------------------------------------------------------------------*/
925 AsmSp asmBeginEnter( AsmBCO bco )
930 void asmEndEnter( AsmBCO bco, AsmSp sp1, AsmSp sp2 )
932 int x = bco->sp - sp1;
934 ASSERT(x >= 0 && y >= 0);
936 emit_i_SLIDE(bco,x,y);
937 decSp(bco,sp1 - sp2);
940 decSp(bco,sizeofW(StgPtr));
943 /* --------------------------------------------------------------------------
944 * Build boxed Ints, Floats, etc
945 * ------------------------------------------------------------------------*/
947 AsmVar asmBox( AsmBCO bco, AsmRep rep )
951 emiti_(bco,i_PACK_CHAR);
954 emiti_(bco,i_PACK_INT);
958 emiti_(bco,i_PACK_WORD);
961 emiti_(bco,i_PACK_ADDR);
964 emiti_(bco,i_PACK_FLOAT);
967 emiti_(bco,i_PACK_DOUBLE);
970 emiti_(bco,i_PACK_STABLE);
974 barf("asmBox %d",rep);
976 /* NB: these operations DO pop their arg */
977 decSp(bco, repSizeW(rep)); /* pop unboxed arg */
978 incSp(bco, sizeofW(StgPtr)); /* push box */
982 /* --------------------------------------------------------------------------
983 * Unbox Ints, Floats, etc
984 * ------------------------------------------------------------------------*/
986 AsmVar asmUnbox( AsmBCO bco, AsmRep rep )
990 emiti_(bco,i_UNPACK_INT);
994 emiti_(bco,i_UNPACK_WORD);
997 emiti_(bco,i_UNPACK_ADDR);
1000 emiti_(bco,i_UNPACK_CHAR);
1003 emiti_(bco,i_UNPACK_FLOAT);
1006 emiti_(bco,i_UNPACK_DOUBLE);
1009 emiti_(bco,i_UNPACK_STABLE);
1012 barf("asmUnbox %d",rep);
1014 /* NB: these operations DO NOT pop their arg */
1015 incSp(bco, repSizeW(rep)); /* push unboxed arg */
1020 /* --------------------------------------------------------------------------
1021 * Push unboxed Ints, Floats, etc
1022 * ------------------------------------------------------------------------*/
1024 void asmConstInt( AsmBCO bco, AsmInt x )
1026 emit_i_CONST_INT(bco,bco->n_words);
1027 asmAddNonPtrWords(bco,AsmInt,x);
1028 incSp(bco, repSizeW(INT_REP));
1031 void asmConstInteger( AsmBCO bco, AsmString x )
1033 emit_i_CONST_INTEGER(bco,bco->n_words);
1034 asmAddNonPtrWords(bco,AsmString,x);
1035 incSp(bco, repSizeW(INTEGER_REP));
1038 void asmConstAddr( AsmBCO bco, AsmAddr x )
1040 emit_i_CONST_ADDR(bco,bco->n_words);
1041 asmAddNonPtrWords(bco,AsmAddr,x);
1042 incSp(bco, repSizeW(ADDR_REP));
1045 void asmConstWord( AsmBCO bco, AsmWord x )
1047 emit_i_CONST_INT(bco,bco->n_words);
1048 asmAddNonPtrWords(bco,AsmWord,(AsmInt)x);
1049 incSp(bco, repSizeW(WORD_REP));
1052 void asmConstChar( AsmBCO bco, AsmChar x )
1054 emit_i_CONST_CHAR(bco,bco->n_words);
1055 asmAddNonPtrWords(bco,AsmChar,x);
1056 incSp(bco, repSizeW(CHAR_REP));
1059 void asmConstFloat( AsmBCO bco, AsmFloat x )
1061 emit_i_CONST_FLOAT(bco,bco->n_words);
1062 asmAddNonPtrWords(bco,AsmFloat,x);
1063 incSp(bco, repSizeW(FLOAT_REP));
1066 void asmConstDouble( AsmBCO bco, AsmDouble x )
1068 emit_i_CONST_DOUBLE(bco,bco->n_words);
1069 asmAddNonPtrWords(bco,AsmDouble,x);
1070 incSp(bco, repSizeW(DOUBLE_REP));
1073 /* --------------------------------------------------------------------------
1074 * Algebraic case helpers
1075 * ------------------------------------------------------------------------*/
1077 /* a mildly bogus pair of functions... */
1078 AsmSp asmBeginCase( AsmBCO bco )
1083 void asmEndCase( AsmBCO bco )
1087 AsmSp asmContinuation( AsmBCO bco, AsmBCO ret_addr )
1089 emit_i_RETADDR(bco,bco->n_refs);
1090 asmAddRefObject(bco,ret_addr);
1091 incSp(bco, 2 * sizeofW(StgPtr));
1095 AsmBCO asmBeginContinuation ( AsmSp sp, int /*List*/ alts )
1097 AsmBCO bco = asmBeginBCO(alts);
1102 void asmEndContinuation ( AsmBCO bco )
1108 /* --------------------------------------------------------------------------
1110 * ------------------------------------------------------------------------*/
1112 AsmSp asmBeginAlt( AsmBCO bco )
1117 void asmEndAlt( AsmBCO bco, AsmSp sp )
1122 AsmPc asmTest( AsmBCO bco, AsmWord tag )
1124 emiti_8_16(bco,i_TEST,tag,0);
1125 return bco->n_insns;
1128 AsmPc asmTestInt ( AsmBCO bco, AsmVar v, AsmInt x )
1130 asmVar(bco,v,INT_REP);
1132 emiti_16(bco,i_TEST_INT,0);
1133 decSp(bco, 2*repSizeW(INT_REP));
1134 return bco->n_insns;
1137 void asmFixBranch ( AsmBCO bco, AsmPc from )
1139 int distance = bco->n_insns - from;
1140 ASSERT(distance >= 0);
1141 ASSERT(distance < 65536);
1142 setInstrs(bco,from-2,distance/256);
1143 setInstrs(bco,from-1,distance%256);
1146 void asmPanic( AsmBCO bco )
1148 emiti_(bco,i_PANIC); /* "irrefutable" pattern failed - oops! */
1151 /* --------------------------------------------------------------------------
1153 * ------------------------------------------------------------------------*/
1155 AsmSp asmBeginPrim( AsmBCO bco )
1160 void asmEndPrim( AsmBCO bco, const AsmPrim* prim, AsmSp base )
1162 emiti_8(bco,prim->prefix,prim->opcode);
1166 char* asmGetPrimopName ( AsmPrim* p )
1171 /* Hugs used to let you add arbitrary primops with arbitrary types
1172 * just by editing Prelude.hs or any other file you wanted.
1173 * We deliberately avoided that approach because we wanted more
1174 * control over which primops are provided.
1176 AsmPrim asmPrimOps[] = {
1178 /* Char# operations */
1179 { "primGtChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_gtChar }
1180 , { "primGeChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_geChar }
1181 , { "primEqChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_eqChar }
1182 , { "primNeChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_neChar }
1183 , { "primLtChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_ltChar }
1184 , { "primLeChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_leChar }
1185 , { "primCharToInt", "C", "I", MONAD_Id, i_PRIMOP1, i_charToInt }
1186 , { "primIntToChar", "I", "C", MONAD_Id, i_PRIMOP1, i_intToChar }
1188 /* Int# operations */
1189 , { "primGtInt", "II", "B", MONAD_Id, i_PRIMOP1, i_gtInt }
1190 , { "primGeInt", "II", "B", MONAD_Id, i_PRIMOP1, i_geInt }
1191 , { "primEqInt", "II", "B", MONAD_Id, i_PRIMOP1, i_eqInt }
1192 , { "primNeInt", "II", "B", MONAD_Id, i_PRIMOP1, i_neInt }
1193 , { "primLtInt", "II", "B", MONAD_Id, i_PRIMOP1, i_ltInt }
1194 , { "primLeInt", "II", "B", MONAD_Id, i_PRIMOP1, i_leInt }
1195 , { "primMinInt", "", "I", MONAD_Id, i_PRIMOP1, i_minInt }
1196 , { "primMaxInt", "", "I", MONAD_Id, i_PRIMOP1, i_maxInt }
1197 , { "primPlusInt", "II", "I", MONAD_Id, i_PRIMOP1, i_plusInt }
1198 , { "primMinusInt", "II", "I", MONAD_Id, i_PRIMOP1, i_minusInt }
1199 , { "primTimesInt", "II", "I", MONAD_Id, i_PRIMOP1, i_timesInt }
1200 , { "primQuotInt", "II", "I", MONAD_Id, i_PRIMOP1, i_quotInt }
1201 , { "primRemInt", "II", "I", MONAD_Id, i_PRIMOP1, i_remInt }
1202 , { "primQuotRemInt", "II", "II", MONAD_Id, i_PRIMOP1, i_quotRemInt }
1203 , { "primNegateInt", "I", "I", MONAD_Id, i_PRIMOP1, i_negateInt }
1205 , { "primAndInt", "II", "I", MONAD_Id, i_PRIMOP1, i_andInt }
1206 , { "primOrInt", "II", "I", MONAD_Id, i_PRIMOP1, i_orInt }
1207 , { "primXorInt", "II", "I", MONAD_Id, i_PRIMOP1, i_xorInt }
1208 , { "primNotInt", "I", "I", MONAD_Id, i_PRIMOP1, i_notInt }
1209 , { "primShiftLInt", "II", "I", MONAD_Id, i_PRIMOP1, i_shiftLInt }
1210 , { "primShiftRAInt", "II", "I", MONAD_Id, i_PRIMOP1, i_shiftRAInt }
1211 , { "primShiftRLInt", "II", "I", MONAD_Id, i_PRIMOP1, i_shiftRLInt }
1213 /* Word# operations */
1214 , { "primGtWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_gtWord }
1215 , { "primGeWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_geWord }
1216 , { "primEqWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_eqWord }
1217 , { "primNeWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_neWord }
1218 , { "primLtWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_ltWord }
1219 , { "primLeWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_leWord }
1220 , { "primMinWord", "", "W", MONAD_Id, i_PRIMOP1, i_minWord }
1221 , { "primMaxWord", "", "W", MONAD_Id, i_PRIMOP1, i_maxWord }
1222 , { "primPlusWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_plusWord }
1223 , { "primMinusWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_minusWord }
1224 , { "primTimesWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_timesWord }
1225 , { "primQuotWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_quotWord }
1226 , { "primRemWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_remWord }
1227 , { "primQuotRemWord", "WW", "WW", MONAD_Id, i_PRIMOP1, i_quotRemWord }
1228 , { "primNegateWord", "W", "W", MONAD_Id, i_PRIMOP1, i_negateWord }
1230 , { "primAndWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_andWord }
1231 , { "primOrWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_orWord }
1232 , { "primXorWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_xorWord }
1233 , { "primNotWord", "W", "W", MONAD_Id, i_PRIMOP1, i_notWord }
1234 , { "primShiftLWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_shiftLWord }
1235 , { "primShiftRAWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_shiftRAWord }
1236 , { "primShiftRLWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_shiftRLWord }
1238 , { "primIntToWord", "I", "W", MONAD_Id, i_PRIMOP1, i_intToWord }
1239 , { "primWordToInt", "W", "I", MONAD_Id, i_PRIMOP1, i_wordToInt }
1241 /* Addr# operations */
1242 , { "primGtAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_gtAddr }
1243 , { "primGeAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_geAddr }
1244 , { "primEqAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_eqAddr }
1245 , { "primNeAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_neAddr }
1246 , { "primLtAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_ltAddr }
1247 , { "primLeAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_leAddr }
1248 , { "primIntToAddr", "I", "A", MONAD_Id, i_PRIMOP1, i_intToAddr }
1249 , { "primAddrToInt", "A", "I", MONAD_Id, i_PRIMOP1, i_addrToInt }
1251 , { "primIndexCharOffAddr", "AI", "C", MONAD_Id, i_PRIMOP1, i_indexCharOffAddr }
1252 , { "primIndexIntOffAddr", "AI", "I", MONAD_Id, i_PRIMOP1, i_indexIntOffAddr }
1253 , { "primIndexWordOffAddr", "AI", "W", MONAD_Id, i_PRIMOP1, i_indexWordOffAddr }
1254 , { "primIndexAddrOffAddr", "AI", "A", MONAD_Id, i_PRIMOP1, i_indexAddrOffAddr }
1255 , { "primIndexFloatOffAddr", "AI", "F", MONAD_Id, i_PRIMOP1, i_indexFloatOffAddr }
1256 , { "primIndexDoubleOffAddr", "AI", "D", MONAD_Id, i_PRIMOP1, i_indexDoubleOffAddr }
1257 , { "primIndexStableOffAddr", "AI", "s", MONAD_Id, i_PRIMOP1, i_indexStableOffAddr }
1259 /* Stable# operations */
1260 , { "primIntToStablePtr", "I", "s", MONAD_Id, i_PRIMOP1, i_intToStable }
1261 , { "primStablePtrToInt", "s", "I", MONAD_Id, i_PRIMOP1, i_stableToInt }
1263 /* These ops really ought to be in the IO monad */
1264 , { "primReadCharOffAddr", "AI", "C", MONAD_ST, i_PRIMOP1, i_readCharOffAddr }
1265 , { "primReadIntOffAddr", "AI", "I", MONAD_ST, i_PRIMOP1, i_readIntOffAddr }
1266 , { "primReadWordOffAddr", "AI", "W", MONAD_ST, i_PRIMOP1, i_readWordOffAddr }
1267 , { "primReadAddrOffAddr", "AI", "A", MONAD_ST, i_PRIMOP1, i_readAddrOffAddr }
1268 , { "primReadFloatOffAddr", "AI", "F", MONAD_ST, i_PRIMOP1, i_readFloatOffAddr }
1269 , { "primReadDoubleOffAddr", "AI", "D", MONAD_ST, i_PRIMOP1, i_readDoubleOffAddr }
1270 , { "primReadStableOffAddr", "AI", "s", MONAD_ST, i_PRIMOP1, i_readStableOffAddr }
1272 /* These ops really ought to be in the IO monad */
1273 , { "primWriteCharOffAddr", "AIC", "", MONAD_ST, i_PRIMOP1, i_writeCharOffAddr }
1274 , { "primWriteIntOffAddr", "AII", "", MONAD_ST, i_PRIMOP1, i_writeIntOffAddr }
1275 , { "primWriteWordOffAddr", "AIW", "", MONAD_ST, i_PRIMOP1, i_writeWordOffAddr }
1276 , { "primWriteAddrOffAddr", "AIA", "", MONAD_ST, i_PRIMOP1, i_writeAddrOffAddr }
1277 , { "primWriteFloatOffAddr", "AIF", "", MONAD_ST, i_PRIMOP1, i_writeFloatOffAddr }
1278 , { "primWriteDoubleOffAddr", "AID", "", MONAD_ST, i_PRIMOP1, i_writeDoubleOffAddr }
1279 , { "primWriteStableOffAddr", "AIs", "", MONAD_ST, i_PRIMOP1, i_writeStableOffAddr }
1281 /* Integer operations */
1282 , { "primCompareInteger", "ZZ", "I", MONAD_Id, i_PRIMOP1, i_compareInteger }
1283 , { "primNegateInteger", "Z", "Z", MONAD_Id, i_PRIMOP1, i_negateInteger }
1284 , { "primPlusInteger", "ZZ", "Z", MONAD_Id, i_PRIMOP1, i_plusInteger }
1285 , { "primMinusInteger", "ZZ", "Z", MONAD_Id, i_PRIMOP1, i_minusInteger }
1286 , { "primTimesInteger", "ZZ", "Z", MONAD_Id, i_PRIMOP1, i_timesInteger }
1287 , { "primQuotRemInteger", "ZZ", "ZZ", MONAD_Id, i_PRIMOP1, i_quotRemInteger }
1288 , { "primDivModInteger", "ZZ", "ZZ", MONAD_Id, i_PRIMOP1, i_divModInteger }
1289 , { "primIntegerToInt", "Z", "I", MONAD_Id, i_PRIMOP1, i_integerToInt }
1290 , { "primIntToInteger", "I", "Z", MONAD_Id, i_PRIMOP1, i_intToInteger }
1291 , { "primIntegerToWord", "Z", "W", MONAD_Id, i_PRIMOP1, i_integerToWord }
1292 , { "primWordToInteger", "W", "Z", MONAD_Id, i_PRIMOP1, i_wordToInteger }
1293 , { "primIntegerToFloat", "Z", "F", MONAD_Id, i_PRIMOP1, i_integerToFloat }
1294 , { "primFloatToInteger", "F", "Z", MONAD_Id, i_PRIMOP1, i_floatToInteger }
1295 , { "primIntegerToDouble", "Z", "D", MONAD_Id, i_PRIMOP1, i_integerToDouble }
1296 , { "primDoubleToInteger", "D", "Z", MONAD_Id, i_PRIMOP1, i_doubleToInteger }
1298 /* Float# operations */
1299 , { "primGtFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_gtFloat }
1300 , { "primGeFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_geFloat }
1301 , { "primEqFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_eqFloat }
1302 , { "primNeFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_neFloat }
1303 , { "primLtFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_ltFloat }
1304 , { "primLeFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_leFloat }
1305 , { "primMinFloat", "", "F", MONAD_Id, i_PRIMOP1, i_minFloat }
1306 , { "primMaxFloat", "", "F", MONAD_Id, i_PRIMOP1, i_maxFloat }
1307 , { "primRadixFloat", "", "I", MONAD_Id, i_PRIMOP1, i_radixFloat }
1308 , { "primDigitsFloat", "", "I", MONAD_Id, i_PRIMOP1, i_digitsFloat }
1309 , { "primMinExpFloat", "", "I", MONAD_Id, i_PRIMOP1, i_minExpFloat }
1310 , { "primMaxExpFloat", "", "I", MONAD_Id, i_PRIMOP1, i_maxExpFloat }
1311 , { "primPlusFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_plusFloat }
1312 , { "primMinusFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_minusFloat }
1313 , { "primTimesFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_timesFloat }
1314 , { "primDivideFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_divideFloat }
1315 , { "primNegateFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_negateFloat }
1316 , { "primFloatToInt", "F", "I", MONAD_Id, i_PRIMOP1, i_floatToInt }
1317 , { "primIntToFloat", "I", "F", MONAD_Id, i_PRIMOP1, i_intToFloat }
1318 , { "primExpFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_expFloat }
1319 , { "primLogFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_logFloat }
1320 , { "primSqrtFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_sqrtFloat }
1321 , { "primSinFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_sinFloat }
1322 , { "primCosFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_cosFloat }
1323 , { "primTanFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_tanFloat }
1324 , { "primAsinFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_asinFloat }
1325 , { "primAcosFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_acosFloat }
1326 , { "primAtanFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_atanFloat }
1327 , { "primSinhFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_sinhFloat }
1328 , { "primCoshFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_coshFloat }
1329 , { "primTanhFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_tanhFloat }
1330 , { "primPowerFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_powerFloat }
1331 , { "primDecodeFloatZ", "F", "ZI", MONAD_Id, i_PRIMOP1, i_decodeFloatZ }
1332 , { "primEncodeFloatZ", "ZI", "F", MONAD_Id, i_PRIMOP1, i_encodeFloatZ }
1333 , { "primIsNaNFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isNaNFloat }
1334 , { "primIsInfiniteFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isInfiniteFloat }
1335 , { "primIsDenormalizedFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isDenormalizedFloat }
1336 , { "primIsNegativeZeroFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isNegativeZeroFloat }
1337 , { "primIsIEEEFloat", "", "B", MONAD_Id, i_PRIMOP1, i_isIEEEFloat }
1339 /* Double# operations */
1340 , { "primGtDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_gtDouble }
1341 , { "primGeDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_geDouble }
1342 , { "primEqDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_eqDouble }
1343 , { "primNeDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_neDouble }
1344 , { "primLtDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_ltDouble }
1345 , { "primLeDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_leDouble }
1346 , { "primMinDouble", "", "D", MONAD_Id, i_PRIMOP1, i_minDouble }
1347 , { "primMaxDouble", "", "D", MONAD_Id, i_PRIMOP1, i_maxDouble }
1348 , { "primRadixDouble", "", "I", MONAD_Id, i_PRIMOP1, i_radixDouble }
1349 , { "primDigitsDouble", "", "I", MONAD_Id, i_PRIMOP1, i_digitsDouble }
1350 , { "primMinExpDouble", "", "I", MONAD_Id, i_PRIMOP1, i_minExpDouble }
1351 , { "primMaxExpDouble", "", "I", MONAD_Id, i_PRIMOP1, i_maxExpDouble }
1352 , { "primPlusDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_plusDouble }
1353 , { "primMinusDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_minusDouble }
1354 , { "primTimesDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_timesDouble }
1355 , { "primDivideDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_divideDouble }
1356 , { "primNegateDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_negateDouble }
1357 , { "primDoubleToInt", "D", "I", MONAD_Id, i_PRIMOP1, i_doubleToInt }
1358 , { "primIntToDouble", "I", "D", MONAD_Id, i_PRIMOP1, i_intToDouble }
1359 , { "primDoubleToFloat", "D", "F", MONAD_Id, i_PRIMOP1, i_doubleToFloat }
1360 , { "primFloatToDouble", "F", "D", MONAD_Id, i_PRIMOP1, i_floatToDouble }
1361 , { "primExpDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_expDouble }
1362 , { "primLogDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_logDouble }
1363 , { "primSqrtDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_sqrtDouble }
1364 , { "primSinDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_sinDouble }
1365 , { "primCosDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_cosDouble }
1366 , { "primTanDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_tanDouble }
1367 , { "primAsinDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_asinDouble }
1368 , { "primAcosDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_acosDouble }
1369 , { "primAtanDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_atanDouble }
1370 , { "primSinhDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_sinhDouble }
1371 , { "primCoshDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_coshDouble }
1372 , { "primTanhDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_tanhDouble }
1373 , { "primPowerDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_powerDouble }
1374 , { "primDecodeDoubleZ", "D", "ZI", MONAD_Id, i_PRIMOP1, i_decodeDoubleZ }
1375 , { "primEncodeDoubleZ", "ZI", "D", MONAD_Id, i_PRIMOP1, i_encodeDoubleZ }
1376 , { "primIsNaNDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isNaNDouble }
1377 , { "primIsInfiniteDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isInfiniteDouble }
1378 , { "primIsDenormalizedDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isDenormalizedDouble }
1379 , { "primIsNegativeZeroDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isNegativeZeroDouble }
1380 , { "primIsIEEEDouble", "", "B", MONAD_Id, i_PRIMOP1, i_isIEEEDouble }
1382 /* Ref operations */
1383 , { "primNewRef", "a", "R", MONAD_ST, i_PRIMOP2, i_newRef }
1384 , { "primWriteRef", "Ra", "", MONAD_ST, i_PRIMOP2, i_writeRef }
1385 , { "primReadRef", "R", "a", MONAD_ST, i_PRIMOP2, i_readRef }
1386 , { "primSameRef", "RR", "B", MONAD_Id, i_PRIMOP2, i_sameRef }
1388 /* PrimArray operations */
1389 , { "primSameMutableArray", "MM", "B", MONAD_Id, i_PRIMOP2, i_sameMutableArray }
1390 , { "primUnsafeFreezeArray", "M", "X", MONAD_ST, i_PRIMOP2, i_unsafeFreezeArray }
1391 , { "primNewArray", "Ia", "M", MONAD_ST, i_PRIMOP2, i_newArray }
1392 , { "primWriteArray", "MIa", "", MONAD_ST, i_PRIMOP2, i_writeArray }
1393 , { "primReadArray", "MI", "a", MONAD_ST, i_PRIMOP2, i_readArray }
1394 , { "primIndexArray", "XI", "a", MONAD_Id, i_PRIMOP2, i_indexArray }
1395 , { "primSizeArray", "X", "I", MONAD_Id, i_PRIMOP2, i_sizeArray }
1396 , { "primSizeMutableArray", "M", "I", MONAD_Id, i_PRIMOP2, i_sizeMutableArray }
1398 /* Prim[Mutable]ByteArray operations */
1399 , { "primSameMutableByteArray", "mm", "B", MONAD_Id, i_PRIMOP2, i_sameMutableByteArray }
1400 , { "primUnsafeFreezeByteArray", "m", "x", MONAD_ST, i_PRIMOP2, i_unsafeFreezeByteArray }
1402 , { "primNewByteArray", "I", "m", MONAD_ST, i_PRIMOP2, i_newByteArray }
1404 , { "primWriteCharArray", "mIC", "", MONAD_ST, i_PRIMOP2, i_writeCharArray }
1405 , { "primReadCharArray", "mI", "C", MONAD_ST, i_PRIMOP2, i_readCharArray }
1406 , { "primIndexCharArray", "xI", "C", MONAD_Id, i_PRIMOP2, i_indexCharArray }
1408 , { "primWriteIntArray", "mII", "", MONAD_ST, i_PRIMOP2, i_writeIntArray }
1409 , { "primReadIntArray", "mI", "I", MONAD_ST, i_PRIMOP2, i_readIntArray }
1410 , { "primIndexIntArray", "xI", "I", MONAD_Id, i_PRIMOP2, i_indexIntArray }
1412 /* {new,write,read,index}IntegerArray not provided */
1414 , { "primWriteWordArray", "mIW", "", MONAD_ST, i_PRIMOP2, i_writeWordArray }
1415 , { "primReadWordArray", "mI", "W", MONAD_ST, i_PRIMOP2, i_readWordArray }
1416 , { "primIndexWordArray", "xI", "W", MONAD_Id, i_PRIMOP2, i_indexWordArray }
1417 , { "primWriteAddrArray", "mIA", "", MONAD_ST, i_PRIMOP2, i_writeAddrArray }
1418 , { "primReadAddrArray", "mI", "A", MONAD_ST, i_PRIMOP2, i_readAddrArray }
1419 , { "primIndexAddrArray", "xI", "A", MONAD_Id, i_PRIMOP2, i_indexAddrArray }
1420 , { "primWriteFloatArray", "mIF", "", MONAD_ST, i_PRIMOP2, i_writeFloatArray }
1421 , { "primReadFloatArray", "mI", "F", MONAD_ST, i_PRIMOP2, i_readFloatArray }
1422 , { "primIndexFloatArray", "xI", "F", MONAD_Id, i_PRIMOP2, i_indexFloatArray }
1423 , { "primWriteDoubleArray" , "mID", "", MONAD_ST, i_PRIMOP2, i_writeDoubleArray }
1424 , { "primReadDoubleArray", "mI", "D", MONAD_ST, i_PRIMOP2, i_readDoubleArray }
1425 , { "primIndexDoubleArray", "xI", "D", MONAD_Id, i_PRIMOP2, i_indexDoubleArray }
1428 #ifdef PROVIDE_STABLE
1429 , { "primWriteStableArray", "mIs", "", MONAD_ST, i_PRIMOP2, i_writeStableArray }
1430 , { "primReadStableArray", "mI", "s", MONAD_ST, i_PRIMOP2, i_readStableArray }
1431 , { "primIndexStableArray", "xI", "s", MONAD_Id, i_PRIMOP2, i_indexStableArray }
1434 /* {new,write,read,index}ForeignObjArray not provided */
1437 #ifdef PROVIDE_FOREIGN
1438 /* ForeignObj# operations */
1439 , { "primMkForeignObj", "A", "f", MONAD_IO, i_PRIMOP2, i_mkForeignObj }
1442 /* WeakPair# operations */
1443 , { "primMakeWeak", "bac", "w", MONAD_IO, i_PRIMOP2, i_makeWeak }
1444 , { "primDeRefWeak", "w", "Ia", MONAD_IO, i_PRIMOP2, i_deRefWeak }
1446 /* StablePtr# operations */
1447 , { "primMakeStablePtr", "a", "s", MONAD_IO, i_PRIMOP2, i_makeStablePtr }
1448 , { "primDeRefStablePtr", "s", "a", MONAD_IO, i_PRIMOP2, i_deRefStablePtr }
1449 , { "primFreeStablePtr", "s", "", MONAD_IO, i_PRIMOP2, i_freeStablePtr }
1451 /* foreign export dynamic support */
1452 , { "primCreateAdjThunkARCH", "sAC","A", MONAD_IO, i_PRIMOP2, i_createAdjThunkARCH }
1454 /* misc handy hacks */
1455 , { "primGetArgc", "", "I", MONAD_IO, i_PRIMOP2, i_getArgc }
1456 , { "primGetArgv", "I", "A", MONAD_IO, i_PRIMOP2, i_getArgv }
1458 #ifdef PROVIDE_PTREQUALITY
1459 , { "primReallyUnsafePtrEquality", "aa", "B",MONAD_Id, i_PRIMOP2, i_reallyUnsafePtrEquality }
1461 #ifdef PROVIDE_COERCE
1462 , { "primUnsafeCoerce", "a", "b", MONAD_Id, i_PRIMOP2, i_unsafeCoerce }
1464 #ifdef PROVIDE_CONCURRENT
1465 /* Concurrency operations */
1466 , { "primForkIO", "a", "T", MONAD_IO, i_PRIMOP2, i_forkIO }
1467 , { "primKillThread", "T", "", MONAD_IO, i_PRIMOP2, i_killThread }
1468 , { "primRaiseInThread", "TE", "", MONAD_IO, i_PRIMOP2, i_raiseInThread }
1470 , { "primWaitRead", "I", "", MONAD_IO, i_PRIMOP2, i_waitRead }
1471 , { "primWaitWrite", "I", "", MONAD_IO, i_PRIMOP2, i_waitWrite }
1472 , { "primYield", "", "", MONAD_IO, i_PRIMOP2, i_yield } , { "primDelay", "I", "", MONAD_IO, i_PRIMOP2, i_delay }
1473 , { "primGetThreadId", "", "T", MONAD_IO, i_PRIMOP2, i_getThreadId }
1474 , { "primCmpThreadIds", "TT", "I", MONAD_Id, i_PRIMOP2, i_cmpThreadIds }
1476 , { "primNewEmptyMVar", "", "r", MONAD_IO, i_PRIMOP2, i_newMVar }
1477 /* primTakeMVar is handwritten bytecode */
1478 , { "primPutMVar", "ra", "", MONAD_IO, i_PRIMOP2, i_putMVar }
1479 , { "primSameMVar", "rr", "B", MONAD_Id, i_PRIMOP2, i_sameMVar }
1482 /* Ccall is polyadic - so it's excluded from this table */
1487 AsmPrim ccall_ccall_Id
1488 = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_ccall_Id };
1489 AsmPrim ccall_ccall_IO
1490 = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_ccall_IO };
1491 AsmPrim ccall_stdcall_Id
1492 = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_stdcall_Id };
1493 AsmPrim ccall_stdcall_IO
1494 = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_stdcall_IO };
1497 void checkBytecodeCount( void );
1498 void checkBytecodeCount( void )
1500 if (MAX_Primop1 >= 255) {
1501 printf("Too many Primop1 bytecodes (%d)\n",MAX_Primop1);
1503 if (MAX_Primop2 >= 255) {
1504 printf("Too many Primop2 bytecodes (%d)\n",MAX_Primop2);
1509 AsmPrim* asmFindPrim( char* s )
1512 for (i=0; asmPrimOps[i].name; ++i) {
1513 if (strcmp(s,asmPrimOps[i].name)==0) {
1514 return &asmPrimOps[i];
1520 AsmPrim* asmFindPrimop( AsmInstr prefix, AsmInstr op )
1523 for (i=0; asmPrimOps[i].name; ++i) {
1524 if (asmPrimOps[i].prefix == prefix && asmPrimOps[i].opcode == op) {
1525 return &asmPrimOps[i];
1531 /* --------------------------------------------------------------------------
1532 * Handwritten primops
1533 * ------------------------------------------------------------------------*/
1535 void* /* StgBCO* */ asm_BCO_catch ( void )
1541 bco = asmBeginBCO(0 /*NIL*/);
1542 emiti_8(bco,i_ARG_CHECK,2);
1543 emiti_8(bco,i_PRIMOP1,i_pushcatchframe);
1544 incSp(bco, (1-2)*sizeofW(StgPtr) + sizeofW(StgCatchFrame));
1545 emiti_(bco,i_ENTER);
1546 decSp(bco, sizeofW(StgPtr));
1549 asmAllocateHeapSpace();
1551 closure = (StgBCO*)(bco->closure);
1556 void* /* StgBCO* */ asm_BCO_raise ( void )
1562 bco = asmBeginBCO(0 /*NIL*/);
1563 emiti_8(bco,i_ARG_CHECK,1);
1564 emiti_8(bco,i_PRIMOP2,i_raise);
1565 decSp(bco,sizeofW(StgPtr));
1568 asmAllocateHeapSpace();
1570 closure = (StgBCO*)(bco->closure);
1575 void* /* StgBCO* */ asm_BCO_seq ( void )
1581 cont = asmBeginBCO(0 /*NIL*/);
1582 emiti_8(cont,i_ARG_CHECK,2); /* should never fail */
1584 emit_i_SLIDE(cont,1,2);
1585 emiti_(cont,i_ENTER);
1586 incSp(cont, 3*sizeofW(StgPtr));
1589 eval = asmBeginBCO(0 /*NIL*/);
1590 emiti_8(eval,i_ARG_CHECK,2);
1591 emit_i_RETADDR(eval,eval->n_refs);
1592 asmAddRefObject(eval,cont);
1594 emit_i_SLIDE(eval,3,1);
1595 emiti_8(eval,i_PRIMOP1,i_pushseqframe);
1596 emiti_(eval,i_ENTER);
1597 incSp(eval, sizeofW(StgSeqFrame) + 4*sizeofW(StgPtr));
1600 asmAllocateHeapSpace();
1602 closure = (StgBCO*)(eval->closure);
1607 void* /* StgBCO* */ asm_BCO_takeMVar ( void )
1609 AsmBCO kase, casecont, take;
1613 take = asmBeginBCO(0 /*NIL*/);
1615 emiti_8(take,i_PRIMOP2,i_takeMVar);
1619 emit_i_SLIDE(take,3,4);
1620 emiti_(take,i_ENTER);
1624 casecont = asmBeginBCO(0 /*NIL*/);
1625 emiti_(casecont,i_UNPACK);
1626 emit_i_VAR(casecont,4);
1627 emit_i_VAR(casecont,4);
1628 emit_i_VAR(casecont,2);
1629 emit_i_CONST(casecont,casecont->n_refs);
1630 asmAddRefObject(casecont,take);
1631 emit_i_SLIDE(casecont,4,5);
1632 emiti_(casecont,i_ENTER);
1634 asmEndBCO(casecont);
1636 kase = asmBeginBCO(0 /*NIL*/);
1637 emiti_8(kase,i_ARG_CHECK,3);
1638 emit_i_RETADDR(kase,kase->n_refs);
1639 asmAddRefObject(kase,casecont);
1641 emiti_(kase,i_ENTER);
1645 asmAllocateHeapSpace();
1647 closure = (StgBCO*)(kase->closure);
1653 /* --------------------------------------------------------------------------
1655 * ------------------------------------------------------------------------*/
1657 AsmVar asmAllocCONSTR ( AsmBCO bco, AsmInfo info )
1660 ASSERT( sizeW_fromITBL(info) >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
1662 /* Look in this bco's collection of nonpointers (literals)
1663 to see if the itbl pointer is already there. If so, re-use it. */
1664 i = asmFindInNonPtrs ( bco, (StgWord)info );
1667 emit_i_ALLOC_CONSTR(bco,bco->n_words);
1668 asmAddNonPtrWords(bco,AsmInfo,info);
1670 emit_i_ALLOC_CONSTR(bco,i);
1673 incSp(bco, sizeofW(StgClosurePtr));
1677 AsmSp asmBeginPack( AsmBCO bco )
1682 void asmEndPack( AsmBCO bco, AsmVar v, AsmSp start, AsmInfo info )
1684 nat size = bco->sp - start;
1685 assert(bco->sp >= start);
1687 /* only reason to include info is for this assertion */
1688 assert(info->layout.payload.ptrs == size);
1689 emit_i_PACK(bco, bco->sp - v);
1693 void asmBeginUnpack( AsmBCO bco )
1695 /* dummy to make it look prettier */
1698 void asmEndUnpack( AsmBCO bco )
1700 emiti_(bco,i_UNPACK);
1703 AsmVar asmAllocAP( AsmBCO bco, AsmNat words )
1705 emiti_8(bco,i_ALLOC_AP,words);
1706 incSp(bco, sizeofW(StgPtr));
1710 AsmSp asmBeginMkAP( AsmBCO bco )
1715 void asmEndMkAP( AsmBCO bco, AsmVar v, AsmSp start )
1717 emit_i_MKAP(bco,bco->sp-v,bco->sp-start-1);
1718 /* -1 because fun isn't counted */
1722 AsmVar asmAllocPAP( AsmBCO bco, AsmNat size )
1724 emiti_8(bco,i_ALLOC_PAP,size);
1725 incSp(bco, sizeofW(StgPtr));
1729 AsmSp asmBeginMkPAP( AsmBCO bco )
1734 void asmEndMkPAP( AsmBCO bco, AsmVar v, AsmSp start )
1736 emiti_8_8(bco,i_MKPAP,bco->sp-v,bco->sp-start-1);
1737 /* -1 because fun isn't counted */
1741 AsmVar asmPushRefHugs ( AsmBCO bco, int /*Name*/ n )
1743 emit_i_CONST(bco,bco->n_refs);
1744 asmAddRefHugs(bco,n);
1745 incSp(bco, sizeofW(StgPtr));
1749 AsmVar asmPushRefObject ( AsmBCO bco, AsmObject p )
1751 emit_i_CONST(bco,bco->n_refs);
1752 asmAddRefObject(bco,p);
1753 incSp(bco, sizeofW(StgPtr));
1757 AsmVar asmPushRefNoOp ( AsmBCO bco, StgPtr p )
1759 emit_i_CONST(bco,bco->n_refs);
1760 asmAddRefNoOp(bco,p);
1761 incSp(bco, sizeofW(StgPtr));
1766 /* --------------------------------------------------------------------------
1767 * Building InfoTables
1768 * ------------------------------------------------------------------------*/
1770 AsmInfo asmMkInfo( AsmNat tag, AsmNat ptrs )
1772 StgInfoTable* info = stgMallocBytes( sizeof(StgInfoTable),"asmMkInfo");
1773 /* Note: the evaluator automatically pads objects with the right number
1774 * of non-ptrs to satisfy MIN_NONUPD_SIZE restrictions.
1776 AsmNat nptrs = stg_max(0,MIN_NONUPD_SIZE-ptrs);
1778 /* initialisation code based on INFO_TABLE_CONSTR */
1779 info->layout.payload.ptrs = ptrs;
1780 info->layout.payload.nptrs = nptrs;
1781 info->srt_len = tag;
1782 info->type = CONSTR;
1783 #ifdef USE_MINIINTERPRETER
1784 info->entry = stgCast(StgFunPtr,&Hugs_CONSTR_entry);
1786 #warning asmMkInfo: Need to insert entry code in some cunning way
1788 ASSERT( sizeW_fromITBL(info) >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
1792 /*-------------------------------------------------------------------------*/
1794 #endif /* INTERPRETER */