ocarina-backends-po_hi_ada-deployment.adb 58.9 KB
Newer Older
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--                  OCARINA.BACKENDS.PO_HI_ADA.DEPLOYMENT                   --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
yoogx's avatar
yoogx committed
9
--    Copyright (C) 2006-2009 Telecom ParisTech, 2010-2017 ESA & ISAE.      --
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
--                                                                          --
-- Ocarina  is free software; you can redistribute it and/or modify under   --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. Ocarina is distributed in the hope that it will be useful, but     --
-- WITHOUT ANY WARRANTY; without even the implied warranty of               --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
26
--                                                                          --
jhugues's avatar
jhugues committed
27 28
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
29 30 31
--                                                                          --
------------------------------------------------------------------------------

32
with Ocarina.Namet;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

with Ocarina.ME_AADL;
with Ocarina.ME_AADL.AADL_Instances.Nodes;
with Ocarina.ME_AADL.AADL_Instances.Nutils;
with Ocarina.ME_AADL.AADL_Instances.Entities;

with Ocarina.Backends.Utils;
with Ocarina.Backends.Messages;
with Ocarina.Backends.Properties;
with Ocarina.Backends.Ada_Values;
with Ocarina.Backends.Ada_Tree.Nutils;
with Ocarina.Backends.Ada_Tree.Nodes;
with Ocarina.Backends.PO_HI_Ada.Mapping;
with Ocarina.Backends.PO_HI_Ada.Runtime;

package body Ocarina.Backends.PO_HI_Ada.Deployment is

50
   use Ocarina.Namet;
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
   use Ocarina.ME_AADL;
   use Ocarina.ME_AADL.AADL_Instances.Nodes;
   use Ocarina.ME_AADL.AADL_Instances.Entities;
   use Ocarina.Backends.Utils;
   use Ocarina.Backends.Messages;
   use Ocarina.Backends.Properties;
   use Ocarina.Backends.Ada_Values;
   use Ocarina.Backends.Ada_Tree.Nutils;
   use Ocarina.Backends.PO_HI_Ada.Mapping;
   use Ocarina.Backends.PO_HI_Ada.Runtime;

   package AAN renames Ocarina.ME_AADL.AADL_Instances.Nodes;
   package AAU renames Ocarina.ME_AADL.AADL_Instances.Nutils;
   package ADN renames Ocarina.Backends.Ada_Tree.Nodes;

   ------------------
   -- Package_Spec --
   ------------------

   package body Package_Spec is
yoogx's avatar
yoogx committed
71
      procedure Visit_Subcomponents_Of is new Visit_Subcomponents_Of_G (Visit);
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

      procedure Visit_Architecture_Instance (E : Node_Id);
      procedure Visit_Component_Instance (E : Node_Id);
      procedure Visit_System_Instance (E : Node_Id);
      procedure Visit_Process_Instance (E : Node_Id);
      procedure Visit_Thread_Instance (E : Node_Id);
      procedure Visit_Data_Instance (E : Node_Id);
      procedure Visit_Device_Instance (E : Node_Id);

      Last_Node_Enum_Pos   : Nat := 1;
      Last_Thread_Enum_Pos : Nat := 1;
      Last_Port_Enum_Pos   : Nat := 1;
      --  Global variables for the representation clauses of the
      --  generated enumerator types. They point to the next free
      --  position in the corresponding enumeration type. We start by
      --  1 because 0 is the value to indicate that no position has
      --  been assigned to the enumerator yet.

      function Get_Node_Enum_Pos (Name : Name_Id) return Nat;
      function Get_Thread_Enum_Pos (Name : Name_Id) return Nat;
      function Get_Port_Enum_Pos (Name : Name_Id) return Nat;
      --  Return the position corresponding to the enumerator. If no
      --  position has been computed for the given enumerator, compute
      --  a new one.

      function Added_Internal_Name (P : Node_Id; E : Node_Id) return Name_Id;
      function Is_Added (P : Node_Id; E : Node_Id) return Boolean;
      procedure Set_Added (P : Node_Id; E : Node_Id);
      --  Used to ensure that the enumerator for Process P and all the
      --  enumerator corresponding to its threads are added only once
      --  to the deployment package of node E.

      type Get_Enum_Pos_Access is access function (Name : Name_Id) return Nat;
      procedure Insert_Node_In_List
        (N            : Node_Id;
         L            : List_Id;
         Get_Enum_Pos : Get_Enum_Pos_Access);
      --  Insert the node N into list L. N is assumed to be an
      --  enumerator or an element association relative to an
      --  enumerator. The insertion is done so that the positions of
      --  all the enumerators in the list are ordered
      --  incresingly. This is necessary in Ada since the enumerator
      --  position order must respect their declaration order.

      function Max_Payload_Size_Declaration (E : Node_Id) return Node_Id;
      --  Builds the Max_Payload_Size constant declaration
      --  corresponding to node E.

      Max_Payload    : Unsigned_Long_Long;
      Max_Payload_Of : Name_Id := No_Name;
      --  Value of Max_Payload_Size

      function Max_Node_Image_Size_Declaration (E : Node_Id) return Node_Id;
      --  Builds the Max_Node_Image_Size constant declaration
      --  corresponding to node E.

128
      Max_Node_Image_Size : Unsigned_Long_Long := 1;
129 130 131 132 133 134
      --  Value of Max_Node_Image_Size

      function Max_Entity_Image_Size_Declaration (E : Node_Id) return Node_Id;
      --  Builds the Max_Entity_Image_Size constant declaration
      --  corresponding to node E.

135
      Max_Entity_Image_Size : Unsigned_Long_Long := 1;
136 137 138 139 140 141
      --  Value of Max_Entity_Image_Size

      function Max_Port_Image_Size_Declaration (E : Node_Id) return Node_Id;
      --  Builds the Max_Port_Image_Size constant declaration
      --  corresponding to node E.

