Skip to content

Fix logical operator constant folding #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions nregress/ok/runnable.fold_logical_ops.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
And00.checkDone -> 0
And01.checkDone -> 0
And10.checkDone -> 0
And11.checkDone -> 1
Or00.checkDone -> 0
Or01.checkDone -> 1
Or10.checkDone -> 1
Or11.checkDone -> 1
Empty file.
1 change: 1 addition & 0 deletions nregress/ok/runnable.fold_logical_ops.exit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
4 changes: 4 additions & 0 deletions nregress/runnable/fold_logical_ops/Check.nc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface Check {
command void check();
event void checkDone(int result);
}
10 changes: 10 additions & 0 deletions nregress/runnable/fold_logical_ops/Module.nc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
generic module Module(int result) {
provides interface Check;
} implementation {
command void Check.check() {
signal Check.checkDone(result);
}

default event void Check.checkDone(int result) {
}
}
57 changes: 57 additions & 0 deletions nregress/runnable/fold_logical_ops/TestP.nc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <stdio.h>

module TestP {
uses {
interface Check as And00;
interface Check as And01;
interface Check as And10;
interface Check as And11;
interface Check as Or00;
interface Check as Or01;
interface Check as Or10;
interface Check as Or11;
}
} implementation {
int main() @C() @spontaneous() {
call And00.check();
call And01.check();
call And10.check();
call And11.check();
call Or00.check();
call Or01.check();
call Or10.check();
call Or11.check();
}

event void And00.checkDone(int result) {
printf("And00.checkDone -> %d\n", result);
}

event void And01.checkDone(int result) {
printf("And01.checkDone -> %d\n", result);
}

event void And10.checkDone(int result) {
printf("And10.checkDone -> %d\n", result);
}

event void And11.checkDone(int result) {
printf("And11.checkDone -> %d\n", result);
}

event void Or00.checkDone(int result) {
printf("Or00.checkDone -> %d\n", result);
}

event void Or01.checkDone(int result) {
printf("Or01.checkDone -> %d\n", result);
}

event void Or10.checkDone(int result) {
printf("Or10.checkDone -> %d\n", result);
}

event void Or11.checkDone(int result) {
printf("Or11.checkDone -> %d\n", result);
}
}
22 changes: 22 additions & 0 deletions nregress/runnable/fold_logical_ops/test.nc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
configuration test {
} implementation {
components TestP;

components new Module(0 == 1 && 0 > 1) as And00;
components new Module(0 == 1 && 0 < 1) as And01;
components new Module(0 != 1 && 0 > 1) as And10;
components new Module(0 != 1 && 0 < 1) as And11;
components new Module(0 == 1 || 0 > 1) as Or00;
components new Module(0 == 1 || 0 < 1) as Or01;
components new Module(0 != 1 || 0 > 1) as Or10;
components new Module(0 != 1 || 0 < 1) as Or11;

TestP.And00 -> And00;
TestP.And01 -> And01;
TestP.And10 -> And10;
TestP.And11 -> And11;
TestP.Or00 -> Or00;
TestP.Or01 -> Or01;
TestP.Or10 -> Or10;
TestP.Or11 -> Or11;
}
8 changes: 8 additions & 0 deletions src/constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ known_cst fold_binary(type t, expression e)
bool c2val = constant_boolvalue(c2);
if (b->kind == kind_andand ? !c2val : c2val)
return make_signed_cst(c2val, t);
if (constant_knownbool(c1))
{
bool c1val = constant_boolvalue(c1);
return make_signed_cst(b->kind == kind_andand
? (c1val && c2val)
: (c1val || c2val),
t);
}
}

if (constant_unknown_number(c2))
Expand Down