2015-09-15 21:43:53 +02:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package gc
|
|
|
|
|
|
2015-09-19 23:55:27 +02:00
|
|
|
import (
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
"cmd/compile/internal/types"
|
2015-09-19 23:55:27 +02:00
|
|
|
"reflect"
|
2016-02-26 13:03:11 -08:00
|
|
|
"sort"
|
2015-09-19 23:55:27 +02:00
|
|
|
"testing"
|
|
|
|
|
)
|
2015-09-15 21:43:53 +02:00
|
|
|
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
func typeWithoutPointers() *types.Type {
|
|
|
|
|
t := types.New(TSTRUCT)
|
|
|
|
|
f := &types.Field{Type: types.New(TINT)}
|
|
|
|
|
t.SetFields([]*types.Field{f})
|
2017-03-29 21:04:00 -07:00
|
|
|
return t
|
2016-04-01 20:11:30 -07:00
|
|
|
}
|
|
|
|
|
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
func typeWithPointers() *types.Type {
|
|
|
|
|
t := types.New(TSTRUCT)
|
2020-08-21 20:20:12 -07:00
|
|
|
f := &types.Field{Type: types.NewPtr(types.New(TINT))}
|
cmd/compile: factor out Pkg, Sym, and Type into package types
- created new package cmd/compile/internal/types
- moved Pkg, Sym, Type to new package
- to break cycles, for now we need the (ugly) types/utils.go
file which contains a handful of functions that must be installed
early by the gc frontend
- to break cycles, for now we need two functions to convert between
*gc.Node and *types.Node (the latter is a dummy type)
- adjusted the gc's code to use the new package and the conversion
functions as needed
- made several Pkg, Sym, and Type methods functions as needed
- renamed constructors typ, typPtr, typArray, etc. to types.New,
types.NewPtr, types.NewArray, etc.
Passes toolstash-check -all.
Change-Id: I8adfa5e85c731645d0a7fd2030375ed6ebf54b72
Reviewed-on: https://go-review.googlesource.com/39855
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-04 17:54:02 -07:00
|
|
|
t.SetFields([]*types.Field{f})
|
2017-03-29 21:04:00 -07:00
|
|
|
return t
|
2016-04-01 20:11:30 -07:00
|
|
|
}
|
|
|
|
|
|
2017-04-27 15:17:57 -07:00
|
|
|
func markUsed(n *Node) *Node {
|
|
|
|
|
n.Name.SetUsed(true)
|
|
|
|
|
return n
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func markNeedZero(n *Node) *Node {
|
|
|
|
|
n.Name.SetNeedzero(true)
|
|
|
|
|
return n
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-15 21:43:53 +02:00
|
|
|
// Test all code paths for cmpstackvarlt.
|
|
|
|
|
func TestCmpstackvar(t *testing.T) {
|
2020-11-18 15:14:24 -05:00
|
|
|
nod := func(xoffset int64, t *types.Type, s *types.Sym, cl Class) *Node {
|
|
|
|
|
if s == nil {
|
|
|
|
|
s = &types.Sym{Name: "."}
|
|
|
|
|
}
|
|
|
|
|
n := newname(s)
|
|
|
|
|
n.Type = t
|
|
|
|
|
n.Xoffset = xoffset
|
|
|
|
|
n.SetClass(cl)
|
|
|
|
|
return n
|
|
|
|
|
}
|
2015-09-15 21:43:53 +02:00
|
|
|
testdata := []struct {
|
2017-04-25 18:14:12 -07:00
|
|
|
a, b *Node
|
2015-09-15 21:43:53 +02:00
|
|
|
lt bool
|
|
|
|
|
}{
|
|
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(0, nil, nil, PAUTO),
|
|
|
|
|
nod(0, nil, nil, PFUNC),
|
2015-09-15 21:43:53 +02:00
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(0, nil, nil, PFUNC),
|
|
|
|
|
nod(0, nil, nil, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(0, nil, nil, PFUNC),
|
|
|
|
|
nod(10, nil, nil, PFUNC),
|
2015-09-15 21:43:53 +02:00
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(20, nil, nil, PFUNC),
|
|
|
|
|
nod(10, nil, nil, PFUNC),
|
2015-09-15 21:43:53 +02:00
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(10, nil, nil, PFUNC),
|
|
|
|
|
nod(10, nil, nil, PFUNC),
|
2015-09-15 21:43:53 +02:00
|
|
|
false,
|
|
|
|
|
},
|
2016-02-20 21:36:12 -08:00
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(10, nil, nil, PPARAM),
|
|
|
|
|
nod(20, nil, nil, PPARAMOUT),
|
2016-02-20 21:36:12 -08:00
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(10, nil, nil, PPARAMOUT),
|
|
|
|
|
nod(20, nil, nil, PPARAM),
|
2016-02-20 21:36:12 -08:00
|
|
|
true,
|
|
|
|
|
},
|
2015-09-15 21:43:53 +02:00
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
markUsed(nod(0, nil, nil, PAUTO)),
|
|
|
|
|
nod(0, nil, nil, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(0, nil, nil, PAUTO),
|
|
|
|
|
markUsed(nod(0, nil, nil, PAUTO)),
|
2015-09-15 21:43:53 +02:00
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(0, typeWithoutPointers(), nil, PAUTO),
|
|
|
|
|
nod(0, typeWithPointers(), nil, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(0, typeWithPointers(), nil, PAUTO),
|
|
|
|
|
nod(0, typeWithoutPointers(), nil, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
markNeedZero(nod(0, &types.Type{}, nil, PAUTO)),
|
|
|
|
|
nod(0, &types.Type{}, nil, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(0, &types.Type{}, nil, PAUTO),
|
|
|
|
|
markNeedZero(nod(0, &types.Type{}, nil, PAUTO)),
|
2015-09-15 21:43:53 +02:00
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(0, &types.Type{Width: 1}, nil, PAUTO),
|
|
|
|
|
nod(0, &types.Type{Width: 2}, nil, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(0, &types.Type{Width: 2}, nil, PAUTO),
|
|
|
|
|
nod(0, &types.Type{Width: 1}, nil, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(0, &types.Type{}, &types.Sym{Name: "abc"}, PAUTO),
|
|
|
|
|
nod(0, &types.Type{}, &types.Sym{Name: "xyz"}, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(0, &types.Type{}, &types.Sym{Name: "abc"}, PAUTO),
|
|
|
|
|
nod(0, &types.Type{}, &types.Sym{Name: "abc"}, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(0, &types.Type{}, &types.Sym{Name: "xyz"}, PAUTO),
|
|
|
|
|
nod(0, &types.Type{}, &types.Sym{Name: "abc"}, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
for _, d := range testdata {
|
2017-04-25 18:14:12 -07:00
|
|
|
got := cmpstackvarlt(d.a, d.b)
|
2015-09-15 21:43:53 +02:00
|
|
|
if got != d.lt {
|
|
|
|
|
t.Errorf("want %#v < %#v", d.a, d.b)
|
|
|
|
|
}
|
2016-02-20 21:36:12 -08:00
|
|
|
// If we expect a < b to be true, check that b < a is false.
|
2017-04-25 18:14:12 -07:00
|
|
|
if d.lt && cmpstackvarlt(d.b, d.a) {
|
2016-02-20 21:36:12 -08:00
|
|
|
t.Errorf("unexpected %#v < %#v", d.b, d.a)
|
|
|
|
|
}
|
2015-09-15 21:43:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
2015-09-19 23:55:27 +02:00
|
|
|
|
2016-02-26 13:03:11 -08:00
|
|
|
func TestStackvarSort(t *testing.T) {
|
2020-11-18 15:14:24 -05:00
|
|
|
nod := func(xoffset int64, t *types.Type, s *types.Sym, cl Class) *Node {
|
|
|
|
|
n := newname(s)
|
|
|
|
|
n.Type = t
|
|
|
|
|
n.Xoffset = xoffset
|
|
|
|
|
n.SetClass(cl)
|
|
|
|
|
return n
|
|
|
|
|
}
|
2015-09-19 23:55:27 +02:00
|
|
|
inp := []*Node{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(0, &types.Type{}, &types.Sym{}, PFUNC),
|
|
|
|
|
nod(0, &types.Type{}, &types.Sym{}, PAUTO),
|
|
|
|
|
nod(0, &types.Type{}, &types.Sym{}, PFUNC),
|
|
|
|
|
nod(10, &types.Type{}, &types.Sym{}, PFUNC),
|
|
|
|
|
nod(20, &types.Type{}, &types.Sym{}, PFUNC),
|
|
|
|
|
markUsed(nod(0, &types.Type{}, &types.Sym{}, PAUTO)),
|
|
|
|
|
nod(0, typeWithoutPointers(), &types.Sym{}, PAUTO),
|
|
|
|
|
nod(0, &types.Type{}, &types.Sym{}, PAUTO),
|
|
|
|
|
markNeedZero(nod(0, &types.Type{}, &types.Sym{}, PAUTO)),
|
|
|
|
|
nod(0, &types.Type{Width: 1}, &types.Sym{}, PAUTO),
|
|
|
|
|
nod(0, &types.Type{Width: 2}, &types.Sym{}, PAUTO),
|
|
|
|
|
nod(0, &types.Type{}, &types.Sym{Name: "abc"}, PAUTO),
|
|
|
|
|
nod(0, &types.Type{}, &types.Sym{Name: "xyz"}, PAUTO),
|
2015-09-19 23:55:27 +02:00
|
|
|
}
|
|
|
|
|
want := []*Node{
|
2020-11-18 15:14:24 -05:00
|
|
|
nod(0, &types.Type{}, &types.Sym{}, PFUNC),
|
|
|
|
|
nod(0, &types.Type{}, &types.Sym{}, PFUNC),
|
|
|
|
|
nod(10, &types.Type{}, &types.Sym{}, PFUNC),
|
|
|
|
|
nod(20, &types.Type{}, &types.Sym{}, PFUNC),
|
|
|
|
|
markUsed(nod(0, &types.Type{}, &types.Sym{}, PAUTO)),
|
|
|
|
|
markNeedZero(nod(0, &types.Type{}, &types.Sym{}, PAUTO)),
|
|
|
|
|
nod(0, &types.Type{Width: 2}, &types.Sym{}, PAUTO),
|
|
|
|
|
nod(0, &types.Type{Width: 1}, &types.Sym{}, PAUTO),
|
|
|
|
|
nod(0, &types.Type{}, &types.Sym{}, PAUTO),
|
|
|
|
|
nod(0, &types.Type{}, &types.Sym{}, PAUTO),
|
|
|
|
|
nod(0, &types.Type{}, &types.Sym{Name: "abc"}, PAUTO),
|
|
|
|
|
nod(0, &types.Type{}, &types.Sym{Name: "xyz"}, PAUTO),
|
|
|
|
|
nod(0, typeWithoutPointers(), &types.Sym{}, PAUTO),
|
2015-09-19 23:55:27 +02:00
|
|
|
}
|
2016-02-26 13:03:11 -08:00
|
|
|
sort.Sort(byStackVar(inp))
|
|
|
|
|
if !reflect.DeepEqual(want, inp) {
|
|
|
|
|
t.Error("sort failed")
|
|
|
|
|
for i := range inp {
|
|
|
|
|
g := inp[i]
|
2015-09-19 23:55:27 +02:00
|
|
|
w := want[i]
|
|
|
|
|
eq := reflect.DeepEqual(w, g)
|
|
|
|
|
if !eq {
|
|
|
|
|
t.Log(i, w, g)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|