/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ GLOBAL VOID RCL IFN3( IU32 *, pop1, /* pntr to dst/src operand */ IU32, op2, /* rotation count operand */ IUM8, op_sz /* 8, 16 or 32-bit */ ) { IU32 result; IU32 feedback; /* Bit posn to feed into carry */ ISM32 i; ISM32 new_of; /* only use lower five bits of count */ if ( (op2 &= 0x1f) == 0 ) return; /* ==== ================= -- |CF| <-- | | | | | | | | | <-- | ==== ================= | --------------------------------- */ feedback = SZ2MSB(op_sz); for ( result = *pop1, i = 0; i < op2; i++ ) { if ( result & feedback ) { result = result << 1 | GET_CF(); SET_CF(1); } else { result = result << 1 | GET_CF(); SET_CF(0); } } /* OF = CF ^ MSB of result */ new_of = GET_CF() ^ (result & feedback) != 0; if ( op2 == 1 ) { SET_OF(new_of); } else { do_multiple_shiftrot_of(new_of); } *pop1 = result; /* Return answer */ }
GLOBAL VOID DAA() { IU8 temp_al; temp_al = GET_AL(); if ( (temp_al & 0xf) > 9 || GET_AF() ) { temp_al += 6; SET_AF(1); } if ( GET_AL() > 0x99 || GET_CF() ) { temp_al += 0x60; SET_CF(1); } SET_AL(temp_al); /* set ZF,SF,PF according to result */ SET_ZF(temp_al == 0); SET_SF((temp_al & BIT7_MASK) != 0); SET_PF(pf_table[temp_al]); /* Set undefined flag(s) */ #ifdef SET_UNDEFINED_FLAG SET_OF(UNDEFINED_FLAG); #endif }
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ GLOBAL VOID SBB IFN3( IU32 *, pop1, /* pntr to dst/lsrc operand */ IU32, op2, /* rsrc operand */ IUM8, op_sz /* 8, 16 or 32-bit */ ) { IU32 result; IU32 carry; IU32 msb; IU32 op1_msb; IU32 op2_msb; IU32 res_msb; msb = SZ2MSB(op_sz); /* Do operation */ result = *pop1 - op2 - GET_CF() & SZ2MASK(op_sz); op1_msb = (*pop1 & msb) != 0; /* Isolate all msb's */ op2_msb = (op2 & msb) != 0; res_msb = (result & msb) != 0; carry = *pop1 ^ op2 ^ result; /* Isolate carries */ /* Determine flags */ /* OF = (op1 == !op2) & (op1 ^ res) ie if operand signs differ and res sign different to original destination set OF. */ SET_OF((op1_msb != op2_msb) & (op1_msb ^ res_msb)); /* Formally:- CF = !op1 & op2 | res & !op1 | res & op2 Equivalently:- CF = OF ^ op1 ^ op2 ^ res */ SET_CF(((carry & msb) != 0) ^ GET_OF()); SET_PF(pf_table[result & BYTE_MASK]); SET_ZF(result == 0); SET_SF((result & msb) != 0); /* SF = MSB */ SET_AF((carry & BIT4_MASK) != 0); /* AF = Bit 4 carry */ *pop1 = result; /* Return answer */ }
// extern __thread jmp_buf jumpBuf; int VM::call(Value A, int nEffArgs, Value *regs, Stack *stack) { Vector<RetInfo> retInfo; // only used if FAST_CALL if (!(IS_O_TYPE(A, O_FUNC) || IS_CF(A) || IS_O_TYPE(A, O_CFUNC))) { return -1; } regs = stack->maybeGrow(regs, 256); int nExpectedArgs = IS_O_TYPE(A, O_FUNC) ? ((Func *)GET_OBJ(A))->proto->nArgs : NARGS_CFUNC; nEffArgs = prepareStackForCall(regs, nExpectedArgs, nEffArgs, gc); if (IS_CF(A) || IS_O_TYPE(A, O_CFUNC)) { if (IS_CF(A)) { tfunc f = GET_CF(A); *regs = f(this, CFunc::CFUNC_CALL, 0, regs, nEffArgs); } else { ((CFunc *) GET_OBJ(A))->call(this, regs, nEffArgs); } return 0; } unsigned code = 0; Value B; Value *ptrC; Func *activeFunc = (Func *) GET_OBJ(A); unsigned *pc = (unsigned *) activeFunc->proto->code.buf(); static void *dispatch[] = { #define _(name) &&name #include "opcodes.inc" #undef _ }; assert(sizeof(dispatch)/sizeof(dispatch[0]) == N_OPCODES); copyUpvals(activeFunc, regs); STEP; JMP: pc += OD(code); STEP; JT: if (!IS_FALSE(*ptrC)) { pc += OD(code); } STEP; JF: if ( IS_FALSE(*ptrC)) { pc += OD(code); } STEP; JLT: if (lessThan(A, B)) { pc += OSC(code); } STEP; JNIS: if (A != B) { pc += OSC(code); } STEP; FOR: A = *(ptrC + 1); B = *(ptrC + 2); if (!IS_NUM(A) || !IS_NUM(B)) { goto error; } // E_FOR_NOT_NUMBER *ptrC = B; if (!(GET_NUM(B) < GET_NUM(A))) { pc += OD(code); } STEP; LOOP: { const double counter = GET_NUM(*ptrC) + 1; if (counter < GET_NUM(*(ptrC+1))) { pc += OD(code); } *ptrC = VAL_NUM(counter); STEP; } FUNC: assert(IS_PROTO(A)); *ptrC = VAL_OBJ(Func::alloc(gc, PROTO(A), regs + 256, regs, OB(code))); STEP; // index, A[B] GETI: *ptrC = types->type(A)->indexGet(A, B); if (*ptrC == VERR) { goto error; } STEP; GETF: *ptrC = types->type(A)->fieldGet(A, B); if (*ptrC == VERR) { goto error; } STEP; SETI: if (!types->type(*ptrC)->indexSet(*ptrC, A, B)) { goto error; } STEP; SETF: if (!types->type(*ptrC)->fieldSet(*ptrC, A, B)) { goto error; } STEP; /* const int oa = OA(code); const int ob = OB(code); int top = max(oa, ob) + 1; top = max(top, activeFunc->proto->localsTop); Value *base = regs + top; printf("top %d\n", top); base[0] = A; base[1] = B; int cPos = ptrC - regs; DO_CALL(v, 2, regs, base, stack); regs[cPos] = base[0]; break; if (*ptrC == VERR) { goto error; } */ GETS: *ptrC = getSlice(gc, A, B, regs[OB(code)+1]); if (*ptrC==VERR) { goto error; } STEP; SETS: if (setSlice(*ptrC, A, regs[OA(code)+1], B)) { goto error; } STEP; RET: { regs[0] = A; Value *root = stack->base; gc->maybeCollect(root, regs - root + 1); #if FAST_CALL if (!retInfo.size()) { return 0; } RetInfo *ri = retInfo.top(); pc = ri->pc; regs = stack->base + ri->base; activeFunc = ri->func; retInfo.pop(); copyUpvals(activeFunc, regs); STEP; #else return 0; #endif } CALL: { if (!IS_OBJ(A) && !IS_CF(A)) { goto error; } // E_CALL_NOT_FUNC int nEffArgs = OSB(code); assert(nEffArgs != 0); Value *base = ptrC; #if FAST_CALL if (IS_O_TYPE(A, O_FUNC)) { Func *f = (Func *) GET_OBJ(A); Proto *proto = f->proto; prepareStackForCall(base, proto->nArgs, nEffArgs, gc); RetInfo *ret = retInfo.push(); ret->pc = pc; ret->base = regs - stack->base; ret->func = activeFunc; regs = stack->maybeGrow(base, 256); copyUpvals(f, regs); pc = proto->code.buf(); activeFunc = f; } else { #endif int ret = DO_CALL(A, nEffArgs, regs, base, stack); if (ret) { goto error; } #if FAST_CALL } #endif STEP; } MOVEUP: { const int slot = regs + 256 - ptrC; activeFunc->setUp(slot, A); } MOVE_R: *ptrC = A; STEP; MOVE_I: *ptrC = VAL_NUM(OD(code)); STEP; MOVE_V: { int id = OA(code); *ptrC = id == CONST_NIL ? VNIL : id == CONST_EMPTY_STRING ? EMPTY_STRING : id == CONST_EMPTY_ARRAY ? VAL_OBJ(emptyArray->copy(gc)) : VAL_OBJ(emptyMap->copy(gc)); STEP; } MOVE_C: { Value v = *pc | (((u64) *(pc+1)) << 32); pc += 2; if (IS_ARRAY(v)) { v = VAL_OBJ(ARRAY(v)->copy(gc)); } else if (IS_MAP(v)) { v = VAL_OBJ(MAP(v)->copy(gc)); } *ptrC = v; STEP; } LEN: *ptrC = VAL_NUM(len(A)); STEP; NOTL: *ptrC = IS_FALSE(A) ? TRUE : FALSE; STEP; // notb: *ptrC = IS_INT(A)? VAL_INT(~getInteger(A)):ERROR(E_WRONG_TYPE); STEP; ADD: *ptrC = doAdd(gc, A, B); if (*ptrC == VERR) { goto error; } STEP; SUB: *ptrC = BINOP(-, A, B); STEP; MUL: *ptrC = BINOP(*, A, B); STEP; DIV: *ptrC = BINOP(/, A, B); STEP; MOD: *ptrC = doMod(A, B); if (*ptrC == VERR) { goto error; } STEP; POW: *ptrC = doPow(A, B); if (*ptrC == VERR) { goto error; } STEP; AND: *ptrC = BITOP(&, A, B); STEP; OR: *ptrC = BITOP(|, A, B); STEP; XOR: *ptrC = BITOP(^, A, B); STEP; SHL_RR: ERR(!IS_NUM(B), E_WRONG_TYPE); *ptrC = doSHL(A, (int)GET_NUM(B)); STEP; SHR_RR: ERR(!IS_NUM(B), E_WRONG_TYPE); *ptrC = doSHR(A, (int)GET_NUM(B)); STEP; SHL_RI: *ptrC = doSHL(A, OSB(code)); STEP; SHR_RI: *ptrC = doSHR(A, OSB(code)); STEP; EQ: *ptrC = equals(A, B) ? TRUE : FALSE; STEP; NEQ: *ptrC = !equals(A, B) ? TRUE : FALSE; STEP; IS: *ptrC = A == B ? TRUE : FALSE; STEP; NIS: *ptrC = A != B ? TRUE : FALSE; STEP; LT: *ptrC = lessThan(A, B) ? TRUE : FALSE; STEP; LE: *ptrC = (equals(A, B) || lessThan(A, B)) ? TRUE : FALSE; STEP; error: return pc - (unsigned *) activeFunc->proto->code.buf(); }