rustc_smir/stable_mir/
target.rs

1//! Provide information about the machine that this is being compiled into.
2
3use serde::Serialize;
4use stable_mir::compiler_interface::with;
5
6use crate::stable_mir;
7
8/// The properties of the target machine being compiled into.
9#[derive(Clone, PartialEq, Eq, Serialize)]
10pub struct MachineInfo {
11    pub endian: Endian,
12    pub pointer_width: MachineSize,
13}
14
15impl MachineInfo {
16    pub fn target() -> MachineInfo {
17        with(|cx| cx.target_info())
18    }
19
20    pub fn target_endianness() -> Endian {
21        with(|cx| cx.target_info().endian)
22    }
23
24    pub fn target_pointer_width() -> MachineSize {
25        with(|cx| cx.target_info().pointer_width)
26    }
27}
28
29#[derive(Copy, Clone, PartialEq, Eq, Serialize)]
30pub enum Endian {
31    Little,
32    Big,
33}
34
35/// Represent the size of a component.
36#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Serialize)]
37pub struct MachineSize {
38    num_bits: usize,
39}
40
41impl MachineSize {
42    #[inline(always)]
43    pub fn bytes(self) -> usize {
44        self.num_bits / 8
45    }
46
47    #[inline(always)]
48    pub fn bits(self) -> usize {
49        self.num_bits
50    }
51
52    #[inline(always)]
53    pub fn from_bits(num_bits: usize) -> MachineSize {
54        MachineSize { num_bits }
55    }
56
57    #[inline]
58    pub fn unsigned_int_max(self) -> Option<u128> {
59        (self.num_bits <= 128).then(|| u128::MAX >> (128 - self.bits()))
60    }
61}