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
      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);
yoogx's avatar
yoogx committed
392
      procedure Visit_Subcomponents_Of is new Visit_Subcomponents_Of_G (Visit);
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414

      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

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

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

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
449
      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

450
451
452
453
         N :=
           Make_Parameter_Association
             (Selector_Name    => Make_Defining_Identifier (PN (P_Entity)),
              Actual_Parameter => Extract_Enumerator (E));
454
455
456
457
458
         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
459
           or else Get_Thread_Dispatch_Protocol (E) = Thread_ISR
460
461
462
         then
            --  The task period of minimal interarrival time

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

            --  The task deadline

            N := Map_Ada_Time (Get_Thread_Deadline (E));

474
475
476
477
478
            N :=
              Make_Parameter_Association
                (Selector_Name =>
                   Make_Defining_Identifier (PN (P_Task_Deadline)),
                 Actual_Parameter => N);
479
480
481
482
483
484
485
486
            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
487
488
489
490
491
               N :=
                 Make_Parameter_Association
                   (Selector_Name =>
                      Make_Defining_Identifier (PN (P_Dispatch_Offset)),
                    Actual_Parameter => Map_Ada_Time (T));
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
               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;

507
508
509
510
         N :=
           Make_Parameter_Association
             (Selector_Name => Make_Defining_Identifier (PN (P_Task_Priority)),
              Actual_Parameter => N);
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
         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;

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

         --  The task job

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

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

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

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

561
562
563
564
565
               N :=
                 Make_Parameter_Association
                   (Selector_Name =>
                      Make_Defining_Identifier (PN (P_Activate_Entrypoint)),
                    Actual_Parameter => Map_Task_Init_Identifier (E));
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
               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.

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

590
591
592
593
594
               N :=
                 Make_Parameter_Association
                   (Selector_Name =>
                      Make_Defining_Identifier (PN (P_Recover_Entrypoint)),
                    Actual_Parameter => Map_Task_Recover_Identifier (E));
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
               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

615
616
617
         N :=
           Make_Package_Instantiation
             (Defining_Identifier => Map_Task_Identifier (E),
618
619
              Generic_Package     =>
                RU (RU_PolyORB_HI_Periodic_Task, Elaborated => True),
yoogx's avatar
yoogx committed
620
              Parameter_List => Parameter_List);
621
622
623
624
625
626
627
628
629
630
631
632
633
         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

634
635
636
637
638
         N :=
           Make_Parameter_Association
             (Selector_Name    => Make_Defining_Identifier (TN (T_Port_Type)),
              Actual_Parameter =>
                Make_Defining_Identifier (Map_Port_Enumeration_Name (E)));
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
         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

666
667
668
669
670
671
         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)));
672
673
674
675
         Append_Node_To_List (N, Parameter_List);

         --  Build the package instantiation

676
677
678
         N :=
           Make_Package_Instantiation
             (Defining_Identifier => Map_Task_Identifier (E),
679
680
              Generic_Package     =>
                RU (RU_PolyORB_HI_Sporadic_Task, Elaborated => True),
yoogx's avatar
yoogx committed
681
              Parameter_List => Parameter_List);
682
683
684
685
686
687
688
689
690
691
692
693
694
         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

695
696
697
698
699
         N :=
           Make_Parameter_Association
             (Selector_Name    => Make_Defining_Identifier (TN (T_Port_Type)),
              Actual_Parameter =>
                Make_Defining_Identifier (Map_Port_Enumeration_Name (E)));
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
725
         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

726
727
728
729
730
731
         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)));
732
733
734
735
         Append_Node_To_List (N, Parameter_List);

         --  Build the package instantiation

736
737
738
         N :=
           Make_Package_Instantiation
             (Defining_Identifier => Map_Task_Identifier (E),
739
740
              Generic_Package     =>
                RU (RU_PolyORB_HI_Aperiodic_Task, Elaborated => True),
yoogx's avatar
yoogx committed
741
              Parameter_List => Parameter_List);
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
         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

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

768
769
770
771
772
773
774
775
776
777
778
779
780
781
      -----------------------------
      -- 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
782
783
784
785
786
         N :=
           Make_Package_Instantiation
             (Defining_Identifier => Map_Task_Identifier (E),
              Generic_Package     => RU (RU_PolyORB_HI_Null_Task),
              Parameter_List      => Parameter_List);
