Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gotypes: provide access to component address #149

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions gotypes/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ type Component interface {
// resolution time.
Resolve() (*Basic, error)

// Addr returns the memory address of the start of this component. Unlike
// Resolve(), the component need not be pointing at a primitive type for
// this to work. Therefore Resolve() should usually be preferred, but Addr()
// can be essential for certain "unsafe" use cases like moving a 128-bit
// register into a [2]uint64. Returns an error if there was a problem with
// any prior calls to Component methods.
Addr() (operand.Mem, error)
MustAddr() operand.Mem

Dereference(r reg.Register) Component // dereference a pointer
Base() Component // base pointer of a string or slice
Len() Component // length of a string or slice
Expand All @@ -50,6 +59,8 @@ func errorf(format string, args ...interface{}) Component {

func (c componenterr) Error() string { return string(c) }
func (c componenterr) Resolve() (*Basic, error) { return nil, c }
func (c componenterr) Addr() (operand.Mem, error) { return operand.Mem{}, c }
func (c componenterr) MustAddr() operand.Mem { panic(c) }
func (c componenterr) Dereference(r reg.Register) Component { return c }
func (c componenterr) Base() Component { return c }
func (c componenterr) Len() Component { return c }
Expand Down Expand Up @@ -83,6 +94,14 @@ func (c *component) Resolve() (*Basic, error) {
}, nil
}

func (c *component) Addr() (operand.Mem, error) {
return c.addr, nil
}

func (c *component) MustAddr() operand.Mem {
return c.addr
}

func (c *component) Dereference(r reg.Register) Component {
p, ok := c.typ.Underlying().(*types.Pointer)
if !ok {
Expand Down
35 changes: 32 additions & 3 deletions gotypes/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func TestComponentErrors(t *testing.T) {
if !strings.Contains(err.Error(), c.ErrorSubstring) {
t.Fatalf("error message %q; expected substring %q", err.Error(), c.ErrorSubstring)
}
_, erraddr := c.Component.Addr()
if erraddr != err {
t.Fatal("expected same error from Resolve() and Addr()")
}
}
}

Expand All @@ -91,13 +95,25 @@ func TestComponentErrorChaining(t *testing.T) {
comp.Field("field"),
}
for _, c := range cases {
_, err := c.Resolve()
if err != expect {
t.Fatal("chaining should preserve error")
if _, err := c.Resolve(); err != expect {
t.Fatal("Resolve: chaining should preserve error")
}
if _, err := c.Addr(); err != expect {
t.Fatal("Addr: chaining should preserve error")
}
}
}

func TestComponentMustAddrPanic(t *testing.T) {
defer func() {
if recover() == nil {
t.Fatal("expected panic")
}
}()
comp := NewComponent(types.Typ[types.Uint32], operand.Mem{}).Index(3)
comp.MustAddr()
}

func TestComponentDeconstruction(t *testing.T) {
cases := []struct {
Name string
Expand Down Expand Up @@ -223,6 +239,19 @@ func TestComponentDeconstruction(t *testing.T) {
if b.Addr.Disp != c.Offset {
t.Errorf("offset %d; expected %d", b.Addr.Disp, c.Offset)
}

a, err := comp.Addr()
if err != nil {
t.Fatal(err)
}

if a != b.Addr {
t.Errorf("Addr() = %v; expect %v", a, b.Addr)
}

if ma := comp.MustAddr(); ma != a {
t.Errorf("MustAddr() = %v; expect %v, the same as Addr()", ma, a)
}
})
}
}
6 changes: 4 additions & 2 deletions script/lint
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ test -z "$(git status --porcelain)"
# (Provide examples directory explicitly since it is skipped by default.)
golangci-lint run ./... ./examples/...

# Check asm declarations.
asmdecl ./...
# Check asm declarations. Exclude issue #145 regression test due to problems
# with the way asmdecl handles large or composite types. See
# https://github.com/golang/go/issues/39220.
asmdecl $(go list ./... | grep -v issue145)

# Custom linters.
./script/linter/pkgdoc
16 changes: 16 additions & 0 deletions tests/fixedbugs/issue145/asm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// +build ignore

package main

import (
. "github.com/mmcloughlin/avo/build"
)

func main() {
TEXT("Halves", NOSPLIT, "func(x uint64) [2]uint32")
Doc("Halves returns the two 32-bit halves of a 64-bit word.")
x := Load(Param("x"), GP64())
MOVQ(x, ReturnIndex(0).MustAddr())
RET()
Generate()
}
2 changes: 2 additions & 0 deletions tests/fixedbugs/issue145/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package issue145 tests "unsafe" component addressing.
package issue145
9 changes: 9 additions & 0 deletions tests/fixedbugs/issue145/issue145.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Code generated by command: go run asm.go -out issue145.s -stubs stub.go. DO NOT EDIT.

#include "textflag.h"

// func Halves(x uint64) [2]uint32
TEXT ·Halves(SB), NOSPLIT, $0-16
MOVQ x+0(FP), AX
MOVQ AX, ret+8(FP)
RET
16 changes: 16 additions & 0 deletions tests/fixedbugs/issue145/issue145_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package issue145

import (
"testing"
)

//go:generate go run asm.go -out issue145.s -stubs stub.go

func TestShuffle(t *testing.T) {
x := uint64(0xfeeddeadbeefcafe)
got := Halves(x)
expect := [2]uint32{0xbeefcafe, 0xfeeddead}
if got != expect {
t.Errorf("Halves(%x) = %x; expect %x", x, got, expect)
}
}
6 changes: 6 additions & 0 deletions tests/fixedbugs/issue145/stub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.