gotypes,build: pointer dereference (#61)
Provides a method on `gotypes.Component` to allow pointer dereference. This will enable `gotypes` helpers to be used with struct pointer arguments, for example. Updates #53 Fixes #54
This commit is contained in:
committed by
GitHub
parent
eb225e9d2c
commit
9eb409b935
@@ -112,6 +112,9 @@ func Load(src gotypes.Component, dst reg.Register) reg.Register { return ctx.Loa
|
|||||||
// attempt to select the right MOV instruction based on the types involved.
|
// attempt to select the right MOV instruction based on the types involved.
|
||||||
func Store(src reg.Register, dst gotypes.Component) { ctx.Store(src, dst) }
|
func Store(src reg.Register, dst gotypes.Component) { ctx.Store(src, dst) }
|
||||||
|
|
||||||
|
// Dereference loads a pointer and returns its element type.
|
||||||
|
func Dereference(ptr gotypes.Component) gotypes.Component { return ctx.Dereference(ptr) }
|
||||||
|
|
||||||
// Doc sets documentation comment lines for the currently active function.
|
// Doc sets documentation comment lines for the currently active function.
|
||||||
func Doc(lines ...string) { ctx.Doc(lines...) }
|
func Doc(lines ...string) { ctx.Doc(lines...) }
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,13 @@ func (c *Context) Store(src reg.Register, dst gotypes.Component) {
|
|||||||
c.mov(src, b.Addr, int(src.Bytes()), int(gotypes.Sizes.Sizeof(b.Type)), b.Type)
|
c.mov(src, b.Addr, int(src.Bytes()), int(gotypes.Sizes.Sizeof(b.Type)), b.Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dereference loads a pointer and returns its element type.
|
||||||
|
func (c *Context) Dereference(ptr gotypes.Component) gotypes.Component {
|
||||||
|
r := c.GP64()
|
||||||
|
c.Load(ptr, r)
|
||||||
|
return ptr.Dereference(r)
|
||||||
|
}
|
||||||
|
|
||||||
// ConstData builds a static data section containing just the given constant.
|
// ConstData builds a static data section containing just the given constant.
|
||||||
func (c *Context) ConstData(name string, v operand.Constant) operand.Mem {
|
func (c *Context) ConstData(name string, v operand.Constant) operand.Mem {
|
||||||
g := c.StaticGlobal(name)
|
g := c.StaticGlobal(name)
|
||||||
|
|||||||
@@ -109,3 +109,10 @@ TEXT ·FieldComplex128Real(SB), NOSPLIT, $0-184
|
|||||||
MOVSD s_Complex128_real+160(FP), X0
|
MOVSD s_Complex128_real+160(FP), X0
|
||||||
MOVSD X0, ret+176(FP)
|
MOVSD X0, ret+176(FP)
|
||||||
RET
|
RET
|
||||||
|
|
||||||
|
// func DereferenceFloat32(s *Struct) float32
|
||||||
|
TEXT ·DereferenceFloat32(SB), NOSPLIT, $0-12
|
||||||
|
MOVQ s+0(FP), AX
|
||||||
|
MOVSS 16(AX), X0
|
||||||
|
MOVSS X0, ret+8(FP)
|
||||||
|
RET
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package args
|
package args
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
"testing/quick"
|
"testing/quick"
|
||||||
)
|
)
|
||||||
@@ -36,3 +37,12 @@ func TestFunctionsEqual(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDereferenceFloat32(t *testing.T) {
|
||||||
|
expect := float32(math.Pi)
|
||||||
|
s := Struct{Float32: expect}
|
||||||
|
got := DereferenceFloat32(&s)
|
||||||
|
if got != expect {
|
||||||
|
t.Errorf("DereferenceFloat32(%v) = %v; expect %v", s, got, expect)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -99,5 +99,11 @@ func main() {
|
|||||||
Store(c128r, ReturnIndex(0))
|
Store(c128r, ReturnIndex(0))
|
||||||
RET()
|
RET()
|
||||||
|
|
||||||
|
TEXT("DereferenceFloat32", NOSPLIT, "func(s *Struct) float32")
|
||||||
|
s := Dereference(Param("s"))
|
||||||
|
f := Load(s.Field("Float32"), XMM())
|
||||||
|
Store(f, ReturnIndex(0))
|
||||||
|
|
||||||
|
RET()
|
||||||
Generate()
|
Generate()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,3 +37,5 @@ func FieldArrayOneC(s Struct) uint16
|
|||||||
func FieldComplex64Imag(s Struct) float32
|
func FieldComplex64Imag(s Struct) float32
|
||||||
|
|
||||||
func FieldComplex128Real(s Struct) float64
|
func FieldComplex128Real(s Struct) float64
|
||||||
|
|
||||||
|
func DereferenceFloat32(s *Struct) float32
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import (
|
|||||||
"go/types"
|
"go/types"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/mmcloughlin/avo/reg"
|
||||||
|
|
||||||
"github.com/mmcloughlin/avo/operand"
|
"github.com/mmcloughlin/avo/operand"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -27,6 +29,7 @@ type Component interface {
|
|||||||
// resolution time.
|
// resolution time.
|
||||||
Resolve() (*Basic, error)
|
Resolve() (*Basic, error)
|
||||||
|
|
||||||
|
Dereference(r reg.Register) Component // dereference a pointer
|
||||||
Base() Component // base pointer of a string or slice
|
Base() Component // base pointer of a string or slice
|
||||||
Len() Component // length of a string or slice
|
Len() Component // length of a string or slice
|
||||||
Cap() Component // capacity of a slice
|
Cap() Component // capacity of a slice
|
||||||
@@ -47,6 +50,7 @@ func errorf(format string, args ...interface{}) Component {
|
|||||||
|
|
||||||
func (c componenterr) Error() string { return string(c) }
|
func (c componenterr) Error() string { return string(c) }
|
||||||
func (c componenterr) Resolve() (*Basic, error) { return nil, c }
|
func (c componenterr) Resolve() (*Basic, error) { return nil, c }
|
||||||
|
func (c componenterr) Dereference(r reg.Register) Component { return c }
|
||||||
func (c componenterr) Base() Component { return c }
|
func (c componenterr) Base() Component { return c }
|
||||||
func (c componenterr) Len() Component { return c }
|
func (c componenterr) Len() Component { return c }
|
||||||
func (c componenterr) Cap() Component { return c }
|
func (c componenterr) Cap() Component { return c }
|
||||||
@@ -56,17 +60,15 @@ func (c componenterr) Index(int) Component { return c }
|
|||||||
func (c componenterr) Field(string) Component { return c }
|
func (c componenterr) Field(string) Component { return c }
|
||||||
|
|
||||||
type component struct {
|
type component struct {
|
||||||
name string
|
|
||||||
typ types.Type
|
typ types.Type
|
||||||
offset int
|
addr operand.Mem
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewComponent builds a component for the named type at the given memory offset.
|
// NewComponent builds a component for the named type at the given address.
|
||||||
func NewComponent(name string, t types.Type, offset int) Component {
|
func NewComponent(t types.Type, addr operand.Mem) Component {
|
||||||
return &component{
|
return &component{
|
||||||
name: name,
|
|
||||||
typ: t,
|
typ: t,
|
||||||
offset: offset,
|
addr: addr,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,11 +78,19 @@ func (c *component) Resolve() (*Basic, error) {
|
|||||||
return nil, errors.New("component is not primitive")
|
return nil, errors.New("component is not primitive")
|
||||||
}
|
}
|
||||||
return &Basic{
|
return &Basic{
|
||||||
Addr: operand.NewParamAddr(c.name, c.offset),
|
Addr: c.addr,
|
||||||
Type: b,
|
Type: b,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *component) Dereference(r reg.Register) Component {
|
||||||
|
p, ok := c.typ.Underlying().(*types.Pointer)
|
||||||
|
if !ok {
|
||||||
|
return errorf("not pointer type")
|
||||||
|
}
|
||||||
|
return NewComponent(p.Elem(), operand.Mem{Base: r})
|
||||||
|
}
|
||||||
|
|
||||||
// Reference: https://github.com/golang/go/blob/50bd1c4d4eb4fac8ddeb5f063c099daccfb71b26/src/reflect/value.go#L1800-L1804
|
// Reference: https://github.com/golang/go/blob/50bd1c4d4eb4fac8ddeb5f063c099daccfb71b26/src/reflect/value.go#L1800-L1804
|
||||||
//
|
//
|
||||||
// type SliceHeader struct {
|
// type SliceHeader struct {
|
||||||
@@ -194,8 +204,10 @@ func (c *component) Field(n string) Component {
|
|||||||
|
|
||||||
func (c *component) sub(suffix string, offset int, t types.Type) *component {
|
func (c *component) sub(suffix string, offset int, t types.Type) *component {
|
||||||
s := *c
|
s := *c
|
||||||
s.name += suffix
|
if s.addr.Symbol.Name != "" {
|
||||||
s.offset += offset
|
s.addr.Symbol.Name += suffix
|
||||||
|
}
|
||||||
|
s.addr = s.addr.Offset(offset)
|
||||||
s.typ = t
|
s.typ = t
|
||||||
return &s
|
return &s
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,12 @@ package gotypes
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"go/types"
|
"go/types"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mmcloughlin/avo/reg"
|
||||||
|
|
||||||
|
"github.com/mmcloughlin/avo/operand"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestBasicKindsArePrimitive(t *testing.T) {
|
func TestBasicKindsArePrimitive(t *testing.T) {
|
||||||
@@ -33,8 +38,61 @@ func TestPointersArePrimitive(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func AssertPrimitive(t *testing.T, typ types.Type) {
|
func AssertPrimitive(t *testing.T, typ types.Type) {
|
||||||
c := NewComponent("primitive", typ, 0)
|
c := NewComponent(typ, operand.NewParamAddr("primitive", 0))
|
||||||
if _, err := c.Resolve(); err != nil {
|
if _, err := c.Resolve(); err != nil {
|
||||||
t.Errorf("expected type %s to be primitive: got error '%s'", typ, err)
|
t.Errorf("expected type %s to be primitive: got error '%s'", typ, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestComponentErrors(t *testing.T) {
|
||||||
|
comp := NewComponent(types.Typ[types.Uint32], operand.Mem{})
|
||||||
|
cases := []struct {
|
||||||
|
Component Component
|
||||||
|
ErrorSubstring string
|
||||||
|
}{
|
||||||
|
{comp.Base(), "only slices and strings"},
|
||||||
|
{comp.Len(), "only slices and strings"},
|
||||||
|
{comp.Cap(), "only slices have"},
|
||||||
|
{comp.Real(), "only complex"},
|
||||||
|
{comp.Imag(), "only complex"},
|
||||||
|
{comp.Index(42), "not array type"},
|
||||||
|
{comp.Field("a"), "not struct type"},
|
||||||
|
{comp.Dereference(reg.R12), "not pointer type"},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
_, err := c.Component.Resolve()
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), c.ErrorSubstring) {
|
||||||
|
t.Fatalf("error message %q; expected substring %q", err.Error(), c.ErrorSubstring)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestComponentErrorChaining(t *testing.T) {
|
||||||
|
// Build a component with an error.
|
||||||
|
comp := NewComponent(types.Typ[types.Uint32], operand.Mem{}).Index(3)
|
||||||
|
_, expect := comp.Resolve()
|
||||||
|
if expect == nil {
|
||||||
|
t.Fatal("expected error")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Confirm that the error is preserved through chaining operations.
|
||||||
|
cases := []Component{
|
||||||
|
comp.Dereference(reg.R13),
|
||||||
|
comp.Base(),
|
||||||
|
comp.Len(),
|
||||||
|
comp.Cap(),
|
||||||
|
comp.Real(),
|
||||||
|
comp.Imag(),
|
||||||
|
comp.Index(42),
|
||||||
|
comp.Field("field"),
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
_, err := c.Resolve()
|
||||||
|
if err != expect {
|
||||||
|
t.Fatal("chaining should preserve error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import (
|
|||||||
"go/token"
|
"go/token"
|
||||||
"go/types"
|
"go/types"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/mmcloughlin/avo/operand"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Signature represents a Go function signature.
|
// Signature represents a Go function signature.
|
||||||
@@ -136,7 +138,8 @@ func newTuple(t *types.Tuple, offsets []int64, size int64, defaultprefix string)
|
|||||||
name += strconv.Itoa(i)
|
name += strconv.Itoa(i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c := NewComponent(name, v.Type(), int(offsets[i]))
|
addr := operand.NewParamAddr(name, int(offsets[i]))
|
||||||
|
c := NewComponent(v.Type(), addr)
|
||||||
tuple.components = append(tuple.components, c)
|
tuple.components = append(tuple.components, c)
|
||||||
if v.Name() != "" {
|
if v.Name() != "" {
|
||||||
tuple.byname[v.Name()] = c
|
tuple.byname[v.Name()] = c
|
||||||
|
|||||||
Reference in New Issue
Block a user