1 /* -----------------------------------------------------------------------------
4 * Copyright (c) The GHC Team, 1994-2002.
5 * ---------------------------------------------------------------------------*/
8 #include "PosixSource.h"
10 /* Hack and slash.. */
24 #include "Bytecodes.h"
26 #include "Disassembler.h"
27 #include "Interpreter.h"
30 /* --------------------------------------------------------------------------
31 * The bytecode interpreter
32 * ------------------------------------------------------------------------*/
34 /* Gather stats about entry, opcode, opcode-pair frequencies. For
35 tuning the interpreter. */
37 /* #define INTERP_STATS */
40 /* Sp points to the lowest live word on the stack. */
42 #define BCO_NEXT instrs[bciPtr++]
43 #define BCO_PTR(n) (W_)ptrs[n]
44 #define BCO_LIT(n) (W_)literals[n]
45 #define BCO_ITBL(n) itbls[n]
47 #define LOAD_STACK_POINTERS \
48 Sp = cap->r.rCurrentTSO->sp; \
49 /* We don't change this ... */ \
50 SpLim = cap->r.rCurrentTSO->stack + RESERVED_STACK_WORDS;
52 #define SAVE_STACK_POINTERS \
53 cap->r.rCurrentTSO->sp = Sp
55 #define RETURN_TO_SCHEDULER(todo,retcode) \
56 SAVE_STACK_POINTERS; \
57 cap->r.rCurrentTSO->what_next = (todo); \
62 allocate_UPD (int n_words)
64 return allocate(stg_max(sizeofW(StgHeader)+MIN_UPD_SIZE, n_words));
68 allocate_NONUPD (int n_words)
70 return allocate(stg_max(sizeofW(StgHeader)+MIN_NONUPD_SIZE, n_words));
76 /* Hacky stats, for tuning the interpreter ... */
77 int it_unknown_entries[N_CLOSURE_TYPES];
78 int it_total_unknown_entries;
90 int it_oofreq[27][27];
93 #define INTERP_TICK(n) (n)++
95 void interp_startup ( void )
98 it_retto_BCO = it_retto_UPDATE = it_retto_other = 0;
99 it_total_entries = it_total_unknown_entries = 0;
100 for (i = 0; i < N_CLOSURE_TYPES; i++)
101 it_unknown_entries[i] = 0;
102 it_slides = it_insns = it_BCO_entries = 0;
103 for (i = 0; i < 27; i++) it_ofreq[i] = 0;
104 for (i = 0; i < 27; i++)
105 for (j = 0; j < 27; j++)
110 void interp_shutdown ( void )
112 int i, j, k, o_max, i_max, j_max;
113 fprintf(stderr, "%d constrs entered -> (%d BCO, %d UPD, %d ??? )\n",
114 it_retto_BCO + it_retto_UPDATE + it_retto_other,
115 it_retto_BCO, it_retto_UPDATE, it_retto_other );
116 fprintf(stderr, "%d total entries, %d unknown entries \n",
117 it_total_entries, it_total_unknown_entries);
118 for (i = 0; i < N_CLOSURE_TYPES; i++) {
119 if (it_unknown_entries[i] == 0) continue;
120 fprintf(stderr, " type %2d: unknown entries (%4.1f%%) == %d\n",
121 i, 100.0 * ((double)it_unknown_entries[i]) /
122 ((double)it_total_unknown_entries),
123 it_unknown_entries[i]);
125 fprintf(stderr, "%d insns, %d slides, %d BCO_entries\n",
126 it_insns, it_slides, it_BCO_entries);
127 for (i = 0; i < 27; i++)
128 fprintf(stderr, "opcode %2d got %d\n", i, it_ofreq[i] );
130 for (k = 1; k < 20; k++) {
133 for (i = 0; i < 27; i++) {
134 for (j = 0; j < 27; j++) {
135 if (it_oofreq[i][j] > o_max) {
136 o_max = it_oofreq[i][j];
137 i_max = i; j_max = j;
142 fprintf ( stderr, "%d: count (%4.1f%%) %6d is %d then %d\n",
143 k, ((double)o_max) * 100.0 / ((double)it_insns), o_max,
145 it_oofreq[i_max][j_max] = 0;
150 #else // !INTERP_STATS
152 #define INTERP_TICK(n) /* nothing */
156 static StgWord app_ptrs_itbl[] = {
159 (W_)&stg_ap_ppp_info,
160 (W_)&stg_ap_pppp_info,
161 (W_)&stg_ap_ppppp_info,
162 (W_)&stg_ap_pppppp_info,
163 (W_)&stg_ap_ppppppp_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 if (info == (StgInfoTable *)&stg_ap_ppppppp_info) {
399 n = 7; m = 7; goto do_apply;
401 goto do_return_unrecognised;
405 // Returning to an update frame: do the update, pop the update
406 // frame, and continue with the next stack frame.
407 INTERP_TICK(it_retto_UPDATE);
408 UPD_IND(((StgUpdateFrame *)Sp)->updatee, obj);
409 Sp += sizeofW(StgUpdateFrame);
413 // Returning to an interpreted continuation: put the object on
414 // the stack, and start executing the BCO.
415 INTERP_TICK(it_retto_BCO);
418 obj = (StgClosure*)Sp[2];
419 ASSERT(get_itbl(obj)->type == BCO);
423 do_return_unrecognised:
425 // Can't handle this return address; yield to scheduler
426 INTERP_TICK(it_retto_other);
427 IF_DEBUG(interpreter,
428 fprintf(stderr, "returning to unknown frame -- yielding to sched\n");
429 printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
433 Sp[0] = (W_)&stg_enter_info;
434 RETURN_TO_SCHEDULER(ThreadRunGHC, ThreadYielding);
438 // -------------------------------------------------------------------------
439 // Returning an unboxed value. The stack looks like this:
456 // where XXXX_info is one of the stg_gc_unbx_r1_info family.
458 // We're only interested in the case when the real return address
459 // is a BCO; otherwise we'll return to the scheduler.
465 ASSERT( Sp[0] == (W_)&stg_gc_unbx_r1_info
466 || Sp[0] == (W_)&stg_gc_unpt_r1_info
467 || Sp[0] == (W_)&stg_gc_f1_info
468 || Sp[0] == (W_)&stg_gc_d1_info
469 || Sp[0] == (W_)&stg_gc_l1_info
470 || Sp[0] == (W_)&stg_gc_void_info // VoidRep
473 // get the offset of the stg_ctoi_ret_XXX itbl
474 offset = stack_frame_sizeW((StgClosure *)Sp);
476 switch (get_itbl((StgClosure *)Sp+offset)->type) {
479 // Returning to an interpreted continuation: put the object on
480 // the stack, and start executing the BCO.
481 INTERP_TICK(it_retto_BCO);
482 obj = (StgClosure*)Sp[offset+1];
483 ASSERT(get_itbl(obj)->type == BCO);
484 goto run_BCO_return_unboxed;
488 // Can't handle this return address; yield to scheduler
489 INTERP_TICK(it_retto_other);
490 IF_DEBUG(interpreter,
491 fprintf(stderr, "returning to unknown frame -- yielding to sched\n");
492 printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
494 RETURN_TO_SCHEDULER(ThreadRunGHC, ThreadYielding);
501 // -------------------------------------------------------------------------
505 // we have a function to apply (obj), and n arguments taking up m
506 // words on the stack. The info table (stg_ap_pp_info or whatever)
507 // is on top of the arguments on the stack.
509 switch (get_itbl(obj)->type) {
517 // we only cope with PAPs whose function is a BCO
518 if (get_itbl(pap->fun)->type != BCO) {
519 goto defer_apply_to_sched;
526 // n must be greater than 1, and the only kinds of
527 // application we support with more than one argument
528 // are all pointers...
530 // Shuffle the args for this function down, and put
531 // the appropriate info table in the gap.
532 for (i = 0; i < arity; i++) {
535 Sp[arity-1] = app_ptrs_itbl[n-arity-1];
537 // unpack the PAP's arguments onto the stack
539 for (i = 0; i < pap->n_args; i++) {
540 Sp[i] = (W_)pap->payload[i];
545 else if (arity == n) {
547 for (i = 0; i < pap->n_args; i++) {
548 Sp[i] = (W_)pap->payload[i];
553 else /* arity > n */ {
554 // build a new PAP and return it.
557 size = PAP_sizeW(pap->n_args + m);
558 new_pap = (StgPAP *)allocate(size);
559 SET_HDR(new_pap,&stg_PAP_info,CCCS);
560 new_pap->arity = pap->arity - n;
561 new_pap->n_args = pap->n_args + m;
562 new_pap->fun = pap->fun;
563 for (i = 0; i < pap->n_args; i++) {
564 new_pap->payload[i] = pap->payload[i];
566 for (i = 0; i < m; i++) {
567 new_pap->payload[pap->n_args + i] = (StgClosure *)Sp[i];
569 obj = (StgClosure *)new_pap;
579 arity = ((StgBCO *)obj)->arity;
582 // n must be greater than 1, and the only kinds of
583 // application we support with more than one argument
584 // are all pointers...
586 // Shuffle the args for this function down, and put
587 // the appropriate info table in the gap.
588 for (i = 0; i < arity; i++) {
591 Sp[arity-1] = app_ptrs_itbl[n-arity-1];
595 else if (arity == n) {
598 else /* arity > n */ {
599 // build a PAP and return it.
603 pap = (StgPAP *)allocate(size);
604 SET_HDR(pap, &stg_PAP_info,CCCS);
605 pap->arity = arity - n;
608 for (i = 0; i < m; i++) {
609 pap->payload[i] = (StgClosure *)Sp[i];
611 obj = (StgClosure *)pap;
617 // No point in us applying machine-code functions
619 defer_apply_to_sched:
622 Sp[0] = (W_)&stg_enter_info;
623 RETURN_TO_SCHEDULER(ThreadRunGHC, ThreadYielding);
626 // ------------------------------------------------------------------------
627 // Ok, we now have a bco (obj), and its arguments are all on the
628 // stack. We can start executing the byte codes.
630 // The stack is in one of two states. First, if this BCO is a
640 // Second, if this BCO is a continuation:
655 // where retval is the value being returned to this continuation.
656 // In the event of a stack check, heap check, or context switch,
657 // we need to leave the stack in a sane state so the garbage
658 // collector can find all the pointers.
660 // (1) BCO is a function: the BCO's bitmap describes the
661 // pointerhood of the arguments.
663 // (2) BCO is a continuation: BCO's bitmap describes the
664 // pointerhood of the free variables.
666 // Sadly we have three different kinds of stack/heap/cswitch check
671 if (doYouWantToGC()) {
672 Sp--; Sp[0] = (W_)&stg_enter_info;
673 RETURN_TO_SCHEDULER(ThreadInterpret, HeapOverflow);
675 // Stack checks aren't necessary at return points, the stack use
676 // is aggregated into the enclosing function entry point.
679 run_BCO_return_unboxed:
681 if (doYouWantToGC()) {
682 RETURN_TO_SCHEDULER(ThreadInterpret, HeapOverflow);
684 // Stack checks aren't necessary at return points, the stack use
685 // is aggregated into the enclosing function entry point.
692 Sp[0] = (W_)&stg_apply_interp_info;
693 checkStackChunk(Sp,SpLim);
698 if (doYouWantToGC()) {
701 Sp[0] = (W_)&stg_apply_interp_info; // placeholder, really
702 RETURN_TO_SCHEDULER(ThreadInterpret, HeapOverflow);
706 if (Sp - INTERP_STACK_CHECK_THRESH < SpLim) {
709 Sp[0] = (W_)&stg_apply_interp_info; // placeholder, really
710 RETURN_TO_SCHEDULER(ThreadInterpret, StackOverflow);
714 // Now, actually interpret the BCO... (no returning to the
715 // scheduler again until the stack is in an orderly state).
717 INTERP_TICK(it_BCO_entries);
719 register int bciPtr = 1; /* instruction pointer */
720 register StgBCO* bco = (StgBCO*)obj;
721 register StgWord16* instrs = (StgWord16*)(bco->instrs->payload);
722 register StgWord* literals = (StgWord*)(&bco->literals->payload[0]);
723 register StgPtr* ptrs = (StgPtr*)(&bco->ptrs->payload[0]);
724 register StgInfoTable** itbls = (StgInfoTable**)
725 (&bco->itbls->payload[0]);
728 it_lastopc = 0; /* no opcode */
732 ASSERT(bciPtr <= instrs[0]);
733 IF_DEBUG(interpreter,
734 //if (do_print_stack) {
735 //fprintf(stderr, "\n-- BEGIN stack\n");
736 //printStack(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size,iSu);
737 //fprintf(stderr, "-- END stack\n\n");
739 fprintf(stderr,"Sp = %p pc = %d ", Sp, bciPtr);
740 disInstr(bco,bciPtr);
742 fprintf(stderr,"\n");
743 for (i = 8; i >= 0; i--) {
744 fprintf(stderr, "%d %p\n", i, (StgPtr)(*(Sp+i)));
746 fprintf(stderr,"\n");
748 //if (do_print_stack) checkStack(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size,iSu);
751 INTERP_TICK(it_insns);
754 ASSERT( (int)instrs[bciPtr] >= 0 && (int)instrs[bciPtr] < 27 );
755 it_ofreq[ (int)instrs[bciPtr] ] ++;
756 it_oofreq[ it_lastopc ][ (int)instrs[bciPtr] ] ++;
757 it_lastopc = (int)instrs[bciPtr];
763 // Explicit stack check at the beginning of a function
764 // *only* (stack checks in case alternatives are
765 // propagated to the enclosing function).
766 int stk_words_reqd = BCO_NEXT + 1;
767 if (Sp - stk_words_reqd < SpLim) {
770 Sp[0] = (W_)&stg_apply_interp_info;
771 RETURN_TO_SCHEDULER(ThreadInterpret, StackOverflow);
806 Sp[-1] = BCO_PTR(o1);
811 case bci_PUSH_ALTS: {
812 int o_bco = BCO_NEXT;
813 Sp[-2] = (W_)&stg_ctoi_ret_R1p_info;
814 Sp[-1] = BCO_PTR(o_bco);
819 case bci_PUSH_ALTS_P: {
820 int o_bco = BCO_NEXT;
821 Sp[-2] = (W_)&stg_ctoi_ret_R1unpt_info;
822 Sp[-1] = BCO_PTR(o_bco);
827 case bci_PUSH_ALTS_N: {
828 int o_bco = BCO_NEXT;
829 Sp[-2] = (W_)&stg_ctoi_ret_R1n_info;
830 Sp[-1] = BCO_PTR(o_bco);
835 case bci_PUSH_ALTS_F: {
836 int o_bco = BCO_NEXT;
837 Sp[-2] = (W_)&stg_ctoi_ret_F1_info;
838 Sp[-1] = BCO_PTR(o_bco);
843 case bci_PUSH_ALTS_D: {
844 int o_bco = BCO_NEXT;
845 Sp[-2] = (W_)&stg_ctoi_ret_D1_info;
846 Sp[-1] = BCO_PTR(o_bco);
851 case bci_PUSH_ALTS_L: {
852 int o_bco = BCO_NEXT;
853 Sp[-2] = (W_)&stg_ctoi_ret_L1_info;
854 Sp[-1] = BCO_PTR(o_bco);
859 case bci_PUSH_ALTS_V: {
860 int o_bco = BCO_NEXT;
861 Sp[-2] = (W_)&stg_ctoi_ret_V_info;
862 Sp[-1] = BCO_PTR(o_bco);
867 case bci_PUSH_APPLY_N:
868 Sp--; Sp[0] = (W_)&stg_ap_n_info;
870 case bci_PUSH_APPLY_V:
871 Sp--; Sp[0] = (W_)&stg_ap_v_info;
873 case bci_PUSH_APPLY_F:
874 Sp--; Sp[0] = (W_)&stg_ap_f_info;
876 case bci_PUSH_APPLY_D:
877 Sp--; Sp[0] = (W_)&stg_ap_d_info;
879 case bci_PUSH_APPLY_L:
880 Sp--; Sp[0] = (W_)&stg_ap_l_info;
882 case bci_PUSH_APPLY_P:
883 Sp--; Sp[0] = (W_)&stg_ap_p_info;
885 case bci_PUSH_APPLY_PP:
886 Sp--; Sp[0] = (W_)&stg_ap_pp_info;
888 case bci_PUSH_APPLY_PPP:
889 Sp--; Sp[0] = (W_)&stg_ap_ppp_info;
891 case bci_PUSH_APPLY_PPPP:
892 Sp--; Sp[0] = (W_)&stg_ap_pppp_info;
894 case bci_PUSH_APPLY_PPPPP:
895 Sp--; Sp[0] = (W_)&stg_ap_ppppp_info;
897 case bci_PUSH_APPLY_PPPPPP:
898 Sp--; Sp[0] = (W_)&stg_ap_pppppp_info;
900 case bci_PUSH_APPLY_PPPPPPP:
901 Sp--; Sp[0] = (W_)&stg_ap_ppppppp_info;
906 int o_lits = BCO_NEXT;
907 int n_words = BCO_NEXT;
909 for (i = 0; i < n_words; i++) {
910 Sp[i] = BCO_LIT(o_lits+i);
918 /* a_1, .. a_n, b_1, .. b_by, s => a_1, .. a_n, s */
923 INTERP_TICK(it_slides);
929 int n_payload = BCO_NEXT;
930 int request = PAP_sizeW(n_payload);
931 ap = (StgAP*)allocate_UPD(request);
933 ap->n_args = n_payload;
934 SET_HDR(ap, &stg_AP_info, CCS_SYSTEM/*ToDo*/)
939 case bci_ALLOC_PAP: {
941 int arity = BCO_NEXT;
942 int n_payload = BCO_NEXT;
943 int request = PAP_sizeW(n_payload);
944 pap = (StgPAP*)allocate_NONUPD(request);
946 pap->n_args = n_payload;
948 SET_HDR(pap, &stg_PAP_info, CCS_SYSTEM/*ToDo*/)
955 int stkoff = BCO_NEXT;
956 int n_payload = BCO_NEXT;
957 StgAP* ap = (StgAP*)Sp[stkoff];
958 ASSERT((int)ap->n_args == n_payload);
959 ap->fun = (StgClosure*)Sp[0];
961 // The function should be a BCO, and its bitmap should
962 // cover the payload of the AP correctly.
963 ASSERT(get_itbl(ap->fun)->type == BCO
964 && (get_itbl(ap)->type == PAP ||
965 BCO_BITMAP_SIZE(ap->fun) == ap->n_args));
967 for (i = 0; i < n_payload; i++)
968 ap->payload[i] = (StgClosure*)Sp[i+1];
970 IF_DEBUG(interpreter,
971 fprintf(stderr,"\tBuilt ");
972 printObj((StgClosure*)ap);
978 /* Unpack N ptr words from t.o.s constructor */
980 int n_words = BCO_NEXT;
981 StgClosure* con = (StgClosure*)Sp[0];
983 for (i = 0; i < n_words; i++) {
984 Sp[i] = (W_)con->payload[i];
991 int o_itbl = BCO_NEXT;
992 int n_words = BCO_NEXT;
993 StgInfoTable* itbl = INFO_PTR_TO_STRUCT(BCO_ITBL(o_itbl));
994 int request = CONSTR_sizeW( itbl->layout.payload.ptrs,
995 itbl->layout.payload.nptrs );
996 StgClosure* con = (StgClosure*)allocate_NONUPD(request);
997 ASSERT( itbl->layout.payload.ptrs + itbl->layout.payload.nptrs > 0);
998 SET_HDR(con, BCO_ITBL(o_itbl), CCS_SYSTEM/*ToDo*/);
999 for (i = 0; i < n_words; i++) {
1000 con->payload[i] = (StgClosure*)Sp[i];
1005 IF_DEBUG(interpreter,
1006 fprintf(stderr,"\tBuilt ");
1007 printObj((StgClosure*)con);
1012 case bci_TESTLT_P: {
1013 int discr = BCO_NEXT;
1014 int failto = BCO_NEXT;
1015 StgClosure* con = (StgClosure*)Sp[0];
1016 if (constrTag(con) >= discr) {
1022 case bci_TESTEQ_P: {
1023 int discr = BCO_NEXT;
1024 int failto = BCO_NEXT;
1025 StgClosure* con = (StgClosure*)Sp[0];
1026 if (constrTag(con) != discr) {
1032 case bci_TESTLT_I: {
1033 // There should be an Int at Sp[1], and an info table at Sp[0].
1034 int discr = BCO_NEXT;
1035 int failto = BCO_NEXT;
1036 I_ stackInt = (I_)Sp[1];
1037 if (stackInt >= (I_)BCO_LIT(discr))
1042 case bci_TESTEQ_I: {
1043 // There should be an Int at Sp[1], and an info table at Sp[0].
1044 int discr = BCO_NEXT;
1045 int failto = BCO_NEXT;
1046 I_ stackInt = (I_)Sp[1];
1047 if (stackInt != (I_)BCO_LIT(discr)) {
1053 case bci_TESTLT_D: {
1054 // There should be a Double at Sp[1], and an info table at Sp[0].
1055 int discr = BCO_NEXT;
1056 int failto = BCO_NEXT;
1057 StgDouble stackDbl, discrDbl;
1058 stackDbl = PK_DBL( & Sp[1] );
1059 discrDbl = PK_DBL( & BCO_LIT(discr) );
1060 if (stackDbl >= discrDbl) {
1066 case bci_TESTEQ_D: {
1067 // There should be a Double at Sp[1], and an info table at Sp[0].
1068 int discr = BCO_NEXT;
1069 int failto = BCO_NEXT;
1070 StgDouble stackDbl, discrDbl;
1071 stackDbl = PK_DBL( & Sp[1] );
1072 discrDbl = PK_DBL( & BCO_LIT(discr) );
1073 if (stackDbl != discrDbl) {
1079 case bci_TESTLT_F: {
1080 // There should be a Float at Sp[1], and an info table at Sp[0].
1081 int discr = BCO_NEXT;
1082 int failto = BCO_NEXT;
1083 StgFloat stackFlt, discrFlt;
1084 stackFlt = PK_FLT( & Sp[1] );
1085 discrFlt = PK_FLT( & BCO_LIT(discr) );
1086 if (stackFlt >= discrFlt) {
1092 case bci_TESTEQ_F: {
1093 // There should be a Float at Sp[1], and an info table at Sp[0].
1094 int discr = BCO_NEXT;
1095 int failto = BCO_NEXT;
1096 StgFloat stackFlt, discrFlt;
1097 stackFlt = PK_FLT( & Sp[1] );
1098 discrFlt = PK_FLT( & BCO_LIT(discr) );
1099 if (stackFlt != discrFlt) {
1105 // Control-flow ish things
1107 // Context-switch check. We put it here to ensure that
1108 // the interpreter has done at least *some* work before
1109 // context switching: sometimes the scheduler can invoke
1110 // the interpreter with context_switch == 1, particularly
1111 // if the -C0 flag has been given on the cmd line.
1112 if (context_switch) {
1113 Sp--; Sp[0] = (W_)&stg_enter_info;
1114 RETURN_TO_SCHEDULER(ThreadInterpret, ThreadYielding);
1119 obj = (StgClosure *)Sp[0];
1125 Sp[0] = (W_)&stg_gc_unpt_r1_info;
1126 goto do_return_unboxed;
1129 Sp[0] = (W_)&stg_gc_unbx_r1_info;
1130 goto do_return_unboxed;
1133 Sp[0] = (W_)&stg_gc_f1_info;
1134 goto do_return_unboxed;
1137 Sp[0] = (W_)&stg_gc_d1_info;
1138 goto do_return_unboxed;
1141 Sp[0] = (W_)&stg_gc_l1_info;
1142 goto do_return_unboxed;
1145 Sp[0] = (W_)&stg_gc_void_info;
1146 goto do_return_unboxed;
1149 int stkoff = BCO_NEXT;
1150 signed short n = (signed short)(BCO_NEXT);
1151 Sp[stkoff] += (W_)n;
1157 int stk_offset = BCO_NEXT;
1158 int o_itbl = BCO_NEXT;
1159 void(*marshall_fn)(void*) = (void (*)(void*))BCO_LIT(o_itbl);
1161 #ifdef RTS_SUPPORTS_THREADS
1163 // Arguments on the TSO stack are not good, because garbage
1164 // collection might move the TSO as soon as we call
1165 // suspendThread below.
1167 W_ arguments[stk_offset];
1169 memcpy(arguments, Sp, sizeof(W_) * stk_offset);
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.
1182 Sp -= RET_DYN_SIZE + sizeofW(StgRetDyn);
1183 ((StgRetDyn *)Sp)->liveness = ALL_NON_PTRS | N_NONPTRS(stk_offset);
1184 ((StgRetDyn *)Sp)->info = (StgInfoTable *)&stg_gc_gen_info;
1186 SAVE_STACK_POINTERS;
1187 tok = suspendThread(&cap->r,rtsFalse);
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 + sizeofW(StgRetDyn)) );
1198 // We already made a malloced copy of the arguments above.
1200 marshall_fn ( arguments );
1203 // And restart the thread again, popping the RET_DYN frame.
1204 cap = (Capability *)((void *)resumeThread(tok,rtsFalse) - sizeof(StgFunTable));
1205 LOAD_STACK_POINTERS;
1206 Sp += RET_DYN_SIZE + sizeofW(StgRetDyn);
1209 #ifdef RTS_SUPPORTS_THREADS
1211 // Copy the "arguments", which might include a return value,
1212 // back to the TSO stack. It would of course be enough to
1213 // just copy the return value, but we don't know the offset.
1214 memcpy(Sp, arguments, sizeof(W_) * stk_offset);
1221 /* BCO_NEXT modifies bciPtr, so be conservative. */
1222 int nextpc = BCO_NEXT;
1228 barf("interpretBCO: hit a CASEFAIL");
1232 barf("interpretBCO: unknown or unimplemented opcode");
1234 } /* switch on opcode */
1238 barf("interpretBCO: fell off end of the interpreter");