ocarina-backends-po_hi_ada-transport.adb 50 KB
Newer Older
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--                  OCARINA.BACKENDS.PO_HI_ADA.TRANSPORT                    --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
--    Copyright (C) 2008-2009 Telecom ParisTech, 2010-2015 ESA & ISAE.      --
--                                                                          --
-- 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

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.Properties;
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.Transport is

48
   use Ocarina.Namet;
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
   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.Properties;
   use Ocarina.Backends.Ada_Tree.Nutils;
   use Ocarina.Backends.PO_HI_Ada.Mapping;
   use Ocarina.Backends.PO_HI_Ada.Runtime;

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

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

   package body Package_Spec is

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

      function Deliver_Spec (E : Node_Id) return Node_Id;
      --  Create a subprogram specification corresponding to the
      --  message delivery routine.

      function Send_Spec (E : Node_Id) return Node_Id;
      --  Create the subprogram specification corresponding to the
      --  transport layer Send routine.

      ------------------
      -- Deliver_Spec --
      ------------------

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

         Profile : constant List_Id := New_List (ADN.K_Parameter_Profile);
         N       : Node_Id;
      begin
         --  Entity

92 93 94 95 96
         N :=
           Make_Parameter_Specification
             (Defining_Identifier => Make_Defining_Identifier (PN (P_Entity)),
              Subtype_Mark        => RE (RE_Entity_Type),
              Parameter_Mode      => Mode_In);
97 98 99 100
         Append_Node_To_List (N, Profile);

         --  Message

101 102 103 104 105
         N :=
           Make_Parameter_Specification
             (Defining_Identifier => Make_Defining_Identifier (PN (P_Message)),
              Subtype_Mark        => RE (RE_Stream_Element_Array),
              Parameter_Mode      => Mode_In);
106 107
         Append_Node_To_List (N, Profile);

108 109 110 111 112
         N :=
           Make_Subprogram_Specification
             (Defining_Identifier => Make_Defining_Identifier (SN (S_Deliver)),
              Parameter_Profile   => Profile,
              Return_Type         => No_Node);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

         return N;
      end Deliver_Spec;

      ---------------
      -- Send_Spec --
      ---------------

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

         Profile : constant List_Id := New_List (ADN.K_Parameter_Profile);
         N       : Node_Id;
      begin
         --  From

129 130 131 132 133
         N :=
           Make_Parameter_Specification
             (Defining_Identifier => Make_Defining_Identifier (PN (P_From)),
              Subtype_Mark        => RE (RE_Entity_Type),
              Parameter_Mode      => Mode_In);
134 135 136 137
         Append_Node_To_List (N, Profile);

         --  Entity

138 139 140 141 142
         N :=
           Make_Parameter_Specification
             (Defining_Identifier => Make_Defining_Identifier (PN (P_Entity)),
              Subtype_Mark        => RE (RE_Entity_Type),
              Parameter_Mode      => Mode_In);
143 144 145 146
         Append_Node_To_List (N, Profile);

         --  Message

147 148 149 150 151
         N :=
           Make_Parameter_Specification
             (Defining_Identifier => Make_Defining_Identifier (PN (P_Message)),
              Subtype_Mark        => RE (RE_Message_Type),
              Parameter_Mode      => Mode_In);
152 153
         Append_Node_To_List (N, Profile);

154 155 156 157 158
         N :=
           Make_Subprogram_Specification
             (Defining_Identifier => Make_Defining_Identifier (SN (S_Send)),
              Parameter_Profile   => Profile,
              Return_Type         => RE (RE_Error_Kind));
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

         return N;
      end Send_Spec;

      -----------
      -- 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
195 196
         Category : constant Component_Category :=
           Get_Category_Of_Component (E);
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
      begin
         case Category is
            when CC_System =>
               Visit_System_Instance (E);

            when CC_Process =>
               Visit_Process_Instance (E);

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

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

      procedure Visit_Process_Instance (E : Node_Id) is
215 216 217
         U : constant Node_Id :=
           ADN.Distributed_Application_Unit
             (ADN.Deployment_Node (Backend_Node (Identifier (E))));
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
         P : constant Node_Id := ADN.Entity (U);
         N : Node_Id;
      begin
         Push_Entity (P);
         Push_Entity (U);
         Set_Transport_Spec;

         --  Generate a delivery spec

         N := Deliver_Spec (E);
         Bind_AADL_To_Deliver (Identifier (E), N);
         Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

         --  Generate the message sending spec if necessary

         N := Send_Spec (E);
         Bind_AADL_To_Send (Identifier (E), N);
         Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

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

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

      procedure Visit_System_Instance (E : Node_Id) is
         S : Node_Id;
      begin
         Push_Entity (Ada_Root);

         --  Visit all the subcomponents of the system

         if not AAU.Is_Empty (Subcomponents (E)) then
            S := First_Node (Subcomponents (E));
            while Present (S) loop
               --  Visit the component instance corresponding to the
               --  subcomponent S.

               Visit (Corresponding_Instance (S));
               S := Next_Node (S);
            end loop;
         end if;

         Pop_Entity; --  Ada_Root
      end Visit_System_Instance;

   end Package_Spec;

   ------------------
   -- Package_Body --
   ------------------

   package body Package_Body is

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

      function Internal_Deliver_Spec (E : Node_Id) return Node_Id;
      function Internal_Deliver_Body (E : Node_Id) return Node_Id;
      --  Create the internal delivery routine corresponding to the
      --  thread E.

      function Internal_Send_Spec (E : Node_Id) return Node_Id;
      function Internal_Send_Body (E : Node_Id) return Node_Id;
      --  Create the internal sending routine corresponding to the
      --  thread E.

      function Deliver_Body (E : Node_Id) return Node_Id;
      --  Create a subprogram implementation corresponding to the
      --  message delivery routine.

      function Send_Body (E : Node_Id) return Node_Id;
      --  Create a subprogram implementation corresponding to the
      --  message sending routine.

      ------------------
      -- Deliver_Body --
      ------------------

      function Deliver_Body (E : Node_Id) return Node_Id is
