q/src/build/cpu/CPU.go

61 lines
1.4 KiB
Go

package cpu
// CPU represents the processor.
type CPU struct {
All []Register
General []Register
Syscall []Register
Input []Register
Output []Register
Reserved uint64
Used uint64
}
// Free will reset the reserved and used status which means the register can be allocated again.
func (c *CPU) Free(reg Register) {
c.Used &= ^(1 << reg)
c.Reserved &= ^(1 << reg)
}
// IsReserved returns true if the register was marked for future use.
func (c *CPU) IsReserved(reg Register) bool {
return c.Reserved&(1<<reg) != 0
}
// IsUsed returns true if the register is currently in use and holds a value.
func (c *CPU) IsUsed(reg Register) bool {
return c.Used&(1<<reg) != 0
}
// Reserve reserves a register for future use.
func (c *CPU) Reserve(reg Register) {
c.Reserved |= (1 << reg)
}
// Use marks a register to be currently in use which means it must be preserved across function calls.
func (c *CPU) Use(reg Register) {
c.Used |= (1 << reg)
}
// FindFree tries to find a free register in the given slice of registers.
func (c *CPU) FindFree(registers []Register) (Register, bool) {
for _, reg := range registers {
if !c.IsReserved(reg) {
return reg, true
}
}
return 0, false
}
// MustFindFree tries to find a free register and panics if it could not be found.
func (c *CPU) MustFindFree(registers []Register) Register {
register, exists := c.FindFree(registers)
if !exists {
panic("no free registers")
}
return register
}