q/src/build/asm/Instructions.go

90 lines
2.2 KiB
Go

package asm
import "git.akyoto.dev/cli/q/src/build/cpu"
// RegisterNumber adds an instruction with a register and a number.
func (a *Assembler) RegisterNumber(mnemonic Mnemonic, reg cpu.Register, number int) {
a.Instructions = append(a.Instructions, Instruction{
Mnemonic: mnemonic,
Data: &RegisterNumber{
Register: reg,
Number: number,
},
})
}
// RegisterRegister adds an instruction using two registers.
func (a *Assembler) RegisterRegister(mnemonic Mnemonic, left cpu.Register, right cpu.Register) {
a.Instructions = append(a.Instructions, Instruction{
Mnemonic: mnemonic,
Data: &RegisterRegister{
Destination: left,
Source: right,
},
})
}
// Register adds an instruction using a single register.
func (a *Assembler) Register(mnemonic Mnemonic, register cpu.Register) {
a.Instructions = append(a.Instructions, Instruction{
Mnemonic: mnemonic,
Data: &Register{
Register: register,
},
})
}
// Label adds an instruction using a label.
func (a *Assembler) Label(mnemonic Mnemonic, name string) {
a.Instructions = append(a.Instructions, Instruction{
Mnemonic: mnemonic,
Data: &Label{
Name: name,
},
})
}
// Comment adds a comment at the current position.
func (a *Assembler) Comment(text string) {
a.Instructions = append(a.Instructions, Instruction{
Mnemonic: COMMENT,
Data: &Label{
Name: text,
},
})
}
// Call calls a function whose position is identified by a label.
func (a *Assembler) Call(name string) {
a.Instructions = append(a.Instructions, Instruction{
Mnemonic: CALL,
Data: &Label{
Name: name,
},
})
}
// Jump jumps to a position that is identified by a label.
func (a *Assembler) Jump(name string) {
a.Instructions = append(a.Instructions, Instruction{
Mnemonic: JUMP,
Data: &Label{
Name: name,
},
})
}
// Return returns back to the caller.
func (a *Assembler) Return() {
if len(a.Instructions) > 0 && a.Instructions[len(a.Instructions)-1].Mnemonic == RETURN {
return
}
a.Instructions = append(a.Instructions, Instruction{Mnemonic: RETURN})
}
// Syscall executes a kernel function.
func (a *Assembler) Syscall() {
a.Instructions = append(a.Instructions, Instruction{Mnemonic: SYSCALL})
}