dvecompile.cpp 70.5 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
5
6
#include <tools/dvecompile.h>
#include <divine/generator/common.h>

using namespace wibble::str;

Alfons Laarman's avatar
Alfons Laarman committed
7
8
9
10
11
12
13
14
15
namespace divine {
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";
}

Elwin Pater's avatar
Elwin Pater committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void dve_compiler::write_C(dve_expression_t & expr, std::ostream & ostr, std::string state_name)
{
    std::map< int, const char * > op;

    op[ T_LT ] = "<"; op[ T_LEQ ] = "<=";
    op[ T_EQ ] = "=="; op[ T_NEQ ] = "!=";
    op[ T_GT ] = ">"; op[ T_GEQ ] = ">=";

    op[ T_PLUS ] = "+"; op[ T_MINUS ] = "-";
    op[ T_MULT ] = "*"; op[ T_DIV ] = "/"; op[ T_MOD ] = "%";

    op[ T_AND ] = "&"; op[ T_OR ] = "|"; op[ T_XOR ] = "^";
    op[ T_LSHIFT ] = "<<"; op[ T_RSHIFT ] = ">>";

    op[ T_BOOL_AND ] = "&&"; op[ T_BOOL_OR ] = "||";

    op[ T_ASSIGNMENT ] = "=";

    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
39
40
            if (!(parent_table->get_variable(expr.get_ident_gid())->is_const()))
                ostr<<state_name<<".";
Elwin Pater's avatar
Elwin Pater committed
41
42
43
44
45
46
47
            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
48
            if (ltsmin) ostr << ".var";
Elwin Pater's avatar
Elwin Pater committed
49
50
51
52
53
54
            break;
        case T_FOREIGN_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
55
            if (ltsmin) ostr << ".var";
Elwin Pater's avatar
Elwin Pater committed
56
57
58
59
60
61
62
63
64
65
            break;
        case T_NAT:
            ostr << expr.get_value();
            break;
        case T_PARENTHESIS:
            ostr << "(";
            write_C(*expr.left(), ostr, state_name);
            ostr << ")";
            break;
        case T_SQUARE_BRACKETS:
Alfons Laarman's avatar
Alfons Laarman committed
66
67
            if (!(parent_table->get_variable(expr.get_ident_gid())->is_const()))
                ostr<<state_name<<".";
Elwin Pater's avatar
Elwin Pater committed
68
69
70
71
72
73
74
75
            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(); ostr<<"["; write_C(*expr.left(), ostr, state_name); ostr<<"]" ;
Michael Weber's avatar
Michael Weber committed
76
            if (ltsmin) ostr << ".var";
Elwin Pater's avatar
Elwin Pater committed
77
78
79
80
81
82
83
            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();
            ostr<<"["; write_C(*expr.left(), ostr, state_name); ostr<<"]";
Michael Weber's avatar
Michael Weber committed
84
            if (ltsmin) ostr << ".var";
Elwin Pater's avatar
Elwin Pater committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
            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: case T_ASSIGNMENT:
            write_C( *expr.left(), ostr, state_name );
            ostr << " " << op[ expr.get_operator() ] << " ";
            write_C( *expr.right(), ostr, state_name );
            break;

        case T_DOT:
            ostr<<state_name<<".";
            ostr<<parent_table->get_process(parent_table->get_state(expr.get_ident_gid())->
Michael Weber's avatar
Michael Weber committed
99
                                            get_process_gid())->get_name(); ostr<<".state"<<(ltsmin?".var":"")<<" == ";
Elwin Pater's avatar
Elwin Pater committed
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
            ostr<<parent_table->get_state(expr.get_ident_gid())->get_lid();
            break;

        case T_IMPLY:
            write_C(*expr.left(), ostr, state_name);
            ostr<<" -> "; // FIXME this looks wrong, -> in C is dereference
            write_C(*expr.right(), ostr, state_name);
            break;
        case T_UNARY_MINUS:
            ostr<<"-";
            write_C(*expr.right(), ostr, state_name);
            break;
        case T_TILDE:
            ostr<<"~";
            write_C(*expr.right(), ostr, state_name);
            break;
        case T_BOOL_NOT:
            ostr<<" ! (";
            write_C(*expr.right(), ostr, state_name);
            ostr<< " )";
            break;
        default:
            gerr << "Problem in expression - unknown operator"
                 << " number " << expr.get_operator() << psh();
    }
}

std::string dve_compiler::cexpr( dve_expression_t & expr, std::string state )
{
    std::stringstream str;
    str << "(";
    write_C( expr, str, state.c_str() );
    str << ")";
    return str.str();
}

void dve_compiler::gen_header()
{
    line( "#include <stdio.h>" );
    line( "#include <string.h>" );
    line( "#include <stdint.h>" );
    line();

Michael Weber's avatar
Michael Weber committed
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
    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;" );
        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
192
        line( compile_defines_str );
Michael Weber's avatar
Michael Weber committed
193
194
195
196
197
198
199
200
201
202
203
        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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
}

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
228
229
230
                    if (ltsmin) append("{");
                    append( cexpr( *((dve_expression_t*)var->get_init_expr(j)), "") );
                    if (ltsmin) append("}");
