From 348351534628c1a8dafd6d31c9675f4ec6eca07d Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 12 Nov 2021 23:23:51 +0100 Subject: [PATCH] preparing for more optimizations --- .../compiler/astprocessing/VariousCleanups.kt | 19 ++++++--------- compiler/test/TestOptimization.kt | 24 +++++++++++++++++-- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt index 576ff6f06..c30808428 100644 --- a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt +++ b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt @@ -74,18 +74,13 @@ internal class VariousCleanups(val program: Program, val errors: IErrorReporter) if(sourceDt istype typecast.type) return listOf(IAstModification.ReplaceNode(typecast, typecast.expression, parent)) - // TODO finish this; get rid of redundant final typecast to variable's type if they are the same -// if(parent is Assignment) { -// val targetDt = (parent).target.inferType(program).getOrElse { throw FatalAstException("invalid dt") } -// if(sourceDt istype targetDt) { -// // we can get rid of this typecast because the type is already -// // TODO REMOVE IT -// println("TYPECAST IN ASSIGNMENT $parent") // TODO DEBUG -// } else { -// // TODO -// println("${typecast.expression} : source=$sourceDt to $targetDt") -// } -// } + if(parent is Assignment) { + val targetDt = (parent).target.inferType(program).getOrElse { throw FatalAstException("invalid dt") } + if(sourceDt istype targetDt) { + // we can get rid of this typecast because the type is already + return listOf(IAstModification.ReplaceNode(typecast, typecast.expression, parent)) + } + } return noModifications } diff --git a/compiler/test/TestOptimization.kt b/compiler/test/TestOptimization.kt index 7c95112d9..32ff1f660 100644 --- a/compiler/test/TestOptimization.kt +++ b/compiler/test/TestOptimization.kt @@ -14,6 +14,7 @@ import prog8.ast.base.Position import prog8.ast.expressions.* import prog8.ast.statements.* import prog8.compiler.BeforeAsmGenerationAstChanger +import prog8.compiler.printAst import prog8.compiler.target.C64Target import prog8.compilerinterface.* import prog8tests.helpers.DummyFunctions @@ -128,6 +129,27 @@ class TestOptimization: FunSpec({ (initY2.value as NumericLiteralValue).number.toDouble() shouldBe 11.0 } + test("typecasted assignment from ubyte logical expressoin to uword var") { + val src = """ + main { + sub start() { + ubyte bb + uword ww + ww = not bb or not ww ; expression combining ubyte and uword + } + } + """ + val result = compileText(C64Target, false, src, writeAssembly = false).assertSuccess() + + // ww = ((( not bb as uword) or not ww) as uword) + val wwAssign = result.program.entrypoint.statements.last() as Assignment + val expr = wwAssign.value as TypecastExpression + + wwAssign.target.identifier?.nameInSource shouldBe listOf("ww") + expr.type shouldBe DataType.UWORD + expr.expression.inferType(result.program).istype(DataType.UBYTE) shouldBe true + } + test("intermediate assignment steps have correct types for codegen phase (BeforeAsmGenerationAstChanger)") { val src = """ main { @@ -168,8 +190,6 @@ class TestOptimization: FunSpec({ val assigns = result.program.entrypoint.statements.filterIsInstance() val bbAssigns = assigns.filter { it.value !is NumericLiteralValue } bbAssigns.size shouldBe 2 - println(bbAssigns[0]) - println(bbAssigns[1]) bbAssigns[0].target.identifier!!.nameInSource shouldBe listOf("bb") bbAssigns[0].value shouldBe instanceOf()