787
788
789
         return N;
      end Null_Task_Instantiation;

yoogx's avatar
yoogx committed
790
791
792
793
794
795
796
      ----------------------------
      -- 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);
797
         Configuration  : Name_Id;
yoogx's avatar
yoogx committed
798
799
800
801
802
803
804
805
806
      begin
         Configuration := Get_Configuration (E);
         if Configuration = No_Name then
            Display_Located_Error
              (Loc (E),
               "No interrupt configured",
               Fatal => True);
         end if;

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

810
811
812
813
814
815
         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
816
817
818
819
820
821
822
823
         Append_Node_To_List (N, Parameter_List);

         --  Append the common parameters

         Cyclic_Task_Instantiation_Formals (E, Parameter_List);

         --  Build the package instantiation

824
825
826
827
828
         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
829
830
831
         return N;
      end ISR_Task_Instantiation;

832
833
834
835
836
837
838
839
840
841
      -------------------------------
      -- 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

842
843
844
845
846
         N :=
           Make_Parameter_Association
             (Selector_Name    => Make_Defining_Identifier (TN (T_Port_Type)),
              Actual_Parameter =>
                Make_Defining_Identifier (Map_Port_Enumeration_Name (E)));
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
873
         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

874
875
876
877
878
879
         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)));
880
881
882
883
         Append_Node_To_List (N, Parameter_List);

         --  Build the package instantiation

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

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

      function Task_Job_Spec (E : Node_Id) return Node_Id is
         N          : Node_Id;
898
         Param_List : List_Id                                     := No_List;
899
900
901
902
903
904
905
         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
906
907
908
909
910
            N :=
              Make_Parameter_Specification
                (Defining_Identifier => Make_Defining_Identifier (PN (P_Port)),
                 Subtype_Mark        =>
                   Make_Defining_Identifier (Map_Port_Enumeration_Name (E)));
911
912
913
914

            Param_List := Make_List_Id (N);
         end if;

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

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

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

934
            --  Send_Output
935

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

939
            --  Put_Value
940

941
942
943
944
            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;
945

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

950
            --  Receive_Input
951

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

955
            --  Get_Value
956

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

961
            --  Get_Sender
962

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

966
            --  Get_Count
967

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

972
            --  Get_Time_Stamp
973

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

977
            --  Next_Value
978

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

982
983
984
985
986
987
988
989
990
            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;
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
      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
1025
1026
         Category : constant Component_Category :=
           Get_Category_Of_Component (E);
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
      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
1048
1049
1050
         U : constant Node_Id :=
           ADN.Distributed_Application_Unit
             (ADN.Deployment_Node (Backend_Node (Identifier (E))));
yoogx's avatar
yoogx committed
1051
         P                   : constant Node_Id              := ADN.Entity (U);
1052
1053
         S                   : Node_Id;
         N                   : Node_Id;
1054
         Scheduling_Protocol : Supported_Scheduling_Protocol :=
1055
1056
1057
           Get_Scheduling_Protocol (Get_Bound_Processor (E));
         The_System : constant Node_Id :=
           Parent_Component (Parent_Subcomponent (E));
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
      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
1070
              (Loc (Get_Bound_Processor (E)),
1071
1072
1073
1074
               "Undefined scheduling protocol, " &
               "will use FIFO_WITHIN_PRIORITIES",
               Fatal   => False,
               Warning => True);
1075

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

            --  XXX In case of Round robin, we should also check that
            --  the scheduler is set to non-preemptive mode.
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
         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
1103
1104
1105
1106
1107
1108
                  N :=
                    Make_Object_Declaration
                      (Defining_Identifier => Map_Ada_Defining_Identifier (S),
                       Object_Definition   =>
                         Map_Ada_Data_Type_Designator
                           (Corresponding_Instance (S)));
1109
1110
1111
1112
1113
                  Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

                  --  Link the variable and the object

                  Bind_AADL_To_Object (Identifier (S), N);
1114

yoogx's avatar
yoogx committed
1115
1116
                  if Get_Concurrency_Protocol (Corresponding_Instance (S)) =
                    Priority_Ceiling
1117
1118
1119
1120
1121
                  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
