github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/cmd/compile/internal/gc/range.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gc
     6  
     7  import (
     8  	"cmd/compile/internal/types"
     9  	"unicode/utf8"
    10  )
    11  
    12  // range
    13  func typecheckrange(n *Node) {
    14  	var toomany int
    15  	var why string
    16  	var t1 *types.Type
    17  	var t2 *types.Type
    18  	var v1 *Node
    19  	var v2 *Node
    20  	var ls []*Node
    21  
    22  	// Typechecking order is important here:
    23  	// 0. first typecheck range expression (slice/map/chan),
    24  	//	it is evaluated only once and so logically it is not part of the loop.
    25  	// 1. typcheck produced values,
    26  	//	this part can declare new vars and so it must be typechecked before body,
    27  	//	because body can contain a closure that captures the vars.
    28  	// 2. decldepth++ to denote loop body.
    29  	// 3. typecheck body.
    30  	// 4. decldepth--.
    31  
    32  	n.Right = typecheck(n.Right, Erv)
    33  
    34  	t := n.Right.Type
    35  	if t == nil {
    36  		goto out
    37  	}
    38  	// delicate little dance.  see typecheckas2
    39  	ls = n.List.Slice()
    40  	for i1, n1 := range ls {
    41  		if n1.Name == nil || n1.Name.Defn != n {
    42  			ls[i1] = typecheck(ls[i1], Erv|Easgn)
    43  		}
    44  	}
    45  
    46  	if t.IsPtr() && t.Elem().IsArray() {
    47  		t = t.Elem()
    48  	}
    49  	n.Type = t
    50  
    51  	toomany = 0
    52  	switch t.Etype {
    53  	default:
    54  		yyerror("cannot range over %L", n.Right)
    55  		goto out
    56  
    57  	case TARRAY, TSLICE:
    58  		t1 = types.Types[TINT]
    59  		t2 = t.Elem()
    60  
    61  	case TMAP:
    62  		t1 = t.Key()
    63  		t2 = t.Val()
    64  
    65  	case TCHAN:
    66  		if !t.ChanDir().CanRecv() {
    67  			yyerror("invalid operation: range %v (receive from send-only type %v)", n.Right, n.Right.Type)
    68  			goto out
    69  		}
    70  
    71  		t1 = t.Elem()
    72  		t2 = nil
    73  		if n.List.Len() == 2 {
    74  			toomany = 1
    75  		}
    76  
    77  	case TSTRING:
    78  		t1 = types.Types[TINT]
    79  		t2 = types.Runetype
    80  	}
    81  
    82  	if n.List.Len() > 2 || toomany != 0 {
    83  		yyerror("too many variables in range")
    84  	}
    85  
    86  	v1 = nil
    87  	if n.List.Len() != 0 {
    88  		v1 = n.List.First()
    89  	}
    90  	v2 = nil
    91  	if n.List.Len() > 1 {
    92  		v2 = n.List.Second()
    93  	}
    94  
    95  	// this is not only a optimization but also a requirement in the spec.
    96  	// "if the second iteration variable is the blank identifier, the range
    97  	// clause is equivalent to the same clause with only the first variable
    98  	// present."
    99  	if isblank(v2) {
   100  		if v1 != nil {
   101  			n.List.Set1(v1)
   102  		}
   103  		v2 = nil
   104  	}
   105  
   106  	if v1 != nil {
   107  		if v1.Name != nil && v1.Name.Defn == n {
   108  			v1.Type = t1
   109  		} else if v1.Type != nil && assignop(t1, v1.Type, &why) == 0 {
   110  			yyerror("cannot assign type %v to %L in range%s", t1, v1, why)
   111  		}
   112  		checkassign(n, v1)
   113  	}
   114  
   115  	if v2 != nil {
   116  		if v2.Name != nil && v2.Name.Defn == n {
   117  			v2.Type = t2
   118  		} else if v2.Type != nil && assignop(t2, v2.Type, &why) == 0 {
   119  			yyerror("cannot assign type %v to %L in range%s", t2, v2, why)
   120  		}
   121  		checkassign(n, v2)
   122  	}
   123  
   124  	// second half of dance
   125  out:
   126  	n.SetTypecheck(1)
   127  	ls = n.List.Slice()
   128  	for i1, n1 := range ls {
   129  		if n1.Typecheck() == 0 {
   130  			ls[i1] = typecheck(ls[i1], Erv|Easgn)
   131  		}
   132  	}
   133  
   134  	decldepth++
   135  	typecheckslice(n.Nbody.Slice(), Etop)
   136  	decldepth--
   137  }
   138  
   139  // walkrange transforms various forms of ORANGE into
   140  // simpler forms.  The result must be assigned back to n.
   141  // Node n may also be modified in place, and may also be
   142  // the returned node.
   143  func walkrange(n *Node) *Node {
   144  	// variable name conventions:
   145  	//	ohv1, hv1, hv2: hidden (old) val 1, 2
   146  	//	ha, hit: hidden aggregate, iterator
   147  	//	hn, hp: hidden len, pointer
   148  	//	hb: hidden bool
   149  	//	a, v1, v2: not hidden aggregate, val 1, 2
   150  
   151  	t := n.Type
   152  
   153  	a := n.Right
   154  	lno := setlineno(a)
   155  	n.Right = nil
   156  
   157  	var v1 *Node
   158  	if n.List.Len() != 0 {
   159  		v1 = n.List.First()
   160  	}
   161  	var v2 *Node
   162  	if n.List.Len() > 1 && !isblank(n.List.Second()) {
   163  		v2 = n.List.Second()
   164  	}
   165  
   166  	if v1 == nil && v2 != nil {
   167  		Fatalf("walkrange: v2 != nil while v1 == nil")
   168  	}
   169  
   170  	var ifGuard *Node
   171  
   172  	translatedLoopOp := OFOR
   173  
   174  	// n.List has no meaning anymore, clear it
   175  	// to avoid erroneous processing by racewalk.
   176  	n.List.Set(nil)
   177  
   178  	var body []*Node
   179  	var init []*Node
   180  	switch t.Etype {
   181  	default:
   182  		Fatalf("walkrange")
   183  
   184  	case TARRAY, TSLICE:
   185  		if memclrrange(n, v1, v2, a) {
   186  			lineno = lno
   187  			return n
   188  		}
   189  
   190  		// orderstmt arranged for a copy of the array/slice variable if needed.
   191  		ha := a
   192  
   193  		hv1 := temp(types.Types[TINT])
   194  		hn := temp(types.Types[TINT])
   195  		var hp *Node
   196  
   197  		init = append(init, nod(OAS, hv1, nil))
   198  		init = append(init, nod(OAS, hn, nod(OLEN, ha, nil)))
   199  
   200  		if v2 != nil {
   201  			hp = temp(types.NewPtr(n.Type.Elem()))
   202  			tmp := nod(OINDEX, ha, nodintconst(0))
   203  			tmp.SetBounded(true)
   204  			init = append(init, nod(OAS, hp, nod(OADDR, tmp, nil)))
   205  		}
   206  
   207  		n.Left = nod(OLT, hv1, hn)
   208  		n.Right = nod(OAS, hv1, nod(OADD, hv1, nodintconst(1)))
   209  		if v1 == nil {
   210  			body = nil
   211  		} else if v2 == nil {
   212  			body = []*Node{nod(OAS, v1, hv1)}
   213  		} else { // for i,a := range thing { body }
   214  			ifGuard = nod(OIF, nil, nil)
   215  			ifGuard.Left = nod(OLT, hv1, hn)
   216  			translatedLoopOp = OFORUNTIL
   217  
   218  			a := nod(OAS2, nil, nil)
   219  			a.List.Set2(v1, v2)
   220  			a.Rlist.Set2(hv1, nod(OIND, hp, nil))
   221  			body = []*Node{a}
   222  
   223  			// Advance pointer as part of increment.
   224  			// We used to advance the pointer before executing the loop body,
   225  			// but doing so would make the pointer point past the end of the
   226  			// array during the final iteration, possibly causing another unrelated
   227  			// piece of memory not to be garbage collected until the loop finished.
   228  			// Advancing during the increment ensures that the pointer p only points
   229  			// pass the end of the array during the final "p++; i++; if(i >= len(x)) break;",
   230  			// after which p is dead, so it cannot confuse the collector.
   231  			tmp := nod(OADD, hp, nodintconst(t.Elem().Width))
   232  
   233  			tmp.Type = hp.Type
   234  			tmp.SetTypecheck(1)
   235  			tmp.Right.Type = types.Types[types.Tptr]
   236  			tmp.Right.SetTypecheck(1)
   237  			a = nod(OAS, hp, tmp)
   238  			a = typecheck(a, Etop)
   239  			n.Right.Ninit.Set1(a)
   240  		}
   241  
   242  	case TMAP:
   243  		// orderstmt allocated the iterator for us.
   244  		// we only use a once, so no copy needed.
   245  		ha := a
   246  
   247  		th := hiter(t)
   248  		hit := prealloc[n]
   249  		hit.Type = th
   250  		n.Left = nil
   251  		keysym := th.Field(0).Sym // depends on layout of iterator struct.  See reflect.go:hiter
   252  		valsym := th.Field(1).Sym // ditto
   253  
   254  		fn := syslook("mapiterinit")
   255  
   256  		fn = substArgTypes(fn, t.Key(), t.Val(), th)
   257  		init = append(init, mkcall1(fn, nil, nil, typename(t), ha, nod(OADDR, hit, nil)))
   258  		n.Left = nod(ONE, nodSym(ODOT, hit, keysym), nodnil())
   259  
   260  		fn = syslook("mapiternext")
   261  		fn = substArgTypes(fn, th)
   262  		n.Right = mkcall1(fn, nil, nil, nod(OADDR, hit, nil))
   263  
   264  		key := nodSym(ODOT, hit, keysym)
   265  		key = nod(OIND, key, nil)
   266  		if v1 == nil {
   267  			body = nil
   268  		} else if v2 == nil {
   269  			body = []*Node{nod(OAS, v1, key)}
   270  		} else {
   271  			val := nodSym(ODOT, hit, valsym)
   272  			val = nod(OIND, val, nil)
   273  			a := nod(OAS2, nil, nil)
   274  			a.List.Set2(v1, v2)
   275  			a.Rlist.Set2(key, val)
   276  			body = []*Node{a}
   277  		}
   278  
   279  	case TCHAN:
   280  		// orderstmt arranged for a copy of the channel variable.
   281  		ha := a
   282  
   283  		n.Left = nil
   284  
   285  		hv1 := temp(t.Elem())
   286  		hv1.SetTypecheck(1)
   287  		if types.Haspointers(t.Elem()) {
   288  			init = append(init, nod(OAS, hv1, nil))
   289  		}
   290  		hb := temp(types.Types[TBOOL])
   291  
   292  		n.Left = nod(ONE, hb, nodbool(false))
   293  		a := nod(OAS2RECV, nil, nil)
   294  		a.SetTypecheck(1)
   295  		a.List.Set2(hv1, hb)
   296  		a.Rlist.Set1(nod(ORECV, ha, nil))
   297  		n.Left.Ninit.Set1(a)
   298  		if v1 == nil {
   299  			body = nil
   300  		} else {
   301  			body = []*Node{nod(OAS, v1, hv1)}
   302  		}
   303  		// Zero hv1. This prevents hv1 from being the sole, inaccessible
   304  		// reference to an otherwise GC-able value during the next channel receive.
   305  		// See issue 15281.
   306  		body = append(body, nod(OAS, hv1, nil))
   307  
   308  	case TSTRING:
   309  		// Transform string range statements like "for v1, v2 = range a" into
   310  		//
   311  		// ha := a
   312  		// for hv1 := 0; hv1 < len(ha); {
   313  		//   hv1t := hv1
   314  		//   hv2 := rune(ha[hv1])
   315  		//   if hv2 < utf8.RuneSelf {
   316  		//      hv1++
   317  		//   } else {
   318  		//      hv2, hv1 = decoderune(ha, hv1)
   319  		//   }
   320  		//   v1, v2 = hv1t, hv2
   321  		//   // original body
   322  		// }
   323  
   324  		// orderstmt arranged for a copy of the string variable.
   325  		ha := a
   326  
   327  		hv1 := temp(types.Types[TINT])
   328  		hv1t := temp(types.Types[TINT])
   329  		hv2 := temp(types.Runetype)
   330  
   331  		// hv1 := 0
   332  		init = append(init, nod(OAS, hv1, nil))
   333  
   334  		// hv1 < len(ha)
   335  		n.Left = nod(OLT, hv1, nod(OLEN, ha, nil))
   336  
   337  		if v1 != nil {
   338  			// hv1t = hv1
   339  			body = append(body, nod(OAS, hv1t, hv1))
   340  		}
   341  
   342  		// hv2 := rune(ha[hv1])
   343  		nind := nod(OINDEX, ha, hv1)
   344  		nind.SetBounded(true)
   345  		body = append(body, nod(OAS, hv2, conv(nind, types.Runetype)))
   346  
   347  		// if hv2 < utf8.RuneSelf
   348  		nif := nod(OIF, nil, nil)
   349  		nif.Left = nod(OLT, hv2, nodintconst(utf8.RuneSelf))
   350  
   351  		// hv1++
   352  		nif.Nbody.Set1(nod(OAS, hv1, nod(OADD, hv1, nodintconst(1))))
   353  
   354  		// } else {
   355  		eif := nod(OAS2, nil, nil)
   356  		nif.Rlist.Set1(eif)
   357  
   358  		// hv2, hv1 = decoderune(ha, hv1)
   359  		eif.List.Set2(hv2, hv1)
   360  		fn := syslook("decoderune")
   361  		eif.Rlist.Set1(mkcall1(fn, fn.Type.Results(), nil, ha, hv1))
   362  
   363  		body = append(body, nif)
   364  
   365  		if v1 != nil {
   366  			if v2 != nil {
   367  				// v1, v2 = hv1t, hv2
   368  				a := nod(OAS2, nil, nil)
   369  				a.List.Set2(v1, v2)
   370  				a.Rlist.Set2(hv1t, hv2)
   371  				body = append(body, a)
   372  			} else {
   373  				// v1 = hv1t
   374  				body = append(body, nod(OAS, v1, hv1t))
   375  			}
   376  		}
   377  	}
   378  
   379  	n.Op = translatedLoopOp
   380  	typecheckslice(init, Etop)
   381  
   382  	if ifGuard != nil {
   383  		ifGuard.Ninit.Append(init...)
   384  		typecheckslice(ifGuard.Left.Ninit.Slice(), Etop)
   385  		ifGuard.Left = typecheck(ifGuard.Left, Erv)
   386  	} else {
   387  		n.Ninit.Append(init...)
   388  	}
   389  
   390  	typecheckslice(n.Left.Ninit.Slice(), Etop)
   391  
   392  	n.Left = typecheck(n.Left, Erv)
   393  	n.Right = typecheck(n.Right, Etop)
   394  	typecheckslice(body, Etop)
   395  	n.Nbody.Prepend(body...)
   396  
   397  	if ifGuard != nil {
   398  		ifGuard.Nbody.Set1(n)
   399  		n = ifGuard
   400  	}
   401  
   402  	n = walkstmt(n)
   403  
   404  	lineno = lno
   405  	return n
   406  }
   407  
   408  // Lower n into runtime·memclr if possible, for
   409  // fast zeroing of slices and arrays (issue 5373).
   410  // Look for instances of
   411  //
   412  // for i := range a {
   413  // 	a[i] = zero
   414  // }
   415  //
   416  // in which the evaluation of a is side-effect-free.
   417  //
   418  // Parameters are as in walkrange: "for v1, v2 = range a".
   419  func memclrrange(n, v1, v2, a *Node) bool {
   420  	if Debug['N'] != 0 || instrumenting {
   421  		return false
   422  	}
   423  	if v1 == nil || v2 != nil {
   424  		return false
   425  	}
   426  	if n.Nbody.Len() == 0 || n.Nbody.First() == nil || n.Nbody.Len() > 1 {
   427  		return false
   428  	}
   429  	stmt := n.Nbody.First() // only stmt in body
   430  	if stmt.Op != OAS || stmt.Left.Op != OINDEX {
   431  		return false
   432  	}
   433  	if !samesafeexpr(stmt.Left.Left, a) || !samesafeexpr(stmt.Left.Right, v1) {
   434  		return false
   435  	}
   436  	elemsize := n.Type.Elem().Width
   437  	if elemsize <= 0 || !iszero(stmt.Right) {
   438  		return false
   439  	}
   440  
   441  	// Convert to
   442  	// if len(a) != 0 {
   443  	// 	hp = &a[0]
   444  	// 	hn = len(a)*sizeof(elem(a))
   445  	// 	memclr{NoHeap,Has}Pointers(hp, hn)
   446  	// 	i = len(a) - 1
   447  	// }
   448  	n.Op = OIF
   449  
   450  	n.Nbody.Set(nil)
   451  	n.Left = nod(ONE, nod(OLEN, a, nil), nodintconst(0))
   452  
   453  	// hp = &a[0]
   454  	hp := temp(types.Types[TUNSAFEPTR])
   455  
   456  	tmp := nod(OINDEX, a, nodintconst(0))
   457  	tmp.SetBounded(true)
   458  	tmp = nod(OADDR, tmp, nil)
   459  	tmp = nod(OCONVNOP, tmp, nil)
   460  	tmp.Type = types.Types[TUNSAFEPTR]
   461  	n.Nbody.Append(nod(OAS, hp, tmp))
   462  
   463  	// hn = len(a) * sizeof(elem(a))
   464  	hn := temp(types.Types[TUINTPTR])
   465  
   466  	tmp = nod(OLEN, a, nil)
   467  	tmp = nod(OMUL, tmp, nodintconst(elemsize))
   468  	tmp = conv(tmp, types.Types[TUINTPTR])
   469  	n.Nbody.Append(nod(OAS, hn, tmp))
   470  
   471  	var fn *Node
   472  	if types.Haspointers(a.Type.Elem()) {
   473  		// memclrHasPointers(hp, hn)
   474  		fn = mkcall("memclrHasPointers", nil, nil, hp, hn)
   475  	} else {
   476  		// memclrNoHeapPointers(hp, hn)
   477  		fn = mkcall("memclrNoHeapPointers", nil, nil, hp, hn)
   478  	}
   479  
   480  	n.Nbody.Append(fn)
   481  
   482  	// i = len(a) - 1
   483  	v1 = nod(OAS, v1, nod(OSUB, nod(OLEN, a, nil), nodintconst(1)))
   484  
   485  	n.Nbody.Append(v1)
   486  
   487  	n.Left = typecheck(n.Left, Erv)
   488  	typecheckslice(n.Nbody.Slice(), Etop)
   489  	n = walkstmt(n)
   490  	return true
   491  }