wasmi_ir 1.0.4

WebAssembly interpreter internal bytecode representation
Documentation
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use core::{fmt::Debug, marker::PhantomData, num::NonZero};

/// Error that may occur upon converting values to [`Const16`].
#[derive(Debug, Copy, Clone)]
pub struct OutOfBoundsConst;

/// A typed 16-bit encoded constant value.
#[derive(Debug)]
pub struct Const16<T> {
    /// The underlying untyped value.
    inner: AnyConst16,
    /// The type marker to satisfy the Rust type system.
    marker: PhantomData<fn() -> T>,
}

impl Const16<u64> {
    /// Casts the `Const16<u32>` to a `Const16<u64>` value.
    pub fn cast(const16: Const16<u32>) -> Self {
        Self::new(const16.inner)
    }
}

impl<T> Const16<T> {
    /// Returns `true` if the [`Const16`] is equal to zero.
    pub fn is_zero(&self) -> bool {
        self.inner == AnyConst16::from(0_i16)
    }
}

impl<T> Const16<T> {
    /// Crete a new typed [`Const16`] value.
    fn new(inner: AnyConst16) -> Self {
        Self {
            inner,
            marker: PhantomData,
        }
    }
}

impl<T> Clone for Const16<T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> Copy for Const16<T> {}

impl<T> PartialEq for Const16<T> {
    fn eq(&self, other: &Self) -> bool {
        self.inner == other.inner
    }
}

impl<T> Eq for Const16<T> {}

macro_rules! impl_const16_from {
    ( $( ($from:ty, $to:ty) ),* $(,)? ) => {
        $(
            impl From<$from> for Const16<$to> {
                fn from(value: $from) -> Self {
                    Self::new(AnyConst16::from(value))
                }
            }

            impl From<NonZero<$from>> for Const16<NonZero<$to>> {
                fn from(value: NonZero<$from>) -> Self {
                    Self::new(AnyConst16::from(value.get()))
                }
            }
        )*
    }
}
impl_const16_from!((i16, i32), (u16, u32), (i16, i64), (u16, u64),);

macro_rules! impl_const16_from {
    ( $($ty:ty),* ) => {
        $(
            impl From<Const16<$ty>> for $ty {
                fn from(value: Const16<Self>) -> Self {
                    Self::from(value.inner)
                }
            }

            impl From<Const16<NonZero<$ty>>> for NonZero<$ty> {
                fn from(value: Const16<Self>) -> Self {
                    // SAFETY: Due to construction of `Const16<NonZeroI32>` we are guaranteed
                    //         that `value.inner` is a valid non-zero value.
                    unsafe { Self::new_unchecked(<$ty as From<AnyConst16>>::from(value.inner)) }
                }
            }

            impl TryFrom<$ty> for Const16<$ty> {
                type Error = OutOfBoundsConst;

                fn try_from(value: $ty) -> Result<Self, Self::Error> {
                    AnyConst16::try_from(value).map(Self::new)
                }
            }

            impl TryFrom<NonZero<$ty>> for Const16<NonZero<$ty>> {
                type Error = OutOfBoundsConst;

                fn try_from(value: NonZero<$ty>) -> Result<Self, Self::Error> {
                    AnyConst16::try_from(value).map(Self::new)
                }
            }
        )*
    };
}
impl_const16_from!(i32, u32, i64, u64);

/// A typed 32-bit encoded constant value.
pub struct Const32<T> {
    /// The underlying untyped value.
    inner: AnyConst32,
    /// The type marker to satisfy the Rust type system.
    marker: PhantomData<fn() -> T>,
}

impl<T> Debug for Const32<T>
where
    Self: Into<T>,
    T: Debug,
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let inner: T = (*self).into();
        inner.fmt(f)
    }
}

impl<T> Const32<T> {
    /// Crete a new typed [`Const32`] value.
    fn new(inner: AnyConst32) -> Self {
        Self {
            inner,
            marker: PhantomData,
        }
    }
}

impl Const32<u64> {
    /// Casts the `Const16<u32>` to a `Const16<u64>` value.
    pub fn cast(const32: Const32<u32>) -> Self {
        Self::new(const32.inner)
    }
}

impl<T> Clone for Const32<T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> Copy for Const32<T> {}

impl<T> PartialEq for Const32<T> {
    fn eq(&self, other: &Self) -> bool {
        self.inner == other.inner
    }
}

impl<T> Eq for Const32<T> {}

macro_rules! impl_const32 {
    ( $ty:ty, $($rest:tt)* ) => {
        impl_const32!(@ $ty, $ty);
        impl_const32!($($rest)*);
    };
    ( $ty64:ty as $ty32:ty, $($rest:tt)* ) => {
        impl TryFrom<$ty64> for Const32<$ty64> {
            type Error = OutOfBoundsConst;

            fn try_from(value: $ty64) -> Result<Self, Self::Error> {
                AnyConst32::try_from(value).map(Self::new)
            }
        }
        impl_const32!(@ $ty64, $ty32);
        impl_const32!($($rest)*);
    };
    ( @ $ty:ty, $ty32:ty ) => {
        impl From<$ty32> for Const32<$ty> {
            fn from(value: $ty32) -> Self {
                Self::new(AnyConst32::from(value))
            }
        }

        impl From<Const32<$ty>> for $ty {
            fn from(value: Const32<Self>) -> Self {
                Self::from(value.inner)
            }
        }
    };
    () => {};
}
impl_const32!(i32, u32, i64 as i32, u64 as u32, f32, f64 as f32,);

