wasmi_core/
typed.rs

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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
use crate::{TrapCode, UntypedVal, ValType, F32, F64};

/// Types that are associated to a static Wasm type.
pub trait Typed {
    /// The static associated Wasm type.
    const TY: ValType;
}
macro_rules! impl_typed_for {
    ( $( $ty:ty => $value_ty:expr );* $(;)? ) => {
        $(
            impl Typed for $ty {
                const TY: ValType = $value_ty;
            }
        )*
    };
}
impl_typed_for! {
    bool => ValType::I32;
    i32 => ValType::I32;
    u32 => ValType::I32;
    i64 => ValType::I64;
    u64 => ValType::I64;
    f32 => ValType::F32;
    f64 => ValType::F64;
    F32 => ValType::F32;
    F64 => ValType::F64;
}

impl From<TypedVal> for UntypedVal {
    fn from(typed_value: TypedVal) -> Self {
        typed_value.value
    }
}

/// An [`UntypedVal`] with its assumed [`ValType`].
///
/// # Note
///
/// We explicitly do not make use of the existing [`Val`]
/// abstraction since [`Val`] is optimized towards being a
/// user facing type whereas [`TypedVal`] is focusing on
/// performance and efficiency in computations.
///
/// [`Val`]: [`crate::core::Value`]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct TypedVal {
    /// The type of the value.
    ty: ValType,
    /// The underlying raw value.
    value: UntypedVal,
}

impl TypedVal {
    /// Create a new [`TypedVal`].
    pub fn new(ty: ValType, value: UntypedVal) -> Self {
        Self { ty, value }
    }

    /// Returns the [`ValType`] of the [`TypedVal`].
    pub fn ty(&self) -> ValType {
        self.ty
    }

    /// Returns the [`UntypedVal`] of the [`TypedVal`].
    pub fn untyped(&self) -> UntypedVal {
        self.value
    }

    /// Changes the [`ValType`] of `self` to `ty`.
    ///
    /// # Note
    ///
    /// This acts similar to a Wasm reinterpret cast and
    /// the underlying `value` bits are unchanged.
    pub fn reinterpret(self, ty: ValType) -> Self {
        Self { ty, ..self }
    }
}

impl<T> From<T> for TypedVal
where
    T: Typed + Into<UntypedVal>,
{
    fn from(value: T) -> Self {
        Self::new(<T as Typed>::TY, value.into())
    }
}

/// Helper trait to access the `Ok` and `Err` type of a `Result` type.
trait ResultType {
    /// The `Result::Ok` type.
    type Ok;
    /// The `Result::Err` type.
    type Err;
}
impl<T, E> ResultType for Result<T, E> {
    type Ok = T;
    type Err = E;
}

macro_rules! impl_from_typed_value_for {
    ( $( impl From<TypedValue> for $ty:ty );* $(;)? ) => {
        $(
            impl From<TypedVal> for $ty {
                fn from(typed_value: TypedVal) -> Self {
                    // # Note
                    //
                    // We only use a `debug_assert` here instead of a proper `assert`
                    // since the whole translation process assumes that Wasm validation
                    // was already performed and thus type checking does not necessarily
                    // need to happen redundantly outside of debug builds.
                    debug_assert!(matches!(typed_value.ty, <$ty as Typed>::TY));
                    Self::from(typed_value.value)
                }
            }
        )*
    };
}
impl_from_typed_value_for! {
    impl From<TypedValue> for bool;
    impl From<TypedValue> for i32;
    impl From<TypedValue> for u32;
    impl From<TypedValue> for i64;
    impl From<TypedValue> for u64;
    impl From<TypedValue> for f32;
    impl From<TypedValue> for f64;
    impl From<TypedValue> for F32;
    impl From<TypedValue> for F64;
}

