Skip to content

Commit

Permalink
language: implement proper virtual machine support
Browse files Browse the repository at this point in the history
  • Loading branch information
azenla committed Nov 28, 2023
1 parent 8951c3c commit f2ff23e
Show file tree
Hide file tree
Showing 55 changed files with 312 additions and 63 deletions.
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrBreak.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrBreak(val target: IrSymbol) : IrCodeElement {
override fun crawl(block: (IrElement) -> Unit) {
block(target)
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrCall.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrCall(
override val target: IrSymbol,
val arguments: List<IrCodeElement>,
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrCodeBlock.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrCodeBlock(val items: List<IrCodeElement>) : IrCodeElement {
override fun crawl(block: (IrElement) -> Unit) {
items.forEach(block)
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrCodeElement.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
sealed interface IrCodeElement : IrElement
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package gay.pizza.pork.compiler

import gay.pizza.pork.bir.*
package gay.pizza.pork.bir

interface IrCodeVisitor<T> : IrVisitor<T> {
override fun visitIrDefinition(ir: IrDefinition): T {
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrConditional.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrConditional(
val conditional: IrCodeElement,
val ifTrue: IrCodeElement,
Expand Down
14 changes: 14 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrConstant.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
sealed interface IrConstant : IrCodeElement {
override fun crawl(block: (IrElement) -> Unit) {}
}

@Serializable
data class IrIntegerConstant(val value: Int) : IrConstant

@Serializable
data class IrLongConstant(val value: Long) : IrConstant

@Serializable
data class IrDoubleConstant(val value: Double) : IrConstant

@Serializable
data class IrStringConstant(val value: String) : IrConstant

@Serializable
data class IrBooleanConstant(val value: Boolean) : IrConstant

@Serializable
data object IrNoneConstant : IrConstant
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrContinue.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrContinue(override val target: IrSymbol) : IrCodeElement, IrSymbolUser {
override fun crawl(block: (IrElement) -> Unit) {
block(target)
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrDefinition.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrDefinition(
override val symbol: IrSymbol,
val type: IrDefinitionType,
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrDefinitionType.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
enum class IrDefinitionType {
Variable,
CodeFunction,
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrElement.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
sealed interface IrElement {
fun crawl(block: (IrElement) -> Unit)
}
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrFunctionArgument.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrFunctionArgument(override val symbol: IrSymbol) : IrSymbolOwner {
override fun crawl(block: (IrElement) -> Unit) {}
}
11 changes: 11 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrIndex.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrIndex(val data: IrCodeElement, val index: IrCodeElement) : IrCodeElement {
override fun crawl(block: (IrElement) -> Unit) {
block(data)
block(index)
}
}
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrInfix.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrInfix(val op: IrInfixOp, val left: IrCodeElement, val right: IrCodeElement) : IrCodeElement {
override fun crawl(block: (IrElement) -> Unit) {
block(left)
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrInfixOp.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
enum class IrInfixOp {
Add,
Subtract,
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrList.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrList(val items: List<IrCodeElement>) : IrCodeElement {
override fun crawl(block: (IrElement) -> Unit) {
items.forEach(block)
Expand Down
10 changes: 10 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrListSize.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrListSize(val list: IrCodeElement) : IrCodeElement {
override fun crawl(block: (IrElement) -> Unit) {
block(list)
}
}
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrLoad.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrLoad(override val target: IrSymbol) : IrCodeElement, IrSymbolUser {
override fun crawl(block: (IrElement) -> Unit) {
block(target)
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrLoop.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrLoop(
override val symbol: IrSymbol,
val condition: IrCodeElement,
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrNativeDefinition.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrNativeDefinition(val form: String, val definitions: List<String>) : IrCodeElement {
override fun crawl(block: (IrElement) -> Unit) {}
}
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrPrefix.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrPrefix(val op: IrPrefixOp, val value: IrCodeElement) : IrCodeElement {
override fun crawl(block: (IrElement) -> Unit) {
block(value)
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrPrefixOp.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
enum class IrPrefixOp {
BooleanNot,
UnaryPlus,
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrReturn.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrReturn(val from: IrSymbol, val value: IrCodeElement) : IrCodeElement {
override fun crawl(block: (IrElement) -> Unit) {
block(from)
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrSlab.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrSlab(
val location: IrSlabLocation,
val definitions: List<IrDefinition>
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrSlabLocation.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrSlabLocation(
val form: String,
val path: String
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrStore.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrStore(override val target: IrSymbol, val value: IrCodeElement) : IrCodeElement, IrSymbolUser {
override fun crawl(block: (IrElement) -> Unit) {
value.crawl(block)
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrSuffix.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrSuffix(val op: IrSuffixOp, override val target: IrSymbol) : IrCodeElement, IrSymbolUser {
override fun crawl(block: (IrElement) -> Unit) {
block(target)
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrSuffixOp.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
enum class IrSuffixOp {
Increment,
Decrement
Expand Down
21 changes: 20 additions & 1 deletion bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbol.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package gay.pizza.pork.bir

data class IrSymbol(val id: UInt, val tag: IrSymbolTag) : IrElement {
import kotlinx.serialization.Serializable

@Serializable
data class IrSymbol(val id: UInt, val tag: IrSymbolTag, val name: String? = null) : IrElement {
override fun crawl(block: (IrElement) -> Unit) {}

override fun equals(other: Any?): Boolean {
if (other !is IrSymbol) return false
return other.id == id && other.tag == tag
}

override fun hashCode(): Int {
var result = id.hashCode()
result = 31 * result + tag.hashCode()
return result
}

val friendlyName: String
get() = if (name != null) {
"$id $tag $name"
} else "$id $tag"
}
3 changes: 2 additions & 1 deletion bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolAssignment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ class IrSymbolAssignment {
private var index = 0u

private fun nextSymbolId(): UInt = index++
fun next(tag: IrSymbolTag): IrSymbol = IrSymbol(nextSymbolId(), tag)
fun next(tag: IrSymbolTag, name: String? = null): IrSymbol =
IrSymbol(id = nextSymbolId(), tag = tag, name = name)
}
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolOwner.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
sealed interface IrSymbolOwner : IrElement {
val symbol: IrSymbol
}
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolTag.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
enum class IrSymbolTag {
Function,
Variable,
Expand Down
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolUser.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
sealed interface IrSymbolUser : IrElement {
val target: IrSymbol
}
4 changes: 4 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrVisitor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ interface IrVisitor<T> {
fun visitIrWorld(ir: IrWorld): T
fun visitIrNativeDefinition(ir: IrNativeDefinition): T
fun visitIrFunctionArgument(ir: IrFunctionArgument): T
fun visitIrIndex(ir: IrIndex): T
fun visitIrListSize(ir: IrListSize): T

fun visit(ir: IrElement): T = when (ir) {
is IrBreak -> visitIrBeak(ir)
Expand Down Expand Up @@ -55,5 +57,7 @@ interface IrVisitor<T> {
is IrWorld -> visitIrWorld(ir)
is IrNativeDefinition -> visitIrNativeDefinition(ir)
is IrFunctionArgument -> visitIrFunctionArgument(ir)
is IrIndex -> visitIrIndex(ir)
is IrListSize -> visitIrListSize(ir)
}
}
3 changes: 3 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrWorld.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gay.pizza.pork.bir

import kotlinx.serialization.Serializable

@Serializable
data class IrWorld(val slabs: List<IrSlab>) : IrElement {
override fun crawl(block: (IrElement) -> Unit) {
slabs.forEach(block)
Expand Down
17 changes: 9 additions & 8 deletions bytecode/src/main/kotlin/gay/pizza/pork/bytecode/Opcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ enum class Opcode(val id: UByte) {
ListMake(31u),
ListSize(32u),
Integer(33u),
Double(34u),
Call(35u),
EuclideanModulo(36u),
Remainder(37u),
Index(38u),
ScopeIn(39u),
ScopeOut(40u),
ReturnAddress(41u),
Long(34u),
Double(35u),
Call(36u),
EuclideanModulo(37u),
Remainder(38u),
Index(39u),
ScopeIn(40u),
ScopeOut(41u),
ReturnAddress(42u),
End(255u),
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ class CompilableSlab(val compiler: Compiler, val slab: Slab) {
it.scopeSymbol.symbol == symbol
}

fun resolveVisible(symbol: Symbol): CompilableSymbol? {
val scopeSymbol = slab.scope.resolve(symbol) ?: return null
return compiler.resolveOrNull(scopeSymbol)
}

private fun compileIrSlab(): IrSlab {
val definitions = mutableListOf<IrDefinition>()
for (compilableSymbol in compilableSymbols) {
Expand Down
Loading

0 comments on commit f2ff23e

Please sign in to comment.