Implemented a basic optimization
This commit is contained in:
parent
58d3aaf21d
commit
6102ab466a
8 changed files with 48 additions and 14 deletions
|
@ -15,6 +15,10 @@ func (a *Assembler) RegisterNumber(mnemonic Mnemonic, reg cpu.Register, number i
|
|||
|
||||
// RegisterRegister adds an instruction using two registers.
|
||||
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{
|
||||
Mnemonic: mnemonic,
|
||||
Data: &RegisterRegister{
|
||||
|
|
34
src/build/asm/Optimizer.go
Normal file
34
src/build/asm/Optimizer.go
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue