diff options
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/vm.nim | 35 | ||||
-rw-r--r-- | compiler/vmdef.nim | 7 | ||||
-rw-r--r-- | compiler/vmgen.nim | 76 |
3 files changed, 85 insertions, 33 deletions
diff --git a/compiler/vm.nim b/compiler/vm.nim index 70eb6f828..8fea6b293 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -68,7 +68,9 @@ proc stackTrace(c: PCtx, tos: PStackFrame, pc: int, msg: TMsgKind, arg = "") = msgWriteln("stack trace: (most recent call last)") stackTraceAux(c, tos, pc) - localError(c.debug[pc], msg, arg) + # XXX test if we want 'globalError' for every mode + if c.mode == emRepl: globalError(c.debug[pc], msg, arg) + else: localError(c.debug[pc], msg, arg) proc bailOut(c: PCtx; tos: PStackFrame) = stackTrace(c, tos, c.exceptionInstr, errUnhandledExceptionX, @@ -333,6 +335,16 @@ proc compile(c: PCtx, s: PSym): int = when debugEchoCode: c.echoCode result #c.echoCode +template handleJmpBack() {.dirty.} = + if c.loopIterations <= 0: + if allowInfiniteLoops in c.features: + c.loopIterations = MaxLoopIterations + else: + msgWriteln("stack trace: (most recent call last)") + stackTraceAux(c, tos, pc) + globalError(c.debug[pc], errTooManyIterations) + dec(c.loopIterations) + proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg = var pc = start var tos = tos @@ -735,6 +747,9 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg = globalError(c.debug[pc], errGenerated, "VM not built with FFI support") elif prc.kind != skTemplate: let newPc = compile(c, prc) + # tricky: a recursion is also a jump back, so we use the same + # logic as for loops: + if newPc < pc: handleJmpBack() #echo "new pc ", newPc, " calling: ", prc.name.s var newFrame = PStackFrame(prc: prc, comesFrom: pc, next: tos) newSeq(newFrame.slots, prc.offset) @@ -778,6 +793,10 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg = # jump Bx let rbx = instr.regBx - wordExcess - 1 # -1 for the following 'inc pc' inc pc, rbx + of opcJmpBack: + let rbx = instr.regBx - wordExcess - 1 # -1 for the following 'inc pc' + inc pc, rbx + handleJmpBack() of opcBranch: # we know the next instruction is a 'fjmp': let branch = c.constants[instr.regBx-wordExcess] @@ -929,17 +948,15 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg = of opcReset: internalError(c.debug[pc], "too implement") of opcNarrowS: - decodeBC(rkInt) - let min = -(1 shl (rc-1)) - let max = (1 shl (rc-1))-1 - if regs[rb].intVal >= min and regs[rb].intVal <= max: - regs[ra].intVal = regs[rb].intVal - else: + decodeB(rkInt) + let min = -(1 shl (rb-1)) + let max = (1 shl (rb-1))-1 + if regs[ra].intVal < min or regs[ra].intVal > max: stackTrace(c, tos, pc, errGenerated, msgKindToString(errUnhandledExceptionX) % "value out of range") of opcNarrowU: - decodeBC(rkInt) - regs[ra].intVal = regs[rb].intVal and ((1'i64 shl rc)-1) + decodeB(rkInt) + regs[ra].intVal = regs[ra].intVal and ((1'i64 shl rb)-1) of opcIsNil: decodeB(rkInt) regs[ra].intVal = ord(regs[rb].node.kind == nkNilLit) diff --git a/compiler/vmdef.nim b/compiler/vmdef.nim index 7725d79b8..90ff8e29f 100644 --- a/compiler/vmdef.nim +++ b/compiler/vmdef.nim @@ -16,6 +16,9 @@ const byteExcess* = 128 # we use excess-K for immediates wordExcess* = 32768 + MaxLoopIterations* = 500_000 # max iterations of all loops + + type TRegister* = range[0..255] TDest* = range[-1 .. 255] @@ -110,6 +113,7 @@ type opcTJmp, # jump Bx if A != 0 opcFJmp, # jump Bx if A == 0 opcJmp, # jump Bx + opcJmpBack, # jump Bx; resulting from a while loop opcBranch, # branch for 'case' opcTry, opcExcept, @@ -182,6 +186,7 @@ type mode*: TEvalMode features*: TSandboxFlags traceActive*: bool + loopIterations*: int TPosition* = distinct int @@ -190,7 +195,7 @@ type proc newCtx*(module: PSym): PCtx = PCtx(code: @[], debug: @[], globals: newNode(nkStmtListExpr), constants: newNode(nkStmtList), types: @[], - prc: PProc(blocks: @[]), module: module) + prc: PProc(blocks: @[]), module: module, loopIterations: MaxLoopIterations) proc refresh*(c: PCtx, module: PSym) = c.module = module diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index 82d1e9ab8..59d3d2495 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -92,10 +92,10 @@ proc genLabel(c: PCtx): TPosition = result = TPosition(c.code.len) #c.jumpTargets.incl(c.code.len) -proc jmpBack(c: PCtx, n: PNode, opc: TOpcode, p = TPosition(0)) = +proc jmpBack(c: PCtx, n: PNode, p = TPosition(0)) = let dist = p.int - c.code.len internalAssert(-0x7fff < dist and dist < 0x7fff) - gABx(c, n, opc, 0, dist) + gABx(c, n, opcJmpBack, 0, dist) proc patch(c: PCtx, p: TPosition) = # patch with current index @@ -229,20 +229,20 @@ proc genWhile(c: PCtx; n: PNode) = withBlock(nil): if isTrue(n.sons[0]): c.gen(n.sons[1]) - c.jmpBack(n, opcJmp, L1) + c.jmpBack(n, L1) elif isNotOpr(n.sons[0]): var tmp = c.genx(n.sons[0].sons[1]) let L2 = c.xjmp(n, opcTJmp, tmp) c.freeTemp(tmp) c.gen(n.sons[1]) - c.jmpBack(n, opcJmp, L1) + c.jmpBack(n, L1) c.patch(L2) else: var tmp = c.genx(n.sons[0]) let L2 = c.xjmp(n, opcFJmp, tmp) c.freeTemp(tmp) c.gen(n.sons[1]) - c.jmpBack(n, opcJmp, L1) + c.jmpBack(n, L1) c.patch(L2) proc genBlock(c: PCtx; n: PNode; dest: var TDest) = @@ -543,6 +543,30 @@ proc genBinaryABC(c: PCtx; n: PNode; dest: var TDest; opc: TOpcode) = c.freeTemp(tmp) c.freeTemp(tmp2) +proc genNarrow(c: PCtx; n: PNode; dest: TDest) = + let t = skipTypes(n.typ, abstractVar-{tyTypeDesc}) + # uint is uint64 in the VM, we we only need to mask the result for + # other unsigned types: + if t.kind in {tyUInt8..tyUInt32}: + c.gABC(n, opcNarrowU, dest, TRegister(t.size*8)) + elif t.kind in {tyInt8..tyInt32}: + c.gABC(n, opcNarrowS, dest, TRegister(t.size*8)) + +proc genNarrowU(c: PCtx; n: PNode; dest: TDest) = + let t = skipTypes(n.typ, abstractVar-{tyTypeDesc}) + # uint is uint64 in the VM, we we only need to mask the result for + # other unsigned types: + if t.kind in {tyUInt8..tyUInt32, tyInt8..tyInt32}: + c.gABC(n, opcNarrowU, dest, TRegister(t.size*8)) + +proc genBinaryABCnarrow(c: PCtx; n: PNode; dest: var TDest; opc: TOpcode) = + genBinaryABC(c, n, dest, opc) + genNarrow(c, n, dest) + +proc genBinaryABCnarrowU(c: PCtx; n: PNode; dest: var TDest; opc: TOpcode) = + genBinaryABC(c, n, dest, opc) + genNarrowU(c, n, dest) + proc genSetType(c: PCtx; n: PNode; dest: TRegister) = let t = skipTypes(n.typ, abstractInst-{tyTypeDesc}) if t.kind == tySet: @@ -604,6 +628,7 @@ proc genAddSubInt(c: PCtx; n: PNode; dest: var TDest; opc: TOpcode) = c.freeTemp(tmp) else: genBinaryABC(c, n, dest, opc) + c.genNarrow(n, dest) proc genConv(c: PCtx; n, arg: PNode; dest: var TDest; opc=opcConv) = let tmp = c.genx(arg) @@ -644,6 +669,7 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest) = let tmp = c.genx(n.sons[2]) c.gABC(n, opc, d, d, tmp) c.freeTemp(tmp) + c.genNarrow(n.sons[1], d) c.genAsgnPatch(n.sons[1], d) c.freeTemp(d) of mOrd, mChr, mArrToSeq: c.gen(n.sons[1], dest) @@ -678,23 +704,23 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest) = c.freeTemp(d) c.freeTemp(tmp) of mCard: genCard(c, n, dest) - of mMulI, mMulI64: genBinaryABC(c, n, dest, opcMulInt) - of mDivI, mDivI64: genBinaryABC(c, n, dest, opcDivInt) - of mModI, mModI64: genBinaryABC(c, n, dest, opcModInt) + of mMulI, mMulI64: genBinaryABCnarrow(c, n, dest, opcMulInt) + of mDivI, mDivI64: genBinaryABCnarrow(c, n, dest, opcDivInt) + of mModI, mModI64: genBinaryABCnarrow(c, n, dest, opcModInt) of mAddF64: genBinaryABC(c, n, dest, opcAddFloat) of mSubF64: genBinaryABC(c, n, dest, opcSubFloat) of mMulF64: genBinaryABC(c, n, dest, opcMulFloat) of mDivF64: genBinaryABC(c, n, dest, opcDivFloat) - of mShrI, mShrI64: genBinaryABC(c, n, dest, opcShrInt) - of mShlI, mShlI64: genBinaryABC(c, n, dest, opcShlInt) - of mBitandI, mBitandI64: genBinaryABC(c, n, dest, opcBitandInt) - of mBitorI, mBitorI64: genBinaryABC(c, n, dest, opcBitorInt) - of mBitxorI, mBitxorI64: genBinaryABC(c, n, dest, opcBitxorInt) - of mAddU: genBinaryABC(c, n, dest, opcAddu) - of mSubU: genBinaryABC(c, n, dest, opcSubu) - of mMulU: genBinaryABC(c, n, dest, opcMulu) - of mDivU: genBinaryABC(c, n, dest, opcDivu) - of mModU: genBinaryABC(c, n, dest, opcModu) + of mShrI, mShrI64: genBinaryABCnarrowU(c, n, dest, opcShrInt) + of mShlI, mShlI64: genBinaryABCnarrowU(c, n, dest, opcShlInt) + of mBitandI, mBitandI64: genBinaryABCnarrowU(c, n, dest, opcBitandInt) + of mBitorI, mBitorI64: genBinaryABCnarrowU(c, n, dest, opcBitorInt) + of mBitxorI, mBitxorI64: genBinaryABCnarrowU(c, n, dest, opcBitxorInt) + of mAddU: genBinaryABCnarrowU(c, n, dest, opcAddu) + of mSubU: genBinaryABCnarrowU(c, n, dest, opcSubu) + of mMulU: genBinaryABCnarrowU(c, n, dest, opcMulu) + of mDivU: genBinaryABCnarrowU(c, n, dest, opcDivu) + of mModU: genBinaryABCnarrowU(c, n, dest, opcModu) of mEqI, mEqI64, mEqB, mEqEnum, mEqCh: genBinaryABC(c, n, dest, opcEqInt) of mLeI, mLeI64, mLeEnum, mLeCh, mLeB: @@ -708,12 +734,16 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest) = of mLtPtr, mLtU, mLtU64: genBinaryABC(c, n, dest, opcLtu) of mEqProc, mEqRef, mEqUntracedRef, mEqCString: genBinaryABC(c, n, dest, opcEqRef) - of mXor: genBinaryABC(c, n, dest, opcXor) + of mXor: genBinaryABCnarrowU(c, n, dest, opcXor) of mNot: genUnaryABC(c, n, dest, opcNot) - of mUnaryMinusI, mUnaryMinusI64: genUnaryABC(c, n, dest, opcUnaryMinusInt) + of mUnaryMinusI, mUnaryMinusI64: + genUnaryABC(c, n, dest, opcUnaryMinusInt) + genNarrow(c, n, dest) of mUnaryMinusF64: genUnaryABC(c, n, dest, opcUnaryMinusFloat) of mUnaryPlusI, mUnaryPlusI64, mUnaryPlusF64: gen(c, n.sons[1], dest) - of mBitnotI, mBitnotI64: genUnaryABC(c, n, dest, opcBitnotInt) + of mBitnotI, mBitnotI64: + genUnaryABC(c, n, dest, opcBitnotInt) + genNarrowU(c, n, dest) of mZe8ToI, mZe8ToI64, mZe16ToI, mZe16ToI64, mZe32ToI64, mZeIToI64, mToU8, mToU16, mToU32, mToFloat, mToBiggestFloat, mToInt, mToBiggestInt, mCharToStr, mBoolToStr, mIntToStr, mInt64ToStr, @@ -1518,7 +1548,7 @@ proc optimizeJumps(c: PCtx; start: int) = var d = i + c.code[i].jmpDiff for iters in countdown(maxIterations, 0): case c.code[d].opcode - of opcJmp: + of opcJmp, opcJmpBack: d = d + c.code[d].jmpDiff of opcTJmp, opcFJmp: if c.code[d].regA != reg: break @@ -1536,7 +1566,7 @@ proc optimizeJumps(c: PCtx; start: int) = else: break if d != i + c.code[i].jmpDiff: c.finalJumpTarget(i, d - i) - of opcJmp: + of opcJmp, opcJmpBack: var d = i + c.code[i].jmpDiff var iters = maxIterations while c.code[d].opcode == opcJmp and iters > 0: |