2015-02-13 14:40:36 -05:00
|
|
|
// Copyright 2009 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-10-22 09:51:12 +09:00
|
|
|
// range
|
2015-02-13 14:40:36 -05:00
|
|
|
func typecheckrange(n *Node) {
|
|
|
|
|
var toomany int
|
|
|
|
|
var why string
|
|
|
|
|
var t1 *Type
|
|
|
|
|
var t2 *Type
|
|
|
|
|
var v1 *Node
|
|
|
|
|
var v2 *Node
|
2016-03-09 12:39:36 -08:00
|
|
|
var ls []*Node
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// Typechecking order is important here:
|
|
|
|
|
// 0. first typecheck range expression (slice/map/chan),
|
|
|
|
|
// it is evaluated only once and so logically it is not part of the loop.
|
|
|
|
|
// 1. typcheck produced values,
|
|
|
|
|
// this part can declare new vars and so it must be typechecked before body,
|
|
|
|
|
// because body can contain a closure that captures the vars.
|
|
|
|
|
// 2. decldepth++ to denote loop body.
|
|
|
|
|
// 3. typecheck body.
|
|
|
|
|
// 4. decldepth--.
|
|
|
|
|
|
|
|
|
|
typecheck(&n.Right, Erv)
|
|
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
t := n.Right.Type
|
2015-02-13 14:40:36 -05:00
|
|
|
if t == nil {
|
|
|
|
|
goto out
|
|
|
|
|
}
|
|
|
|
|
// delicate little dance. see typecheckas2
|
2016-03-09 12:39:36 -08:00
|
|
|
ls = n.List.Slice()
|
|
|
|
|
for i1, n1 := range ls {
|
2016-03-08 15:10:26 -08:00
|
|
|
if n1.Name == nil || n1.Name.Defn != n {
|
2016-03-09 12:39:36 -08:00
|
|
|
typecheck(&ls[i1], Erv|Easgn)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 07:54:01 +00:00
|
|
|
if Isptr[t.Etype] && Isfixedarray(t.Type) {
|
2015-02-13 14:40:36 -05:00
|
|
|
t = t.Type
|
|
|
|
|
}
|
|
|
|
|
n.Type = t
|
|
|
|
|
|
|
|
|
|
toomany = 0
|
|
|
|
|
switch t.Etype {
|
|
|
|
|
default:
|
2016-03-15 13:06:58 -07:00
|
|
|
Yyerror("cannot range over %v", Nconv(n.Right, FmtLong))
|
2015-02-13 14:40:36 -05:00
|
|
|
goto out
|
|
|
|
|
|
|
|
|
|
case TARRAY:
|
|
|
|
|
t1 = Types[TINT]
|
|
|
|
|
t2 = t.Type
|
|
|
|
|
|
|
|
|
|
case TMAP:
|
2016-03-10 05:22:14 -08:00
|
|
|
t1 = t.Key()
|
2015-02-13 14:40:36 -05:00
|
|
|
t2 = t.Type
|
|
|
|
|
|
|
|
|
|
case TCHAN:
|
2015-02-17 22:13:49 -05:00
|
|
|
if t.Chan&Crecv == 0 {
|
2015-04-17 12:03:22 -04:00
|
|
|
Yyerror("invalid operation: range %v (receive from send-only type %v)", n.Right, n.Right.Type)
|
2015-02-13 14:40:36 -05:00
|
|
|
goto out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t1 = t.Type
|
|
|
|
|
t2 = nil
|
2016-03-08 15:10:26 -08:00
|
|
|
if n.List.Len() == 2 {
|
2015-02-13 14:40:36 -05:00
|
|
|
toomany = 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case TSTRING:
|
|
|
|
|
t1 = Types[TINT]
|
|
|
|
|
t2 = runetype
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 15:10:26 -08:00
|
|
|
if n.List.Len() > 2 || toomany != 0 {
|
2015-02-13 14:40:36 -05:00
|
|
|
Yyerror("too many variables in range")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v1 = nil
|
2016-03-08 15:10:26 -08:00
|
|
|
if n.List.Len() != 0 {
|
|
|
|
|
v1 = n.List.First()
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
v2 = nil
|
2016-03-08 15:10:26 -08:00
|
|
|
if n.List.Len() > 1 {
|
|
|
|
|
v2 = n.List.Second()
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this is not only a optimization but also a requirement in the spec.
|
|
|
|
|
// "if the second iteration variable is the blank identifier, the range
|
|
|
|
|
// clause is equivalent to the same clause with only the first variable
|
|
|
|
|
// present."
|
|
|
|
|
if isblank(v2) {
|
|
|
|
|
if v1 != nil {
|
2016-03-10 10:13:42 -08:00
|
|
|
n.List.Set1(v1)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
v2 = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if v1 != nil {
|
2015-05-26 22:19:27 -04:00
|
|
|
if v1.Name != nil && v1.Name.Defn == n {
|
2015-02-13 14:40:36 -05:00
|
|
|
v1.Type = t1
|
|
|
|
|
} else if v1.Type != nil && assignop(t1, v1.Type, &why) == 0 {
|
2016-03-15 13:06:58 -07:00
|
|
|
Yyerror("cannot assign type %v to %v in range%s", t1, Nconv(v1, FmtLong), why)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
checkassign(n, v1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if v2 != nil {
|
2015-05-26 22:19:27 -04:00
|
|
|
if v2.Name != nil && v2.Name.Defn == n {
|
2015-02-13 14:40:36 -05:00
|
|
|
v2.Type = t2
|
|
|
|
|
} else if v2.Type != nil && assignop(t2, v2.Type, &why) == 0 {
|
2016-03-15 13:06:58 -07:00
|
|
|
Yyerror("cannot assign type %v to %v in range%s", t2, Nconv(v2, FmtLong), why)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
checkassign(n, v2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// second half of dance
|
|
|
|
|
out:
|
|
|
|
|
n.Typecheck = 1
|
2016-03-09 12:39:36 -08:00
|
|
|
ls = n.List.Slice()
|
|
|
|
|
for i1, n1 := range ls {
|
|
|
|
|
if n1.Typecheck == 0 {
|
|
|
|
|
typecheck(&ls[i1], Erv|Easgn)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decldepth++
|
2016-03-08 10:26:20 -08:00
|
|
|
typechecklist(n.Nbody.Slice(), Etop)
|
2015-02-13 14:40:36 -05:00
|
|
|
decldepth--
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func walkrange(n *Node) {
|
2015-03-05 13:57:36 -05:00
|
|
|
// variable name conventions:
|
|
|
|
|
// ohv1, hv1, hv2: hidden (old) val 1, 2
|
|
|
|
|
// ha, hit: hidden aggregate, iterator
|
|
|
|
|
// hn, hp: hidden len, pointer
|
|
|
|
|
// hb: hidden bool
|
|
|
|
|
// a, v1, v2: not hidden aggregate, val 1, 2
|
|
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
t := n.Type
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
a := n.Right
|
2016-03-02 17:34:42 -08:00
|
|
|
lno := setlineno(a)
|
2015-05-22 01:16:52 -04:00
|
|
|
n.Right = nil
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-03-02 14:22:05 -05:00
|
|
|
var v1 *Node
|
2016-03-08 15:10:26 -08:00
|
|
|
if n.List.Len() != 0 {
|
|
|
|
|
v1 = n.List.First()
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-03-02 14:22:05 -05:00
|
|
|
var v2 *Node
|
2016-03-08 15:10:26 -08:00
|
|
|
if n.List.Len() > 1 && !isblank(n.List.Second()) {
|
|
|
|
|
v2 = n.List.Second()
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// n->list has no meaning anymore, clear it
|
|
|
|
|
// to avoid erroneous processing by racewalk.
|
2016-03-08 15:10:26 -08:00
|
|
|
n.List.Set(nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-02-27 14:31:33 -08:00
|
|
|
var body []*Node
|
2016-03-08 10:26:20 -08:00
|
|
|
var init []*Node
|
2015-02-13 14:40:36 -05:00
|
|
|
switch t.Etype {
|
|
|
|
|
default:
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("walkrange")
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
case TARRAY:
|
2015-10-15 11:08:09 -07:00
|
|
|
if memclrrange(n, v1, v2, a) {
|
2016-03-02 17:34:42 -08:00
|
|
|
lineno = lno
|
2015-10-15 11:08:09 -07:00
|
|
|
return
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// orderstmt arranged for a copy of the array/slice variable if needed.
|
2015-02-23 16:07:24 -05:00
|
|
|
ha := a
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
hv1 := temp(Types[TINT])
|
|
|
|
|
hn := temp(Types[TINT])
|
2015-03-02 14:22:05 -05:00
|
|
|
var hp *Node
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-03-08 10:26:20 -08:00
|
|
|
init = append(init, Nod(OAS, hv1, nil))
|
|
|
|
|
init = append(init, Nod(OAS, hn, Nod(OLEN, ha, nil)))
|
2015-02-13 14:40:36 -05:00
|
|
|
if v2 != nil {
|
|
|
|
|
hp = temp(Ptrto(n.Type.Type))
|
2015-02-23 16:07:24 -05:00
|
|
|
tmp := Nod(OINDEX, ha, Nodintconst(0))
|
2015-02-17 22:13:49 -05:00
|
|
|
tmp.Bounded = true
|
2016-03-08 10:26:20 -08:00
|
|
|
init = append(init, Nod(OAS, hp, Nod(OADDR, tmp, nil)))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-05-26 21:30:20 -04:00
|
|
|
n.Left = Nod(OLT, hv1, hn)
|
2015-05-22 01:16:52 -04:00
|
|
|
n.Right = Nod(OAS, hv1, Nod(OADD, hv1, Nodintconst(1)))
|
2015-02-13 14:40:36 -05:00
|
|
|
if v1 == nil {
|
|
|
|
|
body = nil
|
|
|
|
|
} else if v2 == nil {
|
2016-02-27 14:31:33 -08:00
|
|
|
body = []*Node{Nod(OAS, v1, hv1)}
|
2015-02-13 14:40:36 -05:00
|
|
|
} else {
|
2015-02-23 16:07:24 -05:00
|
|
|
a := Nod(OAS2, nil, nil)
|
2016-03-08 15:10:26 -08:00
|
|
|
a.List.Set([]*Node{v1, v2})
|
|
|
|
|
a.Rlist.Set([]*Node{hv1, Nod(OIND, hp, nil)})
|
2016-02-27 14:31:33 -08:00
|
|
|
body = []*Node{a}
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// Advance pointer as part of increment.
|
|
|
|
|
// We used to advance the pointer before executing the loop body,
|
|
|
|
|
// but doing so would make the pointer point past the end of the
|
|
|
|
|
// array during the final iteration, possibly causing another unrelated
|
|
|
|
|
// piece of memory not to be garbage collected until the loop finished.
|
|
|
|
|
// Advancing during the increment ensures that the pointer p only points
|
|
|
|
|
// pass the end of the array during the final "p++; i++; if(i >= len(x)) break;",
|
|
|
|
|
// after which p is dead, so it cannot confuse the collector.
|
2015-02-23 16:07:24 -05:00
|
|
|
tmp := Nod(OADD, hp, Nodintconst(t.Type.Width))
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
tmp.Type = hp.Type
|
|
|
|
|
tmp.Typecheck = 1
|
|
|
|
|
tmp.Right.Type = Types[Tptr]
|
|
|
|
|
tmp.Right.Typecheck = 1
|
|
|
|
|
a = Nod(OAS, hp, tmp)
|
|
|
|
|
typecheck(&a, Etop)
|
2016-03-10 10:13:42 -08:00
|
|
|
n.Right.Ninit.Set1(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// orderstmt allocated the iterator for us.
|
|
|
|
|
// we only use a once, so no copy needed.
|
|
|
|
|
case TMAP:
|
2015-02-23 16:07:24 -05:00
|
|
|
ha := a
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-02-23 07:46:01 +00:00
|
|
|
th := hiter(t)
|
2015-05-26 23:05:35 -04:00
|
|
|
hit := prealloc[n]
|
2015-02-13 14:40:36 -05:00
|
|
|
hit.Type = th
|
|
|
|
|
n.Left = nil
|
cmd/compile: change ODOT and friends to use Sym, not Right
The Node type ODOT and its variants all represent a selector, with a
simple name to the right of the dot. Before this change this was
represented by using an ONAME Node in the Right field. This ONAME node
served no useful purpose. This CL changes these Node types to store the
symbol in the Sym field instead, thus not requiring allocating a Node
for each selector.
When compiling x/tools/go/types this CL eliminates nearly 5000 calls to
newname and reduces the total number of Nodes allocated by about 6.6%.
It seems to cut compilation time by 1 to 2 percent.
Getting this right was somewhat subtle, and I added two dubious changes
to produce the exact same output as before. One is to ishairy in
inl.go: the ONAME node increased the cost of ODOT and friends by 1, and
I retained that, although really ODOT is not more expensive than any
other node. The other is to varexpr in walk.go: because the ONAME in
the Right field of an ODOT has no class, varexpr would always return
false for an ODOT, although in fact for some ODOT's it seemingly ought
to return true; I added an && false for now. I will send separate CLs,
that will break toolstash -cmp, to clean these up.
This CL passes toolstash -cmp.
Change-Id: I4af8a10cc59078c436130ce472f25abc3a9b2f80
Reviewed-on: https://go-review.googlesource.com/20890
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-03-18 16:52:30 -07:00
|
|
|
keysym := th.Field(0).Sym // depends on layout of iterator struct. See reflect.go:hiter
|
|
|
|
|
valsym := th.Field(1).Sym // ditto
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-03-04 15:19:06 -08:00
|
|
|
fn := syslook("mapiterinit")
|
2016-02-23 07:46:01 +00:00
|
|
|
|
2016-03-10 05:22:14 -08:00
|
|
|
substArgTypes(&fn, t.Key(), t.Type, th)
|
2016-03-08 10:26:20 -08:00
|
|
|
init = append(init, mkcall1(fn, nil, nil, typename(t), ha, Nod(OADDR, hit, nil)))
|
cmd/compile: change ODOT and friends to use Sym, not Right
The Node type ODOT and its variants all represent a selector, with a
simple name to the right of the dot. Before this change this was
represented by using an ONAME Node in the Right field. This ONAME node
served no useful purpose. This CL changes these Node types to store the
symbol in the Sym field instead, thus not requiring allocating a Node
for each selector.
When compiling x/tools/go/types this CL eliminates nearly 5000 calls to
newname and reduces the total number of Nodes allocated by about 6.6%.
It seems to cut compilation time by 1 to 2 percent.
Getting this right was somewhat subtle, and I added two dubious changes
to produce the exact same output as before. One is to ishairy in
inl.go: the ONAME node increased the cost of ODOT and friends by 1, and
I retained that, although really ODOT is not more expensive than any
other node. The other is to varexpr in walk.go: because the ONAME in
the Right field of an ODOT has no class, varexpr would always return
false for an ODOT, although in fact for some ODOT's it seemingly ought
to return true; I added an && false for now. I will send separate CLs,
that will break toolstash -cmp, to clean these up.
This CL passes toolstash -cmp.
Change-Id: I4af8a10cc59078c436130ce472f25abc3a9b2f80
Reviewed-on: https://go-review.googlesource.com/20890
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-03-18 16:52:30 -07:00
|
|
|
n.Left = Nod(ONE, NodSym(ODOT, hit, keysym), nodnil())
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-03-04 15:19:06 -08:00
|
|
|
fn = syslook("mapiternext")
|
|
|
|
|
substArgTypes(&fn, th)
|
2016-02-23 07:46:01 +00:00
|
|
|
n.Right = mkcall1(fn, nil, nil, Nod(OADDR, hit, nil))
|
2015-02-13 14:40:36 -05:00
|
|
|
|
cmd/compile: change ODOT and friends to use Sym, not Right
The Node type ODOT and its variants all represent a selector, with a
simple name to the right of the dot. Before this change this was
represented by using an ONAME Node in the Right field. This ONAME node
served no useful purpose. This CL changes these Node types to store the
symbol in the Sym field instead, thus not requiring allocating a Node
for each selector.
When compiling x/tools/go/types this CL eliminates nearly 5000 calls to
newname and reduces the total number of Nodes allocated by about 6.6%.
It seems to cut compilation time by 1 to 2 percent.
Getting this right was somewhat subtle, and I added two dubious changes
to produce the exact same output as before. One is to ishairy in
inl.go: the ONAME node increased the cost of ODOT and friends by 1, and
I retained that, although really ODOT is not more expensive than any
other node. The other is to varexpr in walk.go: because the ONAME in
the Right field of an ODOT has no class, varexpr would always return
false for an ODOT, although in fact for some ODOT's it seemingly ought
to return true; I added an && false for now. I will send separate CLs,
that will break toolstash -cmp, to clean these up.
This CL passes toolstash -cmp.
Change-Id: I4af8a10cc59078c436130ce472f25abc3a9b2f80
Reviewed-on: https://go-review.googlesource.com/20890
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-03-18 16:52:30 -07:00
|
|
|
key := NodSym(ODOT, hit, keysym)
|
2015-02-13 14:40:36 -05:00
|
|
|
key = Nod(OIND, key, nil)
|
|
|
|
|
if v1 == nil {
|
|
|
|
|
body = nil
|
|
|
|
|
} else if v2 == nil {
|
2016-02-27 14:31:33 -08:00
|
|
|
body = []*Node{Nod(OAS, v1, key)}
|
2015-02-13 14:40:36 -05:00
|
|
|
} else {
|
cmd/compile: change ODOT and friends to use Sym, not Right
The Node type ODOT and its variants all represent a selector, with a
simple name to the right of the dot. Before this change this was
represented by using an ONAME Node in the Right field. This ONAME node
served no useful purpose. This CL changes these Node types to store the
symbol in the Sym field instead, thus not requiring allocating a Node
for each selector.
When compiling x/tools/go/types this CL eliminates nearly 5000 calls to
newname and reduces the total number of Nodes allocated by about 6.6%.
It seems to cut compilation time by 1 to 2 percent.
Getting this right was somewhat subtle, and I added two dubious changes
to produce the exact same output as before. One is to ishairy in
inl.go: the ONAME node increased the cost of ODOT and friends by 1, and
I retained that, although really ODOT is not more expensive than any
other node. The other is to varexpr in walk.go: because the ONAME in
the Right field of an ODOT has no class, varexpr would always return
false for an ODOT, although in fact for some ODOT's it seemingly ought
to return true; I added an && false for now. I will send separate CLs,
that will break toolstash -cmp, to clean these up.
This CL passes toolstash -cmp.
Change-Id: I4af8a10cc59078c436130ce472f25abc3a9b2f80
Reviewed-on: https://go-review.googlesource.com/20890
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-03-18 16:52:30 -07:00
|
|
|
val := NodSym(ODOT, hit, valsym)
|
2015-02-13 14:40:36 -05:00
|
|
|
val = Nod(OIND, val, nil)
|
2015-02-23 16:07:24 -05:00
|
|
|
a := Nod(OAS2, nil, nil)
|
2016-03-08 15:10:26 -08:00
|
|
|
a.List.Set([]*Node{v1, v2})
|
|
|
|
|
a.Rlist.Set([]*Node{key, val})
|
2016-02-27 14:31:33 -08:00
|
|
|
body = []*Node{a}
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// orderstmt arranged for a copy of the channel variable.
|
|
|
|
|
case TCHAN:
|
2015-02-23 16:07:24 -05:00
|
|
|
ha := a
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-05-26 21:30:20 -04:00
|
|
|
n.Left = nil
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
hv1 := temp(t.Type)
|
2015-02-13 14:40:36 -05:00
|
|
|
hv1.Typecheck = 1
|
|
|
|
|
if haspointers(t.Type) {
|
2016-03-08 10:26:20 -08:00
|
|
|
init = append(init, Nod(OAS, hv1, nil))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-02-23 16:07:24 -05:00
|
|
|
hb := temp(Types[TBOOL])
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-05-26 21:30:20 -04:00
|
|
|
n.Left = Nod(ONE, hb, Nodbool(false))
|
2015-02-23 16:07:24 -05:00
|
|
|
a := Nod(OAS2RECV, nil, nil)
|
2015-02-13 14:40:36 -05:00
|
|
|
a.Typecheck = 1
|
2016-03-08 15:10:26 -08:00
|
|
|
a.List.Set([]*Node{hv1, hb})
|
2016-03-10 10:13:42 -08:00
|
|
|
a.Rlist.Set1(Nod(ORECV, ha, nil))
|
|
|
|
|
n.Left.Ninit.Set1(a)
|
2015-02-13 14:40:36 -05:00
|
|
|
if v1 == nil {
|
|
|
|
|
body = nil
|
|
|
|
|
} else {
|
2016-02-27 14:31:33 -08:00
|
|
|
body = []*Node{Nod(OAS, v1, hv1)}
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// orderstmt arranged for a copy of the string variable.
|
|
|
|
|
case TSTRING:
|
2015-02-23 16:07:24 -05:00
|
|
|
ha := a
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
ohv1 := temp(Types[TINT])
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
hv1 := temp(Types[TINT])
|
2016-03-08 10:26:20 -08:00
|
|
|
init = append(init, Nod(OAS, hv1, nil))
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
var a *Node
|
2015-03-02 20:34:22 -05:00
|
|
|
var hv2 *Node
|
2015-02-13 14:40:36 -05:00
|
|
|
if v2 == nil {
|
|
|
|
|
a = Nod(OAS, hv1, mkcall("stringiter", Types[TINT], nil, ha, hv1))
|
|
|
|
|
} else {
|
|
|
|
|
hv2 = temp(runetype)
|
|
|
|
|
a = Nod(OAS2, nil, nil)
|
2016-03-08 15:10:26 -08:00
|
|
|
a.List.Set([]*Node{hv1, hv2})
|
2016-03-04 15:19:06 -08:00
|
|
|
fn := syslook("stringiter2")
|
2016-03-10 10:13:42 -08:00
|
|
|
a.Rlist.Set1(mkcall1(fn, fn.Type.Results(), nil, ha, hv1))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-05-26 21:30:20 -04:00
|
|
|
n.Left = Nod(ONE, hv1, Nodintconst(0))
|
2016-03-08 15:10:26 -08:00
|
|
|
n.Left.Ninit.Set([]*Node{Nod(OAS, ohv1, hv1), a})
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
body = nil
|
|
|
|
|
if v1 != nil {
|
2016-02-27 14:31:33 -08:00
|
|
|
body = []*Node{Nod(OAS, v1, ohv1)}
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
if v2 != nil {
|
2016-02-27 14:31:33 -08:00
|
|
|
body = append(body, Nod(OAS, v2, hv2))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n.Op = OFOR
|
|
|
|
|
typechecklist(init, Etop)
|
2016-03-08 15:10:26 -08:00
|
|
|
n.Ninit.Append(init...)
|
2016-03-08 10:26:20 -08:00
|
|
|
typechecklist(n.Left.Ninit.Slice(), Etop)
|
2015-05-26 21:30:20 -04:00
|
|
|
typecheck(&n.Left, Erv)
|
2015-05-22 01:16:52 -04:00
|
|
|
typecheck(&n.Right, Etop)
|
2016-02-27 14:31:33 -08:00
|
|
|
typecheckslice(body, Etop)
|
|
|
|
|
n.Nbody.Set(append(body, n.Nbody.Slice()...))
|
2015-02-13 14:40:36 -05:00
|
|
|
walkstmt(&n)
|
|
|
|
|
|
2016-03-02 17:34:42 -08:00
|
|
|
lineno = lno
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2015-10-15 11:08:09 -07:00
|
|
|
|
|
|
|
|
// Lower n into runtime·memclr if possible, for
|
|
|
|
|
// fast zeroing of slices and arrays (issue 5373).
|
|
|
|
|
// Look for instances of
|
|
|
|
|
//
|
|
|
|
|
// for i := range a {
|
|
|
|
|
// a[i] = zero
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// in which the evaluation of a is side-effect-free.
|
|
|
|
|
//
|
|
|
|
|
// Parameters are as in walkrange: "for v1, v2 = range a".
|
|
|
|
|
func memclrrange(n, v1, v2, a *Node) bool {
|
2015-10-20 10:00:07 -07:00
|
|
|
if Debug['N'] != 0 || instrumenting {
|
2015-10-15 11:08:09 -07:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if v1 == nil || v2 != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2016-03-09 12:39:36 -08:00
|
|
|
if n.Nbody.Len() == 0 || n.Nbody.First() == nil || n.Nbody.Len() > 1 {
|
2015-10-15 11:08:09 -07:00
|
|
|
return false
|
|
|
|
|
}
|
2016-03-09 12:39:36 -08:00
|
|
|
stmt := n.Nbody.First() // only stmt in body
|
2015-10-15 11:08:09 -07:00
|
|
|
if stmt.Op != OAS || stmt.Left.Op != OINDEX {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if !samesafeexpr(stmt.Left.Left, a) || !samesafeexpr(stmt.Left.Right, v1) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
elemsize := n.Type.Type.Width
|
|
|
|
|
if elemsize <= 0 || !iszero(stmt.Right) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert to
|
|
|
|
|
// if len(a) != 0 {
|
|
|
|
|
// hp = &a[0]
|
|
|
|
|
// hn = len(a)*sizeof(elem(a))
|
|
|
|
|
// memclr(hp, hn)
|
|
|
|
|
// i = len(a) - 1
|
|
|
|
|
// }
|
|
|
|
|
n.Op = OIF
|
|
|
|
|
|
2016-02-27 14:31:33 -08:00
|
|
|
n.Nbody.Set(nil)
|
2015-10-15 11:08:09 -07:00
|
|
|
n.Left = Nod(ONE, Nod(OLEN, a, nil), Nodintconst(0))
|
|
|
|
|
|
|
|
|
|
// hp = &a[0]
|
|
|
|
|
hp := temp(Ptrto(Types[TUINT8]))
|
|
|
|
|
|
|
|
|
|
tmp := Nod(OINDEX, a, Nodintconst(0))
|
|
|
|
|
tmp.Bounded = true
|
|
|
|
|
tmp = Nod(OADDR, tmp, nil)
|
|
|
|
|
tmp = Nod(OCONVNOP, tmp, nil)
|
|
|
|
|
tmp.Type = Ptrto(Types[TUINT8])
|
2016-02-27 14:31:33 -08:00
|
|
|
n.Nbody.Append(Nod(OAS, hp, tmp))
|
2015-10-15 11:08:09 -07:00
|
|
|
|
|
|
|
|
// hn = len(a) * sizeof(elem(a))
|
|
|
|
|
hn := temp(Types[TUINTPTR])
|
|
|
|
|
|
|
|
|
|
tmp = Nod(OLEN, a, nil)
|
|
|
|
|
tmp = Nod(OMUL, tmp, Nodintconst(elemsize))
|
|
|
|
|
tmp = conv(tmp, Types[TUINTPTR])
|
2016-02-27 14:31:33 -08:00
|
|
|
n.Nbody.Append(Nod(OAS, hn, tmp))
|
2015-10-15 11:08:09 -07:00
|
|
|
|
|
|
|
|
// memclr(hp, hn)
|
|
|
|
|
fn := mkcall("memclr", nil, nil, hp, hn)
|
|
|
|
|
|
2016-02-27 14:31:33 -08:00
|
|
|
n.Nbody.Append(fn)
|
2015-10-15 11:08:09 -07:00
|
|
|
|
|
|
|
|
// i = len(a) - 1
|
|
|
|
|
v1 = Nod(OAS, v1, Nod(OSUB, Nod(OLEN, a, nil), Nodintconst(1)))
|
|
|
|
|
|
2016-02-27 14:31:33 -08:00
|
|
|
n.Nbody.Append(v1)
|
2015-10-15 11:08:09 -07:00
|
|
|
|
|
|
|
|
typecheck(&n.Left, Erv)
|
2016-03-08 10:26:20 -08:00
|
|
|
typechecklist(n.Nbody.Slice(), Etop)
|
2015-10-15 11:08:09 -07:00
|
|
|
walkstmt(&n)
|
|
|
|
|
return true
|
|
|
|
|
}
|