Add CompileError::UnsupportedTarget error

This commit is contained in:
Simon Warta
2020-12-06 13:48:54 +01:00
parent 4768bc27bf
commit 249b955d71
2 changed files with 82 additions and 4 deletions

View File

@ -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};
@ -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()));
}
@ -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),
};
}
}