dvecompile.cpp 122 KB
Newer Older
Alfons Laarman's avatar
Alfons Laarman committed
1
#include <tools/compile.h>
Elwin Pater's avatar
Elwin Pater committed
2
3
4
#include <tools/dvecompile.h>
#include <divine/generator/common.h>

5
6
#include <string>
#include <iostream>
Alfons Laarman's avatar
Alfons Laarman committed
7
#include <iomanip>
8

Elwin Pater's avatar
Elwin Pater committed
9
10
using namespace wibble::str;

Alfons Laarman's avatar
Alfons Laarman committed
11
namespace divine {
12

Alfons Laarman's avatar
Alfons Laarman committed
13
14
15
16
17
18
19
20
const char *compile_defines_str = "\
#define assert_eq(a,b) assert(a == b)\n\
#define assert_neq(a,b) assert(a != b);\n\
#define assert_leq(a,b) assert(a <= b);\n\
#define assert_die() assert(false);\n\
#define BLOB_NO_HASH\n";
}

21
static const char * get_op(int op) {
Elwin Pater's avatar
Elwin Pater committed
22

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
	switch(op) {
		case T_LT: return "<";
		case T_LEQ: return "<=";
		case T_EQ: return "==";
		case T_NEQ: return "!=";
		case T_GT: return ">";
		case T_GEQ: return ">=";
		case T_PLUS: return "+";
		case T_MINUS: return "-";
		case T_MULT: return "*";
		case T_DIV: return "/";
		case T_MOD: return "%";
		case T_AND: return "&";
		case T_OR: return "|";
		case T_XOR: return "^";
		case T_LSHIFT: return "<<";
		case T_RSHIFT: return ">>";
		case T_BOOL_AND: return "&&";
		case T_BOOL_OR: return "||";
		case T_ASSIGNMENT: return "=";
	}
	return "";
}
Elwin Pater's avatar
Elwin Pater committed
46

47
void dve_compiler::write_C(dve_expression_t & expr, std::ostream & ostr, std::string state_name, bool wrap)
48
{
Elwin Pater's avatar
Elwin Pater committed
49
50
51
52
53
    dve_symbol_table_t * parent_table = expr.get_symbol_table();
    if (!parent_table) gerr << "Writing expression: Symbol table not set" << thr();
    switch (expr.get_operator())
    {
        case T_ID:
Alfons Laarman's avatar
Alfons Laarman committed
54
55
            if (!(parent_table->get_variable(expr.get_ident_gid())->is_const()))
                ostr<<state_name<<".";
Elwin Pater's avatar
Elwin Pater committed
56
57
58
59
60
61
62
            if(parent_table->get_variable(expr.get_ident_gid())->get_process_gid() != NO_ID)
            {
                ostr << parent_table->get_process(parent_table->get_variable(expr.get_ident_gid())->
                                                  get_process_gid())->get_name(); //name of process
                ostr<<".";
            }
            ostr << parent_table->get_variable(expr.get_ident_gid())->get_name();
Michael Weber's avatar
Michael Weber committed
63
            if (ltsmin) ostr << ".var";
Elwin Pater's avatar
Elwin Pater committed
64
65
            break;
        case T_FOREIGN_ID:
66
            ostr <<state_name<<"."<< parent_table->get_process(parent_table->get_variable(expr.get_ident_gid())->
Elwin Pater's avatar
Elwin Pater committed
67
                                              get_process_gid())->get_name(); //name of process
68
            ostr<<".";
Elwin Pater's avatar
Elwin Pater committed
69
            ostr << parent_table->get_variable(expr.get_ident_gid())->get_name();
Michael Weber's avatar
Michael Weber committed
70
            if (ltsmin) ostr << ".var";
Elwin Pater's avatar
Elwin Pater committed
71
72
73
74
75
76
            break;
        case T_NAT:
            ostr << expr.get_value();
            break;
        case T_PARENTHESIS:
            ostr << "(";
77
            write_C(*expr.left(), ostr, state_name, wrap);
Elwin Pater's avatar
Elwin Pater committed
78
79
80
            ostr << ")";
            break;
        case T_SQUARE_BRACKETS:
Alfons Laarman's avatar
Alfons Laarman committed
81
82
            if (!(parent_table->get_variable(expr.get_ident_gid())->is_const()))
                ostr<<state_name<<".";
Elwin Pater's avatar
Elwin Pater committed
83
84
85
86
87
88
            if(parent_table->get_variable(expr.get_ident_gid())->get_process_gid() != NO_ID)
            {
                ostr << parent_table->get_process(parent_table->get_variable(expr.get_ident_gid())->
                                                  get_process_gid())->get_name(); //name of process
                ostr<<".";
            }
89
90
91
92
93
94
            ostr << parent_table->get_variable(expr.get_ident_gid())->get_name();
            ostr<<"[";
            if (wrap) ostr << "wrapped_index(";
            write_C(*expr.left(), ostr, state_name, wrap);
            if (wrap) ostr << ", " << parent_table->get_variable(expr.get_ident_gid())->get_vector_size() << ", &jbuf)";
            ostr<<"]" ;
Michael Weber's avatar
Michael Weber committed
95
            if (ltsmin) ostr << ".var";
Elwin Pater's avatar
Elwin Pater committed
96
97
98
99
100
101
            break;
        case T_FOREIGN_SQUARE_BRACKETS:
            ostr << parent_table->get_process(parent_table->get_variable(expr.get_ident_gid())->
                                              get_process_gid())->get_name(); //name of preocess
            ostr<<"->";
            ostr << parent_table->get_variable(expr.get_ident_gid())->get_name();
102
103
104
105
106
            ostr<<"[";
            if (wrap) ostr << "wrapped_index(";
            write_C(*expr.left(), ostr, state_name, wrap);
            if (wrap) ostr << ", " << parent_table->get_variable(expr.get_ident_gid())->get_vector_size() << ", &jbuf)";
            ostr<<"]";
Michael Weber's avatar
Michael Weber committed
107
            if (ltsmin) ostr << ".var";
Elwin Pater's avatar
Elwin Pater committed
108
109
110
            break;

        case T_LT: case T_LEQ: case T_EQ: case T_NEQ: case T_GT: case T_GEQ:
111
        case T_PLUS: case T_MINUS: case T_MULT:
Elwin Pater's avatar
Elwin Pater committed
112
113
        case T_AND: case T_OR: case T_XOR: case T_LSHIFT: case T_RSHIFT:
        case T_BOOL_AND: case T_BOOL_OR: case T_ASSIGNMENT:
114
115
116
117
118
119
120
            write_C( *expr.left(), ostr, state_name, wrap );
            ostr << " " << get_op(expr.get_operator()) << " ";
            write_C( *expr.right(), ostr, state_name, wrap );
            break;
        case T_MOD:
        case T_DIV:
            write_C( *expr.left(), ostr, state_name, wrap );
121
            ostr << " " << get_op(expr.get_operator()) << " ";
122
123
124
            if (wrap) ostr << "wrapped_div(";
            write_C( *expr.right(), ostr, state_name, wrap );
            if (wrap) ostr << ", &jbuf)";
Elwin Pater's avatar
Elwin Pater committed
125
126
127
            break;

        case T_DOT:
128
129
130
            ostr << in_state(
                parent_table->get_state(expr.get_ident_gid())->get_process_gid(), 
                parent_table->get_state(expr.get_ident_gid())->get_lid(), state_name );
Elwin Pater's avatar
Elwin Pater committed
131
132
133
            break;

        case T_IMPLY:
134
            write_C(*expr.left(), ostr, state_name, wrap);
Elwin Pater's avatar
Elwin Pater committed
135
            ostr<<" -> "; // FIXME this looks wrong, -> in C is dereference
136
            write_C(*expr.right(), ostr, state_name, wrap);
Elwin Pater's avatar
Elwin Pater committed
137
138
139
            break;
        case T_UNARY_MINUS:
            ostr<<"-";
140
            write_C(*expr.right(), ostr, state_name, wrap);
Elwin Pater's avatar
Elwin Pater committed
141
142
143
            break;
        case T_TILDE:
            ostr<<"~";
144
            write_C(*expr.right(), ostr, state_name, wrap);
Elwin Pater's avatar
Elwin Pater committed
145
146
147
            break;
        case T_BOOL_NOT:
            ostr<<" ! (";
148
            write_C(*expr.right(), ostr, state_name, wrap);
Elwin Pater's avatar
Elwin Pater committed
149
150
151
152
153
154
155
156
            ostr<< " )";
            break;
        default:
            gerr << "Problem in expression - unknown operator"
                 << " number " << expr.get_operator() << psh();
    }
}

