ocarina-backends-mast_tree-generator.adb 19.8 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
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--                  OCARINA.BACKENDS.MAST_TREE.GENERATOR                    --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--            Copyright (C) 2010, European Space Agency (ESA).              --
--                                                                          --
-- Ocarina  is free software;  you  can  redistribute  it and/or  modify    --
-- it under terms of the GNU General Public License as published by the     --
-- Free Software Foundation; either version 2, or (at your option) any      --
-- later version. Ocarina is distributed  in  the  hope  that it will be    --
-- useful, but WITHOUT ANY WARRANTY;  without even the implied warranty of  --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General --
-- Public License for more details. You should have received  a copy of the --
-- GNU General Public License distributed with Ocarina; see file COPYING.   --
-- If not, write to the Free Software Foundation, 51 Franklin Street, Fifth --
-- Floor, Boston, MA 02111-1301, USA.                                       --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable to be   --
-- covered  by the  GNU  General  Public  License. This exception does not  --
-- however invalidate  any other reasons why the executable file might be   --
-- covered by the GNU Public License.                                       --
--                                                                          --
--                 Ocarina is maintained by the Ocarina team                --
--                       (ocarina-users@listes.enst.fr)                     --
--                                                                          --
------------------------------------------------------------------------------

with Namet;  use Namet;
with Output; use Output;
--  with Utils;  use Utils;

with GNAT.OS_Lib; use GNAT.OS_Lib;

--  with Ocarina.Backends.Utils;
with Ocarina.Backends.MAST_Tree.Nodes;
with Ocarina.Backends.MAST_Tree.Nutils;
with Ocarina.Backends.MAST_Values;
with Ocarina.Backends.Messages;

package body Ocarina.Backends.MAST_Tree.Generator is

--   use Ocarina.Backends.Utils;
   use Ocarina.Backends.MAST_Tree.Nodes;
   use Ocarina.Backends.MAST_Tree.Nutils;
   use Ocarina.Backends.MAST_Values;
   use Ocarina.Backends.Messages;

julien.delange's avatar
julien.delange committed
54
55
   package MTN renames Ocarina.Backends.MAST_Tree.Nodes;

56
57
58
   procedure Generate_Defining_Identifier (N : Node_Id);
   procedure Generate_Literal (N : Node_Id);
   procedure Generate_MAST_File (N : Node_Id);
59
   procedure Generate_Processing_Resource (N : Node_Id);
julien.delange's avatar
julien.delange committed
60
   procedure Generate_Scheduling_Server (N : Node_Id);
61
62
63
64
   procedure Generate_Transaction (N : Node_Id);
   procedure Generate_Event (N : Node_Id);
   procedure Generate_Event_Handler (N : Node_Id);
   procedure Generate_Operation (N : Node_Id);
julien.delange's avatar
julien.delange committed
65
   procedure Generate_Event_Timing_Requirements (N : Node_Id);
66
   procedure Generate_Scheduling_Server_Parameters (N : Node_Id);
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

   procedure Write (T : Token_Type);
   procedure Write_Line (T : Token_Type);

   function Get_File_Name (N : Node_Id) return Name_Id;
   --  Generate a file name from the package node given as parameter

   procedure Release_Output (Fd : File_Descriptor);
   --  Releases the output by closing the opened files

   function Set_Output (N : Node_Id) return File_Descriptor;
   --  Adjust the output depending on the command line options and
   --  return a file descriptor in order to be able to close it.

   -------------------
   -- Get_File_Name --
   -------------------

   function Get_File_Name (N : Node_Id) return Name_Id is
      Suffix : constant String := ".txt";
   begin
      --  The File name corresponding is the lowerd name of N

      Get_Name_String
        (Conventional_Base_Name
         (Name
          (Defining_Identifier
           (N))));

      --  Adding file suffix

      Add_Str_To_Name_Buffer (Suffix);

      return Name_Find;
   end Get_File_Name;

   ----------------
   -- Set_Output --
   ----------------

   function Set_Output (N : Node_Id) return File_Descriptor is
   begin
      if not Print_On_Stdout then
         declare
            File_Name : constant Name_Id
              := Get_File_Name (N);
            Fd : File_Descriptor;
         begin
            Get_Name_String (File_Name);

            --  Create a new file and overwrites existing file with
            --  the same name

            Fd := Create_File
               (Name_Buffer (1 .. Name_Len), Text);

            if Fd = Invalid_FD then
               raise Program_Error;
            end if;

            --  Setting the output

            Set_Output (Fd);
            return Fd;
         end;
      end if;

      return Invalid_FD;
   end Set_Output;

   --------------------
   -- Release_Output --
   --------------------

   procedure Release_Output (Fd : File_Descriptor) is
   begin
      if not Print_On_Stdout and then Fd /= Invalid_FD then
         Set_Standard_Output;
         Close (Fd);
      end if;
   end Release_Output;

   --------------
   -- Generate --
   --------------

   procedure Generate (N : Node_Id) is
   begin
      case Kind (N) is
         when K_MAST_File =>
            Generate_MAST_File (N);

         when K_Defining_Identifier =>
            Generate_Defining_Identifier (N);

         when K_Literal =>
            Generate_Literal (N);

