preprocessing_backend.c 47.5 KB
Newer Older
Maxime Perrotin's avatar
Maxime Perrotin committed
1
/* Buildsupport is (c) 2008-2016 European Space Agency
Maxime Perrotin's avatar
Maxime Perrotin committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15
 * contact: maxime.perrotin@esa.int
 * License is LGPL, check LICENSE file */
/* preprocessing_backend.c

  this backend "pre-processes" the interface view,
  to perform the following operations:
  1) if the FV is a GUI => creates a new separate,
      cyclic FV that is used to poll the GUI queue
  2) if the FV mixes cyclic/sporadic provided interfaces,
     put all the operations in a protected object and create
     one thread per cyclic/sporadic entry
  3) For each PRO/UNPRO RI of each FV,
     attach the RI to the calling thread (SPO/Cyclic FV)
  4) If some nodes use timers, generate the code for a timer
Maxime Perrotin's avatar
Maxime Perrotin committed
16 17
     manager (one per partition).
  5) Set the "ignore_params" flag on interfaces that run on the same node
Maxime Perrotin's avatar
Maxime Perrotin committed
18
  6) Create a TASTE API that provides useful features to User functions
Maxime Perrotin's avatar
Maxime Perrotin committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 */


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <assert.h>
#include <stdbool.h>

#include "my_types.h"
#include "practical_functions.h"
#include "c_ast_construction.h"

static int thread_id = 0;

// Files used for the timer management
FILE *header = NULL, *code = NULL;

Maxime Perrotin's avatar
Maxime Perrotin committed
38 39 40



Maxime Perrotin's avatar
Maxime Perrotin committed
41 42 43 44 45 46 47 48
/* Create a new function to manage the timers of a given node */
FV *Add_timer_manager(Process *node, FV_list *fv_with_timer)
{
    FV *fv = NULL;
    Interface *interface = NULL;
    char *name = NULL;
    char *path = NULL;
    FILE *hook = NULL;
49
    int  timer_res = get_context()->timer_resolution;
Maxime Perrotin's avatar
Maxime Perrotin committed
50 51 52 53 54 55 56 57 58 59 60

    name = make_string ("%s_timer_manager", node->name);

    fv = (FV *) New_FV (name, strlen(name), name);

    Set_Language_To_C();

    Create_Interface (&interface);

    assert (NULL != fv && NULL != interface);

61
    interface->name = make_string ("tick_%dms", timer_res);
Maxime Perrotin's avatar
Maxime Perrotin committed
62 63

    interface->distant_fv = NULL;
Maxime Perrotin's avatar
Maxime Perrotin committed
64 65 66
    interface->direction = PI;
    interface->synchronism = asynch;
    interface->rcm = cyclic;
67 68
    /* clock rate for timers is 100 ms by default */
    interface->period = timer_res;
Maxime Perrotin's avatar
Maxime Perrotin committed
69
    interface->parent_fv = fv;
70 71
    interface->wcet_high = 1;
    interface->wcet_low = 1;
Maxime Perrotin's avatar
Maxime Perrotin committed
72 73 74 75 76 77 78 79 80 81 82 83
    interface->wcet_low_unit = make_string ("ms");
    interface->wcet_high_unit = make_string ("ms");
    APPEND_TO_LIST (Interface, fv->interfaces, interface);
    /* Set flag indicating that this function was created during VT */
    fv->timer = true;

    End_FV();

    /* Add the new FV to the binding list of the Process */
    Set_Current_Process (node);
    Add_Binding(name, strlen(name));

Maxime Perrotin's avatar
Maxime Perrotin committed
84 85
   /* Create a file called "_hook" to add this function
    * to the orchestrator work */
Maxime Perrotin's avatar
Maxime Perrotin committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    path = make_string ("%s/%s", OUTPUT_PATH, fv->name);

    create_file (path, "_hook", &hook);
    create_file (path, make_string ("%s.h", fv->name), &header);
    create_file (path, make_string ("%s.c", fv->name), &code);

    fprintf (header, "/* Timer manager */\n%s", do_not_modify_warning);
    fprintf (code, "/* Timer manager */\n%s", do_not_modify_warning);

    fprintf (header, "#ifndef __AUTO_CODE_H_%s__\n"
                     "#define __AUTO_CODE_H_%s__\n\n"
                     "#include \"C_ASN1_Types.h\"\n"
                     "#ifdef __cplusplus\n"
                     "extern \"C\" {\n"
                     "#endif\n\n",
                     fv->name,
                     fv->name);

    fprintf (header, "void %s_startup();\n\n"
105
                     "void %s_PI_tick_%dms();\n\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
106
                     fv->name,
107 108
                     fv->name,
                     timer_res);
Maxime Perrotin's avatar
Maxime Perrotin committed
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
    fprintf (code, "#include <assert.h>\n\n"
                   "#include \"%s.h\"\n\n", fv->name);

    /* Create a list of all timers present in the current node */
    String_list *all_timers = NULL;
    int count = 0;
    FOREACH (function, FV, fv_with_timer, {
        FOREACH(timer, String, function->timer_list, {
            APPEND_TO_LIST(String,
                           all_timers,
                           make_string("%s_%s", function->name, timer));
            count ++;
        });
    });

    /* In the header file, generate an enum used as array index */
    fprintf (header, "typedef enum {\n");
    bool first = true;
    int idx = 0;
    FOREACH (timer, String, all_timers, {
        fprintf(header, "%s%s = %d",
                        first?"    ": ",\n    ",
                        timer, idx++);
        first = false;
    });
    fprintf (header, "\n} %s_timers;\n\n", fv->name);

    /* In the code, declare a static variable holding the timers state */
    fprintf (code, "static struct {\n"
138
                     "    long long value;\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
139 140 141 142 143 144 145 146 147 148 149
                     "    enum { active, inactive } state;\n"
                     "} timers[%d] = {"
                     " { .value = 0, .state = inactive } };\n\n", count);

    /* Initialisation code of the timer manager - nothing special */
    fprintf (code, "void %s_startup()\n"
                   "{\n"
                   "    /* Timer start up (nothing to do) */\n"
                   "}\n\n", fv->name);

    /* Generate the timer manager code for the cyclic interface */
150 151 152 153
    fprintf (code, "void %s_PI_tick_%dms()\n"
                   "{\n",
                   fv->name,
                   timer_res);
Maxime Perrotin's avatar
Maxime Perrotin committed
154 155 156

    FOREACH (timer, String, all_timers, {
        fprintf (code, "    if (timers[%s].state == active && "
157
                       "1 == __sync_fetch_and_sub(&timers[%s].value, 1)) {\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
                       "        %s_RI_%s();\n"
                       "        timers[%s].state = inactive;\n"
                       "    }\n\n",
                       timer,
                       timer,
                       fv->name,
                       timer,
                       timer);
    });

    fprintf (code, "}\n\n");

    close_file (&hook);
    free (path);

    return fv;
}