157
std::string dve_compiler::cexpr( dve_expression_t & expr, std::string state, bool wrap )
Elwin Pater's avatar
Elwin Pater committed
158
159
160
{
    std::stringstream str;
    str << "(";
161
    write_C( expr, str, state.c_str(), wrap );
Elwin Pater's avatar
Elwin Pater committed
162
163
164
165
166
167
    str << ")";
    return str.str();
}

void dve_compiler::gen_header()
{
Jeroen Meijer's avatar
Jeroen Meijer committed
168
    line( "#include <stdlib.h>" );
Elwin Pater's avatar
Elwin Pater committed
169
170
171
    line( "#include <stdio.h>" );
    line( "#include <string.h>" );
    line( "#include <stdint.h>" );
172
    line( "#include <setjmp.h>" );
Elwin Pater's avatar
Elwin Pater committed
173
    line();
Jeroen Meijer's avatar
Jeroen Meijer committed
174
175
    line( "#pragma clang diagnostic ignored \"-Wparentheses-equality\"" );
    line();
Elwin Pater's avatar
Elwin Pater committed
176

Michael Weber's avatar
Michael Weber committed
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
    if (ltsmin) {
        // note: everything is 32 bit, this introduces a bug
        // for example when byte value should wrap, now it doesn't
        // thus it should be a 32 bit aligned byte instead of a 32 bit int
        line( "typedef uint64_t ulong_long_int_t;" );
        line( "typedef int64_t slong_long_int_t;" );
        line( "typedef uint32_t ulong_int_t;" );
        line( "typedef int32_t slong_int_t;" );
        line( "typedef union" );
        line( "{" );
        line( "    uint16_t var;" );
        line( "    uint32_t __padding__;" );
        line( "} ushort_int_t;" );
        line( "typedef union" );
        line( "{" );
        line( "    int16_t var;" );
        line( "    uint32_t __padding__;" );
        line( "} sshort_int_t;" );
        line( "typedef union" );
        line( "{" );
        line( "    uint8_t var;" );
        line( "    uint32_t __padding__;" );
        line( "} ubyte_t;" );
        line( "typedef ubyte_t byte_t;" );
        line( "typedef union" );
        line( "{" );
        line( "    int8_t var;" );
        line( "    uint32_t __padding__;" );
        line( "} sbyte_t;" );
        line( "typedef size_t size_int_t;" );
        line();
        line( "typedef struct transition_info" );
        line( "{" );
        line( "    int* label;" );
        line( "    int  group;" );
212
        line( "    int  por_proviso;" );
Michael Weber's avatar
Michael Weber committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
        line( "} transition_info_t;" );
        line();
    } else {
        line( "typedef uint64_t ulong_long_int_t;" );
        line( "typedef int64_t slong_long_int_t;" );
        line( "typedef uint32_t ulong_int_t;" );
        line( "typedef int32_t slong_int_t;" );
        line( "typedef uint16_t ushort_int_t;" );
        line( "typedef int16_t sshort_int_t;" );
        line( "typedef uint8_t byte_t;" );
        line( "typedef uint8_t ubyte_t;" );
        line( "typedef int8_t sbyte_t;" );
        line( "typedef size_t size_int_t;" );
        line();
Michael Weber's avatar
Michael Weber committed
227
        line( compile_defines_str );
Michael Weber's avatar
Michael Weber committed
228
229
230
231
232
233
234
235
236
237
238
        line();
        line( divine::pool_h_str );
        line();
        line( divine::circular_h_str );
        line();
        line( divine::blob_h_str );
        line();
        line( "using namespace divine;" );
        line( divine::generator_custom_api_h_str );
        line();
    }
Elwin Pater's avatar
Elwin Pater committed
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
}

void dve_compiler::gen_state_struct()
{
    for (size_int_t i=0; i!=glob_var_count; i++)
    {
        dve_symbol_t * var = get_symbol_table()->get_variable(get_global_variable_gid(i));
        if (var->is_const())
        {
            append( "const " );
            if ( var->is_byte() )
                append( "byte_t " );
            else
                append( "sshort_int_t " );

            append( var->get_name() );

            if (var->is_vector())
            {
                append( "[" + fmt( var->get_vector_size() ) + "]" );

                if ( var->get_init_expr_count() ) append( " = {" );
                for (size_int_t j=0; j!=var->get_init_expr_count(); j++)
                {
Michael Weber's avatar
Michael Weber committed
263
                    if (ltsmin) append("{");
264
                    append( cexpr( *((dve_expression_t*)var->get_init_expr(j)), "", false) );
Michael Weber's avatar
Michael Weber committed
265
                    if (ltsmin) append("}");
Elwin Pater's avatar
Elwin Pater committed
266
267
268
269
270
271
                    if (j!=(var->get_init_expr_count()-1))
                        append( ", " );
                    else
                        append( "}" );
                }
            } else if ( var->get_init_expr() ) {
Michael Weber's avatar
Michael Weber committed
272
273
                append( string( " = " ) );
                if (ltsmin) append(" {");
274
                append( cexpr( *((dve_expression_t*) var->get_init_expr()), "", false) );
Michael Weber's avatar
Michael Weber committed
275
                if (ltsmin) append("}");
Elwin Pater's avatar
Elwin Pater committed
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
            }
            line( ";" );
        }
    }
    line();

    bool global = true;
    string name;
    string process_name = "UNINITIALIZED";
    line( "struct state_struct_t" );
    block_begin();
    for (size_int_t i=0; i!=state_creators_count; ++i)
    {
        switch (state_creators[i].type)
        {
            case state_creator_t::VARIABLE:
            {
                name=get_symbol_table()->get_variable(state_creators[i].gid)->get_name();
                if (state_creators[i].array_size)
                {
                    if (state_creators[i].var_type==VAR_BYTE)
                        append( "byte_t " );
                    else if (state_creators[i].var_type==VAR_INT)
                        append( "sshort_int_t " );
Michael Weber's avatar
Michael Weber committed
300
                    else gerr << "Unexpected error generating state struct array" << thr();
Elwin Pater's avatar
Elwin Pater committed
301
302
303
304
305
306
307
                    line( name + "[" + fmt( state_creators[i].array_size ) + "];" );
                }
                else
                {
                    if (state_creators[i].var_type==VAR_BYTE)
                        line( "byte_t " + name + ";" );
                    else if (state_creators[i].var_type==VAR_INT)
Alfons Laarman's avatar
Alfons Laarman committed
308
                        line( "sshort_int_t " + name + ";" );
Elwin Pater's avatar
Elwin Pater committed
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
                    else gerr << "Unexpected error" << thr();
                }
            }
            break;
            case state_creator_t::PROCESS_STATE:
            {
                if (global)
                {
                    global = false;
                }
                else
                {
                    block_end();
                    line( "__attribute__((__packed__)) " + process_name + ";" );
                }
                line( "struct" );
                block_begin();

                process_name=
                    get_symbol_table()->get_process(state_creators[i].gid)->get_name();
                line( "ushort_int_t state;" );
            }
            break;
            case state_creator_t::CHANNEL_BUFFER:
            {
                name=get_symbol_table()->get_channel(state_creators[i].gid)->get_name();
                line( "struct" );
                block_begin();
                line( "ushort_int_t number_of_items;" );
                line( "struct" );
                block_begin();
                dve_symbol_t * symbol =
                    get_symbol_table()->get_channel(state_creators[i].gid);
                size_int_t item_count = symbol->get_channel_type_list_size();

                for (size_int_t j=0; j<item_count; ++j)
                    if (symbol->get_channel_type_list_item(j)==VAR_BYTE)
                        line( "byte_t x" + fmt( j ) + ";" );
                    else if (symbol->get_channel_type_list_item(j)==VAR_INT)
                        line( "sshort_int_t x" + fmt( j ) + ";" );
Michael Weber's avatar
Michael Weber committed
349
                    else gerr << "Unexpected error generating state struc channel" << thr();
Elwin Pater's avatar
Elwin Pater committed
350
351
352
353
354
355
                block_end();
                line( "content[" + fmt( symbol->get_channel_buffer_size() ) + "];" );
                block_end();
                line( "__attribute__((__packed__)) " + name + ";" );
            }
            break;
Michael Weber's avatar
Michael Weber committed
356
            default: gerr << "Unexpected error generating state struct" << thr();
Elwin Pater's avatar
Elwin Pater committed
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
                break;
        };
    }
    if (!global)
    {
        block_end();
        line( "__attribute__((__packed__)) " + process_name + ";" );
    }
    block_end();
    line( "__attribute__((__packed__));" );

    line( "int state_size = sizeof(state_struct_t);" );
    line();
}


