compiler/mir/mirenv

  Source   Edit

Implements the MirEnv data type together with routines for querying and manipulating it.

Types

EnvCheckpoint = tuple[procs, globals, consts, data: Checkpoint]
A low-cost snapshot of a MirEnv.   Source   Edit
MirEnv = object
  data*: DataTable           ## stores all constant data referenced by constants and MIR code
  constants*: SymbolTable[ConstId, PSym]
  globals*: SymbolTable[GlobalId, PSym] ## includes both normal globals and threadvars
  procedures*: SymbolTable[ProcedureId, PSym]
  types*: TypeEnv            ## the type environment
  numbers*: BiTable[BiggestInt] ## all numerical values referenced by the MIR, stored as bit patterns
  strings*: BiTable[string]  ## all string data referenced by the MIR
  asts*: Store[AstId, PNode] ## all AST fragments referenced by the MIR. No unification is
                             ## performed
  bodies*: OrdinalSeq[ConstId, DataId] ## associates each user-defined constant with its content
                                       ## ## TODO: this needs to be merged into `constants`
  
Stores everything MIR-related that is shared/exists across MIR bodies, such as information about global variables, global constants, etc.   Source   Edit
SymbolTable[I; T] = object
  data: Store[I, T]
  map: Table[int, I]         ## maps a symbol ID to the corresponding entity ID
  

Acts as the central store for a kind of symbol during the mid- and backend phase. Also keeps mappings for PSym to the corresponding backend symbol.

In the mid- and backend, symbols are represented with IDs rather than with ref types.

  Source   Edit

Procs

func `[]`(env: MirEnv; id: AstId): lent PNode {.inline, ...raises: [], tags: [].}
  Source   Edit
func `[]`(env: MirEnv; id: ConstId): lent PSym {.inline, ...raises: [], tags: [].}
  Source   Edit
func `[]`(env: MirEnv; id: DataId): lent ConstrTree {.inline, ...raises: [],
    tags: [].}
  Source   Edit
func `[]`(env: MirEnv; id: GlobalId): lent PSym {.inline, ...raises: [], tags: [].}
  Source   Edit
func `[]`(env: MirEnv; id: ProcedureId): lent PSym {.inline, ...raises: [],
    tags: [].}
  Source   Edit
func `[]`(env: MirEnv; id: StringId): lent string {.inline, ...raises: [], tags: [].}
  Source   Edit
func `[]`[I; T](tab: SymbolTable[I, T]; id: I): lent T {.inline.}
  Source   Edit
func `[]`[I; T](tab: SymbolTable[I, T]; s: PSym): I {.inline.}
Looks up the ID that s is represented with in tab. s must have already been registered in the table.   Source   Edit
func add[I; T](tab: var SymbolTable[I, T]; s: PSym): I
If s was not yet added to tab, translates s to the corresponding MIR representation and returns the ID to fetch the entity from tab with. If s was already added, returns the ID of the already-translated entity.   Source   Edit
func checkpoint(env: MirEnv): EnvCheckpoint {....raises: [], tags: [].}
Creates a snapshot of env. This is a low-cost operation, where no copies are involved.   Source   Edit
func checkpoint[I, T](tab: SymbolTable[I, T]): Checkpoint
Creates a checkpoint that represents the current state of tab. This is a very low-cost operation, which doesn't perform any heap allocation.   Source   Edit
func contains[I, T](tab: SymbolTable[I, T]; s: PSym): bool {.inline.}
Returns whether s was already registered with tab.   Source   Edit
func dataFor(env: MirEnv; id: ConstId): DataId {....raises: [], tags: [].}
Returns the ID of the constant expression associated with id.   Source   Edit
func getFloat(env: MirEnv; id: NumberId): BiggestFloat {.inline, ...raises: [],
    tags: [].}
  Source   Edit
func getInt(env: MirEnv; id: NumberId): BiggestInt {.inline, ...raises: [],
    tags: [].}
  Source   Edit
func getOrIncl(env: var MirEnv; str: string): StringId {.inline, ...raises: [],
    tags: [].}
If not registered already, adds str to the environment.   Source   Edit
func getOrIncl(env: var MirEnv; v: BiggestInt | BiggestUInt | BiggestFloat): NumberId {.
    inline.}
If not registered already, adds v to the environment.   Source   Edit
func getUInt(env: MirEnv; id: NumberId): BiggestUInt {.inline, ...raises: [],
    tags: [].}
  Source   Edit
proc initMirEnv(g: ModuleGraph): MirEnv {.
    ...raises: [KeyError, Exception, ERecoverableError],
    tags: [ReadDirEffect, RootEffect].}
  Source   Edit
proc len[I, T](tab: SymbolTable[I, T]): int
The number of entities in tab.   Source   Edit
proc rewind(env: var MirEnv; to: EnvCheckpoint) {....raises: [], tags: [].}
Undoes all additions to env since to was created. Do note that all checkpoints created after to are invalidated.   Source   Edit
func setData(env: var MirEnv; id: ConstId; data: DataId) {....raises: [], tags: [].}
Sets the body for the constant identified by id.   Source   Edit

Iterators

iterator items[I, T](tab: SymbolTable[I, T]): (I, lent T)
Returns all entities in tab together with their ID.   Source   Edit
iterator since[I; T](tab: SymbolTable[I, T]; p: Checkpoint): (I, lent T)
Returns all entities from tab that were added since p was created.   Source   Edit

Templates

template `[]`(env: MirEnv; id: TypeId): PType
  Source   Edit