+# if CRUDE_PROFILING
+ cp_enter ( bco );
+# endif
+
+
+ bciPtr = &(bcoInstr(bco,0));
+
+ LoopTopLabel
+
+ ASSERT(PC < bco->n_instrs);
+ IF_DEBUG(evaluator,
+ fprintf(stderr,"Sp = %p\tSu = %p\tpc = %d\t", xSp, xSu, PC);
+ SSS;
+ disInstr(bco,PC);
+ //{ int i;
+ //fprintf(stderr,"\n");
+ // for (i = 4; i >= 0; i--)
+ // fprintf(stderr, "%d %p\n", i, (StgPtr)(*(Sp+i)));
+ // }
+ fprintf(stderr,"\n");
+ LLL;
+ );
+
+# if CRUDE_PROFILING
+ SSS; cp_bill_insns(1); LLL;
+# endif
+
+ Dispatch
+
+ Case(i_INTERNAL_ERROR):
+ barf("INTERNAL_ERROR at %p:%d",bco,PC-1);
+ Case(i_PANIC):
+ barf("PANIC at %p:%d",bco,PC-1);
+ Case(i_STK_CHECK):
+ {
+ int n = BCO_INSTR_8;
+ if (xSp - n < xSpLim) {
+ xPushCPtr((StgClosure*)bco); /* code to restart with */
+ RETURN(StackOverflow);
+ }
+ Continue;
+ }
+ Case(i_ARG_CHECK):
+ {
+ nat n = BCO_INSTR_8;
+ if ((StgPtr*)(xSp + n) > (StgPtr*)xSu) {
+ StgWord words = (P_)xSu - xSp;
+
+ /* first build a PAP */
+ ASSERT((P_)xSu >= xSp); /* was (words >= 0) but that's always true */
+ if (words == 0) { /* optimisation */
+ /* Skip building the PAP and update with an indirection. */
+ } else {
+ /* Build the PAP. */
+ /* In the evaluator, we avoid the need to do
+ * a heap check here by including the size of
+ * the PAP in the heap check we performed
+ * when we entered the BCO.
+ */
+ StgInt i;
+ StgPAP* pap;
+ SSS; pap = (StgPAP*)grabHpNonUpd(PAP_sizeW(words)); LLL;
+ SET_HDR(pap,&PAP_info,CC_pap);
+ pap->n_args = words;
+ pap->fun = obj;
+ for (i = 0; i < (I_)words; ++i) {
+ payloadWord(pap,i) = xSp[i];
+ }
+ xSp += words;
+ obj = stgCast(StgClosure*,pap);
+ }
+
+ /* now deal with "update frame" */
+ /* as an optimisation, we process all on top of stack */
+ /* instead of just the top one */
+ ASSERT(xSp==(P_)xSu);
+ do {
+ switch (get_itbl(xSu)->type) {
+ case CATCH_FRAME:
+ /* Hit a catch frame during an arg satisfaction check,
+ * so the thing returning (1) has not thrown an
+ * exception, and (2) is of functional type. Just
+ * zap the catch frame and carry on down the stack
+ * (looking for more arguments, basically).
+ */
+ SSS; PopCatchFrame(); LLL;
+ break;
+ case UPDATE_FRAME:
+ xPopUpdateFrame(obj);
+ break;
+ case STOP_FRAME:
+ SSS; PopStopFrame(obj); LLL;
+ RETURN(ThreadFinished);
+ case SEQ_FRAME:
+ SSS; PopSeqFrame(); LLL;
+ ASSERT(xSp != (P_)xSu);
+ /* Hit a SEQ frame during an arg satisfaction check.
+ * So now return to bco_info which is under the
+ * SEQ frame. The following code is copied from a
+ * case RET_BCO further down. (The reason why we're
+ * here is that something of functional type has
+ * been seq-d on, and we're now returning to the
+ * algebraic-case-continuation which forced the
+ * evaluation in the first place.)
+ */
+ {
+ StgClosure* ret;
+ (void)xPopPtr();
+ ret = xPopCPtr();
+ xPushPtr((P_)obj);
+ obj = ret;
+ goto enterLoop;
+ }
+ break;
+ default:
+ barf("Invalid update frame during argcheck");
+ }
+ } while (xSp==(P_)xSu);
+ goto enterLoop;
+ }
+ Continue;
+ }
+ Case(i_ALLOC_AP):
+ {
+ StgPtr p;
+ int words = BCO_INSTR_8;
+ SSS; p = grabHpUpd(AP_sizeW(words)); LLL;
+ xPushPtr(p);
+ Continue;
+ }
+ Case(i_ALLOC_CONSTR):
+ {
+ StgPtr p;
+ StgInfoTable* info = bcoConstAddr(bco,BCO_INSTR_8);
+ SSS; p = grabHpNonUpd(sizeW_fromITBL(info)); LLL;
+ SET_HDR((StgClosure*)p,info,??);
+ xPushPtr(p);
+ Continue;
+ }
+ Case(i_MKAP):
+ {
+ int x = BCO_INSTR_8; /* ToDo: Word not Int! */
+ int y = BCO_INSTR_8;
+ StgAP_UPD* o = stgCast(StgAP_UPD*,xStackPtr(x));
+ SET_HDR(o,&AP_UPD_info,??);
+ o->n_args = y;
+ o->fun = stgCast(StgClosure*,xPopPtr());
+ for(x=0; x < y; ++x) {
+ payloadWord(o,x) = xPopWord();
+ }
+ IF_DEBUG(evaluator,
+ fprintf(stderr,"\tBuilt ");
+ SSS;
+ printObj(stgCast(StgClosure*,o));
+ LLL;
+ );
+ Continue;
+ }
+ Case(i_MKAP_big):
+ {
+ int x, y;
+ StgAP_UPD* o;
+ x = BCO_INSTR_16;
+ y = BCO_INSTR_16;
+ o = stgCast(StgAP_UPD*,xStackPtr(x));
+ SET_HDR(o,&AP_UPD_info,??);
+ o->n_args = y;
+ o->fun = stgCast(StgClosure*,xPopPtr());
+ for(x=0; x < y; ++x) {
+ payloadWord(o,x) = xPopWord();
+ }
+ IF_DEBUG(evaluator,
+ fprintf(stderr,"\tBuilt ");
+ SSS;
+ printObj(stgCast(StgClosure*,o));
+ LLL;
+ );
+ Continue;
+ }
+ Case(i_MKPAP):
+ {
+ int x = BCO_INSTR_8;
+ int y = BCO_INSTR_8;
+ StgPAP* o = stgCast(StgPAP*,xStackPtr(x));
+ SET_HDR(o,&PAP_info,??);
+ o->n_args = y;
+ o->fun = stgCast(StgClosure*,xPopPtr());
+ for(x=0; x < y; ++x) {
+ payloadWord(o,x) = xPopWord();
+ }
+ IF_DEBUG(evaluator,
+ fprintf(stderr,"\tBuilt ");
+ SSS;
+ printObj(stgCast(StgClosure*,o));
+ LLL;
+ );
+ Continue;
+ }
+ Case(i_PACK):
+ {
+ int offset = BCO_INSTR_8;
+ StgClosure* o = stgCast(StgClosure*,xStackPtr(offset));
+ const StgInfoTable* info = get_itbl(o);
+ nat p = info->layout.payload.ptrs;
+ nat np = info->layout.payload.nptrs;
+ nat i;
+ for(i=0; i < p; ++i) {
+ payloadCPtr(o,i) = xPopCPtr();
+ }
+ for(i=0; i < np; ++i) {
+ payloadWord(o,p+i) = 0xdeadbeef;
+ }
+ IF_DEBUG(evaluator,
+ fprintf(stderr,"\tBuilt ");
+ SSS;
+ printObj(stgCast(StgClosure*,o));
+ LLL;
+ );
+ Continue;
+ }
+ Case(i_PACK_big):
+ {
+ int offset = BCO_INSTR_16;
+ StgClosure* o = stgCast(StgClosure*,xStackPtr(offset));
+ const StgInfoTable* info = get_itbl(o);
+ nat p = info->layout.payload.ptrs;
+ nat np = info->layout.payload.nptrs;
+ nat i;
+ for(i=0; i < p; ++i) {
+ payloadCPtr(o,i) = xPopCPtr();
+ }
+ for(i=0; i < np; ++i) {
+ payloadWord(o,p+i) = 0xdeadbeef;
+ }
+ IF_DEBUG(evaluator,
+ fprintf(stderr,"\tBuilt ");
+ SSS;
+ printObj(stgCast(StgClosure*,o));
+ LLL;
+ );
+ Continue;
+ }
+ Case(i_SLIDE):
+ {
+ int x = BCO_INSTR_8;
+ int y = BCO_INSTR_8;
+ ASSERT(xSp+x+y <= stgCast(StgPtr,xSu));
+ /* a_1, .. a_x, b_1, .. b_y, s => a_1, .. a_x, s */
+ while(--x >= 0) {
+ xSetStackWord(x+y,xStackWord(x));
+ }
+ xSp += y;
+ Continue;
+ }
+ Case(i_SLIDE_big):
+ {
+ int x, y;
+ x = BCO_INSTR_16;
+ y = BCO_INSTR_16;
+ ASSERT(xSp+x+y <= stgCast(StgPtr,xSu));
+ /* a_1, .. a_x, b_1, .. b_y, s => a_1, .. a_x, s */
+ while(--x >= 0) {
+ xSetStackWord(x+y,xStackWord(x));
+ }
+ xSp += y;
+ Continue;
+ }
+ Case(i_ENTER):
+ {
+ obj = xPopCPtr();
+ goto enterLoop;
+ }
+ Case(i_RETADDR):
+ {
+ xPushPtr(bcoConstPtr(bco,BCO_INSTR_8));
+ xPushPtr(stgCast(StgPtr,&ret_bco_info));
+ Continue;
+ }
+ Case(i_TEST):
+ {
+ int tag = BCO_INSTR_8;
+ StgWord offset = BCO_INSTR_16;
+ if (constrTag(stgCast(StgClosure*,xStackPtr(0))) != tag) {
+ bciPtr += offset;
+ }
+ Continue;
+ }
+ Case(i_UNPACK):
+ {
+ StgClosure* o = stgCast(StgClosure*,xStackPtr(0));
+ const StgInfoTable* itbl = get_itbl(o);
+ int i = itbl->layout.payload.ptrs;
+ ASSERT( itbl->type == CONSTR
+ || itbl->type == CONSTR_STATIC
+ || itbl->type == CONSTR_NOCAF_STATIC
+ );
+ while (--i>=0) {
+ xPushCPtr(payloadCPtr(o,i));
+ }
+ Continue;
+ }
+ Case(i_VAR_big):
+ {
+ int n = BCO_INSTR_16;
+ StgPtr p = xStackPtr(n);
+ xPushPtr(p);
+ Continue;
+ }
+ Case(i_VAR):
+ {
+ StgPtr p = xStackPtr(BCO_INSTR_8);
+ xPushPtr(p);
+ Continue;
+ }
+ Case(i_CONST):
+ {
+ xPushPtr(stgCast(StgPtr,bcoConstPtr(bco,BCO_INSTR_8)));
+ Continue;
+ }
+ Case(i_CONST_big):
+ {
+ int n = BCO_INSTR_16;
+ xPushPtr(stgCast(StgPtr,bcoConstPtr(bco,n)));
+ Continue;
+ }
+ Case(i_VOID):
+ {
+ SSS; PushTaggedRealWorld(); LLL;
+ Continue;
+ }
+ Case(i_VAR_INT):
+ {
+ StgInt i = xTaggedStackInt(BCO_INSTR_8);
+ xPushTaggedInt(i);
+ Continue;
+ }
+ Case(i_CONST_INT):
+ {
+ xPushTaggedInt(bcoConstInt(bco,BCO_INSTR_8));
+ Continue;
+ }
+ Case(i_PACK_INT):
+ {
+ StgClosure* o;
+ SSS; o = (StgClosure*)grabHpNonUpd(Izh_sizeW); LLL;
+ SET_HDR(o,&Izh_con_info,??);
+ payloadWord(o,0) = xPopTaggedInt();
+ IF_DEBUG(evaluator,
+ fprintf(stderr,"\tBuilt ");
+ SSS;
+ printObj(stgCast(StgClosure*,o));
+ LLL;
+ );
+ xPushPtr(stgCast(StgPtr,o));
+ Continue;
+ }
+ Case(i_UNPACK_INT):
+ {
+ StgClosure* con = stgCast(StgClosure*,xStackPtr(0));
+ /* ASSERT(isIntLike(con)); */
+ xPushTaggedInt(payloadWord(con,0));
+ Continue;
+ }
+ Case(i_TEST_INT):
+ {
+ StgWord offset = BCO_INSTR_16;
+ StgInt x = xPopTaggedInt();
+ StgInt y = xPopTaggedInt();
+ if (x != y) {
+ bciPtr += offset;
+ }
+ Continue;
+ }
+ Case(i_CONST_INTEGER):
+ {
+ StgPtr p;
+ int n;
+ char* s = bcoConstAddr(bco,BCO_INSTR_8);
+ SSS;
+ n = size_fromStr(s);
+ p = CreateByteArrayToHoldInteger(n);
+ do_fromStr ( s, n, IntegerInsideByteArray(p));
+ SloppifyIntegerEnd(p);
+ LLL;
+ xPushPtr(p);
+ Continue;
+ }
+ Case(i_VAR_WORD):
+ {
+ StgWord w = xTaggedStackWord(BCO_INSTR_8);
+ xPushTaggedWord(w);
+ Continue;
+ }
+ Case(i_CONST_WORD):
+ {
+ xPushTaggedWord(bcoConstWord(bco,BCO_INSTR_8));
+ Continue;
+ }
+ Case(i_PACK_WORD):
+ {
+ StgClosure* o;
+ SSS; o = (StgClosure*)grabHpNonUpd(Wzh_sizeW); LLL;
+ SET_HDR(o,&Wzh_con_info,??);
+ payloadWord(o,0) = xPopTaggedWord();
+ IF_DEBUG(evaluator,
+ fprintf(stderr,"\tBuilt ");
+ SSS;
+ printObj(stgCast(StgClosure*,o));
+ LLL;
+ );
+ xPushPtr(stgCast(StgPtr,o));
+ Continue;
+ }
+ Case(i_UNPACK_WORD):
+ {
+ StgClosure* con = stgCast(StgClosure*,xStackPtr(0));
+ /* ASSERT(isWordLike(con)); */
+ xPushTaggedWord(payloadWord(con,0));
+ Continue;
+ }
+ Case(i_VAR_ADDR):
+ {
+ StgAddr a = xTaggedStackAddr(BCO_INSTR_8);
+ xPushTaggedAddr(a);
+ Continue;
+ }
+ Case(i_CONST_ADDR):
+ {
+ xPushTaggedAddr(bcoConstAddr(bco,BCO_INSTR_8));
+ Continue;
+ }
+ Case(i_PACK_ADDR):
+ {
+ StgClosure* o;
+ SSS; o = (StgClosure*)grabHpNonUpd(Azh_sizeW); LLL;
+ SET_HDR(o,&Azh_con_info,??);
+ payloadPtr(o,0) = xPopTaggedAddr();
+ IF_DEBUG(evaluator,
+ fprintf(stderr,"\tBuilt ");
+ SSS;
+ printObj(stgCast(StgClosure*,o));
+ LLL;
+ );
+ xPushPtr(stgCast(StgPtr,o));
+ Continue;
+ }
+ Case(i_UNPACK_ADDR):
+ {
+ StgClosure* con = stgCast(StgClosure*,xStackPtr(0));
+ /* ASSERT(isAddrLike(con)); */
+ xPushTaggedAddr(payloadPtr(con,0));
+ Continue;
+ }
+ Case(i_VAR_CHAR):
+ {
+ StgChar c = xTaggedStackChar(BCO_INSTR_8);
+ xPushTaggedChar(c);
+ Continue;
+ }
+ Case(i_CONST_CHAR):
+ {
+ xPushTaggedChar(bcoConstChar(bco,BCO_INSTR_8));
+ Continue;
+ }
+ Case(i_PACK_CHAR):
+ {
+ StgClosure* o;
+ SSS; o = (StgClosure*)grabHpNonUpd(Czh_sizeW); LLL;
+ SET_HDR(o,&Czh_con_info,??);
+ payloadWord(o,0) = xPopTaggedChar();
+ xPushPtr(stgCast(StgPtr,o));
+ IF_DEBUG(evaluator,
+ fprintf(stderr,"\tBuilt ");
+ SSS;
+ printObj(stgCast(StgClosure*,o));
+ LLL;
+ );
+ Continue;
+ }
+ Case(i_UNPACK_CHAR):
+ {
+ StgClosure* con = stgCast(StgClosure*,xStackPtr(0));
+ /* ASSERT(isCharLike(con)); */
+ xPushTaggedChar(payloadWord(con,0));
+ Continue;
+ }
+ Case(i_VAR_FLOAT):
+ {
+ StgFloat f = xTaggedStackFloat(BCO_INSTR_8);
+ xPushTaggedFloat(f);
+ Continue;
+ }
+ Case(i_CONST_FLOAT):
+ {
+ xPushTaggedFloat(bcoConstFloat(bco,BCO_INSTR_8));
+ Continue;
+ }
+ Case(i_PACK_FLOAT):
+ {
+ StgClosure* o;
+ SSS; o = (StgClosure*)grabHpNonUpd(Fzh_sizeW); LLL;
+ SET_HDR(o,&Fzh_con_info,??);
+ ASSIGN_FLT(&payloadWord(o,0),xPopTaggedFloat());
+ IF_DEBUG(evaluator,
+ fprintf(stderr,"\tBuilt ");
+ SSS;
+ printObj(stgCast(StgClosure*,o));
+ LLL;
+ );
+ xPushPtr(stgCast(StgPtr,o));
+ Continue;
+ }
+ Case(i_UNPACK_FLOAT):
+ {
+ StgClosure* con = stgCast(StgClosure*,xStackPtr(0));
+ /* ASSERT(isFloatLike(con)); */
+ xPushTaggedFloat(PK_FLT(&payloadWord(con,0)));
+ Continue;
+ }
+ Case(i_VAR_DOUBLE):
+ {
+ StgDouble d = xTaggedStackDouble(BCO_INSTR_8);
+ xPushTaggedDouble(d);
+ Continue;
+ }
+ Case(i_CONST_DOUBLE):
+ {
+ xPushTaggedDouble(bcoConstDouble(bco,BCO_INSTR_8));
+ Continue;
+ }
+ Case(i_CONST_DOUBLE_big):
+ {
+ int n = BCO_INSTR_16;
+ xPushTaggedDouble(bcoConstDouble(bco,n));
+ Continue;
+ }
+ Case(i_PACK_DOUBLE):
+ {
+ StgClosure* o;
+ SSS; o = (StgClosure*)grabHpNonUpd(Dzh_sizeW); LLL;
+ SET_HDR(o,&Dzh_con_info,??);
+ ASSIGN_DBL(&payloadWord(o,0),xPopTaggedDouble());
+ IF_DEBUG(evaluator,
+ fprintf(stderr,"\tBuilt ");
+ printObj(stgCast(StgClosure*,o));
+ );
+ xPushPtr(stgCast(StgPtr,o));
+ Continue;
+ }
+ Case(i_UNPACK_DOUBLE):
+ {
+ StgClosure* con = stgCast(StgClosure*,xStackPtr(0));
+ /* ASSERT(isDoubleLike(con)); */
+ xPushTaggedDouble(PK_DBL(&payloadWord(con,0)));
+ Continue;
+ }
+ Case(i_VAR_STABLE):
+ {
+ fprintf(stderr, "unimp: i_VAR_STABLE\n" ); exit(0);
+ /*fix side effects here ...*/
+ /*
+ xPushTaggedStablePtr(xTaggedStackStable(BCO_INSTR_8));
+ */
+ Continue;
+ }
+ Case(i_PACK_STABLE):
+ {
+ //StgClosure* o;
+ fprintf(stderr, "unimp: i_PACK_STABLE\n" ); exit(0);
+ /*
+ SSS; o = (StgClosure*)grabHpNonUpd(Stablezh_sizeW); LLL;
+ SET_HDR(o,&StablePtr_con_info,??);
+ payloadWord(o,0) = xPopTaggedStablePtr();
+ IF_DEBUG(evaluator,
+ fprintf(stderr,"\tBuilt ");
+ printObj(stgCast(StgClosure*,o));
+ );
+ xPushPtr(stgCast(StgPtr,o));
+ */
+ Continue;
+ }
+ Case(i_UNPACK_STABLE):
+ {
+ //StgClosure* con;
+ fprintf(stderr, "unimp: i_UNPACK_STABLE\n" ); exit(0);
+ /*
+ con = stgCast(StgClosure*,xStackPtr(0));
+ ASSERT(isStableLike(con));
+ xPushTaggedStablePtr(payloadWord(con,0));
+ */
+ Continue;
+ }
+ Case(i_PRIMOP1):
+ {
+ int i;
+ void* p;
+ i = BCO_INSTR_8;
+ SSS; p = enterBCO_primop1 ( i ); LLL;
+ if (p) { obj = p; goto enterLoop; };
+ Continue;
+ }
+ Case(i_PRIMOP2):
+ {
+ int i, trc;
+ void* p;
+ trc = 12345678; /* Hope that no StgThreadReturnCode has this value */
+ i = BCO_INSTR_8;
+ SSS; p = enterBCO_primop2 ( i, &trc ); LLL;
+ if (p) {
+ if (trc == 12345678) {
+ /* we want to enter p */
+ obj = p; goto enterLoop;
+ } else {
+ /* p is the the StgThreadReturnCode for this thread */
+ RETURN((StgThreadReturnCode)p);
+ };
+ }
+ Continue;
+ }
+
+ /* combined insns, created by peephole opt */
+ Case(i_SE):
+ {
+ int x = BCO_INSTR_8;
+ int y = BCO_INSTR_8;
+ ASSERT(xSp+x+y <= stgCast(StgPtr,xSu));
+ /* a_1, .. a_x, b_1, .. b_y, s => a_1, .. a_x, s */
+ if (x == 1) {
+ obj = xPopCPtr();
+ xSp += y;
+ goto enterLoop;
+ } else {
+ while(--x >= 0) {
+ xSetStackWord(x+y,xStackWord(x));
+ }
+ xSp += y;
+ obj = xPopCPtr();
+ }
+ goto enterLoop;
+ }
+ Case(i_VV):
+ {
+ StgPtr p;
+ p = xStackPtr(BCO_INSTR_8);
+ xPushPtr(p);
+ p = xStackPtr(BCO_INSTR_8);
+ xPushPtr(p);
+ Continue;
+ }
+ Case(i_RV):
+ {
+ StgPtr p;
+ xPushPtr(bcoConstPtr(bco,BCO_INSTR_8));
+ xPushPtr(stgCast(StgPtr,&ret_bco_info));
+ p = xStackPtr(BCO_INSTR_8);
+ xPushPtr(p);
+ Continue;
+ }
+ Case(i_RVE):
+ {
+ StgPtr retaddr = bcoConstPtr(bco,BCO_INSTR_8);
+ StgPtr ptr = xStackPtr(BCO_INSTR_8);
+
+ /* A shortcut. We're going to push the address of a
+ return continuation, and then enter a variable, so
+ that when the var is evaluated, we return to the
+ continuation. The shortcut is: if the var is a
+ constructor, don't bother to enter it. Instead,
+ push the variable on the stack (since this is what
+ the continuation expects) and jump directly to the
+ continuation.
+ */
+ if (get_itbl((StgClosure*)ptr)->type == CONSTR) {
+ xPushPtr(ptr);
+ obj = (StgClosure*)retaddr;
+ IF_DEBUG(evaluator,
+ fprintf(stderr, "object to enter is a constructor -- "
+ "jumping directly to return continuation\n" );
+ );
+ goto bco_entry;
+ }