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