[][src]Module c2rust_transpile::cfg

Control Flow Graph analysis

Through switch/case/default and labels/goto, the C language supports jumping directly from one position in the code to another. Rust supports on structured control flow constructs. This means that during translation, we need to somehow eliminate the unstructured control-flow constructs C has. This module is where that happens.

In a nutshell, here are the steps:

  • given an entry point C statement, translate it into a CFG consisting of BasicBlock<Label>
  • simplify this CFG (by eliminating empty blocks that jump unconditionally to the next block)
  • use the Relooper algorithm to convert this CFG into a sequence of Structure<StmtOrDecl>s
  • place the declarations in the right place and produce a sequence of Structure<Stmt>s
  • simplify that sequence of Structure<Stmt>s into another such sequence
  • convert the Vec<Structure<Stmt>> back into a Vec<Stmt>

Modules

loops

This module contains stuff related to choosing better loops in Relooper. The problem is simple: there is a point in the relooper algorithm where we commit to making a Loop structure. At that point, we partition blocks into those that go into the loop and those that go after the loop.

multiples

This module contains stuff related to preserving and using information from the initial C source to better decide when to produce Multiple structures instead of Loop structures. By default, relooper always makes loops. This sometimes leads to some pretty ugly (but correct) translations.

relooper

This modules handles converting a a control-flow graph Cfg into Vec<Structure>, optionally simplifying the latter.

structures

This modules handles converting Vec<Structure> into Vec<Stmt>.

Structs

BasicBlock

Generalized basic block.

Cfg

A CFG graph of regular basic blocks.

DeclStmtInfo

This contains the information one needs to convert a C declaration in all the possible ways:

DeclStmtStore

Stores information about translating C declarations to Rust statements. When seeing a C declaration, we often don't know if it is already in the right place. The fix is to punt: we put into a DeclStmtStore information about what to do in all possible cases and we delay choosing what to do until later.

SwitchCases

The sole purpose of this structure is to accumulate information about what cases/default have been seen which translating the body of the switch.

Enums

GenTerminator

Represents the control flow choices one can make when at the end of a BasicBlock.

ImplicitReturnType

Reaching the end of a body without encountering a return means different things depending on the function we are in.

Label

These labels identify basic blocks in a regular CFG.

StmtOrComment

A Rust statement, or a comment

StmtOrDecl

A Rust statement, or a C declaration, or a comment

Structure

These are the things that the relooper algorithm produces.

StructureLabel

These labels identify structure basic blocks in a structure CFG.