142
      Max_Port_Image_Size : Unsigned_Long_Long := 1;
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
      --  Value of Max_Port_Image_Size

      ------------------
      -- Global Lists --
      ------------------

      --  The lists below are global and initialized at the beginning
      --  of visit of each process instance. This does not cause a
      --  consistency nor a concurrency problem since the visits of
      --  processes are done sequentially and the visit of one process
      --  is performed entirely before beginning the visit of another
      --  one.

      Node_Enumerator_List : List_Id;
      --  Global list for all the distributed application nodes
      --  enumeration type.

      Node_Enumerator_Pos_List : List_Id;
      --  Global list to store the representation clause of the node
      --  enumeration type.

      Node_Image_List : List_Id;
      --  Global list that associates each node of the distributed
      --  application to its String image.

      Thread_Enumerator_List : List_Id;
      --  Global list for all the distributed application threads
      --  enumeration types.

      Thread_Enumerator_Pos_List : List_Id;
      --  Global list to store the representation clause of the thread
      --  enumeration type.

      Entity_Table_List : List_Id;
      --  Global list that associates each thread of the distributed
      --  application to its containing process. Concretly, this is a
      --  list of associations that contain for each enumerator of the
      --  Entity_Type type the corresponding enumerator of the
      --  Node_Type type.

      Entity_Image_List : List_Id;
      --  Global list that associates each thread of the distributed
      --  application to its String image.

      Port_Enumerator_List : List_Id;
      --  Global list for all the distributed application thread port
      --  enumeration types.

      Port_Enumerator_Pos_List : List_Id;
      --  Global list to store the representation clause of the thread
      --  port enumeration type.

      Port_Table_List : List_Id;
      --  Global list that associates each port of the distributed
      --  application to its containing thread. Concretly, this is a
      --  list of associations that contain for each enumerator of the
      --  Port_Type type the corresponding enumerator of the
      --  Entity_Type type.

      Port_Image_List : List_Id;
      --  Global list that associates each port of the distributed
      --  application to its image.

      -----------------------
      -- Get_Node_Enum_Pos --
      -----------------------

      function Get_Node_Enum_Pos (Name : Name_Id) return Nat is
         I_Name   : Name_Id;
         Position : Nat;
      begin
         Set_Str_To_Name_Buffer ("%node_enum_pos%");
         Get_Name_String (Name);
         I_Name := Name_Find;

         --  Check whether a value has been computed for the current
         --  enumerator. Otherwise, compute a new one.

         Position := Get_Name_Table_Info (I_Name);

         if Position = 0 then
            --  Get the next free position

            Position := Last_Node_Enum_Pos;

            --  Link it to the enumerator for future use

            Set_Name_Table_Info (I_Name, Position);

            --  Increment the next free position

            Last_Node_Enum_Pos := Last_Node_Enum_Pos + 1;
         end if;

         return Position;
      end Get_Node_Enum_Pos;

      -------------------------
      -- Get_Thread_Enum_Pos --
      -------------------------

      function Get_Thread_Enum_Pos (Name : Name_Id) return Nat is
         I_Name   : Name_Id;
         Position : Nat;
      begin
         Set_Str_To_Name_Buffer ("%thread_enum_pos%");
         Get_Name_String (Name);
         I_Name := Name_Find;

         --  Check whether a value has been computed for the current
         --  enumerator. Otherwise, compute a new one.

         Position := Get_Name_Table_Info (I_Name);

         if Position = 0 then
            --  Get the next free position

            Position := Last_Thread_Enum_Pos;

            --  Link it to the enumerator for future use

            Set_Name_Table_Info (I_Name, Position);

            --  Increment the next free position

            Last_Thread_Enum_Pos := Last_Thread_Enum_Pos + 1;
         end if;

         return Position;
      end Get_Thread_Enum_Pos;

      -----------------------
      -- Get_Port_Enum_Pos --
      -----------------------

      function Get_Port_Enum_Pos (Name : Name_Id) return Nat is
         I_Name   : Name_Id;
         Position : Nat;
      begin
         Set_Str_To_Name_Buffer ("%port_enum_pos%");
         Get_Name_String (Name);
         I_Name := Name_Find;

         --  Check whether a value has been computed for the current
         --  enumerator. Otherwise, compute a new one.

         Position := Get_Name_Table_Info (I_Name);

         if Position = 0 then
            --  Get the next free position

            Position := Last_Port_Enum_Pos;

            --  Link it to the enumerator for future use

            Set_Name_Table_Info (I_Name, Position);

            --  Increment the next free position

            Last_Port_Enum_Pos := Last_Port_Enum_Pos + 1;
         end if;

         return Position;
      end Get_Port_Enum_Pos;

      -------------------------
      -- Added_Internal_Name --
      -------------------------

      function Added_Internal_Name (P : Node_Id; E : Node_Id) return Name_Id is
      begin
         Set_Str_To_Name_Buffer ("%add%enumerator%");
         Add_Nat_To_Name_Buffer (Nat (P));
         Add_Char_To_Name_Buffer ('%');
         Add_Nat_To_Name_Buffer (Nat (E));

         return Name_Find;
      end Added_Internal_Name;

      --------------
      -- Is_Added --
      --------------

      function Is_Added (P : Node_Id; E : Node_Id) return Boolean is
         I_Name : constant Name_Id := Added_Internal_Name (P, E);
      begin
         return Get_Name_Table_Byte (I_Name) = 1;
      end Is_Added;

      ---------------
      -- Set_Added --
      ---------------

      procedure Set_Added (P : Node_Id; E : Node_Id) is
         I_Name : constant Name_Id := Added_Internal_Name (P, E);
      begin
         Set_Name_Table_Byte (I_Name, 1);
      end Set_Added;

      -------------------------
      -- Insert_Node_In_List --
      -------------------------

      procedure Insert_Node_In_List
        (N            : Node_Id;
         L            : List_Id;
         Get_Enum_Pos : Get_Enum_Pos_Access)
      is
         use ADN;

         M          : Node_Id;
         Position_N : Nat;
         Position_M : Nat;
      begin
357 358 359
         pragma Assert
           (Kind (N) = K_Defining_Identifier
            or else Kind (N) = K_Element_Association);
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

         case ADN.Kind (N) is
            when ADN.K_Defining_Identifier =>
               Position_N := Get_Enum_Pos (ADN.Name (N));
            when ADN.K_Element_Association =>
               Position_N := Get_Enum_Pos (ADN.Name (Index (N)));
            when others =>
               raise Program_Error with "Inconsistency in Insert_Node_In_List";
         end case;

         if Is_Empty (L) then
            Append_Node_To_List (N, L);
         else
            M := ADN.First_Node (L);

            while Present (M) loop
               case ADN.Kind (M) is
                  when ADN.K_Defining_Identifier =>
                     Position_M := Get_Enum_Pos (ADN.Name (M));
                  when ADN.K_Element_Association =>
                     Position_M := Get_Enum_Pos (ADN.Name (Index (M)));
                  when others =>
