From 749defd05add8a906cba9069a19598db61a7a52d Mon Sep 17 00:00:00 2001 From: Karol Stasiak Date: Tue, 24 Jul 2018 23:14:09 +0200 Subject: [PATCH] Decimal subtraction fixes --- src/main/scala/millfork/compiler/mos/BuiltIns.scala | 3 ++- src/main/scala/millfork/env/Constant.scala | 8 +++++++- src/main/scala/millfork/parser/MfParser.scala | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/scala/millfork/compiler/mos/BuiltIns.scala b/src/main/scala/millfork/compiler/mos/BuiltIns.scala index 5f42ac95..18659fb0 100644 --- a/src/main/scala/millfork/compiler/mos/BuiltIns.scala +++ b/src/main/scala/millfork/compiler/mos/BuiltIns.scala @@ -825,6 +825,7 @@ object BuiltIns { if (l.+(1).quickSimplify == h) { return List(AssemblyLine.zeropage(INC_W, l)) } + case _ => } } if (ctx.options.flags(CompilationFlag.EmitNative65816Opcodes)) { @@ -1016,7 +1017,7 @@ object BuiltIns { ??? } buffer ++= staTo(LDA, targetBytes(i)) - buffer ++= ldTo(SBC, addendByteRead(i)) + buffer ++= wrapInSedCldIfNeeded(decimal, ldTo(SBC, addendByteRead(i))) buffer ++= targetBytes(i) } else { if (i >= addendSize) { diff --git a/src/main/scala/millfork/env/Constant.scala b/src/main/scala/millfork/env/Constant.scala index 7aae9d87..359669ef 100644 --- a/src/main/scala/millfork/env/Constant.scala +++ b/src/main/scala/millfork/env/Constant.scala @@ -232,6 +232,9 @@ case class CompoundConstant(operator: MathOperator.Value, lhs: Constant, rhs: Co case (_, CompoundConstant(MathOperator.Plus, a, b)) if operator == MathOperator.Minus => ((l - a) - b).quickSimplify case (CompoundConstant(MathOperator.Shl, SubbyteConstant(c1, 1), NumericConstant(8, _)), SubbyteConstant(c2, 0)) if operator == MathOperator.Or && c1 == c2 => c1 + + case (_, CompoundConstant(MathOperator.DecimalMinus, a, b)) if operator == MathOperator.DecimalPlus => + CompoundConstant(MathOperator.DecimalMinus, CompoundConstant(MathOperator.DecimalPlus, l, a), b).quickSimplify case (NumericConstant(lv, ls), NumericConstant(rv, rs)) => var size = ls max rs val value = operator match { @@ -247,7 +250,9 @@ case class CompoundConstant(operator: MathOperator.Value, lhs: Constant, rhs: Co case MathOperator.Or => lv | rv case MathOperator.And => lv & rv case MathOperator.DecimalPlus if ls == 1 && rs == 1 => - asDecimal(lv, rv, _ + _) & 0xff + asDecimal(lv & 0xff, rv & 0xff, _ + _) & 0xff + case MathOperator.DecimalMinus if ls == 1 && rs == 1 && lv.&(0xff) >= rv.&(0xff) => + asDecimal(lv & 0xff, rv & 0xff, _ - _) & 0xff case _ => return this } operator match { @@ -285,6 +290,7 @@ case class CompoundConstant(operator: MathOperator.Value, lhs: Constant, rhs: Co case MathOperator.DecimalPlus => c case MathOperator.DecimalPlus9 => c case MathOperator.Minus => c + case MathOperator.DecimalMinus => c case MathOperator.Times => Constant.Zero case MathOperator.Shl => c case MathOperator.Shr => c diff --git a/src/main/scala/millfork/parser/MfParser.scala b/src/main/scala/millfork/parser/MfParser.scala index 16b3b54d..280ba551 100644 --- a/src/main/scala/millfork/parser/MfParser.scala +++ b/src/main/scala/millfork/parser/MfParser.scala @@ -222,7 +222,7 @@ abstract class MfParser[T](fileId: String, input: String, currentDirectory: Stri case List("+") | List("-") | List("+", "-") | List("-", "+") => SumExpression(xs.toPairList("+").map { case (op, value) => (op == "-", p(value, level + 1)) }, decimal = false) case List("+'") | List("-'") | List("+'", "-'") | List("-'", "+'") => - SumExpression(xs.toPairList("+").map { case (op, value) => (op == "-", p(value, level + 1)) }, decimal = true) + SumExpression(xs.toPairList("+").map { case (op, value) => (op == "-'", p(value, level + 1)) }, decimal = true) case List(":") => if (xs.size != 2) { ErrorReporting.error("The `:` operator can have only two arguments", xs.head.head.position)