2 /* -----------------------------------------------------------------------------
5 * Copyright (c) 1994-2000.
7 * $RCSfile: Interpreter.c,v $
9 * $Date: 2000/12/11 17:59:01 $
10 * ---------------------------------------------------------------------------*/
20 #include "SchedAPI.h" /* for createGenThread */
21 #include "Schedule.h" /* for context_switch */
22 #include "Bytecodes.h"
23 #include "Assembler.h" /* for CFun stuff */
24 #include "ForeignCall.h"
25 #include "PrimOps.h" /* for __{encode,decode}{Float,Double} */
28 #include "Evaluator.h"
29 #include "sainteger.h"
33 #include "Disassembler.h"
38 #include <math.h> /* These are for primops */
39 #include <limits.h> /* These are for primops */
40 #include <float.h> /* These are for primops */
42 #include <ieee754.h> /* These are for primops */
48 /* --------------------------------------------------------------------------
49 * The new bytecode interpreter
50 * ------------------------------------------------------------------------*/
52 /* Sp points to the lowest live word on the stack. */
54 #define StackWord(n) ((W_*)iSp)[n]
55 #define BCO_NEXT bco_instrs[bciPtr++]
56 #define BCO_PTR(n) bco_ptrs[n]
59 StgThreadReturnCode enter ( Capability* cap )
61 /* On entry, the closure to interpret is on the top of the
64 /* Use of register here is primarily to make it clear to compilers
65 that these entities are non-aliasable.
67 register StgPtr iSp; /* local state -- stack pointer */
68 register StgUpdateFrame* iSu; /* local state -- frame pointer */
69 register StgPtr iSpLim; /* local state -- stack lim pointer */
70 register StgClosure* obj;
72 iSp = cap->rCurrentTSO->sp;
73 iSu = cap->rCurrentTSO->su;
74 iSpLim = cap->rCurrentTSO->stack + RESERVED_STACK_WORDS;
79 "\n---------------------------------------------------------------\n");
80 fprintf(stderr,"Entering: ",); printObj(obj);
81 fprintf(stderr,"xSp = %p\txSu = %p\n", xSp, xSu);
82 fprintf(stderr, "\n" );
83 printStack(iSp,cap->rCurrentTSO->stack+cap->rCurrentTSO->stack_size,iSu);
84 fprintf(stderr, "\n\n");
87 /* Main object-entering loop. Object to be entered is on top of
91 obj = StackWord(0); iSp++;
93 switch ( get_itbl(obj)->type ) {
95 barf("Invalid object %p",obj);
99 /* ---------------------------------------------------- */
100 /* Start of the bytecode interpreter */
101 /* ---------------------------------------------------- */
103 register StgWord8* bciPtr; /* instruction pointer */
104 register StgBCO* bco = (StgBCO*)obj;
105 if (doYouWantToGC()) {
106 iSp--; StackWord(0) = bco;
112 ASSERT((StgWord)(PC) < bco->n_instrs);
114 fprintf(stderr,"Sp = %p\tSu = %p\tpc = %d\t", xSp, xSu, PC);
117 fprintf(stderr,"\n");
118 for (i = 8; i >= 0; i--)
119 fprintf(stderr, "%d %p\n", i, (StgPtr)(*(gSp+i)));
121 fprintf(stderr,"\n");
128 StackWord(-1) = StackWord(o1);
135 StackWord(-1) = StackWord(o1);
136 StackWord(-2) = StackWord(o2);
144 StackWord(-1) = StackWord(o1);
145 StackWord(-2) = StackWord(o2);
146 StackWord(-3) = StackWord(o3);
152 StackWord(-1) = BCO_PTR(o1);
157 int o_bco = BCO_NEXT;
158 int o_itbl = BCO_NEXT;
159 StackWord(-1) = BCO_LITW(o_itbl);
160 StackWord(-2) = BCO_PTR(o_bco);
166 StackWord(-1) = BCO_LIT(o);
171 W_ tag = (W_)(BCO_NEXT);
179 ASSERT(Sp+n+by <= (StgPtr)xSu);
180 /* a_1, .. a_n, b_1, .. b_by, s => a_1, .. a_n, s */
182 StackWord(n+by) = StackWord(n);
188 int n_payload = BCO_NEXT;
189 P_ p = allocate(AP_sizeW(n_payload));
196 int n_payload = BCO_NEXT - 1;
197 StgAP_UPD* ap = StackWord(off);
198 ap->n_args = n_payload;
199 ap->fun = (StgClosure*)StackWord(0);
200 for (i = 0; i < n_payload; i++)
201 ap->payload[i] = StackWord(i+1);
206 /* Unpack N ptr words from t.o.s constructor */
207 /* The common case ! */
208 int n_words = BCO_NEXT;
209 StgClosure* con = StackWord(0);
211 for (i = 0; i < n_words; i++)
212 StackWord(i) = con->payload[i];
215 case bci_UNPACK_BX: {
216 /* Unpack N (non-ptr) words from offset M in the
217 constructor K words down the stack, and then push
218 N as a tag, on top of it. Slow but general; we
219 hope it will be the rare case. */
220 int n_words = BCO_NEXT;
221 int con_off = BCO_NEXT;
222 int stk_off = BCO_NEXT;
223 StgClosure* con = StackWord(stk_off);
225 for (i = 0; i < n_words; i++)
226 StackWord(i) = con->payload[con_off + i];
228 StackWord(0) = n_words;
242 /* Control-flow ish things */
251 } /* switch on opcode */
255 /* ---------------------------------------------------- */
256 /* End of the bytecode interpreter */
257 /* ---------------------------------------------------- */
260 /* Can't handle this object; yield to sched. */
261 fprintf(stderr, "entering unknown closure -- yielding to sched\n");
263 cap->rCurrentTSO->what_next = ThreadEnterGHC;
264 iSp--; StackWord(0) = obj;
265 return ThreadYielding;
267 } /* switch on object kind */
269 barf("fallen off end of switch in enter()");