Skip to content

Commit b272472

Browse files
committed
add volatile copy/copy_nonoverlapping/set
This exposes volatile versions of the memset/memmove/memcpy intrinsics. The volatile parameter must be constant, so this can't simply be a parameter to our intrinsics.
1 parent 09bfb92 commit b272472

File tree

3 files changed

+43
-32
lines changed

3 files changed

+43
-32
lines changed

src/librustc/middle/trans/intrinsic.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pub fn trans_intrinsic(ccx: &CrateContext,
129129
RetVoid(bcx);
130130
}
131131

132-
fn copy_intrinsic(bcx: &Block, allow_overlap: bool, tp_ty: ty::t) {
132+
fn copy_intrinsic(bcx: &Block, allow_overlap: bool, volatile: bool, tp_ty: ty::t) {
133133
let ccx = bcx.ccx();
134134
let lltp_ty = type_of::type_of(ccx, tp_ty);
135135
let align = C_i32(ccx, machine::llalign_of_min(ccx, lltp_ty) as i32);
@@ -154,13 +154,12 @@ pub fn trans_intrinsic(ccx: &CrateContext,
154154
let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), Type::i8p(ccx));
155155
let src_ptr = PointerCast(bcx, get_param(decl, first_real_arg + 1), Type::i8p(ccx));
156156
let count = get_param(decl, first_real_arg + 2);
157-
let volatile = C_i1(ccx, false);
158157
let llfn = ccx.get_intrinsic(&name);
159-
Call(bcx, llfn, [dst_ptr, src_ptr, Mul(bcx, size, count), align, volatile], []);
158+
Call(bcx, llfn, [dst_ptr, src_ptr, Mul(bcx, size, count), align, C_i1(ccx, volatile)], []);
160159
RetVoid(bcx);
161160
}
162161

163-
fn memset_intrinsic(bcx: &Block, tp_ty: ty::t) {
162+
fn memset_intrinsic(bcx: &Block, volatile: bool, tp_ty: ty::t) {
164163
let ccx = bcx.ccx();
165164
let lltp_ty = type_of::type_of(ccx, tp_ty);
166165
let align = C_i32(ccx, machine::llalign_of_min(ccx, lltp_ty) as i32);
@@ -176,9 +175,8 @@ pub fn trans_intrinsic(ccx: &CrateContext,
176175
let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), Type::i8p(ccx));
177176
let val = get_param(decl, first_real_arg + 1);
178177
let count = get_param(decl, first_real_arg + 2);
179-
let volatile = C_i1(ccx, false);
180178
let llfn = ccx.get_intrinsic(&name);
181-
Call(bcx, llfn, [dst_ptr, val, Mul(bcx, size, count), align, volatile], []);
179+
Call(bcx, llfn, [dst_ptr, val, Mul(bcx, size, count), align, C_i1(ccx, volatile)], []);
182180
RetVoid(bcx);
183181
}
184182

@@ -466,11 +464,15 @@ pub fn trans_intrinsic(ccx: &CrateContext,
466464
let lladdr = InBoundsGEP(bcx, ptr, [offset]);
467465
Ret(bcx, lladdr);
468466
}
469-
"copy_nonoverlapping_memory" => {
470-
copy_intrinsic(bcx, false, *substs.tys.get(0))
471-
}
472-
"copy_memory" => copy_intrinsic(bcx, true, *substs.tys.get(0)),
473-
"set_memory" => memset_intrinsic(bcx, *substs.tys.get(0)),
467+
"copy_nonoverlapping_memory" => copy_intrinsic(bcx, false, false, *substs.tys.get(0)),
468+
"copy_memory" => copy_intrinsic(bcx, true, false, *substs.tys.get(0)),
469+
"set_memory" => memset_intrinsic(bcx, false, *substs.tys.get(0)),
470+
471+
"volatile_copy_nonoverlapping_memory" =>
472+
copy_intrinsic(bcx, false, true, *substs.tys.get(0)),
473+
"volatile_copy_memory" => copy_intrinsic(bcx, true, true, *substs.tys.get(0)),
474+
"volatile_set_memory" => memset_intrinsic(bcx, true, *substs.tys.get(0)),
475+
474476
"ctlz8" => count_zeros_intrinsic(bcx, "llvm.ctlz.i8"),
475477
"ctlz16" => count_zeros_intrinsic(bcx, "llvm.ctlz.i16"),
476478
"ctlz32" => count_zeros_intrinsic(bcx, "llvm.ctlz.i32"),

