koopa/back/mod.rs
1//! The backend of the in-memory form Koopa IR.
2//!
3//! This modules provides generators for generating in-memory form
4//! Koopa IR to other forms, including:
5//!
6//! * The Koopa IR generator ([`Generator`]), name manager ([`NameManager`])
7//! and the Koopa IR visitor trait ([`Visitor`]).
8//! * The text form Koopa IR generator ([`KoopaGenerator`]).
9//! * The LLVM IR generator ([`LlvmGenerator`]).
10//!
11//! # Examples
12//!
13//! Convert the in-memory form Koopa IR program into the text form:
14//!
15//! ```
16//! use koopa::ir::{*, builder_traits::*};
17//! use koopa::back::KoopaGenerator;
18//!
19//! let mut program = Program::new();
20//! let main = program.new_func_def("@main".into(), Vec::new(), Type::get_i32());
21//! let main_data = program.func_mut(main);
22//!
23//! let bb = main_data.dfg_mut().new_bb().basic_block(None);
24//! main_data.layout_mut().bbs_mut().push_key_back(bb);
25//!
26//! let lhs = main_data.dfg_mut().new_value().integer(11);
27//! let rhs = main_data.dfg_mut().new_value().integer(31);
28//! let add = main_data.dfg_mut().new_value().binary(BinaryOp::Add, lhs, rhs);
29//! let ret = main_data.dfg_mut().new_value().ret(Some(add));
30//! main_data.layout_mut().bb_mut(bb).insts_mut().extend([add, ret]);
31//!
32//! // convert to text form
33//! let mut g = KoopaGenerator::new(Vec::new());
34//! g.generate_on(&program).unwrap();
35//! let text_form_ir = std::str::from_utf8(&g.writer()).unwrap().to_string();
36//! println!("{}", text_form_ir);
37//! ```
38//!
39//! Convert the in-memory form Koopa IR program into the text-form LLVM IR,
40//! and save the result to a file:
41//!
42//! ```no_run
43//! use koopa::back::LlvmGenerator;
44//!
45//! # fn main() -> std::io::Result<()> {
46//! # let program = koopa::ir::Program::new();
47//! let mut g = LlvmGenerator::from_path("/path/to/the/output/file")?;
48//! g.generate_on(&program).unwrap();
49//! # Ok(())
50//! # }
51//! ```
52
53pub mod generator;
54pub mod koopa;
55pub mod llvm;
56
57pub use generator::{Generator, NameManager, Prefix, Visitor};
58
59/// Generator for generating Koopa IR structures into text formatted Koopa IR.
60pub type KoopaGenerator<W> = Generator<W, koopa::Visitor>;
61
62/// Generator for generating Koopa IR into LLVM IR.
63pub type LlvmGenerator<W> = Generator<W, llvm::Visitor>;