From 78af2cd4dcf38d110bb071f549556148305fa255 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 13 Feb 2021 23:50:03 +0100 Subject: [PATCH] optimize peekw() --- .../c64/codegen/BuiltinFunctionsAsmGen.kt | 56 ++++++++++++++++++- examples/test.p8 | 15 ++--- 2 files changed, 62 insertions(+), 9 deletions(-) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt index 58139e92b..516aec86e 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt @@ -9,6 +9,7 @@ import prog8.ast.statements.* import prog8.ast.toHex import prog8.compiler.AssemblyError import prog8.compiler.functions.FSignature +import prog8.compiler.target.CpuType import prog8.compiler.target.c64.codegen.assignment.* import prog8.compiler.target.subroutineFloatEvalResultVar2 @@ -969,8 +970,59 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val } private fun funcPeekW(fcall: IFunctionCall, resultToStack: Boolean, resultRegister: RegisterOrPair?) { - asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.AY) - asmgen.out(" jsr prog8_lib.func_peekw") + when(val addrExpr = fcall.args[0]) { + is NumericLiteralValue -> { + val addr = addrExpr.number.toHex() + asmgen.out(" lda $addr | ldy ${addr}+1") + } + is IdentifierReference -> { + val varname = asmgen.asmVariableName(addrExpr) + if(asmgen.isZpVar(addrExpr)) { + // pointervar is already in the zero page, no need to copy + if (asmgen.compTarget.machine.cpu == CpuType.CPU65c02) { + asmgen.out(""" + ldy #1 + lda ($varname),y + tay + lda ($varname)""") + } else { + asmgen.out(""" + ldy #0 + lda ($varname),y + pha + iny + lda ($varname),y + tay + pla""") + } + } else { + asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.AY) + asmgen.out(" jsr prog8_lib.func_peekw") + } + } + is BinaryExpression -> { + if(addrExpr.operator=="+" && addrExpr.left is IdentifierReference && addrExpr.right is NumericLiteralValue) { + val varname = asmgen.asmVariableName(addrExpr.left as IdentifierReference) + val index = (addrExpr.right as NumericLiteralValue).number.toHex() + asmgen.out(""" + ldy #$index + lda ($varname),y + pha + iny + lda ($varname),y + tay + pla""") + } else { + asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.AY) + asmgen.out(" jsr prog8_lib.func_peekw") + } + } + else -> { + asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.AY) + asmgen.out(" jsr prog8_lib.func_peekw") + } + } + if(resultToStack){ asmgen.out(" sta P8ESTACK_LO,x | tya | sta P8ESTACK_HI,x | dex") } else { diff --git a/examples/test.p8 b/examples/test.p8 index 69942577e..522f05b82 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -6,14 +6,10 @@ main { sub start() { uword ptr = $4000 + uword ww - poke(ptr, $34) - poke(ptr+1, $ea) - txt.print_ubhex(peek(ptr), 1) - txt.print_ubhex(peek(ptr+1), 1) - txt.nl() - - uword ww = peekw(ptr) + pokew($4000, $98cf) + ww = peekw($4000) txt.print_uwhex(ww,1) txt.nl() @@ -21,5 +17,10 @@ main { ww = peekw(ptr) txt.print_uwhex(ww,1) txt.nl() + + pokew(ptr+2, $1234) + ww = peekw(ptr+2) + txt.print_uwhex(ww,1) + txt.nl() } }