ocarina-backends-po_hi_ada-activity.adb 174 KB
Newer Older
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--  O C A R I N A . B A C K E N D S . P O _ H I _ A D A . A C T I V I T Y   --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
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 50 51
with Locations;

with Ocarina.ME_AADL;
with Ocarina.ME_AADL.AADL_Tree.Nodes;
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.Messages;
with Ocarina.Backends.Ada_Tree.Nutils;
with Ocarina.Backends.Ada_Tree.Nodes;
with Ocarina.Backends.Ada_Values;
with Ocarina.Backends.PO_HI_Ada.Mapping;
with Ocarina.Backends.PO_HI_Ada.Runtime;

package body Ocarina.Backends.PO_HI_Ada.Activity is

52
   use Ocarina.Namet;
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
   use Locations;
   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.Messages;
   use Ocarina.Backends.Ada_Tree.Nutils;
   use Ocarina.Backends.Ada_Values;
   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 ATN renames Ocarina.ME_AADL.AADL_Tree.Nodes;
   package AIN renames Ocarina.ME_AADL.AADL_Instances.Nodes;
   package AINU renames Ocarina.ME_AADL.AADL_Instances.Nutils;
   package ADN renames Ocarina.Backends.Ada_Tree.Nodes;

   function Send_Output_Spec (E : Node_Id) return Node_Id;
   function Put_Value_Spec (E : Node_Id) return Node_Id;
   function Receive_Input_Spec (E : Node_Id) return Node_Id;
   function Get_Value_Spec (E : Node_Id) return Node_Id;
   function Get_Sender_Spec (E : Node_Id) return Node_Id;
   function Get_Count_Spec (E : Node_Id) return Node_Id;
   function Get_Time_Stamp_Spec (E : Node_Id) return Node_Id;
   function Next_Value_Spec (E : Node_Id) return Node_Id;
   function Store_Received_Message_Spec (E : Node_Id) return Node_Id;
   function Wait_For_Incoming_Events_Spec (E : Node_Id) return Node_Id;
   --  Runtime routines provided for each AADL thread

   ----------------------
   -- Send_Output_Spec --
   ----------------------

   function Send_Output_Spec (E : Node_Id) return Node_Id is
      N : Node_Id;
   begin
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
      N :=
        Make_Subprogram_Specification
          (Defining_Identifier =>
             Make_Defining_Identifier (SN (S_Send_Output)),
           Parameter_Profile =>
             Make_List_Id
               (Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Entity)),
                   Subtype_Mark   => RE (RE_Entity_Type),
                   Parameter_Mode => Mode_In),
                Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Port)),
                   Subtype_Mark =>
                     Make_Defining_Identifier (Map_Port_Enumeration_Name (E)),
                   Parameter_Mode => Mode_In)),
           Return_Type => RE (RE_Error_Kind));
109 110 111 112 113 114 115 116 117 118 119

      return N;
   end Send_Output_Spec;

   --------------------
   -- Put_Value_Spec --
   --------------------

   function Put_Value_Spec (E : Node_Id) return Node_Id is
      N : Node_Id;
   begin
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
      N :=
        Make_Subprogram_Specification
          (Defining_Identifier => Make_Defining_Identifier (SN (S_Put_Value)),
           Parameter_Profile   =>
             Make_List_Id
               (Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Entity)),
                   Subtype_Mark   => RE (RE_Entity_Type),
                   Parameter_Mode => Mode_In),
                Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Thread_Interface)),
                   Subtype_Mark =>
                     Make_Defining_Identifier (Map_Port_Interface_Name (E)),
                   Parameter_Mode => Mode_In)),
           Return_Type => No_Node);
137 138 139 140 141 142 143 144 145 146 147

      return N;
   end Put_Value_Spec;

   ------------------------
   -- Receive_Input_Spec --
   ------------------------

   function Receive_Input_Spec (E : Node_Id) return Node_Id is
      N : Node_Id;
   begin
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
      N :=
        Make_Subprogram_Specification
          (Defining_Identifier =>
             Make_Defining_Identifier (SN (S_Receive_Input)),
           Parameter_Profile =>
             Make_List_Id
               (Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Entity)),
                   Subtype_Mark   => RE (RE_Entity_Type),
                   Parameter_Mode => Mode_In),
                Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Port)),
                   Subtype_Mark =>
                     Make_Defining_Identifier (Map_Port_Enumeration_Name (E)),
                   Parameter_Mode => Mode_In)),
           Return_Type => No_Node);
166 167 168 169 170 171 172 173 174 175 176

      return N;
   end Receive_Input_Spec;

   --------------------
   -- Get_Value_Spec --
   --------------------

   function Get_Value_Spec (E : Node_Id) return Node_Id is
      N : Node_Id;
   begin
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
      N :=
        Make_Subprogram_Specification
          (Defining_Identifier => Make_Defining_Identifier (SN (S_Get_Value)),
           Parameter_Profile   =>
             Make_List_Id
               (Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Entity)),
                   Subtype_Mark   => RE (RE_Entity_Type),
                   Parameter_Mode => Mode_In),
                Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Port)),
                   Subtype_Mark =>
                     Make_Defining_Identifier (Map_Port_Enumeration_Name (E)),
                   Parameter_Mode => Mode_In)),
           Return_Type =>
             Make_Defining_Identifier (Map_Port_Interface_Name (E)));
195 196 197 198 199 200 201 202 203 204 205

      return N;
   end Get_Value_Spec;

   ---------------------
   -- Get_Sender_Spec --
   ---------------------

   function Get_Sender_Spec (E : Node_Id) return Node_Id is
      N : Node_Id;
   begin
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
      N :=
        Make_Subprogram_Specification
          (Defining_Identifier => Make_Defining_Identifier (SN (S_Get_Sender)),
           Parameter_Profile   =>
             Make_List_Id
               (Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Entity)),
                   Subtype_Mark   => RE (RE_Entity_Type),
                   Parameter_Mode => Mode_In),
                Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Port)),
                   Subtype_Mark =>
                     Make_Defining_Identifier (Map_Port_Enumeration_Name (E)),
                   Parameter_Mode => Mode_In)),
           Return_Type => RE (RE_Entity_Type));
