2 /* --------------------------------------------------------------------------
5 * Copyright (c) 1994-1998.
7 * $RCSfile: Assembler.c,v $
9 * $Date: 1999/11/29 18:59:40 $
11 * This module provides functions to construct BCOs and other closures
12 * required by the bytecode compiler.
14 * It is supposed to shield the compiler from platform dependent information
20 * and from details of how the abstract machine is implemented such as:
22 * o what does a BCO look like?
23 * o how many bytes does the "Push InfoTable" instruction require?
26 * o (To handle letrecs) We allocate Aps, Paps and Cons using number of
27 * heap allocated args to determine size.
28 * We can't handle unboxed args :-(
29 * o All stack offsets are relative to position of Sp at start of
30 * function or thunk (not BCO - consider continuations)
31 * o Active thunks must be roots during GC - how to achieve this?
32 * o Each BCO contains its own stack and heap check
33 * We don't try to exploit the Hp check optimisation - easier to make
34 * each thunk stand on its own.
35 * o asBind returns a "varid" (which is, in fact, a stack offset)
36 * asVar acts on a "varid" - combining it with the current stack size to
37 * determine actual position
38 * o Assembler.h uses totally neutral types: strings, floats, ints, etc
39 * to minimise conflicts with other parts of the system.
41 * ------------------------------------------------------------------------*/
49 #include "Bytecodes.h"
51 #include "Disassembler.h"
52 #include "StgMiscClosures.h"
55 #include "Evaluator.h"
57 #define INSIDE_ASSEMBLER_C
58 #include "Assembler.h"
59 #undef INSIDE_ASSEMBLER_C
61 /* --------------------------------------------------------------------------
62 * References between BCOs
64 * These are necessary because there can be circular references between
65 * BCOs so we have to keep track of all the references to each object
66 * and fill in all the references once we're done.
68 * ToDo: generalise to allow references between any objects
69 * ------------------------------------------------------------------------*/
72 AsmObject ref; /* who refers to it */
73 AsmNat i; /* index into some table held by referer */
76 /* --------------------------------------------------------------------------
77 * Queues (of instructions, ptrs, nonptrs)
78 * ------------------------------------------------------------------------*/
82 #include "QueueTemplate.h"
87 #define Type AsmObject
88 #include "QueueTemplate.h"
94 #include "QueueTemplate.h"
100 #include "QueueTemplate.h"
104 /* --------------------------------------------------------------------------
105 * AsmObjects are used to build heap objects.
107 * AsmObjects can contain circular references to each other
108 * so we have to keep track of all the references which can't be filled
111 * When we finish building an AsmObject, we allocate an actual heap object and
112 * fill in all the references to the asmObject with pointers to the heap object.
114 * We obtain a limited form of polymorphism through inheritance by putting
115 * the AsmObject first in every structure (as in C++ implementations).
116 * We use the closure type of the allocated object to figure out
117 * where the payload lives in the closure.
118 * ------------------------------------------------------------------------*/
119 /* ToDo: clean up terminology: is Closure right or should it be object or ... */
124 AsmNat num_unresolved; /* number of unfilled references */
125 StgClosure* closure; /* where object was allocated */
129 struct AsmObject_ object; /* must be first in struct */
135 struct AsmObject_ object; /* must be first in struct */
139 struct AsmObject_ object; /* must be first in struct */
144 int /*StgExpr*/ stgexpr;
146 /* abstract machine ("executed" during compilation) */
147 AsmSp sp; /* stack ptr */
149 StgWord hp; /* heap ptr */
154 static void asmResolveRef( AsmObject obj, AsmNat i, AsmClosure reference )
156 ASSERT(obj->closure);
157 switch (get_itbl(obj->closure)->type) {
160 StgBCO* bco = stgCast(StgBCO*,obj->closure);
161 ASSERT(i < bco->n_ptrs && bcoConstPtr(bco,i) == NULL);
162 bcoConstCPtr(bco,i) = reference;
167 StgCAF* caf = stgCast(StgCAF*,obj->closure);
168 ASSERT(i == 0 && caf->body == NULL);
169 caf->body = reference;
174 StgClosure* con = stgCast(StgClosure*,obj->closure);
175 ASSERT(i < get_itbl(con)->layout.payload.nptrs && payloadCPtr(con,i) == NULL);
176 payloadCPtr(con,i) = reference;
181 StgAP_UPD* ap = stgCast(StgAP_UPD*,obj->closure);
182 ASSERT(i < 1+ap->n_args);
184 ASSERT(ap->fun == NULL);
187 ASSERT(payloadCPtr(ap,i-1) == NULL);
188 payloadCPtr(ap,i-1) = reference;
193 barf("asmResolveRef");
195 obj->num_unresolved -= 1;
198 static void asmAddRef( AsmObject referent, AsmObject referer, AsmNat i )
200 if (referent->closure) {
201 asmResolveRef(referer,i,(AsmClosure)referent->closure);
203 insertRefs(&(referent->refs),(AsmRef){referer,i});
207 void asmAddPtr( AsmObject obj, AsmObject arg )
209 ASSERT(obj->closure == 0); /* can't extend an object once it's allocated */
210 insertPtrs( &obj->ptrs, arg );
213 static void asmBeginObject( AsmObject obj )
216 obj->num_unresolved = 0;
217 initRefs(&obj->refs);
218 initPtrs(&obj->ptrs);
221 static void asmEndObject( AsmObject obj, StgClosure* c )
223 obj->num_unresolved = obj->ptrs.len;
225 mapQueue(Ptrs, AsmObject, obj->ptrs, asmAddRef(x,obj,i));
226 mapQueue(Refs, AsmRef, obj->refs, asmResolveRef(x.ref,x.i,c));
228 if (obj->num_unresolved == 0) {
229 freePtrs(&obj->ptrs);
230 freeRefs(&obj->refs);
231 /* we don't print until all ptrs are resolved */
232 IF_DEBUG(codegen,printObj(obj->closure));
236 int asmObjectHasClosure ( AsmObject obj )
238 return (obj->num_unresolved == 0 && obj->closure);
241 AsmClosure asmClosureOfObject ( AsmObject obj )
243 ASSERT(asmObjectHasClosure(obj));
247 void asmMarkObject ( AsmObject obj )
249 ASSERT(obj->num_unresolved == 0 && obj->closure);
250 obj->closure = MarkRoot(obj->closure);
253 /* --------------------------------------------------------------------------
255 * ------------------------------------------------------------------------*/
257 static StgClosure* asmAlloc( nat size )
259 StgClosure* o = stgCast(StgClosure*,allocate(size));
260 ASSERT( size >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
261 /* printf("Allocated %p .. %p\n", o, o+size-1); */
265 static void grabHpUpd( AsmBCO bco, nat size )
267 /* ToDo: sometimes we should test for MIN_UPD_SIZE instead */
268 ASSERT( size >= MIN_UPD_SIZE + sizeofW(StgHeader) );
272 static void grabHpNonUpd( AsmBCO bco, nat size )
274 /* ToDo: sometimes we should test for MIN_UPD_SIZE instead */
275 ASSERT( size >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
279 static void resetHp( AsmBCO bco, nat hp )
281 bco->max_hp = stg_max(bco->hp,bco->max_hp);
285 static void setSp( AsmBCO bco, AsmSp sp )
287 bco->max_sp = stg_max(bco->sp,bco->max_sp);
289 bco->max_sp = stg_max(bco->sp,bco->max_sp);
292 static void incSp ( AsmBCO bco, int sp_delta )
294 bco->max_sp = stg_max(bco->sp,bco->max_sp);
296 bco->max_sp = stg_max(bco->sp,bco->max_sp);
299 static void decSp ( AsmBCO bco, int sp_delta )
301 bco->max_sp = stg_max(bco->sp,bco->max_sp);
303 bco->max_sp = stg_max(bco->sp,bco->max_sp);
306 /* --------------------------------------------------------------------------
308 * ------------------------------------------------------------------------*/
310 AsmObject asmMkObject( AsmClosure c )
312 AsmObject obj = malloc(sizeof(struct AsmObject_));
314 barf("Can't allocate AsmObject");
321 AsmCon asmBeginCon( AsmInfo info )
323 AsmCon con = malloc(sizeof(struct AsmCon_));
325 barf("Can't allocate AsmCon");
327 asmBeginObject(&con->object);
332 void asmEndCon( AsmCon con )
334 nat p = con->object.ptrs.len;
335 nat np = stg_max(0,MIN_NONUPD_SIZE-p);
337 StgClosure* c = asmAlloc(CONSTR_sizeW(p,np));
338 StgClosure* o = stgCast(StgClosure*,c);
339 SET_HDR(o,con->info,??);
340 mapQueue(Ptrs, AsmObject, con->object.ptrs, payloadCPtr(o,i) = NULL);
341 { nat i; for( i=0; i<np; ++i ) { payloadWord(o,p+i) = 0xdeadbeef; } }
342 asmEndObject(&con->object,c);
345 AsmCAF asmBeginCAF( void )
347 AsmCAF caf = malloc(sizeof(struct AsmCAF_));
349 barf("Can't allocate AsmCAF");
351 asmBeginObject(&caf->object);
355 void asmEndCAF( AsmCAF caf, AsmBCO body )
357 StgClosure* c = asmAlloc(CAF_sizeW());
358 StgCAF* o = stgCast(StgCAF*,c);
359 SET_HDR(o,&CAF_UNENTERED_info,??);
361 o->value = stgCast(StgClosure*,0xdeadbeef);
362 o->link = stgCast(StgCAF*,0xdeadbeef);
364 asmAddPtr(&caf->object,&body->object);
365 asmEndObject(&caf->object,c);
368 AsmBCO asmBeginBCO( int /*StgExpr*/ e )
370 AsmBCO bco = malloc(sizeof(struct AsmBCO_));
372 barf("Can't allocate AsmBCO");
374 asmBeginObject(&bco->object);
375 initInstrs(&bco->is);
376 initNonPtrs(&bco->nps);
379 bco->max_sp = bco->sp = 0;
380 bco->max_hp = bco->hp = 0;
381 bco->lastOpc = i_INTERNAL_ERROR;
385 void asmEndBCO( AsmBCO bco )
387 nat p = bco->object.ptrs.len;
388 nat np = bco->nps.len;
389 nat is = bco->is.len + (bco->max_sp <= 255 ? 2 : 3); /* 2 or 3 for stack check */
391 StgClosure* c = asmAlloc(BCO_sizeW(p,np,is));
392 StgBCO* o = stgCast(StgBCO*,c);
393 SET_HDR(o,&BCO_info,??);
397 o->stgexpr = bco->stgexpr;
398 mapQueue(Ptrs, AsmObject, bco->object.ptrs, bcoConstCPtr(o,i) = NULL);
399 mapQueue(NonPtrs, StgWord, bco->nps, bcoConstWord(o,i) = x);
402 bco->max_sp = stg_max(bco->sp,bco->max_sp);
403 bco->max_hp = stg_max(bco->hp,bco->max_hp);
405 ASSERT(bco->max_sp <= 65535);
406 if (bco->max_sp <= 255) {
407 bcoInstr(o,j++) = i_STK_CHECK;
408 bcoInstr(o,j++) = bco->max_sp;
410 bcoInstr(o,j++) = i_STK_CHECK_big;
411 bcoInstr(o,j++) = bco->max_sp / 256;
412 bcoInstr(o,j++) = bco->max_sp % 256;
415 mapQueue(Instrs, StgWord8, bco->is, bcoInstr(o,j++) = x);
418 freeInstrs(&bco->is);
419 freeNonPtrs(&bco->nps);
420 asmEndObject(&bco->object,c);
423 /* --------------------------------------------------------------------------
425 * ------------------------------------------------------------------------*/
427 static void asmInstrOp ( AsmBCO bco, StgWord i )
429 ASSERT(i <= BIGGEST_OPCODE); /* must be a valid opcode */
431 insertInstrs(&(bco->is),i);
434 static void asmInstr8 ( AsmBCO bco, StgWord i )
437 ASSERT(i < 256); /* must be a byte */
439 insertInstrs(&(bco->is),i);
442 static void asmInstr16 ( AsmBCO bco, StgWord i )
444 ASSERT(i < 65536); /* must be a short */
445 insertInstrs(&(bco->is),i / 256);
446 insertInstrs(&(bco->is),i % 256);
449 static Instr asmInstrBack ( AsmBCO bco, StgWord n )
451 return bco->is.elems[bco->is.len - n];
454 static void asmInstrRecede ( AsmBCO bco, StgWord n )
456 if (bco->is.len < n) barf("asmInstrRecede");
460 static void asmPtr( AsmBCO bco, AsmObject x )
462 insertPtrs( &bco->object.ptrs, x );
465 static void asmWord( AsmBCO bco, StgWord i )
467 insertNonPtrs( &bco->nps, i );
470 #define asmWords(bco,ty,x) \
472 union { ty a; AsmWord b[sizeofW(ty)]; } p; \
474 if (sizeof(ty) < sizeof(AsmWord)) p.b[0]=0; \
476 for( i = 0; i < sizeofW(ty); i++ ) { \
477 asmWord(bco,p.b[i]); \
481 static StgWord repSizeW( AsmRep rep )
484 case CHAR_REP: return sizeofW(StgWord) + sizeofW(StgChar);
487 case INT_REP: return sizeofW(StgWord) + sizeofW(StgInt);
489 case WORD_REP: return sizeofW(StgWord) + sizeofW(StgWord);
490 case ADDR_REP: return sizeofW(StgWord) + sizeofW(StgAddr);
491 case FLOAT_REP: return sizeofW(StgWord) + sizeofW(StgFloat);
492 case DOUBLE_REP: return sizeofW(StgWord) + sizeofW(StgDouble);
493 case STABLE_REP: return sizeofW(StgWord) + sizeofW(StgWord);
499 #ifdef PROVIDE_FOREIGN
502 case ALPHA_REP: /* a */
503 case BETA_REP: /* b */
504 case GAMMA_REP: /* c */
505 case DELTA_REP: /* d */
506 case HANDLER_REP: /* IOError -> IO a */
507 case ERROR_REP: /* IOError */
508 case ARR_REP : /* PrimArray a */
509 case BARR_REP : /* PrimByteArray a */
510 case REF_REP : /* Ref s a */
511 case MUTARR_REP : /* PrimMutableArray s a */
512 case MUTBARR_REP: /* PrimMutableByteArray s a */
513 case MVAR_REP: /* MVar a */
514 case PTR_REP: return sizeofW(StgPtr);
516 case VOID_REP: return sizeofW(StgWord);
517 default: barf("repSizeW %d",rep);
522 int asmRepSizeW ( AsmRep rep )
524 return repSizeW ( rep );
528 /* --------------------------------------------------------------------------
529 * Instruction emission. All instructions should be routed through here
530 * so that the peephole optimiser gets to see what's happening.
531 * ------------------------------------------------------------------------*/
533 static void emiti_ ( AsmBCO bco, Instr opcode )
536 if (bco->lastOpc == i_SLIDE && opcode == i_ENTER) {
537 /* SLIDE x y ; ENTER ===> SE x y */
538 x = asmInstrBack(bco,2);
539 y = asmInstrBack(bco,1);
540 asmInstrRecede(bco,3);
541 asmInstrOp(bco,i_SE); asmInstr8(bco,x); asmInstr8(bco,y);
544 if (bco->lastOpc == i_RV && opcode == i_ENTER) {
545 /* RV x y ; ENTER ===> RVE x (y-2)
546 Because RETADDR pushes 2 words on the stack, y must be at least 2. */
547 x = asmInstrBack(bco,2);
548 y = asmInstrBack(bco,1);
549 if (y < 2) barf("emiti_: RVE: impossible y value");
550 asmInstrRecede(bco,3);
551 asmInstrOp(bco, i_RVE); asmInstr8(bco,x); asmInstr8(bco,y-2);
554 asmInstrOp(bco,opcode);
558 static void emiti_8 ( AsmBCO bco, Instr opcode, int arg1 )
561 if (bco->lastOpc == i_VAR && opcode == i_VAR) {
562 /* VAR x ; VAR y ===> VV x y */
563 x = asmInstrBack(bco,1);
564 asmInstrRecede(bco,2);
565 asmInstrOp(bco,i_VV); asmInstr8(bco,x); asmInstr8(bco,arg1);
568 if (bco->lastOpc == i_RETADDR && opcode == i_VAR) {
569 /* RETADDR x ; VAR y ===> RV x y */
570 x = asmInstrBack(bco,1);
571 asmInstrRecede(bco,2);
572 asmInstrOp(bco, i_RV); asmInstr8(bco,x); asmInstr8(bco,arg1);
575 asmInstrOp(bco,opcode);
580 static void emiti_16 ( AsmBCO bco, Instr opcode, int arg1 )
582 asmInstrOp(bco,opcode);
583 asmInstr16(bco,arg1);
586 static void emiti_8_8 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
588 asmInstrOp(bco,opcode);
593 static void emiti_8_16 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
595 asmInstrOp(bco,opcode);
597 asmInstr16(bco,arg2);
600 static void emiti_16_16 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
602 asmInstrOp(bco,opcode);
603 asmInstr16(bco,arg1);
604 asmInstr16(bco,arg2);
608 /* --------------------------------------------------------------------------
609 * Wrappers around the above fns
610 * ------------------------------------------------------------------------*/
612 static void emit_i_VAR_INT ( AsmBCO bco, int arg1 )
616 emiti_8 (bco,i_VAR_INT, arg1); else
617 emiti_16(bco,i_VAR_INT_big,arg1);
620 static void emit_i_VAR_WORD ( AsmBCO bco, int arg1 )
624 emiti_8 (bco,i_VAR_WORD, arg1); else
625 emiti_16(bco,i_VAR_WORD_big,arg1);
628 static void emit_i_VAR_ADDR ( AsmBCO bco, int arg1 )
632 emiti_8 (bco,i_VAR_ADDR, arg1); else
633 emiti_16(bco,i_VAR_ADDR_big,arg1);
636 static void emit_i_VAR_CHAR ( AsmBCO bco, int arg1 )
640 emiti_8 (bco,i_VAR_CHAR, arg1); else
641 emiti_16(bco,i_VAR_CHAR_big,arg1);
644 static void emit_i_VAR_FLOAT ( AsmBCO bco, int arg1 )
648 emiti_8 (bco,i_VAR_FLOAT, arg1); else
649 emiti_16(bco,i_VAR_FLOAT_big,arg1);
652 static void emit_i_VAR_DOUBLE ( AsmBCO bco, int arg1 )
656 emiti_8 (bco,i_VAR_DOUBLE, arg1); else
657 emiti_16(bco,i_VAR_DOUBLE_big,arg1);
660 static void emit_i_VAR_STABLE ( AsmBCO bco, int arg1 )
664 emiti_8 (bco,i_VAR_STABLE, arg1); else
665 emiti_16(bco,i_VAR_STABLE_big,arg1);
668 static void emit_i_VAR ( AsmBCO bco, int arg1 )
672 emiti_8 (bco,i_VAR, arg1); else
673 emiti_16(bco,i_VAR_big,arg1);
676 static void emit_i_PACK ( AsmBCO bco, int arg1 )
680 emiti_8 (bco,i_PACK, arg1); else
681 emiti_16(bco,i_PACK_big,arg1);
684 static void emit_i_SLIDE ( AsmBCO bco, int arg1, int arg2 )
688 if (arg1 < 256 && arg2 < 256)
689 emiti_8_8 (bco,i_SLIDE, arg1,arg2); else
690 emiti_16_16(bco,i_SLIDE_big,arg1,arg2);
693 static void emit_i_MKAP ( AsmBCO bco, int arg1, int arg2 )
697 if (arg1 < 256 && arg2 < 256)
698 emiti_8_8 (bco,i_MKAP, arg1,arg2); else
699 emiti_16_16(bco,i_MKAP_big,arg1,arg2);
703 static void emit_i_CONST_INT ( AsmBCO bco, int arg1 )
707 emiti_8 (bco,i_CONST_INT, arg1); else
708 emiti_16(bco,i_CONST_INT_big,arg1);
711 static void emit_i_CONST_INTEGER ( AsmBCO bco, int arg1 )
715 emiti_8 (bco,i_CONST_INTEGER, arg1); else
716 emiti_16(bco,i_CONST_INTEGER_big,arg1);
719 static void emit_i_CONST_ADDR ( AsmBCO bco, int arg1 )
723 emiti_8 (bco,i_CONST_ADDR, arg1); else
724 emiti_16(bco,i_CONST_ADDR_big,arg1);
727 static void emit_i_CONST_CHAR ( AsmBCO bco, int arg1 )
731 emiti_8 (bco,i_CONST_CHAR, arg1); else
732 emiti_16(bco,i_CONST_CHAR_big,arg1);
735 static void emit_i_CONST_FLOAT ( AsmBCO bco, int arg1 )
739 emiti_8 (bco,i_CONST_FLOAT, arg1); else
740 emiti_16(bco,i_CONST_FLOAT_big,arg1);
743 static void emit_i_CONST_DOUBLE ( AsmBCO bco, int arg1 )
747 emiti_8 (bco,i_CONST_DOUBLE, arg1); else
748 emiti_16(bco,i_CONST_DOUBLE_big,arg1);
751 static void emit_i_CONST ( AsmBCO bco, int arg1 )
755 emiti_8 (bco,i_CONST, arg1); else
756 emiti_16(bco,i_CONST_big,arg1);
759 static void emit_i_RETADDR ( AsmBCO bco, int arg1 )
763 emiti_8 (bco,i_RETADDR, arg1); else
764 emiti_16(bco,i_RETADDR_big,arg1);
768 /* --------------------------------------------------------------------------
770 * ------------------------------------------------------------------------*/
772 AsmSp asmBeginArgCheck ( AsmBCO bco )
774 ASSERT(bco->sp == 0);
778 void asmEndArgCheck ( AsmBCO bco, AsmSp last_arg )
780 nat args = bco->sp - last_arg;
781 if (args != 0) { /* optimisation */
782 emiti_8(bco,i_ARG_CHECK,args);
783 grabHpNonUpd(bco,PAP_sizeW(args-1));
788 /* --------------------------------------------------------------------------
789 * Creating and using "variables"
790 * ------------------------------------------------------------------------*/
792 AsmVar asmBind ( AsmBCO bco, AsmRep rep )
794 incSp(bco,repSizeW(rep));
798 void asmVar ( AsmBCO bco, AsmVar v, AsmRep rep )
802 if (rep == VOID_REP) {
804 incSp(bco,repSizeW(rep));
808 offset = bco->sp - v;
812 emit_i_VAR_INT(bco,offset);
816 emit_i_VAR_WORD(bco,offset);
819 emit_i_VAR_ADDR(bco,offset);
822 emit_i_VAR_CHAR(bco,offset);
825 emit_i_VAR_FLOAT(bco,offset);
828 emit_i_VAR_DOUBLE(bco,offset);
831 emit_i_VAR_STABLE(bco,offset);
838 #ifdef PROVIDE_FOREIGN
841 case ALPHA_REP: /* a */
842 case BETA_REP: /* b */
843 case GAMMA_REP: /* c */
844 case DELTA_REP: /* d */
845 case HANDLER_REP: /* IOError -> IO a */
846 case ERROR_REP: /* IOError */
847 case ARR_REP : /* PrimArray a */
848 case BARR_REP : /* PrimByteArray a */
849 case REF_REP : /* Ref s a */
850 case MUTARR_REP : /* PrimMutableArray s a */
851 case MUTBARR_REP: /* PrimMutableByteArray s a */
852 case MVAR_REP: /* MVar a */
854 emit_i_VAR(bco,offset);
857 barf("asmVar %d",rep);
859 incSp(bco,repSizeW(rep));
862 /* --------------------------------------------------------------------------
864 * ------------------------------------------------------------------------*/
866 AsmSp asmBeginEnter( AsmBCO bco )
871 void asmEndEnter( AsmBCO bco, AsmSp sp1, AsmSp sp2 )
873 int x = bco->sp - sp1;
875 ASSERT(x >= 0 && y >= 0);
877 emit_i_SLIDE(bco,x,y);
878 decSp(bco,sp1 - sp2);
881 decSp(bco,sizeofW(StgPtr));
884 /* --------------------------------------------------------------------------
885 * Build boxed Ints, Floats, etc
886 * ------------------------------------------------------------------------*/
888 AsmVar asmBox( AsmBCO bco, AsmRep rep )
892 emiti_(bco,i_PACK_CHAR);
893 grabHpNonUpd(bco,Czh_sizeW);
896 emiti_(bco,i_PACK_INT);
897 grabHpNonUpd(bco,Izh_sizeW);
901 emiti_(bco,i_PACK_WORD);
902 grabHpNonUpd(bco,Wzh_sizeW);
905 emiti_(bco,i_PACK_ADDR);
906 grabHpNonUpd(bco,Azh_sizeW);
909 emiti_(bco,i_PACK_FLOAT);
910 grabHpNonUpd(bco,Fzh_sizeW);
913 emiti_(bco,i_PACK_DOUBLE);
914 grabHpNonUpd(bco,Dzh_sizeW);
917 emiti_(bco,i_PACK_STABLE);
918 grabHpNonUpd(bco,Stablezh_sizeW);
922 barf("asmBox %d",rep);
924 /* NB: these operations DO pop their arg */
925 decSp(bco, repSizeW(rep)); /* pop unboxed arg */
926 incSp(bco, sizeofW(StgPtr)); /* push box */
930 /* --------------------------------------------------------------------------
931 * Unbox Ints, Floats, etc
932 * ------------------------------------------------------------------------*/
934 AsmVar asmUnbox( AsmBCO bco, AsmRep rep )
938 emiti_(bco,i_UNPACK_INT);
942 emiti_(bco,i_UNPACK_WORD);
945 emiti_(bco,i_UNPACK_ADDR);
948 emiti_(bco,i_UNPACK_CHAR);
951 emiti_(bco,i_UNPACK_FLOAT);
954 emiti_(bco,i_UNPACK_DOUBLE);
957 emiti_(bco,i_UNPACK_STABLE);
960 barf("asmUnbox %d",rep);
962 /* NB: these operations DO NOT pop their arg */
963 incSp(bco, repSizeW(rep)); /* push unboxed arg */
968 /* --------------------------------------------------------------------------
969 * Push unboxed Ints, Floats, etc
970 * ------------------------------------------------------------------------*/
972 void asmConstInt( AsmBCO bco, AsmInt x )
974 emit_i_CONST_INT(bco,bco->nps.len);
975 asmWords(bco,AsmInt,x);
976 incSp(bco, repSizeW(INT_REP));
979 void asmConstInteger( AsmBCO bco, AsmString x )
981 emit_i_CONST_INTEGER(bco,bco->nps.len);
982 asmWords(bco,AsmString,x);
983 incSp(bco, repSizeW(INTEGER_REP));
986 void asmConstAddr( AsmBCO bco, AsmAddr x )
988 emit_i_CONST_ADDR(bco,bco->nps.len);
989 asmWords(bco,AsmAddr,x);
990 incSp(bco, repSizeW(ADDR_REP));
993 void asmConstWord( AsmBCO bco, AsmWord x )
995 emit_i_CONST_INT(bco,bco->nps.len);
996 asmWords(bco,AsmWord,(AsmInt)x);
997 incSp(bco, repSizeW(WORD_REP));
1000 void asmConstChar( AsmBCO bco, AsmChar x )
1002 emit_i_CONST_CHAR(bco,bco->nps.len);
1003 asmWords(bco,AsmChar,x);
1004 incSp(bco, repSizeW(CHAR_REP));
1007 void asmConstFloat( AsmBCO bco, AsmFloat x )
1009 emit_i_CONST_FLOAT(bco,bco->nps.len);
1010 asmWords(bco,AsmFloat,x);
1011 incSp(bco, repSizeW(FLOAT_REP));
1014 void asmConstDouble( AsmBCO bco, AsmDouble x )
1016 emit_i_CONST_DOUBLE(bco,bco->nps.len);
1017 asmWords(bco,AsmDouble,x);
1018 incSp(bco, repSizeW(DOUBLE_REP));
1021 /* --------------------------------------------------------------------------
1022 * Algebraic case helpers
1023 * ------------------------------------------------------------------------*/
1025 /* a mildly bogus pair of functions... */
1026 AsmSp asmBeginCase( AsmBCO bco )
1031 void asmEndCase( AsmBCO bco )
1035 AsmSp asmContinuation( AsmBCO bco, AsmBCO ret_addr )
1037 emit_i_RETADDR(bco,bco->object.ptrs.len);
1038 asmPtr(bco,&(ret_addr->object));
1039 incSp(bco, 2 * sizeofW(StgPtr));
1043 AsmBCO asmBeginContinuation ( AsmSp sp, int /*List*/ alts )
1045 AsmBCO bco = asmBeginBCO(alts);
1050 void asmEndContinuation ( AsmBCO bco )
1056 /* --------------------------------------------------------------------------
1058 * ------------------------------------------------------------------------*/
1060 AsmSp asmBeginAlt( AsmBCO bco )
1065 void asmEndAlt( AsmBCO bco, AsmSp sp )
1070 AsmPc asmTest( AsmBCO bco, AsmWord tag )
1072 emiti_8_16(bco,i_TEST,tag,0);
1076 AsmPc asmTestInt( AsmBCO bco, AsmVar v, AsmInt x )
1078 asmVar(bco,v,INT_REP);
1080 emiti_16(bco,i_TEST_INT,0);
1081 decSp(bco, 2*repSizeW(INT_REP));
1085 void asmFixBranch( AsmBCO bco, AsmPc from )
1087 int distance = bco->is.len - from;
1088 ASSERT(distance >= 0);
1089 ASSERT(distance < 65536);
1090 setInstrs(&(bco->is),from-2,distance/256);
1091 setInstrs(&(bco->is),from-1,distance%256);
1094 void asmPanic( AsmBCO bco )
1096 emiti_(bco,i_PANIC); /* "irrefutable" pattern failed - oops! */
1099 /* --------------------------------------------------------------------------
1101 * ------------------------------------------------------------------------*/
1103 AsmSp asmBeginPrim( AsmBCO bco )
1108 void asmEndPrim( AsmBCO bco, const AsmPrim* prim, AsmSp base )
1110 emiti_8(bco,prim->prefix,prim->opcode);
1114 char* asmGetPrimopName ( AsmPrim* p )
1119 /* Hugs used to let you add arbitrary primops with arbitrary types
1120 * just by editing Prelude.hs or any other file you wanted.
1121 * We deliberately avoided that approach because we wanted more
1122 * control over which primops are provided.
1124 AsmPrim asmPrimOps[] = {
1126 /* Char# operations */
1127 { "primGtChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_gtChar }
1128 , { "primGeChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_geChar }
1129 , { "primEqChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_eqChar }
1130 , { "primNeChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_neChar }
1131 , { "primLtChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_ltChar }
1132 , { "primLeChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_leChar }
1133 , { "primCharToInt", "C", "I", MONAD_Id, i_PRIMOP1, i_charToInt }
1134 , { "primIntToChar", "I", "C", MONAD_Id, i_PRIMOP1, i_intToChar }
1136 /* Int# operations */
1137 , { "primGtInt", "II", "B", MONAD_Id, i_PRIMOP1, i_gtInt }
1138 , { "primGeInt", "II", "B", MONAD_Id, i_PRIMOP1, i_geInt }
1139 , { "primEqInt", "II", "B", MONAD_Id, i_PRIMOP1, i_eqInt }
1140 , { "primNeInt", "II", "B", MONAD_Id, i_PRIMOP1, i_neInt }
1141 , { "primLtInt", "II", "B", MONAD_Id, i_PRIMOP1, i_ltInt }
1142 , { "primLeInt", "II", "B", MONAD_Id, i_PRIMOP1, i_leInt }
1143 , { "primMinInt", "", "I", MONAD_Id, i_PRIMOP1, i_minInt }
1144 , { "primMaxInt", "", "I", MONAD_Id, i_PRIMOP1, i_maxInt }
1145 , { "primPlusInt", "II", "I", MONAD_Id, i_PRIMOP1, i_plusInt }
1146 , { "primMinusInt", "II", "I", MONAD_Id, i_PRIMOP1, i_minusInt }
1147 , { "primTimesInt", "II", "I", MONAD_Id, i_PRIMOP1, i_timesInt }
1148 , { "primQuotInt", "II", "I", MONAD_Id, i_PRIMOP1, i_quotInt }
1149 , { "primRemInt", "II", "I", MONAD_Id, i_PRIMOP1, i_remInt }
1150 , { "primQuotRemInt", "II", "II", MONAD_Id, i_PRIMOP1, i_quotRemInt }
1151 , { "primNegateInt", "I", "I", MONAD_Id, i_PRIMOP1, i_negateInt }
1153 , { "primAndInt", "II", "I", MONAD_Id, i_PRIMOP1, i_andInt }
1154 , { "primOrInt", "II", "I", MONAD_Id, i_PRIMOP1, i_orInt }
1155 , { "primXorInt", "II", "I", MONAD_Id, i_PRIMOP1, i_xorInt }
1156 , { "primNotInt", "I", "I", MONAD_Id, i_PRIMOP1, i_notInt }
1157 , { "primShiftLInt", "II", "I", MONAD_Id, i_PRIMOP1, i_shiftLInt }
1158 , { "primShiftRAInt", "II", "I", MONAD_Id, i_PRIMOP1, i_shiftRAInt }
1159 , { "primShiftRLInt", "II", "I", MONAD_Id, i_PRIMOP1, i_shiftRLInt }
1161 /* Word# operations */
1162 , { "primGtWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_gtWord }
1163 , { "primGeWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_geWord }
1164 , { "primEqWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_eqWord }
1165 , { "primNeWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_neWord }
1166 , { "primLtWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_ltWord }
1167 , { "primLeWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_leWord }
1168 , { "primMinWord", "", "W", MONAD_Id, i_PRIMOP1, i_minWord }
1169 , { "primMaxWord", "", "W", MONAD_Id, i_PRIMOP1, i_maxWord }
1170 , { "primPlusWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_plusWord }
1171 , { "primMinusWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_minusWord }
1172 , { "primTimesWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_timesWord }
1173 , { "primQuotWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_quotWord }
1174 , { "primRemWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_remWord }
1175 , { "primQuotRemWord", "WW", "WW", MONAD_Id, i_PRIMOP1, i_quotRemWord }
1176 , { "primNegateWord", "W", "W", MONAD_Id, i_PRIMOP1, i_negateWord }
1178 , { "primAndWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_andWord }
1179 , { "primOrWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_orWord }
1180 , { "primXorWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_xorWord }
1181 , { "primNotWord", "W", "W", MONAD_Id, i_PRIMOP1, i_notWord }
1182 , { "primShiftLWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_shiftLWord }
1183 , { "primShiftRAWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_shiftRAWord }
1184 , { "primShiftRLWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_shiftRLWord }
1186 , { "primIntToWord", "I", "W", MONAD_Id, i_PRIMOP1, i_intToWord }
1187 , { "primWordToInt", "W", "I", MONAD_Id, i_PRIMOP1, i_wordToInt }
1189 /* Addr# operations */
1190 , { "primGtAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_gtAddr }
1191 , { "primGeAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_geAddr }
1192 , { "primEqAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_eqAddr }
1193 , { "primNeAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_neAddr }
1194 , { "primLtAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_ltAddr }
1195 , { "primLeAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_leAddr }
1196 , { "primIntToAddr", "I", "A", MONAD_Id, i_PRIMOP1, i_intToAddr }
1197 , { "primAddrToInt", "A", "I", MONAD_Id, i_PRIMOP1, i_addrToInt }
1199 , { "primIndexCharOffAddr", "AI", "C", MONAD_Id, i_PRIMOP1, i_indexCharOffAddr }
1200 , { "primIndexIntOffAddr", "AI", "I", MONAD_Id, i_PRIMOP1, i_indexIntOffAddr }
1201 , { "primIndexWordOffAddr", "AI", "W", MONAD_Id, i_PRIMOP1, i_indexWordOffAddr }
1202 , { "primIndexAddrOffAddr", "AI", "A", MONAD_Id, i_PRIMOP1, i_indexAddrOffAddr }
1203 , { "primIndexFloatOffAddr", "AI", "F", MONAD_Id, i_PRIMOP1, i_indexFloatOffAddr }
1204 , { "primIndexDoubleOffAddr", "AI", "D", MONAD_Id, i_PRIMOP1, i_indexDoubleOffAddr }
1205 , { "primIndexStableOffAddr", "AI", "s", MONAD_Id, i_PRIMOP1, i_indexStableOffAddr }
1207 /* Stable# operations */
1208 , { "primIntToStablePtr", "I", "s", MONAD_Id, i_PRIMOP1, i_intToStable }
1209 , { "primStablePtrToInt", "s", "I", MONAD_Id, i_PRIMOP1, i_stableToInt }
1211 /* These ops really ought to be in the IO monad */
1212 , { "primReadCharOffAddr", "AI", "C", MONAD_ST, i_PRIMOP1, i_readCharOffAddr }
1213 , { "primReadIntOffAddr", "AI", "I", MONAD_ST, i_PRIMOP1, i_readIntOffAddr }
1214 , { "primReadWordOffAddr", "AI", "W", MONAD_ST, i_PRIMOP1, i_readWordOffAddr }
1215 , { "primReadAddrOffAddr", "AI", "A", MONAD_ST, i_PRIMOP1, i_readAddrOffAddr }
1216 , { "primReadFloatOffAddr", "AI", "F", MONAD_ST, i_PRIMOP1, i_readFloatOffAddr }
1217 , { "primReadDoubleOffAddr", "AI", "D", MONAD_ST, i_PRIMOP1, i_readDoubleOffAddr }
1218 , { "primReadStableOffAddr", "AI", "s", MONAD_ST, i_PRIMOP1, i_readStableOffAddr }
1220 /* These ops really ought to be in the IO monad */
1221 , { "primWriteCharOffAddr", "AIC", "", MONAD_ST, i_PRIMOP1, i_writeCharOffAddr }
1222 , { "primWriteIntOffAddr", "AII", "", MONAD_ST, i_PRIMOP1, i_writeIntOffAddr }
1223 , { "primWriteWordOffAddr", "AIW", "", MONAD_ST, i_PRIMOP1, i_writeWordOffAddr }
1224 , { "primWriteAddrOffAddr", "AIA", "", MONAD_ST, i_PRIMOP1, i_writeAddrOffAddr }
1225 , { "primWriteFloatOffAddr", "AIF", "", MONAD_ST, i_PRIMOP1, i_writeFloatOffAddr }
1226 , { "primWriteDoubleOffAddr", "AID", "", MONAD_ST, i_PRIMOP1, i_writeDoubleOffAddr }
1227 , { "primWriteStableOffAddr", "AIs", "", MONAD_ST, i_PRIMOP1, i_writeStableOffAddr }
1229 /* Integer operations */
1230 , { "primCompareInteger", "ZZ", "I", MONAD_Id, i_PRIMOP1, i_compareInteger }
1231 , { "primNegateInteger", "Z", "Z", MONAD_Id, i_PRIMOP1, i_negateInteger }
1232 , { "primPlusInteger", "ZZ", "Z", MONAD_Id, i_PRIMOP1, i_plusInteger }
1233 , { "primMinusInteger", "ZZ", "Z", MONAD_Id, i_PRIMOP1, i_minusInteger }
1234 , { "primTimesInteger", "ZZ", "Z", MONAD_Id, i_PRIMOP1, i_timesInteger }
1235 , { "primQuotRemInteger", "ZZ", "ZZ", MONAD_Id, i_PRIMOP1, i_quotRemInteger }
1236 , { "primDivModInteger", "ZZ", "ZZ", MONAD_Id, i_PRIMOP1, i_divModInteger }
1237 , { "primIntegerToInt", "Z", "I", MONAD_Id, i_PRIMOP1, i_integerToInt }
1238 , { "primIntToInteger", "I", "Z", MONAD_Id, i_PRIMOP1, i_intToInteger }
1239 , { "primIntegerToWord", "Z", "W", MONAD_Id, i_PRIMOP1, i_integerToWord }
1240 , { "primWordToInteger", "W", "Z", MONAD_Id, i_PRIMOP1, i_wordToInteger }
1241 , { "primIntegerToFloat", "Z", "F", MONAD_Id, i_PRIMOP1, i_integerToFloat }
1242 , { "primFloatToInteger", "F", "Z", MONAD_Id, i_PRIMOP1, i_floatToInteger }
1243 , { "primIntegerToDouble", "Z", "D", MONAD_Id, i_PRIMOP1, i_integerToDouble }
1244 , { "primDoubleToInteger", "D", "Z", MONAD_Id, i_PRIMOP1, i_doubleToInteger }
1246 /* Float# operations */
1247 , { "primGtFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_gtFloat }
1248 , { "primGeFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_geFloat }
1249 , { "primEqFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_eqFloat }
1250 , { "primNeFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_neFloat }
1251 , { "primLtFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_ltFloat }
1252 , { "primLeFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_leFloat }
1253 , { "primMinFloat", "", "F", MONAD_Id, i_PRIMOP1, i_minFloat }
1254 , { "primMaxFloat", "", "F", MONAD_Id, i_PRIMOP1, i_maxFloat }
1255 , { "primRadixFloat", "", "I", MONAD_Id, i_PRIMOP1, i_radixFloat }
1256 , { "primDigitsFloat", "", "I", MONAD_Id, i_PRIMOP1, i_digitsFloat }
1257 , { "primMinExpFloat", "", "I", MONAD_Id, i_PRIMOP1, i_minExpFloat }
1258 , { "primMaxExpFloat", "", "I", MONAD_Id, i_PRIMOP1, i_maxExpFloat }
1259 , { "primPlusFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_plusFloat }
1260 , { "primMinusFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_minusFloat }
1261 , { "primTimesFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_timesFloat }
1262 , { "primDivideFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_divideFloat }
1263 , { "primNegateFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_negateFloat }
1264 , { "primFloatToInt", "F", "I", MONAD_Id, i_PRIMOP1, i_floatToInt }
1265 , { "primIntToFloat", "I", "F", MONAD_Id, i_PRIMOP1, i_intToFloat }
1266 , { "primExpFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_expFloat }
1267 , { "primLogFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_logFloat }
1268 , { "primSqrtFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_sqrtFloat }
1269 , { "primSinFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_sinFloat }
1270 , { "primCosFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_cosFloat }
1271 , { "primTanFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_tanFloat }
1272 , { "primAsinFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_asinFloat }
1273 , { "primAcosFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_acosFloat }
1274 , { "primAtanFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_atanFloat }
1275 , { "primSinhFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_sinhFloat }
1276 , { "primCoshFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_coshFloat }
1277 , { "primTanhFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_tanhFloat }
1278 , { "primPowerFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_powerFloat }
1279 , { "primDecodeFloatZ", "F", "ZI", MONAD_Id, i_PRIMOP1, i_decodeFloatZ }
1280 , { "primEncodeFloatZ", "ZI", "F", MONAD_Id, i_PRIMOP1, i_encodeFloatZ }
1281 , { "primIsNaNFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isNaNFloat }
1282 , { "primIsInfiniteFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isInfiniteFloat }
1283 , { "primIsDenormalizedFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isDenormalizedFloat }
1284 , { "primIsNegativeZeroFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isNegativeZeroFloat }
1285 , { "primIsIEEEFloat", "", "B", MONAD_Id, i_PRIMOP1, i_isIEEEFloat }
1287 /* Double# operations */
1288 , { "primGtDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_gtDouble }
1289 , { "primGeDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_geDouble }
1290 , { "primEqDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_eqDouble }
1291 , { "primNeDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_neDouble }
1292 , { "primLtDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_ltDouble }
1293 , { "primLeDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_leDouble }
1294 , { "primMinDouble", "", "D", MONAD_Id, i_PRIMOP1, i_minDouble }
1295 , { "primMaxDouble", "", "D", MONAD_Id, i_PRIMOP1, i_maxDouble }
1296 , { "primRadixDouble", "", "I", MONAD_Id, i_PRIMOP1, i_radixDouble }
1297 , { "primDigitsDouble", "", "I", MONAD_Id, i_PRIMOP1, i_digitsDouble }
1298 , { "primMinExpDouble", "", "I", MONAD_Id, i_PRIMOP1, i_minExpDouble }
1299 , { "primMaxExpDouble", "", "I", MONAD_Id, i_PRIMOP1, i_maxExpDouble }
1300 , { "primPlusDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_plusDouble }
1301 , { "primMinusDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_minusDouble }
1302 , { "primTimesDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_timesDouble }
1303 , { "primDivideDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_divideDouble }
1304 , { "primNegateDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_negateDouble }
1305 , { "primDoubleToInt", "D", "I", MONAD_Id, i_PRIMOP1, i_doubleToInt }
1306 , { "primIntToDouble", "I", "D", MONAD_Id, i_PRIMOP1, i_intToDouble }
1307 , { "primDoubleToFloat", "D", "F", MONAD_Id, i_PRIMOP1, i_doubleToFloat }
1308 , { "primFloatToDouble", "F", "D", MONAD_Id, i_PRIMOP1, i_floatToDouble }
1309 , { "primExpDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_expDouble }
1310 , { "primLogDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_logDouble }
1311 , { "primSqrtDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_sqrtDouble }
1312 , { "primSinDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_sinDouble }
1313 , { "primCosDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_cosDouble }
1314 , { "primTanDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_tanDouble }
1315 , { "primAsinDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_asinDouble }
1316 , { "primAcosDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_acosDouble }
1317 , { "primAtanDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_atanDouble }
1318 , { "primSinhDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_sinhDouble }
1319 , { "primCoshDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_coshDouble }
1320 , { "primTanhDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_tanhDouble }
1321 , { "primPowerDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_powerDouble }
1322 , { "primDecodeDoubleZ", "D", "ZI", MONAD_Id, i_PRIMOP1, i_decodeDoubleZ }
1323 , { "primEncodeDoubleZ", "ZI", "D", MONAD_Id, i_PRIMOP1, i_encodeDoubleZ }
1324 , { "primIsNaNDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isNaNDouble }
1325 , { "primIsInfiniteDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isInfiniteDouble }
1326 , { "primIsDenormalizedDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isDenormalizedDouble }
1327 , { "primIsNegativeZeroDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isNegativeZeroDouble }
1328 , { "primIsIEEEDouble", "", "B", MONAD_Id, i_PRIMOP1, i_isIEEEDouble }
1330 /* Ref operations */
1331 , { "primNewRef", "a", "R", MONAD_ST, i_PRIMOP2, i_newRef }
1332 , { "primWriteRef", "Ra", "", MONAD_ST, i_PRIMOP2, i_writeRef }
1333 , { "primReadRef", "R", "a", MONAD_ST, i_PRIMOP2, i_readRef }
1334 , { "primSameRef", "RR", "B", MONAD_Id, i_PRIMOP2, i_sameRef }
1336 /* PrimArray operations */
1337 , { "primSameMutableArray", "MM", "B", MONAD_Id, i_PRIMOP2, i_sameMutableArray }
1338 , { "primUnsafeFreezeArray", "M", "X", MONAD_ST, i_PRIMOP2, i_unsafeFreezeArray }
1339 , { "primNewArray", "Ia", "M", MONAD_ST, i_PRIMOP2, i_newArray }
1340 , { "primWriteArray", "MIa", "", MONAD_ST, i_PRIMOP2, i_writeArray }
1341 , { "primReadArray", "MI", "a", MONAD_ST, i_PRIMOP2, i_readArray }
1342 , { "primIndexArray", "XI", "a", MONAD_Id, i_PRIMOP2, i_indexArray }
1343 , { "primSizeArray", "X", "I", MONAD_Id, i_PRIMOP2, i_sizeArray }
1344 , { "primSizeMutableArray", "M", "I", MONAD_Id, i_PRIMOP2, i_sizeMutableArray }
1346 /* Prim[Mutable]ByteArray operations */
1347 , { "primSameMutableByteArray", "mm", "B", MONAD_Id, i_PRIMOP2, i_sameMutableByteArray }
1348 , { "primUnsafeFreezeByteArray", "m", "x", MONAD_ST, i_PRIMOP2, i_unsafeFreezeByteArray }
1350 , { "primNewByteArray", "I", "m", MONAD_ST, i_PRIMOP2, i_newByteArray }
1352 , { "primWriteCharArray", "mIC", "", MONAD_ST, i_PRIMOP2, i_writeCharArray }
1353 , { "primReadCharArray", "mI", "C", MONAD_ST, i_PRIMOP2, i_readCharArray }
1354 , { "primIndexCharArray", "xI", "C", MONAD_Id, i_PRIMOP2, i_indexCharArray }
1356 , { "primWriteIntArray", "mII", "", MONAD_ST, i_PRIMOP2, i_writeIntArray }
1357 , { "primReadIntArray", "mI", "I", MONAD_ST, i_PRIMOP2, i_readIntArray }
1358 , { "primIndexIntArray", "xI", "I", MONAD_Id, i_PRIMOP2, i_indexIntArray }
1360 /* {new,write,read,index}IntegerArray not provided */
1362 , { "primWriteWordArray", "mIW", "", MONAD_ST, i_PRIMOP2, i_writeWordArray }
1363 , { "primReadWordArray", "mI", "W", MONAD_ST, i_PRIMOP2, i_readWordArray }
1364 , { "primIndexWordArray", "xI", "W", MONAD_Id, i_PRIMOP2, i_indexWordArray }
1365 , { "primWriteAddrArray", "mIA", "", MONAD_ST, i_PRIMOP2, i_writeAddrArray }
1366 , { "primReadAddrArray", "mI", "A", MONAD_ST, i_PRIMOP2, i_readAddrArray }
1367 , { "primIndexAddrArray", "xI", "A", MONAD_Id, i_PRIMOP2, i_indexAddrArray }
1368 , { "primWriteFloatArray", "mIF", "", MONAD_ST, i_PRIMOP2, i_writeFloatArray }
1369 , { "primReadFloatArray", "mI", "F", MONAD_ST, i_PRIMOP2, i_readFloatArray }
1370 , { "primIndexFloatArray", "xI", "F", MONAD_Id, i_PRIMOP2, i_indexFloatArray }
1371 , { "primWriteDoubleArray" , "mID", "", MONAD_ST, i_PRIMOP2, i_writeDoubleArray }
1372 , { "primReadDoubleArray", "mI", "D", MONAD_ST, i_PRIMOP2, i_readDoubleArray }
1373 , { "primIndexDoubleArray", "xI", "D", MONAD_Id, i_PRIMOP2, i_indexDoubleArray }
1376 #ifdef PROVIDE_STABLE
1377 , { "primWriteStableArray", "mIs", "", MONAD_ST, i_PRIMOP2, i_writeStableArray }
1378 , { "primReadStableArray", "mI", "s", MONAD_ST, i_PRIMOP2, i_readStableArray }
1379 , { "primIndexStableArray", "xI", "s", MONAD_Id, i_PRIMOP2, i_indexStableArray }
1382 /* {new,write,read,index}ForeignObjArray not provided */
1385 #ifdef PROVIDE_FOREIGN
1386 /* ForeignObj# operations */
1387 , { "primMakeForeignObj", "A", "f", MONAD_IO, i_PRIMOP2, i_makeForeignObj }
1390 /* WeakPair# operations */
1391 , { "primMakeWeak", "bac", "w", MONAD_IO, i_PRIMOP2, i_makeWeak }
1392 , { "primDeRefWeak", "w", "Ia", MONAD_IO, i_PRIMOP2, i_deRefWeak }
1394 /* StablePtr# operations */
1395 , { "primMakeStablePtr", "a", "s", MONAD_IO, i_PRIMOP2, i_makeStablePtr }
1396 , { "primDeRefStablePtr", "s", "a", MONAD_IO, i_PRIMOP2, i_deRefStablePtr }
1397 , { "primFreeStablePtr", "s", "", MONAD_IO, i_PRIMOP2, i_freeStablePtr }
1399 /* foreign export dynamic support */
1400 , { "primCreateAdjThunkARCH", "sAC","A", MONAD_IO, i_PRIMOP2, i_createAdjThunkARCH }
1402 /* misc handy hacks */
1403 , { "primGetArgc", "", "I", MONAD_IO, i_PRIMOP2, i_getArgc }
1404 , { "primGetArgv", "I", "A", MONAD_IO, i_PRIMOP2, i_getArgv }
1406 #ifdef PROVIDE_PTREQUALITY
1407 , { "primReallyUnsafePtrEquality", "aa", "B",MONAD_Id, i_PRIMOP2, i_reallyUnsafePtrEquality }
1409 #ifdef PROVIDE_COERCE
1410 , { "primUnsafeCoerce", "a", "b", MONAD_Id, i_PRIMOP2, i_unsafeCoerce }
1412 #ifdef PROVIDE_CONCURRENT
1413 /* Concurrency operations */
1414 , { "primFork", "a", "T", MONAD_IO, i_PRIMOP2, i_fork }
1415 , { "primKillThread", "T", "", MONAD_IO, i_PRIMOP2, i_killThread }
1416 , { "primDelay", "I", "", MONAD_IO, i_PRIMOP2, i_delay }
1417 , { "primWaitRead", "I", "", MONAD_IO, i_PRIMOP2, i_waitRead }
1418 , { "primWaitWrite", "I", "", MONAD_IO, i_PRIMOP2, i_waitWrite }
1420 , { "primNewEmptyMVar", "", "r", MONAD_IO, i_PRIMOP2, i_newMVar }
1421 /* primTakeMVar is handwritten bytecode */
1422 , { "primPutMVar", "ra", "", MONAD_IO, i_PRIMOP2, i_putMVar }
1423 , { "primSameMVar", "rr", "B", MONAD_Id, i_PRIMOP2, i_sameMVar }
1424 , { "primGetThreadId", "", "T", MONAD_IO, i_PRIMOP2, i_getThreadId }
1425 , { "primCmpThreadIds", "TT", "I", MONAD_Id, i_PRIMOP2, i_cmpThreadIds }
1426 , { "primForkIO", "a", "T", MONAD_IO, i_PRIMOP2, i_forkIO }
1428 /* Ccall is polyadic - so it's excluded from this table */
1433 AsmPrim ccall_ccall_Id
1434 = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_ccall_Id };
1435 AsmPrim ccall_ccall_IO
1436 = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_ccall_IO };
1437 AsmPrim ccall_stdcall_Id
1438 = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_stdcall_Id };
1439 AsmPrim ccall_stdcall_IO
1440 = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_stdcall_IO };
1443 AsmPrim* asmFindPrim( char* s )
1446 for (i=0; asmPrimOps[i].name; ++i) {
1447 if (strcmp(s,asmPrimOps[i].name)==0) {
1448 return &asmPrimOps[i];
1454 AsmPrim* asmFindPrimop( AsmInstr prefix, AsmInstr op )
1457 for (i=0; asmPrimOps[i].name; ++i) {
1458 if (asmPrimOps[i].prefix == prefix && asmPrimOps[i].opcode == op) {
1459 return &asmPrimOps[i];
1465 /* --------------------------------------------------------------------------
1466 * Handwritten primops
1467 * ------------------------------------------------------------------------*/
1469 AsmBCO asm_BCO_catch ( void )
1471 AsmBCO bco = asmBeginBCO(0 /*NIL*/);
1472 emiti_8(bco,i_ARG_CHECK,2);
1473 emiti_8(bco,i_PRIMOP1,i_pushcatchframe);
1474 incSp(bco, (1-2)*sizeofW(StgPtr) + sizeofW(StgCatchFrame));
1475 emiti_(bco,i_ENTER);
1476 decSp(bco, sizeofW(StgPtr));
1481 AsmBCO asm_BCO_raise ( void )
1483 AsmBCO bco = asmBeginBCO(0 /*NIL*/);
1484 emiti_8(bco,i_ARG_CHECK,1);
1485 emiti_8(bco,i_PRIMOP2,i_raise);
1486 decSp(bco,sizeofW(StgPtr));
1491 AsmBCO asm_BCO_seq ( void )
1495 cont = asmBeginBCO(0 /*NIL*/);
1496 emiti_8(cont,i_ARG_CHECK,2); /* should never fail */
1498 emit_i_SLIDE(cont,1,2);
1499 emiti_(cont,i_ENTER);
1500 incSp(cont, 3*sizeofW(StgPtr));
1503 eval = asmBeginBCO(0 /*NIL*/);
1504 emiti_8(eval,i_ARG_CHECK,2);
1505 emit_i_RETADDR(eval,eval->object.ptrs.len);
1506 asmPtr(eval,&(cont->object));
1508 emit_i_SLIDE(eval,3,1);
1509 emiti_8(eval,i_PRIMOP1,i_pushseqframe);
1510 emiti_(eval,i_ENTER);
1511 incSp(eval, sizeofW(StgSeqFrame) + 4*sizeofW(StgPtr));
1517 AsmBCO asm_BCO_takeMVar ( void )
1519 AsmBCO kase, casecont, take;
1521 take = asmBeginBCO(0 /*NIL*/);
1523 emiti_8(take,i_PRIMOP2,i_takeMVar);
1527 emit_i_SLIDE(take,3,4);
1528 emiti_(take,i_ENTER);
1532 casecont = asmBeginBCO(0 /*NIL*/);
1533 emiti_(casecont,i_UNPACK);
1534 emit_i_VAR(casecont,4);
1535 emit_i_VAR(casecont,4);
1536 emit_i_VAR(casecont,2);
1537 emit_i_CONST(casecont,casecont->object.ptrs.len);
1538 asmPtr(casecont,&(take->object));
1539 emit_i_SLIDE(casecont,4,5);
1540 emiti_(casecont,i_ENTER);
1542 asmEndBCO(casecont);
1544 kase = asmBeginBCO(0 /*NIL*/);
1545 emiti_8(kase,i_ARG_CHECK,3);
1546 emit_i_RETADDR(kase,kase->object.ptrs.len);
1547 asmPtr(kase,&(casecont->object));
1549 emiti_(kase,i_ENTER);
1557 /* --------------------------------------------------------------------------
1559 * ------------------------------------------------------------------------*/
1561 AsmVar asmAllocCONSTR ( AsmBCO bco, AsmInfo info )
1563 ASSERT( sizeW_fromITBL(info) >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
1564 emiti_8(bco,i_ALLOC_CONSTR,bco->nps.len);
1565 asmWords(bco,AsmInfo,info);
1566 incSp(bco, sizeofW(StgClosurePtr));
1567 grabHpNonUpd(bco,sizeW_fromITBL(info));
1571 AsmSp asmBeginPack( AsmBCO bco )
1576 void asmEndPack( AsmBCO bco, AsmVar v, AsmSp start, AsmInfo info )
1578 nat size = bco->sp - start;
1579 assert(bco->sp >= start);
1581 /* only reason to include info is for this assertion */
1582 assert(info->layout.payload.ptrs == size);
1583 emit_i_PACK(bco, bco->sp - v);
1587 void asmBeginUnpack( AsmBCO bco )
1589 /* dummy to make it look prettier */
1592 void asmEndUnpack( AsmBCO bco )
1594 emiti_(bco,i_UNPACK);
1597 AsmVar asmAllocAP( AsmBCO bco, AsmNat words )
1599 emiti_8(bco,i_ALLOC_AP,words);
1600 incSp(bco, sizeofW(StgPtr));
1601 grabHpUpd(bco,AP_sizeW(words));
1605 AsmSp asmBeginMkAP( AsmBCO bco )
1610 void asmEndMkAP( AsmBCO bco, AsmVar v, AsmSp start )
1612 emit_i_MKAP(bco,bco->sp-v,bco->sp-start-1);
1613 /* -1 because fun isn't counted */
1617 AsmVar asmAllocPAP( AsmBCO bco, AsmNat size )
1619 emiti_8(bco,i_ALLOC_PAP,size);
1620 incSp(bco, sizeofW(StgPtr));
1624 AsmSp asmBeginMkPAP( AsmBCO bco )
1629 void asmEndMkPAP( AsmBCO bco, AsmVar v, AsmSp start )
1631 emiti_8_8(bco,i_MKPAP,bco->sp-v,bco->sp-start-1);
1632 /* -1 because fun isn't counted */
1636 AsmVar asmClosure( AsmBCO bco, AsmObject p )
1638 emit_i_CONST(bco,bco->object.ptrs.len);
1640 incSp(bco, sizeofW(StgPtr));
1644 AsmVar asmGHCClosure( AsmBCO bco, AsmObject p )
1646 // A complete hack. Pushes the address as a tagged int
1647 // and then uses SLIDE to get rid of the tag. Appalling.
1648 asmConstInt(bco, (AsmInt)p);
1649 emit_i_SLIDE(bco,0,1); decSp(bco,1);
1654 /* --------------------------------------------------------------------------
1655 * Building InfoTables
1656 * ------------------------------------------------------------------------*/
1658 AsmInfo asmMkInfo( AsmNat tag, AsmNat ptrs )
1660 StgInfoTable* info = stgMallocBytes( sizeof(StgInfoTable),"asmMkInfo");
1661 /* Note: the evaluator automatically pads objects with the right number
1662 * of non-ptrs to satisfy MIN_NONUPD_SIZE restrictions.
1664 AsmNat nptrs = stg_max(0,MIN_NONUPD_SIZE-ptrs);
1666 /* initialisation code based on INFO_TABLE_CONSTR */
1667 info->layout.payload.ptrs = ptrs;
1668 info->layout.payload.nptrs = nptrs;
1669 info->srt_len = tag;
1670 info->type = CONSTR;
1671 #ifdef USE_MINIINTERPRETER
1672 info->entry = stgCast(StgFunPtr,&Hugs_CONSTR_entry);
1674 #warning asmMkInfo: Need to insert entry code in some cunning way
1676 ASSERT( sizeW_fromITBL(info) >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
1680 /*-------------------------------------------------------------------------*/
1682 #endif /* INTERPRETER */