From 10145b946b36ccdaa6a8971c4839240146e451d4 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 13 Oct 2020 16:27:40 +0200 Subject: [PATCH] invalid repeat loop code is generated... --- .../compiler/target/c64/codegen/AsmGen.kt | 3 ++ examples/test.p8 | 46 ++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt index 93d4db5f0..0dc5a18c9 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt @@ -938,6 +938,7 @@ internal class AsmGen(private val program: Program, when { iterations == 0 -> {} iterations <= 256 -> { + // TODO faulty loop code when 256 because of the beq that's also generated (should be ok if that is removed?) out(" lda #${iterations and 255}") repeatByteCountInA(iterations, repeatLabel, endLabel, stmt.body) } @@ -985,6 +986,7 @@ internal class AsmGen(private val program: Program, private fun repeatWordCountInAY(constIterations: Int?, repeatLabel: String, endLabel: String, body: AnonymousScope) { // note: A/Y must have been loaded with the number of iterations already! val counterVar = makeLabel("repeatcounter") + // TODO the 0.w check at the start is invalid(wrong register check order), and must not be done at all if the loop count is numeric literal >0 out(""" bne + cpy #0 @@ -1018,6 +1020,7 @@ $counterVar .word 0""") private fun repeatByteCountInA(constIterations: Int?, repeatLabel: String, endLabel: String, body: AnonymousScope) { // note: A must have been loaded with the number of iterations already! val counterVar = makeLabel("repeatcounter") + // TODO the beq endlabel check can be omitted if the number of iterations is a numeric literal >0 out(""" beq $endLabel sta $counterVar diff --git a/examples/test.p8 b/examples/test.p8 index 9880b7d89..b26920b88 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -7,8 +7,50 @@ main { sub start() { - derp() - rec2() + ubyte xx = 0 + repeat 254 { + txt.chrout('1') + xx++ + } + txt.print_ub(xx) + txt.chrout('\n') + + xx=0 + + repeat 255 { + txt.chrout('2') + xx++ + } + txt.print_ub(xx) + txt.chrout('\n') + + xx=0 + repeat 256 { ; TODO generates faulty code, loop is never executed at all + txt.chrout('3') + xx++ + } + txt.print_ub(xx) + txt.chrout('\n') + + xx=0 + repeat 257 { ; TODO generates invalid 0-check code at start + txt.chrout('4') + xx++ + } + txt.print_ub(xx) + txt.chrout('\n') + + ubyte bb + + repeat bb { + xx++ + } + + uword ww + + repeat ww { + xx++ + } } sub rec2() {