From ea4cefad0a52ed4b141ce5a2c0e24809fee6119e Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 22 Jul 2026 10:54:33 +0200 Subject: [PATCH] Refactored moves for call results --- src/codegen/executeCall.go | 18 +--------------- src/codegen/executeCallExtern.go | 18 +--------------- src/codegen/executeCallPointer.go | 18 +--------------- src/codegen/executeSyscall.go | 18 +--------------- src/codegen/move.go | 34 +++++++++++++++++++++++++++++++ src/codegen/moveCallResult.go | 27 ++++++++++++++++++++++++ src/codegen/spill.go | 31 ---------------------------- 7 files changed, 65 insertions(+), 99 deletions(-) create mode 100644 src/codegen/move.go create mode 100644 src/codegen/moveCallResult.go diff --git a/src/codegen/executeCall.go b/src/codegen/executeCall.go index 33f5ba2..630f92f 100644 --- a/src/codegen/executeCall.go +++ b/src/codegen/executeCall.go @@ -12,21 +12,5 @@ func (f *Function) executeCall(step *Step, instr *ssa.Call) { Label: instr.Func.String(), }) - destination := step.Register - - if destination == -1 || destination == f.CPU.Call.Out[0] { - return - } - - isSpilled := f.isSpilled(destination) - - if isSpilled { - f.storeSpill(step, f.CPU.Call.Out[0]) - return - } - - f.Assembler.Append(&asm.Move{ - Destination: destination, - Source: f.CPU.Call.Out[0], - }) + f.moveCallResult(step, f.CPU.Call.Out[0]) } \ No newline at end of file diff --git a/src/codegen/executeCallExtern.go b/src/codegen/executeCallExtern.go index e93c597..2eb0b72 100644 --- a/src/codegen/executeCallExtern.go +++ b/src/codegen/executeCallExtern.go @@ -53,21 +53,5 @@ func (f *Function) executeCallExtern(step *Step, instr *ssa.CallExtern) { f.Assembler.Append(&asm.AddNumber{Destination: f.CPU.StackPointer, Source: f.CPU.StackPointer, Number: 8}) } - destination := step.Register - - if destination == -1 || destination == f.CPU.ExternCall.Out[0] { - return - } - - isSpilled := f.isSpilled(destination) - - if isSpilled { - f.storeSpill(step, f.CPU.ExternCall.Out[0]) - return - } - - f.Assembler.Append(&asm.Move{ - Destination: destination, - Source: f.CPU.ExternCall.Out[0], - }) + f.moveCallResult(step, f.CPU.ExternCall.Out[0]) } \ No newline at end of file diff --git a/src/codegen/executeCallPointer.go b/src/codegen/executeCallPointer.go index e8eb5ee..c5c7aed 100644 --- a/src/codegen/executeCallPointer.go +++ b/src/codegen/executeCallPointer.go @@ -14,21 +14,5 @@ func (f *Function) executeCallPointer(step *Step, instr *ssa.CallPointer) { Address: address, }) - destination := step.Register - - if destination == -1 || destination == f.CPU.Call.Out[0] { - return - } - - isSpilled := f.isSpilled(destination) - - if isSpilled { - f.storeSpill(step, f.CPU.Call.Out[0]) - return - } - - f.Assembler.Append(&asm.Move{ - Destination: destination, - Source: f.CPU.Call.Out[0], - }) + f.moveCallResult(step, f.CPU.Call.Out[0]) } \ No newline at end of file diff --git a/src/codegen/executeSyscall.go b/src/codegen/executeSyscall.go index c76494b..f805f88 100644 --- a/src/codegen/executeSyscall.go +++ b/src/codegen/executeSyscall.go @@ -8,21 +8,5 @@ import ( func (f *Function) executeSyscall(step *Step, instr *ssa.Syscall) { f.moveValuesToRegisters(instr.Arguments, f.CPU.Syscall.In) f.Assembler.Append(&asm.Syscall{}) - destination := step.Register - - if destination == -1 || destination == f.CPU.Syscall.Out[0] { - return - } - - isSpilled := f.isSpilled(destination) - - if isSpilled { - f.storeSpill(step, f.CPU.Syscall.Out[0]) - return - } - - f.Assembler.Append(&asm.Move{ - Destination: destination, - Source: f.CPU.Syscall.Out[0], - }) + f.moveCallResult(step, f.CPU.Syscall.Out[0]) } \ No newline at end of file diff --git a/src/codegen/move.go b/src/codegen/move.go new file mode 100644 index 0000000..9fc6312 --- /dev/null +++ b/src/codegen/move.go @@ -0,0 +1,34 @@ +package codegen + +import "git.urbach.dev/cli/q/src/asm" + +// move moves source register to destination register and handles virtual registers. +func (f *Function) move(destinationStep *Step, sourceStep *Step, step *Step) { + source := sourceStep.Register + destination := destinationStep.Register + + if source == destination { + return + } + + sourceIsSpilled := f.isSpilled(source) + destinationIsSpilled := f.isSpilled(destination) + + switch { + case !sourceIsSpilled && !destinationIsSpilled: + f.Assembler.Append(&asm.Move{ + Destination: destination, + Source: source, + }) + + case sourceIsSpilled && !destinationIsSpilled: + f.loadSpill(sourceStep, destination) + + case !sourceIsSpilled && destinationIsSpilled: + f.storeSpill(destinationStep, source) + + case sourceIsSpilled && destinationIsSpilled: + source = f.resolveOperand(sourceStep, step.Live) + f.storeSpill(destinationStep, source) + } +} \ No newline at end of file diff --git a/src/codegen/moveCallResult.go b/src/codegen/moveCallResult.go new file mode 100644 index 0000000..2998b70 --- /dev/null +++ b/src/codegen/moveCallResult.go @@ -0,0 +1,27 @@ +package codegen + +import ( + "git.urbach.dev/cli/q/src/asm" + "git.urbach.dev/cli/q/src/cpu" +) + +// moveCallResult moves the result from the call output register to the destination. +func (f *Function) moveCallResult(step *Step, source cpu.Register) { + destination := step.Register + + if destination == -1 || destination == source { + return + } + + isSpilled := f.isSpilled(destination) + + if isSpilled { + f.storeSpill(step, source) + return + } + + f.Assembler.Append(&asm.Move{ + Destination: destination, + Source: source, + }) +} \ No newline at end of file diff --git a/src/codegen/spill.go b/src/codegen/spill.go index adda2c6..d143ac3 100644 --- a/src/codegen/spill.go +++ b/src/codegen/spill.go @@ -45,37 +45,6 @@ func (f *Function) loadSpill(step *Step, destination cpu.Register) { }) } -// move moves source register to destination register and handles virtual registers. -func (f *Function) move(destinationStep *Step, sourceStep *Step, step *Step) { - source := sourceStep.Register - destination := destinationStep.Register - - if source == destination { - return - } - - sourceIsSpilled := f.isSpilled(source) - destinationIsSpilled := f.isSpilled(destination) - - switch { - case !sourceIsSpilled && !destinationIsSpilled: - f.Assembler.Append(&asm.Move{ - Destination: destination, - Source: source, - }) - - case sourceIsSpilled && !destinationIsSpilled: - f.loadSpill(sourceStep, destination) - - case !sourceIsSpilled && destinationIsSpilled: - f.storeSpill(destinationStep, source) - - case sourceIsSpilled && destinationIsSpilled: - source = f.resolveOperand(sourceStep, step.Live) - f.storeSpill(destinationStep, source) - } -} - // resolveOperand returns the register to use for an operand. // If the operand is spilled, it loads it from the stack first. func (f *Function) resolveOperand(step *Step, liveSteps []*Step) cpu.Register { -- 2.51.2