ocarina-backends-c_common-ba.adb 217 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--         O C A R I N A . B A C K E N D S . C _ C O M M O N . B A          --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                     Copyright (C) 2019 ESA & ISAE.                       --
--                                                                          --
-- Ocarina  is free software; you can redistribute it and/or modify under   --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. Ocarina is distributed in the hope that it will be useful, but     --
-- WITHOUT ANY WARRANTY; without even the implied warranty of               --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
--                                                                          --
------------------------------------------------------------------------------

with Ocarina.Namet;
33
with Locations;
34
35
with Utils;
with Ocarina.Backends.Utils;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

with Ocarina.Backends.Messages;
with Ocarina.Backends.C_Tree.Nodes;
with Ocarina.Backends.C_Tree.Nutils;
with Ocarina.Backends.C_Values;
with Ocarina.Backends.Properties;
with Ocarina.Backends.C_Common.Mapping;

with Ocarina.ME_AADL;
with Ocarina.ME_AADL.AADL_Tree.Nodes;
with Ocarina.ME_AADL_BA;
with Ocarina.ME_AADL.AADL_Instances.Nodes;
with Ocarina.ME_AADL.AADL_Instances.Nutils;

with Ocarina.ME_AADL_BA.BA_Tree.Nodes;
with Ocarina.ME_AADL_BA.BA_Tree.Nutils;
with Ocarina.Backends.Helper;
with Ocarina.Analyzer.AADL_BA;
54
with Ocarina.Backends.PO_HI_C.Runtime;
55
with Ocarina.Backends.C_Common.Types;
56
57
58
59
60
61

package body Ocarina.Backends.C_Common.BA is

   use Ocarina.Analyzer.AADL_BA;
   use Ocarina.Backends.C_Tree.Nutils;
   use Ocarina.Namet;
62
   use Locations;
63
64
65
66
67
68
69
   use Ocarina.Backends.Helper;
   use Ocarina.Backends.Messages;
   use Ocarina.Backends.Properties;
   use Ocarina.Backends.C_Tree.Nodes;
   use Ocarina.ME_AADL_BA;
   use Ocarina.ME_AADL_BA.BA_Tree.Nodes;
   use Ocarina.Backends.C_Common.Mapping;
70
   use Ocarina.Backends.Utils;
71
   use Ocarina.Backends.PO_HI_C.Runtime;
72
   use Ocarina.ME_AADL;
73
74
75

   package AAN renames Ocarina.ME_AADL.AADL_Tree.Nodes;
   package AIN renames Ocarina.ME_AADL.AADL_Instances.Nodes;
76
   package AINU renames Ocarina.ME_AADL.AADL_Instances.Nutils;
77
78
79
80
81
82
83
84
85
   package CTN renames Ocarina.Backends.C_Tree.Nodes;
   package CTU renames Ocarina.Backends.C_Tree.Nutils;
   package BATN renames Ocarina.ME_AADL_BA.BA_Tree.Nodes;
   package BANu renames Ocarina.ME_AADL_BA.BA_Tree.Nutils;
   package CV renames Ocarina.Backends.C_Values;

   --  function Get_Instances_Of_Component_Type
   --    (Root : Node_Id; E : Node_Id) return Node_Id;

86
87
   function Map_Used_Type (N : Node_Id) return Node_Id;

88
89
90
91
92
   procedure Map_C_Many_Transitions_Of_A_Thread
     (S            : Node_Id;
      Declarations : List_Id;
      Statements   : List_Id);

93
94
95
   function Compute_Nb_States
     (S            : Node_Id) return Unsigned_Long_Long;

96
97
98
   procedure Map_BA_States_To_C_Types
     (S            : Node_Id);

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
   function Compute_Nb_Trans_Stemmed_From_A_Specific_Complete_Stat
     (BA             : Node_Id;
      Complete_State : Node_Id) return Unsigned_Long_Long;

   procedure Fill_Nb_Dispatch_Triggers_Of_Each_Transition_Array
     (S               : Node_Id;
      BA              : Node_Id;
      Complete_State  : Node_Id;
      Index_Comp_Stat : Unsigned_Long_Long;
      WStatements     : List_Id);

   procedure Fill_Dispatch_Triggers_Of_All_Transitions_Array
     (S               : Node_Id;
      BA              : Node_Id;
      Complete_State  : Node_Id;
      Index_Comp_Stat : Unsigned_Long_Long;
      WStatements     : List_Id);

   function Nb_All_Dispatch_Triggers_From_A_Specific_Complete_Stat
     (BA             : Node_Id;
      Complete_State : Node_Id) return Unsigned_Long_Long;

   function Max_Dispatch_Triggers_Per_Trans_From_A_Specific_Complete_Stat
     (BA             : Node_Id;
      Complete_State : Node_Id) return Unsigned_Long_Long;
