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

View File

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