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.
13 "cmd/compile/internal/base"
14 "cmd/compile/internal/ir"
15 "cmd/compile/internal/reflectdata"
16 "cmd/compile/internal/staticdata"
17 "cmd/compile/internal/typecheck"
18 "cmd/compile/internal/types"
23 // The result of walkExpr MUST be assigned back to n, e.g.
25 // n.Left = walkExpr(n.Left, init)
26 func walkExpr(n ir.Node, init *ir.Nodes) ir.Node {
31 if n, ok := n.(ir.InitNode); ok && init == n.PtrInit() {
32 // not okay to use n->ninit when walking n,
33 // because we might replace n with some other node
34 // and would lose the init list.
35 base.Fatalf("walkExpr init == &n->ninit")
38 if len(n.Init()) != 0 {
39 walkStmtList(n.Init())
40 init.Append(ir.TakeInit(n)...)
45 if base.Flag.LowerW > 1 {
46 ir.Dump("before walk expr", n)
49 if n.Typecheck() != 1 {
50 base.Fatalf("missed typecheck: %+v", n)
53 if n.Type().IsUntyped() {
54 base.Fatalf("expression has untyped type: %+v", n)
57 n = walkExpr1(n, init)
59 // Eagerly compute sizes of all expressions for the back end.
60 if typ := n.Type(); typ != nil && typ.Kind() != types.TBLANK && !typ.IsFuncArgStruct() {
63 if n, ok := n.(*ir.Name); ok && n.Heapaddr != nil {
64 types.CheckSize(n.Heapaddr.Type())
66 if ir.IsConst(n, constant.String) {
67 // Emit string symbol now to avoid emitting
68 // any concurrently during the backend.
69 _ = staticdata.StringSym(n.Pos(), constant.StringVal(n.Val()))
72 if base.Flag.LowerW != 0 && n != nil {
73 ir.Dump("after walk expr", n)
80 func walkExpr1(n ir.Node, init *ir.Nodes) ir.Node {
84 base.Fatalf("walkExpr: switch 1 unknown op %+v", n.Op())
87 case ir.OGETG, ir.OGETCALLERPC, ir.OGETCALLERSP:
90 case ir.OTYPE, ir.ONAME, ir.OLITERAL, ir.ONIL, ir.OLINKSYMOFFSET:
91 // TODO(mdempsky): Just return n; see discussion on CL 38655.
92 // Perhaps refactor to use Node.mayBeShared for these instead.
93 // If these return early, make sure to still call
94 // StringSym for constant strings.
98 // TODO(mdempsky): Do this right after type checking.
99 n := n.(*ir.SelectorExpr)
102 case ir.OMIN, ir.OMAX:
103 n := n.(*ir.CallExpr)
104 return walkMinMax(n, init)
106 case ir.ONOT, ir.ONEG, ir.OPLUS, ir.OBITNOT, ir.OREAL, ir.OIMAG, ir.OSPTR, ir.OITAB, ir.OIDATA:
107 n := n.(*ir.UnaryExpr)
108 n.X = walkExpr(n.X, init)
111 case ir.ODOTMETH, ir.ODOTINTER:
112 n := n.(*ir.SelectorExpr)
113 n.X = walkExpr(n.X, init)
117 n := n.(*ir.AddrExpr)
118 n.X = walkExpr(n.X, init)
122 n := n.(*ir.StarExpr)
123 n.X = walkExpr(n.X, init)
126 case ir.OMAKEFACE, ir.OAND, ir.OANDNOT, ir.OSUB, ir.OMUL, ir.OADD, ir.OOR, ir.OXOR, ir.OLSH, ir.ORSH,
128 n := n.(*ir.BinaryExpr)
129 n.X = walkExpr(n.X, init)
130 n.Y = walkExpr(n.Y, init)
133 case ir.OUNSAFESLICE:
134 n := n.(*ir.BinaryExpr)
135 return walkUnsafeSlice(n, init)
137 case ir.OUNSAFESTRING:
138 n := n.(*ir.BinaryExpr)
139 return walkUnsafeString(n, init)
141 case ir.OUNSAFESTRINGDATA, ir.OUNSAFESLICEDATA:
142 n := n.(*ir.UnaryExpr)
143 return walkUnsafeData(n, init)
145 case ir.ODOT, ir.ODOTPTR:
146 n := n.(*ir.SelectorExpr)
147 return walkDot(n, init)
149 case ir.ODOTTYPE, ir.ODOTTYPE2:
150 n := n.(*ir.TypeAssertExpr)
151 return walkDotType(n, init)
153 case ir.ODYNAMICDOTTYPE, ir.ODYNAMICDOTTYPE2:
154 n := n.(*ir.DynamicTypeAssertExpr)
155 return walkDynamicDotType(n, init)
157 case ir.OLEN, ir.OCAP:
158 n := n.(*ir.UnaryExpr)
159 return walkLenCap(n, init)
162 n := n.(*ir.BinaryExpr)
163 n.X = walkExpr(n.X, init)
164 n.Y = walkExpr(n.Y, init)
167 case ir.OEQ, ir.ONE, ir.OLT, ir.OLE, ir.OGT, ir.OGE:
168 n := n.(*ir.BinaryExpr)
169 return walkCompare(n, init)
171 case ir.OANDAND, ir.OOROR:
172 n := n.(*ir.LogicalExpr)
173 return walkLogical(n, init)
175 case ir.OPRINT, ir.OPRINTN:
176 return walkPrint(n.(*ir.CallExpr), init)
179 n := n.(*ir.UnaryExpr)
180 return mkcall("gopanic", nil, init, n.X)
183 return walkRecoverFP(n.(*ir.CallExpr), init)
188 case ir.OCALLINTER, ir.OCALLFUNC:
189 n := n.(*ir.CallExpr)
190 return walkCall(n, init)
192 case ir.OAS, ir.OASOP:
193 return walkAssign(init, n)
196 n := n.(*ir.AssignListStmt)
197 return walkAssignList(init, n)
201 n := n.(*ir.AssignListStmt)
202 return walkAssignFunc(init, n)
205 // order.stmt made sure x is addressable or blank.
207 n := n.(*ir.AssignListStmt)
208 return walkAssignRecv(init, n)
212 n := n.(*ir.AssignListStmt)
213 return walkAssignMapRead(init, n)
216 n := n.(*ir.CallExpr)
217 return walkDelete(init, n)
220 n := n.(*ir.AssignListStmt)
221 return walkAssignDotType(n, init)
224 n := n.(*ir.ConvExpr)
225 return walkConvInterface(n, init)
227 case ir.OCONV, ir.OCONVNOP:
228 n := n.(*ir.ConvExpr)
229 return walkConv(n, init)
232 n := n.(*ir.ConvExpr)
233 return walkSliceToArray(n, init)
235 case ir.OSLICE2ARRPTR:
236 n := n.(*ir.ConvExpr)
237 n.X = walkExpr(n.X, init)
240 case ir.ODIV, ir.OMOD:
241 n := n.(*ir.BinaryExpr)
242 return walkDivMod(n, init)
245 n := n.(*ir.IndexExpr)
246 return walkIndex(n, init)
249 n := n.(*ir.IndexExpr)
250 return walkIndexMap(n, init)
253 base.Fatalf("walkExpr ORECV") // should see inside OAS only
256 case ir.OSLICEHEADER:
257 n := n.(*ir.SliceHeaderExpr)
258 return walkSliceHeader(n, init)
260 case ir.OSTRINGHEADER:
261 n := n.(*ir.StringHeaderExpr)
262 return walkStringHeader(n, init)
264 case ir.OSLICE, ir.OSLICEARR, ir.OSLICESTR, ir.OSLICE3, ir.OSLICE3ARR:
265 n := n.(*ir.SliceExpr)
266 return walkSlice(n, init)
269 n := n.(*ir.UnaryExpr)
270 return walkNew(n, init)
273 return walkAddString(n.(*ir.AddStringExpr), init)
276 // order should make sure we only see OAS(node, OAPPEND), which we handle above.
277 base.Fatalf("append outside assignment")
281 return walkCopy(n.(*ir.BinaryExpr), init, base.Flag.Cfg.Instrumenting && !base.Flag.CompilingRuntime)
284 n := n.(*ir.UnaryExpr)
288 n := n.(*ir.UnaryExpr)
289 return walkClose(n, init)
292 n := n.(*ir.MakeExpr)
293 return walkMakeChan(n, init)
296 n := n.(*ir.MakeExpr)
297 return walkMakeMap(n, init)
300 n := n.(*ir.MakeExpr)
301 return walkMakeSlice(n, init)
303 case ir.OMAKESLICECOPY:
304 n := n.(*ir.MakeExpr)
305 return walkMakeSliceCopy(n, init)
308 n := n.(*ir.ConvExpr)
309 return walkRuneToString(n, init)
311 case ir.OBYTES2STR, ir.ORUNES2STR:
312 n := n.(*ir.ConvExpr)
313 return walkBytesRunesToString(n, init)
315 case ir.OBYTES2STRTMP:
316 n := n.(*ir.ConvExpr)
317 return walkBytesToStringTemp(n, init)
320 n := n.(*ir.ConvExpr)
321 return walkStringToBytes(n, init)
323 case ir.OSTR2BYTESTMP:
324 n := n.(*ir.ConvExpr)
325 return walkStringToBytesTemp(n, init)
328 n := n.(*ir.ConvExpr)
329 return walkStringToRunes(n, init)
331 case ir.OARRAYLIT, ir.OSLICELIT, ir.OMAPLIT, ir.OSTRUCTLIT, ir.OPTRLIT:
332 return walkCompLit(n, init)
335 n := n.(*ir.SendStmt)
336 return walkSend(n, init)
339 return walkClosure(n.(*ir.ClosureExpr), init)
342 return walkMethodValue(n.(*ir.SelectorExpr), init)
345 // No return! Each case must return (or panic),
346 // to avoid confusion about what gets returned
347 // in the presence of type assertions.
350 // walk the whole tree of the body of an
351 // expression or simple statement.
352 // the types expressions are calculated.
353 // compile-time constants are evaluated.
354 // complex side effects like statements are appended to init.
355 func walkExprList(s []ir.Node, init *ir.Nodes) {
357 s[i] = walkExpr(s[i], init)
361 func walkExprListCheap(s []ir.Node, init *ir.Nodes) {
362 for i, n := range s {
363 s[i] = cheapExpr(n, init)
364 s[i] = walkExpr(s[i], init)
368 func walkExprListSafe(s []ir.Node, init *ir.Nodes) {
369 for i, n := range s {
370 s[i] = safeExpr(n, init)
371 s[i] = walkExpr(s[i], init)
375 // return side-effect free and cheap n, appending side effects to init.
376 // result may not be assignable.
377 func cheapExpr(n ir.Node, init *ir.Nodes) ir.Node {
379 case ir.ONAME, ir.OLITERAL, ir.ONIL:
383 return copyExpr(n, n.Type(), init)
386 // return side effect-free n, appending side effects to init.
387 // result is assignable if n is.
388 func safeExpr(n ir.Node, init *ir.Nodes) ir.Node {
393 if len(n.Init()) != 0 {
394 walkStmtList(n.Init())
395 init.Append(ir.TakeInit(n)...)
399 case ir.ONAME, ir.OLITERAL, ir.ONIL, ir.OLINKSYMOFFSET:
402 case ir.OLEN, ir.OCAP:
403 n := n.(*ir.UnaryExpr)
404 l := safeExpr(n.X, init)
408 a := ir.Copy(n).(*ir.UnaryExpr)
410 return walkExpr(typecheck.Expr(a), init)
412 case ir.ODOT, ir.ODOTPTR:
413 n := n.(*ir.SelectorExpr)
414 l := safeExpr(n.X, init)
418 a := ir.Copy(n).(*ir.SelectorExpr)
420 return walkExpr(typecheck.Expr(a), init)
423 n := n.(*ir.StarExpr)
424 l := safeExpr(n.X, init)
428 a := ir.Copy(n).(*ir.StarExpr)
430 return walkExpr(typecheck.Expr(a), init)
432 case ir.OINDEX, ir.OINDEXMAP:
433 n := n.(*ir.IndexExpr)
434 l := safeExpr(n.X, init)
435 r := safeExpr(n.Index, init)
436 if l == n.X && r == n.Index {
439 a := ir.Copy(n).(*ir.IndexExpr)
442 return walkExpr(typecheck.Expr(a), init)
444 case ir.OSTRUCTLIT, ir.OARRAYLIT, ir.OSLICELIT:
445 n := n.(*ir.CompLitExpr)
446 if isStaticCompositeLiteral(n) {
451 // make a copy; must not be used as an lvalue
452 if ir.IsAddressable(n) {
453 base.Fatalf("missing lvalue case in safeExpr: %v", n)
455 return cheapExpr(n, init)
458 func copyExpr(n ir.Node, t *types.Type, init *ir.Nodes) ir.Node {
459 l := typecheck.TempAt(base.Pos, ir.CurFunc, t)
460 appendWalkStmt(init, ir.NewAssignStmt(base.Pos, l, n))
464 func walkAddString(n *ir.AddStringExpr, init *ir.Nodes) ir.Node {
468 base.Fatalf("walkAddString count %d too small", c)
471 buf := typecheck.NodNil()
472 if n.Esc() == ir.EscNone {
474 for _, n1 := range n.List {
475 if n1.Op() == ir.OLITERAL {
476 sz += int64(len(ir.StringVal(n1)))
480 // Don't allocate the buffer if the result won't fit.
481 if sz < tmpstringbufsize {
482 // Create temporary buffer for result string on stack.
483 buf = stackBufAddr(tmpstringbufsize, types.Types[types.TUINT8])
487 // build list of string arguments
488 args := []ir.Node{buf}
489 for _, n2 := range n.List {
490 args = append(args, typecheck.Conv(n2, types.Types[types.TSTRING]))
495 // small numbers of strings use direct runtime helpers.
496 // note: order.expr knows this cutoff too.
497 fn = fmt.Sprintf("concatstring%d", c)
499 // large numbers of strings are passed to the runtime as a slice.
502 t := types.NewSlice(types.Types[types.TSTRING])
503 // args[1:] to skip buf arg
504 slice := ir.NewCompLitExpr(base.Pos, ir.OCOMPLIT, t, args[1:])
505 slice.Prealloc = n.Prealloc
506 args = []ir.Node{buf, slice}
507 slice.SetEsc(ir.EscNone)
510 cat := typecheck.LookupRuntime(fn)
511 r := ir.NewCallExpr(base.Pos, ir.OCALL, cat, nil)
513 r1 := typecheck.Expr(r)
514 r1 = walkExpr(r1, init)
520 type hookInfo struct {
526 var hooks = map[string]hookInfo{
527 "strings.EqualFold": {paramType: types.TSTRING, argsNum: 2, runtimeFunc: "libfuzzerHookEqualFold"},
530 // walkCall walks an OCALLFUNC or OCALLINTER node.
531 func walkCall(n *ir.CallExpr, init *ir.Nodes) ir.Node {
532 if n.Op() == ir.OCALLMETH {
533 base.FatalfAt(n.Pos(), "OCALLMETH missed by typecheck")
535 if n.Op() == ir.OCALLINTER || n.X.Op() == ir.OMETHEXPR {
536 // We expect both interface call reflect.Type.Method and concrete
537 // call reflect.(*rtype).Method.
540 if n.Op() == ir.OCALLINTER {
541 reflectdata.MarkUsedIfaceMethod(n)
544 if n.Op() == ir.OCALLFUNC && n.X.Op() == ir.OCLOSURE {
548 if ir.IsFuncPCIntrinsic(n) {
549 // For internal/abi.FuncPCABIxxx(fn), if fn is a defined function, rewrite
550 // it to the address of the function of the ABI fn is defined.
551 name := n.X.(*ir.Name).Sym().Name
557 case "FuncPCABIInternal":
558 wantABI = obj.ABIInternal
560 if isIfaceOfFunc(arg) {
561 fn := arg.(*ir.ConvExpr).X.(*ir.Name)
564 base.ErrorfAt(n.Pos(), 0, "internal/abi.%s expects an %v function, %s is defined as %v", name, wantABI, fn.Sym().Name, abi)
566 var e ir.Node = ir.NewLinksymExpr(n.Pos(), fn.Sym().LinksymABI(abi), types.Types[types.TUINTPTR])
567 e = ir.NewAddrExpr(n.Pos(), e)
568 e.SetType(types.Types[types.TUINTPTR].PtrTo())
569 return typecheck.Expr(ir.NewConvExpr(n.Pos(), ir.OCONVNOP, n.Type(), e))
571 // fn is not a defined function. It must be ABIInternal.
572 // Read the address from func value, i.e. *(*uintptr)(idata(fn)).
573 if wantABI != obj.ABIInternal {
574 base.ErrorfAt(n.Pos(), 0, "internal/abi.%s does not accept func expression, which is ABIInternal", name)
576 arg = walkExpr(arg, init)
577 var e ir.Node = ir.NewUnaryExpr(n.Pos(), ir.OIDATA, arg)
578 e.SetType(n.Type().PtrTo())
580 e = ir.NewStarExpr(n.Pos(), e)
590 func walkCall1(n *ir.CallExpr, init *ir.Nodes) {
592 return // already walked
596 if n.Op() == ir.OCALLMETH {
597 base.FatalfAt(n.Pos(), "OCALLMETH missed by typecheck")
601 params := n.X.Type().Params()
603 n.X = walkExpr(n.X, init)
604 walkExprList(args, init)
606 for i, arg := range args {
607 // Validate argument and parameter types match.
609 if !types.Identical(arg.Type(), param.Type) {
610 base.FatalfAt(n.Pos(), "assigning %L to parameter %v (type %v)", arg, param.Sym, param.Type)
613 // For any argument whose evaluation might require a function call,
614 // store that argument into a temporary variable,
615 // to prevent that calls from clobbering arguments already on the stack.
617 // assignment of arg to Temp
618 tmp := typecheck.TempAt(base.Pos, ir.CurFunc, param.Type)
619 init.Append(convas(typecheck.Stmt(ir.NewAssignStmt(base.Pos, tmp, arg)).(*ir.AssignStmt), init))
620 // replace arg with temp
626 if base.Debug.Libfuzzer != 0 && funSym != nil {
627 if hook, found := hooks[funSym.Pkg.Path+"."+funSym.Name]; found {
628 if len(args) != hook.argsNum {
629 panic(fmt.Sprintf("%s.%s expects %d arguments, but received %d", funSym.Pkg.Path, funSym.Name, hook.argsNum, len(args)))
631 var hookArgs []ir.Node
632 for _, arg := range args {
633 hookArgs = append(hookArgs, tracecmpArg(arg, types.Types[hook.paramType], init))
635 hookArgs = append(hookArgs, fakePC(n))
636 init.Append(mkcall(hook.runtimeFunc, nil, init, hookArgs...))
641 // walkDivMod walks an ODIV or OMOD node.
642 func walkDivMod(n *ir.BinaryExpr, init *ir.Nodes) ir.Node {
643 n.X = walkExpr(n.X, init)
644 n.Y = walkExpr(n.Y, init)
646 // rewrite complex div into function call.
647 et := n.X.Type().Kind()
649 if types.IsComplex[et] && n.Op() == ir.ODIV {
651 call := mkcall("complex128div", types.Types[types.TCOMPLEX128], init, typecheck.Conv(n.X, types.Types[types.TCOMPLEX128]), typecheck.Conv(n.Y, types.Types[types.TCOMPLEX128]))
652 return typecheck.Conv(call, t)
655 // Nothing to do for float divisions.
656 if types.IsFloat[et] {
660 // rewrite 64-bit div and mod on 32-bit architectures.
661 // TODO: Remove this code once we can introduce
662 // runtime calls late in SSA processing.
663 if types.RegSize < 8 && (et == types.TINT64 || et == types.TUINT64) {
664 if n.Y.Op() == ir.OLITERAL {
665 // Leave div/mod by constant powers of 2 or small 16-bit constants.
666 // The SSA backend will handle those.
669 c := ir.Int64Val(n.Y)
673 if c != 0 && c&(c-1) == 0 {
677 c := ir.Uint64Val(n.Y)
681 if c != 0 && c&(c-1) == 0 {
687 if et == types.TINT64 {
692 if n.Op() == ir.ODIV {
697 return mkcall(fn, n.Type(), init, typecheck.Conv(n.X, types.Types[et]), typecheck.Conv(n.Y, types.Types[et]))
702 // walkDot walks an ODOT or ODOTPTR node.
703 func walkDot(n *ir.SelectorExpr, init *ir.Nodes) ir.Node {
705 n.X = walkExpr(n.X, init)
709 // walkDotType walks an ODOTTYPE or ODOTTYPE2 node.
710 func walkDotType(n *ir.TypeAssertExpr, init *ir.Nodes) ir.Node {
711 n.X = walkExpr(n.X, init)
712 // Set up interface type addresses for back end.
713 if !n.Type().IsInterface() && !n.X.Type().IsEmptyInterface() {
714 n.ITab = reflectdata.ITabAddrAt(base.Pos, n.Type(), n.X.Type())
719 // walkDynamicDotType walks an ODYNAMICDOTTYPE or ODYNAMICDOTTYPE2 node.
720 func walkDynamicDotType(n *ir.DynamicTypeAssertExpr, init *ir.Nodes) ir.Node {
721 n.X = walkExpr(n.X, init)
722 n.RType = walkExpr(n.RType, init)
723 n.ITab = walkExpr(n.ITab, init)
727 // walkIndex walks an OINDEX node.
728 func walkIndex(n *ir.IndexExpr, init *ir.Nodes) ir.Node {
729 n.X = walkExpr(n.X, init)
731 // save the original node for bounds checking elision.
732 // If it was a ODIV/OMOD walk might rewrite it.
735 n.Index = walkExpr(n.Index, init)
737 // if range of type cannot exceed static array bound,
738 // disable bounds check.
743 if t != nil && t.IsPtr() {
747 n.SetBounded(bounded(r, t.NumElem()))
748 if base.Flag.LowerM != 0 && n.Bounded() && !ir.IsConst(n.Index, constant.Int) {
749 base.Warn("index bounds check elided")
751 } else if ir.IsConst(n.X, constant.String) {
752 n.SetBounded(bounded(r, int64(len(ir.StringVal(n.X)))))
753 if base.Flag.LowerM != 0 && n.Bounded() && !ir.IsConst(n.Index, constant.Int) {
754 base.Warn("index bounds check elided")
760 // mapKeyArg returns an expression for key that is suitable to be passed
761 // as the key argument for runtime map* functions.
762 // n is the map indexing or delete Node (to provide Pos).
763 func mapKeyArg(fast int, n, key ir.Node, assigned bool) ir.Node {
765 // standard version takes key by reference.
766 // orderState.expr made sure key is addressable.
767 return typecheck.NodAddr(key)
770 // mapassign does distinguish pointer vs. integer key.
773 // mapaccess and mapdelete don't distinguish pointer vs. integer key.
776 return ir.NewConvExpr(n.Pos(), ir.OCONVNOP, types.Types[types.TUINT32], key)
778 return ir.NewConvExpr(n.Pos(), ir.OCONVNOP, types.Types[types.TUINT64], key)
780 // fast version takes key by value.
785 // walkIndexMap walks an OINDEXMAP node.
786 // It replaces m[k] with *map{access1,assign}(maptype, m, &k)
787 func walkIndexMap(n *ir.IndexExpr, init *ir.Nodes) ir.Node {
788 n.X = walkExpr(n.X, init)
789 n.Index = walkExpr(n.Index, init)
793 key := mapKeyArg(fast, n, n.Index, n.Assigned)
794 args := []ir.Node{reflectdata.IndexMapRType(base.Pos, n), map_, key}
799 mapFn = mapfn(mapassign[fast], t, false)
800 case t.Elem().Size() > zeroValSize:
801 args = append(args, reflectdata.ZeroAddr(t.Elem().Size()))
802 mapFn = mapfn("mapaccess1_fat", t, true)
804 mapFn = mapfn(mapaccess1[fast], t, false)
806 call := mkcall1(mapFn, nil, init, args...)
807 call.SetType(types.NewPtr(t.Elem()))
808 call.MarkNonNil() // mapaccess1* and mapassign always return non-nil pointers.
809 star := ir.NewStarExpr(base.Pos, call)
810 star.SetType(t.Elem())
815 // walkLogical walks an OANDAND or OOROR node.
816 func walkLogical(n *ir.LogicalExpr, init *ir.Nodes) ir.Node {
817 n.X = walkExpr(n.X, init)
819 // cannot put side effects from n.Right on init,
820 // because they cannot run before n.Left is checked.
821 // save elsewhere and store on the eventual n.Right.
824 n.Y = walkExpr(n.Y, &ll)
825 n.Y = ir.InitExpr(ll, n.Y)
829 // walkSend walks an OSEND node.
830 func walkSend(n *ir.SendStmt, init *ir.Nodes) ir.Node {
832 n1 = typecheck.AssignConv(n1, n.Chan.Type().Elem(), "chan send")
833 n1 = walkExpr(n1, init)
834 n1 = typecheck.NodAddr(n1)
835 return mkcall1(chanfn("chansend1", 2, n.Chan.Type()), nil, init, n.Chan, n1)
838 // walkSlice walks an OSLICE, OSLICEARR, OSLICESTR, OSLICE3, or OSLICE3ARR node.
839 func walkSlice(n *ir.SliceExpr, init *ir.Nodes) ir.Node {
840 n.X = walkExpr(n.X, init)
841 n.Low = walkExpr(n.Low, init)
842 if n.Low != nil && ir.IsZero(n.Low) {
843 // Reduce x[0:j] to x[:j] and x[0:j:k] to x[:j:k].
846 n.High = walkExpr(n.High, init)
847 n.Max = walkExpr(n.Max, init)
849 if (n.Op() == ir.OSLICE || n.Op() == ir.OSLICESTR) && n.Low == nil && n.High == nil {
851 if base.Debug.Slice > 0 {
852 base.Warn("slice: omit slice operation")
859 // walkSliceHeader walks an OSLICEHEADER node.
860 func walkSliceHeader(n *ir.SliceHeaderExpr, init *ir.Nodes) ir.Node {
861 n.Ptr = walkExpr(n.Ptr, init)
862 n.Len = walkExpr(n.Len, init)
863 n.Cap = walkExpr(n.Cap, init)
867 // walkStringHeader walks an OSTRINGHEADER node.
868 func walkStringHeader(n *ir.StringHeaderExpr, init *ir.Nodes) ir.Node {
869 n.Ptr = walkExpr(n.Ptr, init)
870 n.Len = walkExpr(n.Len, init)
874 // return 1 if integer n must be in range [0, max), 0 otherwise.
875 func bounded(n ir.Node, max int64) bool {
876 if n.Type() == nil || !n.Type().IsInteger() {
880 sign := n.Type().IsSigned()
881 bits := int32(8 * n.Type().Size())
883 if ir.IsSmallIntConst(n) {
885 return 0 <= v && v < max
889 case ir.OAND, ir.OANDNOT:
890 n := n.(*ir.BinaryExpr)
893 case ir.IsSmallIntConst(n.X):
895 case ir.IsSmallIntConst(n.Y):
897 if n.Op() == ir.OANDNOT {
900 v &= 1<<uint(bits) - 1
904 if 0 <= v && v < max {
909 n := n.(*ir.BinaryExpr)
910 if !sign && ir.IsSmallIntConst(n.Y) {
911 v := ir.Int64Val(n.Y)
912 if 0 <= v && v <= max {
918 n := n.(*ir.BinaryExpr)
919 if !sign && ir.IsSmallIntConst(n.Y) {
920 v := ir.Int64Val(n.Y)
921 for bits > 0 && v >= 2 {
928 n := n.(*ir.BinaryExpr)
929 if !sign && ir.IsSmallIntConst(n.Y) {
930 v := ir.Int64Val(n.Y)
938 if !sign && bits <= 62 && 1<<uint(bits) <= max {
945 // usemethod checks calls for uses of Method and MethodByName of reflect.Value,
946 // reflect.Type, reflect.(*rtype), and reflect.(*interfaceType).
947 func usemethod(n *ir.CallExpr) {
948 // Don't mark reflect.(*rtype).Method, etc. themselves in the reflect package.
949 // Those functions may be alive via the itab, which should not cause all methods
950 // alive. We only want to mark their callers.
951 if base.Ctxt.Pkgpath == "reflect" {
952 // TODO: is there a better way than hardcoding the names?
953 switch fn := ir.CurFunc.Nname.Sym().Name; {
954 case fn == "(*rtype).Method", fn == "(*rtype).MethodByName":
956 case fn == "(*interfaceType).Method", fn == "(*interfaceType).MethodByName":
958 case fn == "Value.Method", fn == "Value.MethodByName":
960 // StructOf defines closures that look up methods. They only look up methods
961 // reachable via interfaces. The DCE does not remove such methods. It is ok
962 // to not flag closures in StructOf as ReflectMethods and let the DCE run
963 // even if StructOf is reachable.
965 // (*rtype).MethodByName calls into StructOf so flagging StructOf as
966 // ReflectMethod would disable the DCE even when the name of a method
967 // to look up is a compile-time constant.
968 case strings.HasPrefix(fn, "StructOf.func"):
973 dot, ok := n.X.(*ir.SelectorExpr)
978 // looking for either direct method calls and interface method calls of:
979 // reflect.Type.Method - func(int) reflect.Method
980 // reflect.Type.MethodByName - func(string) (reflect.Method, bool)
982 // reflect.Value.Method - func(int) reflect.Value
983 // reflect.Value.MethodByName - func(string) reflect.Value
984 methodName := dot.Sel.Name
985 t := dot.Selection.Type
987 // Check the number of arguments and return values.
988 if t.NumParams() != 1 || (t.NumResults() != 1 && t.NumResults() != 2) {
992 // Check the type of the argument.
993 switch pKind := t.Param(0).Type.Kind(); {
994 case methodName == "Method" && pKind == types.TINT,
995 methodName == "MethodByName" && pKind == types.TSTRING:
998 // not a call to Method or MethodByName of reflect.{Type,Value}.
1002 // Check that first result type is "reflect.Method" or "reflect.Value".
1003 // Note that we have to check sym name and sym package separately, as
1004 // we can't check for exact string "reflect.Method" reliably
1005 // (e.g., see #19028 and #38515).
1006 switch s := t.Result(0).Type.Sym(); {
1007 case s != nil && types.ReflectSymName(s) == "Method",
1008 s != nil && types.ReflectSymName(s) == "Value":
1011 // not a call to Method or MethodByName of reflect.{Type,Value}.
1015 var targetName ir.Node
1018 if methodName == "MethodByName" {
1019 targetName = n.Args[0]
1022 if methodName == "MethodByName" {
1023 targetName = n.Args[1]
1026 base.FatalfAt(dot.Pos(), "usemethod: unexpected dot.Op() %s", dot.Op())
1029 if ir.IsConst(targetName, constant.String) {
1030 name := constant.StringVal(targetName.Val())
1032 r := obj.Addrel(ir.CurFunc.LSym)
1033 r.Type = objabi.R_USENAMEDMETHOD
1034 r.Sym = staticdata.StringSymNoCommon(name)
1036 ir.CurFunc.LSym.Set(obj.AttrReflectMethod, true)
1040 func usefield(n *ir.SelectorExpr) {
1041 if !buildcfg.Experiment.FieldTrack {
1047 base.Fatalf("usefield %v", n.Op())
1049 case ir.ODOT, ir.ODOTPTR:
1053 field := n.Selection
1055 base.Fatalf("usefield %v %v without paramfld", n.X.Type(), n.Sel)
1057 if field.Sym != n.Sel {
1058 base.Fatalf("field inconsistency: %v != %v", field.Sym, n.Sel)
1060 if !strings.Contains(field.Note, "go:\"track\"") {
1066 outer = outer.Elem()
1068 if outer.Sym() == nil {
1069 base.Errorf("tracked field must be in named struct type")
1072 sym := reflectdata.TrackSym(outer, field)
1073 if ir.CurFunc.FieldTrack == nil {
1074 ir.CurFunc.FieldTrack = make(map[*obj.LSym]struct{})
1076 ir.CurFunc.FieldTrack[sym] = struct{}{}