void dve_compiler::gen_initial_state()
{
Michael Weber's avatar
Michael Weber committed
375
376
377
378
379
380
381
382
383
384
385
386
387
388
    if (!ltsmin) {
        setAllocator( new generator::Allocator );
        state_t initial_state =  dve_explicit_system_t::get_initial_state();
        append( "char initial_state[] = {" );
        for(int i = 0; i < initial_state.size; i++)
        {
            append( fmt( (unsigned int)(unsigned char)initial_state.ptr[i] ) );
            if(i != initial_state.size - 1)
                append( ", " );
        }
        line( "};" );
        line();
    } else {

389
390
        std::string sep = "";
        std::string buf;
Michael Weber's avatar
Michael Weber committed
391
392
393
394
395
396
397
398
399
400
401
        append( "state_struct_t initial_state = { " );
        for (size_int_t i=0; i!=state_creators_count; ++i)
        {
            switch (state_creators[i].type)
            {
                case state_creator_t::VARIABLE:
                {
                    if (state_creators[i].array_size)
                    {
                         for(size_int_t j=0; j<state_creators[i].array_size; j++)
                         {
402
403
                            append(sep); sep = ",";
                            buf = fmtf("%d", (initial_values_counts[state_creators[i].gid]?
Michael Weber's avatar
Michael Weber committed
404
405
406
407
408
409
                                                initial_values[state_creators[i].gid].all_values[j]:0));
                            append(buf);
                         }
                    }
                    else
                    {
410
411
                            append(sep); sep = ",";
                            buf = fmtf("%d", (initial_values_counts[state_creators[i].gid]?
Michael Weber's avatar
Michael Weber committed
412
413
414
415
416
417
418
                                                initial_values[state_creators[i].gid].all_value:0));
                            append(buf);
                    }
                }
                break;
                case state_creator_t::PROCESS_STATE:
                {
419
420
                    append(sep); sep = ",";
                    buf = fmtf("%zu", initial_states[state_creators[i].gid]);
Michael Weber's avatar
Michael Weber committed
421
422
423
424
425
426
                    append(buf);
                }
                break;
                case state_creator_t::CHANNEL_BUFFER:
                {
                    // initialize channel to 0
427
                    append(sep); sep = ",";
Michael Weber's avatar
Michael Weber committed
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
                    append("0"); // number_of_items 

                    dve_symbol_t * symbol =
                        get_symbol_table()->get_channel(state_creators[i].gid);
                    size_int_t item_count = symbol->get_channel_type_list_size();
                    size_int_t chan_size = symbol->get_channel_buffer_size();
                    for(size_int_t i=0; i < chan_size; ++i) {
                        for (size_int_t j=0; j<item_count; ++j) {
                            append(sep);
                            append("0");
                        }
                    }
                }
                break;
                default: gerr << "Unexpected error generating initial state" << thr();
                    break;
            };
        }
        line( " };" );
        line();
    }
}

Jeroen Meijer's avatar
Jeroen Meijer committed
451
void dve_compiler::output_dependency_comment( ext_transition_t &ext_transition, bool condition )
Michael Weber's avatar
Michael Weber committed
452
453
454
455
456
457
{
    // only for ltsmin
    if (!ltsmin)
        return;

    int count = count_state_variables();
458
    std::string buf;
Michael Weber's avatar
Michael Weber committed
459

Jeroen Meijer's avatar
Jeroen Meijer committed
460
461
462
463
464
    if (condition) {
        line();
        append("// read:         " );
        for(size_int_t i = 0; i < count; i++)
        {
465
            buf = fmtf("%s%d", ((i==0)?"":","), ext_transition.sv_read[i]);
Jeroen Meijer's avatar
Jeroen Meijer committed
466
467
468
            append(buf);
        }
        line();
Michael Weber's avatar
Michael Weber committed
469

Jeroen Meijer's avatar
Jeroen Meijer committed
470
471
472
        append("// actions_read: " );
        for(size_int_t i = 0; i < count; i++)
        {
473
            buf = fmtf("%s%d", ((i==0)?"":","), ext_transition.sv_actions_read[i]);
Jeroen Meijer's avatar
Jeroen Meijer committed
474
475
476
477
478
479
480
            append(buf);
        }
        line();
    } else {
        append("// actions_read: " );
        for(size_int_t i = 0; i < count; i++)
        {
481
            buf = fmtf("%s%d", ((i==0)?"":","), ext_transition.sv_actions_read[i]);
Jeroen Meijer's avatar
Jeroen Meijer committed
482
483
484
            append(buf);
        }
        line();
Jeroen Meijer's avatar
Jeroen Meijer committed
485

Jeroen Meijer's avatar
Jeroen Meijer committed
486
487
488
        append("// may-write:    " );
        for(size_int_t i = 0; i < count; i++)
        {
489
            buf = fmtf("%s%d", ((i==0)?"":","), ext_transition.sv_may_write[i]);
Jeroen Meijer's avatar
Jeroen Meijer committed
490
491
492
493
494
495
496
            append(buf);
        }
        line();

        append("// must-write:   " );
        for(size_int_t i = 0; i < count; i++)
        {
497
            buf = fmtf("%s%d", ((i==0)?"":","), ext_transition.sv_must_write[i]);
Jeroen Meijer's avatar
Jeroen Meijer committed
498
499
500
            append(buf);
        }
        line();
Elwin Pater's avatar
Elwin Pater committed
501
502
503
    }
}

Michael Weber's avatar
Michael Weber committed
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
void dve_compiler::analyse_expression( dve_expression_t & expr, ext_transition_t &ext_transition, std::vector<int> &dep)
{
    dve_symbol_table_t * parent_table = expr.get_symbol_table();
    if (!parent_table) gerr << "Writing expression: Symbol table not set" << thr();
    switch (expr.get_operator())
    {
        case T_ID:
            //if (!(parent_table->get_variable(expr.get_ident_gid())->is_const())) // should this be here?
            mark_dependency(expr.get_ident_gid(), state_creator_t::VARIABLE, -1, dep);
            break;
        case T_FOREIGN_ID:
            mark_dependency(expr.get_ident_gid(), state_creator_t::VARIABLE, -1, dep);
            break;
        case T_NAT:
            break;
        case T_PARENTHESIS:
            analyse_expression(*expr.left(), ext_transition, dep);
            break;
        case T_FOREIGN_SQUARE_BRACKETS:
        case T_SQUARE_BRACKETS:
            if ((*expr.left()).get_operator() == T_NAT)
            {
                mark_dependency(expr.get_ident_gid(), state_creator_t::VARIABLE, (*expr.left()).get_value(), dep);
            } else {
                // some expression, mark all & continue analysis
Jeroen Meijer's avatar
Jeroen Meijer committed
529
                if (dep == ext_transition.sv_may_write || dep == ext_transition.sv_read || dep == ext_transition.sv_actions_read) {
Jeroen Meijer's avatar
Jeroen Meijer committed
530
531
532
                    mark_dependency(expr.get_ident_gid(), state_creator_t::VARIABLE, -1, dep);
                }
                if (dep == ext_transition.sv_may_write && may_write_add_read) {
533
                    mark_dependency(expr.get_ident_gid(), state_creator_t::VARIABLE, -1, ext_transition.sv_read);
Jeroen Meijer's avatar
Jeroen Meijer committed
534
                    mark_dependency(expr.get_ident_gid(), state_creator_t::VARIABLE, -1, ext_transition.sv_actions_read);
535
                }
536
                if ((*expr.left()).get_operator() == T_ASSIGNMENT) {
Jeroen Meijer's avatar
Jeroen Meijer committed
537
538
539
                    analyse_expression(*expr.left(), ext_transition, ext_transition.sv_may_write);
                    analyse_expression(*expr.left(), ext_transition, ext_transition.sv_must_write);
                } else if (dep == ext_transition.sv_may_write) {
540
                    analyse_expression(*expr.left(), ext_transition, ext_transition.sv_read);
Jeroen Meijer's avatar
Jeroen Meijer committed
541
                    analyse_expression(*expr.left(), ext_transition, ext_transition.sv_actions_read);
Jeroen Meijer's avatar
Jeroen Meijer committed
542
                } else if (dep != ext_transition.sv_must_write) {
543
544
                    analyse_expression(*expr.left(), ext_transition, dep);
                }
Michael Weber's avatar
Michael Weber committed
545
546
547
548
549
550
            }
            break;
        case T_LT: case T_LEQ: case T_EQ: case T_NEQ: case T_GT: case T_GEQ:
        case T_PLUS: case T_MINUS: case T_MULT: case T_DIV: case T_MOD:
        case T_AND: case T_OR: case T_XOR: case T_LSHIFT: case T_RSHIFT:
        case T_BOOL_AND: case T_BOOL_OR:
Jeroen Meijer's avatar
Jeroen Meijer committed
551
552
553
554
            if (dep == ext_transition.sv_read || dep == ext_transition.sv_actions_read) {
                analyse_expression( *expr.left(), ext_transition, dep );
                analyse_expression( *expr.right(), ext_transition, dep );
            }
Michael Weber's avatar
Michael Weber committed
555
556
            break;
        case T_ASSIGNMENT:
Jeroen Meijer's avatar
Jeroen Meijer committed
557
558
            analyse_expression( *expr.left(), ext_transition, ext_transition.sv_may_write );
            analyse_expression( *expr.left(), ext_transition, ext_transition.sv_must_write );
Michael Weber's avatar
Michael Weber committed
559
            analyse_expression( *expr.right(), ext_transition, ext_transition.sv_read );
Jeroen Meijer's avatar
Jeroen Meijer committed
560
            analyse_expression( *expr.right(), ext_transition, ext_transition.sv_actions_read );
Michael Weber's avatar
Michael Weber committed
561
562
563
564
565
            break;
        case T_DOT:
            // dot addes an explicit == (see code), thus must be read
            mark_dependency(parent_table->get_state(expr.get_ident_gid())->get_process_gid(),
                            state_creator_t::PROCESS_STATE, -1, ext_transition.sv_read);
Jeroen Meijer's avatar
Jeroen Meijer committed
566
567
            mark_dependency(parent_table->get_state(expr.get_ident_gid())->get_process_gid(),
                            state_creator_t::PROCESS_STATE, -1, ext_transition.sv_actions_read);
Michael Weber's avatar
Michael Weber committed
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
            break;
        case T_IMPLY:
            analyse_expression( *expr.left(), ext_transition, dep );
            analyse_expression( *expr.right(), ext_transition, dep );
            break;
        case T_UNARY_MINUS:
            analyse_expression( *expr.right(), ext_transition, dep);
            break;
        case T_TILDE:
            analyse_expression( *expr.right(), ext_transition, dep );
            break;
        case T_BOOL_NOT:
            analyse_expression( *expr.right(), ext_transition, dep );
            break;
        default:
            gerr << "Problem in expression - unknown operator"
                 << " number " << expr.get_operator() << psh();
    }
}