Elwin Pater's avatar
Elwin Pater committed
231
232
233
234
235
236
                    if (j!=(var->get_init_expr_count()-1))
                        append( ", " );
                    else
                        append( "}" );
                }
            } else if ( var->get_init_expr() ) {
Michael Weber's avatar
Michael Weber committed
237
238
239
240
                append( string( " = " ) );
                if (ltsmin) append(" {");
                append( cexpr( *((dve_expression_t*) var->get_init_expr()), "") );
                if (ltsmin) append("}");
Elwin Pater's avatar
Elwin Pater committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
            }
            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
265
                    else gerr << "Unexpected error generating state struct array" << thr();
Elwin Pater's avatar
Elwin Pater committed
266
267
268
269
270
271
272
                    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
273
                        line( "sshort_int_t " + name + ";" );
Elwin Pater's avatar
Elwin Pater committed
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
                    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
314
                    else gerr << "Unexpected error generating state struc channel" << thr();
Elwin Pater's avatar
Elwin Pater committed
315
316
317
318
319
320
                block_end();
                line( "content[" + fmt( symbol->get_channel_buffer_size() ) + "];" );
                block_end();
                line( "__attribute__((__packed__)) " + name + ";" );
            }
            break;
Michael Weber's avatar
Michael Weber committed
321
            default: gerr << "Unexpected error generating state struct" << thr();
Elwin Pater's avatar
Elwin Pater committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
                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
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
    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 {

        char sep[2] = "";
        char buf[10];
        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++)
                         {
                            append(sep); sprintf(sep,",");
                            sprintf(buf, "%d", (initial_values_counts[state_creators[i].gid]?
                                                initial_values[state_creators[i].gid].all_values[j]:0));
                            append(buf);
                         }
                    }
                    else
                    {
                            append(sep); sprintf(sep,",");
                            sprintf(buf, "%d", (initial_values_counts[state_creators[i].gid]?
                                                initial_values[state_creators[i].gid].all_value:0));
                            append(buf);
                    }
                }
                break;
                case state_creator_t::PROCESS_STATE:
                {
                    append(sep); sprintf(sep,",");
                    sprintf(buf, "%zu", initial_states[state_creators[i].gid]);
                    append(buf);
                }
                break;
                case state_creator_t::CHANNEL_BUFFER:
                {
                    // initialize channel to 0
                    append(sep); sprintf(sep,",");
                    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();
    }
}

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

    int count = count_state_variables();
    char buf[1024];

    append("// read : " );
    for(size_int_t i = 0; i < count; i++)
    {
        sprintf(buf, "%s%d", ((i==0)?"":","), ext_transition.sv_read[i]);
        append(buf);
    }
    line();

    append("// write: " );
    for(size_int_t i = 0; i < count; i++)
Elwin Pater's avatar
Elwin Pater committed
435
    {
Michael Weber's avatar
Michael Weber committed
436
437
        sprintf(buf, "%s%d", ((i==0)?"":","), ext_transition.sv_write[i]);
        append(buf);
Elwin Pater's avatar
Elwin Pater committed
438
439
440
441
    }
    line();
}

Michael Weber's avatar
Michael Weber committed
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
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
                mark_dependency(expr.get_ident_gid(), state_creator_t::VARIABLE, -1, dep);
                analyse_expression(*expr.left(), ext_transition, dep);
            }
            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:
            analyse_expression( *expr.left(), ext_transition, ext_transition.sv_read );
            analyse_expression( *expr.right(), ext_transition, ext_transition.sv_read );
            break;
        case T_ASSIGNMENT:
            analyse_expression( *expr.left(), ext_transition, ext_transition.sv_write );
            analyse_expression( *expr.right(), ext_transition, ext_transition.sv_read );
            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);
            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);
    ext_transition.sv_write.resize(count);

    // 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);
        }
    }

546
    if (have_property)
Michael Weber's avatar
Michael Weber committed
547
548
549
    {
        // mark process as read/write?
        mark_dependency(ext_transition.property->get_process_gid(),
550
                        state_creator_t::PROCESS_STATE, -1, ext_transition.sv_read);
Michael Weber's avatar
Michael Weber committed
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623

        // 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,
                                ext_transition.sv_write);
            analyse_expression( *(ext_transition.second->get_sync_expr_list_item(s)), ext_transition,
                                ext_transition.sv_read);
        }
    } 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(),
                            state_creator_t::CHANNEL_BUFFER, -1, ext_transition.sv_write);
            // 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);
            }
        }
        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);
            mark_dependency(ext_transition.first->get_channel_gid(),
                            state_creator_t::CHANNEL_BUFFER, -1, ext_transition.sv_write);
            // 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_write);
            }

        }

    }

    // mark process as read (write is probably in transition effect)
    mark_dependency(ext_transition.first->get_process_gid(),
                    state_creator_t::PROCESS_STATE, -1, ext_transition.sv_write);

    // analyse ext_transition->first
    for(size_int_t e = 0;e < ext_transition.first->get_effect_count();e++)
        analyse_expression( *(ext_transition.first->get_effect(e)), ext_transition,
        ext_transition.sv_read);

    // 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(),
                        state_creator_t::PROCESS_STATE, -1, ext_transition.sv_write);

        // analyse ext_transition->second
        for(size_int_t e = 0;e < ext_transition.second->get_effect_count();e++)
            analyse_expression( *(ext_transition.second->get_effect(e)), ext_transition,
            ext_transition.sv_read);
    }

