From bf23ad78e60d3f94d7d7ab61e59a784e5b482183 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 15 Mar 2021 22:24:09 +0100 Subject: [PATCH] improve byte '<' and '>' codegen --- .../cpu6502/codegen/ExpressionsAsmGen.kt | 70 +++++++++++++-- examples/test.p8 | 88 ++++++++++--------- 2 files changed, 110 insertions(+), 48 deletions(-) diff --git a/compiler/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt b/compiler/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt index 399f326ee..0aadc5d85 100644 --- a/compiler/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt @@ -410,23 +410,48 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge return } else { if (left is IdentifierReference) { - val name = asmgen.asmVariableName(left) + asmgen.assignExpressionToRegister(left, RegisterOrPair.A) if(rightConstVal.number.toInt()!=0) asmgen.out(""" - lda $name sec sbc #${rightConstVal.number} bvc + eor #$80 + bpl $jumpIfFalseLabel""") else - asmgen.out(" lda $name | bpl $jumpIfFalseLabel") + asmgen.out(" bpl $jumpIfFalseLabel") return } } } - // TODO optimize if right is variable or mem-read to avoid use of ZP location + if(right is IdentifierReference) { + asmgen.assignExpressionToRegister(left, RegisterOrPair.A) + asmgen.out(""" + sec + sbc ${asmgen.asmVariableName(right)} + bvc + + eor #$80 ++ bpl $jumpIfFalseLabel""") + return + } + var memread = right as? DirectMemoryRead + if(memread==null && right is TypecastExpression) + memread = right.expression as? DirectMemoryRead + if(memread!=null) { + val address = memread.addressExpression as? NumericLiteralValue + if(address!=null) { + asmgen.assignExpressionToRegister(left, RegisterOrPair.A) + asmgen.out(""" + sec + sbc ${address.number.toHex()} + bvc + + eor #$80 ++ bpl $jumpIfFalseLabel""") + return + } + } + asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE, null) asmgen.assignExpressionToRegister(left, RegisterOrPair.A) asmgen.out(""" @@ -547,10 +572,9 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge return } else { if (left is IdentifierReference) { - val name = asmgen.asmVariableName(left) + asmgen.assignExpressionToRegister(left, RegisterOrPair.A) if(rightConstVal.number.toInt()!=0) asmgen.out(""" - lda $name clc sbc #${rightConstVal.number} bvc + @@ -559,13 +583,43 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge bmi $jumpIfFalseLabel +""") else - asmgen.out(" lda $name | bmi $jumpIfFalseLabel | beq $jumpIfFalseLabel") + asmgen.out(" bmi $jumpIfFalseLabel | beq $jumpIfFalseLabel") return } } } - // TODO optimize if right is variable or mem-read to avoid use of ZP location + if(right is IdentifierReference) { + asmgen.assignExpressionToRegister(left, RegisterOrPair.A) + asmgen.out(""" + clc + sbc ${asmgen.asmVariableName(right)} + bvc + + eor #$80 ++ bpl + + bmi $jumpIfFalseLabel ++""") + return + } + var memread = right as? DirectMemoryRead + if(memread==null && right is TypecastExpression) + memread = right.expression as? DirectMemoryRead + if(memread!=null) { + val address = memread.addressExpression as? NumericLiteralValue + if(address!=null) { + asmgen.assignExpressionToRegister(left, RegisterOrPair.A) + asmgen.out(""" + clc + sbc ${address.number.toHex()} + bvc + + eor #$80 ++ bpl + + bmi $jumpIfFalseLabel ++""") + return + } + } + asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE, null) asmgen.assignExpressionToRegister(left, RegisterOrPair.A) asmgen.out(""" diff --git a/examples/test.p8 b/examples/test.p8 index 24adf5a26..7761006d1 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,8 +1,8 @@ -%import graphics +; %import graphics %zeropage basicsafe main { - sub start() { + sub start2() { word xx word yy @@ -45,7 +45,7 @@ main { ; yy++ } - sub start3() { + sub start() { byte xx byte yy @@ -85,42 +85,50 @@ main { if xx!=value yy++ -; while xx>value { -; yy++ -; } -; do { -; yy++ -; } until xx>value + while xx>value { + yy++ + } + while xxvalue + do { + yy++ + } until xxvalue + yy++ + if xxvalue -; yy++ - } - - - sub start2() { - - graphics.enable_bitmap_mode() - - uword xx - ubyte yy - - graphics.line(150,50,150,50) - - for yy in 0 to 199-60 step 16 { - - for xx in 0 to 319-50 step 16 { - graphics.line(30+xx, 10+yy, 50+xx, 30+yy) - graphics.line(49+xx, 30+yy, 10+xx, 30+yy) - graphics.line(11+xx, 29+yy, 29+xx, 11+yy) - - ; triangle 2, counter-clockwise - graphics.line(30+xx, 40+yy, 10+xx, 60+yy) - graphics.line(11+xx, 60+yy, 50+xx, 60+yy) - graphics.line(49+xx, 59+yy, 31+xx,41+yy) - } - } - - repeat { - } - } +; graphics.enable_bitmap_mode() +; +; uword xx +; ubyte yy +; +; graphics.line(150,50,150,50) +; +; for yy in 0 to 199-60 step 16 { +; +; for xx in 0 to 319-50 step 16 { +; graphics.line(30+xx, 10+yy, 50+xx, 30+yy) +; graphics.line(49+xx, 30+yy, 10+xx, 30+yy) +; graphics.line(11+xx, 29+yy, 29+xx, 11+yy) +; +; ; triangle 2, counter-clockwise +; graphics.line(30+xx, 40+yy, 10+xx, 60+yy) +; graphics.line(11+xx, 60+yy, 50+xx, 60+yy) +; graphics.line(49+xx, 59+yy, 31+xx,41+yy) +; } +; } +; +; repeat { +; } +; } }