Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package gc |
| 6 | |
| 7 | import ( |
| 8 | "cmd/internal/obj" |
Matthew Dempsky | c6e11fe | 2016-04-06 12:01:40 -0700 | [diff] [blame] | 9 | "cmd/internal/sys" |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 10 | "fmt" |
| 11 | "strings" |
| 12 | ) |
| 13 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 14 | // The constant is known to runtime. |
| 15 | const ( |
| 16 | tmpstringbufsize = 32 |
| 17 | ) |
| 18 | |
| 19 | func walk(fn *Node) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 20 | Curfn = fn |
| 21 | |
| 22 | if Debug['W'] != 0 { |
Russ Cox | bd4fff6 | 2015-05-27 10:42:55 -0400 | [diff] [blame] | 23 | s := fmt.Sprintf("\nbefore %v", Curfn.Func.Nname.Sym) |
Ian Lance Taylor | 55c65d4 | 2016-03-04 13:16:48 -0800 | [diff] [blame] | 24 | dumplist(s, Curfn.Nbody) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 25 | } |
| 26 | |
Robert Griesemer | c41608f | 2016-03-02 17:34:42 -0800 | [diff] [blame] | 27 | lno := lineno |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 28 | |
| 29 | // Final typecheck for any unused variables. |
Ian Lance Taylor | b66a892 | 2016-02-25 10:35:19 -0800 | [diff] [blame] | 30 | for i, ln := range fn.Func.Dcl { |
Russ Cox | b6dc3e6 | 2016-05-25 01:33:24 -0400 | [diff] [blame] | 31 | if ln.Op == ONAME && (ln.Class == PAUTO || ln.Class == PAUTOHEAP) { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 32 | ln = typecheck(ln, Erv|Easgn) |
Ian Lance Taylor | b66a892 | 2016-02-25 10:35:19 -0800 | [diff] [blame] | 33 | fn.Func.Dcl[i] = ln |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
| 37 | // Propagate the used flag for typeswitch variables up to the NONAME in it's definition. |
Ian Lance Taylor | b66a892 | 2016-02-25 10:35:19 -0800 | [diff] [blame] | 38 | for _, ln := range fn.Func.Dcl { |
Russ Cox | b6dc3e6 | 2016-05-25 01:33:24 -0400 | [diff] [blame] | 39 | if ln.Op == ONAME && (ln.Class == PAUTO || ln.Class == PAUTOHEAP) && ln.Name.Defn != nil && ln.Name.Defn.Op == OTYPESW && ln.Used { |
Ian Lance Taylor | b66a892 | 2016-02-25 10:35:19 -0800 | [diff] [blame] | 40 | ln.Name.Defn.Left.Used = true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
Ian Lance Taylor | b66a892 | 2016-02-25 10:35:19 -0800 | [diff] [blame] | 44 | for _, ln := range fn.Func.Dcl { |
Russ Cox | b6dc3e6 | 2016-05-25 01:33:24 -0400 | [diff] [blame] | 45 | if ln.Op != ONAME || (ln.Class != PAUTO && ln.Class != PAUTOHEAP) || ln.Sym.Name[0] == '&' || ln.Used { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 46 | continue |
| 47 | } |
Ian Lance Taylor | b66a892 | 2016-02-25 10:35:19 -0800 | [diff] [blame] | 48 | if defn := ln.Name.Defn; defn != nil && defn.Op == OTYPESW { |
Russ Cox | 4fdd536 | 2015-05-26 22:19:27 -0400 | [diff] [blame] | 49 | if defn.Left.Used { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 50 | continue |
| 51 | } |
Russ Cox | 4fdd536 | 2015-05-26 22:19:27 -0400 | [diff] [blame] | 52 | lineno = defn.Left.Lineno |
Ian Lance Taylor | b66a892 | 2016-02-25 10:35:19 -0800 | [diff] [blame] | 53 | Yyerror("%v declared and not used", ln.Sym) |
Russ Cox | 4fdd536 | 2015-05-26 22:19:27 -0400 | [diff] [blame] | 54 | defn.Left.Used = true // suppress repeats |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 55 | } else { |
Ian Lance Taylor | b66a892 | 2016-02-25 10:35:19 -0800 | [diff] [blame] | 56 | lineno = ln.Lineno |
| 57 | Yyerror("%v declared and not used", ln.Sym) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Robert Griesemer | c41608f | 2016-03-02 17:34:42 -0800 | [diff] [blame] | 61 | lineno = lno |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 62 | if nerrors != 0 { |
| 63 | return |
| 64 | } |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 65 | walkstmtlist(Curfn.Nbody.Slice()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 66 | if Debug['W'] != 0 { |
Russ Cox | bd4fff6 | 2015-05-27 10:42:55 -0400 | [diff] [blame] | 67 | s := fmt.Sprintf("after walk %v", Curfn.Func.Nname.Sym) |
Ian Lance Taylor | 55c65d4 | 2016-03-04 13:16:48 -0800 | [diff] [blame] | 68 | dumplist(s, Curfn.Nbody) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | heapmoves() |
Josh Bleecher Snyder | 1da62af | 2016-04-24 13:50:26 -0700 | [diff] [blame] | 72 | if Debug['W'] != 0 && Curfn.Func.Enter.Len() > 0 { |
Russ Cox | bd4fff6 | 2015-05-27 10:42:55 -0400 | [diff] [blame] | 73 | s := fmt.Sprintf("enter %v", Curfn.Func.Nname.Sym) |
Ian Lance Taylor | 55c65d4 | 2016-03-04 13:16:48 -0800 | [diff] [blame] | 74 | dumplist(s, Curfn.Func.Enter) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 78 | func walkstmtlist(s []*Node) { |
| 79 | for i := range s { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 80 | s[i] = walkstmt(s[i]) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 84 | func samelist(a, b []*Node) bool { |
| 85 | if len(a) != len(b) { |
| 86 | return false |
Ian Lance Taylor | 188e3d2 | 2016-02-26 14:28:48 -0800 | [diff] [blame] | 87 | } |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 88 | for i, n := range a { |
| 89 | if n != b[i] { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 90 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 91 | } |
| 92 | } |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 93 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 94 | } |
| 95 | |
Dave Cheney | b006d38 | 2015-03-06 18:42:58 +1100 | [diff] [blame] | 96 | func paramoutheap(fn *Node) bool { |
Ian Lance Taylor | b66a892 | 2016-02-25 10:35:19 -0800 | [diff] [blame] | 97 | for _, ln := range fn.Func.Dcl { |
| 98 | switch ln.Class { |
Russ Cox | b6dc3e6 | 2016-05-25 01:33:24 -0400 | [diff] [blame] | 99 | case PPARAMOUT: |
| 100 | if ln.isParamStackCopy() || ln.Addrtaken { |
| 101 | return true |
| 102 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 103 | |
Russ Cox | b6dc3e6 | 2016-05-25 01:33:24 -0400 | [diff] [blame] | 104 | case PAUTO: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 105 | // stop early - parameters are over |
Dave Cheney | b006d38 | 2015-03-06 18:42:58 +1100 | [diff] [blame] | 106 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Dave Cheney | b006d38 | 2015-03-06 18:42:58 +1100 | [diff] [blame] | 110 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // adds "adjust" to all the argument locations for the call n. |
| 114 | // n must be a defer or go node that has already been walked. |
| 115 | func adjustargs(n *Node, adjust int) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 116 | var arg *Node |
| 117 | var lhs *Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 118 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 119 | callfunc := n.Left |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 120 | for _, arg = range callfunc.List.Slice() { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 121 | if arg.Op != OAS { |
| 122 | Yyerror("call arg not assignment") |
| 123 | } |
| 124 | lhs = arg.Left |
| 125 | if lhs.Op == ONAME { |
| 126 | // This is a temporary introduced by reorder1. |
| 127 | // The real store to the stack appears later in the arg list. |
| 128 | continue |
| 129 | } |
| 130 | |
| 131 | if lhs.Op != OINDREG { |
| 132 | Yyerror("call argument store does not use OINDREG") |
| 133 | } |
| 134 | |
| 135 | // can't really check this in machine-indep code. |
| 136 | //if(lhs->val.u.reg != D_SP) |
| 137 | // yyerror("call arg assign not indreg(SP)"); |
| 138 | lhs.Xoffset += int64(adjust) |
| 139 | } |
| 140 | } |
| 141 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 142 | // The result of walkstmt MUST be assigned back to n, e.g. |
| 143 | // n.Left = walkstmt(n.Left) |
| 144 | func walkstmt(n *Node) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 145 | if n == nil { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 146 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 147 | } |
| 148 | if n.Dodata == 2 { // don't walk, generated by anylit. |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 149 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | setlineno(n) |
| 153 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 154 | walkstmtlist(n.Ninit.Slice()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 155 | |
| 156 | switch n.Op { |
| 157 | default: |
| 158 | if n.Op == ONAME { |
Russ Cox | 17228f4 | 2015-04-17 12:03:22 -0400 | [diff] [blame] | 159 | Yyerror("%v is not a top level statement", n.Sym) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 160 | } else { |
Dave Cheney | d3c79d3 | 2016-04-27 15:10:10 +1000 | [diff] [blame] | 161 | Yyerror("%v is not a top level statement", n.Op) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 162 | } |
| 163 | Dump("nottop", n) |
| 164 | |
| 165 | case OAS, |
| 166 | OASOP, |
| 167 | OAS2, |
| 168 | OAS2DOTTYPE, |
| 169 | OAS2RECV, |
| 170 | OAS2FUNC, |
| 171 | OAS2MAPR, |
| 172 | OCLOSE, |
| 173 | OCOPY, |
| 174 | OCALLMETH, |
| 175 | OCALLINTER, |
| 176 | OCALL, |
| 177 | OCALLFUNC, |
| 178 | ODELETE, |
| 179 | OSEND, |
| 180 | OPRINT, |
| 181 | OPRINTN, |
| 182 | OPANIC, |
| 183 | OEMPTY, |
Russ Cox | 92c826b | 2015-04-03 12:23:28 -0400 | [diff] [blame] | 184 | ORECOVER, |
| 185 | OGETG: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 186 | if n.Typecheck == 0 { |
Matthew Dempsky | 6314202 | 2016-03-15 13:06:58 -0700 | [diff] [blame] | 187 | Fatalf("missing typecheck: %v", Nconv(n, FmtSign)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 188 | } |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 189 | wascopy := n.Op == OCOPY |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 190 | init := n.Ninit |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 191 | n.Ninit.Set(nil) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 192 | n = walkexpr(n, &init) |
| 193 | n = addinit(n, init.Slice()) |
| 194 | if wascopy && n.Op == OCONVNOP { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 195 | n.Op = OEMPTY // don't leave plain values as statements. |
| 196 | } |
| 197 | |
| 198 | // special case for a receive where we throw away |
| 199 | // the value received. |
| 200 | case ORECV: |
| 201 | if n.Typecheck == 0 { |
Matthew Dempsky | 6314202 | 2016-03-15 13:06:58 -0700 | [diff] [blame] | 202 | Fatalf("missing typecheck: %v", Nconv(n, FmtSign)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 203 | } |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 204 | init := n.Ninit |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 205 | n.Ninit.Set(nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 206 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 207 | n.Left = walkexpr(n.Left, &init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 208 | n = mkcall1(chanfn("chanrecv1", 2, n.Left.Type), nil, &init, typename(n.Left.Type), n.Left, nodnil()) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 209 | n = walkexpr(n, &init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 210 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 211 | n = addinit(n, init.Slice()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 212 | |
| 213 | case OBREAK, |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 214 | OCONTINUE, |
| 215 | OFALL, |
| 216 | OGOTO, |
| 217 | OLABEL, |
| 218 | ODCLCONST, |
| 219 | ODCLTYPE, |
| 220 | OCHECKNIL, |
Russ Cox | 1ac637c | 2016-01-13 00:46:28 -0500 | [diff] [blame] | 221 | OVARKILL, |
| 222 | OVARLIVE: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 223 | break |
| 224 | |
Russ Cox | b6dc3e6 | 2016-05-25 01:33:24 -0400 | [diff] [blame] | 225 | case ODCL: |
| 226 | v := n.Left |
| 227 | if v.Class == PAUTOHEAP { |
| 228 | if compiling_runtime { |
| 229 | Yyerror("%v escapes to heap, not allowed in runtime.", v) |
| 230 | } |
| 231 | if prealloc[v] == nil { |
| 232 | prealloc[v] = callnew(v.Type) |
| 233 | } |
| 234 | nn := Nod(OAS, v.Name.Heapaddr, prealloc[v]) |
| 235 | nn.Colas = true |
| 236 | nn = typecheck(nn, Etop) |
| 237 | return walkstmt(nn) |
| 238 | } |
| 239 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 240 | case OBLOCK: |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 241 | walkstmtlist(n.List.Slice()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 242 | |
| 243 | case OXCASE: |
| 244 | Yyerror("case statement out of place") |
| 245 | n.Op = OCASE |
| 246 | fallthrough |
| 247 | |
| 248 | case OCASE: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 249 | n.Right = walkstmt(n.Right) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 250 | |
| 251 | case ODEFER: |
HĂ¥vard Haugen | 2594664 | 2015-09-07 22:19:30 +0200 | [diff] [blame] | 252 | hasdefer = true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 253 | switch n.Left.Op { |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 254 | case OPRINT, OPRINTN: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 255 | n.Left = walkprintfunc(n.Left, &n.Ninit) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 256 | |
| 257 | case OCOPY: |
Ian Lance Taylor | 9e902f0 | 2015-10-20 10:00:07 -0700 | [diff] [blame] | 258 | n.Left = copyany(n.Left, &n.Ninit, true) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 259 | |
| 260 | default: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 261 | n.Left = walkexpr(n.Left, &n.Ninit) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | // make room for size & fn arguments. |
| 265 | adjustargs(n, 2*Widthptr) |
| 266 | |
| 267 | case OFOR: |
Russ Cox | 66be148 | 2015-05-26 21:30:20 -0400 | [diff] [blame] | 268 | if n.Left != nil { |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 269 | walkstmtlist(n.Left.Ninit.Slice()) |
Russ Cox | 66be148 | 2015-05-26 21:30:20 -0400 | [diff] [blame] | 270 | init := n.Left.Ninit |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 271 | n.Left.Ninit.Set(nil) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 272 | n.Left = walkexpr(n.Left, &init) |
| 273 | n.Left = addinit(n.Left, init.Slice()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 274 | } |
| 275 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 276 | n.Right = walkstmt(n.Right) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 277 | walkstmtlist(n.Nbody.Slice()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 278 | |
| 279 | case OIF: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 280 | n.Left = walkexpr(n.Left, &n.Ninit) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 281 | walkstmtlist(n.Nbody.Slice()) |
| 282 | walkstmtlist(n.Rlist.Slice()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 283 | |
| 284 | case OPROC: |
| 285 | switch n.Left.Op { |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 286 | case OPRINT, OPRINTN: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 287 | n.Left = walkprintfunc(n.Left, &n.Ninit) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 288 | |
| 289 | case OCOPY: |
Ian Lance Taylor | 9e902f0 | 2015-10-20 10:00:07 -0700 | [diff] [blame] | 290 | n.Left = copyany(n.Left, &n.Ninit, true) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 291 | |
| 292 | default: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 293 | n.Left = walkexpr(n.Left, &n.Ninit) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | // make room for size & fn arguments. |
| 297 | adjustargs(n, 2*Widthptr) |
| 298 | |
| 299 | case ORETURN: |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 300 | walkexprlist(n.List.Slice(), &n.Ninit) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 301 | if n.List.Len() == 0 { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 302 | break |
| 303 | } |
Josh Bleecher Snyder | f38f43d | 2016-04-01 20:11:30 -0700 | [diff] [blame] | 304 | if (Curfn.Type.FuncType().Outnamed && n.List.Len() > 1) || paramoutheap(Curfn) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 305 | // assign to the function out parameters, |
| 306 | // so that reorder3 can fix up conflicts |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 307 | var rl []*Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 308 | |
Robert Griesemer | cd7d738 | 2015-10-26 14:57:36 -0700 | [diff] [blame] | 309 | var cl Class |
Ian Lance Taylor | b66a892 | 2016-02-25 10:35:19 -0800 | [diff] [blame] | 310 | for _, ln := range Curfn.Func.Dcl { |
Russ Cox | b6dc3e6 | 2016-05-25 01:33:24 -0400 | [diff] [blame] | 311 | cl = ln.Class |
| 312 | if cl == PAUTO || cl == PAUTOHEAP { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 313 | break |
| 314 | } |
| 315 | if cl == PPARAMOUT { |
Russ Cox | b6dc3e6 | 2016-05-25 01:33:24 -0400 | [diff] [blame] | 316 | if ln.isParamStackCopy() { |
| 317 | ln = walkexpr(typecheck(Nod(OIND, ln.Name.Heapaddr, nil), Erv), nil) |
| 318 | } |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 319 | rl = append(rl, ln) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 323 | if got, want := n.List.Len(), len(rl); got != want { |
Matthew Dempsky | 22a204d | 2015-12-14 12:37:26 -0800 | [diff] [blame] | 324 | // order should have rewritten multi-value function calls |
| 325 | // with explicit OAS2FUNC nodes. |
| 326 | Fatalf("expected %v return arguments, have %v", want, got) |
| 327 | } |
| 328 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 329 | if samelist(rl, n.List.Slice()) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 330 | // special return in disguise |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 331 | n.List.Set(nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 332 | |
| 333 | break |
| 334 | } |
| 335 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 336 | // move function calls out, to make reorder3's job easier. |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 337 | walkexprlistsafe(n.List.Slice(), &n.Ninit) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 338 | |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 339 | ll := ascompatee(n.Op, rl, n.List.Slice(), &n.Ninit) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 340 | n.List.Set(reorder3(ll)) |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 341 | ls := n.List.Slice() |
| 342 | for i, n := range ls { |
| 343 | ls[i] = applywritebarrier(n) |
Matthew Dempsky | 85dd62d | 2015-12-11 19:11:54 -0800 | [diff] [blame] | 344 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 345 | break |
| 346 | } |
| 347 | |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 348 | ll := ascompatte(n.Op, nil, false, Curfn.Type.Results(), n.List.Slice(), 1, &n.Ninit) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 349 | n.List.Set(ll) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 350 | |
| 351 | case ORETJMP: |
| 352 | break |
| 353 | |
| 354 | case OSELECT: |
| 355 | walkselect(n) |
| 356 | |
| 357 | case OSWITCH: |
| 358 | walkswitch(n) |
| 359 | |
| 360 | case ORANGE: |
| 361 | walkrange(n) |
| 362 | |
| 363 | case OXFALL: |
| 364 | Yyerror("fallthrough statement out of place") |
| 365 | n.Op = OFALL |
| 366 | } |
| 367 | |
| 368 | if n.Op == ONAME { |
Matthew Dempsky | 6314202 | 2016-03-15 13:06:58 -0700 | [diff] [blame] | 369 | Fatalf("walkstmt ended up with name: %v", Nconv(n, FmtSign)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 370 | } |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 371 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 372 | } |
| 373 | |
David Chase | e5060c7 | 2015-05-20 15:16:34 -0400 | [diff] [blame] | 374 | func isSmallMakeSlice(n *Node) bool { |
| 375 | if n.Op != OMAKESLICE { |
| 376 | return false |
| 377 | } |
| 378 | l := n.Left |
| 379 | r := n.Right |
| 380 | if r == nil { |
| 381 | r = l |
| 382 | } |
| 383 | t := n.Type |
| 384 | |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 385 | return Smallintconst(l) && Smallintconst(r) && (t.Elem().Width == 0 || r.Int64() < (1<<16)/t.Elem().Width) |
David Chase | e5060c7 | 2015-05-20 15:16:34 -0400 | [diff] [blame] | 386 | } |
| 387 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 388 | // walk the whole tree of the body of an |
| 389 | // expression or simple statement. |
| 390 | // the types expressions are calculated. |
| 391 | // compile-time constants are evaluated. |
| 392 | // complex side effects like statements are appended to init |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 393 | func walkexprlist(s []*Node, init *Nodes) { |
| 394 | for i := range s { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 395 | s[i] = walkexpr(s[i], init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 399 | func walkexprlistsafe(s []*Node, init *Nodes) { |
| 400 | for i, n := range s { |
| 401 | s[i] = safeexpr(n, init) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 402 | s[i] = walkexpr(s[i], init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 406 | func walkexprlistcheap(s []*Node, init *Nodes) { |
| 407 | for i, n := range s { |
| 408 | s[i] = cheapexpr(n, init) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 409 | s[i] = walkexpr(s[i], init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
Brad Fitzpatrick | e3a9dca | 2016-03-04 22:02:27 +0000 | [diff] [blame] | 413 | // Build name of function: convI2E etc. |
| 414 | // Not all names are possible |
| 415 | // (e.g., we'll never generate convE2E or convE2I). |
| 416 | func convFuncName(from, to *Type) string { |
| 417 | tkind := to.iet() |
| 418 | switch from.iet() { |
| 419 | case 'I': |
| 420 | switch tkind { |
| 421 | case 'E': |
| 422 | return "convI2E" |
| 423 | case 'I': |
| 424 | return "convI2I" |
| 425 | } |
| 426 | case 'T': |
| 427 | switch tkind { |
| 428 | case 'E': |
| 429 | return "convT2E" |
| 430 | case 'I': |
| 431 | return "convT2I" |
| 432 | } |
| 433 | } |
| 434 | Fatalf("unknown conv func %c2%c", from.iet(), to.iet()) |
| 435 | panic("unreachable") |
| 436 | } |
| 437 | |
| 438 | // Build name of function: assertI2E etc. |
| 439 | // If with2suffix is true, the form ending in "2" is returned". |
| 440 | func assertFuncName(from, to *Type, with2suffix bool) string { |
| 441 | l := len("assertX2X2") |
| 442 | if !with2suffix { |
| 443 | l-- |
| 444 | } |
| 445 | tkind := to.iet() |
| 446 | switch from.iet() { |
| 447 | case 'E': |
| 448 | switch tkind { |
| 449 | case 'I': |
| 450 | return "assertE2I2"[:l] |
| 451 | case 'E': |
| 452 | return "assertE2E2"[:l] |
| 453 | case 'T': |
| 454 | return "assertE2T2"[:l] |
| 455 | } |
| 456 | case 'I': |
| 457 | switch tkind { |
| 458 | case 'I': |
| 459 | return "assertI2I2"[:l] |
| 460 | case 'E': |
| 461 | return "assertI2E2"[:l] |
| 462 | case 'T': |
| 463 | return "assertI2T2"[:l] |
| 464 | } |
| 465 | } |
| 466 | Fatalf("unknown assert func %c2%c", from.iet(), to.iet()) |
| 467 | panic("unreachable") |
| 468 | } |
| 469 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 470 | // The result of walkexpr MUST be assigned back to n, e.g. |
| 471 | // n.Left = walkexpr(n.Left, init) |
| 472 | func walkexpr(n *Node, init *Nodes) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 473 | if n == nil { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 474 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | if init == &n.Ninit { |
| 478 | // not okay to use n->ninit when walking n, |
| 479 | // because we might replace n with some other node |
| 480 | // and would lose the init list. |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 481 | Fatalf("walkexpr init == &n->ninit") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 482 | } |
| 483 | |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 484 | if n.Ninit.Len() != 0 { |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 485 | walkstmtlist(n.Ninit.Slice()) |
| 486 | init.AppendNodes(&n.Ninit) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // annoying case - not typechecked |
| 490 | if n.Op == OKEY { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 491 | n.Left = walkexpr(n.Left, init) |
| 492 | n.Right = walkexpr(n.Right, init) |
| 493 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 494 | } |
| 495 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 496 | lno := setlineno(n) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 497 | |
| 498 | if Debug['w'] > 1 { |
| 499 | Dump("walk-before", n) |
| 500 | } |
| 501 | |
| 502 | if n.Typecheck != 1 { |
Matthew Dempsky | 6314202 | 2016-03-15 13:06:58 -0700 | [diff] [blame] | 503 | Fatalf("missed typecheck: %v\n", Nconv(n, FmtSign)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 504 | } |
| 505 | |
Russ Cox | b6dc3e6 | 2016-05-25 01:33:24 -0400 | [diff] [blame] | 506 | if n.Op == ONAME && n.Class == PAUTOHEAP { |
| 507 | nn := Nod(OIND, n.Name.Heapaddr, nil) |
| 508 | nn = typecheck(nn, Erv) |
Cherry Zhang | 4c4ca83 | 2016-08-11 09:54:04 -0400 | [diff] [blame] | 509 | nn = walkexpr(nn, init) |
| 510 | nn.Left.NonNil = true |
| 511 | return nn |
Russ Cox | b6dc3e6 | 2016-05-25 01:33:24 -0400 | [diff] [blame] | 512 | } |
| 513 | |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 514 | opswitch: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 515 | switch n.Op { |
| 516 | default: |
| 517 | Dump("walk", n) |
Matthew Dempsky | 6314202 | 2016-03-15 13:06:58 -0700 | [diff] [blame] | 518 | Fatalf("walkexpr: switch 1 unknown op %v", Nconv(n, FmtShort|FmtSign)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 519 | |
| 520 | case OTYPE, |
| 521 | ONONAME, |
| 522 | OINDREG, |
| 523 | OEMPTY, |
Russ Cox | 92c826b | 2015-04-03 12:23:28 -0400 | [diff] [blame] | 524 | OGETG: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 525 | |
| 526 | case ONOT, |
| 527 | OMINUS, |
| 528 | OPLUS, |
| 529 | OCOM, |
| 530 | OREAL, |
| 531 | OIMAG, |
| 532 | ODOTMETH, |
| 533 | ODOTINTER: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 534 | n.Left = walkexpr(n.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 535 | |
| 536 | case OIND: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 537 | n.Left = walkexpr(n.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 538 | |
| 539 | case ODOT: |
| 540 | usefield(n) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 541 | n.Left = walkexpr(n.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 542 | |
| 543 | case ODOTPTR: |
| 544 | usefield(n) |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 545 | if n.Op == ODOTPTR && n.Left.Type.Elem().Width == 0 { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 546 | // No actual copy will be generated, so emit an explicit nil check. |
| 547 | n.Left = cheapexpr(n.Left, init) |
| 548 | |
| 549 | checknil(n.Left, init) |
| 550 | } |
| 551 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 552 | n.Left = walkexpr(n.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 553 | |
| 554 | case OEFACE: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 555 | n.Left = walkexpr(n.Left, init) |
| 556 | n.Right = walkexpr(n.Right, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 557 | |
Josh Bleecher Snyder | 615a52b | 2016-06-06 12:38:19 -0700 | [diff] [blame] | 558 | case OSPTR, OITAB, OIDATA: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 559 | n.Left = walkexpr(n.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 560 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 561 | case OLEN, OCAP: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 562 | n.Left = walkexpr(n.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 563 | |
| 564 | // replace len(*[10]int) with 10. |
| 565 | // delayed until now to preserve side effects. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 566 | t := n.Left.Type |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 567 | |
Matthew Dempsky | e76fc1b | 2016-03-30 15:09:25 -0700 | [diff] [blame] | 568 | if t.IsPtr() { |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 569 | t = t.Elem() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 570 | } |
Matthew Dempsky | 1624a9c | 2016-03-30 14:45:47 -0700 | [diff] [blame] | 571 | if t.IsArray() { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 572 | safeexpr(n.Left, init) |
Josh Bleecher Snyder | 3a0783c | 2016-03-31 14:46:04 -0700 | [diff] [blame] | 573 | Nodconst(n, n.Type, t.NumElem()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 574 | n.Typecheck = 1 |
| 575 | } |
| 576 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 577 | case OLSH, ORSH: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 578 | n.Left = walkexpr(n.Left, init) |
| 579 | n.Right = walkexpr(n.Right, init) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 580 | t := n.Left.Type |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 581 | n.Bounded = bounded(n.Right, 8*t.Width) |
| 582 | if Debug['m'] != 0 && n.Etype != 0 && !Isconst(n.Right, CTINT) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 583 | Warn("shift bounds check elided") |
| 584 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 585 | |
| 586 | // Use results from call expression as arguments for complex. |
| 587 | case OAND, |
| 588 | OSUB, |
| 589 | OHMUL, |
| 590 | OLT, |
| 591 | OLE, |
| 592 | OGE, |
| 593 | OGT, |
| 594 | OADD, |
| 595 | OCOMPLEX, |
| 596 | OLROT: |
| 597 | if n.Op == OCOMPLEX && n.Left == nil && n.Right == nil { |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 598 | n.Left = n.List.First() |
| 599 | n.Right = n.List.Second() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 600 | } |
| 601 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 602 | n.Left = walkexpr(n.Left, init) |
| 603 | n.Right = walkexpr(n.Right, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 604 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 605 | case OOR, OXOR: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 606 | n.Left = walkexpr(n.Left, init) |
| 607 | n.Right = walkexpr(n.Right, init) |
| 608 | n = walkrotate(n) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 609 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 610 | case OEQ, ONE: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 611 | n.Left = walkexpr(n.Left, init) |
| 612 | n.Right = walkexpr(n.Right, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 613 | |
| 614 | // Disable safemode while compiling this code: the code we |
| 615 | // generate internally can refer to unsafe.Pointer. |
| 616 | // In this case it can happen if we need to generate an == |
| 617 | // for a struct containing a reflect.Value, which itself has |
| 618 | // an unexported field of type unsafe.Pointer. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 619 | old_safemode := safemode |
Matthew Dempsky | 980ab12 | 2016-04-13 18:37:18 -0700 | [diff] [blame] | 620 | safemode = false |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 621 | n = walkcompare(n, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 622 | safemode = old_safemode |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 623 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 624 | case OANDAND, OOROR: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 625 | n.Left = walkexpr(n.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 626 | |
David Chase | ffe7fbf | 2015-03-27 12:34:45 -0400 | [diff] [blame] | 627 | // cannot put side effects from n.Right on init, |
| 628 | // because they cannot run before n.Left is checked. |
| 629 | // save elsewhere and store on the eventual n.Right. |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 630 | var ll Nodes |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 631 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 632 | n.Right = walkexpr(n.Right, &ll) |
| 633 | n.Right = addinit(n.Right, ll.Slice()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 634 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 635 | case OPRINT, OPRINTN: |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 636 | walkexprlist(n.List.Slice(), init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 637 | n = walkprint(n, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 638 | |
| 639 | case OPANIC: |
| 640 | n = mkcall("gopanic", nil, init, n.Left) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 641 | |
| 642 | case ORECOVER: |
| 643 | n = mkcall("gorecover", n.Type, init, Nod(OADDR, nodfp, nil)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 644 | |
| 645 | case OLITERAL: |
Josh Bleecher Snyder | 75883ba | 2015-04-02 19:58:37 -0700 | [diff] [blame] | 646 | n.Addable = true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 647 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 648 | case OCLOSUREVAR, OCFUNC: |
Josh Bleecher Snyder | 75883ba | 2015-04-02 19:58:37 -0700 | [diff] [blame] | 649 | n.Addable = true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 650 | |
| 651 | case ONAME: |
Russ Cox | 20803b8 | 2016-05-25 10:29:50 -0400 | [diff] [blame] | 652 | n.Addable = true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 653 | |
| 654 | case OCALLINTER: |
David Crawshaw | cc15840 | 2016-03-10 16:15:26 -0500 | [diff] [blame] | 655 | usemethod(n) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 656 | t := n.Left.Type |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 657 | if n.List.Len() != 0 && n.List.First().Op == OAS { |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 658 | break |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 659 | } |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 660 | n.Left = walkexpr(n.Left, init) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 661 | walkexprlist(n.List.Slice(), init) |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 662 | ll := ascompatte(n.Op, n, n.Isddd, t.Params(), n.List.Slice(), 0, init) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 663 | n.List.Set(reorder1(ll)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 664 | |
| 665 | case OCALLFUNC: |
| 666 | if n.Left.Op == OCLOSURE { |
| 667 | // Transform direct call of a closure to call of a normal function. |
| 668 | // transformclosure already did all preparation work. |
| 669 | |
David Chase | 731dcda | 2015-07-23 14:17:07 -0400 | [diff] [blame] | 670 | // Prepend captured variables to argument list. |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 671 | n.List.Set(append(n.Left.Func.Enter.Slice(), n.List.Slice()...)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 672 | |
Ian Lance Taylor | 188e3d2 | 2016-02-26 14:28:48 -0800 | [diff] [blame] | 673 | n.Left.Func.Enter.Set(nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 674 | |
| 675 | // Replace OCLOSURE with ONAME/PFUNC. |
Russ Cox | bd4fff6 | 2015-05-27 10:42:55 -0400 | [diff] [blame] | 676 | n.Left = n.Left.Func.Closure.Func.Nname |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 677 | |
| 678 | // Update type of OCALLFUNC node. |
| 679 | // Output arguments had not changed, but their offsets could. |
Matthew Dempsky | c837761 | 2016-03-17 01:47:16 -0700 | [diff] [blame] | 680 | if n.Left.Type.Results().NumFields() == 1 { |
Matthew Dempsky | 0d2e92c | 2016-03-13 23:02:38 -0700 | [diff] [blame] | 681 | n.Type = n.Left.Type.Results().Field(0).Type |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 682 | } else { |
Matthew Dempsky | db506fe | 2016-03-08 16:31:28 -0800 | [diff] [blame] | 683 | n.Type = n.Left.Type.Results() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 687 | t := n.Left.Type |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 688 | if n.List.Len() != 0 && n.List.First().Op == OAS { |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 689 | break |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 690 | } |
| 691 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 692 | n.Left = walkexpr(n.Left, init) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 693 | walkexprlist(n.List.Slice(), init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 694 | |
Russ Cox | 92dba0d | 2015-04-01 16:02:34 -0400 | [diff] [blame] | 695 | if n.Left.Op == ONAME && n.Left.Sym.Name == "Sqrt" && n.Left.Sym.Pkg.Path == "math" { |
Michael Munday | 613ba6c | 2016-04-12 12:26:17 -0400 | [diff] [blame] | 696 | if Thearch.LinkArch.InFamily(sys.AMD64, sys.ARM, sys.ARM64, sys.PPC64, sys.S390X) { |
Russ Cox | 92dba0d | 2015-04-01 16:02:34 -0400 | [diff] [blame] | 697 | n.Op = OSQRT |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 698 | n.Left = n.List.First() |
| 699 | n.List.Set(nil) |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 700 | break opswitch |
Russ Cox | 92dba0d | 2015-04-01 16:02:34 -0400 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 704 | ll := ascompatte(n.Op, n, n.Isddd, t.Params(), n.List.Slice(), 0, init) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 705 | n.List.Set(reorder1(ll)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 706 | |
| 707 | case OCALLMETH: |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 708 | t := n.Left.Type |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 709 | if n.List.Len() != 0 && n.List.First().Op == OAS { |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 710 | break |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 711 | } |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 712 | n.Left = walkexpr(n.Left, init) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 713 | walkexprlist(n.List.Slice(), init) |
Matthew Dempsky | f91b832 | 2016-03-09 20:54:59 -0800 | [diff] [blame] | 714 | ll := ascompatte(n.Op, n, false, t.Recvs(), []*Node{n.Left.Left}, 0, init) |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 715 | lr := ascompatte(n.Op, n, n.Isddd, t.Params(), n.List.Slice(), 0, init) |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 716 | ll = append(ll, lr...) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 717 | n.Left.Left = nil |
| 718 | ullmancalc(n.Left) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 719 | n.List.Set(reorder1(ll)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 720 | |
| 721 | case OAS: |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 722 | init.AppendNodes(&n.Ninit) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 723 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 724 | n.Left = walkexpr(n.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 725 | n.Left = safeexpr(n.Left, init) |
| 726 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 727 | if oaslit(n, init) { |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 728 | break |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 729 | } |
| 730 | |
Ian Lance Taylor | 9e902f0 | 2015-10-20 10:00:07 -0700 | [diff] [blame] | 731 | if n.Right == nil || iszero(n.Right) && !instrumenting { |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 732 | break |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | switch n.Right.Op { |
| 736 | default: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 737 | n.Right = walkexpr(n.Right, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 738 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 739 | case ODOTTYPE: |
Russ Cox | 4224d81 | 2015-03-20 00:06:10 -0400 | [diff] [blame] | 740 | // TODO(rsc): The Isfat is for consistency with componentgen and orderexpr. |
| 741 | // It needs to be removed in all three places. |
| 742 | // That would allow inlining x.(struct{*int}) the same as x.(*int). |
Ian Lance Taylor | 9e902f0 | 2015-10-20 10:00:07 -0700 | [diff] [blame] | 743 | if isdirectiface(n.Right.Type) && !Isfat(n.Right.Type) && !instrumenting { |
Russ Cox | 4224d81 | 2015-03-20 00:06:10 -0400 | [diff] [blame] | 744 | // handled directly during cgen |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 745 | n.Right = walkexpr(n.Right, init) |
Russ Cox | 4224d81 | 2015-03-20 00:06:10 -0400 | [diff] [blame] | 746 | break |
| 747 | } |
| 748 | |
David Chase | ffe7fbf | 2015-03-27 12:34:45 -0400 | [diff] [blame] | 749 | // x = i.(T); n.Left is x, n.Right.Left is i. |
Russ Cox | 4224d81 | 2015-03-20 00:06:10 -0400 | [diff] [blame] | 750 | // orderstmt made sure x is addressable. |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 751 | n.Right.Left = walkexpr(n.Right.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 752 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 753 | n1 := Nod(OADDR, n.Left, nil) |
| 754 | r := n.Right // i.(T) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 755 | |
Russ Cox | 4224d81 | 2015-03-20 00:06:10 -0400 | [diff] [blame] | 756 | if Debug_typeassert > 0 { |
| 757 | Warn("type assertion not inlined") |
| 758 | } |
| 759 | |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 760 | fn := syslook(assertFuncName(r.Left.Type, r.Type, false)) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 761 | fn = substArgTypes(fn, r.Left.Type, r.Type) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 762 | |
| 763 | n = mkcall1(fn, nil, init, typename(r.Type), r.Left, n1) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 764 | n = walkexpr(n, init) |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 765 | break opswitch |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 766 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 767 | case ORECV: |
David Chase | ffe7fbf | 2015-03-27 12:34:45 -0400 | [diff] [blame] | 768 | // x = <-c; n.Left is x, n.Right.Left is c. |
Russ Cox | 4224d81 | 2015-03-20 00:06:10 -0400 | [diff] [blame] | 769 | // orderstmt made sure x is addressable. |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 770 | n.Right.Left = walkexpr(n.Right.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 771 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 772 | n1 := Nod(OADDR, n.Left, nil) |
| 773 | r := n.Right.Left // the channel |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 774 | n = mkcall1(chanfn("chanrecv1", 2, r.Type), nil, init, typename(r.Type), r, n1) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 775 | n = walkexpr(n, init) |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 776 | break opswitch |
Russ Cox | 8552047 | 2015-05-06 12:34:30 -0400 | [diff] [blame] | 777 | |
| 778 | case OAPPEND: |
| 779 | // x = append(...) |
| 780 | r := n.Right |
| 781 | if r.Isddd { |
| 782 | r = appendslice(r, init) // also works for append(slice, string). |
| 783 | } else { |
| 784 | r = walkappend(r, init, n) |
| 785 | } |
| 786 | n.Right = r |
| 787 | if r.Op == OAPPEND { |
| 788 | // Left in place for back end. |
| 789 | // Do not add a new write barrier. |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 790 | break opswitch |
Russ Cox | 8552047 | 2015-05-06 12:34:30 -0400 | [diff] [blame] | 791 | } |
| 792 | // Otherwise, lowered for race detector. |
| 793 | // Treat as ordinary assignment. |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | if n.Left != nil && n.Right != nil { |
Josh Bleecher Snyder | 6888405 | 2016-03-26 08:17:43 -0700 | [diff] [blame] | 797 | dd := n.Dodata |
| 798 | n = convas(n, init) |
| 799 | n.Dodata = dd |
Ian Lance Taylor | 188e3d2 | 2016-02-26 14:28:48 -0800 | [diff] [blame] | 800 | n = applywritebarrier(n) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 801 | } |
| 802 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 803 | case OAS2: |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 804 | init.AppendNodes(&n.Ninit) |
| 805 | walkexprlistsafe(n.List.Slice(), init) |
| 806 | walkexprlistsafe(n.Rlist.Slice(), init) |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 807 | ll := ascompatee(OAS, n.List.Slice(), n.Rlist.Slice(), init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 808 | ll = reorder3(ll) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 809 | for i, n := range ll { |
| 810 | ll[i] = applywritebarrier(n) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 811 | } |
| 812 | n = liststmt(ll) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 813 | |
| 814 | // a,b,... = fn() |
| 815 | case OAS2FUNC: |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 816 | init.AppendNodes(&n.Ninit) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 817 | |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 818 | r := n.Rlist.First() |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 819 | walkexprlistsafe(n.List.Slice(), init) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 820 | r = walkexpr(r, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 821 | |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 822 | ll := ascompatet(n.Op, n.List, r.Type, 0, init) |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 823 | for i, n := range ll { |
| 824 | ll[i] = applywritebarrier(n) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 825 | } |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 826 | n = liststmt(append([]*Node{r}, ll...)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 827 | |
| 828 | // x, y = <-c |
| 829 | // orderstmt made sure x is addressable. |
| 830 | case OAS2RECV: |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 831 | init.AppendNodes(&n.Ninit) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 832 | |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 833 | r := n.Rlist.First() |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 834 | walkexprlistsafe(n.List.Slice(), init) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 835 | r.Left = walkexpr(r.Left, init) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 836 | var n1 *Node |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 837 | if isblank(n.List.First()) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 838 | n1 = nodnil() |
| 839 | } else { |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 840 | n1 = Nod(OADDR, n.List.First(), nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 841 | } |
| 842 | n1.Etype = 1 // addr does not escape |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 843 | fn := chanfn("chanrecv2", 2, r.Left.Type) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 844 | r = mkcall1(fn, n.List.Second().Type, init, typename(r.Left.Type), r.Left, n1) |
| 845 | n = Nod(OAS, n.List.Second(), r) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 846 | n = typecheck(n, Etop) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 847 | |
| 848 | // a,b = m[i]; |
| 849 | case OAS2MAPR: |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 850 | init.AppendNodes(&n.Ninit) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 851 | |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 852 | r := n.Rlist.First() |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 853 | walkexprlistsafe(n.List.Slice(), init) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 854 | r.Left = walkexpr(r.Left, init) |
| 855 | r.Right = walkexpr(r.Right, init) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 856 | t := r.Left.Type |
| 857 | p := "" |
Josh Bleecher Snyder | 093a9a1 | 2016-03-28 21:48:47 -0700 | [diff] [blame] | 858 | if t.Val().Width <= 128 { // Check ../../runtime/hashmap.go:maxValueSize before changing. |
Matthew Dempsky | 1c2bdfb | 2016-03-10 05:22:14 -0800 | [diff] [blame] | 859 | switch algtype(t.Key()) { |
Matthew Dempsky | 423b0cc | 2015-12-07 02:12:12 -0800 | [diff] [blame] | 860 | case AMEM32: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 861 | p = "mapaccess2_fast32" |
Matthew Dempsky | 423b0cc | 2015-12-07 02:12:12 -0800 | [diff] [blame] | 862 | case AMEM64: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 863 | p = "mapaccess2_fast64" |
Matthew Dempsky | 423b0cc | 2015-12-07 02:12:12 -0800 | [diff] [blame] | 864 | case ASTRING: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 865 | p = "mapaccess2_faststr" |
| 866 | } |
| 867 | } |
| 868 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 869 | var key *Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 870 | if p != "" { |
| 871 | // fast versions take key by value |
| 872 | key = r.Right |
| 873 | } else { |
| 874 | // standard version takes key by reference |
| 875 | // orderexpr made sure key is addressable. |
| 876 | key = Nod(OADDR, r.Right, nil) |
| 877 | |
| 878 | p = "mapaccess2" |
| 879 | } |
| 880 | |
| 881 | // from: |
| 882 | // a,b = m[i] |
| 883 | // to: |
| 884 | // var,b = mapaccess2*(t, m, i) |
| 885 | // a = *var |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 886 | a := n.List.First() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 887 | |
Keith Randall | 60fd32a | 2016-04-19 08:31:04 -0700 | [diff] [blame] | 888 | if w := t.Val().Width; w <= 1024 { // 1024 must match ../../../../runtime/hashmap.go:maxZero |
| 889 | fn := mapfn(p, t) |
| 890 | r = mkcall1(fn, fn.Type.Results(), init, typename(t), r.Left, key) |
| 891 | } else { |
| 892 | fn := mapfn("mapaccess2_fat", t) |
| 893 | z := zeroaddr(w) |
| 894 | r = mkcall1(fn, fn.Type.Results(), init, typename(t), r.Left, key, z) |
| 895 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 896 | |
| 897 | // mapaccess2* returns a typed bool, but due to spec changes, |
| 898 | // the boolean result of i.(T) is now untyped so we make it the |
| 899 | // same type as the variable on the lhs. |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 900 | if !isblank(n.List.Second()) { |
Matthew Dempsky | 1c2bdfb | 2016-03-10 05:22:14 -0800 | [diff] [blame] | 901 | r.Type.Field(1).Type = n.List.Second().Type |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 902 | } |
Ian Lance Taylor | c63dbd8 | 2016-03-10 10:13:42 -0800 | [diff] [blame] | 903 | n.Rlist.Set1(r) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 904 | n.Op = OAS2FUNC |
| 905 | |
| 906 | // don't generate a = *var if a is _ |
| 907 | if !isblank(a) { |
Josh Bleecher Snyder | 093a9a1 | 2016-03-28 21:48:47 -0700 | [diff] [blame] | 908 | var_ := temp(Ptrto(t.Val())) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 909 | var_.Typecheck = 1 |
Keith Randall | 3c1a4c1 | 2016-04-19 21:06:53 -0700 | [diff] [blame] | 910 | var_.NonNil = true // mapaccess always returns a non-nil pointer |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 911 | n.List.SetIndex(0, var_) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 912 | n = walkexpr(n, init) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 913 | init.Append(n) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 914 | n = Nod(OAS, a, Nod(OIND, var_, nil)) |
| 915 | } |
| 916 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 917 | n = typecheck(n, Etop) |
| 918 | n = walkexpr(n, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 919 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 920 | case ODELETE: |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 921 | init.AppendNodes(&n.Ninit) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 922 | map_ := n.List.First() |
| 923 | key := n.List.Second() |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 924 | map_ = walkexpr(map_, init) |
| 925 | key = walkexpr(key, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 926 | |
| 927 | // orderstmt made sure key is addressable. |
| 928 | key = Nod(OADDR, key, nil) |
| 929 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 930 | t := map_.Type |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 931 | n = mkcall1(mapfndel("mapdelete", t), nil, init, typename(t), map_, key) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 932 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 933 | case OAS2DOTTYPE: |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 934 | e := n.Rlist.First() // i.(T) |
Russ Cox | 4224d81 | 2015-03-20 00:06:10 -0400 | [diff] [blame] | 935 | // TODO(rsc): The Isfat is for consistency with componentgen and orderexpr. |
| 936 | // It needs to be removed in all three places. |
| 937 | // That would allow inlining x.(struct{*int}) the same as x.(*int). |
Ian Lance Taylor | 9e902f0 | 2015-10-20 10:00:07 -0700 | [diff] [blame] | 938 | if isdirectiface(e.Type) && !Isfat(e.Type) && !instrumenting { |
Russ Cox | 4224d81 | 2015-03-20 00:06:10 -0400 | [diff] [blame] | 939 | // handled directly during gen. |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 940 | walkexprlistsafe(n.List.Slice(), init) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 941 | e.Left = walkexpr(e.Left, init) |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 942 | break |
Russ Cox | 4224d81 | 2015-03-20 00:06:10 -0400 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | // res, ok = i.(T) |
| 946 | // orderstmt made sure a is addressable. |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 947 | init.AppendNodes(&n.Ninit) |
Josh Bleecher Snyder | 55b4516 | 2015-03-17 13:56:29 -0700 | [diff] [blame] | 948 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 949 | walkexprlistsafe(n.List.Slice(), init) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 950 | e.Left = walkexpr(e.Left, init) |
Josh Bleecher Snyder | 55b4516 | 2015-03-17 13:56:29 -0700 | [diff] [blame] | 951 | t := e.Type // T |
| 952 | from := e.Left // i |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 953 | |
Josh Bleecher Snyder | 55b4516 | 2015-03-17 13:56:29 -0700 | [diff] [blame] | 954 | oktype := Types[TBOOL] |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 955 | ok := n.List.Second() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 956 | if !isblank(ok) { |
Josh Bleecher Snyder | 55b4516 | 2015-03-17 13:56:29 -0700 | [diff] [blame] | 957 | oktype = ok.Type |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 958 | } |
Josh Bleecher Snyder | 55b4516 | 2015-03-17 13:56:29 -0700 | [diff] [blame] | 959 | |
Brad Fitzpatrick | e3a9dca | 2016-03-04 22:02:27 +0000 | [diff] [blame] | 960 | fromKind := from.Type.iet() |
| 961 | toKind := t.iet() |
Josh Bleecher Snyder | 25e793d | 2015-03-17 15:14:31 -0700 | [diff] [blame] | 962 | |
Josh Bleecher Snyder | 562d06f | 2016-06-06 08:29:52 -0700 | [diff] [blame] | 963 | res := n.List.First() |
Josh Bleecher Snyder | 615a52b | 2016-06-06 12:38:19 -0700 | [diff] [blame] | 964 | scalar := !haspointers(res.Type) |
Josh Bleecher Snyder | 562d06f | 2016-06-06 08:29:52 -0700 | [diff] [blame] | 965 | |
Josh Bleecher Snyder | 25e793d | 2015-03-17 15:14:31 -0700 | [diff] [blame] | 966 | // Avoid runtime calls in a few cases of the form _, ok := i.(T). |
| 967 | // This is faster and shorter and allows the corresponding assertX2X2 |
| 968 | // routines to skip nil checks on their last argument. |
Josh Bleecher Snyder | 615a52b | 2016-06-06 12:38:19 -0700 | [diff] [blame] | 969 | // Also avoid runtime calls for converting interfaces to scalar concrete types. |
| 970 | if isblank(res) || (scalar && toKind == 'T') { |
Josh Bleecher Snyder | 25e793d | 2015-03-17 15:14:31 -0700 | [diff] [blame] | 971 | var fast *Node |
Josh Bleecher Snyder | 562d06f | 2016-06-06 08:29:52 -0700 | [diff] [blame] | 972 | switch toKind { |
| 973 | case 'T': |
| 974 | tab := Nod(OITAB, from, nil) |
| 975 | if fromKind == 'E' { |
| 976 | typ := Nod(OCONVNOP, typename(t), nil) |
| 977 | typ.Type = Ptrto(Types[TUINTPTR]) |
| 978 | fast = Nod(OEQ, tab, typ) |
| 979 | break |
| 980 | } |
| 981 | fast = Nod(OANDAND, |
| 982 | Nod(ONE, nodnil(), tab), |
| 983 | Nod(OEQ, itabType(tab), typename(t)), |
| 984 | ) |
| 985 | case 'E': |
Josh Bleecher Snyder | 25e793d | 2015-03-17 15:14:31 -0700 | [diff] [blame] | 986 | tab := Nod(OITAB, from, nil) |
Josh Bleecher Snyder | 6d44844 | 2015-03-19 09:49:25 -0700 | [diff] [blame] | 987 | fast = Nod(ONE, nodnil(), tab) |
Josh Bleecher Snyder | 25e793d | 2015-03-17 15:14:31 -0700 | [diff] [blame] | 988 | } |
| 989 | if fast != nil { |
Josh Bleecher Snyder | 615a52b | 2016-06-06 12:38:19 -0700 | [diff] [blame] | 990 | if isblank(res) { |
| 991 | if Debug_typeassert > 0 { |
| 992 | Warn("type assertion (ok only) inlined") |
| 993 | } |
| 994 | n = Nod(OAS, ok, fast) |
| 995 | n = typecheck(n, Etop) |
| 996 | } else { |
| 997 | if Debug_typeassert > 0 { |
| 998 | Warn("type assertion (scalar result) inlined") |
| 999 | } |
| 1000 | n = Nod(OIF, ok, nil) |
| 1001 | n.Likely = 1 |
| 1002 | if isblank(ok) { |
| 1003 | n.Left = fast |
| 1004 | } else { |
| 1005 | n.Ninit.Set1(Nod(OAS, ok, fast)) |
| 1006 | } |
| 1007 | n.Nbody.Set1(Nod(OAS, res, ifaceData(from, res.Type))) |
| 1008 | n.Rlist.Set1(Nod(OAS, res, nil)) |
| 1009 | n = typecheck(n, Etop) |
Russ Cox | 4224d81 | 2015-03-20 00:06:10 -0400 | [diff] [blame] | 1010 | } |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 1011 | break |
Josh Bleecher Snyder | 25e793d | 2015-03-17 15:14:31 -0700 | [diff] [blame] | 1012 | } |
| 1013 | } |
| 1014 | |
Josh Bleecher Snyder | 55b4516 | 2015-03-17 13:56:29 -0700 | [diff] [blame] | 1015 | var resptr *Node // &res |
Josh Bleecher Snyder | 562d06f | 2016-06-06 08:29:52 -0700 | [diff] [blame] | 1016 | if isblank(res) { |
Josh Bleecher Snyder | 55b4516 | 2015-03-17 13:56:29 -0700 | [diff] [blame] | 1017 | resptr = nodnil() |
| 1018 | } else { |
Josh Bleecher Snyder | 562d06f | 2016-06-06 08:29:52 -0700 | [diff] [blame] | 1019 | resptr = Nod(OADDR, res, nil) |
Josh Bleecher Snyder | 55b4516 | 2015-03-17 13:56:29 -0700 | [diff] [blame] | 1020 | } |
| 1021 | resptr.Etype = 1 // addr does not escape |
| 1022 | |
Russ Cox | 4224d81 | 2015-03-20 00:06:10 -0400 | [diff] [blame] | 1023 | if Debug_typeassert > 0 { |
| 1024 | Warn("type assertion not inlined") |
| 1025 | } |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 1026 | fn := syslook(assertFuncName(from.Type, t, true)) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1027 | fn = substArgTypes(fn, from.Type, t) |
Josh Bleecher Snyder | 55b4516 | 2015-03-17 13:56:29 -0700 | [diff] [blame] | 1028 | call := mkcall1(fn, oktype, init, typename(t), from, resptr) |
| 1029 | n = Nod(OAS, ok, call) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1030 | n = typecheck(n, Etop) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1031 | |
Russ Cox | 4224d81 | 2015-03-20 00:06:10 -0400 | [diff] [blame] | 1032 | case ODOTTYPE, ODOTTYPE2: |
| 1033 | if !isdirectiface(n.Type) || Isfat(n.Type) { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 1034 | Fatalf("walkexpr ODOTTYPE") // should see inside OAS only |
Russ Cox | 4224d81 | 2015-03-20 00:06:10 -0400 | [diff] [blame] | 1035 | } |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1036 | n.Left = walkexpr(n.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1037 | |
| 1038 | case OCONVIFACE: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1039 | n.Left = walkexpr(n.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1040 | |
Michel Lespinasse | 7427f2c | 2016-03-18 16:20:20 -0700 | [diff] [blame] | 1041 | // Optimize convT2E or convT2I as a two-word copy when T is pointer-shaped. |
| 1042 | if isdirectiface(n.Left.Type) { |
| 1043 | var t *Node |
Matthew Dempsky | 00e5a68 | 2016-04-01 13:36:24 -0700 | [diff] [blame] | 1044 | if n.Type.IsEmptyInterface() { |
Michel Lespinasse | 7427f2c | 2016-03-18 16:20:20 -0700 | [diff] [blame] | 1045 | t = typename(n.Left.Type) |
| 1046 | } else { |
| 1047 | t = itabname(n.Left.Type, n.Type) |
| 1048 | } |
| 1049 | l := Nod(OEFACE, t, n.Left) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1050 | l.Type = n.Type |
| 1051 | l.Typecheck = n.Typecheck |
| 1052 | n = l |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 1053 | break |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1054 | } |
| 1055 | |
Ian Lance Taylor | f444b8a | 2016-03-09 20:29:21 -0800 | [diff] [blame] | 1056 | var ll []*Node |
Matthew Dempsky | 00e5a68 | 2016-04-01 13:36:24 -0700 | [diff] [blame] | 1057 | if n.Type.IsEmptyInterface() { |
Matthew Dempsky | 1624a9c | 2016-03-30 14:45:47 -0700 | [diff] [blame] | 1058 | if !n.Left.Type.IsInterface() { |
Michel Lespinasse | 859b63c | 2016-03-18 17:21:33 -0700 | [diff] [blame] | 1059 | ll = append(ll, typename(n.Left.Type)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1060 | } |
Michel Lespinasse | 859b63c | 2016-03-18 17:21:33 -0700 | [diff] [blame] | 1061 | } else { |
Matthew Dempsky | 1624a9c | 2016-03-30 14:45:47 -0700 | [diff] [blame] | 1062 | if n.Left.Type.IsInterface() { |
Michel Lespinasse | 859b63c | 2016-03-18 17:21:33 -0700 | [diff] [blame] | 1063 | ll = append(ll, typename(n.Type)) |
| 1064 | } else { |
| 1065 | ll = append(ll, itabname(n.Left.Type, n.Type)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | |
Matthew Dempsky | 1624a9c | 2016-03-30 14:45:47 -0700 | [diff] [blame] | 1069 | if n.Left.Type.IsInterface() { |
Ian Lance Taylor | f444b8a | 2016-03-09 20:29:21 -0800 | [diff] [blame] | 1070 | ll = append(ll, n.Left) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1071 | } else { |
| 1072 | // regular types are passed by reference to avoid C vararg calls |
David Chase | ffe7fbf | 2015-03-27 12:34:45 -0400 | [diff] [blame] | 1073 | // orderexpr arranged for n.Left to be a temporary for all |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1074 | // the conversions it could see. comparison of an interface |
| 1075 | // with a non-interface, especially in a switch on interface value |
| 1076 | // with non-interface cases, is not visible to orderstmt, so we |
| 1077 | // have to fall back on allocating a temp here. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 1078 | if islvalue(n.Left) { |
Ian Lance Taylor | f444b8a | 2016-03-09 20:29:21 -0800 | [diff] [blame] | 1079 | ll = append(ll, Nod(OADDR, n.Left, nil)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1080 | } else { |
Ian Lance Taylor | f444b8a | 2016-03-09 20:29:21 -0800 | [diff] [blame] | 1081 | ll = append(ll, Nod(OADDR, copyexpr(n.Left, n.Left.Type, init), nil)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1082 | } |
David Chase | 2270133 | 2015-03-27 11:21:14 -0400 | [diff] [blame] | 1083 | dowidth(n.Left.Type) |
| 1084 | r := nodnil() |
| 1085 | if n.Esc == EscNone && n.Left.Type.Width <= 1024 { |
| 1086 | // Allocate stack buffer for value stored in interface. |
| 1087 | r = temp(n.Left.Type) |
| 1088 | r = Nod(OAS, r, nil) // zero temp |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1089 | r = typecheck(r, Etop) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 1090 | init.Append(r) |
David Chase | 2270133 | 2015-03-27 11:21:14 -0400 | [diff] [blame] | 1091 | r = Nod(OADDR, r.Left, nil) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1092 | r = typecheck(r, Erv) |
David Chase | 2270133 | 2015-03-27 11:21:14 -0400 | [diff] [blame] | 1093 | } |
Ian Lance Taylor | f444b8a | 2016-03-09 20:29:21 -0800 | [diff] [blame] | 1094 | ll = append(ll, r) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1095 | } |
| 1096 | |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 1097 | fn := syslook(convFuncName(n.Left.Type, n.Type)) |
Matthew Dempsky | 1624a9c | 2016-03-30 14:45:47 -0700 | [diff] [blame] | 1098 | if !n.Left.Type.IsInterface() { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1099 | fn = substArgTypes(fn, n.Left.Type, n.Left.Type, n.Type) |
David Chase | 2270133 | 2015-03-27 11:21:14 -0400 | [diff] [blame] | 1100 | } else { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1101 | fn = substArgTypes(fn, n.Left.Type, n.Type) |
David Chase | 2270133 | 2015-03-27 11:21:14 -0400 | [diff] [blame] | 1102 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1103 | dowidth(fn.Type) |
| 1104 | n = Nod(OCALL, fn, nil) |
Ian Lance Taylor | f444b8a | 2016-03-09 20:29:21 -0800 | [diff] [blame] | 1105 | n.List.Set(ll) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1106 | n = typecheck(n, Erv) |
| 1107 | n = walkexpr(n, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1108 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 1109 | case OCONV, OCONVNOP: |
Matthew Dempsky | c6e11fe | 2016-04-06 12:01:40 -0700 | [diff] [blame] | 1110 | if Thearch.LinkArch.Family == sys.ARM { |
Matthew Dempsky | e76fc1b | 2016-03-30 15:09:25 -0700 | [diff] [blame] | 1111 | if n.Left.Type.IsFloat() { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1112 | if n.Type.Etype == TINT64 { |
| 1113 | n = mkcall("float64toint64", n.Type, init, conv(n.Left, Types[TFLOAT64])) |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 1114 | break |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | if n.Type.Etype == TUINT64 { |
| 1118 | n = mkcall("float64touint64", n.Type, init, conv(n.Left, Types[TFLOAT64])) |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 1119 | break |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | |
Matthew Dempsky | e76fc1b | 2016-03-30 15:09:25 -0700 | [diff] [blame] | 1123 | if n.Type.IsFloat() { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1124 | if n.Left.Type.Etype == TINT64 { |
Cherry Zhang | 59e11d7 | 2016-05-31 11:27:16 -0400 | [diff] [blame] | 1125 | n = conv(mkcall("int64tofloat64", Types[TFLOAT64], init, conv(n.Left, Types[TINT64])), n.Type) |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 1126 | break |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | if n.Left.Type.Etype == TUINT64 { |
Cherry Zhang | 59e11d7 | 2016-05-31 11:27:16 -0400 | [diff] [blame] | 1130 | n = conv(mkcall("uint64tofloat64", Types[TFLOAT64], init, conv(n.Left, Types[TUINT64])), n.Type) |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 1131 | break |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | |
Keith Randall | df2f813 | 2016-07-21 10:37:59 -0700 | [diff] [blame] | 1136 | if Thearch.LinkArch.Family == sys.I386 { |
| 1137 | if n.Left.Type.IsFloat() { |
| 1138 | if n.Type.Etype == TINT64 { |
| 1139 | n = mkcall("float64toint64", n.Type, init, conv(n.Left, Types[TFLOAT64])) |
| 1140 | break |
| 1141 | } |
| 1142 | |
| 1143 | if n.Type.Etype == TUINT64 { |
| 1144 | n = mkcall("float64touint64", n.Type, init, conv(n.Left, Types[TFLOAT64])) |
| 1145 | break |
| 1146 | } |
Cherry Zhang | e6f1a88 | 2016-08-16 12:14:54 -0400 | [diff] [blame] | 1147 | if n.Type.Etype == TUINT32 || n.Type.Etype == TUINT || n.Type.Etype == TUINTPTR { |
Keith Randall | df2f813 | 2016-07-21 10:37:59 -0700 | [diff] [blame] | 1148 | n = mkcall("float64touint32", n.Type, init, conv(n.Left, Types[TFLOAT64])) |
| 1149 | break |
| 1150 | } |
| 1151 | } |
| 1152 | if n.Type.IsFloat() { |
| 1153 | if n.Left.Type.Etype == TINT64 { |
| 1154 | n = conv(mkcall("int64tofloat64", Types[TFLOAT64], init, conv(n.Left, Types[TINT64])), n.Type) |
| 1155 | break |
| 1156 | } |
| 1157 | |
| 1158 | if n.Left.Type.Etype == TUINT64 { |
| 1159 | n = conv(mkcall("uint64tofloat64", Types[TFLOAT64], init, conv(n.Left, Types[TUINT64])), n.Type) |
| 1160 | break |
| 1161 | } |
Cherry Zhang | e6f1a88 | 2016-08-16 12:14:54 -0400 | [diff] [blame] | 1162 | if n.Left.Type.Etype == TUINT32 || n.Left.Type.Etype == TUINT || n.Left.Type.Etype == TUINTPTR { |
Keith Randall | df2f813 | 2016-07-21 10:37:59 -0700 | [diff] [blame] | 1163 | n = conv(mkcall("uint32tofloat64", Types[TFLOAT64], init, conv(n.Left, Types[TUINT32])), n.Type) |
| 1164 | break |
| 1165 | } |
| 1166 | } |
| 1167 | } |
| 1168 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1169 | n.Left = walkexpr(n.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1170 | |
| 1171 | case OANDNOT: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1172 | n.Left = walkexpr(n.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1173 | n.Op = OAND |
| 1174 | n.Right = Nod(OCOM, n.Right, nil) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1175 | n.Right = typecheck(n.Right, Erv) |
| 1176 | n.Right = walkexpr(n.Right, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1177 | |
| 1178 | case OMUL: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1179 | n.Left = walkexpr(n.Left, init) |
| 1180 | n.Right = walkexpr(n.Right, init) |
| 1181 | n = walkmul(n, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1182 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 1183 | case ODIV, OMOD: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1184 | n.Left = walkexpr(n.Left, init) |
| 1185 | n.Right = walkexpr(n.Right, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1186 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 1187 | // rewrite complex div into function call. |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 1188 | et := n.Left.Type.Etype |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1189 | |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 1190 | if Iscomplex[et] && n.Op == ODIV { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1191 | t := n.Type |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1192 | n = mkcall("complex128div", Types[TCOMPLEX128], init, conv(n.Left, Types[TCOMPLEX128]), conv(n.Right, Types[TCOMPLEX128])) |
| 1193 | n = conv(n, t) |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 1194 | break |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | // Nothing to do for float divisions. |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 1198 | if Isfloat[et] { |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 1199 | break |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | // Try rewriting as shifts or magic multiplies. |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1203 | n = walkdiv(n, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1204 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 1205 | // rewrite 64-bit div and mod into function calls |
| 1206 | // on 32-bit architectures. |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1207 | switch n.Op { |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 1208 | case OMOD, ODIV: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1209 | if Widthreg >= 8 || (et != TUINT64 && et != TINT64) { |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 1210 | break opswitch |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1211 | } |
Matthew Dempsky | 8b3670f | 2015-03-06 12:02:24 -0800 | [diff] [blame] | 1212 | var fn string |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1213 | if et == TINT64 { |
Matthew Dempsky | 8b3670f | 2015-03-06 12:02:24 -0800 | [diff] [blame] | 1214 | fn = "int64" |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1215 | } else { |
Matthew Dempsky | 8b3670f | 2015-03-06 12:02:24 -0800 | [diff] [blame] | 1216 | fn = "uint64" |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1217 | } |
| 1218 | if n.Op == ODIV { |
Matthew Dempsky | 8b3670f | 2015-03-06 12:02:24 -0800 | [diff] [blame] | 1219 | fn += "div" |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1220 | } else { |
Matthew Dempsky | 8b3670f | 2015-03-06 12:02:24 -0800 | [diff] [blame] | 1221 | fn += "mod" |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1222 | } |
Matthew Dempsky | 8b3670f | 2015-03-06 12:02:24 -0800 | [diff] [blame] | 1223 | n = mkcall(fn, n.Type, init, conv(n.Left, Types[et]), conv(n.Right, Types[et])) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1224 | } |
| 1225 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1226 | case OINDEX: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1227 | n.Left = walkexpr(n.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1228 | |
| 1229 | // save the original node for bounds checking elision. |
| 1230 | // If it was a ODIV/OMOD walk might rewrite it. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1231 | r := n.Right |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1232 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1233 | n.Right = walkexpr(n.Right, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1234 | |
| 1235 | // if range of type cannot exceed static array bound, |
| 1236 | // disable bounds check. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 1237 | if n.Bounded { |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 1238 | break |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1239 | } |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1240 | t := n.Left.Type |
Matthew Dempsky | e76fc1b | 2016-03-30 15:09:25 -0700 | [diff] [blame] | 1241 | if t != nil && t.IsPtr() { |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 1242 | t = t.Elem() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1243 | } |
Matthew Dempsky | 1624a9c | 2016-03-30 14:45:47 -0700 | [diff] [blame] | 1244 | if t.IsArray() { |
Josh Bleecher Snyder | 3a0783c | 2016-03-31 14:46:04 -0700 | [diff] [blame] | 1245 | n.Bounded = bounded(r, t.NumElem()) |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 1246 | if Debug['m'] != 0 && n.Bounded && !Isconst(n.Right, CTINT) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1247 | Warn("index bounds check elided") |
| 1248 | } |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 1249 | if Smallintconst(n.Right) && !n.Bounded { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1250 | Yyerror("index out of bounds") |
| 1251 | } |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 1252 | } else if Isconst(n.Left, CTSTR) { |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 1253 | n.Bounded = bounded(r, int64(len(n.Left.Val().U.(string)))) |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 1254 | if Debug['m'] != 0 && n.Bounded && !Isconst(n.Right, CTINT) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1255 | Warn("index bounds check elided") |
| 1256 | } |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 1257 | if Smallintconst(n.Right) { |
| 1258 | if !n.Bounded { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1259 | Yyerror("index out of bounds") |
| 1260 | } else { |
| 1261 | // replace "abc"[1] with 'b'. |
| 1262 | // delayed until now because "abc"[1] is not |
| 1263 | // an ideal constant. |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 1264 | v := n.Right.Int64() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1265 | |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 1266 | Nodconst(n, n.Type, int64(n.Left.Val().U.(string)[v])) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1267 | n.Typecheck = 1 |
| 1268 | } |
| 1269 | } |
| 1270 | } |
| 1271 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 1272 | if Isconst(n.Right, CTINT) { |
Robert Griesemer | 07749ae | 2016-03-21 10:52:03 -0700 | [diff] [blame] | 1273 | if n.Right.Val().U.(*Mpint).CmpInt64(0) < 0 || n.Right.Val().U.(*Mpint).Cmp(Maxintval[TINT]) > 0 { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1274 | Yyerror("index out of bounds") |
| 1275 | } |
| 1276 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1277 | |
| 1278 | case OINDEXMAP: |
| 1279 | if n.Etype == 1 { |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 1280 | break |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1281 | } |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1282 | n.Left = walkexpr(n.Left, init) |
| 1283 | n.Right = walkexpr(n.Right, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1284 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1285 | t := n.Left.Type |
| 1286 | p := "" |
Josh Bleecher Snyder | 093a9a1 | 2016-03-28 21:48:47 -0700 | [diff] [blame] | 1287 | if t.Val().Width <= 128 { // Check ../../runtime/hashmap.go:maxValueSize before changing. |
Matthew Dempsky | 1c2bdfb | 2016-03-10 05:22:14 -0800 | [diff] [blame] | 1288 | switch algtype(t.Key()) { |
Matthew Dempsky | 423b0cc | 2015-12-07 02:12:12 -0800 | [diff] [blame] | 1289 | case AMEM32: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1290 | p = "mapaccess1_fast32" |
Matthew Dempsky | 423b0cc | 2015-12-07 02:12:12 -0800 | [diff] [blame] | 1291 | case AMEM64: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1292 | p = "mapaccess1_fast64" |
Matthew Dempsky | 423b0cc | 2015-12-07 02:12:12 -0800 | [diff] [blame] | 1293 | case ASTRING: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1294 | p = "mapaccess1_faststr" |
| 1295 | } |
| 1296 | } |
| 1297 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1298 | var key *Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1299 | if p != "" { |
| 1300 | // fast versions take key by value |
| 1301 | key = n.Right |
| 1302 | } else { |
| 1303 | // standard version takes key by reference. |
| 1304 | // orderexpr made sure key is addressable. |
| 1305 | key = Nod(OADDR, n.Right, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1306 | p = "mapaccess1" |
| 1307 | } |
| 1308 | |
Keith Randall | 60fd32a | 2016-04-19 08:31:04 -0700 | [diff] [blame] | 1309 | if w := t.Val().Width; w <= 1024 { // 1024 must match ../../../../runtime/hashmap.go:maxZero |
| 1310 | n = mkcall1(mapfn(p, t), Ptrto(t.Val()), init, typename(t), n.Left, key) |
| 1311 | } else { |
| 1312 | p = "mapaccess1_fat" |
| 1313 | z := zeroaddr(w) |
| 1314 | n = mkcall1(mapfn(p, t), Ptrto(t.Val()), init, typename(t), n.Left, key, z) |
| 1315 | } |
Keith Randall | 3c1a4c1 | 2016-04-19 21:06:53 -0700 | [diff] [blame] | 1316 | n.NonNil = true // mapaccess always returns a non-nil pointer |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1317 | n = Nod(OIND, n, nil) |
Josh Bleecher Snyder | 093a9a1 | 2016-03-28 21:48:47 -0700 | [diff] [blame] | 1318 | n.Type = t.Val() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1319 | n.Typecheck = 1 |
| 1320 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1321 | case ORECV: |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 1322 | Fatalf("walkexpr ORECV") // should see inside OAS only |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1323 | |
Josh Bleecher Snyder | f12bd8a | 2016-04-21 11:55:33 -0700 | [diff] [blame] | 1324 | case OSLICE, OSLICEARR, OSLICESTR, OSLICE3, OSLICE3ARR: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1325 | n.Left = walkexpr(n.Left, init) |
Josh Bleecher Snyder | f12bd8a | 2016-04-21 11:55:33 -0700 | [diff] [blame] | 1326 | low, high, max := n.SliceBounds() |
| 1327 | low = walkexpr(low, init) |
| 1328 | if low != nil && iszero(low) { |
| 1329 | // Reduce x[0:j] to x[:j] and x[0:j:k] to x[:j:k]. |
| 1330 | low = nil |
Russ Cox | d447279 | 2015-05-06 12:35:53 -0400 | [diff] [blame] | 1331 | } |
Josh Bleecher Snyder | f12bd8a | 2016-04-21 11:55:33 -0700 | [diff] [blame] | 1332 | high = walkexpr(high, init) |
| 1333 | max = walkexpr(max, init) |
| 1334 | n.SetSliceBounds(low, high, max) |
| 1335 | if n.Op.IsSlice3() { |
| 1336 | if max != nil && max.Op == OCAP && samesafeexpr(n.Left, max.Left) { |
| 1337 | // Reduce x[i:j:cap(x)] to x[i:j]. |
| 1338 | if n.Op == OSLICE3 { |
| 1339 | n.Op = OSLICE |
| 1340 | } else { |
| 1341 | n.Op = OSLICEARR |
| 1342 | } |
| 1343 | n = reduceSlice(n) |
Russ Cox | d447279 | 2015-05-06 12:35:53 -0400 | [diff] [blame] | 1344 | } |
Josh Bleecher Snyder | f12bd8a | 2016-04-21 11:55:33 -0700 | [diff] [blame] | 1345 | } else { |
Russ Cox | d447279 | 2015-05-06 12:35:53 -0400 | [diff] [blame] | 1346 | n = reduceSlice(n) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1347 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1348 | |
| 1349 | case OADDR: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1350 | n.Left = walkexpr(n.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1351 | |
| 1352 | case ONEW: |
David Chase | e5060c7 | 2015-05-20 15:16:34 -0400 | [diff] [blame] | 1353 | if n.Esc == EscNone { |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 1354 | if n.Type.Elem().Width >= 1<<16 { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 1355 | Fatalf("large ONEW with EscNone: %v", n) |
David Chase | e5060c7 | 2015-05-20 15:16:34 -0400 | [diff] [blame] | 1356 | } |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 1357 | r := temp(n.Type.Elem()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1358 | r = Nod(OAS, r, nil) // zero temp |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1359 | r = typecheck(r, Etop) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 1360 | init.Append(r) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1361 | r = Nod(OADDR, r.Left, nil) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1362 | r = typecheck(r, Erv) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1363 | n = r |
| 1364 | } else { |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 1365 | n = callnew(n.Type.Elem()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1366 | } |
| 1367 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1368 | // If one argument to the comparison is an empty string, |
| 1369 | // comparing the lengths instead will yield the same result |
| 1370 | // without the function call. |
| 1371 | case OCMPSTR: |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 1372 | if (Isconst(n.Left, CTSTR) && len(n.Left.Val().U.(string)) == 0) || (Isconst(n.Right, CTSTR) && len(n.Right.Val().U.(string)) == 0) { |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 1373 | // TODO(marvin): Fix Node.EType type union. |
| 1374 | r := Nod(Op(n.Etype), Nod(OLEN, n.Left, nil), Nod(OLEN, n.Right, nil)) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1375 | r = typecheck(r, Erv) |
| 1376 | r = walkexpr(r, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1377 | r.Type = n.Type |
| 1378 | n = r |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 1379 | break |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | // s + "badgerbadgerbadger" == "badgerbadgerbadger" |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 1383 | if (Op(n.Etype) == OEQ || Op(n.Etype) == ONE) && Isconst(n.Right, CTSTR) && n.Left.Op == OADDSTR && n.Left.List.Len() == 2 && Isconst(n.Left.List.Second(), CTSTR) && strlit(n.Right) == strlit(n.Left.List.Second()) { |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 1384 | // TODO(marvin): Fix Node.EType type union. |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 1385 | r := Nod(Op(n.Etype), Nod(OLEN, n.Left.List.First(), nil), Nodintconst(0)) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1386 | r = typecheck(r, Erv) |
| 1387 | r = walkexpr(r, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1388 | r.Type = n.Type |
| 1389 | n = r |
HĂ¥vard Haugen | 9238cbd | 2015-09-20 23:28:34 +0200 | [diff] [blame] | 1390 | break |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1391 | } |
| 1392 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1393 | var r *Node |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 1394 | // TODO(marvin): Fix Node.EType type union. |
| 1395 | if Op(n.Etype) == OEQ || Op(n.Etype) == ONE { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1396 | // prepare for rewrite below |
| 1397 | n.Left = cheapexpr(n.Left, init) |
| 1398 | |
| 1399 | n.Right = cheapexpr(n.Right, init) |
| 1400 | |
| 1401 | r = mkcall("eqstring", Types[TBOOL], init, conv(n.Left, Types[TSTRING]), conv(n.Right, Types[TSTRING])) |
| 1402 | |
| 1403 | // quick check of len before full compare for == or != |
| 1404 | // eqstring assumes that the lengths are equal |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 1405 | // TODO(marvin): Fix Node.EType type union. |
| 1406 | if Op(n.Etype) == OEQ { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1407 | // len(left) == len(right) && eqstring(left, right) |
| 1408 | r = Nod(OANDAND, Nod(OEQ, Nod(OLEN, n.Left, nil), Nod(OLEN, n.Right, nil)), r) |
| 1409 | } else { |
| 1410 | // len(left) != len(right) || !eqstring(left, right) |
| 1411 | r = Nod(ONOT, r, nil) |
| 1412 | |
| 1413 | r = Nod(OOROR, Nod(ONE, Nod(OLEN, n.Left, nil), Nod(OLEN, n.Right, nil)), r) |
| 1414 | } |
| 1415 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1416 | r = typecheck(r, Erv) |
| 1417 | r = walkexpr(r, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1418 | } else { |
| 1419 | // sys_cmpstring(s1, s2) :: 0 |
| 1420 | r = mkcall("cmpstring", Types[TINT], init, conv(n.Left, Types[TSTRING]), conv(n.Right, Types[TSTRING])) |
| 1421 | |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 1422 | // TODO(marvin): Fix Node.EType type union. |
| 1423 | r = Nod(Op(n.Etype), r, Nodintconst(0)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1424 | } |
| 1425 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1426 | r = typecheck(r, Erv) |
Matthew Dempsky | 3efefd9 | 2016-03-30 14:56:08 -0700 | [diff] [blame] | 1427 | if !n.Type.IsBoolean() { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 1428 | Fatalf("cmp %v", n.Type) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1429 | } |
| 1430 | r.Type = n.Type |
| 1431 | n = r |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1432 | |
| 1433 | case OADDSTR: |
| 1434 | n = addstr(n, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1435 | |
| 1436 | case OAPPEND: |
Russ Cox | 8552047 | 2015-05-06 12:34:30 -0400 | [diff] [blame] | 1437 | // order should make sure we only see OAS(node, OAPPEND), which we handle above. |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 1438 | Fatalf("append outside assignment") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1439 | |
| 1440 | case OCOPY: |
Ian Lance Taylor | 9e902f0 | 2015-10-20 10:00:07 -0700 | [diff] [blame] | 1441 | n = copyany(n, init, instrumenting) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1442 | |
| 1443 | // cannot use chanfn - closechan takes any, not chan any |
| 1444 | case OCLOSE: |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 1445 | fn := syslook("closechan") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1446 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1447 | fn = substArgTypes(fn, n.Left.Type) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1448 | n = mkcall1(fn, nil, init, n.Left) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1449 | |
| 1450 | case OMAKECHAN: |
| 1451 | n = mkcall1(chanfn("makechan", 1, n.Type), n.Type, init, typename(n.Type), conv(n.Left, Types[TINT64])) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1452 | |
| 1453 | case OMAKEMAP: |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1454 | t := n.Type |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1455 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1456 | a := nodnil() // hmap buffer |
| 1457 | r := nodnil() // bucket buffer |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1458 | if n.Esc == EscNone { |
| 1459 | // Allocate hmap buffer on stack. |
Matthew Dempsky | 9877900 | 2016-02-23 07:46:01 +0000 | [diff] [blame] | 1460 | var_ := temp(hmap(t)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1461 | |
| 1462 | a = Nod(OAS, var_, nil) // zero temp |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1463 | a = typecheck(a, Etop) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 1464 | init.Append(a) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1465 | a = Nod(OADDR, var_, nil) |
| 1466 | |
| 1467 | // Allocate one bucket on stack. |
| 1468 | // Maximum key/value size is 128 bytes, larger objects |
| 1469 | // are stored with an indirection. So max bucket size is 2048+eps. |
| 1470 | var_ = temp(mapbucket(t)) |
| 1471 | |
| 1472 | r = Nod(OAS, var_, nil) // zero temp |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1473 | r = typecheck(r, Etop) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 1474 | init.Append(r) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1475 | r = Nod(OADDR, var_, nil) |
| 1476 | } |
| 1477 | |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 1478 | fn := syslook("makemap") |
Josh Bleecher Snyder | 093a9a1 | 2016-03-28 21:48:47 -0700 | [diff] [blame] | 1479 | fn = substArgTypes(fn, hmap(t), mapbucket(t), t.Key(), t.Val()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1480 | n = mkcall1(fn, n.Type, init, typename(n.Type), conv(n.Left, Types[TINT64]), a, r) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1481 | |
| 1482 | case OMAKESLICE: |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1483 | l := n.Left |
| 1484 | r := n.Right |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1485 | if r == nil { |
| 1486 | r = safeexpr(l, init) |
| 1487 | l = r |
| 1488 | } |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1489 | t := n.Type |
David Chase | e5060c7 | 2015-05-20 15:16:34 -0400 | [diff] [blame] | 1490 | if n.Esc == EscNone { |
| 1491 | if !isSmallMakeSlice(n) { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 1492 | Fatalf("non-small OMAKESLICE with EscNone: %v", n) |
David Chase | e5060c7 | 2015-05-20 15:16:34 -0400 | [diff] [blame] | 1493 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1494 | // var arr [r]T |
| 1495 | // n = arr[:l] |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 1496 | t = aindex(r, t.Elem()) // [r]T |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1497 | var_ := temp(t) |
| 1498 | a := Nod(OAS, var_, nil) // zero temp |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1499 | a = typecheck(a, Etop) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 1500 | init.Append(a) |
Josh Bleecher Snyder | f12bd8a | 2016-04-21 11:55:33 -0700 | [diff] [blame] | 1501 | r := Nod(OSLICE, var_, nil) // arr[:l] |
| 1502 | r.SetSliceBounds(nil, l, nil) |
| 1503 | r = conv(r, n.Type) // in case n.Type is named. |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1504 | r = typecheck(r, Erv) |
| 1505 | r = walkexpr(r, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1506 | n = r |
| 1507 | } else { |
Keith Randall | bfe0cbd | 2016-04-19 15:38:59 -0700 | [diff] [blame] | 1508 | // makeslice(et *Type, nel int64, max int64) (ary []any) |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 1509 | fn := syslook("makeslice") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1510 | |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 1511 | fn = substArgTypes(fn, t.Elem()) // any-1 |
Keith Randall | bfe0cbd | 2016-04-19 15:38:59 -0700 | [diff] [blame] | 1512 | n = mkcall1(fn, n.Type, init, typename(t.Elem()), conv(l, Types[TINT64]), conv(r, Types[TINT64])) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1513 | } |
| 1514 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1515 | case ORUNESTR: |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1516 | a := nodnil() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1517 | if n.Esc == EscNone { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1518 | t := aindex(Nodintconst(4), Types[TUINT8]) |
| 1519 | var_ := temp(t) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1520 | a = Nod(OADDR, var_, nil) |
| 1521 | } |
| 1522 | |
| 1523 | // intstring(*[4]byte, rune) |
| 1524 | n = mkcall("intstring", n.Type, init, a, conv(n.Left, Types[TINT64])) |
| 1525 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1526 | case OARRAYBYTESTR: |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1527 | a := nodnil() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1528 | if n.Esc == EscNone { |
| 1529 | // Create temporary buffer for string on stack. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1530 | t := aindex(Nodintconst(tmpstringbufsize), Types[TUINT8]) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1531 | |
| 1532 | a = Nod(OADDR, temp(t), nil) |
| 1533 | } |
| 1534 | |
| 1535 | // slicebytetostring(*[32]byte, []byte) string; |
| 1536 | n = mkcall("slicebytetostring", n.Type, init, a, n.Left) |
| 1537 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1538 | // slicebytetostringtmp([]byte) string; |
| 1539 | case OARRAYBYTESTRTMP: |
| 1540 | n = mkcall("slicebytetostringtmp", n.Type, init, n.Left) |
| 1541 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1542 | // slicerunetostring(*[32]byte, []rune) string; |
| 1543 | case OARRAYRUNESTR: |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1544 | a := nodnil() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1545 | |
| 1546 | if n.Esc == EscNone { |
| 1547 | // Create temporary buffer for string on stack. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1548 | t := aindex(Nodintconst(tmpstringbufsize), Types[TUINT8]) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1549 | |
| 1550 | a = Nod(OADDR, temp(t), nil) |
| 1551 | } |
| 1552 | |
| 1553 | n = mkcall("slicerunetostring", n.Type, init, a, n.Left) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1554 | |
| 1555 | // stringtoslicebyte(*32[byte], string) []byte; |
| 1556 | case OSTRARRAYBYTE: |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1557 | a := nodnil() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1558 | |
| 1559 | if n.Esc == EscNone { |
| 1560 | // Create temporary buffer for slice on stack. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1561 | t := aindex(Nodintconst(tmpstringbufsize), Types[TUINT8]) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1562 | |
| 1563 | a = Nod(OADDR, temp(t), nil) |
| 1564 | } |
| 1565 | |
| 1566 | n = mkcall("stringtoslicebyte", n.Type, init, a, conv(n.Left, Types[TSTRING])) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1567 | |
| 1568 | // stringtoslicebytetmp(string) []byte; |
| 1569 | case OSTRARRAYBYTETMP: |
| 1570 | n = mkcall("stringtoslicebytetmp", n.Type, init, conv(n.Left, Types[TSTRING])) |
| 1571 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1572 | // stringtoslicerune(*[32]rune, string) []rune |
| 1573 | case OSTRARRAYRUNE: |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1574 | a := nodnil() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1575 | |
| 1576 | if n.Esc == EscNone { |
| 1577 | // Create temporary buffer for slice on stack. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1578 | t := aindex(Nodintconst(tmpstringbufsize), Types[TINT32]) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1579 | |
| 1580 | a = Nod(OADDR, temp(t), nil) |
| 1581 | } |
| 1582 | |
| 1583 | n = mkcall("stringtoslicerune", n.Type, init, a, n.Left) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1584 | |
| 1585 | // ifaceeq(i1 any-1, i2 any-2) (ret bool); |
| 1586 | case OCMPIFACE: |
| 1587 | if !Eqtype(n.Left.Type, n.Right.Type) { |
Dave Cheney | d3c79d3 | 2016-04-27 15:10:10 +1000 | [diff] [blame] | 1588 | Fatalf("ifaceeq %v %v %v", n.Op, n.Left.Type, n.Right.Type) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1589 | } |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1590 | var fn *Node |
Matthew Dempsky | 00e5a68 | 2016-04-01 13:36:24 -0700 | [diff] [blame] | 1591 | if n.Left.Type.IsEmptyInterface() { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 1592 | fn = syslook("efaceeq") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1593 | } else { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 1594 | fn = syslook("ifaceeq") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | n.Right = cheapexpr(n.Right, init) |
| 1598 | n.Left = cheapexpr(n.Left, init) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1599 | fn = substArgTypes(fn, n.Right.Type, n.Left.Type) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1600 | r := mkcall1(fn, n.Type, init, n.Left, n.Right) |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 1601 | // TODO(marvin): Fix Node.EType type union. |
| 1602 | if Op(n.Etype) == ONE { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1603 | r = Nod(ONOT, r, nil) |
| 1604 | } |
| 1605 | |
| 1606 | // check itable/type before full compare. |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 1607 | // TODO(marvin): Fix Node.EType type union. |
| 1608 | if Op(n.Etype) == OEQ { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1609 | r = Nod(OANDAND, Nod(OEQ, Nod(OITAB, n.Left, nil), Nod(OITAB, n.Right, nil)), r) |
| 1610 | } else { |
| 1611 | r = Nod(OOROR, Nod(ONE, Nod(OITAB, n.Left, nil), Nod(OITAB, n.Right, nil)), r) |
| 1612 | } |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1613 | r = typecheck(r, Erv) |
| 1614 | r = walkexpr(r, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1615 | r.Type = n.Type |
| 1616 | n = r |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1617 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 1618 | case OARRAYLIT, OMAPLIT, OSTRUCTLIT, OPTRLIT: |
Josh Bleecher Snyder | 3c6e60c | 2016-04-19 12:08:33 -0700 | [diff] [blame] | 1619 | if isStaticCompositeLiteral(n) { |
Keith Randall | b024ed0 | 2016-04-18 11:17:55 -0700 | [diff] [blame] | 1620 | // n can be directly represented in the read-only data section. |
| 1621 | // Make direct reference to the static data. See issue 12841. |
| 1622 | vstat := staticname(n.Type, 0) |
| 1623 | if n.Op == OSTRUCTLIT { |
| 1624 | structlit(0, 1, n, vstat, init) |
| 1625 | } else { |
| 1626 | arraylit(0, 1, n, vstat, init) |
| 1627 | } |
| 1628 | n = vstat |
| 1629 | n = typecheck(n, Erv) |
| 1630 | break |
| 1631 | } |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1632 | var_ := temp(n.Type) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1633 | anylit(0, n, var_, init) |
| 1634 | n = var_ |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1635 | |
| 1636 | case OSEND: |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1637 | n1 := n.Right |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 1638 | n1 = assignconv(n1, n.Left.Type.Elem(), "chan send") |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1639 | n1 = walkexpr(n1, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1640 | n1 = Nod(OADDR, n1, nil) |
| 1641 | n = mkcall1(chanfn("chansend1", 2, n.Left.Type), nil, init, typename(n.Left.Type), n.Left, n1) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1642 | |
| 1643 | case OCLOSURE: |
| 1644 | n = walkclosure(n, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1645 | |
| 1646 | case OCALLPART: |
| 1647 | n = walkpartialcall(n, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1648 | } |
| 1649 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1650 | // Expressions that are constant at run time but not |
| 1651 | // considered const by the language spec are not turned into |
| 1652 | // constants until walk. For example, if n is y%1 == 0, the |
| 1653 | // walk of y%1 may have replaced it by 0. |
| 1654 | // Check whether n with its updated args is itself now a constant. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1655 | t := n.Type |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1656 | |
| 1657 | evconst(n) |
| 1658 | n.Type = t |
| 1659 | if n.Op == OLITERAL { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1660 | n = typecheck(n, Erv) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | ullmancalc(n) |
| 1664 | |
| 1665 | if Debug['w'] != 0 && n != nil { |
| 1666 | Dump("walk", n) |
| 1667 | } |
| 1668 | |
| 1669 | lineno = lno |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1670 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1671 | } |
| 1672 | |
Josh Bleecher Snyder | f12bd8a | 2016-04-21 11:55:33 -0700 | [diff] [blame] | 1673 | // TODO(josharian): combine this with its caller and simplify |
Russ Cox | d447279 | 2015-05-06 12:35:53 -0400 | [diff] [blame] | 1674 | func reduceSlice(n *Node) *Node { |
Josh Bleecher Snyder | f12bd8a | 2016-04-21 11:55:33 -0700 | [diff] [blame] | 1675 | low, high, max := n.SliceBounds() |
| 1676 | if high != nil && high.Op == OLEN && samesafeexpr(n.Left, high.Left) { |
Russ Cox | d447279 | 2015-05-06 12:35:53 -0400 | [diff] [blame] | 1677 | // Reduce x[i:len(x)] to x[i:]. |
Josh Bleecher Snyder | f12bd8a | 2016-04-21 11:55:33 -0700 | [diff] [blame] | 1678 | high = nil |
Russ Cox | d447279 | 2015-05-06 12:35:53 -0400 | [diff] [blame] | 1679 | } |
Josh Bleecher Snyder | f12bd8a | 2016-04-21 11:55:33 -0700 | [diff] [blame] | 1680 | n.SetSliceBounds(low, high, max) |
| 1681 | if (n.Op == OSLICE || n.Op == OSLICESTR) && low == nil && high == nil { |
Russ Cox | d447279 | 2015-05-06 12:35:53 -0400 | [diff] [blame] | 1682 | // Reduce x[:] to x. |
| 1683 | if Debug_slice > 0 { |
| 1684 | Warn("slice: omit slice operation") |
| 1685 | } |
| 1686 | return n.Left |
| 1687 | } |
| 1688 | return n |
| 1689 | } |
| 1690 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 1691 | func ascompatee1(op Op, l *Node, r *Node, init *Nodes) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1692 | // convas will turn map assigns into function calls, |
| 1693 | // making it impossible for reorder3 to work. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1694 | n := Nod(OAS, l, r) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1695 | |
| 1696 | if l.Op == OINDEXMAP { |
| 1697 | return n |
| 1698 | } |
| 1699 | |
| 1700 | return convas(n, init) |
| 1701 | } |
| 1702 | |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 1703 | func ascompatee(op Op, nl, nr []*Node, init *Nodes) []*Node { |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 1704 | // check assign expression list to |
| 1705 | // a expression list. called in |
| 1706 | // expr-list = expr-list |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1707 | |
| 1708 | // ensure order of evaluation for function calls |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 1709 | for i := range nl { |
| 1710 | nl[i] = safeexpr(nl[i], init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1711 | } |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 1712 | for i1 := range nr { |
| 1713 | nr[i1] = safeexpr(nr[i1], init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1714 | } |
| 1715 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 1716 | var nn []*Node |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 1717 | i := 0 |
| 1718 | for ; i < len(nl); i++ { |
| 1719 | if i >= len(nr) { |
| 1720 | break |
| 1721 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1722 | // Do not generate 'x = x' during return. See issue 4014. |
Russ Cox | b6dc3e6 | 2016-05-25 01:33:24 -0400 | [diff] [blame] | 1723 | if op == ORETURN && samesafeexpr(nl[i], nr[i]) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1724 | continue |
| 1725 | } |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 1726 | nn = append(nn, ascompatee1(op, nl[i], nr[i], init)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | // cannot happen: caller checked that lists had same length |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 1730 | if i < len(nl) || i < len(nr) { |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 1731 | var nln, nrn Nodes |
| 1732 | nln.Set(nl) |
| 1733 | nrn.Set(nr) |
Dave Cheney | 2da642a | 2016-04-27 15:15:47 +1000 | [diff] [blame] | 1734 | Yyerror("error in shape across %v %v %v / %d %d [%s]", hconv(nln, FmtSign), op, hconv(nrn, FmtSign), len(nl), len(nr), Curfn.Func.Nname.Sym.Name) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1735 | } |
| 1736 | return nn |
| 1737 | } |
| 1738 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 1739 | // l is an lv and rt is the type of an rv |
| 1740 | // return 1 if this implies a function call |
| 1741 | // evaluating the lv or a function call |
| 1742 | // in the conversion of the types |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 1743 | func fncall(l *Node, rt *Type) bool { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1744 | if l.Ullman >= UINF || l.Op == OINDEXMAP { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 1745 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1746 | } |
Russ Cox | 175929b | 2015-03-02 14:22:05 -0500 | [diff] [blame] | 1747 | var r Node |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 1748 | if needwritebarrier(l, &r) { |
| 1749 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1750 | } |
| 1751 | if Eqtype(l.Type, rt) { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 1752 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1753 | } |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 1754 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1755 | } |
| 1756 | |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1757 | // check assign type list to |
| 1758 | // a expression list. called in |
| 1759 | // expr-list = func() |
| 1760 | func ascompatet(op Op, nl Nodes, nr *Type, fp int, init *Nodes) []*Node { |
| 1761 | r, saver := IterFields(nr) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1762 | |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1763 | var nn, mm []*Node |
| 1764 | var ullmanOverflow bool |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 1765 | var i int |
| 1766 | for i = 0; i < nl.Len(); i++ { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1767 | if r == nil { |
| 1768 | break |
| 1769 | } |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1770 | l := nl.Index(i) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1771 | if isblank(l) { |
Matthew Dempsky | 7758a94 | 2016-03-08 15:02:40 -0800 | [diff] [blame] | 1772 | r = saver.Next() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1773 | continue |
| 1774 | } |
| 1775 | |
| 1776 | // any lv that causes a fn call must be |
| 1777 | // deferred until all the return arguments |
| 1778 | // have been pulled from the output arguments |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 1779 | if fncall(l, r.Type) { |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1780 | tmp := temp(r.Type) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1781 | tmp = typecheck(tmp, Erv) |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1782 | a := Nod(OAS, l, tmp) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1783 | a = convas(a, init) |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 1784 | mm = append(mm, a) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1785 | l = tmp |
| 1786 | } |
| 1787 | |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1788 | a := Nod(OAS, l, nodarg(r, fp)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1789 | a = convas(a, init) |
| 1790 | ullmancalc(a) |
| 1791 | if a.Ullman >= UINF { |
| 1792 | Dump("ascompatet ucount", a) |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1793 | ullmanOverflow = true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1794 | } |
| 1795 | |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 1796 | nn = append(nn, a) |
Matthew Dempsky | 7758a94 | 2016-03-08 15:02:40 -0800 | [diff] [blame] | 1797 | r = saver.Next() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1798 | } |
| 1799 | |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 1800 | if i < nl.Len() || r != nil { |
Matthew Dempsky | dbed1c6 | 2016-03-17 13:26:08 -0700 | [diff] [blame] | 1801 | Yyerror("ascompatet: assignment count mismatch: %d = %d", nl.Len(), nr.NumFields()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1802 | } |
| 1803 | |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1804 | if ullmanOverflow { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 1805 | Fatalf("ascompatet: too many function calls evaluating parameters") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1806 | } |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 1807 | return append(nn, mm...) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1808 | } |
| 1809 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 1810 | // package all the arguments that match a ... T parameter into a []T. |
Matthew Dempsky | 2e93690 | 2016-03-14 01:20:49 -0700 | [diff] [blame] | 1811 | func mkdotargslice(lr0, nn []*Node, l *Field, fp int, init *Nodes, ddd *Node) []*Node { |
David Chase | 7fbb1b3 | 2015-03-26 16:36:15 -0400 | [diff] [blame] | 1812 | esc := uint16(EscUnknown) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1813 | if ddd != nil { |
Dave Cheney | e498181 | 2015-03-10 09:58:01 +1100 | [diff] [blame] | 1814 | esc = ddd.Esc |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1815 | } |
| 1816 | |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 1817 | tslice := typSlice(l.Type.Elem()) |
Josh Bleecher Snyder | b6b144b | 2015-05-04 15:01:29 -0700 | [diff] [blame] | 1818 | tslice.Noalg = true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1819 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1820 | var n *Node |
Ian Lance Taylor | f444b8a | 2016-03-09 20:29:21 -0800 | [diff] [blame] | 1821 | if len(lr0) == 0 { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1822 | n = nodnil() |
| 1823 | n.Type = tslice |
| 1824 | } else { |
| 1825 | n = Nod(OCOMPLIT, nil, typenod(tslice)) |
Russ Cox | 60e5f5b | 2015-05-26 23:05:35 -0400 | [diff] [blame] | 1826 | if ddd != nil && prealloc[ddd] != nil { |
| 1827 | prealloc[n] = prealloc[ddd] // temporary to use |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1828 | } |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 1829 | n.List.Set(lr0) |
Dave Cheney | e498181 | 2015-03-10 09:58:01 +1100 | [diff] [blame] | 1830 | n.Esc = esc |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1831 | n = typecheck(n, Erv) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1832 | if n.Type == nil { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 1833 | Fatalf("mkdotargslice: typecheck failed") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1834 | } |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1835 | n = walkexpr(n, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1836 | } |
| 1837 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1838 | a := Nod(OAS, nodarg(l, fp), n) |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 1839 | nn = append(nn, convas(a, init)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1840 | return nn |
| 1841 | } |
| 1842 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 1843 | // helpers for shape errors |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1844 | func dumptypes(nl *Type, what string) string { |
| 1845 | s := "" |
Matthew Dempsky | f6bca3f | 2016-03-17 01:32:18 -0700 | [diff] [blame] | 1846 | for _, l := range nl.Fields().Slice() { |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1847 | if s != "" { |
| 1848 | s += ", " |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1849 | } |
Matthew Dempsky | 2e93690 | 2016-03-14 01:20:49 -0700 | [diff] [blame] | 1850 | s += Fldconv(l, 0) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1851 | } |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1852 | if s == "" { |
| 1853 | s = fmt.Sprintf("[no arguments %s]", what) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1854 | } |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1855 | return s |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1856 | } |
| 1857 | |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 1858 | func dumpnodetypes(l []*Node, what string) string { |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1859 | s := "" |
| 1860 | for _, r := range l { |
| 1861 | if s != "" { |
| 1862 | s += ", " |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1863 | } |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1864 | s += Tconv(r.Type, 0) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1865 | } |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1866 | if s == "" { |
| 1867 | s = fmt.Sprintf("[no arguments %s]", what) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1868 | } |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1869 | return s |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1870 | } |
| 1871 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 1872 | // check assign expression list to |
| 1873 | // a type list. called in |
| 1874 | // return expr-list |
| 1875 | // func(expr-list) |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1876 | func ascompatte(op Op, call *Node, isddd bool, nl *Type, lr []*Node, fp int, init *Nodes) []*Node { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1877 | lr0 := lr |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1878 | l, savel := IterFields(nl) |
Russ Cox | 175929b | 2015-03-02 14:22:05 -0500 | [diff] [blame] | 1879 | var r *Node |
Ian Lance Taylor | f444b8a | 2016-03-09 20:29:21 -0800 | [diff] [blame] | 1880 | if len(lr) > 0 { |
| 1881 | r = lr[0] |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1882 | } |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 1883 | var nn []*Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1884 | |
| 1885 | // f(g()) where g has multiple return values |
Josh Bleecher Snyder | fda831e | 2016-04-05 16:44:07 -0700 | [diff] [blame] | 1886 | if r != nil && len(lr) <= 1 && r.Type.IsFuncArgStruct() { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1887 | // optimization - can do block copy |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1888 | if eqtypenoname(r.Type, nl) { |
| 1889 | arg := nodarg(nl, fp) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1890 | r = Nod(OCONVNOP, r, nil) |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1891 | r.Type = arg.Type |
| 1892 | nn = []*Node{convas(Nod(OAS, arg, r), init)} |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1893 | goto ret |
| 1894 | } |
| 1895 | |
| 1896 | // conversions involved. |
| 1897 | // copy into temporaries. |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 1898 | var alist []*Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1899 | |
Matthew Dempsky | f6bca3f | 2016-03-17 01:32:18 -0700 | [diff] [blame] | 1900 | for _, l := range r.Type.Fields().Slice() { |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1901 | tmp := temp(l.Type) |
| 1902 | alist = append(alist, tmp) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1903 | } |
| 1904 | |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1905 | a := Nod(OAS2, nil, nil) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 1906 | a.List.Set(alist) |
| 1907 | a.Rlist.Set(lr) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 1908 | a = typecheck(a, Etop) |
| 1909 | a = walkstmt(a) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 1910 | init.Append(a) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1911 | lr = alist |
Ian Lance Taylor | f444b8a | 2016-03-09 20:29:21 -0800 | [diff] [blame] | 1912 | r = lr[0] |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1913 | l, savel = IterFields(nl) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1914 | } |
| 1915 | |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1916 | for { |
| 1917 | if l != nil && l.Isddd { |
| 1918 | // the ddd parameter must be last |
| 1919 | ll := savel.Next() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1920 | |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1921 | if ll != nil { |
| 1922 | Yyerror("... must be last argument") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1923 | } |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1924 | |
| 1925 | // special case -- |
| 1926 | // only if we are assigning a single ddd |
| 1927 | // argument to a ddd parameter then it is |
Eric Engestrom | 7a8caf7 | 2016-04-03 12:43:27 +0100 | [diff] [blame] | 1928 | // passed through unencapsulated |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1929 | if r != nil && len(lr) <= 1 && isddd && Eqtype(l.Type, r.Type) { |
| 1930 | a := Nod(OAS, nodarg(l, fp), r) |
| 1931 | a = convas(a, init) |
| 1932 | nn = append(nn, a) |
| 1933 | break |
| 1934 | } |
| 1935 | |
| 1936 | // normal case -- make a slice of all |
| 1937 | // remaining arguments and pass it to |
| 1938 | // the ddd parameter. |
| 1939 | nn = mkdotargslice(lr, nn, l, fp, init, call.Right) |
| 1940 | |
| 1941 | break |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1942 | } |
| 1943 | |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1944 | if l == nil || r == nil { |
| 1945 | if l != nil || r != nil { |
| 1946 | l1 := dumptypes(nl, "expected") |
| 1947 | l2 := dumpnodetypes(lr0, "given") |
| 1948 | if l != nil { |
Dave Cheney | d3c79d3 | 2016-04-27 15:10:10 +1000 | [diff] [blame] | 1949 | Yyerror("not enough arguments to %v\n\t%s\n\t%s", op, l1, l2) |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1950 | } else { |
Dave Cheney | d3c79d3 | 2016-04-27 15:10:10 +1000 | [diff] [blame] | 1951 | Yyerror("too many arguments to %v\n\t%s\n\t%s", op, l1, l2) |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1952 | } |
| 1953 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1954 | |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1955 | break |
| 1956 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1957 | |
Matthew Dempsky | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1958 | a := Nod(OAS, nodarg(l, fp), r) |
| 1959 | a = convas(a, init) |
| 1960 | nn = append(nn, a) |
| 1961 | |
| 1962 | l = savel.Next() |
| 1963 | r = nil |
| 1964 | lr = lr[1:] |
| 1965 | if len(lr) > 0 { |
| 1966 | r = lr[0] |
| 1967 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1968 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1969 | |
| 1970 | ret: |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 1971 | for _, n := range nn { |
| 1972 | n.Typecheck = 1 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1973 | } |
| 1974 | return nn |
| 1975 | } |
| 1976 | |
| 1977 | // generate code for print |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 1978 | func walkprint(nn *Node, init *Nodes) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1979 | var r *Node |
| 1980 | var n *Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1981 | var on *Node |
| 1982 | var t *Type |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 1983 | var et EType |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1984 | |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 1985 | op := nn.Op |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1986 | all := nn.List |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 1987 | var calls []*Node |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 1988 | notfirst := false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1989 | |
| 1990 | // Hoist all the argument evaluation up before the lock. |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 1991 | walkexprlistcheap(all.Slice(), init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1992 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 1993 | calls = append(calls, mkcall("printlock", nil, init)) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 1994 | for i1, n1 := range all.Slice() { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 1995 | if notfirst { |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 1996 | calls = append(calls, mkcall("printsp", nil, init)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 1997 | } |
| 1998 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 1999 | notfirst = op == OPRINTN |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2000 | |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2001 | n = n1 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2002 | if n.Op == OLITERAL { |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 2003 | switch n.Val().Ctype() { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2004 | case CTRUNE: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2005 | n = defaultlit(n, runetype) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2006 | |
| 2007 | case CTINT: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2008 | n = defaultlit(n, Types[TINT64]) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2009 | |
| 2010 | case CTFLT: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2011 | n = defaultlit(n, Types[TFLOAT64]) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2012 | } |
| 2013 | } |
| 2014 | |
| 2015 | if n.Op != OLITERAL && n.Type != nil && n.Type.Etype == TIDEAL { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2016 | n = defaultlit(n, Types[TINT64]) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2017 | } |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2018 | n = defaultlit(n, nil) |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 2019 | all.SetIndex(i1, n) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2020 | if n.Type == nil || n.Type.Etype == TFORW { |
| 2021 | continue |
| 2022 | } |
| 2023 | |
| 2024 | t = n.Type |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 2025 | et = n.Type.Etype |
Matthew Dempsky | 1624a9c | 2016-03-30 14:45:47 -0700 | [diff] [blame] | 2026 | if n.Type.IsInterface() { |
Matthew Dempsky | 00e5a68 | 2016-04-01 13:36:24 -0700 | [diff] [blame] | 2027 | if n.Type.IsEmptyInterface() { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2028 | on = syslook("printeface") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2029 | } else { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2030 | on = syslook("printiface") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2031 | } |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2032 | on = substArgTypes(on, n.Type) // any-1 |
Matthew Dempsky | e76fc1b | 2016-03-30 15:09:25 -0700 | [diff] [blame] | 2033 | } else if n.Type.IsPtr() || et == TCHAN || et == TMAP || et == TFUNC || et == TUNSAFEPTR { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2034 | on = syslook("printpointer") |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2035 | on = substArgTypes(on, n.Type) // any-1 |
Matthew Dempsky | 1624a9c | 2016-03-30 14:45:47 -0700 | [diff] [blame] | 2036 | } else if n.Type.IsSlice() { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2037 | on = syslook("printslice") |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2038 | on = substArgTypes(on, n.Type) // any-1 |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 2039 | } else if Isint[et] { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2040 | if et == TUINT64 { |
Matthew Dempsky | 980ab12 | 2016-04-13 18:37:18 -0700 | [diff] [blame] | 2041 | if (t.Sym.Pkg == Runtimepkg || compiling_runtime) && t.Sym.Name == "hex" { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2042 | on = syslook("printhex") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2043 | } else { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2044 | on = syslook("printuint") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2045 | } |
| 2046 | } else { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2047 | on = syslook("printint") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2048 | } |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 2049 | } else if Isfloat[et] { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2050 | on = syslook("printfloat") |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 2051 | } else if Iscomplex[et] { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2052 | on = syslook("printcomplex") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2053 | } else if et == TBOOL { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2054 | on = syslook("printbool") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2055 | } else if et == TSTRING { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2056 | on = syslook("printstring") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2057 | } else { |
| 2058 | badtype(OPRINT, n.Type, nil) |
| 2059 | continue |
| 2060 | } |
| 2061 | |
Matthew Dempsky | 0d2e92c | 2016-03-13 23:02:38 -0700 | [diff] [blame] | 2062 | t = on.Type.Params().Field(0).Type |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2063 | |
| 2064 | if !Eqtype(t, n.Type) { |
| 2065 | n = Nod(OCONV, n, nil) |
| 2066 | n.Type = t |
| 2067 | } |
| 2068 | |
| 2069 | r = Nod(OCALL, on, nil) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2070 | r.List.Append(n) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2071 | calls = append(calls, r) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | if op == OPRINTN { |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2075 | calls = append(calls, mkcall("printnl", nil, nil)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2076 | } |
| 2077 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2078 | calls = append(calls, mkcall("printunlock", nil, init)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2079 | |
Josh Bleecher Snyder | ec7c494 | 2016-03-19 17:02:01 -0700 | [diff] [blame] | 2080 | typecheckslice(calls, Etop) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2081 | walkexprlist(calls, init) |
| 2082 | |
| 2083 | r = Nod(OEMPTY, nil, nil) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2084 | r = typecheck(r, Etop) |
| 2085 | r = walkexpr(r, init) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2086 | r.Ninit.Set(calls) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2087 | return r |
| 2088 | } |
| 2089 | |
| 2090 | func callnew(t *Type) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2091 | dowidth(t) |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2092 | fn := syslook("newobject") |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2093 | fn = substArgTypes(fn, t) |
Keith Randall | 3c1a4c1 | 2016-04-19 21:06:53 -0700 | [diff] [blame] | 2094 | v := mkcall1(fn, Ptrto(t), nil, typename(t)) |
| 2095 | v.NonNil = true |
| 2096 | return v |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2097 | } |
| 2098 | |
Russ Cox | 3b6e86f | 2015-06-29 15:17:14 -0400 | [diff] [blame] | 2099 | func iscallret(n *Node) bool { |
| 2100 | n = outervalue(n) |
| 2101 | return n.Op == OINDREG && n.Reg == int16(Thearch.REGSP) |
| 2102 | } |
| 2103 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2104 | func isstack(n *Node) bool { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2105 | n = outervalue(n) |
| 2106 | |
| 2107 | // If n is *autotmp and autotmp = &foo, replace n with foo. |
| 2108 | // We introduce such temps when initializing struct literals. |
| 2109 | if n.Op == OIND && n.Left.Op == ONAME && strings.HasPrefix(n.Left.Sym.Name, "autotmp_") { |
Russ Cox | 4fdd536 | 2015-05-26 22:19:27 -0400 | [diff] [blame] | 2110 | defn := n.Left.Name.Defn |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2111 | if defn != nil && defn.Op == OAS && defn.Right.Op == OADDR { |
| 2112 | n = defn.Right.Left |
| 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | switch n.Op { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2117 | case OINDREG: |
Russ Cox | 8552047 | 2015-05-06 12:34:30 -0400 | [diff] [blame] | 2118 | return n.Reg == int16(Thearch.REGSP) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2119 | |
| 2120 | case ONAME: |
| 2121 | switch n.Class { |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 2122 | case PAUTO, PPARAM, PPARAMOUT: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2123 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2124 | } |
| 2125 | } |
| 2126 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2127 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2128 | } |
| 2129 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2130 | func isglobal(n *Node) bool { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2131 | n = outervalue(n) |
| 2132 | |
| 2133 | switch n.Op { |
| 2134 | case ONAME: |
| 2135 | switch n.Class { |
| 2136 | case PEXTERN: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2137 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2138 | } |
| 2139 | } |
| 2140 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2141 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | // Do we need a write barrier for the assignment l = r? |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2145 | func needwritebarrier(l *Node, r *Node) bool { |
Matthew Dempsky | 980ab12 | 2016-04-13 18:37:18 -0700 | [diff] [blame] | 2146 | if !use_writebarrier { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2147 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | if l == nil || isblank(l) { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2151 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | // No write barrier for write of non-pointers. |
| 2155 | dowidth(l.Type) |
| 2156 | |
| 2157 | if !haspointers(l.Type) { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2158 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | // No write barrier for write to stack. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2162 | if isstack(l) { |
| 2163 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2164 | } |
| 2165 | |
Russ Cox | 9f90f31 | 2015-06-29 12:49:25 -0400 | [diff] [blame] | 2166 | // No write barrier for implicit zeroing. |
| 2167 | if r == nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2168 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2169 | } |
| 2170 | |
Russ Cox | 9f90f31 | 2015-06-29 12:49:25 -0400 | [diff] [blame] | 2171 | // Ignore no-op conversions when making decision. |
| 2172 | // Ensures that xp = unsafe.Pointer(&x) is treated |
| 2173 | // the same as xp = &x. |
| 2174 | for r.Op == OCONVNOP { |
| 2175 | r = r.Left |
| 2176 | } |
| 2177 | |
| 2178 | // No write barrier for zeroing or initialization to constant. |
| 2179 | if iszero(r) || r.Op == OLITERAL { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2180 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | // No write barrier for storing static (read-only) data. |
| 2184 | if r.Op == ONAME && strings.HasPrefix(r.Sym.Name, "statictmp_") { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2185 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2186 | } |
| 2187 | |
| 2188 | // No write barrier for storing address of stack values, |
| 2189 | // which are guaranteed only to be written to the stack. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2190 | if r.Op == OADDR && isstack(r.Left) { |
| 2191 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | // No write barrier for storing address of global, which |
| 2195 | // is live no matter what. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2196 | if r.Op == OADDR && isglobal(r.Left) { |
| 2197 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2198 | } |
| 2199 | |
Austin Clements | 3e54ca9 | 2016-03-16 18:22:58 -0400 | [diff] [blame] | 2200 | // No write barrier for storing global function, which is live |
| 2201 | // no matter what. |
| 2202 | if r.Op == ONAME && r.Class == PFUNC { |
| 2203 | return false |
| 2204 | } |
| 2205 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2206 | // Otherwise, be conservative and use write barrier. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2207 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | // TODO(rsc): Perhaps componentgen should run before this. |
| 2211 | |
Ian Lance Taylor | 188e3d2 | 2016-02-26 14:28:48 -0800 | [diff] [blame] | 2212 | func applywritebarrier(n *Node) *Node { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2213 | if n.Left != nil && n.Right != nil && needwritebarrier(n.Left, n.Right) { |
Russ Cox | 972a478 | 2015-05-21 15:00:06 -0400 | [diff] [blame] | 2214 | if Debug_wb > 1 { |
Robert Griesemer | b83f397 | 2016-03-02 11:01:25 -0800 | [diff] [blame] | 2215 | Warnl(n.Lineno, "marking %v for barrier", Nconv(n.Left, 0)) |
Russ Cox | 0ad4f8b | 2015-04-17 00:25:10 -0400 | [diff] [blame] | 2216 | } |
Russ Cox | 972a478 | 2015-05-21 15:00:06 -0400 | [diff] [blame] | 2217 | n.Op = OASWB |
| 2218 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2219 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2220 | return n |
| 2221 | } |
| 2222 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2223 | func convas(n *Node, init *Nodes) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2224 | if n.Op != OAS { |
Dave Cheney | d3c79d3 | 2016-04-27 15:10:10 +1000 | [diff] [blame] | 2225 | Fatalf("convas: not OAS %v", n.Op) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | n.Typecheck = 1 |
| 2229 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2230 | var lt *Type |
| 2231 | var rt *Type |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2232 | if n.Left == nil || n.Right == nil { |
| 2233 | goto out |
| 2234 | } |
| 2235 | |
| 2236 | lt = n.Left.Type |
| 2237 | rt = n.Right.Type |
| 2238 | if lt == nil || rt == nil { |
| 2239 | goto out |
| 2240 | } |
| 2241 | |
| 2242 | if isblank(n.Left) { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2243 | n.Right = defaultlit(n.Right, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2244 | goto out |
| 2245 | } |
| 2246 | |
| 2247 | if n.Left.Op == OINDEXMAP { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2248 | map_ := n.Left.Left |
| 2249 | key := n.Left.Right |
| 2250 | val := n.Right |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2251 | map_ = walkexpr(map_, init) |
| 2252 | key = walkexpr(key, init) |
| 2253 | val = walkexpr(val, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2254 | |
| 2255 | // orderexpr made sure key and val are addressable. |
| 2256 | key = Nod(OADDR, key, nil) |
| 2257 | |
| 2258 | val = Nod(OADDR, val, nil) |
| 2259 | n = mkcall1(mapfn("mapassign1", map_.Type), nil, init, typename(map_.Type), map_, key, val) |
| 2260 | goto out |
| 2261 | } |
| 2262 | |
| 2263 | if !Eqtype(lt, rt) { |
| 2264 | n.Right = assignconv(n.Right, lt, "assignment") |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2265 | n.Right = walkexpr(n.Right, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | out: |
| 2269 | ullmancalc(n) |
| 2270 | return n |
| 2271 | } |
| 2272 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2273 | // from ascompat[te] |
| 2274 | // evaluating actual function arguments. |
| 2275 | // f(a,b) |
| 2276 | // if there is exactly one function expr, |
| 2277 | // then it is done first. otherwise must |
| 2278 | // make temp variables |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2279 | func reorder1(all []*Node) []*Node { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2280 | c := 0 // function calls |
| 2281 | t := 0 // total parameters |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2282 | |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2283 | for _, n := range all { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2284 | t++ |
| 2285 | ullmancalc(n) |
| 2286 | if n.Ullman >= UINF { |
| 2287 | c++ |
| 2288 | } |
| 2289 | } |
| 2290 | |
| 2291 | if c == 0 || t == 1 { |
| 2292 | return all |
| 2293 | } |
| 2294 | |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2295 | var g []*Node // fncalls assigned to tempnames |
| 2296 | var f *Node // last fncall assigned to stack |
| 2297 | var r []*Node // non fncalls and tempnames assigned to stack |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2298 | d := 0 |
| 2299 | var a *Node |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2300 | for _, n := range all { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2301 | if n.Ullman < UINF { |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2302 | r = append(r, n) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2303 | continue |
| 2304 | } |
| 2305 | |
| 2306 | d++ |
| 2307 | if d == c { |
| 2308 | f = n |
| 2309 | continue |
| 2310 | } |
| 2311 | |
| 2312 | // make assignment of fncall to tempname |
| 2313 | a = temp(n.Right.Type) |
| 2314 | |
| 2315 | a = Nod(OAS, a, n.Right) |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2316 | g = append(g, a) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2317 | |
| 2318 | // put normal arg assignment on list |
| 2319 | // with fncall replaced by tempname |
| 2320 | n.Right = a.Left |
| 2321 | |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2322 | r = append(r, n) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2323 | } |
| 2324 | |
| 2325 | if f != nil { |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2326 | g = append(g, f) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2327 | } |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2328 | return append(g, r...) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2329 | } |
| 2330 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2331 | // from ascompat[ee] |
| 2332 | // a,b = c,d |
| 2333 | // simultaneous assignment. there cannot |
| 2334 | // be later use of an earlier lvalue. |
| 2335 | // |
| 2336 | // function calls have been removed. |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2337 | func reorder3(all []*Node) []*Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2338 | var l *Node |
| 2339 | |
| 2340 | // If a needed expression may be affected by an |
| 2341 | // earlier assignment, make an early copy of that |
| 2342 | // expression and use the copy instead. |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2343 | var early []*Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2344 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2345 | var mapinit Nodes |
| 2346 | for i, n := range all { |
| 2347 | l = n.Left |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2348 | |
| 2349 | // Save subexpressions needed on left side. |
| 2350 | // Drill through non-dereferences. |
| 2351 | for { |
| 2352 | if l.Op == ODOT || l.Op == OPAREN { |
| 2353 | l = l.Left |
| 2354 | continue |
| 2355 | } |
| 2356 | |
Matthew Dempsky | 1624a9c | 2016-03-30 14:45:47 -0700 | [diff] [blame] | 2357 | if l.Op == OINDEX && l.Left.Type.IsArray() { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2358 | l.Right = reorder3save(l.Right, all, i, &early) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2359 | l = l.Left |
| 2360 | continue |
| 2361 | } |
| 2362 | |
| 2363 | break |
| 2364 | } |
| 2365 | |
| 2366 | switch l.Op { |
| 2367 | default: |
Dave Cheney | 733f835 | 2016-04-27 19:34:17 +1000 | [diff] [blame] | 2368 | Fatalf("reorder3 unexpected lvalue %#v", l.Op) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2369 | |
| 2370 | case ONAME: |
| 2371 | break |
| 2372 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 2373 | case OINDEX, OINDEXMAP: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2374 | l.Left = reorder3save(l.Left, all, i, &early) |
| 2375 | l.Right = reorder3save(l.Right, all, i, &early) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2376 | if l.Op == OINDEXMAP { |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2377 | all[i] = convas(all[i], &mapinit) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2378 | } |
| 2379 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 2380 | case OIND, ODOTPTR: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2381 | l.Left = reorder3save(l.Left, all, i, &early) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | // Save expression on right side. |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2385 | all[i].Right = reorder3save(all[i].Right, all, i, &early) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2386 | } |
| 2387 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2388 | early = append(mapinit.Slice(), early...) |
| 2389 | return append(early, all...) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2390 | } |
| 2391 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2392 | // if the evaluation of *np would be affected by the |
Josh Bleecher Snyder | bcce5bd | 2015-06-10 15:31:53 -0700 | [diff] [blame] | 2393 | // assignments in all up to but not including the ith assignment, |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2394 | // copy into a temporary during *early and |
| 2395 | // replace *np with that temp. |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2396 | // The result of reorder3save MUST be assigned back to n, e.g. |
| 2397 | // n.Left = reorder3save(n.Left, all, i, early) |
| 2398 | func reorder3save(n *Node, all []*Node, i int, early *[]*Node) *Node { |
Josh Bleecher Snyder | bcce5bd | 2015-06-10 15:31:53 -0700 | [diff] [blame] | 2399 | if !aliased(n, all, i) { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2400 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2401 | } |
| 2402 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2403 | q := temp(n.Type) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2404 | q = Nod(OAS, q, n) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2405 | q = typecheck(q, Etop) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2406 | *early = append(*early, q) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2407 | return q.Left |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2408 | } |
| 2409 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2410 | // what's the outer value that a write to n affects? |
| 2411 | // outer value means containing struct or array. |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2412 | func outervalue(n *Node) *Node { |
| 2413 | for { |
| 2414 | if n.Op == OXDOT { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 2415 | Fatalf("OXDOT in walk") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2416 | } |
| 2417 | if n.Op == ODOT || n.Op == OPAREN || n.Op == OCONVNOP { |
| 2418 | n = n.Left |
| 2419 | continue |
| 2420 | } |
| 2421 | |
Matthew Dempsky | 1624a9c | 2016-03-30 14:45:47 -0700 | [diff] [blame] | 2422 | if n.Op == OINDEX && n.Left.Type != nil && n.Left.Type.IsArray() { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2423 | n = n.Left |
| 2424 | continue |
| 2425 | } |
| 2426 | |
| 2427 | break |
| 2428 | } |
| 2429 | |
| 2430 | return n |
| 2431 | } |
| 2432 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2433 | // Is it possible that the computation of n might be |
Josh Bleecher Snyder | bcce5bd | 2015-06-10 15:31:53 -0700 | [diff] [blame] | 2434 | // affected by writes in as up to but not including the ith element? |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2435 | func aliased(n *Node, all []*Node, i int) bool { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2436 | if n == nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2437 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2438 | } |
| 2439 | |
Ian Lance Taylor | 92bb694 | 2016-03-31 13:58:33 -0700 | [diff] [blame] | 2440 | // Treat all fields of a struct as referring to the whole struct. |
| 2441 | // We could do better but we would have to keep track of the fields. |
| 2442 | for n.Op == ODOT { |
| 2443 | n = n.Left |
| 2444 | } |
| 2445 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2446 | // Look for obvious aliasing: a variable being assigned |
| 2447 | // during the all list and appearing in n. |
| 2448 | // Also record whether there are any writes to main memory. |
| 2449 | // Also record whether there are any writes to variables |
| 2450 | // whose addresses have been taken. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2451 | memwrite := 0 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2452 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2453 | varwrite := 0 |
| 2454 | var a *Node |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2455 | for _, an := range all[:i] { |
| 2456 | a = outervalue(an.Left) |
Ian Lance Taylor | 92bb694 | 2016-03-31 13:58:33 -0700 | [diff] [blame] | 2457 | |
| 2458 | for a.Op == ODOT { |
| 2459 | a = a.Left |
| 2460 | } |
| 2461 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2462 | if a.Op != ONAME { |
| 2463 | memwrite = 1 |
| 2464 | continue |
| 2465 | } |
| 2466 | |
| 2467 | switch n.Class { |
| 2468 | default: |
| 2469 | varwrite = 1 |
| 2470 | continue |
| 2471 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 2472 | case PAUTO, PPARAM, PPARAMOUT: |
Dave Cheney | 7885de5 | 2015-03-05 18:20:54 +1100 | [diff] [blame] | 2473 | if n.Addrtaken { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2474 | varwrite = 1 |
| 2475 | continue |
| 2476 | } |
| 2477 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2478 | if vmatch2(a, n) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2479 | // Direct hit. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2480 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2481 | } |
| 2482 | } |
| 2483 | } |
| 2484 | |
| 2485 | // The variables being written do not appear in n. |
| 2486 | // However, n might refer to computed addresses |
| 2487 | // that are being written. |
| 2488 | |
| 2489 | // If no computed addresses are affected by the writes, no aliasing. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2490 | if memwrite == 0 && varwrite == 0 { |
| 2491 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2492 | } |
| 2493 | |
| 2494 | // If n does not refer to computed addresses |
| 2495 | // (that is, if n only refers to variables whose addresses |
| 2496 | // have not been taken), no aliasing. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2497 | if varexpr(n) { |
| 2498 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | // Otherwise, both the writes and n refer to computed memory addresses. |
| 2502 | // Assume that they might conflict. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2503 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2504 | } |
| 2505 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2506 | // does the evaluation of n only refer to variables |
| 2507 | // whose addresses have not been taken? |
| 2508 | // (and no other memory) |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2509 | func varexpr(n *Node) bool { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2510 | if n == nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2511 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | switch n.Op { |
| 2515 | case OLITERAL: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2516 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2517 | |
| 2518 | case ONAME: |
| 2519 | switch n.Class { |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 2520 | case PAUTO, PPARAM, PPARAMOUT: |
Dave Cheney | 7885de5 | 2015-03-05 18:20:54 +1100 | [diff] [blame] | 2521 | if !n.Addrtaken { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2522 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2523 | } |
| 2524 | } |
| 2525 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2526 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2527 | |
| 2528 | case OADD, |
| 2529 | OSUB, |
| 2530 | OOR, |
| 2531 | OXOR, |
| 2532 | OMUL, |
| 2533 | ODIV, |
| 2534 | OMOD, |
| 2535 | OLSH, |
| 2536 | ORSH, |
| 2537 | OAND, |
| 2538 | OANDNOT, |
| 2539 | OPLUS, |
| 2540 | OMINUS, |
| 2541 | OCOM, |
| 2542 | OPAREN, |
| 2543 | OANDAND, |
| 2544 | OOROR, |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2545 | OCONV, |
| 2546 | OCONVNOP, |
| 2547 | OCONVIFACE, |
| 2548 | ODOTTYPE: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2549 | return varexpr(n.Left) && varexpr(n.Right) |
Ian Lance Taylor | 5f525ca | 2016-03-18 16:52:30 -0700 | [diff] [blame] | 2550 | |
| 2551 | case ODOT: // but not ODOTPTR |
Ian Lance Taylor | 92bb694 | 2016-03-31 13:58:33 -0700 | [diff] [blame] | 2552 | // Should have been handled in aliased. |
| 2553 | Fatalf("varexpr unexpected ODOT") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | // Be conservative. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2557 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2558 | } |
| 2559 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2560 | // is the name l mentioned in r? |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2561 | func vmatch2(l *Node, r *Node) bool { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2562 | if r == nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2563 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2564 | } |
| 2565 | switch r.Op { |
| 2566 | // match each right given left |
| 2567 | case ONAME: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2568 | return l == r |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2569 | |
| 2570 | case OLITERAL: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2571 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2572 | } |
| 2573 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2574 | if vmatch2(l, r.Left) { |
| 2575 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2576 | } |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2577 | if vmatch2(l, r.Right) { |
| 2578 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2579 | } |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2580 | for _, n := range r.List.Slice() { |
| 2581 | if vmatch2(l, n) { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2582 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2583 | } |
| 2584 | } |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2585 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2586 | } |
| 2587 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2588 | // is any name mentioned in l also mentioned in r? |
| 2589 | // called by sinit.go |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2590 | func vmatch1(l *Node, r *Node) bool { |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2591 | // isolate all left sides |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2592 | if l == nil || r == nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2593 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2594 | } |
| 2595 | switch l.Op { |
| 2596 | case ONAME: |
| 2597 | switch l.Class { |
Russ Cox | 20803b8 | 2016-05-25 10:29:50 -0400 | [diff] [blame] | 2598 | case PPARAM, PAUTO: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2599 | break |
| 2600 | |
| 2601 | // assignment to non-stack variable |
| 2602 | // must be delayed if right has function calls. |
| 2603 | default: |
| 2604 | if r.Ullman >= UINF { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2605 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | return vmatch2(l, r) |
| 2610 | |
| 2611 | case OLITERAL: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2612 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2613 | } |
| 2614 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2615 | if vmatch1(l.Left, r) { |
| 2616 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2617 | } |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2618 | if vmatch1(l.Right, r) { |
| 2619 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2620 | } |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2621 | for _, n := range l.List.Slice() { |
| 2622 | if vmatch1(n, r) { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2623 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2624 | } |
| 2625 | } |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2626 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2627 | } |
| 2628 | |
Matthew Dempsky | 2339b13 | 2016-03-09 18:54:26 -0800 | [diff] [blame] | 2629 | // paramstoheap returns code to allocate memory for heap-escaped parameters |
| 2630 | // and to copy non-result prameters' values from the stack. |
| 2631 | // If out is true, then code is also produced to zero-initialize their |
| 2632 | // stack memory addresses. |
Russ Cox | dec1bae | 2016-05-25 10:01:58 -0400 | [diff] [blame] | 2633 | func paramstoheap(params *Type) []*Node { |
Ian Lance Taylor | 188e3d2 | 2016-02-26 14:28:48 -0800 | [diff] [blame] | 2634 | var nn []*Node |
Matthew Dempsky | f6bca3f | 2016-03-17 01:32:18 -0700 | [diff] [blame] | 2635 | for _, t := range params.Fields().Slice() { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2636 | // For precise stacks, the garbage collector assumes results |
| 2637 | // are always live, so zero them always. |
Russ Cox | dec1bae | 2016-05-25 10:01:58 -0400 | [diff] [blame] | 2638 | if params.StructType().Funarg == FunargResults { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2639 | // Defer might stop a panic and show the |
| 2640 | // return values as they exist at the time of panic. |
| 2641 | // Make sure to zero them on entry to the function. |
Russ Cox | dec1bae | 2016-05-25 10:01:58 -0400 | [diff] [blame] | 2642 | nn = append(nn, Nod(OAS, nodarg(t, 1), nil)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2643 | } |
| 2644 | |
Russ Cox | b6dc3e6 | 2016-05-25 01:33:24 -0400 | [diff] [blame] | 2645 | v := t.Nname |
| 2646 | if v != nil && v.Sym != nil && strings.HasPrefix(v.Sym.Name, "~r") { // unnamed result |
| 2647 | v = nil |
| 2648 | } |
| 2649 | if v == nil { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2650 | continue |
| 2651 | } |
| 2652 | |
Russ Cox | b6dc3e6 | 2016-05-25 01:33:24 -0400 | [diff] [blame] | 2653 | if stackcopy := v.Name.Param.Stackcopy; stackcopy != nil { |
| 2654 | nn = append(nn, walkstmt(Nod(ODCL, v, nil))) |
| 2655 | if stackcopy.Class == PPARAM { |
| 2656 | nn = append(nn, walkstmt(typecheck(Nod(OAS, v, stackcopy), Etop))) |
| 2657 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2658 | } |
| 2659 | } |
| 2660 | |
| 2661 | return nn |
| 2662 | } |
| 2663 | |
Matthew Dempsky | 2339b13 | 2016-03-09 18:54:26 -0800 | [diff] [blame] | 2664 | // returnsfromheap returns code to copy values for heap-escaped parameters |
| 2665 | // back to the stack. |
| 2666 | func returnsfromheap(params *Type) []*Node { |
Ian Lance Taylor | 188e3d2 | 2016-02-26 14:28:48 -0800 | [diff] [blame] | 2667 | var nn []*Node |
Matthew Dempsky | f6bca3f | 2016-03-17 01:32:18 -0700 | [diff] [blame] | 2668 | for _, t := range params.Fields().Slice() { |
Matthew Dempsky | 2339b13 | 2016-03-09 18:54:26 -0800 | [diff] [blame] | 2669 | v := t.Nname |
Russ Cox | b6dc3e6 | 2016-05-25 01:33:24 -0400 | [diff] [blame] | 2670 | if v == nil { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2671 | continue |
| 2672 | } |
Russ Cox | b6dc3e6 | 2016-05-25 01:33:24 -0400 | [diff] [blame] | 2673 | if stackcopy := v.Name.Param.Stackcopy; stackcopy != nil && stackcopy.Class == PPARAMOUT { |
| 2674 | nn = append(nn, walkstmt(typecheck(Nod(OAS, stackcopy, v), Etop))) |
| 2675 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2676 | } |
| 2677 | |
| 2678 | return nn |
| 2679 | } |
| 2680 | |
Matthew Dempsky | 2339b13 | 2016-03-09 18:54:26 -0800 | [diff] [blame] | 2681 | // heapmoves generates code to handle migrating heap-escaped parameters |
| 2682 | // between the stack and the heap. The generated code is added to Curfn's |
| 2683 | // Enter and Exit lists. |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2684 | func heapmoves() { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2685 | lno := lineno |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2686 | lineno = Curfn.Lineno |
Russ Cox | dec1bae | 2016-05-25 10:01:58 -0400 | [diff] [blame] | 2687 | nn := paramstoheap(Curfn.Type.Recvs()) |
| 2688 | nn = append(nn, paramstoheap(Curfn.Type.Params())...) |
| 2689 | nn = append(nn, paramstoheap(Curfn.Type.Results())...) |
Ian Lance Taylor | 188e3d2 | 2016-02-26 14:28:48 -0800 | [diff] [blame] | 2690 | Curfn.Func.Enter.Append(nn...) |
Josh Bleecher Snyder | 3ed9e4c | 2015-03-25 19:33:01 -0700 | [diff] [blame] | 2691 | lineno = Curfn.Func.Endlineno |
Matthew Dempsky | 2339b13 | 2016-03-09 18:54:26 -0800 | [diff] [blame] | 2692 | Curfn.Func.Exit.Append(returnsfromheap(Curfn.Type.Results())...) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2693 | lineno = lno |
| 2694 | } |
| 2695 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2696 | func vmkcall(fn *Node, t *Type, init *Nodes, va []*Node) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2697 | if fn.Type == nil || fn.Type.Etype != TFUNC { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 2698 | Fatalf("mkcall %v %v", fn, fn.Type) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2699 | } |
| 2700 | |
Matthew Dempsky | c837761 | 2016-03-17 01:47:16 -0700 | [diff] [blame] | 2701 | n := fn.Type.Params().NumFields() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2702 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2703 | r := Nod(OCALL, fn, nil) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2704 | r.List.Set(va[:n]) |
Matthew Dempsky | c837761 | 2016-03-17 01:47:16 -0700 | [diff] [blame] | 2705 | if fn.Type.Results().NumFields() > 0 { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2706 | r = typecheck(r, Erv|Efnstruct) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2707 | } else { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2708 | r = typecheck(r, Etop) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2709 | } |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2710 | r = walkexpr(r, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2711 | r.Type = t |
| 2712 | return r |
| 2713 | } |
| 2714 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2715 | func mkcall(name string, t *Type, init *Nodes, args ...*Node) *Node { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2716 | return vmkcall(syslook(name), t, init, args) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2717 | } |
| 2718 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2719 | func mkcall1(fn *Node, t *Type, init *Nodes, args ...*Node) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2720 | return vmkcall(fn, t, init, args) |
| 2721 | } |
| 2722 | |
| 2723 | func conv(n *Node, t *Type) *Node { |
| 2724 | if Eqtype(n.Type, t) { |
| 2725 | return n |
| 2726 | } |
| 2727 | n = Nod(OCONV, n, nil) |
| 2728 | n.Type = t |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2729 | n = typecheck(n, Erv) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2730 | return n |
| 2731 | } |
| 2732 | |
| 2733 | func chanfn(name string, n int, t *Type) *Node { |
Matthew Dempsky | 3efefd9 | 2016-03-30 14:56:08 -0700 | [diff] [blame] | 2734 | if !t.IsChan() { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 2735 | Fatalf("chanfn %v", t) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2736 | } |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2737 | fn := syslook(name) |
Russ Cox | 13f9c8b | 2015-03-08 13:33:49 -0400 | [diff] [blame] | 2738 | switch n { |
| 2739 | default: |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 2740 | Fatalf("chanfn %d", n) |
Russ Cox | 13f9c8b | 2015-03-08 13:33:49 -0400 | [diff] [blame] | 2741 | case 1: |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 2742 | fn = substArgTypes(fn, t.Elem()) |
Russ Cox | 13f9c8b | 2015-03-08 13:33:49 -0400 | [diff] [blame] | 2743 | case 2: |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 2744 | fn = substArgTypes(fn, t.Elem(), t.Elem()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2745 | } |
| 2746 | return fn |
| 2747 | } |
| 2748 | |
| 2749 | func mapfn(name string, t *Type) *Node { |
Matthew Dempsky | 3efefd9 | 2016-03-30 14:56:08 -0700 | [diff] [blame] | 2750 | if !t.IsMap() { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 2751 | Fatalf("mapfn %v", t) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2752 | } |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2753 | fn := syslook(name) |
Josh Bleecher Snyder | 093a9a1 | 2016-03-28 21:48:47 -0700 | [diff] [blame] | 2754 | fn = substArgTypes(fn, t.Key(), t.Val(), t.Key(), t.Val()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2755 | return fn |
| 2756 | } |
| 2757 | |
| 2758 | func mapfndel(name string, t *Type) *Node { |
Matthew Dempsky | 3efefd9 | 2016-03-30 14:56:08 -0700 | [diff] [blame] | 2759 | if !t.IsMap() { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 2760 | Fatalf("mapfn %v", t) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2761 | } |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2762 | fn := syslook(name) |
Josh Bleecher Snyder | 093a9a1 | 2016-03-28 21:48:47 -0700 | [diff] [blame] | 2763 | fn = substArgTypes(fn, t.Key(), t.Val(), t.Key()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2764 | return fn |
| 2765 | } |
| 2766 | |
| 2767 | func writebarrierfn(name string, l *Type, r *Type) *Node { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2768 | fn := syslook(name) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2769 | fn = substArgTypes(fn, l, r) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2770 | return fn |
| 2771 | } |
| 2772 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2773 | func addstr(n *Node, init *Nodes) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2774 | // orderexpr rewrote OADDSTR to have a list of strings. |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2775 | c := n.List.Len() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2776 | |
| 2777 | if c < 2 { |
| 2778 | Yyerror("addstr count %d too small", c) |
| 2779 | } |
| 2780 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2781 | buf := nodnil() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2782 | if n.Esc == EscNone { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2783 | sz := int64(0) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2784 | for _, n1 := range n.List.Slice() { |
| 2785 | if n1.Op == OLITERAL { |
| 2786 | sz += int64(len(n1.Val().U.(string))) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2787 | } |
| 2788 | } |
| 2789 | |
| 2790 | // Don't allocate the buffer if the result won't fit. |
| 2791 | if sz < tmpstringbufsize { |
| 2792 | // Create temporary buffer for result string on stack. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2793 | t := aindex(Nodintconst(tmpstringbufsize), Types[TUINT8]) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2794 | |
| 2795 | buf = Nod(OADDR, temp(t), nil) |
| 2796 | } |
| 2797 | } |
| 2798 | |
| 2799 | // build list of string arguments |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 2800 | args := []*Node{buf} |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2801 | for _, n2 := range n.List.Slice() { |
| 2802 | args = append(args, conv(n2, Types[TSTRING])) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2803 | } |
| 2804 | |
Matthew Dempsky | 8b3670f | 2015-03-06 12:02:24 -0800 | [diff] [blame] | 2805 | var fn string |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2806 | if c <= 5 { |
| 2807 | // small numbers of strings use direct runtime helpers. |
| 2808 | // note: orderexpr knows this cutoff too. |
Matthew Dempsky | 8b3670f | 2015-03-06 12:02:24 -0800 | [diff] [blame] | 2809 | fn = fmt.Sprintf("concatstring%d", c) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2810 | } else { |
| 2811 | // large numbers of strings are passed to the runtime as a slice. |
Matthew Dempsky | 8b3670f | 2015-03-06 12:02:24 -0800 | [diff] [blame] | 2812 | fn = "concatstrings" |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2813 | |
Josh Bleecher Snyder | eb98e51 | 2016-03-28 22:57:57 -0700 | [diff] [blame] | 2814 | t := typSlice(Types[TSTRING]) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2815 | slice := Nod(OCOMPLIT, nil, typenod(t)) |
Russ Cox | 60e5f5b | 2015-05-26 23:05:35 -0400 | [diff] [blame] | 2816 | if prealloc[n] != nil { |
| 2817 | prealloc[slice] = prealloc[n] |
| 2818 | } |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2819 | slice.List.Set(args[1:]) // skip buf arg |
Keith Randall | 934c359 | 2016-04-23 22:59:01 -0700 | [diff] [blame] | 2820 | args = []*Node{buf, slice} |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2821 | slice.Esc = EscNone |
| 2822 | } |
| 2823 | |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2824 | cat := syslook(fn) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2825 | r := Nod(OCALL, cat, nil) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2826 | r.List.Set(args) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2827 | r = typecheck(r, Erv) |
| 2828 | r = walkexpr(r, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2829 | r.Type = n.Type |
| 2830 | |
| 2831 | return r |
| 2832 | } |
| 2833 | |
| 2834 | // expand append(l1, l2...) to |
| 2835 | // init { |
| 2836 | // s := l1 |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2837 | // n := len(s) + len(l2) |
| 2838 | // // Compare as uint so growslice can panic on overflow. |
| 2839 | // if uint(n) > uint(cap(s)) { |
| 2840 | // s = growslice(s, n) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2841 | // } |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2842 | // s = s[:n] |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2843 | // memmove(&s[len(l1)], &l2[0], len(l2)*sizeof(T)) |
| 2844 | // } |
| 2845 | // s |
| 2846 | // |
| 2847 | // l2 is allowed to be a string. |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2848 | func appendslice(n *Node, init *Nodes) *Node { |
| 2849 | walkexprlistsafe(n.List.Slice(), init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2850 | |
| 2851 | // walkexprlistsafe will leave OINDEX (s[n]) alone if both s |
| 2852 | // and n are name or literal, but those may index the slice we're |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 2853 | // modifying here. Fix explicitly. |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 2854 | ls := n.List.Slice() |
| 2855 | for i1, n1 := range ls { |
| 2856 | ls[i1] = cheapexpr(n1, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2857 | } |
| 2858 | |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2859 | l1 := n.List.First() |
| 2860 | l2 := n.List.Second() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2861 | |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 2862 | var l []*Node |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2863 | |
| 2864 | // var s []T |
| 2865 | s := temp(l1.Type) |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 2866 | l = append(l, Nod(OAS, s, l1)) // s = l1 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2867 | |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2868 | // n := len(s) + len(l2) |
| 2869 | nn := temp(Types[TINT]) |
| 2870 | l = append(l, Nod(OAS, nn, Nod(OADD, Nod(OLEN, s, nil), Nod(OLEN, l2, nil)))) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2871 | |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2872 | // if uint(n) > uint(cap(s)) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2873 | nif := Nod(OIF, nil, nil) |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2874 | nif.Left = Nod(OGT, Nod(OCONV, nn, nil), Nod(OCONV, Nod(OCAP, s, nil), nil)) |
| 2875 | nif.Left.Left.Type = Types[TUINT] |
| 2876 | nif.Left.Right.Type = Types[TUINT] |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2877 | |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2878 | // instantiate growslice(Type*, []any, int) []any |
| 2879 | fn := syslook("growslice") |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 2880 | fn = substArgTypes(fn, s.Type.Elem(), s.Type.Elem()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2881 | |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2882 | // s = growslice(T, s, n) |
Keith Randall | bfe0cbd | 2016-04-19 15:38:59 -0700 | [diff] [blame] | 2883 | nif.Nbody.Set1(Nod(OAS, s, mkcall1(fn, s.Type, &nif.Ninit, typename(s.Type.Elem()), s, nn))) |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 2884 | l = append(l, nif) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2885 | |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2886 | // s = s[:n] |
Josh Bleecher Snyder | f12bd8a | 2016-04-21 11:55:33 -0700 | [diff] [blame] | 2887 | nt := Nod(OSLICE, s, nil) |
| 2888 | nt.SetSliceBounds(nil, nn, nil) |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2889 | nt.Etype = 1 |
| 2890 | l = append(l, Nod(OAS, s, nt)) |
| 2891 | |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 2892 | if haspointers(l1.Type.Elem()) { |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2893 | // copy(s[len(l1):], l2) |
Josh Bleecher Snyder | f12bd8a | 2016-04-21 11:55:33 -0700 | [diff] [blame] | 2894 | nptr1 := Nod(OSLICE, s, nil) |
| 2895 | nptr1.SetSliceBounds(Nod(OLEN, l1, nil), nil, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2896 | nptr1.Etype = 1 |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2897 | nptr2 := l2 |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2898 | fn := syslook("typedslicecopy") |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2899 | fn = substArgTypes(fn, l1.Type, l2.Type) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2900 | var ln Nodes |
| 2901 | ln.Set(l) |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 2902 | nt := mkcall1(fn, Types[TINT], &ln, typename(l1.Type.Elem()), nptr1, nptr2) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2903 | l = append(ln.Slice(), nt) |
Ian Lance Taylor | 9e902f0 | 2015-10-20 10:00:07 -0700 | [diff] [blame] | 2904 | } else if instrumenting { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2905 | // rely on runtime to instrument copy. |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2906 | // copy(s[len(l1):], l2) |
Josh Bleecher Snyder | f12bd8a | 2016-04-21 11:55:33 -0700 | [diff] [blame] | 2907 | nptr1 := Nod(OSLICE, s, nil) |
| 2908 | nptr1.SetSliceBounds(Nod(OLEN, l1, nil), nil, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2909 | nptr1.Etype = 1 |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2910 | nptr2 := l2 |
| 2911 | var fn *Node |
Matthew Dempsky | 3efefd9 | 2016-03-30 14:56:08 -0700 | [diff] [blame] | 2912 | if l2.Type.IsString() { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2913 | fn = syslook("slicestringcopy") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2914 | } else { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2915 | fn = syslook("slicecopy") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2916 | } |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2917 | fn = substArgTypes(fn, l1.Type, l2.Type) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2918 | var ln Nodes |
| 2919 | ln.Set(l) |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 2920 | nt := mkcall1(fn, Types[TINT], &ln, nptr1, nptr2, Nodintconst(s.Type.Elem().Width)) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2921 | l = append(ln.Slice(), nt) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2922 | } else { |
| 2923 | // memmove(&s[len(l1)], &l2[0], len(l2)*sizeof(T)) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2924 | nptr1 := Nod(OINDEX, s, Nod(OLEN, l1, nil)) |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2925 | nptr1.Bounded = true |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2926 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2927 | nptr1 = Nod(OADDR, nptr1, nil) |
| 2928 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2929 | nptr2 := Nod(OSPTR, l2, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2930 | |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2931 | fn := syslook("memmove") |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 2932 | fn = substArgTypes(fn, s.Type.Elem(), s.Type.Elem()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2933 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2934 | var ln Nodes |
| 2935 | ln.Set(l) |
| 2936 | nwid := cheapexpr(conv(Nod(OLEN, l2, nil), Types[TUINTPTR]), &ln) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2937 | |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 2938 | nwid = Nod(OMUL, nwid, Nodintconst(s.Type.Elem().Width)) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2939 | nt := mkcall1(fn, nil, &ln, nptr1, nptr2, nwid) |
| 2940 | l = append(ln.Slice(), nt) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2941 | } |
| 2942 | |
Josh Bleecher Snyder | ec7c494 | 2016-03-19 17:02:01 -0700 | [diff] [blame] | 2943 | typecheckslice(l, Etop) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2944 | walkstmtlist(l) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2945 | init.Append(l...) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2946 | return s |
| 2947 | } |
| 2948 | |
Russ Cox | 8552047 | 2015-05-06 12:34:30 -0400 | [diff] [blame] | 2949 | // Rewrite append(src, x, y, z) so that any side effects in |
| 2950 | // x, y, z (including runtime panics) are evaluated in |
| 2951 | // initialization statements before the append. |
| 2952 | // For normal code generation, stop there and leave the |
| 2953 | // rest to cgen_append. |
| 2954 | // |
| 2955 | // For race detector, expand append(src, a [, b]* ) to |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2956 | // |
| 2957 | // init { |
| 2958 | // s := src |
| 2959 | // const argc = len(args) - 1 |
| 2960 | // if cap(s) - len(s) < argc { |
Russ Cox | 32fddad | 2015-06-25 19:27:20 -0400 | [diff] [blame] | 2961 | // s = growslice(s, len(s)+argc) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2962 | // } |
| 2963 | // n := len(s) |
| 2964 | // s = s[:n+argc] |
| 2965 | // s[n] = a |
| 2966 | // s[n+1] = b |
| 2967 | // ... |
| 2968 | // } |
| 2969 | // s |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2970 | func walkappend(n *Node, init *Nodes, dst *Node) *Node { |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2971 | if !samesafeexpr(dst, n.List.First()) { |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 2972 | n.List.SetIndex(0, safeexpr(n.List.Index(0), init)) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 2973 | n.List.SetIndex(0, walkexpr(n.List.Index(0), init)) |
Russ Cox | 8552047 | 2015-05-06 12:34:30 -0400 | [diff] [blame] | 2974 | } |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2975 | walkexprlistsafe(n.List.Slice()[1:], init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2976 | |
| 2977 | // walkexprlistsafe will leave OINDEX (s[n]) alone if both s |
| 2978 | // and n are name or literal, but those may index the slice we're |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 2979 | // modifying here. Fix explicitly. |
Russ Cox | 8552047 | 2015-05-06 12:34:30 -0400 | [diff] [blame] | 2980 | // Using cheapexpr also makes sure that the evaluation |
| 2981 | // of all arguments (and especially any panics) happen |
| 2982 | // before we begin to modify the slice in a visible way. |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 2983 | ls := n.List.Slice()[1:] |
| 2984 | for i, n := range ls { |
| 2985 | ls[i] = cheapexpr(n, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2986 | } |
| 2987 | |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2988 | nsrc := n.List.First() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2989 | |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2990 | argc := n.List.Len() - 1 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2991 | if argc < 1 { |
| 2992 | return nsrc |
| 2993 | } |
| 2994 | |
Russ Cox | 8552047 | 2015-05-06 12:34:30 -0400 | [diff] [blame] | 2995 | // General case, with no function calls left as arguments. |
Ian Lance Taylor | 0c69f13 | 2015-10-21 07:04:10 -0700 | [diff] [blame] | 2996 | // Leave for gen, except that instrumentation requires old form. |
Ian Lance Taylor | 9e902f0 | 2015-10-20 10:00:07 -0700 | [diff] [blame] | 2997 | if !instrumenting { |
Russ Cox | 8552047 | 2015-05-06 12:34:30 -0400 | [diff] [blame] | 2998 | return n |
| 2999 | } |
| 3000 | |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 3001 | var l []*Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3002 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3003 | ns := temp(nsrc.Type) |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 3004 | l = append(l, Nod(OAS, ns, nsrc)) // s = src |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3005 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3006 | na := Nodintconst(int64(argc)) // const argc |
| 3007 | nx := Nod(OIF, nil, nil) // if cap(s) - len(s) < argc |
Russ Cox | 66be148 | 2015-05-26 21:30:20 -0400 | [diff] [blame] | 3008 | nx.Left = Nod(OLT, Nod(OSUB, Nod(OCAP, ns, nil), Nod(OLEN, ns, nil)), na) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3009 | |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 3010 | fn := syslook("growslice") // growslice(<type>, old []T, mincap int) (ret []T) |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 3011 | fn = substArgTypes(fn, ns.Type.Elem(), ns.Type.Elem()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3012 | |
Ian Lance Taylor | c63dbd8 | 2016-03-10 10:13:42 -0800 | [diff] [blame] | 3013 | nx.Nbody.Set1(Nod(OAS, ns, |
Keith Randall | bfe0cbd | 2016-04-19 15:38:59 -0700 | [diff] [blame] | 3014 | mkcall1(fn, ns.Type, &nx.Ninit, typename(ns.Type.Elem()), ns, |
Ian Lance Taylor | c63dbd8 | 2016-03-10 10:13:42 -0800 | [diff] [blame] | 3015 | Nod(OADD, Nod(OLEN, ns, nil), na)))) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3016 | |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 3017 | l = append(l, nx) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3018 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3019 | nn := temp(Types[TINT]) |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 3020 | l = append(l, Nod(OAS, nn, Nod(OLEN, ns, nil))) // n = len(s) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3021 | |
Josh Bleecher Snyder | f12bd8a | 2016-04-21 11:55:33 -0700 | [diff] [blame] | 3022 | nx = Nod(OSLICE, ns, nil) // ...s[:n+argc] |
| 3023 | nx.SetSliceBounds(nil, Nod(OADD, nn, na), nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3024 | nx.Etype = 1 |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 3025 | l = append(l, Nod(OAS, ns, nx)) // s = s[:n+argc] |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3026 | |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 3027 | ls = n.List.Slice()[1:] |
| 3028 | for i, n := range ls { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3029 | nx = Nod(OINDEX, ns, nn) // s[n] ... |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3030 | nx.Bounded = true |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 3031 | l = append(l, Nod(OAS, nx, n)) // s[n] = arg |
| 3032 | if i+1 < len(ls) { |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 3033 | l = append(l, Nod(OAS, nn, Nod(OADD, nn, Nodintconst(1)))) // n = n + 1 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3034 | } |
| 3035 | } |
| 3036 | |
Josh Bleecher Snyder | ec7c494 | 2016-03-19 17:02:01 -0700 | [diff] [blame] | 3037 | typecheckslice(l, Etop) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3038 | walkstmtlist(l) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3039 | init.Append(l...) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3040 | return ns |
| 3041 | } |
| 3042 | |
| 3043 | // Lower copy(a, b) to a memmove call or a runtime call. |
| 3044 | // |
| 3045 | // init { |
| 3046 | // n := len(a) |
| 3047 | // if n > len(b) { n = len(b) } |
| 3048 | // memmove(a.ptr, b.ptr, n*sizeof(elem(a))) |
| 3049 | // } |
| 3050 | // n; |
| 3051 | // |
| 3052 | // Also works if b is a string. |
| 3053 | // |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3054 | func copyany(n *Node, init *Nodes, runtimecall bool) *Node { |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 3055 | if haspointers(n.Left.Type.Elem()) { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3056 | fn := writebarrierfn("typedslicecopy", n.Left.Type, n.Right.Type) |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 3057 | return mkcall1(fn, n.Type, init, typename(n.Left.Type.Elem()), n.Left, n.Right) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3058 | } |
| 3059 | |
Ian Lance Taylor | 9e902f0 | 2015-10-20 10:00:07 -0700 | [diff] [blame] | 3060 | if runtimecall { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3061 | var fn *Node |
Matthew Dempsky | 3efefd9 | 2016-03-30 14:56:08 -0700 | [diff] [blame] | 3062 | if n.Right.Type.IsString() { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 3063 | fn = syslook("slicestringcopy") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3064 | } else { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 3065 | fn = syslook("slicecopy") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3066 | } |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3067 | fn = substArgTypes(fn, n.Left.Type, n.Right.Type) |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 3068 | return mkcall1(fn, n.Type, init, n.Left, n.Right, Nodintconst(n.Left.Type.Elem().Width)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3069 | } |
| 3070 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3071 | n.Left = walkexpr(n.Left, init) |
| 3072 | n.Right = walkexpr(n.Right, init) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3073 | nl := temp(n.Left.Type) |
| 3074 | nr := temp(n.Right.Type) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3075 | var l []*Node |
| 3076 | l = append(l, Nod(OAS, nl, n.Left)) |
| 3077 | l = append(l, Nod(OAS, nr, n.Right)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3078 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3079 | nfrm := Nod(OSPTR, nr, nil) |
| 3080 | nto := Nod(OSPTR, nl, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3081 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3082 | nlen := temp(Types[TINT]) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3083 | |
| 3084 | // n = len(to) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3085 | l = append(l, Nod(OAS, nlen, Nod(OLEN, nl, nil))) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3086 | |
| 3087 | // if n > len(frm) { n = len(frm) } |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3088 | nif := Nod(OIF, nil, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3089 | |
Russ Cox | 66be148 | 2015-05-26 21:30:20 -0400 | [diff] [blame] | 3090 | nif.Left = Nod(OGT, nlen, Nod(OLEN, nr, nil)) |
Ian Lance Taylor | 1d5001a | 2016-02-27 14:31:33 -0800 | [diff] [blame] | 3091 | nif.Nbody.Append(Nod(OAS, nlen, Nod(OLEN, nr, nil))) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3092 | l = append(l, nif) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3093 | |
| 3094 | // Call memmove. |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 3095 | fn := syslook("memmove") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3096 | |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 3097 | fn = substArgTypes(fn, nl.Type.Elem(), nl.Type.Elem()) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3098 | nwid := temp(Types[TUINTPTR]) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3099 | l = append(l, Nod(OAS, nwid, conv(nlen, Types[TUINTPTR]))) |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 3100 | nwid = Nod(OMUL, nwid, Nodintconst(nl.Type.Elem().Width)) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3101 | l = append(l, mkcall1(fn, nil, init, nto, nfrm, nwid)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3102 | |
Josh Bleecher Snyder | ec7c494 | 2016-03-19 17:02:01 -0700 | [diff] [blame] | 3103 | typecheckslice(l, Etop) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3104 | walkstmtlist(l) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3105 | init.Append(l...) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3106 | return nlen |
| 3107 | } |
| 3108 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3109 | func eqfor(t *Type, needsize *int) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3110 | // Should only arrive here with large memory or |
| 3111 | // a struct/array containing a non-memory field/element. |
| 3112 | // Small memory is handled inline, and single non-memory |
| 3113 | // is handled during type check (OCMPSTR etc). |
Matthew Dempsky | 077902d | 2016-04-01 11:22:03 -0700 | [diff] [blame] | 3114 | switch a, _ := algtype1(t); a { |
| 3115 | case AMEM: |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 3116 | n := syslook("memequal") |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3117 | n = substArgTypes(n, t, t) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3118 | *needsize = 1 |
| 3119 | return n |
Matthew Dempsky | 077902d | 2016-04-01 11:22:03 -0700 | [diff] [blame] | 3120 | case ASPECIAL: |
| 3121 | sym := typesymprefix(".eq", t) |
| 3122 | n := newname(sym) |
| 3123 | n.Class = PFUNC |
| 3124 | ntype := Nod(OTFUNC, nil, nil) |
| 3125 | ntype.List.Append(Nod(ODCLFIELD, nil, typenod(Ptrto(t)))) |
| 3126 | ntype.List.Append(Nod(ODCLFIELD, nil, typenod(Ptrto(t)))) |
| 3127 | ntype.Rlist.Append(Nod(ODCLFIELD, nil, typenod(Types[TBOOL]))) |
| 3128 | ntype = typecheck(ntype, Etype) |
| 3129 | n.Type = ntype.Type |
| 3130 | *needsize = 0 |
| 3131 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3132 | } |
Matthew Dempsky | 077902d | 2016-04-01 11:22:03 -0700 | [diff] [blame] | 3133 | Fatalf("eqfor %v", t) |
| 3134 | return nil |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3135 | } |
| 3136 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3137 | // The result of walkcompare MUST be assigned back to n, e.g. |
| 3138 | // n.Left = walkcompare(n.Left, init) |
| 3139 | func walkcompare(n *Node, init *Nodes) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3140 | // Given interface value l and concrete value r, rewrite |
| 3141 | // l == r |
Josh Bleecher Snyder | 1a7fc7b | 2016-06-06 13:01:48 -0700 | [diff] [blame^] | 3142 | // into types-equal && data-equal. |
| 3143 | // This is efficient, avoids allocations, and avoids runtime calls. |
| 3144 | var l, r *Node |
Matthew Dempsky | 1624a9c | 2016-03-30 14:45:47 -0700 | [diff] [blame] | 3145 | if n.Left.Type.IsInterface() && !n.Right.Type.IsInterface() { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3146 | l = n.Left |
| 3147 | r = n.Right |
Matthew Dempsky | 1624a9c | 2016-03-30 14:45:47 -0700 | [diff] [blame] | 3148 | } else if !n.Left.Type.IsInterface() && n.Right.Type.IsInterface() { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3149 | l = n.Right |
| 3150 | r = n.Left |
| 3151 | } |
| 3152 | |
| 3153 | if l != nil { |
Josh Bleecher Snyder | 1a7fc7b | 2016-06-06 13:01:48 -0700 | [diff] [blame^] | 3154 | // Handle both == and !=. |
| 3155 | eq := n.Op |
| 3156 | var andor Op |
| 3157 | if eq == OEQ { |
| 3158 | andor = OANDAND |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3159 | } else { |
Josh Bleecher Snyder | 1a7fc7b | 2016-06-06 13:01:48 -0700 | [diff] [blame^] | 3160 | andor = OOROR |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3161 | } |
Josh Bleecher Snyder | 1a7fc7b | 2016-06-06 13:01:48 -0700 | [diff] [blame^] | 3162 | // Check for types equal. |
| 3163 | // For empty interface, this is: |
| 3164 | // l.tab == type(r) |
| 3165 | // For non-empty interface, this is: |
| 3166 | // l.tab != nil && l.tab._type == type(r) |
| 3167 | var eqtype *Node |
| 3168 | tab := Nod(OITAB, l, nil) |
| 3169 | rtyp := typename(r.Type) |
| 3170 | if l.Type.IsEmptyInterface() { |
| 3171 | tab.Type = Ptrto(Types[TUINT8]) |
| 3172 | tab.Typecheck = 1 |
| 3173 | eqtype = Nod(eq, tab, rtyp) |
| 3174 | } else { |
| 3175 | nonnil := Nod(Brcom(eq), nodnil(), tab) |
| 3176 | match := Nod(eq, itabType(tab), rtyp) |
| 3177 | eqtype = Nod(andor, nonnil, match) |
| 3178 | } |
| 3179 | // Check for data equal. |
| 3180 | eqdata := Nod(eq, ifaceData(l, r.Type), r) |
| 3181 | // Put it all together. |
| 3182 | expr := Nod(andor, eqtype, eqdata) |
| 3183 | n = finishcompare(n, expr, init) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3184 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | // Must be comparison of array or struct. |
| 3188 | // Otherwise back end handles it. |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3189 | t := n.Left.Type |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3190 | |
| 3191 | switch t.Etype { |
| 3192 | default: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3193 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3194 | |
Matthew Dempsky | 40f1d0c | 2016-04-18 14:02:08 -0700 | [diff] [blame] | 3195 | case TARRAY, TSTRUCT: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3196 | break |
| 3197 | } |
| 3198 | |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3199 | cmpl := n.Left |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3200 | for cmpl != nil && cmpl.Op == OCONVNOP { |
| 3201 | cmpl = cmpl.Left |
| 3202 | } |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3203 | cmpr := n.Right |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3204 | for cmpr != nil && cmpr.Op == OCONVNOP { |
| 3205 | cmpr = cmpr.Left |
| 3206 | } |
| 3207 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3208 | if !islvalue(cmpl) || !islvalue(cmpr) { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 3209 | Fatalf("arguments of comparison must be lvalues - %v %v", cmpl, cmpr) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | l = temp(Ptrto(t)) |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3213 | a := Nod(OAS, l, Nod(OADDR, cmpl, nil)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3214 | a.Right.Etype = 1 // addr does not escape |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3215 | a = typecheck(a, Etop) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3216 | init.Append(a) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3217 | |
| 3218 | r = temp(Ptrto(t)) |
| 3219 | a = Nod(OAS, r, Nod(OADDR, cmpr, nil)) |
| 3220 | a.Right.Etype = 1 // addr does not escape |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3221 | a = typecheck(a, Etop) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3222 | init.Append(a) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3223 | |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 3224 | var andor Op = OANDAND |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3225 | if n.Op == ONE { |
| 3226 | andor = OOROR |
| 3227 | } |
| 3228 | |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3229 | var expr *Node |
Josh Bleecher Snyder | 3a0783c | 2016-03-31 14:46:04 -0700 | [diff] [blame] | 3230 | if t.Etype == TARRAY && t.NumElem() <= 4 && issimple[t.Elem().Etype] { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3231 | // Four or fewer elements of a basic type. |
| 3232 | // Unroll comparisons. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3233 | var li *Node |
| 3234 | var ri *Node |
Josh Bleecher Snyder | 3a0783c | 2016-03-31 14:46:04 -0700 | [diff] [blame] | 3235 | for i := 0; int64(i) < t.NumElem(); i++ { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3236 | li = Nod(OINDEX, l, Nodintconst(int64(i))) |
| 3237 | ri = Nod(OINDEX, r, Nodintconst(int64(i))) |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 3238 | a = Nod(n.Op, li, ri) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3239 | if expr == nil { |
| 3240 | expr = a |
| 3241 | } else { |
| 3242 | expr = Nod(andor, expr, a) |
| 3243 | } |
| 3244 | } |
| 3245 | |
| 3246 | if expr == nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3247 | expr = Nodbool(n.Op == OEQ) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3248 | } |
Josh Bleecher Snyder | babc735 | 2016-03-23 16:43:17 -0700 | [diff] [blame] | 3249 | n = finishcompare(n, expr, init) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3250 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3251 | } |
| 3252 | |
Josh Bleecher Snyder | 4cef0e9 | 2015-05-04 15:02:09 -0700 | [diff] [blame] | 3253 | if t.Etype == TARRAY { |
| 3254 | // Zero- or single-element array, of any type. |
Josh Bleecher Snyder | 3a0783c | 2016-03-31 14:46:04 -0700 | [diff] [blame] | 3255 | switch t.NumElem() { |
Josh Bleecher Snyder | 4cef0e9 | 2015-05-04 15:02:09 -0700 | [diff] [blame] | 3256 | case 0: |
Josh Bleecher Snyder | babc735 | 2016-03-23 16:43:17 -0700 | [diff] [blame] | 3257 | n = finishcompare(n, Nodbool(n.Op == OEQ), init) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3258 | return n |
Josh Bleecher Snyder | 4cef0e9 | 2015-05-04 15:02:09 -0700 | [diff] [blame] | 3259 | case 1: |
| 3260 | l0 := Nod(OINDEX, l, Nodintconst(0)) |
| 3261 | r0 := Nod(OINDEX, r, Nodintconst(0)) |
| 3262 | a := Nod(n.Op, l0, r0) |
Josh Bleecher Snyder | babc735 | 2016-03-23 16:43:17 -0700 | [diff] [blame] | 3263 | n = finishcompare(n, a, init) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3264 | return n |
Josh Bleecher Snyder | 4cef0e9 | 2015-05-04 15:02:09 -0700 | [diff] [blame] | 3265 | } |
| 3266 | } |
| 3267 | |
Matthew Dempsky | 3efefd9 | 2016-03-30 14:56:08 -0700 | [diff] [blame] | 3268 | if t.IsStruct() && t.NumFields() <= 4 { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3269 | // Struct of four or fewer fields. |
| 3270 | // Inline comparisons. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3271 | var li *Node |
| 3272 | var ri *Node |
Matthew Dempsky | f6bca3f | 2016-03-17 01:32:18 -0700 | [diff] [blame] | 3273 | for _, t1 := range t.Fields().Slice() { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3274 | if isblanksym(t1.Sym) { |
| 3275 | continue |
| 3276 | } |
Ian Lance Taylor | 5f525ca | 2016-03-18 16:52:30 -0700 | [diff] [blame] | 3277 | li = NodSym(OXDOT, l, t1.Sym) |
| 3278 | ri = NodSym(OXDOT, r, t1.Sym) |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 3279 | a = Nod(n.Op, li, ri) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3280 | if expr == nil { |
| 3281 | expr = a |
| 3282 | } else { |
| 3283 | expr = Nod(andor, expr, a) |
| 3284 | } |
| 3285 | } |
| 3286 | |
| 3287 | if expr == nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3288 | expr = Nodbool(n.Op == OEQ) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3289 | } |
Josh Bleecher Snyder | babc735 | 2016-03-23 16:43:17 -0700 | [diff] [blame] | 3290 | n = finishcompare(n, expr, init) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3291 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3292 | } |
| 3293 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 3294 | // Chose not to inline. Call equality function directly. |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3295 | var needsize int |
| 3296 | call := Nod(OCALL, eqfor(t, &needsize), nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3297 | |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 3298 | call.List.Append(l) |
| 3299 | call.List.Append(r) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3300 | if needsize != 0 { |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 3301 | call.List.Append(Nodintconst(t.Width)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3302 | } |
| 3303 | r = call |
| 3304 | if n.Op != OEQ { |
| 3305 | r = Nod(ONOT, r, nil) |
| 3306 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3307 | |
Josh Bleecher Snyder | babc735 | 2016-03-23 16:43:17 -0700 | [diff] [blame] | 3308 | n = finishcompare(n, r, init) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3309 | return n |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3310 | } |
| 3311 | |
Josh Bleecher Snyder | babc735 | 2016-03-23 16:43:17 -0700 | [diff] [blame] | 3312 | // The result of finishcompare MUST be assigned back to n, e.g. |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3313 | // n.Left = finishcompare(n.Left, x, r, init) |
Josh Bleecher Snyder | babc735 | 2016-03-23 16:43:17 -0700 | [diff] [blame] | 3314 | func finishcompare(n, r *Node, init *Nodes) *Node { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3315 | // Use nn here to avoid passing r to typecheck. |
Josh Bleecher Snyder | babc735 | 2016-03-23 16:43:17 -0700 | [diff] [blame] | 3316 | nn := r |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3317 | nn = typecheck(nn, Erv) |
| 3318 | nn = walkexpr(nn, init) |
| 3319 | r = nn |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3320 | if r.Type != n.Type { |
| 3321 | r = Nod(OCONVNOP, r, nil) |
| 3322 | r.Type = n.Type |
| 3323 | r.Typecheck = 1 |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3324 | nn = r |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3325 | } |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3326 | return nn |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3327 | } |
| 3328 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3329 | func samecheap(a *Node, b *Node) bool { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3330 | var ar *Node |
| 3331 | var br *Node |
| 3332 | for a != nil && b != nil && a.Op == b.Op { |
| 3333 | switch a.Op { |
| 3334 | default: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3335 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3336 | |
| 3337 | case ONAME: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3338 | return a == b |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3339 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 3340 | case ODOT, ODOTPTR: |
Ian Lance Taylor | 5f525ca | 2016-03-18 16:52:30 -0700 | [diff] [blame] | 3341 | if a.Sym != b.Sym { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3342 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3343 | } |
| 3344 | |
| 3345 | case OINDEX: |
| 3346 | ar = a.Right |
| 3347 | br = b.Right |
Matthew Dempsky | d325387 | 2016-03-20 13:55:42 -0700 | [diff] [blame] | 3348 | if !Isconst(ar, CTINT) || !Isconst(br, CTINT) || ar.Val().U.(*Mpint).Cmp(br.Val().U.(*Mpint)) != 0 { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3349 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | a = a.Left |
| 3354 | b = b.Left |
| 3355 | } |
| 3356 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3357 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3358 | } |
| 3359 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3360 | // The result of walkrotate MUST be assigned back to n, e.g. |
| 3361 | // n.Left = walkrotate(n.Left) |
| 3362 | func walkrotate(n *Node) *Node { |
Cherry Zhang | d99cee7 | 2016-08-10 13:24:03 -0400 | [diff] [blame] | 3363 | //TODO: enable LROT on ARM64 once the old backend is gone |
Matthew Dempsky | c6e11fe | 2016-04-06 12:01:40 -0700 | [diff] [blame] | 3364 | if Thearch.LinkArch.InFamily(sys.MIPS64, sys.ARM64, sys.PPC64) { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3365 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3366 | } |
| 3367 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3368 | // Want << | >> or >> | << or << ^ >> or >> ^ << on unsigned value. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3369 | l := n.Left |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3370 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3371 | r := n.Right |
Matthew Dempsky | e76fc1b | 2016-03-30 15:09:25 -0700 | [diff] [blame] | 3372 | if (n.Op != OOR && n.Op != OXOR) || (l.Op != OLSH && l.Op != ORSH) || (r.Op != OLSH && r.Op != ORSH) || n.Type == nil || n.Type.IsSigned() || l.Op == r.Op { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3373 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | // Want same, side effect-free expression on lhs of both shifts. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3377 | if !samecheap(l.Left, r.Left) { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3378 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | // Constants adding to width? |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3382 | w := int(l.Type.Width * 8) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3383 | |
Michael Munday | 613ba6c | 2016-04-12 12:26:17 -0400 | [diff] [blame] | 3384 | if Thearch.LinkArch.Family == sys.S390X && w != 32 && w != 64 { |
| 3385 | // only supports 32-bit and 64-bit rotates |
| 3386 | return n |
| 3387 | } |
| 3388 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3389 | if Smallintconst(l.Right) && Smallintconst(r.Right) { |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 3390 | sl := int(l.Right.Int64()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3391 | if sl >= 0 { |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 3392 | sr := int(r.Right.Int64()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3393 | if sr >= 0 && sl+sr == w { |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3394 | // Rewrite left shift half to left rotate. |
| 3395 | if l.Op == OLSH { |
| 3396 | n = l |
| 3397 | } else { |
| 3398 | n = r |
| 3399 | } |
| 3400 | n.Op = OLROT |
| 3401 | |
| 3402 | // Remove rotate 0 and rotate w. |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 3403 | s := int(n.Right.Int64()) |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3404 | |
| 3405 | if s == 0 || s == w { |
| 3406 | n = n.Left |
| 3407 | } |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3408 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3409 | } |
| 3410 | } |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3411 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | // TODO: Could allow s and 32-s if s is bounded (maybe s&31 and 32-s&31). |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3415 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3416 | } |
| 3417 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 3418 | // walkmul rewrites integer multiplication by powers of two as shifts. |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3419 | // The result of walkmul MUST be assigned back to n, e.g. |
| 3420 | // n.Left = walkmul(n.Left, init) |
| 3421 | func walkmul(n *Node, init *Nodes) *Node { |
Matthew Dempsky | e76fc1b | 2016-03-30 15:09:25 -0700 | [diff] [blame] | 3422 | if !n.Type.IsInteger() { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3423 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3424 | } |
| 3425 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3426 | var nr *Node |
| 3427 | var nl *Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3428 | if n.Right.Op == OLITERAL { |
| 3429 | nl = n.Left |
| 3430 | nr = n.Right |
| 3431 | } else if n.Left.Op == OLITERAL { |
| 3432 | nl = n.Right |
| 3433 | nr = n.Left |
| 3434 | } else { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3435 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3436 | } |
| 3437 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3438 | neg := 0 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3439 | |
| 3440 | // x*0 is 0 (and side effects of x). |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3441 | var pow int |
| 3442 | var w int |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 3443 | if nr.Int64() == 0 { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3444 | cheapexpr(nl, init) |
| 3445 | Nodconst(n, n.Type, 0) |
| 3446 | goto ret |
| 3447 | } |
| 3448 | |
| 3449 | // nr is a constant. |
| 3450 | pow = powtwo(nr) |
| 3451 | |
| 3452 | if pow < 0 { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3453 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3454 | } |
| 3455 | if pow >= 1000 { |
| 3456 | // negative power of 2, like -16 |
| 3457 | neg = 1 |
| 3458 | |
| 3459 | pow -= 1000 |
| 3460 | } |
| 3461 | |
| 3462 | w = int(nl.Type.Width * 8) |
| 3463 | if pow+1 >= w { // too big, shouldn't happen |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3464 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3465 | } |
| 3466 | |
| 3467 | nl = cheapexpr(nl, init) |
| 3468 | |
| 3469 | if pow == 0 { |
| 3470 | // x*1 is x |
| 3471 | n = nl |
| 3472 | |
| 3473 | goto ret |
| 3474 | } |
| 3475 | |
| 3476 | n = Nod(OLSH, nl, Nodintconst(int64(pow))) |
| 3477 | |
| 3478 | ret: |
| 3479 | if neg != 0 { |
| 3480 | n = Nod(OMINUS, n, nil) |
| 3481 | } |
| 3482 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3483 | n = typecheck(n, Erv) |
| 3484 | n = walkexpr(n, init) |
| 3485 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3486 | } |
| 3487 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 3488 | // walkdiv rewrites division by a constant as less expensive |
| 3489 | // operations. |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3490 | // The result of walkdiv MUST be assigned back to n, e.g. |
| 3491 | // n.Left = walkdiv(n.Left, init) |
| 3492 | func walkdiv(n *Node, init *Nodes) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3493 | // if >= 0, nr is 1<<pow // 1 if nr is negative. |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3494 | |
| 3495 | // TODO(minux) |
Zhongwei Yao | 74a9bad | 2016-04-25 11:08:38 +0800 | [diff] [blame] | 3496 | if Thearch.LinkArch.InFamily(sys.MIPS64, sys.PPC64) { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3497 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3498 | } |
| 3499 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3500 | if n.Right.Op != OLITERAL { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3501 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3502 | } |
| 3503 | |
| 3504 | // nr is a constant. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3505 | nl := cheapexpr(n.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3506 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3507 | nr := n.Right |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3508 | |
| 3509 | // special cases of mod/div |
| 3510 | // by a constant |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3511 | w := int(nl.Type.Width * 8) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3512 | |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 3513 | s := 0 // 1 if nr is negative. |
| 3514 | pow := powtwo(nr) // if >= 0, nr is 1<<pow |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3515 | if pow >= 1000 { |
| 3516 | // negative power of 2 |
| 3517 | s = 1 |
| 3518 | |
| 3519 | pow -= 1000 |
| 3520 | } |
| 3521 | |
| 3522 | if pow+1 >= w { |
| 3523 | // divisor too large. |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3524 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | if pow < 0 { |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3528 | // try to do division by multiply by (2^w)/d |
| 3529 | // see hacker's delight chapter 10 |
| 3530 | // TODO: support 64-bit magic multiply here. |
| 3531 | var m Magic |
| 3532 | m.W = w |
| 3533 | |
Matthew Dempsky | e76fc1b | 2016-03-30 15:09:25 -0700 | [diff] [blame] | 3534 | if nl.Type.IsSigned() { |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 3535 | m.Sd = nr.Int64() |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3536 | Smagic(&m) |
| 3537 | } else { |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 3538 | m.Ud = uint64(nr.Int64()) |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3539 | Umagic(&m) |
| 3540 | } |
| 3541 | |
| 3542 | if m.Bad != 0 { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3543 | return n |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3544 | } |
| 3545 | |
| 3546 | // We have a quick division method so use it |
| 3547 | // for modulo too. |
| 3548 | if n.Op == OMOD { |
| 3549 | // rewrite as A%B = A - (A/B*B). |
| 3550 | n1 := Nod(ODIV, nl, nr) |
| 3551 | |
| 3552 | n2 := Nod(OMUL, n1, nr) |
| 3553 | n = Nod(OSUB, nl, n2) |
| 3554 | goto ret |
| 3555 | } |
| 3556 | |
| 3557 | switch Simtype[nl.Type.Etype] { |
| 3558 | default: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3559 | return n |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3560 | |
| 3561 | // n1 = nl * magic >> w (HMUL) |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 3562 | case TUINT8, TUINT16, TUINT32: |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3563 | var nc Node |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3564 | |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3565 | Nodconst(&nc, nl.Type, int64(m.Um)) |
| 3566 | n1 := Nod(OHMUL, nl, &nc) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3567 | n1 = typecheck(n1, Erv) |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3568 | if m.Ua != 0 { |
| 3569 | // Select a Go type with (at least) twice the width. |
| 3570 | var twide *Type |
| 3571 | switch Simtype[nl.Type.Etype] { |
| 3572 | default: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3573 | return n |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3574 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 3575 | case TUINT8, TUINT16: |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3576 | twide = Types[TUINT32] |
| 3577 | |
| 3578 | case TUINT32: |
| 3579 | twide = Types[TUINT64] |
| 3580 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 3581 | case TINT8, TINT16: |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3582 | twide = Types[TINT32] |
| 3583 | |
| 3584 | case TINT32: |
| 3585 | twide = Types[TINT64] |
| 3586 | } |
| 3587 | |
| 3588 | // add numerator (might overflow). |
| 3589 | // n2 = (n1 + nl) |
| 3590 | n2 := Nod(OADD, conv(n1, twide), conv(nl, twide)) |
| 3591 | |
| 3592 | // shift by m.s |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3593 | var nc Node |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3594 | |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3595 | Nodconst(&nc, Types[TUINT], int64(m.S)) |
| 3596 | n = conv(Nod(ORSH, n2, &nc), nl.Type) |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3597 | } else { |
| 3598 | // n = n1 >> m.s |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3599 | var nc Node |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3600 | |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3601 | Nodconst(&nc, Types[TUINT], int64(m.S)) |
| 3602 | n = Nod(ORSH, n1, &nc) |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | // n1 = nl * magic >> w |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 3606 | case TINT8, TINT16, TINT32: |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3607 | var nc Node |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3608 | |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3609 | Nodconst(&nc, nl.Type, m.Sm) |
| 3610 | n1 := Nod(OHMUL, nl, &nc) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3611 | n1 = typecheck(n1, Erv) |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3612 | if m.Sm < 0 { |
| 3613 | // add the numerator. |
| 3614 | n1 = Nod(OADD, n1, nl) |
| 3615 | } |
| 3616 | |
| 3617 | // shift by m.s |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3618 | var ns Node |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3619 | |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3620 | Nodconst(&ns, Types[TUINT], int64(m.S)) |
| 3621 | n2 := conv(Nod(ORSH, n1, &ns), nl.Type) |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3622 | |
| 3623 | // add 1 iff n1 is negative. |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3624 | var nneg Node |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3625 | |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3626 | Nodconst(&nneg, Types[TUINT], int64(w)-1) |
| 3627 | n3 := Nod(ORSH, nl, &nneg) // n4 = -1 iff n1 is negative. |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3628 | n = Nod(OSUB, n2, n3) |
| 3629 | |
| 3630 | // apply sign. |
| 3631 | if m.Sd < 0 { |
| 3632 | n = Nod(OMINUS, n, nil) |
| 3633 | } |
| 3634 | } |
| 3635 | |
| 3636 | goto ret |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3637 | } |
| 3638 | |
| 3639 | switch pow { |
| 3640 | case 0: |
| 3641 | if n.Op == OMOD { |
| 3642 | // nl % 1 is zero. |
| 3643 | Nodconst(n, n.Type, 0) |
| 3644 | } else if s != 0 { |
| 3645 | // divide by -1 |
| 3646 | n.Op = OMINUS |
| 3647 | |
| 3648 | n.Right = nil |
| 3649 | } else { |
| 3650 | // divide by 1 |
| 3651 | n = nl |
| 3652 | } |
| 3653 | |
| 3654 | default: |
Matthew Dempsky | e76fc1b | 2016-03-30 15:09:25 -0700 | [diff] [blame] | 3655 | if n.Type.IsSigned() { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3656 | if n.Op == OMOD { |
| 3657 | // signed modulo 2^pow is like ANDing |
| 3658 | // with the last pow bits, but if nl < 0, |
| 3659 | // nl & (2^pow-1) is (nl+1)%2^pow - 1. |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3660 | var nc Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3661 | |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3662 | Nodconst(&nc, Types[Simtype[TUINT]], int64(w)-1) |
| 3663 | n1 := Nod(ORSH, nl, &nc) // n1 = -1 iff nl < 0. |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3664 | if pow == 1 { |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3665 | n1 = typecheck(n1, Erv) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3666 | n1 = cheapexpr(n1, init) |
| 3667 | |
| 3668 | // n = (nl+ε)&1 -ε where ε=1 iff nl<0. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3669 | n2 := Nod(OSUB, nl, n1) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3670 | |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3671 | var nc Node |
| 3672 | Nodconst(&nc, nl.Type, 1) |
| 3673 | n3 := Nod(OAND, n2, &nc) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3674 | n = Nod(OADD, n3, n1) |
| 3675 | } else { |
| 3676 | // n = (nl+ε)&(nr-1) - ε where ε=2^pow-1 iff nl<0. |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3677 | var nc Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3678 | |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3679 | Nodconst(&nc, nl.Type, (1<<uint(pow))-1) |
| 3680 | n2 := Nod(OAND, n1, &nc) // n2 = 2^pow-1 iff nl<0. |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3681 | n2 = typecheck(n2, Erv) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3682 | n2 = cheapexpr(n2, init) |
| 3683 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3684 | n3 := Nod(OADD, nl, n2) |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3685 | n4 := Nod(OAND, n3, &nc) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3686 | n = Nod(OSUB, n4, n2) |
| 3687 | } |
| 3688 | |
| 3689 | break |
| 3690 | } else { |
| 3691 | // arithmetic right shift does not give the correct rounding. |
| 3692 | // if nl >= 0, nl >> n == nl / nr |
| 3693 | // if nl < 0, we want to add 2^n-1 first. |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3694 | var nc Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3695 | |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3696 | Nodconst(&nc, Types[Simtype[TUINT]], int64(w)-1) |
| 3697 | n1 := Nod(ORSH, nl, &nc) // n1 = -1 iff nl < 0. |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3698 | if pow == 1 { |
| 3699 | // nl+1 is nl-(-1) |
| 3700 | n.Left = Nod(OSUB, nl, n1) |
| 3701 | } else { |
| 3702 | // Do a logical right right on -1 to keep pow bits. |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3703 | var nc Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3704 | |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3705 | Nodconst(&nc, Types[Simtype[TUINT]], int64(w)-int64(pow)) |
| 3706 | n2 := Nod(ORSH, conv(n1, tounsigned(nl.Type)), &nc) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3707 | n.Left = Nod(OADD, nl, conv(n2, nl.Type)) |
| 3708 | } |
| 3709 | |
| 3710 | // n = (nl + 2^pow-1) >> pow |
| 3711 | n.Op = ORSH |
| 3712 | |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3713 | var n2 Node |
| 3714 | Nodconst(&n2, Types[Simtype[TUINT]], int64(pow)) |
| 3715 | n.Right = &n2 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3716 | n.Typecheck = 0 |
| 3717 | } |
| 3718 | |
| 3719 | if s != 0 { |
| 3720 | n = Nod(OMINUS, n, nil) |
| 3721 | } |
| 3722 | break |
| 3723 | } |
| 3724 | |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3725 | var nc Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3726 | if n.Op == OMOD { |
| 3727 | // n = nl & (nr-1) |
| 3728 | n.Op = OAND |
| 3729 | |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 3730 | Nodconst(&nc, nl.Type, nr.Int64()-1) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3731 | } else { |
| 3732 | // n = nl >> pow |
| 3733 | n.Op = ORSH |
| 3734 | |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3735 | Nodconst(&nc, Types[Simtype[TUINT]], int64(pow)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3736 | } |
| 3737 | |
| 3738 | n.Typecheck = 0 |
Dave Cheney | a4be24c | 2016-03-23 13:27:49 +1100 | [diff] [blame] | 3739 | n.Right = &nc |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3740 | } |
| 3741 | |
| 3742 | goto ret |
| 3743 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3744 | ret: |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 3745 | n = typecheck(n, Erv) |
| 3746 | n = walkexpr(n, init) |
| 3747 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | // return 1 if integer n must be in range [0, max), 0 otherwise |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3751 | func bounded(n *Node, max int64) bool { |
Matthew Dempsky | e76fc1b | 2016-03-30 15:09:25 -0700 | [diff] [blame] | 3752 | if n.Type == nil || !n.Type.IsInteger() { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3753 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3754 | } |
| 3755 | |
Matthew Dempsky | e76fc1b | 2016-03-30 15:09:25 -0700 | [diff] [blame] | 3756 | sign := n.Type.IsSigned() |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3757 | bits := int32(8 * n.Type.Width) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3758 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3759 | if Smallintconst(n) { |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 3760 | v := n.Int64() |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3761 | return 0 <= v && v < max |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3762 | } |
| 3763 | |
| 3764 | switch n.Op { |
| 3765 | case OAND: |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3766 | v := int64(-1) |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3767 | if Smallintconst(n.Left) { |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 3768 | v = n.Left.Int64() |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3769 | } else if Smallintconst(n.Right) { |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 3770 | v = n.Right.Int64() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3771 | } |
| 3772 | |
| 3773 | if 0 <= v && v < max { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3774 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | case OMOD: |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 3778 | if !sign && Smallintconst(n.Right) { |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 3779 | v := n.Right.Int64() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3780 | if 0 <= v && v <= max { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3781 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3782 | } |
| 3783 | } |
| 3784 | |
| 3785 | case ODIV: |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 3786 | if !sign && Smallintconst(n.Right) { |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 3787 | v := n.Right.Int64() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3788 | for bits > 0 && v >= 2 { |
| 3789 | bits-- |
| 3790 | v >>= 1 |
| 3791 | } |
| 3792 | } |
| 3793 | |
| 3794 | case ORSH: |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 3795 | if !sign && Smallintconst(n.Right) { |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 3796 | v := n.Right.Int64() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3797 | if v > int64(bits) { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3798 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3799 | } |
| 3800 | bits -= int32(v) |
| 3801 | } |
| 3802 | } |
| 3803 | |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 3804 | if !sign && bits <= 62 && 1<<uint(bits) <= max { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3805 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3806 | } |
| 3807 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3808 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3809 | } |
| 3810 | |
David Crawshaw | cc15840 | 2016-03-10 16:15:26 -0500 | [diff] [blame] | 3811 | // usemethod check interface method calls for uses of reflect.Type.Method. |
| 3812 | func usemethod(n *Node) { |
| 3813 | t := n.Left.Type |
| 3814 | |
| 3815 | // Looking for either of: |
| 3816 | // Method(int) reflect.Method |
| 3817 | // MethodByName(string) (reflect.Method, bool) |
| 3818 | // |
| 3819 | // TODO(crawshaw): improve precision of match by working out |
| 3820 | // how to check the method name. |
Matthew Dempsky | dbed1c6 | 2016-03-17 13:26:08 -0700 | [diff] [blame] | 3821 | if n := t.Params().NumFields(); n != 1 { |
David Crawshaw | cc15840 | 2016-03-10 16:15:26 -0500 | [diff] [blame] | 3822 | return |
| 3823 | } |
Matthew Dempsky | dbed1c6 | 2016-03-17 13:26:08 -0700 | [diff] [blame] | 3824 | if n := t.Results().NumFields(); n != 1 && n != 2 { |
David Crawshaw | cc15840 | 2016-03-10 16:15:26 -0500 | [diff] [blame] | 3825 | return |
| 3826 | } |
| 3827 | p0 := t.Params().Field(0) |
| 3828 | res0 := t.Results().Field(0) |
Matthew Dempsky | 2e93690 | 2016-03-14 01:20:49 -0700 | [diff] [blame] | 3829 | var res1 *Field |
Matthew Dempsky | dbed1c6 | 2016-03-17 13:26:08 -0700 | [diff] [blame] | 3830 | if t.Results().NumFields() == 2 { |
David Crawshaw | cc15840 | 2016-03-10 16:15:26 -0500 | [diff] [blame] | 3831 | res1 = t.Results().Field(1) |
| 3832 | } |
| 3833 | |
| 3834 | if res1 == nil { |
| 3835 | if p0.Type.Etype != TINT { |
| 3836 | return |
| 3837 | } |
| 3838 | } else { |
Matthew Dempsky | 3efefd9 | 2016-03-30 14:56:08 -0700 | [diff] [blame] | 3839 | if !p0.Type.IsString() { |
David Crawshaw | cc15840 | 2016-03-10 16:15:26 -0500 | [diff] [blame] | 3840 | return |
| 3841 | } |
Matthew Dempsky | 3efefd9 | 2016-03-30 14:56:08 -0700 | [diff] [blame] | 3842 | if !res1.Type.IsBoolean() { |
David Crawshaw | cc15840 | 2016-03-10 16:15:26 -0500 | [diff] [blame] | 3843 | return |
| 3844 | } |
| 3845 | } |
Matthew Dempsky | 2e93690 | 2016-03-14 01:20:49 -0700 | [diff] [blame] | 3846 | if Tconv(res0.Type, 0) != "reflect.Method" { |
David Crawshaw | cc15840 | 2016-03-10 16:15:26 -0500 | [diff] [blame] | 3847 | return |
| 3848 | } |
| 3849 | |
| 3850 | Curfn.Func.ReflectMethod = true |
| 3851 | } |
| 3852 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3853 | func usefield(n *Node) { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3854 | if obj.Fieldtrack_enabled == 0 { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3855 | return |
| 3856 | } |
| 3857 | |
| 3858 | switch n.Op { |
| 3859 | default: |
Dave Cheney | d3c79d3 | 2016-04-27 15:10:10 +1000 | [diff] [blame] | 3860 | Fatalf("usefield %v", n.Op) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3861 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 3862 | case ODOT, ODOTPTR: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3863 | break |
| 3864 | } |
Ian Lance Taylor | 5f525ca | 2016-03-18 16:52:30 -0700 | [diff] [blame] | 3865 | if n.Sym == nil { |
Keith Randall | 71d13a8 | 2016-03-02 20:54:41 -0800 | [diff] [blame] | 3866 | // No field name. This DOTPTR was built by the compiler for access |
| 3867 | // to runtime data structures. Ignore. |
| 3868 | return |
| 3869 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3870 | |
Russ Cox | 496ad0a | 2015-05-26 21:49:31 -0400 | [diff] [blame] | 3871 | t := n.Left.Type |
Matthew Dempsky | e76fc1b | 2016-03-30 15:09:25 -0700 | [diff] [blame] | 3872 | if t.IsPtr() { |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 3873 | t = t.Elem() |
Russ Cox | 496ad0a | 2015-05-26 21:49:31 -0400 | [diff] [blame] | 3874 | } |
Ian Lance Taylor | 5f525ca | 2016-03-18 16:52:30 -0700 | [diff] [blame] | 3875 | field := dotField[typeSym{t.Orig, n.Sym}] |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3876 | if field == nil { |
Ian Lance Taylor | 5f525ca | 2016-03-18 16:52:30 -0700 | [diff] [blame] | 3877 | Fatalf("usefield %v %v without paramfld", n.Left.Type, n.Sym) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3878 | } |
Matthew Dempsky | e48a295 | 2016-04-25 13:24:48 -0700 | [diff] [blame] | 3879 | if !strings.Contains(field.Note, "go:\"track\"") { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3880 | return |
| 3881 | } |
| 3882 | |
Matthew Dempsky | a2a4806 | 2016-03-10 16:15:44 -0800 | [diff] [blame] | 3883 | outer := n.Left.Type |
Matthew Dempsky | e76fc1b | 2016-03-30 15:09:25 -0700 | [diff] [blame] | 3884 | if outer.IsPtr() { |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 3885 | outer = outer.Elem() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3886 | } |
Matthew Dempsky | a2a4806 | 2016-03-10 16:15:44 -0800 | [diff] [blame] | 3887 | if outer.Sym == nil { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3888 | Yyerror("tracked field must be in named struct type") |
| 3889 | } |
| 3890 | if !exportname(field.Sym.Name) { |
| 3891 | Yyerror("tracked field must be exported (upper case)") |
| 3892 | } |
| 3893 | |
Matthew Dempsky | a2a4806 | 2016-03-10 16:15:44 -0800 | [diff] [blame] | 3894 | sym := tracksym(outer, field) |
| 3895 | if Curfn.Func.FieldTrack == nil { |
| 3896 | Curfn.Func.FieldTrack = make(map[*Sym]struct{}) |
| 3897 | } |
| 3898 | Curfn.Func.FieldTrack[sym] = struct{}{} |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3899 | } |
| 3900 | |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 3901 | func candiscardlist(l Nodes) bool { |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 3902 | for _, n := range l.Slice() { |
| 3903 | if !candiscard(n) { |
Ian Lance Taylor | 1d5001a | 2016-02-27 14:31:33 -0800 | [diff] [blame] | 3904 | return false |
| 3905 | } |
| 3906 | } |
| 3907 | return true |
| 3908 | } |
| 3909 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3910 | func candiscard(n *Node) bool { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3911 | if n == nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3912 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3913 | } |
| 3914 | |
| 3915 | switch n.Op { |
| 3916 | default: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3917 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3918 | |
| 3919 | // Discardable as long as the subpieces are. |
| 3920 | case ONAME, |
| 3921 | ONONAME, |
| 3922 | OTYPE, |
| 3923 | OPACK, |
| 3924 | OLITERAL, |
| 3925 | OADD, |
| 3926 | OSUB, |
| 3927 | OOR, |
| 3928 | OXOR, |
| 3929 | OADDSTR, |
| 3930 | OADDR, |
| 3931 | OANDAND, |
| 3932 | OARRAYBYTESTR, |
| 3933 | OARRAYRUNESTR, |
| 3934 | OSTRARRAYBYTE, |
| 3935 | OSTRARRAYRUNE, |
| 3936 | OCAP, |
| 3937 | OCMPIFACE, |
| 3938 | OCMPSTR, |
| 3939 | OCOMPLIT, |
| 3940 | OMAPLIT, |
| 3941 | OSTRUCTLIT, |
| 3942 | OARRAYLIT, |
| 3943 | OPTRLIT, |
| 3944 | OCONV, |
| 3945 | OCONVIFACE, |
| 3946 | OCONVNOP, |
| 3947 | ODOT, |
| 3948 | OEQ, |
| 3949 | ONE, |
| 3950 | OLT, |
| 3951 | OLE, |
| 3952 | OGT, |
| 3953 | OGE, |
| 3954 | OKEY, |
| 3955 | OLEN, |
| 3956 | OMUL, |
| 3957 | OLSH, |
| 3958 | ORSH, |
| 3959 | OAND, |
| 3960 | OANDNOT, |
| 3961 | ONEW, |
| 3962 | ONOT, |
| 3963 | OCOM, |
| 3964 | OPLUS, |
| 3965 | OMINUS, |
| 3966 | OOROR, |
| 3967 | OPAREN, |
| 3968 | ORUNESTR, |
| 3969 | OREAL, |
| 3970 | OIMAG, |
| 3971 | OCOMPLEX: |
| 3972 | break |
| 3973 | |
| 3974 | // Discardable as long as we know it's not division by zero. |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 3975 | case ODIV, OMOD: |
Matthew Dempsky | d325387 | 2016-03-20 13:55:42 -0700 | [diff] [blame] | 3976 | if Isconst(n.Right, CTINT) && n.Right.Val().U.(*Mpint).CmpInt64(0) != 0 { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3977 | break |
| 3978 | } |
Matthew Dempsky | d325387 | 2016-03-20 13:55:42 -0700 | [diff] [blame] | 3979 | if Isconst(n.Right, CTFLT) && n.Right.Val().U.(*Mpflt).CmpFloat64(0) != 0 { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3980 | break |
| 3981 | } |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3982 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3983 | |
| 3984 | // Discardable as long as we know it won't fail because of a bad size. |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 3985 | case OMAKECHAN, OMAKEMAP: |
Matthew Dempsky | d325387 | 2016-03-20 13:55:42 -0700 | [diff] [blame] | 3986 | if Isconst(n.Left, CTINT) && n.Left.Val().U.(*Mpint).CmpInt64(0) == 0 { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3987 | break |
| 3988 | } |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3989 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3990 | |
| 3991 | // Difficult to tell what sizes are okay. |
| 3992 | case OMAKESLICE: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3993 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3994 | } |
| 3995 | |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 3996 | if !candiscard(n.Left) || !candiscard(n.Right) || !candiscardlist(n.Ninit) || !candiscardlist(n.Nbody) || !candiscardlist(n.List) || !candiscardlist(n.Rlist) { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3997 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3998 | } |
| 3999 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 4000 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 4001 | } |
| 4002 | |
| 4003 | // rewrite |
| 4004 | // print(x, y, z) |
| 4005 | // into |
| 4006 | // func(a1, a2, a3) { |
| 4007 | // print(a1, a2, a3) |
| 4008 | // }(x, y, z) |
| 4009 | // and same for println. |
| 4010 | |
| 4011 | var walkprintfunc_prgen int |
| 4012 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 4013 | // The result of walkprintfunc MUST be assigned back to n, e.g. |
| 4014 | // n.Left = walkprintfunc(n.Left, init) |
| 4015 | func walkprintfunc(n *Node, init *Nodes) *Node { |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 4016 | if n.Ninit.Len() != 0 { |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 4017 | walkstmtlist(n.Ninit.Slice()) |
| 4018 | init.AppendNodes(&n.Ninit) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 4019 | } |
| 4020 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 4021 | t := Nod(OTFUNC, nil, nil) |
| 4022 | num := 0 |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 4023 | var printargs []*Node |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 4024 | var a *Node |
| 4025 | var buf string |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 4026 | for _, n1 := range n.List.Slice() { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 4027 | buf = fmt.Sprintf("a%d", num) |
| 4028 | num++ |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 4029 | a = Nod(ODCLFIELD, newname(Lookup(buf)), typenod(n1.Type)) |
| 4030 | t.List.Append(a) |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 4031 | printargs = append(printargs, a.Left) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 4032 | } |
| 4033 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 4034 | fn := Nod(ODCLFUNC, nil, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 4035 | walkprintfunc_prgen++ |
| 4036 | buf = fmt.Sprintf("print·%d", walkprintfunc_prgen) |
Russ Cox | bd4fff6 | 2015-05-27 10:42:55 -0400 | [diff] [blame] | 4037 | fn.Func.Nname = newname(Lookup(buf)) |
| 4038 | fn.Func.Nname.Name.Defn = fn |
| 4039 | fn.Func.Nname.Name.Param.Ntype = t |
| 4040 | declare(fn.Func.Nname, PFUNC) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 4041 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 4042 | oldfn := Curfn |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 4043 | Curfn = nil |
| 4044 | funchdr(fn) |
| 4045 | |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 4046 | a = Nod(n.Op, nil, nil) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 4047 | a.List.Set(printargs) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 4048 | a = typecheck(a, Etop) |
| 4049 | a = walkstmt(a) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 4050 | |
Ian Lance Taylor | c63dbd8 | 2016-03-10 10:13:42 -0800 | [diff] [blame] | 4051 | fn.Nbody.Set1(a) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 4052 | |
| 4053 | funcbody(fn) |
| 4054 | |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 4055 | fn = typecheck(fn, Etop) |
Josh Bleecher Snyder | ec7c494 | 2016-03-19 17:02:01 -0700 | [diff] [blame] | 4056 | typecheckslice(fn.Nbody.Slice(), Etop) |
Ian Lance Taylor | f444b8a | 2016-03-09 20:29:21 -0800 | [diff] [blame] | 4057 | xtop = append(xtop, fn) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 4058 | Curfn = oldfn |
| 4059 | |
| 4060 | a = Nod(OCALL, nil, nil) |
Russ Cox | bd4fff6 | 2015-05-27 10:42:55 -0400 | [diff] [blame] | 4061 | a.Left = fn.Func.Nname |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 4062 | a.List.Set(n.List.Slice()) |
Josh Bleecher Snyder | 34699bc | 2016-03-20 08:03:31 -0700 | [diff] [blame] | 4063 | a = typecheck(a, Etop) |
| 4064 | a = walkexpr(a, init) |
| 4065 | return a |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 4066 | } |