60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
package core
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
|
|
"git.urbach.dev/cli/q/src/asm"
|
|
"git.urbach.dev/cli/q/src/cpu"
|
|
"git.urbach.dev/cli/q/src/errors"
|
|
"git.urbach.dev/cli/q/src/expression"
|
|
"git.urbach.dev/cli/q/src/types"
|
|
)
|
|
|
|
// DotToRegister moves a constant or a function address into the given register.
|
|
func (f *Function) DotToRegister(node *expression.Expression, register cpu.Register) (types.Type, error) {
|
|
left := node.Children[0]
|
|
right := node.Children[1]
|
|
leftText := left.Token.Text(f.File.Bytes)
|
|
rightText := right.Token.Text(f.File.Bytes)
|
|
variable := f.VariableByName(leftText)
|
|
|
|
if variable != nil {
|
|
field := variable.Value.Typ.(*types.Pointer).To.(*types.Struct).FieldByName(rightText)
|
|
|
|
memory := asm.Memory{
|
|
Base: variable.Value.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.All.Constants[f.Package+"."+leftText+"."+rightText]
|
|
|
|
if isConst {
|
|
number, err := ToNumber(constant.Token, constant.File)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
f.SaveRegister(register)
|
|
f.RegisterNumber(asm.MOVE, register, number)
|
|
return types.AnyInt, nil
|
|
}
|
|
|
|
uniqueName := fmt.Sprintf("%s.%s", leftText, rightText)
|
|
function, exists := f.All.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: uniqueName}, f.File, left.Token.Position)
|
|
}
|