Implemented a basic optimization

This commit is contained in:
Eduard Urbach 2024-07-09 11:43:33 +02:00
parent 4a7bb9500a
commit 59d6653eba
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
8 changed files with 48 additions and 14 deletions

View File

@ -15,6 +15,10 @@ func (a *Assembler) RegisterNumber(mnemonic Mnemonic, reg cpu.Register, number i
// RegisterRegister adds an instruction using two registers. // RegisterRegister adds an instruction using two registers.
func (a *Assembler) RegisterRegister(mnemonic Mnemonic, left cpu.Register, right cpu.Register) { func (a *Assembler) RegisterRegister(mnemonic Mnemonic, left cpu.Register, right cpu.Register) {
if a.unnecessary(mnemonic, left, right) {
return
}
a.Instructions = append(a.Instructions, Instruction{ a.Instructions = append(a.Instructions, Instruction{
Mnemonic: mnemonic, Mnemonic: mnemonic,
Data: &RegisterRegister{ Data: &RegisterRegister{

View File

@ -0,0 +1,34 @@
package asm
import "git.akyoto.dev/cli/q/src/build/cpu"
// unnecessary returns true if the register/register operation can be skipped.
func (a *Assembler) unnecessary(mnemonic Mnemonic, left cpu.Register, right cpu.Register) bool {
if mnemonic == MOVE && left == right {
return true
}
if len(a.Instructions) == 0 {
return false
}
last := a.Instructions[len(a.Instructions)-1]
if mnemonic == MOVE && last.Mnemonic == MOVE {
data, isRegReg := last.Data.(*RegisterRegister)
if !isRegReg {
return false
}
if data.Destination == left && data.Source == right {
return true
}
if data.Destination == right && data.Source == left {
return true
}
}
return false
}

View File

@ -29,12 +29,6 @@ func (f *Function) CompileCall(root *expression.Expression) error {
registers = f.cpu.Syscall[:len(parameters)] registers = f.cpu.Syscall[:len(parameters)]
} }
for _, register := range f.cpu.Input {
if f.cpu.IsUsed(register) {
f.SaveRegister(register)
}
}
err := f.ExpressionsToRegisters(parameters, registers) err := f.ExpressionsToRegisters(parameters, registers)
if err != nil { if err != nil {

View File

@ -26,9 +26,7 @@ func (f *Function) ExecuteRegisterRegister(operation token.Token, destination cp
f.assembler.RegisterRegister(asm.COMPARE, destination, source) f.assembler.RegisterRegister(asm.COMPARE, destination, source)
case "=": case "=":
if destination != source {
f.assembler.RegisterRegister(asm.MOVE, destination, source) f.assembler.RegisterRegister(asm.MOVE, destination, source)
}
default: default:
return errors.New(&errors.InvalidOperator{Operator: operation.Text()}, f.File, operation.Position) return errors.New(&errors.InvalidOperator{Operator: operation.Text()}, f.File, operation.Position)

View File

@ -47,6 +47,13 @@ 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()
} }

View File

@ -21,10 +21,7 @@ func (f *Function) TokenToRegister(t token.Token, register cpu.Register) error {
return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, t.Position) return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, t.Position)
} }
if register != variable.Register {
f.assembler.RegisterRegister(asm.MOVE, register, variable.Register) f.assembler.RegisterRegister(asm.MOVE, register, variable.Register)
}
f.useVariable(variable) f.useVariable(variable)
return nil return nil

View File

@ -21,7 +21,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},
{"parameters.q", "", 3}, {"overwrite.q", "", 3},
{"reuse.q", "", 3}, {"reuse.q", "", 3},
{"return.q", "", 6}, {"return.q", "", 6},
{"reassign.q", "", 2}, {"reassign.q", "", 2},