ocarina-backends-po_hi_rtsj-deployment.adb 28.6 KB
Newer Older
1
2
3
4
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
jhugues's avatar
jhugues committed
5
--                 OCARINA.BACKENDS.PO_HI_RTSJ.DEPLOYMENT                   --
6
7
8
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--       Copyright (C) 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; use Ocarina.Namet;
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
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
392
393
394
395
396
397
398
399
400
401
402
403
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
442
443
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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
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
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
718
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779

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.Utils;
with Ocarina.Backends.Messages;
with Ocarina.Backends.Properties;
with Ocarina.Backends.RTSJ_Values;
with Ocarina.Backends.RTSJ_Tree.Nutils;
with Ocarina.Backends.RTSJ_Tree.Nodes;
with Ocarina.Backends.PO_HI_RTSJ.Mapping;
with Ocarina.Backends.PO_HI_RTSJ.Runtime;

package body Ocarina.Backends.PO_HI_RTSJ.Deployment is

   use Ocarina.ME_AADL;
   use Ocarina.ME_AADL.AADL_Instances.Nodes;
   use Ocarina.ME_AADL.AADL_Instances.Nutils;
   use Ocarina.ME_AADL.AADL_Instances.Entities;

   use Ocarina.Backends.Utils;
   use Ocarina.Backends.Messages;
   use Ocarina.Backends.Properties;
   use Ocarina.Backends.RTSJ_Values;
   use Ocarina.Backends.RTSJ_Tree.Nutils;
   use Ocarina.Backends.RTSJ_Tree.Nodes;
   use Ocarina.Backends.PO_HI_RTSJ.Mapping;
   use Ocarina.Backends.PO_HI_RTSJ.Runtime;

   package AIN renames Ocarina.ME_AADL.AADL_Instances.Nodes;
   package AINU renames Ocarina.ME_AADL.AADL_Instances.Nutils;
   package RTN renames Ocarina.Backends.RTSJ_Tree.Nodes;
   package RTU renames Ocarina.Backends.RTSJ_Tree.Nutils;

   --  Global variables
   Main_Class             : Node_Id;
   Init_Statements        : List_Id;
   Class_Attributes       : List_Id;
   Node_Enumerator_List   : List_Id;
   Thread_Enumerator_List : List_Id;
   Port_Enumerator_List   : List_Id;
   Entities_Table_List    : List_Id;
   Ports_Table_List       : List_Id;

   Current_Process_Instance : Node_Id;
   Task_Offset              : Unsigned_Long_Long := 0;

   Node_Identifier       : Unsigned_Long_Long;
   Thread_Identifier     : Unsigned_Long_Long;
   Nb_Local_Threads      : Unsigned_Long_Long;
   Nb_Ports_Total        : Unsigned_Long_Long;
   Nb_Ports_In_Process   : Unsigned_Long_Long;
   Max_Payload           : Unsigned_Long_Long;

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

      function Added_Internal_Name (P : Node_Id; E : Node_Id) return Name_Id;
      function Is_Added (P : Node_Id; E : Node_Id) return Boolean;
      procedure Set_Added (P : Node_Id; E : Node_Id);

      function Need_Deliver (E : Node_Id) return Boolean;
      function Need_Send (E : Node_Id) return Boolean;

      --  Builds the maxPayloadSize constant declaration
      --  corresponding to the node E
      function Max_Payload_Size_Declaration (E : Node_Id) return Node_Id;

      -------------------------
      -- Added_Internal_Name --
      -------------------------
      function Added_Internal_Name
        (P : Node_Id; E : Node_Id)
        return Name_Id
      is
      begin
         Set_Str_To_Name_Buffer ("%add%enumerator%");
         Add_Nat_To_Name_Buffer (Nat (P));
         Add_Char_To_Name_Buffer ('%');
         Add_Nat_To_Name_Buffer (Nat (E));

         return Name_Find;
      end Added_Internal_Name;

      --------------
      -- Is_Added --
      --------------
      function Is_Added (P : Node_Id; E : Node_Id) return Boolean is
         I_Name : constant Name_Id := Added_Internal_Name (P, E);
      begin
         return Get_Name_Table_Byte (I_Name) = 1;
      end Is_Added;

      ---------------
      -- Set_Added --
      ---------------
      procedure Set_Added (P : Node_Id; E : Node_Id) is
         I_Name : constant Name_Id := Added_Internal_Name (P, E);
      begin
         Set_Name_Table_Byte (I_Name, 1);
      end Set_Added;

      ------------------
      -- Need_Deliver --
      ------------------
      function Need_Deliver (E : Node_Id) return Boolean is
         Result : Boolean := Has_In_Ports (E);
         S : Node_Id;
      begin
         pragma Assert (AINU.Is_Process (E));

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

            while Present (S) and then not Result loop
               if AINU.Is_Thread (Corresponding_Instance (S)) then
                  Result := Result
                    or else Has_In_Ports (Corresponding_Instance (S));
               end if;

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

         return Result;
      end Need_Deliver;

      ---------------
      -- Need_Send --
      ---------------
      function Need_Send (E : Node_Id) return Boolean is
         Result : Boolean := Has_Out_Ports (E);
         S : Node_Id;
      begin
         pragma Assert (AINU.Is_Process (E));

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

            while Present (S) and then not Result loop
               if AINU.Is_Thread (Corresponding_Instance (S)) then
                  Result := Result
                    or else Has_Out_Ports (Corresponding_Instance (S));
               end if;

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

         return Result;
      end Need_Send;

      ----------------------------------
      -- Max_Payload_Size_Declaration --
      ----------------------------------
      function Max_Payload_Size_Declaration
        (E : Node_Id)
        return Node_Id
      is
         pragma Unreferenced (E);

         N : Node_Id;
      begin
         --  The structure of a message payload is as follows :
         --  1 - A destination port
         --  2 - An optional timestamp
         --  3 - A data (of one of the marshallable types declared in
         --     the currently being generated file

         --  Size coresponding to (3) has been built incrementally
         --  during the visiting of AADL data component instance.
         --  Add the size corresponding to (2) and (1)

         --  16 bits is the size of the destination port
         --  64 bits is the size of the optional timestamp
         Max_Payload := Max_Payload + 16 + 64;

         N := Make_Assignment_Statement
           (Make_Pointed_Notation
              (Make_Defining_Identifier (ON (O_Context)),
               RE (RE_Max_Payload_Size)),
            Make_Literal (New_Int_Value (Max_Payload, 0, 10)));

         return N;
      end Max_Payload_Size_Declaration;

      -----------
      -- Visit --
      -----------
      procedure Visit (E : Node_Id) is
      begin
         case AIN.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 CC_Data =>
               Visit_Data_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 := RTN.Distributed_Application_Unit
           (RTN.Naming_Node (Backend_Node (Identifier (E))));
         P : constant Node_Id := RTN.Entity (U);
         Root_Sys : constant Node_Id
           := Parent_Component (Parent_Subcomponent (E));
         N : Node_Id;
         C : Node_Id;
         Q : Node_Id;
         F : Node_Id;
         Src : Node_Id;
         Dst : Node_Id;
         Parent : Node_Id;
         Spec : Node_Id;
         Impl : Node_Id;
         Init_Declarations  : constant List_Id := New_List
           (RTN.K_Declaration_List);
      begin
         pragma Assert (AINU.Is_System (Root_Sys));

         Set_Added (E, E);
         Current_Process_Instance := E;

         Push_Entity (P);
         Push_Entity (U);
         RTU.Set_Deployment_Source;

         --  Runtime and libraries imports
         Add_Import (RH (RH_Deployment));

         Class_Attributes       := New_List (K_Attribute_List);
         Init_Statements        := New_List (K_Statement_List);
         Node_Enumerator_List   := New_List (K_Enumeration_Literals);
         Thread_Enumerator_List := New_List (K_Enumeration_Literals);
         Port_Enumerator_List   := New_List (K_Enumeration_Literals);
         Entities_Table_List    := New_List (K_Enumeration_Literals);
         Ports_Table_List       := New_List (K_Enumeration_Literals);

         Node_Identifier     := 0;
         Thread_Identifier   := 0;
         Nb_Local_Threads    := 0;
         Nb_Ports_In_Process := 0;
         Nb_Ports_Total      := 0;

         if Need_Deliver (E) or else Need_Send (E) then
            Max_Payload := 0;
         end if;

         N := Message_Comment ("For each node in the distributed"
                                 & " application, add a constant");
         RTU.Append_Node_To_List (N, Node_Enumerator_List);

         Q := AIN.First_Node (Subcomponents (Root_Sys));

         while Present (Q) loop
            if AINU.Is_Process (Corresponding_Instance (Q)) then
               if Is_Added (Corresponding_Instance (Q), E) then

                  N := Make_Variable_Declaration
                    (Visibility =>
                       Make_List_Id (RE (RE_Public),
                                     RE (RE_Static),
                                     RE (RE_Final)),
                     Used_Type => New_Node (K_Int),
                     Defining_Identifier => Make_Defining_Identifier
                       (Map_RTSJ_Enumerator_Name
                          (Q)),
                     Value => Make_Literal
                       (New_Int_Value (Node_Identifier, 0, 10)));
                  RTU.Append_Node_To_List
                    (N, Node_Enumerator_List);
                  Node_Identifier := Node_Identifier + 1;

               end if;
            end if;

            Q := AIN.Next_Node (Q);
         end loop;

         N := Make_Enumerator (Node_Enumerator_List);
         RTU.Append_Node_To_List (N, Class_Attributes);

         N := Message_Comment ("For each task in the distributed"
                                 & " application, add a constant");
         RTU.Append_Node_To_List (N, Thread_Enumerator_List);

         N := Message_Comment
           ("For each thread port in the distributed application nodes, add "
            & "a number between 0 and 65536");
         RTU.Append_Node_To_List (N, Port_Enumerator_List);

         --  Visit all the subcomponents of the process
         if not AINU.Is_Empty (Subcomponents (E)) then
            C := AIN.First_Node (Subcomponents (E));

            while Present (C) loop

               --  Visit the component instance corresponding to the
               --  subcomponent S.
               Visit (Corresponding_Instance (C));
               C := AIN.Next_Node (C);
            end loop;
         end if;

         N := Make_Enumerator (Thread_Enumerator_List);
         RTU.Append_Node_To_List (N, Class_Attributes);

         N := Make_Enumerator (Port_Enumerator_List);
         RTU.Append_Node_To_List (N, Class_Attributes);

         --  For each of the processes P connected to E, (1) we add an
         --  enumerator corresponding to P and (2) for each one of the
         --  threads of P, we add an enumerator.
         if not AINU.Is_Empty (Features (E)) then
            F := AIN.First_Node (Features (E));

            while Present (F) loop

               --  The sources of F
               if not AINU.Is_Empty (AIN.Sources (F)) then
                  Src := AIN.First_Node (AIN.Sources (F));

                  while Present (Src) loop

                     Parent := Parent_Component (Item (Src));

                     if AINU.Is_Process (Parent)
                       and then Parent /= E
                     then
                        Set_Added (Parent, E);

                        --  Traverse all the subcomponents of Parent
                        if not AINU.Is_Empty (Subcomponents (Parent)) then
                           C := AIN.First_Node (Subcomponents (Parent));

                           while Present (C) loop
                              Visit (Corresponding_Instance (C));

                              C := AIN.Next_Node (C);
                           end loop;
                        end if;

                     end if;

                     Src := AIN.Next_Node (Src);
                  end loop;
               end if;

               --  The destinations of F
               if not AINU.Is_Empty (Destinations (F)) then
                  Dst := AIN.First_Node (Destinations (F));

                  while Present (Dst) loop
                     Parent := Parent_Component (Item (Dst));

                     if AINU.Is_Process (Parent)
                       and then Parent /= E
                     then
                        Set_Added (Parent, E);

                        if not AINU.Is_Empty (Subcomponents (Parent)) then
                           C := AIN.First_Node (Subcomponents (Parent));

                           while Present (C) loop
                              Visit (Corresponding_Instance (C));

                              C := AIN.Next_Node (C);
                           end loop;
                        end if;
                     end if;

                     Dst := AIN.Next_Node (Dst);
                  end loop;
               end if;

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

         --  entitiesTable declaration
         N := Make_Assignment_Statement
           (Defining_Identifier =>
              Make_Pointed_Notation
              (Left_Member =>
                 Make_Defining_Identifier (ON (O_Context)),
               Right_Member => RE (RE_Entities_Table)),
            Expression => Make_New_Statement
            (Defining_Identifier => New_Node (K_Int),
             Parameters => Make_List_Id
             (Make_Literal
              (New_Int_Value
               (Thread_Identifier, 0, 10))),
             Is_Array => True));

         N := Make_Full_Array_Declaration
           (Array_Declaration => N,
            Array_Assignments => Entities_Table_List);
         RTU.Append_Node_To_List (N, Init_Statements);

         --  portsTable declaration
         if Nb_Ports_Total /= 0 then
            N := Make_Assignment_Statement
              (Defining_Identifier =>
                 Make_Pointed_Notation
               (Left_Member =>
                  Make_Defining_Identifier (ON (O_Context)),
                Right_Member => RE (RE_Ports_Table)),
               Expression => Make_New_Statement
               (Defining_Identifier => New_Node (K_Int),
                Parameters => Make_List_Id
                (Make_Literal
                 (New_Int_Value
                  (Nb_Ports_In_Process, 0, 10))),
                Is_Array => True));
         end if;

         N := Make_Full_Array_Declaration
           (Array_Declaration => N,
            Array_Assignments => Ports_Table_List);
         RTU.Append_Node_To_List (N, Init_Statements);

         if Node_Identifier > 1 then
            N := Make_Assignment_Statement
              (Defining_Identifier =>
                 Make_Pointed_Notation
               (Make_Defining_Identifier (ON (O_Context)),
                RE (RE_Transport)),
               Expression =>
                 Make_New_Statement
                 (Defining_Identifier =>
                    Make_Defining_Identifier
                    (ON (O_Transport_High_Level_Impl))));
            RTU.Append_Node_To_List (N, Init_Statements);
         end if;

         --  entitiesOffset initialization
         N := Make_Assignment_Statement
           (Defining_Identifier =>
              Make_Pointed_Notation
              (Left_Member =>
                 Make_Defining_Identifier (ON (O_Context)),
               Right_Member => RE (RE_Entities_Offset)),
            Expression => Make_Literal
              (New_Int_Value (Task_Offset, 0, 10)));
         RTU.Append_Node_To_List (N, Init_Statements);
         Task_Offset := Task_Offset + Nb_Local_Threads;

         --  Initialization of variables of the Context class

         --  Variable : myNode
         --  Identifier of the current process
         N := Make_Assignment_Statement
           (Defining_Identifier =>
              (Make_Pointed_Notation
                 (Left_Member => Make_Defining_Identifier
                    (ON (O_Context)),
                  Right_Member => RE (RE_My_Node))),
            Expression => Make_Pointed_Notation
              (Left_Member => Make_Defining_Identifier
                 (ON (O_Deployment)),
               Right_Member => Make_Defining_Identifier
                 (Map_RTSJ_Enumerator_Name (Parent_Subcomponent (E)))));
         RTU.Append_Node_To_List (N, Init_Declarations);

         --  Variable : nbNodes
         --  Total number of processes
         N := Make_Assignment_Statement
           (Defining_Identifier =>
              Make_Pointed_Notation
              (Left_Member => Make_Defining_Identifier
                 (ON (O_Context)),
               Right_Member => RE (RE_Nb_Nodes)),
            Expression => Make_Literal
              (New_Int_Value (Node_Identifier, 0, 10)));
         RTU.Append_Node_To_List (N, Init_Declarations);

         --  Variable : nbLocalEntities
         --  Total number of threads for the current process
         N := Make_Assignment_Statement
           (Defining_Identifier =>
              Make_Pointed_Notation
              (Left_Member => Make_Defining_Identifier
                 (ON (O_Context)),
               Right_Member => RE (RE_Nb_Local_Entities)),
            Expression => Make_Literal
              (New_Int_Value (Nb_Local_Threads, 0, 10)));
         RTU.Append_Node_To_List (N, Init_Declarations);

         --  Naming initialization
         if not AINU.Is_Empty (Features (E)) then
            F := AIN.First_Node (Features (E));

            while Present (F) loop
               if Kind (F) = K_Port_Spec_Instance then
                  N := Make_Pointed_Notation
                    (Make_Defining_Identifier (ON (O_Naming)),
                     Make_Call_Function
                       (Make_Defining_Identifier
                          (MN (M_Initialization))));
                  RTU.Append_Node_To_List (N, Init_Statements);
                  exit;
               end if;
               F := AIN.Next_Node (F);
            end loop;
         end if;

         --  TransportHighLevel and maxPauloadSize constant initializations
         if Need_Deliver (E) or else Need_Send (E) then
            N := Make_Assignment_Statement
              (Make_Pointed_Notation
                 (Make_Defining_Identifier (ON (O_Context)),
                  Make_Defining_Identifier (VN (V_Transport))),
               Make_New_Statement
                 (Make_Defining_Identifier
                    (ON (O_Transport_High_Level))));
            RTU.Append_Node_To_List (N, Init_Statements);

            N := Max_Payload_Size_Declaration (E);
            RTU.Append_Node_To_List (N, Init_Declarations);
         end if;

         Spec := Make_Function_Specification
           (Visibility => Make_List_Id (RE (RE_Public),
                                        RE (RE_Static)),
            Defining_Identifier =>
              Make_Defining_Identifier (MN (M_Initialization)),
            Return_Type => New_Node (K_Void));

         Impl := Make_Function_Implementation
         (Specification => Spec,
         Declarations => Init_Declarations,
         Statements => Init_Statements);

         Main_Class := Make_Class_Statement
           (Visibility => Make_List_Id (RE (RE_Public)),
            Defining_Identifier =>
              Make_Defining_Identifier (ON (O_Deployment)),
            Attributes => Class_Attributes,
            Methods => Make_List_Id (Impl));
         RTU.Append_Node_To_List
           (Main_Class, RTN.Statements (Current_File));

         Pop_Entity;  --  U
         Pop_Entity;  --  P
      end Visit_Process_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;
      begin
         N := Make_Variable_Declaration
           (Visibility => Make_List_Id (RE (RE_Public),
                                        RE (RE_Static),
                                        RE (RE_Final)),
            Defining_Identifier => Make_Defining_Identifier
            (Map_RTSJ_Enumerator_Name (S)),
            Used_Type => New_Node (K_Int),
            Value => Make_Literal
            (New_Int_Value
             (Thread_Identifier, 0, 10)));
         RTU.Append_Node_To_List (N, Thread_Enumerator_List);

         Thread_Identifier := Thread_Identifier + 1;
         Nb_Local_Threads := Nb_Local_Threads + 1;

         if Parent_Component (Parent_Subcomponent (E))
           = Current_Process_Instance then
            --  entitiesTable assignment
            N := Make_Assignment_Statement
              (Defining_Identifier =>
                 Make_Pointed_Notation
               (Make_Defining_Identifier (ON (O_Context)),
                Make_Array_Value
                (Defining_Identifier =>
                   RE (RE_Entities_Table),
                 Array_Item =>
                   Make_Defining_Identifier
                 (Map_RTSJ_Enumerator_Name (S)))),
               Expression =>
                 Make_Defining_Identifier
               (Map_RTSJ_Enumerator_Name
                (Parent_Subcomponent
                 (Parent_Component (S)))));
            RTU.Append_Node_To_List (N, Entities_Table_List);
         end if;

         if not AINU.Is_Empty (Features (E)) then
            F := AIN.First_Node (Features (E));

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

                  if Parent_Component (Parent_Subcomponent (E))
                    = Current_Process_Instance then
                     --  portsTable enumeration
                     N := Make_Assignment_Statement
                       (Defining_Identifier =>
                          Make_Pointed_Notation
                          (Make_Defining_Identifier (ON (O_Context)),
                           Make_Array_Value
                             (Defining_Identifier => RE (RE_Ports_Table),
                              Array_Item => Make_Defining_Identifier
                                (Map_RTSJ_Enumerator_Name (F)))),
                        Expression => Make_Defining_Identifier
                          (Map_RTSJ_Enumerator_Name (S)));
                     RTU.Append_Node_To_List (N, Ports_Table_List);

                     Nb_Ports_In_Process := Nb_Ports_In_Process + 1;
                  end if;

                  --  ports enumeration
                  N := Make_Variable_Declaration
                    (Visibility => Make_List_Id (RE (RE_Public),
                                                 RE (RE_Static),
                                                 RE (RE_Final)),
                     Defining_Identifier =>
                       Make_Defining_Identifier
                       (Map_RTSJ_Enumerator_Name (F)),
                     Used_Type => New_Node (K_Int),
                     Value => Make_Literal
                       (New_Int_Value (Nb_Ports_Total, 0, 100)));
                  RTU.Append_Node_To_List (N, Port_Enumerator_List);

                  Nb_Ports_Total := Nb_Ports_Total + 1;

                  --  Visit data to compute the maxPayloadSize
                  if AIN.Is_Data (F) then
                     Visit (Corresponding_Instance (F));
                  end if;

               end if;

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

      end Visit_Thread_Instance;

      -------------------------
      -- Visit_Data_Instance --
      -------------------------
      procedure Visit_Data_Instance (E : Node_Id) is
      begin
         --  Visit the data only once
         if No (Get_Handling (E, By_Name, H_RTSJ_Deployment)) then
            if Get_Source_Language (E) = Language_RTSJ then
               Display_Located_Error
                 (AIN.Loc (E),
                  "This data type cannot be used in thread or process "
                    & "features",
                  Fatal => True);
            end if;

            if Get_Data_Representation (E) /= Data_With_Accessors then
               declare
                  Data_Size : Unsigned_Long_Long;
               begin
                  if Get_Data_Size (E) /= Null_Size then
                     Data_Size := To_Bits (Get_Data_Size (E));
                  end if;

                  if Data_Size > Max_Payload then
                     Max_Payload := Data_Size;
                  end if;
               end;

               --  Mark the data as being handled
               Set_Handling (E, By_Name, H_RTSJ_Deployment, E);
            end if;
         end if;
      end Visit_Data_Instance;

      ---------------------------
      -- Visit_System_Instance --
      ---------------------------
      procedure Visit_System_Instance (E : Node_Id) is
         N : Node_Id;
      begin
         Push_Entity (RTSJ_Root);

         if not AINU.Is_Empty (Subcomponents (E)) then
            N := AIN.First_Node (Subcomponents (E));
            while Present (N) loop
               Visit (Corresponding_Instance (N));
               N := AIN.Next_Node (N);
            end loop;
         end if;

         Pop_Entity;
      end Visit_System_Instance;

   end Source_File;

end Ocarina.Backends.PO_HI_RTSJ.Deployment;