void Add_timers_to_function (FV *fv, FV *timer_manager)
{
    Interface *expire = NULL, *set_timer = NULL, *reset_timer = NULL;
    Parameter *param = NULL;
181
    Interface *cyclic_pi = NULL;
182 183
    int timer_res = get_context()->timer_resolution;
    char *cyclic_name = make_string("tick_%dms", timer_res);
Maxime Perrotin's avatar
Maxime Perrotin committed
184 185 186

    assert (NULL != header && NULL != code);

187 188
    /* Find the cyclic interface of the timer manager */
    FOREACH(pi, Interface, timer_manager->interfaces, {
189
        if (!strcmp(pi->name, cyclic_name)) cyclic_pi = pi;
190 191 192
    });


Maxime Perrotin's avatar
Maxime Perrotin committed
193 194
    FOREACH (timer, String, fv->timer_list, {
        /* Declare functions in timer manager code */
195 196 197 198 199
        fprintf (header, "void %s_PI_%s_SET_%s(const asn1SccT_UInt32 *val);\n\n",
                         timer_manager->name,
                         fv->name,
                         timer);
        fprintf (code, "void %s_PI_%s_SET_%s(const asn1SccT_UInt32 *val)\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
200
                       "{\n"
201 202
                       "    /* Timer value must be multiple of %d ms */\n"
                       "    assert (*val %% %d == 0);\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
203
                       "    timers[%s_%s].state = active;\n"
204
                       "    __sync_lock_test_and_set(&timers[%s_%s].value, *val / %d);\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
205 206
                       "}\n\n",
                       timer_manager->name,
207
                       fv->name,
Maxime Perrotin's avatar
Maxime Perrotin committed
208
                       timer,
209 210
                       timer_res,
                       timer_res,
Maxime Perrotin's avatar
Maxime Perrotin committed
211 212 213
                       fv->name,
                       timer,
                       fv->name,
214 215
                       timer,
                       timer_res);
216 217 218 219 220
        fprintf (header, "void %s_PI_%s_RESET_%s();\n\n",
                         timer_manager->name,
                         fv->name,
                         timer);
        fprintf (code, "void %s_PI_%s_RESET_%s()\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
221 222 223 224
                       "{\n"
                       "    timers[%s_%s].state = inactive;\n"
                       "}\n\n",
                       timer_manager->name,
225
                       fv->name,
Maxime Perrotin's avatar
Maxime Perrotin committed
226 227 228
                       timer,
                       fv->name,
                       timer);
Maxime Perrotin's avatar
Maxime Perrotin committed
229

Maxime Perrotin's avatar
Maxime Perrotin committed
230 231 232
        /* Add sporadic PI - corresponding to the timer expiration */
        expire = NULL;
        Create_Interface (&expire);
Maxime Perrotin's avatar
Maxime Perrotin committed
233 234 235 236 237 238
        expire->name           = make_string (timer);
        expire->distant_name   = make_string ("%s_%s", fv->name, timer);
        expire->distant_fv     = make_string (timer_manager->name);
        expire->direction      = PI;
        expire->synchronism    = asynch;
        expire->rcm            = sporadic;
239
        expire->period         = timer_res;
Maxime Perrotin's avatar
Maxime Perrotin committed
240 241 242 243
        expire->parent_fv      = fv;
        expire->wcet_high      = 10;
        expire->wcet_low       = 10;
        expire->wcet_low_unit  = make_string ("ms");
Maxime Perrotin's avatar
Maxime Perrotin committed
244 245
        expire->wcet_high_unit = make_string ("ms");
        APPEND_TO_LIST (Interface, fv->interfaces, expire);
Maxime Perrotin's avatar
Maxime Perrotin committed
246

Maxime Perrotin's avatar
Maxime Perrotin committed
247 248
        /* Add corresponding RI in the timer manager */
        expire = Duplicate_Interface (RI, expire, timer_manager);
Maxime Perrotin's avatar
Maxime Perrotin committed
249 250
        expire->name            = expire->distant_name;
        expire->distant_name    = make_string (timer);
Maxime Perrotin's avatar
Maxime Perrotin committed
251
        free (expire->distant_fv);
Maxime Perrotin's avatar
Maxime Perrotin committed
252
        expire->distant_fv      = make_string (fv->name);
253 254
        /* Filter: set the list of calling PIs in the timer manager */
        ADD_TO_SET(Interface, expire->calling_pis, cyclic_pi);
Maxime Perrotin's avatar
Maxime Perrotin committed
255
        APPEND_TO_LIST (Interface, timer_manager->interfaces, expire);
Maxime Perrotin's avatar
Maxime Perrotin committed
256

257
        /* Add Protected PI "RESET_timer" to timer manager (no param) */
Maxime Perrotin's avatar
Maxime Perrotin committed
258 259
        reset_timer = Duplicate_Interface (PI, expire, timer_manager);
        free (reset_timer->name);
260 261 262
        reset_timer->name         = make_string ("%s_RESET_%s",
                                                 fv->name,
                                                 timer);
Maxime Perrotin's avatar
Maxime Perrotin committed
263
        free (reset_timer->distant_name);
264
        reset_timer->distant_name = make_string ("RESET_%s", timer); // irrelevant in PI
265
        reset_timer->rcm          = protected;
Maxime Perrotin's avatar
Maxime Perrotin committed
266
        reset_timer->synchronism  = synch;
Maxime Perrotin's avatar
Maxime Perrotin committed
267
        APPEND_TO_LIST (Interface, timer_manager->interfaces, reset_timer);
Maxime Perrotin's avatar
Maxime Perrotin committed
268

Maxime Perrotin's avatar
Maxime Perrotin committed
269 270 271
        /* Add corresponding RI in the user FV */
        reset_timer = Duplicate_Interface (RI, reset_timer, fv);
        free (reset_timer->distant_fv);
272 273 274 275 276
        reset_timer->distant_fv   = make_string (timer_manager->name);
        reset_timer->name         = make_string("RESET_%s", timer);
        reset_timer->distant_name = make_string("%s_%s",
                                                fv->name,
                                                reset_timer->name);
Maxime Perrotin's avatar
Maxime Perrotin committed
277
        APPEND_TO_LIST (Interface, fv->interfaces, reset_timer);
Maxime Perrotin's avatar
Maxime Perrotin committed
278

279 280
        /* Add Protected PI "SET_timer(value)" in timer manager */
        /* Note: it must be protected to avoid race condition with the tick function */
Maxime Perrotin's avatar
Maxime Perrotin committed
281 282
        set_timer = Duplicate_Interface (PI, reset_timer, timer_manager);
        free (set_timer->name);
283 284 285
        set_timer->name         = make_string ("%s_SET_%s",
                                               fv->name,
                                               timer);
Maxime Perrotin's avatar
Maxime Perrotin committed
286
        free (set_timer->distant_name);
287
        set_timer->distant_name = make_string ("SET_%s", timer);
Maxime Perrotin's avatar
Maxime Perrotin committed
288

Maxime Perrotin's avatar
Maxime Perrotin committed
289 290
        /* Add IN param holding the timer duration */
        Create_Parameter (&param);
Maxime Perrotin's avatar
Maxime Perrotin committed
291 292 293 294 295 296 297
        param->name            = make_string ("duration");
        param->type            = make_string ("T_UInt32");
        param->encoding        = native;
        param->asn1_module     = make_string ("taste_basictypes");
        param->basic_type      = integer;
        param->asn1_filename   = make_string ("taste-types.asn");
        param->interface       = set_timer;
Maxime Perrotin's avatar
Maxime Perrotin committed
298 299 300
        param->param_direction = param_in;
        APPEND_TO_LIST (Parameter, set_timer->in, param);
        APPEND_TO_LIST (Interface, timer_manager->interfaces, set_timer);
Maxime Perrotin's avatar
Maxime Perrotin committed
301

Maxime Perrotin's avatar
Maxime Perrotin committed
302 303 304
        /* Add corresponding RI in the user FV */
        set_timer = Duplicate_Interface (RI, set_timer, fv);
        free (set_timer->distant_fv);
305 306 307 308
        set_timer->name         = make_string("SET_%s", timer);
        set_timer->distant_name = make_string("%s_%s",
                                                fv->name,
                                                set_timer->name);
Maxime Perrotin's avatar
Maxime Perrotin 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 349 350 351 352 353 354 355 356
        set_timer->distant_fv = make_string (timer_manager->name);
        APPEND_TO_LIST (Interface, fv->interfaces, set_timer);
    });
}

