Skip to content

Commit 10af387

Browse files
committed
Auto merge of #814 - RalfJung:shims, r=RalfJung
move find_fn (which is not specific to foreign items) out of foreign_item
2 parents d2df509 + 7b702b9 commit 10af387

File tree

3 files changed

+48
-41
lines changed

3 files changed

+48
-41
lines changed

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub use rustc_mir::interpret::*;
2828
// Resolve ambiguity.
2929
pub use rustc_mir::interpret::{self, AllocMap, PlaceTy};
3030

31+
pub use crate::shims::{EvalContextExt as ShimsEvalContextExt};
3132
pub use crate::shims::foreign_items::EvalContextExt as ForeignItemsEvalContextExt;
3233
pub use crate::shims::intrinsics::EvalContextExt as IntrinsicsEvalContextExt;
3334
pub use crate::operator::EvalContextExt as OperatorEvalContextExt;

src/shims/foreign_items.rs

-41
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use rustc::ty;
21
use rustc::ty::layout::{Align, LayoutOf, Size};
32
use rustc::hir::def_id::DefId;
43
use rustc::mir;
@@ -11,46 +10,6 @@ use crate::*;
1110

1211
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
1312
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
14-
fn find_fn(
15-
&mut self,
16-
instance: ty::Instance<'tcx>,
17-
args: &[OpTy<'tcx, Tag>],
18-
dest: Option<PlaceTy<'tcx, Tag>>,
19-
ret: Option<mir::BasicBlock>,
20-
) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
21-
let this = self.eval_context_mut();
22-
trace!("eval_fn_call: {:#?}, {:?}", instance, dest.map(|place| *place));
23-
24-
// First, run the common hooks also supported by CTFE.
25-
if this.hook_fn(instance, args, dest)? {
26-
this.goto_block(ret)?;
27-
return Ok(None);
28-
}
29-
// There are some more lang items we want to hook that CTFE does not hook (yet).
30-
if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) {
31-
// FIXME: return a real value in case the target allocation has an
32-
// alignment bigger than the one requested.
33-
let n = u128::max_value();
34-
let dest = dest.unwrap();
35-
let n = this.truncate(n, dest.layout);
36-
this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?;
37-
this.goto_block(ret)?;
38-
return Ok(None);
39-
}
40-
41-
// Try to see if we can do something about foreign items.
42-
if this.tcx.is_foreign_item(instance.def_id()) {
43-
// An external function that we cannot find MIR for, but we can still run enough
44-
// of them to make miri viable.
45-
this.emulate_foreign_item(instance.def_id(), args, dest, ret)?;
46-
// `goto_block` already handled.
47-
return Ok(None);
48-
}
49-
50-
// Otherwise, load the MIR.
51-
Ok(Some(this.load_mir(instance.def)?))
52-
}
53-
5413
/// Returns the minimum alignment for the target architecture.
5514
fn min_align(&self) -> Align {
5615
let this = self.eval_context_ref();

src/shims/mod.rs

+47
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,49 @@
11
pub mod foreign_items;
22
pub mod intrinsics;
3+
4+
use rustc::{ty, mir};
5+
6+
use crate::*;
7+
8+
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
9+
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
10+
fn find_fn(
11+
&mut self,
12+
instance: ty::Instance<'tcx>,
13+
args: &[OpTy<'tcx, Tag>],
14+
dest: Option<PlaceTy<'tcx, Tag>>,
15+
ret: Option<mir::BasicBlock>,
16+
) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
17+
let this = self.eval_context_mut();
18+
trace!("eval_fn_call: {:#?}, {:?}", instance, dest.map(|place| *place));
19+
20+
// First, run the common hooks also supported by CTFE.
21+
if this.hook_fn(instance, args, dest)? {
22+
this.goto_block(ret)?;
23+
return Ok(None);
24+
}
25+
// There are some more lang items we want to hook that CTFE does not hook (yet).
26+
if this.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) {
27+
// FIXME: return a real value in case the target allocation has an
28+
// alignment bigger than the one requested.
29+
let n = u128::max_value();
30+
let dest = dest.unwrap();
31+
let n = this.truncate(n, dest.layout);
32+
this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?;
33+
this.goto_block(ret)?;
34+
return Ok(None);
35+
}
36+
37+
// Try to see if we can do something about foreign items.
38+
if this.tcx.is_foreign_item(instance.def_id()) {
39+
// An external function that we cannot find MIR for, but we can still run enough
40+
// of them to make miri viable.
41+
this.emulate_foreign_item(instance.def_id(), args, dest, ret)?;
42+
// `goto_block` already handled.
43+
return Ok(None);
44+
}
45+
46+
// Otherwise, load the MIR.
47+
Ok(Some(this.load_mir(instance.def)?))
48+
}
49+
}

0 commit comments

Comments
 (0)