Implements the translation of CGIR to a code listing for an abstract machine that focuses on control-flow and exception handling.
This code listing is intended for consumption by the C code generator.
Types
CInstr = object case op*: COpcode of opJump, opErrJump, opLabel, opDispJump: label*: CLabel of opSetTarget, opDispatcher: discr*: uint32 ## ID of the discriminator variable value*: int ## either the value, or number of dispatcher branches of opStmts: stmts*: Slice[int] of opStmt: stmt*: int specifier*: CLabelSpecifier of opBackup, opRestore, opAbort: local*: uint32 ## ID of the backup variable of opPopHandler: nil
- Source Edit
CLabelSpecifier = uint32
- used for identifying the extra labels attached to finally sections Source Edit
COpcode = enum opJump, ## unconditional jump opErrJump, ## jump if in error mode opDispJump, ## jump part of a dispatcher opLabel, ## jump target opSetTarget, ## set the value of a dispatcher's discriminator opDispatcher, ## start of a dispatcher opBackup, ## backup the error state in a local variable and clear it opRestore, ## restore the error from a local variable opStmts, ## slice of statements opStmt, ## control-flow relevant single statement. A label ## specifier is passed along opAbort, opPopHandler
- Source Edit
FinallyInfo = object routes: seq[PathIndex] numExits: int ## number of exits the finally has. Pre-computed for efficiency numErr: int ## number of exceptional jump paths going through this finalizer numNormal: int ## number of non-exception jump paths going through this finalizer discriminator: uint32 ## ID of the discriminator variable to use for the dispatcher errBackupId: uint32 ## only valid if the finally is entered by exceptional control-flow
- Source Edit
Consts
ResumeLabel = 0'u32
- The C label that a cnkResume targets. Source Edit
Procs
func toBlockId(id: CLabelId): BlockId {....raises: [], tags: [].}
- If id was converted to from a valid CGIR label, converts it back to the CGIR label. Source Edit
func toCLabel(n: CgNode): CLabelId {....raises: [], tags: [].}
- Returns the ID of the C label the label-like node n represents. Source Edit
func toCLabel(n: CgNode; specifier: Option[CLabelSpecifier]): CLabel {.inline, ...raises: [], tags: [].}
- Source Edit
proc toInstrList(stmts: CgNode; isFull: bool): seq[CInstr] {....raises: [KeyError], tags: [].}
- Turns the statements list stmts into an instruction list for the abstract machine. isFull signals whether the end of the statement list can be considered the end of the procedure, which allows for the merging of some control-flow paths. Source Edit