syntax.go 18.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// “Abstract” syntax representation.

package gc

// A Node is a single node in the syntax tree.
// Actually the syntax tree is a syntax DAG, because there is only one
// node with Op=ONAME for a given instance of a variable x.
// The same is true for Op=OTYPE and Op=OLITERAL.
type Node struct {
14 15 16 17
	// Tree structure.
	// Generic recursive walks should follow these fields.
	Left  *Node
	Right *Node
18
	Ninit Nodes
19
	Nbody Nodes
20 21
	List  Nodes
	Rlist Nodes
22 23

	// most nodes
24 25
	Type *Type
	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 34
	Sym *Sym        // various
	E   interface{} // Opt or Val, see methods below
35

36 37 38 39 40 41 42
	// Various. Usually an offset into a struct. For example, ONAME nodes
	// that refer to local variables use it to identify their stack frame
	// position. ODOT, ODOTPTR, and OINDREG use it to indicate offset
	// relative to their base address. ONAME nodes on the left side of an
	// OKEY within an OSTRUCTLIT use it to store the named field's offset.
	// OXCASE and OXFALL use it to validate the use of fallthrough.
	// Possibly still more uses. If you find any, document them.
43
	Xoffset int64
44

45
	Lineno int32
46 47 48 49

	// OREGISTER, OINDREG
	Reg int16

50
	Esc uint16 // EscXXX
51

52 53 54
	Op        Op
	Ullman    uint8 // sethi/ullman number
	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 59 60 61 62 63 64 65 66 67 68 69 70
	Class     Class // PPARAM, PAUTO, PEXTERN, etc
	Embedded  uint8 // ODCLFIELD embedded type
	Colas     bool  // OAS resulting from :=
	Diag      uint8 // already printed error about this
	Noescape  bool  // func arguments do not escape; TODO(rsc): move Noescape to Func struct (see CL 7360)
	Walkdef   uint8
	Typecheck uint8
	Local     bool
	Dodata    uint8
	Initorder uint8
	Used      bool
	Isddd     bool // is the argument variadic
	Implicit  bool
71 72 73 74 75 76 77 78 79 80
	Addrtaken bool  // address taken, even if not moved to heap
	Assigned  bool  // is the variable ever assigned to
	Likely    int8  // likeliness of if statement
	hasVal    int8  // +1 for Val, -1 for Opt, 0 for not yet set
	flags     uint8 // TODO: store more bool fields in this flag field
}

const (
	hasBreak = 1 << iota
	notLiveAtEnd
81
	isClosureVar
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
)

func (n *Node) HasBreak() bool {
	return n.flags&hasBreak != 0
}
func (n *Node) SetHasBreak(b bool) {
	if b {
		n.flags |= hasBreak
	} else {
		n.flags &^= hasBreak
	}
}
func (n *Node) NotLiveAtEnd() bool {
	return n.flags&notLiveAtEnd != 0
}
func (n *Node) SetNotLiveAtEnd(b bool) {
	if b {
		n.flags |= notLiveAtEnd
	} else {
		n.flags &^= notLiveAtEnd
	}
103
}
104 105
func (n *Node) isClosureVar() bool {
	return n.flags&isClosureVar != 0
106
}
107
func (n *Node) setIsClosureVar(b bool) {
108
	if b {
109
		n.flags |= isClosureVar
110
	} else {
111
		n.flags &^= isClosureVar
112 113
	}
}
114 115 116 117 118 119 120 121 122 123 124 125 126 127

// Val returns the Val for the node.
func (n *Node) Val() Val {
	if n.hasVal != +1 {
		return Val{}
	}
	return Val{n.E}
}

// SetVal sets the Val for the node, which must not have been used with SetOpt.
func (n *Node) SetVal(v Val) {
	if n.hasVal == -1 {
		Debug['h'] = 1
		Dump("have Opt", n)
128
		Fatalf("have Opt")
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
	}
	n.hasVal = +1
	n.E = v.U
}

// Opt returns the optimizer data for the node.
func (n *Node) Opt() interface{} {
	if n.hasVal != -1 {
		return nil
	}
	return n.E
}

// SetOpt sets the optimizer data for the node, which must not have been used with SetVal.
// SetOpt(nil) is ignored for Vals to simplify call sites that are clearing Opts.
func (n *Node) SetOpt(x interface{}) {
	if x == nil && n.hasVal >= 0 {
		return
	}
	if n.hasVal == +1 {
		Debug['h'] = 1
		Dump("have Val", n)
151
		Fatalf("have Val")
152 153 154
	}
	n.hasVal = -1
	n.E = x
155 156
}

