GHCi debugger: new flag -fbreak-on-exception
[ghc-hetmet.git] / rts / Interpreter.c
1 /* -----------------------------------------------------------------------------
2  * Bytecode interpreter
3  *
4  * Copyright (c) The GHC Team, 1994-2002.
5  * ---------------------------------------------------------------------------*/
6
7 #include "PosixSource.h"
8 #include "Rts.h"
9 #include "RtsAPI.h"
10 #include "RtsUtils.h"
11 #include "Closures.h"
12 #include "TSO.h"
13 #include "Schedule.h"
14 #include "RtsFlags.h"
15 #include "LdvProfile.h"
16 #include "Updates.h"
17 #include "Sanity.h"
18 #include "Liveness.h"
19 #include "Prelude.h"
20
21 #include "Bytecodes.h"
22 #include "Printer.h"
23 #include "Disassembler.h"
24 #include "Interpreter.h"
25
26 #include <string.h>     /* for memcpy */
27 #ifdef HAVE_ERRNO_H
28 #include <errno.h>
29 #endif
30
31
32 /* --------------------------------------------------------------------------
33  * The bytecode interpreter
34  * ------------------------------------------------------------------------*/
35
36 /* Gather stats about entry, opcode, opcode-pair frequencies.  For
37    tuning the interpreter. */
38
39 /* #define INTERP_STATS */
40
41
42 /* Sp points to the lowest live word on the stack. */
43
44 #define BCO_NEXT      instrs[bciPtr++]
45 #define BCO_NEXT_32   (bciPtr += 2, (((StgWord) instrs[bciPtr-2]) << 16) + ((StgWord) instrs[bciPtr-1]))
46 #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]))
47 #if WORD_SIZE_IN_BITS == 32
48 #define BCO_NEXT_WORD BCO_NEXT_32
49 #elif WORD_SIZE_IN_BITS == 64
50 #define BCO_NEXT_WORD BCO_NEXT_64
51 #else
52 #error Cannot cope with WORD_SIZE_IN_BITS being nether 32 nor 64
53 #endif
54 #define BCO_GET_LARGE_ARG ((bci & bci_FLAG_LARGE_ARGS) ? BCO_NEXT_WORD : BCO_NEXT)
55
56 #define BCO_PTR(n)    (W_)ptrs[n]
57 #define BCO_LIT(n)    literals[n]
58
59 #define LOAD_STACK_POINTERS                                     \
60     Sp = cap->r.rCurrentTSO->sp;                                \
61     /* We don't change this ... */                              \
62     SpLim = cap->r.rCurrentTSO->stack + RESERVED_STACK_WORDS;
63
64 #define SAVE_STACK_POINTERS                     \
65     cap->r.rCurrentTSO->sp = Sp
66
67 #define RETURN_TO_SCHEDULER(todo,retcode)       \
68    SAVE_STACK_POINTERS;                         \
69    cap->r.rCurrentTSO->what_next = (todo);      \
70    threadPaused(cap,cap->r.rCurrentTSO);                \
71    cap->r.rRet = (retcode);                     \
72    return cap;
73
74 #define RETURN_TO_SCHEDULER_NO_PAUSE(todo,retcode)      \
75    SAVE_STACK_POINTERS;                                 \
76    cap->r.rCurrentTSO->what_next = (todo);              \
77    cap->r.rRet = (retcode);                             \
78    return cap;
79
80
81 STATIC_INLINE StgPtr
82 allocate_NONUPD (int n_words)
83 {
84     return allocate(stg_max(sizeofW(StgHeader)+MIN_PAYLOAD_SIZE, n_words));
85 }
86
87 int rts_stop_next_breakpoint = 0;
88 int rts_stop_on_exception = 0;
89
90 #ifdef INTERP_STATS
91
92 /* Hacky stats, for tuning the interpreter ... */
93 int it_unknown_entries[N_CLOSURE_TYPES];
94 int it_total_unknown_entries;
95 int it_total_entries;
96
97 int it_retto_BCO;
98 int it_retto_UPDATE;
99 int it_retto_other;
100
101 int it_slides;
102 int it_insns;
103 int it_BCO_entries;
104
105 int it_ofreq[27];
106 int it_oofreq[27][27];
107 int it_lastopc;
108
109
110 #define INTERP_TICK(n) (n)++
111
112 void interp_startup ( void )
113 {
114    int i, j;
115    it_retto_BCO = it_retto_UPDATE = it_retto_other = 0;
116    it_total_entries = it_total_unknown_entries = 0;
117    for (i = 0; i < N_CLOSURE_TYPES; i++)
118       it_unknown_entries[i] = 0;
119    it_slides = it_insns = it_BCO_entries = 0;
120    for (i = 0; i < 27; i++) it_ofreq[i] = 0;
121    for (i = 0; i < 27; i++) 
122      for (j = 0; j < 27; j++)
123         it_oofreq[i][j] = 0;
124    it_lastopc = 0;
125 }
126
127 void interp_shutdown ( void )
128 {
129    int i, j, k, o_max, i_max, j_max;
130    debugBelch("%d constrs entered -> (%d BCO, %d UPD, %d ??? )\n",
131                    it_retto_BCO + it_retto_UPDATE + it_retto_other,
132                    it_retto_BCO, it_retto_UPDATE, it_retto_other );
133    debugBelch("%d total entries, %d unknown entries \n", 
134                    it_total_entries, it_total_unknown_entries);
135    for (i = 0; i < N_CLOSURE_TYPES; i++) {
136      if (it_unknown_entries[i] == 0) continue;
137      debugBelch("   type %2d: unknown entries (%4.1f%%) == %d\n",
138              i, 100.0 * ((double)it_unknown_entries[i]) / 
139                         ((double)it_total_unknown_entries),
140              it_unknown_entries[i]);
141    }
142    debugBelch("%d insns, %d slides, %d BCO_entries\n", 
143                    it_insns, it_slides, it_BCO_entries);
144    for (i = 0; i < 27; i++) 
145       debugBelch("opcode %2d got %d\n", i, it_ofreq[i] );
146
147    for (k = 1; k < 20; k++) {
148       o_max = 0;
149       i_max = j_max = 0;
150       for (i = 0; i < 27; i++) {
151          for (j = 0; j < 27; j++) {
152             if (it_oofreq[i][j] > o_max) {
153                o_max = it_oofreq[i][j];
154                i_max = i; j_max = j;
155             }
156          }
157       }
158       
159       debugBelch("%d:  count (%4.1f%%) %6d   is %d then %d\n",
160                 k, ((double)o_max) * 100.0 / ((double)it_insns), o_max,
161                    i_max, j_max );
162       it_oofreq[i_max][j_max] = 0;
163
164    }
165 }
166
167 #else // !INTERP_STATS
168
169 #define INTERP_TICK(n) /* nothing */
170
171 #endif
172
173 static StgWord app_ptrs_itbl[] = {
174     (W_)&stg_ap_p_info,
175     (W_)&stg_ap_pp_info,
176     (W_)&stg_ap_ppp_info,
177     (W_)&stg_ap_pppp_info,
178     (W_)&stg_ap_ppppp_info,
179     (W_)&stg_ap_pppppp_info,
180 };
181
182 HsStablePtr rts_breakpoint_io_action; // points to the IO action which is executed on a breakpoint
183                                 // it is set in main/GHC.hs:runStmt
184
185 Capability *
186 interpretBCO (Capability* cap)
187 {
188     // Use of register here is primarily to make it clear to compilers
189     // that these entities are non-aliasable.
190     register StgPtr       Sp;    // local state -- stack pointer
191     register StgPtr       SpLim; // local state -- stack lim pointer
192     register StgClosure*  obj;
193     nat n, m;
194
195     LOAD_STACK_POINTERS;
196
197     // ------------------------------------------------------------------------
198     // Case 1:
199     // 
200     //       We have a closure to evaluate.  Stack looks like:
201     //       
202     //          |   XXXX_info   |
203     //          +---------------+
204     //       Sp |      -------------------> closure
205     //          +---------------+
206     //       
207     if (Sp[0] == (W_)&stg_enter_info) {
208        Sp++;
209        goto eval;
210     }
211
212     // ------------------------------------------------------------------------
213     // Case 2:
214     // 
215     //       We have a BCO application to perform.  Stack looks like:
216     //
217     //          |     ....      |
218     //          +---------------+
219     //          |     arg1      |
220     //          +---------------+
221     //          |     BCO       |
222     //          +---------------+
223     //       Sp |   RET_BCO     |
224     //          +---------------+
225     //       
226     else if (Sp[0] == (W_)&stg_apply_interp_info) {
227         obj = (StgClosure *)Sp[1];
228         Sp += 2;
229         goto run_BCO_fun;
230     }
231
232     // ------------------------------------------------------------------------
233     // Case 3:
234     //
235     //       We have an unboxed value to return.  See comment before
236     //       do_return_unboxed, below.
237     //
238     else {
239         goto do_return_unboxed;
240     }
241
242     // Evaluate the object on top of the stack.
243 eval:
244     obj = (StgClosure*)Sp[0]; Sp++;
245
246 eval_obj:
247     INTERP_TICK(it_total_evals);
248
249     IF_DEBUG(interpreter,
250              debugBelch(
251              "\n---------------------------------------------------------------\n");
252              debugBelch("Evaluating: "); printObj(obj);
253              debugBelch("Sp = %p\n", Sp);
254              debugBelch("\n" );
255
256              printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
257              debugBelch("\n\n");
258             );
259
260     IF_DEBUG(sanity,checkStackChunk(Sp, cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size));
261
262     switch ( get_itbl(obj)->type ) {
263
264     case IND:
265     case IND_OLDGEN:
266     case IND_PERM:
267     case IND_OLDGEN_PERM:
268     case IND_STATIC:
269     { 
270         obj = ((StgInd*)obj)->indirectee;
271         goto eval_obj;
272     }
273     
274     case CONSTR:
275     case CONSTR_1_0:
276     case CONSTR_0_1:
277     case CONSTR_2_0:
278     case CONSTR_1_1:
279     case CONSTR_0_2:
280     case CONSTR_STATIC:
281     case CONSTR_NOCAF_STATIC:
282     case FUN:
283     case FUN_1_0:
284     case FUN_0_1:
285     case FUN_2_0:
286     case FUN_1_1:
287     case FUN_0_2:
288     case FUN_STATIC:
289     case PAP:
290         // already in WHNF
291         break;
292         
293     case BCO:
294     {
295         ASSERT(((StgBCO *)obj)->arity > 0);
296         break;
297     }
298
299     case AP:    /* Copied from stg_AP_entry. */
300     {
301         nat i, words;
302         StgAP *ap;
303         
304         ap = (StgAP*)obj;
305         words = ap->n_args;
306         
307         // Stack check
308         if (Sp - (words+sizeofW(StgUpdateFrame)) < SpLim) {
309             Sp -= 2;
310             Sp[1] = (W_)obj;
311             Sp[0] = (W_)&stg_enter_info;
312             RETURN_TO_SCHEDULER(ThreadInterpret, StackOverflow);
313         }
314         
315         /* Ok; we're safe.  Party on.  Push an update frame. */
316         Sp -= sizeofW(StgUpdateFrame);
317         {
318             StgUpdateFrame *__frame;
319             __frame = (StgUpdateFrame *)Sp;
320             SET_INFO(__frame, (StgInfoTable *)&stg_upd_frame_info);
321             __frame->updatee = (StgClosure *)(ap);
322         }
323         
324         /* Reload the stack */
325         Sp -= words;
326         for (i=0; i < words; i++) {
327             Sp[i] = (W_)ap->payload[i];
328         }
329
330         obj = (StgClosure*)ap->fun;
331         ASSERT(get_itbl(obj)->type == BCO);
332         goto run_BCO_fun;
333     }
334
335     default:
336 #ifdef INTERP_STATS
337     { 
338         int j;
339         
340         j = get_itbl(obj)->type;
341         ASSERT(j >= 0 && j < N_CLOSURE_TYPES);
342         it_unknown_entries[j]++;
343         it_total_unknown_entries++;
344     }
345 #endif
346     {
347         // Can't handle this object; yield to scheduler
348         IF_DEBUG(interpreter,
349                  debugBelch("evaluating unknown closure -- yielding to sched\n"); 
350                  printObj(obj);
351             );
352         Sp -= 2;
353         Sp[1] = (W_)obj;
354         Sp[0] = (W_)&stg_enter_info;
355         RETURN_TO_SCHEDULER_NO_PAUSE(ThreadRunGHC, ThreadYielding);
356     }
357     }
358
359     // ------------------------------------------------------------------------
360     // We now have an evaluated object (obj).  The next thing to
361     // do is return it to the stack frame on top of the stack.
362 do_return:
363     ASSERT(closure_HNF(obj));
364
365     IF_DEBUG(interpreter,
366              debugBelch(
367              "\n---------------------------------------------------------------\n");
368              debugBelch("Returning: "); printObj(obj);
369              debugBelch("Sp = %p\n", Sp);
370              debugBelch("\n" );
371              printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
372              debugBelch("\n\n");
373             );
374
375     IF_DEBUG(sanity,checkStackChunk(Sp, cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size));
376
377     switch (get_itbl((StgClosure *)Sp)->type) {
378
379     case RET_SMALL: {
380         const StgInfoTable *info;
381
382         // NOTE: not using get_itbl().
383         info = ((StgClosure *)Sp)->header.info;
384         if (info == (StgInfoTable *)&stg_ap_v_info) {
385             n = 1; m = 0; goto do_apply;
386         }
387         if (info == (StgInfoTable *)&stg_ap_f_info) {
388             n = 1; m = 1; goto do_apply;
389         }
390         if (info == (StgInfoTable *)&stg_ap_d_info) {
391             n = 1; m = sizeofW(StgDouble); goto do_apply;
392         }
393         if (info == (StgInfoTable *)&stg_ap_l_info) {
394             n = 1; m = sizeofW(StgInt64); goto do_apply;
395         }
396         if (info == (StgInfoTable *)&stg_ap_n_info) {
397             n = 1; m = 1; goto do_apply;
398         }
399         if (info == (StgInfoTable *)&stg_ap_p_info) {
400             n = 1; m = 1; goto do_apply;
401         }
402         if (info == (StgInfoTable *)&stg_ap_pp_info) {
403             n = 2; m = 2; goto do_apply;
404         }
405         if (info == (StgInfoTable *)&stg_ap_ppp_info) {
406             n = 3; m = 3; goto do_apply;
407         }
408         if (info == (StgInfoTable *)&stg_ap_pppp_info) {
409             n = 4; m = 4; goto do_apply;
410         }
411         if (info == (StgInfoTable *)&stg_ap_ppppp_info) {
412             n = 5; m = 5; goto do_apply;
413         }
414         if (info == (StgInfoTable *)&stg_ap_pppppp_info) {
415             n = 6; m = 6; goto do_apply;
416         }
417         goto do_return_unrecognised;
418     }
419
420     case UPDATE_FRAME:
421         // Returning to an update frame: do the update, pop the update
422         // frame, and continue with the next stack frame.
423         INTERP_TICK(it_retto_UPDATE);
424         UPD_IND(((StgUpdateFrame *)Sp)->updatee, obj); 
425         Sp += sizeofW(StgUpdateFrame);
426         goto do_return;
427
428     case RET_BCO:
429         // Returning to an interpreted continuation: put the object on
430         // the stack, and start executing the BCO.
431         INTERP_TICK(it_retto_BCO);
432         Sp--;
433         Sp[0] = (W_)obj;
434         obj = (StgClosure*)Sp[2];
435         ASSERT(get_itbl(obj)->type == BCO);
436         goto run_BCO_return;
437
438     default:
439     do_return_unrecognised:
440     {
441         // Can't handle this return address; yield to scheduler
442         INTERP_TICK(it_retto_other);
443         IF_DEBUG(interpreter,
444                  debugBelch("returning to unknown frame -- yielding to sched\n"); 
445                  printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
446             );
447         Sp -= 2;
448         Sp[1] = (W_)obj;
449         Sp[0] = (W_)&stg_enter_info;
450         RETURN_TO_SCHEDULER_NO_PAUSE(ThreadRunGHC, ThreadYielding);
451     }
452     }
453
454     // -------------------------------------------------------------------------
455     // Returning an unboxed value.  The stack looks like this:
456     //
457     //    |     ....      |
458     //    +---------------+
459     //    |     fv2       |
460     //    +---------------+
461     //    |     fv1       |
462     //    +---------------+
463     //    |     BCO       |
464     //    +---------------+
465     //    | stg_ctoi_ret_ |
466     //    +---------------+
467     //    |    retval     |
468     //    +---------------+
469     //    |   XXXX_info   |
470     //    +---------------+
471     //
472     // where XXXX_info is one of the stg_gc_unbx_r1_info family.
473     //
474     // We're only interested in the case when the real return address
475     // is a BCO; otherwise we'll return to the scheduler.
476
477 do_return_unboxed:
478     { 
479         int offset;
480         
481         ASSERT( Sp[0] == (W_)&stg_gc_unbx_r1_info
482                 || Sp[0] == (W_)&stg_gc_unpt_r1_info
483                 || Sp[0] == (W_)&stg_gc_f1_info
484                 || Sp[0] == (W_)&stg_gc_d1_info
485                 || Sp[0] == (W_)&stg_gc_l1_info
486                 || Sp[0] == (W_)&stg_gc_void_info // VoidRep
487             );
488
489         // get the offset of the stg_ctoi_ret_XXX itbl
490         offset = stack_frame_sizeW((StgClosure *)Sp);
491
492         switch (get_itbl((StgClosure *)Sp+offset)->type) {
493
494         case RET_BCO:
495             // Returning to an interpreted continuation: put the object on
496             // the stack, and start executing the BCO.
497             INTERP_TICK(it_retto_BCO);
498             obj = (StgClosure*)Sp[offset+1];
499             ASSERT(get_itbl(obj)->type == BCO);
500             goto run_BCO_return_unboxed;
501
502         default:
503         {
504             // Can't handle this return address; yield to scheduler
505             INTERP_TICK(it_retto_other);
506             IF_DEBUG(interpreter,
507                      debugBelch("returning to unknown frame -- yielding to sched\n"); 
508                      printStackChunk(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size);
509                 );
510             RETURN_TO_SCHEDULER_NO_PAUSE(ThreadRunGHC, ThreadYielding);
511         }
512         }
513     }
514     // not reached.
515
516
517     // -------------------------------------------------------------------------
518     // Application...
519
520 do_apply:
521     // we have a function to apply (obj), and n arguments taking up m
522     // words on the stack.  The info table (stg_ap_pp_info or whatever)
523     // is on top of the arguments on the stack.
524     {
525         switch (get_itbl(obj)->type) {
526
527         case PAP: {
528             StgPAP *pap;
529             nat i, arity;
530
531             pap = (StgPAP *)obj;
532
533             // we only cope with PAPs whose function is a BCO
534             if (get_itbl(pap->fun)->type != BCO) {
535                 goto defer_apply_to_sched;
536             }
537
538             Sp++;
539             arity = pap->arity;
540             ASSERT(arity > 0);
541             if (arity < n) {
542                 // n must be greater than 1, and the only kinds of
543                 // application we support with more than one argument
544                 // are all pointers...
545                 //
546                 // Shuffle the args for this function down, and put
547                 // the appropriate info table in the gap.
548                 for (i = 0; i < arity; i++) {
549                     Sp[(int)i-1] = Sp[i];
550                     // ^^^^^ careful, i-1 might be negative, but i in unsigned
551                 }
552                 Sp[arity-1] = app_ptrs_itbl[n-arity-1];
553                 Sp--;
554                 // unpack the PAP's arguments onto the stack
555                 Sp -= pap->n_args;
556                 for (i = 0; i < pap->n_args; i++) {
557                     Sp[i] = (W_)pap->payload[i];
558                 }
559                 obj = pap->fun;
560                 goto run_BCO_fun;
561             } 
562             else if (arity == n) {
563                 Sp -= pap->n_args;
564                 for (i = 0; i < pap->n_args; i++) {
565                     Sp[i] = (W_)pap->payload[i];
566                 }
567                 obj = pap->fun;
568                 goto run_BCO_fun;
569             } 
570             else /* arity > n */ {
571                 // build a new PAP and return it.
572                 StgPAP *new_pap;
573                 new_pap = (StgPAP *)allocate(PAP_sizeW(pap->n_args + m));
574                 SET_HDR(new_pap,&stg_PAP_info,CCCS);
575                 new_pap->arity = pap->arity - n;
576                 new_pap->n_args = pap->n_args + m;
577                 new_pap->fun = pap->fun;
578                 for (i = 0; i < pap->n_args; i++) {
579                     new_pap->payload[i] = pap->payload[i];
580                 }
581                 for (i = 0; i < m; i++) {
582                     new_pap->payload[pap->n_args + i] = (StgClosure *)Sp[i];
583                 }
584                 obj = (StgClosure *)new_pap;
585                 Sp += m;
586                 goto do_return;
587             }
588         }           
589
590         case BCO: {
591             nat arity, i;
592
593             Sp++;
594             arity = ((StgBCO *)obj)->arity;
595             ASSERT(arity > 0);
596             if (arity < n) {
597                 // n must be greater than 1, and the only kinds of
598                 // application we support with more than one argument
599                 // are all pointers...
600                 //
601                 // Shuffle the args for this function down, and put
602                 // the appropriate info table in the gap.
603                 for (i = 0; i < arity; i++) {
604                     Sp[(int)i-1] = Sp[i];
605                     // ^^^^^ careful, i-1 might be negative, but i in unsigned
606                 }
607                 Sp[arity-1] = app_ptrs_itbl[n-arity-1];
608                 Sp--;
609                 goto run_BCO_fun;
610             } 
611             else if (arity == n) {
612                 goto run_BCO_fun;
613             }
614             else /* arity > n */ {
615                 // build a PAP and return it.
616                 StgPAP *pap;
617                 nat i;
618                 pap = (StgPAP *)allocate(PAP_sizeW(m));
619                 SET_HDR(pap, &stg_PAP_info,CCCS);
620                 pap->arity = arity - n;
621                 pap->fun = obj;
622                 pap->n_args = m;
623                 for (i = 0; i < m; i++) {
624                     pap->payload[i] = (StgClosure *)Sp[i];
625                 }
626                 obj = (StgClosure *)pap;
627                 Sp += m;
628                 goto do_return;
629             }
630         }
631
632         // No point in us applying machine-code functions
633         default:
634         defer_apply_to_sched:
635             Sp -= 2;
636             Sp[1] = (W_)obj;
637             Sp[0] = (W_)&stg_enter_info;
638             RETURN_TO_SCHEDULER_NO_PAUSE(ThreadRunGHC, ThreadYielding);
639     }
640
641     // ------------------------------------------------------------------------
642     // Ok, we now have a bco (obj), and its arguments are all on the
643     // stack.  We can start executing the byte codes.
644     //
645     // The stack is in one of two states.  First, if this BCO is a
646     // function:
647     //
648     //    |     ....      |
649     //    +---------------+
650     //    |     arg2      |
651     //    +---------------+
652     //    |     arg1      |
653     //    +---------------+
654     //
655     // Second, if this BCO is a continuation:
656     //
657     //    |     ....      |
658     //    +---------------+
659     //    |     fv2       |
660     //    +---------------+
661     //    |     fv1       |
662     //    +---------------+
663     //    |     BCO       |
664     //    +---------------+
665     //    | stg_ctoi_ret_ |
666     //    +---------------+
667     //    |    retval     |
668     //    +---------------+
669     // 
670     // where retval is the value being returned to this continuation.
671     // In the event of a stack check, heap check, or context switch,
672     // we need to leave the stack in a sane state so the garbage
673     // collector can find all the pointers.
674     //
675     //  (1) BCO is a function:  the BCO's bitmap describes the
676     //      pointerhood of the arguments.
677     //
678     //  (2) BCO is a continuation: BCO's bitmap describes the
679     //      pointerhood of the free variables.
680     //
681     // Sadly we have three different kinds of stack/heap/cswitch check
682     // to do:
683
684
685 run_BCO_return:
686     // Heap check
687     if (doYouWantToGC()) {
688         Sp--; Sp[0] = (W_)&stg_enter_info;
689         RETURN_TO_SCHEDULER(ThreadInterpret, HeapOverflow);
690     }
691     // Stack checks aren't necessary at return points, the stack use
692     // is aggregated into the enclosing function entry point.
693
694     goto run_BCO;
695     
696 run_BCO_return_unboxed:
697     // Heap check
698     if (doYouWantToGC()) {
699         RETURN_TO_SCHEDULER(ThreadInterpret, HeapOverflow);
700     }
701     // Stack checks aren't necessary at return points, the stack use
702     // is aggregated into the enclosing function entry point.
703
704     goto run_BCO;
705     
706 run_BCO_fun:
707     IF_DEBUG(sanity,
708              Sp -= 2; 
709              Sp[1] = (W_)obj; 
710              Sp[0] = (W_)&stg_apply_interp_info;
711              checkStackChunk(Sp,SpLim);
712              Sp += 2;
713         );
714
715     // Heap check
716     if (doYouWantToGC()) {
717         Sp -= 2; 
718         Sp[1] = (W_)obj; 
719         Sp[0] = (W_)&stg_apply_interp_info; // placeholder, really
720         RETURN_TO_SCHEDULER(ThreadInterpret, HeapOverflow);
721     }
722     
723     // Stack check
724     if (Sp - INTERP_STACK_CHECK_THRESH < SpLim) {
725         Sp -= 2; 
726         Sp[1] = (W_)obj; 
727         Sp[0] = (W_)&stg_apply_interp_info; // placeholder, really
728         RETURN_TO_SCHEDULER(ThreadInterpret, StackOverflow);
729     }
730
731     goto run_BCO;
732     
733     // Now, actually interpret the BCO... (no returning to the
734     // scheduler again until the stack is in an orderly state).
735 run_BCO:
736     INTERP_TICK(it_BCO_entries);
737     {
738         register int       bciPtr     = 1; /* instruction pointer */
739         register StgWord16 bci;
740         register StgBCO*   bco        = (StgBCO*)obj;
741         register StgWord16* instrs    = (StgWord16*)(bco->instrs->payload);
742         register StgWord*  literals   = (StgWord*)(&bco->literals->payload[0]);
743         register StgPtr*   ptrs       = (StgPtr*)(&bco->ptrs->payload[0]);
744
745 #ifdef INTERP_STATS
746         it_lastopc = 0; /* no opcode */
747 #endif
748
749     nextInsn:
750         ASSERT(bciPtr <= instrs[0]);
751         IF_DEBUG(interpreter,
752                  //if (do_print_stack) {
753                  //debugBelch("\n-- BEGIN stack\n");
754                  //printStack(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size,iSu);
755                  //debugBelch("-- END stack\n\n");
756                  //}
757                  debugBelch("Sp = %p   pc = %d      ", Sp, bciPtr);
758                  disInstr(bco,bciPtr);
759                  if (0) { int i;
760                  debugBelch("\n");
761                  for (i = 8; i >= 0; i--) {
762                      debugBelch("%d  %p\n", i, (StgPtr)(*(Sp+i)));
763                  }
764                  debugBelch("\n");
765                  }
766                  //if (do_print_stack) checkStack(Sp,cap->r.rCurrentTSO->stack+cap->r.rCurrentTSO->stack_size,iSu);
767             );
768
769
770         INTERP_TICK(it_insns);
771
772 #ifdef INTERP_STATS
773         ASSERT( (int)instrs[bciPtr] >= 0 && (int)instrs[bciPtr] < 27 );
774         it_ofreq[ (int)instrs[bciPtr] ] ++;
775         it_oofreq[ it_lastopc ][ (int)instrs[bciPtr] ] ++;
776         it_lastopc = (int)instrs[bciPtr];
777 #endif
778
779         bci = BCO_NEXT;
780     /* We use the high 8 bits for flags, only the highest of which is
781      * currently allocated */
782     ASSERT((bci & 0xFF00) == (bci & 0x8000));
783
784     switch (bci & 0xFF) {
785
786         /* check for a breakpoint on the beginning of a let binding */
787         case bci_BRK_FUN: 
788         {
789             int arg1_brk_array, arg2_array_index, arg3_freeVars;
790             StgArrWords *breakPoints;
791             int returning_from_break;     // are we resuming execution from a breakpoint?
792                                           //  if yes, then don't break this time around
793             StgClosure *ioAction;         // the io action to run at a breakpoint
794
795             StgAP_STACK *new_aps;         // a closure to save the top stack frame on the heap
796             int i;
797             int size_words;
798
799             arg1_brk_array      = BCO_NEXT;  // 1st arg of break instruction
800             arg2_array_index    = BCO_NEXT;  // 2nd arg of break instruction
801             arg3_freeVars       = BCO_NEXT;  // 3rd arg of break instruction
802
803             // check if we are returning from a breakpoint - this info
804             // is stored in the flags field of the current TSO
805             returning_from_break = cap->r.rCurrentTSO->flags & TSO_STOPPED_ON_BREAKPOINT; 
806
807             // if we are returning from a break then skip this section
808             // and continue executing
809             if (!returning_from_break)
810             {
811                breakPoints = (StgArrWords *) BCO_PTR(arg1_brk_array);
812
813                // stop the current thread if either the
814                // "rts_stop_next_breakpoint" flag is true OR if the
815                // breakpoint flag for this particular expression is
816                // true
817                if (rts_stop_next_breakpoint == rtsTrue || 
818                    breakPoints->payload[arg2_array_index] == rtsTrue)
819                {
820                   // make sure we don't automatically stop at the
821                   // next breakpoint
822                   rts_stop_next_breakpoint = rtsFalse;
823
824                   // allocate memory for a new AP_STACK, enough to
825                   // store the top stack frame plus an
826                   // stg_apply_interp_info pointer and a pointer to
827                   // the BCO
828                   size_words = BCO_BITMAP_SIZE(obj) + 2;
829                   new_aps = (StgAP_STACK *) allocate (AP_STACK_sizeW(size_words));
830                   SET_HDR(new_aps,&stg_AP_STACK_info,CCS_SYSTEM); 
831                   new_aps->size = size_words;
832                   new_aps->fun = &stg_dummy_ret_closure; 
833
834                   // fill in the payload of the AP_STACK 
835                   new_aps->payload[0] = (StgClosure *)&stg_apply_interp_info;
836                   new_aps->payload[1] = (StgClosure *)obj;
837
838                   // copy the contents of the top stack frame into the AP_STACK
839                   for (i = 2; i < size_words; i++)
840                   {
841                      new_aps->payload[i] = (StgClosure *)Sp[i-2];
842                   }
843
844                   // prepare the stack so that we can call the
845                   // rts_breakpoint_io_action and ensure that the stack is
846                   // in a reasonable state for the GC and so that
847                   // execution of this BCO can continue when we resume
848                   ioAction = (StgClosure *) deRefStablePtr (rts_breakpoint_io_action);
849                   Sp -= 8;
850                   Sp[7] = (W_)obj;   
851                   Sp[6] = (W_)&stg_apply_interp_info;
852                   Sp[5] = (W_)new_aps;                 // the AP_STACK
853                   Sp[4] = (W_)BCO_PTR(arg3_freeVars);  // the info about local vars of the breakpoint
854                   Sp[3] = (W_)False_closure;            // True <=> a breakpoint
855                   Sp[2] = (W_)&stg_ap_pppv_info;
856                   Sp[1] = (W_)ioAction;                // apply the IO action to its two arguments above
857                   Sp[0] = (W_)&stg_enter_info;         // get ready to run the IO action
858
859                   // set the flag in the TSO to say that we are now
860                   // stopping at a breakpoint so that when we resume
861                   // we don't stop on the same breakpoint that we
862                   // already stopped at just now
863                   cap->r.rCurrentTSO->flags |= TSO_STOPPED_ON_BREAKPOINT;
864
865                   // stop this thread and return to the scheduler -
866                   // eventually we will come back and the IO action on
867                   // the top of the stack will be executed
868                   RETURN_TO_SCHEDULER_NO_PAUSE(ThreadRunGHC, ThreadYielding);
869                }
870             }
871             // record that this thread is not stopped at a breakpoint anymore
872             cap->r.rCurrentTSO->flags &= ~TSO_STOPPED_ON_BREAKPOINT;
873
874             // continue normal execution of the byte code instructions
875             goto nextInsn;
876         }
877
878         case bci_STKCHECK: {
879             // Explicit stack check at the beginning of a function
880             // *only* (stack checks in case alternatives are
881             // propagated to the enclosing function).
882             StgWord stk_words_reqd = BCO_GET_LARGE_ARG + 1;
883             if (Sp - stk_words_reqd < SpLim) {
884                 Sp -= 2; 
885                 Sp[1] = (W_)obj; 
886                 Sp[0] = (W_)&stg_apply_interp_info;
887                 RETURN_TO_SCHEDULER(ThreadInterpret, StackOverflow);
888             } else {
889                 goto nextInsn;
890             }
891         }
892
893         case bci_PUSH_L: {
894             int o1 = BCO_NEXT;
895             Sp[-1] = Sp[o1];
896             Sp--;
897             goto nextInsn;
898         }
899
900         case bci_PUSH_LL: {
901             int o1 = BCO_NEXT;
902             int o2 = BCO_NEXT;
903             Sp[-1] = Sp[o1];
904             Sp[-2] = Sp[o2];
905             Sp -= 2;
906             goto nextInsn;
907         }
908
909         case bci_PUSH_LLL: {
910             int o1 = BCO_NEXT;
911             int o2 = BCO_NEXT;
912             int o3 = BCO_NEXT;
913             Sp[-1] = Sp[o1];
914             Sp[-2] = Sp[o2];
915             Sp[-3] = Sp[o3];
916             Sp -= 3;
917             goto nextInsn;
918         }
919
920         case bci_PUSH_G: {
921             int o1 = BCO_NEXT;
922             Sp[-1] = BCO_PTR(o1);
923             Sp -= 1;
924             goto nextInsn;
925         }
926
927         case bci_PUSH_ALTS: {
928             int o_bco  = BCO_NEXT;
929             Sp[-2] = (W_)&stg_ctoi_R1p_info;
930             Sp[-1] = BCO_PTR(o_bco);
931             Sp -= 2;
932             goto nextInsn;
933         }
934
935         case bci_PUSH_ALTS_P: {
936             int o_bco  = BCO_NEXT;
937             Sp[-2] = (W_)&stg_ctoi_R1unpt_info;
938             Sp[-1] = BCO_PTR(o_bco);
939             Sp -= 2;
940             goto nextInsn;
941         }
942
943         case bci_PUSH_ALTS_N: {
944             int o_bco  = BCO_NEXT;
945             Sp[-2] = (W_)&stg_ctoi_R1n_info;
946             Sp[-1] = BCO_PTR(o_bco);
947             Sp -= 2;
948             goto nextInsn;
949         }
950
951         case bci_PUSH_ALTS_F: {
952             int o_bco  = BCO_NEXT;
953             Sp[-2] = (W_)&stg_ctoi_F1_info;
954             Sp[-1] = BCO_PTR(o_bco);
955             Sp -= 2;
956             goto nextInsn;
957         }
958
959         case bci_PUSH_ALTS_D: {
960             int o_bco  = BCO_NEXT;
961             Sp[-2] = (W_)&stg_ctoi_D1_info;
962             Sp[-1] = BCO_PTR(o_bco);
963             Sp -= 2;
964             goto nextInsn;
965         }
966
967         case bci_PUSH_ALTS_L: {
968             int o_bco  = BCO_NEXT;
969             Sp[-2] = (W_)&stg_ctoi_L1_info;
970             Sp[-1] = BCO_PTR(o_bco);
971             Sp -= 2;
972             goto nextInsn;
973         }
974
975         case bci_PUSH_ALTS_V: {
976             int o_bco  = BCO_NEXT;
977             Sp[-2] = (W_)&stg_ctoi_V_info;
978             Sp[-1] = BCO_PTR(o_bco);
979             Sp -= 2;
980             goto nextInsn;
981         }
982
983         case bci_PUSH_APPLY_N:
984             Sp--; Sp[0] = (W_)&stg_ap_n_info;
985             goto nextInsn;
986         case bci_PUSH_APPLY_V:
987             Sp--; Sp[0] = (W_)&stg_ap_v_info;
988             goto nextInsn;
989         case bci_PUSH_APPLY_F:
990             Sp--; Sp[0] = (W_)&stg_ap_f_info;
991             goto nextInsn;
992         case bci_PUSH_APPLY_D:
993             Sp--; Sp[0] = (W_)&stg_ap_d_info;
994             goto nextInsn;
995         case bci_PUSH_APPLY_L:
996             Sp--; Sp[0] = (W_)&stg_ap_l_info;
997             goto nextInsn;
998         case bci_PUSH_APPLY_P:
999             Sp--; Sp[0] = (W_)&stg_ap_p_info;
1000             goto nextInsn;
1001         case bci_PUSH_APPLY_PP:
1002             Sp--; Sp[0] = (W_)&stg_ap_pp_info;
1003             goto nextInsn;
1004         case bci_PUSH_APPLY_PPP:
1005             Sp--; Sp[0] = (W_)&stg_ap_ppp_info;
1006             goto nextInsn;
1007         case bci_PUSH_APPLY_PPPP:
1008             Sp--; Sp[0] = (W_)&stg_ap_pppp_info;
1009             goto nextInsn;
1010         case bci_PUSH_APPLY_PPPPP:
1011             Sp--; Sp[0] = (W_)&stg_ap_ppppp_info;
1012             goto nextInsn;
1013         case bci_PUSH_APPLY_PPPPPP:
1014             Sp--; Sp[0] = (W_)&stg_ap_pppppp_info;
1015             goto nextInsn;
1016             
1017         case bci_PUSH_UBX: {
1018             int i;
1019             int o_lits = BCO_NEXT;
1020             int n_words = BCO_NEXT;
1021             Sp -= n_words;
1022             for (i = 0; i < n_words; i++) {
1023                 Sp[i] = (W_)BCO_LIT(o_lits+i);
1024             }
1025             goto nextInsn;
1026         }
1027
1028         case bci_SLIDE: {
1029             int n  = BCO_NEXT;
1030             int by = BCO_NEXT;
1031             /* a_1, .. a_n, b_1, .. b_by, s => a_1, .. a_n, s */
1032             while(--n >= 0) {
1033                 Sp[n+by] = Sp[n];
1034             }
1035             Sp += by;
1036             INTERP_TICK(it_slides);
1037             goto nextInsn;
1038         }
1039
1040         case bci_ALLOC_AP: {
1041             StgAP* ap; 
1042             int n_payload = BCO_NEXT;
1043             ap = (StgAP*)allocate(AP_sizeW(n_payload));
1044             Sp[-1] = (W_)ap;
1045             ap->n_args = n_payload;
1046             SET_HDR(ap, &stg_AP_info, CCS_SYSTEM/*ToDo*/)
1047             Sp --;
1048             goto nextInsn;
1049         }
1050
1051         case bci_ALLOC_PAP: {
1052             StgPAP* pap; 
1053             int arity = BCO_NEXT;
1054             int n_payload = BCO_NEXT;
1055             pap = (StgPAP*)allocate(PAP_sizeW(n_payload));
1056             Sp[-1] = (W_)pap;
1057             pap->n_args = n_payload;
1058             pap->arity = arity;
1059             SET_HDR(pap, &stg_PAP_info, CCS_SYSTEM/*ToDo*/)
1060             Sp --;
1061             goto nextInsn;
1062         }
1063
1064         case bci_MKAP: {
1065             int i;
1066             int stkoff = BCO_NEXT;
1067             int n_payload = BCO_NEXT;
1068             StgAP* ap = (StgAP*)Sp[stkoff];
1069             ASSERT((int)ap->n_args == n_payload);
1070             ap->fun = (StgClosure*)Sp[0];
1071             
1072             // The function should be a BCO, and its bitmap should
1073             // cover the payload of the AP correctly.
1074             ASSERT(get_itbl(ap->fun)->type == BCO
1075                    && BCO_BITMAP_SIZE(ap->fun) == ap->n_args);
1076             
1077             for (i = 0; i < n_payload; i++)
1078                 ap->payload[i] = (StgClosure*)Sp[i+1];
1079             Sp += n_payload+1;
1080             IF_DEBUG(interpreter,
1081                      debugBelch("\tBuilt "); 
1082                      printObj((StgClosure*)ap);
1083                 );
1084             goto nextInsn;
1085         }
1086
1087         case bci_MKPAP: {
1088             int i;
1089             int stkoff = BCO_NEXT;
1090             int n_payload = BCO_NEXT;
1091             StgPAP* pap = (StgPAP*)Sp[stkoff];
1092             ASSERT((int)pap->n_args == n_payload);
1093             pap->fun = (StgClosure*)Sp[0];
1094             
1095             // The function should be a BCO
1096             ASSERT(get_itbl(pap->fun)->type == BCO);
1097             
1098             for (i = 0; i < n_payload; i++)
1099                 pap->payload[i] = (StgClosure*)Sp[i+1];
1100             Sp += n_payload+1;
1101             IF_DEBUG(interpreter,
1102                      debugBelch("\tBuilt "); 
1103                      printObj((StgClosure*)pap);
1104                 );
1105             goto nextInsn;
1106         }
1107
1108         case bci_UNPACK: {
1109             /* Unpack N ptr words from t.o.s constructor */
1110             int i;
1111             int n_words = BCO_NEXT;
1112             StgClosure* con = (StgClosure*)Sp[0];
1113             Sp -= n_words;
1114             for (i = 0; i < n_words; i++) {
1115                 Sp[i] = (W_)con->payload[i];
1116             }
1117             goto nextInsn;
1118         }
1119
1120         case bci_PACK: {
1121             int i;
1122             int o_itbl         = BCO_NEXT;
1123             int n_words        = BCO_NEXT;
1124             StgInfoTable* itbl = INFO_PTR_TO_STRUCT(BCO_LIT(o_itbl));
1125             int request        = CONSTR_sizeW( itbl->layout.payload.ptrs, 
1126                                                itbl->layout.payload.nptrs );
1127             StgClosure* con = (StgClosure*)allocate_NONUPD(request);
1128             ASSERT( itbl->layout.payload.ptrs + itbl->layout.payload.nptrs > 0);
1129             SET_HDR(con, (StgInfoTable*)BCO_LIT(o_itbl), CCS_SYSTEM/*ToDo*/);
1130             for (i = 0; i < n_words; i++) {
1131                 con->payload[i] = (StgClosure*)Sp[i];
1132             }
1133             Sp += n_words;
1134             Sp --;
1135             Sp[0] = (W_)con;
1136             IF_DEBUG(interpreter,
1137                      debugBelch("\tBuilt "); 
1138                      printObj((StgClosure*)con);
1139                 );
1140             goto nextInsn;
1141         }
1142
1143         case bci_TESTLT_P: {
1144             unsigned int discr  = BCO_NEXT;
1145             int failto = BCO_NEXT;
1146             StgClosure* con = (StgClosure*)Sp[0];
1147             if (GET_TAG(con) >= discr) {
1148                 bciPtr = failto;
1149             }
1150             goto nextInsn;
1151         }
1152
1153         case bci_TESTEQ_P: {
1154             unsigned int discr  = BCO_NEXT;
1155             int failto = BCO_NEXT;
1156             StgClosure* con = (StgClosure*)Sp[0];
1157             if (GET_TAG(con) != discr) {
1158                 bciPtr = failto;
1159             }
1160             goto nextInsn;
1161         }
1162
1163         case bci_TESTLT_I: {
1164             // There should be an Int at Sp[1], and an info table at Sp[0].
1165             int discr   = BCO_NEXT;
1166             int failto  = BCO_NEXT;
1167             I_ stackInt = (I_)Sp[1];
1168             if (stackInt >= (I_)BCO_LIT(discr))
1169                 bciPtr = failto;
1170             goto nextInsn;
1171         }
1172
1173         case bci_TESTEQ_I: {
1174             // There should be an Int at Sp[1], and an info table at Sp[0].
1175             int discr   = BCO_NEXT;
1176             int failto  = BCO_NEXT;
1177             I_ stackInt = (I_)Sp[1];
1178             if (stackInt != (I_)BCO_LIT(discr)) {
1179                 bciPtr = failto;
1180             }
1181             goto nextInsn;
1182         }
1183
1184         case bci_TESTLT_D: {
1185             // There should be a Double at Sp[1], and an info table at Sp[0].
1186             int discr   = BCO_NEXT;
1187             int failto  = BCO_NEXT;
1188             StgDouble stackDbl, discrDbl;
1189             stackDbl = PK_DBL( & Sp[1] );
1190             discrDbl = PK_DBL( & BCO_LIT(discr) );
1191             if (stackDbl >= discrDbl) {
1192                 bciPtr = failto;
1193             }
1194             goto nextInsn;
1195         }
1196
1197         case bci_TESTEQ_D: {
1198             // There should be a Double at Sp[1], and an info table at Sp[0].
1199             int discr   = BCO_NEXT;
1200             int failto  = BCO_NEXT;
1201             StgDouble stackDbl, discrDbl;
1202             stackDbl = PK_DBL( & Sp[1] );
1203             discrDbl = PK_DBL( & BCO_LIT(discr) );
1204             if (stackDbl != discrDbl) {
1205                 bciPtr = failto;
1206             }
1207             goto nextInsn;
1208         }
1209
1210         case bci_TESTLT_F: {
1211             // There should be a Float at Sp[1], and an info table at Sp[0].
1212             int discr   = BCO_NEXT;
1213             int failto  = BCO_NEXT;
1214             StgFloat stackFlt, discrFlt;
1215             stackFlt = PK_FLT( & Sp[1] );
1216             discrFlt = PK_FLT( & BCO_LIT(discr) );
1217             if (stackFlt >= discrFlt) {
1218                 bciPtr = failto;
1219             }
1220             goto nextInsn;
1221         }
1222
1223         case bci_TESTEQ_F: {
1224             // There should be a Float at Sp[1], and an info table at Sp[0].
1225             int discr   = BCO_NEXT;
1226             int failto  = BCO_NEXT;
1227             StgFloat stackFlt, discrFlt;
1228             stackFlt = PK_FLT( & Sp[1] );
1229             discrFlt = PK_FLT( & BCO_LIT(discr) );
1230             if (stackFlt != discrFlt) {
1231                 bciPtr = failto;
1232             }
1233             goto nextInsn;
1234         }
1235
1236         // Control-flow ish things
1237         case bci_ENTER:
1238             // Context-switch check.  We put it here to ensure that
1239             // the interpreter has done at least *some* work before
1240             // context switching: sometimes the scheduler can invoke
1241             // the interpreter with context_switch == 1, particularly
1242             // if the -C0 flag has been given on the cmd line.
1243             if (context_switch) {
1244                 Sp--; Sp[0] = (W_)&stg_enter_info;
1245                 RETURN_TO_SCHEDULER(ThreadInterpret, ThreadYielding);
1246             }
1247             goto eval;
1248
1249         case bci_RETURN:
1250             obj = (StgClosure *)Sp[0];
1251             Sp++;
1252             goto do_return;
1253
1254         case bci_RETURN_P:
1255             Sp--;
1256             Sp[0] = (W_)&stg_gc_unpt_r1_info;
1257             goto do_return_unboxed;
1258         case bci_RETURN_N:
1259             Sp--;
1260             Sp[0] = (W_)&stg_gc_unbx_r1_info;
1261             goto do_return_unboxed;
1262         case bci_RETURN_F:
1263             Sp--;
1264             Sp[0] = (W_)&stg_gc_f1_info;
1265             goto do_return_unboxed;
1266         case bci_RETURN_D:
1267             Sp--;
1268             Sp[0] = (W_)&stg_gc_d1_info;
1269             goto do_return_unboxed;
1270         case bci_RETURN_L:
1271             Sp--;
1272             Sp[0] = (W_)&stg_gc_l1_info;
1273             goto do_return_unboxed;
1274         case bci_RETURN_V:
1275             Sp--;
1276             Sp[0] = (W_)&stg_gc_void_info;
1277             goto do_return_unboxed;
1278
1279         case bci_SWIZZLE: {
1280             int stkoff = BCO_NEXT;
1281             signed short n = (signed short)(BCO_NEXT);
1282             Sp[stkoff] += (W_)n;
1283             goto nextInsn;
1284         }
1285
1286         case bci_CCALL: {
1287             void *tok;
1288             int stk_offset            = BCO_NEXT;
1289             int o_itbl                = BCO_NEXT;
1290             void(*marshall_fn)(void*) = (void (*)(void*))BCO_LIT(o_itbl);
1291             int ret_dyn_size = 
1292                 RET_DYN_BITMAP_SIZE + RET_DYN_NONPTR_REGS_SIZE
1293                 + sizeofW(StgRetDyn);
1294
1295 #ifdef THREADED_RTS
1296             // Threaded RTS:
1297             // Arguments on the TSO stack are not good, because garbage
1298             // collection might move the TSO as soon as we call
1299             // suspendThread below.
1300
1301             W_ arguments[stk_offset];
1302             
1303             memcpy(arguments, Sp, sizeof(W_) * stk_offset);
1304 #endif
1305
1306             // Restore the Haskell thread's current value of errno
1307             errno = cap->r.rCurrentTSO->saved_errno;
1308
1309             // There are a bunch of non-ptr words on the stack (the
1310             // ccall args, the ccall fun address and space for the
1311             // result), which we need to cover with an info table
1312             // since we might GC during this call.
1313             //
1314             // We know how many (non-ptr) words there are before the
1315             // next valid stack frame: it is the stk_offset arg to the
1316             // CCALL instruction.   So we build a RET_DYN stack frame
1317             // on the stack frame to describe this chunk of stack.
1318             //
1319             Sp -= ret_dyn_size;
1320             ((StgRetDyn *)Sp)->liveness = NO_PTRS | N_NONPTRS(stk_offset);
1321             ((StgRetDyn *)Sp)->info = (StgInfoTable *)&stg_gc_gen_info;
1322
1323             SAVE_STACK_POINTERS;
1324             tok = suspendThread(&cap->r);
1325
1326 #ifndef THREADED_RTS
1327             // Careful:
1328             // suspendThread might have shifted the stack
1329             // around (stack squeezing), so we have to grab the real
1330             // Sp out of the TSO to find the ccall args again.
1331
1332             marshall_fn ( (void*)(cap->r.rCurrentTSO->sp + ret_dyn_size) );
1333 #else
1334             // Threaded RTS:
1335             // We already made a copy of the arguments above.
1336
1337             marshall_fn ( arguments );
1338 #endif
1339
1340             // And restart the thread again, popping the RET_DYN frame.
1341             cap = (Capability *)((void *)((unsigned char*)resumeThread(tok) - sizeof(StgFunTable)));
1342             LOAD_STACK_POINTERS;
1343             Sp += ret_dyn_size;
1344             
1345             // Save the Haskell thread's current value of errno
1346             cap->r.rCurrentTSO->saved_errno = errno;
1347                 
1348 #ifdef THREADED_RTS
1349             // Threaded RTS:
1350             // Copy the "arguments", which might include a return value,
1351             // back to the TSO stack. It would of course be enough to
1352             // just copy the return value, but we don't know the offset.
1353             memcpy(Sp, arguments, sizeof(W_) * stk_offset);
1354 #endif
1355
1356             goto nextInsn;
1357         }
1358
1359         case bci_JMP: {
1360             /* BCO_NEXT modifies bciPtr, so be conservative. */
1361             int nextpc = BCO_NEXT;
1362             bciPtr     = nextpc;
1363             goto nextInsn;
1364         }
1365  
1366         case bci_CASEFAIL:
1367             barf("interpretBCO: hit a CASEFAIL");
1368             
1369             // Errors
1370         default: 
1371             barf("interpretBCO: unknown or unimplemented opcode %d",
1372                  (int)BCO_NEXT);
1373
1374         } /* switch on opcode */
1375     }
1376     }
1377
1378     barf("interpretBCO: fell off end of the interpreter");
1379 }