vm: implement local slot storage for fast lookup

This commit is contained in:
2023-11-29 02:16:43 -08:00
parent f2ff23e9be
commit 3dbf8f9c4a
7 changed files with 42 additions and 19 deletions

View File

@ -13,8 +13,8 @@ class InternalMachine(val world: CompiledWorld, val nativeRegistry: NativeRegist
private var inst: UInt = 0u
private val stack = mutableListOf<Any>()
private val locals = mutableListOf<MutableMap<UInt, Any>>(
mutableMapOf()
private val locals = mutableListOf(
LocalSlots()
)
private val callStack = mutableListOf(0u)
private val returnAddressStack = mutableListOf<UInt>()
@ -32,7 +32,7 @@ class InternalMachine(val world: CompiledWorld, val nativeRegistry: NativeRegist
}
fun pushScope() {
locals.add(mutableMapOf())
locals.add(LocalSlots())
}
fun popScope() {
@ -43,14 +43,13 @@ class InternalMachine(val world: CompiledWorld, val nativeRegistry: NativeRegist
val constant = world.constantPool.constants[id.toInt()]
when (constant.tag) {
ConstantTag.String -> push(String(constant.value))
else -> throw VirtualMachineException("Unknown Constant Tag: ${constant.tag.name}")
else -> throw VirtualMachineException("Unknown constant tag: ${constant.tag.name}")
}
}
fun localAt(id: UInt): Any {
val localSet = locals.last()
return localSet[id] ?:
throw VirtualMachineException("Attempted to load local $id but it was not stored.")
return localSet.load(id)
}
fun loadLocal(id: UInt) {
@ -60,7 +59,7 @@ class InternalMachine(val world: CompiledWorld, val nativeRegistry: NativeRegist
fun storeLocal(id: UInt) {
val localSet = locals.last()
val value = popAnyValue()
localSet[id] = value
localSet.store(id, value)
}
fun setNextInst(value: UInt) {
@ -106,7 +105,7 @@ class InternalMachine(val world: CompiledWorld, val nativeRegistry: NativeRegist
callStack.clear()
callStack.add(0u)
locals.clear()
locals.add(mutableMapOf())
locals.add(LocalSlots())
inst = 0u
exitFlag = false
autoNextInst = true

View File

@ -0,0 +1,25 @@
package gay.pizza.pork.vm
class LocalSlots {
private var slots = arrayOfNulls<Any>(4)
fun load(index: UInt): Any {
return slots[index.toInt()]
?: throw VirtualMachineException("Attempted to access local $index that is not stored")
}
fun store(index: UInt, value: Any) {
if (index >= slots.size.toUInt()) {
resize(index + 4u)
}
slots[index.toInt()] = value
}
private fun resize(count: UInt) {
val values = arrayOfNulls<Any>(count.toInt())
for ((i, value) in slots.withIndex()) {
values[i] = value
}
slots = values
}
}

View File

@ -7,8 +7,8 @@ import gay.pizza.pork.vm.OpHandler
object AndOpHandler : OpHandler(Opcode.And) {
override fun handle(machine: InternalMachine, op: Op) {
val left = machine.pop<Boolean>()
val right = machine.pop<Boolean>()
val left = machine.pop<Boolean>()
machine.push(left && right)
}
}

View File

@ -7,8 +7,8 @@ import gay.pizza.pork.vm.OpHandler
object MultiplyOpHandler : OpHandler(Opcode.Multiply) {
override fun handle(machine: InternalMachine, op: Op) {
val left = machine.pop<Int>()
val right = machine.pop<Int>()
val left = machine.pop<Int>()
machine.push(left * right)
}
}

View File

@ -7,8 +7,8 @@ import gay.pizza.pork.vm.OpHandler
object OrOpHandler : OpHandler(Opcode.Or) {
override fun handle(machine: InternalMachine, op: Op) {
val left = machine.pop<Boolean>()
val right = machine.pop<Boolean>()
val left = machine.pop<Boolean>()
machine.push(left || right)
}
}