Moved register state to scopes

This commit is contained in:
2024-07-16 15:30:28 +02:00
parent d1ccd60139
commit d6d018c5c5
22 changed files with 230 additions and 129 deletions

View File

@ -15,7 +15,7 @@ func (f *Function) AddLabel(label string) {
func (f *Function) Call(label string) {
f.assembler.Call(label)
f.cpu.Use(f.cpu.Output[0])
f.Scope().Use(f.cpu.Output[0])
f.postInstruction()
}
@ -33,35 +33,35 @@ func (f *Function) Register(mnemonic asm.Mnemonic, a cpu.Register) {
f.assembler.Register(mnemonic, a)
if mnemonic == asm.POP {
f.cpu.Use(a)
f.Scope().Use(a)
}
f.postInstruction()
}
func (f *Function) RegisterNumber(mnemonic asm.Mnemonic, a cpu.Register, b int) {
if f.cpu.IsUsed(a) && isDestructive(mnemonic) {
if f.Scope().IsUsed(a) && isDestructive(mnemonic) {
f.SaveRegister(a)
}
f.assembler.RegisterNumber(mnemonic, a, b)
if mnemonic == asm.MOVE {
f.cpu.Use(a)
f.Scope().Use(a)
}
f.postInstruction()
}
func (f *Function) RegisterLabel(mnemonic asm.Mnemonic, register cpu.Register, label string) {
if f.cpu.IsUsed(register) && isDestructive(mnemonic) {
if f.Scope().IsUsed(register) && isDestructive(mnemonic) {
f.SaveRegister(register)
}
f.assembler.RegisterLabel(mnemonic, register, label)
if mnemonic == asm.MOVE {
f.cpu.Use(register)
f.Scope().Use(register)
}
f.postInstruction()
@ -72,14 +72,14 @@ func (f *Function) RegisterRegister(mnemonic asm.Mnemonic, a cpu.Register, b cpu
return
}
if f.cpu.IsUsed(a) && isDestructive(mnemonic) {
if f.Scope().IsUsed(a) && isDestructive(mnemonic) {
f.SaveRegister(a)
}
f.assembler.RegisterRegister(mnemonic, a, b)
if mnemonic == asm.MOVE {
f.cpu.Use(a)
f.Scope().Use(a)
}
f.postInstruction()
@ -92,7 +92,7 @@ func (f *Function) Return() {
func (f *Function) Syscall() {
f.assembler.Syscall()
f.cpu.Use(f.cpu.Output[0])
f.Scope().Use(f.cpu.Output[0])
f.postInstruction()
}
@ -101,7 +101,7 @@ func (f *Function) postInstruction() {
return
}
f.registerHistory = append(f.registerHistory, f.cpu.Used)
f.registerHistory = append(f.registerHistory, f.Scope().Used)
}
func isDestructive(mnemonic asm.Mnemonic) bool {