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-2011, European Space Agency (ESA).           --
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
--                                                                          --
-- Ocarina  is free software;  you  can  redistribute  it and/or  modify    --
-- it under terms of the GNU General Public License as published by the     --
-- Free Software Foundation; either version 2, or (at your option) any      --
-- later version. 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. See the GNU General --
-- Public License for more details. You should have received  a copy of the --
-- GNU General Public License distributed with Ocarina; see file COPYING.   --
-- If not, write to the Free Software Foundation, 51 Franklin Street, Fifth --
-- Floor, Boston, MA 02111-1301, USA.                                       --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable to be   --
-- covered  by the  GNU  General  Public  License. This exception does not  --
-- however invalidate  any other reasons why the executable file might be   --
-- covered by the GNU Public License.                                       --
--                                                                          --
--                 Ocarina is maintained by the Ocarina team                --
--                       (ocarina-users@listes.enst.fr)                     --
--                                                                          --
------------------------------------------------------------------------------

with Namet;

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

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

68
   Send_Output_Specification : Node_Id;
jdelange's avatar
jdelange committed
69
   Current_Device            : Node_Id := No_Node;
70

71
72
73
74
75
76
77
78
79
80
   ------------
   -- 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);
81
      procedure Visit_Device_Instance (E : Node_Id);
82
83
84
85
      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;

86
87
      Have_Main_Deliver          : Boolean := False;
      Has_Send_Output_Declared   : Boolean := False;
88
89
90
91
92
93
94
95
96
97

      -------------------
      -- 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
         N := Make_Function_Specification
jdelange's avatar
jdelange committed
98
           (Defining_Identifier => Map_Task_Job_Identifier (S, Current_Device),
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
            Parameters          => No_List,
            Return_Type         => CTU.Make_Pointer_Type
              (New_Node (CTN.K_Void)));
         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
         N := Make_Parameter_Specification
           (Defining_Identifier =>
              Make_Defining_Identifier (PN (P_Request)),
            Parameter_Type =>
              Make_Pointer_Type
              (RE (RE_Request_T)));
         Append_Node_To_List (N, Parameters);

         N := Make_Function_Specification
           (Defining_Identifier => Map_Task_Deliver_Identifier (S),
            Parameters          => Parameters,
            Return_Type         => New_Node (CTN.K_Void));

         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
         Category : constant Component_Category
           := Get_Category_Of_Component (E);
      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
         U          : constant Node_Id := CTN.Distributed_Application_Unit
           (CTN.Naming_Node (Backend_Node (Identifier (E))));
         P          : constant Node_Id := CTN.Entity (U);
         Parameters : constant List_Id := New_List (CTN.K_Parameter_List);
         S          : Node_Id;
         N          : Node_Id;
193
194
         The_System : constant Node_Id := Parent_Component
           (Parent_Subcomponent (E));
195
196
197
198
199
      begin
         Push_Entity (P);
         Push_Entity (U);
         Set_Activity_Header (U);

200
201
         Have_Main_Deliver          := False;
         Has_Send_Output_Declared   := False;
202
203
204
205
206
207
208
209
210
211
212
213
214
215

         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
            N := Make_Parameter_Specification
              (Defining_Identifier =>
216
                 Make_Defining_Identifier (PN (P_Request)),
217
218
               Parameter_Type =>
                 Make_Pointer_Type
219
                 (RE (RE_Request_T)));
220
221
222
223
224
225
226
227
228
229
230
231
232
            Append_Node_To_List (N, Parameters);

            N := Make_Function_Specification
              (Defining_Identifier => RE (RE_Main_Deliver),
               Parameters          => Parameters,
               Return_Type         => New_Node (CTN.K_Void));

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

            Bind_AADL_To_Job
              (Identifier (Parent_Subcomponent (E)), N);
         end if;

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
         --  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))
               and then
                 Get_Bound_Processor (Corresponding_Instance (S))
                 = Get_Bound_Processor (E)
               then
                  Visit_Device_Instance
                    (Corresponding_Instance (S));
               end if;
               S := Next_Node (S);
            end loop;
         end if;

251
252
253
254
         Pop_Entity; -- U
         Pop_Entity; -- P
      end Visit_Process_Instance;

255
256
257
258
259
260
261
262
      ---------------------------
      -- Visit_Device_Instance --
      ---------------------------

      procedure Visit_Device_Instance (E : Node_Id) is
         Implementation  : constant Node_Id := Get_Implementation (E);
         S : Node_Id;
      begin
jdelange's avatar
jdelange committed
263
         Current_Device := E;
264
265
266
267
268
269
270
271
272
         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
273
         Current_Device := No_Node;
274
275
      end Visit_Device_Instance;

276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
      ---------------------------
      -- 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
         P : constant Supported_Thread_Dispatch_Protocol :=
           Get_Thread_Dispatch_Protocol (E);
         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);
316
317
318
319
320
321
322
323
324
325

            N := Task_Deliver_Spec (E);

            if Has_Output_Ports (E) and then
               Has_Send_Output_Declared = False then
               Send_Output_Specification := Make_Function_Specification
                 (Defining_Identifier => RE (RE_Send_Output),
                  Parameters          => Make_List_Id
                     (Make_Parameter_Specification
                        (Defining_Identifier =>
326
327
                           Make_Defining_Identifier (PN (P_Task)),
                        Parameter_Type => RE (RE_Task_Id)),
328
329
330
331
                     Make_Parameter_Specification
                        (Defining_Identifier =>
                           Make_Defining_Identifier (PN (P_Port)),
                        Parameter_Type => RE (RE_Port_T))),
332
                  Return_Type         => New_Node (CTN.K_Int));
333
334
335
336
337
338
               Append_Node_To_List
                  (Send_Output_Specification,
                  CTN.Declarations (Current_File));

               Has_Send_Output_Declared := True;
            end if;
