Auto merge of #2624 - WaffleLapkin:iforgor💀, r=RalfJung

Implement `ptr_mask` intrinsic

I promised I'll implement it, but then forgot 😅
This commit is contained in:
bors 2022-10-27 22:48:45 +00:00
commit bd4a56bce8
2 changed files with 30 additions and 1 deletions

View File

@ -11,7 +11,7 @@
mir,
ty::{self, FloatTy, Ty},
};
use rustc_target::abi::Integer;
use rustc_target::abi::{Integer, Size};
use crate::*;
use atomic::EvalContextExt as _;
@ -120,6 +120,17 @@ fn emulate_intrinsic_by_name(
this.write_bytes_ptr(ptr, iter::repeat(val_byte).take(byte_count.bytes_usize()))?;
}
"ptr_mask" => {
let [ptr, mask] = check_arg_count(args)?;
let ptr = this.read_pointer(ptr)?;
let mask = this.read_scalar(mask)?.to_machine_usize(this)?;
let masked_addr = Size::from_bytes(ptr.addr().bytes() & mask);
this.write_pointer(Pointer::new(ptr.provenance, masked_addr), dest)?;
}
// Floating-point operations
"fabsf32" => {
let [f] = check_arg_count(args)?;

View File

@ -0,0 +1,18 @@
#![feature(ptr_mask)]
#![feature(strict_provenance)]
fn main() {
let v: u32 = 0xABCDABCD;
let ptr: *const u32 = &v;
// u32 is 4 aligned,
// so the lower `log2(4) = 2` bits of the address are always 0
assert_eq!(ptr.addr() & 0b11, 0);
let tagged_ptr = ptr.map_addr(|a| a | 0b11);
let tag = tagged_ptr.addr() & 0b11;
let masked_ptr = tagged_ptr.mask(!0b11);
assert_eq!(tag, 0b11);
assert_eq!(unsafe { *masked_ptr }, 0xABCDABCD);
}