wasmtime_cranelift_shared/
lib.rs

1use cranelift_codegen::binemit;
2use cranelift_codegen::ir;
3use cranelift_codegen::settings;
4use std::collections::BTreeMap;
5use wasmtime_environ::{FlagValue, FuncIndex};
6
7pub mod isa_builder;
8pub mod obj;
9
10/// A record of a relocation to perform.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct Relocation {
13    /// The relocation code.
14    pub reloc: binemit::Reloc,
15    /// Relocation target.
16    pub reloc_target: RelocationTarget,
17    /// The offset where to apply the relocation.
18    pub offset: binemit::CodeOffset,
19    /// The addend to add to the relocation value.
20    pub addend: binemit::Addend,
21}
22
23/// Destination function. Can be either user function or some special one, like `memory.grow`.
24#[derive(Debug, Copy, Clone, PartialEq, Eq)]
25pub enum RelocationTarget {
26    /// The user function index.
27    UserFunc(FuncIndex),
28    /// A compiler-generated libcall.
29    LibCall(ir::LibCall),
30}
31
32/// Converts cranelift_codegen settings to the wasmtime_environ equivalent.
33pub fn clif_flags_to_wasmtime(
34    flags: impl IntoIterator<Item = settings::Value>,
35) -> BTreeMap<String, FlagValue> {
36    flags
37        .into_iter()
38        .map(|val| (val.name.to_string(), to_flag_value(&val)))
39        .collect()
40}
41
42fn to_flag_value(v: &settings::Value) -> FlagValue {
43    match v.kind() {
44        settings::SettingKind::Enum => FlagValue::Enum(v.as_enum().unwrap().into()),
45        settings::SettingKind::Num => FlagValue::Num(v.as_num().unwrap()),
46        settings::SettingKind::Bool => FlagValue::Bool(v.as_bool().unwrap()),
47        settings::SettingKind::Preset => unreachable!(),
48    }
49}