339
340
341
342
343
344
345
346
347
         end if;

         case P is
            when Thread_Periodic =>
               N := Message_Comment
                 ("Periodic task : "
                  & Get_Name_String (Display_Name (Identifier (S))));
               Append_Node_To_List (N, CTN.Declarations (Current_File));

348
349
350
351
352
353
            when Thread_Background =>
               N := Message_Comment
                 ("Background task : "
                  & Get_Name_String (Display_Name (Identifier (S))));
               Append_Node_To_List (N, CTN.Declarations (Current_File));

354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
            when Thread_Sporadic =>
               N := Message_Comment
                 ("Sporadic task : "
                  & Get_Name_String (Display_Name (Identifier (S))));
               Append_Node_To_List (N, CTN.Declarations (Current_File));

            when Thread_Aperiodic =>
               N := Message_Comment
                 ("Aperiodic task : "
                  & Get_Name_String (Display_Name (Identifier (S))));
               Append_Node_To_List (N, CTN.Declarations (Current_File));

            when others =>
               Display_Error ("Unsupported thread type " & P'Img,
                              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, 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);
392
      procedure Visit_Device_Instance (E : Node_Id);
393
394
395
396
397
398
      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.

399
400
401
402
403
      Main_Deliver_Alternatives     : List_Id;
      Has_Send_Output_Declared      : Boolean := False;
      Send_Output_Statements        : List_Id;
      Send_Output_Declarations      : List_Id;
      Send_Output_Alternatives      : List_Id;
404

405
406
      Current_Device : Node_Id := No_Node;

407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
      -------------------
      -- Task_Job_Body --
      -------------------

      function Task_Job_Body (E : Node_Id) return Node_Id is
         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 :=
           Get_Thread_Dispatch_Protocol (E);
         Impl_Kind       : constant Supported_Thread_Implementation :=
           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;
         procedure Make_Thread_Compute_Entrypoint;
         procedure Make_Ports_Compute_Entrypoint;
435
         procedure Make_Activate_Entrypoint;
436
437
         function Make_Get_Valid_Value (F : Node_Id) return Node_Id;

438
439
440
         ------------------------------
         -- Make_Activate_Entrypoint --
         ------------------------------
441

442
         procedure Make_Activate_Entrypoint is
443
            Entrypoint : constant Node_Id
444
                  := Get_Thread_Activate_Entrypoint (E);
445
446
447
448
449
450
451
         begin
            if Entrypoint /= No_Node then
               Append_Node_To_List
                  (Make_Call_Profile
                     (Map_C_Subprogram_Identifier (Entrypoint)),
                  Statements);
            end if;
452
         end Make_Activate_Entrypoint;
453

454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
         --------------------------
         -- 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
            N := Make_Variable_Declaration
              (Defining_Identifier => Make_Defining_Identifier
                 (Map_C_Variable_Name (F, Port_Request => True)),
               Used_Type   => RE (RE_Request_T));
            Append_Node_To_List (N, Declarations);

            Call_Parameters := New_List (CTN.K_Parameter_List);
            Append_Node_To_List
              (Make_Defining_Identifier
               (Map_C_Enumerator_Name (S)),
               Call_Parameters);
            Append_Node_To_List
              (Make_Defining_Identifier
               (Map_C_Enumerator_Name (F, Local_Port => True)),
               Call_Parameters);

            if not Get_Wait_For_All_Events (E) then
               Condition := Make_Call_Profile
                  (RE (RE_Gqueue_Get_Count), Call_Parameters);
            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
              (Make_Defining_Identifier
               (Map_C_Enumerator_Name (S)),
               Call_Parameters);

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

            Append_Node_To_List
              (Make_Variable_Address
               (Make_Defining_Identifier
                (Map_C_Variable_Name (F, Port_Request => True))),
                Call_Parameters);

            N := Make_Call_Profile
              (RE (RE_Gqueue_Get_Value), Call_Parameters);

            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
              (Make_Defining_Identifier
               (Map_C_Enumerator_Name (S)),
               Call_Parameters);

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

            N := Make_Call_Profile
              (RE (RE_Gqueue_Next_Value), Call_Parameters);

            Append_Node_To_List (N, Then_Statements);

            return
              Make_If_Statement
              (Condition, Then_Statements);
         end Make_Get_Valid_Value;

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

         procedure Make_Wait_Event is
         begin
            N := Make_Variable_Declaration
              (Defining_Identifier =>
                 Make_Defining_Identifier (VN (V_Port)),
               Used_Type   => RE (RE_Local_Port_T));
            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);

            N := Make_Variable_Address
              (Make_Defining_Identifier (VN (V_Port)));
            Append_Node_To_List (N, Call_Parameters);

            N := CTU.Make_Call_Profile (RE (RE_Gqueue_Wait_For_Incoming_Event),
                                        Call_Parameters);
            Append_Node_To_List (N, WStatements);

            --  Make the call to __po_hi_compute_next_period

            Call_Parameters := New_List (CTN.K_Parameter_List);
565
566
567
568
569
            N := Make_Defining_Identifier
               (Map_C_Enumerator_Name
                  (S,
                  Custom_Parent => Current_Device));

570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
            Append_Node_To_List (N, Call_Parameters);

            N := CTU.Make_Call_Profile (RE (RE_Compute_Next_Period),
                                        Call_Parameters);
            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);
