ocarina-backends-pok_c-main.adb 66.9 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 K _ C . M A I N           --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--    Copyright (C) 2008-2009 Telecom ParisTech, 2010-2014 ESA & ISAE.      --
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
--                                                                          --
-- 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.                                       --
--                                                                          --
jhugues's avatar
jhugues committed
29
30
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
31
32
33
--                                                                          --
------------------------------------------------------------------------------

34
with Ocarina.Namet;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
with Ocarina.ME_AADL;
with Ocarina.ME_AADL.AADL_Instances.Nodes;
with Ocarina.ME_AADL.AADL_Instances.Nutils;
with Ocarina.ME_AADL.AADL_Instances.Entities;
with Ocarina.Backends.Messages;
with Ocarina.Backends.Utils;
with Ocarina.Backends.Properties;
with Ocarina.Backends.C_Values;
with Ocarina.Backends.C_Tree.Nutils;
with Ocarina.Backends.C_Tree.Nodes;
with Ocarina.Backends.POK_C.Runtime;
with Ocarina.Backends.C_Common.Mapping;

with Ocarina.Instances.Queries;

package body Ocarina.Backends.POK_C.Main 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
   use Ocarina.ME_AADL;
   use Ocarina.ME_AADL.AADL_Instances.Nodes;
   use Ocarina.ME_AADL.AADL_Instances.Entities;
   use Ocarina.Backends.Messages;
   use Ocarina.Backends.Properties;
   use Ocarina.Backends.Utils;
   use Ocarina.Backends.C_Values;
   use Ocarina.Backends.C_Tree.Nutils;
   use Ocarina.Backends.POK_C.Runtime;
   use Ocarina.Backends.C_Common.Mapping;

   use Ocarina.Instances.Queries;

   package AIN renames Ocarina.ME_AADL.AADL_Instances.Nodes;
   package AINU renames Ocarina.ME_AADL.AADL_Instances.Nutils;
   package CTN renames Ocarina.Backends.C_Tree.Nodes;
   package CTU renames Ocarina.Backends.C_Tree.Nutils;
   package CV renames Ocarina.Backends.C_Values;

   -----------------
   -- Source_File --
   -----------------

   package body Source_File is

78
79
80
81
82
      Tattr                 : Node_Id;
      Main_Function         : Node_Id;
      Statements            : List_Id;
      Init_Function         : Node_Id            := No_Node;
      Thread_Id             : Unsigned_Long_Long := 0;
83
84
85
86
87
88
89
90
91
92
93
94
95
      Process_Variable_Type : Node_Id;
      Process_Variable_Name : Node_Id;

      Error_Switch_Alternatives : List_Id;

      Use_Error_Handling : Boolean := False;

      Current_Device : Node_Id := No_Node;

      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
96
97
98
        (E                       : Node_Id;
         Real_Process            : Boolean := True;
         Corresponding_Component : Node_Id := No_Node);
99
100
101
102
103
104
105
106
107
108
      procedure Visit_Device_Instance (E : Node_Id);
      procedure Visit_Processor_Instance (E : Node_Id);
      procedure Visit_Virtual_Processor_Instance (E : Node_Id);
      procedure Visit_Thread_Instance (E : Node_Id);

      procedure Setup_Thread (E : Node_Id);

      function Map_Queueing_Policy (Port : Node_Id) return Node_Id;

      function Map_Queueing_Policy (Port : Node_Id) return Node_Id is
109
110
         ARINC653_Discipline : constant ARINC653_Queuing_Discipline :=
           Get_ARINC653_Queuing_Discipline (Port);
111
      begin
112
113
114
         if ARINC653_Discipline /= Invalid
           and then ARINC653_Discipline = Priority_Based
         then
115

Julien's avatar
Julien committed
116
            if Use_ARINC653_API then
117
118
119
120
121
122
               return RE (RE_Priority);
            else
               return RE (RE_Pok_Port_Queueing_Discipline_Fifo);
            end if;
         end if;

Julien's avatar
Julien committed
123
         if Use_ARINC653_API then
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
            return RE (RE_Fifo);
         else
            return RE (RE_Pok_Port_Queueing_Discipline_Fifo);
         end if;
      end Map_Queueing_Policy;

      ------------------
      -- Setup_Thread --
      ------------------

      procedure Setup_Thread (E : Node_Id) is
         Parameters   : List_Id;
         N            : Node_Id;
         S            : constant Node_Id := Parent_Subcomponent (E);
         Member_Value : Node_Id;
139
         Stack_Size   : Unsigned_Long_Long;
140
141
      begin
         --  Initializes thread attributes.
142
         if Use_ARINC653_API = False then
143
144
145
146
            N :=
              POK_Make_Function_Call_With_Assert
                (RF (RE_Pok_Thread_Attr_Init),
                 Make_List_Id (Make_Variable_Address (Copy_Node (Tattr))));
147
148
149
150
151
152

            Append_Node_To_List (N, Statements);

            POK_Add_Return_Assertion (Statements);
         end if;

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
         --  Make strcpy(tattr.NAME, "prname")

         if POK_Flavor = DEOS then
            N :=
              Make_Call_Profile
                (Make_Defining_Identifier
                  (Get_String_Name ("strcpy")),
                 Make_List_Id (
                  Make_Member_Designator
                     (RE (RE_Name),
                      Copy_Node (Tattr)),
                  Make_Literal
                    (CV.New_Pointed_Char_Value
                     (Name (Identifier (S))))));
            Append_Node_To_List (N, Statements);
         end if;

170
171
         --  Make tattr.entry = entrypoint

Julien's avatar
Julien committed
172
         if Use_ARINC653_API then
173
174
175
176
            N :=
              Make_Expression
                (Left_Expr =>
                   Make_Member_Designator
177
                     (RE (RE_Entry_Point),
178
179
180
181
                      Copy_Node (Tattr)),
                 Operator   => Op_Equal,
                 Right_Expr =>
                   Copy_Node
182
                     (CTN.Defining_Identifier
183
                        (CTN.Job_Node (Backend_Node (Identifier (S))))));
184
185

         else
186
187
188
189
190
191
192
            N :=
              Make_Expression
                (Left_Expr =>
                   Make_Member_Designator (RE (RE_Entry), Copy_Node (Tattr)),
                 Operator   => Op_Equal,
                 Right_Expr =>
                   Copy_Node
193
                     (CTN.Defining_Identifier
194
                        (CTN.Job_Node (Backend_Node (Identifier (S))))));
195
196
197
198
199
200
201
         end if;

         Append_Node_To_List (N, Statements);

         --  Add priority to tattr, tattr.priority = threadpriority

         if Get_Thread_Priority (E) /= 0 then
202
            N := Make_Literal (New_Int_Value (Get_Thread_Priority (E), 1, 10));
203

Julien's avatar
Julien committed
204
            if Use_ARINC653_API then
205
206
207
208
               N :=
                 Make_Expression
                   (Left_Expr =>
                      Make_Member_Designator
209
                        (RE (RE_Base_Priority),
210
211
212
                         Copy_Node (Tattr)),
                    Operator   => Op_Equal,
                    Right_Expr => N);
213
            else
