Skip to content

Commit

Permalink
bytecode: enhance symbol table with both slab and symbol name
Browse files Browse the repository at this point in the history
  • Loading branch information
azenla committed Nov 22, 2023
1 parent 6211ad4 commit 76290a4
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import kotlinx.serialization.Serializable

@Serializable
data class SymbolInfo(
val id: String,
val slab: String,
val symbol: String,
val offset: UInt,
val size: UInt
)
) {
val commonSymbolIdentity: String by lazy { "$slab $symbol" }
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CompilableSymbol(val compilableSlab: CompilableSlab, val scopeSymbol: Scop
}

val id: String
get() = "${compilableSlab.slab.location.commonFriendlyName} ${scopeSymbol.symbol.id}"
get() = "${compilableSlab.slab.location.commonLocationIdentity} ${scopeSymbol.symbol.id}"

override fun toString(): String = "${compilableSlab.slab.location.commonFriendlyName} ${scopeSymbol.symbol.id}"
override fun toString(): String = "${compilableSlab.slab.location.commonLocationIdentity} ${scopeSymbol.symbol.id}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ class CompiledWorldLayout(val compiler: Compiler) : StubResolutionContext {
val start = allStubOps.size
val result = symbol.compiledStubOps
val stubOps = result.ops
symbolTable[symbol] = SymbolInfo(symbol.id, start.toUInt(), stubOps.size.toUInt())
symbolTable[symbol] = SymbolInfo(
slab = symbol.compilableSlab.slab.location.commonLocationIdentity,
symbol = symbol.id,
offset = start.toUInt(),
size = stubOps.size.toUInt()
)
allStubOps.addAll(stubOps)
allStubAnnotations.addAll(result.annotations)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Compiler {
fun resolve(scopeSymbol: ScopeSymbol): CompilableSymbol = resolveOrNull(scopeSymbol) ?:
throw RuntimeException(
"Unable to resolve scope symbol: " +
"${scopeSymbol.slabScope.slab.location.commonFriendlyName} ${scopeSymbol.symbol.id}")
"${scopeSymbol.slabScope.slab.location.commonLocationIdentity} ${scopeSymbol.symbol.id}")

fun contributeCompiledSymbols(
into: MutableSet<CompilableSymbol>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import gay.pizza.pork.ast.gen.FunctionDefinition
import gay.pizza.pork.execution.ArgumentList

class FunctionContext(val slabContext: SlabContext, val node: FunctionDefinition) : CallableFunction {
val name: String by lazy { "${slabContext.slab.location.commonFriendlyName} ${node.symbol.id}" }
val name: String by lazy { "${slabContext.slab.location.commonLocationIdentity} ${node.symbol.id}" }

private fun resolveMaybeNative(): CallableFunction? = if (node.nativeFunctionDescriptor == null) {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import gay.pizza.pork.ast.gen.visit
import gay.pizza.pork.frontend.Slab

class SlabContext(val slab: Slab, val evaluator: Evaluator, rootScope: Scope) {
val internalScope = rootScope.fork("internal ${slab.location.commonFriendlyName}")
val externalScope = rootScope.fork("external ${slab.location.commonFriendlyName}")
val internalScope = rootScope.fork("internal ${slab.location.commonLocationIdentity}")
val externalScope = rootScope.fork("external ${slab.location.commonLocationIdentity}")

init {
processAllDefinitions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gay.pizza.pork.frontend
import gay.pizza.pork.tokenizer.SourceIndex

data class SourceLocation(val form: String, val filePath: String, val index: SourceIndex? = null) {
val commonFriendlyName: String by lazy { "$form $filePath" }
val commonLocationIdentity: String by lazy { "$form $filePath" }

fun withSourceIndex(index: SourceIndex): SourceLocation =
SourceLocation(form, filePath, index)
Expand Down
2 changes: 1 addition & 1 deletion tool/src/main/kotlin/gay/pizza/pork/tool/CompileCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CompileCommand : CliktCommand(help = "Compile Pork to Bytecode", name = "c
val compiledWorld = compiler.compile(compiledMain)
for (symbol in compiledWorld.symbolTable.symbols) {
val code = compiledWorld.code.subList(symbol.offset.toInt(), (symbol.offset + symbol.size).toInt())
println(symbol.id)
println(symbol.commonSymbolIdentity)
for ((index, op) in code.withIndex()) {
var annotation = ""
val annotations = compiledWorld.annotations.filter { it.inst == (symbol.offset + index.toUInt()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ScopeAnalysisCommand : CliktCommand(help = "Run Scope Analysis", name = "s
"symbol ${visibleScopeSymbol.scopeSymbol.symbol.id} " +
"type=${visibleScopeSymbol.scopeSymbol.definition.type.name} " +
"internal=${visibleScopeSymbol.isInternalSymbol} " +
"slab=${visibleScopeSymbol.scopeSymbol.slabScope.slab.location.commonFriendlyName}"
"slab=${visibleScopeSymbol.scopeSymbol.slabScope.slab.location.commonLocationIdentity}"
)
}
}
Expand Down

0 comments on commit 76290a4

Please sign in to comment.