1 //! Codegen of a single function
3 use rustc_ast::InlineAsmOptions;
4 use rustc_index::vec::IndexVec;
5 use rustc_middle::ty::adjustment::PointerCast;
6 use rustc_middle::ty::layout::FnAbiOf;
7 use rustc_middle::ty::print::with_no_trimmed_paths;
8 use rustc_middle::ty::SymbolName;
10 use indexmap::IndexSet;
12 use crate::constant::ConstantCx;
13 use crate::prelude::*;
14 use crate::pretty_clif::CommentWriter;
16 struct CodegenedFunction<'tcx> {
17 symbol_name: SymbolName<'tcx>,
20 clif_comments: CommentWriter,
22 source_info_set: IndexSet<SourceInfo>,
25 pub(crate) fn codegen_and_compile_fn<'tcx>(
27 cx: &mut crate::CodegenCx<'tcx>,
28 cached_context: &mut Context,
29 module: &mut dyn Module,
30 instance: Instance<'tcx>,
33 crate::PrintOnPanic(|| format!("{:?} {}", instance, tcx.symbol_name(instance).name));
35 let cached_func = std::mem::replace(&mut cached_context.func, Function::new());
36 let codegened_func = codegen_fn(tcx, cx, cached_func, module, instance);
38 compile_fn(cx, cached_context, module, codegened_func);
43 cx: &mut crate::CodegenCx<'tcx>,
44 cached_func: Function,
45 module: &mut dyn Module,
46 instance: Instance<'tcx>,
47 ) -> CodegenedFunction<'tcx> {
48 debug_assert!(!instance.substs.needs_infer());
50 let mir = tcx.instance_mir(instance.def);
51 let _mir_guard = crate::PrintOnPanic(|| {
52 let mut buf = Vec::new();
53 with_no_trimmed_paths!({
54 rustc_middle::mir::pretty::write_mir_fn(tcx, mir, &mut |_, _| Ok(()), &mut buf)
57 String::from_utf8_lossy(&buf).into_owned()
61 let symbol_name = tcx.symbol_name(instance);
62 let sig = get_function_sig(tcx, module.isa().triple(), instance);
63 let func_id = module.declare_function(symbol_name.name, Linkage::Local, &sig).unwrap();
65 // Make the FunctionBuilder
66 let mut func_ctx = FunctionBuilderContext::new();
67 let mut func = cached_func;
69 func.name = ExternalName::user(0, func_id.as_u32());
71 func.collect_debug_info();
73 let mut bcx = FunctionBuilder::new(&mut func, &mut func_ctx);
76 let start_block = bcx.create_block();
77 let block_map: IndexVec<BasicBlock, Block> =
78 (0..mir.basic_blocks().len()).map(|_| bcx.create_block()).collect();
81 let target_config = module.target_config();
82 let pointer_type = target_config.pointer_type();
83 let clif_comments = crate::pretty_clif::CommentWriter::new(tcx, instance);
85 let mut fx = FunctionCx {
91 constants_cx: ConstantCx::new(),
96 fn_abi: Some(RevealAllLayoutCx(tcx).fn_abi_of_instance(instance, ty::List::empty())),
100 local_map: IndexVec::with_capacity(mir.local_decls.len()),
101 caller_location: None, // set by `codegen_fn_prelude`
104 source_info_set: indexmap::IndexSet::new(),
108 tcx.sess.time("codegen clif ir", || codegen_fn_body(&mut fx, start_block));
110 // Recover all necessary data from fx, before accessing func will prevent future access to it.
111 let clif_comments = fx.clif_comments;
112 let function_span = fx.mir.span;
113 let source_info_set = fx.source_info_set;
115 fx.constants_cx.finalize(fx.tcx, &mut *fx.module);
117 if cx.should_write_ir {
118 crate::pretty_clif::write_clif_file(
119 tcx.output_filenames(()),
129 verify_func(tcx, &clif_comments, &func);
131 CodegenedFunction { symbol_name, func_id, func, clif_comments, function_span, source_info_set }
135 cx: &mut crate::CodegenCx<'tcx>,
136 cached_context: &mut Context,
137 module: &mut dyn Module,
138 codegened_func: CodegenedFunction<'tcx>,
140 let clif_comments = codegened_func.clif_comments;
142 // Store function in context
143 let context = cached_context;
145 context.func = codegened_func.func;
147 // If the return block is not reachable, then the SSA builder may have inserted an `iconst.i128`
148 // instruction, which doesn't have an encoding.
149 context.compute_cfg();
150 context.compute_domtree();
151 context.eliminate_unreachable_code(module.isa()).unwrap();
152 context.dce(module.isa()).unwrap();
153 // Some Cranelift optimizations expect the domtree to not yet be computed and as such don't
154 // invalidate it when it would change.
155 context.domtree.clear();
157 #[cfg(any())] // This is never true
161 let func_clone = context.func.clone();
162 let clif_comments_clone = clif_comments.clone();
163 let mut clif = String::new();
164 for flag in module.isa().flags().iter() {
165 writeln!(clif, "set {}", flag).unwrap();
167 write!(clif, "target {}", module.isa().triple().architecture.to_string()).unwrap();
168 for isa_flag in module.isa().isa_flags().iter() {
169 write!(clif, " {}", isa_flag).unwrap();
171 writeln!(clif, "\n").unwrap();
172 crate::PrintOnPanic(move || {
173 let mut clif = clif.clone();
174 ::cranelift_codegen::write::decorate_function(
175 &mut &clif_comments_clone,
185 cx.profiler.verbose_generic_activity("define function").run(|| {
186 context.want_disasm = cx.should_write_ir;
187 module.define_function(codegened_func.func_id, context).unwrap();
190 if cx.should_write_ir {
191 // Write optimized function to file for debugging
192 crate::pretty_clif::write_clif_file(
193 &cx.output_filenames,
194 codegened_func.symbol_name.name,
201 if let Some(disasm) = &context.mach_compile_result.as_ref().unwrap().disasm {
202 crate::pretty_clif::write_ir_file(
203 &cx.output_filenames,
204 &format!("{}.vcode", codegened_func.symbol_name.name),
205 |file| file.write_all(disasm.as_bytes()),
210 // Define debuginfo for function
211 let isa = module.isa();
212 let debug_context = &mut cx.debug_context;
213 let unwind_context = &mut cx.unwind_context;
214 cx.profiler.verbose_generic_activity("generate debug info").run(|| {
215 if let Some(debug_context) = debug_context {
216 debug_context.define_function(
217 codegened_func.func_id,
218 codegened_func.symbol_name.name,
220 codegened_func.function_span,
221 &codegened_func.source_info_set,
224 unwind_context.add_function(codegened_func.func_id, &context, isa);
228 pub(crate) fn verify_func(
230 writer: &crate::pretty_clif::CommentWriter,
233 tcx.sess.time("verify clif ir", || {
234 let flags = cranelift_codegen::settings::Flags::new(cranelift_codegen::settings::builder());
235 match cranelift_codegen::verify_function(&func, &flags) {
238 tcx.sess.err(&format!("{:?}", err));
239 let pretty_error = cranelift_codegen::print_errors::pretty_verifier_error(
241 Some(Box::new(writer)),
244 tcx.sess.fatal(&format!("cranelift verify error:\n{}", pretty_error));
250 fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
251 if !crate::constant::check_constants(fx) {
252 fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
253 fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
254 // compilation should have been aborted
255 fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
259 let arg_uninhabited = fx
262 .any(|arg| fx.layout_of(fx.monomorphize(fx.mir.local_decls[arg].ty)).abi.is_uninhabited());
264 fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
265 fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
266 fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
269 fx.tcx.sess.time("codegen prelude", || crate::abi::codegen_fn_prelude(fx, start_block));
271 for (bb, bb_data) in fx.mir.basic_blocks().iter_enumerated() {
272 let block = fx.get_block(bb);
273 fx.bcx.switch_to_block(block);
275 if bb_data.is_cleanup {
276 // Unwinding after panicking is not supported
279 // FIXME Once unwinding is supported and Cranelift supports marking blocks as cold, do
280 // so for cleanup blocks.
284 for stmt in &bb_data.statements {
285 fx.set_debug_loc(stmt.source_info);
286 codegen_stmt(fx, block, stmt);
289 if fx.clif_comments.enabled() {
290 let mut terminator_head = "\n".to_string();
291 with_no_trimmed_paths!({
292 bb_data.terminator().kind.fmt_head(&mut terminator_head).unwrap();
294 let inst = fx.bcx.func.layout.last_inst(block).unwrap();
295 fx.add_comment(inst, terminator_head);
298 let source_info = bb_data.terminator().source_info;
299 fx.set_debug_loc(source_info);
301 match &bb_data.terminator().kind {
302 TerminatorKind::Goto { target } => {
303 if let TerminatorKind::Return = fx.mir[*target].terminator().kind {
304 let mut can_immediately_return = true;
305 for stmt in &fx.mir[*target].statements {
306 if let StatementKind::StorageDead(_) = stmt.kind {
308 // FIXME Can sometimes happen, see rust-lang/rust#70531
309 can_immediately_return = false;
314 if can_immediately_return {
315 crate::abi::codegen_return(fx);
320 let block = fx.get_block(*target);
321 fx.bcx.ins().jump(block, &[]);
323 TerminatorKind::Return => {
324 crate::abi::codegen_return(fx);
326 TerminatorKind::Assert { cond, expected, msg, target, cleanup: _ } => {
327 if !fx.tcx.sess.overflow_checks() {
328 if let mir::AssertKind::OverflowNeg(_) = *msg {
329 let target = fx.get_block(*target);
330 fx.bcx.ins().jump(target, &[]);
334 let cond = codegen_operand(fx, cond).load_scalar(fx);
336 let target = fx.get_block(*target);
337 let failure = fx.bcx.create_block();
338 fx.bcx.set_cold_block(failure);
341 fx.bcx.ins().brz(cond, failure, &[]);
343 fx.bcx.ins().brnz(cond, failure, &[]);
345 fx.bcx.ins().jump(target, &[]);
347 fx.bcx.switch_to_block(failure);
351 AssertKind::BoundsCheck { ref len, ref index } => {
352 let len = codegen_operand(fx, len).load_scalar(fx);
353 let index = codegen_operand(fx, index).load_scalar(fx);
354 let location = fx.get_caller_location(source_info).load_scalar(fx);
358 rustc_hir::LangItem::PanicBoundsCheck,
359 &[index, len, location],
364 let msg_str = msg.description();
365 codegen_panic(fx, msg_str, source_info);
370 TerminatorKind::SwitchInt { discr, switch_ty, targets } => {
371 let discr = codegen_operand(fx, discr).load_scalar(fx);
373 let use_bool_opt = switch_ty.kind() == fx.tcx.types.bool.kind()
374 || (targets.iter().count() == 1 && targets.iter().next().unwrap().0 == 0);
376 assert_eq!(targets.iter().count(), 1);
377 let (then_value, then_block) = targets.iter().next().unwrap();
378 let then_block = fx.get_block(then_block);
379 let else_block = fx.get_block(targets.otherwise());
380 let test_zero = match then_value {
383 _ => unreachable!("{:?}", targets),
386 let discr = crate::optimize::peephole::maybe_unwrap_bint(&mut fx.bcx, discr);
387 let (discr, is_inverted) =
388 crate::optimize::peephole::maybe_unwrap_bool_not(&mut fx.bcx, discr);
389 let test_zero = if is_inverted { !test_zero } else { test_zero };
390 let discr = crate::optimize::peephole::maybe_unwrap_bint(&mut fx.bcx, discr);
391 if let Some(taken) = crate::optimize::peephole::maybe_known_branch_taken(
392 &fx.bcx, discr, test_zero,
395 fx.bcx.ins().jump(then_block, &[]);
397 fx.bcx.ins().jump(else_block, &[]);
401 fx.bcx.ins().brz(discr, then_block, &[]);
402 fx.bcx.ins().jump(else_block, &[]);
404 fx.bcx.ins().brnz(discr, then_block, &[]);
405 fx.bcx.ins().jump(else_block, &[]);
409 let mut switch = ::cranelift_frontend::Switch::new();
410 for (value, block) in targets.iter() {
411 let block = fx.get_block(block);
412 switch.set_entry(value, block);
414 let otherwise_block = fx.get_block(targets.otherwise());
415 switch.emit(&mut fx.bcx, discr, otherwise_block);
418 TerminatorKind::Call {
427 fx.tcx.sess.time("codegen call", || {
428 crate::abi::codegen_terminator_call(
430 mir::SourceInfo { span: *fn_span, ..source_info },
438 TerminatorKind::InlineAsm {
446 if options.contains(InlineAsmOptions::MAY_UNWIND) {
447 fx.tcx.sess.span_fatal(
449 "cranelift doesn't support unwinding from inline assembly.",
453 crate::inline_asm::codegen_inline_asm(
462 TerminatorKind::Resume | TerminatorKind::Abort => {
463 // FIXME implement unwinding
464 fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
466 TerminatorKind::Unreachable => {
467 fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
469 TerminatorKind::Yield { .. }
470 | TerminatorKind::FalseEdge { .. }
471 | TerminatorKind::FalseUnwind { .. }
472 | TerminatorKind::DropAndReplace { .. }
473 | TerminatorKind::GeneratorDrop => {
474 bug!("shouldn't exist at codegen {:?}", bb_data.terminator());
476 TerminatorKind::Drop { place, target, unwind: _ } => {
477 let drop_place = codegen_place(fx, *place);
478 crate::abi::codegen_drop(fx, source_info, drop_place);
480 let target_block = fx.get_block(*target);
481 fx.bcx.ins().jump(target_block, &[]);
486 fx.bcx.seal_all_blocks();
490 fn codegen_stmt<'tcx>(
491 fx: &mut FunctionCx<'_, '_, 'tcx>,
492 #[allow(unused_variables)] cur_block: Block,
493 stmt: &Statement<'tcx>,
495 let _print_guard = crate::PrintOnPanic(|| format!("stmt {:?}", stmt));
497 fx.set_debug_loc(stmt.source_info);
499 #[cfg(any())] // This is never true
501 StatementKind::StorageLive(..) | StatementKind::StorageDead(..) => {} // Those are not very useful
503 if fx.clif_comments.enabled() {
504 let inst = fx.bcx.func.layout.last_inst(cur_block).unwrap();
505 fx.add_comment(inst, format!("{:?}", stmt));
511 StatementKind::SetDiscriminant { place, variant_index } => {
512 let place = codegen_place(fx, **place);
513 crate::discriminant::codegen_set_discriminant(fx, place, *variant_index);
515 StatementKind::Assign(to_place_and_rval) => {
516 let lval = codegen_place(fx, to_place_and_rval.0);
517 let dest_layout = lval.layout();
518 match to_place_and_rval.1 {
519 Rvalue::Use(ref operand) => {
520 let val = codegen_operand(fx, operand);
521 lval.write_cvalue(fx, val);
523 Rvalue::CopyForDeref(place) => {
524 let cplace = codegen_place(fx, place);
525 let val = cplace.to_cvalue(fx);
526 lval.write_cvalue(fx, val)
528 Rvalue::Ref(_, _, place) | Rvalue::AddressOf(_, place) => {
529 let place = codegen_place(fx, place);
530 let ref_ = place.place_ref(fx, lval.layout());
531 lval.write_cvalue(fx, ref_);
533 Rvalue::ThreadLocalRef(def_id) => {
534 let val = crate::constant::codegen_tls_ref(fx, def_id, lval.layout());
535 lval.write_cvalue(fx, val);
537 Rvalue::BinaryOp(bin_op, ref lhs_rhs) => {
538 let lhs = codegen_operand(fx, &lhs_rhs.0);
539 let rhs = codegen_operand(fx, &lhs_rhs.1);
541 let res = crate::num::codegen_binop(fx, bin_op, lhs, rhs);
542 lval.write_cvalue(fx, res);
544 Rvalue::CheckedBinaryOp(bin_op, ref lhs_rhs) => {
545 let lhs = codegen_operand(fx, &lhs_rhs.0);
546 let rhs = codegen_operand(fx, &lhs_rhs.1);
548 let res = if !fx.tcx.sess.overflow_checks() {
550 crate::num::codegen_int_binop(fx, bin_op, lhs, rhs).load_scalar(fx);
551 let is_overflow = fx.bcx.ins().iconst(types::I8, 0);
552 CValue::by_val_pair(val, is_overflow, lval.layout())
554 crate::num::codegen_checked_int_binop(fx, bin_op, lhs, rhs)
557 lval.write_cvalue(fx, res);
559 Rvalue::UnaryOp(un_op, ref operand) => {
560 let operand = codegen_operand(fx, operand);
561 let layout = operand.layout();
562 let val = operand.load_scalar(fx);
563 let res = match un_op {
564 UnOp::Not => match layout.ty.kind() {
566 let res = fx.bcx.ins().icmp_imm(IntCC::Equal, val, 0);
567 CValue::by_val(fx.bcx.ins().bint(types::I8, res), layout)
569 ty::Uint(_) | ty::Int(_) => {
570 CValue::by_val(fx.bcx.ins().bnot(val), layout)
572 _ => unreachable!("un op Not for {:?}", layout.ty),
574 UnOp::Neg => match layout.ty.kind() {
575 ty::Int(IntTy::I128) => {
576 // FIXME remove this case once ineg.i128 works
578 CValue::const_val(fx, layout, ty::ScalarInt::null(layout.size));
579 crate::num::codegen_int_binop(fx, BinOp::Sub, zero, operand)
581 ty::Int(_) => CValue::by_val(fx.bcx.ins().ineg(val), layout),
582 ty::Float(_) => CValue::by_val(fx.bcx.ins().fneg(val), layout),
583 _ => unreachable!("un op Neg for {:?}", layout.ty),
586 lval.write_cvalue(fx, res);
589 CastKind::Pointer(PointerCast::ReifyFnPointer),
593 let from_ty = fx.monomorphize(operand.ty(&fx.mir.local_decls, fx.tcx));
594 let to_layout = fx.layout_of(fx.monomorphize(to_ty));
595 match *from_ty.kind() {
596 ty::FnDef(def_id, substs) => {
597 let func_ref = fx.get_function_ref(
598 Instance::resolve_for_fn_ptr(
600 ParamEnv::reveal_all(),
605 .polymorphize(fx.tcx),
607 let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);
608 lval.write_cvalue(fx, CValue::by_val(func_addr, to_layout));
610 _ => bug!("Trying to ReifyFnPointer on non FnDef {:?}", from_ty),
614 CastKind::Pointer(PointerCast::UnsafeFnPointer),
619 CastKind::Pointer(PointerCast::MutToConstPointer),
624 CastKind::Pointer(PointerCast::ArrayToPointer),
628 let to_layout = fx.layout_of(fx.monomorphize(to_ty));
629 let operand = codegen_operand(fx, operand);
630 lval.write_cvalue(fx, operand.cast_pointer_to(to_layout));
634 | CastKind::PointerExposeAddress
635 | CastKind::PointerFromExposedAddress,
639 let operand = codegen_operand(fx, operand);
640 let from_ty = operand.layout().ty;
641 let to_ty = fx.monomorphize(to_ty);
643 fn is_fat_ptr<'tcx>(fx: &FunctionCx<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool {
644 ty.builtin_deref(true)
645 .map(|ty::TypeAndMut { ty: pointee_ty, mutbl: _ }| {
646 has_ptr_meta(fx.tcx, pointee_ty)
651 if is_fat_ptr(fx, from_ty) {
652 if is_fat_ptr(fx, to_ty) {
653 // fat-ptr -> fat-ptr
654 lval.write_cvalue(fx, operand.cast_pointer_to(dest_layout));
656 // fat-ptr -> thin-ptr
657 let (ptr, _extra) = operand.load_scalar_pair(fx);
658 lval.write_cvalue(fx, CValue::by_val(ptr, dest_layout))
661 let to_clif_ty = fx.clif_type(to_ty).unwrap();
662 let from = operand.load_scalar(fx);
664 let res = clif_int_or_float_cast(
671 lval.write_cvalue(fx, CValue::by_val(res, dest_layout));
675 CastKind::Pointer(PointerCast::ClosureFnPointer(_)),
679 let operand = codegen_operand(fx, operand);
680 match *operand.layout().ty.kind() {
681 ty::Closure(def_id, substs) => {
682 let instance = Instance::resolve_closure(
686 ty::ClosureKind::FnOnce,
688 .expect("failed to normalize and resolve closure during codegen")
689 .polymorphize(fx.tcx);
690 let func_ref = fx.get_function_ref(instance);
691 let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);
692 lval.write_cvalue(fx, CValue::by_val(func_addr, lval.layout()));
694 _ => bug!("{} cannot be cast to a fn ptr", operand.layout().ty),
697 Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), ref operand, _to_ty) => {
698 let operand = codegen_operand(fx, operand);
699 operand.unsize_value(fx, lval);
701 Rvalue::Discriminant(place) => {
702 let place = codegen_place(fx, place);
703 let value = place.to_cvalue(fx);
704 crate::discriminant::codegen_get_discriminant(fx, lval, value, dest_layout);
706 Rvalue::Repeat(ref operand, times) => {
707 let operand = codegen_operand(fx, operand);
710 .eval(fx.tcx, ParamEnv::reveal_all())
712 .try_to_bits(fx.tcx.data_layout.pointer_size)
714 if operand.layout().size.bytes() == 0 {
715 // Do nothing for ZST's
716 } else if fx.clif_type(operand.layout().ty) == Some(types::I8) {
717 let times = fx.bcx.ins().iconst(fx.pointer_type, times as i64);
718 // FIXME use emit_small_memset where possible
719 let addr = lval.to_ptr().get_addr(fx);
720 let val = operand.load_scalar(fx);
721 fx.bcx.call_memset(fx.target_config, addr, val, times);
723 let loop_block = fx.bcx.create_block();
724 let loop_block2 = fx.bcx.create_block();
725 let done_block = fx.bcx.create_block();
726 let index = fx.bcx.append_block_param(loop_block, fx.pointer_type);
727 let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
728 fx.bcx.ins().jump(loop_block, &[zero]);
730 fx.bcx.switch_to_block(loop_block);
731 let done = fx.bcx.ins().icmp_imm(IntCC::Equal, index, times as i64);
732 fx.bcx.ins().brnz(done, done_block, &[]);
733 fx.bcx.ins().jump(loop_block2, &[]);
735 fx.bcx.switch_to_block(loop_block2);
736 let to = lval.place_index(fx, index);
737 to.write_cvalue(fx, operand);
738 let index = fx.bcx.ins().iadd_imm(index, 1);
739 fx.bcx.ins().jump(loop_block, &[index]);
741 fx.bcx.switch_to_block(done_block);
745 Rvalue::Len(place) => {
746 let place = codegen_place(fx, place);
747 let usize_layout = fx.layout_of(fx.tcx.types.usize);
748 let len = codegen_array_len(fx, place);
749 lval.write_cvalue(fx, CValue::by_val(len, usize_layout));
751 Rvalue::ShallowInitBox(ref operand, content_ty) => {
752 let content_ty = fx.monomorphize(content_ty);
753 let box_layout = fx.layout_of(fx.tcx.mk_box(content_ty));
754 let operand = codegen_operand(fx, operand);
755 let operand = operand.load_scalar(fx);
756 lval.write_cvalue(fx, CValue::by_val(operand, box_layout));
758 Rvalue::NullaryOp(null_op, ty) => {
762 .is_sized(fx.tcx.at(stmt.source_info.span), ParamEnv::reveal_all())
764 let layout = fx.layout_of(fx.monomorphize(ty));
765 let val = match null_op {
766 NullOp::SizeOf => layout.size.bytes(),
767 NullOp::AlignOf => layout.align.abi.bytes(),
769 let val = CValue::const_val(fx, fx.layout_of(fx.tcx.types.usize), val.into());
770 lval.write_cvalue(fx, val);
772 Rvalue::Aggregate(ref kind, ref operands) => match kind.as_ref() {
773 AggregateKind::Array(_ty) => {
774 for (i, operand) in operands.iter().enumerate() {
775 let operand = codegen_operand(fx, operand);
776 let index = fx.bcx.ins().iconst(fx.pointer_type, i as i64);
777 let to = lval.place_index(fx, index);
778 to.write_cvalue(fx, operand);
781 _ => unreachable!("shouldn't exist at codegen {:?}", to_place_and_rval.1),
785 StatementKind::StorageLive(_)
786 | StatementKind::StorageDead(_)
787 | StatementKind::Deinit(_)
789 | StatementKind::FakeRead(..)
790 | StatementKind::Retag { .. }
791 | StatementKind::AscribeUserType(..) => {}
793 StatementKind::Coverage { .. } => fx.tcx.sess.fatal("-Zcoverage is unimplemented"),
794 StatementKind::CopyNonOverlapping(inner) => {
795 let dst = codegen_operand(fx, &inner.dst);
798 .pointee_info_at(fx, rustc_target::abi::Size::ZERO)
799 .expect("Expected pointer");
800 let dst = dst.load_scalar(fx);
801 let src = codegen_operand(fx, &inner.src).load_scalar(fx);
802 let count = codegen_operand(fx, &inner.count).load_scalar(fx);
803 let elem_size: u64 = pointee.size.bytes();
805 if elem_size != 1 { fx.bcx.ins().imul_imm(count, elem_size as i64) } else { count };
806 fx.bcx.call_memcpy(fx.target_config, dst, src, bytes);
811 fn codegen_array_len<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, place: CPlace<'tcx>) -> Value {
812 match *place.layout().ty.kind() {
813 ty::Array(_elem_ty, len) => {
814 let len = fx.monomorphize(len).eval_usize(fx.tcx, ParamEnv::reveal_all()) as i64;
815 fx.bcx.ins().iconst(fx.pointer_type, len)
817 ty::Slice(_elem_ty) => {
818 place.to_ptr_maybe_unsized().1.expect("Length metadata for slice place")
820 _ => bug!("Rvalue::Len({:?})", place),
824 pub(crate) fn codegen_place<'tcx>(
825 fx: &mut FunctionCx<'_, '_, 'tcx>,
828 let mut cplace = fx.get_local_place(place.local);
830 for elem in place.projection {
832 PlaceElem::Deref => {
833 cplace = cplace.place_deref(fx);
835 PlaceElem::Field(field, _ty) => {
836 cplace = cplace.place_field(fx, field);
838 PlaceElem::Index(local) => {
839 let index = fx.get_local_place(local).to_cvalue(fx).load_scalar(fx);
840 cplace = cplace.place_index(fx, index);
842 PlaceElem::ConstantIndex { offset, min_length: _, from_end } => {
843 let offset: u64 = offset;
844 let index = if !from_end {
845 fx.bcx.ins().iconst(fx.pointer_type, offset as i64)
847 let len = codegen_array_len(fx, cplace);
848 fx.bcx.ins().iadd_imm(len, -(offset as i64))
850 cplace = cplace.place_index(fx, index);
852 PlaceElem::Subslice { from, to, from_end } => {
853 // These indices are generated by slice patterns.
854 // slice[from:-to] in Python terms.
856 let from: u64 = from;
859 match cplace.layout().ty.kind() {
860 ty::Array(elem_ty, _len) => {
861 assert!(!from_end, "array subslices are never `from_end`");
862 let elem_layout = fx.layout_of(*elem_ty);
863 let ptr = cplace.to_ptr();
864 cplace = CPlace::for_ptr(
865 ptr.offset_i64(fx, elem_layout.size.bytes() as i64 * (from as i64)),
866 fx.layout_of(fx.tcx.mk_array(*elem_ty, to - from)),
869 ty::Slice(elem_ty) => {
870 assert!(from_end, "slice subslices should be `from_end`");
871 let elem_layout = fx.layout_of(*elem_ty);
872 let (ptr, len) = cplace.to_ptr_maybe_unsized();
873 let len = len.unwrap();
874 cplace = CPlace::for_ptr_with_extra(
875 ptr.offset_i64(fx, elem_layout.size.bytes() as i64 * (from as i64)),
876 fx.bcx.ins().iadd_imm(len, -(from as i64 + to as i64)),
883 PlaceElem::Downcast(_adt_def, variant) => {
884 cplace = cplace.downcast_variant(fx, variant);
892 pub(crate) fn codegen_operand<'tcx>(
893 fx: &mut FunctionCx<'_, '_, 'tcx>,
894 operand: &Operand<'tcx>,
897 Operand::Move(place) | Operand::Copy(place) => {
898 let cplace = codegen_place(fx, *place);
901 Operand::Constant(const_) => crate::constant::codegen_constant(fx, const_),
905 pub(crate) fn codegen_panic<'tcx>(
906 fx: &mut FunctionCx<'_, '_, 'tcx>,
908 source_info: mir::SourceInfo,
910 let location = fx.get_caller_location(source_info).load_scalar(fx);
912 let msg_ptr = fx.anonymous_str(msg_str);
913 let msg_len = fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(msg_str.len()).unwrap());
914 let args = [msg_ptr, msg_len, location];
916 codegen_panic_inner(fx, rustc_hir::LangItem::Panic, &args, source_info.span);
919 pub(crate) fn codegen_panic_inner<'tcx>(
920 fx: &mut FunctionCx<'_, '_, 'tcx>,
921 lang_item: rustc_hir::LangItem,
926 fx.tcx.lang_items().require(lang_item).unwrap_or_else(|s| fx.tcx.sess.span_fatal(span, &s));
928 let instance = Instance::mono(fx.tcx, def_id).polymorphize(fx.tcx);
929 let symbol_name = fx.tcx.symbol_name(instance).name;
934 AbiParam::new(fx.pointer_type),
935 AbiParam::new(fx.pointer_type),
936 AbiParam::new(fx.pointer_type),
942 fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);