optimization of > and <= in expressions

This commit is contained in:
Irmen de Jong 2023-08-07 21:20:02 +02:00
parent 0d63cdcb96
commit 017ef8a837
3 changed files with 134 additions and 45 deletions

View File

@ -1150,7 +1150,52 @@ internal class AssignmentAsmGen(private val program: PtProgram,
} }
private fun byteLessEquals(expr: PtBinaryExpression, signed: Boolean) { private fun byteLessEquals(expr: PtBinaryExpression, signed: Boolean) {
// TODO optimize: no need to use a temporary variable if the right expression is a literal number or a variable name (or register name) // note: this is the inverse of byteGreater
when(expr.right) {
is PtNumber -> {
val number = (expr.right as PtNumber).number.toInt()
asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed)
if(signed)
asmgen.out("""
sec
sbc #$number
bvc +
eor #$80
+ bmi +
lda #0
beq ++
+ lda #1
+""")
else
asmgen.out("""
cmp #$number
lda #0
rol a
eor #1""")
}
is PtIdentifier -> {
val varname = (expr.right as PtIdentifier).name
asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed)
if(signed)
asmgen.out("""
sec
sbc $varname
bvc +
eor #$80
+ bmi +
lda #0
beq ++
+ lda #1
+""")
else
asmgen.out("""
cmp $varname
lda #0
rol a
eor #1""")
}
else -> {
// note: left and right operands get reversed here to reduce code size
asmgen.assignByteOperandsToAAndVar(expr.right, expr.left, "P8ZP_SCRATCH_B1") asmgen.assignByteOperandsToAAndVar(expr.right, expr.left, "P8ZP_SCRATCH_B1")
if(signed) if(signed)
asmgen.out(""" asmgen.out("""
@ -1158,10 +1203,10 @@ internal class AssignmentAsmGen(private val program: PtProgram,
sbc P8ZP_SCRATCH_B1 sbc P8ZP_SCRATCH_B1
bvc + bvc +
eor #$80 eor #$80
+ bpl + + bmi +
lda #0 lda #1
beq ++ bne ++
+ lda #1 + lda #0
+""") +""")
else else
asmgen.out(""" asmgen.out("""
@ -1169,9 +1214,54 @@ internal class AssignmentAsmGen(private val program: PtProgram,
lda #0 lda #0
rol a""") rol a""")
} }
}
}
private fun byteGreater(expr: PtBinaryExpression, signed: Boolean) { private fun byteGreater(expr: PtBinaryExpression, signed: Boolean) {
// TODO optimize: no need to use a temporary variable if the right expression is a literal number or a variable name (or register name) // note: this is the inverse of byteLessEqual
when(expr.right) {
is PtNumber -> {
val number = (expr.right as PtNumber).number.toInt()
asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed)
if(signed)
asmgen.out("""
sec
sbc #$number
bvc +
eor #$80
+ bmi +
lda #1
bne ++
+ lda #0
+""")
else
asmgen.out("""
cmp #$number
lda #0
rol a""")
}
is PtIdentifier -> {
val varname = (expr.right as PtIdentifier).name
asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed)
if(signed)
asmgen.out("""
sec
sbc $varname
bvc +
eor #$80
+ bmi +
lda #1
bne ++
+ lda #0
+""")
else
asmgen.out("""
cmp $varname
lda #0
rol a""")
}
else -> {
// note: left and right operands get reversed here to reduce code size
asmgen.assignByteOperandsToAAndVar(expr.right, expr.left, "P8ZP_SCRATCH_B1") asmgen.assignByteOperandsToAAndVar(expr.right, expr.left, "P8ZP_SCRATCH_B1")
if(signed) if(signed)
asmgen.out(""" asmgen.out("""
@ -1191,6 +1281,8 @@ internal class AssignmentAsmGen(private val program: PtProgram,
rol a rol a
eor #1""") eor #1""")
} }
}
}
private fun byteGreaterEquals(expr: PtBinaryExpression, signed: Boolean) { private fun byteGreaterEquals(expr: PtBinaryExpression, signed: Boolean) {
// note: this is the inverse of byteLess // note: this is the inverse of byteLess

View File

@ -1,8 +1,6 @@
TODO TODO
==== ====
- optimize byteLessEquals() and byteGreater() in AssignmentAsmGen.
- in other codegen for comparisons, try to reduce lda #0/lda #1 by rol a using the carry from the cmp if possible.
- investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 .... - investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 ....
- IR: reduce the number of branch instructions such as BEQ, BEQR, etc (gradually), replace with CMP(I) + status branch instruction - IR: reduce the number of branch instructions such as BEQ, BEQR, etc (gradually), replace with CMP(I) + status branch instruction

View File

@ -5,26 +5,25 @@ main {
sub start() { sub start() {
ubyte a = 2 ubyte a = 2
ubyte var1 = 3 ubyte var1 = 3
ubyte var2 = 1 ubyte var2 = 10
if a==2 and a<=10 {
if a==2 and a>=1 {
a++ a++
} }
if a==var1 and a>=var2 { if a==var1 and a<=var2 {
a++ a++
} }
txt.print_ub(a) ; 4 txt.print_ub(a) ; 4
byte sa = 2 byte sa = 2
byte svar1 = 3 byte svar1 = 3
byte svar2 = 1 byte svar2 = 10
if sa==2 and sa>=1 { if sa==2 and sa<=10 {
sa++ sa++
} }
if sa==svar1 and sa>=svar2 { if sa==svar1 and sa<=svar2 {
sa++ sa++
} }
txt.print_b(sa) ; 4 code 287 txt.print_b(sa) ; 4 code 28f
} }
} }