124
125
126
127

   procedure Make_States_Initialization_Function
     (S            : Node_Id);

128
129
130
   procedure Make_Update_Next_Complete_State_Function
     (S            : Node_Id);

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
   function Search_State_Kind
     (BA        : Node_Id;
      State_Idt : Node_Id) return Ocarina.Types.Byte;

   function Find_Index_In_States_Array
     (BA        : Node_Id;
      State_Idt : Node_Id) return Unsigned_Long_Long;

   procedure Update_Current_State
     (E                : Node_Id;
      BA               : Node_Id;
      Transition_Node  : Node_Id;
      Stats            : List_Id);

   function Map_C_Transition_Node
     (Node             : Node_Id;
      S                : Node_Id;
      Declarations     : List_Id;
      Statements       : List_Id;
      Else_St          : List_Id) return List_Id;

152
153
154
155
156
   function Map_C_On_Dispatch_Transition_Node
     (Node             : Node_Id;
      S                : Node_Id;
      Declarations     : List_Id;
      Statements       : List_Id;
157
      Index_Transition : in out Unsigned_Long_Long) return List_Id;
158

159
160
161
162
163
164
165
166
   procedure Map_C_A_List_Of_Transitions
     (S                         : Node_Id;
      BA                        : Node_Id;
      Otherwise_Transition_Node : Node_Id;
      Sub_Transition_List       : List_Id;
      WDeclarations             : List_Id;
      WStatements               : List_Id);

167
   procedure Map_C_A_List_Of_On_Dispatch_Transitions
168
169
170
171
172
     (S                            : Node_Id;
      BA                           : Node_Id;
      Sub_Transition_List          : List_Id;
      WDeclarations                : List_Id;
      WStatements                  : List_Id);
173

174
175
176
177
178
179
180
181
182
183
184
185
186
187
   procedure Examine_Current_State_Until_Reaching_Complete_State
     (S                         : Node_Id;
      BA                        : Node_Id;
      WDeclarations             : List_Id;
      WStatements               : List_Id);

   procedure Make_BA_Initialization_Function
     (S            : Node_Id);

   procedure Map_C_Implementation_of_BA_Body_Function
     (S            : Node_Id;
      Declarations : List_Id;
      Statements   : List_Id);

188
189
190
191
192
193
   procedure Make_BA_Body_Function_For_Periodic_Thread
     (S            : Node_Id;
      Declarations : List_Id;
      Statements   : List_Id);

   procedure Make_BA_Body_Function_For_Sporadic_Thread
194
195
196
197
198
199
200
     (S            : Node_Id;
      Declarations : List_Id;
      Statements   : List_Id);

   function Map_C_State_Kind_Name
     (State_Kind : Ocarina.Types.Byte) return Node_Id;

201
202
203
204
205
   procedure Map_C_Behavior_Action_Block
     (Node         : Node_Id;
      S            : Node_Id;
      Declarations : List_Id;
      Statements   : List_Id);
206

207
208
209
210
211
   procedure Map_C_Behav_Acts
     (Node         : Node_Id;
      S            : Node_Id;
      Declarations : List_Id;
      WStatements  : List_Id);
212
213

   procedure Map_C_Behavior_Action
214
215
216
217
     (Node         : Node_Id;
      S            : Node_Id;
      Declarations : List_Id;
      Statements   : List_Id);
218

219
220
221
222
223
224
225
   function Map_C_Elsif_stat
     (Node         : Node_Id;
      S            : Node_Id;
      Declarations : List_Id;
      Statements   : List_Id;
      Else_st      : Node_Id) return List_Id;

226
   procedure Map_C_If_Cond_Struct
227
228
229
230
     (Node         : Node_Id;
      S            : Node_Id;
      Declarations : List_Id;
      Statements   : List_Id);
231

232
   procedure Map_C_For_or_ForAll_Cond_Struct
233
234
235
236
237
     (Node         : Node_Id;
      S            : Node_Id;
      Declarations : List_Id;
      Statements   : List_Id);

238
239
240
241
242
243
   procedure Map_C_While_Cond_Struct
     (Node         : Node_Id;
      S            : Node_Id;
      Declarations : List_Id;
      Statements   : List_Id);

244
   procedure Map_C_Communication_Action
