summaryrefslogtreecommitdiff
path: root/ghc/rts/Interpreter.c
blob: 83009b9f80ffc008d32377df1e3dceefa288df63 (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

/* -----------------------------------------------------------------------------
 * Bytecode evaluator
 *
 * Copyright (c) 1994-2000.
 *
 * $RCSfile: Interpreter.c,v $
 * $Revision: 1.10 $
 * $Date: 2001/01/10 17:21:18 $
 * ---------------------------------------------------------------------------*/

#ifdef GHCI

#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
 * ------------------------------------------------------------------------*/

/* Sp 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;


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;

    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++;

    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);

#if 0
       case AP_UPD:
        { nat Words;
          nat i;
          StgAP_UPD *ap = (StgAP_UPD*)obj;
          Words = ap->n_args;

	  /* WARNING: do a stack overflow check here !
             This code (copied from stg_AP_UPD_entry) is not correct without it. */

          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;
          }

          iSp -= Words;

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

          iSp--; StackWord(0) = (W_)ap->fun;
          goto nextEnter;
        }
#endif

       case BCO:

       /* ---------------------------------------------------- */
       /* Start of the bytecode interpreter                    */
       /* ---------------------------------------------------- */
       {
          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]);

          if (doYouWantToGC()) {
	     iSp--; StackWord(0) = (W_)bco;
             RETURN(HeapOverflow);
          }

          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");
                         }
                 );

	  //             checkStack(iSp,cap->rCurrentTSO->stack+cap->rCurrentTSO->stack_size,iSu);

          switch (BCO_NEXT) {

              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;
                 /* Handle arg check failure.  Copy the spare args
                    into a PAP frame. */
		 /* fprintf(stderr, "arg check fail %d %d\n", arg_words_reqd, arg_words_avail ); */
                 pap = (StgPAP*)allocate(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;
                 RETURN(ThreadEnterGHC);
              }
              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;
                 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;
                 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;
                 goto nextInsn;
              }
              case bci_ALLOC: {
                 int n_payload = BCO_NEXT - 1;
                 StgAP_UPD* ap = (StgAP_UPD*)allocate(AP_sizeW(n_payload));
                 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;
                 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 = BCO_ITBL(o_itbl);
                 /* A bit of a kludge since n_words = n_p + n_np */
                 int request        = CONSTR_sizeW( n_words, 0 );
                 StgClosure* con = (StgClosure*)allocate(request);
                 SET_HDR(con, 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;
                 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_R1_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;
                     RETURN(ThreadRunGHC);
                 }
              }
        
              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                      */
       /* ---------------------------------------------------- */

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

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

#endif /* GHCI */