stable_mir/
target.rs

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