fixed issues in word '>'

This commit is contained in:
Irmen de Jong 2021-03-16 23:06:28 +01:00
parent 6381d2b6ac
commit 00b9766aea
2 changed files with 174 additions and 142 deletions

View File

@ -501,6 +501,7 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
} }
private fun translateWordLessJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) { private fun translateWordLessJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) {
// TODO 100% checked ok.
fun code(msbCpyOperand: String, lsbCmpOperand: String) { fun code(msbCpyOperand: String, lsbCmpOperand: String) {
asmgen.out(""" asmgen.out("""
@ -666,15 +667,15 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
} }
private fun translateWordGreaterJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) { private fun translateWordGreaterJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) {
// TODO 100% checked ok.
fun code(msbCpyOperand: String, lsbCmpOperand: String) { fun code(leftName: String) {
asmgen.out(""" asmgen.out("""
cmp $lsbCmpOperand cmp $leftName
tya tya
sbc $msbCpyOperand sbc $leftName+1
bvc + bvc +
eor #${'$'}80 eor #$80
+ bmi $jumpIfFalseLabel""") + bpl $jumpIfFalseLabel""")
} }
if(rightConstVal!=null) { if(rightConstVal!=null) {
@ -685,30 +686,24 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
} else { } else {
if (left is IdentifierReference) { if (left is IdentifierReference) {
return if(rightConstVal.number.toInt()!=0) { return if(rightConstVal.number.toInt()!=0) {
// TODO is this correct? word > not-0 asmgen.assignExpressionToRegister(right, RegisterOrPair.AY)
asmgen.assignExpressionToRegister(left, RegisterOrPair.AY) code(asmgen.asmVariableName(left))
code("#>${rightConstVal.number}", "#<${rightConstVal.number}")
} }
else { else {
// TODO is this correct? word > 0 can be shorter as well?
val name = asmgen.asmVariableName(left) val name = asmgen.asmVariableName(left)
asmgen.out(""" asmgen.out("""
lda #0 lda $name+1
cmp $name bmi $jumpIfFalseLabel
sbc $name+1 lda $name
bvc + beq $jumpIfFalseLabel""")
eor #$80
+ bpl $jumpIfFalseLabel""")
} }
} }
} }
} }
if(right is IdentifierReference) { if(left is IdentifierReference) {
// TODO is this correct? word > identifer asmgen.assignExpressionToRegister(right, RegisterOrPair.AY)
asmgen.assignExpressionToRegister(left, RegisterOrPair.AY) return code(asmgen.asmVariableName(left))
val varname = asmgen.asmVariableName(right)
return code("$varname+1", varname)
} }
asmgen.assignExpressionToVariable(left, "P8ZP_SCRATCH_W2", DataType.UWORD, null) asmgen.assignExpressionToVariable(left, "P8ZP_SCRATCH_W2", DataType.UWORD, null)

View File

@ -1,140 +1,177 @@
; %import graphics %import textio
%zeropage basicsafe %zeropage basicsafe
main { main {
sub start2() {
word xx
word yy
; all comparisons with constant values are already optimized.
; but for variables (and memreads) we can optimize further (don't use temporary ZP location)
word value = 100
while xx==value {
yy++
}
while xx!=value {
yy++
}
do {
yy++
} until xx==value
do {
yy++
} until xx!=value
if xx==value
yy++
if xx!=value
yy++
; while xx>value {
; yy++
; }
; do {
; yy++
; } until xx>value
;
; if xx>value
; yy++
}
sub start() { sub start() {
uword uw1 txt.print("\n"*25)
uword uw2
word xx
word compare
xx=10
if xx>9
txt.print("1ok\n")
else
txt.print("1fault\n")
if xx>10
txt.print("2fault\n")
else
txt.print("2ok\n")
if xx>11
txt.print("3fault\n")
else
txt.print("3ok\n")
if xx>2222
txt.print("4fault\n")
else
txt.print("4ok\n")
if xx>-9
txt.print("5ok\n")
else
txt.print("5fault\n")
if xx>-9999
txt.print("6ok\n")
else
txt.print("6fault\n")
if xx>0
txt.print("7ok\n")
else
txt.print("7fault\n")
xx=0
if xx>0
txt.print("8false\n")
else
txt.print("8ok\n")
xx=-9999
if xx>0
txt.print("9false\n")
else
txt.print("9ok\n")
txt.nl()
xx=10
compare=9
if xx>compare
txt.print("1ok\n")
else
txt.print("1fault\n")
compare=10
if xx>compare
txt.print("2fault\n")
else
txt.print("2ok\n")
compare=11
if xx>compare
txt.print("3fault\n")
else
txt.print("3ok\n")
compare=2222
if xx>compare
txt.print("4fault\n")
else
txt.print("4ok\n")
compare=-9
if xx>compare
txt.print("5ok\n")
else
txt.print("5fault\n")
compare=-9999
if xx>compare
txt.print("6ok\n")
else
txt.print("6fault\n")
compare=0
if xx>compare
txt.print("7ok\n")
else
txt.print("7fault\n")
xx=0
if xx>compare
txt.print("8false\n")
else
txt.print("8ok\n")
xx=-9999
if xx>compare
txt.print("9false\n")
else
txt.print("9ok\n")
txt.nl()
xx=9
compare=9
if xx+1>compare
txt.print("1ok\n")
else
txt.print("1fault\n")
compare=10
if xx+1>compare
txt.print("2fault\n")
else
txt.print("2ok\n")
compare=11
if xx+1>compare
txt.print("3fault\n")
else
txt.print("3ok\n")
compare=2222
if xx+1>compare
txt.print("4fault\n")
else
txt.print("4ok\n")
compare=-9
if xx+1>compare
txt.print("5ok\n")
else
txt.print("5fault\n")
compare=-9999
if xx+1>compare
txt.print("6ok\n")
else
txt.print("6fault\n")
compare=0
if xx+1>compare
txt.print("7ok\n")
else
txt.print("7fault\n")
xx=1
if xx-1>compare
txt.print("8false\n")
else
txt.print("8ok\n")
xx=-9999
if xx-1>compare
txt.print("9false\n")
else
txt.print("9ok\n")
if uw1<uw2+2 {
uw1++
} }
byte xx
byte yy
; all comparisons with constant values are already optimized.
; but for variables (and memreads) we can optimize further (don't use temporary ZP location)
byte value = 100
while xx==value {
yy++
}
while xx==@($2000) {
yy++
}
while xx!=value {
yy++
}
while xx!=@($2000) {
yy++
}
do {
yy++
} until xx==value
do {
yy++
} until xx!=value
if xx==value
yy++
if xx!=value
yy++
while xx>value {
yy++
}
while xx<value {
yy++
}
do {
yy++
} until xx>value
do {
yy++
} until xx<value
if xx>value
yy++
if xx<value
yy++
}
; sub start2() {
;
; graphics.enable_bitmap_mode()
;
; uword xx
; ubyte yy
;
; graphics.line(150,50,150,50)
;
; for yy in 0 to 199-60 step 16 {
;
; for xx in 0 to 319-50 step 16 {
; graphics.line(30+xx, 10+yy, 50+xx, 30+yy)
; graphics.line(49+xx, 30+yy, 10+xx, 30+yy)
; graphics.line(11+xx, 29+yy, 29+xx, 11+yy)
;
; ; triangle 2, counter-clockwise
; graphics.line(30+xx, 40+yy, 10+xx, 60+yy)
; graphics.line(11+xx, 60+yy, 50+xx, 60+yy)
; graphics.line(49+xx, 59+yy, 31+xx,41+yy)
; }
; }
;
; repeat {
; }
; }
} }