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, 'tcx: 'a>(
13 tcx: TyCtxt<'a, 'tcx, 'tcx>,
14 module: &mut Module<impl Backend>,
16 ccx: &mut crate::constant::ConstantCx,
17 mono_item: MonoItem<'tcx>,
20 MonoItem::Fn(inst) => {
21 let _inst_guard = PrintOnPanic(|| format!("{:?}", inst));
22 let _mir_guard = PrintOnPanic(|| {
25 | InstanceDef::DropGlue(_, _)
26 | InstanceDef::Virtual(_, _) => {
27 let mut mir = ::std::io::Cursor::new(Vec::new());
28 ::rustc_mir::util::write_mir_pretty(tcx, Some(inst.def_id()), &mut mir)
30 String::from_utf8(mir.into_inner()).unwrap()
32 InstanceDef::FnPtrShim(_, _)
33 | InstanceDef::ClosureOnceShim { .. }
34 | InstanceDef::CloneShim(_, _) => {
35 // FIXME fix write_mir_pretty for these instances
36 format!("{:#?}", tcx.instance_mir(inst.def))
38 InstanceDef::Intrinsic(_) => bug!("tried to codegen intrinsic"),
42 trans_fn(tcx, module, ccx, caches, inst);
44 MonoItem::Static(def_id) => {
45 crate::constant::codegen_static(ccx, def_id);
47 MonoItem::GlobalAsm(node_id) => tcx
49 .fatal(&format!("Unimplemented global asm mono item {:?}", node_id)),
53 fn trans_fn<'a, 'tcx: 'a>(
54 tcx: TyCtxt<'a, 'tcx, 'tcx>,
55 module: &mut Module<impl Backend>,
56 constants: &mut crate::constant::ConstantCx,
58 instance: Instance<'tcx>,
61 let mir = tcx.instance_mir(instance.def);
63 // Step 2. Declare function
64 let (name, sig) = get_function_name_and_sig(tcx, instance);
66 .declare_function(&name, Linkage::Export, &sig)
69 // Step 3. Make FunctionBuilder
70 let mut func = Function::with_name_signature(ExternalName::user(0, 0), sig);
71 let mut func_ctx = FunctionBuilderContext::new();
72 let mut bcx: FunctionBuilder = FunctionBuilder::new(&mut func, &mut func_ctx);
74 // Step 4. Predefine ebb's
75 let start_ebb = bcx.create_ebb();
76 let mut ebb_map: HashMap<BasicBlock, Ebb> = HashMap::new();
77 for (bb, _bb_data) in mir.basic_blocks().iter_enumerated() {
78 ebb_map.insert(bb, bcx.create_ebb());
81 // Step 5. Make FunctionCx
82 let mut fx = FunctionCx {
89 assert!(!instance.substs.needs_infer());
93 local_map: HashMap::new(),
94 comments: HashMap::new(),
101 // Step 6. Codegen function
102 crate::abi::codegen_fn_prelude(&mut fx, start_ebb);
103 codegen_fn_content(&mut fx);
104 fx.bcx.seal_all_blocks();
107 // Step 7. Write function to file for debugging
108 let mut writer = crate::pretty_clif::CommentWriter(fx.comments);
110 let mut cton = String::new();
111 if cfg!(debug_assertions) {
112 ::cranelift::codegen::write::decorate_function(&mut writer, &mut cton, &func, None)
114 let clif_file_name = "target/out/clif/".to_string() + &tcx.symbol_name(instance).as_str();
115 ::std::fs::write(clif_file_name, cton.as_bytes()).unwrap();
118 // Step 8. Verify function
119 verify_func(tcx, writer, &func);
121 // Step 9. Define function
122 // TODO: cranelift doesn't yet support some of the things needed
123 if should_codegen(tcx.sess) {
124 caches.context.func = func;
126 .define_function(func_id, &mut caches.context)
128 caches.context.clear();
132 fn verify_func(tcx: TyCtxt, writer: crate::pretty_clif::CommentWriter, func: &Function) {
133 let flags = settings::Flags::new(settings::builder());
134 match ::cranelift::codegen::verify_function(&func, &flags) {
137 tcx.sess.err(&format!("{:?}", err));
138 let pretty_error = ::cranelift::codegen::print_errors::pretty_verifier_error(
141 Some(Box::new(writer)),
145 .fatal(&format!("cretonne verify error:\n{}", pretty_error));
150 fn codegen_fn_content<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx, impl Backend>) {
151 for (bb, bb_data) in fx.mir.basic_blocks().iter_enumerated() {
152 let ebb = fx.get_ebb(bb);
153 fx.bcx.switch_to_block(ebb);
156 for stmt in &bb_data.statements {
157 trans_stmt(fx, ebb, stmt);
160 let mut terminator_head = "\n".to_string();
164 .fmt_head(&mut terminator_head)
166 let inst = fx.bcx.func.layout.last_inst(ebb).unwrap();
167 fx.add_comment(inst, terminator_head);
169 match &bb_data.terminator().kind {
170 TerminatorKind::Goto { target } => {
171 let ebb = fx.get_ebb(*target);
172 fx.bcx.ins().jump(ebb, &[]);
174 TerminatorKind::Return => {
175 crate::abi::codegen_return(fx);
177 TerminatorKind::Assert {
184 let cond = trans_operand(fx, cond).load_value(fx);
185 // TODO HACK brz/brnz for i8/i16 is not yet implemented
186 let cond = fx.bcx.ins().uextend(types::I32, cond);
187 let target = fx.get_ebb(*target);
189 fx.bcx.ins().brnz(cond, target, &[]);
191 fx.bcx.ins().brz(cond, target, &[]);
193 fx.bcx.ins().trap(TrapCode::User(!0));
196 TerminatorKind::SwitchInt {
202 // TODO: prevent panics on large and negative disciminants
203 if should_codegen(fx.tcx.sess) {
204 let discr = trans_operand(fx, discr).load_value(fx);
205 let mut jt_data = JumpTableData::new();
206 for (i, value) in values.iter().enumerate() {
207 let ebb = fx.get_ebb(targets[i]);
208 jt_data.set_entry(*value as usize, ebb);
210 let jump_table = fx.bcx.create_jump_table(jt_data);
211 fx.bcx.ins().br_table(discr, jump_table);
212 let otherwise_ebb = fx.get_ebb(targets[targets.len() - 1]);
213 fx.bcx.ins().jump(otherwise_ebb, &[]);
215 fx.bcx.ins().trap(TrapCode::User(0));
218 TerminatorKind::Call {
224 crate::abi::codegen_call(fx, func, args, destination);
226 TerminatorKind::Resume | TerminatorKind::Abort | TerminatorKind::Unreachable => {
227 fx.bcx.ins().trap(TrapCode::User(!0));
229 TerminatorKind::Yield { .. }
230 | TerminatorKind::FalseEdges { .. }
231 | TerminatorKind::FalseUnwind { .. } => {
232 bug!("shouldn't exist at trans {:?}", bb_data.terminator());
234 TerminatorKind::Drop { target, .. } | TerminatorKind::DropAndReplace { target, .. } => {
235 // TODO call drop impl
236 // unimplemented!("terminator {:?}", bb_data.terminator());
237 let target_ebb = fx.get_ebb(*target);
238 fx.bcx.ins().jump(target_ebb, &[]);
240 TerminatorKind::GeneratorDrop => {
241 unimplemented!("terminator GeneratorDrop");
246 fx.bcx.seal_all_blocks();
250 fn trans_stmt<'a, 'tcx: 'a>(
251 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
253 stmt: &Statement<'tcx>,
255 let _print_guard = PrintOnPanic(|| format!("stmt {:?}", stmt));
257 let inst = fx.bcx.func.layout.last_inst(cur_ebb).unwrap();
258 fx.add_comment(inst, format!("{:?}", stmt));
261 StatementKind::SetDiscriminant {
265 let place = trans_place(fx, place);
266 let layout = place.layout();
267 if layout.for_variant(&*fx, *variant_index).abi == layout::Abi::Uninhabited {
270 match layout.variants {
271 layout::Variants::Single { index } => {
272 assert_eq!(index, *variant_index);
274 layout::Variants::Tagged { .. } => {
275 let ptr = place.place_field(fx, mir::Field::new(0));
280 .discriminant_for_variant(fx.tcx, *variant_index)
282 let discr = CValue::const_val(fx, ptr.layout().ty, to as u64 as i64);
283 ptr.write_cvalue(fx, discr);
285 layout::Variants::NicheFilling {
291 if *variant_index != dataful_variant {
292 let niche = place.place_field(fx, mir::Field::new(0));
293 //let niche_llty = niche.layout.immediate_llvm_type(bx.cx);
294 let niche_value = ((variant_index - *niche_variants.start()) as u128)
295 .wrapping_add(niche_start);
296 // FIXME(eddyb) Check the actual primitive type here.
297 let niche_llval = if niche_value == 0 {
298 CValue::const_val(fx, niche.layout().ty, 0)
300 CValue::const_val(fx, niche.layout().ty, niche_value as u64 as i64)
302 niche.write_cvalue(fx, niche_llval);
307 StatementKind::Assign(to_place, rval) => {
308 let lval = trans_place(fx, to_place);
309 let dest_layout = lval.layout();
311 Rvalue::Use(operand) => {
312 let val = trans_operand(fx, operand);
313 lval.write_cvalue(fx, val);
315 Rvalue::Ref(_, _, place) => {
316 let place = trans_place(fx, place);
317 place.write_place_ref(fx, lval);
319 Rvalue::BinaryOp(bin_op, lhs, rhs) => {
320 let ty = fx.monomorphize(&lhs.ty(&fx.mir.local_decls, fx.tcx));
321 let lhs = trans_operand(fx, lhs);
322 let rhs = trans_operand(fx, rhs);
324 let res = match ty.sty {
325 ty::Bool => trans_bool_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
327 trans_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, false)
330 trans_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, true)
332 ty::Float(_) => trans_float_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
333 ty::Char => trans_char_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
334 ty::RawPtr(..) => trans_ptr_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
335 _ => unimplemented!("binop {:?} for {:?}", bin_op, ty),
337 lval.write_cvalue(fx, res);
339 Rvalue::CheckedBinaryOp(bin_op, lhs, rhs) => {
340 let ty = fx.monomorphize(&lhs.ty(&fx.mir.local_decls, fx.tcx));
341 let lhs = trans_operand(fx, lhs);
342 let rhs = trans_operand(fx, rhs);
344 let res = match ty.sty {
346 trans_checked_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, false)
349 trans_checked_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, true)
351 _ => unimplemented!("checked binop {:?} for {:?}", bin_op, ty),
353 lval.write_cvalue(fx, res);
355 Rvalue::UnaryOp(un_op, operand) => {
356 let ty = fx.monomorphize(&operand.ty(&fx.mir.local_decls, fx.tcx));
357 let layout = fx.layout_of(ty);
358 let val = trans_operand(fx, operand).load_value(fx);
359 let res = match un_op {
360 UnOp::Not => fx.bcx.ins().bnot(val),
361 UnOp::Neg => match ty.sty {
363 let clif_ty = fx.cton_type(ty).unwrap();
364 let zero = fx.bcx.ins().iconst(clif_ty, 0);
365 fx.bcx.ins().isub(zero, val)
367 ty::Float(_) => fx.bcx.ins().fneg(val),
368 _ => unimplemented!("un op Neg for {:?}", ty),
371 lval.write_cvalue(fx, CValue::ByVal(res, layout));
373 Rvalue::Cast(CastKind::ReifyFnPointer, operand, ty) => {
374 let operand = trans_operand(fx, operand);
375 let layout = fx.layout_of(ty);
376 lval.write_cvalue(fx, operand.unchecked_cast_to(layout));
378 Rvalue::Cast(CastKind::UnsafeFnPointer, operand, ty) => {
379 let operand = trans_operand(fx, operand);
380 let layout = fx.layout_of(ty);
381 lval.write_cvalue(fx, operand.unchecked_cast_to(layout));
383 Rvalue::Cast(CastKind::Misc, operand, to_ty) => {
384 let operand = trans_operand(fx, operand);
385 let from_ty = operand.layout().ty;
386 match (&from_ty.sty, &to_ty.sty) {
387 (ty::Ref(..), ty::Ref(..))
388 | (ty::Ref(..), ty::RawPtr(..))
389 | (ty::RawPtr(..), ty::Ref(..))
390 | (ty::RawPtr(..), ty::RawPtr(..)) => {
391 lval.write_cvalue(fx, operand.unchecked_cast_to(dest_layout));
393 (ty::RawPtr(..), ty::Uint(_)) | (ty::FnPtr(..), ty::Uint(_))
394 if to_ty.sty == fx.tcx.types.usize.sty =>
396 lval.write_cvalue(fx, operand.unchecked_cast_to(dest_layout));
398 (ty::Uint(_), ty::RawPtr(..)) if from_ty.sty == fx.tcx.types.usize.sty => {
399 lval.write_cvalue(fx, operand.unchecked_cast_to(dest_layout));
401 (ty::Char, ty::Uint(_))
402 | (ty::Uint(_), ty::Char)
403 | (ty::Uint(_), ty::Int(_))
404 | (ty::Uint(_), ty::Uint(_)) => {
405 let from = operand.load_value(fx);
406 let res = crate::common::cton_intcast(
409 fx.cton_type(to_ty).unwrap(),
412 lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
414 (ty::Int(_), ty::Int(_)) | (ty::Int(_), ty::Uint(_)) => {
415 let from = operand.load_value(fx);
416 let res = crate::common::cton_intcast(
419 fx.cton_type(to_ty).unwrap(),
422 lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
424 (ty::Float(from_flt), ty::Float(to_flt)) => {
425 let from = operand.load_value(fx);
426 let res = match (from_flt, to_flt) {
427 (FloatTy::F32, FloatTy::F64) => {
428 fx.bcx.ins().fpromote(types::F64, from)
430 (FloatTy::F64, FloatTy::F32) => {
431 fx.bcx.ins().fdemote(types::F32, from)
435 lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
437 (ty::Int(_), ty::Float(_)) => {
438 let from = operand.load_value(fx);
439 let f_type = fx.cton_type(to_ty).unwrap();
440 let res = fx.bcx.ins().fcvt_from_sint(f_type, from);
441 lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
443 (ty::Uint(_), ty::Float(_)) => {
444 let from = operand.load_value(fx);
445 let f_type = fx.cton_type(to_ty).unwrap();
446 let res = fx.bcx.ins().fcvt_from_uint(f_type, from);
447 lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
449 (ty::Bool, ty::Uint(_)) | (ty::Bool, ty::Int(_)) => {
450 let to_ty = fx.cton_type(to_ty).unwrap();
451 let from = operand.load_value(fx);
452 let res = if to_ty != types::I8 {
453 fx.bcx.ins().uextend(to_ty, from)
457 lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
459 _ => unimpl!("rval misc {:?} {:?}", from_ty, to_ty),
462 Rvalue::Cast(CastKind::ClosureFnPointer, operand, ty) => {
463 unimplemented!("rval closure_fn_ptr {:?} {:?}", operand, ty)
465 Rvalue::Cast(CastKind::Unsize, operand, ty) => {
466 let operand = trans_operand(fx, operand);
467 operand.unsize_value(fx, lval);
469 Rvalue::Discriminant(place) => {
470 let place = trans_place(fx, place).to_cvalue(fx);
471 let discr = trans_get_discriminant(fx, place, dest_layout);
472 lval.write_cvalue(fx, discr);
474 Rvalue::Repeat(operand, times) => {
475 let operand = trans_operand(fx, operand);
477 let index = fx.bcx.ins().iconst(fx.module.pointer_type(), i as i64);
478 let to = lval.place_index(fx, index);
479 to.write_cvalue(fx, operand);
482 Rvalue::Len(place) => {
483 let place = trans_place(fx, place);
484 let size = match place {
485 CPlace::Addr(_, size, _) => size.unwrap(),
486 CPlace::Var(_, _) => unreachable!(),
488 let usize_layout = fx.layout_of(fx.tcx.types.usize);
489 lval.write_cvalue(fx, CValue::ByVal(size, usize_layout));
491 Rvalue::NullaryOp(NullOp::Box, ty) => unimplemented!("rval box {:?}", ty),
492 Rvalue::NullaryOp(NullOp::SizeOf, ty) => {
496 .is_sized(fx.tcx.at(DUMMY_SP), ParamEnv::reveal_all())
498 let ty_size = fx.layout_of(ty).size.bytes();
499 let val = CValue::const_val(fx, fx.tcx.types.usize, ty_size as i64);
500 lval.write_cvalue(fx, val);
502 Rvalue::Aggregate(kind, operands) => match **kind {
503 AggregateKind::Array(_ty) => {
504 for (i, operand) in operands.into_iter().enumerate() {
505 let operand = trans_operand(fx, operand);
506 let index = fx.bcx.ins().iconst(fx.module.pointer_type(), i as i64);
507 let to = lval.place_index(fx, index);
508 to.write_cvalue(fx, operand);
511 _ => unimpl!("shouldn't exist at trans {:?}", rval),
515 StatementKind::StorageLive(_)
516 | StatementKind::StorageDead(_)
518 | StatementKind::ReadForMatch(_)
519 | StatementKind::Validate(_, _)
520 | StatementKind::EndRegion(_)
521 | StatementKind::UserAssertTy(_, _) => {}
523 StatementKind::InlineAsm { .. } => unimpl!("Inline assembly is not supported"),
527 pub fn trans_get_discriminant<'a, 'tcx: 'a>(
528 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
530 dest_layout: TyLayout<'tcx>,
532 let layout = value.layout();
534 if layout.abi == layout::Abi::Uninhabited {
535 fx.bcx.ins().trap(TrapCode::User(!0));
537 match layout.variants {
538 layout::Variants::Single { index } => {
539 let discr_val = layout.ty.ty_adt_def().map_or(index as u128, |def| {
540 def.discriminant_for_variant(fx.tcx, index).val
542 return CValue::const_val(fx, dest_layout.ty, discr_val as u64 as i64);
544 layout::Variants::Tagged { .. } | layout::Variants::NicheFilling { .. } => {}
547 let discr = value.value_field(fx, mir::Field::new(0));
548 let discr_ty = discr.layout().ty;
549 let lldiscr = discr.load_value(fx);
550 match layout.variants {
551 layout::Variants::Single { .. } => bug!(),
552 layout::Variants::Tagged { ref tag, .. } => {
553 let signed = match tag.value {
554 layout::Int(_, signed) => signed,
557 let val = cton_intcast(fx, lldiscr, fx.cton_type(dest_layout.ty).unwrap(), signed);
558 return CValue::ByVal(val, dest_layout);
560 layout::Variants::NicheFilling {
566 let niche_llty = fx.cton_type(discr_ty).unwrap();
567 let dest_cton_ty = fx.cton_type(dest_layout.ty).unwrap();
568 if niche_variants.start() == niche_variants.end() {
572 .icmp_imm(IntCC::Equal, lldiscr, niche_start as u64 as i64);
576 .iconst(dest_cton_ty, *niche_variants.start() as u64 as i64);
580 .iconst(dest_cton_ty, dataful_variant as u64 as i64);
581 let val = fx.bcx.ins().select(b, if_true, if_false);
582 return CValue::ByVal(val, dest_layout);
584 // Rebase from niche values to discriminant values.
585 let delta = niche_start.wrapping_sub(*niche_variants.start() as u128);
586 let delta = fx.bcx.ins().iconst(niche_llty, delta as u64 as i64);
587 let lldiscr = fx.bcx.ins().isub(lldiscr, delta);
588 let b = fx.bcx.ins().icmp_imm(
589 IntCC::UnsignedLessThanOrEqual,
591 *niche_variants.end() as u64 as i64,
594 cton_intcast(fx, lldiscr, fx.cton_type(dest_layout.ty).unwrap(), false);
598 .iconst(dest_cton_ty, dataful_variant as u64 as i64);
599 let val = fx.bcx.ins().select(b, if_true, if_false);
600 return CValue::ByVal(val, dest_layout);
606 macro_rules! binop_match {
607 (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, bug) => {
608 bug!("binop {} on {} lhs: {:?} rhs: {:?}", stringify!($var), $bug_fmt, $lhs, $rhs)
610 (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, icmp($cc:ident)) => {{
611 assert_eq!($fx.tcx.types.bool, $ret_ty);
612 let ret_layout = $fx.layout_of($ret_ty);
614 // TODO HACK no encoding for icmp.i8
615 use crate::common::cton_intcast;
617 cton_intcast($fx, $lhs, types::I64, $signed),
618 cton_intcast($fx, $rhs, types::I64, $signed),
620 let b = $fx.bcx.ins().icmp(IntCC::$cc, lhs, rhs);
622 CValue::ByVal($fx.bcx.ins().bint(types::I8, b), ret_layout)
624 (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, fcmp($cc:ident)) => {{
625 assert_eq!($fx.tcx.types.bool, $ret_ty);
626 let ret_layout = $fx.layout_of($ret_ty);
627 let b = $fx.bcx.ins().fcmp(FloatCC::$cc, $lhs, $rhs);
628 CValue::ByVal($fx.bcx.ins().bint(types::I8, b), ret_layout)
630 (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, custom(|| $body:expr)) => {{
633 (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, $name:ident) => {{
634 let ret_layout = $fx.layout_of($ret_ty);
635 CValue::ByVal($fx.bcx.ins().$name($lhs, $rhs), ret_layout)
638 $fx:expr, $bin_op:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, $bug_fmt:expr;
640 $var:ident ($sign:pat) $name:tt $( ( $($next:tt)* ) )? ;
643 let lhs = $lhs.load_value($fx);
644 let rhs = $rhs.load_value($fx);
645 match ($bin_op, $signed) {
647 (BinOp::$var, $sign) => binop_match!(@single $fx, $bug_fmt, $var, $signed, lhs, rhs, $ret_ty, $name $( ( $($next)* ) )?),
653 fn trans_bool_binop<'a, 'tcx: 'a>(
654 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
660 let res = binop_match! {
661 fx, bin_op, false, lhs, rhs, ty, "bool";
674 Lt (_) icmp(UnsignedLessThan);
675 Le (_) icmp(UnsignedLessThanOrEqual);
676 Ne (_) icmp(NotEqual);
677 Ge (_) icmp(UnsignedGreaterThanOrEqual);
678 Gt (_) icmp(UnsignedGreaterThan);
686 pub fn trans_int_binop<'a, 'tcx: 'a>(
687 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
694 if bin_op != BinOp::Shl && bin_op != BinOp::Shr {
698 "int binop requires lhs and rhs of same type"
702 fx, bin_op, signed, lhs, rhs, out_ty, "int/uint";
718 Lt (false) icmp(UnsignedLessThan);
719 Lt (true) icmp(SignedLessThan);
720 Le (false) icmp(UnsignedLessThanOrEqual);
721 Le (true) icmp(SignedLessThanOrEqual);
722 Ne (_) icmp(NotEqual);
723 Ge (false) icmp(UnsignedGreaterThanOrEqual);
724 Ge (true) icmp(SignedGreaterThanOrEqual);
725 Gt (false) icmp(UnsignedGreaterThan);
726 Gt (true) icmp(SignedGreaterThan);
732 pub fn trans_checked_int_binop<'a, 'tcx: 'a>(
733 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
735 in_lhs: CValue<'tcx>,
736 in_rhs: CValue<'tcx>,
740 if bin_op != BinOp::Shl && bin_op != BinOp::Shr {
744 "checked int binop requires lhs and rhs of same type"
747 let res_ty = match out_ty.sty {
748 ty::Tuple(tys) => tys[0],
750 "Checked int binop requires tuple as output, but got {:?}",
755 let lhs = in_lhs.load_value(fx);
756 let rhs = in_rhs.load_value(fx);
757 let res = match bin_op {
758 BinOp::Add => fx.bcx.ins().iadd(lhs, rhs),
759 BinOp::Sub => fx.bcx.ins().isub(lhs, rhs),
760 BinOp::Mul => fx.bcx.ins().imul(lhs, rhs),
761 BinOp::Shl => fx.bcx.ins().ishl(lhs, rhs),
762 BinOp::Shr => if !signed {
763 fx.bcx.ins().ushr(lhs, rhs)
765 fx.bcx.ins().sshr(lhs, rhs)
768 "binop {:?} on checked int/uint lhs: {:?} rhs: {:?}",
775 // TODO: check for overflow
776 let has_overflow = fx.bcx.ins().iconst(types::I8, 0);
778 let out_place = CPlace::temp(fx, out_ty);
779 let out_layout = out_place.layout();
780 out_place.write_cvalue(fx, CValue::ByValPair(res, has_overflow, out_layout));
782 out_place.to_cvalue(fx)
785 fn trans_float_binop<'a, 'tcx: 'a>(
786 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
792 let res = binop_match! {
793 fx, bin_op, false, lhs, rhs, ty, "float";
799 assert_eq!(lhs.layout().ty, ty);
800 assert_eq!(rhs.layout().ty, ty);
802 ty::Float(FloatTy::F32) => fx.easy_call("fmodf", &[lhs, rhs], ty),
803 ty::Float(FloatTy::F64) => fx.easy_call("fmod", &[lhs, rhs], ty),
814 Lt (_) fcmp(LessThan);
815 Le (_) fcmp(LessThanOrEqual);
816 Ne (_) fcmp(NotEqual);
817 Ge (_) fcmp(GreaterThanOrEqual);
818 Gt (_) fcmp(GreaterThan);
826 fn trans_char_binop<'a, 'tcx: 'a>(
827 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
833 let res = binop_match! {
834 fx, bin_op, false, lhs, rhs, ty, "char";
847 Lt (_) icmp(UnsignedLessThan);
848 Le (_) icmp(UnsignedLessThanOrEqual);
849 Ne (_) icmp(NotEqual);
850 Ge (_) icmp(UnsignedGreaterThanOrEqual);
851 Gt (_) icmp(UnsignedGreaterThan);
859 fn trans_ptr_binop<'a, 'tcx: 'a>(
860 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
866 match lhs.layout().ty.sty {
867 ty::RawPtr(TypeAndMut { ty, mutbl: _ }) => {
868 if !ty.is_sized(fx.tcx.at(DUMMY_SP), ParamEnv::reveal_all()) {
869 unimpl!("Unsized values are not yet implemented");
872 _ => bug!("trans_ptr_binop on non ptr"),
875 fx, bin_op, false, lhs, rhs, ty, "ptr";
888 Lt (_) icmp(UnsignedLessThan);
889 Le (_) icmp(UnsignedLessThanOrEqual);
890 Ne (_) icmp(NotEqual);
891 Ge (_) icmp(UnsignedGreaterThanOrEqual);
892 Gt (_) icmp(UnsignedGreaterThan);
898 pub fn trans_place<'a, 'tcx: 'a>(
899 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
903 Place::Local(local) => fx.get_local_place(*local),
904 Place::Promoted(promoted) => crate::constant::trans_promoted(fx, promoted.0),
905 Place::Static(static_) => crate::constant::codegen_static_ref(fx, static_),
906 Place::Projection(projection) => {
907 let base = trans_place(fx, &projection.base);
908 match projection.elem {
909 ProjectionElem::Deref => base.place_deref(fx),
910 ProjectionElem::Field(field, _ty) => base.place_field(fx, field),
911 ProjectionElem::Index(local) => {
912 let index = fx.get_local_place(local).to_cvalue(fx).load_value(fx);
913 base.place_index(fx, index)
915 ProjectionElem::ConstantIndex {
920 "projection const index {:?} offset {:?} not from end",
924 ProjectionElem::ConstantIndex {
929 "projection const index {:?} offset {:?} from end",
933 ProjectionElem::Subslice { from, to } => unimplemented!(
934 "projection subslice {:?} from {} to {}",
939 ProjectionElem::Downcast(_adt_def, variant) => base.downcast_variant(fx, variant),
945 pub fn trans_operand<'a, 'tcx>(
946 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
947 operand: &Operand<'tcx>,
950 Operand::Move(place) | Operand::Copy(place) => {
951 let cplace = trans_place(fx, place);
954 Operand::Constant(const_) => crate::constant::trans_constant(fx, const_),