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
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 18:14:12 -07:00
|
|
|
func nodeWithClass(n Node, c Class) *Node {
|
|
|
|
|
n.SetClass(c)
|
2017-04-27 15:17:57 -07:00
|
|
|
n.Name = new(Name)
|
2017-04-25 18:14:12 -07:00
|
|
|
return &n
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-15 21:43:53 +02:00
|
|
|
// Test all code paths for cmpstackvarlt.
|
|
|
|
|
func TestCmpstackvar(t *testing.T) {
|
|
|
|
|
testdata := []struct {
|
2017-04-25 18:14:12 -07:00
|
|
|
a, b *Node
|
2015-09-15 21:43:53 +02:00
|
|
|
lt bool
|
|
|
|
|
}{
|
|
|
|
|
{
|
2017-04-25 18:14:12 -07:00
|
|
|
nodeWithClass(Node{}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{}, PFUNC),
|
2015-09-15 21:43:53 +02:00
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-04-25 18:14:12 -07:00
|
|
|
nodeWithClass(Node{}, PFUNC),
|
|
|
|
|
nodeWithClass(Node{}, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-04-25 18:14:12 -07:00
|
|
|
nodeWithClass(Node{Xoffset: 0}, PFUNC),
|
|
|
|
|
nodeWithClass(Node{Xoffset: 10}, PFUNC),
|
2015-09-15 21:43:53 +02:00
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-04-25 18:14:12 -07:00
|
|
|
nodeWithClass(Node{Xoffset: 20}, PFUNC),
|
|
|
|
|
nodeWithClass(Node{Xoffset: 10}, PFUNC),
|
2015-09-15 21:43:53 +02:00
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-04-25 18:14:12 -07:00
|
|
|
nodeWithClass(Node{Xoffset: 10}, PFUNC),
|
|
|
|
|
nodeWithClass(Node{Xoffset: 10}, PFUNC),
|
2015-09-15 21:43:53 +02:00
|
|
|
false,
|
|
|
|
|
},
|
2016-02-20 21:36:12 -08:00
|
|
|
{
|
2017-04-25 18:14:12 -07:00
|
|
|
nodeWithClass(Node{Xoffset: 10}, PPARAM),
|
|
|
|
|
nodeWithClass(Node{Xoffset: 20}, PPARAMOUT),
|
2016-02-20 21:36:12 -08:00
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-04-25 18:14:12 -07:00
|
|
|
nodeWithClass(Node{Xoffset: 10}, PPARAMOUT),
|
|
|
|
|
nodeWithClass(Node{Xoffset: 20}, PPARAM),
|
2016-02-20 21:36:12 -08:00
|
|
|
true,
|
|
|
|
|
},
|
2015-09-15 21:43:53 +02:00
|
|
|
{
|
2017-04-27 15:17:57 -07:00
|
|
|
markUsed(nodeWithClass(Node{}, PAUTO)),
|
2017-04-25 18:14:12 -07:00
|
|
|
nodeWithClass(Node{}, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-04-25 18:14:12 -07:00
|
|
|
nodeWithClass(Node{}, PAUTO),
|
2017-04-27 15:17:57 -07:00
|
|
|
markUsed(nodeWithClass(Node{}, PAUTO)),
|
2015-09-15 21:43:53 +02:00
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-04-25 18:14:12 -07:00
|
|
|
nodeWithClass(Node{Type: typeWithoutPointers()}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Type: typeWithPointers()}, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-04-25 18:14:12 -07:00
|
|
|
nodeWithClass(Node{Type: typeWithPointers()}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Type: typeWithoutPointers()}, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-04-27 15:17:57 -07:00
|
|
|
markNeedZero(nodeWithClass(Node{Type: &types.Type{}}, PAUTO)),
|
2017-04-25 18:14:12 -07:00
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Name: &Name{}}, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-04-25 18:14:12 -07:00
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Name: &Name{}}, PAUTO),
|
2017-04-27 15:17:57 -07:00
|
|
|
markNeedZero(nodeWithClass(Node{Type: &types.Type{}}, PAUTO)),
|
2015-09-15 21:43:53 +02:00
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-04-25 18:14:12 -07:00
|
|
|
nodeWithClass(Node{Type: &types.Type{Width: 1}, Name: &Name{}}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Type: &types.Type{Width: 2}, Name: &Name{}}, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-04-25 18:14:12 -07:00
|
|
|
nodeWithClass(Node{Type: &types.Type{Width: 2}, Name: &Name{}}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Type: &types.Type{Width: 1}, Name: &Name{}}, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-04-27 15:17:57 -07:00
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "abc"}}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "xyz"}}, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-04-27 15:17:57 -07:00
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "abc"}}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "abc"}}, PAUTO),
|
2015-09-15 21:43:53 +02:00
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
2017-04-27 15:17:57 -07:00
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "xyz"}}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Sym: &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) {
|
2015-09-19 23:55:27 +02:00
|
|
|
inp := []*Node{
|
2017-04-27 15:17:57 -07:00
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PFUNC),
|
|
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Xoffset: 0, Type: &types.Type{}, Sym: &types.Sym{}}, PFUNC),
|
|
|
|
|
nodeWithClass(Node{Xoffset: 10, Type: &types.Type{}, Sym: &types.Sym{}}, PFUNC),
|
|
|
|
|
nodeWithClass(Node{Xoffset: 20, Type: &types.Type{}, Sym: &types.Sym{}}, PFUNC),
|
|
|
|
|
markUsed(nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PAUTO)),
|
|
|
|
|
nodeWithClass(Node{Type: typeWithoutPointers(), Sym: &types.Sym{}}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PAUTO),
|
|
|
|
|
markNeedZero(nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PAUTO)),
|
|
|
|
|
nodeWithClass(Node{Type: &types.Type{Width: 1}, Sym: &types.Sym{}}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Type: &types.Type{Width: 2}, Sym: &types.Sym{}}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "abc"}}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "xyz"}}, PAUTO),
|
2015-09-19 23:55:27 +02:00
|
|
|
}
|
|
|
|
|
want := []*Node{
|
2017-04-27 15:17:57 -07:00
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PFUNC),
|
|
|
|
|
nodeWithClass(Node{Xoffset: 0, Type: &types.Type{}, Sym: &types.Sym{}}, PFUNC),
|
|
|
|
|
nodeWithClass(Node{Xoffset: 10, Type: &types.Type{}, Sym: &types.Sym{}}, PFUNC),
|
|
|
|
|
nodeWithClass(Node{Xoffset: 20, Type: &types.Type{}, Sym: &types.Sym{}}, PFUNC),
|
|
|
|
|
markUsed(nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PAUTO)),
|
|
|
|
|
markNeedZero(nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PAUTO)),
|
|
|
|
|
nodeWithClass(Node{Type: &types.Type{Width: 2}, Sym: &types.Sym{}}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Type: &types.Type{Width: 1}, Sym: &types.Sym{}}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{}}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "abc"}}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Type: &types.Type{}, Sym: &types.Sym{Name: "xyz"}}, PAUTO),
|
|
|
|
|
nodeWithClass(Node{Type: typeWithoutPointers(), Sym: &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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|