fix optimized swapped in-place byte comparisons

This commit is contained in:
Irmen de Jong 2023-09-03 00:42:41 +02:00
parent 1ce8fe06d5
commit 95be1c9e22
3 changed files with 76 additions and 68 deletions

View File

@ -790,11 +790,12 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
"<" -> {
if(!signed) {
asmgen.out("""
tay
cmp $variable
bcc +
lda #0
cpy $variable
rol a
eor #1""")
beq ++
+ lda #1
+""")
}
else {
// see http://www.6502.org/tutorials/compare_beyond.html
@ -941,17 +942,63 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
}
}
"<" -> {
// variable<A --> A>variable?
if(!signed) {
TODO("swap operand order")
asmgen.out("""
tay
lda #0
cpy $variable
beq +
rol a
eor #1""")
+""")
}
else {
TODO("swap operand order")
// see http://www.6502.org/tutorials/compare_beyond.html
asmgen.out("""
clc
sbc $variable
bvc +
eor #$80
+ bpl +
lda #0
beq ++
+ lda #1
+""")
}
}
"<=" -> {
// variable<=A --> A>=variable?
if(!signed) {
asmgen.out("""
tay
lda #0
cpy $variable
rol a""")
} else {
// see http://www.6502.org/tutorials/compare_beyond.html
asmgen.out("""
sec
sbc $variable
bvc +
eor #$80
+ bpl +
lda #0
beq ++
+ lda #1
+""")
}
}
">" -> {
// variable>A --> A<variable?
if(!signed) {
asmgen.out("""
cmp $variable
bcc +
lda #0
beq ++
+ lda #1
+""")
} else {
// see http://www.6502.org/tutorials/compare_beyond.html
asmgen.out("""
sec
@ -965,9 +1012,9 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
+""")
}
}
"<=" -> {
">=" -> {
// variable>=A --> A<=variable?
if(!signed) {
TODO("swap operand order")
asmgen.out("""
cmp $variable
bcc +
@ -977,7 +1024,6 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
+ lda #1
+""")
} else {
TODO("swap operand order")
// see http://www.6502.org/tutorials/compare_beyond.html
asmgen.out("""
clc
@ -988,54 +1034,6 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
lda #0
beq ++
+ lda #1
+""")
}
}
">" -> {
if(!signed) {
TODO("swap operand order")
asmgen.out("""
tay
lda #0
cpy $variable
beq +
rol a
+""")
} else {
TODO("swap operand order")
// see http://www.6502.org/tutorials/compare_beyond.html
asmgen.out("""
clc
sbc $variable
bvc +
eor #$80
+ bpl +
lda #0
beq ++
+ lda #1
+""")
}
}
">=" -> {
if(!signed) {
TODO("swap operand order")
asmgen.out("""
tay
lda #0
cpy $variable
rol a""")
} else {
TODO("swap operand order")
// see http://www.6502.org/tutorials/compare_beyond.html
asmgen.out("""
sec
sbc $variable
bvc +
eor #$80
+ bpl +
lda #0
beq ++
+ lda #1
+""")
}
}

View File

@ -3,7 +3,6 @@ TODO
- fix on c64 target: examples/cube3d-float (broken since 9.3 with the evalstack removal) it works on x16 target, oddly enough.
More detailed and simpler code for this problem in floatproblem64.p8 / floatproblem64.asm (the minified version)
- fix: search for TODO("swap operand order")
- optimize: search for TODO optimize: don't use scratch var
- prefix prog8 subroutines with p8s_ instead of p8_ to not let them clash with variables in the asm?
- allow 'chained' array indexing for expressions: value = ptrarray[0][0]

View File

@ -3,15 +3,26 @@
main {
sub start() {
ubyte index = 100
ubyte[] t_index = [1,2,3,4,5]
ubyte nibble = 0
byte index = 100
byte[] t_index = [-1,-2,-3,-4,-5]
index += t_index[4]
index += t_index[nibble]
nibble++
index -= t_index[3]
index -= t_index[nibble]
txt.print_ub(index) ; 100
index = 4
index = index>t_index[4]
txt.print_b(index)
txt.nl()
index = index>t_index[4]
txt.print_b(index)
txt.nl()
; index = index < t_index[4]
; index = index < t_index[nibble]
; txt.print_ub(index)
; txt.nl()
;
; nibble++
; index = index > t_index[3]
; index = index > t_index[nibble]
; txt.print_ub(index) ; 100
; txt.nl()
}
}