1 //! This crate generates Rust sources for use by
2 //! [`cranelift_codegen`](../cranelift_codegen/index.html).
3 
4 use std::path::Path;
5 #[macro_use]
6 mod cdsl;
7 mod srcgen;
8 
9 pub mod error;
10 pub mod isa;
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, crate_dir: &Path) -> Result<(), error::Error> {
27     // Create all the definitions:
28     // - common definitions.
29     let mut shared_defs = shared::define();
30 
31     gen_settings::generate(
32         &shared_defs.settings,
33         gen_settings::ParentGroup::None,
34         "settings.rs",
35         &out_dir,
36     )?;
37     gen_types::generate("types.rs", &out_dir)?;
38 
39     // - per ISA definitions.
40     let target_isas = isa::define(isas, &mut shared_defs);
41 
42     // At this point, all definitions are done.
43     let all_formats = shared_defs.verify_instruction_formats();
44 
45     // Generate all the code.
46     gen_inst::generate(
47         all_formats,
48         &shared_defs.all_instructions,
49         "opcodes.rs",
50         "inst_builder.rs",
51         "clif.isle",
52         &out_dir,
53         crate_dir,
54     )?;
55 
56     for isa in target_isas {
57         gen_settings::generate(
58             &isa.settings,
59             gen_settings::ParentGroup::Shared,
60             &format!("settings-{}.rs", isa.name),
61             &out_dir,
62         )?;
63     }
64 
65     Ok(())
66 }
67