ocarina-backends-lnt-tree_generator_processor.adb 68.5 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--              OCARINA.BACKENDS.LNT.TREE_GENERATOR_PROCESSOR               --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                     Copyright (C) 2016 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; use Ocarina.Namet;

with Ocarina.Backends;
with Ocarina.Backends.LNT.Nutils;
with Ocarina.Backends.LNT.Nodes;
with Ocarina.Backends.LNT.Components;
with Ocarina.Backends.Properties;
with Ocarina.Backends.Messages;

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 Utils; use Utils;

use Ocarina.Backends.LNT.Components;
use Ocarina.Backends.Properties;
use Ocarina.Backends.Messages;

use Ocarina.ME_AADL;
use Ocarina.ME_AADL.AADL_Instances.Entities;
with Ocarina.ME_AADL.AADL_Instances.Debug;
use Ocarina.ME_AADL.AADL_Instances.Debug;

with Ada.Text_IO; use Ada.Text_IO;

package body Ocarina.Backends.LNT.Tree_Generator_Processor is

   package AIN renames Ocarina.ME_AADL.AADL_Instances.Nodes;
   package AINu renames Ocarina.ME_AADL.AADL_Instances.Nutils;
   use AIN;
   use AINu;

   package BLN renames Ocarina.Backends.LNT.Nodes;
   package BLNu renames Ocarina.Backends.LNT.Nutils;
   use BLN;
   use BLNu;

   procedure Visit (E : Node_Id;
                  T : in out Thread_Array; P : in out Period_Array);
   procedure Visit_Architecture_Instance (E : Node_Id;
                  T : in out Thread_Array; P : in out Period_Array);
   procedure Visit_Component_Instance (E : Node_Id;
                  T : in out Thread_Array; P : in out Period_Array);
   procedure Visit_System_Instance (E : Node_Id;
                  T : in out Thread_Array; P : in out Period_Array);
   procedure Visit_Process_Instance (E : Node_Id;
                  T : in out Thread_Array; P : in out Period_Array);
   procedure Visit_Thread_Instance (E : Node_Id;
                  T : in out Thread_Array; P : in out Period_Array);

   procedure Make_LNT_RM_Processor (T : Thread_Array);
   procedure Make_LNT_RM_Processor_Functions;
   procedure Quick_Sort_Threads (Threads : in out Thread_Array);
hana's avatar
update:    
hana committed
86
   function Connections_Number (Port : Node_Id) return Natural;
87
88
89
   function Threads_Hyperperiod (T : Period_Array)
    return Natural;
   function Make_Time_Const (A : Natural) return Node_Id;
hana's avatar
update:    
hana committed
90
91
92
   procedure Make_Notif (Event_Port_Number : Natural;
     I : Natural; Notif_Function : String;
      L_Incoming_Event : List_Id);
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
   function Make_Time_Const (A : Natural) return Node_Id is
   begin
      return Make_Constructed_Pattern
            (Make_Identifier ("LNT_Type_Time_Constraint"),
             New_List (Make_Nat (A)));
   end Make_Time_Const;

   Module_Node : Node_Id := No_Node;
   Definitions_List : List_Id := No_List;
   Modules_List : List_Id := No_List;
   Predefined_Functions_List : List_Id := No_List;
   Counter : Natural := 1;

   ----------------------------
   -- Generate_LNT_Processor --
   ----------------------------
   function Generate_LNT_Processor (AADL_Tree : Node_Id)
     return Node_Id
   is

      Threads : Thread_Array (1 .. Thread_Number);
      Periods : Period_Array (1 .. Thread_Number);
   begin
      Put_Line ("Begin Processor");
      Visit (AADL_Tree, Threads, Periods);
      return Module_Node;
   end Generate_LNT_Processor;

   -----------
   -- Visit --
   -----------
   procedure Visit (E : Node_Id;
                    T : in out Thread_Array;
                    P : in out Period_Array) is
   begin
      case AIN.Kind (E) is

      when K_Architecture_Instance =>
         Visit_Architecture_Instance (E, T, P);

      when K_Component_Instance =>
         Visit_Component_Instance (E, T, P);

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

   ---------------------------------
   -- Visit_Architecture_Instance --
   ---------------------------------
   procedure Visit_Architecture_Instance (E : Node_Id;
                                          T : in out Thread_Array;
                                          P : in out Period_Array) is
      N : constant Node_Id := Root_System (E);
   begin
      Module_Node := Make_Module_Definition
       (New_Identifier (Get_String_Name ("_Processor"),
                        Get_Name_String (System_Name)));
      Definitions_List := New_List;
      Make_LNT_RM_Processor_Functions;
