Fixed incorrect number of history entries
This commit is contained in:
parent
c10395eddc
commit
be384c5136
@ -2,8 +2,12 @@ package asm
|
|||||||
|
|
||||||
import "git.akyoto.dev/cli/q/src/cpu"
|
import "git.akyoto.dev/cli/q/src/cpu"
|
||||||
|
|
||||||
// unnecessary returns true if the register/register operation can be skipped.
|
// CanSkip returns true if the register/register operation can be skipped.
|
||||||
func (a *Assembler) unnecessary(mnemonic Mnemonic, left cpu.Register, right cpu.Register) bool {
|
func (a *Assembler) CanSkip(mnemonic Mnemonic, left cpu.Register, right cpu.Register) bool {
|
||||||
|
if mnemonic == MOVE && left == right {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
if len(a.Instructions) == 0 {
|
if len(a.Instructions) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
16
src/asm/CanSkipReturn.go
Normal file
16
src/asm/CanSkipReturn.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package asm
|
||||||
|
|
||||||
|
// CanSkipReturn returns true if the return operation can be skipped.
|
||||||
|
func (a *Assembler) CanSkipReturn() bool {
|
||||||
|
if len(a.Instructions) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
lastMnemonic := a.Instructions[len(a.Instructions)-1].Mnemonic
|
||||||
|
|
||||||
|
if lastMnemonic == RETURN || lastMnemonic == JUMP {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
@ -32,14 +32,6 @@ func (a *Assembler) DLLCall(name string) {
|
|||||||
|
|
||||||
// Return returns back to the caller.
|
// Return returns back to the caller.
|
||||||
func (a *Assembler) Return() {
|
func (a *Assembler) Return() {
|
||||||
if len(a.Instructions) > 0 {
|
|
||||||
lastMnemonic := a.Instructions[len(a.Instructions)-1].Mnemonic
|
|
||||||
|
|
||||||
if lastMnemonic == RETURN || lastMnemonic == JUMP {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
a.Instructions = append(a.Instructions, Instruction{Mnemonic: RETURN})
|
a.Instructions = append(a.Instructions, Instruction{Mnemonic: RETURN})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,10 +19,6 @@ func (data *RegisterRegister) String() string {
|
|||||||
|
|
||||||
// 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{
|
||||||
|
@ -8,9 +8,8 @@ import (
|
|||||||
|
|
||||||
// CompileReturn compiles a return instruction.
|
// CompileReturn compiles a return instruction.
|
||||||
func (f *Function) CompileReturn(node *ast.Return) error {
|
func (f *Function) CompileReturn(node *ast.Return) error {
|
||||||
defer f.Return()
|
|
||||||
|
|
||||||
if len(node.Values) == 0 {
|
if len(node.Values) == 0 {
|
||||||
|
f.Return()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,12 +26,13 @@ func (f *Function) CompileReturn(node *ast.Return) error {
|
|||||||
|
|
||||||
if !types.Is(typ, f.Output[i].Type) {
|
if !types.Is(typ, f.Output[i].Type) {
|
||||||
if f.Package == "mem" && f.Name == "alloc" {
|
if f.Package == "mem" && f.Name == "alloc" {
|
||||||
return nil
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors.New(&errors.TypeMismatch{Encountered: typ.Name(), Expected: f.Output[i].Type.Name(), ParameterName: "", IsReturn: true}, f.File, node.Values[i].Token.Position)
|
return errors.New(&errors.TypeMismatch{Encountered: typ.Name(), Expected: f.Output[i].Type.Name(), ParameterName: "", IsReturn: true}, f.File, node.Values[i].Token.Position)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
f.Return()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -7,5 +7,10 @@ import (
|
|||||||
|
|
||||||
func (f *Machine) MemoryRegister(mnemonic asm.Mnemonic, a asm.Memory, b cpu.Register) {
|
func (f *Machine) MemoryRegister(mnemonic asm.Mnemonic, a asm.Memory, b cpu.Register) {
|
||||||
f.Assembler.MemoryRegister(mnemonic, a, b)
|
f.Assembler.MemoryRegister(mnemonic, a, b)
|
||||||
|
|
||||||
|
if mnemonic == asm.LOAD {
|
||||||
|
f.UseRegister(b)
|
||||||
|
}
|
||||||
|
|
||||||
f.postInstruction()
|
f.postInstruction()
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,6 @@ func (f *Machine) RegisterNumber(mnemonic asm.Mnemonic, a cpu.Register, b int) {
|
|||||||
f.Assembler.RegisterNumber(asm.MOVE, tmp, b)
|
f.Assembler.RegisterNumber(asm.MOVE, tmp, b)
|
||||||
f.UseRegister(tmp)
|
f.UseRegister(tmp)
|
||||||
f.postInstruction()
|
f.postInstruction()
|
||||||
f.Assembler.RegisterRegister(mnemonic, a, tmp)
|
f.RegisterRegister(mnemonic, a, tmp)
|
||||||
f.postInstruction()
|
|
||||||
f.FreeRegister(tmp)
|
f.FreeRegister(tmp)
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (f *Machine) RegisterRegister(mnemonic asm.Mnemonic, a cpu.Register, b cpu.Register) {
|
func (f *Machine) RegisterRegister(mnemonic asm.Mnemonic, a cpu.Register, b cpu.Register) {
|
||||||
if mnemonic == asm.MOVE && a == b {
|
if f.Assembler.CanSkip(mnemonic, a, b) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,13 @@
|
|||||||
package register
|
package register
|
||||||
|
|
||||||
func (f *Machine) Return() {
|
func (f *Machine) Return() {
|
||||||
|
if f.Assembler.CanSkipReturn() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
f.Assembler.Return()
|
f.Assembler.Return()
|
||||||
|
scope := f.CurrentScope()
|
||||||
|
scope.Reserved = 0
|
||||||
|
scope.Used = 0
|
||||||
f.postInstruction()
|
f.postInstruction()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user