Merge pull request #148 from dwrensha/write-bytes-intrinsic
implement the write_bytes() intrinsic
This commit is contained in:
commit
5c82b64ce6
2 changed files with 57 additions and 0 deletions
|
@ -402,6 +402,18 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
"write_bytes" => {
|
||||
let u8 = self.tcx.types.u8;
|
||||
let ty = substs.type_at(0);
|
||||
let ty_align = self.type_align(ty)?;
|
||||
let val_byte = self.value_to_primval(arg_vals[1], u8)?.to_u128()? as u8;
|
||||
let size = self.type_size(ty)?.expect("write_bytes() type must be sized");
|
||||
let ptr = arg_vals[0].read_ptr(&self.memory)?;
|
||||
let count = self.value_to_primval(arg_vals[2], usize)?.to_u64()?;
|
||||
self.memory.check_align(ptr, size * count, ty_align)?;
|
||||
self.memory.write_repeat(ptr, val_byte, size * count)?;
|
||||
}
|
||||
|
||||
name => return Err(EvalError::Unimplemented(format!("unimplemented intrinsic: {}", name))),
|
||||
}
|
||||
|
||||
|
|
45
tests/run-pass/write-bytes.rs
Normal file
45
tests/run-pass/write-bytes.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
#[repr(C)]
|
||||
#[derive(Copy, Clone)]
|
||||
struct Foo {
|
||||
a: u64,
|
||||
b: u64,
|
||||
c: u64,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
const LENGTH: usize = 10;
|
||||
let mut v: [u64; LENGTH] = [0; LENGTH];
|
||||
|
||||
for idx in 0..LENGTH {
|
||||
assert_eq!(v[idx], 0);
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let p = v.as_mut_ptr();
|
||||
::std::ptr::write_bytes(p, 0xab, LENGTH);
|
||||
}
|
||||
|
||||
for idx in 0..LENGTH {
|
||||
assert_eq!(v[idx], 0xabababababababab);
|
||||
}
|
||||
|
||||
// -----
|
||||
|
||||
let mut w: [Foo; LENGTH] = [Foo { a: 0, b: 0, c: 0 }; LENGTH];
|
||||
for idx in 0..LENGTH {
|
||||
assert_eq!(w[idx].a, 0);
|
||||
assert_eq!(w[idx].b, 0);
|
||||
assert_eq!(w[idx].c, 0);
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let p = w.as_mut_ptr();
|
||||
::std::ptr::write_bytes(p, 0xcd, LENGTH);
|
||||
}
|
||||
|
||||
for idx in 0..LENGTH {
|
||||
assert_eq!(w[idx].a, 0xcdcdcdcdcdcdcdcd);
|
||||
assert_eq!(w[idx].b, 0xcdcdcdcdcdcdcdcd);
|
||||
assert_eq!(w[idx].c, 0xcdcdcdcdcdcdcdcd);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue