1 % (c) 2009-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 :- module(static_enabling_analysis,[
6 static_cannot_enable/2,
7 static_disables_itself/1,
8 static_disabled_after/2
9 ]).
10
11 :- use_module(probsrc(module_information),[module_info/2]).
12 :- module_info(group,cbc).
13 :- module_info(description,'This module computes static enabling infos for B operations, without using the constraint solver.').
14
15 :- use_module(library(ordsets),[ord_intersect/2, ord_member/2]).
16 :- use_module(probsrc(debug), [debug_format/3]).
17 :- use_module(probsrc(bmachine),[b_top_level_operation/1]).
18 :- use_module(probsrc(b_read_write_info),
19 [b_get_read_write/3, b_get_operation_read_guard_vars/4]).
20
21 % -------------------- BINARY STATIC ANALYSIS -------------------------
22
23 % certain static analyses used by CBC Test case generation
24
25 % true when OpName1 cannot enable a previously disabled OpName2
26 static_cannot_enable(OpName1,OpName2) :-
27 static_activation_dependence(OpName1,OpName2,Res),
28 (Res \= dependent
29 -> true % activation_independent or independent
30 ; static_disabled_after(OpName1,OpName2)
31 ).
32
33 :- dynamic static_activation_dependence_cache/3.
34 static_activation_dependence(OpName1,OpName2,Res) :-
35 (var(OpName1) ; var(OpName2)),!, % enumerate the operations, so that the cache lookup below is for ground keys
36 b_top_level_operation(OpName1),
37 b_top_level_operation(OpName2),
38 static_activation_dependence(OpName1,OpName2,Res).
39 static_activation_dependence(OpName1,OpName2,Res) :-
40 static_activation_dependence_cache(OpName1,OpName2,Cached),!,
41 Res=Cached.
42 static_activation_dependence(OpName1,OpName2,Res) :-
43 b_top_level_operation(OpName1),
44 b_get_read_write(OpName1,_,Writes1),
45 b_top_level_operation(OpName2),
46 b_get_operation_read_guard_vars(OpName2,true,GuardReads2,Precise),
47 (ord_intersect(Writes1,GuardReads2)
48 -> Cached = dependent % OpName1 could enable or disable OpName2
49 ; %print(indep(OpName1,Writes1,OpName2,GuardReads2)),nl,
50 b_get_read_write(OpName2,Reads2,_Writes2),
51 (ord_intersect(Writes1,Reads2)
52 -> (Precise==precise
53 -> Cached = activation_independent % OpName1 cannot enable or disable OpName2, but can influence effect
54 ; Cached = dependent % Computation of GuardReads2 was not precise (while loops, ... in operation)
55 )
56 ; Cached = independent % but order could still matter; for this we would need to look at Writes2
57 )
58 ),
59 debug_format(19,'Computed static dependence ~w -> ~w : ~w~n',[OpName1,OpName2,Cached]),
60 assertz(static_activation_dependence_cache(OpName1,OpName2,Cached)),
61 Res=Cached.
62
63
64
65
66
67 :- use_module(probsrc(eventhandling),[register_event_listener/3]).
68 :- register_event_listener(specification_initialised,reset_static_enabling_analysis,
69 'Initialise module static_enabling analysis.').
70
71 reset_static_enabling_analysis :-
72 retractall(static_activation_dependence_cache(_,_,_)),
73 retractall(static_disabled_after_cache(_,_,_)).
74
75
76
77 :- use_module(probsrc(bmachine),[b_get_machine_operation_for_animation/4]).
78 :- use_module(probsrc(b_state_model_check), [get_guard_and_precision/3]).
79 :- use_module(probsrc(bsyntaxtree),[get_texpr_expr/2,different_texpr_values/2,select_member_in_conjunction/3]).
80
81 % check statically if an operation must disable itself
82 % the check is aimed to detect typical control flow encoding using booleans or program counters
83 % example op = SELECT op_done=FALSE THEN .... op_done := TRUE END
84 static_disables_itself(OpName) :- static_disabled_after(OpName,OpName,true).
85
86 % check statically if executing OpName1 guarantees that OpName2 is disabled after execution
87 static_disabled_after(OpName1,OpName2) :- static_disabled_after(OpName1,OpName2,true).
88
89 :- dynamic static_disabled_after_cache/3.
90 static_disabled_after(OpName1,OpName2,Res) :-
91 (var(OpName1) ; var(OpName2)),!, % enumerate the operations, so that the cache lookup below is for ground keys
92 b_top_level_operation(OpName1),
93 b_top_level_operation(OpName2),
94 static_disabled_after(OpName1,OpName2,Res).
95 static_disabled_after(OpName1,OpName2,Res) :-
96 static_disabled_after_cache(OpName1,OpName2,Cached),!,
97 Res=Cached.
98 static_disabled_after(OpName1,OpName2,Res) :-
99 b_top_level_operation(OpName1),
100 b_top_level_operation(OpName2),
101 b_get_machine_operation_for_animation(OpName1,_Results,_Parameters,TBody1),
102 b_get_read_write(OpName1,_,Writes1),
103 % identifiers in Writes1 may have a different value after OpName1 than when the RHS was evaluated
104 findall(LHS-TRHS, (get_definite_assignment(TBody1,TLHS1,TRHS), % we must assign LHS := RHS
105 norm_lhs(TLHS1,Writes1,LHS)), Assignments),
106 (Assignments = [] -> Cached=false % no need to compute the guard of OpName2
107 ; get_guard_and_precision(OpName2,Guard2,_),
108 (member(LHS-TRHS,Assignments),
109 ? select_member_in_conjunction(TC,Guard2,_RestProp),
110 get_texpr_expr(TC,C),
111 property_false_due_to_assignment(C,LHS,TRHS,Writes1) % C is in contradiction with the assignment
112 -> Cached=true
113 ; Cached=false
114 )
115 ),
116 % TODO: if OpName1 /= OpName2 we could look for guard equality in OpName1 for unmodified var
117 debug_format(19,'Computed static_disabled_after: after ~w the operation ~w is guaranteed to be disabled: ~w~n',[OpName1,OpName2,Cached]),
118 assertz(static_disabled_after_cache(OpName1,OpName2,Cached)),
119 Res=Cached.
120
121 :- use_module(library(lists)).
122 % get assignments that must definitely be executed when the operation is successful
123 ?get_definite_assignment(b(B,subst,_),LHS,RHS) :- !, get_definite_assignment(B,LHS,RHS).
124 ?get_definite_assignment(assign(LLHS,RRHS),LHS,RHS) :- !, nth1(N,LLHS,LHS), nth1(N,RRHS,RHS).
125 get_definite_assignment(assign_single_id(LHS,RHS),LHS,RHS) :- !.
126 get_definite_assignment(parallel(L),LHS,RHS) :- !, member(S,L), get_definite_assignment(S,LHS,RHS).
127 get_definite_assignment(sequence(L),LHS,RHS) :- !, last(L,S), get_definite_assignment(S,LHS,RHS).
128 get_definite_assignment(var(_,S),LHS,RHS) :- !, get_definite_assignment(S,LHS,RHS).
129 ?get_definite_assignment(precondition(_,S),LHS,RHS) :- !, get_definite_assignment(S,LHS,RHS).
130 ?get_definite_assignment(assertion(_,S),LHS,RHS) :- !, get_definite_assignment(S,LHS,RHS). % if the assertion fails we have an error
131 get_definite_assignment(rlevent(_Name,_Section,_Status,_Params,_Guard,_Thms,Actions,_VWit,_PWit,_Unmod,_AbsEvents),LHS,RHS) :- !,
132 % the concrete Event-B actions are executed in parallel; we ignore the actions of the abstract events
133 ? member(S,Actions), get_definite_assignment(S,LHS,RHS).
134 get_definite_assignment(block(S),LHS,RHS) :- !, get_definite_assignment(S,LHS,RHS).
135 get_definite_assignment(let(_,_,S),LHS,RHS) :- !, get_definite_assignment(S,LHS,RHS).
136 get_definite_assignment(select([b(select_when(_Cond,Then),_,_)]),LHS,RHS) :- !, get_definite_assignment(Then,LHS,RHS).
137 get_definite_assignment(any(_,_,S),LHS,RHS) :- !,
138 get_definite_assignment(S,LHS,RHS). % ok since different_texpr_values used above does not use quantified ids
139 get_definite_assignment(skip,_,_) :- !,fail.
140 get_definite_assignment(while(_,_,_,_),_,_) :- !,fail.
141 get_definite_assignment(choice(_),_,_) :- !,fail.
142 get_definite_assignment(becomes_element_of(_,_),_,_) :- !,fail.
143 get_definite_assignment(becomes_such(_,_),_,_) :- !,fail.
144 get_definite_assignment(operation_call(_,_,_),_,_) :- !,fail.
145 get_definite_assignment(if(_IfList),_,_) :- !, fail.
146 get_definite_assignment(external_subst_call(_,_),_,_) :- !, fail.
147 get_definite_assignment(S,_,_) :- functor(S,F,N),
148 debug_format(19,'Uncovered substitution for static_disabled_after: ~w/~w~n',[F,N]),fail.
149
150
151 :- use_module(probsrc(self_check)).
152 % test fixtures: r : struct(done:BOOL,cnt:INTEGER), y : INTEGER
153 test_texpr(r,b(identifier(r),record([field(cnt,integer),field(done,boolean)]),[])).
154 test_texpr(y,b(identifier(y),integer,[])).
155 test_texpr(r_done,b(record_field(R,done),boolean,[])) :- test_texpr(r,R).
156 test_texpr(r_cnt,b(record_field(R,cnt),integer,[])) :- test_texpr(r,R).
157 test_texpr(true,b(boolean_true,boolean,[])).
158 test_texpr(false,b(boolean_false,boolean,[])).
159
160 % r'done := TRUE falsifies r'done = FALSE and r'done /= TRUE, but not r'cnt = 0
161 :- assert_must_succeed((test_texpr(r_done,LHS),test_texpr(true,RHS),test_texpr(false,F),
162 norm_lhs(LHS,[r],NLHS), property_false_due_to_assignment(equal(LHS,F),NLHS,RHS,[r]))).
163 :- assert_must_succeed((test_texpr(r_done,LHS),test_texpr(true,RHS),
164 norm_lhs(LHS,[r],NLHS), property_false_due_to_assignment(not_equal(RHS,LHS),NLHS,RHS,[r]))).
165 :- assert_must_fail((test_texpr(r_done,LHS),test_texpr(true,RHS),test_texpr(r_cnt,C),
166 norm_lhs(LHS,[r],NLHS), property_false_due_to_assignment(equal(C,b(integer(0),integer,[])),NLHS,RHS,[r]))).
167 % y := r'cnt falsifies y /= r'cnt, unless the operation also writes r
168 :- assert_must_succeed((test_texpr(y,LHS),test_texpr(r_cnt,RHS),
169 norm_lhs(LHS,[y],NLHS), property_false_due_to_assignment(not_equal(LHS,RHS),NLHS,RHS,[y]))).
170 :- assert_must_fail((test_texpr(y,LHS),test_texpr(r_cnt,RHS),
171 norm_lhs(LHS,[r,y],NLHS), property_false_due_to_assignment(not_equal(LHS,RHS),NLHS,RHS,[r,y]))).
172
173 % definite assignments inside Event-B events and ASSERT substitutions
174 :- assert_must_succeed((test_texpr(y,Y),test_texpr(r_cnt,C),A=b(assign([Y],[C]),subst,[]),
175 findall(L,get_definite_assignment(rlevent(ev,sec,ordinary,[],b(truth,pred,[]),[],[A],[],[],[],[]),L,_),Ls),
176 Ls==[Y])).
177 :- assert_must_succeed((test_texpr(y,Y),test_texpr(r_cnt,C),A=b(assign([Y],[C]),subst,[]),
178 get_definite_assignment(assertion(b(truth,pred,[]),A),L,R), L==Y, R==C)).
179
180 % Writes: the identifiers written by the operation performing the assignment;
181 % the value of such an identifier after the operation can differ from its value when the RHS was evaluated
182 property_false_due_to_assignment(equal(TA,TB),NLHS,TRHS,Writes) :-
183 norm_lhs(TA,Writes,NLHS),different_texpr_values(TRHS,TB).
184 property_false_due_to_assignment(equal(TB,TA),NLHS,TRHS,Writes) :-
185 norm_lhs(TA,Writes,NLHS),different_texpr_values(TRHS,TB).
186 property_false_due_to_assignment(not_equal(TA,TB),NLHS,TRHS,Writes) :-
187 norm_lhs(TA,Writes,NLHS), guaranteed_same_value(TRHS,TB,Writes).
188 property_false_due_to_assignment(not_equal(TB,TA),NLHS,TRHS,Writes) :-
189 norm_lhs(TA,Writes,NLHS), guaranteed_same_value(TRHS,TB,Writes).
190 % TODO: greater, ... for integer values
191
192 guaranteed_same_value(TA,TB,Writes) :- norm_arg(TA,Writes,Val), norm_arg(TB,Writes,Val).
193
194 % only accept Identifier, F(Arg) or R'Field (with F, R again of this form),
195 % where Arg contains no identifier written by the operation (in Writes)
196 norm_lhs(b(E,_,_),Writes,NE) :- !, norm_lhs(E,Writes,NE).
197 norm_lhs(identifier(ID),_,ID) :- !.
198 norm_lhs(function(TF,TA),Writes,function(NF,NA)) :- !,
199 norm_lhs(TF,Writes,NF), norm_arg(TA,Writes,NA).
200 norm_lhs(record_field(TR,Field),Writes,record_field(NR,Field)) :- !, % record field assignment r'Field := ...
201 norm_lhs(TR,Writes,NR).
202
203 % normalise an argument; it must denote the same value before and after the operation with writes Writes
204 norm_arg(b(E,_,_),Writes,NE) :- !, norm_arg(E,Writes,NE).
205 norm_arg(integer(Nr),_,NE) :- !,NE=Nr.
206 norm_arg(identifier(ID),Writes,NE) :- !, \+ ord_member(ID,Writes), NE=identifier(ID).
207 norm_arg(empty_sequence,_,N) :- !, N=empty_set.
208 norm_arg(couple(A,B),Writes,couple(NA,NB)) :- !, norm_arg(A,Writes,NA), norm_arg(B,Writes,NB).
209 norm_arg(record_field(R,Field),Writes,record_field(NR,Field)) :- !, norm_arg(R,Writes,NR).
210 % TODO: records, ...
211 norm_arg(X,_,N) :- norm_literal(X),!,N=X.
212 %norm_arg(S,_) :- functor(S,F,N),write(uncov_norm_arg(F,N)),nl,fail.
213
214 norm_literal(string(_)).
215 norm_literal(real(_)).
216 norm_literal(boolean_true).
217 norm_literal(boolean_false).
218 norm_literal(empty_set).
219
220