Implemented dependency tracking
This commit is contained in:
parent
acfa6de1d4
commit
b095a95021
12 changed files with 75 additions and 69 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue