ocarina-backends-po_hi_c-activity.adb 80.1 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 _ C . A C T I V I T Y     --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--    Copyright (C) 2008-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

with Ocarina.ME_AADL;
with Ocarina.ME_AADL.AADL_Instances.Nodes;
with Ocarina.ME_AADL.AADL_Instances.Nutils;
with Ocarina.Backends.Utils;
with Ocarina.ME_AADL.AADL_Instances.Entities;
with Ocarina.Backends.Properties;
with Ocarina.Backends.C_Tree.Nutils;
with Ocarina.Backends.C_Tree.Nodes;
with Ocarina.Backends.C_Common.Mapping;
with Ocarina.Backends.PO_HI_C.Runtime;
with Ocarina.Backends.C_Values;
with Ocarina.Backends.Messages;

package body Ocarina.Backends.PO_HI_C.Activity is

49
   use Ocarina.Namet;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
   use Ocarina.ME_AADL;
   use Ocarina.ME_AADL.AADL_Instances.Nodes;
   use Ocarina.Backends.Utils;
   use Ocarina.ME_AADL.AADL_Instances.Entities;
   use Ocarina.Backends.Properties;
   use Ocarina.Backends.C_Tree.Nutils;
   use Ocarina.Backends.C_Common.Mapping;
   use Ocarina.Backends.PO_HI_C.Runtime;
   use Ocarina.Backends.Messages;

   package AAU renames Ocarina.ME_AADL.AADL_Instances.Nutils;
   package AAN renames Ocarina.ME_AADL.AADL_Instances.Nodes;
   package CTN renames Ocarina.Backends.C_Tree.Nodes;
   package CTU renames Ocarina.Backends.C_Tree.Nutils;
   package CV renames Ocarina.Backends.C_Values;

66
   Current_Device : Node_Id := No_Node;
67

68 69 70 71 72 73 74 75 76 77
   ------------
   -- Header --
   ------------

   package body Header_File 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);
78
      procedure Visit_Device_Instance (E : Node_Id);
79 80 81 82
      procedure Visit_Thread_Instance (E : Node_Id);
      function Task_Job_Spec (E : Node_Id) return Node_Id;
      function Task_Deliver_Spec (E : Node_Id) return Node_Id;

83
      Have_Main_Deliver : Boolean := False;
84 85 86 87 88 89 90 91 92

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

      function Task_Job_Spec (E : Node_Id) return Node_Id is
         N : Node_Id;
         S : constant Node_Id := Parent_Subcomponent (E);
      begin
93 94 95 96 97 98
         N :=
           Make_Function_Specification
             (Defining_Identifier =>
                Map_Task_Job_Identifier (S, Current_Device),
              Parameters  => No_List,
              Return_Type => CTU.Make_Pointer_Type (New_Node (CTN.K_Void)));
99 100 101 102 103 104 105 106 107 108 109 110
         return N;
      end Task_Job_Spec;

      -----------------------
      -- Task_Deliver_Spec --
      -----------------------

      function Task_Deliver_Spec (E : Node_Id) return Node_Id is
         N          : Node_Id;
         S          : constant Node_Id := Parent_Subcomponent (E);
         Parameters : constant List_Id := New_List (CTN.K_Parameter_List);
      begin
111 112 113 114
         N :=
           Make_Parameter_Specification
             (Defining_Identifier => Make_Defining_Identifier (PN (P_Request)),
              Parameter_Type      => Make_Pointer_Type (RE (RE_Request_T)));
115 116
         Append_Node_To_List (N, Parameters);

117 118 119 120 121
         N :=
           Make_Function_Specification
             (Defining_Identifier => Map_Task_Deliver_Identifier (S),
              Parameters          => Parameters,
              Return_Type         => New_Node (CTN.K_Void));
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

         Have_Main_Deliver := True;

         return N;
      end Task_Deliver_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
160 161
         Category : constant Component_Category :=
           Get_Category_Of_Component (E);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
      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
183 184 185
         U : constant Node_Id :=
           CTN.Distributed_Application_Unit
             (CTN.Naming_Node (Backend_Node (Identifier (E))));
186 187 188 189
         P          : constant Node_Id := CTN.Entity (U);
         Parameters : constant List_Id := New_List (CTN.K_Parameter_List);
         S          : Node_Id;
         N          : Node_Id;
190 191
         The_System : constant Node_Id :=
           Parent_Component (Parent_Subcomponent (E));
192 193 194 195 196
      begin
         Push_Entity (P);
         Push_Entity (U);
         Set_Activity_Header (U);

197
         Have_Main_Deliver := False;
198 199 200 201 202 203 204 205 206 207 208 209

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

               Visit (Corresponding_Instance (S));

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

         if Have_Main_Deliver then
210 211 212 213 214
            N :=
              Make_Parameter_Specification
                (Defining_Identifier =>
                   Make_Defining_Identifier (PN (P_Request)),
                 Parameter_Type => Make_Pointer_Type (RE (RE_Request_T)));
215 216
            Append_Node_To_List (N, Parameters);

217 218 219 220 221
            N :=
              Make_Function_Specification
                (Defining_Identifier => RE (RE_Main_Deliver),
                 Parameters          => Parameters,
                 Return_Type         => New_Node (CTN.K_Void));
222 223 224

            Append_Node_To_List (N, CTN.Declarations (Current_File));

225
            Bind_AADL_To_Job (Identifier (Parent_Subcomponent (E)), N);
226 227
         end if;

228 229 230 231 232 233 234
         --  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))
235 236 237
                 and then
                   Get_Bound_Processor (Corresponding_Instance (S)) =
                   Get_Bound_Processor (E)
238
               then
239
                  Visit_Device_Instance (Corresponding_Instance (S));
240 241 242 243 244
               end if;
               S := Next_Node (S);
            end loop;
         end if;

245 246 247 248
         Pop_Entity; -- U
         Pop_Entity; -- P
      end Visit_Process_Instance;

249 250 251 252 253
      ---------------------------
      -- Visit_Device_Instance --
      ---------------------------

      procedure Visit_Device_Instance (E : Node_Id) is
254 255
         Implementation : constant Node_Id := Get_Implementation (E);
         S              : Node_Id;
256
      begin
jdelange's avatar
jdelange committed
257
         Current_Device := E;
258 259 260 261 262 263 264 265 266
         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;
jdelange's avatar
jdelange committed
267
         Current_Device := No_Node;