624
    if (have_property)
Michael Weber's avatar
Michael Weber committed
625
626
627
628
629
630
631
    {
        // mark process as read/write?
        mark_dependency(ext_transition.property->get_process_gid(),
                        state_creator_t::PROCESS_STATE, -1, ext_transition.sv_write);
    }
}

Elwin Pater's avatar
Elwin Pater committed
632
633
634
635
636
637
638
639
640
641
642
643
644
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
645
            analyse_transition_dependencies(ext_transition);
Elwin Pater's avatar
Elwin Pater committed
646
647
648
649
650
651
652
653
654
655
656
657
658
659
            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
660
                analyse_transition_dependencies(ext_transition);
Elwin Pater's avatar
Elwin Pater committed
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
                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
688
                        analyse_transition_dependencies(ext_transition);
Elwin Pater's avatar
Elwin Pater committed
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
                        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
704
                            analyse_transition_dependencies(ext_transition);
Elwin Pater's avatar
Elwin Pater committed
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
                            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 )
{
    if_begin( false );
Michael Weber's avatar
Michael Weber committed
790
791
792
793
794
795

    // 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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
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
    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)
    {
        for(size_int_t s = 0;s < et->first->get_sync_expr_list_size();s++)
            assign( cexpr( *et->first->get_sync_expr_list_item(s), out ),
                    cexpr( *et->second->get_sync_expr_list_item(s), in ) );
    }
    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 ),
                        cexpr( *et->first->get_sync_expr_list_item( s ), in ) );
            }
            line( channel_items( chan, out ) + "++;" );
        }
        if(et->first->get_sync_mode() == SYNC_ASK_BUFFER)
        {
            for(size_int_t s = 0;s < et->first->get_sync_expr_list_size();s++)
                assign( cexpr( *et->first->get_sync_expr_list_item(s), out ),
                        channel_item_at( chan, "0", s, in ) );
            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 ) );
                assign( channel_item_at( chan, "i", s, out ), "0" );
            }
            block_end();
        }
    }

    //first transition effect
    assign( process_state( et->first->get_process_gid(), out ),
            fmt( et->first->get_state2_lid() ) );

    for(size_int_t e = 0;e < et->first->get_effect_count();e++)
        print_cexpr( *et->first->get_effect(e), out );

    if(et->synchronized) //second transiton effect
    {
        assign( process_state( et->second->get_process_gid(), out ),
                fmt( et->second->get_state2_lid() ) );
        for(size_int_t e = 0;e < et->second->get_effect_count();e++)
            print_cexpr( *et->second->get_effect(e), out );
    }

    if(have_property) //change of the property process state
        assign( process_state( et->property->get_process_gid(), out ),
                fmt( et->property->get_state2_lid() ) );
Michael Weber's avatar
Michael Weber committed
880
881
882

    // show dependency information in the source
    output_dependency_comment(*et);
Elwin Pater's avatar
Elwin Pater committed
883
884
885
}

void dve_compiler::new_output_state() {
Michael Weber's avatar
Michael Weber committed
886
887
888
889
890
891
892
893
    if (ltsmin) {
            line( "*out = *in;" );
    } 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
894
895
896
}

void dve_compiler::yield_state() {
Michael Weber's avatar
Michael Weber committed
897
898
899
900
901
    if (ltsmin) {
        if (many) {
            line ("transition_info.group = " + fmt( current_label++) + ";");
        }
        line( "callback(arg, &transition_info, out);" );
Elwin Pater's avatar
Elwin Pater committed
902
903
        line( "++states_emitted;" );
    } else {
Michael Weber's avatar
Michael Weber committed
904
905
906
907
908
909
910
911
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
944
945
946
947
948
949
950
951
        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 ) + ";" );
        }
    }
}

void dve_compiler::gen_ltsmin_successors()
{
    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 ) )
                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))
                    {
                        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()) )
                            {
952
953
954
955
956
957
958
959
960
961
962
963
964
                                // only generate if not synchonized or synchonized with a committed transition
                                new_label();

                                // committed state
                                if_begin( true );

                                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 ) );

                                if_end(); // otherwise this condition is disjoint with the new condition

