Path: blob/main/cranelift/codegen/meta/src/lib.rs
1692 views
//! This crate generates Rust sources for use by1//! [`cranelift_codegen`](../cranelift_codegen/index.html).23use cranelift_srcgen::{Formatter, Language, error};4use shared::Definitions;56#[macro_use]7mod cdsl;89pub mod isa;10pub mod isle;1112mod gen_asm;13mod gen_inst;14mod gen_isle;15mod gen_settings;16mod gen_types;1718mod constant_hash;19mod shared;20mod unique_table;2122#[cfg(feature = "pulley")]23mod pulley;2425/// Generate an ISA from an architecture string (e.g. "x86_64").26pub fn isa_from_arch(arch: &str) -> Result<isa::Isa, String> {27isa::Isa::from_arch(arch).ok_or_else(|| format!("no supported isa found for arch `{arch}`"))28}2930/// Generates all the Rust source files used in Cranelift from the meta-language.31pub fn generate_rust(isas: &[isa::Isa], out_dir: &std::path::Path) -> Result<(), error::Error> {32let shared_defs = shared::define();33generate_rust_for_shared_defs(&shared_defs, isas, out_dir)34}3536fn generate_rust_for_shared_defs(37shared_defs: &Definitions,38isas: &[isa::Isa],39out_dir: &std::path::Path,40) -> Result<(), error::Error> {41gen_settings::generate(42&shared_defs.settings,43gen_settings::ParentGroup::None,44"settings.rs",45out_dir,46)?;4748gen_types::generate("types.rs", out_dir)?;4950gen_inst::generate(51&shared_defs.all_formats,52&shared_defs.all_instructions,53"opcodes.rs",54"inst_builder.rs",55out_dir,56)?;5758// Per ISA definitions.59for isa in isa::define(isas) {60gen_settings::generate(61&isa.settings,62gen_settings::ParentGroup::Shared,63&format!("settings-{}.rs", isa.name),64out_dir,65)?;66}6768#[cfg(feature = "pulley")]69if isas.contains(&isa::Isa::Pulley32) || isas.contains(&isa::Isa::Pulley64) {70pulley::generate_rust("pulley_inst_gen.rs", out_dir)?;71}7273Ok(())74}7576/// Generates all the ISLE source files used in Cranelift from the meta-language.77pub fn generate_isle(isle_dir: &std::path::Path) -> Result<(), error::Error> {78let shared_defs = shared::define();79generate_isle_for_shared_defs(&shared_defs, isle_dir)80}8182fn generate_isle_for_shared_defs(83shared_defs: &Definitions,84isle_dir: &std::path::Path,85) -> Result<(), error::Error> {86gen_isle::generate(87&shared_defs.all_formats,88&shared_defs.all_instructions,89"numerics.isle",90"isle_numerics.rs",91"clif_opt.isle",92"clif_lower.isle",93isle_dir,94)?;9596#[cfg(feature = "pulley")]97pulley::generate_isle("pulley_gen.isle", isle_dir)?;9899Ok(())100}101102/// Generate the ISLE definitions; this provides ISLE glue to access the103/// assembler instructions in [cranelift_assembler_x64_meta].104fn generate_isle_for_assembler(105insts: &[cranelift_assembler_x64_meta::dsl::Inst],106isle_dir: &std::path::Path,107) -> Result<(), error::Error> {108let mut fmt = Formatter::new(Language::Isle);109gen_asm::generate_isle(&mut fmt, insts);110fmt.write("assembler.isle", isle_dir)111}112113/// Generate a macro containing builder functions for the assembler's ISLE114/// constructors; this provides Rust implementations backing up the ISLE115/// definitions in [generate_isle_for_assembler].116fn generate_rust_macro_for_assembler(117insts: &[cranelift_assembler_x64_meta::dsl::Inst],118out_dir: &std::path::Path,119) -> Result<(), error::Error> {120let mut fmt = Formatter::new(Language::Rust);121gen_asm::generate_rust_macro(&mut fmt, insts);122fmt.write("assembler-isle-macro.rs", out_dir)123}124125/// Generates all the source files used in Cranelift from the meta-language.126pub fn generate(127isas: &[isa::Isa],128out_dir: &std::path::Path,129isle_dir: &std::path::Path,130) -> Result<(), error::Error> {131let shared_defs = shared::define();132generate_rust_for_shared_defs(&shared_defs, isas, out_dir)?;133generate_isle_for_shared_defs(&shared_defs, isle_dir)?;134135let insts = cranelift_assembler_x64_meta::instructions::list();136generate_isle_for_assembler(&insts, isle_dir)?;137generate_rust_macro_for_assembler(&insts, out_dir)?;138139Ok(())140}141142143