268 269
      end Visit_Device_Instance;

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
      ---------------------------
      -- Visit_System_Instance --
      ---------------------------

      procedure Visit_System_Instance (E : Node_Id) is
         S : Node_Id;
      begin
         Push_Entity (C_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; --  C_Root
      end Visit_System_Instance;

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

      procedure Visit_Thread_Instance (E : Node_Id) is
         S : constant Node_Id := Parent_Subcomponent (E);
         N : Node_Id;
      begin

         if Has_Ports (E) then
            N := Task_Deliver_Spec (E);
            Append_Node_To_List (N, CTN.Declarations (Current_File));
            Bind_AADL_To_Global_Port (Identifier (S), N);
308 309 310

            N := Task_Deliver_Spec (E);

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
         end if;

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

         N := Task_Job_Spec (E);
         Append_Node_To_List (N, CTN.Declarations (Current_File));
         Bind_AADL_To_Job (Identifier (S), N);

      end Visit_Thread_Instance;

   end Header_File;

   ------------
   -- Source --
   ------------

   package body Source_File 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);
334
      procedure Visit_Device_Instance (E : Node_Id);
335 336 337 338 339 340
      procedure Visit_Thread_Instance (E : Node_Id);

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

341
      Main_Deliver_Alternatives : List_Id;
342

343 344
      Current_Device : Node_Id := No_Node;

345 346 347 348 349
      -------------------
      -- Task_Job_Body --
      -------------------

      function Task_Job_Body (E : Node_Id) return Node_Id is
350 351 352 353 354 355 356
         S    : constant Node_Id := Parent_Subcomponent (E);
         Spec : constant Node_Id :=
           CTN.Job_Node (Backend_Node (Identifier (S)));
         Declarations : constant List_Id := New_List (CTN.K_Declaration_List);
         Statements   : constant List_Id := New_List (CTN.K_Statement_List);
         WStatements  : constant List_Id := New_List (CTN.K_Statement_List);
         P            : constant Supported_Thread_Dispatch_Protocol :=
357
           Get_Thread_Dispatch_Protocol (E);
358
         Impl_Kind : constant Supported_Thread_Implementation :=
359 360 361 362 363 364 365 366 367 368
           Get_Thread_Implementation_Kind (E);
         Call_Parameters : List_Id;
         N               : Node_Id;

         procedure Make_Wait_Event;
         procedure Make_Call_Sequence;
         procedure Make_Set_Out_Ports;
         procedure Make_Send_Out_Ports;
         procedure Make_Task_Blocking;
         procedure Make_Fetch_In_Ports;
369
         procedure Make_Wait_Offset;
370 371
         procedure Make_Thread_Compute_Entrypoint;
         procedure Make_Ports_Compute_Entrypoint;
372
         procedure Make_Activate_Entrypoint;
373 374
         function Make_Get_Valid_Value (F : Node_Id) return Node_Id;

375 376 377
         ------------------------------
         -- Make_Activate_Entrypoint --
         ------------------------------
378

379
         procedure Make_Activate_Entrypoint is
380 381
            Entrypoint : constant Node_Id :=
              Get_Thread_Activate_Entrypoint (E);
382

yoogx's avatar
yoogx committed
383
            Parameter_List : constant List_Id := New_List (CTN.K_List_Id);
384 385 386
         begin
            if Entrypoint /= No_Node then
               Append_Node_To_List
387
                 (Make_Call_Profile (Map_C_Subprogram_Identifier (Entrypoint)),
388
                  Statements);
389 390 391 392 393 394 395 396

               Append_Node_To_List
                 (Make_Extern_Entity_Declaration
                    (Make_Function_Specification
                       (Map_C_Subprogram_Identifier (Entrypoint),
                        Parameters  => Parameter_List, --  XXX
                        Return_Type => New_Node (CTN.K_Void))),
                  CTN.Declarations (Current_File));
397
            end if;
398
         end Make_Activate_Entrypoint;
399

400 401 402 403 404 405 406 407 408 409
         --------------------------
         -- Make_Get_Valid_Value --
         --------------------------

         function Make_Get_Valid_Value (F : Node_Id) return Node_Id is
            Then_Statements : constant List_Id :=
              New_List (CTN.K_Statement_List);
            Condition : Node_Id;
            N         : Node_Id;
         begin
410 411 412 413 414
            N :=
              Make_Variable_Declaration
                (Defining_Identifier =>
                   Make_Defining_Identifier
                     (Map_C_Variable_Name (F, Port_Request => True)),
yoogx's avatar
yoogx committed
415 416
                 Used_Type => RE (RE_Request_T),
                 Is_Static => True);
417 418 419 420
            Append_Node_To_List (N, Declarations);

            Call_Parameters := New_List (CTN.K_Parameter_List);
            Append_Node_To_List
421
              (Make_Defining_Identifier (Map_C_Enumerator_Name (S)),
422 423 424
               Call_Parameters);
            Append_Node_To_List
              (Make_Defining_Identifier
425
                 (Map_C_Enumerator_Name (F, Local_Port => True)),
426 427 428
               Call_Parameters);

            if not Get_Wait_For_All_Events (E) then
429 430
               Condition :=
                 Make_Call_Profile (RE (RE_Gqueue_Get_Count), Call_Parameters);
431 432 433 434 435 436 437 438 439
            else
               Condition := Make_Literal (CV.New_Int_Value (1, 0, 10));
            end if;

            --  If we wait for all events ports, the condition is always true

            Call_Parameters := New_List (CTN.K_Parameter_List);

            Append_Node_To_List
440
              (Make_Defining_Identifier (Map_C_Enumerator_Name (S)),
441 442 443 444
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
445
                 (Map_C_Enumerator_Name (F, Local_Port => True)),
446 447 448 449
               Call_Parameters);

            Append_Node_To_List
              (Make_Variable_Address
450 451 452
                 (Make_Defining_Identifier
                    (Map_C_Variable_Name (F, Port_Request => True))),
               Call_Parameters);
453

454
            N := Make_Call_Profile (RE (RE_Gqueue_Get_Value), Call_Parameters);
455 456 457 458 459 460 461 462

            Append_Node_To_List (N, Then_Statements);

            --  Add the call to next_value

            Call_Parameters := New_List (CTN.K_Parameter_List);

            Append_Node_To_List