586
587
588
589
590
591
592
593
            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;
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
            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, WStatements);
         end Make_Task_Blocking;

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

         procedure Make_Set_Out_Ports is
            N : Node_Id;
            L : Node_Id;
            R : Node_Id;
            F : Node_Id;
            T : Node_Id;
            Z : Node_Id;
            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
               if Kind (F) = K_Port_Spec_Instance and then Is_Out (F) then
                  --  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
                        N := Make_Variable_Declaration
                          (Make_Defining_Identifier
                           (Map_C_Variable_Name
                            (F, Port_Request => True)),
                           RE (RE_Request_T));
                        Append_Node_To_List (N, Declarations);

                        L := 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)));
                        R :=  Make_Defining_Identifier
                                 (Map_C_Variable_Name
                                    (F, Request_Variable => True));

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

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

                        if T /= No_Node and then
                           CTN.Kind (T) = CTN.K_Array_Declaration then
                           Parameters := New_List (CTN.K_Parameter_List);
                           Append_Node_To_List (L, Parameters);
                           Append_Node_To_List (R, Parameters);
                           Append_Node_To_List
                              (Get_Data_Size (Z), Parameters);
                           N := CTU.Make_Call_Profile
                                 (RE (RE_Copy_Array), Parameters);
                        else
                           N := Make_Expression
                              (Left_Expr => L,
                              Operator => Op_Equal,
                              Right_Expr => R);
                        end if;

                        Append_Node_To_List
                          (N, WStatements);

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

                        Call_Parameters := New_List (CTN.K_Parameter_List);
                        N := Make_Defining_Identifier
718
                          (Map_C_Enumerator_Name (S, Current_Device));
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
                        Append_Node_To_List (N, Call_Parameters);

                        N := Make_Defining_Identifier
                          (Map_C_Enumerator_Name (F, Local_Port => True));
                        Append_Node_To_List (N, Call_Parameters);

                        N := Make_Variable_Address
                          (Make_Defining_Identifier
                           (Map_C_Variable_Name (F, Port_Request => True)));
                        Append_Node_To_List (N, Call_Parameters);

                        N := CTU.Make_Call_Profile (RE (RE_Gqueue_Store_Out),
                                                    Call_Parameters);

                        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
747
748
            N                       : Node_Id;
            F                       : Node_Id;
749
750
751
            Used_Bus                : Node_Id;
            Used_Device             : Node_Id;
            Send_Function_Node      : Node_Id;
752
753
            Send_Alternative_Label  : List_Id;
            Send_Alternative_Stmts  : List_Id;
754
            Send_Function_Name      : Name_Id := No_Name;
755
            Error_Already_Defined   : Boolean := False;
756
757
758
759
760
761
762
763
         begin
            N := Message_Comment ("Send the OUT ports");
            Append_Node_To_List (N, WStatements);

            F := First_Node (Features (E));

            while Present (F) loop
               if Kind (F) = K_Port_Spec_Instance and then Is_Out (F) then
764
765
                  Has_Send_Output_Declared := True;

766
767
768
                  Send_Alternative_Label := New_List (CTN.K_Label_List);
                  Send_Alternative_Stmts := New_List (CTN.K_Statement_List);

769
770
771
772
773
                  Append_Node_To_List
                    (Make_Defining_Identifier
                     (Map_C_Enumerator_Name (F)),
                     Send_Alternative_Label);

774
775
776
777
778
779
780
781
782
783
784
785
786
                  Used_Bus := Get_Associated_Bus (F);

                  if Used_Bus /= No_Node then

                     if AAU.Is_Virtual_Bus (Used_Bus) then
                        Used_Bus := Parent_Component
                           (Parent_Subcomponent (Used_Bus));
                     end if;

                     Used_Device := Get_Device_Of_Process
                           (Used_Bus,
                              (Parent_Component
                                 (Parent_Subcomponent (E))));
julien.delange's avatar
julien.delange committed
787
788
789
790
                     if Used_Device /= No_Node then
                        Send_Function_Name := Get_Send_Function_Name
                           (Corresponding_Instance (Used_Device));
                     end if;
791
792
793
                  end if;

                  if Send_Function_Name /= No_Name then
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
                     Append_Node_To_List
                      (Make_Function_Specification
                       (Defining_Identifier =>
                        Make_Defining_Identifier (Send_Function_Name),
                        Parameters          =>
                           Make_List_Id
                              (Make_Parameter_Specification
                               (Defining_Identifier =>
                                Make_Defining_Identifier (PN (P_Task)),
                                Parameter_Type => RE (RE_Task_Id)),
                               Make_Parameter_Specification
                               (Defining_Identifier =>
                                Make_Defining_Identifier (PN (P_Port)),
                                Parameter_Type => RE (RE_Port_T))),
                        Return_Type         => New_Node (CTN.K_Int)),
                     CTN.Declarations (Current_File));

811
812
813
814
815
816
                     Send_Function_Node :=
                        Make_Defining_Identifier (Send_Function_Name);
                  else
                     Send_Function_Node := RE (RE_Transport_Send_Default);
                  end if;

817
                  Append_Node_To_List
818
819
                     (Make_Return_Statement
                        (CTU.Make_Call_Profile
820
                           (Send_Function_Node,
821
                           Make_List_Id
822
                              (Make_Defining_Identifier (PN (P_Task)),
823
                              Make_Defining_Identifier (PN (P_Port))))),
824
825
826
827
828
829
830
831
832
833
                     Send_Alternative_Stmts);

                  N := Make_Switch_Alternative
                    (Send_Alternative_Label, Send_Alternative_Stmts);
                  Append_Node_To_List (N, Send_Output_Alternatives);

                  --  Generate appropriate source code to deliver
                  --  the message when we send it.

                  --  Then, call the send_output in the main loop.
834
835
                  Call_Parameters := New_List (CTN.K_Parameter_List);
                  N := Make_Defining_Identifier
836
                    (Map_C_Enumerator_Name (S, Current_Device));
837
838
839
840
841
842
                  Append_Node_To_List (N, Call_Parameters);

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

843
                  N := CTU.Make_Call_Profile (RE (RE_Send_Output),
844
                                              Call_Parameters);
845

