Added fibonacci example

This commit is contained in:
Eduard Urbach 2024-07-09 10:28:14 +02:00
parent 1c019297d2
commit 5cbc3315a7
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
11 changed files with 107 additions and 16 deletions

View File

@ -0,0 +1,11 @@
main() {
syscall(60, fibonacci(10))
}
fibonacci(x) {
if x == 1 || x == 0 {
return x
}
return fibonacci(x - 1) + fibonacci(x - 2)
}

View File

@ -22,6 +22,7 @@ const (
) )
var ( var (
AllRegisters = []cpu.Register{RAX, RCX, RDX, RBX, RSP, RBP, RSI, RDI, R8, R9, R10, R11, R12, R13, R14, R15}
SyscallRegisters = []cpu.Register{RAX, RDI, RSI, RDX, R10, R8, R9} SyscallRegisters = []cpu.Register{RAX, RDI, RSI, RDX, R10, R8, R9}
GeneralRegisters = []cpu.Register{RCX, RBX, RBP, R11, R12, R13, R14, R15} GeneralRegisters = []cpu.Register{RCX, RBX, RBP, R11, R12, R13, R14, R15}
CallRegisters = SyscallRegisters CallRegisters = SyscallRegisters

View File

@ -29,6 +29,12 @@ func (f *Function) CompileCall(root *expression.Expression) error {
registers = f.cpu.Syscall[:len(parameters)] registers = f.cpu.Syscall[:len(parameters)]
} }
for _, register := range f.cpu.Input {
if f.cpu.IsUsed(register) {
f.SaveRegister(register)
}
}
err := f.ExpressionsToRegisters(parameters, registers) err := f.ExpressionsToRegisters(parameters, registers)
if err != nil { if err != nil {
@ -37,7 +43,7 @@ func (f *Function) CompileCall(root *expression.Expression) error {
// Push // Push
for _, register := range f.cpu.General { for _, register := range f.cpu.General {
if !f.cpu.IsFree(register) { if f.cpu.IsUsed(register) {
f.assembler.Register(asm.PUSH, register) f.assembler.Register(asm.PUSH, register)
} }
} }
@ -53,7 +59,7 @@ func (f *Function) CompileCall(root *expression.Expression) error {
for i := len(f.cpu.General) - 1; i >= 0; i-- { for i := len(f.cpu.General) - 1; i >= 0; i-- {
register := f.cpu.General[i] register := f.cpu.General[i]
if !f.cpu.IsFree(register) { if f.cpu.IsUsed(register) {
f.assembler.Register(asm.POP, register) f.assembler.Register(asm.POP, register)
} }
} }

View File

@ -47,6 +47,7 @@ func (f *Function) AddVariable(variable *Variable) {
} }
f.variables[variable.Name] = variable f.variables[variable.Name] = variable
f.cpu.Reserve(variable.Register)
f.cpu.Use(variable.Register) f.cpu.Use(variable.Register)
} }
@ -85,6 +86,7 @@ func (f *Function) storeVariableInRegister(name string, value *expression.Expres
panic("no free registers") panic("no free registers")
} }
f.cpu.Reserve(reg)
err := f.ExpressionToRegister(value, reg) err := f.ExpressionToRegister(value, reg)
f.AddVariable(&Variable{ f.AddVariable(&Variable{

View File

@ -36,6 +36,7 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
register = f.cpu.MustFindFree(f.cpu.General) register = f.cpu.MustFindFree(f.cpu.General)
} }
f.cpu.Reserve(register)
err := f.ExpressionToRegister(left, register) err := f.ExpressionToRegister(left, register)
if err != nil { if err != nil {
@ -47,8 +48,8 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
if register != final { if register != final {
f.assembler.RegisterRegister(asm.MOVE, final, register) f.assembler.RegisterRegister(asm.MOVE, final, register)
f.cpu.Free(register)
} }
f.cpu.Free(register)
return err return err
} }

View File

@ -31,6 +31,7 @@ func NewFunction(name string, file *fs.File, body token.List) *Function {
Instructions: make([]asm.Instruction, 0, 32), Instructions: make([]asm.Instruction, 0, 32),
}, },
cpu: cpu.CPU{ cpu: cpu.CPU{
All: x64.AllRegisters,
Input: x64.CallRegisters, Input: x64.CallRegisters,
General: x64.GeneralRegisters, General: x64.GeneralRegisters,
Syscall: x64.SyscallRegisters, Syscall: x64.SyscallRegisters,

View File

@ -0,0 +1,37 @@
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"
)
// SaveRegister attempts to move a variable occupying this register to another register.
func (f *Function) SaveRegister(register cpu.Register) {
var variable *Variable
for _, v := range f.variables {
if v.Register == register {
variable = v
break
}
}
if variable == nil || variable.Alive == 0 {
return
}
newRegister := f.cpu.MustFindFree(f.cpu.General)
f.cpu.Reserve(newRegister)
f.cpu.Use(newRegister)
if config.Comments {
f.assembler.Comment(fmt.Sprintf("save %s to %s", register, newRegister))
}
f.assembler.RegisterRegister(asm.MOVE, newRegister, register)
f.cpu.Free(register)
variable.Register = newRegister
}

View File

@ -2,28 +2,45 @@ package cpu
// CPU represents the processor. // CPU represents the processor.
type CPU struct { type CPU struct {
General []Register All []Register
Syscall []Register General []Register
Input []Register Syscall []Register
Output []Register Input []Register
usage uint64 Output []Register
} reserved uint64
used uint64
func (c *CPU) Use(reg Register) {
c.usage |= (1 << reg)
} }
// Free will reset the reserved and used status which means the register can be allocated again.
func (c *CPU) Free(reg Register) { 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 { // IsReserved returns true if the register was marked for future use.
return c.usage&(1<<reg) == 0 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) { func (c *CPU) FindFree(registers []Register) (Register, bool) {
for _, reg := range registers { for _, reg := range registers {
if c.IsFree(reg) { if !c.IsReserved(reg) {
return reg, true return reg, true
} }
} }
@ -31,6 +48,7 @@ func (c *CPU) FindFree(registers []Register) (Register, bool) {
return 0, false 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 { func (c *CPU) MustFindFree(registers []Register) Register {
register, exists := c.FindFree(registers) register, exists := c.FindFree(registers)

View File

@ -12,6 +12,7 @@ var examples = []struct {
}{ }{
{"hello", "", 0}, {"hello", "", 0},
{"write", "ELF", 0}, {"write", "ELF", 0},
{"fibonacci", "", 55},
} }
func TestExamples(t *testing.T) { func TestExamples(t *testing.T) {

View File

@ -0,0 +1,12 @@
main() {
syscall(60, f(1))
}
f(x) {
y := g()
return x + y
}
g() {
return 2
}

View File

@ -21,6 +21,7 @@ var programs = []struct {
{"square-sum.q", "", 25}, {"square-sum.q", "", 25},
{"chained-calls.q", "", 9}, {"chained-calls.q", "", 9},
{"nested-calls.q", "", 4}, {"nested-calls.q", "", 4},
{"parameters.q", "", 3},
{"return.q", "", 6}, {"return.q", "", 6},
{"reassign.q", "", 2}, {"reassign.q", "", 2},
{"branch.q", "", 0}, {"branch.q", "", 0},