]> git.lizzy.rs Git - rust.git/blob - src/interpreter.rs
636aa27240b550c3d303651169163a3ab09cd8c2
[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         use rustc::mir::repr::Operand::*;
745         match *op {
746             Consume(ref lvalue) =>
747                 Ok(try!(self.eval_lvalue(lvalue)).to_ptr()),
748             Constant(mir::Constant { ref literal, ty, .. }) => {
749                 use rustc::mir::repr::Literal::*;
750                 match *literal {
751                     Value { ref value } => Ok(try!(self.const_to_ptr(value))),
752                     Item { .. } => unimplemented!(),
753                 }
754             }
755         }
756     }
757
758     // TODO(tsion): Replace this inefficient hack with a wrapper like LvalueTy (e.g. LvalueLayout).
759     fn lvalue_layout(&self, lvalue: &mir::Lvalue<'tcx>) -> &'tcx Layout {
760         use rustc::mir::tcx::LvalueTy;
761         match self.mir().lvalue_ty(self.tcx, lvalue) {
762             LvalueTy::Ty { ty } => self.type_layout(ty),
763             LvalueTy::Downcast { adt_def, substs, variant_index } => {
764                 let field_tys = adt_def.variants[variant_index].fields.iter()
765                     .map(|f| f.ty(self.tcx, substs));
766
767                 // FIXME(tsion): Handle LvalueTy::Downcast better somehow...
768                 unimplemented!();
769                 // self.repr_arena.alloc(self.make_aggregate_layout(iter::once(field_tys)))
770             }
771         }
772     }
773
774     fn eval_lvalue(&mut self, lvalue: &mir::Lvalue<'tcx>) -> EvalResult<Lvalue> {
775         use rustc::mir::repr::Lvalue::*;
776         let ptr = match *lvalue {
777             ReturnPointer => self.frame().return_ptr
778                 .expect("ReturnPointer used in a function with no return value"),
779             Arg(i) => self.frame().locals[i as usize],
780             Var(i) => self.frame().locals[self.frame().var_offset + i as usize],
781             Temp(i) => self.frame().locals[self.frame().temp_offset + i as usize],
782
783             Static(_def_id) => unimplemented!(),
784
785             Projection(ref proj) => {
786                 let base_ptr = try!(self.eval_lvalue(&proj.base)).ptr;
787                 let base_layout = self.lvalue_layout(&proj.base);
788                 let base_ty = self.lvalue_ty(&proj.base);
789
790                 use rustc::mir::repr::ProjectionElem::*;
791                 match proj.elem {
792                     Field(field, _) => match *base_layout {
793                         Layout::Univariant { ref variant, .. } => {
794                             let offset = variant.field_offset(field.index()).bytes();
795                             base_ptr.offset(offset as isize)
796                         }
797                         _ => panic!("field access on non-product type: {:?}", base_layout),
798                     },
799
800                     Downcast(..) => match *base_layout {
801                         Layout::General { discr, .. } =>
802                             base_ptr.offset(discr.size().bytes() as isize),
803                         _ => panic!("variant downcast on non-aggregate type: {:?}", base_layout),
804                     },
805
806                     Deref => {
807                         let pointee_ty = pointee_type(base_ty).expect("Deref of non-pointer");
808                         let ptr = try!(self.memory.read_ptr(base_ptr));
809                         let extra = match pointee_ty.sty {
810                             ty::TySlice(_) | ty::TyStr => {
811                                 let len_ptr = base_ptr.offset(self.memory.pointer_size as isize);
812                                 let len = try!(self.memory.read_usize(len_ptr));
813                                 LvalueExtra::Length(len)
814                             }
815                             ty::TyTrait(_) => unimplemented!(),
816                             _ => LvalueExtra::None,
817                         };
818                         return Ok(Lvalue { ptr: ptr, extra: extra });
819                     }
820
821                     Index(ref operand) => {
822                         let elem_size = match base_ty.sty {
823                             ty::TyArray(elem_ty, _) => self.type_size(elem_ty),
824                             ty::TySlice(elem_ty) => self.type_size(elem_ty),
825                             _ => panic!("indexing expected an array or slice, got {:?}", base_ty),
826                         };
827                         let n_ptr = try!(self.eval_operand(operand));
828                         let n = try!(self.memory.read_usize(n_ptr));
829                         base_ptr.offset(n as isize * elem_size as isize)
830                     }
831
832                     ConstantIndex { .. } => unimplemented!(),
833                 }
834             }
835         };
836
837         Ok(Lvalue { ptr: ptr, extra: LvalueExtra::None })
838     }
839
840     // TODO(tsion): Try making const_to_primval instead.
841     fn const_to_ptr(&mut self, const_val: &const_val::ConstVal) -> EvalResult<Pointer> {
842         use rustc::middle::const_val::ConstVal::*;
843         match *const_val {
844             Float(_f) => unimplemented!(),
845             Integral(int) => {
846                 // TODO(tsion): Check int constant type.
847                 let ptr = self.memory.allocate(8);
848                 try!(self.memory.write_uint(ptr, int.to_u64_unchecked(), 8));
849                 Ok(ptr)
850             }
851             Str(ref s) => {
852                 let psize = self.memory.pointer_size;
853                 let static_ptr = self.memory.allocate(s.len());
854                 let ptr = self.memory.allocate(psize * 2);
855                 try!(self.memory.write_bytes(static_ptr, s.as_bytes()));
856                 try!(self.memory.write_ptr(ptr, static_ptr));
857                 try!(self.memory.write_usize(ptr.offset(psize as isize), s.len() as u64));
858                 Ok(ptr)
859             }
860             ByteStr(ref bs) => {
861                 let psize = self.memory.pointer_size;
862                 let static_ptr = self.memory.allocate(bs.len());
863                 let ptr = self.memory.allocate(psize);
864                 try!(self.memory.write_bytes(static_ptr, bs));
865                 try!(self.memory.write_ptr(ptr, static_ptr));
866                 Ok(ptr)
867             }
868             Bool(b) => {
869                 let ptr = self.memory.allocate(1);
870                 try!(self.memory.write_bool(ptr, b));
871                 Ok(ptr)
872             }
873             Char(_c)          => unimplemented!(),
874             Struct(_node_id)  => unimplemented!(),
875             Tuple(_node_id)   => unimplemented!(),
876             Function(_def_id) => unimplemented!(),
877             Array(_, _)       => unimplemented!(),
878             Repeat(_, _)      => unimplemented!(),
879             Dummy             => unimplemented!(),
880         }
881     }
882
883     fn lvalue_ty(&self, lvalue: &mir::Lvalue<'tcx>) -> ty::Ty<'tcx> {
884         self.monomorphize(self.mir().lvalue_ty(self.tcx, lvalue).to_ty(self.tcx))
885     }
886
887     fn operand_ty(&self, operand: &mir::Operand<'tcx>) -> ty::Ty<'tcx> {
888         self.monomorphize(self.mir().operand_ty(self.tcx, operand))
889     }
890
891     fn monomorphize(&self, ty: ty::Ty<'tcx>) -> ty::Ty<'tcx> {
892         let substituted = ty.subst(self.tcx, self.substs());
893         infer::normalize_associated_type(self.tcx, &substituted)
894     }
895
896     fn type_needs_drop(&self, ty: ty::Ty<'tcx>) -> bool {
897         self.tcx.type_needs_drop_given_env(ty, &self.tcx.empty_parameter_environment())
898     }
899
900     fn move_(&mut self, src: Pointer, dest: Pointer, ty: ty::Ty<'tcx>) -> EvalResult<()> {
901         let size = self.type_size(ty);
902         try!(self.memory.copy(src, dest, size));
903         if self.type_needs_drop(ty) {
904             try!(self.memory.drop_fill(src, size));
905         }
906         Ok(())
907     }
908
909     fn type_is_sized(&self, ty: ty::Ty<'tcx>) -> bool {
910         ty.is_sized(&self.tcx.empty_parameter_environment(), DUMMY_SP)
911     }
912
913     fn type_size(&self, ty: ty::Ty<'tcx>) -> usize {
914         self.type_layout(ty).size(&self.tcx.data_layout).bytes() as usize
915     }
916
917     fn type_layout(&self, ty: ty::Ty<'tcx>) -> &'tcx Layout {
918         // TODO(tsion): Is this inefficient? Needs investigation.
919         let ty = self.monomorphize(ty);
920
921         let infcx = infer::normalizing_infer_ctxt(self.tcx, &self.tcx.tables, ProjectionMode::Any);
922
923         // TODO(tsion): Report this error properly.
924         ty.layout(&infcx).unwrap()
925     }
926
927     pub fn read_primval(&mut self, ptr: Pointer, ty: ty::Ty<'tcx>) -> EvalResult<PrimVal> {
928         use syntax::ast::{IntTy, UintTy};
929         let val = match ty.sty {
930             ty::TyBool              => PrimVal::Bool(try!(self.memory.read_bool(ptr))),
931             ty::TyInt(IntTy::I8)    => PrimVal::I8(try!(self.memory.read_int(ptr, 1)) as i8),
932             ty::TyInt(IntTy::I16)   => PrimVal::I16(try!(self.memory.read_int(ptr, 2)) as i16),
933             ty::TyInt(IntTy::I32)   => PrimVal::I32(try!(self.memory.read_int(ptr, 4)) as i32),
934             ty::TyInt(IntTy::I64)   => PrimVal::I64(try!(self.memory.read_int(ptr, 8)) as i64),
935             ty::TyUint(UintTy::U8)  => PrimVal::U8(try!(self.memory.read_uint(ptr, 1)) as u8),
936             ty::TyUint(UintTy::U16) => PrimVal::U16(try!(self.memory.read_uint(ptr, 2)) as u16),
937             ty::TyUint(UintTy::U32) => PrimVal::U32(try!(self.memory.read_uint(ptr, 4)) as u32),
938             ty::TyUint(UintTy::U64) => PrimVal::U64(try!(self.memory.read_uint(ptr, 8)) as u64),
939
940             // TODO(tsion): Pick the PrimVal dynamically.
941             ty::TyInt(IntTy::Is)   => PrimVal::I64(try!(self.memory.read_isize(ptr))),
942             ty::TyUint(UintTy::Us) => PrimVal::U64(try!(self.memory.read_usize(ptr))),
943
944             ty::TyRef(_, ty::TypeAndMut { ty, .. }) |
945             ty::TyRawPtr(ty::TypeAndMut { ty, .. }) => {
946                 if self.type_is_sized(ty) {
947                     match self.memory.read_ptr(ptr) {
948                         Ok(p) => PrimVal::AbstractPtr(p),
949                         Err(EvalError::ReadBytesAsPointer) => {
950                             let n = try!(self.memory.read_usize(ptr));
951                             PrimVal::IntegerPtr(n)
952                         }
953                         Err(e) => return Err(e),
954                     }
955                 } else {
956                     panic!("unimplemented: primitive read of fat pointer type: {:?}", ty);
957                 }
958             }
959
960             _ => panic!("primitive read of non-primitive type: {:?}", ty),
961         };
962         Ok(val)
963     }
964
965     fn frame(&self) -> &Frame<'a, 'tcx> {
966         self.stack.last().expect("no call frames exist")
967     }
968
969     fn frame_mut(&mut self) -> &mut Frame<'a, 'tcx> {
970         self.stack.last_mut().expect("no call frames exist")
971     }
972
973     fn mir(&self) -> &mir::Mir<'tcx> {
974         &self.frame().mir
975     }
976
977     fn substs(&self) -> &'tcx Substs<'tcx> {
978         self.substs_stack.last().cloned().unwrap_or_else(|| self.tcx.mk_substs(Substs::empty()))
979     }
980
981     fn load_mir(&self, def_id: DefId) -> CachedMir<'a, 'tcx> {
982         match self.tcx.map.as_local_node_id(def_id) {
983             Some(node_id) => CachedMir::Ref(self.mir_map.map.get(&node_id).unwrap()),
984             None => {
985                 let mut mir_cache = self.mir_cache.borrow_mut();
986                 if let Some(mir) = mir_cache.get(&def_id) {
987                     return CachedMir::Owned(mir.clone());
988                 }
989
990                 use rustc::middle::cstore::CrateStore;
991                 let cs = &self.tcx.sess.cstore;
992                 let mir = cs.maybe_get_item_mir(self.tcx, def_id).unwrap_or_else(|| {
993                     panic!("no mir for {:?}", def_id);
994                 });
995                 let cached = Rc::new(mir);
996                 mir_cache.insert(def_id, cached.clone());
997                 CachedMir::Owned(cached)
998             }
999         }
1000     }
1001
1002     fn fulfill_obligation(&self, trait_ref: ty::PolyTraitRef<'tcx>) -> traits::Vtable<'tcx, ()> {
1003         // Do the initial selection for the obligation. This yields the shallow result we are
1004         // looking for -- that is, what specific impl.
1005         let infcx = infer::normalizing_infer_ctxt(self.tcx, &self.tcx.tables, ProjectionMode::Any);
1006         let mut selcx = traits::SelectionContext::new(&infcx);
1007
1008         let obligation = traits::Obligation::new(
1009             traits::ObligationCause::misc(DUMMY_SP, ast::DUMMY_NODE_ID),
1010             trait_ref.to_poly_trait_predicate(),
1011         );
1012         let selection = selcx.select(&obligation).unwrap().unwrap();
1013
1014         // Currently, we use a fulfillment context to completely resolve all nested obligations.
1015         // This is because they can inform the inference of the impl's type parameters.
1016         let mut fulfill_cx = traits::FulfillmentContext::new();
1017         let vtable = selection.map(|predicate| {
1018             fulfill_cx.register_predicate_obligation(&infcx, predicate);
1019         });
1020         let vtable = infer::drain_fulfillment_cx_or_panic(
1021             DUMMY_SP, &infcx, &mut fulfill_cx, &vtable
1022         );
1023
1024         vtable
1025     }
1026
1027     /// Trait method, which has to be resolved to an impl method.
1028     pub fn trait_method(&self, def_id: DefId, substs: &'tcx Substs<'tcx>)
1029         -> (DefId, &'tcx Substs<'tcx>)
1030     {
1031         let method_item = self.tcx.impl_or_trait_item(def_id);
1032         let trait_id = method_item.container().id();
1033         let trait_ref = ty::Binder(substs.to_trait_ref(self.tcx, trait_id));
1034         match self.fulfill_obligation(trait_ref) {
1035             traits::VtableImpl(vtable_impl) => {
1036                 let impl_did = vtable_impl.impl_def_id;
1037                 let mname = self.tcx.item_name(def_id);
1038                 // Create a concatenated set of substitutions which includes those from the impl
1039                 // and those from the method:
1040                 let impl_substs = vtable_impl.substs.with_method_from(substs);
1041                 let substs = self.tcx.mk_substs(impl_substs);
1042                 let mth = get_impl_method(self.tcx, impl_did, substs, mname);
1043
1044                 (mth.method.def_id, mth.substs)
1045             }
1046
1047             traits::VtableClosure(vtable_closure) =>
1048                 (vtable_closure.closure_def_id, vtable_closure.substs.func_substs),
1049
1050             traits::VtableFnPointer(_fn_ty) => {
1051                 let _trait_closure_kind = self.tcx.lang_items.fn_trait_kind(trait_id).unwrap();
1052                 unimplemented!()
1053                 // let llfn = trans_fn_pointer_shim(ccx, trait_closure_kind, fn_ty);
1054
1055                 // let method_ty = def_ty(tcx, def_id, substs);
1056                 // let fn_ptr_ty = match method_ty.sty {
1057                 //     ty::TyFnDef(_, _, fty) => tcx.mk_ty(ty::TyFnPtr(fty)),
1058                 //     _ => unreachable!("expected fn item type, found {}",
1059                 //                       method_ty)
1060                 // };
1061                 // Callee::ptr(immediate_rvalue(llfn, fn_ptr_ty))
1062             }
1063
1064             traits::VtableObject(ref _data) => {
1065                 unimplemented!()
1066                 // Callee {
1067                 //     data: Virtual(traits::get_vtable_index_of_object_method(
1068                 //                   tcx, data, def_id)),
1069                 //                   ty: def_ty(tcx, def_id, substs)
1070                 // }
1071             }
1072             vtable => unreachable!("resolved vtable bad vtable {:?} in trans", vtable),
1073         }
1074     }
1075 }
1076
1077 fn pointee_type<'tcx>(ptr_ty: ty::Ty<'tcx>) -> Option<ty::Ty<'tcx>> {
1078     match ptr_ty.sty {
1079         ty::TyRef(_, ty::TypeAndMut { ty, .. }) |
1080         ty::TyRawPtr(ty::TypeAndMut { ty, .. }) |
1081         ty::TyBox(ty) => {
1082             Some(ty)
1083         }
1084         _ => None,
1085     }
1086 }
1087
1088 impl Lvalue {
1089     fn to_ptr(self) -> Pointer {
1090         assert_eq!(self.extra, LvalueExtra::None);
1091         self.ptr
1092     }
1093 }
1094
1095 impl<'mir, 'tcx: 'mir> Deref for CachedMir<'mir, 'tcx> {
1096     type Target = mir::Mir<'tcx>;
1097     fn deref(&self) -> &mir::Mir<'tcx> {
1098         match *self {
1099             CachedMir::Ref(r) => r,
1100             CachedMir::Owned(ref rc) => &rc,
1101         }
1102     }
1103 }
1104
1105 #[derive(Debug)]
1106 pub struct ImplMethod<'tcx> {
1107     pub method: Rc<ty::Method<'tcx>>,
1108     pub substs: &'tcx Substs<'tcx>,
1109     pub is_provided: bool,
1110 }
1111
1112 /// Locates the applicable definition of a method, given its name.
1113 pub fn get_impl_method<'tcx>(
1114     tcx: &TyCtxt<'tcx>,
1115     impl_def_id: DefId,
1116     substs: &'tcx Substs<'tcx>,
1117     name: ast::Name,
1118 ) -> ImplMethod<'tcx> {
1119     assert!(!substs.types.needs_infer());
1120
1121     let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap();
1122     let trait_def = tcx.lookup_trait_def(trait_def_id);
1123     let infcx = infer::normalizing_infer_ctxt(tcx, &tcx.tables, ProjectionMode::Any);
1124
1125     match trait_def.ancestors(impl_def_id).fn_defs(tcx, name).next() {
1126         Some(node_item) => {
1127             ImplMethod {
1128                 method: node_item.item,
1129                 substs: traits::translate_substs(&infcx, impl_def_id, substs, node_item.node),
1130                 is_provided: node_item.node.is_from_trait(),
1131             }
1132         }
1133         None => {
1134             bug!("method {:?} not found in {:?}", name, impl_def_id);
1135         }
1136     }
1137 }
1138
1139 pub fn interpret_start_points<'tcx>(tcx: &TyCtxt<'tcx>, mir_map: &MirMap<'tcx>) {
1140     for (&id, mir) in &mir_map.map {
1141         for attr in tcx.map.attrs(id) {
1142             use syntax::attr::AttrMetaMethods;
1143             if attr.check_name("miri_run") {
1144                 let item = tcx.map.expect_item(id);
1145
1146                 println!("Interpreting: {}", item.name);
1147
1148                 let mut miri = Interpreter::new(tcx, mir_map);
1149                 let return_ptr = match mir.return_ty {
1150                     ty::FnConverging(ty) => {
1151                         let size = miri.type_size(ty);
1152                         Some(miri.memory.allocate(size))
1153                     }
1154                     ty::FnDiverging => None,
1155                 };
1156                 let substs = miri.tcx.mk_substs(Substs::empty());
1157                 miri.push_stack_frame(CachedMir::Ref(mir), substs, return_ptr);
1158                 if let Err(_e) = miri.run() {
1159                     // TODO(tsion): Detect whether the error was already reported or not.
1160                     // tcx.sess.err(&e.to_string());
1161                 } else if let Some(ret) = return_ptr {
1162                     miri.memory.dump(ret.alloc_id);
1163                 }
1164                 println!("");
1165             }
1166         }
1167     }
1168 }
1169
1170 // TODO(tsion): Upstream these methods into rustc::ty::layout.
1171
1172 trait IntegerExt {
1173     fn size(self) -> Size;
1174 }
1175
1176 impl IntegerExt for layout::Integer {
1177     fn size(self) -> Size {
1178         use rustc::ty::layout::Integer::*;
1179         match self {
1180             I1 | I8 => Size::from_bits(8),
1181             I16 => Size::from_bits(16),
1182             I32 => Size::from_bits(32),
1183             I64 => Size::from_bits(64),
1184         }
1185     }
1186 }
1187
1188 trait StructExt {
1189     fn field_offset(&self, index: usize) -> Size;
1190 }
1191
1192 impl StructExt for layout::Struct {
1193     fn field_offset(&self, index: usize) -> Size {
1194         if index == 0 {
1195             Size::from_bytes(0)
1196         } else {
1197             self.offset_after_field[index - 1]
1198         }
1199     }
1200 }