compiler/mir/mirconstr

    Dark Mode
Search:
  Source   Edit

Implements routines and types that assist in producing MIR code.

Types

Context = object
  pos: NodeIndex
  num: int
Low-overhead object capturing part of the builder state when starting manual subtree construction.   Source   Edit
Fragment = object
  s: NodeSlice
  num: int                   ## number of child nodes in the fragment
  typ*: TypeId
Identifies a fragment (usually a sub-tree) within the staging buffer.   Source   Edit
MirBuilder = object
  front*: MirBuffer ## the front buffer, targeted by all add/subTree operations. By default,
                    ## this is the "final" buffer
  back: MirBuffer            ## by default, this the "staging" buffer
  swapped: bool ## whether the buffers were swapped. If 'true', `front` holds the
                ## staging buffer
  currentSourceId: SourceId ## the ID of the meta-data to associate with all added nodes (that
                            ## don't have an explicitly assigned source ID)
  locals*: PartialStore[LocalId, Local] ## new locals created with the builder
  nextLabel*: uint32         ## the ID to use when allocating a new label ID
  
Holds the state needed for building MIR trees and allocating temporaries. A double-buffering scheme is used.   Source   Edit
Value = object
  node: MirNode
  info: opt(SourceId)        ## the source ID associated with the node, or none
  
  Source   Edit

Procs

func add(bu: var MirBuilder; id: SourceId; n: sink MirNode) {....raises: [],
    tags: [].}
Adds n to the front buffer and sets the node's source/info ID to id, ignoring the active ID set via setSource.   Source   Edit
func add(bu: var MirBuilder; n: sink MirNode) {.inline, ...raises: [], tags: [].}
Emits n to the node buffers.   Source   Edit
func addLocal(bu: var MirBuilder; data: sink Local): LocalId {.inline,
    ...raises: [], tags: [].}
Adds a new local to the body and returns the ID to address it with.   Source   Edit
func alias(typ: TypeId; id: LocalId): Value {....raises: [], tags: [].}
  Source   Edit
func allocLabel(bu: var MirBuilder): LabelId {....raises: [], tags: [].}
Allocates a fresh label ID.   Source   Edit
func asgn(buf: var MirBuilder; a, b: Value) {....raises: [], tags: [].}
Emits a shallow assignment: a = b.   Source   Edit
func asgnMove(bu: var MirBuilder; a, b: Value) {....raises: [], tags: [].}
Emits a move assignment: a = move b.   Source   Edit
func bindImmutable(bu: var MirBuilder; tree: MirTree; lval: NodePosition): Value {.
    ...raises: [], tags: [].}
  Source   Edit
func bindMut(bu: var MirBuilder; tree: MirTree; lval: NodePosition): Value {.
    ...raises: [], tags: [].}
Creates an alias from the lvalue lval that supports mutations (e.g., using as the destination of an assignment, passing to var. parameter, etc.).   Source   Edit
proc createBody(builder: sink MirBuilder; sm: sink SourceMap): MirBody {.
    ...raises: [], tags: [].}
Creates a MIR body from builder and sm.   Source   Edit
func emitByName(bu: var MirBuilder; val: Value; e: EffectKind) {....raises: [],
    tags: [].}
  Source   Edit
func emitByVal(bu: var MirBuilder; y: Value) {....raises: [], tags: [].}
  Source   Edit
func emitFrom(bu: var MirBuilder; tree: MirTree; n: NodePosition) {....raises: [],
    tags: [].}
Emits the sub-tree at n within tree into bu's front buffer.   Source   Edit
func finish(bu: sink MirBuilder): auto {....raises: [], tags: [].}
Low-level procedure that consumes bu and returns the finished tree and partial store of the locals.   Source   Edit
func finish(bu: sink MirBuilder; locals: sink Store[LocalId, Local]): auto {.
    ...raises: [], tags: [].}
Returns the finished tree from bu, plus locals joined with the locals created with the builder. locals must be the store the bu was initially set-up with.   Source   Edit
proc finish(bu: var MirBuilder; saved: Context) {.inline, ...raises: [], tags: [].}
Finish the manual subtree and restores the saved context previously returned by start.   Source   Edit
func goto(bu: var MirBuilder; label: LabelId) {....raises: [], tags: [].}
  Source   Edit
func hasUnassigned(bu: MirBuilder): bool {....raises: [], tags: [].}
Returns whether nodes with unassigned info fields exist in the buffers.   Source   Edit
func initBuilder(id: SourceId; buf: sink MirNodeSeq = @[]): MirBuilder {.
    ...raises: [], tags: [].}
Initializes a MirBuilder with the given id and buf as the final buffer's initial content.   Source   Edit
func inline(bu: var MirBuilder; tree: MirTree; fr: NodePosition): Value {.
    ...raises: [], tags: [].}
Inlines the lvalue operand for non-mutating use. This is meant to be used for materialzing immutable arguments when inlining calls / expanding assignments.   Source   Edit
func join(bu: var MirBuilder; label: LabelId) {....raises: [], tags: [].}
Emits a join statement with label.   Source   Edit
func len(b: MirBuffer): int {.inline, ...raises: [], tags: [].}
  Source   Edit
func len(f: Fragment): int {....raises: [], tags: [].}
The number of nodes in the fragment.   Source   Edit
func literal(ast: AstId; typ: TypeId): Value {....raises: [], tags: [].}
  Source   Edit
func literal(kind: range[mnkIntLit .. mnkFloatLit]; n: NumberId; typ: TypeId): Value {.
    ...raises: [], tags: [].}
  Source   Edit
func literal(str: StringId; typ: TypeId): Value {....raises: [], tags: [].}
  Source   Edit
