Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 1 addition & 39 deletions cranelift/wasm/src/state/module_state.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::{SignatureIndex, WasmError, WasmResult};
use cranelift_codegen::ir::{types, Type};
use crate::SignatureIndex;
use cranelift_entity::PrimaryMap;
use std::boxed::Box;
use std::vec::Vec;

/// Map of signatures to a function's parameter and return types.
pub(crate) type WasmTypes =
Expand All @@ -23,47 +21,11 @@ pub struct ModuleTranslationState {
pub(crate) wasm_types: WasmTypes,
}

fn cranelift_to_wasmparser_type(ty: Type) -> WasmResult<wasmparser::ValType> {
Ok(match ty {
types::I32 => wasmparser::ValType::I32,
types::I64 => wasmparser::ValType::I64,
types::F32 => wasmparser::ValType::F32,
types::F64 => wasmparser::ValType::F64,
types::R32 | types::R64 => wasmparser::ValType::ExternRef,
_ => {
return Err(WasmError::Unsupported(format!(
"Cannot convert Cranelift type to Wasm signature: {:?}",
ty
)));
}
})
}

impl ModuleTranslationState {
/// Creates a new empty ModuleTranslationState.
pub fn new() -> Self {
Self {
wasm_types: PrimaryMap::new(),
}
}

/// Create a new ModuleTranslationState with the given function signatures,
/// provided in terms of Cranelift types. The provided slice of signatures
/// is indexed by signature number, and contains pairs of (args, results)
/// slices.
pub fn from_func_sigs(sigs: &[(&[Type], &[Type])]) -> WasmResult<Self> {
let mut wasm_types = PrimaryMap::with_capacity(sigs.len());
for &(ref args, ref results) in sigs {
let args: Vec<wasmparser::ValType> = args
.iter()
.map(|&ty| cranelift_to_wasmparser_type(ty))
.collect::<Result<_, _>>()?;
let results: Vec<wasmparser::ValType> = results
.iter()
.map(|&ty| cranelift_to_wasmparser_type(ty))
.collect::<Result<_, _>>()?;
wasm_types.push((args.into_boxed_slice(), results.into_boxed_slice()));
}
Ok(Self { wasm_types })
}
}
44 changes: 0 additions & 44 deletions cranelift/wasm/src/translation_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,6 @@ use cranelift_frontend::FunctionBuilder;
use serde::{Deserialize, Serialize};
use wasmparser::{FuncValidator, WasmFuncType, WasmModuleResources};

/// WebAssembly table element. Can be a function or a scalar type.
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub enum TableElementType {
/// A scalar type.
Val(ir::Type),
/// A function.
Func,
}

/// Helper function translating wasmparser types to Cranelift types when possible.
pub fn type_to_type<PE: TargetEnvironment + ?Sized>(
ty: wasmparser::ValType,
environ: &PE,
) -> WasmResult<ir::Type> {
match ty {
wasmparser::ValType::I32 => Ok(ir::types::I32),
wasmparser::ValType::I64 => Ok(ir::types::I64),
wasmparser::ValType::F32 => Ok(ir::types::F32),
wasmparser::ValType::F64 => Ok(ir::types::F64),
wasmparser::ValType::V128 => Ok(ir::types::I8X16),
wasmparser::ValType::ExternRef | wasmparser::ValType::FuncRef => {
Ok(environ.reference_type(ty.try_into()?))
}
}
}

/// Helper function translating wasmparser possible table types to Cranelift types when possible,
/// or None for Func tables.
pub fn tabletype_to_type<PE: TargetEnvironment + ?Sized>(
ty: wasmparser::ValType,
environ: &PE,
) -> WasmResult<Option<ir::Type>> {
match ty {
wasmparser::ValType::I32 => Ok(Some(ir::types::I32)),
wasmparser::ValType::I64 => Ok(Some(ir::types::I64)),
wasmparser::ValType::F32 => Ok(Some(ir::types::F32)),
wasmparser::ValType::F64 => Ok(Some(ir::types::F64)),
wasmparser::ValType::V128 => Ok(Some(ir::types::I8X16)),
wasmparser::ValType::ExternRef => Ok(Some(environ.reference_type(ty.try_into()?))),
wasmparser::ValType::FuncRef => Ok(None),
}
}

/// Get the parameter and result types for the given Wasm blocktype.
pub fn blocktype_params_results<'a, T>(
validator: &'a FuncValidator<T>,
Expand Down