diff --git a/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt b/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt index 642f89c62..5c40c4d59 100644 --- a/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt +++ b/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt @@ -59,7 +59,7 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge } if (rightConstVal!=null && rightConstVal.number.toDouble() == 0.0) - jumpIfZeroOrNot(left, operator, right, jumpIfFalseLabel, leftConstVal, rightConstVal) + jumpIfZeroOrNot(left, operator, jumpIfFalseLabel) else jumpIfComparison(left, operator, right, jumpIfFalseLabel, leftConstVal, rightConstVal) } @@ -67,71 +67,46 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge private fun jumpIfZeroOrNot( left: Expression, operator: String, - right: Expression, - jumpIfFalseLabel: String, - leftConstVal: NumericLiteralValue?, - rightConstVal: NumericLiteralValue? + jumpIfFalseLabel: String ) { - when(left.inferType(program).getOr(DataType.UNDEFINED)) { - DataType.UBYTE, DataType.BYTE -> { - asmgen.assignExpressionToRegister(left, RegisterOrPair.A) - if (left is FunctionCall && !left.isSimple) - asmgen.out(" cmp #0") + when(val dt = left.inferType(program).getOr(DataType.UNDEFINED)) { + DataType.UBYTE, DataType.UWORD -> { + if(operator=="<") { + asmgen.out(" jmp $jumpIfFalseLabel") + return + } else if(operator==">=") { + return + } + if(dt==DataType.UBYTE) { + asmgen.assignExpressionToRegister(left, RegisterOrPair.A) + if (left is FunctionCall && !left.isSimple) + asmgen.out(" cmp #0") + } else { + asmgen.assignExpressionToRegister(left, RegisterOrPair.AY) + asmgen.out(" sty P8ZP_SCRATCH_B1 | ora P8ZP_SCRATCH_B1") + } when (operator) { - "==" -> { - asmgen.out(" bne $jumpIfFalseLabel") - } - "!=" -> { - asmgen.out(" beq $jumpIfFalseLabel") - } - else -> { - // TODO optimize byte other operators - jumpIfComparison(left, operator, right, jumpIfFalseLabel, leftConstVal, rightConstVal) - } + "==" -> asmgen.out(" bne $jumpIfFalseLabel") + "!=" -> asmgen.out(" beq $jumpIfFalseLabel") + ">" -> asmgen.out(" beq $jumpIfFalseLabel") + "<=" -> asmgen.out(" bne $jumpIfFalseLabel") + else -> throw AssemblyError("invalid comparison operator $operator") } } - DataType.UWORD, DataType.WORD -> { - asmgen.assignExpressionToRegister(left, RegisterOrPair.AY) - asmgen.out(" sty P8ZP_SCRATCH_B1 | ora P8ZP_SCRATCH_B1") - when (operator) { - "==" -> { - asmgen.out(" bne $jumpIfFalseLabel") - } - "!=" -> { - asmgen.out(" beq $jumpIfFalseLabel") - } - else -> { - // TODO optimize word other operators - jumpIfComparison(left, operator, right, jumpIfFalseLabel, leftConstVal, rightConstVal) - } - } + DataType.BYTE, DataType.WORD -> { + TODO("signed BYTE/UWORD comparison $operator") } DataType.FLOAT -> { asmgen.assignExpressionToRegister(left, RegisterOrPair.FAC1) - asmgen.out(" jsr floats.SIGN") + asmgen.out(" jsr floats.SIGN") // SIGN(fac1) to A, $ff, $0, $1 for negative, zero, positive when (operator) { - "==" -> { - // SIGN(fac1) to A, $ff, $0, $1 for negative, zero, positive - asmgen.out(" bne $jumpIfFalseLabel") - } - "!=" -> { - asmgen.out(" beq $jumpIfFalseLabel") - } - ">" -> { - asmgen.out(" bmi $jumpIfFalseLabel | beq $jumpIfFalseLabel") - } - "<" -> { - asmgen.out(" bpl $jumpIfFalseLabel") - } - ">=" -> { - asmgen.out(" bmi $jumpIfFalseLabel") - } - "<=" -> { - asmgen.out(" cmp #1 | beq $jumpIfFalseLabel") - } - else -> { - throw AssemblyError("invalid comparison operator $operator") - } + "==" -> asmgen.out(" bne $jumpIfFalseLabel") + "!=" -> asmgen.out(" beq $jumpIfFalseLabel") + ">" -> asmgen.out(" bmi $jumpIfFalseLabel | beq $jumpIfFalseLabel") + "<" -> asmgen.out(" bpl $jumpIfFalseLabel") + ">=" -> asmgen.out(" bmi $jumpIfFalseLabel") + "<=" -> asmgen.out(" cmp #1 | beq $jumpIfFalseLabel") + else -> throw AssemblyError("invalid comparison operator $operator") } } else -> { diff --git a/examples/test.p8 b/examples/test.p8 index 05b93a13c..628a9dba1 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,4 +1,3 @@ -%import floats %import textio %zeropage basicsafe @@ -6,76 +5,75 @@ main { sub start() { ubyte xx - float ff - ff=0 + xx=0 - if ff>=0 { - txt.print("ff>=0\n") + if xx>=0 { + txt.print("xx>=0\n") } else { txt.print("error1\n") } - if ff<=0 { - txt.print("ff<=0\n") + if xx<=0 { + txt.print("xx<=0\n") } else { txt.print("error1\n") } - if ff>0 { - txt.print("ff>0 error\n") + if xx>0 { + txt.print("xx>0 error\n") } else { txt.print("ok1\n") } - if ff<0 { - txt.print("ff<0 error\n") + if xx<0 { + txt.print("xx<0 error\n") } else { txt.print("ok1\n") } txt.nl() - ff=0.22 - if ff>=0 { - txt.print("ff>=0\n") + xx=22 + if xx>=0 { + txt.print("xx>=0\n") } else { txt.print("error2\n") } - if ff<=0 { - txt.print("ff<=0 error\n") + if xx<=0 { + txt.print("xx<=0 error\n") } else { txt.print("ok2\n") } - if ff>0 { - txt.print("ff>0\n") + if xx>0 { + txt.print("xx>0\n") } else { txt.print("error2\n") } - if ff<0 { - txt.print("ff<0 error\n") + if xx<0 { + txt.print("xx<0 error\n") } else { txt.print("ok2\n") } txt.nl() - ff=-1.11 - if ff>=0 { - txt.print("ff>=0 error\n") - } else { - txt.print("ok3\n") - } - if ff<=0 { - txt.print("ff<=0\n") - } else { - txt.print("error3\n") - } - if ff>0 { - txt.print("ff>0 error\n") - } else { - txt.print("ok3\n") - } - if ff<0 { - txt.print("ff<0\n") - } else { - txt.print("error3\n") - } +; xx=-11 +; if xx>=0 { +; txt.print("xx>=0 error\n") +; } else { +; txt.print("ok3\n") +; } +; if xx<=0 { +; txt.print("xx<=0\n") +; } else { +; txt.print("error3\n") +; } +; if xx>0 { +; txt.print("xx>0 error\n") +; } else { +; txt.print("ok3\n") +; } +; if xx<0 { +; txt.print("xx<0\n") +; } else { +; txt.print("error3\n") +; }