blob: 127a7c46986476295842599a17f9ef68665bacca [file] [log] [blame]
Keith Randalld2fd43a2015-04-15 15:51:25 -07001// 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
5package gc
6
7import (
Josh Bleecher Snyder35fb5142015-08-10 12:15:52 -07008 "bytes"
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -07009 "fmt"
Josh Bleecher Snyder35fb5142015-08-10 12:15:52 -070010 "html"
Josh Bleecher Snyderd2982092015-07-22 13:13:53 -070011 "os"
Brad Fitzpatrick7af53d92015-07-10 10:47:28 -060012 "strings"
Keith Randalld2fd43a2015-04-15 15:51:25 -070013
Keith Randall067e8df2015-05-28 13:49:20 -070014 "cmd/compile/internal/ssa"
Keith Randall083a6462015-05-12 11:06:44 -070015 "cmd/internal/obj"
Keith Randalld2fd43a2015-04-15 15:51:25 -070016)
17
Keith Randallc0740fe2016-03-03 22:06:57 -080018var ssaEnabled = true
19
Keith Randall2f57d0f2016-01-28 13:46:30 -080020var ssaConfig *ssa.Config
21var ssaExp ssaExport
22
David Chase378a8632016-02-25 13:10:51 -050023func 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 Randall5b355a72015-12-11 20:41:52 -080032func shouldssa(fn *Node) bool {
Keith Randall4c9a4702016-03-21 22:57:26 -070033 switch Thearch.Thestring {
34 default:
35 // Only available for testing.
36 if os.Getenv("SSATEST") == "" {
37 return false
38 }
39 // Generally available.
40 case "amd64":
Keith Randall5b355a72015-12-11 20:41:52 -080041 }
Keith Randallc0740fe2016-03-03 22:06:57 -080042 if !ssaEnabled {
43 return false
44 }
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -070045
David Chasee99dd522015-10-19 11:36:07 -040046 // Environment variable control of SSA CG
47 // 1. IF GOSSAFUNC == current function name THEN
48 // compile this function with SSA and log output to ssa.html
49
David Chase729abfa2015-10-26 17:34:06 -040050 // 2. IF GOSSAHASH == "" THEN
David Chasee99dd522015-10-19 11:36:07 -040051 // compile this function (and everything else) with SSA
52
David Chase729abfa2015-10-26 17:34:06 -040053 // 3. IF GOSSAHASH == "n" or "N"
David Chasee99dd522015-10-19 11:36:07 -040054 // IF GOSSAPKG == current package name THEN
55 // compile this function (and everything in this package) with SSA
56 // ELSE
57 // use the old back end for this function.
58 // This is for compatibility with existing test harness and should go away.
59
60 // 4. IF GOSSAHASH is a suffix of the binary-rendered SHA1 hash of the function name THEN
61 // compile this function with SSA
62 // ELSE
63 // compile this function with the old back end.
64
David Chase729abfa2015-10-26 17:34:06 -040065 // Plan is for 3 to be removed when the tests are revised.
66 // SSA is now default, and is disabled by setting
67 // GOSSAHASH to n or N, or selectively with strings of
68 // 0 and 1.
David Chasee99dd522015-10-19 11:36:07 -040069
Keith Randall5b355a72015-12-11 20:41:52 -080070 name := fn.Func.Nname.Sym.Name
71
72 funcname := os.Getenv("GOSSAFUNC")
73 if funcname != "" {
74 // If GOSSAFUNC is set, compile only that function.
75 return name == funcname
76 }
77
78 pkg := os.Getenv("GOSSAPKG")
79 if pkg != "" {
80 // If GOSSAPKG is set, compile only that package.
81 return localpkg.Name == pkg
82 }
83
David Chase378a8632016-02-25 13:10:51 -050084 return initssa().DebugHashMatch("GOSSAHASH", name)
Keith Randall5b355a72015-12-11 20:41:52 -080085}
86
87// buildssa builds an SSA function.
88func buildssa(fn *Node) *ssa.Func {
89 name := fn.Func.Nname.Sym.Name
Keith Randall59681802016-03-01 13:47:48 -080090 printssa := name == os.Getenv("GOSSAFUNC")
Keith Randall5b355a72015-12-11 20:41:52 -080091 if printssa {
Josh Bleecher Snydere0ac5c52015-07-20 18:42:45 -070092 fmt.Println("generating SSA for", name)
Ian Lance Taylor55c65d42016-03-04 13:16:48 -080093 dumplist("buildssa-enter", fn.Func.Enter)
94 dumplist("buildssa-body", fn.Nbody)
95 dumplist("buildssa-exit", fn.Func.Exit)
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -070096 }
Keith Randalld2fd43a2015-04-15 15:51:25 -070097
Keith Randallcfc2aa52015-05-18 16:44:20 -070098 var s state
Michael Matloob81ccf502015-05-30 01:03:06 -040099 s.pushLine(fn.Lineno)
100 defer s.popLine()
101
Keith Randall6a8a9da2016-02-27 17:49:31 -0800102 if fn.Func.Pragma&CgoUnsafeArgs != 0 {
103 s.cgoUnsafeArgs = true
104 }
Keith Randall15ed37d2016-03-16 21:51:17 -0700105 if fn.Func.Pragma&Nowritebarrier != 0 {
106 s.noWB = true
107 }
108 defer func() {
109 if s.WBLineno != 0 {
110 fn.Func.WBLineno = s.WBLineno
111 }
112 }()
Keith Randalld2fd43a2015-04-15 15:51:25 -0700113 // TODO(khr): build config just once at the start of the compiler binary
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -0700114
Keith Randall2f57d0f2016-01-28 13:46:30 -0800115 ssaExp.log = printssa
David Chase378a8632016-02-25 13:10:51 -0500116
117 s.config = initssa()
Keith Randalld2fd43a2015-04-15 15:51:25 -0700118 s.f = s.config.NewFunc()
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -0700119 s.f.Name = name
David Chase8824dcc2015-10-08 12:39:56 -0400120 s.exitCode = fn.Func.Exit
Keith Randall74e568f2015-11-09 21:35:40 -0800121 s.panics = map[funcLine]*ssa.Block{}
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -0700122
Josh Bleecher Snyder35fb5142015-08-10 12:15:52 -0700123 if name == os.Getenv("GOSSAFUNC") {
124 // TODO: tempfile? it is handy to have the location
125 // of this file be stable, so you can just reload in the browser.
Keith Randallda8af472016-01-13 11:14:57 -0800126 s.config.HTML = ssa.NewHTMLWriter("ssa.html", s.config, name)
Josh Bleecher Snyder35fb5142015-08-10 12:15:52 -0700127 // TODO: generate and print a mapping from nodes to values and blocks
128 }
129 defer func() {
Keith Randall5b355a72015-12-11 20:41:52 -0800130 if !printssa {
Josh Bleecher Snyder35fb5142015-08-10 12:15:52 -0700131 s.config.HTML.Close()
132 }
133 }()
134
Keith Randalld2fd43a2015-04-15 15:51:25 -0700135 // Allocate starting block
136 s.f.Entry = s.f.NewBlock(ssa.BlockPlain)
137
Keith Randallcfc2aa52015-05-18 16:44:20 -0700138 // Allocate starting values
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700139 s.labels = map[string]*ssaLabel{}
140 s.labeledNodes = map[*Node]*ssaLabel{}
Keith Randall02f4d0a2015-11-02 08:10:26 -0800141 s.startmem = s.entryNewValue0(ssa.OpInitMem, ssa.TypeMem)
Josh Bleecher Snyder85e03292015-07-30 11:03:05 -0700142 s.sp = s.entryNewValue0(ssa.OpSP, Types[TUINTPTR]) // TODO: use generic pointer type (unsafe.Pointer?) instead
143 s.sb = s.entryNewValue0(ssa.OpSB, Types[TUINTPTR])
Keith Randall8c46aa52015-06-19 21:02:28 -0700144
David Chase956f3192015-09-11 16:40:05 -0400145 s.startBlock(s.f.Entry)
146 s.vars[&memVar] = s.startmem
147
Todd Neald076ef72015-10-15 20:25:32 -0500148 s.varsyms = map[*Node]interface{}{}
149
Keith Randall8c46aa52015-06-19 21:02:28 -0700150 // Generate addresses of local declarations
151 s.decladdrs = map[*Node]*ssa.Value{}
Keith Randall4fffd4562016-02-29 13:31:48 -0800152 for _, n := range fn.Func.Dcl {
Keith Randall8c46aa52015-06-19 21:02:28 -0700153 switch n.Class {
Keith Randall6a8a9da2016-02-27 17:49:31 -0800154 case PPARAM, PPARAMOUT:
Todd Neald076ef72015-10-15 20:25:32 -0500155 aux := s.lookupSymbol(n, &ssa.ArgSymbol{Typ: n.Type, Node: n})
Keith Randall8c46aa52015-06-19 21:02:28 -0700156 s.decladdrs[n] = s.entryNewValue1A(ssa.OpAddr, Ptrto(n.Type), aux, s.sp)
Keith Randall6a8a9da2016-02-27 17:49:31 -0800157 if n.Class == PPARAMOUT && s.canSSA(n) {
158 // Save ssa-able PPARAMOUT variables so we can
159 // store them back to the stack at the end of
160 // the function.
161 s.returns = append(s.returns, n)
162 }
David Chase956f3192015-09-11 16:40:05 -0400163 case PAUTO | PHEAP:
164 // TODO this looks wrong for PAUTO|PHEAP, no vardef, but also no definition
Todd Neald076ef72015-10-15 20:25:32 -0500165 aux := s.lookupSymbol(n, &ssa.AutoSymbol{Typ: n.Type, Node: n})
David Chase956f3192015-09-11 16:40:05 -0400166 s.decladdrs[n] = s.entryNewValue1A(ssa.OpAddr, Ptrto(n.Type), aux, s.sp)
David Chase8824dcc2015-10-08 12:39:56 -0400167 case PPARAM | PHEAP, PPARAMOUT | PHEAP:
168 // This ends up wrong, have to do it at the PARAM node instead.
Keith Randall6a8a9da2016-02-27 17:49:31 -0800169 case PAUTO:
Keith Randalld2107fc2015-08-24 02:16:19 -0700170 // processed at each use, to prevent Addr coming
171 // before the decl.
Keith Randallc3eb1a72015-09-06 13:42:26 -0700172 case PFUNC:
173 // local function - already handled by frontend
Daniel Morsingbe2a3e22015-07-01 20:37:25 +0100174 default:
175 str := ""
176 if n.Class&PHEAP != 0 {
177 str = ",heap"
178 }
Josh Bleecher Snyder58446032015-08-23 20:29:43 -0700179 s.Unimplementedf("local variable with class %s%s unimplemented", classnames[n.Class&^PHEAP], str)
Keith Randall8c46aa52015-06-19 21:02:28 -0700180 }
181 }
Keith Randalld2fd43a2015-04-15 15:51:25 -0700182
183 // Convert the AST-based IR to the SSA-based IR
Keith Randall4fffd4562016-02-29 13:31:48 -0800184 s.stmts(fn.Func.Enter)
Keith Randall9d854fd2016-03-01 12:50:17 -0800185 s.stmts(fn.Nbody)
Keith Randalld2fd43a2015-04-15 15:51:25 -0700186
Keith Randallcfc2aa52015-05-18 16:44:20 -0700187 // fallthrough to exit
Keith Randalla7cfc7592015-09-08 16:04:37 -0700188 if s.curBlock != nil {
Keith Randallddc6b642016-03-09 19:27:57 -0800189 s.pushLine(fn.Func.Endlineno)
190 s.exit()
191 s.popLine()
Keith Randallcfc2aa52015-05-18 16:44:20 -0700192 }
193
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700194 // Check that we used all labels
195 for name, lab := range s.labels {
196 if !lab.used() && !lab.reported {
Robert Griesemerb83f3972016-03-02 11:01:25 -0800197 yyerrorl(lab.defNode.Lineno, "label %v defined and not used", name)
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700198 lab.reported = true
199 }
200 if lab.used() && !lab.defined() && !lab.reported {
Robert Griesemerb83f3972016-03-02 11:01:25 -0800201 yyerrorl(lab.useNode.Lineno, "label %v not defined", name)
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700202 lab.reported = true
203 }
204 }
205
206 // Check any forward gotos. Non-forward gotos have already been checked.
207 for _, n := range s.fwdGotos {
208 lab := s.labels[n.Left.Sym.Name]
209 // If the label is undefined, we have already have printed an error.
210 if lab.defined() {
211 s.checkgoto(n, lab.defNode)
212 }
213 }
214
215 if nerrors > 0 {
Keith Randall4c5459d2016-01-28 16:11:56 -0800216 s.f.Free()
Keith Randall5b355a72015-12-11 20:41:52 -0800217 return nil
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700218 }
219
Keith Randalld2fd43a2015-04-15 15:51:25 -0700220 // Link up variable uses to variable definitions
221 s.linkForwardReferences()
222
David Chase8824dcc2015-10-08 12:39:56 -0400223 // Don't carry reference this around longer than necessary
Keith Randall4fffd4562016-02-29 13:31:48 -0800224 s.exitCode = Nodes{}
David Chase8824dcc2015-10-08 12:39:56 -0400225
Josh Bleecher Snyder983bc8d2015-07-17 16:47:43 +0000226 // Main call to ssa package to compile function
227 ssa.Compile(s.f)
228
Keith Randall5b355a72015-12-11 20:41:52 -0800229 return s.f
Keith Randalld2fd43a2015-04-15 15:51:25 -0700230}
231
Keith Randallcfc2aa52015-05-18 16:44:20 -0700232type state struct {
Keith Randalld2fd43a2015-04-15 15:51:25 -0700233 // configuration (arch) information
234 config *ssa.Config
235
236 // function we're building
237 f *ssa.Func
238
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700239 // labels and labeled control flow nodes (OFOR, OSWITCH, OSELECT) in f
240 labels map[string]*ssaLabel
241 labeledNodes map[*Node]*ssaLabel
242
243 // gotos that jump forward; required for deferred checkgoto calls
244 fwdGotos []*Node
David Chase8824dcc2015-10-08 12:39:56 -0400245 // Code that must precede any return
246 // (e.g., copying value of heap-escaped paramout back to true paramout)
Keith Randall4fffd4562016-02-29 13:31:48 -0800247 exitCode Nodes
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700248
249 // unlabeled break and continue statement tracking
250 breakTo *ssa.Block // current target for plain break statement
251 continueTo *ssa.Block // current target for plain continue statement
Keith Randalld2fd43a2015-04-15 15:51:25 -0700252
253 // current location where we're interpreting the AST
254 curBlock *ssa.Block
255
Keith Randall8c46aa52015-06-19 21:02:28 -0700256 // variable assignments in the current block (map from variable symbol to ssa value)
257 // *Node is the unique identifier (an ONAME Node) for the variable.
258 vars map[*Node]*ssa.Value
Keith Randalld2fd43a2015-04-15 15:51:25 -0700259
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +0000260 // all defined variables at the end of each block. Indexed by block ID.
Keith Randall8c46aa52015-06-19 21:02:28 -0700261 defvars []map[*Node]*ssa.Value
Keith Randalld2fd43a2015-04-15 15:51:25 -0700262
Keith Randalld2107fc2015-08-24 02:16:19 -0700263 // addresses of PPARAM and PPARAMOUT variables.
Keith Randall8c46aa52015-06-19 21:02:28 -0700264 decladdrs map[*Node]*ssa.Value
Keith Randallcfc2aa52015-05-18 16:44:20 -0700265
Todd Neald076ef72015-10-15 20:25:32 -0500266 // symbols for PEXTERN, PAUTO and PPARAMOUT variables so they can be reused.
267 varsyms map[*Node]interface{}
268
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +0000269 // starting values. Memory, stack pointer, and globals pointer
Keith Randallcfc2aa52015-05-18 16:44:20 -0700270 startmem *ssa.Value
Keith Randallcfc2aa52015-05-18 16:44:20 -0700271 sp *ssa.Value
Keith Randall8c46aa52015-06-19 21:02:28 -0700272 sb *ssa.Value
Michael Matloob81ccf502015-05-30 01:03:06 -0400273
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +0000274 // line number stack. The current line number is top of stack
Michael Matloob81ccf502015-05-30 01:03:06 -0400275 line []int32
Keith Randall74e568f2015-11-09 21:35:40 -0800276
277 // list of panic calls by function name and line number.
278 // Used to deduplicate panic calls.
279 panics map[funcLine]*ssa.Block
Keith Randallb5c5efd2016-01-14 16:02:23 -0800280
281 // list of FwdRef values.
282 fwdRefs []*ssa.Value
Keith Randall6a8a9da2016-02-27 17:49:31 -0800283
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +0000284 // list of PPARAMOUT (return) variables. Does not include PPARAM|PHEAP vars.
Keith Randall6a8a9da2016-02-27 17:49:31 -0800285 returns []*Node
286
287 cgoUnsafeArgs bool
Keith Randall15ed37d2016-03-16 21:51:17 -0700288 noWB bool
289 WBLineno int32 // line number of first write barrier. 0=no write barriers
Keith Randall74e568f2015-11-09 21:35:40 -0800290}
291
292type funcLine struct {
293 f *Node
294 line int32
Keith Randalld2fd43a2015-04-15 15:51:25 -0700295}
296
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700297type ssaLabel struct {
298 target *ssa.Block // block identified by this label
299 breakTarget *ssa.Block // block to break to in control flow node identified by this label
300 continueTarget *ssa.Block // block to continue to in control flow node identified by this label
301 defNode *Node // label definition Node (OLABEL)
302 // Label use Node (OGOTO, OBREAK, OCONTINUE).
303 // Used only for error detection and reporting.
304 // There might be multiple uses, but we only need to track one.
305 useNode *Node
306 reported bool // reported indicates whether an error has already been reported for this label
307}
308
309// defined reports whether the label has a definition (OLABEL node).
310func (l *ssaLabel) defined() bool { return l.defNode != nil }
311
312// used reports whether the label has a use (OGOTO, OBREAK, or OCONTINUE node).
313func (l *ssaLabel) used() bool { return l.useNode != nil }
314
315// label returns the label associated with sym, creating it if necessary.
316func (s *state) label(sym *Sym) *ssaLabel {
317 lab := s.labels[sym.Name]
318 if lab == nil {
319 lab = new(ssaLabel)
320 s.labels[sym.Name] = lab
321 }
322 return lab
323}
324
Keith Randallda8af472016-01-13 11:14:57 -0800325func (s *state) Logf(msg string, args ...interface{}) { s.config.Logf(msg, args...) }
David Chase88b230e2016-01-29 14:44:15 -0500326func (s *state) Log() bool { return s.config.Log() }
Keith Randallda8af472016-01-13 11:14:57 -0800327func (s *state) Fatalf(msg string, args ...interface{}) { s.config.Fatalf(s.peekLine(), msg, args...) }
328func (s *state) Unimplementedf(msg string, args ...interface{}) {
329 s.config.Unimplementedf(s.peekLine(), msg, args...)
330}
Todd Neal98b88de2016-03-13 23:04:31 -0500331func (s *state) Warnl(line int32, msg string, args ...interface{}) { s.config.Warnl(line, msg, args...) }
332func (s *state) Debug_checknil() bool { return s.config.Debug_checknil() }
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -0700333
Keith Randall269baa92015-09-17 10:31:16 -0700334var (
335 // dummy node for the memory variable
Keith Randallc24681a2015-10-22 14:22:38 -0700336 memVar = Node{Op: ONAME, Class: Pxxx, Sym: &Sym{Name: "mem"}}
Keith Randall8c46aa52015-06-19 21:02:28 -0700337
Keith Randall269baa92015-09-17 10:31:16 -0700338 // dummy nodes for temporary variables
Keith Randallc24681a2015-10-22 14:22:38 -0700339 ptrVar = Node{Op: ONAME, Class: Pxxx, Sym: &Sym{Name: "ptr"}}
340 capVar = Node{Op: ONAME, Class: Pxxx, Sym: &Sym{Name: "cap"}}
341 typVar = Node{Op: ONAME, Class: Pxxx, Sym: &Sym{Name: "typ"}}
342 idataVar = Node{Op: ONAME, Class: Pxxx, Sym: &Sym{Name: "idata"}}
343 okVar = Node{Op: ONAME, Class: Pxxx, Sym: &Sym{Name: "ok"}}
Keith Randall69a7c1522016-03-21 15:24:08 -0700344 deltaVar = Node{Op: ONAME, Class: Pxxx, Sym: &Sym{Name: "delta"}}
Keith Randall269baa92015-09-17 10:31:16 -0700345)
Keith Randall5505e8c2015-09-12 23:27:26 -0700346
Keith Randalld2fd43a2015-04-15 15:51:25 -0700347// startBlock sets the current block we're generating code in to b.
Keith Randallcfc2aa52015-05-18 16:44:20 -0700348func (s *state) startBlock(b *ssa.Block) {
349 if s.curBlock != nil {
Josh Bleecher Snyder37ddc272015-06-24 14:03:39 -0700350 s.Fatalf("starting block %v when block %v has not ended", b, s.curBlock)
Keith Randallcfc2aa52015-05-18 16:44:20 -0700351 }
Keith Randalld2fd43a2015-04-15 15:51:25 -0700352 s.curBlock = b
Keith Randall8c46aa52015-06-19 21:02:28 -0700353 s.vars = map[*Node]*ssa.Value{}
Keith Randalld2fd43a2015-04-15 15:51:25 -0700354}
355
356// endBlock marks the end of generating code for the current block.
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +0000357// Returns the (former) current block. Returns nil if there is no current
Keith Randalld2fd43a2015-04-15 15:51:25 -0700358// block, i.e. if no code flows to the current execution point.
Keith Randallcfc2aa52015-05-18 16:44:20 -0700359func (s *state) endBlock() *ssa.Block {
Keith Randalld2fd43a2015-04-15 15:51:25 -0700360 b := s.curBlock
361 if b == nil {
362 return nil
363 }
364 for len(s.defvars) <= int(b.ID) {
365 s.defvars = append(s.defvars, nil)
366 }
367 s.defvars[b.ID] = s.vars
368 s.curBlock = nil
369 s.vars = nil
Michael Matloob81ccf502015-05-30 01:03:06 -0400370 b.Line = s.peekLine()
Keith Randalld2fd43a2015-04-15 15:51:25 -0700371 return b
372}
373
Michael Matloob81ccf502015-05-30 01:03:06 -0400374// pushLine pushes a line number on the line number stack.
375func (s *state) pushLine(line int32) {
376 s.line = append(s.line, line)
377}
378
379// popLine pops the top of the line number stack.
380func (s *state) popLine() {
381 s.line = s.line[:len(s.line)-1]
382}
383
384// peekLine peek the top of the line number stack.
385func (s *state) peekLine() int32 {
386 return s.line[len(s.line)-1]
387}
388
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700389func (s *state) Error(msg string, args ...interface{}) {
Robert Griesemerb83f3972016-03-02 11:01:25 -0800390 yyerrorl(s.peekLine(), msg, args...)
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700391}
392
Keith Randall8f22b522015-06-11 21:29:25 -0700393// newValue0 adds a new value with no arguments to the current block.
394func (s *state) newValue0(op ssa.Op, t ssa.Type) *ssa.Value {
395 return s.curBlock.NewValue0(s.peekLine(), op, t)
396}
397
398// newValue0A adds a new value with no arguments and an aux value to the current block.
399func (s *state) newValue0A(op ssa.Op, t ssa.Type, aux interface{}) *ssa.Value {
400 return s.curBlock.NewValue0A(s.peekLine(), op, t, aux)
Michael Matloob81ccf502015-05-30 01:03:06 -0400401}
402
Todd Neal991036a2015-09-03 18:24:22 -0500403// newValue0I adds a new value with no arguments and an auxint value to the current block.
404func (s *state) newValue0I(op ssa.Op, t ssa.Type, auxint int64) *ssa.Value {
405 return s.curBlock.NewValue0I(s.peekLine(), op, t, auxint)
406}
407
Michael Matloob81ccf502015-05-30 01:03:06 -0400408// newValue1 adds a new value with one argument to the current block.
Keith Randall8f22b522015-06-11 21:29:25 -0700409func (s *state) newValue1(op ssa.Op, t ssa.Type, arg *ssa.Value) *ssa.Value {
410 return s.curBlock.NewValue1(s.peekLine(), op, t, arg)
411}
412
413// newValue1A adds a new value with one argument and an aux value to the current block.
414func (s *state) newValue1A(op ssa.Op, t ssa.Type, aux interface{}, arg *ssa.Value) *ssa.Value {
415 return s.curBlock.NewValue1A(s.peekLine(), op, t, aux, arg)
Michael Matloob81ccf502015-05-30 01:03:06 -0400416}
417
Keith Randallcd7e0592015-07-15 21:33:49 -0700418// newValue1I adds a new value with one argument and an auxint value to the current block.
419func (s *state) newValue1I(op ssa.Op, t ssa.Type, aux int64, arg *ssa.Value) *ssa.Value {
420 return s.curBlock.NewValue1I(s.peekLine(), op, t, aux, arg)
421}
422
Michael Matloob81ccf502015-05-30 01:03:06 -0400423// newValue2 adds a new value with two arguments to the current block.
Keith Randall8f22b522015-06-11 21:29:25 -0700424func (s *state) newValue2(op ssa.Op, t ssa.Type, arg0, arg1 *ssa.Value) *ssa.Value {
425 return s.curBlock.NewValue2(s.peekLine(), op, t, arg0, arg1)
Michael Matloob81ccf502015-05-30 01:03:06 -0400426}
427
Daniel Morsing66b47812015-06-27 15:45:20 +0100428// newValue2I adds a new value with two arguments and an auxint value to the current block.
429func (s *state) newValue2I(op ssa.Op, t ssa.Type, aux int64, arg0, arg1 *ssa.Value) *ssa.Value {
430 return s.curBlock.NewValue2I(s.peekLine(), op, t, aux, arg0, arg1)
431}
432
Michael Matloob81ccf502015-05-30 01:03:06 -0400433// newValue3 adds a new value with three arguments to the current block.
Keith Randall8f22b522015-06-11 21:29:25 -0700434func (s *state) newValue3(op ssa.Op, t ssa.Type, arg0, arg1, arg2 *ssa.Value) *ssa.Value {
435 return s.curBlock.NewValue3(s.peekLine(), op, t, arg0, arg1, arg2)
Michael Matloob81ccf502015-05-30 01:03:06 -0400436}
437
Keith Randalld4cc51d2015-08-14 21:47:20 -0700438// newValue3I adds a new value with three arguments and an auxint value to the current block.
439func (s *state) newValue3I(op ssa.Op, t ssa.Type, aux int64, arg0, arg1, arg2 *ssa.Value) *ssa.Value {
440 return s.curBlock.NewValue3I(s.peekLine(), op, t, aux, arg0, arg1, arg2)
441}
442
Todd Neal991036a2015-09-03 18:24:22 -0500443// entryNewValue0 adds a new value with no arguments to the entry block.
Keith Randall8f22b522015-06-11 21:29:25 -0700444func (s *state) entryNewValue0(op ssa.Op, t ssa.Type) *ssa.Value {
445 return s.f.Entry.NewValue0(s.peekLine(), op, t)
446}
447
Todd Neal991036a2015-09-03 18:24:22 -0500448// entryNewValue0A adds a new value with no arguments and an aux value to the entry block.
Keith Randall8f22b522015-06-11 21:29:25 -0700449func (s *state) entryNewValue0A(op ssa.Op, t ssa.Type, aux interface{}) *ssa.Value {
450 return s.f.Entry.NewValue0A(s.peekLine(), op, t, aux)
Michael Matloob81ccf502015-05-30 01:03:06 -0400451}
452
Todd Neal991036a2015-09-03 18:24:22 -0500453// entryNewValue0I adds a new value with no arguments and an auxint value to the entry block.
454func (s *state) entryNewValue0I(op ssa.Op, t ssa.Type, auxint int64) *ssa.Value {
455 return s.f.Entry.NewValue0I(s.peekLine(), op, t, auxint)
456}
457
Michael Matloob81ccf502015-05-30 01:03:06 -0400458// entryNewValue1 adds a new value with one argument to the entry block.
Keith Randall8f22b522015-06-11 21:29:25 -0700459func (s *state) entryNewValue1(op ssa.Op, t ssa.Type, arg *ssa.Value) *ssa.Value {
460 return s.f.Entry.NewValue1(s.peekLine(), op, t, arg)
461}
462
463// entryNewValue1 adds a new value with one argument and an auxint value to the entry block.
464func (s *state) entryNewValue1I(op ssa.Op, t ssa.Type, auxint int64, arg *ssa.Value) *ssa.Value {
465 return s.f.Entry.NewValue1I(s.peekLine(), op, t, auxint, arg)
Michael Matloob81ccf502015-05-30 01:03:06 -0400466}
467
Keith Randall8c46aa52015-06-19 21:02:28 -0700468// entryNewValue1A adds a new value with one argument and an aux value to the entry block.
469func (s *state) entryNewValue1A(op ssa.Op, t ssa.Type, aux interface{}, arg *ssa.Value) *ssa.Value {
470 return s.f.Entry.NewValue1A(s.peekLine(), op, t, aux, arg)
471}
472
Michael Matloob81ccf502015-05-30 01:03:06 -0400473// entryNewValue2 adds a new value with two arguments to the entry block.
Keith Randall8f22b522015-06-11 21:29:25 -0700474func (s *state) entryNewValue2(op ssa.Op, t ssa.Type, arg0, arg1 *ssa.Value) *ssa.Value {
475 return s.f.Entry.NewValue2(s.peekLine(), op, t, arg0, arg1)
Michael Matloob81ccf502015-05-30 01:03:06 -0400476}
477
Josh Bleecher Snydercea44142015-09-08 16:52:25 -0700478// const* routines add a new const value to the entry block.
Josh Bleecher Snyder39214272016-03-06 18:06:09 -0800479func (s *state) constSlice(t ssa.Type) *ssa.Value { return s.f.ConstSlice(s.peekLine(), t) }
480func (s *state) constInterface(t ssa.Type) *ssa.Value { return s.f.ConstInterface(s.peekLine(), t) }
481func (s *state) constNil(t ssa.Type) *ssa.Value { return s.f.ConstNil(s.peekLine(), t) }
482func (s *state) constEmptyString(t ssa.Type) *ssa.Value { return s.f.ConstEmptyString(s.peekLine(), t) }
Josh Bleecher Snydercea44142015-09-08 16:52:25 -0700483func (s *state) constBool(c bool) *ssa.Value {
484 return s.f.ConstBool(s.peekLine(), Types[TBOOL], c)
485}
Keith Randall9cb332e2015-07-28 14:19:20 -0700486func (s *state) constInt8(t ssa.Type, c int8) *ssa.Value {
487 return s.f.ConstInt8(s.peekLine(), t, c)
488}
489func (s *state) constInt16(t ssa.Type, c int16) *ssa.Value {
490 return s.f.ConstInt16(s.peekLine(), t, c)
491}
492func (s *state) constInt32(t ssa.Type, c int32) *ssa.Value {
493 return s.f.ConstInt32(s.peekLine(), t, c)
494}
495func (s *state) constInt64(t ssa.Type, c int64) *ssa.Value {
496 return s.f.ConstInt64(s.peekLine(), t, c)
497}
David Chase997a9f32015-08-12 16:38:11 -0400498func (s *state) constFloat32(t ssa.Type, c float64) *ssa.Value {
499 return s.f.ConstFloat32(s.peekLine(), t, c)
500}
501func (s *state) constFloat64(t ssa.Type, c float64) *ssa.Value {
502 return s.f.ConstFloat64(s.peekLine(), t, c)
503}
Michael Matloob81ccf502015-05-30 01:03:06 -0400504func (s *state) constInt(t ssa.Type, c int64) *ssa.Value {
Keith Randall9cb332e2015-07-28 14:19:20 -0700505 if s.config.IntSize == 8 {
506 return s.constInt64(t, c)
507 }
508 if int64(int32(c)) != c {
509 s.Fatalf("integer constant too big %d", c)
510 }
511 return s.constInt32(t, int32(c))
Michael Matloob81ccf502015-05-30 01:03:06 -0400512}
513
Keith Randall4fffd4562016-02-29 13:31:48 -0800514func (s *state) stmts(a Nodes) {
515 for _, x := range a.Slice() {
516 s.stmt(x)
517 }
518}
519
Keith Randalld2fd43a2015-04-15 15:51:25 -0700520// ssaStmtList converts the statement n to SSA and adds it to s.
Ian Lance Taylorc4012b62016-03-08 10:26:20 -0800521func (s *state) stmtList(l Nodes) {
Ian Lance Taylor38921b32016-03-08 15:10:26 -0800522 for _, n := range l.Slice() {
523 s.stmt(n)
Keith Randalld2fd43a2015-04-15 15:51:25 -0700524 }
525}
526
527// ssaStmt converts the statement n to SSA and adds it to s.
Keith Randallcfc2aa52015-05-18 16:44:20 -0700528func (s *state) stmt(n *Node) {
Michael Matloob81ccf502015-05-30 01:03:06 -0400529 s.pushLine(n.Lineno)
530 defer s.popLine()
531
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700532 // If s.curBlock is nil, then we're about to generate dead code.
533 // We can't just short-circuit here, though,
534 // because we check labels and gotos as part of SSA generation.
535 // Provide a block for the dead code so that we don't have
536 // to add special cases everywhere else.
537 if s.curBlock == nil {
538 dead := s.f.NewBlock(ssa.BlockPlain)
539 s.startBlock(dead)
540 }
541
Keith Randalld2fd43a2015-04-15 15:51:25 -0700542 s.stmtList(n.Ninit)
543 switch n.Op {
544
545 case OBLOCK:
546 s.stmtList(n.List)
547
Josh Bleecher Snyder2574e4a2015-07-16 13:25:36 -0600548 // No-ops
Todd Neal67e43c12015-08-28 21:19:40 -0500549 case OEMPTY, ODCLCONST, ODCLTYPE, OFALL:
Brad Fitzpatrick7af53d92015-07-10 10:47:28 -0600550
Josh Bleecher Snyder2574e4a2015-07-16 13:25:36 -0600551 // Expression statements
552 case OCALLFUNC, OCALLMETH, OCALLINTER:
Keith Randalld24768e2015-09-09 23:56:59 -0700553 s.call(n, callNormal)
Keith Randallfb54e032016-02-24 16:19:20 -0800554 if n.Op == OCALLFUNC && n.Left.Op == ONAME && n.Left.Class == PFUNC &&
555 (compiling_runtime != 0 && n.Left.Sym.Name == "throw" ||
Keith Randall6a8a9da2016-02-27 17:49:31 -0800556 n.Left.Sym.Pkg == Runtimepkg && (n.Left.Sym.Name == "gopanic" || n.Left.Sym.Name == "selectgo" || n.Left.Sym.Name == "block")) {
Keith Randallfaf1bdb2016-02-06 22:35:34 -0800557 m := s.mem()
558 b := s.endBlock()
559 b.Kind = ssa.BlockExit
Keith Randall56e0ecc2016-03-15 20:45:50 -0700560 b.SetControl(m)
Keith Randallfaf1bdb2016-02-06 22:35:34 -0800561 // TODO: never rewrite OPANIC to OCALLFUNC in the
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +0000562 // first place. Need to wait until all backends
Keith Randallfaf1bdb2016-02-06 22:35:34 -0800563 // go through SSA.
564 }
Keith Randalld24768e2015-09-09 23:56:59 -0700565 case ODEFER:
566 s.call(n.Left, callDefer)
567 case OPROC:
568 s.call(n.Left, callGo)
Josh Bleecher Snyder2574e4a2015-07-16 13:25:36 -0600569
Keith Randall269baa92015-09-17 10:31:16 -0700570 case OAS2DOTTYPE:
Ian Lance Taylor38921b32016-03-08 15:10:26 -0800571 res, resok := s.dottype(n.Rlist.First(), true)
Keith Randalld4663e12016-03-21 10:22:03 -0700572 s.assign(n.List.First(), res, needwritebarrier(n.List.First(), n.Rlist.First()), false, n.Lineno, 0)
573 s.assign(n.List.Second(), resok, false, false, n.Lineno, 0)
Keith Randall269baa92015-09-17 10:31:16 -0700574 return
575
Keith Randalld2fd43a2015-04-15 15:51:25 -0700576 case ODCL:
Daniel Morsingc31b6dd2015-06-12 14:23:29 +0100577 if n.Left.Class&PHEAP == 0 {
578 return
579 }
580 if compiling_runtime != 0 {
Keith Randall0ec72b62015-09-08 15:42:53 -0700581 Fatalf("%v escapes to heap, not allowed in runtime.", n)
Daniel Morsingc31b6dd2015-06-12 14:23:29 +0100582 }
583
584 // TODO: the old pass hides the details of PHEAP
585 // variables behind ONAME nodes. Figure out if it's better
586 // to rewrite the tree and make the heapaddr construct explicit
587 // or to keep this detail hidden behind the scenes.
588 palloc := prealloc[n.Left]
589 if palloc == nil {
590 palloc = callnew(n.Left.Type)
591 prealloc[n.Left] = palloc
592 }
Josh Bleecher Snyder07269312015-08-29 14:54:45 -0700593 r := s.expr(palloc)
Keith Randalld4663e12016-03-21 10:22:03 -0700594 s.assign(n.Left.Name.Heapaddr, r, false, false, n.Lineno, 0)
Keith Randalld2fd43a2015-04-15 15:51:25 -0700595
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700596 case OLABEL:
597 sym := n.Left.Sym
598
599 if isblanksym(sym) {
Keith Randall7e4c06d2015-07-12 11:52:09 -0700600 // Empty identifier is valid but useless.
601 // See issues 11589, 11593.
602 return
603 }
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700604
605 lab := s.label(sym)
606
607 // Associate label with its control flow node, if any
608 if ctl := n.Name.Defn; ctl != nil {
609 switch ctl.Op {
610 case OFOR, OSWITCH, OSELECT:
611 s.labeledNodes[ctl] = lab
612 }
Keith Randall0ad9c8c2015-06-12 16:24:33 -0700613 }
Keith Randalld2fd43a2015-04-15 15:51:25 -0700614
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700615 if !lab.defined() {
616 lab.defNode = n
617 } else {
Robert Griesemer2faf5bc2016-03-02 11:30:29 -0800618 s.Error("label %v already defined at %v", sym, linestr(lab.defNode.Lineno))
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700619 lab.reported = true
Keith Randalld2fd43a2015-04-15 15:51:25 -0700620 }
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700621 // The label might already have a target block via a goto.
622 if lab.target == nil {
623 lab.target = s.f.NewBlock(ssa.BlockPlain)
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -0700624 }
Keith Randalld2fd43a2015-04-15 15:51:25 -0700625
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700626 // go to that label (we pretend "label:" is preceded by "goto label")
627 b := s.endBlock()
Todd Neal47d67992015-08-28 21:36:29 -0500628 b.AddEdgeTo(lab.target)
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700629 s.startBlock(lab.target)
630
631 case OGOTO:
632 sym := n.Left.Sym
633
634 lab := s.label(sym)
635 if lab.target == nil {
636 lab.target = s.f.NewBlock(ssa.BlockPlain)
637 }
638 if !lab.used() {
639 lab.useNode = n
640 }
641
642 if lab.defined() {
643 s.checkgoto(n, lab.defNode)
644 } else {
645 s.fwdGotos = append(s.fwdGotos, n)
646 }
647
648 b := s.endBlock()
Todd Neal47d67992015-08-28 21:36:29 -0500649 b.AddEdgeTo(lab.target)
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700650
Keith Randall290d8fc2015-06-10 15:03:06 -0700651 case OAS, OASWB:
Josh Bleecher Snyder6b416652015-07-28 10:56:39 -0700652 // Check whether we can generate static data rather than code.
653 // If so, ignore n and defer data generation until codegen.
654 // Failure to do this causes writes to readonly symbols.
655 if gen_as_init(n, true) {
656 var data []*Node
657 if s.f.StaticData != nil {
658 data = s.f.StaticData.([]*Node)
659 }
660 s.f.StaticData = append(data, n)
661 return
662 }
Keith Randall5ba31942016-01-25 17:06:54 -0800663
Keith Randall309144b2016-04-01 11:05:30 -0700664 if n.Left == n.Right && n.Left.Op == ONAME {
665 // An x=x assignment. No point in doing anything
666 // here. In addition, skipping this assignment
667 // prevents generating:
668 // VARDEF x
669 // COPY x -> x
670 // which is bad because x is incorrectly considered
671 // dead before the vardef. See issue #14904.
672 return
673 }
674
Keith Randall5ba31942016-01-25 17:06:54 -0800675 var t *Type
Josh Bleecher Snyder07269312015-08-29 14:54:45 -0700676 if n.Right != nil {
Keith Randall5ba31942016-01-25 17:06:54 -0800677 t = n.Right.Type
678 } else {
679 t = n.Left.Type
680 }
681
682 // Evaluate RHS.
683 rhs := n.Right
684 if rhs != nil && (rhs.Op == OSTRUCTLIT || rhs.Op == OARRAYLIT) {
685 // All literals with nonzero fields have already been
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +0000686 // rewritten during walk. Any that remain are just T{}
687 // or equivalents. Use the zero value.
Keith Randall5ba31942016-01-25 17:06:54 -0800688 if !iszero(rhs) {
689 Fatalf("literal with nonzero value in SSA: %v", rhs)
690 }
691 rhs = nil
692 }
693 var r *ssa.Value
694 needwb := n.Op == OASWB && rhs != nil
695 deref := !canSSAType(t)
696 if deref {
697 if rhs == nil {
698 r = nil // Signal assign to use OpZero.
Keith Randalld3886902015-09-18 22:12:38 -0700699 } else {
Keith Randall5ba31942016-01-25 17:06:54 -0800700 r = s.addr(rhs, false)
701 }
702 } else {
703 if rhs == nil {
704 r = s.zeroVal(t)
705 } else {
706 r = s.expr(rhs)
Keith Randalld3886902015-09-18 22:12:38 -0700707 }
Josh Bleecher Snyder07269312015-08-29 14:54:45 -0700708 }
Keith Randall5ba31942016-01-25 17:06:54 -0800709 if rhs != nil && rhs.Op == OAPPEND {
Keith Randall9d22c102015-09-11 11:02:57 -0700710 // Yuck! The frontend gets rid of the write barrier, but we need it!
711 // At least, we need it in the case where growslice is called.
712 // TODO: Do the write barrier on just the growslice branch.
713 // TODO: just add a ptr graying to the end of growslice?
714 // TODO: check whether we need to do this for ODOTTYPE and ORECV also.
715 // They get similar wb-removal treatment in walk.go:OAS.
Keith Randall5ba31942016-01-25 17:06:54 -0800716 needwb = true
Keith Randall9d22c102015-09-11 11:02:57 -0700717 }
Keith Randall5ba31942016-01-25 17:06:54 -0800718
Keith Randalld4663e12016-03-21 10:22:03 -0700719 var skip skipMask
720 if rhs != nil && (rhs.Op == OSLICE || rhs.Op == OSLICE3 || rhs.Op == OSLICESTR) && samesafeexpr(rhs.Left, n.Left) {
721 // We're assigning a slicing operation back to its source.
722 // Don't write back fields we aren't changing. See issue #14855.
723 i := rhs.Right.Left
724 var j, k *Node
725 if rhs.Op == OSLICE3 {
726 j = rhs.Right.Right.Left
727 k = rhs.Right.Right.Right
728 } else {
729 j = rhs.Right.Right
730 }
Josh Bleecher Snyder5cab0162016-04-01 14:51:02 -0700731 if i != nil && (i.Op == OLITERAL && i.Val().Ctype() == CTINT && i.Int64() == 0) {
Keith Randalld4663e12016-03-21 10:22:03 -0700732 // [0:...] is the same as [:...]
733 i = nil
734 }
735 // TODO: detect defaults for len/cap also.
736 // Currently doesn't really work because (*p)[:len(*p)] appears here as:
737 // tmp = len(*p)
738 // (*p)[:tmp]
739 //if j != nil && (j.Op == OLEN && samesafeexpr(j.Left, n.Left)) {
740 // j = nil
741 //}
742 //if k != nil && (k.Op == OCAP && samesafeexpr(k.Left, n.Left)) {
743 // k = nil
744 //}
745 if i == nil {
746 skip |= skipPtr
747 if j == nil {
748 skip |= skipLen
749 }
750 if k == nil {
751 skip |= skipCap
752 }
753 }
754 }
755
756 s.assign(n.Left, r, needwb, deref, n.Lineno, skip)
Daniel Morsingc31b6dd2015-06-12 14:23:29 +0100757
Keith Randalld2fd43a2015-04-15 15:51:25 -0700758 case OIF:
Keith Randalld2fd43a2015-04-15 15:51:25 -0700759 bThen := s.f.NewBlock(ssa.BlockPlain)
760 bEnd := s.f.NewBlock(ssa.BlockPlain)
761 var bElse *ssa.Block
Ian Lance Taylor38921b32016-03-08 15:10:26 -0800762 if n.Rlist.Len() != 0 {
Keith Randalld2fd43a2015-04-15 15:51:25 -0700763 bElse = s.f.NewBlock(ssa.BlockPlain)
Keith Randall99187312015-11-02 16:56:53 -0800764 s.condBranch(n.Left, bThen, bElse, n.Likely)
765 } else {
766 s.condBranch(n.Left, bThen, bEnd, n.Likely)
Keith Randalld2fd43a2015-04-15 15:51:25 -0700767 }
768
769 s.startBlock(bThen)
Keith Randall9d854fd2016-03-01 12:50:17 -0800770 s.stmts(n.Nbody)
Josh Bleecher Snydere0ac5c52015-07-20 18:42:45 -0700771 if b := s.endBlock(); b != nil {
Todd Neal47d67992015-08-28 21:36:29 -0500772 b.AddEdgeTo(bEnd)
Keith Randalld2fd43a2015-04-15 15:51:25 -0700773 }
774
Ian Lance Taylor38921b32016-03-08 15:10:26 -0800775 if n.Rlist.Len() != 0 {
Keith Randalld2fd43a2015-04-15 15:51:25 -0700776 s.startBlock(bElse)
Keith Randalle707fbe2015-06-11 10:20:39 -0700777 s.stmtList(n.Rlist)
Josh Bleecher Snydere0ac5c52015-07-20 18:42:45 -0700778 if b := s.endBlock(); b != nil {
Todd Neal47d67992015-08-28 21:36:29 -0500779 b.AddEdgeTo(bEnd)
Keith Randalld2fd43a2015-04-15 15:51:25 -0700780 }
781 }
782 s.startBlock(bEnd)
783
784 case ORETURN:
785 s.stmtList(n.List)
Keith Randall6a8a9da2016-02-27 17:49:31 -0800786 s.exit()
Keith Randall8a1f6212015-09-08 21:28:44 -0700787 case ORETJMP:
788 s.stmtList(n.List)
Keith Randall6a8a9da2016-02-27 17:49:31 -0800789 b := s.exit()
790 b.Kind = ssa.BlockRetJmp // override BlockRet
Keith Randall8a1f6212015-09-08 21:28:44 -0700791 b.Aux = n.Left.Sym
Keith Randalld2fd43a2015-04-15 15:51:25 -0700792
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700793 case OCONTINUE, OBREAK:
794 var op string
795 var to *ssa.Block
796 switch n.Op {
797 case OCONTINUE:
798 op = "continue"
799 to = s.continueTo
800 case OBREAK:
801 op = "break"
802 to = s.breakTo
803 }
804 if n.Left == nil {
805 // plain break/continue
806 if to == nil {
807 s.Error("%s is not in a loop", op)
808 return
809 }
810 // nothing to do; "to" is already the correct target
811 } else {
812 // labeled break/continue; look up the target
813 sym := n.Left.Sym
814 lab := s.label(sym)
815 if !lab.used() {
816 lab.useNode = n.Left
817 }
818 if !lab.defined() {
819 s.Error("%s label not defined: %v", op, sym)
820 lab.reported = true
821 return
822 }
823 switch n.Op {
824 case OCONTINUE:
825 to = lab.continueTarget
826 case OBREAK:
827 to = lab.breakTarget
828 }
829 if to == nil {
830 // Valid label but not usable with a break/continue here, e.g.:
831 // for {
832 // continue abc
833 // }
834 // abc:
835 // for {}
836 s.Error("invalid %s label %v", op, sym)
837 lab.reported = true
838 return
839 }
840 }
841
842 b := s.endBlock()
Todd Neal47d67992015-08-28 21:36:29 -0500843 b.AddEdgeTo(to)
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700844
Keith Randalld2fd43a2015-04-15 15:51:25 -0700845 case OFOR:
Josh Bleecher Snyder51738682015-07-06 15:29:39 -0700846 // OFOR: for Ninit; Left; Right { Nbody }
Keith Randalld2fd43a2015-04-15 15:51:25 -0700847 bCond := s.f.NewBlock(ssa.BlockPlain)
848 bBody := s.f.NewBlock(ssa.BlockPlain)
Josh Bleecher Snyder51738682015-07-06 15:29:39 -0700849 bIncr := s.f.NewBlock(ssa.BlockPlain)
Keith Randalld2fd43a2015-04-15 15:51:25 -0700850 bEnd := s.f.NewBlock(ssa.BlockPlain)
851
852 // first, jump to condition test
853 b := s.endBlock()
Todd Neal47d67992015-08-28 21:36:29 -0500854 b.AddEdgeTo(bCond)
Keith Randalld2fd43a2015-04-15 15:51:25 -0700855
856 // generate code to test condition
Keith Randalld2fd43a2015-04-15 15:51:25 -0700857 s.startBlock(bCond)
Josh Bleecher Snyder51738682015-07-06 15:29:39 -0700858 if n.Left != nil {
Keith Randall99187312015-11-02 16:56:53 -0800859 s.condBranch(n.Left, bBody, bEnd, 1)
Josh Bleecher Snyder51738682015-07-06 15:29:39 -0700860 } else {
Keith Randall99187312015-11-02 16:56:53 -0800861 b := s.endBlock()
862 b.Kind = ssa.BlockPlain
863 b.AddEdgeTo(bBody)
Josh Bleecher Snyder51738682015-07-06 15:29:39 -0700864 }
Keith Randalld2fd43a2015-04-15 15:51:25 -0700865
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700866 // set up for continue/break in body
867 prevContinue := s.continueTo
868 prevBreak := s.breakTo
869 s.continueTo = bIncr
870 s.breakTo = bEnd
871 lab := s.labeledNodes[n]
872 if lab != nil {
873 // labeled for loop
874 lab.continueTarget = bIncr
875 lab.breakTarget = bEnd
876 }
877
Keith Randalld2fd43a2015-04-15 15:51:25 -0700878 // generate body
879 s.startBlock(bBody)
Keith Randall9d854fd2016-03-01 12:50:17 -0800880 s.stmts(n.Nbody)
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700881
882 // tear down continue/break
883 s.continueTo = prevContinue
884 s.breakTo = prevBreak
885 if lab != nil {
886 lab.continueTarget = nil
887 lab.breakTarget = nil
888 }
889
890 // done with body, goto incr
Josh Bleecher Snyder51738682015-07-06 15:29:39 -0700891 if b := s.endBlock(); b != nil {
Todd Neal47d67992015-08-28 21:36:29 -0500892 b.AddEdgeTo(bIncr)
Josh Bleecher Snyder51738682015-07-06 15:29:39 -0700893 }
894
895 // generate incr
896 s.startBlock(bIncr)
Josh Bleecher Snyder46815b92015-06-24 17:48:22 -0700897 if n.Right != nil {
898 s.stmt(n.Right)
899 }
Josh Bleecher Snyder51738682015-07-06 15:29:39 -0700900 if b := s.endBlock(); b != nil {
Todd Neal47d67992015-08-28 21:36:29 -0500901 b.AddEdgeTo(bCond)
Josh Bleecher Snyder6c140592015-07-04 09:07:54 -0700902 }
Keith Randalld2fd43a2015-04-15 15:51:25 -0700903 s.startBlock(bEnd)
904
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700905 case OSWITCH, OSELECT:
906 // These have been mostly rewritten by the front end into their Nbody fields.
907 // Our main task is to correctly hook up any break statements.
908 bEnd := s.f.NewBlock(ssa.BlockPlain)
909
910 prevBreak := s.breakTo
911 s.breakTo = bEnd
912 lab := s.labeledNodes[n]
913 if lab != nil {
914 // labeled
915 lab.breakTarget = bEnd
916 }
917
918 // generate body code
Keith Randall9d854fd2016-03-01 12:50:17 -0800919 s.stmts(n.Nbody)
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700920
921 s.breakTo = prevBreak
922 if lab != nil {
923 lab.breakTarget = nil
924 }
925
Keith Randalleb0cff92016-02-09 12:28:02 -0800926 // OSWITCH never falls through (s.curBlock == nil here).
927 // OSELECT does not fall through if we're calling selectgo.
928 // OSELECT does fall through if we're calling selectnb{send,recv}[2].
929 // In those latter cases, go to the code after the select.
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700930 if b := s.endBlock(); b != nil {
Todd Neal47d67992015-08-28 21:36:29 -0500931 b.AddEdgeTo(bEnd)
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -0700932 }
933 s.startBlock(bEnd)
934
Keith Randalld2fd43a2015-04-15 15:51:25 -0700935 case OVARKILL:
Keith Randalld2107fc2015-08-24 02:16:19 -0700936 // Insert a varkill op to record that a variable is no longer live.
937 // We only care about liveness info at call sites, so putting the
938 // varkill in the store chain is enough to keep it correctly ordered
939 // with respect to call ops.
Keith Randall6a8a9da2016-02-27 17:49:31 -0800940 if !s.canSSA(n.Left) {
Keith Randalld29e92b2015-09-19 12:01:39 -0700941 s.vars[&memVar] = s.newValue1A(ssa.OpVarKill, ssa.TypeMem, n.Left, s.mem())
942 }
Keith Randall9569b952015-08-28 22:51:01 -0700943
Keith Randall23d58102016-01-19 09:59:21 -0800944 case OVARLIVE:
945 // Insert a varlive op to record that a variable is still live.
946 if !n.Left.Addrtaken {
947 s.Fatalf("VARLIVE variable %s must have Addrtaken set", n.Left)
948 }
949 s.vars[&memVar] = s.newValue1A(ssa.OpVarLive, ssa.TypeMem, n.Left, s.mem())
950
Keith Randall46ffb022015-09-12 14:06:44 -0700951 case OCHECKNIL:
952 p := s.expr(n.Left)
953 s.nilCheck(p)
954
Keith Randalld2fd43a2015-04-15 15:51:25 -0700955 default:
Josh Bleecher Snyder37ddc272015-06-24 14:03:39 -0700956 s.Unimplementedf("unhandled stmt %s", opnames[n.Op])
Keith Randalld2fd43a2015-04-15 15:51:25 -0700957 }
958}
959
Keith Randall6a8a9da2016-02-27 17:49:31 -0800960// exit processes any code that needs to be generated just before returning.
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +0000961// It returns a BlockRet block that ends the control flow. Its control value
Keith Randall6a8a9da2016-02-27 17:49:31 -0800962// will be set to the final memory state.
963func (s *state) exit() *ssa.Block {
Keith Randallddc6b642016-03-09 19:27:57 -0800964 if hasdefer {
965 s.rtcall(Deferreturn, true, nil)
966 }
967
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +0000968 // Run exit code. Typically, this code copies heap-allocated PPARAMOUT
Keith Randall6a8a9da2016-02-27 17:49:31 -0800969 // variables back to the stack.
970 s.stmts(s.exitCode)
971
972 // Store SSAable PPARAMOUT variables back to stack locations.
973 for _, n := range s.returns {
974 aux := &ssa.ArgSymbol{Typ: n.Type, Node: n}
975 addr := s.newValue1A(ssa.OpAddr, Ptrto(n.Type), aux, s.sp)
976 val := s.variable(n, n.Type)
977 s.vars[&memVar] = s.newValue1A(ssa.OpVarDef, ssa.TypeMem, n, s.mem())
978 s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, n.Type.Size(), addr, val, s.mem())
979 // TODO: if val is ever spilled, we'd like to use the
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +0000980 // PPARAMOUT slot for spilling it. That won't happen
Keith Randall6a8a9da2016-02-27 17:49:31 -0800981 // currently.
982 }
983
984 // Do actual return.
985 m := s.mem()
986 b := s.endBlock()
987 b.Kind = ssa.BlockRet
Keith Randall56e0ecc2016-03-15 20:45:50 -0700988 b.SetControl(m)
Keith Randall6a8a9da2016-02-27 17:49:31 -0800989 return b
990}
991
Keith Randall67fdb0d2015-07-19 15:48:20 -0700992type opAndType struct {
Keith Randall4304fbc2015-11-16 13:20:16 -0800993 op Op
994 etype EType
Keith Randall67fdb0d2015-07-19 15:48:20 -0700995}
996
997var opToSSA = map[opAndType]ssa.Op{
David Chase997a9f32015-08-12 16:38:11 -0400998 opAndType{OADD, TINT8}: ssa.OpAdd8,
999 opAndType{OADD, TUINT8}: ssa.OpAdd8,
1000 opAndType{OADD, TINT16}: ssa.OpAdd16,
1001 opAndType{OADD, TUINT16}: ssa.OpAdd16,
1002 opAndType{OADD, TINT32}: ssa.OpAdd32,
1003 opAndType{OADD, TUINT32}: ssa.OpAdd32,
1004 opAndType{OADD, TPTR32}: ssa.OpAdd32,
1005 opAndType{OADD, TINT64}: ssa.OpAdd64,
1006 opAndType{OADD, TUINT64}: ssa.OpAdd64,
1007 opAndType{OADD, TPTR64}: ssa.OpAdd64,
1008 opAndType{OADD, TFLOAT32}: ssa.OpAdd32F,
1009 opAndType{OADD, TFLOAT64}: ssa.OpAdd64F,
Keith Randall67fdb0d2015-07-19 15:48:20 -07001010
David Chase997a9f32015-08-12 16:38:11 -04001011 opAndType{OSUB, TINT8}: ssa.OpSub8,
1012 opAndType{OSUB, TUINT8}: ssa.OpSub8,
1013 opAndType{OSUB, TINT16}: ssa.OpSub16,
1014 opAndType{OSUB, TUINT16}: ssa.OpSub16,
1015 opAndType{OSUB, TINT32}: ssa.OpSub32,
1016 opAndType{OSUB, TUINT32}: ssa.OpSub32,
1017 opAndType{OSUB, TINT64}: ssa.OpSub64,
1018 opAndType{OSUB, TUINT64}: ssa.OpSub64,
1019 opAndType{OSUB, TFLOAT32}: ssa.OpSub32F,
1020 opAndType{OSUB, TFLOAT64}: ssa.OpSub64F,
Keith Randall67fdb0d2015-07-19 15:48:20 -07001021
Josh Bleecher Snydere61e7c92015-07-22 19:19:40 -07001022 opAndType{ONOT, TBOOL}: ssa.OpNot,
1023
David Chase3a9d0ac2015-08-28 14:24:10 -04001024 opAndType{OMINUS, TINT8}: ssa.OpNeg8,
1025 opAndType{OMINUS, TUINT8}: ssa.OpNeg8,
1026 opAndType{OMINUS, TINT16}: ssa.OpNeg16,
1027 opAndType{OMINUS, TUINT16}: ssa.OpNeg16,
1028 opAndType{OMINUS, TINT32}: ssa.OpNeg32,
1029 opAndType{OMINUS, TUINT32}: ssa.OpNeg32,
1030 opAndType{OMINUS, TINT64}: ssa.OpNeg64,
1031 opAndType{OMINUS, TUINT64}: ssa.OpNeg64,
1032 opAndType{OMINUS, TFLOAT32}: ssa.OpNeg32F,
1033 opAndType{OMINUS, TFLOAT64}: ssa.OpNeg64F,
Alexandru Moșoi954d5ad2015-07-21 16:58:18 +02001034
Keith Randall4b803152015-07-29 17:07:09 -07001035 opAndType{OCOM, TINT8}: ssa.OpCom8,
1036 opAndType{OCOM, TUINT8}: ssa.OpCom8,
1037 opAndType{OCOM, TINT16}: ssa.OpCom16,
1038 opAndType{OCOM, TUINT16}: ssa.OpCom16,
1039 opAndType{OCOM, TINT32}: ssa.OpCom32,
1040 opAndType{OCOM, TUINT32}: ssa.OpCom32,
1041 opAndType{OCOM, TINT64}: ssa.OpCom64,
1042 opAndType{OCOM, TUINT64}: ssa.OpCom64,
1043
Josh Bleecher Snyderfa5fe192015-09-06 19:24:59 -07001044 opAndType{OIMAG, TCOMPLEX64}: ssa.OpComplexImag,
1045 opAndType{OIMAG, TCOMPLEX128}: ssa.OpComplexImag,
1046 opAndType{OREAL, TCOMPLEX64}: ssa.OpComplexReal,
1047 opAndType{OREAL, TCOMPLEX128}: ssa.OpComplexReal,
1048
David Chase997a9f32015-08-12 16:38:11 -04001049 opAndType{OMUL, TINT8}: ssa.OpMul8,
1050 opAndType{OMUL, TUINT8}: ssa.OpMul8,
1051 opAndType{OMUL, TINT16}: ssa.OpMul16,
1052 opAndType{OMUL, TUINT16}: ssa.OpMul16,
1053 opAndType{OMUL, TINT32}: ssa.OpMul32,
1054 opAndType{OMUL, TUINT32}: ssa.OpMul32,
1055 opAndType{OMUL, TINT64}: ssa.OpMul64,
1056 opAndType{OMUL, TUINT64}: ssa.OpMul64,
1057 opAndType{OMUL, TFLOAT32}: ssa.OpMul32F,
1058 opAndType{OMUL, TFLOAT64}: ssa.OpMul64F,
1059
1060 opAndType{ODIV, TFLOAT32}: ssa.OpDiv32F,
1061 opAndType{ODIV, TFLOAT64}: ssa.OpDiv64F,
Keith Randallbe1eb572015-07-22 13:46:15 -07001062
Todd Neal67cbd5b2015-08-18 19:14:47 -05001063 opAndType{OHMUL, TINT8}: ssa.OpHmul8,
1064 opAndType{OHMUL, TUINT8}: ssa.OpHmul8u,
1065 opAndType{OHMUL, TINT16}: ssa.OpHmul16,
1066 opAndType{OHMUL, TUINT16}: ssa.OpHmul16u,
1067 opAndType{OHMUL, TINT32}: ssa.OpHmul32,
1068 opAndType{OHMUL, TUINT32}: ssa.OpHmul32u,
1069
Todd Neala45f2d82015-08-17 17:46:06 -05001070 opAndType{ODIV, TINT8}: ssa.OpDiv8,
1071 opAndType{ODIV, TUINT8}: ssa.OpDiv8u,
1072 opAndType{ODIV, TINT16}: ssa.OpDiv16,
1073 opAndType{ODIV, TUINT16}: ssa.OpDiv16u,
1074 opAndType{ODIV, TINT32}: ssa.OpDiv32,
1075 opAndType{ODIV, TUINT32}: ssa.OpDiv32u,
1076 opAndType{ODIV, TINT64}: ssa.OpDiv64,
1077 opAndType{ODIV, TUINT64}: ssa.OpDiv64u,
1078
Todd Neal57d9e7e2015-08-18 19:51:44 -05001079 opAndType{OMOD, TINT8}: ssa.OpMod8,
1080 opAndType{OMOD, TUINT8}: ssa.OpMod8u,
1081 opAndType{OMOD, TINT16}: ssa.OpMod16,
1082 opAndType{OMOD, TUINT16}: ssa.OpMod16u,
1083 opAndType{OMOD, TINT32}: ssa.OpMod32,
1084 opAndType{OMOD, TUINT32}: ssa.OpMod32u,
1085 opAndType{OMOD, TINT64}: ssa.OpMod64,
1086 opAndType{OMOD, TUINT64}: ssa.OpMod64u,
1087
Alexandru Moșoiedff8812015-07-28 14:58:49 +02001088 opAndType{OAND, TINT8}: ssa.OpAnd8,
Keith Randall2a5e6c42015-07-23 14:35:02 -07001089 opAndType{OAND, TUINT8}: ssa.OpAnd8,
Alexandru Moșoiedff8812015-07-28 14:58:49 +02001090 opAndType{OAND, TINT16}: ssa.OpAnd16,
Keith Randall2a5e6c42015-07-23 14:35:02 -07001091 opAndType{OAND, TUINT16}: ssa.OpAnd16,
Alexandru Moșoiedff8812015-07-28 14:58:49 +02001092 opAndType{OAND, TINT32}: ssa.OpAnd32,
Keith Randall2a5e6c42015-07-23 14:35:02 -07001093 opAndType{OAND, TUINT32}: ssa.OpAnd32,
Alexandru Moșoiedff8812015-07-28 14:58:49 +02001094 opAndType{OAND, TINT64}: ssa.OpAnd64,
Keith Randall2a5e6c42015-07-23 14:35:02 -07001095 opAndType{OAND, TUINT64}: ssa.OpAnd64,
Alexandru Moșoiedff8812015-07-28 14:58:49 +02001096
Alexandru Moșoi74024162015-07-29 17:52:25 +02001097 opAndType{OOR, TINT8}: ssa.OpOr8,
1098 opAndType{OOR, TUINT8}: ssa.OpOr8,
1099 opAndType{OOR, TINT16}: ssa.OpOr16,
1100 opAndType{OOR, TUINT16}: ssa.OpOr16,
1101 opAndType{OOR, TINT32}: ssa.OpOr32,
1102 opAndType{OOR, TUINT32}: ssa.OpOr32,
1103 opAndType{OOR, TINT64}: ssa.OpOr64,
1104 opAndType{OOR, TUINT64}: ssa.OpOr64,
1105
Alexandru Moșoi6d9362a12015-07-30 12:33:36 +02001106 opAndType{OXOR, TINT8}: ssa.OpXor8,
1107 opAndType{OXOR, TUINT8}: ssa.OpXor8,
1108 opAndType{OXOR, TINT16}: ssa.OpXor16,
1109 opAndType{OXOR, TUINT16}: ssa.OpXor16,
1110 opAndType{OXOR, TINT32}: ssa.OpXor32,
1111 opAndType{OXOR, TUINT32}: ssa.OpXor32,
1112 opAndType{OXOR, TINT64}: ssa.OpXor64,
1113 opAndType{OXOR, TUINT64}: ssa.OpXor64,
1114
Josh Bleecher Snyder1bab5b92015-07-28 14:14:25 -07001115 opAndType{OEQ, TBOOL}: ssa.OpEq8,
1116 opAndType{OEQ, TINT8}: ssa.OpEq8,
1117 opAndType{OEQ, TUINT8}: ssa.OpEq8,
1118 opAndType{OEQ, TINT16}: ssa.OpEq16,
1119 opAndType{OEQ, TUINT16}: ssa.OpEq16,
1120 opAndType{OEQ, TINT32}: ssa.OpEq32,
1121 opAndType{OEQ, TUINT32}: ssa.OpEq32,
1122 opAndType{OEQ, TINT64}: ssa.OpEq64,
1123 opAndType{OEQ, TUINT64}: ssa.OpEq64,
Keith Randall1e4ebfd2015-09-10 13:53:27 -07001124 opAndType{OEQ, TINTER}: ssa.OpEqInter,
1125 opAndType{OEQ, TARRAY}: ssa.OpEqSlice,
Josh Bleecher Snyder1bab5b92015-07-28 14:14:25 -07001126 opAndType{OEQ, TFUNC}: ssa.OpEqPtr,
1127 opAndType{OEQ, TMAP}: ssa.OpEqPtr,
1128 opAndType{OEQ, TCHAN}: ssa.OpEqPtr,
Todd Neal5fdd4fe2015-08-30 20:47:26 -05001129 opAndType{OEQ, TPTR64}: ssa.OpEqPtr,
Josh Bleecher Snyder1bab5b92015-07-28 14:14:25 -07001130 opAndType{OEQ, TUINTPTR}: ssa.OpEqPtr,
1131 opAndType{OEQ, TUNSAFEPTR}: ssa.OpEqPtr,
David Chase8e601b22015-08-18 14:39:26 -04001132 opAndType{OEQ, TFLOAT64}: ssa.OpEq64F,
1133 opAndType{OEQ, TFLOAT32}: ssa.OpEq32F,
Keith Randall67fdb0d2015-07-19 15:48:20 -07001134
Josh Bleecher Snyder1bab5b92015-07-28 14:14:25 -07001135 opAndType{ONE, TBOOL}: ssa.OpNeq8,
1136 opAndType{ONE, TINT8}: ssa.OpNeq8,
1137 opAndType{ONE, TUINT8}: ssa.OpNeq8,
1138 opAndType{ONE, TINT16}: ssa.OpNeq16,
1139 opAndType{ONE, TUINT16}: ssa.OpNeq16,
1140 opAndType{ONE, TINT32}: ssa.OpNeq32,
1141 opAndType{ONE, TUINT32}: ssa.OpNeq32,
1142 opAndType{ONE, TINT64}: ssa.OpNeq64,
1143 opAndType{ONE, TUINT64}: ssa.OpNeq64,
Keith Randall1e4ebfd2015-09-10 13:53:27 -07001144 opAndType{ONE, TINTER}: ssa.OpNeqInter,
1145 opAndType{ONE, TARRAY}: ssa.OpNeqSlice,
Josh Bleecher Snyder1bab5b92015-07-28 14:14:25 -07001146 opAndType{ONE, TFUNC}: ssa.OpNeqPtr,
1147 opAndType{ONE, TMAP}: ssa.OpNeqPtr,
1148 opAndType{ONE, TCHAN}: ssa.OpNeqPtr,
Todd Neal5fdd4fe2015-08-30 20:47:26 -05001149 opAndType{ONE, TPTR64}: ssa.OpNeqPtr,
Josh Bleecher Snyder1bab5b92015-07-28 14:14:25 -07001150 opAndType{ONE, TUINTPTR}: ssa.OpNeqPtr,
1151 opAndType{ONE, TUNSAFEPTR}: ssa.OpNeqPtr,
David Chase8e601b22015-08-18 14:39:26 -04001152 opAndType{ONE, TFLOAT64}: ssa.OpNeq64F,
1153 opAndType{ONE, TFLOAT32}: ssa.OpNeq32F,
Keith Randall67fdb0d2015-07-19 15:48:20 -07001154
David Chase8e601b22015-08-18 14:39:26 -04001155 opAndType{OLT, TINT8}: ssa.OpLess8,
1156 opAndType{OLT, TUINT8}: ssa.OpLess8U,
1157 opAndType{OLT, TINT16}: ssa.OpLess16,
1158 opAndType{OLT, TUINT16}: ssa.OpLess16U,
1159 opAndType{OLT, TINT32}: ssa.OpLess32,
1160 opAndType{OLT, TUINT32}: ssa.OpLess32U,
1161 opAndType{OLT, TINT64}: ssa.OpLess64,
1162 opAndType{OLT, TUINT64}: ssa.OpLess64U,
1163 opAndType{OLT, TFLOAT64}: ssa.OpLess64F,
1164 opAndType{OLT, TFLOAT32}: ssa.OpLess32F,
Keith Randall67fdb0d2015-07-19 15:48:20 -07001165
David Chase8e601b22015-08-18 14:39:26 -04001166 opAndType{OGT, TINT8}: ssa.OpGreater8,
1167 opAndType{OGT, TUINT8}: ssa.OpGreater8U,
1168 opAndType{OGT, TINT16}: ssa.OpGreater16,
1169 opAndType{OGT, TUINT16}: ssa.OpGreater16U,
1170 opAndType{OGT, TINT32}: ssa.OpGreater32,
1171 opAndType{OGT, TUINT32}: ssa.OpGreater32U,
1172 opAndType{OGT, TINT64}: ssa.OpGreater64,
1173 opAndType{OGT, TUINT64}: ssa.OpGreater64U,
1174 opAndType{OGT, TFLOAT64}: ssa.OpGreater64F,
1175 opAndType{OGT, TFLOAT32}: ssa.OpGreater32F,
Keith Randall67fdb0d2015-07-19 15:48:20 -07001176
David Chase8e601b22015-08-18 14:39:26 -04001177 opAndType{OLE, TINT8}: ssa.OpLeq8,
1178 opAndType{OLE, TUINT8}: ssa.OpLeq8U,
1179 opAndType{OLE, TINT16}: ssa.OpLeq16,
1180 opAndType{OLE, TUINT16}: ssa.OpLeq16U,
1181 opAndType{OLE, TINT32}: ssa.OpLeq32,
1182 opAndType{OLE, TUINT32}: ssa.OpLeq32U,
1183 opAndType{OLE, TINT64}: ssa.OpLeq64,
1184 opAndType{OLE, TUINT64}: ssa.OpLeq64U,
1185 opAndType{OLE, TFLOAT64}: ssa.OpLeq64F,
1186 opAndType{OLE, TFLOAT32}: ssa.OpLeq32F,
Keith Randall67fdb0d2015-07-19 15:48:20 -07001187
David Chase8e601b22015-08-18 14:39:26 -04001188 opAndType{OGE, TINT8}: ssa.OpGeq8,
1189 opAndType{OGE, TUINT8}: ssa.OpGeq8U,
1190 opAndType{OGE, TINT16}: ssa.OpGeq16,
1191 opAndType{OGE, TUINT16}: ssa.OpGeq16U,
1192 opAndType{OGE, TINT32}: ssa.OpGeq32,
1193 opAndType{OGE, TUINT32}: ssa.OpGeq32U,
1194 opAndType{OGE, TINT64}: ssa.OpGeq64,
1195 opAndType{OGE, TUINT64}: ssa.OpGeq64U,
1196 opAndType{OGE, TFLOAT64}: ssa.OpGeq64F,
1197 opAndType{OGE, TFLOAT32}: ssa.OpGeq32F,
David Chase40aba8c2015-08-05 22:11:14 -04001198
1199 opAndType{OLROT, TUINT8}: ssa.OpLrot8,
1200 opAndType{OLROT, TUINT16}: ssa.OpLrot16,
1201 opAndType{OLROT, TUINT32}: ssa.OpLrot32,
1202 opAndType{OLROT, TUINT64}: ssa.OpLrot64,
Keith Randalla329e212015-09-12 13:26:57 -07001203
1204 opAndType{OSQRT, TFLOAT64}: ssa.OpSqrt,
Keith Randall67fdb0d2015-07-19 15:48:20 -07001205}
1206
Keith Randall4304fbc2015-11-16 13:20:16 -08001207func (s *state) concreteEtype(t *Type) EType {
Keith Randall2a5e6c42015-07-23 14:35:02 -07001208 e := t.Etype
1209 switch e {
1210 default:
1211 return e
Keith Randall67fdb0d2015-07-19 15:48:20 -07001212 case TINT:
Keith Randall2a5e6c42015-07-23 14:35:02 -07001213 if s.config.IntSize == 8 {
1214 return TINT64
Keith Randall67fdb0d2015-07-19 15:48:20 -07001215 }
Keith Randall2a5e6c42015-07-23 14:35:02 -07001216 return TINT32
Keith Randall67fdb0d2015-07-19 15:48:20 -07001217 case TUINT:
Keith Randall2a5e6c42015-07-23 14:35:02 -07001218 if s.config.IntSize == 8 {
1219 return TUINT64
Keith Randall67fdb0d2015-07-19 15:48:20 -07001220 }
Keith Randall2a5e6c42015-07-23 14:35:02 -07001221 return TUINT32
1222 case TUINTPTR:
1223 if s.config.PtrSize == 8 {
1224 return TUINT64
1225 }
1226 return TUINT32
Keith Randall67fdb0d2015-07-19 15:48:20 -07001227 }
Keith Randall2a5e6c42015-07-23 14:35:02 -07001228}
1229
Keith Randall4304fbc2015-11-16 13:20:16 -08001230func (s *state) ssaOp(op Op, t *Type) ssa.Op {
Keith Randall2a5e6c42015-07-23 14:35:02 -07001231 etype := s.concreteEtype(t)
Keith Randall67fdb0d2015-07-19 15:48:20 -07001232 x, ok := opToSSA[opAndType{op, etype}]
1233 if !ok {
Keith Randall4304fbc2015-11-16 13:20:16 -08001234 s.Unimplementedf("unhandled binary op %s %s", opnames[op], Econv(etype))
Keith Randall67fdb0d2015-07-19 15:48:20 -07001235 }
1236 return x
Josh Bleecher Snyder46815b92015-06-24 17:48:22 -07001237}
1238
David Chase3a9d0ac2015-08-28 14:24:10 -04001239func floatForComplex(t *Type) *Type {
1240 if t.Size() == 8 {
1241 return Types[TFLOAT32]
1242 } else {
1243 return Types[TFLOAT64]
1244 }
1245}
1246
Keith Randall4b803152015-07-29 17:07:09 -07001247type opAndTwoTypes struct {
Keith Randall4304fbc2015-11-16 13:20:16 -08001248 op Op
1249 etype1 EType
1250 etype2 EType
Keith Randall4b803152015-07-29 17:07:09 -07001251}
1252
David Chased052bbd2015-09-01 17:09:00 -04001253type twoTypes struct {
Keith Randall4304fbc2015-11-16 13:20:16 -08001254 etype1 EType
1255 etype2 EType
David Chased052bbd2015-09-01 17:09:00 -04001256}
1257
1258type twoOpsAndType struct {
1259 op1 ssa.Op
1260 op2 ssa.Op
Keith Randall4304fbc2015-11-16 13:20:16 -08001261 intermediateType EType
David Chased052bbd2015-09-01 17:09:00 -04001262}
1263
1264var fpConvOpToSSA = map[twoTypes]twoOpsAndType{
1265
1266 twoTypes{TINT8, TFLOAT32}: twoOpsAndType{ssa.OpSignExt8to32, ssa.OpCvt32to32F, TINT32},
1267 twoTypes{TINT16, TFLOAT32}: twoOpsAndType{ssa.OpSignExt16to32, ssa.OpCvt32to32F, TINT32},
1268 twoTypes{TINT32, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt32to32F, TINT32},
1269 twoTypes{TINT64, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt64to32F, TINT64},
1270
1271 twoTypes{TINT8, TFLOAT64}: twoOpsAndType{ssa.OpSignExt8to32, ssa.OpCvt32to64F, TINT32},
1272 twoTypes{TINT16, TFLOAT64}: twoOpsAndType{ssa.OpSignExt16to32, ssa.OpCvt32to64F, TINT32},
1273 twoTypes{TINT32, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt32to64F, TINT32},
1274 twoTypes{TINT64, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt64to64F, TINT64},
1275
1276 twoTypes{TFLOAT32, TINT8}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpTrunc32to8, TINT32},
1277 twoTypes{TFLOAT32, TINT16}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpTrunc32to16, TINT32},
1278 twoTypes{TFLOAT32, TINT32}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpCopy, TINT32},
1279 twoTypes{TFLOAT32, TINT64}: twoOpsAndType{ssa.OpCvt32Fto64, ssa.OpCopy, TINT64},
1280
1281 twoTypes{TFLOAT64, TINT8}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpTrunc32to8, TINT32},
1282 twoTypes{TFLOAT64, TINT16}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpTrunc32to16, TINT32},
1283 twoTypes{TFLOAT64, TINT32}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpCopy, TINT32},
1284 twoTypes{TFLOAT64, TINT64}: twoOpsAndType{ssa.OpCvt64Fto64, ssa.OpCopy, TINT64},
1285 // unsigned
1286 twoTypes{TUINT8, TFLOAT32}: twoOpsAndType{ssa.OpZeroExt8to32, ssa.OpCvt32to32F, TINT32},
1287 twoTypes{TUINT16, TFLOAT32}: twoOpsAndType{ssa.OpZeroExt16to32, ssa.OpCvt32to32F, TINT32},
1288 twoTypes{TUINT32, TFLOAT32}: twoOpsAndType{ssa.OpZeroExt32to64, ssa.OpCvt64to32F, TINT64}, // go wide to dodge unsigned
1289 twoTypes{TUINT64, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpInvalid, TUINT64}, // Cvt64Uto32F, branchy code expansion instead
1290
1291 twoTypes{TUINT8, TFLOAT64}: twoOpsAndType{ssa.OpZeroExt8to32, ssa.OpCvt32to64F, TINT32},
1292 twoTypes{TUINT16, TFLOAT64}: twoOpsAndType{ssa.OpZeroExt16to32, ssa.OpCvt32to64F, TINT32},
1293 twoTypes{TUINT32, TFLOAT64}: twoOpsAndType{ssa.OpZeroExt32to64, ssa.OpCvt64to64F, TINT64}, // go wide to dodge unsigned
1294 twoTypes{TUINT64, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpInvalid, TUINT64}, // Cvt64Uto64F, branchy code expansion instead
1295
1296 twoTypes{TFLOAT32, TUINT8}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpTrunc32to8, TINT32},
1297 twoTypes{TFLOAT32, TUINT16}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpTrunc32to16, TINT32},
1298 twoTypes{TFLOAT32, TUINT32}: twoOpsAndType{ssa.OpCvt32Fto64, ssa.OpTrunc64to32, TINT64}, // go wide to dodge unsigned
1299 twoTypes{TFLOAT32, TUINT64}: twoOpsAndType{ssa.OpInvalid, ssa.OpCopy, TUINT64}, // Cvt32Fto64U, branchy code expansion instead
1300
1301 twoTypes{TFLOAT64, TUINT8}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpTrunc32to8, TINT32},
1302 twoTypes{TFLOAT64, TUINT16}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpTrunc32to16, TINT32},
1303 twoTypes{TFLOAT64, TUINT32}: twoOpsAndType{ssa.OpCvt64Fto64, ssa.OpTrunc64to32, TINT64}, // go wide to dodge unsigned
1304 twoTypes{TFLOAT64, TUINT64}: twoOpsAndType{ssa.OpInvalid, ssa.OpCopy, TUINT64}, // Cvt64Fto64U, branchy code expansion instead
1305
1306 // float
1307 twoTypes{TFLOAT64, TFLOAT32}: twoOpsAndType{ssa.OpCvt64Fto32F, ssa.OpCopy, TFLOAT32},
1308 twoTypes{TFLOAT64, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpCopy, TFLOAT64},
1309 twoTypes{TFLOAT32, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpCopy, TFLOAT32},
1310 twoTypes{TFLOAT32, TFLOAT64}: twoOpsAndType{ssa.OpCvt32Fto64F, ssa.OpCopy, TFLOAT64},
1311}
1312
Keith Randall4b803152015-07-29 17:07:09 -07001313var shiftOpToSSA = map[opAndTwoTypes]ssa.Op{
1314 opAndTwoTypes{OLSH, TINT8, TUINT8}: ssa.OpLsh8x8,
1315 opAndTwoTypes{OLSH, TUINT8, TUINT8}: ssa.OpLsh8x8,
1316 opAndTwoTypes{OLSH, TINT8, TUINT16}: ssa.OpLsh8x16,
1317 opAndTwoTypes{OLSH, TUINT8, TUINT16}: ssa.OpLsh8x16,
1318 opAndTwoTypes{OLSH, TINT8, TUINT32}: ssa.OpLsh8x32,
1319 opAndTwoTypes{OLSH, TUINT8, TUINT32}: ssa.OpLsh8x32,
1320 opAndTwoTypes{OLSH, TINT8, TUINT64}: ssa.OpLsh8x64,
1321 opAndTwoTypes{OLSH, TUINT8, TUINT64}: ssa.OpLsh8x64,
1322
1323 opAndTwoTypes{OLSH, TINT16, TUINT8}: ssa.OpLsh16x8,
1324 opAndTwoTypes{OLSH, TUINT16, TUINT8}: ssa.OpLsh16x8,
1325 opAndTwoTypes{OLSH, TINT16, TUINT16}: ssa.OpLsh16x16,
1326 opAndTwoTypes{OLSH, TUINT16, TUINT16}: ssa.OpLsh16x16,
1327 opAndTwoTypes{OLSH, TINT16, TUINT32}: ssa.OpLsh16x32,
1328 opAndTwoTypes{OLSH, TUINT16, TUINT32}: ssa.OpLsh16x32,
1329 opAndTwoTypes{OLSH, TINT16, TUINT64}: ssa.OpLsh16x64,
1330 opAndTwoTypes{OLSH, TUINT16, TUINT64}: ssa.OpLsh16x64,
1331
1332 opAndTwoTypes{OLSH, TINT32, TUINT8}: ssa.OpLsh32x8,
1333 opAndTwoTypes{OLSH, TUINT32, TUINT8}: ssa.OpLsh32x8,
1334 opAndTwoTypes{OLSH, TINT32, TUINT16}: ssa.OpLsh32x16,
1335 opAndTwoTypes{OLSH, TUINT32, TUINT16}: ssa.OpLsh32x16,
1336 opAndTwoTypes{OLSH, TINT32, TUINT32}: ssa.OpLsh32x32,
1337 opAndTwoTypes{OLSH, TUINT32, TUINT32}: ssa.OpLsh32x32,
1338 opAndTwoTypes{OLSH, TINT32, TUINT64}: ssa.OpLsh32x64,
1339 opAndTwoTypes{OLSH, TUINT32, TUINT64}: ssa.OpLsh32x64,
1340
1341 opAndTwoTypes{OLSH, TINT64, TUINT8}: ssa.OpLsh64x8,
1342 opAndTwoTypes{OLSH, TUINT64, TUINT8}: ssa.OpLsh64x8,
1343 opAndTwoTypes{OLSH, TINT64, TUINT16}: ssa.OpLsh64x16,
1344 opAndTwoTypes{OLSH, TUINT64, TUINT16}: ssa.OpLsh64x16,
1345 opAndTwoTypes{OLSH, TINT64, TUINT32}: ssa.OpLsh64x32,
1346 opAndTwoTypes{OLSH, TUINT64, TUINT32}: ssa.OpLsh64x32,
1347 opAndTwoTypes{OLSH, TINT64, TUINT64}: ssa.OpLsh64x64,
1348 opAndTwoTypes{OLSH, TUINT64, TUINT64}: ssa.OpLsh64x64,
1349
1350 opAndTwoTypes{ORSH, TINT8, TUINT8}: ssa.OpRsh8x8,
1351 opAndTwoTypes{ORSH, TUINT8, TUINT8}: ssa.OpRsh8Ux8,
1352 opAndTwoTypes{ORSH, TINT8, TUINT16}: ssa.OpRsh8x16,
1353 opAndTwoTypes{ORSH, TUINT8, TUINT16}: ssa.OpRsh8Ux16,
1354 opAndTwoTypes{ORSH, TINT8, TUINT32}: ssa.OpRsh8x32,
1355 opAndTwoTypes{ORSH, TUINT8, TUINT32}: ssa.OpRsh8Ux32,
1356 opAndTwoTypes{ORSH, TINT8, TUINT64}: ssa.OpRsh8x64,
1357 opAndTwoTypes{ORSH, TUINT8, TUINT64}: ssa.OpRsh8Ux64,
1358
1359 opAndTwoTypes{ORSH, TINT16, TUINT8}: ssa.OpRsh16x8,
1360 opAndTwoTypes{ORSH, TUINT16, TUINT8}: ssa.OpRsh16Ux8,
1361 opAndTwoTypes{ORSH, TINT16, TUINT16}: ssa.OpRsh16x16,
1362 opAndTwoTypes{ORSH, TUINT16, TUINT16}: ssa.OpRsh16Ux16,
1363 opAndTwoTypes{ORSH, TINT16, TUINT32}: ssa.OpRsh16x32,
1364 opAndTwoTypes{ORSH, TUINT16, TUINT32}: ssa.OpRsh16Ux32,
1365 opAndTwoTypes{ORSH, TINT16, TUINT64}: ssa.OpRsh16x64,
1366 opAndTwoTypes{ORSH, TUINT16, TUINT64}: ssa.OpRsh16Ux64,
1367
1368 opAndTwoTypes{ORSH, TINT32, TUINT8}: ssa.OpRsh32x8,
1369 opAndTwoTypes{ORSH, TUINT32, TUINT8}: ssa.OpRsh32Ux8,
1370 opAndTwoTypes{ORSH, TINT32, TUINT16}: ssa.OpRsh32x16,
1371 opAndTwoTypes{ORSH, TUINT32, TUINT16}: ssa.OpRsh32Ux16,
1372 opAndTwoTypes{ORSH, TINT32, TUINT32}: ssa.OpRsh32x32,
1373 opAndTwoTypes{ORSH, TUINT32, TUINT32}: ssa.OpRsh32Ux32,
1374 opAndTwoTypes{ORSH, TINT32, TUINT64}: ssa.OpRsh32x64,
1375 opAndTwoTypes{ORSH, TUINT32, TUINT64}: ssa.OpRsh32Ux64,
1376
1377 opAndTwoTypes{ORSH, TINT64, TUINT8}: ssa.OpRsh64x8,
1378 opAndTwoTypes{ORSH, TUINT64, TUINT8}: ssa.OpRsh64Ux8,
1379 opAndTwoTypes{ORSH, TINT64, TUINT16}: ssa.OpRsh64x16,
1380 opAndTwoTypes{ORSH, TUINT64, TUINT16}: ssa.OpRsh64Ux16,
1381 opAndTwoTypes{ORSH, TINT64, TUINT32}: ssa.OpRsh64x32,
1382 opAndTwoTypes{ORSH, TUINT64, TUINT32}: ssa.OpRsh64Ux32,
1383 opAndTwoTypes{ORSH, TINT64, TUINT64}: ssa.OpRsh64x64,
1384 opAndTwoTypes{ORSH, TUINT64, TUINT64}: ssa.OpRsh64Ux64,
1385}
1386
Keith Randall4304fbc2015-11-16 13:20:16 -08001387func (s *state) ssaShiftOp(op Op, t *Type, u *Type) ssa.Op {
Keith Randall4b803152015-07-29 17:07:09 -07001388 etype1 := s.concreteEtype(t)
1389 etype2 := s.concreteEtype(u)
1390 x, ok := shiftOpToSSA[opAndTwoTypes{op, etype1, etype2}]
1391 if !ok {
Keith Randall4304fbc2015-11-16 13:20:16 -08001392 s.Unimplementedf("unhandled shift op %s etype=%s/%s", opnames[op], Econv(etype1), Econv(etype2))
Keith Randall4b803152015-07-29 17:07:09 -07001393 }
1394 return x
1395}
1396
Keith Randall4304fbc2015-11-16 13:20:16 -08001397func (s *state) ssaRotateOp(op Op, t *Type) ssa.Op {
David Chase40aba8c2015-08-05 22:11:14 -04001398 etype1 := s.concreteEtype(t)
1399 x, ok := opToSSA[opAndType{op, etype1}]
1400 if !ok {
Keith Randall4304fbc2015-11-16 13:20:16 -08001401 s.Unimplementedf("unhandled rotate op %s etype=%s", opnames[op], Econv(etype1))
David Chase40aba8c2015-08-05 22:11:14 -04001402 }
1403 return x
1404}
1405
Keith Randalld2fd43a2015-04-15 15:51:25 -07001406// expr converts the expression n to ssa, adds it to s and returns the ssa result.
Keith Randallcfc2aa52015-05-18 16:44:20 -07001407func (s *state) expr(n *Node) *ssa.Value {
Michael Matloob81ccf502015-05-30 01:03:06 -04001408 s.pushLine(n.Lineno)
1409 defer s.popLine()
1410
Keith Randall06f32922015-07-11 11:39:12 -07001411 s.stmtList(n.Ninit)
Keith Randalld2fd43a2015-04-15 15:51:25 -07001412 switch n.Op {
Todd Nealdef7c652015-09-07 19:07:02 -05001413 case OCFUNC:
Todd Neald076ef72015-10-15 20:25:32 -05001414 aux := s.lookupSymbol(n, &ssa.ExternSymbol{n.Type, n.Left.Sym})
Todd Nealdef7c652015-09-07 19:07:02 -05001415 return s.entryNewValue1A(ssa.OpAddr, n.Type, aux, s.sb)
David Chase956f3192015-09-11 16:40:05 -04001416 case OPARAM:
David Chase57670ad2015-10-09 16:48:30 -04001417 addr := s.addr(n, false)
David Chase32ffbf72015-10-08 17:14:12 -04001418 return s.newValue2(ssa.OpLoad, n.Left.Type, addr, s.mem())
Keith Randalld2fd43a2015-04-15 15:51:25 -07001419 case ONAME:
Keith Randall290d8fc2015-06-10 15:03:06 -07001420 if n.Class == PFUNC {
1421 // "value" of a function is the address of the function's closure
Keith Randall8c46aa52015-06-19 21:02:28 -07001422 sym := funcsym(n.Sym)
1423 aux := &ssa.ExternSymbol{n.Type, sym}
1424 return s.entryNewValue1A(ssa.OpAddr, Ptrto(n.Type), aux, s.sb)
Keith Randall23df95b2015-05-12 15:16:52 -07001425 }
Keith Randall6a8a9da2016-02-27 17:49:31 -08001426 if s.canSSA(n) {
Keith Randall8c46aa52015-06-19 21:02:28 -07001427 return s.variable(n, n.Type)
Keith Randall290d8fc2015-06-10 15:03:06 -07001428 }
David Chase57670ad2015-10-09 16:48:30 -04001429 addr := s.addr(n, false)
Keith Randall8f22b522015-06-11 21:29:25 -07001430 return s.newValue2(ssa.OpLoad, n.Type, addr, s.mem())
David Chase956f3192015-09-11 16:40:05 -04001431 case OCLOSUREVAR:
David Chase57670ad2015-10-09 16:48:30 -04001432 addr := s.addr(n, false)
David Chase956f3192015-09-11 16:40:05 -04001433 return s.newValue2(ssa.OpLoad, n.Type, addr, s.mem())
Keith Randalld2fd43a2015-04-15 15:51:25 -07001434 case OLITERAL:
Keith Randalle707fbe2015-06-11 10:20:39 -07001435 switch n.Val().Ctype() {
Keith Randalld2fd43a2015-04-15 15:51:25 -07001436 case CTINT:
Josh Bleecher Snyder5cab0162016-04-01 14:51:02 -07001437 i := n.Int64()
Keith Randall9cb332e2015-07-28 14:19:20 -07001438 switch n.Type.Size() {
1439 case 1:
1440 return s.constInt8(n.Type, int8(i))
1441 case 2:
1442 return s.constInt16(n.Type, int16(i))
1443 case 4:
1444 return s.constInt32(n.Type, int32(i))
1445 case 8:
1446 return s.constInt64(n.Type, i)
1447 default:
1448 s.Fatalf("bad integer size %d", n.Type.Size())
1449 return nil
1450 }
1451 case CTSTR:
Josh Bleecher Snyder39214272016-03-06 18:06:09 -08001452 if n.Val().U == "" {
1453 return s.constEmptyString(n.Type)
1454 }
Keith Randall9cb332e2015-07-28 14:19:20 -07001455 return s.entryNewValue0A(ssa.OpConstString, n.Type, n.Val().U)
1456 case CTBOOL:
Keith Randallb5c5efd2016-01-14 16:02:23 -08001457 v := s.constBool(n.Val().U.(bool))
1458 // For some reason the frontend gets the line numbers of
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00001459 // CTBOOL literals totally wrong. Fix it here by grabbing
Keith Randallb5c5efd2016-01-14 16:02:23 -08001460 // the line number of the enclosing AST node.
1461 if len(s.line) >= 2 {
1462 v.Line = s.line[len(s.line)-2]
1463 }
1464 return v
Brad Fitzpatrick337b7e72015-07-13 17:30:42 -06001465 case CTNIL:
Keith Randall9f954db2015-08-18 10:26:28 -07001466 t := n.Type
1467 switch {
1468 case t.IsSlice():
Josh Bleecher Snyder39214272016-03-06 18:06:09 -08001469 return s.constSlice(t)
Keith Randall9f954db2015-08-18 10:26:28 -07001470 case t.IsInterface():
Josh Bleecher Snyder39214272016-03-06 18:06:09 -08001471 return s.constInterface(t)
Keith Randall9f954db2015-08-18 10:26:28 -07001472 default:
Josh Bleecher Snyder39214272016-03-06 18:06:09 -08001473 return s.constNil(t)
Keith Randall9f954db2015-08-18 10:26:28 -07001474 }
David Chase997a9f32015-08-12 16:38:11 -04001475 case CTFLT:
1476 f := n.Val().U.(*Mpflt)
1477 switch n.Type.Size() {
1478 case 4:
Matthew Dempskyd3253872016-03-20 13:55:42 -07001479 return s.constFloat32(n.Type, f.Float32())
David Chase997a9f32015-08-12 16:38:11 -04001480 case 8:
Matthew Dempskyd3253872016-03-20 13:55:42 -07001481 return s.constFloat64(n.Type, f.Float64())
David Chase997a9f32015-08-12 16:38:11 -04001482 default:
1483 s.Fatalf("bad float size %d", n.Type.Size())
1484 return nil
1485 }
David Chase52578582015-08-28 14:24:10 -04001486 case CTCPLX:
1487 c := n.Val().U.(*Mpcplx)
1488 r := &c.Real
1489 i := &c.Imag
1490 switch n.Type.Size() {
1491 case 8:
1492 {
1493 pt := Types[TFLOAT32]
1494 return s.newValue2(ssa.OpComplexMake, n.Type,
Matthew Dempskyd3253872016-03-20 13:55:42 -07001495 s.constFloat32(pt, r.Float32()),
1496 s.constFloat32(pt, i.Float32()))
David Chase52578582015-08-28 14:24:10 -04001497 }
1498 case 16:
1499 {
1500 pt := Types[TFLOAT64]
1501 return s.newValue2(ssa.OpComplexMake, n.Type,
Matthew Dempskyd3253872016-03-20 13:55:42 -07001502 s.constFloat64(pt, r.Float64()),
1503 s.constFloat64(pt, i.Float64()))
David Chase52578582015-08-28 14:24:10 -04001504 }
1505 default:
1506 s.Fatalf("bad float size %d", n.Type.Size())
1507 return nil
1508 }
David Chase997a9f32015-08-12 16:38:11 -04001509
Keith Randalld2fd43a2015-04-15 15:51:25 -07001510 default:
Josh Bleecher Snyder37ddc272015-06-24 14:03:39 -07001511 s.Unimplementedf("unhandled OLITERAL %v", n.Val().Ctype())
Keith Randalld2fd43a2015-04-15 15:51:25 -07001512 return nil
1513 }
Keith Randall0ad9c8c2015-06-12 16:24:33 -07001514 case OCONVNOP:
Josh Bleecher Snyder95aff4d2015-07-28 14:31:25 -07001515 to := n.Type
1516 from := n.Left.Type
Josh Bleecher Snyder95aff4d2015-07-28 14:31:25 -07001517
1518 // Assume everything will work out, so set up our return value.
1519 // Anything interesting that happens from here is a fatal.
Keith Randall0ad9c8c2015-06-12 16:24:33 -07001520 x := s.expr(n.Left)
David Chasee99dd522015-10-19 11:36:07 -04001521
1522 // Special case for not confusing GC and liveness.
1523 // We don't want pointers accidentally classified
1524 // as not-pointers or vice-versa because of copy
1525 // elision.
Matthew Dempsky788f1122016-03-28 10:55:44 -07001526 if to.IsPtrShaped() != from.IsPtrShaped() {
Keith Randall7807bda2015-11-10 15:35:36 -08001527 return s.newValue2(ssa.OpConvert, to, x, s.mem())
David Chasee99dd522015-10-19 11:36:07 -04001528 }
1529
Josh Bleecher Snyder95aff4d2015-07-28 14:31:25 -07001530 v := s.newValue1(ssa.OpCopy, to, x) // ensure that v has the right type
1531
Todd Nealdef7c652015-09-07 19:07:02 -05001532 // CONVNOP closure
Matthew Dempsky788f1122016-03-28 10:55:44 -07001533 if to.Etype == TFUNC && from.IsPtrShaped() {
Todd Nealdef7c652015-09-07 19:07:02 -05001534 return v
1535 }
1536
Josh Bleecher Snyder95aff4d2015-07-28 14:31:25 -07001537 // named <--> unnamed type or typed <--> untyped const
1538 if from.Etype == to.Etype {
1539 return v
1540 }
David Chasee99dd522015-10-19 11:36:07 -04001541
Josh Bleecher Snyder95aff4d2015-07-28 14:31:25 -07001542 // unsafe.Pointer <--> *T
1543 if to.Etype == TUNSAFEPTR && from.IsPtr() || from.Etype == TUNSAFEPTR && to.IsPtr() {
1544 return v
1545 }
1546
1547 dowidth(from)
1548 dowidth(to)
1549 if from.Width != to.Width {
1550 s.Fatalf("CONVNOP width mismatch %v (%d) -> %v (%d)\n", from, from.Width, to, to.Width)
1551 return nil
1552 }
1553 if etypesign(from.Etype) != etypesign(to.Etype) {
Keith Randall4304fbc2015-11-16 13:20:16 -08001554 s.Fatalf("CONVNOP sign mismatch %v (%s) -> %v (%s)\n", from, Econv(from.Etype), to, Econv(to.Etype))
Josh Bleecher Snyder95aff4d2015-07-28 14:31:25 -07001555 return nil
1556 }
1557
Ian Lance Taylor88e18032016-03-01 15:17:34 -08001558 if instrumenting {
David Chase57670ad2015-10-09 16:48:30 -04001559 // These appear to be fine, but they fail the
1560 // integer constraint below, so okay them here.
1561 // Sample non-integer conversion: map[string]string -> *uint8
1562 return v
Josh Bleecher Snyder95aff4d2015-07-28 14:31:25 -07001563 }
1564
1565 if etypesign(from.Etype) == 0 {
1566 s.Fatalf("CONVNOP unrecognized non-integer %v -> %v\n", from, to)
1567 return nil
1568 }
1569
1570 // integer, same width, same sign
1571 return v
1572
Michael Matloob73054f52015-06-14 11:38:46 -07001573 case OCONV:
1574 x := s.expr(n.Left)
Keith Randall2a5e6c42015-07-23 14:35:02 -07001575 ft := n.Left.Type // from type
1576 tt := n.Type // to type
1577 if ft.IsInteger() && tt.IsInteger() {
1578 var op ssa.Op
1579 if tt.Size() == ft.Size() {
Josh Bleecher Snyder95aff4d2015-07-28 14:31:25 -07001580 op = ssa.OpCopy
Keith Randall2a5e6c42015-07-23 14:35:02 -07001581 } else if tt.Size() < ft.Size() {
1582 // truncation
1583 switch 10*ft.Size() + tt.Size() {
1584 case 21:
1585 op = ssa.OpTrunc16to8
1586 case 41:
1587 op = ssa.OpTrunc32to8
1588 case 42:
1589 op = ssa.OpTrunc32to16
1590 case 81:
1591 op = ssa.OpTrunc64to8
1592 case 82:
1593 op = ssa.OpTrunc64to16
1594 case 84:
1595 op = ssa.OpTrunc64to32
1596 default:
1597 s.Fatalf("weird integer truncation %s -> %s", ft, tt)
1598 }
1599 } else if ft.IsSigned() {
1600 // sign extension
1601 switch 10*ft.Size() + tt.Size() {
1602 case 12:
1603 op = ssa.OpSignExt8to16
1604 case 14:
1605 op = ssa.OpSignExt8to32
1606 case 18:
1607 op = ssa.OpSignExt8to64
1608 case 24:
1609 op = ssa.OpSignExt16to32
1610 case 28:
1611 op = ssa.OpSignExt16to64
1612 case 48:
1613 op = ssa.OpSignExt32to64
1614 default:
1615 s.Fatalf("bad integer sign extension %s -> %s", ft, tt)
1616 }
1617 } else {
1618 // zero extension
1619 switch 10*ft.Size() + tt.Size() {
1620 case 12:
1621 op = ssa.OpZeroExt8to16
1622 case 14:
1623 op = ssa.OpZeroExt8to32
1624 case 18:
1625 op = ssa.OpZeroExt8to64
1626 case 24:
1627 op = ssa.OpZeroExt16to32
1628 case 28:
1629 op = ssa.OpZeroExt16to64
1630 case 48:
1631 op = ssa.OpZeroExt32to64
1632 default:
1633 s.Fatalf("weird integer sign extension %s -> %s", ft, tt)
1634 }
1635 }
1636 return s.newValue1(op, n.Type, x)
1637 }
David Chase42825882015-08-20 15:14:20 -04001638
David Chased052bbd2015-09-01 17:09:00 -04001639 if ft.IsFloat() || tt.IsFloat() {
1640 conv, ok := fpConvOpToSSA[twoTypes{s.concreteEtype(ft), s.concreteEtype(tt)}]
1641 if !ok {
1642 s.Fatalf("weird float conversion %s -> %s", ft, tt)
David Chase42825882015-08-20 15:14:20 -04001643 }
David Chased052bbd2015-09-01 17:09:00 -04001644 op1, op2, it := conv.op1, conv.op2, conv.intermediateType
1645
1646 if op1 != ssa.OpInvalid && op2 != ssa.OpInvalid {
1647 // normal case, not tripping over unsigned 64
1648 if op1 == ssa.OpCopy {
1649 if op2 == ssa.OpCopy {
1650 return x
1651 }
1652 return s.newValue1(op2, n.Type, x)
1653 }
1654 if op2 == ssa.OpCopy {
1655 return s.newValue1(op1, n.Type, x)
1656 }
1657 return s.newValue1(op2, n.Type, s.newValue1(op1, Types[it], x))
1658 }
1659 // Tricky 64-bit unsigned cases.
1660 if ft.IsInteger() {
1661 // therefore tt is float32 or float64, and ft is also unsigned
David Chase42825882015-08-20 15:14:20 -04001662 if tt.Size() == 4 {
1663 return s.uint64Tofloat32(n, x, ft, tt)
1664 }
1665 if tt.Size() == 8 {
1666 return s.uint64Tofloat64(n, x, ft, tt)
1667 }
David Chased052bbd2015-09-01 17:09:00 -04001668 s.Fatalf("weird unsigned integer to float conversion %s -> %s", ft, tt)
David Chase42825882015-08-20 15:14:20 -04001669 }
David Chased052bbd2015-09-01 17:09:00 -04001670 // therefore ft is float32 or float64, and tt is unsigned integer
David Chase73151062015-08-26 14:25:40 -04001671 if ft.Size() == 4 {
David Chased052bbd2015-09-01 17:09:00 -04001672 return s.float32ToUint64(n, x, ft, tt)
David Chase73151062015-08-26 14:25:40 -04001673 }
David Chased052bbd2015-09-01 17:09:00 -04001674 if ft.Size() == 8 {
1675 return s.float64ToUint64(n, x, ft, tt)
David Chase73151062015-08-26 14:25:40 -04001676 }
David Chased052bbd2015-09-01 17:09:00 -04001677 s.Fatalf("weird float to unsigned integer conversion %s -> %s", ft, tt)
1678 return nil
David Chase42825882015-08-20 15:14:20 -04001679 }
David Chase3a9d0ac2015-08-28 14:24:10 -04001680
1681 if ft.IsComplex() && tt.IsComplex() {
1682 var op ssa.Op
1683 if ft.Size() == tt.Size() {
1684 op = ssa.OpCopy
1685 } else if ft.Size() == 8 && tt.Size() == 16 {
1686 op = ssa.OpCvt32Fto64F
1687 } else if ft.Size() == 16 && tt.Size() == 8 {
1688 op = ssa.OpCvt64Fto32F
1689 } else {
1690 s.Fatalf("weird complex conversion %s -> %s", ft, tt)
1691 }
1692 ftp := floatForComplex(ft)
1693 ttp := floatForComplex(tt)
1694 return s.newValue2(ssa.OpComplexMake, tt,
1695 s.newValue1(op, ttp, s.newValue1(ssa.OpComplexReal, ftp, x)),
1696 s.newValue1(op, ttp, s.newValue1(ssa.OpComplexImag, ftp, x)))
1697 }
David Chase42825882015-08-20 15:14:20 -04001698
Keith Randall4304fbc2015-11-16 13:20:16 -08001699 s.Unimplementedf("unhandled OCONV %s -> %s", Econv(n.Left.Type.Etype), Econv(n.Type.Etype))
Keith Randall2a5e6c42015-07-23 14:35:02 -07001700 return nil
Keith Randallcfc2aa52015-05-18 16:44:20 -07001701
Keith Randall269baa92015-09-17 10:31:16 -07001702 case ODOTTYPE:
1703 res, _ := s.dottype(n, false)
1704 return res
1705
Josh Bleecher Snyder46815b92015-06-24 17:48:22 -07001706 // binary ops
1707 case OLT, OEQ, ONE, OLE, OGE, OGT:
Keith Randalld2fd43a2015-04-15 15:51:25 -07001708 a := s.expr(n.Left)
1709 b := s.expr(n.Right)
Keith Randalldb380bf2015-09-10 11:05:42 -07001710 if n.Left.Type.IsComplex() {
Keith Randallc244ce02015-09-10 14:59:00 -07001711 pt := floatForComplex(n.Left.Type)
Keith Randalldb380bf2015-09-10 11:05:42 -07001712 op := s.ssaOp(OEQ, pt)
1713 r := s.newValue2(op, Types[TBOOL], s.newValue1(ssa.OpComplexReal, pt, a), s.newValue1(ssa.OpComplexReal, pt, b))
1714 i := s.newValue2(op, Types[TBOOL], s.newValue1(ssa.OpComplexImag, pt, a), s.newValue1(ssa.OpComplexImag, pt, b))
1715 c := s.newValue2(ssa.OpAnd8, Types[TBOOL], r, i)
1716 switch n.Op {
1717 case OEQ:
1718 return c
1719 case ONE:
1720 return s.newValue1(ssa.OpNot, Types[TBOOL], c)
1721 default:
1722 s.Fatalf("ordered complex compare %s", opnames[n.Op])
1723 }
Keith Randalldb380bf2015-09-10 11:05:42 -07001724 }
Josh Bleecher Snyder85e03292015-07-30 11:03:05 -07001725 return s.newValue2(s.ssaOp(n.Op, n.Left.Type), Types[TBOOL], a, b)
David Chase3a9d0ac2015-08-28 14:24:10 -04001726 case OMUL:
1727 a := s.expr(n.Left)
1728 b := s.expr(n.Right)
1729 if n.Type.IsComplex() {
1730 mulop := ssa.OpMul64F
1731 addop := ssa.OpAdd64F
1732 subop := ssa.OpSub64F
1733 pt := floatForComplex(n.Type) // Could be Float32 or Float64
1734 wt := Types[TFLOAT64] // Compute in Float64 to minimize cancellation error
1735
1736 areal := s.newValue1(ssa.OpComplexReal, pt, a)
1737 breal := s.newValue1(ssa.OpComplexReal, pt, b)
1738 aimag := s.newValue1(ssa.OpComplexImag, pt, a)
1739 bimag := s.newValue1(ssa.OpComplexImag, pt, b)
1740
1741 if pt != wt { // Widen for calculation
1742 areal = s.newValue1(ssa.OpCvt32Fto64F, wt, areal)
1743 breal = s.newValue1(ssa.OpCvt32Fto64F, wt, breal)
1744 aimag = s.newValue1(ssa.OpCvt32Fto64F, wt, aimag)
1745 bimag = s.newValue1(ssa.OpCvt32Fto64F, wt, bimag)
1746 }
1747
1748 xreal := s.newValue2(subop, wt, s.newValue2(mulop, wt, areal, breal), s.newValue2(mulop, wt, aimag, bimag))
1749 ximag := s.newValue2(addop, wt, s.newValue2(mulop, wt, areal, bimag), s.newValue2(mulop, wt, aimag, breal))
1750
1751 if pt != wt { // Narrow to store back
1752 xreal = s.newValue1(ssa.OpCvt64Fto32F, pt, xreal)
1753 ximag = s.newValue1(ssa.OpCvt64Fto32F, pt, ximag)
1754 }
1755
1756 return s.newValue2(ssa.OpComplexMake, n.Type, xreal, ximag)
1757 }
1758 return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
1759
1760 case ODIV:
1761 a := s.expr(n.Left)
1762 b := s.expr(n.Right)
1763 if n.Type.IsComplex() {
1764 // TODO this is not executed because the front-end substitutes a runtime call.
1765 // That probably ought to change; with modest optimization the widen/narrow
1766 // conversions could all be elided in larger expression trees.
1767 mulop := ssa.OpMul64F
1768 addop := ssa.OpAdd64F
1769 subop := ssa.OpSub64F
1770 divop := ssa.OpDiv64F
1771 pt := floatForComplex(n.Type) // Could be Float32 or Float64
1772 wt := Types[TFLOAT64] // Compute in Float64 to minimize cancellation error
1773
1774 areal := s.newValue1(ssa.OpComplexReal, pt, a)
1775 breal := s.newValue1(ssa.OpComplexReal, pt, b)
1776 aimag := s.newValue1(ssa.OpComplexImag, pt, a)
1777 bimag := s.newValue1(ssa.OpComplexImag, pt, b)
1778
1779 if pt != wt { // Widen for calculation
1780 areal = s.newValue1(ssa.OpCvt32Fto64F, wt, areal)
1781 breal = s.newValue1(ssa.OpCvt32Fto64F, wt, breal)
1782 aimag = s.newValue1(ssa.OpCvt32Fto64F, wt, aimag)
1783 bimag = s.newValue1(ssa.OpCvt32Fto64F, wt, bimag)
1784 }
1785
1786 denom := s.newValue2(addop, wt, s.newValue2(mulop, wt, breal, breal), s.newValue2(mulop, wt, bimag, bimag))
1787 xreal := s.newValue2(addop, wt, s.newValue2(mulop, wt, areal, breal), s.newValue2(mulop, wt, aimag, bimag))
1788 ximag := s.newValue2(subop, wt, s.newValue2(mulop, wt, aimag, breal), s.newValue2(mulop, wt, areal, bimag))
1789
1790 // TODO not sure if this is best done in wide precision or narrow
1791 // Double-rounding might be an issue.
1792 // Note that the pre-SSA implementation does the entire calculation
1793 // in wide format, so wide is compatible.
1794 xreal = s.newValue2(divop, wt, xreal, denom)
1795 ximag = s.newValue2(divop, wt, ximag, denom)
1796
1797 if pt != wt { // Narrow to store back
1798 xreal = s.newValue1(ssa.OpCvt64Fto32F, pt, xreal)
1799 ximag = s.newValue1(ssa.OpCvt64Fto32F, pt, ximag)
1800 }
David Chase3a9d0ac2015-08-28 14:24:10 -04001801 return s.newValue2(ssa.OpComplexMake, n.Type, xreal, ximag)
1802 }
David Chase18559e22015-10-28 13:55:46 -04001803 if n.Type.IsFloat() {
1804 return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
1805 } else {
1806 // do a size-appropriate check for zero
1807 cmp := s.newValue2(s.ssaOp(ONE, n.Type), Types[TBOOL], b, s.zeroVal(n.Type))
1808 s.check(cmp, panicdivide)
1809 return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
1810 }
1811 case OMOD:
1812 a := s.expr(n.Left)
1813 b := s.expr(n.Right)
1814 // do a size-appropriate check for zero
1815 cmp := s.newValue2(s.ssaOp(ONE, n.Type), Types[TBOOL], b, s.zeroVal(n.Type))
1816 s.check(cmp, panicdivide)
David Chase3a9d0ac2015-08-28 14:24:10 -04001817 return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
1818 case OADD, OSUB:
1819 a := s.expr(n.Left)
1820 b := s.expr(n.Right)
1821 if n.Type.IsComplex() {
1822 pt := floatForComplex(n.Type)
1823 op := s.ssaOp(n.Op, pt)
1824 return s.newValue2(ssa.OpComplexMake, n.Type,
1825 s.newValue2(op, pt, s.newValue1(ssa.OpComplexReal, pt, a), s.newValue1(ssa.OpComplexReal, pt, b)),
1826 s.newValue2(op, pt, s.newValue1(ssa.OpComplexImag, pt, a), s.newValue1(ssa.OpComplexImag, pt, b)))
1827 }
1828 return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
David Chase18559e22015-10-28 13:55:46 -04001829 case OAND, OOR, OHMUL, OXOR:
Keith Randalld2fd43a2015-04-15 15:51:25 -07001830 a := s.expr(n.Left)
1831 b := s.expr(n.Right)
Keith Randall67fdb0d2015-07-19 15:48:20 -07001832 return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b)
Keith Randall4b803152015-07-29 17:07:09 -07001833 case OLSH, ORSH:
1834 a := s.expr(n.Left)
1835 b := s.expr(n.Right)
1836 return s.newValue2(s.ssaShiftOp(n.Op, n.Type, n.Right.Type), a.Type, a, b)
David Chase40aba8c2015-08-05 22:11:14 -04001837 case OLROT:
1838 a := s.expr(n.Left)
Josh Bleecher Snyder5cab0162016-04-01 14:51:02 -07001839 i := n.Right.Int64()
David Chase40aba8c2015-08-05 22:11:14 -04001840 if i <= 0 || i >= n.Type.Size()*8 {
1841 s.Fatalf("Wrong rotate distance for LROT, expected 1 through %d, saw %d", n.Type.Size()*8-1, i)
1842 }
1843 return s.newValue1I(s.ssaRotateOp(n.Op, n.Type), a.Type, i, a)
Brad Fitzpatricke8167112015-07-10 12:58:53 -06001844 case OANDAND, OOROR:
1845 // To implement OANDAND (and OOROR), we introduce a
1846 // new temporary variable to hold the result. The
1847 // variable is associated with the OANDAND node in the
1848 // s.vars table (normally variables are only
1849 // associated with ONAME nodes). We convert
1850 // A && B
1851 // to
1852 // var = A
1853 // if var {
1854 // var = B
1855 // }
1856 // Using var in the subsequent block introduces the
1857 // necessary phi variable.
1858 el := s.expr(n.Left)
1859 s.vars[n] = el
1860
1861 b := s.endBlock()
1862 b.Kind = ssa.BlockIf
Keith Randall56e0ecc2016-03-15 20:45:50 -07001863 b.SetControl(el)
Josh Bleecher Snyderbbf8c5c2015-08-11 17:28:56 -07001864 // In theory, we should set b.Likely here based on context.
1865 // However, gc only gives us likeliness hints
1866 // in a single place, for plain OIF statements,
1867 // and passing around context is finnicky, so don't bother for now.
Brad Fitzpatricke8167112015-07-10 12:58:53 -06001868
1869 bRight := s.f.NewBlock(ssa.BlockPlain)
1870 bResult := s.f.NewBlock(ssa.BlockPlain)
1871 if n.Op == OANDAND {
Todd Neal47d67992015-08-28 21:36:29 -05001872 b.AddEdgeTo(bRight)
1873 b.AddEdgeTo(bResult)
Brad Fitzpatricke8167112015-07-10 12:58:53 -06001874 } else if n.Op == OOROR {
Todd Neal47d67992015-08-28 21:36:29 -05001875 b.AddEdgeTo(bResult)
1876 b.AddEdgeTo(bRight)
Brad Fitzpatricke8167112015-07-10 12:58:53 -06001877 }
1878
1879 s.startBlock(bRight)
1880 er := s.expr(n.Right)
1881 s.vars[n] = er
1882
1883 b = s.endBlock()
Todd Neal47d67992015-08-28 21:36:29 -05001884 b.AddEdgeTo(bResult)
Brad Fitzpatricke8167112015-07-10 12:58:53 -06001885
1886 s.startBlock(bResult)
Josh Bleecher Snyder35ad1fc2015-08-27 10:11:08 -07001887 return s.variable(n, Types[TBOOL])
Keith Randall7e390722015-09-12 14:14:02 -07001888 case OCOMPLEX:
1889 r := s.expr(n.Left)
1890 i := s.expr(n.Right)
1891 return s.newValue2(ssa.OpComplexMake, n.Type, r, i)
Keith Randalld2fd43a2015-04-15 15:51:25 -07001892
Josh Bleecher Snyder4178f202015-09-05 19:28:00 -07001893 // unary ops
David Chase3a9d0ac2015-08-28 14:24:10 -04001894 case OMINUS:
1895 a := s.expr(n.Left)
1896 if n.Type.IsComplex() {
1897 tp := floatForComplex(n.Type)
1898 negop := s.ssaOp(n.Op, tp)
1899 return s.newValue2(ssa.OpComplexMake, n.Type,
1900 s.newValue1(negop, tp, s.newValue1(ssa.OpComplexReal, tp, a)),
1901 s.newValue1(negop, tp, s.newValue1(ssa.OpComplexImag, tp, a)))
1902 }
1903 return s.newValue1(s.ssaOp(n.Op, n.Type), a.Type, a)
Keith Randalla329e212015-09-12 13:26:57 -07001904 case ONOT, OCOM, OSQRT:
Brad Fitzpatrickd9c72d72015-07-10 11:25:48 -06001905 a := s.expr(n.Left)
Alexandru Moșoi954d5ad2015-07-21 16:58:18 +02001906 return s.newValue1(s.ssaOp(n.Op, n.Type), a.Type, a)
Keith Randall2f518072015-09-10 11:37:09 -07001907 case OIMAG, OREAL:
1908 a := s.expr(n.Left)
1909 return s.newValue1(s.ssaOp(n.Op, n.Left.Type), n.Type, a)
Josh Bleecher Snyder4178f202015-09-05 19:28:00 -07001910 case OPLUS:
1911 return s.expr(n.Left)
Brad Fitzpatrickd9c72d72015-07-10 11:25:48 -06001912
Keith Randallcfc2aa52015-05-18 16:44:20 -07001913 case OADDR:
David Chase57670ad2015-10-09 16:48:30 -04001914 return s.addr(n.Left, n.Bounded)
Keith Randallcfc2aa52015-05-18 16:44:20 -07001915
Josh Bleecher Snyder25d19162015-07-28 12:37:46 -07001916 case OINDREG:
1917 if int(n.Reg) != Thearch.REGSP {
1918 s.Unimplementedf("OINDREG of non-SP register %s in expr: %v", obj.Rconv(int(n.Reg)), n)
1919 return nil
1920 }
1921 addr := s.entryNewValue1I(ssa.OpOffPtr, Ptrto(n.Type), n.Xoffset, s.sp)
1922 return s.newValue2(ssa.OpLoad, n.Type, addr, s.mem())
1923
Keith Randalld2fd43a2015-04-15 15:51:25 -07001924 case OIND:
1925 p := s.expr(n.Left)
Keith Randallcfc2aa52015-05-18 16:44:20 -07001926 s.nilCheck(p)
Keith Randall8f22b522015-06-11 21:29:25 -07001927 return s.newValue2(ssa.OpLoad, n.Type, p, s.mem())
Keith Randallcfc2aa52015-05-18 16:44:20 -07001928
Keith Randallcd7e0592015-07-15 21:33:49 -07001929 case ODOT:
Keith Randalla734bbc2016-01-11 21:05:33 -08001930 t := n.Left.Type
1931 if canSSAType(t) {
1932 v := s.expr(n.Left)
Matthew Dempsky1b9f1682016-03-14 12:45:18 -07001933 return s.newValue1I(ssa.OpStructSelect, n.Type, int64(fieldIdx(n)), v)
Keith Randalla734bbc2016-01-11 21:05:33 -08001934 }
David Chase57670ad2015-10-09 16:48:30 -04001935 p := s.addr(n, false)
Keith Randall97035642015-10-09 09:33:29 -07001936 return s.newValue2(ssa.OpLoad, n.Type, p, s.mem())
Keith Randallcd7e0592015-07-15 21:33:49 -07001937
Keith Randalld2fd43a2015-04-15 15:51:25 -07001938 case ODOTPTR:
1939 p := s.expr(n.Left)
Keith Randallcfc2aa52015-05-18 16:44:20 -07001940 s.nilCheck(p)
Josh Bleecher Snyderda1802f2016-03-04 12:34:43 -08001941 p = s.newValue1I(ssa.OpOffPtr, p.Type, n.Xoffset, p)
Keith Randall8f22b522015-06-11 21:29:25 -07001942 return s.newValue2(ssa.OpLoad, n.Type, p, s.mem())
Keith Randalld2fd43a2015-04-15 15:51:25 -07001943
1944 case OINDEX:
Keith Randall97035642015-10-09 09:33:29 -07001945 switch {
1946 case n.Left.Type.IsString():
Keith Randallcfc2aa52015-05-18 16:44:20 -07001947 a := s.expr(n.Left)
1948 i := s.expr(n.Right)
Keith Randall2a5e6c42015-07-23 14:35:02 -07001949 i = s.extendIndex(i)
Keith Randall97035642015-10-09 09:33:29 -07001950 if !n.Bounded {
1951 len := s.newValue1(ssa.OpStringLen, Types[TINT], a)
1952 s.boundsCheck(i, len)
Josh Bleecher Snydere00d6092015-06-02 09:16:22 -07001953 }
Keith Randall97035642015-10-09 09:33:29 -07001954 ptrtyp := Ptrto(Types[TUINT8])
1955 ptr := s.newValue1(ssa.OpStringPtr, ptrtyp, a)
Josh Bleecher Snyderda1802f2016-03-04 12:34:43 -08001956 if Isconst(n.Right, CTINT) {
Josh Bleecher Snyder5cab0162016-04-01 14:51:02 -07001957 ptr = s.newValue1I(ssa.OpOffPtr, ptrtyp, n.Right.Int64(), ptr)
Josh Bleecher Snyderda1802f2016-03-04 12:34:43 -08001958 } else {
1959 ptr = s.newValue2(ssa.OpAddPtr, ptrtyp, ptr, i)
1960 }
Keith Randall97035642015-10-09 09:33:29 -07001961 return s.newValue2(ssa.OpLoad, Types[TUINT8], ptr, s.mem())
1962 case n.Left.Type.IsSlice():
David Chase57670ad2015-10-09 16:48:30 -04001963 p := s.addr(n, false)
Josh Bleecher Snyder8640b512016-03-30 10:57:47 -07001964 return s.newValue2(ssa.OpLoad, n.Left.Type.Elem(), p, s.mem())
Keith Randall97035642015-10-09 09:33:29 -07001965 case n.Left.Type.IsArray():
1966 // TODO: fix when we can SSA arrays of length 1.
David Chase57670ad2015-10-09 16:48:30 -04001967 p := s.addr(n, false)
Josh Bleecher Snyder8640b512016-03-30 10:57:47 -07001968 return s.newValue2(ssa.OpLoad, n.Left.Type.Elem(), p, s.mem())
Keith Randall97035642015-10-09 09:33:29 -07001969 default:
1970 s.Fatalf("bad type for index %v", n.Left.Type)
1971 return nil
Keith Randallcfc2aa52015-05-18 16:44:20 -07001972 }
Keith Randalld2fd43a2015-04-15 15:51:25 -07001973
Brad Fitzpatrick7af53d92015-07-10 10:47:28 -06001974 case OLEN, OCAP:
Josh Bleecher Snydercc3f0312015-07-03 18:41:28 -07001975 switch {
Brad Fitzpatrick7af53d92015-07-10 10:47:28 -06001976 case n.Left.Type.IsSlice():
1977 op := ssa.OpSliceLen
1978 if n.Op == OCAP {
1979 op = ssa.OpSliceCap
1980 }
Josh Bleecher Snyder85e03292015-07-30 11:03:05 -07001981 return s.newValue1(op, Types[TINT], s.expr(n.Left))
Brad Fitzpatrick7af53d92015-07-10 10:47:28 -06001982 case n.Left.Type.IsString(): // string; not reachable for OCAP
Josh Bleecher Snyder85e03292015-07-30 11:03:05 -07001983 return s.newValue1(ssa.OpStringLen, Types[TINT], s.expr(n.Left))
Todd Neal707af252015-08-28 15:56:43 -05001984 case n.Left.Type.IsMap(), n.Left.Type.IsChan():
1985 return s.referenceTypeBuiltin(n, s.expr(n.Left))
Josh Bleecher Snydercc3f0312015-07-03 18:41:28 -07001986 default: // array
Josh Bleecher Snyder3a0783c2016-03-31 14:46:04 -07001987 return s.constInt(Types[TINT], n.Left.Type.NumElem())
Josh Bleecher Snydercc3f0312015-07-03 18:41:28 -07001988 }
1989
Josh Bleecher Snydera2d15802015-08-12 10:12:14 -07001990 case OSPTR:
1991 a := s.expr(n.Left)
1992 if n.Left.Type.IsSlice() {
1993 return s.newValue1(ssa.OpSlicePtr, n.Type, a)
1994 } else {
1995 return s.newValue1(ssa.OpStringPtr, n.Type, a)
1996 }
1997
Keith Randalld1c15a02015-08-04 15:47:22 -07001998 case OITAB:
1999 a := s.expr(n.Left)
2000 return s.newValue1(ssa.OpITab, n.Type, a)
2001
Josh Bleecher Snyder1792b362015-09-05 19:28:27 -07002002 case OEFACE:
2003 tab := s.expr(n.Left)
2004 data := s.expr(n.Right)
Keith Randall808d7c72015-10-07 14:35:25 -07002005 // The frontend allows putting things like struct{*byte} in
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00002006 // the data portion of an eface. But we don't want struct{*byte}
Keith Randall808d7c72015-10-07 14:35:25 -07002007 // as a register type because (among other reasons) the liveness
2008 // analysis is confused by the "fat" variables that result from
2009 // such types being spilled.
2010 // So here we ensure that we are selecting the underlying pointer
2011 // when we build an eface.
Keith Randalla734bbc2016-01-11 21:05:33 -08002012 // TODO: get rid of this now that structs can be SSA'd?
Matthew Dempsky788f1122016-03-28 10:55:44 -07002013 for !data.Type.IsPtrShaped() {
Keith Randall808d7c72015-10-07 14:35:25 -07002014 switch {
2015 case data.Type.IsArray():
Matthew Dempsky0b281872016-03-10 14:35:39 -08002016 data = s.newValue1I(ssa.OpArrayIndex, data.Type.ElemType(), 0, data)
Keith Randall808d7c72015-10-07 14:35:25 -07002017 case data.Type.IsStruct():
2018 for i := data.Type.NumFields() - 1; i >= 0; i-- {
2019 f := data.Type.FieldType(i)
2020 if f.Size() == 0 {
2021 // eface type could also be struct{p *byte; q [0]int}
2022 continue
2023 }
Matthew Dempsky1b9f1682016-03-14 12:45:18 -07002024 data = s.newValue1I(ssa.OpStructSelect, f, int64(i), data)
Keith Randall808d7c72015-10-07 14:35:25 -07002025 break
2026 }
2027 default:
2028 s.Fatalf("type being put into an eface isn't a pointer")
2029 }
2030 }
Josh Bleecher Snyder1792b362015-09-05 19:28:27 -07002031 return s.newValue2(ssa.OpIMake, n.Type, tab, data)
2032
Keith Randall5505e8c2015-09-12 23:27:26 -07002033 case OSLICE, OSLICEARR:
2034 v := s.expr(n.Left)
2035 var i, j *ssa.Value
2036 if n.Right.Left != nil {
2037 i = s.extendIndex(s.expr(n.Right.Left))
2038 }
2039 if n.Right.Right != nil {
2040 j = s.extendIndex(s.expr(n.Right.Right))
2041 }
2042 p, l, c := s.slice(n.Left.Type, v, i, j, nil)
2043 return s.newValue3(ssa.OpSliceMake, n.Type, p, l, c)
Keith Randall3526cf52015-08-24 23:52:03 -07002044 case OSLICESTR:
Keith Randall5505e8c2015-09-12 23:27:26 -07002045 v := s.expr(n.Left)
2046 var i, j *ssa.Value
2047 if n.Right.Left != nil {
2048 i = s.extendIndex(s.expr(n.Right.Left))
Keith Randall3526cf52015-08-24 23:52:03 -07002049 }
Keith Randall5505e8c2015-09-12 23:27:26 -07002050 if n.Right.Right != nil {
2051 j = s.extendIndex(s.expr(n.Right.Right))
Keith Randall3526cf52015-08-24 23:52:03 -07002052 }
Keith Randall5505e8c2015-09-12 23:27:26 -07002053 p, l, _ := s.slice(n.Left.Type, v, i, j, nil)
2054 return s.newValue2(ssa.OpStringMake, n.Type, p, l)
2055 case OSLICE3, OSLICE3ARR:
2056 v := s.expr(n.Left)
2057 var i *ssa.Value
2058 if n.Right.Left != nil {
2059 i = s.extendIndex(s.expr(n.Right.Left))
Keith Randall3526cf52015-08-24 23:52:03 -07002060 }
Keith Randall5505e8c2015-09-12 23:27:26 -07002061 j := s.extendIndex(s.expr(n.Right.Right.Left))
2062 k := s.extendIndex(s.expr(n.Right.Right.Right))
2063 p, l, c := s.slice(n.Left.Type, v, i, j, k)
2064 return s.newValue3(ssa.OpSliceMake, n.Type, p, l, c)
Keith Randall3526cf52015-08-24 23:52:03 -07002065
David Chase8eec2bb2016-03-11 00:10:52 -05002066 case OCALLFUNC:
2067 if isIntrinsicCall1(n) {
2068 return s.intrinsicCall1(n)
2069 }
2070 fallthrough
2071
2072 case OCALLINTER, OCALLMETH:
Keith Randall5ba31942016-01-25 17:06:54 -08002073 a := s.call(n, callNormal)
2074 return s.newValue2(ssa.OpLoad, n.Type, a, s.mem())
Josh Bleecher Snyder3d23afb2015-08-12 11:22:16 -07002075
2076 case OGETG:
Keith Randalld694f832015-10-19 18:54:40 -07002077 return s.newValue1(ssa.OpGetG, n.Type, s.mem())
Josh Bleecher Snyder3d23afb2015-08-12 11:22:16 -07002078
Keith Randall9d22c102015-09-11 11:02:57 -07002079 case OAPPEND:
Josh Bleecher Snyder5e1b7bd2016-04-04 10:58:21 -07002080 return s.exprAppend(n)
Keith Randall9d22c102015-09-11 11:02:57 -07002081
Keith Randalld2fd43a2015-04-15 15:51:25 -07002082 default:
Josh Bleecher Snyder37ddc272015-06-24 14:03:39 -07002083 s.Unimplementedf("unhandled expr %s", opnames[n.Op])
Keith Randalld2fd43a2015-04-15 15:51:25 -07002084 return nil
2085 }
2086}
2087
Josh Bleecher Snyder5e1b7bd2016-04-04 10:58:21 -07002088// exprAppend converts an OAPPEND node n to an ssa.Value, adds it to s, and returns the Value.
2089func (s *state) exprAppend(n *Node) *ssa.Value {
2090 // append(s, e1, e2, e3). Compile like:
2091 // ptr,len,cap := s
2092 // newlen := len + 3
2093 // if newlen > s.cap {
2094 // ptr,_,cap = growslice(s, newlen)
2095 // }
2096 // *(ptr+len) = e1
2097 // *(ptr+len+1) = e2
2098 // *(ptr+len+2) = e3
2099 // makeslice(ptr,newlen,cap)
2100
2101 et := n.Type.Elem()
2102 pt := Ptrto(et)
2103
2104 // Evaluate slice
2105 slice := s.expr(n.List.First())
2106
2107 // Allocate new blocks
2108 grow := s.f.NewBlock(ssa.BlockPlain)
2109 assign := s.f.NewBlock(ssa.BlockPlain)
2110
2111 // Decide if we need to grow
2112 nargs := int64(n.List.Len() - 1)
2113 p := s.newValue1(ssa.OpSlicePtr, pt, slice)
2114 l := s.newValue1(ssa.OpSliceLen, Types[TINT], slice)
2115 c := s.newValue1(ssa.OpSliceCap, Types[TINT], slice)
2116 nl := s.newValue2(s.ssaOp(OADD, Types[TINT]), Types[TINT], l, s.constInt(Types[TINT], nargs))
2117 cmp := s.newValue2(s.ssaOp(OGT, Types[TINT]), Types[TBOOL], nl, c)
2118 s.vars[&ptrVar] = p
2119 s.vars[&capVar] = c
2120 b := s.endBlock()
2121 b.Kind = ssa.BlockIf
2122 b.Likely = ssa.BranchUnlikely
2123 b.SetControl(cmp)
2124 b.AddEdgeTo(grow)
2125 b.AddEdgeTo(assign)
2126
2127 // Call growslice
2128 s.startBlock(grow)
2129 taddr := s.newValue1A(ssa.OpAddr, Types[TUINTPTR], &ssa.ExternSymbol{Types[TUINTPTR], typenamesym(n.Type)}, s.sb)
2130
2131 r := s.rtcall(growslice, true, []*Type{pt, Types[TINT], Types[TINT]}, taddr, p, l, c, nl)
2132
2133 s.vars[&ptrVar] = r[0]
2134 // Note: we don't need to read r[1], the result's length. It will be nl.
2135 // (or maybe we should, we just have to spill/restore nl otherwise?)
2136 s.vars[&capVar] = r[2]
2137 b = s.endBlock()
2138 b.AddEdgeTo(assign)
2139
2140 // assign new elements to slots
2141 s.startBlock(assign)
2142
2143 // Evaluate args
2144 args := make([]*ssa.Value, 0, nargs)
2145 store := make([]bool, 0, nargs)
2146 for _, n := range n.List.Slice()[1:] {
2147 if canSSAType(n.Type) {
2148 args = append(args, s.expr(n))
2149 store = append(store, true)
2150 } else {
2151 args = append(args, s.addr(n, false))
2152 store = append(store, false)
2153 }
2154 }
2155
2156 p = s.variable(&ptrVar, pt) // generates phi for ptr
2157 c = s.variable(&capVar, Types[TINT]) // generates phi for cap
2158 p2 := s.newValue2(ssa.OpPtrIndex, pt, p, l)
2159 // TODO: just one write barrier call for all of these writes?
2160 // TODO: maybe just one writeBarrier.enabled check?
2161 for i, arg := range args {
2162 addr := s.newValue2(ssa.OpPtrIndex, pt, p2, s.constInt(Types[TINT], int64(i)))
2163 if store[i] {
2164 if haspointers(et) {
2165 s.insertWBstore(et, addr, arg, n.Lineno, 0)
2166 } else {
2167 s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, et.Size(), addr, arg, s.mem())
2168 }
2169 } else {
2170 if haspointers(et) {
2171 s.insertWBmove(et, addr, arg, n.Lineno)
2172 } else {
2173 s.vars[&memVar] = s.newValue3I(ssa.OpMove, ssa.TypeMem, et.Size(), addr, arg, s.mem())
2174 }
2175 }
2176 }
2177
2178 // make result
2179 delete(s.vars, &ptrVar)
2180 delete(s.vars, &capVar)
2181 return s.newValue3(ssa.OpSliceMake, n.Type, p, nl, c)
2182}
2183
Keith Randall99187312015-11-02 16:56:53 -08002184// condBranch evaluates the boolean expression cond and branches to yes
2185// if cond is true and no if cond is false.
2186// This function is intended to handle && and || better than just calling
2187// s.expr(cond) and branching on the result.
2188func (s *state) condBranch(cond *Node, yes, no *ssa.Block, likely int8) {
2189 if cond.Op == OANDAND {
2190 mid := s.f.NewBlock(ssa.BlockPlain)
2191 s.stmtList(cond.Ninit)
2192 s.condBranch(cond.Left, mid, no, max8(likely, 0))
2193 s.startBlock(mid)
2194 s.condBranch(cond.Right, yes, no, likely)
2195 return
2196 // Note: if likely==1, then both recursive calls pass 1.
2197 // If likely==-1, then we don't have enough information to decide
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00002198 // whether the first branch is likely or not. So we pass 0 for
Keith Randall99187312015-11-02 16:56:53 -08002199 // the likeliness of the first branch.
2200 // TODO: have the frontend give us branch prediction hints for
2201 // OANDAND and OOROR nodes (if it ever has such info).
2202 }
2203 if cond.Op == OOROR {
2204 mid := s.f.NewBlock(ssa.BlockPlain)
2205 s.stmtList(cond.Ninit)
2206 s.condBranch(cond.Left, yes, mid, min8(likely, 0))
2207 s.startBlock(mid)
2208 s.condBranch(cond.Right, yes, no, likely)
2209 return
2210 // Note: if likely==-1, then both recursive calls pass -1.
2211 // If likely==1, then we don't have enough info to decide
2212 // the likelihood of the first branch.
2213 }
Keith Randalld19bfc32015-11-03 09:30:17 -08002214 if cond.Op == ONOT {
2215 s.stmtList(cond.Ninit)
2216 s.condBranch(cond.Left, no, yes, -likely)
2217 return
2218 }
Keith Randall99187312015-11-02 16:56:53 -08002219 c := s.expr(cond)
2220 b := s.endBlock()
2221 b.Kind = ssa.BlockIf
Keith Randall56e0ecc2016-03-15 20:45:50 -07002222 b.SetControl(c)
Keith Randall99187312015-11-02 16:56:53 -08002223 b.Likely = ssa.BranchPrediction(likely) // gc and ssa both use -1/0/+1 for likeliness
2224 b.AddEdgeTo(yes)
2225 b.AddEdgeTo(no)
2226}
2227
Keith Randalld4663e12016-03-21 10:22:03 -07002228type skipMask uint8
2229
2230const (
2231 skipPtr skipMask = 1 << iota
2232 skipLen
2233 skipCap
2234)
2235
Keith Randall5ba31942016-01-25 17:06:54 -08002236// assign does left = right.
2237// Right has already been evaluated to ssa, left has not.
2238// If deref is true, then we do left = *right instead (and right has already been nil-checked).
2239// If deref is true and right == nil, just do left = 0.
2240// Include a write barrier if wb is true.
Keith Randalld4663e12016-03-21 10:22:03 -07002241// skip indicates assignments (at the top level) that can be avoided.
2242func (s *state) assign(left *Node, right *ssa.Value, wb, deref bool, line int32, skip skipMask) {
Keith Randalld4cc51d2015-08-14 21:47:20 -07002243 if left.Op == ONAME && isblank(left) {
Keith Randalld4cc51d2015-08-14 21:47:20 -07002244 return
2245 }
Keith Randalld4cc51d2015-08-14 21:47:20 -07002246 t := left.Type
2247 dowidth(t)
Keith Randall6a8a9da2016-02-27 17:49:31 -08002248 if s.canSSA(left) {
Keith Randall5ba31942016-01-25 17:06:54 -08002249 if deref {
2250 s.Fatalf("can SSA LHS %s but not RHS %s", left, right)
2251 }
Keith Randalla734bbc2016-01-11 21:05:33 -08002252 if left.Op == ODOT {
2253 // We're assigning to a field of an ssa-able value.
2254 // We need to build a new structure with the new value for the
2255 // field we're assigning and the old values for the other fields.
2256 // For instance:
2257 // type T struct {a, b, c int}
2258 // var T x
2259 // x.b = 5
2260 // For the x.b = 5 assignment we want to generate x = T{x.a, 5, x.c}
2261
2262 // Grab information about the structure type.
2263 t := left.Left.Type
2264 nf := t.NumFields()
2265 idx := fieldIdx(left)
2266
2267 // Grab old value of structure.
2268 old := s.expr(left.Left)
2269
2270 // Make new structure.
2271 new := s.newValue0(ssa.StructMakeOp(t.NumFields()), t)
2272
2273 // Add fields as args.
Matthew Dempsky1b9f1682016-03-14 12:45:18 -07002274 for i := 0; i < nf; i++ {
Keith Randalla734bbc2016-01-11 21:05:33 -08002275 if i == idx {
2276 new.AddArg(right)
2277 } else {
Matthew Dempsky1b9f1682016-03-14 12:45:18 -07002278 new.AddArg(s.newValue1I(ssa.OpStructSelect, t.FieldType(i), int64(i), old))
Keith Randalla734bbc2016-01-11 21:05:33 -08002279 }
2280 }
2281
2282 // Recursively assign the new value we've made to the base of the dot op.
Keith Randalld4663e12016-03-21 10:22:03 -07002283 s.assign(left.Left, new, false, false, line, 0)
Keith Randalla734bbc2016-01-11 21:05:33 -08002284 // TODO: do we need to update named values here?
2285 return
2286 }
Daniel Morsingc31b6dd2015-06-12 14:23:29 +01002287 // Update variable assignment.
Josh Bleecher Snyder07269312015-08-29 14:54:45 -07002288 s.vars[left] = right
Keith Randallc24681a2015-10-22 14:22:38 -07002289 s.addNamedValue(left, right)
Daniel Morsingc31b6dd2015-06-12 14:23:29 +01002290 return
2291 }
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00002292 // Left is not ssa-able. Compute its address.
David Chase57670ad2015-10-09 16:48:30 -04002293 addr := s.addr(left, false)
Keith Randalld2107fc2015-08-24 02:16:19 -07002294 if left.Op == ONAME {
Keith Randallb32217a2015-09-17 16:45:10 -07002295 s.vars[&memVar] = s.newValue1A(ssa.OpVarDef, ssa.TypeMem, left, s.mem())
Keith Randalld2107fc2015-08-24 02:16:19 -07002296 }
Keith Randall5ba31942016-01-25 17:06:54 -08002297 if deref {
2298 // Treat as a mem->mem move.
2299 if right == nil {
2300 s.vars[&memVar] = s.newValue2I(ssa.OpZero, ssa.TypeMem, t.Size(), addr, s.mem())
2301 return
2302 }
2303 if wb {
2304 s.insertWBmove(t, addr, right, line)
2305 return
2306 }
2307 s.vars[&memVar] = s.newValue3I(ssa.OpMove, ssa.TypeMem, t.Size(), addr, right, s.mem())
2308 return
Keith Randalle3869a62015-09-07 23:18:02 -07002309 }
Keith Randall5ba31942016-01-25 17:06:54 -08002310 // Treat as a store.
2311 if wb {
Keith Randalld4663e12016-03-21 10:22:03 -07002312 if skip&skipPtr != 0 {
2313 // Special case: if we don't write back the pointers, don't bother
2314 // doing the write barrier check.
2315 s.storeTypeScalars(t, addr, right, skip)
2316 return
2317 }
2318 s.insertWBstore(t, addr, right, line, skip)
2319 return
2320 }
2321 if skip != 0 {
2322 if skip&skipPtr == 0 {
2323 s.storeTypePtrs(t, addr, right)
2324 }
2325 s.storeTypeScalars(t, addr, right, skip)
Keith Randall5ba31942016-01-25 17:06:54 -08002326 return
2327 }
2328 s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, t.Size(), addr, right, s.mem())
Daniel Morsingc31b6dd2015-06-12 14:23:29 +01002329}
2330
Josh Bleecher Snyder21bd4832015-07-20 15:30:52 -07002331// zeroVal returns the zero value for type t.
2332func (s *state) zeroVal(t *Type) *ssa.Value {
2333 switch {
Keith Randall9cb332e2015-07-28 14:19:20 -07002334 case t.IsInteger():
2335 switch t.Size() {
2336 case 1:
2337 return s.constInt8(t, 0)
2338 case 2:
2339 return s.constInt16(t, 0)
2340 case 4:
2341 return s.constInt32(t, 0)
2342 case 8:
2343 return s.constInt64(t, 0)
2344 default:
2345 s.Fatalf("bad sized integer type %s", t)
2346 }
Todd Neal752fe4d2015-08-25 19:21:45 -05002347 case t.IsFloat():
2348 switch t.Size() {
2349 case 4:
2350 return s.constFloat32(t, 0)
2351 case 8:
2352 return s.constFloat64(t, 0)
2353 default:
2354 s.Fatalf("bad sized float type %s", t)
2355 }
David Chase52578582015-08-28 14:24:10 -04002356 case t.IsComplex():
2357 switch t.Size() {
2358 case 8:
2359 z := s.constFloat32(Types[TFLOAT32], 0)
Keith Randalla5cffb62015-08-28 13:52:26 -07002360 return s.entryNewValue2(ssa.OpComplexMake, t, z, z)
David Chase52578582015-08-28 14:24:10 -04002361 case 16:
2362 z := s.constFloat64(Types[TFLOAT64], 0)
Keith Randalla5cffb62015-08-28 13:52:26 -07002363 return s.entryNewValue2(ssa.OpComplexMake, t, z, z)
David Chase52578582015-08-28 14:24:10 -04002364 default:
2365 s.Fatalf("bad sized complex type %s", t)
2366 }
2367
Josh Bleecher Snyder21bd4832015-07-20 15:30:52 -07002368 case t.IsString():
Josh Bleecher Snyder39214272016-03-06 18:06:09 -08002369 return s.constEmptyString(t)
Matthew Dempsky788f1122016-03-28 10:55:44 -07002370 case t.IsPtrShaped():
Josh Bleecher Snyder39214272016-03-06 18:06:09 -08002371 return s.constNil(t)
Josh Bleecher Snyder21bd4832015-07-20 15:30:52 -07002372 case t.IsBoolean():
Josh Bleecher Snydercea44142015-09-08 16:52:25 -07002373 return s.constBool(false)
Keith Randall9f954db2015-08-18 10:26:28 -07002374 case t.IsInterface():
Josh Bleecher Snyder39214272016-03-06 18:06:09 -08002375 return s.constInterface(t)
Keith Randall9f954db2015-08-18 10:26:28 -07002376 case t.IsSlice():
Josh Bleecher Snyder39214272016-03-06 18:06:09 -08002377 return s.constSlice(t)
Keith Randalla734bbc2016-01-11 21:05:33 -08002378 case t.IsStruct():
2379 n := t.NumFields()
2380 v := s.entryNewValue0(ssa.StructMakeOp(t.NumFields()), t)
Matthew Dempsky1b9f1682016-03-14 12:45:18 -07002381 for i := 0; i < n; i++ {
Keith Randalla734bbc2016-01-11 21:05:33 -08002382 v.AddArg(s.zeroVal(t.FieldType(i).(*Type)))
2383 }
2384 return v
Josh Bleecher Snyder21bd4832015-07-20 15:30:52 -07002385 }
2386 s.Unimplementedf("zero for type %v not implemented", t)
2387 return nil
2388}
2389
Keith Randalld24768e2015-09-09 23:56:59 -07002390type callKind int8
2391
2392const (
2393 callNormal callKind = iota
2394 callDefer
2395 callGo
2396)
2397
David Chase8eec2bb2016-03-11 00:10:52 -05002398// isSSAIntrinsic1 returns true if n is a call to a recognized 1-arg intrinsic
2399// that can be handled by the SSA backend.
2400// SSA uses this, but so does the front end to see if should not
2401// inline a function because it is a candidate for intrinsic
2402// substitution.
2403func isSSAIntrinsic1(s *Sym) bool {
2404 // The test below is not quite accurate -- in the event that
2405 // a function is disabled on a per-function basis, for example
2406 // because of hash-keyed binary failure search, SSA might be
2407 // disabled for that function but it would not be noted here,
2408 // and thus an inlining would not occur (in practice, inlining
2409 // so far has only been noticed for Bswap32 and the 16-bit count
2410 // leading/trailing instructions, but heuristics might change
2411 // in the future or on different architectures).
2412 if !ssaEnabled || ssa.IntrinsicsDisable || Thearch.Thechar != '6' {
2413 return false
2414 }
2415 if s != nil && s.Pkg != nil && s.Pkg.Path == "runtime/internal/sys" {
2416 switch s.Name {
2417 case
2418 "Ctz64", "Ctz32", "Ctz16",
2419 "Bswap64", "Bswap32":
2420 return true
2421 }
2422 }
2423 return false
2424}
2425
2426func isIntrinsicCall1(n *Node) bool {
2427 if n == nil || n.Left == nil {
2428 return false
2429 }
2430 return isSSAIntrinsic1(n.Left.Sym)
2431}
2432
2433// intrinsicFirstArg extracts arg from n.List and eval
2434func (s *state) intrinsicFirstArg(n *Node) *ssa.Value {
2435 x := n.List.First()
2436 if x.Op == OAS {
2437 x = x.Right
2438 }
2439 return s.expr(x)
2440}
2441
2442// intrinsicCall1 converts a call to a recognized 1-arg intrinsic
2443// into the intrinsic
2444func (s *state) intrinsicCall1(n *Node) *ssa.Value {
2445 var result *ssa.Value
2446 switch n.Left.Sym.Name {
2447 case "Ctz64":
2448 result = s.newValue1(ssa.OpCtz64, Types[TUINT64], s.intrinsicFirstArg(n))
2449 case "Ctz32":
2450 result = s.newValue1(ssa.OpCtz32, Types[TUINT32], s.intrinsicFirstArg(n))
2451 case "Ctz16":
2452 result = s.newValue1(ssa.OpCtz16, Types[TUINT16], s.intrinsicFirstArg(n))
2453 case "Bswap64":
2454 result = s.newValue1(ssa.OpBswap64, Types[TUINT64], s.intrinsicFirstArg(n))
2455 case "Bswap32":
2456 result = s.newValue1(ssa.OpBswap32, Types[TUINT32], s.intrinsicFirstArg(n))
2457 }
2458 if result == nil {
2459 Fatalf("Unknown special call: %v", n.Left.Sym)
2460 }
2461 if ssa.IntrinsicsDebug > 0 {
2462 Warnl(n.Lineno, "intrinsic substitution for %v with %s", n.Left.Sym.Name, result.LongString())
2463 }
2464 return result
2465}
2466
Keith Randall5ba31942016-01-25 17:06:54 -08002467// Calls the function n using the specified call type.
2468// Returns the address of the return value (or nil if none).
Keith Randalld24768e2015-09-09 23:56:59 -07002469func (s *state) call(n *Node, k callKind) *ssa.Value {
2470 var sym *Sym // target symbol (if static)
2471 var closure *ssa.Value // ptr to closure to run (if dynamic)
2472 var codeptr *ssa.Value // ptr to target code (if dynamic)
2473 var rcvr *ssa.Value // receiver to set
2474 fn := n.Left
2475 switch n.Op {
2476 case OCALLFUNC:
2477 if k == callNormal && fn.Op == ONAME && fn.Class == PFUNC {
2478 sym = fn.Sym
2479 break
2480 }
2481 closure = s.expr(fn)
Keith Randalld24768e2015-09-09 23:56:59 -07002482 case OCALLMETH:
2483 if fn.Op != ODOTMETH {
2484 Fatalf("OCALLMETH: n.Left not an ODOTMETH: %v", fn)
2485 }
Keith Randalld24768e2015-09-09 23:56:59 -07002486 if k == callNormal {
Ian Lance Taylor5f525ca2016-03-18 16:52:30 -07002487 sym = fn.Sym
Keith Randalld24768e2015-09-09 23:56:59 -07002488 break
2489 }
Ian Lance Taylor5f525ca2016-03-18 16:52:30 -07002490 n2 := newname(fn.Sym)
Keith Randalld24768e2015-09-09 23:56:59 -07002491 n2.Class = PFUNC
Ian Lance Taylor5f525ca2016-03-18 16:52:30 -07002492 n2.Lineno = fn.Lineno
2493 closure = s.expr(n2)
Keith Randalld24768e2015-09-09 23:56:59 -07002494 // Note: receiver is already assigned in n.List, so we don't
2495 // want to set it here.
2496 case OCALLINTER:
2497 if fn.Op != ODOTINTER {
Matthew Dempskyc3dfad52016-03-07 08:23:55 -08002498 Fatalf("OCALLINTER: n.Left not an ODOTINTER: %v", Oconv(fn.Op, 0))
Keith Randalld24768e2015-09-09 23:56:59 -07002499 }
2500 i := s.expr(fn.Left)
2501 itab := s.newValue1(ssa.OpITab, Types[TUINTPTR], i)
2502 itabidx := fn.Xoffset + 3*int64(Widthptr) + 8 // offset of fun field in runtime.itab
2503 itab = s.newValue1I(ssa.OpOffPtr, Types[TUINTPTR], itabidx, itab)
2504 if k == callNormal {
2505 codeptr = s.newValue2(ssa.OpLoad, Types[TUINTPTR], itab, s.mem())
2506 } else {
2507 closure = itab
2508 }
2509 rcvr = s.newValue1(ssa.OpIData, Types[TUINTPTR], i)
2510 }
2511 dowidth(fn.Type)
Josh Bleecher Snyder361b3342016-03-28 14:31:57 -07002512 stksize := fn.Type.ArgWidth() // includes receiver
Keith Randalld24768e2015-09-09 23:56:59 -07002513
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00002514 // Run all argument assignments. The arg slots have already
Keith Randalld24768e2015-09-09 23:56:59 -07002515 // been offset by the appropriate amount (+2*widthptr for go/defer,
2516 // +widthptr for interface calls).
2517 // For OCALLMETH, the receiver is set in these statements.
2518 s.stmtList(n.List)
2519
2520 // Set receiver (for interface calls)
2521 if rcvr != nil {
Keith Randall7c4fbb62015-10-19 13:56:55 -07002522 argStart := Ctxt.FixedFrameSize()
Keith Randalld24768e2015-09-09 23:56:59 -07002523 if k != callNormal {
2524 argStart += int64(2 * Widthptr)
2525 }
2526 addr := s.entryNewValue1I(ssa.OpOffPtr, Types[TUINTPTR], argStart, s.sp)
Keith Randallb32217a2015-09-17 16:45:10 -07002527 s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, int64(Widthptr), addr, rcvr, s.mem())
Keith Randalld24768e2015-09-09 23:56:59 -07002528 }
2529
2530 // Defer/go args
2531 if k != callNormal {
2532 // Write argsize and closure (args to Newproc/Deferproc).
2533 argsize := s.constInt32(Types[TUINT32], int32(stksize))
Keith Randallb32217a2015-09-17 16:45:10 -07002534 s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, 4, s.sp, argsize, s.mem())
Keith Randalld24768e2015-09-09 23:56:59 -07002535 addr := s.entryNewValue1I(ssa.OpOffPtr, Ptrto(Types[TUINTPTR]), int64(Widthptr), s.sp)
Keith Randallb32217a2015-09-17 16:45:10 -07002536 s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, int64(Widthptr), addr, closure, s.mem())
Keith Randalld24768e2015-09-09 23:56:59 -07002537 stksize += 2 * int64(Widthptr)
2538 }
2539
2540 // call target
2541 bNext := s.f.NewBlock(ssa.BlockPlain)
2542 var call *ssa.Value
2543 switch {
2544 case k == callDefer:
2545 call = s.newValue1(ssa.OpDeferCall, ssa.TypeMem, s.mem())
2546 case k == callGo:
2547 call = s.newValue1(ssa.OpGoCall, ssa.TypeMem, s.mem())
2548 case closure != nil:
2549 codeptr = s.newValue2(ssa.OpLoad, Types[TUINTPTR], closure, s.mem())
2550 call = s.newValue3(ssa.OpClosureCall, ssa.TypeMem, codeptr, closure, s.mem())
2551 case codeptr != nil:
2552 call = s.newValue2(ssa.OpInterCall, ssa.TypeMem, codeptr, s.mem())
2553 case sym != nil:
2554 call = s.newValue1A(ssa.OpStaticCall, ssa.TypeMem, sym, s.mem())
2555 default:
2556 Fatalf("bad call type %s %v", opnames[n.Op], n)
2557 }
2558 call.AuxInt = stksize // Call operations carry the argsize of the callee along with them
2559
2560 // Finish call block
Keith Randallb32217a2015-09-17 16:45:10 -07002561 s.vars[&memVar] = call
Keith Randalld24768e2015-09-09 23:56:59 -07002562 b := s.endBlock()
2563 b.Kind = ssa.BlockCall
Keith Randall56e0ecc2016-03-15 20:45:50 -07002564 b.SetControl(call)
Keith Randalld24768e2015-09-09 23:56:59 -07002565 b.AddEdgeTo(bNext)
Keith Randallddc6b642016-03-09 19:27:57 -08002566 if k == callDefer {
2567 // Add recover edge to exit code.
2568 b.Kind = ssa.BlockDefer
2569 r := s.f.NewBlock(ssa.BlockPlain)
2570 s.startBlock(r)
2571 s.exit()
2572 b.AddEdgeTo(r)
2573 b.Likely = ssa.BranchLikely
2574 }
Keith Randalld24768e2015-09-09 23:56:59 -07002575
Keith Randall5ba31942016-01-25 17:06:54 -08002576 // Start exit block, find address of result.
Keith Randalld24768e2015-09-09 23:56:59 -07002577 s.startBlock(bNext)
Matthew Dempskyf6fab932016-03-15 11:06:03 -07002578 res := n.Left.Type.Results()
2579 if res.NumFields() == 0 || k != callNormal {
Keith Randalld24768e2015-09-09 23:56:59 -07002580 // call has no return value. Continue with the next statement.
2581 return nil
2582 }
Matthew Dempskyf6fab932016-03-15 11:06:03 -07002583 fp := res.Field(0)
Matthew Dempsky62dddd42016-03-28 09:40:53 -07002584 return s.entryNewValue1I(ssa.OpOffPtr, Ptrto(fp.Type), fp.Offset, s.sp)
Keith Randalld24768e2015-09-09 23:56:59 -07002585}
2586
Josh Bleecher Snyder95aff4d2015-07-28 14:31:25 -07002587// etypesign returns the signed-ness of e, for integer/pointer etypes.
2588// -1 means signed, +1 means unsigned, 0 means non-integer/non-pointer.
Keith Randall4304fbc2015-11-16 13:20:16 -08002589func etypesign(e EType) int8 {
Josh Bleecher Snyder95aff4d2015-07-28 14:31:25 -07002590 switch e {
2591 case TINT8, TINT16, TINT32, TINT64, TINT:
2592 return -1
2593 case TUINT8, TUINT16, TUINT32, TUINT64, TUINT, TUINTPTR, TUNSAFEPTR:
2594 return +1
2595 }
2596 return 0
2597}
2598
Todd Neald076ef72015-10-15 20:25:32 -05002599// lookupSymbol is used to retrieve the symbol (Extern, Arg or Auto) used for a particular node.
2600// This improves the effectiveness of cse by using the same Aux values for the
2601// same symbols.
2602func (s *state) lookupSymbol(n *Node, sym interface{}) interface{} {
2603 switch sym.(type) {
2604 default:
2605 s.Fatalf("sym %v is of uknown type %T", sym, sym)
2606 case *ssa.ExternSymbol, *ssa.ArgSymbol, *ssa.AutoSymbol:
2607 // these are the only valid types
2608 }
2609
2610 if lsym, ok := s.varsyms[n]; ok {
2611 return lsym
2612 } else {
2613 s.varsyms[n] = sym
2614 return sym
2615 }
2616}
2617
Josh Bleecher Snydere00d6092015-06-02 09:16:22 -07002618// addr converts the address of the expression n to SSA, adds it to s and returns the SSA result.
Keith Randallc3c84a22015-07-13 15:55:37 -07002619// The value that the returned Value represents is guaranteed to be non-nil.
David Chase57670ad2015-10-09 16:48:30 -04002620// If bounded is true then this address does not require a nil check for its operand
2621// even if that would otherwise be implied.
2622func (s *state) addr(n *Node, bounded bool) *ssa.Value {
Keith Randallc24681a2015-10-22 14:22:38 -07002623 t := Ptrto(n.Type)
Keith Randallcfc2aa52015-05-18 16:44:20 -07002624 switch n.Op {
2625 case ONAME:
Keith Randall290d8fc2015-06-10 15:03:06 -07002626 switch n.Class {
2627 case PEXTERN:
Keith Randallcfc2aa52015-05-18 16:44:20 -07002628 // global variable
Todd Neal74180dd2015-10-27 21:35:48 -05002629 aux := s.lookupSymbol(n, &ssa.ExternSymbol{n.Type, n.Sym})
Keith Randallc24681a2015-10-22 14:22:38 -07002630 v := s.entryNewValue1A(ssa.OpAddr, t, aux, s.sb)
Josh Bleecher Snyder67df7932015-07-28 11:08:44 -07002631 // TODO: Make OpAddr use AuxInt as well as Aux.
2632 if n.Xoffset != 0 {
2633 v = s.entryNewValue1I(ssa.OpOffPtr, v.Type, n.Xoffset, v)
2634 }
2635 return v
David Chase956f3192015-09-11 16:40:05 -04002636 case PPARAM:
2637 // parameter slot
Josh Bleecher Snyder596ddf42015-06-29 11:56:28 -07002638 v := s.decladdrs[n]
Keith Randall4304fbc2015-11-16 13:20:16 -08002639 if v != nil {
2640 return v
Josh Bleecher Snyder596ddf42015-06-29 11:56:28 -07002641 }
Keith Randall4304fbc2015-11-16 13:20:16 -08002642 if n.String() == ".fp" {
2643 // Special arg that points to the frame pointer.
2644 // (Used by the race detector, others?)
2645 aux := s.lookupSymbol(n, &ssa.ArgSymbol{Typ: n.Type, Node: n})
2646 return s.entryNewValue1A(ssa.OpAddr, t, aux, s.sp)
2647 }
2648 s.Fatalf("addr of undeclared ONAME %v. declared: %v", n, s.decladdrs)
2649 return nil
Keith Randalld2107fc2015-08-24 02:16:19 -07002650 case PAUTO:
Todd Neal40bfec02016-03-11 20:03:17 -06002651 aux := s.lookupSymbol(n, &ssa.AutoSymbol{Typ: n.Type, Node: n})
Keith Randallc24681a2015-10-22 14:22:38 -07002652 return s.newValue1A(ssa.OpAddr, t, aux, s.sp)
David Chase956f3192015-09-11 16:40:05 -04002653 case PPARAMOUT: // Same as PAUTO -- cannot generate LEA early.
Todd Neald076ef72015-10-15 20:25:32 -05002654 // ensure that we reuse symbols for out parameters so
2655 // that cse works on their addresses
2656 aux := s.lookupSymbol(n, &ssa.ArgSymbol{Typ: n.Type, Node: n})
Keith Randallc24681a2015-10-22 14:22:38 -07002657 return s.newValue1A(ssa.OpAddr, t, aux, s.sp)
David Chase956f3192015-09-11 16:40:05 -04002658 case PAUTO | PHEAP, PPARAM | PHEAP, PPARAMOUT | PHEAP, PPARAMREF:
Daniel Morsingc31b6dd2015-06-12 14:23:29 +01002659 return s.expr(n.Name.Heapaddr)
Keith Randall290d8fc2015-06-10 15:03:06 -07002660 default:
Josh Bleecher Snyder58446032015-08-23 20:29:43 -07002661 s.Unimplementedf("variable address class %v not implemented", n.Class)
Keith Randall290d8fc2015-06-10 15:03:06 -07002662 return nil
Keith Randallcfc2aa52015-05-18 16:44:20 -07002663 }
Keith Randallcfc2aa52015-05-18 16:44:20 -07002664 case OINDREG:
Josh Bleecher Snyder25d19162015-07-28 12:37:46 -07002665 // indirect off a register
Keith Randallcfc2aa52015-05-18 16:44:20 -07002666 // used for storing/loading arguments/returns to/from callees
Josh Bleecher Snyder25d19162015-07-28 12:37:46 -07002667 if int(n.Reg) != Thearch.REGSP {
2668 s.Unimplementedf("OINDREG of non-SP register %s in addr: %v", obj.Rconv(int(n.Reg)), n)
2669 return nil
2670 }
Keith Randallc24681a2015-10-22 14:22:38 -07002671 return s.entryNewValue1I(ssa.OpOffPtr, t, n.Xoffset, s.sp)
Keith Randallcfc2aa52015-05-18 16:44:20 -07002672 case OINDEX:
Brad Fitzpatrick7af53d92015-07-10 10:47:28 -06002673 if n.Left.Type.IsSlice() {
Keith Randallcfc2aa52015-05-18 16:44:20 -07002674 a := s.expr(n.Left)
2675 i := s.expr(n.Right)
Keith Randall2a5e6c42015-07-23 14:35:02 -07002676 i = s.extendIndex(i)
Keith Randallc24681a2015-10-22 14:22:38 -07002677 len := s.newValue1(ssa.OpSliceLen, Types[TINT], a)
Keith Randall46e62f82015-08-18 14:17:30 -07002678 if !n.Bounded {
2679 s.boundsCheck(i, len)
2680 }
Keith Randallc24681a2015-10-22 14:22:38 -07002681 p := s.newValue1(ssa.OpSlicePtr, t, a)
2682 return s.newValue2(ssa.OpPtrIndex, t, p, i)
Brad Fitzpatrick7af53d92015-07-10 10:47:28 -06002683 } else { // array
David Chase57670ad2015-10-09 16:48:30 -04002684 a := s.addr(n.Left, bounded)
Brad Fitzpatrick7af53d92015-07-10 10:47:28 -06002685 i := s.expr(n.Right)
Keith Randall2a5e6c42015-07-23 14:35:02 -07002686 i = s.extendIndex(i)
Josh Bleecher Snyder3a0783c2016-03-31 14:46:04 -07002687 len := s.constInt(Types[TINT], n.Left.Type.NumElem())
Keith Randall46e62f82015-08-18 14:17:30 -07002688 if !n.Bounded {
2689 s.boundsCheck(i, len)
2690 }
Josh Bleecher Snyder8640b512016-03-30 10:57:47 -07002691 return s.newValue2(ssa.OpPtrIndex, Ptrto(n.Left.Type.Elem()), a, i)
Keith Randallcfc2aa52015-05-18 16:44:20 -07002692 }
Todd Nealb383de22015-07-13 21:22:16 -05002693 case OIND:
2694 p := s.expr(n.Left)
David Chase57670ad2015-10-09 16:48:30 -04002695 if !bounded {
2696 s.nilCheck(p)
2697 }
Todd Nealb383de22015-07-13 21:22:16 -05002698 return p
Keith Randallc3c84a22015-07-13 15:55:37 -07002699 case ODOT:
David Chase57670ad2015-10-09 16:48:30 -04002700 p := s.addr(n.Left, bounded)
Josh Bleecher Snyderda1802f2016-03-04 12:34:43 -08002701 return s.newValue1I(ssa.OpOffPtr, t, n.Xoffset, p)
Keith Randallc3c84a22015-07-13 15:55:37 -07002702 case ODOTPTR:
2703 p := s.expr(n.Left)
David Chase57670ad2015-10-09 16:48:30 -04002704 if !bounded {
2705 s.nilCheck(p)
2706 }
Josh Bleecher Snyderda1802f2016-03-04 12:34:43 -08002707 return s.newValue1I(ssa.OpOffPtr, t, n.Xoffset, p)
David Chase956f3192015-09-11 16:40:05 -04002708 case OCLOSUREVAR:
Josh Bleecher Snyderda1802f2016-03-04 12:34:43 -08002709 return s.newValue1I(ssa.OpOffPtr, t, n.Xoffset,
2710 s.entryNewValue0(ssa.OpGetClosurePtr, Ptrto(Types[TUINT8])))
David Chase32ffbf72015-10-08 17:14:12 -04002711 case OPARAM:
2712 p := n.Left
2713 if p.Op != ONAME || !(p.Class == PPARAM|PHEAP || p.Class == PPARAMOUT|PHEAP) {
2714 s.Fatalf("OPARAM not of ONAME,{PPARAM,PPARAMOUT}|PHEAP, instead %s", nodedump(p, 0))
2715 }
2716
2717 // Recover original offset to address passed-in param value.
2718 original_p := *p
2719 original_p.Xoffset = n.Xoffset
2720 aux := &ssa.ArgSymbol{Typ: n.Type, Node: &original_p}
Keith Randallc24681a2015-10-22 14:22:38 -07002721 return s.entryNewValue1A(ssa.OpAddr, t, aux, s.sp)
David Chase57670ad2015-10-09 16:48:30 -04002722 case OCONVNOP:
2723 addr := s.addr(n.Left, bounded)
Keith Randallc24681a2015-10-22 14:22:38 -07002724 return s.newValue1(ssa.OpCopy, t, addr) // ensure that addr has the right type
Keith Randall5ba31942016-01-25 17:06:54 -08002725 case OCALLFUNC, OCALLINTER, OCALLMETH:
2726 return s.call(n, callNormal)
David Chase57670ad2015-10-09 16:48:30 -04002727
Keith Randallcfc2aa52015-05-18 16:44:20 -07002728 default:
Matthew Dempskyc3dfad52016-03-07 08:23:55 -08002729 s.Unimplementedf("unhandled addr %v", Oconv(n.Op, 0))
Keith Randallcfc2aa52015-05-18 16:44:20 -07002730 return nil
2731 }
2732}
2733
Keith Randall290d8fc2015-06-10 15:03:06 -07002734// canSSA reports whether n is SSA-able.
Keith Randalla734bbc2016-01-11 21:05:33 -08002735// n must be an ONAME (or an ODOT sequence with an ONAME base).
Keith Randall6a8a9da2016-02-27 17:49:31 -08002736func (s *state) canSSA(n *Node) bool {
Keith Randalla734bbc2016-01-11 21:05:33 -08002737 for n.Op == ODOT {
2738 n = n.Left
2739 }
Keith Randall290d8fc2015-06-10 15:03:06 -07002740 if n.Op != ONAME {
Daniel Morsing66b47812015-06-27 15:45:20 +01002741 return false
Keith Randall290d8fc2015-06-10 15:03:06 -07002742 }
2743 if n.Addrtaken {
2744 return false
2745 }
2746 if n.Class&PHEAP != 0 {
2747 return false
2748 }
Josh Bleecher Snyder96548732015-08-28 13:35:32 -07002749 switch n.Class {
Keith Randall6a8a9da2016-02-27 17:49:31 -08002750 case PEXTERN, PPARAMREF:
2751 // TODO: maybe treat PPARAMREF with an Arg-like op to read from closure?
Keith Randall290d8fc2015-06-10 15:03:06 -07002752 return false
Keith Randall6a8a9da2016-02-27 17:49:31 -08002753 case PPARAMOUT:
2754 if hasdefer {
2755 // TODO: handle this case? Named return values must be
2756 // in memory so that the deferred function can see them.
2757 // Maybe do: if !strings.HasPrefix(n.String(), "~") { return false }
2758 return false
2759 }
2760 if s.cgoUnsafeArgs {
2761 // Cgo effectively takes the address of all result args,
2762 // but the compiler can't see that.
2763 return false
2764 }
Keith Randall290d8fc2015-06-10 15:03:06 -07002765 }
Keith Randall8a1f6212015-09-08 21:28:44 -07002766 if n.Class == PPARAM && n.String() == ".this" {
2767 // wrappers generated by genwrapper need to update
2768 // the .this pointer in place.
Keith Randall6a8a9da2016-02-27 17:49:31 -08002769 // TODO: treat as a PPARMOUT?
Keith Randall8a1f6212015-09-08 21:28:44 -07002770 return false
2771 }
Keith Randall9f954db2015-08-18 10:26:28 -07002772 return canSSAType(n.Type)
2773 // TODO: try to make more variables SSAable?
2774}
2775
2776// canSSA reports whether variables of type t are SSA-able.
2777func canSSAType(t *Type) bool {
2778 dowidth(t)
2779 if t.Width > int64(4*Widthptr) {
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00002780 // 4*Widthptr is an arbitrary constant. We want it
Keith Randall9f954db2015-08-18 10:26:28 -07002781 // to be at least 3*Widthptr so slices can be registerized.
2782 // Too big and we'll introduce too much register pressure.
Daniel Morsing66b47812015-06-27 15:45:20 +01002783 return false
2784 }
Keith Randall9f954db2015-08-18 10:26:28 -07002785 switch t.Etype {
2786 case TARRAY:
Matthew Dempsky1624a9c2016-03-30 14:45:47 -07002787 if t.IsSlice() {
Keith Randall9f954db2015-08-18 10:26:28 -07002788 return true
2789 }
2790 // We can't do arrays because dynamic indexing is
2791 // not supported on SSA variables.
2792 // TODO: maybe allow if length is <=1? All indexes
2793 // are constant? Might be good for the arrays
2794 // introduced by the compiler for variadic functions.
2795 return false
2796 case TSTRUCT:
Matthew Dempskydbed1c62016-03-17 13:26:08 -07002797 if t.NumFields() > ssa.MaxStruct {
Keith Randall9f954db2015-08-18 10:26:28 -07002798 return false
2799 }
Matthew Dempskyf6bca3f2016-03-17 01:32:18 -07002800 for _, t1 := range t.Fields().Slice() {
Keith Randall9f954db2015-08-18 10:26:28 -07002801 if !canSSAType(t1.Type) {
2802 return false
2803 }
2804 }
Keith Randalla734bbc2016-01-11 21:05:33 -08002805 return true
Keith Randall9f954db2015-08-18 10:26:28 -07002806 default:
2807 return true
2808 }
Keith Randall290d8fc2015-06-10 15:03:06 -07002809}
2810
Keith Randallcfc2aa52015-05-18 16:44:20 -07002811// nilCheck generates nil pointer checking code.
Josh Bleecher Snyder463858e2015-08-11 09:47:45 -07002812// Starts a new block on return, unless nil checks are disabled.
Josh Bleecher Snyder7e74e432015-07-24 11:55:52 -07002813// Used only for automatically inserted nil checks,
2814// not for user code like 'x != nil'.
Keith Randallcfc2aa52015-05-18 16:44:20 -07002815func (s *state) nilCheck(ptr *ssa.Value) {
Josh Bleecher Snyder463858e2015-08-11 09:47:45 -07002816 if Disable_checknil != 0 {
2817 return
2818 }
Keith Randall31115a52015-10-23 19:12:49 -07002819 chk := s.newValue2(ssa.OpNilCheck, ssa.TypeVoid, ptr, s.mem())
Keith Randallcfc2aa52015-05-18 16:44:20 -07002820 b := s.endBlock()
Keith Randall31115a52015-10-23 19:12:49 -07002821 b.Kind = ssa.BlockCheck
Keith Randall56e0ecc2016-03-15 20:45:50 -07002822 b.SetControl(chk)
Keith Randallcfc2aa52015-05-18 16:44:20 -07002823 bNext := s.f.NewBlock(ssa.BlockPlain)
Todd Neal47d67992015-08-28 21:36:29 -05002824 b.AddEdgeTo(bNext)
Josh Bleecher Snyder463858e2015-08-11 09:47:45 -07002825 s.startBlock(bNext)
Keith Randallcfc2aa52015-05-18 16:44:20 -07002826}
2827
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00002828// boundsCheck generates bounds checking code. Checks if 0 <= idx < len, branches to exit if not.
Keith Randallcfc2aa52015-05-18 16:44:20 -07002829// Starts a new block on return.
2830func (s *state) boundsCheck(idx, len *ssa.Value) {
Keith Randall8d236812015-08-18 15:25:40 -07002831 if Debug['B'] != 0 {
2832 return
2833 }
Keith Randallcfc2aa52015-05-18 16:44:20 -07002834 // TODO: convert index to full width?
2835 // TODO: if index is 64-bit and we're compiling to 32-bit, check that high 32 bits are zero.
2836
2837 // bounds check
Josh Bleecher Snyder85e03292015-07-30 11:03:05 -07002838 cmp := s.newValue2(ssa.OpIsInBounds, Types[TBOOL], idx, len)
Keith Randall3a70bf92015-09-17 16:54:15 -07002839 s.check(cmp, Panicindex)
Keith Randall3526cf52015-08-24 23:52:03 -07002840}
2841
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00002842// sliceBoundsCheck generates slice bounds checking code. Checks if 0 <= idx <= len, branches to exit if not.
Keith Randall3526cf52015-08-24 23:52:03 -07002843// Starts a new block on return.
2844func (s *state) sliceBoundsCheck(idx, len *ssa.Value) {
2845 if Debug['B'] != 0 {
2846 return
2847 }
2848 // TODO: convert index to full width?
2849 // TODO: if index is 64-bit and we're compiling to 32-bit, check that high 32 bits are zero.
2850
2851 // bounds check
2852 cmp := s.newValue2(ssa.OpIsSliceInBounds, Types[TBOOL], idx, len)
Keith Randall3a70bf92015-09-17 16:54:15 -07002853 s.check(cmp, panicslice)
Keith Randall3526cf52015-08-24 23:52:03 -07002854}
2855
Keith Randall3a70bf92015-09-17 16:54:15 -07002856// If cmp (a bool) is true, panic using the given function.
2857func (s *state) check(cmp *ssa.Value, fn *Node) {
Keith Randallcfc2aa52015-05-18 16:44:20 -07002858 b := s.endBlock()
2859 b.Kind = ssa.BlockIf
Keith Randall56e0ecc2016-03-15 20:45:50 -07002860 b.SetControl(cmp)
Josh Bleecher Snyderbbf8c5c2015-08-11 17:28:56 -07002861 b.Likely = ssa.BranchLikely
Keith Randallcfc2aa52015-05-18 16:44:20 -07002862 bNext := s.f.NewBlock(ssa.BlockPlain)
Keith Randall74e568f2015-11-09 21:35:40 -08002863 line := s.peekLine()
2864 bPanic := s.panics[funcLine{fn, line}]
2865 if bPanic == nil {
2866 bPanic = s.f.NewBlock(ssa.BlockPlain)
2867 s.panics[funcLine{fn, line}] = bPanic
2868 s.startBlock(bPanic)
2869 // The panic call takes/returns memory to ensure that the right
2870 // memory state is observed if the panic happens.
2871 s.rtcall(fn, false, nil)
2872 }
Todd Neal47d67992015-08-28 21:36:29 -05002873 b.AddEdgeTo(bNext)
2874 b.AddEdgeTo(bPanic)
Keith Randallcfc2aa52015-05-18 16:44:20 -07002875 s.startBlock(bNext)
2876}
2877
Keith Randall8c5bfcc2015-09-18 15:11:30 -07002878// rtcall issues a call to the given runtime function fn with the listed args.
2879// Returns a slice of results of the given result types.
2880// The call is added to the end of the current block.
2881// If returns is false, the block is marked as an exit block.
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00002882// If returns is true, the block is marked as a call block. A new block
Keith Randall8c5bfcc2015-09-18 15:11:30 -07002883// is started to load the return values.
2884func (s *state) rtcall(fn *Node, returns bool, results []*Type, args ...*ssa.Value) []*ssa.Value {
2885 // Write args to the stack
2886 var off int64 // TODO: arch-dependent starting offset?
2887 for _, arg := range args {
2888 t := arg.Type
2889 off = Rnd(off, t.Alignment())
2890 ptr := s.sp
2891 if off != 0 {
2892 ptr = s.newValue1I(ssa.OpOffPtr, Types[TUINTPTR], off, s.sp)
2893 }
2894 size := t.Size()
2895 s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, size, ptr, arg, s.mem())
2896 off += size
2897 }
2898 off = Rnd(off, int64(Widthptr))
2899
2900 // Issue call
2901 call := s.newValue1A(ssa.OpStaticCall, ssa.TypeMem, fn.Sym, s.mem())
2902 s.vars[&memVar] = call
2903
2904 // Finish block
2905 b := s.endBlock()
2906 if !returns {
2907 b.Kind = ssa.BlockExit
Keith Randall56e0ecc2016-03-15 20:45:50 -07002908 b.SetControl(call)
Keith Randall8c5bfcc2015-09-18 15:11:30 -07002909 call.AuxInt = off
2910 if len(results) > 0 {
2911 Fatalf("panic call can't have results")
2912 }
2913 return nil
2914 }
2915 b.Kind = ssa.BlockCall
Keith Randall56e0ecc2016-03-15 20:45:50 -07002916 b.SetControl(call)
Keith Randall8c5bfcc2015-09-18 15:11:30 -07002917 bNext := s.f.NewBlock(ssa.BlockPlain)
2918 b.AddEdgeTo(bNext)
2919 s.startBlock(bNext)
2920
2921 // Load results
2922 res := make([]*ssa.Value, len(results))
2923 for i, t := range results {
2924 off = Rnd(off, t.Alignment())
2925 ptr := s.sp
2926 if off != 0 {
2927 ptr = s.newValue1I(ssa.OpOffPtr, Types[TUINTPTR], off, s.sp)
2928 }
2929 res[i] = s.newValue2(ssa.OpLoad, t, ptr, s.mem())
2930 off += t.Size()
2931 }
2932 off = Rnd(off, int64(Widthptr))
2933
2934 // Remember how much callee stack space we needed.
2935 call.AuxInt = off
2936
2937 return res
2938}
2939
Keith Randall5ba31942016-01-25 17:06:54 -08002940// insertWBmove inserts the assignment *left = *right including a write barrier.
2941// t is the type being assigned.
2942func (s *state) insertWBmove(t *Type, left, right *ssa.Value, line int32) {
Keith Randall4304fbc2015-11-16 13:20:16 -08002943 // if writeBarrier.enabled {
Keith Randall5ba31942016-01-25 17:06:54 -08002944 // typedmemmove(&t, left, right)
2945 // } else {
2946 // *left = *right
Keith Randall9d22c102015-09-11 11:02:57 -07002947 // }
Keith Randall15ed37d2016-03-16 21:51:17 -07002948
2949 if s.noWB {
2950 s.Fatalf("write barrier prohibited")
2951 }
2952 if s.WBLineno == 0 {
2953 s.WBLineno = left.Line
2954 }
Keith Randall9d22c102015-09-11 11:02:57 -07002955 bThen := s.f.NewBlock(ssa.BlockPlain)
Keith Randall5ba31942016-01-25 17:06:54 -08002956 bElse := s.f.NewBlock(ssa.BlockPlain)
2957 bEnd := s.f.NewBlock(ssa.BlockPlain)
Keith Randall9d22c102015-09-11 11:02:57 -07002958
Matthew Dempskydafbcf62016-03-04 15:19:06 -08002959 aux := &ssa.ExternSymbol{Types[TBOOL], syslook("writeBarrier").Sym}
David Chase8107b002016-02-28 11:15:22 -05002960 flagaddr := s.newValue1A(ssa.OpAddr, Ptrto(Types[TUINT32]), aux, s.sb)
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00002961 // TODO: select the .enabled field. It is currently first, so not needed for now.
David Chase8107b002016-02-28 11:15:22 -05002962 // Load word, test byte, avoiding partial register write from load byte.
2963 flag := s.newValue2(ssa.OpLoad, Types[TUINT32], flagaddr, s.mem())
2964 flag = s.newValue1(ssa.OpTrunc64to8, Types[TBOOL], flag)
Keith Randall9d22c102015-09-11 11:02:57 -07002965 b := s.endBlock()
2966 b.Kind = ssa.BlockIf
2967 b.Likely = ssa.BranchUnlikely
Keith Randall56e0ecc2016-03-15 20:45:50 -07002968 b.SetControl(flag)
Keith Randall9d22c102015-09-11 11:02:57 -07002969 b.AddEdgeTo(bThen)
Keith Randall5ba31942016-01-25 17:06:54 -08002970 b.AddEdgeTo(bElse)
Keith Randall9d22c102015-09-11 11:02:57 -07002971
2972 s.startBlock(bThen)
Keith Randall9d22c102015-09-11 11:02:57 -07002973 taddr := s.newValue1A(ssa.OpAddr, Types[TUINTPTR], &ssa.ExternSymbol{Types[TUINTPTR], typenamesym(t)}, s.sb)
Keith Randall5ba31942016-01-25 17:06:54 -08002974 s.rtcall(typedmemmove, true, nil, taddr, left, right)
2975 s.endBlock().AddEdgeTo(bEnd)
2976
2977 s.startBlock(bElse)
2978 s.vars[&memVar] = s.newValue3I(ssa.OpMove, ssa.TypeMem, t.Size(), left, right, s.mem())
2979 s.endBlock().AddEdgeTo(bEnd)
2980
2981 s.startBlock(bEnd)
Keith Randall9d22c102015-09-11 11:02:57 -07002982
David Chase729abfa2015-10-26 17:34:06 -04002983 if Debug_wb > 0 {
Robert Griesemerb83f3972016-03-02 11:01:25 -08002984 Warnl(line, "write barrier")
David Chase729abfa2015-10-26 17:34:06 -04002985 }
Keith Randall5ba31942016-01-25 17:06:54 -08002986}
David Chase729abfa2015-10-26 17:34:06 -04002987
Keith Randall5ba31942016-01-25 17:06:54 -08002988// insertWBstore inserts the assignment *left = right including a write barrier.
2989// t is the type being assigned.
Keith Randalld4663e12016-03-21 10:22:03 -07002990func (s *state) insertWBstore(t *Type, left, right *ssa.Value, line int32, skip skipMask) {
Keith Randall5ba31942016-01-25 17:06:54 -08002991 // store scalar fields
2992 // if writeBarrier.enabled {
2993 // writebarrierptr for pointer fields
2994 // } else {
2995 // store pointer fields
2996 // }
2997
Keith Randall15ed37d2016-03-16 21:51:17 -07002998 if s.noWB {
2999 s.Fatalf("write barrier prohibited")
3000 }
3001 if s.WBLineno == 0 {
3002 s.WBLineno = left.Line
3003 }
Keith Randalld4663e12016-03-21 10:22:03 -07003004 s.storeTypeScalars(t, left, right, skip)
Keith Randall5ba31942016-01-25 17:06:54 -08003005
3006 bThen := s.f.NewBlock(ssa.BlockPlain)
3007 bElse := s.f.NewBlock(ssa.BlockPlain)
3008 bEnd := s.f.NewBlock(ssa.BlockPlain)
3009
Matthew Dempskydafbcf62016-03-04 15:19:06 -08003010 aux := &ssa.ExternSymbol{Types[TBOOL], syslook("writeBarrier").Sym}
David Chase8107b002016-02-28 11:15:22 -05003011 flagaddr := s.newValue1A(ssa.OpAddr, Ptrto(Types[TUINT32]), aux, s.sb)
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00003012 // TODO: select the .enabled field. It is currently first, so not needed for now.
David Chase8107b002016-02-28 11:15:22 -05003013 // Load word, test byte, avoiding partial register write from load byte.
3014 flag := s.newValue2(ssa.OpLoad, Types[TUINT32], flagaddr, s.mem())
3015 flag = s.newValue1(ssa.OpTrunc64to8, Types[TBOOL], flag)
Keith Randall5ba31942016-01-25 17:06:54 -08003016 b := s.endBlock()
3017 b.Kind = ssa.BlockIf
3018 b.Likely = ssa.BranchUnlikely
Keith Randall56e0ecc2016-03-15 20:45:50 -07003019 b.SetControl(flag)
Keith Randall5ba31942016-01-25 17:06:54 -08003020 b.AddEdgeTo(bThen)
3021 b.AddEdgeTo(bElse)
3022
3023 // Issue write barriers for pointer writes.
3024 s.startBlock(bThen)
Keith Randallaebf6612016-01-29 21:57:57 -08003025 s.storeTypePtrsWB(t, left, right)
3026 s.endBlock().AddEdgeTo(bEnd)
3027
3028 // Issue regular stores for pointer writes.
3029 s.startBlock(bElse)
3030 s.storeTypePtrs(t, left, right)
3031 s.endBlock().AddEdgeTo(bEnd)
3032
3033 s.startBlock(bEnd)
3034
3035 if Debug_wb > 0 {
Robert Griesemerb83f3972016-03-02 11:01:25 -08003036 Warnl(line, "write barrier")
Keith Randallaebf6612016-01-29 21:57:57 -08003037 }
3038}
3039
3040// do *left = right for all scalar (non-pointer) parts of t.
Keith Randalld4663e12016-03-21 10:22:03 -07003041func (s *state) storeTypeScalars(t *Type, left, right *ssa.Value, skip skipMask) {
Keith Randallaebf6612016-01-29 21:57:57 -08003042 switch {
3043 case t.IsBoolean() || t.IsInteger() || t.IsFloat() || t.IsComplex():
3044 s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, t.Size(), left, right, s.mem())
Matthew Dempsky788f1122016-03-28 10:55:44 -07003045 case t.IsPtrShaped():
Keith Randallaebf6612016-01-29 21:57:57 -08003046 // no scalar fields.
3047 case t.IsString():
Keith Randalld4663e12016-03-21 10:22:03 -07003048 if skip&skipLen != 0 {
3049 return
3050 }
Keith Randallaebf6612016-01-29 21:57:57 -08003051 len := s.newValue1(ssa.OpStringLen, Types[TINT], right)
3052 lenAddr := s.newValue1I(ssa.OpOffPtr, Ptrto(Types[TINT]), s.config.IntSize, left)
3053 s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, s.config.IntSize, lenAddr, len, s.mem())
3054 case t.IsSlice():
Keith Randalld4663e12016-03-21 10:22:03 -07003055 if skip&skipLen == 0 {
3056 len := s.newValue1(ssa.OpSliceLen, Types[TINT], right)
3057 lenAddr := s.newValue1I(ssa.OpOffPtr, Ptrto(Types[TINT]), s.config.IntSize, left)
3058 s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, s.config.IntSize, lenAddr, len, s.mem())
3059 }
3060 if skip&skipCap == 0 {
3061 cap := s.newValue1(ssa.OpSliceCap, Types[TINT], right)
3062 capAddr := s.newValue1I(ssa.OpOffPtr, Ptrto(Types[TINT]), 2*s.config.IntSize, left)
3063 s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, s.config.IntSize, capAddr, cap, s.mem())
3064 }
Keith Randallaebf6612016-01-29 21:57:57 -08003065 case t.IsInterface():
3066 // itab field doesn't need a write barrier (even though it is a pointer).
3067 itab := s.newValue1(ssa.OpITab, Ptrto(Types[TUINT8]), right)
3068 s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, s.config.IntSize, left, itab, s.mem())
3069 case t.IsStruct():
3070 n := t.NumFields()
Matthew Dempsky1b9f1682016-03-14 12:45:18 -07003071 for i := 0; i < n; i++ {
Keith Randallaebf6612016-01-29 21:57:57 -08003072 ft := t.FieldType(i)
3073 addr := s.newValue1I(ssa.OpOffPtr, ft.PtrTo(), t.FieldOff(i), left)
Matthew Dempsky1b9f1682016-03-14 12:45:18 -07003074 val := s.newValue1I(ssa.OpStructSelect, ft, int64(i), right)
Keith Randalld4663e12016-03-21 10:22:03 -07003075 s.storeTypeScalars(ft.(*Type), addr, val, 0)
Keith Randallaebf6612016-01-29 21:57:57 -08003076 }
3077 default:
3078 s.Fatalf("bad write barrier type %s", t)
3079 }
3080}
3081
3082// do *left = right for all pointer parts of t.
3083func (s *state) storeTypePtrs(t *Type, left, right *ssa.Value) {
3084 switch {
Matthew Dempsky788f1122016-03-28 10:55:44 -07003085 case t.IsPtrShaped():
Keith Randallaebf6612016-01-29 21:57:57 -08003086 s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, s.config.PtrSize, left, right, s.mem())
3087 case t.IsString():
3088 ptr := s.newValue1(ssa.OpStringPtr, Ptrto(Types[TUINT8]), right)
3089 s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, s.config.PtrSize, left, ptr, s.mem())
3090 case t.IsSlice():
3091 ptr := s.newValue1(ssa.OpSlicePtr, Ptrto(Types[TUINT8]), right)
3092 s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, s.config.PtrSize, left, ptr, s.mem())
3093 case t.IsInterface():
3094 // itab field is treated as a scalar.
3095 idata := s.newValue1(ssa.OpIData, Ptrto(Types[TUINT8]), right)
3096 idataAddr := s.newValue1I(ssa.OpOffPtr, Ptrto(Types[TUINT8]), s.config.PtrSize, left)
3097 s.vars[&memVar] = s.newValue3I(ssa.OpStore, ssa.TypeMem, s.config.PtrSize, idataAddr, idata, s.mem())
3098 case t.IsStruct():
3099 n := t.NumFields()
Matthew Dempsky1b9f1682016-03-14 12:45:18 -07003100 for i := 0; i < n; i++ {
Keith Randallaebf6612016-01-29 21:57:57 -08003101 ft := t.FieldType(i)
3102 if !haspointers(ft.(*Type)) {
3103 continue
3104 }
3105 addr := s.newValue1I(ssa.OpOffPtr, ft.PtrTo(), t.FieldOff(i), left)
Matthew Dempsky1b9f1682016-03-14 12:45:18 -07003106 val := s.newValue1I(ssa.OpStructSelect, ft, int64(i), right)
Keith Randallaebf6612016-01-29 21:57:57 -08003107 s.storeTypePtrs(ft.(*Type), addr, val)
3108 }
3109 default:
3110 s.Fatalf("bad write barrier type %s", t)
3111 }
3112}
3113
3114// do *left = right with a write barrier for all pointer parts of t.
3115func (s *state) storeTypePtrsWB(t *Type, left, right *ssa.Value) {
Keith Randall5ba31942016-01-25 17:06:54 -08003116 switch {
Matthew Dempsky788f1122016-03-28 10:55:44 -07003117 case t.IsPtrShaped():
Keith Randall5ba31942016-01-25 17:06:54 -08003118 s.rtcall(writebarrierptr, true, nil, left, right)
3119 case t.IsString():
3120 ptr := s.newValue1(ssa.OpStringPtr, Ptrto(Types[TUINT8]), right)
3121 s.rtcall(writebarrierptr, true, nil, left, ptr)
3122 case t.IsSlice():
3123 ptr := s.newValue1(ssa.OpSlicePtr, Ptrto(Types[TUINT8]), right)
3124 s.rtcall(writebarrierptr, true, nil, left, ptr)
3125 case t.IsInterface():
3126 idata := s.newValue1(ssa.OpIData, Ptrto(Types[TUINT8]), right)
3127 idataAddr := s.newValue1I(ssa.OpOffPtr, Ptrto(Types[TUINT8]), s.config.PtrSize, left)
3128 s.rtcall(writebarrierptr, true, nil, idataAddr, idata)
Keith Randallaebf6612016-01-29 21:57:57 -08003129 case t.IsStruct():
3130 n := t.NumFields()
Matthew Dempsky1b9f1682016-03-14 12:45:18 -07003131 for i := 0; i < n; i++ {
Keith Randallaebf6612016-01-29 21:57:57 -08003132 ft := t.FieldType(i)
3133 if !haspointers(ft.(*Type)) {
3134 continue
3135 }
3136 addr := s.newValue1I(ssa.OpOffPtr, ft.PtrTo(), t.FieldOff(i), left)
Matthew Dempsky1b9f1682016-03-14 12:45:18 -07003137 val := s.newValue1I(ssa.OpStructSelect, ft, int64(i), right)
Keith Randallaebf6612016-01-29 21:57:57 -08003138 s.storeTypePtrsWB(ft.(*Type), addr, val)
3139 }
Keith Randall5ba31942016-01-25 17:06:54 -08003140 default:
3141 s.Fatalf("bad write barrier type %s", t)
3142 }
Keith Randall9d22c102015-09-11 11:02:57 -07003143}
3144
Keith Randall5505e8c2015-09-12 23:27:26 -07003145// slice computes the slice v[i:j:k] and returns ptr, len, and cap of result.
3146// i,j,k may be nil, in which case they are set to their default value.
3147// t is a slice, ptr to array, or string type.
3148func (s *state) slice(t *Type, v, i, j, k *ssa.Value) (p, l, c *ssa.Value) {
3149 var elemtype *Type
3150 var ptrtype *Type
3151 var ptr *ssa.Value
3152 var len *ssa.Value
3153 var cap *ssa.Value
3154 zero := s.constInt(Types[TINT], 0)
3155 switch {
3156 case t.IsSlice():
Josh Bleecher Snyder8640b512016-03-30 10:57:47 -07003157 elemtype = t.Elem()
Keith Randall5505e8c2015-09-12 23:27:26 -07003158 ptrtype = Ptrto(elemtype)
3159 ptr = s.newValue1(ssa.OpSlicePtr, ptrtype, v)
3160 len = s.newValue1(ssa.OpSliceLen, Types[TINT], v)
3161 cap = s.newValue1(ssa.OpSliceCap, Types[TINT], v)
3162 case t.IsString():
3163 elemtype = Types[TUINT8]
3164 ptrtype = Ptrto(elemtype)
3165 ptr = s.newValue1(ssa.OpStringPtr, ptrtype, v)
3166 len = s.newValue1(ssa.OpStringLen, Types[TINT], v)
3167 cap = len
3168 case t.IsPtr():
Josh Bleecher Snyder8640b512016-03-30 10:57:47 -07003169 if !t.Elem().IsArray() {
Keith Randall5505e8c2015-09-12 23:27:26 -07003170 s.Fatalf("bad ptr to array in slice %v\n", t)
3171 }
Josh Bleecher Snyder8640b512016-03-30 10:57:47 -07003172 elemtype = t.Elem().Elem()
Keith Randall5505e8c2015-09-12 23:27:26 -07003173 ptrtype = Ptrto(elemtype)
3174 s.nilCheck(v)
3175 ptr = v
Josh Bleecher Snyder3a0783c2016-03-31 14:46:04 -07003176 len = s.constInt(Types[TINT], t.Elem().NumElem())
Keith Randall5505e8c2015-09-12 23:27:26 -07003177 cap = len
3178 default:
3179 s.Fatalf("bad type in slice %v\n", t)
3180 }
3181
3182 // Set default values
3183 if i == nil {
3184 i = zero
3185 }
3186 if j == nil {
3187 j = len
3188 }
3189 if k == nil {
3190 k = cap
3191 }
3192
3193 // Panic if slice indices are not in bounds.
3194 s.sliceBoundsCheck(i, j)
3195 if j != k {
3196 s.sliceBoundsCheck(j, k)
3197 }
3198 if k != cap {
3199 s.sliceBoundsCheck(k, cap)
3200 }
3201
3202 // Generate the following code assuming that indexes are in bounds.
3203 // The conditional is to make sure that we don't generate a slice
3204 // that points to the next object in memory.
Keith Randall69a7c1522016-03-21 15:24:08 -07003205 // rlen = j-i
3206 // rcap = k-i
3207 // delta = i*elemsize
3208 // if rcap == 0 {
3209 // delta = 0
Keith Randall5505e8c2015-09-12 23:27:26 -07003210 // }
Keith Randall69a7c1522016-03-21 15:24:08 -07003211 // rptr = p+delta
3212 // result = (SliceMake rptr rlen rcap)
Keith Randall582baae2015-11-02 21:28:13 -08003213 subOp := s.ssaOp(OSUB, Types[TINT])
Keith Randall69a7c1522016-03-21 15:24:08 -07003214 eqOp := s.ssaOp(OEQ, Types[TINT])
Keith Randall582baae2015-11-02 21:28:13 -08003215 mulOp := s.ssaOp(OMUL, Types[TINT])
3216 rlen := s.newValue2(subOp, Types[TINT], j, i)
Keith Randall5505e8c2015-09-12 23:27:26 -07003217 var rcap *ssa.Value
3218 switch {
3219 case t.IsString():
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00003220 // Capacity of the result is unimportant. However, we use
Keith Randall5505e8c2015-09-12 23:27:26 -07003221 // rcap to test if we've generated a zero-length slice.
3222 // Use length of strings for that.
3223 rcap = rlen
3224 case j == k:
3225 rcap = rlen
3226 default:
Keith Randall582baae2015-11-02 21:28:13 -08003227 rcap = s.newValue2(subOp, Types[TINT], k, i)
Keith Randall5505e8c2015-09-12 23:27:26 -07003228 }
3229
Keith Randall69a7c1522016-03-21 15:24:08 -07003230 // delta = # of elements to offset pointer by.
3231 s.vars[&deltaVar] = i
Keith Randall5505e8c2015-09-12 23:27:26 -07003232
Keith Randall69a7c1522016-03-21 15:24:08 -07003233 // Generate code to set delta=0 if the resulting capacity is zero.
3234 if !((i.Op == ssa.OpConst64 && i.AuxInt == 0) ||
3235 (i.Op == ssa.OpConst32 && int32(i.AuxInt) == 0)) {
3236 cmp := s.newValue2(eqOp, Types[TBOOL], rcap, zero)
Keith Randall5505e8c2015-09-12 23:27:26 -07003237
Keith Randall69a7c1522016-03-21 15:24:08 -07003238 b := s.endBlock()
3239 b.Kind = ssa.BlockIf
3240 b.Likely = ssa.BranchUnlikely
3241 b.SetControl(cmp)
Keith Randall5505e8c2015-09-12 23:27:26 -07003242
Keith Randall69a7c1522016-03-21 15:24:08 -07003243 // Generate block which zeros the delta variable.
3244 nz := s.f.NewBlock(ssa.BlockPlain)
3245 b.AddEdgeTo(nz)
3246 s.startBlock(nz)
3247 s.vars[&deltaVar] = zero
3248 s.endBlock()
3249
3250 // All done.
3251 merge := s.f.NewBlock(ssa.BlockPlain)
3252 b.AddEdgeTo(merge)
3253 nz.AddEdgeTo(merge)
3254 s.startBlock(merge)
3255
3256 // TODO: use conditional moves somehow?
Keith Randall5505e8c2015-09-12 23:27:26 -07003257 }
Keith Randall5505e8c2015-09-12 23:27:26 -07003258
Keith Randall69a7c1522016-03-21 15:24:08 -07003259 // Compute rptr = ptr + delta * elemsize
3260 rptr := s.newValue2(ssa.OpAddPtr, ptrtype, ptr, s.newValue2(mulOp, Types[TINT], s.variable(&deltaVar, Types[TINT]), s.constInt(Types[TINT], elemtype.Width)))
3261 delete(s.vars, &deltaVar)
Keith Randall5505e8c2015-09-12 23:27:26 -07003262 return rptr, rlen, rcap
3263}
3264
David Chase42825882015-08-20 15:14:20 -04003265type u2fcvtTab struct {
3266 geq, cvt2F, and, rsh, or, add ssa.Op
3267 one func(*state, ssa.Type, int64) *ssa.Value
3268}
3269
3270var u64_f64 u2fcvtTab = u2fcvtTab{
3271 geq: ssa.OpGeq64,
3272 cvt2F: ssa.OpCvt64to64F,
3273 and: ssa.OpAnd64,
3274 rsh: ssa.OpRsh64Ux64,
3275 or: ssa.OpOr64,
3276 add: ssa.OpAdd64F,
3277 one: (*state).constInt64,
3278}
3279
3280var u64_f32 u2fcvtTab = u2fcvtTab{
3281 geq: ssa.OpGeq64,
3282 cvt2F: ssa.OpCvt64to32F,
3283 and: ssa.OpAnd64,
3284 rsh: ssa.OpRsh64Ux64,
3285 or: ssa.OpOr64,
3286 add: ssa.OpAdd32F,
3287 one: (*state).constInt64,
3288}
3289
3290// Excess generality on a machine with 64-bit integer registers.
3291// Not used on AMD64.
3292var u32_f32 u2fcvtTab = u2fcvtTab{
3293 geq: ssa.OpGeq32,
3294 cvt2F: ssa.OpCvt32to32F,
3295 and: ssa.OpAnd32,
3296 rsh: ssa.OpRsh32Ux32,
3297 or: ssa.OpOr32,
3298 add: ssa.OpAdd32F,
3299 one: func(s *state, t ssa.Type, x int64) *ssa.Value {
3300 return s.constInt32(t, int32(x))
3301 },
3302}
3303
3304func (s *state) uint64Tofloat64(n *Node, x *ssa.Value, ft, tt *Type) *ssa.Value {
3305 return s.uintTofloat(&u64_f64, n, x, ft, tt)
3306}
3307
3308func (s *state) uint64Tofloat32(n *Node, x *ssa.Value, ft, tt *Type) *ssa.Value {
3309 return s.uintTofloat(&u64_f32, n, x, ft, tt)
3310}
3311
3312func (s *state) uintTofloat(cvttab *u2fcvtTab, n *Node, x *ssa.Value, ft, tt *Type) *ssa.Value {
3313 // if x >= 0 {
3314 // result = (floatY) x
3315 // } else {
3316 // y = uintX(x) ; y = x & 1
3317 // z = uintX(x) ; z = z >> 1
3318 // z = z >> 1
3319 // z = z | y
David Chase73151062015-08-26 14:25:40 -04003320 // result = floatY(z)
3321 // result = result + result
David Chase42825882015-08-20 15:14:20 -04003322 // }
3323 //
3324 // Code borrowed from old code generator.
3325 // What's going on: large 64-bit "unsigned" looks like
3326 // negative number to hardware's integer-to-float
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00003327 // conversion. However, because the mantissa is only
David Chase42825882015-08-20 15:14:20 -04003328 // 63 bits, we don't need the LSB, so instead we do an
3329 // unsigned right shift (divide by two), convert, and
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00003330 // double. However, before we do that, we need to be
David Chase42825882015-08-20 15:14:20 -04003331 // sure that we do not lose a "1" if that made the
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00003332 // difference in the resulting rounding. Therefore, we
3333 // preserve it, and OR (not ADD) it back in. The case
David Chase42825882015-08-20 15:14:20 -04003334 // that matters is when the eleven discarded bits are
3335 // equal to 10000000001; that rounds up, and the 1 cannot
3336 // be lost else it would round down if the LSB of the
3337 // candidate mantissa is 0.
3338 cmp := s.newValue2(cvttab.geq, Types[TBOOL], x, s.zeroVal(ft))
3339 b := s.endBlock()
3340 b.Kind = ssa.BlockIf
Keith Randall56e0ecc2016-03-15 20:45:50 -07003341 b.SetControl(cmp)
David Chase42825882015-08-20 15:14:20 -04003342 b.Likely = ssa.BranchLikely
3343
3344 bThen := s.f.NewBlock(ssa.BlockPlain)
3345 bElse := s.f.NewBlock(ssa.BlockPlain)
3346 bAfter := s.f.NewBlock(ssa.BlockPlain)
3347
Todd Neal47d67992015-08-28 21:36:29 -05003348 b.AddEdgeTo(bThen)
David Chase42825882015-08-20 15:14:20 -04003349 s.startBlock(bThen)
3350 a0 := s.newValue1(cvttab.cvt2F, tt, x)
3351 s.vars[n] = a0
3352 s.endBlock()
Todd Neal47d67992015-08-28 21:36:29 -05003353 bThen.AddEdgeTo(bAfter)
David Chase42825882015-08-20 15:14:20 -04003354
Todd Neal47d67992015-08-28 21:36:29 -05003355 b.AddEdgeTo(bElse)
David Chase42825882015-08-20 15:14:20 -04003356 s.startBlock(bElse)
3357 one := cvttab.one(s, ft, 1)
3358 y := s.newValue2(cvttab.and, ft, x, one)
3359 z := s.newValue2(cvttab.rsh, ft, x, one)
3360 z = s.newValue2(cvttab.or, ft, z, y)
3361 a := s.newValue1(cvttab.cvt2F, tt, z)
3362 a1 := s.newValue2(cvttab.add, tt, a, a)
3363 s.vars[n] = a1
3364 s.endBlock()
Todd Neal47d67992015-08-28 21:36:29 -05003365 bElse.AddEdgeTo(bAfter)
David Chase42825882015-08-20 15:14:20 -04003366
3367 s.startBlock(bAfter)
3368 return s.variable(n, n.Type)
3369}
3370
Todd Neal707af252015-08-28 15:56:43 -05003371// referenceTypeBuiltin generates code for the len/cap builtins for maps and channels.
3372func (s *state) referenceTypeBuiltin(n *Node, x *ssa.Value) *ssa.Value {
3373 if !n.Left.Type.IsMap() && !n.Left.Type.IsChan() {
3374 s.Fatalf("node must be a map or a channel")
3375 }
Todd Neale0e40682015-08-26 18:40:52 -05003376 // if n == nil {
3377 // return 0
3378 // } else {
Todd Neal707af252015-08-28 15:56:43 -05003379 // // len
Todd Neale0e40682015-08-26 18:40:52 -05003380 // return *((*int)n)
Todd Neal707af252015-08-28 15:56:43 -05003381 // // cap
3382 // return *(((*int)n)+1)
Todd Neale0e40682015-08-26 18:40:52 -05003383 // }
3384 lenType := n.Type
Josh Bleecher Snyder39214272016-03-06 18:06:09 -08003385 nilValue := s.constNil(Types[TUINTPTR])
Todd Neal67ac8a32015-08-28 15:20:54 -05003386 cmp := s.newValue2(ssa.OpEqPtr, Types[TBOOL], x, nilValue)
Todd Neale0e40682015-08-26 18:40:52 -05003387 b := s.endBlock()
3388 b.Kind = ssa.BlockIf
Keith Randall56e0ecc2016-03-15 20:45:50 -07003389 b.SetControl(cmp)
Todd Neale0e40682015-08-26 18:40:52 -05003390 b.Likely = ssa.BranchUnlikely
3391
3392 bThen := s.f.NewBlock(ssa.BlockPlain)
3393 bElse := s.f.NewBlock(ssa.BlockPlain)
3394 bAfter := s.f.NewBlock(ssa.BlockPlain)
3395
Todd Neal707af252015-08-28 15:56:43 -05003396 // length/capacity of a nil map/chan is zero
Todd Neal47d67992015-08-28 21:36:29 -05003397 b.AddEdgeTo(bThen)
Todd Neale0e40682015-08-26 18:40:52 -05003398 s.startBlock(bThen)
3399 s.vars[n] = s.zeroVal(lenType)
3400 s.endBlock()
Todd Neal47d67992015-08-28 21:36:29 -05003401 bThen.AddEdgeTo(bAfter)
Todd Neale0e40682015-08-26 18:40:52 -05003402
Todd Neal47d67992015-08-28 21:36:29 -05003403 b.AddEdgeTo(bElse)
Todd Neale0e40682015-08-26 18:40:52 -05003404 s.startBlock(bElse)
Todd Neal707af252015-08-28 15:56:43 -05003405 if n.Op == OLEN {
3406 // length is stored in the first word for map/chan
3407 s.vars[n] = s.newValue2(ssa.OpLoad, lenType, x, s.mem())
3408 } else if n.Op == OCAP {
3409 // capacity is stored in the second word for chan
3410 sw := s.newValue1I(ssa.OpOffPtr, lenType.PtrTo(), lenType.Width, x)
3411 s.vars[n] = s.newValue2(ssa.OpLoad, lenType, sw, s.mem())
3412 } else {
3413 s.Fatalf("op must be OLEN or OCAP")
3414 }
Todd Neale0e40682015-08-26 18:40:52 -05003415 s.endBlock()
Todd Neal47d67992015-08-28 21:36:29 -05003416 bElse.AddEdgeTo(bAfter)
Todd Neale0e40682015-08-26 18:40:52 -05003417
3418 s.startBlock(bAfter)
3419 return s.variable(n, lenType)
3420}
3421
David Chase73151062015-08-26 14:25:40 -04003422type f2uCvtTab struct {
3423 ltf, cvt2U, subf ssa.Op
3424 value func(*state, ssa.Type, float64) *ssa.Value
3425}
3426
3427var f32_u64 f2uCvtTab = f2uCvtTab{
3428 ltf: ssa.OpLess32F,
3429 cvt2U: ssa.OpCvt32Fto64,
3430 subf: ssa.OpSub32F,
3431 value: (*state).constFloat32,
3432}
3433
3434var f64_u64 f2uCvtTab = f2uCvtTab{
3435 ltf: ssa.OpLess64F,
3436 cvt2U: ssa.OpCvt64Fto64,
3437 subf: ssa.OpSub64F,
3438 value: (*state).constFloat64,
3439}
3440
3441func (s *state) float32ToUint64(n *Node, x *ssa.Value, ft, tt *Type) *ssa.Value {
3442 return s.floatToUint(&f32_u64, n, x, ft, tt)
3443}
3444func (s *state) float64ToUint64(n *Node, x *ssa.Value, ft, tt *Type) *ssa.Value {
3445 return s.floatToUint(&f64_u64, n, x, ft, tt)
3446}
3447
3448func (s *state) floatToUint(cvttab *f2uCvtTab, n *Node, x *ssa.Value, ft, tt *Type) *ssa.Value {
3449 // if x < 9223372036854775808.0 {
3450 // result = uintY(x)
3451 // } else {
3452 // y = x - 9223372036854775808.0
3453 // z = uintY(y)
3454 // result = z | -9223372036854775808
3455 // }
3456 twoToThe63 := cvttab.value(s, ft, 9223372036854775808.0)
3457 cmp := s.newValue2(cvttab.ltf, Types[TBOOL], x, twoToThe63)
3458 b := s.endBlock()
3459 b.Kind = ssa.BlockIf
Keith Randall56e0ecc2016-03-15 20:45:50 -07003460 b.SetControl(cmp)
David Chase73151062015-08-26 14:25:40 -04003461 b.Likely = ssa.BranchLikely
3462
3463 bThen := s.f.NewBlock(ssa.BlockPlain)
3464 bElse := s.f.NewBlock(ssa.BlockPlain)
3465 bAfter := s.f.NewBlock(ssa.BlockPlain)
3466
Todd Neal47d67992015-08-28 21:36:29 -05003467 b.AddEdgeTo(bThen)
David Chase73151062015-08-26 14:25:40 -04003468 s.startBlock(bThen)
3469 a0 := s.newValue1(cvttab.cvt2U, tt, x)
3470 s.vars[n] = a0
3471 s.endBlock()
Todd Neal47d67992015-08-28 21:36:29 -05003472 bThen.AddEdgeTo(bAfter)
David Chase73151062015-08-26 14:25:40 -04003473
Todd Neal47d67992015-08-28 21:36:29 -05003474 b.AddEdgeTo(bElse)
David Chase73151062015-08-26 14:25:40 -04003475 s.startBlock(bElse)
3476 y := s.newValue2(cvttab.subf, ft, x, twoToThe63)
3477 y = s.newValue1(cvttab.cvt2U, tt, y)
3478 z := s.constInt64(tt, -9223372036854775808)
3479 a1 := s.newValue2(ssa.OpOr64, tt, y, z)
3480 s.vars[n] = a1
3481 s.endBlock()
Todd Neal47d67992015-08-28 21:36:29 -05003482 bElse.AddEdgeTo(bAfter)
David Chase73151062015-08-26 14:25:40 -04003483
3484 s.startBlock(bAfter)
3485 return s.variable(n, n.Type)
3486}
3487
Keith Randall269baa92015-09-17 10:31:16 -07003488// ifaceType returns the value for the word containing the type.
3489// n is the node for the interface expression.
3490// v is the corresponding value.
3491func (s *state) ifaceType(n *Node, v *ssa.Value) *ssa.Value {
3492 byteptr := Ptrto(Types[TUINT8]) // type used in runtime prototypes for runtime type (*byte)
3493
Matthew Dempsky00e5a682016-04-01 13:36:24 -07003494 if n.Type.IsEmptyInterface() {
Keith Randall269baa92015-09-17 10:31:16 -07003495 // Have *eface. The type is the first word in the struct.
3496 return s.newValue1(ssa.OpITab, byteptr, v)
3497 }
3498
3499 // Have *iface.
3500 // The first word in the struct is the *itab.
3501 // If the *itab is nil, return 0.
3502 // Otherwise, the second word in the *itab is the type.
3503
3504 tab := s.newValue1(ssa.OpITab, byteptr, v)
3505 s.vars[&typVar] = tab
Josh Bleecher Snyder39214272016-03-06 18:06:09 -08003506 isnonnil := s.newValue2(ssa.OpNeqPtr, Types[TBOOL], tab, s.constNil(byteptr))
Keith Randall269baa92015-09-17 10:31:16 -07003507 b := s.endBlock()
3508 b.Kind = ssa.BlockIf
Keith Randall56e0ecc2016-03-15 20:45:50 -07003509 b.SetControl(isnonnil)
Keith Randall269baa92015-09-17 10:31:16 -07003510 b.Likely = ssa.BranchLikely
3511
3512 bLoad := s.f.NewBlock(ssa.BlockPlain)
3513 bEnd := s.f.NewBlock(ssa.BlockPlain)
3514
3515 b.AddEdgeTo(bLoad)
3516 b.AddEdgeTo(bEnd)
3517 bLoad.AddEdgeTo(bEnd)
3518
3519 s.startBlock(bLoad)
3520 off := s.newValue1I(ssa.OpOffPtr, byteptr, int64(Widthptr), tab)
3521 s.vars[&typVar] = s.newValue2(ssa.OpLoad, byteptr, off, s.mem())
3522 s.endBlock()
3523
3524 s.startBlock(bEnd)
3525 typ := s.variable(&typVar, byteptr)
3526 delete(s.vars, &typVar)
3527 return typ
3528}
3529
3530// dottype generates SSA for a type assertion node.
3531// commaok indicates whether to panic or return a bool.
3532// If commaok is false, resok will be nil.
3533func (s *state) dottype(n *Node, commaok bool) (res, resok *ssa.Value) {
3534 iface := s.expr(n.Left)
3535 typ := s.ifaceType(n.Left, iface) // actual concrete type
3536 target := s.expr(typename(n.Type)) // target type
3537 if !isdirectiface(n.Type) {
3538 // walk rewrites ODOTTYPE/OAS2DOTTYPE into runtime calls except for this case.
3539 Fatalf("dottype needs a direct iface type %s", n.Type)
3540 }
3541
David Chase729abfa2015-10-26 17:34:06 -04003542 if Debug_typeassert > 0 {
Robert Griesemerb83f3972016-03-02 11:01:25 -08003543 Warnl(n.Lineno, "type assertion inlined")
David Chase729abfa2015-10-26 17:34:06 -04003544 }
3545
Keith Randall269baa92015-09-17 10:31:16 -07003546 // TODO: If we have a nonempty interface and its itab field is nil,
3547 // then this test is redundant and ifaceType should just branch directly to bFail.
3548 cond := s.newValue2(ssa.OpEqPtr, Types[TBOOL], typ, target)
3549 b := s.endBlock()
3550 b.Kind = ssa.BlockIf
Keith Randall56e0ecc2016-03-15 20:45:50 -07003551 b.SetControl(cond)
Keith Randall269baa92015-09-17 10:31:16 -07003552 b.Likely = ssa.BranchLikely
3553
3554 byteptr := Ptrto(Types[TUINT8])
3555
3556 bOk := s.f.NewBlock(ssa.BlockPlain)
3557 bFail := s.f.NewBlock(ssa.BlockPlain)
3558 b.AddEdgeTo(bOk)
3559 b.AddEdgeTo(bFail)
3560
3561 if !commaok {
3562 // on failure, panic by calling panicdottype
3563 s.startBlock(bFail)
Keith Randall269baa92015-09-17 10:31:16 -07003564 taddr := s.newValue1A(ssa.OpAddr, byteptr, &ssa.ExternSymbol{byteptr, typenamesym(n.Left.Type)}, s.sb)
Keith Randall8c5bfcc2015-09-18 15:11:30 -07003565 s.rtcall(panicdottype, false, nil, typ, target, taddr)
Keith Randall269baa92015-09-17 10:31:16 -07003566
3567 // on success, return idata field
3568 s.startBlock(bOk)
3569 return s.newValue1(ssa.OpIData, n.Type, iface), nil
3570 }
3571
3572 // commaok is the more complicated case because we have
3573 // a control flow merge point.
3574 bEnd := s.f.NewBlock(ssa.BlockPlain)
3575
3576 // type assertion succeeded
3577 s.startBlock(bOk)
3578 s.vars[&idataVar] = s.newValue1(ssa.OpIData, n.Type, iface)
3579 s.vars[&okVar] = s.constBool(true)
3580 s.endBlock()
3581 bOk.AddEdgeTo(bEnd)
3582
3583 // type assertion failed
3584 s.startBlock(bFail)
Josh Bleecher Snyder39214272016-03-06 18:06:09 -08003585 s.vars[&idataVar] = s.constNil(byteptr)
Keith Randall269baa92015-09-17 10:31:16 -07003586 s.vars[&okVar] = s.constBool(false)
3587 s.endBlock()
3588 bFail.AddEdgeTo(bEnd)
3589
3590 // merge point
3591 s.startBlock(bEnd)
3592 res = s.variable(&idataVar, byteptr)
3593 resok = s.variable(&okVar, Types[TBOOL])
3594 delete(s.vars, &idataVar)
3595 delete(s.vars, &okVar)
3596 return res, resok
3597}
3598
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -07003599// checkgoto checks that a goto from from to to does not
3600// jump into a block or jump over variable declarations.
3601// It is a copy of checkgoto in the pre-SSA backend,
3602// modified only for line number handling.
3603// TODO: document how this works and why it is designed the way it is.
3604func (s *state) checkgoto(from *Node, to *Node) {
3605 if from.Sym == to.Sym {
3606 return
3607 }
3608
3609 nf := 0
3610 for fs := from.Sym; fs != nil; fs = fs.Link {
3611 nf++
3612 }
3613 nt := 0
3614 for fs := to.Sym; fs != nil; fs = fs.Link {
3615 nt++
3616 }
3617 fs := from.Sym
3618 for ; nf > nt; nf-- {
3619 fs = fs.Link
3620 }
3621 if fs != to.Sym {
3622 // decide what to complain about.
3623 // prefer to complain about 'into block' over declarations,
3624 // so scan backward to find most recent block or else dcl.
3625 var block *Sym
3626
3627 var dcl *Sym
3628 ts := to.Sym
3629 for ; nt > nf; nt-- {
3630 if ts.Pkg == nil {
3631 block = ts
3632 } else {
3633 dcl = ts
3634 }
3635 ts = ts.Link
3636 }
3637
3638 for ts != fs {
3639 if ts.Pkg == nil {
3640 block = ts
3641 } else {
3642 dcl = ts
3643 }
3644 ts = ts.Link
3645 fs = fs.Link
3646 }
3647
Robert Griesemerb83f3972016-03-02 11:01:25 -08003648 lno := from.Left.Lineno
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -07003649 if block != nil {
Robert Griesemer2faf5bc2016-03-02 11:30:29 -08003650 yyerrorl(lno, "goto %v jumps into block starting at %v", from.Left.Sym, linestr(block.Lastlineno))
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -07003651 } else {
Robert Griesemer2faf5bc2016-03-02 11:30:29 -08003652 yyerrorl(lno, "goto %v jumps over declaration of %v at %v", from.Left.Sym, dcl, linestr(dcl.Lastlineno))
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -07003653 }
3654 }
3655}
3656
Keith Randalld2fd43a2015-04-15 15:51:25 -07003657// variable returns the value of a variable at the current location.
Keith Randall8c46aa52015-06-19 21:02:28 -07003658func (s *state) variable(name *Node, t ssa.Type) *ssa.Value {
Keith Randalld2fd43a2015-04-15 15:51:25 -07003659 v := s.vars[name]
3660 if v == nil {
Keith Randall8f22b522015-06-11 21:29:25 -07003661 v = s.newValue0A(ssa.OpFwdRef, t, name)
Keith Randallb5c5efd2016-01-14 16:02:23 -08003662 s.fwdRefs = append(s.fwdRefs, v)
Keith Randalld2fd43a2015-04-15 15:51:25 -07003663 s.vars[name] = v
Keith Randallb5c5efd2016-01-14 16:02:23 -08003664 s.addNamedValue(name, v)
Keith Randalld2fd43a2015-04-15 15:51:25 -07003665 }
3666 return v
3667}
3668
Keith Randallcfc2aa52015-05-18 16:44:20 -07003669func (s *state) mem() *ssa.Value {
Keith Randallb32217a2015-09-17 16:45:10 -07003670 return s.variable(&memVar, ssa.TypeMem)
Keith Randalld2fd43a2015-04-15 15:51:25 -07003671}
3672
Keith Randallcfc2aa52015-05-18 16:44:20 -07003673func (s *state) linkForwardReferences() {
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00003674 // Build SSA graph. Each variable on its first use in a basic block
Keith Randalld2fd43a2015-04-15 15:51:25 -07003675 // leaves a FwdRef in that block representing the incoming value
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00003676 // of that variable. This function links that ref up with possible definitions,
3677 // inserting Phi values as needed. This is essentially the algorithm
Keith Randallb5c5efd2016-01-14 16:02:23 -08003678 // described by Braun, Buchwald, Hack, Leißa, Mallon, and Zwinkau:
Keith Randalld2fd43a2015-04-15 15:51:25 -07003679 // http://pp.info.uni-karlsruhe.de/uploads/publikationen/braun13cc.pdf
Keith Randallb5c5efd2016-01-14 16:02:23 -08003680 // Differences:
3681 // - We use FwdRef nodes to postpone phi building until the CFG is
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00003682 // completely built. That way we can avoid the notion of "sealed"
Keith Randallb5c5efd2016-01-14 16:02:23 -08003683 // blocks.
3684 // - Phi optimization is a separate pass (in ../ssa/phielim.go).
3685 for len(s.fwdRefs) > 0 {
3686 v := s.fwdRefs[len(s.fwdRefs)-1]
3687 s.fwdRefs = s.fwdRefs[:len(s.fwdRefs)-1]
3688 s.resolveFwdRef(v)
Keith Randalld2fd43a2015-04-15 15:51:25 -07003689 }
3690}
3691
Keith Randallb5c5efd2016-01-14 16:02:23 -08003692// resolveFwdRef modifies v to be the variable's value at the start of its block.
3693// v must be a FwdRef op.
3694func (s *state) resolveFwdRef(v *ssa.Value) {
3695 b := v.Block
3696 name := v.Aux.(*Node)
3697 v.Aux = nil
Keith Randalld2fd43a2015-04-15 15:51:25 -07003698 if b == s.f.Entry {
Keith Randallb5c5efd2016-01-14 16:02:23 -08003699 // Live variable at start of function.
Keith Randall6a8a9da2016-02-27 17:49:31 -08003700 if s.canSSA(name) {
Keith Randallb5c5efd2016-01-14 16:02:23 -08003701 v.Op = ssa.OpArg
3702 v.Aux = name
3703 return
Keith Randall02f4d0a2015-11-02 08:10:26 -08003704 }
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00003705 // Not SSAable. Load it.
Keith Randall8c46aa52015-06-19 21:02:28 -07003706 addr := s.decladdrs[name]
3707 if addr == nil {
3708 // TODO: closure args reach here.
David Chase32ffbf72015-10-08 17:14:12 -04003709 s.Unimplementedf("unhandled closure arg %s at entry to function %s", name, b.Func.Name)
Keith Randall8c46aa52015-06-19 21:02:28 -07003710 }
3711 if _, ok := addr.Aux.(*ssa.ArgSymbol); !ok {
3712 s.Fatalf("variable live at start of function %s is not an argument %s", b.Func.Name, name)
3713 }
Keith Randallb5c5efd2016-01-14 16:02:23 -08003714 v.Op = ssa.OpLoad
3715 v.AddArgs(addr, s.startmem)
3716 return
Keith Randalld2fd43a2015-04-15 15:51:25 -07003717 }
Keith Randallb5c5efd2016-01-14 16:02:23 -08003718 if len(b.Preds) == 0 {
Josh Bleecher Snyder61aa0952015-07-20 15:39:14 -07003719 // This block is dead; we have no predecessors and we're not the entry block.
Keith Randallb5c5efd2016-01-14 16:02:23 -08003720 // It doesn't matter what we use here as long as it is well-formed.
3721 v.Op = ssa.OpUnknown
3722 return
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -07003723 }
Keith Randallb5c5efd2016-01-14 16:02:23 -08003724 // Find variable value on each predecessor.
3725 var argstore [4]*ssa.Value
3726 args := argstore[:0]
3727 for _, p := range b.Preds {
3728 args = append(args, s.lookupVarOutgoing(p, v.Type, name, v.Line))
Keith Randalld2fd43a2015-04-15 15:51:25 -07003729 }
Keith Randallb5c5efd2016-01-14 16:02:23 -08003730
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00003731 // Decide if we need a phi or not. We need a phi if there
Keith Randallb5c5efd2016-01-14 16:02:23 -08003732 // are two different args (which are both not v).
3733 var w *ssa.Value
3734 for _, a := range args {
3735 if a == v {
3736 continue // self-reference
3737 }
3738 if a == w {
3739 continue // already have this witness
3740 }
3741 if w != nil {
3742 // two witnesses, need a phi value
3743 v.Op = ssa.OpPhi
3744 v.AddArgs(args...)
3745 return
3746 }
3747 w = a // save witness
3748 }
3749 if w == nil {
3750 s.Fatalf("no witness for reachable phi %s", v)
3751 }
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00003752 // One witness. Make v a copy of w.
Keith Randallb5c5efd2016-01-14 16:02:23 -08003753 v.Op = ssa.OpCopy
3754 v.AddArg(w)
Keith Randalld2fd43a2015-04-15 15:51:25 -07003755}
3756
3757// lookupVarOutgoing finds the variable's value at the end of block b.
Keith Randallb5c5efd2016-01-14 16:02:23 -08003758func (s *state) lookupVarOutgoing(b *ssa.Block, t ssa.Type, name *Node, line int32) *ssa.Value {
Keith Randalld2fd43a2015-04-15 15:51:25 -07003759 m := s.defvars[b.ID]
3760 if v, ok := m[name]; ok {
3761 return v
3762 }
3763 // The variable is not defined by b and we haven't
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00003764 // looked it up yet. Generate a FwdRef for the variable and return that.
Keith Randallb5c5efd2016-01-14 16:02:23 -08003765 v := b.NewValue0A(line, ssa.OpFwdRef, t, name)
3766 s.fwdRefs = append(s.fwdRefs, v)
Keith Randalld2fd43a2015-04-15 15:51:25 -07003767 m[name] = v
Keith Randallb5c5efd2016-01-14 16:02:23 -08003768 s.addNamedValue(name, v)
Keith Randalld2fd43a2015-04-15 15:51:25 -07003769 return v
3770}
3771
Keith Randallc24681a2015-10-22 14:22:38 -07003772func (s *state) addNamedValue(n *Node, v *ssa.Value) {
3773 if n.Class == Pxxx {
3774 // Don't track our dummy nodes (&memVar etc.).
3775 return
3776 }
Keith Randallc24681a2015-10-22 14:22:38 -07003777 if strings.HasPrefix(n.Sym.Name, "autotmp_") {
3778 // Don't track autotmp_ variables.
3779 return
3780 }
Keith Randall31d13f42016-03-08 20:09:48 -08003781 if n.Class == PPARAMOUT {
3782 // Don't track named output values. This prevents return values
3783 // from being assigned too early. See #14591 and #14762. TODO: allow this.
3784 return
3785 }
Keith Randallc24681a2015-10-22 14:22:38 -07003786 if n.Class == PAUTO && n.Xoffset != 0 {
3787 s.Fatalf("AUTO var with offset %s %d", n, n.Xoffset)
3788 }
Keith Randall02f4d0a2015-11-02 08:10:26 -08003789 loc := ssa.LocalSlot{N: n, Type: n.Type, Off: 0}
3790 values, ok := s.f.NamedValues[loc]
Keith Randallc24681a2015-10-22 14:22:38 -07003791 if !ok {
Keith Randall02f4d0a2015-11-02 08:10:26 -08003792 s.f.Names = append(s.f.Names, loc)
Keith Randallc24681a2015-10-22 14:22:38 -07003793 }
Keith Randall02f4d0a2015-11-02 08:10:26 -08003794 s.f.NamedValues[loc] = append(values, v)
Keith Randallc24681a2015-10-22 14:22:38 -07003795}
3796
Michael Pratta4e31d42016-03-12 14:07:40 -08003797// Branch is an unresolved branch.
3798type Branch struct {
3799 P *obj.Prog // branch instruction
3800 B *ssa.Block // target
Keith Randall083a6462015-05-12 11:06:44 -07003801}
3802
Michael Pratta4e31d42016-03-12 14:07:40 -08003803// SSAGenState contains state needed during Prog generation.
3804type SSAGenState struct {
3805 // Branches remembers all the branch instructions we've seen
Keith Randall9569b952015-08-28 22:51:01 -07003806 // and where they would like to go.
Michael Pratta4e31d42016-03-12 14:07:40 -08003807 Branches []Branch
Keith Randall9569b952015-08-28 22:51:01 -07003808
3809 // bstart remembers where each block starts (indexed by block ID)
3810 bstart []*obj.Prog
Keith Randall9569b952015-08-28 22:51:01 -07003811}
3812
Michael Pratta4e31d42016-03-12 14:07:40 -08003813// Pc returns the current Prog.
3814func (s *SSAGenState) Pc() *obj.Prog {
3815 return Pc
3816}
3817
3818// SetLineno sets the current source line number.
3819func (s *SSAGenState) SetLineno(l int32) {
3820 lineno = l
3821}
3822
Keith Randall083a6462015-05-12 11:06:44 -07003823// genssa appends entries to ptxt for each instruction in f.
3824// gcargs and gclocals are filled in with pointer maps for the frame.
3825func genssa(f *ssa.Func, ptxt *obj.Prog, gcargs, gclocals *Sym) {
Michael Pratta4e31d42016-03-12 14:07:40 -08003826 var s SSAGenState
Keith Randall9569b952015-08-28 22:51:01 -07003827
Josh Bleecher Snyderd2982092015-07-22 13:13:53 -07003828 e := f.Config.Frontend().(*ssaExport)
3829 // We're about to emit a bunch of Progs.
3830 // Since the only way to get here is to explicitly request it,
3831 // just fail on unimplemented instead of trying to unwind our mess.
3832 e.mustImplement = true
3833
Keith Randall083a6462015-05-12 11:06:44 -07003834 // Remember where each block starts.
Keith Randall9569b952015-08-28 22:51:01 -07003835 s.bstart = make([]*obj.Prog, f.NumBlocks())
Keith Randall083a6462015-05-12 11:06:44 -07003836
Josh Bleecher Snyderb8efee02015-07-31 14:37:15 -07003837 var valueProgs map[*obj.Prog]*ssa.Value
3838 var blockProgs map[*obj.Prog]*ssa.Block
Dave Cheneycb1f2af2016-03-17 13:46:43 +11003839 var logProgs = e.log
Josh Bleecher Snyderb8efee02015-07-31 14:37:15 -07003840 if logProgs {
3841 valueProgs = make(map[*obj.Prog]*ssa.Value, f.NumValues())
3842 blockProgs = make(map[*obj.Prog]*ssa.Block, f.NumBlocks())
3843 f.Logf("genssa %s\n", f.Name)
3844 blockProgs[Pc] = f.Blocks[0]
3845 }
3846
Keith Randall083a6462015-05-12 11:06:44 -07003847 // Emit basic blocks
3848 for i, b := range f.Blocks {
Keith Randall9569b952015-08-28 22:51:01 -07003849 s.bstart[b.ID] = Pc
Keith Randall083a6462015-05-12 11:06:44 -07003850 // Emit values in block
Michael Pratta4e31d42016-03-12 14:07:40 -08003851 Thearch.SSAMarkMoves(&s, b)
Keith Randall083a6462015-05-12 11:06:44 -07003852 for _, v := range b.Values {
Josh Bleecher Snyderb8efee02015-07-31 14:37:15 -07003853 x := Pc
Michael Pratta4e31d42016-03-12 14:07:40 -08003854 Thearch.SSAGenValue(&s, v)
Josh Bleecher Snyderb8efee02015-07-31 14:37:15 -07003855 if logProgs {
3856 for ; x != Pc; x = x.Link {
3857 valueProgs[x] = v
3858 }
3859 }
Keith Randall083a6462015-05-12 11:06:44 -07003860 }
3861 // Emit control flow instructions for block
3862 var next *ssa.Block
Keith Randall91f69c62016-02-26 16:32:01 -08003863 if i < len(f.Blocks)-1 && (Debug['N'] == 0 || b.Kind == ssa.BlockCall) {
Keith Randall8906d2a2016-02-22 23:19:00 -08003864 // If -N, leave next==nil so every block with successors
Keith Randall91f69c62016-02-26 16:32:01 -08003865 // ends in a JMP (except call blocks - plive doesn't like
3866 // select{send,recv} followed by a JMP call). Helps keep
3867 // line numbers for otherwise empty blocks.
Keith Randall083a6462015-05-12 11:06:44 -07003868 next = f.Blocks[i+1]
3869 }
Josh Bleecher Snyderb8efee02015-07-31 14:37:15 -07003870 x := Pc
Michael Pratta4e31d42016-03-12 14:07:40 -08003871 Thearch.SSAGenBlock(&s, b, next)
Josh Bleecher Snyderb8efee02015-07-31 14:37:15 -07003872 if logProgs {
3873 for ; x != Pc; x = x.Link {
3874 blockProgs[x] = b
3875 }
3876 }
Keith Randall083a6462015-05-12 11:06:44 -07003877 }
3878
3879 // Resolve branches
Michael Pratta4e31d42016-03-12 14:07:40 -08003880 for _, br := range s.Branches {
3881 br.P.To.Val = s.bstart[br.B.ID]
Keith Randall9569b952015-08-28 22:51:01 -07003882 }
Keith Randall083a6462015-05-12 11:06:44 -07003883
Josh Bleecher Snyderb8efee02015-07-31 14:37:15 -07003884 if logProgs {
3885 for p := ptxt; p != nil; p = p.Link {
3886 var s string
3887 if v, ok := valueProgs[p]; ok {
3888 s = v.String()
3889 } else if b, ok := blockProgs[p]; ok {
3890 s = b.String()
3891 } else {
3892 s = " " // most value and branch strings are 2-3 characters long
3893 }
3894 f.Logf("%s\t%s\n", s, p)
3895 }
Josh Bleecher Snyder35fb5142015-08-10 12:15:52 -07003896 if f.Config.HTML != nil {
3897 saved := ptxt.Ctxt.LineHist.PrintFilenameOnly
3898 ptxt.Ctxt.LineHist.PrintFilenameOnly = true
3899 var buf bytes.Buffer
3900 buf.WriteString("<code>")
3901 buf.WriteString("<dl class=\"ssa-gen\">")
3902 for p := ptxt; p != nil; p = p.Link {
3903 buf.WriteString("<dt class=\"ssa-prog-src\">")
3904 if v, ok := valueProgs[p]; ok {
3905 buf.WriteString(v.HTML())
3906 } else if b, ok := blockProgs[p]; ok {
3907 buf.WriteString(b.HTML())
3908 }
3909 buf.WriteString("</dt>")
3910 buf.WriteString("<dd class=\"ssa-prog\">")
3911 buf.WriteString(html.EscapeString(p.String()))
3912 buf.WriteString("</dd>")
3913 buf.WriteString("</li>")
3914 }
3915 buf.WriteString("</dl>")
3916 buf.WriteString("</code>")
3917 f.Config.HTML.WriteColumn("genssa", buf.String())
3918 ptxt.Ctxt.LineHist.PrintFilenameOnly = saved
3919 }
Josh Bleecher Snyderb8efee02015-07-31 14:37:15 -07003920 }
3921
Josh Bleecher Snyder6b416652015-07-28 10:56:39 -07003922 // Emit static data
3923 if f.StaticData != nil {
3924 for _, n := range f.StaticData.([]*Node) {
3925 if !gen_as_init(n, false) {
Keith Randall0ec72b62015-09-08 15:42:53 -07003926 Fatalf("non-static data marked as static: %v\n\n", n, f)
Josh Bleecher Snyder6b416652015-07-28 10:56:39 -07003927 }
3928 }
3929 }
3930
Keith Randalld2107fc2015-08-24 02:16:19 -07003931 // Allocate stack frame
3932 allocauto(ptxt)
Keith Randall083a6462015-05-12 11:06:44 -07003933
Keith Randalld2107fc2015-08-24 02:16:19 -07003934 // Generate gc bitmaps.
3935 liveness(Curfn, ptxt, gcargs, gclocals)
3936 gcsymdup(gcargs)
3937 gcsymdup(gclocals)
Keith Randall083a6462015-05-12 11:06:44 -07003938
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00003939 // Add frame prologue. Zero ambiguously live variables.
Keith Randalld2107fc2015-08-24 02:16:19 -07003940 Thearch.Defframe(ptxt)
3941 if Debug['f'] != 0 {
3942 frame(0)
3943 }
3944
3945 // Remove leftover instrumentation from the instruction stream.
3946 removevardef(ptxt)
Josh Bleecher Snyder35fb5142015-08-10 12:15:52 -07003947
3948 f.Config.HTML.Close()
Keith Randall083a6462015-05-12 11:06:44 -07003949}
3950
Daniel Morsing66b47812015-06-27 15:45:20 +01003951// movZero generates a register indirect move with a 0 immediate and keeps track of bytes left and next offset
Matthew Dempsky0d9258a2016-03-07 18:00:08 -08003952func movZero(as obj.As, width int64, nbytes int64, offset int64, regnum int16) (nleft int64, noff int64) {
Daniel Morsing66b47812015-06-27 15:45:20 +01003953 p := Prog(as)
3954 // TODO: use zero register on archs that support it.
3955 p.From.Type = obj.TYPE_CONST
3956 p.From.Offset = 0
3957 p.To.Type = obj.TYPE_MEM
3958 p.To.Reg = regnum
3959 p.To.Offset = offset
3960 offset += width
3961 nleft = nbytes - width
3962 return nleft, offset
3963}
3964
Michael Pratta4e31d42016-03-12 14:07:40 -08003965type FloatingEQNEJump struct {
3966 Jump obj.As
3967 Index int
David Chase8e601b22015-08-18 14:39:26 -04003968}
3969
Michael Pratta4e31d42016-03-12 14:07:40 -08003970func oneFPJump(b *ssa.Block, jumps *FloatingEQNEJump, likely ssa.BranchPrediction, branches []Branch) []Branch {
3971 p := Prog(jumps.Jump)
David Chase8e601b22015-08-18 14:39:26 -04003972 p.To.Type = obj.TYPE_BRANCH
Michael Pratta4e31d42016-03-12 14:07:40 -08003973 to := jumps.Index
3974 branches = append(branches, Branch{p, b.Succs[to]})
David Chase8e601b22015-08-18 14:39:26 -04003975 if to == 1 {
3976 likely = -likely
3977 }
3978 // liblink reorders the instruction stream as it sees fit.
3979 // Pass along what we know so liblink can make use of it.
3980 // TODO: Once we've fully switched to SSA,
3981 // make liblink leave our output alone.
3982 switch likely {
3983 case ssa.BranchUnlikely:
3984 p.From.Type = obj.TYPE_CONST
3985 p.From.Offset = 0
3986 case ssa.BranchLikely:
3987 p.From.Type = obj.TYPE_CONST
3988 p.From.Offset = 1
3989 }
3990 return branches
3991}
3992
Michael Pratta4e31d42016-03-12 14:07:40 -08003993func SSAGenFPJump(s *SSAGenState, b, next *ssa.Block, jumps *[2][2]FloatingEQNEJump) {
David Chase8e601b22015-08-18 14:39:26 -04003994 likely := b.Likely
3995 switch next {
3996 case b.Succs[0]:
Michael Pratta4e31d42016-03-12 14:07:40 -08003997 s.Branches = oneFPJump(b, &jumps[0][0], likely, s.Branches)
3998 s.Branches = oneFPJump(b, &jumps[0][1], likely, s.Branches)
David Chase8e601b22015-08-18 14:39:26 -04003999 case b.Succs[1]:
Michael Pratta4e31d42016-03-12 14:07:40 -08004000 s.Branches = oneFPJump(b, &jumps[1][0], likely, s.Branches)
4001 s.Branches = oneFPJump(b, &jumps[1][1], likely, s.Branches)
David Chase8e601b22015-08-18 14:39:26 -04004002 default:
Michael Pratta4e31d42016-03-12 14:07:40 -08004003 s.Branches = oneFPJump(b, &jumps[1][0], likely, s.Branches)
4004 s.Branches = oneFPJump(b, &jumps[1][1], likely, s.Branches)
David Chase8e601b22015-08-18 14:39:26 -04004005 q := Prog(obj.AJMP)
4006 q.To.Type = obj.TYPE_BRANCH
Michael Pratta4e31d42016-03-12 14:07:40 -08004007 s.Branches = append(s.Branches, Branch{q, b.Succs[1]})
David Chase8e601b22015-08-18 14:39:26 -04004008 }
Josh Bleecher Snyder71b57072015-07-24 12:47:00 -07004009}
4010
Michael Pratta4e31d42016-03-12 14:07:40 -08004011// AddAux adds the offset in the aux fields (AuxInt and Aux) of v to a.
4012func AddAux(a *obj.Addr, v *ssa.Value) {
4013 AddAux2(a, v, v.AuxInt)
Keith Randall083a6462015-05-12 11:06:44 -07004014}
Michael Pratta4e31d42016-03-12 14:07:40 -08004015func AddAux2(a *obj.Addr, v *ssa.Value, offset int64) {
Keith Randall8c46aa52015-06-19 21:02:28 -07004016 if a.Type != obj.TYPE_MEM {
Michael Pratta4e31d42016-03-12 14:07:40 -08004017 v.Fatalf("bad AddAux addr %s", a)
Keith Randall8c46aa52015-06-19 21:02:28 -07004018 }
4019 // add integer offset
Keith Randalld43f2e32015-10-21 13:13:56 -07004020 a.Offset += offset
Keith Randall8c46aa52015-06-19 21:02:28 -07004021
4022 // If no additional symbol offset, we're done.
4023 if v.Aux == nil {
4024 return
4025 }
4026 // Add symbol's offset from its base register.
4027 switch sym := v.Aux.(type) {
4028 case *ssa.ExternSymbol:
4029 a.Name = obj.NAME_EXTERN
Ian Lance Taylor65b40202016-03-16 22:22:58 -07004030 switch s := sym.Sym.(type) {
4031 case *Sym:
4032 a.Sym = Linksym(s)
4033 case *obj.LSym:
4034 a.Sym = s
4035 default:
4036 v.Fatalf("ExternSymbol.Sym is %T", s)
4037 }
Keith Randall8c46aa52015-06-19 21:02:28 -07004038 case *ssa.ArgSymbol:
Keith Randalld2107fc2015-08-24 02:16:19 -07004039 n := sym.Node.(*Node)
4040 a.Name = obj.NAME_PARAM
4041 a.Node = n
4042 a.Sym = Linksym(n.Orig.Sym)
4043 a.Offset += n.Xoffset // TODO: why do I have to add this here? I don't for auto variables.
Keith Randall8c46aa52015-06-19 21:02:28 -07004044 case *ssa.AutoSymbol:
Keith Randalld2107fc2015-08-24 02:16:19 -07004045 n := sym.Node.(*Node)
4046 a.Name = obj.NAME_AUTO
4047 a.Node = n
4048 a.Sym = Linksym(n.Sym)
Keith Randall8c46aa52015-06-19 21:02:28 -07004049 default:
4050 v.Fatalf("aux in %s not implemented %#v", v, v.Aux)
4051 }
4052}
4053
Keith Randall582baae2015-11-02 21:28:13 -08004054// extendIndex extends v to a full int width.
Keith Randall2a5e6c42015-07-23 14:35:02 -07004055func (s *state) extendIndex(v *ssa.Value) *ssa.Value {
4056 size := v.Type.Size()
Keith Randall582baae2015-11-02 21:28:13 -08004057 if size == s.config.IntSize {
Keith Randall2a5e6c42015-07-23 14:35:02 -07004058 return v
4059 }
Keith Randall582baae2015-11-02 21:28:13 -08004060 if size > s.config.IntSize {
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00004061 // TODO: truncate 64-bit indexes on 32-bit pointer archs. We'd need to test
Keith Randall2a5e6c42015-07-23 14:35:02 -07004062 // the high word and branch to out-of-bounds failure if it is not 0.
4063 s.Unimplementedf("64->32 index truncation not implemented")
4064 return v
4065 }
4066
4067 // Extend value to the required size
4068 var op ssa.Op
4069 if v.Type.IsSigned() {
Keith Randall582baae2015-11-02 21:28:13 -08004070 switch 10*size + s.config.IntSize {
Keith Randall2a5e6c42015-07-23 14:35:02 -07004071 case 14:
4072 op = ssa.OpSignExt8to32
4073 case 18:
4074 op = ssa.OpSignExt8to64
4075 case 24:
4076 op = ssa.OpSignExt16to32
4077 case 28:
4078 op = ssa.OpSignExt16to64
4079 case 48:
4080 op = ssa.OpSignExt32to64
4081 default:
4082 s.Fatalf("bad signed index extension %s", v.Type)
4083 }
4084 } else {
Keith Randall582baae2015-11-02 21:28:13 -08004085 switch 10*size + s.config.IntSize {
Keith Randall2a5e6c42015-07-23 14:35:02 -07004086 case 14:
4087 op = ssa.OpZeroExt8to32
4088 case 18:
4089 op = ssa.OpZeroExt8to64
4090 case 24:
4091 op = ssa.OpZeroExt16to32
4092 case 28:
4093 op = ssa.OpZeroExt16to64
4094 case 48:
4095 op = ssa.OpZeroExt32to64
4096 default:
4097 s.Fatalf("bad unsigned index extension %s", v.Type)
4098 }
4099 }
Keith Randall582baae2015-11-02 21:28:13 -08004100 return s.newValue1(op, Types[TINT], v)
Keith Randall2a5e6c42015-07-23 14:35:02 -07004101}
4102
Michael Pratta4e31d42016-03-12 14:07:40 -08004103// SSARegNum returns the register (in cmd/internal/obj numbering) to
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +00004104// which v has been allocated. Panics if v is not assigned to a
Keith Randall083a6462015-05-12 11:06:44 -07004105// register.
Josh Bleecher Snydere1395492015-08-05 16:06:39 -07004106// TODO: Make this panic again once it stops happening routinely.
Michael Pratta4e31d42016-03-12 14:07:40 -08004107func SSARegNum(v *ssa.Value) int16 {
Josh Bleecher Snydere1395492015-08-05 16:06:39 -07004108 reg := v.Block.Func.RegAlloc[v.ID]
4109 if reg == nil {
4110 v.Unimplementedf("nil regnum for value: %s\n%s\n", v.LongString(), v.Block.Func)
4111 return 0
4112 }
Michael Pratta4e31d42016-03-12 14:07:40 -08004113 return Thearch.SSARegToReg[reg.(*ssa.Register).Num]
Keith Randall083a6462015-05-12 11:06:44 -07004114}
4115
Michael Pratta4e31d42016-03-12 14:07:40 -08004116// AutoVar returns a *Node and int64 representing the auto variable and offset within it
Keith Randall02f4d0a2015-11-02 08:10:26 -08004117// where v should be spilled.
Michael Pratta4e31d42016-03-12 14:07:40 -08004118func AutoVar(v *ssa.Value) (*Node, int64) {
Keith Randall02f4d0a2015-11-02 08:10:26 -08004119 loc := v.Block.Func.RegAlloc[v.ID].(ssa.LocalSlot)
Keith Randall9094e3a2016-01-04 13:34:54 -08004120 if v.Type.Size() > loc.Type.Size() {
4121 v.Fatalf("spill/restore type %s doesn't fit in slot type %s", v.Type, loc.Type)
4122 }
Keith Randall02f4d0a2015-11-02 08:10:26 -08004123 return loc.N.(*Node), loc.Off
Keith Randall083a6462015-05-12 11:06:44 -07004124}
Keith Randallf7f604e2015-05-27 14:52:22 -07004125
Keith Randalla734bbc2016-01-11 21:05:33 -08004126// fieldIdx finds the index of the field referred to by the ODOT node n.
Matthew Dempsky1b9f1682016-03-14 12:45:18 -07004127func fieldIdx(n *Node) int {
Keith Randalla734bbc2016-01-11 21:05:33 -08004128 t := n.Left.Type
Ian Lance Taylor5f525ca2016-03-18 16:52:30 -07004129 f := n.Sym
Matthew Dempsky3efefd92016-03-30 14:56:08 -07004130 if !t.IsStruct() {
Keith Randalla734bbc2016-01-11 21:05:33 -08004131 panic("ODOT's LHS is not a struct")
4132 }
4133
Matthew Dempsky1b9f1682016-03-14 12:45:18 -07004134 var i int
Matthew Dempskyf6bca3f2016-03-17 01:32:18 -07004135 for _, t1 := range t.Fields().Slice() {
Ian Lance Taylor5f525ca2016-03-18 16:52:30 -07004136 if t1.Sym != f {
Keith Randalla734bbc2016-01-11 21:05:33 -08004137 i++
4138 continue
4139 }
Matthew Dempsky62dddd42016-03-28 09:40:53 -07004140 if t1.Offset != n.Xoffset {
Keith Randalla734bbc2016-01-11 21:05:33 -08004141 panic("field offset doesn't match")
4142 }
4143 return i
4144 }
4145 panic(fmt.Sprintf("can't find field in expr %s\n", n))
4146
Eric Engestrom7a8caf72016-04-03 12:43:27 +01004147 // TODO: keep the result of this function somewhere in the ODOT Node
Keith Randalla734bbc2016-01-11 21:05:33 -08004148 // so we don't have to recompute it each time we need it.
4149}
4150
Keith Randallf7f604e2015-05-27 14:52:22 -07004151// ssaExport exports a bunch of compiler services for the ssa backend.
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -07004152type ssaExport struct {
4153 log bool
4154 unimplemented bool
Josh Bleecher Snyderd2982092015-07-22 13:13:53 -07004155 mustImplement bool
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -07004156}
Keith Randallf7f604e2015-05-27 14:52:22 -07004157
Josh Bleecher Snyder85e03292015-07-30 11:03:05 -07004158func (s *ssaExport) TypeBool() ssa.Type { return Types[TBOOL] }
4159func (s *ssaExport) TypeInt8() ssa.Type { return Types[TINT8] }
4160func (s *ssaExport) TypeInt16() ssa.Type { return Types[TINT16] }
4161func (s *ssaExport) TypeInt32() ssa.Type { return Types[TINT32] }
4162func (s *ssaExport) TypeInt64() ssa.Type { return Types[TINT64] }
4163func (s *ssaExport) TypeUInt8() ssa.Type { return Types[TUINT8] }
4164func (s *ssaExport) TypeUInt16() ssa.Type { return Types[TUINT16] }
4165func (s *ssaExport) TypeUInt32() ssa.Type { return Types[TUINT32] }
4166func (s *ssaExport) TypeUInt64() ssa.Type { return Types[TUINT64] }
David Chase52578582015-08-28 14:24:10 -04004167func (s *ssaExport) TypeFloat32() ssa.Type { return Types[TFLOAT32] }
4168func (s *ssaExport) TypeFloat64() ssa.Type { return Types[TFLOAT64] }
Josh Bleecher Snyder85e03292015-07-30 11:03:05 -07004169func (s *ssaExport) TypeInt() ssa.Type { return Types[TINT] }
4170func (s *ssaExport) TypeUintptr() ssa.Type { return Types[TUINTPTR] }
4171func (s *ssaExport) TypeString() ssa.Type { return Types[TSTRING] }
4172func (s *ssaExport) TypeBytePtr() ssa.Type { return Ptrto(Types[TUINT8]) }
4173
Josh Bleecher Snyder8d31df18a2015-07-24 11:28:12 -07004174// StringData returns a symbol (a *Sym wrapped in an interface) which
4175// is the data component of a global string constant containing s.
4176func (*ssaExport) StringData(s string) interface{} {
Keith Randall8c46aa52015-06-19 21:02:28 -07004177 // TODO: is idealstring correct? It might not matter...
Josh Bleecher Snyder8d31df18a2015-07-24 11:28:12 -07004178 _, data := stringsym(s)
4179 return &ssa.ExternSymbol{Typ: idealstring, Sym: data}
Keith Randallf7f604e2015-05-27 14:52:22 -07004180}
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -07004181
Keith Randallc24681a2015-10-22 14:22:38 -07004182func (e *ssaExport) Auto(t ssa.Type) ssa.GCNode {
Keith Randalld2107fc2015-08-24 02:16:19 -07004183 n := temp(t.(*Type)) // Note: adds new auto to Curfn.Func.Dcl list
4184 e.mustImplement = true // This modifies the input to SSA, so we want to make sure we succeed from here!
4185 return n
4186}
4187
Keith Randall4a7aba72016-03-28 11:25:17 -07004188func (e *ssaExport) SplitString(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot) {
4189 n := name.N.(*Node)
4190 ptrType := Ptrto(Types[TUINT8])
4191 lenType := Types[TINT]
4192 if n.Class == PAUTO && !n.Addrtaken {
4193 // Split this string up into two separate variables.
4194 p := e.namedAuto(n.Sym.Name+".ptr", ptrType)
4195 l := e.namedAuto(n.Sym.Name+".len", lenType)
4196 return ssa.LocalSlot{p, ptrType, 0}, ssa.LocalSlot{l, lenType, 0}
4197 }
4198 // Return the two parts of the larger variable.
4199 return ssa.LocalSlot{n, ptrType, name.Off}, ssa.LocalSlot{n, lenType, name.Off + int64(Widthptr)}
4200}
4201
4202func (e *ssaExport) SplitInterface(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot) {
4203 n := name.N.(*Node)
4204 t := Ptrto(Types[TUINT8])
4205 if n.Class == PAUTO && !n.Addrtaken {
4206 // Split this interface up into two separate variables.
4207 f := ".itab"
Matthew Dempsky00e5a682016-04-01 13:36:24 -07004208 if n.Type.IsEmptyInterface() {
Keith Randall4a7aba72016-03-28 11:25:17 -07004209 f = ".type"
4210 }
4211 c := e.namedAuto(n.Sym.Name+f, t)
4212 d := e.namedAuto(n.Sym.Name+".data", t)
4213 return ssa.LocalSlot{c, t, 0}, ssa.LocalSlot{d, t, 0}
4214 }
4215 // Return the two parts of the larger variable.
4216 return ssa.LocalSlot{n, t, name.Off}, ssa.LocalSlot{n, t, name.Off + int64(Widthptr)}
4217}
4218
4219func (e *ssaExport) SplitSlice(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot, ssa.LocalSlot) {
4220 n := name.N.(*Node)
Josh Bleecher Snyderf38f43d2016-04-01 20:11:30 -07004221 ptrType := Ptrto(n.Type.Elem())
Keith Randall4a7aba72016-03-28 11:25:17 -07004222 lenType := Types[TINT]
4223 if n.Class == PAUTO && !n.Addrtaken {
4224 // Split this slice up into three separate variables.
4225 p := e.namedAuto(n.Sym.Name+".ptr", ptrType)
4226 l := e.namedAuto(n.Sym.Name+".len", lenType)
4227 c := e.namedAuto(n.Sym.Name+".cap", lenType)
4228 return ssa.LocalSlot{p, ptrType, 0}, ssa.LocalSlot{l, lenType, 0}, ssa.LocalSlot{c, lenType, 0}
4229 }
4230 // Return the three parts of the larger variable.
4231 return ssa.LocalSlot{n, ptrType, name.Off},
4232 ssa.LocalSlot{n, lenType, name.Off + int64(Widthptr)},
4233 ssa.LocalSlot{n, lenType, name.Off + int64(2*Widthptr)}
4234}
4235
4236func (e *ssaExport) SplitComplex(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot) {
4237 n := name.N.(*Node)
4238 s := name.Type.Size() / 2
4239 var t *Type
4240 if s == 8 {
4241 t = Types[TFLOAT64]
4242 } else {
4243 t = Types[TFLOAT32]
4244 }
4245 if n.Class == PAUTO && !n.Addrtaken {
4246 // Split this complex up into two separate variables.
4247 c := e.namedAuto(n.Sym.Name+".real", t)
4248 d := e.namedAuto(n.Sym.Name+".imag", t)
4249 return ssa.LocalSlot{c, t, 0}, ssa.LocalSlot{d, t, 0}
4250 }
4251 // Return the two parts of the larger variable.
4252 return ssa.LocalSlot{n, t, name.Off}, ssa.LocalSlot{n, t, name.Off + s}
4253}
4254
4255// namedAuto returns a new AUTO variable with the given name and type.
4256func (e *ssaExport) namedAuto(name string, typ ssa.Type) ssa.GCNode {
4257 t := typ.(*Type)
4258 s := Lookup(name)
4259 n := Nod(ONAME, nil, nil)
4260 s.Def = n
4261 s.Def.Used = true
4262 n.Sym = s
4263 n.Type = t
4264 n.Class = PAUTO
4265 n.Addable = true
4266 n.Ullman = 1
4267 n.Esc = EscNever
4268 n.Xoffset = 0
4269 n.Name.Curfn = Curfn
4270 Curfn.Func.Dcl = append(Curfn.Func.Dcl, n)
4271
4272 dowidth(t)
4273 e.mustImplement = true
4274
4275 return n
4276}
4277
Keith Randall7d612462015-10-22 13:07:38 -07004278func (e *ssaExport) CanSSA(t ssa.Type) bool {
Keith Randall37590bd2015-09-18 22:58:10 -07004279 return canSSAType(t.(*Type))
4280}
4281
Keith Randallb5c5efd2016-01-14 16:02:23 -08004282func (e *ssaExport) Line(line int32) string {
Robert Griesemer2faf5bc2016-03-02 11:30:29 -08004283 return linestr(line)
Keith Randallb5c5efd2016-01-14 16:02:23 -08004284}
4285
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -07004286// Log logs a message from the compiler.
Josh Bleecher Snyder37ddc272015-06-24 14:03:39 -07004287func (e *ssaExport) Logf(msg string, args ...interface{}) {
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -07004288 // If e was marked as unimplemented, anything could happen. Ignore.
4289 if e.log && !e.unimplemented {
4290 fmt.Printf(msg, args...)
4291 }
4292}
4293
David Chase88b230e2016-01-29 14:44:15 -05004294func (e *ssaExport) Log() bool {
4295 return e.log
4296}
4297
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -07004298// Fatal reports a compiler error and exits.
Keith Randallda8af472016-01-13 11:14:57 -08004299func (e *ssaExport) Fatalf(line int32, msg string, args ...interface{}) {
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -07004300 // If e was marked as unimplemented, anything could happen. Ignore.
4301 if !e.unimplemented {
Keith Randallda8af472016-01-13 11:14:57 -08004302 lineno = line
Keith Randall0ec72b62015-09-08 15:42:53 -07004303 Fatalf(msg, args...)
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -07004304 }
4305}
4306
4307// Unimplemented reports that the function cannot be compiled.
4308// It will be removed once SSA work is complete.
Keith Randallda8af472016-01-13 11:14:57 -08004309func (e *ssaExport) Unimplementedf(line int32, msg string, args ...interface{}) {
Josh Bleecher Snyderd2982092015-07-22 13:13:53 -07004310 if e.mustImplement {
Keith Randallda8af472016-01-13 11:14:57 -08004311 lineno = line
Keith Randall0ec72b62015-09-08 15:42:53 -07004312 Fatalf(msg, args...)
Josh Bleecher Snyderd2982092015-07-22 13:13:53 -07004313 }
Josh Bleecher Snyder8c6abfe2015-06-12 11:01:13 -07004314 const alwaysLog = false // enable to calculate top unimplemented features
4315 if !e.unimplemented && (e.log || alwaysLog) {
4316 // first implementation failure, print explanation
4317 fmt.Printf("SSA unimplemented: "+msg+"\n", args...)
4318 }
4319 e.unimplemented = true
4320}
Keith Randallc24681a2015-10-22 14:22:38 -07004321
David Chase729abfa2015-10-26 17:34:06 -04004322// Warnl reports a "warning", which is usually flag-triggered
4323// logging output for the benefit of tests.
Todd Neal98b88de2016-03-13 23:04:31 -05004324func (e *ssaExport) Warnl(line int32, fmt_ string, args ...interface{}) {
4325 Warnl(line, fmt_, args...)
David Chase729abfa2015-10-26 17:34:06 -04004326}
4327
4328func (e *ssaExport) Debug_checknil() bool {
4329 return Debug_checknil != 0
4330}
4331
Keith Randallc24681a2015-10-22 14:22:38 -07004332func (n *Node) Typ() ssa.Type {
4333 return n.Type
4334}