Skip to content

Commit 8191b25

Browse files
authored
Translate pointer diff (#57)
1 parent 9a23dc6 commit 8191b25

14 files changed

Lines changed: 329 additions & 49 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2028,7 +2028,7 @@ bool Converter::VisitBinaryOperator(clang::BinaryOperator *expr) {
20282028
std::format("::std::mem::size_of::<{}>()", pointee_type_as_string);
20292029
StrCat(size_of_as_string);
20302030
}
2031-
StrCat(keyword::kAs, "u64");
2031+
ConvertCast(expr->getType());
20322032
computed_expr_type_ = ComputedExprType::FreshValue;
20332033
} else {
20342034
ConvertGenericBinaryOperator(expr);

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,8 +1247,12 @@ bool ConverterRefCount::VisitBinaryOperator(clang::BinaryOperator *expr) {
12471247
// fresh pointers
12481248
if (expr->isAdditiveOp() && lhs_type->isPointerType() &&
12491249
rhs_type->isPointerType()) {
1250-
StrCat(ConvertFreshPointer(lhs), expr->getOpcodeStr(),
1251-
ConvertFreshPointer(rhs));
1250+
{
1251+
PushParen paren(*this);
1252+
StrCat(ConvertFreshPointer(lhs), expr->getOpcodeStr(),
1253+
ConvertFreshPointer(rhs));
1254+
}
1255+
ConvertCast(expr->getType());
12521256
computed_expr_type_ = ComputedExprType::FreshValue;
12531257
return false;
12541258
}

libcc2rs/src/rc.rs

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -675,45 +675,29 @@ impl<T> Sub for Ptr<T> {
675675
}
676676
}
677677

678-
impl<T> std::ops::AddAssign<u64> for Ptr<T> {
679-
#[inline]
680-
fn add_assign(&mut self, other: u64) {
681-
let step = self.elem_step();
682-
self.offset = self
683-
.offset
684-
.wrapping_add((other as usize).wrapping_mul(step));
685-
}
686-
}
687-
688-
impl<T> std::ops::AddAssign<i32> for Ptr<T> {
689-
#[inline]
690-
fn add_assign(&mut self, other: i32) {
691-
let step = self.elem_step();
692-
self.offset = self
693-
.offset
694-
.wrapping_add(((other as isize).wrapping_mul(step as isize)) as usize);
695-
}
696-
}
697-
698-
impl<T> std::ops::AddAssign<u32> for Ptr<T> {
699-
#[inline]
700-
fn add_assign(&mut self, other: u32) {
701-
let step = self.elem_step();
702-
self.offset = self
703-
.offset
704-
.wrapping_add((other as usize).wrapping_mul(step));
705-
}
706-
}
707-
708-
impl<T> std::ops::AddAssign<isize> for Ptr<T> {
709-
#[inline]
710-
fn add_assign(&mut self, other: isize) {
711-
let step = self.elem_step();
712-
self.offset = self
713-
.offset
714-
.wrapping_add((other.wrapping_mul(step as isize)) as usize);
715-
}
678+
macro_rules! impl_ptr_add_sub_assign {
679+
($($rhs:ty),+) => { $(
680+
impl<T> std::ops::AddAssign<$rhs> for Ptr<T> {
681+
#[inline]
682+
fn add_assign(&mut self, other: $rhs) {
683+
let step = self.elem_step();
684+
self.offset = self.offset.wrapping_add(
685+
((other as isize).wrapping_mul(step as isize)) as usize,
686+
);
687+
}
688+
}
689+
impl<T> std::ops::SubAssign<$rhs> for Ptr<T> {
690+
#[inline]
691+
fn sub_assign(&mut self, other: $rhs) {
692+
let step = self.elem_step();
693+
self.offset = self.offset.wrapping_sub(
694+
((other as isize).wrapping_mul(step as isize)) as usize,
695+
);
696+
}
697+
}
698+
)+ }
716699
}
700+
impl_ptr_add_sub_assign!(i32, u32, u64, isize);
717701

718702
macro_rules! impl_ptr_add_sub {
719703
($($rhs:ty),+) => { $(

tests/ub/out/refcount/ub7.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn strlen_0(s: Ptr<u8>) -> u64 {
1212
'loop_: while (((*s.borrow()).read()) != 0) {
1313
(*s.borrow_mut()).prefix_inc();
1414
}
15-
return (((*s.borrow()).clone() - (*begin.borrow()).clone()) as u64);
15+
return ((((*s.borrow()).clone() - (*begin.borrow()).clone()) as i64) as u64);
1616
}
1717
pub fn main() {
1818
std::process::exit(main_0());

tests/ub/out/unsafe/ub7.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub unsafe fn strlen_0(mut s: *const u8) -> u64 {
1111
'loop_: while ((*s) != 0) {
1212
s.prefix_inc();
1313
}
14-
return ((((s as usize - begin as usize) / ::std::mem::size_of::<u8>()) as u64) as u64);
14+
return ((((s as usize - begin as usize) / ::std::mem::size_of::<u8>()) as i64) as u64);
1515
}
1616
pub fn main() {
1717
unsafe {

tests/unit/out/refcount/02_address_taken.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn main_0() -> i32 {
1818
let __rhs = (((*b_ptr_ptr.borrow()).read()).read());
1919
(*b_ptr.borrow()).write(__rhs);
2020
let offset: Value<u64> = Rc::new(RefCell::new(
21-
(((*b_ptr.borrow()).clone() - (*b_ptr.borrow()).clone()) as u64),
21+
((((*b_ptr.borrow()).clone() - (*b_ptr.borrow()).clone()) as i64) as u64),
2222
));
2323
return (*b.borrow());
2424
}

tests/unit/out/refcount/pointer_diff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ fn main_0() -> i32 {
1717
let p1: Value<Ptr<i32>> = Rc::new(RefCell::new(
1818
((a.as_pointer() as Ptr<i32>).offset(4 as isize)),
1919
));
20-
return (((*p1.borrow()).clone() - (*p0.borrow()).clone()) as i32);
20+
return ((((*p1.borrow()).clone() - (*p0.borrow()).clone()) as i64) as i32);
2121
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn main() {
10+
std::process::exit(main_0());
11+
}
12+
fn main_0() -> i32 {
13+
let arr: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([10, 11, 12, 13, 14, 15, 16, 17])));
14+
let p: Value<Ptr<i32>> = Rc::new(RefCell::new((arr.as_pointer() as Ptr<i32>)));
15+
let q: Value<Ptr<i32>> = Rc::new(RefCell::new((*p.borrow()).offset((1) as isize)));
16+
assert!((((*q.borrow()).read()) == 11));
17+
let r: Value<Ptr<i32>> = Rc::new(RefCell::new((*p.borrow()).offset((3) as isize)));
18+
assert!((((*r.borrow()).read()) == 13));
19+
let s: Value<Ptr<i32>> = Rc::new(RefCell::new((*r.borrow()).offset(-((2) as isize))));
20+
assert!((((*s.borrow()).read()) == 11));
21+
let diff: Value<i64> = Rc::new(RefCell::new(
22+
((*r.borrow()).clone() - (*p.borrow()).clone()) as i64,
23+
));
24+
assert!(((*diff.borrow()) == 3_i64));
25+
let idx: Value<u64> = Rc::new(RefCell::new(
26+
(((((*r.borrow()).clone() - (*p.borrow()).clone()) as i64) as i64) as u64),
27+
));
28+
assert!(((*idx.borrow()) == 3_u64));
29+
let q2: Value<Ptr<i32>> = Rc::new(RefCell::new((*p.borrow()).clone()));
30+
(*q2.borrow_mut()).prefix_inc();
31+
assert!((((*q2.borrow()).read()) == 11));
32+
(*q2.borrow_mut()).postfix_inc();
33+
assert!((((*q2.borrow()).read()) == 12));
34+
(*q2.borrow_mut()).prefix_dec();
35+
assert!((((*q2.borrow()).read()) == 11));
36+
(*q2.borrow_mut()).postfix_dec();
37+
assert!((((*q2.borrow()).read()) == 10));
38+
assert!({
39+
let _lhs = (*q2.borrow()).clone();
40+
_lhs == (*p.borrow()).clone()
41+
});
42+
let q3: Value<Ptr<i32>> = Rc::new(RefCell::new((*p.borrow()).clone()));
43+
(*q3.borrow_mut()) += 4;
44+
assert!((((*q3.borrow()).read()) == 14));
45+
(*q3.borrow_mut()) -= 2;
46+
assert!((((*q3.borrow()).read()) == 12));
47+
let step: Value<u64> = Rc::new(RefCell::new(2_u64));
48+
let q4: Value<Ptr<i32>> = Rc::new(RefCell::new(
49+
(*p.borrow()).offset((*step.borrow()) as isize),
50+
));
51+
assert!((((*q4.borrow()).read()) == 12));
52+
let v: Value<i32> = Rc::new(RefCell::new(((*p.borrow()).offset((3) as isize).read())));
53+
assert!(((*v.borrow()) == 13));
54+
let v2: Value<i32> = Rc::new(RefCell::new((((*p.borrow()).offset((4) as isize)).read())));
55+
assert!(((*v2.borrow()) == 14));
56+
((*p.borrow()).offset((5) as isize)).write(99);
57+
assert!((((*p.borrow()).offset((5) as isize).read()) == 99));
58+
assert!(((*arr.borrow())[(5) as usize] == 99));
59+
let end: Value<Ptr<i32>> = Rc::new(RefCell::new((*p.borrow()).offset((8) as isize)));
60+
let sum: Value<i32> = Rc::new(RefCell::new(0));
61+
let it: Value<Ptr<i32>> = Rc::new(RefCell::new((*p.borrow()).clone()));
62+
'loop_: while {
63+
let _lhs = (*it.borrow()).clone();
64+
_lhs != (*end.borrow()).clone()
65+
} {
66+
let __rhs = ((*it.borrow()).read());
67+
(*sum.borrow_mut()) += __rhs;
68+
(*it.borrow_mut()).prefix_inc();
69+
}
70+
assert!(((*sum.borrow()) == (((((((10 + 11) + 12) + 13) + 14) + 99) + 16) + 17)));
71+
let bytes: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::new([
72+
0_u8, 1_u8, 2_u8, 3_u8, 4_u8, 5_u8, 6_u8, 7_u8,
73+
])));
74+
let bp: Value<Ptr<u8>> = Rc::new(RefCell::new((bytes.as_pointer() as Ptr<u8>)));
75+
let bq: Value<Ptr<u8>> = Rc::new(RefCell::new((*bp.borrow()).offset((4) as isize)));
76+
assert!(((((*bq.borrow()).read()) as i32) == 4));
77+
let bdiff: Value<i64> = Rc::new(RefCell::new(
78+
((*bq.borrow()).clone() - (*bp.borrow()).clone()) as i64,
79+
));
80+
assert!(((*bdiff.borrow()) == 4_i64));
81+
let cp: Value<Ptr<i32>> = Rc::new(RefCell::new((arr.as_pointer() as Ptr<i32>)));
82+
let cq: Value<Ptr<i32>> = Rc::new(RefCell::new((*cp.borrow()).offset((2) as isize)));
83+
assert!((((*cq.borrow()).read()) == 12));
84+
let cdiff: Value<i64> = Rc::new(RefCell::new(
85+
((*cq.borrow()).clone() - (*cp.borrow()).clone()) as i64,
86+
));
87+
assert!(((*cdiff.borrow()) == 2_i64));
88+
let n: Value<u64> = Rc::new(RefCell::new(3_u64));
89+
let q5: Value<Ptr<i32>> = Rc::new(RefCell::new(
90+
(arr.as_pointer() as Ptr<i32>).offset((*n.borrow()) as isize),
91+
));
92+
assert!((((*q5.borrow()).read()) == 13));
93+
let q6: Value<Ptr<i32>> = Rc::new(RefCell::new(
94+
((arr.as_pointer() as Ptr<i32>).offset((*n.borrow()) as isize)),
95+
));
96+
assert!({
97+
let _lhs = (*q6.borrow()).clone();
98+
_lhs == (*q5.borrow()).clone()
99+
});
100+
let matrix: Value<Box<[Value<Box<[i32]>>]>> = Rc::new(RefCell::new(Box::new([
101+
Rc::new(RefCell::new(Box::new([0, 1, 2, 3]))),
102+
Rc::new(RefCell::new(Box::new([4, 5, 6, 7]))),
103+
Rc::new(RefCell::new(Box::new([8, 9, 10, 11]))),
104+
])));
105+
let row1: Value<Ptr<i32>> = Rc::new(RefCell::new(
106+
((((matrix.as_pointer() as Ptr<Value<Box<[i32]>>>)
107+
.offset(1 as isize)
108+
.read()
109+
.as_pointer()) as Ptr<i32>)
110+
.offset(0 as isize)),
111+
));
112+
assert!((((*row1.borrow()).offset((2) as isize).read()) == 6));
113+
let back: Value<Ptr<i32>> = Rc::new(RefCell::new((*end.borrow()).offset(-((1) as isize))));
114+
assert!((((*back.borrow()).read()) == 17));
115+
return 0;
116+
}

tests/unit/out/refcount/strlen_diff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn strlen_0(s: Ptr<u8>) -> u64 {
1212
'loop_: while (((*s.borrow()).read()) != 0) {
1313
(*s.borrow_mut()).prefix_inc();
1414
}
15-
return (((*s.borrow()).clone() - (*begin.borrow()).clone()) as u64);
15+
return ((((*s.borrow()).clone() - (*begin.borrow()).clone()) as i64) as u64);
1616
}
1717
pub fn main() {
1818
std::process::exit(main_0());

tests/unit/out/unsafe/02_address_taken.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ unsafe fn main_0() -> i32 {
1919
(*(*b_ptr_ptr)) = 4;
2020
(*b_ptr) = (*(*b_ptr_ptr));
2121
let mut offset: u64 =
22-
((((b_ptr as usize - b_ptr as usize) / ::std::mem::size_of::<i32>()) as u64) as u64);
22+
((((b_ptr as usize - b_ptr as usize) / ::std::mem::size_of::<i32>()) as i64) as u64);
2323
return b;
2424
}

0 commit comments

Comments
 (0)