165
166
167
168
169
170
         when K_Event =>
            Generate_Event (N);

         when K_Event_Handler =>
            Generate_Event_Handler (N);

171
172
173
         when K_Processing_Resource =>
            Generate_Processing_Resource (N);

julien.delange's avatar
julien.delange committed
174
175
176
         when K_Scheduling_Server =>
            Generate_Scheduling_Server (N);

177
178
179
180
181
182
         when K_Transaction =>
            Generate_Transaction (N);

         when K_Operation =>
            Generate_Operation (N);

julien.delange's avatar
julien.delange committed
183
184
185
         when K_Event_Timing_Requirements =>
            Generate_Event_Timing_Requirements (N);

186
187
188
         when K_Scheduling_Server_Parameters =>
            Generate_Scheduling_Server_Parameters (N);

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
         when others =>
            Display_Error ("other element in generator", Fatal => False);
            null;
      end case;
   end Generate;

   ----------------------------------
   -- Generate_Defining_Identifier --
   ----------------------------------

   procedure Generate_Defining_Identifier (N : Node_Id) is
   begin
      Write_Name (Name (N));
   end Generate_Defining_Identifier;

   -----------
   -- Write --
   -----------

   procedure Write (T : Token_Type) is
   begin
      Write_Name (Token_Image (T));
   end Write;

   ----------------
   -- Write_Line --
   ----------------

   procedure Write_Line (T : Token_Type) is
   begin
      Write (T);
      Write_Eol;
   end Write_Line;

   ----------------------
   -- Generate_Literal --
   ----------------------

   procedure Generate_Literal (N : Node_Id) is
   begin
      Write_Str (Image (Value (N)));
   end Generate_Literal;

   -----------------------
   -- Generate_MAST_File --
   -----------------------

   procedure Generate_MAST_File (N : Node_Id) is
      Fd : File_Descriptor;
      F : Node_Id;
   begin
      if No (N) then
         return;
      end if;
      Fd := Set_Output (N);
      if not Is_Empty (Declarations (N)) then
         F := First_Node (Declarations (N));
         while Present (F) loop
            Generate (F);
            F := Next_Node (F);
         end loop;
      end if;

      Release_Output (Fd);
   end Generate_MAST_File;

255
256
257
258
259
260
261
262
263
264
   ----------------------------------
   -- Generate_Processing_Resource --
   ----------------------------------

   procedure Generate_Processing_Resource (N : Node_Id) is
   begin
      Write_Line ("Processing_Resource (");
      Increment_Indentation;

      Write_Indentation (-1);
julien.delange's avatar
julien.delange committed
265
266
      if Regular_Processor (N) then
         Write_Line ("Type => Regular_Processor,");
267
268
      elsif Fixed_Priority_Processor (N) then
         Write_Line ("Type => Fixed_Priority_Processor,");
julien.delange's avatar
julien.delange committed
269
270
271
      else
         Write_Line ("Type => Packet_Based_Network,");
      end if;
272
273

      Write_Indentation (-1);
julien.delange's avatar
julien.delange committed
274
      Write_Str ("Name => ");
275
      Write_Name (Node_Name (N));
julien.delange's avatar
julien.delange committed
276
277
278
279
280
281
      Write_Line (Tok_Colon);

      Write_Indentation (-1);
      Write (Tok_Avg_ISR_Switch);
      Write_Space;
      Write (Tok_Assign);
julien.delange's avatar
julien.delange committed
282
      Write_Space;
julien.delange's avatar
julien.delange committed
283
284
285
286
287
288
289
290
291
292
293
      if Avg_ISR_Switch (N) /= No_Node then
         Generate (Avg_ISR_Switch (N));
      else
         Write_Str ("0.00");
      end if;
      Write_Line (Tok_Colon);

      Write_Indentation (-1);
      Write (Tok_Best_ISR_Switch);
      Write_Space;
      Write (Tok_Assign);