void Close_Timer_Files()
{
    fprintf (header, "#ifdef __cplusplus\n"
                     "}\n"
                     "#endif\n\n"
                     "#endif");
    close_file(&header);
    close_file(&code);
}

void Add_Artificial_Function (Interface *duplicate_pi,
                              RCM pi_rcm,
                              RCM ri_rcm,
                              FV *fv,
                              char *fv_name,
                              char *ri_name,
                              long long period,
                              char *original_name)
{
   FV *new_fv = NULL;
   Interface *duplicate_ri = NULL;
   FILE *hook = NULL;
   char *path = NULL;


   /* 1) Create a new FV called fv_name (argument of the function)
    *    and set 2 interfaces to it (PI and RI) */

   new_fv = (FV *) New_FV (fv_name, strlen(fv_name), fv_name);
   /*
      Setting the language to blackbox_device allows to avoid generating
      unnecessary code for VT-created threads (vm_if, invoke_ri, dataview glue)
   */
   Set_Language_To_BlackBox_Device();

   duplicate_pi->period = period;

   /* TODO
        Question: what about WCET? shouldn't it inherit from the user-code
        WCET, which in turn should be set to 0 ?
   */

   duplicate_pi->parent_fv = new_fv;
Maxime Perrotin's avatar
Maxime Perrotin committed
357
   duplicate_pi->rcm       = pi_rcm;
Maxime Perrotin's avatar
Maxime Perrotin committed
358

Maxime Perrotin's avatar
Maxime Perrotin committed
359
   duplicate_ri = (Interface *) Duplicate_Interface (RI, duplicate_pi, new_fv);
Maxime Perrotin's avatar
Maxime Perrotin committed
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
   duplicate_ri->rcm = ri_rcm;
   free(duplicate_ri->distant_fv);
   duplicate_ri->distant_fv = NULL;
   free(duplicate_ri->name);
   duplicate_ri->name = NULL;

   build_string(&(duplicate_ri->distant_fv), fv->name, strlen(fv->name));

   build_string(&(duplicate_ri->name), ri_name, strlen(ri_name));
   duplicate_ri->synchronism = synch;

   APPEND_TO_LIST (Interface, new_fv->interfaces, duplicate_pi);
   APPEND_TO_LIST (Interface, new_fv->interfaces, duplicate_ri);

   End_FV();

   /* 2) Add the new FV to the binding list of the Process */
   if (NULL == fv->process) {
      ERROR ("** Error: function %s is not bound to any process\n",
              fv->name);
      add_error();
      return;
   }

   Set_Current_Process (fv->process);
   Add_Binding(fv_name, strlen(fv_name));

   /* 3) Create a file called "_hook" to add this function
    *    to the orchestrator work */
    path = make_string ("%s/%s", OUTPUT_PATH, new_fv->name);
Maxime Perrotin's avatar
Maxime Perrotin committed
390
    new_fv->artificial    = true;
Maxime Perrotin's avatar
Maxime Perrotin committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    new_fv->original_name = original_name;

    create_file (path, "_hook", &hook);
    close_file (&hook);
    free (path);
}