463
              (Make_Defining_Identifier (Map_C_Enumerator_Name (S)),
464 465 466 467
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
468
                 (Map_C_Enumerator_Name (F, Local_Port => True)),
469 470
               Call_Parameters);

471 472
            N :=
              Make_Call_Profile (RE (RE_Gqueue_Next_Value), Call_Parameters);
473 474 475

            Append_Node_To_List (N, Then_Statements);

476
            return Make_If_Statement (Condition, Then_Statements);
477 478 479 480 481 482 483 484
         end Make_Get_Valid_Value;

         ---------------------
         -- Make_Wait_Event --
         ---------------------

         procedure Make_Wait_Event is
         begin
485 486 487 488
            N :=
              Make_Variable_Declaration
                (Defining_Identifier => Make_Defining_Identifier (VN (V_Port)),
                 Used_Type           => RE (RE_Local_Port_T));
489 490 491 492 493 494 495 496
            Append_Node_To_List (N, Declarations);

            --  Make the call to __po_hi_gqueue_wait_for_incoming_event

            Call_Parameters := New_List (CTN.K_Parameter_List);
            N := Make_Defining_Identifier (Map_C_Enumerator_Name (S));
            Append_Node_To_List (N, Call_Parameters);

497 498
            N :=
              Make_Variable_Address (Make_Defining_Identifier (VN (V_Port)));
499 500
            Append_Node_To_List (N, Call_Parameters);

501 502 503 504
            N :=
              CTU.Make_Call_Profile
                (RE (RE_Gqueue_Wait_For_Incoming_Event),
                 Call_Parameters);
505 506 507 508 509
            Append_Node_To_List (N, WStatements);

            --  Make the call to __po_hi_compute_next_period

            Call_Parameters := New_List (CTN.K_Parameter_List);
510 511 512
            N               :=
              Make_Defining_Identifier
                (Map_C_Enumerator_Name (S, Custom_Parent => Current_Device));
513

514 515
            Append_Node_To_List (N, Call_Parameters);

516 517 518 519
            N :=
              CTU.Make_Call_Profile
                (RE (RE_Compute_Next_Period),
                 Call_Parameters);
520 521 522 523 524 525 526 527 528 529 530 531
            Append_Node_To_List (N, WStatements);
         end Make_Wait_Event;

         ------------------------
         -- Make_Task_Blocking --
         ------------------------

         procedure Make_Task_Blocking is
         begin
            --  Make the __po_hi_wait_for_next_period call

            Call_Parameters := New_List (CTN.K_Parameter_List);
532
            if Current_Device /= No_Node then
533 534 535 536 537
               N :=
                 Make_Defining_Identifier
                   (Map_C_Enumerator_Name
                      (S,
                       Custom_Parent => Current_Device));
538 539 540
            else
               N := Make_Defining_Identifier (Map_C_Enumerator_Name (S));
            end if;
541 542
            Append_Node_To_List (N, Call_Parameters);

543 544 545 546
            N :=
              CTU.Make_Call_Profile
                (RE (RE_Wait_For_Next_Period),
                 Call_Parameters);
547 548 549 550 551 552 553 554
            Append_Node_To_List (N, WStatements);
         end Make_Task_Blocking;

         ------------------------
         -- Make_Set_Out_Ports --
         ------------------------

         procedure Make_Set_Out_Ports is
555 556 557 558 559 560
            N          : Node_Id;
            L          : Node_Id;
            R          : Node_Id;
            F          : Node_Id;
            T          : Node_Id;
            Z          : Node_Id;
561 562 563 564 565 566 567 568
            Parameters : List_Id;
         begin
            N := Message_Comment ("Set the OUT port values");
            Append_Node_To_List (N, WStatements);

            F := First_Node (Features (E));

            while Present (F) loop
569 570 571 572
               if Kind (F) = K_Port_Spec_Instance
                 and then Is_Out (F)
                 and then not AAU.Is_Empty (AAN.Sources (F))
               then
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
                  --  We do not set the ports that are connected to
                  --  subprogram out ports, this should be done during
                  --  the subprogram call sequence handling.

                  declare
                     use Ocarina.Backends.C_Tree.Nodes;
                     D   : Node_Id := AAN.First_Node (AAN.Sources (F));
                     Set : Boolean := True;
                  begin
                     while Present (D) loop
                        Set := Kind (Item (D)) /= K_Port_Spec_Instance;
                        exit when not Set;

                        D := AAN.Next_Node (D);
                     end loop;

                     if Set then
590 591 592 593
                        N :=
                          Make_Variable_Declaration
                            (Make_Defining_Identifier
                               (Map_C_Variable_Name (F, Port_Request => True)),
594 595
                             RE (RE_Request_T),
                            Is_Static => True);
596 597
                        Append_Node_To_List (N, Declarations);

598 599 600 601
                        L :=
                          Make_Member_Designator
                            (Defining_Identifier =>
                               Make_Member_Designator
602 603
                                 (Defining_Identifier =>
                                    Make_Member_Designator
604 605 606
                                      (Defining_Identifier =>
                                         Make_Defining_Identifier
                                           (Map_C_Enumerator_Name (F)),
607
                                       Aggregate_Name =>
608 609 610 611 612 613
                                         Make_Defining_Identifier
                                           (Map_C_Enumerator_Name (F))),
                                  Aggregate_Name =>
                                    Make_Defining_Identifier (MN (M_Vars))),
                             Aggregate_Name =>
                               Make_Defining_Identifier
614
                                 (Map_C_Variable_Name
615 616 617 618 619 620 621
                                    (F,
                                     Port_Request => True)));
                        R :=
                          Make_Defining_Identifier
                            (Map_C_Variable_Name
                               (F,
                                Request_Variable => True));
622 623 624 625

                        Z := AAN.Corresponding_Instance (F);
                        T := No_Node;

626 627 628 629 630 631 632 633 634
                        if Present (Backend_Node (Identifier (Z)))
                          and then Present
                            (CTN.Type_Definition_Node
                               (Backend_Node (Identifier (Z))))
                        then
                           T :=
                             CTN.Type_Name
                               (CTN.Type_Definition_Node
                                  (Backend_Node (Identifier (Z))));
635 636
                        end if;

637 638 639
                        if T /= No_Node
                          and then CTN.Kind (T) = CTN.K_Array_Declaration
                        then
640 641 642
                           Parameters := New_List (CTN.K_Parameter_List);
                           Append_Node_To_List (L, Parameters);
                           Append_Node_To_List (R, Parameters);
