Skip to content

Commit 659eb45

Browse files
tgtakaokacire831
authored andcommitted
Fix logical operator constant folding (#45)
This fixes [issue#45](#45).
1 parent ac94d8e commit 659eb45

File tree

8 files changed

+110
-0
lines changed

8 files changed

+110
-0
lines changed
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
And00.checkDone -> 0
2+
And01.checkDone -> 0
3+
And10.checkDone -> 0
4+
And11.checkDone -> 1
5+
Or00.checkDone -> 0
6+
Or01.checkDone -> 1
7+
Or10.checkDone -> 1
8+
Or11.checkDone -> 1

nregress/ok/runnable.fold_logical_ops.2

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
interface Check {
2+
command void check();
3+
event void checkDone(int result);
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
generic module Module(int result) {
2+
provides interface Check;
3+
} implementation {
4+
command void Check.check() {
5+
signal Check.checkDone(result);
6+
}
7+
8+
default event void Check.checkDone(int result) {
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <stdio.h>
2+
3+
module TestP {
4+
uses {
5+
interface Check as And00;
6+
interface Check as And01;
7+
interface Check as And10;
8+
interface Check as And11;
9+
interface Check as Or00;
10+
interface Check as Or01;
11+
interface Check as Or10;
12+
interface Check as Or11;
13+
}
14+
} implementation {
15+
int main() @C() @spontaneous() {
16+
call And00.check();
17+
call And01.check();
18+
call And10.check();
19+
call And11.check();
20+
call Or00.check();
21+
call Or01.check();
22+
call Or10.check();
23+
call Or11.check();
24+
}
25+
26+
event void And00.checkDone(int result) {
27+
printf("And00.checkDone -> %d\n", result);
28+
}
29+
30+
event void And01.checkDone(int result) {
31+
printf("And01.checkDone -> %d\n", result);
32+
}
33+
34+
event void And10.checkDone(int result) {
35+
printf("And10.checkDone -> %d\n", result);
36+
}
37+
38+
event void And11.checkDone(int result) {
39+
printf("And11.checkDone -> %d\n", result);
40+
}
41+
42+
event void Or00.checkDone(int result) {
43+
printf("Or00.checkDone -> %d\n", result);
44+
}
45+
46+
event void Or01.checkDone(int result) {
47+
printf("Or01.checkDone -> %d\n", result);
48+
}
49+
50+
event void Or10.checkDone(int result) {
51+
printf("Or10.checkDone -> %d\n", result);
52+
}
53+
54+
event void Or11.checkDone(int result) {
55+
printf("Or11.checkDone -> %d\n", result);
56+
}
57+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
configuration test {
2+
} implementation {
3+
components TestP;
4+
5+
components new Module(0 == 1 && 0 > 1) as And00;
6+
components new Module(0 == 1 && 0 < 1) as And01;
7+
components new Module(0 != 1 && 0 > 1) as And10;
8+
components new Module(0 != 1 && 0 < 1) as And11;
9+
components new Module(0 == 1 || 0 > 1) as Or00;
10+
components new Module(0 == 1 || 0 < 1) as Or01;
11+
components new Module(0 != 1 || 0 > 1) as Or10;
12+
components new Module(0 != 1 || 0 < 1) as Or11;
13+
14+
TestP.And00 -> And00;
15+
TestP.And01 -> And01;
16+
TestP.And10 -> And10;
17+
TestP.And11 -> And11;
18+
TestP.Or00 -> Or00;
19+
TestP.Or01 -> Or01;
20+
TestP.Or10 -> Or10;
21+
TestP.Or11 -> Or11;
22+
}

src/constants.c

+8
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,14 @@ known_cst fold_binary(type t, expression e)
267267
bool c2val = constant_boolvalue(c2);
268268
if (b->kind == kind_andand ? !c2val : c2val)
269269
return make_signed_cst(c2val, t);
270+
if (constant_knownbool(c1))
271+
{
272+
bool c1val = constant_boolvalue(c1);
273+
return make_signed_cst(b->kind == kind_andand
274+
? (c1val && c2val)
275+
: (c1val || c2val),
276+
t);
277+
}
270278
}
271279

272280
if (constant_unknown_number(c2))

0 commit comments

Comments
 (0)