hana's avatar
update:    
hana committed
154
      Modules_List := New_List (Make_Identifier ("Types"));
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
      Predefined_Functions_List := New_List;
      Visit (N, T, P);
      --  Hyperperiod calculation
      Hyperperiod := Threads_Hyperperiod (P);
      Make_LNT_RM_Processor (T);
      Set_Definitions (Module_Node, Definitions_List);
      Set_Modules (Module_Node, Modules_List);
      Set_Predefined_Functions (Module_Node, Predefined_Functions_List);
   end Visit_Architecture_Instance;

   ------------------------------
   -- Visit_Component_Instance --
   ------------------------------
   procedure Visit_Component_Instance (E : Node_Id;
                                       T : in out Thread_Array;
                                       P : in out Period_Array) is
      Category : constant Component_Category
        := Get_Category_Of_Component (E);
   begin
      case Category is
            when CC_System =>
               Visit_System_Instance (E, T, P);
            when CC_Process =>
               Visit_Process_Instance (E, T, P);
            when CC_Thread =>
               Visit_Thread_Instance (E, T, P);
            when others =>
               null;
      end case;
   end Visit_Component_Instance;
   ---------------------------
   -- Visit_System_Instance --
   ---------------------------
   procedure Visit_System_Instance (E : Node_Id;
                                    T : in out Thread_Array;
                                    P : in out Period_Array) is
      S : Node_Id;
   begin
      --  Visit all the subcomponents of the system
      if not AINU.Is_Empty (Subcomponents (E)) then
         S := AIN.First_Node (Subcomponents (E));
         while Present (S) loop
            Visit (Corresponding_Instance (S), T, P);
            S := AIN.Next_Node (S);
         end loop;
      end if;
   end Visit_System_Instance;
   ----------------------------
   -- Visit_Process_Instance --
   ----------------------------
   procedure Visit_Process_Instance (E : Node_Id;
                                     T : in out Thread_Array;
                                     P : in out Period_Array) is
      S : Node_Id;
   begin
      if not AINU.Is_Empty (Subcomponents (E)) then
         S := AIN.First_Node (Subcomponents (E));
         while Present (S) loop
            Visit (Corresponding_Instance (S), T, P);
            S := AIN.Next_Node (S);
         end loop;
      end if;
   end Visit_Process_Instance;

   ----------------------------
   -- Visit_Thread_Instance --
   ----------------------------
   procedure Visit_Thread_Instance (E : Node_Id;
                                    T : in out Thread_Array;
                                    P : in out Period_Array)
   is
hana's avatar
update:    
hana committed
226
      S : Node_Id;
227
228
      Th : Thread;
      Period : Natural;
hana's avatar
update:    
hana committed
229
      Event_Port_Number : Natural := 0;
230
231
      Thread_Identifier : constant Name_Id
        := AIN.Display_Name (AIN.Identifier (E));
hana's avatar
hana committed
232
233
      Dispatch : constant Supported_Thread_Dispatch_Protocol :=
        Get_Thread_Dispatch_Protocol (E);
234
   begin
hana's avatar
update:    
hana committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
      if not AINU.Is_Empty (Features (E)) then
         S := AIN.First_Node (Features (E));
         loop
            if (AIN.Kind (S) = K_Port_Spec_Instance) then
               if Connections_Number (S) > 1 then
                  Display_Located_Error (AIN.Loc (S),
                   "LNT generation requires 1 to 1 connections",
                Fatal => True);
               end if;
               if (AIN.Is_In (S))  and then
                  (AIN.Is_Event (S))
               then
                  Event_Port_Number := Event_Port_Number + 1;
               end if;
            end if;
            S := AIN.Next_Node (S);
            exit when No (S);
         end loop;
      end if;
254
      Th.Identifier := New_Identifier (Thread_Identifier, "Thread_");
hana's avatar
hana committed
255
256
257
258
      if (((Dispatch = Thread_Periodic) or else
           (Dispatch = Thread_Sporadic) or else
           (Dispatch = Thread_Timed) or else
           (Dispatch = Thread_Hybrid)) and then
259
260
261
262
263
          (Get_Execution_Time (E) /= Empty_Time_Array))
      then
         Period := Natural (Get_Thread_Period (E).T);
         Th.Period := Period;
         Th.Capacity := Natural (Get_Execution_Time (E)(1).T);
hana's avatar
hana committed
264
         Th.Dispatch_Protocol := Dispatch;
hana's avatar
update:    
hana committed
265
         Th.Event_Port_Number := Event_Port_Number;
266
267
268
269
270
271
         T (Counter) := Th;
         P (Counter) := Period;
         Counter := Counter + 1;
      else
         Display_Located_Error (AIN.Loc (E),
                "LNT generation requires the definition " &
hana's avatar
hana committed
272
273
                "of Dispatch_Protocol (Periodic, Sporadic," &
                " Timed or Hybrid), " &
274
275
276
277
278
279
280
281
282
283
284
285
                "Execution_Time and Period properties.",
                Fatal => True);
      end if;
   end Visit_Thread_Instance;

   ----------------------------
   --  Make_LNT_RM_Processor --
   ----------------------------
   procedure Make_LNT_RM_Processor (T : Thread_Array) is
      N : Node_Id;
      N_Sort : Node_Id;
      L_While : List_Id;
