mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-06 14:41:33 +00:00
IR WIP
This commit is contained in:
7
bir/build.gradle.kts
Normal file
7
bir/build.gradle.kts
Normal file
@ -0,0 +1,7 @@
|
||||
plugins {
|
||||
id("gay.pizza.pork.module")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":common"))
|
||||
}
|
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrAccess.kt
Normal file
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrAccess.kt
Normal file
@ -0,0 +1,7 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrAccess(override val target: IrSymbol) : IrCodeElement, IrSymbolUser {
|
||||
override fun crawl(block: (IrElement) -> Unit) {
|
||||
block(target)
|
||||
}
|
||||
}
|
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrBreak.kt
Normal file
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrBreak.kt
Normal file
@ -0,0 +1,7 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrBreak(val target: IrSymbol) : IrCodeElement {
|
||||
override fun crawl(block: (IrElement) -> Unit) {
|
||||
block(target)
|
||||
}
|
||||
}
|
13
bir/src/main/kotlin/gay/pizza/pork/bir/IrCall.kt
Normal file
13
bir/src/main/kotlin/gay/pizza/pork/bir/IrCall.kt
Normal file
@ -0,0 +1,13 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrCall(
|
||||
override val target: IrSymbol,
|
||||
val arguments: List<IrCodeElement>,
|
||||
val variableArguments: List<IrCodeElement>?
|
||||
) : IrCodeElement, IrSymbolUser {
|
||||
override fun crawl(block: (IrElement) -> Unit) {
|
||||
block(target)
|
||||
arguments.forEach(block)
|
||||
variableArguments?.forEach(block)
|
||||
}
|
||||
}
|
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrCodeBlock.kt
Normal file
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrCodeBlock.kt
Normal file
@ -0,0 +1,7 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrCodeBlock(val items: List<IrCodeElement>) : IrCodeElement {
|
||||
override fun crawl(block: (IrElement) -> Unit) {
|
||||
items.forEach(block)
|
||||
}
|
||||
}
|
3
bir/src/main/kotlin/gay/pizza/pork/bir/IrCodeElement.kt
Normal file
3
bir/src/main/kotlin/gay/pizza/pork/bir/IrCodeElement.kt
Normal file
@ -0,0 +1,3 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
sealed interface IrCodeElement : IrElement
|
13
bir/src/main/kotlin/gay/pizza/pork/bir/IrConditional.kt
Normal file
13
bir/src/main/kotlin/gay/pizza/pork/bir/IrConditional.kt
Normal file
@ -0,0 +1,13 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrConditional(
|
||||
val conditional: IrCodeElement,
|
||||
val ifTrue: IrCodeElement,
|
||||
val ifFalse: IrCodeElement
|
||||
) : IrCodeElement {
|
||||
override fun crawl(block: (IrElement) -> Unit) {
|
||||
block(conditional)
|
||||
block(ifTrue)
|
||||
block(ifFalse)
|
||||
}
|
||||
}
|
12
bir/src/main/kotlin/gay/pizza/pork/bir/IrConstant.kt
Normal file
12
bir/src/main/kotlin/gay/pizza/pork/bir/IrConstant.kt
Normal file
@ -0,0 +1,12 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
sealed interface IrConstant : IrCodeElement {
|
||||
override fun crawl(block: (IrElement) -> Unit) {}
|
||||
}
|
||||
|
||||
data class IrIntegerConstant(val value: Int) : IrConstant
|
||||
data class IrLongConstant(val value: Long) : IrConstant
|
||||
data class IrDoubleConstant(val value: Double) : IrConstant
|
||||
data class IrStringConstant(val value: String) : IrConstant
|
||||
data class IrBooleanConstant(val value: Boolean) : IrConstant
|
||||
data object IrNoneConstant : IrConstant
|
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrContinue.kt
Normal file
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrContinue.kt
Normal file
@ -0,0 +1,7 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrContinue(override val target: IrSymbol) : IrCodeElement, IrSymbolUser {
|
||||
override fun crawl(block: (IrElement) -> Unit) {
|
||||
block(target)
|
||||
}
|
||||
}
|
12
bir/src/main/kotlin/gay/pizza/pork/bir/IrDefinition.kt
Normal file
12
bir/src/main/kotlin/gay/pizza/pork/bir/IrDefinition.kt
Normal file
@ -0,0 +1,12 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrDefinition(
|
||||
override val symbol: IrSymbol,
|
||||
val type: IrDefinitionType,
|
||||
val code: IrCodeBlock
|
||||
) : IrElement, IrSymbolOwner {
|
||||
override fun crawl(block: (IrElement) -> Unit) {
|
||||
block(symbol)
|
||||
block(code)
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
enum class IrDefinitionType {
|
||||
Variable,
|
||||
Function
|
||||
}
|
5
bir/src/main/kotlin/gay/pizza/pork/bir/IrElement.kt
Normal file
5
bir/src/main/kotlin/gay/pizza/pork/bir/IrElement.kt
Normal file
@ -0,0 +1,5 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
sealed interface IrElement {
|
||||
fun crawl(block: (IrElement) -> Unit)
|
||||
}
|
8
bir/src/main/kotlin/gay/pizza/pork/bir/IrInfix.kt
Normal file
8
bir/src/main/kotlin/gay/pizza/pork/bir/IrInfix.kt
Normal file
@ -0,0 +1,8 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrInfix(val op: IrInfixOp, val left: IrCodeElement, val right: IrCodeElement) : IrCodeElement {
|
||||
override fun crawl(block: (IrElement) -> Unit) {
|
||||
block(left)
|
||||
block(right)
|
||||
}
|
||||
}
|
21
bir/src/main/kotlin/gay/pizza/pork/bir/IrInfixOp.kt
Normal file
21
bir/src/main/kotlin/gay/pizza/pork/bir/IrInfixOp.kt
Normal file
@ -0,0 +1,21 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
enum class IrInfixOp {
|
||||
Add,
|
||||
Subtract,
|
||||
Multiply,
|
||||
Divide,
|
||||
Equals,
|
||||
NotEquals,
|
||||
EuclideanModulo,
|
||||
Remainder,
|
||||
Lesser,
|
||||
Greater,
|
||||
LesserEqual,
|
||||
GreaterEqual,
|
||||
BooleanAnd,
|
||||
BooleanOr,
|
||||
BinaryAnd,
|
||||
BinaryOr,
|
||||
BinaryExclusiveOr
|
||||
}
|
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrList.kt
Normal file
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrList.kt
Normal file
@ -0,0 +1,7 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrList(val items: List<IrCodeElement>) : IrCodeElement {
|
||||
override fun crawl(block: (IrElement) -> Unit) {
|
||||
items.forEach(block)
|
||||
}
|
||||
}
|
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrLoad.kt
Normal file
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrLoad.kt
Normal file
@ -0,0 +1,7 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrLoad(val symbol: IrSymbol) : IrCodeElement {
|
||||
override fun crawl(block: (IrElement) -> Unit) {
|
||||
block(symbol)
|
||||
}
|
||||
}
|
13
bir/src/main/kotlin/gay/pizza/pork/bir/IrLoop.kt
Normal file
13
bir/src/main/kotlin/gay/pizza/pork/bir/IrLoop.kt
Normal file
@ -0,0 +1,13 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrLoop(
|
||||
override val symbol: IrSymbol,
|
||||
val condition: IrCodeElement,
|
||||
val inner: IrCodeElement
|
||||
) : IrCodeElement, IrSymbolOwner {
|
||||
override fun crawl(block: (IrElement) -> Unit) {
|
||||
block(symbol)
|
||||
block(condition)
|
||||
block(inner)
|
||||
}
|
||||
}
|
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrPrefix.kt
Normal file
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrPrefix.kt
Normal file
@ -0,0 +1,7 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrPrefix(val op: IrPrefixOp, val value: IrCodeElement) : IrCodeElement {
|
||||
override fun crawl(block: (IrElement) -> Unit) {
|
||||
block(value)
|
||||
}
|
||||
}
|
8
bir/src/main/kotlin/gay/pizza/pork/bir/IrPrefixOp.kt
Normal file
8
bir/src/main/kotlin/gay/pizza/pork/bir/IrPrefixOp.kt
Normal file
@ -0,0 +1,8 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
enum class IrPrefixOp {
|
||||
BooleanNot,
|
||||
UnaryPlus,
|
||||
UnaryMinus,
|
||||
BinaryNot
|
||||
}
|
8
bir/src/main/kotlin/gay/pizza/pork/bir/IrReturn.kt
Normal file
8
bir/src/main/kotlin/gay/pizza/pork/bir/IrReturn.kt
Normal file
@ -0,0 +1,8 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrReturn(val from: IrSymbol, val value: IrCodeElement) : IrCodeElement {
|
||||
override fun crawl(block: (IrElement) -> Unit) {
|
||||
block(from)
|
||||
block(value)
|
||||
}
|
||||
}
|
11
bir/src/main/kotlin/gay/pizza/pork/bir/IrSlab.kt
Normal file
11
bir/src/main/kotlin/gay/pizza/pork/bir/IrSlab.kt
Normal file
@ -0,0 +1,11 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrSlab(
|
||||
val location: IrSlabLocation,
|
||||
val definitions: List<IrDefinition>
|
||||
) : IrElement {
|
||||
override fun crawl(block: (IrElement) -> Unit) {
|
||||
block(location)
|
||||
definitions.forEach(block)
|
||||
}
|
||||
}
|
8
bir/src/main/kotlin/gay/pizza/pork/bir/IrSlabLocation.kt
Normal file
8
bir/src/main/kotlin/gay/pizza/pork/bir/IrSlabLocation.kt
Normal file
@ -0,0 +1,8 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrSlabLocation(
|
||||
val form: String,
|
||||
val path: String
|
||||
) : IrElement {
|
||||
override fun crawl(block: (IrElement) -> Unit) {}
|
||||
}
|
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrStore.kt
Normal file
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrStore.kt
Normal file
@ -0,0 +1,7 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrStore(val symbol: IrSymbol, val value: IrCodeElement) : IrCodeElement {
|
||||
override fun crawl(block: (IrElement) -> Unit) {
|
||||
value.crawl(block)
|
||||
}
|
||||
}
|
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrSuffix.kt
Normal file
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrSuffix.kt
Normal file
@ -0,0 +1,7 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrSuffix(val op: IrSuffixOp, override val target: IrSymbol) : IrCodeElement, IrSymbolUser {
|
||||
override fun crawl(block: (IrElement) -> Unit) {
|
||||
block(target)
|
||||
}
|
||||
}
|
6
bir/src/main/kotlin/gay/pizza/pork/bir/IrSuffixOp.kt
Normal file
6
bir/src/main/kotlin/gay/pizza/pork/bir/IrSuffixOp.kt
Normal file
@ -0,0 +1,6 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
enum class IrSuffixOp {
|
||||
Increment,
|
||||
Decrement
|
||||
}
|
5
bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbol.kt
Normal file
5
bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbol.kt
Normal file
@ -0,0 +1,5 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrSymbol(val id: UInt, val tag: IrSymbolTag) : IrElement {
|
||||
override fun crawl(block: (IrElement) -> Unit) {}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
class IrSymbolAssignment {
|
||||
private var index = 0u
|
||||
|
||||
private fun nextSymbolId(): UInt = index++
|
||||
fun next(tag: IrSymbolTag): IrSymbol = IrSymbol(nextSymbolId(), tag)
|
||||
}
|
40
bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolGraph.kt
Normal file
40
bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolGraph.kt
Normal file
@ -0,0 +1,40 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
class IrSymbolGraph {
|
||||
private val edges = mutableSetOf<Pair<IrSymbolUser, IrSymbolOwner>>()
|
||||
|
||||
private fun crawlForKnown(known: MutableMap<IrSymbol, IrSymbolOwner>, root: IrElement) {
|
||||
if (root is IrSymbolOwner) {
|
||||
known[root.symbol] = root
|
||||
}
|
||||
|
||||
root.crawl { item ->
|
||||
crawlForKnown(known, item)
|
||||
}
|
||||
}
|
||||
|
||||
private fun crawlForAssociations(known: Map<IrSymbol, IrSymbolOwner>, root: IrElement) {
|
||||
if (root is IrSymbolUser) {
|
||||
val what = known[root.target]
|
||||
if (what != null) {
|
||||
edges.add(root to what)
|
||||
}
|
||||
}
|
||||
|
||||
root.crawl { item ->
|
||||
crawlForAssociations(known, item)
|
||||
}
|
||||
}
|
||||
|
||||
fun crawl(root: IrElement) {
|
||||
val known = mutableMapOf<IrSymbol, IrSymbolOwner>()
|
||||
crawlForKnown(known, root)
|
||||
crawlForAssociations(known, root)
|
||||
}
|
||||
|
||||
fun forEachEdge(block: (IrSymbolUser, IrSymbolOwner) -> Unit) {
|
||||
for ((from, to) in edges) {
|
||||
block(from, to)
|
||||
}
|
||||
}
|
||||
}
|
5
bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolOwner.kt
Normal file
5
bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolOwner.kt
Normal file
@ -0,0 +1,5 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
sealed interface IrSymbolOwner : IrElement {
|
||||
val symbol: IrSymbol
|
||||
}
|
8
bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolTag.kt
Normal file
8
bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolTag.kt
Normal file
@ -0,0 +1,8 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
enum class IrSymbolTag {
|
||||
Function,
|
||||
Variable,
|
||||
Local,
|
||||
Loop
|
||||
}
|
5
bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolUser.kt
Normal file
5
bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolUser.kt
Normal file
@ -0,0 +1,5 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
sealed interface IrSymbolUser : IrElement {
|
||||
val target: IrSymbol
|
||||
}
|
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrWorld.kt
Normal file
7
bir/src/main/kotlin/gay/pizza/pork/bir/IrWorld.kt
Normal file
@ -0,0 +1,7 @@
|
||||
package gay.pizza.pork.bir
|
||||
|
||||
data class IrWorld(val slabs: List<IrSlab>) : IrElement {
|
||||
override fun crawl(block: (IrElement) -> Unit) {
|
||||
slabs.forEach(block)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user