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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use super::{stack::ValueStack, TypedProvider, TypedVal};
use crate::{
engine::bytecode::{AnyConst16, Const16, Provider, Register, RegisterSpanIter, Sign},
Error,
};
/// A WebAssembly integer. Either `i32` or `i64`.
///
/// # Note
///
/// This trait provides some utility methods useful for translation.
pub trait WasmInteger:
Copy + Eq + From<TypedVal> + Into<TypedVal> + TryInto<AnyConst16> + TryInto<Const16<Self>>
{
/// Returns the `i16` shift amount.
///
/// This computes `self % bitwsize<Self>` and returns the result as `i16` value.
///
/// # Note
///
/// This is commonly needed for Wasm translations of shift and rotate instructions
/// since Wasm mandates that the shift amount operand is taken modulo the bitsize
/// of the shifted value type.
fn as_shift_amount(self) -> i16;
/// Returns `true` if `self` is equal to zero (0).
fn eq_zero(self) -> bool;
}
impl WasmInteger for i32 {
fn as_shift_amount(self) -> i16 {
(self % 32) as i16
}
fn eq_zero(self) -> bool {
self == 0
}
}
impl WasmInteger for u32 {
fn as_shift_amount(self) -> i16 {
(self % 32) as i16
}
fn eq_zero(self) -> bool {
self == 0
}
}
impl WasmInteger for i64 {
fn as_shift_amount(self) -> i16 {
(self % 64) as i16
}
fn eq_zero(self) -> bool {
self == 0
}
}
impl WasmInteger for u64 {
fn as_shift_amount(self) -> i16 {
(self % 64) as i16
}
fn eq_zero(self) -> bool {
self == 0
}
}
/// A WebAssembly float. Either `f32` or `f64`.
///
/// # Note
///
/// This trait provides some utility methods useful for translation.
pub trait WasmFloat: Copy + Into<TypedVal> + From<TypedVal> {
/// Returns `true` if `self` is any kind of NaN value.
fn is_nan(self) -> bool;
/// Returns the [`Sign`] of `self`.
fn sign(self) -> Sign;
}
impl WasmFloat for f32 {
fn is_nan(self) -> bool {
self.is_nan()
}
fn sign(self) -> Sign {
match self.is_sign_positive() {
true => Sign::Pos,
false => Sign::Neg,
}
}
}
impl WasmFloat for f64 {
fn is_nan(self) -> bool {
self.is_nan()
}
fn sign(self) -> Sign {
match self.is_sign_positive() {
true => Sign::Pos,
false => Sign::Neg,
}
}
}
impl Provider<u8> {
/// Creates a new `memory` value [`Provider`] from the general [`TypedProvider`].
pub fn new(provider: TypedProvider) -> Self {
match provider {
TypedProvider::Const(value) => Self::Const(u32::from(value) as u8),
TypedProvider::Register(register) => Self::Register(register),
}
}
}
impl Provider<Const16<u32>> {
/// Creates a new `table` or `memory` index [`Provider`] from the general [`TypedProvider`].
///
/// # Note
///
/// This is a convenience function and used by translation
/// procedures for certain Wasm `table` instructions.
pub fn new(provider: TypedProvider, stack: &mut ValueStack) -> Result<Self, Error> {
match provider {
TypedProvider::Const(value) => match Const16::try_from(u32::from(value)).ok() {
Some(value) => Ok(Self::Const(value)),
None => {
let register = stack.alloc_const(value)?;
Ok(Self::Register(register))
}
},
TypedProvider::Register(index) => Ok(Self::Register(index)),
}
}
}
impl RegisterSpanIter {
/// Creates a [`RegisterSpanIter`] from the given slice of [`TypedProvider`] if possible.
///
/// All [`TypedProvider`] must be [`Register`] and have
/// contiguous indices for the conversion to succeed.
///
/// Returns `None` if the `providers` slice is empty.
pub fn from_providers(providers: &[TypedProvider]) -> Option<Self> {
/// Returns the `i16` [`Register`] index if the [`TypedProvider`] is a [`Register`].
fn register_index(provider: &TypedProvider) -> Option<i16> {
match provider {
TypedProvider::Register(index) => Some(index.to_i16()),
TypedProvider::Const(_) => None,
}
}
let (first, rest) = providers.split_first()?;
let first_index = register_index(first)?;
let mut prev_index = first_index;
for next in rest {
let next_index = register_index(next)?;
if next_index.checked_sub(prev_index)? != 1 {
return None;
}
prev_index = next_index;
}
let end_index = prev_index.checked_add(1)?;
Some(Self::from_raw_parts(
Register::from_i16(first_index),
Register::from_i16(end_index),
))
}
}