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 11 mod gen_inst; 12 mod gen_settings; 13 mod gen_types; 14 15 mod constant_hash; 16 mod shared; 17 mod unique_table; 18 19 /// Generate an ISA from an architecture string (e.g. "x86_64"). 20 pub fn isa_from_arch(arch: &str) -> Result<isa::Isa, String> { 21 isa::Isa::from_arch(arch).ok_or_else(|| format!("no supported isa found for arch `{}`", arch)) 22 } 23 24 /// Generates all the Rust source files used in Cranelift from the meta-language. 25 pub fn generate(isas: &[isa::Isa], out_dir: &str, isle_dir: &str) -> Result<(), error::Error> { 26 // Create all the definitions: 27 // - common definitions. 28 let mut 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 // - per ISA definitions. 39 let target_isas = isa::define(isas, &mut shared_defs); 40 41 // At this point, all definitions are done. 42 let all_formats = shared_defs.verify_instruction_formats(); 43 44 // Generate all the code. 45 gen_inst::generate( 46 all_formats, 47 &shared_defs.all_instructions, 48 "opcodes.rs", 49 "inst_builder.rs", 50 "clif.isle", 51 &out_dir, 52 isle_dir, 53 )?; 54 55 for isa in target_isas { 56 gen_settings::generate( 57 &isa.settings, 58 gen_settings::ParentGroup::Shared, 59 &format!("settings-{}.rs", isa.name), 60 &out_dir, 61 )?; 62 } 63 64 Ok(()) 65 } 66