/
lib.rs
152 lines (132 loc) · 6.08 KB
/
lib.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
#![no_std]
use core::marker::PhantomData;
/// Pseudo-structure that provides the inner type definition
/// and cast functions for every pair of types used
/// in C2Rust's implementation of tied inline assembly operands.
/// For two tied operands of types `In` and `Out`, this
/// implementation provides the smallest type that can
/// hold both operands, along with the casts to convert
/// each operand to this type.
pub struct AsmCast<Out, In>(PhantomData<(Out, In)>);
/// This trait implements the cast functions for the type pair
pub trait AsmCastTrait<Out, In> {
type Type;
fn cast_in(_: &mut Out, x: In) -> Self::Type;
fn cast_out(out: &mut Out, _: In, x: Self::Type);
}
macro_rules! impl_triple {
{<$($param:ident),*> ($out:ty, $in:ty) => $inner:ty} => {
impl<$($param),*> AsmCastTrait<$out, $in> for AsmCast<$out, $in> {
type Type = $inner;
fn cast_in(_: &mut $out, x: $in) -> Self::Type {
x as Self::Type
}
fn cast_out(out: &mut $out, _: $in, x: Self::Type) {
*out = x as $out;
}
}
}
}
macro_rules! impl_triple2 {
{<$($param:ident),*> ($out:ty, $in:ty) => $inner:ty} => {
impl_triple!{<$($param),*> ($out, $in) => $inner}
impl_triple!{<$($param),*> ($in, $out) => $inner}
}
}
macro_rules! impl_pair_higher {
{$inner:ty: [$(<$($param:ident),*> $ty1:ty),*]} => {
impl_triple!{<> ($inner, $inner) => $inner}
$(impl_triple2!{<$($param),*> ($inner, $ty1) => $inner})*
}
}
macro_rules! impl_triple_list {
{$inner:ty: [$($ty1:ty: ($(<$($param:ident),*> $ty2:ty),*)),*]} => {
$($(impl_triple2!{<$($param),*> ($ty1, $ty2) => $inner})*)*
}
}
// Any pair of pointers
impl_triple! {<T, U> (*const T, *const U) => usize}
impl_triple! {<T, U> (*const T, *mut U) => usize}
impl_triple! {<T, U> (*mut T, *const U) => usize}
impl_triple! {<T, U> (*mut T, *mut U) => usize}
impl_pair_higher! {u8: [<>i8]}
impl_pair_higher! {u16: [<>u8, <>i8, <>i16]}
impl_pair_higher! {u32: [<>u8, <>u16, <>i8, <>i16, <>i32]}
impl_pair_higher! {u64: [<>u8, <>u16, <>u32, <>i8, <>i16, <>i32, <>i64, <>usize, <>isize]}
impl_pair_higher! {i8: []}
impl_pair_higher! {i16: [<>u8, <>i8]}
impl_pair_higher! {i32: [<>u8, <>u16, <>i8, <>i16]}
impl_pair_higher! {i64: [<>u8, <>u16, <>u32, <>i8, <>i16, <>i32, <>usize, <>isize]}
impl_pair_higher! {usize: [<>u8, <>u16, <>i8, <>i16, <>isize]}
impl_pair_higher! {isize: [<>u8, <>u16, <>i8, <>i16]}
// Types that are always smaller than a pointer
impl_triple_list! {usize: [u8: (<T>*const T, <T>*mut T),
u16: (<T>*const T, <T>*mut T),
i8: (<T>*const T, <T>*mut T),
i16: (<T>*const T, <T>*mut T),
usize: (<T>*const T, <T>*mut T),
isize: (<T>*const T, <T>*mut T)]
}
// Types that are always larger than a pointer
impl_triple_list! {u64: [u64: (<T>*const T, <T>*mut T)]}
impl_triple_list! {i64: [i64: (<T>*const T, <T>*mut T)]}
// Target-specific types
#[cfg(target_pointer_width = "64")]
impl_triple_list! {u64: [usize: (<>u32, <>i32),
isize: (<>u32, <>i32),
u32: (<T>*const T, <T>*mut T),
i32: (<T>*const T, <T>*mut T)]
}
#[cfg(target_pointer_width = "32")]
impl_triple_list! {u32: [usize: (<>u32, <>i32),
isize: (<>u32, <>i32),
u32: (<T>*const T, <T>*mut T),
i32: (<T>*const T, <T>*mut T)]
}
#[cfg(target_pointer_width = "16")]
impl_triple_list! {u32: [usize: (<>u32, <>i32),
isize: (<>u32, <>i32),
u32: (<T>*const T, <T>*mut T),
i32: (<T>*const T, <T>*mut T)]
}
// FIXME: for now, we assume that we have a 16-, 32- or 64-bit architecture
// we'll need to handle other sizes if we ever encounter any
#[cfg(test)]
mod tests {
/// `#[allow(clippy::zero_ptr)]` is used so `0 as $ty2`
/// can be used for both integer and pointer types.
/// Otherwise for pointer types, `rustc` suggests using `core::ptr::null::<T>()`.
/// That should be needed for such a simple test;
/// it would make the test code a lot more verbose and duplicated.
#[allow(clippy::zero_ptr)]
#[test]
fn test_coverage() {
macro_rules! test_combo {
($ty1:ty, [$($ty2:ty),*]) => {
$({
use super::{AsmCast, AsmCastTrait};
let x = 42usize as $ty1;
let mut y: $ty2 = 0 as $ty2;
let z = AsmCast::cast_in(&mut y, x) + 1;
AsmCast::cast_out(&mut y, x, z);
assert_eq!(y as u64, 43);
})*
}
}
// Test all combinations of types to make sure we cover them
test_combo!(u8, [u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, *const u8, *mut u8]);
test_combo!(u16, [u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, *const u8, *mut u8]);
test_combo!(u32, [u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, *const u8, *mut u8]);
test_combo!(u64, [u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, *const u8, *mut u8]);
test_combo!(usize, [u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, *const u8, *mut u8]);
test_combo!(i8, [u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, *const u8, *mut u8]);
test_combo!(i16, [u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, *const u8, *mut u8]);
test_combo!(i32, [u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, *const u8, *mut u8]);
test_combo!(i64, [u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, *const u8, *mut u8]);
test_combo!(isize, [u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, *const u8, *mut u8]);
test_combo!(*const u8, [u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, *const u8, *mut u8]);
test_combo!(*mut u8, [u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, *const u8, *mut u8]);
test_combo!(*const u16, [u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, *const u8, *mut u8]);
test_combo!(*mut u16, [u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, *const u8, *mut u8]);
}
}