comparing function call result to 0 now more efficient, without translateExpression()

This commit is contained in:
Irmen de Jong 2020-11-18 00:05:48 +01:00
parent 79c75adac1
commit e95af7498e
2 changed files with 30 additions and 52 deletions

View File

@ -59,8 +59,8 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
val dt = idt.typeOrElse(DataType.STRUCT) val dt = idt.typeOrElse(DataType.STRUCT)
when (operator) { when (operator) {
"==" -> { "==" -> {
// if the left operand is an expression, and the right is 0, we can just evaluate that expression. // if the left operand is an expression, and the right is 0, we can just evaluate that expression,
// (the extra comparison is not required as the result of the expression is already the required boolean value) // and use the result value directly to determine the boolean result. Shortcut only for integers.
if(rightConstVal?.number?.toDouble() == 0.0) { if(rightConstVal?.number?.toDouble() == 0.0) {
when(left) { when(left) {
is PrefixExpression, is PrefixExpression,
@ -70,24 +70,19 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
is AddressOf, is AddressOf,
is RangeExpr, is RangeExpr,
is FunctionCall -> { is FunctionCall -> {
translateExpression(left) // todo directly into AY reg?
if(dt in ByteDatatypes) { if(dt in ByteDatatypes) {
asmgen.out(""" asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
inx asmgen.out(" bne $jumpIfFalseLabel")
lda P8ESTACK_LO,x
bne $jumpIfFalseLabel""")
return return
} }
else if(dt in WordDatatypes) { else if(dt in WordDatatypes) {
asmgen.assignExpressionToRegister(left, RegisterOrPair.AY)
asmgen.out(""" asmgen.out("""
inx sty P8ZP_SCRATCH_B1
lda P8ESTACK_LO,x ora P8ZP_SCRATCH_B1
clc
adc P8ESTACK_HI,x
bne $jumpIfFalseLabel""") bne $jumpIfFalseLabel""")
return return
} }
// TODO ....float?
} }
else -> {} else -> {}
} }
@ -102,8 +97,8 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
} }
} }
"!=" -> { "!=" -> {
// if the left operand is an expression, and the right is 0, we can just evaluate that expression. // if the left operand is an expression, and the right is 0, we can just evaluate that expression,
// (the extra comparison is not required as the result of the expression is already the required boolean value) // and use the result value directly to determine the boolean result. Shortcut only for integers.
if(rightConstVal?.number?.toDouble() == 0.0) { if(rightConstVal?.number?.toDouble() == 0.0) {
when(left) { when(left) {
is PrefixExpression, is PrefixExpression,
@ -113,24 +108,19 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
is AddressOf, is AddressOf,
is RangeExpr, is RangeExpr,
is FunctionCall -> { is FunctionCall -> {
translateExpression(left) // todo directly into AY?
if(dt in ByteDatatypes) { if(dt in ByteDatatypes) {
asmgen.out(""" asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
inx asmgen.out(" beq $jumpIfFalseLabel")
lda P8ESTACK_LO,x
beq $jumpIfFalseLabel""")
return return
} }
else if(dt in WordDatatypes) { else if(dt in WordDatatypes) {
asmgen.assignExpressionToRegister(left, RegisterOrPair.AY)
asmgen.out(""" asmgen.out("""
inx sty P8ZP_SCRATCH_B1
lda P8ESTACK_LO,x ora P8ZP_SCRATCH_B1
clc
adc P8ESTACK_HI,x
beq $jumpIfFalseLabel""") beq $jumpIfFalseLabel""")
return return
} }
// TODO .... .float?
} }
else -> {} else -> {}
} }

View File

@ -9,41 +9,29 @@ main {
ubyte ub = 4 ubyte ub = 4
uword uw = 5 uword uw = 5
when ub { if ding(22)!=0
1 -> txt.chrout('1') txt.chrout('1')
2 -> txt.chrout('2') if ding(22)
3 -> txt.chrout('3') txt.chrout('2')
4 -> txt.chrout('4')
else -> txt.chrout('?')
}
txt.chrout('\n') txt.chrout('\n')
when uw { if dingw($1100)!=$0000
$0001 -> txt.chrout('1') txt.chrout('1')
$0002 -> txt.chrout('2') if dingw($1100)
$0003 -> txt.chrout('3') txt.chrout('2')
$0004 -> txt.chrout('4')
$0005 -> txt.chrout('5')
else -> txt.chrout('?')
}
txt.chrout('\n') txt.chrout('\n')
testX() testX()
} }
asmsub setflag(ubyte bitje @ Pc) { sub ding(ubyte argument) -> ubyte {
%asm {{ txt.chrout(' ')
bcs + return argument*2
lda #'0' }
jsr c64.CHROUT
lda #13 sub dingw(uword argument) -> uword {
jmp c64.CHROUT txt.chrout(' ')
+ lda #'1' return argument*2
jsr c64.CHROUT
lda #13
jmp c64.CHROUT
}}
} }
asmsub testX() { asmsub testX() {