compiler/backend/mir2cg

  Source   Edit

Implements the MIR -> CGIR translation. At the moment, a lot of the NimSkull's semantics are decided here.

Nomenclature:

  • translating MIR trees / concepts to CGIR -> xToCgir, where x is the MIR level tree/concept
  • routines that emit one more CGIR statements -> emitX
  • routines that produce CGIR AST -> genX or just X

Types

Capability = enum
  capExceptions              ## the code generator supports checked calls, raise, etc.
A capability of the targeted code generator.   Source   Edit
Context = object
  graph {.requiresInit.}: ModuleGraph
  caps: set[Capability]      ## capability of the targeted code generator
  module: CgModule           ## the in-progress module
  tctx: mirtypes2cg.Context
  rttiV1Map: Table[SigHash, StringId] ## maps unified types (via their sighash) to their RTTIv1 global
  rttiV2Map: Table[SigHash, StringId] ## maps unified types (via their sighash) to their RTTIv2 global
  rtti: seq[tuple[global: StringId, id: ItemId]] ## all rtti globals together with their home module
  rttiV1Type: TypeId         ## cached type ID for the ``TNimType`` type
  rttiV1NodeType: TypeId     ## cached type ID for the ``TNimNode`` type
  rttiV2Type: TypeId         ## cached type ID for the ``TNimTypeV2`` type
  names: Table[int, StringId] ## symbol IDs -> external names
  dataMap: Table[DataId, Datum] ## maps MIR data to the corresponding CGIR data. Populated on demand
  defaults: Table[TypeId, Datum] ## type -> default value for the type
  sourceLocs: BiTable[SourceLoc]
  fullPaths: Table[FileIndex, StringId] ## caches the packed string for full module paths
  fileNames: Table[FileIndex, StringId] ## caches the packed string for module names/paths. Populated on demand
                                        ## and used to speed up line-trace generation
  prc: ProcContext
  Source   Edit

Consts

NonMagics = {mNewSeq, mSetLengthSeq, mAppendSeqElem, mEnumToStr, mDotDot,
             mEqCString, mAbsI, mExit, mIsolate}
  Source   Edit

Procs

proc close(c: sink Context): CgModule {....raises: [], tags: [].}
Produces the completed CGIR module.   Source   Edit
proc current(c: Context): lent CgModule {....raises: [], tags: [].}
Returns the in-progress and thus possibly incomplete CG module constructed so far.   Source   Edit
proc defineConst(c; env: var MirEnv; id: ConstId): StringId {.
    ...raises: [KeyError, Exception, ERecoverableError],
    tags: [RootEffect, ReadDirEffect].}
Adds a CGIR definition for the given constant, returning the constant's external named.   Source   Edit
proc defineDynlibProc(c; env; id: ProcedureId): StringId {.
    ...raises: [Exception, KeyError], tags: [RootEffect, ReadDirEffect].}
Adds a definition for the .dynlib procedure identified by id to the module.   Source   Edit
proc defineForeignProc(c; env; id: ProcedureId): StringId {....raises: [Exception],
    tags: [RootEffect].}
Adds a CGIR definition for the given foreign procedure to the module, returning the procedure's external name.   Source   Edit
proc defineGlobal(c; env: var MirEnv; id: GlobalId): StringId {.
    ...raises: [KeyError, Exception], tags: [RootEffect, ReadDirEffect].}
Adds a CGIR definition for the given global to the module, returning the global's external name.   Source   Edit
proc defineProc(c; env: var MirEnv; id: ProcedureId; body: sink MirBody): StringId {.
    ...raises: [KeyError, Exception, ERecoverableError, ValueError],
    tags: [RootEffect, ReadDirEffect].}
Translates the MIR procedure to a CGIR procedure, adding it to the module under the returned name.   Source   Edit
proc genAbiCheck(c; env: MirEnv; id: TypeId): cgir2.NodeIndex {.
    ...raises: [Exception, ValueError, KeyError], tags: [RootEffect, ReadDirEffect].}
Generates an emit statement for a C ABI check.   Source   Edit
proc genLocalDef(c; env; id: LocalId; bu): NodeRef {....raises: [Exception],
    tags: [RootEffect].}
Generates the Def for the given local, also adding the local to the translation context.   Source   Edit
proc getRtti(c: Context): lent seq[(StringId, ItemId)] {....raises: [], tags: [].}
Returns the list of all RTTI globals created so far, together with their originating-from type's ID.   Source   Edit
proc initContext(g: ModuleGraph; caps: set[Capability]): Context {....raises: [],
    tags: [].}
Creates a translation context for a target with the given capabilities.   Source   Edit
proc name(c; s: PSym): Option[StringId] {....raises: [], tags: [].}
For symbols referring to external symbols, returns none. For everything else, returns the external name.   Source   Edit
proc translateTopLevelEmit(c; env: var MirEnv; s: sink MirBody): cgir2.NodeIndex {.
    ...raises: [Exception, KeyError, ERecoverableError],
    tags: [RootEffect, ReadDirEffect].}
Translates the top-level emit or asm statement s to the corresponding CGIR statement, returning its position in the AST.   Source   Edit