Implemented length storage of allocated memory

This commit is contained in:
2025-02-09 14:14:41 +01:00
parent 1ff56e0856
commit 2b2e707520
27 changed files with 134 additions and 49 deletions

View File

@ -40,7 +40,7 @@ func (f *Function) CompileAssignArray(node *ast.Assign) error {
return err
}
memory.Offset = byte(offset)
memory.Offset = int8(offset)
} else {
_, indexRegister, isTemporary, err := f.Evaluate(index)

View File

@ -32,7 +32,7 @@ func (f *Function) CompileAssignField(node *ast.Assign) error {
memory := asm.Memory{
Base: variable.Register,
Offset: byte(field.Offset),
Offset: int8(field.Offset),
OffsetRegister: math.MaxUint8,
Length: byte(field.Type.Size()),
}

View File

@ -27,6 +27,14 @@ func (f *Function) CompileCall(root *expression.Expression) (*Function, error) {
name = nameNode.Token.Text(f.File.Bytes)
switch name {
case "len":
return &Function{
Output: []*Output{
{
Type: types.Int,
},
},
}, f.CompileLen(root)
case "syscall":
return nil, f.CompileSyscall(root)
case "new":

26
src/core/CompileLen.go Normal file
View File

@ -0,0 +1,26 @@
package core
import (
"math"
"git.akyoto.dev/cli/q/src/asm"
"git.akyoto.dev/cli/q/src/expression"
)
// CompileLen returns the length of a slice.
func (f *Function) CompileLen(root *expression.Expression) error {
_, register, isTemporary, err := f.Evaluate(root.Children[1])
if err != nil {
return err
}
f.SaveRegister(f.CPU.Output[0])
f.MemoryRegister(asm.LOAD, asm.Memory{Base: register, Offset: -8, OffsetRegister: math.MaxUint8, Length: 8}, f.CPU.Output[0])
if isTemporary {
f.FreeRegister(register)
}
return nil
}

View File

@ -40,7 +40,7 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
if node.Token.Kind == token.Array {
array := f.VariableByName(node.Children[0].Token.Text(f.File.Bytes))
offset, err := f.Number(node.Children[1].Token)
f.MemoryRegister(asm.LOAD, asm.Memory{Base: array.Register, Offset: byte(offset), Length: 1}, register)
f.MemoryRegister(asm.LOAD, asm.Memory{Base: array.Register, Offset: int8(offset), Length: 1}, register)
return types.Int, err
}
@ -51,7 +51,7 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
right := node.Children[1]
name = right.Token.Text(f.File.Bytes)
field := variable.Type.(*types.Pointer).To.(*types.Struct).FieldByName(name)
f.MemoryRegister(asm.LOAD, asm.Memory{Base: variable.Register, Offset: byte(field.Offset), Length: byte(field.Type.Size())}, register)
f.MemoryRegister(asm.LOAD, asm.Memory{Base: variable.Register, Offset: int8(field.Offset), Length: byte(field.Type.Size())}, register)
return field.Type, nil
}

View File

@ -1,6 +1,8 @@
package core
import (
"encoding/binary"
"git.akyoto.dev/cli/q/src/asm"
"git.akyoto.dev/cli/q/src/cpu"
"git.akyoto.dev/cli/q/src/errors"
@ -45,7 +47,12 @@ func (f *Function) TokenToRegister(t token.Token, register cpu.Register) (types.
case token.String:
data := t.Bytes(f.File.Bytes)
data = String(data)
label := f.AddBytes(data)
slice := make([]byte, len(data)+8)
binary.LittleEndian.PutUint64(slice, uint64(len(data)))
copy(slice[8:], data)
label := f.AddBytes(slice)
f.SaveRegister(register)
f.RegisterLabel(asm.MOVE, register, label)
return types.PointerAny, nil