|
|
|
@ -3,99 +3,55 @@ package gay.pizza.pork.compiler
|
|
|
|
|
import gay.pizza.pork.ast.FunctionLevelVisitor
|
|
|
|
|
import gay.pizza.pork.ast.gen.*
|
|
|
|
|
import gay.pizza.pork.bytecode.MutableRel
|
|
|
|
|
import gay.pizza.pork.bytecode.Op
|
|
|
|
|
import gay.pizza.pork.bytecode.Opcode
|
|
|
|
|
|
|
|
|
|
class StubOpEmitter(val compiler: Compiler, val symbol: CompilableSymbol) : FunctionLevelVisitor<Unit>() {
|
|
|
|
|
private val ops = mutableListOf<StubOp>()
|
|
|
|
|
|
|
|
|
|
private var loopState: LoopState? = null
|
|
|
|
|
private val localVariables = mutableListOf<MutableList<StubVar>>()
|
|
|
|
|
private var localVarIndex = 0u
|
|
|
|
|
|
|
|
|
|
private val requiredLoopState: LoopState
|
|
|
|
|
get() {
|
|
|
|
|
if (loopState != null) {
|
|
|
|
|
return loopState!!
|
|
|
|
|
}
|
|
|
|
|
throw RuntimeException("loopState expected but was not found.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun allocateLocalVariable(symbol: Symbol): StubVar {
|
|
|
|
|
val scope = localVariables.last()
|
|
|
|
|
val variable = StubVar(localVarIndex++, symbol)
|
|
|
|
|
scope.add(variable)
|
|
|
|
|
return variable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun resolveSymbol(symbol: Symbol): CallOrStubVar {
|
|
|
|
|
for (scope in localVariables.reversed()) {
|
|
|
|
|
val found = scope.firstOrNull { it.symbol == symbol }
|
|
|
|
|
if (found != null) {
|
|
|
|
|
return CallOrStubVar(stubVar = found)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
val found = this.symbol.compilableSlab.compilableSymbolOf(symbol)
|
|
|
|
|
if (found != null) {
|
|
|
|
|
return CallOrStubVar(call = found)
|
|
|
|
|
}
|
|
|
|
|
throw RuntimeException("Unable to resolve symbol: ${symbol.id}")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun pushScope() {
|
|
|
|
|
emit(Opcode.ScopeIn)
|
|
|
|
|
localVariables.add(mutableListOf())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun popScope() {
|
|
|
|
|
emit(Opcode.ScopeOut)
|
|
|
|
|
localVariables.removeLast()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun enter() {
|
|
|
|
|
pushScope()
|
|
|
|
|
}
|
|
|
|
|
val code = CodeBuilder(symbol)
|
|
|
|
|
|
|
|
|
|
fun allocateOuterScope(definition: FunctionDefinition) {
|
|
|
|
|
val allNormalArguments = definition.arguments.takeWhile { !it.multiple }
|
|
|
|
|
val varArgument = definition.arguments.firstOrNull { it.multiple }
|
|
|
|
|
|
|
|
|
|
for (arg in allNormalArguments.reversed()) {
|
|
|
|
|
val functionLocal = allocateLocalVariable(arg.symbol)
|
|
|
|
|
emit(Opcode.StoreLocal, listOf(functionLocal.index))
|
|
|
|
|
val functionLocal = code.localState.createLocal(arg.symbol)
|
|
|
|
|
code.emit(Opcode.StoreLocal, listOf(functionLocal.index))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (varArgument != null) {
|
|
|
|
|
val functionLocal = allocateLocalVariable(varArgument.symbol)
|
|
|
|
|
emit(Opcode.StoreLocal, listOf(functionLocal.index))
|
|
|
|
|
val functionLocal = code.localState.createLocal(varArgument.symbol)
|
|
|
|
|
code.emit(Opcode.StoreLocal, listOf(functionLocal.index))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun enter() {
|
|
|
|
|
code.localState.pushScope()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun exit() {
|
|
|
|
|
popScope()
|
|
|
|
|
emit(Opcode.Return)
|
|
|
|
|
code.localState.popScope()
|
|
|
|
|
code.emit(Opcode.Return)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitBlock(node: Block) {
|
|
|
|
|
pushScope()
|
|
|
|
|
code.localState.pushScope()
|
|
|
|
|
node.visitChildren(this)
|
|
|
|
|
popScope()
|
|
|
|
|
code.localState.popScope()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitBooleanLiteral(node: BooleanLiteral) {
|
|
|
|
|
emit(if (node.value) Opcode.True else Opcode.False)
|
|
|
|
|
code.emit(if (node.value) Opcode.True else Opcode.False)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitBreak(node: Break) {
|
|
|
|
|
patch(Opcode.Jump, listOf(0u), 0, symbol, requiredLoopState.exitJumpTarget)
|
|
|
|
|
code.patch(Opcode.Jump, listOf(0u), 0, symbol, code.localState.loopState!!.exitJumpTarget)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitContinue(node: Continue) {
|
|
|
|
|
patch(Opcode.Jump, listOf(0u), 0, symbol, requiredLoopState.startOfLoop)
|
|
|
|
|
code.patch(Opcode.Jump, listOf(0u), 0, symbol, code.localState.loopState!!.startOfLoop)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitDoubleLiteral(node: DoubleLiteral) {
|
|
|
|
|
emit(Opcode.Integer, listOf(node.value.toUInt()))
|
|
|
|
|
code.emit(Opcode.Integer, listOf(node.value.toUInt()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitForIn(node: ForIn) {
|
|
|
|
@ -108,7 +64,7 @@ class StubOpEmitter(val compiler: Compiler, val symbol: CompilableSymbol) : Func
|
|
|
|
|
val targetSymbol = compiler.resolve(targetScopeSymbol)
|
|
|
|
|
val functionDefinition = targetSymbol.scopeSymbol.definition as FunctionDefinition
|
|
|
|
|
val retRel = MutableRel(0u)
|
|
|
|
|
patch(Opcode.Integer, listOf(0u), 0, symbol, retRel)
|
|
|
|
|
code.patch(Opcode.Integer, listOf(0u), 0, symbol, retRel)
|
|
|
|
|
|
|
|
|
|
val normalArguments = mutableListOf<Expression>()
|
|
|
|
|
var variableArguments: List<Expression>? = null
|
|
|
|
@ -127,70 +83,70 @@ class StubOpEmitter(val compiler: Compiler, val symbol: CompilableSymbol) : Func
|
|
|
|
|
for (item in variableArguments.reversed()) {
|
|
|
|
|
item.visit(this)
|
|
|
|
|
}
|
|
|
|
|
emit(Opcode.List, listOf(variableArguments.size.toUInt()))
|
|
|
|
|
code.emit(Opcode.List, listOf(variableArguments.size.toUInt()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (item in normalArguments.reversed()) {
|
|
|
|
|
visit(item)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
retRel.rel = (ops.size + 1).toUInt()
|
|
|
|
|
patch(Opcode.Call, listOf(0u), mapOf(0 to targetSymbol))
|
|
|
|
|
retRel.rel = code.nextOpInst() + 1u
|
|
|
|
|
code.patch(Opcode.Call, listOf(0u), mapOf(0 to targetSymbol))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitIf(node: If) {
|
|
|
|
|
val thenRel = MutableRel(0u)
|
|
|
|
|
val endRel = MutableRel(0u)
|
|
|
|
|
node.condition.visit(this)
|
|
|
|
|
patch(Opcode.JumpIf, listOf(0u), 0, symbol, thenRel)
|
|
|
|
|
code.patch(Opcode.JumpIf, listOf(0u), 0, symbol, thenRel)
|
|
|
|
|
node.elseBlock?.visit(this)
|
|
|
|
|
patch(Opcode.Jump, listOf(0u), 0, symbol, endRel)
|
|
|
|
|
thenRel.rel = ops.size.toUInt()
|
|
|
|
|
code.patch(Opcode.Jump, listOf(0u), 0, symbol, endRel)
|
|
|
|
|
thenRel.rel = code.nextOpInst()
|
|
|
|
|
node.thenBlock.visit(this)
|
|
|
|
|
endRel.rel = ops.size.toUInt()
|
|
|
|
|
endRel.rel = code.nextOpInst()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitIndexedBy(node: IndexedBy) {
|
|
|
|
|
node.expression.visit(this)
|
|
|
|
|
node.index.visit(this)
|
|
|
|
|
emit(Opcode.Index)
|
|
|
|
|
code.emit(Opcode.Index)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitInfixOperation(node: InfixOperation) {
|
|
|
|
|
node.left.visit(this)
|
|
|
|
|
node.right.visit(this)
|
|
|
|
|
when (node.op) {
|
|
|
|
|
InfixOperator.Plus -> emit(Opcode.Add)
|
|
|
|
|
InfixOperator.Minus -> emit(Opcode.Subtract)
|
|
|
|
|
InfixOperator.Multiply -> emit(Opcode.Multiply)
|
|
|
|
|
InfixOperator.Divide -> emit(Opcode.Divide)
|
|
|
|
|
InfixOperator.Equals -> emit(Opcode.CompareEqual)
|
|
|
|
|
InfixOperator.Plus -> code.emit(Opcode.Add)
|
|
|
|
|
InfixOperator.Minus -> code.emit(Opcode.Subtract)
|
|
|
|
|
InfixOperator.Multiply -> code.emit(Opcode.Multiply)
|
|
|
|
|
InfixOperator.Divide -> code.emit(Opcode.Divide)
|
|
|
|
|
InfixOperator.Equals -> code.emit(Opcode.CompareEqual)
|
|
|
|
|
InfixOperator.NotEquals -> {
|
|
|
|
|
emit(Opcode.CompareEqual)
|
|
|
|
|
emit(Opcode.Not)
|
|
|
|
|
code.emit(Opcode.CompareEqual)
|
|
|
|
|
code.emit(Opcode.Not)
|
|
|
|
|
}
|
|
|
|
|
InfixOperator.EuclideanModulo -> emit(Opcode.EuclideanModulo)
|
|
|
|
|
InfixOperator.Remainder -> emit(Opcode.Remainder)
|
|
|
|
|
InfixOperator.Lesser -> emit(Opcode.CompareLesser)
|
|
|
|
|
InfixOperator.Greater -> emit(Opcode.CompareGreater)
|
|
|
|
|
InfixOperator.GreaterEqual -> emit(Opcode.CompareGreaterEqual)
|
|
|
|
|
InfixOperator.LesserEqual -> emit(Opcode.CompareLesserEqual)
|
|
|
|
|
InfixOperator.BooleanAnd -> emit(Opcode.And)
|
|
|
|
|
InfixOperator.BooleanOr -> emit(Opcode.Or)
|
|
|
|
|
InfixOperator.BinaryAnd -> emit(Opcode.BinaryAnd)
|
|
|
|
|
InfixOperator.BinaryOr -> emit(Opcode.BinaryOr)
|
|
|
|
|
InfixOperator.BinaryExclusiveOr -> emit(Opcode.BinaryXor)
|
|
|
|
|
InfixOperator.EuclideanModulo -> code.emit(Opcode.EuclideanModulo)
|
|
|
|
|
InfixOperator.Remainder -> code.emit(Opcode.Remainder)
|
|
|
|
|
InfixOperator.Lesser -> code.emit(Opcode.CompareLesser)
|
|
|
|
|
InfixOperator.Greater -> code.emit(Opcode.CompareGreater)
|
|
|
|
|
InfixOperator.GreaterEqual -> code.emit(Opcode.CompareGreaterEqual)
|
|
|
|
|
InfixOperator.LesserEqual -> code.emit(Opcode.CompareLesserEqual)
|
|
|
|
|
InfixOperator.BooleanAnd -> code.emit(Opcode.And)
|
|
|
|
|
InfixOperator.BooleanOr -> code.emit(Opcode.Or)
|
|
|
|
|
InfixOperator.BinaryAnd -> code.emit(Opcode.BinaryAnd)
|
|
|
|
|
InfixOperator.BinaryOr -> code.emit(Opcode.BinaryOr)
|
|
|
|
|
InfixOperator.BinaryExclusiveOr -> code.emit(Opcode.BinaryXor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitIntegerLiteral(node: IntegerLiteral) {
|
|
|
|
|
emit(Opcode.Integer, listOf(node.value.toUInt()))
|
|
|
|
|
code.emit(Opcode.Integer, listOf(node.value.toUInt()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitLetAssignment(node: LetAssignment) {
|
|
|
|
|
val variable = allocateLocalVariable(node.symbol)
|
|
|
|
|
val variable = code.localState.createLocal(node.symbol)
|
|
|
|
|
node.value.visit(this)
|
|
|
|
|
emit(Opcode.StoreLocal, listOf(variable.index))
|
|
|
|
|
code.emit(Opcode.StoreLocal, listOf(variable.index))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitListLiteral(node: ListLiteral) {
|
|
|
|
@ -198,15 +154,15 @@ class StubOpEmitter(val compiler: Compiler, val symbol: CompilableSymbol) : Func
|
|
|
|
|
for (item in node.items) {
|
|
|
|
|
item.visit(this)
|
|
|
|
|
}
|
|
|
|
|
emit(Opcode.List, listOf(count.toUInt()))
|
|
|
|
|
code.emit(Opcode.List, listOf(count.toUInt()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitLongLiteral(node: LongLiteral) {
|
|
|
|
|
emit(Opcode.Integer, listOf(node.value.toUInt()))
|
|
|
|
|
code.emit(Opcode.Integer, listOf(node.value.toUInt()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitNoneLiteral(node: NoneLiteral) {
|
|
|
|
|
emit(Opcode.None)
|
|
|
|
|
code.emit(Opcode.None)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitParentheses(node: Parentheses) {
|
|
|
|
@ -216,121 +172,90 @@ class StubOpEmitter(val compiler: Compiler, val symbol: CompilableSymbol) : Func
|
|
|
|
|
override fun visitPrefixOperation(node: PrefixOperation) {
|
|
|
|
|
node.expression.visit(this)
|
|
|
|
|
when (node.op) {
|
|
|
|
|
PrefixOperator.BooleanNot -> emit(Opcode.Not)
|
|
|
|
|
PrefixOperator.UnaryPlus -> emit(Opcode.UnaryPlus)
|
|
|
|
|
PrefixOperator.UnaryMinus -> emit(Opcode.UnaryMinus)
|
|
|
|
|
PrefixOperator.BinaryNot -> emit(Opcode.BinaryNot)
|
|
|
|
|
PrefixOperator.BooleanNot -> code.emit(Opcode.Not)
|
|
|
|
|
PrefixOperator.UnaryPlus -> code.emit(Opcode.UnaryPlus)
|
|
|
|
|
PrefixOperator.UnaryMinus -> code.emit(Opcode.UnaryMinus)
|
|
|
|
|
PrefixOperator.BinaryNot -> code.emit(Opcode.BinaryNot)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitSetAssignment(node: SetAssignment) {
|
|
|
|
|
val stubVarOrCall = resolveSymbol(node.symbol)
|
|
|
|
|
val stubVarOrCall = code.localState.resolve(node.symbol)
|
|
|
|
|
if (stubVarOrCall.stubVar == null) {
|
|
|
|
|
throw RuntimeException("Invalid set assignment.")
|
|
|
|
|
}
|
|
|
|
|
node.value.visit(this)
|
|
|
|
|
emit(Opcode.StoreLocal, listOf(stubVarOrCall.stubVar.index))
|
|
|
|
|
code.emit(Opcode.StoreLocal, listOf(stubVarOrCall.stubVar.index))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitStringLiteral(node: StringLiteral) {
|
|
|
|
|
val bytes = node.text.toByteArray()
|
|
|
|
|
val constant = compiler.constantPool.assign(bytes)
|
|
|
|
|
emit(Opcode.Constant, listOf(constant))
|
|
|
|
|
code.emit(Opcode.Constant, listOf(constant))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitSuffixOperation(node: SuffixOperation) {
|
|
|
|
|
val stubVarOrCall = resolveSymbol(node.reference.symbol)
|
|
|
|
|
val stubVarOrCall = code.localState.resolve(node.reference.symbol)
|
|
|
|
|
if (stubVarOrCall.stubVar == null) {
|
|
|
|
|
throw RuntimeException("Invalid suffix operation.")
|
|
|
|
|
}
|
|
|
|
|
load(stubVarOrCall)
|
|
|
|
|
when (node.op) {
|
|
|
|
|
SuffixOperator.Increment -> {
|
|
|
|
|
emit(Opcode.Integer, listOf(1u))
|
|
|
|
|
emit(Opcode.Add, emptyList())
|
|
|
|
|
emit(Opcode.StoreLocal, listOf(stubVarOrCall.stubVar.index))
|
|
|
|
|
code.emit(Opcode.Integer, listOf(1u))
|
|
|
|
|
code.emit(Opcode.Add, emptyList())
|
|
|
|
|
code.emit(Opcode.StoreLocal, listOf(stubVarOrCall.stubVar.index))
|
|
|
|
|
}
|
|
|
|
|
SuffixOperator.Decrement -> {
|
|
|
|
|
emit(Opcode.Integer, listOf(1u))
|
|
|
|
|
emit(Opcode.Subtract, emptyList())
|
|
|
|
|
emit(Opcode.StoreLocal, listOf(stubVarOrCall.stubVar.index))
|
|
|
|
|
code.emit(Opcode.Integer, listOf(1u))
|
|
|
|
|
code.emit(Opcode.Subtract, emptyList())
|
|
|
|
|
code.emit(Opcode.StoreLocal, listOf(stubVarOrCall.stubVar.index))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitSymbolReference(node: SymbolReference) {
|
|
|
|
|
val variable = resolveSymbol(node.symbol)
|
|
|
|
|
val variable = code.localState.resolve(node.symbol)
|
|
|
|
|
load(variable)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitVarAssignment(node: VarAssignment) {
|
|
|
|
|
val variable = allocateLocalVariable(node.symbol)
|
|
|
|
|
val variable = code.localState.createLocal(node.symbol)
|
|
|
|
|
node.value.visit(this)
|
|
|
|
|
emit(Opcode.StoreLocal, listOf(variable.index))
|
|
|
|
|
code.emit(Opcode.StoreLocal, listOf(variable.index))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitWhile(node: While) {
|
|
|
|
|
val startOfBody = MutableRel(0u)
|
|
|
|
|
val startOfLoop = MutableRel(0u)
|
|
|
|
|
val endOfLoop = MutableRel(0u)
|
|
|
|
|
val currentLoopState = LoopState(
|
|
|
|
|
startOfLoop = ops.size.toUInt(),
|
|
|
|
|
exitJumpTarget = endOfLoop,
|
|
|
|
|
body = startOfBody,
|
|
|
|
|
scopeDepth = (loopState?.scopeDepth ?: 0) + 1,
|
|
|
|
|
enclosing = loopState
|
|
|
|
|
)
|
|
|
|
|
loopState = currentLoopState
|
|
|
|
|
code.localState.startLoop(code.nextOpInst(), endOfLoop)
|
|
|
|
|
startOfLoop.rel = code.nextOpInst()
|
|
|
|
|
node.condition.visit(this)
|
|
|
|
|
patch(Opcode.JumpIf, listOf(0u), 0, symbol, startOfBody)
|
|
|
|
|
patch(Opcode.Jump, listOf(0u), 0, symbol, endOfLoop)
|
|
|
|
|
startOfBody.rel = ops.size.toUInt()
|
|
|
|
|
code.patch(Opcode.JumpIf, listOf(0u), 0, symbol, startOfBody)
|
|
|
|
|
code.patch(Opcode.Jump, listOf(0u), 0, symbol, endOfLoop)
|
|
|
|
|
startOfBody.rel = code.nextOpInst()
|
|
|
|
|
node.block.visit(this)
|
|
|
|
|
patch(Opcode.Jump, listOf(0u), 0, symbol, currentLoopState.startOfLoop)
|
|
|
|
|
endOfLoop.rel = ops.size.toUInt()
|
|
|
|
|
code.patch(Opcode.Jump, listOf(0u), 0, symbol, startOfLoop)
|
|
|
|
|
endOfLoop.rel = code.nextOpInst()
|
|
|
|
|
code.localState.endLoop()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun visitNativeFunctionDescriptor(node: NativeFunctionDescriptor) {
|
|
|
|
|
for (def in node.definitions) {
|
|
|
|
|
val defConstant = compiler.constantPool.assign(def.text.toByteArray())
|
|
|
|
|
emit(Opcode.Constant, listOf(defConstant))
|
|
|
|
|
code.emit(Opcode.Constant, listOf(defConstant))
|
|
|
|
|
}
|
|
|
|
|
val formConstant = compiler.constantPool.assign(node.form.id.toByteArray())
|
|
|
|
|
emit(Opcode.Native, listOf(formConstant, node.definitions.size.toUInt()))
|
|
|
|
|
code.emit(Opcode.Native, listOf(formConstant, node.definitions.size.toUInt()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun emit(code: Opcode) {
|
|
|
|
|
emit(code, emptyList())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun emit(code: Opcode, arguments: List<UInt>) {
|
|
|
|
|
ops.add(StaticOp(Op(code, arguments)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun patch(code: Opcode, arguments: List<UInt>, index: Int, symbol: CompilableSymbol, rel: MutableRel) {
|
|
|
|
|
ops.add(PatchRelOp(Op(code, arguments), index, symbol, rel))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun patch(code: Opcode, arguments: List<UInt>, index: Int, symbol: CompilableSymbol, rel: UInt) {
|
|
|
|
|
ops.add(PatchRelOp(Op(code, arguments), index, symbol, MutableRel(rel)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun patch(code: Opcode, arguments: List<UInt>, patches: Map<Int, CompilableSymbol>) {
|
|
|
|
|
ops.add(PatchSymOp(Op(code, arguments), patches))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun ops(): List<StubOp> = ops
|
|
|
|
|
|
|
|
|
|
private fun load(callOrStubVar: CallOrStubVar) {
|
|
|
|
|
private fun load(callOrStubVar: Loadable) {
|
|
|
|
|
if (callOrStubVar.stubVar != null) {
|
|
|
|
|
emit(Opcode.LoadLocal, listOf(callOrStubVar.stubVar.index))
|
|
|
|
|
code.emit(Opcode.LoadLocal, listOf(callOrStubVar.stubVar.index))
|
|
|
|
|
} else {
|
|
|
|
|
emit(Opcode.Integer, listOf(ops.size.toUInt() + 2u))
|
|
|
|
|
patch(Opcode.Call, listOf(0u), mapOf(0 to callOrStubVar.call!!))
|
|
|
|
|
code.emit(Opcode.Integer, listOf(code.nextOpInst() + 2u))
|
|
|
|
|
code.patch(Opcode.Call, listOf(0u), mapOf(0 to callOrStubVar.call!!))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class CallOrStubVar(
|
|
|
|
|
val call: CompilableSymbol? = null,
|
|
|
|
|
val stubVar: StubVar? = null
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|