Michael Weber's avatar
Michael Weber committed
965
966
                                transition_guard( &*iter_ext_transition_vector, in );
                                block_begin();
967
                                new_output_state();
Michael Weber's avatar
Michael Weber committed
968
                                transition_effect( &*iter_ext_transition_vector, in, out );
969
                                yield_state();
Michael Weber's avatar
Michael Weber committed
970
                                block_end();
971
972

                                line("return states_emitted;");
Michael Weber's avatar
Michael Weber committed
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
                            }
                        }
                    }
                }
        }
    }

    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++)
            {
                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++)
                {
                    // 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()) )
                    {

                        new_label();

                        transition_guard( &*iter_ext_transition_vector, in );
                        block_begin();
                        if (some_commited_state)
                        {
                            // committed state
                            if_begin( true );

                            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 ) );

                            if_end();
                            line("    return 0;"); // bail out early
                        }


                        new_output_state();
                        transition_effect( &*iter_ext_transition_vector, in, out );
                        yield_state();
                        block_end();
                        line("return states_emitted;");
                    }
                }
            }
Elwin Pater's avatar
Elwin Pater committed
1025
1026
1027
    }
}

Michael Weber's avatar
Michael Weber committed
1028

Elwin Pater's avatar
Elwin Pater committed
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
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
1042
    if_end(); block_begin(); // committed states
Elwin Pater's avatar
Elwin Pater committed
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070

    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
1071
1072
                            new_label();

Elwin Pater's avatar
Elwin Pater committed
1073
1074
                            transition_guard( &*iter_ext_transition_vector, in );
                            block_begin();
Alfons Laarman's avatar
Alfons Laarman committed
1075
                            new_output_state();
Elwin Pater's avatar
Elwin Pater committed
1076
                            transition_effect( &*iter_ext_transition_vector, in, out );
Alfons Laarman's avatar
Alfons Laarman committed
1077
                            yield_state();
Elwin Pater's avatar
Elwin Pater committed
1078
1079
1080
1081
1082
1083
1084
1085
1086
                            block_end();
                        }
                    }

                    block_end();
                }
            }
    }

Alfons Laarman's avatar
Alfons Laarman committed
1087
1088
1089
    new_label(); // Trick. : - )
    line( ";" );

Elwin Pater's avatar
Elwin Pater committed
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
    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
1101
1102
1103
1104
                // 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
1105
                {
Michael Weber's avatar
Michael Weber committed
1106

Elwin Pater's avatar
Elwin Pater committed
1107
                    new_label();
Michael Weber's avatar
Michael Weber committed
1108
1109
                    if_begin( true );
                    if_clause( in_state( i, iter_process_transition_map->first, in ) );
Elwin Pater's avatar
Elwin Pater committed
1110

Michael Weber's avatar
Michael Weber committed
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
                    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 );
                        if (!ltsmin) line( "system_in_deadlock = false;" );
                        yield_state();
                        block_end();
                    }
Elwin Pater's avatar
Elwin Pater committed
1127
1128
1129
1130
1131
1132
                    block_end();
                }
            }
    }
    block_end();

Michael Weber's avatar
Michael Weber committed
1133
    if (!ltsmin)
Elwin Pater's avatar
Elwin Pater committed
1134
1135
1136
    {
        new_label();

Michael Weber's avatar
Michael Weber committed
1137
1138
        if_begin( true );
        if_clause( "system_in_deadlock" );
Elwin Pater's avatar
Elwin Pater committed
1139
1140
        if_end(); block_begin();

Michael Weber's avatar
Michael Weber committed
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
        for(iter_property_transitions = property_transitions.begin();
            iter_property_transitions != property_transitions.end();
            iter_property_transitions++)
        {
            new_label();
            if_begin( false );

            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 );
Elwin Pater's avatar
Elwin Pater committed
1151

Michael Weber's avatar
Michael Weber committed
1152
1153
1154
1155
1156
1157
1158
1159
1160
            if_end(); block_begin();
            new_output_state();

            assign( process_state( (*iter_property_transitions)->get_process_gid(), in ),
                    fmt( (*iter_property_transitions)->get_state2_lid() ) );

            yield_state();
            block_end();
        }
