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

View File

@ -1,140 +1,177 @@
; %import graphics
%import textio
%zeropage basicsafe
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() {
uword uw1
uword uw2
txt.print("\n"*25)
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 {
; }
; }
}