ocarina-backends-po_hi_c-naming.adb 24.3 KB
Newer Older
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--      O C A R I N A . B A C K E N D S . P O _ H I _ C . N A M I N G       --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--               Copyright (C) 2008-2010, GET-Telecom Paris.                --
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
--                                                                          --
-- Ocarina  is free software;  you  can  redistribute  it and/or  modify    --
-- it under terms of the GNU General Public License as published by the     --
-- Free Software Foundation; either version 2, or (at your option) any      --
-- later version. Ocarina is distributed  in  the  hope  that it will be    --
-- useful, but WITHOUT ANY WARRANTY;  without even the implied warranty of  --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General --
-- Public License for more details. You should have received  a copy of the --
-- GNU General Public License distributed with Ocarina; see file COPYING.   --
-- If not, write to the Free Software Foundation, 51 Franklin Street, Fifth --
-- Floor, Boston, MA 02111-1301, USA.                                       --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable to be   --
-- covered  by the  GNU  General  Public  License. This exception does not  --
-- however invalidate  any other reasons why the executable file might be   --
-- covered by the GNU Public License.                                       --
--                                                                          --
--                 Ocarina is maintained by the Ocarina team                --
--                       (ocarina-users@listes.enst.fr)                     --
--                                                                          --
------------------------------------------------------------------------------

with Namet;
35
with Utils; use Utils;
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
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.Properties;
with Ocarina.Backends.Utils;
with Ocarina.Backends.Messages;
with Ocarina.Backends.C_Common.Mapping;
with Ocarina.Backends.PO_HI_C.Runtime;
with Ocarina.Backends.C_Tree.Nodes;
with Ocarina.Backends.C_Values;
with Ocarina.Backends.C_Tree.Nutils;

