2 /* --------------------------------------------------------------------------
5 * Copyright (c) 1994-1998.
7 * $RCSfile: Assembler.c,v $
9 * $Date: 1999/07/06 16:40:22 $
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 * ------------------------------------------------------------------------*/
81 #include "QueueTemplate.h"
86 #define Type AsmObject
87 #include "QueueTemplate.h"
93 #include "QueueTemplate.h"
99 #include "QueueTemplate.h"
103 /* --------------------------------------------------------------------------
104 * AsmObjects are used to build heap objects.
106 * AsmObjects can contain circular references to each other
107 * so we have to keep track of all the references which can't be filled
110 * When we finish building an AsmObject, we allocate an actual heap object and
111 * fill in all the references to the asmObject with pointers to the heap object.
113 * We obtain a limited form of polymorphism through inheritance by putting
114 * the AsmObject first in every structure (as in C++ implementations).
115 * We use the closure type of the allocated object to figure out
116 * where the payload lives in the closure.
117 * ------------------------------------------------------------------------*/
118 /* ToDo: clean up terminology: is Closure right or should it be object or ... */
123 AsmNat num_unresolved; /* number of unfilled references */
124 StgClosure* closure; /* where object was allocated */
128 struct AsmObject_ object; /* must be first in struct */
134 struct AsmObject_ object; /* must be first in struct */
138 struct AsmObject_ object; /* must be first in struct */
143 int /*StgExpr*/ stgexpr;
145 /* abstract machine ("executed" during compilation) */
146 AsmSp sp; /* stack ptr */
148 StgWord hp; /* heap ptr */
153 static void asmResolveRef( AsmObject obj, AsmNat i, AsmClosure reference )
155 ASSERT(obj->closure);
156 switch (get_itbl(obj->closure)->type) {
159 StgBCO* bco = stgCast(StgBCO*,obj->closure);
160 ASSERT(i < bco->n_ptrs && bcoConstPtr(bco,i) == NULL);
161 bcoConstCPtr(bco,i) = reference;
166 StgCAF* caf = stgCast(StgCAF*,obj->closure);
167 ASSERT(i == 0 && caf->body == NULL);
168 caf->body = reference;
173 StgClosure* con = stgCast(StgClosure*,obj->closure);
174 ASSERT(i < get_itbl(con)->layout.payload.nptrs && payloadCPtr(con,i) == NULL);
175 payloadCPtr(con,i) = reference;
180 StgAP_UPD* ap = stgCast(StgAP_UPD*,obj->closure);
181 ASSERT(i < 1+ap->n_args);
183 ASSERT(ap->fun == NULL);
186 ASSERT(payloadCPtr(ap,i-1) == NULL);
187 payloadCPtr(ap,i-1) = reference;
192 barf("asmResolveRef");
194 obj->num_unresolved -= 1;
197 static void asmAddRef( AsmObject referent, AsmObject referer, AsmNat i )
199 if (referent->closure) {
200 asmResolveRef(referer,i,(AsmClosure)referent->closure);
202 insertRefs(&(referent->refs),(AsmRef){referer,i});
206 void asmAddPtr( AsmObject obj, AsmObject arg )
208 ASSERT(obj->closure == 0); /* can't extend an object once it's allocated */
209 insertPtrs( &obj->ptrs, arg );
212 static void asmBeginObject( AsmObject obj )
215 obj->num_unresolved = 0;
216 initRefs(&obj->refs);
217 initPtrs(&obj->ptrs);
220 static void asmEndObject( AsmObject obj, StgClosure* c )
222 obj->num_unresolved = obj->ptrs.len;
224 mapQueue(Ptrs, AsmObject, obj->ptrs, asmAddRef(x,obj,i));
225 mapQueue(Refs, AsmRef, obj->refs, asmResolveRef(x.ref,x.i,c));
227 if (obj->num_unresolved == 0) {
228 freePtrs(&obj->ptrs);
229 freeRefs(&obj->refs);
230 /* we don't print until all ptrs are resolved */
231 IF_DEBUG(codegen,printObj(obj->closure));
235 int asmObjectHasClosure ( AsmObject obj )
237 return (obj->num_unresolved == 0 && obj->closure);
240 AsmClosure asmClosureOfObject ( AsmObject obj )
242 ASSERT(asmObjectHasClosure(obj));
246 void asmMarkObject ( AsmObject obj )
248 ASSERT(obj->num_unresolved == 0 && obj->closure);
249 obj->closure = MarkRoot(obj->closure);
252 /* --------------------------------------------------------------------------
254 * ------------------------------------------------------------------------*/
256 static StgClosure* asmAlloc( nat size )
258 StgClosure* o = stgCast(StgClosure*,allocate(size));
259 ASSERT( size >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
260 /* printf("Allocated %p .. %p\n", o, o+size-1); */
264 static void grabHpUpd( AsmBCO bco, nat size )
266 /* ToDo: sometimes we should test for MIN_UPD_SIZE instead */
267 ASSERT( size >= MIN_UPD_SIZE + sizeofW(StgHeader) );
271 static void grabHpNonUpd( AsmBCO bco, nat size )
273 /* ToDo: sometimes we should test for MIN_UPD_SIZE instead */
274 ASSERT( size >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
278 static void resetHp( AsmBCO bco, nat hp )
280 bco->max_hp = stg_max(bco->hp,bco->max_hp);
284 static void resetSp( AsmBCO bco, AsmSp sp )
286 bco->max_sp = stg_max(bco->sp,bco->max_sp);
290 /* --------------------------------------------------------------------------
292 * ------------------------------------------------------------------------*/
294 AsmObject asmMkObject( AsmClosure c )
296 AsmObject obj = malloc(sizeof(struct AsmObject_));
298 barf("Can't allocate AsmObject");
305 AsmCon asmBeginCon( AsmInfo info )
307 AsmCon con = malloc(sizeof(struct AsmCon_));
309 barf("Can't allocate AsmCon");
311 asmBeginObject(&con->object);
316 void asmEndCon( AsmCon con )
318 nat p = con->object.ptrs.len;
319 nat np = stg_max(0,MIN_NONUPD_SIZE-p);
321 StgClosure* c = asmAlloc(CONSTR_sizeW(p,np));
322 StgClosure* o = stgCast(StgClosure*,c);
323 SET_HDR(o,con->info,??);
324 mapQueue(Ptrs, AsmObject, con->object.ptrs, payloadCPtr(o,i) = NULL);
325 { nat i; for( i=0; i<np; ++i ) { payloadWord(o,p+i) = 0xdeadbeef; } }
326 asmEndObject(&con->object,c);
329 AsmCAF asmBeginCAF( void )
331 AsmCAF caf = malloc(sizeof(struct AsmCAF_));
333 barf("Can't allocate AsmCAF");
335 asmBeginObject(&caf->object);
339 void asmEndCAF( AsmCAF caf, AsmBCO body )
341 StgClosure* c = asmAlloc(CAF_sizeW());
342 StgCAF* o = stgCast(StgCAF*,c);
343 SET_HDR(o,&CAF_UNENTERED_info,??);
345 o->value = stgCast(StgClosure*,0xdeadbeef);
346 o->link = stgCast(StgCAF*,0xdeadbeef);
348 asmAddPtr(&caf->object,&body->object);
349 asmEndObject(&caf->object,c);
352 AsmBCO asmBeginBCO( int /*StgExpr*/ e )
354 AsmBCO bco = malloc(sizeof(struct AsmBCO_));
356 barf("Can't allocate AsmBCO");
358 asmBeginObject(&bco->object);
359 initInstrs(&bco->is);
360 initNonPtrs(&bco->nps);
363 bco->max_sp = bco->sp = 0;
364 bco->max_hp = bco->hp = 0;
365 bco->lastOpc = i_INTERNAL_ERROR;
369 void asmEndBCO( AsmBCO bco )
371 nat p = bco->object.ptrs.len;
372 nat np = bco->nps.len;
373 nat is = bco->is.len + 2; /* 2 for stack check */
375 StgClosure* c = asmAlloc(BCO_sizeW(p,np,is));
376 StgBCO* o = stgCast(StgBCO*,c);
377 SET_HDR(o,&BCO_info,??);
381 o->stgexpr = bco->stgexpr;
382 mapQueue(Ptrs, AsmObject, bco->object.ptrs, bcoConstCPtr(o,i) = NULL);
383 mapQueue(NonPtrs, StgWord, bco->nps, bcoConstWord(o,i) = x);
386 bco->max_sp = stg_max(bco->sp,bco->max_sp);
387 bco->max_hp = stg_max(bco->hp,bco->max_hp);
388 bcoInstr(o,j++) = i_STK_CHECK;
389 bcoInstr(o,j++) = bco->max_sp;
390 mapQueue(Instrs, StgWord8, bco->is, bcoInstr(o,j++) = x);
393 freeInstrs(&bco->is);
394 freeNonPtrs(&bco->nps);
395 asmEndObject(&bco->object,c);
398 /* --------------------------------------------------------------------------
400 * ------------------------------------------------------------------------*/
402 static void asmInstrOp ( AsmBCO bco, StgWord i )
404 ASSERT(i <= BIGGEST_OPCODE); /* must be a valid opcode */
406 insertInstrs(&(bco->is),i);
409 static void asmInstr8 ( AsmBCO bco, StgWord i )
412 ASSERT(i < 256); /* must be a byte */
414 insertInstrs(&(bco->is),i);
417 static void asmInstr16 ( AsmBCO bco, StgWord i )
419 ASSERT(i < 65536); /* must be a short */
420 insertInstrs(&(bco->is),i / 256);
421 insertInstrs(&(bco->is),i % 256);
424 static Instr asmInstrBack ( AsmBCO bco, StgWord n )
426 return bco->is.elems[bco->is.len - n];
429 static void asmInstrRecede ( AsmBCO bco, StgWord n )
431 if (bco->is.len < n) barf("asmInstrRecede");
435 static void asmPtr( AsmBCO bco, AsmObject x )
437 insertPtrs( &bco->object.ptrs, x );
440 static void asmWord( AsmBCO bco, StgWord i )
442 insertNonPtrs( &bco->nps, i );
445 #define asmWords(bco,ty,x) \
447 union { ty a; AsmWord b[sizeofW(ty)]; } p; \
449 if (sizeof(ty) < sizeof(AsmWord)) p.b[0]=0; \
451 for( i = 0; i < sizeofW(ty); i++ ) { \
452 asmWord(bco,p.b[i]); \
456 static StgWord repSizeW( AsmRep rep )
459 case CHAR_REP: return sizeofW(StgWord) + sizeofW(StgChar);
462 case INT_REP: return sizeofW(StgWord) + sizeofW(StgInt);
463 case WORD_REP: return sizeofW(StgWord) + sizeofW(StgWord);
464 case ADDR_REP: return sizeofW(StgWord) + sizeofW(StgAddr);
465 case FLOAT_REP: return sizeofW(StgWord) + sizeofW(StgFloat);
466 case DOUBLE_REP: return sizeofW(StgWord) + sizeofW(StgDouble);
467 #ifdef PROVIDE_STABLE
468 case STABLE_REP: return sizeofW(StgWord) + sizeofW(StgWord);
475 #ifdef PROVIDE_FOREIGN
478 case ALPHA_REP: /* a */
479 case BETA_REP: /* b */
480 case GAMMA_REP: /* c */
481 case HANDLER_REP: /* IOError -> IO a */
482 case ERROR_REP: /* IOError */
483 case ARR_REP : /* PrimArray a */
484 case BARR_REP : /* PrimByteArray a */
485 case REF_REP : /* Ref s a */
486 case MUTARR_REP : /* PrimMutableArray s a */
487 case MUTBARR_REP: /* PrimMutableByteArray s a */
488 #ifdef PROVIDE_CONCURRENT
489 case THREADID_REP: /* ThreadId */
490 case MVAR_REP: /* MVar a */
492 case PTR_REP: return sizeofW(StgPtr);
494 case VOID_REP: return sizeofW(StgWord);
495 default: barf("repSizeW %d",rep);
500 int asmRepSizeW ( AsmRep rep )
502 return repSizeW ( rep );
506 /* --------------------------------------------------------------------------
507 * Instruction emission. All instructions should be routed through here
508 * so that the peephole optimiser gets to see what's happening.
509 * ------------------------------------------------------------------------*/
511 static void emiti_ ( AsmBCO bco, Instr opcode )
514 if (bco->lastOpc == i_SLIDE && opcode == i_ENTER) {
515 /* SLIDE x y ; ENTER ===> SE x y */
516 x = asmInstrBack(bco,2);
517 y = asmInstrBack(bco,1);
518 asmInstrRecede(bco,3);
519 asmInstrOp(bco,i_SE); asmInstr8(bco,x); asmInstr8(bco,y);
522 if (bco->lastOpc == i_RV && opcode == i_ENTER) {
523 /* RV x y ; ENTER ===> RVE x (y-2)
524 Because RETADDR pushes 2 words on the stack, y must be at least 2. */
525 x = asmInstrBack(bco,2);
526 y = asmInstrBack(bco,1);
527 if (y < 2) barf("emiti_: RVE: impossible y value");
528 asmInstrRecede(bco,3);
529 asmInstrOp(bco, i_RVE); asmInstr8(bco,x); asmInstr8(bco,y-2);
532 asmInstrOp(bco,opcode);
536 static void emiti_8 ( AsmBCO bco, Instr opcode, int arg1 )
539 if (bco->lastOpc == i_VAR && opcode == i_VAR) {
540 /* VAR x ; VAR y ===> VV x y */
541 x = asmInstrBack(bco,1);
542 asmInstrRecede(bco,2);
543 asmInstrOp(bco,i_VV); asmInstr8(bco,x); asmInstr8(bco,arg1);
546 if (bco->lastOpc == i_RETADDR && opcode == i_VAR) {
547 /* RETADDR x ; VAR y ===> RV x y */
548 x = asmInstrBack(bco,1);
549 asmInstrRecede(bco,2);
550 asmInstrOp(bco, i_RV); asmInstr8(bco,x); asmInstr8(bco,arg1);
553 asmInstrOp(bco,opcode);
558 static void emiti_16 ( AsmBCO bco, Instr opcode, int arg1 )
560 asmInstrOp(bco,opcode);
561 asmInstr16(bco,arg1);
564 static void emiti_8_8 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
566 asmInstrOp(bco,opcode);
571 static void emiti_8_16 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
573 asmInstrOp(bco,opcode);
575 asmInstr16(bco,arg2);
578 static void emiti_16_16 ( AsmBCO bco, Instr opcode, int arg1, int arg2 )
580 asmInstrOp(bco,opcode);
581 asmInstr16(bco,arg1);
582 asmInstr16(bco,arg2);
586 /* --------------------------------------------------------------------------
587 * Wrappers around the above fns
588 * ------------------------------------------------------------------------*/
590 static void emit_i_VAR_INT ( AsmBCO bco, int arg1 )
594 emiti_8 (bco,i_VAR_INT, arg1); else
595 emiti_16(bco,i_VAR_INT_big,arg1);
598 static void emit_i_VAR_WORD ( AsmBCO bco, int arg1 )
602 emiti_8 (bco,i_VAR_WORD, arg1); else
603 emiti_16(bco,i_VAR_WORD_big,arg1);
606 static void emit_i_VAR_ADDR ( AsmBCO bco, int arg1 )
610 emiti_8 (bco,i_VAR_ADDR, arg1); else
611 emiti_16(bco,i_VAR_ADDR_big,arg1);
614 static void emit_i_VAR_CHAR ( AsmBCO bco, int arg1 )
618 emiti_8 (bco,i_VAR_CHAR, arg1); else
619 emiti_16(bco,i_VAR_CHAR_big,arg1);
622 static void emit_i_VAR_FLOAT ( AsmBCO bco, int arg1 )
626 emiti_8 (bco,i_VAR_FLOAT, arg1); else
627 emiti_16(bco,i_VAR_FLOAT_big,arg1);
630 static void emit_i_VAR_DOUBLE ( AsmBCO bco, int arg1 )
634 emiti_8 (bco,i_VAR_DOUBLE, arg1); else
635 emiti_16(bco,i_VAR_DOUBLE_big,arg1);
638 static void emit_i_VAR ( AsmBCO bco, int arg1 )
642 emiti_8 (bco,i_VAR, arg1); else
643 emiti_16(bco,i_VAR_big,arg1);
646 static void emit_i_PACK ( AsmBCO bco, int arg1 )
650 emiti_8 (bco,i_PACK, arg1); else
651 emiti_16(bco,i_PACK_big,arg1);
654 static void emit_i_SLIDE ( AsmBCO bco, int arg1, int arg2 )
658 if (arg1 < 256 && arg2 < 256)
659 emiti_8_8 (bco,i_SLIDE, arg1,arg2); else
660 emiti_16_16(bco,i_SLIDE_big,arg1,arg2);
663 static void emit_i_MKAP ( AsmBCO bco, int arg1, int arg2 )
667 if (arg1 < 256 && arg2 < 256)
668 emiti_8_8 (bco,i_MKAP, arg1,arg2); else
669 emiti_16_16(bco,i_MKAP_big,arg1,arg2);
673 static void emit_i_CONST_INT ( AsmBCO bco, int arg1 )
677 emiti_8 (bco,i_CONST_INT, arg1); else
678 emiti_16(bco,i_CONST_INT_big,arg1);
681 static void emit_i_CONST_INTEGER ( AsmBCO bco, int arg1 )
685 emiti_8 (bco,i_CONST_INTEGER, arg1); else
686 emiti_16(bco,i_CONST_INTEGER_big,arg1);
689 static void emit_i_CONST_ADDR ( AsmBCO bco, int arg1 )
693 emiti_8 (bco,i_CONST_ADDR, arg1); else
694 emiti_16(bco,i_CONST_ADDR_big,arg1);
697 static void emit_i_CONST_CHAR ( AsmBCO bco, int arg1 )
701 emiti_8 (bco,i_CONST_CHAR, arg1); else
702 emiti_16(bco,i_CONST_CHAR_big,arg1);
705 static void emit_i_CONST_FLOAT ( AsmBCO bco, int arg1 )
709 emiti_8 (bco,i_CONST_FLOAT, arg1); else
710 emiti_16(bco,i_CONST_FLOAT_big,arg1);
713 static void emit_i_CONST_DOUBLE ( AsmBCO bco, int arg1 )
717 emiti_8 (bco,i_CONST_DOUBLE, arg1); else
718 emiti_16(bco,i_CONST_DOUBLE_big,arg1);
721 static void emit_i_CONST ( AsmBCO bco, int arg1 )
725 emiti_8 (bco,i_CONST, arg1); else
726 emiti_16(bco,i_CONST_big,arg1);
729 static void emit_i_RETADDR ( AsmBCO bco, int arg1 )
733 emiti_8 (bco,i_RETADDR, arg1); else
734 emiti_16(bco,i_RETADDR_big,arg1);
738 /* --------------------------------------------------------------------------
740 * ------------------------------------------------------------------------*/
742 AsmSp asmBeginArgCheck ( AsmBCO bco )
744 ASSERT(bco->sp == 0);
748 void asmEndArgCheck ( AsmBCO bco, AsmSp last_arg )
750 nat args = bco->sp - last_arg;
751 if (args != 0) { /* optimisation */
752 emiti_8(bco,i_ARG_CHECK,args);
753 grabHpNonUpd(bco,PAP_sizeW(args-1));
758 /* --------------------------------------------------------------------------
759 * Creating and using "variables"
760 * ------------------------------------------------------------------------*/
762 AsmVar asmBind ( AsmBCO bco, AsmRep rep )
764 bco->sp += repSizeW(rep);
768 void asmVar ( AsmBCO bco, AsmVar v, AsmRep rep )
772 if (rep == VOID_REP) {
774 bco->sp += repSizeW(rep);
778 offset = bco->sp - v;
782 emit_i_VAR_INT(bco,offset);
785 emit_i_VAR_WORD(bco,offset);
788 emit_i_VAR_ADDR(bco,offset);
791 emit_i_VAR_CHAR(bco,offset);
794 emit_i_VAR_FLOAT(bco,offset);
797 emit_i_VAR_DOUBLE(bco,offset);
799 #ifdef PROVIDE_STABLE
801 emit_i_VAR_STABLE(bco,offset);
809 #ifdef PROVIDE_FOREIGN
812 case ALPHA_REP: /* a */
813 case BETA_REP: /* b */
814 case GAMMA_REP: /* c */
815 case HANDLER_REP: /* IOError -> IO a */
816 case ERROR_REP: /* IOError */
817 case ARR_REP : /* PrimArray a */
818 case BARR_REP : /* PrimByteArray a */
819 case REF_REP : /* Ref s a */
820 case MUTARR_REP : /* PrimMutableArray s a */
821 case MUTBARR_REP: /* PrimMutableByteArray s a */
822 #ifdef PROVIDE_CONCURRENT
823 case THREADID_REP: /* ThreadId */
824 case MVAR_REP: /* MVar a */
827 emit_i_VAR(bco,offset);
830 barf("asmVar %d",rep);
832 bco->sp += repSizeW(rep);
835 /* --------------------------------------------------------------------------
837 * ------------------------------------------------------------------------*/
839 AsmSp asmBeginEnter( AsmBCO bco )
844 void asmEndEnter( AsmBCO bco, AsmSp sp1, AsmSp sp2 )
846 int x = bco->sp - sp1;
848 ASSERT(x >= 0 && y >= 0);
850 emit_i_SLIDE(bco,x,y);
851 bco->sp -= sp1 - sp2;
856 /* --------------------------------------------------------------------------
857 * Build boxed Ints, Floats, etc
858 * ------------------------------------------------------------------------*/
860 AsmVar asmBox( AsmBCO bco, AsmRep rep )
864 emiti_(bco,i_PACK_CHAR);
865 grabHpNonUpd(bco,Czh_sizeW);
868 emiti_(bco,i_PACK_INT);
869 grabHpNonUpd(bco,Izh_sizeW);
872 emiti_(bco,i_PACK_WORD);
873 grabHpNonUpd(bco,Wzh_sizeW);
876 emiti_(bco,i_PACK_ADDR);
877 grabHpNonUpd(bco,Azh_sizeW);
880 emiti_(bco,i_PACK_FLOAT);
881 grabHpNonUpd(bco,Fzh_sizeW);
884 emiti_(bco,i_PACK_DOUBLE);
885 grabHpNonUpd(bco,Dzh_sizeW);
887 #ifdef PROVIDE_STABLE
889 emiti_(bco,i_PACK_STABLE);
890 grabHpNonUpd(bco,Stablezh_sizeW);
895 barf("asmBox %d",rep);
897 /* NB: these operations DO pop their arg */
898 bco->sp -= repSizeW(rep); /* pop unboxed arg */
899 bco->sp += sizeofW(StgPtr); /* push box */
903 /* --------------------------------------------------------------------------
904 * Unbox Ints, Floats, etc
905 * ------------------------------------------------------------------------*/
907 AsmVar asmUnbox( AsmBCO bco, AsmRep rep )
911 emiti_(bco,i_UNPACK_INT);
914 emiti_(bco,i_UNPACK_WORD);
917 emiti_(bco,i_UNPACK_ADDR);
920 emiti_(bco,i_UNPACK_CHAR);
923 emiti_(bco,i_UNPACK_FLOAT);
926 emiti_(bco,i_UNPACK_DOUBLE);
928 #ifdef PROVIDE_STABLE
930 emiti_(bco,i_UNPACK_STABLE);
934 barf("asmUnbox %d",rep);
936 /* NB: these operations DO NOT pop their arg */
937 bco->sp += repSizeW(rep); /* push unboxed arg */
942 /* --------------------------------------------------------------------------
943 * Push unboxed Ints, Floats, etc
944 * ------------------------------------------------------------------------*/
946 void asmConstInt( AsmBCO bco, AsmInt x )
948 emit_i_CONST_INT(bco,bco->nps.len);
949 asmWords(bco,AsmInt,x);
950 bco->sp += repSizeW(INT_REP);
953 void asmConstInteger( AsmBCO bco, AsmString x )
955 emit_i_CONST_INTEGER(bco,bco->nps.len);
956 asmWords(bco,AsmString,x);
957 bco->sp += repSizeW(INTEGER_REP);
960 void asmConstAddr( AsmBCO bco, AsmAddr x )
962 emit_i_CONST_ADDR(bco,bco->nps.len);
963 asmWords(bco,AsmAddr,x);
964 bco->sp += repSizeW(ADDR_REP);
967 void asmConstWord( AsmBCO bco, AsmWord x )
969 emit_i_CONST_INT(bco,bco->nps.len);
970 asmWords(bco,AsmWord,(AsmInt)x);
971 bco->sp += repSizeW(WORD_REP);
974 void asmConstChar( AsmBCO bco, AsmChar x )
976 emit_i_CONST_CHAR(bco,bco->nps.len);
977 asmWords(bco,AsmChar,x);
978 bco->sp += repSizeW(CHAR_REP);
981 void asmConstFloat( AsmBCO bco, AsmFloat x )
983 emit_i_CONST_FLOAT(bco,bco->nps.len);
984 asmWords(bco,AsmFloat,x);
985 bco->sp += repSizeW(FLOAT_REP);
988 void asmConstDouble( AsmBCO bco, AsmDouble x )
990 emit_i_CONST_DOUBLE(bco,bco->nps.len);
991 asmWords(bco,AsmDouble,x);
992 bco->sp += repSizeW(DOUBLE_REP);
995 /* --------------------------------------------------------------------------
996 * Algebraic case helpers
997 * ------------------------------------------------------------------------*/
999 /* a mildly bogus pair of functions... */
1000 AsmSp asmBeginCase( AsmBCO bco )
1005 void asmEndCase( AsmBCO bco )
1009 AsmSp asmContinuation( AsmBCO bco, AsmBCO ret_addr )
1011 emit_i_RETADDR(bco,bco->object.ptrs.len);
1012 asmPtr(bco,&(ret_addr->object));
1013 bco->sp += 2 * sizeofW(StgPtr);
1017 AsmBCO asmBeginContinuation ( AsmSp sp, int /*List*/ alts )
1019 AsmBCO bco = asmBeginBCO(alts);
1024 void asmEndContinuation ( AsmBCO bco )
1030 /* --------------------------------------------------------------------------
1032 * ------------------------------------------------------------------------*/
1034 AsmSp asmBeginAlt( AsmBCO bco )
1039 void asmEndAlt( AsmBCO bco, AsmSp sp )
1044 AsmPc asmTest( AsmBCO bco, AsmWord tag )
1046 emiti_8_16(bco,i_TEST,tag,0);
1050 AsmPc asmTestInt( AsmBCO bco, AsmVar v, AsmInt x )
1052 asmVar(bco,v,INT_REP);
1054 emiti_16(bco,i_TEST_INT,0);
1055 bco->sp -= 2*repSizeW(INT_REP);
1059 void asmFixBranch( AsmBCO bco, AsmPc from )
1061 int distance = bco->is.len - from;
1062 ASSERT(distance >= 0);
1063 ASSERT(distance < 65536);
1064 setInstrs(&(bco->is),from-2,distance/256);
1065 setInstrs(&(bco->is),from-1,distance%256);
1068 void asmPanic( AsmBCO bco )
1070 emiti_(bco,i_PANIC); /* "irrefutable" pattern failed - oops! */
1073 /* --------------------------------------------------------------------------
1075 * ------------------------------------------------------------------------*/
1077 AsmSp asmBeginPrim( AsmBCO bco )
1082 void asmEndPrim( AsmBCO bco, const AsmPrim* prim, AsmSp base )
1084 emiti_8(bco,prim->prefix,prim->opcode);
1088 /* Hugs used to let you add arbitrary primops with arbitrary types
1089 * just by editing Prelude.hs or any other file you wanted.
1090 * We deliberately avoided that approach because we wanted more
1091 * control over which primops are provided.
1093 const AsmPrim asmPrimOps[] = {
1095 /* Char# operations */
1096 { "primGtChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_gtChar }
1097 , { "primGeChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_geChar }
1098 , { "primEqChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_eqChar }
1099 , { "primNeChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_neChar }
1100 , { "primLtChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_ltChar }
1101 , { "primLeChar", "CC", "B", MONAD_Id, i_PRIMOP1, i_leChar }
1102 , { "primCharToInt", "C", "I", MONAD_Id, i_PRIMOP1, i_charToInt }
1103 , { "primIntToChar", "I", "C", MONAD_Id, i_PRIMOP1, i_intToChar }
1105 /* Int# operations */
1106 , { "primGtInt", "II", "B", MONAD_Id, i_PRIMOP1, i_gtInt }
1107 , { "primGeInt", "II", "B", MONAD_Id, i_PRIMOP1, i_geInt }
1108 , { "primEqInt", "II", "B", MONAD_Id, i_PRIMOP1, i_eqInt }
1109 , { "primNeInt", "II", "B", MONAD_Id, i_PRIMOP1, i_neInt }
1110 , { "primLtInt", "II", "B", MONAD_Id, i_PRIMOP1, i_ltInt }
1111 , { "primLeInt", "II", "B", MONAD_Id, i_PRIMOP1, i_leInt }
1112 , { "primMinInt", "", "I", MONAD_Id, i_PRIMOP1, i_minInt }
1113 , { "primMaxInt", "", "I", MONAD_Id, i_PRIMOP1, i_maxInt }
1114 , { "primPlusInt", "II", "I", MONAD_Id, i_PRIMOP1, i_plusInt }
1115 , { "primMinusInt", "II", "I", MONAD_Id, i_PRIMOP1, i_minusInt }
1116 , { "primTimesInt", "II", "I", MONAD_Id, i_PRIMOP1, i_timesInt }
1117 , { "primQuotInt", "II", "I", MONAD_Id, i_PRIMOP1, i_quotInt }
1118 , { "primRemInt", "II", "I", MONAD_Id, i_PRIMOP1, i_remInt }
1119 , { "primQuotRemInt", "II", "II", MONAD_Id, i_PRIMOP1, i_quotRemInt }
1120 , { "primNegateInt", "I", "I", MONAD_Id, i_PRIMOP1, i_negateInt }
1122 , { "primAndInt", "II", "I", MONAD_Id, i_PRIMOP1, i_andInt }
1123 , { "primOrInt", "II", "I", MONAD_Id, i_PRIMOP1, i_orInt }
1124 , { "primXorInt", "II", "I", MONAD_Id, i_PRIMOP1, i_xorInt }
1125 , { "primNotInt", "I", "I", MONAD_Id, i_PRIMOP1, i_notInt }
1126 , { "primShiftLInt", "II", "I", MONAD_Id, i_PRIMOP1, i_shiftLInt }
1127 , { "primShiftRAInt", "II", "I", MONAD_Id, i_PRIMOP1, i_shiftRAInt }
1128 , { "primShiftRLInt", "II", "I", MONAD_Id, i_PRIMOP1, i_shiftRLInt }
1130 /* Word# operations */
1131 , { "primGtWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_gtWord }
1132 , { "primGeWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_geWord }
1133 , { "primEqWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_eqWord }
1134 , { "primNeWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_neWord }
1135 , { "primLtWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_ltWord }
1136 , { "primLeWord", "WW", "B", MONAD_Id, i_PRIMOP1, i_leWord }
1137 , { "primMinWord", "", "W", MONAD_Id, i_PRIMOP1, i_minWord }
1138 , { "primMaxWord", "", "W", MONAD_Id, i_PRIMOP1, i_maxWord }
1139 , { "primPlusWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_plusWord }
1140 , { "primMinusWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_minusWord }
1141 , { "primTimesWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_timesWord }
1142 , { "primQuotWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_quotWord }
1143 , { "primRemWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_remWord }
1144 , { "primQuotRemWord", "WW", "WW", MONAD_Id, i_PRIMOP1, i_quotRemWord }
1145 , { "primNegateWord", "W", "W", MONAD_Id, i_PRIMOP1, i_negateWord }
1147 , { "primAndWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_andWord }
1148 , { "primOrWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_orWord }
1149 , { "primXorWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_xorWord }
1150 , { "primNotWord", "W", "W", MONAD_Id, i_PRIMOP1, i_notWord }
1151 , { "primShiftLWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_shiftLWord }
1152 , { "primShiftRAWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_shiftRAWord }
1153 , { "primShiftRLWord", "WW", "W", MONAD_Id, i_PRIMOP1, i_shiftRLWord }
1155 , { "primIntToWord", "I", "W", MONAD_Id, i_PRIMOP1, i_intToWord }
1156 , { "primWordToInt", "W", "I", MONAD_Id, i_PRIMOP1, i_wordToInt }
1158 /* Addr# operations */
1159 , { "primGtAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_gtAddr }
1160 , { "primGeAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_geAddr }
1161 , { "primEqAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_eqAddr }
1162 , { "primNeAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_neAddr }
1163 , { "primLtAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_ltAddr }
1164 , { "primLeAddr", "AA", "B", MONAD_Id, i_PRIMOP1, i_leAddr }
1165 , { "primIntToAddr", "I", "A", MONAD_Id, i_PRIMOP1, i_intToAddr }
1166 , { "primAddrToInt", "A", "I", MONAD_Id, i_PRIMOP1, i_addrToInt }
1168 , { "primIndexCharOffAddr", "AI", "C", MONAD_Id, i_PRIMOP1, i_indexCharOffAddr }
1169 , { "primIndexIntOffAddr", "AI", "I", MONAD_Id, i_PRIMOP1, i_indexIntOffAddr }
1170 , { "primIndexWordOffAddr", "AI", "W", MONAD_Id, i_PRIMOP1, i_indexWordOffAddr }
1171 , { "primIndexAddrOffAddr", "AI", "A", MONAD_Id, i_PRIMOP1, i_indexAddrOffAddr }
1172 , { "primIndexFloatOffAddr", "AI", "F", MONAD_Id, i_PRIMOP1, i_indexFloatOffAddr }
1173 , { "primIndexDoubleOffAddr", "AI", "D", MONAD_Id, i_PRIMOP1, i_indexDoubleOffAddr }
1174 #ifdef PROVIDE_STABLE
1175 , { "primIndexStableOffAddr", "AI", "s", MONAD_Id, i_PRIMOP1, i_indexStableOffAddr }
1178 /* These ops really ought to be in the IO monad */
1179 , { "primReadCharOffAddr", "AI", "C", MONAD_ST, i_PRIMOP1, i_readCharOffAddr }
1180 , { "primReadIntOffAddr", "AI", "I", MONAD_ST, i_PRIMOP1, i_readIntOffAddr }
1181 , { "primReadWordOffAddr", "AI", "W", MONAD_ST, i_PRIMOP1, i_readWordOffAddr }
1182 , { "primReadAddrOffAddr", "AI", "A", MONAD_ST, i_PRIMOP1, i_readAddrOffAddr }
1183 , { "primReadFloatOffAddr", "AI", "F", MONAD_ST, i_PRIMOP1, i_readFloatOffAddr }
1184 , { "primReadDoubleOffAddr", "AI", "D", MONAD_ST, i_PRIMOP1, i_readDoubleOffAddr }
1185 #ifdef PROVIDE_STABLE
1186 , { "primReadStableOffAddr", "AI", "s", MONAD_ST, i_PRIMOP1, i_readStableOffAddr }
1189 /* These ops really ought to be in the IO monad */
1190 , { "primWriteCharOffAddr", "AIC", "", MONAD_ST, i_PRIMOP1, i_writeCharOffAddr }
1191 , { "primWriteIntOffAddr", "AII", "", MONAD_ST, i_PRIMOP1, i_writeIntOffAddr }
1192 , { "primWriteWordOffAddr", "AIW", "", MONAD_ST, i_PRIMOP1, i_writeWordOffAddr }
1193 , { "primWriteAddrOffAddr", "AIA", "", MONAD_ST, i_PRIMOP1, i_writeAddrOffAddr }
1194 , { "primWriteFloatOffAddr", "AIF", "", MONAD_ST, i_PRIMOP1, i_writeFloatOffAddr }
1195 , { "primWriteDoubleOffAddr", "AID", "", MONAD_ST, i_PRIMOP1, i_writeDoubleOffAddr }
1196 #ifdef PROVIDE_STABLE
1197 , { "primWriteStableOffAddr", "AIs", "", MONAD_ST, i_PRIMOP1, i_writeStableOffAddr }
1200 /* Integer operations */
1201 , { "primCompareInteger", "ZZ", "I", MONAD_Id, i_PRIMOP1, i_compareInteger }
1202 , { "primNegateInteger", "Z", "Z", MONAD_Id, i_PRIMOP1, i_negateInteger }
1203 , { "primPlusInteger", "ZZ", "Z", MONAD_Id, i_PRIMOP1, i_plusInteger }
1204 , { "primMinusInteger", "ZZ", "Z", MONAD_Id, i_PRIMOP1, i_minusInteger }
1205 , { "primTimesInteger", "ZZ", "Z", MONAD_Id, i_PRIMOP1, i_timesInteger }
1206 , { "primQuotRemInteger", "ZZ", "ZZ", MONAD_Id, i_PRIMOP1, i_quotRemInteger }
1207 , { "primDivModInteger", "ZZ", "ZZ", MONAD_Id, i_PRIMOP1, i_divModInteger }
1208 , { "primIntegerToInt", "Z", "I", MONAD_Id, i_PRIMOP1, i_integerToInt }
1209 , { "primIntToInteger", "I", "Z", MONAD_Id, i_PRIMOP1, i_intToInteger }
1210 , { "primIntegerToWord", "Z", "W", MONAD_Id, i_PRIMOP1, i_integerToWord }
1211 , { "primWordToInteger", "W", "Z", MONAD_Id, i_PRIMOP1, i_wordToInteger }
1212 , { "primIntegerToFloat", "Z", "F", MONAD_Id, i_PRIMOP1, i_integerToFloat }
1213 , { "primFloatToInteger", "F", "Z", MONAD_Id, i_PRIMOP1, i_floatToInteger }
1214 , { "primIntegerToDouble", "Z", "D", MONAD_Id, i_PRIMOP1, i_integerToDouble }
1215 , { "primDoubleToInteger", "D", "Z", MONAD_Id, i_PRIMOP1, i_doubleToInteger }
1217 /* Float# operations */
1218 , { "primGtFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_gtFloat }
1219 , { "primGeFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_geFloat }
1220 , { "primEqFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_eqFloat }
1221 , { "primNeFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_neFloat }
1222 , { "primLtFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_ltFloat }
1223 , { "primLeFloat", "FF", "B", MONAD_Id, i_PRIMOP1, i_leFloat }
1224 , { "primMinFloat", "", "F", MONAD_Id, i_PRIMOP1, i_minFloat }
1225 , { "primMaxFloat", "", "F", MONAD_Id, i_PRIMOP1, i_maxFloat }
1226 , { "primRadixFloat", "", "I", MONAD_Id, i_PRIMOP1, i_radixFloat }
1227 , { "primDigitsFloat", "", "I", MONAD_Id, i_PRIMOP1, i_digitsFloat }
1228 , { "primMinExpFloat", "", "I", MONAD_Id, i_PRIMOP1, i_minExpFloat }
1229 , { "primMaxExpFloat", "", "I", MONAD_Id, i_PRIMOP1, i_maxExpFloat }
1230 , { "primPlusFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_plusFloat }
1231 , { "primMinusFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_minusFloat }
1232 , { "primTimesFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_timesFloat }
1233 , { "primDivideFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_divideFloat }
1234 , { "primNegateFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_negateFloat }
1235 , { "primFloatToInt", "F", "I", MONAD_Id, i_PRIMOP1, i_floatToInt }
1236 , { "primIntToFloat", "I", "F", MONAD_Id, i_PRIMOP1, i_intToFloat }
1237 , { "primExpFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_expFloat }
1238 , { "primLogFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_logFloat }
1239 , { "primSqrtFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_sqrtFloat }
1240 , { "primSinFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_sinFloat }
1241 , { "primCosFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_cosFloat }
1242 , { "primTanFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_tanFloat }
1243 , { "primAsinFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_asinFloat }
1244 , { "primAcosFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_acosFloat }
1245 , { "primAtanFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_atanFloat }
1246 , { "primSinhFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_sinhFloat }
1247 , { "primCoshFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_coshFloat }
1248 , { "primTanhFloat", "F", "F", MONAD_Id, i_PRIMOP1, i_tanhFloat }
1249 , { "primPowerFloat", "FF", "F", MONAD_Id, i_PRIMOP1, i_powerFloat }
1250 , { "primDecodeFloatZ", "F", "ZI", MONAD_Id, i_PRIMOP1, i_decodeFloatZ }
1251 , { "primEncodeFloatZ", "ZI", "F", MONAD_Id, i_PRIMOP1, i_encodeFloatZ }
1252 , { "primIsNaNFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isNaNFloat }
1253 , { "primIsInfiniteFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isInfiniteFloat }
1254 , { "primIsDenormalizedFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isDenormalizedFloat }
1255 , { "primIsNegativeZeroFloat", "F", "B", MONAD_Id, i_PRIMOP1, i_isNegativeZeroFloat }
1256 , { "primIsIEEEFloat", "", "B", MONAD_Id, i_PRIMOP1, i_isIEEEFloat }
1258 /* Double# operations */
1259 , { "primGtDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_gtDouble }
1260 , { "primGeDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_geDouble }
1261 , { "primEqDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_eqDouble }
1262 , { "primNeDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_neDouble }
1263 , { "primLtDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_ltDouble }
1264 , { "primLeDouble", "DD", "B", MONAD_Id, i_PRIMOP1, i_leDouble }
1265 , { "primMinDouble", "", "D", MONAD_Id, i_PRIMOP1, i_minDouble }
1266 , { "primMaxDouble", "", "D", MONAD_Id, i_PRIMOP1, i_maxDouble }
1267 , { "primRadixDouble", "", "I", MONAD_Id, i_PRIMOP1, i_radixDouble }
1268 , { "primDigitsDouble", "", "I", MONAD_Id, i_PRIMOP1, i_digitsDouble }
1269 , { "primMinExpDouble", "", "I", MONAD_Id, i_PRIMOP1, i_minExpDouble }
1270 , { "primMaxExpDouble", "", "I", MONAD_Id, i_PRIMOP1, i_maxExpDouble }
1271 , { "primPlusDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_plusDouble }
1272 , { "primMinusDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_minusDouble }
1273 , { "primTimesDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_timesDouble }
1274 , { "primDivideDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_divideDouble }
1275 , { "primNegateDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_negateDouble }
1276 , { "primDoubleToInt", "D", "I", MONAD_Id, i_PRIMOP1, i_doubleToInt }
1277 , { "primIntToDouble", "I", "D", MONAD_Id, i_PRIMOP1, i_intToDouble }
1278 , { "primDoubleToFloat", "D", "F", MONAD_Id, i_PRIMOP1, i_doubleToFloat }
1279 , { "primFloatToDouble", "F", "D", MONAD_Id, i_PRIMOP1, i_floatToDouble }
1280 , { "primExpDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_expDouble }
1281 , { "primLogDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_logDouble }
1282 , { "primSqrtDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_sqrtDouble }
1283 , { "primSinDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_sinDouble }
1284 , { "primCosDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_cosDouble }
1285 , { "primTanDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_tanDouble }
1286 , { "primAsinDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_asinDouble }
1287 , { "primAcosDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_acosDouble }
1288 , { "primAtanDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_atanDouble }
1289 , { "primSinhDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_sinhDouble }
1290 , { "primCoshDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_coshDouble }
1291 , { "primTanhDouble", "D", "D", MONAD_Id, i_PRIMOP1, i_tanhDouble }
1292 , { "primPowerDouble", "DD", "D", MONAD_Id, i_PRIMOP1, i_powerDouble }
1293 , { "primDecodeDoubleZ", "D", "ZI", MONAD_Id, i_PRIMOP1, i_decodeDoubleZ }
1294 , { "primEncodeDoubleZ", "ZI", "D", MONAD_Id, i_PRIMOP1, i_encodeDoubleZ }
1295 , { "primIsNaNDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isNaNDouble }
1296 , { "primIsInfiniteDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isInfiniteDouble }
1297 , { "primIsDenormalizedDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isDenormalizedDouble }
1298 , { "primIsNegativeZeroDouble", "D", "B", MONAD_Id, i_PRIMOP1, i_isNegativeZeroDouble }
1299 , { "primIsIEEEDouble", "", "B", MONAD_Id, i_PRIMOP1, i_isIEEEDouble }
1301 /* Ref operations */
1302 , { "primNewRef", "a", "R", MONAD_ST, i_PRIMOP2, i_newRef }
1303 , { "primWriteRef", "Ra", "", MONAD_ST, i_PRIMOP2, i_writeRef }
1304 , { "primReadRef", "R", "a", MONAD_ST, i_PRIMOP2, i_readRef }
1305 , { "primSameRef", "RR", "B", MONAD_Id, i_PRIMOP2, i_sameRef }
1307 /* PrimArray operations */
1308 , { "primSameMutableArray", "MM", "B", MONAD_Id, i_PRIMOP2, i_sameMutableArray }
1309 , { "primUnsafeFreezeArray", "M", "X", MONAD_ST, i_PRIMOP2, i_unsafeFreezeArray }
1310 , { "primNewArray", "Ia", "M", MONAD_ST, i_PRIMOP2, i_newArray }
1311 , { "primWriteArray", "MIa", "", MONAD_ST, i_PRIMOP2, i_writeArray }
1312 , { "primReadArray", "MI", "a", MONAD_ST, i_PRIMOP2, i_readArray }
1313 , { "primIndexArray", "XI", "a", MONAD_Id, i_PRIMOP2, i_indexArray }
1314 , { "primSizeArray", "X", "I", MONAD_Id, i_PRIMOP2, i_sizeArray }
1315 , { "primSizeMutableArray", "M", "I", MONAD_Id, i_PRIMOP2, i_sizeMutableArray }
1317 /* Prim[Mutable]ByteArray operations */
1318 , { "primSameMutableByteArray", "mm", "B", MONAD_Id, i_PRIMOP2, i_sameMutableByteArray }
1319 , { "primUnsafeFreezeByteArray", "m", "x", MONAD_ST, i_PRIMOP2, i_unsafeFreezeByteArray }
1321 , { "primNewByteArray", "I", "m", MONAD_ST, i_PRIMOP2, i_newByteArray }
1323 , { "primWriteCharArray", "mIC", "", MONAD_ST, i_PRIMOP2, i_writeCharArray }
1324 , { "primReadCharArray", "mI", "C", MONAD_ST, i_PRIMOP2, i_readCharArray }
1325 , { "primIndexCharArray", "xI", "C", MONAD_Id, i_PRIMOP2, i_indexCharArray }
1327 , { "primWriteIntArray", "mII", "", MONAD_ST, i_PRIMOP2, i_writeIntArray }
1328 , { "primReadIntArray", "mI", "I", MONAD_ST, i_PRIMOP2, i_readIntArray }
1329 , { "primIndexIntArray", "xI", "I", MONAD_Id, i_PRIMOP2, i_indexIntArray }
1331 /* {new,write,read,index}IntegerArray not provided */
1333 , { "primWriteWordArray", "mIW", "", MONAD_ST, i_PRIMOP2, i_writeWordArray }
1334 , { "primReadWordArray", "mI", "W", MONAD_ST, i_PRIMOP2, i_readWordArray }
1335 , { "primIndexWordArray", "xI", "W", MONAD_Id, i_PRIMOP2, i_indexWordArray }
1336 , { "primWriteAddrArray", "mIA", "", MONAD_ST, i_PRIMOP2, i_writeAddrArray }
1337 , { "primReadAddrArray", "mI", "A", MONAD_ST, i_PRIMOP2, i_readAddrArray }
1338 , { "primIndexAddrArray", "xI", "A", MONAD_Id, i_PRIMOP2, i_indexAddrArray }
1339 , { "primWriteFloatArray", "mIF", "", MONAD_ST, i_PRIMOP2, i_writeFloatArray }
1340 , { "primReadFloatArray", "mI", "F", MONAD_ST, i_PRIMOP2, i_readFloatArray }
1341 , { "primIndexFloatArray", "xI", "F", MONAD_Id, i_PRIMOP2, i_indexFloatArray }
1342 , { "primWriteDoubleArray" , "mID", "", MONAD_ST, i_PRIMOP2, i_writeDoubleArray }
1343 , { "primReadDoubleArray", "mI", "D", MONAD_ST, i_PRIMOP2, i_readDoubleArray }
1344 , { "primIndexDoubleArray", "xI", "D", MONAD_Id, i_PRIMOP2, i_indexDoubleArray }
1346 #ifdef PROVIDE_STABLE
1347 , { "primWriteStableArray", "mIs", "", MONAD_ST, i_PRIMOP2, i_writeStableArray }
1348 , { "primReadStableArray", "mI", "s", MONAD_ST, i_PRIMOP2, i_readStableArray }
1349 , { "primIndexStableArray", "xI", "s", MONAD_Id, i_PRIMOP2, i_indexStableArray }
1352 /* {new,write,read,index}ForeignObjArray not provided */
1355 #ifdef PROVIDE_FOREIGN
1356 /* ForeignObj# operations */
1357 , { "primMakeForeignObj", "A", "f", MONAD_IO, i_PRIMOP2, i_makeForeignObj }
1360 /* WeakPair# operations */
1361 , { "primMakeWeak", "bac", "w", MONAD_IO, i_PRIMOP2, i_makeWeak }
1362 , { "primDeRefWeak", "w", "Ia", MONAD_IO, i_PRIMOP2, i_deRefWeak }
1364 #ifdef PROVIDE_STABLE
1365 /* StablePtr# operations */
1366 , { "primMakeStablePtr", "a", "s", MONAD_IO, i_PRIMOP2, i_makeStablePtr }
1367 , { "primDeRefStablePtr", "s", "a", MONAD_IO, i_PRIMOP2, i_deRefStablePtr }
1368 , { "primFreeStablePtr", "s", "", MONAD_IO, i_PRIMOP2, i_freeStablePtr }
1370 #ifdef PROVIDE_PTREQUALITY
1371 , { "primReallyUnsafePtrEquality", "aa", "B",MONAD_Id, i_PRIMOP2, i_reallyUnsafePtrEquality }
1373 #ifdef PROVIDE_COERCE
1374 , { "primUnsafeCoerce", "a", "b", MONAD_Id, i_PRIMOP2, i_unsafeCoerce }
1376 #ifdef PROVIDE_CONCURRENT
1377 /* Concurrency operations */
1378 , { "primFork", "a", "T", MONAD_IO, i_PRIMOP2, i_fork }
1379 , { "primKillThread", "T", "", MONAD_IO, i_PRIMOP2, i_killThread }
1380 , { "primSameMVar", "rr", "B", MONAD_Id, i_PRIMOP2, i_sameMVar }
1381 , { "primNewMVar", "", "r", MONAD_IO, i_PRIMOP2, i_newMVar }
1382 , { "primTakeMVar", "r", "a", MONAD_IO, i_PRIMOP2, i_takeMVar }
1383 , { "primPutMVar", "ra", "", MONAD_IO, i_PRIMOP2, i_putMVar }
1384 , { "primDelay", "I", "", MONAD_IO, i_PRIMOP2, i_delay }
1385 , { "primWaitRead", "I", "", MONAD_IO, i_PRIMOP2, i_waitRead }
1386 , { "primWaitWrite", "I", "", MONAD_IO, i_PRIMOP2, i_waitWrite }
1389 /* Ccall is polyadic - so it's excluded from this table */
1394 const AsmPrim ccall_Id = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_Id };
1395 const AsmPrim ccall_IO = { "ccall", 0, 0, MONAD_IO, i_PRIMOP2, i_ccall_IO };
1398 const AsmPrim* asmFindPrim( char* s )
1401 for (i=0; asmPrimOps[i].name; ++i) {
1402 if (strcmp(s,asmPrimOps[i].name)==0) {
1403 return &asmPrimOps[i];
1409 const AsmPrim* asmFindPrimop( AsmInstr prefix, AsmInstr op )
1412 for (i=0; asmPrimOps[i].name; ++i) {
1413 if (asmPrimOps[i].prefix == prefix && asmPrimOps[i].opcode == op) {
1414 return &asmPrimOps[i];
1420 /* --------------------------------------------------------------------------
1421 * Handwritten primops
1422 * ------------------------------------------------------------------------*/
1424 AsmBCO asm_BCO_catch ( void )
1426 AsmBCO bco = asmBeginBCO(0 /*NIL*/);
1427 emiti_8(bco,i_ARG_CHECK,2);
1428 emiti_8(bco,i_PRIMOP1,i_pushcatchframe);
1429 bco->sp += (1-2)*sizeofW(StgPtr) + sizeofW(StgCatchFrame);
1430 emiti_(bco,i_ENTER);
1435 AsmBCO asm_BCO_raise ( void )
1437 AsmBCO bco = asmBeginBCO(0 /*NIL*/);
1438 emiti_8(bco,i_ARG_CHECK,1);
1439 emiti_8(bco,i_PRIMOP2,i_raise);
1444 AsmBCO asm_BCO_seq ( void )
1448 cont = asmBeginBCO(0 /*NIL*/);
1449 emiti_8(cont,i_ARG_CHECK,2);
1451 emit_i_SLIDE(cont,1,2);
1452 emiti_(cont,i_ENTER);
1453 cont->sp += 3*sizeofW(StgPtr);
1456 eval = asmBeginBCO(0 /*NIL*/);
1457 emiti_8(eval,i_ARG_CHECK,2);
1458 emit_i_RETADDR(eval,eval->object.ptrs.len);
1459 asmPtr(eval,&(cont->object));
1461 emit_i_SLIDE(eval,3,1);
1462 emiti_8(eval,i_PRIMOP1,i_pushseqframe);
1463 emiti_(eval,i_ENTER);
1464 eval->sp += sizeofW(StgSeqFrame) + 4*sizeofW(StgPtr);
1470 /* --------------------------------------------------------------------------
1472 * ------------------------------------------------------------------------*/
1474 AsmVar asmAllocCONSTR ( AsmBCO bco, AsmInfo info )
1476 ASSERT( sizeW_fromITBL(info) >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
1477 emiti_8(bco,i_ALLOC_CONSTR,bco->nps.len);
1478 asmWords(bco,AsmInfo,info);
1479 bco->sp += sizeofW(StgClosurePtr);
1480 grabHpNonUpd(bco,sizeW_fromITBL(info));
1484 AsmSp asmBeginPack( AsmBCO bco )
1489 void asmEndPack( AsmBCO bco, AsmVar v, AsmSp start, AsmInfo info )
1491 nat size = bco->sp - start;
1492 assert(bco->sp >= start);
1494 /* only reason to include info is for this assertion */
1495 assert(info->layout.payload.ptrs == size);
1496 emit_i_PACK(bco, bco->sp - v);
1500 void asmBeginUnpack( AsmBCO bco )
1502 /* dummy to make it look prettier */
1505 void asmEndUnpack( AsmBCO bco )
1507 emiti_(bco,i_UNPACK);
1510 AsmVar asmAllocAP( AsmBCO bco, AsmNat words )
1512 emiti_8(bco,i_ALLOC_AP,words);
1513 bco->sp += sizeofW(StgPtr);
1514 grabHpUpd(bco,AP_sizeW(words));
1518 AsmSp asmBeginMkAP( AsmBCO bco )
1523 void asmEndMkAP( AsmBCO bco, AsmVar v, AsmSp start )
1525 emit_i_MKAP(bco,bco->sp-v,bco->sp-start-1);
1526 /* -1 because fun isn't counted */
1530 AsmVar asmAllocPAP( AsmBCO bco, AsmNat size )
1532 emiti_8(bco,i_ALLOC_PAP,size);
1533 bco->sp += sizeofW(StgPtr);
1537 AsmSp asmBeginMkPAP( AsmBCO bco )
1542 void asmEndMkPAP( AsmBCO bco, AsmVar v, AsmSp start )
1544 emiti_8_8(bco,i_MKPAP,bco->sp-v,bco->sp-start-1);
1545 /* -1 because fun isn't counted */
1549 AsmVar asmClosure( AsmBCO bco, AsmObject p )
1551 emit_i_CONST(bco,bco->object.ptrs.len);
1553 bco->sp += sizeofW(StgPtr);
1557 AsmVar asmGHCClosure( AsmBCO bco, AsmObject p )
1559 // A complete hack. Pushes the address as a tagged int
1560 // and then uses SLIDE to get rid of the tag. Appalling.
1561 asmConstInt(bco, (AsmInt)p);
1562 emit_i_SLIDE(bco,0,1); bco->sp -= 1;
1567 /* --------------------------------------------------------------------------
1568 * Building InfoTables
1569 * ------------------------------------------------------------------------*/
1571 AsmInfo asmMkInfo( AsmNat tag, AsmNat ptrs )
1573 StgInfoTable* info = stgMallocBytes( sizeof(StgInfoTable),"asmMkInfo");
1574 /* Note: the evaluator automatically pads objects with the right number
1575 * of non-ptrs to satisfy MIN_NONUPD_SIZE restrictions.
1577 AsmNat nptrs = stg_max(0,MIN_NONUPD_SIZE-ptrs);
1579 /* initialisation code based on INFO_TABLE_CONSTR */
1580 info->layout.payload.ptrs = ptrs;
1581 info->layout.payload.nptrs = nptrs;
1582 info->srt_len = tag;
1583 info->type = CONSTR;
1584 #ifdef USE_MINIINTERPRETER
1585 info->entry = stgCast(StgFunPtr,&Hugs_CONSTR_entry);
1587 #warning asmMkInfo: Need to insert entry code in some cunning way
1589 ASSERT( sizeW_fromITBL(info) >= MIN_NONUPD_SIZE + sizeofW(StgHeader) );
1593 /*-------------------------------------------------------------------------*/
1595 #endif /* INTERPRETER */