From 307558a7e7155f9180267901cc924ef4ccd21163 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 30 Jun 2020 20:42:55 +0200 Subject: [PATCH] removed some double code related to call tree --- compiler/src/prog8/ast/expressions/AstExpressions.kt | 2 +- compiler/src/prog8/ast/processing/AstChecker.kt | 5 ++--- compiler/src/prog8/ast/statements/AstStatements.kt | 2 -- compiler/src/prog8/optimizer/CallGraph.kt | 9 --------- compiler/src/prog8/optimizer/UnusedCodeRemover.kt | 2 +- 5 files changed, 4 insertions(+), 16 deletions(-) diff --git a/compiler/src/prog8/ast/expressions/AstExpressions.kt b/compiler/src/prog8/ast/expressions/AstExpressions.kt index b59490dc5..9b61c9cc8 100644 --- a/compiler/src/prog8/ast/expressions/AstExpressions.kt +++ b/compiler/src/prog8/ast/expressions/AstExpressions.kt @@ -22,7 +22,7 @@ sealed class Expression: Node { abstract fun constValue(program: Program): NumericLiteralValue? abstract fun accept(visitor: IAstVisitor) abstract fun accept(visitor: AstWalker, parent: Node) - abstract fun referencesIdentifiers(vararg name: String): Boolean // todo: remove this and add identifier usage tracking into CallGraph instead + abstract fun referencesIdentifiers(vararg name: String): Boolean abstract fun inferType(program: Program): InferredTypes.InferredType infix fun isSameAs(other: Expression): Boolean { diff --git a/compiler/src/prog8/ast/processing/AstChecker.kt b/compiler/src/prog8/ast/processing/AstChecker.kt index ff6983e85..3a86fd0fd 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")) // TODO use callgraph? + if(repeatLoop.untilCondition.referencesIdentifiers("A", "X", "Y")) 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")) // TODO use callgraph? + if(whileLoop.condition.referencesIdentifiers("A", "X", "Y")) 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) @@ -463,7 +463,6 @@ 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/statements/AstStatements.kt b/compiler/src/prog8/ast/statements/AstStatements.kt index c838bea00..545a72346 100644 --- a/compiler/src/prog8/ast/statements/AstStatements.kt +++ b/compiler/src/prog8/ast/statements/AstStatements.kt @@ -668,8 +668,6 @@ class Subroutine(override val name: String, get() = statements.any { it.expensiveToInline } override lateinit var parent: Node - val calledBy = mutableListOf() // TODO remove, use callgraph only - val calls = mutableSetOf() // TODO remove, use callgraph only val scopedname: String by lazy { makeScopedName(name) } diff --git a/compiler/src/prog8/optimizer/CallGraph.kt b/compiler/src/prog8/optimizer/CallGraph.kt index adaa56513..16253963f 100644 --- a/compiler/src/prog8/optimizer/CallGraph.kt +++ b/compiler/src/prog8/optimizer/CallGraph.kt @@ -57,15 +57,6 @@ class CallGraph(private val program: Program) : IAstVisitor { it.importedBy.addAll(importedBy.getValue(it)) it.imports.addAll(imports.getValue(it)) - - forAllSubroutines(it) { sub -> - sub.calledBy.clear() - sub.calls.clear() - - sub.calledBy.addAll(calledBy.getValue(sub)) - sub.calls.addAll(calls.getValue(sub)) - } - } val rootmodule = program.modules.first() diff --git a/compiler/src/prog8/optimizer/UnusedCodeRemover.kt b/compiler/src/prog8/optimizer/UnusedCodeRemover.kt index d929bc2d2..9a78efded 100644 --- a/compiler/src/prog8/optimizer/UnusedCodeRemover.kt +++ b/compiler/src/prog8/optimizer/UnusedCodeRemover.kt @@ -17,7 +17,7 @@ internal class UnusedCodeRemover: AstWalker() { val entrypoint = program.entrypoint() program.modules.forEach { callgraph.forAllSubroutines(it) { sub -> - if (sub !== entrypoint && !sub.keepAlways && (sub.calledBy.isEmpty() || (sub.containsNoCodeNorVars() && !sub.isAsmSubroutine))) + if (sub !== entrypoint && !sub.keepAlways && (callgraph.calledBy[sub].isNullOrEmpty() || (sub.containsNoCodeNorVars() && !sub.isAsmSubroutine))) removals.add(IAstModification.Remove(sub, sub.definingScope() as Node)) } }