rustc_smir/stable_mir/mir/
alloc.rs
1use std::io::Read;
4
5use serde::Serialize;
6use stable_mir::mir::mono::{Instance, StaticDef};
7use stable_mir::target::{Endian, MachineInfo};
8use stable_mir::ty::{Allocation, Binder, ExistentialTraitRef, IndexedVal, Ty};
9use stable_mir::{Error, with};
10
11use crate::stable_mir;
12
13#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
16pub enum GlobalAlloc {
17 Function(Instance),
19 VTable(Ty, Option<Binder<ExistentialTraitRef>>),
22 Static(StaticDef),
25 Memory(Allocation),
27}
28
29impl From<AllocId> for GlobalAlloc {
30 fn from(value: AllocId) -> Self {
31 with(|cx| cx.global_alloc(value))
32 }
33}
34
35impl GlobalAlloc {
36 pub fn vtable_allocation(&self) -> Option<AllocId> {
43 with(|cx| cx.vtable_allocation(self))
44 }
45}
46
47#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, Serialize)]
49pub struct AllocId(usize);
50
51impl IndexedVal for AllocId {
52 fn to_val(index: usize) -> Self {
53 AllocId(index)
54 }
55 fn to_index(&self) -> usize {
56 self.0
57 }
58}
59
60pub(crate) fn read_target_uint(mut bytes: &[u8]) -> Result<u128, Error> {
62 let mut buf = [0u8; size_of::<u128>()];
63 match MachineInfo::target_endianness() {
64 Endian::Little => {
65 bytes.read_exact(&mut buf[..bytes.len()])?;
66 Ok(u128::from_le_bytes(buf))
67 }
68 Endian::Big => {
69 bytes.read_exact(&mut buf[16 - bytes.len()..])?;
70 Ok(u128::from_be_bytes(buf))
71 }
72 }
73}
74
75pub(crate) fn read_target_int(mut bytes: &[u8]) -> Result<i128, Error> {
77 let mut buf = [0u8; size_of::<i128>()];
78 match MachineInfo::target_endianness() {
79 Endian::Little => {
80 bytes.read_exact(&mut buf[..bytes.len()])?;
81 Ok(i128::from_le_bytes(buf))
82 }
83 Endian::Big => {
84 bytes.read_exact(&mut buf[16 - bytes.len()..])?;
85 Ok(i128::from_be_bytes(buf))
86 }
87 }
88}