Skip to content

Commit 676fec7

Browse files
committed
Add an intrinsic for ptr::metadata
1 parent 05b1415 commit 676fec7

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

src/base.rs

+25-13
Original file line numberDiff line numberDiff line change
@@ -619,22 +619,34 @@ fn codegen_stmt<'tcx>(
619619
Rvalue::UnaryOp(un_op, ref operand) => {
620620
let operand = codegen_operand(fx, operand);
621621
let layout = operand.layout();
622-
let val = operand.load_scalar(fx);
623622
let res = match un_op {
624-
UnOp::Not => match layout.ty.kind() {
625-
ty::Bool => {
626-
let res = fx.bcx.ins().icmp_imm(IntCC::Equal, val, 0);
627-
CValue::by_val(res, layout)
623+
UnOp::Not => {
624+
let val = operand.load_scalar(fx);
625+
match layout.ty.kind() {
626+
ty::Bool => {
627+
let res = fx.bcx.ins().icmp_imm(IntCC::Equal, val, 0);
628+
CValue::by_val(res, layout)
629+
}
630+
ty::Uint(_) | ty::Int(_) => {
631+
CValue::by_val(fx.bcx.ins().bnot(val), layout)
632+
}
633+
_ => unreachable!("un op Not for {:?}", layout.ty),
628634
}
629-
ty::Uint(_) | ty::Int(_) => {
630-
CValue::by_val(fx.bcx.ins().bnot(val), layout)
635+
}
636+
UnOp::Neg => {
637+
let val = operand.load_scalar(fx);
638+
match layout.ty.kind() {
639+
ty::Int(_) => CValue::by_val(fx.bcx.ins().ineg(val), layout),
640+
ty::Float(_) => CValue::by_val(fx.bcx.ins().fneg(val), layout),
641+
_ => unreachable!("un op Neg for {:?}", layout.ty),
631642
}
632-
_ => unreachable!("un op Not for {:?}", layout.ty),
633-
},
634-
UnOp::Neg => match layout.ty.kind() {
635-
ty::Int(_) => CValue::by_val(fx.bcx.ins().ineg(val), layout),
636-
ty::Float(_) => CValue::by_val(fx.bcx.ins().fneg(val), layout),
637-
_ => unreachable!("un op Neg for {:?}", layout.ty),
643+
}
644+
UnOp::PtrMetadata => match layout.abi {
645+
Abi::Scalar(_) => CValue::zst(dest_layout),
646+
Abi::ScalarPair(_, _) => {
647+
CValue::by_val(operand.load_scalar_pair(fx).1, dest_layout)
648+
}
649+
_ => bug!("Unexpected `PtrToMetadata` operand: {operand:?}"),
638650
},
639651
};
640652
lval.write_cvalue(fx, res);

src/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub(crate) fn codegen_const_value<'tcx>(
100100
assert!(layout.is_sized(), "unsized const value");
101101

102102
if layout.is_zst() {
103-
return CValue::by_ref(crate::Pointer::dangling(layout.align.pref), layout);
103+
return CValue::zst(layout);
104104
}
105105

106106
match const_val {

src/value_and_place.rs

+8
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ impl<'tcx> CValue<'tcx> {
9595
CValue(CValueInner::ByValPair(value, extra), layout)
9696
}
9797

98+
/// Create an instance of a ZST
99+
///
100+
/// The is represented by a dangling pointer of suitable alignment.
101+
pub(crate) fn zst(layout: TyAndLayout<'tcx>) -> CValue<'tcx> {
102+
assert!(layout.is_zst());
103+
CValue::by_ref(crate::Pointer::dangling(layout.align.pref), layout)
104+
}
105+
98106
pub(crate) fn layout(&self) -> TyAndLayout<'tcx> {
99107
self.1
100108
}

0 commit comments

Comments
 (0)