1 /* -----------------------------------------------------------------------------
4 * Copyright (c) The GHC Team, 1994-2002.
5 * ---------------------------------------------------------------------------*/
7 #include "PosixSource.h"
16 #include "LdvProfile.h"
21 #include "Bytecodes.h"
23 #include "Disassembler.h"
24 #include "Interpreter.h"
26 #include <string.h> /* for memcpy */
32 /* --------------------------------------------------------------------------
33 * The bytecode interpreter
34 * ------------------------------------------------------------------------*/
36 /* Gather stats about entry, opcode, opcode-pair frequencies. For
37 tuning the interpreter. */
39 /* #define INTERP_STATS */
42 /* Sp points to the lowest live word on the stack. */
44 #define BCO_NEXT instrs[bciPtr++]
45 #define BCO_PTR(n) (W_)ptrs[n]
46 #define BCO_LIT(n) literals[n]
47 #define BCO_ITBL(n) itbls[n]
49 #define LOAD_STACK_POINTERS \
50 Sp = cap->r.rCurrentTSO->sp; \
51 /* We don't change this ... */ \
52 SpLim = cap->r.rCurrentTSO->stack + RESERVED_STACK_WORDS;
54 #define SAVE_STACK_POINTERS \
55 cap->r.rCurrentTSO->sp = Sp
57 #define RETURN_TO_SCHEDULER(todo,retcode) \
58 SAVE_STACK_POINTERS; \
59 cap->r.rCurrentTSO->what_next = (todo); \
60 threadPaused(cap->r.rCurrentTSO); \
61 cap->r.rRet = (retcode); \
64 #define RETURN_TO_SCHEDULER_NO_PAUSE(todo,retcode) \
65 SAVE_STACK_POINTERS; \
66 cap->r.rCurrentTSO->what_next = (todo); \
67 cap->r.rRet = (retcode); \
72 allocate_UPD (int n_words)
74 return allocate(stg_max(sizeofW(StgHeader)+MIN_UPD_SIZE, n_words));
78 allocate_NONUPD (int n_words)
80 return allocate(stg_max(sizeofW(StgHeader)+MIN_NONUPD_SIZE, n_words));
86 /* Hacky stats, for tuning the interpreter ... */
87 int it_unknown_entries[N_CLOSURE_TYPES];
88 int it_total_unknown_entries;
100 int it_oofreq[27][27];
103 #define INTERP_TICK(n) (n)++
105 void interp_startup ( void )
108 it_retto_BCO = it_retto_UPDATE = it_retto_other = 0;
109 it_total_entries = it_total_unknown_entries = 0;
110 for (i = 0; i < N_CLOSURE_TYPES; i++)
111 it_unknown_entries[i] = 0;
112 it_slides = it_insns = it_BCO_entries = 0;
113 for (i = 0; i < 27; i++) it_ofreq[i] = 0;
114 for (i = 0; i < 27; i++)
115 for (j = 0; j < 27; j++)
120 void interp_shutdown ( void )
122 int i, j, k, o_max, i_max, j_max;
123 debugBelch("%d constrs entered -> (%d BCO, %d UPD, %d ??? )\n",
124 it_retto_BCO + it_retto_UPDATE + it_retto_other,
125 it_retto_BCO, it_retto_UPDATE, it_retto_other );
126 debugBelch("%d total entries, %d unknown entries \n",
127 it_total_entries, it_total_unknown_entries);
128 for (i = 0; i < N_CLOSURE_TYPES; i++) {
129 if (it_unknown_entries[i] == 0) continue;
130 debugBelch(" type %2d: unknown entries (%4.1f%%) == %d\n",
131 i, 100.0 * ((double)it_unknown_entries[i]) /
132 ((double)it_total_unknown_entries),
133 it_unknown_entries[i]);
135 debugBelch("%d insns, %d slides, %d BCO_entries\n",
136 it_insns, it_slides, it_BCO_entries);
137 for (i = 0; i < 27; i++)
138 debugBelch("opcode %2d got %d\n", i, it_ofreq[i] );
140 for (k = 1; k < 20; k++) {
143 for (i = 0; i < 27; i++) {
144 for (j = 0; j < 27; j++) {
145 if (it_oofreq[i][j] > o_max) {
146 o_max = it_oofreq[i][j];
147 i_max = i; j_max = j;
152 debugBelch("%d: count (%4.1f%%) %6d is %d then %d\n",
153 k, ((double)o_max) * 100.0 / ((double)it_insns), o_max,
155 it_oofreq[i_max][j_max] = 0;
160 #else // !INTERP_STATS
162 #define INTERP_TICK(n) /* nothing */
166 static StgWord app_ptrs_itbl[] = {
169 (W_)&stg_ap_ppp_info,
170 (W_)&stg_ap_pppp_info,
171 (W_)&stg_ap_ppppp_info,
172 (W_)&stg_ap_pppppp_info,
176 interpretBCO (Capability* cap)
178 // Use of register here is primarily to make it clear to compilers
179 // that these entities are non-aliasable.
180 register StgPtr Sp; // local state -- stack pointer
181 register StgPtr SpLim; // local state -- stack lim pointer
182 register StgClosure* obj;
187 // ------------------------------------------------------------------------
190 // We have a closure to evaluate. Stack looks like:
194 // Sp | -------------------> closure
197 if (Sp[0] == (W_)&stg_enter_info) {
202 // ------------------------------------------------------------------------
205 // We have a BCO application to perform. Stack looks like:
216 else if (Sp[0] == (W_)&stg_apply_interp_info) {
217 obj = (StgClosure *)Sp[1];
222 // ------------------------------------------------------------------------
225 // We have an unboxed value to return. See comment before
226 // do_return_unboxed, below.
229 goto do_return_unboxed;
232 // Evaluate the object on top of the stack.
234 obj = (StgClosure*)Sp[0]; Sp++;
237 INTERP_TICK(it_total_evals);
239 IF_DEBUG(interpreter,
241 "\n---------------------------------------------------------------\n");
242 debugBelch("Evaluating: "); printObj(obj);
243 debugBelch("Sp = %p\n", Sp);
246 printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
250 IF_DEBUG(sanity,checkStackChunk(Sp, cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size));
252 switch ( get_itbl(obj)->type ) {
257 case IND_OLDGEN_PERM:
260 obj = ((StgInd*)obj)->indirectee;
271 case CONSTR_CHARLIKE:
273 case CONSTR_NOCAF_STATIC:
286 ASSERT(((StgBCO *)obj)->arity > 0);
289 case AP: /* Copied from stg_AP_entry. */
298 if (Sp - (words+sizeofW(StgUpdateFrame)) < SpLim) {
301 Sp[0] = (W_)&stg_enter_info;
302 RETURN_TO_SCHEDULER(ThreadInterpret, StackOverflow);
305 /* Ok; we're safe. Party on. Push an update frame. */
306 Sp -= sizeofW(StgUpdateFrame);
308 StgUpdateFrame *__frame;
309 __frame = (StgUpdateFrame *)Sp;
310 SET_INFO(__frame, (StgInfoTable *)&stg_upd_frame_info);
311 __frame->updatee = (StgClosure *)(ap);
314 /* Reload the stack */
316 for (i=0; i < words; i++) {
317 Sp[i] = (W_)ap->payload[i];
320 obj = (StgClosure*)ap->fun;
321 ASSERT(get_itbl(obj)->type == BCO);
330 j = get_itbl(obj)->type;
331 ASSERT(j >= 0 && j < N_CLOSURE_TYPES);
332 it_unknown_entries[j]++;
333 it_total_unknown_entries++;
337 // Can't handle this object; yield to scheduler
338 IF_DEBUG(interpreter,
339 debugBelch("evaluating unknown closure -- yielding to sched\n");
344 Sp[0] = (W_)&stg_enter_info;
345 RETURN_TO_SCHEDULER_NO_PAUSE(ThreadRunGHC, ThreadYielding);
349 // ------------------------------------------------------------------------
350 // We now have an evaluated object (obj). The next thing to
351 // do is return it to the stack frame on top of the stack.
353 ASSERT(closure_HNF(obj));
355 IF_DEBUG(interpreter,
357 "\n---------------------------------------------------------------\n");
358 debugBelch("Returning: "); printObj(obj);
359 debugBelch("Sp = %p\n", Sp);
361 printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
365 IF_DEBUG(sanity,checkStackChunk(Sp, cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size));
367 switch (get_itbl((StgClosure *)Sp)->type) {
370 const StgInfoTable *info;
372 // NOTE: not using get_itbl().
373 info = ((StgClosure *)Sp)->header.info;
374 if (info == (StgInfoTable *)&stg_ap_v_info) {
375 n = 1; m = 0; goto do_apply;
377 if (info == (StgInfoTable *)&stg_ap_f_info) {
378 n = 1; m = 1; goto do_apply;
380 if (info == (StgInfoTable *)&stg_ap_d_info) {
381 n = 1; m = sizeofW(StgDouble); goto do_apply;
383 if (info == (StgInfoTable *)&stg_ap_l_info) {
384 n = 1; m = sizeofW(StgInt64); goto do_apply;
386 if (info == (StgInfoTable *)&stg_ap_n_info) {
387 n = 1; m = 1; goto do_apply;
389 if (info == (StgInfoTable *)&stg_ap_p_info) {
390 n = 1; m = 1; goto do_apply;
392 if (info == (StgInfoTable *)&stg_ap_pp_info) {
393 n = 2; m = 2; goto do_apply;
395 if (info == (StgInfoTable *)&stg_ap_ppp_info) {
396 n = 3; m = 3; goto do_apply;
398 if (info == (StgInfoTable *)&stg_ap_pppp_info) {
399 n = 4; m = 4; goto do_apply;
401 if (info == (StgInfoTable *)&stg_ap_ppppp_info) {
402 n = 5; m = 5; goto do_apply;
404 if (info == (StgInfoTable *)&stg_ap_pppppp_info) {
405 n = 6; m = 6; goto do_apply;
407 goto do_return_unrecognised;
411 // Returning to an update frame: do the update, pop the update
412 // frame, and continue with the next stack frame.
413 INTERP_TICK(it_retto_UPDATE);
414 UPD_IND(((StgUpdateFrame *)Sp)->updatee, obj);
415 Sp += sizeofW(StgUpdateFrame);
419 // Returning to an interpreted continuation: put the object on
420 // the stack, and start executing the BCO.
421 INTERP_TICK(it_retto_BCO);
424 obj = (StgClosure*)Sp[2];
425 ASSERT(get_itbl(obj)->type == BCO);
429 do_return_unrecognised:
431 // Can't handle this return address; yield to scheduler
432 INTERP_TICK(it_retto_other);
433 IF_DEBUG(interpreter,
434 debugBelch("returning to unknown frame -- yielding to sched\n");
435 printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
439 Sp[0] = (W_)&stg_enter_info;
440 RETURN_TO_SCHEDULER_NO_PAUSE(ThreadRunGHC, ThreadYielding);
444 // -------------------------------------------------------------------------
445 // Returning an unboxed value. The stack looks like this:
462 // where XXXX_info is one of the stg_gc_unbx_r1_info family.
464 // We're only interested in the case when the real return address
465 // is a BCO; otherwise we'll return to the scheduler.
471 ASSERT( Sp[0] == (W_)&stg_gc_unbx_r1_info
472 || Sp[0] == (W_)&stg_gc_unpt_r1_info
473 || Sp[0] == (W_)&stg_gc_f1_info
474 || Sp[0] == (W_)&stg_gc_d1_info
475 || Sp[0] == (W_)&stg_gc_l1_info
476 || Sp[0] == (W_)&stg_gc_void_info // VoidRep
479 // get the offset of the stg_ctoi_ret_XXX itbl
480 offset = stack_frame_sizeW((StgClosure *)Sp);
482 switch (get_itbl((StgClosure *)Sp+offset)->type) {
485 // Returning to an interpreted continuation: put the object on
486 // the stack, and start executing the BCO.
487 INTERP_TICK(it_retto_BCO);
488 obj = (StgClosure*)Sp[offset+1];
489 ASSERT(get_itbl(obj)->type == BCO);
490 goto run_BCO_return_unboxed;
494 // Can't handle this return address; yield to scheduler
495 INTERP_TICK(it_retto_other);
496 IF_DEBUG(interpreter,
497 debugBelch("returning to unknown frame -- yielding to sched\n");
498 printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
500 RETURN_TO_SCHEDULER_NO_PAUSE(ThreadRunGHC, ThreadYielding);
507 // -------------------------------------------------------------------------
511 // we have a function to apply (obj), and n arguments taking up m
512 // words on the stack. The info table (stg_ap_pp_info or whatever)
513 // is on top of the arguments on the stack.
515 switch (get_itbl(obj)->type) {
523 // we only cope with PAPs whose function is a BCO
524 if (get_itbl(pap->fun)->type != BCO) {
525 goto defer_apply_to_sched;
532 // n must be greater than 1, and the only kinds of
533 // application we support with more than one argument
534 // are all pointers...
536 // Shuffle the args for this function down, and put
537 // the appropriate info table in the gap.
538 for (i = 0; i < arity; i++) {
539 Sp[(int)i-1] = Sp[i];
540 // ^^^^^ careful, i-1 might be negative, but i in unsigned
542 Sp[arity-1] = app_ptrs_itbl[n-arity-1];
544 // unpack the PAP's arguments onto the stack
546 for (i = 0; i < pap->n_args; i++) {
547 Sp[i] = (W_)pap->payload[i];
552 else if (arity == n) {
554 for (i = 0; i < pap->n_args; i++) {
555 Sp[i] = (W_)pap->payload[i];
560 else /* arity > n */ {
561 // build a new PAP and return it.
564 size = PAP_sizeW(pap->n_args + m);
565 new_pap = (StgPAP *)allocate(size);
566 SET_HDR(new_pap,&stg_PAP_info,CCCS);
567 new_pap->arity = pap->arity - n;
568 new_pap->n_args = pap->n_args + m;
569 new_pap->fun = pap->fun;
570 for (i = 0; i < pap->n_args; i++) {
571 new_pap->payload[i] = pap->payload[i];
573 for (i = 0; i < m; i++) {
574 new_pap->payload[pap->n_args + i] = (StgClosure *)Sp[i];
576 obj = (StgClosure *)new_pap;
586 arity = ((StgBCO *)obj)->arity;
589 // n must be greater than 1, and the only kinds of
590 // application we support with more than one argument
591 // are all pointers...
593 // Shuffle the args for this function down, and put
594 // the appropriate info table in the gap.
595 for (i = 0; i < arity; i++) {
596 Sp[(int)i-1] = Sp[i];
597 // ^^^^^ careful, i-1 might be negative, but i in unsigned
599 Sp[arity-1] = app_ptrs_itbl[n-arity-1];
603 else if (arity == n) {
606 else /* arity > n */ {
607 // build a PAP and return it.
611 pap = (StgPAP *)allocate(size);
612 SET_HDR(pap, &stg_PAP_info,CCCS);
613 pap->arity = arity - n;
616 for (i = 0; i < m; i++) {
617 pap->payload[i] = (StgClosure *)Sp[i];
619 obj = (StgClosure *)pap;
625 // No point in us applying machine-code functions
627 defer_apply_to_sched:
630 Sp[0] = (W_)&stg_enter_info;
631 RETURN_TO_SCHEDULER_NO_PAUSE(ThreadRunGHC, ThreadYielding);
634 // ------------------------------------------------------------------------
635 // Ok, we now have a bco (obj), and its arguments are all on the
636 // stack. We can start executing the byte codes.
638 // The stack is in one of two states. First, if this BCO is a
648 // Second, if this BCO is a continuation:
663 // where retval is the value being returned to this continuation.
664 // In the event of a stack check, heap check, or context switch,
665 // we need to leave the stack in a sane state so the garbage
666 // collector can find all the pointers.
668 // (1) BCO is a function: the BCO's bitmap describes the
669 // pointerhood of the arguments.
671 // (2) BCO is a continuation: BCO's bitmap describes the
672 // pointerhood of the free variables.
674 // Sadly we have three different kinds of stack/heap/cswitch check
679 if (doYouWantToGC()) {
680 Sp--; Sp[0] = (W_)&stg_enter_info;
681 RETURN_TO_SCHEDULER(ThreadInterpret, HeapOverflow);
683 // Stack checks aren't necessary at return points, the stack use
684 // is aggregated into the enclosing function entry point.
687 run_BCO_return_unboxed:
689 if (doYouWantToGC()) {
690 RETURN_TO_SCHEDULER(ThreadInterpret, HeapOverflow);
692 // Stack checks aren't necessary at return points, the stack use
693 // is aggregated into the enclosing function entry point.
700 Sp[0] = (W_)&stg_apply_interp_info;
701 checkStackChunk(Sp,SpLim);
706 if (doYouWantToGC()) {
709 Sp[0] = (W_)&stg_apply_interp_info; // placeholder, really
710 RETURN_TO_SCHEDULER(ThreadInterpret, HeapOverflow);
714 if (Sp - INTERP_STACK_CHECK_THRESH < SpLim) {
717 Sp[0] = (W_)&stg_apply_interp_info; // placeholder, really
718 RETURN_TO_SCHEDULER(ThreadInterpret, StackOverflow);
722 // Now, actually interpret the BCO... (no returning to the
723 // scheduler again until the stack is in an orderly state).
725 INTERP_TICK(it_BCO_entries);
727 register int bciPtr = 1; /* instruction pointer */
728 register StgBCO* bco = (StgBCO*)obj;
729 register StgWord16* instrs = (StgWord16*)(bco->instrs->payload);
730 register StgWord* literals = (StgWord*)(&bco->literals->payload[0]);
731 register StgPtr* ptrs = (StgPtr*)(&bco->ptrs->payload[0]);
732 register StgInfoTable** itbls = (StgInfoTable**)
733 (&bco->itbls->payload[0]);
736 it_lastopc = 0; /* no opcode */
740 ASSERT(bciPtr <= instrs[0]);
741 IF_DEBUG(interpreter,
742 //if (do_print_stack) {
743 //debugBelch("\n-- BEGIN stack\n");
744 //printStack(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size,iSu);
745 //debugBelch("-- END stack\n\n");
747 debugBelch("Sp = %p pc = %d ", Sp, bciPtr);
748 disInstr(bco,bciPtr);
751 for (i = 8; i >= 0; i--) {
752 debugBelch("%d %p\n", i, (StgPtr)(*(Sp+i)));
756 //if (do_print_stack) checkStack(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size,iSu);
759 INTERP_TICK(it_insns);
762 ASSERT( (int)instrs[bciPtr] >= 0 && (int)instrs[bciPtr] < 27 );
763 it_ofreq[ (int)instrs[bciPtr] ] ++;
764 it_oofreq[ it_lastopc ][ (int)instrs[bciPtr] ] ++;
765 it_lastopc = (int)instrs[bciPtr];
771 // Explicit stack check at the beginning of a function
772 // *only* (stack checks in case alternatives are
773 // propagated to the enclosing function).
774 int stk_words_reqd = BCO_NEXT + 1;
775 if (Sp - stk_words_reqd < SpLim) {
778 Sp[0] = (W_)&stg_apply_interp_info;
779 RETURN_TO_SCHEDULER(ThreadInterpret, StackOverflow);
814 Sp[-1] = BCO_PTR(o1);
819 case bci_PUSH_ALTS: {
820 int o_bco = BCO_NEXT;
821 Sp[-2] = (W_)&stg_ctoi_R1p_info;
822 Sp[-1] = BCO_PTR(o_bco);
827 case bci_PUSH_ALTS_P: {
828 int o_bco = BCO_NEXT;
829 Sp[-2] = (W_)&stg_ctoi_R1unpt_info;
830 Sp[-1] = BCO_PTR(o_bco);
835 case bci_PUSH_ALTS_N: {
836 int o_bco = BCO_NEXT;
837 Sp[-2] = (W_)&stg_ctoi_R1n_info;
838 Sp[-1] = BCO_PTR(o_bco);
843 case bci_PUSH_ALTS_F: {
844 int o_bco = BCO_NEXT;
845 Sp[-2] = (W_)&stg_ctoi_F1_info;
846 Sp[-1] = BCO_PTR(o_bco);
851 case bci_PUSH_ALTS_D: {
852 int o_bco = BCO_NEXT;
853 Sp[-2] = (W_)&stg_ctoi_D1_info;
854 Sp[-1] = BCO_PTR(o_bco);
859 case bci_PUSH_ALTS_L: {
860 int o_bco = BCO_NEXT;
861 Sp[-2] = (W_)&stg_ctoi_L1_info;
862 Sp[-1] = BCO_PTR(o_bco);
867 case bci_PUSH_ALTS_V: {
868 int o_bco = BCO_NEXT;
869 Sp[-2] = (W_)&stg_ctoi_V_info;
870 Sp[-1] = BCO_PTR(o_bco);
875 case bci_PUSH_APPLY_N:
876 Sp--; Sp[0] = (W_)&stg_ap_n_info;
878 case bci_PUSH_APPLY_V:
879 Sp--; Sp[0] = (W_)&stg_ap_v_info;
881 case bci_PUSH_APPLY_F:
882 Sp--; Sp[0] = (W_)&stg_ap_f_info;
884 case bci_PUSH_APPLY_D:
885 Sp--; Sp[0] = (W_)&stg_ap_d_info;
887 case bci_PUSH_APPLY_L:
888 Sp--; Sp[0] = (W_)&stg_ap_l_info;
890 case bci_PUSH_APPLY_P:
891 Sp--; Sp[0] = (W_)&stg_ap_p_info;
893 case bci_PUSH_APPLY_PP:
894 Sp--; Sp[0] = (W_)&stg_ap_pp_info;
896 case bci_PUSH_APPLY_PPP:
897 Sp--; Sp[0] = (W_)&stg_ap_ppp_info;
899 case bci_PUSH_APPLY_PPPP:
900 Sp--; Sp[0] = (W_)&stg_ap_pppp_info;
902 case bci_PUSH_APPLY_PPPPP:
903 Sp--; Sp[0] = (W_)&stg_ap_ppppp_info;
905 case bci_PUSH_APPLY_PPPPPP:
906 Sp--; Sp[0] = (W_)&stg_ap_pppppp_info;
911 int o_lits = BCO_NEXT;
912 int n_words = BCO_NEXT;
914 for (i = 0; i < n_words; i++) {
915 Sp[i] = (W_)BCO_LIT(o_lits+i);
923 /* a_1, .. a_n, b_1, .. b_by, s => a_1, .. a_n, s */
928 INTERP_TICK(it_slides);
934 int n_payload = BCO_NEXT;
935 int request = PAP_sizeW(n_payload);
936 ap = (StgAP*)allocate_UPD(request);
938 ap->n_args = n_payload;
939 SET_HDR(ap, &stg_AP_info, CCS_SYSTEM/*ToDo*/)
944 case bci_ALLOC_PAP: {
946 int arity = BCO_NEXT;
947 int n_payload = BCO_NEXT;
948 int request = PAP_sizeW(n_payload);
949 pap = (StgPAP*)allocate_NONUPD(request);
951 pap->n_args = n_payload;
953 SET_HDR(pap, &stg_PAP_info, CCS_SYSTEM/*ToDo*/)
960 int stkoff = BCO_NEXT;
961 int n_payload = BCO_NEXT;
962 StgAP* ap = (StgAP*)Sp[stkoff];
963 ASSERT((int)ap->n_args == n_payload);
964 ap->fun = (StgClosure*)Sp[0];
966 // The function should be a BCO, and its bitmap should
967 // cover the payload of the AP correctly.
968 ASSERT(get_itbl(ap->fun)->type == BCO
969 && (get_itbl(ap)->type == PAP ||
970 BCO_BITMAP_SIZE(ap->fun) == ap->n_args));
972 for (i = 0; i < n_payload; i++)
973 ap->payload[i] = (StgClosure*)Sp[i+1];
975 IF_DEBUG(interpreter,
976 debugBelch("\tBuilt ");
977 printObj((StgClosure*)ap);
983 /* Unpack N ptr words from t.o.s constructor */
985 int n_words = BCO_NEXT;
986 StgClosure* con = (StgClosure*)Sp[0];
988 for (i = 0; i < n_words; i++) {
989 Sp[i] = (W_)con->payload[i];
996 int o_itbl = BCO_NEXT;
997 int n_words = BCO_NEXT;
998 StgInfoTable* itbl = INFO_PTR_TO_STRUCT(BCO_ITBL(o_itbl));
999 int request = CONSTR_sizeW( itbl->layout.payload.ptrs,
1000 itbl->layout.payload.nptrs );
1001 StgClosure* con = (StgClosure*)allocate_NONUPD(request);
1002 ASSERT( itbl->layout.payload.ptrs + itbl->layout.payload.nptrs > 0);
1003 SET_HDR(con, BCO_ITBL(o_itbl), CCS_SYSTEM/*ToDo*/);
1004 for (i = 0; i < n_words; i++) {
1005 con->payload[i] = (StgClosure*)Sp[i];
1010 IF_DEBUG(interpreter,
1011 debugBelch("\tBuilt ");
1012 printObj((StgClosure*)con);
1017 case bci_TESTLT_P: {
1018 unsigned int discr = BCO_NEXT;
1019 int failto = BCO_NEXT;
1020 StgClosure* con = (StgClosure*)Sp[0];
1021 if (GET_TAG(con) >= discr) {
1027 case bci_TESTEQ_P: {
1028 unsigned int discr = BCO_NEXT;
1029 int failto = BCO_NEXT;
1030 StgClosure* con = (StgClosure*)Sp[0];
1031 if (GET_TAG(con) != discr) {
1037 case bci_TESTLT_I: {
1038 // There should be an Int at Sp[1], and an info table at Sp[0].
1039 int discr = BCO_NEXT;
1040 int failto = BCO_NEXT;
1041 I_ stackInt = (I_)Sp[1];
1042 if (stackInt >= (I_)BCO_LIT(discr))
1047 case bci_TESTEQ_I: {
1048 // There should be an Int at Sp[1], and an info table at Sp[0].
1049 int discr = BCO_NEXT;
1050 int failto = BCO_NEXT;
1051 I_ stackInt = (I_)Sp[1];
1052 if (stackInt != (I_)BCO_LIT(discr)) {
1058 case bci_TESTLT_D: {
1059 // There should be a Double at Sp[1], and an info table at Sp[0].
1060 int discr = BCO_NEXT;
1061 int failto = BCO_NEXT;
1062 StgDouble stackDbl, discrDbl;
1063 stackDbl = PK_DBL( & Sp[1] );
1064 discrDbl = PK_DBL( & BCO_LIT(discr) );
1065 if (stackDbl >= discrDbl) {
1071 case bci_TESTEQ_D: {
1072 // There should be a Double at Sp[1], and an info table at Sp[0].
1073 int discr = BCO_NEXT;
1074 int failto = BCO_NEXT;
1075 StgDouble stackDbl, discrDbl;
1076 stackDbl = PK_DBL( & Sp[1] );
1077 discrDbl = PK_DBL( & BCO_LIT(discr) );
1078 if (stackDbl != discrDbl) {
1084 case bci_TESTLT_F: {
1085 // There should be a Float at Sp[1], and an info table at Sp[0].
1086 int discr = BCO_NEXT;
1087 int failto = BCO_NEXT;
1088 StgFloat stackFlt, discrFlt;
1089 stackFlt = PK_FLT( & Sp[1] );
1090 discrFlt = PK_FLT( & BCO_LIT(discr) );
1091 if (stackFlt >= discrFlt) {
1097 case bci_TESTEQ_F: {
1098 // There should be a Float at Sp[1], and an info table at Sp[0].
1099 int discr = BCO_NEXT;
1100 int failto = BCO_NEXT;
1101 StgFloat stackFlt, discrFlt;
1102 stackFlt = PK_FLT( & Sp[1] );
1103 discrFlt = PK_FLT( & BCO_LIT(discr) );
1104 if (stackFlt != discrFlt) {
1110 // Control-flow ish things
1112 // Context-switch check. We put it here to ensure that
1113 // the interpreter has done at least *some* work before
1114 // context switching: sometimes the scheduler can invoke
1115 // the interpreter with context_switch == 1, particularly
1116 // if the -C0 flag has been given on the cmd line.
1117 if (context_switch) {
1118 Sp--; Sp[0] = (W_)&stg_enter_info;
1119 RETURN_TO_SCHEDULER(ThreadInterpret, ThreadYielding);
1124 obj = (StgClosure *)Sp[0];
1130 Sp[0] = (W_)&stg_gc_unpt_r1_info;
1131 goto do_return_unboxed;
1134 Sp[0] = (W_)&stg_gc_unbx_r1_info;
1135 goto do_return_unboxed;
1138 Sp[0] = (W_)&stg_gc_f1_info;
1139 goto do_return_unboxed;
1142 Sp[0] = (W_)&stg_gc_d1_info;
1143 goto do_return_unboxed;
1146 Sp[0] = (W_)&stg_gc_l1_info;
1147 goto do_return_unboxed;
1150 Sp[0] = (W_)&stg_gc_void_info;
1151 goto do_return_unboxed;
1154 int stkoff = BCO_NEXT;
1155 signed short n = (signed short)(BCO_NEXT);
1156 Sp[stkoff] += (W_)n;
1162 int stk_offset = BCO_NEXT;
1163 int o_itbl = BCO_NEXT;
1164 void(*marshall_fn)(void*) = (void (*)(void*))BCO_LIT(o_itbl);
1166 RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE
1167 + sizeofW(StgRetDyn);
1171 // Arguments on the TSO stack are not good, because garbage
1172 // collection might move the TSO as soon as we call
1173 // suspendThread below.
1175 W_ arguments[stk_offset];
1177 memcpy(arguments, Sp, sizeof(W_) * stk_offset);
1180 // Restore the Haskell thread's current value of errno
1181 errno = cap->r.rCurrentTSO->saved_errno;
1183 // There are a bunch of non-ptr words on the stack (the
1184 // ccall args, the ccall fun address and space for the
1185 // result), which we need to cover with an info table
1186 // since we might GC during this call.
1188 // We know how many (non-ptr) words there are before the
1189 // next valid stack frame: it is the stk_offset arg to the
1190 // CCALL instruction. So we build a RET_DYN stack frame
1191 // on the stack frame to describe this chunk of stack.
1194 ((StgRetDyn *)Sp)->liveness = NO_PTRS | N_NONPTRS(stk_offset);
1195 ((StgRetDyn *)Sp)->info = (StgInfoTable *)&stg_gc_gen_info;
1197 SAVE_STACK_POINTERS;
1198 tok = suspendThread(&cap->r);
1200 #ifndef THREADED_RTS
1202 // suspendThread might have shifted the stack
1203 // around (stack squeezing), so we have to grab the real
1204 // Sp out of the TSO to find the ccall args again.
1206 marshall_fn ( (void*)(cap->r.rCurrentTSO->sp + ret_dyn_size) );
1209 // We already made a copy of the arguments above.
1211 marshall_fn ( arguments );
1214 // And restart the thread again, popping the RET_DYN frame.
1215 cap = (Capability *)((void *)((unsigned char*)resumeThread(tok) - sizeof(StgFunTable)));
1216 LOAD_STACK_POINTERS;
1219 // Save the Haskell thread's current value of errno
1220 cap->r.rCurrentTSO->saved_errno = errno;
1224 // Copy the "arguments", which might include a return value,
1225 // back to the TSO stack. It would of course be enough to
1226 // just copy the return value, but we don't know the offset.
1227 memcpy(Sp, arguments, sizeof(W_) * stk_offset);
1234 /* BCO_NEXT modifies bciPtr, so be conservative. */
1235 int nextpc = BCO_NEXT;
1241 barf("interpretBCO: hit a CASEFAIL");
1245 barf("interpretBCO: unknown or unimplemented opcode");
1247 } /* switch on opcode */
1251 barf("interpretBCO: fell off end of the interpreter");