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