mirror of
https://github.com/irmen/prog8.git
synced 2025-01-11 13:29:45 +00:00
fixed optimized code for > and <
This commit is contained in:
parent
584be44743
commit
dc870cd5ea
@ -1085,6 +1085,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checked OK:
|
||||||
private fun byteLess(expr: PtBinaryExpression, signed: Boolean) {
|
private fun byteLess(expr: PtBinaryExpression, signed: Boolean) {
|
||||||
// note: this is the inverse of byteGreaterEqual
|
// note: this is the inverse of byteGreaterEqual
|
||||||
when(expr.right) {
|
when(expr.right) {
|
||||||
@ -1145,8 +1146,12 @@ internal class AssignmentAsmGen(private val program: PtProgram,
|
|||||||
else
|
else
|
||||||
asmgen.out("""
|
asmgen.out("""
|
||||||
cmp P8ZP_SCRATCH_B1
|
cmp P8ZP_SCRATCH_B1
|
||||||
rol a
|
bcc +
|
||||||
and #1""")
|
beq +
|
||||||
|
lda #1
|
||||||
|
bne ++
|
||||||
|
+ lda #0
|
||||||
|
+""")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1155,6 +1160,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
|
|||||||
// note: this is the inverse of byteGreater
|
// note: this is the inverse of byteGreater
|
||||||
when(expr.right) {
|
when(expr.right) {
|
||||||
is PtNumber -> {
|
is PtNumber -> {
|
||||||
|
// TODO verify if this is correct code on all corner cases
|
||||||
val number = (expr.right as PtNumber).number.toInt()
|
val number = (expr.right as PtNumber).number.toInt()
|
||||||
asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed)
|
asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed)
|
||||||
if(signed)
|
if(signed)
|
||||||
@ -1176,6 +1182,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
|
|||||||
eor #1""")
|
eor #1""")
|
||||||
}
|
}
|
||||||
is PtIdentifier -> {
|
is PtIdentifier -> {
|
||||||
|
// TODO verify if this is correct code on all corner cases
|
||||||
val varname = (expr.right as PtIdentifier).name
|
val varname = (expr.right as PtIdentifier).name
|
||||||
asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed)
|
asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed)
|
||||||
if(signed)
|
if(signed)
|
||||||
@ -1197,6 +1204,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
|
|||||||
eor #1""")
|
eor #1""")
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
|
// TODO verify if this is correct code on all corner cases
|
||||||
// note: left and right operands get reversed here to reduce code size
|
// 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)
|
||||||
@ -1219,6 +1227,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checked OK:
|
||||||
private fun byteGreater(expr: PtBinaryExpression, signed: Boolean) {
|
private fun byteGreater(expr: PtBinaryExpression, signed: Boolean) {
|
||||||
// note: this is the inverse of byteLessEqual
|
// note: this is the inverse of byteLessEqual
|
||||||
when(expr.right) {
|
when(expr.right) {
|
||||||
@ -1229,6 +1238,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
|
|||||||
asmgen.out("""
|
asmgen.out("""
|
||||||
sec
|
sec
|
||||||
sbc #$number
|
sbc #$number
|
||||||
|
beq +++
|
||||||
bvc +
|
bvc +
|
||||||
eor #$80
|
eor #$80
|
||||||
+ bmi +
|
+ bmi +
|
||||||
@ -1239,8 +1249,12 @@ internal class AssignmentAsmGen(private val program: PtProgram,
|
|||||||
else
|
else
|
||||||
asmgen.out("""
|
asmgen.out("""
|
||||||
cmp #$number
|
cmp #$number
|
||||||
lda #0
|
bcc +
|
||||||
rol a""")
|
beq +
|
||||||
|
lda #1
|
||||||
|
bne ++
|
||||||
|
+ lda #0
|
||||||
|
+""")
|
||||||
}
|
}
|
||||||
is PtIdentifier -> {
|
is PtIdentifier -> {
|
||||||
val varname = (expr.right as PtIdentifier).name
|
val varname = (expr.right as PtIdentifier).name
|
||||||
@ -1249,6 +1263,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
|
|||||||
asmgen.out("""
|
asmgen.out("""
|
||||||
sec
|
sec
|
||||||
sbc $varname
|
sbc $varname
|
||||||
|
beq +++
|
||||||
bvc +
|
bvc +
|
||||||
eor #$80
|
eor #$80
|
||||||
+ bmi +
|
+ bmi +
|
||||||
@ -1259,8 +1274,12 @@ internal class AssignmentAsmGen(private val program: PtProgram,
|
|||||||
else
|
else
|
||||||
asmgen.out("""
|
asmgen.out("""
|
||||||
cmp $varname
|
cmp $varname
|
||||||
lda #0
|
bcc +
|
||||||
rol a""")
|
beq +
|
||||||
|
lda #1
|
||||||
|
bne ++
|
||||||
|
+ lda #0
|
||||||
|
+""")
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
// note: left and right operands get reversed here to reduce code size
|
// note: left and right operands get reversed here to reduce code size
|
||||||
@ -1290,6 +1309,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
|
|||||||
// note: this is the inverse of byteLess
|
// note: this is the inverse of byteLess
|
||||||
when(expr.right) {
|
when(expr.right) {
|
||||||
is PtNumber -> {
|
is PtNumber -> {
|
||||||
|
// TODO verify if this is correct code on all corner cases
|
||||||
val number = (expr.right as PtNumber).number.toInt()
|
val number = (expr.right as PtNumber).number.toInt()
|
||||||
asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed)
|
asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed)
|
||||||
if(signed) {
|
if(signed) {
|
||||||
@ -1309,6 +1329,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
|
|||||||
and #1""")
|
and #1""")
|
||||||
}
|
}
|
||||||
is PtIdentifier -> {
|
is PtIdentifier -> {
|
||||||
|
// TODO verify if this is correct code on all corner cases
|
||||||
val varname = (expr.right as PtIdentifier).name
|
val varname = (expr.right as PtIdentifier).name
|
||||||
asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed)
|
asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed)
|
||||||
if(signed) {
|
if(signed) {
|
||||||
@ -1328,6 +1349,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
|
|||||||
and #1""")
|
and #1""")
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
|
// TODO verify if this is correct code on all corner cases
|
||||||
// note: left and right operands get reversed here to reduce code size
|
// 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)
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
TODO
|
TODO
|
||||||
====
|
====
|
||||||
|
|
||||||
- fix bug introduced by 017ef8a837077c339993004da9b4ccf5c8ca7b13 (> and/or <= change in AssignmentAsmGen byteLessEquals/byteGreater
|
- verify all comparison cornercases in the TODOS in AssignmentAsmGen
|
||||||
Bug manifests in prog8 where 2nd row of aliens jerks too far to the right after a short while.
|
|
||||||
|
|
||||||
- [on branch:] investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 ....
|
- [on branch:] 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
|
||||||
|
220
examples/test.p8
220
examples/test.p8
@ -2,77 +2,153 @@
|
|||||||
%zeropage basicsafe
|
%zeropage basicsafe
|
||||||
|
|
||||||
main {
|
main {
|
||||||
sub start () {
|
sub start() {
|
||||||
ubyte[4] @shared array = 10 to 20 step 3
|
greater()
|
||||||
ubyte[4] @shared array2 = 20 downto 10 step -3
|
greater_signed()
|
||||||
byte[7] @shared array3 = 10 downto -10 step -3
|
less()
|
||||||
|
less_signed()
|
||||||
ubyte xx
|
|
||||||
byte bb
|
|
||||||
|
|
||||||
cx16.r1=0
|
|
||||||
for cx16.r0 in 0 to 10 {
|
|
||||||
cx16.r1++
|
|
||||||
}
|
|
||||||
txt.print_uw(cx16.r1)
|
|
||||||
txt.nl()
|
|
||||||
txt.nl()
|
|
||||||
|
|
||||||
for xx in array {
|
|
||||||
txt.print_ub(xx)
|
|
||||||
txt.spc()
|
|
||||||
}
|
|
||||||
txt.nl()
|
|
||||||
for xx in array2 {
|
|
||||||
txt.print_ub(xx)
|
|
||||||
txt.spc()
|
|
||||||
}
|
|
||||||
txt.nl()
|
|
||||||
for bb in array3 {
|
|
||||||
txt.print_b(bb)
|
|
||||||
txt.spc()
|
|
||||||
}
|
|
||||||
txt.nl()
|
|
||||||
txt.nl()
|
|
||||||
|
|
||||||
|
|
||||||
for xx in 10 to 20 step 3 { ; TODO fix IR/VM code that wraps around instead of stopping at 19
|
|
||||||
txt.print_ub(xx)
|
|
||||||
txt.spc()
|
|
||||||
}
|
|
||||||
txt.nl()
|
|
||||||
|
|
||||||
for xx in 20 downto 10 step -3 { ; TODO fix IR/VM code that wraps around instead of stopping at 11
|
|
||||||
txt.print_ub(xx)
|
|
||||||
txt.spc()
|
|
||||||
}
|
|
||||||
txt.nl()
|
|
||||||
|
|
||||||
for bb in 10 downto -10 step -3 { ; TODO fix IR/VM code that wraps around instead of stopping at -8
|
|
||||||
txt.print_b(bb)
|
|
||||||
txt.spc()
|
|
||||||
}
|
|
||||||
|
|
||||||
txt.nl()
|
|
||||||
txt.nl()
|
|
||||||
ubyte ending = 20
|
|
||||||
|
|
||||||
for xx in 10 to ending step 3 {
|
|
||||||
txt.print_ub(xx)
|
|
||||||
txt.spc()
|
|
||||||
}
|
|
||||||
txt.nl()
|
|
||||||
ending =10
|
|
||||||
for xx in 20 downto ending step -3 {
|
|
||||||
txt.print_ub(xx)
|
|
||||||
txt.spc()
|
|
||||||
}
|
|
||||||
txt.nl()
|
|
||||||
|
|
||||||
byte endingb = -10
|
|
||||||
for bb in 10 downto endingb step -3 {
|
|
||||||
txt.print_b(bb)
|
|
||||||
txt.spc()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub value(ubyte arg) -> ubyte {
|
||||||
|
cx16.r0++
|
||||||
|
return arg
|
||||||
|
}
|
||||||
|
|
||||||
|
sub svalue(byte arg) -> byte {
|
||||||
|
cx16.r0++
|
||||||
|
return arg
|
||||||
|
}
|
||||||
|
|
||||||
|
sub greater () {
|
||||||
|
ubyte b1 = 10
|
||||||
|
ubyte b2 = 20
|
||||||
|
ubyte b3 = 10
|
||||||
|
|
||||||
|
txt.print("101010: ")
|
||||||
|
ubyte xx
|
||||||
|
xx = b2>10
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
xx = b2>20
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
xx = b2>b1
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
xx = b3>b1
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
xx = b2>value(10)
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
xx = b3>value(20)
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
txt.nl()
|
||||||
|
}
|
||||||
|
|
||||||
|
sub greater_signed () {
|
||||||
|
byte b1 = -20
|
||||||
|
byte b2 = -10
|
||||||
|
byte b3 = -20
|
||||||
|
|
||||||
|
txt.print("101010: ")
|
||||||
|
ubyte xx
|
||||||
|
xx = b2 > -20
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
xx = b2 > -10
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
xx = b2>b1
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
xx = b3>b1
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
xx = b2>svalue(-20)
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
xx = b3>svalue(-10)
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
txt.nl()
|
||||||
|
}
|
||||||
|
|
||||||
|
sub less () {
|
||||||
|
ubyte b1 = 20
|
||||||
|
ubyte b2 = 10
|
||||||
|
ubyte b3 = 20
|
||||||
|
|
||||||
|
txt.print("101010: ")
|
||||||
|
ubyte xx
|
||||||
|
xx = b2<20
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
xx = b2<10
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
xx = b2<b1
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
xx = b3<b1
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
xx = b2<value(20)
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
xx = b2<value(10)
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
txt.nl()
|
||||||
|
}
|
||||||
|
|
||||||
|
sub less_signed () {
|
||||||
|
byte b1 = -10
|
||||||
|
byte b2 = -20
|
||||||
|
byte b3 = -10
|
||||||
|
|
||||||
|
txt.print("101010: ")
|
||||||
|
ubyte xx
|
||||||
|
xx = b2 < -10
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
xx = b2 < -20
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
xx = b2<b1
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
xx = b3<b1
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
xx = b2<svalue(-10)
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
xx = b3<svalue(-20)
|
||||||
|
txt.print_ub(xx)
|
||||||
|
txt.spc()
|
||||||
|
|
||||||
|
txt.nl()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user