245
246
247
248
     (Node         : Node_Id;
      S            : Node_Id;
      Declarations : List_Id;
      Statements   : List_Id);
249

250
251
252
253
254
255
256
257
258
259
260
   procedure Make_Send_Output_Port
     (Node       : Node_Id;
      S          : Node_Id;
      Statements : List_Id);

   procedure Make_Put_Value_On_port
     (Node         : Node_Id;
      S            : Node_Id;
      Declarations : List_Id;
      Statements   : List_Id);

261
   procedure Make_Output_Port_Name
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
     (Node         : Node_Id;
      S            : Node_Id;
      Statements   : List_Id);

   function Map_C_Data_Component_Reference
     (Node             : Node_Id;
      Is_Out_Parameter : Boolean := False;
      S                : Node_Id;
      Declarations     : List_Id;
      Statements       : List_Id) return Node_Id;

   function Map_C_BA_Name
     (Node             : Node_Id;
      Is_Out_Parameter : Boolean := False;
      S                : Node_Id;
      Declarations     : List_Id;
      Statements       : List_Id) return Node_Id;

   function Map_C_Target
     (Node         : Node_Id;
      S            : Node_Id;
      Declarations : List_Id;
      Statements   : List_Id) return Node_Id;
285

286
   procedure Map_C_Assignment_Action
287
288
289
290
     (Node         : Node_Id;
      S            : Node_Id;
      Declarations : List_Id;
      Statements   : List_Id);
291
292
293
294

   function Evaluate_BA_Value_Expression
     (Node             : Node_Id;
      Is_Out_parameter : Boolean := False;
295
296
297
      Subprogram_Root  : Node_Id := No_Node;
      Declarations     : List_Id;
      Statements       : List_Id) return Node_Id;
298
299
300
301

   function Evaluate_BA_Relation
     (Node             : Node_Id;
      Is_Out_parameter : Boolean := False;
302
303
304
      Subprogram_Root  : Node_Id := No_Node;
      Declarations     : List_Id;
      Statements       : List_Id) return Node_Id;
305
306
307

   function Evaluate_BA_Operator (Node : Node_Id) return Operator_Type;

308
      function Evaluate_BA_Simple_Expression
309
310
     (Node             : Node_Id;
      Is_Out_parameter : Boolean := False;
311
312
313
      Subprogram_Root  : Node_Id := No_Node;
      Declarations     : List_Id;
      Statements       : List_Id) return Node_Id;
314
315
316
317

   function Evaluate_BA_Term
     (Node             : Node_Id;
      Is_Out_parameter : Boolean := False;
318
319
320
      Subprogram_Root  : Node_Id := No_Node;
      Declarations     : List_Id;
      Statements       : List_Id) return Node_Id;
321
322
323
324

   function Evaluate_BA_Factor
     (Node             : Node_Id;
      Is_Out_parameter : Boolean := False;
325
326
327
      Subprogram_Root  : Node_Id := No_Node;
      Declarations     : List_Id;
      Statements       : List_Id) return Node_Id;
328
329
330
331

   function Evaluate_BA_Value
     (Node             : Node_Id;
      Is_Out_parameter : Boolean := False;
332
333
334
      Subprogram_Root  : Node_Id := No_Node;
      Declarations     : List_Id;
      Statements       : List_Id) return Node_Id;
335

336
337
   function Evaluate_BA_Integer_Value
     (Node         : Node_Id;
338
      S            : Node_Id;
339
340
341
      Declarations : List_Id;
      Statements   : List_Id) return Node_Id;

342
343
   function Evaluate_BA_Literal (Node : Node_Id) return Node_Id;

344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
   procedure Make_Intermediate_Variable_Declaration
     (N            : Name_Id;
      Used_Type    : Node_Id;
      Declarations : List_Id);

   function Get_Port_Spec_Instance
     (Node             : Node_Id;
      Parent_Component : Node_Id) return Node_Id;

   function Evaluate_BA_Value_Variable
     (Node             : Node_Id;
      S                : Node_Id;
      Declarations     : List_Id;
      Statements       : List_Id) return Node_Id;

359
360
361
   function Evaluate_BA_Property_Constant
     (Node             : Node_Id;
      Is_Out_parameter : Boolean := False;
362
      Subprogram_Root  : Node_Id;
363
364
365
      Declarations     : List_Id;
      Statements       : List_Id) return Node_Id;

