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. |
| 660 | if n.Left.Type.Outtuple == 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 | d20b92e | 2016-03-09 19:32:10 -0800 | [diff] [blame] | 1745 | Yyerror("ascompatet: assignment count mismatch: %d = %d", nl.Len(), structcount(nr)) |
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 | |
| 2025 | typechecklist(calls, Etop) |
| 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 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2149 | // Otherwise, be conservative and use write barrier. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2150 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | // TODO(rsc): Perhaps componentgen should run before this. |
| 2154 | |
Ian Lance Taylor | 188e3d2 | 2016-02-26 14:28:48 -0800 | [diff] [blame] | 2155 | func applywritebarrier(n *Node) *Node { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2156 | if n.Left != nil && n.Right != nil && needwritebarrier(n.Left, n.Right) { |
Russ Cox | 972a478 | 2015-05-21 15:00:06 -0400 | [diff] [blame] | 2157 | if Debug_wb > 1 { |
Robert Griesemer | b83f397 | 2016-03-02 11:01:25 -0800 | [diff] [blame] | 2158 | Warnl(n.Lineno, "marking %v for barrier", Nconv(n.Left, 0)) |
Russ Cox | 0ad4f8b | 2015-04-17 00:25:10 -0400 | [diff] [blame] | 2159 | } |
Russ Cox | 972a478 | 2015-05-21 15:00:06 -0400 | [diff] [blame] | 2160 | n.Op = OASWB |
| 2161 | return n |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2162 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2163 | return n |
| 2164 | } |
| 2165 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2166 | func convas(n *Node, init *Nodes) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2167 | if n.Op != OAS { |
Matthew Dempsky | c3dfad5 | 2016-03-07 08:23:55 -0800 | [diff] [blame] | 2168 | Fatalf("convas: not OAS %v", Oconv(n.Op, 0)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | n.Typecheck = 1 |
| 2172 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2173 | var lt *Type |
| 2174 | var rt *Type |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2175 | if n.Left == nil || n.Right == nil { |
| 2176 | goto out |
| 2177 | } |
| 2178 | |
| 2179 | lt = n.Left.Type |
| 2180 | rt = n.Right.Type |
| 2181 | if lt == nil || rt == nil { |
| 2182 | goto out |
| 2183 | } |
| 2184 | |
| 2185 | if isblank(n.Left) { |
| 2186 | defaultlit(&n.Right, nil) |
| 2187 | goto out |
| 2188 | } |
| 2189 | |
| 2190 | if n.Left.Op == OINDEXMAP { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2191 | map_ := n.Left.Left |
| 2192 | key := n.Left.Right |
| 2193 | val := n.Right |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2194 | walkexpr(&map_, init) |
| 2195 | walkexpr(&key, init) |
| 2196 | walkexpr(&val, init) |
| 2197 | |
| 2198 | // orderexpr made sure key and val are addressable. |
| 2199 | key = Nod(OADDR, key, nil) |
| 2200 | |
| 2201 | val = Nod(OADDR, val, nil) |
| 2202 | n = mkcall1(mapfn("mapassign1", map_.Type), nil, init, typename(map_.Type), map_, key, val) |
| 2203 | goto out |
| 2204 | } |
| 2205 | |
| 2206 | if !Eqtype(lt, rt) { |
| 2207 | n.Right = assignconv(n.Right, lt, "assignment") |
| 2208 | walkexpr(&n.Right, init) |
| 2209 | } |
| 2210 | |
| 2211 | out: |
| 2212 | ullmancalc(n) |
| 2213 | return n |
| 2214 | } |
| 2215 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2216 | // from ascompat[te] |
| 2217 | // evaluating actual function arguments. |
| 2218 | // f(a,b) |
| 2219 | // if there is exactly one function expr, |
| 2220 | // then it is done first. otherwise must |
| 2221 | // make temp variables |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2222 | func reorder1(all []*Node) []*Node { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2223 | c := 0 // function calls |
| 2224 | t := 0 // total parameters |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2225 | |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2226 | for _, n := range all { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2227 | t++ |
| 2228 | ullmancalc(n) |
| 2229 | if n.Ullman >= UINF { |
| 2230 | c++ |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | if c == 0 || t == 1 { |
| 2235 | return all |
| 2236 | } |
| 2237 | |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2238 | var g []*Node // fncalls assigned to tempnames |
| 2239 | var f *Node // last fncall assigned to stack |
| 2240 | var r []*Node // non fncalls and tempnames assigned to stack |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2241 | d := 0 |
| 2242 | var a *Node |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2243 | for _, n := range all { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2244 | if n.Ullman < UINF { |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2245 | r = append(r, n) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2246 | continue |
| 2247 | } |
| 2248 | |
| 2249 | d++ |
| 2250 | if d == c { |
| 2251 | f = n |
| 2252 | continue |
| 2253 | } |
| 2254 | |
| 2255 | // make assignment of fncall to tempname |
| 2256 | a = temp(n.Right.Type) |
| 2257 | |
| 2258 | a = Nod(OAS, a, n.Right) |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2259 | g = append(g, a) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2260 | |
| 2261 | // put normal arg assignment on list |
| 2262 | // with fncall replaced by tempname |
| 2263 | n.Right = a.Left |
| 2264 | |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2265 | r = append(r, n) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | if f != nil { |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2269 | g = append(g, f) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2270 | } |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 2271 | return append(g, r...) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2272 | } |
| 2273 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2274 | // from ascompat[ee] |
| 2275 | // a,b = c,d |
| 2276 | // simultaneous assignment. there cannot |
| 2277 | // be later use of an earlier lvalue. |
| 2278 | // |
| 2279 | // function calls have been removed. |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2280 | func reorder3(all []*Node) []*Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2281 | var l *Node |
| 2282 | |
| 2283 | // If a needed expression may be affected by an |
| 2284 | // earlier assignment, make an early copy of that |
| 2285 | // expression and use the copy instead. |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2286 | var early []*Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2287 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2288 | var mapinit Nodes |
| 2289 | for i, n := range all { |
| 2290 | l = n.Left |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2291 | |
| 2292 | // Save subexpressions needed on left side. |
| 2293 | // Drill through non-dereferences. |
| 2294 | for { |
| 2295 | if l.Op == ODOT || l.Op == OPAREN { |
| 2296 | l = l.Left |
| 2297 | continue |
| 2298 | } |
| 2299 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2300 | if l.Op == OINDEX && Isfixedarray(l.Left.Type) { |
Josh Bleecher Snyder | bcce5bd | 2015-06-10 15:31:53 -0700 | [diff] [blame] | 2301 | reorder3save(&l.Right, all, i, &early) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2302 | l = l.Left |
| 2303 | continue |
| 2304 | } |
| 2305 | |
| 2306 | break |
| 2307 | } |
| 2308 | |
| 2309 | switch l.Op { |
| 2310 | default: |
Matthew Dempsky | 6314202 | 2016-03-15 13:06:58 -0700 | [diff] [blame] | 2311 | Fatalf("reorder3 unexpected lvalue %v", Oconv(l.Op, FmtSharp)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2312 | |
| 2313 | case ONAME: |
| 2314 | break |
| 2315 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 2316 | case OINDEX, OINDEXMAP: |
Josh Bleecher Snyder | bcce5bd | 2015-06-10 15:31:53 -0700 | [diff] [blame] | 2317 | reorder3save(&l.Left, all, i, &early) |
| 2318 | reorder3save(&l.Right, all, i, &early) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2319 | if l.Op == OINDEXMAP { |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2320 | all[i] = convas(all[i], &mapinit) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2321 | } |
| 2322 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 2323 | case OIND, ODOTPTR: |
Josh Bleecher Snyder | bcce5bd | 2015-06-10 15:31:53 -0700 | [diff] [blame] | 2324 | reorder3save(&l.Left, all, i, &early) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | // Save expression on right side. |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2328 | reorder3save(&all[i].Right, all, i, &early) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2329 | } |
| 2330 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2331 | early = append(mapinit.Slice(), early...) |
| 2332 | return append(early, all...) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2333 | } |
| 2334 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2335 | // if the evaluation of *np would be affected by the |
Josh Bleecher Snyder | bcce5bd | 2015-06-10 15:31:53 -0700 | [diff] [blame] | 2336 | // assignments in all up to but not including the ith assignment, |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2337 | // copy into a temporary during *early and |
| 2338 | // replace *np with that temp. |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2339 | func reorder3save(np **Node, all []*Node, i int, early *[]*Node) { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2340 | n := *np |
Josh Bleecher Snyder | bcce5bd | 2015-06-10 15:31:53 -0700 | [diff] [blame] | 2341 | if !aliased(n, all, i) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2342 | return |
| 2343 | } |
| 2344 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2345 | q := temp(n.Type) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2346 | q = Nod(OAS, q, n) |
| 2347 | typecheck(&q, Etop) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2348 | *early = append(*early, q) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2349 | *np = q.Left |
| 2350 | } |
| 2351 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2352 | // what's the outer value that a write to n affects? |
| 2353 | // outer value means containing struct or array. |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2354 | func outervalue(n *Node) *Node { |
| 2355 | for { |
| 2356 | if n.Op == OXDOT { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 2357 | Fatalf("OXDOT in walk") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2358 | } |
| 2359 | if n.Op == ODOT || n.Op == OPAREN || n.Op == OCONVNOP { |
| 2360 | n = n.Left |
| 2361 | continue |
| 2362 | } |
| 2363 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2364 | if n.Op == OINDEX && Isfixedarray(n.Left.Type) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2365 | n = n.Left |
| 2366 | continue |
| 2367 | } |
| 2368 | |
| 2369 | break |
| 2370 | } |
| 2371 | |
| 2372 | return n |
| 2373 | } |
| 2374 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2375 | // Is it possible that the computation of n might be |
Josh Bleecher Snyder | bcce5bd | 2015-06-10 15:31:53 -0700 | [diff] [blame] | 2376 | // 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] | 2377 | func aliased(n *Node, all []*Node, i int) bool { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2378 | if n == nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2379 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | // Look for obvious aliasing: a variable being assigned |
| 2383 | // during the all list and appearing in n. |
| 2384 | // Also record whether there are any writes to main memory. |
| 2385 | // Also record whether there are any writes to variables |
| 2386 | // whose addresses have been taken. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2387 | memwrite := 0 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2388 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2389 | varwrite := 0 |
| 2390 | var a *Node |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2391 | for _, an := range all[:i] { |
| 2392 | a = outervalue(an.Left) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2393 | if a.Op != ONAME { |
| 2394 | memwrite = 1 |
| 2395 | continue |
| 2396 | } |
| 2397 | |
| 2398 | switch n.Class { |
| 2399 | default: |
| 2400 | varwrite = 1 |
| 2401 | continue |
| 2402 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 2403 | case PAUTO, PPARAM, PPARAMOUT: |
Dave Cheney | 7885de5 | 2015-03-05 18:20:54 +1100 | [diff] [blame] | 2404 | if n.Addrtaken { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2405 | varwrite = 1 |
| 2406 | continue |
| 2407 | } |
| 2408 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2409 | if vmatch2(a, n) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2410 | // Direct hit. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2411 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2412 | } |
| 2413 | } |
| 2414 | } |
| 2415 | |
| 2416 | // The variables being written do not appear in n. |
| 2417 | // However, n might refer to computed addresses |
| 2418 | // that are being written. |
| 2419 | |
| 2420 | // If no computed addresses are affected by the writes, no aliasing. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2421 | if memwrite == 0 && varwrite == 0 { |
| 2422 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | // If n does not refer to computed addresses |
| 2426 | // (that is, if n only refers to variables whose addresses |
| 2427 | // have not been taken), no aliasing. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2428 | if varexpr(n) { |
| 2429 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | // Otherwise, both the writes and n refer to computed memory addresses. |
| 2433 | // Assume that they might conflict. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2434 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2435 | } |
| 2436 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2437 | // does the evaluation of n only refer to variables |
| 2438 | // whose addresses have not been taken? |
| 2439 | // (and no other memory) |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2440 | func varexpr(n *Node) bool { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2441 | if n == nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2442 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | switch n.Op { |
| 2446 | case OLITERAL: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2447 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2448 | |
| 2449 | case ONAME: |
| 2450 | switch n.Class { |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 2451 | case PAUTO, PPARAM, PPARAMOUT: |
Dave Cheney | 7885de5 | 2015-03-05 18:20:54 +1100 | [diff] [blame] | 2452 | if !n.Addrtaken { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2453 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2454 | } |
| 2455 | } |
| 2456 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2457 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2458 | |
| 2459 | case OADD, |
| 2460 | OSUB, |
| 2461 | OOR, |
| 2462 | OXOR, |
| 2463 | OMUL, |
| 2464 | ODIV, |
| 2465 | OMOD, |
| 2466 | OLSH, |
| 2467 | ORSH, |
| 2468 | OAND, |
| 2469 | OANDNOT, |
| 2470 | OPLUS, |
| 2471 | OMINUS, |
| 2472 | OCOM, |
| 2473 | OPAREN, |
| 2474 | OANDAND, |
| 2475 | OOROR, |
| 2476 | ODOT, // but not ODOTPTR |
| 2477 | OCONV, |
| 2478 | OCONVNOP, |
| 2479 | OCONVIFACE, |
| 2480 | ODOTTYPE: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2481 | return varexpr(n.Left) && varexpr(n.Right) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | // Be conservative. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2485 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2486 | } |
| 2487 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2488 | // is the name l mentioned in r? |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2489 | func vmatch2(l *Node, r *Node) bool { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2490 | if r == nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2491 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2492 | } |
| 2493 | switch r.Op { |
| 2494 | // match each right given left |
| 2495 | case ONAME: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2496 | return l == r |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2497 | |
| 2498 | case OLITERAL: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2499 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2500 | } |
| 2501 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2502 | if vmatch2(l, r.Left) { |
| 2503 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2504 | } |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2505 | if vmatch2(l, r.Right) { |
| 2506 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2507 | } |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2508 | for _, n := range r.List.Slice() { |
| 2509 | if vmatch2(l, n) { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2510 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2511 | } |
| 2512 | } |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2513 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2514 | } |
| 2515 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2516 | // is any name mentioned in l also mentioned in r? |
| 2517 | // called by sinit.go |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2518 | func vmatch1(l *Node, r *Node) bool { |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 2519 | // isolate all left sides |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2520 | if l == nil || r == nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2521 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2522 | } |
| 2523 | switch l.Op { |
| 2524 | case ONAME: |
| 2525 | switch l.Class { |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 2526 | case PPARAM, PPARAMREF, PAUTO: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2527 | break |
| 2528 | |
| 2529 | // assignment to non-stack variable |
| 2530 | // must be delayed if right has function calls. |
| 2531 | default: |
| 2532 | if r.Ullman >= UINF { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2533 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2534 | } |
| 2535 | } |
| 2536 | |
| 2537 | return vmatch2(l, r) |
| 2538 | |
| 2539 | case OLITERAL: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2540 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2541 | } |
| 2542 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2543 | if vmatch1(l.Left, r) { |
| 2544 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2545 | } |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2546 | if vmatch1(l.Right, r) { |
| 2547 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2548 | } |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2549 | for _, n := range l.List.Slice() { |
| 2550 | if vmatch1(n, r) { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2551 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2552 | } |
| 2553 | } |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2554 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2555 | } |
| 2556 | |
Matthew Dempsky | 2339b13 | 2016-03-09 18:54:26 -0800 | [diff] [blame] | 2557 | // paramstoheap returns code to allocate memory for heap-escaped parameters |
| 2558 | // and to copy non-result prameters' values from the stack. |
| 2559 | // If out is true, then code is also produced to zero-initialize their |
| 2560 | // stack memory addresses. |
| 2561 | func paramstoheap(params *Type, out bool) []*Node { |
Ian Lance Taylor | 188e3d2 | 2016-02-26 14:28:48 -0800 | [diff] [blame] | 2562 | var nn []*Node |
Matthew Dempsky | f6bca3f | 2016-03-17 01:32:18 -0700 | [diff] [blame^] | 2563 | for _, t := range params.Fields().Slice() { |
Matthew Dempsky | 2339b13 | 2016-03-09 18:54:26 -0800 | [diff] [blame] | 2564 | v := t.Nname |
| 2565 | 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] | 2566 | v = nil |
| 2567 | } |
| 2568 | |
| 2569 | // For precise stacks, the garbage collector assumes results |
| 2570 | // are always live, so zero them always. |
Matthew Dempsky | 2339b13 | 2016-03-09 18:54:26 -0800 | [diff] [blame] | 2571 | if out { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2572 | // Defer might stop a panic and show the |
| 2573 | // return values as they exist at the time of panic. |
| 2574 | // Make sure to zero them on entry to the function. |
Keith Randall | 4fffd456 | 2016-02-29 13:31:48 -0800 | [diff] [blame] | 2575 | nn = append(nn, Nod(OAS, nodarg(t, -1), nil)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2576 | } |
| 2577 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2578 | if v == nil || v.Class&PHEAP == 0 { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2579 | continue |
| 2580 | } |
| 2581 | |
| 2582 | // generate allocation & copying code |
| 2583 | if compiling_runtime != 0 { |
Russ Cox | 17228f4 | 2015-04-17 12:03:22 -0400 | [diff] [blame] | 2584 | Yyerror("%v escapes to heap, not allowed in runtime.", v) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2585 | } |
Russ Cox | 60e5f5b | 2015-05-26 23:05:35 -0400 | [diff] [blame] | 2586 | if prealloc[v] == nil { |
| 2587 | prealloc[v] = callnew(v.Type) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2588 | } |
Ian Lance Taylor | 188e3d2 | 2016-02-26 14:28:48 -0800 | [diff] [blame] | 2589 | nn = append(nn, Nod(OAS, v.Name.Heapaddr, prealloc[v])) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2590 | if v.Class&^PHEAP != PPARAMOUT { |
Matthew Dempsky | 2339b13 | 2016-03-09 18:54:26 -0800 | [diff] [blame] | 2591 | as := Nod(OAS, v, v.Name.Param.Stackparam) |
Russ Cox | 3c3019a | 2015-05-27 00:44:05 -0400 | [diff] [blame] | 2592 | v.Name.Param.Stackparam.Typecheck = 1 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2593 | typecheck(&as, Etop) |
Ian Lance Taylor | 188e3d2 | 2016-02-26 14:28:48 -0800 | [diff] [blame] | 2594 | as = applywritebarrier(as) |
| 2595 | nn = append(nn, as) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2596 | } |
| 2597 | } |
| 2598 | |
| 2599 | return nn |
| 2600 | } |
| 2601 | |
Matthew Dempsky | 2339b13 | 2016-03-09 18:54:26 -0800 | [diff] [blame] | 2602 | // returnsfromheap returns code to copy values for heap-escaped parameters |
| 2603 | // back to the stack. |
| 2604 | func returnsfromheap(params *Type) []*Node { |
Ian Lance Taylor | 188e3d2 | 2016-02-26 14:28:48 -0800 | [diff] [blame] | 2605 | var nn []*Node |
Matthew Dempsky | f6bca3f | 2016-03-17 01:32:18 -0700 | [diff] [blame^] | 2606 | for _, t := range params.Fields().Slice() { |
Matthew Dempsky | 2339b13 | 2016-03-09 18:54:26 -0800 | [diff] [blame] | 2607 | v := t.Nname |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2608 | if v == nil || v.Class != PHEAP|PPARAMOUT { |
| 2609 | continue |
| 2610 | } |
Ian Lance Taylor | 188e3d2 | 2016-02-26 14:28:48 -0800 | [diff] [blame] | 2611 | nn = append(nn, Nod(OAS, v.Name.Param.Stackparam, v)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | return nn |
| 2615 | } |
| 2616 | |
Matthew Dempsky | 2339b13 | 2016-03-09 18:54:26 -0800 | [diff] [blame] | 2617 | // heapmoves generates code to handle migrating heap-escaped parameters |
| 2618 | // between the stack and the heap. The generated code is added to Curfn's |
| 2619 | // Enter and Exit lists. |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2620 | func heapmoves() { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2621 | lno := lineno |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2622 | lineno = Curfn.Lineno |
Matthew Dempsky | f91b832 | 2016-03-09 20:54:59 -0800 | [diff] [blame] | 2623 | nn := paramstoheap(Curfn.Type.Recvs(), false) |
Matthew Dempsky | 2339b13 | 2016-03-09 18:54:26 -0800 | [diff] [blame] | 2624 | nn = append(nn, paramstoheap(Curfn.Type.Params(), false)...) |
| 2625 | nn = append(nn, paramstoheap(Curfn.Type.Results(), true)...) |
Ian Lance Taylor | 188e3d2 | 2016-02-26 14:28:48 -0800 | [diff] [blame] | 2626 | Curfn.Func.Enter.Append(nn...) |
Josh Bleecher Snyder | 3ed9e4c | 2015-03-25 19:33:01 -0700 | [diff] [blame] | 2627 | lineno = Curfn.Func.Endlineno |
Matthew Dempsky | 2339b13 | 2016-03-09 18:54:26 -0800 | [diff] [blame] | 2628 | Curfn.Func.Exit.Append(returnsfromheap(Curfn.Type.Results())...) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2629 | lineno = lno |
| 2630 | } |
| 2631 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2632 | func vmkcall(fn *Node, t *Type, init *Nodes, va []*Node) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2633 | if fn.Type == nil || fn.Type.Etype != TFUNC { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 2634 | Fatalf("mkcall %v %v", fn, fn.Type) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2635 | } |
| 2636 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2637 | n := fn.Type.Intuple |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2638 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2639 | r := Nod(OCALL, fn, nil) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2640 | r.List.Set(va[:n]) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2641 | if fn.Type.Outtuple > 0 { |
| 2642 | typecheck(&r, Erv|Efnstruct) |
| 2643 | } else { |
| 2644 | typecheck(&r, Etop) |
| 2645 | } |
| 2646 | walkexpr(&r, init) |
| 2647 | r.Type = t |
| 2648 | return r |
| 2649 | } |
| 2650 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2651 | func mkcall(name string, t *Type, init *Nodes, args ...*Node) *Node { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2652 | return vmkcall(syslook(name), t, init, args) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2653 | } |
| 2654 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2655 | func mkcall1(fn *Node, t *Type, init *Nodes, args ...*Node) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2656 | return vmkcall(fn, t, init, args) |
| 2657 | } |
| 2658 | |
| 2659 | func conv(n *Node, t *Type) *Node { |
| 2660 | if Eqtype(n.Type, t) { |
| 2661 | return n |
| 2662 | } |
| 2663 | n = Nod(OCONV, n, nil) |
| 2664 | n.Type = t |
| 2665 | typecheck(&n, Erv) |
| 2666 | return n |
| 2667 | } |
| 2668 | |
| 2669 | func chanfn(name string, n int, t *Type) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2670 | if t.Etype != TCHAN { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 2671 | Fatalf("chanfn %v", t) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2672 | } |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2673 | fn := syslook(name) |
Russ Cox | 13f9c8b | 2015-03-08 13:33:49 -0400 | [diff] [blame] | 2674 | switch n { |
| 2675 | default: |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 2676 | Fatalf("chanfn %d", n) |
Russ Cox | 13f9c8b | 2015-03-08 13:33:49 -0400 | [diff] [blame] | 2677 | case 1: |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2678 | substArgTypes(&fn, t.Type) |
Russ Cox | 13f9c8b | 2015-03-08 13:33:49 -0400 | [diff] [blame] | 2679 | case 2: |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2680 | substArgTypes(&fn, t.Type, t.Type) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2681 | } |
| 2682 | return fn |
| 2683 | } |
| 2684 | |
| 2685 | func mapfn(name string, t *Type) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2686 | if t.Etype != TMAP { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 2687 | Fatalf("mapfn %v", t) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2688 | } |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2689 | fn := syslook(name) |
Matthew Dempsky | 1c2bdfb | 2016-03-10 05:22:14 -0800 | [diff] [blame] | 2690 | substArgTypes(&fn, t.Key(), t.Type, t.Key(), t.Type) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2691 | return fn |
| 2692 | } |
| 2693 | |
| 2694 | func mapfndel(name string, t *Type) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2695 | if t.Etype != TMAP { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 2696 | Fatalf("mapfn %v", t) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2697 | } |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2698 | fn := syslook(name) |
Matthew Dempsky | 1c2bdfb | 2016-03-10 05:22:14 -0800 | [diff] [blame] | 2699 | substArgTypes(&fn, t.Key(), t.Type, t.Key()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2700 | return fn |
| 2701 | } |
| 2702 | |
| 2703 | func writebarrierfn(name string, l *Type, r *Type) *Node { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2704 | fn := syslook(name) |
| 2705 | substArgTypes(&fn, l, r) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2706 | return fn |
| 2707 | } |
| 2708 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2709 | func addstr(n *Node, init *Nodes) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2710 | // orderexpr rewrote OADDSTR to have a list of strings. |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2711 | c := n.List.Len() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2712 | |
| 2713 | if c < 2 { |
| 2714 | Yyerror("addstr count %d too small", c) |
| 2715 | } |
| 2716 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2717 | buf := nodnil() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2718 | if n.Esc == EscNone { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2719 | sz := int64(0) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2720 | for _, n1 := range n.List.Slice() { |
| 2721 | if n1.Op == OLITERAL { |
| 2722 | sz += int64(len(n1.Val().U.(string))) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2723 | } |
| 2724 | } |
| 2725 | |
| 2726 | // Don't allocate the buffer if the result won't fit. |
| 2727 | if sz < tmpstringbufsize { |
| 2728 | // Create temporary buffer for result string on stack. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2729 | t := aindex(Nodintconst(tmpstringbufsize), Types[TUINT8]) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2730 | |
| 2731 | buf = Nod(OADDR, temp(t), nil) |
| 2732 | } |
| 2733 | } |
| 2734 | |
| 2735 | // build list of string arguments |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 2736 | args := []*Node{buf} |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2737 | for _, n2 := range n.List.Slice() { |
| 2738 | args = append(args, conv(n2, Types[TSTRING])) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2739 | } |
| 2740 | |
Matthew Dempsky | 8b3670f | 2015-03-06 12:02:24 -0800 | [diff] [blame] | 2741 | var fn string |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2742 | if c <= 5 { |
| 2743 | // small numbers of strings use direct runtime helpers. |
| 2744 | // note: orderexpr knows this cutoff too. |
Matthew Dempsky | 8b3670f | 2015-03-06 12:02:24 -0800 | [diff] [blame] | 2745 | fn = fmt.Sprintf("concatstring%d", c) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2746 | } else { |
| 2747 | // large numbers of strings are passed to the runtime as a slice. |
Matthew Dempsky | 8b3670f | 2015-03-06 12:02:24 -0800 | [diff] [blame] | 2748 | fn = "concatstrings" |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2749 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2750 | t := typ(TARRAY) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2751 | t.Type = Types[TSTRING] |
| 2752 | t.Bound = -1 |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2753 | slice := Nod(OCOMPLIT, nil, typenod(t)) |
Russ Cox | 60e5f5b | 2015-05-26 23:05:35 -0400 | [diff] [blame] | 2754 | if prealloc[n] != nil { |
| 2755 | prealloc[slice] = prealloc[n] |
| 2756 | } |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2757 | slice.List.Set(args[1:]) // skip buf arg |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 2758 | args = []*Node{buf} |
| 2759 | args = append(args, slice) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2760 | slice.Esc = EscNone |
| 2761 | } |
| 2762 | |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2763 | cat := syslook(fn) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2764 | r := Nod(OCALL, cat, nil) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2765 | r.List.Set(args) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2766 | typecheck(&r, Erv) |
| 2767 | walkexpr(&r, init) |
| 2768 | r.Type = n.Type |
| 2769 | |
| 2770 | return r |
| 2771 | } |
| 2772 | |
| 2773 | // expand append(l1, l2...) to |
| 2774 | // init { |
| 2775 | // s := l1 |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2776 | // n := len(s) + len(l2) |
| 2777 | // // Compare as uint so growslice can panic on overflow. |
| 2778 | // if uint(n) > uint(cap(s)) { |
| 2779 | // s = growslice(s, n) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2780 | // } |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2781 | // s = s[:n] |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2782 | // memmove(&s[len(l1)], &l2[0], len(l2)*sizeof(T)) |
| 2783 | // } |
| 2784 | // s |
| 2785 | // |
| 2786 | // l2 is allowed to be a string. |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2787 | func appendslice(n *Node, init *Nodes) *Node { |
| 2788 | walkexprlistsafe(n.List.Slice(), init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2789 | |
| 2790 | // walkexprlistsafe will leave OINDEX (s[n]) alone if both s |
| 2791 | // 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] | 2792 | // modifying here. Fix explicitly. |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 2793 | ls := n.List.Slice() |
| 2794 | for i1, n1 := range ls { |
| 2795 | ls[i1] = cheapexpr(n1, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2796 | } |
| 2797 | |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2798 | l1 := n.List.First() |
| 2799 | l2 := n.List.Second() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2800 | |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 2801 | var l []*Node |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2802 | |
| 2803 | // var s []T |
| 2804 | s := temp(l1.Type) |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 2805 | l = append(l, Nod(OAS, s, l1)) // s = l1 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2806 | |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2807 | // n := len(s) + len(l2) |
| 2808 | nn := temp(Types[TINT]) |
| 2809 | 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] | 2810 | |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2811 | // if uint(n) > uint(cap(s)) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2812 | nif := Nod(OIF, nil, nil) |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2813 | nif.Left = Nod(OGT, Nod(OCONV, nn, nil), Nod(OCONV, Nod(OCAP, s, nil), nil)) |
| 2814 | nif.Left.Left.Type = Types[TUINT] |
| 2815 | nif.Left.Right.Type = Types[TUINT] |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2816 | |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2817 | // instantiate growslice(Type*, []any, int) []any |
| 2818 | fn := syslook("growslice") |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2819 | substArgTypes(&fn, s.Type.Type, s.Type.Type) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2820 | |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2821 | // s = growslice(T, s, n) |
| 2822 | 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] | 2823 | l = append(l, nif) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2824 | |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2825 | // s = s[:n] |
| 2826 | nt := Nod(OSLICE, s, Nod(OKEY, nil, nn)) |
| 2827 | nt.Etype = 1 |
| 2828 | l = append(l, Nod(OAS, s, nt)) |
| 2829 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2830 | if haspointers(l1.Type.Type) { |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2831 | // copy(s[len(l1):], l2) |
| 2832 | nptr1 := Nod(OSLICE, s, Nod(OKEY, Nod(OLEN, l1, nil), nil)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2833 | |
| 2834 | nptr1.Etype = 1 |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2835 | nptr2 := l2 |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2836 | fn := syslook("typedslicecopy") |
| 2837 | substArgTypes(&fn, l1.Type, l2.Type) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2838 | var ln Nodes |
| 2839 | ln.Set(l) |
| 2840 | nt := mkcall1(fn, Types[TINT], &ln, typename(l1.Type.Type), nptr1, nptr2) |
| 2841 | l = append(ln.Slice(), nt) |
Ian Lance Taylor | 9e902f0 | 2015-10-20 10:00:07 -0700 | [diff] [blame] | 2842 | } else if instrumenting { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2843 | // rely on runtime to instrument copy. |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2844 | // copy(s[len(l1):], l2) |
| 2845 | nptr1 := Nod(OSLICE, s, Nod(OKEY, Nod(OLEN, l1, nil), nil)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2846 | |
| 2847 | nptr1.Etype = 1 |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2848 | nptr2 := l2 |
| 2849 | var fn *Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2850 | if l2.Type.Etype == TSTRING { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2851 | fn = syslook("slicestringcopy") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2852 | } else { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2853 | fn = syslook("slicecopy") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2854 | } |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2855 | substArgTypes(&fn, l1.Type, l2.Type) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2856 | var ln Nodes |
| 2857 | ln.Set(l) |
| 2858 | nt := mkcall1(fn, Types[TINT], &ln, nptr1, nptr2, Nodintconst(s.Type.Type.Width)) |
| 2859 | l = append(ln.Slice(), nt) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2860 | } else { |
| 2861 | // memmove(&s[len(l1)], &l2[0], len(l2)*sizeof(T)) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2862 | nptr1 := Nod(OINDEX, s, Nod(OLEN, l1, nil)) |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2863 | nptr1.Bounded = true |
Matthew Dempsky | d1341d6 | 2016-03-13 15:22:45 -0700 | [diff] [blame] | 2864 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2865 | nptr1 = Nod(OADDR, nptr1, nil) |
| 2866 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2867 | nptr2 := Nod(OSPTR, l2, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2868 | |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2869 | fn := syslook("memmove") |
| 2870 | substArgTypes(&fn, s.Type.Type, s.Type.Type) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2871 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2872 | var ln Nodes |
| 2873 | ln.Set(l) |
| 2874 | nwid := cheapexpr(conv(Nod(OLEN, l2, nil), Types[TUINTPTR]), &ln) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2875 | |
| 2876 | nwid = Nod(OMUL, nwid, Nodintconst(s.Type.Type.Width)) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2877 | nt := mkcall1(fn, nil, &ln, nptr1, nptr2, nwid) |
| 2878 | l = append(ln.Slice(), nt) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2879 | } |
| 2880 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2881 | typechecklist(l, Etop) |
| 2882 | walkstmtlist(l) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2883 | init.Append(l...) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2884 | return s |
| 2885 | } |
| 2886 | |
Russ Cox | 8552047 | 2015-05-06 12:34:30 -0400 | [diff] [blame] | 2887 | // Rewrite append(src, x, y, z) so that any side effects in |
| 2888 | // x, y, z (including runtime panics) are evaluated in |
| 2889 | // initialization statements before the append. |
| 2890 | // For normal code generation, stop there and leave the |
| 2891 | // rest to cgen_append. |
| 2892 | // |
| 2893 | // For race detector, expand append(src, a [, b]* ) to |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2894 | // |
| 2895 | // init { |
| 2896 | // s := src |
| 2897 | // const argc = len(args) - 1 |
| 2898 | // if cap(s) - len(s) < argc { |
Russ Cox | 32fddad | 2015-06-25 19:27:20 -0400 | [diff] [blame] | 2899 | // s = growslice(s, len(s)+argc) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2900 | // } |
| 2901 | // n := len(s) |
| 2902 | // s = s[:n+argc] |
| 2903 | // s[n] = a |
| 2904 | // s[n+1] = b |
| 2905 | // ... |
| 2906 | // } |
| 2907 | // s |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2908 | func walkappend(n *Node, init *Nodes, dst *Node) *Node { |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2909 | if !samesafeexpr(dst, n.List.First()) { |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 2910 | n.List.SetIndex(0, safeexpr(n.List.Index(0), init)) |
| 2911 | walkexpr(n.List.Addr(0), init) |
Russ Cox | 8552047 | 2015-05-06 12:34:30 -0400 | [diff] [blame] | 2912 | } |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2913 | walkexprlistsafe(n.List.Slice()[1:], init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2914 | |
| 2915 | // walkexprlistsafe will leave OINDEX (s[n]) alone if both s |
| 2916 | // 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] | 2917 | // modifying here. Fix explicitly. |
Russ Cox | 8552047 | 2015-05-06 12:34:30 -0400 | [diff] [blame] | 2918 | // Using cheapexpr also makes sure that the evaluation |
| 2919 | // of all arguments (and especially any panics) happen |
| 2920 | // before we begin to modify the slice in a visible way. |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 2921 | ls := n.List.Slice()[1:] |
| 2922 | for i, n := range ls { |
| 2923 | ls[i] = cheapexpr(n, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2924 | } |
| 2925 | |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2926 | nsrc := n.List.First() |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2927 | |
| 2928 | // Resolve slice type of multi-valued return. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2929 | if Istype(nsrc.Type, TSTRUCT) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2930 | nsrc.Type = nsrc.Type.Type.Type |
| 2931 | } |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 2932 | argc := n.List.Len() - 1 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2933 | if argc < 1 { |
| 2934 | return nsrc |
| 2935 | } |
| 2936 | |
Russ Cox | 8552047 | 2015-05-06 12:34:30 -0400 | [diff] [blame] | 2937 | // General case, with no function calls left as arguments. |
Ian Lance Taylor | 0c69f13 | 2015-10-21 07:04:10 -0700 | [diff] [blame] | 2938 | // Leave for gen, except that instrumentation requires old form. |
Ian Lance Taylor | 9e902f0 | 2015-10-20 10:00:07 -0700 | [diff] [blame] | 2939 | if !instrumenting { |
Russ Cox | 8552047 | 2015-05-06 12:34:30 -0400 | [diff] [blame] | 2940 | return n |
| 2941 | } |
| 2942 | |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 2943 | var l []*Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2944 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2945 | ns := temp(nsrc.Type) |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 2946 | l = append(l, Nod(OAS, ns, nsrc)) // s = src |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2947 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2948 | na := Nodintconst(int64(argc)) // const argc |
| 2949 | nx := Nod(OIF, nil, nil) // if cap(s) - len(s) < argc |
Russ Cox | 66be148 | 2015-05-26 21:30:20 -0400 | [diff] [blame] | 2950 | 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] | 2951 | |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2952 | fn := syslook("growslice") // growslice(<type>, old []T, mincap int) (ret []T) |
| 2953 | substArgTypes(&fn, ns.Type.Type, ns.Type.Type) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2954 | |
Ian Lance Taylor | c63dbd8 | 2016-03-10 10:13:42 -0800 | [diff] [blame] | 2955 | nx.Nbody.Set1(Nod(OAS, ns, |
| 2956 | mkcall1(fn, ns.Type, &nx.Ninit, typename(ns.Type), ns, |
| 2957 | Nod(OADD, Nod(OLEN, ns, nil), na)))) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2958 | |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 2959 | l = append(l, nx) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2960 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2961 | nn := temp(Types[TINT]) |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 2962 | 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] | 2963 | |
| 2964 | nx = Nod(OSLICE, ns, Nod(OKEY, nil, Nod(OADD, nn, na))) // ...s[:n+argc] |
| 2965 | nx.Etype = 1 |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 2966 | l = append(l, Nod(OAS, ns, nx)) // s = s[:n+argc] |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2967 | |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 2968 | ls = n.List.Slice()[1:] |
| 2969 | for i, n := range ls { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2970 | nx = Nod(OINDEX, ns, nn) // s[n] ... |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 2971 | nx.Bounded = true |
Ian Lance Taylor | cd6619d | 2016-03-09 12:39:36 -0800 | [diff] [blame] | 2972 | l = append(l, Nod(OAS, nx, n)) // s[n] = arg |
| 2973 | if i+1 < len(ls) { |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 2974 | 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] | 2975 | } |
| 2976 | } |
| 2977 | |
| 2978 | typechecklist(l, Etop) |
| 2979 | walkstmtlist(l) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2980 | init.Append(l...) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2981 | return ns |
| 2982 | } |
| 2983 | |
| 2984 | // Lower copy(a, b) to a memmove call or a runtime call. |
| 2985 | // |
| 2986 | // init { |
| 2987 | // n := len(a) |
| 2988 | // if n > len(b) { n = len(b) } |
| 2989 | // memmove(a.ptr, b.ptr, n*sizeof(elem(a))) |
| 2990 | // } |
| 2991 | // n; |
| 2992 | // |
| 2993 | // Also works if b is a string. |
| 2994 | // |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 2995 | func copyany(n *Node, init *Nodes, runtimecall bool) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2996 | if haspointers(n.Left.Type.Type) { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 2997 | fn := writebarrierfn("typedslicecopy", n.Left.Type, n.Right.Type) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 2998 | return mkcall1(fn, n.Type, init, typename(n.Left.Type.Type), n.Left, n.Right) |
| 2999 | } |
| 3000 | |
Ian Lance Taylor | 9e902f0 | 2015-10-20 10:00:07 -0700 | [diff] [blame] | 3001 | if runtimecall { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3002 | var fn *Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3003 | if n.Right.Type.Etype == TSTRING { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 3004 | fn = syslook("slicestringcopy") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3005 | } else { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 3006 | fn = syslook("slicecopy") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3007 | } |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 3008 | substArgTypes(&fn, n.Left.Type, n.Right.Type) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3009 | return mkcall1(fn, n.Type, init, n.Left, n.Right, Nodintconst(n.Left.Type.Type.Width)) |
| 3010 | } |
| 3011 | |
| 3012 | walkexpr(&n.Left, init) |
| 3013 | walkexpr(&n.Right, init) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3014 | nl := temp(n.Left.Type) |
| 3015 | nr := temp(n.Right.Type) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3016 | var l []*Node |
| 3017 | l = append(l, Nod(OAS, nl, n.Left)) |
| 3018 | l = append(l, Nod(OAS, nr, n.Right)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3019 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3020 | nfrm := Nod(OSPTR, nr, nil) |
| 3021 | nto := Nod(OSPTR, nl, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3022 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3023 | nlen := temp(Types[TINT]) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3024 | |
| 3025 | // n = len(to) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3026 | l = append(l, Nod(OAS, nlen, Nod(OLEN, nl, nil))) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3027 | |
| 3028 | // if n > len(frm) { n = len(frm) } |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3029 | nif := Nod(OIF, nil, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3030 | |
Russ Cox | 66be148 | 2015-05-26 21:30:20 -0400 | [diff] [blame] | 3031 | nif.Left = Nod(OGT, nlen, Nod(OLEN, nr, nil)) |
Ian Lance Taylor | 1d5001a | 2016-02-27 14:31:33 -0800 | [diff] [blame] | 3032 | nif.Nbody.Append(Nod(OAS, nlen, Nod(OLEN, nr, nil))) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3033 | l = append(l, nif) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3034 | |
| 3035 | // Call memmove. |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 3036 | fn := syslook("memmove") |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3037 | |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 3038 | substArgTypes(&fn, nl.Type.Type, nl.Type.Type) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3039 | nwid := temp(Types[TUINTPTR]) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3040 | l = append(l, Nod(OAS, nwid, conv(nlen, Types[TUINTPTR]))) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3041 | nwid = Nod(OMUL, nwid, Nodintconst(nl.Type.Type.Width)) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3042 | l = append(l, mkcall1(fn, nil, init, nto, nfrm, nwid)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3043 | |
| 3044 | typechecklist(l, Etop) |
| 3045 | walkstmtlist(l) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3046 | init.Append(l...) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3047 | return nlen |
| 3048 | } |
| 3049 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3050 | func eqfor(t *Type, needsize *int) *Node { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3051 | // Should only arrive here with large memory or |
| 3052 | // a struct/array containing a non-memory field/element. |
| 3053 | // Small memory is handled inline, and single non-memory |
| 3054 | // is handled during type check (OCMPSTR etc). |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3055 | a := algtype1(t, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3056 | |
| 3057 | if a != AMEM && a != -1 { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 3058 | Fatalf("eqfor %v", t) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3059 | } |
| 3060 | |
| 3061 | if a == AMEM { |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 3062 | n := syslook("memequal") |
| 3063 | substArgTypes(&n, t, t) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3064 | *needsize = 1 |
| 3065 | return n |
| 3066 | } |
| 3067 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3068 | sym := typesymprefix(".eq", t) |
| 3069 | n := newname(sym) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3070 | n.Class = PFUNC |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3071 | ntype := Nod(OTFUNC, nil, nil) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 3072 | ntype.List.Append(Nod(ODCLFIELD, nil, typenod(Ptrto(t)))) |
| 3073 | ntype.List.Append(Nod(ODCLFIELD, nil, typenod(Ptrto(t)))) |
| 3074 | ntype.Rlist.Append(Nod(ODCLFIELD, nil, typenod(Types[TBOOL]))) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3075 | typecheck(&ntype, Etype) |
| 3076 | n.Type = ntype.Type |
| 3077 | *needsize = 0 |
| 3078 | return n |
| 3079 | } |
| 3080 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3081 | func walkcompare(np **Node, init *Nodes) { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3082 | n := *np |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3083 | |
| 3084 | // Given interface value l and concrete value r, rewrite |
| 3085 | // l == r |
| 3086 | // to |
| 3087 | // x, ok := l.(type(r)); ok && x == r |
| 3088 | // Handle != similarly. |
| 3089 | // This avoids the allocation that would be required |
| 3090 | // to convert r to l for comparison. |
Russ Cox | 175929b | 2015-03-02 14:22:05 -0500 | [diff] [blame] | 3091 | var l *Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3092 | |
Russ Cox | 175929b | 2015-03-02 14:22:05 -0500 | [diff] [blame] | 3093 | var r *Node |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3094 | if Isinter(n.Left.Type) && !Isinter(n.Right.Type) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3095 | l = n.Left |
| 3096 | r = n.Right |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3097 | } else if !Isinter(n.Left.Type) && Isinter(n.Right.Type) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3098 | l = n.Right |
| 3099 | r = n.Left |
| 3100 | } |
| 3101 | |
| 3102 | if l != nil { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3103 | x := temp(r.Type) |
Austin Clements | 05a3b1f | 2015-08-24 13:35:49 -0400 | [diff] [blame] | 3104 | if haspointers(r.Type) { |
| 3105 | a := Nod(OAS, x, nil) |
| 3106 | typecheck(&a, Etop) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3107 | init.Append(a) |
Austin Clements | 05a3b1f | 2015-08-24 13:35:49 -0400 | [diff] [blame] | 3108 | } |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3109 | ok := temp(Types[TBOOL]) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3110 | |
| 3111 | // l.(type(r)) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3112 | a := Nod(ODOTTYPE, l, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3113 | |
| 3114 | a.Type = r.Type |
| 3115 | |
| 3116 | // x, ok := l.(type(r)) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3117 | expr := Nod(OAS2, nil, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3118 | |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 3119 | expr.List.Append(x) |
| 3120 | expr.List.Append(ok) |
| 3121 | expr.Rlist.Append(a) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3122 | typecheck(&expr, Etop) |
| 3123 | walkexpr(&expr, init) |
| 3124 | |
| 3125 | if n.Op == OEQ { |
| 3126 | r = Nod(OANDAND, ok, Nod(OEQ, x, r)) |
| 3127 | } else { |
| 3128 | r = Nod(OOROR, Nod(ONOT, ok, nil), Nod(ONE, x, r)) |
| 3129 | } |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3130 | init.Append(expr) |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3131 | finishcompare(np, n, r, init) |
| 3132 | return |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | // Must be comparison of array or struct. |
| 3136 | // Otherwise back end handles it. |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3137 | t := n.Left.Type |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3138 | |
| 3139 | switch t.Etype { |
| 3140 | default: |
| 3141 | return |
| 3142 | |
| 3143 | case TARRAY: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3144 | if Isslice(t) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3145 | return |
| 3146 | } |
| 3147 | |
| 3148 | case TSTRUCT: |
| 3149 | break |
| 3150 | } |
| 3151 | |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3152 | cmpl := n.Left |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3153 | for cmpl != nil && cmpl.Op == OCONVNOP { |
| 3154 | cmpl = cmpl.Left |
| 3155 | } |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3156 | cmpr := n.Right |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3157 | for cmpr != nil && cmpr.Op == OCONVNOP { |
| 3158 | cmpr = cmpr.Left |
| 3159 | } |
| 3160 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3161 | if !islvalue(cmpl) || !islvalue(cmpr) { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 3162 | Fatalf("arguments of comparison must be lvalues - %v %v", cmpl, cmpr) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | l = temp(Ptrto(t)) |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3166 | a := Nod(OAS, l, Nod(OADDR, cmpl, nil)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3167 | a.Right.Etype = 1 // addr does not escape |
| 3168 | typecheck(&a, Etop) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3169 | init.Append(a) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3170 | |
| 3171 | r = temp(Ptrto(t)) |
| 3172 | a = Nod(OAS, r, Nod(OADDR, cmpr, nil)) |
| 3173 | a.Right.Etype = 1 // addr does not escape |
| 3174 | typecheck(&a, Etop) |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3175 | init.Append(a) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3176 | |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 3177 | var andor Op = OANDAND |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3178 | if n.Op == ONE { |
| 3179 | andor = OOROR |
| 3180 | } |
| 3181 | |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3182 | var expr *Node |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 3183 | if t.Etype == TARRAY && t.Bound <= 4 && issimple[t.Type.Etype] { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3184 | // Four or fewer elements of a basic type. |
| 3185 | // Unroll comparisons. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3186 | var li *Node |
| 3187 | var ri *Node |
| 3188 | for i := 0; int64(i) < t.Bound; i++ { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3189 | li = Nod(OINDEX, l, Nodintconst(int64(i))) |
| 3190 | ri = Nod(OINDEX, r, Nodintconst(int64(i))) |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 3191 | a = Nod(n.Op, li, ri) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3192 | if expr == nil { |
| 3193 | expr = a |
| 3194 | } else { |
| 3195 | expr = Nod(andor, expr, a) |
| 3196 | } |
| 3197 | } |
| 3198 | |
| 3199 | if expr == nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3200 | expr = Nodbool(n.Op == OEQ) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3201 | } |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3202 | finishcompare(np, n, expr, init) |
| 3203 | return |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3204 | } |
| 3205 | |
Josh Bleecher Snyder | 4cef0e9 | 2015-05-04 15:02:09 -0700 | [diff] [blame] | 3206 | if t.Etype == TARRAY { |
| 3207 | // Zero- or single-element array, of any type. |
| 3208 | switch t.Bound { |
| 3209 | case 0: |
| 3210 | finishcompare(np, n, Nodbool(n.Op == OEQ), init) |
| 3211 | return |
| 3212 | case 1: |
| 3213 | l0 := Nod(OINDEX, l, Nodintconst(0)) |
| 3214 | r0 := Nod(OINDEX, r, Nodintconst(0)) |
| 3215 | a := Nod(n.Op, l0, r0) |
| 3216 | finishcompare(np, n, a, init) |
| 3217 | return |
| 3218 | } |
| 3219 | } |
| 3220 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3221 | if t.Etype == TSTRUCT && countfield(t) <= 4 { |
| 3222 | // Struct of four or fewer fields. |
| 3223 | // Inline comparisons. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3224 | var li *Node |
| 3225 | var ri *Node |
Matthew Dempsky | f6bca3f | 2016-03-17 01:32:18 -0700 | [diff] [blame^] | 3226 | for _, t1 := range t.Fields().Slice() { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3227 | if isblanksym(t1.Sym) { |
| 3228 | continue |
| 3229 | } |
| 3230 | li = Nod(OXDOT, l, newname(t1.Sym)) |
| 3231 | ri = Nod(OXDOT, r, newname(t1.Sym)) |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 3232 | a = Nod(n.Op, li, ri) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3233 | if expr == nil { |
| 3234 | expr = a |
| 3235 | } else { |
| 3236 | expr = Nod(andor, expr, a) |
| 3237 | } |
| 3238 | } |
| 3239 | |
| 3240 | if expr == nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3241 | expr = Nodbool(n.Op == OEQ) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3242 | } |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3243 | finishcompare(np, n, expr, init) |
| 3244 | return |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3245 | } |
| 3246 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 3247 | // Chose not to inline. Call equality function directly. |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3248 | var needsize int |
| 3249 | call := Nod(OCALL, eqfor(t, &needsize), nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3250 | |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 3251 | call.List.Append(l) |
| 3252 | call.List.Append(r) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3253 | if needsize != 0 { |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 3254 | call.List.Append(Nodintconst(t.Width)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3255 | } |
| 3256 | r = call |
| 3257 | if n.Op != OEQ { |
| 3258 | r = Nod(ONOT, r, nil) |
| 3259 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3260 | |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3261 | finishcompare(np, n, r, init) |
| 3262 | return |
| 3263 | } |
| 3264 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3265 | func finishcompare(np **Node, n, r *Node, init *Nodes) { |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3266 | // Using np here to avoid passing &r to typecheck. |
| 3267 | *np = r |
| 3268 | typecheck(np, Erv) |
| 3269 | walkexpr(np, init) |
| 3270 | r = *np |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3271 | if r.Type != n.Type { |
| 3272 | r = Nod(OCONVNOP, r, nil) |
| 3273 | r.Type = n.Type |
| 3274 | r.Typecheck = 1 |
Russ Cox | 4492811 | 2015-03-02 20:34:22 -0500 | [diff] [blame] | 3275 | *np = r |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3276 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3277 | } |
| 3278 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3279 | func samecheap(a *Node, b *Node) bool { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3280 | var ar *Node |
| 3281 | var br *Node |
| 3282 | for a != nil && b != nil && a.Op == b.Op { |
| 3283 | switch a.Op { |
| 3284 | default: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3285 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3286 | |
| 3287 | case ONAME: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3288 | return a == b |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3289 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 3290 | case ODOT, ODOTPTR: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3291 | ar = a.Right |
| 3292 | br = b.Right |
| 3293 | if ar.Op != ONAME || br.Op != ONAME || ar.Sym != br.Sym { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3294 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3295 | } |
| 3296 | |
| 3297 | case OINDEX: |
| 3298 | ar = a.Right |
| 3299 | br = b.Right |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 3300 | 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] | 3301 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | a = a.Left |
| 3306 | b = b.Left |
| 3307 | } |
| 3308 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3309 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | func walkrotate(np **Node) { |
Yao Zhang | d5cd4ab | 2015-09-10 11:33:09 -0400 | [diff] [blame] | 3313 | if Thearch.Thechar == '0' || Thearch.Thechar == '7' || Thearch.Thechar == '9' { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3314 | return |
| 3315 | } |
| 3316 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3317 | n := *np |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3318 | |
| 3319 | // Want << | >> or >> | << or << ^ >> or >> ^ << on unsigned value. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3320 | l := n.Left |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3321 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3322 | r := n.Right |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 3323 | 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] | 3324 | return |
| 3325 | } |
| 3326 | |
| 3327 | // Want same, side effect-free expression on lhs of both shifts. |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3328 | if !samecheap(l.Left, r.Left) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3329 | return |
| 3330 | } |
| 3331 | |
| 3332 | // Constants adding to width? |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3333 | w := int(l.Type.Width * 8) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3334 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3335 | if Smallintconst(l.Right) && Smallintconst(r.Right) { |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 3336 | sl := int(Mpgetfix(l.Right.Val().U.(*Mpint))) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3337 | if sl >= 0 { |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 3338 | sr := int(Mpgetfix(r.Right.Val().U.(*Mpint))) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3339 | if sr >= 0 && sl+sr == w { |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3340 | // Rewrite left shift half to left rotate. |
| 3341 | if l.Op == OLSH { |
| 3342 | n = l |
| 3343 | } else { |
| 3344 | n = r |
| 3345 | } |
| 3346 | n.Op = OLROT |
| 3347 | |
| 3348 | // Remove rotate 0 and rotate w. |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 3349 | s := int(Mpgetfix(n.Right.Val().U.(*Mpint))) |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3350 | |
| 3351 | if s == 0 || s == w { |
| 3352 | n = n.Left |
| 3353 | } |
| 3354 | |
| 3355 | *np = n |
| 3356 | return |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3357 | } |
| 3358 | } |
| 3359 | return |
| 3360 | } |
| 3361 | |
| 3362 | // TODO: Could allow s and 32-s if s is bounded (maybe s&31 and 32-s&31). |
| 3363 | return |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3364 | } |
| 3365 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 3366 | // walkmul rewrites integer multiplication by powers of two as shifts. |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3367 | func walkmul(np **Node, init *Nodes) { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3368 | n := *np |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 3369 | if !Isint[n.Type.Etype] { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3370 | return |
| 3371 | } |
| 3372 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3373 | var nr *Node |
| 3374 | var nl *Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3375 | if n.Right.Op == OLITERAL { |
| 3376 | nl = n.Left |
| 3377 | nr = n.Right |
| 3378 | } else if n.Left.Op == OLITERAL { |
| 3379 | nl = n.Right |
| 3380 | nr = n.Left |
| 3381 | } else { |
| 3382 | return |
| 3383 | } |
| 3384 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3385 | neg := 0 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3386 | |
| 3387 | // x*0 is 0 (and side effects of x). |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3388 | var pow int |
| 3389 | var w int |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 3390 | if Mpgetfix(nr.Val().U.(*Mpint)) == 0 { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3391 | cheapexpr(nl, init) |
| 3392 | Nodconst(n, n.Type, 0) |
| 3393 | goto ret |
| 3394 | } |
| 3395 | |
| 3396 | // nr is a constant. |
| 3397 | pow = powtwo(nr) |
| 3398 | |
| 3399 | if pow < 0 { |
| 3400 | return |
| 3401 | } |
| 3402 | if pow >= 1000 { |
| 3403 | // negative power of 2, like -16 |
| 3404 | neg = 1 |
| 3405 | |
| 3406 | pow -= 1000 |
| 3407 | } |
| 3408 | |
| 3409 | w = int(nl.Type.Width * 8) |
| 3410 | if pow+1 >= w { // too big, shouldn't happen |
| 3411 | return |
| 3412 | } |
| 3413 | |
| 3414 | nl = cheapexpr(nl, init) |
| 3415 | |
| 3416 | if pow == 0 { |
| 3417 | // x*1 is x |
| 3418 | n = nl |
| 3419 | |
| 3420 | goto ret |
| 3421 | } |
| 3422 | |
| 3423 | n = Nod(OLSH, nl, Nodintconst(int64(pow))) |
| 3424 | |
| 3425 | ret: |
| 3426 | if neg != 0 { |
| 3427 | n = Nod(OMINUS, n, nil) |
| 3428 | } |
| 3429 | |
| 3430 | typecheck(&n, Erv) |
| 3431 | walkexpr(&n, init) |
| 3432 | *np = n |
| 3433 | } |
| 3434 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 3435 | // walkdiv rewrites division by a constant as less expensive |
| 3436 | // operations. |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3437 | func walkdiv(np **Node, init *Nodes) { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3438 | // if >= 0, nr is 1<<pow // 1 if nr is negative. |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3439 | |
| 3440 | // TODO(minux) |
Yao Zhang | d5cd4ab | 2015-09-10 11:33:09 -0400 | [diff] [blame] | 3441 | if Thearch.Thechar == '0' || Thearch.Thechar == '7' || Thearch.Thechar == '9' { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3442 | return |
| 3443 | } |
| 3444 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3445 | n := *np |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3446 | if n.Right.Op != OLITERAL { |
| 3447 | return |
| 3448 | } |
| 3449 | |
| 3450 | // nr is a constant. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3451 | nl := cheapexpr(n.Left, init) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3452 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3453 | nr := n.Right |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3454 | |
| 3455 | // special cases of mod/div |
| 3456 | // by a constant |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3457 | w := int(nl.Type.Width * 8) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3458 | |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 3459 | s := 0 // 1 if nr is negative. |
| 3460 | pow := powtwo(nr) // if >= 0, nr is 1<<pow |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3461 | if pow >= 1000 { |
| 3462 | // negative power of 2 |
| 3463 | s = 1 |
| 3464 | |
| 3465 | pow -= 1000 |
| 3466 | } |
| 3467 | |
| 3468 | if pow+1 >= w { |
| 3469 | // divisor too large. |
| 3470 | return |
| 3471 | } |
| 3472 | |
| 3473 | if pow < 0 { |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3474 | // try to do division by multiply by (2^w)/d |
| 3475 | // see hacker's delight chapter 10 |
| 3476 | // TODO: support 64-bit magic multiply here. |
| 3477 | var m Magic |
| 3478 | m.W = w |
| 3479 | |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 3480 | if Issigned[nl.Type.Etype] { |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 3481 | m.Sd = Mpgetfix(nr.Val().U.(*Mpint)) |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3482 | Smagic(&m) |
| 3483 | } else { |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 3484 | m.Ud = uint64(Mpgetfix(nr.Val().U.(*Mpint))) |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3485 | Umagic(&m) |
| 3486 | } |
| 3487 | |
| 3488 | if m.Bad != 0 { |
| 3489 | return |
| 3490 | } |
| 3491 | |
| 3492 | // We have a quick division method so use it |
| 3493 | // for modulo too. |
| 3494 | if n.Op == OMOD { |
| 3495 | // rewrite as A%B = A - (A/B*B). |
| 3496 | n1 := Nod(ODIV, nl, nr) |
| 3497 | |
| 3498 | n2 := Nod(OMUL, n1, nr) |
| 3499 | n = Nod(OSUB, nl, n2) |
| 3500 | goto ret |
| 3501 | } |
| 3502 | |
| 3503 | switch Simtype[nl.Type.Etype] { |
| 3504 | default: |
| 3505 | return |
| 3506 | |
| 3507 | // n1 = nl * magic >> w (HMUL) |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 3508 | case TUINT8, TUINT16, TUINT32: |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3509 | nc := Nod(OXXX, nil, nil) |
| 3510 | |
| 3511 | Nodconst(nc, nl.Type, int64(m.Um)) |
Todd Neal | 765c0f3 | 2015-06-23 18:59:52 -0500 | [diff] [blame] | 3512 | n1 := Nod(OHMUL, nl, nc) |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3513 | typecheck(&n1, Erv) |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3514 | if m.Ua != 0 { |
| 3515 | // Select a Go type with (at least) twice the width. |
| 3516 | var twide *Type |
| 3517 | switch Simtype[nl.Type.Etype] { |
| 3518 | default: |
| 3519 | return |
| 3520 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 3521 | case TUINT8, TUINT16: |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3522 | twide = Types[TUINT32] |
| 3523 | |
| 3524 | case TUINT32: |
| 3525 | twide = Types[TUINT64] |
| 3526 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 3527 | case TINT8, TINT16: |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3528 | twide = Types[TINT32] |
| 3529 | |
| 3530 | case TINT32: |
| 3531 | twide = Types[TINT64] |
| 3532 | } |
| 3533 | |
| 3534 | // add numerator (might overflow). |
| 3535 | // n2 = (n1 + nl) |
| 3536 | n2 := Nod(OADD, conv(n1, twide), conv(nl, twide)) |
| 3537 | |
| 3538 | // shift by m.s |
| 3539 | nc := Nod(OXXX, nil, nil) |
| 3540 | |
| 3541 | Nodconst(nc, Types[TUINT], int64(m.S)) |
| 3542 | n = conv(Nod(ORSH, n2, nc), nl.Type) |
| 3543 | } else { |
| 3544 | // n = n1 >> m.s |
| 3545 | nc := Nod(OXXX, nil, nil) |
| 3546 | |
| 3547 | Nodconst(nc, Types[TUINT], int64(m.S)) |
| 3548 | n = Nod(ORSH, n1, nc) |
| 3549 | } |
| 3550 | |
| 3551 | // n1 = nl * magic >> w |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 3552 | case TINT8, TINT16, TINT32: |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3553 | nc := Nod(OXXX, nil, nil) |
| 3554 | |
| 3555 | Nodconst(nc, nl.Type, m.Sm) |
Todd Neal | 765c0f3 | 2015-06-23 18:59:52 -0500 | [diff] [blame] | 3556 | n1 := Nod(OHMUL, nl, nc) |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3557 | typecheck(&n1, Erv) |
Russ Cox | 79f727a | 2015-03-02 12:35:15 -0500 | [diff] [blame] | 3558 | if m.Sm < 0 { |
| 3559 | // add the numerator. |
| 3560 | n1 = Nod(OADD, n1, nl) |
| 3561 | } |
| 3562 | |
| 3563 | // shift by m.s |
| 3564 | nc = Nod(OXXX, nil, nil) |
| 3565 | |
| 3566 | Nodconst(nc, Types[TUINT], int64(m.S)) |
| 3567 | n2 := conv(Nod(ORSH, n1, nc), nl.Type) |
| 3568 | |
| 3569 | // add 1 iff n1 is negative. |
| 3570 | nc = Nod(OXXX, nil, nil) |
| 3571 | |
| 3572 | Nodconst(nc, Types[TUINT], int64(w)-1) |
| 3573 | n3 := Nod(ORSH, nl, nc) // n4 = -1 iff n1 is negative. |
| 3574 | n = Nod(OSUB, n2, n3) |
| 3575 | |
| 3576 | // apply sign. |
| 3577 | if m.Sd < 0 { |
| 3578 | n = Nod(OMINUS, n, nil) |
| 3579 | } |
| 3580 | } |
| 3581 | |
| 3582 | goto ret |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3583 | } |
| 3584 | |
| 3585 | switch pow { |
| 3586 | case 0: |
| 3587 | if n.Op == OMOD { |
| 3588 | // nl % 1 is zero. |
| 3589 | Nodconst(n, n.Type, 0) |
| 3590 | } else if s != 0 { |
| 3591 | // divide by -1 |
| 3592 | n.Op = OMINUS |
| 3593 | |
| 3594 | n.Right = nil |
| 3595 | } else { |
| 3596 | // divide by 1 |
| 3597 | n = nl |
| 3598 | } |
| 3599 | |
| 3600 | default: |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 3601 | if Issigned[n.Type.Etype] { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3602 | if n.Op == OMOD { |
| 3603 | // signed modulo 2^pow is like ANDing |
| 3604 | // with the last pow bits, but if nl < 0, |
| 3605 | // nl & (2^pow-1) is (nl+1)%2^pow - 1. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3606 | nc := Nod(OXXX, nil, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3607 | |
| 3608 | Nodconst(nc, Types[Simtype[TUINT]], int64(w)-1) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3609 | n1 := Nod(ORSH, nl, nc) // n1 = -1 iff nl < 0. |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3610 | if pow == 1 { |
| 3611 | typecheck(&n1, Erv) |
| 3612 | n1 = cheapexpr(n1, init) |
| 3613 | |
| 3614 | // n = (nl+ε)&1 -ε where ε=1 iff nl<0. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3615 | n2 := Nod(OSUB, nl, n1) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3616 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3617 | nc := Nod(OXXX, nil, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3618 | Nodconst(nc, nl.Type, 1) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3619 | n3 := Nod(OAND, n2, nc) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3620 | n = Nod(OADD, n3, n1) |
| 3621 | } else { |
| 3622 | // n = (nl+ε)&(nr-1) - ε where ε=2^pow-1 iff nl<0. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3623 | nc := Nod(OXXX, nil, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3624 | |
| 3625 | Nodconst(nc, nl.Type, (1<<uint(pow))-1) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3626 | n2 := Nod(OAND, n1, nc) // n2 = 2^pow-1 iff nl<0. |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3627 | typecheck(&n2, Erv) |
| 3628 | n2 = cheapexpr(n2, init) |
| 3629 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3630 | n3 := Nod(OADD, nl, n2) |
| 3631 | n4 := Nod(OAND, n3, nc) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3632 | n = Nod(OSUB, n4, n2) |
| 3633 | } |
| 3634 | |
| 3635 | break |
| 3636 | } else { |
| 3637 | // arithmetic right shift does not give the correct rounding. |
| 3638 | // if nl >= 0, nl >> n == nl / nr |
| 3639 | // if nl < 0, we want to add 2^n-1 first. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3640 | nc := Nod(OXXX, nil, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3641 | |
| 3642 | Nodconst(nc, Types[Simtype[TUINT]], int64(w)-1) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3643 | n1 := Nod(ORSH, nl, nc) // n1 = -1 iff nl < 0. |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3644 | if pow == 1 { |
| 3645 | // nl+1 is nl-(-1) |
| 3646 | n.Left = Nod(OSUB, nl, n1) |
| 3647 | } else { |
| 3648 | // Do a logical right right on -1 to keep pow bits. |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3649 | nc := Nod(OXXX, nil, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3650 | |
| 3651 | Nodconst(nc, Types[Simtype[TUINT]], int64(w)-int64(pow)) |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3652 | n2 := Nod(ORSH, conv(n1, tounsigned(nl.Type)), nc) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3653 | n.Left = Nod(OADD, nl, conv(n2, nl.Type)) |
| 3654 | } |
| 3655 | |
| 3656 | // n = (nl + 2^pow-1) >> pow |
| 3657 | n.Op = ORSH |
| 3658 | |
| 3659 | nc = Nod(OXXX, nil, nil) |
| 3660 | Nodconst(nc, Types[Simtype[TUINT]], int64(pow)) |
| 3661 | n.Right = nc |
| 3662 | n.Typecheck = 0 |
| 3663 | } |
| 3664 | |
| 3665 | if s != 0 { |
| 3666 | n = Nod(OMINUS, n, nil) |
| 3667 | } |
| 3668 | break |
| 3669 | } |
| 3670 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3671 | nc := Nod(OXXX, nil, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3672 | if n.Op == OMOD { |
| 3673 | // n = nl & (nr-1) |
| 3674 | n.Op = OAND |
| 3675 | |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 3676 | Nodconst(nc, nl.Type, Mpgetfix(nr.Val().U.(*Mpint))-1) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3677 | } else { |
| 3678 | // n = nl >> pow |
| 3679 | n.Op = ORSH |
| 3680 | |
| 3681 | Nodconst(nc, Types[Simtype[TUINT]], int64(pow)) |
| 3682 | } |
| 3683 | |
| 3684 | n.Typecheck = 0 |
| 3685 | n.Right = nc |
| 3686 | } |
| 3687 | |
| 3688 | goto ret |
| 3689 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3690 | ret: |
| 3691 | typecheck(&n, Erv) |
| 3692 | walkexpr(&n, init) |
| 3693 | *np = n |
| 3694 | } |
| 3695 | |
| 3696 | // 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] | 3697 | func bounded(n *Node, max int64) bool { |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 3698 | if n.Type == nil || !Isint[n.Type.Etype] { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3699 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3700 | } |
| 3701 | |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 3702 | sign := Issigned[n.Type.Etype] |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3703 | bits := int32(8 * n.Type.Width) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3704 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3705 | if Smallintconst(n) { |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 3706 | v := Mpgetfix(n.Val().U.(*Mpint)) |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3707 | return 0 <= v && v < max |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | switch n.Op { |
| 3711 | case OAND: |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3712 | v := int64(-1) |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3713 | if Smallintconst(n.Left) { |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 3714 | v = Mpgetfix(n.Left.Val().U.(*Mpint)) |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3715 | } else if Smallintconst(n.Right) { |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 3716 | v = Mpgetfix(n.Right.Val().U.(*Mpint)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3717 | } |
| 3718 | |
| 3719 | if 0 <= v && v < max { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3720 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | case OMOD: |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 3724 | if !sign && Smallintconst(n.Right) { |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 3725 | v := Mpgetfix(n.Right.Val().U.(*Mpint)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3726 | if 0 <= v && v <= max { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3727 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3728 | } |
| 3729 | } |
| 3730 | |
| 3731 | case ODIV: |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 3732 | if !sign && Smallintconst(n.Right) { |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 3733 | v := Mpgetfix(n.Right.Val().U.(*Mpint)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3734 | for bits > 0 && v >= 2 { |
| 3735 | bits-- |
| 3736 | v >>= 1 |
| 3737 | } |
| 3738 | } |
| 3739 | |
| 3740 | case ORSH: |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 3741 | if !sign && Smallintconst(n.Right) { |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 3742 | v := Mpgetfix(n.Right.Val().U.(*Mpint)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3743 | if v > int64(bits) { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3744 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3745 | } |
| 3746 | bits -= int32(v) |
| 3747 | } |
| 3748 | } |
| 3749 | |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 3750 | if !sign && bits <= 62 && 1<<uint(bits) <= max { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3751 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3752 | } |
| 3753 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3754 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3755 | } |
| 3756 | |
David Crawshaw | cc15840 | 2016-03-10 16:15:26 -0500 | [diff] [blame] | 3757 | // usemethod check interface method calls for uses of reflect.Type.Method. |
| 3758 | func usemethod(n *Node) { |
| 3759 | t := n.Left.Type |
| 3760 | |
| 3761 | // Looking for either of: |
| 3762 | // Method(int) reflect.Method |
| 3763 | // MethodByName(string) (reflect.Method, bool) |
| 3764 | // |
| 3765 | // TODO(crawshaw): improve precision of match by working out |
| 3766 | // how to check the method name. |
| 3767 | if n := countfield(t.Params()); n != 1 { |
| 3768 | return |
| 3769 | } |
| 3770 | if n := countfield(t.Results()); n != 1 && n != 2 { |
| 3771 | return |
| 3772 | } |
| 3773 | p0 := t.Params().Field(0) |
| 3774 | res0 := t.Results().Field(0) |
Matthew Dempsky | 2e93690 | 2016-03-14 01:20:49 -0700 | [diff] [blame] | 3775 | var res1 *Field |
David Crawshaw | cc15840 | 2016-03-10 16:15:26 -0500 | [diff] [blame] | 3776 | if countfield(t.Results()) == 2 { |
| 3777 | res1 = t.Results().Field(1) |
| 3778 | } |
| 3779 | |
| 3780 | if res1 == nil { |
| 3781 | if p0.Type.Etype != TINT { |
| 3782 | return |
| 3783 | } |
| 3784 | } else { |
| 3785 | if p0.Type.Etype != TSTRING { |
| 3786 | return |
| 3787 | } |
| 3788 | if res1.Type.Etype != TBOOL { |
| 3789 | return |
| 3790 | } |
| 3791 | } |
Matthew Dempsky | 2e93690 | 2016-03-14 01:20:49 -0700 | [diff] [blame] | 3792 | if Tconv(res0.Type, 0) != "reflect.Method" { |
David Crawshaw | cc15840 | 2016-03-10 16:15:26 -0500 | [diff] [blame] | 3793 | return |
| 3794 | } |
| 3795 | |
| 3796 | Curfn.Func.ReflectMethod = true |
| 3797 | } |
| 3798 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3799 | func usefield(n *Node) { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3800 | if obj.Fieldtrack_enabled == 0 { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3801 | return |
| 3802 | } |
| 3803 | |
| 3804 | switch n.Op { |
| 3805 | default: |
Matthew Dempsky | c3dfad5 | 2016-03-07 08:23:55 -0800 | [diff] [blame] | 3806 | Fatalf("usefield %v", Oconv(n.Op, 0)) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3807 | |
Josh Bleecher Snyder | b09925b | 2015-04-01 09:38:44 -0700 | [diff] [blame] | 3808 | case ODOT, ODOTPTR: |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3809 | break |
| 3810 | } |
Keith Randall | 71d13a8 | 2016-03-02 20:54:41 -0800 | [diff] [blame] | 3811 | if n.Right == nil { |
| 3812 | // No field name. This DOTPTR was built by the compiler for access |
| 3813 | // to runtime data structures. Ignore. |
| 3814 | return |
| 3815 | } |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3816 | |
Russ Cox | 496ad0a | 2015-05-26 21:49:31 -0400 | [diff] [blame] | 3817 | t := n.Left.Type |
| 3818 | if Isptr[t.Etype] { |
| 3819 | t = t.Type |
| 3820 | } |
Russ Cox | f68d1df | 2015-08-17 21:38:46 -0400 | [diff] [blame] | 3821 | field := dotField[typeSym{t.Orig, n.Right.Sym}] |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3822 | if field == nil { |
HĂ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 3823 | Fatalf("usefield %v %v without paramfld", n.Left.Type, n.Right.Sym) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3824 | } |
Russ Cox | bed1f90 | 2015-03-02 16:03:26 -0500 | [diff] [blame] | 3825 | if field.Note == nil || !strings.Contains(*field.Note, "go:\"track\"") { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3826 | return |
| 3827 | } |
| 3828 | |
Matthew Dempsky | a2a4806 | 2016-03-10 16:15:44 -0800 | [diff] [blame] | 3829 | outer := n.Left.Type |
| 3830 | if Isptr[outer.Etype] { |
| 3831 | outer = outer.Type |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3832 | } |
Matthew Dempsky | a2a4806 | 2016-03-10 16:15:44 -0800 | [diff] [blame] | 3833 | if outer.Sym == nil { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3834 | Yyerror("tracked field must be in named struct type") |
| 3835 | } |
| 3836 | if !exportname(field.Sym.Name) { |
| 3837 | Yyerror("tracked field must be exported (upper case)") |
| 3838 | } |
| 3839 | |
Matthew Dempsky | a2a4806 | 2016-03-10 16:15:44 -0800 | [diff] [blame] | 3840 | sym := tracksym(outer, field) |
| 3841 | if Curfn.Func.FieldTrack == nil { |
| 3842 | Curfn.Func.FieldTrack = make(map[*Sym]struct{}) |
| 3843 | } |
| 3844 | Curfn.Func.FieldTrack[sym] = struct{}{} |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3845 | } |
| 3846 | |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 3847 | func candiscardlist(l Nodes) bool { |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 3848 | for _, n := range l.Slice() { |
| 3849 | if !candiscard(n) { |
Ian Lance Taylor | 1d5001a | 2016-02-27 14:31:33 -0800 | [diff] [blame] | 3850 | return false |
| 3851 | } |
| 3852 | } |
| 3853 | return true |
| 3854 | } |
| 3855 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3856 | func candiscard(n *Node) bool { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3857 | if n == nil { |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3858 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3859 | } |
| 3860 | |
| 3861 | switch n.Op { |
| 3862 | default: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3863 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3864 | |
| 3865 | // Discardable as long as the subpieces are. |
| 3866 | case ONAME, |
| 3867 | ONONAME, |
| 3868 | OTYPE, |
| 3869 | OPACK, |
| 3870 | OLITERAL, |
| 3871 | OADD, |
| 3872 | OSUB, |
| 3873 | OOR, |
| 3874 | OXOR, |
| 3875 | OADDSTR, |
| 3876 | OADDR, |
| 3877 | OANDAND, |
| 3878 | OARRAYBYTESTR, |
| 3879 | OARRAYRUNESTR, |
| 3880 | OSTRARRAYBYTE, |
| 3881 | OSTRARRAYRUNE, |
| 3882 | OCAP, |
| 3883 | OCMPIFACE, |
| 3884 | OCMPSTR, |
| 3885 | OCOMPLIT, |
| 3886 | OMAPLIT, |
| 3887 | OSTRUCTLIT, |
| 3888 | OARRAYLIT, |
| 3889 | OPTRLIT, |
| 3890 | OCONV, |
| 3891 | OCONVIFACE, |
| 3892 | OCONVNOP, |
| 3893 | ODOT, |
| 3894 | OEQ, |
| 3895 | ONE, |
| 3896 | OLT, |
| 3897 | OLE, |
| 3898 | OGT, |
| 3899 | OGE, |
| 3900 | OKEY, |
| 3901 | OLEN, |
| 3902 | OMUL, |
| 3903 | OLSH, |
| 3904 | ORSH, |
| 3905 | OAND, |
| 3906 | OANDNOT, |
| 3907 | ONEW, |
| 3908 | ONOT, |
| 3909 | OCOM, |
| 3910 | OPLUS, |
| 3911 | OMINUS, |
| 3912 | OOROR, |
| 3913 | OPAREN, |
| 3914 | ORUNESTR, |
| 3915 | OREAL, |
| 3916 | OIMAG, |
| 3917 | OCOMPLEX: |
| 3918 | break |
| 3919 | |
| 3920 | // 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] | 3921 | case ODIV, OMOD: |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 3922 | 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] | 3923 | break |
| 3924 | } |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 3925 | 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] | 3926 | break |
| 3927 | } |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3928 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3929 | |
| 3930 | // 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] | 3931 | case OMAKECHAN, OMAKEMAP: |
Russ Cox | 81d5810 | 2015-05-27 00:47:05 -0400 | [diff] [blame] | 3932 | 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] | 3933 | break |
| 3934 | } |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3935 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3936 | |
| 3937 | // Difficult to tell what sizes are okay. |
| 3938 | case OMAKESLICE: |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3939 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3940 | } |
| 3941 | |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 3942 | 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] | 3943 | return false |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3944 | } |
| 3945 | |
Russ Cox | dc7b54b | 2015-02-17 22:13:49 -0500 | [diff] [blame] | 3946 | return true |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3947 | } |
| 3948 | |
| 3949 | // rewrite |
| 3950 | // print(x, y, z) |
| 3951 | // into |
| 3952 | // func(a1, a2, a3) { |
| 3953 | // print(a1, a2, a3) |
| 3954 | // }(x, y, z) |
| 3955 | // and same for println. |
| 3956 | |
| 3957 | var walkprintfunc_prgen int |
| 3958 | |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3959 | func walkprintfunc(np **Node, init *Nodes) { |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3960 | n := *np |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3961 | |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 3962 | if n.Ninit.Len() != 0 { |
Ian Lance Taylor | e28a890 | 2016-03-07 22:54:46 -0800 | [diff] [blame] | 3963 | walkstmtlist(n.Ninit.Slice()) |
| 3964 | init.AppendNodes(&n.Ninit) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3965 | } |
| 3966 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3967 | t := Nod(OTFUNC, nil, nil) |
| 3968 | num := 0 |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 3969 | var printargs []*Node |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3970 | var a *Node |
| 3971 | var buf string |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 3972 | for _, n1 := range n.List.Slice() { |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3973 | buf = fmt.Sprintf("a%d", num) |
| 3974 | num++ |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 3975 | a = Nod(ODCLFIELD, newname(Lookup(buf)), typenod(n1.Type)) |
| 3976 | t.List.Append(a) |
Ian Lance Taylor | 132ebea | 2016-03-03 20:48:00 -0800 | [diff] [blame] | 3977 | printargs = append(printargs, a.Left) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3978 | } |
| 3979 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3980 | fn := Nod(ODCLFUNC, nil, nil) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3981 | walkprintfunc_prgen++ |
| 3982 | buf = fmt.Sprintf("print·%d", walkprintfunc_prgen) |
Russ Cox | bd4fff6 | 2015-05-27 10:42:55 -0400 | [diff] [blame] | 3983 | fn.Func.Nname = newname(Lookup(buf)) |
| 3984 | fn.Func.Nname.Name.Defn = fn |
| 3985 | fn.Func.Nname.Name.Param.Ntype = t |
| 3986 | declare(fn.Func.Nname, PFUNC) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3987 | |
Russ Cox | 382b44e | 2015-02-23 16:07:24 -0500 | [diff] [blame] | 3988 | oldfn := Curfn |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3989 | Curfn = nil |
| 3990 | funchdr(fn) |
| 3991 | |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 3992 | a = Nod(n.Op, nil, nil) |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 3993 | a.List.Set(printargs) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3994 | typecheck(&a, Etop) |
| 3995 | walkstmt(&a) |
| 3996 | |
Ian Lance Taylor | c63dbd8 | 2016-03-10 10:13:42 -0800 | [diff] [blame] | 3997 | fn.Nbody.Set1(a) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 3998 | |
| 3999 | funcbody(fn) |
| 4000 | |
| 4001 | typecheck(&fn, Etop) |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 4002 | typechecklist(fn.Nbody.Slice(), Etop) |
Ian Lance Taylor | f444b8a | 2016-03-09 20:29:21 -0800 | [diff] [blame] | 4003 | xtop = append(xtop, fn) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 4004 | Curfn = oldfn |
| 4005 | |
| 4006 | a = Nod(OCALL, nil, nil) |
Russ Cox | bd4fff6 | 2015-05-27 10:42:55 -0400 | [diff] [blame] | 4007 | a.Left = fn.Func.Nname |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 4008 | a.List.Set(n.List.Slice()) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 4009 | typecheck(&a, Etop) |
| 4010 | walkexpr(&a, init) |
| 4011 | *np = a |
| 4012 | } |