forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixed.rs
More file actions
69 lines (61 loc) · 1.8 KB
/
fixed.rs
File metadata and controls
69 lines (61 loc) · 1.8 KB
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
//! Operands with fixed register encodings.
use crate::{AsReg, Size};
use alloc::string::String;
/// A _fixed_ register.
///
/// Some operands are implicit to the instruction and thus use a fixed register
/// for execution. Because this assembler is generic over any register type
/// (`R`), this wrapper provides a way to record the fixed register encoding we
/// expect to use (`E`).
///
/// ```
/// # use cranelift_assembler_x64::{AsReg, Fixed, gpr};
/// # let valid_reg = 0;
/// let fixed = Fixed::<u8, { gpr::enc::RAX }>(valid_reg);
/// assert_eq!(fixed.enc(), gpr::enc::RAX);
/// ```
///
/// ```should_panic
/// # use cranelift_assembler_x64::{AsReg, Fixed, gpr};
/// # let invalid_reg = 42;
/// let fixed = Fixed::<u8, { gpr::enc::RAX }>(invalid_reg);
/// fixed.enc(); // Will panic because `invalid_reg` does not match `RAX`.
/// ```
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Fixed<R, const E: u8>(pub R);
impl<R, const E: u8> Fixed<R, E> {
/// Return the fixed register encoding.
///
/// Regardless of what `R` is (e.g., pre-register allocation), we want to be
/// able to know what this register should encode as.
pub fn expected_enc(&self) -> u8 {
E
}
/// Return the register name at the given `size`.
pub fn to_string(&self, size: Option<Size>) -> String
where
R: AsReg,
{
self.0.to_string(size)
}
}
impl<R: AsReg, const E: u8> AsReg for Fixed<R, E> {
fn new(reg: u8) -> Self {
assert!(reg == E);
Self(R::new(reg))
}
fn enc(&self) -> u8 {
assert!(self.0.enc() == E);
self.0.enc()
}
}
impl<R, const E: u8> AsRef<R> for Fixed<R, E> {
fn as_ref(&self) -> &R {
&self.0
}
}
impl<R, const E: u8> From<R> for Fixed<R, E> {
fn from(reg: R) -> Fixed<R, E> {
Fixed(reg)
}
}