Implemented dependency tracking

This commit is contained in:
Eduard Urbach 2025-03-03 12:14:53 +01:00
parent acfa6de1d4
commit b095a95021
Signed by: eduard
GPG key ID: 49226B848C78F6C8
12 changed files with 75 additions and 69 deletions

View file

@ -1,37 +1,20 @@
package compiler
import (
"git.urbach.dev/cli/q/src/asm"
"git.urbach.dev/cli/q/src/core"
)
// eachFunction recursively finds all the calls to external functions.
// eachFunction recursively finds all the calls to other functions.
// It avoids calling the same function twice with the help of a hashmap.
func (r *Result) eachFunction(caller *core.Function, traversed map[*core.Function]bool, call func(*core.Function)) {
call(caller)
traversed[caller] = true
for _, x := range caller.Assembler.Instructions {
if x.Mnemonic != asm.CALL {
for _, function := range caller.Dependencies {
if traversed[function] {
continue
}
label, isLabel := x.Data.(*asm.Label)
if !isLabel {
continue
}
callee, exists := r.Functions[label.Name]
if !exists {
continue
}
if traversed[callee] {
continue
}
r.eachFunction(callee, traversed, call)
r.eachFunction(function, traversed, call)
}
}