summaryrefslogtreecommitdiff
path: root/ghc/rts/Interpreter.c
blob: ea0757d2c1f8e4eb6baea015e8c33d71417fd80d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771

/* -----------------------------------------------------------------------------
 * Bytecode evaluator
 *
 * Copyright (c) 1994-2000.
 *
 * $RCSfile: Interpreter.c,v $
 * $Revision: 1.17 $
 * $Date: 2001/02/11 17:51:07 $
 * ---------------------------------------------------------------------------*/

#include "Rts.h"
#include "RtsAPI.h"
#include "RtsUtils.h"
#include "Closures.h"
#include "TSO.h"
#include "Schedule.h"
#include "RtsFlags.h"
#include "Storage.h"
#include "Updates.h"

#include "Bytecodes.h"
#include "Printer.h"
#include "Disassembler.h"
#include "Interpreter.h"


/* --------------------------------------------------------------------------
 * The new bytecode interpreter
 * ------------------------------------------------------------------------*/

/* The interpreter can be compiled so it just interprets BCOs and
   hands literally everything else to the scheduler.  This gives a
   "reference interpreter" which is correct but slow -- useful for
   debugging.  By default, we handle certain closures specially so as
   to dramatically cut down on the number of deferrals to the
   scheduler.  Ie normally you don't want REFERENCE_INTERPRETER to be
   defined. */

/* #define REFERENCE_INTERPRETER */

/* Gather stats about entry, opcode, opcode-pair frequencies.  For
   tuning the interpreter. */

/* #define INTERP_STATS */



/* iSp points to the lowest live word on the stack. */

#define StackWord(n)  iSp[n]
#define BCO_NEXT      instrs[bciPtr++]
#define BCO_PTR(n)    (W_)ptrs[n]
#define BCO_LIT(n)    (W_)literals[n]
#define BCO_ITBL(n)   itbls[n]

#define LOAD_STACK_POINTERS \
    iSp = cap->rCurrentTSO->sp; iSu = cap->rCurrentTSO->su;

#define SAVE_STACK_POINTERS \
    cap->rCurrentTSO->sp = iSp; cap->rCurrentTSO->su = iSu;

#define RETURN(retcode) \
   SAVE_STACK_POINTERS; return retcode;


static __inline__ StgPtr allocate_UPD ( int n_words )
{
   if (n_words - sizeofW(StgHeader) < MIN_UPD_SIZE)
      n_words = MIN_UPD_SIZE + sizeofW(StgHeader);
   return allocate(n_words);
}

static __inline__ StgPtr allocate_NONUPD ( int n_words )
{
   if (n_words - sizeofW(StgHeader) < MIN_NONUPD_SIZE)
      n_words = MIN_NONUPD_SIZE + sizeofW(StgHeader);
   return allocate(n_words);
}


#ifdef INTERP_STATS
/* Hacky stats, for tuning the interpreter ... */
int it_unknown_entries[N_CLOSURE_TYPES];
int it_total_unknown_entries;
int it_total_entries;

int it_retto_BCO;
int it_retto_UPDATE;
int it_retto_other;

int it_slides;
int it_insns;
int it_BCO_entries;

int it_ofreq[27];
int it_oofreq[27][27];
int it_lastopc;

void interp_startup ( void )
{
   int i, j;
   it_retto_BCO = it_retto_UPDATE = it_retto_other = 0;
   it_total_entries = it_total_unknown_entries = 0;
   for (i = 0; i < N_CLOSURE_TYPES; i++)
      it_unknown_entries[i] = 0;
   it_slides = it_insns = it_BCO_entries = 0;
   for (i = 0; i < 27; i++) it_ofreq[i] = 0;
   for (i = 0; i < 27; i++) 
     for (j = 0; j < 27; j++)
        it_oofreq[i][j] = 0;
   it_lastopc = 0;
}