void ProcessArtificial_FV_Creation (Interface *i, RCM rcm)
{
  char *interface_name = string_to_lower (i->name);
  char *artificial_pi_name = NULL;
  char *artificial_fv_name = NULL;
  Interface *distant_RI =NULL;
  Interface *duplicate_if =  NULL; 

    artificial_pi_name = make_string ("%s", i->name);
    artificial_fv_name = make_string ("vt_%s_%s",
                                      i->parent_fv->name,
                                      interface_name);

    duplicate_if = (Interface *) Duplicate_Interface (PI, i, i->parent_fv);
    duplicate_if -> parent_fv = NULL;

    free(duplicate_if->name);
    duplicate_if->name = NULL;
    duplicate_if->name = make_string ("artificial_%s", i->name);

    // Set the "distant_name" field of the new PI to the name of the old PI
    // so that if the thread happens to be in a different process from one
    // of its caller, the interface name from TASTE-IV is respected.
    // MP 19/04/10 this is not consistent with the fact that a PI can be
    // connected to several RI we should add real pointers to connect the
    // interfaces rather than strings with their names (or only pointers to
    // distant fv - this does not cover the case of different names)
    // MP 20/04/10 This will have to be checked again with AADL v2.
    // In fact it is correct that the distant name is the original name of
    // the PI because it is the exposed name that TASTE-IV generates in
    // the deployment view. that's the port name seen from the environment.
    // In that case there is only one name and nothing should be changed here.
    // (except that "distant_name" should be renamed "port_name" maybe).
    // MP 21/11/10: aadlv2 update : I added a "port_name"

    if (NULL != duplicate_if->distant_name) {
        free (duplicate_if->distant_name);
        duplicate_if->distant_name=NULL;
    }
    build_string (&(duplicate_if->distant_name), i->name, strlen(i->name));

    Add_Artificial_Function (duplicate_if,
                             (i->rcm==cyclic)?cyclic:sporadic,
                             rcm,
                             i->parent_fv,
                             artificial_fv_name,
                             artificial_pi_name,
                             i->period,
                             i->parent_fv->name); 

    // Update the distant_Fv of the distant RIs invoking the newly created FV
    if (cyclic != i->rcm) {
        FV_list *callers = NULL;

        callers = (FV_list *)Find_All_Calling_FV(i);


        /* For each caller of the interface, find its corresponding RI
         * and update its "distant_fv" field to point to the newly created FV
         */
        FOREACH (caller, FV, callers, {
            distant_RI = NULL;

            /* Find the corresponding interface in the caller */
Maxime Perrotin's avatar
Maxime Perrotin committed
462 463 464
            distant_RI = FindCorrespondingRI(caller, i);

            if (NULL != distant_RI
Maxime Perrotin's avatar
Maxime Perrotin committed
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
                && strcmp(artificial_fv_name, distant_RI->parent_fv->name)) {
                if (NULL != distant_RI->distant_fv) {
                    free(distant_RI->distant_fv);
                    distant_RI->distant_fv = NULL;
                }
                build_string(&(distant_RI->distant_fv),
                             artificial_fv_name, strlen(artificial_fv_name));

                if (NULL != distant_RI->distant_name) {
                        free(distant_RI->distant_name);
                        distant_RI->distant_name = NULL;
                }
                build_string(&(distant_RI->distant_name),
                             "artificial_", strlen("artificial_"));
                build_string(&(distant_RI->distant_name),
                             i->name, strlen(i->name));
            }
        });

    }

    // remove the old distant_fv
    if (NULL != i->distant_fv) { 
      free (i->distant_fv);
      i->distant_fv = NULL;
    }

    build_string (&(i->distant_fv),
                  artificial_fv_name, strlen(artificial_fv_name));
    i->rcm = rcm;
    i->synchronism = synch;
    free (interface_name);
}
Maxime Perrotin's avatar
Maxime Perrotin committed
498
/*
Maxime Perrotin's avatar
Maxime Perrotin committed
499 500 501 502 503 504 505 506
 * 4th pre-processing:
 * check if a RI is not already present in a FV and if not, add it.
 * MP 27/07/2010 :
 *  In order to support mix between Sync and Async functions,
 *  we must check only that the EXACT SAME RI (same name but also
 *  same synchronism) is not present.
 *  We can have the same RI name but one being asynchronous,
 *  the other synchronous.
Maxime Perrotin's avatar
Maxime Perrotin committed
507
*/
Maxime Perrotin's avatar
Maxime Perrotin committed
508 509 510 511 512 513 514 515 516 517 518
void Add_RI_To_Thread (Interface *i, FV *fv)
{
    bool check = false;
    Interface *new_interface = NULL;
    char *new_name=NULL;

    assert(RI == i->direction);

    build_string(&new_name, i->name, strlen(i->name));
    build_string(&new_name, "_vt", strlen("_vt"));

Maxime Perrotin's avatar
Maxime Perrotin committed
519
    /*
Maxime Perrotin's avatar
Maxime Perrotin committed
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 546 547 548 549 550 551 552 553 554
     * Check if this RI was not already there 
     * Fixed (MP 27/07/2010) : now allows twice the same RI name
     * but with different synchronism
     * No issue in Ada because async RI are in an
     * "Async_RI" package.
     * New Fix (MP 29/07/2010) : now we completely rename the RI
     * in order to avoid naming conflicts in the AADL files.
    */
    FOREACH(t_i, Interface, fv->interfaces, {
           if (RI==t_i->direction &&
                     !strcmp(t_i->name, new_name)) check = true;
    });

    if(false == check) { /* if not, add it */
        new_interface = (Interface *) Duplicate_Interface (RI, i, fv);

        /*
        * change the name of the RI because there might be a PI with the
        * same name in the calling thread, and AADL does not like having
        * two ports with the same name
        */
        build_string(&(new_interface->name), "_vt", strlen("_vt"));

        if(NULL == new_interface->distant_name) {
            build_string(&(new_interface->distant_name),
                         i->name, strlen(i->name));
        }

        APPEND_TO_LIST (Interface, fv->interfaces, new_interface);
    }
    free(new_name);
}


/* Add a thread to the calling list */
555
void Add_Thread_To_Calling_List (FV_list **calling_threads_list, FV *fv) 
Maxime Perrotin's avatar
Maxime Perrotin committed
556 557 558 559 560
{
        assert(NULL != calling_threads_list);
        assert(NULL != fv);

        FOREACH(t, FV, *calling_threads_list, {
561
            if (t == fv) fv = NULL;
Maxime Perrotin's avatar
Maxime Perrotin committed
562 563 564
        });

        if (NULL != fv) {
565 566
            APPEND_TO_LIST(FV, *calling_threads_list, fv);
            return;
Maxime Perrotin's avatar
Maxime Perrotin committed
567 568 569 570 571 572 573
        }
}

/* Pre-Processing: propagate Calling thread to distant FV
 *                 (used to allow sync function to make call to RI). */
void Propagate_Calling_Thread(Interface *i, FV **fv)
{
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
    FV           *distant_fv       = NULL;
    Interface    *corresponding_pi = NULL;

    /* Find the function to which the RI is connected */
    distant_fv = FindFV(i->distant_fv);

    if(NULL == distant_fv) {
        /* This RI is not connected */
        return;
    }

    /* Find the PI corresponding to the RI passed as first argument */
    FOREACH(dist_i, Interface, distant_fv->interfaces, {
        // Long-term temporary fix to support the new version of tasteIV
        char *res = NULL;
        if (NULL != i->distant_name) {
            res = strchr(i->distant_name, '.');
            if (NULL != res) {
                printf("[preprocessing_backend.c] FIXME\n");
                *res = '\0';
Maxime Perrotin's avatar
Maxime Perrotin committed
594
            }
595 596 597 598 599 600 601
        }
        if(PI == dist_i->direction &&
           !strcmp(dist_i->name,
                   NULL != i->distant_name ? i->distant_name : i->name)) {
            corresponding_pi = dist_i;
        }
    });
Maxime Perrotin's avatar
Maxime Perrotin committed
602

603
    assert(NULL != corresponding_pi);
Maxime Perrotin's avatar
Maxime Perrotin committed
604

605 606 607 608 609 610 611 612
    /*
    * Check that the calling thread is not already in the list of
    * the distant fv/pi. This is essential to avoid infinite recursion
    * in case of circular dependencies
    */
    FOREACH(ct, FV, corresponding_pi->calling_threads, {
        if(ct == *fv) return;
    });
Maxime Perrotin's avatar
Maxime Perrotin committed
613

614 615 616
    FOREACH(ct, FV, distant_fv->calling_threads, {
        if(ct == *fv) return;
    });
Maxime Perrotin's avatar
Maxime Perrotin committed
617

618 619
    /* Add the thread to the list of calling FV of the interface */
    Add_Thread_To_Calling_List (&(corresponding_pi->calling_threads), *fv);
Maxime Perrotin's avatar
Maxime Perrotin committed
620

621 622
    /* Also add the thread to the calling list of the FV itself */
    Add_Thread_To_Calling_List (&(distant_fv->calling_threads), *fv);
Maxime Perrotin's avatar
Maxime Perrotin committed
623

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
    /* And change the "distant fv" field of the corresponding PI */
    if(NULL != corresponding_pi->distant_fv) {
        free(corresponding_pi->distant_fv);
        corresponding_pi->distant_fv = NULL;
    }
    build_string(&(corresponding_pi->distant_fv),
                 (*fv)->name, strlen((*fv)->name));

    if(passive_runtime == distant_fv->runtime_nature) {
        /* Do the same recursively until we reach another thread */
        FOREACH(sub_i, Interface, distant_fv->interfaces, {
            if (RI == sub_i->direction) {
            /* filter: some RIs are not called by the code of a given PI
             * for example the timer expiration signal is only called by the
             * cyclic PI of the timer manager. Don't propagate in that case
             * to avoid explosion of the number of AADL ports in the CV */
                if (NULL == sub_i->calling_pis
                   || IN_SET(Interface, sub_i->calling_pis, corresponding_pi)) {
                    Propagate_Calling_Thread(sub_i, fv);
                }
            }
        });
Maxime Perrotin's avatar
Maxime Perrotin committed
646 647 648 649
    }
}


