Skip to content

Commit

Permalink
compiler: declare symbols before store so that symbol graph works
Browse files Browse the repository at this point in the history
  • Loading branch information
azenla committed Nov 29, 2023
1 parent 3dbf8f9 commit 962d079
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
10 changes: 10 additions & 0 deletions bir/src/main/kotlin/gay/pizza/pork/bir/IrDeclare.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 IrDeclare(override val symbol: IrSymbol, val value: IrCodeElement) : IrCodeElement, IrSymbolOwner {
override fun crawl(block: (IrElement) -> Unit) {
value.crawl(block)
}
}
2 changes: 2 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 @@ -29,6 +29,7 @@ interface IrVisitor<T> {
fun visitIrFunctionArgument(ir: IrFunctionArgument): T
fun visitIrIndex(ir: IrIndex): T
fun visitIrListSize(ir: IrListSize): T
fun visitIrDeclare(ir: IrDeclare): T

fun visit(ir: IrElement): T = when (ir) {
is IrBreak -> visitIrBeak(ir)
Expand Down Expand Up @@ -59,5 +60,6 @@ interface IrVisitor<T> {
is IrFunctionArgument -> visitIrFunctionArgument(ir)
is IrIndex -> visitIrIndex(ir)
is IrListSize -> visitIrListSize(ir)
is IrDeclare -> visitIrDeclare(ir)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class IrCodeEmitter(

override fun visitLetAssignment(node: LetAssignment): IrCodeElement {
val symbol = createLocalVariable(node.symbol)
return IrStore(symbol, node.value.visit(this))
return IrDeclare(symbol, node.value.visit(this))
}

override fun visitListLiteral(node: ListLiteral): IrCodeElement =
Expand Down Expand Up @@ -296,7 +296,7 @@ class IrCodeEmitter(

override fun visitVarAssignment(node: VarAssignment): IrCodeElement {
val local = createLocalVariable(node.symbol)
return IrStore(local, node.value.visit(this))
return IrDeclare(local, node.value.visit(this))
}

override fun visitWhile(node: While): IrCodeElement = loop { symbol ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ class IrStubOpEmitter(val irDefinition: IrDefinition, val code: CodeBuilder) : I
code.emit(Opcode.Return)
}

override fun visitIrDeclare(ir: IrDeclare) {
visit(ir.value)
val variable = code.localState.createOrFindLocal(ir.symbol)
store(variable)
}

override fun visitIrStore(ir: IrStore) {
visit(ir.value)
val variable = code.localState.createOrFindLocal(ir.target)
Expand Down

0 comments on commit 962d079

Please sign in to comment.