Skip to content

Commit

Permalink
Fix volatile_load
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed Oct 24, 2023
1 parent 16b3da8 commit 085e827
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,9 +751,8 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
loaded_value.to_rvalue()
}

fn volatile_load(&mut self, _ty: Type<'gcc>, ptr: RValue<'gcc>) -> RValue<'gcc> {
// TODO(antoyo): use ty.
let ptr = self.context.new_cast(None, ptr, ptr.get_type().make_volatile());
fn volatile_load(&mut self, ty: Type<'gcc>, ptr: RValue<'gcc>) -> RValue<'gcc> {
let ptr = self.context.new_cast(None, ptr, ty.make_volatile().make_pointer());
ptr.dereference(None).to_rvalue()
}

Expand Down
14 changes: 9 additions & 5 deletions src/intrinsic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,15 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {

sym::volatile_load | sym::unaligned_volatile_load => {
let tp_ty = fn_args.type_at(0);
let mut ptr = args[0].immediate();
if let PassMode::Cast { cast: ty, .. } = &fn_abi.ret.mode {
ptr = self.pointercast(ptr, self.type_ptr_to(ty.gcc_type(self)));
}
let load = self.volatile_load(ptr.get_type(), ptr);
let ptr = args[0].immediate();
let load =
if let PassMode::Cast { cast: ty, pad_i32: _ } = &fn_abi.ret.mode {
let gcc_ty = ty.gcc_type(self);
self.volatile_load(gcc_ty, ptr)
}
else {
self.volatile_load(self.layout_of(tp_ty).gcc_type(self), ptr)
};
// TODO(antoyo): set alignment.
self.to_immediate(load, self.layout_of(tp_ty))
}
Expand Down
26 changes: 26 additions & 0 deletions tests/run/volatile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Compiler:
//
// Run-time:
// status: 0

use std::mem::MaybeUninit;

#[derive(Debug)]
struct Struct {
pointer: *const (),
func: unsafe fn(*const ()),
}

fn func(ptr: *const ()) {
}

fn main() {
let mut x = MaybeUninit::<&Struct>::uninit();
x.write(&Struct {
pointer: std::ptr::null(),
func,
});
let x = unsafe { x.assume_init() };
let value = unsafe { (x as *const Struct).read_volatile() };
println!("{:?}", value);
}

0 comments on commit 085e827

Please sign in to comment.