compiler/backend/cgendata

  Source   Edit

This module contains the data structures for the C code generation phase.

Types

BModule = ref TCGen
  Source   Edit
BModuleList = ref object of RootObj
  mapping*: Rope             ## the generated mapping file (if requested)
  modules*: seq[BModule]     ## list of all compiled modules
  modulesClosed*: seq[BModule] ## list of the same compiled modules, but in the order they were closed
  generatedHeader*: BModule
  typeInfoMarker*: TypeCacheWithOwner
  typeInfoMarkerV2*: TypeCacheWithOwner
  config*: ConfigRef
  graph*: ModuleGraph
  nimtv*: Rope               ## Nim thread vars; the struct body
  nimtvDeps*: seq[PType]     ## type deps: every module needs whole struct
  nimtvDeclared*: IntSet     ## so that every var/field exists only once
                             ## in the struct
                             ## 'nimtv' is incredibly hard to modularize! Best
                             ## effort is to store all thread vars in a ROD
                             ## section and with their type deps and load them
                             ## unconditionally...
                             ## nimtvDeps is VERY hard to cache because it's
                             ## not a list of IDs nor can it be made to be one.
  env*: MirEnv               ## the project-wide MIR environment
  globals*: SeqMap[GlobalId, TLoc] ## the locs for all alive globals of the program
  consts*: SeqMap[ConstId, TLoc] ## the locs for all alive constants of the program
  procs*: SeqMap[ProcedureId, ProcLoc] ## the locs for all alive procedure of the program
  fields*: Table[FieldId, string] ## stores the C name for each field
  hooks*: seq[(BModule, ProcedureId)] ## late late-dependencies. Generating code for a procedure might lead
                                      ## to the RTTI setup code for some type from a foreign module (i.e., one
                                      ## different from the module that acts as the current context) to be
                                      ## emitted, and this setup code might reference additional procedures.
                                      ## Written: by the code generator and orchestrator; reset by the
                                      ##          orchestrator
                                      ## Read:    by the orchestrator
  
  Source   Edit
BProc = ref TCProc
  Source   Edit
CodegenFlag = enum
  preventStackTrace,        ## true if stack traces need to be prevented
  usesThreadVars,           ## true if the module uses a thread var
  frameDeclared,            ## hack for ROD support so that we don't declare
                             ## a frame var twice in an init proc
  isHeaderFile,             ## C source file is the header file
  includesStringh            ## C source file already includes ``<string.h>``
  Source   Edit
LocFlag = enum
  lfIndirect,               ## code generator introduced a pointer
  lfSingleUse,              ## no location yet and will only be used once
  lfEnforceDeref,           ## a copyMem is required to dereference if this a
                             ## 
                             ## ptr array due to C array limitations.
                             ## See #1181, #6422, #11171
  lfWantLvalue               ## on empty locs, signals that a C lvalue is
                             ## expected
  Source   Edit
ProcLoc = object
  name*: string              ## the name of the C function in the generated
                             ## code
  params*: seq[TLoc]         ## the locs of the parameters
  
  Source   Edit
TBlock = object
  sections*: TCProcSections  ## the code belonging
  frameLen*: int16
  Source   Edit
TCFileSection = enum
  cfsMergeInfo,             ## section containing merge information
  cfsHeaders,               ## section for C include file headers
  cfsFrameDefines,          ## section for nim frame macros
  cfsForwardTypes,          ## section for C forward typedefs
  cfsTypes,                 ## section for C typedefs
  cfsSeqTypes,              ## section for sequence types only
                             ## this is needed for strange type generation
                             ## reasons
  cfsFieldInfo,             ## section for field information
  cfsTypeInfo,              ## section for type information (ag ABI checks)
  cfsProcHeaders,           ## section for C procs prototypes
  cfsData,                  ## section for C constant data
  cfsVars,                  ## section for C variable declarations
  cfsProcs,                 ## section for C procs that are not inline
  cfsInitProc,              ## section for the C init proc
  cfsDatInitProc,           ## section for the C datInit proc
  cfsTypeInit1,             ## section 1 for declarations of type information
  cfsTypeInit2,             ## section 2 for init of type information
  cfsTypeInit3,             ## section 3 for init of type information
  cfsDebugInit               ## section for init of debug information
