Skip to main content

wasmer_compiler_cranelift/
lib.rs

1//! A WebAssembly `Compiler` implementation using Cranelift.
2//!
3//! Cranelift is a fast IR generator created by Mozilla for usage in
4//! Firefox as a next JS compiler generator.
5//!
6//! Compared to LLVM, Cranelift is a bit faster and made entirely in Rust.
7#![deny(missing_docs, trivial_numeric_casts, unused_extern_crates)]
8#![warn(unused_import_braces)]
9#![allow(clippy::new_without_default)]
10#![warn(
11    clippy::float_arithmetic,
12    clippy::mut_mut,
13    clippy::nonminimal_bool,
14    clippy::map_unwrap_or,
15    clippy::print_stdout,
16    clippy::unicode_not_nfc,
17    clippy::use_self
18)]
19#![cfg_attr(docsrs, feature(doc_cfg))]
20
21#[cfg(target_os = "windows")]
22compile_error!(
23    "The Cranelift compiler backend is not supported on Windows. Use the V8 backend instead."
24);
25
26#[cfg(not(feature = "std"))]
27#[macro_use]
28extern crate alloc as std;
29#[cfg(feature = "std")]
30#[macro_use]
31extern crate std;
32
33#[cfg(not(feature = "std"))]
34use hashbrown::{
35    HashMap, hash_map,
36    hash_map::Entry::{Occupied, Vacant},
37};
38#[cfg(feature = "std")]
39use std::collections::{
40    HashMap, hash_map,
41    hash_map::Entry::{Occupied, Vacant},
42};
43
44mod abi;
45mod address_map;
46mod compiler;
47mod config;
48mod debug;
49#[cfg(feature = "unwind")]
50mod dwarf;
51#[cfg(feature = "unwind")]
52mod eh;
53mod elf;
54mod func_environ;
55mod heap;
56mod table;
57mod trampoline;
58mod translator;
59
60use cranelift_codegen::ir::TrapCode;
61
62pub use crate::compiler::CraneliftCompiler;
63pub use crate::config::{Cranelift, CraneliftCallbacks, CraneliftOptLevel};
64pub use crate::debug::{ModuleInfoMemoryOffset, ModuleInfoVmctxInfo, ValueLabelsRanges};
65pub use crate::trampoline::make_trampoline_function_call;
66
67/// Version number of this crate.
68pub const VERSION: &str = env!("CARGO_PKG_VERSION");
69
70/// Offset applied to user-defined trap codes to avoid colliding with
71/// Cranelift-reserved values.
72const TRAP_USER_OFFSET: u8 = 32;
73
74/// Trap reported when an indirect call targets a null function reference.
75#[allow(clippy::identity_op, reason = "for clarity")]
76pub const TRAP_INDIRECT_CALL_TO_NULL: TrapCode = TrapCode::unwrap_user(TRAP_USER_OFFSET + 0);
77/// Trap reported when an indirect call signature does not match.
78pub const TRAP_BAD_SIGNATURE: TrapCode = TrapCode::unwrap_user(TRAP_USER_OFFSET + 1);
79/// Trap reported when a table access goes out of bounds.
80pub const TRAP_TABLE_OUT_OF_BOUNDS: TrapCode = TrapCode::unwrap_user(TRAP_USER_OFFSET + 2);
81/// Trap reported when a heap access violates alignment guarantees.
82pub const TRAP_HEAP_MISALIGNED: TrapCode = TrapCode::unwrap_user(TRAP_USER_OFFSET + 3);
83/// Trap reported when unreachable code is executed.
84pub const TRAP_UNREACHABLE: TrapCode = TrapCode::unwrap_user(TRAP_USER_OFFSET + 4);
85/// Trap reported when a null reference is observed.
86pub const TRAP_NULL_REFERENCE: TrapCode = TrapCode::unwrap_user(TRAP_USER_OFFSET + 5);
87/// Trap reported when a null i31 reference is observed.
88pub const TRAP_NULL_I31_REF: TrapCode = TrapCode::unwrap_user(TRAP_USER_OFFSET + 6);
89/// Trap reported for interrupts (not currently supported).
90pub const TRAP_INTERRUPT: TrapCode = TrapCode::unwrap_user(TRAP_USER_OFFSET + 7);