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