4 /* -----------------------------------------------------------------------------
7 * Copyright (c) 1994-2000.
9 * $RCSfile: Interpreter.c,v $
11 * $Date: 2000/12/14 15:19:48 $
12 * ---------------------------------------------------------------------------*/
22 #include "SchedAPI.h" /* for createGenThread */
23 #include "Schedule.h" /* for context_switch */
24 #include "Bytecodes.h"
25 #include "ForeignCall.h"
26 #include "PrimOps.h" /* for __{encode,decode}{Float,Double} */
29 #include "Evaluator.h"
30 #include "sainteger.h"
34 #include "Disassembler.h"
39 #include <math.h> /* These are for primops */
40 #include <limits.h> /* These are for primops */
41 #include <float.h> /* These are for primops */
43 #include <ieee754.h> /* These are for primops */
49 int /*StgThreadReturnCode*/ interpretBCO ( void* /* Capability* */ cap )
51 fprintf(stderr, "Greetings, earthlings. I am not yet implemented. Bye!\n");
56 /* --------------------------------------------------------------------------
57 * The new bytecode interpreter
58 * ------------------------------------------------------------------------*/
60 /* Sp points to the lowest live word on the stack. */
62 #define StackWord(n) ((W_*)iSp)[n]
63 #define BCO_NEXT bco_instrs[bciPtr++]
64 #define BCO_PTR(n) bco_ptrs[n]
67 StgThreadReturnCode interpretBCO ( Capability* cap )
69 /* On entry, the closure to interpret is on the top of the
72 /* Use of register here is primarily to make it clear to compilers
73 that these entities are non-aliasable.
75 register StgPtr iSp; /* local state -- stack pointer */
76 register StgUpdateFrame* iSu; /* local state -- frame pointer */
77 register StgPtr iSpLim; /* local state -- stack lim pointer */
78 register StgClosure* obj;
80 iSp = cap->rCurrentTSO->sp;
81 iSu = cap->rCurrentTSO->su;
82 iSpLim = cap->rCurrentTSO->stack + RESERVED_STACK_WORDS;
87 "\n---------------------------------------------------------------\n");
88 fprintf(stderr,"Entering: ",); printObj(obj);
89 fprintf(stderr,"xSp = %p\txSu = %p\n", xSp, xSu);
90 fprintf(stderr, "\n" );
91 printStack(iSp,cap->rCurrentTSO->stack+cap->rCurrentTSO->stack_size,iSu);
92 fprintf(stderr, "\n\n");
95 /* Main object-entering loop. Object to be entered is on top of
99 obj = StackWord(0); iSp++;
101 switch ( get_itbl(obj)->type ) {
103 barf("Invalid object %p",obj);
107 /* ---------------------------------------------------- */
108 /* Start of the bytecode interpreter */
109 /* ---------------------------------------------------- */
111 register StgWord8* bciPtr; /* instruction pointer */
112 register StgBCO* bco = (StgBCO*)obj;
113 if (doYouWantToGC()) {
114 iSp--; StackWord(0) = bco;
120 ASSERT((StgWord)(PC) < bco->n_instrs);
122 fprintf(stderr,"Sp = %p\tSu = %p\tpc = %d\t", xSp, xSu, PC);
125 fprintf(stderr,"\n");
126 for (i = 8; i >= 0; i--)
127 fprintf(stderr, "%d %p\n", i, (StgPtr)(*(gSp+i)));
129 fprintf(stderr,"\n");
136 StackWord(-1) = StackWord(o1);
143 StackWord(-1) = StackWord(o1);
144 StackWord(-2) = StackWord(o2);
152 StackWord(-1) = StackWord(o1);
153 StackWord(-2) = StackWord(o2);
154 StackWord(-3) = StackWord(o3);
160 StackWord(-1) = BCO_PTR(o1);
165 int o_bco = BCO_NEXT;
166 int o_itbl = BCO_NEXT;
167 StackWord(-1) = BCO_LIT(o_itbl);
168 StackWord(-2) = BCO_PTR(o_bco);
173 W_ tag = (W_)(BCO_NEXT);
180 StackWord(-1) = BCO_LIT(o);
187 ASSERT(Sp+n+by <= (StgPtr)xSu);
188 /* a_1, .. a_n, b_1, .. b_by, s => a_1, .. a_n, s */
190 StackWord(n+by) = StackWord(n);
196 int n_payload = BCO_NEXT;
197 P_ p = allocate(AP_sizeW(n_payload));
204 int n_payload = BCO_NEXT - 1;
205 StgAP_UPD* ap = StackWord(off);
206 ap->n_args = n_payload;
207 ap->fun = (StgClosure*)StackWord(0);
208 for (i = 0; i < n_payload; i++)
209 ap->payload[i] = StackWord(i+1);
214 /* Unpack N ptr words from t.o.s constructor */
215 /* The common case ! */
216 int n_words = BCO_NEXT;
217 StgClosure* con = StackWord(0);
219 for (i = 0; i < n_words; i++)
220 StackWord(i) = con->payload[i];
223 case bci_UNPACK_BX: {
224 /* Unpack N (non-ptr) words from offset M in the
225 constructor K words down the stack, and then push
226 N as a tag, on top of it. Slow but general; we
227 hope it will be the rare case. */
228 int n_words = BCO_NEXT;
229 int con_off = BCO_NEXT;
230 int stk_off = BCO_NEXT;
231 StgClosure* con = StackWord(stk_off);
233 for (i = 0; i < n_words; i++)
234 StackWord(i) = con->payload[con_off + i];
236 StackWord(0) = n_words;
250 /* Control-flow ish things */
259 } /* switch on opcode */
263 /* ---------------------------------------------------- */
264 /* End of the bytecode interpreter */
265 /* ---------------------------------------------------- */
268 /* Can't handle this object; yield to sched. */
269 fprintf(stderr, "entering unknown closure -- yielding to sched\n");
271 cap->rCurrentTSO->what_next = ThreadEnterGHC;
272 iSp--; StackWord(0) = obj;
273 return ThreadYielding;
275 } /* switch on object kind */
277 barf("fallen off end of switch in enter()");