optimized word equality comparison expressions

This commit is contained in:
Irmen de Jong 2023-05-07 15:28:20 +02:00
parent ffb54110e9
commit 393e914a86
3 changed files with 103 additions and 23 deletions

View File

@ -694,12 +694,12 @@ internal class AssignmentAsmGen(private val program: PtProgram,
fun assignExpressionOperandsLeftScratchRightA() {
if(expr.right.isSimple()) {
assignExpressionToVariable(expr.left, "P8ZP_SCRATCH_B1", DataType.UBYTE)
assignExpressionToVariable(expr.left, "P8ZP_SCRATCH_B1", expr.left.type)
assignExpressionToRegister(expr.right, RegisterOrPair.A, signed)
} else {
assignExpressionToRegister(expr.right, RegisterOrPair.A, signed)
asmgen.saveRegisterStack(CpuRegister.A, false)
assignExpressionToVariable(expr.left, "P8ZP_SCRATCH_B1", DataType.UBYTE)
assignExpressionToVariable(expr.left, "P8ZP_SCRATCH_B1", expr.left.type)
asmgen.restoreRegisterStack(CpuRegister.A, false)
}
}
@ -820,8 +820,90 @@ internal class AssignmentAsmGen(private val program: PtProgram,
}
private fun assignOptimizedComparisonWords(expr: PtBinaryExpression, assign: AsmAssignment): Boolean {
// TODO("Not yet implemented")
return false
val signed = expr.left.type == DataType.BYTE || expr.right.type == DataType.BYTE
fun assignExpressionOperandsLeftScratchRightAY() {
if(expr.right.isSimple()) {
assignExpressionToVariable(expr.left, "P8ZP_SCRATCH_W1", expr.left.type)
assignExpressionToRegister(expr.right, RegisterOrPair.AY, signed)
} else {
assignExpressionToRegister(expr.right, RegisterOrPair.AY, signed)
asmgen.saveRegisterStack(CpuRegister.A, false)
asmgen.saveRegisterStack(CpuRegister.Y, false)
assignExpressionToVariable(expr.left, "P8ZP_SCRATCH_W1", expr.left.type)
asmgen.restoreRegisterStack(CpuRegister.Y, false)
asmgen.restoreRegisterStack(CpuRegister.A, false)
}
}
when(expr.operator) {
"==" -> {
assignExpressionOperandsLeftScratchRightAY()
asmgen.out("""
cmp P8ZP_SCRATCH_W1
bne +
cpy P8ZP_SCRATCH_W1+1
bne +
lda #1
bne ++
+ lda #0
+""")
}
"!=" -> {
assignExpressionOperandsLeftScratchRightAY()
asmgen.out("""
cmp P8ZP_SCRATCH_W1
bne +
cpy P8ZP_SCRATCH_W1+1
bne +
lda #0
beq ++
+ lda #1
+""")
}
"<" -> {
if(signed) {
// TODO("word <")
return false
}
else {
// TODO("uword <")
return false
}
}
"<=" -> {
if(signed) {
// TODO("word <=")
return false
}
else {
// TODO("uword =<")
return false
}
}
">" -> {
if(signed) {
// TODO("word >")
return false
}
else {
// TODO("uword >")
return false
}
}
">=" -> {
if(signed) {
// TODO("word >=")
return false
}
else {
// TODO("uword >=")
return false
}
}
else -> return false
}
assignRegisterByte(assign.target, CpuRegister.A, signed)
return true
}
private fun assignLogicalWithSimpleRightOperandByte(target: AsmAssignTarget, left: PtExpression, operator: String, right: PtExpression) {

View File

@ -3,7 +3,7 @@ TODO
For next minor release
^^^^^^^^^^^^^^^^^^^^^^
- assignOptimizedComparisonBytes() / assignOptimizedComparisonWords()
- finish assignOptimizedComparisonWords()
- find bcc/bcs branches that could be a rol?
- find adc #0 that could be a rol?
- try to optimize newexpr a bit more

View File

@ -8,50 +8,48 @@ main {
; signed and unsigned word:
; ==
; !=
; >
; >=
; <
; <=
; expect yep yep nope nope nope
cx16.r0sL = -10
cx16.r2sL = -9
if (cx16.r0sL <= cx16.r2sL) or (envelope_attacks[cx16.r1L]==0) {
; expect nope nope yep yep yep
cx16.r0 = $ea30
cx16.r2 = $ea31
if (cx16.r0 > cx16.r2) or (envelope_attacks[cx16.r1L]==0) {
txt.print("\nyep\n")
} else {
txt.print("\nnope\n")
}
cx16.r0sL = -9
cx16.r2sL = -9
if (cx16.r0sL <= cx16.r2sL) or (envelope_attacks[cx16.r1L]==0) {
cx16.r0 = $ea31
cx16.r2 = $ea31
if (cx16.r0 > cx16.r2) or (envelope_attacks[cx16.r1L]==0) {
txt.print("\nyep\n")
} else {
txt.print("\nnope\n")
}
cx16.r0sL = -8
cx16.r2sL = -9
if (cx16.r0sL <= cx16.r2sL) or (envelope_attacks[cx16.r1L]==0) {
cx16.r0 = $ea32
cx16.r2 = $ea31
if (cx16.r0 > cx16.r2) or (envelope_attacks[cx16.r1L]==0) {
txt.print("\nyep\n")
} else {
txt.print("\nnope\n")
}
cx16.r0sL = 0
cx16.r2sL = -9
if (cx16.r0sL <= cx16.r2sL) or (envelope_attacks[cx16.r1L]==0) {
cx16.r0 = $ee30
cx16.r2 = $ea31
if (cx16.r0 > cx16.r2) or (envelope_attacks[cx16.r1L]==0) {
txt.print("\nyep\n")
} else {
txt.print("\nnope\n")
}
cx16.r0sL = 10
cx16.r2sL = 1
if (cx16.r0sL <= cx16.r2sL) or (envelope_attacks[cx16.r1L]==0) {
cx16.r0 = $ffff
cx16.r2 = $ee31
if (cx16.r0 > cx16.r2) or (envelope_attacks[cx16.r1L]==0) {
txt.print("\nyep\n")
} else {
txt.print("\nnope\n")