846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
                  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
                             (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);
                  else
                     N := Make_Assignment_Statement
                       (Variable_Identifier =>
                          Make_Defining_Identifier (VN (V_Error)),
                        Expression         => N);
                     Append_Node_To_List (N, WStatements);

                     if not Error_Already_Defined then
                        N := Make_Variable_Declaration
                          (Defining_Identifier => Make_Defining_Identifier
                             (VN (V_Error)),
                           Used_Type           => RE (RE_Int32_T));
                        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
                           N := Make_Extern_Entity_Declaration
                             (Make_Function_Specification
                                (Make_Defining_Identifier (Rec_Entrypoint),
                                 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
                             (Make_Defining_Identifier (Rec_Entrypoint),
                              No_List);
                           Append_Node_To_List (N, Then_Statements);

                           N := Make_If_Statement
                             (Condition =>
                                Make_Expression
                                (Make_Defining_Identifier (VN (V_Error)),
                                 Op_Not_Equal,
                                 RE (RE_SUCCESS)),
                              Statements => Then_Statements);
                           Append_Node_To_List (N, WStatements);
                        end if;
                     end;

                  end if;
               end if;

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

         -------------------------
         -- Make_Fetch_In_Ports --
         -------------------------

         procedure Make_Fetch_In_Ports is
            F : Node_Id;
         begin
            N := Message_Comment
              ("Get the IN ports values");
            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
                    (Make_Defining_Identifier
                     (Map_C_Enumerator_Name (S)),
                     Call_Parameters);

                  Append_Node_To_List
                    (Make_Defining_Identifier
                     (Map_C_Enumerator_Name (F)),
                     Call_Parameters);

                  N := Make_Call_Profile
                    (RE (RE_Gqueue_Next_Value), Call_Parameters);

               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
jdelange's avatar
jdelange committed
972
                 (S, Call_Seq, Declarations, WStatements, Current_Device);
973
               CTU.Simulate_WCET (E, Declarations, WStatements);
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
            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);
                  Switch_Labels := New_List (CTN.K_Label_List);

                  --  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
                       (Make_Defining_Identifier
                        (Map_C_Enumerator_Name (S)),
                        Call_Parameters);

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

                     N := Make_Call_Profile
                       (RE (RE_Gqueue_Next_Value), Call_Parameters);

                     Append_Node_To_List (N, Switch_Statements);
                  end if;

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

                  declare
                     Parameter_List : constant List_Id :=
                       New_List (CTN.K_List_Id);
                  begin
                     N := Make_Parameter_Specification
                       (Make_Defining_Identifier (PN (P_Self)),
                        Parameter_Type => RE (RE_Task_Id));
                     Append_Node_To_List (N, Parameter_List);

                     if AAN.Is_Data (F) then
                        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)));

                        Append_Node_To_List
                          (N, Call_Parameters);

                        N := Map_C_Data_Type_Designator
                          (Corresponding_Instance (F));
                        N := Make_Parameter_Specification
                          (Map_C_Defining_Identifier (F), N);
                        Append_Node_To_List (N, Parameter_List);
                     end if;

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

                  N := Make_Call_Profile
                    (Map_C_Subprogram_Identifier (F), Call_Parameters);
                  Append_Node_To_List (N, Switch_Statements);

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

                  N := Make_Switch_Alternative
                    (Switch_Labels, Switch_Statements);
                  Append_Node_To_List (N, Switch_Alternatives);
               end if;

               F := Next_Node (F);
            end loop;

            N := Make_Switch_Alternative
              (No_List, No_List);
            Append_Node_To_List (N, Switch_Alternatives);

            --  Make the case statement

            N := Make_Switch_Statement
              (Expression =>
                 Make_Defining_Identifier (MN (M_Port)),
               Alternatives =>
                 Switch_Alternatives);
            Append_Node_To_List (N, WStatements);
         end Make_Ports_Compute_Entrypoint;

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

         procedure Make_Thread_Compute_Entrypoint is
            N : Node_Id;
            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);
            N := Make_Defining_Identifier
              (Map_C_Enumerator_Name (S));
            Append_Node_To_List (N, Call_Parameters);

            N := Make_Parameter_Specification
              (Make_Defining_Identifier (PN (P_Self)),
               Parameter_Type => RE (RE_Task_Id));
            Append_Node_To_List (N, Parameter_List);

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

               N := Make_Parameter_Specification
                 (Make_Defining_Identifier (VN (V_Port)),
                  Parameter_Type => RE (RE_Port_T));
               Append_Node_To_List (N, Parameter_List);
            end if;

            if not Has_Modes (E) then
               N := Make_Extern_Entity_Declaration
                 (Make_Function_Specification
                    (Map_C_Subprogram_Identifier (E),
                     Parameters          => Parameter_List,
                     Return_Type         => New_Node (CTN.K_Void)));

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

               N := Make_Call_Profile
                 (Map_C_Subprogram_Identifier (E),
                  Call_Parameters);
               Append_Node_To_List (N, WStatements);
            else
               Display_Located_Error
                 (Loc (E),
                  "Threads with mode controlled compute entrypoints not"
                  & " supported yet",
                  Fatal => True);
            end if;
         end Make_Thread_Compute_Entrypoint;

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

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

1183
1184
1185
1186
1187
1188
            when Thread_Background =>
               N := Message_Comment
                 ("Background task : "
                    & Get_Name_String (Display_Name (Identifier (S))));
               Append_Node_To_List (N, CTN.Declarations (Current_File));

1189
1190
1191
1192
1193
            when others =>
               Display_Error ("unknown type of thread", Fatal => False);
               null;
         end case;

1194
         Make_Activate_Entrypoint;
1195

1196
1197
1198
1199
1200
1201
1202
1203
1204
         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