214
215
216
217
               N :=
                 Make_Expression
                   (Left_Expr =>
                      Make_Member_Designator
218
                        (Make_Defining_Identifier (MN (M_Priority)),
219
220
221
                         Copy_Node (Tattr)),
                    Operator   => Op_Equal,
                    Right_Expr => N);
222
223
224
225
226
227
            end if;

            Append_Node_To_List (N, Statements);
         end if;

         if Get_Thread_Deadline (E) /= Null_Time then
Julien's avatar
Julien committed
228
            if POK_Flavor = ARINC653 then
229
230
               Member_Value :=
                 Map_Time_To_Millisecond (Get_Thread_Deadline (E));
Julien's avatar
Julien committed
231
            elsif POK_Flavor = POK then
232
               Member_Value := Map_Time (Get_Thread_Deadline (E));
Julien's avatar
Julien committed
233
234
            else
               Member_Value := No_Node;
235
236
            end if;

Julien's avatar
Julien committed
237
238
239
240
241
242
243
244
245
246
247
            if Member_Value /= No_Node then
               N :=
                 Make_Expression
                   (Left_Expr =>
                      Make_Member_Designator
                        (RE (RE_Deadline),
                         Copy_Node (Tattr)),
                    Operator   => Op_Equal,
                    Right_Expr => Member_Value);
               Append_Node_To_List (N, Statements);
            end if;
248
249
250
251
252
         else
            Display_Error ("Deadline not specified", Fatal => False);
         end if;

         if Get_Thread_Period (E) /= Null_Time then
253
            if Use_ARINC653_API then
Julien's avatar
Julien committed
254
255
256
257
258
259
260
               if POK_Flavor = POK then
                  Member_Value := Map_Time_To_Millisecond
                                    (Get_Thread_Period (E));
               elsif POK_Flavor = DEOS then
                  Member_Value := Map_Time_To_Nanosecond
                                    (Get_Thread_Period (E));
               end if;
261
262
263
264
            else
               Member_Value := Map_Time (Get_Thread_Period (E));
            end if;

Julien's avatar
Julien committed
265
266
267
268
269
270
271
272
            if Member_Value /= No_Node then
               N :=
                 Make_Expression
                   (Left_Expr =>
                      Make_Member_Designator
                        (RE (RE_Period), Copy_Node (Tattr)),
                    Operator   => Op_Equal,
                    Right_Expr => Member_Value);
273

Julien's avatar
Julien committed
274
275
               Append_Node_To_List (N, Statements);
            end if;
276
277
278
279
         else
            Display_Error ("Period not specified", Fatal => True);
         end if;

280
281
282
283
284
285
         --
         --  Set up the Stack Size
         --  On most system, the default is 4096. So, we set
         --  up 4096 if not explicitly declared. We use
         --  The AADL property Stack_Size if declared.
         --
286

287
288
289
290
         Stack_Size := 4096;

         if Get_Thread_Stack_Size (E) /= Null_Size then
            Stack_Size := To_Bytes (Get_Thread_Stack_Size (E));
291
         end if;
292
293
294
295
296
297
298
299
300
301
302
303
304
         N :=
           Make_Expression
             (Left_Expr =>
                Make_Member_Designator
                  (RE (RE_Stack_Size),
                   Copy_Node (Tattr)),
              Operator   => Op_Equal,
              Right_Expr =>
                Make_Literal
                  (New_Int_Value (Stack_Size,
                                 1,
                                 10)));
         Append_Node_To_List (N, Statements);
305
306

         declare
307
            TA       : constant Time_Array := Get_Execution_Time (E);
308
309
310
            Capacity : Node_Id;
         begin
            if TA /= Empty_Time_Array then
311
               if Use_ARINC653_API then
Julien's avatar
Julien committed
312
313
314
315
316
317
318
                  if POK_Flavor = ARINC653 then
                     Capacity := Map_Time_To_Millisecond (TA (1));
                  elsif POK_Flavor = DEOS then
                     Capacity := Map_Time_To_Nanosecond (TA (1));
                  else
                     Capacity := No_Node;
                  end if;
319
320
321
322
323
               else
                  Capacity := Map_Time (TA (1));
               end if;
            else
               Display_Error
324
325
                 ("Compute execution time not declared",
                  Fatal => False);
326
327

               --  By default, we allocate 1 ms for thread execution.
328
329
               Capacity :=
                 CTU.Make_Literal (CV.New_Int_Value (Thread_Id, 1, 10));
330
            end if;
331

332
333
334
335
            N :=
              Make_Expression
                (Left_Expr =>
                   Make_Member_Designator
336
                     (RE (RE_Time_Capacity),
337
338
339
                      Copy_Node (Tattr)),
                 Operator   => Op_Equal,
                 Right_Expr => Capacity);
340

341
            Append_Node_To_List (N, Statements);
342
343
344
345
346
347
348
         end;

         --  Add tid and other stuff to the parameters and call
         --  pok_thread_create

         Parameters := New_List (CTN.K_Parameter_List);

349
         if Use_ARINC653_API then
350
351
352
353
354
355
356
357
358
359
            Append_Node_To_List
              (Make_Variable_Address (Copy_Node (Tattr)),
               Parameters);

            Append_Node_To_List
              (Make_Variable_Address
                 (Make_Array_Value
                    (Copy_Node (Process_Variable_Name),
                     CTU.Make_Literal (CV.New_Int_Value (Thread_Id, 1, 10)))),
               Parameters);
360
361
            Add_Return_Variable_In_Parameters (Parameters);
         else
362
363
364
365
366
367
            Append_Node_To_List
              (Make_Variable_Address
                 (Make_Array_Value
                    (Copy_Node (Process_Variable_Name),
                     CTU.Make_Literal (CV.New_Int_Value (Thread_Id, 1, 10)))),
               Parameters);
368

369
370
371
            Append_Node_To_List
              (Make_Variable_Address (Copy_Node (Tattr)),
               Parameters);
372
373
374

         end if;

Julien's avatar
Julien committed
375
         if Use_ARINC653_API then
376
            Append_Node_To_List
377
378
379
              (POK_Make_Function_Call_With_Assert
                 (RF (RE_Create_Process),
                  Parameters),
380
               Statements);
jhugues's avatar
jhugues committed
381
            Parameters := New_List (CTN.K_Parameter_List);
382
383
384
385
386
            Append_Node_To_List
              (Make_Array_Value
                 (Copy_Node (Process_Variable_Name),
                  CTU.Make_Literal (CV.New_Int_Value (Thread_Id, 1, 10))),
               Parameters);
jhugues's avatar
jhugues committed
387
            Add_Return_Variable_In_Parameters (Parameters);
388
            Append_Node_To_List
389
              (POK_Make_Function_Call_With_Assert (RF (RE_Start), Parameters),
390
               Statements);
391
392
         else
            Append_Node_To_List
393
394
395
              (POK_Make_Function_Call_With_Assert
                 (RF (RE_Pok_Thread_Create),
                  Parameters),
396
397
398
               Statements);
         end if;

399
400
401
402
403
         N :=
           Message_Comment
             ("This thread was mapped from a thread component contained" &
              "in this process. The function it executes is also generated" &
              "in the file activity.c.");
404
405
406
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
435
436
437
438
439
440
441

         POK_Add_Return_Assertion (Statements);

         CTU.Append_Node_To_List (N, Statements);
      end Setup_Thread;

      -----------
      -- 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