303 304
         Spec : constant Node_Id :=
           ADN.Deliver_Node (Backend_Node (Identifier (E)));
305 306 307 308 309 310 311 312 313 314 315
         Declarations : constant List_Id := New_List (ADN.K_Declaration_List);
         Statements   : constant List_Id := New_List (ADN.K_Statement_List);
         Alternatives : constant List_Id := New_List (ADN.K_List_Id);
         N            : Node_Id;
         T            : Node_Id;
      begin
         pragma Assert (AAU.Is_Process (E));

         if not Need_Deliver (E) then
            --  Generate a dummy Deliver

316 317 318 319 320 321
            N :=
              Make_Pragma_Statement
                (Pragma_Unreferenced,
                 Make_List_Id
                   (Make_Defining_Identifier (PN (P_Entity)),
                    Make_Defining_Identifier (PN (P_Message))));
322 323
            Append_Node_To_List (N, Declarations);

324
            N := Make_Null_Statement;
325 326 327 328
            Append_Node_To_List (N, Statements);
         else
            --  Declarative part

329
            N := Make_Used_Package (RU (RU_PolyORB_HI_Generated_Deployment));
330 331
            Append_Node_To_List (N, Declarations);

332 333 334 335
            N :=
              Make_Object_Declaration
                (Defining_Identifier => Make_Defining_Identifier (PN (P_Msg)),
                 Object_Definition   => RE (RE_Message_Type));
336 337
            Append_Node_To_List (N, Declarations);

338 339 340 341 342
            N :=
              Make_Object_Declaration
                (Defining_Identifier =>
                   Make_Defining_Identifier (PN (P_Value)),
                 Object_Definition => RE (RE_Unsigned_16));
343 344
            Append_Node_To_List (N, Declarations);

345 346 347 348
            N :=
              Make_Object_Declaration
                (Defining_Identifier => Make_Defining_Identifier (PN (P_Port)),
                 Object_Definition   => RE (RE_Port_Type_1));
349 350 351 352 353 354 355 356 357 358 359 360 361
            Append_Node_To_List (N, Declarations);

            --  Add a use clause for the
            --  Ada.Streams.Stream_Element_Offse type to have
            --  visibility on its operators.

            N := Make_Used_Type (RE (RE_Stream_Element_Offset));
            Append_Node_To_List (N, Declarations);

            --  Statements

            --  Get the message payload

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
            N :=
              Make_Expression
                (Make_Attribute_Designator
                   (Make_Designator (PN (P_Message)),
                    A_First),
                 Op_Plus,
                 RE (RE_Header_Size));

            N :=
              Make_Range_Constraint
                (N,
                 Make_Attribute_Designator
                   (Make_Designator (PN (P_Message)),
                    A_Last));

            N :=
              Make_Subprogram_Call
                (Make_Designator (PN (P_Message)),
                 Make_List_Id (N));

            N :=
              Make_Subprogram_Call
                (RE (RE_Write),
                 Make_List_Id (Make_Defining_Identifier (PN (P_Msg)), N));
386 387 388 389
            Append_Node_To_List (N, Statements);

            --  Unmarshall the destination port

390 391 392 393 394 395
            N :=
              Make_Subprogram_Call
                (RE (RE_Unmarshall_1),
                 Make_List_Id
                   (Make_Defining_Identifier (PN (P_Value)),
                    Make_Defining_Identifier (PN (P_Msg))));
396 397
            Append_Node_To_List (N, Statements);

398 399 400 401 402 403 404 405
            N :=
              Make_Subprogram_Call
                (RE (RE_Corresponding_Port),
                 Make_List_Id (Make_Defining_Identifier (PN (P_Value))));
            N :=
              Make_Assignment_Statement
                (Variable_Identifier => Make_Defining_Identifier (PN (P_Port)),
                 Expression          => N);
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429

            Append_Node_To_List (N, Statements);

            --  The case statement: for each thread of the current
            --  process, we generate a case statement alternative to
            --  call its specific delivery routine.

            T := First_Node (Subcomponents (E));

            while Present (T) loop
               if AAU.Is_Thread (Corresponding_Instance (T))
                 and then Has_In_Ports (Corresponding_Instance (T))
               then
                  --  Generate the spec of the internal delivery
                  --  routine of thread T. It is important to do this
                  --  before adding the global delivery body to the
                  --  package statemnets because it uses the internal
                  --  delivery routines.

                  N := Internal_Deliver_Spec (Corresponding_Instance (T));
                  Append_Node_To_List (N, ADN.Statements (Current_Package));

                  --  Call the internal delivery routine of the thread

