Skip to content

Commit

Permalink
IR WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
azenla committed Nov 23, 2023
1 parent 76290a4 commit 2692e3c
Show file tree
Hide file tree
Showing 44 changed files with 669 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bir/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
id("gay.pizza.pork.module")
}

dependencies {
implementation(project(":common"))
}
7 changes: 7 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrAccess.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrBreak.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrCall.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrCodeBlock.kt
Original file line number Diff line number Diff line change
@@ -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 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
@@ -0,0 +1,3 @@
package gay.pizza.pork.bir

sealed interface IrCodeElement : IrElement
13 changes: 13 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrConditional.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 12 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrConstant.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrContinue.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 12 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrDefinition.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
6 changes: 6 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrDefinitionType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package gay.pizza.pork.bir

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

sealed interface IrElement {
fun crawl(block: (IrElement) -> Unit)
}
8 changes: 8 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrInfix.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrInfixOp.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrList.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrLoad.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrLoop.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrPrefix.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrPrefixOp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package gay.pizza.pork.bir

enum class IrPrefixOp {
BooleanNot,
UnaryPlus,
UnaryMinus,
BinaryNot
}
8 changes: 8 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrReturn.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrSlab.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrSlabLocation.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrStore.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrSuffix.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 6 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrSuffixOp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package gay.pizza.pork.bir

enum class IrSuffixOp {
Increment,
Decrement
}
5 changes: 5 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbol.kt
Original file line number Diff line number Diff line change
@@ -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) {}
}
8 changes: 8 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolAssignment.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 40 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolGraph.kt
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrSymbolOwner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package gay.pizza.pork.bir

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

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

sealed interface IrSymbolUser : IrElement {
val target: IrSymbol
}
7 changes: 7 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrWorld.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions compiler/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {

dependencies {
api(project(":ast"))
api(project(":bir"))
api(project(":bytecode"))
api(project(":parser"))
api(project(":frontend"))
Expand Down
14 changes: 14 additions & 0 deletions compiler/src/main/kotlin/gay/pizza/pork/compiler/CompilableSlab.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package gay.pizza.pork.compiler

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.frontend.Slab

class CompilableSlab(val compiler: Compiler, val slab: Slab) {
val compiledIrSlab: IrSlab by lazy { compileIrSlab() }

val compilableSymbols: List<CompilableSymbol> by lazy {
slab.scope.internalSymbols.map { symbol ->
CompilableSymbol(this, symbol)
Expand All @@ -18,4 +23,13 @@ class CompilableSlab(val compiler: Compiler, val slab: Slab) {
val scopeSymbol = slab.scope.resolve(symbol) ?: return null
return compiler.resolveOrNull(scopeSymbol)
}

private fun compileIrSlab(): IrSlab {
val definitions = mutableListOf<IrDefinition>()
for (compilableSymbol in compilableSymbols) {
definitions.add(compilableSymbol.compiledIrDefinition)
}
val irSlabLocation = IrSlabLocation(slab.location.form, slab.location.filePath)
return IrSlab(irSlabLocation, definitions)
}
}
Loading

0 comments on commit 2692e3c

Please sign in to comment.