2 use rustc::middle::const_val;
3 use rustc::hir::def_id::DefId;
4 use rustc::mir::mir_map::MirMap;
5 use rustc::mir::repr as mir;
6 use rustc::traits::{self, ProjectionMode};
7 use rustc::ty::fold::TypeFoldable;
8 use rustc::ty::layout::{self, Layout, Size};
9 use rustc::ty::subst::{self, Subst, Substs};
10 use rustc::ty::{self, TyCtxt};
11 use rustc::util::nodemap::DefIdMap;
12 use std::cell::RefCell;
18 use syntax::codemap::{self, DUMMY_SP};
20 use error::{EvalError, EvalResult};
21 use memory::{Memory, Pointer};
22 use primval::{self, PrimVal};
24 const TRACE_EXECUTION: bool = false;
26 struct Interpreter<'a, 'tcx: 'a> {
27 /// The results of the type checker, from rustc.
28 tcx: &'a TyCtxt<'tcx>,
30 /// A mapping from NodeIds to Mir, from rustc. Only contains MIR for crate-local items.
31 mir_map: &'a MirMap<'tcx>,
33 /// A local cache from DefIds to Mir for non-crate-local items.
34 mir_cache: RefCell<DefIdMap<Rc<mir::Mir<'tcx>>>>,
36 /// The virtual memory system.
39 /// The virtual call stack.
40 stack: Vec<Frame<'a, 'tcx>>,
42 /// Another stack containing the type substitutions for the current function invocation. It
43 /// exists separately from `stack` because it must contain the `Substs` for a function while
44 /// *creating* the `Frame` for that same function.
45 substs_stack: Vec<&'tcx Substs<'tcx>>,
47 // TODO(tsion): Merge with `substs_stack`. Also try restructuring `Frame` to accomodate.
48 /// A stack of the things necessary to print good strack traces:
49 /// * Function DefIds and Substs to print proper substituted function names.
50 /// * Spans pointing to specific function calls in the source.
51 name_stack: Vec<(DefId, &'tcx Substs<'tcx>, codemap::Span)>,
55 struct Frame<'a, 'tcx: 'a> {
56 /// The MIR for the function called on this frame.
57 mir: CachedMir<'a, 'tcx>,
59 /// The block this frame will execute when a function call returns back to this frame.
60 next_block: mir::BasicBlock,
62 /// A pointer for writing the return value of the current call if it's not a diverging call.
63 return_ptr: Option<Pointer>,
65 /// The list of locals for the current function, stored in order as
66 /// `[arguments..., variables..., temporaries...]`. The variables begin at `self.var_offset`
67 /// and the temporaries at `self.temp_offset`.
70 /// The offset of the first variable in `self.locals`.
73 /// The offset of the first temporary in `self.locals`.
77 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
83 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
87 // Vtable(memory::AllocId),
91 enum CachedMir<'mir, 'tcx: 'mir> {
92 Ref(&'mir mir::Mir<'tcx>),
93 Owned(Rc<mir::Mir<'tcx>>)
96 /// Represents the action to be taken in the main loop as a result of executing a terminator.
97 enum TerminatorTarget {
98 /// Make a local jump to the given block.
99 Block(mir::BasicBlock),
101 /// Start executing from the new current frame. (For function calls.)
104 /// Stop executing the current frame and resume the previous frame.
108 impl<'a, 'tcx: 'a> Interpreter<'a, 'tcx> {
109 fn new(tcx: &'a TyCtxt<'tcx>, mir_map: &'a MirMap<'tcx>) -> Self {
113 mir_cache: RefCell::new(DefIdMap()),
114 memory: Memory::new(),
116 substs_stack: Vec::new(),
117 name_stack: Vec::new(),
121 fn maybe_report<T>(&self, span: codemap::Span, r: EvalResult<T>) -> EvalResult<T> {
122 if let Err(ref e) = r {
123 let mut err = self.tcx.sess.struct_span_err(span, &e.to_string());
124 for &(def_id, substs, span) in self.name_stack.iter().rev() {
125 // FIXME(tsion): Find a way to do this without this Display impl hack.
126 use rustc::util::ppaux;
128 struct Instance<'tcx>(DefId, &'tcx Substs<'tcx>);
129 impl<'tcx> fmt::Display for Instance<'tcx> {
130 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
131 ppaux::parameterized(f, self.1, self.0, ppaux::Ns::Value, &[],
132 |tcx| tcx.lookup_item_type(self.0).generics)
135 err.span_note(span, &format!("inside call to {}", Instance(def_id, substs)));
142 fn log<F>(&self, extra_indent: usize, f: F) where F: FnOnce() {
143 let indent = self.stack.len() + extra_indent;
144 if !TRACE_EXECUTION { return; }
145 for _ in 0..indent { print!(" "); }
150 fn run(&mut self) -> EvalResult<()> {
151 'outer: while !self.stack.is_empty() {
152 let mut current_block = self.frame().next_block;
155 self.log(0, || print!("// {:?}", current_block));
156 let current_mir = self.mir().clone(); // Cloning a reference.
157 let block_data = current_mir.basic_block_data(current_block);
159 for stmt in &block_data.statements {
160 self.log(0, || print!("{:?}", stmt));
161 let mir::StatementKind::Assign(ref lvalue, ref rvalue) = stmt.kind;
162 let result = self.eval_assignment(lvalue, rvalue);
163 try!(self.maybe_report(stmt.span, result));
166 let terminator = block_data.terminator();
167 self.log(0, || print!("{:?}", terminator.kind));
169 let result = self.eval_terminator(terminator);
170 match try!(self.maybe_report(terminator.span, result)) {
171 TerminatorTarget::Block(block) => current_block = block,
172 TerminatorTarget::Return => {
173 self.pop_stack_frame();
174 self.name_stack.pop();
177 TerminatorTarget::Call => continue 'outer,
185 fn push_stack_frame(&mut self, mir: CachedMir<'a, 'tcx>, substs: &'tcx Substs<'tcx>,
186 return_ptr: Option<Pointer>)
188 self.substs_stack.push(substs);
190 let arg_tys = mir.arg_decls.iter().map(|a| a.ty);
191 let var_tys = mir.var_decls.iter().map(|v| v.ty);
192 let temp_tys = mir.temp_decls.iter().map(|t| t.ty);
194 let locals: Vec<Pointer> = arg_tys.chain(var_tys).chain(temp_tys).map(|ty| {
195 let size = self.type_size(ty);
196 self.memory.allocate(size)
199 let num_args = mir.arg_decls.len();
200 let num_vars = mir.var_decls.len();
202 self.stack.push(Frame {
204 next_block: mir::START_BLOCK,
205 return_ptr: return_ptr,
207 var_offset: num_args,
208 temp_offset: num_args + num_vars,
212 fn pop_stack_frame(&mut self) {
213 let _frame = self.stack.pop().expect("tried to pop a stack frame, but there were none");
214 // TODO(tsion): Deallocate local variables.
215 self.substs_stack.pop();
218 fn eval_terminator(&mut self, terminator: &mir::Terminator<'tcx>)
219 -> EvalResult<TerminatorTarget> {
220 use rustc::mir::repr::TerminatorKind::*;
221 let target = match terminator.kind {
222 Return => TerminatorTarget::Return,
224 Goto { target } => TerminatorTarget::Block(target),
226 If { ref cond, targets: (then_target, else_target) } => {
227 let cond_ptr = try!(self.eval_operand(cond));
228 let cond_val = try!(self.memory.read_bool(cond_ptr));
229 TerminatorTarget::Block(if cond_val { then_target } else { else_target })
232 SwitchInt { ref discr, ref values, ref targets, .. } => {
233 let discr_ptr = try!(self.eval_lvalue(discr)).to_ptr();
235 self.lvalue_layout(discr).size(&self.tcx.data_layout).bytes() as usize;
236 let discr_val = try!(self.memory.read_uint(discr_ptr, discr_size));
238 // Branch to the `otherwise` case by default, if no match is found.
239 let mut target_block = targets[targets.len() - 1];
241 for (index, val_const) in values.iter().enumerate() {
242 let ptr = try!(self.const_to_ptr(val_const));
243 let val = try!(self.memory.read_uint(ptr, discr_size));
244 if discr_val == val {
245 target_block = targets[index];
250 TerminatorTarget::Block(target_block)
253 Switch { ref discr, ref targets, adt_def } => {
254 let adt_ptr = try!(self.eval_lvalue(discr)).to_ptr();
255 let adt_layout = self.lvalue_layout(discr);
256 let discr_size = match *adt_layout {
257 Layout::General { discr, .. } => discr.size().bytes(),
258 _ => panic!("attmpted to switch on non-aggregate type"),
260 let discr_val = try!(self.memory.read_uint(adt_ptr, discr_size as usize));
262 let matching = adt_def.variants.iter()
263 .position(|v| discr_val == v.disr_val.to_u64_unchecked());
266 Some(i) => TerminatorTarget::Block(targets[i]),
267 None => return Err(EvalError::InvalidDiscriminant),
271 Call { ref func, ref args, ref destination, .. } => {
272 let mut return_ptr = None;
273 if let Some((ref lv, target)) = *destination {
274 self.frame_mut().next_block = target;
275 return_ptr = Some(try!(self.eval_lvalue(lv)).to_ptr());
278 let func_ty = self.operand_ty(func);
280 ty::TyFnDef(def_id, substs, fn_ty) => {
281 use syntax::abi::Abi;
283 Abi::RustIntrinsic => {
284 let name = self.tcx.item_name(def_id).as_str();
285 match fn_ty.sig.0.output {
286 ty::FnConverging(ty) => {
287 let size = self.type_size(ty);
288 try!(self.call_intrinsic(&name, substs, args,
289 return_ptr.unwrap(), size))
291 ty::FnDiverging => unimplemented!(),
296 try!(self.call_c_abi(def_id, args, return_ptr.unwrap())),
298 Abi::Rust | Abi::RustCall => {
299 // TODO(tsion): Adjust the first argument when calling a Fn or
300 // FnMut closure via FnOnce::call_once.
302 // Only trait methods can have a Self parameter.
303 let (resolved_def_id, resolved_substs) = if substs.self_ty().is_some() {
304 self.trait_method(def_id, substs)
309 let mut arg_srcs = Vec::new();
311 let src = try!(self.eval_operand(arg));
312 let src_ty = self.operand_ty(arg);
313 arg_srcs.push((src, src_ty));
316 if fn_ty.abi == Abi::RustCall && !args.is_empty() {
318 let last_arg = args.last().unwrap();
319 let last = try!(self.eval_operand(last_arg));
320 let last_ty = self.operand_ty(last_arg);
321 let last_layout = self.type_layout(last_ty);
322 match (&last_ty.sty, last_layout) {
323 (&ty::TyTuple(ref fields),
324 &Layout::Univariant { ref variant, .. }) => {
325 let offsets = iter::once(0)
326 .chain(variant.offset_after_field.iter()
327 .map(|s| s.bytes()));
328 for (offset, ty) in offsets.zip(fields) {
329 let src = last.offset(offset as isize);
330 arg_srcs.push((src, ty));
333 ty => panic!("expected tuple as last argument in function with 'rust-call' ABI, got {:?}", ty),
337 let mir = self.load_mir(resolved_def_id);
338 self.name_stack.push((def_id, substs, terminator.span));
339 self.push_stack_frame(mir, resolved_substs, return_ptr);
341 for (i, (src, src_ty)) in arg_srcs.into_iter().enumerate() {
342 let dest = self.frame().locals[i];
343 try!(self.move_(src, dest, src_ty));
346 TerminatorTarget::Call
349 abi => panic!("can't handle function with {:?} ABI", abi),
353 _ => panic!("can't handle callee of type {:?}", func_ty),
357 Drop { ref value, target, .. } => {
358 let ptr = try!(self.eval_lvalue(value)).to_ptr();
359 let ty = self.lvalue_ty(value);
360 try!(self.drop(ptr, ty));
361 TerminatorTarget::Block(target)
364 Resume => unimplemented!(),
370 fn drop(&mut self, ptr: Pointer, ty: ty::Ty<'tcx>) -> EvalResult<()> {
371 if !self.type_needs_drop(ty) {
372 self.log(1, || print!("no need to drop {:?}", ty));
375 self.log(1, || print!("need to drop {:?}", ty));
377 // TODO(tsion): Call user-defined Drop::drop impls.
380 ty::TyBox(contents_ty) => {
381 match self.memory.read_ptr(ptr) {
382 Ok(contents_ptr) => {
383 try!(self.drop(contents_ptr, contents_ty));
384 self.log(1, || print!("deallocating box"));
385 try!(self.memory.deallocate(contents_ptr));
387 Err(EvalError::ReadBytesAsPointer) => {
388 let size = self.memory.pointer_size;
389 let possible_drop_fill = try!(self.memory.read_bytes(ptr, size));
390 if possible_drop_fill.iter().all(|&b| b == mem::POST_DROP_U8) {
393 return Err(EvalError::ReadBytesAsPointer);
396 Err(e) => return Err(e),
400 // TODO(tsion): Implement drop for other relevant types (e.g. aggregates).
405 // FIXME(tsion): Trait objects (with no static size) probably get filled, too.
406 let size = self.type_size(ty);
407 try!(self.memory.drop_fill(ptr, size));
415 substs: &'tcx Substs<'tcx>,
416 args: &[mir::Operand<'tcx>],
419 ) -> EvalResult<TerminatorTarget> {
420 let args_res: EvalResult<Vec<Pointer>> = args.iter()
421 .map(|arg| self.eval_operand(arg))
423 let args = try!(args_res);
428 "copy_nonoverlapping" => {
429 let elem_ty = *substs.types.get(subst::FnSpace, 0);
430 let elem_size = self.type_size(elem_ty);
431 let src = try!(self.memory.read_ptr(args[0]));
432 let dest = try!(self.memory.read_ptr(args[1]));
433 let count = try!(self.memory.read_isize(args[2]));
434 try!(self.memory.copy(src, dest, count as usize * elem_size));
438 let arg_ty = *substs.types.get(subst::FnSpace, 0);
439 let arg_size = self.type_size(arg_ty);
440 try!(self.memory.drop_fill(args[0], arg_size));
443 "init" => try!(self.memory.write_repeat(dest, 0, dest_size)),
446 try!(self.memory.write_int(dest, 1, dest_size));
450 let ty = *substs.types.get(subst::FnSpace, 0);
451 let ptr = try!(self.memory.read_ptr(args[0]));
452 try!(self.move_(args[1], ptr, ty));
455 // FIXME(tsion): Handle different integer types correctly.
456 "add_with_overflow" => {
457 let ty = *substs.types.get(subst::FnSpace, 0);
458 let size = self.type_size(ty);
459 let left = try!(self.memory.read_int(args[0], size));
460 let right = try!(self.memory.read_int(args[1], size));
461 let (n, overflowed) = unsafe {
462 ::std::intrinsics::add_with_overflow::<i64>(left, right)
464 try!(self.memory.write_int(dest, n, size));
465 try!(self.memory.write_bool(dest.offset(size as isize), overflowed));
468 // FIXME(tsion): Handle different integer types correctly.
469 "mul_with_overflow" => {
470 let ty = *substs.types.get(subst::FnSpace, 0);
471 let size = self.type_size(ty);
472 let left = try!(self.memory.read_int(args[0], size));
473 let right = try!(self.memory.read_int(args[1], size));
474 let (n, overflowed) = unsafe {
475 ::std::intrinsics::mul_with_overflow::<i64>(left, right)
477 try!(self.memory.write_int(dest, n, size));
478 try!(self.memory.write_bool(dest.offset(size as isize), overflowed));
482 let pointee_ty = *substs.types.get(subst::FnSpace, 0);
483 let pointee_size = self.type_size(pointee_ty) as isize;
484 let ptr_arg = args[0];
485 let offset = try!(self.memory.read_isize(args[1]));
487 match self.memory.read_ptr(ptr_arg) {
489 let result_ptr = ptr.offset(offset as isize * pointee_size);
490 try!(self.memory.write_ptr(dest, result_ptr));
492 Err(EvalError::ReadBytesAsPointer) => {
493 let addr = try!(self.memory.read_isize(ptr_arg));
494 let result_addr = addr + offset * pointee_size as i64;
495 try!(self.memory.write_isize(dest, result_addr));
497 Err(e) => return Err(e),
501 // FIXME(tsion): Handle different integer types correctly. Use primvals?
502 "overflowing_sub" => {
503 let ty = *substs.types.get(subst::FnSpace, 0);
504 let size = self.type_size(ty);
505 let left = try!(self.memory.read_int(args[0], size));
506 let right = try!(self.memory.read_int(args[1], size));
507 let n = left.wrapping_sub(right);
508 try!(self.memory.write_int(dest, n, size));
512 let ty = *substs.types.get(subst::FnSpace, 0);
513 let size = self.type_size(ty) as u64;
514 try!(self.memory.write_uint(dest, size, dest_size));
518 let ty = *substs.types.get(subst::FnSpace, 0);
519 try!(self.move_(args[0], dest, ty));
521 "uninit" => try!(self.memory.mark_definedness(dest, dest_size, false)),
523 name => panic!("can't handle intrinsic: {}", name),
526 // Since we pushed no stack frame, the main loop will act
527 // as if the call just completed and it's returning to the
529 Ok(TerminatorTarget::Call)
535 args: &[mir::Operand<'tcx>],
537 ) -> EvalResult<TerminatorTarget> {
538 let name = self.tcx.item_name(def_id);
539 let attrs = self.tcx.get_attrs(def_id);
540 let link_name = match attr::first_attr_value_str_by_name(&attrs, "link_name") {
541 Some(ln) => ln.clone(),
542 None => name.as_str(),
545 let args_res: EvalResult<Vec<Pointer>> = args.iter()
546 .map(|arg| self.eval_operand(arg))
548 let args = try!(args_res);
550 match &link_name[..] {
551 "__rust_allocate" => {
552 let size = try!(self.memory.read_usize(args[0]));
553 let ptr = self.memory.allocate(size as usize);
554 try!(self.memory.write_ptr(dest, ptr));
557 "__rust_reallocate" => {
558 let ptr = try!(self.memory.read_ptr(args[0]));
559 let size = try!(self.memory.read_usize(args[2]));
560 try!(self.memory.reallocate(ptr, size as usize));
561 try!(self.memory.write_ptr(dest, ptr));
564 _ => panic!("can't call C ABI function: {}", link_name),
567 // Since we pushed no stack frame, the main loop will act
568 // as if the call just completed and it's returning to the
570 Ok(TerminatorTarget::Call)
573 fn assign_fields<I: IntoIterator<Item = u64>>(
577 operands: &[mir::Operand<'tcx>],
578 ) -> EvalResult<()> {
579 for (offset, operand) in offsets.into_iter().zip(operands) {
580 let src = try!(self.eval_operand(operand));
581 let src_ty = self.operand_ty(operand);
582 let field_dest = dest.offset(offset as isize);
583 try!(self.move_(src, field_dest, src_ty));
588 fn eval_assignment(&mut self, lvalue: &mir::Lvalue<'tcx>, rvalue: &mir::Rvalue<'tcx>)
591 let dest = try!(self.eval_lvalue(lvalue)).to_ptr();
592 let dest_ty = self.lvalue_ty(lvalue);
593 let dest_layout = self.lvalue_layout(lvalue);
595 use rustc::mir::repr::Rvalue::*;
597 Use(ref operand) => {
598 let src = try!(self.eval_operand(operand));
599 try!(self.move_(src, dest, dest_ty));
602 BinaryOp(bin_op, ref left, ref right) => {
603 let left_ptr = try!(self.eval_operand(left));
604 let left_ty = self.operand_ty(left);
605 let left_val = try!(self.read_primval(left_ptr, left_ty));
607 let right_ptr = try!(self.eval_operand(right));
608 let right_ty = self.operand_ty(right);
609 let right_val = try!(self.read_primval(right_ptr, right_ty));
611 let val = try!(primval::binary_op(bin_op, left_val, right_val));
612 try!(self.memory.write_primval(dest, val));
615 UnaryOp(un_op, ref operand) => {
616 let ptr = try!(self.eval_operand(operand));
617 let ty = self.operand_ty(operand);
618 let val = try!(self.read_primval(ptr, ty));
619 try!(self.memory.write_primval(dest, primval::unary_op(un_op, val)));
622 Aggregate(ref kind, ref operands) => {
624 Layout::Univariant { ref variant, .. } => {
625 let offsets = iter::once(0)
626 .chain(variant.offset_after_field.iter().map(|s| s.bytes()));
627 try!(self.assign_fields(dest, offsets, operands));
630 Layout::Array { .. } => {
631 let elem_size = match dest_ty.sty {
632 ty::TyArray(elem_ty, _) => self.type_size(elem_ty) as u64,
633 _ => panic!("tried to assign {:?} aggregate to non-array type {:?}",
636 let offsets = (0..).map(|i| i * elem_size);
637 try!(self.assign_fields(dest, offsets, operands));
640 Layout::General { discr, ref variants, .. } => {
641 if let mir::AggregateKind::Adt(adt_def, variant, _) = *kind {
642 let discr_val = adt_def.variants[variant].disr_val.to_u64_unchecked();
643 let discr_size = discr.size().bytes() as usize;
644 try!(self.memory.write_uint(dest, discr_val, discr_size));
646 let offsets = variants[variant].offset_after_field.iter()
648 try!(self.assign_fields(dest, offsets, operands));
650 panic!("tried to assign {:?} aggregate to Layout::General dest", kind);
654 _ => panic!("can't handle destination layout {:?} when assigning {:?}",
659 Repeat(ref operand, _) => {
660 let (elem_size, length) = match dest_ty.sty {
661 ty::TyArray(elem_ty, n) => (self.type_size(elem_ty), n),
662 _ => panic!("tried to assign array-repeat to non-array type {:?}", dest_ty),
665 let src = try!(self.eval_operand(operand));
667 let elem_dest = dest.offset((i * elem_size) as isize);
668 try!(self.memory.copy(src, elem_dest, elem_size));
673 let src = try!(self.eval_lvalue(lvalue));
674 let ty = self.lvalue_ty(lvalue);
675 let len = match ty.sty {
676 ty::TyArray(_, n) => n as u64,
677 ty::TySlice(_) => if let LvalueExtra::Length(n) = src.extra {
680 panic!("Rvalue::Len of a slice given non-slice pointer: {:?}", src);
682 _ => panic!("Rvalue::Len expected array or slice, got {:?}", ty),
684 try!(self.memory.write_usize(dest, len));
687 Ref(_, _, ref lvalue) => {
688 let lv = try!(self.eval_lvalue(lvalue));
689 try!(self.memory.write_ptr(dest, lv.ptr));
691 LvalueExtra::None => {},
692 LvalueExtra::Length(len) => {
693 let len_ptr = dest.offset(self.memory.pointer_size as isize);
694 try!(self.memory.write_usize(len_ptr, len));
700 let size = self.type_size(ty);
701 let ptr = self.memory.allocate(size);
702 try!(self.memory.write_ptr(dest, ptr));
705 Cast(kind, ref operand, dest_ty) => {
706 let src = try!(self.eval_operand(operand));
707 let src_ty = self.operand_ty(operand);
709 use rustc::mir::repr::CastKind::*;
712 try!(self.move_(src, dest, src_ty));
713 let src_pointee_ty = pointee_type(src_ty).unwrap();
714 let dest_pointee_ty = pointee_type(dest_ty).unwrap();
716 match (&src_pointee_ty.sty, &dest_pointee_ty.sty) {
717 (&ty::TyArray(_, length), &ty::TySlice(_)) => {
718 let len_ptr = dest.offset(self.memory.pointer_size as isize);
719 try!(self.memory.write_usize(len_ptr, length as u64));
722 _ => panic!("can't handle cast: {:?}", rvalue),
727 // FIXME(tsion): Wrong for almost everything.
728 let size = dest_layout.size(&self.tcx.data_layout).bytes() as usize;
729 try!(self.memory.copy(src, dest, size));
732 _ => panic!("can't handle cast: {:?}", rvalue),
736 Slice { .. } => unimplemented!(),
737 InlineAsm { .. } => unimplemented!(),
743 fn eval_operand(&mut self, op: &mir::Operand<'tcx>) -> EvalResult<Pointer> {
744 self.eval_operand_and_layout(op).map(|(p, _)| p)
747 fn eval_operand_and_layout(&mut self, op: &mir::Operand<'tcx>)
748 -> EvalResult<(Pointer, &'tcx Layout)>
750 use rustc::mir::repr::Operand::*;
752 Consume(ref lvalue) =>
753 Ok((try!(self.eval_lvalue(lvalue)).to_ptr(), self.lvalue_layout(lvalue))),
754 Constant(mir::Constant { ref literal, ty, .. }) => {
755 use rustc::mir::repr::Literal::*;
757 Value { ref value } => Ok((
758 try!(self.const_to_ptr(value)),
759 self.type_layout(ty),
761 Item { .. } => unimplemented!(),
767 // TODO(tsion): Replace this inefficient hack with a wrapper like LvalueTy (e.g. LvalueLayout).
768 fn lvalue_layout(&self, lvalue: &mir::Lvalue<'tcx>) -> &'tcx Layout {
769 use rustc::mir::tcx::LvalueTy;
770 match self.mir().lvalue_ty(self.tcx, lvalue) {
771 LvalueTy::Ty { ty } => self.type_layout(ty),
772 LvalueTy::Downcast { adt_def, substs, variant_index } => {
773 let field_tys = adt_def.variants[variant_index].fields.iter()
774 .map(|f| f.ty(self.tcx, substs));
776 // FIXME(tsion): Handle LvalueTy::Downcast better somehow...
778 // self.repr_arena.alloc(self.make_aggregate_layout(iter::once(field_tys)))
783 fn eval_lvalue(&mut self, lvalue: &mir::Lvalue<'tcx>) -> EvalResult<Lvalue> {
784 use rustc::mir::repr::Lvalue::*;
785 let ptr = match *lvalue {
786 ReturnPointer => self.frame().return_ptr
787 .expect("ReturnPointer used in a function with no return value"),
788 Arg(i) => self.frame().locals[i as usize],
789 Var(i) => self.frame().locals[self.frame().var_offset + i as usize],
790 Temp(i) => self.frame().locals[self.frame().temp_offset + i as usize],
792 Static(_def_id) => unimplemented!(),
794 Projection(ref proj) => {
795 let base_ptr = try!(self.eval_lvalue(&proj.base)).ptr;
796 let base_layout = self.lvalue_layout(&proj.base);
797 let base_ty = self.lvalue_ty(&proj.base);
799 use rustc::mir::repr::ProjectionElem::*;
801 Field(field, _) => match *base_layout {
802 Layout::Univariant { ref variant, .. } => {
803 let offset = variant.field_offset(field.index()).bytes();
804 base_ptr.offset(offset as isize)
806 _ => panic!("field access on non-product type: {:?}", base_layout),
809 Downcast(..) => match *base_layout {
810 Layout::General { discr, .. } =>
811 base_ptr.offset(discr.size().bytes() as isize),
812 _ => panic!("variant downcast on non-aggregate type: {:?}", base_layout),
816 let pointee_ty = pointee_type(base_ty).expect("Deref of non-pointer");
817 let ptr = try!(self.memory.read_ptr(base_ptr));
818 let extra = match pointee_ty.sty {
819 ty::TySlice(_) | ty::TyStr => {
820 let len_ptr = base_ptr.offset(self.memory.pointer_size as isize);
821 let len = try!(self.memory.read_usize(len_ptr));
822 LvalueExtra::Length(len)
824 ty::TyTrait(_) => unimplemented!(),
825 _ => LvalueExtra::None,
827 return Ok(Lvalue { ptr: ptr, extra: extra });
830 Index(ref operand) => {
831 let elem_size = match base_ty.sty {
832 ty::TyArray(elem_ty, _) => self.type_size(elem_ty),
833 ty::TySlice(elem_ty) => self.type_size(elem_ty),
834 _ => panic!("indexing expected an array or slice, got {:?}", base_ty),
836 let n_ptr = try!(self.eval_operand(operand));
837 let n = try!(self.memory.read_usize(n_ptr));
838 base_ptr.offset(n as isize * elem_size as isize)
841 ConstantIndex { .. } => unimplemented!(),
846 Ok(Lvalue { ptr: ptr, extra: LvalueExtra::None })
849 // TODO(tsion): Try making const_to_primval instead.
850 fn const_to_ptr(&mut self, const_val: &const_val::ConstVal) -> EvalResult<Pointer> {
851 use rustc::middle::const_val::ConstVal::*;
853 Float(_f) => unimplemented!(),
855 // TODO(tsion): Check int constant type.
856 let ptr = self.memory.allocate(8);
857 try!(self.memory.write_uint(ptr, int.to_u64_unchecked(), 8));
861 let psize = self.memory.pointer_size;
862 let static_ptr = self.memory.allocate(s.len());
863 let ptr = self.memory.allocate(psize * 2);
864 try!(self.memory.write_bytes(static_ptr, s.as_bytes()));
865 try!(self.memory.write_ptr(ptr, static_ptr));
866 try!(self.memory.write_usize(ptr.offset(psize as isize), s.len() as u64));
870 let psize = self.memory.pointer_size;
871 let static_ptr = self.memory.allocate(bs.len());
872 let ptr = self.memory.allocate(psize);
873 try!(self.memory.write_bytes(static_ptr, bs));
874 try!(self.memory.write_ptr(ptr, static_ptr));
878 let ptr = self.memory.allocate(1);
879 try!(self.memory.write_bool(ptr, b));
882 Char(_c) => unimplemented!(),
883 Struct(_node_id) => unimplemented!(),
884 Tuple(_node_id) => unimplemented!(),
885 Function(_def_id) => unimplemented!(),
886 Array(_, _) => unimplemented!(),
887 Repeat(_, _) => unimplemented!(),
888 Dummy => unimplemented!(),
892 fn lvalue_ty(&self, lvalue: &mir::Lvalue<'tcx>) -> ty::Ty<'tcx> {
893 self.monomorphize(self.mir().lvalue_ty(self.tcx, lvalue).to_ty(self.tcx))
896 fn operand_ty(&self, operand: &mir::Operand<'tcx>) -> ty::Ty<'tcx> {
897 self.monomorphize(self.mir().operand_ty(self.tcx, operand))
900 fn monomorphize(&self, ty: ty::Ty<'tcx>) -> ty::Ty<'tcx> {
901 let substituted = ty.subst(self.tcx, self.substs());
902 infer::normalize_associated_type(self.tcx, &substituted)
905 fn type_needs_drop(&self, ty: ty::Ty<'tcx>) -> bool {
906 self.tcx.type_needs_drop_given_env(ty, &self.tcx.empty_parameter_environment())
909 fn move_(&mut self, src: Pointer, dest: Pointer, ty: ty::Ty<'tcx>) -> EvalResult<()> {
910 let size = self.type_size(ty);
911 try!(self.memory.copy(src, dest, size));
912 if self.type_needs_drop(ty) {
913 try!(self.memory.drop_fill(src, size));
918 fn type_is_sized(&self, ty: ty::Ty<'tcx>) -> bool {
919 ty.is_sized(&self.tcx.empty_parameter_environment(), DUMMY_SP)
922 fn type_size(&self, ty: ty::Ty<'tcx>) -> usize {
923 self.type_layout(ty).size(&self.tcx.data_layout).bytes() as usize
926 fn type_layout(&self, ty: ty::Ty<'tcx>) -> &'tcx Layout {
927 // TODO(tsion): Is this inefficient? Needs investigation.
928 let ty = self.monomorphize(ty);
930 let infcx = infer::normalizing_infer_ctxt(self.tcx, &self.tcx.tables, ProjectionMode::Any);
932 // TODO(tsion): Report this error properly.
933 ty.layout(&infcx).unwrap()
936 pub fn read_primval(&mut self, ptr: Pointer, ty: ty::Ty<'tcx>) -> EvalResult<PrimVal> {
937 use syntax::ast::{IntTy, UintTy};
938 let val = match ty.sty {
939 ty::TyBool => PrimVal::Bool(try!(self.memory.read_bool(ptr))),
940 ty::TyInt(IntTy::I8) => PrimVal::I8(try!(self.memory.read_int(ptr, 1)) as i8),
941 ty::TyInt(IntTy::I16) => PrimVal::I16(try!(self.memory.read_int(ptr, 2)) as i16),
942 ty::TyInt(IntTy::I32) => PrimVal::I32(try!(self.memory.read_int(ptr, 4)) as i32),
943 ty::TyInt(IntTy::I64) => PrimVal::I64(try!(self.memory.read_int(ptr, 8)) as i64),
944 ty::TyUint(UintTy::U8) => PrimVal::U8(try!(self.memory.read_uint(ptr, 1)) as u8),
945 ty::TyUint(UintTy::U16) => PrimVal::U16(try!(self.memory.read_uint(ptr, 2)) as u16),
946 ty::TyUint(UintTy::U32) => PrimVal::U32(try!(self.memory.read_uint(ptr, 4)) as u32),
947 ty::TyUint(UintTy::U64) => PrimVal::U64(try!(self.memory.read_uint(ptr, 8)) as u64),
949 // TODO(tsion): Pick the PrimVal dynamically.
950 ty::TyInt(IntTy::Is) => PrimVal::I64(try!(self.memory.read_isize(ptr))),
951 ty::TyUint(UintTy::Us) => PrimVal::U64(try!(self.memory.read_usize(ptr))),
953 ty::TyRef(_, ty::TypeAndMut { ty, .. }) |
954 ty::TyRawPtr(ty::TypeAndMut { ty, .. }) => {
955 if self.type_is_sized(ty) {
956 match self.memory.read_ptr(ptr) {
957 Ok(p) => PrimVal::AbstractPtr(p),
958 Err(EvalError::ReadBytesAsPointer) => {
959 let n = try!(self.memory.read_usize(ptr));
960 PrimVal::IntegerPtr(n)
962 Err(e) => return Err(e),
965 panic!("unimplemented: primitive read of fat pointer type: {:?}", ty);
969 _ => panic!("primitive read of non-primitive type: {:?}", ty),
974 fn frame(&self) -> &Frame<'a, 'tcx> {
975 self.stack.last().expect("no call frames exist")
978 fn frame_mut(&mut self) -> &mut Frame<'a, 'tcx> {
979 self.stack.last_mut().expect("no call frames exist")
982 fn mir(&self) -> &mir::Mir<'tcx> {
986 fn substs(&self) -> &'tcx Substs<'tcx> {
987 self.substs_stack.last().cloned().unwrap_or_else(|| self.tcx.mk_substs(Substs::empty()))
990 fn load_mir(&self, def_id: DefId) -> CachedMir<'a, 'tcx> {
991 match self.tcx.map.as_local_node_id(def_id) {
992 Some(node_id) => CachedMir::Ref(self.mir_map.map.get(&node_id).unwrap()),
994 let mut mir_cache = self.mir_cache.borrow_mut();
995 if let Some(mir) = mir_cache.get(&def_id) {
996 return CachedMir::Owned(mir.clone());
999 use rustc::middle::cstore::CrateStore;
1000 let cs = &self.tcx.sess.cstore;
1001 let mir = cs.maybe_get_item_mir(self.tcx, def_id).unwrap_or_else(|| {
1002 panic!("no mir for {:?}", def_id);
1004 let cached = Rc::new(mir);
1005 mir_cache.insert(def_id, cached.clone());
1006 CachedMir::Owned(cached)
1011 fn fulfill_obligation(&self, trait_ref: ty::PolyTraitRef<'tcx>) -> traits::Vtable<'tcx, ()> {
1012 // Do the initial selection for the obligation. This yields the shallow result we are
1013 // looking for -- that is, what specific impl.
1014 let infcx = infer::normalizing_infer_ctxt(self.tcx, &self.tcx.tables, ProjectionMode::Any);
1015 let mut selcx = traits::SelectionContext::new(&infcx);
1017 let obligation = traits::Obligation::new(
1018 traits::ObligationCause::misc(DUMMY_SP, ast::DUMMY_NODE_ID),
1019 trait_ref.to_poly_trait_predicate(),
1021 let selection = selcx.select(&obligation).unwrap().unwrap();
1023 // Currently, we use a fulfillment context to completely resolve all nested obligations.
1024 // This is because they can inform the inference of the impl's type parameters.
1025 let mut fulfill_cx = traits::FulfillmentContext::new();
1026 let vtable = selection.map(|predicate| {
1027 fulfill_cx.register_predicate_obligation(&infcx, predicate);
1029 let vtable = infer::drain_fulfillment_cx_or_panic(
1030 DUMMY_SP, &infcx, &mut fulfill_cx, &vtable
1036 /// Trait method, which has to be resolved to an impl method.
1037 pub fn trait_method(&self, def_id: DefId, substs: &'tcx Substs<'tcx>)
1038 -> (DefId, &'tcx Substs<'tcx>)
1040 let method_item = self.tcx.impl_or_trait_item(def_id);
1041 let trait_id = method_item.container().id();
1042 let trait_ref = ty::Binder(substs.to_trait_ref(self.tcx, trait_id));
1043 match self.fulfill_obligation(trait_ref) {
1044 traits::VtableImpl(vtable_impl) => {
1045 let impl_did = vtable_impl.impl_def_id;
1046 let mname = self.tcx.item_name(def_id);
1047 // Create a concatenated set of substitutions which includes those from the impl
1048 // and those from the method:
1049 let impl_substs = vtable_impl.substs.with_method_from(substs);
1050 let substs = self.tcx.mk_substs(impl_substs);
1051 let mth = get_impl_method(self.tcx, impl_did, substs, mname);
1053 (mth.method.def_id, mth.substs)
1056 traits::VtableClosure(vtable_closure) =>
1057 (vtable_closure.closure_def_id, vtable_closure.substs.func_substs),
1059 traits::VtableFnPointer(_fn_ty) => {
1060 let _trait_closure_kind = self.tcx.lang_items.fn_trait_kind(trait_id).unwrap();
1062 // let llfn = trans_fn_pointer_shim(ccx, trait_closure_kind, fn_ty);
1064 // let method_ty = def_ty(tcx, def_id, substs);
1065 // let fn_ptr_ty = match method_ty.sty {
1066 // ty::TyFnDef(_, _, fty) => tcx.mk_ty(ty::TyFnPtr(fty)),
1067 // _ => unreachable!("expected fn item type, found {}",
1070 // Callee::ptr(immediate_rvalue(llfn, fn_ptr_ty))
1073 traits::VtableObject(ref _data) => {
1076 // data: Virtual(traits::get_vtable_index_of_object_method(
1077 // tcx, data, def_id)),
1078 // ty: def_ty(tcx, def_id, substs)
1081 vtable => unreachable!("resolved vtable bad vtable {:?} in trans", vtable),
1086 fn pointee_type<'tcx>(ptr_ty: ty::Ty<'tcx>) -> Option<ty::Ty<'tcx>> {
1088 ty::TyRef(_, ty::TypeAndMut { ty, .. }) |
1089 ty::TyRawPtr(ty::TypeAndMut { ty, .. }) |
1098 fn to_ptr(self) -> Pointer {
1099 assert_eq!(self.extra, LvalueExtra::None);
1104 impl<'mir, 'tcx: 'mir> Deref for CachedMir<'mir, 'tcx> {
1105 type Target = mir::Mir<'tcx>;
1106 fn deref(&self) -> &mir::Mir<'tcx> {
1108 CachedMir::Ref(r) => r,
1109 CachedMir::Owned(ref rc) => &rc,
1115 pub struct ImplMethod<'tcx> {
1116 pub method: Rc<ty::Method<'tcx>>,
1117 pub substs: &'tcx Substs<'tcx>,
1118 pub is_provided: bool,
1121 /// Locates the applicable definition of a method, given its name.
1122 pub fn get_impl_method<'tcx>(
1125 substs: &'tcx Substs<'tcx>,
1127 ) -> ImplMethod<'tcx> {
1128 assert!(!substs.types.needs_infer());
1130 let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap();
1131 let trait_def = tcx.lookup_trait_def(trait_def_id);
1132 let infcx = infer::normalizing_infer_ctxt(tcx, &tcx.tables, ProjectionMode::Any);
1134 match trait_def.ancestors(impl_def_id).fn_defs(tcx, name).next() {
1135 Some(node_item) => {
1137 method: node_item.item,
1138 substs: traits::translate_substs(&infcx, impl_def_id, substs, node_item.node),
1139 is_provided: node_item.node.is_from_trait(),
1143 bug!("method {:?} not found in {:?}", name, impl_def_id);
1148 pub fn interpret_start_points<'tcx>(tcx: &TyCtxt<'tcx>, mir_map: &MirMap<'tcx>) {
1149 for (&id, mir) in &mir_map.map {
1150 for attr in tcx.map.attrs(id) {
1151 use syntax::attr::AttrMetaMethods;
1152 if attr.check_name("miri_run") {
1153 let item = tcx.map.expect_item(id);
1155 println!("Interpreting: {}", item.name);
1157 let mut miri = Interpreter::new(tcx, mir_map);
1158 let return_ptr = match mir.return_ty {
1159 ty::FnConverging(ty) => {
1160 let size = miri.type_size(ty);
1161 Some(miri.memory.allocate(size))
1163 ty::FnDiverging => None,
1165 let substs = miri.tcx.mk_substs(Substs::empty());
1166 miri.push_stack_frame(CachedMir::Ref(mir), substs, return_ptr);
1167 if let Err(_e) = miri.run() {
1168 // TODO(tsion): Detect whether the error was already reported or not.
1169 // tcx.sess.err(&e.to_string());
1170 } else if let Some(ret) = return_ptr {
1171 miri.memory.dump(ret.alloc_id);
1179 // TODO(tsion): Upstream these methods into rustc::ty::layout.
1182 fn size(self) -> Size;
1185 impl IntegerExt for layout::Integer {
1186 fn size(self) -> Size {
1187 use rustc::ty::layout::Integer::*;
1189 I1 | I8 => Size::from_bits(8),
1190 I16 => Size::from_bits(16),
1191 I32 => Size::from_bits(32),
1192 I64 => Size::from_bits(64),
1198 fn field_offset(&self, index: usize) -> Size;
1201 impl StructExt for layout::Struct {
1202 fn field_offset(&self, index: usize) -> Size {
1206 self.offset_after_field[index - 1]