223 224 225 226 227 228 229 230 231 232 233

      return N;
   end Get_Sender_Spec;

   --------------------
   -- Get_Count_Spec --
   --------------------

   function Get_Count_Spec (E : Node_Id) return Node_Id is
      N : Node_Id;
   begin
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
      N :=
        Make_Subprogram_Specification
          (Defining_Identifier => Make_Defining_Identifier (SN (S_Get_Count)),
           Parameter_Profile   =>
             Make_List_Id
               (Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Entity)),
                   Subtype_Mark   => RE (RE_Entity_Type),
                   Parameter_Mode => Mode_In),
                Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Port)),
                   Subtype_Mark =>
                     Make_Defining_Identifier (Map_Port_Enumeration_Name (E)),
                   Parameter_Mode => Mode_In)),
           Return_Type => RE (RE_Integer));
251 252 253 254 255 256 257 258 259 260 261

      return N;
   end Get_Count_Spec;

   -------------------------
   -- Get_Time_Stamp_Spec --
   -------------------------

   function Get_Time_Stamp_Spec (E : Node_Id) return Node_Id is
      N : Node_Id;
   begin
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
      N :=
        Make_Subprogram_Specification
          (Defining_Identifier =>
             Make_Defining_Identifier (SN (S_Get_Time_Stamp)),
           Parameter_Profile =>
             Make_List_Id
               (Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Entity)),
                   Subtype_Mark   => RE (RE_Entity_Type),
                   Parameter_Mode => Mode_In),
                Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Port)),
                   Subtype_Mark =>
                     Make_Defining_Identifier (Map_Port_Enumeration_Name (E)),
                   Parameter_Mode => Mode_In)),
           Return_Type => RE (RE_Time));
280 281 282 283 284 285 286 287 288 289 290

      return N;
   end Get_Time_Stamp_Spec;

   ---------------------
   -- Next_Value_Spec --
   ---------------------

   function Next_Value_Spec (E : Node_Id) return Node_Id is
      N : Node_Id;
   begin
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
      N :=
        Make_Subprogram_Specification
          (Defining_Identifier => Make_Defining_Identifier (SN (S_Next_Value)),
           Parameter_Profile   =>
             Make_List_Id
               (Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Entity)),
                   Subtype_Mark   => RE (RE_Entity_Type),
                   Parameter_Mode => Mode_In),
                Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Port)),
                   Subtype_Mark =>
                     Make_Defining_Identifier (Map_Port_Enumeration_Name (E)),
                   Parameter_Mode => Mode_In)),
           Return_Type => No_Node);
308 309 310 311 312 313 314 315 316 317 318

      return N;
   end Next_Value_Spec;

   ---------------------------------
   -- Store_Received_Message_Spec --
   ---------------------------------

   function Store_Received_Message_Spec (E : Node_Id) return Node_Id is
      N : Node_Id;
   begin
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
      N :=
        Make_Subprogram_Specification
          (Defining_Identifier =>
             Make_Defining_Identifier (SN (S_Store_Received_Message)),
           Parameter_Profile =>
             Make_List_Id
               (Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Entity)),
                   Subtype_Mark   => RE (RE_Entity_Type),
                   Parameter_Mode => Mode_In),
                Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Thread_Interface)),
                   Subtype_Mark =>
                     Make_Defining_Identifier (Map_Port_Interface_Name (E)),
                   Parameter_Mode => Mode_In),
                Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_From)),
                   Subtype_Mark   => RE (RE_Entity_Type),
                   Parameter_Mode => Mode_In),
                Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Time_Stamp)),
                   Subtype_Mark   => RE (RE_Time),
                   Parameter_Mode => Mode_In,
                   Expression     => RE (RE_Clock))),
           Return_Type => No_Node);
348 349 350 351 352 353 354 355 356 357 358

      return N;
   end Store_Received_Message_Spec;

   -----------------------------------
   -- Wait_For_Incoming_Events_Spec --
   -----------------------------------

   function Wait_For_Incoming_Events_Spec (E : Node_Id) return Node_Id is
      N : Node_Id;
   begin
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
      N :=
        Make_Subprogram_Specification
          (Defining_Identifier =>
             Make_Defining_Identifier (SN (S_Wait_For_Incoming_Events)),
           Parameter_Profile =>
             Make_List_Id
               (Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Entity)),
                   Subtype_Mark   => RE (RE_Entity_Type),
                   Parameter_Mode => Mode_In),
                Make_Parameter_Specification
                  (Defining_Identifier =>
                     Make_Defining_Identifier (PN (P_Port)),
                   Subtype_Mark =>
                     Make_Defining_Identifier (Map_Port_Enumeration_Name (E)),
                   Parameter_Mode => Mode_Out)),
           Return_Type => No_Node);
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
      return N;
   end Wait_For_Incoming_Events_Spec;

   ------------------
   -- 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);
      procedure Visit_Thread_Instance (E : Node_Id);
      procedure Visit_Device_Instance (E : Node_Id);

      procedure Cyclic_Task_Instantiation_Formals
        (E      : Node_Id;
         P_List : List_Id);
      --  Appends the formal generic parameter association which are
      --  common between periodic and sporadic tasks.

      function Periodic_Task_Instantiation (E : Node_Id) return Node_Id;
      --  Build a package instantiation for a periodic task

      function Sporadic_Task_Instantiation (E : Node_Id) return Node_Id;
      --  Build a package instantiation for a sporadic task

      function Aperiodic_Task_Instantiation (E : Node_Id) return Node_Id;
      --  Build a package instantiation for an aperiodic task

      function Hybrid_Task_Instantiation (E : Node_Id) return Node_Id;
      --  Build a package instantiation for a hybrid task

      function Background_Task_Instantiation (E : Node_Id) return Node_Id;
      --  Build a package instantiation for a background task

414 415 416
      function Null_Task_Instantiation (E : Node_Id) return Node_Id;
      --  Build a package instantiation for a null task