1205
               (Map_C_Enumerator_Name (S, Current_Device)),
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
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
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
               (Map_C_Define_Name (S, Nb_Ports => True)),
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
               (Map_C_Variable_Name (S, Port_Queue => True)),
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
               (Map_C_Variable_Name (S, Port_Fifo_Size => True)),
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
               (Map_C_Variable_Name (S, Port_First => True)),
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
               (Map_C_Variable_Name (S, Port_Offsets => True)),
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
               (Map_C_Variable_Name (S, Port_Woffsets => True)),
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
               (Map_C_Variable_Name (S, Port_N_Dest => True)),
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
               (Map_C_Variable_Name (S, Port_Destinations => True)),
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
               (Map_C_Variable_Name (S, Port_Used_Size => True)),
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
               (Map_C_Variable_Name (S, Port_History => True)),
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
               (Map_C_Variable_Name (S, Port_Recent => True)),
               Call_Parameters);

            Append_Node_To_List
              (Make_Defining_Identifier
               (Map_C_Variable_Name (S, Port_Empties => True)),
               Call_Parameters);

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

            N := Make_Call_Profile (RE (RE_Gqueue_Init),
                                    Call_Parameters);
            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;

         --  If an initialize entrypoint has been specified for the
         --  thread, add a call to the corresponding function before
         --  initialization of the runtime is done.

         declare
            Init_Entrypoint : constant Name_Id :=
1371
              Get_Thread_Activate_Entrypoint (E);
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
            Parameter_List : constant List_Id :=
              New_List (CTN.K_List_Id);
         begin
            if Init_Entrypoint /= No_Name then
               N := Make_Extern_Entity_Declaration
                 (Make_Function_Specification
                    (Make_Defining_Identifier (Init_Entrypoint),
                     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
                 (Make_Defining_Identifier (Init_Entrypoint),
                  No_List);
               Append_Node_To_List (N, Statements);
            end if;
         end;

1390
1391
1392
1393
1394
         N := Make_Doxygen_C_Comment
            ("Waiting for other tasks initialization",
               Has_Header_Spaces => False);
         Append_Node_To_List (N, Statements);

1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
         --  Call __po_hi_wait_initialization

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

         --  Compute the next period after initialization, because
         --  the period has passed after init.

         if P = Thread_Periodic or else P = Thread_Sporadic then
            N := CTU.Make_Call_Profile (RE (RE_Compute_Next_Period),
                                        Make_List_Id
                                          (Make_Defining_Identifier
1408
1409
                                             (Map_C_Enumerator_Name
                                                (S, Current_Device))));
1410
1411
1412
            Append_Node_To_List (N, Statements);
         end if;

1413
1414
         if P /= Thread_Background then
            --  Make the while (1){} and add all statements
1415

1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
            N := Make_While_Statement
              (Make_Literal (CV.New_Int_Value (1, 0, 10)),
               WStatements);
            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);
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
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
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492

         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
         Category : constant Component_Category
           := Get_Category_Of_Component (E);
      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
         U            : constant Node_Id := CTN.Distributed_Application_Unit
           (CTN.Naming_Node (Backend_Node (Identifier (E))));
         P            : constant Node_Id := CTN.Entity (U);
         S            : Node_Id;
         N            : Node_Id;
         Declarations : constant List_Id := New_List
           (CTN.K_Declaration_List);
         Statements   : constant List_Id := New_List (CTN.K_Statement_List);
1493
1494
         The_System : constant Node_Id := Parent_Component
           (Parent_Subcomponent (E));
1495
1496
1497
1498
1499
1500
1501
      begin
         Push_Entity (P);
         Push_Entity (U);
         CTU.Set_Activity_Source (U);

         Main_Deliver_Alternatives := New_List (CTN.K_Alternatives_List);

1502
1503
1504
1505
1506
         Has_Send_Output_Declared      := False;
         Send_Output_Statements        := New_List (CTN.K_Statement_List);
         Send_Output_Declarations      := New_List (CTN.K_Declaration_List);
         Send_Output_Alternatives      := New_List (CTN.K_Alternatives_List);

1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
         --  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

                  N := Make_Variable_Declaration
                    (Map_C_Defining_Identifier (S),
                     Map_C_Data_Type_Designator
hugues.jerome's avatar
hugues.jerome committed
1518
                       (Corresponding_Instance (S)));
1519
1520

                  Append_Node_To_List
1521
                    (Make_Extern_Entity_Declaration (N),
1522
1523
1524
                     CTN.Declarations (Current_File));

                  Bind_AADL_To_Object (Identifier (S), N);
1525
1526
1527
1528
               end if;
               S := Next_Node (S);
            end loop;
         end if;
1529

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

1533
            while Present (S) loop
1534

1535
               if not AAU.Is_Data (Corresponding_Instance (S)) then
1536
1537
1538
1539
1540
1541
                  Visit (Corresponding_Instance (S));
               end if;
               S := Next_Node (S);
            end loop;
         end if;

1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
         --  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))
               and then
                 Get_Bound_Processor (Corresponding_Instance (S))
                 = Get_Bound_Processor (E)
               then
                  Visit_Device_Instance
                    (Corresponding_Instance (S));
               end if;
               S := Next_Node (S);
            end loop;
         end if;

1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
         if Present (Backend_Node (Identifier (Parent_Subcomponent
                        (E)))) and then
            Present (CTN.Job_Node (Backend_Node (Identifier
               (Parent_Subcomponent (E))))) then

            Append_Node_To_List
              (Make_Variable_Declaration
               (Defining_Identifier =>
                  Make_Defining_Identifier (VN (V_Entity)),
                Used_Type =>
                  RE (RE_Entity_T)), Declarations);

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

            N := Make_Expression
              (Left_Expr => Make_Defining_Identifier (VN (V_Entity)),
               Operator => Op_Equal,
               Right_Expr =>
1578
1579
1580
1581
                Make_Call_Profile
                 (RE (RE_Get_Entity_From_Global_Port),
                  Make_List_Id
                   (Make_Member_Designator
1582
                    (Defining_Identifier =>
1583
                     Make_Defining_Identifier (MN (M_Port)),
1584
                     Is_Pointer => True,
1585
                     Aggregate_Name =>
1586
                     Make_Defining_Identifier (VN (V_Request))))));
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
            Append_Node_To_List (N, Statements);

            --  Add the switch which redirect to local deliver functions

            N := Make_Switch_Alternative
              (No_List, No_List);

            Append_Node_To_List (N, Main_Deliver_Alternatives);

            N := Make_Switch_Statement
              (Expression =>
                 Make_Defining_Identifier (VN (V_Entity)),
               Alternatives => Main_Deliver_Alternatives);

            Append_Node_To_List (N, Statements);

            N := Make_Function_Implementation
              (CTN.Job_Node
               (Backend_Node (Identifier (Parent_Subcomponent (E)))),
               Declarations,
               Statements);
            Append_Node_To_List (N, CTN.Declarations (Current_File));
         end if;

