diff --git a/compiler/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt b/compiler/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt index 11ecbce89..a6e81e457 100644 --- a/compiler/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt @@ -501,6 +501,7 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge } private fun translateWordLessJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) { + // TODO 100% checked ok. fun code(msbCpyOperand: String, lsbCmpOperand: String) { asmgen.out(""" @@ -666,15 +667,15 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge } private fun translateWordGreaterJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) { - - fun code(msbCpyOperand: String, lsbCmpOperand: String) { + // TODO 100% checked ok. + fun code(leftName: String) { asmgen.out(""" - cmp $lsbCmpOperand - tya - sbc $msbCpyOperand - bvc + - eor #${'$'}80 -+ bmi $jumpIfFalseLabel""") + cmp $leftName + tya + sbc $leftName+1 + bvc + + eor #$80 ++ bpl $jumpIfFalseLabel""") } if(rightConstVal!=null) { @@ -685,30 +686,24 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge } else { if (left is IdentifierReference) { return if(rightConstVal.number.toInt()!=0) { - // TODO is this correct? word > not-0 - asmgen.assignExpressionToRegister(left, RegisterOrPair.AY) - code("#>${rightConstVal.number}", "#<${rightConstVal.number}") + asmgen.assignExpressionToRegister(right, RegisterOrPair.AY) + code(asmgen.asmVariableName(left)) } else { - // TODO is this correct? word > 0 can be shorter as well? val name = asmgen.asmVariableName(left) asmgen.out(""" - lda #0 - cmp $name - sbc $name+1 - bvc + - eor #$80 -+ bpl $jumpIfFalseLabel""") + lda $name+1 + bmi $jumpIfFalseLabel + lda $name + beq $jumpIfFalseLabel""") } } } } - if(right is IdentifierReference) { - // TODO is this correct? word > identifer - asmgen.assignExpressionToRegister(left, RegisterOrPair.AY) - val varname = asmgen.asmVariableName(right) - return code("$varname+1", varname) + if(left is IdentifierReference) { + asmgen.assignExpressionToRegister(right, RegisterOrPair.AY) + return code(asmgen.asmVariableName(left)) } asmgen.assignExpressionToVariable(left, "P8ZP_SCRATCH_W2", DataType.UWORD, null) diff --git a/examples/test.p8 b/examples/test.p8 index c06a780d9..6c1a8a80f 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,140 +1,177 @@ -; %import graphics +%import textio %zeropage basicsafe main { - sub start2() { - - word xx - word yy - - ; all comparisons with constant values are already optimized. - ; but for variables (and memreads) we can optimize further (don't use temporary ZP location) - - word value = 100 - - while xx==value { - yy++ - } - - while xx!=value { - yy++ - } - - do { - yy++ - } until xx==value - - do { - yy++ - } until xx!=value - - if xx==value - yy++ - - if xx!=value - yy++ - -; while xx>value { -; yy++ -; } -; do { -; yy++ -; } until xx>value -; -; if xx>value -; yy++ - } sub start() { - uword uw1 - uword uw2 + txt.print("\n"*25) - if uw19 + txt.print("1ok\n") + else + txt.print("1fault\n") - ; all comparisons with constant values are already optimized. - ; but for variables (and memreads) we can optimize further (don't use temporary ZP location) + if xx>10 + txt.print("2fault\n") + else + txt.print("2ok\n") - byte value = 100 + if xx>11 + txt.print("3fault\n") + else + txt.print("3ok\n") - while xx==value { - yy++ - } + if xx>2222 + txt.print("4fault\n") + else + txt.print("4ok\n") - while xx==@($2000) { - yy++ - } + if xx>-9 + txt.print("5ok\n") + else + txt.print("5fault\n") - while xx!=value { - yy++ - } + if xx>-9999 + txt.print("6ok\n") + else + txt.print("6fault\n") - while xx!=@($2000) { - yy++ - } + if xx>0 + txt.print("7ok\n") + else + txt.print("7fault\n") - do { - yy++ - } until xx==value + xx=0 + if xx>0 + txt.print("8false\n") + else + txt.print("8ok\n") - do { - yy++ - } until xx!=value + xx=-9999 + if xx>0 + txt.print("9false\n") + else + txt.print("9ok\n") - if xx==value - yy++ + txt.nl() - if xx!=value - yy++ + xx=10 + compare=9 + if xx>compare + txt.print("1ok\n") + else + txt.print("1fault\n") - while xx>value { - yy++ - } - while xxvalue - do { - yy++ - } until xxcompare + txt.print("2fault\n") + else + txt.print("2ok\n") + + compare=11 + if xx>compare + txt.print("3fault\n") + else + txt.print("3ok\n") + + compare=2222 + if xx>compare + txt.print("4fault\n") + else + txt.print("4ok\n") + + compare=-9 + if xx>compare + txt.print("5ok\n") + else + txt.print("5fault\n") + + compare=-9999 + if xx>compare + txt.print("6ok\n") + else + txt.print("6fault\n") + + compare=0 + if xx>compare + txt.print("7ok\n") + else + txt.print("7fault\n") + + xx=0 + if xx>compare + txt.print("8false\n") + else + txt.print("8ok\n") + + xx=-9999 + if xx>compare + txt.print("9false\n") + else + txt.print("9ok\n") + + + + txt.nl() + + xx=9 + compare=9 + if xx+1>compare + txt.print("1ok\n") + else + txt.print("1fault\n") + + compare=10 + if xx+1>compare + txt.print("2fault\n") + else + txt.print("2ok\n") + + compare=11 + if xx+1>compare + txt.print("3fault\n") + else + txt.print("3ok\n") + + compare=2222 + if xx+1>compare + txt.print("4fault\n") + else + txt.print("4ok\n") + + compare=-9 + if xx+1>compare + txt.print("5ok\n") + else + txt.print("5fault\n") + + compare=-9999 + if xx+1>compare + txt.print("6ok\n") + else + txt.print("6fault\n") + + compare=0 + if xx+1>compare + txt.print("7ok\n") + else + txt.print("7fault\n") + + xx=1 + if xx-1>compare + txt.print("8false\n") + else + txt.print("8ok\n") + + xx=-9999 + if xx-1>compare + txt.print("9false\n") + else + txt.print("9ok\n") - if xx>value - yy++ - if xx