rustc_public/mir/
alloc.rs1use std::io::Read;
4
5use serde::Serialize;
6
7use crate::mir::mono::{Instance, StaticDef};
8use crate::target::{Endian, MachineInfo};
9use crate::ty::{Allocation, Binder, ExistentialTraitRef, Ty, index_impl};
10use crate::{Error, ThreadLocalIndex, with};
11
12#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
15pub enum GlobalAlloc {
16 Function(Instance),
18 VTable(Ty, Option<Binder<ExistentialTraitRef>>),
21 Static(StaticDef),
24 Memory(Allocation),
26 TypeId { ty: Ty },
29}
30
31impl From<AllocId> for GlobalAlloc {
32 fn from(value: AllocId) -> Self {
33 with(|cx| cx.global_alloc(value))
34 }
35}
36
37impl GlobalAlloc {
38 pub fn vtable_allocation(&self) -> Option<AllocId> {
45 with(|cx| cx.vtable_allocation(self))
46 }
47}
48
49#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
51pub struct AllocId(usize, ThreadLocalIndex);
52index_impl!(AllocId);
53
54pub(crate) fn read_target_uint(mut bytes: &[u8]) -> Result<u128, Error> {
56 let mut buf = [0u8; size_of::<u128>()];
57 match MachineInfo::target_endianness() {
58 Endian::Little => {
59 bytes.read_exact(&mut buf[..bytes.len()])?;
60 Ok(u128::from_le_bytes(buf))
61 }
62 Endian::Big => {
63 bytes.read_exact(&mut buf[16 - bytes.len()..])?;
64 Ok(u128::from_be_bytes(buf))
65 }
66 }
67}
68
69pub(crate) fn read_target_int(mut bytes: &[u8]) -> Result<i128, Error> {
71 let mut buf = [0u8; size_of::<i128>()];
72 match MachineInfo::target_endianness() {
73 Endian::Little => {
74 bytes.read_exact(&mut buf[..bytes.len()])?;
75 Ok(i128::from_le_bytes(buf))
76 }
77 Endian::Big => {
78 bytes.read_exact(&mut buf[16 - bytes.len()..])?;
79 Ok(i128::from_be_bytes(buf))
80 }
81 }
82}