643 644 645 646 647
                           Append_Node_To_List (Get_Data_Size (Z), Parameters);
                           N :=
                             CTU.Make_Call_Profile
                               (RE (RE_Copy_Array),
                                Parameters);
648
                        else
649 650 651 652 653
                           N :=
                             Make_Expression
                               (Left_Expr  => L,
                                Operator   => Op_Equal,
                                Right_Expr => R);
654 655
                        end if;

656
                        Append_Node_To_List (N, WStatements);
657

658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
                        N :=
                          Make_Expression
                            (Left_Expr =>
                               Make_Member_Designator
                                 (Defining_Identifier =>
                                    Make_Defining_Identifier (MN (M_Port)),
                                  Aggregate_Name =>
                                    Make_Defining_Identifier
                                      (Map_C_Variable_Name
                                         (F,
                                          Port_Request => True))),
                             Operator   => Op_Equal,
                             Right_Expr =>
                               Make_Defining_Identifier
                                 (Map_C_Enumerator_Name (F)));
                        Append_Node_To_List (N, WStatements);
674 675

                        Call_Parameters := New_List (CTN.K_Parameter_List);
676 677 678
                        N               :=
                          Make_Defining_Identifier
                            (Map_C_Enumerator_Name (S, Current_Device));
679 680
                        Append_Node_To_List (N, Call_Parameters);

681 682 683
                        N :=
                          Make_Defining_Identifier
                            (Map_C_Enumerator_Name (F, Local_Port => True));
684 685
                        Append_Node_To_List (N, Call_Parameters);

686 687 688 689 690 691
                        N :=
                          Make_Variable_Address
                            (Make_Defining_Identifier
                               (Map_C_Variable_Name
                                  (F,
                                   Port_Request => True)));
692 693
                        Append_Node_To_List (N, Call_Parameters);

694 695 696 697
                        N :=
                          CTU.Make_Call_Profile
                            (RE (RE_Gqueue_Store_Out),
                             Call_Parameters);
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712

                        Append_Node_To_List (N, WStatements);
                     end if;
                  end;
               end if;

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

         -------------------------
         -- Make_Send_Out_Ports --
         -------------------------

         procedure Make_Send_Out_Ports is
713 714 715
            N                     : Node_Id;
            F                     : Node_Id;
            Error_Already_Defined : Boolean := False;
716 717 718 719 720 721 722
         begin
            N := Message_Comment ("Send the OUT ports");
            Append_Node_To_List (N, WStatements);

            F := First_Node (Features (E));

            while Present (F) loop
723

724
               if Kind (F) = K_Port_Spec_Instance and then Is_Out (F) then
725 726

                  --  Then, call the send_output in the main loop.
727
                  Call_Parameters := New_List (CTN.K_Parameter_List);
728 729 730
                  N               :=
                    Make_Defining_Identifier
                      (Map_C_Enumerator_Name (S, Current_Device));
731 732
                  Append_Node_To_List (N, Call_Parameters);

733
                  N := Make_Defining_Identifier (Map_C_Enumerator_Name (F));
734 735
                  Append_Node_To_List (N, Call_Parameters);

736 737 738 739
                  N :=
                    CTU.Make_Call_Profile
                      (RE (RE_Send_Output),
                       Call_Parameters);
740

741 742 743 744 745 746 747 748 749
                  if Get_Miss_Rate (F) /= 0 then
                     --  XXX is this required ? from PolyORB-HI/C,
                     --  looks like it is some hack to add fake miss
                     --  of events.

                     Append_Node_To_List
                       (Make_If_Statement
                          (Condition =>
                             Make_Call_Profile
750 751 752 753 754 755 756 757 758
                               (RE (RE_Compute_Miss),
                                Make_List_Id
                                  (Make_Literal
                                     (CV.New_Int_Value
                                        (Get_Miss_Rate (F),
                                         0,
                                         10)))),
                           Statements => Make_List_Id (N)),
                        WStatements);
759
                  else
760 761 762 763 764
                     N :=
                       Make_Assignment_Statement
                         (Variable_Identifier =>
                            Make_Defining_Identifier (VN (V_Error)),
                          Expression => N);
765 766 767
                     Append_Node_To_List (N, WStatements);

                     if not Error_Already_Defined then
768 769 770 771 772
                        N :=
                          Make_Variable_Declaration
                            (Defining_Identifier =>
                               Make_Defining_Identifier (VN (V_Error)),
                             Used_Type => RE (RE_Int32_T));
773 774 775 776 777 778 779 780 781 782 783 784
                        Append_Node_To_List (N, Declarations);
                        Error_Already_Defined := True;
                     end if;
                     declare
                        Rec_Entrypoint : constant Name_Id :=
                          Get_Thread_Recover_Entrypoint (E);
                        Parameter_List : constant List_Id :=
                          New_List (CTN.K_List_Id);
                        Then_Statements : constant List_Id :=
                          New_List (CTN.K_Statement_List);
                     begin
                        if Rec_Entrypoint /= No_Name then
785 786 787 788 789 790
                           N :=
                             Make_Extern_Entity_Declaration
                               (Make_Function_Specification
                                  (Make_Defining_Identifier (Rec_Entrypoint),
                                   Parameters  => Parameter_List, --  XXX
                                   Return_Type => New_Node (CTN.K_Void)));
791
                           Append_Node_To_List
792 793
                             (N,
                              CTN.Declarations (Current_File));
794

795 796 797 798
                           N :=
                             CTU.Make_Call_Profile
                               (Make_Defining_Identifier (Rec_Entrypoint),
                                No_List);
799 800
                           Append_Node_To_List (N, Then_Statements);

801 802 803 804 805 806 807 808
                           N :=
                             Make_If_Statement
                               (Condition =>
                                  Make_Expression
                                    (Make_Defining_Identifier (VN (V_Error)),
                                     Op_Not_Equal,
                                     RE (RE_SUCCESS)),
                                Statements => Then_Statements);
809 810 811 812 813 814 815 816 817 818 819
                           Append_Node_To_List (N, WStatements);
                        end if;
                     end;

                  end if;
               end if;

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

820 821 822 823 824 825 826 827 828 829 830 831 832 833
         ----------------------
         -- Make_Wait_Offset --
         ----------------------

         procedure Make_Wait_Offset is
            D : Time_Type;
         begin
            if Get_Thread_Dispatch_Protocol (E) /= Thread_Periodic then
               return;
            end if;

            D := Get_Dispatch_Offset (E);

            if D /= Null_Time then
