From d3436b13a572722e1493fec2c527d6e4fea16b17 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 10 Jul 2024 10:48:15 +0200 Subject: [PATCH] Reduced usage of temporary registers --- src/build/core/CompileDefinition.go | 6 ++---- src/build/core/ExpressionToRegister.go | 5 +++++ src/build/core/Function.go | 7 ------- src/build/core/Instructions.go | 6 ++++-- src/build/core/SaveRegister.go | 5 +---- src/build/core/UsesRegister.go | 9 +-------- 6 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/build/core/CompileDefinition.go b/src/build/core/CompileDefinition.go index f2fd234..c23ac56 100644 --- a/src/build/core/CompileDefinition.go +++ b/src/build/core/CompileDefinition.go @@ -1,8 +1,6 @@ package core import ( - "fmt" - "git.akyoto.dev/cli/q/src/build/ast" "git.akyoto.dev/cli/q/src/build/config" "git.akyoto.dev/cli/q/src/build/errors" @@ -43,7 +41,7 @@ func (f *Function) CompileDefinition(node *ast.Define) error { func (f *Function) AddVariable(variable *Variable) { if config.Comments { - f.Comment(fmt.Sprintf("%s = %s (%s, %d uses)", variable.Name, variable.Value, variable.Register, variable.Alive)) + f.Comment("%s = %s (%s, %d uses)", variable.Name, variable.Value, variable.Register, variable.Alive) } f.variables[variable.Name] = variable @@ -60,7 +58,7 @@ func (f *Function) useVariable(variable *Variable) { if variable.Alive == 0 { if config.Comments { - f.Comment(fmt.Sprintf("%s died (%s)", variable.Name, variable.Register)) + f.Comment("%s died (%s)", variable.Name, variable.Register) } f.cpu.Free(variable.Register) diff --git a/src/build/core/ExpressionToRegister.go b/src/build/core/ExpressionToRegister.go index 793403c..acc211d 100644 --- a/src/build/core/ExpressionToRegister.go +++ b/src/build/core/ExpressionToRegister.go @@ -3,6 +3,7 @@ package core import ( "git.akyoto.dev/cli/q/src/build/asm" "git.akyoto.dev/cli/q/src/build/ast" + "git.akyoto.dev/cli/q/src/build/config" "git.akyoto.dev/cli/q/src/build/cpu" "git.akyoto.dev/cli/q/src/build/errors" "git.akyoto.dev/cli/q/src/build/expression" @@ -34,6 +35,10 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp if f.UsesRegister(right, register) { register = f.cpu.MustFindFree(f.cpu.General) + + if config.Comments { + f.Comment("temporary register %s", register) + } } f.cpu.Reserve(register) diff --git a/src/build/core/Function.go b/src/build/core/Function.go index 0af8d06..7e57da2 100644 --- a/src/build/core/Function.go +++ b/src/build/core/Function.go @@ -1,8 +1,6 @@ package core import ( - "fmt" - "git.akyoto.dev/cli/q/src/build/arch/x64" "git.akyoto.dev/cli/q/src/build/asm" "git.akyoto.dev/cli/q/src/build/ast" @@ -102,11 +100,6 @@ func (f *Function) CompileASTNode(node ast.Node) 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. func (f *Function) String() string { return f.Name diff --git a/src/build/core/Instructions.go b/src/build/core/Instructions.go index d9bae49..e6764ca 100644 --- a/src/build/core/Instructions.go +++ b/src/build/core/Instructions.go @@ -1,6 +1,8 @@ package core import ( + "fmt" + "git.akyoto.dev/cli/q/src/build/asm" "git.akyoto.dev/cli/q/src/build/config" "git.akyoto.dev/cli/q/src/build/cpu" @@ -17,8 +19,8 @@ func (f *Function) Call(label string) { f.postInstruction() } -func (f *Function) Comment(comment string) { - f.assembler.Comment(comment) +func (f *Function) Comment(format string, args ...any) { + f.assembler.Comment(fmt.Sprintf(format, args...)) f.postInstruction() } diff --git a/src/build/core/SaveRegister.go b/src/build/core/SaveRegister.go index 17f16cb..30e3c33 100644 --- a/src/build/core/SaveRegister.go +++ b/src/build/core/SaveRegister.go @@ -1,8 +1,6 @@ package core import ( - "fmt" - "git.akyoto.dev/cli/q/src/build/asm" "git.akyoto.dev/cli/q/src/build/config" "git.akyoto.dev/cli/q/src/build/cpu" @@ -37,10 +35,9 @@ func (f *Function) SaveRegister(register cpu.Register) { f.cpu.Reserve(newRegister) if config.Comments { - f.Comment(fmt.Sprintf("save %s to %s", register, newRegister)) + f.Comment("save %s to %s", register, newRegister) } f.RegisterRegister(asm.MOVE, newRegister, register) - f.cpu.Free(register) variable.Register = newRegister } diff --git a/src/build/core/UsesRegister.go b/src/build/core/UsesRegister.go index 5090f63..9a10a85 100644 --- a/src/build/core/UsesRegister.go +++ b/src/build/core/UsesRegister.go @@ -4,19 +4,12 @@ import ( "git.akyoto.dev/cli/q/src/build/ast" "git.akyoto.dev/cli/q/src/build/cpu" "git.akyoto.dev/cli/q/src/build/expression" - "git.akyoto.dev/cli/q/src/build/token" ) // UsesRegister returns true if evaluating the expression would write or read the given register. func (f *Function) UsesRegister(expr *expression.Expression, register cpu.Register) bool { if expr.IsLeaf() { - if expr.Token.Kind == token.Number { - return false - } - - name := expr.Token.Text() - variable := f.variables[name] - return register == variable.Register + return false } if ast.IsFunctionCall(expr) {