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