hana's avatar
hana committed
286
      L_Else_Running_State : List_Id;
287
288
289
      N_Act : Node_Id;
      Aux_Act_1 : Node_Id;
      Aux_Act_2 : Node_Id;
hana's avatar
hana committed
290

291
      Is_Not_Periodic : Boolean := false;
292
293
      Threads : Thread_Array := T;
      L_Processor_Gates   : List_Id;
hana's avatar
hana committed
294
      L_Thread_Activation : List_Id;
295
296
297
      L_Threads_Array     : List_Id;
      L_Stop_Orders       : List_Id;
      L_Incoming_Event    : List_Id;
hana's avatar
hana committed
298
299
300
      N_Ready_State      : Node_Id;
      Thread_Activation : Node_Id;
      Aux_Thread_Activation : Node_Id;
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
      L_In_Then           : List_Id;
      function Make_Var_declaration_List return List_Id;
      function Make_Var_declaration_List
        return List_Id is
         N_Var_Declarations : List_Id;
      begin
         N_Var_Declarations := New_List;

         BLNu.Append_Node_To_List (Make_Var_Declaration
         (Make_Identifier ("Threads"),
          Make_Identifier ("LNT_Type_Thread_Array")), N_Var_Declarations);

         BLNu.Append_Node_To_List (Make_Var_Declaration
         (Make_Identifier ("Counter"),
          Make_Identifier ("LNT_Type_Time_Constraint")),
          N_Var_Declarations);

         BLNu.Append_Node_To_List (Make_Var_Declaration
         (Make_Identifier ("I"),
          Make_Identifier ("Nat")), N_Var_Declarations);

         BLNu.Append_Node_To_List (Make_Var_Declaration
         (Make_Identifier ("k"),
          Make_Identifier ("Nat")), N_Var_Declarations);

hana's avatar
hana committed
326
         if (Not_Periodic_Thread_Number > 0) then
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
            BLNu.Append_Node_To_List (Make_Var_Declaration
             (Make_Identifier ("Is_Activated"),
              Make_Identifier ("bool")), N_Var_Declarations);
         end if;

         BLNu.Append_Node_To_List (Make_Var_Declaration
         (Make_Identifier ("Preempt"),
          Make_Identifier ("bool")), N_Var_Declarations);

         BLNu.Append_Node_To_List (Make_Var_Declaration
         (Make_Identifier ("TODO"),
          Make_Identifier ("LNT_Type_Time_Constraint")),
          N_Var_Declarations);

         BLNu.Append_Node_To_List (Make_Var_Declaration
         (Make_Identifier ("Nb_Active_Thread"),
          Make_Identifier ("Nat")), N_Var_Declarations);

         return N_Var_Declarations;
      end Make_Var_declaration_List;

   begin

      Quick_Sort_Threads (Threads);
      L_Processor_Gates := New_List;
      L_Thread_Activation := New_List;
hana's avatar
hana committed
353
      L_Stop_Orders  := New_List;
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
      L_Incoming_Event  := New_List;

      L_Threads_Array := New_List (
          Make_Assignment_Statement (
            Make_Identifier ("Counter"),
            Make_Time_Const (0)),

          Make_Assignment_Statement (
            Make_Identifier ("Threads"),
            Make_Constructed_Pattern (
              Make_Identifier ("LNT_Type_Thread_Array"),
              New_List (
                Make_Constructed_Pattern (
                  Make_Identifier ("LNT_Type_Thread"),
                  New_List (Make_Time_Const (0)))))));

hana's avatar
hana committed
370
371
      for I in Threads'Range loop
         --  ACTIVATION_I identifier
