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