]> git.lizzy.rs Git - rust.git/blob - src/base.rs
Fix panic with reported const_eval promoted error
[rust.git] / src / base.rs
1 use crate::prelude::*;
2
3 struct PrintOnPanic<F: Fn() -> String>(F);
4 impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
5     fn drop(&mut self) {
6         if ::std::thread::panicking() {
7             println!("{}", (self.0)());
8         }
9     }
10 }
11
12 pub fn trans_mono_item<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
13     cx: &mut crate::CodegenCx<'a, 'clif, 'tcx, B>,
14     mono_item: MonoItem<'tcx>,
15     linkage: Linkage,
16 ) {
17     let tcx = cx.tcx;
18     match mono_item {
19         MonoItem::Fn(inst) => {
20             let _inst_guard =
21                 PrintOnPanic(|| format!("{:?} {}", inst, tcx.symbol_name(inst).as_str()));
22             debug_assert!(!inst.substs.needs_infer());
23             let _mir_guard = PrintOnPanic(|| {
24                 match inst.def {
25                     InstanceDef::Item(_)
26                     | InstanceDef::DropGlue(_, _)
27                     | InstanceDef::Virtual(_, _)
28                         if inst.def_id().krate == LOCAL_CRATE =>
29                     {
30                         let mut mir = ::std::io::Cursor::new(Vec::new());
31                         crate::rustc_mir::util::write_mir_pretty(
32                             tcx,
33                             Some(inst.def_id()),
34                             &mut mir,
35                         )
36                         .unwrap();
37                         String::from_utf8(mir.into_inner()).unwrap()
38                     }
39                     _ => {
40                         // FIXME fix write_mir_pretty for these instances
41                         format!("{:#?}", tcx.instance_mir(inst.def))
42                     }
43                 }
44             });
45
46             trans_fn(cx, inst, linkage);
47         }
48         MonoItem::Static(def_id) => {
49             crate::constant::codegen_static(&mut cx.ccx, def_id);
50         }
51         MonoItem::GlobalAsm(node_id) => tcx
52             .sess
53             .fatal(&format!("Unimplemented global asm mono item {:?}", node_id)),
54     }
55 }
56
57 fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
58     cx: &mut crate::CodegenCx<'a, 'clif, 'tcx, B>,
59     instance: Instance<'tcx>,
60     linkage: Linkage,
61 ) {
62     let tcx = cx.tcx;
63
64     // Step 1. Get mir
65     let mir = tcx.instance_mir(instance.def);
66
67     // Step 2. Check fn sig for u128 and i128 and replace those functions with a trap.
68     {
69         // Step 2a. Check sig for u128 and i128
70         let fn_ty = instance.ty(tcx);
71         let fn_sig = crate::abi::ty_fn_sig(tcx, fn_ty);
72
73         struct UI128Visitor<'a, 'tcx: 'a>(TyCtxt<'a, 'tcx, 'tcx>, bool);
74
75         impl<'a, 'tcx: 'a> rustc::ty::fold::TypeVisitor<'tcx> for UI128Visitor<'a, 'tcx> {
76             fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
77                 if t.sty == self.0.types.u128.sty || t.sty == self.0.types.i128.sty {
78                     self.1 = true;
79                     return false; // stop visiting
80                 }
81
82                 t.super_visit_with(self)
83             }
84         }
85
86         let mut visitor = UI128Visitor(tcx, false);
87         fn_sig.visit_with(&mut visitor);
88
89         // Step 2b. If found replace function with a trap.
90         if visitor.1 {
91             tcx.sess.warn("u128 and i128 are not yet supported. \
92             Functions using these as args will be replaced with a trap.");
93
94             // Step 2b1. Declare function with fake signature
95             let sig = Signature {
96                 params: vec![AbiParam::new(types::INVALID)],
97                 returns: vec![],
98                 call_conv: CallConv::Fast,
99             };
100             let name = tcx.symbol_name(instance).as_str();
101             let func_id = cx.module.declare_function(&*name, linkage, &sig).unwrap();
102
103             // Step 2b2. Create trapping function
104             let mut func = Function::with_name_signature(ExternalName::user(0, 0), sig);
105             let mut func_ctx = FunctionBuilderContext::new();
106             let mut bcx = FunctionBuilder::new(&mut func, &mut func_ctx);
107             let start_ebb = bcx.create_ebb();
108             bcx.append_ebb_params_for_function_params(start_ebb);
109             bcx.switch_to_block(start_ebb);
110             crate::trap::trap_unreachable(&mut bcx);
111             bcx.seal_all_blocks();
112             bcx.finalize();
113
114             // Step 2b3. Define function
115             cx.caches.context.func = func;
116             cx.module
117                 .define_function(func_id, &mut cx.caches.context)
118                 .unwrap();
119             cx.caches.context.clear();
120             return;
121         }
122     }
123
124     // Step 3. Declare function
125     let (name, sig) = get_function_name_and_sig(tcx, instance, false);
126     let func_id = cx.module.declare_function(&name, linkage, &sig).unwrap();
127     let mut debug_context = cx
128         .debug_context
129         .as_mut()
130         .map(|debug_context| FunctionDebugContext::new(tcx, debug_context, mir, &name, &sig));
131
132     // Step 4. Make FunctionBuilder
133     let mut func = Function::with_name_signature(ExternalName::user(0, 0), sig);
134     let mut func_ctx = FunctionBuilderContext::new();
135     let mut bcx = FunctionBuilder::new(&mut func, &mut func_ctx);
136
137     // Step 5. Predefine ebb's
138     let start_ebb = bcx.create_ebb();
139     let mut ebb_map: HashMap<BasicBlock, Ebb> = HashMap::new();
140     for (bb, _bb_data) in mir.basic_blocks().iter_enumerated() {
141         ebb_map.insert(bb, bcx.create_ebb());
142     }
143
144     // Step 6. Make FunctionCx
145     let pointer_type = cx.module.target_config().pointer_type();
146     let clif_comments = crate::pretty_clif::CommentWriter::new(tcx, instance);
147
148     let mut fx = FunctionCx {
149         tcx,
150         module: cx.module,
151         pointer_type,
152
153         instance,
154         mir,
155
156         bcx,
157         ebb_map,
158         local_map: HashMap::new(),
159
160         clif_comments,
161         constants: &mut cx.ccx,
162         caches: &mut cx.caches,
163         source_info_set: indexmap::IndexSet::new(),
164     };
165
166     // Step 7. Codegen function
167     with_unimpl_span(fx.mir.span, || {
168         crate::abi::codegen_fn_prelude(&mut fx, start_ebb);
169         codegen_fn_content(&mut fx);
170     });
171     let source_info_set = fx.source_info_set.clone();
172
173     // Step 8. Write function to file for debugging
174     #[cfg(debug_assertions)]
175     fx.write_clif_file();
176
177     // Step 9. Verify function
178     verify_func(tcx, fx.clif_comments, &func);
179
180     // Step 10. Define function
181     cx.caches.context.func = func;
182     cx.module
183         .define_function(func_id, &mut cx.caches.context)
184         .unwrap();
185
186     // Step 11. Define debuginfo for function
187     let context = &cx.caches.context;
188     let isa = cx.module.isa();
189     debug_context
190         .as_mut()
191         .map(|x| x.define(tcx, context, isa, &source_info_set));
192
193     // Step 12. Clear context to make it usable for the next function
194     cx.caches.context.clear();
195 }
196
197 fn verify_func(tcx: TyCtxt, writer: crate::pretty_clif::CommentWriter, func: &Function) {
198     let flags = settings::Flags::new(settings::builder());
199     match ::cranelift::codegen::verify_function(&func, &flags) {
200         Ok(_) => {}
201         Err(err) => {
202             tcx.sess.err(&format!("{:?}", err));
203             let pretty_error = ::cranelift::codegen::print_errors::pretty_verifier_error(
204                 &func,
205                 None,
206                 Some(Box::new(&writer)),
207                 err,
208             );
209             tcx.sess
210                 .fatal(&format!("cranelift verify error:\n{}", pretty_error));
211         }
212     }
213 }
214
215 fn codegen_fn_content<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx, impl Backend>) {
216     for (bb, bb_data) in fx.mir.basic_blocks().iter_enumerated() {
217         if bb_data.is_cleanup {
218             // Unwinding after panicking is not supported
219             continue;
220         }
221
222         let ebb = fx.get_ebb(bb);
223         fx.bcx.switch_to_block(ebb);
224
225         fx.bcx.ins().nop();
226         for stmt in &bb_data.statements {
227             fx.set_debug_loc(stmt.source_info);
228             trans_stmt(fx, ebb, stmt);
229         }
230
231         #[cfg(debug_assertions)]
232         {
233             let mut terminator_head = "\n".to_string();
234             bb_data
235                 .terminator()
236                 .kind
237                 .fmt_head(&mut terminator_head)
238                 .unwrap();
239             let inst = fx.bcx.func.layout.last_inst(ebb).unwrap();
240             fx.add_comment(inst, terminator_head);
241         }
242
243         fx.set_debug_loc(bb_data.terminator().source_info);
244
245         match &bb_data.terminator().kind {
246             TerminatorKind::Goto { target } => {
247                 let ebb = fx.get_ebb(*target);
248                 fx.bcx.ins().jump(ebb, &[]);
249             }
250             TerminatorKind::Return => {
251                 crate::abi::codegen_return(fx);
252             }
253             TerminatorKind::Assert {
254                 cond,
255                 expected,
256                 msg: _,
257                 target,
258                 cleanup: _,
259             } => {
260                 let cond = trans_operand(fx, cond).load_scalar(fx);
261                 // TODO HACK brz/brnz for i8/i16 is not yet implemented
262                 let cond = fx.bcx.ins().uextend(types::I32, cond);
263                 let target = fx.get_ebb(*target);
264                 if *expected {
265                     fx.bcx.ins().brnz(cond, target, &[]);
266                 } else {
267                     fx.bcx.ins().brz(cond, target, &[]);
268                 };
269                 trap_panic(&mut fx.bcx);
270             }
271
272             TerminatorKind::SwitchInt {
273                 discr,
274                 switch_ty: _,
275                 values,
276                 targets,
277             } => {
278                 let discr = trans_operand(fx, discr).load_scalar(fx);
279                 let mut switch = ::cranelift::frontend::Switch::new();
280                 for (i, value) in values.iter().enumerate() {
281                     let ebb = fx.get_ebb(targets[i]);
282                     switch.set_entry(*value as u64, ebb);
283                 }
284                 let otherwise_ebb = fx.get_ebb(targets[targets.len() - 1]);
285                 switch.emit(&mut fx.bcx, discr, otherwise_ebb);
286             }
287             TerminatorKind::Call {
288                 func,
289                 args,
290                 destination,
291                 cleanup: _,
292                 from_hir_call: _,
293             } => {
294                 crate::abi::codegen_terminator_call(fx, func, args, destination);
295             }
296             TerminatorKind::Resume | TerminatorKind::Abort | TerminatorKind::Unreachable => {
297                 trap_unreachable(&mut fx.bcx);
298             }
299             TerminatorKind::Yield { .. }
300             | TerminatorKind::FalseEdges { .. }
301             | TerminatorKind::FalseUnwind { .. }
302             | TerminatorKind::DropAndReplace { .. }
303             | TerminatorKind::GeneratorDrop => {
304                 bug!("shouldn't exist at trans {:?}", bb_data.terminator());
305             }
306             TerminatorKind::Drop {
307                 location,
308                 target,
309                 unwind: _,
310             } => {
311                 let ty = location.ty(fx.mir, fx.tcx).to_ty(fx.tcx);
312                 let ty = fx.monomorphize(&ty);
313                 let drop_fn = crate::rustc_mir::monomorphize::resolve_drop_in_place(fx.tcx, ty);
314
315                 if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
316                     // we don't actually need to drop anything
317                 } else {
318                     let drop_place = trans_place(fx, location);
319                     let drop_fn_ty = drop_fn.ty(fx.tcx);
320                     match ty.sty {
321                         ty::Dynamic(..) => {
322                             crate::abi::codegen_drop(fx, drop_place, drop_fn_ty);
323                         }
324                         _ => {
325                             let arg_place = CPlace::new_stack_slot(
326                                 fx,
327                                 fx.tcx.mk_ref(
328                                     &ty::RegionKind::ReErased,
329                                     TypeAndMut {
330                                         ty,
331                                         mutbl: crate::rustc::hir::Mutability::MutMutable,
332                                     },
333                                 ),
334                             );
335                             drop_place.write_place_ref(fx, arg_place);
336                             let arg_value = arg_place.to_cvalue(fx);
337                             crate::abi::codegen_call_inner(
338                                 fx,
339                                 None,
340                                 drop_fn_ty,
341                                 vec![arg_value],
342                                 None,
343                             );
344                         }
345                     }
346                 }
347
348                 let target_ebb = fx.get_ebb(*target);
349                 fx.bcx.ins().jump(target_ebb, &[]);
350             }
351         };
352     }
353
354     fx.bcx.seal_all_blocks();
355     fx.bcx.finalize();
356 }
357
358 fn trans_stmt<'a, 'tcx: 'a>(
359     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
360     cur_ebb: Ebb,
361     stmt: &Statement<'tcx>,
362 ) {
363     let _print_guard = PrintOnPanic(|| format!("stmt {:?}", stmt));
364
365     fx.set_debug_loc(stmt.source_info);
366
367     #[cfg(debug_assertions)]
368     match &stmt.kind {
369         StatementKind::StorageLive(..) | StatementKind::StorageDead(..) => {} // Those are not very useful
370         _ => {
371             let inst = fx.bcx.func.layout.last_inst(cur_ebb).unwrap();
372             fx.add_comment(inst, format!("{:?}", stmt));
373         }
374     }
375
376     match &stmt.kind {
377         StatementKind::SetDiscriminant {
378             place,
379             variant_index,
380         } => {
381             let place = trans_place(fx, place);
382             let layout = place.layout();
383             if layout.for_variant(&*fx, *variant_index).abi == layout::Abi::Uninhabited {
384                 return;
385             }
386             match layout.variants {
387                 layout::Variants::Single { index } => {
388                     assert_eq!(index, *variant_index);
389                 }
390                 layout::Variants::Tagged { .. } => {
391                     let ptr = place.place_field(fx, mir::Field::new(0));
392                     let to = layout
393                         .ty
394                         .ty_adt_def()
395                         .unwrap()
396                         .discriminant_for_variant(fx.tcx, *variant_index)
397                         .val;
398                     let discr = CValue::const_val(fx, ptr.layout().ty, to as u64 as i64);
399                     ptr.write_cvalue(fx, discr);
400                 }
401                 layout::Variants::NicheFilling {
402                     dataful_variant,
403                     ref niche_variants,
404                     niche_start,
405                     ..
406                 } => {
407                     if *variant_index != dataful_variant {
408                         let niche = place.place_field(fx, mir::Field::new(0));
409                         //let niche_llty = niche.layout.immediate_llvm_type(bx.cx);
410                         let niche_value =
411                             ((variant_index.as_u32() - niche_variants.start().as_u32()) as u128)
412                                 .wrapping_add(niche_start);
413                         // FIXME(eddyb) Check the actual primitive type here.
414                         let niche_llval = if niche_value == 0 {
415                             CValue::const_val(fx, niche.layout().ty, 0)
416                         } else {
417                             CValue::const_val(fx, niche.layout().ty, niche_value as u64 as i64)
418                         };
419                         niche.write_cvalue(fx, niche_llval);
420                     }
421                 }
422             }
423         }
424         StatementKind::Assign(to_place, rval) => {
425             let lval = trans_place(fx, to_place);
426             let dest_layout = lval.layout();
427             match &**rval {
428                 Rvalue::Use(operand) => {
429                     let val = trans_operand(fx, operand);
430                     lval.write_cvalue(fx, val);
431                 }
432                 Rvalue::Ref(_, _, place) => {
433                     let place = trans_place(fx, place);
434                     place.write_place_ref(fx, lval);
435                 }
436                 Rvalue::BinaryOp(bin_op, lhs, rhs) => {
437                     let ty = fx.monomorphize(&lhs.ty(fx.mir, fx.tcx));
438                     let lhs = trans_operand(fx, lhs);
439                     let rhs = trans_operand(fx, rhs);
440
441                     let res = match ty.sty {
442                         ty::Bool => trans_bool_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
443                         ty::Uint(_) => {
444                             trans_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, false)
445                         }
446                         ty::Int(_) => {
447                             trans_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, true)
448                         }
449                         ty::Float(_) => trans_float_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
450                         ty::Char => trans_char_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
451                         ty::RawPtr(..) => trans_ptr_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
452                         ty::FnPtr(..) => trans_ptr_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
453                         _ => unimplemented!("binop {:?} for {:?}", bin_op, ty),
454                     };
455                     lval.write_cvalue(fx, res);
456                 }
457                 Rvalue::CheckedBinaryOp(bin_op, lhs, rhs) => {
458                     let ty = fx.monomorphize(&lhs.ty(fx.mir, fx.tcx));
459                     let lhs = trans_operand(fx, lhs);
460                     let rhs = trans_operand(fx, rhs);
461
462                     let res = match ty.sty {
463                         ty::Uint(_) => {
464                             trans_checked_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, false)
465                         }
466                         ty::Int(_) => {
467                             trans_checked_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, true)
468                         }
469                         _ => unimplemented!("checked binop {:?} for {:?}", bin_op, ty),
470                     };
471                     lval.write_cvalue(fx, res);
472                 }
473                 Rvalue::UnaryOp(un_op, operand) => {
474                     let operand = trans_operand(fx, operand);
475                     let layout = operand.layout();
476                     let val = operand.load_scalar(fx);
477                     let res = match un_op {
478                         UnOp::Not => {
479                             match layout.ty.sty {
480                                 ty::Bool => {
481                                     let val = fx.bcx.ins().uextend(types::I32, val); // WORKAROUND for CraneStation/cranelift#466
482                                     let res = fx.bcx.ins().icmp_imm(IntCC::Equal, val, 0);
483                                     fx.bcx.ins().bint(types::I8, res)
484                                 }
485                                 ty::Uint(_) | ty::Int(_) => fx.bcx.ins().bnot(val),
486                                 _ => unimplemented!("un op Not for {:?}", layout.ty),
487                             }
488                         }
489                         UnOp::Neg => match layout.ty.sty {
490                             ty::Int(_) => {
491                                 let clif_ty = fx.clif_type(layout.ty).unwrap();
492                                 let zero = fx.bcx.ins().iconst(clif_ty, 0);
493                                 fx.bcx.ins().isub(zero, val)
494                             }
495                             ty::Float(_) => fx.bcx.ins().fneg(val),
496                             _ => unimplemented!("un op Neg for {:?}", layout.ty),
497                         },
498                     };
499                     lval.write_cvalue(fx, CValue::ByVal(res, layout));
500                 }
501                 Rvalue::Cast(CastKind::ReifyFnPointer, operand, ty) => {
502                     let layout = fx.layout_of(ty);
503                     match fx
504                         .monomorphize(&operand.ty(&fx.mir.local_decls, fx.tcx))
505                         .sty
506                     {
507                         ty::FnDef(def_id, substs) => {
508                             let func_ref = fx.get_function_ref(
509                                 Instance::resolve(fx.tcx, ParamEnv::reveal_all(), def_id, substs)
510                                     .unwrap(),
511                             );
512                             let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);
513                             lval.write_cvalue(fx, CValue::ByVal(func_addr, layout));
514                         }
515                         _ => bug!("Trying to ReifyFnPointer on non FnDef {:?}", ty),
516                     }
517                 }
518                 Rvalue::Cast(CastKind::UnsafeFnPointer, operand, ty)
519                 | Rvalue::Cast(CastKind::MutToConstPointer, operand, ty) => {
520                     let operand = trans_operand(fx, operand);
521                     let layout = fx.layout_of(ty);
522                     lval.write_cvalue(fx, operand.unchecked_cast_to(layout));
523                 }
524                 Rvalue::Cast(CastKind::Misc, operand, to_ty) => {
525                     let operand = trans_operand(fx, operand);
526                     let from_ty = operand.layout().ty;
527
528                     fn is_fat_ptr<'a, 'tcx: 'a>(fx: &FunctionCx<'a, 'tcx, impl Backend>, ty: Ty<'tcx>) -> bool {
529                         ty
530                             .builtin_deref(true)
531                             .map(|ty::TypeAndMut {ty: pointee_ty, mutbl: _ }| fx.layout_of(pointee_ty).is_unsized())
532                             .unwrap_or(false)
533                     }
534
535                     if is_fat_ptr(fx, from_ty) {
536                         if is_fat_ptr(fx, to_ty) {
537                             // fat-ptr -> fat-ptr
538                             lval.write_cvalue(fx, operand.unchecked_cast_to(dest_layout));
539                         } else {
540                             // fat-ptr -> thin-ptr
541                             let (ptr, _extra) = operand.load_scalar_pair(fx);
542                             lval.write_cvalue(fx, CValue::ByVal(ptr, dest_layout))
543                         }
544                     } else if let ty::Adt(adt_def, _substs) = from_ty.sty {
545                         // enum -> discriminant value
546                         assert!(adt_def.is_enum());
547                         match to_ty.sty {
548                             ty::Uint(_) | ty::Int(_) => {},
549                             _ => unreachable!("cast adt {} -> {}", from_ty, to_ty),
550                         }
551
552                         // FIXME avoid forcing to stack
553                         let place =
554                             CPlace::Addr(operand.force_stack(fx), None, operand.layout());
555                         let discr = trans_get_discriminant(fx, place, fx.layout_of(to_ty));
556                         lval.write_cvalue(fx, discr);
557                     } else {
558                         let from_clif_ty = fx.clif_type(from_ty).unwrap();
559                         let to_clif_ty = fx.clif_type(to_ty).unwrap();
560                         let from = operand.load_scalar(fx);
561
562                         let signed = match from_ty.sty {
563                             ty::Ref(..) | ty::RawPtr(..) | ty::FnPtr(..) | ty::Char | ty::Uint(..) | ty::Bool => false,
564                             ty::Int(..) => true,
565                             ty::Float(..) => false, // `signed` is unused for floats
566                             _ => panic!("{}", from_ty),
567                         };
568
569                         let res = if from_clif_ty.is_int() && to_clif_ty.is_int() {
570                             // int-like -> int-like
571                             crate::common::clif_intcast(
572                                 fx,
573                                 from,
574                                 to_clif_ty,
575                                 signed,
576                             )
577                         } else if from_clif_ty.is_int() && to_clif_ty.is_float() {
578                             // int-like -> float
579                             // FIXME missing encoding for fcvt_from_sint.f32.i8
580                             let from = if from_clif_ty == types::I8 || from_clif_ty == types::I16 {
581                                 fx.bcx.ins().uextend(types::I32, from)
582                             } else {
583                                 from
584                             };
585                             if signed {
586                                 fx.bcx.ins().fcvt_from_sint(to_clif_ty, from)
587                             } else {
588                                 fx.bcx.ins().fcvt_from_uint(to_clif_ty, from)
589                             }
590                         } else if from_clif_ty.is_float() && to_clif_ty.is_int() {
591                             // float -> int-like
592                             let from = operand.load_scalar(fx);
593                             if signed {
594                                 fx.bcx.ins().fcvt_to_sint_sat(to_clif_ty, from)
595                             } else {
596                                 fx.bcx.ins().fcvt_to_uint_sat(to_clif_ty, from)
597                             }
598                         } else if from_clif_ty.is_float() && to_clif_ty.is_float() {
599                             // float -> float
600                             match (from_clif_ty, to_clif_ty) {
601                                 (types::F32, types::F64) => {
602                                     fx.bcx.ins().fpromote(types::F64, from)
603                                 }
604                                 (types::F64, types::F32) => {
605                                     fx.bcx.ins().fdemote(types::F32, from)
606                                 }
607                                 _ => from,
608                             }
609                         } else {
610                             unimpl!("rval misc {:?} {:?}", from_ty, to_ty)
611                         };
612                         lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
613                     }
614                 }
615                 Rvalue::Cast(CastKind::ClosureFnPointer, operand, _ty) => {
616                     let operand = trans_operand(fx, operand);
617                     match operand.layout().ty.sty {
618                         ty::Closure(def_id, substs) => {
619                             let instance = rustc_mir::monomorphize::resolve_closure(
620                                 fx.tcx,
621                                 def_id,
622                                 substs,
623                                 ty::ClosureKind::FnOnce,
624                             );
625                             let func_ref = fx.get_function_ref(instance);
626                             let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);
627                             lval.write_cvalue(fx, CValue::ByVal(func_addr, lval.layout()));
628                         }
629                         _ => {
630                             bug!("{} cannot be cast to a fn ptr", operand.layout().ty)
631                         }
632                     }
633                 }
634                 Rvalue::Cast(CastKind::Unsize, operand, _ty) => {
635                     let operand = trans_operand(fx, operand);
636                     operand.unsize_value(fx, lval);
637                 }
638                 Rvalue::Discriminant(place) => {
639                     let place = trans_place(fx, place);
640                     let discr = trans_get_discriminant(fx, place, dest_layout);
641                     lval.write_cvalue(fx, discr);
642                 }
643                 Rvalue::Repeat(operand, times) => {
644                     let operand = trans_operand(fx, operand);
645                     for i in 0..*times {
646                         let index = fx.bcx.ins().iconst(fx.pointer_type, i as i64);
647                         let to = lval.place_index(fx, index);
648                         to.write_cvalue(fx, operand);
649                     }
650                 }
651                 Rvalue::Len(place) => {
652                     let place = trans_place(fx, place);
653                     let usize_layout = fx.layout_of(fx.tcx.types.usize);
654                     let len = codegen_array_len(fx, place);
655                     lval.write_cvalue(fx, CValue::ByVal(len, usize_layout));
656                 }
657                 Rvalue::NullaryOp(NullOp::Box, content_ty) => {
658                     use rustc::middle::lang_items::ExchangeMallocFnLangItem;
659
660                     let usize_type = fx.clif_type(fx.tcx.types.usize).unwrap();
661                     let layout = fx.layout_of(content_ty);
662                     let llsize = fx.bcx.ins().iconst(usize_type, layout.size.bytes() as i64);
663                     let llalign = fx
664                         .bcx
665                         .ins()
666                         .iconst(usize_type, layout.align.abi.bytes() as i64);
667                     let box_layout = fx.layout_of(fx.tcx.mk_box(content_ty));
668
669                     // Allocate space:
670                     let def_id = match fx.tcx.lang_items().require(ExchangeMallocFnLangItem) {
671                         Ok(id) => id,
672                         Err(s) => {
673                             fx.tcx
674                                 .sess
675                                 .fatal(&format!("allocation of `{}` {}", box_layout.ty, s));
676                         }
677                     };
678                     let instance = ty::Instance::mono(fx.tcx, def_id);
679                     let func_ref = fx.get_function_ref(instance);
680                     let call = fx.bcx.ins().call(func_ref, &[llsize, llalign]);
681                     let ptr = fx.bcx.inst_results(call)[0];
682                     lval.write_cvalue(fx, CValue::ByVal(ptr, box_layout));
683                 }
684                 Rvalue::NullaryOp(NullOp::SizeOf, ty) => {
685                     assert!(lval
686                         .layout()
687                         .ty
688                         .is_sized(fx.tcx.at(DUMMY_SP), ParamEnv::reveal_all()));
689                     let ty_size = fx.layout_of(ty).size.bytes();
690                     let val = CValue::const_val(fx, fx.tcx.types.usize, ty_size as i64);
691                     lval.write_cvalue(fx, val);
692                 }
693                 Rvalue::Aggregate(kind, operands) => match **kind {
694                     AggregateKind::Array(_ty) => {
695                         for (i, operand) in operands.into_iter().enumerate() {
696                             let operand = trans_operand(fx, operand);
697                             let index = fx.bcx.ins().iconst(fx.pointer_type, i as i64);
698                             let to = lval.place_index(fx, index);
699                             to.write_cvalue(fx, operand);
700                         }
701                     }
702                     _ => unimpl!("shouldn't exist at trans {:?}", rval),
703                 },
704             }
705         }
706         StatementKind::StorageLive(_)
707         | StatementKind::StorageDead(_)
708         | StatementKind::Nop
709         | StatementKind::FakeRead(..)
710         | StatementKind::Retag { .. }
711         | StatementKind::AscribeUserType(..) => {}
712
713         StatementKind::InlineAsm { .. } => unimpl!("Inline assembly is not supported"),
714     }
715 }
716
717 fn codegen_array_len<'a, 'tcx: 'a>(
718     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
719     place: CPlace<'tcx>,
720 ) -> Value {
721     match place.layout().ty.sty {
722         ty::Array(_elem_ty, len) => {
723             let len = crate::constant::force_eval_const(fx, len).unwrap_usize(fx.tcx) as i64;
724             fx.bcx.ins().iconst(fx.pointer_type, len)
725         }
726         ty::Slice(_elem_ty) => place
727             .to_addr_maybe_unsized(fx)
728             .1
729             .expect("Length metadata for slice place"),
730         _ => bug!("Rvalue::Len({:?})", place),
731     }
732 }
733
734 pub fn trans_get_discriminant<'a, 'tcx: 'a>(
735     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
736     place: CPlace<'tcx>,
737     dest_layout: TyLayout<'tcx>,
738 ) -> CValue<'tcx> {
739     let layout = place.layout();
740
741     if layout.abi == layout::Abi::Uninhabited {
742         return trap_unreachable_ret_value(fx, dest_layout);
743     }
744     match layout.variants {
745         layout::Variants::Single { index } => {
746             let discr_val = layout
747                 .ty
748                 .ty_adt_def()
749                 .map_or(index.as_u32() as u128, |def| {
750                     def.discriminant_for_variant(fx.tcx, index).val
751                 });
752             return CValue::const_val(fx, dest_layout.ty, discr_val as u64 as i64);
753         }
754         layout::Variants::Tagged { .. } | layout::Variants::NicheFilling { .. } => {}
755     }
756
757     let discr = place.place_field(fx, mir::Field::new(0)).to_cvalue(fx);
758     let discr_ty = discr.layout().ty;
759     let lldiscr = discr.load_scalar(fx);
760     match layout.variants {
761         layout::Variants::Single { .. } => bug!(),
762         layout::Variants::Tagged { ref tag, .. } => {
763             let signed = match tag.value {
764                 layout::Int(_, signed) => signed,
765                 _ => false,
766             };
767             let val = clif_intcast(fx, lldiscr, fx.clif_type(dest_layout.ty).unwrap(), signed);
768             return CValue::ByVal(val, dest_layout);
769         }
770         layout::Variants::NicheFilling {
771             dataful_variant,
772             ref niche_variants,
773             niche_start,
774             ..
775         } => {
776             let niche_llty = fx.clif_type(discr_ty).unwrap();
777             let dest_clif_ty = fx.clif_type(dest_layout.ty).unwrap();
778             if niche_variants.start() == niche_variants.end() {
779                 let b = fx
780                     .bcx
781                     .ins()
782                     .icmp_imm(IntCC::Equal, lldiscr, niche_start as u64 as i64);
783                 let if_true = fx
784                     .bcx
785                     .ins()
786                     .iconst(dest_clif_ty, niche_variants.start().as_u32() as i64);
787                 let if_false = fx
788                     .bcx
789                     .ins()
790                     .iconst(dest_clif_ty, dataful_variant.as_u32() as i64);
791                 let val = fx.bcx.ins().select(b, if_true, if_false);
792                 return CValue::ByVal(val, dest_layout);
793             } else {
794                 // Rebase from niche values to discriminant values.
795                 let delta = niche_start.wrapping_sub(niche_variants.start().as_u32() as u128);
796                 let delta = fx.bcx.ins().iconst(niche_llty, delta as u64 as i64);
797                 let lldiscr = fx.bcx.ins().isub(lldiscr, delta);
798                 let b = fx.bcx.ins().icmp_imm(
799                     IntCC::UnsignedLessThanOrEqual,
800                     lldiscr,
801                     niche_variants.end().as_u32() as i64,
802                 );
803                 let if_true =
804                     clif_intcast(fx, lldiscr, fx.clif_type(dest_layout.ty).unwrap(), false);
805                 let if_false = fx
806                     .bcx
807                     .ins()
808                     .iconst(dest_clif_ty, dataful_variant.as_u32() as i64);
809                 let val = fx.bcx.ins().select(b, if_true, if_false);
810                 return CValue::ByVal(val, dest_layout);
811             }
812         }
813     }
814 }
815
816 macro_rules! binop_match {
817     (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, bug) => {
818         bug!("binop {} on {} lhs: {:?} rhs: {:?}", stringify!($var), $bug_fmt, $lhs, $rhs)
819     };
820     (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, icmp($cc:ident)) => {{
821         assert_eq!($fx.tcx.types.bool, $ret_ty);
822         let ret_layout = $fx.layout_of($ret_ty);
823
824         let b = $fx.bcx.ins().icmp(IntCC::$cc, $lhs, $rhs);
825         CValue::ByVal($fx.bcx.ins().bint(types::I8, b), ret_layout)
826     }};
827     (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, fcmp($cc:ident)) => {{
828         assert_eq!($fx.tcx.types.bool, $ret_ty);
829         let ret_layout = $fx.layout_of($ret_ty);
830         let b = $fx.bcx.ins().fcmp(FloatCC::$cc, $lhs, $rhs);
831         CValue::ByVal($fx.bcx.ins().bint(types::I8, b), ret_layout)
832     }};
833     (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, custom(|| $body:expr)) => {{
834         $body
835     }};
836     (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, $name:ident) => {{
837         let ret_layout = $fx.layout_of($ret_ty);
838         CValue::ByVal($fx.bcx.ins().$name($lhs, $rhs), ret_layout)
839     }};
840     (
841         $fx:expr, $bin_op:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, $bug_fmt:expr;
842         $(
843             $var:ident ($sign:pat) $name:tt $( ( $($next:tt)* ) )? ;
844         )*
845     ) => {{
846         let lhs = $lhs.load_scalar($fx);
847         let rhs = $rhs.load_scalar($fx);
848         match ($bin_op, $signed) {
849             $(
850                 (BinOp::$var, $sign) => binop_match!(@single $fx, $bug_fmt, $var, $signed, lhs, rhs, $ret_ty, $name $( ( $($next)* ) )?),
851             )*
852         }
853     }}
854 }
855
856 fn trans_bool_binop<'a, 'tcx: 'a>(
857     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
858     bin_op: BinOp,
859     lhs: CValue<'tcx>,
860     rhs: CValue<'tcx>,
861     ty: Ty<'tcx>,
862 ) -> CValue<'tcx> {
863     let res = binop_match! {
864         fx, bin_op, false, lhs, rhs, ty, "bool";
865         Add (_) bug;
866         Sub (_) bug;
867         Mul (_) bug;
868         Div (_) bug;
869         Rem (_) bug;
870         BitXor (_) bxor;
871         BitAnd (_) band;
872         BitOr (_) bor;
873         Shl (_) bug;
874         Shr (_) bug;
875
876         Eq (_) icmp(Equal);
877         Lt (_) icmp(UnsignedLessThan);
878         Le (_) icmp(UnsignedLessThanOrEqual);
879         Ne (_) icmp(NotEqual);
880         Ge (_) icmp(UnsignedGreaterThanOrEqual);
881         Gt (_) icmp(UnsignedGreaterThan);
882
883         Offset (_) bug;
884     };
885
886     res
887 }
888
889 pub fn trans_int_binop<'a, 'tcx: 'a>(
890     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
891     bin_op: BinOp,
892     lhs: CValue<'tcx>,
893     rhs: CValue<'tcx>,
894     out_ty: Ty<'tcx>,
895     signed: bool,
896 ) -> CValue<'tcx> {
897     if bin_op != BinOp::Shl && bin_op != BinOp::Shr {
898         assert_eq!(
899             lhs.layout().ty,
900             rhs.layout().ty,
901             "int binop requires lhs and rhs of same type"
902         );
903     }
904     binop_match! {
905         fx, bin_op, signed, lhs, rhs, out_ty, "int/uint";
906         Add (_) iadd;
907         Sub (_) isub;
908         Mul (_) imul;
909         Div (false) udiv;
910         Div (true) sdiv;
911         Rem (false) urem;
912         Rem (true) srem;
913         BitXor (_) bxor;
914         BitAnd (_) band;
915         BitOr (_) bor;
916         Shl (_) ishl;
917         Shr (false) ushr;
918         Shr (true) sshr;
919
920         Eq (_) icmp(Equal);
921         Lt (false) icmp(UnsignedLessThan);
922         Lt (true) icmp(SignedLessThan);
923         Le (false) icmp(UnsignedLessThanOrEqual);
924         Le (true) icmp(SignedLessThanOrEqual);
925         Ne (_) icmp(NotEqual);
926         Ge (false) icmp(UnsignedGreaterThanOrEqual);
927         Ge (true) icmp(SignedGreaterThanOrEqual);
928         Gt (false) icmp(UnsignedGreaterThan);
929         Gt (true) icmp(SignedGreaterThan);
930
931         Offset (_) bug;
932     }
933 }
934
935 pub fn trans_checked_int_binop<'a, 'tcx: 'a>(
936     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
937     bin_op: BinOp,
938     in_lhs: CValue<'tcx>,
939     in_rhs: CValue<'tcx>,
940     out_ty: Ty<'tcx>,
941     signed: bool,
942 ) -> CValue<'tcx> {
943     if bin_op != BinOp::Shl && bin_op != BinOp::Shr {
944         assert_eq!(
945             in_lhs.layout().ty,
946             in_rhs.layout().ty,
947             "checked int binop requires lhs and rhs of same type"
948         );
949     }
950
951     let lhs = in_lhs.load_scalar(fx);
952     let rhs = in_rhs.load_scalar(fx);
953     let res = match bin_op {
954         BinOp::Add => fx.bcx.ins().iadd(lhs, rhs),
955         BinOp::Sub => fx.bcx.ins().isub(lhs, rhs),
956         BinOp::Mul => fx.bcx.ins().imul(lhs, rhs),
957         BinOp::Shl => fx.bcx.ins().ishl(lhs, rhs),
958         BinOp::Shr => {
959             if !signed {
960                 fx.bcx.ins().ushr(lhs, rhs)
961             } else {
962                 fx.bcx.ins().sshr(lhs, rhs)
963             }
964         }
965         _ => bug!(
966             "binop {:?} on checked int/uint lhs: {:?} rhs: {:?}",
967             bin_op,
968             in_lhs,
969             in_rhs
970         ),
971     };
972
973     // TODO: check for overflow
974     let has_overflow = fx.bcx.ins().iconst(types::I8, 0);
975
976     let out_place = CPlace::new_stack_slot(fx, out_ty);
977     let out_layout = out_place.layout();
978     out_place.write_cvalue(fx, CValue::ByValPair(res, has_overflow, out_layout));
979
980     out_place.to_cvalue(fx)
981 }
982
983 fn trans_float_binop<'a, 'tcx: 'a>(
984     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
985     bin_op: BinOp,
986     lhs: CValue<'tcx>,
987     rhs: CValue<'tcx>,
988     ty: Ty<'tcx>,
989 ) -> CValue<'tcx> {
990     let res = binop_match! {
991         fx, bin_op, false, lhs, rhs, ty, "float";
992         Add (_) fadd;
993         Sub (_) fsub;
994         Mul (_) fmul;
995         Div (_) fdiv;
996         Rem (_) custom(|| {
997             assert_eq!(lhs.layout().ty, ty);
998             assert_eq!(rhs.layout().ty, ty);
999             match ty.sty {
1000                 ty::Float(FloatTy::F32) => fx.easy_call("fmodf", &[lhs, rhs], ty),
1001                 ty::Float(FloatTy::F64) => fx.easy_call("fmod", &[lhs, rhs], ty),
1002                 _ => bug!(),
1003             }
1004         });
1005         BitXor (_) bxor;
1006         BitAnd (_) band;
1007         BitOr (_) bor;
1008         Shl (_) bug;
1009         Shr (_) bug;
1010
1011         Eq (_) fcmp(Equal);
1012         Lt (_) fcmp(LessThan);
1013         Le (_) fcmp(LessThanOrEqual);
1014         Ne (_) fcmp(NotEqual);
1015         Ge (_) fcmp(GreaterThanOrEqual);
1016         Gt (_) fcmp(GreaterThan);
1017
1018         Offset (_) bug;
1019     };
1020
1021     res
1022 }
1023
1024 fn trans_char_binop<'a, 'tcx: 'a>(
1025     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
1026     bin_op: BinOp,
1027     lhs: CValue<'tcx>,
1028     rhs: CValue<'tcx>,
1029     ty: Ty<'tcx>,
1030 ) -> CValue<'tcx> {
1031     let res = binop_match! {
1032         fx, bin_op, false, lhs, rhs, ty, "char";
1033         Add (_) bug;
1034         Sub (_) bug;
1035         Mul (_) bug;
1036         Div (_) bug;
1037         Rem (_) bug;
1038         BitXor (_) bug;
1039         BitAnd (_) bug;
1040         BitOr (_) bug;
1041         Shl (_) bug;
1042         Shr (_) bug;
1043
1044         Eq (_) icmp(Equal);
1045         Lt (_) icmp(UnsignedLessThan);
1046         Le (_) icmp(UnsignedLessThanOrEqual);
1047         Ne (_) icmp(NotEqual);
1048         Ge (_) icmp(UnsignedGreaterThanOrEqual);
1049         Gt (_) icmp(UnsignedGreaterThan);
1050
1051         Offset (_) bug;
1052     };
1053
1054     res
1055 }
1056
1057 fn trans_ptr_binop<'a, 'tcx: 'a>(
1058     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
1059     bin_op: BinOp,
1060     lhs: CValue<'tcx>,
1061     rhs: CValue<'tcx>,
1062     ret_ty: Ty<'tcx>,
1063 ) -> CValue<'tcx> {
1064     let not_fat = match lhs.layout().ty.sty {
1065         ty::RawPtr(TypeAndMut { ty, mutbl: _ }) => {
1066             ty.is_sized(fx.tcx.at(DUMMY_SP), ParamEnv::reveal_all())
1067         }
1068         ty::FnPtr(..) => true,
1069         _ => bug!("trans_ptr_binop on non ptr"),
1070     };
1071     if not_fat {
1072         if let BinOp::Offset = bin_op {
1073             let (base, offset) = (lhs, rhs.load_scalar(fx));
1074             let pointee_ty = base.layout().ty.builtin_deref(true).unwrap().ty;
1075             let pointee_size = fx.layout_of(pointee_ty).size.bytes();
1076             let ptr_diff = fx.bcx.ins().imul_imm(offset, pointee_size as i64);
1077             let base_val = base.load_scalar(fx);
1078             let res = fx.bcx.ins().iadd(base_val, ptr_diff);
1079             return CValue::ByVal(res, base.layout());
1080         }
1081
1082         binop_match! {
1083             fx, bin_op, false, lhs, rhs, ret_ty, "ptr";
1084             Add (_) bug;
1085             Sub (_) bug;
1086             Mul (_) bug;
1087             Div (_) bug;
1088             Rem (_) bug;
1089             BitXor (_) bug;
1090             BitAnd (_) bug;
1091             BitOr (_) bug;
1092             Shl (_) bug;
1093             Shr (_) bug;
1094
1095             Eq (_) icmp(Equal);
1096             Lt (_) icmp(UnsignedLessThan);
1097             Le (_) icmp(UnsignedLessThanOrEqual);
1098             Ne (_) icmp(NotEqual);
1099             Ge (_) icmp(UnsignedGreaterThanOrEqual);
1100             Gt (_) icmp(UnsignedGreaterThan);
1101
1102             Offset (_) bug; // Handled above
1103         }
1104     } else {
1105         let (lhs_ptr, lhs_extra) = lhs.load_scalar_pair(fx);
1106         let (rhs_ptr, rhs_extra) = rhs.load_scalar_pair(fx);
1107         let res = match bin_op {
1108             BinOp::Eq => {
1109                 let ptr_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_ptr, rhs_ptr);
1110                 let extra_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_extra, rhs_extra);
1111                 fx.bcx.ins().band(ptr_eq, extra_eq)
1112             }
1113             BinOp::Ne => {
1114                 let ptr_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_ptr, rhs_ptr);
1115                 let extra_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_extra, rhs_extra);
1116                 fx.bcx.ins().bor(ptr_ne, extra_ne)
1117             }
1118             _ => unimplemented!(
1119                 "trans_ptr_binop({:?}, <fat ptr>, <fat ptr>) not implemented",
1120                 bin_op
1121             ),
1122         };
1123
1124         assert_eq!(fx.tcx.types.bool, ret_ty);
1125         let ret_layout = fx.layout_of(ret_ty);
1126         CValue::ByVal(fx.bcx.ins().bint(types::I8, res), ret_layout)
1127     }
1128 }
1129
1130 pub fn trans_place<'a, 'tcx: 'a>(
1131     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
1132     place: &Place<'tcx>,
1133 ) -> CPlace<'tcx> {
1134     match place {
1135         Place::Base(base) => match base {
1136             PlaceBase::Local(local) => fx.get_local_place(*local),
1137             PlaceBase::Promoted(data) => crate::constant::trans_promoted(fx, data.0, data.1),
1138             PlaceBase::Static(static_) => crate::constant::codegen_static_ref(fx, static_),
1139         }
1140         Place::Projection(projection) => {
1141             let base = trans_place(fx, &projection.base);
1142             match projection.elem {
1143                 ProjectionElem::Deref => base.place_deref(fx),
1144                 ProjectionElem::Field(field, _ty) => base.place_field(fx, field),
1145                 ProjectionElem::Index(local) => {
1146                     let index = fx.get_local_place(local).to_cvalue(fx).load_scalar(fx);
1147                     base.place_index(fx, index)
1148                 }
1149                 ProjectionElem::ConstantIndex {
1150                     offset,
1151                     min_length: _,
1152                     from_end,
1153                 } => {
1154                     let index = if !from_end {
1155                         fx.bcx.ins().iconst(fx.pointer_type, offset as i64)
1156                     } else {
1157                         let len = codegen_array_len(fx, base);
1158                         fx.bcx.ins().iadd_imm(len, -(offset as i64))
1159                     };
1160                     base.place_index(fx, index)
1161                 }
1162                 ProjectionElem::Subslice { from, to } => {
1163                     // These indices are generated by slice patterns.
1164                     // slice[from:-to] in Python terms.
1165
1166                     match base.layout().ty.sty {
1167                         ty::Array(elem_ty, len) => {
1168                             let elem_layout = fx.layout_of(elem_ty);
1169                             let ptr = base.to_addr(fx);
1170                             let len = crate::constant::force_eval_const(fx, len).unwrap_usize(fx.tcx);
1171                             CPlace::Addr(
1172                                 fx.bcx.ins().iadd_imm(ptr, elem_layout.size.bytes() as i64 * from as i64),
1173                                 None,
1174                                 fx.layout_of(fx.tcx.mk_array(elem_ty, len - from as u64 - to as u64)),
1175                             )
1176                         }
1177                         ty::Slice(elem_ty) => {
1178                             let elem_layout = fx.layout_of(elem_ty);
1179                             let (ptr, len) = base.to_addr_maybe_unsized(fx);
1180                             let len = len.unwrap();
1181                             CPlace::Addr(
1182                                 fx.bcx.ins().iadd_imm(ptr, elem_layout.size.bytes() as i64 * from as i64),
1183                                 Some(fx.bcx.ins().iadd_imm(len, -(from as i64 + to as i64))),
1184                                 base.layout(),
1185                             )
1186                         }
1187                         _ => unreachable!(),
1188                     }
1189                 }
1190                 ProjectionElem::Downcast(_adt_def, variant) => base.downcast_variant(fx, variant),
1191             }
1192         }
1193     }
1194 }
1195
1196 pub fn trans_operand<'a, 'tcx>(
1197     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
1198     operand: &Operand<'tcx>,
1199 ) -> CValue<'tcx> {
1200     match operand {
1201         Operand::Move(place) | Operand::Copy(place) => {
1202             let cplace = trans_place(fx, place);
1203             cplace.to_cvalue(fx)
1204         }
1205         Operand::Constant(const_) => crate::constant::trans_constant(fx, const_),
1206     }
1207 }