compiler/backend/cgir2

    Dark Mode
Search:
  Source   Edit

Implements the (mostly) target-language agnostic IR/IL used as the input for the code generators. Abstraction wise, it's a little higher level than C, but a lot simpler and with less quirks.

Types

AsmMode = enum
  asmMsvc,                  ## MSVC inline assembly syntax
  asmGnu,                   ## GNU inline assembly syntax
  asmJs                      ## inline JavaScript
What an inline asm statement represents.   Source   Edit
Ast = seq[CgNode]
Packed storage for AST.   Source   Edit
CgCallConv {.pure.} = enum
  Default,                  ## decided by the implementation
  Nimcall,                  ## calling convention for NimSkull procedures
  Stdcall,                  ## C stdcall
  Cdecl,                    ## C cdecl
  Safecall,                 ## C safecall
  Syscall,                  ## C syscall
  Fastcall                   ## C fastcall
Calling convention.   Source   Edit
CgLocAttrib {.pure.} = enum
  NoAlias = 0,              ## C-specific attribute; may be ignored
  Register = 1,             ## C-specific attribute; may be ignored
  Volatile = 2               ## C-specific attribute; may be ignored
Attributes for locals, globals, and fields.   Source   Edit
CgModule = object
  tast*: Ast                 ## all AST for type bodies
  ast*: Ast                  ## all AST for everything that's not types
  data*: Store[Datum, NodeIndex] ## ID of constant -> index of construction expression AST in `ast`
                                 ## representing the constant/datum
  types*: Table[StringId, NodeIndex] ## type name -> index of corresponding body in `tast`
  globals*: Table[StringId, NodeIndex] ## name of global -> index of corresponding declaration in `ast`
  procs*: Table[StringId, NodeIndex] ## name of proc -> index of coressponding declaration in `ast`
  infos*: seq[SourceLoc]     ## source location information stored out-of-band
  strings*: BiTable[string]  ## interned string values
  numbers*: BiTable[uint64]  ## out-of-band bit patterns, backing large int, uint, and float values
  
A CGIR module using an in-memory representation meant for easy processing.   Source   Edit
CgNode = object
  kindInfo*: uint32          ## 8-bit kind; 24-bit info index (0 meaning 'none')
  val*: uint32               ## for terminals, meaning depends on the kind
                             ## for non-terminals, the number of child nodes
  
A single packed node in an abstract syntax tree.   Source   Edit
CgNodeKind = enum
  cnkInvalid,               ## the node is uninitialized
  cnkBool,                  ## bool value
  cnkInt,                   ## signless packed integer value
  cnkFloat,                 ## packed float value
  cnkString,                ## packed interned string value
  cnkLabel,                 ## label name (32-bit uint)
  cnkType,                  ## reference to a type (by name)
  cnkDatum,                 ## reference to an anonymous constant (by ID)
  cnkProc,                  ## reference to procedure (by name)
  cnkGlobal,                ## reference to global (by name)
  cnkLocal,                 ## reference to local (by name)
  cnkUnwind, cnkUnknown, cnkNilLit, cnkValue, cnkPath, cnkExtField, cnkNeg,
  cnkAdd, cnkSub, cnkMul, cnkDiv, cnkMod, cnkCheckedAdd, cnkCheckedSub,
  cnkCheckedMul, cnkNot, cnkEq, cnkLe, cnkLt, cnkBitNot, cnkBitAnd, cnkBitOr,
  cnkBitXor, cnkShl, cnkShr, cnkUse, cnkLoad, cnkAddr, cnkBitcast, cnkPtrCast,
  cnkConv, cnkZext, cnkSext, cnkTrunc, cnkPromote, cnkDemote, cnkFToI, cnkFToU,
  cnkUToF, cnkIToF, cnkSizeof, cnkAlignof, cnkOffsetof, cnkUnlikely, cnkEmit,
  cnkAsm, cnkStmtList, cnkBlock, cnkScope, cnkTry, cnkIf, cnkDispatch,
  cnkTarget, cnkWhile, cnkBreak, cnkRaise, cnkReturn, cnkUnreachable, cnkDef,
  cnkAsgn, cnkStore, cnkDrop, cnkCheckedCall, cnkCheckedCallAsgn, cnkTailCall,
  cnkCall, cnkConstr, cnkRecConstr, cnkFieldInit, cnkParams, cnkParam,
  cnkProcImp, cnkProcDef, cnkProcExp, cnkGlobalImp, cnkGlobalDef, cnkGlobalExp,
  cnkVoidTy, cnkBoolTy, cnkCharTy, cnkUIntTy, cnkIntTy, cnkFloatTy, cnkOpaqueTy,
  cnkStructTy, cnkUnionTy, cnkArrayTy, cnkPtrTy, cnkProcTy, cnkField, cnkVarargs
  Source   Edit
CgParamAttrib {.pure.} = enum
  NoAlias                    ## C-specific attribute; may be ignored
Parameter attributes.   Source   Edit
CgProcAttrib {.pure.} = enum
  Inline,                   ## try to inline the body where possible
  NoInline                   ## never inline the body at a callsite
  Source   Edit
CgStorage {.pure.} = enum
  Normal,                   ## mutable global storage
  Thread,                   ## thread-local storage
  Const                      ## constant global storage
  Source   Edit
CgTypeKind = range[cnkVoidTy .. cnkProcTy]
  Source   Edit