366
367
368
369
370
371
372
   function Make_Call_Parameter_For_Get_Count_and_Next_Value
     (Node : Node_Id;
      S    : Node_Id) return List_Id;

   function Make_Get_Count_of_Port
     (Node             : Node_Id;
      S                : Node_Id) return Node_Id;
373

374
375
   procedure Make_Next_Value_of_Port
     (Node             : Node_Id;
376
      S                : Node_Id;
377
378
      Statements       : List_Id);

379
380
381
   function Make_Request_Variable_Name_From_Port_Name
     (Port_Name : Name_Id) return Name_Id;

382
   procedure Make_Request_Variable_Declaration
383
384
     (Declarations : List_Id;
      Port_Name    : Name_Id);
385
386
387
388
389
390
391

   function Make_Get_Value_of_Port
     (Node             : Node_Id;
      Subprogram_Root  : Node_Id;
      Declarations     : List_Id;
      Statements       : List_Id) return Node_Id;

392
393
394
   function Evaluate_BA_Identifier
     (Node             : Node_Id;
      Is_Out_parameter : Boolean := False;
395
396
397
      Subprogram_Root  : Node_Id := No_Node;
      Declarations     : List_Id;
      Statements       : List_Id) return Node_Id;
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

   function Evaluate_BA_Boolean_Literal (Node : Node_Id) return Node_Id;

   --     -------------------------------------
   --     -- Get_Instances_Of_Component_Type --
   --     -------------------------------------
   --
   --     function Get_Instances_Of_Component_Type
   --       (Root : Node_Id; E : Node_Id) return Node_Id
   --     is
   --        use type Aan.Node_Kind;
   --
   --        pragma Assert
   --          (AAN.Kind (E) = Aan.K_Component_Type
   --           or else AAN.Kind (E) = Aan.K_Component_Implementation
   --           or else AAN.Kind (E) = Aan.K_Feature_Group_Type);
   --
   --        Fs : constant Ocarina.ME_AADL.AADL_Instances.Nutils.Node_Array
   --          := Features_Of (Root);
   --     begin
   --        for F of Fs loop
   --           if AIN.Corresponding_Declaration
   --             (AIN.Corresponding_Instance (F)) = E
   --           then
   --              return AIN.Corresponding_Instance (F);
   --           end if;
   --
   --        end loop;
   --
   --        --  raise Program_Error;
   --        return No_Node;
   --
   --     end Get_Instances_Of_Component_Type;

432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
   ------------------------------
   -- Is_To_Make_Init_Sequence --
   ------------------------------

   function Is_To_Make_Init_Sequence (S : Node_Id) return Boolean
   is
      BA     : Node_Id;
      State  : Node_Id;
      Result : Boolean := False;
   begin
      BA := Get_Behavior_Specification (S);
      State := BATN.First_Node (BATN.States (BA));

      while Present (State) loop

         Result := Behavior_State_Kind'Val (BATN.State_Kind (State))
           = BSK_Initial;

         exit when Result;
         State := BATN.Next_Node (State);
      end loop;
      return Result;
   end Is_To_Make_Init_Sequence;

456
457
458
   --------------------------------
   -- Get_Behavior_Specification --
   --------------------------------
459

460
   function Get_Behavior_Specification
461
462
463
464
465
466
     (S : Node_Id) return Node_Id is
      D, BA  : Node_Id;
   begin
      D := AIN.First_Node (AIN.Annexes (S));
      BA := No_Node;
      while No (BA) loop
467
468
469
         if (Standard.Utils.To_Upper (AIN.Display_Name (AIN.Identifier (D))) =
               Standard.Utils.To_Upper (Get_String_Name
                                          ("behavior_specification")))
470
471
472
473
474
475
476
477
478
479
480
           and then Present (AIN.Corresponding_Annex (D))
         then
            BA := AIN.Corresponding_Annex (D);
            return BA;
         end if;
         D := AIN.Next_Node (D);
      end loop;

      raise Program_Error;

      return No_Node;
481
   end Get_Behavior_Specification;
482

483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
   -------------------
   -- Map_Used_Type --
   -------------------

   function Map_Used_Type (N : Node_Id) return Node_Id
   is
      Data_Instance : Node_Id;
   begin
      Data_Instance := AAN.Default_Instance (N);

      if No (AIN.Backend_Node (AIN.Identifier (Data_Instance))) then
         Ocarina.Backends.C_Common.Types.Header_File.Visit (Data_Instance);
      end if;

      return Map_C_Data_Type_Designator (Data_Instance);

   end Map_Used_Type;

501
502
503
504
   ------------------------------
   -- Map_C_Behavior_Variables --
   ------------------------------