382 383
                     raise Program_Error
                       with "Inconsistency in Insert_Node_In_List";
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
               end case;

               if Position_N < Position_M then
                  Insert_Before_Node (N, M, L);

                  return; --  IMPORTANT
               end if;

               M := ADN.Next_Node (M);
            end loop;

            Append_Node_To_List (N, L);
         end if;
      end Insert_Node_In_List;

      ----------------------------------
      -- Max_Payload_Size_Declaration --
      ----------------------------------

      function Max_Payload_Size_Declaration (E : Node_Id) return Node_Id is
         pragma Unreferenced (E);

         N : Node_Id;
      begin
         --  The structure of a message payload is as follows:
         --  1 - A destination port (of type Deployment.Port_Type)
         --  2 - An optional time stamp (of type Ada.Real_Time.Time)
         --  3 - A data (of one of the marshallable types declared in
         --      the currently being generated package)

         --  Size corresponding to (3) has been built incrementally
         --  during the visiting of AADL data component instance. Add
         --  the size corresponding to (2) and (1)

         Max_Payload := Max_Payload + 64 + Port_Type_Size;
         --  64 is the size of Ada.Real_Time.Time. We cannot afford
         --  doing a Ada.Real_Time.Time'Size because 'Size for private
         --  Ada types is not a static expression.

423 424 425 426 427 428 429 430
         N :=
           Make_Object_Declaration
             (Defining_Identifier =>
                Make_Defining_Identifier (PN (P_Max_Payload_Size)),
              Constant_Present  => True,
              Object_Definition => RE (RE_Integer),
              Expression        =>
                Make_Literal (New_Integer_Value (Max_Payload, 1, 10)));
431 432 433 434 435 436 437 438

         return N;
      end Max_Payload_Size_Declaration;

      -------------------------------------
      -- Max_Node_Image_Size_Declaration --
      -------------------------------------

439
      function Max_Node_Image_Size_Declaration (E : Node_Id) return Node_Id is
440 441 442 443
         pragma Unreferenced (E);

         N : Node_Id;
      begin
444 445 446 447 448 449 450 451
         N :=
           Make_Object_Declaration
             (Defining_Identifier =>
                Make_Defining_Identifier (PN (P_Max_Node_Image_Size)),
              Constant_Present  => True,
              Object_Definition => RE (RE_Integer),
              Expression        =>
                Make_Literal (New_Integer_Value (Max_Node_Image_Size, 1, 10)));
452 453 454 455 456 457 458
         return N;
      end Max_Node_Image_Size_Declaration;

      ---------------------------------------
      -- Max_Entity_Image_Size_Declaration --
      ---------------------------------------

459 460 461
      function Max_Entity_Image_Size_Declaration
        (E : Node_Id) return Node_Id
      is
462 463 464 465
         pragma Unreferenced (E);

         N : Node_Id;
      begin
466 467 468 469 470 471 472 473 474
         N :=
           Make_Object_Declaration
             (Defining_Identifier =>
                Make_Defining_Identifier (PN (P_Max_Entity_Image_Size)),
              Constant_Present  => True,
              Object_Definition => RE (RE_Integer),
              Expression        =>
                Make_Literal
                  (New_Integer_Value (Max_Entity_Image_Size, 1, 10)));
475 476 477 478 479 480 481
         return N;
      end Max_Entity_Image_Size_Declaration;

      -------------------------------------
      -- Max_Port_Image_Size_Declaration --
      -------------------------------------

482
      function Max_Port_Image_Size_Declaration (E : Node_Id) return Node_Id is
483 484 485 486
         pragma Unreferenced (E);

         N : Node_Id;
      begin
487 488 489 490 491 492 493 494
         N :=
           Make_Object_Declaration
             (Defining_Identifier =>
                Make_Defining_Identifier (PN (P_Max_Port_Image_Size)),
              Constant_Present  => True,
              Object_Definition => RE (RE_Integer),
              Expression        =>
                Make_Literal (New_Integer_Value (Max_Port_Image_Size, 1, 10)));
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
         return N;
      end Max_Port_Image_Size_Declaration;

      -----------
      -- Visit --
      -----------

      procedure Visit (E : Node_Id) is
      begin
         case Kind (E) is
            when K_Architecture_Instance =>
               Visit_Architecture_Instance (E);

            when K_Component_Instance =>
               Visit_Component_Instance (E);

            when others =>
               null;
         end case;
      end Visit;

      ---------------------------------
      -- Visit_Architecture_Instance --
      ---------------------------------

      procedure Visit_Architecture_Instance (E : Node_Id) is
      begin
         Visit (Root_System (E));
      end Visit_Architecture_Instance;

      ------------------------------
      -- Visit_Component_Instance --
      ------------------------------

      procedure Visit_Component_Instance (E : Node_Id) is
530 531
         Category : constant Component_Category :=
           Get_Category_Of_Component (E);
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
      begin
         case Category is
            when CC_System =>
               Visit_System_Instance (E);

            when CC_Process =>
               Visit_Process_Instance (E);

            when CC_Thread =>
               Visit_Thread_Instance (E);

            when CC_Data =>
               Visit_Data_Instance (E);

            when others =>
               null;
         end case;
      end Visit_Component_Instance;

      -------------------------
      -- Visit_Data_Instance --
      -------------------------

      procedure Visit_Data_Instance (E : Node_Id) is
      begin
         --  Do not generate Ada type more than once

         if No (Get_Handling (E, By_Name, H_Ada_Deployment_Spec)) then
            --  Threads and processes cannot have opaque types in
            --  their features because this prevents the static
            --  computation of the buffer sizes.

            if Get_Source_Language (E) = Language_Ada_95 then
               Display_Located_Error
                 (Loc (E),
567 568
                  "This data type cannot be used in thread or process" &
                  " features",
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
                  Fatal => True);
            end if;

            --  If the type is sendable through network, we take into
            --  account its size when calculating the maximal message
            --  payload size.

            if Get_Data_Representation (E) /= Data_With_Accessors then
               declare
                  Data_Size : Unsigned_Long_Long;
               begin
                  if Get_Data_Size (E) /= Null_Size then
                     Data_Size := To_Bits (Get_Data_Size (E));
                  else
                     Data_Size := Estimate_Data_Size (E);
                  end if;

                  if Data_Size > Max_Payload then
                     Max_Payload    := Data_Size;
                     Max_Payload_Of := To_Ada_Name (Name (Identifier (E)));
                  end if;
               end;

               Set_Handling (E, By_Name, H_Ada_Deployment_Spec, E);
            end if;
         end if;
      end Visit_Data_Instance;

      ---------------------------
      -- Visit_Device_Instance --
      ---------------------------

      procedure Visit_Device_Instance (E : Node_Id) is
602 603
         Implementation : constant Node_Id := Get_Implementation (E);
         N              : Node_Id;
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
      begin
         if Implementation /= No_Node then
            if not AAU.Is_Empty (AAN.Subcomponents (Implementation)) then
               N := First_Node (Subcomponents (Implementation));
               while Present (N) loop
                  Visit_Component_Instance (Corresponding_Instance (N));
                  N := Next_Node (N);
               end loop;
            end if;
         end if;
      end Visit_Device_Instance;

      ----------------------------
      -- Visit_Process_Instance --
      ----------------------------

      procedure Visit_Process_Instance (E : Node_Id) is
