1 /* -----------------------------------------------------------------------------
4 * Copyright (c) The GHC Team, 1994-2002.
5 * ---------------------------------------------------------------------------*/
7 #include "PosixSource.h"
15 #include "LdvProfile.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_NEXT_32 (bciPtr += 2, (((StgWord) instrs[bciPtr-2]) << 16) + ((StgWord) instrs[bciPtr-1]))
45 #define BCO_NEXT_64 (bciPtr += 4, (((StgWord) instrs[bciPtr-4]) << 48) + (((StgWord) instrs[bciPtr-3]) << 32) + (((StgWord) instrs[bciPtr-2]) << 16) + ((StgWord) instrs[bciPtr-1]))
46 #if WORD_SIZE_IN_BITS == 32
47 #define BCO_NEXT_WORD BCO_NEXT_32
48 #elif WORD_SIZE_IN_BITS == 64
49 #define BCO_NEXT_WORD BCO_NEXT_64
51 #error Cannot cope with WORD_SIZE_IN_BITS being nether 32 nor 64
53 #define BCO_GET_LARGE_ARG ((bci & bci_FLAG_LARGE_ARGS) ? BCO_NEXT_WORD : BCO_NEXT)
55 #define BCO_PTR(n) (W_)ptrs[n]
56 #define BCO_LIT(n) literals[n]
58 #define LOAD_STACK_POINTERS \
59 Sp = cap->r.rCurrentTSO->sp; \
60 /* We don't change this ... */ \
61 SpLim = cap->r.rCurrentTSO->stack + RESERVED_STACK_WORDS;
63 #define SAVE_STACK_POINTERS \
64 cap->r.rCurrentTSO->sp = Sp
66 #define RETURN_TO_SCHEDULER(todo,retcode) \
67 SAVE_STACK_POINTERS; \
68 cap->r.rCurrentTSO->what_next = (todo); \
69 threadPaused(cap,cap->r.rCurrentTSO); \
70 cap->r.rRet = (retcode); \
73 #define RETURN_TO_SCHEDULER_NO_PAUSE(todo,retcode) \
74 SAVE_STACK_POINTERS; \
75 cap->r.rCurrentTSO->what_next = (todo); \
76 cap->r.rRet = (retcode); \
81 allocate_NONUPD (int n_words)
83 return allocate(stg_max(sizeofW(StgHeader)+MIN_PAYLOAD_SIZE, n_words));
86 rtsBool stop_next_breakpoint = rtsFalse;
90 /* Hacky stats, for tuning the interpreter ... */
91 int it_unknown_entries[N_CLOSURE_TYPES];
92 int it_total_unknown_entries;
104 int it_oofreq[27][27];
108 #define INTERP_TICK(n) (n)++
110 void interp_startup ( void )
113 it_retto_BCO = it_retto_UPDATE = it_retto_other = 0;
114 it_total_entries = it_total_unknown_entries = 0;
115 for (i = 0; i < N_CLOSURE_TYPES; i++)
116 it_unknown_entries[i] = 0;
117 it_slides = it_insns = it_BCO_entries = 0;
118 for (i = 0; i < 27; i++) it_ofreq[i] = 0;
119 for (i = 0; i < 27; i++)
120 for (j = 0; j < 27; j++)
125 void interp_shutdown ( void )
127 int i, j, k, o_max, i_max, j_max;
128 debugBelch("%d constrs entered -> (%d BCO, %d UPD, %d ??? )\n",
129 it_retto_BCO + it_retto_UPDATE + it_retto_other,
130 it_retto_BCO, it_retto_UPDATE, it_retto_other );
131 debugBelch("%d total entries, %d unknown entries \n",
132 it_total_entries, it_total_unknown_entries);
133 for (i = 0; i < N_CLOSURE_TYPES; i++) {
134 if (it_unknown_entries[i] == 0) continue;
135 debugBelch(" type %2d: unknown entries (%4.1f%%) == %d\n",
136 i, 100.0 * ((double)it_unknown_entries[i]) /
137 ((double)it_total_unknown_entries),
138 it_unknown_entries[i]);
140 debugBelch("%d insns, %d slides, %d BCO_entries\n",
141 it_insns, it_slides, it_BCO_entries);
142 for (i = 0; i < 27; i++)
143 debugBelch("opcode %2d got %d\n", i, it_ofreq[i] );
145 for (k = 1; k < 20; k++) {
148 for (i = 0; i < 27; i++) {
149 for (j = 0; j < 27; j++) {
150 if (it_oofreq[i][j] > o_max) {
151 o_max = it_oofreq[i][j];
152 i_max = i; j_max = j;
157 debugBelch("%d: count (%4.1f%%) %6d is %d then %d\n",
158 k, ((double)o_max) * 100.0 / ((double)it_insns), o_max,
160 it_oofreq[i_max][j_max] = 0;
165 #else // !INTERP_STATS
167 #define INTERP_TICK(n) /* nothing */
171 static StgWord app_ptrs_itbl[] = {
174 (W_)&stg_ap_ppp_info,
175 (W_)&stg_ap_pppp_info,
176 (W_)&stg_ap_ppppp_info,
177 (W_)&stg_ap_pppppp_info,
180 HsStablePtr breakPointIOAction; // points to the IO action which is executed on a breakpoint
181 // it is set in main/GHC.hs:runStmt
184 interpretBCO (Capability* cap)
186 // Use of register here is primarily to make it clear to compilers
187 // that these entities are non-aliasable.
188 register StgPtr Sp; // local state -- stack pointer
189 register StgPtr SpLim; // local state -- stack lim pointer
190 register StgClosure* obj;
195 // ------------------------------------------------------------------------
198 // We have a closure to evaluate. Stack looks like:
202 // Sp | -------------------> closure
205 if (Sp[0] == (W_)&stg_enter_info) {
210 // ------------------------------------------------------------------------
213 // We have a BCO application to perform. Stack looks like:
224 else if (Sp[0] == (W_)&stg_apply_interp_info) {
225 obj = (StgClosure *)Sp[1];
230 // ------------------------------------------------------------------------
233 // We have an unboxed value to return. See comment before
234 // do_return_unboxed, below.
237 goto do_return_unboxed;
240 // Evaluate the object on top of the stack.
242 obj = (StgClosure*)Sp[0]; Sp++;
245 INTERP_TICK(it_total_evals);
247 IF_DEBUG(interpreter,
249 "\n---------------------------------------------------------------\n");
250 debugBelch("Evaluating: "); printObj(obj);
251 debugBelch("Sp = %p\n", Sp);
254 printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
258 IF_DEBUG(sanity,checkStackChunk(Sp, cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size));
260 switch ( get_itbl(obj)->type ) {
265 case IND_OLDGEN_PERM:
268 obj = ((StgInd*)obj)->indirectee;
279 case CONSTR_NOCAF_STATIC:
293 ASSERT(((StgBCO *)obj)->arity > 0);
297 case AP: /* Copied from stg_AP_entry. */
306 if (Sp - (words+sizeofW(StgUpdateFrame)) < SpLim) {
309 Sp[0] = (W_)&stg_enter_info;
310 RETURN_TO_SCHEDULER(ThreadInterpret, StackOverflow);
313 /* Ok; we're safe. Party on. Push an update frame. */
314 Sp -= sizeofW(StgUpdateFrame);
316 StgUpdateFrame *__frame;
317 __frame = (StgUpdateFrame *)Sp;
318 SET_INFO(__frame, (StgInfoTable *)&stg_upd_frame_info);
319 __frame->updatee = (StgClosure *)(ap);
322 /* Reload the stack */
324 for (i=0; i < words; i++) {
325 Sp[i] = (W_)ap->payload[i];
328 obj = (StgClosure*)ap->fun;
329 ASSERT(get_itbl(obj)->type == BCO);
338 j = get_itbl(obj)->type;
339 ASSERT(j >= 0 && j < N_CLOSURE_TYPES);
340 it_unknown_entries[j]++;
341 it_total_unknown_entries++;
345 // Can't handle this object; yield to scheduler
346 IF_DEBUG(interpreter,
347 debugBelch("evaluating unknown closure -- yielding to sched\n");
352 Sp[0] = (W_)&stg_enter_info;
353 RETURN_TO_SCHEDULER_NO_PAUSE(ThreadRunGHC, ThreadYielding);
357 // ------------------------------------------------------------------------
358 // We now have an evaluated object (obj). The next thing to
359 // do is return it to the stack frame on top of the stack.
361 ASSERT(closure_HNF(obj));
363 IF_DEBUG(interpreter,
365 "\n---------------------------------------------------------------\n");
366 debugBelch("Returning: "); printObj(obj);
367 debugBelch("Sp = %p\n", Sp);
369 printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
373 IF_DEBUG(sanity,checkStackChunk(Sp, cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size));
375 switch (get_itbl((StgClosure *)Sp)->type) {
378 const StgInfoTable *info;
380 // NOTE: not using get_itbl().
381 info = ((StgClosure *)Sp)->header.info;
382 if (info == (StgInfoTable *)&stg_ap_v_info) {
383 n = 1; m = 0; goto do_apply;
385 if (info == (StgInfoTable *)&stg_ap_f_info) {
386 n = 1; m = 1; goto do_apply;
388 if (info == (StgInfoTable *)&stg_ap_d_info) {
389 n = 1; m = sizeofW(StgDouble); goto do_apply;
391 if (info == (StgInfoTable *)&stg_ap_l_info) {
392 n = 1; m = sizeofW(StgInt64); goto do_apply;
394 if (info == (StgInfoTable *)&stg_ap_n_info) {
395 n = 1; m = 1; goto do_apply;
397 if (info == (StgInfoTable *)&stg_ap_p_info) {
398 n = 1; m = 1; goto do_apply;
400 if (info == (StgInfoTable *)&stg_ap_pp_info) {
401 n = 2; m = 2; goto do_apply;
403 if (info == (StgInfoTable *)&stg_ap_ppp_info) {
404 n = 3; m = 3; goto do_apply;
406 if (info == (StgInfoTable *)&stg_ap_pppp_info) {
407 n = 4; m = 4; goto do_apply;
409 if (info == (StgInfoTable *)&stg_ap_ppppp_info) {
410 n = 5; m = 5; goto do_apply;
412 if (info == (StgInfoTable *)&stg_ap_pppppp_info) {
413 n = 6; m = 6; goto do_apply;
415 goto do_return_unrecognised;
419 // Returning to an update frame: do the update, pop the update
420 // frame, and continue with the next stack frame.
421 INTERP_TICK(it_retto_UPDATE);
422 UPD_IND(((StgUpdateFrame *)Sp)->updatee, obj);
423 Sp += sizeofW(StgUpdateFrame);
427 // Returning to an interpreted continuation: put the object on
428 // the stack, and start executing the BCO.
429 INTERP_TICK(it_retto_BCO);
432 obj = (StgClosure*)Sp[2];
433 ASSERT(get_itbl(obj)->type == BCO);
437 do_return_unrecognised:
439 // Can't handle this return address; yield to scheduler
440 INTERP_TICK(it_retto_other);
441 IF_DEBUG(interpreter,
442 debugBelch("returning to unknown frame -- yielding to sched\n");
443 printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
447 Sp[0] = (W_)&stg_enter_info;
448 RETURN_TO_SCHEDULER_NO_PAUSE(ThreadRunGHC, ThreadYielding);
452 // -------------------------------------------------------------------------
453 // Returning an unboxed value. The stack looks like this:
470 // where XXXX_info is one of the stg_gc_unbx_r1_info family.
472 // We're only interested in the case when the real return address
473 // is a BCO; otherwise we'll return to the scheduler.
479 ASSERT( Sp[0] == (W_)&stg_gc_unbx_r1_info
480 || Sp[0] == (W_)&stg_gc_unpt_r1_info
481 || Sp[0] == (W_)&stg_gc_f1_info
482 || Sp[0] == (W_)&stg_gc_d1_info
483 || Sp[0] == (W_)&stg_gc_l1_info
484 || Sp[0] == (W_)&stg_gc_void_info // VoidRep
487 // get the offset of the stg_ctoi_ret_XXX itbl
488 offset = stack_frame_sizeW((StgClosure *)Sp);
490 switch (get_itbl((StgClosure *)Sp+offset)->type) {
493 // Returning to an interpreted continuation: put the object on
494 // the stack, and start executing the BCO.
495 INTERP_TICK(it_retto_BCO);
496 obj = (StgClosure*)Sp[offset+1];
497 ASSERT(get_itbl(obj)->type == BCO);
498 goto run_BCO_return_unboxed;
502 // Can't handle this return address; yield to scheduler
503 INTERP_TICK(it_retto_other);
504 IF_DEBUG(interpreter,
505 debugBelch("returning to unknown frame -- yielding to sched\n");
506 printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
508 RETURN_TO_SCHEDULER_NO_PAUSE(ThreadRunGHC, ThreadYielding);
515 // -------------------------------------------------------------------------
519 // we have a function to apply (obj), and n arguments taking up m
520 // words on the stack. The info table (stg_ap_pp_info or whatever)
521 // is on top of the arguments on the stack.
523 switch (get_itbl(obj)->type) {
531 // we only cope with PAPs whose function is a BCO
532 if (get_itbl(pap->fun)->type != BCO) {
533 goto defer_apply_to_sched;
540 // n must be greater than 1, and the only kinds of
541 // application we support with more than one argument
542 // are all pointers...
544 // Shuffle the args for this function down, and put
545 // the appropriate info table in the gap.
546 for (i = 0; i < arity; i++) {
547 Sp[(int)i-1] = Sp[i];
548 // ^^^^^ careful, i-1 might be negative, but i in unsigned
550 Sp[arity-1] = app_ptrs_itbl[n-arity-1];
552 // unpack the PAP's arguments onto the stack
554 for (i = 0; i < pap->n_args; i++) {
555 Sp[i] = (W_)pap->payload[i];
560 else if (arity == n) {
562 for (i = 0; i < pap->n_args; i++) {
563 Sp[i] = (W_)pap->payload[i];
568 else /* arity > n */ {
569 // build a new PAP and return it.
571 new_pap = (StgPAP *)allocate(PAP_sizeW(pap->n_args + m));
572 SET_HDR(new_pap,&stg_PAP_info,CCCS);
573 new_pap->arity = pap->arity - n;
574 new_pap->n_args = pap->n_args + m;
575 new_pap->fun = pap->fun;
576 for (i = 0; i < pap->n_args; i++) {
577 new_pap->payload[i] = pap->payload[i];
579 for (i = 0; i < m; i++) {
580 new_pap->payload[pap->n_args + i] = (StgClosure *)Sp[i];
582 obj = (StgClosure *)new_pap;
592 arity = ((StgBCO *)obj)->arity;
595 // n must be greater than 1, and the only kinds of
596 // application we support with more than one argument
597 // are all pointers...
599 // Shuffle the args for this function down, and put
600 // the appropriate info table in the gap.
601 for (i = 0; i < arity; i++) {
602 Sp[(int)i-1] = Sp[i];
603 // ^^^^^ careful, i-1 might be negative, but i in unsigned
605 Sp[arity-1] = app_ptrs_itbl[n-arity-1];
609 else if (arity == n) {
612 else /* arity > n */ {
613 // build a PAP and return it.
616 pap = (StgPAP *)allocate(PAP_sizeW(m));
617 SET_HDR(pap, &stg_PAP_info,CCCS);
618 pap->arity = arity - n;
621 for (i = 0; i < m; i++) {
622 pap->payload[i] = (StgClosure *)Sp[i];
624 obj = (StgClosure *)pap;
630 // No point in us applying machine-code functions
632 defer_apply_to_sched:
635 Sp[0] = (W_)&stg_enter_info;
636 RETURN_TO_SCHEDULER_NO_PAUSE(ThreadRunGHC, ThreadYielding);
639 // ------------------------------------------------------------------------
640 // Ok, we now have a bco (obj), and its arguments are all on the
641 // stack. We can start executing the byte codes.
643 // The stack is in one of two states. First, if this BCO is a
653 // Second, if this BCO is a continuation:
668 // where retval is the value being returned to this continuation.
669 // In the event of a stack check, heap check, or context switch,
670 // we need to leave the stack in a sane state so the garbage
671 // collector can find all the pointers.
673 // (1) BCO is a function: the BCO's bitmap describes the
674 // pointerhood of the arguments.
676 // (2) BCO is a continuation: BCO's bitmap describes the
677 // pointerhood of the free variables.
679 // Sadly we have three different kinds of stack/heap/cswitch check
685 if (doYouWantToGC()) {
686 Sp--; Sp[0] = (W_)&stg_enter_info;
687 RETURN_TO_SCHEDULER(ThreadInterpret, HeapOverflow);
689 // Stack checks aren't necessary at return points, the stack use
690 // is aggregated into the enclosing function entry point.
694 run_BCO_return_unboxed:
696 if (doYouWantToGC()) {
697 RETURN_TO_SCHEDULER(ThreadInterpret, HeapOverflow);
699 // Stack checks aren't necessary at return points, the stack use
700 // is aggregated into the enclosing function entry point.
708 Sp[0] = (W_)&stg_apply_interp_info;
709 checkStackChunk(Sp,SpLim);
714 if (doYouWantToGC()) {
717 Sp[0] = (W_)&stg_apply_interp_info; // placeholder, really
718 RETURN_TO_SCHEDULER(ThreadInterpret, HeapOverflow);
722 if (Sp - INTERP_STACK_CHECK_THRESH < SpLim) {
725 Sp[0] = (W_)&stg_apply_interp_info; // placeholder, really
726 RETURN_TO_SCHEDULER(ThreadInterpret, StackOverflow);
731 // Now, actually interpret the BCO... (no returning to the
732 // scheduler again until the stack is in an orderly state).
734 INTERP_TICK(it_BCO_entries);
736 register int bciPtr = 1; /* instruction pointer */
737 register StgWord16 bci;
738 register StgBCO* bco = (StgBCO*)obj;
739 register StgWord16* instrs = (StgWord16*)(bco->instrs->payload);
740 register StgWord* literals = (StgWord*)(&bco->literals->payload[0]);
741 register StgPtr* ptrs = (StgPtr*)(&bco->ptrs->payload[0]);
744 it_lastopc = 0; /* no opcode */
748 ASSERT(bciPtr <= instrs[0]);
749 IF_DEBUG(interpreter,
750 //if (do_print_stack) {
751 //debugBelch("\n-- BEGIN stack\n");
752 //printStack(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size,iSu);
753 //debugBelch("-- END stack\n\n");
755 debugBelch("Sp = %p pc = %d ", Sp, bciPtr);
756 disInstr(bco,bciPtr);
759 for (i = 8; i >= 0; i--) {
760 debugBelch("%d %p\n", i, (StgPtr)(*(Sp+i)));
764 //if (do_print_stack) checkStack(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size,iSu);
768 INTERP_TICK(it_insns);
771 ASSERT( (int)instrs[bciPtr] >= 0 && (int)instrs[bciPtr] < 27 );
772 it_ofreq[ (int)instrs[bciPtr] ] ++;
773 it_oofreq[ it_lastopc ][ (int)instrs[bciPtr] ] ++;
774 it_lastopc = (int)instrs[bciPtr];
778 /* We use the high 8 bits for flags, only the highest of which is
779 * currently allocated */
780 ASSERT((bci & 0xFF00) == (bci & 0x8000));
782 switch (bci & 0xFF) {
784 /* check for a breakpoint on the beginning of a let binding */
787 int arg1_brk_array, arg2_array_index, arg3_freeVars;
788 StgArrWords *breakPoints;
789 int returning_from_break; /* are we resuming execution from a breakpoint?
790 ** if yes, then don't break this time around */
791 StgClosure *ioAction; // the io action to run at a breakpoint
793 StgAP_STACK *new_aps; // a closure to save the top stack frame on the heap
797 arg1_brk_array = BCO_NEXT; /* first argument of break instruction */
798 arg2_array_index = BCO_NEXT; /* second dargument of break instruction */
799 arg3_freeVars = BCO_NEXT; /* third argument of break instruction */
801 // check if we are returning from a breakpoint - this info is stored in
802 // the flags field of the current TSO
803 returning_from_break = cap->r.rCurrentTSO->flags & TSO_STOPPED_ON_BREAKPOINT;
805 // if we are returning from a break then skip this section and continue executing
806 if (!returning_from_break)
808 breakPoints = (StgArrWords *) BCO_PTR(arg1_brk_array);
810 // stop the current thread if either the "stop_next_breakpoint" flag is true
811 // OR if the breakpoint flag for this particular expression is true
812 if (stop_next_breakpoint == rtsTrue || breakPoints->payload[arg2_array_index] == rtsTrue)
814 stop_next_breakpoint = rtsFalse; // make sure we don't automatically stop at the next breakpoint
816 // allocate memory for a new AP_STACK, enough to store the top stack frame
817 // plus an stg_apply_interp_info pointer and a pointer to the BCO
818 size_words = BCO_BITMAP_SIZE(obj) + 2;
819 new_aps = (StgAP_STACK *) allocate (AP_STACK_sizeW(size_words));
820 SET_HDR(new_aps,&stg_AP_STACK_info,CCS_SYSTEM);
821 new_aps->size = size_words;
822 // we should never enter new_aps->fun, so it is assigned to a dummy value
823 // ToDo: fixme to something that explodes with an error if you enter it
824 new_aps->fun = &stg_dummy_ret_closure;
826 // fill in the payload of the AP_STACK
827 new_aps->payload[0] = (W_)&stg_apply_interp_info;
828 new_aps->payload[1] = (W_)obj;
830 // copy the contents of the top stack frame into the AP_STACK
831 for (i = 2; i < size_words; i++)
833 new_aps->payload[i] = (W_)Sp[i-2];
836 // prepare the stack so that we can call the breakPointIOAction
837 // and ensure that the stack is in a reasonable state for the GC
838 // and so that execution of this BCO can continue when we resume
839 ioAction = (StgClosure *) deRefStablePtr (breakPointIOAction);
842 Sp[5] = (W_)&stg_apply_interp_info;
843 Sp[4] = (W_)new_aps; /* the AP_STACK */
844 Sp[3] = (W_)BCO_PTR(arg3_freeVars); /* the info about local vars of the breakpoint */
845 Sp[2] = (W_)&stg_ap_ppv_info;
846 Sp[1] = (W_)ioAction; /* apply the IO action to its two arguments above */
847 Sp[0] = (W_)&stg_enter_info; /* get ready to run the IO action */
849 // set the flag in the TSO to say that we are now stopping at a breakpoint
850 // so that when we resume we don't stop on the same breakpoint that we already
851 // stopped at just now
852 cap->r.rCurrentTSO->flags |= TSO_STOPPED_ON_BREAKPOINT;
854 // stop this thread and return to the scheduler - eventually we will come back
855 // and the IO action on the top of the stack will be executed
856 RETURN_TO_SCHEDULER_NO_PAUSE(ThreadRunGHC, ThreadYielding);
859 // record that this thread is not stopped at a breakpoint anymore
860 cap->r.rCurrentTSO->flags &= ~TSO_STOPPED_ON_BREAKPOINT;
862 // continue normal execution of the byte code instructions
867 // Explicit stack check at the beginning of a function
868 // *only* (stack checks in case alternatives are
869 // propagated to the enclosing function).
870 StgWord stk_words_reqd = BCO_GET_LARGE_ARG + 1;
871 if (Sp - stk_words_reqd < SpLim) {
874 Sp[0] = (W_)&stg_apply_interp_info;
875 RETURN_TO_SCHEDULER(ThreadInterpret, StackOverflow);
910 Sp[-1] = BCO_PTR(o1);
915 case bci_PUSH_ALTS: {
916 int o_bco = BCO_NEXT;
917 Sp[-2] = (W_)&stg_ctoi_R1p_info;
918 Sp[-1] = BCO_PTR(o_bco);
923 case bci_PUSH_ALTS_P: {
924 int o_bco = BCO_NEXT;
925 Sp[-2] = (W_)&stg_ctoi_R1unpt_info;
926 Sp[-1] = BCO_PTR(o_bco);
931 case bci_PUSH_ALTS_N: {
932 int o_bco = BCO_NEXT;
933 Sp[-2] = (W_)&stg_ctoi_R1n_info;
934 Sp[-1] = BCO_PTR(o_bco);
939 case bci_PUSH_ALTS_F: {
940 int o_bco = BCO_NEXT;
941 Sp[-2] = (W_)&stg_ctoi_F1_info;
942 Sp[-1] = BCO_PTR(o_bco);
947 case bci_PUSH_ALTS_D: {
948 int o_bco = BCO_NEXT;
949 Sp[-2] = (W_)&stg_ctoi_D1_info;
950 Sp[-1] = BCO_PTR(o_bco);
955 case bci_PUSH_ALTS_L: {
956 int o_bco = BCO_NEXT;
957 Sp[-2] = (W_)&stg_ctoi_L1_info;
958 Sp[-1] = BCO_PTR(o_bco);
963 case bci_PUSH_ALTS_V: {
964 int o_bco = BCO_NEXT;
965 Sp[-2] = (W_)&stg_ctoi_V_info;
966 Sp[-1] = BCO_PTR(o_bco);
971 case bci_PUSH_APPLY_N:
972 Sp--; Sp[0] = (W_)&stg_ap_n_info;
974 case bci_PUSH_APPLY_V:
975 Sp--; Sp[0] = (W_)&stg_ap_v_info;
977 case bci_PUSH_APPLY_F:
978 Sp--; Sp[0] = (W_)&stg_ap_f_info;
980 case bci_PUSH_APPLY_D:
981 Sp--; Sp[0] = (W_)&stg_ap_d_info;
983 case bci_PUSH_APPLY_L:
984 Sp--; Sp[0] = (W_)&stg_ap_l_info;
986 case bci_PUSH_APPLY_P:
987 Sp--; Sp[0] = (W_)&stg_ap_p_info;
989 case bci_PUSH_APPLY_PP:
990 Sp--; Sp[0] = (W_)&stg_ap_pp_info;
992 case bci_PUSH_APPLY_PPP:
993 Sp--; Sp[0] = (W_)&stg_ap_ppp_info;
995 case bci_PUSH_APPLY_PPPP:
996 Sp--; Sp[0] = (W_)&stg_ap_pppp_info;
998 case bci_PUSH_APPLY_PPPPP:
999 Sp--; Sp[0] = (W_)&stg_ap_ppppp_info;
1001 case bci_PUSH_APPLY_PPPPPP:
1002 Sp--; Sp[0] = (W_)&stg_ap_pppppp_info;
1005 case bci_PUSH_UBX: {
1007 int o_lits = BCO_NEXT;
1008 int n_words = BCO_NEXT;
1010 for (i = 0; i < n_words; i++) {
1011 Sp[i] = (W_)BCO_LIT(o_lits+i);
1019 /* a_1, .. a_n, b_1, .. b_by, s => a_1, .. a_n, s */
1024 INTERP_TICK(it_slides);
1028 case bci_ALLOC_AP: {
1030 int n_payload = BCO_NEXT;
1031 ap = (StgAP*)allocate(AP_sizeW(n_payload));
1033 ap->n_args = n_payload;
1034 SET_HDR(ap, &stg_AP_info, CCS_SYSTEM/*ToDo*/)
1039 case bci_ALLOC_PAP: {
1041 int arity = BCO_NEXT;
1042 int n_payload = BCO_NEXT;
1043 pap = (StgPAP*)allocate(PAP_sizeW(n_payload));
1045 pap->n_args = n_payload;
1047 SET_HDR(pap, &stg_PAP_info, CCS_SYSTEM/*ToDo*/)
1054 int stkoff = BCO_NEXT;
1055 int n_payload = BCO_NEXT;
1056 StgAP* ap = (StgAP*)Sp[stkoff];
1057 ASSERT((int)ap->n_args == n_payload);
1058 ap->fun = (StgClosure*)Sp[0];
1060 // The function should be a BCO, and its bitmap should
1061 // cover the payload of the AP correctly.
1062 ASSERT(get_itbl(ap->fun)->type == BCO
1063 && BCO_BITMAP_SIZE(ap->fun) == ap->n_args);
1065 for (i = 0; i < n_payload; i++)
1066 ap->payload[i] = (StgClosure*)Sp[i+1];
1068 IF_DEBUG(interpreter,
1069 debugBelch("\tBuilt ");
1070 printObj((StgClosure*)ap);
1077 int stkoff = BCO_NEXT;
1078 int n_payload = BCO_NEXT;
1079 StgPAP* pap = (StgPAP*)Sp[stkoff];
1080 ASSERT((int)pap->n_args == n_payload);
1081 pap->fun = (StgClosure*)Sp[0];
1083 // The function should be a BCO
1084 ASSERT(get_itbl(pap->fun)->type == BCO);
1086 for (i = 0; i < n_payload; i++)
1087 pap->payload[i] = (StgClosure*)Sp[i+1];
1089 IF_DEBUG(interpreter,
1090 debugBelch("\tBuilt ");
1091 printObj((StgClosure*)pap);
1097 /* Unpack N ptr words from t.o.s constructor */
1099 int n_words = BCO_NEXT;
1100 StgClosure* con = (StgClosure*)Sp[0];
1102 for (i = 0; i < n_words; i++) {
1103 Sp[i] = (W_)con->payload[i];
1110 int o_itbl = BCO_NEXT;
1111 int n_words = BCO_NEXT;
1112 StgInfoTable* itbl = INFO_PTR_TO_STRUCT(BCO_LIT(o_itbl));
1113 int request = CONSTR_sizeW( itbl->layout.payload.ptrs,
1114 itbl->layout.payload.nptrs );
1115 StgClosure* con = (StgClosure*)allocate_NONUPD(request);
1116 ASSERT( itbl->layout.payload.ptrs + itbl->layout.payload.nptrs > 0);
1117 SET_HDR(con, (StgInfoTable*)BCO_LIT(o_itbl), CCS_SYSTEM/*ToDo*/);
1118 for (i = 0; i < n_words; i++) {
1119 con->payload[i] = (StgClosure*)Sp[i];
1124 IF_DEBUG(interpreter,
1125 debugBelch("\tBuilt ");
1126 printObj((StgClosure*)con);
1131 case bci_TESTLT_P: {
1132 unsigned int discr = BCO_NEXT;
1133 int failto = BCO_NEXT;
1134 StgClosure* con = (StgClosure*)Sp[0];
1135 if (GET_TAG(con) >= discr) {
1141 case bci_TESTEQ_P: {
1142 unsigned int discr = BCO_NEXT;
1143 int failto = BCO_NEXT;
1144 StgClosure* con = (StgClosure*)Sp[0];
1145 if (GET_TAG(con) != discr) {
1151 case bci_TESTLT_I: {
1152 // There should be an Int at Sp[1], and an info table at Sp[0].
1153 int discr = BCO_NEXT;
1154 int failto = BCO_NEXT;
1155 I_ stackInt = (I_)Sp[1];
1156 if (stackInt >= (I_)BCO_LIT(discr))
1161 case bci_TESTEQ_I: {
1162 // There should be an Int at Sp[1], and an info table at Sp[0].
1163 int discr = BCO_NEXT;
1164 int failto = BCO_NEXT;
1165 I_ stackInt = (I_)Sp[1];
1166 if (stackInt != (I_)BCO_LIT(discr)) {
1172 case bci_TESTLT_D: {
1173 // There should be a Double at Sp[1], and an info table at Sp[0].
1174 int discr = BCO_NEXT;
1175 int failto = BCO_NEXT;
1176 StgDouble stackDbl, discrDbl;
1177 stackDbl = PK_DBL( & Sp[1] );
1178 discrDbl = PK_DBL( & BCO_LIT(discr) );
1179 if (stackDbl >= discrDbl) {
1185 case bci_TESTEQ_D: {
1186 // There should be a Double at Sp[1], and an info table at Sp[0].
1187 int discr = BCO_NEXT;
1188 int failto = BCO_NEXT;
1189 StgDouble stackDbl, discrDbl;
1190 stackDbl = PK_DBL( & Sp[1] );
1191 discrDbl = PK_DBL( & BCO_LIT(discr) );
1192 if (stackDbl != discrDbl) {
1198 case bci_TESTLT_F: {
1199 // There should be a Float at Sp[1], and an info table at Sp[0].
1200 int discr = BCO_NEXT;
1201 int failto = BCO_NEXT;
1202 StgFloat stackFlt, discrFlt;
1203 stackFlt = PK_FLT( & Sp[1] );
1204 discrFlt = PK_FLT( & BCO_LIT(discr) );
1205 if (stackFlt >= discrFlt) {
1211 case bci_TESTEQ_F: {
1212 // There should be a Float at Sp[1], and an info table at Sp[0].
1213 int discr = BCO_NEXT;
1214 int failto = BCO_NEXT;
1215 StgFloat stackFlt, discrFlt;
1216 stackFlt = PK_FLT( & Sp[1] );
1217 discrFlt = PK_FLT( & BCO_LIT(discr) );
1218 if (stackFlt != discrFlt) {
1224 // Control-flow ish things
1226 // Context-switch check. We put it here to ensure that
1227 // the interpreter has done at least *some* work before
1228 // context switching: sometimes the scheduler can invoke
1229 // the interpreter with context_switch == 1, particularly
1230 // if the -C0 flag has been given on the cmd line.
1231 if (context_switch) {
1232 Sp--; Sp[0] = (W_)&stg_enter_info;
1233 RETURN_TO_SCHEDULER(ThreadInterpret, ThreadYielding);
1238 obj = (StgClosure *)Sp[0];
1244 Sp[0] = (W_)&stg_gc_unpt_r1_info;
1245 goto do_return_unboxed;
1248 Sp[0] = (W_)&stg_gc_unbx_r1_info;
1249 goto do_return_unboxed;
1252 Sp[0] = (W_)&stg_gc_f1_info;
1253 goto do_return_unboxed;
1256 Sp[0] = (W_)&stg_gc_d1_info;
1257 goto do_return_unboxed;
1260 Sp[0] = (W_)&stg_gc_l1_info;
1261 goto do_return_unboxed;
1264 Sp[0] = (W_)&stg_gc_void_info;
1265 goto do_return_unboxed;
1268 int stkoff = BCO_NEXT;
1269 signed short n = (signed short)(BCO_NEXT);
1270 Sp[stkoff] += (W_)n;
1276 int stk_offset = BCO_NEXT;
1277 int o_itbl = BCO_NEXT;
1278 void(*marshall_fn)(void*) = (void (*)(void*))BCO_LIT(o_itbl);
1280 RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE
1281 + sizeofW(StgRetDyn);
1285 // Arguments on the TSO stack are not good, because garbage
1286 // collection might move the TSO as soon as we call
1287 // suspendThread below.
1289 W_ arguments[stk_offset];
1291 memcpy(arguments, Sp, sizeof(W_) * stk_offset);
1294 // Restore the Haskell thread's current value of errno
1295 errno = cap->r.rCurrentTSO->saved_errno;
1297 // There are a bunch of non-ptr words on the stack (the
1298 // ccall args, the ccall fun address and space for the
1299 // result), which we need to cover with an info table
1300 // since we might GC during this call.
1302 // We know how many (non-ptr) words there are before the
1303 // next valid stack frame: it is the stk_offset arg to the
1304 // CCALL instruction. So we build a RET_DYN stack frame
1305 // on the stack frame to describe this chunk of stack.
1308 ((StgRetDyn *)Sp)->liveness = NO_PTRS | N_NONPTRS(stk_offset);
1309 ((StgRetDyn *)Sp)->info = (StgInfoTable *)&stg_gc_gen_info;
1311 SAVE_STACK_POINTERS;
1312 tok = suspendThread(&cap->r);
1314 #ifndef THREADED_RTS
1316 // suspendThread might have shifted the stack
1317 // around (stack squeezing), so we have to grab the real
1318 // Sp out of the TSO to find the ccall args again.
1320 marshall_fn ( (void*)(cap->r.rCurrentTSO->sp + ret_dyn_size) );
1323 // We already made a copy of the arguments above.
1325 marshall_fn ( arguments );
1328 // And restart the thread again, popping the RET_DYN frame.
1329 cap = (Capability *)((void *)((unsigned char*)resumeThread(tok) - sizeof(StgFunTable)));
1330 LOAD_STACK_POINTERS;
1333 // Save the Haskell thread's current value of errno
1334 cap->r.rCurrentTSO->saved_errno = errno;
1338 // Copy the "arguments", which might include a return value,
1339 // back to the TSO stack. It would of course be enough to
1340 // just copy the return value, but we don't know the offset.
1341 memcpy(Sp, arguments, sizeof(W_) * stk_offset);
1348 /* BCO_NEXT modifies bciPtr, so be conservative. */
1349 int nextpc = BCO_NEXT;
1355 barf("interpretBCO: hit a CASEFAIL");
1359 barf("interpretBCO: unknown or unimplemented opcode %d",
1362 } /* switch on opcode */
1366 barf("interpretBCO: fell off end of the interpreter");
1369 /* temporary code for peeking inside a AP_STACK and pulling out values
1370 based on their stack offset - used in the debugger for inspecting
1371 the local values of a breakpoint
1373 HsStablePtr rts_getApStackVal (HsStablePtr, int);
1374 HsStablePtr rts_getApStackVal (HsStablePtr apStackSptr, int offset)
1376 HsStablePtr resultSptr;
1377 StgAP_STACK *apStack;
1378 StgClosure **payload;
1381 apStack = (StgAP_STACK *) deRefStablePtr (apStackSptr);
1382 payload = apStack->payload;
1383 val = (StgClosure *) payload[offset+2];
1384 resultSptr = getStablePtr (val);
1388 /* set the single step flag for the debugger to True -
1389 it gets set back to false in the interpreter everytime
1392 void rts_setStepFlag (void);
1393 void rts_setStepFlag (void)
1395 stop_next_breakpoint = rtsTrue;