Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/pulley/src/imms.rs
1690 views
1
//! Immediates.
2
3
use core::fmt;
4
5
/// A PC-relative offset.
6
///
7
/// This is relative to the start of this offset's containing instruction.
8
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
9
pub struct PcRelOffset(i32);
10
11
#[cfg(feature = "arbitrary")]
12
impl<'a> arbitrary::Arbitrary<'a> for PcRelOffset {
13
fn arbitrary(_u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
14
// We can't possibly choose valid offsets for jumping to, so just use
15
// zero as the least dangerous option. It is up to whoever is generating
16
// arbitrary ops to clean this up.
17
Ok(Self(0))
18
}
19
}
20
21
impl From<i32> for PcRelOffset {
22
#[inline]
23
fn from(offset: i32) -> Self {
24
PcRelOffset(offset)
25
}
26
}
27
28
impl From<PcRelOffset> for i32 {
29
#[inline]
30
fn from(offset: PcRelOffset) -> Self {
31
offset.0
32
}
33
}
34
35
impl fmt::Display for PcRelOffset {
36
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37
i32::from(*self).fmt(f)
38
}
39
}
40
41
/// A 6-byte unsigned integer.
42
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
43
pub struct U6(u8);
44
45
impl U6 {
46
/// Attempts to create a new `U6` from the provided byte
47
pub fn new(val: u8) -> Option<U6> {
48
if val << 2 >> 2 == val {
49
Some(U6(val))
50
} else {
51
None
52
}
53
}
54
}
55
56
#[cfg(feature = "arbitrary")]
57
impl<'a> arbitrary::Arbitrary<'a> for U6 {
58
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
59
let byte = u.arbitrary::<u8>()?;
60
Ok(U6(byte << 2 >> 2))
61
}
62
}
63
64
impl From<U6> for u8 {
65
#[inline]
66
fn from(val: U6) -> Self {
67
val.0
68
}
69
}
70
71
impl fmt::Display for U6 {
72
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73
u8::from(*self).fmt(f)
74
}
75
}
76
77