1 //! This crate generates Rust sources for use by
2 //! [`cranelift_codegen`](../cranelift_codegen/index.html).
3 #[macro_use]
4 mod cdsl;
5 mod srcgen;
6 
7 pub mod error;
8 pub mod isa;
9 
10 mod gen_inst;
11 mod gen_settings;
12 mod gen_types;
13 
14 mod default_map;
15 mod shared;
16 mod unique_table;
17 
18 /// Generate an ISA from an architecture string (e.g. "x86_64").
19 pub fn isa_from_arch(arch: &str) -> Result<isa::Isa, String> {
20     isa::Isa::from_arch(arch).ok_or_else(|| format!("no supported isa found for arch `{}`", arch))
21 }
22 
23 /// Generates all the Rust source files used in Cranelift from the meta-language.
24 pub fn generate(isas: &[isa::Isa], out_dir: &str) -> Result<(), error::Error> {
25     // Create all the definitions:
26     // - common definitions.
27     let mut shared_defs = shared::define();
28 
29     gen_settings::generate(
30         &shared_defs.settings,
31         gen_settings::ParentGroup::None,
32         "settings.rs",
33         &out_dir,
34     )?;
35     gen_types::generate("types.rs", &out_dir)?;
36 
37     // - per ISA definitions.
38     let target_isas = isa::define(isas, &mut shared_defs);
39 
40     // At this point, all definitions are done.
41     let all_formats = shared_defs.verify_instruction_formats();
42 
43     // Generate all the code.
44     gen_inst::generate(
45         all_formats,
46         &shared_defs.all_instructions,
47         "opcodes.rs",
48         "inst_builder.rs",
49         &out_dir,
50     )?;
51 
52     for isa in target_isas {
53         gen_settings::generate(
54             &isa.settings,
55             gen_settings::ParentGroup::Shared,
56             &format!("settings-{}.rs", isa.name),
57             &out_dir,
58         )?;
59     }
60 
61     Ok(())
62 }
63