Added scope package
This commit is contained in:
@ -1,10 +1,7 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/build/asm"
|
||||
"git.akyoto.dev/cli/q/src/build/config"
|
||||
"git.akyoto.dev/cli/q/src/build/cpu"
|
||||
)
|
||||
|
||||
@ -15,12 +12,7 @@ func (f *Function) AddLabel(label string) {
|
||||
|
||||
func (f *Function) Call(label string) {
|
||||
f.Assembler.Call(label)
|
||||
f.Scope().Use(f.cpu.Output[0])
|
||||
f.postInstruction()
|
||||
}
|
||||
|
||||
func (f *Function) Comment(format string, args ...any) {
|
||||
f.Assembler.Comment(fmt.Sprintf(format, args...))
|
||||
f.CurrentScope().Use(f.cpu.Output[0])
|
||||
f.postInstruction()
|
||||
}
|
||||
|
||||
@ -38,35 +30,35 @@ func (f *Function) Register(mnemonic asm.Mnemonic, a cpu.Register) {
|
||||
f.Assembler.Register(mnemonic, a)
|
||||
|
||||
if mnemonic == asm.POP {
|
||||
f.Scope().Use(a)
|
||||
f.CurrentScope().Use(a)
|
||||
}
|
||||
|
||||
f.postInstruction()
|
||||
}
|
||||
|
||||
func (f *Function) RegisterNumber(mnemonic asm.Mnemonic, a cpu.Register, b int) {
|
||||
if f.Scope().IsUsed(a) && isDestructive(mnemonic) {
|
||||
if f.CurrentScope().IsUsed(a) && isDestructive(mnemonic) {
|
||||
f.SaveRegister(a)
|
||||
}
|
||||
|
||||
f.Assembler.RegisterNumber(mnemonic, a, b)
|
||||
|
||||
if mnemonic == asm.MOVE {
|
||||
f.Scope().Use(a)
|
||||
f.CurrentScope().Use(a)
|
||||
}
|
||||
|
||||
f.postInstruction()
|
||||
}
|
||||
|
||||
func (f *Function) RegisterLabel(mnemonic asm.Mnemonic, register cpu.Register, label string) {
|
||||
if f.Scope().IsUsed(register) && isDestructive(mnemonic) {
|
||||
if f.CurrentScope().IsUsed(register) && isDestructive(mnemonic) {
|
||||
f.SaveRegister(register)
|
||||
}
|
||||
|
||||
f.Assembler.RegisterLabel(mnemonic, register, label)
|
||||
|
||||
if mnemonic == asm.MOVE {
|
||||
f.Scope().Use(register)
|
||||
f.CurrentScope().Use(register)
|
||||
}
|
||||
|
||||
f.postInstruction()
|
||||
@ -77,14 +69,14 @@ func (f *Function) RegisterRegister(mnemonic asm.Mnemonic, a cpu.Register, b cpu
|
||||
return
|
||||
}
|
||||
|
||||
if f.Scope().IsUsed(a) && isDestructive(mnemonic) {
|
||||
if f.CurrentScope().IsUsed(a) && isDestructive(mnemonic) {
|
||||
f.SaveRegister(a)
|
||||
}
|
||||
|
||||
f.Assembler.RegisterRegister(mnemonic, a, b)
|
||||
|
||||
if mnemonic == asm.MOVE {
|
||||
f.Scope().Use(a)
|
||||
f.CurrentScope().Use(a)
|
||||
}
|
||||
|
||||
f.postInstruction()
|
||||
@ -97,18 +89,10 @@ func (f *Function) Return() {
|
||||
|
||||
func (f *Function) Syscall() {
|
||||
f.Assembler.Syscall()
|
||||
f.Scope().Use(f.cpu.Output[0])
|
||||
f.CurrentScope().Use(f.cpu.Output[0])
|
||||
f.postInstruction()
|
||||
}
|
||||
|
||||
func (f *Function) postInstruction() {
|
||||
if !config.Assembler {
|
||||
return
|
||||
}
|
||||
|
||||
f.registerHistory = append(f.registerHistory, f.Scope().Used)
|
||||
}
|
||||
|
||||
func isDestructive(mnemonic asm.Mnemonic) bool {
|
||||
switch mnemonic {
|
||||
case asm.MOVE, asm.ADD, asm.SUB, asm.MUL, asm.DIV:
|
||||
|
Reference in New Issue
Block a user