vertical_transformation.c 36 KB
Newer Older
Maxime Perrotin's avatar
Maxime Perrotin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* Buildsupport is (c) 2008-2015 European Space Agency
 * contact: maxime.perrotin@esa.int
 * License is LGPL, check LICENSE file */
/*
 * Vertical Transformation (VT) backend:
 * Generates the full "Concurrency View" as needed by Ocarina
 * to generate the Ada code of the real-time architecture.
 *
 * 1st version written by Maxime Perrotin/ESA 15/07/2008
 *
 * XXX The computation of priorities must be refactored
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <assert.h>
#include "my_types.h"
#include "practical_functions.h"
#include "c_ast_construction.h"

25 26 27 28 29 30
static FILE      *thread,
                 *process,
                 *nodes;
static long long period       = 0;
static char      *cyclic_name = NULL;
static           RCM op_kind  = undefined;
Maxime Perrotin's avatar
Maxime Perrotin committed
31

32
bool first_interface = false;
Maxime Perrotin's avatar
Maxime Perrotin committed
33 34 35

static char *system_root_node=NULL;

36
bool features_declared = false;
Maxime Perrotin's avatar
Maxime Perrotin committed
37 38 39
static unsigned int system_connections_declared = 0;


40
/*
Maxime Perrotin's avatar
Maxime Perrotin committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  Make a local copy of the name of cyclic PI, if any
  This is needed because cyclic threads are treated differently
  than other threads in the Concurrency View.
*/
void set_cyclic_name(char *if_name)
{
   build_string(&cyclic_name, if_name, strlen(if_name));
}

void write_thread_preamble(FV *fv)
{
   if (NULL == thread) return;
   fprintf(thread, "THREAD %s_%s\n", fv->name, fv->name);
   /* The first fv->name should be replaced by the namespace
    * (not supported 15/07/2009) */
}

void write_thread_postamble(FV *fv)
{
   if (NULL == thread) return;
   fprintf(thread, "END %s_%s;\n\n", fv->name, fv->name);
   /* The first fv->name should be replaced by the namespace
    * (not supported 15/07/2009) */
}


/*
   Find_Highest_Period among all interfaces of a FV
*/
void Find_Highest_Period (Interface *i, long long *result)
{
   if (i->period > *result) *result = i->period;
}



/* Thread implementation contains some properties set in the IF view */
void write_thread_implementation(FV *fv)
{
   long long               highest_period       = 0;
   long long               priority             = 0;
   uint64_t                wcet_low;
   uint64_t                wcet_high;
   Interface*              tmp_if;
   Interface_list*         tmp_if_list;

   /*
    * Code related to the integration of the BA annex in threads. Not used yet.
    *
    * int                     ba_preamble          = 0;
    * int                     found                = 0;
   */

   if (NULL == thread) {
      return;
   }

   fprintf(thread, "THREAD IMPLEMENTATION %s_%s.others\n", fv->name, fv->name);
   /* fv->name should be replaced by the namespace
    * (not supported 15/07/2009) (used to be aplc) */

   fprintf(thread, "PROPERTIES\n");

104 105
   /* For both PolyORB-HI/Ada and C, entrypoints refer to subprograms
      generated by the glue generators
Maxime Perrotin's avatar
Maxime Perrotin committed
106 107 108
   */
   char *init_name = fv->name;
   if (fv->artificial) {
109 110 111 112 113 114 115 116 117 118 119 120 121
       char *lookfor = NULL;
       FOREACH (interface, Interface, fv->interfaces, {
           if (PI == interface->direction) {
               lookfor = interface->distant_name; // XXX if multiple callers?
           }
       })
       FOREACH (interface, Interface, fv->interfaces, {
           if (RI == interface->direction
                   && !strcmp (lookfor, interface->distant_name) &&
                   synch == interface->synchronism) {
               init_name = interface->distant_fv;
            }
       })
Maxime Perrotin's avatar
Maxime Perrotin committed
122 123
   }

124 125
   fprintf(thread, "\tInitialize_Entrypoint_Source_Text => ");

Maxime Perrotin's avatar
Maxime Perrotin committed
126
   if (0 == fv->system_ast->context->polyorb_hi_c) { /* PolyORB-HI/Ada */
127 128 129
       fprintf(thread, "\"%s_wrappers.C_Init_%s\";\n",
                       init_name,
                       init_name);
Maxime Perrotin's avatar
Maxime Perrotin committed
130 131
   }
   else {  /* PolyORB-HI/C or VT-created thread: Add init. entrypoint */
132 133
       fprintf(thread, "\"init_%s\";\n",
                       init_name);
Maxime Perrotin's avatar
Maxime Perrotin committed
134 135 136
   }

   /* if the thread is cyclic, add a call to its entry point here */
137 138
   if(cyclic == op_kind) {
      fprintf(thread, "\tCompute_Entrypoint_Source_Text => ");
Maxime Perrotin's avatar
Maxime Perrotin committed
139
      if (0 == fv->system_ast->context->polyorb_hi_c) { /* PolyORB-HI/Ada */
140 141 142
         fprintf(thread, "\"%s_wrappers.%s\";\n",
                         fv->name,
                         cyclic_name);
Maxime Perrotin's avatar
Maxime Perrotin committed
143 144
      }
      else { /* PolyORB-HI/C */
145 146 147
         fprintf(thread, "\"po_hi_c_%s_%s\";\n",
                         fv->name,
                         cyclic_name);
Maxime Perrotin's avatar
Maxime Perrotin committed
148 149 150
      }
   }

151 152 153 154 155
   fprintf(thread, "\tDispatch_Protocol => %s;\n"
                   "\tPeriod            => %lld ms;\n"
                   "\tDispatch_Offset   => 0 ms;\n",
                    cyclic == op_kind ? "Periodic" : "Sporadic",
                    0 == period ? 1 : period);
Maxime Perrotin's avatar
Maxime Perrotin committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

   if (op_kind == cyclic)
   {
      tmp_if_list = fv->interfaces;
      wcet_low = 100;
      wcet_high = 0;
      while (tmp_if_list != NULL) {
         tmp_if = tmp_if_list->value;

         if ((tmp_if->wcet_low_unit == NULL) ||
            (tmp_if->wcet_high_unit == NULL)) {
            tmp_if_list = tmp_if_list->next;
            continue;
         }

         if (tmp_if->wcet_low < wcet_low) {
               wcet_low = tmp_if->wcet_low;
         }

         if (tmp_if->wcet_high > wcet_high) {
               wcet_high = tmp_if->wcet_high;
         }

         tmp_if_list = tmp_if_list->next;
      }

      fprintf(thread,"\tCompute_Execution_Time => %llu ms .. %llu ms;\n",
        (long long unsigned int)wcet_low,
        (long long unsigned int)wcet_high);
   }

   if (op_kind == sporadic) {
      tmp_if_list = fv->interfaces;
      wcet_low = 100;
      wcet_high = 0;
      while (tmp_if_list != NULL) {
         tmp_if = tmp_if_list->value;

         if ((tmp_if->wcet_low_unit == NULL) ||
            (tmp_if->wcet_high_unit == NULL)) {
            tmp_if_list = tmp_if_list->next;
            continue;
         }

         if (tmp_if->wcet_low < wcet_low) {
               wcet_low = tmp_if->wcet_low;
         }

         if (tmp_if->wcet_high > wcet_high) {
               wcet_high = tmp_if->wcet_high;
         }

         tmp_if_list = tmp_if_list->next;
      }

      fprintf(thread,"\tCompute_Execution_Time => %llu ms .. %llu ms;\n",
        (unsigned long long) wcet_low,
        (unsigned long long) wcet_high);
   }

   /* Default source stack size per thread */
217
   // XXX the -s from the command line is ignored?
Maxime Perrotin's avatar
Maxime Perrotin committed
218
   fprintf(thread,"\tSource_Stack_Size => 50 KByte;\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
219 220 221

   /* Calculate the priority : temporary solution using the period */
   FOREACH(i, Interface, fv->interfaces, {
222
       Find_Highest_Period(i, &highest_period);
Maxime Perrotin's avatar
Maxime Perrotin committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
   })

   /*
    * Higher period => higher priority.
    * Priority low > priority high (POHIC/Linux)
    */
   if (highest_period < 100) priority = 1;
   else if (highest_period >= 100 && highest_period < 250) priority = 5;
   else if (highest_period >= 250 && highest_period < 500) priority = 6;
   else if (highest_period >= 500 && highest_period < 750) priority = 7;
   else if (highest_period >= 750 && highest_period < 1000) priority = 8;
   else if (highest_period == 1000) priority = 9;
   else if (highest_period > 1000) priority = 10;

   fprintf(thread,"\tPriority => %lld;\n", priority);
   /* 
    MP: To be investigated
    JH: To be computed from a schedulability analysis of the system,
        e.g. GRMA using Cheddar
   */
   fprintf(thread,"END %s_%s.others;\n\n", fv->name, fv->name);
}

/* Create the files and put headers */
int Init_VT_Backend(FV *fv)
{
249 250 251 252
   char   *filename = NULL;   /* to store the wrappers filename */
   size_t len       = 0;      /* length of the filename */
   char   *path     = NULL;

Maxime Perrotin's avatar
Maxime Perrotin committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
   if (NULL != fv->system_ast->context->output) {
      build_string(&path, fv->system_ast->context->output,
                   strlen(fv->system_ast->context->output));
   }

   build_string(&path, "ConcurrencyView", strlen("ConcurrencyView"));

   len = strlen(fv->name) + strlen ("_CV_Thread.aadl");
   filename = (char *) malloc (len+1);
   assert(NULL != filename);

   /* create the Aadl files in a directory called "ConcurrencyView" */
   sprintf(filename, "%s_CV_Thread.aadl", fv->name);
   create_file(path, filename, &thread);

   free (filename);

   assert(NULL != thread);
   fprintf(thread,
       "--  This file was generated automatically: DO NOT MODIFY IT !\n\n"
       "--  This file contains a part of the system CONCURRENCY VIEW.\n"
       "--  It is an input file for OCARINA.\n\n");
275

Maxime Perrotin's avatar
Maxime Perrotin committed
276
   int hasparam=0;
277 278 279 280 281
   fprintf(thread, "package %s_CV_Thread\n"
                   "public\n"
                   "\twith Deployment;\n"
                   "\twith process_package;\n",
                   fv->name);
282

Maxime Perrotin's avatar
Maxime Perrotin committed
283
   FOREACH(i, Interface, fv->interfaces, {
284
       CheckForAsn1Params(i, &hasparam);
Maxime Perrotin's avatar
Maxime Perrotin committed
285
   })
286

287 288
   if (hasparam) {
       fprintf(thread, "\twith Dataview;\n\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
289
   }
290

Maxime Perrotin's avatar
Maxime Perrotin committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
   fv->runtime_nature = thread_runtime;
   write_thread_preamble(fv);
   free(path);
   return 0;
}

/* Add a new interface */
/* Note: it concerns only Async with a single IN param */
void Add_IF_to_VT (Interface *i)
{
   if (NULL == thread) {
      if(-1 == Init_VT_Backend(i->parent_fv)) {
         return;
      }
   }

   /* Determine the nature of the thread, based on the nature of the only
    * PROVIDED (added by MP31/07/2009) interface of the FV that can either be
    * "Cyclic" or "Sporadic". Set the period accordingly.
    */

   if (PI == i->direction) {
      switch (i->rcm)
      {
         case cyclic:
            {
317 318
               op_kind = cyclic;
               period  = i->period;
Maxime Perrotin's avatar
Maxime Perrotin committed
319 320 321 322 323 324
               set_cyclic_name(i->name);
               break;
            }
         case sporadic:
         case variator:
            {
325 326
               op_kind = sporadic;
               period  = i->period; 
Maxime Perrotin's avatar
Maxime Perrotin committed
327 328 329 330 331
               break;
            }
         default:
            break;
      }
332 333 334 335 336 337
      /* If it is a sporadic PI, check that it is actually connected,
       * and otherwise do not generate any aadl FEATURE (port)
       */
      if (sporadic == op_kind && NULL == Find_All_Calling_FV(i)) {
          return;
      }
Maxime Perrotin's avatar
Maxime Perrotin committed
338 339 340 341 342 343 344 345 346
   }


   /*
    * a. Write the interface-related data in the THREAD declaration
    * Note: the case of cyclic PI is treated separately.
    */

   if (cyclic != i->rcm) {
347
      /*
Maxime Perrotin's avatar
Maxime Perrotin committed
348 349 350 351 352 353 354
       * Add a "FEATURE" keyword (once) if
       * the interface is sporadic or protected but NOT unprotected
       * (protected appear in the concurrency view, not unpro).
       * Addition: if protected AND using POHIC
       * (in Ada nothing is generated for protectedRI)
       */

355
      if (false == first_interface && unprotected != i->rcm) {
Maxime Perrotin's avatar
Maxime Perrotin committed
356

357
         /*
Maxime Perrotin's avatar
Maxime Perrotin committed
358 359 360
          * Don't do it if the interface is protected when using Ada runtime
          */
         if (!(protected == i->rcm && !get_context()->polyorb_hi_c)) {
361 362
             fprintf(thread, "features\n");
             first_interface = true;
Maxime Perrotin's avatar
Maxime Perrotin committed
363 364 365 366
         }
      }

      if (asynch == i->synchronism) {
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
          fprintf(thread, "\t%s_%s : %s EVENT %sPORT",
                            (PI == i->direction) ? "INPORT" : "OUTPORT",
                            i->name,
                            (PI == i->direction) ? "IN"     : "OUT",
                            (NULL != i->in)      ? "DATA "  : "");

          if (NULL != i->in) {
             fprintf(thread, " DataView::%s_Buffer.impl",
                             i->in->value->type);
          }

          if (PI == i->direction) {
              fprintf(thread,
                      "\n\t\t{ Compute_Execution_Time => %llu %s .. %llu %s;\n",
                      (long long unsigned) i->wcet_low,
                      i->wcet_low_unit,
                      (long long unsigned) i->wcet_high,
                      i->wcet_high_unit);

              if (i->rcm == sporadic) {
                  fprintf(thread, "\t\t  Queue_Size => %lld;\n", i->queue_size);
              }

              if (USE_PO_HI_C (i->parent_fv)) { /* PolyORB-HI/C */
                  fprintf(thread, "\t\t  Compute_Entrypoint_Source_Text => "
                                  "\"po_hi_c_%s_%s\"; };\n",
                                  i->parent_fv->name,
                                  i->name);
              }
              else {
                  fprintf(thread, "\t\t  Compute_Entrypoint_Source_Text => "
                                  "\"%s_wrappers.%s\"; };\n",
                                  i->parent_fv->name,
                                  i->name);
              }
          }
          else {
              fprintf(thread, ";\n");
          }
Maxime Perrotin's avatar
Maxime Perrotin committed
406 407 408 409 410 411 412 413 414 415
      }
   }
}

/* Write postamble data in the files and close them. */
void End_VT_Backend(FV *fv)
{
   write_thread_postamble(fv);
   write_thread_implementation(fv);
   if (NULL != cyclic_name) {
416 417
       free(cyclic_name);
       cyclic_name = NULL;
Maxime Perrotin's avatar
Maxime Perrotin committed
418 419 420 421 422 423 424 425 426 427 428
   }

   fprintf(thread, "end %s_CV_Thread;\n", fv->name);

   close_file (&thread);
}

/* External interface: Generate the threads for the vertical transformation */
void GLUE_VT_Backend(FV *fv)
{
        String_list *list_of_required_po = NULL;
429
        first_interface = false;
Maxime Perrotin's avatar
Maxime Perrotin committed
430 431 432


        if (thread_runtime == fv->runtime_nature) {
433 434 435 436 437 438
            FOREACH (i, Interface, fv->interfaces, {
                if (protected == i->rcm) {
                    ADD_TO_SET (String, list_of_required_po, i->distant_fv);
                }
                Add_IF_to_VT(i);
            })
Maxime Perrotin's avatar
Maxime Perrotin committed
439 440

        if (get_context()->polyorb_hi_c && NULL != thread) {
441 442
            FOREACH (po, String, list_of_required_po, {
                fprintf(thread, "\t%s_protected :"
Maxime Perrotin's avatar
Maxime Perrotin committed
443 444 445
                                " requires data access process_package::"
                                "TASTE_Protected.Object;\n",
                                po);
446
            })
Maxime Perrotin's avatar
Maxime Perrotin committed
447 448 449 450 451 452 453 454 455 456 457
        }

      End_VT_Backend(fv);
   }
}


/* Create the files and put headers */
int Init_Process_Backend()
{
   Package_list *pkgs_list = get_system_ast()->packages;
458 459 460
   char   *filename    = NULL;   /* to store the filename */
   size_t len          = 0;      /* length of the filename */
   char   *path        = NULL;
Maxime Perrotin's avatar
Maxime Perrotin committed
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

   if (NULL != get_context()->output) {
      build_string(&path, get_context()->output,
            strlen(get_context()->output));
   }

   build_string(&path, "ConcurrencyView", strlen("ConcurrencyView"));

   len = strlen ("process.aadl");
   filename = (char *) malloc (len+1);

   assert(NULL != filename);
   /* create the Aadl files in a directory called "ConcurrencyView" */
   sprintf(filename, "process.aadl");
   create_file(path, filename, &process);

   free (filename);

   assert(NULL != process);

   fprintf(process,
       "--  This file was generated automatically: DO NOT MODIFY IT !\n\n"
       "--  This file contains a part of the system CONCURRENCY VIEW.\n"
       "--  It is an input file for OCARINA.\n\n"
       "package process_package\n"
       "public\n"
       "\twith Deployment;\n"
       "\twith DataView;\n"
       "\twith interfaceview::IV;\n"
       "\twith deploymentview::DV;\n");
491

Maxime Perrotin's avatar
Maxime Perrotin committed
492
   while (pkgs_list != NULL) {
493 494
       fprintf(process, "\twith %s;\n", pkgs_list->value);
       pkgs_list = pkgs_list->next;
Maxime Perrotin's avatar
Maxime Perrotin committed
495
   }
496

Maxime Perrotin's avatar
Maxime Perrotin committed
497 498 499
   /* add "with thread_CV_Thread;"  for all threads (bindings)
    * of all processes */
   FOREACH(p, Process, get_system_ast()->processes, {
500 501 502 503 504
       FOREACH(b, Aplc_binding, p->bindings, {
           if (thread_runtime == b->fv->runtime_nature) {
               fprintf(process, "\twith %s_CV_Thread;\n", b->fv->name);
           }
       });
Maxime Perrotin's avatar
Maxime Perrotin committed
505
   });
506 507

   /*
Maxime Perrotin's avatar
Maxime Perrotin committed
508 509 510 511 512
    * If there is at lease one protected interface in a process,
    * generate a "protected object" data in AADL, so that
    * PolyORB-HI-C will know that it needs to generate
    * semaphores.
    * This is not needed for PolyORB-Hi-Ada since Protected
513 514
    * objects are already part of the Ada language, so it is
    * not required to explicitely configure the underlying
Maxime Perrotin's avatar
Maxime Perrotin committed
515 516 517
    * operating system with the number of needed semaphores.
    */
   if (get_context()->polyorb_hi_c) {
518
       bool haspro = false;
Maxime Perrotin's avatar
Maxime Perrotin committed
519 520
       FOREACH(fv, FV, get_system_ast()->functions, {
                FOREACH(i, Interface, fv->interfaces, {
521
                    if (protected == i->rcm) haspro = true;
Maxime Perrotin's avatar
Maxime Perrotin committed
522 523 524 525 526 527 528
                });
       });
       if (haspro) {
           fprintf(process,
                   "\nDATA TASTE_Protected\n"
                   "PROPERTIES\n"
                   "\tConcurrency_Control_Protocol"
529
                   " => Protected_Access;\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
530 531 532 533
                   "END TASTE_Protected;\n\n"
                   "DATA IMPLEMENTATION TASTE_Protected.Object\n"
                   "PROPERTIES\n"
                   "\tConcurrency_Control_Protocol"
534
                   " => Protected_Access;\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
535 536 537 538 539 540 541 542 543 544 545
                   "END TASTE_Protected.Object;\n\n");
       }
   }
   free(path);
   return 0;
}


void SetThread(Aplc_binding *b)
{
   if(thread_runtime==b->fv->runtime_nature){
546 547 548 549 550
       fprintf(process, "\t%s : thread %s_CV_Thread::%s_%s.others;\n",
                        b->fv->name,
                        b->fv->name,
                        b->fv->name,  /* used to be aplc / should be namespace */
                        b->fv->name);
Maxime Perrotin's avatar
Maxime Perrotin committed
551 552 553
   }
}

554
/*
Maxime Perrotin's avatar
Maxime Perrotin committed
555 556 557 558 559 560 561 562 563 564
 * In the case of PolyORB-HI-C specify the need for semaphores
 * to act as Ada protected objects
 * => in the "process implementation" part of the "process.aadl" file,
 * add "<passive_with_pro_pi_ID>_protected : data TASTE_Protected.Object;"
 * This will provoke the generation of POHIC semaphores that can
 * be locked/unlocked when the passive functions are invoked.
 */
void SetProtectedObject(Aplc_binding *b)
{
    if (passive_runtime == b->fv->runtime_nature) {
565
        bool haspro = false;
Maxime Perrotin's avatar
Maxime Perrotin committed
566
        FOREACH(i, Interface, b->fv->interfaces, {
567
            if (PI == i->direction && protected == i->rcm) haspro = true;
Maxime Perrotin's avatar
Maxime Perrotin committed
568 569 570 571 572 573 574 575 576 577 578 579
        });
        if (haspro) {
            fprintf(process, "\t%s_protected : data TASTE_Protected.Object;\n",
                    b->fv->name);
        }
    }
}

/* FindDistantFV: returns NULL if the distant FV is in the binding list. */
void FindDistantFV(Aplc_binding *b, char **result)
{
   if (NULL!=*result) {
580 581 582
       if(!strcmp(b->fv->name, *result)) {
          *result = NULL;
       }
Maxime Perrotin's avatar
Maxime Perrotin committed
583 584 585 586 587 588 589
   }
}


/* Create the node file used to describe distribution */
int Create_Nodes_file(Process_list *p)
{
590
   char *filename = "nodes";
Maxime Perrotin's avatar
Maxime Perrotin committed
591 592 593 594 595 596 597 598 599 600 601
   char *path=NULL;

   assert (NULL != p);

   if (NULL != p->value && NULL != p->value->bindings 
         && NULL != get_context()->output) {
      build_string(&path, get_context()->output,
            strlen(get_context()->output));
   }

   build_string(&path, "ConcurrencyView", strlen("ConcurrencyView"));
602

Maxime Perrotin's avatar
Maxime Perrotin committed
603 604
   /* create the nodes file in a directory called "ConcurrencyView" */
   create_file(path, filename, &nodes);
605

Maxime Perrotin's avatar
Maxime Perrotin committed
606
   assert (NULL != nodes);
607

Maxime Perrotin's avatar
Maxime Perrotin committed
608 609 610 611 612
   /* Write file headers. */
   fprintf(nodes,
         "--  This file was generated automatically: DO NOT MODIFY IT !\n\n"
         "--  This file contains the description of the system distribution.\n"
         "--  It is an input file for the TASTE ORCHESTRATOR\n\n");
613

Maxime Perrotin's avatar
Maxime Perrotin committed
614 615 616 617 618 619 620 621 622 623 624 625 626
   free(path);
   return 0;
}

//void DeclareProcessFeature(Interface *i, Process **current_process)
void DeclareProcessFeatures (Aplc_binding *b,
                             Process **current_process,
                             Port_list **port_list)
{
   char         *distant_fv     = NULL;
   Port         *port           = NULL;
   Port_list    *ports          = NULL;

627
   ports = *port_list;
Maxime Perrotin's avatar
Maxime Perrotin committed
628 629 630 631 632 633

   FOREACH (i, Interface, b->fv->interfaces, {
       /*
        * Don't connect synchronous and cyclic blocks
        */

634 635
       if (RI == i->direction) {
           distant_fv = make_string("%s", i->distant_fv);
Maxime Perrotin's avatar
Maxime Perrotin committed
636

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 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 681 682
           /*
            * check if distant FV is in the same process
            * (distant_fv becomes NULL in this case)
            */
           FOREACH(binding, Aplc_binding, (*current_process)->bindings, {
               FindDistantFV(binding, &distant_fv);
           });
           if (asynch == i->synchronism && cyclic != i->rcm && NULL != distant_fv) {
               /* Distant PI not in the same process */
               if (features_declared == false) {
                   fprintf(process, "features\n");
                   features_declared = true;
               }

               if (i->in != NULL) {
                   port = make_string ("\tOUTPORT_%s_%s : OUT EVENT DATA PORT"
                                       " DataView::%s_Buffer.impl;\n",
                                       i->parent_fv->name,
                                       i->port_name,
                                       i->in->value->type);
               }
               else {
                   port = make_string ("\tOUTPORT_%s_%s : OUT EVENT PORT;\n",
                                       i->parent_fv->name,
                                       i->port_name);
               }
               ADD_TO_SET(Port, ports, port)
           }
       }
       else { // PI
           FV_list *fv_callers = Find_All_Calling_FV(i);

           FOREACH(caller, FV, fv_callers, {
               distant_fv = make_string("%s", caller->name);
               /*
                * check if distant FV is in the same process
                * (distant_fv becomes NULL in this case)
                */
               FOREACH(binding, Aplc_binding, (*current_process)->bindings, {
                   FindDistantFV(binding, &distant_fv);
               })
               if (asynch == i->synchronism && cyclic != i->rcm && NULL != distant_fv) {
                   /* Distant RI not in the same process */
                   if (false == features_declared) {
                       fprintf(process, "features\n");
                       features_declared = true;
Maxime Perrotin's avatar
Maxime Perrotin committed
683
                   }
684 685

                   if (NULL != i->in) {
Maxime Perrotin's avatar
Maxime Perrotin committed
686
                           port = make_string (
687 688 689 690 691 692 693
                                      "\tINPORT_%s_%s : IN EVENT DATA PORT"
                                      " DataView::%s_Buffer.impl;\n",
                                      i->parent_fv->name,
                                      i->port_name,
                                      i->in->value->type);
                   }
                   else {
Maxime Perrotin's avatar
Maxime Perrotin committed
694
                           port = make_string (
695 696 697
                                      "\tINPORT_%s_%s : IN EVENT PORT;\n",
                                      i->parent_fv->name,
                                      i->port_name);
Maxime Perrotin's avatar
Maxime Perrotin committed
698
                   }
699 700 701
                   ADD_TO_SET(Port, ports, port)
               }
           });
Maxime Perrotin's avatar
Maxime Perrotin committed
702
       }
703

Maxime Perrotin's avatar
Maxime Perrotin committed
704 705 706 707 708 709 710 711 712 713
   });
   *port_list = ports;

}


/* Connect async interfaces (internal and external to the process) */
void ProcessConnectInterfaces(Interface *i, Process **current_process)
{
   bool remote_in_same_process = true;
714
   char *connection = NULL;
Maxime Perrotin's avatar
Maxime Perrotin committed
715

716
   /*
Maxime Perrotin's avatar
Maxime Perrotin committed
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
    * Don't connect synchronous and cyclic blocks
    */
   if(asynch != i->synchronism || cyclic == i->rcm) return;

   /* Determine if the remote FV connected to this interface is
      is the same process or it if it distributed */
   if (PI == i->direction) {
       FOREACH (ct, FV, i->calling_threads, {
           bool here = false;
           FOREACH (b, Aplc_binding, (*current_process)->bindings, {
               if (!strcmp (b->fv->name, ct->name)) here = true;
           });
           if (false == here) remote_in_same_process = false;
       });
   }
   else if (RI == i->direction && NULL != i->distant_fv) {
       remote_in_same_process = false;
       FOREACH(b, Aplc_binding, (*current_process)->bindings, {
           if (!strcmp (b->fv->name, i->distant_fv)) {
               remote_in_same_process = true;
           }
       });
   }


   switch (i->direction) {
      case PI:
         if (true == remote_in_same_process) {
            /*
             * if distant FV is in the same process, we do not do anything here
             * because it is handled by the RI (see below)
             */
         }
         else {
            /*
             * distant FV not in the same process
             */
754 755 756 757 758
            connection = make_string("\tPORT INPORT_%s_%s -> %s.INPORT_%s;\n",
                                     i->parent_fv->name,
                                     i->port_name,
                                     i->parent_fv->name,
                                     i->name);
Maxime Perrotin's avatar
Maxime Perrotin committed
759 760 761 762 763 764 765
         }
         break;
      case RI:
         if (true == remote_in_same_process) {
             /* if distant FV is in the same process */
             /* We do it only for RI and not for PI (see above) because RIs
             * have a unique, thus more reliable Distant FV value. */
766 767 768 769 770 771
             connection = make_string("\tPORT %s.OUTPORT_%s -> %s.INPORT_%s;\n",
                                      i->parent_fv->name,
                                      i->name,
                                      i->distant_fv,
                                      (NULL != i->distant_name)? i->distant_name:i->name
                                      );
Maxime Perrotin's avatar
Maxime Perrotin committed
772 773
         }
         else { /* Distant RI not in the same process */
774 775 776 777 778
            connection = make_string("\tPORT %s.OUTPORT_%s -> OUTPORT_%s_%s;\n",
                                     i->parent_fv->name,
                                     i->name,
                                     i->parent_fv->name,
                                     i->port_name);
Maxime Perrotin's avatar
Maxime Perrotin committed
779 780 781
         }
         break;
   }
782 783 784 785 786 787 788 789 790 791
   if (NULL != connection) {
       if (0==(*current_process)->connections) {
          fprintf(process,"connections\n");
          (*current_process)->connections = 1;
       }

       fprintf(process, "%s", connection);

       free(connection);
   }
Maxime Perrotin's avatar
Maxime Perrotin committed
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
}

void ProcessMakeConnections (Aplc_binding *b, Process **p)
{
   FOREACH(i, Interface, b->fv->interfaces, {
    ProcessConnectInterfaces(i, p);
   });
}


void GenerateProcessImplementation(Process *p)
{
   if (NULL == process || NULL == nodes) return;

   /* Update the Nodes file (used for distribution): */
   fprintf (nodes, "* %s", p->identifier);
   if ( (p->cpu != NULL) && (p->cpu->platform_name != NULL)
           && (strlen (p->cpu->platform_name) > 0)) {
      fprintf (nodes, " %s", p->cpu->platform_name);
   }
   else {
      fprintf (nodes, " default");
   }
Maxime Perrotin's avatar
Maxime Perrotin committed
815 816 817
   if (p->coverage) {
       fprintf(nodes, " coverage");
   }
Maxime Perrotin's avatar
Maxime Perrotin committed
818 819 820
   fprintf (nodes, "\n");
   FOREACH(b, Aplc_binding, p->bindings, { 
      fprintf(nodes, "%s\n", b->fv->name);
821
   });
Maxime Perrotin's avatar
Maxime Perrotin committed
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
   /* End distribution */

   Port_list *ports=NULL;
   /* The following line will only work with Ellidiss 1.3.5
    * Old deployment views have to be converted first
    * Using the "future" flag to keep supporting old models
    * (could be fixed by reading the Version property, but
    * a bug in ocarina prevents from doing so - TBC by Jerome)
    */
   if (!get_context()->future) {
       /* 1.3.5 form */
       fprintf(process,"\nprocess %s extends deploymentview::DV::%s::%s\n",
           p->name, p->processor_board_name, p->name);
   }
   else {
       /* 1.2 form */
       fprintf(process,"\nprocess %s extends deploymentview::DV::%s\n",
           p->name, p->name);
   }
   FOREACH(b, Aplc_binding, p->bindings, {
      if (thread_runtime == b->fv->runtime_nature) {
          DeclareProcessFeatures(b, &p, &ports);
      }
845
   });
Maxime Perrotin's avatar
Maxime Perrotin committed
846 847
   FOREACH (port, Port, ports, {
     fprintf(process, "%s", port);
848
   });
Maxime Perrotin's avatar
Maxime Perrotin committed
849 850 851 852 853 854 855 856 857 858
   FREE_LIST(Port, ports)

   fprintf(process,"end %s;\n", p->name);

   fprintf(process,"\nprocess implementation %s.final\n", p->name);
   fprintf(process,"subcomponents\n");

   FOREACH(b, Aplc_binding, p->bindings, {
     SetThread(b);
     if (get_context()->polyorb_hi_c) SetProtectedObject(b);
859
   });
Maxime Perrotin's avatar
Maxime Perrotin committed
860 861 862

   FOREACH(b, Aplc_binding, p->bindings, {
    if (thread_runtime == b->fv->runtime_nature) ProcessMakeConnections(b, &p);
863
   });
Maxime Perrotin's avatar
Maxime Perrotin committed
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909

   if (get_context()->polyorb_hi_c) {
       /* The algo is :
        * for each thread T binded to the current process P
        *     for each protected RI of T is protected,
        *         get the corresponding (distant) FV name and store it in a set
        * then
        * for each FV stored in the set, add a link between the thread
        * and the corresponding protected object (FV of the set)
        */
        FOREACH(b, Aplc_binding, p->bindings, {
            Protected_Object_Name_list  *set_of_protected = NULL;

            if (thread_runtime == b->fv->runtime_nature) {
                if (get_context()->test) {
                    printf("\n[thread %s]\n", b->fv->name);
                }
                FOREACH (i, Interface, b->fv->interfaces, {
                    if (protected == i->rcm && RI == i->direction) {
                        ADD_TO_SET (Protected_Object_Name,
                                    set_of_protected,
                                    i->distant_fv);
                        if (get_context()->test) {
                            printf(" PO %s (Pro RI %s)\n",
                                i->distant_fv, i->name);
                        }
                    }
                });
                FOREACH(po, Protected_Object_Name, set_of_protected, {
                    if (0 == p->connections) {
                        fprintf(process,"connections\n");
                        p->connections = 1;
                    }
                    fprintf(process,
                     "\t%s_%s: data access %s_protected -> %s.%s_protected;\n",
                         b->fv->name,
                         po,
                         po,
                         b->fv->name,
                         po);
                });
            }
        });
   }

   fprintf(process,"end %s.final;\n\n", p->name);
910
   features_declared = false;
Maxime Perrotin's avatar
Maxime Perrotin committed
911 912 913 914 915 916 917 918 919
}

void GenerateProcessRefinement(Process *p)
{
   fprintf(process, "  %s: process %s.final;\n", p->identifier, p->name);
}

void GenerateProcessor(Process *p, Process_list *processes)
{
920 921 922 923
   FOREACH(p2, Process, processes, {
      if (p2 == p) {
         break;
      }
Maxime Perrotin's avatar
Maxime Perrotin committed
924

925 926 927 928 929 930 931 932 933 934
      if ( (p->cpu != NULL) &&
           (p2->cpu != NULL) &&
           (p2->cpu->name != NULL) &&
           (p->cpu->name != NULL) &&
           strcmp (p2->cpu->name, p->cpu->name) == 0) {
         return;
      }
   })

   fprintf(process, "%s : processor %s;\n", p->cpu->name, p->cpu->classifier);
Maxime Perrotin's avatar
Maxime Perrotin committed
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 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 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 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126

}

/* External interface: Create the PROCESS IMPLEMENTATION part */
void Generate_Full_ConcurrencyView(Process_list *processes, char *root_node)
{
   system_root_node = root_node;

   Create_Nodes_file(processes);

   /* Create the process.aadl file */
   Init_Process_Backend();

   FOREACH(p, Process, processes, {
     GenerateProcessImplementation(p);
   })

   /* postamble: new system implementation */
   if (NULL!= process) {

       fprintf(process, "system %s\nend %s;\n\n", root_node, root_node);
       fprintf(process, "system implementation %s.final\n", root_node);

       fprintf(process, "subcomponents\n");
       FOREACH(b, Bus, get_system_ast()->buses, {
           fprintf(process, "  %s_cv : bus %s;\n", b->name, b->classifier);
       });

      /* Add all drivers of all processes to the complete system */
      FOREACH (p, Process, processes, {
        FOREACH(b, Device, p->drivers, {
                char *driver_no_underscore =
                        underscore_to_dash (b->name, strlen (b->name));
                fprintf(process,
                    "  %s_cv : device %s\n\t{\n\t\tSource_Text =>"
                    " (\"../DriversConfig/%s/DeviceConfig-%s",
                    b->name,
                    b->classifier,
                    p->name,
                    driver_no_underscore);
                if (get_context()->polyorb_hi_c) {
                        fprintf (process,".c");
                }
                else {
                        fprintf (process, ".ads");
                }
                fprintf (process, "\");\n");
                /* Specify the name of the configuration variable
                 * for the driver */
                fprintf (process, "\t\tType_Source_Name => \"");
                /* in C: */
                if (get_context()->polyorb_hi_c) {
                        fprintf (process, "pohidrv_%s_cv",
                            b->name);
                }
                /* or in Ada */
                else {
                        fprintf (process, "DeviceConfig_%s.pohidrv_%s_cv",
                                b->name,
                                b->name);
                }
                fprintf (process, "\";\n");
                fprintf (process, "\t}");
                fprintf(process, ";\n");
                free (driver_no_underscore);
        });
     });

      FOREACH(p, Process, processes, {
        GenerateProcessRefinement(p);
      });

      FOREACH(p, Process, processes, {
          FOREACH(p2, Process, processes, {
              if (p2 == p) {
                  break;
              }

               if ((p->cpu != NULL) &&
                   (p2->cpu != NULL) &&
                   (p2->cpu->name != NULL) &&
                   (p->cpu->name != NULL) &&
                  strcmp (p2->cpu->name, p->cpu->name) == 0) {
                  return;
               }
            });
            fprintf(process, "  %s_cv : processor %s;\n",
                             p->cpu->name,
                             p->cpu->classifier);
         });

          /* Create the connections between the ports at
           * "system implementation deploymentview.final" level
           * this means connect the port name of async RIs to
           * the distant port name */

          if (NULL != get_system_ast()->connections) {
              fprintf(process, "connections\n");
              system_connections_declared = 1;
          }
          FOREACH(cnt, Connection, get_system_ast()->connections, {
                  char  *src_proc=NULL;
                  char  *dest_proc=NULL;

                  FOREACH (local_fv, FV, get_system_ast()->functions, {
                      if (!strcmp(local_fv->name, cnt->src_system)) {
                          src_proc = local_fv->process->name;
                      }
                      if (!strcmp(local_fv->name, cnt->dst_system)) {
                          dest_proc = local_fv->process->name;
                      }
                  });

                  fprintf(process,
                       "  %s_%s_%s_conn_cv :"
                       " port %s.OUTPORT_%s_%s -> %s.INPORT_%s_%s;\n",
                       src_proc,
                       cnt->dst_port,
                       cnt->dst_system,
                       dest_proc,
                       cnt->dst_system,
                       cnt->dst_port,
                       src_proc,
                       cnt->src_system,
                       cnt->src_port);
          })

         FOREACH (p, Process, processes, {
             FOREACH(b, Device, p->drivers, {
                 if (b->accessed_bus != NULL) {
                     if (0 == system_connections_declared) {
                         fprintf(process, "connections\n");
                         system_connections_declared = 1;
                      }
                     fprintf(process, "  bus access %s_cv -> %s_cv.%s;\n",
                                b->accessed_bus,
                                b->name,
                                b->access_port);
                 }
             });
        });

         fprintf (process, "properties\n");
         FOREACH(p, Process, processes, {
             fprintf (process, "  Actual_Processor_Binding =>"
                              " (reference (%s_cv)) applies to %s;\n",
                              p->cpu->name,
                              p->name);
         });

         FOREACH (p, Process, processes, {
             FOREACH(b, Device, p->drivers, {
                 fprintf(process,
                         "  Actual_Processor_Binding =>"
                         " (reference (%s_cv)) applies to %s_cv;\n",
                         b->associated_processor,
                         b->name);
             });
         });

         FOREACH(cnt, Connection, get_system_ast()->connections, {
             char *src_proc = NULL;
             FOREACH (local_fv, FV, get_system_ast()->functions, {
                 if (!strcmp(local_fv->name, cnt->src_system)) {
                     src_proc = local_fv->process->name;
                 }
             });
             fprintf(process,
                 "  Actual_Connection_Binding =>"
                 " (reference (%s_cv)) applies to %s_%s_%s_conn_cv;\n", 
                 cnt->bus,
                 src_proc,
                 cnt->dst_port,
                 cnt->dst_system);
         });


        fprintf(process, "end %s.final;\n", root_node);


        fprintf(process, "end process_package;\n");

        /*
         * Following line is used by the main orchestrator 
         * to know the name of the system (for -I purposes)
        */
        fprintf(process, "\n-- %s.final\n", root_node);

        fclose (process);
    }
    close_file (&nodes);
}