func materialize(bu: var MirBuilder; loc: Value): Value {....raises: [], tags: [].}
Captures the value of the location loc into a non-owning temporary and returns the name of the temporary.   Source   Edit
func materializeMove(bu: var MirBuilder; loc: Value): Value {....raises: [],
    tags: [].}
Emits a new owning temporary that's initialized with the moved-from loc.   Source   Edit
func move(bu: var MirBuilder; val: Value) {....raises: [], tags: [].}
Emits move val.   Source   Edit
func pop(bu: var MirBuilder; f: Fragment) {....raises: [], tags: [].}
Moves the expression/statement identified by v from the top of the staging buffer to the final buffer.   Source   Edit
func popSingle(bu: var MirBuilder; f: Fragment): Value {....raises: [], tags: [].}
Retrieves the topmost atom node identified by v from the staging buffer.   Source   Edit
func procNode(id: ProcedureId): MirNode {.inline, ...raises: [], tags: [].}
  Source   Edit
func setSource(bu: var MirBuilder; id: SourceId) {....raises: [], tags: [].}
Sets id as the active source/info ID. All nodes added after a call to setSource will use id for their info field.   Source   Edit
func staging(bu: var MirBuilder): var MirNodeSeq {.inline, ...raises: [], tags: [].}
Provides access to the staging buffer.   Source   Edit
proc start(bu: var MirBuilder; n: sink MirNode): Context {.inline, ...raises: [],
    tags: [].}
Starts a subtree with a root node of kind. Must be paired with a call to finish.   Source   Edit
func temp(typ: TypeId; id: LocalId): Value {....raises: [], tags: [].}
  Source   Edit
func toValue(id: ConstId; typ: TypeId): Value {....raises: [], tags: [].}
  Source   Edit
func toValue(id: GlobalId; typ: TypeId): Value {....raises: [], tags: [].}
  Source   Edit
func toValue(id: ProcedureId; typ: TypeId): Value {....raises: [], tags: [].}
  Source   Edit
func toValue(kind: range[mnkParam .. mnkLocal]; id: LocalId; typ: TypeId): Value {.
    ...raises: [], tags: [].}
  Source   Edit
func typ(val: Value): TypeId {....raises: [], tags: [].}
  Source   Edit
func typeLit(t: TypeId): Value {....raises: [], tags: [].}
  Source   Edit
func use(bu: var MirBuilder; val: sink Value) {.inline, ...raises: [], tags: [].}
Emits a use of val.   Source   Edit

Templates

template allocTemp(bu: var MirBuilder; t: TypeId; alias = false): Value
  Source   Edit
template buildBlock(bu: var MirBuilder; id: LabelId; body: untyped)
Emits body followed by a join statement for the given id.   Source   Edit
template buildCall(bu: var MirBuilder; prc: ProcedureId; t: TypeId;
                   body: untyped)
Build and emits a call tree to the active buffer. pt is the type of the procedure.   Source   Edit
template buildIf(bu: var MirBuilder; cond, body: untyped)
Emits the start and end of an if, with cond providing the MIR for the condition, and body providing the MIR for the body.   Source   Edit
template buildMagicCall(bu: var MirBuilder; m: TMagic; t: TypeId; body: untyped)
  Source   Edit
template buildStmt(bu: var MirBuilder; body: untyped)
A shortcut for push + pop.   Source   Edit
template buildStmt(bu: var MirBuilder; k: MirNodeKind; body: untyped)
Similar to buildStmt, but also starts a sub- tree of kind k.   Source   Edit
template emitByName(bu: var MirBuilder; e: EffectKind; body: untyped)
  Source   Edit
template pathNamed(bu: var MirBuilder; t: TypeId; f: int32; body: untyped)
Emits a mnkPathNamed expression.   Source   Edit
template pathPos(bu: var MirBuilder; t: TypeId; p: uint32; body: untyped)
Emits a mnkPathPos expression.   Source   Edit
template pathVariant(bu: var MirBuilder; t: TypeId; f: int32; body: untyped)
Emits a mnkPathVariant expression.   Source   Edit
template pos(f: Fragment): NodePosition
Returns the fragment's starting position in the staging buffer.   Source   Edit
template push(bu: var MirBuilder; body: untyped): Fragment
Makes the staging buffer the active one and runs body. The pushed but not-yet-popped fragment is returned.   Source   Edit
template rawBuildCall(bu: var MirBuilder; k: MirNodeKind; t: TypeId;
                      sideEffects: bool; body: untyped)
Builds a call tree, with k as the call kind, t as the call's return type, and sideEffects indicating whether the call potentially modifies global data.   Source   Edit
template scope(bu: var MirBuilder; body: untyped)
  Source   Edit
template stmtList(bu: var MirBuilder; body: untyped)
  Source   Edit
template subTree(bu: var MirBuilder; k: MirNodeKind; body: untyped)
  Source   Edit
template subTree(bu: var MirBuilder; n: MirNode; body: untyped)
  Source   Edit
template withFront(bu: var MirBuilder; body: untyped)
Runs body with the final buffer as the front buffer.   Source   Edit
template wrapAlias(bu: var MirBuilder; t: TypeId; body: untyped): Value
Emits an mnkBind statement with body as the lvalue expression. Returns the name of the alias.   Source   Edit
template wrapMutAlias(bu: var MirBuilder; t: TypeId; body: untyped): Value
Emits a mnkBindMut statement with body as the lvalue expression. Returns the name of the alias.   Source   Edit
template wrapTemp(bu: var MirBuilder; t: TypeId; body: untyped): Value
Emits a definition of a temporary with body as the initializer expression.   Source   Edit