github.com/jd-ly/tools@v0.5.7/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 package ssa 6 7 // This file implements the Function and BasicBlock types. 8 9 import ( 10 "bytes" 11 "fmt" 12 "go/ast" 13 "go/token" 14 "go/types" 15 "io" 16 "os" 17 "strings" 18 ) 19 20 // addEdge adds a control-flow graph edge from from to to. 21 func addEdge(from, to *BasicBlock) { 22 from.Succs = append(from.Succs, to) 23 to.Preds = append(to.Preds, from) 24 } 25 26 // Parent returns the function that contains block b. 27 func (b *BasicBlock) Parent() *Function { return b.parent } 28 29 // String returns a human-readable label of this block. 30 // It is not guaranteed unique within the function. 31 // 32 func (b *BasicBlock) String() string { 33 return fmt.Sprintf("%d", b.Index) 34 } 35 36 // emit appends an instruction to the current basic block. 37 // If the instruction defines a Value, it is returned. 38 // 39 func (b *BasicBlock) emit(i Instruction) Value { 40 i.setBlock(b) 41 b.Instrs = append(b.Instrs, i) 42 v, _ := i.(Value) 43 return v 44 } 45 46 // predIndex returns the i such that b.Preds[i] == c or panics if 47 // there is none. 48 func (b *BasicBlock) predIndex(c *BasicBlock) int { 49 for i, pred := range b.Preds { 50 if pred == c { 51 return i 52 } 53 } 54 panic(fmt.Sprintf("no edge %s -> %s", c, b)) 55 } 56 57 // hasPhi returns true if b.Instrs contains φ-nodes. 58 func (b *BasicBlock) hasPhi() bool { 59 _, ok := b.Instrs[0].(*Phi) 60 return ok 61 } 62 63 // phis returns the prefix of b.Instrs containing all the block's φ-nodes. 64 func (b *BasicBlock) phis() []Instruction { 65 for i, instr := range b.Instrs { 66 if _, ok := instr.(*Phi); !ok { 67 return b.Instrs[:i] 68 } 69 } 70 return nil // unreachable in well-formed blocks 71 } 72 73 // replacePred replaces all occurrences of p in b's predecessor list with q. 74 // Ordinarily there should be at most one. 75 // 76 func (b *BasicBlock) replacePred(p, q *BasicBlock) { 77 for i, pred := range b.Preds { 78 if pred == p { 79 b.Preds[i] = q 80 } 81 } 82 } 83 84 // replaceSucc replaces all occurrences of p in b's successor list with q. 85 // Ordinarily there should be at most one. 86 // 87 func (b *BasicBlock) replaceSucc(p, q *BasicBlock) { 88 for i, succ := range b.Succs { 89 if succ == p { 90 b.Succs[i] = q 91 } 92 } 93 } 94 95 // removePred removes all occurrences of p in b's 96 // predecessor list and φ-nodes. 97 // Ordinarily there should be at most one. 98 // 99 func (b *BasicBlock) removePred(p *BasicBlock) { 100 phis := b.phis() 101 102 // We must preserve edge order for φ-nodes. 103 j := 0 104 for i, pred := range b.Preds { 105 if pred != p { 106 b.Preds[j] = b.Preds[i] 107 // Strike out φ-edge too. 108 for _, instr := range phis { 109 phi := instr.(*Phi) 110 phi.Edges[j] = phi.Edges[i] 111 } 112 j++ 113 } 114 } 115 // Nil out b.Preds[j:] and φ-edges[j:] to aid GC. 116 for i := j; i < len(b.Preds); i++ { 117 b.Preds[i] = nil 118 for _, instr := range phis { 119 instr.(*Phi).Edges[i] = nil 120 } 121 } 122 b.Preds = b.Preds[:j] 123 for _, instr := range phis { 124 phi := instr.(*Phi) 125 phi.Edges = phi.Edges[:j] 126 } 127 } 128 129 // Destinations associated with unlabelled for/switch/select stmts. 130 // We push/pop one of these as we enter/leave each construct and for 131 // each BranchStmt we scan for the innermost target of the right type. 132 // 133 type targets struct { 134 tail *targets // rest of stack 135 _break *BasicBlock 136 _continue *BasicBlock 137 _fallthrough *BasicBlock 138 } 139 140 // Destinations associated with a labelled block. 141 // We populate these as labels are encountered in forward gotos or 142 // labelled statements. 143 // 144 type lblock struct { 145 _goto *BasicBlock 146 _break *BasicBlock 147 _continue *BasicBlock 148 } 149 150 // labelledBlock returns the branch target associated with the 151 // specified label, creating it if needed. 152 // 153 func (f *Function) labelledBlock(label *ast.Ident) *lblock { 154 lb := f.lblocks[label.Obj] 155 if lb == nil { 156 lb = &lblock{_goto: f.newBasicBlock(label.Name)} 157 if f.lblocks == nil { 158 f.lblocks = make(map[*ast.Object]*lblock) 159 } 160 f.lblocks[label.Obj] = lb 161 } 162 return lb 163 } 164 165 // addParam adds a (non-escaping) parameter to f.Params of the 166 // specified name, type and source position. 167 // 168 func (f *Function) addParam(name string, typ types.Type, pos token.Pos) *Parameter { 169 v := &Parameter{ 170 name: name, 171 typ: typ, 172 pos: pos, 173 parent: f, 174 } 175 f.Params = append(f.Params, v) 176 return v 177 } 178 179 func (f *Function) addParamObj(obj types.Object) *Parameter { 180 name := obj.Name() 181 if name == "" { 182 name = fmt.Sprintf("arg%d", len(f.Params)) 183 } 184 param := f.addParam(name, obj.Type(), obj.Pos()) 185 param.object = obj 186 return param 187 } 188 189 // addSpilledParam declares a parameter that is pre-spilled to the 190 // stack; the function body will load/store the spilled location. 191 // Subsequent lifting will eliminate spills where possible. 192 // 193 func (f *Function) addSpilledParam(obj types.Object) { 194 param := f.addParamObj(obj) 195 spill := &Alloc{Comment: obj.Name()} 196 spill.setType(types.NewPointer(obj.Type())) 197 spill.setPos(obj.Pos()) 198 f.objects[obj] = spill 199 f.Locals = append(f.Locals, spill) 200 f.emit(spill) 201 f.emit(&Store{Addr: spill, Val: param}) 202 } 203 204 // startBody initializes the function prior to generating SSA code for its body. 205 // Precondition: f.Type() already set. 206 // 207 func (f *Function) startBody() { 208 f.currentBlock = f.newBasicBlock("entry") 209 f.objects = make(map[types.Object]Value) // needed for some synthetics, e.g. init 210 } 211 212 // createSyntacticParams populates f.Params and generates code (spills 213 // and named result locals) for all the parameters declared in the 214 // syntax. In addition it populates the f.objects mapping. 215 // 216 // Preconditions: 217 // f.startBody() was called. 218 // Postcondition: 219 // len(f.Params) == len(f.Signature.Params) + (f.Signature.Recv() ? 1 : 0) 220 // 221 func (f *Function) createSyntacticParams(recv *ast.FieldList, functype *ast.FuncType) { 222 // Receiver (at most one inner iteration). 223 if recv != nil { 224 for _, field := range recv.List { 225 for _, n := range field.Names { 226 f.addSpilledParam(f.Pkg.info.Defs[n]) 227 } 228 // Anonymous receiver? No need to spill. 229 if field.Names == nil { 230 f.addParamObj(f.Signature.Recv()) 231 } 232 } 233 } 234 235 // Parameters. 236 if functype.Params != nil { 237 n := len(f.Params) // 1 if has recv, 0 otherwise 238 for _, field := range functype.Params.List { 239 for _, n := range field.Names { 240 f.addSpilledParam(f.Pkg.info.Defs[n]) 241 } 242 // Anonymous parameter? No need to spill. 243 if field.Names == nil { 244 f.addParamObj(f.Signature.Params().At(len(f.Params) - n)) 245 } 246 } 247 } 248 249 // Named results. 250 if functype.Results != nil { 251 for _, field := range functype.Results.List { 252 // Implicit "var" decl of locals for named results. 253 for _, n := range field.Names { 254 f.namedResults = append(f.namedResults, f.addLocalForIdent(n)) 255 } 256 } 257 } 258 } 259 260 type setNumable interface { 261 setNum(int) 262 } 263 264 // numberRegisters assigns numbers to all SSA registers 265 // (value-defining Instructions) in f, to aid debugging. 266 // (Non-Instruction Values are named at construction.) 267 // 268 func numberRegisters(f *Function) { 269 v := 0 270 for _, b := range f.Blocks { 271 for _, instr := range b.Instrs { 272 switch instr.(type) { 273 case Value: 274 instr.(setNumable).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 }