Expand description

Sway-IR is a library providing an intermediate representation for the Sway compiler pipeline.

It is inspired heavily by LLVM although it aims to remain a much simpler system, providing only that which is required by Sway to target the Fuel virtual machine. It takes the form of a static single assignment graph and is designed primarily to allow executable code optimization transforms powerful, yet remain relatively simple.

The core data type is Context which contains all the IR state in an entity-component style system. A Context contains one or more Modules, which in turn contain one or more Functions. Functions have a set of arguments, contain a collection of local variables and one or more Blocks. Blocks contain lists of Instructions and may be joined as a graph to represent program control flow.

Other important data types are Value, Type and Constant. Function arguments, local variables, instructions and constants are all Values.

The optimization passes are found in the optimize module.

Note:

Most of the public data types used in this library are in fact wrappers around a handle into the context. The context uses the generational_arena crate to maintain an entity component system, or ECS.

The nature of SSA is that it represents a graph of modules, functions, basic blocks and instructions, which in Rust could be represented using references, Boxes or std::rc::Rc pointers. But the nature of optimization passes are to transform these graphs into generally smaller or at least somehow more efficient versions of themselves, and so to avoid a lot of copying and the interior mutability problem Sway-IR uses the ECS. Each handle implements Copy and so is cheap to pass around by value, making changes to the ECS context simpler in terms of satisfying the Rust borrow checker.

Re-exports

pub use asm::*;
pub use block::*;
pub use constant::*;
pub use context::*;
pub use error::*;
pub use function::*;
pub use instruction::*;
pub use irtype::*;
pub use metadata::*;
pub use module::*;
pub use optimize::*;
pub use parser::*;
pub use pointer::*;
pub use printer::*;
pub use value::*;
pub use verify::*;

Modules

An ‘asm’ block represents an opaque set of Fuel VM assembly instructions, embedded in place and intended to be inserted as is into the assembly code generation.

Represents a ‘basic block’ of Instructions in a control flow graph.

Constant is a typed constant value.

The main handle to an IR instance.

A typical function data type.

Instructions for data manipulation, but mostly control flow.

Each of the valid Value types.

A scope containing a collection of Functions and constant values.

A collection of optimization passes.

A parser for the printed IR, useful mostly for testing.

A value representing a memory location, generally to a function local value.

Print (or serialize) IR to human and machine readable text.

The base descriptor for various values within the IR.

Code to validate the IR in a Context.