diff --git a/src/build/core/CompileDefinition.go b/src/build/core/CompileDefinition.go index 852fb4f..a731841 100644 --- a/src/build/core/CompileDefinition.go +++ b/src/build/core/CompileDefinition.go @@ -48,7 +48,6 @@ func (f *Function) AddVariable(variable *Variable) { variable.Scope = scope scope.variables[variable.Name] = variable - scope.Reserve(variable.Register) scope.Use(variable.Register) } diff --git a/src/build/cpu/Register_test.go b/src/build/cpu/Register_test.go new file mode 100644 index 0000000..8c4c4ce --- /dev/null +++ b/src/build/cpu/Register_test.go @@ -0,0 +1,13 @@ +package cpu_test + +import ( + "testing" + + "git.akyoto.dev/cli/q/src/build/cpu" + "git.akyoto.dev/go/assert" +) + +func TestRegisterString(t *testing.T) { + register := cpu.Register(1) + assert.Equal(t, "r1", register.String()) +} diff --git a/src/build/cpu/State.go b/src/build/cpu/State.go index af1637e..fb8659b 100644 --- a/src/build/cpu/State.go +++ b/src/build/cpu/State.go @@ -35,7 +35,7 @@ func (s *State) Use(reg Register) { // FindFree tries to find a free register in the given slice of registers. func (s *State) FindFree(registers []Register) (Register, bool) { for _, reg := range registers { - if !s.IsReserved(reg) { + if !s.IsReserved(reg) && !s.IsUsed(reg) { return reg, true } } diff --git a/src/build/cpu/State_test.go b/src/build/cpu/State_test.go new file mode 100644 index 0000000..90cdab7 --- /dev/null +++ b/src/build/cpu/State_test.go @@ -0,0 +1,45 @@ +package cpu_test + +import ( + "testing" + + "git.akyoto.dev/cli/q/src/build/cpu" + "git.akyoto.dev/go/assert" +) + +func TestRegisterState(t *testing.T) { + s := cpu.State{} + assert.False(t, s.IsReserved(0)) + assert.False(t, s.IsUsed(0)) + s.Reserve(0) + assert.True(t, s.IsReserved(0)) + assert.False(t, s.IsUsed(0)) + s.Use(0) + assert.True(t, s.IsReserved(0)) + assert.True(t, s.IsUsed(0)) + s.Free(0) + assert.False(t, s.IsReserved(0)) + assert.False(t, s.IsUsed(0)) +} + +func TestFindFree(t *testing.T) { + s := cpu.State{} + s.Reserve(0) + s.Use(1) + + reg, found := s.FindFree([]cpu.Register{0, 1, 2, 3}) + assert.True(t, found) + assert.Equal(t, reg, 2) + + _, found = s.FindFree([]cpu.Register{0, 1}) + assert.False(t, found) +} + +func TestMustFindFree(t *testing.T) { + s := cpu.State{} + s.Reserve(0) + s.Use(1) + + reg := s.MustFindFree([]cpu.Register{0, 1, 2, 3}) + assert.Equal(t, reg, 2) +} diff --git a/src/build/fs/Walk_test.go b/src/build/fs/Walk_test.go index d3f6e17..82651ce 100644 --- a/src/build/fs/Walk_test.go +++ b/src/build/fs/Walk_test.go @@ -19,7 +19,12 @@ func TestWalk(t *testing.T) { assert.Contains(t, files, "Walk_test.go") } -func TestNonExisting(t *testing.T) { +func TestWalkFile(t *testing.T) { + err := fs.Walk("Walk.go", func(file string) {}) + assert.NotNil(t, err) +} + +func TestWalkNonExisting(t *testing.T) { err := fs.Walk("does-not-exist", func(file string) {}) assert.NotNil(t, err) }