diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt index 402160d48..6ba4e33a7 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt @@ -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 { diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 874d4bab1..43bcda67a 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -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 ...