diff --git a/compiler/src/prog8/ast/processing/AstVariousTransforms.kt b/compiler/src/prog8/ast/processing/AstVariousTransforms.kt index adc2792a9..0c3fbacdc 100644 --- a/compiler/src/prog8/ast/processing/AstVariousTransforms.kt +++ b/compiler/src/prog8/ast/processing/AstVariousTransforms.kt @@ -8,6 +8,7 @@ import prog8.ast.statements.* internal class AstVariousTransforms(private val program: Program) : AstWalker() { + private val noModifications = emptyList() override fun before(functionCall: FunctionCall, parent: Node): Iterable { if(functionCall.target.nameInSource.size==1 && functionCall.target.nameInSource[0]=="lsb") { @@ -18,7 +19,7 @@ internal class AstVariousTransforms(private val program: Program) : AstWalker() )) } - return emptyList() + return noModifications } override fun before(decl: VarDecl, parent: Node): Iterable { @@ -33,7 +34,7 @@ internal class AstVariousTransforms(private val program: Program) : AstWalker() )) } - return emptyList() + return noModifications } override fun after(subroutine: Subroutine, parent: Node): Iterable { @@ -52,7 +53,7 @@ internal class AstVariousTransforms(private val program: Program) : AstWalker() } } - return emptyList() + return noModifications } override fun before(expr: BinaryExpression, parent: Node): Iterable { @@ -71,7 +72,7 @@ internal class AstVariousTransforms(private val program: Program) : AstWalker() )) } - return emptyList() + return noModifications } override fun after(string: StringLiteralValue, parent: Node): Iterable { @@ -84,7 +85,7 @@ internal class AstVariousTransforms(private val program: Program) : AstWalker() IAstModification.InsertFirst(vardecl, string.definingScope() as Node) ) } - return emptyList() + return noModifications } override fun after(array: ArrayLiteralValue, parent: Node): Iterable { @@ -112,7 +113,7 @@ internal class AstVariousTransforms(private val program: Program) : AstWalker() } } } - return emptyList() + return noModifications } private fun processBinaryExprWithString(string: StringLiteralValue, operand: Expression, expr: BinaryExpression): Expression { diff --git a/compiler/src/prog8/ast/processing/IAstModifyingVisitor.kt b/compiler/src/prog8/ast/processing/IAstModifyingVisitor.kt index 75b3fc791..b51aa54c4 100644 --- a/compiler/src/prog8/ast/processing/IAstModifyingVisitor.kt +++ b/compiler/src/prog8/ast/processing/IAstModifyingVisitor.kt @@ -6,7 +6,7 @@ import prog8.ast.base.FatalAstException import prog8.ast.expressions.* import prog8.ast.statements.* -// TODO replace all occurrences of this with AstWalker +// TODO replace all occurrences of this with AstWalker (currently only ConstantFoldingOptimizer remaining) interface IAstModifyingVisitor { fun visit(program: Program) { program.modules.forEach { it.accept(this) } diff --git a/compiler/src/prog8/ast/processing/ImportedModuleDirectiveRemover.kt b/compiler/src/prog8/ast/processing/ImportedModuleDirectiveRemover.kt index 2b7c90753..8eee50465 100644 --- a/compiler/src/prog8/ast/processing/ImportedModuleDirectiveRemover.kt +++ b/compiler/src/prog8/ast/processing/ImportedModuleDirectiveRemover.kt @@ -10,11 +10,12 @@ internal class ImportedModuleDirectiveRemover: AstWalker() { */ private val moduleLevelDirectives = listOf("%output", "%launcher", "%zeropage", "%zpreserved", "%address") + private val noModifications = emptyList() override fun before(directive: Directive, parent: Node): Iterable { if(directive.directive in moduleLevelDirectives) { return listOf(IAstModification.Remove(directive, parent)) } - return emptyList() + return noModifications } } diff --git a/compiler/src/prog8/ast/processing/StatementReorderer.kt b/compiler/src/prog8/ast/processing/StatementReorderer.kt index 66889bd22..fbeef2b0e 100644 --- a/compiler/src/prog8/ast/processing/StatementReorderer.kt +++ b/compiler/src/prog8/ast/processing/StatementReorderer.kt @@ -19,7 +19,7 @@ internal class StatementReorderer(val program: Program) : AstWalker() { // - sorts the choices in when statement. // - insert AddressOf (&) expression where required (string params to a UWORD function param etc). - + private val noModifications = emptyList() private val directivesToMove = setOf("%output", "%launcher", "%zeropage", "%zpreserved", "%address", "%option") override fun after(module: Module, parent: Node): Iterable { @@ -33,7 +33,7 @@ internal class StatementReorderer(val program: Program) : AstWalker() { } reorderVardeclsAndDirectives(module.statements) - return emptyList() + return noModifications } private fun reorderVardeclsAndDirectives(statements: MutableList) { @@ -56,7 +56,7 @@ internal class StatementReorderer(val program: Program) : AstWalker() { } reorderVardeclsAndDirectives(block.statements) - return emptyList() + return noModifications } override fun before(subroutine: Subroutine, parent: Node): Iterable { @@ -68,7 +68,7 @@ internal class StatementReorderer(val program: Program) : AstWalker() { ) } } - return emptyList() + return noModifications } override fun after(decl: VarDecl, parent: Node): Iterable { @@ -86,7 +86,7 @@ internal class StatementReorderer(val program: Program) : AstWalker() { ) } } - return emptyList() + return noModifications } override fun after(whenStatement: WhenStatement, parent: Node): Iterable { @@ -95,7 +95,7 @@ internal class StatementReorderer(val program: Program) : AstWalker() { } whenStatement.choices.clear() choices.mapTo(whenStatement.choices) { it.second } - return emptyList() + return noModifications } override fun before(assignment: Assignment, parent: Node): Iterable { @@ -119,7 +119,7 @@ internal class StatementReorderer(val program: Program) : AstWalker() { } } - return emptyList() + return noModifications } private fun flattenStructAssignmentFromStructLiteral(structAssignment: Assignment, program: Program): List { diff --git a/compiler/src/prog8/ast/processing/TypecastsAdder.kt b/compiler/src/prog8/ast/processing/TypecastsAdder.kt index 0f4bb3899..ffe7d6e48 100644 --- a/compiler/src/prog8/ast/processing/TypecastsAdder.kt +++ b/compiler/src/prog8/ast/processing/TypecastsAdder.kt @@ -16,6 +16,8 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke * (this includes function call arguments) */ + private val noModifications = emptyList() + override fun after(expr: BinaryExpression, parent: Node): Iterable { val leftDt = expr.left.inferType(program) val rightDt = expr.right.inferType(program) @@ -32,7 +34,7 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke } } } - return emptyList() + return noModifications } override fun after(assignment: Assignment, parent: Node): Iterable { @@ -72,7 +74,7 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke } } } - return emptyList() + return noModifications } override fun after(functionCallStatement: FunctionCallStatement, parent: Node): Iterable { @@ -143,7 +145,7 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke if(typecast.implicit && typecast.type in setOf(DataType.FLOAT, DataType.ARRAY_F)) { errors.warn("byte or word value implicitly converted to float. Suggestion: use explicit cast as float, a float number, or revert to integer arithmetic", typecast.position) } - return emptyList() + return noModifications } override fun after(memread: DirectMemoryRead, parent: Node): Iterable { @@ -154,7 +156,7 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke ?: TypecastExpression(memread.addressExpression, DataType.UWORD, true, memread.addressExpression.position) return listOf(IAstModification.ReplaceNode(memread.addressExpression, typecast, memread)) } - return emptyList() + return noModifications } override fun after(memwrite: DirectMemoryWrite, parent: Node): Iterable { @@ -165,7 +167,7 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke ?: TypecastExpression(memwrite.addressExpression, DataType.UWORD, true, memwrite.addressExpression.position) return listOf(IAstModification.ReplaceNode(memwrite.addressExpression, typecast, memwrite)) } - return emptyList() + return noModifications } override fun after(structLv: StructLiteralValue, parent: Node): Iterable { @@ -210,7 +212,7 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke } } } - return emptyList() + return noModifications } override fun after(returnStmt: Return, parent: Node): Iterable { @@ -221,7 +223,7 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke if(subroutine.returntypes.size==1) { val subReturnType = subroutine.returntypes.first() if (returnValue.inferType(program).istype(subReturnType)) - return emptyList() + return noModifications if (returnValue is NumericLiteralValue) { returnStmt.value = returnValue.cast(subroutine.returntypes.single()) } else { @@ -232,6 +234,6 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke } } } - return emptyList() + return noModifications } } diff --git a/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt b/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt index f1667e255..cbe803c8d 100644 --- a/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt +++ b/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt @@ -11,12 +11,14 @@ import prog8.ast.statements.* internal class BeforeAsmGenerationAstChanger(val program: Program, val errors: ErrorReporter) : AstWalker() { + private val noModifications = emptyList() + override fun after(decl: VarDecl, parent: Node): Iterable { if (decl.value == null && decl.type == VarDeclType.VAR && decl.datatype in NumericDatatypes) { // a numeric vardecl without an initial value is initialized with zero. decl.value = decl.zeroElementValue() } - return emptyList() + return noModifications } override fun after(scope: AnonymousScope, parent: Node): Iterable { @@ -45,7 +47,7 @@ internal class BeforeAsmGenerationAstChanger(val program: Program, val errors: E decls.map { IAstModification.InsertFirst(it, sub) } // move it up to the subroutine } } - return emptyList() + return noModifications } override fun after(subroutine: Subroutine, parent: Node): Iterable { @@ -99,6 +101,6 @@ internal class BeforeAsmGenerationAstChanger(val program: Program, val errors: E } } - return emptyList() + return noModifications } } diff --git a/compiler/src/prog8/optimizer/AssignmentTransformer.kt b/compiler/src/prog8/optimizer/AssignmentTransformer.kt index 32e33fba8..0892c2c28 100644 --- a/compiler/src/prog8/optimizer/AssignmentTransformer.kt +++ b/compiler/src/prog8/optimizer/AssignmentTransformer.kt @@ -14,6 +14,7 @@ import prog8.ast.statements.PostIncrDecr internal class AssignmentTransformer(val program: Program, val errors: ErrorReporter) : AstWalker() { var optimizationsDone: Int = 0 + private val noModifications = emptyList() override fun before(assignment: Assignment, parent: Node): Iterable { // modify A = A + 5 back into augmented form A += 5 for easier code generation for optimized in-place assignments @@ -27,7 +28,7 @@ internal class AssignmentTransformer(val program: Program, val errors: ErrorRepo assignment.aug_op = binExpr.operator + "=" assignment.value.parent = assignment optimizationsDone++ - return emptyList() + return noModifications } } assignment.aug_op = "setvalue" @@ -151,6 +152,6 @@ internal class AssignmentTransformer(val program: Program, val errors: ErrorRepo } } } - return emptyList() + return noModifications } } diff --git a/compiler/src/prog8/optimizer/ExpressionSimplifier.kt b/compiler/src/prog8/optimizer/ExpressionSimplifier.kt index 9e1559373..b51674066 100644 --- a/compiler/src/prog8/optimizer/ExpressionSimplifier.kt +++ b/compiler/src/prog8/optimizer/ExpressionSimplifier.kt @@ -22,11 +22,12 @@ import kotlin.math.pow internal class ExpressionSimplifier(private val program: Program) : AstWalker() { private val powersOfTwo = (1..16).map { (2.0).pow(it) }.toSet() private val negativePowersOfTwo = powersOfTwo.map { -it }.toSet() + private val noModifications = emptyList() override fun after(assignment: Assignment, parent: Node): Iterable { if (assignment.aug_op != null) throw FatalAstException("augmented assignments should have been converted to normal assignments before this optimizer: $assignment") - return emptyList() + return noModifications } override fun after(typecast: TypecastExpression, parent: Node): Iterable { @@ -82,10 +83,10 @@ internal class ExpressionSimplifier(private val program: Program) : AstWalker() if (newExpr != null) return listOf(IAstModification.ReplaceNode(expr, newExpr, parent)) } - else -> return emptyList() + else -> return noModifications } } - return emptyList() + return noModifications } override fun after(expr: BinaryExpression, parent: Node): Iterable { @@ -297,7 +298,7 @@ internal class ExpressionSimplifier(private val program: Program) : AstWalker() if(newExpr != null) return listOf(IAstModification.ReplaceNode(expr, newExpr, parent)) - return emptyList() + return noModifications } private fun determineY(x: Expression, subBinExpr: BinaryExpression): Expression? { diff --git a/compiler/src/prog8/optimizer/StatementOptimizer.kt b/compiler/src/prog8/optimizer/StatementOptimizer.kt index a40d22571..9d4fc1691 100644 --- a/compiler/src/prog8/optimizer/StatementOptimizer.kt +++ b/compiler/src/prog8/optimizer/StatementOptimizer.kt @@ -23,6 +23,7 @@ import kotlin.math.floor internal class StatementOptimizer(private val program: Program, private val errors: ErrorReporter) : AstWalker() { + private val noModifications = emptyList() private val callgraph = CallGraph(program) private val pureBuiltinFunctions = BuiltinFunctions.filter { it.value.pure } @@ -38,7 +39,7 @@ internal class StatementOptimizer(private val program: Program, return listOf(IAstModification.Remove(block, parent)) } } - return emptyList() + return noModifications } override fun after(subroutine: Subroutine, parent: Node): Iterable { @@ -60,7 +61,7 @@ internal class StatementOptimizer(private val program: Program, return listOf(IAstModification.Remove(subroutine, parent)) } - return emptyList() + return noModifications } override fun after(scope: AnonymousScope, parent: Node): Iterable { @@ -77,7 +78,7 @@ internal class StatementOptimizer(private val program: Program, return listOf(IAstModification.Remove(decl, parent)) } - return emptyList() + return noModifications } override fun after(functionCallStatement: FunctionCallStatement, parent: Node): Iterable { @@ -139,7 +140,7 @@ internal class StatementOptimizer(private val program: Program, return listOf(IAstModification.Remove(functionCallStatement, parent)) } - return emptyList() + return noModifications } override fun before(functionCall: FunctionCall, parent: Node): Iterable { @@ -153,7 +154,7 @@ internal class StatementOptimizer(private val program: Program, return listOf(IAstModification.ReplaceNode(functionCall, constval, parent)) } } - return emptyList() + return noModifications } override fun after(ifStatement: IfStatement, parent: Node): Iterable { @@ -186,7 +187,7 @@ internal class StatementOptimizer(private val program: Program, } } - return emptyList() + return noModifications } override fun after(forLoop: ForLoop, parent: Node): Iterable { @@ -244,7 +245,7 @@ internal class StatementOptimizer(private val program: Program, } } - return emptyList() + return noModifications } override fun before(repeatLoop: RepeatLoop, parent: Node): Iterable { @@ -261,7 +262,7 @@ internal class StatementOptimizer(private val program: Program, return listOf(IAstModification.ReplaceNode(repeatLoop, forever, parent)) } } - return emptyList() + return noModifications } override fun before(whileLoop: WhileLoop, parent: Node): Iterable { @@ -277,7 +278,7 @@ internal class StatementOptimizer(private val program: Program, listOf(IAstModification.Remove(whileLoop, parent)) } } - return emptyList() + return noModifications } override fun after(whenStatement: WhenStatement, parent: Node): Iterable { @@ -299,7 +300,7 @@ internal class StatementOptimizer(private val program: Program, if(label!=null && scope.statements.indexOf(label) == scope.statements.indexOf(jump)+1) return listOf(IAstModification.Remove(jump, parent)) - return emptyList() + return noModifications } override fun after(assignment: Assignment, parent: Node): Iterable { @@ -391,7 +392,7 @@ internal class StatementOptimizer(private val program: Program, } } - return emptyList() + return noModifications } private fun deduplicateAssignments(statements: List): MutableList {