Added missing register deallocation

This commit is contained in:
Eduard Urbach 2025-01-26 14:31:38 +01:00
parent e62eaba6e6
commit fb40059113
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
3 changed files with 14 additions and 5 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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
}