3 struct PrintOnPanic<F: Fn() -> String>(F);
4 impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
6 if ::std::thread::panicking() {
7 println!("{}", (self.0)());
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>,
19 MonoItem::Fn(inst) => {
21 PrintOnPanic(|| format!("{:?} {}", inst, tcx.symbol_name(inst).as_str()));
22 debug_assert!(!inst.substs.needs_infer());
23 let _mir_guard = PrintOnPanic(|| {
26 | InstanceDef::DropGlue(_, _)
27 | InstanceDef::Virtual(_, _)
28 if inst.def_id().krate == LOCAL_CRATE =>
30 let mut mir = ::std::io::Cursor::new(Vec::new());
31 crate::rustc_mir::util::write_mir_pretty(
37 String::from_utf8(mir.into_inner()).unwrap()
40 // FIXME fix write_mir_pretty for these instances
41 format!("{:#?}", tcx.instance_mir(inst.def))
46 trans_fn(cx, inst, linkage);
48 MonoItem::Static(def_id) => {
49 crate::constant::codegen_static(&mut cx.ccx, def_id);
51 MonoItem::GlobalAsm(node_id) => tcx
53 .fatal(&format!("Unimplemented global asm mono item {:?}", node_id)),
57 fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
58 cx: &mut crate::CodegenCx<'a, 'clif, 'tcx, B>,
59 instance: Instance<'tcx>,
65 let mir = tcx.instance_mir(instance.def);
67 // Step 2. Check fn sig for u128 and i128 and replace those functions with a trap.
69 // Step 2a. Check sig for u128 and i128
70 let fn_ty = instance.ty(tcx);
71 let fn_sig = crate::abi::ty_fn_sig(tcx, fn_ty);
73 struct UI128Visitor<'a, 'tcx: 'a>(TyCtxt<'a, 'tcx, 'tcx>, bool);
75 impl<'a, 'tcx: 'a> rustc::ty::fold::TypeVisitor<'tcx> for UI128Visitor<'a, 'tcx> {
76 fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
77 if t.sty == self.0.types.u128.sty || t.sty == self.0.types.i128.sty {
79 return false; // stop visiting
82 t.super_visit_with(self)
86 let mut visitor = UI128Visitor(tcx, false);
87 fn_sig.visit_with(&mut visitor);
89 // Step 2b. If found replace function with a trap.
91 tcx.sess.warn("u128 and i128 are not yet supported. \
92 Functions using these as args will be replaced with a trap.");
94 // Step 2b1. Declare function with fake signature
96 params: vec![AbiParam::new(types::INVALID)],
98 call_conv: CallConv::Fast,
100 let name = tcx.symbol_name(instance).as_str();
101 let func_id = cx.module.declare_function(&*name, linkage, &sig).unwrap();
103 // Step 2b2. Create trapping function
104 let mut func = Function::with_name_signature(ExternalName::user(0, 0), sig);
105 let mut func_ctx = FunctionBuilderContext::new();
106 let mut bcx = FunctionBuilder::new(&mut func, &mut func_ctx);
107 let start_ebb = bcx.create_ebb();
108 bcx.append_ebb_params_for_function_params(start_ebb);
109 bcx.switch_to_block(start_ebb);
110 crate::trap::trap_unreachable(&mut bcx);
111 bcx.seal_all_blocks();
114 // Step 2b3. Define function
115 cx.caches.context.func = func;
117 .define_function(func_id, &mut cx.caches.context)
119 cx.caches.context.clear();
124 // Step 3. Declare function
125 let (name, sig) = get_function_name_and_sig(tcx, instance, false);
126 let func_id = cx.module.declare_function(&name, linkage, &sig).unwrap();
127 let mut debug_context = cx
130 .map(|debug_context| FunctionDebugContext::new(tcx, debug_context, mir, &name, &sig));
132 // Step 4. Make FunctionBuilder
133 let mut func = Function::with_name_signature(ExternalName::user(0, 0), sig);
134 let mut func_ctx = FunctionBuilderContext::new();
135 let mut bcx = FunctionBuilder::new(&mut func, &mut func_ctx);
137 // Step 5. Predefine ebb's
138 let start_ebb = bcx.create_ebb();
139 let mut ebb_map: HashMap<BasicBlock, Ebb> = HashMap::new();
140 for (bb, _bb_data) in mir.basic_blocks().iter_enumerated() {
141 ebb_map.insert(bb, bcx.create_ebb());
144 // Step 6. Make FunctionCx
145 let pointer_type = cx.module.target_config().pointer_type();
146 let clif_comments = crate::pretty_clif::CommentWriter::new(tcx, instance);
148 let mut fx = FunctionCx {
158 local_map: HashMap::new(),
161 constants: &mut cx.ccx,
162 caches: &mut cx.caches,
163 source_info_set: indexmap::IndexSet::new(),
166 // Step 7. Codegen function
167 with_unimpl_span(fx.mir.span, || {
168 crate::abi::codegen_fn_prelude(&mut fx, start_ebb);
169 codegen_fn_content(&mut fx);
171 let source_info_set = fx.source_info_set.clone();
173 // Step 8. Write function to file for debugging
174 #[cfg(debug_assertions)]
175 fx.write_clif_file();
177 // Step 9. Verify function
178 verify_func(tcx, fx.clif_comments, &func);
180 // Step 10. Define function
181 cx.caches.context.func = func;
183 .define_function(func_id, &mut cx.caches.context)
186 // Step 11. Define debuginfo for function
187 let context = &cx.caches.context;
188 let isa = cx.module.isa();
191 .map(|x| x.define(tcx, context, isa, &source_info_set));
193 // Step 12. Clear context to make it usable for the next function
194 cx.caches.context.clear();
197 fn verify_func(tcx: TyCtxt, writer: crate::pretty_clif::CommentWriter, func: &Function) {
198 let flags = settings::Flags::new(settings::builder());
199 match ::cranelift::codegen::verify_function(&func, &flags) {
202 tcx.sess.err(&format!("{:?}", err));
203 let pretty_error = ::cranelift::codegen::print_errors::pretty_verifier_error(
206 Some(Box::new(&writer)),
210 .fatal(&format!("cranelift verify error:\n{}", pretty_error));
215 fn codegen_fn_content<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx, impl Backend>) {
216 for (bb, bb_data) in fx.mir.basic_blocks().iter_enumerated() {
217 if bb_data.is_cleanup {
218 // Unwinding after panicking is not supported
222 let ebb = fx.get_ebb(bb);
223 fx.bcx.switch_to_block(ebb);
226 for stmt in &bb_data.statements {
227 fx.set_debug_loc(stmt.source_info);
228 trans_stmt(fx, ebb, stmt);
231 #[cfg(debug_assertions)]
233 let mut terminator_head = "\n".to_string();
237 .fmt_head(&mut terminator_head)
239 let inst = fx.bcx.func.layout.last_inst(ebb).unwrap();
240 fx.add_comment(inst, terminator_head);
243 fx.set_debug_loc(bb_data.terminator().source_info);
245 match &bb_data.terminator().kind {
246 TerminatorKind::Goto { target } => {
247 let ebb = fx.get_ebb(*target);
248 fx.bcx.ins().jump(ebb, &[]);
250 TerminatorKind::Return => {
251 crate::abi::codegen_return(fx);
253 TerminatorKind::Assert {
260 let cond = trans_operand(fx, cond).load_scalar(fx);
261 // TODO HACK brz/brnz for i8/i16 is not yet implemented
262 let cond = fx.bcx.ins().uextend(types::I32, cond);
263 let target = fx.get_ebb(*target);
265 fx.bcx.ins().brnz(cond, target, &[]);
267 fx.bcx.ins().brz(cond, target, &[]);
269 trap_panic(&mut fx.bcx);
272 TerminatorKind::SwitchInt {
278 let discr = trans_operand(fx, discr).load_scalar(fx);
279 let mut switch = ::cranelift::frontend::Switch::new();
280 for (i, value) in values.iter().enumerate() {
281 let ebb = fx.get_ebb(targets[i]);
282 switch.set_entry(*value as u64, ebb);
284 let otherwise_ebb = fx.get_ebb(targets[targets.len() - 1]);
285 switch.emit(&mut fx.bcx, discr, otherwise_ebb);
287 TerminatorKind::Call {
294 crate::abi::codegen_terminator_call(fx, func, args, destination);
296 TerminatorKind::Resume | TerminatorKind::Abort | TerminatorKind::Unreachable => {
297 trap_unreachable(&mut fx.bcx);
299 TerminatorKind::Yield { .. }
300 | TerminatorKind::FalseEdges { .. }
301 | TerminatorKind::FalseUnwind { .. }
302 | TerminatorKind::DropAndReplace { .. }
303 | TerminatorKind::GeneratorDrop => {
304 bug!("shouldn't exist at trans {:?}", bb_data.terminator());
306 TerminatorKind::Drop {
311 let ty = location.ty(fx.mir, fx.tcx).to_ty(fx.tcx);
312 let ty = fx.monomorphize(&ty);
313 let drop_fn = crate::rustc_mir::monomorphize::resolve_drop_in_place(fx.tcx, ty);
315 if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
316 // we don't actually need to drop anything
318 let drop_place = trans_place(fx, location);
319 let drop_fn_ty = drop_fn.ty(fx.tcx);
322 crate::abi::codegen_drop(fx, drop_place, drop_fn_ty);
325 let arg_place = CPlace::new_stack_slot(
328 &ty::RegionKind::ReErased,
331 mutbl: crate::rustc::hir::Mutability::MutMutable,
335 drop_place.write_place_ref(fx, arg_place);
336 let arg_value = arg_place.to_cvalue(fx);
337 crate::abi::codegen_call_inner(
348 let target_ebb = fx.get_ebb(*target);
349 fx.bcx.ins().jump(target_ebb, &[]);
354 fx.bcx.seal_all_blocks();
358 fn trans_stmt<'a, 'tcx: 'a>(
359 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
361 stmt: &Statement<'tcx>,
363 let _print_guard = PrintOnPanic(|| format!("stmt {:?}", stmt));
365 fx.set_debug_loc(stmt.source_info);
367 #[cfg(debug_assertions)]
369 StatementKind::StorageLive(..) | StatementKind::StorageDead(..) => {} // Those are not very useful
371 let inst = fx.bcx.func.layout.last_inst(cur_ebb).unwrap();
372 fx.add_comment(inst, format!("{:?}", stmt));
377 StatementKind::SetDiscriminant {
381 let place = trans_place(fx, place);
382 let layout = place.layout();
383 if layout.for_variant(&*fx, *variant_index).abi == layout::Abi::Uninhabited {
386 match layout.variants {
387 layout::Variants::Single { index } => {
388 assert_eq!(index, *variant_index);
390 layout::Variants::Tagged { .. } => {
391 let ptr = place.place_field(fx, mir::Field::new(0));
396 .discriminant_for_variant(fx.tcx, *variant_index)
398 let discr = CValue::const_val(fx, ptr.layout().ty, to as u64 as i64);
399 ptr.write_cvalue(fx, discr);
401 layout::Variants::NicheFilling {
407 if *variant_index != dataful_variant {
408 let niche = place.place_field(fx, mir::Field::new(0));
409 //let niche_llty = niche.layout.immediate_llvm_type(bx.cx);
411 ((variant_index.as_u32() - niche_variants.start().as_u32()) as u128)
412 .wrapping_add(niche_start);
413 // FIXME(eddyb) Check the actual primitive type here.
414 let niche_llval = if niche_value == 0 {
415 CValue::const_val(fx, niche.layout().ty, 0)
417 CValue::const_val(fx, niche.layout().ty, niche_value as u64 as i64)
419 niche.write_cvalue(fx, niche_llval);
424 StatementKind::Assign(to_place, rval) => {
425 let lval = trans_place(fx, to_place);
426 let dest_layout = lval.layout();
428 Rvalue::Use(operand) => {
429 let val = trans_operand(fx, operand);
430 lval.write_cvalue(fx, val);
432 Rvalue::Ref(_, _, place) => {
433 let place = trans_place(fx, place);
434 place.write_place_ref(fx, lval);
436 Rvalue::BinaryOp(bin_op, lhs, rhs) => {
437 let ty = fx.monomorphize(&lhs.ty(fx.mir, fx.tcx));
438 let lhs = trans_operand(fx, lhs);
439 let rhs = trans_operand(fx, rhs);
441 let res = match ty.sty {
442 ty::Bool => trans_bool_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
444 trans_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, false)
447 trans_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, true)
449 ty::Float(_) => trans_float_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
450 ty::Char => trans_char_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
451 ty::RawPtr(..) => trans_ptr_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
452 ty::FnPtr(..) => trans_ptr_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
453 _ => unimplemented!("binop {:?} for {:?}", bin_op, ty),
455 lval.write_cvalue(fx, res);
457 Rvalue::CheckedBinaryOp(bin_op, lhs, rhs) => {
458 let ty = fx.monomorphize(&lhs.ty(fx.mir, fx.tcx));
459 let lhs = trans_operand(fx, lhs);
460 let rhs = trans_operand(fx, rhs);
462 let res = match ty.sty {
464 trans_checked_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, false)
467 trans_checked_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, true)
469 _ => unimplemented!("checked binop {:?} for {:?}", bin_op, ty),
471 lval.write_cvalue(fx, res);
473 Rvalue::UnaryOp(un_op, operand) => {
474 let operand = trans_operand(fx, operand);
475 let layout = operand.layout();
476 let val = operand.load_scalar(fx);
477 let res = match un_op {
479 match layout.ty.sty {
481 let val = fx.bcx.ins().uextend(types::I32, val); // WORKAROUND for CraneStation/cranelift#466
482 let res = fx.bcx.ins().icmp_imm(IntCC::Equal, val, 0);
483 fx.bcx.ins().bint(types::I8, res)
485 ty::Uint(_) | ty::Int(_) => fx.bcx.ins().bnot(val),
486 _ => unimplemented!("un op Not for {:?}", layout.ty),
489 UnOp::Neg => match layout.ty.sty {
491 let clif_ty = fx.clif_type(layout.ty).unwrap();
492 let zero = fx.bcx.ins().iconst(clif_ty, 0);
493 fx.bcx.ins().isub(zero, val)
495 ty::Float(_) => fx.bcx.ins().fneg(val),
496 _ => unimplemented!("un op Neg for {:?}", layout.ty),
499 lval.write_cvalue(fx, CValue::ByVal(res, layout));
501 Rvalue::Cast(CastKind::ReifyFnPointer, operand, ty) => {
502 let layout = fx.layout_of(ty);
504 .monomorphize(&operand.ty(&fx.mir.local_decls, fx.tcx))
507 ty::FnDef(def_id, substs) => {
508 let func_ref = fx.get_function_ref(
509 Instance::resolve(fx.tcx, ParamEnv::reveal_all(), def_id, substs)
512 let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);
513 lval.write_cvalue(fx, CValue::ByVal(func_addr, layout));
515 _ => bug!("Trying to ReifyFnPointer on non FnDef {:?}", ty),
518 Rvalue::Cast(CastKind::UnsafeFnPointer, operand, ty)
519 | Rvalue::Cast(CastKind::MutToConstPointer, operand, ty) => {
520 let operand = trans_operand(fx, operand);
521 let layout = fx.layout_of(ty);
522 lval.write_cvalue(fx, operand.unchecked_cast_to(layout));
524 Rvalue::Cast(CastKind::Misc, operand, to_ty) => {
525 let operand = trans_operand(fx, operand);
526 let from_ty = operand.layout().ty;
528 fn is_fat_ptr<'a, 'tcx: 'a>(fx: &FunctionCx<'a, 'tcx, impl Backend>, ty: Ty<'tcx>) -> bool {
531 .map(|ty::TypeAndMut {ty: pointee_ty, mutbl: _ }| fx.layout_of(pointee_ty).is_unsized())
535 if is_fat_ptr(fx, from_ty) {
536 if is_fat_ptr(fx, to_ty) {
537 // fat-ptr -> fat-ptr
538 lval.write_cvalue(fx, operand.unchecked_cast_to(dest_layout));
540 // fat-ptr -> thin-ptr
541 let (ptr, _extra) = operand.load_scalar_pair(fx);
542 lval.write_cvalue(fx, CValue::ByVal(ptr, dest_layout))
544 } else if let ty::Adt(adt_def, _substs) = from_ty.sty {
545 // enum -> discriminant value
546 assert!(adt_def.is_enum());
548 ty::Uint(_) | ty::Int(_) => {},
549 _ => unreachable!("cast adt {} -> {}", from_ty, to_ty),
552 // FIXME avoid forcing to stack
554 CPlace::Addr(operand.force_stack(fx), None, operand.layout());
555 let discr = trans_get_discriminant(fx, place, fx.layout_of(to_ty));
556 lval.write_cvalue(fx, discr);
558 let from_clif_ty = fx.clif_type(from_ty).unwrap();
559 let to_clif_ty = fx.clif_type(to_ty).unwrap();
560 let from = operand.load_scalar(fx);
562 let signed = match from_ty.sty {
563 ty::Ref(..) | ty::RawPtr(..) | ty::FnPtr(..) | ty::Char | ty::Uint(..) | ty::Bool => false,
565 ty::Float(..) => false, // `signed` is unused for floats
566 _ => panic!("{}", from_ty),
569 let res = if from_clif_ty.is_int() && to_clif_ty.is_int() {
570 // int-like -> int-like
571 crate::common::clif_intcast(
577 } else if from_clif_ty.is_int() && to_clif_ty.is_float() {
579 // FIXME missing encoding for fcvt_from_sint.f32.i8
580 let from = if from_clif_ty == types::I8 || from_clif_ty == types::I16 {
581 fx.bcx.ins().uextend(types::I32, from)
586 fx.bcx.ins().fcvt_from_sint(to_clif_ty, from)
588 fx.bcx.ins().fcvt_from_uint(to_clif_ty, from)
590 } else if from_clif_ty.is_float() && to_clif_ty.is_int() {
592 let from = operand.load_scalar(fx);
594 fx.bcx.ins().fcvt_to_sint_sat(to_clif_ty, from)
596 fx.bcx.ins().fcvt_to_uint_sat(to_clif_ty, from)
598 } else if from_clif_ty.is_float() && to_clif_ty.is_float() {
600 match (from_clif_ty, to_clif_ty) {
601 (types::F32, types::F64) => {
602 fx.bcx.ins().fpromote(types::F64, from)
604 (types::F64, types::F32) => {
605 fx.bcx.ins().fdemote(types::F32, from)
610 unimpl!("rval misc {:?} {:?}", from_ty, to_ty)
612 lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
615 Rvalue::Cast(CastKind::ClosureFnPointer, operand, _ty) => {
616 let operand = trans_operand(fx, operand);
617 match operand.layout().ty.sty {
618 ty::Closure(def_id, substs) => {
619 let instance = rustc_mir::monomorphize::resolve_closure(
623 ty::ClosureKind::FnOnce,
625 let func_ref = fx.get_function_ref(instance);
626 let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);
627 lval.write_cvalue(fx, CValue::ByVal(func_addr, lval.layout()));
630 bug!("{} cannot be cast to a fn ptr", operand.layout().ty)
634 Rvalue::Cast(CastKind::Unsize, operand, _ty) => {
635 let operand = trans_operand(fx, operand);
636 operand.unsize_value(fx, lval);
638 Rvalue::Discriminant(place) => {
639 let place = trans_place(fx, place);
640 let discr = trans_get_discriminant(fx, place, dest_layout);
641 lval.write_cvalue(fx, discr);
643 Rvalue::Repeat(operand, times) => {
644 let operand = trans_operand(fx, operand);
646 let index = fx.bcx.ins().iconst(fx.pointer_type, i as i64);
647 let to = lval.place_index(fx, index);
648 to.write_cvalue(fx, operand);
651 Rvalue::Len(place) => {
652 let place = trans_place(fx, place);
653 let usize_layout = fx.layout_of(fx.tcx.types.usize);
654 let len = codegen_array_len(fx, place);
655 lval.write_cvalue(fx, CValue::ByVal(len, usize_layout));
657 Rvalue::NullaryOp(NullOp::Box, content_ty) => {
658 use rustc::middle::lang_items::ExchangeMallocFnLangItem;
660 let usize_type = fx.clif_type(fx.tcx.types.usize).unwrap();
661 let layout = fx.layout_of(content_ty);
662 let llsize = fx.bcx.ins().iconst(usize_type, layout.size.bytes() as i64);
666 .iconst(usize_type, layout.align.abi.bytes() as i64);
667 let box_layout = fx.layout_of(fx.tcx.mk_box(content_ty));
670 let def_id = match fx.tcx.lang_items().require(ExchangeMallocFnLangItem) {
675 .fatal(&format!("allocation of `{}` {}", box_layout.ty, s));
678 let instance = ty::Instance::mono(fx.tcx, def_id);
679 let func_ref = fx.get_function_ref(instance);
680 let call = fx.bcx.ins().call(func_ref, &[llsize, llalign]);
681 let ptr = fx.bcx.inst_results(call)[0];
682 lval.write_cvalue(fx, CValue::ByVal(ptr, box_layout));
684 Rvalue::NullaryOp(NullOp::SizeOf, ty) => {
688 .is_sized(fx.tcx.at(DUMMY_SP), ParamEnv::reveal_all()));
689 let ty_size = fx.layout_of(ty).size.bytes();
690 let val = CValue::const_val(fx, fx.tcx.types.usize, ty_size as i64);
691 lval.write_cvalue(fx, val);
693 Rvalue::Aggregate(kind, operands) => match **kind {
694 AggregateKind::Array(_ty) => {
695 for (i, operand) in operands.into_iter().enumerate() {
696 let operand = trans_operand(fx, operand);
697 let index = fx.bcx.ins().iconst(fx.pointer_type, i as i64);
698 let to = lval.place_index(fx, index);
699 to.write_cvalue(fx, operand);
702 _ => unimpl!("shouldn't exist at trans {:?}", rval),
706 StatementKind::StorageLive(_)
707 | StatementKind::StorageDead(_)
709 | StatementKind::FakeRead(..)
710 | StatementKind::Retag { .. }
711 | StatementKind::AscribeUserType(..) => {}
713 StatementKind::InlineAsm { .. } => unimpl!("Inline assembly is not supported"),
717 fn codegen_array_len<'a, 'tcx: 'a>(
718 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
721 match place.layout().ty.sty {
722 ty::Array(_elem_ty, len) => {
723 let len = crate::constant::force_eval_const(fx, len).unwrap_usize(fx.tcx) as i64;
724 fx.bcx.ins().iconst(fx.pointer_type, len)
726 ty::Slice(_elem_ty) => place
727 .to_addr_maybe_unsized(fx)
729 .expect("Length metadata for slice place"),
730 _ => bug!("Rvalue::Len({:?})", place),
734 pub fn trans_get_discriminant<'a, 'tcx: 'a>(
735 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
737 dest_layout: TyLayout<'tcx>,
739 let layout = place.layout();
741 if layout.abi == layout::Abi::Uninhabited {
742 trap_unreachable(&mut fx.bcx);
744 match layout.variants {
745 layout::Variants::Single { index } => {
746 let discr_val = layout
749 .map_or(index.as_u32() as u128, |def| {
750 def.discriminant_for_variant(fx.tcx, index).val
752 return CValue::const_val(fx, dest_layout.ty, discr_val as u64 as i64);
754 layout::Variants::Tagged { .. } | layout::Variants::NicheFilling { .. } => {}
757 let discr = place.place_field(fx, mir::Field::new(0)).to_cvalue(fx);
758 let discr_ty = discr.layout().ty;
759 let lldiscr = discr.load_scalar(fx);
760 match layout.variants {
761 layout::Variants::Single { .. } => bug!(),
762 layout::Variants::Tagged { ref tag, .. } => {
763 let signed = match tag.value {
764 layout::Int(_, signed) => signed,
767 let val = clif_intcast(fx, lldiscr, fx.clif_type(dest_layout.ty).unwrap(), signed);
768 return CValue::ByVal(val, dest_layout);
770 layout::Variants::NicheFilling {
776 let niche_llty = fx.clif_type(discr_ty).unwrap();
777 let dest_clif_ty = fx.clif_type(dest_layout.ty).unwrap();
778 if niche_variants.start() == niche_variants.end() {
782 .icmp_imm(IntCC::Equal, lldiscr, niche_start as u64 as i64);
786 .iconst(dest_clif_ty, niche_variants.start().as_u32() as i64);
790 .iconst(dest_clif_ty, dataful_variant.as_u32() as i64);
791 let val = fx.bcx.ins().select(b, if_true, if_false);
792 return CValue::ByVal(val, dest_layout);
794 // Rebase from niche values to discriminant values.
795 let delta = niche_start.wrapping_sub(niche_variants.start().as_u32() as u128);
796 let delta = fx.bcx.ins().iconst(niche_llty, delta as u64 as i64);
797 let lldiscr = fx.bcx.ins().isub(lldiscr, delta);
798 let b = fx.bcx.ins().icmp_imm(
799 IntCC::UnsignedLessThanOrEqual,
801 niche_variants.end().as_u32() as i64,
804 clif_intcast(fx, lldiscr, fx.clif_type(dest_layout.ty).unwrap(), false);
808 .iconst(dest_clif_ty, dataful_variant.as_u32() as i64);
809 let val = fx.bcx.ins().select(b, if_true, if_false);
810 return CValue::ByVal(val, dest_layout);
816 macro_rules! binop_match {
817 (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, bug) => {
818 bug!("binop {} on {} lhs: {:?} rhs: {:?}", stringify!($var), $bug_fmt, $lhs, $rhs)
820 (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, icmp($cc:ident)) => {{
821 assert_eq!($fx.tcx.types.bool, $ret_ty);
822 let ret_layout = $fx.layout_of($ret_ty);
824 let b = $fx.bcx.ins().icmp(IntCC::$cc, $lhs, $rhs);
825 CValue::ByVal($fx.bcx.ins().bint(types::I8, b), ret_layout)
827 (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, fcmp($cc:ident)) => {{
828 assert_eq!($fx.tcx.types.bool, $ret_ty);
829 let ret_layout = $fx.layout_of($ret_ty);
830 let b = $fx.bcx.ins().fcmp(FloatCC::$cc, $lhs, $rhs);
831 CValue::ByVal($fx.bcx.ins().bint(types::I8, b), ret_layout)
833 (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, custom(|| $body:expr)) => {{
836 (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, $name:ident) => {{
837 let ret_layout = $fx.layout_of($ret_ty);
838 CValue::ByVal($fx.bcx.ins().$name($lhs, $rhs), ret_layout)
841 $fx:expr, $bin_op:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, $bug_fmt:expr;
843 $var:ident ($sign:pat) $name:tt $( ( $($next:tt)* ) )? ;
846 let lhs = $lhs.load_scalar($fx);
847 let rhs = $rhs.load_scalar($fx);
848 match ($bin_op, $signed) {
850 (BinOp::$var, $sign) => binop_match!(@single $fx, $bug_fmt, $var, $signed, lhs, rhs, $ret_ty, $name $( ( $($next)* ) )?),
856 fn trans_bool_binop<'a, 'tcx: 'a>(
857 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
863 let res = binop_match! {
864 fx, bin_op, false, lhs, rhs, ty, "bool";
877 Lt (_) icmp(UnsignedLessThan);
878 Le (_) icmp(UnsignedLessThanOrEqual);
879 Ne (_) icmp(NotEqual);
880 Ge (_) icmp(UnsignedGreaterThanOrEqual);
881 Gt (_) icmp(UnsignedGreaterThan);
889 pub fn trans_int_binop<'a, 'tcx: 'a>(
890 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
897 if bin_op != BinOp::Shl && bin_op != BinOp::Shr {
901 "int binop requires lhs and rhs of same type"
905 fx, bin_op, signed, lhs, rhs, out_ty, "int/uint";
921 Lt (false) icmp(UnsignedLessThan);
922 Lt (true) icmp(SignedLessThan);
923 Le (false) icmp(UnsignedLessThanOrEqual);
924 Le (true) icmp(SignedLessThanOrEqual);
925 Ne (_) icmp(NotEqual);
926 Ge (false) icmp(UnsignedGreaterThanOrEqual);
927 Ge (true) icmp(SignedGreaterThanOrEqual);
928 Gt (false) icmp(UnsignedGreaterThan);
929 Gt (true) icmp(SignedGreaterThan);
935 pub fn trans_checked_int_binop<'a, 'tcx: 'a>(
936 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
938 in_lhs: CValue<'tcx>,
939 in_rhs: CValue<'tcx>,
943 if bin_op != BinOp::Shl && bin_op != BinOp::Shr {
947 "checked int binop requires lhs and rhs of same type"
951 let lhs = in_lhs.load_scalar(fx);
952 let rhs = in_rhs.load_scalar(fx);
953 let res = match bin_op {
954 BinOp::Add => fx.bcx.ins().iadd(lhs, rhs),
955 BinOp::Sub => fx.bcx.ins().isub(lhs, rhs),
956 BinOp::Mul => fx.bcx.ins().imul(lhs, rhs),
957 BinOp::Shl => fx.bcx.ins().ishl(lhs, rhs),
960 fx.bcx.ins().ushr(lhs, rhs)
962 fx.bcx.ins().sshr(lhs, rhs)
966 "binop {:?} on checked int/uint lhs: {:?} rhs: {:?}",
973 // TODO: check for overflow
974 let has_overflow = fx.bcx.ins().iconst(types::I8, 0);
976 let out_place = CPlace::new_stack_slot(fx, out_ty);
977 let out_layout = out_place.layout();
978 out_place.write_cvalue(fx, CValue::ByValPair(res, has_overflow, out_layout));
980 out_place.to_cvalue(fx)
983 fn trans_float_binop<'a, 'tcx: 'a>(
984 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
990 let res = binop_match! {
991 fx, bin_op, false, lhs, rhs, ty, "float";
997 assert_eq!(lhs.layout().ty, ty);
998 assert_eq!(rhs.layout().ty, ty);
1000 ty::Float(FloatTy::F32) => fx.easy_call("fmodf", &[lhs, rhs], ty),
1001 ty::Float(FloatTy::F64) => fx.easy_call("fmod", &[lhs, rhs], ty),
1012 Lt (_) fcmp(LessThan);
1013 Le (_) fcmp(LessThanOrEqual);
1014 Ne (_) fcmp(NotEqual);
1015 Ge (_) fcmp(GreaterThanOrEqual);
1016 Gt (_) fcmp(GreaterThan);
1024 fn trans_char_binop<'a, 'tcx: 'a>(
1025 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
1031 let res = binop_match! {
1032 fx, bin_op, false, lhs, rhs, ty, "char";
1045 Lt (_) icmp(UnsignedLessThan);
1046 Le (_) icmp(UnsignedLessThanOrEqual);
1047 Ne (_) icmp(NotEqual);
1048 Ge (_) icmp(UnsignedGreaterThanOrEqual);
1049 Gt (_) icmp(UnsignedGreaterThan);
1057 fn trans_ptr_binop<'a, 'tcx: 'a>(
1058 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
1064 let not_fat = match lhs.layout().ty.sty {
1065 ty::RawPtr(TypeAndMut { ty, mutbl: _ }) => {
1066 ty.is_sized(fx.tcx.at(DUMMY_SP), ParamEnv::reveal_all())
1068 ty::FnPtr(..) => true,
1069 _ => bug!("trans_ptr_binop on non ptr"),
1072 if let BinOp::Offset = bin_op {
1073 let (base, offset) = (lhs, rhs.load_scalar(fx));
1074 let pointee_ty = base.layout().ty.builtin_deref(true).unwrap().ty;
1075 let pointee_size = fx.layout_of(pointee_ty).size.bytes();
1076 let ptr_diff = fx.bcx.ins().imul_imm(offset, pointee_size as i64);
1077 let base_val = base.load_scalar(fx);
1078 let res = fx.bcx.ins().iadd(base_val, ptr_diff);
1079 return CValue::ByVal(res, base.layout());
1083 fx, bin_op, false, lhs, rhs, ret_ty, "ptr";
1096 Lt (_) icmp(UnsignedLessThan);
1097 Le (_) icmp(UnsignedLessThanOrEqual);
1098 Ne (_) icmp(NotEqual);
1099 Ge (_) icmp(UnsignedGreaterThanOrEqual);
1100 Gt (_) icmp(UnsignedGreaterThan);
1102 Offset (_) bug; // Handled above
1105 let (lhs_ptr, lhs_extra) = lhs.load_scalar_pair(fx);
1106 let (rhs_ptr, rhs_extra) = rhs.load_scalar_pair(fx);
1107 let res = match bin_op {
1109 let ptr_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_ptr, rhs_ptr);
1110 let extra_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_extra, rhs_extra);
1111 fx.bcx.ins().band(ptr_eq, extra_eq)
1114 let ptr_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_ptr, rhs_ptr);
1115 let extra_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_extra, rhs_extra);
1116 fx.bcx.ins().bor(ptr_ne, extra_ne)
1118 _ => unimplemented!(
1119 "trans_ptr_binop({:?}, <fat ptr>, <fat ptr>) not implemented",
1124 assert_eq!(fx.tcx.types.bool, ret_ty);
1125 let ret_layout = fx.layout_of(ret_ty);
1126 CValue::ByVal(fx.bcx.ins().bint(types::I8, res), ret_layout)
1130 pub fn trans_place<'a, 'tcx: 'a>(
1131 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
1132 place: &Place<'tcx>,
1135 Place::Base(base) => match base {
1136 PlaceBase::Local(local) => fx.get_local_place(*local),
1137 PlaceBase::Promoted(promoted) => crate::constant::trans_promoted(fx, promoted.0),
1138 PlaceBase::Static(static_) => crate::constant::codegen_static_ref(fx, static_),
1140 Place::Projection(projection) => {
1141 let base = trans_place(fx, &projection.base);
1142 match projection.elem {
1143 ProjectionElem::Deref => base.place_deref(fx),
1144 ProjectionElem::Field(field, _ty) => base.place_field(fx, field),
1145 ProjectionElem::Index(local) => {
1146 let index = fx.get_local_place(local).to_cvalue(fx).load_scalar(fx);
1147 base.place_index(fx, index)
1149 ProjectionElem::ConstantIndex {
1154 let index = if !from_end {
1155 fx.bcx.ins().iconst(fx.pointer_type, offset as i64)
1157 let len = codegen_array_len(fx, base);
1158 fx.bcx.ins().iadd_imm(len, -(offset as i64))
1160 base.place_index(fx, index)
1162 ProjectionElem::Subslice { from, to } => {
1163 // These indices are generated by slice patterns.
1164 // slice[from:-to] in Python terms.
1166 match base.layout().ty.sty {
1167 ty::Array(elem_ty, len) => {
1168 let elem_layout = fx.layout_of(elem_ty);
1169 let ptr = base.to_addr(fx);
1170 let len = crate::constant::force_eval_const(fx, len).unwrap_usize(fx.tcx);
1172 fx.bcx.ins().iadd_imm(ptr, elem_layout.size.bytes() as i64 * from as i64),
1174 fx.layout_of(fx.tcx.mk_array(elem_ty, len - from as u64 - to as u64)),
1177 ty::Slice(elem_ty) => {
1178 let elem_layout = fx.layout_of(elem_ty);
1179 let (ptr, len) = base.to_addr_maybe_unsized(fx);
1180 let len = len.unwrap();
1182 fx.bcx.ins().iadd_imm(ptr, elem_layout.size.bytes() as i64 * from as i64),
1183 Some(fx.bcx.ins().iadd_imm(len, -(from as i64 + to as i64))),
1187 _ => unreachable!(),
1190 ProjectionElem::Downcast(_adt_def, variant) => base.downcast_variant(fx, variant),
1196 pub fn trans_operand<'a, 'tcx>(
1197 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
1198 operand: &Operand<'tcx>,
1201 Operand::Move(place) | Operand::Copy(place) => {
1202 let cplace = trans_place(fx, place);
1203 cplace.to_cvalue(fx)
1205 Operand::Constant(const_) => crate::constant::trans_constant(fx, const_),