Elwin Pater's avatar
Elwin Pater committed
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
        block_end();
    }
}

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
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
    if (ltsmin) {
        line( "extern \"C\" int buchi_is_accepting( void* model, char *_state )" );
        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
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
1208

    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
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
    if (ltsmin) {
        line( "extern \"C\" void get_initial_state( char *to )" );
        block_begin();
        line( "memcpy(to, (char*)&initial_state, state_size);" );
        block_end();
        line();

        line( "extern \"C\" bool have_property()" );
        block_begin();
        if (have_property) {
            line("return true;");
        } else {
            line("return false;");
        }
        block_end();
        line();

Elwin Pater's avatar
Elwin Pater committed
1226
1227
        gen_is_accepting();

Michael Weber's avatar
Michael Weber committed
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
        many = false;
        current_label = 0;
        line( "extern \"C\" int get_successor( void* model, int t, state_struct_t *in, void (*callback)(void* arg, transition_info_t *transition_info, state_struct_t *out), void *arg ) " );
        block_begin();
        line( "transition_info_t transition_info = { NULL, t };" );
        line( "(void)model; // ignore model" );
        line( "int states_emitted = 0;" );
        line( "state_struct_t tmp;" );
        line( "state_struct_t *out = &tmp;" );
        line( "goto switch_state;" );
        gen_ltsmin_successors();
        // switch block
        line( "switch_state: switch( t )" );
        block_begin();
        for(int i=0; i < current_label; i++)
                line( "case " + fmt( i ) + ": goto l" + fmt( i ) + ";" );
        block_end();
        line("return 0;");
        // end switch block
        block_end();
        line();

        many = true;
        current_label = 0;

        line( "extern \"C\" int get_successors( void *model, state_struct_t *in, void (*callback)(void *arg, transition_info_t *transition_info, state_struct_t *out), void *arg ) " );
        block_begin();
        line( "(void)model; // ignore model" );
        line( "transition_info_t transition_info = { NULL, -1 };" );
        line( "int states_emitted = 0;" );
        line( "state_struct_t tmp;" );
        line( "state_struct_t *out = &tmp;" );
        gen_successors();
        line( "return states_emitted;" );
        block_end();
        line();
Alfons Laarman's avatar
Alfons Laarman committed
1264

Michael Weber's avatar
Michael Weber committed
1265
1266
1267
1268
1269
1270
1271
1272
        // state descriptors
        gen_state_info();
        gen_transition_info();
    } else {
        line( "extern \"C\" int setup( CustomSetup *setup ) {" );
        line( "    setup->state_size = state_size;" );
        line( "    setup->has_property = " + fmt( get_with_property() ) + ";" );
        line( "}" );
Elwin Pater's avatar
Elwin Pater committed
1273

Michael Weber's avatar
Michael Weber committed
1274
1275
1276
1277
1278
1279
        line( "extern \"C\" void get_initial( CustomSetup *setup, Blob *out ) {" );
        line( "    Blob b( *(setup->pool), state_size + setup->slack );" );
        line( "    memcpy(b.data() + setup->slack, initial_state, state_size);" );
        line( "    *out = b;" );
        line( "}" );
        line();
Elwin Pater's avatar
Elwin Pater committed
1280

Michael Weber's avatar
Michael Weber committed
1281
1282
        many = false;
        current_label = 1;
Elwin Pater's avatar
Elwin Pater committed
1283

Michael Weber's avatar
Michael Weber committed
1284
        gen_is_accepting();
Elwin Pater's avatar
Elwin Pater committed
1285

Michael Weber's avatar
Michael Weber committed
1286
1287
1288
1289
1290
        line( "extern \"C\" int get_successor( CustomSetup *setup, int next_state, Blob from, Blob *to ) " );
        block_begin();
        line( "state_struct_t *in = &from.get< state_struct_t >( setup->slack );" );
        line( "bool system_in_deadlock = false;" );
        line( "goto switch_state;" );
Elwin Pater's avatar
Elwin Pater committed
1291

Michael Weber's avatar
Michael Weber committed
1292
        gen_successors();
Elwin Pater's avatar
Elwin Pater committed
1293

Michael Weber's avatar
Michael Weber committed
1294
1295
        new_label();
        line( "return 0;" );
Elwin Pater's avatar
Elwin Pater committed
1296

Michael Weber's avatar
Michael Weber committed
1297
1298
1299
1300
1301
1302
1303
1304
        line( "switch_state: switch( next_state )" );
        block_begin();
        for(int i=1; i < current_label; i++)
            if (i==1)
                line( "case " + fmt( i ) + ": system_in_deadlock = true; goto l" + fmt( i ) + ";" );
            else
                line( "case " + fmt( i ) + ": goto l" + fmt( i ) + ";" );
        block_end();
Elwin Pater's avatar
Elwin Pater committed
1305

Michael Weber's avatar
Michael Weber committed
1306
1307
1308
1309
        block_end();

        // many = true;
        // current_label = 0;
Alfons Laarman's avatar
Alfons Laarman committed
1310
#if 0
Michael Weber's avatar
Michael Weber committed
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
        line( "extern \"C\" void get_many_successors( int slack, char *_pool, char *," );
        line( "                                       char *_buf_in, char *_buf_outf, char *_buf_outs ) " );
        block_begin();
        line( "divine::Pool *pool = (divine::Pool *) _pool;" );
        line( "typedef divine::Circular< divine::Blob, 0 > Buffer;" );
        line( "Buffer *buf_in = (Buffer *) _buf_in;" );
        line( "Buffer *buf_out = (Buffer *) _buf_out;" );
        line( "int states_emitted;" );
        line( "bool system_in_deadlock;" );
        line( "state_struct_t *in;" );

        line( "next:" );
        line( "system_in_deadlock = true;" );
        line( "states_emitted = 0;" );
        line( "in = (state_struct_t*) ((*buf_in)[ 0 ].data() + slack);" );
        gen_successors();
        line( "buf_in->drop( 1 );" );
        line( "if ( buf_in->empty() ) return;" );
        line( "goto next;" );
        block_end();
#endif
    }
}

