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