1 // Copyright 2012 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.
12 "cmd/compile/internal/base"
13 "cmd/compile/internal/escape"
14 "cmd/compile/internal/ir"
15 "cmd/compile/internal/reflectdata"
16 "cmd/compile/internal/staticinit"
17 "cmd/compile/internal/typecheck"
18 "cmd/compile/internal/types"
22 // Rewrite tree to use separate statements to enforce
23 // order of evaluation. Makes walk easier, because it
24 // can (after this runs) reorder at will within an expression.
26 // Rewrite m[k] op= r into m[k] = m[k] op r if op is / or %.
28 // Introduce temporaries as needed by runtime routines.
29 // For example, the map runtime routines take the map key
30 // by reference, so make sure all map keys are addressable
31 // by copying them to temporaries as needed.
32 // The same is true for channel operations.
34 // Arrange that map index expressions only appear in direct
35 // assignments x = m[k] or m[k] = x, never in larger expressions.
37 // Arrange that receive expressions only appear in direct assignments
38 // x = <-c or as standalone statements <-c, never in larger expressions.
40 // TODO(rsc): The temporary introduction during multiple assignments
41 // should be moved into this file, so that the temporaries can be cleaned
42 // and so that conversions implicit in the OAS2FUNC and OAS2RECV
43 // nodes can be made explicit and then have their temporaries cleaned.
45 // TODO(rsc): Goto and multilevel break/continue can jump over
46 // inserted VARKILL annotations. Work out a way to handle these.
47 // The current implementation is safe, in that it will execute correctly.
48 // But it won't reuse temporaries as aggressively as it might, and
49 // it can result in unnecessary zeroing of those variables in the function
52 // orderState holds state during the ordering process.
53 type orderState struct {
54 out []ir.Node // list of generated statements
55 temp []*ir.Name // stack of temporary variables
56 free map[string][]*ir.Name // free list of unused temporaries, by type.LongString().
57 edit func(ir.Node) ir.Node // cached closure of o.exprNoLHS
60 // Order rewrites fn.Nbody to apply the ordering constraints
61 // described in the comment at the top of the file.
62 func order(fn *ir.Func) {
64 s := fmt.Sprintf("\nbefore order %v", fn.Sym())
65 ir.DumpList(s, fn.Body)
68 orderBlock(&fn.Body, map[string][]*ir.Name{})
71 // append typechecks stmt and appends it to out.
72 func (o *orderState) append(stmt ir.Node) {
73 o.out = append(o.out, typecheck.Stmt(stmt))
76 // newTemp allocates a new temporary with the given type,
77 // pushes it onto the temp stack, and returns it.
78 // If clear is true, newTemp emits code to zero the temporary.
79 func (o *orderState) newTemp(t *types.Type, clear bool) *ir.Name {
81 // Note: LongString is close to the type equality we want,
82 // but not exactly. We still need to double-check with types.Identical.
86 if types.Identical(t, n.Type()) {
98 o.append(ir.NewAssignStmt(base.Pos, v, nil))
101 o.temp = append(o.temp, v)
105 // copyExpr behaves like newTemp but also emits
106 // code to initialize the temporary to the value n.
107 func (o *orderState) copyExpr(n ir.Node) *ir.Name {
108 return o.copyExpr1(n, false)
111 // copyExprClear is like copyExpr but clears the temp before assignment.
112 // It is provided for use when the evaluation of tmp = n turns into
113 // a function call that is passed a pointer to the temporary as the output space.
114 // If the call blocks before tmp has been written,
115 // the garbage collector will still treat the temporary as live,
116 // so we must zero it before entering that call.
117 // Today, this only happens for channel receive operations.
118 // (The other candidate would be map access, but map access
119 // returns a pointer to the result data instead of taking a pointer
121 func (o *orderState) copyExprClear(n ir.Node) *ir.Name {
122 return o.copyExpr1(n, true)
125 func (o *orderState) copyExpr1(n ir.Node, clear bool) *ir.Name {
127 v := o.newTemp(t, clear)
128 o.append(ir.NewAssignStmt(base.Pos, v, n))
132 // cheapExpr returns a cheap version of n.
133 // The definition of cheap is that n is a variable or constant.
134 // If not, cheapExpr allocates a new tmp, emits tmp = n,
135 // and then returns tmp.
136 func (o *orderState) cheapExpr(n ir.Node) ir.Node {
142 case ir.ONAME, ir.OLITERAL, ir.ONIL:
144 case ir.OLEN, ir.OCAP:
145 n := n.(*ir.UnaryExpr)
146 l := o.cheapExpr(n.X)
150 a := ir.SepCopy(n).(*ir.UnaryExpr)
152 return typecheck.Expr(a)
158 // safeExpr returns a safe version of n.
159 // The definition of safe is that n can appear multiple times
160 // without violating the semantics of the original program,
161 // and that assigning to the safe version has the same effect
162 // as assigning to the original n.
164 // The intended use is to apply to x when rewriting x += y into x = x + y.
165 func (o *orderState) safeExpr(n ir.Node) ir.Node {
167 case ir.ONAME, ir.OLITERAL, ir.ONIL:
170 case ir.OLEN, ir.OCAP:
171 n := n.(*ir.UnaryExpr)
176 a := ir.SepCopy(n).(*ir.UnaryExpr)
178 return typecheck.Expr(a)
181 n := n.(*ir.SelectorExpr)
186 a := ir.SepCopy(n).(*ir.SelectorExpr)
188 return typecheck.Expr(a)
191 n := n.(*ir.SelectorExpr)
192 l := o.cheapExpr(n.X)
196 a := ir.SepCopy(n).(*ir.SelectorExpr)
198 return typecheck.Expr(a)
201 n := n.(*ir.StarExpr)
202 l := o.cheapExpr(n.X)
206 a := ir.SepCopy(n).(*ir.StarExpr)
208 return typecheck.Expr(a)
210 case ir.OINDEX, ir.OINDEXMAP:
211 n := n.(*ir.IndexExpr)
213 if n.X.Type().IsArray() {
218 r := o.cheapExpr(n.Index)
219 if l == n.X && r == n.Index {
222 a := ir.SepCopy(n).(*ir.IndexExpr)
225 return typecheck.Expr(a)
228 base.Fatalf("order.safeExpr %v", n.Op())
229 return nil // not reached
233 // isaddrokay reports whether it is okay to pass n's address to runtime routines.
234 // Taking the address of a variable makes the liveness and optimization analyses
235 // lose track of where the variable's lifetime ends. To avoid hurting the analyses
236 // of ordinary stack variables, those are not 'isaddrokay'. Temporaries are okay,
237 // because we emit explicit VARKILL instructions marking the end of those
238 // temporaries' lifetimes.
239 func isaddrokay(n ir.Node) bool {
240 return ir.IsAddressable(n) && (n.Op() != ir.ONAME || n.(*ir.Name).Class == ir.PEXTERN || ir.IsAutoTmp(n))
243 // addrTemp ensures that n is okay to pass by address to runtime routines.
244 // If the original argument n is not okay, addrTemp creates a tmp, emits
245 // tmp = n, and then returns tmp.
246 // The result of addrTemp MUST be assigned back to n, e.g.
247 // n.Left = o.addrTemp(n.Left)
248 func (o *orderState) addrTemp(n ir.Node) ir.Node {
249 if n.Op() == ir.OLITERAL || n.Op() == ir.ONIL {
250 // TODO: expand this to all static composite literal nodes?
251 n = typecheck.DefaultLit(n, nil)
252 types.CalcSize(n.Type())
253 vstat := readonlystaticname(n.Type())
254 var s staticinit.Schedule
255 s.StaticAssign(vstat, 0, n, n.Type())
257 base.Fatalf("staticassign of const generated code: %+v", n)
259 vstat = typecheck.Expr(vstat).(*ir.Name)
268 // mapKeyTemp prepares n to be a key in a map runtime call and returns n.
269 // It should only be used for map runtime calls which have *_fast* versions.
270 func (o *orderState) mapKeyTemp(t *types.Type, n ir.Node) ir.Node {
271 // Most map calls need to take the address of the key.
272 // Exception: map*_fast* calls. See golang.org/issue/19015.
280 kt = types.Types[types.TUINT32]
282 kt = types.Types[types.TUINT64]
283 case mapfast32ptr, mapfast64ptr:
284 kt = types.Types[types.TUNSAFEPTR]
286 kt = types.Types[types.TSTRING]
292 case nt.Kind() == kt.Kind(), nt.IsPtrShaped() && kt.IsPtrShaped():
293 // can directly convert (e.g. named type to underlying type, or one pointer to another)
294 return typecheck.Expr(ir.NewConvExpr(n.Pos(), ir.OCONVNOP, kt, n))
295 case nt.IsInteger() && kt.IsInteger():
296 // can directly convert (e.g. int32 to uint32)
297 if n.Op() == ir.OLITERAL && nt.IsSigned() {
298 // avoid constant overflow error
299 n = ir.NewConstExpr(constant.MakeUint64(uint64(ir.Int64Val(n))), n)
303 return typecheck.Expr(ir.NewConvExpr(n.Pos(), ir.OCONV, kt, n))
305 // Unsafe cast through memory.
306 // We'll need to do a load with type kt. Create a temporary of type kt to
307 // ensure sufficient alignment. nt may be under-aligned.
308 if kt.Align < nt.Align {
309 base.Fatalf("mapKeyTemp: key type is not sufficiently aligned, kt=%v nt=%v", kt, nt)
311 tmp := o.newTemp(kt, true)
313 var e ir.Node = typecheck.NodAddr(tmp)
314 e = ir.NewConvExpr(n.Pos(), ir.OCONVNOP, nt.PtrTo(), e)
315 e = ir.NewStarExpr(n.Pos(), e)
316 o.append(ir.NewAssignStmt(base.Pos, e, n))
321 // mapKeyReplaceStrConv replaces OBYTES2STR by OBYTES2STRTMP
322 // in n to avoid string allocations for keys in map lookups.
323 // Returns a bool that signals if a modification was made.
327 // x = m[T1{... Tn{..., string(k), ...}]
328 // where k is []byte, T1 to Tn is a nesting of struct and array literals,
329 // the allocation of backing bytes for the string can be avoided
330 // by reusing the []byte backing array. These are special cases
331 // for avoiding allocations when converting byte slices to strings.
332 // It would be nice to handle these generally, but because
333 // []byte keys are not allowed in maps, the use of string(k)
334 // comes up in important cases in practice. See issue 3512.
335 func mapKeyReplaceStrConv(n ir.Node) bool {
339 n := n.(*ir.ConvExpr)
340 n.SetOp(ir.OBYTES2STRTMP)
343 n := n.(*ir.CompLitExpr)
344 for _, elem := range n.List {
345 elem := elem.(*ir.StructKeyExpr)
346 if mapKeyReplaceStrConv(elem.Value) {
351 n := n.(*ir.CompLitExpr)
352 for _, elem := range n.List {
353 if elem.Op() == ir.OKEY {
354 elem = elem.(*ir.KeyExpr).Value
356 if mapKeyReplaceStrConv(elem) {
366 // markTemp returns the top of the temporary variable stack.
367 func (o *orderState) markTemp() ordermarker {
368 return ordermarker(len(o.temp))
371 // popTemp pops temporaries off the stack until reaching the mark,
372 // which must have been returned by markTemp.
373 func (o *orderState) popTemp(mark ordermarker) {
374 for _, n := range o.temp[mark:] {
375 key := n.Type().LongString()
376 o.free[key] = append(o.free[key], n)
378 o.temp = o.temp[:mark]
381 // cleanTempNoPop emits VARKILL instructions to *out
382 // for each temporary above the mark on the temporary stack.
383 // It does not pop the temporaries from the stack.
384 func (o *orderState) cleanTempNoPop(mark ordermarker) []ir.Node {
386 for i := len(o.temp) - 1; i >= int(mark); i-- {
388 out = append(out, typecheck.Stmt(ir.NewUnaryExpr(base.Pos, ir.OVARKILL, n)))
393 // cleanTemp emits VARKILL instructions for each temporary above the
394 // mark on the temporary stack and removes them from the stack.
395 func (o *orderState) cleanTemp(top ordermarker) {
396 o.out = append(o.out, o.cleanTempNoPop(top)...)
400 // stmtList orders each of the statements in the list.
401 func (o *orderState) stmtList(l ir.Nodes) {
404 orderMakeSliceCopy(s[i:])
409 // orderMakeSliceCopy matches the pattern:
410 // m = OMAKESLICE([]T, x); OCOPY(m, s)
411 // and rewrites it to:
412 // m = OMAKESLICECOPY([]T, x, s); nil
413 func orderMakeSliceCopy(s []ir.Node) {
414 if base.Flag.N != 0 || base.Flag.Cfg.Instrumenting {
417 if len(s) < 2 || s[0] == nil || s[0].Op() != ir.OAS || s[1] == nil || s[1].Op() != ir.OCOPY {
421 as := s[0].(*ir.AssignStmt)
422 cp := s[1].(*ir.BinaryExpr)
423 if as.Y == nil || as.Y.Op() != ir.OMAKESLICE || ir.IsBlank(as.X) ||
424 as.X.Op() != ir.ONAME || cp.X.Op() != ir.ONAME || cp.Y.Op() != ir.ONAME ||
425 as.X.Name() != cp.X.Name() || cp.X.Name() == cp.Y.Name() {
426 // The line above this one is correct with the differing equality operators:
427 // we want as.X and cp.X to be the same name,
428 // but we want the initial data to be coming from a different name.
432 mk := as.Y.(*ir.MakeExpr)
433 if mk.Esc() == ir.EscNone || mk.Len == nil || mk.Cap != nil {
436 mk.SetOp(ir.OMAKESLICECOPY)
438 // Set bounded when m = OMAKESLICE([]T, len(s)); OCOPY(m, s)
439 mk.SetBounded(mk.Len.Op() == ir.OLEN && ir.SameSafeExpr(mk.Len.(*ir.UnaryExpr).X, cp.Y))
440 as.Y = typecheck.Expr(mk)
441 s[1] = nil // remove separate copy call
444 // edge inserts coverage instrumentation for libfuzzer.
445 func (o *orderState) edge() {
446 if base.Debug.Libfuzzer == 0 {
450 // Create a new uint8 counter to be allocated in section
451 // __libfuzzer_extra_counters.
452 counter := staticinit.StaticName(types.Types[types.TUINT8])
453 counter.SetLibfuzzerExtraCounter(true)
456 incr := ir.NewAssignOpStmt(base.Pos, ir.OADD, counter, ir.NewInt(1))
460 // orderBlock orders the block of statements in n into a new slice,
461 // and then replaces the old slice in n with the new slice.
462 // free is a map that can be used to obtain temporary variables by type.
463 func orderBlock(n *ir.Nodes, free map[string][]*ir.Name) {
466 mark := order.markTemp()
469 order.cleanTemp(mark)
473 // exprInPlace orders the side effects in *np and
474 // leaves them as the init list of the final *np.
475 // The result of exprInPlace MUST be assigned back to n, e.g.
476 // n.Left = o.exprInPlace(n.Left)
477 func (o *orderState) exprInPlace(n ir.Node) ir.Node {
480 n = order.expr(n, nil)
481 n = ir.InitExpr(order.out, n)
483 // insert new temporaries from order
484 // at head of outer list.
485 o.temp = append(o.temp, order.temp...)
489 // orderStmtInPlace orders the side effects of the single statement *np
490 // and replaces it with the resulting statement list.
491 // The result of orderStmtInPlace MUST be assigned back to n, e.g.
492 // n.Left = orderStmtInPlace(n.Left)
493 // free is a map that can be used to obtain temporary variables by type.
494 func orderStmtInPlace(n ir.Node, free map[string][]*ir.Name) ir.Node {
497 mark := order.markTemp()
499 order.cleanTemp(mark)
500 return ir.NewBlockStmt(src.NoXPos, order.out)
503 // init moves n's init list to o.out.
504 func (o *orderState) init(n ir.Node) {
505 if ir.MayBeShared(n) {
506 // For concurrency safety, don't mutate potentially shared nodes.
507 // First, ensure that no work is required here.
508 if len(n.Init()) > 0 {
509 base.Fatalf("order.init shared node with ninit")
513 o.stmtList(ir.TakeInit(n))
516 // call orders the call expression n.
517 // n.Op is OCALLMETH/OCALLFUNC/OCALLINTER or a builtin like OCOPY.
518 func (o *orderState) call(nn ir.Node) {
519 if len(nn.Init()) > 0 {
520 // Caller should have already called o.init(nn).
521 base.Fatalf("%v with unexpected ninit", nn.Op())
524 // Builtin functions.
525 if nn.Op() != ir.OCALLFUNC && nn.Op() != ir.OCALLMETH && nn.Op() != ir.OCALLINTER {
526 switch n := nn.(type) {
528 base.Fatalf("unexpected call: %+v", n)
530 n.X = o.expr(n.X, nil)
532 n.X = o.expr(n.X, nil)
534 n.X = o.expr(n.X, nil)
535 n.Y = o.expr(n.Y, nil)
537 n.Len = o.expr(n.Len, nil)
538 n.Cap = o.expr(n.Cap, nil)
545 n := nn.(*ir.CallExpr)
546 typecheck.FixVariadicCall(n)
548 if isFuncPCIntrinsic(n) && isIfaceOfFunc(n.Args[0]) {
549 // For internal/abi.FuncPCABIxxx(fn), if fn is a defined function,
550 // do not introduce temporaries here, so it is easier to rewrite it
551 // to symbol address reference later in walk.
555 n.X = o.expr(n.X, nil)
558 if n.Op() == ir.OCALLINTER {
561 keepAlive := func(arg ir.Node) {
562 // If the argument is really a pointer being converted to uintptr,
563 // arrange for the pointer to be kept alive until the call returns,
564 // by copying it into a temp and marking that temp
565 // still alive when we pop the temp stack.
566 if arg.Op() == ir.OCONVNOP {
567 arg := arg.(*ir.ConvExpr)
568 if arg.X.Type().IsUnsafePtr() {
569 x := o.copyExpr(arg.X)
571 x.SetAddrtaken(true) // ensure SSA keeps the x variable
572 n.KeepAlive = append(n.KeepAlive, x)
577 // Check for "unsafe-uintptr" tag provided by escape analysis.
578 for i, param := range n.X.Type().Params().FieldSlice() {
579 if param.Note == escape.UnsafeUintptrNote || param.Note == escape.UintptrEscapesNote {
580 if arg := n.Args[i]; arg.Op() == ir.OSLICELIT {
581 arg := arg.(*ir.CompLitExpr)
582 for _, elt := range arg.List {
592 // mapAssign appends n to o.out.
593 func (o *orderState) mapAssign(n ir.Node) {
596 base.Fatalf("order.mapAssign %v", n.Op())
599 n := n.(*ir.AssignStmt)
600 if n.X.Op() == ir.OINDEXMAP {
601 n.Y = o.safeMapRHS(n.Y)
603 o.out = append(o.out, n)
605 n := n.(*ir.AssignOpStmt)
606 if n.X.Op() == ir.OINDEXMAP {
607 n.Y = o.safeMapRHS(n.Y)
609 o.out = append(o.out, n)
613 func (o *orderState) safeMapRHS(r ir.Node) ir.Node {
614 // Make sure we evaluate the RHS before starting the map insert.
615 // We need to make sure the RHS won't panic. See issue 22881.
616 if r.Op() == ir.OAPPEND {
617 r := r.(*ir.CallExpr)
619 for i, n := range s {
620 s[i] = o.cheapExpr(n)
624 return o.cheapExpr(r)
627 // stmt orders the statement n, appending to o.out.
628 // Temporaries created during the statement are cleaned
629 // up using VARKILL instructions as possible.
630 func (o *orderState) stmt(n ir.Node) {
640 base.Fatalf("order.stmt %v", n.Op())
642 case ir.OVARKILL, ir.OVARLIVE, ir.OINLMARK:
643 o.out = append(o.out, n)
646 n := n.(*ir.AssignStmt)
648 n.X = o.expr(n.X, nil)
649 n.Y = o.expr(n.Y, n.X)
654 n := n.(*ir.AssignOpStmt)
656 n.X = o.expr(n.X, nil)
657 n.Y = o.expr(n.Y, nil)
659 if base.Flag.Cfg.Instrumenting || n.X.Op() == ir.OINDEXMAP && (n.AsOp == ir.ODIV || n.AsOp == ir.OMOD) {
660 // Rewrite m[k] op= r into m[k] = m[k] op r so
661 // that we can ensure that if op panics
662 // because r is zero, the panic happens before
663 // the map assignment.
664 // DeepCopy is a big hammer here, but safeExpr
665 // makes sure there is nothing too deep being copied.
666 l1 := o.safeExpr(n.X)
667 l2 := ir.DeepCopy(src.NoXPos, l1)
668 if l2.Op() == ir.OINDEXMAP {
669 l2 := l2.(*ir.IndexExpr)
673 r := o.expr(typecheck.Expr(ir.NewBinaryExpr(n.Pos(), n.AsOp, l2, n.Y)), nil)
674 as := typecheck.Stmt(ir.NewAssignStmt(n.Pos(), l1, r))
684 n := n.(*ir.AssignListStmt)
688 o.out = append(o.out, n)
691 // Special: avoid copy of func call n.Right
693 n := n.(*ir.AssignListStmt)
701 // Special: use temporary variables to hold result,
702 // so that runtime can take address of temporary.
703 // No temporary for blank assignment.
705 // OAS2MAPR: make sure key is addressable if needed,
706 // and make sure OINDEXMAP is not copied out.
707 case ir.OAS2DOTTYPE, ir.OAS2RECV, ir.OAS2MAPR:
708 n := n.(*ir.AssignListStmt)
712 switch r := n.Rhs[0]; r.Op() {
714 r := r.(*ir.TypeAssertExpr)
715 r.X = o.expr(r.X, nil)
717 r := r.(*ir.UnaryExpr)
718 r.X = o.expr(r.X, nil)
720 r := r.(*ir.IndexExpr)
721 r.X = o.expr(r.X, nil)
722 r.Index = o.expr(r.Index, nil)
723 // See similar conversion for OINDEXMAP below.
724 _ = mapKeyReplaceStrConv(r.Index)
725 r.Index = o.mapKeyTemp(r.X.Type(), r.Index)
727 base.Fatalf("order.stmt: %v", r.Op())
733 // Special: does not save n onto out.
735 n := n.(*ir.BlockStmt)
738 // Special: n->left is not an expression; save as is.
748 o.out = append(o.out, n)
750 // Special: handle call arguments.
751 case ir.OCALLFUNC, ir.OCALLINTER, ir.OCALLMETH:
752 n := n.(*ir.CallExpr)
755 o.out = append(o.out, n)
758 case ir.OCLOSE, ir.ORECV:
759 n := n.(*ir.UnaryExpr)
761 n.X = o.expr(n.X, nil)
762 o.out = append(o.out, n)
766 n := n.(*ir.BinaryExpr)
768 n.X = o.expr(n.X, nil)
769 n.Y = o.expr(n.Y, nil)
770 o.out = append(o.out, n)
773 case ir.OPRINT, ir.OPRINTN, ir.ORECOVER:
774 n := n.(*ir.CallExpr)
777 o.out = append(o.out, n)
780 // Special: order arguments to inner call but not call itself.
781 case ir.ODEFER, ir.OGO:
782 n := n.(*ir.GoDeferStmt)
786 if n.Call.Op() == ir.ORECOVER {
787 // Special handling of "defer recover()". We need to evaluate the FP
788 // argument before wrapping.
790 n.Call = walkRecover(n.Call.(*ir.CallExpr), &init)
793 if buildcfg.Experiment.RegabiDefer {
796 o.out = append(o.out, n)
800 n := n.(*ir.CallExpr)
802 n.Args[0] = o.expr(n.Args[0], nil)
803 n.Args[1] = o.expr(n.Args[1], nil)
804 n.Args[1] = o.mapKeyTemp(n.Args[0].Type(), n.Args[1])
805 o.out = append(o.out, n)
808 // Clean temporaries from condition evaluation at
809 // beginning of loop body and after for statement.
813 n.Cond = o.exprInPlace(n.Cond)
814 n.Body.Prepend(o.cleanTempNoPop(t)...)
815 orderBlock(&n.Body, o.free)
816 n.Post = orderStmtInPlace(n.Post, o.free)
817 o.out = append(o.out, n)
820 // Clean temporaries from condition at
821 // beginning of both branches.
825 n.Cond = o.exprInPlace(n.Cond)
826 n.Body.Prepend(o.cleanTempNoPop(t)...)
827 n.Else.Prepend(o.cleanTempNoPop(t)...)
829 orderBlock(&n.Body, o.free)
830 orderBlock(&n.Else, o.free)
831 o.out = append(o.out, n)
834 n := n.(*ir.UnaryExpr)
836 n.X = o.expr(n.X, nil)
837 if !n.X.Type().IsEmptyInterface() {
838 base.FatalfAt(n.Pos(), "bad argument to panic: %L", n.X)
840 o.out = append(o.out, n)
844 // n.Right is the expression being ranged over.
845 // order it, and then make a copy if we need one.
846 // We almost always do, to ensure that we don't
847 // see any value changes made during the loop.
848 // Usually the copy is cheap (e.g., array pointer,
849 // chan, slice, string are all tiny).
850 // The exception is ranging over an array value
851 // (not a slice, not a pointer to array),
852 // which must make a copy to avoid seeing updates made during
853 // the range body. Ranging over an array value is uncommon though.
855 // Mark []byte(str) range expression to reuse string backing storage.
856 // It is safe because the storage cannot be mutated.
857 n := n.(*ir.RangeStmt)
858 if n.X.Op() == ir.OSTR2BYTES {
859 n.X.(*ir.ConvExpr).SetOp(ir.OSTR2BYTESTMP)
863 n.X = o.expr(n.X, nil)
866 xt := typecheck.RangeExprType(n.X.Type())
869 base.Fatalf("order.stmt range %v", n.Type())
871 case types.TARRAY, types.TSLICE:
872 if n.Value == nil || ir.IsBlank(n.Value) {
873 // for i := range x will only use x once, to compute len(x).
874 // No need to copy it.
879 case types.TCHAN, types.TSTRING:
880 // chan, string, slice, array ranges use value multiple times.
884 if r.Type().IsString() && r.Type() != types.Types[types.TSTRING] {
885 r = ir.NewConvExpr(base.Pos, ir.OCONV, nil, r)
886 r.SetType(types.Types[types.TSTRING])
887 r = typecheck.Expr(r)
894 // Preserve the body of the map clear pattern so it can
895 // be detected during walk. The loop body will not be used
896 // when optimizing away the range loop to a runtime call.
901 // copy the map value in case it is a map literal.
902 // TODO(rsc): Make tmp = literal expressions reuse tmp.
903 // For maps tmp is just one word so it hardly matters.
907 // n.Prealloc is the temp for the iterator.
908 // MapIterType contains pointers and needs to be zeroed.
909 n.Prealloc = o.newTemp(reflectdata.MapIterType(xt), true)
911 n.Key = o.exprInPlace(n.Key)
912 n.Value = o.exprInPlace(n.Value)
914 orderBlock(&n.Body, o.free)
916 o.out = append(o.out, n)
920 n := n.(*ir.ReturnStmt)
921 o.exprList(n.Results)
922 o.out = append(o.out, n)
924 // Special: clean case temporaries in each block entry.
925 // Select must enter one of its blocks, so there is no
926 // need for a cleaning at the end.
927 // Doubly special: evaluation order for select is stricter
928 // than ordinary expressions. Even something like p.c
929 // has to be hoisted into a temporary, so that it cannot be
930 // reordered after the channel evaluation for a different
931 // case (if p were nil, then the timing of the fault would
934 n := n.(*ir.SelectStmt)
936 for _, ncas := range n.Cases {
940 // Append any new body prologue to ninit.
941 // The next loop will insert ninit into nbody.
942 if len(ncas.Init()) != 0 {
943 base.Fatalf("order select ninit")
950 ir.Dump("select case", r)
951 base.Fatalf("unknown op in select %v", r.Op())
955 r := r.(*ir.AssignListStmt)
956 recv := r.Rhs[0].(*ir.UnaryExpr)
957 recv.X = o.expr(recv.X, nil)
958 if !ir.IsAutoTmp(recv.X) {
959 recv.X = o.copyExpr(recv.X)
961 init := ir.TakeInit(r)
964 do := func(i int, t *types.Type) {
969 // If this is case x := <-ch or case x, y := <-ch, the case has
970 // the ODCL nodes to declare x and y. We want to delay that
971 // declaration (and possible allocation) until inside the case body.
972 // Delete the ODCL nodes here and recreate them inside the body below.
974 if len(init) > 0 && init[0].Op() == ir.ODCL && init[0].(*ir.Decl).X == n {
977 dcl := typecheck.Stmt(ir.NewDecl(base.Pos, ir.ODCL, n.(*ir.Name)))
978 ncas.PtrInit().Append(dcl)
980 tmp := o.newTemp(t, t.HasPointers())
981 as := typecheck.Stmt(ir.NewAssignStmt(base.Pos, n, typecheck.Conv(tmp, n.Type())))
982 ncas.PtrInit().Append(as)
985 do(0, recv.X.Type().Elem())
986 do(1, types.Types[types.TBOOL])
988 ir.DumpList("ninit", r.Init())
989 base.Fatalf("ninit on select recv")
991 orderBlock(ncas.PtrInit(), o.free)
994 r := r.(*ir.SendStmt)
995 if len(r.Init()) != 0 {
996 ir.DumpList("ninit", r.Init())
997 base.Fatalf("ninit on select send")
1001 // r->left is c, r->right is x, both are always evaluated.
1002 r.Chan = o.expr(r.Chan, nil)
1004 if !ir.IsAutoTmp(r.Chan) {
1005 r.Chan = o.copyExpr(r.Chan)
1007 r.Value = o.expr(r.Value, nil)
1008 if !ir.IsAutoTmp(r.Value) {
1009 r.Value = o.copyExpr(r.Value)
1013 // Now that we have accumulated all the temporaries, clean them.
1014 // Also insert any ninit queued during the previous loop.
1015 // (The temporary cleaning must follow that ninit work.)
1016 for _, cas := range n.Cases {
1017 orderBlock(&cas.Body, o.free)
1018 cas.Body.Prepend(o.cleanTempNoPop(t)...)
1020 // TODO(mdempsky): Is this actually necessary?
1021 // walkSelect appears to walk Ninit.
1022 cas.Body.Prepend(ir.TakeInit(cas)...)
1025 o.out = append(o.out, n)
1028 // Special: value being sent is passed as a pointer; make it addressable.
1030 n := n.(*ir.SendStmt)
1032 n.Chan = o.expr(n.Chan, nil)
1033 n.Value = o.expr(n.Value, nil)
1034 if base.Flag.Cfg.Instrumenting {
1035 // Force copying to the stack so that (chan T)(nil) <- x
1036 // is still instrumented as a read of x.
1037 n.Value = o.copyExpr(n.Value)
1039 n.Value = o.addrTemp(n.Value)
1041 o.out = append(o.out, n)
1044 // TODO(rsc): Clean temporaries more aggressively.
1045 // Note that because walkSwitch will rewrite some of the
1046 // switch into a binary search, this is not as easy as it looks.
1047 // (If we ran that code here we could invoke order.stmt on
1048 // the if-else chain instead.)
1049 // For now just clean all the temporaries at the end.
1050 // In practice that's fine.
1052 n := n.(*ir.SwitchStmt)
1053 if base.Debug.Libfuzzer != 0 && !hasDefaultCase(n) {
1054 // Add empty "default:" case for instrumentation.
1055 n.Cases = append(n.Cases, ir.NewCaseStmt(base.Pos, nil, nil))
1059 n.Tag = o.expr(n.Tag, nil)
1060 for _, ncas := range n.Cases {
1061 o.exprListInPlace(ncas.List)
1062 orderBlock(&ncas.Body, o.free)
1065 o.out = append(o.out, n)
1072 func hasDefaultCase(n *ir.SwitchStmt) bool {
1073 for _, ncas := range n.Cases {
1074 if len(ncas.List) == 0 {
1081 // exprList orders the expression list l into o.
1082 func (o *orderState) exprList(l ir.Nodes) {
1085 s[i] = o.expr(s[i], nil)
1089 // exprListInPlace orders the expression list l but saves
1090 // the side effects on the individual expression ninit lists.
1091 func (o *orderState) exprListInPlace(l ir.Nodes) {
1094 s[i] = o.exprInPlace(s[i])
1098 func (o *orderState) exprNoLHS(n ir.Node) ir.Node {
1099 return o.expr(n, nil)
1102 // expr orders a single expression, appending side
1103 // effects to o.out as needed.
1104 // If this is part of an assignment lhs = *np, lhs is given.
1105 // Otherwise lhs == nil. (When lhs != nil it may be possible
1106 // to avoid copying the result of the expression to a temporary.)
1107 // The result of expr MUST be assigned back to n, e.g.
1108 // n.Left = o.expr(n.Left, lhs)
1109 func (o *orderState) expr(n, lhs ir.Node) ir.Node {
1119 func (o *orderState) expr1(n, lhs ir.Node) ir.Node {
1125 o.edit = o.exprNoLHS // create closure once
1127 ir.EditChildren(n, o.edit)
1130 // Addition of strings turns into a function call.
1131 // Allocate a temporary to hold the strings.
1132 // Fewer than 5 strings use direct runtime helpers.
1134 n := n.(*ir.AddStringExpr)
1137 if len(n.List) > 5 {
1138 t := types.NewArray(types.Types[types.TSTRING], int64(len(n.List)))
1139 n.Prealloc = o.newTemp(t, false)
1142 // Mark string(byteSlice) arguments to reuse byteSlice backing
1143 // buffer during conversion. String concatenation does not
1144 // memorize the strings for later use, so it is safe.
1145 // However, we can do it only if there is at least one non-empty string literal.
1146 // Otherwise if all other arguments are empty strings,
1147 // concatstrings will return the reference to the temp string
1152 for _, n1 := range n.List {
1153 hasbyte = hasbyte || n1.Op() == ir.OBYTES2STR
1154 haslit = haslit || n1.Op() == ir.OLITERAL && len(ir.StringVal(n1)) != 0
1157 if haslit && hasbyte {
1158 for _, n2 := range n.List {
1159 if n2.Op() == ir.OBYTES2STR {
1160 n2 := n2.(*ir.ConvExpr)
1161 n2.SetOp(ir.OBYTES2STRTMP)
1168 n := n.(*ir.IndexExpr)
1169 n.X = o.expr(n.X, nil)
1170 n.Index = o.expr(n.Index, nil)
1174 // Enforce that any []byte slices we are not copying
1175 // can not be changed before the map index by forcing
1176 // the map index to happen immediately following the
1177 // conversions. See copyExpr a few lines below.
1178 needCopy = mapKeyReplaceStrConv(n.Index)
1180 if base.Flag.Cfg.Instrumenting {
1181 // Race detector needs the copy.
1186 // key must be addressable
1187 n.Index = o.mapKeyTemp(n.X.Type(), n.Index)
1189 return o.copyExpr(n)
1193 // concrete type (not interface) argument might need an addressable
1194 // temporary to pass to the runtime conversion routine.
1196 n := n.(*ir.ConvExpr)
1197 n.X = o.expr(n.X, nil)
1198 if n.X.Type().IsInterface() {
1201 if _, _, needsaddr := convFuncName(n.X.Type(), n.Type()); needsaddr || isStaticCompositeLiteral(n.X) {
1202 // Need a temp if we need to pass the address to the conversion function.
1203 // We also process static composite literal node here, making a named static global
1204 // whose address we can put directly in an interface (see OCONVIFACE case in walk).
1205 n.X = o.addrTemp(n.X)
1210 n := n.(*ir.ConvExpr)
1211 if n.Type().IsKind(types.TUNSAFEPTR) && n.X.Type().IsKind(types.TUINTPTR) && (n.X.Op() == ir.OCALLFUNC || n.X.Op() == ir.OCALLINTER || n.X.Op() == ir.OCALLMETH) {
1212 call := n.X.(*ir.CallExpr)
1213 // When reordering unsafe.Pointer(f()) into a separate
1214 // statement, the conversion and function call must stay
1215 // together. See golang.org/issue/15329.
1218 if lhs == nil || lhs.Op() != ir.ONAME || base.Flag.Cfg.Instrumenting {
1219 return o.copyExpr(n)
1222 n.X = o.expr(n.X, nil)
1226 case ir.OANDAND, ir.OOROR:
1231 // if r { // or !r, for OROR
1236 n := n.(*ir.LogicalExpr)
1237 r := o.newTemp(n.Type(), false)
1239 // Evaluate left-hand side.
1240 lhs := o.expr(n.X, nil)
1241 o.out = append(o.out, typecheck.Stmt(ir.NewAssignStmt(base.Pos, r, lhs)))
1243 // Evaluate right-hand side, save generated code.
1248 rhs := o.expr(n.Y, nil)
1249 o.out = append(o.out, typecheck.Stmt(ir.NewAssignStmt(base.Pos, r, rhs)))
1254 // If left-hand side doesn't cause a short-circuit, issue right-hand side.
1255 nif := ir.NewIfStmt(base.Pos, r, nil, nil)
1256 if n.Op() == ir.OANDAND {
1261 o.out = append(o.out, nif)
1284 // len([]rune(s)) is rewritten to runtime.countrunes(s) later.
1285 conv := n.(*ir.UnaryExpr).X.(*ir.ConvExpr)
1286 conv.X = o.expr(conv.X, nil)
1291 if lhs == nil || lhs.Op() != ir.ONAME || base.Flag.Cfg.Instrumenting {
1292 return o.copyExpr(n)
1297 // Check for append(x, make([]T, y)...) .
1298 n := n.(*ir.CallExpr)
1299 if isAppendOfMake(n) {
1300 n.Args[0] = o.expr(n.Args[0], nil) // order x
1301 mk := n.Args[1].(*ir.MakeExpr)
1302 mk.Len = o.expr(mk.Len, nil) // order y
1307 if lhs == nil || lhs.Op() != ir.ONAME && !ir.SameSafeExpr(lhs, n.Args[0]) {
1308 return o.copyExpr(n)
1312 case ir.OSLICE, ir.OSLICEARR, ir.OSLICESTR, ir.OSLICE3, ir.OSLICE3ARR:
1313 n := n.(*ir.SliceExpr)
1314 n.X = o.expr(n.X, nil)
1315 n.Low = o.cheapExpr(o.expr(n.Low, nil))
1316 n.High = o.cheapExpr(o.expr(n.High, nil))
1317 n.Max = o.cheapExpr(o.expr(n.Max, nil))
1318 if lhs == nil || lhs.Op() != ir.ONAME && !ir.SameSafeExpr(lhs, n.X) {
1319 return o.copyExpr(n)
1324 n := n.(*ir.ClosureExpr)
1325 if n.Transient() && len(n.Func.ClosureVars) > 0 {
1326 n.Prealloc = o.newTemp(typecheck.ClosureType(n), false)
1331 n := n.(*ir.SelectorExpr)
1332 n.X = o.expr(n.X, nil)
1334 t := typecheck.PartialCallType(n)
1335 n.Prealloc = o.newTemp(t, false)
1340 n := n.(*ir.CompLitExpr)
1343 t := types.NewArray(n.Type().Elem(), n.Len)
1344 n.Prealloc = o.newTemp(t, false)
1348 case ir.ODOTTYPE, ir.ODOTTYPE2:
1349 n := n.(*ir.TypeAssertExpr)
1350 n.X = o.expr(n.X, nil)
1351 if !types.IsDirectIface(n.Type()) || base.Flag.Cfg.Instrumenting {
1352 return o.copyExprClear(n)
1357 n := n.(*ir.UnaryExpr)
1358 n.X = o.expr(n.X, nil)
1359 return o.copyExprClear(n)
1361 case ir.OEQ, ir.ONE, ir.OLT, ir.OLE, ir.OGT, ir.OGE:
1362 n := n.(*ir.BinaryExpr)
1363 n.X = o.expr(n.X, nil)
1364 n.Y = o.expr(n.Y, nil)
1369 // Mark string(byteSlice) arguments to reuse byteSlice backing
1370 // buffer during conversion. String comparison does not
1371 // memorize the strings for later use, so it is safe.
1372 if n.X.Op() == ir.OBYTES2STR {
1373 n.X.(*ir.ConvExpr).SetOp(ir.OBYTES2STRTMP)
1375 if n.Y.Op() == ir.OBYTES2STR {
1376 n.Y.(*ir.ConvExpr).SetOp(ir.OBYTES2STRTMP)
1379 case t.IsStruct() || t.IsArray():
1380 // for complex comparisons, we need both args to be
1381 // addressable so we can pass them to the runtime.
1382 n.X = o.addrTemp(n.X)
1383 n.Y = o.addrTemp(n.Y)
1388 // Order map by converting:
1395 // m := map[int]int{}
1399 // Then order the result.
1400 // Without this special case, order would otherwise compute all
1401 // the keys and values before storing any of them to the map.
1403 n := n.(*ir.CompLitExpr)
1405 statics := entries[:0]
1406 var dynamics []*ir.KeyExpr
1407 for _, r := range entries {
1408 r := r.(*ir.KeyExpr)
1410 if !isStaticCompositeLiteral(r.Key) || !isStaticCompositeLiteral(r.Value) {
1411 dynamics = append(dynamics, r)
1415 // Recursively ordering some static entries can change them to dynamic;
1416 // e.g., OCONVIFACE nodes. See #31777.
1417 r = o.expr(r, nil).(*ir.KeyExpr)
1418 if !isStaticCompositeLiteral(r.Key) || !isStaticCompositeLiteral(r.Value) {
1419 dynamics = append(dynamics, r)
1423 statics = append(statics, r)
1427 if len(dynamics) == 0 {
1431 // Emit the creation of the map (with all its static entries).
1432 m := o.newTemp(n.Type(), false)
1433 as := ir.NewAssignStmt(base.Pos, m, n)
1437 // Emit eval+insert of dynamic entries, one at a time.
1438 for _, r := range dynamics {
1439 as := ir.NewAssignStmt(base.Pos, ir.NewIndexExpr(base.Pos, m, r.Key), r.Value)
1440 typecheck.Stmt(as) // Note: this converts the OINDEX to an OINDEXMAP
1446 // No return - type-assertions above. Each case must return for itself.
1449 // as2func orders OAS2FUNC nodes. It creates temporaries to ensure left-to-right assignment.
1450 // The caller should order the right-hand side of the assignment before calling order.as2func.
1454 // tmp1, tmp2, tmp3 = ...
1455 // a, b, a = tmp1, tmp2, tmp3
1456 // This is necessary to ensure left to right assignment order.
1457 func (o *orderState) as2func(n *ir.AssignListStmt) {
1458 results := n.Rhs[0].Type()
1459 as := ir.NewAssignListStmt(n.Pos(), ir.OAS2, nil, nil)
1460 for i, nl := range n.Lhs {
1461 if !ir.IsBlank(nl) {
1462 typ := results.Field(i).Type
1463 tmp := o.newTemp(typ, typ.HasPointers())
1465 as.Lhs = append(as.Lhs, nl)
1466 as.Rhs = append(as.Rhs, tmp)
1470 o.out = append(o.out, n)
1471 o.stmt(typecheck.Stmt(as))
1474 // as2ok orders OAS2XXX with ok.
1475 // Just like as2func, this also adds temporaries to ensure left-to-right assignment.
1476 func (o *orderState) as2ok(n *ir.AssignListStmt) {
1477 as := ir.NewAssignListStmt(n.Pos(), ir.OAS2, nil, nil)
1479 do := func(i int, typ *types.Type) {
1480 if nl := n.Lhs[i]; !ir.IsBlank(nl) {
1481 var tmp ir.Node = o.newTemp(typ, typ.HasPointers())
1483 as.Lhs = append(as.Lhs, nl)
1485 // The "ok" result is an untyped boolean according to the Go
1486 // spec. We need to explicitly convert it to the LHS type in
1487 // case the latter is a defined boolean type (#8475).
1488 tmp = typecheck.Conv(tmp, nl.Type())
1490 as.Rhs = append(as.Rhs, tmp)
1494 do(0, n.Rhs[0].Type())
1495 do(1, types.Types[types.TBOOL])
1497 o.out = append(o.out, n)
1498 o.stmt(typecheck.Stmt(as))
1501 var wrapGoDefer_prgen int
1503 // wrapGoDefer wraps the target of a "go" or "defer" statement with a
1504 // new "function with no arguments" closure. Specifically, it converts
1511 // defer func() { f(x1, y1) }()
1513 // This is primarily to enable a quicker bringup of defers under the
1514 // new register ABI; by doing this conversion, we can simplify the
1515 // code in the runtime that invokes defers on the panic path.
1516 func (o *orderState) wrapGoDefer(n *ir.GoDeferStmt) {
1519 var callX ir.Node // thing being called
1520 var callArgs []ir.Node // call arguments
1521 var keepAlive []*ir.Name // KeepAlive list from call, if present
1523 // A helper to recreate the call within the closure.
1524 var mkNewCall func(pos src.XPos, op ir.Op, fun ir.Node, args []ir.Node) ir.Node
1526 // Defer calls come in many shapes and sizes; not all of them
1527 // are ir.CallExpr's. Examine the type to see what we're dealing with.
1528 switch x := call.(type) {
1532 keepAlive = x.KeepAlive
1533 mkNewCall = func(pos src.XPos, op ir.Op, fun ir.Node, args []ir.Node) ir.Node {
1534 newcall := ir.NewCallExpr(pos, op, fun, args)
1535 newcall.IsDDD = x.IsDDD
1536 return ir.Node(newcall)
1538 case *ir.UnaryExpr: // ex: OCLOSE
1539 callArgs = []ir.Node{x.X}
1540 mkNewCall = func(pos src.XPos, op ir.Op, fun ir.Node, args []ir.Node) ir.Node {
1542 panic("internal error, expecting single arg")
1544 return ir.Node(ir.NewUnaryExpr(pos, op, args[0]))
1546 case *ir.BinaryExpr: // ex: OCOPY
1547 callArgs = []ir.Node{x.X, x.Y}
1548 mkNewCall = func(pos src.XPos, op ir.Op, fun ir.Node, args []ir.Node) ir.Node {
1550 panic("internal error, expecting two args")
1552 return ir.Node(ir.NewBinaryExpr(pos, op, args[0], args[1]))
1555 panic("unhandled op")
1558 // No need to wrap if called func has no args, no receiver, and no results.
1559 // However in the case of "defer func() { ... }()" we need to
1560 // protect against the possibility of directClosureCall rewriting
1561 // things so that the call does have arguments.
1563 // Do wrap method calls (OCALLMETH, OCALLINTER), because it has
1566 // Also do wrap builtin functions, because they may be expanded to
1567 // calls with arguments (e.g. ORECOVER).
1569 // TODO: maybe not wrap if the called function has no arguments and
1570 // only in-register results?
1571 if len(callArgs) == 0 && call.Op() == ir.OCALLFUNC && callX.Type().NumResults() == 0 {
1572 if c, ok := call.(*ir.CallExpr); ok && callX != nil && callX.Op() == ir.OCLOSURE {
1573 cloFunc := callX.(*ir.ClosureExpr).Func
1574 cloFunc.SetClosureCalled(false)
1575 c.PreserveClosure = true
1580 if c, ok := call.(*ir.CallExpr); ok {
1581 // To simplify things, turn f(a, b, []T{c, d, e}...) back
1582 // into f(a, b, c, d, e) -- when the final call is run through the
1583 // type checker below, it will rebuild the proper slice literal.
1589 // This is set to true if the closure we're generating escapes
1590 // (needs heap allocation).
1591 cloEscapes := func() bool {
1592 if n.Op() == ir.OGO {
1593 // For "go", assume that all closures escape.
1596 // For defer, just use whatever result escape analysis
1597 // has determined for the defer.
1598 return n.Esc() != ir.EscNever
1601 // A helper for making a copy of an argument. Note that it is
1602 // not safe to use o.copyExpr(arg) if we're putting a
1603 // reference to the temp into the closure (as opposed to
1604 // copying it in by value), since in the by-reference case we
1605 // need a temporary whose lifetime extends to the end of the
1606 // function (as opposed to being local to the current block or
1607 // statement being ordered).
1608 mkArgCopy := func(arg ir.Node) *ir.Name {
1610 byval := t.Size() <= 128 || cloEscapes
1611 var argCopy *ir.Name
1613 argCopy = o.copyExpr(arg)
1615 argCopy = typecheck.Temp(t)
1616 o.append(ir.NewAssignStmt(base.Pos, argCopy, arg))
1618 // The value of 128 below is meant to be consistent with code
1619 // in escape analysis that picks byval/byaddr based on size.
1620 argCopy.SetByval(byval)
1624 // getUnsafeArg looks for an unsafe.Pointer arg that has been
1625 // previously captured into the call's keepalive list, returning
1626 // the name node for it if found.
1627 getUnsafeArg := func(arg ir.Node) *ir.Name {
1628 // Look for uintptr(unsafe.Pointer(name))
1629 if arg.Op() != ir.OCONVNOP {
1632 if !arg.Type().IsUintptr() {
1635 if !arg.(*ir.ConvExpr).X.Type().IsUnsafePtr() {
1638 arg = arg.(*ir.ConvExpr).X
1639 argname, ok := arg.(*ir.Name)
1643 for i := range keepAlive {
1644 if argname == keepAlive[i] {
1651 // Copy the arguments to the function into temps.
1653 // For calls with uintptr(unsafe.Pointer(...)) args that are being
1654 // kept alive (see code in (*orderState).call that does this), use
1655 // the existing arg copy instead of creating a new copy.
1656 unsafeArgs := make([]*ir.Name, len(callArgs))
1657 origArgs := callArgs
1658 var newNames []*ir.Name
1659 for i := range callArgs {
1661 var argname *ir.Name
1662 unsafeArgName := getUnsafeArg(arg)
1663 if unsafeArgName != nil {
1664 // arg has been copied already, use keepalive copy
1665 argname = unsafeArgName
1666 unsafeArgs[i] = unsafeArgName
1668 argname = mkArgCopy(arg)
1670 newNames = append(newNames, argname)
1673 // Deal with cases where the function expression (what we're
1674 // calling) is not a simple function symbol.
1676 var methSelectorExpr *ir.SelectorExpr
1679 case callX.Op() == ir.ODOTMETH || callX.Op() == ir.ODOTINTER:
1680 // Handle defer of a method call, e.g. "defer v.MyMethod(x, y)"
1681 n := callX.(*ir.SelectorExpr)
1682 n.X = mkArgCopy(n.X)
1683 methSelectorExpr = n
1684 if callX.Op() == ir.ODOTINTER {
1685 // Currently for "defer i.M()" if i is nil it panics at the
1686 // point of defer statement, not when deferred function is called.
1687 // (I think there is an issue discussing what is the intended
1688 // behavior but I cannot find it.)
1689 // We need to do the nil check outside of the wrapper.
1690 tab := typecheck.Expr(ir.NewUnaryExpr(base.Pos, ir.OITAB, n.X))
1691 c := ir.NewUnaryExpr(n.Pos(), ir.OCHECKNIL, tab)
1695 case !(callX.Op() == ir.ONAME && callX.(*ir.Name).Class == ir.PFUNC):
1696 // Deal with "defer returnsafunc()(x, y)" (for
1697 // example) by copying the callee expression.
1698 fnExpr = mkArgCopy(callX)
1699 if callX.Op() == ir.OCLOSURE {
1700 // For "defer func(...)", in addition to copying the
1701 // closure into a temp, mark it as no longer directly
1703 callX.(*ir.ClosureExpr).Func.SetClosureCalled(false)
1708 // Create a new no-argument function that we'll hand off to defer.
1709 var noFuncArgs []*ir.Field
1710 noargst := ir.NewFuncType(base.Pos, nil, noFuncArgs, nil)
1712 outerfn := ir.CurFunc
1713 wrapname := fmt.Sprintf("%v·dwrap·%d", outerfn, wrapGoDefer_prgen)
1714 sym := types.LocalPkg.Lookup(wrapname)
1715 fn := typecheck.DeclFunc(sym, noargst)
1716 fn.SetIsHiddenClosure(true)
1719 // helper for capturing reference to a var declared in an outer scope.
1720 capName := func(pos src.XPos, fn *ir.Func, n *ir.Name) *ir.Name {
1722 cv := ir.CaptureName(pos, fn, n)
1724 return typecheck.Expr(cv).(*ir.Name)
1727 // Call args (x1, y1) need to be captured as part of the newly
1729 newCallArgs := []ir.Node{}
1730 for i := range newNames {
1732 arg = capName(callArgs[i].Pos(), fn, newNames[i])
1733 if unsafeArgs[i] != nil {
1734 arg = ir.NewConvExpr(arg.Pos(), origArgs[i].Op(), origArgs[i].Type(), arg)
1736 newCallArgs = append(newCallArgs, arg)
1738 // Also capture the function or method expression (if needed) into
1741 callX = capName(callX.Pos(), fn, fnExpr)
1743 if methSelectorExpr != nil {
1744 methSelectorExpr.X = capName(callX.Pos(), fn, methSelectorExpr.X.(*ir.Name))
1746 ir.FinishCaptureNames(n.Pos(), outerfn, fn)
1748 // This flags a builtin as opposed to a regular call.
1749 irregular := (call.Op() != ir.OCALLFUNC &&
1750 call.Op() != ir.OCALLMETH &&
1751 call.Op() != ir.OCALLINTER)
1753 // Construct new function body: f(x1, y1)
1758 newcall := mkNewCall(call.Pos(), op, callX, newCallArgs)
1760 // Type-check the result.
1762 typecheck.Call(newcall.(*ir.CallExpr))
1764 typecheck.Stmt(newcall)
1767 // Finalize body, register function on the main decls list.
1768 fn.Body = []ir.Node{newcall}
1769 typecheck.FinishFuncBody()
1771 typecheck.Target.Decls = append(typecheck.Target.Decls, fn)
1773 // Create closure expr
1774 clo := ir.NewClosureExpr(n.Pos(), fn)
1776 clo.SetType(fn.Type())
1778 // Set escape properties for closure.
1779 if n.Op() == ir.OGO {
1780 // For "go", assume that the closure is going to escape
1781 // (with an exception for the runtime, which doesn't
1782 // permit heap-allocated closures).
1783 if base.Ctxt.Pkgpath != "runtime" {
1784 clo.SetEsc(ir.EscHeap)
1787 // For defer, just use whatever result escape analysis
1788 // has determined for the defer.
1789 if n.Esc() == ir.EscNever {
1790 clo.SetTransient(true)
1791 clo.SetEsc(ir.EscNone)
1795 // Create new top level call to closure over argless function.
1796 topcall := ir.NewCallExpr(n.Pos(), ir.OCALL, clo, []ir.Node{})
1797 typecheck.Call(topcall)
1799 // Tag the call to insure that directClosureCall doesn't undo our work.
1800 topcall.PreserveClosure = true
1802 fn.SetClosureCalled(false)
1804 // Finally, point the defer statement at the newly generated call.
1808 // isFuncPCIntrinsic returns whether n is a direct call of internal/abi.FuncPCABIxxx functions.
1809 func isFuncPCIntrinsic(n *ir.CallExpr) bool {
1810 if n.Op() != ir.OCALLFUNC || n.X.Op() != ir.ONAME {
1813 fn := n.X.(*ir.Name).Sym()
1814 return (fn.Name == "FuncPCABI0" || fn.Name == "FuncPCABIInternal") &&
1815 (fn.Pkg.Path == "internal/abi" || fn.Pkg == types.LocalPkg && base.Ctxt.Pkgpath == "internal/abi")
1818 // isIfaceOfFunc returns whether n is an interface conversion from a direct reference of a func.
1819 func isIfaceOfFunc(n ir.Node) bool {
1820 return n.Op() == ir.OCONVIFACE && n.(*ir.ConvExpr).X.Op() == ir.ONAME && n.(*ir.ConvExpr).X.(*ir.Name).Class == ir.PFUNC