1
- % this simple implementation does NOT consider the control flow graph
2
- % i.e. if (){
3
- % x=1;}
4
- % else{
5
- % x=2;}
6
- % y=x;
7
- % will lead to inconsistencies in the calculation
8
- % thus it is insufficient for real world examples
1
+ @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
2
+ false("false"^^xsd:boolean).
9
3
10
- % NOTE: AssignLocal MUST NOT contain phi-assignments, otherwise the chase is not successfull
11
- % run something like 'grep -v "phi-assign" data/AssignLocal.tsv.gz > data/AssignLocal.tsv.gz'
4
+ %! this simple implementation does NOT consider the control flow graph
5
+ %! e.g. if (){
6
+ %! x=1;}
7
+ %! else{
8
+ %! x=2;}
9
+ %! y=x;
10
+ %! will lead to inconsistencies in the calculation
11
+ %! thus it is insufficient for real world examples
12
12
13
- % dive into doop-syntax:
14
- % the instrucion-id (?ins) is a unique identifier for each line of intermediate code (IR)
15
- % everything that happens on one IR-line, will be linked with the same instruction-id
16
- % it may look like this: "<Example: void main(java.lang.String[])>/assign/4"
17
- % where "<Example: void main(java.lang.String[])>/" is the function signature
18
- % "assign/" means it is an assignment (i.e. a=b;), and "4" is the line number of the statement
13
+ %! dive into doop-syntax:
14
+ %! the instrucion-id (?ins) is a unique identifier for each line of intermediate code (IR)
15
+ %! everything that happens on one IR-line, will be linked with the same instruction-id
16
+ %! it may look like this: "<Example: void main(java.lang.String[])>/assign/4"
17
+ %! where "<Example: void main(java.lang.String[])>/" is the function signature
18
+ %! "assign/" means it is an assignment (i.e. a=b;), and "4" is the line number of the statement
19
19
20
20
21
- %loaded files from doop:
21
+ %% load facts generated by doop
22
22
23
- % AssignNumConstant: contains all variables that hold a constant value (i.e. a = 1) -> this is the base for this analysis
23
+ %%% Contains all variables that hold a constant value (i.e. a = 1) -> this is the base for this analysis
24
24
@import AssignNumConstant :- tsv { resource = "data/AssignNumConstant.tsv.gz" } .
25
25
26
26
27
- % AssignLocal: holds assignments of type a = b.
28
- % which only holds static assignments (assignments that include the string "phi-assign" are dynamically and have been removed)
27
+ %%% Holds assignments of type a = b.
28
+ %%% Which only holds static assignments (assignments that include the string "phi-assign" are dynamically and have been removed)
29
29
@import AssignLocal :- tsv { resource = "data/AssignLocal.tsv.gz" } .
30
30
31
31
32
- % OperatorAt: links an instruction-id of a unary or binary expression to its operator
33
- % most common operators: (~, +, -, *, /, >=, <=, ==, <<, !=, >, len, <, %, ^, cmp, &, cmpl, cmpg, >>>, >>, |)
32
+ %%% Links an instruction-id of a unary or binary expression to its operator
33
+ %%% Most common operators: (~, +, -, *, /, >=, <=, ==, <<, !=, >, len, <, %, ^, cmp, &, cmpl, cmpg, >>>, >>, |)
34
34
@import OperatorAt :- tsv { resource = "data/OperatorAt.tsv.gz" } .
35
35
36
36
37
- % AssignUnop: holds the instruction-id and variable that is getting assigned by a unary expression
37
+ %%% Holds the instruction-id and variable that is getting assigned by a unary expression
38
38
@import AssignUnop :- tsv { resource = "data/AssignUnop.tsv.gz" } .
39
39
40
40
41
- % AssignBinop: holds the instruction-id and the variable that is getting assigned by a binary expression
41
+ %%% Holds the instruction-id and the variable that is getting assigned by a binary expression
42
42
@import AssignBinop :- tsv { resource = "data/AssignBinop.tsv.gz" } .
43
43
44
44
45
- % AssignOperFrom: links an instruction-id of a unary or binary expression, to the VARIABLE(S) that are used
46
- % (i.e . a = b + c, will have two entries in AssignOperFrom)
45
+ %%% Links an instruction-id of a unary or binary expression, to the VARIABLE(S) that are used
46
+ %%% (e.g . a = b + c, will have two entries in AssignOperFrom)
47
47
@import AssignOperFrom :- tsv { resource = "data/AssignOperFrom.tsv.gz" } .
48
48
49
49
50
- % AssignOperFromConstant: links an instruction-id of a unary or binary expression to the CONSTANT(S) used
51
- % (i.e. a = b + 2, will have one entries in AssignOperFromConstant)
50
+ %%% Links an instruction-id of a unary or binary expression to the CONSTANT(S) used
51
+ %%% (e.g. a = b + 2, will have one entry in AssignOperFromConstant)
52
52
@import AssignOperFromConstant :- tsv { resource = "data/AssignOperFromConstant.tsv.gz" }.
53
53
54
54
55
-
56
- % Implementation:
55
+ %% Implementation:
57
56
% IntegerConstant contains all constants (given & computed)
58
57
% it is initialized with all constants that are supplied by doop in AssignNumConstant (i.e. a = 2;)
59
58
% ConstFolding will contain all variables and values where constant folding & propagation was successfull
59
+
60
60
% the doop relations AssignUnop & AssignBinop ONLY contain the instruction-id and the assigned variable
61
61
% -> we need to manually check, if the expression consists of constants (a = 1 + 2;) variables (a = b + c;) or both (a = b + 2;)
62
62
% -> 2 cases for unary expressions: (- const), (- var)
63
63
% -> 3 cases each for binary expressions (+,*): (var + var), (const + const), (const + var)-> order of var & const is irrelevant
64
64
% -> 4 cases each for binary expressions (-, /): (var - var), (const - const), (const - var), (var - const) -> order of var & const is relevant
65
65
66
-
67
- % Initialize IntegerConstant with all constants from AssignNumConstant
66
+ %%% Initialize IntegerConstant with all constants from AssignNumConstant
68
67
IntegerConstant(?var, ?val,?meth) :-
69
68
AssignNumConstant(_, _, ?val, ?var, ?meth).
70
69
71
- %Assignment without Operator (i.e. a = b)
70
+ %%% Assignment without Operator (i.e. a = b)
71
+ %%% Remove statements containing 'phi-assign' since they indicate CFG-issues
72
72
IntegerConstant(?var_to, ?val, ?meth),
73
73
ConstFolding(?ins,?var_to, ?val, ?meth) :-
74
74
AssignLocal(?ins, _, ?var_from, ?var_to, ?meth),
75
- IntegerConstant(?var_from, ?val, ?meth).
76
-
75
+ false(CONTAINS(?ins,"phi-assign")),
76
+ IntegerConstant(?var_from, ?val, ?meth).
77
77
78
78
79
- % Unary-Negation: "-" is translated to "~" in OperatorAt
79
+ %% Unary-Negation: "-" is translated to "~" in OperatorAt
80
80
81
81
IntegerConstant(?var, 0 - ?val1, ?meth),
82
82
ConstFolding(?ins,?var, 0 - ?val1, ?meth) :-
@@ -94,7 +94,8 @@ ConstFolding(?ins,?var, 0 - ?val1, ?meth) :-
94
94
AssignOperFromConstant(?ins, _, ?val1).
95
95
96
96
97
- % Addition
97
+ %% Addition
98
+
98
99
IntegerConstant(?var, ?val1 + ?val2, ?meth),
99
100
ConstFolding(?ins,?var, ?val1 + ?val2, ?meth) :-
100
101
AssignBinop(?ins,_, ?var, ?meth),
@@ -127,7 +128,7 @@ ConstFolding(?ins,?var, ?val1 + ?val2, ?meth) :-
127
128
128
129
129
130
130
- % Subtraction
131
+ %% Subtraction
131
132
132
133
IntegerConstant(?var, ?val1 - ?val2, ?meth),
133
134
ConstFolding(?ins,?var, ?val1 - ?val2, ?meth) :-
@@ -170,8 +171,7 @@ ConstFolding(?ins,?var, ?val1 - ?val2, ?meth) :-
170
171
IntegerConstant(?var2, ?val2,?meth).
171
172
172
173
173
-
174
- % Multiplication
174
+ %% Multiplication
175
175
176
176
IntegerConstant(?var, ?val1 * ?val2, ?meth),
177
177
ConstFolding(?ins,?var, ?val1 * ?val2, ?meth) :-
@@ -202,7 +202,8 @@ ConstFolding(?ins,?var, ?val1 * ?val2, ?meth) :-
202
202
IntegerConstant(?var1, ?val1, ?meth),
203
203
IntegerConstant(?var2, ?val2,?meth).
204
204
205
- % Division
205
+
206
+ %% Division
206
207
207
208
IntegerConstant(?var, ?val1 / ?val2, ?meth),
208
209
ConstFolding(?ins,?var, ?val1 / ?val2, ?meth) :-
0 commit comments