Maxime Perrotin's avatar
Maxime Perrotin committed
650
/*
Maxime Perrotin's avatar
Maxime Perrotin committed
651 652 653 654
  Set of preprocessing done for each FV independently
*/
void Preprocess_FV (FV *fv)
{
Maxime Perrotin's avatar
Maxime Perrotin committed
655
   int  count_thread    = 0,
Maxime Perrotin's avatar
Maxime Perrotin committed
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
        count_pro       = 0,
        count_unpro     = 0;

   /* pre-processing: if a FV is a GUI, add a cyclic PI to the function,
    *  if the GUI has RIs (otherwise there is nothing to poll) */
   if (gui == fv->language) {
      int count_ri=0;
      FOREACH(i, Interface, fv->interfaces, {
            if(RI==i->direction) count_ri++;
      })

      if (count_ri>0)  {
         Interface *interface = NULL;
         char *cyclic_pi_name=NULL;
         build_string(&cyclic_pi_name, "gui_polling_", strlen("gui_polling_"));
         build_string(&cyclic_pi_name, fv->name, strlen(fv->name));

         Create_Interface (&interface);
         if (NULL != interface) {
            build_string (&(interface->name),
                          cyclic_pi_name, strlen(cyclic_pi_name));
            interface->distant_fv = NULL;
            interface->direction=PI;
            interface->synchronism=asynch;
            interface->rcm=cyclic;
681 682
            /* Poll GUI queue every 10 ms */
            interface->period = 10;
Maxime Perrotin's avatar
Maxime Perrotin committed
683 684 685 686 687 688 689 690 691
            interface->parent_fv = fv;
            interface->wcet_high = 1;
            interface->wcet_low = 1;
            build_string(&(interface->wcet_low_unit), "ms", 2);
            build_string(&(interface->wcet_high_unit), "ms", 2);
            APPEND_TO_LIST (Interface, fv->interfaces, interface);
         }
      }
   }
Maxime Perrotin's avatar
Maxime Perrotin committed
692

Maxime Perrotin's avatar
Maxime Perrotin committed
693 694 695 696 697 698 699 700 701 702 703 704 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
/*
     preprocessing: FV containing more than one
     'active' PI (cyclic/sporadic, protected)
     In that case create one function per PI of the block
     and put all existing PI as protected functions

     1) Count the number of PI and check if more than one is active.
        That's the condition for this VT rule.
*/
        /* Count active and passive PIs of the function */
        FOREACH(i, Interface, fv->interfaces, {
                if (PI == i->direction) 
                        switch (i->rcm) {
                                case cyclic:
                                case sporadic:
                                case variator:    count_thread ++;     break;
                                case protected:   count_pro ++;        break;
                                case unprotected: count_unpro ++;      break;
                                default :                              break;
                        }
        });

/*
        Major change (MP 07/11/2010) to allow mix of UNPRO with other RCM
        Now, user code ALWAYS resides in a "passive" function, and threads
        are created as "wrappers" around them, as blackbox devices
        (so that no additional code is generated).
        This is a generic and optimized mechanism to handle all functions
        in the same manner
*/

    /* 2) Create a new FV for each PI
     *    (except protected/unprotected which remain as is) */

        FOREACH(i, Interface, fv->interfaces, {
            RCM rcm = undefined;
            if (PI == i->direction && (cyclic == i->rcm ||
                                       sporadic == i->rcm ||
                                       variator == i->rcm)) {
                /* Set the interface of the user block as protected
                 * if there is more than 1 active PI on the FV */
                if (count_thread + count_pro > 1) {
                        rcm = protected;
                }
                /* Otherwise there is no need for mutual exclusion -
                 * define as unprotected */
                else rcm = unprotected;
                /* Create a new FV (thread) if there is more than
741 742
                 * one PI in the function (unprotected don't count) */
                if (count_pro+count_thread>1) {
Maxime Perrotin's avatar
Maxime Perrotin committed
743 744 745 746 747 748 749 750
                    ProcessArtificial_FV_Creation (i, rcm);
                }
            }
        });


  /*  preprocessing: determine if a FV is a thread or a passive function
   *  (once all artificial threads have been created) */
751
        fv->runtime_nature = passive_runtime;
Maxime Perrotin's avatar
Maxime Perrotin committed
752 753

        FOREACH (i, Interface, fv->interfaces, {
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
            if (PI==i->direction) {
                switch (i->rcm) {
                    case cyclic:
                        fv->runtime_nature = thread_runtime;
                        break;
                    case sporadic:
                    case variator:
                        if (NULL != Find_All_Calling_FV(i)) {
                             fv->runtime_nature = thread_runtime;
                        }
                        else {
                            printf("unconnected interface %s %s\n", i->name, i->distant_name);
                            }
                        break;
                    default: break;
                 }
            }
Maxime Perrotin's avatar
Maxime Perrotin committed
771
        });
772

Maxime Perrotin's avatar
Maxime Perrotin committed
773 774 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 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
        if(thread_runtime == fv->runtime_nature) {
            /* Set the (unique) thread ID */
            fv->thread_id = ++thread_id;
        }
}

/* Create code that handles timers
 * For each node:
 *      if any timer, add a FV with one cyclic PI named "<node>_timer_manager"
 *      then for each FV of the node:
 *          for each timer with name "t":
 *              add sporadic PI "t" with no param (called when timer expires)
 *              add unpro RI "SET_t" with an int32 param and native encoding
 *              add unpro RI "RESET_t" with no param
 *              add the corresponding PI and RIs to <node>_timer_manager
 *      generate the code of the PIs of the <node>_timer_manager
 */
void Preprocess_timers (Process *node)
{
    bool any_timer = false;
    FV_list *fv_with_timer = NULL;
    FV *timer_manager = NULL;

    /* 1) check if any of the functions of this node uses timer(s) */
    FOREACH (binding, Aplc_binding, node->bindings, {
        if (has_timer(binding->fv)) {
            any_timer = true;
            APPEND_TO_LIST(FV, fv_with_timer, binding->fv);
        }
    });

    if (false == any_timer) {
        return;
    }
    /* 2) Add FV with one cyclic PI to manage the timers of this node */
    timer_manager = Add_timer_manager(node, fv_with_timer);

    /* 3) For each FV with a timer add PI and RI to set, reset, consume */
    FOREACH (fv, FV, fv_with_timer, {
        Add_timers_to_function (fv, timer_manager);
    });

    Close_Timer_Files();
}

