]> git.lizzy.rs Git - rust.git/blob - src/interpreter.rs
546935dee76667b7561d557f012a641b8f4c8bfa
[rust.git] / src / interpreter.rs
1 use rustc::infer;
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;
13 use std::ops::Deref;
14 use std::rc::Rc;
15 use std::{iter, mem};
16 use syntax::ast;
17 use syntax::attr;
18 use syntax::codemap::{self, DUMMY_SP};
19
20 use error::{EvalError, EvalResult};
21 use memory::{Memory, Pointer};
22 use primval::{self, PrimVal};
23
24 const TRACE_EXECUTION: bool = false;
25
26 struct Interpreter<'a, 'tcx: 'a> {
27     /// The results of the type checker, from rustc.
28     tcx: &'a TyCtxt<'tcx>,
29
30     /// A mapping from NodeIds to Mir, from rustc. Only contains MIR for crate-local items.
31     mir_map: &'a MirMap<'tcx>,
32
33     /// A local cache from DefIds to Mir for non-crate-local items.
34     mir_cache: RefCell<DefIdMap<Rc<mir::Mir<'tcx>>>>,
35
36     /// The virtual memory system.
37     memory: Memory,
38
39     /// The virtual call stack.
40     stack: Vec<Frame<'a, 'tcx>>,
41
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>>,
46
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)>,
52 }
53
54 /// A stack frame.
55 struct Frame<'a, 'tcx: 'a> {
56     /// The MIR for the function called on this frame.
57     mir: CachedMir<'a, 'tcx>,
58
59     /// The block this frame will execute when a function call returns back to this frame.
60     next_block: mir::BasicBlock,
61
62     /// A pointer for writing the return value of the current call if it's not a diverging call.
63     return_ptr: Option<Pointer>,
64
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`.
68     locals: Vec<Pointer>,
69
70     /// The offset of the first variable in `self.locals`.
71     var_offset: usize,
72
73     /// The offset of the first temporary in `self.locals`.
74     temp_offset: usize,
75 }
76
77 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
78 struct Lvalue {
79     ptr: Pointer,
80     extra: LvalueExtra,
81 }
82
83 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
84 enum LvalueExtra {
85     None,
86     Length(u64),
87     // Vtable(memory::AllocId),
88 }
89
90 #[derive(Clone)]
91 enum CachedMir<'mir, 'tcx: 'mir> {
92     Ref(&'mir mir::Mir<'tcx>),
93     Owned(Rc<mir::Mir<'tcx>>)
94 }
95
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),
100
101     /// Start executing from the new current frame. (For function calls.)
102     Call,
103
104     /// Stop executing the current frame and resume the previous frame.
105     Return,
106 }
107
108 impl<'a, 'tcx: 'a> Interpreter<'a, 'tcx> {
109     fn new(tcx: &'a TyCtxt<'tcx>, mir_map: &'a MirMap<'tcx>) -> Self {
110         Interpreter {
111             tcx: tcx,
112             mir_map: mir_map,
113             mir_cache: RefCell::new(DefIdMap()),
114             memory: Memory::new(),
115             stack: Vec::new(),
116             substs_stack: Vec::new(),
117             name_stack: Vec::new(),
118         }
119     }
120
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;
127                 use std::fmt;
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)
133                     }
134                 }
135                 err.span_note(span, &format!("inside call to {}", Instance(def_id, substs)));
136             }
137             err.emit();
138         }
139         r
140     }
141
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!("    "); }
146         f();
147         println!("");
148     }
149
150     fn run(&mut self) -> EvalResult<()> {
151         'outer: while !self.stack.is_empty() {
152             let mut current_block = self.frame().next_block;
153
154             loop {
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);
158
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));
164                 }
165
166                 let terminator = block_data.terminator();
167                 self.log(0, || print!("{:?}", terminator.kind));
168
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();
175                         continue 'outer;
176                     }
177                     TerminatorTarget::Call => continue 'outer,
178                 }
179             }
180         }
181
182         Ok(())
183     }
184
185     fn push_stack_frame(&mut self, mir: CachedMir<'a, 'tcx>, substs: &'tcx Substs<'tcx>,
186         return_ptr: Option<Pointer>)
187     {
188         self.substs_stack.push(substs);
189
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);
193
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)
197         }).collect();
198
199         let num_args = mir.arg_decls.len();
200         let num_vars = mir.var_decls.len();
201
202         self.stack.push(Frame {
203             mir: mir.clone(),
204             next_block: mir::START_BLOCK,
205             return_ptr: return_ptr,
206             locals: locals,
207             var_offset: num_args,
208             temp_offset: num_args + num_vars,
209         });
210     }
211
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();
216     }
217
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,
223
224             Goto { target } => TerminatorTarget::Block(target),
225
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 })
230             }
231
232             SwitchInt { ref discr, ref values, ref targets, .. } => {
233                 let discr_ptr = try!(self.eval_lvalue(discr)).to_ptr();
234                 let discr_size =
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));
237
238                 // Branch to the `otherwise` case by default, if no match is found.
239                 let mut target_block = targets[targets.len() - 1];
240
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];
246                         break;
247                     }
248                 }
249
250                 TerminatorTarget::Block(target_block)
251             }
252
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"),
259                 };
260                 let discr_val = try!(self.memory.read_uint(adt_ptr, discr_size as usize));
261
262                 let matching = adt_def.variants.iter()
263                     .position(|v| discr_val == v.disr_val.to_u64_unchecked());
264
265                 match matching {
266                     Some(i) => TerminatorTarget::Block(targets[i]),
267                     None => return Err(EvalError::InvalidDiscriminant),
268                 }
269             }
270
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());
276                 }
277
278                 let func_ty = self.operand_ty(func);
279                 match func_ty.sty {
280                     ty::TyFnDef(def_id, substs, fn_ty) => {
281                         use syntax::abi::Abi;
282                         match fn_ty.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))
290                                     }
291                                     ty::FnDiverging => unimplemented!(),
292                                 }
293                             }
294
295                             Abi::C =>
296                                 try!(self.call_c_abi(def_id, args, return_ptr.unwrap())),
297
298                             Abi::Rust | Abi::RustCall => {
299                                 // TODO(tsion): Adjust the first argument when calling a Fn or
300                                 // FnMut closure via FnOnce::call_once.
301
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)
305                                 } else {
306                                     (def_id, substs)
307                                 };
308
309                                 let mut arg_srcs = Vec::new();
310                                 for arg in args {
311                                     let src = try!(self.eval_operand(arg));
312                                     let src_ty = self.operand_ty(arg);
313                                     arg_srcs.push((src, src_ty));
314                                 }
315
316                                 if fn_ty.abi == Abi::RustCall && !args.is_empty() {
317                                     arg_srcs.pop();
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));
331                                             }
332                                         }
333                                         ty => panic!("expected tuple as last argument in function with 'rust-call' ABI, got {:?}", ty),
334                                     }
335                                 }
336
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);
340
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));
344                                 }
345
346                                 TerminatorTarget::Call
347                             }
348
349                             abi => panic!("can't handle function with {:?} ABI", abi),
350                         }
351                     }
352
353                     _ => panic!("can't handle callee of type {:?}", func_ty),
354                 }
355             }
356
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)
362             }
363
364             Resume => unimplemented!(),
365         };
366
367         Ok(target)
368     }
369
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));
373             return Ok(());
374         }
375         self.log(1, || print!("need to drop {:?}", ty));
376
377         // TODO(tsion): Call user-defined Drop::drop impls.
378
379         match ty.sty {
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));
386                     }
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) {
391                             return Ok(());
392                         } else {
393                             return Err(EvalError::ReadBytesAsPointer);
394                         }
395                     }
396                     Err(e) => return Err(e),
397                 }
398             }
399
400             // TODO(tsion): Implement drop for other relevant types (e.g. aggregates).
401             _ => {}
402         }
403
404         // Filling drop.
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));
408
409         Ok(())
410     }
411
412     fn call_intrinsic(
413         &mut self,
414         name: &str,
415         substs: &'tcx Substs<'tcx>,
416         args: &[mir::Operand<'tcx>],
417         dest: Pointer,
418         dest_size: usize
419     ) -> EvalResult<TerminatorTarget> {
420         let args_res: EvalResult<Vec<Pointer>> = args.iter()
421             .map(|arg| self.eval_operand(arg))
422             .collect();
423         let args = try!(args_res);
424
425         match name {
426             "assume" => {}
427
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));
435             }
436
437             "forget" => {
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));
441             }
442
443             "init" => try!(self.memory.write_repeat(dest, 0, dest_size)),
444
445             "min_align_of" => {
446                 try!(self.memory.write_int(dest, 1, dest_size));
447             }
448
449             "move_val_init" => {
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));
453             }
454
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)
463                 };
464                 try!(self.memory.write_int(dest, n, size));
465                 try!(self.memory.write_bool(dest.offset(size as isize), overflowed));
466             }
467
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)
476                 };
477                 try!(self.memory.write_int(dest, n, size));
478                 try!(self.memory.write_bool(dest.offset(size as isize), overflowed));
479             }
480
481             "offset" => {
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]));
486
487                 match self.memory.read_ptr(ptr_arg) {
488                     Ok(ptr) => {
489                         let result_ptr = ptr.offset(offset as isize * pointee_size);
490                         try!(self.memory.write_ptr(dest, result_ptr));
491                     }
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));
496                     }
497                     Err(e) => return Err(e),
498                 }
499             }
500
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));
509             }
510
511             "size_of" => {
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));
515             }
516
517             "transmute" => {
518                 let ty = *substs.types.get(subst::FnSpace, 0);
519                 try!(self.move_(args[0], dest, ty));
520             }
521             "uninit" => try!(self.memory.mark_definedness(dest, dest_size, false)),
522
523             name => panic!("can't handle intrinsic: {}", name),
524         }
525
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
528         // current frame.
529         Ok(TerminatorTarget::Call)
530     }
531
532     fn call_c_abi(
533         &mut self,
534         def_id: DefId,
535         args: &[mir::Operand<'tcx>],
536         dest: Pointer
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(),
543         };
544
545         let args_res: EvalResult<Vec<Pointer>> = args.iter()
546             .map(|arg| self.eval_operand(arg))
547             .collect();
548         let args = try!(args_res);
549
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));
555             }
556
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));
562             }
563
564             _ => panic!("can't call C ABI function: {}", link_name),
565         }
566
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
569         // current frame.
570         Ok(TerminatorTarget::Call)
571     }
572
573     fn assign_fields<I: IntoIterator<Item = u64>>(
574         &mut self,
575         dest: Pointer,
576         offsets: I,
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));
584         }
585         Ok(())
586     }
587
588     fn eval_assignment(&mut self, lvalue: &mir::Lvalue<'tcx>, rvalue: &mir::Rvalue<'tcx>)
589         -> EvalResult<()>
590     {
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);
594
595         use rustc::mir::repr::Rvalue::*;
596         match *rvalue {
597             Use(ref operand) => {
598                 let src = try!(self.eval_operand(operand));
599                 try!(self.move_(src, dest, dest_ty));
600             }
601
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));
606
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));
610
611                 let val = try!(primval::binary_op(bin_op, left_val, right_val));
612                 try!(self.memory.write_primval(dest, val));
613             }
614
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)));
620             }
621
622             Aggregate(ref kind, ref operands) => {
623                 match *dest_layout {
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));
628                     }
629
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 {:?}",
634                                         kind, dest_ty),
635                         };
636                         let offsets = (0..).map(|i| i * elem_size);
637                         try!(self.assign_fields(dest, offsets, operands));
638                     }
639
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));
645
646                             let offsets = variants[variant].offset_after_field.iter()
647                                 .map(|s| s.bytes());
648                             try!(self.assign_fields(dest, offsets, operands));
649                         } else {
650                             panic!("tried to assign {:?} aggregate to Layout::General dest", kind);
651                         }
652                     }
653
654                     _ => panic!("can't handle destination layout {:?} when assigning {:?}",
655                                 dest_layout, kind),
656                 }
657             }
658
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),
663                 };
664
665                 let src = try!(self.eval_operand(operand));
666                 for i in 0..length {
667                     let elem_dest = dest.offset((i * elem_size) as isize);
668                     try!(self.memory.copy(src, elem_dest, elem_size));
669                 }
670             }
671
672             Len(ref lvalue) => {
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 {
678                         n
679                     } else {
680                         panic!("Rvalue::Len of a slice given non-slice pointer: {:?}", src);
681                     },
682                     _ => panic!("Rvalue::Len expected array or slice, got {:?}", ty),
683                 };
684                 try!(self.memory.write_usize(dest, len));
685             }
686
687             Ref(_, _, ref lvalue) => {
688                 let lv = try!(self.eval_lvalue(lvalue));
689                 try!(self.memory.write_ptr(dest, lv.ptr));
690                 match lv.extra {
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));
695                     }
696                 }
697             }
698
699             Box(ty) => {
700                 let size = self.type_size(ty);
701                 let ptr = self.memory.allocate(size);
702                 try!(self.memory.write_ptr(dest, ptr));
703             }
704
705             Cast(kind, ref operand, dest_ty) => {
706                 let src = try!(self.eval_operand(operand));
707                 let src_ty = self.operand_ty(operand);
708
709                 use rustc::mir::repr::CastKind::*;
710                 match kind {
711                     Unsize => {
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();
715
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));
720                             }
721
722                             _ => panic!("can't handle cast: {:?}", rvalue),
723                         }
724                     }
725
726                     Misc => {
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));
730                     }
731
732                     _ => panic!("can't handle cast: {:?}", rvalue),
733                 }
734             }
735
736             Slice { .. } => unimplemented!(),
737             InlineAsm { .. } => unimplemented!(),
738         }
739
740         Ok(())
741     }
742
743     fn eval_operand(&mut self, op: &mir::Operand<'tcx>) -> EvalResult<Pointer> {
744         self.eval_operand_and_layout(op).map(|(p, _)| p)
745     }
746
747     fn eval_operand_and_layout(&mut self, op: &mir::Operand<'tcx>)
748         -> EvalResult<(Pointer, &'tcx Layout)>
749     {
750         use rustc::mir::repr::Operand::*;
751         match *op {
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::*;
756                 match *literal {
757                     Value { ref value } => Ok((
758                         try!(self.const_to_ptr(value)),
759                         self.type_layout(ty),
760                     )),
761                     Item { .. } => unimplemented!(),
762                 }
763             }
764         }
765     }
766
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));
775
776                 // FIXME(tsion): Handle LvalueTy::Downcast better somehow...
777                 unimplemented!();
778                 // self.repr_arena.alloc(self.make_aggregate_layout(iter::once(field_tys)))
779             }
780         }
781     }
782
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],
791
792             Static(_def_id) => unimplemented!(),
793
794             Projection(ref proj) => {
795                 let base_ptr = try!(self.eval_lvalue(&proj.base)).to_ptr();
796                 let base_layout = self.lvalue_layout(&proj.base);
797                 let base_ty = self.lvalue_ty(&proj.base);
798
799                 use rustc::mir::repr::ProjectionElem::*;
800                 match proj.elem {
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)
805                         }
806                         _ => panic!("field access on non-product type: {:?}", base_layout),
807                     },
808
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),
813                     },
814
815                     Deref => {
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)
823                             }
824                             ty::TyTrait(_) => unimplemented!(),
825                             _ => LvalueExtra::None,
826                         };
827                         return Ok(Lvalue { ptr: ptr, extra: extra });
828                     }
829
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),
835                         };
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)
839                     }
840
841                     ConstantIndex { .. } => unimplemented!(),
842                 }
843             }
844         };
845
846         Ok(Lvalue { ptr: ptr, extra: LvalueExtra::None })
847     }
848
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::*;
852         match *const_val {
853             Float(_f) => unimplemented!(),
854             Integral(int) => {
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));
858                 Ok(ptr)
859             }
860             Str(ref s) => {
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));
867                 Ok(ptr)
868             }
869             ByteStr(ref bs) => {
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));
875                 Ok(ptr)
876             }
877             Bool(b) => {
878                 let ptr = self.memory.allocate(1);
879                 try!(self.memory.write_bool(ptr, b));
880                 Ok(ptr)
881             }
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!(),
889         }
890     }
891
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))
894     }
895
896     fn operand_ty(&self, operand: &mir::Operand<'tcx>) -> ty::Ty<'tcx> {
897         self.monomorphize(self.mir().operand_ty(self.tcx, operand))
898     }
899
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)
903     }
904
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())
907     }
908
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));
914         }
915         Ok(())
916     }
917
918     fn type_is_sized(&self, ty: ty::Ty<'tcx>) -> bool {
919         ty.is_sized(&self.tcx.empty_parameter_environment(), DUMMY_SP)
920     }
921
922     fn type_size(&self, ty: ty::Ty<'tcx>) -> usize {
923         self.type_layout(ty).size(&self.tcx.data_layout).bytes() as usize
924     }
925
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);
929
930         let infcx = infer::normalizing_infer_ctxt(self.tcx, &self.tcx.tables, ProjectionMode::Any);
931
932         // TODO(tsion): Report this error properly.
933         ty.layout(&infcx).unwrap()
934     }
935
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),
948
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))),
952
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)
961                         }
962                         Err(e) => return Err(e),
963                     }
964                 } else {
965                     panic!("unimplemented: primitive read of fat pointer type: {:?}", ty);
966                 }
967             }
968
969             _ => panic!("primitive read of non-primitive type: {:?}", ty),
970         };
971         Ok(val)
972     }
973
974     fn frame(&self) -> &Frame<'a, 'tcx> {
975         self.stack.last().expect("no call frames exist")
976     }
977
978     fn frame_mut(&mut self) -> &mut Frame<'a, 'tcx> {
979         self.stack.last_mut().expect("no call frames exist")
980     }
981
982     fn mir(&self) -> &mir::Mir<'tcx> {
983         &self.frame().mir
984     }
985
986     fn substs(&self) -> &'tcx Substs<'tcx> {
987         self.substs_stack.last().cloned().unwrap_or_else(|| self.tcx.mk_substs(Substs::empty()))
988     }
989
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()),
993             None => {
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());
997                 }
998
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);
1003                 });
1004                 let cached = Rc::new(mir);
1005                 mir_cache.insert(def_id, cached.clone());
1006                 CachedMir::Owned(cached)
1007             }
1008         }
1009     }
1010
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);
1016
1017         let obligation = traits::Obligation::new(
1018             traits::ObligationCause::misc(DUMMY_SP, ast::DUMMY_NODE_ID),
1019             trait_ref.to_poly_trait_predicate(),
1020         );
1021         let selection = selcx.select(&obligation).unwrap().unwrap();
1022
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);
1028         });
1029         let vtable = infer::drain_fulfillment_cx_or_panic(
1030             DUMMY_SP, &infcx, &mut fulfill_cx, &vtable
1031         );
1032
1033         vtable
1034     }
1035
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>)
1039     {
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);
1052
1053                 (mth.method.def_id, mth.substs)
1054             }
1055
1056             traits::VtableClosure(vtable_closure) =>
1057                 (vtable_closure.closure_def_id, vtable_closure.substs.func_substs),
1058
1059             traits::VtableFnPointer(_fn_ty) => {
1060                 let _trait_closure_kind = self.tcx.lang_items.fn_trait_kind(trait_id).unwrap();
1061                 unimplemented!()
1062                 // let llfn = trans_fn_pointer_shim(ccx, trait_closure_kind, fn_ty);
1063
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 {}",
1068                 //                       method_ty)
1069                 // };
1070                 // Callee::ptr(immediate_rvalue(llfn, fn_ptr_ty))
1071             }
1072
1073             traits::VtableObject(ref _data) => {
1074                 unimplemented!()
1075                 // Callee {
1076                 //     data: Virtual(traits::get_vtable_index_of_object_method(
1077                 //                   tcx, data, def_id)),
1078                 //                   ty: def_ty(tcx, def_id, substs)
1079                 // }
1080             }
1081             vtable => unreachable!("resolved vtable bad vtable {:?} in trans", vtable),
1082         }
1083     }
1084 }
1085
1086 fn pointee_type<'tcx>(ptr_ty: ty::Ty<'tcx>) -> Option<ty::Ty<'tcx>> {
1087     match ptr_ty.sty {
1088         ty::TyRef(_, ty::TypeAndMut { ty, .. }) |
1089         ty::TyRawPtr(ty::TypeAndMut { ty, .. }) |
1090         ty::TyBox(ty) => {
1091             Some(ty)
1092         }
1093         _ => None,
1094     }
1095 }
1096
1097 impl Lvalue {
1098     fn to_ptr(self) -> Pointer {
1099         assert_eq!(self.extra, LvalueExtra::None);
1100         self.ptr
1101     }
1102 }
1103
1104 impl<'mir, 'tcx: 'mir> Deref for CachedMir<'mir, 'tcx> {
1105     type Target = mir::Mir<'tcx>;
1106     fn deref(&self) -> &mir::Mir<'tcx> {
1107         match *self {
1108             CachedMir::Ref(r) => r,
1109             CachedMir::Owned(ref rc) => &rc,
1110         }
1111     }
1112 }
1113
1114 #[derive(Debug)]
1115 pub struct ImplMethod<'tcx> {
1116     pub method: Rc<ty::Method<'tcx>>,
1117     pub substs: &'tcx Substs<'tcx>,
1118     pub is_provided: bool,
1119 }
1120
1121 /// Locates the applicable definition of a method, given its name.
1122 pub fn get_impl_method<'tcx>(
1123     tcx: &TyCtxt<'tcx>,
1124     impl_def_id: DefId,
1125     substs: &'tcx Substs<'tcx>,
1126     name: ast::Name,
1127 ) -> ImplMethod<'tcx> {
1128     assert!(!substs.types.needs_infer());
1129
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);
1133
1134     match trait_def.ancestors(impl_def_id).fn_defs(tcx, name).next() {
1135         Some(node_item) => {
1136             ImplMethod {
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(),
1140             }
1141         }
1142         None => {
1143             bug!("method {:?} not found in {:?}", name, impl_def_id);
1144         }
1145     }
1146 }
1147
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);
1154
1155                 println!("Interpreting: {}", item.name);
1156
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))
1162                     }
1163                     ty::FnDiverging => None,
1164                 };
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);
1172                 }
1173                 println!("");
1174             }
1175         }
1176     }
1177 }
1178
1179 // TODO(tsion): Upstream these methods into rustc::ty::layout.
1180
1181 trait IntegerExt {
1182     fn size(self) -> Size;
1183 }
1184
1185 impl IntegerExt for layout::Integer {
1186     fn size(self) -> Size {
1187         use rustc::ty::layout::Integer::*;
1188         match self {
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),
1193         }
1194     }
1195 }
1196
1197 trait StructExt {
1198     fn field_offset(&self, index: usize) -> Size;
1199 }
1200
1201 impl StructExt for layout::Struct {
1202     fn field_offset(&self, index: usize) -> Size {
1203         if index == 0 {
1204             Size::from_bytes(0)
1205         } else {
1206             self.offset_after_field[index - 1]
1207         }
1208     }
1209 }