1 use rustc::ty::adjustment::PointerCast;
5 pub fn trans_fn<'clif, 'tcx, B: Backend + 'static>(
6 cx: &mut crate::CodegenCx<'clif, 'tcx, B>,
7 instance: Instance<'tcx>,
12 let mir = tcx.instance_mir(instance.def);
15 let (name, sig) = get_function_name_and_sig(tcx, instance, false);
16 let func_id = cx.module.declare_function(&name, linkage, &sig).unwrap();
17 let mut debug_context = cx
20 .map(|debug_context| FunctionDebugContext::new(tcx, debug_context, mir, &name, &sig));
22 // Make FunctionBuilder
23 let mut func = Function::with_name_signature(ExternalName::user(0, 0), sig);
24 let mut func_ctx = FunctionBuilderContext::new();
25 let mut bcx = FunctionBuilder::new(&mut func, &mut func_ctx);
28 let start_ebb = bcx.create_ebb();
29 let mut ebb_map: HashMap<BasicBlock, Ebb> = HashMap::new();
30 for (bb, _bb_data) in mir.basic_blocks().iter_enumerated() {
31 ebb_map.insert(bb, bcx.create_ebb());
35 let pointer_type = cx.module.target_config().pointer_type();
36 let clif_comments = crate::pretty_clif::CommentWriter::new(tcx, instance);
38 let mut fx = FunctionCx {
48 local_map: HashMap::new(),
51 constants_cx: &mut cx.constants_cx,
52 caches: &mut cx.caches,
53 source_info_set: indexmap::IndexSet::new(),
56 with_unimpl_span(fx.mir.span, || {
57 crate::abi::codegen_fn_prelude(&mut fx, start_ebb);
58 codegen_fn_content(&mut fx);
61 // Recover all necessary data from fx, before accessing func will prevent future access to it.
62 let instance = fx.instance;
63 let clif_comments = fx.clif_comments;
64 let source_info_set = fx.source_info_set;
66 #[cfg(debug_assertions)]
67 crate::pretty_clif::write_clif_file(cx.tcx, "unopt", instance, &func, &clif_comments, None);
70 verify_func(tcx, &clif_comments, &func);
73 let context = &mut cx.caches.context;
75 cx.module.define_function(func_id, context).unwrap();
77 let value_ranges = context
78 .build_value_labels_ranges(cx.module.isa())
79 .expect("value location ranges");
81 // Write optimized function to file for debugging
82 #[cfg(debug_assertions)]
83 crate::pretty_clif::write_clif_file(
92 // Define debuginfo for function
93 let isa = cx.module.isa();
96 .map(|x| x.define(tcx, context, isa, &source_info_set));
98 // Clear context to make it usable for the next function
102 fn verify_func(tcx: TyCtxt, writer: &crate::pretty_clif::CommentWriter, func: &Function) {
103 let flags = settings::Flags::new(settings::builder());
104 match ::cranelift::codegen::verify_function(&func, &flags) {
107 tcx.sess.err(&format!("{:?}", err));
108 let pretty_error = ::cranelift::codegen::print_errors::pretty_verifier_error(
111 Some(Box::new(writer)),
115 .fatal(&format!("cranelift verify error:\n{}", pretty_error));
120 fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Backend>) {
121 for (bb, bb_data) in fx.mir.basic_blocks().iter_enumerated() {
122 if bb_data.is_cleanup {
123 // Unwinding after panicking is not supported
127 let ebb = fx.get_ebb(bb);
128 fx.bcx.switch_to_block(ebb);
131 for stmt in &bb_data.statements {
132 fx.set_debug_loc(stmt.source_info);
133 trans_stmt(fx, ebb, stmt);
136 #[cfg(debug_assertions)]
138 let mut terminator_head = "\n".to_string();
142 .fmt_head(&mut terminator_head)
144 let inst = fx.bcx.func.layout.last_inst(ebb).unwrap();
145 fx.add_comment(inst, terminator_head);
148 fx.set_debug_loc(bb_data.terminator().source_info);
150 match &bb_data.terminator().kind {
151 TerminatorKind::Goto { target } => {
152 let ebb = fx.get_ebb(*target);
153 fx.bcx.ins().jump(ebb, &[]);
155 TerminatorKind::Return => {
156 crate::abi::codegen_return(fx);
158 TerminatorKind::Assert {
165 let cond = trans_operand(fx, cond).load_scalar(fx);
166 // TODO HACK brz/brnz for i8/i16 is not yet implemented
167 let cond = fx.bcx.ins().uextend(types::I32, cond);
168 let target = fx.get_ebb(*target);
170 fx.bcx.ins().brnz(cond, target, &[]);
172 fx.bcx.ins().brz(cond, target, &[]);
177 "[panic] Assert {:?} at {:?} failed.",
179 bb_data.terminator().source_info.span
184 TerminatorKind::SwitchInt {
190 let discr = trans_operand(fx, discr).load_scalar(fx);
191 let mut switch = ::cranelift::frontend::Switch::new();
192 for (i, value) in values.iter().enumerate() {
193 let ebb = fx.get_ebb(targets[i]);
194 switch.set_entry(*value as u64, ebb);
196 let otherwise_ebb = fx.get_ebb(targets[targets.len() - 1]);
197 switch.emit(&mut fx.bcx, discr, otherwise_ebb);
199 TerminatorKind::Call {
206 crate::abi::codegen_terminator_call(fx, func, args, destination);
208 TerminatorKind::Resume | TerminatorKind::Abort => {
209 trap_unreachable(fx, "[corruption] Unwinding bb reached.");
211 TerminatorKind::Unreachable => {
212 trap_unreachable(fx, "[corruption] Hit unreachable code.");
214 TerminatorKind::Yield { .. }
215 | TerminatorKind::FalseEdges { .. }
216 | TerminatorKind::FalseUnwind { .. }
217 | TerminatorKind::DropAndReplace { .. }
218 | TerminatorKind::GeneratorDrop => {
219 bug!("shouldn't exist at trans {:?}", bb_data.terminator());
221 TerminatorKind::Drop {
226 let drop_place = trans_place(fx, location);
227 crate::abi::codegen_drop(fx, drop_place);
229 let target_ebb = fx.get_ebb(*target);
230 fx.bcx.ins().jump(target_ebb, &[]);
235 fx.bcx.seal_all_blocks();
240 fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
242 stmt: &Statement<'tcx>,
244 let _print_guard = PrintOnPanic(|| format!("stmt {:?}", stmt));
246 fx.set_debug_loc(stmt.source_info);
248 #[cfg(debug_assertions)]
250 StatementKind::StorageLive(..) | StatementKind::StorageDead(..) => {} // Those are not very useful
252 let inst = fx.bcx.func.layout.last_inst(cur_ebb).unwrap();
253 fx.add_comment(inst, format!("{:?}", stmt));
258 StatementKind::SetDiscriminant {
262 let place = trans_place(fx, place);
263 crate::discriminant::codegen_set_discriminant(fx, place, *variant_index);
265 StatementKind::Assign(to_place, rval) => {
266 let lval = trans_place(fx, to_place);
267 let dest_layout = lval.layout();
269 Rvalue::Use(operand) => {
270 let val = trans_operand(fx, operand);
271 lval.write_cvalue(fx, val);
273 Rvalue::Ref(_, _, place) => {
274 let place = trans_place(fx, place);
275 place.write_place_ref(fx, lval);
277 Rvalue::BinaryOp(bin_op, lhs, rhs) => {
278 let lhs = trans_operand(fx, lhs);
279 let rhs = trans_operand(fx, rhs);
281 let res = crate::num::codegen_binop(fx, *bin_op, lhs, rhs);
282 lval.write_cvalue(fx, res);
284 Rvalue::CheckedBinaryOp(bin_op, lhs, rhs) => {
285 let lhs = trans_operand(fx, lhs);
286 let rhs = trans_operand(fx, rhs);
288 let res = if !fx.tcx.sess.overflow_checks() {
290 crate::num::trans_int_binop(fx, *bin_op, lhs, rhs).load_scalar(fx);
291 let is_overflow = fx.bcx.ins().iconst(types::I8, 0);
292 CValue::by_val_pair(val, is_overflow, lval.layout())
294 crate::num::trans_checked_int_binop(fx, *bin_op, lhs, rhs)
297 lval.write_cvalue(fx, res);
299 Rvalue::UnaryOp(un_op, operand) => {
300 let operand = trans_operand(fx, operand);
301 let layout = operand.layout();
302 let val = operand.load_scalar(fx);
303 let res = match un_op {
305 match layout.ty.sty {
307 let val = fx.bcx.ins().uextend(types::I32, val); // WORKAROUND for CraneStation/cranelift#466
308 let res = fx.bcx.ins().icmp_imm(IntCC::Equal, val, 0);
309 fx.bcx.ins().bint(types::I8, res)
311 ty::Uint(_) | ty::Int(_) => fx.bcx.ins().bnot(val),
312 _ => unimplemented!("un op Not for {:?}", layout.ty),
315 UnOp::Neg => match layout.ty.sty {
317 let clif_ty = fx.clif_type(layout.ty).unwrap();
318 if clif_ty == types::I128 {
319 // FIXME implement it
320 crate::trap::trap_unreachable_ret_value(
323 "i128 neg is not yet supported",
327 let zero = fx.bcx.ins().iconst(clif_ty, 0);
328 fx.bcx.ins().isub(zero, val)
331 ty::Float(_) => fx.bcx.ins().fneg(val),
332 _ => unimplemented!("un op Neg for {:?}", layout.ty),
335 lval.write_cvalue(fx, CValue::by_val(res, layout));
337 Rvalue::Cast(CastKind::Pointer(PointerCast::ReifyFnPointer), operand, ty) => {
338 let layout = fx.layout_of(ty);
340 .monomorphize(&operand.ty(&fx.mir.local_decls, fx.tcx))
343 ty::FnDef(def_id, substs) => {
344 let func_ref = fx.get_function_ref(
345 Instance::resolve(fx.tcx, ParamEnv::reveal_all(), def_id, substs)
348 let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);
349 lval.write_cvalue(fx, CValue::by_val(func_addr, layout));
351 _ => bug!("Trying to ReifyFnPointer on non FnDef {:?}", ty),
354 Rvalue::Cast(CastKind::Pointer(PointerCast::UnsafeFnPointer), operand, ty)
355 | Rvalue::Cast(CastKind::Pointer(PointerCast::MutToConstPointer), operand, ty) => {
356 let operand = trans_operand(fx, operand);
357 let layout = fx.layout_of(ty);
358 lval.write_cvalue(fx, operand.unchecked_cast_to(layout));
360 Rvalue::Cast(CastKind::Misc, operand, to_ty) => {
361 let operand = trans_operand(fx, operand);
362 let from_ty = operand.layout().ty;
365 fx: &FunctionCx<'_, 'tcx, impl Backend>,
368 ty.builtin_deref(true)
374 fx.layout_of(pointee_ty).is_unsized()
380 if is_fat_ptr(fx, from_ty) {
381 if is_fat_ptr(fx, to_ty) {
382 // fat-ptr -> fat-ptr
383 lval.write_cvalue(fx, operand.unchecked_cast_to(dest_layout));
385 // fat-ptr -> thin-ptr
386 let (ptr, _extra) = operand.load_scalar_pair(fx);
387 lval.write_cvalue(fx, CValue::by_val(ptr, dest_layout))
389 } else if let ty::Adt(adt_def, _substs) = from_ty.sty {
390 // enum -> discriminant value
391 assert!(adt_def.is_enum());
393 ty::Uint(_) | ty::Int(_) => {}
394 _ => unreachable!("cast adt {} -> {}", from_ty, to_ty),
397 let discr = crate::discriminant::codegen_get_discriminant(
402 lval.write_cvalue(fx, discr);
404 let to_clif_ty = fx.clif_type(to_ty).unwrap();
405 let from = operand.load_scalar(fx);
407 let res = clif_int_or_float_cast(
414 lval.write_cvalue(fx, CValue::by_val(res, dest_layout));
417 Rvalue::Cast(CastKind::Pointer(PointerCast::ClosureFnPointer(_)), operand, _ty) => {
418 let operand = trans_operand(fx, operand);
419 match operand.layout().ty.sty {
420 ty::Closure(def_id, substs) => {
421 let instance = Instance::resolve_closure(
425 ty::ClosureKind::FnOnce,
427 let func_ref = fx.get_function_ref(instance);
428 let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);
429 lval.write_cvalue(fx, CValue::by_val(func_addr, lval.layout()));
431 _ => bug!("{} cannot be cast to a fn ptr", operand.layout().ty),
434 Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), operand, _ty) => {
435 let operand = trans_operand(fx, operand);
436 operand.unsize_value(fx, lval);
438 Rvalue::Discriminant(place) => {
439 let place = trans_place(fx, place);
440 let value = place.to_cvalue(fx);
442 crate::discriminant::codegen_get_discriminant(fx, value, dest_layout);
443 lval.write_cvalue(fx, discr);
445 Rvalue::Repeat(operand, times) => {
446 let operand = trans_operand(fx, operand);
448 let index = fx.bcx.ins().iconst(fx.pointer_type, i as i64);
449 let to = lval.place_index(fx, index);
450 to.write_cvalue(fx, operand);
453 Rvalue::Len(place) => {
454 let place = trans_place(fx, place);
455 let usize_layout = fx.layout_of(fx.tcx.types.usize);
456 let len = codegen_array_len(fx, place);
457 lval.write_cvalue(fx, CValue::by_val(len, usize_layout));
459 Rvalue::NullaryOp(NullOp::Box, content_ty) => {
460 use rustc::middle::lang_items::ExchangeMallocFnLangItem;
462 let usize_type = fx.clif_type(fx.tcx.types.usize).unwrap();
463 let layout = fx.layout_of(content_ty);
464 let llsize = fx.bcx.ins().iconst(usize_type, layout.size.bytes() as i64);
468 .iconst(usize_type, layout.align.abi.bytes() as i64);
469 let box_layout = fx.layout_of(fx.tcx.mk_box(content_ty));
472 let def_id = match fx.tcx.lang_items().require(ExchangeMallocFnLangItem) {
477 .fatal(&format!("allocation of `{}` {}", box_layout.ty, s));
480 let instance = ty::Instance::mono(fx.tcx, def_id);
481 let func_ref = fx.get_function_ref(instance);
482 let call = fx.bcx.ins().call(func_ref, &[llsize, llalign]);
483 let ptr = fx.bcx.inst_results(call)[0];
484 lval.write_cvalue(fx, CValue::by_val(ptr, box_layout));
486 Rvalue::NullaryOp(NullOp::SizeOf, ty) => {
490 .is_sized(fx.tcx.at(DUMMY_SP), ParamEnv::reveal_all()));
491 let ty_size = fx.layout_of(ty).size.bytes();
492 let val = CValue::const_val(fx, fx.tcx.types.usize, ty_size.into());
493 lval.write_cvalue(fx, val);
495 Rvalue::Aggregate(kind, operands) => match **kind {
496 AggregateKind::Array(_ty) => {
497 for (i, operand) in operands.into_iter().enumerate() {
498 let operand = trans_operand(fx, operand);
499 let index = fx.bcx.ins().iconst(fx.pointer_type, i as i64);
500 let to = lval.place_index(fx, index);
501 to.write_cvalue(fx, operand);
504 _ => unimpl!("shouldn't exist at trans {:?}", rval),
508 StatementKind::StorageLive(_)
509 | StatementKind::StorageDead(_)
511 | StatementKind::FakeRead(..)
512 | StatementKind::Retag { .. }
513 | StatementKind::AscribeUserType(..) => {}
515 StatementKind::InlineAsm(asm) => {
516 use syntax::ast::Name;
522 let rustc::hir::InlineAsm {
523 asm: asm_code, // Name
524 outputs, // Vec<Name>
526 clobbers, // Vec<Name>
529 dialect: _, // syntax::ast::AsmDialect
532 match &*asm_code.as_str() {
533 "cpuid" | "cpuid\n" => {
534 assert_eq!(inputs, &[Name::intern("{eax}"), Name::intern("{ecx}")]);
536 assert_eq!(outputs.len(), 4);
537 for (i, c) in (&["={eax}", "={ebx}", "={ecx}", "={edx}"])
541 assert_eq!(&outputs[i].constraint.as_str(), c);
542 assert!(!outputs[i].is_rw);
543 assert!(!outputs[i].is_indirect);
546 assert_eq!(clobbers, &[Name::intern("rbx")]);
549 assert!(!alignstack);
551 crate::trap::trap_unimplemented(
553 "__cpuid_count arch intrinsic is not supported",
557 assert_eq!(inputs, &[Name::intern("{ecx}")]);
559 assert_eq!(outputs.len(), 2);
560 for (i, c) in (&["={eax}", "={edx}"]).iter().enumerate() {
561 assert_eq!(&outputs[i].constraint.as_str(), c);
562 assert!(!outputs[i].is_rw);
563 assert!(!outputs[i].is_indirect);
566 assert_eq!(clobbers, &[]);
569 assert!(!alignstack);
571 crate::trap::trap_unimplemented(fx, "_xgetbv arch intrinsic is not supported");
573 _ if fx.tcx.symbol_name(fx.instance).as_str() == "__rust_probestack" => {
574 crate::trap::trap_unimplemented(fx, "__rust_probestack is not supported");
576 _ => unimpl!("Inline assembly is not supported"),
582 fn codegen_array_len<'tcx>(
583 fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
586 match place.layout().ty.sty {
587 ty::Array(_elem_ty, len) => {
588 let len = crate::constant::force_eval_const(fx, len)
589 .eval_usize(fx.tcx, ParamEnv::reveal_all()) as i64;
590 fx.bcx.ins().iconst(fx.pointer_type, len)
592 ty::Slice(_elem_ty) => place
593 .to_addr_maybe_unsized(fx)
595 .expect("Length metadata for slice place"),
596 _ => bug!("Rvalue::Len({:?})", place),
600 pub fn trans_place<'tcx>(
601 fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
604 let base = match &place.base {
605 PlaceBase::Local(local) => fx.get_local_place(*local),
606 PlaceBase::Static(static_) => match static_.kind {
607 StaticKind::Static => {
608 crate::constant::codegen_static_ref(fx, static_.def_id, static_.ty)
610 StaticKind::Promoted(promoted, substs) => {
611 let instance = Instance::new(static_.def_id, fx.monomorphize(&substs));
612 crate::constant::trans_promoted(fx, instance, promoted, static_.ty)
617 trans_place_projection(fx, base, &place.projection)
620 pub fn trans_place_projection<'tcx>(
621 fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
623 projection: &Option<Box<Projection<'tcx>>>,
625 let projection = if let Some(projection) = projection {
631 let base = trans_place_projection(fx, base, &projection.base);
633 match projection.elem {
634 ProjectionElem::Deref => base.place_deref(fx),
635 ProjectionElem::Field(field, _ty) => base.place_field(fx, field),
636 ProjectionElem::Index(local) => {
637 let index = fx.get_local_place(local).to_cvalue(fx).load_scalar(fx);
638 base.place_index(fx, index)
640 ProjectionElem::ConstantIndex {
645 let index = if !from_end {
646 fx.bcx.ins().iconst(fx.pointer_type, offset as i64)
648 let len = codegen_array_len(fx, base);
649 fx.bcx.ins().iadd_imm(len, -(offset as i64))
651 base.place_index(fx, index)
653 ProjectionElem::Subslice { from, to } => {
654 // These indices are generated by slice patterns.
655 // slice[from:-to] in Python terms.
657 match base.layout().ty.sty {
658 ty::Array(elem_ty, len) => {
659 let elem_layout = fx.layout_of(elem_ty);
660 let ptr = base.to_addr(fx);
661 let len = crate::constant::force_eval_const(fx, len)
662 .eval_usize(fx.tcx, ParamEnv::reveal_all());
666 .iadd_imm(ptr, elem_layout.size.bytes() as i64 * from as i64),
667 fx.layout_of(fx.tcx.mk_array(elem_ty, len - from as u64 - to as u64)),
670 ty::Slice(elem_ty) => {
671 let elem_layout = fx.layout_of(elem_ty);
672 let (ptr, len) = base.to_addr_maybe_unsized(fx);
673 let len = len.unwrap();
674 CPlace::for_addr_with_extra(
677 .iadd_imm(ptr, elem_layout.size.bytes() as i64 * from as i64),
678 fx.bcx.ins().iadd_imm(len, -(from as i64 + to as i64)),
685 ProjectionElem::Downcast(_adt_def, variant) => base.downcast_variant(fx, variant),
689 pub fn trans_operand<'tcx>(
690 fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
691 operand: &Operand<'tcx>,
694 Operand::Move(place) | Operand::Copy(place) => {
695 let cplace = trans_place(fx, place);
698 Operand::Constant(const_) => crate::constant::trans_constant(fx, const_),