compiler/backend/jsgen

  Source   Edit

This is the JavaScript code generator.Type info generation for the JS backend.

Types

BModule = ref TJSGen
  Source   Edit
PGlobals = ref object
  typeInfo, constants*, code*: Rope
  typeInfoGenerated: IntSet
  dataGenerated: IntSet
  unique: int
  requestArrayConstr: bool   ## used for deferring registration of the ``arrayConstr`` proc
  names: Table[int, string]  ## maps a symbol IDs to the symbol's JavaScript name
  procs: SeqMap[ProcedureId, string] ## the JavaScript name for each procedure
  consts: SeqMap[ConstId, Loc]
  globals: SeqMap[GlobalId, Loc]
  env*: MirEnv               ## the project-wide MIR environment
  
  Source   Edit
PProc = ref TProc
  Source   Edit
TProc = object
  prc: PSym
  fullBody*: Body            ## the procedure's full body
  defs, body: Rope
  options: TOptions
  module: BModule
  g: PGlobals
  lastErrorBackupNeeded: bool ## signals whether the value of ``lastJSError`` needs to be captured
                              ## on procedure entry
  unique: int
  blocks: seq[BlockInfo] ## enclosing exception handlers, finallys, and labeled blocks. Used
                         ## for correcting wrong control-flow paths and tracking where the
                         ## current exception needs to be restored
  numHandlers: int ## number of enclosing 'catch' clauses. The name of the
                   ## exception local is derived from this counter
  extraIndent: int
  locals: OrdinalSeq[LocalId, Loc] ## stores all relevant code generator state for the procedure's
                                   ## locals
  addrTaken: PackedSet[LocalId] ## locals that have their address taken at some point
  
  Source   Edit

Consts

NonMagics = {mAbsI, mDotDot, mParseBiggestFloat, mExit}
magics that are treated like normal procedures by the code generator   Source   Edit
sfModuleInit = sfMainModule
the procedure is the 'init' procedure of a module   Source   Edit

Procs

proc defineGlobal(globals: PGlobals; m: BModule; id: GlobalId) {.
    ...raises: [KeyError, Exception, ERecoverableError],
    tags: [ReadDirEffect, RootEffect].}
Emits the definition for the single global v into the top-level section, with m being the module the global belongs to. Also sets up the symbol's JavaScript name.   Source   Edit
proc finishProc(p: PProc): string {....raises: [KeyError, Exception,
    ERecoverableError], tags: [ReadDirEffect, RootEffect].}
  Source   Edit
proc genConstant(g: PGlobals; m: BModule; id: ConstId) {.
    ...raises: [KeyError, Exception, ERecoverableError],
    tags: [ReadDirEffect, RootEffect].}
  Source   Edit
proc genHeader(): Rope {....raises: [ValueError], tags: [].}
  Source   Edit
proc genPartial(p: PProc; n: CgNode) {....raises: [KeyError, Exception,
    ERecoverableError], tags: [ReadDirEffect, RootEffect].}
Generates the JavaScript code for n and appends the result to p. This is intended for CG IR that wasn't already available when calling startProc.   Source   Edit
proc genProc(g: PGlobals; module: BModule; id: ProcedureId; body: sink Body): Rope {.
    ...raises: [KeyError, Exception, ERecoverableError],
    tags: [ReadDirEffect, RootEffect].}
  Source   Edit
proc genTopLevelStmt(globals: PGlobals; m: BModule; body: sink Body) {.
    ...raises: [Exception, ERecoverableError, KeyError],
    tags: [RootEffect, ReadDirEffect].}
  Source   Edit
proc newGlobals(g: ModuleGraph): PGlobals {.
    ...raises: [KeyError, Exception, ERecoverableError],
    tags: [ReadDirEffect, RootEffect].}
  Source   Edit
proc newModule(g: ModuleGraph; module: PSym): BModule {....raises: [], tags: [].}
  Source   Edit
proc startProc(g: PGlobals; module: BModule; id: ProcedureId; body: sink Body): PProc {.
    ...raises: [], tags: [].}
  Source   Edit
proc wholeCode(globals: PGlobals): Rope {....raises: [], tags: [].}
  Source   Edit

Templates

template config(p: PProc): ConfigRef
  Source   Edit
template env(p: PProc): untyped
  Source   Edit