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