optimized ubyte comparison expressions

This commit is contained in:
Irmen de Jong 2023-05-07 14:47:31 +02:00
parent c65279b672
commit 533d825f1a
2 changed files with 94 additions and 2 deletions

View File

@ -690,8 +690,97 @@ internal class AssignmentAsmGen(private val program: PtProgram,
}
private fun assignOptimizedComparisonBytes(expr: PtBinaryExpression, assign: AsmAssignment): Boolean {
// TODO("Not yet implemented")
return false
val signed = expr.left.type == DataType.BYTE || expr.right.type == DataType.BYTE
fun assignExpressionOperandsLeftScratchRightA() {
if(expr.right.isSimple()) {
assignExpressionToVariable(expr.left, "P8ZP_SCRATCH_B1", DataType.UBYTE)
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)
asmgen.restoreRegisterStack(CpuRegister.A, false)
}
}
when(expr.operator) {
"==" -> {
assignExpressionOperandsLeftScratchRightA()
asmgen.out("""
cmp P8ZP_SCRATCH_B1
beq +
lda #0
beq ++
+ lda #1
+""")
}
"!=" -> {
assignExpressionOperandsLeftScratchRightA()
asmgen.out("""
cmp P8ZP_SCRATCH_B1
bne +
lda #0
beq ++
+ lda #1
+""")
}
"<" -> {
assignExpressionOperandsLeftScratchRightA()
if(signed)
return false // TODO("< signed")
else
asmgen.out("""
cmp P8ZP_SCRATCH_B1
beq +
bcs ++
+ lda #0
beq ++
+ lda #1
+""")
}
"<=" -> {
assignExpressionOperandsLeftScratchRightA()
if(signed)
return false // TODO("< signed")
else
asmgen.out("""
cmp P8ZP_SCRATCH_B1
lda #0
rol a""")
}
">" -> {
assignExpressionOperandsLeftScratchRightA()
if(signed)
return false // TODO("< signed")
else
asmgen.out("""
cmp P8ZP_SCRATCH_B1
bcc +
lda #0
beq ++
+ lda #1
+""")
}
">=" -> {
assignExpressionOperandsLeftScratchRightA()
if(signed)
return false // TODO(">= signed")
else
asmgen.out("""
cmp P8ZP_SCRATCH_B1
bcc +
beq +
lda #0
beq ++
+ lda #1
+""")
}
else -> return false
}
assignRegisterByte(assign.target, CpuRegister.A, signed)
return true
}
private fun assignOptimizedComparisonWords(expr: PtBinaryExpression, assign: AsmAssignment): Boolean {

View File

@ -3,6 +3,9 @@ TODO
For next minor release
^^^^^^^^^^^^^^^^^^^^^^
- assignOptimizedComparisonBytes() / 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
...