From 2f790067f31f506a455bec748068b578300a8fd5 Mon Sep 17 00:00:00 2001 From: prod2 <95874442+prod2@users.noreply.github.com> Date: Sun, 6 Feb 2022 02:46:26 +0100 Subject: [PATCH] optimization: inline some things that were procs --- src/ndspkg/vm.nim | 57 ++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/src/ndspkg/vm.nim b/src/ndspkg/vm.nim index 23d9140..72d9a56 100644 --- a/src/ndspkg/vm.nim +++ b/src/ndspkg/vm.nim @@ -51,17 +51,18 @@ proc run*(chunk: Chunk): InterpretResult = template popn(stack: var Stack[NdValue], amt: int) = stack.deleteTopN(amt) - proc readUI8(): int = - result = ip[].int + template readUI8(ip: var ptr uint8): int = + let res = ip[].int ip = ip.padd(1) + res - proc readDU8(): int = - result = ip.DU8ptrToInt + template readDU8(ip: var ptr uint8): int = + let res = ip.DU8ptrToInt ip = ip.padd(argSize) + res - proc readConstant(): NdValue = - let index = readDU8() - chunk.constants[index] + template readConstant(ip: var ptr uint8, chunk: Chunk): NdValue = + chunk.constants[ip.readDU8()] template frameBottom: int = frames.peek().stackBottom @@ -104,13 +105,13 @@ proc run*(chunk: Chunk): InterpretResult = of opPop: discard stack.pop() of opPopA: - let amt = readDU8() + let amt = ip.readDU8() stack.popn(amt) of opPopSA: - let amt = readUI8() + let amt = ip.readUI8() stack.popn(amt) of opConstant: - let val: NdValue = readConstant() + let val: NdValue = ip.readConstant(chunk) stack.add(val) of opNegate: let res = stack.peek().negate() @@ -171,62 +172,62 @@ proc run*(chunk: Chunk): InterpretResult = of opPrint: echo $stack.peek() of opDefineGlobal: - let name = readConstant().asString() - let existed = globals.tableSet(name.fromNdString(), stack.pop()) + let name = ip.readConstant(chunk) + let existed = globals.tableSet(name, stack.pop()) if existed: runtimeError("Attempt to redefine an existing global variable.") break of opGetGlobal: - let name = readConstant().asString() + let name = ip.readConstant(chunk) var val: NdValue - let existed = globals.tableGet(name.fromNdString(), val) + let existed = globals.tableGet(name, val) if existed: stack.add(val) else: runtimeError(&"Undefined global variable {name}.") break of opSetGlobal: - let name = readConstant().asString() - let existed = globals.tableSet(name.fromNdString(), stack.peek()) + let name = ip.readConstant(chunk) + let existed = globals.tableSet(name, stack.peek()) if not existed: runtimeError("Attempt to redefine an existing global variable.") break of opGetLocal: - let slot = readDU8() + let slot = ip.readDU8() stack.add(stack[slot + frameBottom]) of opSetLocal: - let slot = readDU8() + let slot = ip.readDU8() stack[slot + frameBottom] = stack.peek() of opGetUpvalue: discard of opSetUpvalue: discard of opJumpIfFalse: - let offset = readDU8() + let offset = ip.readDU8() if stack.peek().isFalsey(): ip = ip.padd(offset) of opJumpIfFalsePop: - let offset = readDU8() + let offset = ip.readDU8() if stack.pop().isFalsey(): ip = ip.padd(offset) of opJump: - let offset = readDU8() + let offset = ip.readDU8() ip = ip.padd(offset) of opLoop: - let offset = readDU8() + let offset = ip.readDU8() ip = ip.psub(offset) of opFunctionDef: - let offset = readDU8() + let offset = ip.readDU8() let faddr: ptr uint8 = ip ip = ip.padd(offset) stack.push(faddr.fromFunct()) of opClosure: - let offset = readDU8() + let offset = ip.readDU8() let faddr: ptr uint8 = ip ip = ip.padd(offset) stack.push(newClosure[NdValue](faddr, 0).fromClosure()) of opCheckArity: - let arity = readUI8() + let arity = ip.readUI8() let argcount = stack.high() - frameBottom if arity != argcount: runtimeError(&"Wrong number of arguments, expected {arity}, got {argcount}.") @@ -237,7 +238,7 @@ proc run*(chunk: Chunk): InterpretResult = # ... # opCall converts it to this # ... - let argcount = readUI8() + let argcount = ip.readUI8() let funct = stack.getIndexNeg(argcount) stack.setIndexNeg(argcount, fromNil()) # replace the function with nil: this is the return value slot @@ -246,7 +247,7 @@ proc run*(chunk: Chunk): InterpretResult = break of opCreateList: - let listLen = readDU8() + let listLen = ip.readDU8() if listLen == 0: stack.push(newList[NdValue]().fromList()) else: @@ -255,7 +256,7 @@ proc run*(chunk: Chunk): InterpretResult = stack.deleteTopN(listLen) stack.push(list.fromList()) of opCreateTable: - let tblLen = readDU8() + let tblLen = ip.readDU8() var tbl = newNdTable[NdValue, NdValue](tblLen) for i in countup(0, tblLen - 1): let val = stack.pop()