void dve_compiler::gen_state_info()
{
    char buf[1024];
    // number of variables in the state
    line( "extern \"C\" int get_state_variable_count() " );
Elwin Pater's avatar
Elwin Pater committed
1340
    block_begin();
Michael Weber's avatar
Michael Weber committed
1341
1342
    sprintf(buf, "return %d;", count_state_variables());
    line(buf);
Elwin Pater's avatar
Elwin Pater committed
1343
    block_end();
Michael Weber's avatar
Michael Weber committed
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
    line();

    // name of state variables
    line( "extern \"C\" const char* get_state_variable_name(int var)" );
    block_begin();

    line("switch (var)");
    block_begin();

    // iterate over state variables, output name per variable
    bool global = true;
    string name = "UNINITIALIZED";
    string process_name = "UNINITIALIZED";
    int k=0;
    for (size_int_t i=0; i<state_creators_count; ++i, ++k)
    {
        sprintf(buf, "case %d:", k);
        line(buf);

        switch (state_creators[i].type)
        {
            case state_creator_t::VARIABLE:
                name = (global?"":process_name + ".") + 
                        get_symbol_table()->get_variable(state_creators[i].gid)->get_name();

                if (state_creators[i].array_size)
                {
                    for(size_int_t j=0; j < state_creators[i].array_size; ++j)
                    {
                        sprintf(buf, "    return \"%s[%zu]\";", name.c_str(), j);
                        line(buf);
                        if (j < state_creators[i].array_size - 1) {
                            sprintf(buf, "case %d:", ++k);
                            line(buf);
                        }
                    }
                    continue;
                }
                break;
            case state_creator_t::PROCESS_STATE:
                global = false;
                name = get_symbol_table()->get_process(state_creators[i].gid)->get_name();
                process_name = name;
                break;
            case state_creator_t::CHANNEL_BUFFER:
            {
                name = get_symbol_table()->get_channel(state_creators[i].gid)->get_name();
                sprintf(buf, "    return \"%s.number_of_items\";", name.c_str());
                line(buf);

                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)
                    {
                        sprintf(buf, "case %d:", ++k);
                        line(buf);
                        sprintf(buf, "    return \"%s[%zu].x%zu\";", name.c_str(), i,j);
                        line(buf);
                    }
                }
                continue;
            }
            default: gerr << "Unexpected error generating variable names" << thr();
                break;
        };

        sprintf(buf, "    return \"%s\";", name.c_str());
        line(buf);
    }

    line("default:");
    line("    return NULL;");
    block_end();

    block_end();
    line();

    // gather type information
    std::map<string, int> type_no;
    std::map<string, std::vector<string> > type_value;
    int type_count = 0;
    for (size_int_t i=0; i<state_creators_count; ++i, ++k)
    {
        vector<string> values;
        values.clear();
        string type_name = "UNINITIALIZED";

        switch (state_creators[i].type)
        {
            case state_creator_t::VARIABLE:
            {
                dve_symbol_t * var = get_symbol_table()->get_variable(state_creators[i].gid);
                if (var->is_byte()) { type_name = "byte"; } else { type_name = "int"; };
                break;
            }
            case state_creator_t::PROCESS_STATE:
            {
                type_name = get_symbol_table()->get_process(state_creators[i].gid)->get_name();
                for(size_int_t j = 0;
                    j < dynamic_cast<dve_process_t*>(this->get_process(state_creators[i].gid))->get_state_count();
                    j++)
                {
                    // add to possible process values
                    values.push_back(
                        get_symbol_table()->get_state(
                        dynamic_cast<dve_process_t*>(this->get_process(state_creators[i].gid))->get_state_gid(j))->get_name()
                    );
                }
                break;
            }
            case state_creator_t::CHANNEL_BUFFER:
            {
                // the int type is added for the channel by default (number of items is int)
                // iterate over the channel, if type byte is used, add it also
                type_name = "byte";

                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)
                    {
                        // check existence of byte type
                        if (type_no.find(type_name) == type_no.end()) {
                            type_no[type_name] = type_count++;
                            type_value[type_name] = values;
                            // if byte exists, the only other possibility is int, which is added
                            // by default
                            break;
                        }
                    }
                }
                // byte migth be added ab
                type_name = "int";
                break;
            }
            default: gerr << "Unexpected error while gathering type information" << thr();
                break;
        }
        if (type_no.find(type_name) == type_no.end())
        {
            type_no[type_name] = type_count++;
            type_value[type_name] = values;
        }
    }

    // name of state variables
    line( "extern \"C\" int get_state_variable_type(int var)" );
    block_begin();

    line("switch (var)");
    block_begin();

    // iterate over state variables, output name per variable
    k = 0;
    for (size_int_t i=0; i<state_creators_count; ++i, ++k)
    {
        string type_name = "UNINITIALIZED";

        sprintf(buf, "case %d:", k);
        line(buf);

        switch (state_creators[i].type)
        {
            case state_creator_t::VARIABLE:
            {
                dve_symbol_t * var = get_symbol_table()->get_variable(state_creators[i].gid);
                if (var->is_byte()) { type_name = "byte"; } else { type_name = "int"; };

                if (state_creators[i].array_size)
                {
                    for(size_int_t j=0; j < state_creators[i].array_size - 1; ++j)
                    {
                        sprintf(buf, "    return %d;", type_no[type_name], j);
                        line(buf);
                        sprintf(buf, "case %d:", ++k);
                        line(buf);
                    }
                }
                break;
            }
            case state_creator_t::PROCESS_STATE:
                type_name = get_symbol_table()->get_process(state_creators[i].gid)->get_name();
                break;
            case state_creator_t::CHANNEL_BUFFER:
            {
                sprintf(buf, "    return %d;", type_no["int"]);
                line(buf);

                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)
                    {
                        sprintf(buf, "case %d:", ++k);
                        line(buf);
                        if (symbol->get_channel_type_list_item(j) == VAR_BYTE)
                        {
                            sprintf(buf, "    return %d;", type_no["byte"]);
                            line(buf);
                        } else {
                            sprintf(buf, "    return %d;", type_no["int"]);
                            line(buf);
                        }
                    }
                }
                continue;
            }
            default: gerr << "Unexpected error while writing name per variable" << thr();
                break;
        };

        sprintf(buf, "    return %d;", type_no[type_name]);
        line(buf);
    }

    line("default:");
    line("    return -1;");
    block_end();

    block_end();
    line();


    // number of different types in the state
    line( "extern \"C\" int get_state_variable_type_count() " );
    block_begin();
    sprintf(buf, "return %d;", type_count);
    line(buf);
    block_end();
    line();

    // names of types
    line( "extern \"C\" const char* get_state_variable_type_name(int type) " );
    block_begin();
        line("switch (type)");
        block_begin();
        for(std::map<string, int>::iterator ix = type_no.begin(); ix != type_no.end(); ++ix)
        {
            sprintf(buf, "case %d:", ix->second);
            line(buf);
            sprintf(buf, "    return \"%s\";", ix->first.c_str());
            line(buf);
        }
        line("default:");
        line("    return NULL;");
        block_end();
    block_end();
    line();

    // number of different values of a type
    line( "extern \"C\" int get_state_variable_type_value_count(int type)" );
    block_begin();
        line("switch (type)");
        block_begin();
        for(std::map<string, int>::iterator ix = type_no.begin(); ix != type_no.end(); ++ix)
        {
            sprintf(buf, "case %d: // %s", ix->second, ix->first.c_str());
            line(buf);
            sprintf(buf, "    return %zu;", type_value[ix->first].size());
            line(buf);
        }
        line("default:");
        line("    return -1;");
        block_end();
    block_end();
    line();

    // values of types
    line( "extern \"C\" const char* get_state_variable_type_value(int type, int value) " );
    block_begin();
        line("switch (type)");
        block_begin();
        for(std::map<string, int>::iterator ix = type_no.begin(); ix != type_no.end(); ++ix)
        {
            if (type_value[ix->first].size())
            {
                sprintf(buf, "case %d:", ix->second);
                line(buf);
                block_begin();
                    line("switch (value)");
                    block_begin();
                    for(int i=0; i < type_value[ix->first].size(); ++i)
                    {
                        sprintf(buf, "case %d:", i);
                        line(buf);
                        sprintf(buf, "    return \"%s\";", type_value[ix->first][i].c_str());
                        line(buf);
                    }
                    block_end();
                block_end();
            }
        }
        block_end();
        line("return NULL;");
    block_end();
    line();


}

