improved asm generation for conditions that compare byte/word to zero

This commit is contained in:
Irmen de Jong 2021-10-31 01:58:16 +02:00
parent 2388359a99
commit 33d17afc32
2 changed files with 43 additions and 24 deletions

View File

@ -94,7 +94,26 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
}
}
DataType.BYTE, DataType.WORD -> {
TODO("signed BYTE/UWORD comparison $operator")
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
}
when (operator) {
"==" -> asmgen.out(" bne $jumpIfFalseLabel")
"!=" -> asmgen.out(" beq $jumpIfFalseLabel")
">" -> asmgen.out(" beq $jumpIfFalseLabel | bmi $jumpIfFalseLabel")
"<" -> asmgen.out(" bpl $jumpIfFalseLabel")
">=" -> asmgen.out(" bmi $jumpIfFalseLabel")
"<=" -> asmgen.out("""
beq +
bpl $jumpIfFalseLabel
+ """)
else -> throw AssemblyError("invalid comparison operator $operator")
}
}
DataType.FLOAT -> {
asmgen.assignExpressionToRegister(left, RegisterOrPair.FAC1)

View File

@ -4,7 +4,7 @@
main {
sub start() {
ubyte xx
byte xx
xx=0
@ -16,7 +16,7 @@ main {
if xx<=0 {
txt.print("xx<=0\n")
} else {
txt.print("error1\n")
txt.print("error1qq\n")
}
if xx>0 {
txt.print("xx>0 error\n")
@ -53,27 +53,27 @@ main {
}
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")
; }
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")
}