From fb40059113abad2eb49f247b43ae90226beed090 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sun, 26 Jan 2025 14:31:38 +0100 Subject: [PATCH] Added missing register deallocation --- src/core/CompileAssignArray.go | 6 +++++- src/core/CompileAssignDivision.go | 7 ++++++- src/core/Evaluate.go | 6 +++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/core/CompileAssignArray.go b/src/core/CompileAssignArray.go index 339f3f6..08f73e6 100644 --- a/src/core/CompileAssignArray.go +++ b/src/core/CompileAssignArray.go @@ -42,13 +42,17 @@ func (f *Function) CompileAssignArray(node *ast.Assign) error { memory.Offset = byte(offset) } else { - _, indexRegister, err := f.Evaluate(index) + _, indexRegister, isTemporary, err := f.Evaluate(index) if err != nil { return err } memory.OffsetRegister = indexRegister + + if isTemporary { + defer f.FreeRegister(indexRegister) + } } _, err := f.ExpressionToMemory(right, memory) diff --git a/src/core/CompileAssignDivision.go b/src/core/CompileAssignDivision.go index 22f68fd..e169827 100644 --- a/src/core/CompileAssignDivision.go +++ b/src/core/CompileAssignDivision.go @@ -29,7 +29,7 @@ func (f *Function) CompileAssignDivision(node *ast.Assign) error { } dividend := right.Children[0] - _, dividendRegister, err := f.Evaluate(dividend) + _, dividendRegister, isTemporary, err := f.Evaluate(dividend) if err != nil { return err @@ -39,5 +39,10 @@ func (f *Function) CompileAssignDivision(node *ast.Assign) error { err = f.Execute(right.Token, dividendRegister, divisor) f.RegisterRegister(asm.MOVE, quotientVariable.Register, x64.RAX) f.RegisterRegister(asm.MOVE, remainderVariable.Register, x64.RDX) + + if isTemporary { + f.FreeRegister(dividendRegister) + } + return err } diff --git a/src/core/Evaluate.go b/src/core/Evaluate.go index 235f917..349c077 100644 --- a/src/core/Evaluate.go +++ b/src/core/Evaluate.go @@ -8,18 +8,18 @@ import ( ) // Evaluate evaluates an expression and returns a register that contains the value of the expression. -func (f *Function) Evaluate(expr *expression.Expression) (*types.Type, cpu.Register, error) { +func (f *Function) Evaluate(expr *expression.Expression) (*types.Type, cpu.Register, bool, error) { if expr.Token.Kind == token.Identifier { name := expr.Token.Text(f.File.Bytes) variable := f.VariableByName(name) if variable.Alive == 1 { f.UseVariable(variable) - return variable.Type, variable.Register, nil + return variable.Type, variable.Register, false, nil } } tmp := f.NewRegister() typ, err := f.ExpressionToRegister(expr, tmp) - return typ, tmp, err + return typ, tmp, true, err }