github.com/robhaswell/grandperspective-scan@v0.1.0/test/go-go1.7.1/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 // A Node is a single node in the syntax tree. 10 // Actually the syntax tree is a syntax DAG, because there is only one 11 // node with Op=ONAME for a given instance of a variable x. 12 // The same is true for Op=OTYPE and Op=OLITERAL. 13 type Node struct { 14 // Tree structure. 15 // Generic recursive walks should follow these fields. 16 Left *Node 17 Right *Node 18 Ninit Nodes 19 Nbody Nodes 20 List Nodes 21 Rlist Nodes 22 23 // most nodes 24 Type *Type 25 Orig *Node // original form, for printing, and tracking copies of ONAMEs 26 27 // func 28 Func *Func 29 30 // ONAME 31 Name *Name 32 33 Sym *Sym // various 34 E interface{} // Opt or Val, see methods below 35 36 // Various. Usually an offset into a struct. For example, ONAME nodes 37 // that refer to local variables use it to identify their stack frame 38 // position. ODOT, ODOTPTR, and OINDREG use it to indicate offset 39 // relative to their base address. ONAME nodes on the left side of an 40 // OKEY within an OSTRUCTLIT use it to store the named field's offset. 41 // OXCASE and OXFALL use it to validate the use of fallthrough. 42 // Possibly still more uses. If you find any, document them. 43 Xoffset int64 44 45 Lineno int32 46 47 // OREGISTER, OINDREG 48 Reg int16 49 50 Esc uint16 // EscXXX 51 52 Op Op 53 Ullman uint8 // sethi/ullman number 54 Addable bool // addressable 55 Etype EType // op for OASOP, etype for OTYPE, exclam for export, 6g saved reg, ChanDir for OTCHAN 56 Bounded bool // bounds check unnecessary 57 NonNil bool // guaranteed to be non-nil 58 Class Class // PPARAM, PAUTO, PEXTERN, etc 59 Embedded uint8 // ODCLFIELD embedded type 60 Colas bool // OAS resulting from := 61 Diag uint8 // already printed error about this 62 Noescape bool // func arguments do not escape; TODO(rsc): move Noescape to Func struct (see CL 7360) 63 Walkdef uint8 64 Typecheck uint8 65 Local bool 66 Dodata uint8 67 Initorder uint8 68 Used bool 69 Isddd bool // is the argument variadic 70 Implicit bool 71 Addrtaken bool // address taken, even if not moved to heap 72 Assigned bool // is the variable ever assigned to 73 Likely int8 // likeliness of if statement 74 hasVal int8 // +1 for Val, -1 for Opt, 0 for not yet set 75 flags uint8 // TODO: store more bool fields in this flag field 76 } 77 78 const ( 79 hasBreak = 1 << iota 80 notLiveAtEnd 81 isClosureVar 82 isOutputParamHeapAddr 83 ) 84 85 func (n *Node) HasBreak() bool { 86 return n.flags&hasBreak != 0 87 } 88 func (n *Node) SetHasBreak(b bool) { 89 if b { 90 n.flags |= hasBreak 91 } else { 92 n.flags &^= hasBreak 93 } 94 } 95 func (n *Node) NotLiveAtEnd() bool { 96 return n.flags¬LiveAtEnd != 0 97 } 98 func (n *Node) SetNotLiveAtEnd(b bool) { 99 if b { 100 n.flags |= notLiveAtEnd 101 } else { 102 n.flags &^= notLiveAtEnd 103 } 104 } 105 func (n *Node) isClosureVar() bool { 106 return n.flags&isClosureVar != 0 107 } 108 func (n *Node) setIsClosureVar(b bool) { 109 if b { 110 n.flags |= isClosureVar 111 } else { 112 n.flags &^= isClosureVar 113 } 114 } 115 116 func (n *Node) IsOutputParamHeapAddr() bool { 117 return n.flags&isOutputParamHeapAddr != 0 118 } 119 func (n *Node) setIsOutputParamHeapAddr(b bool) { 120 if b { 121 n.flags |= isOutputParamHeapAddr 122 } else { 123 n.flags &^= isOutputParamHeapAddr 124 } 125 } 126 127 // Val returns the Val for the node. 128 func (n *Node) Val() Val { 129 if n.hasVal != +1 { 130 return Val{} 131 } 132 return Val{n.E} 133 } 134 135 // SetVal sets the Val for the node, which must not have been used with SetOpt. 136 func (n *Node) SetVal(v Val) { 137 if n.hasVal == -1 { 138 Debug['h'] = 1 139 Dump("have Opt", n) 140 Fatalf("have Opt") 141 } 142 n.hasVal = +1 143 n.E = v.U 144 } 145 146 // Opt returns the optimizer data for the node. 147 func (n *Node) Opt() interface{} { 148 if n.hasVal != -1 { 149 return nil 150 } 151 return n.E 152 } 153 154 // SetOpt sets the optimizer data for the node, which must not have been used with SetVal. 155 // SetOpt(nil) is ignored for Vals to simplify call sites that are clearing Opts. 156 func (n *Node) SetOpt(x interface{}) { 157 if x == nil && n.hasVal >= 0 { 158 return 159 } 160 if n.hasVal == +1 { 161 Debug['h'] = 1 162 Dump("have Val", n) 163 Fatalf("have Val") 164 } 165 n.hasVal = -1 166 n.E = x 167 } 168 169 // Name holds Node fields used only by named nodes (ONAME, OPACK, OLABEL, ODCLFIELD, some OLITERAL). 170 type Name struct { 171 Pack *Node // real package for import . names 172 Pkg *Pkg // pkg for OPACK nodes 173 Heapaddr *Node // temp holding heap address of param (could move to Param?) 174 Inlvar *Node // ONAME substitute while inlining (could move to Param?) 175 Defn *Node // initializing assignment 176 Curfn *Node // function for local variables 177 Param *Param // additional fields for ONAME, ODCLFIELD 178 Decldepth int32 // declaration loop depth, increased for every loop or label 179 Vargen int32 // unique name for ONAME within a function. Function outputs are numbered starting at one. 180 Iota int32 // value if this name is iota 181 Funcdepth int32 182 Method bool // OCALLMETH name 183 Readonly bool 184 Captured bool // is the variable captured by a closure 185 Byval bool // is the variable captured by value or by reference 186 Needzero bool // if it contains pointers, needs to be zeroed on function entry 187 Keepalive bool // mark value live across unknown assembly call 188 } 189 190 type Param struct { 191 Ntype *Node 192 193 // ONAME PAUTOHEAP 194 Stackcopy *Node // the PPARAM/PPARAMOUT on-stack slot (moved func params only) 195 196 // ONAME PPARAM 197 Field *Field // TFIELD in arg struct 198 199 // ONAME closure linkage 200 // Consider: 201 // 202 // func f() { 203 // x := 1 // x1 204 // func() { 205 // use(x) // x2 206 // func() { 207 // use(x) // x3 208 // --- parser is here --- 209 // }() 210 // }() 211 // } 212 // 213 // There is an original declaration of x and then a chain of mentions of x 214 // leading into the current function. Each time x is mentioned in a new closure, 215 // we create a variable representing x for use in that specific closure, 216 // since the way you get to x is different in each closure. 217 // 218 // Let's number the specific variables as shown in the code: 219 // x1 is the original x, x2 is when mentioned in the closure, 220 // and x3 is when mentioned in the closure in the closure. 221 // 222 // We keep these linked (assume N > 1): 223 // 224 // - x1.Defn = original declaration statement for x (like most variables) 225 // - x1.Innermost = current innermost closure x (in this case x3), or nil for none 226 // - x1.isClosureVar() = false 227 // 228 // - xN.Defn = x1, N > 1 229 // - xN.isClosureVar() = true, N > 1 230 // - x2.Outer = nil 231 // - xN.Outer = x(N-1), N > 2 232 // 233 // 234 // When we look up x in the symbol table, we always get x1. 235 // Then we can use x1.Innermost (if not nil) to get the x 236 // for the innermost known closure function, 237 // but the first reference in a closure will find either no x1.Innermost 238 // or an x1.Innermost with .Funcdepth < Funcdepth. 239 // In that case, a new xN must be created, linked in with: 240 // 241 // xN.Defn = x1 242 // xN.Outer = x1.Innermost 243 // x1.Innermost = xN 244 // 245 // When we finish the function, we'll process its closure variables 246 // and find xN and pop it off the list using: 247 // 248 // x1 := xN.Defn 249 // x1.Innermost = xN.Outer 250 // 251 // We leave xN.Innermost set so that we can still get to the original 252 // variable quickly. Not shown here, but once we're 253 // done parsing a function and no longer need xN.Outer for the 254 // lexical x reference links as described above, closurebody 255 // recomputes xN.Outer as the semantic x reference link tree, 256 // even filling in x in intermediate closures that might not 257 // have mentioned it along the way to inner closures that did. 258 // See closurebody for details. 259 // 260 // During the eventual compilation, then, for closure variables we have: 261 // 262 // xN.Defn = original variable 263 // xN.Outer = variable captured in next outward scope 264 // to make closure where xN appears 265 // 266 // Because of the sharding of pieces of the node, x.Defn means x.Name.Defn 267 // and x.Innermost/Outer means x.Name.Param.Innermost/Outer. 268 Innermost *Node 269 Outer *Node 270 } 271 272 // Func holds Node fields used only with function-like nodes. 273 type Func struct { 274 Shortname *Node 275 Enter Nodes // for example, allocate and initialize memory for escaping parameters 276 Exit Nodes 277 Cvars Nodes // closure params 278 Dcl []*Node // autodcl for this func/closure 279 Inldcl Nodes // copy of dcl for use in inlining 280 Closgen int 281 Outerfunc *Node // outer function (for closure) 282 FieldTrack map[*Sym]struct{} 283 Ntype *Node // signature 284 Top int // top context (Ecall, Eproc, etc) 285 Closure *Node // OCLOSURE <-> ODCLFUNC 286 FCurfn *Node 287 Nname *Node 288 289 Inl Nodes // copy of the body for use in inlining 290 InlCost int32 291 Depth int32 292 293 Endlineno int32 294 WBLineno int32 // line number of first write barrier 295 296 Pragma Pragma // go:xxx function annotations 297 Dupok bool // duplicate definitions ok 298 Wrapper bool // is method wrapper 299 Needctxt bool // function uses context register (has closure variables) 300 ReflectMethod bool // function calls reflect.Type.Method or MethodByName 301 } 302 303 type Op uint8 304 305 // Node ops. 306 const ( 307 OXXX = Op(iota) 308 309 // names 310 ONAME // var, const or func name 311 ONONAME // unnamed arg or return value: f(int, string) (int, error) { etc } 312 OTYPE // type name 313 OPACK // import 314 OLITERAL // literal 315 316 // expressions 317 OADD // Left + Right 318 OSUB // Left - Right 319 OOR // Left | Right 320 OXOR // Left ^ Right 321 OADDSTR // +{List} (string addition, list elements are strings) 322 OADDR // &Left 323 OANDAND // Left && Right 324 OAPPEND // append(List) 325 OARRAYBYTESTR // Type(Left) (Type is string, Left is a []byte) 326 OARRAYBYTESTRTMP // Type(Left) (Type is string, Left is a []byte, ephemeral) 327 OARRAYRUNESTR // Type(Left) (Type is string, Left is a []rune) 328 OSTRARRAYBYTE // Type(Left) (Type is []byte, Left is a string) 329 OSTRARRAYBYTETMP // Type(Left) (Type is []byte, Left is a string, ephemeral) 330 OSTRARRAYRUNE // Type(Left) (Type is []rune, Left is a string) 331 OAS // Left = Right or (if Colas=true) Left := Right 332 OAS2 // List = Rlist (x, y, z = a, b, c) 333 OAS2FUNC // List = Rlist (x, y = f()) 334 OAS2RECV // List = Rlist (x, ok = <-c) 335 OAS2MAPR // List = Rlist (x, ok = m["foo"]) 336 OAS2DOTTYPE // List = Rlist (x, ok = I.(int)) 337 OASOP // Left Etype= Right (x += y) 338 OASWB // Left = Right (with write barrier) 339 OCALL // Left(List) (function call, method call or type conversion) 340 OCALLFUNC // Left(List) (function call f(args)) 341 OCALLMETH // Left(List) (direct method call x.Method(args)) 342 OCALLINTER // Left(List) (interface method call x.Method(args)) 343 OCALLPART // Left.Right (method expression x.Method, not called) 344 OCAP // cap(Left) 345 OCLOSE // close(Left) 346 OCLOSURE // func Type { Body } (func literal) 347 OCMPIFACE // Left Etype Right (interface comparison, x == y or x != y) 348 OCMPSTR // Left Etype Right (string comparison, x == y, x < y, etc) 349 OCOMPLIT // Right{List} (composite literal, not yet lowered to specific form) 350 OMAPLIT // Type{List} (composite literal, Type is map) 351 OSTRUCTLIT // Type{List} (composite literal, Type is struct) 352 OARRAYLIT // Type{List} (composite literal, Type is array or slice) 353 OPTRLIT // &Left (left is composite literal) 354 OCONV // Type(Left) (type conversion) 355 OCONVIFACE // Type(Left) (type conversion, to interface) 356 OCONVNOP // Type(Left) (type conversion, no effect) 357 OCOPY // copy(Left, Right) 358 ODCL // var Left (declares Left of type Left.Type) 359 360 // Used during parsing but don't last. 361 ODCLFUNC // func f() or func (r) f() 362 ODCLFIELD // struct field, interface field, or func/method argument/return value. 363 ODCLCONST // const pi = 3.14 364 ODCLTYPE // type Int int 365 366 ODELETE // delete(Left, Right) 367 ODOT // Left.Sym (Left is of struct type) 368 ODOTPTR // Left.Sym (Left is of pointer to struct type) 369 ODOTMETH // Left.Sym (Left is non-interface, Right is method name) 370 ODOTINTER // Left.Sym (Left is interface, Right is method name) 371 OXDOT // Left.Sym (before rewrite to one of the preceding) 372 ODOTTYPE // Left.Right or Left.Type (.Right during parsing, .Type once resolved) 373 ODOTTYPE2 // Left.Right or Left.Type (.Right during parsing, .Type once resolved; on rhs of OAS2DOTTYPE) 374 OEQ // Left == Right 375 ONE // Left != Right 376 OLT // Left < Right 377 OLE // Left <= Right 378 OGE // Left >= Right 379 OGT // Left > Right 380 OIND // *Left 381 OINDEX // Left[Right] (index of array or slice) 382 OINDEXMAP // Left[Right] (index of map) 383 OKEY // Left:Right (key:value in struct/array/map literal, or slice index pair) 384 _ // was OPARAM, but cannot remove without breaking binary blob in builtin.go 385 OLEN // len(Left) 386 OMAKE // make(List) (before type checking converts to one of the following) 387 OMAKECHAN // make(Type, Left) (type is chan) 388 OMAKEMAP // make(Type, Left) (type is map) 389 OMAKESLICE // make(Type, Left, Right) (type is slice) 390 OMUL // Left * Right 391 ODIV // Left / Right 392 OMOD // Left % Right 393 OLSH // Left << Right 394 ORSH // Left >> Right 395 OAND // Left & Right 396 OANDNOT // Left &^ Right 397 ONEW // new(Left) 398 ONOT // !Left 399 OCOM // ^Left 400 OPLUS // +Left 401 OMINUS // -Left 402 OOROR // Left || Right 403 OPANIC // panic(Left) 404 OPRINT // print(List) 405 OPRINTN // println(List) 406 OPAREN // (Left) 407 OSEND // Left <- Right 408 OSLICE // Left[Right.Left : Right.Right] (Left is untypechecked or slice; Right.Op==OKEY) 409 OSLICEARR // Left[Right.Left : Right.Right] (Left is array) 410 OSLICESTR // Left[Right.Left : Right.Right] (Left is string) 411 OSLICE3 // Left[R.Left : R.R.Left : R.R.R] (R=Right; Left is untypedchecked or slice; R.Op and R.R.Op==OKEY) 412 OSLICE3ARR // Left[R.Left : R.R.Left : R.R.R] (R=Right; Left is array; R.Op and R.R.Op==OKEY) 413 ORECOVER // recover() 414 ORECV // <-Left 415 ORUNESTR // Type(Left) (Type is string, Left is rune) 416 OSELRECV // Left = <-Right.Left: (appears as .Left of OCASE; Right.Op == ORECV) 417 OSELRECV2 // List = <-Right.Left: (apperas as .Left of OCASE; count(List) == 2, Right.Op == ORECV) 418 OIOTA // iota 419 OREAL // real(Left) 420 OIMAG // imag(Left) 421 OCOMPLEX // complex(Left, Right) 422 423 // statements 424 OBLOCK // { List } (block of code) 425 OBREAK // break 426 OCASE // case List: Nbody (select case after processing; List==nil means default) 427 OXCASE // case List: Nbody (select case before processing; List==nil means default) 428 OCONTINUE // continue 429 ODEFER // defer Left (Left must be call) 430 OEMPTY // no-op (empty statement) 431 OFALL // fallthrough (after processing) 432 OXFALL // fallthrough (before processing) 433 OFOR // for Ninit; Left; Right { Nbody } 434 OGOTO // goto Left 435 OIF // if Ninit; Left { Nbody } else { Rlist } 436 OLABEL // Left: 437 OPROC // go Left (Left must be call) 438 ORANGE // for List = range Right { Nbody } 439 ORETURN // return List 440 OSELECT // select { List } (List is list of OXCASE or OCASE) 441 OSWITCH // switch Ninit; Left { List } (List is a list of OXCASE or OCASE) 442 OTYPESW // List = Left.(type) (appears as .Left of OSWITCH) 443 444 // types 445 OTCHAN // chan int 446 OTMAP // map[string]int 447 OTSTRUCT // struct{} 448 OTINTER // interface{} 449 OTFUNC // func() 450 OTARRAY // []int, [8]int, [N]int or [...]int 451 452 // misc 453 ODDD // func f(args ...int) or f(l...) or var a = [...]int{0, 1, 2}. 454 ODDDARG // func f(args ...int), introduced by escape analysis. 455 OINLCALL // intermediary representation of an inlined call. 456 OEFACE // itable and data words of an empty-interface value. 457 OITAB // itable word of an interface value. 458 OSPTR // base pointer of a slice or string. 459 OCLOSUREVAR // variable reference at beginning of closure function 460 OCFUNC // reference to c function pointer (not go func value) 461 OCHECKNIL // emit code to ensure pointer/interface not nil 462 OVARKILL // variable is dead 463 OVARLIVE // variable is alive 464 465 // thearch-specific registers 466 OREGISTER // a register, such as AX. 467 OINDREG // offset plus indirect of a register, such as 8(SP). 468 469 // arch-specific opcodes 470 OCMP // compare: ACMP. 471 ODEC // decrement: ADEC. 472 OINC // increment: AINC. 473 OEXTEND // extend: ACWD/ACDQ/ACQO. 474 OHMUL // high mul: AMUL/AIMUL for unsigned/signed (OMUL uses AIMUL for both). 475 OLROT // left rotate: AROL. 476 ORROTC // right rotate-carry: ARCR. 477 ORETJMP // return to other function 478 OPS // compare parity set (for x86 NaN check) 479 OPC // compare parity clear (for x86 NaN check) 480 OSQRT // sqrt(float64), on systems that have hw support 481 OGETG // runtime.getg() (read g pointer) 482 483 OEND 484 ) 485 486 // Nodes is a pointer to a slice of *Node. 487 // For fields that are not used in most nodes, this is used instead of 488 // a slice to save space. 489 type Nodes struct{ slice *[]*Node } 490 491 // Slice returns the entries in Nodes as a slice. 492 // Changes to the slice entries (as in s[i] = n) will be reflected in 493 // the Nodes. 494 func (n Nodes) Slice() []*Node { 495 if n.slice == nil { 496 return nil 497 } 498 return *n.slice 499 } 500 501 // Len returns the number of entries in Nodes. 502 func (n Nodes) Len() int { 503 if n.slice == nil { 504 return 0 505 } 506 return len(*n.slice) 507 } 508 509 // Index returns the i'th element of Nodes. 510 // It panics if n does not have at least i+1 elements. 511 func (n Nodes) Index(i int) *Node { 512 return (*n.slice)[i] 513 } 514 515 // First returns the first element of Nodes (same as n.Index(0)). 516 // It panics if n has no elements. 517 func (n Nodes) First() *Node { 518 return (*n.slice)[0] 519 } 520 521 // Second returns the second element of Nodes (same as n.Index(1)). 522 // It panics if n has fewer than two elements. 523 func (n Nodes) Second() *Node { 524 return (*n.slice)[1] 525 } 526 527 // Set sets n to a slice. 528 // This takes ownership of the slice. 529 func (n *Nodes) Set(s []*Node) { 530 if len(s) == 0 { 531 n.slice = nil 532 } else { 533 // Copy s and take address of t rather than s to avoid 534 // allocation in the case where len(s) == 0 (which is 535 // over 3x more common, dynamically, for make.bash). 536 t := s 537 n.slice = &t 538 } 539 } 540 541 // Set1 sets n to a slice containing a single node. 542 func (n *Nodes) Set1(node *Node) { 543 n.slice = &[]*Node{node} 544 } 545 546 // MoveNodes sets n to the contents of n2, then clears n2. 547 func (n *Nodes) MoveNodes(n2 *Nodes) { 548 n.slice = n2.slice 549 n2.slice = nil 550 } 551 552 // SetIndex sets the i'th element of Nodes to node. 553 // It panics if n does not have at least i+1 elements. 554 func (n Nodes) SetIndex(i int, node *Node) { 555 (*n.slice)[i] = node 556 } 557 558 // Addr returns the address of the i'th element of Nodes. 559 // It panics if n does not have at least i+1 elements. 560 func (n Nodes) Addr(i int) **Node { 561 return &(*n.slice)[i] 562 } 563 564 // Append appends entries to Nodes. 565 // If a slice is passed in, this will take ownership of it. 566 func (n *Nodes) Append(a ...*Node) { 567 if n.slice == nil { 568 if len(a) > 0 { 569 n.slice = &a 570 } 571 } else { 572 *n.slice = append(*n.slice, a...) 573 } 574 } 575 576 // AppendNodes appends the contents of *n2 to n, then clears n2. 577 func (n *Nodes) AppendNodes(n2 *Nodes) { 578 switch { 579 case n2.slice == nil: 580 case n.slice == nil: 581 n.slice = n2.slice 582 default: 583 *n.slice = append(*n.slice, *n2.slice...) 584 } 585 n2.slice = nil 586 }