void interp_shutdown ( void )
{
   int i, j, k, o_max, i_max, j_max;
   fprintf(stderr, "%d constrs entered -> (%d BCO, %d UPD, %d ???)\n",
                   it_retto_BCO + it_retto_UPDATE + it_retto_other,
                   it_retto_BCO, it_retto_UPDATE, it_retto_other );
   fprintf(stderr, "%d total entries, %d unknown entries \n", 
                   it_total_entries, it_total_unknown_entries);
   for (i = 0; i < N_CLOSURE_TYPES; i++) {
     if (it_unknown_entries[i] == 0) continue;
     fprintf(stderr, "   type %2d: unknown entries (%4.1f%%) == %d\n",
	     i, 100.0 * ((double)it_unknown_entries[i]) / 
                        ((double)it_total_unknown_entries),
             it_unknown_entries[i]);
   }
   fprintf(stderr, "%d insns, %d slides, %d BCO_entries\n", 
                   it_insns, it_slides, it_BCO_entries);
   for (i = 0; i < 27; i++) 
      fprintf(stderr, "opcode %2d got %d\n", i, it_ofreq[i] );

   for (k = 1; k < 20; k++) {
      o_max = 0;
      i_max = j_max = 0;
      for (i = 0; i < 27; i++) {
         for (j = 0; j < 27; j++) {
	    if (it_oofreq[i][j] > o_max) {
               o_max = it_oofreq[i][j];
	       i_max = i; j_max = j;
	    }
	 }
      }
      
      fprintf ( stderr, "%d:  count (%4.1f%%) %6d   is %d then %d\n",
                k, ((double)o_max) * 100.0 / ((double)it_insns), o_max,
                   i_max, j_max );
      it_oofreq[i_max][j_max] = 0;

   }
}
#endif


