Improved code generation
This commit is contained in:
parent
3e92c83c0d
commit
d771708693
@ -8,7 +8,6 @@ import (
|
|||||||
// ExpressionsToRegisters moves multiple expressions into the specified registers.
|
// ExpressionsToRegisters moves multiple expressions into the specified registers.
|
||||||
func (f *Function) ExpressionsToRegisters(expressions []*expression.Expression, registers []cpu.Register) error {
|
func (f *Function) ExpressionsToRegisters(expressions []*expression.Expression, registers []cpu.Register) error {
|
||||||
for i := len(registers) - 1; i >= 0; i-- {
|
for i := len(registers) - 1; i >= 0; i-- {
|
||||||
f.SaveRegister(registers[i])
|
|
||||||
err := f.ExpressionToRegister(expressions[i], registers[i])
|
err := f.ExpressionToRegister(expressions[i], registers[i])
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -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
|
|
||||||
}
|
|
11
tests/programs/return.q
Normal file
11
tests/programs/return.q
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
main() {
|
||||||
|
syscall(60, f(2))
|
||||||
|
}
|
||||||
|
|
||||||
|
f(x) {
|
||||||
|
return x + 1 + g(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
g(x) {
|
||||||
|
return x + 1
|
||||||
|
}
|
@ -20,6 +20,7 @@ var programs = []struct {
|
|||||||
{"square-sum.q", "", 25},
|
{"square-sum.q", "", 25},
|
||||||
{"chained-calls.q", "", 9},
|
{"chained-calls.q", "", 9},
|
||||||
{"nested-calls.q", "", 4},
|
{"nested-calls.q", "", 4},
|
||||||
|
{"return.q", "", 6},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPrograms(t *testing.T) {
|
func TestPrograms(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user