From 008f097186437b3ad3dda5083f5d3a9782c13a6d Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Mon, 31 Mar 2025 14:36:42 +0200 Subject: [PATCH] Simplified CPU state --- src/core/CompileAssign.go | 13 +++++++++---- src/cpu/State.go | 24 +++++------------------- src/cpu/State_test.go | 16 +++++----------- src/register/Machine.go | 2 +- src/register/NewRegister.go | 2 +- src/register/Return.go | 4 +--- src/register/postInstruction.go | 2 +- 7 files changed, 23 insertions(+), 40 deletions(-) diff --git a/src/core/CompileAssign.go b/src/core/CompileAssign.go index 1a2f2e6..af628ef 100644 --- a/src/core/CompileAssign.go +++ b/src/core/CompileAssign.go @@ -37,12 +37,17 @@ func (f *Function) CompileAssign(node *ast.Assign) error { switch leftValue := leftValue.(type) { case *eval.Register: - // TODO: Reservation needs to be canceled on defer - f.CurrentScope().Reserve(leftValue.Register) + if !f.RegisterIsUsed(leftValue.Register) { + f.UseRegister(leftValue.Register) + defer f.FreeRegister(leftValue.Register) + } + f.Execute(operation, leftValue.Register, right) case *eval.Memory: - // TODO: Reservation needs to be canceled on defer - f.CurrentScope().Reserve(leftValue.Memory.Base) + if !f.RegisterIsUsed(leftValue.Memory.Base) { + f.UseRegister(leftValue.Memory.Base) + defer f.FreeRegister(leftValue.Memory.Base) + } if operation.Kind == token.Assign { rightValue, err := f.Evaluate(right) diff --git a/src/cpu/State.go b/src/cpu/State.go index fb8659b..481b585 100644 --- a/src/cpu/State.go +++ b/src/cpu/State.go @@ -1,41 +1,27 @@ package cpu // State contains information about which registers are currently in use. -type State struct { - Reserved uint64 - Used uint64 -} +type State uint64 // Free will reset the reserved and used status which means the register can be allocated again. func (s *State) Free(reg Register) { - s.Reserved &= ^(1 << reg) - s.Used &= ^(1 << reg) -} - -// IsReserved returns true if the register was marked for future use. -func (s *State) IsReserved(reg Register) bool { - return s.Reserved&(1<