157
// Name holds Node fields used only by named nodes (ONAME, OPACK, OLABEL, ODCLFIELD, some OLITERAL).
158
type Name struct {
159 160 161 162 163 164
	Pack      *Node  // real package for import . names
	Pkg       *Pkg   // pkg for OPACK nodes
	Heapaddr  *Node  // temp holding heap address of param (could move to Param?)
	Inlvar    *Node  // ONAME substitute while inlining (could move to Param?)
	Defn      *Node  // initializing assignment
	Curfn     *Node  // function for local variables
165
	Param     *Param // additional fields for ONAME, ODCLFIELD
166 167 168
	Decldepth int32  // declaration loop depth, increased for every loop or label
	Vargen    int32  // unique name for ONAME within a function.  Function outputs are numbered starting at one.
	Iota      int32  // value if this name is iota
169 170
	Funcdepth int32
	Method    bool // OCALLMETH name
171 172 173 174
	Readonly  bool
	Captured  bool // is the variable captured by a closure
	Byval     bool // is the variable captured by value or by reference
	Needzero  bool // if it contains pointers, needs to be zeroed on function entry
175
	Keepalive bool // mark value live across unknown assembly call
176 177
}

178 179 180
type Param struct {
	Ntype *Node

181 182
	// ONAME PAUTOHEAP
	Stackcopy *Node // the PPARAM/PPARAMOUT on-stack slot (moved func params only)
183

184
	// ONAME PPARAM
185
	Field *Field // TFIELD in arg struct
186

187
	// ONAME closure linkage
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
	// Consider:
	//
	//	func f() {
	//		x := 1 // x1
	//		func() {
	//			use(x) // x2
	//			func() {
	//				use(x) // x3
	//				--- parser is here ---
	//			}()
	//		}()
	//	}
	//
	// There is an original declaration of x and then a chain of mentions of x
	// leading into the current function. Each time x is mentioned in a new closure,
	// we create a variable representing x for use in that specific closure,
	// since the way you get to x is different in each closure.
	//
	// Let's number the specific variables as shown in the code:
	// x1 is the original x, x2 is when mentioned in the closure,
	// and x3 is when mentioned in the closure in the closure.
	//
	// We keep these linked (assume N > 1):
	//
	//   - x1.Defn = original declaration statement for x (like most variables)
	//   - x1.Innermost = current innermost closure x (in this case x3), or nil for none
	//   - x1.isClosureVar() = false
	//
	//   - xN.Defn = x1, N > 1
	//   - xN.isClosureVar() = true, N > 1
	//   - x2.Outer = nil
	//   - xN.Outer = x(N-1), N > 2
	//
	//
	// When we look up x in the symbol table, we always get x1.
	// Then we can use x1.Innermost (if not nil) to get the x
	// for the innermost known closure function,
	// but the first reference in a closure will find either no x1.Innermost
	// or an x1.Innermost with .Funcdepth < Funcdepth.
	// In that case, a new xN must be created, linked in with:
	//
	//     xN.Defn = x1
	//     xN.Outer = x1.Innermost
	//     x1.Innermost = xN
	//
	// When we finish the function, we'll process its closure variables
	// and find xN and pop it off the list using:
	//
	//     x1 := xN.Defn
	//     x1.Innermost = xN.Outer
	//
	// We leave xN.Innermost set so that we can still get to the original
	// variable quickly. Not shown here, but once we're
	// done parsing a function and no longer need xN.Outer for the
	// lexical x reference links as described above, closurebody
	// recomputes xN.Outer as the semantic x reference link tree,
	// even filling in x in intermediate closures that might not
	// have mentioned it along the way to inner closures that did.
	// See closurebody for details.
	//
	// During the eventual compilation, then, for closure variables we have:
	//
	//     xN.Defn = original variable
	//     xN.Outer = variable captured in next outward scope
	//                to make closure where xN appears
	//
	// Because of the sharding of pieces of the node, x.Defn means x.Name.Defn
	// and x.Innermost/Outer means x.Name.Param.Innermost/Outer.
	Innermost *Node
257
	Outer     *Node
258 259
}