Datum = distinct uint32
ID of an anonymous constant.   Source   Edit
NodeIndex = distinct uint32
Index of a node in an Ast.   Source   Edit
SourceLoc = object
  line*: uint16              ## line number; '0' means invalid
  column*: uint16            ## column number. '0' means "full line"
  file*: StringId            ## full path of the source file
  
Source location information.   Source   Edit

Consts

AllNodes = {cnkInvalid..cnkVarargs}
  Source   Edit
cnkBlocks = {cnkBlock, cnkTry, cnkScope}
  Source   Edit
cnkExprs = {cnkNilLit..cnkPath, cnkNeg..cnkOffsetof, cnkCall}
  Source   Edit
cnkStmts = {cnkEmit..cnkStmtList, cnkIf..cnkDispatch, cnkWhile..cnkCall}
  Source   Edit
cnkSyms = {cnkLocal, cnkGlobal, cnkProc, cnkDatum, cnkUnknown}
  Source   Edit

Procs

func `[]`(ast: Ast; n: NodeIndex): CgNode {.inline, ...raises: [], tags: [].}
  Source   Edit
func `[]`(ast: Ast; n: NodeIndex; i: SomeInteger): CgNode {.inline.}
  Source   Edit
func append(dst: var Ast; src: Ast): NodeIndex {....raises: [], tags: [].}
Appends src to dst.   Source   Edit
func append(dst: var Ast; src: Ast; n: NodeIndex): NodeIndex {....raises: [],
    tags: [].}
Appends the sub-tree at n in src to the dst.   Source   Edit
func child(ast: Ast; n: NodeIndex; i: SomeInteger): NodeIndex {.inline.}
Returns the index of the i-th child node of n.   Source   Edit
func get(m: CgModule; s: StringId): lent string {.inline, ...raises: [], tags: [].}
Retrieves a string previously added to the module via put.   Source   Edit
func isBool(m: CgModule; n: CgNode): bool {.inline, ...raises: [], tags: [].}
Whether n is a valid bool terminal.   Source   Edit
func isDatum(m: CgModule; n: CgNode): bool {.inline, ...raises: [], tags: [].}
Whether n is a valid datum terminal (whether the datum actually exists is not considered).   Source   Edit
func isFloat(m: CgModule; n: CgNode): bool {.inline, ...raises: [], tags: [].}
Whether n is a valid float terminal.   Source   Edit
func isGlobal(m: CgModule; n: CgNode): bool {.inline, ...raises: [], tags: [].}
Whether n is a valid global terminal (whether the global actually exists is not considered).   Source   Edit
func isInt(m: CgModule; n: CgNode): bool {.inline, ...raises: [], tags: [].}
Whether n is a valid int terminal.   Source   Edit
func isLocal(m: CgModule; n: CgNode): bool {.inline, ...raises: [], tags: [].}
Whether n is a valid local terminal (whether the local actually exists is not considered).   Source   Edit
func isProc(m: CgModule; n: CgNode): bool {.inline, ...raises: [], tags: [].}
Whether n is a valid proc terminal (whether the proc actually exists is not considered).   Source   Edit
func isString(m: CgModule; n: CgNode): bool {.inline, ...raises: [], tags: [].}
Whether n is a valid string terminal.   Source   Edit
func isType(m: CgModule; n: CgNode): bool {.inline, ...raises: [], tags: [].}
Whether n is a valid type terminal (whether the type actually exists is not considered).   Source   Edit
func last(ast: Ast; n: NodeIndex): NodeIndex {.inline, ...raises: [], tags: [].}
Returns the index of the last child node of n.   Source   Edit
func len(ast: Ast; n: NodeIndex): int {.inline, ...raises: [], tags: [].}
Returns the number of direct child nodes the node at n has.   Source   Edit
func next(ast: Ast; n: NodeIndex): NodeIndex {.inline, ...raises: [], tags: [].}
Returns the index of the following sibling node, or, if there's no sibling, the index of the parent node's sibling.   Source   Edit
func pack(m: var CgModule; f: float64): uint32 {.inline, ...raises: [], tags: [].}
Packs f and returns the packed representation.   Source   Edit
func pack(m: var CgModule; i: int64): uint32 {.inline, ...raises: [], tags: [].}
Packs i and returns the packed representation.   Source   Edit
func put(m: var CgModule; val: sink string): StringId {....raises: [], tags: [].}
  Source   Edit
func span(ast: Ast; n: NodeIndex): int {....raises: [], tags: [].}
Computes the number of nodes the sub-tree at n spans.   Source   Edit
func unpackFloat(m: CgModule; p: uint32): float64 {.inline, ...raises: [], tags: [].}
Retrieves the float64 for the packed float previously added to m via pack.   Source   Edit
func unpackInt(m: CgModule; p: uint32): int64 {.inline, ...raises: [], tags: [].}
Retrieves the signed interpretation for the packed int previously added to m via pack.   Source   Edit
func unpackUInt(m: CgModule; p: uint32): uint64 {.inline, ...raises: [], tags: [].}
Retrieves the unsigned interpretation for the packed int previously added to m via pack.   Source   Edit

Templates

template info(n: CgNode): uint32
  Source   Edit
template isLeaf(n: CgNode): bool
Whether n is a terminal node.   Source   Edit
template kind(n: CgNode): CgNodeKind
  Source   Edit
template node(k: CgNodeKind): CgNode
  Source   Edit
template node(k: CgNodeKind; info, v: uint32): CgNode
  Source   Edit
template node(k: CgNodeKind; v: uint32): CgNode
  Source   Edit

Exports

StringId, LitId, ==, ==, ==, ==, ==, ==, ==, ==, ==, ==, ==, ==, ==