Implemented function pointers as parameters
This commit is contained in:
parent
313302b9c8
commit
dd6d1cc16c
@ -2,18 +2,18 @@ import mem
|
||||
import sys
|
||||
|
||||
main() {
|
||||
start()
|
||||
start()
|
||||
start()
|
||||
start(thread)
|
||||
start(thread)
|
||||
start(thread)
|
||||
thread()
|
||||
}
|
||||
|
||||
start() {
|
||||
start(func Pointer) {
|
||||
size := 4096
|
||||
stack := mem.alloc(size)
|
||||
pointer := stack + size - 8
|
||||
store(pointer, 8, thread)
|
||||
sys.clone(0x100 | 0x200 | 0x400 | 0x800 | 0x8000 | 0x10000 | 0x80000000, pointer)
|
||||
rip := stack + size - 8
|
||||
store(rip, 8, func)
|
||||
sys.clone(0x100|0x200|0x400|0x800|0x8000|0x10000|0x80000000, rip)
|
||||
}
|
||||
|
||||
thread() {
|
||||
|
@ -225,24 +225,37 @@ func (a Assembler) Finalize(dlls dll.List) ([]byte, []byte) {
|
||||
opSize := len(code) - size - start
|
||||
regLabel := x.Data.(*RegisterLabel)
|
||||
|
||||
if !strings.HasPrefix(regLabel.Label, "data_") {
|
||||
panic("non-data moves not implemented yet")
|
||||
if strings.HasPrefix(regLabel.Label, "data_") {
|
||||
dataPointers = append(dataPointers, &Pointer{
|
||||
Position: Address(len(code) - size),
|
||||
OpSize: uint8(opSize),
|
||||
Size: uint8(size),
|
||||
Resolve: func() Address {
|
||||
destination, exists := dataLabels[regLabel.Label]
|
||||
|
||||
if !exists {
|
||||
panic("unknown label")
|
||||
}
|
||||
|
||||
return Address(destination)
|
||||
},
|
||||
})
|
||||
} else {
|
||||
funcPointers = append(funcPointers, &Pointer{
|
||||
Position: Address(len(code) - size),
|
||||
OpSize: uint8(opSize),
|
||||
Size: uint8(size),
|
||||
Resolve: func() Address {
|
||||
destination, exists := codeLabels[regLabel.Label]
|
||||
|
||||
if !exists {
|
||||
panic("unknown label")
|
||||
}
|
||||
|
||||
return Address(destination)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
dataPointers = append(dataPointers, &Pointer{
|
||||
Position: Address(len(code) - size),
|
||||
OpSize: uint8(opSize),
|
||||
Size: uint8(size),
|
||||
Resolve: func() Address {
|
||||
destination, exists := dataLabels[regLabel.Label]
|
||||
|
||||
if !exists {
|
||||
panic("unknown label")
|
||||
}
|
||||
|
||||
return Address(destination)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
case NEGATE:
|
||||
|
@ -15,8 +15,24 @@ import (
|
||||
func (f *Function) ExpressionToMemory(node *expression.Expression, memory asm.Memory) (*types.Type, error) {
|
||||
if node.IsLeaf() {
|
||||
if node.Token.Kind == token.Identifier {
|
||||
f.MemoryLabel(asm.STORE, memory, fmt.Sprintf("%s.%s", f.Package, node.Token.Text(f.File.Bytes)))
|
||||
return types.Pointer, nil
|
||||
name := node.Token.Text(f.File.Bytes)
|
||||
variable := f.VariableByName(name)
|
||||
|
||||
if variable != nil {
|
||||
f.UseVariable(variable)
|
||||
f.MemoryRegister(asm.STORE, memory, variable.Register)
|
||||
return types.Pointer, nil
|
||||
}
|
||||
|
||||
uniqueName := fmt.Sprintf("%s.%s", f.Package, name)
|
||||
_, exists := f.Functions[uniqueName]
|
||||
|
||||
if exists {
|
||||
f.MemoryLabel(asm.STORE, memory, uniqueName)
|
||||
return types.Pointer, nil
|
||||
}
|
||||
|
||||
return nil, errors.New(&errors.UnknownIdentifier{Name: name}, f.File, node.Token.Position)
|
||||
}
|
||||
|
||||
if node.Token.IsNumeric() {
|
||||
|
@ -1,6 +1,8 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/asm"
|
||||
"git.akyoto.dev/cli/q/src/cpu"
|
||||
"git.akyoto.dev/cli/q/src/errors"
|
||||
@ -16,14 +18,23 @@ func (f *Function) TokenToRegister(t token.Token, register cpu.Register) (*types
|
||||
name := t.Text(f.File.Bytes)
|
||||
variable := f.VariableByName(name)
|
||||
|
||||
if variable == nil {
|
||||
return nil, errors.New(&errors.UnknownIdentifier{Name: name}, f.File, t.Position)
|
||||
if variable != nil {
|
||||
f.UseVariable(variable)
|
||||
f.SaveRegister(register)
|
||||
f.RegisterRegister(asm.MOVE, register, variable.Register)
|
||||
return variable.Type, nil
|
||||
}
|
||||
|
||||
f.UseVariable(variable)
|
||||
f.SaveRegister(register)
|
||||
f.RegisterRegister(asm.MOVE, register, variable.Register)
|
||||
return variable.Type, nil
|
||||
uniqueName := fmt.Sprintf("%s.%s", f.Package, name)
|
||||
_, exists := f.Functions[uniqueName]
|
||||
|
||||
if exists {
|
||||
f.SaveRegister(register)
|
||||
f.RegisterLabel(asm.MOVE, register, uniqueName)
|
||||
return types.Pointer, nil
|
||||
}
|
||||
|
||||
return nil, errors.New(&errors.UnknownIdentifier{Name: name}, f.File, t.Position)
|
||||
|
||||
case token.Number, token.Rune:
|
||||
number, err := f.Number(t)
|
||||
|
@ -1,12 +1,12 @@
|
||||
main() {
|
||||
f(1, 2, 3, 4, 5, 6)
|
||||
f1(1, 2, 3, 4, 5, 6)
|
||||
}
|
||||
|
||||
f(a Int, b Int, c Int, d Int, e Int, f Int) {
|
||||
g(f, e, d, c, b, a)
|
||||
f1(a Int, b Int, c Int, d Int, e Int, f Int) {
|
||||
f2(f, e, d, c, b, a)
|
||||
}
|
||||
|
||||
g(a Int, b Int, c Int, d Int, e Int, f Int) {
|
||||
f2(a Int, b Int, c Int, d Int, e Int, f Int) {
|
||||
assert a == 6
|
||||
assert b == 5
|
||||
assert c == 4
|
||||
|
Loading…
Reference in New Issue
Block a user