1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use crate::{SignatureIndex, WasmError, WasmResult};
use cranelift_codegen::ir::{types, Type};
use cranelift_entity::PrimaryMap;
use std::boxed::Box;
use std::vec::Vec;
pub(crate) type WasmTypes =
PrimaryMap<SignatureIndex, (Box<[wasmparser::ValType]>, Box<[wasmparser::ValType]>)>;
#[derive(Debug)]
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 {
pub fn new() -> Self {
Self {
wasm_types: PrimaryMap::new(),
}
}
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 })
}
}