github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/go/ssa/func.go (about) 1 // Copyright 2013 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 // +build go1.5 6 7 package ssa 8 9 // This file implements the Function and BasicBlock types. 10 11 import ( 12 "bytes" 13 "fmt" 14 "go/ast" 15 "go/token" 16 "go/types" 17 "io" 18 "os" 19 "strings" 20 ) 21 22 // addEdge adds a control-flow graph edge from from to to. 23 func addEdge(from, to *BasicBlock) { 24 from.Succs = append(from.Succs, to) 25 to.Preds = append(to.Preds, from) 26 } 27 28 // Parent returns the function that contains block b. 29 func (b *BasicBlock) Parent() *Function { return b.parent } 30 31 // String returns a human-readable label of this block. 32 // It is not guaranteed unique within the function. 33 // 34 func (b *BasicBlock) String() string { 35 return fmt.Sprintf("%d", b.Index) 36 } 37 38 // emit appends an instruction to the current basic block. 39 // If the instruction defines a Value, it is returned. 40 // 41 func (b *BasicBlock) emit(i Instruction) Value { 42 i.setBlock(b) 43 b.Instrs = append(b.Instrs, i) 44 v, _ := i.(Value) 45 return v 46 } 47 48 // predIndex returns the i such that b.Preds[i] == c or panics if 49 // there is none. 50 func (b *BasicBlock) predIndex(c *BasicBlock) int { 51 for i, pred := range b.Preds { 52 if pred == c { 53 return i 54 } 55 } 56 panic(fmt.Sprintf("no edge %s -> %s", c, b)) 57 } 58 59 // hasPhi returns true if b.Instrs contains φ-nodes. 60 func (b *BasicBlock) hasPhi() bool { 61 _, ok := b.Instrs[0].(*Phi) 62 return ok 63 } 64 65 // phis returns the prefix of b.Instrs containing all the block's φ-nodes. 66 func (b *BasicBlock) phis() []Instruction { 67 for i, instr := range b.Instrs { 68 if _, ok := instr.(*Phi); !ok { 69 return b.Instrs[:i] 70 } 71 } 72 return nil // unreachable in well-formed blocks 73 } 74 75 // replacePred replaces all occurrences of p in b's predecessor list with q. 76 // Ordinarily there should be at most one. 77 // 78 func (b *BasicBlock) replacePred(p, q *BasicBlock) { 79 for i, pred := range b.Preds { 80 if pred == p { 81 b.Preds[i] = q 82 } 83 } 84 } 85 86 // replaceSucc replaces all occurrences of p in b's successor list with q. 87 // Ordinarily there should be at most one. 88 // 89 func (b *BasicBlock) replaceSucc(p, q *BasicBlock) { 90 for i, succ := range b.Succs { 91 if succ == p { 92 b.Succs[i] = q 93 } 94 } 95 } 96 97 // removePred removes all occurrences of p in b's 98 // predecessor list and φ-nodes. 99 // Ordinarily there should be at most one. 100 // 101 func (b *BasicBlock) removePred(p *BasicBlock) { 102 phis := b.phis() 103 104 // We must preserve edge order for φ-nodes. 105 j := 0 106 for i, pred := range b.Preds { 107 if pred != p { 108 b.Preds[j] = b.Preds[i] 109 // Strike out φ-edge too. 110 for _, instr := range phis { 111 phi := instr.(*Phi) 112 phi.Edges[j] = phi.Edges[i] 113 } 114 j++ 115 } 116 } 117 // Nil out b.Preds[j:] and φ-edges[j:] to aid GC. 118 for i := j; i < len(b.Preds); i++ { 119 b.Preds[i] = nil 120 for _, instr := range phis { 121 instr.(*Phi).Edges[i] = nil 122 } 123 } 124 b.Preds = b.Preds[:j] 125 for _, instr := range phis { 126 phi := instr.(*Phi) 127 phi.Edges = phi.Edges[:j] 128 } 129 } 130 131 // Destinations associated with unlabelled for/switch/select stmts. 132 // We push/pop one of these as we enter/leave each construct and for 133 // each BranchStmt we scan for the innermost target of the right type. 134 // 135 type targets struct { 136 tail *targets // rest of stack 137 _break *BasicBlock 138 _continue *BasicBlock 139 _fallthrough *BasicBlock 140 } 141 142 // Destinations associated with a labelled block. 143 // We populate these as labels are encountered in forward gotos or 144 // labelled statements. 145 // 146 type lblock struct { 147 _goto *BasicBlock 148 _break *BasicBlock 149 _continue *BasicBlock 150 } 151 152 // labelledBlock returns the branch target associated with the 153 // specified label, creating it if needed. 154 // 155 func (f *Function) labelledBlock(label *ast.Ident) *lblock { 156 lb := f.lblocks[label.Obj] 157 if lb == nil { 158 lb = &lblock{_goto: f.newBasicBlock(label.Name)} 159 if f.lblocks == nil { 160 f.lblocks = make(map[*ast.Object]*lblock) 161 } 162 f.lblocks[label.Obj] = lb 163 } 164 return lb 165 } 166 167 // addParam adds a (non-escaping) parameter to f.Params of the 168 // specified name, type and source position. 169 // 170 func (f *Function) addParam(name string, typ types.Type, pos token.Pos) *Parameter { 171 v := &Parameter{ 172 name: name, 173 typ: typ, 174 pos: pos, 175 parent: f, 176 } 177 f.Params = append(f.Params, v) 178 return v 179 } 180 181 func (f *Function) addParamObj(obj types.Object) *Parameter { 182 name := obj.Name() 183 if name == "" { 184 name = fmt.Sprintf("arg%d", len(f.Params)) 185 } 186 param := f.addParam(name, obj.Type(), obj.Pos()) 187 param.object = obj 188 return param 189 } 190 191 // addSpilledParam declares a parameter that is pre-spilled to the 192 // stack; the function body will load/store the spilled location. 193 // Subsequent lifting will eliminate spills where possible. 194 // 195 func (f *Function) addSpilledParam(obj types.Object) { 196 param := f.addParamObj(obj) 197 spill := &Alloc{Comment: obj.Name()} 198 spill.setType(types.NewPointer(obj.Type())) 199 spill.setPos(obj.Pos()) 200 f.objects[obj] = spill 201 f.Locals = append(f.Locals, spill) 202 f.emit(spill) 203 f.emit(&Store{Addr: spill, Val: param}) 204 } 205 206 // startBody initializes the function prior to generating SSA code for its body. 207 // Precondition: f.Type() already set. 208 // 209 func (f *Function) startBody() { 210 f.currentBlock = f.newBasicBlock("entry") 211 f.objects = make(map[types.Object]Value) // needed for some synthetics, e.g. init 212 } 213 214 // createSyntacticParams populates f.Params and generates code (spills 215 // and named result locals) for all the parameters declared in the 216 // syntax. In addition it populates the f.objects mapping. 217 // 218 // Preconditions: 219 // f.startBody() was called. 220 // Postcondition: 221 // len(f.Params) == len(f.Signature.Params) + (f.Signature.Recv() ? 1 : 0) 222 // 223 func (f *Function) createSyntacticParams(recv *ast.FieldList, functype *ast.FuncType) { 224 // Receiver (at most one inner iteration). 225 if recv != nil { 226 for _, field := range recv.List { 227 for _, n := range field.Names { 228 f.addSpilledParam(f.Pkg.info.Defs[n]) 229 } 230 // Anonymous receiver? No need to spill. 231 if field.Names == nil { 232 f.addParamObj(f.Signature.Recv()) 233 } 234 } 235 } 236 237 // Parameters. 238 if functype.Params != nil { 239 n := len(f.Params) // 1 if has recv, 0 otherwise 240 for _, field := range functype.Params.List { 241 for _, n := range field.Names { 242 f.addSpilledParam(f.Pkg.info.Defs[n]) 243 } 244 // Anonymous parameter? No need to spill. 245 if field.Names == nil { 246 f.addParamObj(f.Signature.Params().At(len(f.Params) - n)) 247 } 248 } 249 } 250 251 // Named results. 252 if functype.Results != nil { 253 for _, field := range functype.Results.List { 254 // Implicit "var" decl of locals for named results. 255 for _, n := range field.Names { 256 f.namedResults = append(f.namedResults, f.addLocalForIdent(n)) 257 } 258 } 259 } 260 } 261 262 // numberRegisters assigns numbers to all SSA registers 263 // (value-defining Instructions) in f, to aid debugging. 264 // (Non-Instruction Values are named at construction.) 265 // 266 func numberRegisters(f *Function) { 267 v := 0 268 for _, b := range f.Blocks { 269 for _, instr := range b.Instrs { 270 switch instr.(type) { 271 case Value: 272 instr.(interface { 273 setNum(int) 274 }).setNum(v) 275 v++ 276 } 277 } 278 } 279 } 280 281 // buildReferrers populates the def/use information in all non-nil 282 // Value.Referrers slice. 283 // Precondition: all such slices are initially empty. 284 func buildReferrers(f *Function) { 285 var rands []*Value 286 for _, b := range f.Blocks { 287 for _, instr := range b.Instrs { 288 rands = instr.Operands(rands[:0]) // recycle storage 289 for _, rand := range rands { 290 if r := *rand; r != nil { 291 if ref := r.Referrers(); ref != nil { 292 *ref = append(*ref, instr) 293 } 294 } 295 } 296 } 297 } 298 } 299 300 // finishBody() finalizes the function after SSA code generation of its body. 301 func (f *Function) finishBody() { 302 f.objects = nil 303 f.currentBlock = nil 304 f.lblocks = nil 305 306 // Don't pin the AST in memory (except in debug mode). 307 if n := f.syntax; n != nil && !f.debugInfo() { 308 f.syntax = extentNode{n.Pos(), n.End()} 309 } 310 311 // Remove from f.Locals any Allocs that escape to the heap. 312 j := 0 313 for _, l := range f.Locals { 314 if !l.Heap { 315 f.Locals[j] = l 316 j++ 317 } 318 } 319 // Nil out f.Locals[j:] to aid GC. 320 for i := j; i < len(f.Locals); i++ { 321 f.Locals[i] = nil 322 } 323 f.Locals = f.Locals[:j] 324 325 optimizeBlocks(f) 326 327 buildReferrers(f) 328 329 buildDomTree(f) 330 331 if f.Prog.mode&NaiveForm == 0 { 332 // For debugging pre-state of lifting pass: 333 // numberRegisters(f) 334 // f.WriteTo(os.Stderr) 335 lift(f) 336 } 337 338 f.namedResults = nil // (used by lifting) 339 340 numberRegisters(f) 341 342 if f.Prog.mode&PrintFunctions != 0 { 343 printMu.Lock() 344 f.WriteTo(os.Stdout) 345 printMu.Unlock() 346 } 347 348 if f.Prog.mode&SanityCheckFunctions != 0 { 349 mustSanityCheck(f, nil) 350 } 351 } 352 353 // removeNilBlocks eliminates nils from f.Blocks and updates each 354 // BasicBlock.Index. Use this after any pass that may delete blocks. 355 // 356 func (f *Function) removeNilBlocks() { 357 j := 0 358 for _, b := range f.Blocks { 359 if b != nil { 360 b.Index = j 361 f.Blocks[j] = b 362 j++ 363 } 364 } 365 // Nil out f.Blocks[j:] to aid GC. 366 for i := j; i < len(f.Blocks); i++ { 367 f.Blocks[i] = nil 368 } 369 f.Blocks = f.Blocks[:j] 370 } 371 372 // SetDebugMode sets the debug mode for package pkg. If true, all its 373 // functions will include full debug info. This greatly increases the 374 // size of the instruction stream, and causes Functions to depend upon 375 // the ASTs, potentially keeping them live in memory for longer. 376 // 377 func (pkg *Package) SetDebugMode(debug bool) { 378 // TODO(adonovan): do we want ast.File granularity? 379 pkg.debug = debug 380 } 381 382 // debugInfo reports whether debug info is wanted for this function. 383 func (f *Function) debugInfo() bool { 384 return f.Pkg != nil && f.Pkg.debug 385 } 386 387 // addNamedLocal creates a local variable, adds it to function f and 388 // returns it. Its name and type are taken from obj. Subsequent 389 // calls to f.lookup(obj) will return the same local. 390 // 391 func (f *Function) addNamedLocal(obj types.Object) *Alloc { 392 l := f.addLocal(obj.Type(), obj.Pos()) 393 l.Comment = obj.Name() 394 f.objects[obj] = l 395 return l 396 } 397 398 func (f *Function) addLocalForIdent(id *ast.Ident) *Alloc { 399 return f.addNamedLocal(f.Pkg.info.Defs[id]) 400 } 401 402 // addLocal creates an anonymous local variable of type typ, adds it 403 // to function f and returns it. pos is the optional source location. 404 // 405 func (f *Function) addLocal(typ types.Type, pos token.Pos) *Alloc { 406 v := &Alloc{} 407 v.setType(types.NewPointer(typ)) 408 v.setPos(pos) 409 f.Locals = append(f.Locals, v) 410 f.emit(v) 411 return v 412 } 413 414 // lookup returns the address of the named variable identified by obj 415 // that is local to function f or one of its enclosing functions. 416 // If escaping, the reference comes from a potentially escaping pointer 417 // expression and the referent must be heap-allocated. 418 // 419 func (f *Function) lookup(obj types.Object, escaping bool) Value { 420 if v, ok := f.objects[obj]; ok { 421 if alloc, ok := v.(*Alloc); ok && escaping { 422 alloc.Heap = true 423 } 424 return v // function-local var (address) 425 } 426 427 // Definition must be in an enclosing function; 428 // plumb it through intervening closures. 429 if f.parent == nil { 430 panic("no ssa.Value for " + obj.String()) 431 } 432 outer := f.parent.lookup(obj, true) // escaping 433 v := &FreeVar{ 434 name: obj.Name(), 435 typ: outer.Type(), 436 pos: outer.Pos(), 437 outer: outer, 438 parent: f, 439 } 440 f.objects[obj] = v 441 f.FreeVars = append(f.FreeVars, v) 442 return v 443 } 444 445 // emit emits the specified instruction to function f. 446 func (f *Function) emit(instr Instruction) Value { 447 return f.currentBlock.emit(instr) 448 } 449 450 // RelString returns the full name of this function, qualified by 451 // package name, receiver type, etc. 452 // 453 // The specific formatting rules are not guaranteed and may change. 454 // 455 // Examples: 456 // "math.IsNaN" // a package-level function 457 // "(*bytes.Buffer).Bytes" // a declared method or a wrapper 458 // "(*bytes.Buffer).Bytes$thunk" // thunk (func wrapping method; receiver is param 0) 459 // "(*bytes.Buffer).Bytes$bound" // bound (func wrapping method; receiver supplied by closure) 460 // "main.main$1" // an anonymous function in main 461 // "main.init#1" // a declared init function 462 // "main.init" // the synthesized package initializer 463 // 464 // When these functions are referred to from within the same package 465 // (i.e. from == f.Pkg.Object), they are rendered without the package path. 466 // For example: "IsNaN", "(*Buffer).Bytes", etc. 467 // 468 // All non-synthetic functions have distinct package-qualified names. 469 // (But two methods may have the same name "(T).f" if one is a synthetic 470 // wrapper promoting a non-exported method "f" from another package; in 471 // that case, the strings are equal but the identifiers "f" are distinct.) 472 // 473 func (f *Function) RelString(from *types.Package) string { 474 // Anonymous? 475 if f.parent != nil { 476 // An anonymous function's Name() looks like "parentName$1", 477 // but its String() should include the type/package/etc. 478 parent := f.parent.RelString(from) 479 for i, anon := range f.parent.AnonFuncs { 480 if anon == f { 481 return fmt.Sprintf("%s$%d", parent, 1+i) 482 } 483 } 484 485 return f.name // should never happen 486 } 487 488 // Method (declared or wrapper)? 489 if recv := f.Signature.Recv(); recv != nil { 490 return f.relMethod(from, recv.Type()) 491 } 492 493 // Thunk? 494 if f.method != nil { 495 return f.relMethod(from, f.method.Recv()) 496 } 497 498 // Bound? 499 if len(f.FreeVars) == 1 && strings.HasSuffix(f.name, "$bound") { 500 return f.relMethod(from, f.FreeVars[0].Type()) 501 } 502 503 // Package-level function? 504 // Prefix with package name for cross-package references only. 505 if p := f.pkg(); p != nil && p != from { 506 return fmt.Sprintf("%s.%s", p.Path(), f.name) 507 } 508 509 // Unknown. 510 return f.name 511 } 512 513 func (f *Function) relMethod(from *types.Package, recv types.Type) string { 514 return fmt.Sprintf("(%s).%s", relType(recv, from), f.name) 515 } 516 517 // writeSignature writes to buf the signature sig in declaration syntax. 518 func writeSignature(buf *bytes.Buffer, from *types.Package, name string, sig *types.Signature, params []*Parameter) { 519 buf.WriteString("func ") 520 if recv := sig.Recv(); recv != nil { 521 buf.WriteString("(") 522 if n := params[0].Name(); n != "" { 523 buf.WriteString(n) 524 buf.WriteString(" ") 525 } 526 types.WriteType(buf, params[0].Type(), types.RelativeTo(from)) 527 buf.WriteString(") ") 528 } 529 buf.WriteString(name) 530 types.WriteSignature(buf, sig, types.RelativeTo(from)) 531 } 532 533 func (f *Function) pkg() *types.Package { 534 if f.Pkg != nil { 535 return f.Pkg.Pkg 536 } 537 return nil 538 } 539 540 var _ io.WriterTo = (*Function)(nil) // *Function implements io.Writer 541 542 func (f *Function) WriteTo(w io.Writer) (int64, error) { 543 var buf bytes.Buffer 544 WriteFunction(&buf, f) 545 n, err := w.Write(buf.Bytes()) 546 return int64(n), err 547 } 548 549 // WriteFunction writes to buf a human-readable "disassembly" of f. 550 func WriteFunction(buf *bytes.Buffer, f *Function) { 551 fmt.Fprintf(buf, "# Name: %s\n", f.String()) 552 if f.Pkg != nil { 553 fmt.Fprintf(buf, "# Package: %s\n", f.Pkg.Pkg.Path()) 554 } 555 if syn := f.Synthetic; syn != "" { 556 fmt.Fprintln(buf, "# Synthetic:", syn) 557 } 558 if pos := f.Pos(); pos.IsValid() { 559 fmt.Fprintf(buf, "# Location: %s\n", f.Prog.Fset.Position(pos)) 560 } 561 562 if f.parent != nil { 563 fmt.Fprintf(buf, "# Parent: %s\n", f.parent.Name()) 564 } 565 566 if f.Recover != nil { 567 fmt.Fprintf(buf, "# Recover: %s\n", f.Recover) 568 } 569 570 from := f.pkg() 571 572 if f.FreeVars != nil { 573 buf.WriteString("# Free variables:\n") 574 for i, fv := range f.FreeVars { 575 fmt.Fprintf(buf, "# % 3d:\t%s %s\n", i, fv.Name(), relType(fv.Type(), from)) 576 } 577 } 578 579 if len(f.Locals) > 0 { 580 buf.WriteString("# Locals:\n") 581 for i, l := range f.Locals { 582 fmt.Fprintf(buf, "# % 3d:\t%s %s\n", i, l.Name(), relType(deref(l.Type()), from)) 583 } 584 } 585 writeSignature(buf, from, f.Name(), f.Signature, f.Params) 586 buf.WriteString(":\n") 587 588 if f.Blocks == nil { 589 buf.WriteString("\t(external)\n") 590 } 591 592 // NB. column calculations are confused by non-ASCII 593 // characters and assume 8-space tabs. 594 const punchcard = 80 // for old time's sake. 595 const tabwidth = 8 596 for _, b := range f.Blocks { 597 if b == nil { 598 // Corrupt CFG. 599 fmt.Fprintf(buf, ".nil:\n") 600 continue 601 } 602 n, _ := fmt.Fprintf(buf, "%d:", b.Index) 603 bmsg := fmt.Sprintf("%s P:%d S:%d", b.Comment, len(b.Preds), len(b.Succs)) 604 fmt.Fprintf(buf, "%*s%s\n", punchcard-1-n-len(bmsg), "", bmsg) 605 606 if false { // CFG debugging 607 fmt.Fprintf(buf, "\t# CFG: %s --> %s --> %s\n", b.Preds, b, b.Succs) 608 } 609 for _, instr := range b.Instrs { 610 buf.WriteString("\t") 611 switch v := instr.(type) { 612 case Value: 613 l := punchcard - tabwidth 614 // Left-align the instruction. 615 if name := v.Name(); name != "" { 616 n, _ := fmt.Fprintf(buf, "%s = ", name) 617 l -= n 618 } 619 n, _ := buf.WriteString(instr.String()) 620 l -= n 621 // Right-align the type if there's space. 622 if t := v.Type(); t != nil { 623 buf.WriteByte(' ') 624 ts := relType(t, from) 625 l -= len(ts) + len(" ") // (spaces before and after type) 626 if l > 0 { 627 fmt.Fprintf(buf, "%*s", l, "") 628 } 629 buf.WriteString(ts) 630 } 631 case nil: 632 // Be robust against bad transforms. 633 buf.WriteString("<deleted>") 634 default: 635 buf.WriteString(instr.String()) 636 } 637 buf.WriteString("\n") 638 } 639 } 640 fmt.Fprintf(buf, "\n") 641 } 642 643 // newBasicBlock adds to f a new basic block and returns it. It does 644 // not automatically become the current block for subsequent calls to emit. 645 // comment is an optional string for more readable debugging output. 646 // 647 func (f *Function) newBasicBlock(comment string) *BasicBlock { 648 b := &BasicBlock{ 649 Index: len(f.Blocks), 650 Comment: comment, 651 parent: f, 652 } 653 b.Succs = b.succs2[:0] 654 f.Blocks = append(f.Blocks, b) 655 return b 656 } 657 658 // NewFunction returns a new synthetic Function instance belonging to 659 // prog, with its name and signature fields set as specified. 660 // 661 // The caller is responsible for initializing the remaining fields of 662 // the function object, e.g. Pkg, Params, Blocks. 663 // 664 // It is practically impossible for clients to construct well-formed 665 // SSA functions/packages/programs directly, so we assume this is the 666 // job of the Builder alone. NewFunction exists to provide clients a 667 // little flexibility. For example, analysis tools may wish to 668 // construct fake Functions for the root of the callgraph, a fake 669 // "reflect" package, etc. 670 // 671 // TODO(adonovan): think harder about the API here. 672 // 673 func (prog *Program) NewFunction(name string, sig *types.Signature, provenance string) *Function { 674 return &Function{Prog: prog, name: name, Signature: sig, Synthetic: provenance} 675 } 676 677 type extentNode [2]token.Pos 678 679 func (n extentNode) Pos() token.Pos { return n[0] } 680 func (n extentNode) End() token.Pos { return n[1] } 681 682 // Syntax returns an ast.Node whose Pos/End methods provide the 683 // lexical extent of the function if it was defined by Go source code 684 // (f.Synthetic==""), or nil otherwise. 685 // 686 // If f was built with debug information (see Package.SetDebugRef), 687 // the result is the *ast.FuncDecl or *ast.FuncLit that declared the 688 // function. Otherwise, it is an opaque Node providing only position 689 // information; this avoids pinning the AST in memory. 690 // 691 func (f *Function) Syntax() ast.Node { return f.syntax }