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