621 622 623 624 625 626 627 628 629 630 631 632 633 634
         P       : constant Node_Id   := Map_HI_Node (E);
         U       : Node_Id;
         S       : constant Node_Id   := Parent_Subcomponent (E);
         Img_Len : Unsigned_Long_Long :=
           Get_Name_String (Map_Ada_Enumerator_Name (S))'Length;
         N          : Node_Id;
         C          : Node_Id;
         F          : Node_Id;
         Src        : Node_Id;
         Dst        : Node_Id;
         Parent     : Node_Id;
         S_Parent   : Node_Id;
         The_System : constant Node_Id :=
           Parent_Component (Parent_Subcomponent (E));
635 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 683
      begin
         Push_Entity (P);

         --  It is important that we push P at the top of the entity
         --  stack before generating the package unit.

         U := Map_HI_Unit (E);
         Push_Entity (U);
         Set_Deployment_Spec;

         --  Start recording the handling since they have to be reset
         --  for each node.

         Start_Recording_Handlings;

         --  Initialize Max_Payload

         if Need_Deliver (E) or else Need_Send (E) then
            Max_Payload := 0;
         end if;

         --  Create the lists

         Node_Enumerator_List       := New_List (ADN.K_Enumeration_Literals);
         Node_Enumerator_Pos_List   := New_List (ADN.K_Element_List);
         Node_Image_List            := New_List (ADN.K_Element_List);
         Thread_Enumerator_List     := New_List (ADN.K_Enumeration_Literals);
         Thread_Enumerator_Pos_List := New_List (ADN.K_Element_List);
         Entity_Table_List          := New_List (ADN.K_Element_List);
         Entity_Image_List          := New_List (ADN.K_Element_List);
         Port_Enumerator_List       := New_List (ADN.K_Enumeration_Literals);
         Port_Enumerator_Pos_List   := New_List (ADN.K_Element_List);
         Port_Table_List            := New_List (ADN.K_Element_List);
         Port_Image_List            := New_List (ADN.K_Element_List);

         --  The Deployment package must be preelaborated

         N := Make_Pragma_Statement (Pragma_Preelaborate, No_List);
         Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

         --  Build the enumerator corresponding to the distributed
         --  application node.

         N := Make_Defining_Identifier (Map_Ada_Enumerator_Name (S));
         Append_Node_To_List (N, Node_Enumerator_List);
         Bind_AADL_To_Enumerator (Identifier (S), N);

         --  Build the representation clause of the enumerator

684 685 686 687 688 689 690 691 692
         N :=
           Make_Element_Association
             (Make_Defining_Identifier (Map_Ada_Enumerator_Name (S)),
              Make_Literal
                (New_Integer_Value
                   (Unsigned_Long_Long
                      (Get_Node_Enum_Pos (Map_Ada_Enumerator_Name (S))),
                    1,
                    10)));
693
         Insert_Node_In_List
