W3cubDocs

/Nim

dfa

Source Edit

Data flow analysis for Nim. We transform the AST into a linear list of instructions first to make this easier to handle: There are only 3 different branching instructions: 'goto X' is an unconditional goto, 'fork X' is a conditional goto (either the next instruction or 'X' can be taken), 'loop X' is the only jump that jumps back.

Exhaustive case statements are translated so that the last branch is transformed into an 'else' branch. return and break are all covered by 'goto'.

The data structures and algorithms used here are inspired by "A Graph–Free Approach to Data–Flow Analysis" by Markus Mohnen. https://link.springer.com/content/pdf/10.1007/3-540-45937-5_6.pdf

Imports

ast, lineinfos, renderer, aliasanalysis

Types

ControlFlowGraph = seq[Instr]
Source Edit
Instr = object
  case kind*: InstrKind
  of goto, fork, loop:
    dest*: int
  of def, use:
    n*: PNode
Source Edit
InstrKind = enum
  goto, loop, fork, def, use
Source Edit

Procs

proc constructCfg(s: PSym; body: PNode; root: PSym): ControlFlowGraph {.
    ...raises: [Exception], tags: [RootEffect], forbids: [].}
constructs a control flow graph for body. Source Edit
proc echoCfg(c: ControlFlowGraph; start = 0; last = -1) {....deprecated,
    raises: [Exception, KeyError], tags: [RootEffect], forbids: [].}
Deprecated
echos the ControlFlowGraph for debugging purposes. Source Edit

© 2006–2024 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/compiler/dfa.html