Skip to content

Commit

Permalink
Add CompileError::UnsupportedTarget error
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Dec 7, 2020
1 parent 4768bc2 commit 249b955
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
81 changes: 77 additions & 4 deletions lib/compiler-singlepass/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use rayon::prelude::{IntoParallelIterator, IntoParallelRefIterator, ParallelIter
use std::sync::Arc;
use wasmer_compiler::wasmparser::BinaryReaderError;
use wasmer_compiler::TrapInformation;
use wasmer_compiler::{Compilation, CompileError, CompiledFunction, Compiler, SectionIndex};
use wasmer_compiler::{
CompileModuleInfo, CompilerConfig, MiddlewareBinaryReader, ModuleMiddlewareChain,
ModuleTranslationState, Target,
Architecture, CompileModuleInfo, CompilerConfig, MiddlewareBinaryReader, ModuleMiddlewareChain,
ModuleTranslationState, OperatingSystem, Target,
};
use wasmer_compiler::{Compilation, CompileError, CompiledFunction, Compiler, SectionIndex};
use wasmer_compiler::{FunctionBody, FunctionBodyData};
use wasmer_types::entity::{EntityRef, PrimaryMap};
use wasmer_types::{FunctionIndex, FunctionType, LocalFunctionIndex, MemoryIndex, TableIndex};
Expand Down Expand Up @@ -44,11 +44,19 @@ impl Compiler for SinglepassCompiler {
/// associated relocations.
fn compile_module(
&self,
_target: &Target,
target: &Target,
compile_info: &mut CompileModuleInfo,
_module_translation: &ModuleTranslationState,
function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'_>>,
) -> Result<Compilation, CompileError> {
if target.triple().operating_system == OperatingSystem::Windows {
return Err(CompileError::UnsupportedTarget(
OperatingSystem::Windows.to_string(),
));
}
if let Architecture::X86_32(arch) = target.triple().architecture {
return Err(CompileError::UnsupportedTarget(arch.to_string()));
}
if compile_info.features.multi_value {
return Err(CompileError::UnsupportedFeature("multivalue".to_string()));
}
Expand Down Expand Up @@ -163,3 +171,68 @@ impl ToCompileError for CodegenError {
fn to_compile_error<T: ToCompileError>(x: T) -> CompileError {
x.to_compile_error()
}

#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
use wasmer_compiler::{CpuFeature, Features, Triple};
use wasmer_vm::{MemoryStyle, TableStyle};

fn dummy_compilation_ingredients<'a>() -> (
CompileModuleInfo,
ModuleTranslationState,
PrimaryMap<LocalFunctionIndex, FunctionBodyData<'a>>,
) {
let compile_info = CompileModuleInfo {
features: Features::new(),
module: Arc::new(ModuleInfo::new()),
memory_styles: PrimaryMap::<MemoryIndex, MemoryStyle>::new(),
table_styles: PrimaryMap::<TableIndex, TableStyle>::new(),
};
let module_translation = ModuleTranslationState::new();
let function_body_inputs = PrimaryMap::<LocalFunctionIndex, FunctionBodyData<'_>>::new();
(compile_info, module_translation, function_body_inputs)
}

#[test]
fn errors_for_unsupported_targets() {
let compiler = SinglepassCompiler::new(Singlepass::default());

// Compile for win64
let win64 = Target::new(
Triple::from_str("x86_64-pc-windows-msvc").unwrap(),
CpuFeature::for_host(),
);
let (mut info, translation, inputs) = dummy_compilation_ingredients();
let result = compiler.compile_module(&win64, &mut info, &translation, inputs);
match result.unwrap_err() {
CompileError::UnsupportedTarget(name) => assert_eq!(name, "windows"),
error => panic!("Unexpected error: {:?}", error),
};

// Compile for 32bit Linux
let linux32 = Target::new(
Triple::from_str("i686-unknown-linux-gnu").unwrap(),
CpuFeature::for_host(),
);
let (mut info, translation, inputs) = dummy_compilation_ingredients();
let result = compiler.compile_module(&linux32, &mut info, &translation, inputs);
match result.unwrap_err() {
CompileError::UnsupportedTarget(name) => assert_eq!(name, "i686"),
error => panic!("Unexpected error: {:?}", error),
};

// Compile for win32
let win32 = Target::new(
Triple::from_str("i686-pc-windows-gnu").unwrap(),
CpuFeature::for_host(),
);
let (mut info, translation, inputs) = dummy_compilation_ingredients();
let result = compiler.compile_module(&win32, &mut info, &translation, inputs);
match result.unwrap_err() {
CompileError::UnsupportedTarget(name) => assert_eq!(name, "windows"), // Windows should be checked before architecture
error => panic!("Unexpected error: {:?}", error),
};
}
}
5 changes: 5 additions & 0 deletions lib/compiler/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ pub enum CompileError {
#[cfg_attr(feature = "std", error("Feature {0} is not yet supported"))]
UnsupportedFeature(String),

/// The compiler cannot compile for the given target.
/// This can refer to the OS, the chipset or any other aspect of the target system.
#[cfg_attr(feature = "std", error("The target {0} is not yet supported (see https://docs.wasmer.io/ecosystem/wasmer/wasmer-features)"))]
UnsupportedTarget(String),

/// Insufficient resources available for execution.
#[cfg_attr(feature = "std", error("Insufficient resources: {0}"))]
Resource(String),
Expand Down

0 comments on commit 249b955

Please sign in to comment.