macro_rules! impl_forwarding {
    ( $( $(#[$mode:ident])? fn $name:ident $params:tt -> $result_ty:ty );* $(;)? ) => {
        $(
            impl_forwarding!( @impl $(#[$mode])? fn $name $params -> $result_ty );
        )*
    };
    ( @impl #[fallible] fn $name:ident($lhs_ty:ty, $rhs_ty:ty) -> $result_ty:ty ) => {
        #[doc = concat!("Forwards to [`UntypedVal::", stringify!($name), "`] with debug type checks.")]
        #[doc = ""]
        #[doc = "# Errors"]
        #[doc = ""]
        #[doc = concat!("If [`UntypedVal::", stringify!($name), "`] returns an error.")]
        #[doc = ""]
        #[doc = "# Panics (Debug)"]
        #[doc = ""]
        #[doc = "If type checks fail."]
        pub fn $name(self, other: Self) -> Result<Self, TrapCode> {
            debug_assert!(matches!(self.ty(), <$lhs_ty as Typed>::TY));
            debug_assert!(matches!(other.ty(), <$rhs_ty as Typed>::TY));
            Ok(Self::new(
                <<$result_ty as ResultType>::Ok as Typed>::TY,
                UntypedVal::$name(UntypedVal::from(self), UntypedVal::from(other))?,
            ))
        }
    };
    ( @impl fn $name:ident($lhs_ty:ty, $rhs_ty:ty) -> $result_ty:ty ) => {
        #[doc = concat!("Forwards to [`UntypedVal::", stringify!($name), "`] with debug type checks.")]
        #[doc = ""]
        #[doc = "# Panics (Debug)"]
        #[doc = ""]
        #[doc = "If type checks fail."]
        pub fn $name(self, other: Self) -> Self {
            debug_assert!(matches!(self.ty(), <$lhs_ty as Typed>::TY));
            debug_assert!(matches!(other.ty(), <$rhs_ty as Typed>::TY));
            Self::new(
                <$result_ty as Typed>::TY,
                UntypedVal::$name(UntypedVal::from(self), UntypedVal::from(other)),
            )
        }
    };
    ( @impl #[fallible] fn $name:ident($input_ty:ty) -> $result_ty:ty ) => {
        #[doc = concat!("Forwards to [`UntypedVal::", stringify!($name), "`] with debug type checks.")]
        #[doc = ""]
        #[doc = "# Errors"]
        #[doc = ""]
        #[doc = concat!("If [`UntypedVal::", stringify!($name), "`] returns an error.")]
        #[doc = ""]
        #[doc = "# Panics (Debug)"]
        #[doc = ""]
        #[doc = "If type checks fail."]
        pub fn $name(self) -> Result<Self, TrapCode> {
            debug_assert!(matches!(self.ty(), <$input_ty as Typed>::TY));
            Ok(Self::new(
                <<$result_ty as ResultType>::Ok as Typed>::TY,
                UntypedVal::$name(UntypedVal::from(self))?,
            ))
        }
    };
    ( @impl fn $name:ident($input_ty:ty) -> $result_ty:ty ) => {
        #[doc = concat!("Forwards to [`UntypedVal::", stringify!($name), "`] with debug type checks.")]
        #[doc = ""]
        #[doc = "# Panics (Debug)"]
        #[doc = ""]
        #[doc = "If type checks fail."]
        pub fn $name(self) -> Self {
            debug_assert!(matches!(self.ty(), <$input_ty as Typed>::TY));
            Self::new(
                <$result_ty as Typed>::TY,
                UntypedVal::$name(UntypedVal::from(self)),
            )
        }
    };
}
impl TypedVal {
    impl_forwarding! {
        // Comparison Instructions

        fn i32_eq(i32, i32) -> i32;
        fn i64_eq(i64, i64) -> i32;
        fn f32_eq(f32, f32) -> i32;
        fn f64_eq(f64, f64) -> i32;

        fn i32_ne(i32, i32) -> i32;
        fn i64_ne(i64, i64) -> i32;
        fn f32_ne(f32, f32) -> i32;
        fn f64_ne(f64, f64) -> i32;

        fn i32_lt_s(i32, i32) -> i32;
        fn i32_lt_u(i32, i32) -> i32;
        fn i32_gt_s(i32, i32) -> i32;
        fn i32_gt_u(i32, i32) -> i32;
        fn i32_le_s(i32, i32) -> i32;
        fn i32_le_u(i32, i32) -> i32;
        fn i32_ge_s(i32, i32) -> i32;
        fn i32_ge_u(i32, i32) -> i32;

        fn i64_lt_s(i64, i64) -> i32;
        fn i64_lt_u(i64, i64) -> i32;
        fn i64_gt_s(i64, i64) -> i32;
        fn i64_gt_u(i64, i64) -> i32;
        fn i64_le_s(i64, i64) -> i32;
        fn i64_le_u(i64, i64) -> i32;
        fn i64_ge_s(i64, i64) -> i32;
        fn i64_ge_u(i64, i64) -> i32;

        fn f32_lt(f32, f32) -> i32;
        fn f32_gt(f32, f32) -> i32;
        fn f32_le(f32, f32) -> i32;
        fn f32_ge(f32, f32) -> i32;

        fn f64_lt(f64, f64) -> i32;
        fn f64_gt(f64, f64) -> i32;
        fn f64_le(f64, f64) -> i32;
        fn f64_ge(f64, f64) -> i32;

        // Integer Arithmetic Instructions

        fn i32_clz(i32) -> i32;
        fn i32_ctz(i32) -> i32;
        fn i32_popcnt(i32) -> i32;

        fn i64_clz(i64) -> i64;
        fn i64_ctz(i64) -> i64;
        fn i64_popcnt(i64) -> i64;

        fn i32_add(i32, i32) -> i32;
        fn i32_sub(i32, i32) -> i32;
        fn i32_mul(i32, i32) -> i32;

        fn i64_add(i64, i64) -> i64;
        fn i64_sub(i64, i64) -> i64;
        fn i64_mul(i64, i64) -> i64;

        #[fallible] fn i32_div_s(i32, i32) -> Result<i32, TrapCode>;
        #[fallible] fn i32_div_u(i32, i32) -> Result<i32, TrapCode>;
        #[fallible] fn i32_rem_s(i32, i32) -> Result<i32, TrapCode>;
        #[fallible] fn i32_rem_u(i32, i32) -> Result<i32, TrapCode>;

        #[fallible] fn i64_div_s(i64, i64) -> Result<i64, TrapCode>;
        #[fallible] fn i64_div_u(i64, i64) -> Result<i64, TrapCode>;
        #[fallible] fn i64_rem_s(i64, i64) -> Result<i64, TrapCode>;
        #[fallible] fn i64_rem_u(i64, i64) -> Result<i64, TrapCode>;

        // Shift & Rotate Instructions

        fn i32_shl(i32, i32) -> i32;
        fn i32_shr_s(i32, i32) -> i32;
        fn i32_shr_u(i32, i32) -> i32;
        fn i32_rotl(i32, i32) -> i32;
        fn i32_rotr(i32, i32) -> i32;

        fn i64_shl(i64, i64) -> i64;
        fn i64_shr_s(i64, i64) -> i64;
        fn i64_shr_u(i64, i64) -> i64;
        fn i64_rotl(i64, i64) -> i64;
        fn i64_rotr(i64, i64) -> i64;

        // Bitwise Instructions

        fn i32_and(i32, i32) -> i32;
        fn i32_or(i32, i32) -> i32;
        fn i32_xor(i32, i32) -> i32;

        fn i64_and(i64, i64) -> i64;
        fn i64_or(i64, i64) -> i64;
        fn i64_xor(i64, i64) -> i64;

        // Float Arithmetic Instructions

        fn f32_abs(f32) -> f32;
        fn f32_neg(f32) -> f32;
        fn f32_ceil(f32) -> f32;
        fn f32_floor(f32) -> f32;
        fn f32_trunc(f32) -> f32;
        fn f32_nearest(f32) -> f32;
        fn f32_sqrt(f32) -> f32;

        fn f64_abs(f64) -> f64;
        fn f64_neg(f64) -> f64;
        fn f64_ceil(f64) -> f64;
        fn f64_floor(f64) -> f64;
        fn f64_trunc(f64) -> f64;
        fn f64_nearest(f64) -> f64;
        fn f64_sqrt(f64) -> f64;

        fn f32_add(f32, f32) -> f32;
        fn f32_sub(f32, f32) -> f32;
        fn f32_mul(f32, f32) -> f32;
        fn f32_div(f32, f32) -> f32;
        fn f32_min(f32, f32) -> f32;
        fn f32_max(f32, f32) -> f32;
        fn f32_copysign(f32, f32) -> f32;

        fn f64_add(f64, f64) -> f64;
        fn f64_sub(f64, f64) -> f64;
        fn f64_mul(f64, f64) -> f64;
        fn f64_div(f64, f64) -> f64;
        fn f64_min(f64, f64) -> f64;
        fn f64_max(f64, f64) -> f64;
        fn f64_copysign(f64, f64) -> f64;

        // Conversions

        fn i32_wrap_i64(i64) -> i32;
        fn i64_extend_i32_s(i32) -> i64;

        fn f32_demote_f64(f64) -> f32;
        fn f64_promote_f32(f32) -> f64;

        #[fallible] fn i32_trunc_f32_s(f32) -> Result<i32, TrapCode>;
        #[fallible] fn i32_trunc_f32_u(f32) -> Result<i32, TrapCode>;
        #[fallible] fn i32_trunc_f64_s(f64) -> Result<i32, TrapCode>;
        #[fallible] fn i32_trunc_f64_u(f64) -> Result<i32, TrapCode>;
        #[fallible] fn i64_trunc_f32_s(f32) -> Result<i64, TrapCode>;
        #[fallible] fn i64_trunc_f32_u(f32) -> Result<i64, TrapCode>;
        #[fallible] fn i64_trunc_f64_s(f64) -> Result<i64, TrapCode>;
        #[fallible] fn i64_trunc_f64_u(f64) -> Result<i64, TrapCode>;

        fn i32_trunc_sat_f32_s(f32) -> i32;
        fn i32_trunc_sat_f32_u(f32) -> i32;
        fn i32_trunc_sat_f64_s(f64) -> i32;
        fn i32_trunc_sat_f64_u(f64) -> i32;
        fn i64_trunc_sat_f32_s(f32) -> i64;
        fn i64_trunc_sat_f32_u(f32) -> i64;
        fn i64_trunc_sat_f64_s(f64) -> i64;
        fn i64_trunc_sat_f64_u(f64) -> i64;

        fn f32_convert_i32_s(i32) -> f32;
        fn f32_convert_i32_u(i32) -> f32;
        fn f32_convert_i64_s(i64) -> f32;
        fn f32_convert_i64_u(i64) -> f32;
        fn f64_convert_i32_s(i32) -> f64;
        fn f64_convert_i32_u(i32) -> f64;
        fn f64_convert_i64_s(i64) -> f64;
        fn f64_convert_i64_u(i64) -> f64;

        fn i32_extend8_s(i32) -> i32;
        fn i32_extend16_s(i32) -> i32;
        fn i64_extend8_s(i64) -> i64;
        fn i64_extend16_s(i64) -> i64;
        fn i64_extend32_s(i64) -> i64;
    }
}