505
506
507
   procedure Map_C_Behavior_Variables (S            : Node_Id;
                                       Declarations : List_Id)
   is
508
      BA, P, T      : Node_Id;
509
510
   begin

511
      BA := Get_Behavior_Specification (S);
512
513
514
515
516
      if not BANu.Is_Empty (BATN.Variables (BA)) then
         P := BATN.First_Node (BATN.Variables (BA));
         loop
            T := BATN.First_Node (BATN.Identifiers (P));
            loop
517
518
               if Present (BATN.Corresponding_Declaration
                           (BATN.Classifier_Ref (P)))
519
520
521
                 and then Present (AAN.Default_Instance
                                   (BATN.Corresponding_Declaration
                                      (BATN.Classifier_Ref (P))))
522
               then
523
524
525
526
527

                  CTU.Append_Node_To_List
                    (CTU.Make_Variable_Declaration
                       (Defining_Identifier => CTU.Make_Defining_Identifier
                            (BATN.Display_Name (T)),
528
529
530
                        Used_Type =>  Map_Used_Type
                          (BATN.Corresponding_Declaration
                               (BATN.Classifier_Ref (P)))),
531
532
                     Declarations);

533
               else
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
                  if not BANu.Is_Empty (BATN.Package_Name
                                        (BATN.Classifier_Ref (P)))
                  then
                     Display_Error
                       (" The mapping of BA variable type ("
                        & Get_Name_String (BATN.Display_Name
                          (BATN.Full_Identifier (BATN.Classifier_Ref (P))))
                        & ") at " & Image (BATN.Loc (BATN.Classifier_Ref (P)))
                        & ", is not yet supported",
                        Fatal => True);
                  else
                     if Present (BATN.Component_Impl (BATN.Classifier_Ref (P)))
                     then
                        Display_Error
                          (" The mapping of BA variable type ("
                           & Get_Name_String
                             (Standard.Utils.Remove_Prefix_From_Name
                                  ("%ba%", BATN.Name (BATN.Component_Type
                                   (BATN.Classifier_Ref (P)))))
                           & "."
                           & Get_Name_String
                             (Standard.Utils.Remove_Prefix_From_Name
                                  ("%ba%", BATN.Name (BATN.Component_Impl
                                   (BATN.Classifier_Ref (P)))))
                           & ") at "
                           & Image (BATN.Loc (BATN.Classifier_Ref (P)))
                           & ", is not yet supported",
                           Fatal => True);
                     else
                        Display_Error
                          (" The mapping of BA variable type ("
                           & Get_Name_String
                             (Standard.Utils.Remove_Prefix_From_Name
                                  ("%ba%", BATN.Name (BATN.Component_Type
                                   (BATN.Classifier_Ref (P)))))
                           & ") at "
                           & Image (BATN.Loc (BATN.Classifier_Ref (P)))
                           & ", is not yet supported",
                           Fatal => True);
                     end if;
                  end if;
575
               end if;
576
577
578
579
580
581
582
583
584
585
586

               T := BATN.Next_Node (T);
               exit when No (T);
            end loop;
            P := BATN.Next_Node (P);
            exit when No (P);
         end loop;
      end if;

   end Map_C_Behavior_Variables;

587
588
589
   --------------------------------
   -- Map_C_Behavior_Transitions --
   --------------------------------
590

591
592
593
   procedure Map_C_Behavior_Transitions (S            : Node_Id;
                                         Declarations : List_Id;
                                         Statements   :  List_Id)
594
   is
595
596
597
598
599
      BA                 : Node_Id;
      behav_transition   : Node_Id;
      Transition_Node    : Node_Id;
   begin

600
      BA := Get_Behavior_Specification (S);
601
602
603
604
605
606

      if not BANu.Is_Empty (BATN.Transitions (BA)) then

         behav_transition := BATN.First_Node (BATN.Transitions (BA));
         Transition_Node := BATN.Transition (behav_transition);

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
         if BANu.Length (BATN.Transitions (BA)) = 1 then
            if AINU.Is_Thread (S) then
               Map_C_Behavior_Variables (S, Declarations);
            end if;

            if Present (BATN.Behavior_Action_Block (Transition_Node)) then
               if BATN.Kind (Transition_Node) =
                 BATN.K_Execution_Behavior_Transition
               then
                  --  For an AADL subprogram or thread with BA that includes
                  --  a unique transition, the Behavior_Action_Block
                  --  of this transition is mapped into C-statements.
                  Map_C_Behavior_Action_Block
                    (BATN.Behavior_Action_Block (Transition_Node),
                     S, Declarations, Statements);

                  if AINU.Is_Thread (S) then
                     Map_C_Implementation_of_BA_Body_Function
                       (S, Declarations, Statements);
                  end if;

               else
                  --  i.e. Kind (Transition_Node) = K_Mode_Transition
                  --  We do not support Mode transition in a subprogram
                  --
                  Display_Error
                    ("Mode Transition is not supported",
                     Fatal => True);
               end if;
            end if;
         else
            --  For an AADL subprogram with BA, many transitions
            --  are not supported.
            if AINU.Is_Subprogram (S) then
               Display_Error
                 ("Many transitions in the BA of a"
                  & "subprogram are not supported",
                  Fatal => True);
            elsif AINU.Is_Thread (S) then
