From d771708693b6c94017ed57ad40e7d5318978eea4 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 5 Jul 2024 16:31:33 +0200 Subject: [PATCH] Improved code generation --- src/build/core/ExpressionsToRegisters.go | 1 - src/build/core/SaveRegister.go | 40 ------------------------ tests/programs/return.q | 11 +++++++ tests/programs_test.go | 1 + 4 files changed, 12 insertions(+), 41 deletions(-) delete mode 100644 src/build/core/SaveRegister.go create mode 100644 tests/programs/return.q diff --git a/src/build/core/ExpressionsToRegisters.go b/src/build/core/ExpressionsToRegisters.go index 6e293e2..64d6816 100644 --- a/src/build/core/ExpressionsToRegisters.go +++ b/src/build/core/ExpressionsToRegisters.go @@ -8,7 +8,6 @@ import ( // ExpressionsToRegisters moves multiple expressions into the specified registers. func (f *Function) ExpressionsToRegisters(expressions []*expression.Expression, registers []cpu.Register) error { for i := len(registers) - 1; i >= 0; i-- { - f.SaveRegister(registers[i]) err := f.ExpressionToRegister(expressions[i], registers[i]) if err != nil { diff --git a/src/build/core/SaveRegister.go b/src/build/core/SaveRegister.go deleted file mode 100644 index 7a23eb8..0000000 --- a/src/build/core/SaveRegister.go +++ /dev/null @@ -1,40 +0,0 @@ -package core - -import ( - "fmt" - - "git.akyoto.dev/cli/q/src/build/asm" - "git.akyoto.dev/cli/q/src/build/config" - "git.akyoto.dev/cli/q/src/build/cpu" -) - -// SaveRegister attempts to move a variable occupying this register to another register. -func (f *Function) SaveRegister(register cpu.Register) { - if f.cpu.IsFree(register) { - return - } - - var variable *Variable - - for _, v := range f.variables { - if v.Register == register { - variable = v - break - } - } - - if variable == nil || variable.Alive == 0 { - return - } - - newRegister := f.cpu.MustFindFree(f.cpu.General) - f.cpu.Use(newRegister) - - if config.Comments { - f.assembler.Comment(fmt.Sprintf("save %s to %s", register, newRegister)) - } - - f.assembler.RegisterRegister(asm.MOVE, newRegister, register) - f.cpu.Free(register) - variable.Register = newRegister -} diff --git a/tests/programs/return.q b/tests/programs/return.q new file mode 100644 index 0000000..13f1e6a --- /dev/null +++ b/tests/programs/return.q @@ -0,0 +1,11 @@ +main() { + syscall(60, f(2)) +} + +f(x) { + return x + 1 + g(x) +} + +g(x) { + return x + 1 +} \ No newline at end of file diff --git a/tests/programs_test.go b/tests/programs_test.go index cd70f45..91c483d 100644 --- a/tests/programs_test.go +++ b/tests/programs_test.go @@ -20,6 +20,7 @@ var programs = []struct { {"square-sum.q", "", 25}, {"chained-calls.q", "", 9}, {"nested-calls.q", "", 4}, + {"return.q", "", 6}, } func TestPrograms(t *testing.T) {