package body Ocarina.Backends.PO_HI_C.Naming is

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

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

   -----------------
   -- Header_File --
   -----------------

   package body Header_File is

      procedure Visit_Architecture_Instance (E : Node_Id);
      procedure Visit_Component_Instance (E : Node_Id);
      procedure Visit_System_Instance (E : Node_Id);
      procedure Visit_Process_Instance (E : Node_Id);

      -----------
      -- Visit --
      -----------

      procedure Visit (E : Node_Id) is
      begin
         case Kind (E) is
            when K_Architecture_Instance =>
               Visit_Architecture_Instance (E);

            when K_Component_Instance =>
               Visit_Component_Instance (E);

            when others =>
               null;
         end case;
      end Visit;

      ---------------------------------
      -- Visit_Architecture_Instance --
      ---------------------------------

      procedure Visit_Architecture_Instance (E : Node_Id) is
      begin
         Visit (Root_System (E));
      end Visit_Architecture_Instance;

      ------------------------------
      -- Visit_Component_Instance --
      ------------------------------

      procedure Visit_Component_Instance (E : Node_Id) is
         Category : constant Component_Category
           := Get_Category_Of_Component (E);
      begin
         case Category is
            when CC_System =>
               Visit_System_Instance (E);

            when CC_Process =>
               Visit_Process_Instance (E);

            when others =>
               null;
         end case;
      end Visit_Component_Instance;

      ----------------------------
      -- Visit_Process_Instance --
      ----------------------------

      procedure Visit_Process_Instance (E : Node_Id) is
         P                 : constant Node_Id := Map_HI_Node (E);
         U                 : Node_Id;
         Root_Sys          : constant Node_Id
           := Parent_Component (Parent_Subcomponent (E));
         Platform          : constant Supported_Execution_Platform
           := Get_Execution_Platform (Get_Bound_Processor (E));
      begin
         pragma Assert (AAU.Is_System (Root_Sys));

         if Platform = Platform_None then
            Display_Located_Error
              (Loc (Parent_Subcomponent (E)),
               "This process subcomponent is bound to a processor without"
               & " execution platform specification",
               Fatal => True);
         end if;

         Push_Entity (P);
         U := Map_HI_Unit (E);
         Push_Entity (U);

         Set_Naming_Header (U);

         Pop_Entity;
         Pop_Entity;

      end Visit_Process_Instance;

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

      procedure Visit_System_Instance (E : Node_Id) is
         A : constant Node_Id := Map_Distributed_Application (E);
         C : Node_Id;
         S : Node_Id;
      begin
         C_Root := A;
         Push_Entity (A);

         --  Verify the consistency of the distributed application
         --  hierachy.

         if not AAU.Is_Empty (Connections (E)) then
            C := First_Node (Connections (E));
            while Present (C) loop
               Check_Connection_Consistency (C);

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

         --  Visit all the subcomponents of the system

         if not AAU.Is_Empty (Subcomponents (E)) then
            S := First_Node (Subcomponents (E));
            while Present (S) loop
               --  Visit the component instance corresponding to the
               --  subcomponent S.

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

         Pop_Entity; --  A
      end Visit_System_Instance;

   end Header_File;

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

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

      Inetport_Enumerator_List : Node_Id;
      Inetaddr_Enumerator_List : 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 ("%naming%info%");
         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;

      -------------------------------
      -- Socket_Naming_Information --
      -------------------------------

      procedure Socket_Naming_Information (E : Node_Id) is
         Location    : Name_Id;
         Port_Number : Value_Id;
         L           : Node_Id;
         P           : Node_Id;
      begin
         pragma Assert (AAU.Is_Process (E));

         Location := Get_Location (Get_Bound_Processor (E));
         Port_Number := Get_Port_Number (E);

         --  If the node does not have a port number, we don't assign
         --  information to it.

         if  Port_Number = CV.No_Value then
            L := RE (RE_Noaddr);
            P := RE (RE_Noport);
         else
            --  Every node that has a port number must be bound to a
            --  processor that have a location.

            if Location = No_Name then
               Display_Located_Error
                 (Loc (Parent_Subcomponent (E)),
                  "A process that has a port number must be bound"
                  & " to a processor that has a location",
                  Fatal => True);
            end if;

            L := Make_Literal
              (CV.New_Pointed_Char_Value
               (Location));

            P := Make_Literal
              (CV.To_C_Value
               (Port_Number));
         end if;

         Append_Node_To_List (P, CTN.Values (Inetport_Enumerator_List));
         Append_Node_To_List (L, CTN.Values (Inetaddr_Enumerator_List));
      end Socket_Naming_Information;

      -----------
      -- Visit --
      -----------

      procedure Visit (E : Node_Id) is
      begin
         case Kind (E) is
            when K_Architecture_Instance =>
               Visit_Architecture_Instance (E);

            when K_Component_Instance =>
               Visit_Component_Instance (E);

            when others =>
               null;
         end case;
      end Visit;

      ---------------------------------
      -- Visit_Architecture_Instance --
      ---------------------------------

      procedure Visit_Architecture_Instance (E : Node_Id) is
      begin
         Visit (Root_System (E));
      end Visit_Architecture_Instance;

      ------------------------------
      -- Visit_Component_Instance --
      ------------------------------

      procedure Visit_Component_Instance (E : Node_Id) is
         Category : constant Component_Category
           := Get_Category_Of_Component (E);
      begin
         case Category is
            when CC_System =>
               Visit_System_Instance (E);

            when CC_Process =>
               Visit_Process_Instance (E);

            when others =>
               null;
         end case;
      end Visit_Component_Instance;

      ----------------------------
      -- Visit_Process_Instance --
      ----------------------------

      procedure Visit_Process_Instance (E : Node_Id) is
         U        : constant Node_Id := CTN.Distributed_Application_Unit
           (CTN.Naming_Node (Backend_Node (Identifier (E))));
         P        : constant Node_Id := CTN.Entity (U);
         N        : Node_Id;
         S                 : Node_Id;
         F                 : Node_Id;
         B                 : Node_Id;
         C                 : Node_Id;
         C_End             : Node_Id;
         End_List          : List_Id;
         Parent            : Node_Id;