julien.delange's avatar
julien.delange committed
294
      Write_Space;
julien.delange's avatar
julien.delange committed
295
296
297
298
299
300
301
302
303
304
305
      if Best_ISR_Switch (N) /= No_Node then
         Generate (Best_ISR_Switch (N));
      else
         Write_Str ("0.00");
      end if;
      Write_Line (Tok_Colon);

      Write_Indentation (-1);
      Write (Tok_Worst_ISR_Switch);
      Write_Space;
      Write (Tok_Assign);
julien.delange's avatar
julien.delange committed
306
      Write_Space;
julien.delange's avatar
julien.delange committed
307
308
309
310
311
      if Worst_ISR_Switch (N) /= No_Node then
         Generate (Worst_ISR_Switch (N));
      else
         Write_Str ("0.00");
      end if;
312
313

      Write_Line (");");
julien.delange's avatar
julien.delange committed
314
      Decrement_Indentation;
315
316
   end Generate_Processing_Resource;

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
   -------------------------------------------
   -- Generate_Scheduling_Server_Parameters --
   -------------------------------------------

   procedure Generate_Scheduling_Server_Parameters (N : Node_Id) is
   begin
      Write (Tok_Type);
      Write_Space;
      Write (Tok_Assign);
      Write_Space;
      if MTN.Fixed_Priority (N) then
         Write (Tok_Fixed_Priority_Policy);
      else
         Write (Tok_Unknown);
      end if;
      Write (Tok_Colon);
      Write_Eol;

      Write_Indentation (-1);
      Write (Tok_The_Priority);
      Write_Space;
      Write (Tok_Assign);
      Write_Space;
      Generate (Priority (N));
   end Generate_Scheduling_Server_Parameters;

julien.delange's avatar
julien.delange committed
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
   --------------------------------
   -- Generate_Scheduling_Server --
   --------------------------------

   procedure Generate_Scheduling_Server (N : Node_Id) is
   begin
      Write_Line ("Scheduling_Server (");
      Increment_Indentation;

      Write_Indentation (-1);
      if Is_Regular (N) then
         Write_Line ("Type => Regular,");
      else
         Write_Str ("Type => ");
         Write_Name (Associated_Scheduler (N));
         Write_Line (Tok_Colon);
      end if;

      Write_Indentation (-1);
      Write_Str ("Name => ");
      Write_Name (Node_Name (N));
      Write_Line (Tok_Colon);

      if Parameters (N) /= No_Node then
         Write_Indentation (-1);
368
         Write (Tok_Server_Sched_Parameters);
julien.delange's avatar
julien.delange committed
369
370
371
372
         Write_Space;
         Write (Tok_Assign);
         Write_Space;
         Write (Tok_Left_Paren);
373
374
375
         Increment_Indentation;
         Write_Eol;
         Write_Indentation (-1);
julien.delange's avatar
julien.delange committed
376
377
         Generate (Parameters (N));
         Write (Tok_Right_Paren);
378
379
380
         Write (Tok_Colon);
         Decrement_Indentation;
         Write_Eol;
julien.delange's avatar
julien.delange committed
381
382
383
384
385
386
387
388
389
390
391
      end if;

      Write_Indentation (-1);
      Write (Tok_Server_Processing_Resource);
      Write_Space;
      Write (Tok_Assign);
      Write_Space;
      Write_Name (MTN.Server_Processing_Resource (N));
      Write_Line (");");
   end Generate_Scheduling_Server;

392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
   --------------------------
   -- Generate_Transaction --
   --------------------------

   procedure Generate_Transaction (N : Node_Id) is
      F : Node_Id;
   begin
      Write_Line ("Transaction (");
      Increment_Indentation;

      Write_Indentation (-1);
      if Is_Regular (N) then
         Write_Line ("Type => Regular,");
      elsif Is_Sporadic (N) then
         Write_Line ("Type => Sporadic,");
      else
         Write_Line ("Type => Periodic,");
      end if;

julien.delange's avatar
julien.delange committed
411
412
413
414
      Write_Indentation (-1);
      Write_Str ("Name => ");
      Write_Name (Node_Name (N));
      Write (Tok_Colon);
415

julien.delange's avatar
julien.delange committed
416
417
      if not Is_Empty (External_Events (N))then
         Write_Eol;
418
419
420
421
422
         Write_Indentation (-1);
         Write (Tok_External_Events);
         Write_Space;
         Write (Tok_Assign);
         Write_Space;
julien.delange's avatar
julien.delange committed
423
424
         Write (Tok_Left_Paren);
         Write_Eol;
