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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use crate::environ::TargetEnvironment;
use crate::WasmResult;
use core::convert::TryInto;
use core::u32;
use cranelift_codegen::ir;
use cranelift_frontend::FunctionBuilder;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use wasmparser::{FuncValidator, WasmFuncType, WasmModuleResources};
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub enum TableElementType {
Val(ir::Type),
Func,
}
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()?))
}
}
}
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),
}
}
pub fn blocktype_params_results<'a, T>(
validator: &'a FuncValidator<T>,
ty: wasmparser::BlockType,
) -> WasmResult<(
impl ExactSizeIterator<Item = wasmparser::ValType> + Clone + 'a,
impl ExactSizeIterator<Item = wasmparser::ValType> + Clone + 'a,
)>
where
T: WasmModuleResources,
{
return Ok(match ty {
wasmparser::BlockType::Empty => {
let params: &'static [wasmparser::ValType] = &[];
let results: &'static [wasmparser::ValType] = &[];
(
itertools::Either::Left(params.iter().copied()),
itertools::Either::Left(results.iter().copied()),
)
}
wasmparser::BlockType::Type(ty) => {
let params: &'static [wasmparser::ValType] = &[];
let results: &'static [wasmparser::ValType] = match ty {
wasmparser::ValType::I32 => &[wasmparser::ValType::I32],
wasmparser::ValType::I64 => &[wasmparser::ValType::I64],
wasmparser::ValType::F32 => &[wasmparser::ValType::F32],
wasmparser::ValType::F64 => &[wasmparser::ValType::F64],
wasmparser::ValType::V128 => &[wasmparser::ValType::V128],
wasmparser::ValType::ExternRef => &[wasmparser::ValType::ExternRef],
wasmparser::ValType::FuncRef => &[wasmparser::ValType::FuncRef],
};
(
itertools::Either::Left(params.iter().copied()),
itertools::Either::Left(results.iter().copied()),
)
}
wasmparser::BlockType::FuncType(ty_index) => {
let ty = validator
.resources()
.func_type_at(ty_index)
.expect("should be valid");
(
itertools::Either::Right(ty.inputs()),
itertools::Either::Right(ty.outputs()),
)
}
});
}
pub fn block_with_params<PE: TargetEnvironment + ?Sized>(
builder: &mut FunctionBuilder,
params: impl IntoIterator<Item = wasmparser::ValType>,
environ: &PE,
) -> WasmResult<ir::Block> {
let block = builder.create_block();
for ty in params {
match ty {
wasmparser::ValType::I32 => {
builder.append_block_param(block, ir::types::I32);
}
wasmparser::ValType::I64 => {
builder.append_block_param(block, ir::types::I64);
}
wasmparser::ValType::F32 => {
builder.append_block_param(block, ir::types::F32);
}
wasmparser::ValType::F64 => {
builder.append_block_param(block, ir::types::F64);
}
wasmparser::ValType::ExternRef | wasmparser::ValType::FuncRef => {
builder.append_block_param(block, environ.reference_type(ty.try_into()?));
}
wasmparser::ValType::V128 => {
builder.append_block_param(block, ir::types::I8X16);
}
}
}
Ok(block)
}
pub fn f32_translation(x: wasmparser::Ieee32) -> ir::immediates::Ieee32 {
ir::immediates::Ieee32::with_bits(x.bits())
}
pub fn f64_translation(x: wasmparser::Ieee64) -> ir::immediates::Ieee64 {
ir::immediates::Ieee64::with_bits(x.bits())
}
pub fn get_vmctx_value_label() -> ir::ValueLabel {
const VMCTX_LABEL: u32 = 0xffff_fffe;
ir::ValueLabel::from_u32(VMCTX_LABEL)
}