834 835 836 837
               N :=
                 Make_Variable_Declaration
                   (Make_Defining_Identifier (VN (V_Offset)),
                    RE (RE_Time_T));
838 839 840
               Append_Node_To_List (N, Statements);

               N := Map_Time (D, VN (V_Offset));
841
               Append_Node_To_List (N, Statements);
842 843 844

               Append_Node_To_List
                 (CTU.Make_Call_Profile
845
                    (RE (RE_Task_Wait_Offset),
846
                     Make_List_Id
847 848
                       (Make_Variable_Address
                          (Make_Defining_Identifier (VN (V_Offset))))),
yoogx's avatar
yoogx committed
849
                  Statements);
850 851 852 853

            end if;
         end Make_Wait_Offset;

854 855 856 857 858 859 860
         -------------------------
         -- Make_Fetch_In_Ports --
         -------------------------

         procedure Make_Fetch_In_Ports is
            F : Node_Id;
         begin
861
            N := Message_Comment ("Get the IN ports values");
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
            Append_Node_To_List (N, CTN.Declarations (Current_File));

            F := First_Node (Features (E));

            while Present (F) loop
               if Kind (F) = K_Port_Spec_Instance
                 and then Is_In (F)
                 and then AAN.Is_Data (F)
               then
                  --  Assign the port value

                  N := Make_Get_Valid_Value (F);
                  Append_Node_To_List (N, WStatements);

                  Call_Parameters := New_List (CTN.K_Parameter_List);

                  Append_Node_To_List
879
                    (Make_Defining_Identifier (Map_C_Enumerator_Name (S)),
880 881 882
                     Call_Parameters);

                  Append_Node_To_List
883
                    (Make_Defining_Identifier (Map_C_Enumerator_Name (F)),
884 885
                     Call_Parameters);

886 887 888 889
                  N :=
                    Make_Call_Profile
                      (RE (RE_Gqueue_Next_Value),
                       Call_Parameters);
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908

               end if;

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

         ------------------------
         -- Make_Call_Sequence --
         ------------------------

         procedure Make_Call_Sequence is
            Call_Seq : constant Node_Id := First_Node (Calls (E));
         begin
            if not Has_Modes (E) or else AAU.Length (Calls (E)) = 1 then
               --  If the thread has no modes, then it should has one
               --  unique call sequence, handle it.

               CTU.Handle_Call_Sequence
909 910 911 912 913
                 (S,
                  Call_Seq,
                  Declarations,
                  WStatements,
                  Current_Device);
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
            else
               N := Message_Comment ("not implemented yet");
               Append_Node_To_List (N, WStatements);
            end if;
         end Make_Call_Sequence;

         -----------------------------------
         -- Make_Ports_Compute_Entrypoint --
         -----------------------------------

         procedure Make_Ports_Compute_Entrypoint is
            N                   : Node_Id;
            F                   : Node_Id;
            Switch_Alternatives : List_Id;
            Switch_Statements   : List_Id;
            Switch_Labels       : List_Id;
         begin
            N := Message_Comment ("Make_Ports_Compute_Entrypoint");
            Append_Node_To_List (N, WStatements);

            F := First_Node (Features (E));

            Switch_Alternatives := New_List (CTN.K_Alternatives_List);

            while Present (F) loop
               if Kind (F) = K_Port_Spec_Instance and then Is_In (F) then

                  Switch_Statements := New_List (CTN.K_Statement_List);
942
                  Switch_Labels     := New_List (CTN.K_Label_List);
943 944 945 946 947 948 949 950 951 952 953 954 955

                  --  Declare local data variable if the port is a
                  --  data port.

                  if AAN.Is_Data (F) then
                     N := Make_Get_Valid_Value (F);
                     Append_Node_To_List (N, Switch_Statements);
                  end if;

                  if Is_Event (F) and then not AAN.Is_Data (F) then
                     Call_Parameters := New_List (CTN.K_Parameter_List);

                     Append_Node_To_List
956
                       (Make_Defining_Identifier (Map_C_Enumerator_Name (S)),
957 958 959 960
                        Call_Parameters);

                     Append_Node_To_List
                       (Make_Defining_Identifier
961
                          (Map_C_Enumerator_Name (F, Local_Port => True)),
962 963
                        Call_Parameters);

964 965 966 967
                     N :=
                       Make_Call_Profile
                         (RE (RE_Gqueue_Next_Value),
                          Call_Parameters);
968 969 970 971 972

                     Append_Node_To_List (N, Switch_Statements);
                  end if;

                  Call_Parameters := New_List (CTN.K_Parameter_List);
973
                  N := Make_Defining_Identifier (Map_C_Enumerator_Name (S));
974 975 976 977 978 979
                  Append_Node_To_List (N, Call_Parameters);

                  declare
                     Parameter_List : constant List_Id :=
                       New_List (CTN.K_List_Id);
                  begin
980 981 982 983
                     N :=
                       Make_Parameter_Specification
                         (Make_Defining_Identifier (PN (P_Self)),
                          Parameter_Type => RE (RE_Task_Id));
984 985 986
                     Append_Node_To_List (N, Parameter_List);

                     if AAN.Is_Data (F) then
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
                        N :=
                          Make_Member_Designator
                            (Defining_Identifier =>
                               Make_Member_Designator
                                 (Defining_Identifier =>
                                    Make_Member_Designator
                                      (Defining_Identifier =>
                                         Make_Defining_Identifier
                                           (Map_C_Enumerator_Name (F)),
                                       Aggregate_Name =>
                                         Make_Defining_Identifier
                                           (Map_C_Enumerator_Name (F))),
                                  Aggregate_Name =>
                                    Make_Defining_Identifier (MN (M_Vars))),
                             Aggregate_Name =>
                               Make_Defining_Identifier
                                 (Map_C_Variable_Name
                                    (F,
                                     Port_Request => True)));
1006

1007
                        Append_Node_To_List (N, Call_Parameters);
1008

1009 1010 1011 1012 1013 1014 1015
                        N :=
                          Map_C_Data_Type_Designator
                            (Corresponding_Instance (F));
                        N :=
                          Make_Parameter_Specification
                            (Map_C_Defining_Identifier (F),
                             N);
1016 1017 1018
                        Append_Node_To_List (N, Parameter_List);
                     end if;

