Improved variable lifetime tracking

This commit is contained in:
Eduard Urbach 2024-06-30 00:17:14 +02:00
parent 6e1af81733
commit 3fe2cd1da2
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
17 changed files with 114 additions and 31 deletions

View File

@ -24,12 +24,3 @@ func BenchmarkExpressions(b *testing.B) {
assert.Nil(b, err) assert.Nil(b, err)
} }
} }
func BenchmarkHello(b *testing.B) {
compiler := build.New("examples/hello")
for i := 0; i < b.N; i++ {
_, err := compiler.Run()
assert.Nil(b, err)
}
}

View File

@ -34,6 +34,7 @@ func TestErrors(t *testing.T) {
{"VariableAlreadyExists.q", &errors.VariableAlreadyExists{Name: "x"}}, {"VariableAlreadyExists.q", &errors.VariableAlreadyExists{Name: "x"}},
{"UnknownIdentifier.q", &errors.UnknownIdentifier{Name: "x"}}, {"UnknownIdentifier.q", &errors.UnknownIdentifier{Name: "x"}},
{"UnknownIdentifier2.q", &errors.UnknownIdentifier{Name: "x"}}, {"UnknownIdentifier2.q", &errors.UnknownIdentifier{Name: "x"}},
{"UnusedVariable.q", &errors.UnusedVariable{Name: "x"}},
} }
for _, test := range tests { for _, test := range tests {

View File

@ -1,6 +1,6 @@
main() { main() {
address := 4194304 + 1 address := 4194304 + 1
length := (0 + 50 - 20) * 10 / 100 length := 3
print(address, length) print(address, length)
} }

View File

@ -4,6 +4,7 @@ import (
"strconv" "strconv"
"git.akyoto.dev/cli/q/src/build/asm" "git.akyoto.dev/cli/q/src/build/asm"
"git.akyoto.dev/cli/q/src/build/config"
"git.akyoto.dev/cli/q/src/build/cpu" "git.akyoto.dev/cli/q/src/build/cpu"
"git.akyoto.dev/cli/q/src/build/expression" "git.akyoto.dev/cli/q/src/build/expression"
"git.akyoto.dev/cli/q/src/build/token" "git.akyoto.dev/cli/q/src/build/token"
@ -12,6 +13,10 @@ import (
// Execute executes an operation on a register with a value operand. // Execute executes an operation on a register with a value operand.
func (f *Function) Execute(operation token.Token, register cpu.Register, value *expression.Expression) error { func (f *Function) Execute(operation token.Token, register cpu.Register, value *expression.Expression) error {
if config.Verbose {
f.Logf("execute: %s on register %s with value %s", operation.Text(), register, value)
}
if value.IsLeaf() { if value.IsLeaf() {
return f.ExecuteLeaf(operation, register, value.Token) return f.ExecuteLeaf(operation, register, value.Token)
} }
@ -122,6 +127,10 @@ func (f *Function) ExecuteRegisterRegister(operation token.Token, destination cp
// ExpressionToRegister moves the result of an expression into the given register. // ExpressionToRegister moves the result of an expression into the given register.
func (f *Function) ExpressionToRegister(root *expression.Expression, register cpu.Register) error { func (f *Function) ExpressionToRegister(root *expression.Expression, register cpu.Register) error {
if config.Verbose {
f.Logf("%s to register %s", root, register)
}
operation := root.Token operation := root.Token
if root.IsLeaf() { if root.IsLeaf() {

View File

@ -1,6 +1,8 @@
package build package build
import ( import (
"fmt"
"git.akyoto.dev/cli/q/src/build/config" "git.akyoto.dev/cli/q/src/build/config"
"git.akyoto.dev/cli/q/src/build/expression" "git.akyoto.dev/cli/q/src/build/expression"
"git.akyoto.dev/cli/q/src/build/fs" "git.akyoto.dev/cli/q/src/build/fs"
@ -44,6 +46,10 @@ func (f *Function) CompileTokens(body token.List) error {
instruction := body[start:i] instruction := body[start:i]
if config.Verbose { if config.Verbose {
f.Logf("compiling: %s", instruction)
}
if config.Assembler {
f.debug = append(f.debug, debug{ f.debug = append(f.debug, debug{
position: len(f.assembler.Instructions), position: len(f.assembler.Instructions),
source: instruction, source: instruction,
@ -108,6 +114,11 @@ func (f *Function) CompileInstruction(line token.List) error {
} }
} }
// Logf formats a message for verbose output.
func (f *Function) Logf(format string, data ...any) {
fmt.Printf("[%s @ %d] %s\n", f, len(f.assembler.Instructions), fmt.Sprintf(format, data...))
}
// String returns the function name. // String returns the function name.
func (f *Function) String() string { func (f *Function) String() string {
return f.Name return f.Name

View File

@ -1,6 +1,7 @@
package build package build
import ( import (
"git.akyoto.dev/cli/q/src/build/config"
"git.akyoto.dev/cli/q/src/build/cpu" "git.akyoto.dev/cli/q/src/build/cpu"
"git.akyoto.dev/cli/q/src/build/expression" "git.akyoto.dev/cli/q/src/build/expression"
) )
@ -28,6 +29,10 @@ func (f *Function) CompileFunctionCall(expr *expression.Expression) error {
err := f.ExpressionsToRegisters(parameters, registers) err := f.ExpressionsToRegisters(parameters, registers)
if config.Verbose {
f.Logf("call: %s", funcName)
}
if isSyscall { if isSyscall {
f.assembler.Syscall() f.assembler.Syscall()
} else { } else {

View File

@ -2,6 +2,7 @@ package build
import ( import (
"git.akyoto.dev/cli/q/src/build/asm" "git.akyoto.dev/cli/q/src/build/asm"
"git.akyoto.dev/cli/q/src/build/config"
"git.akyoto.dev/cli/q/src/build/cpu" "git.akyoto.dev/cli/q/src/build/cpu"
) )
@ -20,7 +21,7 @@ func (f *Function) SaveRegister(register cpu.Register) {
} }
} }
if variable == nil || variable.UsesRemaining == 0 { if variable == nil || variable.Alive == 0 {
return return
} }
@ -30,6 +31,10 @@ func (f *Function) SaveRegister(register cpu.Register) {
panic("no free registers") panic("no free registers")
} }
if config.Verbose {
f.Logf("moving %s from %s to %s (alive: %d)", variable.Name, variable.Register, newRegister, variable.Alive)
}
f.assembler.RegisterRegister(asm.MOVE, newRegister, register) f.assembler.RegisterRegister(asm.MOVE, newRegister, register)
f.cpu.Free(register) f.cpu.Free(register)
f.cpu.Use(newRegister) f.cpu.Use(newRegister)

View File

@ -8,5 +8,5 @@ import (
type Variable struct { type Variable struct {
Name string Name string
Register cpu.Register Register cpu.Register
UsesRemaining int Alive int
} }

View File

@ -1,6 +1,7 @@
package build package build
import ( import (
"git.akyoto.dev/cli/q/src/build/config"
"git.akyoto.dev/cli/q/src/build/expression" "git.akyoto.dev/cli/q/src/build/expression"
"git.akyoto.dev/cli/q/src/build/token" "git.akyoto.dev/cli/q/src/build/token"
"git.akyoto.dev/cli/q/src/errors" "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) 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) reg, exists := f.cpu.FindFree(f.cpu.General)
if !exists { if !exists {
@ -30,30 +37,40 @@ func (f *Function) CompileVariableDefinition(expr *expression.Expression) error
return err return err
} }
variable := &Variable{ f.addVariable(&Variable{
Name: name, Name: name,
Register: reg, Register: reg,
} Alive: uses,
})
f.addVariable(variable)
f.useVariable(variable)
return nil return nil
} }
func (f *Function) addVariable(variable *Variable) { 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.variables[variable.Name] = variable
f.cpu.Use(variable.Register) f.cpu.Use(variable.Register)
} }
func (f *Function) useVariable(variable *Variable) { 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") 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) f.cpu.Free(variable.Register)
} }
} }

View File

@ -8,5 +8,6 @@ const (
) )
var ( var (
Assembler = false
Verbose = false Verbose = false
) )

View File

@ -260,7 +260,18 @@ func scanFile(path string, functions chan<- *Function) error {
if len(tokens) == 1 { if len(tokens) == 1 {
name := tokens[0].Text() name := tokens[0].Text()
register := x64.CallRegisters[len(function.variables)] register := x64.CallRegisters[len(function.variables)]
variable := &Variable{Name: name, Register: register} uses := countIdentifier(function.Body, name)
if uses == 0 {
return errors.New(&errors.UnusedVariable{Name: name}, file, tokens[0].Position)
}
variable := &Variable{
Name: name,
Register: register,
Alive: uses,
}
function.addVariable(variable) function.addVariable(variable)
return nil return nil
} }

View File

@ -19,6 +19,9 @@ func Build(args []string) int {
case "--dry": case "--dry":
dry = true dry = true
case "--assembler", "-a":
config.Assembler = true
case "--verbose", "-v": case "--verbose", "-v":
config.Verbose = true config.Verbose = true
@ -43,7 +46,7 @@ func Build(args []string) int {
return 1 return 1
} }
if config.Verbose { if config.Assembler {
result.EachFunction(result.Main, map[*build.Function]bool{}, func(f *build.Function) { result.EachFunction(result.Main, map[*build.Function]bool{}, func(f *build.Function) {
f.PrintInstructions() f.PrintInstructions()
}) })

View File

@ -7,10 +7,17 @@ import (
// Help shows the command line argument usage. // Help shows the command line argument usage.
func Help(w io.Writer, code int) int { func Help(w io.Writer, code int) int {
fmt.Fprintln(w, "Usage: q [command] [options]") fmt.Fprintln(w, `Usage: q [command] [options]
fmt.Fprintln(w, "")
fmt.Fprintln(w, " build [directory] [file]") commands:
fmt.Fprintln(w, " help")
fmt.Fprintln(w, " system") build [directory | file]
help
system
build options:
--assembler, -a Show assembler instructions.
--verbose, -v Show verbose output.`)
return code return code
} }

View File

@ -0,0 +1,13 @@
package errors
import "fmt"
// UnusedVariable error is created when a defined variable is never used.
type UnusedVariable struct {
Name string
}
// Error generates the string representation.
func (err *UnusedVariable) Error() string {
return fmt.Sprintf("Unused variable '%s'", err.Name)
}

View File

@ -1,3 +1,8 @@
main() { main() {
return 1+2*3 x := f(2, 3)
syscall(60, x)
}
f(x, y) {
return (x + y) * (x + y)
} }

View File

@ -1,3 +1,4 @@
main() { main() {
x := 123 +++ 456 x := 123 +++ 456
syscall(60, x)
} }

View File

@ -0,0 +1,3 @@
main() {
x := 1
}