Added fibonacci example
This commit is contained in:
parent
1c019297d2
commit
5cbc3315a7
11
examples/fibonacci/fibonacci.q
Normal file
11
examples/fibonacci/fibonacci.q
Normal 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)
|
||||
}
|
@ -22,6 +22,7 @@ const (
|
||||
)
|
||||
|
||||
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}
|
||||
GeneralRegisters = []cpu.Register{RCX, RBX, RBP, R11, R12, R13, R14, R15}
|
||||
CallRegisters = SyscallRegisters
|
||||
|
@ -29,6 +29,12 @@ func (f *Function) CompileCall(root *expression.Expression) error {
|
||||
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)
|
||||
|
||||
if err != nil {
|
||||
@ -37,7 +43,7 @@ func (f *Function) CompileCall(root *expression.Expression) error {
|
||||
|
||||
// Push
|
||||
for _, register := range f.cpu.General {
|
||||
if !f.cpu.IsFree(register) {
|
||||
if f.cpu.IsUsed(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-- {
|
||||
register := f.cpu.General[i]
|
||||
|
||||
if !f.cpu.IsFree(register) {
|
||||
if f.cpu.IsUsed(register) {
|
||||
f.assembler.Register(asm.POP, register)
|
||||
}
|
||||
}
|
||||
|
@ -47,6 +47,7 @@ func (f *Function) AddVariable(variable *Variable) {
|
||||
}
|
||||
|
||||
f.variables[variable.Name] = variable
|
||||
f.cpu.Reserve(variable.Register)
|
||||
f.cpu.Use(variable.Register)
|
||||
}
|
||||
|
||||
@ -85,6 +86,7 @@ func (f *Function) storeVariableInRegister(name string, value *expression.Expres
|
||||
panic("no free registers")
|
||||
}
|
||||
|
||||
f.cpu.Reserve(reg)
|
||||
err := f.ExpressionToRegister(value, reg)
|
||||
|
||||
f.AddVariable(&Variable{
|
||||
|
@ -36,6 +36,7 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
|
||||
register = f.cpu.MustFindFree(f.cpu.General)
|
||||
}
|
||||
|
||||
f.cpu.Reserve(register)
|
||||
err := f.ExpressionToRegister(left, register)
|
||||
|
||||
if err != nil {
|
||||
@ -47,8 +48,8 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
|
||||
|
||||
if register != final {
|
||||
f.assembler.RegisterRegister(asm.MOVE, final, register)
|
||||
f.cpu.Free(register)
|
||||
}
|
||||
|
||||
f.cpu.Free(register)
|
||||
return err
|
||||
}
|
||||
|
@ -31,6 +31,7 @@ func NewFunction(name string, file *fs.File, body token.List) *Function {
|
||||
Instructions: make([]asm.Instruction, 0, 32),
|
||||
},
|
||||
cpu: cpu.CPU{
|
||||
All: x64.AllRegisters,
|
||||
Input: x64.CallRegisters,
|
||||
General: x64.GeneralRegisters,
|
||||
Syscall: x64.SyscallRegisters,
|
||||
|
37
src/build/core/SaveRegister.go
Normal file
37
src/build/core/SaveRegister.go
Normal 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
|
||||
}
|
@ -2,28 +2,45 @@ package cpu
|
||||
|
||||
// CPU represents the processor.
|
||||
type CPU struct {
|
||||
All []Register
|
||||
General []Register
|
||||
Syscall []Register
|
||||
Input []Register
|
||||
Output []Register
|
||||
usage uint64
|
||||
}
|
||||
|
||||
func (c *CPU) Use(reg Register) {
|
||||
c.usage |= (1 << reg)
|
||||
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)
|
||||
|
||||
|
@ -12,6 +12,7 @@ var examples = []struct {
|
||||
}{
|
||||
{"hello", "", 0},
|
||||
{"write", "ELF", 0},
|
||||
{"fibonacci", "", 55},
|
||||
}
|
||||
|
||||
func TestExamples(t *testing.T) {
|
||||
|
12
tests/programs/parameters.q
Normal file
12
tests/programs/parameters.q
Normal file
@ -0,0 +1,12 @@
|
||||
main() {
|
||||
syscall(60, f(1))
|
||||
}
|
||||
|
||||
f(x) {
|
||||
y := g()
|
||||
return x + y
|
||||
}
|
||||
|
||||
g() {
|
||||
return 2
|
||||
}
|
@ -21,6 +21,7 @@ var programs = []struct {
|
||||
{"square-sum.q", "", 25},
|
||||
{"chained-calls.q", "", 9},
|
||||
{"nested-calls.q", "", 4},
|
||||
{"parameters.q", "", 3},
|
||||
{"return.q", "", 6},
|
||||
{"reassign.q", "", 2},
|
||||
{"branch.q", "", 0},
|
||||
|
Loading…
Reference in New Issue
Block a user