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