2016-03-06 13:04:52 +11:00
|
|
|
// Copyright 2016 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
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"reflect"
|
|
|
|
|
"testing"
|
|
|
|
|
"unsafe"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Assert that the size of important structures do not change unexpectedly.
|
|
|
|
|
|
|
|
|
|
func TestSizeof(t *testing.T) {
|
|
|
|
|
const _64bit = unsafe.Sizeof(uintptr(0)) == 8
|
|
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
|
val interface{} // type as a value
|
|
|
|
|
_32bit uintptr // size on 32bit platforms
|
|
|
|
|
_64bit uintptr // size on 64bit platforms
|
|
|
|
|
}{
|
[dev.regabi] cmd/compile: clean up Node.Func
The original meaning of type Func was "extra fields factored out
of a few cases of type Node having to do with functions",
but those specific cases didn't necessarily have any relation.
A typical declared function is represented by an ODCLFUNC Node
at its declaration and an ONAME node at its uses, and both those
have a .Func field, but they are *different* Funcs.
Similarly, a closure is represented both by an OCLOSURE Node for
the value itself and an ODCLFUNC Node for the underlying function
implementing the closure. Those too have *different* Funcs,
and the Func.Closure field in one points to the other and vice versa.
This has led to no end of confusion over the years.
This CL elevates type Func to be the canonical identifier for
a given Go function.
This looks like a trivial CL but in fact is the result of a lot of
scaffolding and rewriting, discarded once the result was achieved, to
separate out the three different kinds of Func nodes into three
separate fields, limited in use to each specific Node type, to
understand which Func fields are used by which Node types and what the
possible overlaps are. There were a few overlaps, most notably around
closures, which led to more fields being added to type Func to keep
them separate even though there is now a single Func instead of two
different ones for each function.
A future CL can and should change Curfn to be a *Func instead of
a *Node, finally eliminating the confusion about whether Curfn
is an ODCLFUNC node (as it is most of the time) or an ONAME node
(as it is when type-checking an inlined function body).
Although sizeof_test.go makes it look like Func is growing by two
words, there are now half as many Funcs in a running compilation,
so the memory footprint has actually been reduced substantially.
Change-Id: I598bd96c95728093dc769a835d48f2154a406a61
Reviewed-on: https://go-review.googlesource.com/c/go/+/272253
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2020-11-16 17:00:10 -05:00
|
|
|
{Func{}, 132, 240},
|
2018-03-06 18:30:58 -08:00
|
|
|
{Name{}, 32, 56},
|
2018-03-07 15:11:54 -08:00
|
|
|
{Param{}, 24, 48},
|
2017-04-25 18:14:12 -07:00
|
|
|
{Node{}, 76, 128},
|
2016-03-06 13:04:52 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
want := tt._32bit
|
|
|
|
|
if _64bit {
|
|
|
|
|
want = tt._64bit
|
|
|
|
|
}
|
|
|
|
|
got := reflect.TypeOf(tt.val).Size()
|
|
|
|
|
if want != got {
|
2016-03-06 18:05:41 -08:00
|
|
|
t.Errorf("unsafe.Sizeof(%T) = %d, want %d", tt.val, got, want)
|
2016-03-06 13:04:52 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|