diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrBreak.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrBreak.kt index d648c0f..f64ac28 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrBreak.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrBreak.kt @@ -3,7 +3,7 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -data class IrBreak(val target: IrSymbol) : IrCodeElement { +data class IrBreak(var target: IrSymbol) : IrCodeElement() { override fun crawl(block: (IrElement) -> Unit) { block(target) } diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrCall.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrCall.kt index b1e169d..c97bba4 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrCall.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrCall.kt @@ -4,10 +4,10 @@ import kotlinx.serialization.Serializable @Serializable data class IrCall( - override val target: IrSymbol, - val arguments: List, - val variableArguments: List? -) : IrCodeElement, IrSymbolUser { + override var target: IrSymbol, + var arguments: List, + var variableArguments: List? +) : IrCodeElement(), IrSymbolUser { override fun crawl(block: (IrElement) -> Unit) { block(target) arguments.forEach(block) diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrCodeBlock.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrCodeBlock.kt index 01bf0fe..d3d6a2a 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrCodeBlock.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrCodeBlock.kt @@ -3,7 +3,7 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -data class IrCodeBlock(val items: List) : IrCodeElement { +data class IrCodeBlock(var items: List) : IrCodeElement() { override fun crawl(block: (IrElement) -> Unit) { items.forEach(block) } diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrCodeElement.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrCodeElement.kt index 2885fcb..c068291 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrCodeElement.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrCodeElement.kt @@ -3,4 +3,4 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -sealed interface IrCodeElement : IrElement +sealed class IrCodeElement : IrElement() diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrConditional.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrConditional.kt index ccca1a8..165cf83 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrConditional.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrConditional.kt @@ -4,10 +4,10 @@ import kotlinx.serialization.Serializable @Serializable data class IrConditional( - val conditional: IrCodeElement, - val ifTrue: IrCodeElement, - val ifFalse: IrCodeElement -) : IrCodeElement { + var conditional: IrCodeElement, + var ifTrue: IrCodeElement, + var ifFalse: IrCodeElement +) : IrCodeElement() { override fun crawl(block: (IrElement) -> Unit) { block(conditional) block(ifTrue) diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrConstant.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrConstant.kt index fc29f64..5097cfc 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrConstant.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrConstant.kt @@ -3,24 +3,24 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -sealed interface IrConstant : IrCodeElement { +sealed class IrConstant : IrCodeElement() { override fun crawl(block: (IrElement) -> Unit) {} } @Serializable -data class IrIntegerConstant(val value: Int) : IrConstant +data class IrIntegerConstant(var value: Int) : IrConstant() @Serializable -data class IrLongConstant(val value: Long) : IrConstant +data class IrLongConstant(var value: Long) : IrConstant() @Serializable -data class IrDoubleConstant(val value: Double) : IrConstant +data class IrDoubleConstant(var value: Double) : IrConstant() @Serializable -data class IrStringConstant(val value: String) : IrConstant +data class IrStringConstant(var value: String) : IrConstant() @Serializable -data class IrBooleanConstant(val value: Boolean) : IrConstant +data class IrBooleanConstant(var value: Boolean) : IrConstant() @Serializable -data object IrNoneConstant : IrConstant +data object IrNoneConstant : IrConstant() diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrContinue.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrContinue.kt index 444f304..313ef5b 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrContinue.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrContinue.kt @@ -3,7 +3,7 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -data class IrContinue(override val target: IrSymbol) : IrCodeElement, IrSymbolUser { +data class IrContinue(override var target: IrSymbol) : IrCodeElement(), IrSymbolUser { override fun crawl(block: (IrElement) -> Unit) { block(target) } diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrDeclare.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrDeclare.kt index 7331de2..eeae8e4 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrDeclare.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrDeclare.kt @@ -3,7 +3,7 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -data class IrDeclare(override val symbol: IrSymbol, val value: IrCodeElement) : IrCodeElement, IrSymbolOwner { +data class IrDeclare(override var symbol: IrSymbol, var value: IrCodeElement) : IrCodeElement(), IrSymbolOwner { override fun crawl(block: (IrElement) -> Unit) { value.crawl(block) } diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrDefinition.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrDefinition.kt index eefc1ab..030b7ef 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrDefinition.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrDefinition.kt @@ -4,11 +4,11 @@ import kotlinx.serialization.Serializable @Serializable data class IrDefinition( - override val symbol: IrSymbol, - val type: IrDefinitionType, - val arguments: List, - val code: IrCodeBlock -) : IrElement, IrSymbolOwner { + override var symbol: IrSymbol, + var type: IrDefinitionType, + var arguments: List, + var code: IrCodeBlock +) : IrElement(), IrSymbolOwner { override fun crawl(block: (IrElement) -> Unit) { block(symbol) arguments.forEach(block) diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrElement.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrElement.kt index 86aea1d..8457f5c 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrElement.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrElement.kt @@ -3,6 +3,6 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -sealed interface IrElement { - fun crawl(block: (IrElement) -> Unit) +sealed class IrElement { + abstract fun crawl(block: (IrElement) -> Unit) } diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrFunctionArgument.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrFunctionArgument.kt index 7998456..c31d5cc 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrFunctionArgument.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrFunctionArgument.kt @@ -3,6 +3,6 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -data class IrFunctionArgument(override val symbol: IrSymbol) : IrSymbolOwner { +data class IrFunctionArgument(override var symbol: IrSymbol) : IrElement(), IrSymbolOwner { override fun crawl(block: (IrElement) -> Unit) {} } diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrIndex.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrIndex.kt index b590283..f25d47c 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrIndex.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrIndex.kt @@ -3,7 +3,7 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -data class IrIndex(val data: IrCodeElement, val index: IrCodeElement) : IrCodeElement { +data class IrIndex(var data: IrCodeElement, var index: IrCodeElement) : IrCodeElement() { override fun crawl(block: (IrElement) -> Unit) { block(data) block(index) diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrInfix.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrInfix.kt index 943ebb8..624968c 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrInfix.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrInfix.kt @@ -3,7 +3,7 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -data class IrInfix(val op: IrInfixOp, val left: IrCodeElement, val right: IrCodeElement) : IrCodeElement { +data class IrInfix(var op: IrInfixOp, var left: IrCodeElement, var right: IrCodeElement) : IrCodeElement() { override fun crawl(block: (IrElement) -> Unit) { block(left) block(right) diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrList.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrList.kt index 338d78f..4d62bc7 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrList.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrList.kt @@ -3,7 +3,7 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -data class IrList(val items: List) : IrCodeElement { +data class IrList(var items: List) : IrCodeElement() { override fun crawl(block: (IrElement) -> Unit) { items.forEach(block) } diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrListSize.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrListSize.kt index 585663c..1d6578e 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrListSize.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrListSize.kt @@ -3,7 +3,7 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -data class IrListSize(val list: IrCodeElement) : IrCodeElement { +data class IrListSize(var list: IrCodeElement) : IrCodeElement() { override fun crawl(block: (IrElement) -> Unit) { block(list) } diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrLoad.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrLoad.kt index f2870ba..fa0d7bc 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrLoad.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrLoad.kt @@ -3,7 +3,7 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -data class IrLoad(override val target: IrSymbol) : IrCodeElement, IrSymbolUser { +data class IrLoad(override var target: IrSymbol) : IrCodeElement(), IrSymbolUser { override fun crawl(block: (IrElement) -> Unit) { block(target) } diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrLoop.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrLoop.kt index 61bbf6f..61fb4ae 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrLoop.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrLoop.kt @@ -4,10 +4,10 @@ import kotlinx.serialization.Serializable @Serializable data class IrLoop( - override val symbol: IrSymbol, - val condition: IrCodeElement, - val inner: IrCodeElement -) : IrCodeElement, IrSymbolOwner { + override var symbol: IrSymbol, + var condition: IrCodeElement, + var inner: IrCodeElement +) : IrCodeElement(), IrSymbolOwner { override fun crawl(block: (IrElement) -> Unit) { block(symbol) block(condition) diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrNativeDefinition.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrNativeDefinition.kt index 676f32c..fff2801 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrNativeDefinition.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrNativeDefinition.kt @@ -3,6 +3,6 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -data class IrNativeDefinition(val form: String, val definitions: List) : IrCodeElement { +data class IrNativeDefinition(var form: String, var definitions: List) : IrCodeElement() { override fun crawl(block: (IrElement) -> Unit) {} } diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrPrefix.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrPrefix.kt index 8f43627..b7560cf 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrPrefix.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrPrefix.kt @@ -3,7 +3,7 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -data class IrPrefix(val op: IrPrefixOp, val value: IrCodeElement) : IrCodeElement { +data class IrPrefix(var op: IrPrefixOp, var value: IrCodeElement) : IrCodeElement() { override fun crawl(block: (IrElement) -> Unit) { block(value) } diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrReturn.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrReturn.kt index 05dfacb..36b0c3c 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrReturn.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrReturn.kt @@ -3,7 +3,7 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -data class IrReturn(val from: IrSymbol, val value: IrCodeElement) : IrCodeElement { +data class IrReturn(var from: IrSymbol, var value: IrCodeElement) : IrCodeElement() { override fun crawl(block: (IrElement) -> Unit) { block(from) block(value) diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSlab.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSlab.kt index 3b72be2..6e1a8e4 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSlab.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSlab.kt @@ -4,9 +4,10 @@ import kotlinx.serialization.Serializable @Serializable data class IrSlab( - val location: IrSlabLocation, - val definitions: List -) : IrElement { + override var symbol: IrSymbol, + var location: IrSlabLocation, + var definitions: List +) : IrElement(), IrSymbolOwner { override fun crawl(block: (IrElement) -> Unit) { block(location) definitions.forEach(block) diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSlabLocation.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSlabLocation.kt index e99898b..a750a35 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSlabLocation.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSlabLocation.kt @@ -4,8 +4,8 @@ import kotlinx.serialization.Serializable @Serializable data class IrSlabLocation( - val form: String, - val path: String -) : IrElement { + var form: String, + var path: String +) : IrElement() { override fun crawl(block: (IrElement) -> Unit) {} } diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrStore.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrStore.kt index d802e23..bf0ea3d 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrStore.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrStore.kt @@ -3,7 +3,7 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -data class IrStore(override val target: IrSymbol, val value: IrCodeElement) : IrCodeElement, IrSymbolUser { +data class IrStore(override var target: IrSymbol, var value: IrCodeElement) : IrCodeElement(), IrSymbolUser { override fun crawl(block: (IrElement) -> Unit) { value.crawl(block) } diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSuffix.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSuffix.kt index 9433be9..9204d7e 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSuffix.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSuffix.kt @@ -3,7 +3,7 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -data class IrSuffix(val op: IrSuffixOp, override val target: IrSymbol) : IrCodeElement, IrSymbolUser { +data class IrSuffix(var op: IrSuffixOp, override var target: IrSymbol) : IrCodeElement(), IrSymbolUser { override fun crawl(block: (IrElement) -> Unit) { block(target) } diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbol.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbol.kt index c3cfeaf..1e0b057 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbol.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbol.kt @@ -3,7 +3,7 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -data class IrSymbol(val id: UInt, val tag: IrSymbolTag, val name: String? = null) : IrElement { +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 { diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolGraph.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolGraph.kt index 0d89130..1323bbf 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolGraph.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolGraph.kt @@ -1,7 +1,10 @@ package gay.pizza.pork.bir +import kotlinx.serialization.Serializable + +@Serializable(with = IrSymbolGraphSerializer::class) class IrSymbolGraph { - private val edges = mutableSetOf>() + private val edges = mutableSetOf() private fun crawlForKnown(known: MutableMap, root: IrElement) { if (root is IrSymbolOwner) { @@ -17,7 +20,7 @@ class IrSymbolGraph { if (root is IrSymbolUser) { val what = known[root.target] if (what != null) { - edges.add(root to what) + edges.add(IrSymbolGraphEdge(root, what)) } } @@ -26,12 +29,16 @@ class IrSymbolGraph { } } - fun crawl(root: IrElement) { + fun buildFromRoot(root: IrElement) { val known = mutableMapOf() crawlForKnown(known, root) crawlForAssociations(known, root) } + fun buildFromEdges(edges: Collection) { + this.edges.addAll(edges) + } + fun forEachEdge(block: (IrSymbolUser, IrSymbolOwner) -> Unit) { for ((from, to) in edges) { block(from, to) diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolGraphEdge.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolGraphEdge.kt new file mode 100644 index 0000000..0d478af --- /dev/null +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolGraphEdge.kt @@ -0,0 +1,6 @@ +package gay.pizza.pork.bir + +import kotlinx.serialization.Serializable + +@Serializable +data class IrSymbolGraphEdge(val user: IrSymbolUser, val owner: IrSymbolOwner) diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolGraphSerializer.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolGraphSerializer.kt new file mode 100644 index 0000000..89ebab2 --- /dev/null +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolGraphSerializer.kt @@ -0,0 +1,26 @@ +package gay.pizza.pork.bir + +import kotlinx.serialization.KSerializer +import kotlinx.serialization.builtins.ListSerializer +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder + +class IrSymbolGraphSerializer : KSerializer { + private val serializer = ListSerializer(IrSymbolGraphEdge.serializer()) + override val descriptor: SerialDescriptor = serializer.descriptor + + override fun deserialize(decoder: Decoder): IrSymbolGraph { + val graph = IrSymbolGraph() + graph.buildFromEdges(serializer.deserialize(decoder)) + return graph + } + + override fun serialize(encoder: Encoder, value: IrSymbolGraph) { + val edges = mutableListOf() + value.forEachEdge { user, owner -> + edges.add(IrSymbolGraphEdge(user, owner)) + } + serializer.serialize(encoder, edges) + } +} diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolOwner.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolOwner.kt index 8d307fb..5049bb0 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolOwner.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolOwner.kt @@ -3,6 +3,6 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -sealed interface IrSymbolOwner : IrElement { - val symbol: IrSymbol +sealed interface IrSymbolOwner { + var symbol: IrSymbol } diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolTag.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolTag.kt index 78e2e6c..a68006e 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolTag.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolTag.kt @@ -4,6 +4,7 @@ import kotlinx.serialization.Serializable @Serializable enum class IrSymbolTag { + Slab, Function, Variable, Local, diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolUser.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolUser.kt index 98ede87..ff880b7 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolUser.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolUser.kt @@ -3,6 +3,6 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -sealed interface IrSymbolUser : IrElement { - val target: IrSymbol +sealed interface IrSymbolUser { + var target: IrSymbol } diff --git a/bir/src/main/kotlin/gay/pizza/pork/bir/IrWorld.kt b/bir/src/main/kotlin/gay/pizza/pork/bir/IrWorld.kt index 0b860bc..7d982c1 100644 --- a/bir/src/main/kotlin/gay/pizza/pork/bir/IrWorld.kt +++ b/bir/src/main/kotlin/gay/pizza/pork/bir/IrWorld.kt @@ -3,7 +3,7 @@ package gay.pizza.pork.bir import kotlinx.serialization.Serializable @Serializable -data class IrWorld(val slabs: List) : IrElement { +data class IrWorld(var slabs: List) : IrElement() { override fun crawl(block: (IrElement) -> Unit) { slabs.forEach(block) } diff --git a/compiler/src/main/kotlin/gay/pizza/pork/compiler/IrCodeEmitter.kt b/compiler/src/main/kotlin/gay/pizza/pork/compiler/AstIrEmitter.kt similarity index 99% rename from compiler/src/main/kotlin/gay/pizza/pork/compiler/IrCodeEmitter.kt rename to compiler/src/main/kotlin/gay/pizza/pork/compiler/AstIrEmitter.kt index 579ad59..59969f0 100644 --- a/compiler/src/main/kotlin/gay/pizza/pork/compiler/IrCodeEmitter.kt +++ b/compiler/src/main/kotlin/gay/pizza/pork/compiler/AstIrEmitter.kt @@ -6,7 +6,7 @@ import gay.pizza.pork.bir.* import gay.pizza.pork.frontend.scope.ScopeSymbol import gay.pizza.pork.frontend.scope.SlabScope -class IrCodeEmitter( +class AstIrEmitter( val self: IrSymbol, val irSymbolWorld: IrSymbolWorld, val irSymbolAssignment: IrSymbolAssignment, diff --git a/compiler/src/main/kotlin/gay/pizza/pork/compiler/CompilableSlab.kt b/compiler/src/main/kotlin/gay/pizza/pork/compiler/CompilableSlab.kt index 36aade8..1217517 100644 --- a/compiler/src/main/kotlin/gay/pizza/pork/compiler/CompilableSlab.kt +++ b/compiler/src/main/kotlin/gay/pizza/pork/compiler/CompilableSlab.kt @@ -4,6 +4,7 @@ import gay.pizza.pork.ast.gen.Symbol import gay.pizza.pork.bir.IrDefinition import gay.pizza.pork.bir.IrSlab import gay.pizza.pork.bir.IrSlabLocation +import gay.pizza.pork.bir.IrSymbolTag import gay.pizza.pork.frontend.Slab class CompilableSlab(val compiler: Compiler, val slab: Slab) { @@ -20,11 +21,12 @@ class CompilableSlab(val compiler: Compiler, val slab: Slab) { } private fun compileIrSlab(): IrSlab { + val slabSymbol = compiler.irSymbolAssignment.next(IrSymbolTag.Slab, slab.location.commonLocationIdentity) val definitions = mutableListOf() for (compilableSymbol in compilableSymbols) { definitions.add(compilableSymbol.compiledIrDefinition) } val irSlabLocation = IrSlabLocation(slab.location.form, slab.location.filePath) - return IrSlab(irSlabLocation, definitions) + return IrSlab(slabSymbol, irSlabLocation, definitions) } } diff --git a/compiler/src/main/kotlin/gay/pizza/pork/compiler/CompilableSymbol.kt b/compiler/src/main/kotlin/gay/pizza/pork/compiler/CompilableSymbol.kt index 797e17d..d27fabb 100644 --- a/compiler/src/main/kotlin/gay/pizza/pork/compiler/CompilableSymbol.kt +++ b/compiler/src/main/kotlin/gay/pizza/pork/compiler/CompilableSymbol.kt @@ -29,7 +29,7 @@ class CompilableSymbol(val compilableSlab: CompilableSlab, val scopeSymbol: Scop private fun compileIrDefinition(): IrDefinition { val compiler = compilableSlab.compiler val functionSymbol = compiler.irSymbolWorld.create(scopeSymbol, IrSymbolTag.Function, scopeSymbol.symbol.id) - val irCodeEmitter = IrCodeEmitter( + val irCodeEmitter = AstIrEmitter( self = functionSymbol, irSymbolWorld = compiler.irSymbolWorld, irSymbolAssignment = compiler.irSymbolAssignment, diff --git a/tool/src/main/kotlin/gay/pizza/pork/tool/CompileCommand.kt b/tool/src/main/kotlin/gay/pizza/pork/tool/CompileCommand.kt index 1bb892e..085f722 100644 --- a/tool/src/main/kotlin/gay/pizza/pork/tool/CompileCommand.kt +++ b/tool/src/main/kotlin/gay/pizza/pork/tool/CompileCommand.kt @@ -4,16 +4,26 @@ import com.charleskorn.kaml.PolymorphismStyle import com.charleskorn.kaml.Yaml import com.github.ajalt.clikt.core.CliktCommand import com.github.ajalt.clikt.parameters.arguments.argument +import com.github.ajalt.clikt.parameters.options.flag +import com.github.ajalt.clikt.parameters.options.option import gay.pizza.dough.fs.PlatformFsProvider import gay.pizza.pork.ast.gen.Symbol +import gay.pizza.pork.bir.IrSymbolGraph import gay.pizza.pork.bir.IrWorld import gay.pizza.pork.bytecode.CompiledWorld import gay.pizza.pork.compiler.Compiler import gay.pizza.pork.minimal.FileTool -class CompileCommand : CliktCommand(help = "Compile Pork to Bytecode", name = "compile") { +class CompileCommand : CliktCommand(help = "Compile Pork", name = "compile") { + val showIrCode by option("--show-ir-code").flag(default = false) + val showIrSymbolGraph by option("--show-ir-symbol-graph").flag(default = false) + val path by argument("file") + private val yaml = Yaml(configuration = Yaml.default.configuration.copy( + polymorphismStyle = PolymorphismStyle.Property + )) + override fun run() { val tool = FileTool(PlatformFsProvider.resolve(path)) val world = tool.buildWorld() @@ -22,16 +32,22 @@ class CompileCommand : CliktCommand(help = "Compile Pork to Bytecode", name = "c val compiledSlab = compiler.compilableSlabs.of(slab) val compiledMain = compiledSlab.resolve(Symbol("main")) ?: throw RuntimeException("'main' function not found.") - val compiledWorld = compiler.compile(compiledMain) val irWorld = compiler.compileIrWorld() - printCompiledIr(irWorld) + val compiledWorld = compiler.compile(compiledMain) + if (showIrCode) { + printCompiledIr(irWorld) + } + + if (showIrSymbolGraph) { + val irSymbolGraph = IrSymbolGraph() + irSymbolGraph.buildFromRoot(irWorld) + println(yaml.encodeToString(IrSymbolGraph.serializer(), irSymbolGraph)) + } + printCompiledWorld(compiledWorld) } private fun printCompiledIr(irWorld: IrWorld) { - val yaml = Yaml(configuration = Yaml.default.configuration.copy( - polymorphismStyle = PolymorphismStyle.Property - )) println(yaml.encodeToString(IrWorld.serializer(), irWorld)) }