callgraph no longer forgets some identifier occurrences

This commit is contained in:
Irmen de Jong
2025-01-18 21:18:08 +01:00
parent 08cd2fd6e8
commit 228be5cd04
2 changed files with 10 additions and 9 deletions

View File

@@ -117,11 +117,12 @@ class TestCallgraph: FunSpec({
val result = compileText(C64Target(), false, sourcecode)!!
val graph = CallGraph(result.compilerAst)
graph.allIdentifiers.size shouldBeGreaterThanOrEqual 5
val empties = graph.allIdentifiers.keys.filter { it.nameInSource==listOf("empty") }
val empties = graph.allIdentifiers.filter { it.first.nameInSource==listOf("empty") }
println(graph.allIdentifiers)
empties.size shouldBe 3
empties[0].position.line shouldBe 4
empties[1].position.line shouldBe 5
empties[2].position.line shouldBe 6
empties[0].first.position.line shouldBe 4
empties[1].first.position.line shouldBe 5
empties[2].first.position.line shouldBe 6
}
test("checking block and subroutine names usage in assembly code") {

View File

@@ -18,14 +18,14 @@ class CallGraph(private val program: Program) : IAstVisitor {
val calls = mutableMapOf<Subroutine, Set<Subroutine>>().withDefault { setOf() }
val calledBy = mutableMapOf<Subroutine, Set<Node>>().withDefault { setOf() }
val notCalledButReferenced = mutableSetOf<Subroutine>()
private val allIdentifiersAndTargets = mutableMapOf<IdentifierReference, Statement>()
private val allIdentifiersAndTargets = mutableListOf<Pair<IdentifierReference, Statement>>()
private val allAssemblyNodes = mutableListOf<InlineAssembly>()
init {
visit(program)
}
val allIdentifiers: Map<IdentifierReference, Statement> = allIdentifiersAndTargets
val allIdentifiers: List<Pair<IdentifierReference, Statement>> = allIdentifiersAndTargets
private val usedSubroutines: Set<Subroutine> by lazy {
calledBy.keys + program.entrypoint + notCalledButReferenced
@@ -36,7 +36,7 @@ class CallGraph(private val program: Program) : IAstVisitor {
val used = mutableSetOf<Block>()
allIdentifiersAndTargets.forEach {
val target = it.value.definingBlock
val target = it.second.definingBlock
used.add(target)
}
@@ -108,7 +108,7 @@ class CallGraph(private val program: Program) : IAstVisitor {
override fun visit(identifier: IdentifierReference) {
val target = identifier.targetStatement(program)
if(target!=null)
allIdentifiersAndTargets[identifier] = target
allIdentifiersAndTargets.add(identifier to target)
// if it's a scoped identifier, the subroutines in the name are also referenced!
val scope = identifier.definingScope
@@ -196,7 +196,7 @@ class CallGraph(private val program: Program) : IAstVisitor {
val assemblyBlocks = allAssemblyNodes.filter {
decl.name in it.names || "p8v_" + decl.name in it.names
}
return allIdentifiersAndTargets.filter { decl===it.value }.map{ it.key } + assemblyBlocks
return allIdentifiersAndTargets.filter { decl===it.second }.map{ it.first } + assemblyBlocks
}
private val prefixes = listOf("p8b_", "p8v_", "p8s_", "p8c_", "p8l_", "p8_", "")