646
647
648
               if Get_Thread_Dispatch_Protocol (S) = Thread_Periodic or else
                 Get_Thread_Dispatch_Protocol (S) = Thread_Sporadic
               then
649
650
651
652
                  Map_C_Many_Transitions_Of_A_Thread
                    (S, Declarations, Statements);
               else
                  Display_Error
653
654
655
                    ("The mapping of many BA transitions is only "
                     & " supported of periodic or sporadic threads",
                     Fatal => True);
656
657
658
659
660
661
662
663
               end if;
            end if;
         end if;

      end if;

   end Map_C_Behavior_Transitions;

664
665
666
   ----------------------------------------
   -- Compute_Nb_On_Dispatch_Transitions --
   -----------------------------------------
667

668
669
   function Compute_Nb_On_Dispatch_Transitions
     (S : Node_Id) return Unsigned_Long_Long
670
   is
671
672
673
674
      BA                        : Node_Id;
      behav_transition          : Node_Id;
      Transition_Node           : Node_Id;
      Nb_Dispatch_Transitions   : Unsigned_Long_Long;
675
676
   begin

677
      BA := Get_Behavior_Specification (S);
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
      Nb_Dispatch_Transitions := 0;

      --  /** compute the number of all  on dispatch  transitions
      --  that stems from the state "List_Node"
      --  i.e. have  BATN.Display_Name (List_Node)  as source state.
      --
      if not BANu.Is_Empty (BATN.Transitions (BA)) then
         Behav_Transition := BATN.First_Node (BATN.Transitions (BA));
         while Present (Behav_Transition) loop
            Transition_Node := BATN.Transition (Behav_Transition);

            if BATN.Kind (Transition_Node) =
              BATN.K_Execution_Behavior_Transition
              and then
                Present (BATN.Behavior_Condition (Transition_Node))
                and then
                  Present (BATN.Condition
                           (BATN.Behavior_Condition
                              (Transition_Node)))
              and then
                BATN.Kind
                  (BATN.Condition
                     (Behavior_Condition (Transition_Node)))
                = BATN.K_Dispatch_Condition_Thread
            then
               Nb_Dispatch_Transitions := Nb_Dispatch_Transitions + 1;
            end if;
            Behav_Transition := BATN.Next_Node (Behav_Transition);
         end loop;
      end if;

      return Nb_Dispatch_Transitions;

   end Compute_Nb_On_Dispatch_Transitions;

   ---------------------------------------------------------
   -- Compute_Max_Dispatch_Transitions_Per_Complete_State --
   ---------------------------------------------------------

   function Compute_Max_Dispatch_Transitions_Per_Complete_State
     (S : Node_Id) return Unsigned_Long_Long
   is
      BA                        : Node_Id;
      State                     : Node_Id;
      List_Node                 : Node_Id;
      behav_transition          : Node_Id;
      Transition_Node           : Node_Id;
      Source                    : Node_Id;
      Max_Dispatch_Transitions  : Unsigned_Long_Long := 0;
      Nb_Dispatch_Transitions   : Unsigned_Long_Long;
   begin

      BA := Get_Behavior_Specification (S);

      State := BATN.First_Node (BATN.States (BA));

      while Present (State) loop
         if Behavior_State_Kind'Val (BATN.State_Kind (State))
           = BSK_Initial_Complete
           or else Behavior_State_Kind'Val (BATN.State_Kind (State))
             = BSK_Initial_Complete_Final
           or else Behavior_State_Kind'Val (BATN.State_Kind (State))
             = BSK_Complete
           or else Behavior_State_Kind'Val (BATN.State_Kind (State))
             = BSK_Complete_Final
744
         then
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
            List_Node := BATN.First_Node (BATN.Identifiers (State));

            while Present (List_Node) loop

               Nb_Dispatch_Transitions := 0;

               --  /** compute the number of all  on dispatch  transitions
               --  that stems from the state "List_Node"
               --  i.e. have  BATN.Display_Name (List_Node)  as source state.
               --
               if not BANu.Is_Empty (BATN.Transitions (BA)) then
                  Behav_Transition := BATN.First_Node (BATN.Transitions (BA));
                  while Present (Behav_Transition) loop
                     Transition_Node := BATN.Transition (Behav_Transition);

                     if BATN.Kind (Transition_Node) =
                       BATN.K_Execution_Behavior_Transition
                       and then
                         Present (BATN.Behavior_Condition (Transition_Node))
                         and then
                           Present (BATN.Condition
                                    (BATN.Behavior_Condition
                                       (Transition_Node)))
                       and then
                         BATN.Kind
                           (BATN.Condition
                              (Behavior_Condition (Transition_Node)))
                         = BATN.K_Dispatch_Condition_Thread
                     then
                        if BANu.Length (BATN.Sources (Transition_Node)) = 1
                        then
                           Source := BATN.First_Node
                             (BATN.Sources (Transition_Node));
                           if  (Standard.Utils.To_Upper
                                (BATN.Display_Name (List_Node)) =
                                  Standard.Utils.To_Upper
                                    (BATN.Display_Name (Source)))
                           then
                              Nb_Dispatch_Transitions :=
                                Nb_Dispatch_Transitions + 1;
                           end if;
                        end if;
                     end if;
                     Behav_Transition := BATN.Next_Node (Behav_Transition);
                  end loop;
               end if;

               if Nb_Dispatch_Transitions > Max_Dispatch_Transitions then
                  Max_Dispatch_Transitions := Nb_Dispatch_Transitions;
               end if;

               List_Node := BATN.Next_Node (List_Node);
            end loop;
798
         end if;
799
800

         State := BATN.Next_Node (State);
801
802
      end loop;

803
      return Max_Dispatch_Transitions;
804

805
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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
   end Compute_Max_Dispatch_Transitions_Per_Complete_State;

   -----------------------------------------------------------
   -- Compute_Max_Dispatch_Triggers_Per_Dispatch_Transition --
   -----------------------------------------------------------

   function Compute_Max_Dispatch_Triggers_Per_Dispatch_Transition
     (S : Node_Id) return Unsigned_Long_Long
   is
      BA                        : Node_Id;
      behav_transition          : Node_Id;
      Node                      : Node_Id;
      Dispatch_Conjunction_Node : Node_Id;
      Dipatch_Trigger_Event     : Node_Id;
      Max_Dispatch_Triggers     : Unsigned_Long_Long := 0;
      Nb_Dispatch_Triggers      : Unsigned_Long_Long;
   begin

      BA := Get_Behavior_Specification (S);

      if not BANu.Is_Empty (BATN.Transitions (BA)) then

         behav_transition := BATN.First_Node (BATN.Transitions (BA));

         while Present (Behav_Transition) loop

            Node := BATN.Transition (Behav_Transition);

            if BATN.Kind (Node) = BATN.K_Execution_Behavior_Transition
              and then Present (BATN.Behavior_Condition (Node))
              and then Present (BATN.Condition
                                (BATN.Behavior_Condition (Node)))
              and then
                BATN.Kind (BATN.Condition (Behavior_Condition (Node)))
              = BATN.K_Dispatch_Condition_Thread
              and then Present
                (BATN.Dispatch_Trigger_Condition
                   (BATN.Condition
                      (BATN.Behavior_Condition (Node))))
              and then not BANu.Is_Empty
                (BATN.Dispatch_Conjunction
                   (BATN.Dispatch_Trigger_Condition
                      (BATN.Condition
                           (BATN.Behavior_Condition (Node)))))
            then
               Dispatch_Conjunction_Node :=
                 BATN.First_Node
                   (Dispatch_Conjunction
                      (Dispatch_Trigger_Condition (BATN.Condition
                       (BATN.Behavior_Condition (Node)))));

               while Present (Dispatch_Conjunction_Node) loop

                  if not BANu.Is_Empty
                    (Dispatch_Triggers (Dispatch_Conjunction_Node))
                  then
                     Dipatch_Trigger_Event := BATN.First_Node
                       (Dispatch_Triggers (Dispatch_Conjunction_Node));

                     Nb_Dispatch_Triggers := 0;

                     while Present (Dipatch_Trigger_Event) loop

                        Nb_Dispatch_Triggers := Nb_Dispatch_Triggers + 1;

                        Dipatch_Trigger_Event := BATN.Next_Node
                          (Dipatch_Trigger_Event);
                     end loop;

                     if Nb_Dispatch_Triggers > Max_Dispatch_Triggers then
                        Max_Dispatch_Triggers := Nb_Dispatch_Triggers;
                     end if;

                  end if;

                  Dispatch_Conjunction_Node := BATN.Next_Node
                    (Dispatch_Conjunction_Node);
               end loop;

            end if;
            behav_transition := BATN.Next_Node (behav_transition);
         end loop;
      end if;

      return Max_Dispatch_Triggers;

   end Compute_Max_Dispatch_Triggers_Per_Dispatch_Transition;

   --------------------------------------
   -- Create_Enum_Type_Of_States_Names --
   --------------------------------------

   procedure Create_Enum_Type_Of_States_Names (S : Node_Id)
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
   is
      BA                         : Node_Id;
      State, List_Node           : Node_Id;
      N                          : Node_Id;
      State_Enumerator_List      : List_Id;
      State_Identifier           : Unsigned_Long_Long;
      Thread_Instance_Name       : Name_Id;
      E                          : constant Node_Id :=
        AIN.Parent_Subcomponent (S);
   begin

      BA := Get_Behavior_Specification (S);

      --  1) Create an enumeration type declaration
      --  called __po_hi_<<thread_instance_name>>_state_name_t
      --  that enumerates all states in the BA of the thread
      --  typedef enum
      --  {
      --     s0, s1, s2, s3
917
      --  } __po_hi__<<thread_instance_name>>__state_name_t;
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965

      State_Enumerator_List := New_List (CTN.K_Enumeration_Literals);

      State := BATN.First_Node (BATN.States (BA));
      State_Identifier := 0;

      while Present (State) loop

         if not BANu.Is_Empty (BATN.Identifiers (State)) then
            List_Node := BATN.First_Node (BATN.Identifiers (State));

            while Present (List_Node) loop

               N := CTU.Make_Defining_Identifier
                 (BATN.Display_Name (List_Node));

--                 N := Make_Expression
--                   (CTU.Make_Defining_Identifier
--                      (BATN.Display_Name (List_Node)),
--                    Op_Equal,
--                    Make_Literal
--                      (CV.New_Int_Value (State_Identifier, 0, 10)));

               Append_Node_To_List (N, State_Enumerator_List);
               State_Identifier := State_Identifier + 1;

               List_Node := BATN.Next_Node (List_Node);
            end loop;
         end if;

         State := BATN.Next_Node (State);
      end loop;

      Thread_Instance_Name := Map_Thread_Port_Variable_Name (S);
      N :=
        Message_Comment
          ("For each state in the BA of the thread instance << " &
             Get_Name_String (Thread_Instance_Name) &
             " >> add an enumerator");
      Append_Node_To_List (N, CTN.Declarations (Current_File));

      N :=
        Make_Full_Type_Declaration
          (Defining_Identifier => Make_Defining_Identifier
             (Map_C_Variable_Name (E, State_Name_T => True)),
           Type_Definition => Make_Enum_Aggregate (State_Enumerator_List));
      Append_Node_To_List (N, CTN.Declarations (Current_File));

966
   end Create_Enum_Type_Of_States_Names;
967

968
969
970
   -----------------------
   -- Create_State_Type --
   -----------------------
971

972
973
974
975
976
977
978
   procedure Create_State_Type (S : Node_Id)
   is
      N                          : Node_Id;
      State_Struct               : List_Id;
      E                          : constant Node_Id :=
        AIN.Parent_Subcomponent (S);
   begin
979
980
981
982
983
984
985
986
987
988
989
990

      --  3)
      --  typedef struct
      --  {
      --    __po_hi_<<thread_instance_name>>_state_name_t name;
      --    __po_hi_state_kind_t kind;
      --  } __po_hi_<<thread_instance_name>>_state_t;

      State_Struct := New_List (CTN.K_Enumeration_Literals);

      N :=
        Make_Member_Declaration
991
992
          (Defining_Identifier => Make_Defining_Identifier
             (MN (M_Name)),
993
994
995
996
997
998
           Used_Type           => Make_Defining_Identifier
             (Map_C_Variable_Name (E, State_Name_T => True)));
      Append_Node_To_List (N, State_Struct);

      N :=
        Make_Member_Declaration
999
1000
          (Defining_Identifier => Make_Defining_Identifier
             (MN (M_kind)),