StgThreadReturnCode interpretBCO ( Capability* cap )
{
   /* On entry, the closure to interpret is on the top of the
      stack. */
 
   /* Use of register here is primarily to make it clear to compilers
      that these entities are non-aliasable.
   */
    register W_*              iSp;    /* local state -- stack pointer */
    register StgUpdateFrame*  iSu;    /* local state -- frame pointer */
    register StgPtr           iSpLim; /* local state -- stack lim pointer */
    register StgClosure*      obj;

    LOAD_STACK_POINTERS;

    /* We don't change this ... */
    iSpLim = cap->rCurrentTSO->stack + RESERVED_STACK_WORDS;

    /* Main object-entering loop.  Object to be entered is on top of
       stack. */
    nextEnter:

    obj = (StgClosure*)StackWord(0); iSp++;

    nextEnter_obj:

#   ifdef INTERP_STATS
    it_total_entries++;
#   endif

    IF_DEBUG(evaluator,
             fprintf(stderr, 
             "\n---------------------------------------------------------------\n");
             fprintf(stderr,"Entering: "); printObj(obj);
             fprintf(stderr,"iSp = %p\tiSu = %p\n", iSp, iSu);
             fprintf(stderr, "\n" );

	     //	     checkSanity(1);
	     //             iSp--; StackWord(0) = obj;
	     //             checkStack(iSp,cap->rCurrentTSO->stack+cap->rCurrentTSO->stack_size,iSu);
	     //             iSp++;

             printStack(iSp,cap->rCurrentTSO->stack+cap->rCurrentTSO->stack_size,iSu);
             fprintf(stderr, "\n\n");
            );



    switch ( get_itbl(obj)->type ) {

       case INVALID_OBJECT:
               barf("Invalid object %p",(StgPtr)obj);

#      ifndef REFERENCE_INTERPRETER

       case IND:
       case IND_OLDGEN:
       case IND_PERM:
       case IND_OLDGEN_PERM:
       case IND_STATIC:
       { 
          obj = ((StgInd*)obj)->indirectee;
          goto nextEnter_obj;
       }

       case CONSTR:
       case CONSTR_1_0:
       case CONSTR_0_1:
       case CONSTR_2_0:
       case CONSTR_1_1:
       case CONSTR_0_2:
       case CONSTR_INTLIKE:
       case CONSTR_CHARLIKE:
       case CONSTR_STATIC:
       case CONSTR_NOCAF_STATIC:
       nextEnter_obj_CONSTR:
       {
          StgInfoTable* ret_itbl = (StgInfoTable*)StackWord(0);
          if (ret_itbl == (StgInfoTable*)&stg_ctoi_ret_R1p_info) {
#            ifdef INTERP_STATS
             it_retto_BCO++;
#            endif
             /* Returning this constr to a BCO.  Push the constr on
                the stack and enter the return continuation BCO, which
                is immediately underneath ret_itbl. */
             StackWord(-1) = (W_)obj;
             obj = (StgClosure*)StackWord(1);
             iSp --;
	     if (get_itbl(obj)->type == BCO) 
                goto nextEnter_obj_BCO; /* fast-track common case */
             else
                goto nextEnter_obj; /* a safe fallback */
	  } else
	  if (ret_itbl == (StgInfoTable*)&stg_upd_frame_info) {
#            ifdef INTERP_STATS
	     it_retto_UPDATE++;
#            endif
             /* Returning this constr to an update frame.  Do the
                update and re-enter the constr. */
             ASSERT((W_*)iSu == iSp);
             UPD_IND(iSu->updatee, obj); 
             iSu = iSu->link;
             iSp += sizeofW(StgUpdateFrame);
             goto nextEnter_obj_CONSTR;
          }
#         ifdef INTERP_STATS
          else it_retto_other++;
#         endif
          goto defer_to_sched;
       }

       case AP_UPD:
       /* Copied from stg_AP_UPD_entry. */
       { 
          nat i, words;
          StgAP_UPD *ap = (StgAP_UPD*)obj;
          words = ap->n_args;

	  /* Stack check.  If a stack overflow might occur, don't enter
             the closure; let the scheduler handle it instead. */
          if (iSp - (words+sizeofW(StgUpdateFrame)) < iSpLim)
             goto defer_to_sched;

	  /* Ok; we're safe.  Party on.  Push an update frame. */
          iSp -= sizeofW(StgUpdateFrame);
          {
              StgUpdateFrame *__frame;
              __frame = (StgUpdateFrame *)iSp;
              SET_INFO(__frame, (StgInfoTable *)&stg_upd_frame_info);
              __frame->link = iSu;
              __frame->updatee = (StgClosure *)(ap);
              iSu = __frame;
          }

          /* Reload the stack */
          iSp -= words;
          for (i=0; i < words; i++) StackWord(i) = (W_)ap->payload[i];

          obj = (StgClosure*)ap->fun;
          goto nextEnter_obj;
       }

       case PAP:
       /* Copied from stg_PAP_entry. */
       {
          nat     words, i;
          StgPAP* pap = (StgPAP *)obj;
  
          /*
           * remove any update frames on the top of the stack, by just
           * performing the update here.
           */
          while ((W_)iSu - (W_)iSp == 0) {

             switch (get_itbl(iSu)->type) {

             case UPDATE_FRAME:
                /* We're sitting on top of an update frame, so let's
                   do the business. */
                UPD_IND(iSu->updatee, pap);
                iSu = iSu->link;
                iSp += sizeofW(StgUpdateFrame);
                continue;

             case SEQ_FRAME:
                /* Too complicated ... adopt the Usual Solution. */
                fprintf(stderr, "!!! SEQ frame in PAP update\n");
                goto defer_to_sched;

             case CATCH_FRAME:
                /* can't happen, see stg_update_PAP */
                barf("interpretBCO: PAP_entry: CATCH_FRAME");

             default:
                barf("interpretBCO: PAP_entry: strange activation record");
             }
          }

          words = pap->n_args;

	  /* Stack check.  If a stack overflow might occur, don't enter
             the closure; let the scheduler handle it instead. */
          if (iSp - words < iSpLim)
             goto defer_to_sched;

          /* Ok; safe. */         
          iSp -= words;
          for (i=0; i < words; i++) StackWord(i) = (W_)pap->payload[i];

          obj = (StgClosure*)pap->fun;
          goto nextEnter_obj;
       }

#      endif /* ndef REFERENCE_INTERPRETER */

       case BCO:
       /* ---------------------------------------------------- */
       /* Start of the bytecode interpreter                    */
       /* ---------------------------------------------------- */
       nextEnter_obj_BCO:
#      ifdef INTERP_STATS
       it_BCO_entries++;
#      endif
       {
          int do_print_stack = 1;
          register int       bciPtr     = 1; /* instruction pointer */
          register StgBCO*   bco        = (StgBCO*)obj;
          register UShort*   instrs     = (UShort*)(&bco->instrs->payload[0]);
          register StgWord*  literals   = (StgWord*)(&bco->literals->payload[0]);
          register StgPtr*   ptrs       = (StgPtr*)(&bco->ptrs->payload[0]);
          register StgInfoTable** itbls = (StgInfoTable**)
                                             (&bco->itbls->payload[0]);

          /* Heap check */
          if (doYouWantToGC()) {
	     iSp--; StackWord(0) = (W_)bco;
             cap->rCurrentTSO->what_next = ThreadEnterInterp;
             RETURN(HeapOverflow);
          }

          /* "Standard" stack check */
          if (iSp - (INTERP_STACK_CHECK_THRESH+1) < iSpLim) {
             iSp--;
             StackWord(0) = (W_)obj;
             cap->rCurrentTSO->what_next = ThreadEnterInterp;
             RETURN(StackOverflow);
          }

          /* Context-switch check */
          if (context_switch) {
             iSp--;
             StackWord(0) = (W_)obj;
             cap->rCurrentTSO->what_next = ThreadEnterInterp;
             RETURN(ThreadYielding);
	  }
 

#         ifdef INTERP_STATS
          it_lastopc = 0; /* no opcode */
#         endif

          nextInsn:

          ASSERT(bciPtr <= instrs[0]);
          IF_DEBUG(evaluator,
		   //if (do_print_stack) {
		   //fprintf(stderr, "\n-- BEGIN stack\n");
		   //printStack(iSp,cap->rCurrentTSO->stack+cap->rCurrentTSO->stack_size,iSu);
		   //fprintf(stderr, "-- END stack\n\n");
		   //}
                   do_print_stack = 1;
		   fprintf(stderr,"iSp = %p   iSu = %p   pc = %d      ", iSp, iSu, bciPtr);
                   disInstr(bco,bciPtr);
                    if (0) { int i;
                             fprintf(stderr,"\n");
                             for (i = 8; i >= 0; i--) 
                                fprintf(stderr, "%d  %p\n", i, (StgPtr)(*(iSp+i)));
                             fprintf(stderr,"\n");
                           }
		    //if (do_print_stack) checkStack(iSp,cap->rCurrentTSO->stack+cap->rCurrentTSO->stack_size,iSu);
                  );

#         ifdef INTERP_STATS
          it_insns++;
          ASSERT( (int)instrs[bciPtr] >= 0 && (int)instrs[bciPtr] < 27 );
          it_ofreq[ (int)instrs[bciPtr] ] ++;
          it_oofreq[ it_lastopc ][ (int)instrs[bciPtr] ] ++;
          it_lastopc = (int)instrs[bciPtr];
#         endif

          switch (BCO_NEXT) {

              case bci_STKCHECK: {
		/* An explicit stack check; we hope these will be
                   rare. */
                int stk_words_reqd = BCO_NEXT + 1;
                if (iSp - stk_words_reqd < iSpLim) {
                   iSp--;
                   StackWord(0) = (W_)obj;
                   cap->rCurrentTSO->what_next = ThreadEnterInterp;
                   RETURN(StackOverflow);
                }
                goto nextInsn;
              }
              case bci_ARGCHECK: {
                 int i;
                 StgPAP* pap;
                 int arg_words_reqd = BCO_NEXT;
                 int arg_words_avail = ((W_*)iSu) - ((W_*)iSp);
                 if (arg_words_avail >= arg_words_reqd) goto nextInsn;

#                ifndef REFERENCE_INTERPRETER

                 /* Optimisation: if there are no args avail and the
                    t-o-s is an update frame, do the update, and
                    re-enter the object. */
                 if (arg_words_avail == 0 
                    && get_itbl(iSu)->type == UPDATE_FRAME) {
                    UPD_IND(iSu->updatee, obj); 
                    iSu = iSu->link;
                    iSp += sizeofW(StgUpdateFrame);
                    goto nextEnter_obj_BCO;
		 }

#                endif /* ndef REFERENCE_INTERPRETER */

                 /* Handle arg check failure.  General case: copy the
                    spare args into a PAP frame. */
                 pap = (StgPAP*)allocate_UPD(PAP_sizeW(arg_words_avail));
                 SET_HDR(pap,&stg_PAP_info,CCS_SYSTEM/*ToDo*/);
                 pap->n_args = arg_words_avail;
                 pap->fun = obj;
                 for (i = 0; i < arg_words_avail; i++)
                    pap->payload[i] = (StgClosure*)StackWord(i);

                 /* Push on the stack and defer to the scheduler. */
                 iSp = (StgPtr)iSu;
                 iSp --;
                 StackWord(0) = (W_)pap;
		 IF_DEBUG(evaluator,
                          fprintf(stderr,"\tBuilt "); 
                          printObj((StgClosure*)pap);
		         );
                 cap->rCurrentTSO->what_next = ThreadEnterGHC;
                 RETURN(ThreadYielding);
              }
              case bci_PUSH_L: {
                 int o1 = BCO_NEXT;
                 ASSERT((W_*)iSp+o1 < (W_*)iSu);
                 StackWord(-1) = StackWord(o1);
                 iSp--;
                 do_print_stack = 0;
                 goto nextInsn;
              }
              case bci_PUSH_LL: {
                 int o1 = BCO_NEXT;
                 int o2 = BCO_NEXT;
                 ASSERT((W_*)iSp+o1 < (W_*)iSu);
                 ASSERT((W_*)iSp+o2 < (W_*)iSu);
                 StackWord(-1) = StackWord(o1);
                 StackWord(-2) = StackWord(o2);
                 iSp -= 2;
                 goto nextInsn;
              }
              case bci_PUSH_LLL: {
                 int o1 = BCO_NEXT;
                 int o2 = BCO_NEXT;
                 int o3 = BCO_NEXT;
                 ASSERT((W_*)iSp+o1 < (W_*)iSu);
                 ASSERT((W_*)iSp+o2 < (W_*)iSu);
                 ASSERT((W_*)iSp+o3 < (W_*)iSu);
                 StackWord(-1) = StackWord(o1);
                 StackWord(-2) = StackWord(o2);
                 StackWord(-3) = StackWord(o3);
                 iSp -= 3;
                 goto nextInsn;
              }
              case bci_PUSH_G: {
                 int o1 = BCO_NEXT;
                 StackWord(-1) = BCO_PTR(o1);
                 iSp -= 1;
                 goto nextInsn;
              }
              case bci_PUSH_AS: {
                 int o_bco  = BCO_NEXT;
                 int o_itbl = BCO_NEXT;
                 StackWord(-2) = BCO_LIT(o_itbl);
                 StackWord(-1) = BCO_PTR(o_bco);
                 iSp -= 2;
                 goto nextInsn;
              }
              case bci_PUSH_UBX: {
                 int i;
                 int o_lits = BCO_NEXT;
                 int n_words = BCO_NEXT;
                 iSp -= n_words;
                 for (i = 0; i < n_words; i++)
                    StackWord(i) = BCO_LIT(o_lits+i);
                 do_print_stack = 0;
                 goto nextInsn;
              }
              case bci_PUSH_TAG: {
                 W_ tag = (W_)(BCO_NEXT);
                 StackWord(-1) = tag;
                 iSp --;
                 goto nextInsn;
              }
              case bci_SLIDE: {
                 int n  = BCO_NEXT;
                 int by = BCO_NEXT;
                 ASSERT((W_*)iSp+n+by <= (W_*)iSu);
                 /* a_1, .. a_n, b_1, .. b_by, s => a_1, .. a_n, s */
                 while(--n >= 0) {
                    StackWord(n+by) = StackWord(n);
                 }
                 iSp += by;
#                ifdef INTERP_STATS
                 it_slides++;
#                endif
                 goto nextInsn;
              }
              case bci_ALLOC: {
                 StgAP_UPD* ap; 
                 int n_payload = BCO_NEXT - 1;
                 int request   = AP_sizeW(n_payload);
                 ap = (StgAP_UPD*)allocate_UPD(request);
                 StackWord(-1) = (W_)ap;
                 ap->n_args = n_payload;
                 SET_HDR(ap, &stg_AP_UPD_info, ??)
                 iSp --;
                 goto nextInsn;
              }
              case bci_MKAP: {
                 int i;
                 int stkoff = BCO_NEXT;
                 int n_payload = BCO_NEXT - 1;
                 StgAP_UPD* ap = (StgAP_UPD*)StackWord(stkoff);
                 ASSERT((int)ap->n_args == n_payload);
                 ap->fun = (StgClosure*)StackWord(0);
                 for (i = 0; i < n_payload; i++)
                    ap->payload[i] = (StgClosure*)StackWord(i+1);
                 iSp += n_payload+1;
		 IF_DEBUG(evaluator,
                          fprintf(stderr,"\tBuilt "); 
                          printObj((StgClosure*)ap);
		         );
                 goto nextInsn;
              }
              case bci_UNPACK: {
                 /* Unpack N ptr words from t.o.s constructor */
                 /* The common case ! */
                 int i;
                 int n_words = BCO_NEXT;
                 StgClosure* con = (StgClosure*)StackWord(0);
                 iSp -= n_words;
                 for (i = 0; i < n_words; i++)
                    StackWord(i) = (W_)con->payload[i];
                 goto nextInsn;
              }
              case bci_UPK_TAG: {
                 /* Unpack N (non-ptr) words from offset M in the
                    constructor K words down the stack, and then push
                    N as a tag, on top of it.  Slow but general; we
                    hope it will be the rare case. */
                 int i;                
                 int n_words = BCO_NEXT;
                 int con_off = BCO_NEXT;
                 int stk_off = BCO_NEXT;
                 StgClosure* con = (StgClosure*)StackWord(stk_off);
                 iSp -= n_words;
                 for (i = 0; i < n_words; i++) 
                    StackWord(i) = (W_)con->payload[con_off + i];
                 iSp --;
                 StackWord(0) = n_words;
                 goto nextInsn;
              }
              case bci_PACK: {
                 int i;
                 int o_itbl         = BCO_NEXT;
                 int n_words        = BCO_NEXT;
                 StgInfoTable* itbl = INFO_PTR_TO_STRUCT(BCO_ITBL(o_itbl));
                 int request        = CONSTR_sizeW( itbl->layout.payload.ptrs, 
                                                    itbl->layout.payload.nptrs );
                 StgClosure* con = (StgClosure*)allocate_NONUPD(request);
                 ASSERT( itbl->layout.payload.ptrs + itbl->layout.payload.nptrs > 0);
                 SET_HDR(con, BCO_ITBL(o_itbl), CCS_SYSTEM/*ToDo*/);
                 for (i = 0; i < n_words; i++)
                    con->payload[i] = (StgClosure*)StackWord(i);
                 iSp += n_words;
                 iSp --;
                 StackWord(0) = (W_)con;
		 IF_DEBUG(evaluator,
                          fprintf(stderr,"\tBuilt "); 
                          printObj((StgClosure*)con);
		         );
                 goto nextInsn;
              }
              case bci_TESTLT_P: {
                 int discr  = BCO_NEXT;
                 int failto = BCO_NEXT;
                 StgClosure* con = (StgClosure*)StackWord(0);
                 if (constrTag(con) >= discr)
                    bciPtr = failto;
                 goto nextInsn;
              }
              case bci_TESTEQ_P: {
                 int discr  = BCO_NEXT;
                 int failto = BCO_NEXT;
                 StgClosure* con = (StgClosure*)StackWord(0);
                 if (constrTag(con) != discr)
                    bciPtr = failto;
                 goto nextInsn;
              }
              case bci_TESTLT_I: {
                 /* The top thing on the stack should be a tagged int. */
                 int discr   = BCO_NEXT;
                 int failto  = BCO_NEXT;
                 I_ stackInt = (I_)StackWord(1);
                 ASSERT(1 == StackWord(0));
                 if (stackInt >= (I_)BCO_LIT(discr))
                    bciPtr = failto;
                 goto nextInsn;
              }
              case bci_TESTEQ_I: {
                 /* The top thing on the stack should be a tagged int. */
                 int discr   = BCO_NEXT;
                 int failto  = BCO_NEXT;
                 I_ stackInt = (I_)StackWord(1);
                 ASSERT(1 == StackWord(0));
                 if (stackInt != (I_)BCO_LIT(discr))
                    bciPtr = failto;
                 goto nextInsn;
              }
              case bci_TESTLT_D: {
                 /* The top thing on the stack should be a tagged double. */
                 int discr   = BCO_NEXT;
                 int failto  = BCO_NEXT;
                 StgDouble stackDbl, discrDbl;
                 ASSERT(sizeofW(StgDouble) == StackWord(0));
                 stackDbl = PK_DBL( & StackWord(1) );
                 discrDbl = PK_DBL( & BCO_LIT(discr) );
                 if (stackDbl >= discrDbl)
                    bciPtr = failto;
                 goto nextInsn;
              }
              case bci_TESTEQ_D: {
                 /* The top thing on the stack should be a tagged double. */
                 int discr   = BCO_NEXT;
                 int failto  = BCO_NEXT;
                 StgDouble stackDbl, discrDbl;
                 ASSERT(sizeofW(StgDouble) == StackWord(0));
                 stackDbl = PK_DBL( & StackWord(1) );
                 discrDbl = PK_DBL( & BCO_LIT(discr) );
                 if (stackDbl != discrDbl)
                    bciPtr = failto;
                 goto nextInsn;
              }

              /* Control-flow ish things */
              case bci_ENTER: {
                 goto nextEnter;
              }
              case bci_RETURN: {
                 /* Figure out whether returning to interpreted or
                    compiled code. */
                 int           o_itoc_itbl = BCO_NEXT;
                 int           tag         = StackWord(0);
                 StgInfoTable* ret_itbl    = (StgInfoTable*)StackWord(tag +1);
                 ASSERT(tag <= 2); /* say ... */
                 if (ret_itbl == (StgInfoTable*)&stg_ctoi_ret_R1p_info
                     || ret_itbl == (StgInfoTable*)&stg_ctoi_ret_R1n_info
                     || ret_itbl == (StgInfoTable*)&stg_ctoi_ret_F1_info
                     || ret_itbl == (StgInfoTable*)&stg_ctoi_ret_D1_info) {
                     /* Returning to interpreted code.  Interpret the BCO 
                        immediately underneath the itbl. */
                     StgBCO* ret_bco = (StgBCO*)StackWord(tag +1+1);
                     iSp --;
                     StackWord(0) = (W_)ret_bco;
                     goto nextEnter;
                 } else {
                     /* Returning (unboxed value) to compiled code.
                        Replace tag with a suitable itbl and ask the
                        scheduler to run it.  The itbl code will copy
                        the TOS value into R1/F1/D1 and do a standard
                        compiled-code return. */
                     StgInfoTable* magic_itbl = BCO_ITBL(o_itoc_itbl);
                     StackWord(0) = (W_)magic_itbl;
                     cap->rCurrentTSO->what_next = ThreadRunGHC;
                     RETURN(ThreadYielding);
                 }
              }
        
              case bci_CASEFAIL:
                 barf("interpretBCO: hit a CASEFAIL");

              /* As yet unimplemented */
              case bci_TESTLT_F:
              case bci_TESTEQ_F:

              /* Errors */
              default: 
                 barf("interpretBCO: unknown or unimplemented opcode");

          } /* switch on opcode */

	  barf("interpretBCO: fell off end of insn loop");

       }
       /* ---------------------------------------------------- */
       /* End of the bytecode interpreter                      */
       /* ---------------------------------------------------- */

       defer_to_sched:
       default: {
#         ifdef INTERP_STATS
          { int j = get_itbl(obj)->type;
            ASSERT(j >= 0 && j < N_CLOSURE_TYPES);
            it_unknown_entries[j]++;
            it_total_unknown_entries++;
          }
#         endif

          /* Can't handle this object; yield to sched. */
          IF_DEBUG(evaluator,
                   fprintf(stderr, "entering unknown closure -- yielding to sched\n"); 
                   printObj(obj);
                  );
          iSp--; StackWord(0) = (W_)obj;
          cap->rCurrentTSO->what_next = ThreadEnterGHC;
          RETURN(ThreadYielding);
       }
    } /* switch on object kind */

    barf("fallen off end of object-type switch in interpretBCO()");
}