430 431 432 433 434 435 436 437 438 439
                  N :=
                    Make_Subprogram_Call
                      (Make_Defining_Identifier
                         (Map_Deliver_Name (Corresponding_Instance (T))),
                       Make_List_Id
                         (Make_Defining_Identifier (PN (P_Port)),
                          Make_Subprogram_Call
                            (RE (RE_Sender),
                             Make_List_Id (Make_Designator (PN (P_Message)))),
                          Make_Defining_Identifier (PN (P_Msg))));
440 441 442

                  --  The case statement alternative

443 444 445 446 447 448 449
                  N :=
                    Make_Elsif_Statement
                      (Make_Expression
                         (Make_Defining_Identifier (PN (P_Entity)),
                          Op_Equal,
                          Extract_Enumerator (Corresponding_Instance (T))),
                       Make_List_Id (N));
450 451 452 453 454 455
                  Append_Node_To_List (N, Alternatives);
               end if;

               T := Next_Node (T);
            end loop;

456 457
            declare
               Elsif_Statements : constant List_Id := New_List (ADN.K_List_Id);
458

459
            begin
460 461 462
               ADN.Set_First_Node
                 (Elsif_Statements,
                  ADN.Next_Node (ADN.First_Node (Alternatives)));
463

464 465 466 467 468 469
               N :=
                 Make_If_Statement
                   (Condition => ADN.Condition (ADN.First_Node (Alternatives)),
                    Then_Statements =>
                      ADN.Then_Statements (ADN.First_Node (Alternatives)),
                    Elsif_Statements => Elsif_Statements);
470

471 472
               Append_Node_To_List (N, Statements);
            end;
473 474 475 476 477 478 479 480 481 482 483
         end if;

         N := Make_Subprogram_Implementation (Spec, Declarations, Statements);
         return N;
      end Deliver_Body;

      ---------------
      -- Send_Body --
      ---------------

      function Send_Body (E : Node_Id) return Node_Id is
484 485
         Spec : constant Node_Id :=
           ADN.Send_Node (Backend_Node (Identifier (E)));
486 487 488 489 490 491 492 493 494 495 496
         Declarations : constant List_Id := New_List (ADN.K_Declaration_List);
         Statements   : constant List_Id := New_List (ADN.K_Statement_List);
         Alternatives : constant List_Id := New_List (ADN.K_List_Id);
         N            : Node_Id;
         T            : Node_Id;
      begin
         pragma Assert (AAU.Is_Process (E));

         if not Need_Send (E) then
            --  Generate a dummy Send

497 498 499 500 501 502 503
            N :=
              Make_Pragma_Statement
                (Pragma_Unreferenced,
                 Make_List_Id
                   (Make_Defining_Identifier (PN (P_From)),
                    Make_Defining_Identifier (PN (P_Entity)),
                    Make_Defining_Identifier (PN (P_Message))));
504 505
            Append_Node_To_List (N, Declarations);

506 507 508 509 510
            N :=
              Make_Qualified_Expression
                (RE (RE_Error_Kind),
                 Make_Record_Aggregate
                   (Make_List_Id (RE (RE_Error_Transport))));
511 512 513 514 515 516
            N := Make_Return_Statement (N);
            Append_Node_To_List (N, Statements);

         else
            --  Declarative part

517
            N := Make_Used_Package (RU (RU_PolyORB_HI_Generated_Deployment));
518 519
            Append_Node_To_List (N, Declarations);

520 521 522 523 524 525 526 527 528 529 530 531
            N :=
              Make_Object_Declaration
                (Defining_Identifier => Make_Defining_Identifier (PN (P_Msg)),
                 Constant_Present    => True,
                 Object_Definition   => RE (RE_Stream_Element_Array),
                 Expression          =>
                   Make_Subprogram_Call
                     (RE (RE_Encapsulate),
                      Make_List_Id
                        (Make_Defining_Identifier (PN (P_Message)),
                         Make_Defining_Identifier (PN (P_From)),
                         Make_Defining_Identifier (PN (P_Entity)))));
532 533 534 535
            Append_Node_To_List (N, Declarations);

            --  Statements

536
            --  The if/elsif statement: for each thread of the current
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
            --  process, we generate a case statement alternative to
            --  call its specific sending routine.

            T := First_Node (Subcomponents (E));

            while Present (T) loop
               if AAU.Is_Thread (Corresponding_Instance (T))
                 and then Has_Out_Ports (Corresponding_Instance (T))
               then
                  --  Generate the spec of the internal sending
                  --  routine of thread T. It is important to do this
                  --  before adding the global sending body to the
                  --  package statemnets because it uses the internal
                  --  sending routines.

                  N := Internal_Send_Spec (Corresponding_Instance (T));
                  Append_Node_To_List (N, ADN.Statements (Current_Package));

                  --  Call the internal sending routine of the thread

557 558 559 560 561 562 563
                  N :=
                    Make_Subprogram_Call
                      (Make_Defining_Identifier
                         (Map_Send_Name (Corresponding_Instance (T))),
                       Make_List_Id
                         (Make_Defining_Identifier (PN (P_Entity)),
                          Make_Defining_Identifier (PN (P_Msg))));
564 565 566 567
                  N := Make_Return_Statement (N);

                  --  The case statement alternative

568 569 570 571 572 573 574
                  N :=
                    Make_Elsif_Statement
                      (Make_Expression
                         (Make_Defining_Identifier (PN (P_From)),
                          Op_Equal,
                          Extract_Enumerator (Corresponding_Instance (T))),
                       Make_List_Id (N));
575 576 577 578 579 580
                  Append_Node_To_List (N, Alternatives);
               end if;

               T := Next_Node (T);
            end loop;

581 582
            declare
               Elsif_Statements : constant List_Id := New_List (ADN.K_List_Id);
583
               Else_Statements  : constant List_Id := New_List (ADN.K_List_Id);
584

585
            begin
586
               if Present (ADN.First_Node (Alternatives)) then
587 588 589 590 591
                  N :=
                    Make_Qualified_Expression
                      (RE (RE_Error_Kind),
                       Make_Record_Aggregate
                         (Make_List_Id (RE (RE_Error_Transport))));
592 593 594
                  N := Make_Return_Statement (N);
                  Append_Node_To_List (N, Else_Statements);

595 596 597
                  ADN.Set_First_Node
                    (Elsif_Statements,
                     ADN.Next_Node (ADN.First_Node (Alternatives)));
598

599 600 601 602 603 604 605 606
                  N :=
                    Make_If_Statement
                      (Condition =>
                         ADN.Condition (ADN.First_Node (Alternatives)),
                       Then_Statements =>
                         ADN.Then_Statements (ADN.First_Node (Alternatives)),
                       Elsif_Statements => Elsif_Statements,
                       Else_Statements  => Else_Statements);
607 608 609

                  Append_Node_To_List (N, Statements);
               else
610 611 612 613 614
                  N :=
                    Make_Qualified_Expression
                      (RE (RE_Error_Kind),
                       Make_Record_Aggregate
                         (Make_List_Id (RE (RE_Error_Transport))));
615 616 617
                  N := Make_Return_Statement (N);
                  Append_Node_To_List (N, Statements);
               end if;
618
            end;
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
         end if;

         N := Make_Subprogram_Implementation (Spec, Declarations, Statements);
         return N;
      end Send_Body;

      ---------------------------
      -- Internal_Deliver_Spec --
      ---------------------------

      function Internal_Deliver_Spec (E : Node_Id) return Node_Id is
         Profile : constant List_Id := New_List (ADN.K_Parameter_Profile);
         N       : Node_Id;
      begin
         --  The Port parameter

635 636 637 638 639
         N :=
           Make_Parameter_Specification
             (Defining_Identifier => Make_Defining_Identifier (PN (P_Port)),
              Subtype_Mark        => RE (RE_Port_Type_1),
              Parameter_Mode      => Mode_In);
640 641 642 643
         Append_Node_To_List (N, Profile);

         --  The Sender parameter

644 645 646 647 648
         N :=
           Make_Parameter_Specification
             (Defining_Identifier => Make_Defining_Identifier (PN (P_From)),
              Subtype_Mark        => RE (RE_Entity_Type),
              Parameter_Mode      => Mode_In);
649 650 651 652
         Append_Node_To_List (N, Profile);

         --  The Msg parameter

653 654 655 656 657
         N :=
           Make_Parameter_Specification
             (Defining_Identifier => Make_Defining_Identifier (PN (P_Msg)),
              Subtype_Mark        => RE (RE_Message_Type),
              Parameter_Mode      => Mode_Inout);
658 659 660 661
         Append_Node_To_List (N, Profile);

         --  The subprogram spec

662 663 664 665 666 667
         N :=
           Make_Subprogram_Specification
             (Defining_Identifier =>
                Make_Defining_Identifier (Map_Deliver_Name (E)),
              Parameter_Profile => Profile,
              Return_Type       => No_Node);
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
         return N;
      end Internal_Deliver_Spec;

      ---------------------------
      -- Internal_Deliver_Body --
      ---------------------------

      function Internal_Deliver_Body (E : Node_Id) return Node_Id is
         Spec         : constant Node_Id := Internal_Deliver_Spec (E);
         Declarations : constant List_Id := New_List (ADN.K_Declaration_List);
         Statements   : constant List_Id := New_List (ADN.K_Statement_List);
         Alternatives : constant List_Id := New_List (ADN.K_List_Id);
         N            : Node_Id;
         F            : Node_Id;

         Time_Stamp_Declared : Boolean := False;
      begin
         if not AAU.Is_Empty (Features (E)) and then Has_In_Ports (E) then
            --  Add a 'use' clause to the Activity package

            Add_With_Package
              (RU (RU_PolyORB_HI_Generated_Activity, False),
               Used => True);

            --  Declare a local variable of type the thread interface

