1 //! This crate generates Rust sources for use by 2 //! [`cranelift_codegen`](../cranelift_codegen/index.html). 3 4 use shared::Definitions; 5 6 #[macro_use] 7 mod cdsl; 8 mod srcgen; 9 10 pub mod error; 11 pub mod isa; 12 pub mod isle; 13 14 mod gen_inst; 15 mod gen_isle; 16 mod gen_settings; 17 mod gen_types; 18 19 mod constant_hash; 20 mod shared; 21 mod unique_table; 22 23 /// Generate an ISA from an architecture string (e.g. "x86_64"). 24 pub fn isa_from_arch(arch: &str) -> Result<isa::Isa, String> { 25 isa::Isa::from_arch(arch).ok_or_else(|| format!("no supported isa found for arch `{}`", arch)) 26 } 27 28 /// Generates all the Rust source files used in Cranelift from the meta-language. 29 pub fn generate_rust(isas: &[isa::Isa], out_dir: &std::path::Path) -> Result<(), error::Error> { 30 let shared_defs = shared::define(); 31 generate_rust_for_shared_defs(&shared_defs, isas, out_dir) 32 } 33 34 fn generate_rust_for_shared_defs( 35 shared_defs: &Definitions, 36 isas: &[isa::Isa], 37 out_dir: &std::path::Path, 38 ) -> Result<(), error::Error> { 39 gen_settings::generate( 40 &shared_defs.settings, 41 gen_settings::ParentGroup::None, 42 "settings.rs", 43 out_dir, 44 )?; 45 46 gen_types::generate("types.rs", out_dir)?; 47 48 gen_inst::generate( 49 &shared_defs.all_formats, 50 &shared_defs.all_instructions, 51 "opcodes.rs", 52 "inst_builder.rs", 53 out_dir, 54 )?; 55 56 // Per ISA definitions. 57 for isa in isa::define(isas) { 58 gen_settings::generate( 59 &isa.settings, 60 gen_settings::ParentGroup::Shared, 61 &format!("settings-{}.rs", isa.name), 62 out_dir, 63 )?; 64 } 65 66 Ok(()) 67 } 68 69 /// Generates all the ISLE source files used in Cranelift from the meta-language. 70 pub fn generate_isle(isle_dir: &std::path::Path) -> Result<(), error::Error> { 71 let shared_defs = shared::define(); 72 generate_isle_for_shared_defs(&shared_defs, isle_dir) 73 } 74 75 fn generate_isle_for_shared_defs( 76 shared_defs: &Definitions, 77 isle_dir: &std::path::Path, 78 ) -> Result<(), error::Error> { 79 gen_isle::generate( 80 &shared_defs.all_formats, 81 &shared_defs.all_instructions, 82 "clif_opt.isle", 83 "clif_lower.isle", 84 isle_dir, 85 ) 86 } 87 88 /// Generates all the source files used in Cranelift from the meta-language. 89 pub fn generate( 90 isas: &[isa::Isa], 91 out_dir: &std::path::Path, 92 isle_dir: &std::path::Path, 93 ) -> Result<(), error::Error> { 94 let shared_defs = shared::define(); 95 generate_rust_for_shared_defs(&shared_defs, isas, out_dir)?; 96 generate_isle_for_shared_defs(&shared_defs, isle_dir)?; 97 Ok(()) 98 } 99