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