-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compiler: first attempt at restructuring
- Loading branch information
Showing
10 changed files
with
192 additions
and
164 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
compiler/src/main/kotlin/gay/pizza/pork/compiler/CodeBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package gay.pizza.pork.compiler | ||
|
||
import gay.pizza.pork.bytecode.MutableRel | ||
import gay.pizza.pork.bytecode.Op | ||
import gay.pizza.pork.bytecode.Opcode | ||
|
||
class CodeBuilder(val symbol: CompilableSymbol) { | ||
private val ops = mutableListOf<StubOp>() | ||
|
||
val localState: LocalState = LocalState(symbol) | ||
|
||
fun nextOpInst(): UInt = ops.size.toUInt() | ||
|
||
fun emit(op: StubOp) { | ||
ops.add(op) | ||
} | ||
|
||
fun emit(op: Op) { | ||
ops.add(StaticOp(op)) | ||
} | ||
|
||
fun emit(code: Opcode, arguments: List<UInt>) { | ||
emit(Op(code, arguments)) | ||
} | ||
|
||
fun emit(code: Opcode) { | ||
emit(code, emptyList()) | ||
} | ||
|
||
fun patch(code: Opcode, arguments: List<UInt>, index: Int, symbol: CompilableSymbol, rel: MutableRel) { | ||
emit(PatchRelOp(Op(code, arguments), index, symbol, rel)) | ||
} | ||
|
||
fun patch(code: Opcode, arguments: List<UInt>, index: Int, symbol: CompilableSymbol, rel: UInt) { | ||
emit(PatchRelOp(Op(code, arguments), index, symbol, MutableRel(rel))) | ||
} | ||
|
||
fun patch(code: Opcode, arguments: List<UInt>, patches: Map<Int, CompilableSymbol>) { | ||
ops.add(PatchSymOp(Op(code, arguments), patches)) | ||
} | ||
|
||
fun build(): List<StubOp> = ops.toList() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package gay.pizza.pork.compiler | ||
|
||
class Loadable( | ||
val call: CompilableSymbol? = null, | ||
val stubVar: StubVar? = null | ||
) |
55 changes: 55 additions & 0 deletions
55
compiler/src/main/kotlin/gay/pizza/pork/compiler/LocalState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package gay.pizza.pork.compiler | ||
|
||
import gay.pizza.pork.ast.gen.Symbol | ||
import gay.pizza.pork.bytecode.MutableRel | ||
|
||
class LocalState(val symbol: CompilableSymbol) { | ||
private var internalLoopState: LoopState? = null | ||
val loopState: LoopState? | ||
get() = internalLoopState | ||
|
||
private var localVarIndex: UInt = 0u | ||
private val variables = mutableListOf<MutableList<StubVar>>() | ||
|
||
fun startLoop(startOfLoop: UInt, exitJumpTarget: MutableRel) { | ||
internalLoopState = LoopState( | ||
startOfLoop = startOfLoop, | ||
exitJumpTarget = exitJumpTarget, | ||
scopeDepth = (internalLoopState?.scopeDepth ?: 0u) + 1u, | ||
enclosing = internalLoopState | ||
) | ||
} | ||
|
||
fun endLoop() { | ||
internalLoopState = internalLoopState?.enclosing | ||
} | ||
|
||
fun createLocal(symbol: Symbol): StubVar { | ||
val scope = variables.last() | ||
val variable = StubVar(localVarIndex++, symbol) | ||
scope.add(variable) | ||
return variable | ||
} | ||
|
||
fun pushScope() { | ||
variables.add(mutableListOf()) | ||
} | ||
|
||
fun popScope() { | ||
variables.removeLast() | ||
} | ||
|
||
fun resolve(symbol: Symbol): Loadable { | ||
for (scope in variables.reversed()) { | ||
val found = scope.firstOrNull { it.symbol == symbol } | ||
if (found != null) { | ||
return Loadable(stubVar = found) | ||
} | ||
} | ||
val found = this.symbol.compilableSlab.resolve(symbol) | ||
if (found != null) { | ||
return Loadable(call = found) | ||
} | ||
throw RuntimeException("Unable to resolve symbol: ${symbol.id}") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.