Skip to content

[MIR] Implement as casting (Misc cast kind) #30586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 84 additions & 1 deletion src/librustc_trans/trans/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use llvm::ValueRef;
use rustc::middle::ty::{self, Ty};
use middle::ty::cast::{CastTy, IntTy};
use rustc::mir::repr as mir;

use trans::asm;
Expand Down Expand Up @@ -198,7 +199,89 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
}
}
}
mir::CastKind::Misc => unimplemented!()
mir::CastKind::Misc if common::type_is_immediate(bcx.ccx(), operand.ty) => {
debug_assert!(common::type_is_immediate(bcx.ccx(), cast_ty));
let r_t_in = CastTy::from_ty(operand.ty).expect("bad input type for cast");
let r_t_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
let ll_t_in = type_of::arg_type_of(bcx.ccx(), operand.ty);
let ll_t_out = type_of::arg_type_of(bcx.ccx(), cast_ty);
let (llval, ll_t_in, signed) = if let CastTy::Int(IntTy::CEnum) = r_t_in {
let repr = adt::represent_type(bcx.ccx(), operand.ty);
let llval = operand.immediate();
let discr = adt::trans_get_discr(bcx, &*repr, llval, None);
(discr, common::val_ty(discr), adt::is_discr_signed(&*repr))
} else {
(operand.immediate(), ll_t_in, operand.ty.is_signed())
};

let newval = match (r_t_in, r_t_out) {
(CastTy::Int(_), CastTy::Int(_)) => {
let srcsz = ll_t_in.int_width();
let dstsz = ll_t_out.int_width();
if srcsz == dstsz {
build::BitCast(bcx, llval, ll_t_out)
} else if srcsz > dstsz {
build::Trunc(bcx, llval, ll_t_out)
} else if signed {
build::SExt(bcx, llval, ll_t_out)
} else {
build::ZExt(bcx, llval, ll_t_out)
}
}
(CastTy::Float, CastTy::Float) => {
let srcsz = ll_t_in.float_width();
let dstsz = ll_t_out.float_width();
if dstsz > srcsz {
build::FPExt(bcx, llval, ll_t_out)
} else if srcsz > dstsz {
build::FPTrunc(bcx, llval, ll_t_out)
} else {
llval
}
}
(CastTy::Ptr(_), CastTy::Ptr(_)) |
(CastTy::FnPtr, CastTy::Ptr(_)) |
(CastTy::RPtr(_), CastTy::Ptr(_)) =>
build::PointerCast(bcx, llval, ll_t_out),
(CastTy::Ptr(_), CastTy::Int(_)) |
(CastTy::FnPtr, CastTy::Int(_)) =>
build::PtrToInt(bcx, llval, ll_t_out),
(CastTy::Int(_), CastTy::Ptr(_)) =>
build::IntToPtr(bcx, llval, ll_t_out),
(CastTy::Int(_), CastTy::Float) if signed =>
build::SIToFP(bcx, llval, ll_t_out),
(CastTy::Int(_), CastTy::Float) =>
build::UIToFP(bcx, llval, ll_t_out),
(CastTy::Float, CastTy::Int(IntTy::I)) =>
build::FPToSI(bcx, llval, ll_t_out),
(CastTy::Float, CastTy::Int(_)) =>
build::FPToUI(bcx, llval, ll_t_out),
_ => bcx.ccx().sess().bug(
&format!("unsupported cast: {:?} to {:?}", operand.ty, cast_ty)
)
};
OperandValue::Immediate(newval)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if assuming everything is immediate here is correct, but I can’t think of a case where it could be something else.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be careful of fat pointers.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the cast cases written out above would handle or produce fat pointers. I’m more concerned with Ref vs Immediate. I.e. can Misc branch be reached with a OperandValue::Ref for castee.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The discriminant of an OperandValue is supposed to be a function of the operand's type.

}
mir::CastKind::Misc => { // Casts from a fat-ptr.
let ll_cast_ty = type_of::arg_type_of(bcx.ccx(), cast_ty);
let ll_from_ty = type_of::arg_type_of(bcx.ccx(), operand.ty);
if let OperandValue::FatPtr(data_ptr, meta_ptr) = operand.val {
if common::type_is_fat_ptr(bcx.tcx(), cast_ty) {
let ll_cft = ll_cast_ty.field_types();
let ll_fft = ll_from_ty.field_types();
let data_cast = build::PointerCast(bcx, data_ptr, ll_cft[0]);
assert_eq!(ll_cft[1].kind(), ll_fft[1].kind());
OperandValue::FatPtr(data_cast, meta_ptr)
} else { // cast to thin-ptr
// Cast of fat-ptr to thin-ptr is an extraction of data-ptr and
// pointer-cast of that pointer to desired pointer type.
let llval = build::PointerCast(bcx, data_ptr, ll_cast_ty);
OperandValue::Immediate(llval)
}
} else {
panic!("Unexpected non-FatPtr operand")
}
}
};
(bcx, OperandRef {
val: val,
Expand Down
Loading