From e3c00669c1782b94df4773ae8622fecc233bfaee Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 31 Oct 2021 02:39:45 +0200 Subject: [PATCH] fixed improved asm generation for conditions that compare signed word to zero --- .../cpu6502/codegen/ExpressionsAsmGen.kt | 37 +++++++--- examples/test.p8 | 68 +++---------------- 2 files changed, 36 insertions(+), 69 deletions(-) diff --git a/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt b/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt index 10f746edf..04309a8d8 100644 --- a/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt +++ b/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt @@ -93,15 +93,10 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge else -> throw AssemblyError("invalid comparison operator $operator") } } - DataType.BYTE, DataType.WORD -> { - if(dt==DataType.BYTE) { - 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") // TODO PROBABLY NOT OKAY FOR WORDS - } + DataType.BYTE -> { + asmgen.assignExpressionToRegister(left, RegisterOrPair.A) + if (left is FunctionCall && !left.isSimple) + asmgen.out(" cmp #0") when (operator) { "==" -> asmgen.out(" bne $jumpIfFalseLabel") "!=" -> asmgen.out(" beq $jumpIfFalseLabel") @@ -115,6 +110,30 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge else -> throw AssemblyError("invalid comparison operator $operator") } } + DataType.WORD -> { + asmgen.assignExpressionToRegister(left, RegisterOrPair.AY) + when (operator) { + "==" -> asmgen.out(" bne $jumpIfFalseLabel | cpy #0 | bne $jumpIfFalseLabel") + "!=" -> asmgen.out(" sty P8ZP_SCRATCH_B1 | ora P8ZP_SCRATCH_B1 | beq $jumpIfFalseLabel") + ">" -> asmgen.out(""" + cpy #0 + bmi $jumpIfFalseLabel + bne + + cmp #0 + beq $jumpIfFalseLabel + + """) + "<" -> asmgen.out(" cpy #0 | bpl $jumpIfFalseLabel") + ">=" -> asmgen.out(" cpy #0 | bmi $jumpIfFalseLabel") + "<=" -> asmgen.out(""" + cpy #0 + bmi + + bne $jumpIfFalseLabel + cmp #0 + bne $jumpIfFalseLabel + + """) + else -> throw AssemblyError("invalid comparison operator $operator") + } + } DataType.FLOAT -> { asmgen.assignExpressionToRegister(left, RegisterOrPair.FAC1) asmgen.out(" jsr floats.SIGN") // SIGN(fac1) to A, $ff, $0, $1 for negative, zero, positive diff --git a/examples/test.p8 b/examples/test.p8 index 3cc40bfb6..203e1b84f 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -4,76 +4,24 @@ main { sub start() { - byte xx + word xx - xx=0 + xx=-$7f00 - if xx>=0 { - txt.print("xx>=0\n") - } else { - txt.print("error1\n") - } - if xx<=0 { - txt.print("xx<=0\n") - } else { - txt.print("error1qq\n") - } - if xx>0 { - txt.print("xx>0 error\n") - } else { - txt.print("ok1\n") - } - if xx<0 { - txt.print("xx<0 error\n") - } else { - txt.print("ok1\n") - } + txt.print_w(xx) txt.nl() - - xx=22 if xx>=0 { - txt.print("xx>=0\n") + txt.print(">=0\n") } else { - txt.print("error2\n") + txt.print("<0\n") } if xx<=0 { - txt.print("xx<=0 error\n") + txt.print("<=0\n") } else { - txt.print("ok2\n") + txt.print(">0\n") } - if xx>0 { - txt.print("xx>0\n") - } else { - txt.print("error2\n") - } - if xx<0 { - txt.print("xx<0 error\n") - } else { - txt.print("ok2\n") - } - txt.nl() - 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") - } + return