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) =
|
template popn(stack: var Stack[NdValue], amt: int) =
|
||||||
stack.deleteTopN(amt)
|
stack.deleteTopN(amt)
|
||||||
|
|
||||||
proc readUI8(): int =
|
template readUI8(ip: var ptr uint8): int =
|
||||||
result = ip[].int
|
let res = ip[].int
|
||||||
ip = ip.padd(1)
|
ip = ip.padd(1)
|
||||||
|
res
|
||||||
|
|
||||||
proc readDU8(): int =
|
template readDU8(ip: var ptr uint8): int =
|
||||||
result = ip.DU8ptrToInt
|
let res = ip.DU8ptrToInt
|
||||||
ip = ip.padd(argSize)
|
ip = ip.padd(argSize)
|
||||||
|
res
|
||||||
|
|
||||||
proc readConstant(): NdValue =
|
template readConstant(ip: var ptr uint8, chunk: Chunk): NdValue =
|
||||||
let index = readDU8()
|
chunk.constants[ip.readDU8()]
|
||||||
chunk.constants[index]
|
|
||||||
|
|
||||||
template frameBottom: int = frames.peek().stackBottom
|
template frameBottom: int = frames.peek().stackBottom
|
||||||
|
|
||||||
|
@ -104,13 +105,13 @@ proc run*(chunk: Chunk): InterpretResult =
|
||||||
of opPop:
|
of opPop:
|
||||||
discard stack.pop()
|
discard stack.pop()
|
||||||
of opPopA:
|
of opPopA:
|
||||||
let amt = readDU8()
|
let amt = ip.readDU8()
|
||||||
stack.popn(amt)
|
stack.popn(amt)
|
||||||
of opPopSA:
|
of opPopSA:
|
||||||
let amt = readUI8()
|
let amt = ip.readUI8()
|
||||||
stack.popn(amt)
|
stack.popn(amt)
|
||||||
of opConstant:
|
of opConstant:
|
||||||
let val: NdValue = readConstant()
|
let val: NdValue = ip.readConstant(chunk)
|
||||||
stack.add(val)
|
stack.add(val)
|
||||||
of opNegate:
|
of opNegate:
|
||||||
let res = stack.peek().negate()
|
let res = stack.peek().negate()
|
||||||
|
@ -171,62 +172,62 @@ proc run*(chunk: Chunk): InterpretResult =
|
||||||
of opPrint:
|
of opPrint:
|
||||||
echo $stack.peek()
|
echo $stack.peek()
|
||||||
of opDefineGlobal:
|
of opDefineGlobal:
|
||||||
let name = readConstant().asString()
|
let name = ip.readConstant(chunk)
|
||||||
let existed = globals.tableSet(name.fromNdString(), stack.pop())
|
let existed = globals.tableSet(name, stack.pop())
|
||||||
if existed:
|
if existed:
|
||||||
runtimeError("Attempt to redefine an existing global variable.")
|
runtimeError("Attempt to redefine an existing global variable.")
|
||||||
break
|
break
|
||||||
of opGetGlobal:
|
of opGetGlobal:
|
||||||
let name = readConstant().asString()
|
let name = ip.readConstant(chunk)
|
||||||
var val: NdValue
|
var val: NdValue
|
||||||
let existed = globals.tableGet(name.fromNdString(), val)
|
let existed = globals.tableGet(name, val)
|
||||||
if existed:
|
if existed:
|
||||||
stack.add(val)
|
stack.add(val)
|
||||||
else:
|
else:
|
||||||
runtimeError(&"Undefined global variable {name}.")
|
runtimeError(&"Undefined global variable {name}.")
|
||||||
break
|
break
|
||||||
of opSetGlobal:
|
of opSetGlobal:
|
||||||
let name = readConstant().asString()
|
let name = ip.readConstant(chunk)
|
||||||
let existed = globals.tableSet(name.fromNdString(), stack.peek())
|
let existed = globals.tableSet(name, stack.peek())
|
||||||
if not existed:
|
if not existed:
|
||||||
runtimeError("Attempt to redefine an existing global variable.")
|
runtimeError("Attempt to redefine an existing global variable.")
|
||||||
break
|
break
|
||||||
of opGetLocal:
|
of opGetLocal:
|
||||||
let slot = readDU8()
|
let slot = ip.readDU8()
|
||||||
stack.add(stack[slot + frameBottom])
|
stack.add(stack[slot + frameBottom])
|
||||||
of opSetLocal:
|
of opSetLocal:
|
||||||
let slot = readDU8()
|
let slot = ip.readDU8()
|
||||||
stack[slot + frameBottom] = stack.peek()
|
stack[slot + frameBottom] = stack.peek()
|
||||||
of opGetUpvalue:
|
of opGetUpvalue:
|
||||||
discard
|
discard
|
||||||
of opSetUpvalue:
|
of opSetUpvalue:
|
||||||
discard
|
discard
|
||||||
of opJumpIfFalse:
|
of opJumpIfFalse:
|
||||||
let offset = readDU8()
|
let offset = ip.readDU8()
|
||||||
if stack.peek().isFalsey():
|
if stack.peek().isFalsey():
|
||||||
ip = ip.padd(offset)
|
ip = ip.padd(offset)
|
||||||
of opJumpIfFalsePop:
|
of opJumpIfFalsePop:
|
||||||
let offset = readDU8()
|
let offset = ip.readDU8()
|
||||||
if stack.pop().isFalsey():
|
if stack.pop().isFalsey():
|
||||||
ip = ip.padd(offset)
|
ip = ip.padd(offset)
|
||||||
of opJump:
|
of opJump:
|
||||||
let offset = readDU8()
|
let offset = ip.readDU8()
|
||||||
ip = ip.padd(offset)
|
ip = ip.padd(offset)
|
||||||
of opLoop:
|
of opLoop:
|
||||||
let offset = readDU8()
|
let offset = ip.readDU8()
|
||||||
ip = ip.psub(offset)
|
ip = ip.psub(offset)
|
||||||
of opFunctionDef:
|
of opFunctionDef:
|
||||||
let offset = readDU8()
|
let offset = ip.readDU8()
|
||||||
let faddr: ptr uint8 = ip
|
let faddr: ptr uint8 = ip
|
||||||
ip = ip.padd(offset)
|
ip = ip.padd(offset)
|
||||||
stack.push(faddr.fromFunct())
|
stack.push(faddr.fromFunct())
|
||||||
of opClosure:
|
of opClosure:
|
||||||
let offset = readDU8()
|
let offset = ip.readDU8()
|
||||||
let faddr: ptr uint8 = ip
|
let faddr: ptr uint8 = ip
|
||||||
ip = ip.padd(offset)
|
ip = ip.padd(offset)
|
||||||
stack.push(newClosure[NdValue](faddr, 0).fromClosure())
|
stack.push(newClosure[NdValue](faddr, 0).fromClosure())
|
||||||
of opCheckArity:
|
of opCheckArity:
|
||||||
let arity = readUI8()
|
let arity = ip.readUI8()
|
||||||
let argcount = stack.high() - frameBottom
|
let argcount = stack.high() - frameBottom
|
||||||
if arity != argcount:
|
if arity != argcount:
|
||||||
runtimeError(&"Wrong number of arguments, expected {arity}, got {argcount}.")
|
runtimeError(&"Wrong number of arguments, expected {arity}, got {argcount}.")
|
||||||
|
@ -237,7 +238,7 @@ proc run*(chunk: Chunk): InterpretResult =
|
||||||
# ... <funct obj> <arg1> <arg2> <arg3>
|
# ... <funct obj> <arg1> <arg2> <arg3>
|
||||||
# opCall converts it to this
|
# opCall converts it to this
|
||||||
# ... <ret val> <arg1> <arg2> <arg3>
|
# ... <ret val> <arg1> <arg2> <arg3>
|
||||||
let argcount = readUI8()
|
let argcount = ip.readUI8()
|
||||||
let funct = stack.getIndexNeg(argcount)
|
let funct = stack.getIndexNeg(argcount)
|
||||||
|
|
||||||
stack.setIndexNeg(argcount, fromNil()) # replace the function with nil: this is the return value slot
|
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
|
break
|
||||||
|
|
||||||
of opCreateList:
|
of opCreateList:
|
||||||
let listLen = readDU8()
|
let listLen = ip.readDU8()
|
||||||
if listLen == 0:
|
if listLen == 0:
|
||||||
stack.push(newList[NdValue]().fromList())
|
stack.push(newList[NdValue]().fromList())
|
||||||
else:
|
else:
|
||||||
|
@ -255,7 +256,7 @@ proc run*(chunk: Chunk): InterpretResult =
|
||||||
stack.deleteTopN(listLen)
|
stack.deleteTopN(listLen)
|
||||||
stack.push(list.fromList())
|
stack.push(list.fromList())
|
||||||
of opCreateTable:
|
of opCreateTable:
|
||||||
let tblLen = readDU8()
|
let tblLen = ip.readDU8()
|
||||||
var tbl = newNdTable[NdValue, NdValue](tblLen)
|
var tbl = newNdTable[NdValue, NdValue](tblLen)
|
||||||
for i in countup(0, tblLen - 1):
|
for i in countup(0, tblLen - 1):
|
||||||
let val = stack.pop()
|
let val = stack.pop()
|
||||||
|
|
Loading…
Reference in New Issue