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