1653
void dve_compiler::fill_transition_vector(std::vector<ext_transition_t>& transitions)
Michael Weber's avatar
Michael Weber committed
1654
{
1655
    // true if committed states are found
Michael Weber's avatar
Michael Weber committed
1656
1657
    bool some_commited_state = false;

1658
1659
    // committed transitions
    for(size_int_t i = 0; i < this->get_process_count(); i++)
Michael Weber's avatar
Michael Weber committed
1660
    {
1661
1662
1663
1664
1665
1666
1667
        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))
Michael Weber's avatar
Michael Weber committed
1668
                {
1669
1670
1671
                    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
1672
                    {
1673
1674
1675
1676
1677
                        // !! 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
1678
                        {
1679
1680
1681
1682
1683
                            // store transition in map
                            ext_transition_t tmp = *iter_ext_transition_vector;
                            tmp.commited = 1;
                            transitions.push_back(tmp);
                            trans_count++;
Michael Weber's avatar
Michael Weber committed
1684
1685
1686
                        }
                    }
                }
1687
            }
Michael Weber's avatar
Michael Weber committed
1688
    }
1689
1690

    // normal transitions
Michael Weber's avatar
Michael Weber committed
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
    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++)
            {
                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++)
                {
                    // 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()) )
                    {
1708
1709
1710
                        ext_transition_t tmp = *iter_ext_transition_vector;
                        tmp.commited = 0;
                        transitions.push_back(tmp);
Michael Weber's avatar
Michael Weber committed
1711
1712
1713
1714
1715
                        trans_count++;
                    }
                }
            }
    }
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
}

