From 68a6f99c9fb8796d2cceffde0f41649f6dd7b015 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 7 Aug 2023 01:49:00 +0200 Subject: [PATCH] optimization of < in expressions --- .../cpu6502/assignment/AssignmentAsmGen.kt | 78 +++++++++++++------ examples/test.p8 | 24 +++--- 2 files changed, 71 insertions(+), 31 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt index 2fed282ec..cb91d8cd0 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt @@ -996,8 +996,8 @@ internal class AssignmentAsmGen(private val program: PtProgram, private fun assignOptimizedComparisonBytes(expr: PtBinaryExpression, assign: AsmAssignment): Boolean { val signed = expr.left.type == DataType.BYTE || expr.right.type == DataType.BYTE when(expr.operator) { - "==" -> byteEquals(expr, signed) - "!=" -> byteNotEquals(expr, signed) + "==" -> byteEquals(expr) + "!=" -> byteNotEquals(expr) "<" -> byteLess(expr, signed) "<=" -> byteLessEquals(expr, signed) ">" -> byteGreater(expr, signed) @@ -1005,15 +1005,15 @@ internal class AssignmentAsmGen(private val program: PtProgram, else -> return false } - assignRegisterByte(assign.target, CpuRegister.A, signed) + assignRegisterByte(assign.target, CpuRegister.A, false) return true } - private fun byteEquals(expr: PtBinaryExpression, signed: Boolean) { + private fun byteEquals(expr: PtBinaryExpression) { when (expr.right) { is PtNumber -> { val number = (expr.right as PtNumber).number.toInt() - asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed) + asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A) asmgen.out(""" cmp #$number beq + @@ -1024,7 +1024,7 @@ internal class AssignmentAsmGen(private val program: PtProgram, } is PtIdentifier -> { val varname = (expr.right as PtIdentifier).name - asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed) + asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A) asmgen.out(""" cmp $varname beq + @@ -1046,11 +1046,11 @@ internal class AssignmentAsmGen(private val program: PtProgram, } } - private fun byteNotEquals(expr: PtBinaryExpression, signed: Boolean) { + private fun byteNotEquals(expr: PtBinaryExpression) { when(expr.right) { is PtNumber -> { val number = (expr.right as PtNumber).number.toInt() - asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed) + asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A) asmgen.out(""" cmp #$number bne + @@ -1061,7 +1061,7 @@ internal class AssignmentAsmGen(private val program: PtProgram, } is PtIdentifier -> { val varname = (expr.right as PtIdentifier).name - asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed) + asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A) asmgen.out(""" cmp $varname bne + @@ -1084,27 +1084,61 @@ internal class AssignmentAsmGen(private val program: PtProgram, } private fun byteLess(expr: PtBinaryExpression, signed: Boolean) { - // TODO no need to use a temporary variable if the right expression is a literal number or a variable name (or register name) when(expr.right) { -// is PtNumber -> { -// TODO("< number") -// } -// is PtIdentifier -> { -// TODO("< variable") -// } + is PtNumber -> { + val number = (expr.right as PtNumber).number.toInt() + asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed) + if(signed) { + asmgen.out(""" + sec + sbc #$number + bvs + + eor #$80 ++ asl a + rol a + and #1 + eor #1""") + } + else + asmgen.out(""" + cmp #$number + asl a + and #1 + eor #1""") + } + is PtIdentifier -> { + val varname = (expr.right as PtIdentifier).name + asmgen.assignExpressionToRegister(expr.left, RegisterOrPair.A, signed) + if(signed) { + asmgen.out(""" + sec + sbc $varname + bvs + + eor #$80 ++ asl a + rol a + and #1 + eor #1""") + } + else + asmgen.out(""" + cmp $varname + rol a + and #1 + eor #1""") + } else -> { asmgen.assignByteOperandsToAAndVar(expr.right, expr.left, "P8ZP_SCRATCH_B1") if(signed) asmgen.out(""" clc sbc P8ZP_SCRATCH_B1 - bvc + + bvs + eor #$80 -+ bpl + - lda #0 - beq ++ -+ lda #1 -+""") ++ asl a + rol a + and #1 + eor #1""") else asmgen.out(""" tay diff --git a/examples/test.p8 b/examples/test.p8 index b1526b5ac..7d2164f19 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -4,21 +4,27 @@ main { sub start() { ubyte a = 1 - ubyte var1 = 1 + ubyte var1 = 2 ubyte var2 = 4 + if a==1 and a<4 { + a++ + } if a==var1 and a4 or a<2 { -; a++ -; } -; -; if a>=2 and a<4 { -; a++ -; } + byte sa = 1 + byte svar1 = 2 + byte svar2 = 4 + if sa==1 and sa<4 { + sa++ + } + if sa==svar1 and sa