1
Fork 0

make alloc_extra machine hook a bit nicer

This commit is contained in:
Ralf Jung 2019-11-29 19:42:37 +01:00
parent c8743dbe51
commit 96ea142c89
5 changed files with 21 additions and 17 deletions

View file

@ -457,14 +457,14 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
} }
#[inline(always)] #[inline(always)]
fn tag_allocation<'b>( fn init_allocation_extra<'b>(
_memory_extra: &(), _memory_extra: &(),
_id: AllocId, _id: AllocId,
alloc: Cow<'b, Allocation>, alloc: Cow<'b, Allocation>,
_kind: Option<MemoryKind<!>>, _kind: Option<MemoryKind<!>>,
) -> (Cow<'b, Allocation<Self::PointerTag>>, Self::PointerTag) { ) -> Cow<'b, Allocation<Self::PointerTag>> {
// We do not use a tag so we can just cheaply forward the allocation // We do not use a tag so we can just cheaply forward the allocation
(alloc, ()) alloc
} }
#[inline(always)] #[inline(always)]

View file

@ -250,6 +250,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
self.memory.force_bits(scalar, size) self.memory.force_bits(scalar, size)
} }
/// Call this to turn untagged "global" pointers (obtained via `tcx`) into
/// the *canonical* machine pointer to the allocation. This represents a *direct*
/// access to that memory, as opposed to access through a pointer that was created
/// by the program. Must never be used for derived (program-created) pointers!
#[inline(always)] #[inline(always)]
pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer<M::PointerTag> { pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer<M::PointerTag> {
self.memory.tag_static_base_pointer(ptr) self.memory.tag_static_base_pointer(ptr)

View file

@ -240,15 +240,12 @@ pub trait Machine<'mir, 'tcx>: Sized {
/// allocation (because a copy had to be done to add tags or metadata), machine memory will /// allocation (because a copy had to be done to add tags or metadata), machine memory will
/// cache the result. (This relies on `AllocMap::get_or` being able to add the /// cache the result. (This relies on `AllocMap::get_or` being able to add the
/// owned allocation to the map even when the map is shared.) /// owned allocation to the map even when the map is shared.)
/// fn init_allocation_extra<'b>(
/// For static allocations, the tag returned must be the same as the one returned by
/// `tag_static_base_pointer`.
fn tag_allocation<'b>(
memory_extra: &Self::MemoryExtra, memory_extra: &Self::MemoryExtra,
id: AllocId, id: AllocId,
alloc: Cow<'b, Allocation>, alloc: Cow<'b, Allocation>,
kind: Option<MemoryKind<Self::MemoryKinds>>, kind: Option<MemoryKind<Self::MemoryKinds>>,
) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag); ) -> Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>;
/// Return the "base" tag for the given static allocation: the one that is used for direct /// Return the "base" tag for the given static allocation: the one that is used for direct
/// accesses to this static/const/fn allocation. /// accesses to this static/const/fn allocation.

View file

@ -143,6 +143,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
} }
} }
/// Call this to turn untagged "global" pointers (obtained via `tcx`) into
/// the *canonical* machine pointer to the allocation. This represents a *direct*
/// access to that memory, as opposed to access through a pointer that was created
/// by the program. Must never be used for derived (program-created) pointers!
#[inline] #[inline]
pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer<M::PointerTag> { pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer<M::PointerTag> {
ptr.with_tag(M::tag_static_base_pointer(&self.extra, ptr.alloc_id)) ptr.with_tag(M::tag_static_base_pointer(&self.extra, ptr.alloc_id))
@ -191,9 +195,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
kind: MemoryKind<M::MemoryKinds>, kind: MemoryKind<M::MemoryKinds>,
) -> Pointer<M::PointerTag> { ) -> Pointer<M::PointerTag> {
let id = self.tcx.alloc_map.lock().reserve(); let id = self.tcx.alloc_map.lock().reserve();
let (alloc, tag) = M::tag_allocation(&self.extra, id, Cow::Owned(alloc), Some(kind)); let alloc = M::init_allocation_extra(&self.extra, id, Cow::Owned(alloc), Some(kind));
self.alloc_map.insert(id, (kind, alloc.into_owned())); self.alloc_map.insert(id, (kind, alloc.into_owned()));
Pointer::from(id).with_tag(tag) self.tag_static_base_pointer(Pointer::from(id))
} }
pub fn reallocate( pub fn reallocate(
@ -473,14 +477,13 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
} }
} }
}; };
// We got tcx memory. Let the machine figure out whether and how to // We got tcx memory. Let the machine initialize its "extra" stuff.
// turn that into memory with the right pointer tag. Ok(M::init_allocation_extra(
Ok(M::tag_allocation(
memory_extra, memory_extra,
id, // always use the ID we got as input, not the "hidden" one. id, // always use the ID we got as input, not the "hidden" one.
alloc, alloc,
M::STATIC_KIND.map(MemoryKind::Machine), M::STATIC_KIND.map(MemoryKind::Machine),
).0) ))
} }
/// Gives raw access to the `Allocation`, without bounds or alignment checks. /// Gives raw access to the `Allocation`, without bounds or alignment checks.

View file

@ -191,14 +191,14 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
} }
#[inline(always)] #[inline(always)]
fn tag_allocation<'b>( fn init_allocation_extra<'b>(
_memory_extra: &(), _memory_extra: &(),
_id: AllocId, _id: AllocId,
alloc: Cow<'b, Allocation>, alloc: Cow<'b, Allocation>,
_kind: Option<MemoryKind<!>>, _kind: Option<MemoryKind<!>>,
) -> (Cow<'b, Allocation<Self::PointerTag>>, Self::PointerTag) { ) -> Cow<'b, Allocation<Self::PointerTag>> {
// We do not use a tag so we can just cheaply forward the allocation // We do not use a tag so we can just cheaply forward the allocation
(alloc, ()) alloc
} }
#[inline(always)] #[inline(always)]