github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/cmd/compile/internal/gc/ssa.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package gc 6 7 import ( 8 "bufio" 9 "bytes" 10 "encoding/binary" 11 "fmt" 12 "html" 13 "os" 14 "sort" 15 16 "cmd/compile/internal/ssa" 17 "cmd/compile/internal/types" 18 "cmd/internal/obj" 19 "cmd/internal/objabi" 20 "cmd/internal/src" 21 "cmd/internal/sys" 22 ) 23 24 var ssaConfig *ssa.Config 25 var ssaCaches []ssa.Cache 26 27 var ssaDump string // early copy of $GOSSAFUNC; the func name to dump output for 28 var ssaDumpStdout bool // whether to dump to stdout 29 var ssaDumpCFG string // generate CFGs for these phases 30 const ssaDumpFile = "ssa.html" 31 32 // ssaDumpInlined holds all inlined functions when ssaDump contains a function name. 33 var ssaDumpInlined []*Node 34 35 func initssaconfig() { 36 types_ := ssa.NewTypes() 37 38 if thearch.SoftFloat { 39 softfloatInit() 40 } 41 42 // Generate a few pointer types that are uncommon in the frontend but common in the backend. 43 // Caching is disabled in the backend, so generating these here avoids allocations. 44 _ = types.NewPtr(types.Types[TINTER]) // *interface{} 45 _ = types.NewPtr(types.NewPtr(types.Types[TSTRING])) // **string 46 _ = types.NewPtr(types.NewPtr(types.Idealstring)) // **string 47 _ = types.NewPtr(types.NewSlice(types.Types[TINTER])) // *[]interface{} 48 _ = types.NewPtr(types.NewPtr(types.Bytetype)) // **byte 49 _ = types.NewPtr(types.NewSlice(types.Bytetype)) // *[]byte 50 _ = types.NewPtr(types.NewSlice(types.Types[TSTRING])) // *[]string 51 _ = types.NewPtr(types.NewSlice(types.Idealstring)) // *[]string 52 _ = types.NewPtr(types.NewPtr(types.NewPtr(types.Types[TUINT8]))) // ***uint8 53 _ = types.NewPtr(types.Types[TINT16]) // *int16 54 _ = types.NewPtr(types.Types[TINT64]) // *int64 55 _ = types.NewPtr(types.Errortype) // *error 56 types.NewPtrCacheEnabled = false 57 ssaConfig = ssa.NewConfig(thearch.LinkArch.Name, *types_, Ctxt, Debug['N'] == 0) 58 if thearch.LinkArch.Name == "386" { 59 ssaConfig.Set387(thearch.Use387) 60 } 61 ssaConfig.SoftFloat = thearch.SoftFloat 62 ssaConfig.Race = flag_race 63 ssaCaches = make([]ssa.Cache, nBackendWorkers) 64 65 // Set up some runtime functions we'll need to call. 66 assertE2I = sysfunc("assertE2I") 67 assertE2I2 = sysfunc("assertE2I2") 68 assertI2I = sysfunc("assertI2I") 69 assertI2I2 = sysfunc("assertI2I2") 70 deferproc = sysfunc("deferproc") 71 Deferreturn = sysfunc("deferreturn") 72 Duffcopy = sysvar("duffcopy") // asm func with special ABI 73 Duffzero = sysvar("duffzero") // asm func with special ABI 74 gcWriteBarrier = sysvar("gcWriteBarrier") // asm func with special ABI 75 goschedguarded = sysfunc("goschedguarded") 76 growslice = sysfunc("growslice") 77 msanread = sysfunc("msanread") 78 msanwrite = sysfunc("msanwrite") 79 newproc = sysfunc("newproc") 80 panicdivide = sysfunc("panicdivide") 81 panicdottypeE = sysfunc("panicdottypeE") 82 panicdottypeI = sysfunc("panicdottypeI") 83 panicindex = sysfunc("panicindex") 84 panicnildottype = sysfunc("panicnildottype") 85 panicoverflow = sysfunc("panicoverflow") 86 panicslice = sysfunc("panicslice") 87 raceread = sysfunc("raceread") 88 racereadrange = sysfunc("racereadrange") 89 racewrite = sysfunc("racewrite") 90 racewriterange = sysfunc("racewriterange") 91 x86HasPOPCNT = sysvar("x86HasPOPCNT") // bool 92 x86HasSSE41 = sysvar("x86HasSSE41") // bool 93 arm64HasATOMICS = sysvar("arm64HasATOMICS") // bool 94 typedmemclr = sysfunc("typedmemclr") 95 typedmemmove = sysfunc("typedmemmove") 96 Udiv = sysvar("udiv") // asm func with special ABI 97 writeBarrier = sysvar("writeBarrier") // struct { bool; ... } 98 99 // GO386=387 runtime definitions 100 ControlWord64trunc = sysvar("controlWord64trunc") // uint16 101 ControlWord32 = sysvar("controlWord32") // uint16 102 103 // Wasm (all asm funcs with special ABIs) 104 WasmMove = sysvar("wasmMove") 105 WasmZero = sysvar("wasmZero") 106 WasmDiv = sysvar("wasmDiv") 107 WasmTruncS = sysvar("wasmTruncS") 108 WasmTruncU = sysvar("wasmTruncU") 109 SigPanic = sysfunc("sigpanic") 110 } 111 112 // buildssa builds an SSA function for fn. 113 // worker indicates which of the backend workers is doing the processing. 114 func buildssa(fn *Node, worker int) *ssa.Func { 115 name := fn.funcname() 116 printssa := name == ssaDump 117 var astBuf *bytes.Buffer 118 if printssa { 119 astBuf = &bytes.Buffer{} 120 fdumplist(astBuf, "buildssa-enter", fn.Func.Enter) 121 fdumplist(astBuf, "buildssa-body", fn.Nbody) 122 fdumplist(astBuf, "buildssa-exit", fn.Func.Exit) 123 if ssaDumpStdout { 124 fmt.Println("generating SSA for", name) 125 fmt.Print(astBuf.String()) 126 } 127 } 128 129 var s state 130 s.pushLine(fn.Pos) 131 defer s.popLine() 132 133 s.hasdefer = fn.Func.HasDefer() 134 if fn.Func.Pragma&CgoUnsafeArgs != 0 { 135 s.cgoUnsafeArgs = true 136 } 137 138 fe := ssafn{ 139 curfn: fn, 140 log: printssa && ssaDumpStdout, 141 } 142 s.curfn = fn 143 144 s.f = ssa.NewFunc(&fe) 145 s.config = ssaConfig 146 s.f.Type = fn.Type 147 s.f.Config = ssaConfig 148 s.f.Cache = &ssaCaches[worker] 149 s.f.Cache.Reset() 150 s.f.DebugTest = s.f.DebugHashMatch("GOSSAHASH", name) 151 s.f.Name = name 152 s.f.PrintOrHtmlSSA = printssa 153 if fn.Func.Pragma&Nosplit != 0 { 154 s.f.NoSplit = true 155 } 156 s.panics = map[funcLine]*ssa.Block{} 157 s.softFloat = s.config.SoftFloat 158 159 if printssa { 160 s.f.HTMLWriter = ssa.NewHTMLWriter(ssaDumpFile, s.f.Frontend(), name, ssaDumpCFG) 161 // TODO: generate and print a mapping from nodes to values and blocks 162 dumpSourcesColumn(s.f.HTMLWriter, fn) 163 s.f.HTMLWriter.WriteAST("AST", astBuf) 164 } 165 166 // Allocate starting block 167 s.f.Entry = s.f.NewBlock(ssa.BlockPlain) 168 169 // Allocate starting values 170 s.labels = map[string]*ssaLabel{} 171 s.labeledNodes = map[*Node]*ssaLabel{} 172 s.fwdVars = map[*Node]*ssa.Value{} 173 s.startmem = s.entryNewValue0(ssa.OpInitMem, types.TypeMem) 174 s.sp = s.entryNewValue0(ssa.OpSP, types.Types[TUINTPTR]) // TODO: use generic pointer type (unsafe.Pointer?) instead 175 s.sb = s.entryNewValue0(ssa.OpSB, types.Types[TUINTPTR]) 176 177 s.startBlock(s.f.Entry) 178 s.vars[&memVar] = s.startmem 179 180 // Generate addresses of local declarations 181 s.decladdrs = map[*Node]*ssa.Value{} 182 for _, n := range fn.Func.Dcl { 183 switch n.Class() { 184 case PPARAM, PPARAMOUT: 185 s.decladdrs[n] = s.entryNewValue2A(ssa.OpLocalAddr, types.NewPtr(n.Type), n, s.sp, s.startmem) 186 if n.Class() == PPARAMOUT && s.canSSA(n) { 187 // Save ssa-able PPARAMOUT variables so we can 188 // store them back to the stack at the end of 189 // the function. 190 s.returns = append(s.returns, n) 191 } 192 case PAUTO: 193 // processed at each use, to prevent Addr coming 194 // before the decl. 195 case PAUTOHEAP: 196 // moved to heap - already handled by frontend 197 case PFUNC: 198 // local function - already handled by frontend 199 default: 200 s.Fatalf("local variable with class %v unimplemented", n.Class()) 201 } 202 } 203 204 // Populate SSAable arguments. 205 for _, n := range fn.Func.Dcl { 206 if n.Class() == PPARAM && s.canSSA(n) { 207 v := s.newValue0A(ssa.OpArg, n.Type, n) 208 s.vars[n] = v 209 s.addNamedValue(n, v) // This helps with debugging information, not needed for compilation itself. 210 } 211 } 212 213 // Convert the AST-based IR to the SSA-based IR 214 s.stmtList(fn.Func.Enter) 215 s.stmtList(fn.Nbody) 216 217 // fallthrough to exit 218 if s.curBlock != nil { 219 s.pushLine(fn.Func.Endlineno) 220 s.exit() 221 s.popLine() 222 } 223 224 for _, b := range s.f.Blocks { 225 if b.Pos != src.NoXPos { 226 s.updateUnsetPredPos(b) 227 } 228 } 229 230 s.insertPhis() 231 232 // Main call to ssa package to compile function 233 ssa.Compile(s.f) 234 return s.f 235 } 236 237 func dumpSourcesColumn(writer *ssa.HTMLWriter, fn *Node) { 238 // Read sources of target function fn. 239 fname := Ctxt.PosTable.Pos(fn.Pos).Filename() 240 targetFn, err := readFuncLines(fname, fn.Pos.Line(), fn.Func.Endlineno.Line()) 241 if err != nil { 242 writer.Logger.Logf("cannot read sources for function %v: %v", fn, err) 243 } 244 245 // Read sources of inlined functions. 246 var inlFns []*ssa.FuncLines 247 for _, fi := range ssaDumpInlined { 248 var elno src.XPos 249 if fi.Name.Defn == nil { 250 // Endlineno is filled from exported data. 251 elno = fi.Func.Endlineno 252 } else { 253 elno = fi.Name.Defn.Func.Endlineno 254 } 255 fname := Ctxt.PosTable.Pos(fi.Pos).Filename() 256 fnLines, err := readFuncLines(fname, fi.Pos.Line(), elno.Line()) 257 if err != nil { 258 writer.Logger.Logf("cannot read sources for function %v: %v", fi, err) 259 continue 260 } 261 inlFns = append(inlFns, fnLines) 262 } 263 264 sort.Sort(ssa.ByTopo(inlFns)) 265 if targetFn != nil { 266 inlFns = append([]*ssa.FuncLines{targetFn}, inlFns...) 267 } 268 269 writer.WriteSources("sources", inlFns) 270 } 271 272 func readFuncLines(file string, start, end uint) (*ssa.FuncLines, error) { 273 f, err := os.Open(os.ExpandEnv(file)) 274 if err != nil { 275 return nil, err 276 } 277 defer f.Close() 278 var lines []string 279 ln := uint(1) 280 scanner := bufio.NewScanner(f) 281 for scanner.Scan() && ln <= end { 282 if ln >= start { 283 lines = append(lines, scanner.Text()) 284 } 285 ln++ 286 } 287 return &ssa.FuncLines{Filename: file, StartLineno: start, Lines: lines}, nil 288 } 289 290 // updateUnsetPredPos propagates the earliest-value position information for b 291 // towards all of b's predecessors that need a position, and recurs on that 292 // predecessor if its position is updated. B should have a non-empty position. 293 func (s *state) updateUnsetPredPos(b *ssa.Block) { 294 if b.Pos == src.NoXPos { 295 s.Fatalf("Block %s should have a position", b) 296 } 297 bestPos := src.NoXPos 298 for _, e := range b.Preds { 299 p := e.Block() 300 if !p.LackingPos() { 301 continue 302 } 303 if bestPos == src.NoXPos { 304 bestPos = b.Pos 305 for _, v := range b.Values { 306 if v.LackingPos() { 307 continue 308 } 309 if v.Pos != src.NoXPos { 310 // Assume values are still in roughly textual order; 311 // TODO: could also seek minimum position? 312 bestPos = v.Pos 313 break 314 } 315 } 316 } 317 p.Pos = bestPos 318 s.updateUnsetPredPos(p) // We do not expect long chains of these, thus recursion is okay. 319 } 320 } 321 322 type state struct { 323 // configuration (arch) information 324 config *ssa.Config 325 326 // function we're building 327 f *ssa.Func 328 329 // Node for function 330 curfn *Node 331 332 // labels and labeled control flow nodes (OFOR, OFORUNTIL, OSWITCH, OSELECT) in f 333 labels map[string]*ssaLabel 334 labeledNodes map[*Node]*ssaLabel 335 336 // unlabeled break and continue statement tracking 337 breakTo *ssa.Block // current target for plain break statement 338 continueTo *ssa.Block // current target for plain continue statement 339 340 // current location where we're interpreting the AST 341 curBlock *ssa.Block 342 343 // variable assignments in the current block (map from variable symbol to ssa value) 344 // *Node is the unique identifier (an ONAME Node) for the variable. 345 // TODO: keep a single varnum map, then make all of these maps slices instead? 346 vars map[*Node]*ssa.Value 347 348 // fwdVars are variables that are used before they are defined in the current block. 349 // This map exists just to coalesce multiple references into a single FwdRef op. 350 // *Node is the unique identifier (an ONAME Node) for the variable. 351 fwdVars map[*Node]*ssa.Value 352 353 // all defined variables at the end of each block. Indexed by block ID. 354 defvars []map[*Node]*ssa.Value 355 356 // addresses of PPARAM and PPARAMOUT variables. 357 decladdrs map[*Node]*ssa.Value 358 359 // starting values. Memory, stack pointer, and globals pointer 360 startmem *ssa.Value 361 sp *ssa.Value 362 sb *ssa.Value 363 364 // line number stack. The current line number is top of stack 365 line []src.XPos 366 // the last line number processed; it may have been popped 367 lastPos src.XPos 368 369 // list of panic calls by function name and line number. 370 // Used to deduplicate panic calls. 371 panics map[funcLine]*ssa.Block 372 373 // list of PPARAMOUT (return) variables. 374 returns []*Node 375 376 cgoUnsafeArgs bool 377 hasdefer bool // whether the function contains a defer statement 378 softFloat bool 379 } 380 381 type funcLine struct { 382 f *obj.LSym 383 base *src.PosBase 384 line uint 385 } 386 387 type ssaLabel struct { 388 target *ssa.Block // block identified by this label 389 breakTarget *ssa.Block // block to break to in control flow node identified by this label 390 continueTarget *ssa.Block // block to continue to in control flow node identified by this label 391 } 392 393 // label returns the label associated with sym, creating it if necessary. 394 func (s *state) label(sym *types.Sym) *ssaLabel { 395 lab := s.labels[sym.Name] 396 if lab == nil { 397 lab = new(ssaLabel) 398 s.labels[sym.Name] = lab 399 } 400 return lab 401 } 402 403 func (s *state) Logf(msg string, args ...interface{}) { s.f.Logf(msg, args...) } 404 func (s *state) Log() bool { return s.f.Log() } 405 func (s *state) Fatalf(msg string, args ...interface{}) { 406 s.f.Frontend().Fatalf(s.peekPos(), msg, args...) 407 } 408 func (s *state) Warnl(pos src.XPos, msg string, args ...interface{}) { s.f.Warnl(pos, msg, args...) } 409 func (s *state) Debug_checknil() bool { return s.f.Frontend().Debug_checknil() } 410 411 var ( 412 // dummy node for the memory variable 413 memVar = Node{Op: ONAME, Sym: &types.Sym{Name: "mem"}} 414 415 // dummy nodes for temporary variables 416 ptrVar = Node{Op: ONAME, Sym: &types.Sym{Name: "ptr"}} 417 lenVar = Node{Op: ONAME, Sym: &types.Sym{Name: "len"}} 418 newlenVar = Node{Op: ONAME, Sym: &types.Sym{Name: "newlen"}} 419 capVar = Node{Op: ONAME, Sym: &types.Sym{Name: "cap"}} 420 typVar = Node{Op: ONAME, Sym: &types.Sym{Name: "typ"}} 421 okVar = Node{Op: ONAME, Sym: &types.Sym{Name: "ok"}} 422 ) 423 424 // startBlock sets the current block we're generating code in to b. 425 func (s *state) startBlock(b *ssa.Block) { 426 if s.curBlock != nil { 427 s.Fatalf("starting block %v when block %v has not ended", b, s.curBlock) 428 } 429 s.curBlock = b 430 s.vars = map[*Node]*ssa.Value{} 431 for n := range s.fwdVars { 432 delete(s.fwdVars, n) 433 } 434 } 435 436 // endBlock marks the end of generating code for the current block. 437 // Returns the (former) current block. Returns nil if there is no current 438 // block, i.e. if no code flows to the current execution point. 439 func (s *state) endBlock() *ssa.Block { 440 b := s.curBlock 441 if b == nil { 442 return nil 443 } 444 for len(s.defvars) <= int(b.ID) { 445 s.defvars = append(s.defvars, nil) 446 } 447 s.defvars[b.ID] = s.vars 448 s.curBlock = nil 449 s.vars = nil 450 if b.LackingPos() { 451 // Empty plain blocks get the line of their successor (handled after all blocks created), 452 // except for increment blocks in For statements (handled in ssa conversion of OFOR), 453 // and for blocks ending in GOTO/BREAK/CONTINUE. 454 b.Pos = src.NoXPos 455 } else { 456 b.Pos = s.lastPos 457 } 458 return b 459 } 460 461 // pushLine pushes a line number on the line number stack. 462 func (s *state) pushLine(line src.XPos) { 463 if !line.IsKnown() { 464 // the frontend may emit node with line number missing, 465 // use the parent line number in this case. 466 line = s.peekPos() 467 if Debug['K'] != 0 { 468 Warn("buildssa: unknown position (line 0)") 469 } 470 } else { 471 s.lastPos = line 472 } 473 474 s.line = append(s.line, line) 475 } 476 477 // popLine pops the top of the line number stack. 478 func (s *state) popLine() { 479 s.line = s.line[:len(s.line)-1] 480 } 481 482 // peekPos peeks the top of the line number stack. 483 func (s *state) peekPos() src.XPos { 484 return s.line[len(s.line)-1] 485 } 486 487 // newValue0 adds a new value with no arguments to the current block. 488 func (s *state) newValue0(op ssa.Op, t *types.Type) *ssa.Value { 489 return s.curBlock.NewValue0(s.peekPos(), op, t) 490 } 491 492 // newValue0A adds a new value with no arguments and an aux value to the current block. 493 func (s *state) newValue0A(op ssa.Op, t *types.Type, aux interface{}) *ssa.Value { 494 return s.curBlock.NewValue0A(s.peekPos(), op, t, aux) 495 } 496 497 // newValue0I adds a new value with no arguments and an auxint value to the current block. 498 func (s *state) newValue0I(op ssa.Op, t *types.Type, auxint int64) *ssa.Value { 499 return s.curBlock.NewValue0I(s.peekPos(), op, t, auxint) 500 } 501 502 // newValue1 adds a new value with one argument to the current block. 503 func (s *state) newValue1(op ssa.Op, t *types.Type, arg *ssa.Value) *ssa.Value { 504 return s.curBlock.NewValue1(s.peekPos(), op, t, arg) 505 } 506 507 // newValue1A adds a new value with one argument and an aux value to the current block. 508 func (s *state) newValue1A(op ssa.Op, t *types.Type, aux interface{}, arg *ssa.Value) *ssa.Value { 509 return s.curBlock.NewValue1A(s.peekPos(), op, t, aux, arg) 510 } 511 512 // newValue1Apos adds a new value with one argument and an aux value to the current block. 513 // isStmt determines whether the created values may be a statement or not 514 // (i.e., false means never, yes means maybe). 515 func (s *state) newValue1Apos(op ssa.Op, t *types.Type, aux interface{}, arg *ssa.Value, isStmt bool) *ssa.Value { 516 if isStmt { 517 return s.curBlock.NewValue1A(s.peekPos(), op, t, aux, arg) 518 } 519 return s.curBlock.NewValue1A(s.peekPos().WithNotStmt(), op, t, aux, arg) 520 } 521 522 // newValue1I adds a new value with one argument and an auxint value to the current block. 523 func (s *state) newValue1I(op ssa.Op, t *types.Type, aux int64, arg *ssa.Value) *ssa.Value { 524 return s.curBlock.NewValue1I(s.peekPos(), op, t, aux, arg) 525 } 526 527 // newValue2 adds a new value with two arguments to the current block. 528 func (s *state) newValue2(op ssa.Op, t *types.Type, arg0, arg1 *ssa.Value) *ssa.Value { 529 return s.curBlock.NewValue2(s.peekPos(), op, t, arg0, arg1) 530 } 531 532 // newValue2Apos adds a new value with two arguments and an aux value to the current block. 533 // isStmt determines whether the created values may be a statement or not 534 // (i.e., false means never, yes means maybe). 535 func (s *state) newValue2Apos(op ssa.Op, t *types.Type, aux interface{}, arg0, arg1 *ssa.Value, isStmt bool) *ssa.Value { 536 if isStmt { 537 return s.curBlock.NewValue2A(s.peekPos(), op, t, aux, arg0, arg1) 538 } 539 return s.curBlock.NewValue2A(s.peekPos().WithNotStmt(), op, t, aux, arg0, arg1) 540 } 541 542 // newValue2I adds a new value with two arguments and an auxint value to the current block. 543 func (s *state) newValue2I(op ssa.Op, t *types.Type, aux int64, arg0, arg1 *ssa.Value) *ssa.Value { 544 return s.curBlock.NewValue2I(s.peekPos(), op, t, aux, arg0, arg1) 545 } 546 547 // newValue3 adds a new value with three arguments to the current block. 548 func (s *state) newValue3(op ssa.Op, t *types.Type, arg0, arg1, arg2 *ssa.Value) *ssa.Value { 549 return s.curBlock.NewValue3(s.peekPos(), op, t, arg0, arg1, arg2) 550 } 551 552 // newValue3I adds a new value with three arguments and an auxint value to the current block. 553 func (s *state) newValue3I(op ssa.Op, t *types.Type, aux int64, arg0, arg1, arg2 *ssa.Value) *ssa.Value { 554 return s.curBlock.NewValue3I(s.peekPos(), op, t, aux, arg0, arg1, arg2) 555 } 556 557 // newValue3A adds a new value with three arguments and an aux value to the current block. 558 func (s *state) newValue3A(op ssa.Op, t *types.Type, aux interface{}, arg0, arg1, arg2 *ssa.Value) *ssa.Value { 559 return s.curBlock.NewValue3A(s.peekPos(), op, t, aux, arg0, arg1, arg2) 560 } 561 562 // newValue3Apos adds a new value with three arguments and an aux value to the current block. 563 // isStmt determines whether the created values may be a statement or not 564 // (i.e., false means never, yes means maybe). 565 func (s *state) newValue3Apos(op ssa.Op, t *types.Type, aux interface{}, arg0, arg1, arg2 *ssa.Value, isStmt bool) *ssa.Value { 566 if isStmt { 567 return s.curBlock.NewValue3A(s.peekPos(), op, t, aux, arg0, arg1, arg2) 568 } 569 return s.curBlock.NewValue3A(s.peekPos().WithNotStmt(), op, t, aux, arg0, arg1, arg2) 570 } 571 572 // newValue4 adds a new value with four arguments to the current block. 573 func (s *state) newValue4(op ssa.Op, t *types.Type, arg0, arg1, arg2, arg3 *ssa.Value) *ssa.Value { 574 return s.curBlock.NewValue4(s.peekPos(), op, t, arg0, arg1, arg2, arg3) 575 } 576 577 // entryNewValue0 adds a new value with no arguments to the entry block. 578 func (s *state) entryNewValue0(op ssa.Op, t *types.Type) *ssa.Value { 579 return s.f.Entry.NewValue0(src.NoXPos, op, t) 580 } 581 582 // entryNewValue0A adds a new value with no arguments and an aux value to the entry block. 583 func (s *state) entryNewValue0A(op ssa.Op, t *types.Type, aux interface{}) *ssa.Value { 584 return s.f.Entry.NewValue0A(src.NoXPos, op, t, aux) 585 } 586 587 // entryNewValue1 adds a new value with one argument to the entry block. 588 func (s *state) entryNewValue1(op ssa.Op, t *types.Type, arg *ssa.Value) *ssa.Value { 589 return s.f.Entry.NewValue1(src.NoXPos, op, t, arg) 590 } 591 592 // entryNewValue1 adds a new value with one argument and an auxint value to the entry block. 593 func (s *state) entryNewValue1I(op ssa.Op, t *types.Type, auxint int64, arg *ssa.Value) *ssa.Value { 594 return s.f.Entry.NewValue1I(src.NoXPos, op, t, auxint, arg) 595 } 596 597 // entryNewValue1A adds a new value with one argument and an aux value to the entry block. 598 func (s *state) entryNewValue1A(op ssa.Op, t *types.Type, aux interface{}, arg *ssa.Value) *ssa.Value { 599 return s.f.Entry.NewValue1A(src.NoXPos, op, t, aux, arg) 600 } 601 602 // entryNewValue2 adds a new value with two arguments to the entry block. 603 func (s *state) entryNewValue2(op ssa.Op, t *types.Type, arg0, arg1 *ssa.Value) *ssa.Value { 604 return s.f.Entry.NewValue2(src.NoXPos, op, t, arg0, arg1) 605 } 606 607 // entryNewValue2A adds a new value with two arguments and an aux value to the entry block. 608 func (s *state) entryNewValue2A(op ssa.Op, t *types.Type, aux interface{}, arg0, arg1 *ssa.Value) *ssa.Value { 609 return s.f.Entry.NewValue2A(src.NoXPos, op, t, aux, arg0, arg1) 610 } 611 612 // const* routines add a new const value to the entry block. 613 func (s *state) constSlice(t *types.Type) *ssa.Value { 614 return s.f.ConstSlice(t) 615 } 616 func (s *state) constInterface(t *types.Type) *ssa.Value { 617 return s.f.ConstInterface(t) 618 } 619 func (s *state) constNil(t *types.Type) *ssa.Value { return s.f.ConstNil(t) } 620 func (s *state) constEmptyString(t *types.Type) *ssa.Value { 621 return s.f.ConstEmptyString(t) 622 } 623 func (s *state) constBool(c bool) *ssa.Value { 624 return s.f.ConstBool(types.Types[TBOOL], c) 625 } 626 func (s *state) constInt8(t *types.Type, c int8) *ssa.Value { 627 return s.f.ConstInt8(t, c) 628 } 629 func (s *state) constInt16(t *types.Type, c int16) *ssa.Value { 630 return s.f.ConstInt16(t, c) 631 } 632 func (s *state) constInt32(t *types.Type, c int32) *ssa.Value { 633 return s.f.ConstInt32(t, c) 634 } 635 func (s *state) constInt64(t *types.Type, c int64) *ssa.Value { 636 return s.f.ConstInt64(t, c) 637 } 638 func (s *state) constFloat32(t *types.Type, c float64) *ssa.Value { 639 return s.f.ConstFloat32(t, c) 640 } 641 func (s *state) constFloat64(t *types.Type, c float64) *ssa.Value { 642 return s.f.ConstFloat64(t, c) 643 } 644 func (s *state) constInt(t *types.Type, c int64) *ssa.Value { 645 if s.config.PtrSize == 8 { 646 return s.constInt64(t, c) 647 } 648 if int64(int32(c)) != c { 649 s.Fatalf("integer constant too big %d", c) 650 } 651 return s.constInt32(t, int32(c)) 652 } 653 func (s *state) constOffPtrSP(t *types.Type, c int64) *ssa.Value { 654 return s.f.ConstOffPtrSP(t, c, s.sp) 655 } 656 657 // newValueOrSfCall* are wrappers around newValue*, which may create a call to a 658 // soft-float runtime function instead (when emitting soft-float code). 659 func (s *state) newValueOrSfCall1(op ssa.Op, t *types.Type, arg *ssa.Value) *ssa.Value { 660 if s.softFloat { 661 if c, ok := s.sfcall(op, arg); ok { 662 return c 663 } 664 } 665 return s.newValue1(op, t, arg) 666 } 667 func (s *state) newValueOrSfCall2(op ssa.Op, t *types.Type, arg0, arg1 *ssa.Value) *ssa.Value { 668 if s.softFloat { 669 if c, ok := s.sfcall(op, arg0, arg1); ok { 670 return c 671 } 672 } 673 return s.newValue2(op, t, arg0, arg1) 674 } 675 676 func (s *state) instrument(t *types.Type, addr *ssa.Value, wr bool) { 677 if !s.curfn.Func.InstrumentBody() { 678 return 679 } 680 681 w := t.Size() 682 if w == 0 { 683 return // can't race on zero-sized things 684 } 685 686 if ssa.IsSanitizerSafeAddr(addr) { 687 return 688 } 689 690 var fn *obj.LSym 691 needWidth := false 692 693 if flag_msan { 694 fn = msanread 695 if wr { 696 fn = msanwrite 697 } 698 needWidth = true 699 } else if flag_race && t.NumComponents(types.CountBlankFields) > 1 { 700 // for composite objects we have to write every address 701 // because a write might happen to any subobject. 702 // composites with only one element don't have subobjects, though. 703 fn = racereadrange 704 if wr { 705 fn = racewriterange 706 } 707 needWidth = true 708 } else if flag_race { 709 // for non-composite objects we can write just the start 710 // address, as any write must write the first byte. 711 fn = raceread 712 if wr { 713 fn = racewrite 714 } 715 } else { 716 panic("unreachable") 717 } 718 719 args := []*ssa.Value{addr} 720 if needWidth { 721 args = append(args, s.constInt(types.Types[TUINTPTR], w)) 722 } 723 s.rtcall(fn, true, nil, args...) 724 } 725 726 func (s *state) load(t *types.Type, src *ssa.Value) *ssa.Value { 727 s.instrument(t, src, false) 728 return s.rawLoad(t, src) 729 } 730 731 func (s *state) rawLoad(t *types.Type, src *ssa.Value) *ssa.Value { 732 return s.newValue2(ssa.OpLoad, t, src, s.mem()) 733 } 734 735 func (s *state) store(t *types.Type, dst, val *ssa.Value) { 736 s.vars[&memVar] = s.newValue3A(ssa.OpStore, types.TypeMem, t, dst, val, s.mem()) 737 } 738 739 func (s *state) zero(t *types.Type, dst *ssa.Value) { 740 s.instrument(t, dst, true) 741 store := s.newValue2I(ssa.OpZero, types.TypeMem, t.Size(), dst, s.mem()) 742 store.Aux = t 743 s.vars[&memVar] = store 744 } 745 746 func (s *state) move(t *types.Type, dst, src *ssa.Value) { 747 s.instrument(t, src, false) 748 s.instrument(t, dst, true) 749 store := s.newValue3I(ssa.OpMove, types.TypeMem, t.Size(), dst, src, s.mem()) 750 store.Aux = t 751 s.vars[&memVar] = store 752 } 753 754 // stmtList converts the statement list n to SSA and adds it to s. 755 func (s *state) stmtList(l Nodes) { 756 for _, n := range l.Slice() { 757 s.stmt(n) 758 } 759 } 760 761 // stmt converts the statement n to SSA and adds it to s. 762 func (s *state) stmt(n *Node) { 763 if !(n.Op == OVARKILL || n.Op == OVARLIVE || n.Op == OVARDEF) { 764 // OVARKILL, OVARLIVE, and OVARDEF are invisible to the programmer, so we don't use their line numbers to avoid confusion in debugging. 765 s.pushLine(n.Pos) 766 defer s.popLine() 767 } 768 769 // If s.curBlock is nil, and n isn't a label (which might have an associated goto somewhere), 770 // then this code is dead. Stop here. 771 if s.curBlock == nil && n.Op != OLABEL { 772 return 773 } 774 775 s.stmtList(n.Ninit) 776 switch n.Op { 777 778 case OBLOCK: 779 s.stmtList(n.List) 780 781 // No-ops 782 case OEMPTY, ODCLCONST, ODCLTYPE, OFALL: 783 784 // Expression statements 785 case OCALLFUNC: 786 if isIntrinsicCall(n) { 787 s.intrinsicCall(n) 788 return 789 } 790 fallthrough 791 792 case OCALLMETH, OCALLINTER: 793 s.call(n, callNormal) 794 if n.Op == OCALLFUNC && n.Left.Op == ONAME && n.Left.Class() == PFUNC { 795 if fn := n.Left.Sym.Name; compiling_runtime && fn == "throw" || 796 n.Left.Sym.Pkg == Runtimepkg && (fn == "throwinit" || fn == "gopanic" || fn == "panicwrap" || fn == "block" || fn == "panicmakeslicelen" || fn == "panicmakeslicecap") { 797 m := s.mem() 798 b := s.endBlock() 799 b.Kind = ssa.BlockExit 800 b.SetControl(m) 801 // TODO: never rewrite OPANIC to OCALLFUNC in the 802 // first place. Need to wait until all backends 803 // go through SSA. 804 } 805 } 806 case ODEFER: 807 s.call(n.Left, callDefer) 808 case OGO: 809 s.call(n.Left, callGo) 810 811 case OAS2DOTTYPE: 812 res, resok := s.dottype(n.Rlist.First(), true) 813 deref := false 814 if !canSSAType(n.Rlist.First().Type) { 815 if res.Op != ssa.OpLoad { 816 s.Fatalf("dottype of non-load") 817 } 818 mem := s.mem() 819 if mem.Op == ssa.OpVarKill { 820 mem = mem.Args[0] 821 } 822 if res.Args[1] != mem { 823 s.Fatalf("memory no longer live from 2-result dottype load") 824 } 825 deref = true 826 res = res.Args[0] 827 } 828 s.assign(n.List.First(), res, deref, 0) 829 s.assign(n.List.Second(), resok, false, 0) 830 return 831 832 case OAS2FUNC: 833 // We come here only when it is an intrinsic call returning two values. 834 if !isIntrinsicCall(n.Rlist.First()) { 835 s.Fatalf("non-intrinsic AS2FUNC not expanded %v", n.Rlist.First()) 836 } 837 v := s.intrinsicCall(n.Rlist.First()) 838 v1 := s.newValue1(ssa.OpSelect0, n.List.First().Type, v) 839 v2 := s.newValue1(ssa.OpSelect1, n.List.Second().Type, v) 840 s.assign(n.List.First(), v1, false, 0) 841 s.assign(n.List.Second(), v2, false, 0) 842 return 843 844 case ODCL: 845 if n.Left.Class() == PAUTOHEAP { 846 Fatalf("DCL %v", n) 847 } 848 849 case OLABEL: 850 sym := n.Sym 851 lab := s.label(sym) 852 853 // Associate label with its control flow node, if any 854 if ctl := n.labeledControl(); ctl != nil { 855 s.labeledNodes[ctl] = lab 856 } 857 858 // The label might already have a target block via a goto. 859 if lab.target == nil { 860 lab.target = s.f.NewBlock(ssa.BlockPlain) 861 } 862 863 // Go to that label. 864 // (We pretend "label:" is preceded by "goto label", unless the predecessor is unreachable.) 865 if s.curBlock != nil { 866 b := s.endBlock() 867 b.AddEdgeTo(lab.target) 868 } 869 s.startBlock(lab.target) 870 871 case OGOTO: 872 sym := n.Sym 873 874 lab := s.label(sym) 875 if lab.target == nil { 876 lab.target = s.f.NewBlock(ssa.BlockPlain) 877 } 878 879 b := s.endBlock() 880 b.Pos = s.lastPos.WithIsStmt() // Do this even if b is an empty block. 881 b.AddEdgeTo(lab.target) 882 883 case OAS: 884 if n.Left == n.Right && n.Left.Op == ONAME { 885 // An x=x assignment. No point in doing anything 886 // here. In addition, skipping this assignment 887 // prevents generating: 888 // VARDEF x 889 // COPY x -> x 890 // which is bad because x is incorrectly considered 891 // dead before the vardef. See issue #14904. 892 return 893 } 894 895 // Evaluate RHS. 896 rhs := n.Right 897 if rhs != nil { 898 switch rhs.Op { 899 case OSTRUCTLIT, OARRAYLIT, OSLICELIT: 900 // All literals with nonzero fields have already been 901 // rewritten during walk. Any that remain are just T{} 902 // or equivalents. Use the zero value. 903 if !isZero(rhs) { 904 Fatalf("literal with nonzero value in SSA: %v", rhs) 905 } 906 rhs = nil 907 case OAPPEND: 908 // Check whether we're writing the result of an append back to the same slice. 909 // If so, we handle it specially to avoid write barriers on the fast 910 // (non-growth) path. 911 if !samesafeexpr(n.Left, rhs.List.First()) || Debug['N'] != 0 { 912 break 913 } 914 // If the slice can be SSA'd, it'll be on the stack, 915 // so there will be no write barriers, 916 // so there's no need to attempt to prevent them. 917 if s.canSSA(n.Left) { 918 if Debug_append > 0 { // replicating old diagnostic message 919 Warnl(n.Pos, "append: len-only update (in local slice)") 920 } 921 break 922 } 923 if Debug_append > 0 { 924 Warnl(n.Pos, "append: len-only update") 925 } 926 s.append(rhs, true) 927 return 928 } 929 } 930 931 if n.Left.isBlank() { 932 // _ = rhs 933 // Just evaluate rhs for side-effects. 934 if rhs != nil { 935 s.expr(rhs) 936 } 937 return 938 } 939 940 var t *types.Type 941 if n.Right != nil { 942 t = n.Right.Type 943 } else { 944 t = n.Left.Type 945 } 946 947 var r *ssa.Value 948 deref := !canSSAType(t) 949 if deref { 950 if rhs == nil { 951 r = nil // Signal assign to use OpZero. 952 } else { 953 r = s.addr(rhs, false) 954 } 955 } else { 956 if rhs == nil { 957 r = s.zeroVal(t) 958 } else { 959 r = s.expr(rhs) 960 } 961 } 962 963 var skip skipMask 964 if rhs != nil && (rhs.Op == OSLICE || rhs.Op == OSLICE3 || rhs.Op == OSLICESTR) && samesafeexpr(rhs.Left, n.Left) { 965 // We're assigning a slicing operation back to its source. 966 // Don't write back fields we aren't changing. See issue #14855. 967 i, j, k := rhs.SliceBounds() 968 if i != nil && (i.Op == OLITERAL && i.Val().Ctype() == CTINT && i.Int64() == 0) { 969 // [0:...] is the same as [:...] 970 i = nil 971 } 972 // TODO: detect defaults for len/cap also. 973 // Currently doesn't really work because (*p)[:len(*p)] appears here as: 974 // tmp = len(*p) 975 // (*p)[:tmp] 976 //if j != nil && (j.Op == OLEN && samesafeexpr(j.Left, n.Left)) { 977 // j = nil 978 //} 979 //if k != nil && (k.Op == OCAP && samesafeexpr(k.Left, n.Left)) { 980 // k = nil 981 //} 982 if i == nil { 983 skip |= skipPtr 984 if j == nil { 985 skip |= skipLen 986 } 987 if k == nil { 988 skip |= skipCap 989 } 990 } 991 } 992 993 s.assign(n.Left, r, deref, skip) 994 995 case OIF: 996 bThen := s.f.NewBlock(ssa.BlockPlain) 997 bEnd := s.f.NewBlock(ssa.BlockPlain) 998 var bElse *ssa.Block 999 var likely int8 1000 if n.Likely() { 1001 likely = 1 1002 } 1003 if n.Rlist.Len() != 0 { 1004 bElse = s.f.NewBlock(ssa.BlockPlain) 1005 s.condBranch(n.Left, bThen, bElse, likely) 1006 } else { 1007 s.condBranch(n.Left, bThen, bEnd, likely) 1008 } 1009 1010 s.startBlock(bThen) 1011 s.stmtList(n.Nbody) 1012 if b := s.endBlock(); b != nil { 1013 b.AddEdgeTo(bEnd) 1014 } 1015 1016 if n.Rlist.Len() != 0 { 1017 s.startBlock(bElse) 1018 s.stmtList(n.Rlist) 1019 if b := s.endBlock(); b != nil { 1020 b.AddEdgeTo(bEnd) 1021 } 1022 } 1023 s.startBlock(bEnd) 1024 1025 case ORETURN: 1026 s.stmtList(n.List) 1027 b := s.exit() 1028 b.Pos = s.lastPos.WithIsStmt() 1029 1030 case ORETJMP: 1031 s.stmtList(n.List) 1032 b := s.exit() 1033 b.Kind = ssa.BlockRetJmp // override BlockRet 1034 b.Aux = n.Sym.Linksym() 1035 1036 case OCONTINUE, OBREAK: 1037 var to *ssa.Block 1038 if n.Sym == nil { 1039 // plain break/continue 1040 switch n.Op { 1041 case OCONTINUE: 1042 to = s.continueTo 1043 case OBREAK: 1044 to = s.breakTo 1045 } 1046 } else { 1047 // labeled break/continue; look up the target 1048 sym := n.Sym 1049 lab := s.label(sym) 1050 switch n.Op { 1051 case OCONTINUE: 1052 to = lab.continueTarget 1053 case OBREAK: 1054 to = lab.breakTarget 1055 } 1056 } 1057 1058 b := s.endBlock() 1059 b.Pos = s.lastPos.WithIsStmt() // Do this even if b is an empty block. 1060 b.AddEdgeTo(to) 1061 1062 case OFOR, OFORUNTIL: 1063 // OFOR: for Ninit; Left; Right { Nbody } 1064 // cond (Left); body (Nbody); incr (Right) 1065 // 1066 // OFORUNTIL: for Ninit; Left; Right; List { Nbody } 1067 // => body: { Nbody }; incr: Right; if Left { lateincr: List; goto body }; end: 1068 bCond := s.f.NewBlock(ssa.BlockPlain) 1069 bBody := s.f.NewBlock(ssa.BlockPlain) 1070 bIncr := s.f.NewBlock(ssa.BlockPlain) 1071 bEnd := s.f.NewBlock(ssa.BlockPlain) 1072 1073 // first, jump to condition test (OFOR) or body (OFORUNTIL) 1074 b := s.endBlock() 1075 if n.Op == OFOR { 1076 b.AddEdgeTo(bCond) 1077 // generate code to test condition 1078 s.startBlock(bCond) 1079 if n.Left != nil { 1080 s.condBranch(n.Left, bBody, bEnd, 1) 1081 } else { 1082 b := s.endBlock() 1083 b.Kind = ssa.BlockPlain 1084 b.AddEdgeTo(bBody) 1085 } 1086 1087 } else { 1088 b.AddEdgeTo(bBody) 1089 } 1090 1091 // set up for continue/break in body 1092 prevContinue := s.continueTo 1093 prevBreak := s.breakTo 1094 s.continueTo = bIncr 1095 s.breakTo = bEnd 1096 lab := s.labeledNodes[n] 1097 if lab != nil { 1098 // labeled for loop 1099 lab.continueTarget = bIncr 1100 lab.breakTarget = bEnd 1101 } 1102 1103 // generate body 1104 s.startBlock(bBody) 1105 s.stmtList(n.Nbody) 1106 1107 // tear down continue/break 1108 s.continueTo = prevContinue 1109 s.breakTo = prevBreak 1110 if lab != nil { 1111 lab.continueTarget = nil 1112 lab.breakTarget = nil 1113 } 1114 1115 // done with body, goto incr 1116 if b := s.endBlock(); b != nil { 1117 b.AddEdgeTo(bIncr) 1118 } 1119 1120 // generate incr (and, for OFORUNTIL, condition) 1121 s.startBlock(bIncr) 1122 if n.Right != nil { 1123 s.stmt(n.Right) 1124 } 1125 if n.Op == OFOR { 1126 if b := s.endBlock(); b != nil { 1127 b.AddEdgeTo(bCond) 1128 // It can happen that bIncr ends in a block containing only VARKILL, 1129 // and that muddles the debugging experience. 1130 if n.Op != OFORUNTIL && b.Pos == src.NoXPos { 1131 b.Pos = bCond.Pos 1132 } 1133 } 1134 } else { 1135 // bCond is unused in OFORUNTIL, so repurpose it. 1136 bLateIncr := bCond 1137 // test condition 1138 s.condBranch(n.Left, bLateIncr, bEnd, 1) 1139 // generate late increment 1140 s.startBlock(bLateIncr) 1141 s.stmtList(n.List) 1142 s.endBlock().AddEdgeTo(bBody) 1143 } 1144 1145 s.startBlock(bEnd) 1146 1147 case OSWITCH, OSELECT: 1148 // These have been mostly rewritten by the front end into their Nbody fields. 1149 // Our main task is to correctly hook up any break statements. 1150 bEnd := s.f.NewBlock(ssa.BlockPlain) 1151 1152 prevBreak := s.breakTo 1153 s.breakTo = bEnd 1154 lab := s.labeledNodes[n] 1155 if lab != nil { 1156 // labeled 1157 lab.breakTarget = bEnd 1158 } 1159 1160 // generate body code 1161 s.stmtList(n.Nbody) 1162 1163 s.breakTo = prevBreak 1164 if lab != nil { 1165 lab.breakTarget = nil 1166 } 1167 1168 // walk adds explicit OBREAK nodes to the end of all reachable code paths. 1169 // If we still have a current block here, then mark it unreachable. 1170 if s.curBlock != nil { 1171 m := s.mem() 1172 b := s.endBlock() 1173 b.Kind = ssa.BlockExit 1174 b.SetControl(m) 1175 } 1176 s.startBlock(bEnd) 1177 1178 case OVARDEF: 1179 if !s.canSSA(n.Left) { 1180 s.vars[&memVar] = s.newValue1Apos(ssa.OpVarDef, types.TypeMem, n.Left, s.mem(), false) 1181 } 1182 case OVARKILL: 1183 // Insert a varkill op to record that a variable is no longer live. 1184 // We only care about liveness info at call sites, so putting the 1185 // varkill in the store chain is enough to keep it correctly ordered 1186 // with respect to call ops. 1187 if !s.canSSA(n.Left) { 1188 s.vars[&memVar] = s.newValue1Apos(ssa.OpVarKill, types.TypeMem, n.Left, s.mem(), false) 1189 } 1190 1191 case OVARLIVE: 1192 // Insert a varlive op to record that a variable is still live. 1193 if !n.Left.Addrtaken() { 1194 s.Fatalf("VARLIVE variable %v must have Addrtaken set", n.Left) 1195 } 1196 switch n.Left.Class() { 1197 case PAUTO, PPARAM, PPARAMOUT: 1198 default: 1199 s.Fatalf("VARLIVE variable %v must be Auto or Arg", n.Left) 1200 } 1201 s.vars[&memVar] = s.newValue1A(ssa.OpVarLive, types.TypeMem, n.Left, s.mem()) 1202 1203 case OCHECKNIL: 1204 p := s.expr(n.Left) 1205 s.nilCheck(p) 1206 1207 case OINLMARK: 1208 s.newValue1I(ssa.OpInlMark, types.TypeVoid, n.Xoffset, s.mem()) 1209 1210 default: 1211 s.Fatalf("unhandled stmt %v", n.Op) 1212 } 1213 } 1214 1215 // exit processes any code that needs to be generated just before returning. 1216 // It returns a BlockRet block that ends the control flow. Its control value 1217 // will be set to the final memory state. 1218 func (s *state) exit() *ssa.Block { 1219 if s.hasdefer { 1220 s.rtcall(Deferreturn, true, nil) 1221 } 1222 1223 // Run exit code. Typically, this code copies heap-allocated PPARAMOUT 1224 // variables back to the stack. 1225 s.stmtList(s.curfn.Func.Exit) 1226 1227 // Store SSAable PPARAMOUT variables back to stack locations. 1228 for _, n := range s.returns { 1229 addr := s.decladdrs[n] 1230 val := s.variable(n, n.Type) 1231 s.vars[&memVar] = s.newValue1A(ssa.OpVarDef, types.TypeMem, n, s.mem()) 1232 s.store(n.Type, addr, val) 1233 // TODO: if val is ever spilled, we'd like to use the 1234 // PPARAMOUT slot for spilling it. That won't happen 1235 // currently. 1236 } 1237 1238 // Do actual return. 1239 m := s.mem() 1240 b := s.endBlock() 1241 b.Kind = ssa.BlockRet 1242 b.SetControl(m) 1243 return b 1244 } 1245 1246 type opAndType struct { 1247 op Op 1248 etype types.EType 1249 } 1250 1251 var opToSSA = map[opAndType]ssa.Op{ 1252 opAndType{OADD, TINT8}: ssa.OpAdd8, 1253 opAndType{OADD, TUINT8}: ssa.OpAdd8, 1254 opAndType{OADD, TINT16}: ssa.OpAdd16, 1255 opAndType{OADD, TUINT16}: ssa.OpAdd16, 1256 opAndType{OADD, TINT32}: ssa.OpAdd32, 1257 opAndType{OADD, TUINT32}: ssa.OpAdd32, 1258 opAndType{OADD, TINT64}: ssa.OpAdd64, 1259 opAndType{OADD, TUINT64}: ssa.OpAdd64, 1260 opAndType{OADD, TFLOAT32}: ssa.OpAdd32F, 1261 opAndType{OADD, TFLOAT64}: ssa.OpAdd64F, 1262 1263 opAndType{OSUB, TINT8}: ssa.OpSub8, 1264 opAndType{OSUB, TUINT8}: ssa.OpSub8, 1265 opAndType{OSUB, TINT16}: ssa.OpSub16, 1266 opAndType{OSUB, TUINT16}: ssa.OpSub16, 1267 opAndType{OSUB, TINT32}: ssa.OpSub32, 1268 opAndType{OSUB, TUINT32}: ssa.OpSub32, 1269 opAndType{OSUB, TINT64}: ssa.OpSub64, 1270 opAndType{OSUB, TUINT64}: ssa.OpSub64, 1271 opAndType{OSUB, TFLOAT32}: ssa.OpSub32F, 1272 opAndType{OSUB, TFLOAT64}: ssa.OpSub64F, 1273 1274 opAndType{ONOT, TBOOL}: ssa.OpNot, 1275 1276 opAndType{ONEG, TINT8}: ssa.OpNeg8, 1277 opAndType{ONEG, TUINT8}: ssa.OpNeg8, 1278 opAndType{ONEG, TINT16}: ssa.OpNeg16, 1279 opAndType{ONEG, TUINT16}: ssa.OpNeg16, 1280 opAndType{ONEG, TINT32}: ssa.OpNeg32, 1281 opAndType{ONEG, TUINT32}: ssa.OpNeg32, 1282 opAndType{ONEG, TINT64}: ssa.OpNeg64, 1283 opAndType{ONEG, TUINT64}: ssa.OpNeg64, 1284 opAndType{ONEG, TFLOAT32}: ssa.OpNeg32F, 1285 opAndType{ONEG, TFLOAT64}: ssa.OpNeg64F, 1286 1287 opAndType{OBITNOT, TINT8}: ssa.OpCom8, 1288 opAndType{OBITNOT, TUINT8}: ssa.OpCom8, 1289 opAndType{OBITNOT, TINT16}: ssa.OpCom16, 1290 opAndType{OBITNOT, TUINT16}: ssa.OpCom16, 1291 opAndType{OBITNOT, TINT32}: ssa.OpCom32, 1292 opAndType{OBITNOT, TUINT32}: ssa.OpCom32, 1293 opAndType{OBITNOT, TINT64}: ssa.OpCom64, 1294 opAndType{OBITNOT, TUINT64}: ssa.OpCom64, 1295 1296 opAndType{OIMAG, TCOMPLEX64}: ssa.OpComplexImag, 1297 opAndType{OIMAG, TCOMPLEX128}: ssa.OpComplexImag, 1298 opAndType{OREAL, TCOMPLEX64}: ssa.OpComplexReal, 1299 opAndType{OREAL, TCOMPLEX128}: ssa.OpComplexReal, 1300 1301 opAndType{OMUL, TINT8}: ssa.OpMul8, 1302 opAndType{OMUL, TUINT8}: ssa.OpMul8, 1303 opAndType{OMUL, TINT16}: ssa.OpMul16, 1304 opAndType{OMUL, TUINT16}: ssa.OpMul16, 1305 opAndType{OMUL, TINT32}: ssa.OpMul32, 1306 opAndType{OMUL, TUINT32}: ssa.OpMul32, 1307 opAndType{OMUL, TINT64}: ssa.OpMul64, 1308 opAndType{OMUL, TUINT64}: ssa.OpMul64, 1309 opAndType{OMUL, TFLOAT32}: ssa.OpMul32F, 1310 opAndType{OMUL, TFLOAT64}: ssa.OpMul64F, 1311 1312 opAndType{ODIV, TFLOAT32}: ssa.OpDiv32F, 1313 opAndType{ODIV, TFLOAT64}: ssa.OpDiv64F, 1314 1315 opAndType{ODIV, TINT8}: ssa.OpDiv8, 1316 opAndType{ODIV, TUINT8}: ssa.OpDiv8u, 1317 opAndType{ODIV, TINT16}: ssa.OpDiv16, 1318 opAndType{ODIV, TUINT16}: ssa.OpDiv16u, 1319 opAndType{ODIV, TINT32}: ssa.OpDiv32, 1320 opAndType{ODIV, TUINT32}: ssa.OpDiv32u, 1321 opAndType{ODIV, TINT64}: ssa.OpDiv64, 1322 opAndType{ODIV, TUINT64}: ssa.OpDiv64u, 1323 1324 opAndType{OMOD, TINT8}: ssa.OpMod8, 1325 opAndType{OMOD, TUINT8}: ssa.OpMod8u, 1326 opAndType{OMOD, TINT16}: ssa.OpMod16, 1327 opAndType{OMOD, TUINT16}: ssa.OpMod16u, 1328 opAndType{OMOD, TINT32}: ssa.OpMod32, 1329 opAndType{OMOD, TUINT32}: ssa.OpMod32u, 1330 opAndType{OMOD, TINT64}: ssa.OpMod64, 1331 opAndType{OMOD, TUINT64}: ssa.OpMod64u, 1332 1333 opAndType{OAND, TINT8}: ssa.OpAnd8, 1334 opAndType{OAND, TUINT8}: ssa.OpAnd8, 1335 opAndType{OAND, TINT16}: ssa.OpAnd16, 1336 opAndType{OAND, TUINT16}: ssa.OpAnd16, 1337 opAndType{OAND, TINT32}: ssa.OpAnd32, 1338 opAndType{OAND, TUINT32}: ssa.OpAnd32, 1339 opAndType{OAND, TINT64}: ssa.OpAnd64, 1340 opAndType{OAND, TUINT64}: ssa.OpAnd64, 1341 1342 opAndType{OOR, TINT8}: ssa.OpOr8, 1343 opAndType{OOR, TUINT8}: ssa.OpOr8, 1344 opAndType{OOR, TINT16}: ssa.OpOr16, 1345 opAndType{OOR, TUINT16}: ssa.OpOr16, 1346 opAndType{OOR, TINT32}: ssa.OpOr32, 1347 opAndType{OOR, TUINT32}: ssa.OpOr32, 1348 opAndType{OOR, TINT64}: ssa.OpOr64, 1349 opAndType{OOR, TUINT64}: ssa.OpOr64, 1350 1351 opAndType{OXOR, TINT8}: ssa.OpXor8, 1352 opAndType{OXOR, TUINT8}: ssa.OpXor8, 1353 opAndType{OXOR, TINT16}: ssa.OpXor16, 1354 opAndType{OXOR, TUINT16}: ssa.OpXor16, 1355 opAndType{OXOR, TINT32}: ssa.OpXor32, 1356 opAndType{OXOR, TUINT32}: ssa.OpXor32, 1357 opAndType{OXOR, TINT64}: ssa.OpXor64, 1358 opAndType{OXOR, TUINT64}: ssa.OpXor64, 1359 1360 opAndType{OEQ, TBOOL}: ssa.OpEqB, 1361 opAndType{OEQ, TINT8}: ssa.OpEq8, 1362 opAndType{OEQ, TUINT8}: ssa.OpEq8, 1363 opAndType{OEQ, TINT16}: ssa.OpEq16, 1364 opAndType{OEQ, TUINT16}: ssa.OpEq16, 1365 opAndType{OEQ, TINT32}: ssa.OpEq32, 1366 opAndType{OEQ, TUINT32}: ssa.OpEq32, 1367 opAndType{OEQ, TINT64}: ssa.OpEq64, 1368 opAndType{OEQ, TUINT64}: ssa.OpEq64, 1369 opAndType{OEQ, TINTER}: ssa.OpEqInter, 1370 opAndType{OEQ, TSLICE}: ssa.OpEqSlice, 1371 opAndType{OEQ, TFUNC}: ssa.OpEqPtr, 1372 opAndType{OEQ, TMAP}: ssa.OpEqPtr, 1373 opAndType{OEQ, TCHAN}: ssa.OpEqPtr, 1374 opAndType{OEQ, TPTR}: ssa.OpEqPtr, 1375 opAndType{OEQ, TUINTPTR}: ssa.OpEqPtr, 1376 opAndType{OEQ, TUNSAFEPTR}: ssa.OpEqPtr, 1377 opAndType{OEQ, TFLOAT64}: ssa.OpEq64F, 1378 opAndType{OEQ, TFLOAT32}: ssa.OpEq32F, 1379 1380 opAndType{ONE, TBOOL}: ssa.OpNeqB, 1381 opAndType{ONE, TINT8}: ssa.OpNeq8, 1382 opAndType{ONE, TUINT8}: ssa.OpNeq8, 1383 opAndType{ONE, TINT16}: ssa.OpNeq16, 1384 opAndType{ONE, TUINT16}: ssa.OpNeq16, 1385 opAndType{ONE, TINT32}: ssa.OpNeq32, 1386 opAndType{ONE, TUINT32}: ssa.OpNeq32, 1387 opAndType{ONE, TINT64}: ssa.OpNeq64, 1388 opAndType{ONE, TUINT64}: ssa.OpNeq64, 1389 opAndType{ONE, TINTER}: ssa.OpNeqInter, 1390 opAndType{ONE, TSLICE}: ssa.OpNeqSlice, 1391 opAndType{ONE, TFUNC}: ssa.OpNeqPtr, 1392 opAndType{ONE, TMAP}: ssa.OpNeqPtr, 1393 opAndType{ONE, TCHAN}: ssa.OpNeqPtr, 1394 opAndType{ONE, TPTR}: ssa.OpNeqPtr, 1395 opAndType{ONE, TUINTPTR}: ssa.OpNeqPtr, 1396 opAndType{ONE, TUNSAFEPTR}: ssa.OpNeqPtr, 1397 opAndType{ONE, TFLOAT64}: ssa.OpNeq64F, 1398 opAndType{ONE, TFLOAT32}: ssa.OpNeq32F, 1399 1400 opAndType{OLT, TINT8}: ssa.OpLess8, 1401 opAndType{OLT, TUINT8}: ssa.OpLess8U, 1402 opAndType{OLT, TINT16}: ssa.OpLess16, 1403 opAndType{OLT, TUINT16}: ssa.OpLess16U, 1404 opAndType{OLT, TINT32}: ssa.OpLess32, 1405 opAndType{OLT, TUINT32}: ssa.OpLess32U, 1406 opAndType{OLT, TINT64}: ssa.OpLess64, 1407 opAndType{OLT, TUINT64}: ssa.OpLess64U, 1408 opAndType{OLT, TFLOAT64}: ssa.OpLess64F, 1409 opAndType{OLT, TFLOAT32}: ssa.OpLess32F, 1410 1411 opAndType{OGT, TINT8}: ssa.OpGreater8, 1412 opAndType{OGT, TUINT8}: ssa.OpGreater8U, 1413 opAndType{OGT, TINT16}: ssa.OpGreater16, 1414 opAndType{OGT, TUINT16}: ssa.OpGreater16U, 1415 opAndType{OGT, TINT32}: ssa.OpGreater32, 1416 opAndType{OGT, TUINT32}: ssa.OpGreater32U, 1417 opAndType{OGT, TINT64}: ssa.OpGreater64, 1418 opAndType{OGT, TUINT64}: ssa.OpGreater64U, 1419 opAndType{OGT, TFLOAT64}: ssa.OpGreater64F, 1420 opAndType{OGT, TFLOAT32}: ssa.OpGreater32F, 1421 1422 opAndType{OLE, TINT8}: ssa.OpLeq8, 1423 opAndType{OLE, TUINT8}: ssa.OpLeq8U, 1424 opAndType{OLE, TINT16}: ssa.OpLeq16, 1425 opAndType{OLE, TUINT16}: ssa.OpLeq16U, 1426 opAndType{OLE, TINT32}: ssa.OpLeq32, 1427 opAndType{OLE, TUINT32}: ssa.OpLeq32U, 1428 opAndType{OLE, TINT64}: ssa.OpLeq64, 1429 opAndType{OLE, TUINT64}: ssa.OpLeq64U, 1430 opAndType{OLE, TFLOAT64}: ssa.OpLeq64F, 1431 opAndType{OLE, TFLOAT32}: ssa.OpLeq32F, 1432 1433 opAndType{OGE, TINT8}: ssa.OpGeq8, 1434 opAndType{OGE, TUINT8}: ssa.OpGeq8U, 1435 opAndType{OGE, TINT16}: ssa.OpGeq16, 1436 opAndType{OGE, TUINT16}: ssa.OpGeq16U, 1437 opAndType{OGE, TINT32}: ssa.OpGeq32, 1438 opAndType{OGE, TUINT32}: ssa.OpGeq32U, 1439 opAndType{OGE, TINT64}: ssa.OpGeq64, 1440 opAndType{OGE, TUINT64}: ssa.OpGeq64U, 1441 opAndType{OGE, TFLOAT64}: ssa.OpGeq64F, 1442 opAndType{OGE, TFLOAT32}: ssa.OpGeq32F, 1443 } 1444 1445 func (s *state) concreteEtype(t *types.Type) types.EType { 1446 e := t.Etype 1447 switch e { 1448 default: 1449 return e 1450 case TINT: 1451 if s.config.PtrSize == 8 { 1452 return TINT64 1453 } 1454 return TINT32 1455 case TUINT: 1456 if s.config.PtrSize == 8 { 1457 return TUINT64 1458 } 1459 return TUINT32 1460 case TUINTPTR: 1461 if s.config.PtrSize == 8 { 1462 return TUINT64 1463 } 1464 return TUINT32 1465 } 1466 } 1467 1468 func (s *state) ssaOp(op Op, t *types.Type) ssa.Op { 1469 etype := s.concreteEtype(t) 1470 x, ok := opToSSA[opAndType{op, etype}] 1471 if !ok { 1472 s.Fatalf("unhandled binary op %v %s", op, etype) 1473 } 1474 return x 1475 } 1476 1477 func floatForComplex(t *types.Type) *types.Type { 1478 if t.Size() == 8 { 1479 return types.Types[TFLOAT32] 1480 } else { 1481 return types.Types[TFLOAT64] 1482 } 1483 } 1484 1485 type opAndTwoTypes struct { 1486 op Op 1487 etype1 types.EType 1488 etype2 types.EType 1489 } 1490 1491 type twoTypes struct { 1492 etype1 types.EType 1493 etype2 types.EType 1494 } 1495 1496 type twoOpsAndType struct { 1497 op1 ssa.Op 1498 op2 ssa.Op 1499 intermediateType types.EType 1500 } 1501 1502 var fpConvOpToSSA = map[twoTypes]twoOpsAndType{ 1503 1504 twoTypes{TINT8, TFLOAT32}: twoOpsAndType{ssa.OpSignExt8to32, ssa.OpCvt32to32F, TINT32}, 1505 twoTypes{TINT16, TFLOAT32}: twoOpsAndType{ssa.OpSignExt16to32, ssa.OpCvt32to32F, TINT32}, 1506 twoTypes{TINT32, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt32to32F, TINT32}, 1507 twoTypes{TINT64, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt64to32F, TINT64}, 1508 1509 twoTypes{TINT8, TFLOAT64}: twoOpsAndType{ssa.OpSignExt8to32, ssa.OpCvt32to64F, TINT32}, 1510 twoTypes{TINT16, TFLOAT64}: twoOpsAndType{ssa.OpSignExt16to32, ssa.OpCvt32to64F, TINT32}, 1511 twoTypes{TINT32, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt32to64F, TINT32}, 1512 twoTypes{TINT64, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt64to64F, TINT64}, 1513 1514 twoTypes{TFLOAT32, TINT8}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpTrunc32to8, TINT32}, 1515 twoTypes{TFLOAT32, TINT16}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpTrunc32to16, TINT32}, 1516 twoTypes{TFLOAT32, TINT32}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpCopy, TINT32}, 1517 twoTypes{TFLOAT32, TINT64}: twoOpsAndType{ssa.OpCvt32Fto64, ssa.OpCopy, TINT64}, 1518 1519 twoTypes{TFLOAT64, TINT8}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpTrunc32to8, TINT32}, 1520 twoTypes{TFLOAT64, TINT16}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpTrunc32to16, TINT32}, 1521 twoTypes{TFLOAT64, TINT32}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpCopy, TINT32}, 1522 twoTypes{TFLOAT64, TINT64}: twoOpsAndType{ssa.OpCvt64Fto64, ssa.OpCopy, TINT64}, 1523 // unsigned 1524 twoTypes{TUINT8, TFLOAT32}: twoOpsAndType{ssa.OpZeroExt8to32, ssa.OpCvt32to32F, TINT32}, 1525 twoTypes{TUINT16, TFLOAT32}: twoOpsAndType{ssa.OpZeroExt16to32, ssa.OpCvt32to32F, TINT32}, 1526 twoTypes{TUINT32, TFLOAT32}: twoOpsAndType{ssa.OpZeroExt32to64, ssa.OpCvt64to32F, TINT64}, // go wide to dodge unsigned 1527 twoTypes{TUINT64, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpInvalid, TUINT64}, // Cvt64Uto32F, branchy code expansion instead 1528 1529 twoTypes{TUINT8, TFLOAT64}: twoOpsAndType{ssa.OpZeroExt8to32, ssa.OpCvt32to64F, TINT32}, 1530 twoTypes{TUINT16, TFLOAT64}: twoOpsAndType{ssa.OpZeroExt16to32, ssa.OpCvt32to64F, TINT32}, 1531 twoTypes{TUINT32, TFLOAT64}: twoOpsAndType{ssa.OpZeroExt32to64, ssa.OpCvt64to64F, TINT64}, // go wide to dodge unsigned 1532 twoTypes{TUINT64, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpInvalid, TUINT64}, // Cvt64Uto64F, branchy code expansion instead 1533 1534 twoTypes{TFLOAT32, TUINT8}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpTrunc32to8, TINT32}, 1535 twoTypes{TFLOAT32, TUINT16}: twoOpsAndType{ssa.OpCvt32Fto32, ssa.OpTrunc32to16, TINT32}, 1536 twoTypes{TFLOAT32, TUINT32}: twoOpsAndType{ssa.OpCvt32Fto64, ssa.OpTrunc64to32, TINT64}, // go wide to dodge unsigned 1537 twoTypes{TFLOAT32, TUINT64}: twoOpsAndType{ssa.OpInvalid, ssa.OpCopy, TUINT64}, // Cvt32Fto64U, branchy code expansion instead 1538 1539 twoTypes{TFLOAT64, TUINT8}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpTrunc32to8, TINT32}, 1540 twoTypes{TFLOAT64, TUINT16}: twoOpsAndType{ssa.OpCvt64Fto32, ssa.OpTrunc32to16, TINT32}, 1541 twoTypes{TFLOAT64, TUINT32}: twoOpsAndType{ssa.OpCvt64Fto64, ssa.OpTrunc64to32, TINT64}, // go wide to dodge unsigned 1542 twoTypes{TFLOAT64, TUINT64}: twoOpsAndType{ssa.OpInvalid, ssa.OpCopy, TUINT64}, // Cvt64Fto64U, branchy code expansion instead 1543 1544 // float 1545 twoTypes{TFLOAT64, TFLOAT32}: twoOpsAndType{ssa.OpCvt64Fto32F, ssa.OpCopy, TFLOAT32}, 1546 twoTypes{TFLOAT64, TFLOAT64}: twoOpsAndType{ssa.OpRound64F, ssa.OpCopy, TFLOAT64}, 1547 twoTypes{TFLOAT32, TFLOAT32}: twoOpsAndType{ssa.OpRound32F, ssa.OpCopy, TFLOAT32}, 1548 twoTypes{TFLOAT32, TFLOAT64}: twoOpsAndType{ssa.OpCvt32Fto64F, ssa.OpCopy, TFLOAT64}, 1549 } 1550 1551 // this map is used only for 32-bit arch, and only includes the difference 1552 // on 32-bit arch, don't use int64<->float conversion for uint32 1553 var fpConvOpToSSA32 = map[twoTypes]twoOpsAndType{ 1554 twoTypes{TUINT32, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt32Uto32F, TUINT32}, 1555 twoTypes{TUINT32, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt32Uto64F, TUINT32}, 1556 twoTypes{TFLOAT32, TUINT32}: twoOpsAndType{ssa.OpCvt32Fto32U, ssa.OpCopy, TUINT32}, 1557 twoTypes{TFLOAT64, TUINT32}: twoOpsAndType{ssa.OpCvt64Fto32U, ssa.OpCopy, TUINT32}, 1558 } 1559 1560 // uint64<->float conversions, only on machines that have intructions for that 1561 var uint64fpConvOpToSSA = map[twoTypes]twoOpsAndType{ 1562 twoTypes{TUINT64, TFLOAT32}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt64Uto32F, TUINT64}, 1563 twoTypes{TUINT64, TFLOAT64}: twoOpsAndType{ssa.OpCopy, ssa.OpCvt64Uto64F, TUINT64}, 1564 twoTypes{TFLOAT32, TUINT64}: twoOpsAndType{ssa.OpCvt32Fto64U, ssa.OpCopy, TUINT64}, 1565 twoTypes{TFLOAT64, TUINT64}: twoOpsAndType{ssa.OpCvt64Fto64U, ssa.OpCopy, TUINT64}, 1566 } 1567 1568 var shiftOpToSSA = map[opAndTwoTypes]ssa.Op{ 1569 opAndTwoTypes{OLSH, TINT8, TUINT8}: ssa.OpLsh8x8, 1570 opAndTwoTypes{OLSH, TUINT8, TUINT8}: ssa.OpLsh8x8, 1571 opAndTwoTypes{OLSH, TINT8, TUINT16}: ssa.OpLsh8x16, 1572 opAndTwoTypes{OLSH, TUINT8, TUINT16}: ssa.OpLsh8x16, 1573 opAndTwoTypes{OLSH, TINT8, TUINT32}: ssa.OpLsh8x32, 1574 opAndTwoTypes{OLSH, TUINT8, TUINT32}: ssa.OpLsh8x32, 1575 opAndTwoTypes{OLSH, TINT8, TUINT64}: ssa.OpLsh8x64, 1576 opAndTwoTypes{OLSH, TUINT8, TUINT64}: ssa.OpLsh8x64, 1577 1578 opAndTwoTypes{OLSH, TINT16, TUINT8}: ssa.OpLsh16x8, 1579 opAndTwoTypes{OLSH, TUINT16, TUINT8}: ssa.OpLsh16x8, 1580 opAndTwoTypes{OLSH, TINT16, TUINT16}: ssa.OpLsh16x16, 1581 opAndTwoTypes{OLSH, TUINT16, TUINT16}: ssa.OpLsh16x16, 1582 opAndTwoTypes{OLSH, TINT16, TUINT32}: ssa.OpLsh16x32, 1583 opAndTwoTypes{OLSH, TUINT16, TUINT32}: ssa.OpLsh16x32, 1584 opAndTwoTypes{OLSH, TINT16, TUINT64}: ssa.OpLsh16x64, 1585 opAndTwoTypes{OLSH, TUINT16, TUINT64}: ssa.OpLsh16x64, 1586 1587 opAndTwoTypes{OLSH, TINT32, TUINT8}: ssa.OpLsh32x8, 1588 opAndTwoTypes{OLSH, TUINT32, TUINT8}: ssa.OpLsh32x8, 1589 opAndTwoTypes{OLSH, TINT32, TUINT16}: ssa.OpLsh32x16, 1590 opAndTwoTypes{OLSH, TUINT32, TUINT16}: ssa.OpLsh32x16, 1591 opAndTwoTypes{OLSH, TINT32, TUINT32}: ssa.OpLsh32x32, 1592 opAndTwoTypes{OLSH, TUINT32, TUINT32}: ssa.OpLsh32x32, 1593 opAndTwoTypes{OLSH, TINT32, TUINT64}: ssa.OpLsh32x64, 1594 opAndTwoTypes{OLSH, TUINT32, TUINT64}: ssa.OpLsh32x64, 1595 1596 opAndTwoTypes{OLSH, TINT64, TUINT8}: ssa.OpLsh64x8, 1597 opAndTwoTypes{OLSH, TUINT64, TUINT8}: ssa.OpLsh64x8, 1598 opAndTwoTypes{OLSH, TINT64, TUINT16}: ssa.OpLsh64x16, 1599 opAndTwoTypes{OLSH, TUINT64, TUINT16}: ssa.OpLsh64x16, 1600 opAndTwoTypes{OLSH, TINT64, TUINT32}: ssa.OpLsh64x32, 1601 opAndTwoTypes{OLSH, TUINT64, TUINT32}: ssa.OpLsh64x32, 1602 opAndTwoTypes{OLSH, TINT64, TUINT64}: ssa.OpLsh64x64, 1603 opAndTwoTypes{OLSH, TUINT64, TUINT64}: ssa.OpLsh64x64, 1604 1605 opAndTwoTypes{ORSH, TINT8, TUINT8}: ssa.OpRsh8x8, 1606 opAndTwoTypes{ORSH, TUINT8, TUINT8}: ssa.OpRsh8Ux8, 1607 opAndTwoTypes{ORSH, TINT8, TUINT16}: ssa.OpRsh8x16, 1608 opAndTwoTypes{ORSH, TUINT8, TUINT16}: ssa.OpRsh8Ux16, 1609 opAndTwoTypes{ORSH, TINT8, TUINT32}: ssa.OpRsh8x32, 1610 opAndTwoTypes{ORSH, TUINT8, TUINT32}: ssa.OpRsh8Ux32, 1611 opAndTwoTypes{ORSH, TINT8, TUINT64}: ssa.OpRsh8x64, 1612 opAndTwoTypes{ORSH, TUINT8, TUINT64}: ssa.OpRsh8Ux64, 1613 1614 opAndTwoTypes{ORSH, TINT16, TUINT8}: ssa.OpRsh16x8, 1615 opAndTwoTypes{ORSH, TUINT16, TUINT8}: ssa.OpRsh16Ux8, 1616 opAndTwoTypes{ORSH, TINT16, TUINT16}: ssa.OpRsh16x16, 1617 opAndTwoTypes{ORSH, TUINT16, TUINT16}: ssa.OpRsh16Ux16, 1618 opAndTwoTypes{ORSH, TINT16, TUINT32}: ssa.OpRsh16x32, 1619 opAndTwoTypes{ORSH, TUINT16, TUINT32}: ssa.OpRsh16Ux32, 1620 opAndTwoTypes{ORSH, TINT16, TUINT64}: ssa.OpRsh16x64, 1621 opAndTwoTypes{ORSH, TUINT16, TUINT64}: ssa.OpRsh16Ux64, 1622 1623 opAndTwoTypes{ORSH, TINT32, TUINT8}: ssa.OpRsh32x8, 1624 opAndTwoTypes{ORSH, TUINT32, TUINT8}: ssa.OpRsh32Ux8, 1625 opAndTwoTypes{ORSH, TINT32, TUINT16}: ssa.OpRsh32x16, 1626 opAndTwoTypes{ORSH, TUINT32, TUINT16}: ssa.OpRsh32Ux16, 1627 opAndTwoTypes{ORSH, TINT32, TUINT32}: ssa.OpRsh32x32, 1628 opAndTwoTypes{ORSH, TUINT32, TUINT32}: ssa.OpRsh32Ux32, 1629 opAndTwoTypes{ORSH, TINT32, TUINT64}: ssa.OpRsh32x64, 1630 opAndTwoTypes{ORSH, TUINT32, TUINT64}: ssa.OpRsh32Ux64, 1631 1632 opAndTwoTypes{ORSH, TINT64, TUINT8}: ssa.OpRsh64x8, 1633 opAndTwoTypes{ORSH, TUINT64, TUINT8}: ssa.OpRsh64Ux8, 1634 opAndTwoTypes{ORSH, TINT64, TUINT16}: ssa.OpRsh64x16, 1635 opAndTwoTypes{ORSH, TUINT64, TUINT16}: ssa.OpRsh64Ux16, 1636 opAndTwoTypes{ORSH, TINT64, TUINT32}: ssa.OpRsh64x32, 1637 opAndTwoTypes{ORSH, TUINT64, TUINT32}: ssa.OpRsh64Ux32, 1638 opAndTwoTypes{ORSH, TINT64, TUINT64}: ssa.OpRsh64x64, 1639 opAndTwoTypes{ORSH, TUINT64, TUINT64}: ssa.OpRsh64Ux64, 1640 } 1641 1642 func (s *state) ssaShiftOp(op Op, t *types.Type, u *types.Type) ssa.Op { 1643 etype1 := s.concreteEtype(t) 1644 etype2 := s.concreteEtype(u) 1645 x, ok := shiftOpToSSA[opAndTwoTypes{op, etype1, etype2}] 1646 if !ok { 1647 s.Fatalf("unhandled shift op %v etype=%s/%s", op, etype1, etype2) 1648 } 1649 return x 1650 } 1651 1652 // expr converts the expression n to ssa, adds it to s and returns the ssa result. 1653 func (s *state) expr(n *Node) *ssa.Value { 1654 if !(n.Op == ONAME || n.Op == OLITERAL && n.Sym != nil) { 1655 // ONAMEs and named OLITERALs have the line number 1656 // of the decl, not the use. See issue 14742. 1657 s.pushLine(n.Pos) 1658 defer s.popLine() 1659 } 1660 1661 s.stmtList(n.Ninit) 1662 switch n.Op { 1663 case OBYTES2STRTMP: 1664 slice := s.expr(n.Left) 1665 ptr := s.newValue1(ssa.OpSlicePtr, s.f.Config.Types.BytePtr, slice) 1666 len := s.newValue1(ssa.OpSliceLen, types.Types[TINT], slice) 1667 return s.newValue2(ssa.OpStringMake, n.Type, ptr, len) 1668 case OSTR2BYTESTMP: 1669 str := s.expr(n.Left) 1670 ptr := s.newValue1(ssa.OpStringPtr, s.f.Config.Types.BytePtr, str) 1671 len := s.newValue1(ssa.OpStringLen, types.Types[TINT], str) 1672 return s.newValue3(ssa.OpSliceMake, n.Type, ptr, len, len) 1673 case OCFUNC: 1674 aux := n.Left.Sym.Linksym() 1675 return s.entryNewValue1A(ssa.OpAddr, n.Type, aux, s.sb) 1676 case ONAME: 1677 if n.Class() == PFUNC { 1678 // "value" of a function is the address of the function's closure 1679 sym := funcsym(n.Sym).Linksym() 1680 return s.entryNewValue1A(ssa.OpAddr, types.NewPtr(n.Type), sym, s.sb) 1681 } 1682 if s.canSSA(n) { 1683 return s.variable(n, n.Type) 1684 } 1685 addr := s.addr(n, false) 1686 return s.load(n.Type, addr) 1687 case OCLOSUREVAR: 1688 addr := s.addr(n, false) 1689 return s.load(n.Type, addr) 1690 case OLITERAL: 1691 switch u := n.Val().U.(type) { 1692 case *Mpint: 1693 i := u.Int64() 1694 switch n.Type.Size() { 1695 case 1: 1696 return s.constInt8(n.Type, int8(i)) 1697 case 2: 1698 return s.constInt16(n.Type, int16(i)) 1699 case 4: 1700 return s.constInt32(n.Type, int32(i)) 1701 case 8: 1702 return s.constInt64(n.Type, i) 1703 default: 1704 s.Fatalf("bad integer size %d", n.Type.Size()) 1705 return nil 1706 } 1707 case string: 1708 if u == "" { 1709 return s.constEmptyString(n.Type) 1710 } 1711 return s.entryNewValue0A(ssa.OpConstString, n.Type, u) 1712 case bool: 1713 return s.constBool(u) 1714 case *NilVal: 1715 t := n.Type 1716 switch { 1717 case t.IsSlice(): 1718 return s.constSlice(t) 1719 case t.IsInterface(): 1720 return s.constInterface(t) 1721 default: 1722 return s.constNil(t) 1723 } 1724 case *Mpflt: 1725 switch n.Type.Size() { 1726 case 4: 1727 return s.constFloat32(n.Type, u.Float32()) 1728 case 8: 1729 return s.constFloat64(n.Type, u.Float64()) 1730 default: 1731 s.Fatalf("bad float size %d", n.Type.Size()) 1732 return nil 1733 } 1734 case *Mpcplx: 1735 r := &u.Real 1736 i := &u.Imag 1737 switch n.Type.Size() { 1738 case 8: 1739 pt := types.Types[TFLOAT32] 1740 return s.newValue2(ssa.OpComplexMake, n.Type, 1741 s.constFloat32(pt, r.Float32()), 1742 s.constFloat32(pt, i.Float32())) 1743 case 16: 1744 pt := types.Types[TFLOAT64] 1745 return s.newValue2(ssa.OpComplexMake, n.Type, 1746 s.constFloat64(pt, r.Float64()), 1747 s.constFloat64(pt, i.Float64())) 1748 default: 1749 s.Fatalf("bad float size %d", n.Type.Size()) 1750 return nil 1751 } 1752 1753 default: 1754 s.Fatalf("unhandled OLITERAL %v", n.Val().Ctype()) 1755 return nil 1756 } 1757 case OCONVNOP: 1758 to := n.Type 1759 from := n.Left.Type 1760 1761 // Assume everything will work out, so set up our return value. 1762 // Anything interesting that happens from here is a fatal. 1763 x := s.expr(n.Left) 1764 1765 // Special case for not confusing GC and liveness. 1766 // We don't want pointers accidentally classified 1767 // as not-pointers or vice-versa because of copy 1768 // elision. 1769 if to.IsPtrShaped() != from.IsPtrShaped() { 1770 return s.newValue2(ssa.OpConvert, to, x, s.mem()) 1771 } 1772 1773 v := s.newValue1(ssa.OpCopy, to, x) // ensure that v has the right type 1774 1775 // CONVNOP closure 1776 if to.Etype == TFUNC && from.IsPtrShaped() { 1777 return v 1778 } 1779 1780 // named <--> unnamed type or typed <--> untyped const 1781 if from.Etype == to.Etype { 1782 return v 1783 } 1784 1785 // unsafe.Pointer <--> *T 1786 if to.Etype == TUNSAFEPTR && from.IsPtrShaped() || from.Etype == TUNSAFEPTR && to.IsPtrShaped() { 1787 return v 1788 } 1789 1790 // map <--> *hmap 1791 if to.Etype == TMAP && from.IsPtr() && 1792 to.MapType().Hmap == from.Elem() { 1793 return v 1794 } 1795 1796 dowidth(from) 1797 dowidth(to) 1798 if from.Width != to.Width { 1799 s.Fatalf("CONVNOP width mismatch %v (%d) -> %v (%d)\n", from, from.Width, to, to.Width) 1800 return nil 1801 } 1802 if etypesign(from.Etype) != etypesign(to.Etype) { 1803 s.Fatalf("CONVNOP sign mismatch %v (%s) -> %v (%s)\n", from, from.Etype, to, to.Etype) 1804 return nil 1805 } 1806 1807 if instrumenting { 1808 // These appear to be fine, but they fail the 1809 // integer constraint below, so okay them here. 1810 // Sample non-integer conversion: map[string]string -> *uint8 1811 return v 1812 } 1813 1814 if etypesign(from.Etype) == 0 { 1815 s.Fatalf("CONVNOP unrecognized non-integer %v -> %v\n", from, to) 1816 return nil 1817 } 1818 1819 // integer, same width, same sign 1820 return v 1821 1822 case OCONV: 1823 x := s.expr(n.Left) 1824 ft := n.Left.Type // from type 1825 tt := n.Type // to type 1826 if ft.IsBoolean() && tt.IsKind(TUINT8) { 1827 // Bool -> uint8 is generated internally when indexing into runtime.staticbyte. 1828 return s.newValue1(ssa.OpCopy, n.Type, x) 1829 } 1830 if ft.IsInteger() && tt.IsInteger() { 1831 var op ssa.Op 1832 if tt.Size() == ft.Size() { 1833 op = ssa.OpCopy 1834 } else if tt.Size() < ft.Size() { 1835 // truncation 1836 switch 10*ft.Size() + tt.Size() { 1837 case 21: 1838 op = ssa.OpTrunc16to8 1839 case 41: 1840 op = ssa.OpTrunc32to8 1841 case 42: 1842 op = ssa.OpTrunc32to16 1843 case 81: 1844 op = ssa.OpTrunc64to8 1845 case 82: 1846 op = ssa.OpTrunc64to16 1847 case 84: 1848 op = ssa.OpTrunc64to32 1849 default: 1850 s.Fatalf("weird integer truncation %v -> %v", ft, tt) 1851 } 1852 } else if ft.IsSigned() { 1853 // sign extension 1854 switch 10*ft.Size() + tt.Size() { 1855 case 12: 1856 op = ssa.OpSignExt8to16 1857 case 14: 1858 op = ssa.OpSignExt8to32 1859 case 18: 1860 op = ssa.OpSignExt8to64 1861 case 24: 1862 op = ssa.OpSignExt16to32 1863 case 28: 1864 op = ssa.OpSignExt16to64 1865 case 48: 1866 op = ssa.OpSignExt32to64 1867 default: 1868 s.Fatalf("bad integer sign extension %v -> %v", ft, tt) 1869 } 1870 } else { 1871 // zero extension 1872 switch 10*ft.Size() + tt.Size() { 1873 case 12: 1874 op = ssa.OpZeroExt8to16 1875 case 14: 1876 op = ssa.OpZeroExt8to32 1877 case 18: 1878 op = ssa.OpZeroExt8to64 1879 case 24: 1880 op = ssa.OpZeroExt16to32 1881 case 28: 1882 op = ssa.OpZeroExt16to64 1883 case 48: 1884 op = ssa.OpZeroExt32to64 1885 default: 1886 s.Fatalf("weird integer sign extension %v -> %v", ft, tt) 1887 } 1888 } 1889 return s.newValue1(op, n.Type, x) 1890 } 1891 1892 if ft.IsFloat() || tt.IsFloat() { 1893 conv, ok := fpConvOpToSSA[twoTypes{s.concreteEtype(ft), s.concreteEtype(tt)}] 1894 if s.config.RegSize == 4 && thearch.LinkArch.Family != sys.MIPS && !s.softFloat { 1895 if conv1, ok1 := fpConvOpToSSA32[twoTypes{s.concreteEtype(ft), s.concreteEtype(tt)}]; ok1 { 1896 conv = conv1 1897 } 1898 } 1899 if thearch.LinkArch.Family == sys.ARM64 || thearch.LinkArch.Family == sys.Wasm || s.softFloat { 1900 if conv1, ok1 := uint64fpConvOpToSSA[twoTypes{s.concreteEtype(ft), s.concreteEtype(tt)}]; ok1 { 1901 conv = conv1 1902 } 1903 } 1904 1905 if thearch.LinkArch.Family == sys.MIPS && !s.softFloat { 1906 if ft.Size() == 4 && ft.IsInteger() && !ft.IsSigned() { 1907 // tt is float32 or float64, and ft is also unsigned 1908 if tt.Size() == 4 { 1909 return s.uint32Tofloat32(n, x, ft, tt) 1910 } 1911 if tt.Size() == 8 { 1912 return s.uint32Tofloat64(n, x, ft, tt) 1913 } 1914 } else if tt.Size() == 4 && tt.IsInteger() && !tt.IsSigned() { 1915 // ft is float32 or float64, and tt is unsigned integer 1916 if ft.Size() == 4 { 1917 return s.float32ToUint32(n, x, ft, tt) 1918 } 1919 if ft.Size() == 8 { 1920 return s.float64ToUint32(n, x, ft, tt) 1921 } 1922 } 1923 } 1924 1925 if !ok { 1926 s.Fatalf("weird float conversion %v -> %v", ft, tt) 1927 } 1928 op1, op2, it := conv.op1, conv.op2, conv.intermediateType 1929 1930 if op1 != ssa.OpInvalid && op2 != ssa.OpInvalid { 1931 // normal case, not tripping over unsigned 64 1932 if op1 == ssa.OpCopy { 1933 if op2 == ssa.OpCopy { 1934 return x 1935 } 1936 return s.newValueOrSfCall1(op2, n.Type, x) 1937 } 1938 if op2 == ssa.OpCopy { 1939 return s.newValueOrSfCall1(op1, n.Type, x) 1940 } 1941 return s.newValueOrSfCall1(op2, n.Type, s.newValueOrSfCall1(op1, types.Types[it], x)) 1942 } 1943 // Tricky 64-bit unsigned cases. 1944 if ft.IsInteger() { 1945 // tt is float32 or float64, and ft is also unsigned 1946 if tt.Size() == 4 { 1947 return s.uint64Tofloat32(n, x, ft, tt) 1948 } 1949 if tt.Size() == 8 { 1950 return s.uint64Tofloat64(n, x, ft, tt) 1951 } 1952 s.Fatalf("weird unsigned integer to float conversion %v -> %v", ft, tt) 1953 } 1954 // ft is float32 or float64, and tt is unsigned integer 1955 if ft.Size() == 4 { 1956 return s.float32ToUint64(n, x, ft, tt) 1957 } 1958 if ft.Size() == 8 { 1959 return s.float64ToUint64(n, x, ft, tt) 1960 } 1961 s.Fatalf("weird float to unsigned integer conversion %v -> %v", ft, tt) 1962 return nil 1963 } 1964 1965 if ft.IsComplex() && tt.IsComplex() { 1966 var op ssa.Op 1967 if ft.Size() == tt.Size() { 1968 switch ft.Size() { 1969 case 8: 1970 op = ssa.OpRound32F 1971 case 16: 1972 op = ssa.OpRound64F 1973 default: 1974 s.Fatalf("weird complex conversion %v -> %v", ft, tt) 1975 } 1976 } else if ft.Size() == 8 && tt.Size() == 16 { 1977 op = ssa.OpCvt32Fto64F 1978 } else if ft.Size() == 16 && tt.Size() == 8 { 1979 op = ssa.OpCvt64Fto32F 1980 } else { 1981 s.Fatalf("weird complex conversion %v -> %v", ft, tt) 1982 } 1983 ftp := floatForComplex(ft) 1984 ttp := floatForComplex(tt) 1985 return s.newValue2(ssa.OpComplexMake, tt, 1986 s.newValueOrSfCall1(op, ttp, s.newValue1(ssa.OpComplexReal, ftp, x)), 1987 s.newValueOrSfCall1(op, ttp, s.newValue1(ssa.OpComplexImag, ftp, x))) 1988 } 1989 1990 s.Fatalf("unhandled OCONV %s -> %s", n.Left.Type.Etype, n.Type.Etype) 1991 return nil 1992 1993 case ODOTTYPE: 1994 res, _ := s.dottype(n, false) 1995 return res 1996 1997 // binary ops 1998 case OLT, OEQ, ONE, OLE, OGE, OGT: 1999 a := s.expr(n.Left) 2000 b := s.expr(n.Right) 2001 if n.Left.Type.IsComplex() { 2002 pt := floatForComplex(n.Left.Type) 2003 op := s.ssaOp(OEQ, pt) 2004 r := s.newValueOrSfCall2(op, types.Types[TBOOL], s.newValue1(ssa.OpComplexReal, pt, a), s.newValue1(ssa.OpComplexReal, pt, b)) 2005 i := s.newValueOrSfCall2(op, types.Types[TBOOL], s.newValue1(ssa.OpComplexImag, pt, a), s.newValue1(ssa.OpComplexImag, pt, b)) 2006 c := s.newValue2(ssa.OpAndB, types.Types[TBOOL], r, i) 2007 switch n.Op { 2008 case OEQ: 2009 return c 2010 case ONE: 2011 return s.newValue1(ssa.OpNot, types.Types[TBOOL], c) 2012 default: 2013 s.Fatalf("ordered complex compare %v", n.Op) 2014 } 2015 } 2016 if n.Left.Type.IsFloat() { 2017 return s.newValueOrSfCall2(s.ssaOp(n.Op, n.Left.Type), types.Types[TBOOL], a, b) 2018 } 2019 return s.newValue2(s.ssaOp(n.Op, n.Left.Type), types.Types[TBOOL], a, b) 2020 case OMUL: 2021 a := s.expr(n.Left) 2022 b := s.expr(n.Right) 2023 if n.Type.IsComplex() { 2024 mulop := ssa.OpMul64F 2025 addop := ssa.OpAdd64F 2026 subop := ssa.OpSub64F 2027 pt := floatForComplex(n.Type) // Could be Float32 or Float64 2028 wt := types.Types[TFLOAT64] // Compute in Float64 to minimize cancelation error 2029 2030 areal := s.newValue1(ssa.OpComplexReal, pt, a) 2031 breal := s.newValue1(ssa.OpComplexReal, pt, b) 2032 aimag := s.newValue1(ssa.OpComplexImag, pt, a) 2033 bimag := s.newValue1(ssa.OpComplexImag, pt, b) 2034 2035 if pt != wt { // Widen for calculation 2036 areal = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, areal) 2037 breal = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, breal) 2038 aimag = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, aimag) 2039 bimag = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, bimag) 2040 } 2041 2042 xreal := s.newValueOrSfCall2(subop, wt, s.newValueOrSfCall2(mulop, wt, areal, breal), s.newValueOrSfCall2(mulop, wt, aimag, bimag)) 2043 ximag := s.newValueOrSfCall2(addop, wt, s.newValueOrSfCall2(mulop, wt, areal, bimag), s.newValueOrSfCall2(mulop, wt, aimag, breal)) 2044 2045 if pt != wt { // Narrow to store back 2046 xreal = s.newValueOrSfCall1(ssa.OpCvt64Fto32F, pt, xreal) 2047 ximag = s.newValueOrSfCall1(ssa.OpCvt64Fto32F, pt, ximag) 2048 } 2049 2050 return s.newValue2(ssa.OpComplexMake, n.Type, xreal, ximag) 2051 } 2052 2053 if n.Type.IsFloat() { 2054 return s.newValueOrSfCall2(s.ssaOp(n.Op, n.Type), a.Type, a, b) 2055 } 2056 2057 return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b) 2058 2059 case ODIV: 2060 a := s.expr(n.Left) 2061 b := s.expr(n.Right) 2062 if n.Type.IsComplex() { 2063 // TODO this is not executed because the front-end substitutes a runtime call. 2064 // That probably ought to change; with modest optimization the widen/narrow 2065 // conversions could all be elided in larger expression trees. 2066 mulop := ssa.OpMul64F 2067 addop := ssa.OpAdd64F 2068 subop := ssa.OpSub64F 2069 divop := ssa.OpDiv64F 2070 pt := floatForComplex(n.Type) // Could be Float32 or Float64 2071 wt := types.Types[TFLOAT64] // Compute in Float64 to minimize cancelation error 2072 2073 areal := s.newValue1(ssa.OpComplexReal, pt, a) 2074 breal := s.newValue1(ssa.OpComplexReal, pt, b) 2075 aimag := s.newValue1(ssa.OpComplexImag, pt, a) 2076 bimag := s.newValue1(ssa.OpComplexImag, pt, b) 2077 2078 if pt != wt { // Widen for calculation 2079 areal = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, areal) 2080 breal = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, breal) 2081 aimag = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, aimag) 2082 bimag = s.newValueOrSfCall1(ssa.OpCvt32Fto64F, wt, bimag) 2083 } 2084 2085 denom := s.newValueOrSfCall2(addop, wt, s.newValueOrSfCall2(mulop, wt, breal, breal), s.newValueOrSfCall2(mulop, wt, bimag, bimag)) 2086 xreal := s.newValueOrSfCall2(addop, wt, s.newValueOrSfCall2(mulop, wt, areal, breal), s.newValueOrSfCall2(mulop, wt, aimag, bimag)) 2087 ximag := s.newValueOrSfCall2(subop, wt, s.newValueOrSfCall2(mulop, wt, aimag, breal), s.newValueOrSfCall2(mulop, wt, areal, bimag)) 2088 2089 // TODO not sure if this is best done in wide precision or narrow 2090 // Double-rounding might be an issue. 2091 // Note that the pre-SSA implementation does the entire calculation 2092 // in wide format, so wide is compatible. 2093 xreal = s.newValueOrSfCall2(divop, wt, xreal, denom) 2094 ximag = s.newValueOrSfCall2(divop, wt, ximag, denom) 2095 2096 if pt != wt { // Narrow to store back 2097 xreal = s.newValueOrSfCall1(ssa.OpCvt64Fto32F, pt, xreal) 2098 ximag = s.newValueOrSfCall1(ssa.OpCvt64Fto32F, pt, ximag) 2099 } 2100 return s.newValue2(ssa.OpComplexMake, n.Type, xreal, ximag) 2101 } 2102 if n.Type.IsFloat() { 2103 return s.newValueOrSfCall2(s.ssaOp(n.Op, n.Type), a.Type, a, b) 2104 } 2105 return s.intDivide(n, a, b) 2106 case OMOD: 2107 a := s.expr(n.Left) 2108 b := s.expr(n.Right) 2109 return s.intDivide(n, a, b) 2110 case OADD, OSUB: 2111 a := s.expr(n.Left) 2112 b := s.expr(n.Right) 2113 if n.Type.IsComplex() { 2114 pt := floatForComplex(n.Type) 2115 op := s.ssaOp(n.Op, pt) 2116 return s.newValue2(ssa.OpComplexMake, n.Type, 2117 s.newValueOrSfCall2(op, pt, s.newValue1(ssa.OpComplexReal, pt, a), s.newValue1(ssa.OpComplexReal, pt, b)), 2118 s.newValueOrSfCall2(op, pt, s.newValue1(ssa.OpComplexImag, pt, a), s.newValue1(ssa.OpComplexImag, pt, b))) 2119 } 2120 if n.Type.IsFloat() { 2121 return s.newValueOrSfCall2(s.ssaOp(n.Op, n.Type), a.Type, a, b) 2122 } 2123 return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b) 2124 case OAND, OOR, OXOR: 2125 a := s.expr(n.Left) 2126 b := s.expr(n.Right) 2127 return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b) 2128 case OLSH, ORSH: 2129 a := s.expr(n.Left) 2130 b := s.expr(n.Right) 2131 return s.newValue2(s.ssaShiftOp(n.Op, n.Type, n.Right.Type), a.Type, a, b) 2132 case OANDAND, OOROR: 2133 // To implement OANDAND (and OOROR), we introduce a 2134 // new temporary variable to hold the result. The 2135 // variable is associated with the OANDAND node in the 2136 // s.vars table (normally variables are only 2137 // associated with ONAME nodes). We convert 2138 // A && B 2139 // to 2140 // var = A 2141 // if var { 2142 // var = B 2143 // } 2144 // Using var in the subsequent block introduces the 2145 // necessary phi variable. 2146 el := s.expr(n.Left) 2147 s.vars[n] = el 2148 2149 b := s.endBlock() 2150 b.Kind = ssa.BlockIf 2151 b.SetControl(el) 2152 // In theory, we should set b.Likely here based on context. 2153 // However, gc only gives us likeliness hints 2154 // in a single place, for plain OIF statements, 2155 // and passing around context is finnicky, so don't bother for now. 2156 2157 bRight := s.f.NewBlock(ssa.BlockPlain) 2158 bResult := s.f.NewBlock(ssa.BlockPlain) 2159 if n.Op == OANDAND { 2160 b.AddEdgeTo(bRight) 2161 b.AddEdgeTo(bResult) 2162 } else if n.Op == OOROR { 2163 b.AddEdgeTo(bResult) 2164 b.AddEdgeTo(bRight) 2165 } 2166 2167 s.startBlock(bRight) 2168 er := s.expr(n.Right) 2169 s.vars[n] = er 2170 2171 b = s.endBlock() 2172 b.AddEdgeTo(bResult) 2173 2174 s.startBlock(bResult) 2175 return s.variable(n, types.Types[TBOOL]) 2176 case OCOMPLEX: 2177 r := s.expr(n.Left) 2178 i := s.expr(n.Right) 2179 return s.newValue2(ssa.OpComplexMake, n.Type, r, i) 2180 2181 // unary ops 2182 case ONEG: 2183 a := s.expr(n.Left) 2184 if n.Type.IsComplex() { 2185 tp := floatForComplex(n.Type) 2186 negop := s.ssaOp(n.Op, tp) 2187 return s.newValue2(ssa.OpComplexMake, n.Type, 2188 s.newValue1(negop, tp, s.newValue1(ssa.OpComplexReal, tp, a)), 2189 s.newValue1(negop, tp, s.newValue1(ssa.OpComplexImag, tp, a))) 2190 } 2191 return s.newValue1(s.ssaOp(n.Op, n.Type), a.Type, a) 2192 case ONOT, OBITNOT: 2193 a := s.expr(n.Left) 2194 return s.newValue1(s.ssaOp(n.Op, n.Type), a.Type, a) 2195 case OIMAG, OREAL: 2196 a := s.expr(n.Left) 2197 return s.newValue1(s.ssaOp(n.Op, n.Left.Type), n.Type, a) 2198 case OPLUS: 2199 return s.expr(n.Left) 2200 2201 case OADDR: 2202 return s.addr(n.Left, n.Bounded()) 2203 2204 case OINDREGSP: 2205 addr := s.constOffPtrSP(types.NewPtr(n.Type), n.Xoffset) 2206 return s.load(n.Type, addr) 2207 2208 case ODEREF: 2209 p := s.exprPtr(n.Left, false, n.Pos) 2210 return s.load(n.Type, p) 2211 2212 case ODOT: 2213 if n.Left.Op == OSTRUCTLIT { 2214 // All literals with nonzero fields have already been 2215 // rewritten during walk. Any that remain are just T{} 2216 // or equivalents. Use the zero value. 2217 if !isZero(n.Left) { 2218 Fatalf("literal with nonzero value in SSA: %v", n.Left) 2219 } 2220 return s.zeroVal(n.Type) 2221 } 2222 // If n is addressable and can't be represented in 2223 // SSA, then load just the selected field. This 2224 // prevents false memory dependencies in race/msan 2225 // instrumentation. 2226 if islvalue(n) && !s.canSSA(n) { 2227 p := s.addr(n, false) 2228 return s.load(n.Type, p) 2229 } 2230 v := s.expr(n.Left) 2231 return s.newValue1I(ssa.OpStructSelect, n.Type, int64(fieldIdx(n)), v) 2232 2233 case ODOTPTR: 2234 p := s.exprPtr(n.Left, false, n.Pos) 2235 p = s.newValue1I(ssa.OpOffPtr, types.NewPtr(n.Type), n.Xoffset, p) 2236 return s.load(n.Type, p) 2237 2238 case OINDEX: 2239 switch { 2240 case n.Left.Type.IsString(): 2241 if n.Bounded() && Isconst(n.Left, CTSTR) && Isconst(n.Right, CTINT) { 2242 // Replace "abc"[1] with 'b'. 2243 // Delayed until now because "abc"[1] is not an ideal constant. 2244 // See test/fixedbugs/issue11370.go. 2245 return s.newValue0I(ssa.OpConst8, types.Types[TUINT8], int64(int8(n.Left.Val().U.(string)[n.Right.Int64()]))) 2246 } 2247 a := s.expr(n.Left) 2248 i := s.expr(n.Right) 2249 i = s.extendIndex(i, panicindex) 2250 if !n.Bounded() { 2251 len := s.newValue1(ssa.OpStringLen, types.Types[TINT], a) 2252 s.boundsCheck(i, len) 2253 } 2254 ptrtyp := s.f.Config.Types.BytePtr 2255 ptr := s.newValue1(ssa.OpStringPtr, ptrtyp, a) 2256 if Isconst(n.Right, CTINT) { 2257 ptr = s.newValue1I(ssa.OpOffPtr, ptrtyp, n.Right.Int64(), ptr) 2258 } else { 2259 ptr = s.newValue2(ssa.OpAddPtr, ptrtyp, ptr, i) 2260 } 2261 return s.load(types.Types[TUINT8], ptr) 2262 case n.Left.Type.IsSlice(): 2263 p := s.addr(n, false) 2264 return s.load(n.Left.Type.Elem(), p) 2265 case n.Left.Type.IsArray(): 2266 if canSSAType(n.Left.Type) { 2267 // SSA can handle arrays of length at most 1. 2268 bound := n.Left.Type.NumElem() 2269 a := s.expr(n.Left) 2270 i := s.expr(n.Right) 2271 if bound == 0 { 2272 // Bounds check will never succeed. Might as well 2273 // use constants for the bounds check. 2274 z := s.constInt(types.Types[TINT], 0) 2275 s.boundsCheck(z, z) 2276 // The return value won't be live, return junk. 2277 return s.newValue0(ssa.OpUnknown, n.Type) 2278 } 2279 i = s.extendIndex(i, panicindex) 2280 if !n.Bounded() { 2281 s.boundsCheck(i, s.constInt(types.Types[TINT], bound)) 2282 } 2283 return s.newValue1I(ssa.OpArraySelect, n.Type, 0, a) 2284 } 2285 p := s.addr(n, false) 2286 return s.load(n.Left.Type.Elem(), p) 2287 default: 2288 s.Fatalf("bad type for index %v", n.Left.Type) 2289 return nil 2290 } 2291 2292 case OLEN, OCAP: 2293 switch { 2294 case n.Left.Type.IsSlice(): 2295 op := ssa.OpSliceLen 2296 if n.Op == OCAP { 2297 op = ssa.OpSliceCap 2298 } 2299 return s.newValue1(op, types.Types[TINT], s.expr(n.Left)) 2300 case n.Left.Type.IsString(): // string; not reachable for OCAP 2301 return s.newValue1(ssa.OpStringLen, types.Types[TINT], s.expr(n.Left)) 2302 case n.Left.Type.IsMap(), n.Left.Type.IsChan(): 2303 return s.referenceTypeBuiltin(n, s.expr(n.Left)) 2304 default: // array 2305 return s.constInt(types.Types[TINT], n.Left.Type.NumElem()) 2306 } 2307 2308 case OSPTR: 2309 a := s.expr(n.Left) 2310 if n.Left.Type.IsSlice() { 2311 return s.newValue1(ssa.OpSlicePtr, n.Type, a) 2312 } else { 2313 return s.newValue1(ssa.OpStringPtr, n.Type, a) 2314 } 2315 2316 case OITAB: 2317 a := s.expr(n.Left) 2318 return s.newValue1(ssa.OpITab, n.Type, a) 2319 2320 case OIDATA: 2321 a := s.expr(n.Left) 2322 return s.newValue1(ssa.OpIData, n.Type, a) 2323 2324 case OEFACE: 2325 tab := s.expr(n.Left) 2326 data := s.expr(n.Right) 2327 return s.newValue2(ssa.OpIMake, n.Type, tab, data) 2328 2329 case OSLICEHEADER: 2330 p := s.expr(n.Left) 2331 l := s.expr(n.List.First()) 2332 c := s.expr(n.List.Second()) 2333 return s.newValue3(ssa.OpSliceMake, n.Type, p, l, c) 2334 2335 case OSLICE, OSLICEARR, OSLICE3, OSLICE3ARR: 2336 v := s.expr(n.Left) 2337 var i, j, k *ssa.Value 2338 low, high, max := n.SliceBounds() 2339 if low != nil { 2340 i = s.extendIndex(s.expr(low), panicslice) 2341 } 2342 if high != nil { 2343 j = s.extendIndex(s.expr(high), panicslice) 2344 } 2345 if max != nil { 2346 k = s.extendIndex(s.expr(max), panicslice) 2347 } 2348 p, l, c := s.slice(n.Left.Type, v, i, j, k, n.Bounded()) 2349 return s.newValue3(ssa.OpSliceMake, n.Type, p, l, c) 2350 2351 case OSLICESTR: 2352 v := s.expr(n.Left) 2353 var i, j *ssa.Value 2354 low, high, _ := n.SliceBounds() 2355 if low != nil { 2356 i = s.extendIndex(s.expr(low), panicslice) 2357 } 2358 if high != nil { 2359 j = s.extendIndex(s.expr(high), panicslice) 2360 } 2361 p, l, _ := s.slice(n.Left.Type, v, i, j, nil, n.Bounded()) 2362 return s.newValue2(ssa.OpStringMake, n.Type, p, l) 2363 2364 case OCALLFUNC: 2365 if isIntrinsicCall(n) { 2366 return s.intrinsicCall(n) 2367 } 2368 fallthrough 2369 2370 case OCALLINTER, OCALLMETH: 2371 a := s.call(n, callNormal) 2372 return s.load(n.Type, a) 2373 2374 case OGETG: 2375 return s.newValue1(ssa.OpGetG, n.Type, s.mem()) 2376 2377 case OAPPEND: 2378 return s.append(n, false) 2379 2380 case OSTRUCTLIT, OARRAYLIT: 2381 // All literals with nonzero fields have already been 2382 // rewritten during walk. Any that remain are just T{} 2383 // or equivalents. Use the zero value. 2384 if !isZero(n) { 2385 Fatalf("literal with nonzero value in SSA: %v", n) 2386 } 2387 return s.zeroVal(n.Type) 2388 2389 default: 2390 s.Fatalf("unhandled expr %v", n.Op) 2391 return nil 2392 } 2393 } 2394 2395 // append converts an OAPPEND node to SSA. 2396 // If inplace is false, it converts the OAPPEND expression n to an ssa.Value, 2397 // adds it to s, and returns the Value. 2398 // If inplace is true, it writes the result of the OAPPEND expression n 2399 // back to the slice being appended to, and returns nil. 2400 // inplace MUST be set to false if the slice can be SSA'd. 2401 func (s *state) append(n *Node, inplace bool) *ssa.Value { 2402 // If inplace is false, process as expression "append(s, e1, e2, e3)": 2403 // 2404 // ptr, len, cap := s 2405 // newlen := len + 3 2406 // if newlen > cap { 2407 // ptr, len, cap = growslice(s, newlen) 2408 // newlen = len + 3 // recalculate to avoid a spill 2409 // } 2410 // // with write barriers, if needed: 2411 // *(ptr+len) = e1 2412 // *(ptr+len+1) = e2 2413 // *(ptr+len+2) = e3 2414 // return makeslice(ptr, newlen, cap) 2415 // 2416 // 2417 // If inplace is true, process as statement "s = append(s, e1, e2, e3)": 2418 // 2419 // a := &s 2420 // ptr, len, cap := s 2421 // newlen := len + 3 2422 // if uint(newlen) > uint(cap) { 2423 // newptr, len, newcap = growslice(ptr, len, cap, newlen) 2424 // vardef(a) // if necessary, advise liveness we are writing a new a 2425 // *a.cap = newcap // write before ptr to avoid a spill 2426 // *a.ptr = newptr // with write barrier 2427 // } 2428 // newlen = len + 3 // recalculate to avoid a spill 2429 // *a.len = newlen 2430 // // with write barriers, if needed: 2431 // *(ptr+len) = e1 2432 // *(ptr+len+1) = e2 2433 // *(ptr+len+2) = e3 2434 2435 et := n.Type.Elem() 2436 pt := types.NewPtr(et) 2437 2438 // Evaluate slice 2439 sn := n.List.First() // the slice node is the first in the list 2440 2441 var slice, addr *ssa.Value 2442 if inplace { 2443 addr = s.addr(sn, false) 2444 slice = s.load(n.Type, addr) 2445 } else { 2446 slice = s.expr(sn) 2447 } 2448 2449 // Allocate new blocks 2450 grow := s.f.NewBlock(ssa.BlockPlain) 2451 assign := s.f.NewBlock(ssa.BlockPlain) 2452 2453 // Decide if we need to grow 2454 nargs := int64(n.List.Len() - 1) 2455 p := s.newValue1(ssa.OpSlicePtr, pt, slice) 2456 l := s.newValue1(ssa.OpSliceLen, types.Types[TINT], slice) 2457 c := s.newValue1(ssa.OpSliceCap, types.Types[TINT], slice) 2458 nl := s.newValue2(s.ssaOp(OADD, types.Types[TINT]), types.Types[TINT], l, s.constInt(types.Types[TINT], nargs)) 2459 2460 cmp := s.newValue2(s.ssaOp(OGT, types.Types[TUINT]), types.Types[TBOOL], nl, c) 2461 s.vars[&ptrVar] = p 2462 2463 if !inplace { 2464 s.vars[&newlenVar] = nl 2465 s.vars[&capVar] = c 2466 } else { 2467 s.vars[&lenVar] = l 2468 } 2469 2470 b := s.endBlock() 2471 b.Kind = ssa.BlockIf 2472 b.Likely = ssa.BranchUnlikely 2473 b.SetControl(cmp) 2474 b.AddEdgeTo(grow) 2475 b.AddEdgeTo(assign) 2476 2477 // Call growslice 2478 s.startBlock(grow) 2479 taddr := s.expr(n.Left) 2480 r := s.rtcall(growslice, true, []*types.Type{pt, types.Types[TINT], types.Types[TINT]}, taddr, p, l, c, nl) 2481 2482 if inplace { 2483 if sn.Op == ONAME && sn.Class() != PEXTERN { 2484 // Tell liveness we're about to build a new slice 2485 s.vars[&memVar] = s.newValue1A(ssa.OpVarDef, types.TypeMem, sn, s.mem()) 2486 } 2487 capaddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, int64(array_cap), addr) 2488 s.store(types.Types[TINT], capaddr, r[2]) 2489 s.store(pt, addr, r[0]) 2490 // load the value we just stored to avoid having to spill it 2491 s.vars[&ptrVar] = s.load(pt, addr) 2492 s.vars[&lenVar] = r[1] // avoid a spill in the fast path 2493 } else { 2494 s.vars[&ptrVar] = r[0] 2495 s.vars[&newlenVar] = s.newValue2(s.ssaOp(OADD, types.Types[TINT]), types.Types[TINT], r[1], s.constInt(types.Types[TINT], nargs)) 2496 s.vars[&capVar] = r[2] 2497 } 2498 2499 b = s.endBlock() 2500 b.AddEdgeTo(assign) 2501 2502 // assign new elements to slots 2503 s.startBlock(assign) 2504 2505 if inplace { 2506 l = s.variable(&lenVar, types.Types[TINT]) // generates phi for len 2507 nl = s.newValue2(s.ssaOp(OADD, types.Types[TINT]), types.Types[TINT], l, s.constInt(types.Types[TINT], nargs)) 2508 lenaddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, int64(array_nel), addr) 2509 s.store(types.Types[TINT], lenaddr, nl) 2510 } 2511 2512 // Evaluate args 2513 type argRec struct { 2514 // if store is true, we're appending the value v. If false, we're appending the 2515 // value at *v. 2516 v *ssa.Value 2517 store bool 2518 } 2519 args := make([]argRec, 0, nargs) 2520 for _, n := range n.List.Slice()[1:] { 2521 if canSSAType(n.Type) { 2522 args = append(args, argRec{v: s.expr(n), store: true}) 2523 } else { 2524 v := s.addr(n, false) 2525 args = append(args, argRec{v: v}) 2526 } 2527 } 2528 2529 p = s.variable(&ptrVar, pt) // generates phi for ptr 2530 if !inplace { 2531 nl = s.variable(&newlenVar, types.Types[TINT]) // generates phi for nl 2532 c = s.variable(&capVar, types.Types[TINT]) // generates phi for cap 2533 } 2534 p2 := s.newValue2(ssa.OpPtrIndex, pt, p, l) 2535 for i, arg := range args { 2536 addr := s.newValue2(ssa.OpPtrIndex, pt, p2, s.constInt(types.Types[TINT], int64(i))) 2537 if arg.store { 2538 s.storeType(et, addr, arg.v, 0, true) 2539 } else { 2540 s.move(et, addr, arg.v) 2541 } 2542 } 2543 2544 delete(s.vars, &ptrVar) 2545 if inplace { 2546 delete(s.vars, &lenVar) 2547 return nil 2548 } 2549 delete(s.vars, &newlenVar) 2550 delete(s.vars, &capVar) 2551 // make result 2552 return s.newValue3(ssa.OpSliceMake, n.Type, p, nl, c) 2553 } 2554 2555 // condBranch evaluates the boolean expression cond and branches to yes 2556 // if cond is true and no if cond is false. 2557 // This function is intended to handle && and || better than just calling 2558 // s.expr(cond) and branching on the result. 2559 func (s *state) condBranch(cond *Node, yes, no *ssa.Block, likely int8) { 2560 switch cond.Op { 2561 case OANDAND: 2562 mid := s.f.NewBlock(ssa.BlockPlain) 2563 s.stmtList(cond.Ninit) 2564 s.condBranch(cond.Left, mid, no, max8(likely, 0)) 2565 s.startBlock(mid) 2566 s.condBranch(cond.Right, yes, no, likely) 2567 return 2568 // Note: if likely==1, then both recursive calls pass 1. 2569 // If likely==-1, then we don't have enough information to decide 2570 // whether the first branch is likely or not. So we pass 0 for 2571 // the likeliness of the first branch. 2572 // TODO: have the frontend give us branch prediction hints for 2573 // OANDAND and OOROR nodes (if it ever has such info). 2574 case OOROR: 2575 mid := s.f.NewBlock(ssa.BlockPlain) 2576 s.stmtList(cond.Ninit) 2577 s.condBranch(cond.Left, yes, mid, min8(likely, 0)) 2578 s.startBlock(mid) 2579 s.condBranch(cond.Right, yes, no, likely) 2580 return 2581 // Note: if likely==-1, then both recursive calls pass -1. 2582 // If likely==1, then we don't have enough info to decide 2583 // the likelihood of the first branch. 2584 case ONOT: 2585 s.stmtList(cond.Ninit) 2586 s.condBranch(cond.Left, no, yes, -likely) 2587 return 2588 } 2589 c := s.expr(cond) 2590 b := s.endBlock() 2591 b.Kind = ssa.BlockIf 2592 b.SetControl(c) 2593 b.Likely = ssa.BranchPrediction(likely) // gc and ssa both use -1/0/+1 for likeliness 2594 b.AddEdgeTo(yes) 2595 b.AddEdgeTo(no) 2596 } 2597 2598 type skipMask uint8 2599 2600 const ( 2601 skipPtr skipMask = 1 << iota 2602 skipLen 2603 skipCap 2604 ) 2605 2606 // assign does left = right. 2607 // Right has already been evaluated to ssa, left has not. 2608 // If deref is true, then we do left = *right instead (and right has already been nil-checked). 2609 // If deref is true and right == nil, just do left = 0. 2610 // skip indicates assignments (at the top level) that can be avoided. 2611 func (s *state) assign(left *Node, right *ssa.Value, deref bool, skip skipMask) { 2612 if left.Op == ONAME && left.isBlank() { 2613 return 2614 } 2615 t := left.Type 2616 dowidth(t) 2617 if s.canSSA(left) { 2618 if deref { 2619 s.Fatalf("can SSA LHS %v but not RHS %s", left, right) 2620 } 2621 if left.Op == ODOT { 2622 // We're assigning to a field of an ssa-able value. 2623 // We need to build a new structure with the new value for the 2624 // field we're assigning and the old values for the other fields. 2625 // For instance: 2626 // type T struct {a, b, c int} 2627 // var T x 2628 // x.b = 5 2629 // For the x.b = 5 assignment we want to generate x = T{x.a, 5, x.c} 2630 2631 // Grab information about the structure type. 2632 t := left.Left.Type 2633 nf := t.NumFields() 2634 idx := fieldIdx(left) 2635 2636 // Grab old value of structure. 2637 old := s.expr(left.Left) 2638 2639 // Make new structure. 2640 new := s.newValue0(ssa.StructMakeOp(t.NumFields()), t) 2641 2642 // Add fields as args. 2643 for i := 0; i < nf; i++ { 2644 if i == idx { 2645 new.AddArg(right) 2646 } else { 2647 new.AddArg(s.newValue1I(ssa.OpStructSelect, t.FieldType(i), int64(i), old)) 2648 } 2649 } 2650 2651 // Recursively assign the new value we've made to the base of the dot op. 2652 s.assign(left.Left, new, false, 0) 2653 // TODO: do we need to update named values here? 2654 return 2655 } 2656 if left.Op == OINDEX && left.Left.Type.IsArray() { 2657 // We're assigning to an element of an ssa-able array. 2658 // a[i] = v 2659 t := left.Left.Type 2660 n := t.NumElem() 2661 2662 i := s.expr(left.Right) // index 2663 if n == 0 { 2664 // The bounds check must fail. Might as well 2665 // ignore the actual index and just use zeros. 2666 z := s.constInt(types.Types[TINT], 0) 2667 s.boundsCheck(z, z) 2668 return 2669 } 2670 if n != 1 { 2671 s.Fatalf("assigning to non-1-length array") 2672 } 2673 // Rewrite to a = [1]{v} 2674 i = s.extendIndex(i, panicindex) 2675 s.boundsCheck(i, s.constInt(types.Types[TINT], 1)) 2676 v := s.newValue1(ssa.OpArrayMake1, t, right) 2677 s.assign(left.Left, v, false, 0) 2678 return 2679 } 2680 // Update variable assignment. 2681 s.vars[left] = right 2682 s.addNamedValue(left, right) 2683 return 2684 } 2685 // Left is not ssa-able. Compute its address. 2686 if left.Op == ONAME && left.Class() != PEXTERN && skip == 0 { 2687 s.vars[&memVar] = s.newValue1Apos(ssa.OpVarDef, types.TypeMem, left, s.mem(), !left.IsAutoTmp()) 2688 } 2689 addr := s.addr(left, false) 2690 if isReflectHeaderDataField(left) { 2691 // Package unsafe's documentation says storing pointers into 2692 // reflect.SliceHeader and reflect.StringHeader's Data fields 2693 // is valid, even though they have type uintptr (#19168). 2694 // Mark it pointer type to signal the writebarrier pass to 2695 // insert a write barrier. 2696 t = types.Types[TUNSAFEPTR] 2697 } 2698 if deref { 2699 // Treat as a mem->mem move. 2700 if right == nil { 2701 s.zero(t, addr) 2702 } else { 2703 s.move(t, addr, right) 2704 } 2705 return 2706 } 2707 // Treat as a store. 2708 s.storeType(t, addr, right, skip, !left.IsAutoTmp()) 2709 } 2710 2711 // zeroVal returns the zero value for type t. 2712 func (s *state) zeroVal(t *types.Type) *ssa.Value { 2713 switch { 2714 case t.IsInteger(): 2715 switch t.Size() { 2716 case 1: 2717 return s.constInt8(t, 0) 2718 case 2: 2719 return s.constInt16(t, 0) 2720 case 4: 2721 return s.constInt32(t, 0) 2722 case 8: 2723 return s.constInt64(t, 0) 2724 default: 2725 s.Fatalf("bad sized integer type %v", t) 2726 } 2727 case t.IsFloat(): 2728 switch t.Size() { 2729 case 4: 2730 return s.constFloat32(t, 0) 2731 case 8: 2732 return s.constFloat64(t, 0) 2733 default: 2734 s.Fatalf("bad sized float type %v", t) 2735 } 2736 case t.IsComplex(): 2737 switch t.Size() { 2738 case 8: 2739 z := s.constFloat32(types.Types[TFLOAT32], 0) 2740 return s.entryNewValue2(ssa.OpComplexMake, t, z, z) 2741 case 16: 2742 z := s.constFloat64(types.Types[TFLOAT64], 0) 2743 return s.entryNewValue2(ssa.OpComplexMake, t, z, z) 2744 default: 2745 s.Fatalf("bad sized complex type %v", t) 2746 } 2747 2748 case t.IsString(): 2749 return s.constEmptyString(t) 2750 case t.IsPtrShaped(): 2751 return s.constNil(t) 2752 case t.IsBoolean(): 2753 return s.constBool(false) 2754 case t.IsInterface(): 2755 return s.constInterface(t) 2756 case t.IsSlice(): 2757 return s.constSlice(t) 2758 case t.IsStruct(): 2759 n := t.NumFields() 2760 v := s.entryNewValue0(ssa.StructMakeOp(t.NumFields()), t) 2761 for i := 0; i < n; i++ { 2762 v.AddArg(s.zeroVal(t.FieldType(i))) 2763 } 2764 return v 2765 case t.IsArray(): 2766 switch t.NumElem() { 2767 case 0: 2768 return s.entryNewValue0(ssa.OpArrayMake0, t) 2769 case 1: 2770 return s.entryNewValue1(ssa.OpArrayMake1, t, s.zeroVal(t.Elem())) 2771 } 2772 } 2773 s.Fatalf("zero for type %v not implemented", t) 2774 return nil 2775 } 2776 2777 type callKind int8 2778 2779 const ( 2780 callNormal callKind = iota 2781 callDefer 2782 callGo 2783 ) 2784 2785 type sfRtCallDef struct { 2786 rtfn *obj.LSym 2787 rtype types.EType 2788 } 2789 2790 var softFloatOps map[ssa.Op]sfRtCallDef 2791 2792 func softfloatInit() { 2793 // Some of these operations get transformed by sfcall. 2794 softFloatOps = map[ssa.Op]sfRtCallDef{ 2795 ssa.OpAdd32F: sfRtCallDef{sysfunc("fadd32"), TFLOAT32}, 2796 ssa.OpAdd64F: sfRtCallDef{sysfunc("fadd64"), TFLOAT64}, 2797 ssa.OpSub32F: sfRtCallDef{sysfunc("fadd32"), TFLOAT32}, 2798 ssa.OpSub64F: sfRtCallDef{sysfunc("fadd64"), TFLOAT64}, 2799 ssa.OpMul32F: sfRtCallDef{sysfunc("fmul32"), TFLOAT32}, 2800 ssa.OpMul64F: sfRtCallDef{sysfunc("fmul64"), TFLOAT64}, 2801 ssa.OpDiv32F: sfRtCallDef{sysfunc("fdiv32"), TFLOAT32}, 2802 ssa.OpDiv64F: sfRtCallDef{sysfunc("fdiv64"), TFLOAT64}, 2803 2804 ssa.OpEq64F: sfRtCallDef{sysfunc("feq64"), TBOOL}, 2805 ssa.OpEq32F: sfRtCallDef{sysfunc("feq32"), TBOOL}, 2806 ssa.OpNeq64F: sfRtCallDef{sysfunc("feq64"), TBOOL}, 2807 ssa.OpNeq32F: sfRtCallDef{sysfunc("feq32"), TBOOL}, 2808 ssa.OpLess64F: sfRtCallDef{sysfunc("fgt64"), TBOOL}, 2809 ssa.OpLess32F: sfRtCallDef{sysfunc("fgt32"), TBOOL}, 2810 ssa.OpGreater64F: sfRtCallDef{sysfunc("fgt64"), TBOOL}, 2811 ssa.OpGreater32F: sfRtCallDef{sysfunc("fgt32"), TBOOL}, 2812 ssa.OpLeq64F: sfRtCallDef{sysfunc("fge64"), TBOOL}, 2813 ssa.OpLeq32F: sfRtCallDef{sysfunc("fge32"), TBOOL}, 2814 ssa.OpGeq64F: sfRtCallDef{sysfunc("fge64"), TBOOL}, 2815 ssa.OpGeq32F: sfRtCallDef{sysfunc("fge32"), TBOOL}, 2816 2817 ssa.OpCvt32to32F: sfRtCallDef{sysfunc("fint32to32"), TFLOAT32}, 2818 ssa.OpCvt32Fto32: sfRtCallDef{sysfunc("f32toint32"), TINT32}, 2819 ssa.OpCvt64to32F: sfRtCallDef{sysfunc("fint64to32"), TFLOAT32}, 2820 ssa.OpCvt32Fto64: sfRtCallDef{sysfunc("f32toint64"), TINT64}, 2821 ssa.OpCvt64Uto32F: sfRtCallDef{sysfunc("fuint64to32"), TFLOAT32}, 2822 ssa.OpCvt32Fto64U: sfRtCallDef{sysfunc("f32touint64"), TUINT64}, 2823 ssa.OpCvt32to64F: sfRtCallDef{sysfunc("fint32to64"), TFLOAT64}, 2824 ssa.OpCvt64Fto32: sfRtCallDef{sysfunc("f64toint32"), TINT32}, 2825 ssa.OpCvt64to64F: sfRtCallDef{sysfunc("fint64to64"), TFLOAT64}, 2826 ssa.OpCvt64Fto64: sfRtCallDef{sysfunc("f64toint64"), TINT64}, 2827 ssa.OpCvt64Uto64F: sfRtCallDef{sysfunc("fuint64to64"), TFLOAT64}, 2828 ssa.OpCvt64Fto64U: sfRtCallDef{sysfunc("f64touint64"), TUINT64}, 2829 ssa.OpCvt32Fto64F: sfRtCallDef{sysfunc("f32to64"), TFLOAT64}, 2830 ssa.OpCvt64Fto32F: sfRtCallDef{sysfunc("f64to32"), TFLOAT32}, 2831 } 2832 } 2833 2834 // TODO: do not emit sfcall if operation can be optimized to constant in later 2835 // opt phase 2836 func (s *state) sfcall(op ssa.Op, args ...*ssa.Value) (*ssa.Value, bool) { 2837 if callDef, ok := softFloatOps[op]; ok { 2838 switch op { 2839 case ssa.OpLess32F, 2840 ssa.OpLess64F, 2841 ssa.OpLeq32F, 2842 ssa.OpLeq64F: 2843 args[0], args[1] = args[1], args[0] 2844 case ssa.OpSub32F, 2845 ssa.OpSub64F: 2846 args[1] = s.newValue1(s.ssaOp(ONEG, types.Types[callDef.rtype]), args[1].Type, args[1]) 2847 } 2848 2849 result := s.rtcall(callDef.rtfn, true, []*types.Type{types.Types[callDef.rtype]}, args...)[0] 2850 if op == ssa.OpNeq32F || op == ssa.OpNeq64F { 2851 result = s.newValue1(ssa.OpNot, result.Type, result) 2852 } 2853 return result, true 2854 } 2855 return nil, false 2856 } 2857 2858 var intrinsics map[intrinsicKey]intrinsicBuilder 2859 2860 // An intrinsicBuilder converts a call node n into an ssa value that 2861 // implements that call as an intrinsic. args is a list of arguments to the func. 2862 type intrinsicBuilder func(s *state, n *Node, args []*ssa.Value) *ssa.Value 2863 2864 type intrinsicKey struct { 2865 arch *sys.Arch 2866 pkg string 2867 fn string 2868 } 2869 2870 func init() { 2871 intrinsics = map[intrinsicKey]intrinsicBuilder{} 2872 2873 var all []*sys.Arch 2874 var p4 []*sys.Arch 2875 var p8 []*sys.Arch 2876 var lwatomics []*sys.Arch 2877 for _, a := range sys.Archs { 2878 all = append(all, a) 2879 if a.PtrSize == 4 { 2880 p4 = append(p4, a) 2881 } else { 2882 p8 = append(p8, a) 2883 } 2884 if a.Family != sys.PPC64 { 2885 lwatomics = append(lwatomics, a) 2886 } 2887 } 2888 2889 // add adds the intrinsic b for pkg.fn for the given list of architectures. 2890 add := func(pkg, fn string, b intrinsicBuilder, archs ...*sys.Arch) { 2891 for _, a := range archs { 2892 intrinsics[intrinsicKey{a, pkg, fn}] = b 2893 } 2894 } 2895 // addF does the same as add but operates on architecture families. 2896 addF := func(pkg, fn string, b intrinsicBuilder, archFamilies ...sys.ArchFamily) { 2897 m := 0 2898 for _, f := range archFamilies { 2899 if f >= 32 { 2900 panic("too many architecture families") 2901 } 2902 m |= 1 << uint(f) 2903 } 2904 for _, a := range all { 2905 if m>>uint(a.Family)&1 != 0 { 2906 intrinsics[intrinsicKey{a, pkg, fn}] = b 2907 } 2908 } 2909 } 2910 // alias defines pkg.fn = pkg2.fn2 for all architectures in archs for which pkg2.fn2 exists. 2911 alias := func(pkg, fn, pkg2, fn2 string, archs ...*sys.Arch) { 2912 for _, a := range archs { 2913 if b, ok := intrinsics[intrinsicKey{a, pkg2, fn2}]; ok { 2914 intrinsics[intrinsicKey{a, pkg, fn}] = b 2915 } 2916 } 2917 } 2918 2919 /******** runtime ********/ 2920 if !instrumenting { 2921 add("runtime", "slicebytetostringtmp", 2922 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 2923 // Compiler frontend optimizations emit OBYTES2STRTMP nodes 2924 // for the backend instead of slicebytetostringtmp calls 2925 // when not instrumenting. 2926 slice := args[0] 2927 ptr := s.newValue1(ssa.OpSlicePtr, s.f.Config.Types.BytePtr, slice) 2928 len := s.newValue1(ssa.OpSliceLen, types.Types[TINT], slice) 2929 return s.newValue2(ssa.OpStringMake, n.Type, ptr, len) 2930 }, 2931 all...) 2932 } 2933 addF("runtime/internal/math", "MulUintptr", 2934 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 2935 if s.config.PtrSize == 4 { 2936 return s.newValue2(ssa.OpMul32uover, types.NewTuple(types.Types[TUINT], types.Types[TUINT]), args[0], args[1]) 2937 } 2938 return s.newValue2(ssa.OpMul64uover, types.NewTuple(types.Types[TUINT], types.Types[TUINT]), args[0], args[1]) 2939 }, 2940 sys.AMD64, sys.I386) 2941 add("runtime", "KeepAlive", 2942 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 2943 data := s.newValue1(ssa.OpIData, s.f.Config.Types.BytePtr, args[0]) 2944 s.vars[&memVar] = s.newValue2(ssa.OpKeepAlive, types.TypeMem, data, s.mem()) 2945 return nil 2946 }, 2947 all...) 2948 add("runtime", "getclosureptr", 2949 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 2950 return s.newValue0(ssa.OpGetClosurePtr, s.f.Config.Types.Uintptr) 2951 }, 2952 all...) 2953 2954 add("runtime", "getcallerpc", 2955 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 2956 return s.newValue0(ssa.OpGetCallerPC, s.f.Config.Types.Uintptr) 2957 }, 2958 all...) 2959 2960 add("runtime", "getcallersp", 2961 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 2962 return s.newValue0(ssa.OpGetCallerSP, s.f.Config.Types.Uintptr) 2963 }, 2964 all...) 2965 2966 /******** runtime/internal/sys ********/ 2967 addF("runtime/internal/sys", "Ctz32", 2968 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 2969 return s.newValue1(ssa.OpCtz32, types.Types[TINT], args[0]) 2970 }, 2971 sys.AMD64, sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64) 2972 addF("runtime/internal/sys", "Ctz64", 2973 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 2974 return s.newValue1(ssa.OpCtz64, types.Types[TINT], args[0]) 2975 }, 2976 sys.AMD64, sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64) 2977 addF("runtime/internal/sys", "Bswap32", 2978 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 2979 return s.newValue1(ssa.OpBswap32, types.Types[TUINT32], args[0]) 2980 }, 2981 sys.AMD64, sys.ARM64, sys.ARM, sys.S390X) 2982 addF("runtime/internal/sys", "Bswap64", 2983 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 2984 return s.newValue1(ssa.OpBswap64, types.Types[TUINT64], args[0]) 2985 }, 2986 sys.AMD64, sys.ARM64, sys.ARM, sys.S390X) 2987 2988 /******** runtime/internal/atomic ********/ 2989 addF("runtime/internal/atomic", "Load", 2990 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 2991 v := s.newValue2(ssa.OpAtomicLoad32, types.NewTuple(types.Types[TUINT32], types.TypeMem), args[0], s.mem()) 2992 s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v) 2993 return s.newValue1(ssa.OpSelect0, types.Types[TUINT32], v) 2994 }, 2995 sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS, sys.MIPS64, sys.PPC64) 2996 addF("runtime/internal/atomic", "Load64", 2997 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 2998 v := s.newValue2(ssa.OpAtomicLoad64, types.NewTuple(types.Types[TUINT64], types.TypeMem), args[0], s.mem()) 2999 s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v) 3000 return s.newValue1(ssa.OpSelect0, types.Types[TUINT64], v) 3001 }, 3002 sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS64, sys.PPC64) 3003 addF("runtime/internal/atomic", "LoadAcq", 3004 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3005 v := s.newValue2(ssa.OpAtomicLoadAcq32, types.NewTuple(types.Types[TUINT32], types.TypeMem), args[0], s.mem()) 3006 s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v) 3007 return s.newValue1(ssa.OpSelect0, types.Types[TUINT32], v) 3008 }, 3009 sys.PPC64) 3010 addF("runtime/internal/atomic", "Loadp", 3011 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3012 v := s.newValue2(ssa.OpAtomicLoadPtr, types.NewTuple(s.f.Config.Types.BytePtr, types.TypeMem), args[0], s.mem()) 3013 s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v) 3014 return s.newValue1(ssa.OpSelect0, s.f.Config.Types.BytePtr, v) 3015 }, 3016 sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS, sys.MIPS64, sys.PPC64) 3017 3018 addF("runtime/internal/atomic", "Store", 3019 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3020 s.vars[&memVar] = s.newValue3(ssa.OpAtomicStore32, types.TypeMem, args[0], args[1], s.mem()) 3021 return nil 3022 }, 3023 sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS, sys.MIPS64, sys.PPC64) 3024 addF("runtime/internal/atomic", "Store64", 3025 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3026 s.vars[&memVar] = s.newValue3(ssa.OpAtomicStore64, types.TypeMem, args[0], args[1], s.mem()) 3027 return nil 3028 }, 3029 sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS64, sys.PPC64) 3030 addF("runtime/internal/atomic", "StorepNoWB", 3031 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3032 s.vars[&memVar] = s.newValue3(ssa.OpAtomicStorePtrNoWB, types.TypeMem, args[0], args[1], s.mem()) 3033 return nil 3034 }, 3035 sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS, sys.MIPS64) 3036 addF("runtime/internal/atomic", "StoreRel", 3037 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3038 s.vars[&memVar] = s.newValue3(ssa.OpAtomicStoreRel32, types.TypeMem, args[0], args[1], s.mem()) 3039 return nil 3040 }, 3041 sys.PPC64) 3042 3043 addF("runtime/internal/atomic", "Xchg", 3044 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3045 v := s.newValue3(ssa.OpAtomicExchange32, types.NewTuple(types.Types[TUINT32], types.TypeMem), args[0], args[1], s.mem()) 3046 s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v) 3047 return s.newValue1(ssa.OpSelect0, types.Types[TUINT32], v) 3048 }, 3049 sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS, sys.MIPS64, sys.PPC64) 3050 addF("runtime/internal/atomic", "Xchg64", 3051 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3052 v := s.newValue3(ssa.OpAtomicExchange64, types.NewTuple(types.Types[TUINT64], types.TypeMem), args[0], args[1], s.mem()) 3053 s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v) 3054 return s.newValue1(ssa.OpSelect0, types.Types[TUINT64], v) 3055 }, 3056 sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS64, sys.PPC64) 3057 3058 addF("runtime/internal/atomic", "Xadd", 3059 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3060 v := s.newValue3(ssa.OpAtomicAdd32, types.NewTuple(types.Types[TUINT32], types.TypeMem), args[0], args[1], s.mem()) 3061 s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v) 3062 return s.newValue1(ssa.OpSelect0, types.Types[TUINT32], v) 3063 }, 3064 sys.AMD64, sys.S390X, sys.MIPS, sys.MIPS64, sys.PPC64) 3065 addF("runtime/internal/atomic", "Xadd64", 3066 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3067 v := s.newValue3(ssa.OpAtomicAdd64, types.NewTuple(types.Types[TUINT64], types.TypeMem), args[0], args[1], s.mem()) 3068 s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v) 3069 return s.newValue1(ssa.OpSelect0, types.Types[TUINT64], v) 3070 }, 3071 sys.AMD64, sys.S390X, sys.MIPS64, sys.PPC64) 3072 3073 makeXaddARM64 := func(op0 ssa.Op, op1 ssa.Op, ty types.EType) func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3074 return func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3075 // Target Atomic feature is identified by dynamic detection 3076 addr := s.entryNewValue1A(ssa.OpAddr, types.Types[TBOOL].PtrTo(), arm64HasATOMICS, s.sb) 3077 v := s.load(types.Types[TBOOL], addr) 3078 b := s.endBlock() 3079 b.Kind = ssa.BlockIf 3080 b.SetControl(v) 3081 bTrue := s.f.NewBlock(ssa.BlockPlain) 3082 bFalse := s.f.NewBlock(ssa.BlockPlain) 3083 bEnd := s.f.NewBlock(ssa.BlockPlain) 3084 b.AddEdgeTo(bTrue) 3085 b.AddEdgeTo(bFalse) 3086 b.Likely = ssa.BranchUnlikely // most machines don't have Atomics nowadays 3087 3088 // We have atomic instructions - use it directly. 3089 s.startBlock(bTrue) 3090 v0 := s.newValue3(op1, types.NewTuple(types.Types[ty], types.TypeMem), args[0], args[1], s.mem()) 3091 s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v0) 3092 s.vars[n] = s.newValue1(ssa.OpSelect0, types.Types[ty], v0) 3093 s.endBlock().AddEdgeTo(bEnd) 3094 3095 // Use original instruction sequence. 3096 s.startBlock(bFalse) 3097 v1 := s.newValue3(op0, types.NewTuple(types.Types[ty], types.TypeMem), args[0], args[1], s.mem()) 3098 s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v1) 3099 s.vars[n] = s.newValue1(ssa.OpSelect0, types.Types[ty], v1) 3100 s.endBlock().AddEdgeTo(bEnd) 3101 3102 // Merge results. 3103 s.startBlock(bEnd) 3104 return s.variable(n, types.Types[ty]) 3105 } 3106 } 3107 3108 addF("runtime/internal/atomic", "Xadd", 3109 makeXaddARM64(ssa.OpAtomicAdd32, ssa.OpAtomicAdd32Variant, TUINT32), 3110 sys.ARM64) 3111 addF("runtime/internal/atomic", "Xadd64", 3112 makeXaddARM64(ssa.OpAtomicAdd64, ssa.OpAtomicAdd64Variant, TUINT64), 3113 sys.ARM64) 3114 3115 addF("runtime/internal/atomic", "Cas", 3116 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3117 v := s.newValue4(ssa.OpAtomicCompareAndSwap32, types.NewTuple(types.Types[TBOOL], types.TypeMem), args[0], args[1], args[2], s.mem()) 3118 s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v) 3119 return s.newValue1(ssa.OpSelect0, types.Types[TBOOL], v) 3120 }, 3121 sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS, sys.MIPS64, sys.PPC64) 3122 addF("runtime/internal/atomic", "Cas64", 3123 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3124 v := s.newValue4(ssa.OpAtomicCompareAndSwap64, types.NewTuple(types.Types[TBOOL], types.TypeMem), args[0], args[1], args[2], s.mem()) 3125 s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v) 3126 return s.newValue1(ssa.OpSelect0, types.Types[TBOOL], v) 3127 }, 3128 sys.AMD64, sys.ARM64, sys.S390X, sys.MIPS64, sys.PPC64) 3129 addF("runtime/internal/atomic", "CasRel", 3130 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3131 v := s.newValue4(ssa.OpAtomicCompareAndSwap32, types.NewTuple(types.Types[TBOOL], types.TypeMem), args[0], args[1], args[2], s.mem()) 3132 s.vars[&memVar] = s.newValue1(ssa.OpSelect1, types.TypeMem, v) 3133 return s.newValue1(ssa.OpSelect0, types.Types[TBOOL], v) 3134 }, 3135 sys.PPC64) 3136 3137 addF("runtime/internal/atomic", "And8", 3138 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3139 s.vars[&memVar] = s.newValue3(ssa.OpAtomicAnd8, types.TypeMem, args[0], args[1], s.mem()) 3140 return nil 3141 }, 3142 sys.AMD64, sys.ARM64, sys.MIPS, sys.PPC64) 3143 addF("runtime/internal/atomic", "Or8", 3144 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3145 s.vars[&memVar] = s.newValue3(ssa.OpAtomicOr8, types.TypeMem, args[0], args[1], s.mem()) 3146 return nil 3147 }, 3148 sys.AMD64, sys.ARM64, sys.MIPS, sys.PPC64) 3149 3150 alias("runtime/internal/atomic", "Loadint64", "runtime/internal/atomic", "Load64", all...) 3151 alias("runtime/internal/atomic", "Xaddint64", "runtime/internal/atomic", "Xadd64", all...) 3152 alias("runtime/internal/atomic", "Loaduint", "runtime/internal/atomic", "Load", p4...) 3153 alias("runtime/internal/atomic", "Loaduint", "runtime/internal/atomic", "Load64", p8...) 3154 alias("runtime/internal/atomic", "Loaduintptr", "runtime/internal/atomic", "Load", p4...) 3155 alias("runtime/internal/atomic", "Loaduintptr", "runtime/internal/atomic", "Load64", p8...) 3156 alias("runtime/internal/atomic", "LoadAcq", "runtime/internal/atomic", "Load", lwatomics...) 3157 alias("runtime/internal/atomic", "Storeuintptr", "runtime/internal/atomic", "Store", p4...) 3158 alias("runtime/internal/atomic", "Storeuintptr", "runtime/internal/atomic", "Store64", p8...) 3159 alias("runtime/internal/atomic", "StoreRel", "runtime/internal/atomic", "Store", lwatomics...) 3160 alias("runtime/internal/atomic", "Xchguintptr", "runtime/internal/atomic", "Xchg", p4...) 3161 alias("runtime/internal/atomic", "Xchguintptr", "runtime/internal/atomic", "Xchg64", p8...) 3162 alias("runtime/internal/atomic", "Xadduintptr", "runtime/internal/atomic", "Xadd", p4...) 3163 alias("runtime/internal/atomic", "Xadduintptr", "runtime/internal/atomic", "Xadd64", p8...) 3164 alias("runtime/internal/atomic", "Casuintptr", "runtime/internal/atomic", "Cas", p4...) 3165 alias("runtime/internal/atomic", "Casuintptr", "runtime/internal/atomic", "Cas64", p8...) 3166 alias("runtime/internal/atomic", "Casp1", "runtime/internal/atomic", "Cas", p4...) 3167 alias("runtime/internal/atomic", "Casp1", "runtime/internal/atomic", "Cas64", p8...) 3168 alias("runtime/internal/atomic", "CasRel", "runtime/internal/atomic", "Cas", lwatomics...) 3169 3170 alias("runtime/internal/sys", "Ctz8", "math/bits", "TrailingZeros8", all...) 3171 3172 /******** math ********/ 3173 addF("math", "Sqrt", 3174 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3175 return s.newValue1(ssa.OpSqrt, types.Types[TFLOAT64], args[0]) 3176 }, 3177 sys.I386, sys.AMD64, sys.ARM, sys.ARM64, sys.MIPS, sys.MIPS64, sys.PPC64, sys.S390X) 3178 addF("math", "Trunc", 3179 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3180 return s.newValue1(ssa.OpTrunc, types.Types[TFLOAT64], args[0]) 3181 }, 3182 sys.ARM64, sys.PPC64, sys.S390X) 3183 addF("math", "Ceil", 3184 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3185 return s.newValue1(ssa.OpCeil, types.Types[TFLOAT64], args[0]) 3186 }, 3187 sys.ARM64, sys.PPC64, sys.S390X) 3188 addF("math", "Floor", 3189 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3190 return s.newValue1(ssa.OpFloor, types.Types[TFLOAT64], args[0]) 3191 }, 3192 sys.ARM64, sys.PPC64, sys.S390X) 3193 addF("math", "Round", 3194 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3195 return s.newValue1(ssa.OpRound, types.Types[TFLOAT64], args[0]) 3196 }, 3197 sys.ARM64, sys.PPC64, sys.S390X) 3198 addF("math", "RoundToEven", 3199 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3200 return s.newValue1(ssa.OpRoundToEven, types.Types[TFLOAT64], args[0]) 3201 }, 3202 sys.ARM64, sys.S390X) 3203 addF("math", "Abs", 3204 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3205 return s.newValue1(ssa.OpAbs, types.Types[TFLOAT64], args[0]) 3206 }, 3207 sys.ARM64, sys.PPC64) 3208 addF("math", "Copysign", 3209 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3210 return s.newValue2(ssa.OpCopysign, types.Types[TFLOAT64], args[0], args[1]) 3211 }, 3212 sys.PPC64) 3213 3214 makeRoundAMD64 := func(op ssa.Op) func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3215 return func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3216 addr := s.entryNewValue1A(ssa.OpAddr, types.Types[TBOOL].PtrTo(), x86HasSSE41, s.sb) 3217 v := s.load(types.Types[TBOOL], addr) 3218 b := s.endBlock() 3219 b.Kind = ssa.BlockIf 3220 b.SetControl(v) 3221 bTrue := s.f.NewBlock(ssa.BlockPlain) 3222 bFalse := s.f.NewBlock(ssa.BlockPlain) 3223 bEnd := s.f.NewBlock(ssa.BlockPlain) 3224 b.AddEdgeTo(bTrue) 3225 b.AddEdgeTo(bFalse) 3226 b.Likely = ssa.BranchLikely // most machines have sse4.1 nowadays 3227 3228 // We have the intrinsic - use it directly. 3229 s.startBlock(bTrue) 3230 s.vars[n] = s.newValue1(op, types.Types[TFLOAT64], args[0]) 3231 s.endBlock().AddEdgeTo(bEnd) 3232 3233 // Call the pure Go version. 3234 s.startBlock(bFalse) 3235 a := s.call(n, callNormal) 3236 s.vars[n] = s.load(types.Types[TFLOAT64], a) 3237 s.endBlock().AddEdgeTo(bEnd) 3238 3239 // Merge results. 3240 s.startBlock(bEnd) 3241 return s.variable(n, types.Types[TFLOAT64]) 3242 } 3243 } 3244 addF("math", "RoundToEven", 3245 makeRoundAMD64(ssa.OpRoundToEven), 3246 sys.AMD64) 3247 addF("math", "Floor", 3248 makeRoundAMD64(ssa.OpFloor), 3249 sys.AMD64) 3250 addF("math", "Ceil", 3251 makeRoundAMD64(ssa.OpCeil), 3252 sys.AMD64) 3253 addF("math", "Trunc", 3254 makeRoundAMD64(ssa.OpTrunc), 3255 sys.AMD64) 3256 3257 /******** math/bits ********/ 3258 addF("math/bits", "TrailingZeros64", 3259 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3260 return s.newValue1(ssa.OpCtz64, types.Types[TINT], args[0]) 3261 }, 3262 sys.AMD64, sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64) 3263 addF("math/bits", "TrailingZeros32", 3264 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3265 return s.newValue1(ssa.OpCtz32, types.Types[TINT], args[0]) 3266 }, 3267 sys.AMD64, sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64) 3268 addF("math/bits", "TrailingZeros16", 3269 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3270 x := s.newValue1(ssa.OpZeroExt16to32, types.Types[TUINT32], args[0]) 3271 c := s.constInt32(types.Types[TUINT32], 1<<16) 3272 y := s.newValue2(ssa.OpOr32, types.Types[TUINT32], x, c) 3273 return s.newValue1(ssa.OpCtz32, types.Types[TINT], y) 3274 }, 3275 sys.ARM, sys.MIPS) 3276 addF("math/bits", "TrailingZeros16", 3277 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3278 return s.newValue1(ssa.OpCtz16, types.Types[TINT], args[0]) 3279 }, 3280 sys.AMD64) 3281 addF("math/bits", "TrailingZeros16", 3282 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3283 x := s.newValue1(ssa.OpZeroExt16to64, types.Types[TUINT64], args[0]) 3284 c := s.constInt64(types.Types[TUINT64], 1<<16) 3285 y := s.newValue2(ssa.OpOr64, types.Types[TUINT64], x, c) 3286 return s.newValue1(ssa.OpCtz64, types.Types[TINT], y) 3287 }, 3288 sys.ARM64, sys.S390X, sys.PPC64) 3289 addF("math/bits", "TrailingZeros8", 3290 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3291 x := s.newValue1(ssa.OpZeroExt8to32, types.Types[TUINT32], args[0]) 3292 c := s.constInt32(types.Types[TUINT32], 1<<8) 3293 y := s.newValue2(ssa.OpOr32, types.Types[TUINT32], x, c) 3294 return s.newValue1(ssa.OpCtz32, types.Types[TINT], y) 3295 }, 3296 sys.ARM, sys.MIPS) 3297 addF("math/bits", "TrailingZeros8", 3298 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3299 return s.newValue1(ssa.OpCtz8, types.Types[TINT], args[0]) 3300 }, 3301 sys.AMD64) 3302 addF("math/bits", "TrailingZeros8", 3303 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3304 x := s.newValue1(ssa.OpZeroExt8to64, types.Types[TUINT64], args[0]) 3305 c := s.constInt64(types.Types[TUINT64], 1<<8) 3306 y := s.newValue2(ssa.OpOr64, types.Types[TUINT64], x, c) 3307 return s.newValue1(ssa.OpCtz64, types.Types[TINT], y) 3308 }, 3309 sys.ARM64, sys.S390X) 3310 alias("math/bits", "ReverseBytes64", "runtime/internal/sys", "Bswap64", all...) 3311 alias("math/bits", "ReverseBytes32", "runtime/internal/sys", "Bswap32", all...) 3312 // ReverseBytes inlines correctly, no need to intrinsify it. 3313 // ReverseBytes16 lowers to a rotate, no need for anything special here. 3314 addF("math/bits", "Len64", 3315 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3316 return s.newValue1(ssa.OpBitLen64, types.Types[TINT], args[0]) 3317 }, 3318 sys.AMD64, sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64) 3319 addF("math/bits", "Len32", 3320 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3321 return s.newValue1(ssa.OpBitLen32, types.Types[TINT], args[0]) 3322 }, 3323 sys.AMD64) 3324 addF("math/bits", "Len32", 3325 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3326 if s.config.PtrSize == 4 { 3327 return s.newValue1(ssa.OpBitLen32, types.Types[TINT], args[0]) 3328 } 3329 x := s.newValue1(ssa.OpZeroExt32to64, types.Types[TUINT64], args[0]) 3330 return s.newValue1(ssa.OpBitLen64, types.Types[TINT], x) 3331 }, 3332 sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64) 3333 addF("math/bits", "Len16", 3334 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3335 if s.config.PtrSize == 4 { 3336 x := s.newValue1(ssa.OpZeroExt16to32, types.Types[TUINT32], args[0]) 3337 return s.newValue1(ssa.OpBitLen32, types.Types[TINT], x) 3338 } 3339 x := s.newValue1(ssa.OpZeroExt16to64, types.Types[TUINT64], args[0]) 3340 return s.newValue1(ssa.OpBitLen64, types.Types[TINT], x) 3341 }, 3342 sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64) 3343 addF("math/bits", "Len16", 3344 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3345 return s.newValue1(ssa.OpBitLen16, types.Types[TINT], args[0]) 3346 }, 3347 sys.AMD64) 3348 addF("math/bits", "Len8", 3349 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3350 if s.config.PtrSize == 4 { 3351 x := s.newValue1(ssa.OpZeroExt8to32, types.Types[TUINT32], args[0]) 3352 return s.newValue1(ssa.OpBitLen32, types.Types[TINT], x) 3353 } 3354 x := s.newValue1(ssa.OpZeroExt8to64, types.Types[TUINT64], args[0]) 3355 return s.newValue1(ssa.OpBitLen64, types.Types[TINT], x) 3356 }, 3357 sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64) 3358 addF("math/bits", "Len8", 3359 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3360 return s.newValue1(ssa.OpBitLen8, types.Types[TINT], args[0]) 3361 }, 3362 sys.AMD64) 3363 addF("math/bits", "Len", 3364 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3365 if s.config.PtrSize == 4 { 3366 return s.newValue1(ssa.OpBitLen32, types.Types[TINT], args[0]) 3367 } 3368 return s.newValue1(ssa.OpBitLen64, types.Types[TINT], args[0]) 3369 }, 3370 sys.AMD64, sys.ARM64, sys.ARM, sys.S390X, sys.MIPS, sys.PPC64) 3371 // LeadingZeros is handled because it trivially calls Len. 3372 addF("math/bits", "Reverse64", 3373 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3374 return s.newValue1(ssa.OpBitRev64, types.Types[TINT], args[0]) 3375 }, 3376 sys.ARM64) 3377 addF("math/bits", "Reverse32", 3378 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3379 return s.newValue1(ssa.OpBitRev32, types.Types[TINT], args[0]) 3380 }, 3381 sys.ARM64) 3382 addF("math/bits", "Reverse16", 3383 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3384 return s.newValue1(ssa.OpBitRev16, types.Types[TINT], args[0]) 3385 }, 3386 sys.ARM64) 3387 addF("math/bits", "Reverse8", 3388 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3389 return s.newValue1(ssa.OpBitRev8, types.Types[TINT], args[0]) 3390 }, 3391 sys.ARM64) 3392 addF("math/bits", "Reverse", 3393 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3394 if s.config.PtrSize == 4 { 3395 return s.newValue1(ssa.OpBitRev32, types.Types[TINT], args[0]) 3396 } 3397 return s.newValue1(ssa.OpBitRev64, types.Types[TINT], args[0]) 3398 }, 3399 sys.ARM64) 3400 addF("math/bits", "RotateLeft8", 3401 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3402 return s.newValue2(ssa.OpRotateLeft8, types.Types[TUINT8], args[0], args[1]) 3403 }, 3404 sys.AMD64) 3405 addF("math/bits", "RotateLeft16", 3406 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3407 return s.newValue2(ssa.OpRotateLeft16, types.Types[TUINT16], args[0], args[1]) 3408 }, 3409 sys.AMD64) 3410 addF("math/bits", "RotateLeft32", 3411 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3412 return s.newValue2(ssa.OpRotateLeft32, types.Types[TUINT32], args[0], args[1]) 3413 }, 3414 sys.AMD64, sys.ARM64, sys.S390X) 3415 addF("math/bits", "RotateLeft64", 3416 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3417 return s.newValue2(ssa.OpRotateLeft64, types.Types[TUINT64], args[0], args[1]) 3418 }, 3419 sys.AMD64, sys.ARM64, sys.S390X) 3420 alias("math/bits", "RotateLeft", "math/bits", "RotateLeft64", p8...) 3421 3422 makeOnesCountAMD64 := func(op64 ssa.Op, op32 ssa.Op) func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3423 return func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3424 addr := s.entryNewValue1A(ssa.OpAddr, types.Types[TBOOL].PtrTo(), x86HasPOPCNT, s.sb) 3425 v := s.load(types.Types[TBOOL], addr) 3426 b := s.endBlock() 3427 b.Kind = ssa.BlockIf 3428 b.SetControl(v) 3429 bTrue := s.f.NewBlock(ssa.BlockPlain) 3430 bFalse := s.f.NewBlock(ssa.BlockPlain) 3431 bEnd := s.f.NewBlock(ssa.BlockPlain) 3432 b.AddEdgeTo(bTrue) 3433 b.AddEdgeTo(bFalse) 3434 b.Likely = ssa.BranchLikely // most machines have popcnt nowadays 3435 3436 // We have the intrinsic - use it directly. 3437 s.startBlock(bTrue) 3438 op := op64 3439 if s.config.PtrSize == 4 { 3440 op = op32 3441 } 3442 s.vars[n] = s.newValue1(op, types.Types[TINT], args[0]) 3443 s.endBlock().AddEdgeTo(bEnd) 3444 3445 // Call the pure Go version. 3446 s.startBlock(bFalse) 3447 a := s.call(n, callNormal) 3448 s.vars[n] = s.load(types.Types[TINT], a) 3449 s.endBlock().AddEdgeTo(bEnd) 3450 3451 // Merge results. 3452 s.startBlock(bEnd) 3453 return s.variable(n, types.Types[TINT]) 3454 } 3455 } 3456 addF("math/bits", "OnesCount64", 3457 makeOnesCountAMD64(ssa.OpPopCount64, ssa.OpPopCount64), 3458 sys.AMD64) 3459 addF("math/bits", "OnesCount64", 3460 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3461 return s.newValue1(ssa.OpPopCount64, types.Types[TINT], args[0]) 3462 }, 3463 sys.PPC64, sys.ARM64, sys.S390X) 3464 addF("math/bits", "OnesCount32", 3465 makeOnesCountAMD64(ssa.OpPopCount32, ssa.OpPopCount32), 3466 sys.AMD64) 3467 addF("math/bits", "OnesCount32", 3468 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3469 return s.newValue1(ssa.OpPopCount32, types.Types[TINT], args[0]) 3470 }, 3471 sys.PPC64, sys.ARM64, sys.S390X) 3472 addF("math/bits", "OnesCount16", 3473 makeOnesCountAMD64(ssa.OpPopCount16, ssa.OpPopCount16), 3474 sys.AMD64) 3475 addF("math/bits", "OnesCount16", 3476 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3477 return s.newValue1(ssa.OpPopCount16, types.Types[TINT], args[0]) 3478 }, 3479 sys.ARM64, sys.S390X, sys.PPC64) 3480 addF("math/bits", "OnesCount8", 3481 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3482 return s.newValue1(ssa.OpPopCount8, types.Types[TINT], args[0]) 3483 }, 3484 sys.S390X, sys.PPC64) 3485 addF("math/bits", "OnesCount", 3486 makeOnesCountAMD64(ssa.OpPopCount64, ssa.OpPopCount32), 3487 sys.AMD64) 3488 addF("math/bits", "Mul64", 3489 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3490 return s.newValue2(ssa.OpMul64uhilo, types.NewTuple(types.Types[TUINT64], types.Types[TUINT64]), args[0], args[1]) 3491 }, 3492 sys.AMD64, sys.ARM64, sys.PPC64) 3493 alias("math/bits", "Mul", "math/bits", "Mul64", sys.ArchAMD64, sys.ArchARM64, sys.ArchPPC64) 3494 addF("math/bits", "Add64", 3495 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3496 return s.newValue3(ssa.OpAdd64carry, types.NewTuple(types.Types[TUINT64], types.Types[TUINT64]), args[0], args[1], args[2]) 3497 }, 3498 sys.AMD64) 3499 alias("math/bits", "Add", "math/bits", "Add64", sys.ArchAMD64) 3500 addF("math/bits", "Sub64", 3501 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3502 return s.newValue3(ssa.OpSub64borrow, types.NewTuple(types.Types[TUINT64], types.Types[TUINT64]), args[0], args[1], args[2]) 3503 }, 3504 sys.AMD64) 3505 alias("math/bits", "Sub", "math/bits", "Sub64", sys.ArchAMD64) 3506 addF("math/bits", "Div64", 3507 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3508 // check for divide-by-zero/overflow and panic with appropriate message 3509 cmpZero := s.newValue2(s.ssaOp(ONE, types.Types[TUINT64]), types.Types[TBOOL], args[2], s.zeroVal(types.Types[TUINT64])) 3510 s.check(cmpZero, panicdivide) 3511 cmpOverflow := s.newValue2(s.ssaOp(OLT, types.Types[TUINT64]), types.Types[TBOOL], args[0], args[2]) 3512 s.check(cmpOverflow, panicoverflow) 3513 return s.newValue3(ssa.OpDiv128u, types.NewTuple(types.Types[TUINT64], types.Types[TUINT64]), args[0], args[1], args[2]) 3514 }, 3515 sys.AMD64) 3516 alias("math/bits", "Div", "math/bits", "Div64", sys.ArchAMD64) 3517 3518 /******** sync/atomic ********/ 3519 3520 // Note: these are disabled by flag_race in findIntrinsic below. 3521 alias("sync/atomic", "LoadInt32", "runtime/internal/atomic", "Load", all...) 3522 alias("sync/atomic", "LoadInt64", "runtime/internal/atomic", "Load64", all...) 3523 alias("sync/atomic", "LoadPointer", "runtime/internal/atomic", "Loadp", all...) 3524 alias("sync/atomic", "LoadUint32", "runtime/internal/atomic", "Load", all...) 3525 alias("sync/atomic", "LoadUint64", "runtime/internal/atomic", "Load64", all...) 3526 alias("sync/atomic", "LoadUintptr", "runtime/internal/atomic", "Load", p4...) 3527 alias("sync/atomic", "LoadUintptr", "runtime/internal/atomic", "Load64", p8...) 3528 3529 alias("sync/atomic", "StoreInt32", "runtime/internal/atomic", "Store", all...) 3530 alias("sync/atomic", "StoreInt64", "runtime/internal/atomic", "Store64", all...) 3531 // Note: not StorePointer, that needs a write barrier. Same below for {CompareAnd}Swap. 3532 alias("sync/atomic", "StoreUint32", "runtime/internal/atomic", "Store", all...) 3533 alias("sync/atomic", "StoreUint64", "runtime/internal/atomic", "Store64", all...) 3534 alias("sync/atomic", "StoreUintptr", "runtime/internal/atomic", "Store", p4...) 3535 alias("sync/atomic", "StoreUintptr", "runtime/internal/atomic", "Store64", p8...) 3536 3537 alias("sync/atomic", "SwapInt32", "runtime/internal/atomic", "Xchg", all...) 3538 alias("sync/atomic", "SwapInt64", "runtime/internal/atomic", "Xchg64", all...) 3539 alias("sync/atomic", "SwapUint32", "runtime/internal/atomic", "Xchg", all...) 3540 alias("sync/atomic", "SwapUint64", "runtime/internal/atomic", "Xchg64", all...) 3541 alias("sync/atomic", "SwapUintptr", "runtime/internal/atomic", "Xchg", p4...) 3542 alias("sync/atomic", "SwapUintptr", "runtime/internal/atomic", "Xchg64", p8...) 3543 3544 alias("sync/atomic", "CompareAndSwapInt32", "runtime/internal/atomic", "Cas", all...) 3545 alias("sync/atomic", "CompareAndSwapInt64", "runtime/internal/atomic", "Cas64", all...) 3546 alias("sync/atomic", "CompareAndSwapUint32", "runtime/internal/atomic", "Cas", all...) 3547 alias("sync/atomic", "CompareAndSwapUint64", "runtime/internal/atomic", "Cas64", all...) 3548 alias("sync/atomic", "CompareAndSwapUintptr", "runtime/internal/atomic", "Cas", p4...) 3549 alias("sync/atomic", "CompareAndSwapUintptr", "runtime/internal/atomic", "Cas64", p8...) 3550 3551 alias("sync/atomic", "AddInt32", "runtime/internal/atomic", "Xadd", all...) 3552 alias("sync/atomic", "AddInt64", "runtime/internal/atomic", "Xadd64", all...) 3553 alias("sync/atomic", "AddUint32", "runtime/internal/atomic", "Xadd", all...) 3554 alias("sync/atomic", "AddUint64", "runtime/internal/atomic", "Xadd64", all...) 3555 alias("sync/atomic", "AddUintptr", "runtime/internal/atomic", "Xadd", p4...) 3556 alias("sync/atomic", "AddUintptr", "runtime/internal/atomic", "Xadd64", p8...) 3557 3558 /******** math/big ********/ 3559 add("math/big", "mulWW", 3560 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3561 return s.newValue2(ssa.OpMul64uhilo, types.NewTuple(types.Types[TUINT64], types.Types[TUINT64]), args[0], args[1]) 3562 }, 3563 sys.ArchAMD64, sys.ArchARM64, sys.ArchPPC64LE, sys.ArchPPC64) 3564 add("math/big", "divWW", 3565 func(s *state, n *Node, args []*ssa.Value) *ssa.Value { 3566 return s.newValue3(ssa.OpDiv128u, types.NewTuple(types.Types[TUINT64], types.Types[TUINT64]), args[0], args[1], args[2]) 3567 }, 3568 sys.ArchAMD64) 3569 } 3570 3571 // findIntrinsic returns a function which builds the SSA equivalent of the 3572 // function identified by the symbol sym. If sym is not an intrinsic call, returns nil. 3573 func findIntrinsic(sym *types.Sym) intrinsicBuilder { 3574 if ssa.IntrinsicsDisable { 3575 return nil 3576 } 3577 if sym == nil || sym.Pkg == nil { 3578 return nil 3579 } 3580 pkg := sym.Pkg.Path 3581 if sym.Pkg == localpkg { 3582 pkg = myimportpath 3583 } 3584 if flag_race && pkg == "sync/atomic" { 3585 // The race detector needs to be able to intercept these calls. 3586 // We can't intrinsify them. 3587 return nil 3588 } 3589 // Skip intrinsifying math functions (which may contain hard-float 3590 // instructions) when soft-float 3591 if thearch.SoftFloat && pkg == "math" { 3592 return nil 3593 } 3594 3595 fn := sym.Name 3596 return intrinsics[intrinsicKey{thearch.LinkArch.Arch, pkg, fn}] 3597 } 3598 3599 func isIntrinsicCall(n *Node) bool { 3600 if n == nil || n.Left == nil { 3601 return false 3602 } 3603 return findIntrinsic(n.Left.Sym) != nil 3604 } 3605 3606 // intrinsicCall converts a call to a recognized intrinsic function into the intrinsic SSA operation. 3607 func (s *state) intrinsicCall(n *Node) *ssa.Value { 3608 v := findIntrinsic(n.Left.Sym)(s, n, s.intrinsicArgs(n)) 3609 if ssa.IntrinsicsDebug > 0 { 3610 x := v 3611 if x == nil { 3612 x = s.mem() 3613 } 3614 if x.Op == ssa.OpSelect0 || x.Op == ssa.OpSelect1 { 3615 x = x.Args[0] 3616 } 3617 Warnl(n.Pos, "intrinsic substitution for %v with %s", n.Left.Sym.Name, x.LongString()) 3618 } 3619 return v 3620 } 3621 3622 // intrinsicArgs extracts args from n, evaluates them to SSA values, and returns them. 3623 func (s *state) intrinsicArgs(n *Node) []*ssa.Value { 3624 // Construct map of temps; see comments in s.call about the structure of n. 3625 temps := map[*Node]*ssa.Value{} 3626 for _, a := range n.List.Slice() { 3627 if a.Op != OAS { 3628 s.Fatalf("non-assignment as a temp function argument %v", a.Op) 3629 } 3630 l, r := a.Left, a.Right 3631 if l.Op != ONAME { 3632 s.Fatalf("non-ONAME temp function argument %v", a.Op) 3633 } 3634 // Evaluate and store to "temporary". 3635 // Walk ensures these temporaries are dead outside of n. 3636 temps[l] = s.expr(r) 3637 } 3638 args := make([]*ssa.Value, n.Rlist.Len()) 3639 for i, n := range n.Rlist.Slice() { 3640 // Store a value to an argument slot. 3641 if x, ok := temps[n]; ok { 3642 // This is a previously computed temporary. 3643 args[i] = x 3644 continue 3645 } 3646 // This is an explicit value; evaluate it. 3647 args[i] = s.expr(n) 3648 } 3649 return args 3650 } 3651 3652 // Calls the function n using the specified call type. 3653 // Returns the address of the return value (or nil if none). 3654 func (s *state) call(n *Node, k callKind) *ssa.Value { 3655 var sym *types.Sym // target symbol (if static) 3656 var closure *ssa.Value // ptr to closure to run (if dynamic) 3657 var codeptr *ssa.Value // ptr to target code (if dynamic) 3658 var rcvr *ssa.Value // receiver to set 3659 fn := n.Left 3660 switch n.Op { 3661 case OCALLFUNC: 3662 if k == callNormal && fn.Op == ONAME && fn.Class() == PFUNC { 3663 sym = fn.Sym 3664 break 3665 } 3666 closure = s.expr(fn) 3667 if thearch.LinkArch.Family == sys.Wasm || objabi.GOOS == "aix" && k != callGo { 3668 // On AIX, the closure needs to be verified as fn can be nil, except if it's a call go. This needs to be handled by the runtime to have the "go of nil func value" error. 3669 // TODO(neelance): On other architectures this should be eliminated by the optimization steps 3670 s.nilCheck(closure) 3671 } 3672 case OCALLMETH: 3673 if fn.Op != ODOTMETH { 3674 Fatalf("OCALLMETH: n.Left not an ODOTMETH: %v", fn) 3675 } 3676 if k == callNormal { 3677 sym = fn.Sym 3678 break 3679 } 3680 // Make a name n2 for the function. 3681 // fn.Sym might be sync.(*Mutex).Unlock. 3682 // Make a PFUNC node out of that, then evaluate it. 3683 // We get back an SSA value representing &sync.(*Mutex).Unlock·f. 3684 // We can then pass that to defer or go. 3685 n2 := newnamel(fn.Pos, fn.Sym) 3686 n2.Name.Curfn = s.curfn 3687 n2.SetClass(PFUNC) 3688 // n2.Sym already existed, so it's already marked as a function. 3689 n2.Pos = fn.Pos 3690 n2.Type = types.Types[TUINT8] // dummy type for a static closure. Could use runtime.funcval if we had it. 3691 closure = s.expr(n2) 3692 // Note: receiver is already present in n.Rlist, so we don't 3693 // want to set it here. 3694 case OCALLINTER: 3695 if fn.Op != ODOTINTER { 3696 Fatalf("OCALLINTER: n.Left not an ODOTINTER: %v", fn.Op) 3697 } 3698 i := s.expr(fn.Left) 3699 itab := s.newValue1(ssa.OpITab, types.Types[TUINTPTR], i) 3700 s.nilCheck(itab) 3701 itabidx := fn.Xoffset + 2*int64(Widthptr) + 8 // offset of fun field in runtime.itab 3702 itab = s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.UintptrPtr, itabidx, itab) 3703 if k == callNormal { 3704 codeptr = s.load(types.Types[TUINTPTR], itab) 3705 } else { 3706 closure = itab 3707 } 3708 rcvr = s.newValue1(ssa.OpIData, types.Types[TUINTPTR], i) 3709 } 3710 dowidth(fn.Type) 3711 stksize := fn.Type.ArgWidth() // includes receiver 3712 3713 // Run all assignments of temps. 3714 // The temps are introduced to avoid overwriting argument 3715 // slots when arguments themselves require function calls. 3716 s.stmtList(n.List) 3717 3718 // Store arguments to stack, including defer/go arguments and receiver for method calls. 3719 // These are written in SP-offset order. 3720 argStart := Ctxt.FixedFrameSize() 3721 // Defer/go args. 3722 if k != callNormal { 3723 // Write argsize and closure (args to newproc/deferproc). 3724 argsize := s.constInt32(types.Types[TUINT32], int32(stksize)) 3725 addr := s.constOffPtrSP(s.f.Config.Types.UInt32Ptr, argStart) 3726 s.store(types.Types[TUINT32], addr, argsize) 3727 addr = s.constOffPtrSP(s.f.Config.Types.UintptrPtr, argStart+int64(Widthptr)) 3728 s.store(types.Types[TUINTPTR], addr, closure) 3729 stksize += 2 * int64(Widthptr) 3730 argStart += 2 * int64(Widthptr) 3731 } 3732 3733 // Set receiver (for interface calls). 3734 if rcvr != nil { 3735 addr := s.constOffPtrSP(s.f.Config.Types.UintptrPtr, argStart) 3736 s.store(types.Types[TUINTPTR], addr, rcvr) 3737 } 3738 3739 // Write args. 3740 t := n.Left.Type 3741 args := n.Rlist.Slice() 3742 if n.Op == OCALLMETH { 3743 f := t.Recv() 3744 s.storeArg(args[0], f.Type, argStart+f.Offset) 3745 args = args[1:] 3746 } 3747 for i, n := range args { 3748 f := t.Params().Field(i) 3749 s.storeArg(n, f.Type, argStart+f.Offset) 3750 } 3751 3752 // call target 3753 var call *ssa.Value 3754 switch { 3755 case k == callDefer: 3756 call = s.newValue1A(ssa.OpStaticCall, types.TypeMem, deferproc, s.mem()) 3757 case k == callGo: 3758 call = s.newValue1A(ssa.OpStaticCall, types.TypeMem, newproc, s.mem()) 3759 case closure != nil: 3760 // rawLoad because loading the code pointer from a 3761 // closure is always safe, but IsSanitizerSafeAddr 3762 // can't always figure that out currently, and it's 3763 // critical that we not clobber any arguments already 3764 // stored onto the stack. 3765 codeptr = s.rawLoad(types.Types[TUINTPTR], closure) 3766 call = s.newValue3(ssa.OpClosureCall, types.TypeMem, codeptr, closure, s.mem()) 3767 case codeptr != nil: 3768 call = s.newValue2(ssa.OpInterCall, types.TypeMem, codeptr, s.mem()) 3769 case sym != nil: 3770 call = s.newValue1A(ssa.OpStaticCall, types.TypeMem, sym.Linksym(), s.mem()) 3771 default: 3772 Fatalf("bad call type %v %v", n.Op, n) 3773 } 3774 call.AuxInt = stksize // Call operations carry the argsize of the callee along with them 3775 s.vars[&memVar] = call 3776 3777 // Finish block for defers 3778 if k == callDefer { 3779 b := s.endBlock() 3780 b.Kind = ssa.BlockDefer 3781 b.SetControl(call) 3782 bNext := s.f.NewBlock(ssa.BlockPlain) 3783 b.AddEdgeTo(bNext) 3784 // Add recover edge to exit code. 3785 r := s.f.NewBlock(ssa.BlockPlain) 3786 s.startBlock(r) 3787 s.exit() 3788 b.AddEdgeTo(r) 3789 b.Likely = ssa.BranchLikely 3790 s.startBlock(bNext) 3791 } 3792 3793 res := n.Left.Type.Results() 3794 if res.NumFields() == 0 || k != callNormal { 3795 // call has no return value. Continue with the next statement. 3796 return nil 3797 } 3798 fp := res.Field(0) 3799 return s.constOffPtrSP(types.NewPtr(fp.Type), fp.Offset+Ctxt.FixedFrameSize()) 3800 } 3801 3802 // etypesign returns the signed-ness of e, for integer/pointer etypes. 3803 // -1 means signed, +1 means unsigned, 0 means non-integer/non-pointer. 3804 func etypesign(e types.EType) int8 { 3805 switch e { 3806 case TINT8, TINT16, TINT32, TINT64, TINT: 3807 return -1 3808 case TUINT8, TUINT16, TUINT32, TUINT64, TUINT, TUINTPTR, TUNSAFEPTR: 3809 return +1 3810 } 3811 return 0 3812 } 3813 3814 // addr converts the address of the expression n to SSA, adds it to s and returns the SSA result. 3815 // The value that the returned Value represents is guaranteed to be non-nil. 3816 // If bounded is true then this address does not require a nil check for its operand 3817 // even if that would otherwise be implied. 3818 func (s *state) addr(n *Node, bounded bool) *ssa.Value { 3819 t := types.NewPtr(n.Type) 3820 switch n.Op { 3821 case ONAME: 3822 switch n.Class() { 3823 case PEXTERN: 3824 // global variable 3825 v := s.entryNewValue1A(ssa.OpAddr, t, n.Sym.Linksym(), s.sb) 3826 // TODO: Make OpAddr use AuxInt as well as Aux. 3827 if n.Xoffset != 0 { 3828 v = s.entryNewValue1I(ssa.OpOffPtr, v.Type, n.Xoffset, v) 3829 } 3830 return v 3831 case PPARAM: 3832 // parameter slot 3833 v := s.decladdrs[n] 3834 if v != nil { 3835 return v 3836 } 3837 if n == nodfp { 3838 // Special arg that points to the frame pointer (Used by ORECOVER). 3839 return s.entryNewValue2A(ssa.OpLocalAddr, t, n, s.sp, s.startmem) 3840 } 3841 s.Fatalf("addr of undeclared ONAME %v. declared: %v", n, s.decladdrs) 3842 return nil 3843 case PAUTO: 3844 return s.newValue2Apos(ssa.OpLocalAddr, t, n, s.sp, s.mem(), !n.IsAutoTmp()) 3845 3846 case PPARAMOUT: // Same as PAUTO -- cannot generate LEA early. 3847 // ensure that we reuse symbols for out parameters so 3848 // that cse works on their addresses 3849 return s.newValue2Apos(ssa.OpLocalAddr, t, n, s.sp, s.mem(), true) 3850 default: 3851 s.Fatalf("variable address class %v not implemented", n.Class()) 3852 return nil 3853 } 3854 case OINDREGSP: 3855 // indirect off REGSP 3856 // used for storing/loading arguments/returns to/from callees 3857 return s.constOffPtrSP(t, n.Xoffset) 3858 case OINDEX: 3859 if n.Left.Type.IsSlice() { 3860 a := s.expr(n.Left) 3861 i := s.expr(n.Right) 3862 i = s.extendIndex(i, panicindex) 3863 len := s.newValue1(ssa.OpSliceLen, types.Types[TINT], a) 3864 if !n.Bounded() { 3865 s.boundsCheck(i, len) 3866 } 3867 p := s.newValue1(ssa.OpSlicePtr, t, a) 3868 return s.newValue2(ssa.OpPtrIndex, t, p, i) 3869 } else { // array 3870 a := s.addr(n.Left, bounded) 3871 i := s.expr(n.Right) 3872 i = s.extendIndex(i, panicindex) 3873 len := s.constInt(types.Types[TINT], n.Left.Type.NumElem()) 3874 if !n.Bounded() { 3875 s.boundsCheck(i, len) 3876 } 3877 return s.newValue2(ssa.OpPtrIndex, types.NewPtr(n.Left.Type.Elem()), a, i) 3878 } 3879 case ODEREF: 3880 return s.exprPtr(n.Left, bounded, n.Pos) 3881 case ODOT: 3882 p := s.addr(n.Left, bounded) 3883 return s.newValue1I(ssa.OpOffPtr, t, n.Xoffset, p) 3884 case ODOTPTR: 3885 p := s.exprPtr(n.Left, bounded, n.Pos) 3886 return s.newValue1I(ssa.OpOffPtr, t, n.Xoffset, p) 3887 case OCLOSUREVAR: 3888 return s.newValue1I(ssa.OpOffPtr, t, n.Xoffset, 3889 s.entryNewValue0(ssa.OpGetClosurePtr, s.f.Config.Types.BytePtr)) 3890 case OCONVNOP: 3891 addr := s.addr(n.Left, bounded) 3892 return s.newValue1(ssa.OpCopy, t, addr) // ensure that addr has the right type 3893 case OCALLFUNC, OCALLINTER, OCALLMETH: 3894 return s.call(n, callNormal) 3895 case ODOTTYPE: 3896 v, _ := s.dottype(n, false) 3897 if v.Op != ssa.OpLoad { 3898 s.Fatalf("dottype of non-load") 3899 } 3900 if v.Args[1] != s.mem() { 3901 s.Fatalf("memory no longer live from dottype load") 3902 } 3903 return v.Args[0] 3904 default: 3905 s.Fatalf("unhandled addr %v", n.Op) 3906 return nil 3907 } 3908 } 3909 3910 // canSSA reports whether n is SSA-able. 3911 // n must be an ONAME (or an ODOT sequence with an ONAME base). 3912 func (s *state) canSSA(n *Node) bool { 3913 if Debug['N'] != 0 { 3914 return false 3915 } 3916 for n.Op == ODOT || (n.Op == OINDEX && n.Left.Type.IsArray()) { 3917 n = n.Left 3918 } 3919 if n.Op != ONAME { 3920 return false 3921 } 3922 if n.Addrtaken() { 3923 return false 3924 } 3925 if n.isParamHeapCopy() { 3926 return false 3927 } 3928 if n.Class() == PAUTOHEAP { 3929 Fatalf("canSSA of PAUTOHEAP %v", n) 3930 } 3931 switch n.Class() { 3932 case PEXTERN: 3933 return false 3934 case PPARAMOUT: 3935 if s.hasdefer { 3936 // TODO: handle this case? Named return values must be 3937 // in memory so that the deferred function can see them. 3938 // Maybe do: if !strings.HasPrefix(n.String(), "~") { return false } 3939 // Or maybe not, see issue 18860. Even unnamed return values 3940 // must be written back so if a defer recovers, the caller can see them. 3941 return false 3942 } 3943 if s.cgoUnsafeArgs { 3944 // Cgo effectively takes the address of all result args, 3945 // but the compiler can't see that. 3946 return false 3947 } 3948 } 3949 if n.Class() == PPARAM && n.Sym != nil && n.Sym.Name == ".this" { 3950 // wrappers generated by genwrapper need to update 3951 // the .this pointer in place. 3952 // TODO: treat as a PPARMOUT? 3953 return false 3954 } 3955 return canSSAType(n.Type) 3956 // TODO: try to make more variables SSAable? 3957 } 3958 3959 // canSSA reports whether variables of type t are SSA-able. 3960 func canSSAType(t *types.Type) bool { 3961 dowidth(t) 3962 if t.Width > int64(4*Widthptr) { 3963 // 4*Widthptr is an arbitrary constant. We want it 3964 // to be at least 3*Widthptr so slices can be registerized. 3965 // Too big and we'll introduce too much register pressure. 3966 return false 3967 } 3968 switch t.Etype { 3969 case TARRAY: 3970 // We can't do larger arrays because dynamic indexing is 3971 // not supported on SSA variables. 3972 // TODO: allow if all indexes are constant. 3973 if t.NumElem() <= 1 { 3974 return canSSAType(t.Elem()) 3975 } 3976 return false 3977 case TSTRUCT: 3978 if t.NumFields() > ssa.MaxStruct { 3979 return false 3980 } 3981 for _, t1 := range t.Fields().Slice() { 3982 if !canSSAType(t1.Type) { 3983 return false 3984 } 3985 } 3986 return true 3987 default: 3988 return true 3989 } 3990 } 3991 3992 // exprPtr evaluates n to a pointer and nil-checks it. 3993 func (s *state) exprPtr(n *Node, bounded bool, lineno src.XPos) *ssa.Value { 3994 p := s.expr(n) 3995 if bounded || n.NonNil() { 3996 if s.f.Frontend().Debug_checknil() && lineno.Line() > 1 { 3997 s.f.Warnl(lineno, "removed nil check") 3998 } 3999 return p 4000 } 4001 s.nilCheck(p) 4002 return p 4003 } 4004 4005 // nilCheck generates nil pointer checking code. 4006 // Used only for automatically inserted nil checks, 4007 // not for user code like 'x != nil'. 4008 func (s *state) nilCheck(ptr *ssa.Value) { 4009 if disable_checknil != 0 || s.curfn.Func.NilCheckDisabled() { 4010 return 4011 } 4012 s.newValue2(ssa.OpNilCheck, types.TypeVoid, ptr, s.mem()) 4013 } 4014 4015 // boundsCheck generates bounds checking code. Checks if 0 <= idx < len, branches to exit if not. 4016 // Starts a new block on return. 4017 // idx is already converted to full int width. 4018 func (s *state) boundsCheck(idx, len *ssa.Value) { 4019 if Debug['B'] != 0 { 4020 return 4021 } 4022 4023 // bounds check 4024 cmp := s.newValue2(ssa.OpIsInBounds, types.Types[TBOOL], idx, len) 4025 s.check(cmp, panicindex) 4026 } 4027 4028 func couldBeNegative(v *ssa.Value) bool { 4029 switch v.Op { 4030 case ssa.OpSliceLen, ssa.OpSliceCap, ssa.OpStringLen: 4031 return false 4032 case ssa.OpConst64: 4033 return v.AuxInt < 0 4034 case ssa.OpConst32: 4035 return int32(v.AuxInt) < 0 4036 } 4037 return true 4038 } 4039 4040 // sliceBoundsCheck generates slice bounds checking code. Checks if 0 <= idx <= len, branches to exit if not. 4041 // Starts a new block on return. 4042 // idx and len are already converted to full int width. 4043 func (s *state) sliceBoundsCheck(idx, len *ssa.Value) { 4044 if Debug['B'] != 0 { 4045 return 4046 } 4047 if couldBeNegative(len) { 4048 // OpIsSliceInBounds requires second arg not negative; if it's not obviously true, must check. 4049 cmpop := ssa.OpGeq64 4050 if len.Type.Size() == 4 { 4051 cmpop = ssa.OpGeq32 4052 } 4053 cmp := s.newValue2(cmpop, types.Types[TBOOL], len, s.zeroVal(len.Type)) 4054 s.check(cmp, panicslice) 4055 } 4056 4057 // bounds check 4058 cmp := s.newValue2(ssa.OpIsSliceInBounds, types.Types[TBOOL], idx, len) 4059 s.check(cmp, panicslice) 4060 } 4061 4062 // If cmp (a bool) is false, panic using the given function. 4063 func (s *state) check(cmp *ssa.Value, fn *obj.LSym) { 4064 b := s.endBlock() 4065 b.Kind = ssa.BlockIf 4066 b.SetControl(cmp) 4067 b.Likely = ssa.BranchLikely 4068 bNext := s.f.NewBlock(ssa.BlockPlain) 4069 line := s.peekPos() 4070 pos := Ctxt.PosTable.Pos(line) 4071 fl := funcLine{f: fn, base: pos.Base(), line: pos.Line()} 4072 bPanic := s.panics[fl] 4073 if bPanic == nil { 4074 bPanic = s.f.NewBlock(ssa.BlockPlain) 4075 s.panics[fl] = bPanic 4076 s.startBlock(bPanic) 4077 // The panic call takes/returns memory to ensure that the right 4078 // memory state is observed if the panic happens. 4079 s.rtcall(fn, false, nil) 4080 } 4081 b.AddEdgeTo(bNext) 4082 b.AddEdgeTo(bPanic) 4083 s.startBlock(bNext) 4084 } 4085 4086 func (s *state) intDivide(n *Node, a, b *ssa.Value) *ssa.Value { 4087 needcheck := true 4088 switch b.Op { 4089 case ssa.OpConst8, ssa.OpConst16, ssa.OpConst32, ssa.OpConst64: 4090 if b.AuxInt != 0 { 4091 needcheck = false 4092 } 4093 } 4094 if needcheck { 4095 // do a size-appropriate check for zero 4096 cmp := s.newValue2(s.ssaOp(ONE, n.Type), types.Types[TBOOL], b, s.zeroVal(n.Type)) 4097 s.check(cmp, panicdivide) 4098 } 4099 return s.newValue2(s.ssaOp(n.Op, n.Type), a.Type, a, b) 4100 } 4101 4102 // rtcall issues a call to the given runtime function fn with the listed args. 4103 // Returns a slice of results of the given result types. 4104 // The call is added to the end of the current block. 4105 // If returns is false, the block is marked as an exit block. 4106 func (s *state) rtcall(fn *obj.LSym, returns bool, results []*types.Type, args ...*ssa.Value) []*ssa.Value { 4107 // Write args to the stack 4108 off := Ctxt.FixedFrameSize() 4109 for _, arg := range args { 4110 t := arg.Type 4111 off = Rnd(off, t.Alignment()) 4112 ptr := s.constOffPtrSP(t.PtrTo(), off) 4113 size := t.Size() 4114 s.store(t, ptr, arg) 4115 off += size 4116 } 4117 off = Rnd(off, int64(Widthreg)) 4118 4119 // Issue call 4120 call := s.newValue1A(ssa.OpStaticCall, types.TypeMem, fn, s.mem()) 4121 s.vars[&memVar] = call 4122 4123 if !returns { 4124 // Finish block 4125 b := s.endBlock() 4126 b.Kind = ssa.BlockExit 4127 b.SetControl(call) 4128 call.AuxInt = off - Ctxt.FixedFrameSize() 4129 if len(results) > 0 { 4130 Fatalf("panic call can't have results") 4131 } 4132 return nil 4133 } 4134 4135 // Load results 4136 res := make([]*ssa.Value, len(results)) 4137 for i, t := range results { 4138 off = Rnd(off, t.Alignment()) 4139 ptr := s.constOffPtrSP(types.NewPtr(t), off) 4140 res[i] = s.load(t, ptr) 4141 off += t.Size() 4142 } 4143 off = Rnd(off, int64(Widthptr)) 4144 4145 // Remember how much callee stack space we needed. 4146 call.AuxInt = off 4147 4148 return res 4149 } 4150 4151 // do *left = right for type t. 4152 func (s *state) storeType(t *types.Type, left, right *ssa.Value, skip skipMask, leftIsStmt bool) { 4153 s.instrument(t, left, true) 4154 4155 if skip == 0 && (!types.Haspointers(t) || ssa.IsStackAddr(left)) { 4156 // Known to not have write barrier. Store the whole type. 4157 s.vars[&memVar] = s.newValue3Apos(ssa.OpStore, types.TypeMem, t, left, right, s.mem(), leftIsStmt) 4158 return 4159 } 4160 4161 // store scalar fields first, so write barrier stores for 4162 // pointer fields can be grouped together, and scalar values 4163 // don't need to be live across the write barrier call. 4164 // TODO: if the writebarrier pass knows how to reorder stores, 4165 // we can do a single store here as long as skip==0. 4166 s.storeTypeScalars(t, left, right, skip) 4167 if skip&skipPtr == 0 && types.Haspointers(t) { 4168 s.storeTypePtrs(t, left, right) 4169 } 4170 } 4171 4172 // do *left = right for all scalar (non-pointer) parts of t. 4173 func (s *state) storeTypeScalars(t *types.Type, left, right *ssa.Value, skip skipMask) { 4174 switch { 4175 case t.IsBoolean() || t.IsInteger() || t.IsFloat() || t.IsComplex(): 4176 s.store(t, left, right) 4177 case t.IsPtrShaped(): 4178 // no scalar fields. 4179 case t.IsString(): 4180 if skip&skipLen != 0 { 4181 return 4182 } 4183 len := s.newValue1(ssa.OpStringLen, types.Types[TINT], right) 4184 lenAddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, s.config.PtrSize, left) 4185 s.store(types.Types[TINT], lenAddr, len) 4186 case t.IsSlice(): 4187 if skip&skipLen == 0 { 4188 len := s.newValue1(ssa.OpSliceLen, types.Types[TINT], right) 4189 lenAddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, s.config.PtrSize, left) 4190 s.store(types.Types[TINT], lenAddr, len) 4191 } 4192 if skip&skipCap == 0 { 4193 cap := s.newValue1(ssa.OpSliceCap, types.Types[TINT], right) 4194 capAddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.IntPtr, 2*s.config.PtrSize, left) 4195 s.store(types.Types[TINT], capAddr, cap) 4196 } 4197 case t.IsInterface(): 4198 // itab field doesn't need a write barrier (even though it is a pointer). 4199 itab := s.newValue1(ssa.OpITab, s.f.Config.Types.BytePtr, right) 4200 s.store(types.Types[TUINTPTR], left, itab) 4201 case t.IsStruct(): 4202 n := t.NumFields() 4203 for i := 0; i < n; i++ { 4204 ft := t.FieldType(i) 4205 addr := s.newValue1I(ssa.OpOffPtr, ft.PtrTo(), t.FieldOff(i), left) 4206 val := s.newValue1I(ssa.OpStructSelect, ft, int64(i), right) 4207 s.storeTypeScalars(ft, addr, val, 0) 4208 } 4209 case t.IsArray() && t.NumElem() == 0: 4210 // nothing 4211 case t.IsArray() && t.NumElem() == 1: 4212 s.storeTypeScalars(t.Elem(), left, s.newValue1I(ssa.OpArraySelect, t.Elem(), 0, right), 0) 4213 default: 4214 s.Fatalf("bad write barrier type %v", t) 4215 } 4216 } 4217 4218 // do *left = right for all pointer parts of t. 4219 func (s *state) storeTypePtrs(t *types.Type, left, right *ssa.Value) { 4220 switch { 4221 case t.IsPtrShaped(): 4222 s.store(t, left, right) 4223 case t.IsString(): 4224 ptr := s.newValue1(ssa.OpStringPtr, s.f.Config.Types.BytePtr, right) 4225 s.store(s.f.Config.Types.BytePtr, left, ptr) 4226 case t.IsSlice(): 4227 elType := types.NewPtr(t.Elem()) 4228 ptr := s.newValue1(ssa.OpSlicePtr, elType, right) 4229 s.store(elType, left, ptr) 4230 case t.IsInterface(): 4231 // itab field is treated as a scalar. 4232 idata := s.newValue1(ssa.OpIData, s.f.Config.Types.BytePtr, right) 4233 idataAddr := s.newValue1I(ssa.OpOffPtr, s.f.Config.Types.BytePtrPtr, s.config.PtrSize, left) 4234 s.store(s.f.Config.Types.BytePtr, idataAddr, idata) 4235 case t.IsStruct(): 4236 n := t.NumFields() 4237 for i := 0; i < n; i++ { 4238 ft := t.FieldType(i) 4239 if !types.Haspointers(ft) { 4240 continue 4241 } 4242 addr := s.newValue1I(ssa.OpOffPtr, ft.PtrTo(), t.FieldOff(i), left) 4243 val := s.newValue1I(ssa.OpStructSelect, ft, int64(i), right) 4244 s.storeTypePtrs(ft, addr, val) 4245 } 4246 case t.IsArray() && t.NumElem() == 0: 4247 // nothing 4248 case t.IsArray() && t.NumElem() == 1: 4249 s.storeTypePtrs(t.Elem(), left, s.newValue1I(ssa.OpArraySelect, t.Elem(), 0, right)) 4250 default: 4251 s.Fatalf("bad write barrier type %v", t) 4252 } 4253 } 4254 4255 func (s *state) storeArg(n *Node, t *types.Type, off int64) { 4256 pt := types.NewPtr(t) 4257 sp := s.constOffPtrSP(pt, off) 4258 4259 if !canSSAType(t) { 4260 a := s.addr(n, false) 4261 s.move(t, sp, a) 4262 return 4263 } 4264 4265 a := s.expr(n) 4266 s.storeType(t, sp, a, 0, false) 4267 } 4268 4269 // slice computes the slice v[i:j:k] and returns ptr, len, and cap of result. 4270 // i,j,k may be nil, in which case they are set to their default value. 4271 // t is a slice, ptr to array, or string type. 4272 func (s *state) slice(t *types.Type, v, i, j, k *ssa.Value, bounded bool) (p, l, c *ssa.Value) { 4273 var elemtype *types.Type 4274 var ptrtype *types.Type 4275 var ptr *ssa.Value 4276 var len *ssa.Value 4277 var cap *ssa.Value 4278 zero := s.constInt(types.Types[TINT], 0) 4279 switch { 4280 case t.IsSlice(): 4281 elemtype = t.Elem() 4282 ptrtype = types.NewPtr(elemtype) 4283 ptr = s.newValue1(ssa.OpSlicePtr, ptrtype, v) 4284 len = s.newValue1(ssa.OpSliceLen, types.Types[TINT], v) 4285 cap = s.newValue1(ssa.OpSliceCap, types.Types[TINT], v) 4286 case t.IsString(): 4287 elemtype = types.Types[TUINT8] 4288 ptrtype = types.NewPtr(elemtype) 4289 ptr = s.newValue1(ssa.OpStringPtr, ptrtype, v) 4290 len = s.newValue1(ssa.OpStringLen, types.Types[TINT], v) 4291 cap = len 4292 case t.IsPtr(): 4293 if !t.Elem().IsArray() { 4294 s.Fatalf("bad ptr to array in slice %v\n", t) 4295 } 4296 elemtype = t.Elem().Elem() 4297 ptrtype = types.NewPtr(elemtype) 4298 s.nilCheck(v) 4299 ptr = v 4300 len = s.constInt(types.Types[TINT], t.Elem().NumElem()) 4301 cap = len 4302 default: 4303 s.Fatalf("bad type in slice %v\n", t) 4304 } 4305 4306 // Set default values 4307 if i == nil { 4308 i = zero 4309 } 4310 if j == nil { 4311 j = len 4312 } 4313 if k == nil { 4314 k = cap 4315 } 4316 4317 if !bounded { 4318 // Panic if slice indices are not in bounds. 4319 s.sliceBoundsCheck(i, j) 4320 if j != k { 4321 s.sliceBoundsCheck(j, k) 4322 } 4323 if k != cap { 4324 s.sliceBoundsCheck(k, cap) 4325 } 4326 } 4327 4328 // Generate the following code assuming that indexes are in bounds. 4329 // The masking is to make sure that we don't generate a slice 4330 // that points to the next object in memory. 4331 // rlen = j - i 4332 // rcap = k - i 4333 // delta = i * elemsize 4334 // rptr = p + delta&mask(rcap) 4335 // result = (SliceMake rptr rlen rcap) 4336 // where mask(x) is 0 if x==0 and -1 if x>0. 4337 subOp := s.ssaOp(OSUB, types.Types[TINT]) 4338 mulOp := s.ssaOp(OMUL, types.Types[TINT]) 4339 andOp := s.ssaOp(OAND, types.Types[TINT]) 4340 rlen := s.newValue2(subOp, types.Types[TINT], j, i) 4341 var rcap *ssa.Value 4342 switch { 4343 case t.IsString(): 4344 // Capacity of the result is unimportant. However, we use 4345 // rcap to test if we've generated a zero-length slice. 4346 // Use length of strings for that. 4347 rcap = rlen 4348 case j == k: 4349 rcap = rlen 4350 default: 4351 rcap = s.newValue2(subOp, types.Types[TINT], k, i) 4352 } 4353 4354 var rptr *ssa.Value 4355 if (i.Op == ssa.OpConst64 || i.Op == ssa.OpConst32) && i.AuxInt == 0 { 4356 // No pointer arithmetic necessary. 4357 rptr = ptr 4358 } else { 4359 // delta = # of bytes to offset pointer by. 4360 delta := s.newValue2(mulOp, types.Types[TINT], i, s.constInt(types.Types[TINT], elemtype.Width)) 4361 // If we're slicing to the point where the capacity is zero, 4362 // zero out the delta. 4363 mask := s.newValue1(ssa.OpSlicemask, types.Types[TINT], rcap) 4364 delta = s.newValue2(andOp, types.Types[TINT], delta, mask) 4365 // Compute rptr = ptr + delta 4366 rptr = s.newValue2(ssa.OpAddPtr, ptrtype, ptr, delta) 4367 } 4368 4369 return rptr, rlen, rcap 4370 } 4371 4372 type u642fcvtTab struct { 4373 geq, cvt2F, and, rsh, or, add ssa.Op 4374 one func(*state, *types.Type, int64) *ssa.Value 4375 } 4376 4377 var u64_f64 = u642fcvtTab{ 4378 geq: ssa.OpGeq64, 4379 cvt2F: ssa.OpCvt64to64F, 4380 and: ssa.OpAnd64, 4381 rsh: ssa.OpRsh64Ux64, 4382 or: ssa.OpOr64, 4383 add: ssa.OpAdd64F, 4384 one: (*state).constInt64, 4385 } 4386 4387 var u64_f32 = u642fcvtTab{ 4388 geq: ssa.OpGeq64, 4389 cvt2F: ssa.OpCvt64to32F, 4390 and: ssa.OpAnd64, 4391 rsh: ssa.OpRsh64Ux64, 4392 or: ssa.OpOr64, 4393 add: ssa.OpAdd32F, 4394 one: (*state).constInt64, 4395 } 4396 4397 func (s *state) uint64Tofloat64(n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value { 4398 return s.uint64Tofloat(&u64_f64, n, x, ft, tt) 4399 } 4400 4401 func (s *state) uint64Tofloat32(n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value { 4402 return s.uint64Tofloat(&u64_f32, n, x, ft, tt) 4403 } 4404 4405 func (s *state) uint64Tofloat(cvttab *u642fcvtTab, n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value { 4406 // if x >= 0 { 4407 // result = (floatY) x 4408 // } else { 4409 // y = uintX(x) ; y = x & 1 4410 // z = uintX(x) ; z = z >> 1 4411 // z = z >> 1 4412 // z = z | y 4413 // result = floatY(z) 4414 // result = result + result 4415 // } 4416 // 4417 // Code borrowed from old code generator. 4418 // What's going on: large 64-bit "unsigned" looks like 4419 // negative number to hardware's integer-to-float 4420 // conversion. However, because the mantissa is only 4421 // 63 bits, we don't need the LSB, so instead we do an 4422 // unsigned right shift (divide by two), convert, and 4423 // double. However, before we do that, we need to be 4424 // sure that we do not lose a "1" if that made the 4425 // difference in the resulting rounding. Therefore, we 4426 // preserve it, and OR (not ADD) it back in. The case 4427 // that matters is when the eleven discarded bits are 4428 // equal to 10000000001; that rounds up, and the 1 cannot 4429 // be lost else it would round down if the LSB of the 4430 // candidate mantissa is 0. 4431 cmp := s.newValue2(cvttab.geq, types.Types[TBOOL], x, s.zeroVal(ft)) 4432 b := s.endBlock() 4433 b.Kind = ssa.BlockIf 4434 b.SetControl(cmp) 4435 b.Likely = ssa.BranchLikely 4436 4437 bThen := s.f.NewBlock(ssa.BlockPlain) 4438 bElse := s.f.NewBlock(ssa.BlockPlain) 4439 bAfter := s.f.NewBlock(ssa.BlockPlain) 4440 4441 b.AddEdgeTo(bThen) 4442 s.startBlock(bThen) 4443 a0 := s.newValue1(cvttab.cvt2F, tt, x) 4444 s.vars[n] = a0 4445 s.endBlock() 4446 bThen.AddEdgeTo(bAfter) 4447 4448 b.AddEdgeTo(bElse) 4449 s.startBlock(bElse) 4450 one := cvttab.one(s, ft, 1) 4451 y := s.newValue2(cvttab.and, ft, x, one) 4452 z := s.newValue2(cvttab.rsh, ft, x, one) 4453 z = s.newValue2(cvttab.or, ft, z, y) 4454 a := s.newValue1(cvttab.cvt2F, tt, z) 4455 a1 := s.newValue2(cvttab.add, tt, a, a) 4456 s.vars[n] = a1 4457 s.endBlock() 4458 bElse.AddEdgeTo(bAfter) 4459 4460 s.startBlock(bAfter) 4461 return s.variable(n, n.Type) 4462 } 4463 4464 type u322fcvtTab struct { 4465 cvtI2F, cvtF2F ssa.Op 4466 } 4467 4468 var u32_f64 = u322fcvtTab{ 4469 cvtI2F: ssa.OpCvt32to64F, 4470 cvtF2F: ssa.OpCopy, 4471 } 4472 4473 var u32_f32 = u322fcvtTab{ 4474 cvtI2F: ssa.OpCvt32to32F, 4475 cvtF2F: ssa.OpCvt64Fto32F, 4476 } 4477 4478 func (s *state) uint32Tofloat64(n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value { 4479 return s.uint32Tofloat(&u32_f64, n, x, ft, tt) 4480 } 4481 4482 func (s *state) uint32Tofloat32(n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value { 4483 return s.uint32Tofloat(&u32_f32, n, x, ft, tt) 4484 } 4485 4486 func (s *state) uint32Tofloat(cvttab *u322fcvtTab, n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value { 4487 // if x >= 0 { 4488 // result = floatY(x) 4489 // } else { 4490 // result = floatY(float64(x) + (1<<32)) 4491 // } 4492 cmp := s.newValue2(ssa.OpGeq32, types.Types[TBOOL], x, s.zeroVal(ft)) 4493 b := s.endBlock() 4494 b.Kind = ssa.BlockIf 4495 b.SetControl(cmp) 4496 b.Likely = ssa.BranchLikely 4497 4498 bThen := s.f.NewBlock(ssa.BlockPlain) 4499 bElse := s.f.NewBlock(ssa.BlockPlain) 4500 bAfter := s.f.NewBlock(ssa.BlockPlain) 4501 4502 b.AddEdgeTo(bThen) 4503 s.startBlock(bThen) 4504 a0 := s.newValue1(cvttab.cvtI2F, tt, x) 4505 s.vars[n] = a0 4506 s.endBlock() 4507 bThen.AddEdgeTo(bAfter) 4508 4509 b.AddEdgeTo(bElse) 4510 s.startBlock(bElse) 4511 a1 := s.newValue1(ssa.OpCvt32to64F, types.Types[TFLOAT64], x) 4512 twoToThe32 := s.constFloat64(types.Types[TFLOAT64], float64(1<<32)) 4513 a2 := s.newValue2(ssa.OpAdd64F, types.Types[TFLOAT64], a1, twoToThe32) 4514 a3 := s.newValue1(cvttab.cvtF2F, tt, a2) 4515 4516 s.vars[n] = a3 4517 s.endBlock() 4518 bElse.AddEdgeTo(bAfter) 4519 4520 s.startBlock(bAfter) 4521 return s.variable(n, n.Type) 4522 } 4523 4524 // referenceTypeBuiltin generates code for the len/cap builtins for maps and channels. 4525 func (s *state) referenceTypeBuiltin(n *Node, x *ssa.Value) *ssa.Value { 4526 if !n.Left.Type.IsMap() && !n.Left.Type.IsChan() { 4527 s.Fatalf("node must be a map or a channel") 4528 } 4529 // if n == nil { 4530 // return 0 4531 // } else { 4532 // // len 4533 // return *((*int)n) 4534 // // cap 4535 // return *(((*int)n)+1) 4536 // } 4537 lenType := n.Type 4538 nilValue := s.constNil(types.Types[TUINTPTR]) 4539 cmp := s.newValue2(ssa.OpEqPtr, types.Types[TBOOL], x, nilValue) 4540 b := s.endBlock() 4541 b.Kind = ssa.BlockIf 4542 b.SetControl(cmp) 4543 b.Likely = ssa.BranchUnlikely 4544 4545 bThen := s.f.NewBlock(ssa.BlockPlain) 4546 bElse := s.f.NewBlock(ssa.BlockPlain) 4547 bAfter := s.f.NewBlock(ssa.BlockPlain) 4548 4549 // length/capacity of a nil map/chan is zero 4550 b.AddEdgeTo(bThen) 4551 s.startBlock(bThen) 4552 s.vars[n] = s.zeroVal(lenType) 4553 s.endBlock() 4554 bThen.AddEdgeTo(bAfter) 4555 4556 b.AddEdgeTo(bElse) 4557 s.startBlock(bElse) 4558 switch n.Op { 4559 case OLEN: 4560 // length is stored in the first word for map/chan 4561 s.vars[n] = s.load(lenType, x) 4562 case OCAP: 4563 // capacity is stored in the second word for chan 4564 sw := s.newValue1I(ssa.OpOffPtr, lenType.PtrTo(), lenType.Width, x) 4565 s.vars[n] = s.load(lenType, sw) 4566 default: 4567 s.Fatalf("op must be OLEN or OCAP") 4568 } 4569 s.endBlock() 4570 bElse.AddEdgeTo(bAfter) 4571 4572 s.startBlock(bAfter) 4573 return s.variable(n, lenType) 4574 } 4575 4576 type f2uCvtTab struct { 4577 ltf, cvt2U, subf, or ssa.Op 4578 floatValue func(*state, *types.Type, float64) *ssa.Value 4579 intValue func(*state, *types.Type, int64) *ssa.Value 4580 cutoff uint64 4581 } 4582 4583 var f32_u64 = f2uCvtTab{ 4584 ltf: ssa.OpLess32F, 4585 cvt2U: ssa.OpCvt32Fto64, 4586 subf: ssa.OpSub32F, 4587 or: ssa.OpOr64, 4588 floatValue: (*state).constFloat32, 4589 intValue: (*state).constInt64, 4590 cutoff: 9223372036854775808, 4591 } 4592 4593 var f64_u64 = f2uCvtTab{ 4594 ltf: ssa.OpLess64F, 4595 cvt2U: ssa.OpCvt64Fto64, 4596 subf: ssa.OpSub64F, 4597 or: ssa.OpOr64, 4598 floatValue: (*state).constFloat64, 4599 intValue: (*state).constInt64, 4600 cutoff: 9223372036854775808, 4601 } 4602 4603 var f32_u32 = f2uCvtTab{ 4604 ltf: ssa.OpLess32F, 4605 cvt2U: ssa.OpCvt32Fto32, 4606 subf: ssa.OpSub32F, 4607 or: ssa.OpOr32, 4608 floatValue: (*state).constFloat32, 4609 intValue: func(s *state, t *types.Type, v int64) *ssa.Value { return s.constInt32(t, int32(v)) }, 4610 cutoff: 2147483648, 4611 } 4612 4613 var f64_u32 = f2uCvtTab{ 4614 ltf: ssa.OpLess64F, 4615 cvt2U: ssa.OpCvt64Fto32, 4616 subf: ssa.OpSub64F, 4617 or: ssa.OpOr32, 4618 floatValue: (*state).constFloat64, 4619 intValue: func(s *state, t *types.Type, v int64) *ssa.Value { return s.constInt32(t, int32(v)) }, 4620 cutoff: 2147483648, 4621 } 4622 4623 func (s *state) float32ToUint64(n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value { 4624 return s.floatToUint(&f32_u64, n, x, ft, tt) 4625 } 4626 func (s *state) float64ToUint64(n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value { 4627 return s.floatToUint(&f64_u64, n, x, ft, tt) 4628 } 4629 4630 func (s *state) float32ToUint32(n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value { 4631 return s.floatToUint(&f32_u32, n, x, ft, tt) 4632 } 4633 4634 func (s *state) float64ToUint32(n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value { 4635 return s.floatToUint(&f64_u32, n, x, ft, tt) 4636 } 4637 4638 func (s *state) floatToUint(cvttab *f2uCvtTab, n *Node, x *ssa.Value, ft, tt *types.Type) *ssa.Value { 4639 // cutoff:=1<<(intY_Size-1) 4640 // if x < floatX(cutoff) { 4641 // result = uintY(x) 4642 // } else { 4643 // y = x - floatX(cutoff) 4644 // z = uintY(y) 4645 // result = z | -(cutoff) 4646 // } 4647 cutoff := cvttab.floatValue(s, ft, float64(cvttab.cutoff)) 4648 cmp := s.newValue2(cvttab.ltf, types.Types[TBOOL], x, cutoff) 4649 b := s.endBlock() 4650 b.Kind = ssa.BlockIf 4651 b.SetControl(cmp) 4652 b.Likely = ssa.BranchLikely 4653 4654 bThen := s.f.NewBlock(ssa.BlockPlain) 4655 bElse := s.f.NewBlock(ssa.BlockPlain) 4656 bAfter := s.f.NewBlock(ssa.BlockPlain) 4657 4658 b.AddEdgeTo(bThen) 4659 s.startBlock(bThen) 4660 a0 := s.newValue1(cvttab.cvt2U, tt, x) 4661 s.vars[n] = a0 4662 s.endBlock() 4663 bThen.AddEdgeTo(bAfter) 4664 4665 b.AddEdgeTo(bElse) 4666 s.startBlock(bElse) 4667 y := s.newValue2(cvttab.subf, ft, x, cutoff) 4668 y = s.newValue1(cvttab.cvt2U, tt, y) 4669 z := cvttab.intValue(s, tt, int64(-cvttab.cutoff)) 4670 a1 := s.newValue2(cvttab.or, tt, y, z) 4671 s.vars[n] = a1 4672 s.endBlock() 4673 bElse.AddEdgeTo(bAfter) 4674 4675 s.startBlock(bAfter) 4676 return s.variable(n, n.Type) 4677 } 4678 4679 // dottype generates SSA for a type assertion node. 4680 // commaok indicates whether to panic or return a bool. 4681 // If commaok is false, resok will be nil. 4682 func (s *state) dottype(n *Node, commaok bool) (res, resok *ssa.Value) { 4683 iface := s.expr(n.Left) // input interface 4684 target := s.expr(n.Right) // target type 4685 byteptr := s.f.Config.Types.BytePtr 4686 4687 if n.Type.IsInterface() { 4688 if n.Type.IsEmptyInterface() { 4689 // Converting to an empty interface. 4690 // Input could be an empty or nonempty interface. 4691 if Debug_typeassert > 0 { 4692 Warnl(n.Pos, "type assertion inlined") 4693 } 4694 4695 // Get itab/type field from input. 4696 itab := s.newValue1(ssa.OpITab, byteptr, iface) 4697 // Conversion succeeds iff that field is not nil. 4698 cond := s.newValue2(ssa.OpNeqPtr, types.Types[TBOOL], itab, s.constNil(byteptr)) 4699 4700 if n.Left.Type.IsEmptyInterface() && commaok { 4701 // Converting empty interface to empty interface with ,ok is just a nil check. 4702 return iface, cond 4703 } 4704 4705 // Branch on nilness. 4706 b := s.endBlock() 4707 b.Kind = ssa.BlockIf 4708 b.SetControl(cond) 4709 b.Likely = ssa.BranchLikely 4710 bOk := s.f.NewBlock(ssa.BlockPlain) 4711 bFail := s.f.NewBlock(ssa.BlockPlain) 4712 b.AddEdgeTo(bOk) 4713 b.AddEdgeTo(bFail) 4714 4715 if !commaok { 4716 // On failure, panic by calling panicnildottype. 4717 s.startBlock(bFail) 4718 s.rtcall(panicnildottype, false, nil, target) 4719 4720 // On success, return (perhaps modified) input interface. 4721 s.startBlock(bOk) 4722 if n.Left.Type.IsEmptyInterface() { 4723 res = iface // Use input interface unchanged. 4724 return 4725 } 4726 // Load type out of itab, build interface with existing idata. 4727 off := s.newValue1I(ssa.OpOffPtr, byteptr, int64(Widthptr), itab) 4728 typ := s.load(byteptr, off) 4729 idata := s.newValue1(ssa.OpIData, n.Type, iface) 4730 res = s.newValue2(ssa.OpIMake, n.Type, typ, idata) 4731 return 4732 } 4733 4734 s.startBlock(bOk) 4735 // nonempty -> empty 4736 // Need to load type from itab 4737 off := s.newValue1I(ssa.OpOffPtr, byteptr, int64(Widthptr), itab) 4738 s.vars[&typVar] = s.load(byteptr, off) 4739 s.endBlock() 4740 4741 // itab is nil, might as well use that as the nil result. 4742 s.startBlock(bFail) 4743 s.vars[&typVar] = itab 4744 s.endBlock() 4745 4746 // Merge point. 4747 bEnd := s.f.NewBlock(ssa.BlockPlain) 4748 bOk.AddEdgeTo(bEnd) 4749 bFail.AddEdgeTo(bEnd) 4750 s.startBlock(bEnd) 4751 idata := s.newValue1(ssa.OpIData, n.Type, iface) 4752 res = s.newValue2(ssa.OpIMake, n.Type, s.variable(&typVar, byteptr), idata) 4753 resok = cond 4754 delete(s.vars, &typVar) 4755 return 4756 } 4757 // converting to a nonempty interface needs a runtime call. 4758 if Debug_typeassert > 0 { 4759 Warnl(n.Pos, "type assertion not inlined") 4760 } 4761 if n.Left.Type.IsEmptyInterface() { 4762 if commaok { 4763 call := s.rtcall(assertE2I2, true, []*types.Type{n.Type, types.Types[TBOOL]}, target, iface) 4764 return call[0], call[1] 4765 } 4766 return s.rtcall(assertE2I, true, []*types.Type{n.Type}, target, iface)[0], nil 4767 } 4768 if commaok { 4769 call := s.rtcall(assertI2I2, true, []*types.Type{n.Type, types.Types[TBOOL]}, target, iface) 4770 return call[0], call[1] 4771 } 4772 return s.rtcall(assertI2I, true, []*types.Type{n.Type}, target, iface)[0], nil 4773 } 4774 4775 if Debug_typeassert > 0 { 4776 Warnl(n.Pos, "type assertion inlined") 4777 } 4778 4779 // Converting to a concrete type. 4780 direct := isdirectiface(n.Type) 4781 itab := s.newValue1(ssa.OpITab, byteptr, iface) // type word of interface 4782 if Debug_typeassert > 0 { 4783 Warnl(n.Pos, "type assertion inlined") 4784 } 4785 var targetITab *ssa.Value 4786 if n.Left.Type.IsEmptyInterface() { 4787 // Looking for pointer to target type. 4788 targetITab = target 4789 } else { 4790 // Looking for pointer to itab for target type and source interface. 4791 targetITab = s.expr(n.List.First()) 4792 } 4793 4794 var tmp *Node // temporary for use with large types 4795 var addr *ssa.Value // address of tmp 4796 if commaok && !canSSAType(n.Type) { 4797 // unSSAable type, use temporary. 4798 // TODO: get rid of some of these temporaries. 4799 tmp = tempAt(n.Pos, s.curfn, n.Type) 4800 s.vars[&memVar] = s.newValue1A(ssa.OpVarDef, types.TypeMem, tmp, s.mem()) 4801 addr = s.addr(tmp, false) 4802 } 4803 4804 cond := s.newValue2(ssa.OpEqPtr, types.Types[TBOOL], itab, targetITab) 4805 b := s.endBlock() 4806 b.Kind = ssa.BlockIf 4807 b.SetControl(cond) 4808 b.Likely = ssa.BranchLikely 4809 4810 bOk := s.f.NewBlock(ssa.BlockPlain) 4811 bFail := s.f.NewBlock(ssa.BlockPlain) 4812 b.AddEdgeTo(bOk) 4813 b.AddEdgeTo(bFail) 4814 4815 if !commaok { 4816 // on failure, panic by calling panicdottype 4817 s.startBlock(bFail) 4818 taddr := s.expr(n.Right.Right) 4819 if n.Left.Type.IsEmptyInterface() { 4820 s.rtcall(panicdottypeE, false, nil, itab, target, taddr) 4821 } else { 4822 s.rtcall(panicdottypeI, false, nil, itab, target, taddr) 4823 } 4824 4825 // on success, return data from interface 4826 s.startBlock(bOk) 4827 if direct { 4828 return s.newValue1(ssa.OpIData, n.Type, iface), nil 4829 } 4830 p := s.newValue1(ssa.OpIData, types.NewPtr(n.Type), iface) 4831 return s.load(n.Type, p), nil 4832 } 4833 4834 // commaok is the more complicated case because we have 4835 // a control flow merge point. 4836 bEnd := s.f.NewBlock(ssa.BlockPlain) 4837 // Note that we need a new valVar each time (unlike okVar where we can 4838 // reuse the variable) because it might have a different type every time. 4839 valVar := &Node{Op: ONAME, Sym: &types.Sym{Name: "val"}} 4840 4841 // type assertion succeeded 4842 s.startBlock(bOk) 4843 if tmp == nil { 4844 if direct { 4845 s.vars[valVar] = s.newValue1(ssa.OpIData, n.Type, iface) 4846 } else { 4847 p := s.newValue1(ssa.OpIData, types.NewPtr(n.Type), iface) 4848 s.vars[valVar] = s.load(n.Type, p) 4849 } 4850 } else { 4851 p := s.newValue1(ssa.OpIData, types.NewPtr(n.Type), iface) 4852 s.move(n.Type, addr, p) 4853 } 4854 s.vars[&okVar] = s.constBool(true) 4855 s.endBlock() 4856 bOk.AddEdgeTo(bEnd) 4857 4858 // type assertion failed 4859 s.startBlock(bFail) 4860 if tmp == nil { 4861 s.vars[valVar] = s.zeroVal(n.Type) 4862 } else { 4863 s.zero(n.Type, addr) 4864 } 4865 s.vars[&okVar] = s.constBool(false) 4866 s.endBlock() 4867 bFail.AddEdgeTo(bEnd) 4868 4869 // merge point 4870 s.startBlock(bEnd) 4871 if tmp == nil { 4872 res = s.variable(valVar, n.Type) 4873 delete(s.vars, valVar) 4874 } else { 4875 res = s.load(n.Type, addr) 4876 s.vars[&memVar] = s.newValue1A(ssa.OpVarKill, types.TypeMem, tmp, s.mem()) 4877 } 4878 resok = s.variable(&okVar, types.Types[TBOOL]) 4879 delete(s.vars, &okVar) 4880 return res, resok 4881 } 4882 4883 // variable returns the value of a variable at the current location. 4884 func (s *state) variable(name *Node, t *types.Type) *ssa.Value { 4885 v := s.vars[name] 4886 if v != nil { 4887 return v 4888 } 4889 v = s.fwdVars[name] 4890 if v != nil { 4891 return v 4892 } 4893 4894 if s.curBlock == s.f.Entry { 4895 // No variable should be live at entry. 4896 s.Fatalf("Value live at entry. It shouldn't be. func %s, node %v, value %v", s.f.Name, name, v) 4897 } 4898 // Make a FwdRef, which records a value that's live on block input. 4899 // We'll find the matching definition as part of insertPhis. 4900 v = s.newValue0A(ssa.OpFwdRef, t, name) 4901 s.fwdVars[name] = v 4902 s.addNamedValue(name, v) 4903 return v 4904 } 4905 4906 func (s *state) mem() *ssa.Value { 4907 return s.variable(&memVar, types.TypeMem) 4908 } 4909 4910 func (s *state) addNamedValue(n *Node, v *ssa.Value) { 4911 if n.Class() == Pxxx { 4912 // Don't track our dummy nodes (&memVar etc.). 4913 return 4914 } 4915 if n.IsAutoTmp() { 4916 // Don't track temporary variables. 4917 return 4918 } 4919 if n.Class() == PPARAMOUT { 4920 // Don't track named output values. This prevents return values 4921 // from being assigned too early. See #14591 and #14762. TODO: allow this. 4922 return 4923 } 4924 if n.Class() == PAUTO && n.Xoffset != 0 { 4925 s.Fatalf("AUTO var with offset %v %d", n, n.Xoffset) 4926 } 4927 loc := ssa.LocalSlot{N: n, Type: n.Type, Off: 0} 4928 values, ok := s.f.NamedValues[loc] 4929 if !ok { 4930 s.f.Names = append(s.f.Names, loc) 4931 } 4932 s.f.NamedValues[loc] = append(values, v) 4933 } 4934 4935 // Branch is an unresolved branch. 4936 type Branch struct { 4937 P *obj.Prog // branch instruction 4938 B *ssa.Block // target 4939 } 4940 4941 // SSAGenState contains state needed during Prog generation. 4942 type SSAGenState struct { 4943 pp *Progs 4944 4945 // Branches remembers all the branch instructions we've seen 4946 // and where they would like to go. 4947 Branches []Branch 4948 4949 // bstart remembers where each block starts (indexed by block ID) 4950 bstart []*obj.Prog 4951 4952 // 387 port: maps from SSE registers (REG_X?) to 387 registers (REG_F?) 4953 SSEto387 map[int16]int16 4954 // Some architectures require a 64-bit temporary for FP-related register shuffling. Examples include x86-387, PPC, and Sparc V8. 4955 ScratchFpMem *Node 4956 4957 maxarg int64 // largest frame size for arguments to calls made by the function 4958 4959 // Map from GC safe points to liveness index, generated by 4960 // liveness analysis. 4961 livenessMap LivenessMap 4962 4963 // lineRunStart records the beginning of the current run of instructions 4964 // within a single block sharing the same line number 4965 // Used to move statement marks to the beginning of such runs. 4966 lineRunStart *obj.Prog 4967 4968 // wasm: The number of values on the WebAssembly stack. This is only used as a safeguard. 4969 OnWasmStackSkipped int 4970 } 4971 4972 // Prog appends a new Prog. 4973 func (s *SSAGenState) Prog(as obj.As) *obj.Prog { 4974 p := s.pp.Prog(as) 4975 if ssa.LosesStmtMark(as) { 4976 return p 4977 } 4978 // Float a statement start to the beginning of any same-line run. 4979 // lineRunStart is reset at block boundaries, which appears to work well. 4980 if s.lineRunStart == nil || s.lineRunStart.Pos.Line() != p.Pos.Line() { 4981 s.lineRunStart = p 4982 } else if p.Pos.IsStmt() == src.PosIsStmt { 4983 s.lineRunStart.Pos = s.lineRunStart.Pos.WithIsStmt() 4984 p.Pos = p.Pos.WithNotStmt() 4985 } 4986 return p 4987 } 4988 4989 // Pc returns the current Prog. 4990 func (s *SSAGenState) Pc() *obj.Prog { 4991 return s.pp.next 4992 } 4993 4994 // SetPos sets the current source position. 4995 func (s *SSAGenState) SetPos(pos src.XPos) { 4996 s.pp.pos = pos 4997 } 4998 4999 // Br emits a single branch instruction and returns the instruction. 5000 // Not all architectures need the returned instruction, but otherwise 5001 // the boilerplate is common to all. 5002 func (s *SSAGenState) Br(op obj.As, target *ssa.Block) *obj.Prog { 5003 p := s.Prog(op) 5004 p.To.Type = obj.TYPE_BRANCH 5005 s.Branches = append(s.Branches, Branch{P: p, B: target}) 5006 return p 5007 } 5008 5009 // DebugFriendlySetPos adjusts Pos.IsStmt subject to heuristics 5010 // that reduce "jumpy" line number churn when debugging. 5011 // Spill/fill/copy instructions from the register allocator, 5012 // phi functions, and instructions with a no-pos position 5013 // are examples of instructions that can cause churn. 5014 func (s *SSAGenState) DebugFriendlySetPosFrom(v *ssa.Value) { 5015 switch v.Op { 5016 case ssa.OpPhi, ssa.OpCopy, ssa.OpLoadReg, ssa.OpStoreReg: 5017 // These are not statements 5018 s.SetPos(v.Pos.WithNotStmt()) 5019 default: 5020 p := v.Pos 5021 if p != src.NoXPos { 5022 // If the position is defined, update the position. 5023 // Also convert default IsStmt to NotStmt; only 5024 // explicit statement boundaries should appear 5025 // in the generated code. 5026 if p.IsStmt() != src.PosIsStmt { 5027 p = p.WithNotStmt() 5028 } 5029 s.SetPos(p) 5030 } 5031 } 5032 } 5033 5034 // byXoffset implements sort.Interface for []*Node using Xoffset as the ordering. 5035 type byXoffset []*Node 5036 5037 func (s byXoffset) Len() int { return len(s) } 5038 func (s byXoffset) Less(i, j int) bool { return s[i].Xoffset < s[j].Xoffset } 5039 func (s byXoffset) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 5040 5041 func emitStackObjects(e *ssafn, pp *Progs) { 5042 var vars []*Node 5043 for _, n := range e.curfn.Func.Dcl { 5044 if livenessShouldTrack(n) && n.Addrtaken() { 5045 vars = append(vars, n) 5046 } 5047 } 5048 if len(vars) == 0 { 5049 return 5050 } 5051 5052 // Sort variables from lowest to highest address. 5053 sort.Sort(byXoffset(vars)) 5054 5055 // Populate the stack object data. 5056 // Format must match runtime/stack.go:stackObjectRecord. 5057 x := e.curfn.Func.lsym.Func.StackObjects 5058 off := 0 5059 off = duintptr(x, off, uint64(len(vars))) 5060 for _, v := range vars { 5061 // Note: arguments and return values have non-negative Xoffset, 5062 // in which case the offset is relative to argp. 5063 // Locals have a negative Xoffset, in which case the offset is relative to varp. 5064 off = duintptr(x, off, uint64(v.Xoffset)) 5065 if !typesym(v.Type).Siggen() { 5066 Fatalf("stack object's type symbol not generated for type %s", v.Type) 5067 } 5068 off = dsymptr(x, off, dtypesym(v.Type), 0) 5069 } 5070 5071 // Emit a funcdata pointing at the stack object data. 5072 p := pp.Prog(obj.AFUNCDATA) 5073 Addrconst(&p.From, objabi.FUNCDATA_StackObjects) 5074 p.To.Type = obj.TYPE_MEM 5075 p.To.Name = obj.NAME_EXTERN 5076 p.To.Sym = x 5077 5078 if debuglive != 0 { 5079 for _, v := range vars { 5080 Warnl(v.Pos, "stack object %v %s", v, v.Type.String()) 5081 } 5082 } 5083 } 5084 5085 // genssa appends entries to pp for each instruction in f. 5086 func genssa(f *ssa.Func, pp *Progs) { 5087 var s SSAGenState 5088 5089 e := f.Frontend().(*ssafn) 5090 5091 s.livenessMap = liveness(e, f, pp) 5092 emitStackObjects(e, pp) 5093 5094 // Remember where each block starts. 5095 s.bstart = make([]*obj.Prog, f.NumBlocks()) 5096 s.pp = pp 5097 var progToValue map[*obj.Prog]*ssa.Value 5098 var progToBlock map[*obj.Prog]*ssa.Block 5099 var valueToProgAfter []*obj.Prog // The first Prog following computation of a value v; v is visible at this point. 5100 if f.PrintOrHtmlSSA { 5101 progToValue = make(map[*obj.Prog]*ssa.Value, f.NumValues()) 5102 progToBlock = make(map[*obj.Prog]*ssa.Block, f.NumBlocks()) 5103 f.Logf("genssa %s\n", f.Name) 5104 progToBlock[s.pp.next] = f.Blocks[0] 5105 } 5106 5107 if thearch.Use387 { 5108 s.SSEto387 = map[int16]int16{} 5109 } 5110 5111 s.ScratchFpMem = e.scratchFpMem 5112 5113 if Ctxt.Flag_locationlists { 5114 if cap(f.Cache.ValueToProgAfter) < f.NumValues() { 5115 f.Cache.ValueToProgAfter = make([]*obj.Prog, f.NumValues()) 5116 } 5117 valueToProgAfter = f.Cache.ValueToProgAfter[:f.NumValues()] 5118 for i := range valueToProgAfter { 5119 valueToProgAfter[i] = nil 5120 } 5121 } 5122 5123 // If the very first instruction is not tagged as a statement, 5124 // debuggers may attribute it to previous function in program. 5125 firstPos := src.NoXPos 5126 for _, v := range f.Entry.Values { 5127 if v.Pos.IsStmt() == src.PosIsStmt { 5128 firstPos = v.Pos 5129 v.Pos = firstPos.WithDefaultStmt() 5130 break 5131 } 5132 } 5133 5134 // Emit basic blocks 5135 for i, b := range f.Blocks { 5136 s.bstart[b.ID] = s.pp.next 5137 s.pp.nextLive = LivenessInvalid 5138 s.lineRunStart = nil 5139 5140 // Emit values in block 5141 thearch.SSAMarkMoves(&s, b) 5142 for _, v := range b.Values { 5143 x := s.pp.next 5144 s.DebugFriendlySetPosFrom(v) 5145 // Attach this safe point to the next 5146 // instruction. 5147 s.pp.nextLive = s.livenessMap.Get(v) 5148 switch v.Op { 5149 case ssa.OpInitMem: 5150 // memory arg needs no code 5151 case ssa.OpArg: 5152 // input args need no code 5153 case ssa.OpSP, ssa.OpSB: 5154 // nothing to do 5155 case ssa.OpSelect0, ssa.OpSelect1: 5156 // nothing to do 5157 case ssa.OpGetG: 5158 // nothing to do when there's a g register, 5159 // and checkLower complains if there's not 5160 case ssa.OpVarDef, ssa.OpVarLive, ssa.OpKeepAlive, ssa.OpVarKill: 5161 // nothing to do; already used by liveness 5162 case ssa.OpPhi: 5163 CheckLoweredPhi(v) 5164 case ssa.OpConvert: 5165 // nothing to do; no-op conversion for liveness 5166 if v.Args[0].Reg() != v.Reg() { 5167 v.Fatalf("OpConvert should be a no-op: %s; %s", v.Args[0].LongString(), v.LongString()) 5168 } 5169 case ssa.OpInlMark: 5170 p := thearch.Ginsnop(s.pp) 5171 if pp.curfn.Func.lsym != nil { 5172 // lsym is nil if the function name is "_". 5173 pp.curfn.Func.lsym.Func.AddInlMark(p, v.AuxInt32()) 5174 } 5175 // TODO: if matching line number, merge somehow with previous instruction? 5176 5177 default: 5178 // let the backend handle it 5179 // Special case for first line in function; move it to the start. 5180 if firstPos != src.NoXPos { 5181 s.SetPos(firstPos) 5182 firstPos = src.NoXPos 5183 } 5184 thearch.SSAGenValue(&s, v) 5185 } 5186 5187 if Ctxt.Flag_locationlists { 5188 valueToProgAfter[v.ID] = s.pp.next 5189 } 5190 5191 if f.PrintOrHtmlSSA { 5192 for ; x != s.pp.next; x = x.Link { 5193 progToValue[x] = v 5194 } 5195 } 5196 } 5197 // Emit control flow instructions for block 5198 var next *ssa.Block 5199 if i < len(f.Blocks)-1 && Debug['N'] == 0 { 5200 // If -N, leave next==nil so every block with successors 5201 // ends in a JMP (except call blocks - plive doesn't like 5202 // select{send,recv} followed by a JMP call). Helps keep 5203 // line numbers for otherwise empty blocks. 5204 next = f.Blocks[i+1] 5205 } 5206 x := s.pp.next 5207 s.SetPos(b.Pos) 5208 thearch.SSAGenBlock(&s, b, next) 5209 if f.PrintOrHtmlSSA { 5210 for ; x != s.pp.next; x = x.Link { 5211 progToBlock[x] = b 5212 } 5213 } 5214 } 5215 5216 if Ctxt.Flag_locationlists { 5217 e.curfn.Func.DebugInfo = ssa.BuildFuncDebug(Ctxt, f, Debug_locationlist > 1, stackOffset) 5218 bstart := s.bstart 5219 // Note that at this moment, Prog.Pc is a sequence number; it's 5220 // not a real PC until after assembly, so this mapping has to 5221 // be done later. 5222 e.curfn.Func.DebugInfo.GetPC = func(b, v ssa.ID) int64 { 5223 switch v { 5224 case ssa.BlockStart.ID: 5225 if b == f.Entry.ID { 5226 return 0 // Start at the very beginning, at the assembler-generated prologue. 5227 // this should only happen for function args (ssa.OpArg) 5228 } 5229 return bstart[b].Pc 5230 case ssa.BlockEnd.ID: 5231 return e.curfn.Func.lsym.Size 5232 default: 5233 return valueToProgAfter[v].Pc 5234 } 5235 } 5236 } 5237 5238 // Resolve branches, and relax DefaultStmt into NotStmt 5239 for _, br := range s.Branches { 5240 br.P.To.Val = s.bstart[br.B.ID] 5241 if br.P.Pos.IsStmt() != src.PosIsStmt { 5242 br.P.Pos = br.P.Pos.WithNotStmt() 5243 } 5244 } 5245 5246 if e.log { // spew to stdout 5247 filename := "" 5248 for p := pp.Text; p != nil; p = p.Link { 5249 if p.Pos.IsKnown() && p.InnermostFilename() != filename { 5250 filename = p.InnermostFilename() 5251 f.Logf("# %s\n", filename) 5252 } 5253 5254 var s string 5255 if v, ok := progToValue[p]; ok { 5256 s = v.String() 5257 } else if b, ok := progToBlock[p]; ok { 5258 s = b.String() 5259 } else { 5260 s = " " // most value and branch strings are 2-3 characters long 5261 } 5262 f.Logf(" %-6s\t%.5d (%s)\t%s\n", s, p.Pc, p.InnermostLineNumber(), p.InstructionString()) 5263 } 5264 } 5265 if f.HTMLWriter != nil { // spew to ssa.html 5266 var buf bytes.Buffer 5267 buf.WriteString("<code>") 5268 buf.WriteString("<dl class=\"ssa-gen\">") 5269 filename := "" 5270 for p := pp.Text; p != nil; p = p.Link { 5271 // Don't spam every line with the file name, which is often huge. 5272 // Only print changes, and "unknown" is not a change. 5273 if p.Pos.IsKnown() && p.InnermostFilename() != filename { 5274 filename = p.InnermostFilename() 5275 buf.WriteString("<dt class=\"ssa-prog-src\"></dt><dd class=\"ssa-prog\">") 5276 buf.WriteString(html.EscapeString("# " + filename)) 5277 buf.WriteString("</dd>") 5278 } 5279 5280 buf.WriteString("<dt class=\"ssa-prog-src\">") 5281 if v, ok := progToValue[p]; ok { 5282 buf.WriteString(v.HTML()) 5283 } else if b, ok := progToBlock[p]; ok { 5284 buf.WriteString("<b>" + b.HTML() + "</b>") 5285 } 5286 buf.WriteString("</dt>") 5287 buf.WriteString("<dd class=\"ssa-prog\">") 5288 buf.WriteString(fmt.Sprintf("%.5d <span class=\"l%v line-number\">(%s)</span> %s", p.Pc, p.InnermostLineNumber(), p.InnermostLineNumberHTML(), html.EscapeString(p.InstructionString()))) 5289 buf.WriteString("</dd>") 5290 } 5291 buf.WriteString("</dl>") 5292 buf.WriteString("</code>") 5293 f.HTMLWriter.WriteColumn("genssa", "genssa", "ssa-prog", buf.String()) 5294 } 5295 5296 defframe(&s, e) 5297 5298 f.HTMLWriter.Close() 5299 f.HTMLWriter = nil 5300 } 5301 5302 func defframe(s *SSAGenState, e *ssafn) { 5303 pp := s.pp 5304 5305 frame := Rnd(s.maxarg+e.stksize, int64(Widthreg)) 5306 if thearch.PadFrame != nil { 5307 frame = thearch.PadFrame(frame) 5308 } 5309 5310 // Fill in argument and frame size. 5311 pp.Text.To.Type = obj.TYPE_TEXTSIZE 5312 pp.Text.To.Val = int32(Rnd(e.curfn.Type.ArgWidth(), int64(Widthreg))) 5313 pp.Text.To.Offset = frame 5314 5315 // Insert code to zero ambiguously live variables so that the 5316 // garbage collector only sees initialized values when it 5317 // looks for pointers. 5318 p := pp.Text 5319 var lo, hi int64 5320 5321 // Opaque state for backend to use. Current backends use it to 5322 // keep track of which helper registers have been zeroed. 5323 var state uint32 5324 5325 // Iterate through declarations. They are sorted in decreasing Xoffset order. 5326 for _, n := range e.curfn.Func.Dcl { 5327 if !n.Name.Needzero() { 5328 continue 5329 } 5330 if n.Class() != PAUTO { 5331 Fatalf("needzero class %d", n.Class()) 5332 } 5333 if n.Type.Size()%int64(Widthptr) != 0 || n.Xoffset%int64(Widthptr) != 0 || n.Type.Size() == 0 { 5334 Fatalf("var %L has size %d offset %d", n, n.Type.Size(), n.Xoffset) 5335 } 5336 5337 if lo != hi && n.Xoffset+n.Type.Size() >= lo-int64(2*Widthreg) { 5338 // Merge with range we already have. 5339 lo = n.Xoffset 5340 continue 5341 } 5342 5343 // Zero old range 5344 p = thearch.ZeroRange(pp, p, frame+lo, hi-lo, &state) 5345 5346 // Set new range. 5347 lo = n.Xoffset 5348 hi = lo + n.Type.Size() 5349 } 5350 5351 // Zero final range. 5352 thearch.ZeroRange(pp, p, frame+lo, hi-lo, &state) 5353 } 5354 5355 type FloatingEQNEJump struct { 5356 Jump obj.As 5357 Index int 5358 } 5359 5360 func (s *SSAGenState) oneFPJump(b *ssa.Block, jumps *FloatingEQNEJump) { 5361 p := s.Prog(jumps.Jump) 5362 p.To.Type = obj.TYPE_BRANCH 5363 p.Pos = b.Pos 5364 to := jumps.Index 5365 s.Branches = append(s.Branches, Branch{p, b.Succs[to].Block()}) 5366 } 5367 5368 func (s *SSAGenState) FPJump(b, next *ssa.Block, jumps *[2][2]FloatingEQNEJump) { 5369 switch next { 5370 case b.Succs[0].Block(): 5371 s.oneFPJump(b, &jumps[0][0]) 5372 s.oneFPJump(b, &jumps[0][1]) 5373 case b.Succs[1].Block(): 5374 s.oneFPJump(b, &jumps[1][0]) 5375 s.oneFPJump(b, &jumps[1][1]) 5376 default: 5377 s.oneFPJump(b, &jumps[1][0]) 5378 s.oneFPJump(b, &jumps[1][1]) 5379 q := s.Prog(obj.AJMP) 5380 q.Pos = b.Pos 5381 q.To.Type = obj.TYPE_BRANCH 5382 s.Branches = append(s.Branches, Branch{q, b.Succs[1].Block()}) 5383 } 5384 } 5385 5386 func AuxOffset(v *ssa.Value) (offset int64) { 5387 if v.Aux == nil { 5388 return 0 5389 } 5390 n, ok := v.Aux.(*Node) 5391 if !ok { 5392 v.Fatalf("bad aux type in %s\n", v.LongString()) 5393 } 5394 if n.Class() == PAUTO { 5395 return n.Xoffset 5396 } 5397 return 0 5398 } 5399 5400 // AddAux adds the offset in the aux fields (AuxInt and Aux) of v to a. 5401 func AddAux(a *obj.Addr, v *ssa.Value) { 5402 AddAux2(a, v, v.AuxInt) 5403 } 5404 func AddAux2(a *obj.Addr, v *ssa.Value, offset int64) { 5405 if a.Type != obj.TYPE_MEM && a.Type != obj.TYPE_ADDR { 5406 v.Fatalf("bad AddAux addr %v", a) 5407 } 5408 // add integer offset 5409 a.Offset += offset 5410 5411 // If no additional symbol offset, we're done. 5412 if v.Aux == nil { 5413 return 5414 } 5415 // Add symbol's offset from its base register. 5416 switch n := v.Aux.(type) { 5417 case *obj.LSym: 5418 a.Name = obj.NAME_EXTERN 5419 a.Sym = n 5420 case *Node: 5421 if n.Class() == PPARAM || n.Class() == PPARAMOUT { 5422 a.Name = obj.NAME_PARAM 5423 a.Sym = n.Orig.Sym.Linksym() 5424 a.Offset += n.Xoffset 5425 break 5426 } 5427 a.Name = obj.NAME_AUTO 5428 a.Sym = n.Sym.Linksym() 5429 a.Offset += n.Xoffset 5430 default: 5431 v.Fatalf("aux in %s not implemented %#v", v, v.Aux) 5432 } 5433 } 5434 5435 // extendIndex extends v to a full int width. 5436 // panic using the given function if v does not fit in an int (only on 32-bit archs). 5437 func (s *state) extendIndex(v *ssa.Value, panicfn *obj.LSym) *ssa.Value { 5438 size := v.Type.Size() 5439 if size == s.config.PtrSize { 5440 return v 5441 } 5442 if size > s.config.PtrSize { 5443 // truncate 64-bit indexes on 32-bit pointer archs. Test the 5444 // high word and branch to out-of-bounds failure if it is not 0. 5445 if Debug['B'] == 0 { 5446 hi := s.newValue1(ssa.OpInt64Hi, types.Types[TUINT32], v) 5447 cmp := s.newValue2(ssa.OpEq32, types.Types[TBOOL], hi, s.constInt32(types.Types[TUINT32], 0)) 5448 s.check(cmp, panicfn) 5449 } 5450 return s.newValue1(ssa.OpTrunc64to32, types.Types[TINT], v) 5451 } 5452 5453 // Extend value to the required size 5454 var op ssa.Op 5455 if v.Type.IsSigned() { 5456 switch 10*size + s.config.PtrSize { 5457 case 14: 5458 op = ssa.OpSignExt8to32 5459 case 18: 5460 op = ssa.OpSignExt8to64 5461 case 24: 5462 op = ssa.OpSignExt16to32 5463 case 28: 5464 op = ssa.OpSignExt16to64 5465 case 48: 5466 op = ssa.OpSignExt32to64 5467 default: 5468 s.Fatalf("bad signed index extension %s", v.Type) 5469 } 5470 } else { 5471 switch 10*size + s.config.PtrSize { 5472 case 14: 5473 op = ssa.OpZeroExt8to32 5474 case 18: 5475 op = ssa.OpZeroExt8to64 5476 case 24: 5477 op = ssa.OpZeroExt16to32 5478 case 28: 5479 op = ssa.OpZeroExt16to64 5480 case 48: 5481 op = ssa.OpZeroExt32to64 5482 default: 5483 s.Fatalf("bad unsigned index extension %s", v.Type) 5484 } 5485 } 5486 return s.newValue1(op, types.Types[TINT], v) 5487 } 5488 5489 // CheckLoweredPhi checks that regalloc and stackalloc correctly handled phi values. 5490 // Called during ssaGenValue. 5491 func CheckLoweredPhi(v *ssa.Value) { 5492 if v.Op != ssa.OpPhi { 5493 v.Fatalf("CheckLoweredPhi called with non-phi value: %v", v.LongString()) 5494 } 5495 if v.Type.IsMemory() { 5496 return 5497 } 5498 f := v.Block.Func 5499 loc := f.RegAlloc[v.ID] 5500 for _, a := range v.Args { 5501 if aloc := f.RegAlloc[a.ID]; aloc != loc { // TODO: .Equal() instead? 5502 v.Fatalf("phi arg at different location than phi: %v @ %s, but arg %v @ %s\n%s\n", v, loc, a, aloc, v.Block.Func) 5503 } 5504 } 5505 } 5506 5507 // CheckLoweredGetClosurePtr checks that v is the first instruction in the function's entry block. 5508 // The output of LoweredGetClosurePtr is generally hardwired to the correct register. 5509 // That register contains the closure pointer on closure entry. 5510 func CheckLoweredGetClosurePtr(v *ssa.Value) { 5511 entry := v.Block.Func.Entry 5512 if entry != v.Block || entry.Values[0] != v { 5513 Fatalf("in %s, badly placed LoweredGetClosurePtr: %v %v", v.Block.Func.Name, v.Block, v) 5514 } 5515 } 5516 5517 // AutoVar returns a *Node and int64 representing the auto variable and offset within it 5518 // where v should be spilled. 5519 func AutoVar(v *ssa.Value) (*Node, int64) { 5520 loc := v.Block.Func.RegAlloc[v.ID].(ssa.LocalSlot) 5521 if v.Type.Size() > loc.Type.Size() { 5522 v.Fatalf("spill/restore type %s doesn't fit in slot type %s", v.Type, loc.Type) 5523 } 5524 return loc.N.(*Node), loc.Off 5525 } 5526 5527 func AddrAuto(a *obj.Addr, v *ssa.Value) { 5528 n, off := AutoVar(v) 5529 a.Type = obj.TYPE_MEM 5530 a.Sym = n.Sym.Linksym() 5531 a.Reg = int16(thearch.REGSP) 5532 a.Offset = n.Xoffset + off 5533 if n.Class() == PPARAM || n.Class() == PPARAMOUT { 5534 a.Name = obj.NAME_PARAM 5535 } else { 5536 a.Name = obj.NAME_AUTO 5537 } 5538 } 5539 5540 func (s *SSAGenState) AddrScratch(a *obj.Addr) { 5541 if s.ScratchFpMem == nil { 5542 panic("no scratch memory available; forgot to declare usesScratch for Op?") 5543 } 5544 a.Type = obj.TYPE_MEM 5545 a.Name = obj.NAME_AUTO 5546 a.Sym = s.ScratchFpMem.Sym.Linksym() 5547 a.Reg = int16(thearch.REGSP) 5548 a.Offset = s.ScratchFpMem.Xoffset 5549 } 5550 5551 // Call returns a new CALL instruction for the SSA value v. 5552 // It uses PrepareCall to prepare the call. 5553 func (s *SSAGenState) Call(v *ssa.Value) *obj.Prog { 5554 s.PrepareCall(v) 5555 5556 p := s.Prog(obj.ACALL) 5557 p.Pos = v.Pos 5558 if sym, ok := v.Aux.(*obj.LSym); ok { 5559 p.To.Type = obj.TYPE_MEM 5560 p.To.Name = obj.NAME_EXTERN 5561 p.To.Sym = sym 5562 } else { 5563 // TODO(mdempsky): Can these differences be eliminated? 5564 switch thearch.LinkArch.Family { 5565 case sys.AMD64, sys.I386, sys.PPC64, sys.S390X, sys.Wasm: 5566 p.To.Type = obj.TYPE_REG 5567 case sys.ARM, sys.ARM64, sys.MIPS, sys.MIPS64: 5568 p.To.Type = obj.TYPE_MEM 5569 default: 5570 Fatalf("unknown indirect call family") 5571 } 5572 p.To.Reg = v.Args[0].Reg() 5573 } 5574 return p 5575 } 5576 5577 // PrepareCall prepares to emit a CALL instruction for v and does call-related bookkeeping. 5578 // It must be called immediately before emitting the actual CALL instruction, 5579 // since it emits PCDATA for the stack map at the call (calls are safe points). 5580 func (s *SSAGenState) PrepareCall(v *ssa.Value) { 5581 idx := s.livenessMap.Get(v) 5582 if !idx.Valid() { 5583 // typedmemclr and typedmemmove are write barriers and 5584 // deeply non-preemptible. They are unsafe points and 5585 // hence should not have liveness maps. 5586 if sym, _ := v.Aux.(*obj.LSym); !(sym == typedmemclr || sym == typedmemmove) { 5587 Fatalf("missing stack map index for %v", v.LongString()) 5588 } 5589 } 5590 5591 if sym, _ := v.Aux.(*obj.LSym); sym == Deferreturn { 5592 // Deferred calls will appear to be returning to 5593 // the CALL deferreturn(SB) that we are about to emit. 5594 // However, the stack trace code will show the line 5595 // of the instruction byte before the return PC. 5596 // To avoid that being an unrelated instruction, 5597 // insert an actual hardware NOP that will have the right line number. 5598 // This is different from obj.ANOP, which is a virtual no-op 5599 // that doesn't make it into the instruction stream. 5600 thearch.Ginsnopdefer(s.pp) 5601 } 5602 5603 if sym, ok := v.Aux.(*obj.LSym); ok { 5604 // Record call graph information for nowritebarrierrec 5605 // analysis. 5606 if nowritebarrierrecCheck != nil { 5607 nowritebarrierrecCheck.recordCall(s.pp.curfn, sym, v.Pos) 5608 } 5609 } 5610 5611 if s.maxarg < v.AuxInt { 5612 s.maxarg = v.AuxInt 5613 } 5614 } 5615 5616 // fieldIdx finds the index of the field referred to by the ODOT node n. 5617 func fieldIdx(n *Node) int { 5618 t := n.Left.Type 5619 f := n.Sym 5620 if !t.IsStruct() { 5621 panic("ODOT's LHS is not a struct") 5622 } 5623 5624 var i int 5625 for _, t1 := range t.Fields().Slice() { 5626 if t1.Sym != f { 5627 i++ 5628 continue 5629 } 5630 if t1.Offset != n.Xoffset { 5631 panic("field offset doesn't match") 5632 } 5633 return i 5634 } 5635 panic(fmt.Sprintf("can't find field in expr %v\n", n)) 5636 5637 // TODO: keep the result of this function somewhere in the ODOT Node 5638 // so we don't have to recompute it each time we need it. 5639 } 5640 5641 // ssafn holds frontend information about a function that the backend is processing. 5642 // It also exports a bunch of compiler services for the ssa backend. 5643 type ssafn struct { 5644 curfn *Node 5645 strings map[string]interface{} // map from constant string to data symbols 5646 scratchFpMem *Node // temp for floating point register / memory moves on some architectures 5647 stksize int64 // stack size for current frame 5648 stkptrsize int64 // prefix of stack containing pointers 5649 log bool // print ssa debug to the stdout 5650 } 5651 5652 // StringData returns a symbol (a *types.Sym wrapped in an interface) which 5653 // is the data component of a global string constant containing s. 5654 func (e *ssafn) StringData(s string) interface{} { 5655 if aux, ok := e.strings[s]; ok { 5656 return aux 5657 } 5658 if e.strings == nil { 5659 e.strings = make(map[string]interface{}) 5660 } 5661 data := stringsym(e.curfn.Pos, s) 5662 e.strings[s] = data 5663 return data 5664 } 5665 5666 func (e *ssafn) Auto(pos src.XPos, t *types.Type) ssa.GCNode { 5667 n := tempAt(pos, e.curfn, t) // Note: adds new auto to e.curfn.Func.Dcl list 5668 return n 5669 } 5670 5671 func (e *ssafn) SplitString(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot) { 5672 n := name.N.(*Node) 5673 ptrType := types.NewPtr(types.Types[TUINT8]) 5674 lenType := types.Types[TINT] 5675 if n.Class() == PAUTO && !n.Addrtaken() { 5676 // Split this string up into two separate variables. 5677 p := e.splitSlot(&name, ".ptr", 0, ptrType) 5678 l := e.splitSlot(&name, ".len", ptrType.Size(), lenType) 5679 return p, l 5680 } 5681 // Return the two parts of the larger variable. 5682 return ssa.LocalSlot{N: n, Type: ptrType, Off: name.Off}, ssa.LocalSlot{N: n, Type: lenType, Off: name.Off + int64(Widthptr)} 5683 } 5684 5685 func (e *ssafn) SplitInterface(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot) { 5686 n := name.N.(*Node) 5687 u := types.Types[TUINTPTR] 5688 t := types.NewPtr(types.Types[TUINT8]) 5689 if n.Class() == PAUTO && !n.Addrtaken() { 5690 // Split this interface up into two separate variables. 5691 f := ".itab" 5692 if n.Type.IsEmptyInterface() { 5693 f = ".type" 5694 } 5695 c := e.splitSlot(&name, f, 0, u) // see comment in plive.go:onebitwalktype1. 5696 d := e.splitSlot(&name, ".data", u.Size(), t) 5697 return c, d 5698 } 5699 // Return the two parts of the larger variable. 5700 return ssa.LocalSlot{N: n, Type: u, Off: name.Off}, ssa.LocalSlot{N: n, Type: t, Off: name.Off + int64(Widthptr)} 5701 } 5702 5703 func (e *ssafn) SplitSlice(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot, ssa.LocalSlot) { 5704 n := name.N.(*Node) 5705 ptrType := types.NewPtr(name.Type.Elem()) 5706 lenType := types.Types[TINT] 5707 if n.Class() == PAUTO && !n.Addrtaken() { 5708 // Split this slice up into three separate variables. 5709 p := e.splitSlot(&name, ".ptr", 0, ptrType) 5710 l := e.splitSlot(&name, ".len", ptrType.Size(), lenType) 5711 c := e.splitSlot(&name, ".cap", ptrType.Size()+lenType.Size(), lenType) 5712 return p, l, c 5713 } 5714 // Return the three parts of the larger variable. 5715 return ssa.LocalSlot{N: n, Type: ptrType, Off: name.Off}, 5716 ssa.LocalSlot{N: n, Type: lenType, Off: name.Off + int64(Widthptr)}, 5717 ssa.LocalSlot{N: n, Type: lenType, Off: name.Off + int64(2*Widthptr)} 5718 } 5719 5720 func (e *ssafn) SplitComplex(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot) { 5721 n := name.N.(*Node) 5722 s := name.Type.Size() / 2 5723 var t *types.Type 5724 if s == 8 { 5725 t = types.Types[TFLOAT64] 5726 } else { 5727 t = types.Types[TFLOAT32] 5728 } 5729 if n.Class() == PAUTO && !n.Addrtaken() { 5730 // Split this complex up into two separate variables. 5731 r := e.splitSlot(&name, ".real", 0, t) 5732 i := e.splitSlot(&name, ".imag", t.Size(), t) 5733 return r, i 5734 } 5735 // Return the two parts of the larger variable. 5736 return ssa.LocalSlot{N: n, Type: t, Off: name.Off}, ssa.LocalSlot{N: n, Type: t, Off: name.Off + s} 5737 } 5738 5739 func (e *ssafn) SplitInt64(name ssa.LocalSlot) (ssa.LocalSlot, ssa.LocalSlot) { 5740 n := name.N.(*Node) 5741 var t *types.Type 5742 if name.Type.IsSigned() { 5743 t = types.Types[TINT32] 5744 } else { 5745 t = types.Types[TUINT32] 5746 } 5747 if n.Class() == PAUTO && !n.Addrtaken() { 5748 // Split this int64 up into two separate variables. 5749 if thearch.LinkArch.ByteOrder == binary.BigEndian { 5750 return e.splitSlot(&name, ".hi", 0, t), e.splitSlot(&name, ".lo", t.Size(), types.Types[TUINT32]) 5751 } 5752 return e.splitSlot(&name, ".hi", t.Size(), t), e.splitSlot(&name, ".lo", 0, types.Types[TUINT32]) 5753 } 5754 // Return the two parts of the larger variable. 5755 if thearch.LinkArch.ByteOrder == binary.BigEndian { 5756 return ssa.LocalSlot{N: n, Type: t, Off: name.Off}, ssa.LocalSlot{N: n, Type: types.Types[TUINT32], Off: name.Off + 4} 5757 } 5758 return ssa.LocalSlot{N: n, Type: t, Off: name.Off + 4}, ssa.LocalSlot{N: n, Type: types.Types[TUINT32], Off: name.Off} 5759 } 5760 5761 func (e *ssafn) SplitStruct(name ssa.LocalSlot, i int) ssa.LocalSlot { 5762 n := name.N.(*Node) 5763 st := name.Type 5764 ft := st.FieldType(i) 5765 var offset int64 5766 for f := 0; f < i; f++ { 5767 offset += st.FieldType(f).Size() 5768 } 5769 if n.Class() == PAUTO && !n.Addrtaken() { 5770 // Note: the _ field may appear several times. But 5771 // have no fear, identically-named but distinct Autos are 5772 // ok, albeit maybe confusing for a debugger. 5773 return e.splitSlot(&name, "."+st.FieldName(i), offset, ft) 5774 } 5775 return ssa.LocalSlot{N: n, Type: ft, Off: name.Off + st.FieldOff(i)} 5776 } 5777 5778 func (e *ssafn) SplitArray(name ssa.LocalSlot) ssa.LocalSlot { 5779 n := name.N.(*Node) 5780 at := name.Type 5781 if at.NumElem() != 1 { 5782 Fatalf("bad array size") 5783 } 5784 et := at.Elem() 5785 if n.Class() == PAUTO && !n.Addrtaken() { 5786 return e.splitSlot(&name, "[0]", 0, et) 5787 } 5788 return ssa.LocalSlot{N: n, Type: et, Off: name.Off} 5789 } 5790 5791 func (e *ssafn) DerefItab(it *obj.LSym, offset int64) *obj.LSym { 5792 return itabsym(it, offset) 5793 } 5794 5795 // splitSlot returns a slot representing the data of parent starting at offset. 5796 func (e *ssafn) splitSlot(parent *ssa.LocalSlot, suffix string, offset int64, t *types.Type) ssa.LocalSlot { 5797 s := &types.Sym{Name: parent.N.(*Node).Sym.Name + suffix, Pkg: localpkg} 5798 5799 n := &Node{ 5800 Name: new(Name), 5801 Op: ONAME, 5802 Pos: parent.N.(*Node).Pos, 5803 } 5804 n.Orig = n 5805 5806 s.Def = asTypesNode(n) 5807 asNode(s.Def).Name.SetUsed(true) 5808 n.Sym = s 5809 n.Type = t 5810 n.SetClass(PAUTO) 5811 n.SetAddable(true) 5812 n.Esc = EscNever 5813 n.Name.Curfn = e.curfn 5814 e.curfn.Func.Dcl = append(e.curfn.Func.Dcl, n) 5815 dowidth(t) 5816 return ssa.LocalSlot{N: n, Type: t, Off: 0, SplitOf: parent, SplitOffset: offset} 5817 } 5818 5819 func (e *ssafn) CanSSA(t *types.Type) bool { 5820 return canSSAType(t) 5821 } 5822 5823 func (e *ssafn) Line(pos src.XPos) string { 5824 return linestr(pos) 5825 } 5826 5827 // Log logs a message from the compiler. 5828 func (e *ssafn) Logf(msg string, args ...interface{}) { 5829 if e.log { 5830 fmt.Printf(msg, args...) 5831 } 5832 } 5833 5834 func (e *ssafn) Log() bool { 5835 return e.log 5836 } 5837 5838 // Fatal reports a compiler error and exits. 5839 func (e *ssafn) Fatalf(pos src.XPos, msg string, args ...interface{}) { 5840 lineno = pos 5841 nargs := append([]interface{}{e.curfn.funcname()}, args...) 5842 Fatalf("'%s': "+msg, nargs...) 5843 } 5844 5845 // Warnl reports a "warning", which is usually flag-triggered 5846 // logging output for the benefit of tests. 5847 func (e *ssafn) Warnl(pos src.XPos, fmt_ string, args ...interface{}) { 5848 Warnl(pos, fmt_, args...) 5849 } 5850 5851 func (e *ssafn) Debug_checknil() bool { 5852 return Debug_checknil != 0 5853 } 5854 5855 func (e *ssafn) UseWriteBarrier() bool { 5856 return use_writebarrier 5857 } 5858 5859 func (e *ssafn) Syslook(name string) *obj.LSym { 5860 switch name { 5861 case "goschedguarded": 5862 return goschedguarded 5863 case "writeBarrier": 5864 return writeBarrier 5865 case "gcWriteBarrier": 5866 return gcWriteBarrier 5867 case "typedmemmove": 5868 return typedmemmove 5869 case "typedmemclr": 5870 return typedmemclr 5871 } 5872 Fatalf("unknown Syslook func %v", name) 5873 return nil 5874 } 5875 5876 func (e *ssafn) SetWBPos(pos src.XPos) { 5877 e.curfn.Func.setWBPos(pos) 5878 } 5879 5880 func (n *Node) Typ() *types.Type { 5881 return n.Type 5882 } 5883 func (n *Node) StorageClass() ssa.StorageClass { 5884 switch n.Class() { 5885 case PPARAM: 5886 return ssa.ClassParam 5887 case PPARAMOUT: 5888 return ssa.ClassParamOut 5889 case PAUTO: 5890 return ssa.ClassAuto 5891 default: 5892 Fatalf("untranslatable storage class for %v: %s", n, n.Class()) 5893 return 0 5894 } 5895 }