1019 1020 1021 1022 1023 1024 1025
                     N :=
                       Make_Extern_Entity_Declaration
                         (Make_Function_Specification
                            (Map_C_Subprogram_Identifier (F),
                             Parameters  => Parameter_List,
                             Return_Type => New_Node (CTN.K_Void)));
                     Append_Node_To_List (N, CTN.Declarations (Current_File));
1026 1027
                  end;

1028 1029 1030 1031
                  N :=
                    Make_Call_Profile
                      (Map_C_Subprogram_Identifier (F),
                       Call_Parameters);
1032 1033 1034 1035
                  Append_Node_To_List (N, Switch_Statements);

                  Append_Node_To_List
                    (Make_Defining_Identifier
1036
                       (Map_C_Enumerator_Name (F, Local_Port => True)),
1037 1038
                     Switch_Labels);

1039 1040
                  N :=
                    Make_Switch_Alternative (Switch_Labels, Switch_Statements);
1041 1042 1043 1044 1045 1046
                  Append_Node_To_List (N, Switch_Alternatives);
               end if;

               F := Next_Node (F);
            end loop;

1047
            N := Make_Switch_Alternative (No_List, No_List);
1048 1049 1050 1051
            Append_Node_To_List (N, Switch_Alternatives);

            --  Make the case statement

1052 1053 1054 1055
            N :=
              Make_Switch_Statement
                (Expression   => Make_Defining_Identifier (MN (M_Port)),
                 Alternatives => Switch_Alternatives);
1056 1057 1058 1059 1060 1061 1062 1063
            Append_Node_To_List (N, WStatements);
         end Make_Ports_Compute_Entrypoint;

         ------------------------------------
         -- Make_Thread_Compute_Entrypoint --
         ------------------------------------

         procedure Make_Thread_Compute_Entrypoint is
1064
            N              : Node_Id;
1065 1066 1067 1068 1069 1070
            Parameter_List : constant List_Id := New_List (CTN.K_List_Id);
         begin
            N := Message_Comment ("Make_Thread_Compute_Entrypoint");
            Append_Node_To_List (N, WStatements);

            Call_Parameters := New_List (CTN.K_Parameter_List);
1071
            N := Make_Defining_Identifier (Map_C_Enumerator_Name (S));
1072 1073
            Append_Node_To_List (N, Call_Parameters);

1074 1075 1076 1077
            N :=
              Make_Parameter_Specification
                (Make_Defining_Identifier (PN (P_Self)),
                 Parameter_Type => RE (RE_Task_Id));
1078 1079 1080 1081 1082 1083 1084
            Append_Node_To_List (N, Parameter_List);

            if P = Thread_Sporadic then
               Append_Node_To_List
                 (Make_Defining_Identifier (VN (V_Port)),
                  Call_Parameters);

1085 1086 1087 1088
               N :=
                 Make_Parameter_Specification
                   (Make_Defining_Identifier (VN (V_Port)),
                    Parameter_Type => RE (RE_Port_T));
1089 1090 1091 1092
               Append_Node_To_List (N, Parameter_List);
            end if;

            if not Has_Modes (E) then
1093 1094 1095 1096 1097 1098
               N :=
                 Make_Extern_Entity_Declaration
                   (Make_Function_Specification
                      (Map_C_Subprogram_Identifier (E),
                       Parameters  => Parameter_List,
                       Return_Type => New_Node (CTN.K_Void)));
1099

1100
               Append_Node_To_List (N, CTN.Declarations (Current_File));
1101

1102 1103 1104 1105
               N :=
                 Make_Call_Profile
                   (Map_C_Subprogram_Identifier (E),
                    Call_Parameters);
1106 1107 1108 1109
               Append_Node_To_List (N, WStatements);
            else
               Display_Located_Error
                 (Loc (E),
1110 1111
                  "Threads with mode controlled compute entrypoints not" &
                  " supported yet",
1112 1113 1114 1115 1116 1117 1118
                  Fatal => True);
            end if;
         end Make_Thread_Compute_Entrypoint;

      begin
         case P is
            when Thread_Periodic =>
1119 1120 1121 1122
               N :=
                 Message_Comment
                   ("Periodic task : " &
                    Get_Name_String (Display_Name (Identifier (S))));
1123 1124 1125
               Append_Node_To_List (N, CTN.Declarations (Current_File));

            when Thread_Sporadic =>
1126 1127 1128 1129
               N :=
                 Message_Comment
                   ("Sporadic task : " &
                    Get_Name_String (Display_Name (Identifier (S))));
1130 1131
               Append_Node_To_List (N, CTN.Declarations (Current_File));

1132
            when Thread_Background =>
1133 1134 1135 1136
               N :=
                 Message_Comment
                   ("Background task : " &
                    Get_Name_String (Display_Name (Identifier (S))));
1137 1138
               Append_Node_To_List (N, CTN.Declarations (Current_File));

1139 1140 1141 1142 1143
            when others =>
               Display_Error ("unknown type of thread", Fatal => False);
               null;
         end case;

1144
         Make_Activate_Entrypoint;
1145

1146 1147 1148 1149 1150 1151 1152 1153 1154
         Check_Thread_Consistency (E);

         if Has_Ports (E) then
            --  Make the __po_hi_gqueue_init call

            Call_Parameters := New_List (CTN.K_Parameter_List);

            Append_Node_To_List
              (Make_Defining_Identifier
1155
                 (Map_C_Enumerator_Name (S, Current_Device)),
1156 1157 1158 1159
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
1160
                 (Map_C_Define_Name (S, Nb_Ports => True)),
1161 1162 1163 1164
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
1165
                 (Map_C_Variable_Name (S, Port_Queue => True)),
1166 1167 1168 1169
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
1170
                 (Map_C_Variable_Name (S, Port_Fifo_Size => True)),
1171 1172 1173 1174
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
1175
                 (Map_C_Variable_Name (S, Port_First => True)),
1176 1177 1178 1179
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
1180
                 (Map_C_Variable_Name (S, Port_Offsets => True)),
1181 1182 1183 1184
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
1185
                 (Map_C_Variable_Name (S, Port_Woffsets => True)),
1186 1187 1188 1189
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
1190
                 (Map_C_Variable_Name (S, Port_N_Dest => True)),
1191 1192 1193 1194
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
1195
                 (Map_C_Variable_Name (S, Port_Destinations => True)),
1196 1197 1198 1199
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
1200
                 (Map_C_Variable_Name (S, Port_Used_Size => True)),
1201 1202 1203 1204
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
1205
                 (Map_C_Variable_Name (S, Port_History => True)),
1206 1207 1208 1209
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
1210
                 (Map_C_Variable_Name (S, Port_Recent => True)),
1211 1212 1213 1214
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
1215
                 (Map_C_Variable_Name (S, Port_Empties => True)),
1216 1217 1218 1219
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
1220
                 (Map_C_Variable_Name (S, Port_Total_Fifo => True)),
1221 1222
               Call_Parameters);