void dve_compiler::analyse_transition_dependencies( ext_transition_t &ext_transition )
{
    // only for ltsmin
    if (!ltsmin)
        return;

    // initialize read/write dependency vector
    int count = count_state_variables();
    ext_transition.sv_read.resize(count);
Jeroen Meijer's avatar
Jeroen Meijer committed
597
598
    ext_transition.sv_may_write.resize(count);
    ext_transition.sv_must_write.resize(count);
Jeroen Meijer's avatar
Jeroen Meijer committed
599
    ext_transition.sv_actions_read.resize(count);
Michael Weber's avatar
Michael Weber committed
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626

    // guard

    // mark process as read
    mark_dependency(ext_transition.first->get_process_gid(),
                    state_creator_t::PROCESS_STATE, -1, ext_transition.sv_read);

    if (ext_transition.first->get_guard())
    analyse_expression( *(ext_transition.first->get_guard()), ext_transition,
                        ext_transition.sv_read);

    if (ext_transition.synchronized)
    {
        // mark process as read
        mark_dependency(ext_transition.second->get_process_gid(),
                        state_creator_t::PROCESS_STATE, -1, ext_transition.sv_read);

        // analyse ext_transition->second->get_guard
        if (ext_transition.second->get_guard())
            analyse_expression( *(ext_transition.second->get_guard()), ext_transition,
            ext_transition.sv_read);
    } else {
        int sm = ext_transition.first->get_sync_mode();
        if (sm == SYNC_EXCLAIM_BUFFER || sm == SYNC_ASK_BUFFER)
        {
            mark_dependency(ext_transition.first->get_channel_gid(),
                            state_creator_t::CHANNEL_BUFFER, -1, ext_transition.sv_read);
Jeroen Meijer's avatar
Jeroen Meijer committed
627
628
            mark_dependency(ext_transition.first->get_channel_gid(),
                            state_creator_t::CHANNEL_BUFFER, -1, ext_transition.sv_actions_read);
Michael Weber's avatar
Michael Weber committed
629
630
631
        }
    }

632
    if (have_property)
Michael Weber's avatar
Michael Weber committed
633
634
635
    {
        // mark process as read/write?
        mark_dependency(ext_transition.property->get_process_gid(),
636
                        state_creator_t::PROCESS_STATE, -1, ext_transition.sv_read);
Jeroen Meijer's avatar
Jeroen Meijer committed
637
638
        mark_dependency(ext_transition.property->get_process_gid(),
                        state_creator_t::PROCESS_STATE, -1, ext_transition.sv_actions_read);
Michael Weber's avatar
Michael Weber committed
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653

        // analyse ext_transition->property->get_guard
        if (ext_transition.property->get_guard())
            analyse_expression( *(ext_transition.property->get_guard()), ext_transition,
            ext_transition.sv_read);
    }

    // effect
    // todo: synchronized & channel effects...
    if (ext_transition.synchronized)
    {
        for(size_int_t s = 0;s < ext_transition.first->get_sync_expr_list_size();s++)
        {
            // todo: test  :)
            analyse_expression( *(ext_transition.first->get_sync_expr_list_item(s)), ext_transition,
Jeroen Meijer's avatar
Jeroen Meijer committed
654
655
656
                                ext_transition.sv_may_write);
            analyse_expression( *(ext_transition.first->get_sync_expr_list_item(s)), ext_transition,
                                ext_transition.sv_must_write);
Michael Weber's avatar
Michael Weber committed
657
658
            analyse_expression( *(ext_transition.second->get_sync_expr_list_item(s)), ext_transition,
                                ext_transition.sv_read);
Jeroen Meijer's avatar
Jeroen Meijer committed
659
660
            analyse_expression( *(ext_transition.second->get_sync_expr_list_item(s)), ext_transition,
                                ext_transition.sv_actions_read);
Michael Weber's avatar
Michael Weber committed
661
662
663
664
665
666
667
        }
    } else {
        int sm = ext_transition.first->get_sync_mode();
        if (sm == SYNC_EXCLAIM_BUFFER)
        {
            // mark entire channel
            mark_dependency(ext_transition.first->get_channel_gid(),
Jeroen Meijer's avatar
Jeroen Meijer committed
668
669
670
                            state_creator_t::CHANNEL_BUFFER, -1, ext_transition.sv_may_write);
            mark_dependency(ext_transition.first->get_channel_gid(),
                            state_creator_t::CHANNEL_BUFFER, -1, ext_transition.sv_must_write);
Michael Weber's avatar
Michael Weber committed
671
672
673
674
675
            // mark sync expressions
            for(size_int_t s = 0;s < ext_transition.first->get_sync_expr_list_size();s++)
            {
                analyse_expression( *(ext_transition.first->get_sync_expr_list_item(s)), ext_transition,
                                    ext_transition.sv_read);
Jeroen Meijer's avatar
Jeroen Meijer committed
676
677
                analyse_expression( *(ext_transition.first->get_sync_expr_list_item(s)), ext_transition,
                                    ext_transition.sv_actions_read);
Michael Weber's avatar
Michael Weber committed
678
679
680
681
682
683
684
            }
        }
        if (sm == SYNC_ASK_BUFFER)
        {
            // mark entire channel
            mark_dependency(ext_transition.first->get_channel_gid(),
                            state_creator_t::CHANNEL_BUFFER, -1, ext_transition.sv_read);
Jeroen Meijer's avatar
Jeroen Meijer committed
685
686
            mark_dependency(ext_transition.first->get_channel_gid(),
                            state_creator_t::CHANNEL_BUFFER, -1, ext_transition.sv_actions_read);
Michael Weber's avatar
Michael Weber committed
687
            mark_dependency(ext_transition.first->get_channel_gid(),
Jeroen Meijer's avatar
Jeroen Meijer committed
688
689
690
                            state_creator_t::CHANNEL_BUFFER, -1, ext_transition.sv_may_write);
            mark_dependency(ext_transition.first->get_channel_gid(),
                            state_creator_t::CHANNEL_BUFFER, -1, ext_transition.sv_must_write);
Michael Weber's avatar
Michael Weber committed
691
692
693
694
            // mark sync expressions
            for(size_int_t s = 0;s < ext_transition.first->get_sync_expr_list_size();s++)
            {
                analyse_expression( *(ext_transition.first->get_sync_expr_list_item(s)), ext_transition,
Jeroen Meijer's avatar
Jeroen Meijer committed
695
696
697
                                    ext_transition.sv_may_write);
                analyse_expression( *(ext_transition.first->get_sync_expr_list_item(s)), ext_transition,
                                    ext_transition.sv_must_write);
Michael Weber's avatar
Michael Weber committed
698
699
700
701
702
703
704
705
            }

        }

    }

    // mark process as read (write is probably in transition effect)
    mark_dependency(ext_transition.first->get_process_gid(),
Jeroen Meijer's avatar
Jeroen Meijer committed
706
707
708
                    state_creator_t::PROCESS_STATE, -1, ext_transition.sv_may_write);
    mark_dependency(ext_transition.first->get_process_gid(),
                    state_creator_t::PROCESS_STATE, -1, ext_transition.sv_must_write);
Michael Weber's avatar
Michael Weber committed
709
710

    // analyse ext_transition->first
Jeroen Meijer's avatar
Jeroen Meijer committed
711
    for(size_int_t e = 0;e < ext_transition.first->get_effect_count();e++) {
Michael Weber's avatar
Michael Weber committed
712
713
        analyse_expression( *(ext_transition.first->get_effect(e)), ext_transition,
        ext_transition.sv_read);
Jeroen Meijer's avatar
Jeroen Meijer committed
714
715
716
        analyse_expression( *(ext_transition.first->get_effect(e)), ext_transition,
        ext_transition.sv_actions_read);
    }
Michael Weber's avatar
Michael Weber committed
717
718
719
720
721
722

    // analyse ext_transition->second?
    if (ext_transition.synchronized)
    {
        // mark process as read (write is probably in transition effect)
        mark_dependency(ext_transition.second->get_process_gid(),
Jeroen Meijer's avatar
Jeroen Meijer committed
723
724
725
                        state_creator_t::PROCESS_STATE, -1, ext_transition.sv_may_write);
        mark_dependency(ext_transition.second->get_process_gid(),
                        state_creator_t::PROCESS_STATE, -1, ext_transition.sv_must_write);
Michael Weber's avatar
Michael Weber committed
726
727

        // analyse ext_transition->second
Jeroen Meijer's avatar
Jeroen Meijer committed
728
        for(size_int_t e = 0;e < ext_transition.second->get_effect_count();e++) {
Michael Weber's avatar
Michael Weber committed
729
730
            analyse_expression( *(ext_transition.second->get_effect(e)), ext_transition,
            ext_transition.sv_read);
Jeroen Meijer's avatar
Jeroen Meijer committed
731
732
733
            analyse_expression( *(ext_transition.second->get_effect(e)), ext_transition,
            ext_transition.sv_actions_read);
        }
Michael Weber's avatar
Michael Weber committed
734
735
    }

736
    if (have_property)
Michael Weber's avatar
Michael Weber committed
737
738
739
    {
        // mark process as read/write?
        mark_dependency(ext_transition.property->get_process_gid(),
Jeroen Meijer's avatar
Jeroen Meijer committed
740
741
742
                        state_creator_t::PROCESS_STATE, -1, ext_transition.sv_may_write);
        mark_dependency(ext_transition.property->get_process_gid(),
                        state_creator_t::PROCESS_STATE, -1, ext_transition.sv_must_write);
Michael Weber's avatar
Michael Weber committed
743
744
745
    }
}