694
            N := Make_Used_Package (RU (RU_PolyORB_HI_Generated_Deployment));
695 696
            Append_Node_To_List (N, Declarations);

697 698 699 700 701 702
            N :=
              Make_Object_Declaration
                (Defining_Identifier =>
                   Make_Defining_Identifier (VN (V_Thread_Interface)),
                 Object_Definition =>
                   Make_Defining_Identifier (Map_Port_Interface_Name (E)));
703 704 705 706 707 708 709 710 711 712 713
            Append_Node_To_List (N, Declarations);

            --  For each port of the thread, create a switch case
            --  alternative to store the message to the proper
            --  destination.

            F := First_Node (Features (E));

            while Present (F) loop
               if Kind (F) = K_Port_Spec_Instance and then Is_In (F) then
                  declare
714
                     St : constant List_Id := New_List (ADN.K_Statement_List);
715 716 717 718 719 720 721 722 723 724 725
                     Call_Profile : constant List_Id :=
                       New_List (ADN.K_List_Id);
                  begin
                     --  In case of a data port, unmarshalls the time
                     --  stamp.

                     if not Is_Event (F) then
                        --  Declare the time stamp local variable if
                        --  it has not been done yet.

                        if not Time_Stamp_Declared then
726 727 728 729 730
                           N :=
                             Make_Object_Declaration
                               (Defining_Identifier =>
                                  Make_Defining_Identifier (VN (V_Time_Stamp)),
                                Object_Definition => RE (RE_Time));
731 732 733 734 735
                           Append_Node_To_List (N, Declarations);

                           Time_Stamp_Declared := True;
                        end if;

736 737 738 739 740 741
                        N :=
                          Make_Subprogram_Call
                            (RE (RE_Unmarshall_2),
                             Make_List_Id
                               (Make_Defining_Identifier (VN (V_Time_Stamp)),
                                Make_Defining_Identifier (PN (P_Msg))));
742 743 744 745 746
                        Append_Node_To_List (N, St);
                     end if;

                     --  Unmarshall the received message

747 748 749 750 751 752 753 754 755 756
                     N :=
                       Make_Subprogram_Call
                         (Extract_Designator
                            (ADN.Unmarshall_Node
                               (Backend_Node (Identifier (E)))),
                          Make_List_Id
                            (Map_Ada_Defining_Identifier (F),
                             Make_Defining_Identifier
                               (VN (V_Thread_Interface)),
                             Make_Defining_Identifier (PN (P_Msg))));
757 758 759 760
                     Append_Node_To_List (N, St);

                     --  Store the received message

761 762 763 764
                     N :=
                       Extract_Designator
                         (ADN.Store_Received_Message_Node
                            (Backend_Node (Identifier (E))));
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788

                     Append_Node_To_List
                       (Extract_Enumerator (E),
                        Call_Profile);

                     Append_Node_To_List
                       (Make_Defining_Identifier (VN (V_Thread_Interface)),
                        Call_Profile);

                     Append_Node_To_List
                       (Make_Defining_Identifier (PN (P_From)),
                        Call_Profile);

                     if not Is_Event (F) then
                        Append_Node_To_List
                          (Make_Defining_Identifier (VN (V_Time_Stamp)),
                           Call_Profile);
                     end if;

                     N := Make_Subprogram_Call (N, Call_Profile);
                     Append_Node_To_List (N, St);

                     --  Create the case statement alternative

789 790 791 792 793 794 795
                     N :=
                       Make_Elsif_Statement
                         (Make_Expression
                            (Make_Defining_Identifier (PN (P_Port)),
                             Op_Equal,
                             Extract_Enumerator (F)),
                          St);
796 797 798 799 800 801 802
                     Append_Node_To_List (N, Alternatives);
                  end;
               end if;

               F := Next_Node (F);
            end loop;

803 804
            declare
               Elsif_Statements : constant List_Id := New_List (ADN.K_List_Id);
805

806
            begin
807 808 809
               ADN.Set_First_Node
                 (Elsif_Statements,
                  ADN.Next_Node (ADN.First_Node (Alternatives)));
810

811 812 813 814 815 816
               N :=
                 Make_If_Statement
                   (Condition => ADN.Condition (ADN.First_Node (Alternatives)),
                    Then_Statements =>
                      ADN.Then_Statements (ADN.First_Node (Alternatives)),
                    Elsif_Statements => Elsif_Statements);
817

818 819
               Append_Node_To_List (N, Statements);
            end;
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
         end if;

         N := Make_Subprogram_Implementation (Spec, Declarations, Statements);
         return N;
      end Internal_Deliver_Body;

      ------------------------
      -- Internal_Send_Spec --
      ------------------------

      function Internal_Send_Spec (E : Node_Id) return Node_Id is
         Profile : constant List_Id := New_List (ADN.K_Parameter_Profile);
         N       : Node_Id;
      begin
         --  Entity