1611
1612
1613
1614
         --  If some threads are connected to other nodes, we declare
         --  a send_output function that is responsible to send
         --  data over the network using device drivers functions.

1615
1616
1617
1618
         if Has_Send_Output_Declared then
            Append_Node_To_List
               (Make_Switch_Alternative
                  (No_List,
1619
1620
1621
                  Make_List_Id
                     (Make_Return_Statement
                        (CTU.Make_Call_Profile
julien.delange's avatar
julien.delange committed
1622
                           (RE (RE_Transport_Send_Default),
1623
                           Make_List_Id
1624
                              (Make_Defining_Identifier (PN (P_Task)),
1625
                              Make_Defining_Identifier (PN (P_Port))))))),
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
               Send_Output_Alternatives);

            Append_Node_To_List
               (Message_Comment
                  ("By default, we try to use the default protocol"),
               Send_Output_Alternatives);

            Append_Node_To_List
               (Make_Switch_Statement
                  (Expression    => Make_Defining_Identifier (VN (V_Port)),
                  Alternatives   => Send_Output_Alternatives),
               Send_Output_Statements);

            N := Make_Function_Implementation
               (Send_Output_Specification,
               Send_Output_Declarations,
               Send_Output_Statements);

            Append_Node_To_List (N, CTN.Declarations (Current_File));
         end if;
1646

1647
1648
1649
1650
         Pop_Entity; -- U
         Pop_Entity; -- P
      end Visit_Process_Instance;

1651
1652
1653
1654
1655
1656
1657
1658
      ---------------------------
      -- Visit_Device_Instance --
      ---------------------------

      procedure Visit_Device_Instance (E : Node_Id) is
         Implementation  : constant Node_Id := Get_Implementation (E);
         S : Node_Id;
      begin
1659
1660
         Current_Device := E;

1661
1662
1663
1664
1665
1666
1667
1668
1669
         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;
1670
1671

         Current_Device := No_Node;
1672
1673
      end Visit_Device_Instance;

1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
      ---------------------------
      -- 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;
         F                    : Node_Id;
         D                    : Node_Id;
         Fifo_Size_Values     : Node_Id;
         N_Dest_Values        : Node_Id;
         Local_Dest_Values    : Node_Id;
         Destinations_Values  : Node_Id;
         Destinations         : List_Id;
         Deliver_Declarations : List_Id;
         Deliver_Statements   : List_Id;
         Deliver_Alternatives : List_Id;
         Switch_Labels        : List_Id;
         Switch_Statements    : List_Id;
         Parameters           : List_Id;
         Queue_Size           : Long_Long;
         Fifo_Size            : Unsigned_Long_Long := 0;
         Nb_Dest              : Unsigned_Long_Long := 0;
         Has_Local_Deliver    : Boolean := False;
      begin

         if Has_Ports (E) then
            F := First_Node (Features (E));

            Fifo_Size_Values    := Make_Array_Values;
            N_Dest_Values       := Make_Array_Values;
            Destinations_Values := Make_Array_Values;

            Deliver_Declarations := New_List (CTN.K_Declaration_List);
            Deliver_Statements   := New_List (CTN.K_Statement_List);
            Deliver_Alternatives := New_List (CTN.K_Alternatives_List);

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

                  if Is_Out (F) then