1122
1123
1124
1125
                     N :=
                       Make_Pragma_Statement
                         (Pragma_SPARK_Mode,
                          Make_List_Id (RE (RE_Off)));
1126
1127

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

1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
               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))
1150
1151
1152
                 and then
                   Get_Bound_Processor (Corresponding_Instance (S)) =
                   Get_Bound_Processor (E)
1153
               then
1154
                  Visit_Device_Instance (Corresponding_Instance (S));
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
               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
1173
1174
         Implementation : constant Node_Id := Get_Implementation (E);
         S              : Node_Id;
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
      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
      begin
         Push_Entity (Ada_Root);

         --  Visit all the subcomponents of the system

yoogx's avatar
yoogx committed
1197
         Visit_Subcomponents_Of (E);
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211

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

yoogx's avatar
yoogx committed
1213
1214
         Scheduling_Protocol : Supported_Scheduling_Protocol :=
           Unknown_Scheduler;
1215
         Process_Node : Node_Id;
1216

1217
      begin
1218
1219
         --  Determine the scheduler that controls the current thread instance

yoogx's avatar
yoogx committed
1220
         Process_Node := Get_Container_Process (Parent_Subcomponent (E));
1221
1222

         if Present (Process_Node) then
yoogx's avatar
yoogx committed
1223
1224
1225
            Scheduling_Protocol :=
              Get_Scheduling_Protocol
                (Get_Bound_Processor (Corresponding_Instance (Process_Node)));
1226
1227
         end if;

1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
         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.

1238
1239
            if No
                (Get_Handling
1240
1241
1242
1243
                   (Corresponding_Declaration (E),
                    By_Node,
                    H_Ada_Activity_Interr_Spec))
            then
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
               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))));
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
               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);

1274
1275
1276
1277
               N :=
                 Message_Comment
                   ("END: Entities used by all instances of component " &
                    Get_Name_String (Display_Name (Identifier (E))));
1278
1279
1280
1281
1282
1283
               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
1284
1285
1286
1287
1288
1289
                  Found : constant Node_Id :=
                    Get_Handling
                      (Corresponding_Declaration (E),
                       By_Node,
                       H_Ada_Activity_Interr_Spec);
                  BE : constant Node_Id := Backend_Node (Identifier (Found));
1290
1291
               begin
                  Bind_AADL_To_Port_Enumeration
1292
1293
                    (Identifier (E),
                     ADN.Port_Enumeration_Node (BE));
1294
                  Bind_AADL_To_Port_Interface
1295
1296
                    (Identifier (E),
                     ADN.Port_Interface_Node (BE));
1297
                  Bind_AADL_To_Put_Value
1298
1299
                    (Identifier (E),
                     ADN.Put_Value_Node (BE));
1300
                  Bind_AADL_To_Get_Value
1301
1302
                    (Identifier (E),
                     ADN.Get_Value_Node (BE));
1303
                  Bind_AADL_To_Get_Count
1304
1305
                    (Identifier (E),
                     ADN.Get_Count_Node (BE));
1306
                  Bind_AADL_To_Store_Received_Message
1307
1308
                    (Identifier (E),
                     ADN.Store_Received_Message_Node (BE));
1309
1310
1311
1312
1313
1314
               end;
            end if;
         end if;

         case P is
            when Thread_Periodic =>
yoogx's avatar
yoogx committed
1315
1316
1317
1318
               N :=
                 Message_Comment
                   ("Periodic task : " &
                    Get_Name_String (Display_Name (Identifier (S))));
1319
1320
1321
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

            when Thread_Sporadic =>
yoogx's avatar
yoogx committed
1322
1323
1324
1325
               N :=
                 Message_Comment
                   ("Sporadic task : " &
                    Get_Name_String (Display_Name (Identifier (S))));
1326
1327
1328
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

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

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

            when Thread_Background =>
yoogx's avatar
yoogx committed
1343
1344
1345
1346
               N :=
                 Message_Comment
                   ("Background task : " &
                    Get_Name_String (Display_Name (Identifier (S))));
1347
1348
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

yoogx's avatar
yoogx committed
1349
            when Thread_ISR =>
yoogx's avatar
yoogx committed
1350
1351
1352
1353
               N :=
                 Message_Comment
                   ("ISR task : " &
                    Get_Name_String (Display_Name (Identifier (S))));
