From 88d5c68b329f6c441c39b5e73896fcc4e391d837 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 20 Aug 2020 21:16:48 +0200 Subject: [PATCH] don't inc/dec a memory mapped register --- .../src/prog8/optimizer/StatementOptimizer.kt | 8 +- examples/test.p8 | 79 ++++++++++--------- 2 files changed, 46 insertions(+), 41 deletions(-) diff --git a/compiler/src/prog8/optimizer/StatementOptimizer.kt b/compiler/src/prog8/optimizer/StatementOptimizer.kt index 7f6918f86..be6794d97 100644 --- a/compiler/src/prog8/optimizer/StatementOptimizer.kt +++ b/compiler/src/prog8/optimizer/StatementOptimizer.kt @@ -405,8 +405,8 @@ internal class StatementOptimizer(private val program: Program, if (cv == 0.0) { return listOf(IAstModification.Remove(assignment, parent)) } else if (targetDt in IntegerDatatypes && floor(cv) == cv) { - if ((vardeclDt == VarDeclType.MEMORY && cv in 1.0..3.0) || (vardeclDt != VarDeclType.MEMORY && cv in 1.0..8.0)) { - // replace by several INCs (a bit less when dealing with memory targets) + if (vardeclDt != VarDeclType.MEMORY && cv in 1.0..4.0) { + // replace by several INCs if it's not a memory address (inc on a memory mapped register doesn't work very well) val incs = AnonymousScope(mutableListOf(), assignment.position) repeat(cv.toInt()) { incs.statements.add(PostIncrDecr(assignment.target, "++", assignment.position)) @@ -419,8 +419,8 @@ internal class StatementOptimizer(private val program: Program, if (cv == 0.0) { return listOf(IAstModification.Remove(assignment, parent)) } else if (targetDt in IntegerDatatypes && floor(cv) == cv) { - if ((vardeclDt == VarDeclType.MEMORY && cv in 1.0..3.0) || (vardeclDt != VarDeclType.MEMORY && cv in 1.0..8.0)) { - // replace by several DECs (a bit less when dealing with memory targets) + if (vardeclDt != VarDeclType.MEMORY && cv in 1.0..4.0) { + // replace by several DECs if it's not a memory address (dec on a memory mapped register doesn't work very well) val decs = AnonymousScope(mutableListOf(), assignment.position) repeat(cv.toInt()) { decs.statements.add(PostIncrDecr(assignment.target, "--", assignment.position)) diff --git a/examples/test.p8 b/examples/test.p8 index 3b1924e06..9e8bb7570 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -7,44 +7,49 @@ main { sub start() { -; byte A = 99 -; ubyte U = $18 -; word B = 9999 -; uword W = $18f0 -; -; c64scr.print_b(A) -; c64.CHROUT('\n') -; A = -A -; c64scr.print_b(A) -; c64.CHROUT('\n') -; -; U = ~U -; c64scr.print_ubhex(U, true) -; c64.CHROUT('\n') -; U = not U -; c64scr.print_ubhex(U, true) -; c64.CHROUT('\n') -; U = not U -; c64scr.print_ubhex(U, true) -; c64.CHROUT('\n') -; -; c64scr.print_w(B) -; c64.CHROUT('\n') -; B = -B -; c64scr.print_w(B) -; c64.CHROUT('\n') -; -; W = ~W -; c64scr.print_uwhex(W, true) -; c64.CHROUT('\n') -; W = not W -; c64scr.print_uwhex(W, true) -; c64.CHROUT('\n') -; W = not W -; c64scr.print_uwhex(W, true) -; c64.CHROUT('\n') + byte A = 99 + ubyte U = $18 + word B = 9999 + uword W = $18f0 - uword W = 43210 + c64scr.print_b(A) + c64.CHROUT('\n') + A = -A + c64scr.print_b(A) + c64.CHROUT('\n') + + U = ~U + c64scr.print_ubhex(U, true) + c64.CHROUT('\n') + U = not U + c64scr.print_ubhex(U, true) + c64.CHROUT('\n') + U = not U + c64scr.print_ubhex(U, true) + c64.CHROUT('\n') + + c64scr.print_w(B) + c64.CHROUT('\n') + B = -B + c64scr.print_w(B) + c64.CHROUT('\n') + + W = ~W + c64scr.print_uwhex(W, true) + c64.CHROUT('\n') + W = not W + c64scr.print_uwhex(W, true) + c64.CHROUT('\n') + W = not W + c64scr.print_uwhex(W, true) + c64.CHROUT('\n') + + ;@($d020) += @($d020) ; TODO fix compiler hang + + W += 3 + @($d020) += 3 + + W = 43210 W = W as ubyte c64scr.print_uw(W) c64.CHROUT('\n')