1223
            N := Make_Call_Profile (RE (RE_Gqueue_Init), Call_Parameters);
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 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
            Append_Node_To_List (N, Statements);
         end if;

         --  If the thread is sporadic or aperiodic, we generate the
         --  call to block waiting for events.

         if P = Thread_Sporadic or else P = Thread_Aperiodic then
            Make_Wait_Event;
         end if;

         --  Depending on the implementation kind, call the proper
         --  implementation routines.

         case Impl_Kind is
            when Thread_With_Call_Sequence =>
               --  This kind of implementation is the simplest
               --  one. The user has only to implementation the
               --  behaviour of subprograms and does not have to worry
               --  about sending and receiving ports.

               --  Get IN ports values and dequeue them

               if Has_In_Ports (E) then
                  Make_Fetch_In_Ports;
               end if;

               --  Handle the thread call sequences

               if not AAU.Is_Empty (Calls (E)) then
                  Make_Call_Sequence;
               end if;

               --  Set OUT ports values

               if Has_Out_Ports (E) then
                  Make_Set_Out_Ports;
               end if;

               --  Send OUT ports

               if Has_Out_Ports (E) then
                  Make_Send_Out_Ports;
               end if;

            when Thread_With_Compute_Entrypoint =>
               --  Call the compute entrypoint. The code of the
               --  compute entry point will include the setting of
               --  the thread OUT ports.

               Make_Thread_Compute_Entrypoint;

               --  Send OUT ports

               --  FIXME: Depending on an AADL property, the code of
               --  the thread entrypoint may include the sending of
               --  OUT ports.

               if Has_Out_Ports (E) then
                  Make_Send_Out_Ports;
               end if;

            when Thread_With_Port_Compute_Entrypoint =>
               --  Call the compute entrypoints of the triggeing
               --  port. The code of the compute entry point will
               --  include the sending of the thread OUT ports.

               Make_Ports_Compute_Entrypoint;

               --  Send OUT ports

               --  FIXME: Depending on an AADL property, the code of
               --  the port entrypoints may include the sending of OUT
               --  ports.

               if Has_Out_Ports (E) then
                  Make_Send_Out_Ports;
               end if;

            when others =>
               raise Program_Error with "Unconsistency in Task_Job_Body";
         end case;

         --  Block until the next dispatch with respect to the
         --  inter-arrival time in case of a sporadic or periodic
         --  thread.

         if P = Thread_Periodic or else P = Thread_Sporadic then
            Make_Task_Blocking;
         end if;

1314
         --  If an activate entrypoint has been specified for the
1315 1316 1317 1318
         --  thread, add a call to the corresponding function before
         --  initialization of the runtime is done.

         declare
1319
            Activate_Entrypoint : constant Name_Id :=
1320
              Get_Thread_Activate_Entrypoint (E);
1321
            Parameter_List : constant List_Id := New_List (CTN.K_List_Id);
1322
         begin
1323
            if Activate_Entrypoint /= No_Name then
1324 1325 1326
               N :=
                 Make_Extern_Entity_Declaration
                   (Make_Function_Specification
1327
                      (Make_Defining_Identifier (Activate_Entrypoint),
1328 1329 1330 1331 1332
                       Parameters  => Parameter_List, --  XXX
                       Return_Type => New_Node (CTN.K_Void)));
               Append_Node_To_List (N, CTN.Declarations (Current_File));
               N :=
                 CTU.Make_Call_Profile
1333
                   (Make_Defining_Identifier (Activate_Entrypoint),
1334
                    No_List);
1335 1336 1337 1338
               Append_Node_To_List (N, Statements);
            end if;
         end;

1339 1340 1341 1342
         N :=
           Make_Doxygen_C_Comment
             ("Waiting for other tasks initialization",
              Has_Header_Spaces => False);
1343 1344
         Append_Node_To_List (N, Statements);

1345 1346
         --  Call __po_hi_wait_initialization

1347
         N := CTU.Make_Call_Profile (RE (RE_Wait_Initialization), No_List);
1348 1349
         Append_Node_To_List (N, Statements);

yoogx's avatar
yoogx committed
1350 1351 1352 1353
         --  Call __po_hi_wait_offset() iff necessary

         Make_Wait_Offset;

1354
         --  Compute the next period after initialization, because
1355
         --  the period may have passed after init.
1356 1357

         if P = Thread_Periodic or else P = Thread_Sporadic then
1358 1359 1360 1361 1362 1363
            N :=
              CTU.Make_Call_Profile
                (RE (RE_Compute_Next_Period),
                 Make_List_Id
                   (Make_Defining_Identifier
                      (Map_C_Enumerator_Name (S, Current_Device))));
1364 1365 1366
            Append_Node_To_List (N, Statements);
         end if;

1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
         if P = Thread_Periodic then
            --  For periodic threads, we force a first wait to ensure
            --  synchronized start after initialization. The runtime
            --  computes a specific epoch to ensure such start.

            N :=
              Make_Doxygen_C_Comment
              ("Waiting for the first dispatch instant",
               Has_Header_Spaces => False);
            Append_Node_To_List (N, Statements);

            Call_Parameters := New_List (CTN.K_Parameter_List);
            if Current_Device /= No_Node then
               N :=
                 Make_Defining_Identifier
                 (Map_C_Enumerator_Name
                    (S,
                     Custom_Parent => Current_Device));
            else
               N := Make_Defining_Identifier (Map_C_Enumerator_Name (S));
            end if;
            Append_Node_To_List (N, Call_Parameters);
            N :=
              CTU.Make_Call_Profile
              (RE (RE_Wait_For_Next_Period),
               Call_Parameters);
            Append_Node_To_List (N, Statements);
         end if;

1396 1397
         if P /= Thread_Background then
            --  Make the while (1){} and add all statements