836 837 838 839 840
         N :=
           Make_Parameter_Specification
             (Defining_Identifier => Make_Defining_Identifier (PN (P_Entity)),
              Subtype_Mark        => RE (RE_Entity_Type),
              Parameter_Mode      => Mode_In);
841 842 843 844
         Append_Node_To_List (N, Profile);

         --  Message

845 846 847 848 849
         N :=
           Make_Parameter_Specification
             (Defining_Identifier => Make_Defining_Identifier (PN (P_Message)),
              Subtype_Mark        => RE (RE_Stream_Element_Array),
              Parameter_Mode      => Mode_In);
850 851 852 853
         Append_Node_To_List (N, Profile);

         --  The subprogram spec

854 855 856 857 858 859
         N :=
           Make_Subprogram_Specification
             (Defining_Identifier =>
                Make_Defining_Identifier (Map_Send_Name (E)),
              Parameter_Profile => Profile,
              Return_Type       => RE (RE_Error_Kind));
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
         return N;
      end Internal_Send_Spec;

      ------------------------
      -- Internal_Send_Body --
      ------------------------

      function Internal_Send_Body (E : Node_Id) return Node_Id is
         Handled_Thread : constant String := "%HandledFor%";

         function Is_Handled (T : Node_Id) return Boolean;
         procedure Set_Handled (T : Node_Id);
         --  Used to avoid duplicating case statement alternatives

         ----------------
         -- Is_Handled --
         ----------------

         function Is_Handled (T : Node_Id) return Boolean is
879 880 881
            I_Name : constant Name_Id :=
              Get_String_Name
                (Node_Id'Image (T) & Handled_Thread & Node_Id'Image (E));
882 883 884 885 886 887 888 889 890
         begin
            return Get_Name_Table_Byte (I_Name) = 1;
         end Is_Handled;

         -----------------
         -- Set_Handled --
         -----------------

         procedure Set_Handled (T : Node_Id) is
891 892 893
            I_Name : constant Name_Id :=
              Get_String_Name
                (Node_Id'Image (T) & Handled_Thread & Node_Id'Image (E));
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
         begin
            Set_Name_Table_Byte (I_Name, 1);
         end Set_Handled;

         Spec         : constant Node_Id := Internal_Send_Spec (E);
         Declarations : constant List_Id := New_List (ADN.K_Declaration_List);
         Statements   : constant List_Id := New_List (ADN.K_Statement_List);
         Alternatives : constant List_Id := New_List (ADN.K_List_Id);
         N            : Node_Id;
         F            : Node_Id;
      begin
         if not AAU.Is_Empty (Features (E)) and then Has_Out_Ports (E) then
            --  We loop through all the OUT ports of the thread, then
            --  through all their destinations and create a switch
            --  case alternative for each thread that may be a
            --  destination of E to send the message using the proper
            --  transport layer.

912 913 914 915 916 917
            N :=
              Make_Pragma_Statement
                (Pragma_Warnings,
                 Make_List_Id
                   (RE (RE_Off),
                    Make_Defining_Identifier (PN (P_Message))));
918
            Append_Node_To_List (N, Declarations);
919 920 921 922 923 924
            N :=
              Make_Pragma_Statement
                (Pragma_Warnings,
                 Make_List_Id
                   (RE (RE_Off),
                    Make_Defining_Identifier (PN (P_Entity))));
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
            Append_Node_To_List (N, Declarations);

            F := First_Node (Features (E));

            while Present (F) loop
               if Kind (F) = K_Port_Spec_Instance and then Is_Out (F) then
                  declare
                     Dest_List : constant List_Id := Get_Destination_Ports (F);
                     Dest_Th   : Node_Id;
                     Dest      : Node_Id;
                     B, K      : Node_Id;
                     Device    : Node_Id;
                  begin
                     if not AAU.Is_Empty (Dest_List) then
                        Dest := First_Node (Dest_List);

                        while Present (Dest) loop
                           --  Get the thread that contains Dest

                           Dest_Th := Parent_Component (Item (Dest));

                           --  If Dest_Th has not been handled in the
                           --  context of E yet, then create a case
                           --  statement alternative corresponding to
                           --  Dest_Th and mark it as handled.

                           if AAU.Is_Thread (Dest_Th)
                             and then not Is_Handled (Dest_Th)
                           then
                              --  Get the bus that connects the two ports

                              B := Extra_Item (Dest);

                              Device := Get_Device_Of_Process (B, E);

                              if No (B) then
                                 --  There is no bus involved in the
                                 --  connection, therefore it is a
                                 --  local communication: use the
                                 --  deliver routine.

966 967 968 969 970 971 972 973 974
                                 K :=
                                   Make_Subprogram_Call
                                     (Make_Defining_Identifier
                                        (SN (S_Deliver)),
                                      Make_List_Id
                                        (Make_Defining_Identifier
                                           (PN (P_Entity)),
                                         Make_Defining_Identifier
                                           (PN (P_Message))));
975 976 977 978 979

                                 --  Local delivery implies no
                                 --  potential error, we return
                                 --  Error_None.

980 981 982 983 984
                                 N :=
                                   Make_Qualified_Expression
                                     (RE (RE_Error_Kind),
                                      Make_Record_Aggregate
                                        (Make_List_Id (RE (RE_Error_None))));
