Skip to main content

cranelift_codegen_meta/
lib.rs

1//! This crate generates Rust sources for use by
2//! [`cranelift_codegen`](../cranelift_codegen/index.html).
3
4use cranelift_srcgen::{Formatter, Language, error};
5use shared::Definitions;
6
7#[macro_use]
8mod cdsl;
9
10pub mod isa;
11pub mod isle;
12
13mod gen_asm;
14mod gen_inst;
15mod gen_isle;
16mod gen_settings;
17mod gen_types;
18
19mod constant_hash;
20mod shared;
21mod unique_table;
22
23#[cfg(feature = "pulley")]
24mod pulley;
25
26/// Generate an ISA from an architecture string (e.g. "x86_64").
27pub fn isa_from_arch(arch: &str) -> Result<isa::Isa, String> {
28    isa::Isa::from_arch(arch).ok_or_else(|| format!("no supported isa found for arch `{arch}`"))
29}
30
31/// Generates all the Rust source files used in Cranelift from the meta-language.
32pub fn generate_rust(isas: &[isa::Isa], out_dir: &std::path::Path) -> Result<(), error::Error> {
33    let shared_defs = shared::define();
34    generate_rust_for_shared_defs(&shared_defs, isas, out_dir)
35}
36
37fn generate_rust_for_shared_defs(
38    shared_defs: &Definitions,
39    isas: &[isa::Isa],
40    out_dir: &std::path::Path,
41) -> Result<(), error::Error> {
42    gen_settings::generate(
43        &shared_defs.settings,
44        gen_settings::ParentGroup::None,
45        "settings.rs",
46        out_dir,
47    )?;
48
49    gen_types::generate("types.rs", out_dir)?;
50
51    gen_inst::generate(
52        &shared_defs.all_formats,
53        &shared_defs.all_instructions,
54        "opcodes.rs",
55        "inst_builder.rs",
56        out_dir,
57    )?;
58
59    // Per ISA definitions.
60    for isa in isa::define(isas) {
61        gen_settings::generate(
62            &isa.settings,
63            gen_settings::ParentGroup::Shared,
64            &format!("settings-{}.rs", isa.name),
65            out_dir,
66        )?;
67    }
68
69    #[cfg(feature = "pulley")]
70    if isas.contains(&isa::Isa::Pulley32) || isas.contains(&isa::Isa::Pulley64) {
71        pulley::generate_rust("pulley_inst_gen.rs", out_dir)?;
72    }
73
74    Ok(())
75}
76
77/// Generates all the ISLE source files used in Cranelift from the meta-language.
78pub fn generate_isle(isle_dir: &std::path::Path) -> Result<(), error::Error> {
79    let shared_defs = shared::define();
80    generate_isle_for_shared_defs(&shared_defs, isle_dir)?;
81    let insts = cranelift_assembler_x64_meta::instructions::list();
82    generate_isle_for_assembler(&insts, isle_dir)?;
83
84    Ok(())
85}
86
87fn generate_isle_for_shared_defs(
88    shared_defs: &Definitions,
89    isle_dir: &std::path::Path,
90) -> Result<(), error::Error> {
91    gen_isle::generate(
92        &shared_defs.all_formats,
93        &shared_defs.all_instructions,
94        "numerics.isle",
95        "isle_numerics.rs",
96        "clif_opt.isle",
97        "clif_lower.isle",
98        isle_dir,
99    )?;
100
101    #[cfg(feature = "pulley")]
102    pulley::generate_isle("pulley_gen.isle", isle_dir)?;
103
104    Ok(())
105}
106
107/// Generate the ISLE definitions; this provides ISLE glue to access the
108/// assembler instructions in [cranelift_assembler_x64_meta].
109fn generate_isle_for_assembler(
110    insts: &[cranelift_assembler_x64_meta::dsl::Inst],
111    isle_dir: &std::path::Path,
112) -> Result<(), error::Error> {
113    let mut fmt = Formatter::new(Language::Isle);
114    gen_asm::generate_isle(&mut fmt, insts);
115    fmt.write("assembler.isle", isle_dir)
116}
117
118/// Generate a macro containing builder functions for the assembler's ISLE
119/// constructors; this provides Rust implementations backing up the ISLE
120/// definitions in [generate_isle_for_assembler].
121fn generate_rust_macro_for_assembler(
122    insts: &[cranelift_assembler_x64_meta::dsl::Inst],
123    out_dir: &std::path::Path,
124) -> Result<(), error::Error> {
125    let mut fmt = Formatter::new(Language::Rust);
126    gen_asm::generate_rust_macro(&mut fmt, insts);
127    fmt.write("assembler-isle-macro.rs", out_dir)
128}
129
130/// Generates all the source files used in Cranelift from the meta-language.
131pub fn generate(
132    isas: &[isa::Isa],
133    out_dir: &std::path::Path,
134    isle_dir: &std::path::Path,
135) -> Result<(), error::Error> {
136    let shared_defs = shared::define();
137    generate_rust_for_shared_defs(&shared_defs, isas, out_dir)?;
138    generate_isle_for_shared_defs(&shared_defs, isle_dir)?;
139
140    let insts = cranelift_assembler_x64_meta::instructions::list();
141    generate_isle_for_assembler(&insts, isle_dir)?;
142    generate_rust_macro_for_assembler(&insts, out_dir)?;
143
144    Ok(())
145}