github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/cmd/compile/internal/gc/syntax.go (about) 1 // Copyright 2009 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 // “Abstract” syntax representation. 6 7 package gc 8 9 import ( 10 "cmd/compile/internal/ssa" 11 "cmd/compile/internal/syntax" 12 "cmd/compile/internal/types" 13 "cmd/internal/obj" 14 "cmd/internal/src" 15 ) 16 17 // A Node is a single node in the syntax tree. 18 // Actually the syntax tree is a syntax DAG, because there is only one 19 // node with Op=ONAME for a given instance of a variable x. 20 // The same is true for Op=OTYPE and Op=OLITERAL. See Node.mayBeShared. 21 type Node struct { 22 // Tree structure. 23 // Generic recursive walks should follow these fields. 24 Left *Node 25 Right *Node 26 Ninit Nodes 27 Nbody Nodes 28 List Nodes 29 Rlist Nodes 30 31 // most nodes 32 Type *types.Type 33 Orig *Node // original form, for printing, and tracking copies of ONAMEs 34 35 // func 36 Func *Func 37 38 // ONAME, OTYPE, OPACK, OLABEL, some OLITERAL 39 Name *Name 40 41 Sym *types.Sym // various 42 E interface{} // Opt or Val, see methods below 43 44 // Various. Usually an offset into a struct. For example: 45 // - ONAME nodes that refer to local variables use it to identify their stack frame position. 46 // - ODOT, ODOTPTR, and OINDREGSP use it to indicate offset relative to their base address. 47 // - OSTRUCTKEY uses it to store the named field's offset. 48 // - Named OLITERALs use it to store their ambient iota value. 49 // - OINLMARK stores an index into the inlTree data structure. 50 // Possibly still more uses. If you find any, document them. 51 Xoffset int64 52 53 Pos src.XPos 54 55 flags bitset32 56 57 Esc uint16 // EscXXX 58 59 Op Op 60 aux uint8 61 } 62 63 func (n *Node) ResetAux() { 64 n.aux = 0 65 } 66 67 func (n *Node) SubOp() Op { 68 switch n.Op { 69 case OASOP, ONAME: 70 default: 71 Fatalf("unexpected op: %v", n.Op) 72 } 73 return Op(n.aux) 74 } 75 76 func (n *Node) SetSubOp(op Op) { 77 switch n.Op { 78 case OASOP, ONAME: 79 default: 80 Fatalf("unexpected op: %v", n.Op) 81 } 82 n.aux = uint8(op) 83 } 84 85 func (n *Node) IndexMapLValue() bool { 86 if n.Op != OINDEXMAP { 87 Fatalf("unexpected op: %v", n.Op) 88 } 89 return n.aux != 0 90 } 91 92 func (n *Node) SetIndexMapLValue(b bool) { 93 if n.Op != OINDEXMAP { 94 Fatalf("unexpected op: %v", n.Op) 95 } 96 if b { 97 n.aux = 1 98 } else { 99 n.aux = 0 100 } 101 } 102 103 func (n *Node) TChanDir() types.ChanDir { 104 if n.Op != OTCHAN { 105 Fatalf("unexpected op: %v", n.Op) 106 } 107 return types.ChanDir(n.aux) 108 } 109 110 func (n *Node) SetTChanDir(dir types.ChanDir) { 111 if n.Op != OTCHAN { 112 Fatalf("unexpected op: %v", n.Op) 113 } 114 n.aux = uint8(dir) 115 } 116 117 func (n *Node) IsSynthetic() bool { 118 name := n.Sym.Name 119 return name[0] == '.' || name[0] == '~' 120 } 121 122 // IsAutoTmp indicates if n was created by the compiler as a temporary, 123 // based on the setting of the .AutoTemp flag in n's Name. 124 func (n *Node) IsAutoTmp() bool { 125 if n == nil || n.Op != ONAME { 126 return false 127 } 128 return n.Name.AutoTemp() 129 } 130 131 const ( 132 nodeClass, _ = iota, 1 << iota // PPARAM, PAUTO, PEXTERN, etc; three bits; first in the list because frequently accessed 133 _, _ // second nodeClass bit 134 _, _ // third nodeClass bit 135 nodeWalkdef, _ // tracks state during typecheckdef; 2 == loop detected; two bits 136 _, _ // second nodeWalkdef bit 137 nodeTypecheck, _ // tracks state during typechecking; 2 == loop detected; two bits 138 _, _ // second nodeTypecheck bit 139 nodeInitorder, _ // tracks state during init1; two bits 140 _, _ // second nodeInitorder bit 141 _, nodeHasBreak 142 _, nodeIsClosureVar 143 _, nodeIsOutputParamHeapAddr 144 _, nodeNoInline // used internally by inliner to indicate that a function call should not be inlined; set for OCALLFUNC and OCALLMETH only 145 _, nodeAssigned // is the variable ever assigned to 146 _, nodeAddrtaken // address taken, even if not moved to heap 147 _, nodeImplicit 148 _, nodeIsDDD // is the argument variadic 149 _, nodeDiag // already printed error about this 150 _, nodeColas // OAS resulting from := 151 _, nodeNonNil // guaranteed to be non-nil 152 _, nodeNoescape // func arguments do not escape; TODO(rsc): move Noescape to Func struct (see CL 7360) 153 _, nodeBounded // bounds check unnecessary 154 _, nodeAddable // addressable 155 _, nodeHasCall // expression contains a function call 156 _, nodeLikely // if statement condition likely 157 _, nodeHasVal // node.E contains a Val 158 _, nodeHasOpt // node.E contains an Opt 159 _, nodeEmbedded // ODCLFIELD embedded type 160 _, nodeInlFormal // OPAUTO created by inliner, derived from callee formal 161 _, nodeInlLocal // OPAUTO created by inliner, derived from callee local 162 ) 163 164 func (n *Node) Class() Class { return Class(n.flags.get3(nodeClass)) } 165 func (n *Node) Walkdef() uint8 { return n.flags.get2(nodeWalkdef) } 166 func (n *Node) Typecheck() uint8 { return n.flags.get2(nodeTypecheck) } 167 func (n *Node) Initorder() uint8 { return n.flags.get2(nodeInitorder) } 168 169 func (n *Node) HasBreak() bool { return n.flags&nodeHasBreak != 0 } 170 func (n *Node) IsClosureVar() bool { return n.flags&nodeIsClosureVar != 0 } 171 func (n *Node) NoInline() bool { return n.flags&nodeNoInline != 0 } 172 func (n *Node) IsOutputParamHeapAddr() bool { return n.flags&nodeIsOutputParamHeapAddr != 0 } 173 func (n *Node) Assigned() bool { return n.flags&nodeAssigned != 0 } 174 func (n *Node) Addrtaken() bool { return n.flags&nodeAddrtaken != 0 } 175 func (n *Node) Implicit() bool { return n.flags&nodeImplicit != 0 } 176 func (n *Node) IsDDD() bool { return n.flags&nodeIsDDD != 0 } 177 func (n *Node) Diag() bool { return n.flags&nodeDiag != 0 } 178 func (n *Node) Colas() bool { return n.flags&nodeColas != 0 } 179 func (n *Node) NonNil() bool { return n.flags&nodeNonNil != 0 } 180 func (n *Node) Noescape() bool { return n.flags&nodeNoescape != 0 } 181 func (n *Node) Bounded() bool { return n.flags&nodeBounded != 0 } 182 func (n *Node) Addable() bool { return n.flags&nodeAddable != 0 } 183 func (n *Node) HasCall() bool { return n.flags&nodeHasCall != 0 } 184 func (n *Node) Likely() bool { return n.flags&nodeLikely != 0 } 185 func (n *Node) HasVal() bool { return n.flags&nodeHasVal != 0 } 186 func (n *Node) HasOpt() bool { return n.flags&nodeHasOpt != 0 } 187 func (n *Node) Embedded() bool { return n.flags&nodeEmbedded != 0 } 188 func (n *Node) InlFormal() bool { return n.flags&nodeInlFormal != 0 } 189 func (n *Node) InlLocal() bool { return n.flags&nodeInlLocal != 0 } 190 191 func (n *Node) SetClass(b Class) { n.flags.set3(nodeClass, uint8(b)) } 192 func (n *Node) SetWalkdef(b uint8) { n.flags.set2(nodeWalkdef, b) } 193 func (n *Node) SetTypecheck(b uint8) { n.flags.set2(nodeTypecheck, b) } 194 func (n *Node) SetInitorder(b uint8) { n.flags.set2(nodeInitorder, b) } 195 196 func (n *Node) SetHasBreak(b bool) { n.flags.set(nodeHasBreak, b) } 197 func (n *Node) SetIsClosureVar(b bool) { n.flags.set(nodeIsClosureVar, b) } 198 func (n *Node) SetNoInline(b bool) { n.flags.set(nodeNoInline, b) } 199 func (n *Node) SetIsOutputParamHeapAddr(b bool) { n.flags.set(nodeIsOutputParamHeapAddr, b) } 200 func (n *Node) SetAssigned(b bool) { n.flags.set(nodeAssigned, b) } 201 func (n *Node) SetAddrtaken(b bool) { n.flags.set(nodeAddrtaken, b) } 202 func (n *Node) SetImplicit(b bool) { n.flags.set(nodeImplicit, b) } 203 func (n *Node) SetIsDDD(b bool) { n.flags.set(nodeIsDDD, b) } 204 func (n *Node) SetDiag(b bool) { n.flags.set(nodeDiag, b) } 205 func (n *Node) SetColas(b bool) { n.flags.set(nodeColas, b) } 206 func (n *Node) SetNonNil(b bool) { n.flags.set(nodeNonNil, b) } 207 func (n *Node) SetNoescape(b bool) { n.flags.set(nodeNoescape, b) } 208 func (n *Node) SetBounded(b bool) { n.flags.set(nodeBounded, b) } 209 func (n *Node) SetAddable(b bool) { n.flags.set(nodeAddable, b) } 210 func (n *Node) SetHasCall(b bool) { n.flags.set(nodeHasCall, b) } 211 func (n *Node) SetLikely(b bool) { n.flags.set(nodeLikely, b) } 212 func (n *Node) SetHasVal(b bool) { n.flags.set(nodeHasVal, b) } 213 func (n *Node) SetHasOpt(b bool) { n.flags.set(nodeHasOpt, b) } 214 func (n *Node) SetEmbedded(b bool) { n.flags.set(nodeEmbedded, b) } 215 func (n *Node) SetInlFormal(b bool) { n.flags.set(nodeInlFormal, b) } 216 func (n *Node) SetInlLocal(b bool) { n.flags.set(nodeInlLocal, b) } 217 218 // Val returns the Val for the node. 219 func (n *Node) Val() Val { 220 if !n.HasVal() { 221 return Val{} 222 } 223 return Val{n.E} 224 } 225 226 // SetVal sets the Val for the node, which must not have been used with SetOpt. 227 func (n *Node) SetVal(v Val) { 228 if n.HasOpt() { 229 Debug['h'] = 1 230 Dump("have Opt", n) 231 Fatalf("have Opt") 232 } 233 n.SetHasVal(true) 234 n.E = v.U 235 } 236 237 // Opt returns the optimizer data for the node. 238 func (n *Node) Opt() interface{} { 239 if !n.HasOpt() { 240 return nil 241 } 242 return n.E 243 } 244 245 // SetOpt sets the optimizer data for the node, which must not have been used with SetVal. 246 // SetOpt(nil) is ignored for Vals to simplify call sites that are clearing Opts. 247 func (n *Node) SetOpt(x interface{}) { 248 if x == nil && n.HasVal() { 249 return 250 } 251 if n.HasVal() { 252 Debug['h'] = 1 253 Dump("have Val", n) 254 Fatalf("have Val") 255 } 256 n.SetHasOpt(true) 257 n.E = x 258 } 259 260 func (n *Node) Iota() int64 { 261 return n.Xoffset 262 } 263 264 func (n *Node) SetIota(x int64) { 265 n.Xoffset = x 266 } 267 268 // mayBeShared reports whether n may occur in multiple places in the AST. 269 // Extra care must be taken when mutating such a node. 270 func (n *Node) mayBeShared() bool { 271 switch n.Op { 272 case ONAME, OLITERAL, OTYPE: 273 return true 274 } 275 return false 276 } 277 278 // isMethodExpression reports whether n represents a method expression T.M. 279 func (n *Node) isMethodExpression() bool { 280 return n.Op == ONAME && n.Left != nil && n.Left.Op == OTYPE && n.Right != nil && n.Right.Op == ONAME 281 } 282 283 // funcname returns the name of the function n. 284 func (n *Node) funcname() string { 285 if n == nil || n.Func == nil || n.Func.Nname == nil { 286 return "<nil>" 287 } 288 return n.Func.Nname.Sym.Name 289 } 290 291 // Name holds Node fields used only by named nodes (ONAME, OTYPE, OPACK, OLABEL, some OLITERAL). 292 type Name struct { 293 Pack *Node // real package for import . names 294 Pkg *types.Pkg // pkg for OPACK nodes 295 Defn *Node // initializing assignment 296 Curfn *Node // function for local variables 297 Param *Param // additional fields for ONAME, OTYPE 298 Decldepth int32 // declaration loop depth, increased for every loop or label 299 Vargen int32 // unique name for ONAME within a function. Function outputs are numbered starting at one. 300 flags bitset8 301 } 302 303 const ( 304 nameCaptured = 1 << iota // is the variable captured by a closure 305 nameReadonly 306 nameByval // is the variable captured by value or by reference 307 nameNeedzero // if it contains pointers, needs to be zeroed on function entry 308 nameKeepalive // mark value live across unknown assembly call 309 nameAutoTemp // is the variable a temporary (implies no dwarf info. reset if escapes to heap) 310 nameUsed // for variable declared and not used error 311 ) 312 313 func (n *Name) Captured() bool { return n.flags&nameCaptured != 0 } 314 func (n *Name) Readonly() bool { return n.flags&nameReadonly != 0 } 315 func (n *Name) Byval() bool { return n.flags&nameByval != 0 } 316 func (n *Name) Needzero() bool { return n.flags&nameNeedzero != 0 } 317 func (n *Name) Keepalive() bool { return n.flags&nameKeepalive != 0 } 318 func (n *Name) AutoTemp() bool { return n.flags&nameAutoTemp != 0 } 319 func (n *Name) Used() bool { return n.flags&nameUsed != 0 } 320 321 func (n *Name) SetCaptured(b bool) { n.flags.set(nameCaptured, b) } 322 func (n *Name) SetReadonly(b bool) { n.flags.set(nameReadonly, b) } 323 func (n *Name) SetByval(b bool) { n.flags.set(nameByval, b) } 324 func (n *Name) SetNeedzero(b bool) { n.flags.set(nameNeedzero, b) } 325 func (n *Name) SetKeepalive(b bool) { n.flags.set(nameKeepalive, b) } 326 func (n *Name) SetAutoTemp(b bool) { n.flags.set(nameAutoTemp, b) } 327 func (n *Name) SetUsed(b bool) { n.flags.set(nameUsed, b) } 328 329 type Param struct { 330 Ntype *Node 331 Heapaddr *Node // temp holding heap address of param 332 333 // ONAME PAUTOHEAP 334 Stackcopy *Node // the PPARAM/PPARAMOUT on-stack slot (moved func params only) 335 336 // ONAME closure linkage 337 // Consider: 338 // 339 // func f() { 340 // x := 1 // x1 341 // func() { 342 // use(x) // x2 343 // func() { 344 // use(x) // x3 345 // --- parser is here --- 346 // }() 347 // }() 348 // } 349 // 350 // There is an original declaration of x and then a chain of mentions of x 351 // leading into the current function. Each time x is mentioned in a new closure, 352 // we create a variable representing x for use in that specific closure, 353 // since the way you get to x is different in each closure. 354 // 355 // Let's number the specific variables as shown in the code: 356 // x1 is the original x, x2 is when mentioned in the closure, 357 // and x3 is when mentioned in the closure in the closure. 358 // 359 // We keep these linked (assume N > 1): 360 // 361 // - x1.Defn = original declaration statement for x (like most variables) 362 // - x1.Innermost = current innermost closure x (in this case x3), or nil for none 363 // - x1.IsClosureVar() = false 364 // 365 // - xN.Defn = x1, N > 1 366 // - xN.IsClosureVar() = true, N > 1 367 // - x2.Outer = nil 368 // - xN.Outer = x(N-1), N > 2 369 // 370 // 371 // When we look up x in the symbol table, we always get x1. 372 // Then we can use x1.Innermost (if not nil) to get the x 373 // for the innermost known closure function, 374 // but the first reference in a closure will find either no x1.Innermost 375 // or an x1.Innermost with .Funcdepth < Funcdepth. 376 // In that case, a new xN must be created, linked in with: 377 // 378 // xN.Defn = x1 379 // xN.Outer = x1.Innermost 380 // x1.Innermost = xN 381 // 382 // When we finish the function, we'll process its closure variables 383 // and find xN and pop it off the list using: 384 // 385 // x1 := xN.Defn 386 // x1.Innermost = xN.Outer 387 // 388 // We leave xN.Innermost set so that we can still get to the original 389 // variable quickly. Not shown here, but once we're 390 // done parsing a function and no longer need xN.Outer for the 391 // lexical x reference links as described above, closurebody 392 // recomputes xN.Outer as the semantic x reference link tree, 393 // even filling in x in intermediate closures that might not 394 // have mentioned it along the way to inner closures that did. 395 // See closurebody for details. 396 // 397 // During the eventual compilation, then, for closure variables we have: 398 // 399 // xN.Defn = original variable 400 // xN.Outer = variable captured in next outward scope 401 // to make closure where xN appears 402 // 403 // Because of the sharding of pieces of the node, x.Defn means x.Name.Defn 404 // and x.Innermost/Outer means x.Name.Param.Innermost/Outer. 405 Innermost *Node 406 Outer *Node 407 408 // OTYPE 409 // 410 // TODO: Should Func pragmas also be stored on the Name? 411 Pragma syntax.Pragma 412 Alias bool // node is alias for Ntype (only used when type-checking ODCLTYPE) 413 } 414 415 // Functions 416 // 417 // A simple function declaration is represented as an ODCLFUNC node f 418 // and an ONAME node n. They're linked to one another through 419 // f.Func.Nname == n and n.Name.Defn == f. When functions are 420 // referenced by name in an expression, the function's ONAME node is 421 // used directly. 422 // 423 // Function names have n.Class() == PFUNC. This distinguishes them 424 // from variables of function type. 425 // 426 // Confusingly, n.Func and f.Func both exist, but commonly point to 427 // different Funcs. (Exception: an OCALLPART's Func does point to its 428 // ODCLFUNC's Func.) 429 // 430 // A method declaration is represented like functions, except n.Sym 431 // will be the qualified method name (e.g., "T.m") and 432 // f.Func.Shortname is the bare method name (e.g., "m"). 433 // 434 // Method expressions are represented as ONAME/PFUNC nodes like 435 // function names, but their Left and Right fields still point to the 436 // type and method, respectively. They can be distinguished from 437 // normal functions with isMethodExpression. Also, unlike function 438 // name nodes, method expression nodes exist for each method 439 // expression. The declaration ONAME can be accessed with 440 // x.Type.Nname(), where x is the method expression ONAME node. 441 // 442 // Method values are represented by ODOTMETH/ODOTINTER when called 443 // immediately, and OCALLPART otherwise. They are like method 444 // expressions, except that for ODOTMETH/ODOTINTER the method name is 445 // stored in Sym instead of Right. 446 // 447 // Closures are represented by OCLOSURE node c. They link back and 448 // forth with the ODCLFUNC via Func.Closure; that is, c.Func.Closure 449 // == f and f.Func.Closure == c. 450 // 451 // Function bodies are stored in f.Nbody, and inline function bodies 452 // are stored in n.Func.Inl. Pragmas are stored in f.Func.Pragma. 453 // 454 // Imported functions skip the ODCLFUNC, so n.Name.Defn is nil. They 455 // also use Dcl instead of Inldcl. 456 457 // Func holds Node fields used only with function-like nodes. 458 type Func struct { 459 Shortname *types.Sym 460 Enter Nodes // for example, allocate and initialize memory for escaping parameters 461 Exit Nodes 462 Cvars Nodes // closure params 463 Dcl []*Node // autodcl for this func/closure 464 465 // Parents records the parent scope of each scope within a 466 // function. The root scope (0) has no parent, so the i'th 467 // scope's parent is stored at Parents[i-1]. 468 Parents []ScopeID 469 470 // Marks records scope boundary changes. 471 Marks []Mark 472 473 // Closgen tracks how many closures have been generated within 474 // this function. Used by closurename for creating unique 475 // function names. 476 Closgen int 477 478 FieldTrack map[*types.Sym]struct{} 479 DebugInfo *ssa.FuncDebug 480 Ntype *Node // signature 481 Top int // top context (ctxCallee, etc) 482 Closure *Node // OCLOSURE <-> ODCLFUNC 483 Nname *Node 484 lsym *obj.LSym 485 486 Inl *Inline 487 488 Label int32 // largest auto-generated label in this function 489 490 Endlineno src.XPos 491 WBPos src.XPos // position of first write barrier; see SetWBPos 492 493 Pragma syntax.Pragma // go:xxx function annotations 494 495 flags bitset16 496 497 // nwbrCalls records the LSyms of functions called by this 498 // function for go:nowritebarrierrec analysis. Only filled in 499 // if nowritebarrierrecCheck != nil. 500 nwbrCalls *[]nowritebarrierrecCallSym 501 } 502 503 // An Inline holds fields used for function bodies that can be inlined. 504 type Inline struct { 505 Cost int32 // heuristic cost of inlining this function 506 507 // Copies of Func.Dcl and Nbody for use during inlining. 508 Dcl []*Node 509 Body []*Node 510 } 511 512 // A Mark represents a scope boundary. 513 type Mark struct { 514 // Pos is the position of the token that marks the scope 515 // change. 516 Pos src.XPos 517 518 // Scope identifies the innermost scope to the right of Pos. 519 Scope ScopeID 520 } 521 522 // A ScopeID represents a lexical scope within a function. 523 type ScopeID int32 524 525 const ( 526 funcDupok = 1 << iota // duplicate definitions ok 527 funcWrapper // is method wrapper 528 funcNeedctxt // function uses context register (has closure variables) 529 funcReflectMethod // function calls reflect.Type.Method or MethodByName 530 funcIsHiddenClosure 531 funcHasDefer // contains a defer statement 532 funcNilCheckDisabled // disable nil checks when compiling this function 533 funcInlinabilityChecked // inliner has already determined whether the function is inlinable 534 funcExportInline // include inline body in export data 535 funcInstrumentBody // add race/msan instrumentation during SSA construction 536 ) 537 538 func (f *Func) Dupok() bool { return f.flags&funcDupok != 0 } 539 func (f *Func) Wrapper() bool { return f.flags&funcWrapper != 0 } 540 func (f *Func) Needctxt() bool { return f.flags&funcNeedctxt != 0 } 541 func (f *Func) ReflectMethod() bool { return f.flags&funcReflectMethod != 0 } 542 func (f *Func) IsHiddenClosure() bool { return f.flags&funcIsHiddenClosure != 0 } 543 func (f *Func) HasDefer() bool { return f.flags&funcHasDefer != 0 } 544 func (f *Func) NilCheckDisabled() bool { return f.flags&funcNilCheckDisabled != 0 } 545 func (f *Func) InlinabilityChecked() bool { return f.flags&funcInlinabilityChecked != 0 } 546 func (f *Func) ExportInline() bool { return f.flags&funcExportInline != 0 } 547 func (f *Func) InstrumentBody() bool { return f.flags&funcInstrumentBody != 0 } 548 549 func (f *Func) SetDupok(b bool) { f.flags.set(funcDupok, b) } 550 func (f *Func) SetWrapper(b bool) { f.flags.set(funcWrapper, b) } 551 func (f *Func) SetNeedctxt(b bool) { f.flags.set(funcNeedctxt, b) } 552 func (f *Func) SetReflectMethod(b bool) { f.flags.set(funcReflectMethod, b) } 553 func (f *Func) SetIsHiddenClosure(b bool) { f.flags.set(funcIsHiddenClosure, b) } 554 func (f *Func) SetHasDefer(b bool) { f.flags.set(funcHasDefer, b) } 555 func (f *Func) SetNilCheckDisabled(b bool) { f.flags.set(funcNilCheckDisabled, b) } 556 func (f *Func) SetInlinabilityChecked(b bool) { f.flags.set(funcInlinabilityChecked, b) } 557 func (f *Func) SetExportInline(b bool) { f.flags.set(funcExportInline, b) } 558 func (f *Func) SetInstrumentBody(b bool) { f.flags.set(funcInstrumentBody, b) } 559 560 func (f *Func) setWBPos(pos src.XPos) { 561 if Debug_wb != 0 { 562 Warnl(pos, "write barrier") 563 } 564 if !f.WBPos.IsKnown() { 565 f.WBPos = pos 566 } 567 } 568 569 //go:generate stringer -type=Op -trimprefix=O 570 571 type Op uint8 572 573 // Node ops. 574 const ( 575 OXXX Op = iota 576 577 // names 578 ONAME // var or func name 579 ONONAME // unnamed arg or return value: f(int, string) (int, error) { etc } 580 OTYPE // type name 581 OPACK // import 582 OLITERAL // literal 583 584 // expressions 585 OADD // Left + Right 586 OSUB // Left - Right 587 OOR // Left | Right 588 OXOR // Left ^ Right 589 OADDSTR // +{List} (string addition, list elements are strings) 590 OADDR // &Left 591 OANDAND // Left && Right 592 OAPPEND // append(List); after walk, Left may contain elem type descriptor 593 OBYTES2STR // Type(Left) (Type is string, Left is a []byte) 594 OBYTES2STRTMP // Type(Left) (Type is string, Left is a []byte, ephemeral) 595 ORUNES2STR // Type(Left) (Type is string, Left is a []rune) 596 OSTR2BYTES // Type(Left) (Type is []byte, Left is a string) 597 OSTR2BYTESTMP // Type(Left) (Type is []byte, Left is a string, ephemeral) 598 OSTR2RUNES // Type(Left) (Type is []rune, Left is a string) 599 OAS // Left = Right or (if Colas=true) Left := Right 600 OAS2 // List = Rlist (x, y, z = a, b, c) 601 OAS2FUNC // List = Rlist (x, y = f()) 602 OAS2RECV // List = Rlist (x, ok = <-c) 603 OAS2MAPR // List = Rlist (x, ok = m["foo"]) 604 OAS2DOTTYPE // List = Rlist (x, ok = I.(int)) 605 OASOP // Left Etype= Right (x += y) 606 OCALL // Left(List) (function call, method call or type conversion) 607 608 // OCALLFUNC, OCALLMETH, and OCALLINTER have the same structure. 609 // Prior to walk, they are: Left(List), where List is all regular arguments. 610 // If present, Right is an ODDDARG that holds the 611 // generated slice used in a call to a variadic function. 612 // After walk, List is a series of assignments to temporaries, 613 // and Rlist is an updated set of arguments, including any ODDDARG slice. 614 // TODO(josharian/khr): Use Ninit instead of List for the assignments to temporaries. See CL 114797. 615 OCALLFUNC // Left(List/Rlist) (function call f(args)) 616 OCALLMETH // Left(List/Rlist) (direct method call x.Method(args)) 617 OCALLINTER // Left(List/Rlist) (interface method call x.Method(args)) 618 OCALLPART // Left.Right (method expression x.Method, not called) 619 OCAP // cap(Left) 620 OCLOSE // close(Left) 621 OCLOSURE // func Type { Body } (func literal) 622 OCOMPLIT // Right{List} (composite literal, not yet lowered to specific form) 623 OMAPLIT // Type{List} (composite literal, Type is map) 624 OSTRUCTLIT // Type{List} (composite literal, Type is struct) 625 OARRAYLIT // Type{List} (composite literal, Type is array) 626 OSLICELIT // Type{List} (composite literal, Type is slice) Right.Int64() = slice length. 627 OPTRLIT // &Left (left is composite literal) 628 OCONV // Type(Left) (type conversion) 629 OCONVIFACE // Type(Left) (type conversion, to interface) 630 OCONVNOP // Type(Left) (type conversion, no effect) 631 OCOPY // copy(Left, Right) 632 ODCL // var Left (declares Left of type Left.Type) 633 634 // Used during parsing but don't last. 635 ODCLFUNC // func f() or func (r) f() 636 ODCLFIELD // struct field, interface field, or func/method argument/return value. 637 ODCLCONST // const pi = 3.14 638 ODCLTYPE // type Int int or type Int = int 639 640 ODELETE // delete(Left, Right) 641 ODOT // Left.Sym (Left is of struct type) 642 ODOTPTR // Left.Sym (Left is of pointer to struct type) 643 ODOTMETH // Left.Sym (Left is non-interface, Right is method name) 644 ODOTINTER // Left.Sym (Left is interface, Right is method name) 645 OXDOT // Left.Sym (before rewrite to one of the preceding) 646 ODOTTYPE // Left.Right or Left.Type (.Right during parsing, .Type once resolved); after walk, .Right contains address of interface type descriptor and .Right.Right contains address of concrete type descriptor 647 ODOTTYPE2 // Left.Right or Left.Type (.Right during parsing, .Type once resolved; on rhs of OAS2DOTTYPE); after walk, .Right contains address of interface type descriptor 648 OEQ // Left == Right 649 ONE // Left != Right 650 OLT // Left < Right 651 OLE // Left <= Right 652 OGE // Left >= Right 653 OGT // Left > Right 654 ODEREF // *Left 655 OINDEX // Left[Right] (index of array or slice) 656 OINDEXMAP // Left[Right] (index of map) 657 OKEY // Left:Right (key:value in struct/array/map literal) 658 OSTRUCTKEY // Sym:Left (key:value in struct literal, after type checking) 659 OLEN // len(Left) 660 OMAKE // make(List) (before type checking converts to one of the following) 661 OMAKECHAN // make(Type, Left) (type is chan) 662 OMAKEMAP // make(Type, Left) (type is map) 663 OMAKESLICE // make(Type, Left, Right) (type is slice) 664 OMUL // Left * Right 665 ODIV // Left / Right 666 OMOD // Left % Right 667 OLSH // Left << Right 668 ORSH // Left >> Right 669 OAND // Left & Right 670 OANDNOT // Left &^ Right 671 ONEW // new(Left) 672 ONOT // !Left 673 OBITNOT // ^Left 674 OPLUS // +Left 675 ONEG // -Left 676 OOROR // Left || Right 677 OPANIC // panic(Left) 678 OPRINT // print(List) 679 OPRINTN // println(List) 680 OPAREN // (Left) 681 OSEND // Left <- Right 682 OSLICE // Left[List[0] : List[1]] (Left is untypechecked or slice) 683 OSLICEARR // Left[List[0] : List[1]] (Left is array) 684 OSLICESTR // Left[List[0] : List[1]] (Left is string) 685 OSLICE3 // Left[List[0] : List[1] : List[2]] (Left is untypedchecked or slice) 686 OSLICE3ARR // Left[List[0] : List[1] : List[2]] (Left is array) 687 OSLICEHEADER // sliceheader{Left, List[0], List[1]} (Left is unsafe.Pointer, List[0] is length, List[1] is capacity) 688 ORECOVER // recover() 689 ORECV // <-Left 690 ORUNESTR // Type(Left) (Type is string, Left is rune) 691 OSELRECV // Left = <-Right.Left: (appears as .Left of OCASE; Right.Op == ORECV) 692 OSELRECV2 // List = <-Right.Left: (apperas as .Left of OCASE; count(List) == 2, Right.Op == ORECV) 693 OIOTA // iota 694 OREAL // real(Left) 695 OIMAG // imag(Left) 696 OCOMPLEX // complex(Left, Right) or complex(List[0]) where List[0] is a 2-result function call 697 OALIGNOF // unsafe.Alignof(Left) 698 OOFFSETOF // unsafe.Offsetof(Left) 699 OSIZEOF // unsafe.Sizeof(Left) 700 701 // statements 702 OBLOCK // { List } (block of code) 703 OBREAK // break [Sym] 704 OCASE // case Left or List[0]..List[1]: Nbody (select case after processing; Left==nil and List==nil means default) 705 OXCASE // case List: Nbody (select case before processing; List==nil means default) 706 OCONTINUE // continue [Sym] 707 ODEFER // defer Left (Left must be call) 708 OEMPTY // no-op (empty statement) 709 OFALL // fallthrough 710 OFOR // for Ninit; Left; Right { Nbody } 711 // OFORUNTIL is like OFOR, but the test (Left) is applied after the body: 712 // Ninit 713 // top: { Nbody } // Execute the body at least once 714 // cont: Right 715 // if Left { // And then test the loop condition 716 // List // Before looping to top, execute List 717 // goto top 718 // } 719 // OFORUNTIL is created by walk. There's no way to write this in Go code. 720 OFORUNTIL 721 OGOTO // goto Sym 722 OIF // if Ninit; Left { Nbody } else { Rlist } 723 OLABEL // Sym: 724 OGO // go Left (Left must be call) 725 ORANGE // for List = range Right { Nbody } 726 ORETURN // return List 727 OSELECT // select { List } (List is list of OXCASE or OCASE) 728 OSWITCH // switch Ninit; Left { List } (List is a list of OXCASE or OCASE) 729 OTYPESW // Left = Right.(type) (appears as .Left of OSWITCH) 730 731 // types 732 OTCHAN // chan int 733 OTMAP // map[string]int 734 OTSTRUCT // struct{} 735 OTINTER // interface{} 736 OTFUNC // func() 737 OTARRAY // []int, [8]int, [N]int or [...]int 738 739 // misc 740 ODDD // func f(args ...int) or f(l...) or var a = [...]int{0, 1, 2}. 741 ODDDARG // func f(args ...int), introduced by escape analysis. 742 OINLCALL // intermediary representation of an inlined call. 743 OEFACE // itable and data words of an empty-interface value. 744 OITAB // itable word of an interface value. 745 OIDATA // data word of an interface value in Left 746 OSPTR // base pointer of a slice or string. 747 OCLOSUREVAR // variable reference at beginning of closure function 748 OCFUNC // reference to c function pointer (not go func value) 749 OCHECKNIL // emit code to ensure pointer/interface not nil 750 OVARDEF // variable is about to be fully initialized 751 OVARKILL // variable is dead 752 OVARLIVE // variable is alive 753 OINDREGSP // offset plus indirect of REGSP, such as 8(SP). 754 OINLMARK // start of an inlined body, with file/line of caller. Xoffset is an index into the inline tree. 755 756 // arch-specific opcodes 757 ORETJMP // return to other function 758 OGETG // runtime.getg() (read g pointer) 759 760 OEND 761 ) 762 763 // Nodes is a pointer to a slice of *Node. 764 // For fields that are not used in most nodes, this is used instead of 765 // a slice to save space. 766 type Nodes struct{ slice *[]*Node } 767 768 // asNodes returns a slice of *Node as a Nodes value. 769 func asNodes(s []*Node) Nodes { 770 return Nodes{&s} 771 } 772 773 // Slice returns the entries in Nodes as a slice. 774 // Changes to the slice entries (as in s[i] = n) will be reflected in 775 // the Nodes. 776 func (n Nodes) Slice() []*Node { 777 if n.slice == nil { 778 return nil 779 } 780 return *n.slice 781 } 782 783 // Len returns the number of entries in Nodes. 784 func (n Nodes) Len() int { 785 if n.slice == nil { 786 return 0 787 } 788 return len(*n.slice) 789 } 790 791 // Index returns the i'th element of Nodes. 792 // It panics if n does not have at least i+1 elements. 793 func (n Nodes) Index(i int) *Node { 794 return (*n.slice)[i] 795 } 796 797 // First returns the first element of Nodes (same as n.Index(0)). 798 // It panics if n has no elements. 799 func (n Nodes) First() *Node { 800 return (*n.slice)[0] 801 } 802 803 // Second returns the second element of Nodes (same as n.Index(1)). 804 // It panics if n has fewer than two elements. 805 func (n Nodes) Second() *Node { 806 return (*n.slice)[1] 807 } 808 809 // Set sets n to a slice. 810 // This takes ownership of the slice. 811 func (n *Nodes) Set(s []*Node) { 812 if len(s) == 0 { 813 n.slice = nil 814 } else { 815 // Copy s and take address of t rather than s to avoid 816 // allocation in the case where len(s) == 0 (which is 817 // over 3x more common, dynamically, for make.bash). 818 t := s 819 n.slice = &t 820 } 821 } 822 823 // Set1 sets n to a slice containing a single node. 824 func (n *Nodes) Set1(n1 *Node) { 825 n.slice = &[]*Node{n1} 826 } 827 828 // Set2 sets n to a slice containing two nodes. 829 func (n *Nodes) Set2(n1, n2 *Node) { 830 n.slice = &[]*Node{n1, n2} 831 } 832 833 // Set3 sets n to a slice containing three nodes. 834 func (n *Nodes) Set3(n1, n2, n3 *Node) { 835 n.slice = &[]*Node{n1, n2, n3} 836 } 837 838 // MoveNodes sets n to the contents of n2, then clears n2. 839 func (n *Nodes) MoveNodes(n2 *Nodes) { 840 n.slice = n2.slice 841 n2.slice = nil 842 } 843 844 // SetIndex sets the i'th element of Nodes to node. 845 // It panics if n does not have at least i+1 elements. 846 func (n Nodes) SetIndex(i int, node *Node) { 847 (*n.slice)[i] = node 848 } 849 850 // SetFirst sets the first element of Nodes to node. 851 // It panics if n does not have at least one elements. 852 func (n Nodes) SetFirst(node *Node) { 853 (*n.slice)[0] = node 854 } 855 856 // SetSecond sets the second element of Nodes to node. 857 // It panics if n does not have at least two elements. 858 func (n Nodes) SetSecond(node *Node) { 859 (*n.slice)[1] = node 860 } 861 862 // Addr returns the address of the i'th element of Nodes. 863 // It panics if n does not have at least i+1 elements. 864 func (n Nodes) Addr(i int) **Node { 865 return &(*n.slice)[i] 866 } 867 868 // Append appends entries to Nodes. 869 func (n *Nodes) Append(a ...*Node) { 870 if len(a) == 0 { 871 return 872 } 873 if n.slice == nil { 874 s := make([]*Node, len(a)) 875 copy(s, a) 876 n.slice = &s 877 return 878 } 879 *n.slice = append(*n.slice, a...) 880 } 881 882 // Prepend prepends entries to Nodes. 883 // If a slice is passed in, this will take ownership of it. 884 func (n *Nodes) Prepend(a ...*Node) { 885 if len(a) == 0 { 886 return 887 } 888 if n.slice == nil { 889 n.slice = &a 890 } else { 891 *n.slice = append(a, *n.slice...) 892 } 893 } 894 895 // AppendNodes appends the contents of *n2 to n, then clears n2. 896 func (n *Nodes) AppendNodes(n2 *Nodes) { 897 switch { 898 case n2.slice == nil: 899 case n.slice == nil: 900 n.slice = n2.slice 901 default: 902 *n.slice = append(*n.slice, *n2.slice...) 903 } 904 n2.slice = nil 905 } 906 907 // inspect invokes f on each node in an AST in depth-first order. 908 // If f(n) returns false, inspect skips visiting n's children. 909 func inspect(n *Node, f func(*Node) bool) { 910 if n == nil || !f(n) { 911 return 912 } 913 inspectList(n.Ninit, f) 914 inspect(n.Left, f) 915 inspect(n.Right, f) 916 inspectList(n.List, f) 917 inspectList(n.Nbody, f) 918 inspectList(n.Rlist, f) 919 } 920 921 func inspectList(l Nodes, f func(*Node) bool) { 922 for _, n := range l.Slice() { 923 inspect(n, f) 924 } 925 } 926 927 // nodeQueue is a FIFO queue of *Node. The zero value of nodeQueue is 928 // a ready-to-use empty queue. 929 type nodeQueue struct { 930 ring []*Node 931 head, tail int 932 } 933 934 // empty reports whether q contains no Nodes. 935 func (q *nodeQueue) empty() bool { 936 return q.head == q.tail 937 } 938 939 // pushRight appends n to the right of the queue. 940 func (q *nodeQueue) pushRight(n *Node) { 941 if len(q.ring) == 0 { 942 q.ring = make([]*Node, 16) 943 } else if q.head+len(q.ring) == q.tail { 944 // Grow the ring. 945 nring := make([]*Node, len(q.ring)*2) 946 // Copy the old elements. 947 part := q.ring[q.head%len(q.ring):] 948 if q.tail-q.head <= len(part) { 949 part = part[:q.tail-q.head] 950 copy(nring, part) 951 } else { 952 pos := copy(nring, part) 953 copy(nring[pos:], q.ring[:q.tail%len(q.ring)]) 954 } 955 q.ring, q.head, q.tail = nring, 0, q.tail-q.head 956 } 957 958 q.ring[q.tail%len(q.ring)] = n 959 q.tail++ 960 } 961 962 // popLeft pops a node from the left of the queue. It panics if q is 963 // empty. 964 func (q *nodeQueue) popLeft() *Node { 965 if q.empty() { 966 panic("dequeue empty") 967 } 968 n := q.ring[q.head%len(q.ring)] 969 q.head++ 970 return n 971 }