Skip to content

Commit

Permalink
Merge pull request #148 from dwrensha/write-bytes-intrinsic
Browse files Browse the repository at this point in the history
implement the write_bytes() intrinsic
  • Loading branch information
solson authored Mar 14, 2017
2 parents feeb13c + 41c2aa6 commit 5c82b64
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/terminator/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))),
}

Expand Down
45 changes: 45 additions & 0 deletions tests/run-pass/write-bytes.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 5c82b64

Please sign in to comment.