diff --git a/src/main/scala/millfork/compiler/BuiltIns.scala b/src/main/scala/millfork/compiler/BuiltIns.scala index 7ba63955..fb99542a 100644 --- a/src/main/scala/millfork/compiler/BuiltIns.scala +++ b/src/main/scala/millfork/compiler/BuiltIns.scala @@ -290,6 +290,9 @@ object BuiltIns { def compileByteComparison(ctx: CompilationContext, compType: ComparisonType.Value, lhs: Expression, rhs: Expression, branches: BranchSpec): List[AssemblyLine] = { val env = ctx.env val b = env.get[Type]("byte") + if (simplicity(env, lhs) >= 'J' && simplicity(env, rhs) < 'J') { + return compileByteComparison(ctx, ComparisonType.flip(compType), rhs, lhs, branches) + } val firstParamCompiled = MlCompiler.compile(ctx, lhs, Some(b -> RegisterVariable(Register.A, b)), NoBranching) val maybeConstant = env.eval(rhs) maybeConstant match { diff --git a/src/main/scala/millfork/compiler/MfCompiler.scala b/src/main/scala/millfork/compiler/MfCompiler.scala index 67bd2429..fa930bd4 100644 --- a/src/main/scala/millfork/compiler/MfCompiler.scala +++ b/src/main/scala/millfork/compiler/MfCompiler.scala @@ -1157,6 +1157,24 @@ object MlCompiler { case List(_) | Nil => ErrorReporting.fatal("") case _ => + params.tail.init.foreach { e => + if (ctx.env.eval(e).isEmpty) e match { + case VariableExpression(_) => + case LiteralExpression(_, _) => + case IndexedExpression(_, VariableExpression(_)) => + case IndexedExpression(_, LiteralExpression(_, _)) => + case IndexedExpression(_, SumExpression(List( + (_, LiteralExpression(_, _)), + (false, VariableExpression(_)) + ), false)) => + case IndexedExpression(_, SumExpression(List( + (false, VariableExpression(_)), + (_, LiteralExpression(_, _)) + ), false)) => + case _ => + ErrorReporting.warn("A complex expression may be evaluated multiple times", ctx.options, e.position) + } + } val conjunction = params.init.zip(params.tail).map { case (l, r) => FunctionCallExpression(operator, List(l, r)) }.reduceLeft((a, b) => FunctionCallExpression("&&", List(a, b))) diff --git a/src/test/scala/millfork/test/ComparisonSuite.scala b/src/test/scala/millfork/test/ComparisonSuite.scala index 43f5506c..806e4f8a 100644 --- a/src/test/scala/millfork/test/ComparisonSuite.scala +++ b/src/test/scala/millfork/test/ComparisonSuite.scala @@ -293,4 +293,23 @@ class ComparisonSuite extends FunSuite with Matchers { | } """.stripMargin)(_.readWord(0xc000) should equal(6)) } + + test("Warnings") { + EmuBenchmarkRun( + """ + | byte output @$c000 + | void main () { + | output = 5 + | if 2 <= three() <= 4 { + | output += 1 + | } + | if 2 <= three() <= 2 { + | output += 78 + | } + | } + | byte three() { + | return 3 + | } + """.stripMargin)(_.readWord(0xc000) should equal(6)) + } }