compiler/backend/cgirbuilder

  Source   Edit

Provides some convenience types and routines for building CGIR trees. The underlying idea is to use one big staging buffer that contains "ref" nodes, which are nodes storing a local pointer to some node sequence. Once building is finished, all references are resolved (i.e., the ref is replaced with the ref'd tree).

The downside is that this requires a resolution pass. On the upside, this allows for much easier construction, and there's also less temporary sequences.

Types

Builder = object
  nodes: seq[CgNode]
  loc: uint32                ## source-loc to use for nodes
  info: TLineInfo            ## info corresponding to `loc`
  
  Source   Edit
NodeRef = distinct CgNode
Represents a reference to some node. May only be used with the Builder it originated from.   Source   Edit

Procs

proc append(to: var seq[CgNode]; bu: sink Builder; start: NodeRef): NodeIndex {.
    ...raises: [], tags: [].}
Copies the node/tree referenced by start stored in bu appending it to to.   Source   Edit
func get(bu: Builder): (uint32, TLineInfo) {.inline, ...raises: [], tags: [].}
Returns the current source-loc/info state.   Source   Edit
proc initBuilder(): Builder {....raises: [], tags: [].}
  Source   Edit
func update(bu: var Builder; loc: uint32; info: TLineInfo) {.inline, ...raises: [],
    tags: [].}
Sets the current source-loc/info state.   Source   Edit

Macros

macro build(b: var Builder; e: untyped): NodeRef

Constructs the sub-tree represented by e and returns a reference to it.

The following syntax is used for e:

  • a call expression describes a tree; it can be arbitrarily nested, and every argument represents a child node/sub-tree
  • an unary hat (^) is used as the unquote operator; the expression following it is evaluated normally and passed to valToNode (mixed in at the callsite)
  • identifiers and literal values are implicitly unquoted (e.g., x is the same as ^x)
  • the star (*) operator is used for custom forms. *x(y, z) expands to x(<builder>, build(<builder>, y), build(<builder>, z))

Example construction expression:

Call(val, ^complex(...), *call(...))
  Source   Edit