-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc_printer.c
442 lines (366 loc) · 13.1 KB
/
c_printer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* This file contains the function required to
print a Büchi automaton in C, and the analysis function
to determine the stutter acceptance from a give state.
*/
#include "ltl2ba.h"
extern FILE* tl_out;
extern int accept;
extern BState *bstates;
extern int sym_id, sym_size, mod;
extern char** sym_table;
const char* assume_str = "__ESBMC_assume";
const char* assert_str = "__ESBMC_assert";
const char* nondet_str = "nondet_uint";
int n_ba_state; /* Number of states in the ba */
BScc *scc_stack; /* Stack used for automaton explorations */
_Bool *stutter_acceptance_table;
/* Count the number of state in a BA */
int
count_ba_states() {
BState *s;
int n = 0;
for (s = bstates->prv; s != bstates; s = s->prv)
n++;
return n;
}
/* Return true if the transition t can be used with
a valuation prop_state of the atomic propositions
*/
_Bool
is_transition_valid(BTrans *t, int *prop_state) {
int i;
for (i = 0; i < sym_size; i++) {
if ((t->pos[i] & prop_state[i]) != t->pos[i])
return 0;
if ((t->neg[i] & ~prop_state[i]) != t->neg[i])
return 0;
}
return 1;
}
/* Determine whether the stutter extension of a word is accepted,
given a state and a final program state (i.e a predicate valuation).
WARNING : Use of the incoming field, does not anymore represent the scc...
Explore the automaton using only the transition compatibles with the final program state.
When a cycle containing a final state is found, mark all states leading to it
as stutter accepting.
*/
void
stutter_acceptance_state(BState *s, int *stutter_state) {
BTrans *t;
BScc *c;
BScc *scc = (BScc *)tl_emalloc(sizeof(BScc));
/* Mark the state and add it in the stack */
s->incoming = 1;
scc->bstate = s;
scc->nxt = scc_stack;
/* Invariant : It is possible to reach the current state from
every state on the stack */
scc_stack = scc;
/* Visit every successors reachable with the program state */
for (t = s->trans->nxt; t != s->trans; t = t->nxt) {
if (! is_transition_valid(t, stutter_state))
continue;
/* The successor have not been reached already */
if (t->to->incoming == 0) {
stutter_acceptance_state(t->to, stutter_state);
/* If the successor is stutter-accepted */
if (t->to->incoming == 3) {
s->incoming = 3;
scc_stack = scc_stack->nxt;
return;
}
/* The successor is currently being visited : we found a cycle */
} else if (t->to->incoming == 1) {
/* Search for a final state in the cycle */
_Bool final_cycle = 0;
for(c = scc_stack; c != 0; c = c->nxt) {
if (c->bstate->final == accept || c->bstate->id == 0)
final_cycle = 1;
if (c->bstate == t->to)
break;
}
/* If there is final state in the cycle, all the state in the cycle are
accepted. We mark the current one, others will be marked recursively */
if (final_cycle) {
s->incoming = 3;
scc_stack = scc_stack->nxt;
return;
}
/* The successor has already been visited */
} else {
/* If the successor lead to a final cycle, marks all the state as accepting */
if (t->to->incoming == 3) {
s->incoming = 3;
scc_stack = scc_stack->nxt;
return;
}
}
}
/* If no final cycle have been found, there is none from this state */
if (s->incoming == 1) {
s->incoming = 2;
scc_stack = scc_stack->nxt;
}
}
/* Compute the stutter acceptance for all automaton state and all final
program state */
void
stutter_acceptance() {
BState *s;
scc_stack = 0;
int i, k;
int stutter_state[sym_size];
if (sym_size > 1)
fatal("c_printer, stutter_acceptance", "sym_size > 1 : too many states for an exploration");
if(bstates == bstates->nxt)
return;
for (k = 0; k < (1 << sym_id); k++) {
stutter_state[0] = k;
/* Unmark all states */
for (s = bstates->nxt; s != bstates; s = s->nxt)
s->incoming = 0;
/* Explore the graph from every state */
for (s = bstates->nxt; s != bstates; s = s->nxt) {
if (s->incoming == 0)
stutter_acceptance_state(s, stutter_state);
}
/* Collect the results */
for (s = bstates->nxt, i=0; s != bstates; s = s->nxt, i++) {
if (s->incoming == 3)
stutter_acceptance_table[k * n_ba_state + i] = 1;
else
stutter_acceptance_table[k * n_ba_state + i] = 0;
}
}
}
/* Print the condition of a transition */
void
c_print_set(int* pos, int* neg) {
int i, j, start = 1;
for(i = 0; i < sym_size; i++)
for(j = 0; j < mod; j++) {
if(pos[i] & (1 << j)) {
if(!start)
fprintf(tl_out, " && ");
fprintf(tl_out, "_ltl2ba_atomic_%s", sym_table[mod * i + j]);
start = 0;
}
if(neg[i] & (1 << j)) {
if(!start)
fprintf(tl_out, " && ");
fprintf(tl_out, "!_ltl2ba_atomic_%s", sym_table[mod * i + j]);
start = 0;
}
}
if(start)
fprintf(tl_out, "1");
}
/* Print variables for each atomic predicate */
void
print_c_atomics_definition() {
int i;
for (i = 0; i < sym_id; i++) {
fprintf(tl_out, "_Bool _ltl2ba_atomic_%s = 0;\n", sym_table[i]);
}
fprintf(tl_out, "\n");
}
/* Print an enumeration containing the ba states */
void
print_c_states_definition() {
BState *s;
fprintf(tl_out, "typedef enum {\n");
for (s = bstates->prv; s != bstates; s = s->prv)
fprintf(tl_out, "\t_ltl2ba_state_%i_%i,\n", s->id + 1, s->final);
fprintf(tl_out, "} _ltl2ba_state;\n\n");
}
/* Print the transition function of the ba */
void
print_c_transition_function() {
BState *s;
BTrans *t;
fprintf(tl_out, "void\n_ltl2ba_transition() {\n");
/* If the automaton is empty (no states) */
if (bstates->nxt == bstates) {
fprintf(tl_out, "\t%s(0);\n}\n", assume_str);
return;
}
fprintf(tl_out, "\tint choice = %s();\n", nondet_str);
fprintf(tl_out, "\tswitch (_ltl2ba_state_var) {\n");
for(s = bstates->prv; s != bstates; s = s->prv) {
fprintf(tl_out, "\tcase _ltl2ba_state_%i_%i:\n", s->id + 1, s->final);
/* The state of id 0 is an accepting well.
Every word will be accepted and the state will no longer change
*/
if(s->id == 0) {
fprintf(tl_out, "\t\t%s(0, \"Error sure\");\n", assert_str);
fprintf(tl_out, "\t\tbreak;\n");
continue;
}
/* If there is no transition from this state */
t = s->trans->nxt;
if(t == s->trans) {
fprintf(tl_out, "\t\t%s(0);\n", assume_str);
continue;
}
/* First transition from the current state */
fprintf(tl_out, "\t\tif (choice == 0) {\n");
fprintf(tl_out, "\t\t\t%s(", assume_str);
c_print_set(t->pos, t->neg);
fprintf(tl_out, ");\n");
fprintf(tl_out, "\t\t\t_ltl2ba_state_var = _ltl2ba_state_%i_%i;\n",
t->to->id + 1, t->to->final);
fprintf(tl_out, "\t\t}");
/* Other transition from the current state */
int trans_num;
for(trans_num = 1, t = s->trans->nxt->nxt; t != s->trans; t = t->nxt, trans_num++) {
fprintf(tl_out, " else if (choice == %i) {\n", trans_num);
fprintf(tl_out, "\t\t\t%s(", assume_str);
c_print_set(t->pos, t->neg);
fprintf(tl_out, ");\n");
fprintf(tl_out, "\t\t\t_ltl2ba_state_var = _ltl2ba_state_%i_%i;\n",
t->to->id + 1, t->to->final);
fprintf(tl_out, "\t\t}");
}
/* Prune other choices */
fprintf(tl_out, " else {\n");
fprintf(tl_out, "\t\t\t%s(0);\n", assume_str);
fprintf(tl_out, "\t\t}");
fprintf(tl_out, "\n\t\tbreak;\n");
}
fprintf(tl_out, "\t}\n}\n\n");
}
/* Print the table indicating if from the current state,
every word will be accepted (whatever the suffix)
*/
void
print_c_surely_accept_state_table() {
BState *s;
fprintf(tl_out, "_Bool _ltl2ba_surely_accept[%i] = {", n_ba_state);
/* No states in the ba */
if (bstates->prv == bstates) {
fprintf(tl_out, "};\n");
return;
}
/* First state */
s = bstates->prv;
if (s->id == 0)
fprintf(tl_out, "1");
else
fprintf(tl_out, "0");
/* Following states */
for (s = s->prv; s != bstates; s = s->prv) {
/* TODO : is this true ??? It is the case only if the automaton is in reduced form... */
/* As the automaton is in reduced form, only an accepting well
will accept every words whatever the suffix.
Accepting well have an id = 0 */
if (s->id == 0)
fprintf(tl_out, ", 1");
else
fprintf(tl_out, ", 0");
}
fprintf(tl_out, "};\n");
}
/* Print the table indicating if from the current state,
every word will be rejected (whatever the suffix)
*/
void
print_c_surely_reject_state_table() {
BState *s;
fprintf(tl_out, "_Bool _ltl2ba_surely_reject[%i] = {", n_ba_state);
/* No states in the ba */
if (bstates->prv == bstates) {
fprintf(tl_out, "};\n");
return;
}
/* First state */
s = bstates->prv;
fprintf(tl_out, "0");
/* Following states */
for (s = s->prv; s != bstates; s = s->prv) {
/* TODO : is this true ??? It is the case only if the automaton is in reduced form... */
/* As the automaton is in reduced form,
no state will reject every suffix (elsewhere, this state would have been removed
from the automaton) */
fprintf(tl_out, ", 0");
}
fprintf(tl_out, "};\n");
}
void
print_c_stutter_acceptance_table() {
BState *s;
int i, k;
fprintf(tl_out, "_Bool _ltl2ba_stutter_accept[%i] = {",
n_ba_state * (1 << sym_id));
/* Other states */
for (k = 0; k < (1 << sym_id); k++) {
fprintf(tl_out, "\n\t");
for (s = bstates->prv, i = n_ba_state - 1; s != bstates; s = s->prv, i--)
fprintf(tl_out, "%i,", stutter_acceptance_table[k * n_ba_state + i]);
}
fprintf(tl_out, "\n};\n");
}
/* Print a C function that build a program state id from the value of atomic predicates */
void print_c_sym_to_id_function() {
int i;
fprintf(tl_out, "unsigned int\n");
fprintf(tl_out, "_ltl2ba_sym_to_id() {\n");
fprintf(tl_out, "\tunsigned int id = 0;\n\n");
for (i = 0; i < sym_id; i++) {
fprintf(tl_out, "\tid |= (_ltl2ba_atomic_%s << %i);\n", sym_table[i], i);
}
fprintf(tl_out, "\treturn id;\n");
fprintf(tl_out, "};\n\n");
}
void
print_c_conclusion_function() {
fprintf(tl_out, "void\n");
fprintf(tl_out, "_ltl2ba_result() {\n");
fprintf(tl_out, "\t_Bool reject_sure = _ltl2ba_surely_reject[_ltl2ba_state_var];\n");
fprintf(tl_out, "\t%s(!reject_sure);\n\n", assume_str);
fprintf(tl_out, "\t_Bool accept_sure = _ltl2ba_surely_accept[_ltl2ba_state_var];\n");
fprintf(tl_out, "\t%s(!accept_sure, \"ERROR SURE\");\n\n", assert_str);
fprintf(tl_out, "\tunsigned int id = _ltl2ba_sym_to_id();\n");
fprintf(tl_out,
"\t_Bool accept_stutter = _ltl2ba_stutter_accept[id * %i + _ltl2ba_state_var];\n",
n_ba_state);
fprintf(tl_out, "\t%s(!accept_stutter, \"ERROR MAYBE\");\n", assert_str);
fprintf(tl_out, "\t%s(accept_stutter, \"VALID MAYBE\");\n", assert_str);
fprintf(tl_out, "}\n\n");
}
void
print_c_buchi() {
n_ba_state = count_ba_states();
stutter_acceptance_table = (_Bool *)tl_emalloc(n_ba_state * (1 << sym_id) * sizeof(_Bool));
stutter_acceptance();
fprintf(tl_out, "/* ");
put_uform();
fprintf(tl_out, " */\n\n");
print_c_atomics_definition();
fprintf(tl_out, "\n");
print_c_states_definition();
/* Declare and initialize the global variable that will maintain the state
of the automaton.
The initial state has always an id of -1 (+1 in the name).
*/
BState *s;
for(s = bstates->prv; s != bstates; s = s->prv) {
if (s->id == -1) {
fprintf(tl_out, "_ltl2ba_state _ltl2ba_state_var = _ltl2ba_state_0_%d;\n\n", s->final);
break;
}
}
print_c_transition_function();
/* TODO: Surely accepting states are currently built under the assumption
the automaton is in reduced form
*/
print_c_surely_accept_state_table();
/* TODO: Surely rejecting states are currently built under the assumption
the automaton is in reduced form
*/
print_c_surely_reject_state_table();
/* TODO: Under the assumption there is no more than sizeof(int)*8 symbols */
print_c_stutter_acceptance_table();
/* Print the conclusion function */
print_c_sym_to_id_function();
print_c_conclusion_function();
}