ocarina-transfo-fusions.adb 180 KB
Newer Older
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--              O C A R I N A . T R A N S F O . F U S I O N S               --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--    Copyright (C) 2008-2009 Telecom ParisTech, 2010-2014 ESA & ISAE.      --
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
--                                                                          --
-- Ocarina  is free software;  you  can  redistribute  it and/or  modify    --
-- it under terms of the GNU General Public License as published by the     --
-- Free Software Foundation; either version 2, or (at your option) any      --
-- later version. Ocarina is distributed  in  the  hope  that it will be    --
-- useful, but WITHOUT ANY WARRANTY;  without even the implied warranty of  --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General --
-- Public License for more details. You should have received  a copy of the --
-- GNU General Public License distributed with Ocarina; see file COPYING.   --
-- If not, write to the Free Software Foundation, 51 Franklin Street, Fifth --
-- Floor, Boston, MA 02111-1301, USA.                                       --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable to be   --
-- covered  by the  GNU  General  Public  License. This exception does not  --
-- however invalidate  any other reasons why the executable file might be   --
-- covered by the GNU Public License.                                       --
--                                                                          --
jhugues's avatar
jhugues committed
29
30
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
31
32
33
34
35
36
37
38
39
40
41
42
43
44
--                                                                          --
------------------------------------------------------------------------------

with Ocarina.Transfo;
with Ocarina.ME_AADL;
with Ocarina.ME_AADL.AADL_Tree.Nodes;
with Ocarina.ME_AADL.AADL_Tree.Nutils;
with Ocarina.ME_AADL.AADL_Tree.Entities;
with Ocarina.ME_AADL.AADL_Tree.Debug;

with Ocarina.ME_AADl.AADL_Instances.Nodes;
with Ocarina.ME_AADl.AADL_Instances.Nutils;
with Ocarina.Transfo.Fusions.Scheduler;
with Errors;
45
with Ocarina.Namet;
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
with GNAT.OS_Lib; use GNAT.OS_Lib;
with GNAT.Dynamic_Tables;
with Ocarina.Builder.AADL.Components;
with Ocarina.Builder.AADL.Components.Subprogram_Calls;
with Ocarina.Builder.AADL.Components.Subcomponents;
with Ocarina.Builder.AADL.Components.Connections;
with Ocarina.Builder.AADL.Components.Features;
with Ocarina.AADL_Values;

with Ocarina.Builder.AADL.Namespaces;
with Ocarina.Builder.AADL.Properties;
with Ocarina.Analyzer.AADL.Queries;
with Ocarina.Analyzer.AADL.Finder;
with Ocarina.Analyzer.AADL.Naming_Rules;
with Ocarina.Analyzer.AADL.Semantics;
with Locations;
with Charset;
with Utils;

with Ocarina.Instances.Queries;
with Ocarina.Backends.Messages;

package body Ocarina.Transfo.Fusions is
   use Ocarina.Transfo;

   use Ocarina.ME_AADL;
   use Ocarina.ME_AADL.AADL_Tree.Nodes;
   use Ocarina.ME_AADL.AADL_Tree.Debug;
   use Ocarina.ME_AADL.AADL_Tree.Nutils;
   use Ocarina.Backends.Messages;

   use Errors;
