2 /* --------------------------------------------------------------------------
5 * Copyright (c) 1994-1998.
7 * $RCSfile: Assembler.c,v $
9 * $Date: 2000/05/10 09:00:20 $
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 /* Defined in this file ... */
66 AsmObject asmNewObject ( void );
67 void asmAddEntity ( AsmObject, Asm_Kind, StgWord );
68 int asmCalcHeapSizeW ( AsmObject );
69 StgClosure* asmDerefEntity ( Asm_Entity );
71 /* --------------------------------------------------------------------------
72 * Initialising and managing objects and entities
73 * ------------------------------------------------------------------------*/
75 static struct AsmObject_* objects;
77 #define INITIALISE_TABLE(Type,table,size,used) \
81 #define ENSURE_SPACE_IN_TABLE(Type,table,size,used) \
84 size = (size ? 2*size : 1); \
85 new = malloc ( size * sizeof(Type)); \
87 barf("bytecode assembler: can't expand table of type " \
89 memcpy ( new, table, used * sizeof(Type) ); \
90 if (table) free(table); \
94 void asmInitialise ( void )
100 AsmObject asmNewObject ( void )
102 AsmObject obj = malloc(sizeof(struct AsmObject_));
104 barf("bytecode assembler: can't malloc in asmNewObject");
107 obj->n_refs = obj->n_words = obj->n_insns = 0;
109 obj->magic = 0x31415927;
110 INITIALISE_TABLE(AsmEntity,obj->entities,
117 void asmAddEntity ( AsmObject obj,
121 ENSURE_SPACE_IN_TABLE(
122 Asm_Entity,obj->entities,
123 obj->sizeEntities,obj->usedEntities);
124 obj->entities[obj->usedEntities].kind = kind;
125 obj->entities[obj->usedEntities].val = val;
128 case Asm_RefNoOp: case Asm_RefObject: case Asm_RefHugs:
129 obj->n_refs++; break;
131 obj->n_words++; break;
133 obj->n_insns++; break;
135 barf("asmAddEntity");
139 static int asmFindInNonPtrs ( AsmBCO bco, StgWord w )
142 for (i = 0; i < bco->usedEntities; i++) {
143 if (bco->entities[i].kind == Asm_NonPtrWord) {
144 if (bco->entities[i].val == w) return j;
151 static void setInstrs ( AsmBCO bco, int instr_no, StgWord new_instr_byte )
154 for (i = 0; i < bco->usedEntities; i++) {
155 if (bco->entities[i].kind == Asm_Insn8) {
157 bco->entities[i].val = new_instr_byte;
166 void* asmGetClosureOfObject ( AsmObject obj )
172 /* --------------------------------------------------------------------------
173 * Top level assembler/BCO linker functions
174 * ------------------------------------------------------------------------*/
176 int asmCalcHeapSizeW ( AsmObject obj )
183 is = obj->n_insns + (obj->max_sp <= 255 ? 2 : 3);
184 ws = BCO_sizeW ( p, np, is );
192 ws = CONSTR_sizeW ( p, np );
195 barf("asmCalcHeapSizeW");
197 if (ws - sizeofW(StgHeader) < MIN_NONUPD_SIZE)
198 ws = sizeofW(StgHeader) + MIN_NONUPD_SIZE;
203 void asmAllocateHeapSpace ( void )
206 for (obj = objects; obj; obj = obj->next) {
207 StgClosure* c = asmAlloc ( asmCalcHeapSizeW ( obj ) );
212 void asmShutdown ( void )
215 AsmObject next = NULL;
216 for (obj = objects; obj; obj = next) {
218 obj->magic = 0x27180828;
219 if ( /*paranoia*/ obj->entities)
226 StgClosure* asmDerefEntity ( Asm_Entity entity )
228 switch (entity.kind) {
230 return (StgClosure*)entity.val;
233 ASSERT( ((AsmObject)(entity.val))->magic == 0x31415927 );
234 return ((AsmObject)(entity.val))->closure;
236 return getNameOrTupleClosureCPtr(entity.val);
238 barf("asmDerefEntity");
240 return NULL; /*notreached*/
243 void asmCopyAndLink ( void )
248 for (obj = objects; obj; obj = obj->next) {
249 StgClosure** p = (StgClosure**)(obj->closure);
255 AsmBCO abco = (AsmBCO)obj;
256 StgBCO* bco = (StgBCO*)p;
257 SET_HDR(bco,&BCO_info,??);
258 bco->n_ptrs = abco->n_refs;
259 bco->n_words = abco->n_words;
260 bco->n_instrs = abco->n_insns + (obj->max_sp <= 255 ? 2 : 3);
261 bco->stgexpr = abco->stgexpr;
263 /* First copy in the ptrs. */
265 for (j = 0; j < obj->usedEntities; j++) {
266 switch (obj->entities[j].kind) {
270 bcoConstCPtr(bco,k++)
271 = (StgClosure*)asmDerefEntity(obj->entities[j]); break;
277 /* Now the non-ptrs. */
279 for (j = 0; j < obj->usedEntities; j++) {
280 switch (obj->entities[j].kind) {
282 bcoConstWord(bco,k++) = obj->entities[j].val; break;
288 /* Finally the insns, adding a stack check at the start. */
290 abco->max_sp = stg_max(abco->sp,abco->max_sp);
292 ASSERT(abco->max_sp <= 65535);
293 if (abco->max_sp <= 255) {
294 bcoInstr(bco,k++) = i_STK_CHECK;
295 bcoInstr(bco,k++) = abco->max_sp;
297 bcoInstr(bco,k++) = i_STK_CHECK_big;
298 bcoInstr(bco,k++) = abco->max_sp / 256;
299 bcoInstr(bco,k++) = abco->max_sp % 256;
301 for (j = 0; j < obj->usedEntities; j++) {
302 switch (obj->entities[j].kind) {
304 bcoInstr(bco,k++) = obj->entities[j].val; break;
311 barf("asmCopyAndLink: strange stuff in AsmBCO");
315 ASSERT((unsigned int)k == bco->n_instrs);
320 StgCAF* caf = (StgCAF*)p;
321 SET_HDR(caf,&CAF_UNENTERED_info,??);
323 caf->mut_link = NULL;
324 caf->value = (StgClosure*)0xdeadbeef;
325 ASSERT(obj->usedEntities == 1);
326 switch (obj->entities[0].kind) {
330 caf->body = (StgClosure*)asmDerefEntity(obj->entities[0]);
333 barf("asmCopyAndLink: strange stuff in AsmCAF");
340 SET_HDR((StgClosure*)p,obj->itbl,??);
342 /* First put in the pointers, then the non-pointers. */
343 for (j = 0; j < obj->usedEntities; j++) {
344 switch (obj->entities[j].kind) {
348 *p++ = asmDerefEntity(obj->entities[j]); break;
353 for (j = 0; j < obj->usedEntities; j++) {
354 switch (obj->entities[j].kind) {
356 *p++ = (StgClosure*)(obj->entities[j].val); break;
358 barf("asmCopyAndLink: strange stuff in AsmCon");
365 barf("asmCopyAndLink");
371 /* --------------------------------------------------------------------------
372 * Keeping track of the simulated stack pointer
373 * ------------------------------------------------------------------------*/
375 static StgClosure* asmAlloc( nat size )
377 StgClosure* o = stgCast(StgClosure*,allocate(size));
378 ASSERT( size >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
379 /* printf("Allocated %p .. %p\n", o, o+size-1); */
383 static void setSp( AsmBCO bco, AsmSp sp )
385 bco->max_sp = stg_max(bco->sp,bco->max_sp);
387 bco->max_sp = stg_max(bco->sp,bco->max_sp);
390 static void incSp ( AsmBCO bco, int sp_delta )
392 bco->max_sp = stg_max(bco->sp,bco->max_sp);
394 bco->max_sp = stg_max(bco->sp,bco->max_sp);
397 static void decSp ( AsmBCO bco, int sp_delta )
399 bco->max_sp = stg_max(bco->sp,bco->max_sp);
401 bco->max_sp = stg_max(bco->sp,bco->max_sp);
404 /* --------------------------------------------------------------------------
406 * ------------------------------------------------------------------------*/
408 AsmCon asmBeginCon( AsmInfo info )
410 AsmCon con = asmNewObject();
416 void asmEndCon( AsmCon con __attribute__ ((unused)) )
420 AsmCAF asmBeginCAF( void )
422 AsmCAF caf = asmNewObject();
427 void asmEndCAF( AsmCAF caf __attribute__ ((unused)) )
431 AsmBCO asmBeginBCO( int /*StgExpr*/ e )
433 AsmBCO bco = asmNewObject();
438 bco->lastOpc = i_INTERNAL_ERROR;
442 void asmEndBCO( AsmBCO bco __attribute__ ((unused)) )
446 /* --------------------------------------------------------------------------
448 * ------------------------------------------------------------------------*/
450 static void asmAddInstr ( AsmBCO bco, StgWord i )
452 asmAddEntity ( bco, Asm_Insn8, i );
455 static void asmAddNonPtrWord ( AsmObject obj, StgWord i )
457 asmAddEntity ( obj, Asm_NonPtrWord, i );
460 void asmAddRefHugs ( AsmObject obj,int /*Name*/ n )
462 asmAddEntity ( obj, Asm_RefHugs, n );
465 void asmAddRefObject ( AsmObject obj, AsmObject p )
467 ASSERT(p->magic == 0x31415927);
468 asmAddEntity ( obj, Asm_RefObject, (StgWord)p );
471 void asmAddRefNoOp ( AsmObject obj, StgPtr p )
473 asmAddEntity ( obj, Asm_RefNoOp, (StgWord)p );
478 static void asmInstrOp ( AsmBCO bco, StgWord i )
480 ASSERT(i <= BIGGEST_OPCODE); /* must be a valid opcode */
485 static void asmInstr8 ( AsmBCO bco, StgWord i )
488 ASSERT(i < 256); /* must be a byte */
493 static void asmInstr16 ( AsmBCO bco, StgWord i )
495 ASSERT(i < 65536); /* must be a short */
496 asmAddInstr(bco,i / 256);
497 asmAddInstr(bco,i % 256);
501 static Instr asmInstrBack ( AsmBCO bco, StgWord n )
503 return bco->is.elems[bco->is.len - n];
506 static void asmInstrRecede ( AsmBCO bco, StgWord n )
508 if (bco->is.len < n) barf("asmInstrRecede");
513 #define asmAddNonPtrWords(bco,ty,x) \
515 union { ty a; AsmWord b[sizeofW(ty)]; } p; \
517 if (sizeof(ty) < sizeof(AsmWord)) p.b[0]=0; \
519 for( i = 0; i < sizeofW(ty); i++ ) { \
520 asmAddNonPtrWord(bco,p.b[i]); \
524 static StgWord repSizeW( AsmRep rep )
527 case CHAR_REP: return sizeofW(StgWord) + sizeofW(StgChar);
530 case INT_REP: return sizeofW(StgWord) + sizeofW(StgInt);
532 case WORD_REP: return sizeofW(StgWord) + sizeofW(StgWord);
533 case ADDR_REP: return sizeofW(StgWord) + sizeofW(StgAddr);
534 case FLOAT_REP: return sizeofW(StgWord) + sizeofW(StgFloat);
535 case DOUBLE_REP: return sizeofW(StgWord) + sizeofW(StgDouble);
536 case STABLE_REP: return sizeofW(StgWord) + sizeofW(StgWord);
542 #ifdef PROVIDE_FOREIGN
545 case ALPHA_REP: /* a */
546 case BETA_REP: /* b */
547 case GAMMA_REP: /* c */
548 case DELTA_REP: /* d */
549 case HANDLER_REP: /* IOError -> IO a */
550 case ERROR_REP: /* IOError */
551 case ARR_REP : /* PrimArray a */
552 case BARR_REP : /* PrimByteArray a */
553 case REF_REP : /* Ref s a */
554 case MUTARR_REP : /* PrimMutableArray s a */
555 case MUTBARR_REP: /* PrimMutableByteArray s a */
556 case MVAR_REP: /* MVar a */
557 case PTR_REP: return sizeofW(StgPtr);
559 case VOID_REP: return sizeofW(StgWord);
560 default: barf("repSizeW %d",rep);
565 int asmRepSizeW ( AsmRep rep )
567 return repSizeW ( rep );
571 /* --------------------------------------------------------------------------
572 * Instruction emission. All instructions should be routed through here
573 * so that the peephole optimiser gets to see what's happening.
574 * ------------------------------------------------------------------------*/
576 static void emiti_ ( AsmBCO bco, Instr opcode )
580 if (bco->lastOpc == i_SLIDE && opcode == i_ENTER) {
581 /* SLIDE x y ; ENTER ===> SE x y */
582 x = asmInstrBack(bco,2);
583 y = asmInstrBack(bco,1);
584 asmInstrRecede(bco,3);
585 asmInstrOp(bco,i_SE); asmInstr8(bco,x); asmInstr8(bco,y);
588 if (bco->lastOpc == i_RV && opcode == i_ENTER) {
589 /* RV x y ; ENTER ===> RVE x (y-2)
590 Because RETADDR pushes 2 words on the stack, y must be at least 2. */
591 x = asmInstrBack(bco,2);
592 y = asmInstrBack(bco,1);
593 if (y < 2) barf("emiti_: RVE: impossible y value");
594 asmInstrRecede(bco,3);
595 asmInstrOp(bco, i_RVE); asmInstr8(bco,x); asmInstr8(bco,y-2);
598 asmInstrOp(bco,opcode);
601 asmInstrOp(bco,opcode);
605 static void emiti_8 ( AsmBCO bco, Instr opcode, int arg1 )
609 if (bco->lastOpc == i_VAR && opcode == i_VAR) {
610 /* VAR x ; VAR y ===> VV x y */
611 x = asmInstrBack(bco,1);
612 asmInstrRecede(bco,2);
613 asmInstrOp(bco,i_VV); asmInstr8(bco,x); asmInstr8(bco,arg1);
616 if (bco->lastOpc == i_RETADDR && opcode == i_VAR) {
617 /* RETADDR x ; VAR y ===> RV x y */
618 x = asmInstrBack(bco,1);
619 asmInstrRecede(bco,2);
620 asmInstrOp(bco, i_RV); asmInstr8(bco,x); asmInstr8(bco,arg1);
623 asmInstrOp(bco,opcode);
627 asmInstrOp(bco,opcode);
632 static void emiti_16 ( AsmBCO bco, Instr opcode, int arg1 )
634 asmInstrOp(bco,opcode);
635 asmInstr16(bco,arg1);
638 static void emiti_8_8 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
640 asmInstrOp(bco,opcode);
645 static void emiti_8_16 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
647 asmInstrOp(bco,opcode);
649 asmInstr16(bco,arg2);
652 static void emiti_16_16 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
654 asmInstrOp(bco,opcode);
655 asmInstr16(bco,arg1);
656 asmInstr16(bco,arg2);
660 /* --------------------------------------------------------------------------
661 * Wrappers around the above fns
662 * ------------------------------------------------------------------------*/
664 static void emit_i_VAR_INT ( AsmBCO bco, int arg1 )
668 emiti_8 (bco,i_VAR_INT, arg1); else
669 emiti_16(bco,i_VAR_INT_big,arg1);
672 static void emit_i_VAR_WORD ( AsmBCO bco, int arg1 )
676 emiti_8 (bco,i_VAR_WORD, arg1); else
677 emiti_16(bco,i_VAR_WORD_big,arg1);
680 static void emit_i_VAR_ADDR ( AsmBCO bco, int arg1 )
684 emiti_8 (bco,i_VAR_ADDR, arg1); else
685 emiti_16(bco,i_VAR_ADDR_big,arg1);
688 static void emit_i_VAR_CHAR ( AsmBCO bco, int arg1 )
692 emiti_8 (bco,i_VAR_CHAR, arg1); else
693 emiti_16(bco,i_VAR_CHAR_big,arg1);
696 static void emit_i_VAR_FLOAT ( AsmBCO bco, int arg1 )
700 emiti_8 (bco,i_VAR_FLOAT, arg1); else
701 emiti_16(bco,i_VAR_FLOAT_big,arg1);
704 static void emit_i_VAR_DOUBLE ( AsmBCO bco, int arg1 )
708 emiti_8 (bco,i_VAR_DOUBLE, arg1); else
709 emiti_16(bco,i_VAR_DOUBLE_big,arg1);
712 static void emit_i_VAR_STABLE ( AsmBCO bco, int arg1 )
716 emiti_8 (bco,i_VAR_STABLE, arg1); else
717 emiti_16(bco,i_VAR_STABLE_big,arg1);
720 static void emit_i_VAR ( AsmBCO bco, int arg1 )
724 emiti_8 (bco,i_VAR, arg1); else
725 emiti_16(bco,i_VAR_big,arg1);
728 static void emit_i_PACK ( AsmBCO bco, int arg1 )
732 emiti_8 (bco,i_PACK, arg1); else
733 emiti_16(bco,i_PACK_big,arg1);
736 static void emit_i_SLIDE ( AsmBCO bco, int arg1, int arg2 )
740 if (arg1 < 256 && arg2 < 256)
741 emiti_8_8 (bco,i_SLIDE, arg1,arg2); else
742 emiti_16_16(bco,i_SLIDE_big,arg1,arg2);
745 static void emit_i_MKAP ( AsmBCO bco, int arg1, int arg2 )
749 if (arg1 < 256 && arg2 < 256)
750 emiti_8_8 (bco,i_MKAP, arg1,arg2); else
751 emiti_16_16(bco,i_MKAP_big,arg1,arg2);
755 static void emit_i_CONST_INT ( AsmBCO bco, int arg1 )
759 emiti_8 (bco,i_CONST_INT, arg1); else
760 emiti_16(bco,i_CONST_INT_big,arg1);
763 static void emit_i_CONST_INTEGER ( AsmBCO bco, int arg1 )
767 emiti_8 (bco,i_CONST_INTEGER, arg1); else
768 emiti_16(bco,i_CONST_INTEGER_big,arg1);
771 static void emit_i_CONST_ADDR ( AsmBCO bco, int arg1 )
775 emiti_8 (bco,i_CONST_ADDR, arg1); else
776 emiti_16(bco,i_CONST_ADDR_big,arg1);
779 static void emit_i_CONST_CHAR ( AsmBCO bco, int arg1 )
783 emiti_8 (bco,i_CONST_CHAR, arg1); else
784 emiti_16(bco,i_CONST_CHAR_big,arg1);
787 static void emit_i_CONST_FLOAT ( AsmBCO bco, int arg1 )
791 emiti_8 (bco,i_CONST_FLOAT, arg1); else
792 emiti_16(bco,i_CONST_FLOAT_big,arg1);
795 static void emit_i_CONST_DOUBLE ( AsmBCO bco, int arg1 )
799 emiti_8 (bco,i_CONST_DOUBLE, arg1); else
800 emiti_16(bco,i_CONST_DOUBLE_big,arg1);
803 static void emit_i_CONST ( AsmBCO bco, int arg1 )
807 emiti_8 (bco,i_CONST, arg1); else
808 emiti_16(bco,i_CONST_big,arg1);
811 static void emit_i_RETADDR ( AsmBCO bco, int arg1 )
815 emiti_8 (bco,i_RETADDR, arg1); else
816 emiti_16(bco,i_RETADDR_big,arg1);
819 static void emit_i_ALLOC_CONSTR ( AsmBCO bco, int arg1 )
823 emiti_8 (bco,i_ALLOC_CONSTR, arg1); else
824 emiti_16(bco,i_ALLOC_CONSTR_big,arg1);
827 /* --------------------------------------------------------------------------
829 * ------------------------------------------------------------------------*/
831 AsmSp asmBeginArgCheck ( AsmBCO bco )
833 ASSERT(bco->sp == 0);
837 void asmEndArgCheck ( AsmBCO bco, AsmSp last_arg )
839 nat args = bco->sp - last_arg;
840 if (args != 0) { /* optimisation */
841 emiti_8(bco,i_ARG_CHECK,args);
845 /* --------------------------------------------------------------------------
846 * Creating and using "variables"
847 * ------------------------------------------------------------------------*/
849 AsmVar asmBind ( AsmBCO bco, AsmRep rep )
851 incSp(bco,repSizeW(rep));
855 void asmVar ( AsmBCO bco, AsmVar v, AsmRep rep )
859 if (rep == VOID_REP) {
861 incSp(bco,repSizeW(rep));
865 offset = bco->sp - v;
869 emit_i_VAR_INT(bco,offset);
873 emit_i_VAR_WORD(bco,offset);
876 emit_i_VAR_ADDR(bco,offset);
879 emit_i_VAR_CHAR(bco,offset);
882 emit_i_VAR_FLOAT(bco,offset);
885 emit_i_VAR_DOUBLE(bco,offset);
888 emit_i_VAR_STABLE(bco,offset);
895 #ifdef PROVIDE_FOREIGN
898 case ALPHA_REP: /* a */
899 case BETA_REP: /* b */
900 case GAMMA_REP: /* c */
901 case DELTA_REP: /* d */
902 case HANDLER_REP: /* IOError -> IO a */
903 case ERROR_REP: /* IOError */
904 case ARR_REP : /* PrimArray a */
905 case BARR_REP : /* PrimByteArray a */
906 case REF_REP : /* Ref s a */
907 case MUTARR_REP : /* PrimMutableArray s a */
908 case MUTBARR_REP: /* PrimMutableByteArray s a */
909 case MVAR_REP: /* MVar a */
911 emit_i_VAR(bco,offset);
914 barf("asmVar %d",rep);
916 incSp(bco,repSizeW(rep));
919 /* --------------------------------------------------------------------------
921 * ------------------------------------------------------------------------*/
923 AsmSp asmBeginEnter( AsmBCO bco )
928 void asmEndEnter( AsmBCO bco, AsmSp sp1, AsmSp sp2 )
930 int x = bco->sp - sp1;
932 ASSERT(x >= 0 && y >= 0);
934 emit_i_SLIDE(bco,x,y);
935 decSp(bco,sp1 - sp2);
938 decSp(bco,sizeofW(StgPtr));
941 /* --------------------------------------------------------------------------
942 * Build boxed Ints, Floats, etc
943 * ------------------------------------------------------------------------*/
945 AsmVar asmBox( AsmBCO bco, AsmRep rep )
949 emiti_(bco,i_PACK_CHAR);
952 emiti_(bco,i_PACK_INT);
956 emiti_(bco,i_PACK_WORD);
959 emiti_(bco,i_PACK_ADDR);
962 emiti_(bco,i_PACK_FLOAT);
965 emiti_(bco,i_PACK_DOUBLE);
968 emiti_(bco,i_PACK_STABLE);
972 barf("asmBox %d",rep);
974 /* NB: these operations DO pop their arg */
975 decSp(bco, repSizeW(rep)); /* pop unboxed arg */
976 incSp(bco, sizeofW(StgPtr)); /* push box */
980 /* --------------------------------------------------------------------------
981 * Unbox Ints, Floats, etc
982 * ------------------------------------------------------------------------*/
984 AsmVar asmUnbox( AsmBCO bco, AsmRep rep )
988 emiti_(bco,i_UNPACK_INT);
992 emiti_(bco,i_UNPACK_WORD);
995 emiti_(bco,i_UNPACK_ADDR);
998 emiti_(bco,i_UNPACK_CHAR);
1001 emiti_(bco,i_UNPACK_FLOAT);
1004 emiti_(bco,i_UNPACK_DOUBLE);
1007 emiti_(bco,i_UNPACK_STABLE);
1010 barf("asmUnbox %d",rep);
1012 /* NB: these operations DO NOT pop their arg */
1013 incSp(bco, repSizeW(rep)); /* push unboxed arg */
1018 /* --------------------------------------------------------------------------
1019 * Push unboxed Ints, Floats, etc
1020 * ------------------------------------------------------------------------*/
1022 void asmConstInt( AsmBCO bco, AsmInt x )
1024 emit_i_CONST_INT(bco,bco->n_words);
1025 asmAddNonPtrWords(bco,AsmInt,x);
1026 incSp(bco, repSizeW(INT_REP));
1029 void asmConstInteger( AsmBCO bco, AsmString x )
1031 emit_i_CONST_INTEGER(bco,bco->n_words);
1032 asmAddNonPtrWords(bco,AsmString,x);
1033 incSp(bco, repSizeW(INTEGER_REP));
1036 void asmConstAddr( AsmBCO bco, AsmAddr x )
1038 emit_i_CONST_ADDR(bco,bco->n_words);
1039 asmAddNonPtrWords(bco,AsmAddr,x);
1040 incSp(bco, repSizeW(ADDR_REP));
1043 void asmConstWord( AsmBCO bco, AsmWord x )
1045 emit_i_CONST_INT(bco,bco->n_words);
1046 asmAddNonPtrWords(bco,AsmWord,(AsmInt)x);
1047 incSp(bco, repSizeW(WORD_REP));
1050 void asmConstChar( AsmBCO bco, AsmChar x )
1052 emit_i_CONST_CHAR(bco,bco->n_words);
1053 asmAddNonPtrWords(bco,AsmChar,x);
1054 incSp(bco, repSizeW(CHAR_REP));
1057 void asmConstFloat( AsmBCO bco, AsmFloat x )
1059 emit_i_CONST_FLOAT(bco,bco->n_words);
1060 asmAddNonPtrWords(bco,AsmFloat,x);
1061 incSp(bco, repSizeW(FLOAT_REP));
1064 void asmConstDouble( AsmBCO bco, AsmDouble x )
1066 emit_i_CONST_DOUBLE(bco,bco->n_words);
1067 asmAddNonPtrWords(bco,AsmDouble,x);
1068 incSp(bco, repSizeW(DOUBLE_REP));
1071 /* --------------------------------------------------------------------------
1072 * Algebraic case helpers
1073 * ------------------------------------------------------------------------*/
1075 /* a mildly bogus pair of functions... */
1076 AsmSp asmBeginCase( AsmBCO bco )
1081 void asmEndCase( AsmBCO bco __attribute__ ((unused)) )
1085 AsmSp asmContinuation( AsmBCO bco, AsmBCO ret_addr )
1087 emit_i_RETADDR(bco,bco->n_refs);
1088 asmAddRefObject(bco,ret_addr);
1089 incSp(bco, 2 * sizeofW(StgPtr));
1093 AsmBCO asmBeginContinuation ( AsmSp sp, int /*List*/ alts )
1095 AsmBCO bco = asmBeginBCO(alts);
1100 void asmEndContinuation ( AsmBCO bco )
1106 /* --------------------------------------------------------------------------
1108 * ------------------------------------------------------------------------*/
1110 AsmSp asmBeginAlt( AsmBCO bco )
1115 void asmEndAlt( AsmBCO bco, AsmSp sp )
1120 AsmPc asmTest( AsmBCO bco, AsmWord tag )
1122 emiti_8_16(bco,i_TEST,tag,0);
1123 return bco->n_insns;
1126 AsmPc asmTestInt ( AsmBCO bco, AsmVar v, AsmInt x )
1128 asmVar(bco,v,INT_REP);
1130 emiti_16(bco,i_TEST_INT,0);
1131 decSp(bco, 2*repSizeW(INT_REP));
1132 return bco->n_insns;
1135 void asmFixBranch ( AsmBCO bco, AsmPc from )
1137 int distance = bco->n_insns - from;
1138 ASSERT(distance >= 0);
1139 ASSERT(distance < 65536);
1140 setInstrs(bco,from-2,distance/256);
1141 setInstrs(bco,from-1,distance%256);
1144 void asmPanic( AsmBCO bco )
1146 emiti_(bco,i_PANIC); /* "irrefutable" pattern failed - oops! */
1149 /* --------------------------------------------------------------------------
1151 * ------------------------------------------------------------------------*/
1153 AsmSp asmBeginPrim( AsmBCO bco )
1158 void asmEndPrim( AsmBCO bco, const AsmPrim* prim, AsmSp base )
1160 emiti_8(bco,prim->prefix,prim->opcode);
1164 char* asmGetPrimopName ( AsmPrim* p )
1169 /* Hugs used to let you add arbitrary primops with arbitrary types
1170 * just by editing Prelude.hs or any other file you wanted.
1171 * We deliberately avoided that approach because we wanted more
1172 * control over which primops are provided.
1174 AsmPrim asmPrimOps[] = {
1176 /* Char# operations */
1177 { "primGtChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_gtChar }
1178 , { "primGeChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_geChar }
1179 , { "primEqChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_eqChar }
1180 , { "primNeChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_neChar }
1181 , { "primLtChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_ltChar }
1182 , { "primLeChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_leChar }
1183 , { "primCharToInt", "C", "I", MONAD_Id, i_PRIMOP1, i_charToInt }
1184 , { "primIntToChar", "I", "C", MONAD_Id, i_PRIMOP1, i_intToChar }
1186 /* Int# operations */
1187 , { "primGtInt", "II", "B", MONAD_Id, i_PRIMOP1, i_gtInt }
1188 , { "primGeInt", "II", "B", MONAD_Id, i_PRIMOP1, i_geInt }
1189 , { "primEqInt", "II", "B", MONAD_Id, i_PRIMOP1, i_eqInt }
1190 , { "primNeInt", "II", "B", MONAD_Id, i_PRIMOP1, i_neInt }
1191 , { "primLtInt", "II", "B", MONAD_Id, i_PRIMOP1, i_ltInt }
1192 , { "primLeInt", "II", "B", MONAD_Id, i_PRIMOP1, i_leInt }
1193 , { "primMinInt", "", "I", MONAD_Id, i_PRIMOP1, i_minInt }
1194 , { "primMaxInt", "", "I", MONAD_Id, i_PRIMOP1, i_maxInt }
1195 , { "primPlusInt", "II", "I", MONAD_Id, i_PRIMOP1, i_plusInt }
1196 , { "primMinusInt", "II", "I", MONAD_Id, i_PRIMOP1, i_minusInt }
1197 , { "primTimesInt", "II", "I", MONAD_Id, i_PRIMOP1, i_timesInt }
1198 , { "primQuotInt", "II", "I", MONAD_Id, i_PRIMOP1, i_quotInt }
1199 , { "primRemInt", "II", "I", MONAD_Id, i_PRIMOP1, i_remInt }
1200 , { "primQuotRemInt", "II", "II", MONAD_Id, i_PRIMOP1, i_quotRemInt }
1201 , { "primNegateInt", "I", "I", MONAD_Id, i_PRIMOP1, i_negateInt }
1203 , { "primAndInt", "II", "I", MONAD_Id, i_PRIMOP1, i_andInt }
1204 , { "primOrInt", "II", "I", MONAD_Id, i_PRIMOP1, i_orInt }
1205 , { "primXorInt", "II", "I", MONAD_Id, i_PRIMOP1, i_xorInt }
1206 , { "primNotInt", "I", "I", MONAD_Id, i_PRIMOP1, i_notInt }
1207 , { "primShiftLInt", "II", "I", MONAD_Id, i_PRIMOP1, i_shiftLInt }
1208 , { "primShiftRAInt", "II", "I", MONAD_Id, i_PRIMOP1, i_shiftRAInt }
1209 , { "primShiftRLInt", "II", "I", MONAD_Id, i_PRIMOP1, i_shiftRLInt }
1211 /* Word# operations */
1212 , { "primGtWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_gtWord }
1213 , { "primGeWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_geWord }
1214 , { "primEqWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_eqWord }
1215 , { "primNeWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_neWord }
1216 , { "primLtWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_ltWord }
1217 , { "primLeWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_leWord }
1218 , { "primMinWord", "", "W", MONAD_Id, i_PRIMOP1, i_minWord }
1219 , { "primMaxWord", "", "W", MONAD_Id, i_PRIMOP1, i_maxWord }
1220 , { "primPlusWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_plusWord }
1221 , { "primMinusWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_minusWord }
1222 , { "primTimesWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_timesWord }
1223 , { "primQuotWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_quotWord }
1224 , { "primRemWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_remWord }
1225 , { "primQuotRemWord", "WW", "WW", MONAD_Id, i_PRIMOP1, i_quotRemWord }
1226 , { "primNegateWord", "W", "W", MONAD_Id, i_PRIMOP1, i_negateWord }
1228 , { "primAndWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_andWord }
1229 , { "primOrWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_orWord }
1230 , { "primXorWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_xorWord }
1231 , { "primNotWord", "W", "W", MONAD_Id, i_PRIMOP1, i_notWord }
1232 , { "primShiftLWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_shiftLWord }
1233 , { "primShiftRAWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_shiftRAWord }
1234 , { "primShiftRLWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_shiftRLWord }
1236 , { "primIntToWord", "I", "W", MONAD_Id, i_PRIMOP1, i_intToWord }
1237 , { "primWordToInt", "W", "I", MONAD_Id, i_PRIMOP1, i_wordToInt }
1239 /* Addr# operations */
1240 , { "primGtAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_gtAddr }
1241 , { "primGeAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_geAddr }
1242 , { "primEqAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_eqAddr }
1243 , { "primNeAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_neAddr }
1244 , { "primLtAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_ltAddr }
1245 , { "primLeAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_leAddr }
1246 , { "primIntToAddr", "I", "A", MONAD_Id, i_PRIMOP1, i_intToAddr }
1247 , { "primAddrToInt", "A", "I", MONAD_Id, i_PRIMOP1, i_addrToInt }
1249 , { "primIndexCharOffAddr", "AI", "C", MONAD_Id, i_PRIMOP1, i_indexCharOffAddr }
1250 , { "primIndexIntOffAddr", "AI", "I", MONAD_Id, i_PRIMOP1, i_indexIntOffAddr }
1251 , { "primIndexWordOffAddr", "AI", "W", MONAD_Id, i_PRIMOP1, i_indexWordOffAddr }
1252 , { "primIndexAddrOffAddr", "AI", "A", MONAD_Id, i_PRIMOP1, i_indexAddrOffAddr }
1253 , { "primIndexFloatOffAddr", "AI", "F", MONAD_Id, i_PRIMOP1, i_indexFloatOffAddr }
1254 , { "primIndexDoubleOffAddr", "AI", "D", MONAD_Id, i_PRIMOP1, i_indexDoubleOffAddr }
1255 , { "primIndexStableOffAddr", "AI", "s", MONAD_Id, i_PRIMOP1, i_indexStableOffAddr }
1257 /* Stable# operations */
1258 , { "primIntToStablePtr", "I", "s", MONAD_Id, i_PRIMOP1, i_intToStable }
1259 , { "primStablePtrToInt", "s", "I", MONAD_Id, i_PRIMOP1, i_stableToInt }
1261 /* These ops really ought to be in the IO monad */
1262 , { "primReadCharOffAddr", "AI", "C", MONAD_ST, i_PRIMOP1, i_readCharOffAddr }
1263 , { "primReadIntOffAddr", "AI", "I", MONAD_ST, i_PRIMOP1, i_readIntOffAddr }
1264 , { "primReadWordOffAddr", "AI", "W", MONAD_ST, i_PRIMOP1, i_readWordOffAddr }
1265 , { "primReadAddrOffAddr", "AI", "A", MONAD_ST, i_PRIMOP1, i_readAddrOffAddr }
1266 , { "primReadFloatOffAddr", "AI", "F", MONAD_ST, i_PRIMOP1, i_readFloatOffAddr }
1267 , { "primReadDoubleOffAddr", "AI", "D", MONAD_ST, i_PRIMOP1, i_readDoubleOffAddr }
1268 , { "primReadStableOffAddr", "AI", "s", MONAD_ST, i_PRIMOP1, i_readStableOffAddr }
1270 /* These ops really ought to be in the IO monad */
1271 , { "primWriteCharOffAddr", "AIC", "", MONAD_ST, i_PRIMOP1, i_writeCharOffAddr }
1272 , { "primWriteIntOffAddr", "AII", "", MONAD_ST, i_PRIMOP1, i_writeIntOffAddr }
1273 , { "primWriteWordOffAddr", "AIW", "", MONAD_ST, i_PRIMOP1, i_writeWordOffAddr }
1274 , { "primWriteAddrOffAddr", "AIA", "", MONAD_ST, i_PRIMOP1, i_writeAddrOffAddr }
1275 , { "primWriteFloatOffAddr", "AIF", "", MONAD_ST, i_PRIMOP1, i_writeFloatOffAddr }
1276 , { "primWriteDoubleOffAddr", "AID", "", MONAD_ST, i_PRIMOP1, i_writeDoubleOffAddr }
1277 , { "primWriteStableOffAddr", "AIs", "", MONAD_ST, i_PRIMOP1, i_writeStableOffAddr }
1279 /* Integer operations */
1280 , { "primCompareInteger", "ZZ", "I", MONAD_Id, i_PRIMOP1, i_compareInteger }
1281 , { "primNegateInteger", "Z", "Z", MONAD_Id, i_PRIMOP1, i_negateInteger }
1282 , { "primPlusInteger", "ZZ", "Z", MONAD_Id, i_PRIMOP1, i_plusInteger }
1283 , { "primMinusInteger", "ZZ", "Z", MONAD_Id, i_PRIMOP1, i_minusInteger }
1284 , { "primTimesInteger", "ZZ", "Z", MONAD_Id, i_PRIMOP1, i_timesInteger }
1285 , { "primQuotRemInteger", "ZZ", "ZZ", MONAD_Id, i_PRIMOP1, i_quotRemInteger }
1286 , { "primDivModInteger", "ZZ", "ZZ", MONAD_Id, i_PRIMOP1, i_divModInteger }
1287 , { "primIntegerToInt", "Z", "I", MONAD_Id, i_PRIMOP1, i_integerToInt }
1288 , { "primIntToInteger", "I", "Z", MONAD_Id, i_PRIMOP1, i_intToInteger }
1289 , { "primIntegerToWord", "Z", "W", MONAD_Id, i_PRIMOP1, i_integerToWord }
1290 , { "primWordToInteger", "W", "Z", MONAD_Id, i_PRIMOP1, i_wordToInteger }
1291 , { "primIntegerToFloat", "Z", "F", MONAD_Id, i_PRIMOP1, i_integerToFloat }
1292 , { "primFloatToInteger", "F", "Z", MONAD_Id, i_PRIMOP1, i_floatToInteger }
1293 , { "primIntegerToDouble", "Z", "D", MONAD_Id, i_PRIMOP1, i_integerToDouble }
1294 , { "primDoubleToInteger", "D", "Z", MONAD_Id, i_PRIMOP1, i_doubleToInteger }
1296 /* Float# operations */
1297 , { "primGtFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_gtFloat }
1298 , { "primGeFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_geFloat }
1299 , { "primEqFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_eqFloat }
1300 , { "primNeFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_neFloat }
1301 , { "primLtFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_ltFloat }
1302 , { "primLeFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_leFloat }
1303 , { "primMinFloat", "", "F", MONAD_Id, i_PRIMOP1, i_minFloat }
1304 , { "primMaxFloat", "", "F", MONAD_Id, i_PRIMOP1, i_maxFloat }
1305 , { "primRadixFloat", "", "I", MONAD_Id, i_PRIMOP1, i_radixFloat }
1306 , { "primDigitsFloat", "", "I", MONAD_Id, i_PRIMOP1, i_digitsFloat }
1307 , { "primMinExpFloat", "", "I", MONAD_Id, i_PRIMOP1, i_minExpFloat }
1308 , { "primMaxExpFloat", "", "I", MONAD_Id, i_PRIMOP1, i_maxExpFloat }
1309 , { "primPlusFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_plusFloat }
1310 , { "primMinusFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_minusFloat }
1311 , { "primTimesFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_timesFloat }
1312 , { "primDivideFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_divideFloat }
1313 , { "primNegateFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_negateFloat }
1314 , { "primFloatToInt", "F", "I", MONAD_Id, i_PRIMOP1, i_floatToInt }
1315 , { "primIntToFloat", "I", "F", MONAD_Id, i_PRIMOP1, i_intToFloat }
1316 , { "primExpFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_expFloat }
1317 , { "primLogFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_logFloat }
1318 , { "primSqrtFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_sqrtFloat }
1319 , { "primSinFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_sinFloat }
1320 , { "primCosFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_cosFloat }
1321 , { "primTanFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_tanFloat }
1322 , { "primAsinFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_asinFloat }
1323 , { "primAcosFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_acosFloat }
1324 , { "primAtanFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_atanFloat }
1325 , { "primSinhFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_sinhFloat }
1326 , { "primCoshFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_coshFloat }
1327 , { "primTanhFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_tanhFloat }
1328 , { "primPowerFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_powerFloat }
1329 , { "primDecodeFloatZ", "F", "ZI", MONAD_Id, i_PRIMOP1, i_decodeFloatZ }
1330 , { "primEncodeFloatZ", "ZI", "F", MONAD_Id, i_PRIMOP1, i_encodeFloatZ }
1331 , { "primIsNaNFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isNaNFloat }
1332 , { "primIsInfiniteFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isInfiniteFloat }
1333 , { "primIsDenormalizedFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isDenormalizedFloat }
1334 , { "primIsNegativeZeroFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isNegativeZeroFloat }
1335 , { "primIsIEEEFloat", "", "B", MONAD_Id, i_PRIMOP1, i_isIEEEFloat }
1337 /* Double# operations */
1338 , { "primGtDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_gtDouble }
1339 , { "primGeDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_geDouble }
1340 , { "primEqDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_eqDouble }
1341 , { "primNeDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_neDouble }
1342 , { "primLtDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_ltDouble }
1343 , { "primLeDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_leDouble }
1344 , { "primMinDouble", "", "D", MONAD_Id, i_PRIMOP1, i_minDouble }
1345 , { "primMaxDouble", "", "D", MONAD_Id, i_PRIMOP1, i_maxDouble }
1346 , { "primRadixDouble", "", "I", MONAD_Id, i_PRIMOP1, i_radixDouble }
1347 , { "primDigitsDouble", "", "I", MONAD_Id, i_PRIMOP1, i_digitsDouble }
1348 , { "primMinExpDouble", "", "I", MONAD_Id, i_PRIMOP1, i_minExpDouble }
1349 , { "primMaxExpDouble", "", "I", MONAD_Id, i_PRIMOP1, i_maxExpDouble }
1350 , { "primPlusDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_plusDouble }
1351 , { "primMinusDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_minusDouble }
1352 , { "primTimesDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_timesDouble }
1353 , { "primDivideDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_divideDouble }
1354 , { "primNegateDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_negateDouble }
1355 , { "primDoubleToInt", "D", "I", MONAD_Id, i_PRIMOP1, i_doubleToInt }
1356 , { "primIntToDouble", "I", "D", MONAD_Id, i_PRIMOP1, i_intToDouble }
1357 , { "primDoubleToFloat", "D", "F", MONAD_Id, i_PRIMOP1, i_doubleToFloat }
1358 , { "primFloatToDouble", "F", "D", MONAD_Id, i_PRIMOP1, i_floatToDouble }
1359 , { "primExpDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_expDouble }
1360 , { "primLogDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_logDouble }
1361 , { "primSqrtDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_sqrtDouble }
1362 , { "primSinDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_sinDouble }
1363 , { "primCosDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_cosDouble }
1364 , { "primTanDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_tanDouble }
1365 , { "primAsinDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_asinDouble }
1366 , { "primAcosDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_acosDouble }
1367 , { "primAtanDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_atanDouble }
1368 , { "primSinhDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_sinhDouble }
1369 , { "primCoshDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_coshDouble }
1370 , { "primTanhDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_tanhDouble }
1371 , { "primPowerDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_powerDouble }
1372 , { "primDecodeDoubleZ", "D", "ZI", MONAD_Id, i_PRIMOP1, i_decodeDoubleZ }
1373 , { "primEncodeDoubleZ", "ZI", "D", MONAD_Id, i_PRIMOP1, i_encodeDoubleZ }
1374 , { "primIsNaNDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isNaNDouble }
1375 , { "primIsInfiniteDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isInfiniteDouble }
1376 , { "primIsDenormalizedDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isDenormalizedDouble }
1377 , { "primIsNegativeZeroDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isNegativeZeroDouble }
1378 , { "primIsIEEEDouble", "", "B", MONAD_Id, i_PRIMOP1, i_isIEEEDouble }
1380 /* Ref operations */
1381 , { "primNewRef", "a", "R", MONAD_ST, i_PRIMOP2, i_newRef }
1382 , { "primWriteRef", "Ra", "", MONAD_ST, i_PRIMOP2, i_writeRef }
1383 , { "primReadRef", "R", "a", MONAD_ST, i_PRIMOP2, i_readRef }
1384 , { "primSameRef", "RR", "B", MONAD_Id, i_PRIMOP2, i_sameRef }
1386 /* PrimArray operations */
1387 , { "primSameMutableArray", "MM", "B", MONAD_Id, i_PRIMOP2, i_sameMutableArray }
1388 , { "primUnsafeFreezeArray", "M", "X", MONAD_ST, i_PRIMOP2, i_unsafeFreezeArray }
1389 , { "primNewArray", "Ia", "M", MONAD_ST, i_PRIMOP2, i_newArray }
1390 , { "primWriteArray", "MIa", "", MONAD_ST, i_PRIMOP2, i_writeArray }
1391 , { "primReadArray", "MI", "a", MONAD_ST, i_PRIMOP2, i_readArray }
1392 , { "primIndexArray", "XI", "a", MONAD_Id, i_PRIMOP2, i_indexArray }
1393 , { "primSizeArray", "X", "I", MONAD_Id, i_PRIMOP2, i_sizeArray }
1394 , { "primSizeMutableArray", "M", "I", MONAD_Id, i_PRIMOP2, i_sizeMutableArray }
1396 /* Prim[Mutable]ByteArray operations */
1397 , { "primSameMutableByteArray", "mm", "B", MONAD_Id, i_PRIMOP2, i_sameMutableByteArray }
1398 , { "primUnsafeFreezeByteArray", "m", "x", MONAD_ST, i_PRIMOP2, i_unsafeFreezeByteArray }
1400 , { "primNewByteArray", "I", "m", MONAD_ST, i_PRIMOP2, i_newByteArray }
1402 , { "primWriteCharArray", "mIC", "", MONAD_ST, i_PRIMOP2, i_writeCharArray }
1403 , { "primReadCharArray", "mI", "C", MONAD_ST, i_PRIMOP2, i_readCharArray }
1404 , { "primIndexCharArray", "xI", "C", MONAD_Id, i_PRIMOP2, i_indexCharArray }
1406 , { "primWriteIntArray", "mII", "", MONAD_ST, i_PRIMOP2, i_writeIntArray }
1407 , { "primReadIntArray", "mI", "I", MONAD_ST, i_PRIMOP2, i_readIntArray }
1408 , { "primIndexIntArray", "xI", "I", MONAD_Id, i_PRIMOP2, i_indexIntArray }
1410 /* {new,write,read,index}IntegerArray not provided */
1412 , { "primWriteWordArray", "mIW", "", MONAD_ST, i_PRIMOP2, i_writeWordArray }
1413 , { "primReadWordArray", "mI", "W", MONAD_ST, i_PRIMOP2, i_readWordArray }
1414 , { "primIndexWordArray", "xI", "W", MONAD_Id, i_PRIMOP2, i_indexWordArray }
1415 , { "primWriteAddrArray", "mIA", "", MONAD_ST, i_PRIMOP2, i_writeAddrArray }
1416 , { "primReadAddrArray", "mI", "A", MONAD_ST, i_PRIMOP2, i_readAddrArray }
1417 , { "primIndexAddrArray", "xI", "A", MONAD_Id, i_PRIMOP2, i_indexAddrArray }
1418 , { "primWriteFloatArray", "mIF", "", MONAD_ST, i_PRIMOP2, i_writeFloatArray }
1419 , { "primReadFloatArray", "mI", "F", MONAD_ST, i_PRIMOP2, i_readFloatArray }
1420 , { "primIndexFloatArray", "xI", "F", MONAD_Id, i_PRIMOP2, i_indexFloatArray }
1421 , { "primWriteDoubleArray" , "mID", "", MONAD_ST, i_PRIMOP2, i_writeDoubleArray }
1422 , { "primReadDoubleArray", "mI", "D", MONAD_ST, i_PRIMOP2, i_readDoubleArray }
1423 , { "primIndexDoubleArray", "xI", "D", MONAD_Id, i_PRIMOP2, i_indexDoubleArray }
1426 #ifdef PROVIDE_STABLE
1427 , { "primWriteStableArray", "mIs", "", MONAD_ST, i_PRIMOP2, i_writeStableArray }
1428 , { "primReadStableArray", "mI", "s", MONAD_ST, i_PRIMOP2, i_readStableArray }
1429 , { "primIndexStableArray", "xI", "s", MONAD_Id, i_PRIMOP2, i_indexStableArray }
1432 /* {new,write,read,index}ForeignObjArray not provided */
1435 #ifdef PROVIDE_FOREIGN
1436 /* ForeignObj# operations */
1437 , { "primMkForeignObj", "A", "f", MONAD_IO, i_PRIMOP2, i_mkForeignObj }
1440 /* WeakPair# operations */
1441 , { "primMakeWeak", "bac", "w", MONAD_IO, i_PRIMOP2, i_makeWeak }
1442 , { "primDeRefWeak", "w", "Ia", MONAD_IO, i_PRIMOP2, i_deRefWeak }
1444 /* StablePtr# operations */
1445 , { "primMakeStablePtr", "a", "s", MONAD_IO, i_PRIMOP2, i_makeStablePtr }
1446 , { "primDeRefStablePtr", "s", "a", MONAD_IO, i_PRIMOP2, i_deRefStablePtr }
1447 , { "primFreeStablePtr", "s", "", MONAD_IO, i_PRIMOP2, i_freeStablePtr }
1449 /* foreign export dynamic support */
1450 , { "primCreateAdjThunkARCH", "sAC","A", MONAD_IO, i_PRIMOP2, i_createAdjThunkARCH }
1452 /* misc handy hacks */
1453 , { "primGetArgc", "", "I", MONAD_IO, i_PRIMOP2, i_getArgc }
1454 , { "primGetArgv", "I", "A", MONAD_IO, i_PRIMOP2, i_getArgv }
1456 #ifdef PROVIDE_PTREQUALITY
1457 , { "primReallyUnsafePtrEquality", "aa", "B",MONAD_Id, i_PRIMOP2, i_reallyUnsafePtrEquality }
1459 #ifdef PROVIDE_COERCE
1460 , { "primUnsafeCoerce", "a", "b", MONAD_Id, i_PRIMOP2, i_unsafeCoerce }
1462 #ifdef PROVIDE_CONCURRENT
1463 /* Concurrency operations */
1464 , { "primForkIO", "a", "T", MONAD_IO, i_PRIMOP2, i_forkIO }
1465 , { "primKillThread", "T", "", MONAD_IO, i_PRIMOP2, i_killThread }
1466 , { "primRaiseInThread", "TE", "", MONAD_IO, i_PRIMOP2, i_raiseInThread }
1468 , { "primWaitRead", "I", "", MONAD_IO, i_PRIMOP2, i_waitRead }
1469 , { "primWaitWrite", "I", "", MONAD_IO, i_PRIMOP2, i_waitWrite }
1470 , { "primYield", "", "", MONAD_IO, i_PRIMOP2, i_yield } , { "primDelay", "I", "", MONAD_IO, i_PRIMOP2, i_delay }
1471 , { "primGetThreadId", "", "T", MONAD_IO, i_PRIMOP2, i_getThreadId }
1472 , { "primCmpThreadIds", "TT", "I", MONAD_Id, i_PRIMOP2, i_cmpThreadIds }
1474 , { "primNewEmptyMVar", "", "r", MONAD_IO, i_PRIMOP2, i_newMVar }
1475 /* primTakeMVar is handwritten bytecode */
1476 , { "primPutMVar", "ra", "", MONAD_IO, i_PRIMOP2, i_putMVar }
1477 , { "primSameMVar", "rr", "B", MONAD_Id, i_PRIMOP2, i_sameMVar }
1480 /* Ccall is polyadic - so it's excluded from this table */
1485 AsmPrim ccall_ccall_Id
1486 = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_ccall_Id };
1487 AsmPrim ccall_ccall_IO
1488 = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_ccall_IO };
1489 AsmPrim ccall_stdcall_Id
1490 = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_stdcall_Id };
1491 AsmPrim ccall_stdcall_IO
1492 = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_stdcall_IO };
1495 void checkBytecodeCount( void );
1496 void checkBytecodeCount( void )
1498 if (MAX_Primop1 >= 255) {
1499 printf("Too many Primop1 bytecodes (%d)\n",MAX_Primop1);
1501 if (MAX_Primop2 >= 255) {
1502 printf("Too many Primop2 bytecodes (%d)\n",MAX_Primop2);
1507 AsmPrim* asmFindPrim( char* s )
1510 for (i=0; asmPrimOps[i].name; ++i) {
1511 if (strcmp(s,asmPrimOps[i].name)==0) {
1512 return &asmPrimOps[i];
1518 AsmPrim* asmFindPrimop( AsmInstr prefix, AsmInstr op )
1521 for (i=0; asmPrimOps[i].name; ++i) {
1522 if (asmPrimOps[i].prefix == prefix && asmPrimOps[i].opcode == op) {
1523 return &asmPrimOps[i];
1529 /* --------------------------------------------------------------------------
1530 * Handwritten primops
1531 * ------------------------------------------------------------------------*/
1533 void* /* StgBCO* */ asm_BCO_catch ( void )
1539 bco = asmBeginBCO(0 /*NIL*/);
1540 emiti_8(bco,i_ARG_CHECK,2);
1541 emiti_8(bco,i_PRIMOP1,i_pushcatchframe);
1542 incSp(bco, (1-2)*sizeofW(StgPtr) + sizeofW(StgCatchFrame));
1543 emiti_(bco,i_ENTER);
1544 decSp(bco, sizeofW(StgPtr));
1547 asmAllocateHeapSpace();
1549 closure = (StgBCO*)(bco->closure);
1554 void* /* StgBCO* */ asm_BCO_raise ( void )
1560 bco = asmBeginBCO(0 /*NIL*/);
1561 emiti_8(bco,i_ARG_CHECK,1);
1562 emiti_8(bco,i_PRIMOP2,i_raise);
1563 decSp(bco,sizeofW(StgPtr));
1566 asmAllocateHeapSpace();
1568 closure = (StgBCO*)(bco->closure);
1573 void* /* StgBCO* */ asm_BCO_seq ( void )
1579 cont = asmBeginBCO(0 /*NIL*/);
1580 emiti_8(cont,i_ARG_CHECK,2); /* should never fail */
1582 emit_i_SLIDE(cont,1,2);
1583 emiti_(cont,i_ENTER);
1584 incSp(cont, 3*sizeofW(StgPtr));
1587 eval = asmBeginBCO(0 /*NIL*/);
1588 emiti_8(eval,i_ARG_CHECK,2);
1589 emit_i_RETADDR(eval,eval->n_refs);
1590 asmAddRefObject(eval,cont);
1592 emit_i_SLIDE(eval,3,1);
1593 emiti_8(eval,i_PRIMOP1,i_pushseqframe);
1594 emiti_(eval,i_ENTER);
1595 incSp(eval, sizeofW(StgSeqFrame) + 4*sizeofW(StgPtr));
1598 asmAllocateHeapSpace();
1600 closure = (StgBCO*)(eval->closure);
1605 void* /* StgBCO* */ asm_BCO_takeMVar ( void )
1607 AsmBCO kase, casecont, take;
1611 take = asmBeginBCO(0 /*NIL*/);
1613 emiti_8(take,i_PRIMOP2,i_takeMVar);
1617 emit_i_SLIDE(take,3,4);
1618 emiti_(take,i_ENTER);
1622 casecont = asmBeginBCO(0 /*NIL*/);
1623 emiti_(casecont,i_UNPACK);
1624 emit_i_VAR(casecont,4);
1625 emit_i_VAR(casecont,4);
1626 emit_i_VAR(casecont,2);
1627 emit_i_CONST(casecont,casecont->n_refs);
1628 asmAddRefObject(casecont,take);
1629 emit_i_SLIDE(casecont,4,5);
1630 emiti_(casecont,i_ENTER);
1632 asmEndBCO(casecont);
1634 kase = asmBeginBCO(0 /*NIL*/);
1635 emiti_8(kase,i_ARG_CHECK,3);
1636 emit_i_RETADDR(kase,kase->n_refs);
1637 asmAddRefObject(kase,casecont);
1639 emiti_(kase,i_ENTER);
1643 asmAllocateHeapSpace();
1645 closure = (StgBCO*)(kase->closure);
1651 /* --------------------------------------------------------------------------
1653 * ------------------------------------------------------------------------*/
1655 AsmVar asmAllocCONSTR ( AsmBCO bco, AsmInfo info )
1658 ASSERT( sizeW_fromITBL(info) >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
1660 /* Look in this bco's collection of nonpointers (literals)
1661 to see if the itbl pointer is already there. If so, re-use it. */
1662 i = asmFindInNonPtrs ( bco, (StgWord)info );
1665 emit_i_ALLOC_CONSTR(bco,bco->n_words);
1666 asmAddNonPtrWords(bco,AsmInfo,info);
1668 emit_i_ALLOC_CONSTR(bco,i);
1671 incSp(bco, sizeofW(StgClosurePtr));
1675 AsmSp asmBeginPack( AsmBCO bco )
1680 void asmEndPack( AsmBCO bco, AsmVar v, AsmSp start, AsmInfo info )
1682 nat size = bco->sp - start;
1683 assert(bco->sp >= start);
1685 /* only reason to include info is for this assertion */
1686 assert(info->layout.payload.ptrs == size);
1687 emit_i_PACK(bco, bco->sp - v);
1691 void asmBeginUnpack( AsmBCO bco __attribute__ ((unused)) )
1693 /* dummy to make it look prettier */
1696 void asmEndUnpack( AsmBCO bco )
1698 emiti_(bco,i_UNPACK);
1701 AsmVar asmAllocAP( AsmBCO bco, AsmNat words )
1703 emiti_8(bco,i_ALLOC_AP,words);
1704 incSp(bco, sizeofW(StgPtr));
1708 AsmSp asmBeginMkAP( AsmBCO bco )
1713 void asmEndMkAP( AsmBCO bco, AsmVar v, AsmSp start )
1715 emit_i_MKAP(bco,bco->sp-v,bco->sp-start-1);
1716 /* -1 because fun isn't counted */
1720 AsmVar asmAllocPAP( AsmBCO bco, AsmNat size )
1722 emiti_8(bco,i_ALLOC_PAP,size);
1723 incSp(bco, sizeofW(StgPtr));
1727 AsmSp asmBeginMkPAP( AsmBCO bco )
1732 void asmEndMkPAP( AsmBCO bco, AsmVar v, AsmSp start )
1734 emiti_8_8(bco,i_MKPAP,bco->sp-v,bco->sp-start-1);
1735 /* -1 because fun isn't counted */
1739 AsmVar asmPushRefHugs ( AsmBCO bco, int /*Name*/ n )
1741 emit_i_CONST(bco,bco->n_refs);
1742 asmAddRefHugs(bco,n);
1743 incSp(bco, sizeofW(StgPtr));
1747 AsmVar asmPushRefObject ( AsmBCO bco, AsmObject p )
1749 emit_i_CONST(bco,bco->n_refs);
1750 asmAddRefObject(bco,p);
1751 incSp(bco, sizeofW(StgPtr));
1755 AsmVar asmPushRefNoOp ( AsmBCO bco, StgPtr p )
1757 emit_i_CONST(bco,bco->n_refs);
1758 asmAddRefNoOp(bco,p);
1759 incSp(bco, sizeofW(StgPtr));
1764 /* --------------------------------------------------------------------------
1765 * Building InfoTables
1766 * ------------------------------------------------------------------------*/
1768 AsmInfo asmMkInfo( AsmNat tag, AsmNat ptrs )
1770 StgInfoTable* info = stgMallocBytes( sizeof(StgInfoTable),"asmMkInfo");
1771 /* Note: the evaluator automatically pads objects with the right number
1772 * of non-ptrs to satisfy MIN_NONUPD_SIZE restrictions.
1774 AsmNat nptrs = stg_max(0,MIN_NONUPD_SIZE-ptrs);
1776 /* initialisation code based on INFO_TABLE_CONSTR */
1777 info->layout.payload.ptrs = ptrs;
1778 info->layout.payload.nptrs = nptrs;
1779 info->srt_len = tag;
1780 info->type = CONSTR;
1781 #ifdef USE_MINIINTERPRETER
1782 info->entry = stgCast(StgFunPtr,&Hugs_CONSTR_entry);
1784 #warning asmMkInfo: Need to insert entry code in some cunning way
1786 ASSERT( sizeW_fromITBL(info) >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
1790 /*-------------------------------------------------------------------------*/
1792 #endif /* INTERPRETER */