parameter loading
This commit is contained in:
18
build/pseudo.go
Normal file
18
build/pseudo.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"github.com/mmcloughlin/avo/reg"
|
||||
|
||||
"github.com/mmcloughlin/avo/gotypes"
|
||||
)
|
||||
|
||||
//go:generate avogen -output zmov.go mov
|
||||
|
||||
func (c *Context) Load(src gotypes.Component, dst reg.Register) {
|
||||
b, err := src.Resolve()
|
||||
if err != nil {
|
||||
c.AddError(err)
|
||||
return
|
||||
}
|
||||
c.mov(b.Addr, dst, int(gotypes.Sizes.Sizeof(b.Type)), int(dst.Bytes()), b.Type)
|
||||
}
|
||||
67
build/zmov.go
Normal file
67
build/zmov.go
Normal file
@@ -0,0 +1,67 @@
|
||||
// Code generated by command: avogen -output zmov.go mov. DO NOT EDIT.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"github.com/mmcloughlin/avo/operand"
|
||||
"go/types"
|
||||
)
|
||||
|
||||
func (c *Context) mov(a, b operand.Op, an, bn int, t *types.Basic) {
|
||||
switch {
|
||||
case an == 1 && bn == 1 && (t.Info()&types.IsInteger) != 0:
|
||||
c.MOVB(a, b)
|
||||
case an == 1 && bn == 4 && (t.Info()&types.IsInteger) != 0 && (t.Info()&types.IsUnsigned) == 0:
|
||||
c.MOVBLSX(a, b)
|
||||
case an == 1 && bn == 4 && (t.Info()&types.IsInteger) != 0 && (t.Info()&types.IsUnsigned) != 0:
|
||||
c.MOVBLZX(a, b)
|
||||
case an == 1 && bn == 8 && (t.Info()&types.IsInteger) != 0 && (t.Info()&types.IsUnsigned) == 0:
|
||||
c.MOVBQSX(a, b)
|
||||
case an == 1 && bn == 8 && (t.Info()&types.IsInteger) != 0 && (t.Info()&types.IsUnsigned) != 0:
|
||||
c.MOVBQZX(a, b)
|
||||
case an == 1 && bn == 2 && (t.Info()&types.IsInteger) != 0 && (t.Info()&types.IsUnsigned) == 0:
|
||||
c.MOVBWSX(a, b)
|
||||
case an == 1 && bn == 2 && (t.Info()&types.IsInteger) != 0 && (t.Info()&types.IsUnsigned) != 0:
|
||||
c.MOVBWZX(a, b)
|
||||
case an == 4 && bn == 4 && (t.Info()&types.IsInteger) != 0:
|
||||
c.MOVL(a, b)
|
||||
case an == 4 && bn == 8 && (t.Info()&types.IsInteger) != 0 && (t.Info()&types.IsUnsigned) == 0:
|
||||
c.MOVLQSX(a, b)
|
||||
case an == 4 && bn == 8 && (t.Info()&types.IsInteger) != 0 && (t.Info()&types.IsUnsigned) != 0:
|
||||
c.MOVLQZX(a, b)
|
||||
case an == 16 && bn == 16 && (t.Info()&types.IsInteger) != 0:
|
||||
c.MOVOU(a, b)
|
||||
case an == 16 && bn == 8 && (t.Info()&types.IsInteger) != 0:
|
||||
c.MOVQ(a, b)
|
||||
case an == 8 && bn == 16 && (t.Info()&types.IsInteger) != 0:
|
||||
c.MOVQ(a, b)
|
||||
case an == 16 && bn == 16 && (t.Info()&types.IsInteger) != 0:
|
||||
c.MOVQ(a, b)
|
||||
case an == 8 && bn == 8 && (t.Info()&types.IsInteger) != 0:
|
||||
c.MOVQ(a, b)
|
||||
case an == 16 && bn == 8 && (t.Info()&types.IsFloat) != 0:
|
||||
c.MOVSD(a, b)
|
||||
case an == 16 && bn == 16 && (t.Info()&types.IsFloat) != 0:
|
||||
c.MOVSD(a, b)
|
||||
case an == 8 && bn == 16 && (t.Info()&types.IsFloat) != 0:
|
||||
c.MOVSD(a, b)
|
||||
case an == 16 && bn == 16 && (t.Info()&types.IsFloat) != 0:
|
||||
c.MOVSS(a, b)
|
||||
case an == 4 && bn == 16 && (t.Info()&types.IsFloat) != 0:
|
||||
c.MOVSS(a, b)
|
||||
case an == 16 && bn == 4 && (t.Info()&types.IsFloat) != 0:
|
||||
c.MOVSS(a, b)
|
||||
case an == 2 && bn == 2 && (t.Info()&types.IsInteger) != 0:
|
||||
c.MOVW(a, b)
|
||||
case an == 2 && bn == 4 && (t.Info()&types.IsInteger) != 0 && (t.Info()&types.IsUnsigned) == 0:
|
||||
c.MOVWLSX(a, b)
|
||||
case an == 2 && bn == 4 && (t.Info()&types.IsInteger) != 0 && (t.Info()&types.IsUnsigned) != 0:
|
||||
c.MOVWLZX(a, b)
|
||||
case an == 2 && bn == 8 && (t.Info()&types.IsInteger) != 0 && (t.Info()&types.IsUnsigned) == 0:
|
||||
c.MOVWQSX(a, b)
|
||||
case an == 2 && bn == 8 && (t.Info()&types.IsInteger) != 0 && (t.Info()&types.IsUnsigned) != 0:
|
||||
c.MOVWQZX(a, b)
|
||||
default:
|
||||
c.AddErrorMessage("could not deduce mov instruction")
|
||||
}
|
||||
}
|
||||
@@ -35,28 +35,30 @@ func (c componenterr) Index(int) Component { return c }
|
||||
|
||||
type component struct {
|
||||
name string
|
||||
offset int
|
||||
typ types.Type
|
||||
offset int
|
||||
err error
|
||||
}
|
||||
|
||||
func NewComponentFromVar(v *types.Var) Component {
|
||||
func NewComponent(name string, t types.Type, offset int) Component {
|
||||
return &component{
|
||||
name: v.Name(),
|
||||
name: name,
|
||||
typ: t,
|
||||
offset: offset,
|
||||
}
|
||||
}
|
||||
|
||||
func NewComponentFromVar(v *types.Var, offset int) Component {
|
||||
return NewComponent(v.Name(), v.Type(), offset)
|
||||
}
|
||||
|
||||
func (c *component) Resolve() (*Basic, error) {
|
||||
b, ok := c.typ.(*types.Basic)
|
||||
if !ok {
|
||||
return nil, errors.New("not basic type")
|
||||
}
|
||||
if b.Kind() == types.String {
|
||||
return nil, errors.New("string types are handled specially")
|
||||
if !isprimitive(c.typ) {
|
||||
return nil, errors.New("component is not primitive")
|
||||
}
|
||||
return &Basic{
|
||||
Addr: operand.NewParamAddr(c.name, c.offset),
|
||||
Type: b,
|
||||
Type: c.typ.(*types.Basic),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -134,51 +136,7 @@ func (c *component) sub(suffix string, offset int, t types.Type) *component {
|
||||
}
|
||||
|
||||
// TODO(mbm): gotypes.Component handling for structs
|
||||
// TODO(mbm): gotypes.Component handling for complex
|
||||
|
||||
type Signature struct {
|
||||
sig *types.Signature
|
||||
}
|
||||
|
||||
func newSignature(sig *types.Signature) *Signature {
|
||||
return &Signature{
|
||||
sig: sig,
|
||||
}
|
||||
}
|
||||
|
||||
func ParseSignature(expr string) (*Signature, error) {
|
||||
tv, err := types.Eval(token.NewFileSet(), nil, token.NoPos, expr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if tv.Value != nil {
|
||||
return nil, errors.New("signature expression should have nil value")
|
||||
}
|
||||
s, ok := tv.Type.(*types.Signature)
|
||||
if !ok {
|
||||
return nil, errors.New("provided type is not a function signature")
|
||||
}
|
||||
return newSignature(s), nil
|
||||
}
|
||||
|
||||
func (s *Signature) Parameter(name string) Component {
|
||||
p := s.param(name)
|
||||
if p == nil {
|
||||
return componenterr("unknown parameter")
|
||||
}
|
||||
return NewComponentFromVar(p)
|
||||
}
|
||||
|
||||
func (s *Signature) param(name string) *types.Var {
|
||||
t := s.sig.Params()
|
||||
for i := 0; i < t.Len(); i++ {
|
||||
p := t.At(i)
|
||||
if p.Name() == name {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// TODO(mbm): gotypes.Component handling for complex64/128
|
||||
|
||||
func isslice(t types.Type) bool {
|
||||
_, ok := t.(*types.Slice)
|
||||
@@ -189,3 +147,9 @@ func isstring(t types.Type) bool {
|
||||
b, ok := t.(*types.Basic)
|
||||
return ok && b.Kind() == types.String
|
||||
}
|
||||
|
||||
// isprimitive returns true if the type cannot be broken into components.
|
||||
func isprimitive(t types.Type) bool {
|
||||
b, ok := t.(*types.Basic)
|
||||
return ok && (b.Info()&(types.IsString|types.IsComplex)) == 0
|
||||
}
|
||||
101
gotypes/signature.go
Normal file
101
gotypes/signature.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package gotypes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"go/token"
|
||||
"go/types"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Signature struct {
|
||||
sig *types.Signature
|
||||
params *Tuple
|
||||
results *Tuple
|
||||
}
|
||||
|
||||
func NewSignature(sig *types.Signature) *Signature {
|
||||
s := &Signature{
|
||||
sig: sig,
|
||||
}
|
||||
s.init()
|
||||
return s
|
||||
}
|
||||
|
||||
func ParseSignature(expr string) (*Signature, error) {
|
||||
tv, err := types.Eval(token.NewFileSet(), nil, token.NoPos, expr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if tv.Value != nil {
|
||||
return nil, errors.New("signature expression should have nil value")
|
||||
}
|
||||
s, ok := tv.Type.(*types.Signature)
|
||||
if !ok {
|
||||
return nil, errors.New("provided type is not a function signature")
|
||||
}
|
||||
return NewSignature(s), nil
|
||||
}
|
||||
|
||||
func (s *Signature) Params() *Tuple { return s.params }
|
||||
|
||||
func (s *Signature) Results() *Tuple { return s.results }
|
||||
|
||||
func (s *Signature) Bytes() int { return s.Params().Bytes() + s.Results().Bytes() }
|
||||
|
||||
func (s *Signature) init() {
|
||||
p := s.sig.Params()
|
||||
r := s.sig.Results()
|
||||
|
||||
// Compute offsets of parameters and return values.
|
||||
vs := tuplevars(p)
|
||||
vs = append(vs, tuplevars(r)...)
|
||||
vs = append(vs, types.NewParam(token.NoPos, nil, "sentinel", types.Typ[types.Uint64]))
|
||||
offsets := Sizes.Offsetsof(vs)
|
||||
|
||||
// Build components for them.
|
||||
s.params = newTuple(p, offsets, "arg")
|
||||
s.results = newTuple(r, offsets[p.Len():], "ret")
|
||||
}
|
||||
|
||||
type Tuple struct {
|
||||
components []Component
|
||||
byname map[string]Component
|
||||
size int
|
||||
}
|
||||
|
||||
func newTuple(t *types.Tuple, offsets []int64, defaultprefix string) *Tuple {
|
||||
tuple := &Tuple{
|
||||
byname: map[string]Component{},
|
||||
size: int(offsets[t.Len()]),
|
||||
}
|
||||
for i := 0; i < t.Len(); i++ {
|
||||
v := t.At(i)
|
||||
name := v.Name()
|
||||
if name == "" {
|
||||
name = defaultprefix
|
||||
if i > 0 {
|
||||
name += strconv.Itoa(i)
|
||||
}
|
||||
}
|
||||
c := NewComponent(name, v.Type(), int(offsets[i]))
|
||||
tuple.components = append(tuple.components, c)
|
||||
if v.Name() != "" {
|
||||
tuple.byname[v.Name()] = c
|
||||
}
|
||||
}
|
||||
return tuple
|
||||
}
|
||||
|
||||
func (t *Tuple) Lookup(name string) Component { return t.byname[name] }
|
||||
|
||||
func (t *Tuple) At(i int) Component { return t.components[i] }
|
||||
|
||||
func (t *Tuple) Bytes() int { return t.size }
|
||||
|
||||
func tuplevars(t *types.Tuple) []*types.Var {
|
||||
vs := make([]*types.Var, t.Len())
|
||||
for i := 0; i < t.Len(); i++ {
|
||||
vs[i] = t.At(i)
|
||||
}
|
||||
return vs
|
||||
}
|
||||
@@ -51,11 +51,11 @@ func TestParseSignature(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !TypesTuplesEqual(s.Params(), c.ExpectParams) {
|
||||
t.Errorf("parameter mismatch\ngot %#v\nexpect %#v\n", s.Params(), c.ExpectParams)
|
||||
if !TypesTuplesEqual(s.sig.Params(), c.ExpectParams) {
|
||||
t.Errorf("parameter mismatch\ngot %#v\nexpect %#v\n", s.sig.Params(), c.ExpectParams)
|
||||
}
|
||||
if !TypesTuplesEqual(s.Results(), c.ExpectReturn) {
|
||||
t.Errorf("return value(s) mismatch\ngot %#v\nexpect %#v\n", s.Results(), c.ExpectReturn)
|
||||
if !TypesTuplesEqual(s.sig.Results(), c.ExpectReturn) {
|
||||
t.Errorf("return value(s) mismatch\ngot %#v\nexpect %#v\n", s.sig.Results(), c.ExpectReturn)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ var generators = map[string]gen.Builder{
|
||||
"ctors": gen.NewCtors,
|
||||
"ctorstest": gen.NewCtorsTest,
|
||||
"build": gen.NewBuild,
|
||||
"mov": gen.NewMOV,
|
||||
}
|
||||
|
||||
// Command-line flags.
|
||||
|
||||
@@ -10,5 +10,6 @@ func TestBuilderInterfaces(t *testing.T) {
|
||||
NewCtors,
|
||||
NewCtorsTest,
|
||||
NewBuild,
|
||||
NewMOV,
|
||||
}
|
||||
}
|
||||
|
||||
138
internal/gen/mov.go
Normal file
138
internal/gen/mov.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package gen
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/mmcloughlin/avo/internal/inst"
|
||||
)
|
||||
|
||||
type mov struct {
|
||||
cfg Config
|
||||
generator
|
||||
}
|
||||
|
||||
func NewMOV(cfg Config) Interface {
|
||||
return GoFmt(&mov{cfg: cfg})
|
||||
}
|
||||
|
||||
func (m *mov) Generate(is []inst.Instruction) ([]byte, error) {
|
||||
m.Printf("// %s\n\n", m.cfg.GeneratedWarning())
|
||||
m.Printf("package build\n\n")
|
||||
|
||||
m.Printf("import (\n")
|
||||
m.Printf("\t\"go/types\"\n")
|
||||
m.Printf("\t\"%s/operand\"\n", pkg)
|
||||
m.Printf(")\n\n")
|
||||
|
||||
m.Printf("func (c *Context) mov(a, b operand.Op, an, bn int, t *types.Basic) {\n")
|
||||
m.Printf("switch {\n")
|
||||
for _, i := range is {
|
||||
if ismov(i) {
|
||||
m.instruction(i)
|
||||
}
|
||||
}
|
||||
m.Printf("default:\n")
|
||||
m.Printf("c.AddErrorMessage(\"could not deduce mov instruction\")\n")
|
||||
m.Printf("}\n")
|
||||
m.Printf("}\n")
|
||||
return m.Result()
|
||||
}
|
||||
|
||||
func (m *mov) instruction(i inst.Instruction) {
|
||||
f := flags(i)
|
||||
sizes, err := formsizes(i)
|
||||
if err != nil {
|
||||
m.AddError(err)
|
||||
return
|
||||
}
|
||||
for _, size := range sizes {
|
||||
conds := []string{
|
||||
fmt.Sprintf("an == %d", size.A),
|
||||
fmt.Sprintf("bn == %d", size.B),
|
||||
}
|
||||
for c, on := range f {
|
||||
cmp := map[bool]string{true: "!=", false: "=="}
|
||||
cond := fmt.Sprintf("(t.Info() & %s) %s 0", c, cmp[on])
|
||||
conds = append(conds, cond)
|
||||
}
|
||||
m.Printf("case %s:\n", strings.Join(conds, " && "))
|
||||
m.Printf("c.%s(a, b)\n", i.Opcode)
|
||||
}
|
||||
}
|
||||
|
||||
// ismov decides whether the given instruction is a plain move instruction.
|
||||
func ismov(i inst.Instruction) bool {
|
||||
if i.AliasOf != "" || !strings.HasPrefix(i.Opcode, "MOV") {
|
||||
return false
|
||||
}
|
||||
exclude := []string{"Packed", "Duplicate", "Aligned", "Hint", "Swapping"}
|
||||
for _, substring := range exclude {
|
||||
if strings.Contains(i.Summary, substring) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func flags(i inst.Instruction) map[string]bool {
|
||||
f := map[string]bool{}
|
||||
switch {
|
||||
case strings.Contains(i.Summary, "Floating-Point"):
|
||||
f["types.IsFloat"] = true
|
||||
case strings.Contains(i.Summary, "Zero-Extend"):
|
||||
f["types.IsInteger"] = true
|
||||
f["types.IsUnsigned"] = true
|
||||
case strings.Contains(i.Summary, "Sign-Extension"):
|
||||
f["types.IsInteger"] = true
|
||||
f["types.IsUnsigned"] = false
|
||||
default:
|
||||
f["types.IsInteger"] = true
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
type movsize struct{ A, B int }
|
||||
|
||||
func formsizes(i inst.Instruction) ([]movsize, error) {
|
||||
set := map[movsize]bool{}
|
||||
for _, f := range i.Forms {
|
||||
if f.Arity() != 2 {
|
||||
continue
|
||||
}
|
||||
s := movsize{
|
||||
A: opsize[f.Operands[0].Type],
|
||||
B: opsize[f.Operands[1].Type],
|
||||
}
|
||||
if s.A < 0 || s.B < 0 {
|
||||
continue
|
||||
}
|
||||
if s.A == 0 || s.B == 0 {
|
||||
return nil, errors.New("unknown operand type")
|
||||
}
|
||||
set[s] = true
|
||||
}
|
||||
var ss []movsize
|
||||
for s := range set {
|
||||
ss = append(ss, s)
|
||||
}
|
||||
return ss, nil
|
||||
}
|
||||
|
||||
var opsize = map[string]int{
|
||||
"imm8": -1,
|
||||
"imm16": -1,
|
||||
"imm32": -1,
|
||||
"imm64": -1,
|
||||
"r8": 1,
|
||||
"r16": 2,
|
||||
"r32": 4,
|
||||
"r64": 8,
|
||||
"xmm": 16,
|
||||
"m8": 1,
|
||||
"m16": 2,
|
||||
"m32": 4,
|
||||
"m64": 8,
|
||||
"m128": 16,
|
||||
}
|
||||
@@ -38,7 +38,7 @@ func (i Instruction) IsConditionalBranch() bool {
|
||||
func (i Instruction) Arities() []int {
|
||||
s := map[int]bool{}
|
||||
for _, f := range i.Forms {
|
||||
s[len(f.Operands)] = true
|
||||
s[f.Arity()] = true
|
||||
}
|
||||
a := make([]int, 0, len(s))
|
||||
for n := range s {
|
||||
@@ -71,8 +71,12 @@ type Form struct {
|
||||
ImplicitOperands []ImplicitOperand
|
||||
}
|
||||
|
||||
func (f Form) Arity() int {
|
||||
return len(f.Operands)
|
||||
}
|
||||
|
||||
func (f Form) Signature() []string {
|
||||
s := make([]string, len(f.Operands))
|
||||
s := make([]string, f.Arity())
|
||||
for i, op := range f.Operands {
|
||||
s[i] = op.Type
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user