src/librustc/middle/typeck/check/mod.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -4127,7 +4127,8 @@ pub fn check_intrinsic_type(ccx: &CrateCtxt, it: &ast::ForeignItem) {
41274127
mutbl: ast::MutImmutable
41284128
}))
41294129
}
4130-
"copy_nonoverlapping_memory" => {
4130+
"copy_memory" | "copy_nonoverlapping_memory" |
4131+
"volatile_copy_memory" | "volatile_copy_nonoverlapping_memory" => {
41314132
(1,
41324133
vec!(
41334134
ty::mk_ptr(tcx, ty::mt {
@@ -4142,22 +4143,7 @@ pub fn check_intrinsic_type(ccx: &CrateCtxt, it: &ast::ForeignItem) {
41424143
),
41434144
ty::mk_nil())
41444145
}
4145-
"copy_memory" => {
4146-
(1,
4147-
vec!(
4148-
ty::mk_ptr(tcx, ty::mt {
4149-
ty: param(ccx, 0),
4150-
mutbl: ast::MutMutable
4151-
}),
4152-
ty::mk_ptr(tcx, ty::mt {
4153-
ty: param(ccx, 0),
4154-
mutbl: ast::MutImmutable
4155-
}),
4156-
ty::mk_uint()
4157-
),
4158-
ty::mk_nil())
4159-
}
4160-
"set_memory" => {
4146+
"set_memory" | "volatile_set_memory" => {
41614147
(1,
41624148
vec!(
41634149
ty::mk_ptr(tcx, ty::mt {

src/libstd/intrinsics.rs

+27-4
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,6 @@ extern "rust-intrinsic" {
261261
/// Execute a breakpoint trap, for inspection by a debugger.
262262
pub fn breakpoint();
263263

264-
pub fn volatile_load<T>(src: *T) -> T;
265-
pub fn volatile_store<T>(dst: *mut T, val: T);
266-
267-
268264
/// The size of a type in bytes.
269265
///
270266
/// This is the exact number of bytes in memory taken up by a
@@ -338,6 +334,33 @@ extern "rust-intrinsic" {
338334
/// `min_align_of::<T>()`
339335
pub fn set_memory<T>(dst: *mut T, val: u8, count: uint);
340336

337+
/// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
338+
/// a size of `count` * `size_of::<T>()` and an alignment of
339+
/// `min_align_of::<T>()`
340+
///
341+
/// The volatile parameter parameter is set to `true`, so it will not be optimized out.
342+
#[cfg(not(stage0))]
343+
pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *T, count: uint);
344+
/// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
345+
/// a size of `count` * `size_of::<T>()` and an alignment of
346+
/// `min_align_of::<T>()`
347+
///
348+
/// The volatile parameter parameter is set to `true`, so it will not be optimized out.
349+
#[cfg(not(stage0))]
350+
pub fn volatile_copy_memory<T>(dst: *mut T, src: *T, count: uint);
351+
/// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
352+
/// size of `count` * `size_of::<T>()` and an alignment of
353+
/// `min_align_of::<T>()`.
354+
///
355+
/// The volatile parameter parameter is set to `true`, so it will not be optimized out.
356+
#[cfg(not(stage0))]
357+
pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: uint);
358+
359+
/// Perform a volatile load from the `src` pointer.
360+
pub fn volatile_load<T>(src: *T) -> T;
361+
/// Perform a volatile store to the `dst` pointer.
362+
pub fn volatile_store<T>(dst: *mut T, val: T);
363+
341364
pub fn sqrtf32(x: f32) -> f32;
342365
pub fn sqrtf64(x: f64) -> f64;
343366

0 commit comments

Comments
 (0)