2019-01-18 20:13:01 +00:00
|
|
|
use crate::{
|
|
|
|
error::CompileResult,
|
|
|
|
module::ModuleInner,
|
2019-06-12 14:02:15 +00:00
|
|
|
state::ModuleStateMap,
|
2019-04-09 22:53:01 +00:00
|
|
|
typed_func::Wasm,
|
2019-04-12 01:01:54 +00:00
|
|
|
types::{LocalFuncIndex, SigIndex},
|
2019-01-18 20:13:01 +00:00
|
|
|
vm,
|
|
|
|
};
|
2019-02-19 23:36:22 +00:00
|
|
|
|
2019-02-07 00:26:45 +00:00
|
|
|
use crate::{
|
2019-02-22 01:06:49 +00:00
|
|
|
cache::{Artifact, Error as CacheError},
|
2019-07-03 17:45:06 +00:00
|
|
|
codegen::BreakpointMap,
|
2019-02-07 00:26:45 +00:00
|
|
|
module::ModuleInfo,
|
|
|
|
sys::Memory,
|
|
|
|
};
|
2019-03-04 20:57:26 +00:00
|
|
|
use std::{any::Any, ptr::NonNull};
|
2019-01-08 17:09:47 +00:00
|
|
|
|
2019-03-27 21:01:27 +00:00
|
|
|
use hashbrown::HashMap;
|
|
|
|
|
2019-01-21 19:51:41 +00:00
|
|
|
pub mod sys {
|
|
|
|
pub use crate::sys::*;
|
|
|
|
}
|
2019-01-11 03:59:57 +00:00
|
|
|
pub use crate::sig_registry::SigRegistry;
|
2019-01-09 23:31:11 +00:00
|
|
|
|
2019-02-21 00:41:41 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
|
2019-02-07 00:26:45 +00:00
|
|
|
pub enum Backend {
|
|
|
|
Cranelift,
|
2019-04-11 19:44:03 +00:00
|
|
|
Singlepass,
|
2019-02-08 22:19:58 +00:00
|
|
|
LLVM,
|
2019-02-07 00:26:45 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 17:50:28 +00:00
|
|
|
impl Backend {
|
|
|
|
pub fn variants() -> &'static [&'static str] {
|
|
|
|
&[
|
|
|
|
"cranelift",
|
|
|
|
#[cfg(feature = "backend:singlepass")]
|
|
|
|
"singlepass",
|
|
|
|
#[cfg(feature = "backend:llvm")]
|
|
|
|
"llvm",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// stable string representation of the backend
|
|
|
|
/// can be used as part of a cache key, for example
|
|
|
|
pub fn to_string(&self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Backend::Cranelift => "cranelift",
|
|
|
|
Backend::Singlepass => "singlepass",
|
|
|
|
Backend::LLVM => "llvm",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 19:22:17 +00:00
|
|
|
impl Default for Backend {
|
|
|
|
fn default() -> Self {
|
|
|
|
Backend::Cranelift
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 17:50:28 +00:00
|
|
|
impl std::str::FromStr for Backend {
|
|
|
|
type Err = String;
|
|
|
|
fn from_str(s: &str) -> Result<Backend, String> {
|
|
|
|
match s.to_lowercase().as_str() {
|
|
|
|
"singlepass" => Ok(Backend::Singlepass),
|
|
|
|
"cranelift" => Ok(Backend::Cranelift),
|
|
|
|
"llvm" => Ok(Backend::LLVM),
|
|
|
|
_ => Err(format!("The backend {} doesn't exist", s)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod backend_test {
|
|
|
|
use super::*;
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn str_repr_matches() {
|
|
|
|
// if this test breaks, think hard about why it's breaking
|
|
|
|
// can we avoid having these be different?
|
|
|
|
|
|
|
|
for &backend in &[Backend::Cranelift, Backend::LLVM, Backend::Singlepass] {
|
|
|
|
assert_eq!(backend, Backend::from_str(backend.to_string()).unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 20:13:01 +00:00
|
|
|
/// This type cannot be constructed from
|
|
|
|
/// outside the runtime crate.
|
|
|
|
pub struct Token {
|
|
|
|
_private: (),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Token {
|
|
|
|
pub(crate) fn generate() -> Self {
|
|
|
|
Self { _private: () }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 17:25:29 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum MemoryBoundCheckMode {
|
|
|
|
Default,
|
|
|
|
Enable,
|
|
|
|
Disable,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for MemoryBoundCheckMode {
|
|
|
|
fn default() -> MemoryBoundCheckMode {
|
|
|
|
MemoryBoundCheckMode::Default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 21:01:27 +00:00
|
|
|
/// Configuration data for the compiler
|
2019-05-06 14:15:30 +00:00
|
|
|
#[derive(Default)]
|
2019-03-27 21:01:27 +00:00
|
|
|
pub struct CompilerConfig {
|
|
|
|
/// Symbol information generated from emscripten; used for more detailed debug messages
|
|
|
|
pub symbol_map: Option<HashMap<u32, String>>,
|
2019-05-08 17:25:29 +00:00
|
|
|
pub memory_bound_check_mode: MemoryBoundCheckMode,
|
2019-05-06 14:15:30 +00:00
|
|
|
pub enforce_stack_check: bool,
|
2019-03-27 21:01:27 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:09:47 +00:00
|
|
|
pub trait Compiler {
|
2019-01-18 20:13:01 +00:00
|
|
|
/// Compiles a `Module` from WebAssembly binary format.
|
|
|
|
/// The `CompileToken` parameter ensures that this can only
|
|
|
|
/// be called from inside the runtime.
|
2019-03-27 21:01:27 +00:00
|
|
|
fn compile(
|
|
|
|
&self,
|
|
|
|
wasm: &[u8],
|
|
|
|
comp_conf: CompilerConfig,
|
|
|
|
_: Token,
|
|
|
|
) -> CompileResult<ModuleInner>;
|
2019-02-07 00:26:45 +00:00
|
|
|
|
2019-02-22 01:06:49 +00:00
|
|
|
unsafe fn from_cache(&self, cache: Artifact, _: Token) -> Result<ModuleInner, CacheError>;
|
2019-01-18 20:13:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 01:01:54 +00:00
|
|
|
pub trait RunnableModule: Send + Sync {
|
2019-01-18 20:13:01 +00:00
|
|
|
/// This returns a pointer to the function designated by the `local_func_index`
|
|
|
|
/// parameter.
|
2019-04-12 01:01:54 +00:00
|
|
|
fn get_func(
|
2019-01-16 18:26:10 +00:00
|
|
|
&self,
|
2019-04-12 01:01:54 +00:00
|
|
|
info: &ModuleInfo,
|
2019-01-16 18:26:10 +00:00
|
|
|
local_func_index: LocalFuncIndex,
|
|
|
|
) -> Option<NonNull<vm::Func>>;
|
2019-04-12 01:01:54 +00:00
|
|
|
|
2019-06-12 14:02:15 +00:00
|
|
|
fn get_module_state_map(&self) -> Option<ModuleStateMap> {
|
|
|
|
None
|
|
|
|
}
|
2019-06-09 13:21:18 +00:00
|
|
|
|
2019-07-03 17:45:06 +00:00
|
|
|
fn get_breakpoints(&self) -> Option<BreakpointMap> {
|
2019-06-27 07:49:43 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-06-18 22:00:19 +00:00
|
|
|
/// A wasm trampoline contains the necessary data to dynamically call an exported wasm function.
|
2019-04-12 01:01:54 +00:00
|
|
|
/// Given a particular signature index, we are returned a trampoline that is matched with that
|
|
|
|
/// signature and an invoke function that can call the trampoline.
|
|
|
|
fn get_trampoline(&self, info: &ModuleInfo, sig_index: SigIndex) -> Option<Wasm>;
|
|
|
|
|
2019-04-18 17:00:15 +00:00
|
|
|
unsafe fn do_early_trap(&self, data: Box<dyn Any>) -> !;
|
2019-05-02 16:23:41 +00:00
|
|
|
|
2019-05-14 08:13:42 +00:00
|
|
|
/// Returns the machine code associated with this module.
|
2019-05-14 08:04:08 +00:00
|
|
|
fn get_code(&self) -> Option<&[u8]> {
|
|
|
|
None
|
|
|
|
}
|
2019-05-14 08:13:42 +00:00
|
|
|
|
|
|
|
/// Returns the beginning offsets of all functions, including import trampolines.
|
2019-05-14 08:04:08 +00:00
|
|
|
fn get_offsets(&self) -> Option<Vec<usize>> {
|
|
|
|
None
|
|
|
|
}
|
2019-01-08 17:09:47 +00:00
|
|
|
}
|
2019-02-18 19:56:20 +00:00
|
|
|
|
|
|
|
pub trait CacheGen: Send + Sync {
|
2019-04-19 20:54:48 +00:00
|
|
|
fn generate_cache(&self) -> Result<(Box<[u8]>, Memory), CacheError>;
|
2019-02-21 00:41:41 +00:00
|
|
|
}
|