260 261
// Func holds Node fields used only with function-like nodes.
type Func struct {
262
	Shortname  *Node
263
	Enter      Nodes // for example, allocate and initialize memory for escaping parameters
264
	Exit       Nodes
265 266 267
	Cvars      Nodes   // closure params
	Dcl        []*Node // autodcl for this func/closure
	Inldcl     Nodes   // copy of dcl for use in inlining
268
	Closgen    int
Russ Cox's avatar
Russ Cox committed
269
	Outerfunc  *Node // outer function (for closure)
270
	FieldTrack map[*Sym]struct{}
271 272 273
	Ntype      *Node // signature
	Top        int   // top context (Ecall, Eproc, etc)
	Closure    *Node // OCLOSURE <-> ODCLFUNC
274
	FCurfn     *Node
275
	Nname      *Node
276

277
	Inl     Nodes // copy of the body for use in inlining
278
	InlCost int32
279
	Depth   int32
280

281
	Endlineno int32
282
	WBLineno  int32 // line number of first write barrier
283

284 285 286 287 288
	Pragma        Pragma // go:xxx function annotations
	Dupok         bool   // duplicate definitions ok
	Wrapper       bool   // is method wrapper
	Needctxt      bool   // function uses context register (has closure variables)
	ReflectMethod bool   // function calls reflect.Type.Method or MethodByName
289 290
}

291 292
type Op uint8

