Implemented loads with register offsets
This commit is contained in:
70
src/core/ArrayElementToRegister.go
Normal file
70
src/core/ArrayElementToRegister.go
Normal file
@ -0,0 +1,70 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/asm"
|
||||
"git.akyoto.dev/cli/q/src/cpu"
|
||||
"git.akyoto.dev/cli/q/src/errors"
|
||||
"git.akyoto.dev/cli/q/src/expression"
|
||||
"git.akyoto.dev/cli/q/src/token"
|
||||
"git.akyoto.dev/cli/q/src/types"
|
||||
)
|
||||
|
||||
// ArrayElementToRegister moves the value of an array element into the given register.
|
||||
func (f *Function) ArrayElementToRegister(node *expression.Expression, register cpu.Register) (types.Type, error) {
|
||||
name := node.Children[0].Token.Text(f.File.Bytes)
|
||||
array := f.VariableByName(name)
|
||||
|
||||
if array == nil {
|
||||
return nil, errors.New(&errors.UnknownIdentifier{Name: name}, f.File, node.Children[0].Token.Position)
|
||||
}
|
||||
|
||||
index := node.Children[1]
|
||||
|
||||
memory := asm.Memory{
|
||||
Base: array.Register,
|
||||
Offset: 0,
|
||||
OffsetRegister: math.MaxUint8,
|
||||
Length: byte(1),
|
||||
}
|
||||
|
||||
if index.Token.IsNumeric() {
|
||||
offset, err := f.ToNumber(index.Token)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
memory.Offset = int8(offset)
|
||||
|
||||
} else if index.Token.Kind == token.Identifier {
|
||||
indexName := index.Token.Text(f.File.Bytes)
|
||||
indexVariable := f.VariableByName(indexName)
|
||||
|
||||
if indexVariable == nil {
|
||||
return nil, errors.New(&errors.UnknownIdentifier{Name: indexName}, f.File, index.Token.Position)
|
||||
}
|
||||
|
||||
if !types.Is(indexVariable.Type, types.Int) {
|
||||
return nil, errors.New(&errors.TypeMismatch{Encountered: indexVariable.Type.Name(), Expected: types.Int.Name()}, f.File, index.Token.Position)
|
||||
}
|
||||
|
||||
memory.OffsetRegister = indexVariable.Register
|
||||
} else {
|
||||
typ, err := f.ExpressionToRegister(index, register)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !types.Is(typ, types.Int) {
|
||||
return nil, errors.New(&errors.TypeMismatch{Encountered: typ.Name(), Expected: types.Int.Name()}, f.File, index.Token.Position)
|
||||
}
|
||||
|
||||
memory.OffsetRegister = register
|
||||
}
|
||||
|
||||
f.MemoryRegister(asm.LOAD, memory, register)
|
||||
return types.Int, nil
|
||||
}
|
27
src/core/CallToRegister.go
Normal file
27
src/core/CallToRegister.go
Normal file
@ -0,0 +1,27 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"git.akyoto.dev/cli/q/src/asm"
|
||||
"git.akyoto.dev/cli/q/src/cpu"
|
||||
"git.akyoto.dev/cli/q/src/expression"
|
||||
"git.akyoto.dev/cli/q/src/types"
|
||||
)
|
||||
|
||||
// CallToRegister moves the result of a function call into the given register.
|
||||
func (f *Function) CallToRegister(node *expression.Expression, register cpu.Register) (types.Type, error) {
|
||||
types, err := f.CompileCall(node)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if register != f.CPU.Output[0] {
|
||||
f.RegisterRegister(asm.MOVE, register, f.CPU.Output[0])
|
||||
}
|
||||
|
||||
if len(types) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return types[0], err
|
||||
}
|
@ -5,7 +5,6 @@ import (
|
||||
|
||||
"git.akyoto.dev/cli/q/src/asm"
|
||||
"git.akyoto.dev/cli/q/src/ast"
|
||||
"git.akyoto.dev/cli/q/src/cpu"
|
||||
"git.akyoto.dev/cli/q/src/errors"
|
||||
"git.akyoto.dev/cli/q/src/types"
|
||||
)
|
||||
@ -27,7 +26,7 @@ func (f *Function) CompileAssignArray(node *ast.Assign) error {
|
||||
memory := asm.Memory{
|
||||
Base: variable.Register,
|
||||
Offset: 0,
|
||||
OffsetRegister: cpu.Register(math.MaxUint8),
|
||||
OffsetRegister: math.MaxUint8,
|
||||
Length: byte(1),
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,7 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/asm"
|
||||
"git.akyoto.dev/cli/q/src/ast"
|
||||
"git.akyoto.dev/cli/q/src/cpu"
|
||||
"git.akyoto.dev/cli/q/src/errors"
|
||||
"git.akyoto.dev/cli/q/src/expression"
|
||||
@ -24,96 +20,13 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
|
||||
return f.TokenToRegister(node.Token, register)
|
||||
}
|
||||
|
||||
if ast.IsFunctionCall(node) {
|
||||
types, err := f.CompileCall(node)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if register != f.CPU.Output[0] {
|
||||
f.RegisterRegister(asm.MOVE, register, f.CPU.Output[0])
|
||||
}
|
||||
|
||||
if len(types) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return types[0], err
|
||||
}
|
||||
|
||||
if node.Token.Kind == token.Array {
|
||||
name := node.Children[0].Token.Text(f.File.Bytes)
|
||||
array := f.VariableByName(name)
|
||||
|
||||
if array == nil {
|
||||
return nil, errors.New(&errors.UnknownIdentifier{Name: name}, f.File, node.Children[0].Token.Position)
|
||||
}
|
||||
|
||||
index := node.Children[1]
|
||||
|
||||
memory := asm.Memory{
|
||||
Base: array.Register,
|
||||
Offset: 0,
|
||||
OffsetRegister: cpu.Register(math.MaxUint8),
|
||||
Length: byte(1),
|
||||
}
|
||||
|
||||
if index.Token.IsNumeric() {
|
||||
offset, err := f.ToNumber(index.Token)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
memory.Offset = int8(offset)
|
||||
} else {
|
||||
typ, err := f.ExpressionToRegister(index, register)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !types.Is(typ, types.Int) {
|
||||
return nil, errors.New(&errors.TypeMismatch{Encountered: typ.Name(), Expected: types.Int.Name()}, f.File, index.Token.Position)
|
||||
}
|
||||
|
||||
memory.OffsetRegister = register
|
||||
}
|
||||
|
||||
f.MemoryRegister(asm.LOAD, memory, register)
|
||||
return types.Int, nil
|
||||
}
|
||||
|
||||
if node.Token.Kind == token.Period {
|
||||
left := node.Children[0]
|
||||
leftText := left.Token.Text(f.File.Bytes)
|
||||
right := node.Children[1]
|
||||
rightText := right.Token.Text(f.File.Bytes)
|
||||
variable := f.VariableByName(leftText)
|
||||
|
||||
if variable != nil {
|
||||
field := variable.Type.(*types.Pointer).To.(*types.Struct).FieldByName(rightText)
|
||||
f.MemoryRegister(asm.LOAD, asm.Memory{Base: variable.Register, Offset: int8(field.Offset), Length: byte(field.Type.Size())}, register)
|
||||
return field.Type, nil
|
||||
}
|
||||
|
||||
constant, isConst := f.Constants[f.Package+"."+leftText+"."+rightText]
|
||||
|
||||
if isConst {
|
||||
return f.TokenToRegister(constant.Value, register)
|
||||
}
|
||||
|
||||
uniqueName := fmt.Sprintf("%s.%s", leftText, rightText)
|
||||
function, exists := f.Functions[uniqueName]
|
||||
|
||||
if exists {
|
||||
f.File.Imports[leftText].Used = true
|
||||
f.RegisterLabel(asm.MOVE, register, function.UniqueName)
|
||||
return types.AnyPointer, nil
|
||||
}
|
||||
|
||||
return nil, errors.New(&errors.UnknownIdentifier{Name: leftText}, f.File, left.Token.Position)
|
||||
switch node.Token.Kind {
|
||||
case token.Call:
|
||||
return f.CallToRegister(node, register)
|
||||
case token.Array:
|
||||
return f.ArrayElementToRegister(node, register)
|
||||
case token.Period:
|
||||
return f.PeriodToRegister(node, register)
|
||||
}
|
||||
|
||||
if len(node.Children) == 1 {
|
||||
|
52
src/core/PeriodToRegister.go
Normal file
52
src/core/PeriodToRegister.go
Normal file
@ -0,0 +1,52 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/asm"
|
||||
"git.akyoto.dev/cli/q/src/cpu"
|
||||
"git.akyoto.dev/cli/q/src/errors"
|
||||
"git.akyoto.dev/cli/q/src/expression"
|
||||
"git.akyoto.dev/cli/q/src/types"
|
||||
)
|
||||
|
||||
// PeriodToRegister moves a constant or a function address into the given register.
|
||||
func (f *Function) PeriodToRegister(node *expression.Expression, register cpu.Register) (types.Type, error) {
|
||||
left := node.Children[0]
|
||||
leftText := left.Token.Text(f.File.Bytes)
|
||||
right := node.Children[1]
|
||||
rightText := right.Token.Text(f.File.Bytes)
|
||||
variable := f.VariableByName(leftText)
|
||||
|
||||
if variable != nil {
|
||||
field := variable.Type.(*types.Pointer).To.(*types.Struct).FieldByName(rightText)
|
||||
|
||||
memory := asm.Memory{
|
||||
Base: variable.Register,
|
||||
Offset: int8(field.Offset),
|
||||
OffsetRegister: math.MaxUint8,
|
||||
Length: byte(field.Type.Size()),
|
||||
}
|
||||
|
||||
f.MemoryRegister(asm.LOAD, memory, register)
|
||||
return field.Type, nil
|
||||
}
|
||||
|
||||
constant, isConst := f.Constants[f.Package+"."+leftText+"."+rightText]
|
||||
|
||||
if isConst {
|
||||
return f.TokenToRegister(constant.Value, register)
|
||||
}
|
||||
|
||||
uniqueName := fmt.Sprintf("%s.%s", leftText, rightText)
|
||||
function, exists := f.Functions[uniqueName]
|
||||
|
||||
if exists {
|
||||
f.File.Imports[leftText].Used = true
|
||||
f.RegisterLabel(asm.MOVE, register, function.UniqueName)
|
||||
return types.AnyPointer, nil
|
||||
}
|
||||
|
||||
return nil, errors.New(&errors.UnknownIdentifier{Name: leftText}, f.File, left.Token.Position)
|
||||
}
|
Reference in New Issue
Block a user