From: Josh Bleecher Snyder Date: Wed, 26 Apr 2017 17:18:41 +0000 (-0700) Subject: cmd/compile: minor cleanup X-Git-Tag: go1.9beta1~454 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=e1a7db7f3b9c2d684c0ac4b0cc2c845f8b70fd85;p=gostls13.git cmd/compile: minor cleanup Follow-up to review comments on CL 41797. Mask the input to set2 and set3, so that at the very least, we won't corrupt the rest of the flags in case of a bad input. It also seems more semantically appropriate. Do minor cleanup in addrescapes. I started on larger cleanup, but it wasn't clear that it was an improvement. Add warning comments and sanity checks to Initorder and Class constants, to attempt to prevent them from overflowing their allotted flag bits. Passes toolstash-check. Change-Id: I57b9661ba36f56406aa7a1d8da9b7c70338f9119 Reviewed-on: https://go-review.googlesource.com/41817 Run-TryBot: Josh Bleecher Snyder Reviewed-by: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- diff --git a/src/cmd/compile/internal/gc/bitset.go b/src/cmd/compile/internal/gc/bitset.go index 89e6fe85c8..90babd5a9f 100644 --- a/src/cmd/compile/internal/gc/bitset.go +++ b/src/cmd/compile/internal/gc/bitset.go @@ -28,20 +28,22 @@ func (f bitset32) get2(shift uint8) uint8 { return uint8(f>>shift) & 3 } +// set2 sets two bits in f using the bottom two bits of b. func (f *bitset32) set2(shift uint8, b uint8) { // Clear old bits. *(*uint32)(f) &^= 3 << shift // Set new bits. - *(*uint32)(f) |= uint32(b) << shift + *(*uint32)(f) |= uint32(b&3) << shift } func (f bitset32) get3(shift uint8) uint8 { return uint8(f>>shift) & 7 } +// set3 sets three bits in f using the bottom three bits of b. func (f *bitset32) set3(shift uint8, b uint8) { // Clear old bits. *(*uint32)(f) &^= 7 << shift // Set new bits. - *(*uint32)(f) |= uint32(b) << shift + *(*uint32)(f) |= uint32(b&7) << shift } diff --git a/src/cmd/compile/internal/gc/gen.go b/src/cmd/compile/internal/gc/gen.go index fffc5b76dd..941c41502a 100644 --- a/src/cmd/compile/internal/gc/gen.go +++ b/src/cmd/compile/internal/gc/gen.go @@ -24,10 +24,11 @@ func Sysfunc(name string) *obj.LSym { // to be taken. func addrescapes(n *Node) { switch n.Op { - // probably a type error already. - // dump("addrescapes", n); default: - break + // Unexpected Op, probably due to a previous type error. Ignore. + + case OIND, ODOTPTR: + // Nothing to do. case ONAME: if n == nodfp { @@ -73,9 +74,6 @@ func addrescapes(n *Node) { Curfn = oldfn lineno = ln - case OIND, ODOTPTR: - break - // ODOTPTR has already been introduced, // so these are the non-pointer ODOT and OINDEX. // In &x[0], if x is a slice, then x does not diff --git a/src/cmd/compile/internal/gc/go.go b/src/cmd/compile/internal/gc/go.go index 46cec3e8bc..f795ce0bf0 100644 --- a/src/cmd/compile/internal/gc/go.go +++ b/src/cmd/compile/internal/gc/go.go @@ -31,17 +31,25 @@ func isRuntimePkg(p *types.Pkg) bool { type Class uint8 const ( - Pxxx Class = iota - PEXTERN // global variable - PAUTO // local variables - PAUTOHEAP // local variable or parameter moved to heap - PPARAM // input arguments - PPARAMOUT // output results - PFUNC // global function + Pxxx Class = iota // no class; used during ssa conversion to indicate pseudo-variables + PEXTERN // global variable + PAUTO // local variables + PAUTOHEAP // local variable or parameter moved to heap + PPARAM // input arguments + PPARAMOUT // output results + PFUNC // global function PDISCARD // discard during parse of duplicate import + // Careful: Class is stored in three bits in Node.flags. + // Adding a new Class will overflow that. ) +func init() { + if PDISCARD != 7 { + panic("PDISCARD changed; does all Class values still fit in three bits?") + } +} + // note this is the runtime representation // of the compilers arrays. // diff --git a/src/cmd/compile/internal/gc/sinit.go b/src/cmd/compile/internal/gc/sinit.go index e8f3d70bd1..1a1dbc0de7 100644 --- a/src/cmd/compile/internal/gc/sinit.go +++ b/src/cmd/compile/internal/gc/sinit.go @@ -9,11 +9,12 @@ import ( "fmt" ) -// static initialization +// Static initialization ordering state. +// These values are stored in two bits in Node.flags. const ( - InitNotStarted = 0 - InitDone = 1 - InitPending = 2 + InitNotStarted = iota + InitDone + InitPending ) type InitEntry struct {