Maxime Perrotin's avatar
Maxime Perrotin committed
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
/* Create a function named <node>_api and add a number of unprotected PIs */
void Add_api(Process *node, FV_list *all_fv)
{
    FV   *fv   = NULL;
    char *name = NULL;
    char *path = NULL;
    FILE *hook = NULL;

    name = make_string("%s_taste_api", node->name);

    fv = New_FV(name, strlen(name), name);
    assert (NULL != fv);

    Set_Language_To_C();

    /* Add a PI for each of the node's functions */
    FOREACH (function, FV, all_fv, {
        Interface *pi = NULL;
        Interface *ri = NULL;
        Create_Interface(&pi);
        assert(NULL != pi);
        pi->name           = make_string("%s_has_pending_msg", function->name);
840 841
        pi->distant_fv     = make_string("%s", function->name);
        pi->distant_name   = make_string("check_queue", pi->name);
Maxime Perrotin's avatar
Maxime Perrotin committed
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
        pi->direction      = PI;
        pi->synchronism    = synch;
        pi->rcm            = unprotected;
        pi->period         = 0;
        pi->parent_fv      = fv;
        pi->wcet_high      = 10;
        pi->wcet_low       = 10;
        pi->wcet_low_unit  = make_string("ms");
        pi->wcet_high_unit = make_string("ms");

        /* Add OUT param to handle the result */
        Parameter *param      = NULL;
        Create_Parameter(&param);
        param->name            = make_string("res");
        param->type            = make_string("T_Boolean");
        param->encoding        = native;
        param->asn1_module     = make_string("taste_basictypes");
        param->basic_type      = boolean;
        param->asn1_filename   = make_string("taste-types.asn");
        param->interface       = pi;
        param->param_direction = param_out;
        APPEND_TO_LIST(Parameter, pi->out, param);
        APPEND_TO_LIST(Interface, fv->interfaces, pi);

        /* Add the corresponding RI in the user FV */
867 868
        ri = Duplicate_Interface(RI, pi, function);
        ri->name              = make_string("check_queue");
Maxime Perrotin's avatar
Maxime Perrotin committed
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
        ri->distant_name      = make_string("%s", pi->name);
        free(ri->distant_fv);
        ri->distant_fv = make_string("%s", fv->name);
        APPEND_TO_LIST(Interface, function->interfaces, ri);
    });

    /* Set flag indicating that this function was created during VT */
    fv->timer = true;
    End_FV();

    /* Add the new FV to the binding list of the Process */
    Set_Current_Process (node);
    Add_Binding(name, strlen(name));

   /* Create a file called "_hook" to add this function
    * to the orchestrator work */
    path = make_string ("%s/%s", OUTPUT_PATH, fv->name);
    create_file(path, "_hook", &hook);
    close_file(&hook);

   /* Create files containing the implementation of the function */
    create_file (path, make_string ("%s.h", fv->name), &header);
    create_file (path, make_string ("%s.c", fv->name), &code);

    fprintf (header, "/* TASTE API */\n%s", do_not_modify_warning);
    fprintf (code,   "/* TASTE API */\n%s", do_not_modify_warning);

896 897
    fprintf (code, "#include <deployment.h>\n\n"
                   "#include \"%s.h\"\n\n"
898
                   "extern int __po_hi_gqueue_get_count(int, int);\n\n",
899
                   fv->name);
Maxime Perrotin's avatar
Maxime Perrotin committed
900 901 902 903 904 905


    fprintf (header, "#ifndef __AUTO_CODE_H_%s__\n"
                     "#define __AUTO_CODE_H_%s__\n\n"
                     "#include \"C_ASN1_Types.h\"\n"
                     "#ifdef __cplusplus\n"
906
                     "    extern \"C\" {\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
907 908 909 910
                     "#endif\n\n",
                     fv->name,
                     fv->name);

Maxime Perrotin's avatar
Maxime Perrotin committed
911 912
    /* Debug mode - Unix platform, when env variable CHECKQ_DEBUG is set */
    fprintf (header, "#ifdef __unix__\n"
913 914 915
                     "    #include <stdbool.h>\n"
                     "    #include <stdlib.h>\n"
                     "    static bool debugCheckQ = false;\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
916 917
                     "#endif\n\n");

918 919 920
    fprintf (code,   "#ifdef __unix__\n"
                     "    #include <stdio.h>\n"
                     "#endif\n\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
921

Maxime Perrotin's avatar
Maxime Perrotin committed
922 923 924
    fprintf (header, "void %s_startup();\n\n", fv->name);
    fprintf (code,   "void %s_startup()\n"
                     "{\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
925 926 927 928
                     "    /* TASTE API start up */\n"
                     "    #ifdef __unix__\n"
                     "        debugCheckQ = getenv(\"CHECKQ_DEBUG\");\n"
                     "    #endif\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
929 930 931 932
                     "}\n\n", fv->name);

    FOREACH(function, FV, all_fv, {
        char *decl = NULL;
933 934 935
        char *task_id = NULL;
        char *port = NULL;
        decl = make_string("void %s_PI_%s_has_pending_msg(asn1SccT_Boolean *res)", fv->name, function->name);
Maxime Perrotin's avatar
Maxime Perrotin committed
936 937
        fprintf(header, "%s;\n\n", decl);
        fprintf(code, "%s {\n"
938
                      "    /* Check all incoming queues (if any) for a pending message */\n", decl);
939 940 941
        /* Naming of ports/task id is different if there is more than 1 active PI */
        int active = CountActivePI(function->interfaces);
        FOREACH(pi, Interface, function->interfaces, {
942
            if(PI == pi->direction && asynch == pi->synchronism && cyclic != pi->rcm && NULL != Find_All_Calling_FV(pi)) {
943 944 945 946 947 948 949 950
                if (1 == active) {
                    task_id = make_string("%s_%s_k", node->name, function->name);
                    port = make_string("%s_local_inport_%s", function->name, pi->name);
                }
                else { /* More than one active PI */
                    task_id = make_string("%s_vt_%s_%s_k", node->name, function->name, pi->name);
                    port = make_string("vt_%s_%s_local_inport_artificial_%s", function->name, pi->name, pi->name);
                }
951 952
                fprintf(code, "    *res = 0;\n"
                              "    if (__po_hi_gqueue_get_count(%s, %s)) {\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
953 954
                              "        *res = 1;\n"
                              "        #ifdef __unix__\n"
955 956 957
                              "            if (debugCheckQ) {\n"
                              "                printf (\"[DEBUG] Pending message %s in function %s\\n\");\n"
                              "            }\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
958 959
                              "        #endif\n"
                              "    }\n",
960
                              string_to_lower(task_id),
Maxime Perrotin's avatar
Maxime Perrotin committed
961 962 963
                              string_to_lower(port),
                              pi->name,
                              function->name);
964 965 966 967
                free(task_id);
                free(port);
            }
        });
Maxime Perrotin's avatar
Maxime Perrotin committed
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988

        fprintf(code, "}\n\n");
        free(decl);
    });

    fprintf (header, "#ifdef __cplusplus\n"
                     "}\n"
                     "#endif\n\n"
                     "#endif");
    close_file(&header);
    close_file(&code);
    free(path);
}


/* Create a function to handle the calls to the TASTE API
 * For each node:
 *      create a function named <node>_taste_api
 *      for each FV of the node:
 *          add a PI named "<fv>_has_pending_msg (result: boolean)
 *             (reports true if any of the PI queue holds a message)
989
 *      (later) add a PI named "event"
Maxime Perrotin's avatar
Maxime Perrotin committed
990 991 992 993 994 995 996
 */
void Preprocess_taste_api (Process *node)
{
    FV_list *all_fv = NULL;

    /* 1) List all functions of this node */
    FOREACH (binding, Aplc_binding, node->bindings, {
997 998 999 1000 1001 1002 1003 1004
        bool is_async = false;
        FOREACH(iface, Interface, binding->fv->interfaces, {
            /* Discard functions with no active interface */
            if (PI == iface->direction && asynch == iface->synchronism) {
                is_async = true;
            }
        });
        if (gui != binding->fv->language && true == is_async) {
Maxime Perrotin's avatar
Maxime Perrotin committed
1005 1006 1007 1008
            APPEND_TO_LIST(FV, all_fv, binding->fv);
        }
    });

1009
    /* 2) Create the FV */
1010 1011 1012
    if (NULL != all_fv) {
        Add_api(node, all_fv);
    }
Maxime Perrotin's avatar
Maxime Perrotin committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 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 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
}

/* Create code that handles the collection of code coverage data
 * For each node:
 *      if flag "coverage" is set, add a periodic task that dumps the
 *      coverage data to disk (using gcov flush function)
 */
void Preprocess_coverage (Process *node)
{
    FV *fv = NULL;
    Interface *interface = NULL;
    char *name = NULL;
    char *path = NULL;
    FILE *hook = NULL;

    name = make_string ("%s_coverage_collector", node->name);

    fv = (FV *) New_FV (name, strlen(name), name);

    Set_Language_To_C();

    Create_Interface (&interface);

    assert (NULL != fv && NULL != interface);

    interface->name = make_string ("dump_coverage");

    interface->distant_fv = NULL;
    interface->direction = PI;
    interface->synchronism = asynch;
    interface->rcm = cyclic;
    /* Interface needs to be cyclic but the call does not do anything */
    /* because to collect the coverage data user must send SIGUSR2 */
    interface->period = 100000;
    interface->parent_fv = fv;
    interface->wcet_high = 10;
    interface->wcet_low = 10;
    interface->wcet_low_unit = make_string ("ms");
    interface->wcet_high_unit = make_string ("ms");
    APPEND_TO_LIST (Interface, fv->interfaces, interface);
    /* Set flag indicating that this function was created during VT */
    fv->timer = true;

    End_FV();

    /* Add the new FV to the binding list of the Process */
    Set_Current_Process (node);
    Add_Binding(name, strlen(name));

   /* 3) Create a file called "_hook" to add this function
    *    to the orchestrator work */
    path = make_string ("%s/%s", OUTPUT_PATH, fv->name);

    create_file (path, "_hook", &hook);
    create_file (path, make_string ("%s.h", fv->name), &header);
    create_file (path, make_string ("%s.c", fv->name), &code);

    fprintf (header, "/* Code Coverage manager */\n%s", do_not_modify_warning);
    fprintf (code, "/* Code Coverage manager */\n%s", do_not_modify_warning);

    fprintf (header, "#ifndef __COVERAGE_CODE_H_%s__\n"
                     "#define __COVERAGE_CODE_H_%s__\n\n"
                     "#ifdef __cplusplus\n"
                     "extern \"C\" {\n"
                     "#endif\n\n",
                     fv->name,
                     fv->name);

    fprintf (header, "void %s_startup();\n\n"
                     "void %s_PI_dump_coverage();\n\n",
                     fv->name,
                     fv->name);
    fprintf (code, "#include <signal.h>\n\n"
                   "#include \"%s.h\"\n\n", fv->name);

    fprintf (code, "/* Dummy cyclic function */\n"
                   "void %s_PI_dump_coverage()\n"
                   "{\n"
                   "}\n\n", fv->name);

    fprintf (code, "/* Signal handler to dump coverage information */\n"
                   "void gc_handler()\n"
                   "{\n"
                   "    puts(\"Collecting code coverage\");\n"
                   "    extern void __gcov_flush();\n"
                   "    __gcov_flush();\n"
                   "    exit(0);\n"
                   "}\n\n");

1102
    /* Initialisation code of the coverage manager */
Maxime Perrotin's avatar
Maxime Perrotin committed
1103 1104
    fprintf (code, "void %s_startup()\n"
                   "{\n"
1105
                   "    puts(\"use kill -SIGUSR2 to collect code coverage\");\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
                   "    signal(SIGUSR2, gc_handler);\n"
                   "}\n\n",
                   fv->name);

    close_file (&hook);
    free (path);

    fprintf (header, "#ifdef __cplusplus\n"
                     "}\n"
                     "#endif\n\n"
                     "#endif");
    close_file(&header);
    close_file(&code);
}



Maxime Perrotin's avatar
Maxime Perrotin committed
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
/* Look at all provided interfaces of a function and if some do
 * not reside on the same node as any of their callers set the ignore_params
 * flag to false. This enables some runtime optimisations wrt buffer copies.
*/
void Set_Ignore_Params(FV *fv)
{
    Interface *distant_RI = NULL;

    FOREACH(i, Interface, fv->interfaces, {
        if (PI == i->direction) {
            FOREACH (remote, FV, Find_All_Calling_FV(i), {
                if (strcmp(remote->process->name, fv->process->name)) {
                    i->ignore_params = false;
Maxime Perrotin's avatar
Maxime Perrotin committed
1136
                    //printf("set PI %s->ignore_params to FALSE", i->name);
Maxime Perrotin's avatar
Maxime Perrotin committed
1137 1138 1139
                    distant_RI = FindCorrespondingRI(remote, i);
                    if (NULL != distant_RI) {
                        distant_RI->ignore_params = false;
1140
                    //printf("set RI %s->ignore_params to FALSE", i->name);
Maxime Perrotin's avatar
Maxime Perrotin committed
1141 1142 1143 1144 1145 1146
                    }
                }
            });
        }
    });
}
Maxime Perrotin's avatar
Maxime Perrotin committed
1147 1148 1149 1150 1151 1152

/* External interface */
void Preprocessing_Backend (System *s)
{

    FOREACH (node, Process, s->processes, {
Maxime Perrotin's avatar
Maxime Perrotin committed
1153
        /* Manage timers that may be declared as context parameters */
Maxime Perrotin's avatar
Maxime Perrotin committed
1154
        Preprocess_timers(node);
Maxime Perrotin's avatar
Maxime Perrotin committed
1155
        /* Create a TASTE API function with a set of unprotected PIs */
1156 1157 1158 1159
        if(get_context()->polyorb_hi_c) {
            /* Functionality requires POHIC API */
            Preprocess_taste_api(node);
        }
Maxime Perrotin's avatar
Maxime Perrotin committed
1160 1161 1162 1163 1164
    });

    /* Manage coverage flag for each node */
    FOREACH (node, Process, s->processes, {
        if(node->coverage) Preprocess_coverage(node);
Maxime Perrotin's avatar
Maxime Perrotin committed
1165 1166 1167 1168
    });

    FOREACH (fv, FV, s->functions, {
        Preprocess_FV(fv);
1169
    });
Maxime Perrotin's avatar
Maxime Perrotin committed
1170

1171 1172 1173 1174
    /* Propagate calling thread to distant PI (recursively)
     * i.e look for all threads (functions have already been preprocessed)
     * then go through all their RIs recursively until reaching another thread,
     * and set that the function is a calling thread of the reached one */
Maxime Perrotin's avatar
Maxime Perrotin committed
1175
    FOREACH(fv, FV, s->functions, {
1176
        if (thread_runtime == fv->runtime_nature) {
Maxime Perrotin's avatar
Maxime Perrotin committed
1177 1178 1179 1180
            FOREACH(i, Interface, fv->interfaces, {
                if (RI == i->direction) Propagate_Calling_Thread (i, &fv);
            })
        }
1181
    });
Maxime Perrotin's avatar
Maxime Perrotin committed
1182

1183
    /* Add each RI of passive functions as RI of their calling threads */
Maxime Perrotin's avatar
Maxime Perrotin committed
1184 1185 1186 1187
    FOREACH(fv, FV, s->functions, {
        if(passive_runtime==fv->runtime_nature) {
            FOREACH(i, Interface, fv->interfaces, {
                if (RI==i->direction && i->distant_qgen->language == other) {
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
                    /* Add to calling threads only of PIs that actually call
                     * the current RI - filtering to be done, but the result
                     * is the ri->calling_pis list */
                    FV_list *callers = NULL;
                    if (NULL == i->calling_pis) {
                        callers = fv->calling_threads;
                    }
                    else {
                        /* Build a set of actual calling threads of this RI */
                        FOREACH(calling_pi, Interface, i->calling_pis, {
                            FOREACH(thread_caller, FV, calling_pi->calling_threads, {
                                ADD_TO_SET(FV, callers, thread_caller);
                            });
                        });
                    }
                    //FOREACH(thread, FV, fv->calling_threads, {
                    FOREACH(thread, FV, callers, {
                        Add_RI_To_Thread(i, thread);
Maxime Perrotin's avatar
Maxime Perrotin committed
1206
                        Propagate_Calling_Thread(i, &thread);
1207
                    });
Maxime Perrotin's avatar
Maxime Perrotin committed
1208
                }
1209
            });
Maxime Perrotin's avatar
Maxime Perrotin committed
1210 1211 1212
        }
    });

Maxime Perrotin's avatar
Maxime Perrotin committed
1213
    /*
Maxime Perrotin's avatar
Maxime Perrotin committed
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
     * After the preprocessing, we must refine the system-level connections:
     * During AST construction, connections are only between threads
     * but since we transform some threads into (un)protected functions,
     * and we create new threads we must refactor the connections 
     * For each connection:
     *   if destination is passive: define actual_dest=corresponding thread
     *   else keep current destination
     *   Then if source is passive:
     *      for each calling thread CT of source
     *          create a new connection between CT and actual_dest
     *   else create a new connection between source and actual dest
     *   then remove the original connnection 
     *  Note: for some reasons src and dest are inverted in the AST:
     *   src_system means FV of the callee (destination of the message)
     *   dst_system means FV of the caller (source of the message)
     */
    Connection_list *new_connections = NULL;
    Connection_list *connections_to_remove = NULL;
    FOREACH (cnt, Connection, get_system_ast()->connections, {
        FV          *caller = NULL;
        FV          *callee = NULL;
        Interface   *ri     = NULL;

        /* Find the FV of the caller */
        FOREACH (fv, FV, get_system_ast()->functions, {
            if (!strcmp (fv->name, cnt->dst_system)) caller = fv;
Maxime Perrotin's avatar
Maxime Perrotin committed
1240
         });
Maxime Perrotin's avatar
Maxime Perrotin committed
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
        assert (NULL != caller);

        /* Find the corresponding RI in the caller */
        FOREACH (i, Interface, caller->interfaces, {
            if (!strcmp (i->name, cnt->dst_port)) ri = i;
        });
        assert (NULL != ri);

        /* Find the FV of the callee */
        FOREACH (fv, FV, get_system_ast()->functions, {
            if (!strcmp (fv->name, ri->distant_fv)) callee = fv;
        });

        /* If the caller is passive we must create one connection
         * per calling thread */
Maxime Perrotin's avatar
Maxime Perrotin committed
1256
        if (passive_runtime == caller->runtime_nature && NULL != callee) {
Maxime Perrotin's avatar
Maxime Perrotin committed
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
            FOREACH (ct, FV, caller->calling_threads, {
                New_Connection (
                    callee->name,   strlen (callee->name),
                    cnt->src_port,    strlen (cnt->src_port),
                    cnt->bus,       strlen (cnt->bus),
                    ct->name,       strlen (ct->name),
                    cnt->dst_port,    strlen (cnt->dst_port)
                );
                APPEND_TO_LIST (Connection, new_connections, Get_Connection());
            });
            APPEND_TO_LIST (Connection, connections_to_remove, cnt);
        }
Maxime Perrotin's avatar
Maxime Perrotin committed
1269
        else if (NULL != callee && strcmp (callee->name, cnt->src_system)) {
Maxime Perrotin's avatar
Maxime Perrotin committed
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
                New_Connection (
                    callee->name,     strlen (callee->name),
                    cnt->src_port,    strlen (cnt->src_port),
                    cnt->bus,         strlen (cnt->bus),
                    caller->name,     strlen (caller->name),
                    cnt->dst_port,    strlen (cnt->dst_port)
                );
                APPEND_TO_LIST (Connection, new_connections, Get_Connection());
                APPEND_TO_LIST (Connection, connections_to_remove, cnt);
        }
    });
    /* Add newly-created connections to the global list, remove the old ones */
    if (NULL != new_connections) {
        FOREACH (cnt, Connection, new_connections, {
            APPEND_TO_LIST (Connection, get_system_ast()->connections, cnt);
        });
        FOREACH (cnt, Connection, connections_to_remove, {
            REMOVE_FROM_LIST (Connection, get_system_ast()->connections, cnt);
        });
    }

    new_connections = NULL;
    connections_to_remove = NULL;
Maxime Perrotin's avatar
Maxime Perrotin committed
1293 1294 1295 1296 1297 1298 1299

    /* Once all AST transformations are done, set the ignore_params flags
     * in all interfaces, if needed 
     */
    FOREACH (fv, FV, s->functions, {
        Set_Ignore_Params(fv);
    });
Maxime Perrotin's avatar
Maxime Perrotin committed
1300
}