From 4179b4e54335c859c4165bc27b6c8641b16e01c9 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 26 Sep 2020 17:24:17 +0200 Subject: [PATCH] all unsigned comparisons --- .../target/c64/codegen/ExpressionsAsmGen.kt | 86 +++++++++++++++++-- examples/cmp/comparisons.p8 | 40 +++++++++ examples/wizzine.p8 | 2 +- 3 files changed, 119 insertions(+), 9 deletions(-) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/ExpressionsAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/ExpressionsAsmGen.kt index 7343f5a86..ca841e389 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/ExpressionsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/ExpressionsAsmGen.kt @@ -325,7 +325,34 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge } private fun translateUwordLessOrEqual(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) { - // TODO compare with optimized asm + if(rightConstVal!=null) { + if(leftConstVal!=null) { + if(rightConstVal>leftConstVal) + asmgen.out(" jmp $jumpIfFalseLabel") + return + } else { + if (left is IdentifierReference) { + val name = asmgen.asmVariableName(left) + if(rightConstVal.number.toInt()!=0) + asmgen.out(""" + lda #>${rightConstVal.number} + cmp $name+1 + bcc $jumpIfFalseLabel + bne + + lda #<${rightConstVal.number} + cmp $name + bcc $jumpIfFalseLabel ++""") + else + asmgen.out(""" + lda $name + ora $name+1 + bne $jumpIfFalseLabel""") + return + } + } + } + asmgen.translateExpression(left) asmgen.translateExpression(right) asmgen.out(" jsr prog8_lib.lesseq_uw | inx | lda P8ESTACK_LO,x | beq $jumpIfFalseLabel") @@ -383,7 +410,7 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge private fun translateUwordGreater(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) { if(rightConstVal!=null) { if(leftConstVal!=null) { - if(rightConstVal>=leftConstVal) + if(rightConstVal<=leftConstVal) asmgen.out(" jmp $jumpIfFalseLabel") return } else { @@ -403,10 +430,8 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge else asmgen.out(""" lda $name - bne + - lda $name+1 - beq $jumpIfFalseLabel -+""") + ora $name+1 + beq $jumpIfFalseLabel""") return } } @@ -425,7 +450,30 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge } private fun translateUbyteGreaterOrEqual(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) { - // TODO compare with optimized asm + if(rightConstVal!=null) { + if(leftConstVal!=null) { + if(rightConstVal${rightConstVal.number} + bcc $jumpIfFalseLabel + bne + + lda $name + cmp #<${rightConstVal.number} + bcc $jumpIfFalseLabel ++""") + return + } + } + } + asmgen.translateExpression(left) asmgen.translateExpression(right) asmgen.out(" jsr prog8_lib.greatereq_uw | inx | lda P8ESTACK_LO,x | beq $jumpIfFalseLabel") diff --git a/examples/cmp/comparisons.p8 b/examples/cmp/comparisons.p8 index d149bbd47..db3c87b7c 100644 --- a/examples/cmp/comparisons.p8 +++ b/examples/cmp/comparisons.p8 @@ -370,6 +370,11 @@ main { } else { txt.print("fail < eb6\n") } + if b2 < 0 { + txt.print("ok < eb7\n") + } else { + txt.print("fail < eb7\n") + } if w2 < -32768 { txt.print("fail < ew1\n") @@ -401,6 +406,11 @@ main { } else { txt.print("fail < ew6\n") } + if w2 < 0 { + txt.print("ok < ew7\n") + } else { + txt.print("fail < ew7\n") + } } sub greater() { @@ -557,6 +567,11 @@ main { } else { txt.print("ok > eb6\n") } + if b2 > 0 { + txt.print("fail > eb7\n") + } else { + txt.print("ok > eb7\n") + } if w2 > -32768 { txt.print("ok > ew1\n") @@ -588,6 +603,11 @@ main { } else { txt.print("ok > ew6\n") } + if w2 > 0 { + txt.print("fail > ew6\n") + } else { + txt.print("ok > ew6\n") + } } sub lessequal() { @@ -745,6 +765,11 @@ main { } else { txt.print("fail <= eb6\n") } + if b2 <= 0 { + txt.print("ok <= eb7\n") + } else { + txt.print("fail <= eb7\n") + } if w2 <= -32768 { txt.print("fail <= ew1\n") @@ -776,6 +801,11 @@ main { } else { txt.print("fail <= ew6\n") } + if w2 <= 0 { + txt.print("ok <= ew7\n") + } else { + txt.print("fail <= ew7\n") + } } @@ -933,6 +963,11 @@ main { } else { txt.print("ok >= eb6\n") } + if b2 >= 0 { + txt.print("fail >= eb7\n") + } else { + txt.print("ok >= eb7\n") + } if w2 >= -32768 { txt.print("ok >= ew1\n") @@ -964,5 +999,10 @@ main { } else { txt.print("ok >= ew6\n") } + if w2 >= 0 { + txt.print("fail >= ew7\n") + } else { + txt.print("ok >= ew7\n") + } } } diff --git a/examples/wizzine.p8 b/examples/wizzine.p8 index c33b22a51..9ba38dc9d 100644 --- a/examples/wizzine.p8 +++ b/examples/wizzine.p8 @@ -58,7 +58,7 @@ irq { ubyte @zp y = cos8u(angle*3-spri*16) / 2 + 70 c64.SPXYW[spri] = mkword(y, lsb(x)) c64.MSIGX <<= 1 - if msb(x) c64.MSIGX++ + if msb(x) c64.MSIGX++ ; TODO has this become badly translated to asm?? it makes the loop quite slow } c64.EXTCOL-=8 }