425
         Increment_Indentation;
julien.delange's avatar
julien.delange committed
426
427
         Write_Indentation (-1);

428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
         F := First_Node (External_Events (N));
         while Present (F) loop
            Generate (F);
            if Present (Next_Node (F)) then
               Write_Eol;
            else

               Write (Tok_Right_Paren);
               Write_Line (Tok_Colon);
            end if;
            F := Next_Node (F);
         end loop;
         Decrement_Indentation;
      end if;

      if not Is_Empty (Internal_Events (N))then
         Write_Indentation (-1);
         Write (Tok_Internal_Events);
         Write_Space;
         Write (Tok_Assign);
         Write_Space;
julien.delange's avatar
julien.delange committed
449
450
451
         Write_Eol;
         Write_Indentation (-1);
         Write (Tok_Left_Paren);
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

         Increment_Indentation;
         F := First_Node (Internal_Events (N));
         while Present (F) loop
            Generate (F);
            if Present (Next_Node (F)) then
               Write_Eol;
            else

               Write (Tok_Right_Paren);
               Write_Line (Tok_Colon);
            end if;
            F := Next_Node (F);
         end loop;
         Decrement_Indentation;
      end if;

      if not Is_Empty (Event_Handlers (N))then
         Write_Indentation (-1);
         Write (Tok_Event_Handlers);
         Write_Space;
         Write (Tok_Assign);
         Write_Space;
         Write_Line (Tok_Left_Paren);

         Increment_Indentation;
         F := First_Node (Event_Handlers (N));
         while Present (F) loop
            Write_Indentation (-1);
            Generate (F);
            if Present (Next_Node (F)) then
               Write_Eol;
            else
               Write (Tok_Right_Paren);
            end if;
            F := Next_Node (F);
         end loop;
         Decrement_Indentation;
      end if;
      Write_Line (");");
   end Generate_Transaction;

   --------------------
   -- Generate_Event --
   --------------------

   procedure Generate_Event (N : Node_Id) is
   begin
      Write (Tok_Left_Paren);
      Write (Tok_Type);
      Write_Space;
      Write (Tok_Assign);
      Write_Space;
      if Is_Periodic (N) then
         Write_Str ("periodic");
      elsif Is_Sporadic (N) then
         Write_Str ("sporadic");
      else
         Write_Str ("regular");
      end if;
      Write_Line (Tok_Colon);

julien.delange's avatar
julien.delange committed
514
515
516
517
518
519
520
521
      Write_Indentation (-1);
      Write (Tok_Name);
      Write_Space;
      Write (Tok_Assign);
      Write_Space;
      Write_Name (Node_Name (N));
      Write (Tok_Colon);

julien.delange's avatar
julien.delange committed
522
      if MTN.Period (N) /= No_Node then
julien.delange's avatar
julien.delange committed
523
         Write_Eol;
julien.delange's avatar
julien.delange committed
524
525
526
527
528
529
530
531
         Write_Indentation (-1);
         Write (Tok_Period);
         Write_Space;
         Write (Tok_Assign);
         Write_Space;
         Generate (Period (N));
      end if;

532
      if Timing_Requirements (N) /= No_Node then
julien.delange's avatar
julien.delange committed
533
         Write_Eol;
534
535
536
537
         Write_Indentation (-1);
         Write (Tok_Timing_Requirements);
         Write_Space;
         Write (Tok_Assign);
julien.delange's avatar
julien.delange committed
538
539
540
541
         Write_Eol;
         Increment_Indentation;
         Write_Indentation (-1);
         Write (Tok_Left_Paren);
542
         Generate (Timing_Requirements (N));
julien.delange's avatar
julien.delange committed
543
544
         Write (Tok_Right_Paren);
         Decrement_Indentation;
545
546
547
548
      end if;
      Write (Tok_Right_Paren);
   end Generate_Event;