694 695 696
           (N,
            Node_Enumerator_Pos_List,
            Get_Node_Enum_Pos'Access);
697 698 699 700 701

         if Max_Node_Image_Size < Img_Len then
            Max_Node_Image_Size := Img_Len;
         end if;

702 703 704 705
         N :=
           Make_Element_Association
             (Make_Defining_Identifier (Map_Ada_Enumerator_Name (S)),
              Make_Literal (New_String_Value (Map_Ada_Enumerator_Name (S))));
706

707
         Insert_Node_In_List (N, Node_Image_List, Get_Node_Enum_Pos'Access);
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727

         --  Visit all the subcomponents of the process

         if not AAU.Is_Empty (Subcomponents (E)) then
            C := First_Node (Subcomponents (E));

            while Present (C) loop
               Visit (Corresponding_Instance (C));

               C := Next_Node (C);
            end loop;
         end if;

         --  Visit all devices attached to the parent system that
         --  share the same processor as process E.

         if not AAU.Is_Empty (Subcomponents (The_System)) then
            C := First_Node (Subcomponents (The_System));
            while Present (C) loop
               if AAU.Is_Device (Corresponding_Instance (C))
728 729 730
                 and then
                   Get_Bound_Processor (Corresponding_Instance (C)) =
                   Get_Bound_Processor (E)
731 732 733 734 735 736
               then
                  --  Build the enumerator corresponding to the device
                  --  Note: we reuse the process name XXX

                  N := Make_Defining_Identifier (Map_Ada_Enumerator_Name (S));
                  Bind_AADL_To_Enumerator
737 738
                    (Identifier (Corresponding_Instance (C)),
                     N);
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770

                  Visit_Device_Instance (Corresponding_Instance (C));
               end if;
               C := Next_Node (C);
            end loop;
         end if;

         --  For each of the processes P connected to E, (1) we add an
         --  enumerator corresponding to P and (2) for each one of the
         --  threads of P, we add an enumerator.

         if not AAU.Is_Empty (Features (E)) then
            F := First_Node (Features (E));

            while Present (F) loop
               --  The sources of F

               if not AAU.Is_Empty (Sources (F)) then
                  Src := First_Node (Sources (F));

                  while Present (Src) loop

                     Parent := Parent_Component (Item (Src));

                     if AAU.Is_Process (Parent)
                       and then Parent /= E
                       and then not Is_Added (Parent, E)
                     then
                        --  Add the process to the deployment
                        --  enumerators of E.

                        S_Parent := Parent_Subcomponent (Parent);
771 772 773 774 775 776
                        Img_Len  :=
                          Get_Name_String (Map_Ada_Enumerator_Name (S_Parent))'
                            Length;
                        N :=
                          Make_Defining_Identifier
                            (Map_Ada_Enumerator_Name (S_Parent));
777 778 779 780 781 782 783 784
                        Insert_Node_In_List
                          (N,
                           Node_Enumerator_List,
                           Get_Node_Enum_Pos'Access);

                        --  Add a representation clause for the
                        --  enumerator corresponding to Parent.

785 786 787 788 789 790 791 792 793 794 795
                        N :=
                          Make_Element_Association
                            (Make_Defining_Identifier
                               (Map_Ada_Enumerator_Name (S_Parent)),
                             Make_Literal
                               (New_Integer_Value
                                  (Unsigned_Long_Long
                                     (Get_Node_Enum_Pos
                                        (Map_Ada_Enumerator_Name (S_Parent))),
                                   1,
                                   10)));
796 797 798 799 800 801 802 803 804
                        Insert_Node_In_List
                          (N,
                           Node_Enumerator_Pos_List,
                           Get_Node_Enum_Pos'Access);

                        if Max_Node_Image_Size < Img_Len then
                           Max_Node_Image_Size := Img_Len;
                        end if;

805 806 807 808 809 810 811
                        N :=
                          Make_Element_Association
                            (Make_Defining_Identifier
                               (Map_Ada_Enumerator_Name (S_Parent)),
                             Make_Literal
                               (New_String_Value
                                  (Map_Ada_Enumerator_Name (S_Parent))));
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854

                        Insert_Node_In_List
                          (N,
                           Node_Image_List,
                           Get_Node_Enum_Pos'Access);

                        --  Traverse all the subcomponents of Parent

                        if not AAU.Is_Empty (Subcomponents (Parent)) then
                           C := First_Node (Subcomponents (Parent));

                           while Present (C) loop
                              Visit (Corresponding_Instance (C));

                              C := Next_Node (C);
                           end loop;
                        end if;

                        --  Mark P as being Added

                        Set_Added (Parent, E);
                     end if;

                     Src := Next_Node (Src);
                  end loop;
               end if;

               --  The destinations of F

               if not AAU.Is_Empty (Destinations (F)) then
                  Dst := First_Node (Destinations (F));

                  while Present (Dst) loop
                     Parent := Parent_Component (Item (Dst));

                     if AAU.Is_Process (Parent)
                       and then Parent /= E
                       and then not Is_Added (Parent, E)
                     then
                        --  Add the process to the deployment
                        --  enumerators of E.

                        S_Parent := Parent_Subcomponent (Parent);
855 856 857 858 859 860
                        Img_Len  :=
                          Get_Name_String (Map_Ada_Enumerator_Name (S_Parent))'
                            Length;
                        N :=
                          Make_Defining_Identifier
                            (Map_Ada_Enumerator_Name (S_Parent));
861 862 863 864 865 866 867 868
                        Insert_Node_In_List
                          (N,
                           Node_Enumerator_List,
                           Get_Node_Enum_Pos'Access);

                        --  Add a representation clause for the
                        --  enumerator corresponding to P.

869 870 871 872 873 874 875 876 877 878 879
                        N :=
                          Make_Element_Association
                            (Make_Defining_Identifier
                               (Map_Ada_Enumerator_Name (S_Parent)),
                             Make_Literal
                               (New_Integer_Value
                                  (Unsigned_Long_Long
                                     (Get_Node_Enum_Pos
                                        (Map_Ada_Enumerator_Name (S_Parent))),
                                   1,
                                   10)));
880 881 882 883 884 885 886 887 888
                        Insert_Node_In_List
                          (N,
                           Node_Enumerator_Pos_List,
                           Get_Node_Enum_Pos'Access);

                        if Max_Node_Image_Size < Img_Len then
                           Max_Node_Image_Size := Img_Len;
                        end if;

889 890 891 892 893 894 895
                        N :=
                          Make_Element_Association
                            (Make_Defining_Identifier
                               (Map_Ada_Enumerator_Name (S_Parent)),
                             Make_Literal
                               (New_String_Value
                                  (Map_Ada_Enumerator_Name (S_Parent))));
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931

                        Insert_Node_In_List
                          (N,
                           Node_Image_List,
                           Get_Node_Enum_Pos'Access);

                        --  Traverse all the subcomponensts of P

                        if not AAU.Is_Empty (Subcomponents (Parent)) then
                           C := First_Node (Subcomponents (Parent));

                           while Present (C) loop
                              Visit (Corresponding_Instance (C));

                              C := Next_Node (C);
                           end loop;
                        end if;

                        --  Mark P as being Added

                        Set_Added (Parent, E);
                     end if;

                     Dst := Next_Node (Dst);
                  end loop;
               end if;

               F := Next_Node (F);
            end loop;
         end if;

         --  Create the node enumeration type declaration. Note that
         --  the type creation is possible even the enumeration list
         --  is incomplete. We can do this in the first traversal
         --  since we are sure that the enumerator list is not empty.

932 933 934 935
         N :=
           Message_Comment
             ("For each node in the distributed" &
              " application add an enumerator");
936 937
         Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

938 939 940 941 942 943
         N :=
           Make_Full_Type_Declaration
             (Defining_Identifier =>
                Make_Defining_Identifier (TN (T_Node_Type)),
              Type_Definition =>
                Make_Enumeration_Type_Definition (Node_Enumerator_List));
944 945 946 947 948 949
         Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

         --  Create the enumeration representation clause so that all
         --  the enumerators in all the generated deployment packages
         --  have coherent position.

950 951 952 953
         N :=
           Message_Comment
             ("Representation clause to have consistent" &
              " positions for enumerators");
954 955
         Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

956 957 958 959 960 961
         N :=
           Make_Enumeration_Representation_Clause
             (Defining_Identifier =>
                Make_Defining_Identifier (TN (T_Node_Type)),
              Array_Aggregate =>
                Make_Array_Aggregate (Node_Enumerator_Pos_List));
962 963 964 965 966 967 968
         Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

         --  Fix the size of type Node_Type to Node_Type_Size bit
         --  because the message stream allocates one byte for it for
         --  now. This implies a maximum value of 256 nodes per
         --  application.

969 970 971 972 973
         N :=
           Message_Comment
             ("Size of Node_Type fixed to" &
              Integer'Image (Node_Type_Size) &
              " bits");
974 975
         Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

976 977 978 979 980 981 982
         N :=
           Make_Attribute_Definition_Clause
             (Defining_Identifier =>
                Make_Defining_Identifier (TN (T_Node_Type)),
              Attribute_Designator => A_Size,
              Expression           =>
                Make_Literal (New_Integer_Value (Node_Type_Size, 1, 10)));
983 984 985 986 987
         Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

         N := Max_Node_Image_Size_Declaration (E);
         Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

988
         N := Message_Comment ("Maximal Node_Image size for this" & " node");
989 990 991 992 993
         Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

         N := Message_Comment ("Node Image");
         Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
         N :=
           Make_Array_Type_Definition
             (Range_Constraints =>
                Make_List_Id
                  (Make_Range_Constraint
                     (No_Node,
                      No_Node,
                      Make_Attribute_Designator
                        (Make_Designator (TN (T_Node_Type)),
                         A_Range))),
              Component_Definition =>
                Make_Indexed_Component
                  (RE (RE_String),
                   Make_List_Id
                     (Make_Range_Constraint
                        (Make_Literal (New_Integer_Value (1, 0, 10)),
                         Make_Defining_Identifier
                           (PN (P_Max_Node_Image_Size))))));
1012 1013 1014 1015 1016 1017 1018 1019 1020

         --  Normalize Node image strings to fit in the Node_Image
         --  array constraint (Max_Node_Image_Size)

         declare
            Cur : Node_Id := ADN.First_Node (Node_Image_List);
         begin
            for J in 1 .. Length (Node_Image_List) loop
               declare
1021 1022
                  Str : constant String :=
                    Image (ADN.Value (ADN.Expression (Cur)));
1023 1024 1025 1026 1027
                  Res : String (1 .. Integer (Max_Node_Image_Size));
               begin
                  --  Get the string without the quotes
                  Res (1 .. Str'Last - 2) := Str (2 .. Str'Last - 1);
                  --  Fill the end with spaces
1028
                  for I in Str'Last - 1 .. Integer (Max_Node_Image_Size) loop
1029 1030
                     Res (I) := ' ';
                  end loop;
1031 1032 1033
                  ADN.Set_Value
                    (ADN.Expression (Cur),
                     New_String_Value (Get_String_Name (Res)));
1034 1035 1036 1037 1038
               end;
               Cur := ADN.Next_Node (Cur);
            end loop;
         end;

1039 1040 1041 1042 1043 1044 1045
         N :=
           Make_Object_Declaration
             (Defining_Identifier =>
                Make_Defining_Identifier (PN (P_Node_Image)),
              Constant_Present  => True,
              Object_Definition => N,
              Expression        => Make_Array_Aggregate (Node_Image_List));
1046 1047 1048 1049
         Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

         --  Declare the constant that represents the current node

1050 1051 1052 1053 1054 1055 1056
         N :=
           Make_Object_Declaration
             (Defining_Identifier => Make_Defining_Identifier (PN (P_My_Node)),
              Constant_Present    => True,
              Object_Definition   => Make_Designator (TN (T_Node_Type)),
              Expression          =>
                Make_Defining_Identifier (Map_Ada_Enumerator_Name (S)));
1057 1058 1059 1060 1061 1062 1063
         Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

         --  Create the thread enumeration type declaration. Note that
         --  the type creation is possible even the enumeration list
         --  is incomplete.

         if not Is_Empty (Thread_Enumerator_List) then
1064 1065 1066 1067 1068
            N :=
              Message_Comment
                ("For each thread in the distributed" &
                 " application nodes, add an" &
                 " enumerator");
1069 1070
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

1071 1072 1073 1074 1075 1076
            N :=
              Make_Full_Type_Declaration
                (Defining_Identifier =>
                   Make_Defining_Identifier (TN (T_Entity_Type)),
                 Type_Definition =>
                   Make_Enumeration_Type_Definition (Thread_Enumerator_List));
1077 1078 1079 1080 1081 1082
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

            --  Create the enumeration representation clause so that
            --  all the enumerators in all the generated deployment
            --  packages have coherent position.

1083 1084 1085 1086
            N :=
              Message_Comment
                ("Representation clause to have consistent" &
                 " positions for enumerators");
1087 1088
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

1089 1090 1091 1092 1093 1094
            N :=
              Make_Enumeration_Representation_Clause
                (Defining_Identifier =>
                   Make_Defining_Identifier (TN (T_Entity_Type)),
                 Array_Aggregate =>
                   Make_Array_Aggregate (Thread_Enumerator_Pos_List));
1095 1096 1097 1098 1099 1100 1101
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

            --  Fix the size of type Entity_Type to Entity_Type_Size
            --  bit because the message stream allocates one byte for
            --  it for now. This implies a maximum value of 256 nodes
            --  per application.

1102 1103 1104 1105 1106
            N :=
              Message_Comment
                ("Size of Entity_Type fixed to" &
                 Integer'Image (Entity_Type_Size) &
                 " bits");
1107 1108
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

1109 1110 1111 1112 1113 1114 1115
            N :=
              Make_Attribute_Definition_Clause
                (Defining_Identifier =>
                   Make_Defining_Identifier (TN (T_Entity_Type)),
                 Attribute_Designator => A_Size,
                 Expression           =>
                   Make_Literal (New_Integer_Value (Entity_Type_Size, 1, 10)));
1116 1117 1118 1119 1120 1121 1122 1123 1124
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
         end if;

         --  Declare the Entity Table when necessary

         if not Is_Empty (Entity_Table_List) then
            N := Message_Comment ("Entity Table");
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
            N :=
              Make_Array_Type_Definition
                (Range_Constraints =>
                   Make_List_Id
                     (Make_Range_Constraint
                        (No_Node,
                         No_Node,
                         Make_Attribute_Designator
                           (Make_Designator (TN (T_Entity_Type)),
                            A_Range))),
                 Component_Definition =>
                   Make_Defining_Identifier (TN (T_Node_Type)));

            N :=
              Make_Object_Declaration
                (Defining_Identifier =>
                   Make_Defining_Identifier (PN (P_Entity_Table)),
                 Constant_Present  => True,
                 Object_Definition => N,
                 Expression => Make_Array_Aggregate (Entity_Table_List));
1145 1146 1147 1148 1149
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

            N := Max_Entity_Image_Size_Declaration (E);
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

1150 1151
            N :=
              Message_Comment ("Maximal Entity_Image size for this" & " node");
1152 1153 1154 1155 1156
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

            N := Message_Comment ("Entity Image");
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
            N :=
              Make_Array_Type_Definition
                (Range_Constraints =>
                   Make_List_Id
                     (Make_Range_Constraint
                        (No_Node,
                         No_Node,
                         Make_Attribute_Designator
                           (Make_Designator (TN (T_Entity_Type)),
                            A_Range))),
                 Component_Definition =>
                   Make_Indexed_Component
                     (RE (RE_String),
                      Make_List_Id
                        (Make_Range_Constraint
                           (Make_Literal (New_Integer_Value (1, 0, 10)),
                            Make_Defining_Identifier
                              (PN (P_Max_Entity_Image_Size))))));
1175 1176 1177 1178 1179 1180 1181 1182 1183

            --  Normalize Entity image strings to fit in the Entity_Image
            --  array constraint (Max_Entity_Image_Size)

            declare
               Cur : Node_Id := ADN.First_Node (Entity_Image_List);
            begin
               for J in 1 .. Length (Entity_Image_List) loop
                  declare
1184 1185
                     Str : constant String :=
                       Image (ADN.Value (ADN.Expression (Cur)));
1186 1187 1188 1189 1190 1191 1192 1193 1194
                     Res : String (1 .. Integer (Max_Entity_Image_Size));
                  begin
                     --  Get the string without the quotes
                     Res (1 .. Str'Last - 2) := Str (2 .. Str'Last - 1);
                     --  Fill the end with spaces
                     for I in Str'Last - 1 .. Integer (Max_Entity_Image_Size)
                     loop
                        Res (I) := ' ';
                     end loop;
1195 1196 1197
                     ADN.Set_Value
                       (ADN.Expression (Cur),
                        New_String_Value (Get_String_Name (Res)));
1198 1199 1200 1201 1202
                  end;
                  Cur := ADN.Next_Node (Cur);
               end loop;
            end;

1203 1204 1205 1206 1207 1208 1209
            N :=
              Make_Object_Declaration
                (Defining_Identifier =>
                   Make_Defining_Identifier (PN (P_Entity_Image)),
                 Constant_Present  => True,
                 Object_Definition => N,
                 Expression => Make_Array_Aggregate (Entity_Image_List));
1210 1211 1212 1213 1214 1215 1216 1217
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
         end if;

         --  Create the port enumeration type declaration. Note that
         --  the type creation is possible even the enumeration list
         --  is incomplete.

         if not Is_Empty (Port_Enumerator_List) then
1218 1219 1220 1221 1222
            N :=
              Message_Comment
                ("For each thread port in the distributed" &
                 " application nodes, add an" &
                 " enumerator");
1223 1224
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

1225 1226 1227 1228 1229 1230
            N :=
              Make_Full_Type_Declaration
                (Defining_Identifier =>
                   Make_Defining_Identifier (TN (T_Port_Type)),
                 Type_Definition =>
                   Make_Enumeration_Type_Definition (Port_Enumerator_List));
1231 1232 1233 1234 1235 1236
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

            --  Create the enumeration representation clause so that
            --  all the enumerators in all the generated deployment
            --  packages have coherent position.

1237 1238 1239 1240
            N :=
              Message_Comment
                ("Representation clause to have consistent" &
                 " positions for enumerators");
1241 1242
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

1243 1244 1245 1246 1247 1248
            N :=
              Make_Enumeration_Representation_Clause
                (Defining_Identifier =>
                   Make_Defining_Identifier (TN (T_Port_Type)),
                 Array_Aggregate =>
                   Make_Array_Aggregate (Port_Enumerator_Pos_List));
1249 1250 1251 1252 1253
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

            --  Fix the size of type Port_Type to Port_Type_Size bits
            --  to be able to instanciate a Marshallers_G for it.

1254 1255 1256 1257 1258
            N :=
              Message_Comment
                ("Size of Port_Type fixed to" &
                 Integer'Image (Port_Type_Size) &
                 " bits");
1259 1260
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

1261 1262 1263 1264 1265 1266 1267
            N :=
              Make_Attribute_Definition_Clause
                (Defining_Identifier =>
                   Make_Defining_Identifier (TN (T_Port_Type)),
                 Attribute_Designator => A_Size,
                 Expression           =>
                   Make_Literal (New_Integer_Value (Port_Type_Size, 1, 10)));
1268 1269 1270 1271 1272 1273 1274 1275 1276
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
         end if;

         --  Declare the Port Table when necessary

         if not Is_Empty (Port_Table_List) then
            N := Message_Comment ("Port Table");
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
            N :=
              Make_Array_Type_Definition
                (Range_Constraints =>
                   Make_List_Id
                     (Make_Range_Constraint
                        (No_Node,
                         No_Node,
                         Make_Attribute_Designator
                           (Make_Designator (TN (T_Port_Type)),
                            A_Range))),
                 Component_Definition =>
                   Make_Defining_Identifier (TN (T_Entity_Type)));

            N :=
              Make_Object_Declaration
                (Defining_Identifier =>
                   Make_Defining_Identifier (PN (P_Port_Table)),
                 Constant_Present  => True,
                 Object_Definition => N,
                 Expression        => Make_Array_Aggregate (Port_Table_List));
1297 1298 1299 1300 1301
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

            N := Max_Port_Image_Size_Declaration (E);
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

1302 1303
            N :=
              Message_Comment ("Maximal Port_Image size for this" & " node");
1304 1305
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
            N :=
              Message_Comment
                ("A String subtype with Port_Image_Size" & " constraint");
            N :=
              Make_Full_Type_Declaration
                (Make_Defining_Identifier (PN (P_Port_Sized_String)),
                 Make_Indexed_Component
                   (RE (RE_String),
                    Make_List_Id
                      (Make_Range_Constraint
                         (Make_Literal (New_Integer_Value (1, 0, 10)),
                          RE (RE_Max_Port_Image_Size)))),
                 Is_Subtype => True);
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

            N := Message_Comment ("Port Image");
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

            --  Normalize Port image strings to fit in the Port_Image
            --  array constraint (Max_Port_Image_Size)

            declare
               Cur : Node_Id := ADN.First_Node (Port_Image_List);
            begin
               for J in 1 .. Length (Port_Image_List) loop
                  declare
1332 1333
                     Str : constant String :=
                       Image (ADN.Value (ADN.Expression (Cur)));
1334 1335 1336 1337 1338 1339 1340 1341 1342
                     Res : String (1 .. Integer (Max_Port_Image_Size));
                  begin
                     --  Get the string without the quotes
                     Res (1 .. Str'Last - 2) := Str (2 .. Str'Last - 1);
                     --  Fill the end with spaces
                     for I in Str'Last - 1 .. Integer (Max_Port_Image_Size)
                     loop
                        Res (I) := ' ';
                     end loop;
1343 1344 1345
                     ADN.Set_Value
                       (ADN.Expression (Cur),
                        New_String_Value (Get_String_Name (Res)));
1346 1347 1348 1349 1350
                  end;
                  Cur := ADN.Next_Node (Cur);
               end loop;
            end;

1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
            N :=
              Make_Array_Type_Definition
                (Range_Constraints =>
                   Make_List_Id
                     (Make_Range_Constraint
                        (No_Node,
                         No_Node,
                         Make_Attribute_Designator
                           (Make_Designator (TN (T_Port_Type)),
                            A_Range))),
                 Component_Definition =>
                   Make_Defining_Identifier (PN (P_Port_Sized_String)));

            N :=
              Make_Object_Declaration
                (Defining_Identifier =>
                   Make_Defining_Identifier (PN (P_Port_Image)),
                 Constant_Present  => True,
                 Object_Definition => N,
                 Expression        => Make_Array_Aggregate (Port_Image_List));
1371 1372 1373 1374 1375 1376 1377 1378
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
         end if;

         --  Generate the Max_Payload_Size constant declaration only
         --  if there is a real communication between threads or
         --  nodes.

         if Need_Deliver (E) or else Need_Send (E) then
1379 1380 1381
            N :=
              Message_Comment
                ("Maximal message payload size for this" & " node (in bits)");
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

            N := Max_Payload_Size_Declaration (E);
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

            --  Indicate to which type corresonds the maximal size

            --  Is not relevant if the system does not handle data
            --  or event data communication between threads or nodes

            if Max_Payload_Of /= No_Name then
1393 1394 1395
               N :=
                 Message_Comment
                   ("Biggest type: " & Get_Name_String (Max_Payload_Of));
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
            end if;
         end if;

         --  Unmark all the marked types

         Reset_Handlings;

         Pop_Entity; -- U
         Pop_Entity; -- P
      end Visit_Process_Instance;

      ---------------------------
      -- Visit_System_Instance --
      ---------------------------

      procedure Visit_System_Instance (E : Node_Id) is
         A : constant Node_Id := Map_Distributed_Application (E);
yoogx's avatar
yoogx committed
1414

1415 1416 1417
      begin
         Push_Entity (A);

yoogx's avatar
yoogx committed
1418
         Visit_Subcomponents_Of (E);
1419 1420 1421 1422 1423 1424 1425 1426 1427

         Pop_Entity; --  A
      end Visit_System_Instance;

      ---------------------------
      -- Visit_Thread_Instance --
      ---------------------------

      procedure Visit_Thread_Instance (E : Node_Id) is
1428 1429 1430 1431 1432 1433
         N          : Node_Id;
         P          : Node_Id;
         F          : Node_Id;
         S          : constant Node_Id            := Parent_Subcomponent (E);
         Img_Length : constant Unsigned_Long_Long :=
           Get_Name_String (Map_Ada_Enumerator_Name (S))'Length;
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
      begin
         --  Build the enumerator corresponding to the thread

         N := Make_Defining_Identifier (Map_Ada_Enumerator_Name (S));
         Insert_Node_In_List
           (N,
            Thread_Enumerator_List,
            Get_Thread_Enum_Pos'Access);
         Bind_AADL_To_Enumerator (Identifier (S), N);

         --  Build the representation clause for the enumerator

1446 1447 1448 1449 1450 1451 1452 1453 1454
         N :=
           Make_Element_Association
             (Make_Defining_Identifier (Map_Ada_Enumerator_Name (S)),
              Make_Literal
                (New_Integer_Value
                   (Unsigned_Long_Long
                      (Get_Thread_Enum_Pos (Map_Ada_Enumerator_Name (S))),
                    1,
                    10)));
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
         Insert_Node_In_List
           (N,
            Thread_Enumerator_Pos_List,
            Get_Thread_Enum_Pos'Access);

         --  For each thread, build the corresponding element
         --  association and append it to the entity list.

         --  Get the Process parent of the thread

         P := Parent_Component (S);
1466
         pragma Assert (AAU.Is_Process (P) or else AAU.Is_Abstract (P));
1467 1468

         if AAU.Is_Process (P) then
1469 1470 1471 1472 1473
            N :=
              Make_Element_Association
                (Make_Defining_Identifier (Map_Ada_Enumerator_Name (S)),
                 Make_Defining_Identifier
                   (Map_Ada_Enumerator_Name (Parent_Subcomponent (P))));
1474 1475

         elsif AAU.Is_Abstract (P) then
1476 1477 1478 1479
            N :=
              Make_Element_Association
                (Make_Defining_Identifier (Map_Ada_Enumerator_Name (S)),
                 Make_Defining_Identifier (PN (P_My_Node)));
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490

         end if;
         Insert_Node_In_List
           (N,
            Entity_Table_List,
            Get_Thread_Enum_Pos'Access);

         if Max_Entity_Image_Size < Img_Length then
            Max_Entity_Image_Size := Img_Length;
         end if;

1491 1492 1493 1494
         N :=
           Make_Element_Association
             (Make_Defining_Identifier (Map_Ada_Enumerator_Name (S)),
              Make_Literal (New_String_Value (Map_Ada_Enumerator_Name (S))));
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510

         Insert_Node_In_List
           (N,
            Entity_Image_List,
            Get_Thread_Enum_Pos'Access);

         --  For each one of the thread ports, create its
         --  corresponding enumerator, representation clause and
         --  association.

         if not AAU.Is_Empty (Features (E)) then
            F := First_Node (Features (E));

            while Present (F) loop
               if Kind (F) = K_Port_Spec_Instance then
                  declare
1511 1512
                     Enum_Name : constant Name_Id :=
                       Map_Ada_Full_Feature_Name (F, 'K');
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
                     Port_Img_Length : constant Unsigned_Long_Long :=
                       Get_Name_String (Enum_Name)'Length;
                  begin
                     --  Visit the feature to compute the data size

                     if Kind (F) = K_Port_Spec_Instance
                       and then AAN.Is_Data (F)
                     then
                        Visit (Corresponding_Instance (F));
                     end if;

                     --  Create the enumerator corresponding to the
                     --  port.

                     N := Make_Defining_Identifier (Enum_Name);
                     Insert_Node_In_List
                       (N,
                        Port_Enumerator_List,
                        Get_Port_Enum_Pos'Access);
                     Bind_AADL_To_Enumerator (Identifier (F), N);

                     --  Build the representation clause for the
                     --  enumerator.

1537 1538 1539 1540 1541 1542 1543 1544 1545
                     N :=
                       Make_Element_Association
                         (Make_Defining_Identifier (Enum_Name),
                          Make_Literal
                            (New_Integer_Value
                               (Unsigned_Long_Long
                                  (Get_Port_Enum_Pos (Enum_Name)),
                                1,
                                10)));
1546 1547 1548 1549 1550 1551 1552 1553 1554
                     Insert_Node_In_List
                       (N,
                        Port_Enumerator_Pos_List,
                        Get_Port_Enum_Pos'Access);

                     --  For each port, build the corresponding
                     --  element association and append it to the
                     --  association list.

1555 1556 1557 1558 1559
                     N :=
                       Make_Element_Association
                         (Make_Defining_Identifier (Enum_Name),
                          Make_Defining_Identifier
                            (Map_Ada_Enumerator_Name (S)));
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
                     Insert_Node_In_List
                       (N,
                        Port_Table_List,
                        Get_Port_Enum_Pos'Access);

                     --  For each port, build the corresponding
                     --  image association and append it to the
                     --  association list.

                     if Max_Port_Image_Size < Port_Img_Length then
                        Max_Port_Image_Size := Port_Img_Length;
                     end if;

1573 1574 1575 1576
                     N :=
                       Make_Element_Association
                         (Make_Defining_Identifier (Enum_Name),
                          Make_Literal (New_String_Value (Enum_Name)));
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
                     Insert_Node_In_List
                       (N,
                        Port_Image_List,
                        Get_Port_Enum_Pos'Access);
                  end;
               end if;

               F := Next_Node (F);
            end loop;

         end if;
      end Visit_Thread_Instance;

   end Package_Spec;

end Ocarina.Backends.PO_HI_Ada.Deployment;