yoogx's avatar
yoogx committed
417 418 419
      function ISR_Task_Instantiation (E : Node_Id) return Node_Id;
      --  Build a package instantiation for a background task

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
      function Task_Job_Spec (E : Node_Id) return Node_Id;
      --  Creates the parameterless subprogram specification that does
      --  the thread's job.

      procedure Runtime_Routine_Specs (E : Node_Id);
      --  Creates the specs of all the routines provided by the runtime
      --  to the user-code to manipulate thread interface.

      function Make_Mode_Updater_Spec (E : Node_Id) return Node_Id;
      --  Create the procedure which will update the current mode

      function Make_Modes_Enumeration (E : Node_Id) return Node_Id;
      --  Create the mode enumeration

      ---------------------------------------
      -- Cyclic_Task_Instantiation_Formals --
      ---------------------------------------

      procedure Cyclic_Task_Instantiation_Formals
        (E      : Node_Id;
         P_List : List_Id)
      is
         N : Node_Id;
         I : Unsigned_Long_Long;
         T : Time_Type;

      begin
         --  The entity name

449 450 451 452
         N :=
           Make_Parameter_Association
             (Selector_Name    => Make_Defining_Identifier (PN (P_Entity)),
              Actual_Parameter => Extract_Enumerator (E));
453 454 455 456 457
         Append_Node_To_List (N, P_List);

         if Get_Thread_Dispatch_Protocol (E) = Thread_Periodic
           or else Get_Thread_Dispatch_Protocol (E) = Thread_Sporadic
           or else Get_Thread_Dispatch_Protocol (E) = Thread_Hybrid
yoogx's avatar
yoogx committed
458
           or else Get_Thread_Dispatch_Protocol (E) = Thread_ISR
459 460 461
         then
            --  The task period of minimal interarrival time

462 463 464 465 466
            N :=
              Make_Parameter_Association
                (Selector_Name =>
                   Make_Defining_Identifier (PN (P_Task_Period)),
                 Actual_Parameter => Map_Ada_Time (Get_Thread_Period (E)));
467 468 469 470 471 472
            Append_Node_To_List (N, P_List);

            --  The task deadline

            N := Map_Ada_Time (Get_Thread_Deadline (E));

473 474 475 476 477
            N :=
              Make_Parameter_Association
                (Selector_Name =>
                   Make_Defining_Identifier (PN (P_Task_Deadline)),
                 Actual_Parameter => N);
478 479 480 481 482 483 484 485
            Append_Node_To_List (N, P_List);
         end if;

         if Get_Thread_Dispatch_Protocol (E) = Thread_Periodic then
            --  The dispatch offset
            T := Get_Dispatch_Offset (E);

            if T /= Null_Time then
486 487 488 489 490
               N :=
                 Make_Parameter_Association
                   (Selector_Name =>
                      Make_Defining_Identifier (PN (P_Dispatch_Offset)),
                    Actual_Parameter => Map_Ada_Time (T));
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
               Append_Node_To_List (N, P_List);
            end if;
         end if;

         --  The task priority, if the thread has no priority, we
         --  assign a default one.

         I := Get_Thread_Priority (E);

         if I = 0 then
            N := RE (RE_Default_Priority);
         else
            N := Map_Ada_Priority (I);
         end if;

506 507 508 509
         N :=
           Make_Parameter_Association
             (Selector_Name => Make_Defining_Identifier (PN (P_Task_Priority)),
              Actual_Parameter => N);
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
         Append_Node_To_List (N, P_List);

         --  The task stack size, if the thread has no stack size, we
         --  assign a default one.

         I := To_Bytes (Get_Thread_Stack_Size (E));

         if I = 0 then
            --  The default stack size is 100 Kb

            N := Make_Literal (New_Integer_Value (100_000, 1, 10));
         else
            N := Make_Literal (New_Integer_Value (I, 1, 10));
         end if;

525 526 527 528 529
         N :=
           Make_Parameter_Association
             (Selector_Name =>
                Make_Defining_Identifier (PN (P_Task_Stack_Size)),
              Actual_Parameter => N);
530 531 532 533
         Append_Node_To_List (N, P_List);

         --  The task job

534 535 536 537
         N :=
           Make_Parameter_Association
             (Selector_Name    => Make_Defining_Identifier (PN (P_Job)),
              Actual_Parameter => Map_Task_Job_Identifier (E));
538 539
         Append_Node_To_List (N, P_List);

540
         --  If an activate entrypoint has been specified for the
541 542 543
         --  thread, add an additional parameter association

         declare
544 545
            Activate_Entrypoint : constant Name_Id :=
              Get_Thread_Activate_Entrypoint (E);
546
         begin
547 548
            if Activate_Entrypoint /= No_Name then
               --  We cannot use direcly the activate entrypoint
549 550 551 552
               --  because the Activity spec must not depend on user
               --  package specs (to avoid introducing elaboration
               --  cycles). We use subprogram renaming as workaround.

553 554 555 556 557
               N :=
                 Make_Subprogram_Specification
                   (Defining_Identifier => Map_Task_Init_Identifier (E),
                    Parameter_Profile   => No_List,
                    Return_Type         => No_Node);
558 559
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

560 561 562 563 564
               N :=
                 Make_Parameter_Association
                   (Selector_Name =>
                      Make_Defining_Identifier (PN (P_Activate_Entrypoint)),
                    Actual_Parameter => Map_Task_Init_Identifier (E));
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
               Append_Node_To_List (N, P_List);
            end if;
         end;

         --  If a recover entrypoint has been specified for the
         --  thread, add an additional parameter association

         declare
            Rec_Entrypoint : constant Name_Id :=
              Get_Thread_Recover_Entrypoint (E);
         begin
            if Rec_Entrypoint /= No_Name then
               --  We cannot use direcly the recover entrypoint
               --  because the Activity spec must not depend on user
               --  package specs (to avoid introducing elaboration
               --  cycles). We use subprogram renaminng as workaround.

582 583 584 585 586
               N :=
                 Make_Subprogram_Specification
                   (Defining_Identifier => Map_Task_Recover_Identifier (E),
                    Parameter_Profile   => No_List,
                    Return_Type         => No_Node);
587 588
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

