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