Skip to content

Commit d6a7e71

Browse files
braw-leeP-E-P
authored andcommitted
Improve compressed point bit manipulation
gcc/rust/ChangeLog: * checks/errors/borrowck/polonius/rust-polonius.h (struct FullPoint): Added comments and made extraction of statement more verbose for better understanding. * checks/errors/borrowck/ffi-polonius/src/lib.rs: Likewise. Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
1 parent a395a81 commit d6a7e71

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

gcc/rust/checks/errors/borrowck/ffi-polonius/src/lib.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,24 @@ impl From<gccrs_ffi::FactsView> for AllFacts<GccrsFacts> {
105105

106106
fn print_point(point: GccrsAtom) {
107107
let val: usize = point.into();
108+
// Point is a 32 bit unsigned integer
109+
// 16 15 1
110+
// xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxx x
111+
// ^~~~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~ ^
112+
// | | |
113+
// basic_block | start/mid
114+
// statement
115+
// the left most 16 bits store the basic block number
116+
// the right most bit, represents the start/mid status
117+
// the remaining 15 bits between these two represent the statement
108118
let mid = val % 2 == 1;
109119
let bb = val >> 16;
110-
let stmt = (val >> 1) & ((1 << 15) - 1);
120+
// firstly we can get rid of right most bit by performing left shift once
121+
let hide_left_most_bit = val >> 1;
122+
// now we only need the 15 bits on the right
123+
// we can mask the remaining bits by performing bitwise AND with fifteen
124+
// 1's which in hexadecimal is 0x7FFF
125+
let stmt = hide_left_most_bit & 0x7FFF;
111126
eprint!("{}(bb{}[{}])", if mid { "Mid" } else { "Start" }, bb, stmt);
112127
}
113128

gcc/rust/checks/errors/borrowck/polonius/rust-polonius.h

+19-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct FullPoint
3535
bool mid;
3636

3737
/** Expands a compressed `Point` into its components.
38-
* See `Point` docs for encoding details.
38+
* See `Point` docs for encoding details in ./rust-polonius-ffi.h
3939
*/
4040
explicit FullPoint (Point point)
4141
: bb (extract_bb (point)), stmt (extract_stmt (point)),
@@ -45,7 +45,24 @@ struct FullPoint
4545
static uint32_t extract_bb (Point point) { return point >> 16; }
4646
static uint32_t extract_stmt (Point point)
4747
{
48-
return (point >> 1) & ((1 << 15) - 1);
48+
// Point is a 32 bit unsigned integer
49+
// 16 15 1
50+
// xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxx x
51+
// ^~~~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~ ^
52+
// | | |
53+
// basic_block | start/mid
54+
// statement
55+
// the left most 16 bits store the basic block number
56+
// the right most bit, represents the start/mid status
57+
// the remaining 15 bits between these two represent the statement number
58+
// which we need to extract in this fucntion
59+
//
60+
// firstly we can get rid of right most bit by performing left shift once
61+
auto hide_left_most_bit = point >> 1;
62+
// now we only need the 15 bits on the right
63+
// we can mask the remaining bits by performing bitwise AND with fifteen
64+
// 1's which in hexadecimal is 0x7FFF
65+
return hide_left_most_bit & 0x7FFF;
4966
}
5067
static bool extract_mid (Point point) { return point & 1; }
5168

0 commit comments

Comments
 (0)