442
443
         Category : constant Component_Category :=
           Get_Category_Of_Component (E);
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
      begin
         case Category is
            when CC_System =>
               Visit_System_Instance (E);

            when CC_Process =>
               Visit_Process_Instance (E);

            when CC_Device =>
               Visit_Device_Instance (E);

            when CC_Processor =>
               Visit_Processor_Instance (E);

            when CC_Virtual_Processor =>
               Visit_Virtual_Processor_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
474
475
476
        (E                       : Node_Id;
         Real_Process            : Boolean := True;
         Corresponding_Component : Node_Id := No_Node)
477
      is
478
479
480
481
482
483
484
485
486
487
488
489
490
491
         U                : Node_Id;
         P                : Node_Id;
         N                : Node_Id;
         S                : Node_Id;
         Spec             : Node_Id;
         Declarations     : List_Id := New_List (CTN.K_Declaration_List);
         Call_Parameters  : List_Id;
         Called_Function  : Node_Id;
         Used_Type        : Node_Id;
         Used_Member      : Node_Id;
         While_Statements : constant List_Id :=
           New_List (CTN.K_Statement_List);
         Shared_Data             : Node_Id;
         Function_Call           : Node_Id;
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
         Communicating_Component : Node_Id;

         procedure Setup_In_Ports;
         procedure Setup_Out_Ports;

         --------------------
         -- Setup_In_Ports --
         --------------------

         procedure Setup_In_Ports is
            F               : Node_Id;
            Called_Function : Node_Id;
            Added_Parameter : Node_Id;
            Variable_Type   : Node_Id;
         begin
            if AINU.Is_Empty (Features (Communicating_Component)) then
               return;
            end if;

            F := First_Node (Features (Communicating_Component));

            while Present (F) loop
               Call_Parameters := New_List (CTN.K_Parameter_List);

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
               if Kind (F) = K_Port_Spec_Instance
                 and then Is_In (F)
                 and then not Is_Out (F)
                 and then Get_Connection_Pattern (F) = Inter_Process
                 and then not Is_Virtual (Get_Port_By_Name (F, Current_Device))
               then
                  if AIN.Is_Data (F) and then Is_Event (F) then
                     Append_Node_To_List
                       (Make_Literal
                          (CV.New_Pointed_Char_Value (Map_Port_Name (F))),
                        Call_Parameters);

                     if POK_Flavor = POK then
                        N := Map_Queue_Size_With_Data (F);
                        Append_Node_To_List (N, Call_Parameters);
                     else

                        --  Map the size

                        N := Get_Data_Size (Corresponding_Instance (F));

                        Append_Node_To_List (N, Call_Parameters);

                        --  Map the queue size

                        N := Map_Queue_Size (F);
                        Append_Node_To_List (N, Call_Parameters);
                     end if;

545
                     if Use_ARINC653_API then
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
                        Added_Parameter := RE (RE_Destination);
                     else
                        Added_Parameter := RE (RE_Pok_Port_Direction_In);
                     end if;

                     Append_Node_To_List (Added_Parameter, Call_Parameters);

                     Added_Parameter := Map_Queueing_Policy (F);

                     Append_Node_To_List (Added_Parameter, Call_Parameters);

                     Append_Node_To_List
                       (Make_Variable_Address
                          (Make_Defining_Identifier (Map_Port_Var (F))),
                        Call_Parameters);

562
                     if Use_ARINC653_API then
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
                        Append_Node_To_List
                          (Make_Variable_Address
                             (Make_Defining_Identifier (VN (V_Ret))),
                           Call_Parameters);

                        Called_Function := RF (RE_Create_Queuing_Port);
                     else
                        Called_Function := RF (RE_Pok_Port_Queueing_Create);
                     end if;

                     Append_Node_To_List
                       (POK_Make_Function_Call_With_Assert
                          (Called_Function,
                           Call_Parameters),
                        Statements);

                     POK_Add_Return_Assertion (Statements);

581
                     if Use_ARINC653_API then
582
583
584
585
586
587
588
589
590
591
592
593
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
                        Variable_Type := RE (RE_Queuing_Port_Id_Type);
                     else
                        Variable_Type := RE (RE_Uint8_T);
                     end if;

                     Append_Node_To_List
                       (Make_Variable_Declaration
                          (Defining_Identifier =>
                             (Make_Defining_Identifier (Map_Port_Var (F))),
                           Used_Type => Variable_Type),
                        CTN.Declarations (Current_File));

                     N :=
                       Message_Comment
                         ("This queueing port was mapped from an in" &
                          "event data port contained in the process. It" &
                          "is used for inter-partition communication.");

                     CTU.Append_Node_To_List (N, Statements);
                  elsif AIN.Is_Data (F) and then not Is_Event (F) then
                     Append_Node_To_List
                       (Make_Literal
                          (CV.New_Pointed_Char_Value (Map_Port_Name (F))),
                        Call_Parameters);

                     if Current_Device = No_Node then
                        N := Get_Inter_Partition_Port_Size (F);

                        if Is_Using_Virtual_Bus (F) then
                           Add_Include (RH (RH_Protocols));
                           Add_Define_Deployment (RE (RE_Pok_Needs_Protocols));
                        end if;
                     else
                        N :=
                          Get_Inter_Partition_Port_Size
                            (Get_Port_By_Name (F, Current_Device));

                        if Is_Using_Virtual_Bus
                            (Get_Port_By_Name (F, Current_Device))
                        then
                           Add_Include (RH (RH_Protocols));
                           Add_Define_Deployment (RE (RE_Pok_Needs_Protocols));
                        end if;
                     end if;

                     Append_Node_To_List (N, Call_Parameters);

                     --  Map the port of the sampling port, take in
                     --  account potential virtual bus layers.

632
                     if Use_ARINC653_API then
633
634
635
636
637
638
639
640
641
642
                        Append_Node_To_List
                          (RE (RE_Destination),
                           Call_Parameters);
                     else
                        Append_Node_To_List
                          (RE (RE_Pok_Port_Direction_In),
                           Call_Parameters);
                     end if;

                     if Get_POK_Refresh_Time (F) /= Null_Time then
643
                        if Use_ARINC653_API then
644
645
646
647
648
649
650
651
                           N :=
                             Map_Time_To_Millisecond
                               (Get_POK_Refresh_Time (F));
                        else
                           N := Map_Time (Get_POK_Refresh_Time (F));
                        end if;
                     else
                        N := CTU.Make_Literal (CV.New_Int_Value (0, 1, 10));
652
                     end if;
653
654
655
656
657
658
659
                     Append_Node_To_List (N, Call_Parameters);

                     Append_Node_To_List
                       (Make_Variable_Address
                          (Make_Defining_Identifier (Map_Port_Var (F))),
                        Call_Parameters);

660
                     if Use_ARINC653_API then
661
662
663
664
665
666
667
668
669
670
671
672
673
674
                        Add_Return_Variable_In_Parameters (Call_Parameters);
                        Called_Function := RF (RE_Create_Sampling_Port);
                     else
                        Called_Function := RF (RE_Pok_Port_Sampling_Create);
                     end if;

                     Append_Node_To_List
                       (POK_Make_Function_Call_With_Assert
                          (Called_Function,
                           Call_Parameters),
                        Statements);

                     POK_Add_Return_Assertion (Statements);