the sections a generated C file consists of   Source   Edit
TCFileSections = array[TCFileSection, Rope]
represents a generated C file   Source   Edit
TCProcFlag = enum
  beforeRetNeeded, threadVarAccessed, hasCurFramePointer, nimErrorFlagAccessed,
  nimErrorFlagDeclared, nimErrorFlagDisabled
  Source   Edit
TCProcSection = enum
  cpsLocals,                ## section of local variables for C proc
  cpsInit,                  ## section for init of variables for C proc
  cpsStmts                   ## section of local statements for C proc
the sections a generated C proc consists of   Source   Edit
TCProcSections = array[TCProcSection, Rope]
  Source   Edit
TCTypeKind = enum
  ctVoid, ctChar, ctBool, ctInt, ctInt8, ctInt16, ctInt32, ctInt64, ctFloat,
  ctFloat32, ctFloat64, ctUInt, ctUInt8, ctUInt16, ctUInt32, ctUInt64, ctArray,
  ctPtrToArray, ctStruct, ctPtr, ctNimStr, ctNimSeq, ctProc, ctNimOpenArray,
  ctCString
describes the type kind of a C type   Source   Edit
TLabel = Rope
for the C generator a label is just a rope   Source   Edit
TLoc = object
  k*: TLocKind               ## kind of location
  storage*: TStorageLoc
  flags*: set[LocFlag]       ## location's flags
  lode*: CgNode              ## Node where the location came from; can be faked
  r*: Rope                   ## rope value of location (code generators)
  
  Source   Edit
TLocKind = enum
  locNone,                  ## no location
  locTemp,                  ## temporary location
  locLocalVar,              ## location is a local variable
  locGlobalVar,             ## location is a global variable
  locParam,                 ## location is a parameter
  locExpr,                  ## "location" is really an expression
  locData,                  ## location is a constant
  locCall,                  ## location is a call expression
  locOther                   ## location is something other
  Source   Edit
TStorageLoc = enum
  OnUnknown,                ## location is unknown (stack, heap or static)
  OnStatic,                 ## in a static section
  OnStack,                  ## location is on hardware stack
  OnHeap                     ## location is on heap or global
                             ## (reference counting needed)
  Source   Edit
TTypeSeq = seq[PType]
  Source   Edit
TypeCache = Table[SigHash, Rope]
  Source   Edit
TypeCacheWithOwner = Table[SigHash, tuple[str: Rope, owner: int32]]
  Source   Edit

Procs

proc includeHeader(this: BModule; header: string) {....raises: [], tags: [].}
  Source   Edit
func isFilled(x: ProcLoc): bool {.inline, ...raises: [], tags: [].}
  Source   Edit
func isFilled(x: TLoc): bool {.inline, ...raises: [], tags: [].}
  Source   Edit
proc newModuleList(g: ModuleGraph): BModuleList {.
    ...raises: [KeyError, Exception, ERecoverableError],
    tags: [ReadDirEffect, RootEffect].}
  Source   Edit
proc newProc(prc: PSym; module: BModule): BProc {....raises: [], tags: [].}
  Source   Edit
proc procSec(p: BProc; s: TCProcSection): var Rope {.inline, ...raises: [],
    tags: [].}
  Source   Edit
proc s(p: BProc; s: TCProcSection): var Rope {.inline, ...raises: [], tags: [].}
  Source   Edit

Iterators

iterator cgenModules(g: BModuleList): BModule {....raises: [], tags: [].}
  Source   Edit

Templates

template config(m: BModule): ConfigRef
  Source   Edit
template config(p: BProc): ConfigRef
  Source   Edit
template consts(m: BModule): untyped
  Source   Edit
template env(p: BProc): untyped
  Source   Edit
template fields(m: BModule): untyped
  Source   Edit
template globals(m: BModule): untyped
  Source   Edit
template params(p: BProc): seq[TLoc]
Returns the mutable list with the locs of p's parameters.   Source   Edit
template procs(m: BModule): untyped
  Source   Edit