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.
15 // The constant is known to runtime.
24 s := fmt.Sprintf("\nbefore %v", Curfn.Func.Nname.Sym)
25 dumplist(s, Curfn.Nbody)
30 // Final typecheck for any unused variables.
31 // It's hard to be on the heap when not-used, but best to be consistent about &~PHEAP here and below.
32 for l := fn.Func.Dcl; l != nil; l = l.Next {
33 if l.N.Op == ONAME && l.N.Class&^PHEAP == PAUTO {
34 typecheck(&l.N, Erv|Easgn)
38 // Propagate the used flag for typeswitch variables up to the NONAME in it's definition.
39 for l := fn.Func.Dcl; l != nil; l = l.Next {
40 if l.N.Op == ONAME && l.N.Class&^PHEAP == PAUTO && l.N.Name.Defn != nil && l.N.Name.Defn.Op == OTYPESW && l.N.Used {
41 l.N.Name.Defn.Left.Used = true
45 for l := fn.Func.Dcl; l != nil; l = l.Next {
46 if l.N.Op != ONAME || l.N.Class&^PHEAP != PAUTO || l.N.Sym.Name[0] == '&' || l.N.Used {
49 if defn := l.N.Name.Defn; defn != nil && defn.Op == OTYPESW {
53 lineno = defn.Left.Lineno
54 Yyerror("%v declared and not used", l.N.Sym)
55 defn.Left.Used = true // suppress repeats
58 Yyerror("%v declared and not used", l.N.Sym)
66 walkstmtlist(Curfn.Nbody)
68 s := fmt.Sprintf("after walk %v", Curfn.Func.Nname.Sym)
69 dumplist(s, Curfn.Nbody)
73 if Debug['W'] != 0 && Curfn.Func.Enter != nil {
74 s := fmt.Sprintf("enter %v", Curfn.Func.Nname.Sym)
75 dumplist(s, Curfn.Func.Enter)
79 func walkstmtlist(l *NodeList) {
80 for ; l != nil; l = l.Next {
85 func samelist(a *NodeList, b *NodeList) bool {
86 for ; a != nil && b != nil; a, b = a.Next, b.Next {
94 func paramoutheap(fn *Node) bool {
95 for l := fn.Func.Dcl; l != nil; l = l.Next {
101 // stop early - parameters are over
111 // adds "adjust" to all the argument locations for the call n.
112 // n must be a defer or go node that has already been walked.
113 func adjustargs(n *Node, adjust int) {
118 for args := callfunc.List; args != nil; args = args.Next {
121 Yyerror("call arg not assignment")
125 // This is a temporary introduced by reorder1.
126 // The real store to the stack appears later in the arg list.
130 if lhs.Op != OINDREG {
131 Yyerror("call argument store does not use OINDREG")
134 // can't really check this in machine-indep code.
135 //if(lhs->val.u.reg != D_SP)
136 // yyerror("call arg assign not indreg(SP)");
137 lhs.Xoffset += int64(adjust)
141 func walkstmt(np **Node) {
146 if n.Dodata == 2 { // don't walk, generated by anylit.
152 walkstmtlist(n.Ninit)
157 Yyerror("%v is not a top level statement", n.Sym)
159 Yyerror("%v is not a top level statement", Oconv(int(n.Op), 0))
184 if n.Typecheck == 0 {
185 Fatalf("missing typecheck: %v", Nconv(n, obj.FmtSign))
191 if (*np).Op == OCOPY && n.Op == OCONVNOP {
192 n.Op = OEMPTY // don't leave plain values as statements.
195 // special case for a receive where we throw away
196 // the value received.
198 if n.Typecheck == 0 {
199 Fatalf("missing typecheck: %v", Nconv(n, obj.FmtSign))
204 walkexpr(&n.Left, &init)
205 n = mkcall1(chanfn("chanrecv1", 2, n.Left.Type), nil, &init, typename(n.Left.Type), n.Left, nodnil())
226 Yyerror("case statement out of place")
236 case OPRINT, OPRINTN:
237 walkprintfunc(&n.Left, &n.Ninit)
240 n.Left = copyany(n.Left, &n.Ninit, true)
243 walkexpr(&n.Left, &n.Ninit)
246 // make room for size & fn arguments.
247 adjustargs(n, 2*Widthptr)
251 walkstmtlist(n.Left.Ninit)
254 walkexpr(&n.Left, &init)
255 addinit(&n.Left, init)
259 walkstmtlist(n.Nbody)
262 walkexpr(&n.Left, &n.Ninit)
263 walkstmtlist(n.Nbody)
264 walkstmtlist(n.Rlist)
268 case OPRINT, OPRINTN:
269 walkprintfunc(&n.Left, &n.Ninit)
272 n.Left = copyany(n.Left, &n.Ninit, true)
275 walkexpr(&n.Left, &n.Ninit)
278 // make room for size & fn arguments.
279 adjustargs(n, 2*Widthptr)
282 walkexprlist(n.List, &n.Ninit)
286 if (Curfn.Type.Outnamed && count(n.List) > 1) || paramoutheap(Curfn) {
287 // assign to the function out parameters,
288 // so that reorder3 can fix up conflicts
292 for ll := Curfn.Func.Dcl; ll != nil; ll = ll.Next {
293 cl = ll.N.Class &^ PHEAP
302 if samelist(rl, n.List) {
303 // special return in disguise
309 if count(n.List) == 1 && count(rl) > 1 {
310 // OAS2FUNC in disguise
313 if f.Op != OCALLFUNC && f.Op != OCALLMETH && f.Op != OCALLINTER {
314 Fatalf("expected return of call, have %v", f)
316 n.List = concat(list1(f), ascompatet(n.Op, rl, &f.Type, 0, &n.Ninit))
320 // move function calls out, to make reorder3's job easier.
321 walkexprlistsafe(n.List, &n.Ninit)
323 ll := ascompatee(n.Op, rl, n.List, &n.Ninit)
324 n.List = reorder3(ll)
328 ll := ascompatte(n.Op, nil, false, Getoutarg(Curfn.Type), n.List, 1, &n.Ninit)
344 Yyerror("fallthrough statement out of place")
349 Fatalf("walkstmt ended up with name: %v", Nconv(n, obj.FmtSign))
355 func isSmallMakeSlice(n *Node) bool {
356 if n.Op != OMAKESLICE {
366 return Smallintconst(l) && Smallintconst(r) && (t.Type.Width == 0 || Mpgetfix(r.Val().U.(*Mpint)) < (1<<16)/t.Type.Width)
369 // walk the whole tree of the body of an
370 // expression or simple statement.
371 // the types expressions are calculated.
372 // compile-time constants are evaluated.
373 // complex side effects like statements are appended to init
374 func walkexprlist(l *NodeList, init **NodeList) {
375 for ; l != nil; l = l.Next {
380 func walkexprlistsafe(l *NodeList, init **NodeList) {
381 for ; l != nil; l = l.Next {
382 l.N = safeexpr(l.N, init)
387 func walkexprlistcheap(l *NodeList, init **NodeList) {
388 for ; l != nil; l = l.Next {
389 l.N = cheapexpr(l.N, init)
394 func walkexpr(np **Node, init **NodeList) {
401 if init == &n.Ninit {
402 // not okay to use n->ninit when walking n,
403 // because we might replace n with some other node
404 // and would lose the init list.
405 Fatalf("walkexpr init == &n->ninit")
409 walkstmtlist(n.Ninit)
410 *init = concat(*init, n.Ninit)
414 // annoying case - not typechecked
416 walkexpr(&n.Left, init)
417 walkexpr(&n.Right, init)
424 Dump("walk-before", n)
427 if n.Typecheck != 1 {
428 Fatalf("missed typecheck: %v\n", Nconv(n, obj.FmtSign))
435 Fatalf("walkexpr: switch 1 unknown op %v", Nconv(n, obj.FmtShort|obj.FmtSign))
452 walkexpr(&n.Left, init)
455 walkexpr(&n.Left, init)
459 walkexpr(&n.Left, init)
463 if n.Op == ODOTPTR && n.Left.Type.Type.Width == 0 {
464 // No actual copy will be generated, so emit an explicit nil check.
465 n.Left = cheapexpr(n.Left, init)
467 checknil(n.Left, init)
470 walkexpr(&n.Left, init)
473 walkexpr(&n.Left, init)
474 walkexpr(&n.Right, init)
477 walkexpr(&n.Left, init)
480 walkexpr(&n.Left, init)
482 // replace len(*[10]int) with 10.
483 // delayed until now to preserve side effects.
490 safeexpr(n.Left, init)
491 Nodconst(n, n.Type, t.Bound)
496 walkexpr(&n.Left, init)
497 walkexpr(&n.Right, init)
499 n.Bounded = bounded(n.Right, 8*t.Width)
500 if Debug['m'] != 0 && n.Etype != 0 && !Isconst(n.Right, CTINT) {
501 Warn("shift bounds check elided")
504 // Use results from call expression as arguments for complex.
515 if n.Op == OCOMPLEX && n.Left == nil && n.Right == nil {
517 n.Right = n.List.Next.N
520 walkexpr(&n.Left, init)
521 walkexpr(&n.Right, init)
524 walkexpr(&n.Left, init)
525 walkexpr(&n.Right, init)
529 walkexpr(&n.Left, init)
530 walkexpr(&n.Right, init)
532 // Disable safemode while compiling this code: the code we
533 // generate internally can refer to unsafe.Pointer.
534 // In this case it can happen if we need to generate an ==
535 // for a struct containing a reflect.Value, which itself has
536 // an unexported field of type unsafe.Pointer.
537 old_safemode := safemode
540 walkcompare(&n, init)
541 safemode = old_safemode
544 walkexpr(&n.Left, init)
546 // cannot put side effects from n.Right on init,
547 // because they cannot run before n.Left is checked.
548 // save elsewhere and store on the eventual n.Right.
551 walkexpr(&n.Right, &ll)
552 addinit(&n.Right, ll)
554 case OPRINT, OPRINTN:
555 walkexprlist(n.List, init)
556 n = walkprint(n, init)
559 n = mkcall("gopanic", nil, init, n.Left)
562 n = mkcall("gorecover", n.Type, init, Nod(OADDR, nodfp, nil))
567 case OCLOSUREVAR, OCFUNC:
571 if n.Class&PHEAP == 0 && n.Class != PPARAMREF {
577 if n.List != nil && n.List.N.Op == OAS {
580 walkexpr(&n.Left, init)
581 walkexprlist(n.List, init)
582 ll := ascompatte(n.Op, n, n.Isddd, getinarg(t), n.List, 0, init)
583 n.List = reorder1(ll)
586 if n.Left.Op == OCLOSURE {
587 // Transform direct call of a closure to call of a normal function.
588 // transformclosure already did all preparation work.
590 // Prepend captured variables to argument list.
591 n.List = concat(n.Left.Func.Enter, n.List)
593 n.Left.Func.Enter = nil
595 // Replace OCLOSURE with ONAME/PFUNC.
596 n.Left = n.Left.Func.Closure.Func.Nname
598 // Update type of OCALLFUNC node.
599 // Output arguments had not changed, but their offsets could.
600 if n.Left.Type.Outtuple == 1 {
601 t := getoutargx(n.Left.Type).Type
602 if t.Etype == TFIELD {
607 n.Type = getoutargx(n.Left.Type)
612 if n.List != nil && n.List.N.Op == OAS {
616 walkexpr(&n.Left, init)
617 walkexprlist(n.List, init)
619 if n.Left.Op == ONAME && n.Left.Sym.Name == "Sqrt" && n.Left.Sym.Pkg.Path == "math" {
620 switch Thearch.Thechar {
629 ll := ascompatte(n.Op, n, n.Isddd, getinarg(t), n.List, 0, init)
630 n.List = reorder1(ll)
634 if n.List != nil && n.List.N.Op == OAS {
637 walkexpr(&n.Left, init)
638 walkexprlist(n.List, init)
639 ll := ascompatte(n.Op, n, false, getthis(t), list1(n.Left.Left), 0, init)
640 lr := ascompatte(n.Op, n, n.Isddd, getinarg(t), n.List, 0, init)
644 n.List = reorder1(ll)
647 *init = concat(*init, n.Ninit)
650 walkexpr(&n.Left, init)
651 n.Left = safeexpr(n.Left, init)
657 if n.Right == nil || iszero(n.Right) && !instrumenting {
663 walkexpr(&n.Right, init)
666 // TODO(rsc): The Isfat is for consistency with componentgen and orderexpr.
667 // It needs to be removed in all three places.
668 // That would allow inlining x.(struct{*int}) the same as x.(*int).
669 if isdirectiface(n.Right.Type) && !Isfat(n.Right.Type) && !instrumenting {
670 // handled directly during cgen
671 walkexpr(&n.Right, init)
675 // x = i.(T); n.Left is x, n.Right.Left is i.
676 // orderstmt made sure x is addressable.
677 walkexpr(&n.Right.Left, init)
679 n1 := Nod(OADDR, n.Left, nil)
680 r := n.Right // i.(T)
682 if Debug_typeassert > 0 {
683 Warn("type assertion not inlined")
686 buf := "assert" + type2IET(r.Left.Type) + "2" + type2IET(r.Type)
687 fn := syslook(buf, 1)
688 substArgTypes(fn, r.Left.Type, r.Type)
690 n = mkcall1(fn, nil, init, typename(r.Type), r.Left, n1)
695 // x = <-c; n.Left is x, n.Right.Left is c.
696 // orderstmt made sure x is addressable.
697 walkexpr(&n.Right.Left, init)
699 n1 := Nod(OADDR, n.Left, nil)
700 r := n.Right.Left // the channel
701 n = mkcall1(chanfn("chanrecv1", 2, r.Type), nil, init, typename(r.Type), r, n1)
709 r = appendslice(r, init) // also works for append(slice, string).
711 r = walkappend(r, init, n)
715 // Left in place for back end.
716 // Do not add a new write barrier.
719 // Otherwise, lowered for race detector.
720 // Treat as ordinary assignment.
723 if n.Left != nil && n.Right != nil {
724 r := convas(Nod(OAS, n.Left, n.Right), init)
727 n = applywritebarrier(n, init)
731 *init = concat(*init, n.Ninit)
733 walkexprlistsafe(n.List, init)
734 walkexprlistsafe(n.Rlist, init)
735 ll := ascompatee(OAS, n.List, n.Rlist, init)
737 for lr := ll; lr != nil; lr = lr.Next {
738 lr.N = applywritebarrier(lr.N, init)
744 *init = concat(*init, n.Ninit)
748 walkexprlistsafe(n.List, init)
751 ll := ascompatet(n.Op, n.List, &r.Type, 0, init)
752 for lr := ll; lr != nil; lr = lr.Next {
753 lr.N = applywritebarrier(lr.N, init)
755 n = liststmt(concat(list1(r), ll))
758 // orderstmt made sure x is addressable.
760 *init = concat(*init, n.Ninit)
764 walkexprlistsafe(n.List, init)
765 walkexpr(&r.Left, init)
767 if isblank(n.List.N) {
770 n1 = Nod(OADDR, n.List.N, nil)
772 n1.Etype = 1 // addr does not escape
773 fn := chanfn("chanrecv2", 2, r.Left.Type)
774 r = mkcall1(fn, n.List.Next.N.Type, init, typename(r.Left.Type), r.Left, n1)
775 n = Nod(OAS, n.List.Next.N, r)
780 *init = concat(*init, n.Ninit)
784 walkexprlistsafe(n.List, init)
785 walkexpr(&r.Left, init)
786 walkexpr(&r.Right, init)
789 if t.Type.Width <= 128 { // Check ../../runtime/hashmap.go:maxValueSize before changing.
790 switch Simsimtype(t.Down) {
791 case TINT32, TUINT32:
792 p = "mapaccess2_fast32"
794 case TINT64, TUINT64:
795 p = "mapaccess2_fast64"
798 p = "mapaccess2_faststr"
804 // fast versions take key by value
807 // standard version takes key by reference
808 // orderexpr made sure key is addressable.
809 key = Nod(OADDR, r.Right, nil)
817 // var,b = mapaccess2*(t, m, i)
822 r = mkcall1(fn, getoutargx(fn.Type), init, typename(t), r.Left, key)
824 // mapaccess2* returns a typed bool, but due to spec changes,
825 // the boolean result of i.(T) is now untyped so we make it the
826 // same type as the variable on the lhs.
827 if !isblank(n.List.Next.N) {
828 r.Type.Type.Down.Type = n.List.Next.N.Type
833 // don't generate a = *var if a is _
835 var_ := temp(Ptrto(t.Type))
839 *init = list(*init, n)
840 n = Nod(OAS, a, Nod(OIND, var_, nil))
846 // TODO: ptr is always non-nil, so disable nil check for this OIND op.
849 *init = concat(*init, n.Ninit)
853 walkexpr(&map_, init)
856 // orderstmt made sure key is addressable.
857 key = Nod(OADDR, key, nil)
860 n = mkcall1(mapfndel("mapdelete", t), nil, init, typename(t), map_, key)
863 e := n.Rlist.N // i.(T)
864 // TODO(rsc): The Isfat is for consistency with componentgen and orderexpr.
865 // It needs to be removed in all three places.
866 // That would allow inlining x.(struct{*int}) the same as x.(*int).
867 if isdirectiface(e.Type) && !Isfat(e.Type) && !instrumenting {
868 // handled directly during gen.
869 walkexprlistsafe(n.List, init)
870 walkexpr(&e.Left, init)
875 // orderstmt made sure a is addressable.
876 *init = concat(*init, n.Ninit)
879 walkexprlistsafe(n.List, init)
880 walkexpr(&e.Left, init)
884 oktype := Types[TBOOL]
890 fromKind := type2IET(from.Type)
891 toKind := type2IET(t)
893 // Avoid runtime calls in a few cases of the form _, ok := i.(T).
894 // This is faster and shorter and allows the corresponding assertX2X2
895 // routines to skip nil checks on their last argument.
896 if isblank(n.List.N) {
899 case fromKind == "E" && toKind == "T":
900 tab := Nod(OITAB, from, nil) // type:eface::tab:iface
901 typ := Nod(OCONVNOP, typename(t), nil)
902 typ.Type = Ptrto(Types[TUINTPTR])
903 fast = Nod(OEQ, tab, typ)
904 case fromKind == "I" && toKind == "E",
905 fromKind == "E" && toKind == "E":
906 tab := Nod(OITAB, from, nil)
907 fast = Nod(ONE, nodnil(), tab)
910 if Debug_typeassert > 0 {
911 Warn("type assertion (ok only) inlined")
913 n = Nod(OAS, ok, fast)
919 var resptr *Node // &res
920 if isblank(n.List.N) {
923 resptr = Nod(OADDR, n.List.N, nil)
925 resptr.Etype = 1 // addr does not escape
927 if Debug_typeassert > 0 {
928 Warn("type assertion not inlined")
930 buf := "assert" + fromKind + "2" + toKind + "2"
931 fn := syslook(buf, 1)
932 substArgTypes(fn, from.Type, t)
933 call := mkcall1(fn, oktype, init, typename(t), from, resptr)
934 n = Nod(OAS, ok, call)
937 case ODOTTYPE, ODOTTYPE2:
938 if !isdirectiface(n.Type) || Isfat(n.Type) {
939 Fatalf("walkexpr ODOTTYPE") // should see inside OAS only
941 walkexpr(&n.Left, init)
944 walkexpr(&n.Left, init)
946 // Optimize convT2E as a two-word copy when T is pointer-shaped.
947 if isnilinter(n.Type) && isdirectiface(n.Left.Type) {
948 l := Nod(OEFACE, typename(n.Left.Type), n.Left)
950 l.Typecheck = n.Typecheck
955 // Build name of function: convI2E etc.
956 // Not all names are possible
957 // (e.g., we'll never generate convE2E or convE2I).
958 buf := "conv" + type2IET(n.Left.Type) + "2" + type2IET(n.Type)
959 fn := syslook(buf, 1)
961 if !Isinter(n.Left.Type) {
962 ll = list(ll, typename(n.Left.Type))
964 if !isnilinter(n.Type) {
965 ll = list(ll, typename(n.Type))
967 if !Isinter(n.Left.Type) && !isnilinter(n.Type) {
968 sym := Pkglookup(Tconv(n.Left.Type, obj.FmtLeft)+"."+Tconv(n.Type, obj.FmtLeft), itabpkg)
970 l := Nod(ONAME, nil, nil)
972 l.Type = Ptrto(Types[TUINT8])
977 ggloblsym(sym, int32(Widthptr), obj.DUPOK|obj.NOPTR)
980 l := Nod(OADDR, sym.Def, nil)
984 if isdirectiface(n.Left.Type) {
985 // For pointer types, we can make a special form of optimization
987 // These statements are put onto the expression init list:
988 // Itab *tab = atomicloadtype(&cache);
990 // tab = typ2Itab(type, itype, &cache);
992 // The CONVIFACE expression is replaced with this:
994 l := temp(Ptrto(Types[TUINT8]))
996 n1 := Nod(OAS, l, sym.Def)
998 *init = list(*init, n1)
1000 fn := syslook("typ2Itab", 1)
1001 n1 = Nod(OCALL, fn, nil)
1006 n2 := Nod(OIF, nil, nil)
1007 n2.Left = Nod(OEQ, l, nodnil())
1008 n2.Nbody = list1(Nod(OAS, l, n1))
1010 typecheck(&n2, Etop)
1011 *init = list(*init, n2)
1013 l = Nod(OEFACE, l, n.Left)
1014 l.Typecheck = n.Typecheck
1021 if Isinter(n.Left.Type) {
1022 ll = list(ll, n.Left)
1024 // regular types are passed by reference to avoid C vararg calls
1025 // orderexpr arranged for n.Left to be a temporary for all
1026 // the conversions it could see. comparison of an interface
1027 // with a non-interface, especially in a switch on interface value
1028 // with non-interface cases, is not visible to orderstmt, so we
1029 // have to fall back on allocating a temp here.
1030 if islvalue(n.Left) {
1031 ll = list(ll, Nod(OADDR, n.Left, nil))
1033 ll = list(ll, Nod(OADDR, copyexpr(n.Left, n.Left.Type, init), nil))
1035 dowidth(n.Left.Type)
1037 if n.Esc == EscNone && n.Left.Type.Width <= 1024 {
1038 // Allocate stack buffer for value stored in interface.
1039 r = temp(n.Left.Type)
1040 r = Nod(OAS, r, nil) // zero temp
1042 *init = list(*init, r)
1043 r = Nod(OADDR, r.Left, nil)
1049 if !Isinter(n.Left.Type) {
1050 substArgTypes(fn, n.Left.Type, n.Left.Type, n.Type)
1052 substArgTypes(fn, n.Left.Type, n.Type)
1055 n = Nod(OCALL, fn, nil)
1060 case OCONV, OCONVNOP:
1061 if Thearch.Thechar == '5' {
1062 if Isfloat[n.Left.Type.Etype] {
1063 if n.Type.Etype == TINT64 {
1064 n = mkcall("float64toint64", n.Type, init, conv(n.Left, Types[TFLOAT64]))
1068 if n.Type.Etype == TUINT64 {
1069 n = mkcall("float64touint64", n.Type, init, conv(n.Left, Types[TFLOAT64]))
1074 if Isfloat[n.Type.Etype] {
1075 if n.Left.Type.Etype == TINT64 {
1076 n = mkcall("int64tofloat64", n.Type, init, conv(n.Left, Types[TINT64]))
1080 if n.Left.Type.Etype == TUINT64 {
1081 n = mkcall("uint64tofloat64", n.Type, init, conv(n.Left, Types[TUINT64]))
1087 walkexpr(&n.Left, init)
1090 walkexpr(&n.Left, init)
1092 n.Right = Nod(OCOM, n.Right, nil)
1093 typecheck(&n.Right, Erv)
1094 walkexpr(&n.Right, init)
1097 walkexpr(&n.Left, init)
1098 walkexpr(&n.Right, init)
1102 walkexpr(&n.Left, init)
1103 walkexpr(&n.Right, init)
1105 // rewrite complex div into function call.
1106 et := n.Left.Type.Etype
1108 if Iscomplex[et] && n.Op == ODIV {
1110 n = mkcall("complex128div", Types[TCOMPLEX128], init, conv(n.Left, Types[TCOMPLEX128]), conv(n.Right, Types[TCOMPLEX128]))
1115 // Nothing to do for float divisions.
1120 // Try rewriting as shifts or magic multiplies.
1123 // rewrite 64-bit div and mod into function calls
1124 // on 32-bit architectures.
1127 if Widthreg >= 8 || (et != TUINT64 && et != TINT64) {
1141 n = mkcall(fn, n.Type, init, conv(n.Left, Types[et]), conv(n.Right, Types[et]))
1145 walkexpr(&n.Left, init)
1147 // save the original node for bounds checking elision.
1148 // If it was a ODIV/OMOD walk might rewrite it.
1151 walkexpr(&n.Right, init)
1153 // if range of type cannot exceed static array bound,
1154 // disable bounds check.
1159 if t != nil && Isptr[t.Etype] {
1162 if Isfixedarray(t) {
1163 n.Bounded = bounded(r, t.Bound)
1164 if Debug['m'] != 0 && n.Bounded && !Isconst(n.Right, CTINT) {
1165 Warn("index bounds check elided")
1167 if Smallintconst(n.Right) && !n.Bounded {
1168 Yyerror("index out of bounds")
1170 } else if Isconst(n.Left, CTSTR) {
1171 n.Bounded = bounded(r, int64(len(n.Left.Val().U.(string))))
1172 if Debug['m'] != 0 && n.Bounded && !Isconst(n.Right, CTINT) {
1173 Warn("index bounds check elided")
1175 if Smallintconst(n.Right) {
1177 Yyerror("index out of bounds")
1179 // replace "abc"[1] with 'b'.
1180 // delayed until now because "abc"[1] is not
1181 // an ideal constant.
1182 v := Mpgetfix(n.Right.Val().U.(*Mpint))
1184 Nodconst(n, n.Type, int64(n.Left.Val().U.(string)[v]))
1190 if Isconst(n.Right, CTINT) {
1191 if Mpcmpfixfix(n.Right.Val().U.(*Mpint), &mpzero) < 0 || Mpcmpfixfix(n.Right.Val().U.(*Mpint), Maxintval[TINT]) > 0 {
1192 Yyerror("index out of bounds")
1200 walkexpr(&n.Left, init)
1201 walkexpr(&n.Right, init)
1205 if t.Type.Width <= 128 { // Check ../../runtime/hashmap.go:maxValueSize before changing.
1206 switch Simsimtype(t.Down) {
1207 case TINT32, TUINT32:
1208 p = "mapaccess1_fast32"
1210 case TINT64, TUINT64:
1211 p = "mapaccess1_fast64"
1214 p = "mapaccess1_faststr"
1220 // fast versions take key by value
1223 // standard version takes key by reference.
1224 // orderexpr made sure key is addressable.
1225 key = Nod(OADDR, n.Right, nil)
1230 n = mkcall1(mapfn(p, t), Ptrto(t.Type), init, typename(t), n.Left, key)
1231 n = Nod(OIND, n, nil)
1236 Fatalf("walkexpr ORECV") // should see inside OAS only
1238 case OSLICE, OSLICEARR, OSLICESTR:
1239 walkexpr(&n.Left, init)
1240 walkexpr(&n.Right.Left, init)
1241 if n.Right.Left != nil && iszero(n.Right.Left) {
1242 // Reduce x[0:j] to x[:j].
1245 walkexpr(&n.Right.Right, init)
1248 case OSLICE3, OSLICE3ARR:
1249 walkexpr(&n.Left, init)
1250 walkexpr(&n.Right.Left, init)
1251 if n.Right.Left != nil && iszero(n.Right.Left) {
1252 // Reduce x[0:j:k] to x[:j:k].
1255 walkexpr(&n.Right.Right.Left, init)
1256 walkexpr(&n.Right.Right.Right, init)
1258 r := n.Right.Right.Right
1259 if r != nil && r.Op == OCAP && samesafeexpr(n.Left, r.Left) {
1260 // Reduce x[i:j:cap(x)] to x[i:j].
1261 n.Right.Right = n.Right.Right.Left
1262 if n.Op == OSLICE3 {
1271 walkexpr(&n.Left, init)
1274 if n.Esc == EscNone {
1275 if n.Type.Type.Width >= 1<<16 {
1276 Fatalf("large ONEW with EscNone: %v", n)
1278 r := temp(n.Type.Type)
1279 r = Nod(OAS, r, nil) // zero temp
1281 *init = list(*init, r)
1282 r = Nod(OADDR, r.Left, nil)
1286 n = callnew(n.Type.Type)
1289 // If one argument to the comparison is an empty string,
1290 // comparing the lengths instead will yield the same result
1291 // without the function call.
1293 if (Isconst(n.Left, CTSTR) && len(n.Left.Val().U.(string)) == 0) || (Isconst(n.Right, CTSTR) && len(n.Right.Val().U.(string)) == 0) {
1294 // TODO(marvin): Fix Node.EType type union.
1295 r := Nod(Op(n.Etype), Nod(OLEN, n.Left, nil), Nod(OLEN, n.Right, nil))
1303 // s + "badgerbadgerbadger" == "badgerbadgerbadger"
1304 if (Op(n.Etype) == OEQ || Op(n.Etype) == ONE) && Isconst(n.Right, CTSTR) && n.Left.Op == OADDSTR && count(n.Left.List) == 2 && Isconst(n.Left.List.Next.N, CTSTR) && strlit(n.Right) == strlit(n.Left.List.Next.N) {
1305 // TODO(marvin): Fix Node.EType type union.
1306 r := Nod(Op(n.Etype), Nod(OLEN, n.Left.List.N, nil), Nodintconst(0))
1315 // TODO(marvin): Fix Node.EType type union.
1316 if Op(n.Etype) == OEQ || Op(n.Etype) == ONE {
1317 // prepare for rewrite below
1318 n.Left = cheapexpr(n.Left, init)
1320 n.Right = cheapexpr(n.Right, init)
1322 r = mkcall("eqstring", Types[TBOOL], init, conv(n.Left, Types[TSTRING]), conv(n.Right, Types[TSTRING]))
1324 // quick check of len before full compare for == or !=
1325 // eqstring assumes that the lengths are equal
1326 // TODO(marvin): Fix Node.EType type union.
1327 if Op(n.Etype) == OEQ {
1328 // len(left) == len(right) && eqstring(left, right)
1329 r = Nod(OANDAND, Nod(OEQ, Nod(OLEN, n.Left, nil), Nod(OLEN, n.Right, nil)), r)
1331 // len(left) != len(right) || !eqstring(left, right)
1332 r = Nod(ONOT, r, nil)
1334 r = Nod(OOROR, Nod(ONE, Nod(OLEN, n.Left, nil), Nod(OLEN, n.Right, nil)), r)
1340 // sys_cmpstring(s1, s2) :: 0
1341 r = mkcall("cmpstring", Types[TINT], init, conv(n.Left, Types[TSTRING]), conv(n.Right, Types[TSTRING]))
1343 // TODO(marvin): Fix Node.EType type union.
1344 r = Nod(Op(n.Etype), r, Nodintconst(0))
1348 if n.Type.Etype != TBOOL {
1349 Fatalf("cmp %v", n.Type)
1358 // order should make sure we only see OAS(node, OAPPEND), which we handle above.
1359 Fatalf("append outside assignment")
1362 n = copyany(n, init, instrumenting)
1364 // cannot use chanfn - closechan takes any, not chan any
1366 fn := syslook("closechan", 1)
1368 substArgTypes(fn, n.Left.Type)
1369 n = mkcall1(fn, nil, init, n.Left)
1372 n = mkcall1(chanfn("makechan", 1, n.Type), n.Type, init, typename(n.Type), conv(n.Left, Types[TINT64]))
1377 fn := syslook("makemap", 1)
1379 a := nodnil() // hmap buffer
1380 r := nodnil() // bucket buffer
1381 if n.Esc == EscNone {
1382 // Allocate hmap buffer on stack.
1383 var_ := temp(hmap(t))
1385 a = Nod(OAS, var_, nil) // zero temp
1387 *init = list(*init, a)
1388 a = Nod(OADDR, var_, nil)
1390 // Allocate one bucket on stack.
1391 // Maximum key/value size is 128 bytes, larger objects
1392 // are stored with an indirection. So max bucket size is 2048+eps.
1393 var_ = temp(mapbucket(t))
1395 r = Nod(OAS, var_, nil) // zero temp
1397 *init = list(*init, r)
1398 r = Nod(OADDR, var_, nil)
1401 substArgTypes(fn, hmap(t), mapbucket(t), t.Down, t.Type)
1402 n = mkcall1(fn, n.Type, init, typename(n.Type), conv(n.Left, Types[TINT64]), a, r)
1408 r = safeexpr(l, init)
1412 if n.Esc == EscNone {
1413 if !isSmallMakeSlice(n) {
1414 Fatalf("non-small OMAKESLICE with EscNone: %v", n)
1418 t = aindex(r, t.Type) // [r]T
1420 a := Nod(OAS, var_, nil) // zero temp
1422 *init = list(*init, a)
1423 r := Nod(OSLICE, var_, Nod(OKEY, nil, l)) // arr[:l]
1424 r = conv(r, n.Type) // in case n.Type is named.
1429 // makeslice(t *Type, nel int64, max int64) (ary []any)
1430 fn := syslook("makeslice", 1)
1432 substArgTypes(fn, t.Type) // any-1
1433 n = mkcall1(fn, n.Type, init, typename(n.Type), conv(l, Types[TINT64]), conv(r, Types[TINT64]))
1438 if n.Esc == EscNone {
1439 t := aindex(Nodintconst(4), Types[TUINT8])
1441 a = Nod(OADDR, var_, nil)
1444 // intstring(*[4]byte, rune)
1445 n = mkcall("intstring", n.Type, init, a, conv(n.Left, Types[TINT64]))
1449 if n.Esc == EscNone {
1450 // Create temporary buffer for string on stack.
1451 t := aindex(Nodintconst(tmpstringbufsize), Types[TUINT8])
1453 a = Nod(OADDR, temp(t), nil)
1456 // slicebytetostring(*[32]byte, []byte) string;
1457 n = mkcall("slicebytetostring", n.Type, init, a, n.Left)
1459 // slicebytetostringtmp([]byte) string;
1460 case OARRAYBYTESTRTMP:
1461 n = mkcall("slicebytetostringtmp", n.Type, init, n.Left)
1463 // slicerunetostring(*[32]byte, []rune) string;
1467 if n.Esc == EscNone {
1468 // Create temporary buffer for string on stack.
1469 t := aindex(Nodintconst(tmpstringbufsize), Types[TUINT8])
1471 a = Nod(OADDR, temp(t), nil)
1474 n = mkcall("slicerunetostring", n.Type, init, a, n.Left)
1476 // stringtoslicebyte(*32[byte], string) []byte;
1480 if n.Esc == EscNone {
1481 // Create temporary buffer for slice on stack.
1482 t := aindex(Nodintconst(tmpstringbufsize), Types[TUINT8])
1484 a = Nod(OADDR, temp(t), nil)
1487 n = mkcall("stringtoslicebyte", n.Type, init, a, conv(n.Left, Types[TSTRING]))
1489 // stringtoslicebytetmp(string) []byte;
1490 case OSTRARRAYBYTETMP:
1491 n = mkcall("stringtoslicebytetmp", n.Type, init, conv(n.Left, Types[TSTRING]))
1493 // stringtoslicerune(*[32]rune, string) []rune
1497 if n.Esc == EscNone {
1498 // Create temporary buffer for slice on stack.
1499 t := aindex(Nodintconst(tmpstringbufsize), Types[TINT32])
1501 a = Nod(OADDR, temp(t), nil)
1504 n = mkcall("stringtoslicerune", n.Type, init, a, n.Left)
1506 // ifaceeq(i1 any-1, i2 any-2) (ret bool);
1508 if !Eqtype(n.Left.Type, n.Right.Type) {
1509 Fatalf("ifaceeq %v %v %v", Oconv(int(n.Op), 0), n.Left.Type, n.Right.Type)
1512 if isnilinter(n.Left.Type) {
1513 fn = syslook("efaceeq", 1)
1515 fn = syslook("ifaceeq", 1)
1518 n.Right = cheapexpr(n.Right, init)
1519 n.Left = cheapexpr(n.Left, init)
1520 substArgTypes(fn, n.Right.Type, n.Left.Type)
1521 r := mkcall1(fn, n.Type, init, n.Left, n.Right)
1522 // TODO(marvin): Fix Node.EType type union.
1523 if Op(n.Etype) == ONE {
1524 r = Nod(ONOT, r, nil)
1527 // check itable/type before full compare.
1528 // TODO(marvin): Fix Node.EType type union.
1529 if Op(n.Etype) == OEQ {
1530 r = Nod(OANDAND, Nod(OEQ, Nod(OITAB, n.Left, nil), Nod(OITAB, n.Right, nil)), r)
1532 r = Nod(OOROR, Nod(ONE, Nod(OITAB, n.Left, nil), Nod(OITAB, n.Right, nil)), r)
1539 case OARRAYLIT, OMAPLIT, OSTRUCTLIT, OPTRLIT:
1540 var_ := temp(n.Type)
1541 anylit(0, n, var_, init)
1546 n1 = assignconv(n1, n.Left.Type.Type, "chan send")
1548 n1 = Nod(OADDR, n1, nil)
1549 n = mkcall1(chanfn("chansend1", 2, n.Left.Type), nil, init, typename(n.Left.Type), n.Left, n1)
1552 n = walkclosure(n, init)
1555 n = walkpartialcall(n, init)
1558 // Expressions that are constant at run time but not
1559 // considered const by the language spec are not turned into
1560 // constants until walk. For example, if n is y%1 == 0, the
1561 // walk of y%1 may have replaced it by 0.
1562 // Check whether n with its updated args is itself now a constant.
1567 if n.Op == OLITERAL {
1573 if Debug['w'] != 0 && n != nil {
1581 func reduceSlice(n *Node) *Node {
1583 if r != nil && r.Op == OLEN && samesafeexpr(n.Left, r.Left) {
1584 // Reduce x[i:len(x)] to x[i:].
1587 if (n.Op == OSLICE || n.Op == OSLICESTR) && n.Right.Left == nil && n.Right.Right == nil {
1588 // Reduce x[:] to x.
1589 if Debug_slice > 0 {
1590 Warn("slice: omit slice operation")
1597 func ascompatee1(op Op, l *Node, r *Node, init **NodeList) *Node {
1598 // convas will turn map assigns into function calls,
1599 // making it impossible for reorder3 to work.
1602 if l.Op == OINDEXMAP {
1606 return convas(n, init)
1609 func ascompatee(op Op, nl *NodeList, nr *NodeList, init **NodeList) *NodeList {
1610 // check assign expression list to
1611 // a expression list. called in
1612 // expr-list = expr-list
1614 // ensure order of evaluation for function calls
1615 for ll := nl; ll != nil; ll = ll.Next {
1616 ll.N = safeexpr(ll.N, init)
1618 for lr := nr; lr != nil; lr = lr.Next {
1619 lr.N = safeexpr(lr.N, init)
1625 for ; ll != nil && lr != nil; ll, lr = ll.Next, lr.Next {
1626 // Do not generate 'x = x' during return. See issue 4014.
1627 if op == ORETURN && ll.N == lr.N {
1630 nn = list(nn, ascompatee1(op, ll.N, lr.N, init))
1633 // cannot happen: caller checked that lists had same length
1634 if ll != nil || lr != nil {
1635 Yyerror("error in shape across %v %v %v / %d %d [%s]", Hconv(nl, obj.FmtSign), Oconv(int(op), 0), Hconv(nr, obj.FmtSign), count(nl), count(nr), Curfn.Func.Nname.Sym.Name)
1640 // l is an lv and rt is the type of an rv
1641 // return 1 if this implies a function call
1642 // evaluating the lv or a function call
1643 // in the conversion of the types
1644 func fncall(l *Node, rt *Type) bool {
1645 if l.Ullman >= UINF || l.Op == OINDEXMAP {
1649 if needwritebarrier(l, &r) {
1652 if Eqtype(l.Type, rt) {
1658 func ascompatet(op Op, nl *NodeList, nr **Type, fp int, init **NodeList) *NodeList {
1665 // check assign type list to
1666 // a expression list. called in
1667 // expr-list = func()
1668 r := Structfirst(&saver, nr)
1673 for ll = nl; ll != nil; ll = ll.Next {
1679 r = structnext(&saver)
1683 // any lv that causes a fn call must be
1684 // deferred until all the return arguments
1685 // have been pulled from the output arguments
1686 if fncall(l, r.Type) {
1688 typecheck(&tmp, Erv)
1689 a = Nod(OAS, l, tmp)
1695 a = Nod(OAS, l, nodarg(r, fp))
1698 if a.Ullman >= UINF {
1699 Dump("ascompatet ucount", a)
1704 r = structnext(&saver)
1707 if ll != nil || r != nil {
1708 Yyerror("ascompatet: assignment count mismatch: %d = %d", count(nl), structcount(*nr))
1712 Fatalf("ascompatet: too many function calls evaluating parameters")
1714 return concat(nn, mm)
1717 // package all the arguments that match a ... T parameter into a []T.
1718 func mkdotargslice(lr0 *NodeList, nn *NodeList, l *Type, fp int, init **NodeList, ddd *Node) *NodeList {
1719 esc := uint16(EscUnknown)
1724 tslice := typ(TARRAY)
1725 tslice.Type = l.Type.Type
1729 if count(lr0) == 0 {
1733 n = Nod(OCOMPLIT, nil, typenod(tslice))
1734 if ddd != nil && prealloc[ddd] != nil {
1735 prealloc[n] = prealloc[ddd] // temporary to use
1741 Fatalf("mkdotargslice: typecheck failed")
1746 a := Nod(OAS, nodarg(l, fp), n)
1747 nn = list(nn, convas(a, init))
1751 // helpers for shape errors
1752 func dumptypes(nl **Type, what string) string {
1758 for l := Structfirst(&savel, nl); l != nil; l = structnext(&savel) {
1768 fmt_ += fmt.Sprintf("[no arguments %s]", what)
1773 func dumpnodetypes(l *NodeList, what string) string {
1779 for ; l != nil; l = l.Next {
1786 fmt_ += Tconv(r.Type, 0)
1790 fmt_ += fmt.Sprintf("[no arguments %s]", what)
1795 // check assign expression list to
1796 // a type list. called in
1799 func ascompatte(op Op, call *Node, isddd bool, nl **Type, lr *NodeList, fp int, init **NodeList) *NodeList {
1803 l := Structfirst(&savel, nl)
1810 // f(g()) where g has multiple return values
1815 if r != nil && lr.Next == nil && r.Type.Etype == TSTRUCT && r.Type.Funarg {
1816 // optimization - can do block copy
1817 if eqtypenoname(r.Type, *nl) {
1818 a := nodarg(*nl, fp)
1819 r = Nod(OCONVNOP, r, nil)
1821 nn = list1(convas(Nod(OAS, a, r), init))
1825 // conversions involved.
1826 // copy into temporaries.
1829 for l := Structfirst(&savel, &r.Type); l != nil; l = structnext(&savel) {
1831 alist = list(alist, a)
1834 a = Nod(OAS2, nil, nil)
1839 *init = list(*init, a)
1842 l = Structfirst(&savel, nl)
1846 if l != nil && l.Isddd {
1847 // the ddd parameter must be last
1848 ll = structnext(&savel)
1851 Yyerror("... must be last argument")
1855 // only if we are assigning a single ddd
1856 // argument to a ddd parameter then it is
1857 // passed thru unencapsulated
1858 if r != nil && lr.Next == nil && isddd && Eqtype(l.Type, r.Type) {
1859 a = Nod(OAS, nodarg(l, fp), r)
1865 // normal case -- make a slice of all
1866 // remaining arguments and pass it to
1867 // the ddd parameter.
1868 nn = mkdotargslice(lr, nn, l, fp, init, call.Right)
1873 if l == nil || r == nil {
1874 if l != nil || r != nil {
1875 l1 = dumptypes(nl, "expected")
1876 l2 = dumpnodetypes(lr0, "given")
1878 Yyerror("not enough arguments to %v\n%s\n%s", Oconv(int(op), 0), l1, l2)
1880 Yyerror("too many arguments to %v\n%s\n%s", Oconv(int(op), 0), l1, l2)
1887 a = Nod(OAS, nodarg(l, fp), r)
1891 l = structnext(&savel)
1900 for lr = nn; lr != nil; lr = lr.Next {
1906 // generate code for print
1907 func walkprint(nn *Node, init **NodeList) *Node {
1919 // Hoist all the argument evaluation up before the lock.
1920 walkexprlistcheap(all, init)
1922 calls = list(calls, mkcall("printlock", nil, init))
1924 for l := all; l != nil; l = l.Next {
1926 calls = list(calls, mkcall("printsp", nil, init))
1929 notfirst = op == OPRINTN
1932 if n.Op == OLITERAL {
1933 switch n.Val().Ctype() {
1935 defaultlit(&n, runetype)
1938 defaultlit(&n, Types[TINT64])
1941 defaultlit(&n, Types[TFLOAT64])
1945 if n.Op != OLITERAL && n.Type != nil && n.Type.Etype == TIDEAL {
1946 defaultlit(&n, Types[TINT64])
1950 if n.Type == nil || n.Type.Etype == TFORW {
1956 if Isinter(n.Type) {
1957 if isnilinter(n.Type) {
1958 on = syslook("printeface", 1)
1960 on = syslook("printiface", 1)
1962 substArgTypes(on, n.Type) // any-1
1963 } else if Isptr[et] || et == TCHAN || et == TMAP || et == TFUNC || et == TUNSAFEPTR {
1964 on = syslook("printpointer", 1)
1965 substArgTypes(on, n.Type) // any-1
1966 } else if Isslice(n.Type) {
1967 on = syslook("printslice", 1)
1968 substArgTypes(on, n.Type) // any-1
1969 } else if Isint[et] {
1971 if (t.Sym.Pkg == Runtimepkg || compiling_runtime != 0) && t.Sym.Name == "hex" {
1972 on = syslook("printhex", 0)
1974 on = syslook("printuint", 0)
1977 on = syslook("printint", 0)
1979 } else if Isfloat[et] {
1980 on = syslook("printfloat", 0)
1981 } else if Iscomplex[et] {
1982 on = syslook("printcomplex", 0)
1983 } else if et == TBOOL {
1984 on = syslook("printbool", 0)
1985 } else if et == TSTRING {
1986 on = syslook("printstring", 0)
1988 badtype(OPRINT, n.Type, nil)
1992 t = *getinarg(on.Type)
2000 if !Eqtype(t, n.Type) {
2001 n = Nod(OCONV, n, nil)
2005 r = Nod(OCALL, on, nil)
2007 calls = list(calls, r)
2011 calls = list(calls, mkcall("printnl", nil, nil))
2014 calls = list(calls, mkcall("printunlock", nil, init))
2016 typechecklist(calls, Etop)
2017 walkexprlist(calls, init)
2019 r = Nod(OEMPTY, nil, nil)
2026 func callnew(t *Type) *Node {
2028 fn := syslook("newobject", 1)
2029 substArgTypes(fn, t)
2030 return mkcall1(fn, Ptrto(t), nil, typename(t))
2033 func iscallret(n *Node) bool {
2035 return n.Op == OINDREG && n.Reg == int16(Thearch.REGSP)
2038 func isstack(n *Node) bool {
2041 // If n is *autotmp and autotmp = &foo, replace n with foo.
2042 // We introduce such temps when initializing struct literals.
2043 if n.Op == OIND && n.Left.Op == ONAME && strings.HasPrefix(n.Left.Sym.Name, "autotmp_") {
2044 defn := n.Left.Name.Defn
2045 if defn != nil && defn.Op == OAS && defn.Right.Op == OADDR {
2052 return n.Reg == int16(Thearch.REGSP)
2056 case PAUTO, PPARAM, PPARAMOUT:
2064 func isglobal(n *Node) bool {
2078 // Do we need a write barrier for the assignment l = r?
2079 func needwritebarrier(l *Node, r *Node) bool {
2080 if use_writebarrier == 0 {
2084 if l == nil || isblank(l) {
2088 // No write barrier for write of non-pointers.
2091 if !haspointers(l.Type) {
2095 // No write barrier for write to stack.
2100 // No write barrier for implicit zeroing.
2105 // Ignore no-op conversions when making decision.
2106 // Ensures that xp = unsafe.Pointer(&x) is treated
2107 // the same as xp = &x.
2108 for r.Op == OCONVNOP {
2112 // No write barrier for zeroing or initialization to constant.
2113 if iszero(r) || r.Op == OLITERAL {
2117 // No write barrier for storing static (read-only) data.
2118 if r.Op == ONAME && strings.HasPrefix(r.Sym.Name, "statictmp_") {
2122 // No write barrier for storing address of stack values,
2123 // which are guaranteed only to be written to the stack.
2124 if r.Op == OADDR && isstack(r.Left) {
2128 // No write barrier for storing address of global, which
2129 // is live no matter what.
2130 if r.Op == OADDR && isglobal(r.Left) {
2134 // Otherwise, be conservative and use write barrier.
2138 // TODO(rsc): Perhaps componentgen should run before this.
2140 func applywritebarrier(n *Node, init **NodeList) *Node {
2141 if n.Left != nil && n.Right != nil && needwritebarrier(n.Left, n.Right) {
2143 Warnl(int(n.Lineno), "marking %v for barrier", Nconv(n.Left, 0))
2151 func convas(n *Node, init **NodeList) *Node {
2153 Fatalf("convas: not OAS %v", Oconv(int(n.Op), 0))
2160 if n.Left == nil || n.Right == nil {
2166 if lt == nil || rt == nil {
2170 if isblank(n.Left) {
2171 defaultlit(&n.Right, nil)
2175 if n.Left.Op == OINDEXMAP {
2179 walkexpr(&map_, init)
2180 walkexpr(&key, init)
2181 walkexpr(&val, init)
2183 // orderexpr made sure key and val are addressable.
2184 key = Nod(OADDR, key, nil)
2186 val = Nod(OADDR, val, nil)
2187 n = mkcall1(mapfn("mapassign1", map_.Type), nil, init, typename(map_.Type), map_, key, val)
2191 if !Eqtype(lt, rt) {
2192 n.Right = assignconv(n.Right, lt, "assignment")
2193 walkexpr(&n.Right, init)
2201 // from ascompat[te]
2202 // evaluating actual function arguments.
2204 // if there is exactly one function expr,
2205 // then it is done first. otherwise must
2206 // make temp variables
2207 func reorder1(all *NodeList) *NodeList {
2210 c := 0 // function calls
2211 t := 0 // total parameters
2213 for l := all; l != nil; l = l.Next {
2217 if n.Ullman >= UINF {
2222 if c == 0 || t == 1 {
2226 var g *NodeList // fncalls assigned to tempnames
2227 var f *Node // last fncall assigned to stack
2228 var r *NodeList // non fncalls and tempnames assigned to stack
2231 for l := all; l != nil; l = l.Next {
2233 if n.Ullman < UINF {
2244 // make assignment of fncall to tempname
2245 a = temp(n.Right.Type)
2247 a = Nod(OAS, a, n.Right)
2250 // put normal arg assignment on list
2251 // with fncall replaced by tempname
2263 // from ascompat[ee]
2265 // simultaneous assignment. there cannot
2266 // be later use of an earlier lvalue.
2268 // function calls have been removed.
2269 func reorder3(all *NodeList) *NodeList {
2272 // If a needed expression may be affected by an
2273 // earlier assignment, make an early copy of that
2274 // expression and use the copy instead.
2277 var mapinit *NodeList
2278 for list := all; list != nil; list = list.Next {
2281 // Save subexpressions needed on left side.
2282 // Drill through non-dereferences.
2284 if l.Op == ODOT || l.Op == OPAREN {
2289 if l.Op == OINDEX && Isfixedarray(l.Left.Type) {
2290 reorder3save(&l.Right, all, list, &early)
2300 Fatalf("reorder3 unexpected lvalue %v", Oconv(int(l.Op), obj.FmtSharp))
2305 case OINDEX, OINDEXMAP:
2306 reorder3save(&l.Left, all, list, &early)
2307 reorder3save(&l.Right, all, list, &early)
2308 if l.Op == OINDEXMAP {
2309 list.N = convas(list.N, &mapinit)
2313 reorder3save(&l.Left, all, list, &early)
2316 // Save expression on right side.
2317 reorder3save(&list.N.Right, all, list, &early)
2320 early = concat(mapinit, early)
2321 return concat(early, all)
2324 // if the evaluation of *np would be affected by the
2325 // assignments in all up to but not including stop,
2326 // copy into a temporary during *early and
2327 // replace *np with that temp.
2328 func reorder3save(np **Node, all *NodeList, stop *NodeList, early **NodeList) {
2330 if !aliased(n, all, stop) {
2337 *early = list(*early, q)
2341 // what's the outer value that a write to n affects?
2342 // outer value means containing struct or array.
2343 func outervalue(n *Node) *Node {
2346 Fatalf("OXDOT in walk")
2348 if n.Op == ODOT || n.Op == OPAREN || n.Op == OCONVNOP {
2353 if n.Op == OINDEX && Isfixedarray(n.Left.Type) {
2364 // Is it possible that the computation of n might be
2365 // affected by writes in as up to but not including stop?
2366 func aliased(n *Node, all *NodeList, stop *NodeList) bool {
2371 // Look for obvious aliasing: a variable being assigned
2372 // during the all list and appearing in n.
2373 // Also record whether there are any writes to main memory.
2374 // Also record whether there are any writes to variables
2375 // whose addresses have been taken.
2380 for l := all; l != stop; l = l.Next {
2381 a = outervalue(l.N.Left)
2392 case PAUTO, PPARAM, PPARAMOUT:
2405 // The variables being written do not appear in n.
2406 // However, n might refer to computed addresses
2407 // that are being written.
2409 // If no computed addresses are affected by the writes, no aliasing.
2410 if memwrite == 0 && varwrite == 0 {
2414 // If n does not refer to computed addresses
2415 // (that is, if n only refers to variables whose addresses
2416 // have not been taken), no aliasing.
2421 // Otherwise, both the writes and n refer to computed memory addresses.
2422 // Assume that they might conflict.
2426 // does the evaluation of n only refer to variables
2427 // whose addresses have not been taken?
2428 // (and no other memory)
2429 func varexpr(n *Node) bool {
2440 case PAUTO, PPARAM, PPARAMOUT:
2465 ODOT, // but not ODOTPTR
2470 return varexpr(n.Left) && varexpr(n.Right)
2477 // is the name l mentioned in r?
2478 func vmatch2(l *Node, r *Node) bool {
2483 // match each right given left
2491 if vmatch2(l, r.Left) {
2494 if vmatch2(l, r.Right) {
2497 for ll := r.List; ll != nil; ll = ll.Next {
2498 if vmatch2(l, ll.N) {
2505 // is any name mentioned in l also mentioned in r?
2506 // called by sinit.go
2507 func vmatch1(l *Node, r *Node) bool {
2508 // isolate all left sides
2509 if l == nil || r == nil {
2515 case PPARAM, PPARAMREF, PAUTO:
2518 // assignment to non-stack variable
2519 // must be delayed if right has function calls.
2521 if r.Ullman >= UINF {
2526 return vmatch2(l, r)
2532 if vmatch1(l.Left, r) {
2535 if vmatch1(l.Right, r) {
2538 for ll := l.List; ll != nil; ll = ll.Next {
2539 if vmatch1(ll.N, r) {
2546 // walk through argin parameters.
2547 // generate and return code to allocate
2548 // copies of escaped parameters to the heap.
2549 func paramstoheap(argin **Type, out int) *NodeList {
2555 for t := Structfirst(&savet, argin); t != nil; t = structnext(&savet) {
2557 if v != nil && v.Sym != nil && v.Sym.Name[0] == '~' && v.Sym.Name[1] == 'r' { // unnamed result
2561 // For precise stacks, the garbage collector assumes results
2562 // are always live, so zero them always.
2564 // Defer might stop a panic and show the
2565 // return values as they exist at the time of panic.
2566 // Make sure to zero them on entry to the function.
2567 nn = list(nn, Nod(OAS, nodarg(t, -1), nil))
2570 if v == nil || v.Class&PHEAP == 0 {
2574 // generate allocation & copying code
2575 if compiling_runtime != 0 {
2576 Yyerror("%v escapes to heap, not allowed in runtime.", v)
2578 if prealloc[v] == nil {
2579 prealloc[v] = callnew(v.Type)
2581 nn = list(nn, Nod(OAS, v.Name.Heapaddr, prealloc[v]))
2582 if v.Class&^PHEAP != PPARAMOUT {
2583 as = Nod(OAS, v, v.Name.Param.Stackparam)
2584 v.Name.Param.Stackparam.Typecheck = 1
2585 typecheck(&as, Etop)
2586 as = applywritebarrier(as, &nn)
2594 // walk through argout parameters copying back to stack
2595 func returnsfromheap(argin **Type) *NodeList {
2600 for t := Structfirst(&savet, argin); t != nil; t = structnext(&savet) {
2602 if v == nil || v.Class != PHEAP|PPARAMOUT {
2605 nn = list(nn, Nod(OAS, v.Name.Param.Stackparam, v))
2611 // take care of migrating any function in/out args
2612 // between the stack and the heap. adds code to
2613 // curfn's before and after lists.
2616 lineno = Curfn.Lineno
2617 nn := paramstoheap(getthis(Curfn.Type), 0)
2618 nn = concat(nn, paramstoheap(getinarg(Curfn.Type), 0))
2619 nn = concat(nn, paramstoheap(Getoutarg(Curfn.Type), 1))
2620 Curfn.Func.Enter = concat(Curfn.Func.Enter, nn)
2621 lineno = Curfn.Func.Endlineno
2622 Curfn.Func.Exit = returnsfromheap(Getoutarg(Curfn.Type))
2626 func vmkcall(fn *Node, t *Type, init **NodeList, va []*Node) *Node {
2627 if fn.Type == nil || fn.Type.Etype != TFUNC {
2628 Fatalf("mkcall %v %v", fn, fn.Type)
2632 n := fn.Type.Intuple
2633 for i := 0; i < n; i++ {
2634 args = list(args, va[i])
2637 r := Nod(OCALL, fn, nil)
2639 if fn.Type.Outtuple > 0 {
2640 typecheck(&r, Erv|Efnstruct)
2649 func mkcall(name string, t *Type, init **NodeList, args ...*Node) *Node {
2650 return vmkcall(syslook(name, 0), t, init, args)
2653 func mkcall1(fn *Node, t *Type, init **NodeList, args ...*Node) *Node {
2654 return vmkcall(fn, t, init, args)
2657 func conv(n *Node, t *Type) *Node {
2658 if Eqtype(n.Type, t) {
2661 n = Nod(OCONV, n, nil)
2667 func chanfn(name string, n int, t *Type) *Node {
2668 if t.Etype != TCHAN {
2669 Fatalf("chanfn %v", t)
2671 fn := syslook(name, 1)
2674 Fatalf("chanfn %d", n)
2676 substArgTypes(fn, t.Type)
2678 substArgTypes(fn, t.Type, t.Type)
2683 func mapfn(name string, t *Type) *Node {
2684 if t.Etype != TMAP {
2685 Fatalf("mapfn %v", t)
2687 fn := syslook(name, 1)
2688 substArgTypes(fn, t.Down, t.Type, t.Down, t.Type)
2692 func mapfndel(name string, t *Type) *Node {
2693 if t.Etype != TMAP {
2694 Fatalf("mapfn %v", t)
2696 fn := syslook(name, 1)
2697 substArgTypes(fn, t.Down, t.Type, t.Down)
2701 func writebarrierfn(name string, l *Type, r *Type) *Node {
2702 fn := syslook(name, 1)
2703 substArgTypes(fn, l, r)
2707 func addstr(n *Node, init **NodeList) *Node {
2708 // orderexpr rewrote OADDSTR to have a list of strings.
2712 Yyerror("addstr count %d too small", c)
2716 if n.Esc == EscNone {
2718 for l := n.List; l != nil; l = l.Next {
2719 if n.Op == OLITERAL {
2720 sz += int64(len(n.Val().U.(string)))
2724 // Don't allocate the buffer if the result won't fit.
2725 if sz < tmpstringbufsize {
2726 // Create temporary buffer for result string on stack.
2727 t := aindex(Nodintconst(tmpstringbufsize), Types[TUINT8])
2729 buf = Nod(OADDR, temp(t), nil)
2733 // build list of string arguments
2736 for l := n.List; l != nil; l = l.Next {
2737 args = list(args, conv(l.N, Types[TSTRING]))
2742 // small numbers of strings use direct runtime helpers.
2743 // note: orderexpr knows this cutoff too.
2744 fn = fmt.Sprintf("concatstring%d", c)
2746 // large numbers of strings are passed to the runtime as a slice.
2747 fn = "concatstrings"
2750 t.Type = Types[TSTRING]
2752 slice := Nod(OCOMPLIT, nil, typenod(t))
2753 if prealloc[n] != nil {
2754 prealloc[slice] = prealloc[n]
2756 slice.List = args.Next // skip buf arg
2758 args = list(args, slice)
2762 cat := syslook(fn, 1)
2763 r := Nod(OCALL, cat, nil)
2772 // expand append(l1, l2...) to
2775 // if n := len(l1) + len(l2) - cap(s); n > 0 {
2776 // s = growslice_n(s, n)
2778 // s = s[:len(l1)+len(l2)]
2779 // memmove(&s[len(l1)], &l2[0], len(l2)*sizeof(T))
2783 // l2 is allowed to be a string.
2784 func appendslice(n *Node, init **NodeList) *Node {
2785 walkexprlistsafe(n.List, init)
2787 // walkexprlistsafe will leave OINDEX (s[n]) alone if both s
2788 // and n are name or literal, but those may index the slice we're
2789 // modifying here. Fix explicitly.
2790 for l := n.List; l != nil; l = l.Next {
2791 l.N = cheapexpr(l.N, init)
2797 s := temp(l1.Type) // var s []T
2799 l = list(l, Nod(OAS, s, l1)) // s = l1
2801 nt := temp(Types[TINT])
2803 nif := Nod(OIF, nil, nil)
2805 // n := len(s) + len(l2) - cap(s)
2806 nif.Ninit = list1(Nod(OAS, nt, Nod(OSUB, Nod(OADD, Nod(OLEN, s, nil), Nod(OLEN, l2, nil)), Nod(OCAP, s, nil))))
2808 nif.Left = Nod(OGT, nt, Nodintconst(0))
2810 // instantiate growslice_n(Type*, []any, int) []any
2811 fn := syslook("growslice_n", 1) // growslice_n(<type>, old []T, n int64) (ret []T)
2812 substArgTypes(fn, s.Type.Type, s.Type.Type)
2814 // s = growslice_n(T, s, n)
2815 nif.Nbody = list1(Nod(OAS, s, mkcall1(fn, s.Type, &nif.Ninit, typename(s.Type), s, nt)))
2819 if haspointers(l1.Type.Type) {
2820 // copy(s[len(l1):len(l1)+len(l2)], l2)
2821 nptr1 := Nod(OSLICE, s, Nod(OKEY, Nod(OLEN, l1, nil), Nod(OADD, Nod(OLEN, l1, nil), Nod(OLEN, l2, nil))))
2825 fn := syslook("typedslicecopy", 1)
2826 substArgTypes(fn, l1.Type, l2.Type)
2827 nt := mkcall1(fn, Types[TINT], &l, typename(l1.Type.Type), nptr1, nptr2)
2829 } else if instrumenting {
2830 // rely on runtime to instrument copy.
2831 // copy(s[len(l1):len(l1)+len(l2)], l2)
2832 nptr1 := Nod(OSLICE, s, Nod(OKEY, Nod(OLEN, l1, nil), Nod(OADD, Nod(OLEN, l1, nil), Nod(OLEN, l2, nil))))
2837 if l2.Type.Etype == TSTRING {
2838 fn = syslook("slicestringcopy", 1)
2840 fn = syslook("slicecopy", 1)
2842 substArgTypes(fn, l1.Type, l2.Type)
2843 nt := mkcall1(fn, Types[TINT], &l, nptr1, nptr2, Nodintconst(s.Type.Type.Width))
2846 // memmove(&s[len(l1)], &l2[0], len(l2)*sizeof(T))
2847 nptr1 := Nod(OINDEX, s, Nod(OLEN, l1, nil))
2849 nptr1.Bounded = true
2850 nptr1 = Nod(OADDR, nptr1, nil)
2852 nptr2 := Nod(OSPTR, l2, nil)
2854 fn := syslook("memmove", 1)
2855 substArgTypes(fn, s.Type.Type, s.Type.Type)
2857 nwid := cheapexpr(conv(Nod(OLEN, l2, nil), Types[TUINTPTR]), &l)
2859 nwid = Nod(OMUL, nwid, Nodintconst(s.Type.Type.Width))
2860 nt := mkcall1(fn, nil, &l, nptr1, nptr2, nwid)
2864 // s = s[:len(l1)+len(l2)]
2865 nt = Nod(OADD, Nod(OLEN, l1, nil), Nod(OLEN, l2, nil))
2867 nt = Nod(OSLICE, s, Nod(OKEY, nil, nt))
2869 l = list(l, Nod(OAS, s, nt))
2871 typechecklist(l, Etop)
2873 *init = concat(*init, l)
2877 // Rewrite append(src, x, y, z) so that any side effects in
2878 // x, y, z (including runtime panics) are evaluated in
2879 // initialization statements before the append.
2880 // For normal code generation, stop there and leave the
2881 // rest to cgen_append.
2883 // For race detector, expand append(src, a [, b]* ) to
2887 // const argc = len(args) - 1
2888 // if cap(s) - len(s) < argc {
2889 // s = growslice(s, len(s)+argc)
2898 func walkappend(n *Node, init **NodeList, dst *Node) *Node {
2899 if !samesafeexpr(dst, n.List.N) {
2901 l.N = safeexpr(l.N, init)
2902 walkexpr(&l.N, init)
2904 walkexprlistsafe(n.List.Next, init)
2906 // walkexprlistsafe will leave OINDEX (s[n]) alone if both s
2907 // and n are name or literal, but those may index the slice we're
2908 // modifying here. Fix explicitly.
2909 // Using cheapexpr also makes sure that the evaluation
2910 // of all arguments (and especially any panics) happen
2911 // before we begin to modify the slice in a visible way.
2912 for l := n.List.Next; l != nil; l = l.Next {
2913 l.N = cheapexpr(l.N, init)
2918 // Resolve slice type of multi-valued return.
2919 if Istype(nsrc.Type, TSTRUCT) {
2920 nsrc.Type = nsrc.Type.Type.Type
2922 argc := count(n.List) - 1
2927 // General case, with no function calls left as arguments.
2928 // Leave for gen, except that instrumentation requires old form.
2935 ns := temp(nsrc.Type)
2936 l = list(l, Nod(OAS, ns, nsrc)) // s = src
2938 na := Nodintconst(int64(argc)) // const argc
2939 nx := Nod(OIF, nil, nil) // if cap(s) - len(s) < argc
2940 nx.Left = Nod(OLT, Nod(OSUB, Nod(OCAP, ns, nil), Nod(OLEN, ns, nil)), na)
2942 fn := syslook("growslice", 1) // growslice(<type>, old []T, mincap int) (ret []T)
2943 substArgTypes(fn, ns.Type.Type, ns.Type.Type)
2945 nx.Nbody = list1(Nod(OAS, ns, mkcall1(fn, ns.Type, &nx.Ninit, typename(ns.Type), ns, Nod(OADD, Nod(OLEN, ns, nil), na))))
2949 nn := temp(Types[TINT])
2950 l = list(l, Nod(OAS, nn, Nod(OLEN, ns, nil))) // n = len(s)
2952 nx = Nod(OSLICE, ns, Nod(OKEY, nil, Nod(OADD, nn, na))) // ...s[:n+argc]
2954 l = list(l, Nod(OAS, ns, nx)) // s = s[:n+argc]
2956 for a := n.List.Next; a != nil; a = a.Next {
2957 nx = Nod(OINDEX, ns, nn) // s[n] ...
2959 l = list(l, Nod(OAS, nx, a.N)) // s[n] = arg
2961 l = list(l, Nod(OAS, nn, Nod(OADD, nn, Nodintconst(1)))) // n = n + 1
2965 typechecklist(l, Etop)
2967 *init = concat(*init, l)
2971 // Lower copy(a, b) to a memmove call or a runtime call.
2975 // if n > len(b) { n = len(b) }
2976 // memmove(a.ptr, b.ptr, n*sizeof(elem(a)))
2980 // Also works if b is a string.
2982 func copyany(n *Node, init **NodeList, runtimecall bool) *Node {
2983 if haspointers(n.Left.Type.Type) {
2984 fn := writebarrierfn("typedslicecopy", n.Left.Type, n.Right.Type)
2985 return mkcall1(fn, n.Type, init, typename(n.Left.Type.Type), n.Left, n.Right)
2990 if n.Right.Type.Etype == TSTRING {
2991 fn = syslook("slicestringcopy", 1)
2993 fn = syslook("slicecopy", 1)
2995 substArgTypes(fn, n.Left.Type, n.Right.Type)
2996 return mkcall1(fn, n.Type, init, n.Left, n.Right, Nodintconst(n.Left.Type.Type.Width))
2999 walkexpr(&n.Left, init)
3000 walkexpr(&n.Right, init)
3001 nl := temp(n.Left.Type)
3002 nr := temp(n.Right.Type)
3004 l = list(l, Nod(OAS, nl, n.Left))
3005 l = list(l, Nod(OAS, nr, n.Right))
3007 nfrm := Nod(OSPTR, nr, nil)
3008 nto := Nod(OSPTR, nl, nil)
3010 nlen := temp(Types[TINT])
3013 l = list(l, Nod(OAS, nlen, Nod(OLEN, nl, nil)))
3015 // if n > len(frm) { n = len(frm) }
3016 nif := Nod(OIF, nil, nil)
3018 nif.Left = Nod(OGT, nlen, Nod(OLEN, nr, nil))
3019 nif.Nbody = list(nif.Nbody, Nod(OAS, nlen, Nod(OLEN, nr, nil)))
3023 fn := syslook("memmove", 1)
3025 substArgTypes(fn, nl.Type.Type, nl.Type.Type)
3026 nwid := temp(Types[TUINTPTR])
3027 l = list(l, Nod(OAS, nwid, conv(nlen, Types[TUINTPTR])))
3028 nwid = Nod(OMUL, nwid, Nodintconst(nl.Type.Type.Width))
3029 l = list(l, mkcall1(fn, nil, init, nto, nfrm, nwid))
3031 typechecklist(l, Etop)
3033 *init = concat(*init, l)
3037 func eqfor(t *Type, needsize *int) *Node {
3038 // Should only arrive here with large memory or
3039 // a struct/array containing a non-memory field/element.
3040 // Small memory is handled inline, and single non-memory
3041 // is handled during type check (OCMPSTR etc).
3042 a := algtype1(t, nil)
3044 if a != AMEM && a != -1 {
3045 Fatalf("eqfor %v", t)
3049 n := syslook("memequal", 1)
3050 substArgTypes(n, t, t)
3055 sym := typesymprefix(".eq", t)
3058 ntype := Nod(OTFUNC, nil, nil)
3059 ntype.List = list(ntype.List, Nod(ODCLFIELD, nil, typenod(Ptrto(t))))
3060 ntype.List = list(ntype.List, Nod(ODCLFIELD, nil, typenod(Ptrto(t))))
3061 ntype.Rlist = list(ntype.Rlist, Nod(ODCLFIELD, nil, typenod(Types[TBOOL])))
3062 typecheck(&ntype, Etype)
3068 func countfield(t *Type) int {
3070 for t1 := t.Type; t1 != nil; t1 = t1.Down {
3076 func walkcompare(np **Node, init **NodeList) {
3079 // Given interface value l and concrete value r, rewrite
3082 // x, ok := l.(type(r)); ok && x == r
3083 // Handle != similarly.
3084 // This avoids the allocation that would be required
3085 // to convert r to l for comparison.
3089 if Isinter(n.Left.Type) && !Isinter(n.Right.Type) {
3092 } else if !Isinter(n.Left.Type) && Isinter(n.Right.Type) {
3099 if haspointers(r.Type) {
3100 a := Nod(OAS, x, nil)
3102 *init = list(*init, a)
3104 ok := temp(Types[TBOOL])
3107 a := Nod(ODOTTYPE, l, nil)
3111 // x, ok := l.(type(r))
3112 expr := Nod(OAS2, nil, nil)
3114 expr.List = list1(x)
3115 expr.List = list(expr.List, ok)
3116 expr.Rlist = list1(a)
3117 typecheck(&expr, Etop)
3118 walkexpr(&expr, init)
3121 r = Nod(OANDAND, ok, Nod(OEQ, x, r))
3123 r = Nod(OOROR, Nod(ONOT, ok, nil), Nod(ONE, x, r))
3125 *init = list(*init, expr)
3126 finishcompare(np, n, r, init)
3130 // Must be comparison of array or struct.
3131 // Otherwise back end handles it.
3148 for cmpl != nil && cmpl.Op == OCONVNOP {
3152 for cmpr != nil && cmpr.Op == OCONVNOP {
3156 if !islvalue(cmpl) || !islvalue(cmpr) {
3157 Fatalf("arguments of comparison must be lvalues - %v %v", cmpl, cmpr)
3161 a := Nod(OAS, l, Nod(OADDR, cmpl, nil))
3162 a.Right.Etype = 1 // addr does not escape
3164 *init = list(*init, a)
3167 a = Nod(OAS, r, Nod(OADDR, cmpr, nil))
3168 a.Right.Etype = 1 // addr does not escape
3170 *init = list(*init, a)
3172 var andor Op = OANDAND
3178 if t.Etype == TARRAY && t.Bound <= 4 && issimple[t.Type.Etype] {
3179 // Four or fewer elements of a basic type.
3180 // Unroll comparisons.
3183 for i := 0; int64(i) < t.Bound; i++ {
3184 li = Nod(OINDEX, l, Nodintconst(int64(i)))
3185 ri = Nod(OINDEX, r, Nodintconst(int64(i)))
3186 a = Nod(n.Op, li, ri)
3190 expr = Nod(andor, expr, a)
3195 expr = Nodbool(n.Op == OEQ)
3197 finishcompare(np, n, expr, init)
3201 if t.Etype == TSTRUCT && countfield(t) <= 4 {
3202 // Struct of four or fewer fields.
3203 // Inline comparisons.
3206 for t1 := t.Type; t1 != nil; t1 = t1.Down {
3207 if isblanksym(t1.Sym) {
3210 li = Nod(OXDOT, l, newname(t1.Sym))
3211 ri = Nod(OXDOT, r, newname(t1.Sym))
3212 a = Nod(n.Op, li, ri)
3216 expr = Nod(andor, expr, a)
3221 expr = Nodbool(n.Op == OEQ)
3223 finishcompare(np, n, expr, init)
3227 // Chose not to inline. Call equality function directly.
3229 call := Nod(OCALL, eqfor(t, &needsize), nil)
3231 call.List = list(call.List, l)
3232 call.List = list(call.List, r)
3234 call.List = list(call.List, Nodintconst(t.Width))
3238 r = Nod(ONOT, r, nil)
3241 finishcompare(np, n, r, init)
3245 func finishcompare(np **Node, n, r *Node, init **NodeList) {
3246 // Using np here to avoid passing &r to typecheck.
3251 if r.Type != n.Type {
3252 r = Nod(OCONVNOP, r, nil)
3259 func samecheap(a *Node, b *Node) bool {
3262 for a != nil && b != nil && a.Op == b.Op {
3273 if ar.Op != ONAME || br.Op != ONAME || ar.Sym != br.Sym {
3280 if !Isconst(ar, CTINT) || !Isconst(br, CTINT) || Mpcmpfixfix(ar.Val().U.(*Mpint), br.Val().U.(*Mpint)) != 0 {
3292 func walkrotate(np **Node) {
3293 if Thearch.Thechar == '0' || Thearch.Thechar == '7' || Thearch.Thechar == '9' {
3299 // Want << | >> or >> | << or << ^ >> or >> ^ << on unsigned value.
3303 if (n.Op != OOR && n.Op != OXOR) || (l.Op != OLSH && l.Op != ORSH) || (r.Op != OLSH && r.Op != ORSH) || n.Type == nil || Issigned[n.Type.Etype] || l.Op == r.Op {
3307 // Want same, side effect-free expression on lhs of both shifts.
3308 if !samecheap(l.Left, r.Left) {
3312 // Constants adding to width?
3313 w := int(l.Type.Width * 8)
3315 if Smallintconst(l.Right) && Smallintconst(r.Right) {
3316 sl := int(Mpgetfix(l.Right.Val().U.(*Mpint)))
3318 sr := int(Mpgetfix(r.Right.Val().U.(*Mpint)))
3319 if sr >= 0 && sl+sr == w {
3320 // Rewrite left shift half to left rotate.
3328 // Remove rotate 0 and rotate w.
3329 s := int(Mpgetfix(n.Right.Val().U.(*Mpint)))
3331 if s == 0 || s == w {
3342 // TODO: Could allow s and 32-s if s is bounded (maybe s&31 and 32-s&31).
3346 // walkmul rewrites integer multiplication by powers of two as shifts.
3347 func walkmul(np **Node, init **NodeList) {
3349 if !Isint[n.Type.Etype] {
3355 if n.Right.Op == OLITERAL {
3358 } else if n.Left.Op == OLITERAL {
3367 // x*0 is 0 (and side effects of x).
3370 if Mpgetfix(nr.Val().U.(*Mpint)) == 0 {
3372 Nodconst(n, n.Type, 0)
3376 // nr is a constant.
3383 // negative power of 2, like -16
3389 w = int(nl.Type.Width * 8)
3390 if pow+1 >= w { // too big, shouldn't happen
3394 nl = cheapexpr(nl, init)
3403 n = Nod(OLSH, nl, Nodintconst(int64(pow)))
3407 n = Nod(OMINUS, n, nil)
3415 // walkdiv rewrites division by a constant as less expensive
3417 func walkdiv(np **Node, init **NodeList) {
3418 // if >= 0, nr is 1<<pow // 1 if nr is negative.
3421 if Thearch.Thechar == '0' || Thearch.Thechar == '7' || Thearch.Thechar == '9' {
3426 if n.Right.Op != OLITERAL {
3430 // nr is a constant.
3431 nl := cheapexpr(n.Left, init)
3435 // special cases of mod/div
3437 w := int(nl.Type.Width * 8)
3439 s := 0 // 1 if nr is negative.
3440 pow := powtwo(nr) // if >= 0, nr is 1<<pow
3442 // negative power of 2
3449 // divisor too large.
3454 // try to do division by multiply by (2^w)/d
3455 // see hacker's delight chapter 10
3456 // TODO: support 64-bit magic multiply here.
3460 if Issigned[nl.Type.Etype] {
3461 m.Sd = Mpgetfix(nr.Val().U.(*Mpint))
3464 m.Ud = uint64(Mpgetfix(nr.Val().U.(*Mpint)))
3472 // We have a quick division method so use it
3475 // rewrite as A%B = A - (A/B*B).
3476 n1 := Nod(ODIV, nl, nr)
3478 n2 := Nod(OMUL, n1, nr)
3479 n = Nod(OSUB, nl, n2)
3483 switch Simtype[nl.Type.Etype] {
3487 // n1 = nl * magic >> w (HMUL)
3488 case TUINT8, TUINT16, TUINT32:
3489 nc := Nod(OXXX, nil, nil)
3491 Nodconst(nc, nl.Type, int64(m.Um))
3492 n1 := Nod(OHMUL, nl, nc)
3495 // Select a Go type with (at least) twice the width.
3497 switch Simtype[nl.Type.Etype] {
3501 case TUINT8, TUINT16:
3502 twide = Types[TUINT32]
3505 twide = Types[TUINT64]
3508 twide = Types[TINT32]
3511 twide = Types[TINT64]
3514 // add numerator (might overflow).
3516 n2 := Nod(OADD, conv(n1, twide), conv(nl, twide))
3519 nc := Nod(OXXX, nil, nil)
3521 Nodconst(nc, Types[TUINT], int64(m.S))
3522 n = conv(Nod(ORSH, n2, nc), nl.Type)
3525 nc := Nod(OXXX, nil, nil)
3527 Nodconst(nc, Types[TUINT], int64(m.S))
3528 n = Nod(ORSH, n1, nc)
3531 // n1 = nl * magic >> w
3532 case TINT8, TINT16, TINT32:
3533 nc := Nod(OXXX, nil, nil)
3535 Nodconst(nc, nl.Type, m.Sm)
3536 n1 := Nod(OHMUL, nl, nc)
3539 // add the numerator.
3540 n1 = Nod(OADD, n1, nl)
3544 nc = Nod(OXXX, nil, nil)
3546 Nodconst(nc, Types[TUINT], int64(m.S))
3547 n2 := conv(Nod(ORSH, n1, nc), nl.Type)
3549 // add 1 iff n1 is negative.
3550 nc = Nod(OXXX, nil, nil)
3552 Nodconst(nc, Types[TUINT], int64(w)-1)
3553 n3 := Nod(ORSH, nl, nc) // n4 = -1 iff n1 is negative.
3554 n = Nod(OSUB, n2, n3)
3558 n = Nod(OMINUS, n, nil)
3569 Nodconst(n, n.Type, 0)
3581 if Issigned[n.Type.Etype] {
3583 // signed modulo 2^pow is like ANDing
3584 // with the last pow bits, but if nl < 0,
3585 // nl & (2^pow-1) is (nl+1)%2^pow - 1.
3586 nc := Nod(OXXX, nil, nil)
3588 Nodconst(nc, Types[Simtype[TUINT]], int64(w)-1)
3589 n1 := Nod(ORSH, nl, nc) // n1 = -1 iff nl < 0.
3592 n1 = cheapexpr(n1, init)
3594 // n = (nl+ε)&1 -ε where ε=1 iff nl<0.
3595 n2 := Nod(OSUB, nl, n1)
3597 nc := Nod(OXXX, nil, nil)
3598 Nodconst(nc, nl.Type, 1)
3599 n3 := Nod(OAND, n2, nc)
3600 n = Nod(OADD, n3, n1)
3602 // n = (nl+ε)&(nr-1) - ε where ε=2^pow-1 iff nl<0.
3603 nc := Nod(OXXX, nil, nil)
3605 Nodconst(nc, nl.Type, (1<<uint(pow))-1)
3606 n2 := Nod(OAND, n1, nc) // n2 = 2^pow-1 iff nl<0.
3608 n2 = cheapexpr(n2, init)
3610 n3 := Nod(OADD, nl, n2)
3611 n4 := Nod(OAND, n3, nc)
3612 n = Nod(OSUB, n4, n2)
3617 // arithmetic right shift does not give the correct rounding.
3618 // if nl >= 0, nl >> n == nl / nr
3619 // if nl < 0, we want to add 2^n-1 first.
3620 nc := Nod(OXXX, nil, nil)
3622 Nodconst(nc, Types[Simtype[TUINT]], int64(w)-1)
3623 n1 := Nod(ORSH, nl, nc) // n1 = -1 iff nl < 0.
3626 n.Left = Nod(OSUB, nl, n1)
3628 // Do a logical right right on -1 to keep pow bits.
3629 nc := Nod(OXXX, nil, nil)
3631 Nodconst(nc, Types[Simtype[TUINT]], int64(w)-int64(pow))
3632 n2 := Nod(ORSH, conv(n1, tounsigned(nl.Type)), nc)
3633 n.Left = Nod(OADD, nl, conv(n2, nl.Type))
3636 // n = (nl + 2^pow-1) >> pow
3639 nc = Nod(OXXX, nil, nil)
3640 Nodconst(nc, Types[Simtype[TUINT]], int64(pow))
3646 n = Nod(OMINUS, n, nil)
3651 nc := Nod(OXXX, nil, nil)
3656 Nodconst(nc, nl.Type, Mpgetfix(nr.Val().U.(*Mpint))-1)
3661 Nodconst(nc, Types[Simtype[TUINT]], int64(pow))
3676 // return 1 if integer n must be in range [0, max), 0 otherwise
3677 func bounded(n *Node, max int64) bool {
3678 if n.Type == nil || !Isint[n.Type.Etype] {
3682 sign := Issigned[n.Type.Etype]
3683 bits := int32(8 * n.Type.Width)
3685 if Smallintconst(n) {
3686 v := Mpgetfix(n.Val().U.(*Mpint))
3687 return 0 <= v && v < max
3693 if Smallintconst(n.Left) {
3694 v = Mpgetfix(n.Left.Val().U.(*Mpint))
3695 } else if Smallintconst(n.Right) {
3696 v = Mpgetfix(n.Right.Val().U.(*Mpint))
3699 if 0 <= v && v < max {
3704 if !sign && Smallintconst(n.Right) {
3705 v := Mpgetfix(n.Right.Val().U.(*Mpint))
3706 if 0 <= v && v <= max {
3712 if !sign && Smallintconst(n.Right) {
3713 v := Mpgetfix(n.Right.Val().U.(*Mpint))
3714 for bits > 0 && v >= 2 {
3721 if !sign && Smallintconst(n.Right) {
3722 v := Mpgetfix(n.Right.Val().U.(*Mpint))
3723 if v > int64(bits) {
3730 if !sign && bits <= 62 && 1<<uint(bits) <= max {
3737 func usefield(n *Node) {
3738 if obj.Fieldtrack_enabled == 0 {
3744 Fatalf("usefield %v", Oconv(int(n.Op), 0))
3754 field := dotField[typeSym{t.Orig, n.Right.Sym}]
3756 Fatalf("usefield %v %v without paramfld", n.Left.Type, n.Right.Sym)
3758 if field.Note == nil || !strings.Contains(*field.Note, "go:\"track\"") {
3763 if field.Lastfn == Curfn {
3766 field.Lastfn = Curfn
3767 field.Outer = n.Left.Type
3768 if Isptr[field.Outer.Etype] {
3769 field.Outer = field.Outer.Type
3771 if field.Outer.Sym == nil {
3772 Yyerror("tracked field must be in named struct type")
3774 if !exportname(field.Sym.Name) {
3775 Yyerror("tracked field must be exported (upper case)")
3778 Curfn.Func.Fieldtrack = append(Curfn.Func.Fieldtrack, field)
3781 func candiscardlist(l *NodeList) bool {
3782 for ; l != nil; l = l.Next {
3783 if !candiscard(l.N) {
3790 func candiscard(n *Node) bool {
3799 // Discardable as long as the subpieces are.
3854 // Discardable as long as we know it's not division by zero.
3856 if Isconst(n.Right, CTINT) && mpcmpfixc(n.Right.Val().U.(*Mpint), 0) != 0 {
3859 if Isconst(n.Right, CTFLT) && mpcmpfltc(n.Right.Val().U.(*Mpflt), 0) != 0 {
3864 // Discardable as long as we know it won't fail because of a bad size.
3865 case OMAKECHAN, OMAKEMAP:
3866 if Isconst(n.Left, CTINT) && mpcmpfixc(n.Left.Val().U.(*Mpint), 0) == 0 {
3871 // Difficult to tell what sizes are okay.
3876 if !candiscard(n.Left) || !candiscard(n.Right) || !candiscardlist(n.Ninit) || !candiscardlist(n.Nbody) || !candiscardlist(n.List) || !candiscardlist(n.Rlist) {
3886 // func(a1, a2, a3) {
3887 // print(a1, a2, a3)
3889 // and same for println.
3891 var walkprintfunc_prgen int
3893 func walkprintfunc(np **Node, init **NodeList) {
3897 walkstmtlist(n.Ninit)
3898 *init = concat(*init, n.Ninit)
3902 t := Nod(OTFUNC, nil, nil)
3904 var printargs *NodeList
3907 for l := n.List; l != nil; l = l.Next {
3908 buf = fmt.Sprintf("a%d", num)
3910 a = Nod(ODCLFIELD, newname(Lookup(buf)), typenod(l.N.Type))
3911 t.List = list(t.List, a)
3912 printargs = list(printargs, a.Left)
3915 fn := Nod(ODCLFUNC, nil, nil)
3916 walkprintfunc_prgen++
3917 buf = fmt.Sprintf("print·%d", walkprintfunc_prgen)
3918 fn.Func.Nname = newname(Lookup(buf))
3919 fn.Func.Nname.Name.Defn = fn
3920 fn.Func.Nname.Name.Param.Ntype = t
3921 declare(fn.Func.Nname, PFUNC)
3927 a = Nod(n.Op, nil, nil)
3936 typecheck(&fn, Etop)
3937 typechecklist(fn.Nbody, Etop)
3938 xtop = list(xtop, fn)
3941 a = Nod(OCALL, nil, nil)
3942 a.Left = fn.Func.Nname