Skip to content

Commit

Permalink
cmd/compile: be explicit about NodeList List storage stealing
Browse files Browse the repository at this point in the history
This only matters in a couple of places.
Be explicit about it.

This helps to narrow the scope of any given
NodeList-related change.

Change-Id: I506f2ee0417bc6eb70d4bc0456d87a92cd6f7973
  • Loading branch information
josharian committed Jun 11, 2015
1 parent af8d72f commit 497ed0c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/cmd/compile/internal/gc/go.y
Expand Up @@ -774,7 +774,7 @@ if_stmt:
if nn.N.Op == OIF {
popdcl();
}
n.Rlist = list1(nn.N);
n.Rlist = list1Steal(nn.N)
n = nn.N;
}
}
Expand Down Expand Up @@ -1811,7 +1811,7 @@ non_dcl_stmt:
$1.Name.Defn = $4;
l = list1($1);
if $4 != nil {
l = list(l, $4);
l = listSteal(l, $4)
}
$$ = liststmt(l);
}
Expand Down Expand Up @@ -1868,14 +1868,14 @@ stmt_list:
{
$$ = nil;
if $1 != nil {
$$ = list1($1);
$$ = list1Steal($1)
}
}
| stmt_list ';' stmt
{
$$ = $1;
if $3 != nil {
$$ = list($$, $3);
$$ = listSteal($$, $3)
}
}

Expand Down
27 changes: 18 additions & 9 deletions src/cmd/compile/internal/gc/syntax.go
Expand Up @@ -387,15 +387,6 @@ func list1(n *Node) *NodeList {
if n == nil {
return nil
}
if n.Op == OBLOCK && n.Ninit == nil {
// Flatten list and steal storage.
// Poison pointer to catch errant uses.
l := n.List

n.List = nil
return l
}

l := new(NodeList)
l.N = n
l.End = l
Expand All @@ -407,6 +398,24 @@ func list(l *NodeList, n *Node) *NodeList {
return concat(l, list1(n))
}

func list1Steal(n *Node) *NodeList {
if n == nil {
return nil
}
if n.Op == OBLOCK && n.Ninit == nil {
// Flatten list and steal storage.
// Poison pointer to catch errant uses.
l := n.List
n.List = nil
return l
}
return list1(n)
}

func listSteal(l *NodeList, n *Node) *NodeList {
return concat(l, list1Steal(n))
}

// listsort sorts *l in place according to the 3-way comparison function f.
// The algorithm is mergesort, so it is guaranteed to be O(n log n).
func listsort(l **NodeList, f func(*Node, *Node) int) {
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/compile/internal/gc/y.go
Expand Up @@ -1882,7 +1882,7 @@ yydefault:
if nn.N.Op == OIF {
popdcl()
}
n.Rlist = list1(nn.N)
n.Rlist = list1Steal(nn.N)
n = nn.N
}
}
Expand Down Expand Up @@ -2985,7 +2985,7 @@ yydefault:
yyDollar[1].node.Name.Defn = yyDollar[4].node
l = list1(yyDollar[1].node)
if yyDollar[4].node != nil {
l = list(l, yyDollar[4].node)
l = listSteal(l, yyDollar[4].node)
}
yyVAL.node = liststmt(l)
}
Expand Down Expand Up @@ -3056,7 +3056,7 @@ yydefault:
{
yyVAL.list = nil
if yyDollar[1].node != nil {
yyVAL.list = list1(yyDollar[1].node)
yyVAL.list = list1Steal(yyDollar[1].node)
}
}
case 271:
Expand All @@ -3065,7 +3065,7 @@ yydefault:
{
yyVAL.list = yyDollar[1].list
if yyDollar[3].node != nil {
yyVAL.list = list(yyVAL.list, yyDollar[3].node)
yyVAL.list = listSteal(yyVAL.list, yyDollar[3].node)
}
}
case 272:
Expand Down

0 comments on commit 497ed0c

Please sign in to comment.