589 590 591 592 593
               N :=
                 Make_Parameter_Association
                   (Selector_Name =>
                      Make_Defining_Identifier (PN (P_Recover_Entrypoint)),
                    Actual_Parameter => Map_Task_Recover_Identifier (E));
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
               Append_Node_To_List (N, P_List);
            end if;
         end;
      end Cyclic_Task_Instantiation_Formals;

      ---------------------------------
      -- Periodic_Task_Instantiation --
      ---------------------------------

      function Periodic_Task_Instantiation (E : Node_Id) return Node_Id is
         N              : Node_Id;
         Parameter_List : constant List_Id :=
           New_List (ADN.K_Parameter_Profile);
      begin
         --  Build the common parameters

         Cyclic_Task_Instantiation_Formals (E, Parameter_List);

         --  Build the package instantiation

614 615 616
         N :=
           Make_Package_Instantiation
             (Defining_Identifier => Map_Task_Identifier (E),
617 618
              Generic_Package     =>
                RU (RU_PolyORB_HI_Periodic_Task, Elaborated => True),
yoogx's avatar
yoogx committed
619
              Parameter_List => Parameter_List);
620 621 622 623 624 625 626 627 628 629 630 631 632
         return N;
      end Periodic_Task_Instantiation;

      ---------------------------------
      -- Sporadic_Task_Instantiation --
      ---------------------------------

      function Sporadic_Task_Instantiation (E : Node_Id) return Node_Id is
         N              : Node_Id;
         Parameter_List : constant List_Id := New_List (ADN.K_List_Id);
      begin
         --  Port_Type

633 634 635 636 637
         N :=
           Make_Parameter_Association
             (Selector_Name    => Make_Defining_Identifier (TN (T_Port_Type)),
              Actual_Parameter =>
                Make_Defining_Identifier (Map_Port_Enumeration_Name (E)));
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
         Append_Node_To_List (N, Parameter_List);

         --  Raise an error if the thread does not have IN ports

         if not Has_In_Ports (E) then
            Display_Located_Error
              (Loc (E),
               "This sporadic thread does not have IN ports",
               Fatal => True);
         end if;

         --  Raise an error if the thread does not have 'in event'
         --  ports.

         if not Has_In_Event_Ports (E) then
            Display_Located_Error
              (Loc (E),
               "None of the IN ports of this sporadic thread is an event port",
               Fatal => True);
         end if;

         --  Append the common parameters

         Cyclic_Task_Instantiation_Formals (E, Parameter_List);

         --  The blocking routine

665 666 667 668 669 670
         N :=
           Make_Parameter_Association
             (Selector_Name =>
                Make_Defining_Identifier (SN (S_Wait_For_Incoming_Events)),
              Actual_Parameter =>
                Make_Defining_Identifier (SN (S_Wait_For_Incoming_Events)));
671 672 673 674
         Append_Node_To_List (N, Parameter_List);

         --  Build the package instantiation

675 676 677
         N :=
           Make_Package_Instantiation
             (Defining_Identifier => Map_Task_Identifier (E),
678 679
              Generic_Package     =>
                RU (RU_PolyORB_HI_Sporadic_Task, Elaborated => True),
yoogx's avatar
yoogx committed
680
              Parameter_List => Parameter_List);
681 682 683 684 685 686 687 688 689 690 691 692 693
         return N;
      end Sporadic_Task_Instantiation;

      ----------------------------------
      -- Aperiodic_Task_Instantiation --
      ----------------------------------

      function Aperiodic_Task_Instantiation (E : Node_Id) return Node_Id is
         N              : Node_Id;
         Parameter_List : constant List_Id := New_List (ADN.K_List_Id);
      begin
         --  Port_Type

694 695 696 697 698
         N :=
           Make_Parameter_Association
             (Selector_Name    => Make_Defining_Identifier (TN (T_Port_Type)),
              Actual_Parameter =>
                Make_Defining_Identifier (Map_Port_Enumeration_Name (E)));
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
         Append_Node_To_List (N, Parameter_List);

         --  Raise an error if the thread does not have IN ports

         if not Has_In_Ports (E) then
            Display_Located_Error
              (Loc (E),
               "This sporadic thread does not have IN ports",
               Fatal => True);
         end if;

         --  Raise an error if the thread has no 'in event' ports

         if not Has_In_Event_Ports (E) then
            Display_Located_Error
              (Loc (E),
               "None of the IN ports of this thread is an event port",
               Fatal => True);
         end if;

         --  Append the common parameters

         Cyclic_Task_Instantiation_Formals (E, Parameter_List);

         --  The blocking routine

725 726 727 728 729 730
         N :=
           Make_Parameter_Association
             (Selector_Name =>
                Make_Defining_Identifier (SN (S_Wait_For_Incoming_Events)),
              Actual_Parameter =>
                Make_Defining_Identifier (SN (S_Wait_For_Incoming_Events)));
731 732 733 734
         Append_Node_To_List (N, Parameter_List);

         --  Build the package instantiation

735 736 737
         N :=
           Make_Package_Instantiation
             (Defining_Identifier => Map_Task_Identifier (E),
738 739
              Generic_Package     =>
                RU (RU_PolyORB_HI_Aperiodic_Task, Elaborated => True),
yoogx's avatar
yoogx committed
740
              Parameter_List => Parameter_List);
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
         return N;
      end Aperiodic_Task_Instantiation;

      -----------------------------------
      -- Background_Task_Instantiation --
      -----------------------------------

      function Background_Task_Instantiation (E : Node_Id) return Node_Id is
         N              : Node_Id;
         Parameter_List : constant List_Id := New_List (ADN.K_List_Id);
      begin
         --  Append the common parameters

         Cyclic_Task_Instantiation_Formals (E, Parameter_List);

         --  Build the package instantiation

758 759 760
         N :=
           Make_Package_Instantiation
             (Defining_Identifier => Map_Task_Identifier (E),
761 762
              Generic_Package     =>
                RU (RU_PolyORB_HI_Background_Task, Elaborated => True),
yoogx's avatar
yoogx committed
763
              Parameter_List => Parameter_List);
764 765 766
         return N;
      end Background_Task_Instantiation;

767 768 769 770 771 772 773 774 775 776 777 778 779 780
      -----------------------------
      -- Null_Task_Instantiation --
      -----------------------------

      function Null_Task_Instantiation (E : Node_Id) return Node_Id is
         N              : Node_Id;
         Parameter_List : constant List_Id := New_List (ADN.K_List_Id);
      begin
         --  Append the common parameters

         Cyclic_Task_Instantiation_Formals (E, Parameter_List);

         --  Build the package instantiation

yoogx's avatar
yoogx committed
781 782 783 784 785
         N :=
           Make_Package_Instantiation
             (Defining_Identifier => Map_Task_Identifier (E),
              Generic_Package     => RU (RU_PolyORB_HI_Null_Task),
              Parameter_List      => Parameter_List);
786 787 788
         return N;
      end Null_Task_Instantiation;

yoogx's avatar
yoogx committed
789 790 791 792 793 794 795
      ----------------------------
      -- ISR_Task_Instantiation --
      ----------------------------

      function ISR_Task_Instantiation (E : Node_Id) return Node_Id is
         N              : Node_Id;
         Parameter_List : constant List_Id := New_List (ADN.K_List_Id);
796
         Configuration  : Name_Id;
yoogx's avatar
yoogx committed
797 798 799 800 801 802 803 804 805
      begin
         Configuration := Get_Configuration (E);
         if Configuration = No_Name then
            Display_Located_Error
              (Loc (E),
               "No interrupt configured",
               Fatal => True);
         end if;

806 807
         Add_With_Package (RU (RU_Ada_Interrupts_Names));
         Add_With_Package (RU (RU_Ada_Interrupts_Names));
yoogx's avatar
yoogx committed
808

809 810 811 812 813 814
         N :=
           Make_Parameter_Association
             (Selector_Name =>
                Make_Defining_Identifier (PN (P_Interrupt_Identifier)),
              Actual_Parameter =>
                Make_Defining_Identifier (Get_Configuration (E)));
yoogx's avatar
yoogx committed
815 816 817 818 819 820 821 822
         Append_Node_To_List (N, Parameter_List);

         --  Append the common parameters

         Cyclic_Task_Instantiation_Formals (E, Parameter_List);

         --  Build the package instantiation

823 824 825 826 827
         N :=
           Make_Package_Instantiation
             (Defining_Identifier => Map_Task_Identifier (E),
              Generic_Package     => RU (RU_PolyORB_HI_ISR_Task),
              Parameter_List      => Parameter_List);
yoogx's avatar
yoogx committed
828 829 830
         return N;
      end ISR_Task_Instantiation;

831 832 833 834 835 836 837 838 839 840
      -------------------------------
      -- Hybrid_Task_Instantiation --
      -------------------------------

      function Hybrid_Task_Instantiation (E : Node_Id) return Node_Id is
         N              : Node_Id;
         Parameter_List : constant List_Id := New_List (ADN.K_List_Id);
      begin
         --  Port_Type

841 842 843 844 845
         N :=
           Make_Parameter_Association
             (Selector_Name    => Make_Defining_Identifier (TN (T_Port_Type)),
              Actual_Parameter =>
                Make_Defining_Identifier (Map_Port_Enumeration_Name (E)));
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
         Append_Node_To_List (N, Parameter_List);

         --  Raise an error if the thread does not have IN ports

         if not Has_In_Ports (E) then
            Display_Located_Error
              (Loc (E),
               "This hybrid thread does not have IN ports",
               Fatal => True);
         end if;

         --  Raise an error if the thread does not have 'in event'
         --  ports.

         if not Has_In_Event_Ports (E) then
            Display_Located_Error
              (Loc (E),
               "None of the IN ports of this hybrid thread is an event port",
               Fatal => True);
         end if;

         --  Append the common parameters

         Cyclic_Task_Instantiation_Formals (E, Parameter_List);

         --  The blocking routine

873 874 875 876 877 878
         N :=
           Make_Parameter_Association
             (Selector_Name =>
                Make_Defining_Identifier (SN (S_Wait_For_Incoming_Events)),
              Actual_Parameter =>
                Make_Defining_Identifier (SN (S_Wait_For_Incoming_Events)));
879 880 881 882
         Append_Node_To_List (N, Parameter_List);

         --  Build the package instantiation

883 884 885 886 887
         N :=
           Make_Package_Instantiation
             (Defining_Identifier => Map_Task_Identifier (E),
              Generic_Package     => RU (RU_PolyORB_HI_Hybrid_Task),
              Parameter_List      => Parameter_List);
888 889 890 891 892 893 894 895 896
         return N;
      end Hybrid_Task_Instantiation;

      -------------------
      -- Task_Job_Spec --
      -------------------

      function Task_Job_Spec (E : Node_Id) return Node_Id is
         N          : Node_Id;
897
         Param_List : List_Id                                     := No_List;
898 899 900 901 902 903 904
         P          : constant Supported_Thread_Dispatch_Protocol :=
           Get_Thread_Dispatch_Protocol (E);
      begin
         if P = Thread_Sporadic
           or else P = Thread_Hybrid
           or else P = Thread_Aperiodic
         then
905 906 907 908 909
            N :=
              Make_Parameter_Specification
                (Defining_Identifier => Make_Defining_Identifier (PN (P_Port)),
                 Subtype_Mark        =>
                   Make_Defining_Identifier (Map_Port_Enumeration_Name (E)));
910 911 912 913

            Param_List := Make_List_Id (N);
         end if;

914 915 916 917 918
         N :=
           Make_Subprogram_Specification
             (Defining_Identifier => Map_Task_Job_Identifier (E),
              Parameter_Profile   => Param_List,
              Return_Type         => RE (RE_Error_Kind));
919 920 921 922 923 924 925 926 927 928
         return N;
      end Task_Job_Spec;

      ---------------------------
      -- Runtime_Routine_Specs --
      ---------------------------

      procedure Runtime_Routine_Specs (E : Node_Id) is
         N : Node_Id;
      begin
929 930 931
         if Has_Out_Ports (E) then
            --  The following functions are made visible iff the
            --  thread has *out* ports
932

933
            --  Send_Output
934

935 936
            N := Send_Output_Spec (E);
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
937

938
            --  Put_Value
939

940 941 942 943
            N := Put_Value_Spec (E);
            Bind_AADL_To_Put_Value (Identifier (E), N);
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
         end if;
944

945 946 947
         if Has_In_Ports (E) then
            --  The following functions are made visible iff the
            --  thread has *in* ports
948

949
            --  Receive_Input
950

951 952
            N := Receive_Input_Spec (E);
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
953

954
            --  Get_Value
955

956 957 958
            N := Get_Value_Spec (E);
            Bind_AADL_To_Get_Value (Identifier (E), N);
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
959

960
            --  Get_Sender
961

962 963
            N := Get_Sender_Spec (E);
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
964

965
            --  Get_Count
966

967 968 969
            N := Get_Count_Spec (E);
            Bind_AADL_To_Get_Count (Identifier (E), N);
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
970

971
            --  Get_Time_Stamp
972

973 974
            N := Get_Time_Stamp_Spec (E);
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
975

976
            --  Next_Value
977

978 979
            N := Next_Value_Spec (E);
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
980

981 982 983 984 985 986 987 988 989
            N := Store_Received_Message_Spec (E);
            Bind_AADL_To_Store_Received_Message (Identifier (E), N);
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

            --  Wait_For_Incoming_Events

            N := Wait_For_Incoming_Events_Spec (E);
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
         end if;
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
      end Runtime_Routine_Specs;

      -----------
      -- 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
1024 1025
         Category : constant Component_Category :=
           Get_Category_Of_Component (E);
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
      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
1047 1048 1049
         U : constant Node_Id :=
           ADN.Distributed_Application_Unit
             (ADN.Deployment_Node (Backend_Node (Identifier (E))));
yoogx's avatar
yoogx committed
1050
         P                   : constant Node_Id              := ADN.Entity (U);
1051 1052
         S                   : Node_Id;
         N                   : Node_Id;
1053
         Scheduling_Protocol : Supported_Scheduling_Protocol :=
1054 1055 1056
           Get_Scheduling_Protocol (Get_Bound_Processor (E));
         The_System : constant Node_Id :=
           Parent_Component (Parent_Subcomponent (E));
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
      begin
         Push_Entity (P);
         Push_Entity (U);
         Set_Activity_Spec;

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

         Start_Recording_Handlings;

         if Scheduling_Protocol = Unknown_Scheduler then
            Display_Located_Error
jhugues's avatar
jhugues committed
1069
              (Loc (Get_Bound_Processor (E)),
1070 1071 1072 1073
               "Undefined scheduling protocol, " &
               "will use FIFO_WITHIN_PRIORITIES",
               Fatal   => False,
               Warning => True);
1074

1075
            Scheduling_Protocol := POSIX_1003_HIGHEST_PRIORITY_FIRST_PROTOCOL;
yoogx's avatar
yoogx committed
1076 1077
         elsif Scheduling_Protocol /=
           POSIX_1003_HIGHEST_PRIORITY_FIRST_PROTOCOL
1078
           and then Scheduling_Protocol /= ROUND_ROBIN_PROTOCOL
1079 1080 1081
         then
            Display_Located_Error
              (Loc (Parent_Subcomponent (E)),
yoogx's avatar
yoogx committed
1082 1083 1084 1085
               "Incompatible scheduling protocol, " &
               "PolyORB-HI/Ada runtime requires " &
               "POSIX_1003_HIGHEST_PRIORITY_FIRST_PROTOCOL or" &
               " ROUND_ROBIN_PROTOCOL",
1086
               Fatal => True);
1087 1088 1089

            --  XXX In case of Round robin, we should also check that
            --  the scheduler is set to non-preemptive mode.
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
         end if;

         --  Visit all the subcomponents of the process

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

            while Present (S) loop
               --  If the process has a data subcomponent, then map a
               --  shared variable.

               if AINU.Is_Data (Corresponding_Instance (S)) then
1102 1103 1104 1105 1106 1107
                  N :=
                    Make_Object_Declaration
                      (Defining_Identifier => Map_Ada_Defining_Identifier (S),
                       Object_Definition   =>
                         Map_Ada_Data_Type_Designator
                           (Corresponding_Instance (S)));
1108 1109 1110 1111 1112
                  Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

                  --  Link the variable and the object

                  Bind_AADL_To_Object (Identifier (S), N);
1113

yoogx's avatar
yoogx committed
1114 1115
                  if Get_Concurrency_Protocol (Corresponding_Instance (S)) =
                    Priority_Ceiling
1116 1117 1118 1119 1120
                  then
                     --  XXX For now, we disable SPARK_Mode due to the
                     --  inability of SPARK GPL2015 to support
                     --  variable that denotes protected objects.

yoogx's avatar
yoogx committed
1121 1122 1123 1124
                     N :=
                       Make_Pragma_Statement
                         (Pragma_SPARK_Mode,
                          Make_List_Id (RE (RE_Off)));
1125 1126

                     Append_Node_To_List
yoogx's avatar
yoogx committed
1127 1128
                       (N,
                        ADN.Package_Headers (Current_Package));
1129 1130
                  end if;

1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
               else
                  --  Visit the component instance corresponding to the
                  --  subcomponent S.

                  Visit (Corresponding_Instance (S));
               end if;

               S := Next_Node (S);
            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
            S := First_Node (Subcomponents (The_System));
            while Present (S) loop
               if AAU.Is_Device (Corresponding_Instance (S))
1149 1150 1151
                 and then
                   Get_Bound_Processor (Corresponding_Instance (S)) =
                   Get_Bound_Processor (E)
1152
               then
1153
                  Visit_Device_Instance (Corresponding_Instance (S));
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
               end if;
               S := Next_Node (S);
            end loop;
         end if;

         --  Unmark all the marked types

         Reset_Handlings;

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

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

      procedure Visit_Device_Instance (E : Node_Id) is
1172 1173
         Implementation : constant Node_Id := Get_Implementation (E);
         S              : Node_Id;
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
      begin
         if Implementation /= No_Node then
            if not AAU.Is_Empty (AAN.Subcomponents (Implementation)) then
               S := First_Node (Subcomponents (Implementation));
               while Present (S) loop
                  Visit_Component_Instance (Corresponding_Instance (S));
                  S := Next_Node (S);
               end loop;
            end if;
         end if;
      end Visit_Device_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 AINU.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
         P : constant Supported_Thread_Dispatch_Protocol :=
           Get_Thread_Dispatch_Protocol (E);
         S : constant Node_Id := Parent_Subcomponent (E);
         N : Node_Id;
         O : Node_Id;
