Improved variable lifetime tracking
This commit is contained in:
parent
1510f88165
commit
b8c011d742
17 changed files with 114 additions and 31 deletions
|
@ -1,6 +1,7 @@
|
|||
package build
|
||||
|
||||
import (
|
||||
"git.akyoto.dev/cli/q/src/build/config"
|
||||
"git.akyoto.dev/cli/q/src/build/expression"
|
||||
"git.akyoto.dev/cli/q/src/build/token"
|
||||
"git.akyoto.dev/cli/q/src/errors"
|
||||
|
@ -18,6 +19,12 @@ func (f *Function) CompileVariableDefinition(expr *expression.Expression) error
|
|||
return errors.New(&errors.VariableAlreadyExists{Name: name}, f.File, expr.Children[0].Token.Position)
|
||||
}
|
||||
|
||||
uses := countIdentifier(f.Body, name) - 1
|
||||
|
||||
if uses == 0 {
|
||||
return errors.New(&errors.UnusedVariable{Name: name}, f.File, expr.Children[0].Token.Position)
|
||||
}
|
||||
|
||||
reg, exists := f.cpu.FindFree(f.cpu.General)
|
||||
|
||||
if !exists {
|
||||
|
@ -30,30 +37,40 @@ func (f *Function) CompileVariableDefinition(expr *expression.Expression) error
|
|||
return err
|
||||
}
|
||||
|
||||
variable := &Variable{
|
||||
f.addVariable(&Variable{
|
||||
Name: name,
|
||||
Register: reg,
|
||||
}
|
||||
Alive: uses,
|
||||
})
|
||||
|
||||
f.addVariable(variable)
|
||||
f.useVariable(variable)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Function) addVariable(variable *Variable) {
|
||||
variable.UsesRemaining = countIdentifier(f.Body, variable.Name)
|
||||
if config.Verbose {
|
||||
f.Logf("%s occupies %s (alive: %d)", variable.Name, variable.Register, variable.Alive)
|
||||
}
|
||||
|
||||
f.variables[variable.Name] = variable
|
||||
f.cpu.Use(variable.Register)
|
||||
}
|
||||
|
||||
func (f *Function) useVariable(variable *Variable) {
|
||||
variable.UsesRemaining--
|
||||
variable.Alive--
|
||||
|
||||
if variable.UsesRemaining < 0 {
|
||||
if config.Verbose {
|
||||
f.Logf("%s occupying %s was used (alive: %d)", variable.Name, variable.Register, variable.Alive)
|
||||
}
|
||||
|
||||
if variable.Alive < 0 {
|
||||
panic("incorrect number of variable use calls")
|
||||
}
|
||||
|
||||
if variable.UsesRemaining == 0 {
|
||||
if variable.Alive == 0 {
|
||||
if config.Verbose {
|
||||
f.Logf("%s is no longer used, free register: %s", variable.Name, variable.Register)
|
||||
}
|
||||
|
||||
f.cpu.Free(variable.Register)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue