Skip to content

Commit bc6eb6f

Browse files
committed
move intrinsic to CTFE, add FIXME
1 parent 899a59e commit bc6eb6f

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

compiler/rustc_mir/src/const_eval/machine.rs

+17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_hir::def_id::DefId;
1313
use rustc_middle::mir::AssertMessage;
1414
use rustc_session::Limit;
1515
use rustc_span::symbol::{sym, Symbol};
16+
use rustc_target::abi::{Align, Size};
1617

1718
use crate::interpret::{
1819
self, compile_time_machine, AllocId, Allocation, Frame, GlobalId, ImmTy, InterpCx,
@@ -304,6 +305,22 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
304305
};
305306
ecx.write_scalar(Scalar::from_bool(cmp), dest)?;
306307
}
308+
sym::const_allocate => {
309+
let size = ecx.read_scalar(args[0])?.to_machine_usize(ecx)?;
310+
let align = ecx.read_scalar(args[1])?.to_machine_usize(ecx)?;
311+
312+
let align = match Align::from_bytes(align) {
313+
Ok(a) => a,
314+
Err(err) => throw_ub_format!("align has to be a power of 2, {}", err),
315+
};
316+
317+
let ptr = ecx.memory.allocate(
318+
Size::from_bytes(size as u64),
319+
align,
320+
interpret::MemoryKind::ConstHeap,
321+
);
322+
ecx.write_scalar(Scalar::Ptr(ptr), dest)?;
323+
}
307324
_ => {
308325
return Err(ConstEvalErrKind::NeedsRfc(format!(
309326
"calling intrinsic `{}`",

compiler/rustc_mir/src/interpret/intrinsics.rs

+2-19
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ use rustc_middle::ty;
1414
use rustc_middle::ty::subst::SubstsRef;
1515
use rustc_middle::ty::{Ty, TyCtxt};
1616
use rustc_span::symbol::{sym, Symbol};
17-
use rustc_target::abi::{Abi, Align, LayoutOf as _, Primitive, Size};
17+
use rustc_target::abi::{Abi, LayoutOf as _, Primitive, Size};
1818

1919
use super::{
20-
util::ensure_monomorphic_enough, CheckInAllocMsg, ImmTy, InterpCx, Machine, MemoryKind, OpTy,
21-
PlaceTy,
20+
util::ensure_monomorphic_enough, CheckInAllocMsg, ImmTy, InterpCx, Machine, OpTy, PlaceTy,
2221
};
2322

2423
mod caller_location;
@@ -338,22 +337,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
338337
let result = Scalar::from_uint(truncated_bits, layout.size);
339338
self.write_scalar(result, dest)?;
340339
}
341-
sym::const_allocate => {
342-
let size = self.read_scalar(args[0])?.to_machine_usize(self)?;
343-
let align = self.read_scalar(args[1])?.to_machine_usize(self)?;
344-
345-
let align = match Align::from_bytes(align) {
346-
Ok(a) => a,
347-
Err(err) => throw_ub_format!("align has to be a power of 2, {}", err),
348-
};
349-
350-
let ptr = self.memory.allocate(
351-
Size::from_bytes(size as u64),
352-
align,
353-
MemoryKind::ConstHeap,
354-
);
355-
self.write_scalar(Scalar::Ptr(ptr), dest)?;
356-
}
357340
sym::offset => {
358341
let ptr = self.read_scalar(args[0])?.check_init()?;
359342
let offset_count = self.read_scalar(args[1])?.to_machine_isize(self)?;

compiler/rustc_mir/src/interpret/memory.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub enum MemoryKind<T> {
2828
/// Stack memory. Error if deallocated except during a stack pop.
2929
Stack,
3030
/// Heap memory.
31+
/// FIXME: this variant should be in const_eval
3132
ConstHeap,
3233
/// Memory backing vtables. Error if ever deallocated.
3334
Vtable,

0 commit comments

Comments
 (0)