From b5523c7077fb79a169ef5d8fee0b7957a77c6445 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 16 Mar 2021 00:33:15 +0100 Subject: [PATCH] don't optimize with inlining too aggressively (code bloat) --- compiler/res/prog8lib/prog8_lib.asm | 13 ++++ .../cpu6502/codegen/ExpressionsAsmGen.kt | 61 ++++++++++++++++--- examples/test.p8 | 6 ++ 3 files changed, 73 insertions(+), 7 deletions(-) diff --git a/compiler/res/prog8lib/prog8_lib.asm b/compiler/res/prog8lib/prog8_lib.asm index d7ed29798..4ba193cc7 100644 --- a/compiler/res/prog8lib/prog8_lib.asm +++ b/compiler/res/prog8lib/prog8_lib.asm @@ -442,6 +442,19 @@ less_b .proc bpl equal_b._equal_b_false .pend +reg_less_uw .proc + ; AY < P8ZP_SCRATCH_W2? + cpy P8ZP_SCRATCH_W2+1 + bcc _true + bne _false + cmp P8ZP_SCRATCH_W2 + bcc _true +_false lda #0 + rts +_true lda #1 + rts + .pend + less_uw .proc lda P8ESTACK_HI+2,x cmp P8ESTACK_HI+1,x diff --git a/compiler/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt b/compiler/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt index d980cb44e..86fc3f074 100644 --- a/compiler/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/cpu6502/codegen/ExpressionsAsmGen.kt @@ -497,7 +497,7 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_W2", DataType.UWORD, null) asmgen.assignExpressionToRegister(left, RegisterOrPair.AY) - return code("P8ZP_SCRATCH_W2+1", "P8ZP_SCRATCH_W2") + return asmgen.out(" jsr prog8_lib.reg_less_uw | beq $jumpIfFalseLabel") } private fun translateWordLessJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) { @@ -526,7 +526,13 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge } } - // TODO optimize this so it doesn't call a comparison-subroutine reg_less_w + if(right is IdentifierReference) { + // TODO optimize comparison against identifier +// asmgen.assignExpressionToRegister(left, RegisterOrPair.AY) +// val varname = asmgen.asmVariableName(right) +// return code("$varname+1", varname) + } + asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_W2", DataType.UWORD, null) asmgen.assignExpressionToRegister(left, RegisterOrPair.AY) return asmgen.out(" jsr prog8_lib.reg_less_w | beq $jumpIfFalseLabel") @@ -656,6 +662,17 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge } private fun translateWordGreaterJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) { + + fun code(msbCpyOperand: String, lsbCmpOperand: String) { + asmgen.out(""" + cmp $lsbCmpOperand + tya + sbc $msbCpyOperand + bvc + + eor #${'$'}80 ++ bpl $jumpIfFalseLabel""") + } + if(rightConstVal!=null) { if(leftConstVal!=null) { if(rightConstVal<=leftConstVal) @@ -685,7 +702,13 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge } } - // TODO optimize this so it doesn't call a comparison-subroutine reg_less_w + if(right is IdentifierReference) { + // TODO optimize comparison against identifier +// asmgen.assignExpressionToRegister(left, RegisterOrPair.AY) +// val varname = asmgen.asmVariableName(right) +// return code("$varname+1", varname) + } + asmgen.assignExpressionToVariable(left, "P8ZP_SCRATCH_W2", DataType.UWORD, null) asmgen.assignExpressionToRegister(right, RegisterOrPair.AY) return asmgen.out(" jsr prog8_lib.reg_less_w | beq $jumpIfFalseLabel") @@ -797,7 +820,13 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge } } - // TODO optimize this so it doesn't call a comparison-subroutine reg_lesseq_uw + if(right is IdentifierReference) { + // TODO optimize comparison against identifier +// asmgen.assignExpressionToRegister(left, RegisterOrPair.AY) +// val varname = asmgen.asmVariableName(right) +// return code("$varname+1", varname) + } + asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_W2", DataType.UWORD, null) asmgen.assignExpressionToRegister(left, RegisterOrPair.AY) return asmgen.out(" jsr prog8_lib.reg_lesseq_uw | beq $jumpIfFalseLabel") @@ -833,7 +862,13 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge } } - // TODO optimize this so it doesn't call a comparison-subroutine reg_lesseq_w + if(right is IdentifierReference) { + // TODO optimize comparison against identifier +// asmgen.assignExpressionToRegister(left, RegisterOrPair.AY) +// val varname = asmgen.asmVariableName(right) +// return code("$varname+1", varname) + } + asmgen.assignExpressionToVariable(left, "P8ZP_SCRATCH_W2", DataType.UWORD, null) asmgen.assignExpressionToRegister(right, RegisterOrPair.AY) return asmgen.out(" jsr prog8_lib.reg_lesseq_w | beq $jumpIfFalseLabel") @@ -934,7 +969,13 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge } } - // TODO optimize this so it doesn't call a comparison-subroutine reg_lesseq_uw + if(right is IdentifierReference) { + // TODO optimize comparison against identifier +// asmgen.assignExpressionToRegister(left, RegisterOrPair.AY) +// val varname = asmgen.asmVariableName(right) +// return code("$varname+1", varname) + } + asmgen.assignExpressionToVariable(left, "P8ZP_SCRATCH_W2", DataType.UWORD, null) asmgen.assignExpressionToRegister(right, RegisterOrPair.AY) return asmgen.out(" jsr prog8_lib.reg_lesseq_uw | beq $jumpIfFalseLabel") @@ -964,7 +1005,13 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge } } - // TODO optimize this so it doesn't call a comparison-subroutine reg_lesseq_w + if(right is IdentifierReference) { + // TODO optimize comparison against identifier +// asmgen.assignExpressionToRegister(left, RegisterOrPair.AY) +// val varname = asmgen.asmVariableName(right) +// return code("$varname+1", varname) + } + asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_W2", DataType.UWORD, null) asmgen.assignExpressionToRegister(left, RegisterOrPair.AY) return asmgen.out(" jsr prog8_lib.reg_lesseq_w | beq $jumpIfFalseLabel") diff --git a/examples/test.p8 b/examples/test.p8 index 7761006d1..c06a780d9 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -46,6 +46,12 @@ main { } sub start() { + uword uw1 + uword uw2 + + if uw1