Skip to content

Commit d05784f

Browse files
committed
Address comments
1 parent 8de8580 commit d05784f

File tree

5 files changed

+49
-47
lines changed

5 files changed

+49
-47
lines changed

miri/lib.rs

+3-40
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ mod tls;
3838
use fn_call::EvalContextExt as MissingFnsEvalContextExt;
3939
use operator::EvalContextExt as OperatorEvalContextExt;
4040
use intrinsic::EvalContextExt as IntrinsicEvalContextExt;
41-
use tls::MemoryExt as TlsMemoryExt;
41+
use tls::EvalContextExt as TlsEvalContextExt;
4242

4343
pub fn eval_main<'a, 'tcx: 'a>(
4444
tcx: TyCtxt<'a, 'tcx, 'tcx>,
@@ -111,7 +111,7 @@ pub fn eval_main<'a, 'tcx: 'a>(
111111
}
112112

113113
while ecx.step()? {}
114-
ecx.finish()?;
114+
ecx.run_tls_dtors()?;
115115
if let Some(cleanup_ptr) = cleanup_ptr {
116116
ecx.memory_mut().deallocate(cleanup_ptr, None, Kind::Stack)?;
117117
}
@@ -157,43 +157,6 @@ struct MemoryData<'tcx> {
157157
thread_local: BTreeMap<TlsKey, TlsEntry<'tcx>>,
158158
}
159159

160-
trait EvalContextExt<'tcx> {
161-
fn finish(&mut self) -> EvalResult<'tcx>;
162-
}
163-
164-
impl<'a, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'tcx, Evaluator> {
165-
fn finish(&mut self) -> EvalResult<'tcx> {
166-
let mut dtor = self.memory.fetch_tls_dtor(None)?;
167-
// FIXME: replace loop by some structure that works with stepping
168-
while let Some((instance, ptr, key)) = dtor {
169-
trace!("Running TLS dtor {:?} on {:?}", instance, ptr);
170-
// TODO: Potentially, this has to support all the other possible instances?
171-
// See eval_fn_call in interpret/terminator/mod.rs
172-
let mir = self.load_mir(instance.def)?;
173-
self.push_stack_frame(
174-
instance,
175-
mir.span,
176-
mir,
177-
Lvalue::undef(),
178-
StackPopCleanup::None,
179-
)?;
180-
let arg_local = self.frame().mir.args_iter().next().ok_or(EvalError::AbiViolation("TLS dtor does not take enough arguments.".to_owned()))?;
181-
let dest = self.eval_lvalue(&mir::Lvalue::Local(arg_local))?;
182-
let ty = self.tcx.mk_mut_ptr(self.tcx.types.u8);
183-
self.write_ptr(dest, ptr, ty)?;
184-
185-
// step until out of stackframes
186-
while self.step()? {}
187-
188-
dtor = match self.memory.fetch_tls_dtor(Some(key))? {
189-
dtor @ Some(_) => dtor,
190-
None => self.memory.fetch_tls_dtor(None)?,
191-
};
192-
}
193-
Ok(())
194-
}
195-
}
196-
197160
impl<'tcx> Machine<'tcx> for Evaluator {
198161
type Data = EvaluatorData;
199162
type MemoryData = MemoryData<'tcx>;
@@ -223,7 +186,7 @@ impl<'tcx> Machine<'tcx> for Evaluator {
223186
ecx.call_intrinsic(instance, args, dest, dest_ty, dest_layout, target)
224187
}
225188

226-
fn ptr_op<'a>(
189+
fn try_ptr_op<'a>(
227190
ecx: &rustc_miri::interpret::EvalContext<'a, 'tcx, Self>,
228191
bin_op: mir::BinOp,
229192
left: PrimVal,

miri/tls.rs

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use rustc::ty;
1+
use rustc::{ty, mir};
22

33
use super::{
44
TlsKey, TlsEntry,
55
EvalResult, EvalError,
66
Pointer,
77
Memory,
88
Evaluator,
9+
Lvalue,
10+
StackPopCleanup, EvalContext,
911
};
1012

1113
pub trait MemoryExt<'tcx> {
@@ -16,6 +18,10 @@ pub trait MemoryExt<'tcx> {
1618
fn fetch_tls_dtor(&mut self, key: Option<TlsKey>) -> EvalResult<'tcx, Option<(ty::Instance<'tcx>, Pointer, TlsKey)>>;
1719
}
1820

