1
0
mirror of https://github.com/KarolS/millfork.git synced 2024-11-17 16:05:31 +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)
}
}
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 {
case NoBranching => return Nil
case BranchIfTrue(l) => compType -> l
@ -523,6 +523,14 @@ object BuiltIns {
case ComparisonType.LessSigned =>
if (comparingAgainstZero) List(AssemblyLine.relative(BMI, label)) else {
maybeConstant match {
case Some(NumericConstant(n@(1 | 2 | 4 | 8 | 16 | 32 | 64), _)) =>
secondParamCompiled = Nil
List(
AssemblyLine.immediate(AND, 255 ^ (n - 1)),
AssemblyLine.relative(BEQ, label),
AssemblyLine.relative(BMI, label))
case _ =>
val fixup = ctx.nextLabel("co")
List(
AssemblyLine.relative(BVC, fixup),
@ -530,19 +538,39 @@ object BuiltIns {
AssemblyLine.label(fixup),
AssemblyLine.relative(BMI, label))
}
}
case ComparisonType.GreaterOrEqualSigned =>
if (comparingAgainstZero) List(AssemblyLine.relative(BPL, label)) else {
maybeConstant match {
case Some(NumericConstant(n@(1 | 2 | 4 | 8 | 16 | 32 | 64), _)) =>
secondParamCompiled = Nil
val x = ctx.nextLabel("co")
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 =>
if (comparingAgainstZero) {
List(AssemblyLine.relative(BEQ, label),
AssemblyLine.relative(BMI, label))
} else {
maybeConstant match {
case Some(NumericConstant(n@(0 | 1 | 3 | 7 | 15 | 31 | 63), _)) =>
secondParamCompiled = Nil
List(
AssemblyLine.immediate(AND, 255 ^ n),
AssemblyLine.relative(BEQ, label),
AssemblyLine.relative(BMI, label))
case _ =>
val fixup = ctx.nextLabel("co")
List(AssemblyLine.relative(BVC, fixup),
AssemblyLine.immediate(EOR, 0x80),
@ -550,6 +578,7 @@ object BuiltIns {
AssemblyLine.relative(BMI, label),
AssemblyLine.relative(BEQ, label))
}
}
case ComparisonType.GreaterSigned =>
if (comparingAgainstZero) {
val x = ctx.nextLabel("co")
@ -557,6 +586,16 @@ object BuiltIns {
AssemblyLine.relative(BPL, label),
AssemblyLine.label(x))
} else {
maybeConstant match {
case Some(NumericConstant(n@(0 | 1 | 3 | 7 | 15 | 31 | 63), _)) =>
secondParamCompiled = Nil
val x = ctx.nextLabel("co")
List(
AssemblyLine.immediate(AND, 255 ^ n),
AssemblyLine.relative(BEQ, x),
AssemblyLine.relative(BPL, label),
AssemblyLine.label(x))
case _ =>
val fixup = ctx.nextLabel("co")
val x = ctx.nextLabel("co")
List(
@ -568,6 +607,7 @@ object BuiltIns {
AssemblyLine.label(x))
}
}
}
firstParamCompiled ++ secondParamCompiled ++ branchingCompiled
}