360
361
         Accessing_Device  : Node_Id;
         Driver_Name       : Name_Id;
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
         Root_Sys          : constant Node_Id
           := Parent_Component (Parent_Subcomponent (E));
         Transport_API     : Supported_Transport_APIs := Transport_None;
         My_Node           : Node_Id;
      begin
         Push_Entity (P);
         Push_Entity (U);

         Inetport_Enumerator_List     := Make_Array_Values;
         Inetaddr_Enumerator_List     := Make_Array_Values;

         Set_Naming_Source;

         Set_Added (E, E);

         My_Node := CTU.Make_Defining_Identifier
            (Map_C_Enumerator_Name
               (Parent_Subcomponent (E)));

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

            while Present (F) loop

               --  We make two iteration to traverse (1) the sources
               --  of F then (2) the destinations of F.

               End_List := Sources (F);

               for I in Boolean'Range loop
                  if not AAU.Is_Empty (End_List) then
                     C_End := First_Node (End_List);

                     while Present (C_End) loop

                        Parent := Parent_Component (Item (C_End));

                        if AAU.Is_Process (Parent) then
                           if Parent /= E then
                              --  Mark the parent component of the
                              --  remote feature as involved with the
                              --  current process.

                              Set_Added (Parent, E);
                           end if;

                           --  Get the connection involving C_End

                           C := Extra_Item (C_End);

                           if No (C) then
                              --  There hasbeen definitly a bug while
                              --  expanding connections.

                              raise Program_Error with
                                "Wrong expansion of connections";
                           end if;

                           --  Get the bus of the connection

                           B := Get_Bound_Bus (C);

                           --  Get the transport layer of the Bus and
                           --  verify that all the features use the
                           --  same transport layer for thir
                           --  connections.

                           if Transport_API /= Transport_None and then
430
                             Transport_API /= Get_Transport_API (B, E)
431
432
433
434
435
436
437
438
439
                           then
                              Display_Located_Error
                                (Loc (Parent_Subcomponent (E)),
                                 "The features of this process are involved"
                                 & " in connetions that do not use the same"
                                 & " transport layer. This is not supported"
                                 & " for now.",
                                 Fatal => True);
                           else
440
                              Transport_API := Get_Transport_API (B, E);
441
442
443
444
445
446
447
448
449
450
451
452
453

                              --  If we have a bus for which no
                              --  transport layer has been specified,
                              --  we raise an error.

                              if Transport_API = Transport_None then
                                 Display_Located_Error
                                   (Loc (B),
                                    "No transport layer has been specified"
                                    & " for this bus",
                                    Fatal => True);
                              end if;
                           end if;
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

                           case Transport_API is
                              when Transport_BSD_Sockets =>
                                 Set_Deployment_Header;

                                 Add_Define_Deployment
                                    (RE (RE_Need_Driver_Sockets));

                              when Transport_User =>
                                 if AAU.Is_Bus (B) then
                                    Accessing_Device :=
                                       Get_Device_Of_Process (B, E);
                                 elsif AAU.Is_Virtual_Bus (B) then
                                    Accessing_Device := Get_Device_Of_Process
                                       (Parent_Component
                                          (Parent_Subcomponent (B)), E);
                                 else
                                    Display_Located_Error
                                       (Loc (B),
                                        "Unknown bus kind !",
                                        Fatal => True);
                                 end if;

                                 if Accessing_Device = No_Node then
                                    Display_Located_Error
                                       (Loc (B),
                                        "No device is accessing this bus !",
                                        Fatal => True);
                                 end if;

                                 Driver_Name := Get_Driver_Name
                                    (Corresponding_Instance
                                       (Accessing_Device));

                                 if Driver_Name = No_Name then
                                    Display_Located_Error
                                       (Loc (B),
                                        "Driver must have a name" &
                                        "(see Deployment::Driver_Name) !",
                                        Fatal => True);
                                 end if;

                                 Set_Str_To_Name_Buffer
                                    ("__PO_HI_NEED_DRIVER_");
                                 Get_Name_String_And_Append (Driver_Name);

                                 Driver_Name := Name_Find;
                                 Driver_Name := To_Upper (Driver_Name);

                                 Add_Define_Deployment
                                    (Make_Defining_Identifier
                                       (Driver_Name,
                                       C_Conversion => False));

                              when others =>
                                 null;
                           end case;
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
                        end if;

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

                  --  In the next iteration, we traverse the
                  --  Destinations of F.

                  End_List := Destinations (F);
               end loop;

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

         Bind_Transport_API (E, Transport_API);

         case Transport_API is
            when Transport_BSD_Sockets =>
               --  Build the node information for all the application
               --  nodes involved with the current one and append it
               --  to the naming list.

535
536
               Set_Deployment_Header;

537
538
               Add_Define_Deployment
                  (RE (RE_Need_Driver_Sockets));