985 986 987 988 989

                                 N := Make_Return_Statement (N);

                                 --  Create the case statement alternative

990 991 992 993 994 995 996 997
                                 N :=
                                   Make_Elsif_Statement
                                     (Make_Expression
                                        (Make_Defining_Identifier
                                           (PN (P_Entity)),
                                         Op_Equal,
                                         Extract_Enumerator (Dest_Th)),
                                      Make_List_Id (K, N));
998 999 1000 1001
                                 Append_Node_To_List (N, Alternatives);

                              elsif Device /= No_Node
                                and then AAU.Is_Device
1002
                                  (Corresponding_Instance (Device))
1003
                              then
1004 1005 1006 1007 1008 1009
                                 K :=
                                   Message_Comment
                                     ("User-provided transport mechanism, " &
                                      "device " &
                                      Get_Name_String
                                        (Name (Identifier (Device))));
1010 1011

                                 declare
1012 1013
                                    Profile : constant List_Id :=
                                      New_List (ADN.K_Parameter_Profile);
1014
                                    A : Node_Id;
1015 1016 1017
                                 begin
                                    --  Entity

1018 1019 1020 1021 1022 1023
                                    A :=
                                      Make_Parameter_Specification
                                        (Make_Defining_Identifier
                                           (PN (P_Node)),
                                         RE (RE_Node_Type),
                                         Mode_In);
1024 1025 1026 1027
                                    Append_Node_To_List (A, Profile);

                                    --  Message

1028 1029 1030 1031 1032 1033
                                    A :=
                                      Make_Parameter_Specification
                                        (Make_Defining_Identifier
                                           (PN (P_Message)),
                                         RE (RE_Stream_Element_Array),
                                         Mode_In);
1034 1035
                                    Append_Node_To_List (A, Profile);

1036 1037 1038 1039 1040 1041
                                    A :=
                                      Make_Parameter_Specification
                                        (Make_Defining_Identifier
                                           (PN (P_Size)),
                                         RE (RE_Stream_Element_Offset),
                                         Mode_In);
1042 1043 1044
                                    Append_Node_To_List (A, Profile);
                                 end;

1045 1046 1047 1048 1049
                                 N :=
                                   Make_Designator
                                     (Unit_Name
                                        (Get_Send_Function_Name
                                           (Corresponding_Instance (Device))));
1050 1051
                                 Add_With_Package (N);

1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
                                 N :=
                                   Make_Designator
                                     (Local_Name
                                        (Get_Send_Function_Name
                                           (Corresponding_Instance (Device))),
                                      Unit_Name
                                        (Get_Send_Function_Name
                                           (Corresponding_Instance (Device))));

                                 N :=
                                   Make_Subprogram_Call
                                     (ADN.Defining_Identifier (N),
1064
                                      Make_List_Id
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
                                        (Make_Indexed_Component
                                           (RE (RE_Entity_Table),
                                            Make_List_Id
                                              (Make_Defining_Identifier
                                                 (PN (P_Entity)))),
                                         Make_Defining_Identifier
                                           (PN (P_Message)),
                                         Make_Attribute_Designator
                                           (Prefix =>
                                              Make_Defining_Identifier
                                                (PN (P_Message)),
                                            Attribute => A_Length)));
1077 1078 1079 1080 1081

                                 N := Make_Return_Statement (N);

                                 --  Create the case statement alternative

1082 1083 1084 1085 1086 1087 1088 1089
                                 N :=
                                   Make_Elsif_Statement
                                     (Make_Expression
                                        (Make_Defining_Identifier
                                           (PN (P_Entity)),
                                         Op_Equal,
                                         Extract_Enumerator (Dest_Th)),
                                      Make_List_Id (K, N));
1090 1091 1092 1093 1094 1095 1096 1097 1098
                                 Append_Node_To_List (N, Alternatives);

                              else
                                 --  If the user did not specify any
                                 --  specific device drivers in the
                                 --  AADL model, use default transport
                                 --  mechanism provided by the
                                 --  runtime.

1099 1100
                                 pragma Assert
                                   (Get_Transport_API (B) /= Transport_None);
1101

1102 1103 1104
                                 K :=
                                   Message_Comment
                                     ("Default transport mechanism");
1105

1106 1107 1108
                                 N :=
                                   Make_Subprogram_Call
                                     (RE (RE_Send_3),
1109
                                      Make_List_Id
1110 1111 1112 1113 1114 1115 1116
                                        (Make_Indexed_Component
                                           (RE (RE_Entity_Table),
                                            Make_List_Id
                                              (Make_Defining_Identifier
                                                 (PN (P_Entity)))),
                                         Make_Defining_Identifier
                                           (PN (P_Message))));
1117 1118 1119 1120 1121

                                 N := Make_Return_Statement (N);

                                 --  Create the case statement alternative

1122 1123 1124 1125 1126 1127 1128 1129
                                 N :=
                                   Make_Elsif_Statement
                                     (Make_Expression
                                        (Make_Defining_Identifier
                                           (PN (P_Entity)),
                                         Op_Equal,
                                         Extract_Enumerator (Dest_Th)),
                                      Make_List_Id (K, N));