675
                     if Use_ARINC653_API then
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
                        Variable_Type := RE (RE_Sampling_Port_Id_Type);
                     else
                        Variable_Type := RE (RE_Uint8_T);
                     end if;

                     Append_Node_To_List
                       (Make_Variable_Declaration
                          (Defining_Identifier =>
                             (Make_Defining_Identifier (Map_Port_Var (F))),
                           Used_Type => (Variable_Type)),
                        CTN.Declarations (Current_File));

                     N :=
                       Message_Comment
                         ("This sampling port was mapped from a in data" &
                          "port contained in the process. It is used" &
                          "for inter-partition communication.");

                     CTU.Append_Node_To_List (N, Statements);
                  end if;
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
               end if;
               F := Next_Node (F);
            end loop;
         end Setup_In_Ports;

         ---------------------
         -- Setup_Out_Ports --
         ---------------------

         procedure Setup_Out_Ports is
            F               : Node_Id;
            Called_Function : Node_Id;
            Added_Parameter : Node_Id;
            Variable_Type   : Node_Id;
         begin

            Error_Switch_Alternatives := New_List (CTN.K_Alternatives_List);

            if AINU.Is_Empty (Features (Communicating_Component)) then
               return;
            end if;

            F := First_Node (Features (Communicating_Component));

            while Present (F) loop

               Call_Parameters := New_List (CTN.K_Parameter_List);

724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
               if Kind (F) = K_Port_Spec_Instance
                 and then Is_Out (F)
                 and then not Is_In (F)
                 and then Get_Connection_Pattern (F) = Inter_Process
                 and then not Is_Virtual (Get_Port_By_Name (F, Current_Device))
               then
                  if AIN.Is_Data (F) and then not Is_Event (F) then
                     Append_Node_To_List
                       (Make_Literal
                          (CV.New_Pointed_Char_Value (Map_Port_Name (F))),
                        Call_Parameters);

                     if Current_Device = No_Node then
                        N := Get_Inter_Partition_Port_Size (F);

                        if Is_Using_Virtual_Bus (F) then
                           Add_Include (RH (RH_Protocols));
                           Add_Define_Deployment (RE (RE_Pok_Needs_Protocols));
                        end if;
                     else
                        N :=
                          Get_Inter_Partition_Port_Size
                            (Get_Port_By_Name (F, Current_Device));

                        if Is_Using_Virtual_Bus
                            (Get_Port_By_Name (F, Current_Device))
                        then
                           Add_Include (RH (RH_Protocols));
                           Add_Define_Deployment (RE (RE_Pok_Needs_Protocols));
                        end if;
                     end if;

                     Append_Node_To_List (N, Call_Parameters);

                     --  Map the size of the port, take in account
                     --  potential virtual bus layers.

761
                     if Use_ARINC653_API then
762
763
764
765
766
767
768
769
770
                        Append_Node_To_List (RE (RE_Source), Call_Parameters);
                     else
                        Append_Node_To_List
                          (RE (RE_Pok_Port_Direction_Out),
                           Call_Parameters);
                     end if;

                     if Get_POK_Refresh_Time (F) /= Null_Time then

771
                        if Use_ARINC653_API then
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
                           N :=
                             Map_Time_To_Millisecond
                               (Get_POK_Refresh_Time (F));
                        else
                           N := Map_Time (Get_POK_Refresh_Time (F));
                        end if;

                     else
                        N := CTU.Make_Literal (CV.New_Int_Value (0, 1, 10));
                     end if;

                     Append_Node_To_List (N, Call_Parameters);

                     Append_Node_To_List
                       (Make_Variable_Address
                          (Make_Defining_Identifier (Map_Port_Var (F))),
                        Call_Parameters);

790
                     if Use_ARINC653_API then
791
792
793
794
795
796
797
798
799
800
801
802
803
804
                        Add_Return_Variable_In_Parameters (Call_Parameters);
                        Called_Function := RF (RE_Create_Sampling_Port);
                     else
                        Called_Function := RF (RE_Pok_Port_Sampling_Create);
                     end if;

                     N :=
                       POK_Make_Function_Call_With_Assert
                         (Called_Function,
                          Call_Parameters);
                     Append_Node_To_List (N, Statements);

                     POK_Add_Return_Assertion (Statements);

805
                     if Use_ARINC653_API then
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
                        Variable_Type := RE (RE_Sampling_Port_Id_Type);
                     else
                        Variable_Type := RE (RE_Uint8_T);
                     end if;

                     Append_Node_To_List
                       (Make_Variable_Declaration
                          (Defining_Identifier =>
                             (Make_Defining_Identifier (Map_Port_Var (F))),
                           Used_Type => (Variable_Type)),
                        CTN.Declarations (Current_File));

                     N :=
                       Message_Comment
                         ("This sampling port was mapped from an out" &
                          "data port contained in the process. It is" &
                          "used for inter-partition communication.");

                     CTU.Append_Node_To_List (N, Statements);
                  elsif AIN.Is_Data (F) and then Is_Event (F) then
                     Append_Node_To_List
                       (Make_Literal
                          (CV.New_Pointed_Char_Value (Map_Port_Name (F))),
                        Call_Parameters);

                     if POK_Flavor = POK then
                        N := Map_Queue_Size_With_Data (F);
                        Append_Node_To_List (N, Call_Parameters);
                     else
                        --  Map the size

                        N := Get_Data_Size (Corresponding_Instance (F));

                        Append_Node_To_List (N, Call_Parameters);

                        --  Map the queue size

                        N := Map_Queue_Size (F);
                        Append_Node_To_List (N, Call_Parameters);
                     end if;

847
                     if Use_ARINC653_API then
848
849
850
                        Added_Parameter := RE (RE_Source);
                     else
                        Added_Parameter := RE (RE_Pok_Port_Direction_Out);
851
                     end if;
852
853
854
855
856
857
858
859
860
861
862
863

                     Append_Node_To_List (Added_Parameter, Call_Parameters);

                     Added_Parameter := Map_Queueing_Policy (F);

                     Append_Node_To_List (Added_Parameter, Call_Parameters);

                     Append_Node_To_List
                       (Make_Variable_Address
                          (Make_Defining_Identifier (Map_Port_Var (F))),
                        Call_Parameters);

864
                     if Use_ARINC653_API then
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
                        Add_Return_Variable_In_Parameters (Call_Parameters);

                        Called_Function := RF (RE_Create_Queuing_Port);
                     else
                        Called_Function := RF (RE_Pok_Port_Queueing_Create);
                     end if;

                     N :=
                       POK_Make_Function_Call_With_Assert
                         (Called_Function,
                          Call_Parameters);

                     Append_Node_To_List (N, Statements);

                     POK_Add_Return_Assertion (Statements);

881
                     if Use_ARINC653_API then
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
                        Variable_Type := RE (RE_Queuing_Port_Id_Type);
                     else
                        Variable_Type := RE (RE_Uint8_T);
                     end if;

                     Append_Node_To_List
                       (Make_Variable_Declaration
                          (Defining_Identifier =>
                             (Make_Defining_Identifier (Map_Port_Var (F))),
                           Used_Type => (Variable_Type)),
                        CTN.Declarations (Current_File));

                     N :=
                       Message_Comment
                         ("This queueing port was mapped from an out " &
                          "event data port contained in the process. It" &
                          "is used for inter-partition communication.");

                     CTU.Append_Node_To_List (N, Statements);
                  end if;