Elwin Pater's avatar
Elwin Pater committed
746
747
748
749
750
751
752
753
754
755
756
757
758
void dve_compiler::analyse_transition(
    dve_transition_t * transition,
    vector<ext_transition_t> &ext_transition_vector )
{
    if(!transition->is_sync_ask())
    {
        // transition not of type SYNC_ASK
        if(!have_property)
        {
            // no properties, just add to ext_transition vector
            ext_transition_t ext_transition;
            ext_transition.synchronized = false;
            ext_transition.first = transition;
Michael Weber's avatar
Michael Weber committed
759
            analyse_transition_dependencies(ext_transition);
Elwin Pater's avatar
Elwin Pater committed
760
761
762
763
764
765
766
767
768
769
770
771
772
773
            ext_transition_vector.push_back(ext_transition);
        }
        else
        {
            // this transition is not a property, but there are properties
            // forall properties, add this transition to ext_transition_vector
            for(iter_property_transitions = property_transitions.begin();
                iter_property_transitions != property_transitions.end();
                iter_property_transitions++)
            {
                ext_transition_t ext_transition;
                ext_transition.synchronized = false;
                ext_transition.first = transition;
                ext_transition.property = (*iter_property_transitions);
Michael Weber's avatar
Michael Weber committed
774
                analyse_transition_dependencies(ext_transition);
Elwin Pater's avatar
Elwin Pater committed
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
                ext_transition_vector.push_back(ext_transition);
            }
        }
    }
    else
    {
        // transition of type SYNC_ASK
        iter_channel_map = channel_map.find(transition->get_channel_gid());
        if(iter_channel_map != channel_map.end())
        {
            // channel of this transition is found
            // (strange test, no else part for if statement)
            // assume: channel should always be present
            // forall transitions that also use this channel, add to ext_transitions
            for(iter_transition_vector  = iter_channel_map->second.begin();
                iter_transition_vector != iter_channel_map->second.end();
                iter_transition_vector++)
            {
                if (transition->get_process_gid() != (*iter_transition_vector)->get_process_gid() ) //not synchronize with yourself
                {
                    if(!have_property)
                    {
                        // system has no properties, so add only once without property
                        ext_transition_t ext_transition;
                        ext_transition.synchronized = true;
                        ext_transition.first = transition;
                        ext_transition.second = (*iter_transition_vector);
Michael Weber's avatar
Michael Weber committed
802
                        analyse_transition_dependencies(ext_transition);
Elwin Pater's avatar
Elwin Pater committed
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
                        ext_transition_vector.push_back(ext_transition);
                    }
                    else
                    {
                        // system has properties, so forall properties, add the combination if this transition,
                        // the transition that also uses this channel and the property
                        for(iter_property_transitions = property_transitions.begin();
                            iter_property_transitions != property_transitions.end();
                            iter_property_transitions++)
                        {
                            ext_transition_t ext_transition;
                            ext_transition.synchronized = true;
                            ext_transition.first = transition;
                            ext_transition.second = (*iter_transition_vector);
                            ext_transition.property = (*iter_property_transitions);
Michael Weber's avatar
Michael Weber committed
818
                            analyse_transition_dependencies(ext_transition);
Elwin Pater's avatar
Elwin Pater committed
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
                            ext_transition_vector.push_back(ext_transition);
                        }
                    }
                }
            }
        }
    }
}

void dve_compiler::analyse()
{
    dve_transition_t * transition;
    have_property = get_with_property();

    // obtain transition with synchronization of the type SYNC_EXCLAIM and property transitions
    for(size_int_t i = 0; i < get_trans_count(); i++)
    {
        transition = dynamic_cast<dve_transition_t*>(get_transition(i));
        if(transition->is_sync_exclaim())
        {
            iter_channel_map = channel_map.find(transition->get_channel_gid());
            if(iter_channel_map == channel_map.end()) //new channel
            {
                vector<dve_transition_t*> transition_vector;
                transition_vector.push_back(transition);
                channel_map.insert(pair<size_int_t,vector<dve_transition_t*> >(
                                       transition->get_channel_gid(),transition_vector));
            }
            else{
                iter_channel_map->second.push_back(transition);
            }
        }

        if( is_property( transition->get_process_gid() ) )
            property_transitions.push_back(transition);
    }

    // obtain map of transitions
    for(size_int_t i = 0; i < get_trans_count(); i++)
    {
        transition = dynamic_cast<dve_transition_t*>(get_transition(i));
        if(!transition->is_sync_exclaim() && !is_property( transition->get_process_gid() ) )
        {
            // not syncronized sender without buffer and not a property transition
            iter_transition_map = transition_map.find(transition->get_process_gid());

            //new process it means that new state in process is also new
            if( iter_transition_map == transition_map.end())
            {
                // new process, add to transition map
                map<size_int_t,vector<ext_transition_t> >  process_transition_map;
                vector<ext_transition_t> ext_transition_vector;

                analyse_transition( transition, ext_transition_vector );

                // for this process state, add the ext transitions
                process_transition_map.insert(pair<size_int_t,vector<ext_transition_t> >(
                                                  transition->get_state1_lid(),ext_transition_vector));
                // then add this vector to the transition map for this process
                transition_map.insert(pair<size_int_t,map<size_int_t,vector<ext_transition_t> > >(
                                          transition->get_process_gid(),process_transition_map));
            } else {
                // existing process, find process_transition_map
                iter_process_transition_map =
                    iter_transition_map->second.find(transition->get_state1_lid());

                //new state in current process
                if( iter_process_transition_map == iter_transition_map->second.end())
                {
                    vector<ext_transition_t> ext_transition_vector;
                    analyse_transition( transition, ext_transition_vector );

                    // and reinsert result
                    iter_transition_map->second.insert(
                        pair<size_int_t,vector<ext_transition_t> >(
                            transition->get_state1_lid(),ext_transition_vector) );
                } else analyse_transition( transition, iter_process_transition_map->second );
            }
        }
    }
}

