compiler/backend/cgen

  Source   Edit

Implements the C code generator. The code generator takes CGIR modules as input and produces C translation units, nothing else.

Only a subset of the CGIR is supported:

  • no support for exception handling
  • limited support for array types; array types always have to be named

Types

Emit = object
  includes*: seq[NodeIndex]
  types*: seq[NodeIndex]
  globals*: seq[NodeIndex]
  procs*: seq[NodeIndex]
Top-level emit and asm statements to add to the C module.   Source   Edit
ModuleDesc = object
  headers: seq[StringId]
  dataFwd: seq[Datum]
  data: seq[Datum]
  tdecls: seq[StringId]
  tdefs: seq[StringId]
  gdecls: seq[StringId]
  gdefs: seq[StringId]
  fdecls: seq[tuple[inlined: bool, name: StringId]]
  fdefs: seq[tuple[inlined: bool, name: StringId]]
  emit: Emit
Describes the shape of a C module, i.e., what entities need to be declared and defined and in what order.   Source   Edit

Procs

proc initModuleDesc(m: CgModule; procs, globals: seq[StringId]; emit: sink Emit): ModuleDesc {.
    ...raises: [KeyError, Exception], tags: [RootEffect].}
Creates a module description containing all functions and globals given by procs and globals, plus their dependencies.   Source   Edit
proc moduleToC(m: CgModule; desc: ModuleDesc; preamble: string;
               withLineDir: bool): string {....raises: [KeyError, Exception],
    tags: [RootEffect].}
Generates the code for a full C translation unit for m and desc. preamble is text that's placed at the start of the unit. withLineDir controls whether C line directives are enabled.   Source   Edit