From 961bcdb7ae00ae4298c72ca1945c5ca6dc176a54 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 15 May 2020 00:24:25 +0200 Subject: [PATCH] some more todo's noted down --- compiler/src/prog8/ast/processing/AstChecker.kt | 5 +++-- .../src/prog8/ast/processing/AstIdentifiersChecker.kt | 2 +- .../src/prog8/ast/processing/IAstModifyingVisitor.kt | 2 +- .../src/prog8/optimizer/ConstantFoldingOptimizer.kt | 3 ++- compiler/src/prog8/optimizer/StatementOptimizer.kt | 11 ++++++----- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/compiler/src/prog8/ast/processing/AstChecker.kt b/compiler/src/prog8/ast/processing/AstChecker.kt index a9efe2732..2018cfc62 100644 --- a/compiler/src/prog8/ast/processing/AstChecker.kt +++ b/compiler/src/prog8/ast/processing/AstChecker.kt @@ -326,7 +326,7 @@ internal class AstChecker(private val program: Program, } override fun visit(repeatLoop: RepeatLoop) { - if(repeatLoop.untilCondition.referencesIdentifiers("A", "X", "Y")) + if(repeatLoop.untilCondition.referencesIdentifiers("A", "X", "Y")) // TODO use callgraph? errors.warn("using a register in the loop condition is risky (it could get clobbered)", repeatLoop.untilCondition.position) if(repeatLoop.untilCondition.inferType(program).typeOrElse(DataType.STRUCT) !in IntegerDatatypes) errors.err("condition value should be an integer type", repeatLoop.untilCondition.position) @@ -334,7 +334,7 @@ internal class AstChecker(private val program: Program, } override fun visit(whileLoop: WhileLoop) { - if(whileLoop.condition.referencesIdentifiers("A", "X", "Y")) + if(whileLoop.condition.referencesIdentifiers("A", "X", "Y")) // TODO use callgraph? errors.warn("using a register in the loop condition is risky (it could get clobbered)", whileLoop.condition.position) if(whileLoop.condition.inferType(program).typeOrElse(DataType.STRUCT) !in IntegerDatatypes) errors.err("condition value should be an integer type", whileLoop.condition.position) @@ -466,6 +466,7 @@ internal class AstChecker(private val program: Program, } // the initializer value can't refer to the variable itself (recursive definition) + // TODO use callgraph for check? if(decl.value?.referencesIdentifiers(decl.name) == true || decl.arraysize?.index?.referencesIdentifiers(decl.name) == true) { err("recursive var declaration") } diff --git a/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt b/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt index 2dfdad08f..f7ea775e1 100644 --- a/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt +++ b/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt @@ -10,7 +10,7 @@ import prog8.ast.statements.* import prog8.compiler.target.CompilationTarget import prog8.functions.BuiltinFunctions - +// TODO implement using AstWalker instead of IAstModifyingVisitor internal class AstIdentifiersChecker(private val program: Program, private val errors: ErrorReporter) : IAstModifyingVisitor { private var blocks = mutableMapOf() diff --git a/compiler/src/prog8/ast/processing/IAstModifyingVisitor.kt b/compiler/src/prog8/ast/processing/IAstModifyingVisitor.kt index 0bd1ea5cd..75b3fc791 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 interface IAstModifyingVisitor { fun visit(program: Program) { program.modules.forEach { it.accept(this) } diff --git a/compiler/src/prog8/optimizer/ConstantFoldingOptimizer.kt b/compiler/src/prog8/optimizer/ConstantFoldingOptimizer.kt index 39018b79a..9ef66603f 100644 --- a/compiler/src/prog8/optimizer/ConstantFoldingOptimizer.kt +++ b/compiler/src/prog8/optimizer/ConstantFoldingOptimizer.kt @@ -10,12 +10,13 @@ import prog8.compiler.target.CompilationTarget import prog8.functions.BuiltinFunctions +// TODO implement using AstWalker instead of IAstModifyingVisitor internal class ConstantFoldingOptimizer(private val program: Program, private val errors: ErrorReporter) : IAstModifyingVisitor { var optimizationsDone: Int = 0 override fun visit(decl: VarDecl): Statement { // the initializer value can't refer to the variable itself (recursive definition) - // TODO: use call tree for this? + // TODO: use call graph for this? if(decl.value?.referencesIdentifiers(decl.name) == true || decl.arraysize?.index?.referencesIdentifiers(decl.name) == true) { errors.err("recursive var declaration", decl.position) return decl diff --git a/compiler/src/prog8/optimizer/StatementOptimizer.kt b/compiler/src/prog8/optimizer/StatementOptimizer.kt index c9f3c5b38..38511febf 100644 --- a/compiler/src/prog8/optimizer/StatementOptimizer.kt +++ b/compiler/src/prog8/optimizer/StatementOptimizer.kt @@ -5,6 +5,7 @@ import prog8.ast.Program import prog8.ast.base.* import prog8.ast.expressions.* import prog8.ast.processing.IAstModifyingVisitor +import prog8.ast.processing.IAstVisitor import prog8.ast.statements.* import prog8.compiler.target.CompilationTarget import prog8.functions.BuiltinFunctions @@ -17,6 +18,7 @@ import kotlin.math.floor */ +// TODO implement using AstWalker instead of IAstModifyingVisitor internal class StatementOptimizer(private val program: Program, private val errors: ErrorReporter) : IAstModifyingVisitor { var optimizationsDone: Int = 0 @@ -317,20 +319,19 @@ internal class StatementOptimizer(private val program: Program, private fun hasContinueOrBreak(scope: INameScope): Boolean { - class Searcher: IAstModifyingVisitor + class Searcher: IAstVisitor { var count=0 - override fun visit(breakStmt: Break): Statement { + override fun visit(breakStmt: Break) { count++ - return super.visit(breakStmt) } - override fun visit(contStmt: Continue): Statement { + override fun visit(contStmt: Continue) { count++ - return super.visit(contStmt) } } + val s=Searcher() for(stmt in scope.statements) { stmt.accept(s)