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