yoogx's avatar
yoogx committed
1354
1355
               Append_Node_To_List (N, ADN.Visible_Part (Current_Package));

1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
            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);

1370
         --  For each AADL thread, we instantiate a task.
1371

1372
1373
1374
1375
1376
1377
         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
1378
            case P is
1379
1380
               when Thread_Periodic =>
                  --  Instantiate the periodic task
1381

1382
1383
                  N := Periodic_Task_Instantiation (E);
                  Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
1384

1385
1386
               when Thread_Sporadic =>
                  --  Instantiate the sporadic task
1387

1388
1389
                  N := Sporadic_Task_Instantiation (E);
                  Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
1390

1391
1392
               when Thread_Hybrid =>
                  --  Instantiate the hybrid task
1393

1394
1395
                  N := Hybrid_Task_Instantiation (E);
                  Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
1396

1397
1398
               when Thread_Aperiodic =>
                  --  Instantiate the aperiodic task
1399

1400
1401
                  N := Aperiodic_Task_Instantiation (E);
                  Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
1402

yoogx's avatar
yoogx committed
1403
               when Thread_Background =>
1404
                  --  Instantiate the background task
1405

1406
1407
                  N := Background_Task_Instantiation (E);
                  Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
1408

1409
1410
               when Thread_ISR =>
                  --  Instantiate the ISR task
yoogx's avatar
yoogx committed
1411

1412
1413
                  N := ISR_Task_Instantiation (E);
                  Append_Node_To_List (N, ADN.Visible_Part (Current_Package));
yoogx's avatar
yoogx committed
1414

1415
1416
1417
1418
               when others =>
                  raise Program_Error;
            end case;
         end if;
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440

         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
1441
1442
1443
1444
1445
1446
1447
                  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));
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476

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

1477
1478
1479
1480
1481
         N :=
           Make_Full_Type_Declaration
             (Defining_Identifier =>
                Make_Defining_Identifier (Map_Modes_Enumeration_Name (E)),
              Type_Definition => Make_Enumeration_Type_Definition (Enum_List));
1482
1483
1484
1485
1486
1487
1488
1489

         return N;
      end Make_Modes_Enumeration;

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

1490
      function Make_Mode_Updater_Spec (E : Node_Id) return Node_Id is
1491
1492
         N : Node_Id;
      begin
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
         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);
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
         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);
yoogx's avatar
yoogx committed
1526
      procedure Visit_Subcomponents_Of is new Visit_Subcomponents_Of_G (Visit);
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542

      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

1543
1544
      Has_Hybrid_Threads       : Boolean            := False;
      Hybrid_Thread_Elements   : List_Id            := No_List;
1545
1546
      Last_Hybrid_Thread_Index : Unsigned_Long_Long := 0;

1547
      Current_Mode_Identifier : Node_Id;
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570

      --  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;
1571
         RR     : Runtime_Routine) return Name_Id;
1572
1573
1574
1575
      --  Code factorization between the two subprograms below

      function Get_List
        (Thread : Node_Id;
1576
         RR     : Runtime_Routine) return List_Id;
1577
1578
1579
      --  Return the List_Id corresponding to the runtime routine 'RR'
      --  and associated to 'Thread'.

1580
      procedure Set_List (Thread : Node_Id; RR : Runtime_Routine; L : List_Id);
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
      --  Set a new value (L) to the list corresponding to the runtime
      --  routine 'RR' and associated to 'Thread'.

      Interrogation_Routine_List : List_Id;
      --  This list will hold all the bodies declarations of the
      --  interrogation routines. We do this to ensure all the bodies
      --  are appended after all entities generated for threads since
      --  they need visibility on these entities.

      -------------------
      -- Task_Job_Body --
      -------------------

      function Task_Job_Body (E : Node_Id) return Node_Id is
1595
1596
1597
         S    : constant Node_Id := Parent_Subcomponent (E);
         Spec : constant Node_Id :=
           ADN.Job_Node (Backend_Node (Identifier (S)));
1598
1599
1600
1601
         Declarations : constant List_Id := New_List (ADN.K_Declaration_List);
         Statements   : constant List_Id := New_List (ADN.K_Statement_List);
         P            : constant Supported_Thread_Dispatch_Protocol :=
           Get_Thread_Dispatch_Protocol (E);
1602
         Impl_Kind : constant Supported_Thread_Implementation :=
