Improved code generation
This commit is contained in:
parent
8103faa8b6
commit
1204591cdc
@ -35,6 +35,12 @@ func (f *Function) CompileCall(root *expression.Expression) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
f.SaveRegister(f.cpu.Output[0])
|
||||||
|
|
||||||
|
for _, register := range registers {
|
||||||
|
f.SaveRegister(register)
|
||||||
|
}
|
||||||
|
|
||||||
// Push
|
// Push
|
||||||
for _, register := range f.cpu.General {
|
for _, register := range f.cpu.General {
|
||||||
if f.cpu.IsUsed(register) {
|
if f.cpu.IsUsed(register) {
|
||||||
|
@ -34,6 +34,8 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
|
|||||||
|
|
||||||
if f.UsesRegister(right, register) {
|
if f.UsesRegister(right, register) {
|
||||||
register = f.cpu.MustFindFree(f.cpu.General)
|
register = f.cpu.MustFindFree(f.cpu.General)
|
||||||
|
} else {
|
||||||
|
f.SaveRegister(register)
|
||||||
}
|
}
|
||||||
|
|
||||||
f.cpu.Reserve(register)
|
f.cpu.Reserve(register)
|
||||||
|
@ -47,13 +47,6 @@ func NewFunction(name string, file *fs.File, body token.List) *Function {
|
|||||||
func (f *Function) Compile() {
|
func (f *Function) Compile() {
|
||||||
defer close(f.finished)
|
defer close(f.finished)
|
||||||
f.assembler.Label(asm.LABEL, f.Name)
|
f.assembler.Label(asm.LABEL, f.Name)
|
||||||
|
|
||||||
for _, register := range f.cpu.Input {
|
|
||||||
if f.cpu.IsUsed(register) {
|
|
||||||
f.SaveRegister(register)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
f.err = f.CompileTokens(f.Body)
|
f.err = f.CompileTokens(f.Body)
|
||||||
f.assembler.Return()
|
f.assembler.Return()
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,10 @@ import (
|
|||||||
|
|
||||||
// SaveRegister attempts to move a variable occupying this register to another register.
|
// SaveRegister attempts to move a variable occupying this register to another register.
|
||||||
func (f *Function) SaveRegister(register cpu.Register) {
|
func (f *Function) SaveRegister(register cpu.Register) {
|
||||||
|
if !f.cpu.IsUsed(register) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var variable *Variable
|
var variable *Variable
|
||||||
|
|
||||||
for _, v := range f.variables {
|
for _, v := range f.variables {
|
||||||
|
Loading…
Reference in New Issue
Block a user