3 struct PrintOnPanic(String);
4 impl Drop for PrintOnPanic {
6 if ::std::thread::panicking() {
7 println!("{}", self.0);
12 pub fn trans_mono_item<'a, 'tcx: 'a>(
13 cx: &mut CodegenCx<'a, 'tcx, impl Backend>,
14 mono_item: MonoItem<'tcx>,
17 let context = &mut cx.context;
20 MonoItem::Fn(inst) => {
21 let _print_guard = PrintOnPanic(format!("{:?}", inst));
22 let mir = match inst.def {
23 InstanceDef::Item(_) | InstanceDef::DropGlue(_, _) | InstanceDef::Virtual(_, _) => {
24 let mut mir = ::std::io::Cursor::new(Vec::new());
25 ::rustc_mir::util::write_mir_pretty(tcx, Some(inst.def_id()), &mut mir)
29 InstanceDef::FnPtrShim(_, _)
30 | InstanceDef::ClosureOnceShim { .. }
31 | InstanceDef::CloneShim(_, _) => {
32 // FIXME fix write_mir_pretty for these instances
33 format!("{:#?}", cx.tcx.instance_mir(inst.def)).into_bytes()
35 InstanceDef::Intrinsic(_) => bug!("tried to codegen intrinsic"),
38 "target/out/mir/".to_string() + &format!("{:?}", inst.def_id()).replace('/', "@");
39 ::std::fs::write(mir_file_name, mir).unwrap();
41 trans_fn(tcx, cx.module, &mut cx.ccx, context, inst);
43 MonoItem::Static(def_id) => {
44 crate::constant::codegen_static(&mut cx.ccx, def_id);
46 MonoItem::GlobalAsm(node_id) => cx
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,
57 context: &mut Context,
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(),
100 // Step 6. Codegen function
101 crate::abi::codegen_fn_prelude(&mut fx, start_ebb);
102 codegen_fn_content(&mut fx);
103 fx.bcx.seal_all_blocks();
106 // Step 7. Print function to terminal for debugging
107 let mut writer = crate::pretty_clif::CommentWriter(fx.comments);
108 let mut cton = String::new();
109 ::cranelift::codegen::write::decorate_function(&mut writer, &mut cton, &func, None).unwrap();
110 let clif_file_name = "target/out/clif/".to_string() + &tcx.symbol_name(instance).as_str();
111 ::std::fs::write(clif_file_name, cton.as_bytes()).unwrap();
113 // Step 8. Verify function
114 verify_func(tcx, writer, &func);
116 // Step 9. Define function
117 // TODO: cranelift doesn't yet support some of the things needed
118 if should_codegen(tcx.sess) {
120 module.define_function(func_id, context).unwrap();
125 fn verify_func(tcx: TyCtxt, writer: crate::pretty_clif::CommentWriter, func: &Function) {
126 let flags = settings::Flags::new(settings::builder());
127 match ::cranelift::codegen::verify_function(&func, &flags) {
130 tcx.sess.err(&format!("{:?}", err));
131 let pretty_error = ::cranelift::codegen::print_errors::pretty_verifier_error(
134 Some(Box::new(writer)),
138 .fatal(&format!("cretonne verify error:\n{}", pretty_error));
143 fn codegen_fn_content<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx, impl Backend>) {
144 for (bb, bb_data) in fx.mir.basic_blocks().iter_enumerated() {
145 let ebb = fx.get_ebb(bb);
146 fx.bcx.switch_to_block(ebb);
149 for stmt in &bb_data.statements {
150 trans_stmt(fx, ebb, stmt);
153 let mut terminator_head = "\n".to_string();
157 .fmt_head(&mut terminator_head)
159 let inst = fx.bcx.func.layout.last_inst(ebb).unwrap();
160 fx.add_comment(inst, terminator_head);
162 match &bb_data.terminator().kind {
163 TerminatorKind::Goto { target } => {
164 let ebb = fx.get_ebb(*target);
165 fx.bcx.ins().jump(ebb, &[]);
167 TerminatorKind::Return => {
168 crate::abi::codegen_return(fx);
170 TerminatorKind::Assert {
177 let cond = trans_operand(fx, cond).load_value(fx);
178 // TODO HACK brz/brnz for i8/i16 is not yet implemented
179 let cond = fx.bcx.ins().uextend(types::I32, cond);
180 let target = fx.get_ebb(*target);
182 fx.bcx.ins().brnz(cond, target, &[]);
184 fx.bcx.ins().brz(cond, target, &[]);
186 fx.bcx.ins().trap(TrapCode::User(!0));
189 TerminatorKind::SwitchInt {
195 // TODO: prevent panics on large and negative disciminants
196 if should_codegen(fx.tcx.sess) {
197 let discr = trans_operand(fx, discr).load_value(fx);
198 let mut jt_data = JumpTableData::new();
199 for (i, value) in values.iter().enumerate() {
200 let ebb = fx.get_ebb(targets[i]);
201 jt_data.set_entry(*value as usize, ebb);
203 let jump_table = fx.bcx.create_jump_table(jt_data);
204 fx.bcx.ins().br_table(discr, jump_table);
205 let otherwise_ebb = fx.get_ebb(targets[targets.len() - 1]);
206 fx.bcx.ins().jump(otherwise_ebb, &[]);
208 fx.bcx.ins().trap(TrapCode::User(0));
211 TerminatorKind::Call {
217 crate::abi::codegen_call(fx, func, args, destination);
219 TerminatorKind::Resume | TerminatorKind::Abort | TerminatorKind::Unreachable => {
220 fx.bcx.ins().trap(TrapCode::User(!0));
222 TerminatorKind::Yield { .. }
223 | TerminatorKind::FalseEdges { .. }
224 | TerminatorKind::FalseUnwind { .. } => {
225 bug!("shouldn't exist at trans {:?}", bb_data.terminator());
227 TerminatorKind::Drop { target, .. } | TerminatorKind::DropAndReplace { target, .. } => {
228 // TODO call drop impl
229 // unimplemented!("terminator {:?}", bb_data.terminator());
230 let target_ebb = fx.get_ebb(*target);
231 fx.bcx.ins().jump(target_ebb, &[]);
233 TerminatorKind::GeneratorDrop => {
234 unimplemented!("terminator GeneratorDrop");
239 fx.bcx.seal_all_blocks();
243 fn trans_stmt<'a, 'tcx: 'a>(
244 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
246 stmt: &Statement<'tcx>,
248 let _print_guard = PrintOnPanic(format!("stmt {:?}", stmt));
250 let inst = fx.bcx.func.layout.last_inst(cur_ebb).unwrap();
251 fx.add_comment(inst, format!("{:?}", stmt));
254 StatementKind::SetDiscriminant {
258 let place = trans_place(fx, place);
259 let layout = place.layout();
260 if layout.for_variant(&*fx, *variant_index).abi == layout::Abi::Uninhabited {
263 match layout.variants {
264 layout::Variants::Single { index } => {
265 assert_eq!(index, *variant_index);
267 layout::Variants::Tagged { .. } => {
268 let ptr = place.place_field(fx, mir::Field::new(0));
273 .discriminant_for_variant(fx.tcx, *variant_index)
275 let discr = CValue::const_val(fx, ptr.layout().ty, to as u64 as i64);
276 ptr.write_cvalue(fx, discr);
278 layout::Variants::NicheFilling {
284 if *variant_index != dataful_variant {
285 let niche = place.place_field(fx, mir::Field::new(0));
286 //let niche_llty = niche.layout.immediate_llvm_type(bx.cx);
287 let niche_value = ((variant_index - *niche_variants.start()) as u128)
288 .wrapping_add(niche_start);
289 // FIXME(eddyb) Check the actual primitive type here.
290 let niche_llval = if niche_value == 0 {
291 CValue::const_val(fx, niche.layout().ty, 0)
293 CValue::const_val(fx, niche.layout().ty, niche_value as u64 as i64)
295 niche.write_cvalue(fx, niche_llval);
300 StatementKind::Assign(to_place, rval) => {
301 let lval = trans_place(fx, to_place);
302 let dest_layout = lval.layout();
304 Rvalue::Use(operand) => {
305 let val = trans_operand(fx, operand);
306 lval.write_cvalue(fx, val);
308 Rvalue::Ref(_, _, place) => {
309 let place = trans_place(fx, place);
310 let addr = place.expect_addr();
311 lval.write_cvalue(fx, CValue::ByVal(addr, dest_layout));
313 Rvalue::BinaryOp(bin_op, lhs, rhs) => {
314 let ty = fx.monomorphize(&lhs.ty(&fx.mir.local_decls, fx.tcx));
315 let lhs = trans_operand(fx, lhs);
316 let rhs = trans_operand(fx, rhs);
318 let res = match ty.sty {
319 ty::Bool => trans_bool_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
321 trans_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, false)
324 trans_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, true)
326 ty::Float(_) => trans_float_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
327 ty::Char => trans_char_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
328 ty::RawPtr(..) => trans_ptr_binop(fx, *bin_op, lhs, rhs, lval.layout().ty),
329 _ => unimplemented!("binop {:?} for {:?}", bin_op, ty),
331 lval.write_cvalue(fx, res);
333 Rvalue::CheckedBinaryOp(bin_op, lhs, rhs) => {
334 let ty = fx.monomorphize(&lhs.ty(&fx.mir.local_decls, fx.tcx));
335 let lhs = trans_operand(fx, lhs);
336 let rhs = trans_operand(fx, rhs);
338 let res = match ty.sty {
340 trans_checked_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, false)
343 trans_checked_int_binop(fx, *bin_op, lhs, rhs, lval.layout().ty, true)
345 _ => unimplemented!("checked binop {:?} for {:?}", bin_op, ty),
347 lval.write_cvalue(fx, res);
349 Rvalue::UnaryOp(un_op, operand) => {
350 let ty = fx.monomorphize(&operand.ty(&fx.mir.local_decls, fx.tcx));
351 let layout = fx.layout_of(ty);
352 let val = trans_operand(fx, operand).load_value(fx);
353 let res = match un_op {
354 UnOp::Not => fx.bcx.ins().bnot(val),
355 UnOp::Neg => match ty.sty {
357 let clif_ty = fx.cton_type(ty).unwrap();
358 let zero = fx.bcx.ins().iconst(clif_ty, 0);
359 fx.bcx.ins().isub(zero, val)
361 ty::Float(_) => fx.bcx.ins().fneg(val),
362 _ => unimplemented!("un op Neg for {:?}", ty),
365 lval.write_cvalue(fx, CValue::ByVal(res, layout));
367 Rvalue::Cast(CastKind::ReifyFnPointer, operand, ty) => {
368 let operand = trans_operand(fx, operand);
369 let layout = fx.layout_of(ty);
370 lval.write_cvalue(fx, operand.unchecked_cast_to(layout));
372 Rvalue::Cast(CastKind::UnsafeFnPointer, operand, ty) => {
373 let operand = trans_operand(fx, operand);
374 let layout = fx.layout_of(ty);
375 lval.write_cvalue(fx, operand.unchecked_cast_to(layout));
377 Rvalue::Cast(CastKind::Misc, operand, to_ty) => {
378 let operand = trans_operand(fx, operand);
379 let from_ty = operand.layout().ty;
380 match (&from_ty.sty, &to_ty.sty) {
381 (ty::Ref(..), ty::Ref(..))
382 | (ty::Ref(..), ty::RawPtr(..))
383 | (ty::RawPtr(..), ty::Ref(..))
384 | (ty::RawPtr(..), ty::RawPtr(..)) => {
385 lval.write_cvalue(fx, operand.unchecked_cast_to(dest_layout));
387 (ty::RawPtr(..), ty::Uint(_)) | (ty::FnPtr(..), ty::Uint(_))
388 if to_ty.sty == fx.tcx.types.usize.sty =>
390 lval.write_cvalue(fx, operand.unchecked_cast_to(dest_layout));
392 (ty::Uint(_), ty::RawPtr(..)) if from_ty.sty == fx.tcx.types.usize.sty => {
393 lval.write_cvalue(fx, operand.unchecked_cast_to(dest_layout));
395 (ty::Char, ty::Uint(_))
396 | (ty::Uint(_), ty::Char)
397 | (ty::Uint(_), ty::Int(_))
398 | (ty::Uint(_), ty::Uint(_)) => {
399 let from = operand.load_value(fx);
400 let res = crate::common::cton_intcast(
403 fx.cton_type(to_ty).unwrap(),
406 lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
408 (ty::Int(_), ty::Int(_)) | (ty::Int(_), ty::Uint(_)) => {
409 let from = operand.load_value(fx);
410 let res = crate::common::cton_intcast(
413 fx.cton_type(to_ty).unwrap(),
416 lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
418 (ty::Float(from_flt), ty::Float(to_flt)) => {
419 let from = operand.load_value(fx);
420 let res = match (from_flt, to_flt) {
421 (FloatTy::F32, FloatTy::F64) => {
422 fx.bcx.ins().fpromote(types::F64, from)
424 (FloatTy::F64, FloatTy::F32) => {
425 fx.bcx.ins().fdemote(types::F32, from)
429 lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
431 (ty::Int(_), ty::Float(_)) => {
432 let from = operand.load_value(fx);
433 let f_type = fx.cton_type(to_ty).unwrap();
434 let res = fx.bcx.ins().fcvt_from_sint(f_type, from);
435 lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
437 (ty::Uint(_), 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_uint(f_type, from);
441 lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
443 (ty::Bool, ty::Uint(_)) | (ty::Bool, ty::Int(_)) => {
444 let to_ty = fx.cton_type(to_ty).unwrap();
445 let from = operand.load_value(fx);
446 let res = if to_ty != types::I8 {
447 fx.bcx.ins().uextend(to_ty, from)
451 lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
453 _ => unimpl!("rval misc {:?} {:?}", from_ty, to_ty),
456 Rvalue::Cast(CastKind::ClosureFnPointer, operand, ty) => {
457 unimplemented!("rval closure_fn_ptr {:?} {:?}", operand, ty)
459 Rvalue::Cast(CastKind::Unsize, operand, ty) => {
460 unimpl!("rval unsize {:?} {:?}", operand, ty);
462 Rvalue::Discriminant(place) => {
463 let place = trans_place(fx, place).to_cvalue(fx);
464 let discr = trans_get_discriminant(fx, place, dest_layout);
465 lval.write_cvalue(fx, discr);
467 Rvalue::Repeat(operand, times) => {
468 let operand = trans_operand(fx, operand);
470 let index = fx.bcx.ins().iconst(fx.module.pointer_type(), i as i64);
471 let to = lval.place_index(fx, index);
472 to.write_cvalue(fx, operand);
475 Rvalue::Len(lval) => unimpl!("rval len {:?}", lval),
476 Rvalue::NullaryOp(NullOp::Box, ty) => unimplemented!("rval box {:?}", ty),
477 Rvalue::NullaryOp(NullOp::SizeOf, ty) => {
481 .is_sized(fx.tcx.at(DUMMY_SP), ParamEnv::reveal_all())
483 let ty_size = fx.layout_of(ty).size.bytes();
484 let val = CValue::const_val(fx, fx.tcx.types.usize, ty_size as i64);
485 lval.write_cvalue(fx, val);
487 Rvalue::Aggregate(kind, operands) => match **kind {
488 AggregateKind::Array(_ty) => {
489 for (i, operand) in operands.into_iter().enumerate() {
490 let operand = trans_operand(fx, operand);
491 let index = fx.bcx.ins().iconst(fx.module.pointer_type(), i as i64);
492 let to = lval.place_index(fx, index);
493 to.write_cvalue(fx, operand);
496 _ => unimpl!("shouldn't exist at trans {:?}", rval),
500 StatementKind::StorageLive(_)
501 | StatementKind::StorageDead(_)
503 | StatementKind::ReadForMatch(_)
504 | StatementKind::Validate(_, _)
505 | StatementKind::EndRegion(_)
506 | StatementKind::UserAssertTy(_, _) => {}
508 StatementKind::InlineAsm { .. } => unimpl!("Inline assembly is not supported"),
512 pub fn trans_get_discriminant<'a, 'tcx: 'a>(
513 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
515 dest_layout: TyLayout<'tcx>,
517 let layout = value.layout();
519 if layout.abi == layout::Abi::Uninhabited {
520 fx.bcx.ins().trap(TrapCode::User(!0));
522 match layout.variants {
523 layout::Variants::Single { index } => {
524 let discr_val = layout.ty.ty_adt_def().map_or(index as u128, |def| {
525 def.discriminant_for_variant(fx.tcx, index).val
527 return CValue::const_val(fx, dest_layout.ty, discr_val as u64 as i64);
529 layout::Variants::Tagged { .. } | layout::Variants::NicheFilling { .. } => {}
532 let discr = value.value_field(fx, mir::Field::new(0));
533 let discr_ty = discr.layout().ty;
534 let lldiscr = discr.load_value(fx);
535 match layout.variants {
536 layout::Variants::Single { .. } => bug!(),
537 layout::Variants::Tagged { ref tag, .. } => {
538 let signed = match tag.value {
539 layout::Int(_, signed) => signed,
542 let val = cton_intcast(fx, lldiscr, fx.cton_type(dest_layout.ty).unwrap(), signed);
543 return CValue::ByVal(val, dest_layout);
545 layout::Variants::NicheFilling {
551 let niche_llty = fx.cton_type(discr_ty).unwrap();
552 let dest_cton_ty = fx.cton_type(dest_layout.ty).unwrap();
553 if niche_variants.start() == niche_variants.end() {
557 .icmp_imm(IntCC::Equal, lldiscr, niche_start as u64 as i64);
561 .iconst(dest_cton_ty, *niche_variants.start() as u64 as i64);
565 .iconst(dest_cton_ty, dataful_variant as u64 as i64);
566 let val = fx.bcx.ins().select(b, if_true, if_false);
567 return CValue::ByVal(val, dest_layout);
569 // Rebase from niche values to discriminant values.
570 let delta = niche_start.wrapping_sub(*niche_variants.start() as u128);
571 let delta = fx.bcx.ins().iconst(niche_llty, delta as u64 as i64);
572 let lldiscr = fx.bcx.ins().isub(lldiscr, delta);
573 let b = fx.bcx.ins().icmp_imm(
574 IntCC::UnsignedLessThanOrEqual,
576 *niche_variants.end() as u64 as i64,
579 cton_intcast(fx, lldiscr, fx.cton_type(dest_layout.ty).unwrap(), false);
583 .iconst(dest_cton_ty, dataful_variant as u64 as i64);
584 let val = fx.bcx.ins().select(b, if_true, if_false);
585 return CValue::ByVal(val, dest_layout);
591 macro_rules! binop_match {
592 (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, bug) => {
593 bug!("binop {} on {} lhs: {:?} rhs: {:?}", stringify!($var), $bug_fmt, $lhs, $rhs)
595 (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, icmp($cc:ident)) => {{
596 assert_eq!($fx.tcx.types.bool, $ret_ty);
597 let ret_layout = $fx.layout_of($ret_ty);
599 // TODO HACK no encoding for icmp.i8
600 use crate::common::cton_intcast;
602 cton_intcast($fx, $lhs, types::I64, $signed),
603 cton_intcast($fx, $rhs, types::I64, $signed),
605 let b = $fx.bcx.ins().icmp(IntCC::$cc, lhs, rhs);
607 CValue::ByVal($fx.bcx.ins().bint(types::I8, b), ret_layout)
609 (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, fcmp($cc:ident)) => {{
610 assert_eq!($fx.tcx.types.bool, $ret_ty);
611 let ret_layout = $fx.layout_of($ret_ty);
612 let b = $fx.bcx.ins().fcmp(FloatCC::$cc, $lhs, $rhs);
613 CValue::ByVal($fx.bcx.ins().bint(types::I8, b), ret_layout)
615 (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, custom(|| $body:expr)) => {{
618 (@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, $name:ident) => {{
619 let ret_layout = $fx.layout_of($ret_ty);
620 CValue::ByVal($fx.bcx.ins().$name($lhs, $rhs), ret_layout)
623 $fx:expr, $bin_op:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, $bug_fmt:expr;
625 $var:ident ($sign:pat) $name:tt $( ( $($next:tt)* ) )? ;
628 let lhs = $lhs.load_value($fx);
629 let rhs = $rhs.load_value($fx);
630 match ($bin_op, $signed) {
632 (BinOp::$var, $sign) => binop_match!(@single $fx, $bug_fmt, $var, $signed, lhs, rhs, $ret_ty, $name $( ( $($next)* ) )?),
638 fn trans_bool_binop<'a, 'tcx: 'a>(
639 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
645 let res = binop_match! {
646 fx, bin_op, false, lhs, rhs, ty, "bool";
659 Lt (_) icmp(UnsignedLessThan);
660 Le (_) icmp(UnsignedLessThanOrEqual);
661 Ne (_) icmp(NotEqual);
662 Ge (_) icmp(UnsignedGreaterThanOrEqual);
663 Gt (_) icmp(UnsignedGreaterThan);
671 pub fn trans_int_binop<'a, 'tcx: 'a>(
672 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
679 if bin_op != BinOp::Shl && bin_op != BinOp::Shr {
683 "int binop requires lhs and rhs of same type"
687 fx, bin_op, signed, lhs, rhs, out_ty, "int/uint";
703 Lt (false) icmp(UnsignedLessThan);
704 Lt (true) icmp(SignedLessThan);
705 Le (false) icmp(UnsignedLessThanOrEqual);
706 Le (true) icmp(SignedLessThanOrEqual);
707 Ne (_) icmp(NotEqual);
708 Ge (false) icmp(UnsignedGreaterThanOrEqual);
709 Ge (true) icmp(SignedGreaterThanOrEqual);
710 Gt (false) icmp(UnsignedGreaterThan);
711 Gt (true) icmp(SignedGreaterThan);
717 pub fn trans_checked_int_binop<'a, 'tcx: 'a>(
718 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
720 in_lhs: CValue<'tcx>,
721 in_rhs: CValue<'tcx>,
725 if bin_op != BinOp::Shl && bin_op != BinOp::Shr {
729 "checked int binop requires lhs and rhs of same type"
732 let res_ty = match out_ty.sty {
733 ty::Tuple(tys) => tys[0],
735 "Checked int binop requires tuple as output, but got {:?}",
740 let lhs = in_lhs.load_value(fx);
741 let rhs = in_rhs.load_value(fx);
742 let res = match bin_op {
743 BinOp::Add => fx.bcx.ins().iadd(lhs, rhs),
744 BinOp::Sub => fx.bcx.ins().isub(lhs, rhs),
745 BinOp::Mul => fx.bcx.ins().imul(lhs, rhs),
746 BinOp::Shl => fx.bcx.ins().ishl(lhs, rhs),
747 BinOp::Shr => if !signed {
748 fx.bcx.ins().ushr(lhs, rhs)
750 fx.bcx.ins().sshr(lhs, rhs)
753 "binop {:?} on checked int/uint lhs: {:?} rhs: {:?}",
760 // TODO: check for overflow
761 let has_overflow = fx.bcx.ins().iconst(types::I8, 0);
763 let out_place = CPlace::temp(fx, out_ty);
764 let out_layout = out_place.layout();
765 out_place.write_cvalue(fx, CValue::ByValPair(res, has_overflow, out_layout));
767 out_place.to_cvalue(fx)
770 fn trans_float_binop<'a, 'tcx: 'a>(
771 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
777 let res = binop_match! {
778 fx, bin_op, false, lhs, rhs, ty, "float";
784 assert_eq!(lhs.layout().ty, ty);
785 assert_eq!(rhs.layout().ty, ty);
787 ty::Float(FloatTy::F32) => fx.easy_call("fmodf", &[lhs, rhs], ty),
788 ty::Float(FloatTy::F64) => fx.easy_call("fmod", &[lhs, rhs], ty),
799 Lt (_) fcmp(LessThan);
800 Le (_) fcmp(LessThanOrEqual);
801 Ne (_) fcmp(NotEqual);
802 Ge (_) fcmp(GreaterThanOrEqual);
803 Gt (_) fcmp(GreaterThan);
811 fn trans_char_binop<'a, 'tcx: 'a>(
812 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
818 let res = binop_match! {
819 fx, bin_op, false, lhs, rhs, ty, "char";
832 Lt (_) icmp(UnsignedLessThan);
833 Le (_) icmp(UnsignedLessThanOrEqual);
834 Ne (_) icmp(NotEqual);
835 Ge (_) icmp(UnsignedGreaterThanOrEqual);
836 Gt (_) icmp(UnsignedGreaterThan);
844 fn trans_ptr_binop<'a, 'tcx: 'a>(
845 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
851 match lhs.layout().ty.sty {
852 ty::RawPtr(TypeAndMut { ty, mutbl: _ }) => {
853 if !ty.is_sized(fx.tcx.at(DUMMY_SP), ParamEnv::reveal_all()) {
854 unimpl!("Unsized values are not yet implemented");
857 _ => bug!("trans_ptr_binop on non ptr"),
860 fx, bin_op, false, lhs, rhs, ty, "ptr";
873 Lt (_) icmp(UnsignedLessThan);
874 Le (_) icmp(UnsignedLessThanOrEqual);
875 Ne (_) icmp(NotEqual);
876 Ge (_) icmp(UnsignedGreaterThanOrEqual);
877 Gt (_) icmp(UnsignedGreaterThan);
883 pub fn trans_place<'a, 'tcx: 'a>(
884 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
888 Place::Local(local) => fx.get_local_place(*local),
889 Place::Promoted(promoted) => crate::constant::trans_promoted(fx, promoted.0),
890 Place::Static(static_) => crate::constant::codegen_static_ref(fx, static_),
891 Place::Projection(projection) => {
892 let base = trans_place(fx, &projection.base);
893 match projection.elem {
894 ProjectionElem::Deref => {
895 let layout = fx.layout_of(place.ty(&*fx.mir, fx.tcx).to_ty(fx.tcx));
896 if layout.is_unsized() {
897 unimpl!("Unsized places are not yet implemented");
899 CPlace::Addr(base.to_cvalue(fx).load_value(fx), layout)
901 ProjectionElem::Field(field, _ty) => base.place_field(fx, field),
902 ProjectionElem::Index(local) => {
903 let index = fx.get_local_place(local).to_cvalue(fx).load_value(fx);
904 base.place_index(fx, index)
906 ProjectionElem::ConstantIndex {
911 "projection const index {:?} offset {:?} not from end",
915 ProjectionElem::ConstantIndex {
920 "projection const index {:?} offset {:?} from end",
924 ProjectionElem::Subslice { from, to } => unimplemented!(
925 "projection subslice {:?} from {} to {}",
930 ProjectionElem::Downcast(_adt_def, variant) => base.downcast_variant(fx, variant),
936 pub fn trans_operand<'a, 'tcx>(
937 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
938 operand: &Operand<'tcx>,
941 Operand::Move(place) | Operand::Copy(place) => {
942 let cplace = trans_place(fx, place);
945 Operand::Constant(const_) => crate::constant::trans_constant(fx, const_),