Skip to content

Commit 2c67f24

Browse files
authored
[unsafe] Add ctlz, popcountl, mut_overflow builtin rules (#140)
1 parent bbfc459 commit 2c67f24

9 files changed

Lines changed: 627 additions & 0 deletions

File tree

rules/builtin/ir_refcount.json

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
{
2+
"f10": {
3+
"body": [
4+
{
5+
"text": "let (val, ovf) = "
6+
},
7+
{
8+
"method_call": {
9+
"receiver": [
10+
{
11+
"placeholder": {
12+
"arg": 0,
13+
"access": "read"
14+
}
15+
}
16+
],
17+
"body": [
18+
{
19+
"text": ".overflowing_mul("
20+
},
21+
{
22+
"placeholder": {
23+
"arg": 1,
24+
"access": "read"
25+
}
26+
},
27+
{
28+
"text": ")"
29+
}
30+
]
31+
}
32+
},
33+
{
34+
"text": ";\n "
35+
},
36+
{
37+
"method_call": {
38+
"receiver": [
39+
{
40+
"placeholder": {
41+
"arg": 2,
42+
"access": "read"
43+
}
44+
}
45+
],
46+
"body": [
47+
{
48+
"text": ".write(val)"
49+
}
50+
]
51+
}
52+
},
53+
{
54+
"text": ";\n ovf"
55+
}
56+
],
57+
"multi_statement": true,
58+
"params": {
59+
"a0": {
60+
"type": "i64"
61+
},
62+
"a1": {
63+
"type": "i64"
64+
},
65+
"a2": {
66+
"type": "Ptr<i64>",
67+
"is_refcount_pointer": true
68+
}
69+
},
70+
"return_type": {
71+
"type": "bool"
72+
}
73+
},
74+
"f9": {
75+
"body": [
76+
{
77+
"text": "let (val, ovf) = "
78+
},
79+
{
80+
"method_call": {
81+
"receiver": [
82+
{
83+
"placeholder": {
84+
"arg": 0,
85+
"access": "read"
86+
}
87+
}
88+
],
89+
"body": [
90+
{
91+
"text": ".overflowing_mul("
92+
},
93+
{
94+
"placeholder": {
95+
"arg": 1,
96+
"access": "read"
97+
}
98+
},
99+
{
100+
"text": ")"
101+
}
102+
]
103+
}
104+
},
105+
{
106+
"text": ";\n "
107+
},
108+
{
109+
"method_call": {
110+
"receiver": [
111+
{
112+
"placeholder": {
113+
"arg": 2,
114+
"access": "read"
115+
}
116+
}
117+
],
118+
"body": [
119+
{
120+
"text": ".write(val)"
121+
}
122+
]
123+
}
124+
},
125+
{
126+
"text": ";\n ovf"
127+
}
128+
],
129+
"multi_statement": true,
130+
"params": {
131+
"a0": {
132+
"type": "i64"
133+
},
134+
"a1": {
135+
"type": "i64"
136+
},
137+
"a2": {
138+
"type": "Ptr<i64>",
139+
"is_refcount_pointer": true
140+
}
141+
},
142+
"return_type": {
143+
"type": "bool"
144+
}
145+
}
146+
}

rules/builtin/ir_unsafe.json

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,67 @@
2020
"type": "usize"
2121
}
2222
},
23+
"f10": {
24+
"body": [
25+
{
26+
"text": "let (val, ovf) = "
27+
},
28+
{
29+
"method_call": {
30+
"receiver": [
31+
{
32+
"placeholder": {
33+
"arg": 0,
34+
"access": "read"
35+
}
36+
}
37+
],
38+
"body": [
39+
{
40+
"text": ".overflowing_mul("
41+
},
42+
{
43+
"placeholder": {
44+
"arg": 1,
45+
"access": "read"
46+
}
47+
},
48+
{
49+
"text": ")"
50+
}
51+
]
52+
}
53+
},
54+
{
55+
"text": ";\n *"
56+
},
57+
{
58+
"placeholder": {
59+
"arg": 2,
60+
"access": "write"
61+
}
62+
},
63+
{
64+
"text": " = val;\n ovf"
65+
}
66+
],
67+
"multi_statement": true,
68+
"params": {
69+
"a0": {
70+
"type": "i64"
71+
},
72+
"a1": {
73+
"type": "i64"
74+
},
75+
"a2": {
76+
"type": "*mut i64",
77+
"is_unsafe_pointer": true
78+
}
79+
},
80+
"return_type": {
81+
"type": "bool"
82+
}
83+
},
2384
"f2": {
2485
"body": [
2586
{
@@ -170,5 +231,130 @@
170231
"return_type": {
171232
"type": "u64"
172233
}
234+
},
235+
"f7": {
236+
"body": [
237+
{
238+
"method_call": {
239+
"receiver": [
240+
{
241+
"placeholder": {
242+
"arg": 0,
243+
"access": "read"
244+
}
245+
}
246+
],
247+
"body": [
248+
{
249+
"text": ".trailing_zeros()"
250+
}
251+
]
252+
}
253+
},
254+
{
255+
"text": " as i32"
256+
}
257+
],
258+
"params": {
259+
"a0": {
260+
"type": "u64"
261+
}
262+
},
263+
"return_type": {
264+
"type": "i32"
265+
}
266+
},
267+
"f8": {
268+
"body": [
269+
{
270+
"method_call": {
271+
"receiver": [
272+
{
273+
"placeholder": {
274+
"arg": 0,
275+
"access": "read"
276+
}
277+
}
278+
],
279+
"body": [
280+
{
281+
"text": ".count_ones()"
282+
}
283+
]
284+
}
285+
},
286+
{
287+
"text": " as i32"
288+
}
289+
],
290+
"params": {
291+
"a0": {
292+
"type": "u64"
293+
}
294+
},
295+
"return_type": {
296+
"type": "i32"
297+
}
298+
},
299+
"f9": {
300+
"body": [
301+
{
302+
"text": "let (val, ovf) = "
303+
},
304+
{
305+
"method_call": {
306+
"receiver": [
307+
{
308+
"placeholder": {
309+
"arg": 0,
310+
"access": "read"
311+
}
312+
}
313+
],
314+
"body": [
315+
{
316+
"text": ".overflowing_mul("
317+
},
318+
{
319+
"placeholder": {
320+
"arg": 1,
321+
"access": "read"
322+
}
323+
},
324+
{
325+
"text": ")"
326+
}
327+
]
328+
}
329+
},
330+
{
331+
"text": ";\n *"
332+
},
333+
{
334+
"placeholder": {
335+
"arg": 2,
336+
"access": "write"
337+
}
338+
},
339+
{
340+
"text": " = val;\n ovf"
341+
}
342+
],
343+
"multi_statement": true,
344+
"params": {
345+
"a0": {
346+
"type": "i64"
347+
},
348+
"a1": {
349+
"type": "i64"
350+
},
351+
"a2": {
352+
"type": "*mut i64",
353+
"is_unsafe_pointer": true
354+
}
355+
},
356+
"return_type": {
357+
"type": "bool"
358+
}
173359
}
174360
}

rules/builtin/src.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ unsigned short f4(unsigned short x) { return __builtin_bswap16(x); }
1919
unsigned int f5(unsigned int x) { return __builtin_bswap32(x); }
2020
unsigned long long f6(unsigned long long x) { return __builtin_bswap64(x); }
2121
#endif
22+
int f7(unsigned long x) { return __builtin_ctzl(x); }
23+
int f8(unsigned long x) { return __builtin_popcountl(x); }
24+
bool f9(long a, long b, long *r) { return __builtin_mul_overflow(a, b, r); }
25+
bool f10(long long a, long long b, long long *r) { return __builtin_mul_overflow(a, b, r); }

rules/builtin/tgt_refcount.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use libcc2rs::*;
5+
6+
fn f9(a0: i64, a1: i64, a2: Ptr<i64>) -> bool {
7+
let (val, ovf) = a0.overflowing_mul(a1);
8+
a2.write(val);
9+
ovf
10+
}
11+
fn f10(a0: i64, a1: i64, a2: Ptr<i64>) -> bool {
12+
let (val, ovf) = a0.overflowing_mul(a1);
13+
a2.write(val);
14+
ovf
15+
}

rules/builtin/tgt_unsafe.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,19 @@ unsafe fn f5(a0: u32) -> u32 {
1919
unsafe fn f6(a0: u64) -> u64 {
2020
a0.swap_bytes()
2121
}
22+
unsafe fn f7(a0: u64) -> i32 {
23+
a0.trailing_zeros() as i32
24+
}
25+
unsafe fn f8(a0: u64) -> i32 {
26+
a0.count_ones() as i32
27+
}
28+
unsafe fn f9(a0: i64, a1: i64, a2: *mut i64) -> bool {
29+
let (val, ovf) = a0.overflowing_mul(a1);
30+
*a2 = val;
31+
ovf
32+
}
33+
unsafe fn f10(a0: i64, a1: i64, a2: *mut i64) -> bool {
34+
let (val, ovf) = a0.overflowing_mul(a1);
35+
*a2 = val;
36+
ovf
37+
}

rules/src/modules.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub mod assert_tgt_unsafe;
1616
pub mod brotli_tgt_refcount;
1717
#[path = r#"../brotli/tgt_unsafe.rs"#]
1818
pub mod brotli_tgt_unsafe;
19+
#[path = r#"../builtin/tgt_refcount.rs"#]
20+
pub mod builtin_tgt_refcount;
1921
#[path = r#"../builtin/tgt_unsafe.rs"#]
2022
pub mod builtin_tgt_unsafe;
2123
#[path = r#"../carray/tgt_refcount.rs"#]

0 commit comments

Comments
 (0)