Added fibonacci example

This commit is contained in:
2024-07-09 10:28:14 +02:00
parent 1c019297d2
commit 5cbc3315a7
11 changed files with 107 additions and 16 deletions

View File

@ -2,28 +2,45 @@ package cpu
// CPU represents the processor.
type CPU struct {
General []Register
Syscall []Register
Input []Register
Output []Register
usage uint64
}
func (c *CPU) Use(reg Register) {
c.usage |= (1 << reg)
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.usage &= ^(1 << reg)
c.used &= ^(1 << reg)
c.reserved &= ^(1 << reg)
}
func (c *CPU) IsFree(reg Register) bool {
return c.usage&(1<<reg) == 0
// 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.IsFree(reg) {
if !c.IsReserved(reg) {
return reg, true
}
}
@ -31,6 +48,7 @@ func (c *CPU) FindFree(registers []Register) (Register, bool) {
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)