2 /* --------------------------------------------------------------------------
5 * Copyright (c) 1994-1998.
7 * $RCSfile: Assembler.c,v $
9 * $Date: 1999/03/02 19:52:24 $
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 "Evaluator.h"
53 #include "StgMiscClosures.h"
56 #define INSIDE_ASSEMBLER_C
57 #include "Assembler.h"
58 #undef INSIDE_ASSEMBLER_C
60 /* --------------------------------------------------------------------------
61 * References between BCOs
63 * These are necessary because there can be circular references between
64 * BCOs so we have to keep track of all the references to each object
65 * and fill in all the references once we're done.
67 * ToDo: generalise to allow references between any objects
68 * ------------------------------------------------------------------------*/
71 AsmObject ref; /* who refers to it */
72 AsmNat i; /* index into some table held by referer */
75 /* --------------------------------------------------------------------------
76 * Queues (of instructions, ptrs, nonptrs)
77 * ------------------------------------------------------------------------*/
79 /* ToDo: while debugging, we use a chunk size of 1 to stress-test the code
80 * this should be fine-tuned using statistics on common sizes
83 #define InstrsChunkSize 40
84 #define PtrsChunkSize 10
85 #define RefsChunkSize 10
86 #define NonPtrsChunkSize 10
90 #include "QueueTemplate.h"
95 #define Type AsmObject
96 #include "QueueTemplate.h"
102 #include "QueueTemplate.h"
106 #define Queue NonPtrs
108 #include "QueueTemplate.h"
112 /* --------------------------------------------------------------------------
113 * AsmObjects are used to build heap objects.
115 * AsmObjects can contain circular references to each other
116 * so we have to keep track of all the references which can't be filled
119 * When we finish building an AsmObject, we allocate an actual heap object and
120 * fill in all the references to the asmObject with pointers to the heap object.
122 * We obtain a limited form of polymorphism through inheritance by putting
123 * the AsmObject first in every structure (as in C++ implementations).
124 * We use the closure type of the allocated object to figure out
125 * where the payload lives in the closure.
126 * ------------------------------------------------------------------------*/
127 /* ToDo: clean up terminology: is Closure right or should it be object or ... */
132 AsmNat num_unresolved; /* number of unfilled references */
133 StgClosure* closure; /* where object was allocated */
137 struct AsmObject_ object; /* must be first in struct */
143 struct AsmObject_ object; /* must be first in struct */
147 struct AsmObject_ object; /* must be first in struct */
149 int /*StgExpr*/ stgexpr;
153 /* abstract machine ("executed" during compilation) */
154 AsmSp sp; /* stack ptr */
156 StgWord hp; /* heap ptr */
160 static void asmResolveRef( AsmObject obj, AsmNat i, AsmClosure reference )
162 ASSERT(obj->closure);
163 switch (get_itbl(obj->closure)->type) {
166 StgBCO* bco = stgCast(StgBCO*,obj->closure);
167 ASSERT(i < bco->n_ptrs && bcoConstPtr(bco,i) == NULL);
168 bcoConstCPtr(bco,i) = reference;
173 StgCAF* caf = stgCast(StgCAF*,obj->closure);
174 ASSERT(i == 0 && caf->body == NULL);
175 caf->body = reference;
180 StgClosure* con = stgCast(StgClosure*,obj->closure);
181 ASSERT(i < get_itbl(con)->layout.payload.nptrs && payloadCPtr(con,i) == NULL);
182 payloadCPtr(con,i) = reference;
187 StgAP_UPD* ap = stgCast(StgAP_UPD*,obj->closure);
188 ASSERT(i < 1+ap->n_args);
190 ASSERT(ap->fun == NULL);
193 ASSERT(payloadCPtr(ap,i-1) == NULL);
194 payloadCPtr(ap,i-1) = reference;
199 barf("asmResolveRef");
201 obj->num_unresolved -= 1;
203 if (obj->num_unresolved == 0) {
204 /* todo: free the queues */
206 /* we don't print until all ptrs are resolved */
207 IF_DEBUG(codegen,printObj(obj->closure);printf("\n\n"));
211 static void asmAddRef( AsmObject referent, AsmObject referer, AsmNat i )
213 if (referent->closure) {
214 asmResolveRef(referer,i,(AsmClosure)referent->closure);
216 insertRefs(&(referent->refs),(AsmRef){referer,i});
220 void asmAddPtr( AsmObject obj, AsmObject arg )
222 ASSERT(obj->closure == 0); /* can't extend an object once it's allocated */
223 insertPtrs( &obj->ptrs, arg );
226 static void asmBeginObject( AsmObject obj )
229 obj->num_unresolved = 0;
230 initRefs(&obj->refs);
231 initPtrs(&obj->ptrs);
234 static void asmEndObject( AsmObject obj, StgClosure* c )
236 obj->num_unresolved = obj->ptrs.len;
238 mapQueue(Ptrs, AsmObject, obj->ptrs, asmAddRef(x,obj,i));
239 mapQueue(Refs, AsmRef, obj->refs, asmResolveRef(x.ref,x.i,c));
241 if (obj->num_unresolved == 0) {
242 /* todo: free the queues */
243 /* we don't print until all ptrs are resolved */
245 if (obj->num_unresolved > 0)
246 fprintf(stderr, "{{%d unresolved}} ", obj->num_unresolved);
248 IF_DEBUG(codegen,printObj(obj->closure));
250 //printf( "unresolved %d\n", obj->num_unresolved);
251 //printObj(obj->closure);
255 int asmObjectHasClosure ( AsmObject obj )
257 return (obj->num_unresolved == 0 && obj->closure);
260 AsmClosure asmClosureOfObject ( AsmObject obj )
262 ASSERT(asmObjectHasClosure(obj));
266 void asmMarkObject ( AsmObject obj )
268 ASSERT(obj->num_unresolved == 0 && obj->closure);
269 obj->closure = MarkRoot(obj->closure);
272 /* --------------------------------------------------------------------------
274 * ------------------------------------------------------------------------*/
276 static StgClosure* asmAlloc( nat size )
278 StgClosure* o = stgCast(StgClosure*,allocate(size));
279 ASSERT( size >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
280 /* printf("Allocated %p .. %p\n", o, o+size-1); */
284 static void grabHpUpd( AsmBCO bco, nat size )
286 /* ToDo: sometimes we should test for MIN_UPD_SIZE instead */
287 ASSERT( size >= MIN_UPD_SIZE + sizeofW(StgHeader) );
291 static void grabHpNonUpd( AsmBCO bco, nat size )
293 /* ToDo: sometimes we should test for MIN_UPD_SIZE instead */
294 ASSERT( size >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
298 static void resetHp( AsmBCO bco, nat hp )
300 bco->max_hp = stg_max(bco->hp,bco->max_hp);
304 static void resetSp( AsmBCO bco, AsmSp sp )
306 bco->max_sp = stg_max(bco->sp,bco->max_sp);
310 /* --------------------------------------------------------------------------
312 * ------------------------------------------------------------------------*/
314 AsmObject asmMkObject( AsmClosure c )
316 AsmObject obj = malloc(sizeof(struct AsmObject_));
318 barf("Can't allocate AsmObject");
325 AsmCon asmBeginCon( AsmInfo info )
327 AsmCon con = malloc(sizeof(struct AsmCon_));
329 barf("Can't allocate AsmCon");
331 asmBeginObject(&con->object);
336 void asmEndCon( AsmCon con )
338 nat p = con->object.ptrs.len;
339 nat np = stg_max(0,MIN_NONUPD_SIZE-p);
341 StgClosure* c = asmAlloc(CONSTR_sizeW(p,np));
342 StgClosure* o = stgCast(StgClosure*,c);
343 SET_HDR(o,con->info,??);
344 mapQueue(Ptrs, AsmObject, con->object.ptrs, payloadCPtr(o,i) = NULL);
345 { nat i; for( i=0; i<np; ++i ) { payloadWord(o,p+i) = 0xdeadbeef; } }
346 asmEndObject(&con->object,c);
349 AsmCAF asmBeginCAF( void )
351 AsmCAF caf = malloc(sizeof(struct AsmCAF_));
353 barf("Can't allocate AsmCAF");
355 asmBeginObject(&caf->object);
359 void asmEndCAF( AsmCAF caf, AsmBCO body )
361 StgClosure* c = asmAlloc(CAF_sizeW());
362 StgCAF* o = stgCast(StgCAF*,c);
363 SET_HDR(o,&CAF_UNENTERED_info,??);
365 o->value = stgCast(StgClosure*,0xdeadbeef);
366 o->link = stgCast(StgCAF*,0xdeadbeef);
367 asmAddPtr(&caf->object,&body->object);
368 asmEndObject(&caf->object,c);
371 AsmBCO asmBeginBCO( int /*StgExpr*/ e )
373 AsmBCO bco = malloc(sizeof(struct AsmBCO_));
375 barf("Can't allocate AsmBCO");
377 asmBeginObject(&bco->object);
378 initInstrs(&bco->is);
379 initNonPtrs(&bco->nps);
382 bco->max_sp = bco->sp = 0;
383 bco->max_hp = bco->hp = 0;
387 void asmEndBCO( AsmBCO bco )
389 nat p = bco->object.ptrs.len;
390 nat np = bco->nps.len;
392 nat is = bco->is.len + 4; /* 4 for stack and heap checks */
394 nat is = bco->is.len + 2; /* 4 for stack check */
397 StgClosure* c = asmAlloc(BCO_sizeW(p,np,is));
398 StgBCO* o = stgCast(StgBCO*,c);
399 SET_HDR(o,&BCO_info,??);
403 o->stgexpr = bco->stgexpr;
404 mapQueue(Ptrs, AsmObject, bco->object.ptrs, bcoConstCPtr(o,i) = NULL);
405 mapQueue(NonPtrs, StgWord, bco->nps, bcoConstWord(o,i) = x);
408 bco->max_sp = stg_max(bco->sp,bco->max_sp);
409 bco->max_hp = stg_max(bco->hp,bco->max_hp);
410 bcoInstr(o,j++) = i_STK_CHECK;
411 bcoInstr(o,j++) = bco->max_sp;
413 bcoInstr(o,j++) = i_HP_CHECK;
414 bcoInstr(o,j++) = bco->max_hp;
416 mapQueue(Instrs, StgWord8, bco->is, bcoInstr(o,j++) = x);
419 asmEndObject(&bco->object,c);
422 /* --------------------------------------------------------------------------
424 * ------------------------------------------------------------------------*/
426 static void asmInstr( AsmBCO bco, StgWord i )
428 ASSERT(i < 256); /* must be a byte */
429 insertInstrs(&(bco->is),i);
432 static void asmPtr( AsmBCO bco, AsmObject x )
434 insertPtrs( &bco->object.ptrs, x );
437 static void asmWord( AsmBCO bco, StgWord i )
439 insertNonPtrs( &bco->nps, i );
442 #define asmWords(bco,ty,x) \
444 union { ty a; AsmWord b[sizeofW(ty)]; } p; \
446 if (sizeof(ty) < sizeof(AsmWord)) p.b[0]=0; \
448 for( i = 0; i < sizeofW(ty); i++ ) { \
449 asmWord(bco,p.b[i]); \
453 static StgWord repSizeW( AsmRep rep )
456 case CHAR_REP: return sizeofW(StgWord) + sizeofW(StgChar);
459 case INT_REP: return sizeofW(StgWord) + sizeofW(StgInt);
461 case INT64_REP: return sizeofW(StgWord) + sizeofW(StgInt64);
464 case WORD_REP: return sizeofW(StgWord) + sizeofW(StgWord);
467 case ADDR_REP: return sizeofW(StgWord) + sizeofW(StgAddr);
469 case FLOAT_REP: return sizeofW(StgWord) + sizeofW(StgFloat);
470 case DOUBLE_REP: return sizeofW(StgWord) + sizeofW(StgDouble);
471 #ifdef PROVIDE_STABLE
472 case STABLE_REP: return sizeofW(StgWord) + sizeofW(StgWord);
475 #ifdef PROVIDE_INTEGER
481 #ifdef PROVIDE_FOREIGN
484 case ALPHA_REP: /* a */
485 case BETA_REP: /* b */
486 case GAMMA_REP: /* c */
487 case HANDLER_REP: /* IOError -> IO a */
488 case ERROR_REP: /* IOError */
490 case ARR_REP : /* PrimArray a */
491 case BARR_REP : /* PrimByteArray a */
492 case REF_REP : /* Ref s a */
493 case MUTARR_REP : /* PrimMutableArray s a */
494 case MUTBARR_REP: /* PrimMutableByteArray s a */
496 #ifdef PROVIDE_CONCURRENT
497 case THREADID_REP: /* ThreadId */
498 case MVAR_REP: /* MVar a */
500 case PTR_REP: return sizeofW(StgPtr);
502 case VOID_REP: return sizeofW(StgWord);
503 default: barf("repSizeW %d",rep);
507 /* --------------------------------------------------------------------------
509 * ------------------------------------------------------------------------*/
511 AsmSp asmBeginArgCheck ( AsmBCO bco )
513 ASSERT(bco->sp == 0);
517 void asmEndArgCheck ( AsmBCO bco, AsmSp last_arg )
519 nat args = bco->sp - last_arg;
520 if (args != 0) { /* optimisation */
521 asmInstr(bco,i_ARG_CHECK);
523 grabHpNonUpd(bco,PAP_sizeW(args-1));
528 /* --------------------------------------------------------------------------
529 * Creating and using "variables"
530 * ------------------------------------------------------------------------*/
532 AsmVar asmBind ( AsmBCO bco, AsmRep rep )
534 bco->sp += repSizeW(rep);
538 void asmVar ( AsmBCO bco, AsmVar v, AsmRep rep )
543 asmInstr(bco,i_VAR_INT);
547 asmInstr(bco,i_VAR_INT64);
552 asmInstr(bco,i_VAR_WORD);
557 asmInstr(bco,i_VAR_ADDR);
561 asmInstr(bco,i_VAR_CHAR);
564 asmInstr(bco,i_VAR_FLOAT);
567 asmInstr(bco,i_VAR_DOUBLE);
569 #ifdef PROVIDE_STABLE
571 asmInstr(bco,i_VAR_STABLE);
575 #ifdef PROVIDE_INTEGER
581 #ifdef PROVIDE_FOREIGN
584 case ALPHA_REP: /* a */
585 case BETA_REP: /* b */
586 case GAMMA_REP: /* c */
587 case HANDLER_REP: /* IOError -> IO a */
588 case ERROR_REP: /* IOError */
590 case ARR_REP : /* PrimArray a */
591 case BARR_REP : /* PrimByteArray a */
592 case REF_REP : /* Ref s a */
593 case MUTARR_REP : /* PrimMutableArray s a */
594 case MUTBARR_REP: /* PrimMutableByteArray s a */
596 #ifdef PROVIDE_CONCURRENT
597 case THREADID_REP: /* ThreadId */
598 case MVAR_REP: /* MVar a */
605 asmInstr(bco,i_VOID);
606 bco->sp += repSizeW(rep);
607 return; /* NB we don't break! */
609 barf("asmVar %d",rep);
611 asmInstr(bco,bco->sp - v);
612 bco->sp += repSizeW(rep);
615 /* --------------------------------------------------------------------------
617 * ------------------------------------------------------------------------*/
619 AsmSp asmBeginEnter( AsmBCO bco )
624 void asmEndEnter( AsmBCO bco, AsmSp sp1, AsmSp sp2 )
626 int x = bco->sp - sp1;
628 ASSERT(x >= 0 && y >= 0);
630 asmInstr(bco,i_SLIDE);
633 bco->sp -= sp1 - sp2;
635 asmInstr(bco,i_ENTER);
638 /* --------------------------------------------------------------------------
639 * Build boxed Ints, Floats, etc
640 * ------------------------------------------------------------------------*/
642 AsmVar asmBox( AsmBCO bco, AsmRep rep )
646 asmInstr(bco,i_PACK_CHAR);
647 grabHpNonUpd(bco,Czh_sizeW);
650 asmInstr(bco,i_PACK_INT);
651 grabHpNonUpd(bco,Izh_sizeW);
655 asmInstr(bco,i_PACK_INT64);
656 grabHpNonUpd(bco,I64zh_sizeW);
661 asmInstr(bco,i_PACK_WORD);
662 grabHpNonUpd(bco,Wzh_sizeW);
667 asmInstr(bco,i_PACK_ADDR);
668 grabHpNonUpd(bco,Azh_sizeW);
672 asmInstr(bco,i_PACK_FLOAT);
673 grabHpNonUpd(bco,Fzh_sizeW);
676 asmInstr(bco,i_PACK_DOUBLE);
677 grabHpNonUpd(bco,Dzh_sizeW);
679 #ifdef PROVIDE_STABLE
681 asmInstr(bco,i_PACK_STABLE);
682 grabHpNonUpd(bco,Stablezh_sizeW);
687 barf("asmBox %d",rep);
689 /* NB: these operations DO pop their arg */
690 bco->sp -= repSizeW(rep); /* pop unboxed arg */
691 bco->sp += sizeofW(StgPtr); /* push box */
695 /* --------------------------------------------------------------------------
696 * Unbox Ints, Floats, etc
697 * ------------------------------------------------------------------------*/
699 AsmVar asmUnbox( AsmBCO bco, AsmRep rep )
703 asmInstr(bco,i_UNPACK_INT);
707 asmInstr(bco,i_UNPACK_INT64);
712 asmInstr(bco,i_UNPACK_WORD);
717 asmInstr(bco,i_UNPACK_ADDR);
721 asmInstr(bco,i_UNPACK_CHAR);
724 asmInstr(bco,i_UNPACK_FLOAT);
727 asmInstr(bco,i_UNPACK_DOUBLE);
729 #ifdef PROVIDE_STABLE
731 asmInstr(bco,i_UNPACK_STABLE);
735 barf("asmUnbox %d",rep);
737 /* NB: these operations DO NOT pop their arg */
738 bco->sp += repSizeW(rep); /* push unboxed arg */
742 /* --------------------------------------------------------------------------
743 * Return unboxed Ints, Floats, etc
744 * ------------------------------------------------------------------------*/
746 void asmReturnUnboxed( AsmBCO bco, AsmRep rep )
750 asmInstr(bco,i_RETURN_CHAR);
753 asmInstr(bco,i_RETURN_INT);
757 asmInstr(bco,i_RETURN_INT64);
762 asmInstr(bco,i_RETURN_WORD);
767 asmInstr(bco,i_RETURN_ADDR);
771 asmInstr(bco,i_RETURN_FLOAT);
774 asmInstr(bco,i_RETURN_DOUBLE);
776 #ifdef PROVIDE_STABLE
778 asmInstr(bco,i_RETURN_STABLE);
781 #ifdef PROVIDE_INTEGER
787 #ifdef PROVIDE_FOREIGN
791 case ARR_REP : /* PrimArray a */
792 case BARR_REP : /* PrimByteArray a */
793 case REF_REP : /* Ref s a */
794 case MUTARR_REP : /* PrimMutableArray s a */
795 case MUTBARR_REP: /* PrimMutableByteArray s a */
797 #ifdef PROVIDE_CONCURRENT
798 case THREADID_REP: /* ThreadId */
799 case MVAR_REP: /* MVar a */
801 asmInstr(bco,i_RETURN_GENERIC);
804 barf("asmReturnUnboxed %d",rep);
808 /* --------------------------------------------------------------------------
809 * Push unboxed Ints, Floats, etc
810 * ------------------------------------------------------------------------*/
812 void asmConstInt( AsmBCO bco, AsmInt x )
814 asmInstr(bco,i_CONST_INT);
815 asmInstr(bco,bco->nps.len);
816 asmWords(bco,AsmInt,x);
817 bco->sp += repSizeW(INT_REP);
821 void asmConstInt64( AsmBCO bco, AsmInt64 x )
823 asmInstr(bco,i_CONST_INT64);
824 asmInstr(bco,bco->nps.len);
825 asmWords(bco,AsmInt64,x);
826 bco->sp += repSizeW(INT64_REP);
830 #ifdef PROVIDE_INTEGER
831 void asmConstInteger( AsmBCO bco, AsmString x )
833 asmInstr(bco,i_CONST_INTEGER);
834 asmInstr(bco,bco->nps.len);
835 asmWords(bco,AsmString,x);
836 bco->sp += repSizeW(INTEGER_REP);
841 void asmConstAddr( AsmBCO bco, AsmAddr x )
843 asmInstr(bco,i_CONST_ADDR);
844 asmInstr(bco,bco->nps.len);
845 asmWords(bco,AsmAddr,x);
846 bco->sp += repSizeW(ADDR_REP);
851 void asmConstWord( AsmBCO bco, AsmWord x )
853 asmInstr(bco,i_CONST_INT);
854 asmInstr(bco,bco->nps.len);
855 asmWords(bco,AsmWord,x);
856 bco->sp += repSizeW(WORD_REP);
860 void asmConstChar( AsmBCO bco, AsmChar x )
862 asmInstr(bco,i_CONST_CHAR);
863 asmInstr(bco,bco->nps.len);
864 asmWords(bco,AsmChar,x);
865 bco->sp += repSizeW(CHAR_REP);
868 void asmConstFloat( AsmBCO bco, AsmFloat x )
870 asmInstr(bco,i_CONST_FLOAT);
871 asmInstr(bco,bco->nps.len);
872 asmWords(bco,AsmFloat,x);
873 bco->sp += repSizeW(FLOAT_REP);
876 void asmConstDouble( AsmBCO bco, AsmDouble x )
878 asmInstr(bco,i_CONST_DOUBLE);
879 asmInstr(bco,bco->nps.len);
880 asmWords(bco,AsmDouble,x);
881 bco->sp += repSizeW(DOUBLE_REP);
884 /* --------------------------------------------------------------------------
886 * ------------------------------------------------------------------------*/
888 /* a mildly bogus pair of functions... */
889 AsmSp asmBeginCase( AsmBCO bco )
894 void asmEndCase( AsmBCO bco )
898 AsmSp asmContinuation( AsmBCO bco, AsmBCO ret_addr )
900 asmInstr(bco,i_RETADDR);
901 asmInstr(bco,bco->object.ptrs.len);
902 asmPtr(bco,&(ret_addr->object));
903 bco->sp += 2 * sizeofW(StgPtr);
907 AsmBCO asmBeginContinuation ( AsmSp sp, int /*List*/ alts )
909 AsmBCO bco = asmBeginBCO(alts);
914 void asmEndContinuation ( AsmBCO bco )
920 /* --------------------------------------------------------------------------
922 * ------------------------------------------------------------------------*/
924 AsmSp asmBeginAlt( AsmBCO bco )
929 void asmEndAlt( AsmBCO bco, AsmSp sp )
932 /* This warning is now redundant since we no longer use the hp/max_hp
933 * information calculated by the assembler
935 #warning ToDo: adjust hp/max_hp in asmEndAlt
940 AsmPc asmTest( AsmBCO bco, AsmWord tag )
942 asmInstr(bco,i_TEST);
948 AsmPc asmTestInt( AsmBCO bco, AsmVar v, AsmInt x )
950 asmVar(bco,v,INT_REP);
952 asmInstr(bco,i_TEST_INT);
954 bco->sp -= 2*repSizeW(INT_REP);
958 void asmFixBranch( AsmBCO bco, AsmPc from )
960 int distance = bco->is.len - from;
961 ASSERT(distance >= 0);
962 setInstrs(&(bco->is),from-1,distance);
965 void asmPanic( AsmBCO bco )
967 asmInstr(bco,i_PANIC); /* "irrefutable" pattern failed - oops! */
970 /* --------------------------------------------------------------------------
972 * ------------------------------------------------------------------------*/
974 AsmSp asmBeginPrim( AsmBCO bco )
979 void asmEndPrim( AsmBCO bco, const AsmPrim* prim, AsmSp base )
981 asmInstr(bco,prim->prefix);
982 asmInstr(bco,prim->opcode);
986 /* Hugs used to let you add arbitrary primops with arbitrary types
987 * just by editing Prelude.hs or any other file you wanted.
988 * We deliberately avoided that approach because we wanted more
989 * control over which primops are provided.
991 const AsmPrim asmPrimOps[] = {
993 /* Char# operations */
994 { "primGtChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_gtChar }
995 , { "primGeChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_geChar }
996 , { "primEqChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_eqChar }
997 , { "primNeChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_neChar }
998 , { "primLtChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_ltChar }
999 , { "primLeChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_leChar }
1000 , { "primCharToInt", "C", "I", MONAD_Id, i_PRIMOP1, i_charToInt }
1001 , { "primIntToChar", "I", "C", MONAD_Id, i_PRIMOP1, i_intToChar }
1003 /* Int# operations */
1004 , { "primGtInt", "II", "B", MONAD_Id, i_PRIMOP1, i_gtInt }
1005 , { "primGeInt", "II", "B", MONAD_Id, i_PRIMOP1, i_geInt }
1006 , { "primEqInt", "II", "B", MONAD_Id, i_PRIMOP1, i_eqInt }
1007 , { "primNeInt", "II", "B", MONAD_Id, i_PRIMOP1, i_neInt }
1008 , { "primLtInt", "II", "B", MONAD_Id, i_PRIMOP1, i_ltInt }
1009 , { "primLeInt", "II", "B", MONAD_Id, i_PRIMOP1, i_leInt }
1010 , { "primMinInt", "", "I", MONAD_Id, i_PRIMOP1, i_minInt }
1011 , { "primMaxInt", "", "I", MONAD_Id, i_PRIMOP1, i_maxInt }
1012 , { "primPlusInt", "II", "I", MONAD_Id, i_PRIMOP1, i_plusInt }
1013 , { "primMinusInt", "II", "I", MONAD_Id, i_PRIMOP1, i_minusInt }
1014 , { "primTimesInt", "II", "I", MONAD_Id, i_PRIMOP1, i_timesInt }
1015 , { "primQuotInt", "II", "I", MONAD_Id, i_PRIMOP1, i_quotInt }
1016 , { "primRemInt", "II", "I", MONAD_Id, i_PRIMOP1, i_remInt }
1017 , { "primQuotRemInt", "II", "II", MONAD_Id, i_PRIMOP1, i_quotRemInt }
1018 , { "primNegateInt", "I", "I", MONAD_Id, i_PRIMOP1, i_negateInt }
1020 , { "primAndInt", "II", "I", MONAD_Id, i_PRIMOP1, i_andInt }
1021 , { "primOrInt", "II", "I", MONAD_Id, i_PRIMOP1, i_orInt }
1022 , { "primXorInt", "II", "I", MONAD_Id, i_PRIMOP1, i_xorInt }
1023 , { "primNotInt", "I", "I", MONAD_Id, i_PRIMOP1, i_notInt }
1024 , { "primShiftLInt", "II", "I", MONAD_Id, i_PRIMOP1, i_shiftLInt }
1025 , { "primShiftRAInt", "II", "I", MONAD_Id, i_PRIMOP1, i_shiftRAInt }
1026 , { "primShiftRLInt", "II", "I", MONAD_Id, i_PRIMOP1, i_shiftRLInt }
1028 #ifdef PROVIDE_INT64
1029 /* Int64# operations */
1030 , { "primGtInt64", "zz", "B", MONAD_Id, i_PRIMOP1, i_gtInt64 }
1031 , { "primGeInt64", "zz", "B", MONAD_Id, i_PRIMOP1, i_geInt64 }
1032 , { "primEqInt64", "zz", "B", MONAD_Id, i_PRIMOP1, i_eqInt64 }
1033 , { "primNeInt64", "zz", "B", MONAD_Id, i_PRIMOP1, i_neInt64 }
1034 , { "primLtInt64", "zz", "B", MONAD_Id, i_PRIMOP1, i_ltInt64 }
1035 , { "primLeInt64", "zz", "B", MONAD_Id, i_PRIMOP1, i_leInt64 }
1036 , { "primMinInt64", "", "z", MONAD_Id, i_PRIMOP1, i_minInt64 }
1037 , { "primMaxInt64", "", "z", MONAD_Id, i_PRIMOP1, i_maxInt64 }
1038 , { "primPlusInt64", "zz", "z", MONAD_Id, i_PRIMOP1, i_plusInt64 }
1039 , { "primMinusInt64", "zz", "z", MONAD_Id, i_PRIMOP1, i_minusInt64 }
1040 , { "primTimesInt64", "zz", "z", MONAD_Id, i_PRIMOP1, i_timesInt64 }
1041 , { "primQuotInt64", "zz", "z", MONAD_Id, i_PRIMOP1, i_quotInt64 }
1042 , { "primRemInt64", "zz", "z", MONAD_Id, i_PRIMOP1, i_remInt64 }
1043 , { "primQuotRemInt64", "zz", "zz", MONAD_Id, i_PRIMOP1, i_quotRemInt64 }
1044 , { "primNegateInt64", "z", "z", MONAD_Id, i_PRIMOP1, i_negateInt64 }
1046 , { "primAndInt64", "zz", "z", MONAD_Id, i_PRIMOP1, i_andInt64 }
1047 , { "primOrInt64", "zz", "z", MONAD_Id, i_PRIMOP1, i_orInt64 }
1048 , { "primXorInt64", "zz", "z", MONAD_Id, i_PRIMOP1, i_xorInt64 }
1049 , { "primNotInt64", "z", "z", MONAD_Id, i_PRIMOP1, i_notInt64 }
1050 , { "primShiftLInt64", "zW", "z", MONAD_Id, i_PRIMOP1, i_shiftLInt64 }
1051 , { "primShiftRAInt64", "zW", "z", MONAD_Id, i_PRIMOP1, i_shiftRAInt64 }
1052 , { "primShiftRLInt64", "zW", "z", MONAD_Id, i_PRIMOP1, i_shiftRLInt64 }
1054 , { "primInt64ToInt", "z", "I", MONAD_Id, i_PRIMOP1, i_int64ToInt }
1055 , { "primIntToInt64", "I", "z", MONAD_Id, i_PRIMOP1, i_intToInt64 }
1057 , { "primInt64ToWord", "z", "W", MONAD_Id, i_PRIMOP1, i_int64ToWord }
1058 , { "primWordToInt64", "W", "z", MONAD_Id, i_PRIMOP1, i_wordToInt64 }
1060 , { "primInt64ToFloat", "z", "F", MONAD_Id, i_PRIMOP1, i_int64ToFloat }
1061 , { "primFloatToInt64", "F", "z", MONAD_Id, i_PRIMOP1, i_floatToInt64 }
1062 , { "primInt64ToDouble", "z", "D", MONAD_Id, i_PRIMOP1, i_int64ToDouble }
1063 , { "primDoubleToInt64", "D", "z", MONAD_Id, i_PRIMOP1, i_doubleToInt64 }
1067 /* Word# operations */
1068 , { "primGtWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_gtWord }
1069 , { "primGeWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_geWord }
1070 , { "primEqWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_eqWord }
1071 , { "primNeWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_neWord }
1072 , { "primLtWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_ltWord }
1073 , { "primLeWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_leWord }
1074 , { "primMinWord", "", "W", MONAD_Id, i_PRIMOP1, i_minWord }
1075 , { "primMaxWord", "", "W", MONAD_Id, i_PRIMOP1, i_maxWord }
1076 , { "primPlusWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_plusWord }
1077 , { "primMinusWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_minusWord }
1078 , { "primTimesWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_timesWord }
1079 , { "primQuotWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_quotWord }
1080 , { "primRemWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_remWord }
1081 , { "primQuotRemWord", "WW", "WW", MONAD_Id, i_PRIMOP1, i_quotRemWord }
1082 , { "primNegateWord", "W", "W", MONAD_Id, i_PRIMOP1, i_negateWord }
1084 , { "primAndWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_andWord }
1085 , { "primOrWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_orWord }
1086 , { "primXorWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_xorWord }
1087 , { "primNotWord", "W", "W", MONAD_Id, i_PRIMOP1, i_notWord }
1088 , { "primShiftLWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_shiftLWord }
1089 , { "primShiftRAWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_shiftRAWord }
1090 , { "primShiftRLWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_shiftRLWord }
1092 , { "primIntToWord", "I", "W", MONAD_Id, i_PRIMOP1, i_intToWord }
1093 , { "primWordToInt", "W", "I", MONAD_Id, i_PRIMOP1, i_wordToInt }
1097 /* Addr# operations */
1098 , { "primGtAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_gtAddr }
1099 , { "primGeAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_geAddr }
1100 , { "primEqAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_eqAddr }
1101 , { "primNeAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_neAddr }
1102 , { "primLtAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_ltAddr }
1103 , { "primLeAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_leAddr }
1104 , { "primIntToAddr", "I", "A", MONAD_Id, i_PRIMOP1, i_intToAddr }
1105 , { "primAddrToInt", "A", "I", MONAD_Id, i_PRIMOP1, i_addrToInt }
1107 , { "primIndexCharOffAddr", "AI", "C", MONAD_Id, i_PRIMOP1, i_indexCharOffAddr }
1108 , { "primIndexIntOffAddr", "AI", "I", MONAD_Id, i_PRIMOP1, i_indexIntOffAddr }
1109 #ifdef PROVIDE_INT64
1110 , { "primIndexInt64OffAddr", "AI", "z", MONAD_Id, i_PRIMOP1, i_indexInt64OffAddr }
1113 , { "primIndexWordOffAddr", "AI", "W", MONAD_Id, i_PRIMOP1, i_indexWordOffAddr }
1115 , { "primIndexAddrOffAddr", "AI", "A", MONAD_Id, i_PRIMOP1, i_indexAddrOffAddr }
1116 , { "primIndexFloatOffAddr", "AI", "F", MONAD_Id, i_PRIMOP1, i_indexFloatOffAddr }
1117 , { "primIndexDoubleOffAddr", "AI", "D", MONAD_Id, i_PRIMOP1, i_indexDoubleOffAddr }
1118 #ifdef PROVIDE_STABLE
1119 , { "primIndexStableOffAddr", "AI", "s", MONAD_Id, i_PRIMOP1, i_indexStableOffAddr }
1122 /* These ops really ought to be in the IO monad */
1123 , { "primReadCharOffAddr", "AI", "C", MONAD_ST, i_PRIMOP1, i_readCharOffAddr }
1124 , { "primReadIntOffAddr", "AI", "I", MONAD_ST, i_PRIMOP1, i_readIntOffAddr }
1125 #ifdef PROVIDE_INT64
1126 , { "primReadInt64OffAddr", "AI", "z", MONAD_ST, i_PRIMOP1, i_readInt64OffAddr }
1129 , { "primReadWordOffAddr", "AI", "W", MONAD_ST, i_PRIMOP1, i_readWordOffAddr }
1131 , { "primReadAddrOffAddr", "AI", "A", MONAD_ST, i_PRIMOP1, i_readAddrOffAddr }
1132 , { "primReadFloatOffAddr", "AI", "F", MONAD_ST, i_PRIMOP1, i_readFloatOffAddr }
1133 , { "primReadDoubleOffAddr", "AI", "D", MONAD_ST, i_PRIMOP1, i_readDoubleOffAddr }
1134 #ifdef PROVIDE_STABLE
1135 , { "primReadStableOffAddr", "AI", "s", MONAD_ST, i_PRIMOP1, i_readStableOffAddr }
1138 /* These ops really ought to be in the IO monad */
1139 , { "primWriteCharOffAddr", "AIC", "", MONAD_ST, i_PRIMOP1, i_writeCharOffAddr }
1140 , { "primWriteIntOffAddr", "AII", "", MONAD_ST, i_PRIMOP1, i_writeIntOffAddr }
1141 #ifdef PROVIDE_INT64
1142 , { "primWriteInt64OffAddr", "AIz", "", MONAD_ST, i_PRIMOP1, i_writeInt64OffAddr }
1145 , { "primWriteWordOffAddr", "AIW", "", MONAD_ST, i_PRIMOP1, i_writeWordOffAddr }
1147 , { "primWriteAddrOffAddr", "AIA", "", MONAD_ST, i_PRIMOP1, i_writeAddrOffAddr }
1148 , { "primWriteFloatOffAddr", "AIF", "", MONAD_ST, i_PRIMOP1, i_writeFloatOffAddr }
1149 , { "primWriteDoubleOffAddr", "AID", "", MONAD_ST, i_PRIMOP1, i_writeDoubleOffAddr }
1150 #ifdef PROVIDE_STABLE
1151 , { "primWriteStableOffAddr", "AIs", "", MONAD_ST, i_PRIMOP1, i_writeStableOffAddr }
1154 #endif /* PROVIDE_ADDR */
1156 #ifdef PROVIDE_INTEGER
1157 /* Integer operations */
1158 , { "primCompareInteger", "ZZ", "I", MONAD_Id, i_PRIMOP1, i_compareInteger }
1159 , { "primNegateInteger", "Z", "Z", MONAD_Id, i_PRIMOP1, i_negateInteger }
1160 , { "primPlusInteger", "ZZ", "Z", MONAD_Id, i_PRIMOP1, i_plusInteger }
1161 , { "primMinusInteger", "ZZ", "Z", MONAD_Id, i_PRIMOP1, i_minusInteger }
1162 , { "primTimesInteger", "ZZ", "Z", MONAD_Id, i_PRIMOP1, i_timesInteger }
1163 , { "primQuotRemInteger", "ZZ", "ZZ", MONAD_Id, i_PRIMOP1, i_quotRemInteger }
1164 , { "primDivModInteger", "ZZ", "ZZ", MONAD_Id, i_PRIMOP1, i_divModInteger }
1165 , { "primIntegerToInt", "Z", "I", MONAD_Id, i_PRIMOP1, i_integerToInt }
1166 , { "primIntToInteger", "I", "Z", MONAD_Id, i_PRIMOP1, i_intToInteger }
1167 #ifdef PROVIDE_INT64
1168 , { "primIntegerToInt64", "Z", "z", MONAD_Id, i_PRIMOP1, i_integerToInt64 }
1169 , { "primInt64ToInteger", "z", "Z", MONAD_Id, i_PRIMOP1, i_int64ToInteger }
1172 , { "primIntegerToWord", "Z", "W", MONAD_Id, i_PRIMOP1, i_integerToWord }
1173 , { "primWordToInteger", "W", "Z", MONAD_Id, i_PRIMOP1, i_wordToInteger }
1175 , { "primIntegerToFloat", "Z", "F", MONAD_Id, i_PRIMOP1, i_integerToFloat }
1176 , { "primFloatToInteger", "F", "Z", MONAD_Id, i_PRIMOP1, i_floatToInteger }
1177 , { "primIntegerToDouble", "Z", "D", MONAD_Id, i_PRIMOP1, i_integerToDouble }
1178 , { "primDoubleToInteger", "D", "Z", MONAD_Id, i_PRIMOP1, i_doubleToInteger }
1181 /* Float# operations */
1182 , { "primGtFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_gtFloat }
1183 , { "primGeFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_geFloat }
1184 , { "primEqFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_eqFloat }
1185 , { "primNeFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_neFloat }
1186 , { "primLtFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_ltFloat }
1187 , { "primLeFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_leFloat }
1188 , { "primMinFloat", "", "F", MONAD_Id, i_PRIMOP1, i_minFloat }
1189 , { "primMaxFloat", "", "F", MONAD_Id, i_PRIMOP1, i_maxFloat }
1190 , { "primRadixFloat", "", "I", MONAD_Id, i_PRIMOP1, i_radixFloat }
1191 , { "primDigitsFloat", "", "I", MONAD_Id, i_PRIMOP1, i_digitsFloat }
1192 , { "primMinExpFloat", "", "I", MONAD_Id, i_PRIMOP1, i_minExpFloat }
1193 , { "primMaxExpFloat", "", "I", MONAD_Id, i_PRIMOP1, i_maxExpFloat }
1194 , { "primPlusFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_plusFloat }
1195 , { "primMinusFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_minusFloat }
1196 , { "primTimesFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_timesFloat }
1197 , { "primDivideFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_divideFloat }
1198 , { "primNegateFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_negateFloat }
1199 , { "primFloatToInt", "F", "I", MONAD_Id, i_PRIMOP1, i_floatToInt }
1200 , { "primIntToFloat", "I", "F", MONAD_Id, i_PRIMOP1, i_intToFloat }
1201 , { "primExpFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_expFloat }
1202 , { "primLogFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_logFloat }
1203 , { "primSqrtFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_sqrtFloat }
1204 , { "primSinFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_sinFloat }
1205 , { "primCosFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_cosFloat }
1206 , { "primTanFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_tanFloat }
1207 , { "primAsinFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_asinFloat }
1208 , { "primAcosFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_acosFloat }
1209 , { "primAtanFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_atanFloat }
1210 , { "primSinhFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_sinhFloat }
1211 , { "primCoshFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_coshFloat }
1212 , { "primTanhFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_tanhFloat }
1213 , { "primPowerFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_powerFloat }
1214 #ifdef PROVIDE_INT64
1215 , { "primDecodeFloatz", "F", "zI", MONAD_Id, i_PRIMOP1, i_decodeFloatz }
1216 , { "primEncodeFloatz", "zI", "F", MONAD_Id, i_PRIMOP1, i_encodeFloatz }
1218 #ifdef PROVIDE_INTEGER
1219 , { "primDecodeFloatZ", "F", "ZI", MONAD_Id, i_PRIMOP1, i_decodeFloatZ }
1220 , { "primEncodeFloatZ", "ZI", "F", MONAD_Id, i_PRIMOP1, i_encodeFloatZ }
1222 , { "primIsNaNFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isNaNFloat }
1223 , { "primIsInfiniteFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isInfiniteFloat }
1224 , { "primIsDenormalizedFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isDenormalizedFloat }
1225 , { "primIsNegativeZeroFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isNegativeZeroFloat }
1226 , { "primIsIEEEFloat", "", "B", MONAD_Id, i_PRIMOP1, i_isIEEEFloat }
1228 /* Double# operations */
1229 , { "primGtDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_gtDouble }
1230 , { "primGeDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_geDouble }
1231 , { "primEqDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_eqDouble }
1232 , { "primNeDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_neDouble }
1233 , { "primLtDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_ltDouble }
1234 , { "primLeDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_leDouble }
1235 , { "primMinDouble", "", "D", MONAD_Id, i_PRIMOP1, i_minDouble }
1236 , { "primMaxDouble", "", "D", MONAD_Id, i_PRIMOP1, i_maxDouble }
1237 , { "primRadixDouble", "", "I", MONAD_Id, i_PRIMOP1, i_radixDouble }
1238 , { "primDigitsDouble", "", "I", MONAD_Id, i_PRIMOP1, i_digitsDouble }
1239 , { "primMinExpDouble", "", "I", MONAD_Id, i_PRIMOP1, i_minExpDouble }
1240 , { "primMaxExpDouble", "", "I", MONAD_Id, i_PRIMOP1, i_maxExpDouble }
1241 , { "primPlusDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_plusDouble }
1242 , { "primMinusDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_minusDouble }
1243 , { "primTimesDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_timesDouble }
1244 , { "primDivideDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_divideDouble }
1245 , { "primNegateDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_negateDouble }
1246 , { "primDoubleToInt", "D", "I", MONAD_Id, i_PRIMOP1, i_doubleToInt }
1247 , { "primIntToDouble", "I", "D", MONAD_Id, i_PRIMOP1, i_intToDouble }
1248 , { "primDoubleToFloat", "D", "F", MONAD_Id, i_PRIMOP1, i_doubleToFloat }
1249 , { "primFloatToDouble", "F", "D", MONAD_Id, i_PRIMOP1, i_floatToDouble }
1250 , { "primExpDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_expDouble }
1251 , { "primLogDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_logDouble }
1252 , { "primSqrtDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_sqrtDouble }
1253 , { "primSinDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_sinDouble }
1254 , { "primCosDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_cosDouble }
1255 , { "primTanDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_tanDouble }
1256 , { "primAsinDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_asinDouble }
1257 , { "primAcosDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_acosDouble }
1258 , { "primAtanDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_atanDouble }
1259 , { "primSinhDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_sinhDouble }
1260 , { "primCoshDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_coshDouble }
1261 , { "primTanhDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_tanhDouble }
1262 , { "primPowerDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_powerDouble }
1263 #ifdef PROVIDE_INT64
1264 , { "primDecodeDoublez", "D", "zI", MONAD_Id, i_PRIMOP1, i_decodeDoublez }
1265 , { "primEncodeDoublez", "zI", "D", MONAD_Id, i_PRIMOP1, i_encodeDoublez }
1267 #ifdef PROVIDE_INTEGER
1268 , { "primDecodeDoubleZ", "D", "ZI", MONAD_Id, i_PRIMOP1, i_decodeDoubleZ }
1269 , { "primEncodeDoubleZ", "ZI", "D", MONAD_Id, i_PRIMOP1, i_encodeDoubleZ }
1271 , { "primIsNaNDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isNaNDouble }
1272 , { "primIsInfiniteDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isInfiniteDouble }
1273 , { "primIsDenormalizedDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isDenormalizedDouble }
1274 , { "primIsNegativeZeroDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isNegativeZeroDouble }
1275 , { "primIsIEEEDouble", "", "B", MONAD_Id, i_PRIMOP1, i_isIEEEDouble }
1278 /* Polymorphic force :: a -> (# #) */
1279 /* , { "primForce", "a", "", MONAD_Id, i_PRIMOP2, i_force } */
1281 /* Error operations - not in IO monad! */
1282 //, { "primRaise", "E", "a", MONAD_Id, i_PRIMOP2, i_raise }
1283 //, { "primCatch'", "aH", "a", MONAD_Id, i_PRIMOP2, i_catch }
1285 #ifdef PROVIDE_ARRAY
1286 /* Ref operations */
1287 , { "primNewRef", "a", "R", MONAD_ST, i_PRIMOP2, i_newRef }
1288 , { "primWriteRef", "Ra", "", MONAD_ST, i_PRIMOP2, i_writeRef }
1289 , { "primReadRef", "R", "a", MONAD_ST, i_PRIMOP2, i_readRef }
1290 , { "primSameRef", "RR", "B", MONAD_Id, i_PRIMOP2, i_sameRef }
1292 /* PrimArray operations */
1293 , { "primSameMutableArray", "MM", "B", MONAD_Id, i_PRIMOP2, i_sameMutableArray }
1294 , { "primUnsafeFreezeArray", "M", "X", MONAD_ST, i_PRIMOP2, i_unsafeFreezeArray }
1295 , { "primNewArray", "Ia", "M", MONAD_ST, i_PRIMOP2, i_newArray }
1296 , { "primWriteArray", "MIa", "", MONAD_ST, i_PRIMOP2, i_writeArray }
1297 , { "primReadArray", "MI", "a", MONAD_ST, i_PRIMOP2, i_readArray }
1298 , { "primIndexArray", "XI", "a", MONAD_Id, i_PRIMOP2, i_indexArray }
1299 , { "primSizeArray", "X", "I", MONAD_Id, i_PRIMOP2, i_sizeArray }
1300 , { "primSizeMutableArray", "M", "I", MONAD_Id, i_PRIMOP2, i_sizeMutableArray }
1302 /* Prim[Mutable]ByteArray operations */
1303 , { "primSameMutableByteArray", "mm", "B", MONAD_Id, i_PRIMOP2, i_sameMutableByteArray }
1304 , { "primUnsafeFreezeByteArray", "m", "x", MONAD_ST, i_PRIMOP2, i_unsafeFreezeByteArray }
1306 , { "primNewByteArray", "I", "m", MONAD_ST, i_PRIMOP2, i_newByteArray }
1308 , { "primWriteCharArray", "mIC", "", MONAD_ST, i_PRIMOP2, i_writeCharArray }
1309 , { "primReadCharArray", "mI", "C", MONAD_ST, i_PRIMOP2, i_readCharArray }
1310 , { "primIndexCharArray", "xI", "C", MONAD_Id, i_PRIMOP2, i_indexCharArray }
1312 , { "primWriteIntArray", "mII", "", MONAD_ST, i_PRIMOP2, i_writeIntArray }
1313 , { "primReadIntArray", "mI", "I", MONAD_ST, i_PRIMOP2, i_readIntArray }
1314 , { "primIndexIntArray", "xI", "I", MONAD_Id, i_PRIMOP2, i_indexIntArray }
1316 #ifdef PROVIDE_INT64
1317 , { "primWriteInt64Array", "mIz", "", MONAD_ST, i_PRIMOP2, i_writeInt64Array }
1318 , { "primReadInt64Array", "mI", "z", MONAD_ST, i_PRIMOP2, i_readInt64Array }
1319 , { "primIndexInt64Array", "xI", "z", MONAD_Id, i_PRIMOP2, i_indexInt64Array }
1322 /* {new,write,read,index}IntegerArray not provided */
1325 , { "primWriteWordArray", "mIW", "", MONAD_ST, i_PRIMOP2, i_writeWordArray }
1326 , { "primReadWordArray", "mI", "W", MONAD_ST, i_PRIMOP2, i_readWordArray }
1327 , { "primIndexWordArray", "xI", "W", MONAD_Id, i_PRIMOP2, i_indexWordArray }
1330 , { "primWriteAddrArray", "mIA", "", MONAD_ST, i_PRIMOP2, i_writeAddrArray }
1331 , { "primReadAddrArray", "mI", "A", MONAD_ST, i_PRIMOP2, i_readAddrArray }
1332 , { "primIndexAddrArray", "xI", "A", MONAD_Id, i_PRIMOP2, i_indexAddrArray }
1334 , { "primWriteFloatArray", "mIF", "", MONAD_ST, i_PRIMOP2, i_writeFloatArray }
1335 , { "primReadFloatArray", "mI", "F", MONAD_ST, i_PRIMOP2, i_readFloatArray }
1336 , { "primIndexFloatArray", "xI", "F", MONAD_Id, i_PRIMOP2, i_indexFloatArray }
1338 , { "primWriteDoubleArray" , "mID", "", MONAD_ST, i_PRIMOP2, i_writeDoubleArray }
1339 , { "primReadDoubleArray", "mI", "D", MONAD_ST, i_PRIMOP2, i_readDoubleArray }
1340 , { "primIndexDoubleArray", "xI", "D", MONAD_Id, i_PRIMOP2, i_indexDoubleArray }
1342 #ifdef PROVIDE_STABLE
1343 , { "primWriteStableArray", "mIs", "", MONAD_ST, i_PRIMOP2, i_writeStableArray }
1344 , { "primReadStableArray", "mI", "s", MONAD_ST, i_PRIMOP2, i_readStableArray }
1345 , { "primIndexStableArray", "xI", "s", MONAD_Id, i_PRIMOP2, i_indexStableArray }
1348 /* {new,write,read,index}ForeignObjArray not provided */
1350 #endif PROVIDE_ARRAY
1352 #ifdef PROVIDE_FOREIGN
1353 /* ForeignObj# operations */
1354 , { "primMakeForeignObj", "A", "f", MONAD_IO, i_PRIMOP2, i_makeForeignObj }
1357 /* WeakPair# operations */
1358 , { "primMakeWeak", "bac", "w", MONAD_IO, i_PRIMOP2, i_makeWeak }
1359 , { "primDeRefWeak", "w", "Ia", MONAD_IO, i_PRIMOP2, i_deRefWeak }
1361 #ifdef PROVIDE_STABLE
1362 /* StablePtr# operations */
1363 , { "primMakeStablePtr", "a", "s", MONAD_IO, i_PRIMOP2, i_makeStablePtr }
1364 , { "primDeRefStablePtr", "s", "a", MONAD_IO, i_PRIMOP2, i_deRefStablePtr }
1365 , { "primFreeStablePtr", "s", "", MONAD_IO, i_PRIMOP2, i_freeStablePtr }
1367 #ifdef PROVIDE_PTREQUALITY
1368 , { "primReallyUnsafePtrEquality", "aa", "B",MONAD_Id, i_PRIMOP2, i_reallyUnsafePtrEquality }
1370 #ifdef PROVIDE_COERCE
1371 , { "primUnsafeCoerce", "a", "b", MONAD_Id, i_PRIMOP2, i_unsafeCoerce }
1373 #ifdef PROVIDE_CONCURRENT
1374 /* Concurrency operations */
1375 , { "primFork", "a", "T", MONAD_IO, i_PRIMOP2, i_fork }
1376 , { "primKillThread", "T", "", MONAD_IO, i_PRIMOP2, i_killThread }
1377 , { "primSameMVar", "rr", "B", MONAD_Id, i_PRIMOP2, i_sameMVar }
1378 , { "primNewMVar", "", "r", MONAD_IO, i_PRIMOP2, i_newMVar }
1379 , { "primTakeMVar", "r", "a", MONAD_IO, i_PRIMOP2, i_takeMVar }
1380 , { "primPutMVar", "ra", "", MONAD_IO, i_PRIMOP2, i_putMVar }
1381 , { "primDelay", "I", "", MONAD_IO, i_PRIMOP2, i_delay }
1382 , { "primWaitRead", "I", "", MONAD_IO, i_PRIMOP2, i_waitRead }
1383 , { "primWaitWrite", "I", "", MONAD_IO, i_PRIMOP2, i_waitWrite }
1386 /* Ccall is polyadic - so it's excluded from this table */
1391 const AsmPrim ccall_Id = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_Id };
1392 const AsmPrim ccall_IO = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_IO };
1395 const AsmPrim* asmFindPrim( char* s )
1398 for (i=0; asmPrimOps[i].name; ++i) {
1399 if (strcmp(s,asmPrimOps[i].name)==0) {
1400 return &asmPrimOps[i];
1406 const AsmPrim* asmFindPrimop( AsmInstr prefix, AsmInstr op )
1409 for (i=0; asmPrimOps[i].name; ++i) {
1410 if (asmPrimOps[i].prefix == prefix && asmPrimOps[i].opcode == op) {
1411 return &asmPrimOps[i];
1417 /* --------------------------------------------------------------------------
1418 * Handwritten primops
1419 * ------------------------------------------------------------------------*/
1421 AsmBCO asm_BCO_catch ( void )
1423 AsmBCO bco = asmBeginBCO(0 /*NIL*/);
1424 asmInstr(bco,i_ARG_CHECK); asmInstr(bco,2);
1425 asmInstr(bco,i_PRIMOP1); asmInstr(bco,i_pushcatchframe);
1426 bco->sp += (1-2)*sizeofW(StgPtr) + sizeofW(StgCatchFrame);
1427 asmInstr(bco,i_ENTER);
1432 AsmBCO asm_BCO_raise ( void )
1434 AsmBCO bco = asmBeginBCO(0 /*NIL*/);
1435 asmInstr(bco,i_ARG_CHECK); asmInstr(bco,1);
1436 asmInstr(bco,i_PRIMOP2); asmInstr(bco,i_raise);
1441 AsmBCO asm_BCO_seq ( void )
1445 cont = asmBeginBCO(0 /*NIL*/);
1446 asmInstr(cont,i_ARG_CHECK); asmInstr(cont,2);
1447 asmInstr(cont,i_VAR); asmInstr(cont,1);
1448 asmInstr(cont,i_SLIDE); asmInstr(cont,1); asmInstr(cont,2);
1449 asmInstr(cont,i_ENTER);
1450 cont->sp += 3*sizeofW(StgPtr);
1453 eval = asmBeginBCO(0 /*NIL*/);
1454 asmInstr(eval,i_ARG_CHECK); asmInstr(eval,2);
1455 asmInstr(eval,i_RETADDR);
1456 asmInstr(eval,eval->object.ptrs.len);
1457 asmPtr(eval,&(cont->object));
1458 asmInstr(eval,i_VAR); asmInstr(eval,2);
1459 asmInstr(eval,i_SLIDE); asmInstr(eval,3); asmInstr(eval,1);
1460 asmInstr(eval,i_PRIMOP1); asmInstr(eval,i_pushseqframe);
1461 asmInstr(eval,i_ENTER);
1462 eval->sp += sizeofW(StgSeqFrame) + 4*sizeofW(StgPtr);
1468 /* --------------------------------------------------------------------------
1470 * ------------------------------------------------------------------------*/
1472 AsmVar asmAllocCONSTR ( AsmBCO bco, AsmInfo info )
1474 ASSERT( sizeW_fromITBL(info) >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
1475 asmInstr(bco,i_ALLOC_CONSTR);
1476 asmInstr(bco,bco->nps.len);
1477 asmWords(bco,AsmInfo,info);
1478 bco->sp += sizeofW(StgClosurePtr);
1479 grabHpNonUpd(bco,sizeW_fromITBL(info));
1483 AsmSp asmBeginPack( AsmBCO bco )
1488 void asmEndPack( AsmBCO bco, AsmVar v, AsmSp start, AsmInfo info )
1490 nat size = bco->sp - start;
1491 assert(bco->sp >= start);
1493 /* only reason to include info is for this assertion */
1494 assert(info->layout.payload.ptrs == size);
1495 asmInstr(bco,i_PACK);
1496 asmInstr(bco,bco->sp - v);
1500 void asmBeginUnpack( AsmBCO bco )
1502 /* dummy to make it look prettier */
1505 void asmEndUnpack( AsmBCO bco )
1507 asmInstr(bco,i_UNPACK);
1510 AsmVar asmAllocAP( AsmBCO bco, AsmNat words )
1512 asmInstr(bco,i_ALLOC_AP);
1513 asmInstr(bco,words);
1514 bco->sp += sizeofW(StgPtr);
1515 grabHpUpd(bco,AP_sizeW(words));
1519 AsmSp asmBeginMkAP( AsmBCO bco )
1524 void asmEndMkAP( AsmBCO bco, AsmVar v, AsmSp start )
1526 asmInstr(bco,i_MKAP);
1527 asmInstr(bco,bco->sp-v);
1528 asmInstr(bco,bco->sp-start-1); /* -1 because fun isn't counted */
1532 AsmVar asmAllocPAP( AsmBCO bco, AsmNat size )
1534 asmInstr(bco,i_ALLOC_PAP);
1536 bco->sp += sizeofW(StgPtr);
1540 AsmSp asmBeginMkPAP( AsmBCO bco )
1545 void asmEndMkPAP( AsmBCO bco, AsmVar v, AsmSp start )
1547 asmInstr(bco,i_MKPAP);
1548 asmInstr(bco,bco->sp-v);
1549 asmInstr(bco,bco->sp-start-1); /* -1 because fun isn't counted */
1553 AsmVar asmClosure( AsmBCO bco, AsmObject p )
1555 StgWord o = bco->object.ptrs.len;
1557 asmInstr(bco,i_CONST);
1561 asmInstr(bco,i_CONST2);
1562 asmInstr(bco,o / 256);
1563 asmInstr(bco,o % 256);
1566 bco->sp += sizeofW(StgPtr);
1570 /* --------------------------------------------------------------------------
1571 * Building InfoTables
1572 * ------------------------------------------------------------------------*/
1574 AsmInfo asmMkInfo( AsmNat tag, AsmNat ptrs )
1576 StgInfoTable* info = stgMallocBytes( sizeof(StgInfoTable),"asmMkInfo");
1577 /* Note: the evaluator automatically pads objects with the right number
1578 * of non-ptrs to satisfy MIN_NONUPD_SIZE restrictions.
1580 AsmNat nptrs = stg_max(0,MIN_NONUPD_SIZE-ptrs);
1582 /* initialisation code based on INFO_TABLE_CONSTR */
1583 info->layout.payload.ptrs = ptrs;
1584 info->layout.payload.nptrs = nptrs;
1585 info->srt_len = tag;
1586 info->type = CONSTR;
1587 info->flags = FLAGS_CONSTR;
1588 #ifdef USE_MINIINTERPRETER
1589 info->entry = stgCast(StgFunPtr,&Hugs_CONSTR_entry);
1591 #warning asmMkInfo: Need to insert entry code in some cunning way
1593 ASSERT( sizeW_fromITBL(info) >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
1597 /*-------------------------------------------------------------------------*/
1599 #endif /* INTERPRETER */