Skip to content

Commit

Permalink
cmd/compile: remove funcdepth variables
Browse files Browse the repository at this point in the history
There were only two large classes of use for these variables:

1) Testing "funcdepth != 0" or "funcdepth > 0", which is equivalent to
checking "Curfn != nil".

2) In oldname, detecting whether a closure variable has been created
for the current function, which can be handled by instead testing
"n.Name.Curfn != Curfn".

Lastly, merge funcstart into funchdr, since it's only called once, and
it better matches up with funcbody now.

Passes toolstash-check.

Change-Id: I8fe159a9d37ef7debc4cd310354cea22a8b23394
Reviewed-on: https://go-review.googlesource.com/99076
Run-TryBot: Matthew Dempsky <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Brad Fitzpatrick <[email protected]>
  • Loading branch information
mdempsky committed Mar 7, 2018
1 parent aa00ca1 commit d7eb490
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/cmd/compile/internal/gc/bimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ func Import(imp *types.Pkg, in *bufio.Reader) {
}
i0 = i

if funcdepth != 0 {
p.formatErrorf("unexpected Funcdepth %d", funcdepth)
if Curfn != nil {
p.formatErrorf("unexpected Curfn %v", Curfn)
}

// Note: In the original code, funchdr and funcbody are called for
Expand Down
3 changes: 0 additions & 3 deletions src/cmd/compile/internal/gc/closure.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func (p *noder) funcLit(expr *syntax.FuncLit) *Node {
n := p.nod(expr, OCLOSURE, nil, nil)
n.Func.SetIsHiddenClosure(Curfn != nil)
n.Func.Ntype = ntype
n.Func.Depth = funcdepth
n.Func.Outerfunc = Curfn

old := p.funchdr(n)
Expand Down Expand Up @@ -220,8 +219,6 @@ func makeclosure(func_ *Node) *Node {
xfunc.Func.Nname.Name.Param.Ntype = xtype
xfunc.Func.Nname.Name.Defn = xfunc
declare(xfunc.Func.Nname, PFUNC)
xfunc.Func.Nname.Name.Funcdepth = func_.Func.Depth
xfunc.Func.Depth = func_.Func.Depth
xfunc.Func.Endlineno = func_.Func.Endlineno
if Ctxt.Flag_dynlink {
makefuncsym(xfunc.Func.Nname.Sym)
Expand Down
31 changes: 10 additions & 21 deletions src/cmd/compile/internal/gc/dcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ func declare(n *Node, ctxt Class) {
s.Lastlineno = lineno
s.Def = asTypesNode(n)
n.Name.Vargen = int32(gen)
n.Name.Funcdepth = funcdepth
n.SetClass(ctxt)

autoexport(n, ctxt)
Expand Down Expand Up @@ -160,7 +159,7 @@ func variter(vl []*Node, t *Node, el []*Node) []*Node {
declare(v, dclcontext)
v.Name.Param.Ntype = t
v.Name.Defn = as2
if funcdepth > 0 {
if Curfn != nil {
init = append(init, nod(ODCL, v, nil))
}
}
Expand All @@ -183,8 +182,8 @@ func variter(vl []*Node, t *Node, el []*Node) []*Node {
declare(v, dclcontext)
v.Name.Param.Ntype = t

if e != nil || funcdepth > 0 || isblank(v) {
if funcdepth > 0 {
if e != nil || Curfn != nil || isblank(v) {
if Curfn != nil {
init = append(init, nod(ODCL, v, nil))
}
e = nod(OAS, v, e)
Expand Down Expand Up @@ -276,23 +275,22 @@ func oldname(s *types.Sym) *Node {
return newnoname(s)
}

if Curfn != nil && n.Op == ONAME && n.Name.Funcdepth > 0 && n.Name.Funcdepth != funcdepth {
if Curfn != nil && n.Op == ONAME && n.Name.Curfn != nil && n.Name.Curfn != Curfn {
// Inner func is referring to var in outer func.
//
// TODO(rsc): If there is an outer variable x and we
// are parsing x := 5 inside the closure, until we get to
// the := it looks like a reference to the outer x so we'll
// make x a closure variable unnecessarily.
c := n.Name.Param.Innermost
if c == nil || c.Name.Funcdepth != funcdepth {
if c == nil || c.Name.Curfn != Curfn {
// Do not have a closure var for the active closure yet; make one.
c = newname(s)
c.SetClass(PAUTOHEAP)
c.SetIsClosureVar(true)
c.SetIsddd(n.Isddd())
c.Name.Defn = n
c.SetAddable(false)
c.Name.Funcdepth = funcdepth

// Link into list of active closure variables.
// Popped from list in func closurebody.
Expand Down Expand Up @@ -384,12 +382,14 @@ func ifacedcl(n *Node) {
// returns in auto-declaration context.
func funchdr(n *Node) {
// change the declaration context from extern to auto
if funcdepth == 0 && dclcontext != PEXTERN {
if Curfn == nil && dclcontext != PEXTERN {
Fatalf("funchdr: dclcontext = %d", dclcontext)
}

dclcontext = PAUTO
funcstart(n)
types.Markdcl()
funcstack = append(funcstack, Curfn)
Curfn = n

if n.Func.Nname != nil {
funcargs(n.Func.Nname.Name.Param.Ntype)
Expand Down Expand Up @@ -523,16 +523,6 @@ func funcargs2(t *types.Type) {
}

var funcstack []*Node // stack of previous values of Curfn
var funcdepth int32 // len(funcstack) during parsing, but then forced to be the same later during compilation

// start the function.
// called before funcargs; undone at end of funcbody.
func funcstart(n *Node) {
types.Markdcl()
funcstack = append(funcstack, Curfn)
funcdepth++
Curfn = n
}

// finish the body.
// called in auto-declaration context.
Expand All @@ -544,8 +534,7 @@ func funcbody() {
}
types.Popdcl()
funcstack, Curfn = funcstack[:len(funcstack)-1], funcstack[len(funcstack)-1]
funcdepth--
if funcdepth == 0 {
if Curfn == nil {
dclcontext = PEXTERN
}
}
Expand Down
7 changes: 0 additions & 7 deletions src/cmd/compile/internal/gc/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,6 @@ func (n *Node) jconv(s fmt.State, flag FmtFlag) {
fmt.Fprintf(s, " colas(%v)", n.Colas())
}

if n.Name != nil && n.Name.Funcdepth != 0 {
fmt.Fprintf(s, " f(%d)", n.Name.Funcdepth)
}
if n.Func != nil && n.Func.Depth != 0 {
fmt.Fprintf(s, " ff(%d)", n.Func.Depth)
}

switch n.Esc {
case EscUnknown:
break
Expand Down
2 changes: 0 additions & 2 deletions src/cmd/compile/internal/gc/pgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,11 @@ func funccompile(fn *Node) {
}

dclcontext = PAUTO
funcdepth = fn.Func.Depth + 1
Curfn = fn

compile(fn)

Curfn = nil
funcdepth = 0
dclcontext = PEXTERN
}

Expand Down
4 changes: 2 additions & 2 deletions src/cmd/compile/internal/gc/sizeof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func TestSizeof(t *testing.T) {
_32bit uintptr // size on 32bit platforms
_64bit uintptr // size on 64bit platforms
}{
{Func{}, 132, 232},
{Name{}, 36, 56},
{Func{}, 128, 232},
{Name{}, 32, 56},
{Param{}, 28, 56},
{Node{}, 76, 128},
}
Expand Down
2 changes: 0 additions & 2 deletions src/cmd/compile/internal/gc/syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ type Name struct {
Param *Param // additional fields for ONAME, OTYPE
Decldepth int32 // declaration loop depth, increased for every loop or label
Vargen int32 // unique name for ONAME within a function. Function outputs are numbered starting at one.
Funcdepth int32

used bool // for variable declared and not used error
flags bitset8
Expand Down Expand Up @@ -433,7 +432,6 @@ type Func struct {

Inl Nodes // copy of the body for use in inlining
InlCost int32
Depth int32

Label int32 // largest auto-generated label in this function

Expand Down
4 changes: 0 additions & 4 deletions src/cmd/compile/internal/gc/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3868,9 +3868,6 @@ func wrapCall(n *Node, init *Nodes) *Node {
args = append(args, a.Left)
}

oldfn := Curfn
Curfn = nil

wrapCall_prgen++
sym := lookupN("wrap·", wrapCall_prgen)
fn := dclfunc(sym, t)
Expand All @@ -3885,7 +3882,6 @@ func wrapCall(n *Node, init *Nodes) *Node {
fn = typecheck(fn, Etop)
typecheckslice(fn.Nbody.Slice(), Etop)
xtop = append(xtop, fn)
Curfn = oldfn

a = nod(OCALL, nil, nil)
a.Left = fn.Func.Nname
Expand Down

0 comments on commit d7eb490

Please sign in to comment.