539
540
541
542
543
544
545

               Set_Naming_Source;

               --  Here, by default, we activate the socket
               --  transport layer. This is the default behavior
               --  if no device is specified for transport concerns.

546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
               S := First_Node (Subcomponents (Root_Sys));

               while Present (S) loop
                  if AAU.Is_Process (Corresponding_Instance (S))
                    and then Is_Added (Corresponding_Instance (S), E)
                  then
                     Socket_Naming_Information
                       (Corresponding_Instance (S));
                  end if;

                  S := Next_Node (S);
               end loop;

               --  Declare the Naming Table

               N := Message_Comment ("Naming Table");
               Append_Node_To_List (N, CTN.Declarations (Current_File));

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
            when Transport_User =>
               if AAU.Is_Bus (B) then
                  Accessing_Device := Get_Device_Of_Process (B, E);
               elsif AAU.Is_Virtual_Bus (B) then
                  Accessing_Device := Get_Device_Of_Process
                     (Parent_Component (Parent_Subcomponent (B)), E);
               else
                  Display_Located_Error
                     (Loc (B),
                      "Unknown bus kind !",
                      Fatal => True);
               end if;

               if Accessing_Device = No_Node then
                  Display_Located_Error
                     (Loc (B),
                      "No device is accessing this bus !",
                      Fatal => True);
               end if;

               Driver_Name := Get_Driver_Name
                  (Corresponding_Instance (Accessing_Device));

               if Driver_Name = No_Name then
                  Display_Located_Error
                     (Loc (B),
                      "Driver must have a name" &
                      "(see Deployment::Driver_Name) !",
                      Fatal => True);
               end if;

               Set_Deployment_Header;
596
               Set_Str_To_Name_Buffer ("__PO_HI_NEED_DRIVER_");
597
598
599
600
601
               Get_Name_String_And_Append (Driver_Name);

               Driver_Name := Name_Find;
               Driver_Name := To_Upper (Driver_Name);

602
603
604
605
               Add_Define_Deployment
                  (Make_Defining_Identifier
                     (Driver_Name,
                     C_Conversion => False));
606
607
608

               Set_Naming_Source;

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
            when others =>
               --  If we did not fetch a meaningful transport layer,
               --  this means the application does not use the
               --  network. No naming table will be generated.

               null;
         end case;

         N := Make_Expression
           (Left_Expr =>
              Make_Variable_Declaration
              (Defining_Identifier =>
                 Make_Array_Declaration
              (Defining_Identifier =>
                 Make_Defining_Identifier
                 (VN (V_Node_Port)),
               Array_Size =>
                 RE (RE_Nb_Nodes)),
               Used_Type =>
                 RE (RE_Inetport_T)),
            Operator => Op_Equal,
            Right_Expr =>
              Inetport_Enumerator_List);

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

         N := Make_Expression
           (Left_Expr =>
              Make_Variable_Declaration
              (Defining_Identifier =>
                 Make_Array_Declaration
              (Defining_Identifier =>
                 Make_Defining_Identifier
                 (VN (V_Node_Addr)),
               Array_Size =>
                 RE (RE_Nb_Nodes)),
               Used_Type =>
                 RE (RE_Inetaddr_T)),
            Operator => Op_Equal,
            Right_Expr =>
              Inetaddr_Enumerator_List);
         Append_Node_To_List
           (N, CTN.Declarations (Current_File));

         N := Make_Expression
           (Left_Expr =>
              Make_Variable_Declaration
              (Defining_Identifier =>
                 Make_Defining_Identifier
                 (VN (V_Mynode)),
               Used_Type =>
                 RE (RE_Node_T)),
            Operator => Op_Equal,
            Right_Expr => My_Node);
         Append_Node_To_List (N, CTN.Declarations (Current_File));

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

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

      procedure Visit_System_Instance (E : Node_Id) is
         S : Node_Id;
      begin
         Push_Entity (C_Root);

         --  Visit all the subcomponents of the system

         if not AAU.Is_Empty (Subcomponents (E)) then
            S := First_Node (Subcomponents (E));
            while Present (S) loop
               --  Visit the component instance corresponding to the
               --  subcomponent S.

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

         Pop_Entity; --  C_Root
      end Visit_System_Instance;

   end Source_File;

end Ocarina.Backends.PO_HI_C.Naming;