293 294
// Node ops.
const (
295
	OXXX = Op(iota)
296 297 298 299 300 301 302 303 304

	// names
	ONAME    // var, const or func name
	ONONAME  // unnamed arg or return value: f(int, string) (int, error) { etc }
	OTYPE    // type name
	OPACK    // import
	OLITERAL // literal

	// expressions
305 306 307 308
	OADD             // Left + Right
	OSUB             // Left - Right
	OOR              // Left | Right
	OXOR             // Left ^ Right
309
	OADDSTR          // +{List} (string addition, list elements are strings)
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
	OADDR            // &Left
	OANDAND          // Left && Right
	OAPPEND          // append(List)
	OARRAYBYTESTR    // Type(Left) (Type is string, Left is a []byte)
	OARRAYBYTESTRTMP // Type(Left) (Type is string, Left is a []byte, ephemeral)
	OARRAYRUNESTR    // Type(Left) (Type is string, Left is a []rune)
	OSTRARRAYBYTE    // Type(Left) (Type is []byte, Left is a string)
	OSTRARRAYBYTETMP // Type(Left) (Type is []byte, Left is a string, ephemeral)
	OSTRARRAYRUNE    // Type(Left) (Type is []rune, Left is a string)
	OAS              // Left = Right or (if Colas=true) Left := Right
	OAS2             // List = Rlist (x, y, z = a, b, c)
	OAS2FUNC         // List = Rlist (x, y = f())
	OAS2RECV         // List = Rlist (x, ok = <-c)
	OAS2MAPR         // List = Rlist (x, ok = m["foo"])
	OAS2DOTTYPE      // List = Rlist (x, ok = I.(int))
	OASOP            // Left Etype= Right (x += y)
	OASWB            // Left = Right (with write barrier)
	OCALL            // Left(List) (function call, method call or type conversion)
	OCALLFUNC        // Left(List) (function call f(args))
	OCALLMETH        // Left(List) (direct method call x.Method(args))
	OCALLINTER       // Left(List) (interface method call x.Method(args))
	OCALLPART        // Left.Right (method expression x.Method, not called)
	OCAP             // cap(Left)
	OCLOSE           // close(Left)
	OCLOSURE         // func Type { Body } (func literal)
	OCMPIFACE        // Left Etype Right (interface comparison, x == y or x != y)
	OCMPSTR          // Left Etype Right (string comparison, x == y, x < y, etc)
	OCOMPLIT         // Right{List} (composite literal, not yet lowered to specific form)
	OMAPLIT          // Type{List} (composite literal, Type is map)
	OSTRUCTLIT       // Type{List} (composite literal, Type is struct)
	OARRAYLIT        // Type{List} (composite literal, Type is array or slice)
	OPTRLIT          // &Left (left is composite literal)
	OCONV            // Type(Left) (type conversion)
	OCONVIFACE       // Type(Left) (type conversion, to interface)
	OCONVNOP         // Type(Left) (type conversion, no effect)
	OCOPY            // copy(Left, Right)
	ODCL             // var Left (declares Left of type Left.Type)

	// Used during parsing but don't last.
	ODCLFUNC  // func f() or func (r) f()
	ODCLFIELD // struct field, interface field, or func/method argument/return value.
	ODCLCONST // const pi = 3.14
	ODCLTYPE  // type Int int

	ODELETE    // delete(Left, Right)
355 356 357 358 359
	ODOT       // Left.Sym (Left is of struct type)
	ODOTPTR    // Left.Sym (Left is of pointer to struct type)
	ODOTMETH   // Left.Sym (Left is non-interface, Right is method name)
	ODOTINTER  // Left.Sym (Left is interface, Right is method name)
	OXDOT      // Left.Sym (before rewrite to one of the preceding)
360 361 362 363 364 365 366 367 368 369 370 371
	ODOTTYPE   // Left.Right or Left.Type (.Right during parsing, .Type once resolved)
	ODOTTYPE2  // Left.Right or Left.Type (.Right during parsing, .Type once resolved; on rhs of OAS2DOTTYPE)
	OEQ        // Left == Right
	ONE        // Left != Right
	OLT        // Left < Right
	OLE        // Left <= Right
	OGE        // Left >= Right
	OGT        // Left > Right
	OIND       // *Left
	OINDEX     // Left[Right] (index of array or slice)
	OINDEXMAP  // Left[Right] (index of map)
	OKEY       // Left:Right (key:value in struct/array/map literal, or slice index pair)
372
	_          // was OPARAM, but cannot remove without breaking binary blob in builtin.go
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
	OLEN       // len(Left)
	OMAKE      // make(List) (before type checking converts to one of the following)
	OMAKECHAN  // make(Type, Left) (type is chan)
	OMAKEMAP   // make(Type, Left) (type is map)
	OMAKESLICE // make(Type, Left, Right) (type is slice)
	OMUL       // Left * Right
	ODIV       // Left / Right
	OMOD       // Left % Right
	OLSH       // Left << Right
	ORSH       // Left >> Right
	OAND       // Left & Right
	OANDNOT    // Left &^ Right
	ONEW       // new(Left)
	ONOT       // !Left
	OCOM       // ^Left
	OPLUS      // +Left
	OMINUS     // -Left
	OOROR      // Left || Right
	OPANIC     // panic(Left)
	OPRINT     // print(List)
	OPRINTN    // println(List)
	OPAREN     // (Left)
	OSEND      // Left <- Right
	OSLICE     // Left[Right.Left : Right.Right] (Left is untypechecked or slice; Right.Op==OKEY)
	OSLICEARR  // Left[Right.Left : Right.Right] (Left is array)
	OSLICESTR  // Left[Right.Left : Right.Right] (Left is string)
	OSLICE3    // Left[R.Left : R.R.Left : R.R.R] (R=Right; Left is untypedchecked or slice; R.Op and R.R.Op==OKEY)
	OSLICE3ARR // Left[R.Left : R.R.Left : R.R.R] (R=Right; Left is array; R.Op and R.R.Op==OKEY)
	ORECOVER   // recover()
	ORECV      // <-Left
	ORUNESTR   // Type(Left) (Type is string, Left is rune)
	OSELRECV   // Left = <-Right.Left: (appears as .Left of OCASE; Right.Op == ORECV)
	OSELRECV2  // List = <-Right.Left: (apperas as .Left of OCASE; count(List) == 2, Right.Op == ORECV)
	OIOTA      // iota
	OREAL      // real(Left)
	OIMAG      // imag(Left)
	OCOMPLEX   // complex(Left, Right)
410 411

	// statements
412
	OBLOCK    // { List } (block of code)
413
	OBREAK    // break
414 415
	OCASE     // case List: Nbody (select case after processing; List==nil means default)
	OXCASE    // case List: Nbody (select case before processing; List==nil means default)
416
	OCONTINUE // continue
417 418 419 420 421 422 423 424 425 426 427 428 429 430
	ODEFER    // defer Left (Left must be call)
	OEMPTY    // no-op (empty statement)
	OFALL     // fallthrough (after processing)
	OXFALL    // fallthrough (before processing)
	OFOR      // for Ninit; Left; Right { Nbody }
	OGOTO     // goto Left
	OIF       // if Ninit; Left { Nbody } else { Rlist }
	OLABEL    // Left:
	OPROC     // go Left (Left must be call)
	ORANGE    // for List = range Right { Nbody }
	ORETURN   // return List
	OSELECT   // select { List } (List is list of OXCASE or OCASE)
	OSWITCH   // switch Ninit; Left { List } (List is a list of OXCASE or OCASE)
	OTYPESW   // List = Left.(type) (appears as .Left of OSWITCH)
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

	// types
	OTCHAN   // chan int
	OTMAP    // map[string]int
	OTSTRUCT // struct{}
	OTINTER  // interface{}
	OTFUNC   // func()
	OTARRAY  // []int, [8]int, [N]int or [...]int

	// misc
	ODDD        // func f(args ...int) or f(l...) or var a = [...]int{0, 1, 2}.
	ODDDARG     // func f(args ...int), introduced by escape analysis.
	OINLCALL    // intermediary representation of an inlined call.
	OEFACE      // itable and data words of an empty-interface value.
	OITAB       // itable word of an interface value.
	OSPTR       // base pointer of a slice or string.
	OCLOSUREVAR // variable reference at beginning of closure function
	OCFUNC      // reference to c function pointer (not go func value)
	OCHECKNIL   // emit code to ensure pointer/interface not nil
	OVARKILL    // variable is dead
451
	OVARLIVE    // variable is alive
452 453 454 455 456

	// thearch-specific registers
	OREGISTER // a register, such as AX.
	OINDREG   // offset plus indirect of a register, such as 8(SP).

457
	// arch-specific opcodes
458 459 460 461 462 463 464 465
	OCMP    // compare: ACMP.
	ODEC    // decrement: ADEC.
	OINC    // increment: AINC.
	OEXTEND // extend: ACWD/ACDQ/ACQO.
	OHMUL   // high mul: AMUL/AIMUL for unsigned/signed (OMUL uses AIMUL for both).
	OLROT   // left rotate: AROL.
	ORROTC  // right rotate-carry: ARCR.
	ORETJMP // return to other function
466
	OPS     // compare parity set (for x86 NaN check)
467
	OPC     // compare parity clear (for x86 NaN check)
468
	OSQRT   // sqrt(float64), on systems that have hw support
469
	OGETG   // runtime.getg() (read g pointer)
470

471 472 473
	OEND
)

