vm: very basic virtual machine

This commit is contained in:
2023-11-14 23:44:10 -08:00
parent 8c48c93663
commit 041848c14e
92 changed files with 1652 additions and 243 deletions

View File

@ -0,0 +1,7 @@
plugins {
id("gay.pizza.pork.module")
}
dependencies {
implementation(project(":common"))
}

View File

@ -0,0 +1,10 @@
package gay.pizza.pork.bytecode
import kotlinx.serialization.Serializable
@Serializable
data class CompiledWorld(
val constantPool: ConstantPool,
val symbolTable: SymbolTable,
val code: List<Op>
)

View File

@ -0,0 +1,3 @@
package gay.pizza.pork.bytecode
class Constant(val id: UInt, val value: ByteArray)

View File

@ -0,0 +1,6 @@
package gay.pizza.pork.bytecode
import kotlinx.serialization.Serializable
@Serializable
data class ConstantPool(val constants: List<ByteArray>)

View File

@ -0,0 +1,18 @@
package gay.pizza.pork.bytecode
class MutableConstantPool {
private val pool = mutableListOf<Constant>()
fun assign(content: ByteArray): UInt {
for (constant in pool) {
if (constant.value.contentEquals(content)) {
return constant.id
}
}
val id = pool.size.toUInt()
pool.add(Constant(id, content))
return id
}
fun all(): List<Constant> = pool
}

View File

@ -0,0 +1,3 @@
package gay.pizza.pork.bytecode
class MutableRel(var rel: UInt)

View File

@ -0,0 +1,6 @@
package gay.pizza.pork.bytecode
import kotlinx.serialization.Serializable
@Serializable
data class Op(val code: Opcode, val args: List<UInt>)

View File

@ -0,0 +1,43 @@
package gay.pizza.pork.bytecode
enum class Opcode(val id: UByte) {
Constant(1u),
None(2u),
False(3u),
True(4u),
Pop(5u),
Jump(6u),
JumpIf(7u),
Not(8u),
UnaryPlus(9u),
UnaryMinus(10u),
BinaryNot(11u),
And(20u),
Native(24u),
Return(10u),
StoreLocal(16u),
LoadLocal(17u),
Add(18u),
Subtract(19u),
Multiply(20u),
Divide(21u),
CompareEqual(22u),
CompareLesser(23u),
CompareGreater(24u),
CompareLesserEqual(25u),
CompareGreaterEqual(26u),
Or(27u),
BinaryAnd(28u),
BinaryOr(29u),
BinaryXor(30u),
List(31u),
Integer(32u),
Double(33u),
Call(34u),
EuclideanModulo(35u),
Remainder(36u),
Index(37u),
ScopeIn(38u),
ScopeOut(39u),
End(255u),
}

View File

@ -0,0 +1,3 @@
package gay.pizza.pork.bytecode
class Ops(val ops: List<Op>)

View File

@ -0,0 +1,10 @@
package gay.pizza.pork.bytecode
import kotlinx.serialization.Serializable
@Serializable
data class SymbolInfo(
val id: String,
val offset: UInt,
val size: UInt
)

View File

@ -0,0 +1,8 @@
package gay.pizza.pork.bytecode
import kotlinx.serialization.Serializable
@Serializable
data class SymbolTable(
val symbols: List<SymbolInfo>
)