1 /* -----------------------------------------------------------------------------
4 * Copyright (c) The GHC Team, 1994-2002.
5 * ---------------------------------------------------------------------------*/
7 #include "PosixSource.h"
20 #include "Bytecodes.h"
22 #include "Disassembler.h"
23 #include "Interpreter.h"
25 #include <string.h> /* for memcpy */
31 /* --------------------------------------------------------------------------
32 * The bytecode interpreter
33 * ------------------------------------------------------------------------*/
35 /* Gather stats about entry, opcode, opcode-pair frequencies. For
36 tuning the interpreter. */
38 /* #define INTERP_STATS */
41 /* Sp points to the lowest live word on the stack. */
43 #define BCO_NEXT instrs[bciPtr++]
44 #define BCO_PTR(n) (W_)ptrs[n]
45 #define BCO_LIT(n) (W_)literals[n]
46 #define BCO_ITBL(n) itbls[n]
48 #define LOAD_STACK_POINTERS \
49 Sp = cap->r.rCurrentTSO->sp; \
50 /* We don't change this ... */ \
51 SpLim = cap->r.rCurrentTSO->stack + RESERVED_STACK_WORDS;
53 #define SAVE_STACK_POINTERS \
54 cap->r.rCurrentTSO->sp = Sp
56 #define RETURN_TO_SCHEDULER(todo,retcode) \
57 SAVE_STACK_POINTERS; \
58 cap->r.rCurrentTSO->what_next = (todo); \
63 allocate_UPD (int n_words)
65 return allocate(stg_max(sizeofW(StgHeader)+MIN_UPD_SIZE, n_words));
69 allocate_NONUPD (int n_words)
71 return allocate(stg_max(sizeofW(StgHeader)+MIN_NONUPD_SIZE, n_words));
77 /* Hacky stats, for tuning the interpreter ... */
78 int it_unknown_entries[N_CLOSURE_TYPES];
79 int it_total_unknown_entries;
91 int it_oofreq[27][27];
94 #define INTERP_TICK(n) (n)++
96 void interp_startup ( void )
99 it_retto_BCO = it_retto_UPDATE = it_retto_other = 0;
100 it_total_entries = it_total_unknown_entries = 0;
101 for (i = 0; i < N_CLOSURE_TYPES; i++)
102 it_unknown_entries[i] = 0;
103 it_slides = it_insns = it_BCO_entries = 0;
104 for (i = 0; i < 27; i++) it_ofreq[i] = 0;
105 for (i = 0; i < 27; i++)
106 for (j = 0; j < 27; j++)
111 void interp_shutdown ( void )
113 int i, j, k, o_max, i_max, j_max;
114 fprintf(stderr, "%d constrs entered -> (%d BCO, %d UPD, %d ??? )\n",
115 it_retto_BCO + it_retto_UPDATE + it_retto_other,
116 it_retto_BCO, it_retto_UPDATE, it_retto_other );
117 fprintf(stderr, "%d total entries, %d unknown entries \n",
118 it_total_entries, it_total_unknown_entries);
119 for (i = 0; i < N_CLOSURE_TYPES; i++) {
120 if (it_unknown_entries[i] == 0) continue;
121 fprintf(stderr, " type %2d: unknown entries (%4.1f%%) == %d\n",
122 i, 100.0 * ((double)it_unknown_entries[i]) /
123 ((double)it_total_unknown_entries),
124 it_unknown_entries[i]);
126 fprintf(stderr, "%d insns, %d slides, %d BCO_entries\n",
127 it_insns, it_slides, it_BCO_entries);
128 for (i = 0; i < 27; i++)
129 fprintf(stderr, "opcode %2d got %d\n", i, it_ofreq[i] );
131 for (k = 1; k < 20; k++) {
134 for (i = 0; i < 27; i++) {
135 for (j = 0; j < 27; j++) {
136 if (it_oofreq[i][j] > o_max) {
137 o_max = it_oofreq[i][j];
138 i_max = i; j_max = j;
143 fprintf ( stderr, "%d: count (%4.1f%%) %6d is %d then %d\n",
144 k, ((double)o_max) * 100.0 / ((double)it_insns), o_max,
146 it_oofreq[i_max][j_max] = 0;
151 #else // !INTERP_STATS
153 #define INTERP_TICK(n) /* nothing */
157 static StgWord app_ptrs_itbl[] = {
160 (W_)&stg_ap_ppp_info,
161 (W_)&stg_ap_pppp_info,
162 (W_)&stg_ap_ppppp_info,
163 (W_)&stg_ap_pppppp_info,
167 interpretBCO (Capability* cap)
169 // Use of register here is primarily to make it clear to compilers
170 // that these entities are non-aliasable.
171 register StgPtr Sp; // local state -- stack pointer
172 register StgPtr SpLim; // local state -- stack lim pointer
173 register StgClosure* obj;
178 // ------------------------------------------------------------------------
181 // We have a closure to evaluate. Stack looks like:
185 // Sp | -------------------> closure
188 if (Sp[0] == (W_)&stg_enter_info) {
193 // ------------------------------------------------------------------------
196 // We have a BCO application to perform. Stack looks like:
207 else if (Sp[0] == (W_)&stg_apply_interp_info) {
208 obj = (StgClosure *)Sp[1];
213 // ------------------------------------------------------------------------
216 // We have an unboxed value to return. See comment before
217 // do_return_unboxed, below.
220 goto do_return_unboxed;
223 // Evaluate the object on top of the stack.
225 obj = (StgClosure*)Sp[0]; Sp++;
228 INTERP_TICK(it_total_evals);
230 IF_DEBUG(interpreter,
232 "\n---------------------------------------------------------------\n");
233 fprintf(stderr,"Evaluating: "); printObj(obj);
234 fprintf(stderr,"Sp = %p\n", Sp);
235 fprintf(stderr, "\n" );
237 printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
238 fprintf(stderr, "\n\n");
241 IF_DEBUG(sanity,checkStackChunk(Sp, cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size));
243 switch ( get_itbl(obj)->type ) {
248 case IND_OLDGEN_PERM:
251 obj = ((StgInd*)obj)->indirectee;
262 case CONSTR_CHARLIKE:
264 case CONSTR_NOCAF_STATIC:
277 ASSERT(((StgBCO *)obj)->arity > 0);
280 case AP: /* Copied from stg_AP_entry. */
289 if (Sp - (words+sizeofW(StgUpdateFrame)) < SpLim) {
292 Sp[0] = (W_)&stg_enter_info;
293 RETURN_TO_SCHEDULER(ThreadInterpret, StackOverflow);
296 /* Ok; we're safe. Party on. Push an update frame. */
297 Sp -= sizeofW(StgUpdateFrame);
299 StgUpdateFrame *__frame;
300 __frame = (StgUpdateFrame *)Sp;
301 SET_INFO(__frame, (StgInfoTable *)&stg_upd_frame_info);
302 __frame->updatee = (StgClosure *)(ap);
305 /* Reload the stack */
307 for (i=0; i < words; i++) {
308 Sp[i] = (W_)ap->payload[i];
311 obj = (StgClosure*)ap->fun;
312 ASSERT(get_itbl(obj)->type == BCO);
321 j = get_itbl(obj)->type;
322 ASSERT(j >= 0 && j < N_CLOSURE_TYPES);
323 it_unknown_entries[j]++;
324 it_total_unknown_entries++;
328 // Can't handle this object; yield to scheduler
329 IF_DEBUG(interpreter,
330 fprintf(stderr, "evaluating unknown closure -- yielding to sched\n");
335 Sp[0] = (W_)&stg_enter_info;
336 RETURN_TO_SCHEDULER(ThreadRunGHC, ThreadYielding);
340 // ------------------------------------------------------------------------
341 // We now have an evaluated object (obj). The next thing to
342 // do is return it to the stack frame on top of the stack.
344 ASSERT(closure_HNF(obj));
346 IF_DEBUG(interpreter,
348 "\n---------------------------------------------------------------\n");
349 fprintf(stderr,"Returning: "); printObj(obj);
350 fprintf(stderr,"Sp = %p\n", Sp);
351 fprintf(stderr, "\n" );
352 printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
353 fprintf(stderr, "\n\n");
356 IF_DEBUG(sanity,checkStackChunk(Sp, cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size));
358 switch (get_itbl((StgClosure *)Sp)->type) {
361 const StgInfoTable *info;
363 // NOTE: not using get_itbl().
364 info = ((StgClosure *)Sp)->header.info;
365 if (info == (StgInfoTable *)&stg_ap_v_info) {
366 n = 1; m = 0; goto do_apply;
368 if (info == (StgInfoTable *)&stg_ap_f_info) {
369 n = 1; m = 1; goto do_apply;
371 if (info == (StgInfoTable *)&stg_ap_d_info) {
372 n = 1; m = sizeofW(StgDouble); goto do_apply;
374 if (info == (StgInfoTable *)&stg_ap_l_info) {
375 n = 1; m = sizeofW(StgInt64); goto do_apply;
377 if (info == (StgInfoTable *)&stg_ap_n_info) {
378 n = 1; m = 1; goto do_apply;
380 if (info == (StgInfoTable *)&stg_ap_p_info) {
381 n = 1; m = 1; goto do_apply;
383 if (info == (StgInfoTable *)&stg_ap_pp_info) {
384 n = 2; m = 2; goto do_apply;
386 if (info == (StgInfoTable *)&stg_ap_ppp_info) {
387 n = 3; m = 3; goto do_apply;
389 if (info == (StgInfoTable *)&stg_ap_pppp_info) {
390 n = 4; m = 4; goto do_apply;
392 if (info == (StgInfoTable *)&stg_ap_ppppp_info) {
393 n = 5; m = 5; goto do_apply;
395 if (info == (StgInfoTable *)&stg_ap_pppppp_info) {
396 n = 6; m = 6; goto do_apply;
398 goto do_return_unrecognised;
402 // Returning to an update frame: do the update, pop the update
403 // frame, and continue with the next stack frame.
404 INTERP_TICK(it_retto_UPDATE);
405 UPD_IND(((StgUpdateFrame *)Sp)->updatee, obj);
406 Sp += sizeofW(StgUpdateFrame);
410 // Returning to an interpreted continuation: put the object on
411 // the stack, and start executing the BCO.
412 INTERP_TICK(it_retto_BCO);
415 obj = (StgClosure*)Sp[2];
416 ASSERT(get_itbl(obj)->type == BCO);
420 do_return_unrecognised:
422 // Can't handle this return address; yield to scheduler
423 INTERP_TICK(it_retto_other);
424 IF_DEBUG(interpreter,
425 fprintf(stderr, "returning to unknown frame -- yielding to sched\n");
426 printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
430 Sp[0] = (W_)&stg_enter_info;
431 RETURN_TO_SCHEDULER(ThreadRunGHC, ThreadYielding);
435 // -------------------------------------------------------------------------
436 // Returning an unboxed value. The stack looks like this:
453 // where XXXX_info is one of the stg_gc_unbx_r1_info family.
455 // We're only interested in the case when the real return address
456 // is a BCO; otherwise we'll return to the scheduler.
462 ASSERT( Sp[0] == (W_)&stg_gc_unbx_r1_info
463 || Sp[0] == (W_)&stg_gc_unpt_r1_info
464 || Sp[0] == (W_)&stg_gc_f1_info
465 || Sp[0] == (W_)&stg_gc_d1_info
466 || Sp[0] == (W_)&stg_gc_l1_info
467 || Sp[0] == (W_)&stg_gc_void_info // VoidRep
470 // get the offset of the stg_ctoi_ret_XXX itbl
471 offset = stack_frame_sizeW((StgClosure *)Sp);
473 switch (get_itbl((StgClosure *)Sp+offset)->type) {
476 // Returning to an interpreted continuation: put the object on
477 // the stack, and start executing the BCO.
478 INTERP_TICK(it_retto_BCO);
479 obj = (StgClosure*)Sp[offset+1];
480 ASSERT(get_itbl(obj)->type == BCO);
481 goto run_BCO_return_unboxed;
485 // Can't handle this return address; yield to scheduler
486 INTERP_TICK(it_retto_other);
487 IF_DEBUG(interpreter,
488 fprintf(stderr, "returning to unknown frame -- yielding to sched\n");
489 printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
491 RETURN_TO_SCHEDULER(ThreadRunGHC, ThreadYielding);
498 // -------------------------------------------------------------------------
502 // we have a function to apply (obj), and n arguments taking up m
503 // words on the stack. The info table (stg_ap_pp_info or whatever)
504 // is on top of the arguments on the stack.
506 switch (get_itbl(obj)->type) {
514 // we only cope with PAPs whose function is a BCO
515 if (get_itbl(pap->fun)->type != BCO) {
516 goto defer_apply_to_sched;
523 // n must be greater than 1, and the only kinds of
524 // application we support with more than one argument
525 // are all pointers...
527 // Shuffle the args for this function down, and put
528 // the appropriate info table in the gap.
529 for (i = 0; i < arity; i++) {
532 Sp[arity-1] = app_ptrs_itbl[n-arity-1];
534 // unpack the PAP's arguments onto the stack
536 for (i = 0; i < pap->n_args; i++) {
537 Sp[i] = (W_)pap->payload[i];
542 else if (arity == n) {
544 for (i = 0; i < pap->n_args; i++) {
545 Sp[i] = (W_)pap->payload[i];
550 else /* arity > n */ {
551 // build a new PAP and return it.
554 size = PAP_sizeW(pap->n_args + m);
555 new_pap = (StgPAP *)allocate(size);
556 SET_HDR(new_pap,&stg_PAP_info,CCCS);
557 new_pap->arity = pap->arity - n;
558 new_pap->n_args = pap->n_args + m;
559 new_pap->fun = pap->fun;
560 for (i = 0; i < pap->n_args; i++) {
561 new_pap->payload[i] = pap->payload[i];
563 for (i = 0; i < m; i++) {
564 new_pap->payload[pap->n_args + i] = (StgClosure *)Sp[i];
566 obj = (StgClosure *)new_pap;
576 arity = ((StgBCO *)obj)->arity;
579 // n must be greater than 1, and the only kinds of
580 // application we support with more than one argument
581 // are all pointers...
583 // Shuffle the args for this function down, and put
584 // the appropriate info table in the gap.
585 for (i = 0; i < arity; i++) {
588 Sp[arity-1] = app_ptrs_itbl[n-arity-1];
592 else if (arity == n) {
595 else /* arity > n */ {
596 // build a PAP and return it.
600 pap = (StgPAP *)allocate(size);
601 SET_HDR(pap, &stg_PAP_info,CCCS);
602 pap->arity = arity - n;
605 for (i = 0; i < m; i++) {
606 pap->payload[i] = (StgClosure *)Sp[i];
608 obj = (StgClosure *)pap;
614 // No point in us applying machine-code functions
616 defer_apply_to_sched:
619 Sp[0] = (W_)&stg_enter_info;
620 RETURN_TO_SCHEDULER(ThreadRunGHC, ThreadYielding);
623 // ------------------------------------------------------------------------
624 // Ok, we now have a bco (obj), and its arguments are all on the
625 // stack. We can start executing the byte codes.
627 // The stack is in one of two states. First, if this BCO is a
637 // Second, if this BCO is a continuation:
652 // where retval is the value being returned to this continuation.
653 // In the event of a stack check, heap check, or context switch,
654 // we need to leave the stack in a sane state so the garbage
655 // collector can find all the pointers.
657 // (1) BCO is a function: the BCO's bitmap describes the
658 // pointerhood of the arguments.
660 // (2) BCO is a continuation: BCO's bitmap describes the
661 // pointerhood of the free variables.
663 // Sadly we have three different kinds of stack/heap/cswitch check
668 if (doYouWantToGC()) {
669 Sp--; Sp[0] = (W_)&stg_enter_info;
670 RETURN_TO_SCHEDULER(ThreadInterpret, HeapOverflow);
672 // Stack checks aren't necessary at return points, the stack use
673 // is aggregated into the enclosing function entry point.
676 run_BCO_return_unboxed:
678 if (doYouWantToGC()) {
679 RETURN_TO_SCHEDULER(ThreadInterpret, HeapOverflow);
681 // Stack checks aren't necessary at return points, the stack use
682 // is aggregated into the enclosing function entry point.
689 Sp[0] = (W_)&stg_apply_interp_info;
690 checkStackChunk(Sp,SpLim);
695 if (doYouWantToGC()) {
698 Sp[0] = (W_)&stg_apply_interp_info; // placeholder, really
699 RETURN_TO_SCHEDULER(ThreadInterpret, HeapOverflow);
703 if (Sp - INTERP_STACK_CHECK_THRESH < SpLim) {
706 Sp[0] = (W_)&stg_apply_interp_info; // placeholder, really
707 RETURN_TO_SCHEDULER(ThreadInterpret, StackOverflow);
711 // Now, actually interpret the BCO... (no returning to the
712 // scheduler again until the stack is in an orderly state).
714 INTERP_TICK(it_BCO_entries);
716 register int bciPtr = 1; /* instruction pointer */
717 register StgBCO* bco = (StgBCO*)obj;
718 register StgWord16* instrs = (StgWord16*)(bco->instrs->payload);
719 register StgWord* literals = (StgWord*)(&bco->literals->payload[0]);
720 register StgPtr* ptrs = (StgPtr*)(&bco->ptrs->payload[0]);
721 register StgInfoTable** itbls = (StgInfoTable**)
722 (&bco->itbls->payload[0]);
725 it_lastopc = 0; /* no opcode */
729 ASSERT(bciPtr <= instrs[0]);
730 IF_DEBUG(interpreter,
731 //if (do_print_stack) {
732 //fprintf(stderr, "\n-- BEGIN stack\n");
733 //printStack(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size,iSu);
734 //fprintf(stderr, "-- END stack\n\n");
736 fprintf(stderr,"Sp = %p pc = %d ", Sp, bciPtr);
737 disInstr(bco,bciPtr);
739 fprintf(stderr,"\n");
740 for (i = 8; i >= 0; i--) {
741 fprintf(stderr, "%d %p\n", i, (StgPtr)(*(Sp+i)));
743 fprintf(stderr,"\n");
745 //if (do_print_stack) checkStack(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size,iSu);
748 INTERP_TICK(it_insns);
751 ASSERT( (int)instrs[bciPtr] >= 0 && (int)instrs[bciPtr] < 27 );
752 it_ofreq[ (int)instrs[bciPtr] ] ++;
753 it_oofreq[ it_lastopc ][ (int)instrs[bciPtr] ] ++;
754 it_lastopc = (int)instrs[bciPtr];
760 // Explicit stack check at the beginning of a function
761 // *only* (stack checks in case alternatives are
762 // propagated to the enclosing function).
763 int stk_words_reqd = BCO_NEXT + 1;
764 if (Sp - stk_words_reqd < SpLim) {
767 Sp[0] = (W_)&stg_apply_interp_info;
768 RETURN_TO_SCHEDULER(ThreadInterpret, StackOverflow);
803 Sp[-1] = BCO_PTR(o1);
808 case bci_PUSH_ALTS: {
809 int o_bco = BCO_NEXT;
810 Sp[-2] = (W_)&stg_ctoi_R1p_info;
811 Sp[-1] = BCO_PTR(o_bco);
816 case bci_PUSH_ALTS_P: {
817 int o_bco = BCO_NEXT;
818 Sp[-2] = (W_)&stg_ctoi_R1unpt_info;
819 Sp[-1] = BCO_PTR(o_bco);
824 case bci_PUSH_ALTS_N: {
825 int o_bco = BCO_NEXT;
826 Sp[-2] = (W_)&stg_ctoi_R1n_info;
827 Sp[-1] = BCO_PTR(o_bco);
832 case bci_PUSH_ALTS_F: {
833 int o_bco = BCO_NEXT;
834 Sp[-2] = (W_)&stg_ctoi_F1_info;
835 Sp[-1] = BCO_PTR(o_bco);
840 case bci_PUSH_ALTS_D: {
841 int o_bco = BCO_NEXT;
842 Sp[-2] = (W_)&stg_ctoi_D1_info;
843 Sp[-1] = BCO_PTR(o_bco);
848 case bci_PUSH_ALTS_L: {
849 int o_bco = BCO_NEXT;
850 Sp[-2] = (W_)&stg_ctoi_L1_info;
851 Sp[-1] = BCO_PTR(o_bco);
856 case bci_PUSH_ALTS_V: {
857 int o_bco = BCO_NEXT;
858 Sp[-2] = (W_)&stg_ctoi_V_info;
859 Sp[-1] = BCO_PTR(o_bco);
864 case bci_PUSH_APPLY_N:
865 Sp--; Sp[0] = (W_)&stg_ap_n_info;
867 case bci_PUSH_APPLY_V:
868 Sp--; Sp[0] = (W_)&stg_ap_v_info;
870 case bci_PUSH_APPLY_F:
871 Sp--; Sp[0] = (W_)&stg_ap_f_info;
873 case bci_PUSH_APPLY_D:
874 Sp--; Sp[0] = (W_)&stg_ap_d_info;
876 case bci_PUSH_APPLY_L:
877 Sp--; Sp[0] = (W_)&stg_ap_l_info;
879 case bci_PUSH_APPLY_P:
880 Sp--; Sp[0] = (W_)&stg_ap_p_info;
882 case bci_PUSH_APPLY_PP:
883 Sp--; Sp[0] = (W_)&stg_ap_pp_info;
885 case bci_PUSH_APPLY_PPP:
886 Sp--; Sp[0] = (W_)&stg_ap_ppp_info;
888 case bci_PUSH_APPLY_PPPP:
889 Sp--; Sp[0] = (W_)&stg_ap_pppp_info;
891 case bci_PUSH_APPLY_PPPPP:
892 Sp--; Sp[0] = (W_)&stg_ap_ppppp_info;
894 case bci_PUSH_APPLY_PPPPPP:
895 Sp--; Sp[0] = (W_)&stg_ap_pppppp_info;
900 int o_lits = BCO_NEXT;
901 int n_words = BCO_NEXT;
903 for (i = 0; i < n_words; i++) {
904 Sp[i] = BCO_LIT(o_lits+i);
912 /* a_1, .. a_n, b_1, .. b_by, s => a_1, .. a_n, s */
917 INTERP_TICK(it_slides);
923 int n_payload = BCO_NEXT;
924 int request = PAP_sizeW(n_payload);
925 ap = (StgAP*)allocate_UPD(request);
927 ap->n_args = n_payload;
928 SET_HDR(ap, &stg_AP_info, CCS_SYSTEM/*ToDo*/)
933 case bci_ALLOC_PAP: {
935 int arity = BCO_NEXT;
936 int n_payload = BCO_NEXT;
937 int request = PAP_sizeW(n_payload);
938 pap = (StgPAP*)allocate_NONUPD(request);
940 pap->n_args = n_payload;
942 SET_HDR(pap, &stg_PAP_info, CCS_SYSTEM/*ToDo*/)
949 int stkoff = BCO_NEXT;
950 int n_payload = BCO_NEXT;
951 StgAP* ap = (StgAP*)Sp[stkoff];
952 ASSERT((int)ap->n_args == n_payload);
953 ap->fun = (StgClosure*)Sp[0];
955 // The function should be a BCO, and its bitmap should
956 // cover the payload of the AP correctly.
957 ASSERT(get_itbl(ap->fun)->type == BCO
958 && (get_itbl(ap)->type == PAP ||
959 BCO_BITMAP_SIZE(ap->fun) == ap->n_args));
961 for (i = 0; i < n_payload; i++)
962 ap->payload[i] = (StgClosure*)Sp[i+1];
964 IF_DEBUG(interpreter,
965 fprintf(stderr,"\tBuilt ");
966 printObj((StgClosure*)ap);
972 /* Unpack N ptr words from t.o.s constructor */
974 int n_words = BCO_NEXT;
975 StgClosure* con = (StgClosure*)Sp[0];
977 for (i = 0; i < n_words; i++) {
978 Sp[i] = (W_)con->payload[i];
985 int o_itbl = BCO_NEXT;
986 int n_words = BCO_NEXT;
987 StgInfoTable* itbl = INFO_PTR_TO_STRUCT(BCO_ITBL(o_itbl));
988 int request = CONSTR_sizeW( itbl->layout.payload.ptrs,
989 itbl->layout.payload.nptrs );
990 StgClosure* con = (StgClosure*)allocate_NONUPD(request);
991 ASSERT( itbl->layout.payload.ptrs + itbl->layout.payload.nptrs > 0);
992 SET_HDR(con, BCO_ITBL(o_itbl), CCS_SYSTEM/*ToDo*/);
993 for (i = 0; i < n_words; i++) {
994 con->payload[i] = (StgClosure*)Sp[i];
999 IF_DEBUG(interpreter,
1000 fprintf(stderr,"\tBuilt ");
1001 printObj((StgClosure*)con);
1006 case bci_TESTLT_P: {
1007 int discr = BCO_NEXT;
1008 int failto = BCO_NEXT;
1009 StgClosure* con = (StgClosure*)Sp[0];
1010 if (GET_TAG(con) >= discr) {
1016 case bci_TESTEQ_P: {
1017 int discr = BCO_NEXT;
1018 int failto = BCO_NEXT;
1019 StgClosure* con = (StgClosure*)Sp[0];
1020 if (GET_TAG(con) != discr) {
1026 case bci_TESTLT_I: {
1027 // There should be an Int at Sp[1], and an info table at Sp[0].
1028 int discr = BCO_NEXT;
1029 int failto = BCO_NEXT;
1030 I_ stackInt = (I_)Sp[1];
1031 if (stackInt >= (I_)BCO_LIT(discr))
1036 case bci_TESTEQ_I: {
1037 // There should be an Int at Sp[1], and an info table at Sp[0].
1038 int discr = BCO_NEXT;
1039 int failto = BCO_NEXT;
1040 I_ stackInt = (I_)Sp[1];
1041 if (stackInt != (I_)BCO_LIT(discr)) {
1047 case bci_TESTLT_D: {
1048 // There should be a Double at Sp[1], and an info table at Sp[0].
1049 int discr = BCO_NEXT;
1050 int failto = BCO_NEXT;
1051 StgDouble stackDbl, discrDbl;
1052 stackDbl = PK_DBL( & Sp[1] );
1053 discrDbl = PK_DBL( & BCO_LIT(discr) );
1054 if (stackDbl >= discrDbl) {
1060 case bci_TESTEQ_D: {
1061 // There should be a Double at Sp[1], and an info table at Sp[0].
1062 int discr = BCO_NEXT;
1063 int failto = BCO_NEXT;
1064 StgDouble stackDbl, discrDbl;
1065 stackDbl = PK_DBL( & Sp[1] );
1066 discrDbl = PK_DBL( & BCO_LIT(discr) );
1067 if (stackDbl != discrDbl) {
1073 case bci_TESTLT_F: {
1074 // There should be a Float at Sp[1], and an info table at Sp[0].
1075 int discr = BCO_NEXT;
1076 int failto = BCO_NEXT;
1077 StgFloat stackFlt, discrFlt;
1078 stackFlt = PK_FLT( & Sp[1] );
1079 discrFlt = PK_FLT( & BCO_LIT(discr) );
1080 if (stackFlt >= discrFlt) {
1086 case bci_TESTEQ_F: {
1087 // There should be a Float at Sp[1], and an info table at Sp[0].
1088 int discr = BCO_NEXT;
1089 int failto = BCO_NEXT;
1090 StgFloat stackFlt, discrFlt;
1091 stackFlt = PK_FLT( & Sp[1] );
1092 discrFlt = PK_FLT( & BCO_LIT(discr) );
1093 if (stackFlt != discrFlt) {
1099 // Control-flow ish things
1101 // Context-switch check. We put it here to ensure that
1102 // the interpreter has done at least *some* work before
1103 // context switching: sometimes the scheduler can invoke
1104 // the interpreter with context_switch == 1, particularly
1105 // if the -C0 flag has been given on the cmd line.
1106 if (context_switch) {
1107 Sp--; Sp[0] = (W_)&stg_enter_info;
1108 RETURN_TO_SCHEDULER(ThreadInterpret, ThreadYielding);
1113 obj = (StgClosure *)Sp[0];
1119 Sp[0] = (W_)&stg_gc_unpt_r1_info;
1120 goto do_return_unboxed;
1123 Sp[0] = (W_)&stg_gc_unbx_r1_info;
1124 goto do_return_unboxed;
1127 Sp[0] = (W_)&stg_gc_f1_info;
1128 goto do_return_unboxed;
1131 Sp[0] = (W_)&stg_gc_d1_info;
1132 goto do_return_unboxed;
1135 Sp[0] = (W_)&stg_gc_l1_info;
1136 goto do_return_unboxed;
1139 Sp[0] = (W_)&stg_gc_void_info;
1140 goto do_return_unboxed;
1143 int stkoff = BCO_NEXT;
1144 signed short n = (signed short)(BCO_NEXT);
1145 Sp[stkoff] += (W_)n;
1151 int stk_offset = BCO_NEXT;
1152 int o_itbl = BCO_NEXT;
1153 void(*marshall_fn)(void*) = (void (*)(void*))BCO_LIT(o_itbl);
1155 RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE
1156 + sizeofW(StgRetDyn);
1158 #ifdef RTS_SUPPORTS_THREADS
1160 // Arguments on the TSO stack are not good, because garbage
1161 // collection might move the TSO as soon as we call
1162 // suspendThread below.
1164 W_ arguments[stk_offset];
1166 memcpy(arguments, Sp, sizeof(W_) * stk_offset);
1169 // Restore the Haskell thread's current value of errno
1170 errno = cap->r.rCurrentTSO->saved_errno;
1172 // There are a bunch of non-ptr words on the stack (the
1173 // ccall args, the ccall fun address and space for the
1174 // result), which we need to cover with an info table
1175 // since we might GC during this call.
1177 // We know how many (non-ptr) words there are before the
1178 // next valid stack frame: it is the stk_offset arg to the
1179 // CCALL instruction. So we build a RET_DYN stack frame
1180 // on the stack frame to describe this chunk of stack.
1183 ((StgRetDyn *)Sp)->liveness = NO_PTRS | N_NONPTRS(stk_offset);
1184 ((StgRetDyn *)Sp)->info = (StgInfoTable *)&stg_gc_gen_info;
1186 SAVE_STACK_POINTERS;
1187 tok = suspendThread(&cap->r);
1189 #ifndef RTS_SUPPORTS_THREADS
1191 // suspendThread might have shifted the stack
1192 // around (stack squeezing), so we have to grab the real
1193 // Sp out of the TSO to find the ccall args again.
1195 marshall_fn ( (void*)(cap->r.rCurrentTSO->sp + ret_dyn_size) );
1198 // We already made a copy of the arguments above.
1200 marshall_fn ( arguments );
1203 // And restart the thread again, popping the RET_DYN frame.
1204 cap = (Capability *)((void *)((unsigned char*)resumeThread(tok) - sizeof(StgFunTable)));
1205 LOAD_STACK_POINTERS;
1208 // Save the Haskell thread's current value of errno
1209 cap->r.rCurrentTSO->saved_errno = errno;
1211 #ifdef RTS_SUPPORTS_THREADS
1213 // Copy the "arguments", which might include a return value,
1214 // back to the TSO stack. It would of course be enough to
1215 // just copy the return value, but we don't know the offset.
1216 memcpy(Sp, arguments, sizeof(W_) * stk_offset);
1223 /* BCO_NEXT modifies bciPtr, so be conservative. */
1224 int nextpc = BCO_NEXT;
1230 barf("interpretBCO: hit a CASEFAIL");
1234 barf("interpretBCO: unknown or unimplemented opcode");
1236 } /* switch on opcode */
1240 barf("interpretBCO: fell off end of the interpreter");