improve byte '<' and '>' codegen

This commit is contained in:
Irmen de Jong 2021-03-15 22:24:09 +01:00
parent ded1d19737
commit bf23ad78e6
2 changed files with 110 additions and 48 deletions

View File

@ -410,23 +410,48 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
return return
} else { } else {
if (left is IdentifierReference) { if (left is IdentifierReference) {
val name = asmgen.asmVariableName(left) asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
if(rightConstVal.number.toInt()!=0) if(rightConstVal.number.toInt()!=0)
asmgen.out(""" asmgen.out("""
lda $name
sec sec
sbc #${rightConstVal.number} sbc #${rightConstVal.number}
bvc + bvc +
eor #$80 eor #$80
+ bpl $jumpIfFalseLabel""") + bpl $jumpIfFalseLabel""")
else else
asmgen.out(" lda $name | bpl $jumpIfFalseLabel") asmgen.out(" bpl $jumpIfFalseLabel")
return return
} }
} }
} }
// TODO optimize if right is variable or mem-read to avoid use of ZP location if(right is IdentifierReference) {
asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
asmgen.out("""
sec
sbc ${asmgen.asmVariableName(right)}
bvc +
eor #$80
+ bpl $jumpIfFalseLabel""")
return
}
var memread = right as? DirectMemoryRead
if(memread==null && right is TypecastExpression)
memread = right.expression as? DirectMemoryRead
if(memread!=null) {
val address = memread.addressExpression as? NumericLiteralValue
if(address!=null) {
asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
asmgen.out("""
sec
sbc ${address.number.toHex()}
bvc +
eor #$80
+ bpl $jumpIfFalseLabel""")
return
}
}
asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE, null) asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE, null)
asmgen.assignExpressionToRegister(left, RegisterOrPair.A) asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
asmgen.out(""" asmgen.out("""
@ -547,10 +572,9 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
return return
} else { } else {
if (left is IdentifierReference) { if (left is IdentifierReference) {
val name = asmgen.asmVariableName(left) asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
if(rightConstVal.number.toInt()!=0) if(rightConstVal.number.toInt()!=0)
asmgen.out(""" asmgen.out("""
lda $name
clc clc
sbc #${rightConstVal.number} sbc #${rightConstVal.number}
bvc + bvc +
@ -559,13 +583,43 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
bmi $jumpIfFalseLabel bmi $jumpIfFalseLabel
+""") +""")
else else
asmgen.out(" lda $name | bmi $jumpIfFalseLabel | beq $jumpIfFalseLabel") asmgen.out(" bmi $jumpIfFalseLabel | beq $jumpIfFalseLabel")
return return
} }
} }
} }
// TODO optimize if right is variable or mem-read to avoid use of ZP location if(right is IdentifierReference) {
asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
asmgen.out("""
clc
sbc ${asmgen.asmVariableName(right)}
bvc +
eor #$80
+ bpl +
bmi $jumpIfFalseLabel
+""")
return
}
var memread = right as? DirectMemoryRead
if(memread==null && right is TypecastExpression)
memread = right.expression as? DirectMemoryRead
if(memread!=null) {
val address = memread.addressExpression as? NumericLiteralValue
if(address!=null) {
asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
asmgen.out("""
clc
sbc ${address.number.toHex()}
bvc +
eor #$80
+ bpl +
bmi $jumpIfFalseLabel
+""")
return
}
}
asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE, null) asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE, null)
asmgen.assignExpressionToRegister(left, RegisterOrPair.A) asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
asmgen.out(""" asmgen.out("""

View File

@ -1,8 +1,8 @@
%import graphics ; %import graphics
%zeropage basicsafe %zeropage basicsafe
main { main {
sub start() { sub start2() {
word xx word xx
word yy word yy
@ -45,7 +45,7 @@ main {
; yy++ ; yy++
} }
sub start3() { sub start() {
byte xx byte xx
byte yy byte yy
@ -85,42 +85,50 @@ main {
if xx!=value if xx!=value
yy++ yy++
; while xx>value { while xx>value {
; yy++ yy++
; } }
; do { while xx<value {
; yy++ yy++
; } until xx>value }
do {
yy++
} until xx>value
do {
yy++
} until xx<value
if xx>value
yy++
if xx<value
yy++
}
; sub start2() {
; ;
; if xx>value ; graphics.enable_bitmap_mode()
; yy++ ;
} ; uword xx
; ubyte yy
;
sub start2() { ; graphics.line(150,50,150,50)
;
graphics.enable_bitmap_mode() ; for yy in 0 to 199-60 step 16 {
;
uword xx ; for xx in 0 to 319-50 step 16 {
ubyte yy ; graphics.line(30+xx, 10+yy, 50+xx, 30+yy)
; graphics.line(49+xx, 30+yy, 10+xx, 30+yy)
graphics.line(150,50,150,50) ; graphics.line(11+xx, 29+yy, 29+xx, 11+yy)
;
for yy in 0 to 199-60 step 16 { ; ; triangle 2, counter-clockwise
; graphics.line(30+xx, 40+yy, 10+xx, 60+yy)
for xx in 0 to 319-50 step 16 { ; graphics.line(11+xx, 60+yy, 50+xx, 60+yy)
graphics.line(30+xx, 10+yy, 50+xx, 30+yy) ; graphics.line(49+xx, 59+yy, 31+xx,41+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 ; repeat {
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 {
}
}
} }