902
903
904
905
906
907
908
909
910
911
               end if;
               F := Next_Node (F);
            end loop;
         end Setup_Out_Ports;

         -------------------------
         -- Setup_Virtual_Ports --
         -------------------------

         procedure Setup_Virtual_Ports is
912
            F : Node_Id;
913
914
915
916
917
918
919
920
921
922
923
924
         begin
            Call_Parameters := New_List (CTN.K_Parameter_List);

            if AINU.Is_Empty (Features (Communicating_Component)) then
               return;
            end if;

            F := First_Node (Features (Communicating_Component));

            while Present (F) loop
               Call_Parameters := New_List (CTN.K_Parameter_List);

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
               if Kind (F) = K_Port_Spec_Instance
                 and then Is_Data (F)
                 and then Get_Connection_Pattern (F) = Inter_Process
                 and then Is_Virtual (Get_Port_By_Name (F, Current_Device))
               then
                  Append_Node_To_List
                    (Make_Literal
                       (CV.New_Pointed_Char_Value (Map_Port_Name (F))),
                     Call_Parameters);

                  Append_Node_To_List
                    (Make_Variable_Address
                       (Make_Defining_Identifier (Map_Port_Var (F))),
                     Call_Parameters);

                  N :=
                    Make_Call_Profile
                      (RF (RE_Pok_Port_Virtual_Create),
                       Call_Parameters);
                  Append_Node_To_List (N, Statements);

                  Append_Node_To_List
                    (Make_Variable_Declaration
                       (Defining_Identifier =>
                          (Make_Defining_Identifier (Map_Port_Var (F))),
                        Used_Type => RE (RE_Uint8_T)),
                     CTN.Declarations (Current_File));
952
953
954
955
956
957
958
               end if;
               F := Next_Node (F);
            end loop;
         end Setup_Virtual_Ports;

      begin
         if Real_Process then
959
960
961
            U :=
              CTN.Distributed_Application_Unit
                (CTN.Naming_Node (Backend_Node (Identifier (E))));
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
            P := CTN.Entity (U);

            Push_Entity (P);
            Push_Entity (U);

            Communicating_Component := E;
         else
            Communicating_Component := Corresponding_Component;
         end if;

         Set_Main_Source;

         Use_Error_Handling := False;

         Thread_Id := 1;

         Statements := New_List (CTN.K_Statement_List);

980
         Add_Include (E => RH (RH_Activity));
981

982
         if Use_ARINC653_API then
983
984
985
986
987
988
989
990
991
            Process_Variable_Type := RE (RE_Process_Id_Type);
            Process_Variable_Name := RE (RE_Arinc_Threads);
         else
            Process_Variable_Type := RE (RE_Uint32_T);
            Process_Variable_Name := RE (RE_Pok_Threads);
         end if;

         --  Set up ports of the partition
         Append_Node_To_List
992
993
994
995
           (Make_Variable_Declaration
              (CTU.Make_Array_Declaration
                 (Defining_Identifier => Process_Variable_Name,
                  Array_Size          => RE (RE_Pok_Config_Nb_Threads)),
996
997
998
999
1000
1001
               Used_Type => Process_Variable_Type),
            CTN.Declarations (Current_File));

         Set_Str_To_Name_Buffer ("tattr");
         Tattr := Make_Defining_Identifier (Name_Find);

1002
         if Use_ARINC653_API then
1003
1004
1005
1006
            N :=
              Make_Variable_Declaration
                (Defining_Identifier => Copy_Node (Tattr),
                 Used_Type           => RE (RE_Process_Attribute_Type));
1007
         else
1008
1009
1010
1011
            N :=
              Make_Variable_Declaration
                (Defining_Identifier => Copy_Node (Tattr),
                 Used_Type           => RE (RE_Pok_Thread_Attr_T));
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
         end if;

         --  Declare the variable that contain
         --  tasks/process attributes.

         Append_Node_To_List (N, Statements);

         POK_Declare_Return_Variable (Statements);

         Setup_In_Ports;
         Setup_Out_Ports;
         Setup_Virtual_Ports;

         --  Make the main function specification and add it in the current
         --  file (main.c).

1028
1029
1030
1031
1032
         Spec :=
           Make_Function_Specification
             (Defining_Identifier => RE (RE_Main),
              Parameters          => No_List,
              Return_Type         => RE (RE_Int));
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051

         if Init_Function /= No_Node then
            Append_Node_To_List (Init_Function, Statements);
         end if;

         --  First, generate code to create semaphores associated
         --  with data components.
         if not AINU.Is_Empty (Subcomponents (E)) then
            S := First_Node (Subcomponents (E));
            while Present (S) loop
               if AINU.Is_Data (Corresponding_Instance (S)) then
                  Shared_Data := Corresponding_Instance (S);
                  --  Automatically use the types.h header if we use
                  --  protected data.

                  Add_Include (RH (RH_Types));

                  --  Declare the variable in the file

1052
1053
1054
1055
                  N :=
                    Make_Variable_Declaration
                      (Map_C_Defining_Identifier (S),
                       Map_C_Data_Type_Designator (Shared_Data));
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066

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

                  Bind_AADL_To_Object (Identifier (S), N);

                  --  If the data needs to be protected, generate
                  --  function call to create locking objects.

                  if Is_Protected_Data (Shared_Data) then
                     Call_Parameters := New_List (CTN.K_Parameter_List);

1067
                     if Use_ARINC653_API then
1068
1069
                        --  Add the semaphore name.
                        Append_Node_To_List
1070
1071
1072
                          (Make_Literal
                             (CV.New_Pointed_Char_Value
                                (Map_Associated_Locking_Entity_Name (S))),
1073
1074
1075
1076
                           Call_Parameters);

                        --  Current semaphore value.
                        Append_Node_To_List
1077
                          (Make_Literal (New_Int_Value (1, 1, 10)),
1078
1079
1080
1081
                           Call_Parameters);

                        --  Maximum semaphore value.
                        Append_Node_To_List
1082
                          (Make_Literal (New_Int_Value (1, 1, 10)),
1083
1084
1085
                           Call_Parameters);

                        --  Specify the queueing port discipline
1086
                        Append_Node_To_List (RE (RE_Fifo), Call_Parameters);
1087
1088

                        Append_Node_To_List
1089
1090
1091
                          (Make_Variable_Address
                             (Make_Defining_Identifier
                                (Map_Associated_Locking_Entity_Name (S))),
1092
1093
1094
1095
                           Call_Parameters);

                        Add_Return_Variable_In_Parameters (Call_Parameters);

1096
1097
1098
1099
                        Function_Call :=
                          POK_Make_Function_Call_With_Assert
                            (RF (RE_Create_Semaphore),
                             Call_Parameters);
1100
1101
                     else
                        Append_Node_To_List
1102
1103
1104
                          (Make_Variable_Address
                             (Make_Defining_Identifier
                                (Map_Associated_Locking_Entity_Name (S))),
1105
1106
1107
1108
                           Call_Parameters);

                        --  Current semaphore value.
                        Append_Node_To_List
1109
                          (Make_Literal (New_Int_Value (1, 1, 10)),
1110
1111
1112
1113
                           Call_Parameters);

                        --  Maximum semaphore value.
                        Append_Node_To_List
