optimization: inline some things that were procs
This commit is contained in:
parent
62995df504
commit
2f790067f3
|
@ -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 =
|
|||
# ... <funct obj> <arg1> <arg2> <arg3>
|
||||
# opCall converts it to this
|
||||
# ... <ret val> <arg1> <arg2> <arg3>
|
||||
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()
|
||||
|
|
Loading…
Reference in New Issue