Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 1 | // Copyright 2015 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 ( |
Josh Bleecher Snyder | 35fb514 | 2015-08-10 12:15:52 -0700 | [diff] [blame] | 8 | "bytes" |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 9 | "fmt" |
Josh Bleecher Snyder | 35fb514 | 2015-08-10 12:15:52 -0700 | [diff] [blame] | 10 | "html" |
Josh Bleecher Snyder | d298209 | 2015-07-22 13:13:53 -0700 | [diff] [blame] | 11 | "os" |
Brad Fitzpatrick | 7af53d9 | 2015-07-10 10:47:28 -0600 | [diff] [blame] | 12 | "strings" |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 13 | |
Keith Randall | 067e8df | 2015-05-28 13:49:20 -0700 | [diff] [blame] | 14 | "cmd/compile/internal/ssa" |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 15 | "cmd/internal/obj" |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 16 | ) |
| 17 | |
Keith Randall | c0740fe | 2016-03-03 22:06:57 -0800 | [diff] [blame] | 18 | var ssaEnabled = true |
| 19 | |
Keith Randall | 2f57d0f | 2016-01-28 13:46:30 -0800 | [diff] [blame] | 20 | var ssaConfig *ssa.Config |
| 21 | var ssaExp ssaExport |
| 22 | |
David Chase | 378a863 | 2016-02-25 13:10:51 -0500 | [diff] [blame] | 23 | func initssa() *ssa.Config { |
| 24 | ssaExp.unimplemented = false |
| 25 | ssaExp.mustImplement = true |
| 26 | if ssaConfig == nil { |
| 27 | ssaConfig = ssa.NewConfig(Thearch.Thestring, &ssaExp, Ctxt, Debug['N'] == 0) |
| 28 | } |
| 29 | return ssaConfig |
| 30 | } |
| 31 | |
Keith Randall | 5b355a7 | 2015-12-11 20:41:52 -0800 | [diff] [blame] | 32 | func shouldssa(fn *Node) bool { |
Keith Randall | 4c9a470 | 2016-03-21 22:57:26 -0700 | [diff] [blame] | 33 | switch Thearch.Thestring { |
| 34 | default: |
| 35 | // Only available for testing. |
| 36 | if os.Getenv("SSATEST") == "" { |
| 37 | return false |
| 38 | } |
| 39 | // Generally available. |
| 40 | case "amd64": |
Keith Randall | 5b355a7 | 2015-12-11 20:41:52 -0800 | [diff] [blame] | 41 | } |
Keith Randall | c0740fe | 2016-03-03 22:06:57 -0800 | [diff] [blame] | 42 | if !ssaEnabled { |
| 43 | return false |
| 44 | } |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 45 | |
David Chase | e99dd52 | 2015-10-19 11:36:07 -0400 | [diff] [blame] | 46 | // Environment variable control of SSA CG |
| 47 | // 1. IF GOSSAFUNC == current function name THEN |
| 48 | // compile this function with SSA and log output to ssa.html |
| 49 | |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 50 | // 2. IF GOSSAHASH == "" THEN |
David Chase | e99dd52 | 2015-10-19 11:36:07 -0400 | [diff] [blame] | 51 | // compile this function (and everything else) with SSA |
| 52 | |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 53 | // 3. IF GOSSAHASH == "n" or "N" |
David Chase | e99dd52 | 2015-10-19 11:36:07 -0400 | [diff] [blame] | 54 | // IF GOSSAPKG == current package name THEN |
| 55 | // compile this function (and everything in this package) with SSA |
| 56 | // ELSE |
| 57 | // use the old back end for this function. |
| 58 | // This is for compatibility with existing test harness and should go away. |
| 59 | |
| 60 | // 4. IF GOSSAHASH is a suffix of the binary-rendered SHA1 hash of the function name THEN |
| 61 | // compile this function with SSA |
| 62 | // ELSE |
| 63 | // compile this function with the old back end. |
| 64 | |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 65 | // Plan is for 3 to be removed when the tests are revised. |
| 66 | // SSA is now default, and is disabled by setting |
| 67 | // GOSSAHASH to n or N, or selectively with strings of |
| 68 | // 0 and 1. |
David Chase | e99dd52 | 2015-10-19 11:36:07 -0400 | [diff] [blame] | 69 | |
Keith Randall | 5b355a7 | 2015-12-11 20:41:52 -0800 | [diff] [blame] | 70 | name := fn.Func.Nname.Sym.Name |
| 71 | |
| 72 | funcname := os.Getenv("GOSSAFUNC") |
| 73 | if funcname != "" { |
| 74 | // If GOSSAFUNC is set, compile only that function. |
| 75 | return name == funcname |
| 76 | } |
| 77 | |
| 78 | pkg := os.Getenv("GOSSAPKG") |
| 79 | if pkg != "" { |
| 80 | // If GOSSAPKG is set, compile only that package. |
| 81 | return localpkg.Name == pkg |
| 82 | } |
| 83 | |
David Chase | 378a863 | 2016-02-25 13:10:51 -0500 | [diff] [blame] | 84 | return initssa().DebugHashMatch("GOSSAHASH", name) |
Keith Randall | 5b355a7 | 2015-12-11 20:41:52 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // buildssa builds an SSA function. |
| 88 | func buildssa(fn *Node) *ssa.Func { |
| 89 | name := fn.Func.Nname.Sym.Name |
Keith Randall | 5968180 | 2016-03-01 13:47:48 -0800 | [diff] [blame] | 90 | printssa := name == os.Getenv("GOSSAFUNC") |
Keith Randall | 5b355a7 | 2015-12-11 20:41:52 -0800 | [diff] [blame] | 91 | if printssa { |
Josh Bleecher Snyder | e0ac5c5 | 2015-07-20 18:42:45 -0700 | [diff] [blame] | 92 | fmt.Println("generating SSA for", name) |
Ian Lance Taylor | 55c65d4 | 2016-03-04 13:16:48 -0800 | [diff] [blame] | 93 | dumplist("buildssa-enter", fn.Func.Enter) |
| 94 | dumplist("buildssa-body", fn.Nbody) |
| 95 | dumplist("buildssa-exit", fn.Func.Exit) |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 96 | } |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 97 | |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 98 | var s state |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 99 | s.pushLine(fn.Lineno) |
| 100 | defer s.popLine() |
| 101 | |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 102 | if fn.Func.Pragma&CgoUnsafeArgs != 0 { |
| 103 | s.cgoUnsafeArgs = true |
| 104 | } |
Keith Randall | 15ed37d | 2016-03-16 21:51:17 -0700 | [diff] [blame] | 105 | if fn.Func.Pragma&Nowritebarrier != 0 { |
| 106 | s.noWB = true |
| 107 | } |
| 108 | defer func() { |
| 109 | if s.WBLineno != 0 { |
| 110 | fn.Func.WBLineno = s.WBLineno |
| 111 | } |
| 112 | }() |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 113 | // TODO(khr): build config just once at the start of the compiler binary |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 114 | |
Keith Randall | 2f57d0f | 2016-01-28 13:46:30 -0800 | [diff] [blame] | 115 | ssaExp.log = printssa |
David Chase | 378a863 | 2016-02-25 13:10:51 -0500 | [diff] [blame] | 116 | |
| 117 | s.config = initssa() |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 118 | s.f = s.config.NewFunc() |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 119 | s.f.Name = name |
David Chase | 8824dcc | 2015-10-08 12:39:56 -0400 | [diff] [blame] | 120 | s.exitCode = fn.Func.Exit |
Keith Randall | 74e568f | 2015-11-09 21:35:40 -0800 | [diff] [blame] | 121 | s.panics = map[funcLine]*ssa.Block{} |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 122 | |
Josh Bleecher Snyder | 35fb514 | 2015-08-10 12:15:52 -0700 | [diff] [blame] | 123 | if name == os.Getenv("GOSSAFUNC") { |
| 124 | // TODO: tempfile? it is handy to have the location |
| 125 | // of this file be stable, so you can just reload in the browser. |
Keith Randall | da8af47 | 2016-01-13 11:14:57 -0800 | [diff] [blame] | 126 | s.config.HTML = ssa.NewHTMLWriter("ssa.html", s.config, name) |
Josh Bleecher Snyder | 35fb514 | 2015-08-10 12:15:52 -0700 | [diff] [blame] | 127 | // TODO: generate and print a mapping from nodes to values and blocks |
| 128 | } |
| 129 | defer func() { |
Keith Randall | 5b355a7 | 2015-12-11 20:41:52 -0800 | [diff] [blame] | 130 | if !printssa { |
Josh Bleecher Snyder | 35fb514 | 2015-08-10 12:15:52 -0700 | [diff] [blame] | 131 | s.config.HTML.Close() |
| 132 | } |
| 133 | }() |
| 134 | |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 135 | // Allocate starting block |
| 136 | s.f.Entry = s.f.NewBlock(ssa.BlockPlain) |
| 137 | |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 138 | // Allocate starting values |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 139 | s.labels = map[string]*ssaLabel{} |
| 140 | s.labeledNodes = map[*Node]*ssaLabel{} |
Keith Randall | 02f4d0a | 2015-11-02 08:10:26 -0800 | [diff] [blame] | 141 | s.startmem = s.entryNewValue0(ssa.OpInitMem, ssa.TypeMem) |
Josh Bleecher Snyder | 85e0329 | 2015-07-30 11:03:05 -0700 | [diff] [blame] | 142 | s.sp = s.entryNewValue0(ssa.OpSP, Types[TUINTPTR]) // TODO: use generic pointer type (unsafe.Pointer?) instead |
| 143 | s.sb = s.entryNewValue0(ssa.OpSB, Types[TUINTPTR]) |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 144 | |
David Chase | 956f319 | 2015-09-11 16:40:05 -0400 | [diff] [blame] | 145 | s.startBlock(s.f.Entry) |
| 146 | s.vars[&memVar] = s.startmem |
| 147 | |
Todd Neal | d076ef7 | 2015-10-15 20:25:32 -0500 | [diff] [blame] | 148 | s.varsyms = map[*Node]interface{}{} |
| 149 | |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 150 | // Generate addresses of local declarations |
| 151 | s.decladdrs = map[*Node]*ssa.Value{} |
Keith Randall | 4fffd456 | 2016-02-29 13:31:48 -0800 | [diff] [blame] | 152 | for _, n := range fn.Func.Dcl { |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 153 | switch n.Class { |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 154 | case PPARAM, PPARAMOUT: |
Todd Neal | d076ef7 | 2015-10-15 20:25:32 -0500 | [diff] [blame] | 155 | aux := s.lookupSymbol(n, &ssa.ArgSymbol{Typ: n.Type, Node: n}) |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 156 | s.decladdrs[n] = s.entryNewValue1A(ssa.OpAddr, Ptrto(n.Type), aux, s.sp) |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 157 | if n.Class == PPARAMOUT && s.canSSA(n) { |
| 158 | // Save ssa-able PPARAMOUT variables so we can |
| 159 | // store them back to the stack at the end of |
| 160 | // the function. |
| 161 | s.returns = append(s.returns, n) |
| 162 | } |
David Chase | 956f319 | 2015-09-11 16:40:05 -0400 | [diff] [blame] | 163 | case PAUTO | PHEAP: |
| 164 | // TODO this looks wrong for PAUTO|PHEAP, no vardef, but also no definition |
Todd Neal | d076ef7 | 2015-10-15 20:25:32 -0500 | [diff] [blame] | 165 | aux := s.lookupSymbol(n, &ssa.AutoSymbol{Typ: n.Type, Node: n}) |
David Chase | 956f319 | 2015-09-11 16:40:05 -0400 | [diff] [blame] | 166 | s.decladdrs[n] = s.entryNewValue1A(ssa.OpAddr, Ptrto(n.Type), aux, s.sp) |
David Chase | 8824dcc | 2015-10-08 12:39:56 -0400 | [diff] [blame] | 167 | case PPARAM | PHEAP, PPARAMOUT | PHEAP: |
| 168 | // This ends up wrong, have to do it at the PARAM node instead. |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 169 | case PAUTO: |
Keith Randall | d2107fc | 2015-08-24 02:16:19 -0700 | [diff] [blame] | 170 | // processed at each use, to prevent Addr coming |
| 171 | // before the decl. |
Keith Randall | c3eb1a7 | 2015-09-06 13:42:26 -0700 | [diff] [blame] | 172 | case PFUNC: |
| 173 | // local function - already handled by frontend |
Daniel Morsing | be2a3e2 | 2015-07-01 20:37:25 +0100 | [diff] [blame] | 174 | default: |
| 175 | str := "" |
| 176 | if n.Class&PHEAP != 0 { |
| 177 | str = ",heap" |
| 178 | } |
Josh Bleecher Snyder | 5844603 | 2015-08-23 20:29:43 -0700 | [diff] [blame] | 179 | s.Unimplementedf("local variable with class %s%s unimplemented", classnames[n.Class&^PHEAP], str) |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 182 | |
| 183 | // Convert the AST-based IR to the SSA-based IR |
Keith Randall | 4fffd456 | 2016-02-29 13:31:48 -0800 | [diff] [blame] | 184 | s.stmts(fn.Func.Enter) |
Keith Randall | 9d854fd | 2016-03-01 12:50:17 -0800 | [diff] [blame] | 185 | s.stmts(fn.Nbody) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 186 | |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 187 | // fallthrough to exit |
Keith Randall | a7cfc759 | 2015-09-08 16:04:37 -0700 | [diff] [blame] | 188 | if s.curBlock != nil { |
Keith Randall | ddc6b64 | 2016-03-09 19:27:57 -0800 | [diff] [blame] | 189 | s.pushLine(fn.Func.Endlineno) |
| 190 | s.exit() |
| 191 | s.popLine() |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 194 | // Check that we used all labels |
| 195 | for name, lab := range s.labels { |
| 196 | if !lab.used() && !lab.reported { |
Robert Griesemer | b83f397 | 2016-03-02 11:01:25 -0800 | [diff] [blame] | 197 | yyerrorl(lab.defNode.Lineno, "label %v defined and not used", name) |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 198 | lab.reported = true |
| 199 | } |
| 200 | if lab.used() && !lab.defined() && !lab.reported { |
Robert Griesemer | b83f397 | 2016-03-02 11:01:25 -0800 | [diff] [blame] | 201 | yyerrorl(lab.useNode.Lineno, "label %v not defined", name) |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 202 | lab.reported = true |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // Check any forward gotos. Non-forward gotos have already been checked. |
| 207 | for _, n := range s.fwdGotos { |
| 208 | lab := s.labels[n.Left.Sym.Name] |
| 209 | // If the label is undefined, we have already have printed an error. |
| 210 | if lab.defined() { |
| 211 | s.checkgoto(n, lab.defNode) |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | if nerrors > 0 { |
Keith Randall | 4c5459d | 2016-01-28 16:11:56 -0800 | [diff] [blame] | 216 | s.f.Free() |
Keith Randall | 5b355a7 | 2015-12-11 20:41:52 -0800 | [diff] [blame] | 217 | return nil |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 220 | // Link up variable uses to variable definitions |
| 221 | s.linkForwardReferences() |
| 222 | |
David Chase | 8824dcc | 2015-10-08 12:39:56 -0400 | [diff] [blame] | 223 | // Don't carry reference this around longer than necessary |
Keith Randall | 4fffd456 | 2016-02-29 13:31:48 -0800 | [diff] [blame] | 224 | s.exitCode = Nodes{} |
David Chase | 8824dcc | 2015-10-08 12:39:56 -0400 | [diff] [blame] | 225 | |
Josh Bleecher Snyder | 983bc8d | 2015-07-17 16:47:43 +0000 | [diff] [blame] | 226 | // Main call to ssa package to compile function |
| 227 | ssa.Compile(s.f) |
| 228 | |
Keith Randall | 5b355a7 | 2015-12-11 20:41:52 -0800 | [diff] [blame] | 229 | return s.f |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 232 | type state struct { |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 233 | // configuration (arch) information |
| 234 | config *ssa.Config |
| 235 | |
| 236 | // function we're building |
| 237 | f *ssa.Func |
| 238 | |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 239 | // labels and labeled control flow nodes (OFOR, OSWITCH, OSELECT) in f |
| 240 | labels map[string]*ssaLabel |
| 241 | labeledNodes map[*Node]*ssaLabel |
| 242 | |
| 243 | // gotos that jump forward; required for deferred checkgoto calls |
| 244 | fwdGotos []*Node |
David Chase | 8824dcc | 2015-10-08 12:39:56 -0400 | [diff] [blame] | 245 | // Code that must precede any return |
| 246 | // (e.g., copying value of heap-escaped paramout back to true paramout) |
Keith Randall | 4fffd456 | 2016-02-29 13:31:48 -0800 | [diff] [blame] | 247 | exitCode Nodes |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 248 | |
| 249 | // unlabeled break and continue statement tracking |
| 250 | breakTo *ssa.Block // current target for plain break statement |
| 251 | continueTo *ssa.Block // current target for plain continue statement |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 252 | |
| 253 | // current location where we're interpreting the AST |
| 254 | curBlock *ssa.Block |
| 255 | |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 256 | // variable assignments in the current block (map from variable symbol to ssa value) |
| 257 | // *Node is the unique identifier (an ONAME Node) for the variable. |
| 258 | vars map[*Node]*ssa.Value |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 259 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 260 | // all defined variables at the end of each block. Indexed by block ID. |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 261 | defvars []map[*Node]*ssa.Value |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 262 | |
Keith Randall | d2107fc | 2015-08-24 02:16:19 -0700 | [diff] [blame] | 263 | // addresses of PPARAM and PPARAMOUT variables. |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 264 | decladdrs map[*Node]*ssa.Value |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 265 | |
Todd Neal | d076ef7 | 2015-10-15 20:25:32 -0500 | [diff] [blame] | 266 | // symbols for PEXTERN, PAUTO and PPARAMOUT variables so they can be reused. |
| 267 | varsyms map[*Node]interface{} |
| 268 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 269 | // starting values. Memory, stack pointer, and globals pointer |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 270 | startmem *ssa.Value |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 271 | sp *ssa.Value |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 272 | sb *ssa.Value |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 273 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 274 | // line number stack. The current line number is top of stack |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 275 | line []int32 |
Keith Randall | 74e568f | 2015-11-09 21:35:40 -0800 | [diff] [blame] | 276 | |
| 277 | // list of panic calls by function name and line number. |
| 278 | // Used to deduplicate panic calls. |
| 279 | panics map[funcLine]*ssa.Block |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 280 | |
| 281 | // list of FwdRef values. |
| 282 | fwdRefs []*ssa.Value |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 283 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 284 | // list of PPARAMOUT (return) variables. Does not include PPARAM|PHEAP vars. |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 285 | returns []*Node |
| 286 | |
| 287 | cgoUnsafeArgs bool |
Keith Randall | 15ed37d | 2016-03-16 21:51:17 -0700 | [diff] [blame] | 288 | noWB bool |
| 289 | WBLineno int32 // line number of first write barrier. 0=no write barriers |
Keith Randall | 74e568f | 2015-11-09 21:35:40 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | type funcLine struct { |
| 293 | f *Node |
| 294 | line int32 |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 297 | type ssaLabel struct { |
| 298 | target *ssa.Block // block identified by this label |
| 299 | breakTarget *ssa.Block // block to break to in control flow node identified by this label |
| 300 | continueTarget *ssa.Block // block to continue to in control flow node identified by this label |
| 301 | defNode *Node // label definition Node (OLABEL) |
| 302 | // Label use Node (OGOTO, OBREAK, OCONTINUE). |
| 303 | // Used only for error detection and reporting. |
| 304 | // There might be multiple uses, but we only need to track one. |
| 305 | useNode *Node |
| 306 | reported bool // reported indicates whether an error has already been reported for this label |
| 307 | } |
| 308 | |
| 309 | // defined reports whether the label has a definition (OLABEL node). |
| 310 | func (l *ssaLabel) defined() bool { return l.defNode != nil } |
| 311 | |
| 312 | // used reports whether the label has a use (OGOTO, OBREAK, or OCONTINUE node). |
| 313 | func (l *ssaLabel) used() bool { return l.useNode != nil } |
| 314 | |
| 315 | // label returns the label associated with sym, creating it if necessary. |
| 316 | func (s *state) label(sym *Sym) *ssaLabel { |
| 317 | lab := s.labels[sym.Name] |
| 318 | if lab == nil { |
| 319 | lab = new(ssaLabel) |
| 320 | s.labels[sym.Name] = lab |
| 321 | } |
| 322 | return lab |
| 323 | } |
| 324 | |
Keith Randall | da8af47 | 2016-01-13 11:14:57 -0800 | [diff] [blame] | 325 | func (s *state) Logf(msg string, args ...interface{}) { s.config.Logf(msg, args...) } |
David Chase | 88b230e | 2016-01-29 14:44:15 -0500 | [diff] [blame] | 326 | func (s *state) Log() bool { return s.config.Log() } |
Keith Randall | da8af47 | 2016-01-13 11:14:57 -0800 | [diff] [blame] | 327 | func (s *state) Fatalf(msg string, args ...interface{}) { s.config.Fatalf(s.peekLine(), msg, args...) } |
| 328 | func (s *state) Unimplementedf(msg string, args ...interface{}) { |
| 329 | s.config.Unimplementedf(s.peekLine(), msg, args...) |
| 330 | } |
Todd Neal | 98b88de | 2016-03-13 23:04:31 -0500 | [diff] [blame] | 331 | func (s *state) Warnl(line int32, msg string, args ...interface{}) { s.config.Warnl(line, msg, args...) } |
| 332 | func (s *state) Debug_checknil() bool { return s.config.Debug_checknil() } |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 333 | |
Keith Randall | 269baa9 | 2015-09-17 10:31:16 -0700 | [diff] [blame] | 334 | var ( |
| 335 | // dummy node for the memory variable |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 336 | memVar = Node{Op: ONAME, Class: Pxxx, Sym: &Sym{Name: "mem"}} |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 337 | |
Keith Randall | 269baa9 | 2015-09-17 10:31:16 -0700 | [diff] [blame] | 338 | // dummy nodes for temporary variables |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 339 | ptrVar = Node{Op: ONAME, Class: Pxxx, Sym: &Sym{Name: "ptr"}} |
| 340 | capVar = Node{Op: ONAME, Class: Pxxx, Sym: &Sym{Name: "cap"}} |
| 341 | typVar = Node{Op: ONAME, Class: Pxxx, Sym: &Sym{Name: "typ"}} |
| 342 | idataVar = Node{Op: ONAME, Class: Pxxx, Sym: &Sym{Name: "idata"}} |
| 343 | okVar = Node{Op: ONAME, Class: Pxxx, Sym: &Sym{Name: "ok"}} |
Keith Randall | 69a7c152 | 2016-03-21 15:24:08 -0700 | [diff] [blame] | 344 | deltaVar = Node{Op: ONAME, Class: Pxxx, Sym: &Sym{Name: "delta"}} |
Keith Randall | 269baa9 | 2015-09-17 10:31:16 -0700 | [diff] [blame] | 345 | ) |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 346 | |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 347 | // startBlock sets the current block we're generating code in to b. |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 348 | func (s *state) startBlock(b *ssa.Block) { |
| 349 | if s.curBlock != nil { |
Josh Bleecher Snyder | 37ddc27 | 2015-06-24 14:03:39 -0700 | [diff] [blame] | 350 | s.Fatalf("starting block %v when block %v has not ended", b, s.curBlock) |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 351 | } |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 352 | s.curBlock = b |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 353 | s.vars = map[*Node]*ssa.Value{} |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | // endBlock marks the end of generating code for the current block. |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 357 | // Returns the (former) current block. Returns nil if there is no current |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 358 | // block, i.e. if no code flows to the current execution point. |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 359 | func (s *state) endBlock() *ssa.Block { |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 360 | b := s.curBlock |
| 361 | if b == nil { |
| 362 | return nil |
| 363 | } |
| 364 | for len(s.defvars) <= int(b.ID) { |
| 365 | s.defvars = append(s.defvars, nil) |
| 366 | } |
| 367 | s.defvars[b.ID] = s.vars |
| 368 | s.curBlock = nil |
| 369 | s.vars = nil |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 370 | b.Line = s.peekLine() |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 371 | return b |
| 372 | } |
| 373 | |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 374 | // pushLine pushes a line number on the line number stack. |
| 375 | func (s *state) pushLine(line int32) { |
| 376 | s.line = append(s.line, line) |
| 377 | } |
| 378 | |
| 379 | // popLine pops the top of the line number stack. |
| 380 | func (s *state) popLine() { |
| 381 | s.line = s.line[:len(s.line)-1] |
| 382 | } |
| 383 | |
| 384 | // peekLine peek the top of the line number stack. |
| 385 | func (s *state) peekLine() int32 { |
| 386 | return s.line[len(s.line)-1] |
| 387 | } |
| 388 | |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 389 | func (s *state) Error(msg string, args ...interface{}) { |
Robert Griesemer | b83f397 | 2016-03-02 11:01:25 -0800 | [diff] [blame] | 390 | yyerrorl(s.peekLine(), msg, args...) |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Keith Randall | 8f22b52 | 2015-06-11 21:29:25 -0700 | [diff] [blame] | 393 | // newValue0 adds a new value with no arguments to the current block. |
| 394 | func (s *state) newValue0(op ssa.Op, t ssa.Type) *ssa.Value { |
| 395 | return s.curBlock.NewValue0(s.peekLine(), op, t) |
| 396 | } |
| 397 | |
| 398 | // newValue0A adds a new value with no arguments and an aux value to the current block. |
| 399 | func (s *state) newValue0A(op ssa.Op, t ssa.Type, aux interface{}) *ssa.Value { |
| 400 | return s.curBlock.NewValue0A(s.peekLine(), op, t, aux) |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 401 | } |
| 402 | |
Todd Neal | 991036a | 2015-09-03 18:24:22 -0500 | [diff] [blame] | 403 | // newValue0I adds a new value with no arguments and an auxint value to the current block. |
| 404 | func (s *state) newValue0I(op ssa.Op, t ssa.Type, auxint int64) *ssa.Value { |
| 405 | return s.curBlock.NewValue0I(s.peekLine(), op, t, auxint) |
| 406 | } |
| 407 | |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 408 | // newValue1 adds a new value with one argument to the current block. |
Keith Randall | 8f22b52 | 2015-06-11 21:29:25 -0700 | [diff] [blame] | 409 | func (s *state) newValue1(op ssa.Op, t ssa.Type, arg *ssa.Value) *ssa.Value { |
| 410 | return s.curBlock.NewValue1(s.peekLine(), op, t, arg) |
| 411 | } |
| 412 | |
| 413 | // newValue1A adds a new value with one argument and an aux value to the current block. |
| 414 | func (s *state) newValue1A(op ssa.Op, t ssa.Type, aux interface{}, arg *ssa.Value) *ssa.Value { |
| 415 | return s.curBlock.NewValue1A(s.peekLine(), op, t, aux, arg) |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 416 | } |
| 417 | |
Keith Randall | cd7e059 | 2015-07-15 21:33:49 -0700 | [diff] [blame] | 418 | // newValue1I adds a new value with one argument and an auxint value to the current block. |
| 419 | func (s *state) newValue1I(op ssa.Op, t ssa.Type, aux int64, arg *ssa.Value) *ssa.Value { |
| 420 | return s.curBlock.NewValue1I(s.peekLine(), op, t, aux, arg) |
| 421 | } |
| 422 | |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 423 | // newValue2 adds a new value with two arguments to the current block. |
Keith Randall | 8f22b52 | 2015-06-11 21:29:25 -0700 | [diff] [blame] | 424 | func (s *state) newValue2(op ssa.Op, t ssa.Type, arg0, arg1 *ssa.Value) *ssa.Value { |
| 425 | return s.curBlock.NewValue2(s.peekLine(), op, t, arg0, arg1) |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 426 | } |
| 427 | |
Daniel Morsing | 66b4781 | 2015-06-27 15:45:20 +0100 | [diff] [blame] | 428 | // newValue2I adds a new value with two arguments and an auxint value to the current block. |
| 429 | func (s *state) newValue2I(op ssa.Op, t ssa.Type, aux int64, arg0, arg1 *ssa.Value) *ssa.Value { |
| 430 | return s.curBlock.NewValue2I(s.peekLine(), op, t, aux, arg0, arg1) |
| 431 | } |
| 432 | |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 433 | // newValue3 adds a new value with three arguments to the current block. |
Keith Randall | 8f22b52 | 2015-06-11 21:29:25 -0700 | [diff] [blame] | 434 | func (s *state) newValue3(op ssa.Op, t ssa.Type, arg0, arg1, arg2 *ssa.Value) *ssa.Value { |
| 435 | return s.curBlock.NewValue3(s.peekLine(), op, t, arg0, arg1, arg2) |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 436 | } |
| 437 | |
Keith Randall | d4cc51d | 2015-08-14 21:47:20 -0700 | [diff] [blame] | 438 | // newValue3I adds a new value with three arguments and an auxint value to the current block. |
| 439 | func (s *state) newValue3I(op ssa.Op, t ssa.Type, aux int64, arg0, arg1, arg2 *ssa.Value) *ssa.Value { |
| 440 | return s.curBlock.NewValue3I(s.peekLine(), op, t, aux, arg0, arg1, arg2) |
| 441 | } |
| 442 | |
Todd Neal | 991036a | 2015-09-03 18:24:22 -0500 | [diff] [blame] | 443 | // entryNewValue0 adds a new value with no arguments to the entry block. |
Keith Randall | 8f22b52 | 2015-06-11 21:29:25 -0700 | [diff] [blame] | 444 | func (s *state) entryNewValue0(op ssa.Op, t ssa.Type) *ssa.Value { |
| 445 | return s.f.Entry.NewValue0(s.peekLine(), op, t) |
| 446 | } |
| 447 | |
Todd Neal | 991036a | 2015-09-03 18:24:22 -0500 | [diff] [blame] | 448 | // entryNewValue0A adds a new value with no arguments and an aux value to the entry block. |
Keith Randall | 8f22b52 | 2015-06-11 21:29:25 -0700 | [diff] [blame] | 449 | func (s *state) entryNewValue0A(op ssa.Op, t ssa.Type, aux interface{}) *ssa.Value { |
| 450 | return s.f.Entry.NewValue0A(s.peekLine(), op, t, aux) |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 451 | } |
| 452 | |
Todd Neal | 991036a | 2015-09-03 18:24:22 -0500 | [diff] [blame] | 453 | // entryNewValue0I adds a new value with no arguments and an auxint value to the entry block. |
| 454 | func (s *state) entryNewValue0I(op ssa.Op, t ssa.Type, auxint int64) *ssa.Value { |
| 455 | return s.f.Entry.NewValue0I(s.peekLine(), op, t, auxint) |
| 456 | } |
| 457 | |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 458 | // entryNewValue1 adds a new value with one argument to the entry block. |
Keith Randall | 8f22b52 | 2015-06-11 21:29:25 -0700 | [diff] [blame] | 459 | func (s *state) entryNewValue1(op ssa.Op, t ssa.Type, arg *ssa.Value) *ssa.Value { |
| 460 | return s.f.Entry.NewValue1(s.peekLine(), op, t, arg) |
| 461 | } |
| 462 | |
| 463 | // entryNewValue1 adds a new value with one argument and an auxint value to the entry block. |
| 464 | func (s *state) entryNewValue1I(op ssa.Op, t ssa.Type, auxint int64, arg *ssa.Value) *ssa.Value { |
| 465 | return s.f.Entry.NewValue1I(s.peekLine(), op, t, auxint, arg) |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 466 | } |
| 467 | |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 468 | // entryNewValue1A adds a new value with one argument and an aux value to the entry block. |
| 469 | func (s *state) entryNewValue1A(op ssa.Op, t ssa.Type, aux interface{}, arg *ssa.Value) *ssa.Value { |
| 470 | return s.f.Entry.NewValue1A(s.peekLine(), op, t, aux, arg) |
| 471 | } |
| 472 | |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 473 | // entryNewValue2 adds a new value with two arguments to the entry block. |
Keith Randall | 8f22b52 | 2015-06-11 21:29:25 -0700 | [diff] [blame] | 474 | func (s *state) entryNewValue2(op ssa.Op, t ssa.Type, arg0, arg1 *ssa.Value) *ssa.Value { |
| 475 | return s.f.Entry.NewValue2(s.peekLine(), op, t, arg0, arg1) |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 476 | } |
| 477 | |
Josh Bleecher Snyder | cea4414 | 2015-09-08 16:52:25 -0700 | [diff] [blame] | 478 | // const* routines add a new const value to the entry block. |
Josh Bleecher Snyder | 3921427 | 2016-03-06 18:06:09 -0800 | [diff] [blame] | 479 | func (s *state) constSlice(t ssa.Type) *ssa.Value { return s.f.ConstSlice(s.peekLine(), t) } |
| 480 | func (s *state) constInterface(t ssa.Type) *ssa.Value { return s.f.ConstInterface(s.peekLine(), t) } |
| 481 | func (s *state) constNil(t ssa.Type) *ssa.Value { return s.f.ConstNil(s.peekLine(), t) } |
| 482 | func (s *state) constEmptyString(t ssa.Type) *ssa.Value { return s.f.ConstEmptyString(s.peekLine(), t) } |
Josh Bleecher Snyder | cea4414 | 2015-09-08 16:52:25 -0700 | [diff] [blame] | 483 | func (s *state) constBool(c bool) *ssa.Value { |
| 484 | return s.f.ConstBool(s.peekLine(), Types[TBOOL], c) |
| 485 | } |
Keith Randall | 9cb332e | 2015-07-28 14:19:20 -0700 | [diff] [blame] | 486 | func (s *state) constInt8(t ssa.Type, c int8) *ssa.Value { |
| 487 | return s.f.ConstInt8(s.peekLine(), t, c) |
| 488 | } |
| 489 | func (s *state) constInt16(t ssa.Type, c int16) *ssa.Value { |
| 490 | return s.f.ConstInt16(s.peekLine(), t, c) |
| 491 | } |
| 492 | func (s *state) constInt32(t ssa.Type, c int32) *ssa.Value { |
| 493 | return s.f.ConstInt32(s.peekLine(), t, c) |
| 494 | } |
| 495 | func (s *state) constInt64(t ssa.Type, c int64) *ssa.Value { |
| 496 | return s.f.ConstInt64(s.peekLine(), t, c) |
| 497 | } |
David Chase | 997a9f3 | 2015-08-12 16:38:11 -0400 | [diff] [blame] | 498 | func (s *state) constFloat32(t ssa.Type, c float64) *ssa.Value { |
| 499 | return s.f.ConstFloat32(s.peekLine(), t, c) |
| 500 | } |
| 501 | func (s *state) constFloat64(t ssa.Type, c float64) *ssa.Value { |
| 502 | return s.f.ConstFloat64(s.peekLine(), t, c) |
| 503 | } |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 504 | func (s *state) constInt(t ssa.Type, c int64) *ssa.Value { |
Keith Randall | 9cb332e | 2015-07-28 14:19:20 -0700 | [diff] [blame] | 505 | if s.config.IntSize == 8 { |
| 506 | return s.constInt64(t, c) |
| 507 | } |
| 508 | if int64(int32(c)) != c { |
| 509 | s.Fatalf("integer constant too big %d", c) |
| 510 | } |
| 511 | return s.constInt32(t, int32(c)) |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 512 | } |
| 513 | |
Keith Randall | 4fffd456 | 2016-02-29 13:31:48 -0800 | [diff] [blame] | 514 | func (s *state) stmts(a Nodes) { |
| 515 | for _, x := range a.Slice() { |
| 516 | s.stmt(x) |
| 517 | } |
| 518 | } |
| 519 | |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 520 | // ssaStmtList converts the statement n to SSA and adds it to s. |
Ian Lance Taylor | c4012b6 | 2016-03-08 10:26:20 -0800 | [diff] [blame] | 521 | func (s *state) stmtList(l Nodes) { |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 522 | for _, n := range l.Slice() { |
| 523 | s.stmt(n) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | // ssaStmt converts the statement n to SSA and adds it to s. |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 528 | func (s *state) stmt(n *Node) { |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 529 | s.pushLine(n.Lineno) |
| 530 | defer s.popLine() |
| 531 | |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 532 | // If s.curBlock is nil, then we're about to generate dead code. |
| 533 | // We can't just short-circuit here, though, |
| 534 | // because we check labels and gotos as part of SSA generation. |
| 535 | // Provide a block for the dead code so that we don't have |
| 536 | // to add special cases everywhere else. |
| 537 | if s.curBlock == nil { |
| 538 | dead := s.f.NewBlock(ssa.BlockPlain) |
| 539 | s.startBlock(dead) |
| 540 | } |
| 541 | |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 542 | s.stmtList(n.Ninit) |
| 543 | switch n.Op { |
| 544 | |
| 545 | case OBLOCK: |
| 546 | s.stmtList(n.List) |
| 547 | |
Josh Bleecher Snyder | 2574e4a | 2015-07-16 13:25:36 -0600 | [diff] [blame] | 548 | // No-ops |
Todd Neal | 67e43c1 | 2015-08-28 21:19:40 -0500 | [diff] [blame] | 549 | case OEMPTY, ODCLCONST, ODCLTYPE, OFALL: |
Brad Fitzpatrick | 7af53d9 | 2015-07-10 10:47:28 -0600 | [diff] [blame] | 550 | |
Josh Bleecher Snyder | 2574e4a | 2015-07-16 13:25:36 -0600 | [diff] [blame] | 551 | // Expression statements |
| 552 | case OCALLFUNC, OCALLMETH, OCALLINTER: |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 553 | s.call(n, callNormal) |
Keith Randall | fb54e03 | 2016-02-24 16:19:20 -0800 | [diff] [blame] | 554 | if n.Op == OCALLFUNC && n.Left.Op == ONAME && n.Left.Class == PFUNC && |
| 555 | (compiling_runtime != 0 && n.Left.Sym.Name == "throw" || |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 556 | n.Left.Sym.Pkg == Runtimepkg && (n.Left.Sym.Name == "gopanic" || n.Left.Sym.Name == "selectgo" || n.Left.Sym.Name == "block")) { |
Keith Randall | faf1bdb | 2016-02-06 22:35:34 -0800 | [diff] [blame] | 557 | m := s.mem() |
| 558 | b := s.endBlock() |
| 559 | b.Kind = ssa.BlockExit |
Keith Randall | 56e0ecc | 2016-03-15 20:45:50 -0700 | [diff] [blame] | 560 | b.SetControl(m) |
Keith Randall | faf1bdb | 2016-02-06 22:35:34 -0800 | [diff] [blame] | 561 | // TODO: never rewrite OPANIC to OCALLFUNC in the |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 562 | // first place. Need to wait until all backends |
Keith Randall | faf1bdb | 2016-02-06 22:35:34 -0800 | [diff] [blame] | 563 | // go through SSA. |
| 564 | } |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 565 | case ODEFER: |
| 566 | s.call(n.Left, callDefer) |
| 567 | case OPROC: |
| 568 | s.call(n.Left, callGo) |
Josh Bleecher Snyder | 2574e4a | 2015-07-16 13:25:36 -0600 | [diff] [blame] | 569 | |
Keith Randall | 269baa9 | 2015-09-17 10:31:16 -0700 | [diff] [blame] | 570 | case OAS2DOTTYPE: |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 571 | res, resok := s.dottype(n.Rlist.First(), true) |
Keith Randall | d4663e1 | 2016-03-21 10:22:03 -0700 | [diff] [blame] | 572 | s.assign(n.List.First(), res, needwritebarrier(n.List.First(), n.Rlist.First()), false, n.Lineno, 0) |
| 573 | s.assign(n.List.Second(), resok, false, false, n.Lineno, 0) |
Keith Randall | 269baa9 | 2015-09-17 10:31:16 -0700 | [diff] [blame] | 574 | return |
| 575 | |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 576 | case ODCL: |
Daniel Morsing | c31b6dd | 2015-06-12 14:23:29 +0100 | [diff] [blame] | 577 | if n.Left.Class&PHEAP == 0 { |
| 578 | return |
| 579 | } |
| 580 | if compiling_runtime != 0 { |
Keith Randall | 0ec72b6 | 2015-09-08 15:42:53 -0700 | [diff] [blame] | 581 | Fatalf("%v escapes to heap, not allowed in runtime.", n) |
Daniel Morsing | c31b6dd | 2015-06-12 14:23:29 +0100 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | // TODO: the old pass hides the details of PHEAP |
| 585 | // variables behind ONAME nodes. Figure out if it's better |
| 586 | // to rewrite the tree and make the heapaddr construct explicit |
| 587 | // or to keep this detail hidden behind the scenes. |
| 588 | palloc := prealloc[n.Left] |
| 589 | if palloc == nil { |
| 590 | palloc = callnew(n.Left.Type) |
| 591 | prealloc[n.Left] = palloc |
| 592 | } |
Josh Bleecher Snyder | 0726931 | 2015-08-29 14:54:45 -0700 | [diff] [blame] | 593 | r := s.expr(palloc) |
Keith Randall | d4663e1 | 2016-03-21 10:22:03 -0700 | [diff] [blame] | 594 | s.assign(n.Left.Name.Heapaddr, r, false, false, n.Lineno, 0) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 595 | |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 596 | case OLABEL: |
| 597 | sym := n.Left.Sym |
| 598 | |
| 599 | if isblanksym(sym) { |
Keith Randall | 7e4c06d | 2015-07-12 11:52:09 -0700 | [diff] [blame] | 600 | // Empty identifier is valid but useless. |
| 601 | // See issues 11589, 11593. |
| 602 | return |
| 603 | } |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 604 | |
| 605 | lab := s.label(sym) |
| 606 | |
| 607 | // Associate label with its control flow node, if any |
| 608 | if ctl := n.Name.Defn; ctl != nil { |
| 609 | switch ctl.Op { |
| 610 | case OFOR, OSWITCH, OSELECT: |
| 611 | s.labeledNodes[ctl] = lab |
| 612 | } |
Keith Randall | 0ad9c8c | 2015-06-12 16:24:33 -0700 | [diff] [blame] | 613 | } |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 614 | |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 615 | if !lab.defined() { |
| 616 | lab.defNode = n |
| 617 | } else { |
Robert Griesemer | 2faf5bc | 2016-03-02 11:30:29 -0800 | [diff] [blame] | 618 | s.Error("label %v already defined at %v", sym, linestr(lab.defNode.Lineno)) |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 619 | lab.reported = true |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 620 | } |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 621 | // The label might already have a target block via a goto. |
| 622 | if lab.target == nil { |
| 623 | lab.target = s.f.NewBlock(ssa.BlockPlain) |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 624 | } |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 625 | |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 626 | // go to that label (we pretend "label:" is preceded by "goto label") |
| 627 | b := s.endBlock() |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 628 | b.AddEdgeTo(lab.target) |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 629 | s.startBlock(lab.target) |
| 630 | |
| 631 | case OGOTO: |
| 632 | sym := n.Left.Sym |
| 633 | |
| 634 | lab := s.label(sym) |
| 635 | if lab.target == nil { |
| 636 | lab.target = s.f.NewBlock(ssa.BlockPlain) |
| 637 | } |
| 638 | if !lab.used() { |
| 639 | lab.useNode = n |
| 640 | } |
| 641 | |
| 642 | if lab.defined() { |
| 643 | s.checkgoto(n, lab.defNode) |
| 644 | } else { |
| 645 | s.fwdGotos = append(s.fwdGotos, n) |
| 646 | } |
| 647 | |
| 648 | b := s.endBlock() |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 649 | b.AddEdgeTo(lab.target) |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 650 | |
Keith Randall | 290d8fc | 2015-06-10 15:03:06 -0700 | [diff] [blame] | 651 | case OAS, OASWB: |
Josh Bleecher Snyder | 6b41665 | 2015-07-28 10:56:39 -0700 | [diff] [blame] | 652 | // Check whether we can generate static data rather than code. |
| 653 | // If so, ignore n and defer data generation until codegen. |
| 654 | // Failure to do this causes writes to readonly symbols. |
| 655 | if gen_as_init(n, true) { |
| 656 | var data []*Node |
| 657 | if s.f.StaticData != nil { |
| 658 | data = s.f.StaticData.([]*Node) |
| 659 | } |
| 660 | s.f.StaticData = append(data, n) |
| 661 | return |
| 662 | } |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 663 | |
Keith Randall | 309144b | 2016-04-01 11:05:30 -0700 | [diff] [blame] | 664 | if n.Left == n.Right && n.Left.Op == ONAME { |
| 665 | // An x=x assignment. No point in doing anything |
| 666 | // here. In addition, skipping this assignment |
| 667 | // prevents generating: |
| 668 | // VARDEF x |
| 669 | // COPY x -> x |
| 670 | // which is bad because x is incorrectly considered |
| 671 | // dead before the vardef. See issue #14904. |
| 672 | return |
| 673 | } |
| 674 | |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 675 | var t *Type |
Josh Bleecher Snyder | 0726931 | 2015-08-29 14:54:45 -0700 | [diff] [blame] | 676 | if n.Right != nil { |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 677 | t = n.Right.Type |
| 678 | } else { |
| 679 | t = n.Left.Type |
| 680 | } |
| 681 | |
| 682 | // Evaluate RHS. |
| 683 | rhs := n.Right |
| 684 | if rhs != nil && (rhs.Op == OSTRUCTLIT || rhs.Op == OARRAYLIT) { |
| 685 | // All literals with nonzero fields have already been |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 686 | // rewritten during walk. Any that remain are just T{} |
| 687 | // or equivalents. Use the zero value. |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 688 | if !iszero(rhs) { |
| 689 | Fatalf("literal with nonzero value in SSA: %v", rhs) |
| 690 | } |
| 691 | rhs = nil |
| 692 | } |
| 693 | var r *ssa.Value |
| 694 | needwb := n.Op == OASWB && rhs != nil |
| 695 | deref := !canSSAType(t) |
| 696 | if deref { |
| 697 | if rhs == nil { |
| 698 | r = nil // Signal assign to use OpZero. |
Keith Randall | d388690 | 2015-09-18 22:12:38 -0700 | [diff] [blame] | 699 | } else { |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 700 | r = s.addr(rhs, false) |
| 701 | } |
| 702 | } else { |
| 703 | if rhs == nil { |
| 704 | r = s.zeroVal(t) |
| 705 | } else { |
| 706 | r = s.expr(rhs) |
Keith Randall | d388690 | 2015-09-18 22:12:38 -0700 | [diff] [blame] | 707 | } |
Josh Bleecher Snyder | 0726931 | 2015-08-29 14:54:45 -0700 | [diff] [blame] | 708 | } |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 709 | if rhs != nil && rhs.Op == OAPPEND { |
Keith Randall | 9d22c10 | 2015-09-11 11:02:57 -0700 | [diff] [blame] | 710 | // Yuck! The frontend gets rid of the write barrier, but we need it! |
| 711 | // At least, we need it in the case where growslice is called. |
| 712 | // TODO: Do the write barrier on just the growslice branch. |
| 713 | // TODO: just add a ptr graying to the end of growslice? |
| 714 | // TODO: check whether we need to do this for ODOTTYPE and ORECV also. |
| 715 | // They get similar wb-removal treatment in walk.go:OAS. |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 716 | needwb = true |
Keith Randall | 9d22c10 | 2015-09-11 11:02:57 -0700 | [diff] [blame] | 717 | } |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 718 | |
Keith Randall | d4663e1 | 2016-03-21 10:22:03 -0700 | [diff] [blame] | 719 | var skip skipMask |
| 720 | if rhs != nil && (rhs.Op == OSLICE || rhs.Op == OSLICE3 || rhs.Op == OSLICESTR) && samesafeexpr(rhs.Left, n.Left) { |
| 721 | // We're assigning a slicing operation back to its source. |
| 722 | // Don't write back fields we aren't changing. See issue #14855. |
| 723 | i := rhs.Right.Left |
| 724 | var j, k *Node |
| 725 | if rhs.Op == OSLICE3 { |
| 726 | j = rhs.Right.Right.Left |
| 727 | k = rhs.Right.Right.Right |
| 728 | } else { |
| 729 | j = rhs.Right.Right |
| 730 | } |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 731 | if i != nil && (i.Op == OLITERAL && i.Val().Ctype() == CTINT && i.Int64() == 0) { |
Keith Randall | d4663e1 | 2016-03-21 10:22:03 -0700 | [diff] [blame] | 732 | // [0:...] is the same as [:...] |
| 733 | i = nil |
| 734 | } |
| 735 | // TODO: detect defaults for len/cap also. |
| 736 | // Currently doesn't really work because (*p)[:len(*p)] appears here as: |
| 737 | // tmp = len(*p) |
| 738 | // (*p)[:tmp] |
| 739 | //if j != nil && (j.Op == OLEN && samesafeexpr(j.Left, n.Left)) { |
| 740 | // j = nil |
| 741 | //} |
| 742 | //if k != nil && (k.Op == OCAP && samesafeexpr(k.Left, n.Left)) { |
| 743 | // k = nil |
| 744 | //} |
| 745 | if i == nil { |
| 746 | skip |= skipPtr |
| 747 | if j == nil { |
| 748 | skip |= skipLen |
| 749 | } |
| 750 | if k == nil { |
| 751 | skip |= skipCap |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | s.assign(n.Left, r, needwb, deref, n.Lineno, skip) |
Daniel Morsing | c31b6dd | 2015-06-12 14:23:29 +0100 | [diff] [blame] | 757 | |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 758 | case OIF: |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 759 | bThen := s.f.NewBlock(ssa.BlockPlain) |
| 760 | bEnd := s.f.NewBlock(ssa.BlockPlain) |
| 761 | var bElse *ssa.Block |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 762 | if n.Rlist.Len() != 0 { |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 763 | bElse = s.f.NewBlock(ssa.BlockPlain) |
Keith Randall | 9918731 | 2015-11-02 16:56:53 -0800 | [diff] [blame] | 764 | s.condBranch(n.Left, bThen, bElse, n.Likely) |
| 765 | } else { |
| 766 | s.condBranch(n.Left, bThen, bEnd, n.Likely) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | s.startBlock(bThen) |
Keith Randall | 9d854fd | 2016-03-01 12:50:17 -0800 | [diff] [blame] | 770 | s.stmts(n.Nbody) |
Josh Bleecher Snyder | e0ac5c5 | 2015-07-20 18:42:45 -0700 | [diff] [blame] | 771 | if b := s.endBlock(); b != nil { |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 772 | b.AddEdgeTo(bEnd) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Ian Lance Taylor | 38921b3 | 2016-03-08 15:10:26 -0800 | [diff] [blame] | 775 | if n.Rlist.Len() != 0 { |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 776 | s.startBlock(bElse) |
Keith Randall | e707fbe | 2015-06-11 10:20:39 -0700 | [diff] [blame] | 777 | s.stmtList(n.Rlist) |
Josh Bleecher Snyder | e0ac5c5 | 2015-07-20 18:42:45 -0700 | [diff] [blame] | 778 | if b := s.endBlock(); b != nil { |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 779 | b.AddEdgeTo(bEnd) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | s.startBlock(bEnd) |
| 783 | |
| 784 | case ORETURN: |
| 785 | s.stmtList(n.List) |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 786 | s.exit() |
Keith Randall | 8a1f621 | 2015-09-08 21:28:44 -0700 | [diff] [blame] | 787 | case ORETJMP: |
| 788 | s.stmtList(n.List) |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 789 | b := s.exit() |
| 790 | b.Kind = ssa.BlockRetJmp // override BlockRet |
Keith Randall | 8a1f621 | 2015-09-08 21:28:44 -0700 | [diff] [blame] | 791 | b.Aux = n.Left.Sym |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 792 | |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 793 | case OCONTINUE, OBREAK: |
| 794 | var op string |
| 795 | var to *ssa.Block |
| 796 | switch n.Op { |
| 797 | case OCONTINUE: |
| 798 | op = "continue" |
| 799 | to = s.continueTo |
| 800 | case OBREAK: |
| 801 | op = "break" |
| 802 | to = s.breakTo |
| 803 | } |
| 804 | if n.Left == nil { |
| 805 | // plain break/continue |
| 806 | if to == nil { |
| 807 | s.Error("%s is not in a loop", op) |
| 808 | return |
| 809 | } |
| 810 | // nothing to do; "to" is already the correct target |
| 811 | } else { |
| 812 | // labeled break/continue; look up the target |
| 813 | sym := n.Left.Sym |
| 814 | lab := s.label(sym) |
| 815 | if !lab.used() { |
| 816 | lab.useNode = n.Left |
| 817 | } |
| 818 | if !lab.defined() { |
| 819 | s.Error("%s label not defined: %v", op, sym) |
| 820 | lab.reported = true |
| 821 | return |
| 822 | } |
| 823 | switch n.Op { |
| 824 | case OCONTINUE: |
| 825 | to = lab.continueTarget |
| 826 | case OBREAK: |
| 827 | to = lab.breakTarget |
| 828 | } |
| 829 | if to == nil { |
| 830 | // Valid label but not usable with a break/continue here, e.g.: |
| 831 | // for { |
| 832 | // continue abc |
| 833 | // } |
| 834 | // abc: |
| 835 | // for {} |
| 836 | s.Error("invalid %s label %v", op, sym) |
| 837 | lab.reported = true |
| 838 | return |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | b := s.endBlock() |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 843 | b.AddEdgeTo(to) |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 844 | |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 845 | case OFOR: |
Josh Bleecher Snyder | 5173868 | 2015-07-06 15:29:39 -0700 | [diff] [blame] | 846 | // OFOR: for Ninit; Left; Right { Nbody } |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 847 | bCond := s.f.NewBlock(ssa.BlockPlain) |
| 848 | bBody := s.f.NewBlock(ssa.BlockPlain) |
Josh Bleecher Snyder | 5173868 | 2015-07-06 15:29:39 -0700 | [diff] [blame] | 849 | bIncr := s.f.NewBlock(ssa.BlockPlain) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 850 | bEnd := s.f.NewBlock(ssa.BlockPlain) |
| 851 | |
| 852 | // first, jump to condition test |
| 853 | b := s.endBlock() |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 854 | b.AddEdgeTo(bCond) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 855 | |
| 856 | // generate code to test condition |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 857 | s.startBlock(bCond) |
Josh Bleecher Snyder | 5173868 | 2015-07-06 15:29:39 -0700 | [diff] [blame] | 858 | if n.Left != nil { |
Keith Randall | 9918731 | 2015-11-02 16:56:53 -0800 | [diff] [blame] | 859 | s.condBranch(n.Left, bBody, bEnd, 1) |
Josh Bleecher Snyder | 5173868 | 2015-07-06 15:29:39 -0700 | [diff] [blame] | 860 | } else { |
Keith Randall | 9918731 | 2015-11-02 16:56:53 -0800 | [diff] [blame] | 861 | b := s.endBlock() |
| 862 | b.Kind = ssa.BlockPlain |
| 863 | b.AddEdgeTo(bBody) |
Josh Bleecher Snyder | 5173868 | 2015-07-06 15:29:39 -0700 | [diff] [blame] | 864 | } |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 865 | |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 866 | // set up for continue/break in body |
| 867 | prevContinue := s.continueTo |
| 868 | prevBreak := s.breakTo |
| 869 | s.continueTo = bIncr |
| 870 | s.breakTo = bEnd |
| 871 | lab := s.labeledNodes[n] |
| 872 | if lab != nil { |
| 873 | // labeled for loop |
| 874 | lab.continueTarget = bIncr |
| 875 | lab.breakTarget = bEnd |
| 876 | } |
| 877 | |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 878 | // generate body |
| 879 | s.startBlock(bBody) |
Keith Randall | 9d854fd | 2016-03-01 12:50:17 -0800 | [diff] [blame] | 880 | s.stmts(n.Nbody) |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 881 | |
| 882 | // tear down continue/break |
| 883 | s.continueTo = prevContinue |
| 884 | s.breakTo = prevBreak |
| 885 | if lab != nil { |
| 886 | lab.continueTarget = nil |
| 887 | lab.breakTarget = nil |
| 888 | } |
| 889 | |
| 890 | // done with body, goto incr |
Josh Bleecher Snyder | 5173868 | 2015-07-06 15:29:39 -0700 | [diff] [blame] | 891 | if b := s.endBlock(); b != nil { |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 892 | b.AddEdgeTo(bIncr) |
Josh Bleecher Snyder | 5173868 | 2015-07-06 15:29:39 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | // generate incr |
| 896 | s.startBlock(bIncr) |
Josh Bleecher Snyder | 46815b9 | 2015-06-24 17:48:22 -0700 | [diff] [blame] | 897 | if n.Right != nil { |
| 898 | s.stmt(n.Right) |
| 899 | } |
Josh Bleecher Snyder | 5173868 | 2015-07-06 15:29:39 -0700 | [diff] [blame] | 900 | if b := s.endBlock(); b != nil { |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 901 | b.AddEdgeTo(bCond) |
Josh Bleecher Snyder | 6c14059 | 2015-07-04 09:07:54 -0700 | [diff] [blame] | 902 | } |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 903 | s.startBlock(bEnd) |
| 904 | |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 905 | case OSWITCH, OSELECT: |
| 906 | // These have been mostly rewritten by the front end into their Nbody fields. |
| 907 | // Our main task is to correctly hook up any break statements. |
| 908 | bEnd := s.f.NewBlock(ssa.BlockPlain) |
| 909 | |
| 910 | prevBreak := s.breakTo |
| 911 | s.breakTo = bEnd |
| 912 | lab := s.labeledNodes[n] |
| 913 | if lab != nil { |
| 914 | // labeled |
| 915 | lab.breakTarget = bEnd |
| 916 | } |
| 917 | |
| 918 | // generate body code |
Keith Randall | 9d854fd | 2016-03-01 12:50:17 -0800 | [diff] [blame] | 919 | s.stmts(n.Nbody) |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 920 | |
| 921 | s.breakTo = prevBreak |
| 922 | if lab != nil { |
| 923 | lab.breakTarget = nil |
| 924 | } |
| 925 | |
Keith Randall | eb0cff9 | 2016-02-09 12:28:02 -0800 | [diff] [blame] | 926 | // OSWITCH never falls through (s.curBlock == nil here). |
| 927 | // OSELECT does not fall through if we're calling selectgo. |
| 928 | // OSELECT does fall through if we're calling selectnb{send,recv}[2]. |
| 929 | // In those latter cases, go to the code after the select. |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 930 | if b := s.endBlock(); b != nil { |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 931 | b.AddEdgeTo(bEnd) |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 932 | } |
| 933 | s.startBlock(bEnd) |
| 934 | |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 935 | case OVARKILL: |
Keith Randall | d2107fc | 2015-08-24 02:16:19 -0700 | [diff] [blame] | 936 | // Insert a varkill op to record that a variable is no longer live. |
| 937 | // We only care about liveness info at call sites, so putting the |
| 938 | // varkill in the store chain is enough to keep it correctly ordered |
| 939 | // with respect to call ops. |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 940 | if !s.canSSA(n.Left) { |
Keith Randall | d29e92b | 2015-09-19 12:01:39 -0700 | [diff] [blame] | 941 | s.vars[&memVar] = s.newValue1A(ssa.OpVarKill, ssa.TypeMem, n.Left, s.mem()) |
| 942 | } |
Keith Randall | 9569b95 | 2015-08-28 22:51:01 -0700 | [diff] [blame] | 943 | |
Keith Randall | 23d5810 | 2016-01-19 09:59:21 -0800 | [diff] [blame] | 944 | case OVARLIVE: |
| 945 | // Insert a varlive op to record that a variable is still live. |
| 946 | if !n.Left.Addrtaken { |
| 947 | s.Fatalf("VARLIVE variable %s must have Addrtaken set", n.Left) |
| 948 | } |
| 949 | s.vars[&memVar] = s.newValue1A(ssa.OpVarLive, ssa.TypeMem, n.Left, s.mem()) |
| 950 | |
Keith Randall | 46ffb02 | 2015-09-12 14:06:44 -0700 | [diff] [blame] | 951 | case OCHECKNIL: |
| 952 | p := s.expr(n.Left) |
| 953 | s.nilCheck(p) |
| 954 | |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 955 | default: |
Josh Bleecher Snyder | 37ddc27 | 2015-06-24 14:03:39 -0700 | [diff] [blame] | 956 | s.Unimplementedf("unhandled stmt %s", opnames[n.Op]) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 960 | // exit processes any code that needs to be generated just before returning. |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 961 | // It returns a BlockRet block that ends the control flow. Its control value |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 962 | // will be set to the final memory state. |
| 963 | func (s *state) exit() *ssa.Block { |
Keith Randall | ddc6b64 | 2016-03-09 19:27:57 -0800 | [diff] [blame] | 964 | if hasdefer { |
| 965 | s.rtcall(Deferreturn, true, nil) |
| 966 | } |
| 967 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 968 | // Run exit code. Typically, this code copies heap-allocated PPARAMOUT |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 969 | // variables back to the stack. |
| 970 | s.stmts(s.exitCode) |
| 971 | |
| 972 | // Store SSAable PPARAMOUT variables back to stack locations. |
| 973 | for _, n := range s.returns { |
| 974 | aux := &ssa.ArgSymbol{Typ: n.Type, Node: n} |
| 975 | addr := s.newValue1A(ssa.OpAddr, Ptrto(n.Type), aux, s.sp) |
| 976 | val := s.variable(n, n.Type) |
| 977 | s.vars[&memVar] = s.newValue1A(ssa.OpVarDef, ssa.TypeMem, n, s.mem()) |
| 978 | s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, n.Type.Size(), addr, val, s.mem()) |
| 979 | // TODO: if val is ever spilled, we'd like to use the |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 980 | // PPARAMOUT slot for spilling it. That won't happen |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 981 | // currently. |
| 982 | } |
| 983 | |
| 984 | // Do actual return. |
| 985 | m := s.mem() |
| 986 | b := s.endBlock() |
| 987 | b.Kind = ssa.BlockRet |
Keith Randall | 56e0ecc | 2016-03-15 20:45:50 -0700 | [diff] [blame] | 988 | b.SetControl(m) |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 989 | return b |
| 990 | } |
| 991 | |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 992 | type opAndType struct { |
Keith Randall | 4304fbc | 2015-11-16 13:20:16 -0800 | [diff] [blame] | 993 | op Op |
| 994 | etype EType |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | var opToSSA = map[opAndType]ssa.Op{ |
David Chase | 997a9f3 | 2015-08-12 16:38:11 -0400 | [diff] [blame] | 998 | opAndType{OADD, TINT8}: ssa.OpAdd8, |
| 999 | opAndType{OADD, TUINT8}: ssa.OpAdd8, |
| 1000 | opAndType{OADD, TINT16}: ssa.OpAdd16, |
| 1001 | opAndType{OADD, TUINT16}: ssa.OpAdd16, |
| 1002 | opAndType{OADD, TINT32}: ssa.OpAdd32, |
| 1003 | opAndType{OADD, TUINT32}: ssa.OpAdd32, |
| 1004 | opAndType{OADD, TPTR32}: ssa.OpAdd32, |
| 1005 | opAndType{OADD, TINT64}: ssa.OpAdd64, |
| 1006 | opAndType{OADD, TUINT64}: ssa.OpAdd64, |
| 1007 | opAndType{OADD, TPTR64}: ssa.OpAdd64, |
| 1008 | opAndType{OADD, TFLOAT32}: ssa.OpAdd32F, |
| 1009 | opAndType{OADD, TFLOAT64}: ssa.OpAdd64F, |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 1010 | |
David Chase | 997a9f3 | 2015-08-12 16:38:11 -0400 | [diff] [blame] | 1011 | opAndType{OSUB, TINT8}: ssa.OpSub8, |
| 1012 | opAndType{OSUB, TUINT8}: ssa.OpSub8, |
| 1013 | opAndType{OSUB, TINT16}: ssa.OpSub16, |
| 1014 | opAndType{OSUB, TUINT16}: ssa.OpSub16, |
| 1015 | opAndType{OSUB, TINT32}: ssa.OpSub32, |
| 1016 | opAndType{OSUB, TUINT32}: ssa.OpSub32, |
| 1017 | opAndType{OSUB, TINT64}: ssa.OpSub64, |
| 1018 | opAndType{OSUB, TUINT64}: ssa.OpSub64, |
| 1019 | opAndType{OSUB, TFLOAT32}: ssa.OpSub32F, |
| 1020 | opAndType{OSUB, TFLOAT64}: ssa.OpSub64F, |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 1021 | |
Josh Bleecher Snyder | e61e7c9 | 2015-07-22 19:19:40 -0700 | [diff] [blame] | 1022 | opAndType{ONOT, TBOOL}: ssa.OpNot, |
| 1023 | |
David Chase | 3a9d0ac | 2015-08-28 14:24:10 -0400 | [diff] [blame] | 1024 | opAndType{OMINUS, TINT8}: ssa.OpNeg8, |
| 1025 | opAndType{OMINUS, TUINT8}: ssa.OpNeg8, |
| 1026 | opAndType{OMINUS, TINT16}: ssa.OpNeg16, |
| 1027 | opAndType{OMINUS, TUINT16}: ssa.OpNeg16, |
| 1028 | opAndType{OMINUS, TINT32}: ssa.OpNeg32, |
| 1029 | opAndType{OMINUS, TUINT32}: ssa.OpNeg32, |
| 1030 | opAndType{OMINUS, TINT64}: ssa.OpNeg64, |
| 1031 | opAndType{OMINUS, TUINT64}: ssa.OpNeg64, |
| 1032 | opAndType{OMINUS, TFLOAT32}: ssa.OpNeg32F, |
| 1033 | opAndType{OMINUS, TFLOAT64}: ssa.OpNeg64F, |
Alexandru Moșoi | 954d5ad | 2015-07-21 16:58:18 +0200 | [diff] [blame] | 1034 | |
Keith Randall | 4b80315 | 2015-07-29 17:07:09 -0700 | [diff] [blame] | 1035 | opAndType{OCOM, TINT8}: ssa.OpCom8, |
| 1036 | opAndType{OCOM, TUINT8}: ssa.OpCom8, |
| 1037 | opAndType{OCOM, TINT16}: ssa.OpCom16, |
| 1038 | opAndType{OCOM, TUINT16}: ssa.OpCom16, |
| 1039 | opAndType{OCOM, TINT32}: ssa.OpCom32, |
| 1040 | opAndType{OCOM, TUINT32}: ssa.OpCom32, |
| 1041 | opAndType{OCOM, TINT64}: ssa.OpCom64, |
| 1042 | opAndType{OCOM, TUINT64}: ssa.OpCom64, |
| 1043 | |
Josh Bleecher Snyder | fa5fe19 | 2015-09-06 19:24:59 -0700 | [diff] [blame] | 1044 | opAndType{OIMAG, TCOMPLEX64}: ssa.OpComplexImag, |
| 1045 | opAndType{OIMAG, TCOMPLEX128}: ssa.OpComplexImag, |
| 1046 | opAndType{OREAL, TCOMPLEX64}: ssa.OpComplexReal, |
| 1047 | opAndType{OREAL, TCOMPLEX128}: ssa.OpComplexReal, |
| 1048 | |
David Chase | 997a9f3 | 2015-08-12 16:38:11 -0400 | [diff] [blame] | 1049 | opAndType{OMUL, TINT8}: ssa.OpMul8, |
| 1050 | opAndType{OMUL, TUINT8}: ssa.OpMul8, |
| 1051 | opAndType{OMUL, TINT16}: ssa.OpMul16, |
| 1052 | opAndType{OMUL, TUINT16}: ssa.OpMul16, |
| 1053 | opAndType{OMUL, TINT32}: ssa.OpMul32, |
| 1054 | opAndType{OMUL, TUINT32}: ssa.OpMul32, |
| 1055 | opAndType{OMUL, TINT64}: ssa.OpMul64, |
| 1056 | opAndType{OMUL, TUINT64}: ssa.OpMul64, |
| 1057 | opAndType{OMUL, TFLOAT32}: ssa.OpMul32F, |
| 1058 | opAndType{OMUL, TFLOAT64}: ssa.OpMul64F, |
| 1059 | |
| 1060 | opAndType{ODIV, TFLOAT32}: ssa.OpDiv32F, |
| 1061 | opAndType{ODIV, TFLOAT64}: ssa.OpDiv64F, |
Keith Randall | be1eb57 | 2015-07-22 13:46:15 -0700 | [diff] [blame] | 1062 | |
Todd Neal | 67cbd5b | 2015-08-18 19:14:47 -0500 | [diff] [blame] | 1063 | opAndType{OHMUL, TINT8}: ssa.OpHmul8, |
| 1064 | opAndType{OHMUL, TUINT8}: ssa.OpHmul8u, |
| 1065 | opAndType{OHMUL, TINT16}: ssa.OpHmul16, |
| 1066 | opAndType{OHMUL, TUINT16}: ssa.OpHmul16u, |
| 1067 | opAndType{OHMUL, TINT32}: ssa.OpHmul32, |
| 1068 | opAndType{OHMUL, TUINT32}: ssa.OpHmul32u, |
| 1069 | |
Todd Neal | a45f2d8 | 2015-08-17 17:46:06 -0500 | [diff] [blame] | 1070 | opAndType{ODIV, TINT8}: ssa.OpDiv8, |
| 1071 | opAndType{ODIV, TUINT8}: ssa.OpDiv8u, |
| 1072 | opAndType{ODIV, TINT16}: ssa.OpDiv16, |
| 1073 | opAndType{ODIV, TUINT16}: ssa.OpDiv16u, |
| 1074 | opAndType{ODIV, TINT32}: ssa.OpDiv32, |
| 1075 | opAndType{ODIV, TUINT32}: ssa.OpDiv32u, |
| 1076 | opAndType{ODIV, TINT64}: ssa.OpDiv64, |
| 1077 | opAndType{ODIV, TUINT64}: ssa.OpDiv64u, |
| 1078 | |
Todd Neal | 57d9e7e | 2015-08-18 19:51:44 -0500 | [diff] [blame] | 1079 | opAndType{OMOD, TINT8}: ssa.OpMod8, |
| 1080 | opAndType{OMOD, TUINT8}: ssa.OpMod8u, |
| 1081 | opAndType{OMOD, TINT16}: ssa.OpMod16, |
| 1082 | opAndType{OMOD, TUINT16}: ssa.OpMod16u, |
| 1083 | opAndType{OMOD, TINT32}: ssa.OpMod32, |
| 1084 | opAndType{OMOD, TUINT32}: ssa.OpMod32u, |
| 1085 | opAndType{OMOD, TINT64}: ssa.OpMod64, |
| 1086 | opAndType{OMOD, TUINT64}: ssa.OpMod64u, |
| 1087 | |
Alexandru Moșoi | edff881 | 2015-07-28 14:58:49 +0200 | [diff] [blame] | 1088 | opAndType{OAND, TINT8}: ssa.OpAnd8, |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 1089 | opAndType{OAND, TUINT8}: ssa.OpAnd8, |
Alexandru Moșoi | edff881 | 2015-07-28 14:58:49 +0200 | [diff] [blame] | 1090 | opAndType{OAND, TINT16}: ssa.OpAnd16, |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 1091 | opAndType{OAND, TUINT16}: ssa.OpAnd16, |
Alexandru Moșoi | edff881 | 2015-07-28 14:58:49 +0200 | [diff] [blame] | 1092 | opAndType{OAND, TINT32}: ssa.OpAnd32, |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 1093 | opAndType{OAND, TUINT32}: ssa.OpAnd32, |
Alexandru Moșoi | edff881 | 2015-07-28 14:58:49 +0200 | [diff] [blame] | 1094 | opAndType{OAND, TINT64}: ssa.OpAnd64, |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 1095 | opAndType{OAND, TUINT64}: ssa.OpAnd64, |
Alexandru Moșoi | edff881 | 2015-07-28 14:58:49 +0200 | [diff] [blame] | 1096 | |
Alexandru Moșoi | 7402416 | 2015-07-29 17:52:25 +0200 | [diff] [blame] | 1097 | opAndType{OOR, TINT8}: ssa.OpOr8, |
| 1098 | opAndType{OOR, TUINT8}: ssa.OpOr8, |
| 1099 | opAndType{OOR, TINT16}: ssa.OpOr16, |
| 1100 | opAndType{OOR, TUINT16}: ssa.OpOr16, |
| 1101 | opAndType{OOR, TINT32}: ssa.OpOr32, |
| 1102 | opAndType{OOR, TUINT32}: ssa.OpOr32, |
| 1103 | opAndType{OOR, TINT64}: ssa.OpOr64, |
| 1104 | opAndType{OOR, TUINT64}: ssa.OpOr64, |
| 1105 | |
Alexandru Moșoi | 6d9362a1 | 2015-07-30 12:33:36 +0200 | [diff] [blame] | 1106 | opAndType{OXOR, TINT8}: ssa.OpXor8, |
| 1107 | opAndType{OXOR, TUINT8}: ssa.OpXor8, |
| 1108 | opAndType{OXOR, TINT16}: ssa.OpXor16, |
| 1109 | opAndType{OXOR, TUINT16}: ssa.OpXor16, |
| 1110 | opAndType{OXOR, TINT32}: ssa.OpXor32, |
| 1111 | opAndType{OXOR, TUINT32}: ssa.OpXor32, |
| 1112 | opAndType{OXOR, TINT64}: ssa.OpXor64, |
| 1113 | opAndType{OXOR, TUINT64}: ssa.OpXor64, |
| 1114 | |
Josh Bleecher Snyder | 1bab5b9 | 2015-07-28 14:14:25 -0700 | [diff] [blame] | 1115 | opAndType{OEQ, TBOOL}: ssa.OpEq8, |
| 1116 | opAndType{OEQ, TINT8}: ssa.OpEq8, |
| 1117 | opAndType{OEQ, TUINT8}: ssa.OpEq8, |
| 1118 | opAndType{OEQ, TINT16}: ssa.OpEq16, |
| 1119 | opAndType{OEQ, TUINT16}: ssa.OpEq16, |
| 1120 | opAndType{OEQ, TINT32}: ssa.OpEq32, |
| 1121 | opAndType{OEQ, TUINT32}: ssa.OpEq32, |
| 1122 | opAndType{OEQ, TINT64}: ssa.OpEq64, |
| 1123 | opAndType{OEQ, TUINT64}: ssa.OpEq64, |
Keith Randall | 1e4ebfd | 2015-09-10 13:53:27 -0700 | [diff] [blame] | 1124 | opAndType{OEQ, TINTER}: ssa.OpEqInter, |
| 1125 | opAndType{OEQ, TARRAY}: ssa.OpEqSlice, |
Josh Bleecher Snyder | 1bab5b9 | 2015-07-28 14:14:25 -0700 | [diff] [blame] | 1126 | opAndType{OEQ, TFUNC}: ssa.OpEqPtr, |
| 1127 | opAndType{OEQ, TMAP}: ssa.OpEqPtr, |
| 1128 | opAndType{OEQ, TCHAN}: ssa.OpEqPtr, |
Todd Neal | 5fdd4fe | 2015-08-30 20:47:26 -0500 | [diff] [blame] | 1129 | opAndType{OEQ, TPTR64}: ssa.OpEqPtr, |
Josh Bleecher Snyder | 1bab5b9 | 2015-07-28 14:14:25 -0700 | [diff] [blame] | 1130 | opAndType{OEQ, TUINTPTR}: ssa.OpEqPtr, |
| 1131 | opAndType{OEQ, TUNSAFEPTR}: ssa.OpEqPtr, |
David Chase | 8e601b2 | 2015-08-18 14:39:26 -0400 | [diff] [blame] | 1132 | opAndType{OEQ, TFLOAT64}: ssa.OpEq64F, |
| 1133 | opAndType{OEQ, TFLOAT32}: ssa.OpEq32F, |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 1134 | |
Josh Bleecher Snyder | 1bab5b9 | 2015-07-28 14:14:25 -0700 | [diff] [blame] | 1135 | opAndType{ONE, TBOOL}: ssa.OpNeq8, |
| 1136 | opAndType{ONE, TINT8}: ssa.OpNeq8, |
| 1137 | opAndType{ONE, TUINT8}: ssa.OpNeq8, |
| 1138 | opAndType{ONE, TINT16}: ssa.OpNeq16, |
| 1139 | opAndType{ONE, TUINT16}: ssa.OpNeq16, |
| 1140 | opAndType{ONE, TINT32}: ssa.OpNeq32, |
| 1141 | opAndType{ONE, TUINT32}: ssa.OpNeq32, |
| 1142 | opAndType{ONE, TINT64}: ssa.OpNeq64, |
| 1143 | opAndType{ONE, TUINT64}: ssa.OpNeq64, |
Keith Randall | 1e4ebfd | 2015-09-10 13:53:27 -0700 | [diff] [blame] | 1144 | opAndType{ONE, TINTER}: ssa.OpNeqInter, |
| 1145 | opAndType{ONE, TARRAY}: ssa.OpNeqSlice, |
Josh Bleecher Snyder | 1bab5b9 | 2015-07-28 14:14:25 -0700 | [diff] [blame] | 1146 | opAndType{ONE, TFUNC}: ssa.OpNeqPtr, |
| 1147 | opAndType{ONE, TMAP}: ssa.OpNeqPtr, |
| 1148 | opAndType{ONE, TCHAN}: ssa.OpNeqPtr, |
Todd Neal | 5fdd4fe | 2015-08-30 20:47:26 -0500 | [diff] [blame] | 1149 | opAndType{ONE, TPTR64}: ssa.OpNeqPtr, |
Josh Bleecher Snyder | 1bab5b9 | 2015-07-28 14:14:25 -0700 | [diff] [blame] | 1150 | opAndType{ONE, TUINTPTR}: ssa.OpNeqPtr, |
| 1151 | opAndType{ONE, TUNSAFEPTR}: ssa.OpNeqPtr, |
David Chase | 8e601b2 | 2015-08-18 14:39:26 -0400 | [diff] [blame] | 1152 | opAndType{ONE, TFLOAT64}: ssa.OpNeq64F, |
| 1153 | opAndType{ONE, TFLOAT32}: ssa.OpNeq32F, |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 1154 | |
David Chase | 8e601b2 | 2015-08-18 14:39:26 -0400 | [diff] [blame] | 1155 | opAndType{OLT, TINT8}: ssa.OpLess8, |
| 1156 | opAndType{OLT, TUINT8}: ssa.OpLess8U, |
| 1157 | opAndType{OLT, TINT16}: ssa.OpLess16, |
| 1158 | opAndType{OLT, TUINT16}: ssa.OpLess16U, |
| 1159 | opAndType{OLT, TINT32}: ssa.OpLess32, |
| 1160 | opAndType{OLT, TUINT32}: ssa.OpLess32U, |
| 1161 | opAndType{OLT, TINT64}: ssa.OpLess64, |
| 1162 | opAndType{OLT, TUINT64}: ssa.OpLess64U, |
| 1163 | opAndType{OLT, TFLOAT64}: ssa.OpLess64F, |
| 1164 | opAndType{OLT, TFLOAT32}: ssa.OpLess32F, |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 1165 | |
David Chase | 8e601b2 | 2015-08-18 14:39:26 -0400 | [diff] [blame] | 1166 | opAndType{OGT, TINT8}: ssa.OpGreater8, |
| 1167 | opAndType{OGT, TUINT8}: ssa.OpGreater8U, |
| 1168 | opAndType{OGT, TINT16}: ssa.OpGreater16, |
| 1169 | opAndType{OGT, TUINT16}: ssa.OpGreater16U, |
| 1170 | opAndType{OGT, TINT32}: ssa.OpGreater32, |
| 1171 | opAndType{OGT, TUINT32}: ssa.OpGreater32U, |
| 1172 | opAndType{OGT, TINT64}: ssa.OpGreater64, |
| 1173 | opAndType{OGT, TUINT64}: ssa.OpGreater64U, |
| 1174 | opAndType{OGT, TFLOAT64}: ssa.OpGreater64F, |
| 1175 | opAndType{OGT, TFLOAT32}: ssa.OpGreater32F, |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 1176 | |
David Chase | 8e601b2 | 2015-08-18 14:39:26 -0400 | [diff] [blame] | 1177 | opAndType{OLE, TINT8}: ssa.OpLeq8, |
| 1178 | opAndType{OLE, TUINT8}: ssa.OpLeq8U, |
| 1179 | opAndType{OLE, TINT16}: ssa.OpLeq16, |
| 1180 | opAndType{OLE, TUINT16}: ssa.OpLeq16U, |
| 1181 | opAndType{OLE, TINT32}: ssa.OpLeq32, |
| 1182 | opAndType{OLE, TUINT32}: ssa.OpLeq32U, |
| 1183 | opAndType{OLE, TINT64}: ssa.OpLeq64, |
| 1184 | opAndType{OLE, TUINT64}: ssa.OpLeq64U, |
| 1185 | opAndType{OLE, TFLOAT64}: ssa.OpLeq64F, |
| 1186 | opAndType{OLE, TFLOAT32}: ssa.OpLeq32F, |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 1187 | |
David Chase | 8e601b2 | 2015-08-18 14:39:26 -0400 | [diff] [blame] | 1188 | opAndType{OGE, TINT8}: ssa.OpGeq8, |
| 1189 | opAndType{OGE, TUINT8}: ssa.OpGeq8U, |
| 1190 | opAndType{OGE, TINT16}: ssa.OpGeq16, |
| 1191 | opAndType{OGE, TUINT16}: ssa.OpGeq16U, |
| 1192 | opAndType{OGE, TINT32}: ssa.OpGeq32, |
| 1193 | opAndType{OGE, TUINT32}: ssa.OpGeq32U, |
| 1194 | opAndType{OGE, TINT64}: ssa.OpGeq64, |
| 1195 | opAndType{OGE, TUINT64}: ssa.OpGeq64U, |
| 1196 | opAndType{OGE, TFLOAT64}: ssa.OpGeq64F, |
| 1197 | opAndType{OGE, TFLOAT32}: ssa.OpGeq32F, |
David Chase | 40aba8c | 2015-08-05 22:11:14 -0400 | [diff] [blame] | 1198 | |
| 1199 | opAndType{OLROT, TUINT8}: ssa.OpLrot8, |
| 1200 | opAndType{OLROT, TUINT16}: ssa.OpLrot16, |
| 1201 | opAndType{OLROT, TUINT32}: ssa.OpLrot32, |
| 1202 | opAndType{OLROT, TUINT64}: ssa.OpLrot64, |
Keith Randall | a329e21 | 2015-09-12 13:26:57 -0700 | [diff] [blame] | 1203 | |
| 1204 | opAndType{OSQRT, TFLOAT64}: ssa.OpSqrt, |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
Keith Randall | 4304fbc | 2015-11-16 13:20:16 -0800 | [diff] [blame] | 1207 | func (s *state) concreteEtype(t *Type) EType { |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 1208 | e := t.Etype |
| 1209 | switch e { |
| 1210 | default: |
| 1211 | return e |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 1212 | case TINT: |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 1213 | if s.config.IntSize == 8 { |
| 1214 | return TINT64 |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 1215 | } |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 1216 | return TINT32 |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 1217 | case TUINT: |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 1218 | if s.config.IntSize == 8 { |
| 1219 | return TUINT64 |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 1220 | } |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 1221 | return TUINT32 |
| 1222 | case TUINTPTR: |
| 1223 | if s.config.PtrSize == 8 { |
| 1224 | return TUINT64 |
| 1225 | } |
| 1226 | return TUINT32 |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 1227 | } |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
Keith Randall | 4304fbc | 2015-11-16 13:20:16 -0800 | [diff] [blame] | 1230 | func (s *state) ssaOp(op Op, t *Type) ssa.Op { |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 1231 | etype := s.concreteEtype(t) |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 1232 | x, ok := opToSSA[opAndType{op, etype}] |
| 1233 | if !ok { |
Keith Randall | 4304fbc | 2015-11-16 13:20:16 -0800 | [diff] [blame] | 1234 | s.Unimplementedf("unhandled binary op %s %s", opnames[op], Econv(etype)) |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 1235 | } |
| 1236 | return x |
Josh Bleecher Snyder | 46815b9 | 2015-06-24 17:48:22 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
David Chase | 3a9d0ac | 2015-08-28 14:24:10 -0400 | [diff] [blame] | 1239 | func floatForComplex(t *Type) *Type { |
| 1240 | if t.Size() == 8 { |
| 1241 | return Types[TFLOAT32] |
| 1242 | } else { |
| 1243 | return Types[TFLOAT64] |
| 1244 | } |
| 1245 | } |
| 1246 | |
Keith Randall | 4b80315 | 2015-07-29 17:07:09 -0700 | [diff] [blame] | 1247 | type opAndTwoTypes struct { |
Keith Randall | 4304fbc | 2015-11-16 13:20:16 -0800 | [diff] [blame] | 1248 | op Op |
| 1249 | etype1 EType |
| 1250 | etype2 EType |
Keith Randall | 4b80315 | 2015-07-29 17:07:09 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
David Chase | d052bbd | 2015-09-01 17:09:00 -0400 | [diff] [blame] | 1253 | type twoTypes struct { |
Keith Randall | 4304fbc | 2015-11-16 13:20:16 -0800 | [diff] [blame] | 1254 | etype1 EType |
| 1255 | etype2 EType |
David Chase | d052bbd | 2015-09-01 17:09:00 -0400 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | type twoOpsAndType struct { |
| 1259 | op1 ssa.Op |
| 1260 | op2 ssa.Op |
Keith Randall | 4304fbc | 2015-11-16 13:20:16 -0800 | [diff] [blame] | 1261 | intermediateType EType |
David Chase | d052bbd | 2015-09-01 17:09:00 -0400 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | var fpConvOpToSSA = map[twoTypes]twoOpsAndType{ |
| 1265 | |
| 1266 | twoTypes{TINT8, TFLOAT32}: twoOpsAndType{ssa.OpSignExt8to32, ssa.OpCvt32to32F, TINT32}, |
| 1267 | twoTypes{TINT16, TFLOAT32}: twoOpsAndType{ssa.OpSignExt16to32, ssa.OpCvt32to32F, TINT32}, |
| 1268 | twoTypes{TINT32, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt32to32F, TINT32}, |
| 1269 | twoTypes{TINT64, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt64to32F, TINT64}, |
| 1270 | |
| 1271 | twoTypes{TINT8, TFLOAT64}: twoOpsAndType{ssa.OpSignExt8to32, ssa.OpCvt32to64F, TINT32}, |
| 1272 | twoTypes{TINT16, TFLOAT64}: twoOpsAndType{ssa.OpSignExt16to32, ssa.OpCvt32to64F, TINT32}, |
| 1273 | twoTypes{TINT32, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt32to64F, TINT32}, |
| 1274 | twoTypes{TINT64, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt64to64F, TINT64}, |
| 1275 | |
| 1276 | twoTypes{TFLOAT32, TINT8}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpTrunc32to8, TINT32}, |
| 1277 | twoTypes{TFLOAT32, TINT16}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpTrunc32to16, TINT32}, |
| 1278 | twoTypes{TFLOAT32, TINT32}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpCopy, TINT32}, |
| 1279 | twoTypes{TFLOAT32, TINT64}: twoOpsAndType{ssa.OpCvt32Fto64, ssa.OpCopy, TINT64}, |
| 1280 | |
| 1281 | twoTypes{TFLOAT64, TINT8}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpTrunc32to8, TINT32}, |
| 1282 | twoTypes{TFLOAT64, TINT16}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpTrunc32to16, TINT32}, |
| 1283 | twoTypes{TFLOAT64, TINT32}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpCopy, TINT32}, |
| 1284 | twoTypes{TFLOAT64, TINT64}: twoOpsAndType{ssa.OpCvt64Fto64, ssa.OpCopy, TINT64}, |
| 1285 | // unsigned |
| 1286 | twoTypes{TUINT8, TFLOAT32}: twoOpsAndType{ssa.OpZeroExt8to32, ssa.OpCvt32to32F, TINT32}, |
| 1287 | twoTypes{TUINT16, TFLOAT32}: twoOpsAndType{ssa.OpZeroExt16to32, ssa.OpCvt32to32F, TINT32}, |
| 1288 | twoTypes{TUINT32, TFLOAT32}: twoOpsAndType{ssa.OpZeroExt32to64, ssa.OpCvt64to32F, TINT64}, // go wide to dodge unsigned |
| 1289 | twoTypes{TUINT64, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpInvalid, TUINT64}, // Cvt64Uto32F, branchy code expansion instead |
| 1290 | |
| 1291 | twoTypes{TUINT8, TFLOAT64}: twoOpsAndType{ssa.OpZeroExt8to32, ssa.OpCvt32to64F, TINT32}, |
| 1292 | twoTypes{TUINT16, TFLOAT64}: twoOpsAndType{ssa.OpZeroExt16to32, ssa.OpCvt32to64F, TINT32}, |
| 1293 | twoTypes{TUINT32, TFLOAT64}: twoOpsAndType{ssa.OpZeroExt32to64, ssa.OpCvt64to64F, TINT64}, // go wide to dodge unsigned |
| 1294 | twoTypes{TUINT64, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpInvalid, TUINT64}, // Cvt64Uto64F, branchy code expansion instead |
| 1295 | |
| 1296 | twoTypes{TFLOAT32, TUINT8}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpTrunc32to8, TINT32}, |
| 1297 | twoTypes{TFLOAT32, TUINT16}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpTrunc32to16, TINT32}, |
| 1298 | twoTypes{TFLOAT32, TUINT32}: twoOpsAndType{ssa.OpCvt32Fto64, ssa.OpTrunc64to32, TINT64}, // go wide to dodge unsigned |
| 1299 | twoTypes{TFLOAT32, TUINT64}: twoOpsAndType{ssa.OpInvalid, ssa.OpCopy, TUINT64}, // Cvt32Fto64U, branchy code expansion instead |
| 1300 | |
| 1301 | twoTypes{TFLOAT64, TUINT8}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpTrunc32to8, TINT32}, |
| 1302 | twoTypes{TFLOAT64, TUINT16}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpTrunc32to16, TINT32}, |
| 1303 | twoTypes{TFLOAT64, TUINT32}: twoOpsAndType{ssa.OpCvt64Fto64, ssa.OpTrunc64to32, TINT64}, // go wide to dodge unsigned |
| 1304 | twoTypes{TFLOAT64, TUINT64}: twoOpsAndType{ssa.OpInvalid, ssa.OpCopy, TUINT64}, // Cvt64Fto64U, branchy code expansion instead |
| 1305 | |
| 1306 | // float |
| 1307 | twoTypes{TFLOAT64, TFLOAT32}: twoOpsAndType{ssa.OpCvt64Fto32F, ssa.OpCopy, TFLOAT32}, |
| 1308 | twoTypes{TFLOAT64, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpCopy, TFLOAT64}, |
| 1309 | twoTypes{TFLOAT32, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpCopy, TFLOAT32}, |
| 1310 | twoTypes{TFLOAT32, TFLOAT64}: twoOpsAndType{ssa.OpCvt32Fto64F, ssa.OpCopy, TFLOAT64}, |
| 1311 | } |
| 1312 | |
Keith Randall | 4b80315 | 2015-07-29 17:07:09 -0700 | [diff] [blame] | 1313 | var shiftOpToSSA = map[opAndTwoTypes]ssa.Op{ |
| 1314 | opAndTwoTypes{OLSH, TINT8, TUINT8}: ssa.OpLsh8x8, |
| 1315 | opAndTwoTypes{OLSH, TUINT8, TUINT8}: ssa.OpLsh8x8, |
| 1316 | opAndTwoTypes{OLSH, TINT8, TUINT16}: ssa.OpLsh8x16, |
| 1317 | opAndTwoTypes{OLSH, TUINT8, TUINT16}: ssa.OpLsh8x16, |
| 1318 | opAndTwoTypes{OLSH, TINT8, TUINT32}: ssa.OpLsh8x32, |
| 1319 | opAndTwoTypes{OLSH, TUINT8, TUINT32}: ssa.OpLsh8x32, |
| 1320 | opAndTwoTypes{OLSH, TINT8, TUINT64}: ssa.OpLsh8x64, |
| 1321 | opAndTwoTypes{OLSH, TUINT8, TUINT64}: ssa.OpLsh8x64, |
| 1322 | |
| 1323 | opAndTwoTypes{OLSH, TINT16, TUINT8}: ssa.OpLsh16x8, |
| 1324 | opAndTwoTypes{OLSH, TUINT16, TUINT8}: ssa.OpLsh16x8, |
| 1325 | opAndTwoTypes{OLSH, TINT16, TUINT16}: ssa.OpLsh16x16, |
| 1326 | opAndTwoTypes{OLSH, TUINT16, TUINT16}: ssa.OpLsh16x16, |
| 1327 | opAndTwoTypes{OLSH, TINT16, TUINT32}: ssa.OpLsh16x32, |
| 1328 | opAndTwoTypes{OLSH, TUINT16, TUINT32}: ssa.OpLsh16x32, |
| 1329 | opAndTwoTypes{OLSH, TINT16, TUINT64}: ssa.OpLsh16x64, |
| 1330 | opAndTwoTypes{OLSH, TUINT16, TUINT64}: ssa.OpLsh16x64, |
| 1331 | |
| 1332 | opAndTwoTypes{OLSH, TINT32, TUINT8}: ssa.OpLsh32x8, |
| 1333 | opAndTwoTypes{OLSH, TUINT32, TUINT8}: ssa.OpLsh32x8, |
| 1334 | opAndTwoTypes{OLSH, TINT32, TUINT16}: ssa.OpLsh32x16, |
| 1335 | opAndTwoTypes{OLSH, TUINT32, TUINT16}: ssa.OpLsh32x16, |
| 1336 | opAndTwoTypes{OLSH, TINT32, TUINT32}: ssa.OpLsh32x32, |
| 1337 | opAndTwoTypes{OLSH, TUINT32, TUINT32}: ssa.OpLsh32x32, |
| 1338 | opAndTwoTypes{OLSH, TINT32, TUINT64}: ssa.OpLsh32x64, |
| 1339 | opAndTwoTypes{OLSH, TUINT32, TUINT64}: ssa.OpLsh32x64, |
| 1340 | |
| 1341 | opAndTwoTypes{OLSH, TINT64, TUINT8}: ssa.OpLsh64x8, |
| 1342 | opAndTwoTypes{OLSH, TUINT64, TUINT8}: ssa.OpLsh64x8, |
| 1343 | opAndTwoTypes{OLSH, TINT64, TUINT16}: ssa.OpLsh64x16, |
| 1344 | opAndTwoTypes{OLSH, TUINT64, TUINT16}: ssa.OpLsh64x16, |
| 1345 | opAndTwoTypes{OLSH, TINT64, TUINT32}: ssa.OpLsh64x32, |
| 1346 | opAndTwoTypes{OLSH, TUINT64, TUINT32}: ssa.OpLsh64x32, |
| 1347 | opAndTwoTypes{OLSH, TINT64, TUINT64}: ssa.OpLsh64x64, |
| 1348 | opAndTwoTypes{OLSH, TUINT64, TUINT64}: ssa.OpLsh64x64, |
| 1349 | |
| 1350 | opAndTwoTypes{ORSH, TINT8, TUINT8}: ssa.OpRsh8x8, |
| 1351 | opAndTwoTypes{ORSH, TUINT8, TUINT8}: ssa.OpRsh8Ux8, |
| 1352 | opAndTwoTypes{ORSH, TINT8, TUINT16}: ssa.OpRsh8x16, |
| 1353 | opAndTwoTypes{ORSH, TUINT8, TUINT16}: ssa.OpRsh8Ux16, |
| 1354 | opAndTwoTypes{ORSH, TINT8, TUINT32}: ssa.OpRsh8x32, |
| 1355 | opAndTwoTypes{ORSH, TUINT8, TUINT32}: ssa.OpRsh8Ux32, |
| 1356 | opAndTwoTypes{ORSH, TINT8, TUINT64}: ssa.OpRsh8x64, |
| 1357 | opAndTwoTypes{ORSH, TUINT8, TUINT64}: ssa.OpRsh8Ux64, |
| 1358 | |
| 1359 | opAndTwoTypes{ORSH, TINT16, TUINT8}: ssa.OpRsh16x8, |
| 1360 | opAndTwoTypes{ORSH, TUINT16, TUINT8}: ssa.OpRsh16Ux8, |
| 1361 | opAndTwoTypes{ORSH, TINT16, TUINT16}: ssa.OpRsh16x16, |
| 1362 | opAndTwoTypes{ORSH, TUINT16, TUINT16}: ssa.OpRsh16Ux16, |
| 1363 | opAndTwoTypes{ORSH, TINT16, TUINT32}: ssa.OpRsh16x32, |
| 1364 | opAndTwoTypes{ORSH, TUINT16, TUINT32}: ssa.OpRsh16Ux32, |
| 1365 | opAndTwoTypes{ORSH, TINT16, TUINT64}: ssa.OpRsh16x64, |
| 1366 | opAndTwoTypes{ORSH, TUINT16, TUINT64}: ssa.OpRsh16Ux64, |
| 1367 | |
| 1368 | opAndTwoTypes{ORSH, TINT32, TUINT8}: ssa.OpRsh32x8, |
| 1369 | opAndTwoTypes{ORSH, TUINT32, TUINT8}: ssa.OpRsh32Ux8, |
| 1370 | opAndTwoTypes{ORSH, TINT32, TUINT16}: ssa.OpRsh32x16, |
| 1371 | opAndTwoTypes{ORSH, TUINT32, TUINT16}: ssa.OpRsh32Ux16, |
| 1372 | opAndTwoTypes{ORSH, TINT32, TUINT32}: ssa.OpRsh32x32, |
| 1373 | opAndTwoTypes{ORSH, TUINT32, TUINT32}: ssa.OpRsh32Ux32, |
| 1374 | opAndTwoTypes{ORSH, TINT32, TUINT64}: ssa.OpRsh32x64, |
| 1375 | opAndTwoTypes{ORSH, TUINT32, TUINT64}: ssa.OpRsh32Ux64, |
| 1376 | |
| 1377 | opAndTwoTypes{ORSH, TINT64, TUINT8}: ssa.OpRsh64x8, |
| 1378 | opAndTwoTypes{ORSH, TUINT64, TUINT8}: ssa.OpRsh64Ux8, |
| 1379 | opAndTwoTypes{ORSH, TINT64, TUINT16}: ssa.OpRsh64x16, |
| 1380 | opAndTwoTypes{ORSH, TUINT64, TUINT16}: ssa.OpRsh64Ux16, |
| 1381 | opAndTwoTypes{ORSH, TINT64, TUINT32}: ssa.OpRsh64x32, |
| 1382 | opAndTwoTypes{ORSH, TUINT64, TUINT32}: ssa.OpRsh64Ux32, |
| 1383 | opAndTwoTypes{ORSH, TINT64, TUINT64}: ssa.OpRsh64x64, |
| 1384 | opAndTwoTypes{ORSH, TUINT64, TUINT64}: ssa.OpRsh64Ux64, |
| 1385 | } |
| 1386 | |
Keith Randall | 4304fbc | 2015-11-16 13:20:16 -0800 | [diff] [blame] | 1387 | func (s *state) ssaShiftOp(op Op, t *Type, u *Type) ssa.Op { |
Keith Randall | 4b80315 | 2015-07-29 17:07:09 -0700 | [diff] [blame] | 1388 | etype1 := s.concreteEtype(t) |
| 1389 | etype2 := s.concreteEtype(u) |
| 1390 | x, ok := shiftOpToSSA[opAndTwoTypes{op, etype1, etype2}] |
| 1391 | if !ok { |
Keith Randall | 4304fbc | 2015-11-16 13:20:16 -0800 | [diff] [blame] | 1392 | s.Unimplementedf("unhandled shift op %s etype=%s/%s", opnames[op], Econv(etype1), Econv(etype2)) |
Keith Randall | 4b80315 | 2015-07-29 17:07:09 -0700 | [diff] [blame] | 1393 | } |
| 1394 | return x |
| 1395 | } |
| 1396 | |
Keith Randall | 4304fbc | 2015-11-16 13:20:16 -0800 | [diff] [blame] | 1397 | func (s *state) ssaRotateOp(op Op, t *Type) ssa.Op { |
David Chase | 40aba8c | 2015-08-05 22:11:14 -0400 | [diff] [blame] | 1398 | etype1 := s.concreteEtype(t) |
| 1399 | x, ok := opToSSA[opAndType{op, etype1}] |
| 1400 | if !ok { |
Keith Randall | 4304fbc | 2015-11-16 13:20:16 -0800 | [diff] [blame] | 1401 | s.Unimplementedf("unhandled rotate op %s etype=%s", opnames[op], Econv(etype1)) |
David Chase | 40aba8c | 2015-08-05 22:11:14 -0400 | [diff] [blame] | 1402 | } |
| 1403 | return x |
| 1404 | } |
| 1405 | |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 1406 | // expr converts the expression n to ssa, adds it to s and returns the ssa result. |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 1407 | func (s *state) expr(n *Node) *ssa.Value { |
Michael Matloob | 81ccf50 | 2015-05-30 01:03:06 -0400 | [diff] [blame] | 1408 | s.pushLine(n.Lineno) |
| 1409 | defer s.popLine() |
| 1410 | |
Keith Randall | 06f3292 | 2015-07-11 11:39:12 -0700 | [diff] [blame] | 1411 | s.stmtList(n.Ninit) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 1412 | switch n.Op { |
Todd Neal | def7c65 | 2015-09-07 19:07:02 -0500 | [diff] [blame] | 1413 | case OCFUNC: |
Todd Neal | d076ef7 | 2015-10-15 20:25:32 -0500 | [diff] [blame] | 1414 | aux := s.lookupSymbol(n, &ssa.ExternSymbol{n.Type, n.Left.Sym}) |
Todd Neal | def7c65 | 2015-09-07 19:07:02 -0500 | [diff] [blame] | 1415 | return s.entryNewValue1A(ssa.OpAddr, n.Type, aux, s.sb) |
David Chase | 956f319 | 2015-09-11 16:40:05 -0400 | [diff] [blame] | 1416 | case OPARAM: |
David Chase | 57670ad | 2015-10-09 16:48:30 -0400 | [diff] [blame] | 1417 | addr := s.addr(n, false) |
David Chase | 32ffbf7 | 2015-10-08 17:14:12 -0400 | [diff] [blame] | 1418 | return s.newValue2(ssa.OpLoad, n.Left.Type, addr, s.mem()) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 1419 | case ONAME: |
Keith Randall | 290d8fc | 2015-06-10 15:03:06 -0700 | [diff] [blame] | 1420 | if n.Class == PFUNC { |
| 1421 | // "value" of a function is the address of the function's closure |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 1422 | sym := funcsym(n.Sym) |
| 1423 | aux := &ssa.ExternSymbol{n.Type, sym} |
| 1424 | return s.entryNewValue1A(ssa.OpAddr, Ptrto(n.Type), aux, s.sb) |
Keith Randall | 23df95b | 2015-05-12 15:16:52 -0700 | [diff] [blame] | 1425 | } |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 1426 | if s.canSSA(n) { |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 1427 | return s.variable(n, n.Type) |
Keith Randall | 290d8fc | 2015-06-10 15:03:06 -0700 | [diff] [blame] | 1428 | } |
David Chase | 57670ad | 2015-10-09 16:48:30 -0400 | [diff] [blame] | 1429 | addr := s.addr(n, false) |
Keith Randall | 8f22b52 | 2015-06-11 21:29:25 -0700 | [diff] [blame] | 1430 | return s.newValue2(ssa.OpLoad, n.Type, addr, s.mem()) |
David Chase | 956f319 | 2015-09-11 16:40:05 -0400 | [diff] [blame] | 1431 | case OCLOSUREVAR: |
David Chase | 57670ad | 2015-10-09 16:48:30 -0400 | [diff] [blame] | 1432 | addr := s.addr(n, false) |
David Chase | 956f319 | 2015-09-11 16:40:05 -0400 | [diff] [blame] | 1433 | return s.newValue2(ssa.OpLoad, n.Type, addr, s.mem()) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 1434 | case OLITERAL: |
Keith Randall | e707fbe | 2015-06-11 10:20:39 -0700 | [diff] [blame] | 1435 | switch n.Val().Ctype() { |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 1436 | case CTINT: |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 1437 | i := n.Int64() |
Keith Randall | 9cb332e | 2015-07-28 14:19:20 -0700 | [diff] [blame] | 1438 | switch n.Type.Size() { |
| 1439 | case 1: |
| 1440 | return s.constInt8(n.Type, int8(i)) |
| 1441 | case 2: |
| 1442 | return s.constInt16(n.Type, int16(i)) |
| 1443 | case 4: |
| 1444 | return s.constInt32(n.Type, int32(i)) |
| 1445 | case 8: |
| 1446 | return s.constInt64(n.Type, i) |
| 1447 | default: |
| 1448 | s.Fatalf("bad integer size %d", n.Type.Size()) |
| 1449 | return nil |
| 1450 | } |
| 1451 | case CTSTR: |
Josh Bleecher Snyder | 3921427 | 2016-03-06 18:06:09 -0800 | [diff] [blame] | 1452 | if n.Val().U == "" { |
| 1453 | return s.constEmptyString(n.Type) |
| 1454 | } |
Keith Randall | 9cb332e | 2015-07-28 14:19:20 -0700 | [diff] [blame] | 1455 | return s.entryNewValue0A(ssa.OpConstString, n.Type, n.Val().U) |
| 1456 | case CTBOOL: |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 1457 | v := s.constBool(n.Val().U.(bool)) |
| 1458 | // For some reason the frontend gets the line numbers of |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 1459 | // CTBOOL literals totally wrong. Fix it here by grabbing |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 1460 | // the line number of the enclosing AST node. |
| 1461 | if len(s.line) >= 2 { |
| 1462 | v.Line = s.line[len(s.line)-2] |
| 1463 | } |
| 1464 | return v |
Brad Fitzpatrick | 337b7e7 | 2015-07-13 17:30:42 -0600 | [diff] [blame] | 1465 | case CTNIL: |
Keith Randall | 9f954db | 2015-08-18 10:26:28 -0700 | [diff] [blame] | 1466 | t := n.Type |
| 1467 | switch { |
| 1468 | case t.IsSlice(): |
Josh Bleecher Snyder | 3921427 | 2016-03-06 18:06:09 -0800 | [diff] [blame] | 1469 | return s.constSlice(t) |
Keith Randall | 9f954db | 2015-08-18 10:26:28 -0700 | [diff] [blame] | 1470 | case t.IsInterface(): |
Josh Bleecher Snyder | 3921427 | 2016-03-06 18:06:09 -0800 | [diff] [blame] | 1471 | return s.constInterface(t) |
Keith Randall | 9f954db | 2015-08-18 10:26:28 -0700 | [diff] [blame] | 1472 | default: |
Josh Bleecher Snyder | 3921427 | 2016-03-06 18:06:09 -0800 | [diff] [blame] | 1473 | return s.constNil(t) |
Keith Randall | 9f954db | 2015-08-18 10:26:28 -0700 | [diff] [blame] | 1474 | } |
David Chase | 997a9f3 | 2015-08-12 16:38:11 -0400 | [diff] [blame] | 1475 | case CTFLT: |
| 1476 | f := n.Val().U.(*Mpflt) |
| 1477 | switch n.Type.Size() { |
| 1478 | case 4: |
Matthew Dempsky | d325387 | 2016-03-20 13:55:42 -0700 | [diff] [blame] | 1479 | return s.constFloat32(n.Type, f.Float32()) |
David Chase | 997a9f3 | 2015-08-12 16:38:11 -0400 | [diff] [blame] | 1480 | case 8: |
Matthew Dempsky | d325387 | 2016-03-20 13:55:42 -0700 | [diff] [blame] | 1481 | return s.constFloat64(n.Type, f.Float64()) |
David Chase | 997a9f3 | 2015-08-12 16:38:11 -0400 | [diff] [blame] | 1482 | default: |
| 1483 | s.Fatalf("bad float size %d", n.Type.Size()) |
| 1484 | return nil |
| 1485 | } |
David Chase | 5257858 | 2015-08-28 14:24:10 -0400 | [diff] [blame] | 1486 | case CTCPLX: |
| 1487 | c := n.Val().U.(*Mpcplx) |
| 1488 | r := &c.Real |
| 1489 | i := &c.Imag |
| 1490 | switch n.Type.Size() { |
| 1491 | case 8: |
| 1492 | { |
| 1493 | pt := Types[TFLOAT32] |
| 1494 | return s.newValue2(ssa.OpComplexMake, n.Type, |
Matthew Dempsky | d325387 | 2016-03-20 13:55:42 -0700 | [diff] [blame] | 1495 | s.constFloat32(pt, r.Float32()), |
| 1496 | s.constFloat32(pt, i.Float32())) |
David Chase | 5257858 | 2015-08-28 14:24:10 -0400 | [diff] [blame] | 1497 | } |
| 1498 | case 16: |
| 1499 | { |
| 1500 | pt := Types[TFLOAT64] |
| 1501 | return s.newValue2(ssa.OpComplexMake, n.Type, |
Matthew Dempsky | d325387 | 2016-03-20 13:55:42 -0700 | [diff] [blame] | 1502 | s.constFloat64(pt, r.Float64()), |
| 1503 | s.constFloat64(pt, i.Float64())) |
David Chase | 5257858 | 2015-08-28 14:24:10 -0400 | [diff] [blame] | 1504 | } |
| 1505 | default: |
| 1506 | s.Fatalf("bad float size %d", n.Type.Size()) |
| 1507 | return nil |
| 1508 | } |
David Chase | 997a9f3 | 2015-08-12 16:38:11 -0400 | [diff] [blame] | 1509 | |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 1510 | default: |
Josh Bleecher Snyder | 37ddc27 | 2015-06-24 14:03:39 -0700 | [diff] [blame] | 1511 | s.Unimplementedf("unhandled OLITERAL %v", n.Val().Ctype()) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 1512 | return nil |
| 1513 | } |
Keith Randall | 0ad9c8c | 2015-06-12 16:24:33 -0700 | [diff] [blame] | 1514 | case OCONVNOP: |
Josh Bleecher Snyder | 95aff4d | 2015-07-28 14:31:25 -0700 | [diff] [blame] | 1515 | to := n.Type |
| 1516 | from := n.Left.Type |
Josh Bleecher Snyder | 95aff4d | 2015-07-28 14:31:25 -0700 | [diff] [blame] | 1517 | |
| 1518 | // Assume everything will work out, so set up our return value. |
| 1519 | // Anything interesting that happens from here is a fatal. |
Keith Randall | 0ad9c8c | 2015-06-12 16:24:33 -0700 | [diff] [blame] | 1520 | x := s.expr(n.Left) |
David Chase | e99dd52 | 2015-10-19 11:36:07 -0400 | [diff] [blame] | 1521 | |
| 1522 | // Special case for not confusing GC and liveness. |
| 1523 | // We don't want pointers accidentally classified |
| 1524 | // as not-pointers or vice-versa because of copy |
| 1525 | // elision. |
Matthew Dempsky | 788f112 | 2016-03-28 10:55:44 -0700 | [diff] [blame] | 1526 | if to.IsPtrShaped() != from.IsPtrShaped() { |
Keith Randall | 7807bda | 2015-11-10 15:35:36 -0800 | [diff] [blame] | 1527 | return s.newValue2(ssa.OpConvert, to, x, s.mem()) |
David Chase | e99dd52 | 2015-10-19 11:36:07 -0400 | [diff] [blame] | 1528 | } |
| 1529 | |
Josh Bleecher Snyder | 95aff4d | 2015-07-28 14:31:25 -0700 | [diff] [blame] | 1530 | v := s.newValue1(ssa.OpCopy, to, x) // ensure that v has the right type |
| 1531 | |
Todd Neal | def7c65 | 2015-09-07 19:07:02 -0500 | [diff] [blame] | 1532 | // CONVNOP closure |
Matthew Dempsky | 788f112 | 2016-03-28 10:55:44 -0700 | [diff] [blame] | 1533 | if to.Etype == TFUNC && from.IsPtrShaped() { |
Todd Neal | def7c65 | 2015-09-07 19:07:02 -0500 | [diff] [blame] | 1534 | return v |
| 1535 | } |
| 1536 | |
Josh Bleecher Snyder | 95aff4d | 2015-07-28 14:31:25 -0700 | [diff] [blame] | 1537 | // named <--> unnamed type or typed <--> untyped const |
| 1538 | if from.Etype == to.Etype { |
| 1539 | return v |
| 1540 | } |
David Chase | e99dd52 | 2015-10-19 11:36:07 -0400 | [diff] [blame] | 1541 | |
Josh Bleecher Snyder | 95aff4d | 2015-07-28 14:31:25 -0700 | [diff] [blame] | 1542 | // unsafe.Pointer <--> *T |
| 1543 | if to.Etype == TUNSAFEPTR && from.IsPtr() || from.Etype == TUNSAFEPTR && to.IsPtr() { |
| 1544 | return v |
| 1545 | } |
| 1546 | |
| 1547 | dowidth(from) |
| 1548 | dowidth(to) |
| 1549 | if from.Width != to.Width { |
| 1550 | s.Fatalf("CONVNOP width mismatch %v (%d) -> %v (%d)\n", from, from.Width, to, to.Width) |
| 1551 | return nil |
| 1552 | } |
| 1553 | if etypesign(from.Etype) != etypesign(to.Etype) { |
Keith Randall | 4304fbc | 2015-11-16 13:20:16 -0800 | [diff] [blame] | 1554 | s.Fatalf("CONVNOP sign mismatch %v (%s) -> %v (%s)\n", from, Econv(from.Etype), to, Econv(to.Etype)) |
Josh Bleecher Snyder | 95aff4d | 2015-07-28 14:31:25 -0700 | [diff] [blame] | 1555 | return nil |
| 1556 | } |
| 1557 | |
Ian Lance Taylor | 88e1803 | 2016-03-01 15:17:34 -0800 | [diff] [blame] | 1558 | if instrumenting { |
David Chase | 57670ad | 2015-10-09 16:48:30 -0400 | [diff] [blame] | 1559 | // These appear to be fine, but they fail the |
| 1560 | // integer constraint below, so okay them here. |
| 1561 | // Sample non-integer conversion: map[string]string -> *uint8 |
| 1562 | return v |
Josh Bleecher Snyder | 95aff4d | 2015-07-28 14:31:25 -0700 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | if etypesign(from.Etype) == 0 { |
| 1566 | s.Fatalf("CONVNOP unrecognized non-integer %v -> %v\n", from, to) |
| 1567 | return nil |
| 1568 | } |
| 1569 | |
| 1570 | // integer, same width, same sign |
| 1571 | return v |
| 1572 | |
Michael Matloob | 73054f5 | 2015-06-14 11:38:46 -0700 | [diff] [blame] | 1573 | case OCONV: |
| 1574 | x := s.expr(n.Left) |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 1575 | ft := n.Left.Type // from type |
| 1576 | tt := n.Type // to type |
| 1577 | if ft.IsInteger() && tt.IsInteger() { |
| 1578 | var op ssa.Op |
| 1579 | if tt.Size() == ft.Size() { |
Josh Bleecher Snyder | 95aff4d | 2015-07-28 14:31:25 -0700 | [diff] [blame] | 1580 | op = ssa.OpCopy |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 1581 | } else if tt.Size() < ft.Size() { |
| 1582 | // truncation |
| 1583 | switch 10*ft.Size() + tt.Size() { |
| 1584 | case 21: |
| 1585 | op = ssa.OpTrunc16to8 |
| 1586 | case 41: |
| 1587 | op = ssa.OpTrunc32to8 |
| 1588 | case 42: |
| 1589 | op = ssa.OpTrunc32to16 |
| 1590 | case 81: |
| 1591 | op = ssa.OpTrunc64to8 |
| 1592 | case 82: |
| 1593 | op = ssa.OpTrunc64to16 |
| 1594 | case 84: |
| 1595 | op = ssa.OpTrunc64to32 |
| 1596 | default: |
| 1597 | s.Fatalf("weird integer truncation %s -> %s", ft, tt) |
| 1598 | } |
| 1599 | } else if ft.IsSigned() { |
| 1600 | // sign extension |
| 1601 | switch 10*ft.Size() + tt.Size() { |
| 1602 | case 12: |
| 1603 | op = ssa.OpSignExt8to16 |
| 1604 | case 14: |
| 1605 | op = ssa.OpSignExt8to32 |
| 1606 | case 18: |
| 1607 | op = ssa.OpSignExt8to64 |
| 1608 | case 24: |
| 1609 | op = ssa.OpSignExt16to32 |
| 1610 | case 28: |
| 1611 | op = ssa.OpSignExt16to64 |
| 1612 | case 48: |
| 1613 | op = ssa.OpSignExt32to64 |
| 1614 | default: |
| 1615 | s.Fatalf("bad integer sign extension %s -> %s", ft, tt) |
| 1616 | } |
| 1617 | } else { |
| 1618 | // zero extension |
| 1619 | switch 10*ft.Size() + tt.Size() { |
| 1620 | case 12: |
| 1621 | op = ssa.OpZeroExt8to16 |
| 1622 | case 14: |
| 1623 | op = ssa.OpZeroExt8to32 |
| 1624 | case 18: |
| 1625 | op = ssa.OpZeroExt8to64 |
| 1626 | case 24: |
| 1627 | op = ssa.OpZeroExt16to32 |
| 1628 | case 28: |
| 1629 | op = ssa.OpZeroExt16to64 |
| 1630 | case 48: |
| 1631 | op = ssa.OpZeroExt32to64 |
| 1632 | default: |
| 1633 | s.Fatalf("weird integer sign extension %s -> %s", ft, tt) |
| 1634 | } |
| 1635 | } |
| 1636 | return s.newValue1(op, n.Type, x) |
| 1637 | } |
David Chase | 4282588 | 2015-08-20 15:14:20 -0400 | [diff] [blame] | 1638 | |
David Chase | d052bbd | 2015-09-01 17:09:00 -0400 | [diff] [blame] | 1639 | if ft.IsFloat() || tt.IsFloat() { |
| 1640 | conv, ok := fpConvOpToSSA[twoTypes{s.concreteEtype(ft), s.concreteEtype(tt)}] |
| 1641 | if !ok { |
| 1642 | s.Fatalf("weird float conversion %s -> %s", ft, tt) |
David Chase | 4282588 | 2015-08-20 15:14:20 -0400 | [diff] [blame] | 1643 | } |
David Chase | d052bbd | 2015-09-01 17:09:00 -0400 | [diff] [blame] | 1644 | op1, op2, it := conv.op1, conv.op2, conv.intermediateType |
| 1645 | |
| 1646 | if op1 != ssa.OpInvalid && op2 != ssa.OpInvalid { |
| 1647 | // normal case, not tripping over unsigned 64 |
| 1648 | if op1 == ssa.OpCopy { |
| 1649 | if op2 == ssa.OpCopy { |
| 1650 | return x |
| 1651 | } |
| 1652 | return s.newValue1(op2, n.Type, x) |
| 1653 | } |
| 1654 | if op2 == ssa.OpCopy { |
| 1655 | return s.newValue1(op1, n.Type, x) |
| 1656 | } |
| 1657 | return s.newValue1(op2, n.Type, s.newValue1(op1, Types[it], x)) |
| 1658 | } |
| 1659 | // Tricky 64-bit unsigned cases. |
| 1660 | if ft.IsInteger() { |
| 1661 | // therefore tt is float32 or float64, and ft is also unsigned |
David Chase | 4282588 | 2015-08-20 15:14:20 -0400 | [diff] [blame] | 1662 | if tt.Size() == 4 { |
| 1663 | return s.uint64Tofloat32(n, x, ft, tt) |
| 1664 | } |
| 1665 | if tt.Size() == 8 { |
| 1666 | return s.uint64Tofloat64(n, x, ft, tt) |
| 1667 | } |
David Chase | d052bbd | 2015-09-01 17:09:00 -0400 | [diff] [blame] | 1668 | s.Fatalf("weird unsigned integer to float conversion %s -> %s", ft, tt) |
David Chase | 4282588 | 2015-08-20 15:14:20 -0400 | [diff] [blame] | 1669 | } |
David Chase | d052bbd | 2015-09-01 17:09:00 -0400 | [diff] [blame] | 1670 | // therefore ft is float32 or float64, and tt is unsigned integer |
David Chase | 7315106 | 2015-08-26 14:25:40 -0400 | [diff] [blame] | 1671 | if ft.Size() == 4 { |
David Chase | d052bbd | 2015-09-01 17:09:00 -0400 | [diff] [blame] | 1672 | return s.float32ToUint64(n, x, ft, tt) |
David Chase | 7315106 | 2015-08-26 14:25:40 -0400 | [diff] [blame] | 1673 | } |
David Chase | d052bbd | 2015-09-01 17:09:00 -0400 | [diff] [blame] | 1674 | if ft.Size() == 8 { |
| 1675 | return s.float64ToUint64(n, x, ft, tt) |
David Chase | 7315106 | 2015-08-26 14:25:40 -0400 | [diff] [blame] | 1676 | } |
David Chase | d052bbd | 2015-09-01 17:09:00 -0400 | [diff] [blame] | 1677 | s.Fatalf("weird float to unsigned integer conversion %s -> %s", ft, tt) |
| 1678 | return nil |
David Chase | 4282588 | 2015-08-20 15:14:20 -0400 | [diff] [blame] | 1679 | } |
David Chase | 3a9d0ac | 2015-08-28 14:24:10 -0400 | [diff] [blame] | 1680 | |
| 1681 | if ft.IsComplex() && tt.IsComplex() { |
| 1682 | var op ssa.Op |
| 1683 | if ft.Size() == tt.Size() { |
| 1684 | op = ssa.OpCopy |
| 1685 | } else if ft.Size() == 8 && tt.Size() == 16 { |
| 1686 | op = ssa.OpCvt32Fto64F |
| 1687 | } else if ft.Size() == 16 && tt.Size() == 8 { |
| 1688 | op = ssa.OpCvt64Fto32F |
| 1689 | } else { |
| 1690 | s.Fatalf("weird complex conversion %s -> %s", ft, tt) |
| 1691 | } |
| 1692 | ftp := floatForComplex(ft) |
| 1693 | ttp := floatForComplex(tt) |
| 1694 | return s.newValue2(ssa.OpComplexMake, tt, |
| 1695 | s.newValue1(op, ttp, s.newValue1(ssa.OpComplexReal, ftp, x)), |
| 1696 | s.newValue1(op, ttp, s.newValue1(ssa.OpComplexImag, ftp, x))) |
| 1697 | } |
David Chase | 4282588 | 2015-08-20 15:14:20 -0400 | [diff] [blame] | 1698 | |
Keith Randall | 4304fbc | 2015-11-16 13:20:16 -0800 | [diff] [blame] | 1699 | s.Unimplementedf("unhandled OCONV %s -> %s", Econv(n.Left.Type.Etype), Econv(n.Type.Etype)) |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 1700 | return nil |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 1701 | |
Keith Randall | 269baa9 | 2015-09-17 10:31:16 -0700 | [diff] [blame] | 1702 | case ODOTTYPE: |
| 1703 | res, _ := s.dottype(n, false) |
| 1704 | return res |
| 1705 | |
Josh Bleecher Snyder | 46815b9 | 2015-06-24 17:48:22 -0700 | [diff] [blame] | 1706 | // binary ops |
| 1707 | case OLT, OEQ, ONE, OLE, OGE, OGT: |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 1708 | a := s.expr(n.Left) |
| 1709 | b := s.expr(n.Right) |
Keith Randall | db380bf | 2015-09-10 11:05:42 -0700 | [diff] [blame] | 1710 | if n.Left.Type.IsComplex() { |
Keith Randall | c244ce0 | 2015-09-10 14:59:00 -0700 | [diff] [blame] | 1711 | pt := floatForComplex(n.Left.Type) |
Keith Randall | db380bf | 2015-09-10 11:05:42 -0700 | [diff] [blame] | 1712 | op := s.ssaOp(OEQ, pt) |
| 1713 | r := s.newValue2(op, Types[TBOOL], s.newValue1(ssa.OpComplexReal, pt, a), s.newValue1(ssa.OpComplexReal, pt, b)) |
| 1714 | i := s.newValue2(op, Types[TBOOL], s.newValue1(ssa.OpComplexImag, pt, a), s.newValue1(ssa.OpComplexImag, pt, b)) |
| 1715 | c := s.newValue2(ssa.OpAnd8, Types[TBOOL], r, i) |
| 1716 | switch n.Op { |
| 1717 | case OEQ: |
| 1718 | return c |
| 1719 | case ONE: |
| 1720 | return s.newValue1(ssa.OpNot, Types[TBOOL], c) |
| 1721 | default: |
| 1722 | s.Fatalf("ordered complex compare %s", opnames[n.Op]) |
| 1723 | } |
Keith Randall | db380bf | 2015-09-10 11:05:42 -0700 | [diff] [blame] | 1724 | } |
Josh Bleecher Snyder | 85e0329 | 2015-07-30 11:03:05 -0700 | [diff] [blame] | 1725 | return s.newValue2(s.ssaOp(n.Op, n.Left.Type), Types[TBOOL], a, b) |
David Chase | 3a9d0ac | 2015-08-28 14:24:10 -0400 | [diff] [blame] | 1726 | case OMUL: |
| 1727 | a := s.expr(n.Left) |
| 1728 | b := s.expr(n.Right) |
| 1729 | if n.Type.IsComplex() { |
| 1730 | mulop := ssa.OpMul64F |
| 1731 | addop := ssa.OpAdd64F |
| 1732 | subop := ssa.OpSub64F |
| 1733 | pt := floatForComplex(n.Type) // Could be Float32 or Float64 |
| 1734 | wt := Types[TFLOAT64] // Compute in Float64 to minimize cancellation error |
| 1735 | |
| 1736 | areal := s.newValue1(ssa.OpComplexReal, pt, a) |
| 1737 | breal := s.newValue1(ssa.OpComplexReal, pt, b) |
| 1738 | aimag := s.newValue1(ssa.OpComplexImag, pt, a) |
| 1739 | bimag := s.newValue1(ssa.OpComplexImag, pt, b) |
| 1740 | |
| 1741 | if pt != wt { // Widen for calculation |
| 1742 | areal = s.newValue1(ssa.OpCvt32Fto64F, wt, areal) |
| 1743 | breal = s.newValue1(ssa.OpCvt32Fto64F, wt, breal) |
| 1744 | aimag = s.newValue1(ssa.OpCvt32Fto64F, wt, aimag) |
| 1745 | bimag = s.newValue1(ssa.OpCvt32Fto64F, wt, bimag) |
| 1746 | } |
| 1747 | |
| 1748 | xreal := s.newValue2(subop, wt, s.newValue2(mulop, wt, areal, breal), s.newValue2(mulop, wt, aimag, bimag)) |
| 1749 | ximag := s.newValue2(addop, wt, s.newValue2(mulop, wt, areal, bimag), s.newValue2(mulop, wt, aimag, breal)) |
| 1750 | |
| 1751 | if pt != wt { // Narrow to store back |
| 1752 | xreal = s.newValue1(ssa.OpCvt64Fto32F, pt, xreal) |
| 1753 | ximag = s.newValue1(ssa.OpCvt64Fto32F, pt, ximag) |
| 1754 | } |
| 1755 | |
| 1756 | return s.newValue2(ssa.OpComplexMake, n.Type, xreal, ximag) |
| 1757 | } |
| 1758 | return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b) |
| 1759 | |
| 1760 | case ODIV: |
| 1761 | a := s.expr(n.Left) |
| 1762 | b := s.expr(n.Right) |
| 1763 | if n.Type.IsComplex() { |
| 1764 | // TODO this is not executed because the front-end substitutes a runtime call. |
| 1765 | // That probably ought to change; with modest optimization the widen/narrow |
| 1766 | // conversions could all be elided in larger expression trees. |
| 1767 | mulop := ssa.OpMul64F |
| 1768 | addop := ssa.OpAdd64F |
| 1769 | subop := ssa.OpSub64F |
| 1770 | divop := ssa.OpDiv64F |
| 1771 | pt := floatForComplex(n.Type) // Could be Float32 or Float64 |
| 1772 | wt := Types[TFLOAT64] // Compute in Float64 to minimize cancellation error |
| 1773 | |
| 1774 | areal := s.newValue1(ssa.OpComplexReal, pt, a) |
| 1775 | breal := s.newValue1(ssa.OpComplexReal, pt, b) |
| 1776 | aimag := s.newValue1(ssa.OpComplexImag, pt, a) |
| 1777 | bimag := s.newValue1(ssa.OpComplexImag, pt, b) |
| 1778 | |
| 1779 | if pt != wt { // Widen for calculation |
| 1780 | areal = s.newValue1(ssa.OpCvt32Fto64F, wt, areal) |
| 1781 | breal = s.newValue1(ssa.OpCvt32Fto64F, wt, breal) |
| 1782 | aimag = s.newValue1(ssa.OpCvt32Fto64F, wt, aimag) |
| 1783 | bimag = s.newValue1(ssa.OpCvt32Fto64F, wt, bimag) |
| 1784 | } |
| 1785 | |
| 1786 | denom := s.newValue2(addop, wt, s.newValue2(mulop, wt, breal, breal), s.newValue2(mulop, wt, bimag, bimag)) |
| 1787 | xreal := s.newValue2(addop, wt, s.newValue2(mulop, wt, areal, breal), s.newValue2(mulop, wt, aimag, bimag)) |
| 1788 | ximag := s.newValue2(subop, wt, s.newValue2(mulop, wt, aimag, breal), s.newValue2(mulop, wt, areal, bimag)) |
| 1789 | |
| 1790 | // TODO not sure if this is best done in wide precision or narrow |
| 1791 | // Double-rounding might be an issue. |
| 1792 | // Note that the pre-SSA implementation does the entire calculation |
| 1793 | // in wide format, so wide is compatible. |
| 1794 | xreal = s.newValue2(divop, wt, xreal, denom) |
| 1795 | ximag = s.newValue2(divop, wt, ximag, denom) |
| 1796 | |
| 1797 | if pt != wt { // Narrow to store back |
| 1798 | xreal = s.newValue1(ssa.OpCvt64Fto32F, pt, xreal) |
| 1799 | ximag = s.newValue1(ssa.OpCvt64Fto32F, pt, ximag) |
| 1800 | } |
David Chase | 3a9d0ac | 2015-08-28 14:24:10 -0400 | [diff] [blame] | 1801 | return s.newValue2(ssa.OpComplexMake, n.Type, xreal, ximag) |
| 1802 | } |
David Chase | 18559e2 | 2015-10-28 13:55:46 -0400 | [diff] [blame] | 1803 | if n.Type.IsFloat() { |
| 1804 | return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b) |
| 1805 | } else { |
| 1806 | // do a size-appropriate check for zero |
| 1807 | cmp := s.newValue2(s.ssaOp(ONE, n.Type), Types[TBOOL], b, s.zeroVal(n.Type)) |
| 1808 | s.check(cmp, panicdivide) |
| 1809 | return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b) |
| 1810 | } |
| 1811 | case OMOD: |
| 1812 | a := s.expr(n.Left) |
| 1813 | b := s.expr(n.Right) |
| 1814 | // do a size-appropriate check for zero |
| 1815 | cmp := s.newValue2(s.ssaOp(ONE, n.Type), Types[TBOOL], b, s.zeroVal(n.Type)) |
| 1816 | s.check(cmp, panicdivide) |
David Chase | 3a9d0ac | 2015-08-28 14:24:10 -0400 | [diff] [blame] | 1817 | return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b) |
| 1818 | case OADD, OSUB: |
| 1819 | a := s.expr(n.Left) |
| 1820 | b := s.expr(n.Right) |
| 1821 | if n.Type.IsComplex() { |
| 1822 | pt := floatForComplex(n.Type) |
| 1823 | op := s.ssaOp(n.Op, pt) |
| 1824 | return s.newValue2(ssa.OpComplexMake, n.Type, |
| 1825 | s.newValue2(op, pt, s.newValue1(ssa.OpComplexReal, pt, a), s.newValue1(ssa.OpComplexReal, pt, b)), |
| 1826 | s.newValue2(op, pt, s.newValue1(ssa.OpComplexImag, pt, a), s.newValue1(ssa.OpComplexImag, pt, b))) |
| 1827 | } |
| 1828 | return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b) |
David Chase | 18559e2 | 2015-10-28 13:55:46 -0400 | [diff] [blame] | 1829 | case OAND, OOR, OHMUL, OXOR: |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 1830 | a := s.expr(n.Left) |
| 1831 | b := s.expr(n.Right) |
Keith Randall | 67fdb0d | 2015-07-19 15:48:20 -0700 | [diff] [blame] | 1832 | return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b) |
Keith Randall | 4b80315 | 2015-07-29 17:07:09 -0700 | [diff] [blame] | 1833 | case OLSH, ORSH: |
| 1834 | a := s.expr(n.Left) |
| 1835 | b := s.expr(n.Right) |
| 1836 | return s.newValue2(s.ssaShiftOp(n.Op, n.Type, n.Right.Type), a.Type, a, b) |
David Chase | 40aba8c | 2015-08-05 22:11:14 -0400 | [diff] [blame] | 1837 | case OLROT: |
| 1838 | a := s.expr(n.Left) |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 1839 | i := n.Right.Int64() |
David Chase | 40aba8c | 2015-08-05 22:11:14 -0400 | [diff] [blame] | 1840 | if i <= 0 || i >= n.Type.Size()*8 { |
| 1841 | s.Fatalf("Wrong rotate distance for LROT, expected 1 through %d, saw %d", n.Type.Size()*8-1, i) |
| 1842 | } |
| 1843 | return s.newValue1I(s.ssaRotateOp(n.Op, n.Type), a.Type, i, a) |
Brad Fitzpatrick | e816711 | 2015-07-10 12:58:53 -0600 | [diff] [blame] | 1844 | case OANDAND, OOROR: |
| 1845 | // To implement OANDAND (and OOROR), we introduce a |
| 1846 | // new temporary variable to hold the result. The |
| 1847 | // variable is associated with the OANDAND node in the |
| 1848 | // s.vars table (normally variables are only |
| 1849 | // associated with ONAME nodes). We convert |
| 1850 | // A && B |
| 1851 | // to |
| 1852 | // var = A |
| 1853 | // if var { |
| 1854 | // var = B |
| 1855 | // } |
| 1856 | // Using var in the subsequent block introduces the |
| 1857 | // necessary phi variable. |
| 1858 | el := s.expr(n.Left) |
| 1859 | s.vars[n] = el |
| 1860 | |
| 1861 | b := s.endBlock() |
| 1862 | b.Kind = ssa.BlockIf |
Keith Randall | 56e0ecc | 2016-03-15 20:45:50 -0700 | [diff] [blame] | 1863 | b.SetControl(el) |
Josh Bleecher Snyder | bbf8c5c | 2015-08-11 17:28:56 -0700 | [diff] [blame] | 1864 | // In theory, we should set b.Likely here based on context. |
| 1865 | // However, gc only gives us likeliness hints |
| 1866 | // in a single place, for plain OIF statements, |
| 1867 | // and passing around context is finnicky, so don't bother for now. |
Brad Fitzpatrick | e816711 | 2015-07-10 12:58:53 -0600 | [diff] [blame] | 1868 | |
| 1869 | bRight := s.f.NewBlock(ssa.BlockPlain) |
| 1870 | bResult := s.f.NewBlock(ssa.BlockPlain) |
| 1871 | if n.Op == OANDAND { |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 1872 | b.AddEdgeTo(bRight) |
| 1873 | b.AddEdgeTo(bResult) |
Brad Fitzpatrick | e816711 | 2015-07-10 12:58:53 -0600 | [diff] [blame] | 1874 | } else if n.Op == OOROR { |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 1875 | b.AddEdgeTo(bResult) |
| 1876 | b.AddEdgeTo(bRight) |
Brad Fitzpatrick | e816711 | 2015-07-10 12:58:53 -0600 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | s.startBlock(bRight) |
| 1880 | er := s.expr(n.Right) |
| 1881 | s.vars[n] = er |
| 1882 | |
| 1883 | b = s.endBlock() |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 1884 | b.AddEdgeTo(bResult) |
Brad Fitzpatrick | e816711 | 2015-07-10 12:58:53 -0600 | [diff] [blame] | 1885 | |
| 1886 | s.startBlock(bResult) |
Josh Bleecher Snyder | 35ad1fc | 2015-08-27 10:11:08 -0700 | [diff] [blame] | 1887 | return s.variable(n, Types[TBOOL]) |
Keith Randall | 7e39072 | 2015-09-12 14:14:02 -0700 | [diff] [blame] | 1888 | case OCOMPLEX: |
| 1889 | r := s.expr(n.Left) |
| 1890 | i := s.expr(n.Right) |
| 1891 | return s.newValue2(ssa.OpComplexMake, n.Type, r, i) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 1892 | |
Josh Bleecher Snyder | 4178f20 | 2015-09-05 19:28:00 -0700 | [diff] [blame] | 1893 | // unary ops |
David Chase | 3a9d0ac | 2015-08-28 14:24:10 -0400 | [diff] [blame] | 1894 | case OMINUS: |
| 1895 | a := s.expr(n.Left) |
| 1896 | if n.Type.IsComplex() { |
| 1897 | tp := floatForComplex(n.Type) |
| 1898 | negop := s.ssaOp(n.Op, tp) |
| 1899 | return s.newValue2(ssa.OpComplexMake, n.Type, |
| 1900 | s.newValue1(negop, tp, s.newValue1(ssa.OpComplexReal, tp, a)), |
| 1901 | s.newValue1(negop, tp, s.newValue1(ssa.OpComplexImag, tp, a))) |
| 1902 | } |
| 1903 | return s.newValue1(s.ssaOp(n.Op, n.Type), a.Type, a) |
Keith Randall | a329e21 | 2015-09-12 13:26:57 -0700 | [diff] [blame] | 1904 | case ONOT, OCOM, OSQRT: |
Brad Fitzpatrick | d9c72d7 | 2015-07-10 11:25:48 -0600 | [diff] [blame] | 1905 | a := s.expr(n.Left) |
Alexandru Moșoi | 954d5ad | 2015-07-21 16:58:18 +0200 | [diff] [blame] | 1906 | return s.newValue1(s.ssaOp(n.Op, n.Type), a.Type, a) |
Keith Randall | 2f51807 | 2015-09-10 11:37:09 -0700 | [diff] [blame] | 1907 | case OIMAG, OREAL: |
| 1908 | a := s.expr(n.Left) |
| 1909 | return s.newValue1(s.ssaOp(n.Op, n.Left.Type), n.Type, a) |
Josh Bleecher Snyder | 4178f20 | 2015-09-05 19:28:00 -0700 | [diff] [blame] | 1910 | case OPLUS: |
| 1911 | return s.expr(n.Left) |
Brad Fitzpatrick | d9c72d7 | 2015-07-10 11:25:48 -0600 | [diff] [blame] | 1912 | |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 1913 | case OADDR: |
David Chase | 57670ad | 2015-10-09 16:48:30 -0400 | [diff] [blame] | 1914 | return s.addr(n.Left, n.Bounded) |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 1915 | |
Josh Bleecher Snyder | 25d1916 | 2015-07-28 12:37:46 -0700 | [diff] [blame] | 1916 | case OINDREG: |
| 1917 | if int(n.Reg) != Thearch.REGSP { |
| 1918 | s.Unimplementedf("OINDREG of non-SP register %s in expr: %v", obj.Rconv(int(n.Reg)), n) |
| 1919 | return nil |
| 1920 | } |
| 1921 | addr := s.entryNewValue1I(ssa.OpOffPtr, Ptrto(n.Type), n.Xoffset, s.sp) |
| 1922 | return s.newValue2(ssa.OpLoad, n.Type, addr, s.mem()) |
| 1923 | |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 1924 | case OIND: |
| 1925 | p := s.expr(n.Left) |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 1926 | s.nilCheck(p) |
Keith Randall | 8f22b52 | 2015-06-11 21:29:25 -0700 | [diff] [blame] | 1927 | return s.newValue2(ssa.OpLoad, n.Type, p, s.mem()) |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 1928 | |
Keith Randall | cd7e059 | 2015-07-15 21:33:49 -0700 | [diff] [blame] | 1929 | case ODOT: |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 1930 | t := n.Left.Type |
| 1931 | if canSSAType(t) { |
| 1932 | v := s.expr(n.Left) |
Matthew Dempsky | 1b9f168 | 2016-03-14 12:45:18 -0700 | [diff] [blame] | 1933 | return s.newValue1I(ssa.OpStructSelect, n.Type, int64(fieldIdx(n)), v) |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 1934 | } |
David Chase | 57670ad | 2015-10-09 16:48:30 -0400 | [diff] [blame] | 1935 | p := s.addr(n, false) |
Keith Randall | 9703564 | 2015-10-09 09:33:29 -0700 | [diff] [blame] | 1936 | return s.newValue2(ssa.OpLoad, n.Type, p, s.mem()) |
Keith Randall | cd7e059 | 2015-07-15 21:33:49 -0700 | [diff] [blame] | 1937 | |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 1938 | case ODOTPTR: |
| 1939 | p := s.expr(n.Left) |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 1940 | s.nilCheck(p) |
Josh Bleecher Snyder | da1802f | 2016-03-04 12:34:43 -0800 | [diff] [blame] | 1941 | p = s.newValue1I(ssa.OpOffPtr, p.Type, n.Xoffset, p) |
Keith Randall | 8f22b52 | 2015-06-11 21:29:25 -0700 | [diff] [blame] | 1942 | return s.newValue2(ssa.OpLoad, n.Type, p, s.mem()) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 1943 | |
| 1944 | case OINDEX: |
Keith Randall | 9703564 | 2015-10-09 09:33:29 -0700 | [diff] [blame] | 1945 | switch { |
| 1946 | case n.Left.Type.IsString(): |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 1947 | a := s.expr(n.Left) |
| 1948 | i := s.expr(n.Right) |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 1949 | i = s.extendIndex(i) |
Keith Randall | 9703564 | 2015-10-09 09:33:29 -0700 | [diff] [blame] | 1950 | if !n.Bounded { |
| 1951 | len := s.newValue1(ssa.OpStringLen, Types[TINT], a) |
| 1952 | s.boundsCheck(i, len) |
Josh Bleecher Snyder | e00d609 | 2015-06-02 09:16:22 -0700 | [diff] [blame] | 1953 | } |
Keith Randall | 9703564 | 2015-10-09 09:33:29 -0700 | [diff] [blame] | 1954 | ptrtyp := Ptrto(Types[TUINT8]) |
| 1955 | ptr := s.newValue1(ssa.OpStringPtr, ptrtyp, a) |
Josh Bleecher Snyder | da1802f | 2016-03-04 12:34:43 -0800 | [diff] [blame] | 1956 | if Isconst(n.Right, CTINT) { |
Josh Bleecher Snyder | 5cab016 | 2016-04-01 14:51:02 -0700 | [diff] [blame] | 1957 | ptr = s.newValue1I(ssa.OpOffPtr, ptrtyp, n.Right.Int64(), ptr) |
Josh Bleecher Snyder | da1802f | 2016-03-04 12:34:43 -0800 | [diff] [blame] | 1958 | } else { |
| 1959 | ptr = s.newValue2(ssa.OpAddPtr, ptrtyp, ptr, i) |
| 1960 | } |
Keith Randall | 9703564 | 2015-10-09 09:33:29 -0700 | [diff] [blame] | 1961 | return s.newValue2(ssa.OpLoad, Types[TUINT8], ptr, s.mem()) |
| 1962 | case n.Left.Type.IsSlice(): |
David Chase | 57670ad | 2015-10-09 16:48:30 -0400 | [diff] [blame] | 1963 | p := s.addr(n, false) |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 1964 | return s.newValue2(ssa.OpLoad, n.Left.Type.Elem(), p, s.mem()) |
Keith Randall | 9703564 | 2015-10-09 09:33:29 -0700 | [diff] [blame] | 1965 | case n.Left.Type.IsArray(): |
| 1966 | // TODO: fix when we can SSA arrays of length 1. |
David Chase | 57670ad | 2015-10-09 16:48:30 -0400 | [diff] [blame] | 1967 | p := s.addr(n, false) |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 1968 | return s.newValue2(ssa.OpLoad, n.Left.Type.Elem(), p, s.mem()) |
Keith Randall | 9703564 | 2015-10-09 09:33:29 -0700 | [diff] [blame] | 1969 | default: |
| 1970 | s.Fatalf("bad type for index %v", n.Left.Type) |
| 1971 | return nil |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 1972 | } |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 1973 | |
Brad Fitzpatrick | 7af53d9 | 2015-07-10 10:47:28 -0600 | [diff] [blame] | 1974 | case OLEN, OCAP: |
Josh Bleecher Snyder | cc3f031 | 2015-07-03 18:41:28 -0700 | [diff] [blame] | 1975 | switch { |
Brad Fitzpatrick | 7af53d9 | 2015-07-10 10:47:28 -0600 | [diff] [blame] | 1976 | case n.Left.Type.IsSlice(): |
| 1977 | op := ssa.OpSliceLen |
| 1978 | if n.Op == OCAP { |
| 1979 | op = ssa.OpSliceCap |
| 1980 | } |
Josh Bleecher Snyder | 85e0329 | 2015-07-30 11:03:05 -0700 | [diff] [blame] | 1981 | return s.newValue1(op, Types[TINT], s.expr(n.Left)) |
Brad Fitzpatrick | 7af53d9 | 2015-07-10 10:47:28 -0600 | [diff] [blame] | 1982 | case n.Left.Type.IsString(): // string; not reachable for OCAP |
Josh Bleecher Snyder | 85e0329 | 2015-07-30 11:03:05 -0700 | [diff] [blame] | 1983 | return s.newValue1(ssa.OpStringLen, Types[TINT], s.expr(n.Left)) |
Todd Neal | 707af25 | 2015-08-28 15:56:43 -0500 | [diff] [blame] | 1984 | case n.Left.Type.IsMap(), n.Left.Type.IsChan(): |
| 1985 | return s.referenceTypeBuiltin(n, s.expr(n.Left)) |
Josh Bleecher Snyder | cc3f031 | 2015-07-03 18:41:28 -0700 | [diff] [blame] | 1986 | default: // array |
Josh Bleecher Snyder | 3a0783c | 2016-03-31 14:46:04 -0700 | [diff] [blame] | 1987 | return s.constInt(Types[TINT], n.Left.Type.NumElem()) |
Josh Bleecher Snyder | cc3f031 | 2015-07-03 18:41:28 -0700 | [diff] [blame] | 1988 | } |
| 1989 | |
Josh Bleecher Snyder | a2d1580 | 2015-08-12 10:12:14 -0700 | [diff] [blame] | 1990 | case OSPTR: |
| 1991 | a := s.expr(n.Left) |
| 1992 | if n.Left.Type.IsSlice() { |
| 1993 | return s.newValue1(ssa.OpSlicePtr, n.Type, a) |
| 1994 | } else { |
| 1995 | return s.newValue1(ssa.OpStringPtr, n.Type, a) |
| 1996 | } |
| 1997 | |
Keith Randall | d1c15a0 | 2015-08-04 15:47:22 -0700 | [diff] [blame] | 1998 | case OITAB: |
| 1999 | a := s.expr(n.Left) |
| 2000 | return s.newValue1(ssa.OpITab, n.Type, a) |
| 2001 | |
Josh Bleecher Snyder | 1792b36 | 2015-09-05 19:28:27 -0700 | [diff] [blame] | 2002 | case OEFACE: |
| 2003 | tab := s.expr(n.Left) |
| 2004 | data := s.expr(n.Right) |
Keith Randall | 808d7c7 | 2015-10-07 14:35:25 -0700 | [diff] [blame] | 2005 | // The frontend allows putting things like struct{*byte} in |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 2006 | // the data portion of an eface. But we don't want struct{*byte} |
Keith Randall | 808d7c7 | 2015-10-07 14:35:25 -0700 | [diff] [blame] | 2007 | // as a register type because (among other reasons) the liveness |
| 2008 | // analysis is confused by the "fat" variables that result from |
| 2009 | // such types being spilled. |
| 2010 | // So here we ensure that we are selecting the underlying pointer |
| 2011 | // when we build an eface. |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 2012 | // TODO: get rid of this now that structs can be SSA'd? |
Matthew Dempsky | 788f112 | 2016-03-28 10:55:44 -0700 | [diff] [blame] | 2013 | for !data.Type.IsPtrShaped() { |
Keith Randall | 808d7c7 | 2015-10-07 14:35:25 -0700 | [diff] [blame] | 2014 | switch { |
| 2015 | case data.Type.IsArray(): |
Matthew Dempsky | 0b28187 | 2016-03-10 14:35:39 -0800 | [diff] [blame] | 2016 | data = s.newValue1I(ssa.OpArrayIndex, data.Type.ElemType(), 0, data) |
Keith Randall | 808d7c7 | 2015-10-07 14:35:25 -0700 | [diff] [blame] | 2017 | case data.Type.IsStruct(): |
| 2018 | for i := data.Type.NumFields() - 1; i >= 0; i-- { |
| 2019 | f := data.Type.FieldType(i) |
| 2020 | if f.Size() == 0 { |
| 2021 | // eface type could also be struct{p *byte; q [0]int} |
| 2022 | continue |
| 2023 | } |
Matthew Dempsky | 1b9f168 | 2016-03-14 12:45:18 -0700 | [diff] [blame] | 2024 | data = s.newValue1I(ssa.OpStructSelect, f, int64(i), data) |
Keith Randall | 808d7c7 | 2015-10-07 14:35:25 -0700 | [diff] [blame] | 2025 | break |
| 2026 | } |
| 2027 | default: |
| 2028 | s.Fatalf("type being put into an eface isn't a pointer") |
| 2029 | } |
| 2030 | } |
Josh Bleecher Snyder | 1792b36 | 2015-09-05 19:28:27 -0700 | [diff] [blame] | 2031 | return s.newValue2(ssa.OpIMake, n.Type, tab, data) |
| 2032 | |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 2033 | case OSLICE, OSLICEARR: |
| 2034 | v := s.expr(n.Left) |
| 2035 | var i, j *ssa.Value |
| 2036 | if n.Right.Left != nil { |
| 2037 | i = s.extendIndex(s.expr(n.Right.Left)) |
| 2038 | } |
| 2039 | if n.Right.Right != nil { |
| 2040 | j = s.extendIndex(s.expr(n.Right.Right)) |
| 2041 | } |
| 2042 | p, l, c := s.slice(n.Left.Type, v, i, j, nil) |
| 2043 | return s.newValue3(ssa.OpSliceMake, n.Type, p, l, c) |
Keith Randall | 3526cf5 | 2015-08-24 23:52:03 -0700 | [diff] [blame] | 2044 | case OSLICESTR: |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 2045 | v := s.expr(n.Left) |
| 2046 | var i, j *ssa.Value |
| 2047 | if n.Right.Left != nil { |
| 2048 | i = s.extendIndex(s.expr(n.Right.Left)) |
Keith Randall | 3526cf5 | 2015-08-24 23:52:03 -0700 | [diff] [blame] | 2049 | } |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 2050 | if n.Right.Right != nil { |
| 2051 | j = s.extendIndex(s.expr(n.Right.Right)) |
Keith Randall | 3526cf5 | 2015-08-24 23:52:03 -0700 | [diff] [blame] | 2052 | } |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 2053 | p, l, _ := s.slice(n.Left.Type, v, i, j, nil) |
| 2054 | return s.newValue2(ssa.OpStringMake, n.Type, p, l) |
| 2055 | case OSLICE3, OSLICE3ARR: |
| 2056 | v := s.expr(n.Left) |
| 2057 | var i *ssa.Value |
| 2058 | if n.Right.Left != nil { |
| 2059 | i = s.extendIndex(s.expr(n.Right.Left)) |
Keith Randall | 3526cf5 | 2015-08-24 23:52:03 -0700 | [diff] [blame] | 2060 | } |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 2061 | j := s.extendIndex(s.expr(n.Right.Right.Left)) |
| 2062 | k := s.extendIndex(s.expr(n.Right.Right.Right)) |
| 2063 | p, l, c := s.slice(n.Left.Type, v, i, j, k) |
| 2064 | return s.newValue3(ssa.OpSliceMake, n.Type, p, l, c) |
Keith Randall | 3526cf5 | 2015-08-24 23:52:03 -0700 | [diff] [blame] | 2065 | |
David Chase | 8eec2bb | 2016-03-11 00:10:52 -0500 | [diff] [blame] | 2066 | case OCALLFUNC: |
| 2067 | if isIntrinsicCall1(n) { |
| 2068 | return s.intrinsicCall1(n) |
| 2069 | } |
| 2070 | fallthrough |
| 2071 | |
| 2072 | case OCALLINTER, OCALLMETH: |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 2073 | a := s.call(n, callNormal) |
| 2074 | return s.newValue2(ssa.OpLoad, n.Type, a, s.mem()) |
Josh Bleecher Snyder | 3d23afb | 2015-08-12 11:22:16 -0700 | [diff] [blame] | 2075 | |
| 2076 | case OGETG: |
Keith Randall | d694f83 | 2015-10-19 18:54:40 -0700 | [diff] [blame] | 2077 | return s.newValue1(ssa.OpGetG, n.Type, s.mem()) |
Josh Bleecher Snyder | 3d23afb | 2015-08-12 11:22:16 -0700 | [diff] [blame] | 2078 | |
Keith Randall | 9d22c10 | 2015-09-11 11:02:57 -0700 | [diff] [blame] | 2079 | case OAPPEND: |
Josh Bleecher Snyder | 5e1b7bd | 2016-04-04 10:58:21 -0700 | [diff] [blame] | 2080 | return s.exprAppend(n) |
Keith Randall | 9d22c10 | 2015-09-11 11:02:57 -0700 | [diff] [blame] | 2081 | |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 2082 | default: |
Josh Bleecher Snyder | 37ddc27 | 2015-06-24 14:03:39 -0700 | [diff] [blame] | 2083 | s.Unimplementedf("unhandled expr %s", opnames[n.Op]) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 2084 | return nil |
| 2085 | } |
| 2086 | } |
| 2087 | |
Josh Bleecher Snyder | 5e1b7bd | 2016-04-04 10:58:21 -0700 | [diff] [blame] | 2088 | // exprAppend converts an OAPPEND node n to an ssa.Value, adds it to s, and returns the Value. |
| 2089 | func (s *state) exprAppend(n *Node) *ssa.Value { |
| 2090 | // append(s, e1, e2, e3). Compile like: |
| 2091 | // ptr,len,cap := s |
| 2092 | // newlen := len + 3 |
| 2093 | // if newlen > s.cap { |
| 2094 | // ptr,_,cap = growslice(s, newlen) |
| 2095 | // } |
| 2096 | // *(ptr+len) = e1 |
| 2097 | // *(ptr+len+1) = e2 |
| 2098 | // *(ptr+len+2) = e3 |
| 2099 | // makeslice(ptr,newlen,cap) |
| 2100 | |
| 2101 | et := n.Type.Elem() |
| 2102 | pt := Ptrto(et) |
| 2103 | |
| 2104 | // Evaluate slice |
| 2105 | slice := s.expr(n.List.First()) |
| 2106 | |
| 2107 | // Allocate new blocks |
| 2108 | grow := s.f.NewBlock(ssa.BlockPlain) |
| 2109 | assign := s.f.NewBlock(ssa.BlockPlain) |
| 2110 | |
| 2111 | // Decide if we need to grow |
| 2112 | nargs := int64(n.List.Len() - 1) |
| 2113 | p := s.newValue1(ssa.OpSlicePtr, pt, slice) |
| 2114 | l := s.newValue1(ssa.OpSliceLen, Types[TINT], slice) |
| 2115 | c := s.newValue1(ssa.OpSliceCap, Types[TINT], slice) |
| 2116 | nl := s.newValue2(s.ssaOp(OADD, Types[TINT]), Types[TINT], l, s.constInt(Types[TINT], nargs)) |
| 2117 | cmp := s.newValue2(s.ssaOp(OGT, Types[TINT]), Types[TBOOL], nl, c) |
| 2118 | s.vars[&ptrVar] = p |
| 2119 | s.vars[&capVar] = c |
| 2120 | b := s.endBlock() |
| 2121 | b.Kind = ssa.BlockIf |
| 2122 | b.Likely = ssa.BranchUnlikely |
| 2123 | b.SetControl(cmp) |
| 2124 | b.AddEdgeTo(grow) |
| 2125 | b.AddEdgeTo(assign) |
| 2126 | |
| 2127 | // Call growslice |
| 2128 | s.startBlock(grow) |
| 2129 | taddr := s.newValue1A(ssa.OpAddr, Types[TUINTPTR], &ssa.ExternSymbol{Types[TUINTPTR], typenamesym(n.Type)}, s.sb) |
| 2130 | |
| 2131 | r := s.rtcall(growslice, true, []*Type{pt, Types[TINT], Types[TINT]}, taddr, p, l, c, nl) |
| 2132 | |
| 2133 | s.vars[&ptrVar] = r[0] |
| 2134 | // Note: we don't need to read r[1], the result's length. It will be nl. |
| 2135 | // (or maybe we should, we just have to spill/restore nl otherwise?) |
| 2136 | s.vars[&capVar] = r[2] |
| 2137 | b = s.endBlock() |
| 2138 | b.AddEdgeTo(assign) |
| 2139 | |
| 2140 | // assign new elements to slots |
| 2141 | s.startBlock(assign) |
| 2142 | |
| 2143 | // Evaluate args |
| 2144 | args := make([]*ssa.Value, 0, nargs) |
| 2145 | store := make([]bool, 0, nargs) |
| 2146 | for _, n := range n.List.Slice()[1:] { |
| 2147 | if canSSAType(n.Type) { |
| 2148 | args = append(args, s.expr(n)) |
| 2149 | store = append(store, true) |
| 2150 | } else { |
| 2151 | args = append(args, s.addr(n, false)) |
| 2152 | store = append(store, false) |
| 2153 | } |
| 2154 | } |
| 2155 | |
| 2156 | p = s.variable(&ptrVar, pt) // generates phi for ptr |
| 2157 | c = s.variable(&capVar, Types[TINT]) // generates phi for cap |
| 2158 | p2 := s.newValue2(ssa.OpPtrIndex, pt, p, l) |
| 2159 | // TODO: just one write barrier call for all of these writes? |
| 2160 | // TODO: maybe just one writeBarrier.enabled check? |
| 2161 | for i, arg := range args { |
| 2162 | addr := s.newValue2(ssa.OpPtrIndex, pt, p2, s.constInt(Types[TINT], int64(i))) |
| 2163 | if store[i] { |
| 2164 | if haspointers(et) { |
| 2165 | s.insertWBstore(et, addr, arg, n.Lineno, 0) |
| 2166 | } else { |
| 2167 | s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, et.Size(), addr, arg, s.mem()) |
| 2168 | } |
| 2169 | } else { |
| 2170 | if haspointers(et) { |
| 2171 | s.insertWBmove(et, addr, arg, n.Lineno) |
| 2172 | } else { |
| 2173 | s.vars[&memVar] = s.newValue3I(ssa.OpMove, ssa.TypeMem, et.Size(), addr, arg, s.mem()) |
| 2174 | } |
| 2175 | } |
| 2176 | } |
| 2177 | |
| 2178 | // make result |
| 2179 | delete(s.vars, &ptrVar) |
| 2180 | delete(s.vars, &capVar) |
| 2181 | return s.newValue3(ssa.OpSliceMake, n.Type, p, nl, c) |
| 2182 | } |
| 2183 | |
Keith Randall | 9918731 | 2015-11-02 16:56:53 -0800 | [diff] [blame] | 2184 | // condBranch evaluates the boolean expression cond and branches to yes |
| 2185 | // if cond is true and no if cond is false. |
| 2186 | // This function is intended to handle && and || better than just calling |
| 2187 | // s.expr(cond) and branching on the result. |
| 2188 | func (s *state) condBranch(cond *Node, yes, no *ssa.Block, likely int8) { |
| 2189 | if cond.Op == OANDAND { |
| 2190 | mid := s.f.NewBlock(ssa.BlockPlain) |
| 2191 | s.stmtList(cond.Ninit) |
| 2192 | s.condBranch(cond.Left, mid, no, max8(likely, 0)) |
| 2193 | s.startBlock(mid) |
| 2194 | s.condBranch(cond.Right, yes, no, likely) |
| 2195 | return |
| 2196 | // Note: if likely==1, then both recursive calls pass 1. |
| 2197 | // If likely==-1, then we don't have enough information to decide |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 2198 | // whether the first branch is likely or not. So we pass 0 for |
Keith Randall | 9918731 | 2015-11-02 16:56:53 -0800 | [diff] [blame] | 2199 | // the likeliness of the first branch. |
| 2200 | // TODO: have the frontend give us branch prediction hints for |
| 2201 | // OANDAND and OOROR nodes (if it ever has such info). |
| 2202 | } |
| 2203 | if cond.Op == OOROR { |
| 2204 | mid := s.f.NewBlock(ssa.BlockPlain) |
| 2205 | s.stmtList(cond.Ninit) |
| 2206 | s.condBranch(cond.Left, yes, mid, min8(likely, 0)) |
| 2207 | s.startBlock(mid) |
| 2208 | s.condBranch(cond.Right, yes, no, likely) |
| 2209 | return |
| 2210 | // Note: if likely==-1, then both recursive calls pass -1. |
| 2211 | // If likely==1, then we don't have enough info to decide |
| 2212 | // the likelihood of the first branch. |
| 2213 | } |
Keith Randall | d19bfc3 | 2015-11-03 09:30:17 -0800 | [diff] [blame] | 2214 | if cond.Op == ONOT { |
| 2215 | s.stmtList(cond.Ninit) |
| 2216 | s.condBranch(cond.Left, no, yes, -likely) |
| 2217 | return |
| 2218 | } |
Keith Randall | 9918731 | 2015-11-02 16:56:53 -0800 | [diff] [blame] | 2219 | c := s.expr(cond) |
| 2220 | b := s.endBlock() |
| 2221 | b.Kind = ssa.BlockIf |
Keith Randall | 56e0ecc | 2016-03-15 20:45:50 -0700 | [diff] [blame] | 2222 | b.SetControl(c) |
Keith Randall | 9918731 | 2015-11-02 16:56:53 -0800 | [diff] [blame] | 2223 | b.Likely = ssa.BranchPrediction(likely) // gc and ssa both use -1/0/+1 for likeliness |
| 2224 | b.AddEdgeTo(yes) |
| 2225 | b.AddEdgeTo(no) |
| 2226 | } |
| 2227 | |
Keith Randall | d4663e1 | 2016-03-21 10:22:03 -0700 | [diff] [blame] | 2228 | type skipMask uint8 |
| 2229 | |
| 2230 | const ( |
| 2231 | skipPtr skipMask = 1 << iota |
| 2232 | skipLen |
| 2233 | skipCap |
| 2234 | ) |
| 2235 | |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 2236 | // assign does left = right. |
| 2237 | // Right has already been evaluated to ssa, left has not. |
| 2238 | // If deref is true, then we do left = *right instead (and right has already been nil-checked). |
| 2239 | // If deref is true and right == nil, just do left = 0. |
| 2240 | // Include a write barrier if wb is true. |
Keith Randall | d4663e1 | 2016-03-21 10:22:03 -0700 | [diff] [blame] | 2241 | // skip indicates assignments (at the top level) that can be avoided. |
| 2242 | func (s *state) assign(left *Node, right *ssa.Value, wb, deref bool, line int32, skip skipMask) { |
Keith Randall | d4cc51d | 2015-08-14 21:47:20 -0700 | [diff] [blame] | 2243 | if left.Op == ONAME && isblank(left) { |
Keith Randall | d4cc51d | 2015-08-14 21:47:20 -0700 | [diff] [blame] | 2244 | return |
| 2245 | } |
Keith Randall | d4cc51d | 2015-08-14 21:47:20 -0700 | [diff] [blame] | 2246 | t := left.Type |
| 2247 | dowidth(t) |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 2248 | if s.canSSA(left) { |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 2249 | if deref { |
| 2250 | s.Fatalf("can SSA LHS %s but not RHS %s", left, right) |
| 2251 | } |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 2252 | if left.Op == ODOT { |
| 2253 | // We're assigning to a field of an ssa-able value. |
| 2254 | // We need to build a new structure with the new value for the |
| 2255 | // field we're assigning and the old values for the other fields. |
| 2256 | // For instance: |
| 2257 | // type T struct {a, b, c int} |
| 2258 | // var T x |
| 2259 | // x.b = 5 |
| 2260 | // For the x.b = 5 assignment we want to generate x = T{x.a, 5, x.c} |
| 2261 | |
| 2262 | // Grab information about the structure type. |
| 2263 | t := left.Left.Type |
| 2264 | nf := t.NumFields() |
| 2265 | idx := fieldIdx(left) |
| 2266 | |
| 2267 | // Grab old value of structure. |
| 2268 | old := s.expr(left.Left) |
| 2269 | |
| 2270 | // Make new structure. |
| 2271 | new := s.newValue0(ssa.StructMakeOp(t.NumFields()), t) |
| 2272 | |
| 2273 | // Add fields as args. |
Matthew Dempsky | 1b9f168 | 2016-03-14 12:45:18 -0700 | [diff] [blame] | 2274 | for i := 0; i < nf; i++ { |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 2275 | if i == idx { |
| 2276 | new.AddArg(right) |
| 2277 | } else { |
Matthew Dempsky | 1b9f168 | 2016-03-14 12:45:18 -0700 | [diff] [blame] | 2278 | new.AddArg(s.newValue1I(ssa.OpStructSelect, t.FieldType(i), int64(i), old)) |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | // Recursively assign the new value we've made to the base of the dot op. |
Keith Randall | d4663e1 | 2016-03-21 10:22:03 -0700 | [diff] [blame] | 2283 | s.assign(left.Left, new, false, false, line, 0) |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 2284 | // TODO: do we need to update named values here? |
| 2285 | return |
| 2286 | } |
Daniel Morsing | c31b6dd | 2015-06-12 14:23:29 +0100 | [diff] [blame] | 2287 | // Update variable assignment. |
Josh Bleecher Snyder | 0726931 | 2015-08-29 14:54:45 -0700 | [diff] [blame] | 2288 | s.vars[left] = right |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 2289 | s.addNamedValue(left, right) |
Daniel Morsing | c31b6dd | 2015-06-12 14:23:29 +0100 | [diff] [blame] | 2290 | return |
| 2291 | } |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 2292 | // Left is not ssa-able. Compute its address. |
David Chase | 57670ad | 2015-10-09 16:48:30 -0400 | [diff] [blame] | 2293 | addr := s.addr(left, false) |
Keith Randall | d2107fc | 2015-08-24 02:16:19 -0700 | [diff] [blame] | 2294 | if left.Op == ONAME { |
Keith Randall | b32217a | 2015-09-17 16:45:10 -0700 | [diff] [blame] | 2295 | s.vars[&memVar] = s.newValue1A(ssa.OpVarDef, ssa.TypeMem, left, s.mem()) |
Keith Randall | d2107fc | 2015-08-24 02:16:19 -0700 | [diff] [blame] | 2296 | } |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 2297 | if deref { |
| 2298 | // Treat as a mem->mem move. |
| 2299 | if right == nil { |
| 2300 | s.vars[&memVar] = s.newValue2I(ssa.OpZero, ssa.TypeMem, t.Size(), addr, s.mem()) |
| 2301 | return |
| 2302 | } |
| 2303 | if wb { |
| 2304 | s.insertWBmove(t, addr, right, line) |
| 2305 | return |
| 2306 | } |
| 2307 | s.vars[&memVar] = s.newValue3I(ssa.OpMove, ssa.TypeMem, t.Size(), addr, right, s.mem()) |
| 2308 | return |
Keith Randall | e3869a6 | 2015-09-07 23:18:02 -0700 | [diff] [blame] | 2309 | } |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 2310 | // Treat as a store. |
| 2311 | if wb { |
Keith Randall | d4663e1 | 2016-03-21 10:22:03 -0700 | [diff] [blame] | 2312 | if skip&skipPtr != 0 { |
| 2313 | // Special case: if we don't write back the pointers, don't bother |
| 2314 | // doing the write barrier check. |
| 2315 | s.storeTypeScalars(t, addr, right, skip) |
| 2316 | return |
| 2317 | } |
| 2318 | s.insertWBstore(t, addr, right, line, skip) |
| 2319 | return |
| 2320 | } |
| 2321 | if skip != 0 { |
| 2322 | if skip&skipPtr == 0 { |
| 2323 | s.storeTypePtrs(t, addr, right) |
| 2324 | } |
| 2325 | s.storeTypeScalars(t, addr, right, skip) |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 2326 | return |
| 2327 | } |
| 2328 | s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, t.Size(), addr, right, s.mem()) |
Daniel Morsing | c31b6dd | 2015-06-12 14:23:29 +0100 | [diff] [blame] | 2329 | } |
| 2330 | |
Josh Bleecher Snyder | 21bd483 | 2015-07-20 15:30:52 -0700 | [diff] [blame] | 2331 | // zeroVal returns the zero value for type t. |
| 2332 | func (s *state) zeroVal(t *Type) *ssa.Value { |
| 2333 | switch { |
Keith Randall | 9cb332e | 2015-07-28 14:19:20 -0700 | [diff] [blame] | 2334 | case t.IsInteger(): |
| 2335 | switch t.Size() { |
| 2336 | case 1: |
| 2337 | return s.constInt8(t, 0) |
| 2338 | case 2: |
| 2339 | return s.constInt16(t, 0) |
| 2340 | case 4: |
| 2341 | return s.constInt32(t, 0) |
| 2342 | case 8: |
| 2343 | return s.constInt64(t, 0) |
| 2344 | default: |
| 2345 | s.Fatalf("bad sized integer type %s", t) |
| 2346 | } |
Todd Neal | 752fe4d | 2015-08-25 19:21:45 -0500 | [diff] [blame] | 2347 | case t.IsFloat(): |
| 2348 | switch t.Size() { |
| 2349 | case 4: |
| 2350 | return s.constFloat32(t, 0) |
| 2351 | case 8: |
| 2352 | return s.constFloat64(t, 0) |
| 2353 | default: |
| 2354 | s.Fatalf("bad sized float type %s", t) |
| 2355 | } |
David Chase | 5257858 | 2015-08-28 14:24:10 -0400 | [diff] [blame] | 2356 | case t.IsComplex(): |
| 2357 | switch t.Size() { |
| 2358 | case 8: |
| 2359 | z := s.constFloat32(Types[TFLOAT32], 0) |
Keith Randall | a5cffb6 | 2015-08-28 13:52:26 -0700 | [diff] [blame] | 2360 | return s.entryNewValue2(ssa.OpComplexMake, t, z, z) |
David Chase | 5257858 | 2015-08-28 14:24:10 -0400 | [diff] [blame] | 2361 | case 16: |
| 2362 | z := s.constFloat64(Types[TFLOAT64], 0) |
Keith Randall | a5cffb6 | 2015-08-28 13:52:26 -0700 | [diff] [blame] | 2363 | return s.entryNewValue2(ssa.OpComplexMake, t, z, z) |
David Chase | 5257858 | 2015-08-28 14:24:10 -0400 | [diff] [blame] | 2364 | default: |
| 2365 | s.Fatalf("bad sized complex type %s", t) |
| 2366 | } |
| 2367 | |
Josh Bleecher Snyder | 21bd483 | 2015-07-20 15:30:52 -0700 | [diff] [blame] | 2368 | case t.IsString(): |
Josh Bleecher Snyder | 3921427 | 2016-03-06 18:06:09 -0800 | [diff] [blame] | 2369 | return s.constEmptyString(t) |
Matthew Dempsky | 788f112 | 2016-03-28 10:55:44 -0700 | [diff] [blame] | 2370 | case t.IsPtrShaped(): |
Josh Bleecher Snyder | 3921427 | 2016-03-06 18:06:09 -0800 | [diff] [blame] | 2371 | return s.constNil(t) |
Josh Bleecher Snyder | 21bd483 | 2015-07-20 15:30:52 -0700 | [diff] [blame] | 2372 | case t.IsBoolean(): |
Josh Bleecher Snyder | cea4414 | 2015-09-08 16:52:25 -0700 | [diff] [blame] | 2373 | return s.constBool(false) |
Keith Randall | 9f954db | 2015-08-18 10:26:28 -0700 | [diff] [blame] | 2374 | case t.IsInterface(): |
Josh Bleecher Snyder | 3921427 | 2016-03-06 18:06:09 -0800 | [diff] [blame] | 2375 | return s.constInterface(t) |
Keith Randall | 9f954db | 2015-08-18 10:26:28 -0700 | [diff] [blame] | 2376 | case t.IsSlice(): |
Josh Bleecher Snyder | 3921427 | 2016-03-06 18:06:09 -0800 | [diff] [blame] | 2377 | return s.constSlice(t) |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 2378 | case t.IsStruct(): |
| 2379 | n := t.NumFields() |
| 2380 | v := s.entryNewValue0(ssa.StructMakeOp(t.NumFields()), t) |
Matthew Dempsky | 1b9f168 | 2016-03-14 12:45:18 -0700 | [diff] [blame] | 2381 | for i := 0; i < n; i++ { |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 2382 | v.AddArg(s.zeroVal(t.FieldType(i).(*Type))) |
| 2383 | } |
| 2384 | return v |
Josh Bleecher Snyder | 21bd483 | 2015-07-20 15:30:52 -0700 | [diff] [blame] | 2385 | } |
| 2386 | s.Unimplementedf("zero for type %v not implemented", t) |
| 2387 | return nil |
| 2388 | } |
| 2389 | |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2390 | type callKind int8 |
| 2391 | |
| 2392 | const ( |
| 2393 | callNormal callKind = iota |
| 2394 | callDefer |
| 2395 | callGo |
| 2396 | ) |
| 2397 | |
David Chase | 8eec2bb | 2016-03-11 00:10:52 -0500 | [diff] [blame] | 2398 | // isSSAIntrinsic1 returns true if n is a call to a recognized 1-arg intrinsic |
| 2399 | // that can be handled by the SSA backend. |
| 2400 | // SSA uses this, but so does the front end to see if should not |
| 2401 | // inline a function because it is a candidate for intrinsic |
| 2402 | // substitution. |
| 2403 | func isSSAIntrinsic1(s *Sym) bool { |
| 2404 | // The test below is not quite accurate -- in the event that |
| 2405 | // a function is disabled on a per-function basis, for example |
| 2406 | // because of hash-keyed binary failure search, SSA might be |
| 2407 | // disabled for that function but it would not be noted here, |
| 2408 | // and thus an inlining would not occur (in practice, inlining |
| 2409 | // so far has only been noticed for Bswap32 and the 16-bit count |
| 2410 | // leading/trailing instructions, but heuristics might change |
| 2411 | // in the future or on different architectures). |
| 2412 | if !ssaEnabled || ssa.IntrinsicsDisable || Thearch.Thechar != '6' { |
| 2413 | return false |
| 2414 | } |
| 2415 | if s != nil && s.Pkg != nil && s.Pkg.Path == "runtime/internal/sys" { |
| 2416 | switch s.Name { |
| 2417 | case |
| 2418 | "Ctz64", "Ctz32", "Ctz16", |
| 2419 | "Bswap64", "Bswap32": |
| 2420 | return true |
| 2421 | } |
| 2422 | } |
| 2423 | return false |
| 2424 | } |
| 2425 | |
| 2426 | func isIntrinsicCall1(n *Node) bool { |
| 2427 | if n == nil || n.Left == nil { |
| 2428 | return false |
| 2429 | } |
| 2430 | return isSSAIntrinsic1(n.Left.Sym) |
| 2431 | } |
| 2432 | |
| 2433 | // intrinsicFirstArg extracts arg from n.List and eval |
| 2434 | func (s *state) intrinsicFirstArg(n *Node) *ssa.Value { |
| 2435 | x := n.List.First() |
| 2436 | if x.Op == OAS { |
| 2437 | x = x.Right |
| 2438 | } |
| 2439 | return s.expr(x) |
| 2440 | } |
| 2441 | |
| 2442 | // intrinsicCall1 converts a call to a recognized 1-arg intrinsic |
| 2443 | // into the intrinsic |
| 2444 | func (s *state) intrinsicCall1(n *Node) *ssa.Value { |
| 2445 | var result *ssa.Value |
| 2446 | switch n.Left.Sym.Name { |
| 2447 | case "Ctz64": |
| 2448 | result = s.newValue1(ssa.OpCtz64, Types[TUINT64], s.intrinsicFirstArg(n)) |
| 2449 | case "Ctz32": |
| 2450 | result = s.newValue1(ssa.OpCtz32, Types[TUINT32], s.intrinsicFirstArg(n)) |
| 2451 | case "Ctz16": |
| 2452 | result = s.newValue1(ssa.OpCtz16, Types[TUINT16], s.intrinsicFirstArg(n)) |
| 2453 | case "Bswap64": |
| 2454 | result = s.newValue1(ssa.OpBswap64, Types[TUINT64], s.intrinsicFirstArg(n)) |
| 2455 | case "Bswap32": |
| 2456 | result = s.newValue1(ssa.OpBswap32, Types[TUINT32], s.intrinsicFirstArg(n)) |
| 2457 | } |
| 2458 | if result == nil { |
| 2459 | Fatalf("Unknown special call: %v", n.Left.Sym) |
| 2460 | } |
| 2461 | if ssa.IntrinsicsDebug > 0 { |
| 2462 | Warnl(n.Lineno, "intrinsic substitution for %v with %s", n.Left.Sym.Name, result.LongString()) |
| 2463 | } |
| 2464 | return result |
| 2465 | } |
| 2466 | |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 2467 | // Calls the function n using the specified call type. |
| 2468 | // Returns the address of the return value (or nil if none). |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2469 | func (s *state) call(n *Node, k callKind) *ssa.Value { |
| 2470 | var sym *Sym // target symbol (if static) |
| 2471 | var closure *ssa.Value // ptr to closure to run (if dynamic) |
| 2472 | var codeptr *ssa.Value // ptr to target code (if dynamic) |
| 2473 | var rcvr *ssa.Value // receiver to set |
| 2474 | fn := n.Left |
| 2475 | switch n.Op { |
| 2476 | case OCALLFUNC: |
| 2477 | if k == callNormal && fn.Op == ONAME && fn.Class == PFUNC { |
| 2478 | sym = fn.Sym |
| 2479 | break |
| 2480 | } |
| 2481 | closure = s.expr(fn) |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2482 | case OCALLMETH: |
| 2483 | if fn.Op != ODOTMETH { |
| 2484 | Fatalf("OCALLMETH: n.Left not an ODOTMETH: %v", fn) |
| 2485 | } |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2486 | if k == callNormal { |
Ian Lance Taylor | 5f525ca | 2016-03-18 16:52:30 -0700 | [diff] [blame] | 2487 | sym = fn.Sym |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2488 | break |
| 2489 | } |
Ian Lance Taylor | 5f525ca | 2016-03-18 16:52:30 -0700 | [diff] [blame] | 2490 | n2 := newname(fn.Sym) |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2491 | n2.Class = PFUNC |
Ian Lance Taylor | 5f525ca | 2016-03-18 16:52:30 -0700 | [diff] [blame] | 2492 | n2.Lineno = fn.Lineno |
| 2493 | closure = s.expr(n2) |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2494 | // Note: receiver is already assigned in n.List, so we don't |
| 2495 | // want to set it here. |
| 2496 | case OCALLINTER: |
| 2497 | if fn.Op != ODOTINTER { |
Matthew Dempsky | c3dfad5 | 2016-03-07 08:23:55 -0800 | [diff] [blame] | 2498 | Fatalf("OCALLINTER: n.Left not an ODOTINTER: %v", Oconv(fn.Op, 0)) |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2499 | } |
| 2500 | i := s.expr(fn.Left) |
| 2501 | itab := s.newValue1(ssa.OpITab, Types[TUINTPTR], i) |
| 2502 | itabidx := fn.Xoffset + 3*int64(Widthptr) + 8 // offset of fun field in runtime.itab |
| 2503 | itab = s.newValue1I(ssa.OpOffPtr, Types[TUINTPTR], itabidx, itab) |
| 2504 | if k == callNormal { |
| 2505 | codeptr = s.newValue2(ssa.OpLoad, Types[TUINTPTR], itab, s.mem()) |
| 2506 | } else { |
| 2507 | closure = itab |
| 2508 | } |
| 2509 | rcvr = s.newValue1(ssa.OpIData, Types[TUINTPTR], i) |
| 2510 | } |
| 2511 | dowidth(fn.Type) |
Josh Bleecher Snyder | 361b334 | 2016-03-28 14:31:57 -0700 | [diff] [blame] | 2512 | stksize := fn.Type.ArgWidth() // includes receiver |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2513 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 2514 | // Run all argument assignments. The arg slots have already |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2515 | // been offset by the appropriate amount (+2*widthptr for go/defer, |
| 2516 | // +widthptr for interface calls). |
| 2517 | // For OCALLMETH, the receiver is set in these statements. |
| 2518 | s.stmtList(n.List) |
| 2519 | |
| 2520 | // Set receiver (for interface calls) |
| 2521 | if rcvr != nil { |
Keith Randall | 7c4fbb6 | 2015-10-19 13:56:55 -0700 | [diff] [blame] | 2522 | argStart := Ctxt.FixedFrameSize() |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2523 | if k != callNormal { |
| 2524 | argStart += int64(2 * Widthptr) |
| 2525 | } |
| 2526 | addr := s.entryNewValue1I(ssa.OpOffPtr, Types[TUINTPTR], argStart, s.sp) |
Keith Randall | b32217a | 2015-09-17 16:45:10 -0700 | [diff] [blame] | 2527 | s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, int64(Widthptr), addr, rcvr, s.mem()) |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | // Defer/go args |
| 2531 | if k != callNormal { |
| 2532 | // Write argsize and closure (args to Newproc/Deferproc). |
| 2533 | argsize := s.constInt32(Types[TUINT32], int32(stksize)) |
Keith Randall | b32217a | 2015-09-17 16:45:10 -0700 | [diff] [blame] | 2534 | s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, 4, s.sp, argsize, s.mem()) |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2535 | addr := s.entryNewValue1I(ssa.OpOffPtr, Ptrto(Types[TUINTPTR]), int64(Widthptr), s.sp) |
Keith Randall | b32217a | 2015-09-17 16:45:10 -0700 | [diff] [blame] | 2536 | s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, int64(Widthptr), addr, closure, s.mem()) |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2537 | stksize += 2 * int64(Widthptr) |
| 2538 | } |
| 2539 | |
| 2540 | // call target |
| 2541 | bNext := s.f.NewBlock(ssa.BlockPlain) |
| 2542 | var call *ssa.Value |
| 2543 | switch { |
| 2544 | case k == callDefer: |
| 2545 | call = s.newValue1(ssa.OpDeferCall, ssa.TypeMem, s.mem()) |
| 2546 | case k == callGo: |
| 2547 | call = s.newValue1(ssa.OpGoCall, ssa.TypeMem, s.mem()) |
| 2548 | case closure != nil: |
| 2549 | codeptr = s.newValue2(ssa.OpLoad, Types[TUINTPTR], closure, s.mem()) |
| 2550 | call = s.newValue3(ssa.OpClosureCall, ssa.TypeMem, codeptr, closure, s.mem()) |
| 2551 | case codeptr != nil: |
| 2552 | call = s.newValue2(ssa.OpInterCall, ssa.TypeMem, codeptr, s.mem()) |
| 2553 | case sym != nil: |
| 2554 | call = s.newValue1A(ssa.OpStaticCall, ssa.TypeMem, sym, s.mem()) |
| 2555 | default: |
| 2556 | Fatalf("bad call type %s %v", opnames[n.Op], n) |
| 2557 | } |
| 2558 | call.AuxInt = stksize // Call operations carry the argsize of the callee along with them |
| 2559 | |
| 2560 | // Finish call block |
Keith Randall | b32217a | 2015-09-17 16:45:10 -0700 | [diff] [blame] | 2561 | s.vars[&memVar] = call |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2562 | b := s.endBlock() |
| 2563 | b.Kind = ssa.BlockCall |
Keith Randall | 56e0ecc | 2016-03-15 20:45:50 -0700 | [diff] [blame] | 2564 | b.SetControl(call) |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2565 | b.AddEdgeTo(bNext) |
Keith Randall | ddc6b64 | 2016-03-09 19:27:57 -0800 | [diff] [blame] | 2566 | if k == callDefer { |
| 2567 | // Add recover edge to exit code. |
| 2568 | b.Kind = ssa.BlockDefer |
| 2569 | r := s.f.NewBlock(ssa.BlockPlain) |
| 2570 | s.startBlock(r) |
| 2571 | s.exit() |
| 2572 | b.AddEdgeTo(r) |
| 2573 | b.Likely = ssa.BranchLikely |
| 2574 | } |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2575 | |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 2576 | // Start exit block, find address of result. |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2577 | s.startBlock(bNext) |
Matthew Dempsky | f6fab93 | 2016-03-15 11:06:03 -0700 | [diff] [blame] | 2578 | res := n.Left.Type.Results() |
| 2579 | if res.NumFields() == 0 || k != callNormal { |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2580 | // call has no return value. Continue with the next statement. |
| 2581 | return nil |
| 2582 | } |
Matthew Dempsky | f6fab93 | 2016-03-15 11:06:03 -0700 | [diff] [blame] | 2583 | fp := res.Field(0) |
Matthew Dempsky | 62dddd4 | 2016-03-28 09:40:53 -0700 | [diff] [blame] | 2584 | return s.entryNewValue1I(ssa.OpOffPtr, Ptrto(fp.Type), fp.Offset, s.sp) |
Keith Randall | d24768e | 2015-09-09 23:56:59 -0700 | [diff] [blame] | 2585 | } |
| 2586 | |
Josh Bleecher Snyder | 95aff4d | 2015-07-28 14:31:25 -0700 | [diff] [blame] | 2587 | // etypesign returns the signed-ness of e, for integer/pointer etypes. |
| 2588 | // -1 means signed, +1 means unsigned, 0 means non-integer/non-pointer. |
Keith Randall | 4304fbc | 2015-11-16 13:20:16 -0800 | [diff] [blame] | 2589 | func etypesign(e EType) int8 { |
Josh Bleecher Snyder | 95aff4d | 2015-07-28 14:31:25 -0700 | [diff] [blame] | 2590 | switch e { |
| 2591 | case TINT8, TINT16, TINT32, TINT64, TINT: |
| 2592 | return -1 |
| 2593 | case TUINT8, TUINT16, TUINT32, TUINT64, TUINT, TUINTPTR, TUNSAFEPTR: |
| 2594 | return +1 |
| 2595 | } |
| 2596 | return 0 |
| 2597 | } |
| 2598 | |
Todd Neal | d076ef7 | 2015-10-15 20:25:32 -0500 | [diff] [blame] | 2599 | // lookupSymbol is used to retrieve the symbol (Extern, Arg or Auto) used for a particular node. |
| 2600 | // This improves the effectiveness of cse by using the same Aux values for the |
| 2601 | // same symbols. |
| 2602 | func (s *state) lookupSymbol(n *Node, sym interface{}) interface{} { |
| 2603 | switch sym.(type) { |
| 2604 | default: |
| 2605 | s.Fatalf("sym %v is of uknown type %T", sym, sym) |
| 2606 | case *ssa.ExternSymbol, *ssa.ArgSymbol, *ssa.AutoSymbol: |
| 2607 | // these are the only valid types |
| 2608 | } |
| 2609 | |
| 2610 | if lsym, ok := s.varsyms[n]; ok { |
| 2611 | return lsym |
| 2612 | } else { |
| 2613 | s.varsyms[n] = sym |
| 2614 | return sym |
| 2615 | } |
| 2616 | } |
| 2617 | |
Josh Bleecher Snyder | e00d609 | 2015-06-02 09:16:22 -0700 | [diff] [blame] | 2618 | // addr converts the address of the expression n to SSA, adds it to s and returns the SSA result. |
Keith Randall | c3c84a2 | 2015-07-13 15:55:37 -0700 | [diff] [blame] | 2619 | // The value that the returned Value represents is guaranteed to be non-nil. |
David Chase | 57670ad | 2015-10-09 16:48:30 -0400 | [diff] [blame] | 2620 | // If bounded is true then this address does not require a nil check for its operand |
| 2621 | // even if that would otherwise be implied. |
| 2622 | func (s *state) addr(n *Node, bounded bool) *ssa.Value { |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 2623 | t := Ptrto(n.Type) |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2624 | switch n.Op { |
| 2625 | case ONAME: |
Keith Randall | 290d8fc | 2015-06-10 15:03:06 -0700 | [diff] [blame] | 2626 | switch n.Class { |
| 2627 | case PEXTERN: |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2628 | // global variable |
Todd Neal | 74180dd | 2015-10-27 21:35:48 -0500 | [diff] [blame] | 2629 | aux := s.lookupSymbol(n, &ssa.ExternSymbol{n.Type, n.Sym}) |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 2630 | v := s.entryNewValue1A(ssa.OpAddr, t, aux, s.sb) |
Josh Bleecher Snyder | 67df793 | 2015-07-28 11:08:44 -0700 | [diff] [blame] | 2631 | // TODO: Make OpAddr use AuxInt as well as Aux. |
| 2632 | if n.Xoffset != 0 { |
| 2633 | v = s.entryNewValue1I(ssa.OpOffPtr, v.Type, n.Xoffset, v) |
| 2634 | } |
| 2635 | return v |
David Chase | 956f319 | 2015-09-11 16:40:05 -0400 | [diff] [blame] | 2636 | case PPARAM: |
| 2637 | // parameter slot |
Josh Bleecher Snyder | 596ddf4 | 2015-06-29 11:56:28 -0700 | [diff] [blame] | 2638 | v := s.decladdrs[n] |
Keith Randall | 4304fbc | 2015-11-16 13:20:16 -0800 | [diff] [blame] | 2639 | if v != nil { |
| 2640 | return v |
Josh Bleecher Snyder | 596ddf4 | 2015-06-29 11:56:28 -0700 | [diff] [blame] | 2641 | } |
Keith Randall | 4304fbc | 2015-11-16 13:20:16 -0800 | [diff] [blame] | 2642 | if n.String() == ".fp" { |
| 2643 | // Special arg that points to the frame pointer. |
| 2644 | // (Used by the race detector, others?) |
| 2645 | aux := s.lookupSymbol(n, &ssa.ArgSymbol{Typ: n.Type, Node: n}) |
| 2646 | return s.entryNewValue1A(ssa.OpAddr, t, aux, s.sp) |
| 2647 | } |
| 2648 | s.Fatalf("addr of undeclared ONAME %v. declared: %v", n, s.decladdrs) |
| 2649 | return nil |
Keith Randall | d2107fc | 2015-08-24 02:16:19 -0700 | [diff] [blame] | 2650 | case PAUTO: |
Todd Neal | 40bfec0 | 2016-03-11 20:03:17 -0600 | [diff] [blame] | 2651 | aux := s.lookupSymbol(n, &ssa.AutoSymbol{Typ: n.Type, Node: n}) |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 2652 | return s.newValue1A(ssa.OpAddr, t, aux, s.sp) |
David Chase | 956f319 | 2015-09-11 16:40:05 -0400 | [diff] [blame] | 2653 | case PPARAMOUT: // Same as PAUTO -- cannot generate LEA early. |
Todd Neal | d076ef7 | 2015-10-15 20:25:32 -0500 | [diff] [blame] | 2654 | // ensure that we reuse symbols for out parameters so |
| 2655 | // that cse works on their addresses |
| 2656 | aux := s.lookupSymbol(n, &ssa.ArgSymbol{Typ: n.Type, Node: n}) |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 2657 | return s.newValue1A(ssa.OpAddr, t, aux, s.sp) |
David Chase | 956f319 | 2015-09-11 16:40:05 -0400 | [diff] [blame] | 2658 | case PAUTO | PHEAP, PPARAM | PHEAP, PPARAMOUT | PHEAP, PPARAMREF: |
Daniel Morsing | c31b6dd | 2015-06-12 14:23:29 +0100 | [diff] [blame] | 2659 | return s.expr(n.Name.Heapaddr) |
Keith Randall | 290d8fc | 2015-06-10 15:03:06 -0700 | [diff] [blame] | 2660 | default: |
Josh Bleecher Snyder | 5844603 | 2015-08-23 20:29:43 -0700 | [diff] [blame] | 2661 | s.Unimplementedf("variable address class %v not implemented", n.Class) |
Keith Randall | 290d8fc | 2015-06-10 15:03:06 -0700 | [diff] [blame] | 2662 | return nil |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2663 | } |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2664 | case OINDREG: |
Josh Bleecher Snyder | 25d1916 | 2015-07-28 12:37:46 -0700 | [diff] [blame] | 2665 | // indirect off a register |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2666 | // used for storing/loading arguments/returns to/from callees |
Josh Bleecher Snyder | 25d1916 | 2015-07-28 12:37:46 -0700 | [diff] [blame] | 2667 | if int(n.Reg) != Thearch.REGSP { |
| 2668 | s.Unimplementedf("OINDREG of non-SP register %s in addr: %v", obj.Rconv(int(n.Reg)), n) |
| 2669 | return nil |
| 2670 | } |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 2671 | return s.entryNewValue1I(ssa.OpOffPtr, t, n.Xoffset, s.sp) |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2672 | case OINDEX: |
Brad Fitzpatrick | 7af53d9 | 2015-07-10 10:47:28 -0600 | [diff] [blame] | 2673 | if n.Left.Type.IsSlice() { |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2674 | a := s.expr(n.Left) |
| 2675 | i := s.expr(n.Right) |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 2676 | i = s.extendIndex(i) |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 2677 | len := s.newValue1(ssa.OpSliceLen, Types[TINT], a) |
Keith Randall | 46e62f8 | 2015-08-18 14:17:30 -0700 | [diff] [blame] | 2678 | if !n.Bounded { |
| 2679 | s.boundsCheck(i, len) |
| 2680 | } |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 2681 | p := s.newValue1(ssa.OpSlicePtr, t, a) |
| 2682 | return s.newValue2(ssa.OpPtrIndex, t, p, i) |
Brad Fitzpatrick | 7af53d9 | 2015-07-10 10:47:28 -0600 | [diff] [blame] | 2683 | } else { // array |
David Chase | 57670ad | 2015-10-09 16:48:30 -0400 | [diff] [blame] | 2684 | a := s.addr(n.Left, bounded) |
Brad Fitzpatrick | 7af53d9 | 2015-07-10 10:47:28 -0600 | [diff] [blame] | 2685 | i := s.expr(n.Right) |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 2686 | i = s.extendIndex(i) |
Josh Bleecher Snyder | 3a0783c | 2016-03-31 14:46:04 -0700 | [diff] [blame] | 2687 | len := s.constInt(Types[TINT], n.Left.Type.NumElem()) |
Keith Randall | 46e62f8 | 2015-08-18 14:17:30 -0700 | [diff] [blame] | 2688 | if !n.Bounded { |
| 2689 | s.boundsCheck(i, len) |
| 2690 | } |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 2691 | return s.newValue2(ssa.OpPtrIndex, Ptrto(n.Left.Type.Elem()), a, i) |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2692 | } |
Todd Neal | b383de2 | 2015-07-13 21:22:16 -0500 | [diff] [blame] | 2693 | case OIND: |
| 2694 | p := s.expr(n.Left) |
David Chase | 57670ad | 2015-10-09 16:48:30 -0400 | [diff] [blame] | 2695 | if !bounded { |
| 2696 | s.nilCheck(p) |
| 2697 | } |
Todd Neal | b383de2 | 2015-07-13 21:22:16 -0500 | [diff] [blame] | 2698 | return p |
Keith Randall | c3c84a2 | 2015-07-13 15:55:37 -0700 | [diff] [blame] | 2699 | case ODOT: |
David Chase | 57670ad | 2015-10-09 16:48:30 -0400 | [diff] [blame] | 2700 | p := s.addr(n.Left, bounded) |
Josh Bleecher Snyder | da1802f | 2016-03-04 12:34:43 -0800 | [diff] [blame] | 2701 | return s.newValue1I(ssa.OpOffPtr, t, n.Xoffset, p) |
Keith Randall | c3c84a2 | 2015-07-13 15:55:37 -0700 | [diff] [blame] | 2702 | case ODOTPTR: |
| 2703 | p := s.expr(n.Left) |
David Chase | 57670ad | 2015-10-09 16:48:30 -0400 | [diff] [blame] | 2704 | if !bounded { |
| 2705 | s.nilCheck(p) |
| 2706 | } |
Josh Bleecher Snyder | da1802f | 2016-03-04 12:34:43 -0800 | [diff] [blame] | 2707 | return s.newValue1I(ssa.OpOffPtr, t, n.Xoffset, p) |
David Chase | 956f319 | 2015-09-11 16:40:05 -0400 | [diff] [blame] | 2708 | case OCLOSUREVAR: |
Josh Bleecher Snyder | da1802f | 2016-03-04 12:34:43 -0800 | [diff] [blame] | 2709 | return s.newValue1I(ssa.OpOffPtr, t, n.Xoffset, |
| 2710 | s.entryNewValue0(ssa.OpGetClosurePtr, Ptrto(Types[TUINT8]))) |
David Chase | 32ffbf7 | 2015-10-08 17:14:12 -0400 | [diff] [blame] | 2711 | case OPARAM: |
| 2712 | p := n.Left |
| 2713 | if p.Op != ONAME || !(p.Class == PPARAM|PHEAP || p.Class == PPARAMOUT|PHEAP) { |
| 2714 | s.Fatalf("OPARAM not of ONAME,{PPARAM,PPARAMOUT}|PHEAP, instead %s", nodedump(p, 0)) |
| 2715 | } |
| 2716 | |
| 2717 | // Recover original offset to address passed-in param value. |
| 2718 | original_p := *p |
| 2719 | original_p.Xoffset = n.Xoffset |
| 2720 | aux := &ssa.ArgSymbol{Typ: n.Type, Node: &original_p} |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 2721 | return s.entryNewValue1A(ssa.OpAddr, t, aux, s.sp) |
David Chase | 57670ad | 2015-10-09 16:48:30 -0400 | [diff] [blame] | 2722 | case OCONVNOP: |
| 2723 | addr := s.addr(n.Left, bounded) |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 2724 | return s.newValue1(ssa.OpCopy, t, addr) // ensure that addr has the right type |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 2725 | case OCALLFUNC, OCALLINTER, OCALLMETH: |
| 2726 | return s.call(n, callNormal) |
David Chase | 57670ad | 2015-10-09 16:48:30 -0400 | [diff] [blame] | 2727 | |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2728 | default: |
Matthew Dempsky | c3dfad5 | 2016-03-07 08:23:55 -0800 | [diff] [blame] | 2729 | s.Unimplementedf("unhandled addr %v", Oconv(n.Op, 0)) |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2730 | return nil |
| 2731 | } |
| 2732 | } |
| 2733 | |
Keith Randall | 290d8fc | 2015-06-10 15:03:06 -0700 | [diff] [blame] | 2734 | // canSSA reports whether n is SSA-able. |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 2735 | // n must be an ONAME (or an ODOT sequence with an ONAME base). |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 2736 | func (s *state) canSSA(n *Node) bool { |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 2737 | for n.Op == ODOT { |
| 2738 | n = n.Left |
| 2739 | } |
Keith Randall | 290d8fc | 2015-06-10 15:03:06 -0700 | [diff] [blame] | 2740 | if n.Op != ONAME { |
Daniel Morsing | 66b4781 | 2015-06-27 15:45:20 +0100 | [diff] [blame] | 2741 | return false |
Keith Randall | 290d8fc | 2015-06-10 15:03:06 -0700 | [diff] [blame] | 2742 | } |
| 2743 | if n.Addrtaken { |
| 2744 | return false |
| 2745 | } |
| 2746 | if n.Class&PHEAP != 0 { |
| 2747 | return false |
| 2748 | } |
Josh Bleecher Snyder | 9654873 | 2015-08-28 13:35:32 -0700 | [diff] [blame] | 2749 | switch n.Class { |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 2750 | case PEXTERN, PPARAMREF: |
| 2751 | // TODO: maybe treat PPARAMREF with an Arg-like op to read from closure? |
Keith Randall | 290d8fc | 2015-06-10 15:03:06 -0700 | [diff] [blame] | 2752 | return false |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 2753 | case PPARAMOUT: |
| 2754 | if hasdefer { |
| 2755 | // TODO: handle this case? Named return values must be |
| 2756 | // in memory so that the deferred function can see them. |
| 2757 | // Maybe do: if !strings.HasPrefix(n.String(), "~") { return false } |
| 2758 | return false |
| 2759 | } |
| 2760 | if s.cgoUnsafeArgs { |
| 2761 | // Cgo effectively takes the address of all result args, |
| 2762 | // but the compiler can't see that. |
| 2763 | return false |
| 2764 | } |
Keith Randall | 290d8fc | 2015-06-10 15:03:06 -0700 | [diff] [blame] | 2765 | } |
Keith Randall | 8a1f621 | 2015-09-08 21:28:44 -0700 | [diff] [blame] | 2766 | if n.Class == PPARAM && n.String() == ".this" { |
| 2767 | // wrappers generated by genwrapper need to update |
| 2768 | // the .this pointer in place. |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 2769 | // TODO: treat as a PPARMOUT? |
Keith Randall | 8a1f621 | 2015-09-08 21:28:44 -0700 | [diff] [blame] | 2770 | return false |
| 2771 | } |
Keith Randall | 9f954db | 2015-08-18 10:26:28 -0700 | [diff] [blame] | 2772 | return canSSAType(n.Type) |
| 2773 | // TODO: try to make more variables SSAable? |
| 2774 | } |
| 2775 | |
| 2776 | // canSSA reports whether variables of type t are SSA-able. |
| 2777 | func canSSAType(t *Type) bool { |
| 2778 | dowidth(t) |
| 2779 | if t.Width > int64(4*Widthptr) { |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 2780 | // 4*Widthptr is an arbitrary constant. We want it |
Keith Randall | 9f954db | 2015-08-18 10:26:28 -0700 | [diff] [blame] | 2781 | // to be at least 3*Widthptr so slices can be registerized. |
| 2782 | // Too big and we'll introduce too much register pressure. |
Daniel Morsing | 66b4781 | 2015-06-27 15:45:20 +0100 | [diff] [blame] | 2783 | return false |
| 2784 | } |
Keith Randall | 9f954db | 2015-08-18 10:26:28 -0700 | [diff] [blame] | 2785 | switch t.Etype { |
| 2786 | case TARRAY: |
Matthew Dempsky | 1624a9c | 2016-03-30 14:45:47 -0700 | [diff] [blame] | 2787 | if t.IsSlice() { |
Keith Randall | 9f954db | 2015-08-18 10:26:28 -0700 | [diff] [blame] | 2788 | return true |
| 2789 | } |
| 2790 | // We can't do arrays because dynamic indexing is |
| 2791 | // not supported on SSA variables. |
| 2792 | // TODO: maybe allow if length is <=1? All indexes |
| 2793 | // are constant? Might be good for the arrays |
| 2794 | // introduced by the compiler for variadic functions. |
| 2795 | return false |
| 2796 | case TSTRUCT: |
Matthew Dempsky | dbed1c6 | 2016-03-17 13:26:08 -0700 | [diff] [blame] | 2797 | if t.NumFields() > ssa.MaxStruct { |
Keith Randall | 9f954db | 2015-08-18 10:26:28 -0700 | [diff] [blame] | 2798 | return false |
| 2799 | } |
Matthew Dempsky | f6bca3f | 2016-03-17 01:32:18 -0700 | [diff] [blame] | 2800 | for _, t1 := range t.Fields().Slice() { |
Keith Randall | 9f954db | 2015-08-18 10:26:28 -0700 | [diff] [blame] | 2801 | if !canSSAType(t1.Type) { |
| 2802 | return false |
| 2803 | } |
| 2804 | } |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 2805 | return true |
Keith Randall | 9f954db | 2015-08-18 10:26:28 -0700 | [diff] [blame] | 2806 | default: |
| 2807 | return true |
| 2808 | } |
Keith Randall | 290d8fc | 2015-06-10 15:03:06 -0700 | [diff] [blame] | 2809 | } |
| 2810 | |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2811 | // nilCheck generates nil pointer checking code. |
Josh Bleecher Snyder | 463858e | 2015-08-11 09:47:45 -0700 | [diff] [blame] | 2812 | // Starts a new block on return, unless nil checks are disabled. |
Josh Bleecher Snyder | 7e74e43 | 2015-07-24 11:55:52 -0700 | [diff] [blame] | 2813 | // Used only for automatically inserted nil checks, |
| 2814 | // not for user code like 'x != nil'. |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2815 | func (s *state) nilCheck(ptr *ssa.Value) { |
Josh Bleecher Snyder | 463858e | 2015-08-11 09:47:45 -0700 | [diff] [blame] | 2816 | if Disable_checknil != 0 { |
| 2817 | return |
| 2818 | } |
Keith Randall | 31115a5 | 2015-10-23 19:12:49 -0700 | [diff] [blame] | 2819 | chk := s.newValue2(ssa.OpNilCheck, ssa.TypeVoid, ptr, s.mem()) |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2820 | b := s.endBlock() |
Keith Randall | 31115a5 | 2015-10-23 19:12:49 -0700 | [diff] [blame] | 2821 | b.Kind = ssa.BlockCheck |
Keith Randall | 56e0ecc | 2016-03-15 20:45:50 -0700 | [diff] [blame] | 2822 | b.SetControl(chk) |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2823 | bNext := s.f.NewBlock(ssa.BlockPlain) |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 2824 | b.AddEdgeTo(bNext) |
Josh Bleecher Snyder | 463858e | 2015-08-11 09:47:45 -0700 | [diff] [blame] | 2825 | s.startBlock(bNext) |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2826 | } |
| 2827 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 2828 | // boundsCheck generates bounds checking code. Checks if 0 <= idx < len, branches to exit if not. |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2829 | // Starts a new block on return. |
| 2830 | func (s *state) boundsCheck(idx, len *ssa.Value) { |
Keith Randall | 8d23681 | 2015-08-18 15:25:40 -0700 | [diff] [blame] | 2831 | if Debug['B'] != 0 { |
| 2832 | return |
| 2833 | } |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2834 | // TODO: convert index to full width? |
| 2835 | // TODO: if index is 64-bit and we're compiling to 32-bit, check that high 32 bits are zero. |
| 2836 | |
| 2837 | // bounds check |
Josh Bleecher Snyder | 85e0329 | 2015-07-30 11:03:05 -0700 | [diff] [blame] | 2838 | cmp := s.newValue2(ssa.OpIsInBounds, Types[TBOOL], idx, len) |
Keith Randall | 3a70bf9 | 2015-09-17 16:54:15 -0700 | [diff] [blame] | 2839 | s.check(cmp, Panicindex) |
Keith Randall | 3526cf5 | 2015-08-24 23:52:03 -0700 | [diff] [blame] | 2840 | } |
| 2841 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 2842 | // sliceBoundsCheck generates slice bounds checking code. Checks if 0 <= idx <= len, branches to exit if not. |
Keith Randall | 3526cf5 | 2015-08-24 23:52:03 -0700 | [diff] [blame] | 2843 | // Starts a new block on return. |
| 2844 | func (s *state) sliceBoundsCheck(idx, len *ssa.Value) { |
| 2845 | if Debug['B'] != 0 { |
| 2846 | return |
| 2847 | } |
| 2848 | // TODO: convert index to full width? |
| 2849 | // TODO: if index is 64-bit and we're compiling to 32-bit, check that high 32 bits are zero. |
| 2850 | |
| 2851 | // bounds check |
| 2852 | cmp := s.newValue2(ssa.OpIsSliceInBounds, Types[TBOOL], idx, len) |
Keith Randall | 3a70bf9 | 2015-09-17 16:54:15 -0700 | [diff] [blame] | 2853 | s.check(cmp, panicslice) |
Keith Randall | 3526cf5 | 2015-08-24 23:52:03 -0700 | [diff] [blame] | 2854 | } |
| 2855 | |
Keith Randall | 3a70bf9 | 2015-09-17 16:54:15 -0700 | [diff] [blame] | 2856 | // If cmp (a bool) is true, panic using the given function. |
| 2857 | func (s *state) check(cmp *ssa.Value, fn *Node) { |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2858 | b := s.endBlock() |
| 2859 | b.Kind = ssa.BlockIf |
Keith Randall | 56e0ecc | 2016-03-15 20:45:50 -0700 | [diff] [blame] | 2860 | b.SetControl(cmp) |
Josh Bleecher Snyder | bbf8c5c | 2015-08-11 17:28:56 -0700 | [diff] [blame] | 2861 | b.Likely = ssa.BranchLikely |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2862 | bNext := s.f.NewBlock(ssa.BlockPlain) |
Keith Randall | 74e568f | 2015-11-09 21:35:40 -0800 | [diff] [blame] | 2863 | line := s.peekLine() |
| 2864 | bPanic := s.panics[funcLine{fn, line}] |
| 2865 | if bPanic == nil { |
| 2866 | bPanic = s.f.NewBlock(ssa.BlockPlain) |
| 2867 | s.panics[funcLine{fn, line}] = bPanic |
| 2868 | s.startBlock(bPanic) |
| 2869 | // The panic call takes/returns memory to ensure that the right |
| 2870 | // memory state is observed if the panic happens. |
| 2871 | s.rtcall(fn, false, nil) |
| 2872 | } |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 2873 | b.AddEdgeTo(bNext) |
| 2874 | b.AddEdgeTo(bPanic) |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 2875 | s.startBlock(bNext) |
| 2876 | } |
| 2877 | |
Keith Randall | 8c5bfcc | 2015-09-18 15:11:30 -0700 | [diff] [blame] | 2878 | // rtcall issues a call to the given runtime function fn with the listed args. |
| 2879 | // Returns a slice of results of the given result types. |
| 2880 | // The call is added to the end of the current block. |
| 2881 | // If returns is false, the block is marked as an exit block. |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 2882 | // If returns is true, the block is marked as a call block. A new block |
Keith Randall | 8c5bfcc | 2015-09-18 15:11:30 -0700 | [diff] [blame] | 2883 | // is started to load the return values. |
| 2884 | func (s *state) rtcall(fn *Node, returns bool, results []*Type, args ...*ssa.Value) []*ssa.Value { |
| 2885 | // Write args to the stack |
| 2886 | var off int64 // TODO: arch-dependent starting offset? |
| 2887 | for _, arg := range args { |
| 2888 | t := arg.Type |
| 2889 | off = Rnd(off, t.Alignment()) |
| 2890 | ptr := s.sp |
| 2891 | if off != 0 { |
| 2892 | ptr = s.newValue1I(ssa.OpOffPtr, Types[TUINTPTR], off, s.sp) |
| 2893 | } |
| 2894 | size := t.Size() |
| 2895 | s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, size, ptr, arg, s.mem()) |
| 2896 | off += size |
| 2897 | } |
| 2898 | off = Rnd(off, int64(Widthptr)) |
| 2899 | |
| 2900 | // Issue call |
| 2901 | call := s.newValue1A(ssa.OpStaticCall, ssa.TypeMem, fn.Sym, s.mem()) |
| 2902 | s.vars[&memVar] = call |
| 2903 | |
| 2904 | // Finish block |
| 2905 | b := s.endBlock() |
| 2906 | if !returns { |
| 2907 | b.Kind = ssa.BlockExit |
Keith Randall | 56e0ecc | 2016-03-15 20:45:50 -0700 | [diff] [blame] | 2908 | b.SetControl(call) |
Keith Randall | 8c5bfcc | 2015-09-18 15:11:30 -0700 | [diff] [blame] | 2909 | call.AuxInt = off |
| 2910 | if len(results) > 0 { |
| 2911 | Fatalf("panic call can't have results") |
| 2912 | } |
| 2913 | return nil |
| 2914 | } |
| 2915 | b.Kind = ssa.BlockCall |
Keith Randall | 56e0ecc | 2016-03-15 20:45:50 -0700 | [diff] [blame] | 2916 | b.SetControl(call) |
Keith Randall | 8c5bfcc | 2015-09-18 15:11:30 -0700 | [diff] [blame] | 2917 | bNext := s.f.NewBlock(ssa.BlockPlain) |
| 2918 | b.AddEdgeTo(bNext) |
| 2919 | s.startBlock(bNext) |
| 2920 | |
| 2921 | // Load results |
| 2922 | res := make([]*ssa.Value, len(results)) |
| 2923 | for i, t := range results { |
| 2924 | off = Rnd(off, t.Alignment()) |
| 2925 | ptr := s.sp |
| 2926 | if off != 0 { |
| 2927 | ptr = s.newValue1I(ssa.OpOffPtr, Types[TUINTPTR], off, s.sp) |
| 2928 | } |
| 2929 | res[i] = s.newValue2(ssa.OpLoad, t, ptr, s.mem()) |
| 2930 | off += t.Size() |
| 2931 | } |
| 2932 | off = Rnd(off, int64(Widthptr)) |
| 2933 | |
| 2934 | // Remember how much callee stack space we needed. |
| 2935 | call.AuxInt = off |
| 2936 | |
| 2937 | return res |
| 2938 | } |
| 2939 | |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 2940 | // insertWBmove inserts the assignment *left = *right including a write barrier. |
| 2941 | // t is the type being assigned. |
| 2942 | func (s *state) insertWBmove(t *Type, left, right *ssa.Value, line int32) { |
Keith Randall | 4304fbc | 2015-11-16 13:20:16 -0800 | [diff] [blame] | 2943 | // if writeBarrier.enabled { |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 2944 | // typedmemmove(&t, left, right) |
| 2945 | // } else { |
| 2946 | // *left = *right |
Keith Randall | 9d22c10 | 2015-09-11 11:02:57 -0700 | [diff] [blame] | 2947 | // } |
Keith Randall | 15ed37d | 2016-03-16 21:51:17 -0700 | [diff] [blame] | 2948 | |
| 2949 | if s.noWB { |
| 2950 | s.Fatalf("write barrier prohibited") |
| 2951 | } |
| 2952 | if s.WBLineno == 0 { |
| 2953 | s.WBLineno = left.Line |
| 2954 | } |
Keith Randall | 9d22c10 | 2015-09-11 11:02:57 -0700 | [diff] [blame] | 2955 | bThen := s.f.NewBlock(ssa.BlockPlain) |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 2956 | bElse := s.f.NewBlock(ssa.BlockPlain) |
| 2957 | bEnd := s.f.NewBlock(ssa.BlockPlain) |
Keith Randall | 9d22c10 | 2015-09-11 11:02:57 -0700 | [diff] [blame] | 2958 | |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 2959 | aux := &ssa.ExternSymbol{Types[TBOOL], syslook("writeBarrier").Sym} |
David Chase | 8107b00 | 2016-02-28 11:15:22 -0500 | [diff] [blame] | 2960 | flagaddr := s.newValue1A(ssa.OpAddr, Ptrto(Types[TUINT32]), aux, s.sb) |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 2961 | // TODO: select the .enabled field. It is currently first, so not needed for now. |
David Chase | 8107b00 | 2016-02-28 11:15:22 -0500 | [diff] [blame] | 2962 | // Load word, test byte, avoiding partial register write from load byte. |
| 2963 | flag := s.newValue2(ssa.OpLoad, Types[TUINT32], flagaddr, s.mem()) |
| 2964 | flag = s.newValue1(ssa.OpTrunc64to8, Types[TBOOL], flag) |
Keith Randall | 9d22c10 | 2015-09-11 11:02:57 -0700 | [diff] [blame] | 2965 | b := s.endBlock() |
| 2966 | b.Kind = ssa.BlockIf |
| 2967 | b.Likely = ssa.BranchUnlikely |
Keith Randall | 56e0ecc | 2016-03-15 20:45:50 -0700 | [diff] [blame] | 2968 | b.SetControl(flag) |
Keith Randall | 9d22c10 | 2015-09-11 11:02:57 -0700 | [diff] [blame] | 2969 | b.AddEdgeTo(bThen) |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 2970 | b.AddEdgeTo(bElse) |
Keith Randall | 9d22c10 | 2015-09-11 11:02:57 -0700 | [diff] [blame] | 2971 | |
| 2972 | s.startBlock(bThen) |
Keith Randall | 9d22c10 | 2015-09-11 11:02:57 -0700 | [diff] [blame] | 2973 | taddr := s.newValue1A(ssa.OpAddr, Types[TUINTPTR], &ssa.ExternSymbol{Types[TUINTPTR], typenamesym(t)}, s.sb) |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 2974 | s.rtcall(typedmemmove, true, nil, taddr, left, right) |
| 2975 | s.endBlock().AddEdgeTo(bEnd) |
| 2976 | |
| 2977 | s.startBlock(bElse) |
| 2978 | s.vars[&memVar] = s.newValue3I(ssa.OpMove, ssa.TypeMem, t.Size(), left, right, s.mem()) |
| 2979 | s.endBlock().AddEdgeTo(bEnd) |
| 2980 | |
| 2981 | s.startBlock(bEnd) |
Keith Randall | 9d22c10 | 2015-09-11 11:02:57 -0700 | [diff] [blame] | 2982 | |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 2983 | if Debug_wb > 0 { |
Robert Griesemer | b83f397 | 2016-03-02 11:01:25 -0800 | [diff] [blame] | 2984 | Warnl(line, "write barrier") |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 2985 | } |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 2986 | } |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 2987 | |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 2988 | // insertWBstore inserts the assignment *left = right including a write barrier. |
| 2989 | // t is the type being assigned. |
Keith Randall | d4663e1 | 2016-03-21 10:22:03 -0700 | [diff] [blame] | 2990 | func (s *state) insertWBstore(t *Type, left, right *ssa.Value, line int32, skip skipMask) { |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 2991 | // store scalar fields |
| 2992 | // if writeBarrier.enabled { |
| 2993 | // writebarrierptr for pointer fields |
| 2994 | // } else { |
| 2995 | // store pointer fields |
| 2996 | // } |
| 2997 | |
Keith Randall | 15ed37d | 2016-03-16 21:51:17 -0700 | [diff] [blame] | 2998 | if s.noWB { |
| 2999 | s.Fatalf("write barrier prohibited") |
| 3000 | } |
| 3001 | if s.WBLineno == 0 { |
| 3002 | s.WBLineno = left.Line |
| 3003 | } |
Keith Randall | d4663e1 | 2016-03-21 10:22:03 -0700 | [diff] [blame] | 3004 | s.storeTypeScalars(t, left, right, skip) |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 3005 | |
| 3006 | bThen := s.f.NewBlock(ssa.BlockPlain) |
| 3007 | bElse := s.f.NewBlock(ssa.BlockPlain) |
| 3008 | bEnd := s.f.NewBlock(ssa.BlockPlain) |
| 3009 | |
Matthew Dempsky | dafbcf6 | 2016-03-04 15:19:06 -0800 | [diff] [blame] | 3010 | aux := &ssa.ExternSymbol{Types[TBOOL], syslook("writeBarrier").Sym} |
David Chase | 8107b00 | 2016-02-28 11:15:22 -0500 | [diff] [blame] | 3011 | flagaddr := s.newValue1A(ssa.OpAddr, Ptrto(Types[TUINT32]), aux, s.sb) |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 3012 | // TODO: select the .enabled field. It is currently first, so not needed for now. |
David Chase | 8107b00 | 2016-02-28 11:15:22 -0500 | [diff] [blame] | 3013 | // Load word, test byte, avoiding partial register write from load byte. |
| 3014 | flag := s.newValue2(ssa.OpLoad, Types[TUINT32], flagaddr, s.mem()) |
| 3015 | flag = s.newValue1(ssa.OpTrunc64to8, Types[TBOOL], flag) |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 3016 | b := s.endBlock() |
| 3017 | b.Kind = ssa.BlockIf |
| 3018 | b.Likely = ssa.BranchUnlikely |
Keith Randall | 56e0ecc | 2016-03-15 20:45:50 -0700 | [diff] [blame] | 3019 | b.SetControl(flag) |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 3020 | b.AddEdgeTo(bThen) |
| 3021 | b.AddEdgeTo(bElse) |
| 3022 | |
| 3023 | // Issue write barriers for pointer writes. |
| 3024 | s.startBlock(bThen) |
Keith Randall | aebf661 | 2016-01-29 21:57:57 -0800 | [diff] [blame] | 3025 | s.storeTypePtrsWB(t, left, right) |
| 3026 | s.endBlock().AddEdgeTo(bEnd) |
| 3027 | |
| 3028 | // Issue regular stores for pointer writes. |
| 3029 | s.startBlock(bElse) |
| 3030 | s.storeTypePtrs(t, left, right) |
| 3031 | s.endBlock().AddEdgeTo(bEnd) |
| 3032 | |
| 3033 | s.startBlock(bEnd) |
| 3034 | |
| 3035 | if Debug_wb > 0 { |
Robert Griesemer | b83f397 | 2016-03-02 11:01:25 -0800 | [diff] [blame] | 3036 | Warnl(line, "write barrier") |
Keith Randall | aebf661 | 2016-01-29 21:57:57 -0800 | [diff] [blame] | 3037 | } |
| 3038 | } |
| 3039 | |
| 3040 | // do *left = right for all scalar (non-pointer) parts of t. |
Keith Randall | d4663e1 | 2016-03-21 10:22:03 -0700 | [diff] [blame] | 3041 | func (s *state) storeTypeScalars(t *Type, left, right *ssa.Value, skip skipMask) { |
Keith Randall | aebf661 | 2016-01-29 21:57:57 -0800 | [diff] [blame] | 3042 | switch { |
| 3043 | case t.IsBoolean() || t.IsInteger() || t.IsFloat() || t.IsComplex(): |
| 3044 | s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, t.Size(), left, right, s.mem()) |
Matthew Dempsky | 788f112 | 2016-03-28 10:55:44 -0700 | [diff] [blame] | 3045 | case t.IsPtrShaped(): |
Keith Randall | aebf661 | 2016-01-29 21:57:57 -0800 | [diff] [blame] | 3046 | // no scalar fields. |
| 3047 | case t.IsString(): |
Keith Randall | d4663e1 | 2016-03-21 10:22:03 -0700 | [diff] [blame] | 3048 | if skip&skipLen != 0 { |
| 3049 | return |
| 3050 | } |
Keith Randall | aebf661 | 2016-01-29 21:57:57 -0800 | [diff] [blame] | 3051 | len := s.newValue1(ssa.OpStringLen, Types[TINT], right) |
| 3052 | lenAddr := s.newValue1I(ssa.OpOffPtr, Ptrto(Types[TINT]), s.config.IntSize, left) |
| 3053 | s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, s.config.IntSize, lenAddr, len, s.mem()) |
| 3054 | case t.IsSlice(): |
Keith Randall | d4663e1 | 2016-03-21 10:22:03 -0700 | [diff] [blame] | 3055 | if skip&skipLen == 0 { |
| 3056 | len := s.newValue1(ssa.OpSliceLen, Types[TINT], right) |
| 3057 | lenAddr := s.newValue1I(ssa.OpOffPtr, Ptrto(Types[TINT]), s.config.IntSize, left) |
| 3058 | s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, s.config.IntSize, lenAddr, len, s.mem()) |
| 3059 | } |
| 3060 | if skip&skipCap == 0 { |
| 3061 | cap := s.newValue1(ssa.OpSliceCap, Types[TINT], right) |
| 3062 | capAddr := s.newValue1I(ssa.OpOffPtr, Ptrto(Types[TINT]), 2*s.config.IntSize, left) |
| 3063 | s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, s.config.IntSize, capAddr, cap, s.mem()) |
| 3064 | } |
Keith Randall | aebf661 | 2016-01-29 21:57:57 -0800 | [diff] [blame] | 3065 | case t.IsInterface(): |
| 3066 | // itab field doesn't need a write barrier (even though it is a pointer). |
| 3067 | itab := s.newValue1(ssa.OpITab, Ptrto(Types[TUINT8]), right) |
| 3068 | s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, s.config.IntSize, left, itab, s.mem()) |
| 3069 | case t.IsStruct(): |
| 3070 | n := t.NumFields() |
Matthew Dempsky | 1b9f168 | 2016-03-14 12:45:18 -0700 | [diff] [blame] | 3071 | for i := 0; i < n; i++ { |
Keith Randall | aebf661 | 2016-01-29 21:57:57 -0800 | [diff] [blame] | 3072 | ft := t.FieldType(i) |
| 3073 | addr := s.newValue1I(ssa.OpOffPtr, ft.PtrTo(), t.FieldOff(i), left) |
Matthew Dempsky | 1b9f168 | 2016-03-14 12:45:18 -0700 | [diff] [blame] | 3074 | val := s.newValue1I(ssa.OpStructSelect, ft, int64(i), right) |
Keith Randall | d4663e1 | 2016-03-21 10:22:03 -0700 | [diff] [blame] | 3075 | s.storeTypeScalars(ft.(*Type), addr, val, 0) |
Keith Randall | aebf661 | 2016-01-29 21:57:57 -0800 | [diff] [blame] | 3076 | } |
| 3077 | default: |
| 3078 | s.Fatalf("bad write barrier type %s", t) |
| 3079 | } |
| 3080 | } |
| 3081 | |
| 3082 | // do *left = right for all pointer parts of t. |
| 3083 | func (s *state) storeTypePtrs(t *Type, left, right *ssa.Value) { |
| 3084 | switch { |
Matthew Dempsky | 788f112 | 2016-03-28 10:55:44 -0700 | [diff] [blame] | 3085 | case t.IsPtrShaped(): |
Keith Randall | aebf661 | 2016-01-29 21:57:57 -0800 | [diff] [blame] | 3086 | s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, s.config.PtrSize, left, right, s.mem()) |
| 3087 | case t.IsString(): |
| 3088 | ptr := s.newValue1(ssa.OpStringPtr, Ptrto(Types[TUINT8]), right) |
| 3089 | s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, s.config.PtrSize, left, ptr, s.mem()) |
| 3090 | case t.IsSlice(): |
| 3091 | ptr := s.newValue1(ssa.OpSlicePtr, Ptrto(Types[TUINT8]), right) |
| 3092 | s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, s.config.PtrSize, left, ptr, s.mem()) |
| 3093 | case t.IsInterface(): |
| 3094 | // itab field is treated as a scalar. |
| 3095 | idata := s.newValue1(ssa.OpIData, Ptrto(Types[TUINT8]), right) |
| 3096 | idataAddr := s.newValue1I(ssa.OpOffPtr, Ptrto(Types[TUINT8]), s.config.PtrSize, left) |
| 3097 | s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, s.config.PtrSize, idataAddr, idata, s.mem()) |
| 3098 | case t.IsStruct(): |
| 3099 | n := t.NumFields() |
Matthew Dempsky | 1b9f168 | 2016-03-14 12:45:18 -0700 | [diff] [blame] | 3100 | for i := 0; i < n; i++ { |
Keith Randall | aebf661 | 2016-01-29 21:57:57 -0800 | [diff] [blame] | 3101 | ft := t.FieldType(i) |
| 3102 | if !haspointers(ft.(*Type)) { |
| 3103 | continue |
| 3104 | } |
| 3105 | addr := s.newValue1I(ssa.OpOffPtr, ft.PtrTo(), t.FieldOff(i), left) |
Matthew Dempsky | 1b9f168 | 2016-03-14 12:45:18 -0700 | [diff] [blame] | 3106 | val := s.newValue1I(ssa.OpStructSelect, ft, int64(i), right) |
Keith Randall | aebf661 | 2016-01-29 21:57:57 -0800 | [diff] [blame] | 3107 | s.storeTypePtrs(ft.(*Type), addr, val) |
| 3108 | } |
| 3109 | default: |
| 3110 | s.Fatalf("bad write barrier type %s", t) |
| 3111 | } |
| 3112 | } |
| 3113 | |
| 3114 | // do *left = right with a write barrier for all pointer parts of t. |
| 3115 | func (s *state) storeTypePtrsWB(t *Type, left, right *ssa.Value) { |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 3116 | switch { |
Matthew Dempsky | 788f112 | 2016-03-28 10:55:44 -0700 | [diff] [blame] | 3117 | case t.IsPtrShaped(): |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 3118 | s.rtcall(writebarrierptr, true, nil, left, right) |
| 3119 | case t.IsString(): |
| 3120 | ptr := s.newValue1(ssa.OpStringPtr, Ptrto(Types[TUINT8]), right) |
| 3121 | s.rtcall(writebarrierptr, true, nil, left, ptr) |
| 3122 | case t.IsSlice(): |
| 3123 | ptr := s.newValue1(ssa.OpSlicePtr, Ptrto(Types[TUINT8]), right) |
| 3124 | s.rtcall(writebarrierptr, true, nil, left, ptr) |
| 3125 | case t.IsInterface(): |
| 3126 | idata := s.newValue1(ssa.OpIData, Ptrto(Types[TUINT8]), right) |
| 3127 | idataAddr := s.newValue1I(ssa.OpOffPtr, Ptrto(Types[TUINT8]), s.config.PtrSize, left) |
| 3128 | s.rtcall(writebarrierptr, true, nil, idataAddr, idata) |
Keith Randall | aebf661 | 2016-01-29 21:57:57 -0800 | [diff] [blame] | 3129 | case t.IsStruct(): |
| 3130 | n := t.NumFields() |
Matthew Dempsky | 1b9f168 | 2016-03-14 12:45:18 -0700 | [diff] [blame] | 3131 | for i := 0; i < n; i++ { |
Keith Randall | aebf661 | 2016-01-29 21:57:57 -0800 | [diff] [blame] | 3132 | ft := t.FieldType(i) |
| 3133 | if !haspointers(ft.(*Type)) { |
| 3134 | continue |
| 3135 | } |
| 3136 | addr := s.newValue1I(ssa.OpOffPtr, ft.PtrTo(), t.FieldOff(i), left) |
Matthew Dempsky | 1b9f168 | 2016-03-14 12:45:18 -0700 | [diff] [blame] | 3137 | val := s.newValue1I(ssa.OpStructSelect, ft, int64(i), right) |
Keith Randall | aebf661 | 2016-01-29 21:57:57 -0800 | [diff] [blame] | 3138 | s.storeTypePtrsWB(ft.(*Type), addr, val) |
| 3139 | } |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 3140 | default: |
| 3141 | s.Fatalf("bad write barrier type %s", t) |
| 3142 | } |
Keith Randall | 9d22c10 | 2015-09-11 11:02:57 -0700 | [diff] [blame] | 3143 | } |
| 3144 | |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 3145 | // slice computes the slice v[i:j:k] and returns ptr, len, and cap of result. |
| 3146 | // i,j,k may be nil, in which case they are set to their default value. |
| 3147 | // t is a slice, ptr to array, or string type. |
| 3148 | func (s *state) slice(t *Type, v, i, j, k *ssa.Value) (p, l, c *ssa.Value) { |
| 3149 | var elemtype *Type |
| 3150 | var ptrtype *Type |
| 3151 | var ptr *ssa.Value |
| 3152 | var len *ssa.Value |
| 3153 | var cap *ssa.Value |
| 3154 | zero := s.constInt(Types[TINT], 0) |
| 3155 | switch { |
| 3156 | case t.IsSlice(): |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 3157 | elemtype = t.Elem() |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 3158 | ptrtype = Ptrto(elemtype) |
| 3159 | ptr = s.newValue1(ssa.OpSlicePtr, ptrtype, v) |
| 3160 | len = s.newValue1(ssa.OpSliceLen, Types[TINT], v) |
| 3161 | cap = s.newValue1(ssa.OpSliceCap, Types[TINT], v) |
| 3162 | case t.IsString(): |
| 3163 | elemtype = Types[TUINT8] |
| 3164 | ptrtype = Ptrto(elemtype) |
| 3165 | ptr = s.newValue1(ssa.OpStringPtr, ptrtype, v) |
| 3166 | len = s.newValue1(ssa.OpStringLen, Types[TINT], v) |
| 3167 | cap = len |
| 3168 | case t.IsPtr(): |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 3169 | if !t.Elem().IsArray() { |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 3170 | s.Fatalf("bad ptr to array in slice %v\n", t) |
| 3171 | } |
Josh Bleecher Snyder | 8640b51 | 2016-03-30 10:57:47 -0700 | [diff] [blame] | 3172 | elemtype = t.Elem().Elem() |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 3173 | ptrtype = Ptrto(elemtype) |
| 3174 | s.nilCheck(v) |
| 3175 | ptr = v |
Josh Bleecher Snyder | 3a0783c | 2016-03-31 14:46:04 -0700 | [diff] [blame] | 3176 | len = s.constInt(Types[TINT], t.Elem().NumElem()) |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 3177 | cap = len |
| 3178 | default: |
| 3179 | s.Fatalf("bad type in slice %v\n", t) |
| 3180 | } |
| 3181 | |
| 3182 | // Set default values |
| 3183 | if i == nil { |
| 3184 | i = zero |
| 3185 | } |
| 3186 | if j == nil { |
| 3187 | j = len |
| 3188 | } |
| 3189 | if k == nil { |
| 3190 | k = cap |
| 3191 | } |
| 3192 | |
| 3193 | // Panic if slice indices are not in bounds. |
| 3194 | s.sliceBoundsCheck(i, j) |
| 3195 | if j != k { |
| 3196 | s.sliceBoundsCheck(j, k) |
| 3197 | } |
| 3198 | if k != cap { |
| 3199 | s.sliceBoundsCheck(k, cap) |
| 3200 | } |
| 3201 | |
| 3202 | // Generate the following code assuming that indexes are in bounds. |
| 3203 | // The conditional is to make sure that we don't generate a slice |
| 3204 | // that points to the next object in memory. |
Keith Randall | 69a7c152 | 2016-03-21 15:24:08 -0700 | [diff] [blame] | 3205 | // rlen = j-i |
| 3206 | // rcap = k-i |
| 3207 | // delta = i*elemsize |
| 3208 | // if rcap == 0 { |
| 3209 | // delta = 0 |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 3210 | // } |
Keith Randall | 69a7c152 | 2016-03-21 15:24:08 -0700 | [diff] [blame] | 3211 | // rptr = p+delta |
| 3212 | // result = (SliceMake rptr rlen rcap) |
Keith Randall | 582baae | 2015-11-02 21:28:13 -0800 | [diff] [blame] | 3213 | subOp := s.ssaOp(OSUB, Types[TINT]) |
Keith Randall | 69a7c152 | 2016-03-21 15:24:08 -0700 | [diff] [blame] | 3214 | eqOp := s.ssaOp(OEQ, Types[TINT]) |
Keith Randall | 582baae | 2015-11-02 21:28:13 -0800 | [diff] [blame] | 3215 | mulOp := s.ssaOp(OMUL, Types[TINT]) |
| 3216 | rlen := s.newValue2(subOp, Types[TINT], j, i) |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 3217 | var rcap *ssa.Value |
| 3218 | switch { |
| 3219 | case t.IsString(): |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 3220 | // Capacity of the result is unimportant. However, we use |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 3221 | // rcap to test if we've generated a zero-length slice. |
| 3222 | // Use length of strings for that. |
| 3223 | rcap = rlen |
| 3224 | case j == k: |
| 3225 | rcap = rlen |
| 3226 | default: |
Keith Randall | 582baae | 2015-11-02 21:28:13 -0800 | [diff] [blame] | 3227 | rcap = s.newValue2(subOp, Types[TINT], k, i) |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 3228 | } |
| 3229 | |
Keith Randall | 69a7c152 | 2016-03-21 15:24:08 -0700 | [diff] [blame] | 3230 | // delta = # of elements to offset pointer by. |
| 3231 | s.vars[&deltaVar] = i |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 3232 | |
Keith Randall | 69a7c152 | 2016-03-21 15:24:08 -0700 | [diff] [blame] | 3233 | // Generate code to set delta=0 if the resulting capacity is zero. |
| 3234 | if !((i.Op == ssa.OpConst64 && i.AuxInt == 0) || |
| 3235 | (i.Op == ssa.OpConst32 && int32(i.AuxInt) == 0)) { |
| 3236 | cmp := s.newValue2(eqOp, Types[TBOOL], rcap, zero) |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 3237 | |
Keith Randall | 69a7c152 | 2016-03-21 15:24:08 -0700 | [diff] [blame] | 3238 | b := s.endBlock() |
| 3239 | b.Kind = ssa.BlockIf |
| 3240 | b.Likely = ssa.BranchUnlikely |
| 3241 | b.SetControl(cmp) |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 3242 | |
Keith Randall | 69a7c152 | 2016-03-21 15:24:08 -0700 | [diff] [blame] | 3243 | // Generate block which zeros the delta variable. |
| 3244 | nz := s.f.NewBlock(ssa.BlockPlain) |
| 3245 | b.AddEdgeTo(nz) |
| 3246 | s.startBlock(nz) |
| 3247 | s.vars[&deltaVar] = zero |
| 3248 | s.endBlock() |
| 3249 | |
| 3250 | // All done. |
| 3251 | merge := s.f.NewBlock(ssa.BlockPlain) |
| 3252 | b.AddEdgeTo(merge) |
| 3253 | nz.AddEdgeTo(merge) |
| 3254 | s.startBlock(merge) |
| 3255 | |
| 3256 | // TODO: use conditional moves somehow? |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 3257 | } |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 3258 | |
Keith Randall | 69a7c152 | 2016-03-21 15:24:08 -0700 | [diff] [blame] | 3259 | // Compute rptr = ptr + delta * elemsize |
| 3260 | rptr := s.newValue2(ssa.OpAddPtr, ptrtype, ptr, s.newValue2(mulOp, Types[TINT], s.variable(&deltaVar, Types[TINT]), s.constInt(Types[TINT], elemtype.Width))) |
| 3261 | delete(s.vars, &deltaVar) |
Keith Randall | 5505e8c | 2015-09-12 23:27:26 -0700 | [diff] [blame] | 3262 | return rptr, rlen, rcap |
| 3263 | } |
| 3264 | |
David Chase | 4282588 | 2015-08-20 15:14:20 -0400 | [diff] [blame] | 3265 | type u2fcvtTab struct { |
| 3266 | geq, cvt2F, and, rsh, or, add ssa.Op |
| 3267 | one func(*state, ssa.Type, int64) *ssa.Value |
| 3268 | } |
| 3269 | |
| 3270 | var u64_f64 u2fcvtTab = u2fcvtTab{ |
| 3271 | geq: ssa.OpGeq64, |
| 3272 | cvt2F: ssa.OpCvt64to64F, |
| 3273 | and: ssa.OpAnd64, |
| 3274 | rsh: ssa.OpRsh64Ux64, |
| 3275 | or: ssa.OpOr64, |
| 3276 | add: ssa.OpAdd64F, |
| 3277 | one: (*state).constInt64, |
| 3278 | } |
| 3279 | |
| 3280 | var u64_f32 u2fcvtTab = u2fcvtTab{ |
| 3281 | geq: ssa.OpGeq64, |
| 3282 | cvt2F: ssa.OpCvt64to32F, |
| 3283 | and: ssa.OpAnd64, |
| 3284 | rsh: ssa.OpRsh64Ux64, |
| 3285 | or: ssa.OpOr64, |
| 3286 | add: ssa.OpAdd32F, |
| 3287 | one: (*state).constInt64, |
| 3288 | } |
| 3289 | |
| 3290 | // Excess generality on a machine with 64-bit integer registers. |
| 3291 | // Not used on AMD64. |
| 3292 | var u32_f32 u2fcvtTab = u2fcvtTab{ |
| 3293 | geq: ssa.OpGeq32, |
| 3294 | cvt2F: ssa.OpCvt32to32F, |
| 3295 | and: ssa.OpAnd32, |
| 3296 | rsh: ssa.OpRsh32Ux32, |
| 3297 | or: ssa.OpOr32, |
| 3298 | add: ssa.OpAdd32F, |
| 3299 | one: func(s *state, t ssa.Type, x int64) *ssa.Value { |
| 3300 | return s.constInt32(t, int32(x)) |
| 3301 | }, |
| 3302 | } |
| 3303 | |
| 3304 | func (s *state) uint64Tofloat64(n *Node, x *ssa.Value, ft, tt *Type) *ssa.Value { |
| 3305 | return s.uintTofloat(&u64_f64, n, x, ft, tt) |
| 3306 | } |
| 3307 | |
| 3308 | func (s *state) uint64Tofloat32(n *Node, x *ssa.Value, ft, tt *Type) *ssa.Value { |
| 3309 | return s.uintTofloat(&u64_f32, n, x, ft, tt) |
| 3310 | } |
| 3311 | |
| 3312 | func (s *state) uintTofloat(cvttab *u2fcvtTab, n *Node, x *ssa.Value, ft, tt *Type) *ssa.Value { |
| 3313 | // if x >= 0 { |
| 3314 | // result = (floatY) x |
| 3315 | // } else { |
| 3316 | // y = uintX(x) ; y = x & 1 |
| 3317 | // z = uintX(x) ; z = z >> 1 |
| 3318 | // z = z >> 1 |
| 3319 | // z = z | y |
David Chase | 7315106 | 2015-08-26 14:25:40 -0400 | [diff] [blame] | 3320 | // result = floatY(z) |
| 3321 | // result = result + result |
David Chase | 4282588 | 2015-08-20 15:14:20 -0400 | [diff] [blame] | 3322 | // } |
| 3323 | // |
| 3324 | // Code borrowed from old code generator. |
| 3325 | // What's going on: large 64-bit "unsigned" looks like |
| 3326 | // negative number to hardware's integer-to-float |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 3327 | // conversion. However, because the mantissa is only |
David Chase | 4282588 | 2015-08-20 15:14:20 -0400 | [diff] [blame] | 3328 | // 63 bits, we don't need the LSB, so instead we do an |
| 3329 | // unsigned right shift (divide by two), convert, and |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 3330 | // double. However, before we do that, we need to be |
David Chase | 4282588 | 2015-08-20 15:14:20 -0400 | [diff] [blame] | 3331 | // sure that we do not lose a "1" if that made the |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 3332 | // difference in the resulting rounding. Therefore, we |
| 3333 | // preserve it, and OR (not ADD) it back in. The case |
David Chase | 4282588 | 2015-08-20 15:14:20 -0400 | [diff] [blame] | 3334 | // that matters is when the eleven discarded bits are |
| 3335 | // equal to 10000000001; that rounds up, and the 1 cannot |
| 3336 | // be lost else it would round down if the LSB of the |
| 3337 | // candidate mantissa is 0. |
| 3338 | cmp := s.newValue2(cvttab.geq, Types[TBOOL], x, s.zeroVal(ft)) |
| 3339 | b := s.endBlock() |
| 3340 | b.Kind = ssa.BlockIf |
Keith Randall | 56e0ecc | 2016-03-15 20:45:50 -0700 | [diff] [blame] | 3341 | b.SetControl(cmp) |
David Chase | 4282588 | 2015-08-20 15:14:20 -0400 | [diff] [blame] | 3342 | b.Likely = ssa.BranchLikely |
| 3343 | |
| 3344 | bThen := s.f.NewBlock(ssa.BlockPlain) |
| 3345 | bElse := s.f.NewBlock(ssa.BlockPlain) |
| 3346 | bAfter := s.f.NewBlock(ssa.BlockPlain) |
| 3347 | |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 3348 | b.AddEdgeTo(bThen) |
David Chase | 4282588 | 2015-08-20 15:14:20 -0400 | [diff] [blame] | 3349 | s.startBlock(bThen) |
| 3350 | a0 := s.newValue1(cvttab.cvt2F, tt, x) |
| 3351 | s.vars[n] = a0 |
| 3352 | s.endBlock() |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 3353 | bThen.AddEdgeTo(bAfter) |
David Chase | 4282588 | 2015-08-20 15:14:20 -0400 | [diff] [blame] | 3354 | |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 3355 | b.AddEdgeTo(bElse) |
David Chase | 4282588 | 2015-08-20 15:14:20 -0400 | [diff] [blame] | 3356 | s.startBlock(bElse) |
| 3357 | one := cvttab.one(s, ft, 1) |
| 3358 | y := s.newValue2(cvttab.and, ft, x, one) |
| 3359 | z := s.newValue2(cvttab.rsh, ft, x, one) |
| 3360 | z = s.newValue2(cvttab.or, ft, z, y) |
| 3361 | a := s.newValue1(cvttab.cvt2F, tt, z) |
| 3362 | a1 := s.newValue2(cvttab.add, tt, a, a) |
| 3363 | s.vars[n] = a1 |
| 3364 | s.endBlock() |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 3365 | bElse.AddEdgeTo(bAfter) |
David Chase | 4282588 | 2015-08-20 15:14:20 -0400 | [diff] [blame] | 3366 | |
| 3367 | s.startBlock(bAfter) |
| 3368 | return s.variable(n, n.Type) |
| 3369 | } |
| 3370 | |
Todd Neal | 707af25 | 2015-08-28 15:56:43 -0500 | [diff] [blame] | 3371 | // referenceTypeBuiltin generates code for the len/cap builtins for maps and channels. |
| 3372 | func (s *state) referenceTypeBuiltin(n *Node, x *ssa.Value) *ssa.Value { |
| 3373 | if !n.Left.Type.IsMap() && !n.Left.Type.IsChan() { |
| 3374 | s.Fatalf("node must be a map or a channel") |
| 3375 | } |
Todd Neal | e0e4068 | 2015-08-26 18:40:52 -0500 | [diff] [blame] | 3376 | // if n == nil { |
| 3377 | // return 0 |
| 3378 | // } else { |
Todd Neal | 707af25 | 2015-08-28 15:56:43 -0500 | [diff] [blame] | 3379 | // // len |
Todd Neal | e0e4068 | 2015-08-26 18:40:52 -0500 | [diff] [blame] | 3380 | // return *((*int)n) |
Todd Neal | 707af25 | 2015-08-28 15:56:43 -0500 | [diff] [blame] | 3381 | // // cap |
| 3382 | // return *(((*int)n)+1) |
Todd Neal | e0e4068 | 2015-08-26 18:40:52 -0500 | [diff] [blame] | 3383 | // } |
| 3384 | lenType := n.Type |
Josh Bleecher Snyder | 3921427 | 2016-03-06 18:06:09 -0800 | [diff] [blame] | 3385 | nilValue := s.constNil(Types[TUINTPTR]) |
Todd Neal | 67ac8a3 | 2015-08-28 15:20:54 -0500 | [diff] [blame] | 3386 | cmp := s.newValue2(ssa.OpEqPtr, Types[TBOOL], x, nilValue) |
Todd Neal | e0e4068 | 2015-08-26 18:40:52 -0500 | [diff] [blame] | 3387 | b := s.endBlock() |
| 3388 | b.Kind = ssa.BlockIf |
Keith Randall | 56e0ecc | 2016-03-15 20:45:50 -0700 | [diff] [blame] | 3389 | b.SetControl(cmp) |
Todd Neal | e0e4068 | 2015-08-26 18:40:52 -0500 | [diff] [blame] | 3390 | b.Likely = ssa.BranchUnlikely |
| 3391 | |
| 3392 | bThen := s.f.NewBlock(ssa.BlockPlain) |
| 3393 | bElse := s.f.NewBlock(ssa.BlockPlain) |
| 3394 | bAfter := s.f.NewBlock(ssa.BlockPlain) |
| 3395 | |
Todd Neal | 707af25 | 2015-08-28 15:56:43 -0500 | [diff] [blame] | 3396 | // length/capacity of a nil map/chan is zero |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 3397 | b.AddEdgeTo(bThen) |
Todd Neal | e0e4068 | 2015-08-26 18:40:52 -0500 | [diff] [blame] | 3398 | s.startBlock(bThen) |
| 3399 | s.vars[n] = s.zeroVal(lenType) |
| 3400 | s.endBlock() |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 3401 | bThen.AddEdgeTo(bAfter) |
Todd Neal | e0e4068 | 2015-08-26 18:40:52 -0500 | [diff] [blame] | 3402 | |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 3403 | b.AddEdgeTo(bElse) |
Todd Neal | e0e4068 | 2015-08-26 18:40:52 -0500 | [diff] [blame] | 3404 | s.startBlock(bElse) |
Todd Neal | 707af25 | 2015-08-28 15:56:43 -0500 | [diff] [blame] | 3405 | if n.Op == OLEN { |
| 3406 | // length is stored in the first word for map/chan |
| 3407 | s.vars[n] = s.newValue2(ssa.OpLoad, lenType, x, s.mem()) |
| 3408 | } else if n.Op == OCAP { |
| 3409 | // capacity is stored in the second word for chan |
| 3410 | sw := s.newValue1I(ssa.OpOffPtr, lenType.PtrTo(), lenType.Width, x) |
| 3411 | s.vars[n] = s.newValue2(ssa.OpLoad, lenType, sw, s.mem()) |
| 3412 | } else { |
| 3413 | s.Fatalf("op must be OLEN or OCAP") |
| 3414 | } |
Todd Neal | e0e4068 | 2015-08-26 18:40:52 -0500 | [diff] [blame] | 3415 | s.endBlock() |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 3416 | bElse.AddEdgeTo(bAfter) |
Todd Neal | e0e4068 | 2015-08-26 18:40:52 -0500 | [diff] [blame] | 3417 | |
| 3418 | s.startBlock(bAfter) |
| 3419 | return s.variable(n, lenType) |
| 3420 | } |
| 3421 | |
David Chase | 7315106 | 2015-08-26 14:25:40 -0400 | [diff] [blame] | 3422 | type f2uCvtTab struct { |
| 3423 | ltf, cvt2U, subf ssa.Op |
| 3424 | value func(*state, ssa.Type, float64) *ssa.Value |
| 3425 | } |
| 3426 | |
| 3427 | var f32_u64 f2uCvtTab = f2uCvtTab{ |
| 3428 | ltf: ssa.OpLess32F, |
| 3429 | cvt2U: ssa.OpCvt32Fto64, |
| 3430 | subf: ssa.OpSub32F, |
| 3431 | value: (*state).constFloat32, |
| 3432 | } |
| 3433 | |
| 3434 | var f64_u64 f2uCvtTab = f2uCvtTab{ |
| 3435 | ltf: ssa.OpLess64F, |
| 3436 | cvt2U: ssa.OpCvt64Fto64, |
| 3437 | subf: ssa.OpSub64F, |
| 3438 | value: (*state).constFloat64, |
| 3439 | } |
| 3440 | |
| 3441 | func (s *state) float32ToUint64(n *Node, x *ssa.Value, ft, tt *Type) *ssa.Value { |
| 3442 | return s.floatToUint(&f32_u64, n, x, ft, tt) |
| 3443 | } |
| 3444 | func (s *state) float64ToUint64(n *Node, x *ssa.Value, ft, tt *Type) *ssa.Value { |
| 3445 | return s.floatToUint(&f64_u64, n, x, ft, tt) |
| 3446 | } |
| 3447 | |
| 3448 | func (s *state) floatToUint(cvttab *f2uCvtTab, n *Node, x *ssa.Value, ft, tt *Type) *ssa.Value { |
| 3449 | // if x < 9223372036854775808.0 { |
| 3450 | // result = uintY(x) |
| 3451 | // } else { |
| 3452 | // y = x - 9223372036854775808.0 |
| 3453 | // z = uintY(y) |
| 3454 | // result = z | -9223372036854775808 |
| 3455 | // } |
| 3456 | twoToThe63 := cvttab.value(s, ft, 9223372036854775808.0) |
| 3457 | cmp := s.newValue2(cvttab.ltf, Types[TBOOL], x, twoToThe63) |
| 3458 | b := s.endBlock() |
| 3459 | b.Kind = ssa.BlockIf |
Keith Randall | 56e0ecc | 2016-03-15 20:45:50 -0700 | [diff] [blame] | 3460 | b.SetControl(cmp) |
David Chase | 7315106 | 2015-08-26 14:25:40 -0400 | [diff] [blame] | 3461 | b.Likely = ssa.BranchLikely |
| 3462 | |
| 3463 | bThen := s.f.NewBlock(ssa.BlockPlain) |
| 3464 | bElse := s.f.NewBlock(ssa.BlockPlain) |
| 3465 | bAfter := s.f.NewBlock(ssa.BlockPlain) |
| 3466 | |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 3467 | b.AddEdgeTo(bThen) |
David Chase | 7315106 | 2015-08-26 14:25:40 -0400 | [diff] [blame] | 3468 | s.startBlock(bThen) |
| 3469 | a0 := s.newValue1(cvttab.cvt2U, tt, x) |
| 3470 | s.vars[n] = a0 |
| 3471 | s.endBlock() |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 3472 | bThen.AddEdgeTo(bAfter) |
David Chase | 7315106 | 2015-08-26 14:25:40 -0400 | [diff] [blame] | 3473 | |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 3474 | b.AddEdgeTo(bElse) |
David Chase | 7315106 | 2015-08-26 14:25:40 -0400 | [diff] [blame] | 3475 | s.startBlock(bElse) |
| 3476 | y := s.newValue2(cvttab.subf, ft, x, twoToThe63) |
| 3477 | y = s.newValue1(cvttab.cvt2U, tt, y) |
| 3478 | z := s.constInt64(tt, -9223372036854775808) |
| 3479 | a1 := s.newValue2(ssa.OpOr64, tt, y, z) |
| 3480 | s.vars[n] = a1 |
| 3481 | s.endBlock() |
Todd Neal | 47d6799 | 2015-08-28 21:36:29 -0500 | [diff] [blame] | 3482 | bElse.AddEdgeTo(bAfter) |
David Chase | 7315106 | 2015-08-26 14:25:40 -0400 | [diff] [blame] | 3483 | |
| 3484 | s.startBlock(bAfter) |
| 3485 | return s.variable(n, n.Type) |
| 3486 | } |
| 3487 | |
Keith Randall | 269baa9 | 2015-09-17 10:31:16 -0700 | [diff] [blame] | 3488 | // ifaceType returns the value for the word containing the type. |
| 3489 | // n is the node for the interface expression. |
| 3490 | // v is the corresponding value. |
| 3491 | func (s *state) ifaceType(n *Node, v *ssa.Value) *ssa.Value { |
| 3492 | byteptr := Ptrto(Types[TUINT8]) // type used in runtime prototypes for runtime type (*byte) |
| 3493 | |
Matthew Dempsky | 00e5a68 | 2016-04-01 13:36:24 -0700 | [diff] [blame] | 3494 | if n.Type.IsEmptyInterface() { |
Keith Randall | 269baa9 | 2015-09-17 10:31:16 -0700 | [diff] [blame] | 3495 | // Have *eface. The type is the first word in the struct. |
| 3496 | return s.newValue1(ssa.OpITab, byteptr, v) |
| 3497 | } |
| 3498 | |
| 3499 | // Have *iface. |
| 3500 | // The first word in the struct is the *itab. |
| 3501 | // If the *itab is nil, return 0. |
| 3502 | // Otherwise, the second word in the *itab is the type. |
| 3503 | |
| 3504 | tab := s.newValue1(ssa.OpITab, byteptr, v) |
| 3505 | s.vars[&typVar] = tab |
Josh Bleecher Snyder | 3921427 | 2016-03-06 18:06:09 -0800 | [diff] [blame] | 3506 | isnonnil := s.newValue2(ssa.OpNeqPtr, Types[TBOOL], tab, s.constNil(byteptr)) |
Keith Randall | 269baa9 | 2015-09-17 10:31:16 -0700 | [diff] [blame] | 3507 | b := s.endBlock() |
| 3508 | b.Kind = ssa.BlockIf |
Keith Randall | 56e0ecc | 2016-03-15 20:45:50 -0700 | [diff] [blame] | 3509 | b.SetControl(isnonnil) |
Keith Randall | 269baa9 | 2015-09-17 10:31:16 -0700 | [diff] [blame] | 3510 | b.Likely = ssa.BranchLikely |
| 3511 | |
| 3512 | bLoad := s.f.NewBlock(ssa.BlockPlain) |
| 3513 | bEnd := s.f.NewBlock(ssa.BlockPlain) |
| 3514 | |
| 3515 | b.AddEdgeTo(bLoad) |
| 3516 | b.AddEdgeTo(bEnd) |
| 3517 | bLoad.AddEdgeTo(bEnd) |
| 3518 | |
| 3519 | s.startBlock(bLoad) |
| 3520 | off := s.newValue1I(ssa.OpOffPtr, byteptr, int64(Widthptr), tab) |
| 3521 | s.vars[&typVar] = s.newValue2(ssa.OpLoad, byteptr, off, s.mem()) |
| 3522 | s.endBlock() |
| 3523 | |
| 3524 | s.startBlock(bEnd) |
| 3525 | typ := s.variable(&typVar, byteptr) |
| 3526 | delete(s.vars, &typVar) |
| 3527 | return typ |
| 3528 | } |
| 3529 | |
| 3530 | // dottype generates SSA for a type assertion node. |
| 3531 | // commaok indicates whether to panic or return a bool. |
| 3532 | // If commaok is false, resok will be nil. |
| 3533 | func (s *state) dottype(n *Node, commaok bool) (res, resok *ssa.Value) { |
| 3534 | iface := s.expr(n.Left) |
| 3535 | typ := s.ifaceType(n.Left, iface) // actual concrete type |
| 3536 | target := s.expr(typename(n.Type)) // target type |
| 3537 | if !isdirectiface(n.Type) { |
| 3538 | // walk rewrites ODOTTYPE/OAS2DOTTYPE into runtime calls except for this case. |
| 3539 | Fatalf("dottype needs a direct iface type %s", n.Type) |
| 3540 | } |
| 3541 | |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 3542 | if Debug_typeassert > 0 { |
Robert Griesemer | b83f397 | 2016-03-02 11:01:25 -0800 | [diff] [blame] | 3543 | Warnl(n.Lineno, "type assertion inlined") |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 3544 | } |
| 3545 | |
Keith Randall | 269baa9 | 2015-09-17 10:31:16 -0700 | [diff] [blame] | 3546 | // TODO: If we have a nonempty interface and its itab field is nil, |
| 3547 | // then this test is redundant and ifaceType should just branch directly to bFail. |
| 3548 | cond := s.newValue2(ssa.OpEqPtr, Types[TBOOL], typ, target) |
| 3549 | b := s.endBlock() |
| 3550 | b.Kind = ssa.BlockIf |
Keith Randall | 56e0ecc | 2016-03-15 20:45:50 -0700 | [diff] [blame] | 3551 | b.SetControl(cond) |
Keith Randall | 269baa9 | 2015-09-17 10:31:16 -0700 | [diff] [blame] | 3552 | b.Likely = ssa.BranchLikely |
| 3553 | |
| 3554 | byteptr := Ptrto(Types[TUINT8]) |
| 3555 | |
| 3556 | bOk := s.f.NewBlock(ssa.BlockPlain) |
| 3557 | bFail := s.f.NewBlock(ssa.BlockPlain) |
| 3558 | b.AddEdgeTo(bOk) |
| 3559 | b.AddEdgeTo(bFail) |
| 3560 | |
| 3561 | if !commaok { |
| 3562 | // on failure, panic by calling panicdottype |
| 3563 | s.startBlock(bFail) |
Keith Randall | 269baa9 | 2015-09-17 10:31:16 -0700 | [diff] [blame] | 3564 | taddr := s.newValue1A(ssa.OpAddr, byteptr, &ssa.ExternSymbol{byteptr, typenamesym(n.Left.Type)}, s.sb) |
Keith Randall | 8c5bfcc | 2015-09-18 15:11:30 -0700 | [diff] [blame] | 3565 | s.rtcall(panicdottype, false, nil, typ, target, taddr) |
Keith Randall | 269baa9 | 2015-09-17 10:31:16 -0700 | [diff] [blame] | 3566 | |
| 3567 | // on success, return idata field |
| 3568 | s.startBlock(bOk) |
| 3569 | return s.newValue1(ssa.OpIData, n.Type, iface), nil |
| 3570 | } |
| 3571 | |
| 3572 | // commaok is the more complicated case because we have |
| 3573 | // a control flow merge point. |
| 3574 | bEnd := s.f.NewBlock(ssa.BlockPlain) |
| 3575 | |
| 3576 | // type assertion succeeded |
| 3577 | s.startBlock(bOk) |
| 3578 | s.vars[&idataVar] = s.newValue1(ssa.OpIData, n.Type, iface) |
| 3579 | s.vars[&okVar] = s.constBool(true) |
| 3580 | s.endBlock() |
| 3581 | bOk.AddEdgeTo(bEnd) |
| 3582 | |
| 3583 | // type assertion failed |
| 3584 | s.startBlock(bFail) |
Josh Bleecher Snyder | 3921427 | 2016-03-06 18:06:09 -0800 | [diff] [blame] | 3585 | s.vars[&idataVar] = s.constNil(byteptr) |
Keith Randall | 269baa9 | 2015-09-17 10:31:16 -0700 | [diff] [blame] | 3586 | s.vars[&okVar] = s.constBool(false) |
| 3587 | s.endBlock() |
| 3588 | bFail.AddEdgeTo(bEnd) |
| 3589 | |
| 3590 | // merge point |
| 3591 | s.startBlock(bEnd) |
| 3592 | res = s.variable(&idataVar, byteptr) |
| 3593 | resok = s.variable(&okVar, Types[TBOOL]) |
| 3594 | delete(s.vars, &idataVar) |
| 3595 | delete(s.vars, &okVar) |
| 3596 | return res, resok |
| 3597 | } |
| 3598 | |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 3599 | // checkgoto checks that a goto from from to to does not |
| 3600 | // jump into a block or jump over variable declarations. |
| 3601 | // It is a copy of checkgoto in the pre-SSA backend, |
| 3602 | // modified only for line number handling. |
| 3603 | // TODO: document how this works and why it is designed the way it is. |
| 3604 | func (s *state) checkgoto(from *Node, to *Node) { |
| 3605 | if from.Sym == to.Sym { |
| 3606 | return |
| 3607 | } |
| 3608 | |
| 3609 | nf := 0 |
| 3610 | for fs := from.Sym; fs != nil; fs = fs.Link { |
| 3611 | nf++ |
| 3612 | } |
| 3613 | nt := 0 |
| 3614 | for fs := to.Sym; fs != nil; fs = fs.Link { |
| 3615 | nt++ |
| 3616 | } |
| 3617 | fs := from.Sym |
| 3618 | for ; nf > nt; nf-- { |
| 3619 | fs = fs.Link |
| 3620 | } |
| 3621 | if fs != to.Sym { |
| 3622 | // decide what to complain about. |
| 3623 | // prefer to complain about 'into block' over declarations, |
| 3624 | // so scan backward to find most recent block or else dcl. |
| 3625 | var block *Sym |
| 3626 | |
| 3627 | var dcl *Sym |
| 3628 | ts := to.Sym |
| 3629 | for ; nt > nf; nt-- { |
| 3630 | if ts.Pkg == nil { |
| 3631 | block = ts |
| 3632 | } else { |
| 3633 | dcl = ts |
| 3634 | } |
| 3635 | ts = ts.Link |
| 3636 | } |
| 3637 | |
| 3638 | for ts != fs { |
| 3639 | if ts.Pkg == nil { |
| 3640 | block = ts |
| 3641 | } else { |
| 3642 | dcl = ts |
| 3643 | } |
| 3644 | ts = ts.Link |
| 3645 | fs = fs.Link |
| 3646 | } |
| 3647 | |
Robert Griesemer | b83f397 | 2016-03-02 11:01:25 -0800 | [diff] [blame] | 3648 | lno := from.Left.Lineno |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 3649 | if block != nil { |
Robert Griesemer | 2faf5bc | 2016-03-02 11:30:29 -0800 | [diff] [blame] | 3650 | yyerrorl(lno, "goto %v jumps into block starting at %v", from.Left.Sym, linestr(block.Lastlineno)) |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 3651 | } else { |
Robert Griesemer | 2faf5bc | 2016-03-02 11:30:29 -0800 | [diff] [blame] | 3652 | yyerrorl(lno, "goto %v jumps over declaration of %v at %v", from.Left.Sym, dcl, linestr(dcl.Lastlineno)) |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 3653 | } |
| 3654 | } |
| 3655 | } |
| 3656 | |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 3657 | // variable returns the value of a variable at the current location. |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 3658 | func (s *state) variable(name *Node, t ssa.Type) *ssa.Value { |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 3659 | v := s.vars[name] |
| 3660 | if v == nil { |
Keith Randall | 8f22b52 | 2015-06-11 21:29:25 -0700 | [diff] [blame] | 3661 | v = s.newValue0A(ssa.OpFwdRef, t, name) |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3662 | s.fwdRefs = append(s.fwdRefs, v) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 3663 | s.vars[name] = v |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3664 | s.addNamedValue(name, v) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 3665 | } |
| 3666 | return v |
| 3667 | } |
| 3668 | |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 3669 | func (s *state) mem() *ssa.Value { |
Keith Randall | b32217a | 2015-09-17 16:45:10 -0700 | [diff] [blame] | 3670 | return s.variable(&memVar, ssa.TypeMem) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 3671 | } |
| 3672 | |
Keith Randall | cfc2aa5 | 2015-05-18 16:44:20 -0700 | [diff] [blame] | 3673 | func (s *state) linkForwardReferences() { |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 3674 | // Build SSA graph. Each variable on its first use in a basic block |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 3675 | // leaves a FwdRef in that block representing the incoming value |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 3676 | // of that variable. This function links that ref up with possible definitions, |
| 3677 | // inserting Phi values as needed. This is essentially the algorithm |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3678 | // described by Braun, Buchwald, Hack, Leißa, Mallon, and Zwinkau: |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 3679 | // http://pp.info.uni-karlsruhe.de/uploads/publikationen/braun13cc.pdf |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3680 | // Differences: |
| 3681 | // - We use FwdRef nodes to postpone phi building until the CFG is |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 3682 | // completely built. That way we can avoid the notion of "sealed" |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3683 | // blocks. |
| 3684 | // - Phi optimization is a separate pass (in ../ssa/phielim.go). |
| 3685 | for len(s.fwdRefs) > 0 { |
| 3686 | v := s.fwdRefs[len(s.fwdRefs)-1] |
| 3687 | s.fwdRefs = s.fwdRefs[:len(s.fwdRefs)-1] |
| 3688 | s.resolveFwdRef(v) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 3689 | } |
| 3690 | } |
| 3691 | |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3692 | // resolveFwdRef modifies v to be the variable's value at the start of its block. |
| 3693 | // v must be a FwdRef op. |
| 3694 | func (s *state) resolveFwdRef(v *ssa.Value) { |
| 3695 | b := v.Block |
| 3696 | name := v.Aux.(*Node) |
| 3697 | v.Aux = nil |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 3698 | if b == s.f.Entry { |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3699 | // Live variable at start of function. |
Keith Randall | 6a8a9da | 2016-02-27 17:49:31 -0800 | [diff] [blame] | 3700 | if s.canSSA(name) { |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3701 | v.Op = ssa.OpArg |
| 3702 | v.Aux = name |
| 3703 | return |
Keith Randall | 02f4d0a | 2015-11-02 08:10:26 -0800 | [diff] [blame] | 3704 | } |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 3705 | // Not SSAable. Load it. |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 3706 | addr := s.decladdrs[name] |
| 3707 | if addr == nil { |
| 3708 | // TODO: closure args reach here. |
David Chase | 32ffbf7 | 2015-10-08 17:14:12 -0400 | [diff] [blame] | 3709 | s.Unimplementedf("unhandled closure arg %s at entry to function %s", name, b.Func.Name) |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 3710 | } |
| 3711 | if _, ok := addr.Aux.(*ssa.ArgSymbol); !ok { |
| 3712 | s.Fatalf("variable live at start of function %s is not an argument %s", b.Func.Name, name) |
| 3713 | } |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3714 | v.Op = ssa.OpLoad |
| 3715 | v.AddArgs(addr, s.startmem) |
| 3716 | return |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 3717 | } |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3718 | if len(b.Preds) == 0 { |
Josh Bleecher Snyder | 61aa095 | 2015-07-20 15:39:14 -0700 | [diff] [blame] | 3719 | // This block is dead; we have no predecessors and we're not the entry block. |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3720 | // It doesn't matter what we use here as long as it is well-formed. |
| 3721 | v.Op = ssa.OpUnknown |
| 3722 | return |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 3723 | } |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3724 | // Find variable value on each predecessor. |
| 3725 | var argstore [4]*ssa.Value |
| 3726 | args := argstore[:0] |
| 3727 | for _, p := range b.Preds { |
| 3728 | args = append(args, s.lookupVarOutgoing(p, v.Type, name, v.Line)) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 3729 | } |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3730 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 3731 | // Decide if we need a phi or not. We need a phi if there |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3732 | // are two different args (which are both not v). |
| 3733 | var w *ssa.Value |
| 3734 | for _, a := range args { |
| 3735 | if a == v { |
| 3736 | continue // self-reference |
| 3737 | } |
| 3738 | if a == w { |
| 3739 | continue // already have this witness |
| 3740 | } |
| 3741 | if w != nil { |
| 3742 | // two witnesses, need a phi value |
| 3743 | v.Op = ssa.OpPhi |
| 3744 | v.AddArgs(args...) |
| 3745 | return |
| 3746 | } |
| 3747 | w = a // save witness |
| 3748 | } |
| 3749 | if w == nil { |
| 3750 | s.Fatalf("no witness for reachable phi %s", v) |
| 3751 | } |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 3752 | // One witness. Make v a copy of w. |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3753 | v.Op = ssa.OpCopy |
| 3754 | v.AddArg(w) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 3755 | } |
| 3756 | |
| 3757 | // lookupVarOutgoing finds the variable's value at the end of block b. |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3758 | func (s *state) lookupVarOutgoing(b *ssa.Block, t ssa.Type, name *Node, line int32) *ssa.Value { |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 3759 | m := s.defvars[b.ID] |
| 3760 | if v, ok := m[name]; ok { |
| 3761 | return v |
| 3762 | } |
| 3763 | // The variable is not defined by b and we haven't |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 3764 | // looked it up yet. Generate a FwdRef for the variable and return that. |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3765 | v := b.NewValue0A(line, ssa.OpFwdRef, t, name) |
| 3766 | s.fwdRefs = append(s.fwdRefs, v) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 3767 | m[name] = v |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 3768 | s.addNamedValue(name, v) |
Keith Randall | d2fd43a | 2015-04-15 15:51:25 -0700 | [diff] [blame] | 3769 | return v |
| 3770 | } |
| 3771 | |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 3772 | func (s *state) addNamedValue(n *Node, v *ssa.Value) { |
| 3773 | if n.Class == Pxxx { |
| 3774 | // Don't track our dummy nodes (&memVar etc.). |
| 3775 | return |
| 3776 | } |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 3777 | if strings.HasPrefix(n.Sym.Name, "autotmp_") { |
| 3778 | // Don't track autotmp_ variables. |
| 3779 | return |
| 3780 | } |
Keith Randall | 31d13f4 | 2016-03-08 20:09:48 -0800 | [diff] [blame] | 3781 | if n.Class == PPARAMOUT { |
| 3782 | // Don't track named output values. This prevents return values |
| 3783 | // from being assigned too early. See #14591 and #14762. TODO: allow this. |
| 3784 | return |
| 3785 | } |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 3786 | if n.Class == PAUTO && n.Xoffset != 0 { |
| 3787 | s.Fatalf("AUTO var with offset %s %d", n, n.Xoffset) |
| 3788 | } |
Keith Randall | 02f4d0a | 2015-11-02 08:10:26 -0800 | [diff] [blame] | 3789 | loc := ssa.LocalSlot{N: n, Type: n.Type, Off: 0} |
| 3790 | values, ok := s.f.NamedValues[loc] |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 3791 | if !ok { |
Keith Randall | 02f4d0a | 2015-11-02 08:10:26 -0800 | [diff] [blame] | 3792 | s.f.Names = append(s.f.Names, loc) |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 3793 | } |
Keith Randall | 02f4d0a | 2015-11-02 08:10:26 -0800 | [diff] [blame] | 3794 | s.f.NamedValues[loc] = append(values, v) |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 3795 | } |
| 3796 | |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 3797 | // Branch is an unresolved branch. |
| 3798 | type Branch struct { |
| 3799 | P *obj.Prog // branch instruction |
| 3800 | B *ssa.Block // target |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 3801 | } |
| 3802 | |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 3803 | // SSAGenState contains state needed during Prog generation. |
| 3804 | type SSAGenState struct { |
| 3805 | // Branches remembers all the branch instructions we've seen |
Keith Randall | 9569b95 | 2015-08-28 22:51:01 -0700 | [diff] [blame] | 3806 | // and where they would like to go. |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 3807 | Branches []Branch |
Keith Randall | 9569b95 | 2015-08-28 22:51:01 -0700 | [diff] [blame] | 3808 | |
| 3809 | // bstart remembers where each block starts (indexed by block ID) |
| 3810 | bstart []*obj.Prog |
Keith Randall | 9569b95 | 2015-08-28 22:51:01 -0700 | [diff] [blame] | 3811 | } |
| 3812 | |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 3813 | // Pc returns the current Prog. |
| 3814 | func (s *SSAGenState) Pc() *obj.Prog { |
| 3815 | return Pc |
| 3816 | } |
| 3817 | |
| 3818 | // SetLineno sets the current source line number. |
| 3819 | func (s *SSAGenState) SetLineno(l int32) { |
| 3820 | lineno = l |
| 3821 | } |
| 3822 | |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 3823 | // genssa appends entries to ptxt for each instruction in f. |
| 3824 | // gcargs and gclocals are filled in with pointer maps for the frame. |
| 3825 | func genssa(f *ssa.Func, ptxt *obj.Prog, gcargs, gclocals *Sym) { |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 3826 | var s SSAGenState |
Keith Randall | 9569b95 | 2015-08-28 22:51:01 -0700 | [diff] [blame] | 3827 | |
Josh Bleecher Snyder | d298209 | 2015-07-22 13:13:53 -0700 | [diff] [blame] | 3828 | e := f.Config.Frontend().(*ssaExport) |
| 3829 | // We're about to emit a bunch of Progs. |
| 3830 | // Since the only way to get here is to explicitly request it, |
| 3831 | // just fail on unimplemented instead of trying to unwind our mess. |
| 3832 | e.mustImplement = true |
| 3833 | |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 3834 | // Remember where each block starts. |
Keith Randall | 9569b95 | 2015-08-28 22:51:01 -0700 | [diff] [blame] | 3835 | s.bstart = make([]*obj.Prog, f.NumBlocks()) |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 3836 | |
Josh Bleecher Snyder | b8efee0 | 2015-07-31 14:37:15 -0700 | [diff] [blame] | 3837 | var valueProgs map[*obj.Prog]*ssa.Value |
| 3838 | var blockProgs map[*obj.Prog]*ssa.Block |
Dave Cheney | cb1f2af | 2016-03-17 13:46:43 +1100 | [diff] [blame] | 3839 | var logProgs = e.log |
Josh Bleecher Snyder | b8efee0 | 2015-07-31 14:37:15 -0700 | [diff] [blame] | 3840 | if logProgs { |
| 3841 | valueProgs = make(map[*obj.Prog]*ssa.Value, f.NumValues()) |
| 3842 | blockProgs = make(map[*obj.Prog]*ssa.Block, f.NumBlocks()) |
| 3843 | f.Logf("genssa %s\n", f.Name) |
| 3844 | blockProgs[Pc] = f.Blocks[0] |
| 3845 | } |
| 3846 | |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 3847 | // Emit basic blocks |
| 3848 | for i, b := range f.Blocks { |
Keith Randall | 9569b95 | 2015-08-28 22:51:01 -0700 | [diff] [blame] | 3849 | s.bstart[b.ID] = Pc |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 3850 | // Emit values in block |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 3851 | Thearch.SSAMarkMoves(&s, b) |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 3852 | for _, v := range b.Values { |
Josh Bleecher Snyder | b8efee0 | 2015-07-31 14:37:15 -0700 | [diff] [blame] | 3853 | x := Pc |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 3854 | Thearch.SSAGenValue(&s, v) |
Josh Bleecher Snyder | b8efee0 | 2015-07-31 14:37:15 -0700 | [diff] [blame] | 3855 | if logProgs { |
| 3856 | for ; x != Pc; x = x.Link { |
| 3857 | valueProgs[x] = v |
| 3858 | } |
| 3859 | } |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 3860 | } |
| 3861 | // Emit control flow instructions for block |
| 3862 | var next *ssa.Block |
Keith Randall | 91f69c6 | 2016-02-26 16:32:01 -0800 | [diff] [blame] | 3863 | if i < len(f.Blocks)-1 && (Debug['N'] == 0 || b.Kind == ssa.BlockCall) { |
Keith Randall | 8906d2a | 2016-02-22 23:19:00 -0800 | [diff] [blame] | 3864 | // If -N, leave next==nil so every block with successors |
Keith Randall | 91f69c6 | 2016-02-26 16:32:01 -0800 | [diff] [blame] | 3865 | // ends in a JMP (except call blocks - plive doesn't like |
| 3866 | // select{send,recv} followed by a JMP call). Helps keep |
| 3867 | // line numbers for otherwise empty blocks. |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 3868 | next = f.Blocks[i+1] |
| 3869 | } |
Josh Bleecher Snyder | b8efee0 | 2015-07-31 14:37:15 -0700 | [diff] [blame] | 3870 | x := Pc |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 3871 | Thearch.SSAGenBlock(&s, b, next) |
Josh Bleecher Snyder | b8efee0 | 2015-07-31 14:37:15 -0700 | [diff] [blame] | 3872 | if logProgs { |
| 3873 | for ; x != Pc; x = x.Link { |
| 3874 | blockProgs[x] = b |
| 3875 | } |
| 3876 | } |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 3877 | } |
| 3878 | |
| 3879 | // Resolve branches |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 3880 | for _, br := range s.Branches { |
| 3881 | br.P.To.Val = s.bstart[br.B.ID] |
Keith Randall | 9569b95 | 2015-08-28 22:51:01 -0700 | [diff] [blame] | 3882 | } |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 3883 | |
Josh Bleecher Snyder | b8efee0 | 2015-07-31 14:37:15 -0700 | [diff] [blame] | 3884 | if logProgs { |
| 3885 | for p := ptxt; p != nil; p = p.Link { |
| 3886 | var s string |
| 3887 | if v, ok := valueProgs[p]; ok { |
| 3888 | s = v.String() |
| 3889 | } else if b, ok := blockProgs[p]; ok { |
| 3890 | s = b.String() |
| 3891 | } else { |
| 3892 | s = " " // most value and branch strings are 2-3 characters long |
| 3893 | } |
| 3894 | f.Logf("%s\t%s\n", s, p) |
| 3895 | } |
Josh Bleecher Snyder | 35fb514 | 2015-08-10 12:15:52 -0700 | [diff] [blame] | 3896 | if f.Config.HTML != nil { |
| 3897 | saved := ptxt.Ctxt.LineHist.PrintFilenameOnly |
| 3898 | ptxt.Ctxt.LineHist.PrintFilenameOnly = true |
| 3899 | var buf bytes.Buffer |
| 3900 | buf.WriteString("<code>") |
| 3901 | buf.WriteString("<dl class=\"ssa-gen\">") |
| 3902 | for p := ptxt; p != nil; p = p.Link { |
| 3903 | buf.WriteString("<dt class=\"ssa-prog-src\">") |
| 3904 | if v, ok := valueProgs[p]; ok { |
| 3905 | buf.WriteString(v.HTML()) |
| 3906 | } else if b, ok := blockProgs[p]; ok { |
| 3907 | buf.WriteString(b.HTML()) |
| 3908 | } |
| 3909 | buf.WriteString("</dt>") |
| 3910 | buf.WriteString("<dd class=\"ssa-prog\">") |
| 3911 | buf.WriteString(html.EscapeString(p.String())) |
| 3912 | buf.WriteString("</dd>") |
| 3913 | buf.WriteString("</li>") |
| 3914 | } |
| 3915 | buf.WriteString("</dl>") |
| 3916 | buf.WriteString("</code>") |
| 3917 | f.Config.HTML.WriteColumn("genssa", buf.String()) |
| 3918 | ptxt.Ctxt.LineHist.PrintFilenameOnly = saved |
| 3919 | } |
Josh Bleecher Snyder | b8efee0 | 2015-07-31 14:37:15 -0700 | [diff] [blame] | 3920 | } |
| 3921 | |
Josh Bleecher Snyder | 6b41665 | 2015-07-28 10:56:39 -0700 | [diff] [blame] | 3922 | // Emit static data |
| 3923 | if f.StaticData != nil { |
| 3924 | for _, n := range f.StaticData.([]*Node) { |
| 3925 | if !gen_as_init(n, false) { |
Keith Randall | 0ec72b6 | 2015-09-08 15:42:53 -0700 | [diff] [blame] | 3926 | Fatalf("non-static data marked as static: %v\n\n", n, f) |
Josh Bleecher Snyder | 6b41665 | 2015-07-28 10:56:39 -0700 | [diff] [blame] | 3927 | } |
| 3928 | } |
| 3929 | } |
| 3930 | |
Keith Randall | d2107fc | 2015-08-24 02:16:19 -0700 | [diff] [blame] | 3931 | // Allocate stack frame |
| 3932 | allocauto(ptxt) |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 3933 | |
Keith Randall | d2107fc | 2015-08-24 02:16:19 -0700 | [diff] [blame] | 3934 | // Generate gc bitmaps. |
| 3935 | liveness(Curfn, ptxt, gcargs, gclocals) |
| 3936 | gcsymdup(gcargs) |
| 3937 | gcsymdup(gclocals) |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 3938 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 3939 | // Add frame prologue. Zero ambiguously live variables. |
Keith Randall | d2107fc | 2015-08-24 02:16:19 -0700 | [diff] [blame] | 3940 | Thearch.Defframe(ptxt) |
| 3941 | if Debug['f'] != 0 { |
| 3942 | frame(0) |
| 3943 | } |
| 3944 | |
| 3945 | // Remove leftover instrumentation from the instruction stream. |
| 3946 | removevardef(ptxt) |
Josh Bleecher Snyder | 35fb514 | 2015-08-10 12:15:52 -0700 | [diff] [blame] | 3947 | |
| 3948 | f.Config.HTML.Close() |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 3949 | } |
| 3950 | |
Daniel Morsing | 66b4781 | 2015-06-27 15:45:20 +0100 | [diff] [blame] | 3951 | // movZero generates a register indirect move with a 0 immediate and keeps track of bytes left and next offset |
Matthew Dempsky | 0d9258a | 2016-03-07 18:00:08 -0800 | [diff] [blame] | 3952 | func movZero(as obj.As, width int64, nbytes int64, offset int64, regnum int16) (nleft int64, noff int64) { |
Daniel Morsing | 66b4781 | 2015-06-27 15:45:20 +0100 | [diff] [blame] | 3953 | p := Prog(as) |
| 3954 | // TODO: use zero register on archs that support it. |
| 3955 | p.From.Type = obj.TYPE_CONST |
| 3956 | p.From.Offset = 0 |
| 3957 | p.To.Type = obj.TYPE_MEM |
| 3958 | p.To.Reg = regnum |
| 3959 | p.To.Offset = offset |
| 3960 | offset += width |
| 3961 | nleft = nbytes - width |
| 3962 | return nleft, offset |
| 3963 | } |
| 3964 | |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 3965 | type FloatingEQNEJump struct { |
| 3966 | Jump obj.As |
| 3967 | Index int |
David Chase | 8e601b2 | 2015-08-18 14:39:26 -0400 | [diff] [blame] | 3968 | } |
| 3969 | |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 3970 | func oneFPJump(b *ssa.Block, jumps *FloatingEQNEJump, likely ssa.BranchPrediction, branches []Branch) []Branch { |
| 3971 | p := Prog(jumps.Jump) |
David Chase | 8e601b2 | 2015-08-18 14:39:26 -0400 | [diff] [blame] | 3972 | p.To.Type = obj.TYPE_BRANCH |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 3973 | to := jumps.Index |
| 3974 | branches = append(branches, Branch{p, b.Succs[to]}) |
David Chase | 8e601b2 | 2015-08-18 14:39:26 -0400 | [diff] [blame] | 3975 | if to == 1 { |
| 3976 | likely = -likely |
| 3977 | } |
| 3978 | // liblink reorders the instruction stream as it sees fit. |
| 3979 | // Pass along what we know so liblink can make use of it. |
| 3980 | // TODO: Once we've fully switched to SSA, |
| 3981 | // make liblink leave our output alone. |
| 3982 | switch likely { |
| 3983 | case ssa.BranchUnlikely: |
| 3984 | p.From.Type = obj.TYPE_CONST |
| 3985 | p.From.Offset = 0 |
| 3986 | case ssa.BranchLikely: |
| 3987 | p.From.Type = obj.TYPE_CONST |
| 3988 | p.From.Offset = 1 |
| 3989 | } |
| 3990 | return branches |
| 3991 | } |
| 3992 | |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 3993 | func SSAGenFPJump(s *SSAGenState, b, next *ssa.Block, jumps *[2][2]FloatingEQNEJump) { |
David Chase | 8e601b2 | 2015-08-18 14:39:26 -0400 | [diff] [blame] | 3994 | likely := b.Likely |
| 3995 | switch next { |
| 3996 | case b.Succs[0]: |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 3997 | s.Branches = oneFPJump(b, &jumps[0][0], likely, s.Branches) |
| 3998 | s.Branches = oneFPJump(b, &jumps[0][1], likely, s.Branches) |
David Chase | 8e601b2 | 2015-08-18 14:39:26 -0400 | [diff] [blame] | 3999 | case b.Succs[1]: |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 4000 | s.Branches = oneFPJump(b, &jumps[1][0], likely, s.Branches) |
| 4001 | s.Branches = oneFPJump(b, &jumps[1][1], likely, s.Branches) |
David Chase | 8e601b2 | 2015-08-18 14:39:26 -0400 | [diff] [blame] | 4002 | default: |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 4003 | s.Branches = oneFPJump(b, &jumps[1][0], likely, s.Branches) |
| 4004 | s.Branches = oneFPJump(b, &jumps[1][1], likely, s.Branches) |
David Chase | 8e601b2 | 2015-08-18 14:39:26 -0400 | [diff] [blame] | 4005 | q := Prog(obj.AJMP) |
| 4006 | q.To.Type = obj.TYPE_BRANCH |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 4007 | s.Branches = append(s.Branches, Branch{q, b.Succs[1]}) |
David Chase | 8e601b2 | 2015-08-18 14:39:26 -0400 | [diff] [blame] | 4008 | } |
Josh Bleecher Snyder | 71b5707 | 2015-07-24 12:47:00 -0700 | [diff] [blame] | 4009 | } |
| 4010 | |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 4011 | // AddAux adds the offset in the aux fields (AuxInt and Aux) of v to a. |
| 4012 | func AddAux(a *obj.Addr, v *ssa.Value) { |
| 4013 | AddAux2(a, v, v.AuxInt) |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 4014 | } |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 4015 | func AddAux2(a *obj.Addr, v *ssa.Value, offset int64) { |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 4016 | if a.Type != obj.TYPE_MEM { |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 4017 | v.Fatalf("bad AddAux addr %s", a) |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 4018 | } |
| 4019 | // add integer offset |
Keith Randall | d43f2e3 | 2015-10-21 13:13:56 -0700 | [diff] [blame] | 4020 | a.Offset += offset |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 4021 | |
| 4022 | // If no additional symbol offset, we're done. |
| 4023 | if v.Aux == nil { |
| 4024 | return |
| 4025 | } |
| 4026 | // Add symbol's offset from its base register. |
| 4027 | switch sym := v.Aux.(type) { |
| 4028 | case *ssa.ExternSymbol: |
| 4029 | a.Name = obj.NAME_EXTERN |
Ian Lance Taylor | 65b4020 | 2016-03-16 22:22:58 -0700 | [diff] [blame] | 4030 | switch s := sym.Sym.(type) { |
| 4031 | case *Sym: |
| 4032 | a.Sym = Linksym(s) |
| 4033 | case *obj.LSym: |
| 4034 | a.Sym = s |
| 4035 | default: |
| 4036 | v.Fatalf("ExternSymbol.Sym is %T", s) |
| 4037 | } |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 4038 | case *ssa.ArgSymbol: |
Keith Randall | d2107fc | 2015-08-24 02:16:19 -0700 | [diff] [blame] | 4039 | n := sym.Node.(*Node) |
| 4040 | a.Name = obj.NAME_PARAM |
| 4041 | a.Node = n |
| 4042 | a.Sym = Linksym(n.Orig.Sym) |
| 4043 | a.Offset += n.Xoffset // TODO: why do I have to add this here? I don't for auto variables. |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 4044 | case *ssa.AutoSymbol: |
Keith Randall | d2107fc | 2015-08-24 02:16:19 -0700 | [diff] [blame] | 4045 | n := sym.Node.(*Node) |
| 4046 | a.Name = obj.NAME_AUTO |
| 4047 | a.Node = n |
| 4048 | a.Sym = Linksym(n.Sym) |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 4049 | default: |
| 4050 | v.Fatalf("aux in %s not implemented %#v", v, v.Aux) |
| 4051 | } |
| 4052 | } |
| 4053 | |
Keith Randall | 582baae | 2015-11-02 21:28:13 -0800 | [diff] [blame] | 4054 | // extendIndex extends v to a full int width. |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 4055 | func (s *state) extendIndex(v *ssa.Value) *ssa.Value { |
| 4056 | size := v.Type.Size() |
Keith Randall | 582baae | 2015-11-02 21:28:13 -0800 | [diff] [blame] | 4057 | if size == s.config.IntSize { |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 4058 | return v |
| 4059 | } |
Keith Randall | 582baae | 2015-11-02 21:28:13 -0800 | [diff] [blame] | 4060 | if size > s.config.IntSize { |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 4061 | // TODO: truncate 64-bit indexes on 32-bit pointer archs. We'd need to test |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 4062 | // the high word and branch to out-of-bounds failure if it is not 0. |
| 4063 | s.Unimplementedf("64->32 index truncation not implemented") |
| 4064 | return v |
| 4065 | } |
| 4066 | |
| 4067 | // Extend value to the required size |
| 4068 | var op ssa.Op |
| 4069 | if v.Type.IsSigned() { |
Keith Randall | 582baae | 2015-11-02 21:28:13 -0800 | [diff] [blame] | 4070 | switch 10*size + s.config.IntSize { |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 4071 | case 14: |
| 4072 | op = ssa.OpSignExt8to32 |
| 4073 | case 18: |
| 4074 | op = ssa.OpSignExt8to64 |
| 4075 | case 24: |
| 4076 | op = ssa.OpSignExt16to32 |
| 4077 | case 28: |
| 4078 | op = ssa.OpSignExt16to64 |
| 4079 | case 48: |
| 4080 | op = ssa.OpSignExt32to64 |
| 4081 | default: |
| 4082 | s.Fatalf("bad signed index extension %s", v.Type) |
| 4083 | } |
| 4084 | } else { |
Keith Randall | 582baae | 2015-11-02 21:28:13 -0800 | [diff] [blame] | 4085 | switch 10*size + s.config.IntSize { |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 4086 | case 14: |
| 4087 | op = ssa.OpZeroExt8to32 |
| 4088 | case 18: |
| 4089 | op = ssa.OpZeroExt8to64 |
| 4090 | case 24: |
| 4091 | op = ssa.OpZeroExt16to32 |
| 4092 | case 28: |
| 4093 | op = ssa.OpZeroExt16to64 |
| 4094 | case 48: |
| 4095 | op = ssa.OpZeroExt32to64 |
| 4096 | default: |
| 4097 | s.Fatalf("bad unsigned index extension %s", v.Type) |
| 4098 | } |
| 4099 | } |
Keith Randall | 582baae | 2015-11-02 21:28:13 -0800 | [diff] [blame] | 4100 | return s.newValue1(op, Types[TINT], v) |
Keith Randall | 2a5e6c4 | 2015-07-23 14:35:02 -0700 | [diff] [blame] | 4101 | } |
| 4102 | |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 4103 | // SSARegNum returns the register (in cmd/internal/obj numbering) to |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 4104 | // which v has been allocated. Panics if v is not assigned to a |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 4105 | // register. |
Josh Bleecher Snyder | e139549 | 2015-08-05 16:06:39 -0700 | [diff] [blame] | 4106 | // TODO: Make this panic again once it stops happening routinely. |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 4107 | func SSARegNum(v *ssa.Value) int16 { |
Josh Bleecher Snyder | e139549 | 2015-08-05 16:06:39 -0700 | [diff] [blame] | 4108 | reg := v.Block.Func.RegAlloc[v.ID] |
| 4109 | if reg == nil { |
| 4110 | v.Unimplementedf("nil regnum for value: %s\n%s\n", v.LongString(), v.Block.Func) |
| 4111 | return 0 |
| 4112 | } |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 4113 | return Thearch.SSARegToReg[reg.(*ssa.Register).Num] |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 4114 | } |
| 4115 | |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 4116 | // AutoVar returns a *Node and int64 representing the auto variable and offset within it |
Keith Randall | 02f4d0a | 2015-11-02 08:10:26 -0800 | [diff] [blame] | 4117 | // where v should be spilled. |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 4118 | func AutoVar(v *ssa.Value) (*Node, int64) { |
Keith Randall | 02f4d0a | 2015-11-02 08:10:26 -0800 | [diff] [blame] | 4119 | loc := v.Block.Func.RegAlloc[v.ID].(ssa.LocalSlot) |
Keith Randall | 9094e3a | 2016-01-04 13:34:54 -0800 | [diff] [blame] | 4120 | if v.Type.Size() > loc.Type.Size() { |
| 4121 | v.Fatalf("spill/restore type %s doesn't fit in slot type %s", v.Type, loc.Type) |
| 4122 | } |
Keith Randall | 02f4d0a | 2015-11-02 08:10:26 -0800 | [diff] [blame] | 4123 | return loc.N.(*Node), loc.Off |
Keith Randall | 083a646 | 2015-05-12 11:06:44 -0700 | [diff] [blame] | 4124 | } |
Keith Randall | f7f604e | 2015-05-27 14:52:22 -0700 | [diff] [blame] | 4125 | |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 4126 | // fieldIdx finds the index of the field referred to by the ODOT node n. |
Matthew Dempsky | 1b9f168 | 2016-03-14 12:45:18 -0700 | [diff] [blame] | 4127 | func fieldIdx(n *Node) int { |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 4128 | t := n.Left.Type |
Ian Lance Taylor | 5f525ca | 2016-03-18 16:52:30 -0700 | [diff] [blame] | 4129 | f := n.Sym |
Matthew Dempsky | 3efefd9 | 2016-03-30 14:56:08 -0700 | [diff] [blame] | 4130 | if !t.IsStruct() { |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 4131 | panic("ODOT's LHS is not a struct") |
| 4132 | } |
| 4133 | |
Matthew Dempsky | 1b9f168 | 2016-03-14 12:45:18 -0700 | [diff] [blame] | 4134 | var i int |
Matthew Dempsky | f6bca3f | 2016-03-17 01:32:18 -0700 | [diff] [blame] | 4135 | for _, t1 := range t.Fields().Slice() { |
Ian Lance Taylor | 5f525ca | 2016-03-18 16:52:30 -0700 | [diff] [blame] | 4136 | if t1.Sym != f { |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 4137 | i++ |
| 4138 | continue |
| 4139 | } |
Matthew Dempsky | 62dddd4 | 2016-03-28 09:40:53 -0700 | [diff] [blame] | 4140 | if t1.Offset != n.Xoffset { |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 4141 | panic("field offset doesn't match") |
| 4142 | } |
| 4143 | return i |
| 4144 | } |
| 4145 | panic(fmt.Sprintf("can't find field in expr %s\n", n)) |
| 4146 | |
Eric Engestrom | 7a8caf7 | 2016-04-03 12:43:27 +0100 | [diff] [blame] | 4147 | // TODO: keep the result of this function somewhere in the ODOT Node |
Keith Randall | a734bbc | 2016-01-11 21:05:33 -0800 | [diff] [blame] | 4148 | // so we don't have to recompute it each time we need it. |
| 4149 | } |
| 4150 | |
Keith Randall | f7f604e | 2015-05-27 14:52:22 -0700 | [diff] [blame] | 4151 | // ssaExport exports a bunch of compiler services for the ssa backend. |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 4152 | type ssaExport struct { |
| 4153 | log bool |
| 4154 | unimplemented bool |
Josh Bleecher Snyder | d298209 | 2015-07-22 13:13:53 -0700 | [diff] [blame] | 4155 | mustImplement bool |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 4156 | } |
Keith Randall | f7f604e | 2015-05-27 14:52:22 -0700 | [diff] [blame] | 4157 | |
Josh Bleecher Snyder | 85e0329 | 2015-07-30 11:03:05 -0700 | [diff] [blame] | 4158 | func (s *ssaExport) TypeBool() ssa.Type { return Types[TBOOL] } |
| 4159 | func (s *ssaExport) TypeInt8() ssa.Type { return Types[TINT8] } |
| 4160 | func (s *ssaExport) TypeInt16() ssa.Type { return Types[TINT16] } |
| 4161 | func (s *ssaExport) TypeInt32() ssa.Type { return Types[TINT32] } |
| 4162 | func (s *ssaExport) TypeInt64() ssa.Type { return Types[TINT64] } |
| 4163 | func (s *ssaExport) TypeUInt8() ssa.Type { return Types[TUINT8] } |
| 4164 | func (s *ssaExport) TypeUInt16() ssa.Type { return Types[TUINT16] } |
| 4165 | func (s *ssaExport) TypeUInt32() ssa.Type { return Types[TUINT32] } |
| 4166 | func (s *ssaExport) TypeUInt64() ssa.Type { return Types[TUINT64] } |
David Chase | 5257858 | 2015-08-28 14:24:10 -0400 | [diff] [blame] | 4167 | func (s *ssaExport) TypeFloat32() ssa.Type { return Types[TFLOAT32] } |
| 4168 | func (s *ssaExport) TypeFloat64() ssa.Type { return Types[TFLOAT64] } |
Josh Bleecher Snyder | 85e0329 | 2015-07-30 11:03:05 -0700 | [diff] [blame] | 4169 | func (s *ssaExport) TypeInt() ssa.Type { return Types[TINT] } |
| 4170 | func (s *ssaExport) TypeUintptr() ssa.Type { return Types[TUINTPTR] } |
| 4171 | func (s *ssaExport) TypeString() ssa.Type { return Types[TSTRING] } |
| 4172 | func (s *ssaExport) TypeBytePtr() ssa.Type { return Ptrto(Types[TUINT8]) } |
| 4173 | |
Josh Bleecher Snyder | 8d31df18a | 2015-07-24 11:28:12 -0700 | [diff] [blame] | 4174 | // StringData returns a symbol (a *Sym wrapped in an interface) which |
| 4175 | // is the data component of a global string constant containing s. |
| 4176 | func (*ssaExport) StringData(s string) interface{} { |
Keith Randall | 8c46aa5 | 2015-06-19 21:02:28 -0700 | [diff] [blame] | 4177 | // TODO: is idealstring correct? It might not matter... |
Josh Bleecher Snyder | 8d31df18a | 2015-07-24 11:28:12 -0700 | [diff] [blame] | 4178 | _, data := stringsym(s) |
| 4179 | return &ssa.ExternSymbol{Typ: idealstring, Sym: data} |
Keith Randall | f7f604e | 2015-05-27 14:52:22 -0700 | [diff] [blame] | 4180 | } |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 4181 | |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 4182 | func (e *ssaExport) Auto(t ssa.Type) ssa.GCNode { |
Keith Randall | d2107fc | 2015-08-24 02:16:19 -0700 | [diff] [blame] | 4183 | n := temp(t.(*Type)) // Note: adds new auto to Curfn.Func.Dcl list |
| 4184 | e.mustImplement = true // This modifies the input to SSA, so we want to make sure we succeed from here! |
| 4185 | return n |
| 4186 | } |
| 4187 | |
Keith Randall | 4a7aba7 | 2016-03-28 11:25:17 -0700 | [diff] [blame] | 4188 | func (e *ssaExport) SplitString(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot) { |
| 4189 | n := name.N.(*Node) |
| 4190 | ptrType := Ptrto(Types[TUINT8]) |
| 4191 | lenType := Types[TINT] |
| 4192 | if n.Class == PAUTO && !n.Addrtaken { |
| 4193 | // Split this string up into two separate variables. |
| 4194 | p := e.namedAuto(n.Sym.Name+".ptr", ptrType) |
| 4195 | l := e.namedAuto(n.Sym.Name+".len", lenType) |
| 4196 | return ssa.LocalSlot{p, ptrType, 0}, ssa.LocalSlot{l, lenType, 0} |
| 4197 | } |
| 4198 | // Return the two parts of the larger variable. |
| 4199 | return ssa.LocalSlot{n, ptrType, name.Off}, ssa.LocalSlot{n, lenType, name.Off + int64(Widthptr)} |
| 4200 | } |
| 4201 | |
| 4202 | func (e *ssaExport) SplitInterface(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot) { |
| 4203 | n := name.N.(*Node) |
| 4204 | t := Ptrto(Types[TUINT8]) |
| 4205 | if n.Class == PAUTO && !n.Addrtaken { |
| 4206 | // Split this interface up into two separate variables. |
| 4207 | f := ".itab" |
Matthew Dempsky | 00e5a68 | 2016-04-01 13:36:24 -0700 | [diff] [blame] | 4208 | if n.Type.IsEmptyInterface() { |
Keith Randall | 4a7aba7 | 2016-03-28 11:25:17 -0700 | [diff] [blame] | 4209 | f = ".type" |
| 4210 | } |
| 4211 | c := e.namedAuto(n.Sym.Name+f, t) |
| 4212 | d := e.namedAuto(n.Sym.Name+".data", t) |
| 4213 | return ssa.LocalSlot{c, t, 0}, ssa.LocalSlot{d, t, 0} |
| 4214 | } |
| 4215 | // Return the two parts of the larger variable. |
| 4216 | return ssa.LocalSlot{n, t, name.Off}, ssa.LocalSlot{n, t, name.Off + int64(Widthptr)} |
| 4217 | } |
| 4218 | |
| 4219 | func (e *ssaExport) SplitSlice(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot, ssa.LocalSlot) { |
| 4220 | n := name.N.(*Node) |
Josh Bleecher Snyder | f38f43d | 2016-04-01 20:11:30 -0700 | [diff] [blame] | 4221 | ptrType := Ptrto(n.Type.Elem()) |
Keith Randall | 4a7aba7 | 2016-03-28 11:25:17 -0700 | [diff] [blame] | 4222 | lenType := Types[TINT] |
| 4223 | if n.Class == PAUTO && !n.Addrtaken { |
| 4224 | // Split this slice up into three separate variables. |
| 4225 | p := e.namedAuto(n.Sym.Name+".ptr", ptrType) |
| 4226 | l := e.namedAuto(n.Sym.Name+".len", lenType) |
| 4227 | c := e.namedAuto(n.Sym.Name+".cap", lenType) |
| 4228 | return ssa.LocalSlot{p, ptrType, 0}, ssa.LocalSlot{l, lenType, 0}, ssa.LocalSlot{c, lenType, 0} |
| 4229 | } |
| 4230 | // Return the three parts of the larger variable. |
| 4231 | return ssa.LocalSlot{n, ptrType, name.Off}, |
| 4232 | ssa.LocalSlot{n, lenType, name.Off + int64(Widthptr)}, |
| 4233 | ssa.LocalSlot{n, lenType, name.Off + int64(2*Widthptr)} |
| 4234 | } |
| 4235 | |
| 4236 | func (e *ssaExport) SplitComplex(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot) { |
| 4237 | n := name.N.(*Node) |
| 4238 | s := name.Type.Size() / 2 |
| 4239 | var t *Type |
| 4240 | if s == 8 { |
| 4241 | t = Types[TFLOAT64] |
| 4242 | } else { |
| 4243 | t = Types[TFLOAT32] |
| 4244 | } |
| 4245 | if n.Class == PAUTO && !n.Addrtaken { |
| 4246 | // Split this complex up into two separate variables. |
| 4247 | c := e.namedAuto(n.Sym.Name+".real", t) |
| 4248 | d := e.namedAuto(n.Sym.Name+".imag", t) |
| 4249 | return ssa.LocalSlot{c, t, 0}, ssa.LocalSlot{d, t, 0} |
| 4250 | } |
| 4251 | // Return the two parts of the larger variable. |
| 4252 | return ssa.LocalSlot{n, t, name.Off}, ssa.LocalSlot{n, t, name.Off + s} |
| 4253 | } |
| 4254 | |
| 4255 | // namedAuto returns a new AUTO variable with the given name and type. |
| 4256 | func (e *ssaExport) namedAuto(name string, typ ssa.Type) ssa.GCNode { |
| 4257 | t := typ.(*Type) |
| 4258 | s := Lookup(name) |
| 4259 | n := Nod(ONAME, nil, nil) |
| 4260 | s.Def = n |
| 4261 | s.Def.Used = true |
| 4262 | n.Sym = s |
| 4263 | n.Type = t |
| 4264 | n.Class = PAUTO |
| 4265 | n.Addable = true |
| 4266 | n.Ullman = 1 |
| 4267 | n.Esc = EscNever |
| 4268 | n.Xoffset = 0 |
| 4269 | n.Name.Curfn = Curfn |
| 4270 | Curfn.Func.Dcl = append(Curfn.Func.Dcl, n) |
| 4271 | |
| 4272 | dowidth(t) |
| 4273 | e.mustImplement = true |
| 4274 | |
| 4275 | return n |
| 4276 | } |
| 4277 | |
Keith Randall | 7d61246 | 2015-10-22 13:07:38 -0700 | [diff] [blame] | 4278 | func (e *ssaExport) CanSSA(t ssa.Type) bool { |
Keith Randall | 37590bd | 2015-09-18 22:58:10 -0700 | [diff] [blame] | 4279 | return canSSAType(t.(*Type)) |
| 4280 | } |
| 4281 | |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 4282 | func (e *ssaExport) Line(line int32) string { |
Robert Griesemer | 2faf5bc | 2016-03-02 11:30:29 -0800 | [diff] [blame] | 4283 | return linestr(line) |
Keith Randall | b5c5efd | 2016-01-14 16:02:23 -0800 | [diff] [blame] | 4284 | } |
| 4285 | |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 4286 | // Log logs a message from the compiler. |
Josh Bleecher Snyder | 37ddc27 | 2015-06-24 14:03:39 -0700 | [diff] [blame] | 4287 | func (e *ssaExport) Logf(msg string, args ...interface{}) { |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 4288 | // If e was marked as unimplemented, anything could happen. Ignore. |
| 4289 | if e.log && !e.unimplemented { |
| 4290 | fmt.Printf(msg, args...) |
| 4291 | } |
| 4292 | } |
| 4293 | |
David Chase | 88b230e | 2016-01-29 14:44:15 -0500 | [diff] [blame] | 4294 | func (e *ssaExport) Log() bool { |
| 4295 | return e.log |
| 4296 | } |
| 4297 | |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 4298 | // Fatal reports a compiler error and exits. |
Keith Randall | da8af47 | 2016-01-13 11:14:57 -0800 | [diff] [blame] | 4299 | func (e *ssaExport) Fatalf(line int32, msg string, args ...interface{}) { |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 4300 | // If e was marked as unimplemented, anything could happen. Ignore. |
| 4301 | if !e.unimplemented { |
Keith Randall | da8af47 | 2016-01-13 11:14:57 -0800 | [diff] [blame] | 4302 | lineno = line |
Keith Randall | 0ec72b6 | 2015-09-08 15:42:53 -0700 | [diff] [blame] | 4303 | Fatalf(msg, args...) |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 4304 | } |
| 4305 | } |
| 4306 | |
| 4307 | // Unimplemented reports that the function cannot be compiled. |
| 4308 | // It will be removed once SSA work is complete. |
Keith Randall | da8af47 | 2016-01-13 11:14:57 -0800 | [diff] [blame] | 4309 | func (e *ssaExport) Unimplementedf(line int32, msg string, args ...interface{}) { |
Josh Bleecher Snyder | d298209 | 2015-07-22 13:13:53 -0700 | [diff] [blame] | 4310 | if e.mustImplement { |
Keith Randall | da8af47 | 2016-01-13 11:14:57 -0800 | [diff] [blame] | 4311 | lineno = line |
Keith Randall | 0ec72b6 | 2015-09-08 15:42:53 -0700 | [diff] [blame] | 4312 | Fatalf(msg, args...) |
Josh Bleecher Snyder | d298209 | 2015-07-22 13:13:53 -0700 | [diff] [blame] | 4313 | } |
Josh Bleecher Snyder | 8c6abfe | 2015-06-12 11:01:13 -0700 | [diff] [blame] | 4314 | const alwaysLog = false // enable to calculate top unimplemented features |
| 4315 | if !e.unimplemented && (e.log || alwaysLog) { |
| 4316 | // first implementation failure, print explanation |
| 4317 | fmt.Printf("SSA unimplemented: "+msg+"\n", args...) |
| 4318 | } |
| 4319 | e.unimplemented = true |
| 4320 | } |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 4321 | |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 4322 | // Warnl reports a "warning", which is usually flag-triggered |
| 4323 | // logging output for the benefit of tests. |
Todd Neal | 98b88de | 2016-03-13 23:04:31 -0500 | [diff] [blame] | 4324 | func (e *ssaExport) Warnl(line int32, fmt_ string, args ...interface{}) { |
| 4325 | Warnl(line, fmt_, args...) |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 4326 | } |
| 4327 | |
| 4328 | func (e *ssaExport) Debug_checknil() bool { |
| 4329 | return Debug_checknil != 0 |
| 4330 | } |
| 4331 | |
Keith Randall | c24681a | 2015-10-22 14:22:38 -0700 | [diff] [blame] | 4332 | func (n *Node) Typ() ssa.Type { |
| 4333 | return n.Type |
| 4334 | } |