372
373
374
375
376
377
378
379
380
381
382
383
384
385
         N_Act := New_Identifier (
           Remove_Prefix_From_Name (
           " ", Get_String_Name (Integer'Image (I))),
           "ACTIVATION_");

         Aux_Act_1 := BLNu.Make_Node_Container (N_Act);
         Aux_Act_2 := BLNu.Make_Node_Container (N_Act);

         BLNu.Append_Node_To_List (
             Make_Gate_Declaration (
               Make_Identifier ("LNT_Channel_Dispatch"),
               N_Act),
             L_Processor_Gates);

hana's avatar
hana committed
386
387
         BLNu.Append_Node_To_List (Aux_Act_2,
             L_Thread_Activation);
388
389
390
391
392
393
394
395
396
397
398

         BLNu.Append_Node_To_List (
           Make_Communication_Statement
             (Aux_Act_1, New_List (Make_Identifier ("T_Stop")),
             false, No_Node),
             L_Stop_Orders);

--  ---------------------------------------------
--  Thread ([0][1][2][3][4][5][6][7][8][9][10][11])
--  Thread ([C][P][0][1][0][P][0][0][0][1][1 ][0 ]) P
--  Thread ([C][P][0][0][0][P][0][0][0][1][0 ][1 ]) S
hana's avatar
hana committed
399
400
--  Thread ([C][P][0][0][0][P][0][0][0][1][1 ][2 ]) T
--  Thread ([C][P][0][0][0][P][0][0][0][1][1 ][3 ]) H
401
--  ---------------------------------------------
hana's avatar
hana committed
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

         case Threads (I).Dispatch_Protocol is
            when Thread_Periodic =>
               Is_Not_Periodic := false;
               BLNu.Append_Node_To_List (
                 Make_Array_Element_Assignment_Statement
                 (Make_Identifier ("Threads"),
                  Make_Identifier (Integer'Image (I)),
                  Make_Constructed_Pattern
                   (Make_Identifier ("LNT_Type_Thread"),
                    New_List (
                           Make_Time_Const (Threads (I).Capacity),
                           Make_Time_Const (Threads (I).Period),
                           Make_Time_Const (0),
                           Make_Time_Const (1),
                           Make_Time_Const (0),
                           Make_Time_Const (Threads (I).Period),
                           Make_Time_Const (0),
                           Make_Time_Const (0),
                           Make_Time_Const (0),
                           Make_Time_Const (1),
                           Make_Time_Const (1),
                           Make_Time_Const (0)))),
                 L_Threads_Array);
            when Thread_Sporadic =>
hana's avatar
update:    
hana committed
427
               Is_Not_Periodic := true;
hana's avatar
hana committed
428
429
430
431
432
433
               BLNu.Append_Node_To_List (
                Make_Array_Element_Assignment_Statement
                 (Make_Identifier ("Threads"),
                  Make_Identifier (Integer'Image (I)),
                  Make_Constructed_Pattern
                   (Make_Identifier ("LNT_Type_Thread"),
434
435
436
437
438
439
440
441
442
443
444
445
446
                    New_List (
                           Make_Time_Const (Threads (I).Capacity),
                           Make_Time_Const (Threads (I).Period),
                           Make_Time_Const (0),
                           Make_Time_Const (0),
                           Make_Time_Const (0),
                           Make_Time_Const (Threads (I).Period),
                           Make_Time_Const (0),
                           Make_Time_Const (0),
                           Make_Time_Const (0),
                           Make_Time_Const (1),
                           Make_Time_Const (0),
                           Make_Time_Const (1)))),
hana's avatar
hana committed
447
448
                L_Threads_Array);

hana's avatar
update:    
hana committed
449
450
               Make_Notif (Threads (I).Event_Port_Number,
                 I, "Sporadic_Notif", L_Incoming_Event);
hana's avatar
hana committed
451
            when Thread_Timed =>
hana's avatar
update:    
hana committed
452
               Is_Not_Periodic := true;
hana's avatar
hana committed
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
               BLNu.Append_Node_To_List (
                Make_Array_Element_Assignment_Statement
                 (Make_Identifier ("Threads"),
                  Make_Identifier (Integer'Image (I)),
                  Make_Constructed_Pattern
                   (Make_Identifier ("LNT_Type_Thread"),
                    New_List (
                           Make_Time_Const (Threads (I).Capacity),
                           Make_Time_Const (Threads (I).Period),
                           Make_Time_Const (0),
                           Make_Time_Const (0),
                           Make_Time_Const (0),
                           Make_Time_Const (Threads (I).Period),
                           Make_Time_Const (0),
                           Make_Time_Const (0),
                           Make_Time_Const (0),
                           Make_Time_Const (1),
                           Make_Time_Const (1),
                           Make_Time_Const (2)))),
                L_Threads_Array);
hana's avatar
update:    
hana committed
473
474
               Make_Notif (Threads (I).Event_Port_Number,
                 I, "Timed_Hybrid_Notif", L_Incoming_Event);
hana's avatar
hana committed
475
            when Thread_Hybrid =>
hana's avatar
update:    
hana committed
476
               Is_Not_Periodic := true;
hana's avatar
hana committed
477
478
479
480
481
482
               BLNu.Append_Node_To_List (
                Make_Array_Element_Assignment_Statement
                 (Make_Identifier ("Threads"),
                  Make_Identifier (Integer'Image (I)),
                  Make_Constructed_Pattern
                   (Make_Identifier ("LNT_Type_Thread"),
483
484
485
486
487
488
489
490
491
492
493
494
                    New_List (
                           Make_Time_Const (Threads (I).Capacity),
                           Make_Time_Const (Threads (I).Period),
                           Make_Time_Const (0),
                           Make_Time_Const (1),
                           Make_Time_Const (0),
                           Make_Time_Const (Threads (I).Period),
                           Make_Time_Const (0),
                           Make_Time_Const (0),
                           Make_Time_Const (0),
                           Make_Time_Const (1),
                           Make_Time_Const (1),
hana's avatar
hana committed
495
496
                           Make_Time_Const (3)))),
                L_Threads_Array);
hana's avatar
update:    
hana committed
497
498
               Make_Notif (Threads (I).Event_Port_Number,
                 I, "Timed_Hybrid_Notif", L_Incoming_Event);
hana's avatar
hana committed
499
500
501
502
503
504
505
506
507
508
509
            when others =>
               null;
         end case;

         --  instances list for Main generation
         BLNu.Append_Node_To_List (
            Make_Process_Instantiation_Statement (
                Make_Identifier (Threads (I).Identifier),
                No_List, No_List, Is_Not_Periodic),
                LNT_Thread_Instance_List);

510
      end loop;
hana's avatar
hana committed
511
512
513
514
515
516
517
518
519
      Thread_Activation := Make_Process_Instantiation_Statement
             (Make_Identifier ("Activate_k"),
              L_Thread_Activation,
              New_List (
               Make_Actual_Parameter
                (Make_Identifier ("Threads"), false, true),
               Make_Actual_Parameter
                (Make_Identifier ("k"))));
      Aux_Thread_Activation := BLNu.Make_Node_Container (Thread_Activation);
520

hana's avatar
hana committed
521
      L_Else_Running_State :=  New_List (
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
          Make_Assignment_Statement (
            Make_Identifier ("Preempt"),
            Make_Identifier ("true")),
----------------------------  assi TODO := Threads [K][0] - Threads [K][2];
          Make_Assignment_Statement (
            Make_Identifier ("TODO"),
            Make_Infix_Function_Call_Expression (
              Make_Identifier ("-"),
              Make_Array_Elt_Access_Expression (
                Make_Identifier ("0"),
                Make_Array_Elt_Access_Expression (
                  Make_Identifier ("K"),
                  Make_Identifier ("Threads"))),
              Make_Array_Elt_Access_Expression (
                Make_Nat (2),
                Make_Array_Elt_Access_Expression (
                  Make_Identifier ("K"),
                  Make_Identifier ("Threads"))))),
---------------------------  assi I := 1;
          Make_Assignment_Statement (
            Make_Identifier ("I"),
            Make_Nat (1)),
----------------------------  while
          Make_While_Statement (
            Make_Parenthesized_Expression (
            Make_Infix_Function_Call_Expression (
              Make_Predefined_Function (K_And),
              Make_Parenthesized_Expression (
              Make_Infix_Function_Call_Expression (
                Make_Predefined_Function (K_Less_Than_Or_Equal_To, false),
                Make_Identifier ("I"),
                Make_Parenthesized_Expression (
                  Make_Infix_Function_Call_Expression (
                    Make_Identifier ("-"),
                    Make_Identifier ("K"),
                    Make_Nat (1))))),
              Make_Identifier ("Preempt"))),

            New_List (
              Make_If_Statement (
               Make_Infix_Function_Call_Expression (
                Make_Predefined_Function (K_And),
                Make_Parenthesized_Expression (
                Make_Infix_Function_Call_Expression (
                  Make_Predefined_Function (K_Equality, false),
                    Make_Array_Elt_Access_Expression (
                      Make_Nat (10),
                      Make_Array_Elt_Access_Expression (
                      Make_Identifier ("I"),
                      Make_Identifier ("Threads"))),
                    Make_Time_Const (1))),

                Make_Infix_Function_Call_Expression (
                 Make_Predefined_Function (K_And),
                  Make_Parenthesized_Expression (
                  Make_Infix_Function_Call_Expression (
                    Make_Predefined_Function (K_Equality, false),
                      Make_Array_Elt_Access_Expression (
                        Make_Nat (9),
                        Make_Array_Elt_Access_Expression (
                        Make_Identifier ("I"),
                        Make_Identifier ("Threads"))),
                       Make_Time_Const (1))),
                 Make_Parenthesized_Expression (
                 Make_Infix_Function_Call_Expression (
                  Make_Predefined_Function (K_Greater_Than, false),
                  Make_Infix_Function_Call_Expression (
                    Make_Identifier ("+"),
                    Make_Identifier ("Counter"),
                    Make_Identifier ("TODO")),
                  Make_Array_Elt_Access_Expression (
                      Make_Nat (4),
                      Make_Array_Elt_Access_Expression (
                        Make_Identifier ("I"),
                        Make_Identifier ("Threads")))))
                     )),
                New_List (
                  Make_Assignment_Statement (
                    Make_Identifier ("TODO"),
                    Make_Infix_Function_Call_Expression (
                      Make_Identifier ("-"),
                      Make_Array_Elt_Access_Expression (
                        Make_Nat (4),
                        Make_Array_Elt_Access_Expression (
                          Make_Identifier ("I"),
                          Make_Identifier ("Threads"))),
                        Make_Identifier ("Counter"))),
                  Make_Assignment_Statement (
                    Make_Identifier ("Preempt"),
                    Make_Identifier ("false"))),
                No_List,
                New_List (Make_Assignment_Statement (
                  Make_Identifier ("I"),
                    Make_Infix_Function_Call_Expression (
                      Make_Identifier ("+"),
                      Make_Identifier ("I"),
                      Make_Nat (1))))
                 ))),
-----------------------------  assi Counter := Counter + TODO;
          Make_Assignment_Statement (
            Make_Identifier ("Counter"),
            Make_Infix_Function_Call_Expression (
              Make_Identifier ("+"),
              Make_Identifier ("Counter"),
              Make_Identifier ("TODO"))),
----------------------------  assi Nb_Active_Thread := Nb_Active_Thread + 1;
          Make_Assignment_Statement (
            Make_Identifier ("Nb_Active_Thread"),
            Make_Infix_Function_Call_Expression (
              Make_Identifier ("+"),
              Make_Identifier ("Nb_Active_Thread"),
              Make_Nat (1))),
----------------------------
          Make_Assignment_Statement (
               Make_Identifier ("Threads"),
               Make_Function_Call_Expression (
                Make_Identifier ("Update_Thread"),
                New_List (
                  Make_Identifier ("Threads"),
                  Make_Identifier ("K"),
                  Make_Identifier ("TODO")))));

hana's avatar
hana committed
644
645
646
      BLNu.Append_Node_To_List (Aux_Thread_Activation, L_Else_Running_State);
      BLNu.Append_Node_To_List (Make_If_Statement
                    (Make_Identifier ("Preempt"),
647
648
                     New_List (Make_Assignment_Statement
                    (Make_Identifier ("k"),
hana's avatar
hana committed
649
                     Make_Identifier ("I"))),
650
651
652
653
654
655
                     No_List,
                     New_List (Make_Assignment_Statement
                    (Make_Identifier ("k"),
                      Make_Infix_Function_Call_Expression
                          (Make_Identifier ("+"),
                           Make_Identifier ("k"),
hana's avatar
hana committed
656
657
                           Make_Nat (1))))),
            L_Else_Running_State);
658

hana's avatar
hana committed
659
660
661
662
      N_Ready_State := Make_If_Statement
              (Make_Infix_Function_Call_Expression (
                    Make_Predefined_Function
                     (K_Greater_Than_Or_Equal_To, false),
663
664
                    Make_Identifier ("Counter"),
                    Make_Array_Elt_Access_Expression (
hana's avatar
hana committed
665
                      Make_Nat (5),
666
667
                      Make_Array_Elt_Access_Expression (
                      Make_Identifier ("K"),
hana's avatar
hana committed
668
669
670
671
                      Make_Identifier ("Threads")))),
            --  then Missed_Deadline state
            New_List (
                Make_Assignment_Statement (
672
673
674
675
676
677
678
                   Make_Identifier ("Threads"),
                   Make_Function_Call_Expression
                     (Make_Identifier ("Assign"),
                       New_List (
                        Make_Identifier ("Threads"),
                        Make_Identifier ("K"),
                        Make_Nat (9),
hana's avatar
hana committed
679
680
681
682
683
684
685
686
687
688
689
690
691
692
                        Make_Time_Const (0)))),
                Thread_Activation,
                Make_Assignment_Statement
                    (Make_Identifier ("k"),
                      Make_Infix_Function_Call_Expression
                          (Make_Identifier ("+"),
                           Make_Identifier ("k"),
                           Make_Nat (1)))),
            No_List,
            --  else Running state
            L_Else_Running_State);

      L_While := New_List (
           Make_If_Statement (
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
               Make_Infix_Function_Call_Expression
               (Make_Predefined_Function (K_And),
                Make_Parenthesized_Expression (
                Make_Infix_Function_Call_Expression (
                  Make_Predefined_Function (K_Equality, false),
                    Make_Array_Elt_Access_Expression (
                      Make_Nat (10),
                      Make_Array_Elt_Access_Expression (
                      Make_Identifier ("K"),
                      Make_Identifier ("Threads"))),
                    Make_Time_Const (1))),

               Make_Infix_Function_Call_Expression
                (Make_Predefined_Function (K_And),
                  Make_Parenthesized_Expression (
                  Make_Infix_Function_Call_Expression (
                    Make_Predefined_Function (K_Equality, false),
                      Make_Array_Elt_Access_Expression (
                        Make_Nat (9),
                        Make_Array_Elt_Access_Expression (
                        Make_Identifier ("K"),
                        Make_Identifier ("Threads"))),
                       Make_Time_Const (1))),
hana's avatar
hana committed
716
717
718
719
720
721
722
723
724
725
726
727
728
729
                   Make_Parenthesized_Expression (
                   Make_Infix_Function_Call_Expression (
                    Make_Predefined_Function
                     (K_Greater_Than_Or_Equal_To, false),
                    Make_Identifier ("Counter"),
                    Make_Array_Elt_Access_Expression (
                      Make_Nat (4),
                      Make_Array_Elt_Access_Expression (
                      Make_Identifier ("K"),
                      Make_Identifier ("Threads")))))
                     )),
        --  test Missed_Deadline state
        New_List (N_Ready_State),
        No_List,
730
731
732
733
734
735
        New_List (Make_Assignment_Statement
                    (Make_Identifier ("k"),
                      Make_Infix_Function_Call_Expression
                          (Make_Identifier ("+"),
                           Make_Identifier ("k"),
                           Make_Nat (1))))));
hana's avatar
hana committed
736
737
738
739
740
741
742
743
744
745
746
      if (Not_Periodic_Thread_Number > 0) then
         BLNu.Append_Node_To_List (Make_Assignment_Statement
                    (Make_Identifier ("Is_Activated"),
                     Make_Identifier ("false")), L_While);
         BLNu.Append_List_To_List (L_Incoming_Event, L_While);
         BLNu.Append_Node_To_List (Make_If_Statement
                    (Make_Identifier ("Is_Activated"),
                     New_List (Make_Assignment_Statement
                    (Make_Identifier ("k"),
                     Make_Nat (1))),
                     No_List,
747
                     No_List), L_While);
hana's avatar
hana committed
748
749
750

      end if;

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
      L_In_Then := New_List (
                   Make_Assignment_Statement
                    (Make_Identifier ("Nb_Active_Thread"),
                     Make_Identifier ("0")),
                   Make_Assignment_Statement
                    (Make_Identifier ("K"),
                     Make_Nat (1)),

                   Make_While_Statement (
                    Make_Parenthesized_Expression (
                     Make_Infix_Function_Call_Expression
                      (Make_Predefined_Function
                       (K_Less_Than_Or_Equal_To, false),
                        Make_Identifier ("K"),
                        Make_Identifier ("Thread_Number"))),
                     L_While),

                   Make_If_Statement
                    (Make_Infix_Function_Call_Expression
                      (Make_Predefined_Function
                        (K_Equality, false),
                       Make_Identifier ("Nb_Active_Thread"),
                       Make_Identifier ("0")),
                     New_List (
                       Make_Assignment_Statement
                        (Make_Identifier ("Counter"),
                         Make_Infix_Function_Call_Expression
                          (Make_Identifier ("+"),
                           Make_Identifier ("Counter"),
                           Make_Time_Const (1)))),
                     No_List,
                     No_List));

      N_Sort := Make_If_Statement
                (Make_Infix_Function_Call_Expression
                  (Make_Predefined_Function
                     (K_Less_Than, false),
                   Make_Identifier ("Counter"),
                   Make_Identifier ("PPCM_THREAD")),
                 L_In_Then,
                 No_List,
hana's avatar
hana committed
792
                 L_Stop_Orders);
793
794
795
796
797

      N := Make_Process_Definition
      (No_Node,
       Make_Identifier ("Processor"),
       No_List,
hana's avatar
update:    
hana committed
798
       No_List, -- to be added in main generation
799
800
801
802
803
804
       No_List,
       No_List,
       New_List (
         Make_Var_Loop_Select (
           Make_Var_declaration_List,
           L_Threads_Array,
hana's avatar
hana committed
805
           New_List (N_Sort), false)));
hana's avatar
update:    
hana committed
806
      The_Processor := BLNu.Make_Node_Container (N);
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
      BLNu.Append_Node_To_List (N, Definitions_List);
   end Make_LNT_RM_Processor;

   --------------------------
   --  Threads_Hyperperiod --
   --------------------------
   function Threads_Hyperperiod (T : Period_Array)
    return Natural is
      function Array_Min (Ar : Period_Array) return Natural;
      function Array_Min (Ar : Period_Array) return Natural is
         A : Natural := Ar (1);
      begin
         for i in 2 .. Ar'Length loop
            if Ar (i) < A then
               A := Ar (i);
            end if;
         end loop;
         return A;
      end Array_Min;
      Aux : Period_Array := T;
      Min : Natural;
      Loop_End : boolean;
   begin
      loop
         Loop_End := true;
         Min := Array_Min (Aux);
         for i in T'Range loop
            if Aux (i) > Min then
               Loop_End := false;
               exit;
            end if;
         end loop;
         exit when Loop_End;
         for i in T'Range loop
            if Aux (i) = Min then
               Aux (i) := Aux (i) + T (i);
            end if;
         end loop;
      end loop;
      return Min;

   end Threads_HyperPeriod;

   -------------------------------------
   -- Make_LNT_RM_Processor_Functions --
   -------------------------------------
   procedure Make_LNT_RM_Processor_Functions is
      N : Node_Id;
      L_Sts_S : List_Id;
      L_Sts_A : List_Id;
hana's avatar
hana committed
857
858
859
860
      N_Case_Update : Node_Id;
      Activate_K_Gates_List : List_Id;
      Activate_K_Case_Sts_List : List_Id;
      Activate_K_Case_St : Node_Id;
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
   begin

      N := Make_Function_Definition
        (Make_Identifier ("Assign"),
         New_List (
          Make_Parameter_Specification
             (Make_Identifier ("Threads"),
              Make_Identifier ("LNT_Type_Thread_Array"),
              Mode_Out),
          Make_Parameter_Specification
             (Make_Identifier ("I"),
              Make_Identifier ("Nat"),
              Mode_In),
          Make_Parameter_Specification
             (Make_Identifier ("K"),
              Make_Identifier ("Nat"),
              Mode_In),
          Make_Parameter_Specification
             (Make_Identifier ("Val"),
              Make_Identifier ("LNT_Type_Time_Constraint"),
              Mode_In)),
         Make_Identifier ("LNT_Type_Thread_Array"),
         No_List,
         No_List,
         New_List (
           Make_Var_Statement
            (New_List (Make_Var_Declaration
              (Make_Identifier ("P"),
               Make_Identifier ("LNT_Type_Thread"))),
             New_List (
               Make_Assignment_Statement
               (Make_Identifier ("P"),
                Make_Array_Elt_Access_Expression
                 (Make_Identifier ("I"),
                  Make_Identifier ("Threads"))),
               Make_Array_Element_Assignment_Statement
                   (Make_Identifier ("P"),
                    Make_Identifier ("K"),
                    Make_Identifier ("Val")),
               Make_Array_Element_Assignment_Statement
                   (Make_Identifier ("Threads"),
                    Make_Identifier ("I"),
                    Make_Identifier ("P")))),
           Make_Return_Statement
           (Make_Identifier ("Threads"), true))
         );
      BLNu.Append_Node_To_List (N, Definitions_List);

hana's avatar
hana committed
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
      N_Case_Update := Make_Case_Statement (
         Make_Pattern (
            Make_Array_Elt_Access_Expression (
                 Make_Nat (11),
                 Make_Identifier ("P")),
            Make_Identifier ("LNT_Type_Time_Constraint"),
            false,
            true),
          No_List,
          New_List (
           --  Sporadic
           Make_Case_Statement_Alternative (
              New_List (Make_Time_Const (1)),
              New_List (Make_Array_Element_Assignment_Statement
                     (Make_Identifier ("P"),
                      Make_Nat (4),
                      Make_Array_Elt_Access_Expression
                         (Make_Nat (5),
                          Make_Identifier ("P"))),
                     Make_Array_Element_Assignment_Statement
                     (Make_Identifier ("P"),
                      Make_Nat (5),
                      Make_Identifier ("PPCM_THREAD")),
                     Make_Array_Element_Assignment_Statement
                     (Make_Identifier ("P"),
                      Make_Nat (10),
                      Make_Time_Const (0)))),
           --  Hybrid and Periodic
           Make_Case_Statement_Alternative (
              New_List (Make_Time_Const (0), Make_Time_Const (3)),
              New_List (Make_If_Statement (
                   Make_Infix_Function_Call_Expression (
                   Make_Predefined_Function (K_And),
                   Make_Parenthesized_Expression (
                     Make_Infix_Function_Call_Expression
                      (Make_Predefined_Function
                       (K_Less_Than_Or_Equal_To, false),
                       Make_Infix_Function_Call_Expression
                      (Make_Identifier ("*"),
                       Make_Infix_Function_Call_Expression
                      (Make_Identifier ("+"),
                       Make_Array_Elt_Access_Expression
                         (Make_Nat (3),
                          Make_Identifier ("P")),
                       Make_Time_Const (1)),
                       Make_Array_Elt_Access_Expression
                         (Make_Nat (1),
                          Make_Identifier ("P"))),
                       Make_Identifier ("PPCM_THREAD"))),
                   Make_Parenthesized_Expression (
                     Make_Infix_Function_Call_Expression
                      (Make_Predefined_Function
                       (K_Less_Than_Or_Equal_To, false),
                       Make_Infix_Function_Call_Expression
                      (Make_Identifier ("+"),
                       Make_Array_Elt_Access_Expression
                         (Make_Nat (4),
                          Make_Identifier ("P")),
                       Make_Array_Elt_Access_Expression
                         (Make_Nat (1),
                          Make_Identifier ("P"))),
                          Make_Identifier ("PPCM_THREAD")))),
                 New_List (
                   Make_Array_Element_Assignment_Statement
                     (Make_Identifier ("P"),
                      Make_Nat (4),
                      Make_Infix_Function_Call_Expression
                      (Make_Identifier ("*"),
                       Make_Array_Elt_Access_Expression
                         (Make_Nat (3),
                          Make_Identifier ("P")),
                       Make_Array_Elt_Access_Expression
                         (Make_Nat (1),
                          Make_Identifier ("P")))),
                   Make_Array_Element_Assignment_Statement
                     (Make_Identifier ("P"),
                      Make_Nat (3),
                      Make_Infix_Function_Call_Expression
                      (Make_Identifier ("+"),
                       Make_Array_Elt_Access_Expression
                         (Make_Nat (3),
                          Make_Identifier ("P")),
                       Make_Time_Const (1))),
                   Make_Array_Element_Assignment_Statement
                     (Make_Identifier ("P"),
                      Make_Nat (5),
                      Make_Infix_Function_Call_Expression
                      (Make_Identifier ("*"),
                       Make_Array_Elt_Access_Expression
                         (Make_Nat (3),
                          Make_Identifier ("P")),
                       Make_Array_Elt_Access_Expression