From eb160afd91a2406d1090171f8258307823d0bb37 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Mon, 22 Jul 2024 22:54:24 +0200 Subject: [PATCH] Reduced memory usage --- src/build/ast/Count.go | 4 ++-- src/build/core/CompileDefinition.go | 13 ++++++++++++- src/build/core/storeVariableInRegister.go | 20 -------------------- src/build/expression/Expression.go | 4 ++-- src/build/scope/Variable.go | 8 ++++---- src/build/token/Count.go | 4 ++-- 6 files changed, 22 insertions(+), 31 deletions(-) delete mode 100644 src/build/core/storeVariableInRegister.go diff --git a/src/build/ast/Count.go b/src/build/ast/Count.go index b14c652..0c0c4a8 100644 --- a/src/build/ast/Count.go +++ b/src/build/ast/Count.go @@ -3,8 +3,8 @@ package ast import "git.akyoto.dev/cli/q/src/build/token" // Count counts how often the given token appears in the AST. -func Count(body AST, buffer []byte, kind token.Kind, name string) int { - count := 0 +func Count(body AST, buffer []byte, kind token.Kind, name string) uint8 { + count := uint8(0) for _, node := range body { switch node := node.(type) { diff --git a/src/build/core/CompileDefinition.go b/src/build/core/CompileDefinition.go index c5473b1..716aa28 100644 --- a/src/build/core/CompileDefinition.go +++ b/src/build/core/CompileDefinition.go @@ -3,6 +3,7 @@ package core import ( "git.akyoto.dev/cli/q/src/build/ast" "git.akyoto.dev/cli/q/src/build/errors" + "git.akyoto.dev/cli/q/src/build/scope" "git.akyoto.dev/cli/q/src/build/token" ) @@ -20,5 +21,15 @@ func (f *Function) CompileDefinition(node *ast.Define) error { return errors.New(&errors.UnusedVariable{Name: name}, f.File, node.Name.Position) } - return f.storeVariableInRegister(name, node.Value, uses) + register := f.CurrentScope().MustFindFree(f.cpu.General) + f.CurrentScope().Reserve(register) + err := f.ExpressionToRegister(node.Value, register) + + f.AddVariable(&scope.Variable{ + Name: name, + Register: register, + Alive: uses, + }) + + return err } diff --git a/src/build/core/storeVariableInRegister.go b/src/build/core/storeVariableInRegister.go deleted file mode 100644 index 7adf578..0000000 --- a/src/build/core/storeVariableInRegister.go +++ /dev/null @@ -1,20 +0,0 @@ -package core - -import ( - "git.akyoto.dev/cli/q/src/build/expression" - "git.akyoto.dev/cli/q/src/build/scope" -) - -func (f *Function) storeVariableInRegister(name string, value *expression.Expression, uses int) error { - reg := f.CurrentScope().MustFindFree(f.cpu.General) - f.CurrentScope().Reserve(reg) - err := f.ExpressionToRegister(value, reg) - - f.AddVariable(&scope.Variable{ - Name: name, - Register: reg, - Alive: uses, - }) - - return err -} diff --git a/src/build/expression/Expression.go b/src/build/expression/Expression.go index 5027b8e..7b39b12 100644 --- a/src/build/expression/Expression.go +++ b/src/build/expression/Expression.go @@ -37,8 +37,8 @@ func (expr *Expression) AddChild(child *Expression) { } // Count counts how often the given token appears in the expression. -func (expr *Expression) Count(buffer []byte, kind token.Kind, name string) int { - count := 0 +func (expr *Expression) Count(buffer []byte, kind token.Kind, name string) uint8 { + count := uint8(0) expr.EachLeaf(func(leaf *Expression) error { if leaf.Token.Kind == kind && leaf.Token.Text(buffer) == name { diff --git a/src/build/scope/Variable.go b/src/build/scope/Variable.go index e1a54c3..28e845a 100644 --- a/src/build/scope/Variable.go +++ b/src/build/scope/Variable.go @@ -7,7 +7,7 @@ import ( // Variable represents a named register. type Variable struct { Name string - Alive int + Alive uint8 Depth uint8 Register cpu.Register } @@ -19,9 +19,9 @@ func (v *Variable) IsAlive() bool { // Use reduces the lifetime counter by one. func (v *Variable) Use() { - v.Alive-- - - if v.Alive < 0 { + if v.Alive == 0 { panic("incorrect number of variable use calls") } + + v.Alive-- } diff --git a/src/build/token/Count.go b/src/build/token/Count.go index e83fff6..cf460a3 100644 --- a/src/build/token/Count.go +++ b/src/build/token/Count.go @@ -1,8 +1,8 @@ package token // Count counts how often the given token appears in the token list. -func Count(tokens []Token, buffer []byte, kind Kind, name string) int { - count := 0 +func Count(tokens []Token, buffer []byte, kind Kind, name string) uint8 { + count := uint8(0) for _, t := range tokens { if t.Kind == Identifier && t.Text(buffer) == name {