julien.delange's avatar
julien.delange committed
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
   ----------------------------------------
   -- Generate_Event_Timing_Requirements --
   ----------------------------------------

   procedure Generate_Event_Timing_Requirements (N : Node_Id) is
   begin
      Write (Tok_Type);
      Write_Space;
      Write (Tok_Assign);
      Write_Space;
      if Is_Hard_Deadline (N) then
         Write (Tok_Hard_Global_Deadline);
      else
         Write_Str ("unknown");
      end if;
      Write (Tok_Colon);
      Write_Eol;

      Write_Indentation (-1);
      Write (Tok_Deadline);
      Write_Space;
      Write (Tok_Assign);
      Write_Space;
      Generate (Deadline (N));
      Write (Tok_Colon);
      Write_Eol;

      Write_Indentation (-1);
      Write (Tok_Referenced_Event);
      Write_Space;
      Write (Tok_Assign);
      Write_Space;
      Write_Name (Referenced_Event (N));
   end Generate_Event_Timing_Requirements;

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
   ----------------------------
   -- Generate_Event_Handler --
   ----------------------------

   procedure Generate_Event_Handler (N : Node_Id) is
   begin
      Write (Tok_Left_Paren);
      Write (Tok_Type);
      Write_Space;
      Write (Tok_Assign);
      Write_Space;
      if Is_Activity (N) then
         Write_Str ("activity");
      else
         Write_Str ("unknown");
      end if;
      Write_Line (Tok_Colon);

      Write_Indentation (-1);
      Write (Tok_Output_Event);
      Write_Space;
      Write (Tok_Assign);
      Write_Space;
      Write_Name (Output_Name (N));
      Write_Line (Tok_Colon);

      Write_Indentation (-1);
      Write (Tok_Activity_Operation);
      Write_Space;
      Write (Tok_Assign);
      Write_Space;
      Write_Name (Operation_Name (N));
      Write_Line (Tok_Colon);

      Write_Indentation (-1);
      Write (Tok_Activity_Server);
      Write_Space;
      Write (Tok_Assign);
      Write_Space;
      Write_Name (Server_Name (N));
      Write_Line (Tok_Colon);

      Write_Indentation (-1);
      Write (Tok_Input_Event);
      Write_Space;
      Write (Tok_Assign);
      Write_Space;
      Write_Name (Input_Name (N));
      Write (Tok_Right_Paren);
   end Generate_Event_Handler;

   ------------------------
   -- Generate_Operation --
   ------------------------

   procedure Generate_Operation (N : Node_Id) is
julien.delange's avatar
julien.delange committed
640
      Op : Node_Id;
641
642
643
644
645
646
647
648
649
650
651
652
653
   begin
      Write_Line ("Operation (");
      Increment_Indentation;

      Write_Indentation (-1);
      if Is_Simple (N) then
         Write_Line ("Type => Simple,");
      elsif Is_Composite (N) then
         Write_Line ("Type => Composite,");
      else
         Write_Line ("Type => Enclosing,");
      end if;

julien.delange's avatar
julien.delange committed
654
655
656
657
      Write_Indentation (-1);
      Write_Str ("Name => ");
      Write_Name (Node_Name (N));

658
      if Worst_Case_Execution_Time (N) /= No_Node then
659
660
         Write (Tok_Colon);
         Write_Eol;
661
662
663
664
665
666
667
668
669
         Write_Indentation (-1);
         Write (Tok_Worst_Case_Execution_Time);
         Write_Space;
         Write (Tok_Assign);
         Write_Space;
         Generate (Worst_Case_Execution_Time (N));
      end if;

      if Avg_Case_Execution_Time (N) /= No_Node then
670
671
         Write (Tok_Colon);
         Write_Eol;
672
673
674
675
676
677
678
679
680
         Write_Indentation (-1);
         Write (Tok_Avg_Case_Execution_Time);
         Write_Space;
         Write (Tok_Assign);
         Write_Space;
         Generate (Avg_Case_Execution_Time (N));
      end if;

      if Best_Case_Execution_Time (N) /= No_Node then
681
682
         Write (Tok_Colon);
         Write_Eol;
683
684
685
686
687
688
689
690
         Write_Indentation (-1);
         Write (Tok_Best_Case_Execution_Time);
         Write_Space;
         Write (Tok_Assign);
         Write_Space;
         Generate (Best_Case_Execution_Time (N));
      end if;

julien.delange's avatar
julien.delange committed
691
      if not Is_Empty (Operations (N)) then
julien.delange's avatar
julien.delange committed
692
693
         Write (Tok_Colon);
         Write_Eol;
julien.delange's avatar
julien.delange committed
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
         Write_Indentation (-1);
         Write (Tok_Composite_Operation_List);
         Write_Space;
         Write (Tok_Assign);
         Write_Space;
         Write (Tok_Left_Paren);

         Op := First_Node (Operations (N));
         while Present (Op) loop
            Generate (Op);
            if Next_Node (Op) /= No_Node then
               Write (Tok_Colon);
            end if;
            Op := Next_Node (Op);
         end loop;

         Write (Tok_Right_Paren);
      end if;
712
713
714
      Write_Line (");");
   end Generate_Operation;

715
end Ocarina.Backends.MAST_Tree.Generator;