474 475 476 477 478 479 480 481
// Nodes is a pointer to a slice of *Node.
// For fields that are not used in most nodes, this is used instead of
// a slice to save space.
type Nodes struct{ slice *[]*Node }

// Slice returns the entries in Nodes as a slice.
// Changes to the slice entries (as in s[i] = n) will be reflected in
// the Nodes.
482
func (n Nodes) Slice() []*Node {
483 484 485 486 487 488
	if n.slice == nil {
		return nil
	}
	return *n.slice
}

489
// Len returns the number of entries in Nodes.
490
func (n Nodes) Len() int {
491 492 493 494 495 496
	if n.slice == nil {
		return 0
	}
	return len(*n.slice)
}

497 498
// Index returns the i'th element of Nodes.
// It panics if n does not have at least i+1 elements.
499
func (n Nodes) Index(i int) *Node {
500 501 502 503 504
	return (*n.slice)[i]
}

// First returns the first element of Nodes (same as n.Index(0)).
// It panics if n has no elements.
505
func (n Nodes) First() *Node {
506 507 508
	return (*n.slice)[0]
}

509 510
// Second returns the second element of Nodes (same as n.Index(1)).
// It panics if n has fewer than two elements.
511
func (n Nodes) Second() *Node {
512 513 514
	return (*n.slice)[1]
}

515
// Set sets n to a slice.
516 517 518 519 520
// This takes ownership of the slice.
func (n *Nodes) Set(s []*Node) {
	if len(s) == 0 {
		n.slice = nil
	} else {
521 522 523 524 525
		// Copy s and take address of t rather than s to avoid
		// allocation in the case where len(s) == 0 (which is
		// over 3x more common, dynamically, for make.bash).
		t := s
		n.slice = &t
526 527 528
	}
}

529 530 531 532 533
// Set1 sets n to a slice containing a single node.
func (n *Nodes) Set1(node *Node) {
	n.slice = &[]*Node{node}
}

534 535 536 537 538 539 540 541
// MoveNodes sets n to the contents of n2, then clears n2.
func (n *Nodes) MoveNodes(n2 *Nodes) {
	n.slice = n2.slice
	n2.slice = nil
}

// SetIndex sets the i'th element of Nodes to node.
// It panics if n does not have at least i+1 elements.
542
func (n Nodes) SetIndex(i int, node *Node) {
543 544 545 546 547
	(*n.slice)[i] = node
}

// Addr returns the address of the i'th element of Nodes.
// It panics if n does not have at least i+1 elements.
548
func (n Nodes) Addr(i int) **Node {
549 550 551
	return &(*n.slice)[i]
}

552 553 554 555 556 557 558 559 560 561 562
// Append appends entries to Nodes.
// If a slice is passed in, this will take ownership of it.
func (n *Nodes) Append(a ...*Node) {
	if n.slice == nil {
		if len(a) > 0 {
			n.slice = &a
		}
	} else {
		*n.slice = append(*n.slice, a...)
	}
}
563

564 565 566 567 568 569 570 571 572 573 574
// AppendNodes appends the contents of *n2 to n, then clears n2.
func (n *Nodes) AppendNodes(n2 *Nodes) {
	switch {
	case n2.slice == nil:
	case n.slice == nil:
		n.slice = n2.slice
	default:
		*n.slice = append(*n.slice, *n2.slice...)
	}
	n2.slice = nil
}