mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-02 21:00:56 +00:00
vm: implement local slot storage for fast lookup
This commit is contained in:
parent
f2ff23e9be
commit
3dbf8f9c4a
@ -10,6 +10,7 @@ dependencies {
|
|||||||
api(project(":compiler"))
|
api(project(":compiler"))
|
||||||
api(project(":vm"))
|
api(project(":vm"))
|
||||||
api("com.github.ajalt.clikt:clikt:4.2.0")
|
api("com.github.ajalt.clikt:clikt:4.2.0")
|
||||||
|
api("com.charleskorn.kaml:kaml:0.55.0")
|
||||||
|
|
||||||
implementation(project(":common"))
|
implementation(project(":common"))
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package gay.pizza.pork.tool
|
package gay.pizza.pork.tool
|
||||||
|
|
||||||
|
import com.charleskorn.kaml.PolymorphismStyle
|
||||||
|
import com.charleskorn.kaml.Yaml
|
||||||
import com.github.ajalt.clikt.core.CliktCommand
|
import com.github.ajalt.clikt.core.CliktCommand
|
||||||
import com.github.ajalt.clikt.parameters.arguments.argument
|
import com.github.ajalt.clikt.parameters.arguments.argument
|
||||||
import gay.pizza.dough.fs.PlatformFsProvider
|
import gay.pizza.dough.fs.PlatformFsProvider
|
||||||
@ -8,10 +10,7 @@ import gay.pizza.pork.bir.IrWorld
|
|||||||
import gay.pizza.pork.bytecode.CompiledWorld
|
import gay.pizza.pork.bytecode.CompiledWorld
|
||||||
import gay.pizza.pork.compiler.Compiler
|
import gay.pizza.pork.compiler.Compiler
|
||||||
import gay.pizza.pork.minimal.FileTool
|
import gay.pizza.pork.minimal.FileTool
|
||||||
import kotlinx.serialization.ExperimentalSerializationApi
|
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
|
|
||||||
@OptIn(ExperimentalSerializationApi::class)
|
|
||||||
class CompileCommand : CliktCommand(help = "Compile Pork to Bytecode", name = "compile") {
|
class CompileCommand : CliktCommand(help = "Compile Pork to Bytecode", name = "compile") {
|
||||||
val path by argument("file")
|
val path by argument("file")
|
||||||
|
|
||||||
@ -30,11 +29,10 @@ class CompileCommand : CliktCommand(help = "Compile Pork to Bytecode", name = "c
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun printCompiledIr(irWorld: IrWorld) {
|
private fun printCompiledIr(irWorld: IrWorld) {
|
||||||
val json = Json {
|
val yaml = Yaml(configuration = Yaml.default.configuration.copy(
|
||||||
prettyPrint = true
|
polymorphismStyle = PolymorphismStyle.Property
|
||||||
prettyPrintIndent = " "
|
))
|
||||||
}
|
println(yaml.encodeToString(IrWorld.serializer(), irWorld))
|
||||||
println(json.encodeToString(IrWorld.serializer(), irWorld))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun printCompiledWorld(compiledWorld: CompiledWorld) {
|
private fun printCompiledWorld(compiledWorld: CompiledWorld) {
|
||||||
|
@ -13,8 +13,8 @@ class InternalMachine(val world: CompiledWorld, val nativeRegistry: NativeRegist
|
|||||||
|
|
||||||
private var inst: UInt = 0u
|
private var inst: UInt = 0u
|
||||||
private val stack = mutableListOf<Any>()
|
private val stack = mutableListOf<Any>()
|
||||||
private val locals = mutableListOf<MutableMap<UInt, Any>>(
|
private val locals = mutableListOf(
|
||||||
mutableMapOf()
|
LocalSlots()
|
||||||
)
|
)
|
||||||
private val callStack = mutableListOf(0u)
|
private val callStack = mutableListOf(0u)
|
||||||
private val returnAddressStack = mutableListOf<UInt>()
|
private val returnAddressStack = mutableListOf<UInt>()
|
||||||
@ -32,7 +32,7 @@ class InternalMachine(val world: CompiledWorld, val nativeRegistry: NativeRegist
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun pushScope() {
|
fun pushScope() {
|
||||||
locals.add(mutableMapOf())
|
locals.add(LocalSlots())
|
||||||
}
|
}
|
||||||
|
|
||||||
fun popScope() {
|
fun popScope() {
|
||||||
@ -43,14 +43,13 @@ class InternalMachine(val world: CompiledWorld, val nativeRegistry: NativeRegist
|
|||||||
val constant = world.constantPool.constants[id.toInt()]
|
val constant = world.constantPool.constants[id.toInt()]
|
||||||
when (constant.tag) {
|
when (constant.tag) {
|
||||||
ConstantTag.String -> push(String(constant.value))
|
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 {
|
fun localAt(id: UInt): Any {
|
||||||
val localSet = locals.last()
|
val localSet = locals.last()
|
||||||
return localSet[id] ?:
|
return localSet.load(id)
|
||||||
throw VirtualMachineException("Attempted to load local $id but it was not stored.")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun loadLocal(id: UInt) {
|
fun loadLocal(id: UInt) {
|
||||||
@ -60,7 +59,7 @@ class InternalMachine(val world: CompiledWorld, val nativeRegistry: NativeRegist
|
|||||||
fun storeLocal(id: UInt) {
|
fun storeLocal(id: UInt) {
|
||||||
val localSet = locals.last()
|
val localSet = locals.last()
|
||||||
val value = popAnyValue()
|
val value = popAnyValue()
|
||||||
localSet[id] = value
|
localSet.store(id, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setNextInst(value: UInt) {
|
fun setNextInst(value: UInt) {
|
||||||
@ -106,7 +105,7 @@ class InternalMachine(val world: CompiledWorld, val nativeRegistry: NativeRegist
|
|||||||
callStack.clear()
|
callStack.clear()
|
||||||
callStack.add(0u)
|
callStack.add(0u)
|
||||||
locals.clear()
|
locals.clear()
|
||||||
locals.add(mutableMapOf())
|
locals.add(LocalSlots())
|
||||||
inst = 0u
|
inst = 0u
|
||||||
exitFlag = false
|
exitFlag = false
|
||||||
autoNextInst = true
|
autoNextInst = true
|
||||||
|
25
vm/src/main/kotlin/gay/pizza/pork/vm/LocalSlots.kt
Normal file
25
vm/src/main/kotlin/gay/pizza/pork/vm/LocalSlots.kt
Normal 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
|
||||||
|
}
|
||||||
|
}
|
@ -7,8 +7,8 @@ import gay.pizza.pork.vm.OpHandler
|
|||||||
|
|
||||||
object AndOpHandler : OpHandler(Opcode.And) {
|
object AndOpHandler : OpHandler(Opcode.And) {
|
||||||
override fun handle(machine: InternalMachine, op: Op) {
|
override fun handle(machine: InternalMachine, op: Op) {
|
||||||
val left = machine.pop<Boolean>()
|
|
||||||
val right = machine.pop<Boolean>()
|
val right = machine.pop<Boolean>()
|
||||||
|
val left = machine.pop<Boolean>()
|
||||||
machine.push(left && right)
|
machine.push(left && right)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,8 @@ import gay.pizza.pork.vm.OpHandler
|
|||||||
|
|
||||||
object MultiplyOpHandler : OpHandler(Opcode.Multiply) {
|
object MultiplyOpHandler : OpHandler(Opcode.Multiply) {
|
||||||
override fun handle(machine: InternalMachine, op: Op) {
|
override fun handle(machine: InternalMachine, op: Op) {
|
||||||
val left = machine.pop<Int>()
|
|
||||||
val right = machine.pop<Int>()
|
val right = machine.pop<Int>()
|
||||||
|
val left = machine.pop<Int>()
|
||||||
machine.push(left * right)
|
machine.push(left * right)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,8 @@ import gay.pizza.pork.vm.OpHandler
|
|||||||
|
|
||||||
object OrOpHandler : OpHandler(Opcode.Or) {
|
object OrOpHandler : OpHandler(Opcode.Or) {
|
||||||
override fun handle(machine: InternalMachine, op: Op) {
|
override fun handle(machine: InternalMachine, op: Op) {
|
||||||
val left = machine.pop<Boolean>()
|
|
||||||
val right = machine.pop<Boolean>()
|
val right = machine.pop<Boolean>()
|
||||||
|
val left = machine.pop<Boolean>()
|
||||||
machine.push(left || right)
|
machine.push(left || right)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user