1114
                          (Make_Literal (New_Int_Value (1, 1, 10)),
1115
1116
1117
1118
                           Call_Parameters);

                        --  Specify the queueing port discipline
                        Append_Node_To_List
1119
                          (RE (RE_Pok_Semaphore_Discipline_Fifo),
1120
1121
                           Call_Parameters);

1122
1123
1124
1125
                        Function_Call :=
                          POK_Make_Function_Call_With_Assert
                            (RF (RE_Pok_Sem_Create),
                             Call_Parameters);
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
                     end if;

                     Append_Node_To_List (Function_Call, Statements);
                  end if;
               end if;

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

         --  Then, generate the other components
         --  correspond to thread initialization.

         if not AINU.Is_Empty (Subcomponents (E)) then
            S := First_Node (Subcomponents (E));
            while Present (S) loop
1142
               Visit (Corresponding_Instance (S));
1143
1144
1145
1146
1147
1148
1149
               S := Next_Node (S);
            end loop;
         end if;

         if Use_Error_Handling then
            Call_Parameters := New_List (CTN.K_Parameter_List);

1150
            if Use_ARINC653_API then
1151
1152
1153
               Called_Function := RF (RE_Create_Error_Handler);

               Append_Node_To_List
1154
1155
                 (RE (RE_Pok_Error_Handler_Worker),
                  Call_Parameters);
1156
1157

               Append_Node_To_List
1158
                 (Make_Literal (CV.New_Int_Value (8192, 1, 10)),
1159
1160
1161
1162
1163
1164
1165
                  Call_Parameters);

               Add_Return_Variable_In_Parameters (Call_Parameters);
            else
               Called_Function := RF (RE_Pok_Error_Handler_Create);
            end if;

1166
            N := CTU.Make_Call_Profile (Called_Function, Call_Parameters);
1167
1168
1169
1170
            Append_Node_To_List (N, Statements);

            POK_Add_Return_Assertion (Statements);

1171
1172
1173
1174
1175
            N :=
              Message_Comment
                ("One thread inside the partition can raise faults." &
                 "We start the error handle to treat these potential" &
                 "faults.");
1176
1177
1178
1179
1180
1181
1182
1183

            CTU.Append_Node_To_List (N, Statements);
         end if;

         --  Here, all threads were created. Put the partition
         --  in the NORMAL state.

         if POK_Flavor = POK then
1184
1185
1186
1187
            N :=
              CTU.Make_Call_Profile
                (RF (RE_Pok_Partition_Set_Mode),
                 Make_List_Id (RE (RE_Pok_Partition_Mode_Normal)));
1188
1189
1190
1191
1192
1193
         else
            Call_Parameters := New_List (CTN.K_Parameter_List);

            Append_Node_To_List (RE (RE_Normal), Call_Parameters);
            Add_Return_Variable_In_Parameters (Call_Parameters);

1194
1195
1196
1197
            N :=
              CTU.Make_Call_Profile
                (RF (RE_Set_Partition_Mode),
                 Call_Parameters);
1198
1199
1200
         end if;
         Append_Node_To_List (N, Statements);

1201
1202
1203
1204
1205
1206
         N :=
           Message_Comment
             ("Now, we created all resources of the process. Consequently," &
              "this thread will not be used any more and it will be kept" &
              "in a dormant state. By doing that, we also allow one more" &
              "thread in this partition");
1207
1208
1209

         CTU.Append_Node_To_List (N, Statements);

1210
1211
         N :=
           CTU.Make_Return_Statement (Make_Literal (New_Int_Value (0, 1, 10)));
1212
1213
         Append_Node_To_List (N, Statements);

1214
1215
         Main_Function :=
           Make_Function_Implementation (Spec, Declarations, Statements);
1216
1217
1218
1219
1220
1221
1222
1223

         --  Now, we define the error handler callback.
         --  This function is used each time an error is raised by
         --  the kernel.

         if Use_Error_Handling then
            Set_Deployment_Header;

1224
1225
1226
1227
1228
            N :=
              CTU.Make_Define_Statement
                (Defining_Identifier =>
                   RE (RE_Pok_Use_Generated_Error_Handler),
                 Value => CTU.Make_Literal (CV.New_Int_Value (1, 1, 10)));
1229
1230
1231
1232
1233
1234

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

            Set_Main_Source;

            Declarations := New_List (CTN.K_Declaration_List);
1235
            Statements   := New_List (CTN.K_Statement_List);
1236

1237
            if Use_ARINC653_API then
1238
1239
1240
1241
1242
1243
               Used_Type := RE (RE_Error_Status_Type);
            else
               Used_Type := RE (RE_Pok_Error_Status_T);
            end if;

            Append_Node_To_List
1244
1245
1246
              (Make_Variable_Declaration
                 (Defining_Identifier =>
                    Make_Defining_Identifier (VN (V_Error_Status)),
1247
1248
1249
                  Used_Type => Used_Type),
               Statements);

1250
            if Use_ARINC653_API then
1251
               Append_Node_To_List
1252
1253
1254
                 (Make_Variable_Declaration
                    (Defining_Identifier =>
                       Make_Defining_Identifier (VN (V_Ret)),
1255
1256
1257
1258
                     Used_Type => RE (RE_Return_Code_Type)),
                  Statements);
            end if;

1259
1260
1261
1262
1263
            N :=
              Message_Comment
                ("The variables error and thread are created" &
                 "to store the thread-id and the error-id" &
                 "when a fault is raised");
1264
1265
1266

            CTU.Append_Node_To_List (N, Statements);

1267
1268
1269
1270
            N :=
              Message_Comment
                ("We prefer to force a default value to the variables" &
                 "error and thread");
1271
1272
1273
1274
1275
1276
1277
1278
            Append_Node_To_List (N, Statements);

            --  Here, we initialize the variables to be sure to have
            --  a good default value.

            Call_Parameters := New_List (CTN.K_Parameter_List);

            Append_Node_To_List
1279
1280
              (Make_Variable_Address
                 (Make_Defining_Identifier (VN (V_Error_Status))),
1281
1282
               Call_Parameters);

1283
1284
1285
1286
1287
            N :=
              Message_Comment
                ("Here, we declare the fault handler as ready and" &
                 "that faulty thread-id and error-id must be stored" &
                 "in the thread and error variables");
1288
1289
1290

            CTU.Append_Node_To_List (N, Statements);

1291
            if Use_ARINC653_API then
1292
1293
1294
1295
1296
1297
               Called_Function := RF (RE_Stop_Self);
            else
               Called_Function := RF (RE_Pok_Thread_Stop_Self);
            end if;

            Append_Node_To_List
1298
              (Make_Call_Profile (Called_Function, No_List),
1299
1300
               While_Statements);

1301
            if Use_ARINC653_API then
1302
1303
1304
1305
1306
1307
1308
1309
               Called_Function := RF (RE_Get_Error_Status);
            else
               Called_Function := RF (RE_Pok_Error_Get);
            end if;

            Call_Parameters := New_List (CTN.K_Parameter_List);

            Append_Node_To_List
1310
1311
              (Make_Variable_Address
                 (Make_Defining_Identifier (VN (V_Error_Status))),
1312
1313
               Call_Parameters);