void dve_compiler::transition_guard( ext_transition_t *et, std::string in )
{
Jeroen Meijer's avatar
Jeroen Meijer committed
903
904
    output_dependency_comment(*et, true);

Elwin Pater's avatar
Elwin Pater committed
905
    if_begin( false );
Michael Weber's avatar
Michael Weber committed
906
907
908
909
910
911

    // ltsmin guard extension
    if ( ltsmin && !many) {
        if_clause( in_state( et->first->get_process_gid(), et->first->get_state1_lid(), in) );
    }

Elwin Pater's avatar
Elwin Pater committed
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
    if_cexpr_clause( et->first->get_guard(), in );

    if( et->synchronized )
    {
        if_clause( in_state( et->second->get_process_gid(),
                             et->second->get_state1_lid(), in ) );
        if_cexpr_clause( et->second->get_guard(), in );
    }
    else
    {
        int chan = et->first->get_channel_gid();
        if(et->first->get_sync_mode() == SYNC_EXCLAIM_BUFFER)
            if_clause( relate( channel_items( chan, in ), "!=",
                               fmt( channel_capacity( chan ) ) ) );

        if(et->first->get_sync_mode() == SYNC_ASK_BUFFER)
            if_clause( relate( channel_items( chan, in ), "!=", "0" ) );
    }
    if(have_property)
    {
        if_clause( in_state( et->property->get_process_gid(),
                             et->property->get_state1_lid(), in ) );
        if_cexpr_clause( et->property->get_guard(), in );
    }

    if_end();
}

void dve_compiler::transition_effect( ext_transition_t *et, std::string in, std::string out )
{
    if(et->synchronized)
    {
Jeroen Meijer's avatar
Jeroen Meijer committed
944
        for(size_int_t s = 0;s < et->first->get_sync_expr_list_size();s++) {
945
946
947
            assign( cexpr( *et->first->get_sync_expr_list_item(s), out, false ),
                    cexpr( *et->second->get_sync_expr_list_item(s), in, false ) );
            line("cpy[((int*)&"+cexpr( *et->first->get_sync_expr_list_item(s), out, false )+" - (int*)&"+out+")] = 0;");
Jeroen Meijer's avatar
Jeroen Meijer committed
948
        }
Elwin Pater's avatar
Elwin Pater committed
949
950
951
952
953
954
955
956
957
    }
    else
    {
        int chan = et->first->get_channel_gid();
        if(et->first->get_sync_mode() == SYNC_EXCLAIM_BUFFER)
        {
            for(size_int_t s = 0;s < et->first->get_sync_expr_list_size();s++)
            {
                assign( channel_item_at( chan, channel_items( chan, in ), s, out ),
958
                        cexpr( *et->first->get_sync_expr_list_item( s ), in, false ) );
Jeroen Meijer's avatar
Jeroen Meijer committed
959
                line("cpy[((int*)&"+channel_item_at( chan, channel_items( chan, in ), s, out )+" - (int*)&"+out+")] = 0;");
Elwin Pater's avatar
Elwin Pater committed
960
961
962
963
964
            }
            line( channel_items( chan, out ) + "++;" );
        }
        if(et->first->get_sync_mode() == SYNC_ASK_BUFFER)
        {
Jeroen Meijer's avatar
Jeroen Meijer committed
965
            for(size_int_t s = 0;s < et->first->get_sync_expr_list_size();s++) {
966
                assign( cexpr( *et->first->get_sync_expr_list_item(s), out, false ),
Elwin Pater's avatar
Elwin Pater committed
967
                        channel_item_at( chan, "0", s, in ) );
968
                line("cpy[((int*)&"+cexpr( *et->first->get_sync_expr_list_item(s), out, false )+" - (int*)&"+out+")] = 0;");
Jeroen Meijer's avatar
Jeroen Meijer committed
969
            }
Elwin Pater's avatar
Elwin Pater committed
970
971
972
973
974
975
976
            line( channel_items( chan, out ) + "--;" );

            line( "for(size_int_t i = 1 ; i <= " + channel_items( chan, out ) + "; i++)" );
            block_begin();
            for(size_int_t s = 0;s < et->first->get_sync_expr_list_size();s++)
            {
                assign( channel_item_at( chan, "i-1", s, out ), channel_item_at( chan, "i", s, in ) );
Jeroen Meijer's avatar
Jeroen Meijer committed
977
                line("cpy[((int*)&"+channel_item_at( chan, "i-1", s, out )+" - (int*)&"+out+")] = 0;");
Elwin Pater's avatar
Elwin Pater committed
978
                assign( channel_item_at( chan, "i", s, out ), "0" );
Jeroen Meijer's avatar
Jeroen Meijer committed
979
                line("cpy[((int*)&"+channel_item_at( chan, "i", s, out )+" - (int*)&"+out+")] = 0;");
Elwin Pater's avatar
Elwin Pater committed
980
981
982
983
984
985
986
987
            }
            block_end();
        }
    }

    //first transition effect
    assign( process_state( et->first->get_process_gid(), out ),
            fmt( et->first->get_state2_lid() ) );
Jeroen Meijer's avatar
Jeroen Meijer committed
988
    line("cpy[((int*)&"+process_state( et->first->get_process_gid(), out )+" - (int*)&"+out+")] = 0;");
Elwin Pater's avatar
Elwin Pater committed
989

Jeroen Meijer's avatar
Jeroen Meijer committed
990
    for(size_int_t e = 0;e < et->first->get_effect_count();e++) {
Elwin Pater's avatar
Elwin Pater committed
991
        print_cexpr( *et->first->get_effect(e), out );
992
        line("cpy[((int*)&"+cexpr(*et->first->get_effect(e)->left(), out, false)+" - (int*)&"+out+")] = 0;");
Jeroen Meijer's avatar
Jeroen Meijer committed
993
    }
Elwin Pater's avatar
Elwin Pater committed
994
995
996
997
998

    if(et->synchronized) //second transiton effect
    {
        assign( process_state( et->second->get_process_gid(), out ),
                fmt( et->second->get_state2_lid() ) );
Jeroen Meijer's avatar
Jeroen Meijer committed
999
1000
        line("cpy[((int*)&"+process_state( et->second->get_process_gid(), out )+" - (int*)&"+out+")] = 0;");
        for(size_int_t e = 0;e < et->second->get_effect_count();e++) {
Elwin Pater's avatar
Elwin Pater committed
1001
            print_cexpr( *et->second->get_effect(e), out );
1002
            line("cpy[((int*)&"+cexpr(*et->second->get_effect(e)->left(), out, false)+" - (int*)&"+out+")] = 0;");
Jeroen Meijer's avatar
Jeroen Meijer committed
1003
        }
Elwin Pater's avatar
Elwin Pater committed
1004
1005
1006
    }

    if(have_property) //change of the property process state
Jeroen Meijer's avatar
Jeroen Meijer committed
1007
    {
Elwin Pater's avatar
Elwin Pater committed
1008
1009
        assign( process_state( et->property->get_process_gid(), out ),
                fmt( et->property->get_state2_lid() ) );
Jeroen Meijer's avatar
Jeroen Meijer committed
1010
1011
        line("cpy[((int*)&"+process_state( et->property->get_process_gid(), out )+" - (int*)&"+out+")] = 0;");
    }
Michael Weber's avatar
Michael Weber committed
1012
1013

    // show dependency information in the source
Jeroen Meijer's avatar
Jeroen Meijer committed
1014
    output_dependency_comment(*et, false);
Elwin Pater's avatar
Elwin Pater committed
1015
1016
1017
}

