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, 1)
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, 1)
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(int(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(int(n.Op), rl, n.List, &n.Ninit)
324 n.List = reorder3(ll)
328 ll := ascompatte(int(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)
370 * walk the whole tree of the body of an
371 * expression or simple statement.
372 * the types expressions are calculated.
373 * compile-time constants are evaluated.
374 * complex side effects like statements are appended to init
376 func walkexprlist(l *NodeList, init **NodeList) {
377 for ; l != nil; l = l.Next {
382 func walkexprlistsafe(l *NodeList, init **NodeList) {
383 for ; l != nil; l = l.Next {
384 l.N = safeexpr(l.N, init)
389 func walkexprlistcheap(l *NodeList, init **NodeList) {
390 for ; l != nil; l = l.Next {
391 l.N = cheapexpr(l.N, init)
396 func walkexpr(np **Node, init **NodeList) {
403 if init == &n.Ninit {
404 // not okay to use n->ninit when walking n,
405 // because we might replace n with some other node
406 // and would lose the init list.
407 Fatalf("walkexpr init == &n->ninit")
411 walkstmtlist(n.Ninit)
412 *init = concat(*init, n.Ninit)
416 // annoying case - not typechecked
418 walkexpr(&n.Left, init)
419 walkexpr(&n.Right, init)
426 Dump("walk-before", n)
429 if n.Typecheck != 1 {
430 Fatalf("missed typecheck: %v\n", Nconv(n, obj.FmtSign))
436 Fatalf("walkexpr: switch 1 unknown op %v", Nconv(n, obj.FmtShort|obj.FmtSign))
454 walkexpr(&n.Left, init)
458 walkexpr(&n.Left, init)
463 walkexpr(&n.Left, init)
468 if n.Op == ODOTPTR && n.Left.Type.Type.Width == 0 {
469 // No actual copy will be generated, so emit an explicit nil check.
470 n.Left = cheapexpr(n.Left, init)
472 checknil(n.Left, init)
475 walkexpr(&n.Left, init)
479 walkexpr(&n.Left, init)
480 walkexpr(&n.Right, init)
484 walkexpr(&n.Left, init)
488 walkexpr(&n.Left, init)
490 // replace len(*[10]int) with 10.
491 // delayed until now to preserve side effects.
498 safeexpr(n.Left, init)
499 Nodconst(n, n.Type, t.Bound)
506 walkexpr(&n.Left, init)
507 walkexpr(&n.Right, init)
509 n.Bounded = bounded(n.Right, 8*t.Width)
510 if Debug['m'] != 0 && n.Etype != 0 && !Isconst(n.Right, CTINT) {
511 Warn("shift bounds check elided")
515 // Use results from call expression as arguments for complex.
526 if n.Op == OCOMPLEX && n.Left == nil && n.Right == nil {
528 n.Right = n.List.Next.N
531 walkexpr(&n.Left, init)
532 walkexpr(&n.Right, init)
536 walkexpr(&n.Left, init)
537 walkexpr(&n.Right, init)
542 walkexpr(&n.Left, init)
543 walkexpr(&n.Right, init)
545 // Disable safemode while compiling this code: the code we
546 // generate internally can refer to unsafe.Pointer.
547 // In this case it can happen if we need to generate an ==
548 // for a struct containing a reflect.Value, which itself has
549 // an unexported field of type unsafe.Pointer.
550 old_safemode := safemode
553 walkcompare(&n, init)
554 safemode = old_safemode
558 walkexpr(&n.Left, init)
560 // cannot put side effects from n.Right on init,
561 // because they cannot run before n.Left is checked.
562 // save elsewhere and store on the eventual n.Right.
565 walkexpr(&n.Right, &ll)
566 addinit(&n.Right, ll)
569 case OPRINT, OPRINTN:
570 walkexprlist(n.List, init)
571 n = walkprint(n, init)
575 n = mkcall("gopanic", nil, init, n.Left)
579 n = mkcall("gorecover", n.Type, init, Nod(OADDR, nodfp, nil))
586 case OCLOSUREVAR, OCFUNC:
591 if n.Class&PHEAP == 0 && n.Class != PPARAMREF {
598 if n.List != nil && n.List.N.Op == OAS {
601 walkexpr(&n.Left, init)
602 walkexprlist(n.List, init)
603 ll := ascompatte(int(n.Op), n, n.Isddd, getinarg(t), n.List, 0, init)
604 n.List = reorder1(ll)
608 if n.Left.Op == OCLOSURE {
609 // Transform direct call of a closure to call of a normal function.
610 // transformclosure already did all preparation work.
612 // Prepend captured variables to argument list.
613 n.List = concat(n.Left.Func.Enter, n.List)
615 n.Left.Func.Enter = nil
617 // Replace OCLOSURE with ONAME/PFUNC.
618 n.Left = n.Left.Func.Closure.Func.Nname
620 // Update type of OCALLFUNC node.
621 // Output arguments had not changed, but their offsets could.
622 if n.Left.Type.Outtuple == 1 {
623 t := getoutargx(n.Left.Type).Type
624 if t.Etype == TFIELD {
629 n.Type = getoutargx(n.Left.Type)
634 if n.List != nil && n.List.N.Op == OAS {
638 walkexpr(&n.Left, init)
639 walkexprlist(n.List, init)
641 if n.Left.Op == ONAME && n.Left.Sym.Name == "Sqrt" && n.Left.Sym.Pkg.Path == "math" {
642 switch Thearch.Thechar {
651 ll := ascompatte(int(n.Op), n, n.Isddd, getinarg(t), n.List, 0, init)
652 n.List = reorder1(ll)
657 if n.List != nil && n.List.N.Op == OAS {
660 walkexpr(&n.Left, init)
661 walkexprlist(n.List, init)
662 ll := ascompatte(int(n.Op), n, false, getthis(t), list1(n.Left.Left), 0, init)
663 lr := ascompatte(int(n.Op), n, n.Isddd, getinarg(t), n.List, 0, init)
667 n.List = reorder1(ll)
671 *init = concat(*init, n.Ninit)
674 walkexpr(&n.Left, init)
675 n.Left = safeexpr(n.Left, init)
681 if n.Right == nil || iszero(n.Right) && flag_race == 0 {
687 walkexpr(&n.Right, init)
690 // TODO(rsc): The Isfat is for consistency with componentgen and orderexpr.
691 // It needs to be removed in all three places.
692 // That would allow inlining x.(struct{*int}) the same as x.(*int).
693 if isdirectiface(n.Right.Type) && !Isfat(n.Right.Type) && flag_race == 0 {
694 // handled directly during cgen
695 walkexpr(&n.Right, init)
699 // x = i.(T); n.Left is x, n.Right.Left is i.
700 // orderstmt made sure x is addressable.
701 walkexpr(&n.Right.Left, init)
703 n1 := Nod(OADDR, n.Left, nil)
704 r := n.Right // i.(T)
706 if Debug_typeassert > 0 {
707 Warn("type assertion not inlined")
710 buf := "assert" + type2IET(r.Left.Type) + "2" + type2IET(r.Type)
711 fn := syslook(buf, 1)
712 substArgTypes(fn, r.Left.Type, r.Type)
714 n = mkcall1(fn, nil, init, typename(r.Type), r.Left, n1)
719 // x = <-c; n.Left is x, n.Right.Left is c.
720 // orderstmt made sure x is addressable.
721 walkexpr(&n.Right.Left, init)
723 n1 := Nod(OADDR, n.Left, nil)
724 r := n.Right.Left // the channel
725 n = mkcall1(chanfn("chanrecv1", 2, r.Type), nil, init, typename(r.Type), r, n1)
733 r = appendslice(r, init) // also works for append(slice, string).
735 r = walkappend(r, init, n)
739 // Left in place for back end.
740 // Do not add a new write barrier.
743 // Otherwise, lowered for race detector.
744 // Treat as ordinary assignment.
747 if n.Left != nil && n.Right != nil {
748 r := convas(Nod(OAS, n.Left, n.Right), init)
751 n = applywritebarrier(n, init)
757 *init = concat(*init, n.Ninit)
759 walkexprlistsafe(n.List, init)
760 walkexprlistsafe(n.Rlist, init)
761 ll := ascompatee(OAS, n.List, n.Rlist, init)
763 for lr := ll; lr != nil; lr = lr.Next {
764 lr.N = applywritebarrier(lr.N, init)
771 *init = concat(*init, n.Ninit)
775 walkexprlistsafe(n.List, init)
778 ll := ascompatet(int(n.Op), n.List, &r.Type, 0, init)
779 for lr := ll; lr != nil; lr = lr.Next {
780 lr.N = applywritebarrier(lr.N, init)
782 n = liststmt(concat(list1(r), ll))
786 // orderstmt made sure x is addressable.
788 *init = concat(*init, n.Ninit)
792 walkexprlistsafe(n.List, init)
793 walkexpr(&r.Left, init)
795 if isblank(n.List.N) {
798 n1 = Nod(OADDR, n.List.N, nil)
800 n1.Etype = 1 // addr does not escape
801 fn := chanfn("chanrecv2", 2, r.Left.Type)
802 r = mkcall1(fn, n.List.Next.N.Type, init, typename(r.Left.Type), r.Left, n1)
803 n = Nod(OAS, n.List.Next.N, r)
809 *init = concat(*init, n.Ninit)
813 walkexprlistsafe(n.List, init)
814 walkexpr(&r.Left, init)
815 walkexpr(&r.Right, init)
818 if t.Type.Width <= 128 { // Check ../../runtime/hashmap.go:maxValueSize before changing.
819 switch Simsimtype(t.Down) {
820 case TINT32, TUINT32:
821 p = "mapaccess2_fast32"
823 case TINT64, TUINT64:
824 p = "mapaccess2_fast64"
827 p = "mapaccess2_faststr"
833 // fast versions take key by value
836 // standard version takes key by reference
837 // orderexpr made sure key is addressable.
838 key = Nod(OADDR, r.Right, nil)
846 // var,b = mapaccess2*(t, m, i)
851 r = mkcall1(fn, getoutargx(fn.Type), init, typename(t), r.Left, key)
853 // mapaccess2* returns a typed bool, but due to spec changes,
854 // the boolean result of i.(T) is now untyped so we make it the
855 // same type as the variable on the lhs.
856 if !isblank(n.List.Next.N) {
857 r.Type.Type.Down.Type = n.List.Next.N.Type
862 // don't generate a = *var if a is _
864 var_ := temp(Ptrto(t.Type))
868 *init = list(*init, n)
869 n = Nod(OAS, a, Nod(OIND, var_, nil))
875 // TODO: ptr is always non-nil, so disable nil check for this OIND op.
879 *init = concat(*init, n.Ninit)
883 walkexpr(&map_, init)
886 // orderstmt made sure key is addressable.
887 key = Nod(OADDR, key, nil)
890 n = mkcall1(mapfndel("mapdelete", t), nil, init, typename(t), map_, key)
894 e := n.Rlist.N // i.(T)
895 // TODO(rsc): The Isfat is for consistency with componentgen and orderexpr.
896 // It needs to be removed in all three places.
897 // That would allow inlining x.(struct{*int}) the same as x.(*int).
898 if isdirectiface(e.Type) && !Isfat(e.Type) && flag_race == 0 {
899 // handled directly during gen.
900 walkexprlistsafe(n.List, init)
901 walkexpr(&e.Left, init)
906 // orderstmt made sure a is addressable.
907 *init = concat(*init, n.Ninit)
910 walkexprlistsafe(n.List, init)
911 walkexpr(&e.Left, init)
915 oktype := Types[TBOOL]
921 fromKind := type2IET(from.Type)
922 toKind := type2IET(t)
924 // Avoid runtime calls in a few cases of the form _, ok := i.(T).
925 // This is faster and shorter and allows the corresponding assertX2X2
926 // routines to skip nil checks on their last argument.
927 if isblank(n.List.N) {
930 case fromKind == "E" && toKind == "T":
931 tab := Nod(OITAB, from, nil) // type:eface::tab:iface
932 typ := Nod(OCONVNOP, typename(t), nil)
933 typ.Type = Ptrto(Types[TUINTPTR])
934 fast = Nod(OEQ, tab, typ)
935 case fromKind == "I" && toKind == "E",
936 fromKind == "E" && toKind == "E":
937 tab := Nod(OITAB, from, nil)
938 fast = Nod(ONE, nodnil(), tab)
941 if Debug_typeassert > 0 {
942 Warn("type assertion (ok only) inlined")
944 n = Nod(OAS, ok, fast)
950 var resptr *Node // &res
951 if isblank(n.List.N) {
954 resptr = Nod(OADDR, n.List.N, nil)
956 resptr.Etype = 1 // addr does not escape
958 if Debug_typeassert > 0 {
959 Warn("type assertion not inlined")
961 buf := "assert" + fromKind + "2" + toKind + "2"
962 fn := syslook(buf, 1)
963 substArgTypes(fn, from.Type, t)
964 call := mkcall1(fn, oktype, init, typename(t), from, resptr)
965 n = Nod(OAS, ok, call)
969 case ODOTTYPE, ODOTTYPE2:
970 if !isdirectiface(n.Type) || Isfat(n.Type) {
971 Fatalf("walkexpr ODOTTYPE") // should see inside OAS only
973 walkexpr(&n.Left, init)
977 walkexpr(&n.Left, init)
979 // Optimize convT2E as a two-word copy when T is pointer-shaped.
980 if isnilinter(n.Type) && isdirectiface(n.Left.Type) {
981 l := Nod(OEFACE, typename(n.Left.Type), n.Left)
983 l.Typecheck = n.Typecheck
988 // Build name of function: convI2E etc.
989 // Not all names are possible
990 // (e.g., we'll never generate convE2E or convE2I).
991 buf := "conv" + type2IET(n.Left.Type) + "2" + type2IET(n.Type)
992 fn := syslook(buf, 1)
994 if !Isinter(n.Left.Type) {
995 ll = list(ll, typename(n.Left.Type))
997 if !isnilinter(n.Type) {
998 ll = list(ll, typename(n.Type))
1000 if !Isinter(n.Left.Type) && !isnilinter(n.Type) {
1001 sym := Pkglookup(Tconv(n.Left.Type, obj.FmtLeft)+"."+Tconv(n.Type, obj.FmtLeft), itabpkg)
1003 l := Nod(ONAME, nil, nil)
1005 l.Type = Ptrto(Types[TUINT8])
1010 ggloblsym(sym, int32(Widthptr), obj.DUPOK|obj.NOPTR)
1013 l := Nod(OADDR, sym.Def, nil)
1017 if isdirectiface(n.Left.Type) {
1018 /* For pointer types, we can make a special form of optimization
1020 * These statements are put onto the expression init list:
1021 * Itab *tab = atomicloadtype(&cache);
1023 * tab = typ2Itab(type, itype, &cache);
1025 * The CONVIFACE expression is replaced with this:
1028 l := temp(Ptrto(Types[TUINT8]))
1030 n1 := Nod(OAS, l, sym.Def)
1031 typecheck(&n1, Etop)
1032 *init = list(*init, n1)
1034 fn := syslook("typ2Itab", 1)
1035 n1 = Nod(OCALL, fn, nil)
1040 n2 := Nod(OIF, nil, nil)
1041 n2.Left = Nod(OEQ, l, nodnil())
1042 n2.Nbody = list1(Nod(OAS, l, n1))
1044 typecheck(&n2, Etop)
1045 *init = list(*init, n2)
1047 l = Nod(OEFACE, l, n.Left)
1048 l.Typecheck = n.Typecheck
1055 if Isinter(n.Left.Type) {
1056 ll = list(ll, n.Left)
1058 // regular types are passed by reference to avoid C vararg calls
1059 // orderexpr arranged for n.Left to be a temporary for all
1060 // the conversions it could see. comparison of an interface
1061 // with a non-interface, especially in a switch on interface value
1062 // with non-interface cases, is not visible to orderstmt, so we
1063 // have to fall back on allocating a temp here.
1064 if islvalue(n.Left) {
1065 ll = list(ll, Nod(OADDR, n.Left, nil))
1067 ll = list(ll, Nod(OADDR, copyexpr(n.Left, n.Left.Type, init), nil))
1069 dowidth(n.Left.Type)
1071 if n.Esc == EscNone && n.Left.Type.Width <= 1024 {
1072 // Allocate stack buffer for value stored in interface.
1073 r = temp(n.Left.Type)
1074 r = Nod(OAS, r, nil) // zero temp
1076 *init = list(*init, r)
1077 r = Nod(OADDR, r.Left, nil)
1083 if !Isinter(n.Left.Type) {
1084 substArgTypes(fn, n.Left.Type, n.Left.Type, n.Type)
1086 substArgTypes(fn, n.Left.Type, n.Type)
1089 n = Nod(OCALL, fn, nil)
1095 case OCONV, OCONVNOP:
1096 if Thearch.Thechar == '5' {
1097 if Isfloat[n.Left.Type.Etype] {
1098 if n.Type.Etype == TINT64 {
1099 n = mkcall("float64toint64", n.Type, init, conv(n.Left, Types[TFLOAT64]))
1103 if n.Type.Etype == TUINT64 {
1104 n = mkcall("float64touint64", n.Type, init, conv(n.Left, Types[TFLOAT64]))
1109 if Isfloat[n.Type.Etype] {
1110 if n.Left.Type.Etype == TINT64 {
1111 n = mkcall("int64tofloat64", n.Type, init, conv(n.Left, Types[TINT64]))
1115 if n.Left.Type.Etype == TUINT64 {
1116 n = mkcall("uint64tofloat64", n.Type, init, conv(n.Left, Types[TUINT64]))
1122 walkexpr(&n.Left, init)
1126 walkexpr(&n.Left, init)
1128 n.Right = Nod(OCOM, n.Right, nil)
1129 typecheck(&n.Right, Erv)
1130 walkexpr(&n.Right, init)
1134 walkexpr(&n.Left, init)
1135 walkexpr(&n.Right, init)
1140 walkexpr(&n.Left, init)
1141 walkexpr(&n.Right, init)
1144 * rewrite complex div into function call.
1146 et := int(n.Left.Type.Etype)
1148 if Iscomplex[et] && n.Op == ODIV {
1150 n = mkcall("complex128div", Types[TCOMPLEX128], init, conv(n.Left, Types[TCOMPLEX128]), conv(n.Right, Types[TCOMPLEX128]))
1155 // Nothing to do for float divisions.
1160 // Try rewriting as shifts or magic multiplies.
1164 * rewrite 64-bit div and mod into function calls
1165 * on 32-bit architectures.
1169 if Widthreg >= 8 || (et != TUINT64 && et != TINT64) {
1183 n = mkcall(fn, n.Type, init, conv(n.Left, Types[et]), conv(n.Right, Types[et]))
1192 walkexpr(&n.Left, init)
1194 // save the original node for bounds checking elision.
1195 // If it was a ODIV/OMOD walk might rewrite it.
1198 walkexpr(&n.Right, init)
1200 // if range of type cannot exceed static array bound,
1201 // disable bounds check.
1206 if t != nil && Isptr[t.Etype] {
1209 if Isfixedarray(t) {
1210 n.Bounded = bounded(r, t.Bound)
1211 if Debug['m'] != 0 && n.Bounded && !Isconst(n.Right, CTINT) {
1212 Warn("index bounds check elided")
1214 if Smallintconst(n.Right) && !n.Bounded {
1215 Yyerror("index out of bounds")
1217 } else if Isconst(n.Left, CTSTR) {
1218 n.Bounded = bounded(r, int64(len(n.Left.Val().U.(string))))
1219 if Debug['m'] != 0 && n.Bounded && !Isconst(n.Right, CTINT) {
1220 Warn("index bounds check elided")
1222 if Smallintconst(n.Right) {
1224 Yyerror("index out of bounds")
1226 // replace "abc"[1] with 'b'.
1227 // delayed until now because "abc"[1] is not
1228 // an ideal constant.
1229 v := Mpgetfix(n.Right.Val().U.(*Mpint))
1231 Nodconst(n, n.Type, int64(n.Left.Val().U.(string)[v]))
1237 if Isconst(n.Right, CTINT) {
1238 if Mpcmpfixfix(n.Right.Val().U.(*Mpint), &mpzero) < 0 || Mpcmpfixfix(n.Right.Val().U.(*Mpint), Maxintval[TINT]) > 0 {
1239 Yyerror("index out of bounds")
1248 walkexpr(&n.Left, init)
1249 walkexpr(&n.Right, init)
1253 if t.Type.Width <= 128 { // Check ../../runtime/hashmap.go:maxValueSize before changing.
1254 switch Simsimtype(t.Down) {
1255 case TINT32, TUINT32:
1256 p = "mapaccess1_fast32"
1258 case TINT64, TUINT64:
1259 p = "mapaccess1_fast64"
1262 p = "mapaccess1_faststr"
1268 // fast versions take key by value
1271 // standard version takes key by reference.
1272 // orderexpr made sure key is addressable.
1273 key = Nod(OADDR, n.Right, nil)
1278 n = mkcall1(mapfn(p, t), Ptrto(t.Type), init, typename(t), n.Left, key)
1279 n = Nod(OIND, n, nil)
1286 Fatalf("walkexpr ORECV") // should see inside OAS only
1288 case OSLICE, OSLICEARR, OSLICESTR:
1289 walkexpr(&n.Left, init)
1290 walkexpr(&n.Right.Left, init)
1291 if n.Right.Left != nil && iszero(n.Right.Left) {
1292 // Reduce x[0:j] to x[:j].
1295 walkexpr(&n.Right.Right, init)
1299 case OSLICE3, OSLICE3ARR:
1300 walkexpr(&n.Left, init)
1301 walkexpr(&n.Right.Left, init)
1302 if n.Right.Left != nil && iszero(n.Right.Left) {
1303 // Reduce x[0:j:k] to x[:j:k].
1306 walkexpr(&n.Right.Right.Left, init)
1307 walkexpr(&n.Right.Right.Right, init)
1309 r := n.Right.Right.Right
1310 if r != nil && r.Op == OCAP && samesafeexpr(n.Left, r.Left) {
1311 // Reduce x[i:j:cap(x)] to x[i:j].
1312 n.Right.Right = n.Right.Right.Left
1313 if n.Op == OSLICE3 {
1324 walkexpr(&n.Left, init)
1328 if n.Esc == EscNone {
1329 if n.Type.Type.Width >= 1<<16 {
1330 Fatalf("large ONEW with EscNone: %v", n)
1332 r := temp(n.Type.Type)
1333 r = Nod(OAS, r, nil) // zero temp
1335 *init = list(*init, r)
1336 r = Nod(OADDR, r.Left, nil)
1340 n = callnew(n.Type.Type)
1345 // If one argument to the comparison is an empty string,
1346 // comparing the lengths instead will yield the same result
1347 // without the function call.
1349 if (Isconst(n.Left, CTSTR) && len(n.Left.Val().U.(string)) == 0) || (Isconst(n.Right, CTSTR) && len(n.Right.Val().U.(string)) == 0) {
1350 r := Nod(int(n.Etype), Nod(OLEN, n.Left, nil), Nod(OLEN, n.Right, nil))
1358 // s + "badgerbadgerbadger" == "badgerbadgerbadger"
1359 if (n.Etype == OEQ || 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) {
1360 r := Nod(int(n.Etype), Nod(OLEN, n.Left.List.N, nil), Nodintconst(0))
1369 if n.Etype == OEQ || n.Etype == ONE {
1370 // prepare for rewrite below
1371 n.Left = cheapexpr(n.Left, init)
1373 n.Right = cheapexpr(n.Right, init)
1375 r = mkcall("eqstring", Types[TBOOL], init, conv(n.Left, Types[TSTRING]), conv(n.Right, Types[TSTRING]))
1377 // quick check of len before full compare for == or !=
1378 // eqstring assumes that the lengths are equal
1380 // len(left) == len(right) && eqstring(left, right)
1381 r = Nod(OANDAND, Nod(OEQ, Nod(OLEN, n.Left, nil), Nod(OLEN, n.Right, nil)), r)
1383 // len(left) != len(right) || !eqstring(left, right)
1384 r = Nod(ONOT, r, nil)
1386 r = Nod(OOROR, Nod(ONE, Nod(OLEN, n.Left, nil), Nod(OLEN, n.Right, nil)), r)
1392 // sys_cmpstring(s1, s2) :: 0
1393 r = mkcall("cmpstring", Types[TINT], init, conv(n.Left, Types[TSTRING]), conv(n.Right, Types[TSTRING]))
1395 r = Nod(int(n.Etype), r, Nodintconst(0))
1399 if n.Type.Etype != TBOOL {
1400 Fatalf("cmp %v", n.Type)
1411 // order should make sure we only see OAS(node, OAPPEND), which we handle above.
1412 Fatalf("append outside assignment")
1415 n = copyany(n, init, flag_race)
1418 // cannot use chanfn - closechan takes any, not chan any
1420 fn := syslook("closechan", 1)
1422 substArgTypes(fn, n.Left.Type)
1423 n = mkcall1(fn, nil, init, n.Left)
1427 n = mkcall1(chanfn("makechan", 1, n.Type), n.Type, init, typename(n.Type), conv(n.Left, Types[TINT64]))
1433 fn := syslook("makemap", 1)
1435 a := nodnil() // hmap buffer
1436 r := nodnil() // bucket buffer
1437 if n.Esc == EscNone {
1438 // Allocate hmap buffer on stack.
1439 var_ := temp(hmap(t))
1441 a = Nod(OAS, var_, nil) // zero temp
1443 *init = list(*init, a)
1444 a = Nod(OADDR, var_, nil)
1446 // Allocate one bucket on stack.
1447 // Maximum key/value size is 128 bytes, larger objects
1448 // are stored with an indirection. So max bucket size is 2048+eps.
1449 var_ = temp(mapbucket(t))
1451 r = Nod(OAS, var_, nil) // zero temp
1453 *init = list(*init, r)
1454 r = Nod(OADDR, var_, nil)
1457 substArgTypes(fn, hmap(t), mapbucket(t), t.Down, t.Type)
1458 n = mkcall1(fn, n.Type, init, typename(n.Type), conv(n.Left, Types[TINT64]), a, r)
1465 r = safeexpr(l, init)
1469 if n.Esc == EscNone {
1470 if !isSmallMakeSlice(n) {
1471 Fatalf("non-small OMAKESLICE with EscNone: %v", n)
1475 t = aindex(r, t.Type) // [r]T
1477 a := Nod(OAS, var_, nil) // zero temp
1479 *init = list(*init, a)
1480 r := Nod(OSLICE, var_, Nod(OKEY, nil, l)) // arr[:l]
1481 r = conv(r, n.Type) // in case n.Type is named.
1486 // makeslice(t *Type, nel int64, max int64) (ary []any)
1487 fn := syslook("makeslice", 1)
1489 substArgTypes(fn, t.Type) // any-1
1490 n = mkcall1(fn, n.Type, init, typename(n.Type), conv(l, Types[TINT64]), conv(r, Types[TINT64]))
1497 if n.Esc == EscNone {
1498 t := aindex(Nodintconst(4), Types[TUINT8])
1500 a = Nod(OADDR, var_, nil)
1503 // intstring(*[4]byte, rune)
1504 n = mkcall("intstring", n.Type, init, a, conv(n.Left, Types[TINT64]))
1510 if n.Esc == EscNone {
1511 // Create temporary buffer for string on stack.
1512 t := aindex(Nodintconst(tmpstringbufsize), Types[TUINT8])
1514 a = Nod(OADDR, temp(t), nil)
1517 // slicebytetostring(*[32]byte, []byte) string;
1518 n = mkcall("slicebytetostring", n.Type, init, a, n.Left)
1522 // slicebytetostringtmp([]byte) string;
1523 case OARRAYBYTESTRTMP:
1524 n = mkcall("slicebytetostringtmp", n.Type, init, n.Left)
1528 // slicerunetostring(*[32]byte, []rune) string;
1532 if n.Esc == EscNone {
1533 // Create temporary buffer for string on stack.
1534 t := aindex(Nodintconst(tmpstringbufsize), Types[TUINT8])
1536 a = Nod(OADDR, temp(t), nil)
1539 n = mkcall("slicerunetostring", n.Type, init, a, n.Left)
1542 // stringtoslicebyte(*32[byte], string) []byte;
1546 if n.Esc == EscNone {
1547 // Create temporary buffer for slice on stack.
1548 t := aindex(Nodintconst(tmpstringbufsize), Types[TUINT8])
1550 a = Nod(OADDR, temp(t), nil)
1553 n = mkcall("stringtoslicebyte", n.Type, init, a, conv(n.Left, Types[TSTRING]))
1556 // stringtoslicebytetmp(string) []byte;
1557 case OSTRARRAYBYTETMP:
1558 n = mkcall("stringtoslicebytetmp", n.Type, init, conv(n.Left, Types[TSTRING]))
1562 // stringtoslicerune(*[32]rune, string) []rune
1566 if n.Esc == EscNone {
1567 // Create temporary buffer for slice on stack.
1568 t := aindex(Nodintconst(tmpstringbufsize), Types[TINT32])
1570 a = Nod(OADDR, temp(t), nil)
1573 n = mkcall("stringtoslicerune", n.Type, init, a, n.Left)
1576 // ifaceeq(i1 any-1, i2 any-2) (ret bool);
1578 if !Eqtype(n.Left.Type, n.Right.Type) {
1579 Fatalf("ifaceeq %v %v %v", Oconv(int(n.Op), 0), n.Left.Type, n.Right.Type)
1582 if isnilinter(n.Left.Type) {
1583 fn = syslook("efaceeq", 1)
1585 fn = syslook("ifaceeq", 1)
1588 n.Right = cheapexpr(n.Right, init)
1589 n.Left = cheapexpr(n.Left, init)
1590 substArgTypes(fn, n.Right.Type, n.Left.Type)
1591 r := mkcall1(fn, n.Type, init, n.Left, n.Right)
1593 r = Nod(ONOT, r, nil)
1596 // check itable/type before full compare.
1598 r = Nod(OANDAND, Nod(OEQ, Nod(OITAB, n.Left, nil), Nod(OITAB, n.Right, nil)), r)
1600 r = Nod(OOROR, Nod(ONE, Nod(OITAB, n.Left, nil), Nod(OITAB, n.Right, nil)), r)
1608 case OARRAYLIT, OMAPLIT, OSTRUCTLIT, OPTRLIT:
1609 var_ := temp(n.Type)
1610 anylit(0, n, var_, init)
1616 n1 = assignconv(n1, n.Left.Type.Type, "chan send")
1618 n1 = Nod(OADDR, n1, nil)
1619 n = mkcall1(chanfn("chansend1", 2, n.Left.Type), nil, init, typename(n.Left.Type), n.Left, n1)
1623 n = walkclosure(n, init)
1627 n = walkpartialcall(n, init)
1631 Fatalf("missing switch %v", Oconv(int(n.Op), 0))
1633 // Expressions that are constant at run time but not
1634 // considered const by the language spec are not turned into
1635 // constants until walk. For example, if n is y%1 == 0, the
1636 // walk of y%1 may have replaced it by 0.
1637 // Check whether n with its updated args is itself now a constant.
1643 if n.Op == OLITERAL {
1649 if Debug['w'] != 0 && n != nil {
1657 func reduceSlice(n *Node) *Node {
1659 if r != nil && r.Op == OLEN && samesafeexpr(n.Left, r.Left) {
1660 // Reduce x[i:len(x)] to x[i:].
1663 if (n.Op == OSLICE || n.Op == OSLICESTR) && n.Right.Left == nil && n.Right.Right == nil {
1664 // Reduce x[:] to x.
1665 if Debug_slice > 0 {
1666 Warn("slice: omit slice operation")
1673 func ascompatee1(op int, l *Node, r *Node, init **NodeList) *Node {
1674 // convas will turn map assigns into function calls,
1675 // making it impossible for reorder3 to work.
1678 if l.Op == OINDEXMAP {
1682 return convas(n, init)
1685 func ascompatee(op int, nl *NodeList, nr *NodeList, init **NodeList) *NodeList {
1687 * check assign expression list to
1688 * a expression list. called in
1689 * expr-list = expr-list
1692 // ensure order of evaluation for function calls
1693 for ll := nl; ll != nil; ll = ll.Next {
1694 ll.N = safeexpr(ll.N, init)
1696 for lr := nr; lr != nil; lr = lr.Next {
1697 lr.N = safeexpr(lr.N, init)
1703 for ; ll != nil && lr != nil; ll, lr = ll.Next, lr.Next {
1704 // Do not generate 'x = x' during return. See issue 4014.
1705 if op == ORETURN && ll.N == lr.N {
1708 nn = list(nn, ascompatee1(op, ll.N, lr.N, init))
1711 // cannot happen: caller checked that lists had same length
1712 if ll != nil || lr != nil {
1713 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)
1719 * l is an lv and rt is the type of an rv
1720 * return 1 if this implies a function call
1721 * evaluating the lv or a function call
1722 * in the conversion of the types
1724 func fncall(l *Node, rt *Type) bool {
1725 if l.Ullman >= UINF || l.Op == OINDEXMAP {
1729 if needwritebarrier(l, &r) {
1732 if Eqtype(l.Type, rt) {
1738 func ascompatet(op int, nl *NodeList, nr **Type, fp int, init **NodeList) *NodeList {
1746 * check assign type list to
1747 * a expression list. called in
1748 * expr-list = func()
1750 r := Structfirst(&saver, nr)
1755 for ll = nl; ll != nil; ll = ll.Next {
1761 r = structnext(&saver)
1765 // any lv that causes a fn call must be
1766 // deferred until all the return arguments
1767 // have been pulled from the output arguments
1768 if fncall(l, r.Type) {
1770 typecheck(&tmp, Erv)
1771 a = Nod(OAS, l, tmp)
1777 a = Nod(OAS, l, nodarg(r, fp))
1780 if a.Ullman >= UINF {
1781 Dump("ascompatet ucount", a)
1786 r = structnext(&saver)
1789 if ll != nil || r != nil {
1790 Yyerror("ascompatet: assignment count mismatch: %d = %d", count(nl), structcount(*nr))
1794 Fatalf("ascompatet: too many function calls evaluating parameters")
1796 return concat(nn, mm)
1800 * package all the arguments that match a ... T parameter into a []T.
1802 func mkdotargslice(lr0 *NodeList, nn *NodeList, l *Type, fp int, init **NodeList, ddd *Node) *NodeList {
1803 esc := uint16(EscUnknown)
1808 tslice := typ(TARRAY)
1809 tslice.Type = l.Type.Type
1813 if count(lr0) == 0 {
1817 n = Nod(OCOMPLIT, nil, typenod(tslice))
1818 if ddd != nil && prealloc[ddd] != nil {
1819 prealloc[n] = prealloc[ddd] // temporary to use
1825 Fatalf("mkdotargslice: typecheck failed")
1830 a := Nod(OAS, nodarg(l, fp), n)
1831 nn = list(nn, convas(a, init))
1836 * helpers for shape errors
1838 func dumptypes(nl **Type, what string) string {
1844 for l := Structfirst(&savel, nl); l != nil; l = structnext(&savel) {
1854 fmt_ += fmt.Sprintf("[no arguments %s]", what)
1859 func dumpnodetypes(l *NodeList, what string) string {
1865 for ; l != nil; l = l.Next {
1872 fmt_ += Tconv(r.Type, 0)
1876 fmt_ += fmt.Sprintf("[no arguments %s]", what)
1882 * check assign expression list to
1883 * a type list. called in
1887 func ascompatte(op int, call *Node, isddd bool, nl **Type, lr *NodeList, fp int, init **NodeList) *NodeList {
1891 l := Structfirst(&savel, nl)
1898 // f(g()) where g has multiple return values
1903 if r != nil && lr.Next == nil && r.Type.Etype == TSTRUCT && r.Type.Funarg {
1904 // optimization - can do block copy
1905 if eqtypenoname(r.Type, *nl) {
1906 a := nodarg(*nl, fp)
1907 r = Nod(OCONVNOP, r, nil)
1909 nn = list1(convas(Nod(OAS, a, r), init))
1913 // conversions involved.
1914 // copy into temporaries.
1917 for l := Structfirst(&savel, &r.Type); l != nil; l = structnext(&savel) {
1919 alist = list(alist, a)
1922 a = Nod(OAS2, nil, nil)
1927 *init = list(*init, a)
1930 l = Structfirst(&savel, nl)
1934 if l != nil && l.Isddd {
1935 // the ddd parameter must be last
1936 ll = structnext(&savel)
1939 Yyerror("... must be last argument")
1943 // only if we are assigning a single ddd
1944 // argument to a ddd parameter then it is
1945 // passed thru unencapsulated
1946 if r != nil && lr.Next == nil && isddd && Eqtype(l.Type, r.Type) {
1947 a = Nod(OAS, nodarg(l, fp), r)
1953 // normal case -- make a slice of all
1954 // remaining arguments and pass it to
1955 // the ddd parameter.
1956 nn = mkdotargslice(lr, nn, l, fp, init, call.Right)
1961 if l == nil || r == nil {
1962 if l != nil || r != nil {
1963 l1 = dumptypes(nl, "expected")
1964 l2 = dumpnodetypes(lr0, "given")
1966 Yyerror("not enough arguments to %v\n%s\n%s", Oconv(int(op), 0), l1, l2)
1968 Yyerror("too many arguments to %v\n%s\n%s", Oconv(int(op), 0), l1, l2)
1975 a = Nod(OAS, nodarg(l, fp), r)
1979 l = structnext(&savel)
1988 for lr = nn; lr != nil; lr = lr.Next {
1994 // generate code for print
1995 func walkprint(nn *Node, init **NodeList) *Node {
2007 // Hoist all the argument evaluation up before the lock.
2008 walkexprlistcheap(all, init)
2010 calls = list(calls, mkcall("printlock", nil, init))
2012 for l := all; l != nil; l = l.Next {
2014 calls = list(calls, mkcall("printsp", nil, init))
2017 notfirst = op == OPRINTN
2020 if n.Op == OLITERAL {
2021 switch n.Val().Ctype() {
2023 defaultlit(&n, runetype)
2026 defaultlit(&n, Types[TINT64])
2029 defaultlit(&n, Types[TFLOAT64])
2033 if n.Op != OLITERAL && n.Type != nil && n.Type.Etype == TIDEAL {
2034 defaultlit(&n, Types[TINT64])
2038 if n.Type == nil || n.Type.Etype == TFORW {
2043 et = int(n.Type.Etype)
2044 if Isinter(n.Type) {
2045 if isnilinter(n.Type) {
2046 on = syslook("printeface", 1)
2048 on = syslook("printiface", 1)
2050 substArgTypes(on, n.Type) // any-1
2051 } else if Isptr[et] || et == TCHAN || et == TMAP || et == TFUNC || et == TUNSAFEPTR {
2052 on = syslook("printpointer", 1)
2053 substArgTypes(on, n.Type) // any-1
2054 } else if Isslice(n.Type) {
2055 on = syslook("printslice", 1)
2056 substArgTypes(on, n.Type) // any-1
2057 } else if Isint[et] {
2059 if (t.Sym.Pkg == Runtimepkg || compiling_runtime != 0) && t.Sym.Name == "hex" {
2060 on = syslook("printhex", 0)
2062 on = syslook("printuint", 0)
2065 on = syslook("printint", 0)
2067 } else if Isfloat[et] {
2068 on = syslook("printfloat", 0)
2069 } else if Iscomplex[et] {
2070 on = syslook("printcomplex", 0)
2071 } else if et == TBOOL {
2072 on = syslook("printbool", 0)
2073 } else if et == TSTRING {
2074 on = syslook("printstring", 0)
2076 badtype(OPRINT, n.Type, nil)
2080 t = *getinarg(on.Type)
2088 if !Eqtype(t, n.Type) {
2089 n = Nod(OCONV, n, nil)
2093 r = Nod(OCALL, on, nil)
2095 calls = list(calls, r)
2099 calls = list(calls, mkcall("printnl", nil, nil))
2102 calls = list(calls, mkcall("printunlock", nil, init))
2104 typechecklist(calls, Etop)
2105 walkexprlist(calls, init)
2107 r = Nod(OEMPTY, nil, nil)
2114 func callnew(t *Type) *Node {
2116 fn := syslook("newobject", 1)
2117 substArgTypes(fn, t)
2118 return mkcall1(fn, Ptrto(t), nil, typename(t))
2121 func iscallret(n *Node) bool {
2123 return n.Op == OINDREG && n.Reg == int16(Thearch.REGSP)
2126 func isstack(n *Node) bool {
2129 // If n is *autotmp and autotmp = &foo, replace n with foo.
2130 // We introduce such temps when initializing struct literals.
2131 if n.Op == OIND && n.Left.Op == ONAME && strings.HasPrefix(n.Left.Sym.Name, "autotmp_") {
2132 defn := n.Left.Name.Defn
2133 if defn != nil && defn.Op == OAS && defn.Right.Op == OADDR {
2140 return n.Reg == int16(Thearch.REGSP)
2144 case PAUTO, PPARAM, PPARAMOUT:
2152 func isglobal(n *Node) bool {
2166 // Do we need a write barrier for the assignment l = r?
2167 func needwritebarrier(l *Node, r *Node) bool {
2168 if use_writebarrier == 0 {
2172 if l == nil || isblank(l) {
2176 // No write barrier for write of non-pointers.
2179 if !haspointers(l.Type) {
2183 // No write barrier for write to stack.
2188 // No write barrier for implicit zeroing.
2193 // Ignore no-op conversions when making decision.
2194 // Ensures that xp = unsafe.Pointer(&x) is treated
2195 // the same as xp = &x.
2196 for r.Op == OCONVNOP {
2200 // No write barrier for zeroing or initialization to constant.
2201 if iszero(r) || r.Op == OLITERAL {
2205 // No write barrier for storing static (read-only) data.
2206 if r.Op == ONAME && strings.HasPrefix(r.Sym.Name, "statictmp_") {
2210 // No write barrier for storing address of stack values,
2211 // which are guaranteed only to be written to the stack.
2212 if r.Op == OADDR && isstack(r.Left) {
2216 // No write barrier for storing address of global, which
2217 // is live no matter what.
2218 if r.Op == OADDR && isglobal(r.Left) {
2222 // Otherwise, be conservative and use write barrier.
2226 // TODO(rsc): Perhaps componentgen should run before this.
2228 func applywritebarrier(n *Node, init **NodeList) *Node {
2229 if n.Left != nil && n.Right != nil && needwritebarrier(n.Left, n.Right) {
2231 Warnl(int(n.Lineno), "marking %v for barrier", Nconv(n.Left, 0))
2239 func convas(n *Node, init **NodeList) *Node {
2241 Fatalf("convas: not OAS %v", Oconv(int(n.Op), 0))
2248 if n.Left == nil || n.Right == nil {
2254 if lt == nil || rt == nil {
2258 if isblank(n.Left) {
2259 defaultlit(&n.Right, nil)
2263 if n.Left.Op == OINDEXMAP {
2267 walkexpr(&map_, init)
2268 walkexpr(&key, init)
2269 walkexpr(&val, init)
2271 // orderexpr made sure key and val are addressable.
2272 key = Nod(OADDR, key, nil)
2274 val = Nod(OADDR, val, nil)
2275 n = mkcall1(mapfn("mapassign1", map_.Type), nil, init, typename(map_.Type), map_, key, val)
2279 if !Eqtype(lt, rt) {
2280 n.Right = assignconv(n.Right, lt, "assignment")
2281 walkexpr(&n.Right, init)
2291 * evaluating actual function arguments.
2293 * if there is exactly one function expr,
2294 * then it is done first. otherwise must
2295 * make temp variables
2297 func reorder1(all *NodeList) *NodeList {
2300 c := 0 // function calls
2301 t := 0 // total parameters
2303 for l := all; l != nil; l = l.Next {
2307 if n.Ullman >= UINF {
2312 if c == 0 || t == 1 {
2316 var g *NodeList // fncalls assigned to tempnames
2317 var f *Node // last fncall assigned to stack
2318 var r *NodeList // non fncalls and tempnames assigned to stack
2321 for l := all; l != nil; l = l.Next {
2323 if n.Ullman < UINF {
2334 // make assignment of fncall to tempname
2335 a = temp(n.Right.Type)
2337 a = Nod(OAS, a, n.Right)
2340 // put normal arg assignment on list
2341 // with fncall replaced by tempname
2356 * simultaneous assignment. there cannot
2357 * be later use of an earlier lvalue.
2359 * function calls have been removed.
2361 func reorder3(all *NodeList) *NodeList {
2364 // If a needed expression may be affected by an
2365 // earlier assignment, make an early copy of that
2366 // expression and use the copy instead.
2369 var mapinit *NodeList
2370 for list := all; list != nil; list = list.Next {
2373 // Save subexpressions needed on left side.
2374 // Drill through non-dereferences.
2376 if l.Op == ODOT || l.Op == OPAREN {
2381 if l.Op == OINDEX && Isfixedarray(l.Left.Type) {
2382 reorder3save(&l.Right, all, list, &early)
2392 Fatalf("reorder3 unexpected lvalue %v", Oconv(int(l.Op), obj.FmtSharp))
2397 case OINDEX, OINDEXMAP:
2398 reorder3save(&l.Left, all, list, &early)
2399 reorder3save(&l.Right, all, list, &early)
2400 if l.Op == OINDEXMAP {
2401 list.N = convas(list.N, &mapinit)
2405 reorder3save(&l.Left, all, list, &early)
2408 // Save expression on right side.
2409 reorder3save(&list.N.Right, all, list, &early)
2412 early = concat(mapinit, early)
2413 return concat(early, all)
2417 * if the evaluation of *np would be affected by the
2418 * assignments in all up to but not including stop,
2419 * copy into a temporary during *early and
2420 * replace *np with that temp.
2422 func reorder3save(np **Node, all *NodeList, stop *NodeList, early **NodeList) {
2424 if !aliased(n, all, stop) {
2431 *early = list(*early, q)
2436 * what's the outer value that a write to n affects?
2437 * outer value means containing struct or array.
2439 func outervalue(n *Node) *Node {
2442 Fatalf("OXDOT in walk")
2444 if n.Op == ODOT || n.Op == OPAREN || n.Op == OCONVNOP {
2449 if n.Op == OINDEX && Isfixedarray(n.Left.Type) {
2461 * Is it possible that the computation of n might be
2462 * affected by writes in as up to but not including stop?
2464 func aliased(n *Node, all *NodeList, stop *NodeList) bool {
2469 // Look for obvious aliasing: a variable being assigned
2470 // during the all list and appearing in n.
2471 // Also record whether there are any writes to main memory.
2472 // Also record whether there are any writes to variables
2473 // whose addresses have been taken.
2478 for l := all; l != stop; l = l.Next {
2479 a = outervalue(l.N.Left)
2490 case PAUTO, PPARAM, PPARAMOUT:
2503 // The variables being written do not appear in n.
2504 // However, n might refer to computed addresses
2505 // that are being written.
2507 // If no computed addresses are affected by the writes, no aliasing.
2508 if memwrite == 0 && varwrite == 0 {
2512 // If n does not refer to computed addresses
2513 // (that is, if n only refers to variables whose addresses
2514 // have not been taken), no aliasing.
2519 // Otherwise, both the writes and n refer to computed memory addresses.
2520 // Assume that they might conflict.
2525 * does the evaluation of n only refer to variables
2526 * whose addresses have not been taken?
2527 * (and no other memory)
2529 func varexpr(n *Node) bool {
2540 case PAUTO, PPARAM, PPARAMOUT:
2565 ODOT, // but not ODOTPTR
2570 return varexpr(n.Left) && varexpr(n.Right)
2578 * is the name l mentioned in r?
2580 func vmatch2(l *Node, r *Node) bool {
2585 // match each right given left
2593 if vmatch2(l, r.Left) {
2596 if vmatch2(l, r.Right) {
2599 for ll := r.List; ll != nil; ll = ll.Next {
2600 if vmatch2(l, ll.N) {
2608 * is any name mentioned in l also mentioned in r?
2609 * called by sinit.go
2611 func vmatch1(l *Node, r *Node) bool {
2613 * isolate all left sides
2615 if l == nil || r == nil {
2621 case PPARAM, PPARAMREF, PAUTO:
2624 // assignment to non-stack variable
2625 // must be delayed if right has function calls.
2627 if r.Ullman >= UINF {
2632 return vmatch2(l, r)
2638 if vmatch1(l.Left, r) {
2641 if vmatch1(l.Right, r) {
2644 for ll := l.List; ll != nil; ll = ll.Next {
2645 if vmatch1(ll.N, r) {
2653 * walk through argin parameters.
2654 * generate and return code to allocate
2655 * copies of escaped parameters to the heap.
2657 func paramstoheap(argin **Type, out int) *NodeList {
2663 for t := Structfirst(&savet, argin); t != nil; t = structnext(&savet) {
2665 if v != nil && v.Sym != nil && v.Sym.Name[0] == '~' && v.Sym.Name[1] == 'r' { // unnamed result
2669 // For precise stacks, the garbage collector assumes results
2670 // are always live, so zero them always.
2672 // Defer might stop a panic and show the
2673 // return values as they exist at the time of panic.
2674 // Make sure to zero them on entry to the function.
2675 nn = list(nn, Nod(OAS, nodarg(t, -1), nil))
2678 if v == nil || v.Class&PHEAP == 0 {
2682 // generate allocation & copying code
2683 if compiling_runtime != 0 {
2684 Yyerror("%v escapes to heap, not allowed in runtime.", v)
2686 if prealloc[v] == nil {
2687 prealloc[v] = callnew(v.Type)
2689 nn = list(nn, Nod(OAS, v.Name.Heapaddr, prealloc[v]))
2690 if v.Class&^PHEAP != PPARAMOUT {
2691 as = Nod(OAS, v, v.Name.Param.Stackparam)
2692 v.Name.Param.Stackparam.Typecheck = 1
2693 typecheck(&as, Etop)
2694 as = applywritebarrier(as, &nn)
2703 * walk through argout parameters copying back to stack
2705 func returnsfromheap(argin **Type) *NodeList {
2710 for t := Structfirst(&savet, argin); t != nil; t = structnext(&savet) {
2712 if v == nil || v.Class != PHEAP|PPARAMOUT {
2715 nn = list(nn, Nod(OAS, v.Name.Param.Stackparam, v))
2722 * take care of migrating any function in/out args
2723 * between the stack and the heap. adds code to
2724 * curfn's before and after lists.
2728 lineno = Curfn.Lineno
2729 nn := paramstoheap(getthis(Curfn.Type), 0)
2730 nn = concat(nn, paramstoheap(getinarg(Curfn.Type), 0))
2731 nn = concat(nn, paramstoheap(Getoutarg(Curfn.Type), 1))
2732 Curfn.Func.Enter = concat(Curfn.Func.Enter, nn)
2733 lineno = Curfn.Func.Endlineno
2734 Curfn.Func.Exit = returnsfromheap(Getoutarg(Curfn.Type))
2738 func vmkcall(fn *Node, t *Type, init **NodeList, va []*Node) *Node {
2739 if fn.Type == nil || fn.Type.Etype != TFUNC {
2740 Fatalf("mkcall %v %v", fn, fn.Type)
2744 n := fn.Type.Intuple
2745 for i := 0; i < n; i++ {
2746 args = list(args, va[i])
2749 r := Nod(OCALL, fn, nil)
2751 if fn.Type.Outtuple > 0 {
2752 typecheck(&r, Erv|Efnstruct)
2761 func mkcall(name string, t *Type, init **NodeList, args ...*Node) *Node {
2762 return vmkcall(syslook(name, 0), t, init, args)
2765 func mkcall1(fn *Node, t *Type, init **NodeList, args ...*Node) *Node {
2766 return vmkcall(fn, t, init, args)
2769 func conv(n *Node, t *Type) *Node {
2770 if Eqtype(n.Type, t) {
2773 n = Nod(OCONV, n, nil)
2779 func chanfn(name string, n int, t *Type) *Node {
2780 if t.Etype != TCHAN {
2781 Fatalf("chanfn %v", t)
2783 fn := syslook(name, 1)
2786 Fatalf("chanfn %d", n)
2788 substArgTypes(fn, t.Type)
2790 substArgTypes(fn, t.Type, t.Type)
2795 func mapfn(name string, t *Type) *Node {
2796 if t.Etype != TMAP {
2797 Fatalf("mapfn %v", t)
2799 fn := syslook(name, 1)
2800 substArgTypes(fn, t.Down, t.Type, t.Down, t.Type)
2804 func mapfndel(name string, t *Type) *Node {
2805 if t.Etype != TMAP {
2806 Fatalf("mapfn %v", t)
2808 fn := syslook(name, 1)
2809 substArgTypes(fn, t.Down, t.Type, t.Down)
2813 func writebarrierfn(name string, l *Type, r *Type) *Node {
2814 fn := syslook(name, 1)
2815 substArgTypes(fn, l, r)
2819 func addstr(n *Node, init **NodeList) *Node {
2820 // orderexpr rewrote OADDSTR to have a list of strings.
2824 Yyerror("addstr count %d too small", c)
2828 if n.Esc == EscNone {
2830 for l := n.List; l != nil; l = l.Next {
2831 if n.Op == OLITERAL {
2832 sz += int64(len(n.Val().U.(string)))
2836 // Don't allocate the buffer if the result won't fit.
2837 if sz < tmpstringbufsize {
2838 // Create temporary buffer for result string on stack.
2839 t := aindex(Nodintconst(tmpstringbufsize), Types[TUINT8])
2841 buf = Nod(OADDR, temp(t), nil)
2845 // build list of string arguments
2848 for l := n.List; l != nil; l = l.Next {
2849 args = list(args, conv(l.N, Types[TSTRING]))
2854 // small numbers of strings use direct runtime helpers.
2855 // note: orderexpr knows this cutoff too.
2856 fn = fmt.Sprintf("concatstring%d", c)
2858 // large numbers of strings are passed to the runtime as a slice.
2859 fn = "concatstrings"
2862 t.Type = Types[TSTRING]
2864 slice := Nod(OCOMPLIT, nil, typenod(t))
2865 if prealloc[n] != nil {
2866 prealloc[slice] = prealloc[n]
2868 slice.List = args.Next // skip buf arg
2870 args = list(args, slice)
2874 cat := syslook(fn, 1)
2875 r := Nod(OCALL, cat, nil)
2884 // expand append(l1, l2...) to
2887 // if n := len(l1) + len(l2) - cap(s); n > 0 {
2888 // s = growslice_n(s, n)
2890 // s = s[:len(l1)+len(l2)]
2891 // memmove(&s[len(l1)], &l2[0], len(l2)*sizeof(T))
2895 // l2 is allowed to be a string.
2896 func appendslice(n *Node, init **NodeList) *Node {
2897 walkexprlistsafe(n.List, init)
2899 // walkexprlistsafe will leave OINDEX (s[n]) alone if both s
2900 // and n are name or literal, but those may index the slice we're
2901 // modifying here. Fix explicitly.
2902 for l := n.List; l != nil; l = l.Next {
2903 l.N = cheapexpr(l.N, init)
2909 s := temp(l1.Type) // var s []T
2911 l = list(l, Nod(OAS, s, l1)) // s = l1
2913 nt := temp(Types[TINT])
2915 nif := Nod(OIF, nil, nil)
2917 // n := len(s) + len(l2) - cap(s)
2918 nif.Ninit = list1(Nod(OAS, nt, Nod(OSUB, Nod(OADD, Nod(OLEN, s, nil), Nod(OLEN, l2, nil)), Nod(OCAP, s, nil))))
2920 nif.Left = Nod(OGT, nt, Nodintconst(0))
2922 // instantiate growslice_n(Type*, []any, int) []any
2923 fn := syslook("growslice_n", 1) // growslice_n(<type>, old []T, n int64) (ret []T)
2924 substArgTypes(fn, s.Type.Type, s.Type.Type)
2926 // s = growslice_n(T, s, n)
2927 nif.Nbody = list1(Nod(OAS, s, mkcall1(fn, s.Type, &nif.Ninit, typename(s.Type), s, nt)))
2931 if haspointers(l1.Type.Type) {
2932 // copy(s[len(l1):len(l1)+len(l2)], l2)
2933 nptr1 := Nod(OSLICE, s, Nod(OKEY, Nod(OLEN, l1, nil), Nod(OADD, Nod(OLEN, l1, nil), Nod(OLEN, l2, nil))))
2937 fn := syslook("typedslicecopy", 1)
2938 substArgTypes(fn, l1.Type, l2.Type)
2939 nt := mkcall1(fn, Types[TINT], &l, typename(l1.Type.Type), nptr1, nptr2)
2941 } else if flag_race != 0 {
2942 // rely on runtime to instrument copy.
2943 // copy(s[len(l1):len(l1)+len(l2)], l2)
2944 nptr1 := Nod(OSLICE, s, Nod(OKEY, Nod(OLEN, l1, nil), Nod(OADD, Nod(OLEN, l1, nil), Nod(OLEN, l2, nil))))
2949 if l2.Type.Etype == TSTRING {
2950 fn = syslook("slicestringcopy", 1)
2952 fn = syslook("slicecopy", 1)
2954 substArgTypes(fn, l1.Type, l2.Type)
2955 nt := mkcall1(fn, Types[TINT], &l, nptr1, nptr2, Nodintconst(s.Type.Type.Width))
2958 // memmove(&s[len(l1)], &l2[0], len(l2)*sizeof(T))
2959 nptr1 := Nod(OINDEX, s, Nod(OLEN, l1, nil))
2961 nptr1.Bounded = true
2962 nptr1 = Nod(OADDR, nptr1, nil)
2964 nptr2 := Nod(OSPTR, l2, nil)
2966 fn := syslook("memmove", 1)
2967 substArgTypes(fn, s.Type.Type, s.Type.Type)
2969 nwid := cheapexpr(conv(Nod(OLEN, l2, nil), Types[TUINTPTR]), &l)
2971 nwid = Nod(OMUL, nwid, Nodintconst(s.Type.Type.Width))
2972 nt := mkcall1(fn, nil, &l, nptr1, nptr2, nwid)
2976 // s = s[:len(l1)+len(l2)]
2977 nt = Nod(OADD, Nod(OLEN, l1, nil), Nod(OLEN, l2, nil))
2979 nt = Nod(OSLICE, s, Nod(OKEY, nil, nt))
2981 l = list(l, Nod(OAS, s, nt))
2983 typechecklist(l, Etop)
2985 *init = concat(*init, l)
2989 // Rewrite append(src, x, y, z) so that any side effects in
2990 // x, y, z (including runtime panics) are evaluated in
2991 // initialization statements before the append.
2992 // For normal code generation, stop there and leave the
2993 // rest to cgen_append.
2995 // For race detector, expand append(src, a [, b]* ) to
2999 // const argc = len(args) - 1
3000 // if cap(s) - len(s) < argc {
3001 // s = growslice(s, len(s)+argc)
3010 func walkappend(n *Node, init **NodeList, dst *Node) *Node {
3011 if !samesafeexpr(dst, n.List.N) {
3013 l.N = safeexpr(l.N, init)
3014 walkexpr(&l.N, init)
3016 walkexprlistsafe(n.List.Next, init)
3018 // walkexprlistsafe will leave OINDEX (s[n]) alone if both s
3019 // and n are name or literal, but those may index the slice we're
3020 // modifying here. Fix explicitly.
3021 // Using cheapexpr also makes sure that the evaluation
3022 // of all arguments (and especially any panics) happen
3023 // before we begin to modify the slice in a visible way.
3024 for l := n.List.Next; l != nil; l = l.Next {
3025 l.N = cheapexpr(l.N, init)
3030 // Resolve slice type of multi-valued return.
3031 if Istype(nsrc.Type, TSTRUCT) {
3032 nsrc.Type = nsrc.Type.Type.Type
3034 argc := count(n.List) - 1
3039 // General case, with no function calls left as arguments.
3040 // Leave for gen, except that race detector requires old form
3047 ns := temp(nsrc.Type)
3048 l = list(l, Nod(OAS, ns, nsrc)) // s = src
3050 na := Nodintconst(int64(argc)) // const argc
3051 nx := Nod(OIF, nil, nil) // if cap(s) - len(s) < argc
3052 nx.Left = Nod(OLT, Nod(OSUB, Nod(OCAP, ns, nil), Nod(OLEN, ns, nil)), na)
3054 fn := syslook("growslice", 1) // growslice(<type>, old []T, mincap int) (ret []T)
3055 substArgTypes(fn, ns.Type.Type, ns.Type.Type)
3057 nx.Nbody = list1(Nod(OAS, ns, mkcall1(fn, ns.Type, &nx.Ninit, typename(ns.Type), ns, Nod(OADD, Nod(OLEN, ns, nil), na))))
3061 nn := temp(Types[TINT])
3062 l = list(l, Nod(OAS, nn, Nod(OLEN, ns, nil))) // n = len(s)
3064 nx = Nod(OSLICE, ns, Nod(OKEY, nil, Nod(OADD, nn, na))) // ...s[:n+argc]
3066 l = list(l, Nod(OAS, ns, nx)) // s = s[:n+argc]
3068 for a := n.List.Next; a != nil; a = a.Next {
3069 nx = Nod(OINDEX, ns, nn) // s[n] ...
3071 l = list(l, Nod(OAS, nx, a.N)) // s[n] = arg
3073 l = list(l, Nod(OAS, nn, Nod(OADD, nn, Nodintconst(1)))) // n = n + 1
3077 typechecklist(l, Etop)
3079 *init = concat(*init, l)
3083 // Lower copy(a, b) to a memmove call or a runtime call.
3087 // if n > len(b) { n = len(b) }
3088 // memmove(a.ptr, b.ptr, n*sizeof(elem(a)))
3092 // Also works if b is a string.
3094 func copyany(n *Node, init **NodeList, runtimecall int) *Node {
3095 if haspointers(n.Left.Type.Type) {
3096 fn := writebarrierfn("typedslicecopy", n.Left.Type, n.Right.Type)
3097 return mkcall1(fn, n.Type, init, typename(n.Left.Type.Type), n.Left, n.Right)
3100 if runtimecall != 0 {
3102 if n.Right.Type.Etype == TSTRING {
3103 fn = syslook("slicestringcopy", 1)
3105 fn = syslook("slicecopy", 1)
3107 substArgTypes(fn, n.Left.Type, n.Right.Type)
3108 return mkcall1(fn, n.Type, init, n.Left, n.Right, Nodintconst(n.Left.Type.Type.Width))
3111 walkexpr(&n.Left, init)
3112 walkexpr(&n.Right, init)
3113 nl := temp(n.Left.Type)
3114 nr := temp(n.Right.Type)
3116 l = list(l, Nod(OAS, nl, n.Left))
3117 l = list(l, Nod(OAS, nr, n.Right))
3119 nfrm := Nod(OSPTR, nr, nil)
3120 nto := Nod(OSPTR, nl, nil)
3122 nlen := temp(Types[TINT])
3125 l = list(l, Nod(OAS, nlen, Nod(OLEN, nl, nil)))
3127 // if n > len(frm) { n = len(frm) }
3128 nif := Nod(OIF, nil, nil)
3130 nif.Left = Nod(OGT, nlen, Nod(OLEN, nr, nil))
3131 nif.Nbody = list(nif.Nbody, Nod(OAS, nlen, Nod(OLEN, nr, nil)))
3135 fn := syslook("memmove", 1)
3137 substArgTypes(fn, nl.Type.Type, nl.Type.Type)
3138 nwid := temp(Types[TUINTPTR])
3139 l = list(l, Nod(OAS, nwid, conv(nlen, Types[TUINTPTR])))
3140 nwid = Nod(OMUL, nwid, Nodintconst(nl.Type.Type.Width))
3141 l = list(l, mkcall1(fn, nil, init, nto, nfrm, nwid))
3143 typechecklist(l, Etop)
3145 *init = concat(*init, l)
3149 func eqfor(t *Type, needsize *int) *Node {
3150 // Should only arrive here with large memory or
3151 // a struct/array containing a non-memory field/element.
3152 // Small memory is handled inline, and single non-memory
3153 // is handled during type check (OCMPSTR etc).
3154 a := algtype1(t, nil)
3156 if a != AMEM && a != -1 {
3157 Fatalf("eqfor %v", t)
3161 n := syslook("memequal", 1)
3162 substArgTypes(n, t, t)
3167 sym := typesymprefix(".eq", t)
3170 ntype := Nod(OTFUNC, nil, nil)
3171 ntype.List = list(ntype.List, Nod(ODCLFIELD, nil, typenod(Ptrto(t))))
3172 ntype.List = list(ntype.List, Nod(ODCLFIELD, nil, typenod(Ptrto(t))))
3173 ntype.Rlist = list(ntype.Rlist, Nod(ODCLFIELD, nil, typenod(Types[TBOOL])))
3174 typecheck(&ntype, Etype)
3180 func countfield(t *Type) int {
3182 for t1 := t.Type; t1 != nil; t1 = t1.Down {
3188 func walkcompare(np **Node, init **NodeList) {
3191 // Given interface value l and concrete value r, rewrite
3194 // x, ok := l.(type(r)); ok && x == r
3195 // Handle != similarly.
3196 // This avoids the allocation that would be required
3197 // to convert r to l for comparison.
3201 if Isinter(n.Left.Type) && !Isinter(n.Right.Type) {
3204 } else if !Isinter(n.Left.Type) && Isinter(n.Right.Type) {
3211 if haspointers(r.Type) {
3212 a := Nod(OAS, x, nil)
3214 *init = list(*init, a)
3216 ok := temp(Types[TBOOL])
3219 a := Nod(ODOTTYPE, l, nil)
3223 // x, ok := l.(type(r))
3224 expr := Nod(OAS2, nil, nil)
3226 expr.List = list1(x)
3227 expr.List = list(expr.List, ok)
3228 expr.Rlist = list1(a)
3229 typecheck(&expr, Etop)
3230 walkexpr(&expr, init)
3233 r = Nod(OANDAND, ok, Nod(OEQ, x, r))
3235 r = Nod(OOROR, Nod(ONOT, ok, nil), Nod(ONE, x, r))
3237 *init = list(*init, expr)
3238 finishcompare(np, n, r, init)
3242 // Must be comparison of array or struct.
3243 // Otherwise back end handles it.
3260 for cmpl != nil && cmpl.Op == OCONVNOP {
3264 for cmpr != nil && cmpr.Op == OCONVNOP {
3268 if !islvalue(cmpl) || !islvalue(cmpr) {
3269 Fatalf("arguments of comparison must be lvalues - %v %v", cmpl, cmpr)
3273 a := Nod(OAS, l, Nod(OADDR, cmpl, nil))
3274 a.Right.Etype = 1 // addr does not escape
3276 *init = list(*init, a)
3279 a = Nod(OAS, r, Nod(OADDR, cmpr, nil))
3280 a.Right.Etype = 1 // addr does not escape
3282 *init = list(*init, a)
3290 if t.Etype == TARRAY && t.Bound <= 4 && issimple[t.Type.Etype] {
3291 // Four or fewer elements of a basic type.
3292 // Unroll comparisons.
3295 for i := 0; int64(i) < t.Bound; i++ {
3296 li = Nod(OINDEX, l, Nodintconst(int64(i)))
3297 ri = Nod(OINDEX, r, Nodintconst(int64(i)))
3298 a = Nod(int(n.Op), li, ri)
3302 expr = Nod(andor, expr, a)
3307 expr = Nodbool(n.Op == OEQ)
3309 finishcompare(np, n, expr, init)
3313 if t.Etype == TSTRUCT && countfield(t) <= 4 {
3314 // Struct of four or fewer fields.
3315 // Inline comparisons.
3318 for t1 := t.Type; t1 != nil; t1 = t1.Down {
3319 if isblanksym(t1.Sym) {
3322 li = Nod(OXDOT, l, newname(t1.Sym))
3323 ri = Nod(OXDOT, r, newname(t1.Sym))
3324 a = Nod(int(n.Op), li, ri)
3328 expr = Nod(andor, expr, a)
3333 expr = Nodbool(n.Op == OEQ)
3335 finishcompare(np, n, expr, init)
3339 // Chose not to inline. Call equality function directly.
3341 call := Nod(OCALL, eqfor(t, &needsize), nil)
3343 call.List = list(call.List, l)
3344 call.List = list(call.List, r)
3346 call.List = list(call.List, Nodintconst(t.Width))
3350 r = Nod(ONOT, r, nil)
3353 finishcompare(np, n, r, init)
3357 func finishcompare(np **Node, n, r *Node, init **NodeList) {
3358 // Using np here to avoid passing &r to typecheck.
3363 if r.Type != n.Type {
3364 r = Nod(OCONVNOP, r, nil)
3371 func samecheap(a *Node, b *Node) bool {
3374 for a != nil && b != nil && a.Op == b.Op {
3385 if ar.Op != ONAME || br.Op != ONAME || ar.Sym != br.Sym {
3392 if !Isconst(ar, CTINT) || !Isconst(br, CTINT) || Mpcmpfixfix(ar.Val().U.(*Mpint), br.Val().U.(*Mpint)) != 0 {
3404 func walkrotate(np **Node) {
3405 if Thearch.Thechar == '7' || Thearch.Thechar == '9' {
3411 // Want << | >> or >> | << or << ^ >> or >> ^ << on unsigned value.
3415 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 {
3419 // Want same, side effect-free expression on lhs of both shifts.
3420 if !samecheap(l.Left, r.Left) {
3424 // Constants adding to width?
3425 w := int(l.Type.Width * 8)
3427 if Smallintconst(l.Right) && Smallintconst(r.Right) {
3428 sl := int(Mpgetfix(l.Right.Val().U.(*Mpint)))
3430 sr := int(Mpgetfix(r.Right.Val().U.(*Mpint)))
3431 if sr >= 0 && sl+sr == w {
3432 // Rewrite left shift half to left rotate.
3440 // Remove rotate 0 and rotate w.
3441 s := int(Mpgetfix(n.Right.Val().U.(*Mpint)))
3443 if s == 0 || s == w {
3454 // TODO: Could allow s and 32-s if s is bounded (maybe s&31 and 32-s&31).
3459 * walkmul rewrites integer multiplication by powers of two as shifts.
3461 func walkmul(np **Node, init **NodeList) {
3463 if !Isint[n.Type.Etype] {
3469 if n.Right.Op == OLITERAL {
3472 } else if n.Left.Op == OLITERAL {
3481 // x*0 is 0 (and side effects of x).
3484 if Mpgetfix(nr.Val().U.(*Mpint)) == 0 {
3486 Nodconst(n, n.Type, 0)
3490 // nr is a constant.
3497 // negative power of 2, like -16
3503 w = int(nl.Type.Width * 8)
3504 if pow+1 >= w { // too big, shouldn't happen
3508 nl = cheapexpr(nl, init)
3517 n = Nod(OLSH, nl, Nodintconst(int64(pow)))
3521 n = Nod(OMINUS, n, nil)
3530 * walkdiv rewrites division by a constant as less expensive
3533 func walkdiv(np **Node, init **NodeList) {
3534 // if >= 0, nr is 1<<pow // 1 if nr is negative.
3537 if Thearch.Thechar == '7' || Thearch.Thechar == '9' {
3542 if n.Right.Op != OLITERAL {
3546 // nr is a constant.
3547 nl := cheapexpr(n.Left, init)
3551 // special cases of mod/div
3553 w := int(nl.Type.Width * 8)
3555 s := 0 // 1 if nr is negative.
3556 pow := powtwo(nr) // if >= 0, nr is 1<<pow
3558 // negative power of 2
3565 // divisor too large.
3570 // try to do division by multiply by (2^w)/d
3571 // see hacker's delight chapter 10
3572 // TODO: support 64-bit magic multiply here.
3576 if Issigned[nl.Type.Etype] {
3577 m.Sd = Mpgetfix(nr.Val().U.(*Mpint))
3580 m.Ud = uint64(Mpgetfix(nr.Val().U.(*Mpint)))
3588 // We have a quick division method so use it
3591 // rewrite as A%B = A - (A/B*B).
3592 n1 := Nod(ODIV, nl, nr)
3594 n2 := Nod(OMUL, n1, nr)
3595 n = Nod(OSUB, nl, n2)
3599 switch Simtype[nl.Type.Etype] {
3603 // n1 = nl * magic >> w (HMUL)
3604 case TUINT8, TUINT16, TUINT32:
3605 nc := Nod(OXXX, nil, nil)
3607 Nodconst(nc, nl.Type, int64(m.Um))
3608 n1 := Nod(OHMUL, nl, nc)
3611 // Select a Go type with (at least) twice the width.
3613 switch Simtype[nl.Type.Etype] {
3617 case TUINT8, TUINT16:
3618 twide = Types[TUINT32]
3621 twide = Types[TUINT64]
3624 twide = Types[TINT32]
3627 twide = Types[TINT64]
3630 // add numerator (might overflow).
3632 n2 := Nod(OADD, conv(n1, twide), conv(nl, twide))
3635 nc := Nod(OXXX, nil, nil)
3637 Nodconst(nc, Types[TUINT], int64(m.S))
3638 n = conv(Nod(ORSH, n2, nc), nl.Type)
3641 nc := Nod(OXXX, nil, nil)
3643 Nodconst(nc, Types[TUINT], int64(m.S))
3644 n = Nod(ORSH, n1, nc)
3647 // n1 = nl * magic >> w
3648 case TINT8, TINT16, TINT32:
3649 nc := Nod(OXXX, nil, nil)
3651 Nodconst(nc, nl.Type, m.Sm)
3652 n1 := Nod(OHMUL, nl, nc)
3655 // add the numerator.
3656 n1 = Nod(OADD, n1, nl)
3660 nc = Nod(OXXX, nil, nil)
3662 Nodconst(nc, Types[TUINT], int64(m.S))
3663 n2 := conv(Nod(ORSH, n1, nc), nl.Type)
3665 // add 1 iff n1 is negative.
3666 nc = Nod(OXXX, nil, nil)
3668 Nodconst(nc, Types[TUINT], int64(w)-1)
3669 n3 := Nod(ORSH, nl, nc) // n4 = -1 iff n1 is negative.
3670 n = Nod(OSUB, n2, n3)
3674 n = Nod(OMINUS, n, nil)
3685 Nodconst(n, n.Type, 0)
3697 if Issigned[n.Type.Etype] {
3699 // signed modulo 2^pow is like ANDing
3700 // with the last pow bits, but if nl < 0,
3701 // nl & (2^pow-1) is (nl+1)%2^pow - 1.
3702 nc := Nod(OXXX, nil, nil)
3704 Nodconst(nc, Types[Simtype[TUINT]], int64(w)-1)
3705 n1 := Nod(ORSH, nl, nc) // n1 = -1 iff nl < 0.
3708 n1 = cheapexpr(n1, init)
3710 // n = (nl+ε)&1 -ε where ε=1 iff nl<0.
3711 n2 := Nod(OSUB, nl, n1)
3713 nc := Nod(OXXX, nil, nil)
3714 Nodconst(nc, nl.Type, 1)
3715 n3 := Nod(OAND, n2, nc)
3716 n = Nod(OADD, n3, n1)
3718 // n = (nl+ε)&(nr-1) - ε where ε=2^pow-1 iff nl<0.
3719 nc := Nod(OXXX, nil, nil)
3721 Nodconst(nc, nl.Type, (1<<uint(pow))-1)
3722 n2 := Nod(OAND, n1, nc) // n2 = 2^pow-1 iff nl<0.
3724 n2 = cheapexpr(n2, init)
3726 n3 := Nod(OADD, nl, n2)
3727 n4 := Nod(OAND, n3, nc)
3728 n = Nod(OSUB, n4, n2)
3733 // arithmetic right shift does not give the correct rounding.
3734 // if nl >= 0, nl >> n == nl / nr
3735 // if nl < 0, we want to add 2^n-1 first.
3736 nc := Nod(OXXX, nil, nil)
3738 Nodconst(nc, Types[Simtype[TUINT]], int64(w)-1)
3739 n1 := Nod(ORSH, nl, nc) // n1 = -1 iff nl < 0.
3742 n.Left = Nod(OSUB, nl, n1)
3744 // Do a logical right right on -1 to keep pow bits.
3745 nc := Nod(OXXX, nil, nil)
3747 Nodconst(nc, Types[Simtype[TUINT]], int64(w)-int64(pow))
3748 n2 := Nod(ORSH, conv(n1, tounsigned(nl.Type)), nc)
3749 n.Left = Nod(OADD, nl, conv(n2, nl.Type))
3752 // n = (nl + 2^pow-1) >> pow
3755 nc = Nod(OXXX, nil, nil)
3756 Nodconst(nc, Types[Simtype[TUINT]], int64(pow))
3762 n = Nod(OMINUS, n, nil)
3767 nc := Nod(OXXX, nil, nil)
3772 Nodconst(nc, nl.Type, Mpgetfix(nr.Val().U.(*Mpint))-1)
3777 Nodconst(nc, Types[Simtype[TUINT]], int64(pow))
3792 // return 1 if integer n must be in range [0, max), 0 otherwise
3793 func bounded(n *Node, max int64) bool {
3794 if n.Type == nil || !Isint[n.Type.Etype] {
3798 sign := Issigned[n.Type.Etype]
3799 bits := int32(8 * n.Type.Width)
3801 if Smallintconst(n) {
3802 v := Mpgetfix(n.Val().U.(*Mpint))
3803 return 0 <= v && v < max
3809 if Smallintconst(n.Left) {
3810 v = Mpgetfix(n.Left.Val().U.(*Mpint))
3811 } else if Smallintconst(n.Right) {
3812 v = Mpgetfix(n.Right.Val().U.(*Mpint))
3815 if 0 <= v && v < max {
3820 if !sign && Smallintconst(n.Right) {
3821 v := Mpgetfix(n.Right.Val().U.(*Mpint))
3822 if 0 <= v && v <= max {
3828 if !sign && Smallintconst(n.Right) {
3829 v := Mpgetfix(n.Right.Val().U.(*Mpint))
3830 for bits > 0 && v >= 2 {
3837 if !sign && Smallintconst(n.Right) {
3838 v := Mpgetfix(n.Right.Val().U.(*Mpint))
3839 if v > int64(bits) {
3846 if !sign && bits <= 62 && 1<<uint(bits) <= max {
3853 func usefield(n *Node) {
3854 if obj.Fieldtrack_enabled == 0 {
3860 Fatalf("usefield %v", Oconv(int(n.Op), 0))
3870 field := dotField[typeSym{t.Orig, n.Right.Sym}]
3872 Fatalf("usefield %v %v without paramfld", n.Left.Type, n.Right.Sym)
3874 if field.Note == nil || !strings.Contains(*field.Note, "go:\"track\"") {
3879 if field.Lastfn == Curfn {
3882 field.Lastfn = Curfn
3883 field.Outer = n.Left.Type
3884 if Isptr[field.Outer.Etype] {
3885 field.Outer = field.Outer.Type
3887 if field.Outer.Sym == nil {
3888 Yyerror("tracked field must be in named struct type")
3890 if !exportname(field.Sym.Name) {
3891 Yyerror("tracked field must be exported (upper case)")
3894 Curfn.Func.Fieldtrack = append(Curfn.Func.Fieldtrack, field)
3897 func candiscardlist(l *NodeList) bool {
3898 for ; l != nil; l = l.Next {
3899 if !candiscard(l.N) {
3906 func candiscard(n *Node) bool {
3915 // Discardable as long as the subpieces are.
3970 // Discardable as long as we know it's not division by zero.
3972 if Isconst(n.Right, CTINT) && mpcmpfixc(n.Right.Val().U.(*Mpint), 0) != 0 {
3975 if Isconst(n.Right, CTFLT) && mpcmpfltc(n.Right.Val().U.(*Mpflt), 0) != 0 {
3980 // Discardable as long as we know it won't fail because of a bad size.
3981 case OMAKECHAN, OMAKEMAP:
3982 if Isconst(n.Left, CTINT) && mpcmpfixc(n.Left.Val().U.(*Mpint), 0) == 0 {
3987 // Difficult to tell what sizes are okay.
3992 if !candiscard(n.Left) || !candiscard(n.Right) || !candiscardlist(n.Ninit) || !candiscardlist(n.Nbody) || !candiscardlist(n.List) || !candiscardlist(n.Rlist) {
4002 // func(a1, a2, a3) {
4003 // print(a1, a2, a3)
4005 // and same for println.
4007 var walkprintfunc_prgen int
4009 func walkprintfunc(np **Node, init **NodeList) {
4013 walkstmtlist(n.Ninit)
4014 *init = concat(*init, n.Ninit)
4018 t := Nod(OTFUNC, nil, nil)
4020 var printargs *NodeList
4023 for l := n.List; l != nil; l = l.Next {
4024 buf = fmt.Sprintf("a%d", num)
4026 a = Nod(ODCLFIELD, newname(Lookup(buf)), typenod(l.N.Type))
4027 t.List = list(t.List, a)
4028 printargs = list(printargs, a.Left)
4031 fn := Nod(ODCLFUNC, nil, nil)
4032 walkprintfunc_prgen++
4033 buf = fmt.Sprintf("print·%d", walkprintfunc_prgen)
4034 fn.Func.Nname = newname(Lookup(buf))
4035 fn.Func.Nname.Name.Defn = fn
4036 fn.Func.Nname.Name.Param.Ntype = t
4037 declare(fn.Func.Nname, PFUNC)
4043 a = Nod(int(n.Op), nil, nil)
4052 typecheck(&fn, Etop)
4053 typechecklist(fn.Nbody, Etop)
4054 xtop = list(xtop, fn)
4057 a = Nod(OCALL, nil, nil)
4058 a.Left = fn.Func.Nname