1221

yoogx's avatar
yoogx committed
1222 1223
         Scheduling_Protocol : Supported_Scheduling_Protocol :=
           Unknown_Scheduler;
1224
         Process_Node : Node_Id;
1225

1226
      begin
1227 1228
         --  Determine the scheduler that controls the current thread instance

yoogx's avatar
yoogx committed
1229
         Process_Node := Get_Container_Process (Parent_Subcomponent (E));
1230 1231

         if Present (Process_Node) then
yoogx's avatar
yoogx committed
1232 1233 1234
            Scheduling_Protocol :=
              Get_Scheduling_Protocol
                (Get_Bound_Processor (Corresponding_Instance (Process_Node)));
1235 1236
         end if;

1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
         if Has_Ports (E) then
            --  The data types and the interrogation routines
            --  generated from a thread are not instance specific. We
            --  generate them once per thread component. This allows
            --  us to support having multiple instances of the same
            --  thread in the model. Multiple thread instances of the
            --  same component share the same generated entities. This
            --  avoids having instance-specific code inside compute
            --  entrypoints.

1247 1248
            if No
                (Get_Handling
1249 1250 1251 1252
                   (Corresponding_Declaration (E),
                    By_Node,
                    H_Ada_Activity_Interr_Spec))
            then
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
               Set_Handling
                 (Corresponding_Declaration (E),
                  By_Node,
                  H_Ada_Activity_Interr_Spec,
                  E);

               N :=
                 Message_Comment
                   ("BEGIN: Entities used by all instances of component " &
                    Get_Name_String (Display_Name (Identifier (E))));
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

               --  Declare the enumeration type gathering all the
               --  thread ports.

               N := Map_Port_Enumeration (E);
               Bind_AADL_To_Port_Enumeration (Identifier (E), N);
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

               --  Declare the thread interface discriminated record type

               N := Map_Port_Interface (E);
               Bind_AADL_To_Port_Interface (Identifier (E), N);
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

               --  Declare the routines that allow user code to
               --  manipulate the thread.

               Runtime_Routine_Specs (E);

1283 1284 1285 1286
               N :=
                 Message_Comment
                   ("END: Entities used by all instances of component " &
                    Get_Name_String (Display_Name (Identifier (E))));
1287 1288 1289 1290 1291 1292
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
            else
               --  We bind the entities generated for the found
               --  instance to be able to reference them later.

               declare
1293 1294 1295 1296 1297 1298
                  Found : constant Node_Id :=
                    Get_Handling
                      (Corresponding_Declaration (E),
                       By_Node,
                       H_Ada_Activity_Interr_Spec);
                  BE : constant Node_Id := Backend_Node (Identifier (Found));
1299 1300
               begin
                  Bind_AADL_To_Port_Enumeration
1301 1302
                    (Identifier (E),
                     ADN.Port_Enumeration_Node (BE));
1303
                  Bind_AADL_To_Port_Interface
1304 1305
                    (Identifier (E),
                     ADN.Port_Interface_Node (BE));
1306
                  Bind_AADL_To_Put_Value
1307 1308
                    (Identifier (E),
                     ADN.Put_Value_Node (BE));
1309
                  Bind_AADL_To_Get_Value
1310 1311
                    (Identifier (E),
                     ADN.Get_Value_Node (BE));
1312
                  Bind_AADL_To_Get_Count
1313 1314
                    (Identifier (E),
                     ADN.Get_Count_Node (BE));
1315
                  Bind_AADL_To_Store_Received_Message
1316 1317
                    (Identifier (E),
                     ADN.Store_Received_Message_Node (BE));
1318 1319 1320 1321 1322 1323
               end;
            end if;
         end if;

         case P is
            when Thread_Periodic =>
yoogx's avatar
yoogx committed
1324 1325 1326 1327
               N :=
                 Message_Comment
                   ("Periodic task : " &
                    Get_Name_String (Display_Name (Identifier (S))));
1328 1329 1330
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

            when Thread_Sporadic =>
yoogx's avatar
yoogx committed
1331 1332 1333 1334
               N :=
                 Message_Comment
                   ("Sporadic task : " &
                    Get_Name_String (Display_Name (Identifier (S))));
1335 1336 1337
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

            when Thread_Hybrid =>
yoogx's avatar
yoogx committed
1338 1339 1340 1341
               N :=
                 Message_Comment
                   ("Hybrid task : " &
                    Get_Name_String (Display_Name (Identifier (S))));
1342 1343 1344
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

            when Thread_Aperiodic =>
yoogx's avatar
yoogx committed
1345 1346 1347 1348
               N :=
                 Message_Comment
                   ("Aperiodic task : " &
                    Get_Name_String (Display_Name (Identifier (S))));
1349 1350 1351
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

            when Thread_Background =>
yoogx's avatar
yoogx committed
1352 1353 1354 1355
               N :=
                 Message_Comment
                   ("Background task : " &
                    Get_Name_String (Display_Name (Identifier (S))));
1356 1357
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

yoogx's avatar
yoogx committed
1358
            when Thread_ISR =>
yoogx's avatar
yoogx committed
1359 1360 1361 1362
               N :=
                 Message_Comment
                   ("ISR task : " &
                    Get_Name_String (Display_Name (Identifier (S))));
yoogx's avatar
yoogx committed
1363 1364
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
            when others =>
               Display_Located_Error
                 (AIN.Loc (E),
                  "Unsupported dispatch protocol",
                  Fatal => True);
         end case;

         --  Create the spec of the parameterless subprogram that
         --  executes the thread job.

         N := Task_Job_Spec (E);
         Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
         Bind_AADL_To_Job (Identifier (S), N);

1379
         --  For each AADL thread, we instantiate a task.
1380

1381 1382 1383 1384 1385 1386
         if Scheduling_Protocol = ROUND_ROBIN_PROTOCOL then
            N := Null_Task_Instantiation (E);
            Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

         else
            --  Default case : FIFO_WITHIN_PRIORITIES
yoogx's avatar
yoogx committed
1387
            case P is
1388 1389
               when Thread_Periodic =>
                  --  Instantiate the periodic task
1390