/// A 16-bit constant value of any type.
///
/// # Note
///
/// Can be used to store information about small integer values.
/// Upon use the small 16-bit value has to be sign-extended to
/// the actual integer type, e.g. `i32` or `i64`.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct AnyConst16 {
    bits: u16,
}

impl AnyConst16 {
    /// Creates a new [`AnyConst16`] from the given `bits`.
    fn from_bits(bits: u16) -> Self {
        Self { bits }
    }
}

macro_rules! impl_any_const16 {
    ( $( $ty:ty as $ty16:ty ),* $(,)? ) => {
        $(
            impl TryFrom<$ty> for AnyConst16 {
                type Error = OutOfBoundsConst;

                fn try_from(value: $ty) -> Result<Self, Self::Error> {
                    <$ty16>::try_from(value)
                        .map(Self::from)
                        .map_err(|_| OutOfBoundsConst)
                }
            }

            impl TryFrom<NonZero<$ty>> for AnyConst16 {
                type Error = OutOfBoundsConst;

                fn try_from(value: NonZero<$ty>) -> Result<Self, Self::Error> {
                    <NonZero<$ty16>>::try_from(value)
                        .map(<NonZero<$ty16>>::get)
                        .map(Self::from)
                        .map_err(|_| OutOfBoundsConst)
                }
            }
        )*
    };
}
impl_any_const16!(i32 as i16, u32 as u16, i64 as i16, u64 as u16);

impl From<i8> for AnyConst16 {
    fn from(value: i8) -> Self {
        Self::from_bits(value as u8 as u16)
    }
}

impl From<i16> for AnyConst16 {
    fn from(value: i16) -> Self {
        Self::from_bits(value as u16)
    }
}

impl From<u16> for AnyConst16 {
    fn from(value: u16) -> Self {
        Self::from_bits(value)
    }
}

impl From<AnyConst16> for i8 {
    fn from(value: AnyConst16) -> Self {
        value.bits as i8
    }
}

impl From<AnyConst16> for i16 {
    fn from(value: AnyConst16) -> Self {
        u16::from(value) as i16
    }
}

impl From<AnyConst16> for u16 {
    fn from(value: AnyConst16) -> Self {
        value.bits
    }
}

impl From<AnyConst16> for i32 {
    fn from(value: AnyConst16) -> Self {
        Self::from(i16::from(value))
    }
}

impl From<AnyConst16> for i64 {
    fn from(value: AnyConst16) -> Self {
        Self::from(i16::from(value))
    }
}

impl From<AnyConst16> for u32 {
    fn from(value: AnyConst16) -> Self {
        Self::from(u16::from(value))
    }
}

impl From<AnyConst16> for u64 {
    fn from(value: AnyConst16) -> Self {
        Self::from(u16::from(value))
    }
}

/// A 32-bit constant value of any type.
///
/// # Note
///
/// Can be used to store information about small integer values.
/// Upon use the small 32-bit value has to be sign-extended to
/// the actual integer type, e.g. `i32` or `i64`.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct AnyConst32 {
    bits: u32,
}

impl AnyConst32 {
    /// Creates a new [`AnyConst32`] from the given `bits`.
    fn from_bits(bits: u32) -> Self {
        Self { bits }
    }
}

impl TryFrom<u64> for AnyConst32 {
    type Error = OutOfBoundsConst;

    fn try_from(value: u64) -> Result<Self, Self::Error> {
        u32::try_from(value)
            .map(Self::from)
            .map_err(|_| OutOfBoundsConst)
    }
}

impl TryFrom<i64> for AnyConst32 {
    type Error = OutOfBoundsConst;

    fn try_from(value: i64) -> Result<Self, Self::Error> {
        i32::try_from(value)
            .map(Self::from)
            .map_err(|_| OutOfBoundsConst)
    }
}

impl TryFrom<f64> for AnyConst32 {
    type Error = OutOfBoundsConst;

    fn try_from(value: f64) -> Result<Self, Self::Error> {
        let truncated = value as f32;
        if value.to_bits() != f64::from(truncated).to_bits() {
            return Err(OutOfBoundsConst);
        }
        Ok(Self::from(truncated))
    }
}

impl<T> From<Const32<T>> for AnyConst32 {
    fn from(value: Const32<T>) -> Self {
        value.inner
    }
}

macro_rules! impl_from_for_anyconst32 {
    ( $($ty:ty),* $(,)? ) => {
        $(
            impl From<$ty> for AnyConst32 {
                fn from(value: $ty) -> Self {
                    Self::from_bits(value as _)
                }
            }
        )*
    };
}
impl_from_for_anyconst32!(bool, i8, u8, i16, u16, i32, u32);

impl From<f32> for AnyConst32 {
    fn from(value: f32) -> Self {
        Self::from_bits(f32::to_bits(value))
    }
}

impl From<AnyConst32> for i32 {
    fn from(value: AnyConst32) -> Self {
        u32::from(value) as _
    }
}

impl From<AnyConst32> for i64 {
    fn from(value: AnyConst32) -> Self {
        Self::from(i32::from(value))
    }
}

impl From<AnyConst32> for u32 {
    fn from(value: AnyConst32) -> Self {
        value.bits
    }
}

impl From<AnyConst32> for u64 {
    fn from(value: AnyConst32) -> Self {
        Self::from(u32::from(value))
    }
}

impl From<AnyConst32> for f32 {
    fn from(value: AnyConst32) -> Self {
        Self::from_bits(u32::from(value))
    }
}

impl From<AnyConst32> for f64 {
    fn from(value: AnyConst32) -> Self {
        Self::from(f32::from(value))
    }
}