1314
            if Use_ARINC653_API then
1315
               Append_Node_To_List
1316
1317
                 (Make_Variable_Address
                    (Make_Defining_Identifier (VN (V_Ret))),
1318
1319
1320
1321
                  Call_Parameters);
            end if;

            Append_Node_To_List
1322
              (Make_Call_Profile (Called_Function, Call_Parameters),
1323
1324
               While_Statements);

1325
            if Use_ARINC653_API then
1326
1327
               Used_Member := RE (RE_Failed_Process_Id);
            else
1328
               Used_Member := Make_Defining_Identifier (MN (M_Failed_Thread));
1329
1330
            end if;
            Append_Node_To_List
1331
1332
1333
1334
              (Make_Switch_Statement
                 (Make_Member_Designator
                    (Used_Member,
                     Make_Defining_Identifier (VN (V_Error_Status))),
1335
1336
1337
1338
1339
1340
                  Error_Switch_Alternatives),
               While_Statements);

            --  We declare the while (1) loop executed by the task

            Append_Node_To_List
1341
1342
              (Make_While_Statement
                 (Make_Literal (CV.New_Int_Value (1, 0, 10)),
1343
1344
1345
                  While_Statements),
               Statements);

1346
1347
1348
1349
1350
            Spec :=
              Make_Function_Specification
                (Defining_Identifier => RE (RE_Pok_Error_Handler_Worker),
                 Parameters          => No_List,
                 Return_Type => Make_Defining_Identifier (TN (T_Void)));
1351
1352

            Append_Node_To_List
1353
              (Make_Function_Implementation (Spec, Declarations, Statements),
1354
1355
               CTN.Declarations (Current_File));

1356
1357
1358
1359
1360
            N :=
              Message_Comment
                ("IMPORTANT : this main function creates all resources " &
                 "(ports, data, tasks/threads/processes used by this   " &
                 "node");
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401

            CTU.Append_Node_To_List (N, Statements);

            --  Finally, we implement the main function that handles errors.
         end if;

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

         if Real_Process then
            Pop_Entity; -- U
            Pop_Entity; -- P
         end if;
      end Visit_Process_Instance;

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

      procedure Visit_System_Instance (E : Node_Id) is
         S : Node_Id;
      begin
         --  Visit all the subcomponents of the system

         if not AINU.Is_Empty (Subcomponents (E)) then
            S := First_Node (Subcomponents (E));
            while Present (S) loop
               --  Visit the component instance corresponding to the
               --  subcomponent S.
               if AINU.Is_Processor (Corresponding_Instance (S)) then
                  Visit (Corresponding_Instance (S));
               end if;
               S := Next_Node (S);
            end loop;
         end if;
      end Visit_System_Instance;

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

      procedure Visit_Device_Instance (E : Node_Id) is
1402
1403
1404
1405
1406
1407
1408
         U : constant Node_Id :=
           CTN.Distributed_Application_Unit
             (CTN.Naming_Node (Backend_Node (Identifier (E))));
         P              : constant Node_Id := CTN.Entity (U);
         Implementation : Node_Id;
         S              : Node_Id;
         Entrypoint : constant Node_Id := Get_Thread_Initialize_Entrypoint (E);
1409
1410
1411
1412
1413
1414
1415
1416
1417
      begin
         Push_Entity (P);
         Push_Entity (U);

         Set_Main_Source;

         Current_Device := E;

         if Entrypoint /= No_Node then
1418
1419
            Init_Function :=
              Make_Call_Profile (Map_C_Subprogram_Identifier (Entrypoint));
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
         end if;

         Implementation := Get_Classifier_Property (E, "implemented_as");

         if Implementation /= No_Node then
            if not AINU.Is_Empty (AIN.Subcomponents (Implementation)) then
               S := First_Node (Subcomponents (Implementation));
               while Present (S) loop
                  if Get_Category_Of_Component (S) = CC_Process then
                     Visit_Process_Instance
1430
1431
1432
                       (Corresponding_Instance (S),
                        False,
                        E);
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
                  end if;

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

         Init_Function := No_Node;

         Current_Device := No_Node;

         Pop_Entity; -- U
         Pop_Entity; -- P
      end Visit_Device_Instance;

      --------------------------------------
      -- Visit_Virtual_Processor_Instance --
      --------------------------------------

      procedure Visit_Virtual_Processor_Instance (E : Node_Id) is
1453
1454
         S         : Node_Id;
         U         : Node_Id;
1455
1456
1457
1458
         Processes : List_Id;
      begin
         if Present (Backend_Node (Identifier (E))) then
            Processes := CTN.Processes (Backend_Node (Identifier (E)));
1459
            S         := AIN.First_Node (Processes);
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
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
            while Present (S) loop
               U := Current_Entity;
               Pop_Entity;
               Visit (AIN.Item (S));
               Push_Entity (U);
               S := AIN.Next_Node (S);
            end loop;
         end if;
      end Visit_Virtual_Processor_Instance;

      ------------------------------
      -- Visit_Processor_Instance --
      ------------------------------

      procedure Visit_Processor_Instance (E : Node_Id) is
         S : Node_Id;
         U : Node_Id;
         P : Node_Id;
      begin
         U := CTN.Naming_Node (Backend_Node (Identifier (E)));
         P := CTN.Entity (U);

         Push_Entity (C_Root);

         Runtime.Register_Kernel_Unit (U);

         Push_Entity (P);
         Push_Entity (U);

         if not AINU.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;

         Pop_Entity;
         Pop_Entity;
         Pop_Entity;
      end Visit_Processor_Instance;

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

      procedure Visit_Thread_Instance (E : Node_Id) is

         procedure Setup_Ports is
            F               : Node_Id;
            Call_Parameters : List_Id;
            Called_Function : Node_Id;
            Type_Used       : Node_Id;
            N               : Node_Id;
         begin
            if AINU.Is_Empty (Features (E)) then
               return;
            end if;

            F := AIN.First_Node (Features (E));
            while Present (F) loop
               Call_Parameters := New_List (CTN.K_Parameter_List);

1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
               if Kind (F) = K_Port_Spec_Instance
                 and then Is_In (F)
                 and then not Is_Out (F)
                 and then Get_Port_By_Name (F, Current_Device) = No_Node
                 and then Get_Connection_Pattern (F) = Intra_Process
               then
                  if AIN.Is_Data (F) and then not AIN.Is_Event (F) then
                     Append_Node_To_List
                       (Make_Literal
                          (CV.New_Pointed_Char_Value (Map_Port_Name (F))),
                        Call_Parameters);

                     N := CTU.Get_Data_Size (Corresponding_Instance (F));
                     Append_Node_To_List (N, Call_Parameters);

                     Append_Node_To_List
                       (Make_Variable_Address
                          (Make_Defining_Identifier (Map_Port_Var (F))),
                        Call_Parameters);

1545
                     if Use_ARINC653_API then
1546
1547
1548
1549
                        Add_Return_Variable_In_Parameters (Call_Parameters);

                        Called_Function := RF (RE_Create_Blackboard);
                        Type_Used       := RE (RE_Blackboard_Id_Type);
1550
                     else
1551
1552
                        Called_Function := RF (RE_Pok_Blackboard_Create);
                        Type_Used       := RE (RE_Uint8_T);