1398 1399 1400 1401 1402
            N :=
              Make_Doxygen_C_Comment
              ("Task body",
               Has_Header_Spaces => False);
            Append_Node_To_List (N, Statements);
1403

1404 1405 1406 1407
            N :=
              Make_While_Statement
                (Make_Literal (CV.New_Int_Value (1, 0, 10)),
                 WStatements);
1408 1409 1410 1411 1412 1413 1414
            Append_Node_To_List (N, Statements);
         else
            --  Simply append statements
            Append_Node_To_List (CTN.First_Node (WStatements), Statements);
         end if;

         N := Make_Function_Implementation (Spec, Declarations, Statements);
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450

         return N;
      end Task_Job_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
1451 1452
         Category : constant Component_Category :=
           Get_Category_Of_Component (E);
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
      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
1474 1475 1476
         U : constant Node_Id :=
           CTN.Distributed_Application_Unit
             (CTN.Naming_Node (Backend_Node (Identifier (E))));
1477 1478 1479
         P            : constant Node_Id := CTN.Entity (U);
         S            : Node_Id;
         N            : Node_Id;
1480
         Declarations : constant List_Id := New_List (CTN.K_Declaration_List);
1481
         Statements   : constant List_Id := New_List (CTN.K_Statement_List);
1482 1483
         The_System   : constant Node_Id :=
           Parent_Component (Parent_Subcomponent (E));
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
      begin
         Push_Entity (P);
         Push_Entity (U);
         CTU.Set_Activity_Source (U);

         Main_Deliver_Alternatives := New_List (CTN.K_Alternatives_List);

         --  Visit all the subcomponents of the process

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

            while Present (S) loop
               if AAU.Is_Data (Corresponding_Instance (S)) then

1499 1500 1501 1502 1503
                  N :=
                    Make_Variable_Declaration
                      (Map_C_Defining_Identifier (S),
                       Map_C_Data_Type_Designator
                         (Corresponding_Instance (S)));
1504 1505

                  Append_Node_To_List
1506
                    (Make_Extern_Entity_Declaration (N),
1507 1508 1509
                     CTN.Declarations (Current_File));

                  Bind_AADL_To_Object (Identifier (S), N);
1510 1511 1512 1513
               end if;
               S := Next_Node (S);
            end loop;
         end if;
1514

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

1518
            while Present (S) loop
1519

1520
               if not AAU.Is_Data (Corresponding_Instance (S)) then
1521 1522 1523 1524 1525 1526
                  Visit (Corresponding_Instance (S));
               end if;
               S := Next_Node (S);
            end loop;
         end if;

1527 1528 1529 1530 1531 1532 1533
         --  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))
1534 1535 1536
                 and then
                   Get_Bound_Processor (Corresponding_Instance (S)) =
                   Get_Bound_Processor (E)
1537
               then
1538
                  Visit_Device_Instance (Corresponding_Instance (S));
1539 1540 1541 1542 1543
               end if;
               S := Next_Node (S);
            end loop;
         end if;

1544 1545 1546 1547 1548
         if Present (Backend_Node (Identifier (Parent_Subcomponent (E))))
           and then Present
             (CTN.Job_Node
                (Backend_Node (Identifier (Parent_Subcomponent (E)))))
         then
1549 1550 1551

            Append_Node_To_List
              (Make_Variable_Declaration
1552 1553 1554 1555
                 (Defining_Identifier =>
                    Make_Defining_Identifier (VN (V_Entity)),
                  Used_Type => RE (RE_Entity_T)),
               Declarations);
1556 1557 1558

            --  Add the call to entity = __po_hi_global...[port]

1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
            N :=
              Make_Expression
                (Left_Expr  => Make_Defining_Identifier (VN (V_Entity)),
                 Operator   => Op_Equal,
                 Right_Expr =>
                   Make_Call_Profile
                     (RE (RE_Get_Entity_From_Global_Port),
                      Make_List_Id
                        (Make_Member_Designator
                           (Defining_Identifier =>
                              Make_Defining_Identifier (MN (M_Port)),
                            Is_Pointer     => True,
                            Aggregate_Name =>
                              Make_Defining_Identifier (VN (V_Request))))));
1573 1574 1575 1576
            Append_Node_To_List (N, Statements);

            --  Add the switch which redirect to local deliver functions

1577
            N := Make_Switch_Alternative (No_List, No_List);
1578 1579 1580

            Append_Node_To_List (N, Main_Deliver_Alternatives);

1581 1582 1583 1584
            N :=
              Make_Switch_Statement
                (Expression   => Make_Defining_Identifier (VN (V_Entity)),
                 Alternatives => Main_Deliver_Alternatives);
1585 1586 1587

            Append_Node_To_List (N, Statements);

1588 1589 1590 1591 1592 1593 1594 1595 1596
            N :=
              Make_Doxygen_C_Comment
                (Is_Function  => True,
                 Element_Name =>
                   "void __po_hi_main_deliver " &
                   "(__po_hi_request_t* request)",
                 Brief => "Used to deliver request to the appropriate ports",
                 Desc  =>
                   "This function takes a request as argument (\arg request)" &
yoogx's avatar
yoogx committed
1597 1598 1599 1600
                   " and calls the appropriate function for its delivery." &
                   "To specify which function should be called, it " &
                   "extracts the receiver entity using the destination " &
                   "port.",
1601
                 Has_Header_Spaces => False);
1602 1603
            Append_Node_To_List (N, CTN.Declarations (Current_File));

1604 1605 1606 1607 1608 1609
            N :=
              Make_Function_Implementation
                (CTN.Job_Node
                   (Backend_Node (Identifier (Parent_Subcomponent (E)))),
                 Declarations,
                 Statements);
1610 1611 1612 1613 1614 1615 1616
            Append_Node_To_List (N, CTN.Declarations (Current_File));
         end if;

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

1617 1618 1619 1620 1621
      ---------------------------
      -- Visit_Device_Instance --
      ---------------------------

      procedure Visit_Device_Instance (E : Node_Id) is
1622 1623
         Implementation : constant Node_Id := Get_Implementation (E);
         S              : Node_Id;
1624
      begin
1625 1626
         Current_Device := E;

1627 1628 1629 1630 1631 1632 1633 1634 1635
         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;
1636 1637

         Current_Device := No_Node;
1638 1639
      end Visit_Device_Instance;

1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
      ---------------------------
      -- Visit_System_Instance --
      ---------------------------

      procedure Visit_System_Instance <