1130 1131 1132 1133 1134 1135
                                 Append_Node_To_List (N, Alternatives);
                              end if;

                              Set_Handled (Dest_Th);

                           elsif AAU.Is_Device (Dest_Th)
1136 1137
                             and then not Is_Handled (Dest_Th)
                           then
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159

                              N := Message_Comment ("Device");
                              Append_Node_To_List (N, Statements);

                              Set_Handled (Dest_Th);

                           elsif not Is_Handled (Dest_Th) then
                              raise Program_Error;
                           end if;

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

               F := Next_Node (F);
            end loop;

            --  Raise an error if other ports are targeted.

            if Length (Alternatives) /= 0 then
1160 1161
               N :=
                 Make_Used_Package (RU (RU_PolyORB_HI_Generated_Deployment));
1162 1163 1164
               Append_Node_To_List (N, Declarations);

               declare
1165 1166 1167 1168
                  Elsif_Statements : constant List_Id :=
                    New_List (ADN.K_List_Id);
                  Else_Statements : constant List_Id :=
                    New_List (ADN.K_List_Id);
1169 1170

               begin
1171 1172 1173 1174 1175
                  N :=
                    Make_Qualified_Expression
                      (RE (RE_Error_Kind),
                       Make_Record_Aggregate
                         (Make_List_Id (RE (RE_Error_Transport))));
1176 1177 1178
                  N := Make_Return_Statement (N);
                  Append_Node_To_List (N, Else_Statements);

1179 1180 1181
                  ADN.Set_First_Node
                    (Elsif_Statements,
                     ADN.Next_Node (ADN.First_Node (Alternatives)));
1182

1183 1184 1185 1186 1187 1188 1189 1190
                  N :=
                    Make_If_Statement
                      (Condition =>
                         ADN.Condition (ADN.First_Node (Alternatives)),
                       Then_Statements =>
                         ADN.Then_Statements (ADN.First_Node (Alternatives)),
                       Elsif_Statements => Elsif_Statements,
                       Else_Statements  => Else_Statements);
1191 1192 1193

                  Append_Node_To_List (N, Statements);
               end;
1194 1195

            else
1196 1197 1198 1199 1200
               N :=
                 Make_Qualified_Expression
                   (RE (RE_Error_Kind),
                    Make_Record_Aggregate
                      (Make_List_Id (RE (RE_Error_Transport))));
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
               N := Make_Return_Statement (N);
               Append_Node_To_List (N, Statements);
            end if;
         end if;

         N := Make_Subprogram_Implementation (Spec, Declarations, Statements);
         return N;
      end Internal_Send_Body;

      -----------
      -- 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
1242 1243
         Category : constant Component_Category :=
           Get_Category_Of_Component (E);
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
      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 others =>
               null;
         end case;
      end Visit_Component_Instance;

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

      procedure Visit_Process_Instance (E : Node_Id) is
1265 1266 1267
         U : constant Node_Id :=
           ADN.Distributed_Application_Unit
             (ADN.Deployment_Node (Backend_Node (Identifier (E))));
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
         P : constant Node_Id := ADN.Entity (U);
         S : Node_Id;
         N : Node_Id;
      begin
         Push_Entity (P);
         Push_Entity (U);
         Set_Transport_Body;

         --  Generate a delivery body

         N := Deliver_Body (E);
         Append_Node_To_List (N, ADN.Statements (Current_Package));

         --  Generate a sending body if necessary

         N := Send_Body (E);
         Append_Node_To_List (N, ADN.Statements (Current_Package));

         --  Visit all the subcomponents of the process

         if not AAU.Is_Empty (Subcomponents (E)) then
            S := First_Node (Subcomponents (E));
            while Present (S) loop
               --  Visit the component instance corresponding to the
               --  subcomponent S.

               Visit (Corresponding_Instance (S));
               S := Next_Node (S);
            end loop;
         end if;

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

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

      procedure Visit_System_Instance (E : Node_Id) is
         S : Node_Id;
      begin
         Push_Entity (Ada_Root);

         --  Visit all the subcomponents of the system

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

            while Present (S) loop
               --  Visit the component instance corresponding to the
               --  subcomponent S.

               Visit (Corresponding_Instance (S));
               S := Next_Node (S);
            end loop;
         end if;

         Pop_Entity; --  Ada_Root
      end Visit_System_Instance;

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

      procedure Visit_Thread_Instance (E : Node_Id) is
         N : Node_Id;
      begin
         if Has_In_Ports (E) then
            --  Generate the body of the internal delivery routine

            N := Internal_Deliver_Body (E);
            Append_Node_To_List (N, ADN.Statements (Current_Package));
         end if;

         if Has_Out_Ports (E) then
            --  Generate the body of the internal sending routine

            N := Internal_Send_Body (E);
            Append_Node_To_List (N, ADN.Statements (Current_Package));
         end if;
      end Visit_Thread_Instance;

   end Package_Body;

end Ocarina.Backends.PO_HI_Ada.Transport;