Improved code generation
This commit is contained in:
parent
4d88260333
commit
3340a5824f
@ -1,5 +1,9 @@
|
|||||||
main() {
|
main() {
|
||||||
syscall(60, f(1) + f(2) + f(3))
|
exit(f(1) + f(2) + f(3))
|
||||||
|
}
|
||||||
|
|
||||||
|
exit(code) {
|
||||||
|
syscall(60, code)
|
||||||
}
|
}
|
||||||
|
|
||||||
f(x) {
|
f(x) {
|
||||||
|
@ -25,18 +25,21 @@ func (f *Function) CompileCall(root *expression.Expression) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Push
|
||||||
for _, register := range f.cpu.General {
|
for _, register := range f.cpu.General {
|
||||||
if !f.cpu.IsFree(register) {
|
if !f.cpu.IsFree(register) {
|
||||||
f.assembler.Register(asm.PUSH, register)
|
f.assembler.Register(asm.PUSH, register)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Call
|
||||||
if isSyscall {
|
if isSyscall {
|
||||||
f.assembler.Syscall()
|
f.assembler.Syscall()
|
||||||
} else {
|
} else {
|
||||||
f.assembler.Call(funcName)
|
f.assembler.Call(funcName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pop
|
||||||
for i := len(f.cpu.General) - 1; i >= 0; i-- {
|
for i := len(f.cpu.General) - 1; i >= 0; i-- {
|
||||||
register := f.cpu.General[i]
|
register := f.cpu.General[i]
|
||||||
|
|
||||||
|
@ -23,7 +23,8 @@ func (f *Function) Execute(operation token.Token, register cpu.Register, value *
|
|||||||
return f.ExecuteRegisterRegister(operation, register, f.cpu.Output[0])
|
return f.ExecuteRegisterRegister(operation, register, f.cpu.Output[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp := f.cpu.MustUseFree(f.cpu.General)
|
tmp := f.cpu.MustFindFree(f.cpu.General)
|
||||||
|
f.cpu.Use(tmp)
|
||||||
defer f.cpu.Free(tmp)
|
defer f.cpu.Free(tmp)
|
||||||
|
|
||||||
err := f.ExpressionToRegister(value, tmp)
|
err := f.ExpressionToRegister(value, tmp)
|
||||||
|
@ -8,19 +8,28 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// ExpressionToRegister puts the result of an expression into the specified register.
|
// ExpressionToRegister puts the result of an expression into the specified register.
|
||||||
func (f *Function) ExpressionToRegister(root *expression.Expression, register cpu.Register) error {
|
func (f *Function) ExpressionToRegister(node *expression.Expression, register cpu.Register) error {
|
||||||
if root.IsLeaf() {
|
if node.IsLeaf() {
|
||||||
return f.TokenToRegister(root.Token, register)
|
return f.TokenToRegister(node.Token, register)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ast.IsFunctionCall(root) {
|
if ast.IsFunctionCall(node) {
|
||||||
err := f.CompileCall(root)
|
err := f.CompileCall(node)
|
||||||
f.assembler.RegisterRegister(asm.MOVE, register, f.cpu.Output[0])
|
|
||||||
|
if register != f.cpu.Output[0] {
|
||||||
|
f.assembler.RegisterRegister(asm.MOVE, register, f.cpu.Output[0])
|
||||||
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
left := root.Children[0]
|
left := node.Children[0]
|
||||||
right := root.Children[1]
|
right := node.Children[1]
|
||||||
|
final := register
|
||||||
|
|
||||||
|
if OverwritesRegister(right, register) {
|
||||||
|
register = f.cpu.MustFindFree(f.cpu.General)
|
||||||
|
}
|
||||||
|
|
||||||
err := f.ExpressionToRegister(left, register)
|
err := f.ExpressionToRegister(left, register)
|
||||||
|
|
||||||
@ -28,6 +37,13 @@ func (f *Function) ExpressionToRegister(root *expression.Expression, register cp
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
f.SaveRegister(register)
|
f.cpu.Use(register)
|
||||||
return f.Execute(root.Token, register, right)
|
err = f.Execute(node.Token, register, right)
|
||||||
|
|
||||||
|
if register != final {
|
||||||
|
f.assembler.RegisterRegister(asm.MOVE, final, register)
|
||||||
|
}
|
||||||
|
|
||||||
|
f.cpu.Free(register)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
11
src/build/core/OverwritesRegister.go
Normal file
11
src/build/core/OverwritesRegister.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.akyoto.dev/cli/q/src/build/cpu"
|
||||||
|
"git.akyoto.dev/cli/q/src/build/expression"
|
||||||
|
)
|
||||||
|
|
||||||
|
// OverwritesRegister returns true if evaluating the expression would overwrite the given register.
|
||||||
|
func OverwritesRegister(expr *expression.Expression, register cpu.Register) bool {
|
||||||
|
return !expr.IsLeaf()
|
||||||
|
}
|
@ -27,7 +27,8 @@ func (f *Function) SaveRegister(register cpu.Register) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newRegister := f.cpu.MustUseFree(f.cpu.General)
|
newRegister := f.cpu.MustFindFree(f.cpu.General)
|
||||||
|
f.cpu.Use(newRegister)
|
||||||
|
|
||||||
if config.Comments {
|
if config.Comments {
|
||||||
f.assembler.Comment(fmt.Sprintf("save %s to %s", register, newRegister))
|
f.assembler.Comment(fmt.Sprintf("save %s to %s", register, newRegister))
|
||||||
|
@ -31,13 +31,12 @@ func (c *CPU) FindFree(registers []Register) (Register, bool) {
|
|||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CPU) MustUseFree(registers []Register) Register {
|
func (c *CPU) MustFindFree(registers []Register) Register {
|
||||||
register, exists := c.FindFree(registers)
|
register, exists := c.FindFree(registers)
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
panic("no free registers")
|
panic("no free registers")
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Use(register)
|
|
||||||
return register
|
return register
|
||||||
}
|
}
|
||||||
|
11
tests/programs/exit.q
Normal file
11
tests/programs/exit.q
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
main() {
|
||||||
|
exit(f(1) + f(2) + f(3))
|
||||||
|
}
|
||||||
|
|
||||||
|
exit(code) {
|
||||||
|
syscall(60, code)
|
||||||
|
}
|
||||||
|
|
||||||
|
f(x) {
|
||||||
|
return x + 1
|
||||||
|
}
|
@ -18,6 +18,15 @@ var programs = []struct {
|
|||||||
{"empty.q", "", 0},
|
{"empty.q", "", 0},
|
||||||
{"square-sum.q", "", 25},
|
{"square-sum.q", "", 25},
|
||||||
{"multi-calls.q", "", 9},
|
{"multi-calls.q", "", 9},
|
||||||
|
{"exit.q", "", 9},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrograms(t *testing.T) {
|
||||||
|
for _, test := range programs {
|
||||||
|
t.Run(test.Name, func(t *testing.T) {
|
||||||
|
run(t, filepath.Join("programs", test.Name), test.ExpectedOutput, test.ExpectedExitCode)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkPrograms(b *testing.B) {
|
func BenchmarkPrograms(b *testing.B) {
|
||||||
@ -33,14 +42,6 @@ func BenchmarkPrograms(b *testing.B) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPrograms(t *testing.T) {
|
|
||||||
for _, test := range programs {
|
|
||||||
t.Run(test.Name, func(t *testing.T) {
|
|
||||||
run(t, filepath.Join("programs", test.Name), test.ExpectedOutput, test.ExpectedExitCode)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// run builds and runs the file to check if the output matches the expected output.
|
// run builds and runs the file to check if the output matches the expected output.
|
||||||
func run(t *testing.T, name string, expectedOutput string, expectedExitCode int) {
|
func run(t *testing.T, name string, expectedOutput string, expectedExitCode int) {
|
||||||
b := build.New(name)
|
b := build.New(name)
|
||||||
|
Loading…
Reference in New Issue
Block a user