1
0
mirror of https://github.com/KarolS/millfork.git synced 2024-12-25 21:29:25 +00:00

6502: optimize signed comparisons against certain constants

This commit is contained in:
Karol Stasiak 2019-05-29 14:08:42 +02:00
parent 138dcfa19f
commit 43686e0c33

View File

@ -469,7 +469,7 @@ object BuiltIns {
simpleOperation(cmpOp, ctx, rhs, IndexChoice.PreferY, preserveA = true, commutative = false) simpleOperation(cmpOp, ctx, rhs, IndexChoice.PreferY, preserveA = true, commutative = false)
} }
} }
val secondParamCompiled = if(cmpOp == SBC && !comparingAgainstZero) AssemblyLine.implied(SEC) :: secondParamCompiled0 else secondParamCompiled0 var secondParamCompiled = if(cmpOp == SBC && !comparingAgainstZero) AssemblyLine.implied(SEC) :: secondParamCompiled0 else secondParamCompiled0
val (effectiveComparisonType, label) = branches match { val (effectiveComparisonType, label) = branches match {
case NoBranching => return Nil case NoBranching => return Nil
case BranchIfTrue(l) => compType -> l case BranchIfTrue(l) => compType -> l
@ -523,32 +523,61 @@ object BuiltIns {
case ComparisonType.LessSigned => case ComparisonType.LessSigned =>
if (comparingAgainstZero) List(AssemblyLine.relative(BMI, label)) else { if (comparingAgainstZero) List(AssemblyLine.relative(BMI, label)) else {
val fixup = ctx.nextLabel("co") maybeConstant match {
List( case Some(NumericConstant(n@(1 | 2 | 4 | 8 | 16 | 32 | 64), _)) =>
AssemblyLine.relative(BVC, fixup), secondParamCompiled = Nil
AssemblyLine.immediate(EOR, 0x80), List(
AssemblyLine.label(fixup), AssemblyLine.immediate(AND, 255 ^ (n - 1)),
AssemblyLine.relative(BMI, label)) AssemblyLine.relative(BEQ, label),
AssemblyLine.relative(BMI, label))
case _ =>
val fixup = ctx.nextLabel("co")
List(
AssemblyLine.relative(BVC, fixup),
AssemblyLine.immediate(EOR, 0x80),
AssemblyLine.label(fixup),
AssemblyLine.relative(BMI, label))
}
} }
case ComparisonType.GreaterOrEqualSigned => case ComparisonType.GreaterOrEqualSigned =>
if (comparingAgainstZero) List(AssemblyLine.relative(BPL, label)) else { if (comparingAgainstZero) List(AssemblyLine.relative(BPL, label)) else {
val fixup = ctx.nextLabel("co") maybeConstant match {
List( case Some(NumericConstant(n@(1 | 2 | 4 | 8 | 16 | 32 | 64), _)) =>
AssemblyLine.relative(BVC, fixup), secondParamCompiled = Nil
AssemblyLine.immediate(EOR, 0x80), val x = ctx.nextLabel("co")
AssemblyLine.label(fixup), AssemblyLine.relative(BPL, label)) List(
AssemblyLine.immediate(AND, 255 ^ (n - 1)),
AssemblyLine.relative(BEQ, x),
AssemblyLine.relative(BPL, label),
AssemblyLine.label(x))
case _ =>
val fixup = ctx.nextLabel("co")
List(
AssemblyLine.relative(BVC, fixup),
AssemblyLine.immediate(EOR, 0x80),
AssemblyLine.label(fixup), AssemblyLine.relative(BPL, label))
}
} }
case ComparisonType.LessOrEqualSigned => case ComparisonType.LessOrEqualSigned =>
if (comparingAgainstZero) { if (comparingAgainstZero) {
List(AssemblyLine.relative(BEQ, label), List(AssemblyLine.relative(BEQ, label),
AssemblyLine.relative(BMI, label)) AssemblyLine.relative(BMI, label))
} else { } else {
val fixup = ctx.nextLabel("co") maybeConstant match {
List(AssemblyLine.relative(BVC, fixup), case Some(NumericConstant(n@(0 | 1 | 3 | 7 | 15 | 31 | 63), _)) =>
AssemblyLine.immediate(EOR, 0x80), secondParamCompiled = Nil
AssemblyLine.label(fixup), List(
AssemblyLine.relative(BMI, label), AssemblyLine.immediate(AND, 255 ^ n),
AssemblyLine.relative(BEQ, label)) AssemblyLine.relative(BEQ, label),
AssemblyLine.relative(BMI, label))
case _ =>
val fixup = ctx.nextLabel("co")
List(AssemblyLine.relative(BVC, fixup),
AssemblyLine.immediate(EOR, 0x80),
AssemblyLine.label(fixup),
AssemblyLine.relative(BMI, label),
AssemblyLine.relative(BEQ, label))
}
} }
case ComparisonType.GreaterSigned => case ComparisonType.GreaterSigned =>
if (comparingAgainstZero) { if (comparingAgainstZero) {
@ -557,15 +586,26 @@ object BuiltIns {
AssemblyLine.relative(BPL, label), AssemblyLine.relative(BPL, label),
AssemblyLine.label(x)) AssemblyLine.label(x))
} else { } else {
val fixup = ctx.nextLabel("co") maybeConstant match {
val x = ctx.nextLabel("co") case Some(NumericConstant(n@(0 | 1 | 3 | 7 | 15 | 31 | 63), _)) =>
List( secondParamCompiled = Nil
AssemblyLine.relative(BVC, fixup), val x = ctx.nextLabel("co")
AssemblyLine.immediate(EOR, 0x80), List(
AssemblyLine.label(fixup), AssemblyLine.immediate(AND, 255 ^ n),
AssemblyLine.relative(BEQ, x), AssemblyLine.relative(BEQ, x),
AssemblyLine.relative(BPL, label), AssemblyLine.relative(BPL, label),
AssemblyLine.label(x)) AssemblyLine.label(x))
case _ =>
val fixup = ctx.nextLabel("co")
val x = ctx.nextLabel("co")
List(
AssemblyLine.relative(BVC, fixup),
AssemblyLine.immediate(EOR, 0x80),
AssemblyLine.label(fixup),
AssemblyLine.relative(BEQ, x),
AssemblyLine.relative(BPL, label),
AssemblyLine.label(x))
}
} }
} }
firstParamCompiled ++ secondParamCompiled ++ branchingCompiled firstParamCompiled ++ secondParamCompiled ++ branchingCompiled