1391 1392
                  N := Periodic_Task_Instantiation (E);
                  Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
1393

1394 1395
               when Thread_Sporadic =>
                  --  Instantiate the sporadic task
1396

1397 1398
                  N := Sporadic_Task_Instantiation (E);
                  Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
1399

1400 1401
               when Thread_Hybrid =>
                  --  Instantiate the hybrid task
1402

1403 1404
                  N := Hybrid_Task_Instantiation (E);
                  Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
1405

1406 1407
               when Thread_Aperiodic =>
                  --  Instantiate the aperiodic task
1408

1409 1410
                  N := Aperiodic_Task_Instantiation (E);
                  Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
1411

yoogx's avatar
yoogx committed
1412
               when Thread_Background =>
1413
                  --  Instantiate the background task
1414

1415 1416
                  N := Background_Task_Instantiation (E);
                  Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
1417

1418 1419
               when Thread_ISR =>
                  --  Instantiate the ISR task
yoogx's avatar
yoogx committed
1420

1421 1422
                  N := ISR_Task_Instantiation (E);
                  Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
yoogx's avatar
yoogx committed
1423

1424 1425 1426 1427
               when others =>
                  raise Program_Error;
            end case;
         end if;
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449

         if Has_Modes (E) then
            --  If the thread has operational modes, then generate the
            --  enumeration type corresponding to the thread mode list
            --  and the procedure allowing to update the current mode.

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

            if Is_Fusioned (E) then
               N := Make_Mode_Updater_Spec (E);
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
            end if;
         end if;

         --  Visit thread local objects

         if not AINU.Is_Empty (Subcomponents (E)) then
            O := First_Node (Subcomponents (E));

            while Present (O) loop
               if AINU.Is_Data (Corresponding_Instance (O)) then
yoogx's avatar
yoogx committed
1450 1451 1452 1453 1454 1455 1456
                  N :=
                    Make_Object_Declaration
                      (Defining_Identifier => Map_Ada_Defining_Identifier (O),
                       Object_Definition   =>
                         Map_Ada_Data_Type_Designator
                           (Corresponding_Instance (O)));
                  Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485

                  --  Link the variable and the object

                  Bind_AADL_To_Object (Identifier (O), N);
               end if;

               O := Next_Node (O);
            end loop;
         end if;
      end Visit_Thread_Instance;

      ----------------------------
      -- Make_Modes_Enumeration --
      ----------------------------

      function Make_Modes_Enumeration (E : Node_Id) return Node_Id is
         Enum_List : constant List_Id := New_List (ADN.K_Enumeration_Literals);
         M         : Node_Id;
         N         : Node_Id;
      begin
         M := First_Node (Modes (E));

         while Present (M) loop
            N := Map_Ada_Defining_Identifier (M);
            Append_Node_To_List (N, Enum_List);

            M := Next_Node (M);
         end loop;

1486 1487 1488 1489 1490
         N :=
           Make_Full_Type_Declaration
             (Defining_Identifier =>
                Make_Defining_Identifier (Map_Modes_Enumeration_Name (E)),
              Type_Definition => Make_Enumeration_Type_Definition (Enum_List));
1491 1492 1493 1494 1495 1496 1497 1498

         return N;
      end Make_Modes_Enumeration;

      ----------------------------
      -- Make_Mode_Updater_Spec --
      ----------------------------

1499
      function Make_Mode_Updater_Spec (E : Node_Id) return Node_Id is
1500 1501
         N : Node_Id;
      begin
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
         N :=
           Make_Subprogram_Specification
             (Defining_Identifier =>
                Make_Defining_Identifier (SN (S_Change_Mode)),
              Parameter_Profile =>
                Make_List_Id
                  (Make_Parameter_Specification
                     (Defining_Identifier =>
                        Make_Defining_Identifier (PN (P_Mode)),
                      Subtype_Mark =>
                        Make_Defining_Identifier
                          (Map_Modes_Enumeration_Name (E)),
                      Parameter_Mode => Mode_In)),
              Return_Type => No_Node);
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
         Set_Backend_Node (Identifier (First_Node (Modes (E))), N);

         return N;
      end Make_Mode_Updater_Spec;

   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);
      procedure Visit_Device_Instance (E : Node_Id);

      function Task_Job_Body (E : Node_Id) return Node_Id;
      --  Creates the parameterless subprogram body that does the
      --  thread's job.

      procedure Runtime_Routine_Bodies (E : Node_Id);
      --  Creates the implementations of all the routines provided by
      --  the runtime to the user-code to manipulate thread interface.

      function Make_Current_Mode_Declaration (E : Node_Id) return Node_Id;
      --  Create, if necessary, the current mode variable declaration for
      --  thread E.

      function Make_Mode_Updater_body (E : Node_Id) return Node_Id;
      --  Create the procedure which will update the current mode

1551 1552
      Has_Hybrid_Threads       : Boolean            := False;
      Hybrid_Thread_Elements   : List_Id            := No_List;
1553 1554
      Last_Hybrid_Thread_Index : Unsigned_Long_Long := 0;

1555
      Current_Mode_Identifier : Node_Id;
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578

      --  The runtime routines are generated per thread component and
      --  not per thread instance. For each thread instance, we must
      --  complete the case alternative specific to it in each one of
      --  the routines. To perform this, we attache to each thread
      --  component a set of List_Id's which represent the case
      --  statement of the corresponding routines. The entities below
      --  allow to Get/Set these list for each thread component.

      type Runtime_Routine is
        (RR_Send_Output,
         RR_Put_Value,
         RR_Receive_Input,
         RR_Get_Value,
         RR_Get_Sender,
         RR_Get_Count,
         RR_Get_Time_Stamp,
         RR_Next_Value,
         RR_Store_Received_Message,
         RR_Wait_For_Incoming_Events);

      function Get_List_Internal_Name
        (Thread : Node_Id;
1579
         RR     : Runtime_Routine) return Name_Id;
1580 1581 1582 1583
      --  Code factorization between the two subprograms below

      function Get_List
        (Thread : Node_Id;
1584
         RR     : Runtime_Routine) return List_Id;
1585 1586 1587
      --  Return the List_Id corresponding to the runtime routine 'RR'
      --  and associated to 'Thread'.