From 5e6e711f33f88d873333c2e7c7fedb91ca60f3ab Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 14 Feb 2021 00:05:57 +0100 Subject: [PATCH] optimize pokew() --- .../c64/codegen/BuiltinFunctionsAsmGen.kt | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt index 516aec86e..0e062d184 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt @@ -964,6 +964,55 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val } private fun funcPokeW(fcall: IFunctionCall) { + when(val addrExpr = fcall.args[0]) { + is NumericLiteralValue -> { + asmgen.assignExpressionToRegister(fcall.args[1], RegisterOrPair.AY) + val addr = addrExpr.number.toHex() + asmgen.out(" sta $addr | sty ${addr}+1") + return + } + is IdentifierReference -> { + val varname = asmgen.asmVariableName(addrExpr) + if(asmgen.isZpVar(addrExpr)) { + // pointervar is already in the zero page, no need to copy + asmgen.saveRegisterLocal(CpuRegister.X, (fcall as Node).definingSubroutine()!!) + asmgen.assignExpressionToRegister(fcall.args[1], RegisterOrPair.AX) + if (asmgen.compTarget.machine.cpu == CpuType.CPU65c02) { + asmgen.out(""" + sta ($varname) + txa + ldy #1 + sta ($varname),y""") + } else { + asmgen.out(""" + ldy #0 + sta ($varname),y + txa + iny + sta ($varname),y""") + } + asmgen.restoreRegisterLocal(CpuRegister.X) + return + } + } + is BinaryExpression -> { + if(addrExpr.operator=="+" && addrExpr.left is IdentifierReference && addrExpr.right is NumericLiteralValue) { + asmgen.saveRegisterLocal(CpuRegister.X, (fcall as Node).definingSubroutine()!!) + asmgen.assignExpressionToRegister(fcall.args[1], RegisterOrPair.AX) + val varname = asmgen.asmVariableName(addrExpr.left as IdentifierReference) + val index = (addrExpr.right as NumericLiteralValue).number.toHex() + asmgen.out(""" + ldy #$index + sta ($varname),y + txa + iny + sta ($varname),y""") + asmgen.restoreRegisterLocal(CpuRegister.X) + return + } + } + } + asmgen.assignExpressionToVariable(fcall.args[0], "P8ZP_SCRATCH_W1", DataType.UWORD, null) asmgen.assignExpressionToRegister(fcall.args[1], RegisterOrPair.AY) asmgen.out(" jsr prog8_lib.func_pokew")