1603
1604
1605
           Get_Thread_Implementation_Kind (E);
         Need_Error_Initialization : Boolean := True;

1606
         function Get_Fully_Qualified_Subprogram (S : Name_Id) return Node_Id;
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
         --  Return an identifier to S whose parent unit name is the
         --  instantiated package correspodning to the interface of E.

         function Make_Get_Valid_Value (F : Node_Id) return Node_Id;
         --  This function generated an If statement that tests
         --  whether the port ever received a value. In this case, it
         --  returns tha last received value. Otherwithe, it return
         --  the default value for the port data type.

         --------------------------------------------------------------
         -- All routines below do NOT perfom any verification on the --
         -- thread and rely completely on the good faith of their    --
         -- caller.                                                  --
         --------------------------------------------------------------

         procedure Make_Mode_Update;
         --  Generate a case statement that updates the thread mode
         --  depending on the received event port. The event port that
         --  causes the mode switch is dequeued.

         procedure Make_Fetch_In_Ports;
         --  Generate the routines to fetch the values of the thread
         --  IN ports in a non-blocking way.

         procedure Make_Fetch_In_Ports
1632
           (Statements   : List_Id;
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
            Declarations : List_Id);
         --  Generate the routines to fetch the values of the thread
         --  IN ports in a non-blocking way, puting the result in
         --  the parameters.

         procedure Make_Dequeue_In_Ports;
         --  Generate the routines to dequeue the oldest values of the
         --  thread IN ports in a non-blocking way.

         procedure Make_Dequeue_In_Ports
1643
           (Statements   : List_Id;
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
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
            Declarations : List_Id);
         --  Generate the routines to dequeue the oldest values of the
         --  thread IN ports in a non-blocking way, puting the results
         --  in the parameters.

         procedure Make_Call_Sequence (CS : Node_Id := No_Node);
         --  Generate code relying on the thread call sequence

         procedure Make_Thread_Compute_Entrypoint;
         --  Generate code relying on the thread's own compute
         --  entrypoint.

         procedure Make_Ports_Compute_Entrypoint;
         --  Generate code relying on the compute entrypoints of the
         --  thread ports.

         procedure Make_Set_Call_Sequence_Out_Ports
           (CS         : Node_Id;
            Statements : List_Id);
         --  Generate the routines to set the values of the thread OUT
         --  ports used by the call sequence CS.

         procedure Make_Send_Call_Sequence_Out_Ports
           (CS         : Node_Id;
            Statements : List_Id);
         --  Generate the routines to send the values of the thread
         --  OUT ports from teh call sequence CS.

         procedure Make_Set_Out_Ports;
         --  Generate the routines to set the values of the thread
         --  OUT ports.

         procedure Make_Send_Out_Ports;
         --  Generate the routines to send the values of the thread
         --  OUT ports.

         procedure Create_Call_Sequence
           (Stats : List_Id;
            Decl  : List_Id;
            CS    : Node_Id := No_Node;
            Port  : Node_Id := No_Node);

         ------------------------------------
         -- Get_Fully_Qualified_Subprogram --
         ------------------------------------

         function Get_Fully_Qualified_Subprogram
1691
           (S : Name_Id) return Node_Id
1692
         is
1693
1694
            P : constant Node_Id :=
              Make_Defining_Identifier (Map_Interrogators_Name (E));
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
            N : constant Node_Id := Make_Defining_Identifier (S);
         begin
            Set_Homogeneous_Parent_Unit_Name (N, P);

            return N;
         end Get_Fully_Qualified_Subprogram;

         --------------------------
         -- Make_Get_Valid_Value --
         --------------------------

         function Make_Get_Valid_Value (F : Node_Id) return Node_Id is
            Then_Statements : constant List_Id :=
              New_List (ADN.K_Statement_List);
            Else_Statements : constant List_Id :=
              New_List (ADN.K_Statement_List);
            Condition : Node_Id;
            N         : Node_Id;
         begin
            --  The condition of validity is that the return value of
            --  Get_Count is different from -1.

1717
1718
1719
1720
            N :=
              Make_Subprogram_Call
                (Get_Fully_Qualified_Subprogram (SN (S_Get_Count)),
                 Make_List_Id (Map_Ada_Defining_Identifier (F)));
1721

1722