1741
                     Destinations := Get_Destination_Ports (F, Current_Device);
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
                     Local_Dest_Values   := Make_Array_Values;

                     if AAU.Is_Empty (Destinations) then
                        Display_Located_Error
                          (Loc (F),
                           "This OUT port is not connected to any"
                           & " destination",
                           Fatal => True);
                     end if;

                     D := First_Node (Destinations);

                     Nb_Dest := 0;

                     while Present (D) loop
                        Append_Node_To_List
                          (Make_Defining_Identifier
                           (Map_C_Enumerator_Name
                            (Item (D), Port_Type => True)),
                           CTN.Values (Local_Dest_Values));

                        Nb_Dest := Nb_Dest + 1;

                        D := Next_Node (D);
                     end loop;

                     --  Make the array which indicate all destinations
                     --  for an out port

                     N := Make_Expression
                       (Left_Expr =>
                          Make_Variable_Declaration
                          (Defining_Identifier =>
                             Make_Array_Declaration
                             (Defining_Identifier =>
                                Make_Defining_Identifier
                                (Map_C_Variable_Name
                                 (F, Port_Local_Dest => True)),
                              Array_Size =>
                                Make_Literal
                                (CV.New_Int_Value
                                 (Nb_Dest, 0, 10))),
                           Used_Type =>
                             RE (RE_Port_T)),
                        Operator => Op_Equal,
                        Right_Expr => Local_Dest_Values);
                     Append_Node_To_List (N, CTN.Declarations (Current_File));

                     --  Add the last array name in the destination array

                     Append_Node_To_List
                       (Make_Defining_Identifier
                        (Map_C_Variable_Name
                         (F, Port_Local_Dest => True)),
                        CTN.Values (Destinations_Values));

                     --  Add the number of destinations in the nb_dest array

                     N := RE (RE_Gqueue_Fifo_Out);
                     Append_Node_To_List
                       (Make_Literal
                        (CV.New_Int_Value
                         (Nb_Dest, 0, 10)),
                        CTN.Values (N_Dest_Values));

                     Append_Node_To_List
                       (N, CTN.Values (Fifo_Size_Values));
                  else
                     if AAN.Is_Data (F) and then not Is_Event (F) then

                        Has_Local_Deliver := True;
                        N := RE (RE_Gqueue_Fifo_Indata);
                        Queue_Size := 0;
                     else

                        Has_Local_Deliver := True;

                        Queue_Size := Get_Queue_Size (F);

                        if Queue_Size = -1 then
                           Queue_Size := Default_Queue_Size;

                           N := Make_Literal
                             (CV.New_Int_Value (Default_Queue_Size, 0, 10));
                        elsif Queue_Size = 0 then
                           --  0 length queues are not supported

                           Display_Located_Error
                             (Loc (F),
                              "Zero length port queues are not supported",
                              Fatal => True);
                        else
                           N := Make_Literal
                             (CV.New_Int_Value
                              (Unsigned_Long_Long (Queue_Size), 0, 10));
                        end if;
                     end if;

                     Append_Node_To_List
                       (N, CTN.Values (Fifo_Size_Values));

                     Append_Node_To_List
                       (Make_Literal
                        (CV.New_Int_Value
                         (0, 0, 10)),
                        CTN.Values (N_Dest_Values));

                     N := Make_Defining_Identifier (CONST (C_Null),
                                                    C_Conversion => False);
                     Append_Node_To_List
                       (N, CTN.Values (Destinations_Values));

                     --  Make the switch alternative for the deliver function

                     Parameters        := New_List (CTN.K_Parameter_List);
                     Switch_Statements := New_List (CTN.K_Statement_List);
                     Switch_Labels     := New_List (CTN.K_Label_List);

                     Append_Node_To_List
                       (Make_Defining_Identifier
                        (Map_C_Enumerator_Name (F, Port_Type => True)),
                        Switch_Labels);

                     Append_Node_To_List
                       (Make_Defining_Identifier
                        (Map_C_Enumerator_Name (S)),
                        Parameters);

                     Append_Node_To_List
                       (Make_Defining_Identifier
                        (Map_C_Enumerator_Name (F, Local_Port => True)),
                        Parameters);

                     Append_Node_To_List
                       (Make_Defining_Identifier (PN (P_Request)),
                        Parameters);

                     N := Make_Call_Profile
                       (RE (RE_Gqueue_Store_In),
                        Parameters);

                     Append_Node_To_List
                       (N, Switch_Statements);

                     N := Make_Switch_Alternative
                       (Switch_Labels, Switch_Statements);
                     Append_Node_To_List (N, Deliver_Alternatives);

                     Fifo_Size := Fifo_Size + Unsigned_Long_Long (Queue_Size);
                  end if;

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

            --  Declare all the needed tables by each thread in order
            --  to handle the ports.

            N := Make_Variable_Declaration
              (Defining_Identifier =>
                 Make_Array_Declaration
                 (Defining_Identifier =>
                    Make_Defining_Identifier
                    (Map_C_Variable_Name (S, Port_Woffsets => True)),
                  Array_Size =>
                    Make_Defining_Identifier
                    (Map_C_Define_Name (S, Nb_Ports => True))),
               Used_Type =>
                 RE (RE_Uint8_T));
            Append_Node_To_List (N, CTN.Declarations (Current_File));

            N := Make_Variable_Declaration
              (Defining_Identifier =>
                 Make_Array_Declaration
                 (Defining_Identifier =>
                    Make_Defining_Identifier
                    (Map_C_Variable_Name (S, Port_Offsets => True)),
                  Array_Size =>
                    Make_Defining_Identifier
                    (Map_C_Define_Name (S, Nb_Ports => True))),
               Used_Type =>
                 RE (RE_Uint8_T));
            Append_Node_To_List (N, CTN.Declarations (Current_File));

            N := Make_Variable_Declaration
              (Defining_Identifier =>
                 Make_Array_Declaration
                 (Defining_Identifier =>
                    Make_Defining_Identifier
                    (Map_C_Variable_Name (S, Port_Used_Size => True)),
                  Array_Size =>
                    Make_Defining_Identifier
                    (Map_C_Define_Name (S, Nb_Ports => True))),
               Used_Type =>
                 RE (RE_Uint8_T));
            Append_Node_To_List (N, CTN.Declarations (Current_File));

            N := Make_Variable_Declaration
              (Defining_Identifier =>
                 Make_Array_Declaration
                 (Defining_Identifier =>
                    Make_Defining_Identifier
                    (Map_C_Variable_Name (S, Port_Empties => True)),
                  Array_Size =>
                    Make_Defining_Identifier
                    (Map_C_Define_Name (S, Nb_Ports => True))),
               Used_Type =>
                 RE (RE_Uint8_T));
            Append_Node_To_List (N, CTN.Declarations (Current_File));

            N := Make_Variable_Declaration
              (Defining_Identifier =>
                 Make_Array_Declaration
                 (Defining_Identifier =>
                    Make_Defining_Identifier
                    (Map_C_Variable_Name (S, Port_First => True)),
                  Array_Size =>
                    Make_Defining_Identifier
                    (Map_C_Define_Name (S, Nb_Ports => True))),
               Used_Type =>
                 RE (RE_Uint8_T));
            Append_Node_To_List (N, CTN.Declarations (Current_File));

            N := Make_Variable_Declaration
              (Defining_Identifier =>
                 Make_Array_Declaration
                 (Defining_Identifier =>
                    Make_Defining_Identifier
                    (Map_C_Variable_Name (S, Port_Recent => True)),
                  Array_Size =>
                       Make_Defining_Identifier
                       (Map_C_Define_Name (S, Nb_Ports => True))),
               Used_Type =>
                 RE (RE_Request_T));
            Append_Node_To_List (N, CTN.Declarations (Current_File));

            N := Make_Variable_Declaration
              (Defining_Identifier =>
                 Make_Array_Declaration
                 (Defining_Identifier =>
                    Make_Defining_Identifier
                    (Map_C_Variable_Name (S, Port_Queue => True)),
                  Array_Size =>
                    Make_Expression
                    (Left_Expr => Make_Literal
                       (CV.New_Int_Value (Fifo_Size, 0, 10)),
                       Operator => Op_Asterisk,
                     Right_Expr =>
                       Make_Call_Profile
                       (Make_Defining_Identifier
                        (FN (F_Sizeof)),
                        Make_List_Id
                        (RE (RE_Request_T))))),
               Used_Type =>
                 RE (RE_Port_T));
            Append_Node_To_List (N, CTN.Declarations (Current_File));

            N := Make_Variable_Declaration
              (Defining_Identifier =>
                 Make_Expression
                 (Left_Expr =>
                    Make_Defining_Identifier
                    (Map_C_Variable_Name (S, Port_Total_Fifo => True)),
                  Operator => Op_Equal,
                  Right_Expr =>
                    Make_Literal (CV.New_Int_Value (Fifo_Size, 0, 10))),
               Used_Type =>
                 RE (RE_Uint16_T));
            Append_Node_To_List (N, CTN.Declarations (Current_File));

            N := Make_Variable_Declaration
              (Defining_Identifier =>
                 Make_Array_Declaration
                 (Defining_Identifier =>
                    Make_Defining_Identifier
                    (Map_C_Variable_Name (S, Port_History => True)),
                  Array_Size =>
                    Make_Literal (CV.New_Int_Value (Fifo_Size, 0, 10))),
               Used_Type =>
                 RE (RE_Local_Port_T));
            Append_Node_To_List (N, CTN.Declarations (Current_File));

            N := Make_Expression
              (Left_Expr =>
                 Make_Variable_Declaration
                 (Defining_Identifier =>
                    Make_Array_Declaration
                    (Defining_Identifier =>
                       Make_Defining_Identifier
                       (Map_C_Variable_Name (S, Port_N_Dest => True)),
                     Array_Size =>
                       Make_Defining_Identifier
                       (Map_C_Define_Name (S, Nb_Ports => True))),
                  Used_Type =>
                    RE (RE_Uint8_T)),
               Operator => Op_Equal,
               Right_Expr => N_Dest_Values);
            Append_Node_To_List (N, CTN.Declarations (Current_File));

            N := Make_Expression
              (Left_Expr =>
                 Make_Variable_Declaration
                 (Defining_Identifier =>
                    Make_Array_Declaration
                    (Defining_Identifier =>
                       Make_Defining_Identifier
                       (Map_C_Variable_Name (S, Port_Fifo_Size => True)),
                     Array_Size =>
                       Make_Defining_Identifier
                       (Map_C_Define_Name (S, Nb_Ports => True))),
                  Used_Type =>
                    RE (RE_Int8_T)),
               Operator => Op_Equal,
               Right_Expr => Fifo_Size_Values);
            Append_Node_To_List (N, CTN.Declarations (Current_File));

            N := Make_Expression
              (Left_Expr =>
                 Make_Variable_Declaration
                 (Defining_Identifier =>
                    Make_Array_Declaration
                    (Defining_Identifier =>
                       Make_Defining_Identifier
                       (Map_C_Variable_Name (S, Port_Destinations => True)),
                     Array_Size =>
                       Make_Defining_Identifier
                       (Map_C_Define_Name (S, Nb_Ports => True))),
                  Used_Type => Make_Pointer_Type
                    (RE (RE_Port_T))),
               Operator => Op_Equal,
               Right_Expr => Destinations_Values);
            Append_Node_To_List (N, CTN.Declarations (Current_File));

            Append_Node_To_List (Make_Switch_Alternative (No_List, No_List),
                                 Deliver_Alternatives);

            if Has_Local_Deliver then
               N := Make_Switch_Statement
                 (Expression =>
                    Make_Member_Designator
                    (Defining_Identifier =>
                       Make_Defining_Identifier (MN (M_Port)),
                     Aggregate_Name =>
                       Make_Defining_Identifier (VN (V_Request)),
                     Is_Pointer => True),
                  Alternatives => Deliver_Alternatives);

               Append_Node_To_List (N, Deliver_Statements);

               --  Make the deliver function specific to a thread

               N := Make_Function_Implementation
                 (CTN.Global_Port_Node (Backend_Node (Identifier (S))),
                  Deliver_Declarations, Deliver_Statements);
               Append_Node_To_List (N, CTN.Declarations (Current_File));

               --  Add a switch alternative to the main deliver
               --  in order to used our local delivery function.

               Parameters        := New_List (CTN.K_Parameter_List);
               Switch_Statements := New_List (CTN.K_Statement_List);
               Switch_Labels     := New_List (CTN.K_Label_List);

               Append_Node_To_List
                 (Make_Defining_Identifier
                  (Map_C_Enumerator_Name
                   (S, Entity => True)),
                  Switch_Labels);

               Append_Node_To_List
                  (Make_Defining_Identifier
2113
                   (VN (V_Request)),
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
                  Parameters);

               N := Make_Call_Profile
                 (Defining_Identifier => Map_Task_Deliver_Identifier (S),
                  Parameters => Parameters);

               Append_Node_To_List (N, Switch_Statements);

               N := Make_Switch_Alternative
                 (Switch_Labels, Switch_Statements);

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

         N := Task_Job_Body (E);
         Append_Node_To_List
           (N, CTN.Declarations (Current_File));

      end Visit_Thread_Instance;

   end Source_File;

end Ocarina.Backends.PO_HI_C.Activity;