21+
pub trait EvalContextExt<'tcx> {
22+
fn run_tls_dtors(&mut self) -> EvalResult<'tcx>;
23+
}
24+
1925
impl<'a, 'tcx: 'a> MemoryExt<'tcx> for Memory<'a, 'tcx, Evaluator> {
2026
fn create_tls_key(&mut self, dtor: Option<ty::Instance<'tcx>>) -> TlsKey {
2127
let new_key = self.data.next_thread_local;
@@ -92,3 +98,36 @@ impl<'a, 'tcx: 'a> MemoryExt<'tcx> for Memory<'a, 'tcx, Evaluator> {
9298
return Ok(None);
9399
}
94100
}
101+
102+
impl<'a, 'tcx: 'a> EvalContextExt<'tcx> for EvalContext<'a, 'tcx, Evaluator> {
103+
fn run_tls_dtors(&mut self) -> EvalResult<'tcx> {
104+
let mut dtor = self.memory.fetch_tls_dtor(None)?;
105+
// FIXME: replace loop by some structure that works with stepping
106+
while let Some((instance, ptr, key)) = dtor {
107+
trace!("Running TLS dtor {:?} on {:?}", instance, ptr);
108+
// TODO: Potentially, this has to support all the other possible instances?
109+
// See eval_fn_call in interpret/terminator/mod.rs
110+
let mir = self.load_mir(instance.def)?;
111+
self.push_stack_frame(
112+
instance,
113+
mir.span,
114+
mir,
115+
Lvalue::undef(),
116+
StackPopCleanup::None,
117+
)?;
118+
let arg_local = self.frame().mir.args_iter().next().ok_or(EvalError::AbiViolation("TLS dtor does not take enough arguments.".to_owned()))?;
119+
let dest = self.eval_lvalue(&mir::Lvalue::Local(arg_local))?;
120+
let ty = self.tcx.mk_mut_ptr(self.tcx.types.u8);
121+
self.write_ptr(dest, ptr, ty)?;
122+
123+
// step until out of stackframes
124+
while self.step()? {}
125+
126+
dtor = match self.memory.fetch_tls_dtor(Some(key))? {
127+
dtor @ Some(_) => dtor,
128+
None => self.memory.fetch_tls_dtor(None)?,
129+
};
130+
}
131+
Ok(())
132+
}
133+
}

src/librustc_mir/interpret/const_eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl<'tcx> super::Machine<'tcx> for CompileTimeFunctionEvaluator {
176176
Err(ConstEvalError::NeedsRfc("calling intrinsics".to_string()).into())
177177
}
178178

179-
fn ptr_op<'a>(
179+
fn try_ptr_op<'a>(
180180
_ecx: &EvalContext<'a, 'tcx, Self>,
181181
_bin_op: mir::BinOp,
182182
left: PrimVal,

src/librustc_mir/interpret/machine.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ pub trait Machine<'tcx>: Sized {
5050
target: mir::BasicBlock,
5151
) -> EvalResult<'tcx>;
5252

53-
/// Called when operating on the value of pointers.
53+
/// Called for all binary operations except on float types.
5454
///
5555
/// Returns `None` if the operation should be handled by the integer
56-
/// op code
56+
/// op code in order to share more code between machines
5757
///
58-
/// Returns a (value, overflowed) pair otherwise
59-
fn ptr_op<'a>(
58+
/// Returns a (value, overflowed) pair if the operation succeeded
59+
fn try_ptr_op<'a>(
6060
ecx: &EvalContext<'a, 'tcx, Self>,
6161
bin_op: mir::BinOp,
6262
left: PrimVal,

src/librustc_mir/interpret/operator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
153153

154154
// I: Handle operations that support pointers
155155
if !left_kind.is_float() && !right_kind.is_float() {
156-
if let Some(handled) = M::ptr_op(self, bin_op, left, left_ty, right, right_ty)? {
156+
if let Some(handled) = M::try_ptr_op(self, bin_op, left, left_ty, right, right_ty)? {
157157
return Ok(handled);
158158
}
159159
}

0 commit comments

Comments
 (0)