mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 21:21:33 +00:00
vm: very basic virtual machine
This commit is contained in:
7
bytecode/build.gradle.kts
Normal file
7
bytecode/build.gradle.kts
Normal file
@ -0,0 +1,7 @@
|
||||
plugins {
|
||||
id("gay.pizza.pork.module")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":common"))
|
||||
}
|
@ -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>
|
||||
)
|
@ -0,0 +1,3 @@
|
||||
package gay.pizza.pork.bytecode
|
||||
|
||||
class Constant(val id: UInt, val value: ByteArray)
|
@ -0,0 +1,6 @@
|
||||
package gay.pizza.pork.bytecode
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class ConstantPool(val constants: List<ByteArray>)
|
@ -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
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
package gay.pizza.pork.bytecode
|
||||
|
||||
class MutableRel(var rel: UInt)
|
6
bytecode/src/main/kotlin/gay/pizza/pork/bytecode/Op.kt
Normal file
6
bytecode/src/main/kotlin/gay/pizza/pork/bytecode/Op.kt
Normal 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>)
|
43
bytecode/src/main/kotlin/gay/pizza/pork/bytecode/Opcode.kt
Normal file
43
bytecode/src/main/kotlin/gay/pizza/pork/bytecode/Opcode.kt
Normal 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),
|
||||
}
|
3
bytecode/src/main/kotlin/gay/pizza/pork/bytecode/Ops.kt
Normal file
3
bytecode/src/main/kotlin/gay/pizza/pork/bytecode/Ops.kt
Normal file
@ -0,0 +1,3 @@
|
||||
package gay.pizza.pork.bytecode
|
||||
|
||||
class Ops(val ops: List<Op>)
|
@ -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
|
||||
)
|
@ -0,0 +1,8 @@
|
||||
package gay.pizza.pork.bytecode
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class SymbolTable(
|
||||
val symbols: List<SymbolInfo>
|
||||
)
|
Reference in New Issue
Block a user