From 203ec5fa46f883885b7e66526f18770aa1717785 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 14 Oct 2023 07:57:36 +0200 Subject: [PATCH] implement taking address of array var with variable index --- .../cpu6502/assignment/AssignmentAsmGen.kt | 25 ++++++++++++++-- docs/source/todo.rst | 3 +- examples/test.p8 | 29 ++++++++++++++----- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt index f2ed04c63..f3866622f 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt @@ -2210,7 +2210,26 @@ internal class AssignmentAsmGen(private val program: PtProgram, program.memsizer.memorySize(arrayDt!!, constIndex) // add arrayIndexExpr * elementsize to the address of the array variable. } else { val eltSize = program.memsizer.memorySize(ArrayToElementTypes.getValue(arrayDt!!)) - TODO("address-of array element $sourceName size $eltSize with non-const index at ${target.position}") + assignExpressionToVariable(arrayIndexExpr, "P8ZP_SCRATCH_W1", DataType.UWORD) + when(eltSize) { + 1 -> {} + 2 -> { + if(arrayDt !in SplitWordArrayTypes) + asmgen.out(" asl P8ZP_SCRATCH_W1 | rol P8ZP_SCRATCH_W1+1") + } + else -> TODO("address-of array element $sourceName size $eltSize with non-const index at ${target.position}") + } + asmgen.out(""" + lda #<$sourceName + clc + adc P8ZP_SCRATCH_W1 + tax + lda #>$sourceName + adc P8ZP_SCRATCH_W1+1 + tay + txa""") + assignRegisterpairWord(target, RegisterOrPair.AY) + return } } else 0 @@ -2286,9 +2305,9 @@ internal class AssignmentAsmGen(private val program: PtProgram, if(sourceDt==DataType.UBYTE) { asmgen.out(" lda $sourceName | sta ${target.asmVarname}") if(asmgen.isTargetCpu(CpuType.CPU65c02)) - asmgen.out(" stz ${target.asmVarname}") + asmgen.out(" stz ${target.asmVarname}+1") else - asmgen.out(" lda #0 | sta ${target.asmVarname}") + asmgen.out(" lda #0 | sta ${target.asmVarname}+1") } else asmgen.out(""" diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 31ed498a3..dcd61635a 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,9 +1,8 @@ TODO ==== -- fix TODO "address-of array element" in assignAddressOf() - - gfx2: use vera auto in/decrement in the flood fill routine +- add an option to inject an actual instruction (STP on 65c02, BRK on 6502) for breakpoints, instead of only listing them in the vice symbol file. - [on branch: shortcircuit] investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 .... - [on branch: ir-less-branch-opcodes] IR: reduce the number of branch instructions such as BEQ, BEQR, etc (gradually), replace with CMP(I) + status branch instruction diff --git a/examples/test.p8 b/examples/test.p8 index 8d3b96799..de569c8e9 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -3,6 +3,7 @@ main { sub start() { + ubyte[] b_array = [11,22,33,44] uword[] w_array = [1111,2222,3333,4444] uword[] @split split_array = [1111,2222,3333,4444] ubyte index = 2 @@ -13,21 +14,35 @@ main { uword pstep = w_array[index] uword psteps = split_array[index] - ;; uword @zp ptr = &w_array[index] txt.print_uw(pstep) txt.nl() txt.print_uw(psteps) txt.nl() -; txt.print_uw(peekw(ptr)) -; txt.nl() - w_array[index] += 10 - split_array[index] += 10 - txt.print_uw(w_array[index]) + uword @zp ptr = &w_array[index] + txt.print_uw(peekw(ptr)) txt.nl() - txt.print_uw(split_array[index]) + txt.print_uwhex(&w_array, true) + txt.spc() + txt.print_uwhex(ptr, true) txt.nl() + ptr = &split_array + txt.print_uw(peekw(ptr)) + txt.nl() + txt.print_uwhex(&w_array, true) + txt.spc() + txt.print_uwhex(ptr, true) + txt.nl() + + ptr = &b_array[index] + txt.print_ub(peek(ptr)) + txt.nl() + txt.print_uwhex(&b_array, true) + txt.spc() + txt.print_uwhex(ptr, true) + txt.nl() + } }