1553
                     end if;
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574

                     N :=
                       POK_Make_Function_Call_With_Assert
                         (Called_Function,
                          Call_Parameters);
                     Append_Node_To_List (N, Statements);

                     POK_Add_Return_Assertion (Statements);

                     Append_Node_To_List
                       (Make_Extern_Entity_Declaration
                          (Make_Variable_Declaration
                             (Defining_Identifier =>
                                (Make_Defining_Identifier (Map_Port_Var (F))),
                              Used_Type => Type_Used)),
                        CTN.Declarations (Current_File));

                  elsif AIN.Is_Data (F)
                    and then Is_Event (F)
                    and then Is_In (F)
                  then
1575
                     if Use_ARINC653_API then
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
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
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
                        Append_Node_To_List
                          (Make_Literal
                             (CV.New_Pointed_Char_Value (Map_Port_Name (F))),
                           Call_Parameters);

                        N := CTU.Get_Data_Size (Corresponding_Instance (F));
                        Append_Node_To_List (N, Call_Parameters);

                        N := Map_Queue_Size (F);
                        Append_Node_To_List (N, Call_Parameters);

                        Append_Node_To_List
                          (Map_Queueing_Policy (F),
                           Call_Parameters);

                        Append_Node_To_List
                          (Make_Variable_Address
                             (Make_Defining_Identifier (Map_Port_Var (F))),
                           Call_Parameters);

                        Add_Return_Variable_In_Parameters (Call_Parameters);

                        Called_Function := RF (RE_Create_Buffer);
                        Type_Used       := RE (RE_Buffer_Id_Type);
                     else
                        Append_Node_To_List
                          (Make_Literal
                             (CV.New_Pointed_Char_Value (Map_Port_Name (F))),
                           Call_Parameters);

                        N := Map_Queue_Size (F);
                        Append_Node_To_List (N, Call_Parameters);

                        N := CTU.Get_Data_Size (Corresponding_Instance (F));
                        Append_Node_To_List (N, Call_Parameters);

                        Append_Node_To_List
                          (Map_Queueing_Policy (F),
                           Call_Parameters);

                        Append_Node_To_List
                          (Make_Variable_Address
                             (Make_Defining_Identifier (Map_Port_Var (F))),
                           Call_Parameters);

                        Called_Function := RF (RE_Pok_Buffer_Create);
                        Type_Used       := RE (RE_Uint8_T);
                     end if;

                     N :=
                       POK_Make_Function_Call_With_Assert
                         (Called_Function,
                          Call_Parameters);
                     Append_Node_To_List (N, Statements);

                     POK_Add_Return_Assertion (Statements);

                     Append_Node_To_List
                       (Make_Extern_Entity_Declaration
                          (Make_Variable_Declaration
                             (Defining_Identifier =>
                                (Make_Defining_Identifier (Map_Port_Var (F))),
                              Used_Type => Type_Used)),
                        CTN.Declarations (Current_File));
                  elsif not AIN.Is_Data (F)
                    and then Is_Event (F)
                    and then Is_In (F)
                  then
1644
                     if Use_ARINC653_API then
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
                        Append_Node_To_List
                          (Make_Literal
                             (CV.New_Pointed_Char_Value (Map_Port_Name (F))),
                           Call_Parameters);

                        Append_Node_To_List
                          (Make_Variable_Address
                             (Make_Defining_Identifier (Map_Port_Var (F))),
                           Call_Parameters);

                        Add_Return_Variable_In_Parameters (Call_Parameters);

                        Called_Function := RF (RE_Create_Event);
                        Type_Used       := RE (RE_Event_Id_Type);
                     else
                        Append_Node_To_List
                          (Make_Variable_Address
                             (Make_Defining_Identifier (Map_Port_Var (F))),
                           Call_Parameters);

                        Called_Function := RF (RE_Pok_Event_Create);
                        Type_Used       := RE (RE_Pok_Event_Id_T);
                     end if;

                     N :=
                       POK_Make_Function_Call_With_Assert
                         (Called_Function,
                          Call_Parameters);
                     Append_Node_To_List (N, Statements);

                     POK_Add_Return_Assertion (Statements);

                     Append_Node_To_List
                       (Make_Extern_Entity_Declaration
                          (Make_Variable_Declaration
                             (Defining_Identifier =>
                                (Make_Defining_Identifier (Map_Port_Var (F))),
                              Used_Type => Type_Used)),
                        CTN.Declarations (Current_File));
                  else
                     Display_Communication_Error;
                  end if;
1687
1688
1689
1690
1691
               end if;
               F := Next_Node (F);
            end loop;
         end Setup_Ports;

1692
         Used_Member : Node_Id;
1693
1694
1695
1696
1697
1698
1699
1700
      begin
         Setup_Ports;
         Setup_Thread (E);

         Runtime.POK_Mode;

         if Get_POK_Recovery_Errors (E) /= POK_Empty_Handled_Errors then
            declare
1701
1702
1703
1704
1705
               POK_Errors : constant POK_Handled_Errors :=
                 Get_POK_Recovery_Errors (E);
               POK_Actions : constant POK_Handled_Actions :=
                 Get_POK_Recovery_Actions (E);
               POK_Error : Node_Id;
1706
1707
               --  Corresponding error code in the POK runtime.

1708
               Error : Supported_POK_Error;
1709
1710
               --  Error code from the model.

1711
               Action : Supported_POK_Action;
1712
1713
               --  Error code from the model.

1714
               Comment : Node_Id;
1715

1716
1717
               Thread_Switch_Alternatives : constant List_Id :=
                 New_List (CTN.K_Alternatives_List);
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
            begin
               Use_Error_Handling := True;

               for Error_Id in POK_Errors'Range loop
                  --  Here, for each error, we take its associated
                  --  actions and call the right function to recover
                  --  the error.

                  Error := POK_Errors (Error_Id);
                  --  This array correspond to the property
                  --  POK_Recovery_Errors.

                  Action := POK_Actions (Error_Id);
                  --  This array correspond to the property
                  --  POK_Recovery_Actions.

                  POK_Error := Map_POK_Error (Error);

                  if POK_Error /= No_Node then
                     Append_Node_To_List
1738
1739
1740
1741
                       (Make_Switch_Alternative
                          (Make_List_Id (POK_Error),
                           Make_List_Id
                             (Map_POK_Action (Action, Thread_Id, POK_Error))),
1742
1743
1744
1745
                        Thread_Switch_Alternatives);
                  end if;
               end loop;

1746
               if Use_ARINC653_API then
1747
1748
1749
1750
1751
1752
                  Used_Member := RE (RE_Error_Code);
               else
                  Used_Member := Make_Defining_Identifier (MN (M_Error_Kind));
               end if;

               Append_Node_To_List
1753
1754
1755
1756
1757
1758
1759
                 (Make_Switch_Alternative
                    (Make_List_Id
                       (Make_Literal (New_Int_Value (Thread_Id, 1, 10))),
                     Make_List_Id
                       (Make_Switch_Statement
                          (Make_Member_Designator
                             (Used_Member,
1760
1761
1762
1763
                              Make_Defining_Identifier (VN (V_Error_Status))),
                           Thread_Switch_Alternatives))),
                  Error_Switch_Alternatives);

1764
1765
               --  Here, we make a switch statement that is added
               --  to a global switch that choose the thread.