78
   use Ocarina.Namet;
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
   use Locations;

   package AIN renames Ocarina.ME_AADL.AADL_Instances.Nodes;
   package AINU renames Ocarina.ME_AADL.AADL_Instances.Nutils;
   package ATNU renames Ocarina.ME_AADL.AADL_Tree.Nutils;
   package ATN renames Ocarina.ME_AADL.AADL_Tree.Nodes;

   package Node_Counters is new GNAT.Dynamic_Tables
     (Table_Component_Type => Node_Counter,
      Table_Index_Type     => Natural,
      Table_Low_Bound      => 1,
      Table_Initial        => 100,  -- # of elements
      Table_Increment      => 50); -- % increase

   package Node_Mapping is new GNAT.Dynamic_Tables
     (Table_Component_Type => Node_Association,
      Table_Index_Type     => Natural,
      Table_Low_Bound      => 1,
      Table_Initial        => 100,  -- # of elements
      Table_Increment      => 50); -- % increase

   package Node_Priorities is new GNAT.Dynamic_Tables
     (Table_Component_Type => Node_Priority,
      Table_Index_Type     => Natural,
      Table_Low_Bound      => 1,
      Table_Initial        => 100,  -- # of elements
      Table_Increment      => 50); -- % increase

   function Is_In_Call_Sequence
     (Call : Name_Id; Call_Sequence : Node_id)
     return Boolean;
   --  Return true if their is a suprogram call of identifier "Call" in the
   --  parameter-given call sequence

   function Is_Call_Sequence_Sporadic
     (Call_Seq, Thread : Node_Id)
     return Boolean;
   --  Return true if the call sequence depends of at least on event or
   --  event data port

   function Get_Entrypoint_Sporadic_Port
     (Call_Seq, Thread : Node_Id)
     return Node_Id;
   --  return the port which is teh entrypoint of call sequence call_seq,
   --  return no_node if call_seq has no entrypoint defined

   procedure Compute_Call_Sequences
     (Th_Inst_1, Th_Inst_2       : Node_Id;
      New_Thread_Impl            : Node_Id;
      Th1_Mapping, Th2_Mapping   : Node_Mapping.Instance);
   --  Copy all call sequences in the new thread, creating new modes
   --  and assigning properties

   procedure Set_Flat_Entity_Reference (Node : Node_Id; Name : Name_Id);
   --  Set the entity reference in the specific case where the path
   --  is a single element

   function Declare_Wrapper_Subprogram
     (Owner_Thread : Node_Id) return Node_Id;
   --  Create a wrapper (declaration and implementation) for the
   --  thread passed though parameters.
   --  Return the component implementation,
   --  return no_node whenever declaration fail

   procedure Declare_Wrapper_Subprogram (Owner_Thread : Node_Id);
   pragma Unreferenced (Declare_Wrapper_Subprogram);
   --  Create a wrapper (declaration and implementation) for the
   --  thread passed though parameters.
   --  raise a program_error whenever declaration fail

   function Duplicate_Subcomponent
     (Old_Subcomp, Owner : Node_Id;
      Suffix : String := "") return Node_Id;
   --  Duplicate the subcomponent

   function Duplicate_Process (Old_Proc : Node_Id) return Node_Id;
   --  Duplicate the process, minus connections and subcomponents

   procedure Clean_Type_Declaration
     (Old_Decl, Namespace : Node_Id);
   --  Delete the declaration of the old threads iff they are
   --  not referenced anymore
   procedure Clean_Implementation_Declaration
     (Old_Impl, Namespace : Node_Id);
   --  Delete the implementation of the old threads iff they are
   --  not referenced anymore
   procedure Clean_Declaration
     (Old_Decl, Namespace : Node_Id);
   --  Delete the (type/implementation) declaration of the old threads
   --  iff they are not referenced anymore

   procedure Exchange_Processes_In_System
     (System, Old_Proc_Subcomponent, New_Proc : Node_Id;
      Success : out Boolean);
   --  In the component system, replace the subcomponent
   --  Old_Proc_Subcomponent by a subcomponent containing New_Proc

   procedure Clean;
   --  Free all global variables

   procedure Copy_Feature_Core
     (Feature, New_Feature, Owner_Component : Node_Id;
      Suffix : String := "");
   --  Copy the information from a feature generic object to a new one

   function Find_New_Feature_Node
     (Old_Feature_Name : Name_Id;
      Parent           : Node_Id;
      Mapping          : Node_Mapping.Instance)
     return Node_Id;
   --  Find a new feature node using the old name as a key

   procedure Create_Connection
     (Src_Path, Dst_Path : List_Id;
      Old_Cnx, Wrapper_impl : Node_Id;
      CT : Ocarina.ME_AADL.Connection_Type;
      New_Name : Name_Id := No_Name);
   --  Create the local connection from src_path to dst_path,
   --  and add it to the wrapper

   procedure Create_Connection_End_Path
     (Path : in out List_Id; Token_1 : Name_Id; Token_2 : Name_Id := No_Name);
   --  Create the list that contains the path tokens

   procedure Fusion_Parent_Connections
     (Old_Proc, New_Proc, Old_Thread_1, Old_Thread_2 : Node_Id;
      Success : out Boolean);
   --  Fusion connections declared in parent process
   --  FIXME : does not support more than one level of
   --  subcomponent addressing

   procedure Copy_All_Features
     (Features_List : List_Id;
      Mapping : in out Node_Mapping.Instance;
      Thread_Component, Owner_Thread : Node_Id);
   --  Copy all features in thread_component to the feature list
   --  For each of them, create a reference and associate a 0-value
   --  to it in the counter list. Will be used to count the number
   --  of connection supported by a feature.
   --  Will also increment the related counter

   procedure Increment_Related_Counter (Feature : Node_Id);
   --  Increment the counter corresponding to the feature

   procedure Clean_Unused_Features;
   pragma Unreferenced (Clean_Unused_Features);
   --  Delete all features whose counter is equal to 0

   procedure Copy_All_Subcomponents
     (Features_List : List_Id;
      Thread_Impl, Owner_Thread : Node_Id);
   --  Copy all subcomponents from Thread_Impl to the feature_list.

   function Search_Subprogram_By_Name
     (Key : Name_Id; Call_Mapping : Node_Mapping.Instance)
     return Node_Id;
   --  Find a subprogram from its name
   --  return No_Node if no such was found

   procedure Manage_Component_Properties
     (Old_Thread_1, Old_Thread_2, New_Thread : Node_Id;
      Success                                : out Boolean;
      Is_Sporadic, Is_Periodic               : Boolean := False);
   --  Fusion the properties from old threads to the fusionned one

   function Declare_Generated_Procedure
     (New_Thread : Node_Id; Proc_Str : String) return Node_Id;
   --  Factorized code for declare_*

   function Declare_Mode_Scheduler (New_Thread : Node_Id) return Node_Id;
   --  Declare the AADL code corresponding to the mode scheduler
   --  associated to the fusioned thread. Returns the implementation.

   function Declare_Next_Period (New_Thread : Node_Id) return Node_Id;
   --  Declare the AADL code corresponding to the period iterator
   --  associated to the fusioned thread. Returns the implementation.

   procedure Declare_Scheduler_Property
     (Thread : Node_Id; Subcomponent_Name : Name_Id);
   --  add the scheduler_name property for the thread

   procedure Set_Original_Name_Property
     (Thread : Node_Id; Subcomponent_Name : Name_Id);

   procedure Add_Needed_Packages (Thread : Node_Id);
   --  Add packages needed for any fusion
   --  (currently only 'transformations')

   function Dot_To_Underscore (Str : String) return String_Access;

   procedure Set_WCET (E : Node_Id; Max_Duration : Natural);
   --  Set the thread or subprogram WCET

   function Get_WCET (S : Node_Id) return Unsigned_Long_Long;
   --  return a component WCET

   procedure Set_Period (S : Node_Id; Quantum : Natural);
   --  Set the quantum as subprogram period

   procedure Set_Priority (S : Node_Id; Prio : Natural);
   --  Set the subprogram priority

   procedure Set_Deadline (S : Node_Id; Value : Natural);
   --  Set a thread deadline

   procedure Put_Data_Subcomponent_Back
     (Old_Process  : Node_Id;
      Old_Thread_1 : Node_Id;
      Old_Thread_2 : Node_Id;
      New_Process  : Node_Id);
   --  Put back unfusioned subcomponents in the owner process

   function Get_Property_Association
     (Component : Node_Id;
      Property  : Name_Id) return Node_Id;
   --  Search a property association in component
   --  implementation and declaration

   Total_WCET : Natural;
   --  Sum the WCET of al subprogram executed in the merged thread

   Counters : Node_Counters.Instance;

   Priorities : Node_Priorities.Instance;

   AADL_Root : Node_Id := No_Node;
   --  Root of the AADL tree

   Instance_Name : Name_Id;
   --  The newly created instance name
   New_Process  : Node_Id;
   --  The newly created process implementation
   Thread_Namespace : Node_Id;
   --  namespace of the first thread to be fusioned into (both old
   --  and new) processes
   Dummy_Internal_Type : Node_Id;

   Th1_Mapping      : Node_Mapping.Instance;
   Th2_Mapping      : Node_Mapping.Instance;
   --  Features

   Th1_Inst, Th2_Inst : String_Access;

   Mode_Scheduler   : Node_Id;
   Next_Period      : Node_Id;
   Stop_Mode_Name   : Name_Id;

   Cnx_Num          : Int := 1;
   --  A counter of the number of connections created

   Dispatch_Protocol_Str   : constant String := "dispatch_protocol";
   Deadline_Str            : constant String := "deadline";
   WCET_Str                : constant String := "compute_execution_time";
   Cheddar_Priority_Str    : constant String :=
     "cheddar_properties::fixed_priority";
   Deployment_Priority_Str : constant String := "deployment::priority";
   Scheduler_Call_Str      : constant String := "schedule";
   Iterator_Call_Str       : constant String := "next_period";
   Compute_Entrypoint_Str  : constant String :=
     "compute_entrypoint_call_sequence";
   CS_Period_Str           : constant String :=
     "transformations::call_sequence_period";
   Period_Str              : constant String := "period";
   Transfo_Priority_Str    : constant String := "transformations::priority";
   Raw_Priority_Str        : constant String := "priority";
   Raw_Fixed_Priority_Str  : constant String := "fixed_priority";
   Transfo_Occurred_Str    : constant String :=
     "transformations::fusion_occurred";
   Raw_Occurred_Str        : constant String := "fusion_occurred";
   Priority_Shifter_Str    : constant String :=
     "transformations::priority_shifter";
   Raw_Priority_Shifter_Str : constant String :=
     "priority_shifter";

   Stop_Mode_Str           : constant String := "stop_mode";
   Transfo_Pkg_Str         : constant String := "transformations";
   Cheddar_Pkg_Str         : constant String := "cheddar_properties";
   Raw_Data_Str            : constant String := "data_representation";
   Data_Representation_Str : constant String :=
     "data_model::data_representation";
   Data_Model_Pkg_Str      : constant String := "data_model";
   Transfo_Scheduler_Name_Str : constant String :=
     "transformations::scheduler_name";
   Scheduler_Name_Str : constant String :=
     "scheduler_name";
   Transfo_Original_Name_Str : constant String :=
     "transformations::original_name";
   Original_Name_Str : constant String :=
     "original_name";

   -----------------------
   -- Dot_To_Underscore --
   -----------------------

   function Dot_To_Underscore (Str : String) return String_Access
   is
      Len  : constant Natural := Str'Length;
      Str2 : constant String_Access := new String'(Str);
   begin
      for I in 1 .. Len loop
         if Str (I) = '.' then
            Str2 (I) := '_';
         end if;
      end loop;

      return Str2;
   end Dot_To_Underscore;

   ---------------------------------
   -- Declare_Generated_Procedure --
   ---------------------------------

   function Declare_Generated_Procedure
     (New_Thread : Node_Id; Proc_Str : String) return Node_Id
   is
      use Ocarina.Builder.AADL.Components;

      Decl    : Node_Id;
      Impl    : Node_Id;
      Id_Name : Name_Id := Build_Unique_Name
        (Declarations (Namespace (New_Thread)),
         Get_String_Name (Proc_Str));
      Str     : String_Access := Dot_To_Underscore
        (Get_Name_String (Id_Name));
   begin
      Id_Name := Get_String_Name (Str.all);

      --  Create the declaration

      Decl := Add_New_Component_Type
        (No_Location,
         Make_Identifier (No_Location, Id_Name, Id_Name, No_Node),
         Namespace (New_Thread),
         Ocarina.ME_AADL.CC_Subprogram);
      if No (Decl) then
         DE ("Could not declare the mode scheduler");
         raise Program_Error;
      end if;

      --  Create the implementation

      Id_Name := Get_String_Name (Str.all & ".i");
      Impl := Add_New_Component_Implementation
        (No_Location,
         Make_Identifier (No_Location, Id_Name, Id_Name, No_Node),
         Namespace (New_Thread),
         Ocarina.ME_AADL.CC_Subprogram);
      Set_Component_Type_Identifier (Impl, Identifier (Decl));
      if No (Impl) then
         DE ("Could not implements the procedure");
         raise Program_Error;
      end if;

      --  Add properties

      declare
         use Ocarina.Builder.AADL.Properties;
         use Ocarina.AADL_Values;

         Lang   : constant Name_Id := Get_String_Name ("source_language");
         Raw_Source_Name : constant Name_Id := Get_String_Name
           ("source_name");
         Source_Name : constant Name_Id := Get_String_Name
           ("transformations::source_name");
         Ada    : constant Name_Id := Get_String_Name ("ada95");
         Src    : constant Name_Id := Get_String_Name
           (Get_Name_String (Name (Identifier (Namespace (New_Thread))))
            & "_" & Get_Name_String (Instance_Name) & "_mode." & Proc_Str);
         S, S2  : Node_Id;
         Id     : Node_Id;
      begin
         S := New_Node (K_Property_Term, No_Location);
         Set_Identifier
           (S, Make_Identifier
            (No_Location, Ada, Ada, No_Node));
         Id := Make_Identifier (No_Location, Lang, Lang, No_Node);
         S2 := New_Node (K_Entity_Reference, No_Location);
         Set_Identifier
           (S2, Make_Identifier (No_Location, Lang, Lang, No_Node));
         Set_Full_Identifier (S2, Id);
         Id := Add_New_Property_Association
           (Loc            => No_Location,
            Name           => Id,
            Property_Name  => S2,
            Container      => Impl,
            In_Binding     => No_Node,
            In_Modes       => No_Node,
            Property_Value => S,
            Is_Constant    => False,
            Is_Access      => False,
            Is_Additive    => False,
            Applies_To     => No_List);
         if No (Id) then
            raise Program_Error;
         end if;

         S := New_Node (K_Literal, No_Location);
         Set_Value (S, New_String_Value (Src));
         Id := Make_Identifier (No_Location, Source_Name,
                                Source_Name, No_Node);
         S2 := New_Node (K_Entity_Reference, No_Location);
         Set_Identifier
           (S2, Make_Identifier
            (No_Location, Raw_Source_Name, Raw_Source_Name, No_Node));
         Set_Namespace_Identifier
           (S2, Make_Identifier
            (No_Location, Transfo_Pkg, Transfo_Pkg, No_Node));
         Set_Full_Identifier (S2, Id);
         Id := Add_New_Property_Association
           (Loc            => No_Location,
            Name           => Id,
            Property_Name  => S2,
            Container      => Impl,
            In_Binding     => No_Node,
            In_Modes       => No_Node,
            Property_Value => S,
            Is_Constant    => False,
            Is_Access      => False,
            Is_Additive    => False,
            Applies_To     => No_List);
         if No (Id) then
            raise Program_Error;
         end if;
      end;
      Free (Str);

      return Impl;
   end Declare_Generated_Procedure;

   ----------------------------
   -- Declare_Mode_Scheduler --
   ----------------------------

   function Declare_Mode_Scheduler (New_Thread : Node_Id) return Node_Id
   is
   begin
      return Declare_Generated_Procedure (New_Thread, "mode_scheduler");
   end Declare_Mode_Scheduler;

   -------------------------
   -- Declare_Next_Period --
   -------------------------

   function Declare_Next_Period (New_Thread : Node_Id) return Node_Id
   is
   begin
      return Declare_Generated_Procedure (New_Thread, "next_period");
   end Declare_Next_Period;

   -------------------------
   -- Add_Needed_Packages --
   -------------------------

   procedure Add_Needed_Packages (Thread : Node_Id)
   is
      use Ocarina.Analyzer.AADL.Finder;

      pragma Assert (Kind (Thread) = K_Component_Implementation);

      Transfo    : constant Name_Id := Get_String_Name
              ("transformations");
      Transfo_Id : constant Node_Id := Make_Identifier
        (No_Location, Transfo, Transfo, No_Node);
      Pkg        : constant Node_Id := Namespace (Thread);
   begin
      if not Find_In_Import_Declaration (Pkg, Transfo_Id) then
         declare
            Import_Decl     : constant Node_Id := New_Node
              (K_Import_Declaration, No_Location);
            Visibility_Decl : constant Node_Id := New_Node
              (K_Name_Visibility_Declaration, No_Location);
         begin
            if Is_Empty (Declarations (pkg)) then
               Set_Declarations
                 (Pkg, New_List (K_List_Id, No_Location));
            end if;

            Set_List_Items
              (Visibility_Decl, New_List (K_List_Id, No_Location));
            Set_List_Items
              (Import_Decl, New_List (K_List_Id, No_Location));
            Append_Node_To_List
              (Transfo_Id, List_Items (Import_Decl));
            Append_Node_To_List
              (Import_Decl, List_Items (Visibility_Decl));
            Push_Node_To_List
              (Visibility_Decl, Declarations (pkg));
         end;
      end if;
   end Add_Needed_Packages;

   --------------------------------
   -- Set_Original_Name_Property --
   --------------------------------

   procedure Set_Original_Name_Property
     (Thread : Node_Id; Subcomponent_Name : Name_Id)
   is
      pragma Assert (Kind (Thread) = K_Component_Implementation);
      use Ocarina.AADL_Values;
      use Ocarina.Builder.AADL.Properties;

      N      : Name_Id;
      L      : constant Node_Id := New_Node (K_Literal, No_Location);
      S2, Id : Node_Id;
   begin
      N := Get_String_Name
        (Get_Name_String (Name (Identifier (Namespace (Thread))))
         & "_" & Get_Name_String (Subcomponent_Name));

      S2 := New_Node (K_Entity_Reference, No_Location);
      Id := Make_Identifier
        (No_Location, Transfo_Original_Name,
         Transfo_Original_Name, No_Node);
      Set_Identifier
        (S2, Make_Identifier
         (No_Location, Raw_Original_Name,
          Raw_Original_Name, No_Node));
      Set_Namespace_Identifier
        (S2, Make_Identifier
         (No_Location, Transfo_Pkg, Transfo_Pkg, No_Node));
      Set_Full_Identifier
        (S2, Make_Identifier
         (No_Location, Transfo_Original_Name,
          Transfo_Original_Name, No_Node));

      Set_Value (L, New_String_Value (N));

      Id := Add_New_Property_Association
        (Loc            => No_Location,
         Name           => Id,
         Property_Name  => S2,
         Container      => Thread,
         In_Binding     => No_Node,
         In_Modes       => No_Node,
         Property_Value => L,
         Is_Constant    => False,
         Is_Access      => False,
         Is_Additive    => False,
         Applies_To     => No_List);
      if No (Id) then
         raise Program_Error;
      end if;
   end Set_Original_Name_Property;

   --------------------------------
   -- Declare_Scheduler_Property --
   --------------------------------

   procedure Declare_Scheduler_Property
     (Thread : Node_Id; Subcomponent_Name : Name_Id)
   is
      pragma Assert (Kind (Thread) = K_Component_Implementation);
      use Ocarina.AADL_Values;
      use Ocarina.Builder.AADL.Properties;

      N      : Name_Id;
      L      : constant Node_Id := New_Node (K_Literal, No_Location);
      S2, Id : Node_Id;
   begin
      N := Get_String_Name
        (Get_Name_String (Name (Identifier (Namespace (Thread))))
         & "_" & Get_Name_String (Subcomponent_Name)
         & "_Scheduler_Instance");

      S2 := New_Node (K_Entity_Reference, No_Location);
      Id := Make_Identifier
        (No_Location, Transfo_Scheduler_Name,
         Transfo_Scheduler_Name, No_Node);
      Set_Identifier
        (S2, Make_Identifier
         (No_Location, Raw_Scheduler_Name,
          Raw_Scheduler_Name, No_Node));
      Set_Namespace_Identifier
        (S2, Make_Identifier
         (No_Location, Transfo_Pkg, Transfo_Pkg, No_Node));
      Set_Full_Identifier
        (S2, Make_Identifier
         (No_Location, Transfo_Scheduler_Name,
          Transfo_Scheduler_Name, No_Node));

      Set_Value (L, New_String_Value (N));

      Id := Add_New_Property_Association
        (Loc            => No_Location,
         Name           => Id,
         Property_Name  => S2,
         Container      => Thread,
         In_Binding     => No_Node,
         In_Modes       => No_Node,
         Property_Value => L,
         Is_Constant    => False,
         Is_Access      => False,
         Is_Additive    => False,
         Applies_To     => No_List);
      if No (Id) then
         raise Program_Error;
      end if;
   end Declare_Scheduler_Property;

   --------------------
   -- Fusion_Threads --
   --------------------

   procedure Fusion_Threads
     (Root       : Node_Id;
      Owner_Process, Thread_1, Thread_2 : Name_Id;
      New_Thread : out Node_Id;
      Success    : out Boolean)
   is
      use Ocarina.ME_AADL.AADL_Tree.Entities;

      Proc_Str        : constant String :=
        Get_Name_String (Owner_Process);
      Proc            : Node_Id;
      Th_1            : Node_Id;
      Th_2            : Node_Id;
      Th1_Impl        : Node_Id;
      Th2_Impl        : Node_Id;
      Id              : Node_Id;
      Thread_Impl     : Node_Id;
      Th_Namespace    : Node_Id;
      Thread_Impl_Str : String_Access;
   begin
      AADL_Root := Root;

      --  1/ Search the AADL process instance

      Proc := Search_Process_By_Name (Proc_Str);
      if No (Proc) then
         Success := False;
         DE ("Could not found process instance " & Proc_Str);
         return;
      end if;

      --  2/ Search for the threads

      Th_1 := Search_Thread_By_Name
        (Proc, Get_Name_String (Thread_1));
      if No (Th_1) then
         Success := False;
         DE ("Could not found thread instance " &
             Get_Name_String (Thread_1) &
             " in process " & Proc_Str);
         return;
      end if;

      Th_2 := Search_Thread_By_Name
        (Proc, Get_Name_String (Thread_2));
      if No (Th_2) then
         Success := False;
         DE ("Could not found thread instance " &
             Get_Name_String (Thread_2) &
             " in process " & Proc_Str);
         return;
      end if;

      --  FIXME
      --  check that all namespace definition is done relatively to
      --  th_1.

      Thread_Namespace := Namespace_Identifier
        (Entity_Ref
         (AIN.Corresponding_Declaration (Th_1)));

      Th1_Inst := new String'(Get_Name_String (Thread_1));
      Th2_Inst := new String'(Get_Name_String (Thread_2));

      Th1_Impl :=  AIN.Corresponding_Declaration
        (AIN.Corresponding_Instance (Th_1));
      Th2_Impl := AIN.Corresponding_Declaration
        (AIN.Corresponding_Instance (Th_2));

      Instance_Name := Build_Unique_Subcomponent_Name
        (AIN.Corresponding_Declaration (Proc), Thread_Prefix);

      --  3/ Fusion the 2 threads

      declare
         use Ocarina.Builder.AADL.Components;

         Th1_Decl     : constant Node_Id :=
           Corresponding_Entity
           (Component_Type_Identifier (Th1_Impl));
         Th2_Decl     : constant Node_Id :=
           Corresponding_Entity
           (Component_Type_Identifier (Th2_Impl));
         Thread_Name  : constant Name_Id := Build_Unique_Component_Name
           (Namespace (Th1_Decl), Concat_Names (Th1_Decl, Th2_Decl));
         Thread_Comp  : Node_Id;
         Impl_Name    : Name_Id;
         L            : List_Id;

         pragma Assert ((Kind (Th1_Decl) = Kind (Th2_Decl))
                        and then (Kind (Th1_Decl) = K_Component_Type));
      begin
         --  Add the transformation package

         Add_Needed_Packages (Th1_Impl);

         --  3.1/ Create a new thread component declaration

         Th_Namespace := Namespace (Th1_Decl);
         pragma Assert (Th_Namespace = Namespace (Th2_Decl));

         --  3.1.1/ Create the new component node and identifier

         Id := Make_Identifier
           (No_Location, Thread_Name, Thread_Name, No_Node);
         Thread_Comp := Add_New_Component_Type
           (No_Location, Id, Th_Namespace, CC_Thread,
            Is_Private => False);
         Set_Identifier (Thread_Comp, Id);

         --  3.1.2/ Copy all the features from the old threads

         L := New_List (K_List_Id, No_Location);
         Copy_All_Features (L, Th1_Mapping, Th1_Decl, Thread_Comp);
         Copy_All_Features (L, Th2_Mapping, Th2_Decl, Thread_Comp);
         Set_Features (Thread_Comp, L);

         --  3.1.3/ Copy the properties

         Manage_Component_Properties
           (Th1_Decl, Th2_Decl, Thread_Comp, Success);
         if not Success then
            return;
         end if;

         --  3.1.4/  Remove unused thread type declarations in namepsace

         Clean_Type_Declaration (Th1_Decl, Th_Namespace);
         Clean_Type_Declaration (Th2_Decl, Th_Namespace);

         --  3.2/ Create the corresponding implementation

         declare
            use Ocarina.Builder.AADL.Components.Subprogram_Calls;

            Impl_Id      : Node_Id;
         begin
            --  3.2.1/ Build the identifier and link to the component type

            Id := Make_Identifier
              (No_Location, Thread_Name, Thread_Name, No_Node);
            Set_Corresponding_Entity (Id, Thread_Comp);

            Thread_Impl_Str := new String'(Get_Name_String (Thread_Name)
                                           & ".i");
            Impl_Name := Get_String_Name (Thread_Impl_Str.all);
            Impl_Id := Make_Identifier
              (No_Location, Impl_Name, Impl_Name, No_Node);

            --  3.2.2/ Create the implementation node

            Thread_Impl := Add_New_Component_Implementation
              (No_Location,
               Impl_Id,
               Th_Namespace,
               CC_Thread);
            Set_Component_Type_Identifier
              (Thread_Impl, Identifier (Thread_Comp));

            --  3.2.3/ Copy subcomponents from both threads

            L := New_List (K_List_Id, No_Location);
            Copy_All_Subcomponents (L, Th1_Impl, Thread_Impl);
            Copy_All_Subcomponents (L, Th2_Impl, Thread_Impl);
            Set_Subcomponents (Thread_Impl, L);

            --  3.2.4/ Create the related mode scheduler and
            --         Iterator

            Mode_Scheduler := Declare_Mode_Scheduler (Thread_Impl);
            Next_Period := Declare_Next_Period (Thread_Impl);

            --  3.3.5/ Create a deep copy of the parent process,
            --  with *no* connections or subcomponents

            New_Process := Duplicate_Process
              (AIN.Corresponding_Declaration (Proc));

            --  3.2.6/ Copy subprogram calls from both threads

            Compute_Call_Sequences
              (AIN.Corresponding_Instance (Th_1),
               AIN.Corresponding_Instance (Th_2),
               Thread_Impl,
               Th1_Mapping, Th2_Mapping);

            --  3.2.7/ Put back data subcomponents that are either :
            --  * _not_ priority shifter
            --  * priority shifter connected to a non-fusionned thread

            Put_Data_Subcomponent_Back
              (AIN.Corresponding_Declaration (Proc),
               Th_1, Th_2, New_Process);

            --  3.2.6/ Annexes

            --  FIXME TODO
         end;
      end;

      --  3.3.x/ Delete unused thread implementation in namespace

      Clean_Implementation_Declaration (Th1_Impl, Th_Namespace);
      Clean_Implementation_Declaration (Th2_Impl, Th_Namespace);

      --  3.3.x/ Add *used* thread implementation in the new process

      declare
         Switcher : constant Name_Id := Get_String_Name ("priority_switcher_");
         Inst     : Node_Id := First_Node
           (Subcomponents (AIN.Corresponding_Declaration (Proc)));
         Cp       : Node_Id;
      begin
         while Present (Inst) loop
            if Name (Identifier (Inst)) /= Thread_1 and then
              Name (Identifier (Inst)) /= Thread_2 then
               declare
                  use Utils;
                  N2       : constant Node_Id := Entity (Entity_Ref (Inst));
                  Obj_Name : constant Name_Id := Name (Identifier (N2));
               begin
                  if not Is_Prefix (Switcher, Obj_Name) then
                     Cp := Duplicate_Subcomponent (Inst, New_Process);
                     Append_Node_To_List (Cp, Subcomponents (New_Process));
                  end if;
               end;
            end if;
            Inst := Next_Node (Inst);
         end loop;
      end;

      --  3.3.x/ Register the new process implementation

      Success := Ocarina.Builder.AADL.Namespaces.Add_Declaration
        (Namespace (AIN.Corresponding_Declaration (Proc)), New_Process);

      if not Success then
         raise Program_Error;
      end if;

      --  3.3.y/ Register the component implementation in the
      --  new process

      declare
         use Ocarina.Builder.AADL.Components.Subcomponents;

         SC            : Node_Id;
         Impl_Name     : Name_Id;
         E             : constant Node_Id :=
           New_Node (K_Entity_Reference, No_Location);
         C, V          : Node_Id;
         Th1_Ref       : constant Node_Id :=
           Entity_Ref (AIN.Corresponding_Declaration (Th_1));
         pragma Unreferenced (V);
      begin
         Id := Make_Identifier
           (No_Location,
            Instance_Name,
            Instance_Name,
            No_Node);
         SC := Add_New_Subcomponent
           (Loc                 => No_Location,
            Name                => Id,
            Comp_Impl           => New_Process,
            Is_Refinement       => False,
            Category            => CC_Thread,
            In_Modes            => No_Node,
            Prototypes_Bindings => No_List);

         --  Create the new entity reference

         Set_Namespace_Path (E, Namespace_Path (Th1_Ref));
         Set_Path (E, Path (Th1_Ref));
         V := Remove_Last_Node_From_List (Path (E));
         C := Make_Identifier
           (No_Location, Instance_Name, Instance_Name, No_Node);
         Append_Node_To_List (C, Path (E));
         Set_Namespace_Path (E, No_List);

         if Present (Thread_Namespace) then
            C := Make_Identifier
              (No_Location,
               Name (Thread_Namespace),
               Display_Name (Thread_Namespace),
               E);
            Set_Namespace_Identifier (E, C);
         else
            Set_Namespace_Identifier (E, No_Node);
         end if;
         Impl_Name := Get_String_Name (Thread_Impl_Str.all);
         Set_Identifier
           (E, Make_Identifier (No_Location, Impl_Name, Impl_Name, E));
         Set_Entity_Ref (SC, E);

         --  Add reference to the Ada package containing the schedule

         Declare_Scheduler_Property (Thread_Impl, Instance_Name);
         Set_Original_Name_Property (Thread_Impl, Instance_Name);

         --  Fusion connections

         Fusion_Parent_Connections
           (AIN.Corresponding_Declaration (Proc), New_Process,
            Th1_Impl, Th2_Impl, Success);
         if not Success then
            Clean;
            return;
         end if;

         --  Remove the old process if it is no more referenced
         --  (that is, if their was only one instance before fusion)

         Clean_Declaration
           (AIN.Corresponding_Declaration (Proc), Th_Namespace);

         --  Add an instance of the new process in the parent system,
         --  and remove the subcomponent corresponding to the previous
         --  process
For faster browsing, not all history is shown. View entire blame