void dve_compiler::new_output_state() {
Michael Weber's avatar
Michael Weber committed
1018
1019
    if (ltsmin) {
            line( "*out = *in;" );
Alfons Laarman's avatar
Alfons Laarman committed
1020
1021
1022
            append( "int cpy[" + fmt(count_state_variables())+ "] = { " );
            for (int i = 0; i < count_state_variables(); i++) append ("1,");
            line("};");
Michael Weber's avatar
Michael Weber committed
1023
1024
1025
1026
1027
1028
    } else {
        line( "divine::Blob blob_out( *(setup->pool), setup->slack + state_size );" );
        line( "state_struct_t *out = &blob_out.get< state_struct_t >( setup->slack );" );
        line( "blob_out.clear( 0, setup->slack );" );
        line( "*out = *in;" );
    }
Elwin Pater's avatar
Elwin Pater committed
1029
1030
1031
}

void dve_compiler::yield_state() {
Michael Weber's avatar
Michael Weber committed
1032
1033
1034
1035
    if (ltsmin) {
        if (many) {
            line ("transition_info.group = " + fmt( current_label++) + ";");
        }
Jeroen Meijer's avatar
Jeroen Meijer committed
1036
        line( "callback(arg, &transition_info, out, cpy);" );
Elwin Pater's avatar
Elwin Pater committed
1037
1038
        line( "++states_emitted;" );
    } else {
Michael Weber's avatar
Michael Weber committed
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
        if ( many ) {
            line( "if (buf_out->space() < 2) {" );
            line( "    buf_out->unadd( states_emitted );" );
            line( "    return;" );
            line( "}");
            line( "buf_out->add( (*buf_in)[ 0 ] );" );
            line( "buf_out->add( blob_out );" );
            line( "++states_emitted;" );
        } else {
            line( "*to = blob_out;" );
            line( "return " + fmt( current_label ) + ";" );
        }
    }
}

Jeroen Meijer's avatar
Jeroen Meijer committed
1054
void dve_compiler::gen_ltsmin_successors(bool condition)
Michael Weber's avatar
Michael Weber committed
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
{
    string in = "(*in)", out = "(*out)", space = "";
    bool some_commited_state = false;

    // find some commited state
    for(size_int_t i = 0; i < get_process_count(); i++)
        for(size_int_t j = 0; j < dynamic_cast<dve_process_t*>(get_process(i))->get_state_count(); j++)
            if(dynamic_cast<dve_process_t*>(get_process(i))->get_commited(j))
                some_commited_state = true;

    if (some_commited_state)
    {
        for(size_int_t i = 0; i < this->get_process_count(); i++)
        {
            if( transition_map.find(i) != transition_map.end() && !is_property( i ) )
Jeroen Meijer's avatar
Jeroen Meijer committed
1070
1071
1072
1073
1074
1075
            for(iter_process_transition_map = transition_map.find(i)->second.begin();
                iter_process_transition_map != transition_map.find(i)->second.end();
                iter_process_transition_map++)
            {
                if(dynamic_cast<dve_process_t*>(get_process(i))->get_commited(
                       iter_process_transition_map->first))
Michael Weber's avatar
Michael Weber committed
1076
                {
Jeroen Meijer's avatar
Jeroen Meijer committed
1077
1078
1079
                    for(iter_ext_transition_vector = iter_process_transition_map->second.begin();
                        iter_ext_transition_vector != iter_process_transition_map->second.end();
                        iter_ext_transition_vector++)
Michael Weber's avatar
Michael Weber committed
1080
                    {
Jeroen Meijer's avatar
Jeroen Meijer committed
1081
1082
1083
1084
1085
                        // !! jak je to s property synchronizaci v comitted stavech !!
                        if( !iter_ext_transition_vector->synchronized ||
                            dynamic_cast<dve_process_t*>(
                                get_process(iter_ext_transition_vector->second->get_process_gid()))->
                            get_commited(iter_ext_transition_vector->second->get_state1_lid()) )
Michael Weber's avatar
Michael Weber committed
1086
                        {
Jeroen Meijer's avatar
Jeroen Meijer committed
1087
1088
                            // only generate if not synchonized or synchonized with a committed transition
                            new_label();
Jeroen Meijer's avatar
Jeroen Meijer committed
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
                            if (condition) { // generate get_successor
                                block_begin();
                                transition_guard( &*iter_ext_transition_vector, in );
                                line("states_emitted = get_action(model, t, in, callback, arg);");
                                block_end();
                                line("return states_emitted;");
                            } else {
                                block_begin();
                                new_output_state();
                                transition_effect( &*iter_ext_transition_vector, in, out );
                                yield_state();
                                block_end();
                                line("return states_emitted;");
                            }
Michael Weber's avatar
Michael Weber committed
1103
1104
1105
                        }
                    }
                }
Jeroen Meijer's avatar
Jeroen Meijer committed
1106
            }
Michael Weber's avatar
Michael Weber committed
1107
1108
1109
1110
1111
1112
        }
    }

    for(size_int_t i = 0; i < get_process_count(); i++)
    {
        if(transition_map.find(i) != transition_map.end() && !is_property( i ))
Jeroen Meijer's avatar
Jeroen Meijer committed
1113
1114
1115
1116
1117
1118
1119
        for(iter_process_transition_map = transition_map.find(i)->second.begin();
            iter_process_transition_map != transition_map.find(i)->second.end();
            iter_process_transition_map++)
        {
            for(iter_ext_transition_vector = iter_process_transition_map->second.begin();
                iter_ext_transition_vector != iter_process_transition_map->second.end();
                iter_ext_transition_vector++)
Michael Weber's avatar
Michael Weber committed
1120
            {
Jeroen Meijer's avatar
Jeroen Meijer committed
1121
1122
1123
1124
1125
                // make sure this transition is not a committed one
                if (!
                    dynamic_cast<dve_process_t*>(
                        get_process(iter_ext_transition_vector->first->get_process_gid()))->
                    get_commited(iter_ext_transition_vector->first->get_state1_lid()) )
Michael Weber's avatar
Michael Weber committed
1126
1127
                {

Jeroen Meijer's avatar
Jeroen Meijer committed
1128
                    new_label();
Jeroen Meijer's avatar
Jeroen Meijer committed
1129
1130
1131
1132
1133
1134
1135
                    if (condition) {
                        block_begin();
                        transition_guard( &*iter_ext_transition_vector, in );
                        line("states_emitted = get_action(model, t, in, callback, arg);");
                        block_end();
                        line("return states_emitted;");
                    } else {
Michael Weber's avatar
Michael Weber committed
1136

Jeroen Meijer's avatar
Jeroen Meijer committed
1137
1138
1139
1140
1141
                        block_begin();
                        if (some_commited_state)
                        {
                            // committed state
                            if_begin( true );
Michael Weber's avatar
Michael Weber committed
1142

Jeroen Meijer's avatar
Jeroen Meijer committed
1143
1144
1145
1146
                            for(size_int_t p = 0; p < get_process_count(); p++)
                                for(size_int_t c = 0; c < dynamic_cast<dve_process_t*>(get_process(p))->get_state_count(); c++)
                                    if(dynamic_cast<dve_process_t*>(get_process(p))->get_commited(c))
                                        if_clause( in_state( p, c, in ) );
Michael Weber's avatar
Michael Weber committed
1147

Jeroen Meijer's avatar
Jeroen Meijer committed
1148
1149
1150
                            if_end();
                            line("    return 0;"); // bail out early
                        }
Michael Weber's avatar
Michael Weber committed
1151
1152


Jeroen Meijer's avatar
Jeroen Meijer committed
1153
1154
1155
1156
1157
1158
                        new_output_state();
                        transition_effect( &*iter_ext_transition_vector, in, out );
                        yield_state();
                        block_end();
                        line("return states_emitted;");
                    }
Michael Weber's avatar
Michael Weber committed
1159
1160
                }
            }
Jeroen Meijer's avatar
Jeroen Meijer committed
1161
        }
Elwin Pater's avatar
Elwin Pater committed
1162
1163
1164
    }
}

Michael Weber's avatar
Michael Weber committed
1165

