cranelift_frontend/lib.rs
1//! Cranelift IR builder library.
2//!
3//! Provides a straightforward way to create a Cranelift IR function and fill it with instructions
4//! corresponding to your source program written in another language.
5//!
6//! To get started, create an [`FunctionBuilderContext`](struct.FunctionBuilderContext.html) and
7//! pass it as an argument to a [`FunctionBuilder`](struct.FunctionBuilder.html).
8//!
9//! # Mutable variables and Cranelift IR values
10//!
11//! The most interesting feature of this API is that it provides a single way to deal with all your
12//! variable problems. Indeed, the [`FunctionBuilder`](struct.FunctionBuilder.html) struct has a
13//! type `Variable` that should be an index of your source language variables. Then, through
14//! calling the functions
15//! [`declare_var`](struct.FunctionBuilder.html#method.declare_var),
16//! [`def_var`](struct.FunctionBuilder.html#method.def_var) and
17//! [`use_var`](struct.FunctionBuilder.html#method.use_var), the
18//! [`FunctionBuilder`](struct.FunctionBuilder.html) will create for you all the Cranelift IR
19//! values corresponding to your variables.
20//!
21//! This API has been designed to help you translate your mutable variables into
22//! [`SSA`](https://en.wikipedia.org/wiki/Static_single_assignment_form) form.
23//! [`use_var`](struct.FunctionBuilder.html#method.use_var) will return the Cranelift IR value
24//! that corresponds to your mutable variable at a precise point in the program. However, if you know
25//! beforehand that one of your variables is defined only once, for instance if it is the result
26//! of an intermediate expression in an expression-based language, then you can translate it
27//! directly by the Cranelift IR value returned by the instruction builder. Using the
28//! [`use_var`](struct.FunctionBuilder.html#method.use_var) API for such an immutable variable
29//! would also work but with a slight additional overhead (the SSA algorithm does not know
30//! beforehand if a variable is immutable or not).
31//!
32//! The moral is that you should use these three functions to handle all your mutable variables,
33//! even those that are not present in the source code but artifacts of the translation. It is up
34//! to you to keep a mapping between the mutable variables of your language and their `Variable`
35//! index that is used by Cranelift. Caution: as the `Variable` is used by Cranelift to index an
36//! array containing information about your mutable variables, when you create a new `Variable`
37//! with [`Variable::new(var_index)`] you should make sure that `var_index` is provided by a
38//! counter incremented by 1 each time you encounter a new mutable variable.
39//!
40//! # Example
41//!
42//! Here is a pseudo-program we want to transform into Cranelift IR:
43//!
44//! ```clif
45//! function(x) {
46//! x, y, z : i32
47//! block0:
48//! y = 2;
49//! z = x + y;
50//! jump block1
51//! block1:
52//! z = z + y;
53//! brnz y, block3;
54//! jump block2
55//! block2:
56//! z = z - x;
57//! return y
58//! block3:
59//! y = y - x
60//! jump block1
61//! }
62//! ```
63//!
64//! Here is how you build the corresponding Cranelift IR function using `FunctionBuilderContext`:
65//!
66//! ```rust
67//! extern crate cranelift_codegen;
68//! extern crate cranelift_frontend;
69//!
70//! use cranelift_codegen::entity::EntityRef;
71//! use cranelift_codegen::ir::types::*;
72//! use cranelift_codegen::ir::{AbiParam, ExternalName, Function, InstBuilder, Signature};
73//! use cranelift_codegen::isa::CallConv;
74//! use cranelift_codegen::settings;
75//! use cranelift_codegen::verifier::verify_function;
76//! use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
77//!
78//! fn main() {
79//! let mut sig = Signature::new(CallConv::SystemV);
80//! sig.returns.push(AbiParam::new(I32));
81//! sig.params.push(AbiParam::new(I32));
82//! let mut fn_builder_ctx = FunctionBuilderContext::new();
83//! let mut func = Function::with_name_signature(ExternalName::user(0, 0), sig);
84//! {
85//! let mut builder = FunctionBuilder::new(&mut func, &mut fn_builder_ctx);
86//!
87//! let block0 = builder.create_ebb();
88//! let block1 = builder.create_ebb();
89//! let block2 = builder.create_ebb();
90//! let block3 = builder.create_ebb();
91//! let x = Variable::new(0);
92//! let y = Variable::new(1);
93//! let z = Variable::new(2);
94//! builder.declare_var(x, I32);
95//! builder.declare_var(y, I32);
96//! builder.declare_var(z, I32);
97//! builder.append_ebb_params_for_function_params(block0);
98//!
99//! builder.switch_to_block(block0);
100//! builder.seal_block(block0);
101//! {
102//! let tmp = builder.ebb_params(block0)[0]; // the first function parameter
103//! builder.def_var(x, tmp);
104//! }
105//! {
106//! let tmp = builder.ins().iconst(I32, 2);
107//! builder.def_var(y, tmp);
108//! }
109//! {
110//! let arg1 = builder.use_var(x);
111//! let arg2 = builder.use_var(y);
112//! let tmp = builder.ins().iadd(arg1, arg2);
113//! builder.def_var(z, tmp);
114//! }
115//! builder.ins().jump(block1, &[]);
116//!
117//! builder.switch_to_block(block1);
118//! {
119//! let arg1 = builder.use_var(y);
120//! let arg2 = builder.use_var(z);
121//! let tmp = builder.ins().iadd(arg1, arg2);
122//! builder.def_var(z, tmp);
123//! }
124//! {
125//! let arg = builder.use_var(y);
126//! builder.ins().brnz(arg, block3, &[]);
127//! }
128//! builder.ins().jump(block2, &[]);
129//!
130//! builder.switch_to_block(block2);
131//! builder.seal_block(block2);
132//! {
133//! let arg1 = builder.use_var(z);
134//! let arg2 = builder.use_var(x);
135//! let tmp = builder.ins().isub(arg1, arg2);
136//! builder.def_var(z, tmp);
137//! }
138//! {
139//! let arg = builder.use_var(y);
140//! builder.ins().return_(&[arg]);
141//! }
142//!
143//! builder.switch_to_block(block3);
144//! builder.seal_block(block3);
145//!
146//! {
147//! let arg1 = builder.use_var(y);
148//! let arg2 = builder.use_var(x);
149//! let tmp = builder.ins().isub(arg1, arg2);
150//! builder.def_var(y, tmp);
151//! }
152//! builder.ins().jump(block1, &[]);
153//! builder.seal_block(block1);
154//!
155//! builder.finalize();
156//! }
157//!
158//! let flags = settings::Flags::new(settings::builder());
159//! let res = verify_function(&func, &flags);
160//! println!("{}", func.display(None));
161//! if let Err(errors) = res {
162//! panic!("{}", errors);
163//! }
164//! }
165//! ```
166
167#![deny(missing_docs, trivial_numeric_casts, unused_extern_crates)]
168#![warn(unused_import_braces)]
169#![cfg_attr(feature = "std", deny(unstable_features))]
170#![cfg_attr(feature = "cargo-clippy", allow(clippy::new_without_default))]
171#![cfg_attr(
172 feature = "cargo-clippy",
173 warn(
174 clippy::float_arithmetic,
175 clippy::mut_mut,
176 clippy::nonminimal_bool,
177 clippy::option_map_unwrap_or,
178 clippy::option_map_unwrap_or_else,
179 clippy::print_stdout,
180 clippy::unicode_not_nfc,
181 clippy::use_self
182 )
183)]
184#![no_std]
185
186#[allow(unused_imports)] // #[macro_use] is required for no_std
187#[macro_use]
188extern crate alloc;
189
190#[cfg(feature = "std")]
191#[macro_use]
192extern crate std;
193
194#[cfg(not(feature = "std"))]
195use hashbrown::{hash_map, HashMap};
196#[cfg(feature = "std")]
197use std::collections::{hash_map, HashMap};
198
199pub use crate::frontend::{FunctionBuilder, FunctionBuilderContext};
200pub use crate::switch::Switch;
201pub use crate::variable::Variable;
202
203mod frontend;
204mod ssa;
205mod switch;
206mod variable;
207
208/// Version number of this crate.
209pub const VERSION: &str = env!("CARGO_PKG_VERSION");