void dve_compiler::gen_transition_info()
{
    int sv_count = count_state_variables();
    char buf[1024];

    // initialize read/write dependency vector
    std::vector<int> c_base_sv_read(sv_count);
    std::vector<ext_transition_t> transitions;
    fill_transition_vector(transitions);

    // mark commited processes as read
    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))
                mark_dependency(dynamic_cast<dve_process_t*>(get_process(i))->get_gid(),
                                state_creator_t::PROCESS_STATE, -1, c_base_sv_read);

    // output transition vectors
    sprintf(buf, "int transition_dependency[][2][%d] = ", sv_count);
    line(buf);
    block_begin();
    line("// { ... read ...}, { ... write ...}");

    for(size_int_t i = 0; i < transitions.size(); i++) {
        ext_transition_t& current = transitions[i];
        if (i!=0) { line(","); }
        append("{{" );
        for(size_int_t j = 0; j < sv_count; j++)
        {
            sprintf(buf, "%s%d", ((j==0)?"":","), max(c_base_sv_read[j], current.sv_read[j]) );
            append(buf);
        }
        append("},{" );
        for(size_int_t j = 0; j < sv_count; j++)
        {
            sprintf(buf, "%s%d", ((j==0)?"":","), current.sv_write[j] );
            append(buf);
        }
        append("}}");
    }
Michael Weber's avatar
Michael Weber committed
1758
1759
1760
1761
1762
1763
1764
1765
    line();
    block_end();
    line(";");
    line();

    // number of transitions
    line( "extern \"C\" int get_transition_count() " );
    block_begin();
1766
    sprintf(buf, "return %zu;", transitions.size());
Michael Weber's avatar
Michael Weber committed
1767
1768
1769
1770
1771
1772
1773
    line(buf);
    block_end();
    line();

    // read dependencies
    line( "extern \"C\" const int* get_transition_read_dependencies(int t) " );
    block_begin();
1774
    sprintf(buf, "if (t>=0 && t < %zu) return transition_dependency[t][0];", transitions.size());
Michael Weber's avatar
Michael Weber committed
1775
1776
1777
1778
1779
1780
1781
1782
1783
    line(buf);
    sprintf(buf, "return NULL;");
    line(buf);
    block_end();
    line();

    // write dependencies
    line( "extern \"C\" const int* get_transition_write_dependencies(int t) " );
    block_begin();
1784
    sprintf(buf, "if (t>=0 && t < %zu) return transition_dependency[t][1];", transitions.size());
Michael Weber's avatar
Michael Weber committed
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
    line(buf);
    sprintf(buf, "return NULL;");
    line(buf);
    block_end();
    line();

}


void dve_compiler::mark_dependency( size_int_t gid, int type, int idx, std::vector<int> &dep )
{
    size_int_t size = 0;
    bool mark = false;
    for (size_int_t i=0; i!=state_creators_count; ++i)
    {
        mark = state_creators[i].gid == gid && type == state_creators[i].type;
        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++)
                     {
                        if (mark && (idx == -1 || idx == j)) dep[size]=1;
                        size++;
                     }
                }
                else
                {
                    if (mark) { dep[size]=1; }
                    size++;
                }
            }
            break;
            case state_creator_t::PROCESS_STATE:
            {
                if (mark) { dep[size]=1; }
                size++;
            }
            break;
            case state_creator_t::CHANNEL_BUFFER:
            {
                // mark number of items
                if (mark) dep[size]=1;
                size++;

                // mark channel
                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) {
                        if (mark) dep[size]=1;
                        size++;
                    }
                }
            }
            break;
            default: gerr << "Unexpected error while marking dependency" << thr();
                break;
        };
    }
}

int dve_compiler::count_state_variables()
{
    size_int_t size = 0;
    for (size_int_t i=0; i!=state_creators_count; ++i)
    {
        switch (state_creators[i].type)
        {
            case state_creator_t::VARIABLE:
                size += (state_creators[i].array_size)?state_creators[i].array_size:1;
                break;
            case state_creator_t::PROCESS_STATE:
                size++;
                break;
            case state_creator_t::CHANNEL_BUFFER:
            {
                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();
                size += (chan_size * item_count) + 1;
                break;
            }
            default: gerr << "Unexpected error while counting length of state" << thr();
                break;
        };
    }
    return size;
Elwin Pater's avatar
Elwin Pater committed
1878
}