Elwin Pater's avatar
Elwin Pater committed
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
void dve_compiler::gen_successors()
{
    string in = "(*in)", out = "(*out)", space = "";
    bool some_commited_state = false;

    new_label();
    if_begin( true );

    for(size_int_t i = 0; i < get_process_count(); i++)
        for(size_int_t j = 0; j < dynamic_cast<dve_process_t*>(get_process(i))->get_state_count(); j++)
            if(dynamic_cast<dve_process_t*>(get_process(i))->get_commited(j))
                if_clause( in_state( i, j, in ) );

Alfons Laarman's avatar
Alfons Laarman committed
1179
    if_end(); block_begin(); // committed states
Elwin Pater's avatar
Elwin Pater committed
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207

    for(size_int_t i = 0; i < this->get_process_count(); i++)
    {
        if( transition_map.find(i) != transition_map.end() && !is_property( i ) )
            for(iter_process_transition_map = transition_map.find(i)->second.begin();
                iter_process_transition_map != transition_map.find(i)->second.end();
                iter_process_transition_map++)
            {
                if(dynamic_cast<dve_process_t*>(get_process(i))->get_commited(
                       iter_process_transition_map->first))
                {
                    new_label();

                    if_begin( true );
                    if_clause( in_state( i, iter_process_transition_map->first, in ) );
                    if_end(); block_begin();


                    for(iter_ext_transition_vector = iter_process_transition_map->second.begin();
                        iter_ext_transition_vector != iter_process_transition_map->second.end();
                        iter_ext_transition_vector++)
                    {
                        // !! jak je to s property synchronizaci v comitted stavech !!
                        if( !iter_ext_transition_vector->synchronized ||
                            dynamic_cast<dve_process_t*>(
                                get_process(iter_ext_transition_vector->second->get_process_gid()))->
                            get_commited(iter_ext_transition_vector->second->get_state1_lid()) )
                        {
Alfons Laarman's avatar
Alfons Laarman committed
1208
                            new_label();
Elwin Pater's avatar
Elwin Pater committed
1209
1210
                            transition_guard( &*iter_ext_transition_vector, in );
                            block_begin();
Alfons Laarman's avatar
Alfons Laarman committed
1211
                            new_output_state();
Elwin Pater's avatar
Elwin Pater committed
1212
                            transition_effect( &*iter_ext_transition_vector, in, out );
Alfons Laarman's avatar
Alfons Laarman committed
1213
                            yield_state();
Elwin Pater's avatar
Elwin Pater committed
1214
1215
1216
1217
1218
1219
1220
1221
1222
                            block_end();
                        }
                    }

                    block_end();
                }
            }
    }

Alfons Laarman's avatar
Alfons Laarman committed
1223
1224
1225
    new_label(); // Trick. : - )
    line( ";" );

Elwin Pater's avatar
Elwin Pater committed
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
    block_end();
    line( "else" );
    block_begin();

    for(size_int_t i = 0; i < get_process_count(); i++)
    {
        if(transition_map.find(i) != transition_map.end() && !is_property( i ))
            for(iter_process_transition_map = transition_map.find(i)->second.begin();
                iter_process_transition_map != transition_map.find(i)->second.end();
                iter_process_transition_map++)
            {
Michael Weber's avatar
Michael Weber committed
1237
1238
1239
1240
                // make sure this transition is not a committed one
                if (!
                    dynamic_cast<dve_process_t*>(
                        get_process(i))->get_commited(iter_process_transition_map->first) )
Elwin Pater's avatar
Elwin Pater committed
1241
                {
Michael Weber's avatar
Michael Weber committed
1242

Elwin Pater's avatar
Elwin Pater committed
1243
                    new_label();
Michael Weber's avatar
Michael Weber committed
1244
1245
                    if_begin( true );
                    if_clause( in_state( i, iter_process_transition_map->first, in ) );
Elwin Pater's avatar
Elwin Pater committed
1246

Michael Weber's avatar
Michael Weber committed
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
                    if_end(); block_begin();

                    for(iter_ext_transition_vector = iter_process_transition_map->second.begin();
                        iter_ext_transition_vector != iter_process_transition_map->second.end();
                        iter_ext_transition_vector++)
                    {
                        new_label();

                        transition_guard( &*iter_ext_transition_vector, in );
                        block_begin();
                        new_output_state();
                        transition_effect( &*iter_ext_transition_vector, in, out );
1259
                        line( "system_in_deadlock = false;" );
Michael Weber's avatar
Michael Weber committed
1260
1261
1262
                        yield_state();
                        block_end();
                    }
Elwin Pater's avatar
Elwin Pater committed
1263
1264
1265
1266
1267
1268
                    block_end();
                }
            }
    }
    block_end();

1269
    if (ltsmin_ltl) return;
Elwin Pater's avatar
Elwin Pater committed
1270

1271
    new_label();
Elwin Pater's avatar
Elwin Pater committed
1272

1273
1274
1275
    if_begin( true );
    if_clause( "system_in_deadlock" );
    if_end(); block_begin();
Michael Weber's avatar
Michael Weber committed
1276

1277
1278
1279
1280
1281
1282
    for(iter_property_transitions = property_transitions.begin();
        iter_property_transitions != property_transitions.end();
        iter_property_transitions++)
    {
        new_label();
        if_begin( false );
Elwin Pater's avatar
Elwin Pater committed
1283

1284
1285
1286
        if_clause( in_state( (*iter_property_transitions)->get_process_gid(),
                             (*iter_property_transitions)->get_state1_lid(), in ) );
        if_cexpr_clause( (*iter_property_transitions)->get_guard(), in );
Michael Weber's avatar
Michael Weber committed
1287

1288
1289
        if_end(); block_begin();
        new_output_state();
Michael Weber's avatar
Michael Weber committed
1290

1291
1292
        assign( process_state( (*iter_property_transitions)->get_process_gid(), out ),
                fmt( (*iter_property_transitions)->get_state2_lid() ) );
Jeroen Meijer's avatar
Jeroen Meijer committed
1293
        line("cpy[((int*)&"+process_state( (*iter_property_transitions)->get_process_gid(), out )+" - (int*)&"+out+")] = 0;");
1294
1295

        yield_state();
Elwin Pater's avatar
Elwin Pater committed
1296
1297
        block_end();
    }
1298
    block_end();
Elwin Pater's avatar
Elwin Pater committed
1299
1300
1301
1302
1303
1304
1305
1306
}

void dve_compiler::gen_is_accepting()
{
    // not compulsory, so don't bother if not needed
    if(!have_property)
        return;

Elwin Pater's avatar
Elwin Pater committed
1307
    if (ltsmin) {
1308
        line( "extern \"C\" int buchi_is_accepting( void* model, void *_state )" );
Elwin Pater's avatar
Elwin Pater committed
1309
1310
1311
1312
1313
1314
1315
1316
        block_begin();
        line( "(void)model;" );
        line( "state_struct_t &state = * (state_struct_t*) _state;" );
    } else {
        line( "extern \"C\" bool is_accepting( CustomSetup *setup, Blob b, int size )" );
        block_begin();
        line( "state_struct_t &state = b.get< state_struct_t >( setup->slack );" );
    }
Elwin Pater's avatar
Elwin Pater committed
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344

    for(size_int_t i = 0; i < dynamic_cast<dve_process_t*>(get_process((get_property_gid())))->get_state_count(); i++)
    {
        if (dynamic_cast<dve_process_t*>(get_process((get_property_gid())))->get_acceptance(i, 0, 1) )
        {
            if_begin( true );
            if_clause( in_state( get_property_gid(), i, "state" ) );
            if_end();
            line( "    return true;" );
        }
    }
    line( "return false;" );
    block_end();

    line();
}

void dve_compiler::print_generator()
{
    gen_header();
    gen_state_struct();
    gen_initial_state();

    line( "extern \"C\" int get_state_size() {" );
    line( "    return state_size;" );
    line( "}" );
    line();

Michael Weber's avatar
Michael Weber committed
1345
    if (ltsmin) {
1346
        line( "extern \"C\" void get_initial_state( void *to )" );
Michael Weber's avatar
Michael Weber committed
1347
        block_begin();
1348
        line( "memcpy(to, &initial_state, state_size);" );
Michael Weber's avatar
Michael Weber committed
1349
1350
1351
        block_end();
        line();

1352
        line( "extern \"C\" int have_property()" );
Michael Weber's avatar
Michael Weber committed
1353
1354
1355
1356
1357
1358
1359
1360
1361
        block_begin();
        if (have_property) {
            line("return true;");
        } else {
            line("return false;");
        }
        block_end();
        line();

Elwin Pater's avatar
Elwin Pater committed
1362
1363
        gen_is_accepting();

Jeroen Meijer's avatar
Jeroen Meijer committed
1364
        // generate get_action
Michael Weber's avatar
Michael Weber committed
1365
1366
        many = false;
        current_label = 0;