ocarina-backends-pok_c-runtime.adb 18.3 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
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
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--       O C A R I N A . B A C K E N D S . P O K _ C . R U N T I M E        --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--               Copyright (C) 2008-2009, GET-Telecom Paris.                --
--                                                                          --
-- 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 GNAT.OS_Lib; use GNAT.OS_Lib;
with GNAT.Case_Util;

with Utils; use Utils;

with Charset; use Charset;
with Namet;   use Namet;

with Ocarina.Backends.C_Tree.Nodes;
with Ocarina.Backends.C_Tree.Nutils;

package body Ocarina.Backends.POK_C.Runtime is

   use Ocarina.Backends.C_Tree.Nodes;
   use Ocarina.Backends.C_Tree.Nutils;

   Initialized : Boolean := False;

   Current_Kernel_Unit : Node_Id;

   RED : array (RE_Id) of Node_Id := (RE_Id'Range => No_Node);
   RHD : array (RH_Id) of Node_Id := (RH_Id'Range => No_Node);

   --  Arrays of run-time entity and unit designators

   type Casing_Rule is record
      Size : Natural;
      From : String_Access;
      Into : String_Access;
   end record;

   Rules : array (1 .. 64) of Casing_Rule;
   Rules_Last : Natural := 0;

   procedure Apply_Casing_Rules (S : in out String);
   --  Apply the registered casing rules on the string S

   procedure Register_Casing_Rule (S : String);
   --  Register a custom casing rule

   ------------------------
   -- Apply_Casing_Rules --
   ------------------------

   procedure Apply_Casing_Rules (S : in out String) is
      New_Word : Boolean := True;
      Length   : Natural := S'Length;
      S1       : constant String := To_Lower (S);
   begin
      GNAT.Case_Util.To_Mixed (S);

      for I in S'Range loop
         if New_Word then
            New_Word := False;
            for J in 1 .. Rules_Last loop
               if Rules (J).Size <= Length
                 and then S1 (I .. I + Rules (J).Size - 1) = Rules (J).From.all
               then
                  S (I .. I + Rules (J).Size - 1) := Rules (J).Into.all;
               end if;
            end loop;
         end if;
         if S (I) = '_' then
            New_Word := True;
            for J in 1 .. Rules_Last loop
               if Rules (J).Size <= Length
                 and then S1 (I .. I + Rules (J).Size - 1) = Rules (J).From.all
               then
                  S (I .. I + Rules (J).Size - 1) := Rules (J).Into.all;
               end if;
            end loop;
         end if;
         Length := Length - 1;
      end loop;
   end Apply_Casing_Rules;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize is
      Name       : Name_Id;
      N          : Node_Id;
      Is_Local   : Boolean;
   begin
      --  Initialize the runtime only once

      if Initialized then
         return;
      end if;

      Initialized := True;

      if POK_Flavor = ARINC653 then
         RH_Service_Table (RH_Assert)     := RHS_Null;
         RH_Service_Table (RH_Thread)     := RHS_Core;
         RH_Service_Table (RH_Error)      := RHS_ARINC653;
         RH_Service_Table (RH_Blackboard) := RHS_ARINC653;
         RH_Service_Table (RH_Buffer)     := RHS_ARINC653;
         RH_Service_Table (RH_Queueing)   := RHS_ARINC653;
         RH_Service_Table (RH_Semaphore)  := RHS_ARINC653;
         RH_Service_Table (RH_Event)      := RHS_ARINC653;
         RH_Service_Table (RH_Sampling)   := RHS_ARINC653;
         RH_Service_Table (RH_Types)      := RHS_ARINC653;
         RH_Service_Table (RH_Time)       := RHS_ARINC653;
         RH_Service_Table (RH_Partition)  := RHS_ARINC653;
      end if;

      --  When we use the ARINC653 API, we change the include/
      --  directory for headers files. So, we change the following
      --  table that indicates the directory of each include file.

      Register_Casing_Rule ("AADL");
      Register_Casing_Rule ("char_array");
      Register_Casing_Rule ("nul");

      --  Apply casing rule for POK functions.
      --  All types are in lower case.

      for E in PRF_Id loop
         Set_Str_To_Name_Buffer (RE_Id'Image (E));
         Set_Str_To_Name_Buffer (Name_Buffer (4 .. Name_Len));

         Apply_Casing_Rules (Name_Buffer (1 .. Name_Len));

         while Name_Buffer (Name_Len) = '_'
         loop
            Name_Len := Name_Len - 1;
         end loop;

         Name := Name_Find;

         Name := Utils.To_Lower (Name);
         RED (E) := New_Node (K_Defining_Identifier);
         Set_Name
           (RED (E), Name);
      end loop;

      --  Apply casing rule for ARINC653 functions.
      --  All functions are in upper case.

      for E in ARF_Id loop
         Set_Str_To_Name_Buffer (RE_Id'Image (E));
         Set_Str_To_Name_Buffer (Name_Buffer (4 .. Name_Len));

         Apply_Casing_Rules (Name_Buffer (1 .. Name_Len));

         while Name_Buffer (Name_Len) = '_'
         loop
            Name_Len := Name_Len - 1;
         end loop;

         Name := Name_Find;

         Name := Utils.To_Upper (Name);
         RED (E) := New_Node (K_Defining_Identifier);
         Set_Name
           (RED (E), Name);
      end loop;

      --  Apply casing rule for POK types.
      --  All types are in lower case.

      for E in PRT_Id loop
         Set_Str_To_Name_Buffer (RE_Id'Image (E));
         Set_Str_To_Name_Buffer (Name_Buffer (4 .. Name_Len));

         Apply_Casing_Rules (Name_Buffer (1 .. Name_Len));

         while Name_Buffer (Name_Len) = '_'
         loop
            Name_Len := Name_Len - 1;
         end loop;

         Name := Name_Find;

         Name := Utils.To_Lower (Name);
         RED (E) := New_Node (K_Defining_Identifier);
         Set_Name
           (RED (E), Name);
      end loop;

      --  Apply casing rule for ARINC653 types.
      --  All types are in upper case.

      for E in ART_Id loop
         Set_Str_To_Name_Buffer (RE_Id'Image (E));
         Set_Str_To_Name_Buffer (Name_Buffer (4 .. Name_Len));

         Apply_Casing_Rules (Name_Buffer (1 .. Name_Len));

         while Name_Buffer (Name_Len) = '_'
         loop
            Name_Len := Name_Len - 1;
         end loop;

         Name := Name_Find;

         Name := Utils.To_Upper (Name);
         RED (E) := New_Node (K_Defining_Identifier);
         Set_Name
           (RED (E), Name);
      end loop;

      --  Apply casing rule for headers, there is no difference
      --  between POK and ARINC653 headers casing rules, so, we
      --  use the same.

      for E in RH_Id loop
         Set_Str_To_Name_Buffer (RH_Id'Image (E));
         Set_Str_To_Name_Buffer (Name_Buffer (4 .. Name_Len));
         Apply_Casing_Rules (Name_Buffer (1 .. Name_Len));

         while Name_Buffer (Name_Len) = '_'
         loop
            Name_Len := Name_Len - 1;
         end loop;

         if RH_Service_Table (E) = RHS_Generated then
            Is_Local := True;
         elsif RH_Service_Table (E) /= RHS_Null then
            Is_Local := False;
            Name := Name_Find;
            Set_Str_To_Name_Buffer (RHS_Id'Image (RH_Service_Table (E)));
            Set_Str_To_Name_Buffer (Name_Buffer (5 .. Name_Len));
            Add_Str_To_Name_Buffer ("/");
            Apply_Casing_Rules (Name_Buffer (1 .. Name_Len));
            Get_Name_String_And_Append (Name);
         else
            Is_Local := False;
         end if;

         Name := Name_Find;

         Name := Utils.To_Lower (Name);

         N := New_Node (K_Defining_Identifier);
         Set_Name (N, Name);
         RHD (E) := Make_Include_Clause (N, Is_Local);
      end loop;

      --  Apply casing rule for constants, there is no difference
      --  between POK and ARINC653 constants casing rules, so, we
      --  use the same.

      for E in RC_Id loop
         Set_Str_To_Name_Buffer (RC_Id'Image (E));
         Set_Str_To_Name_Buffer (Name_Buffer (4 .. Name_Len));
         Apply_Casing_Rules (Name_Buffer (1 .. Name_Len));

         while Name_Buffer (Name_Len) in '0' .. '9'
           or else Name_Buffer (Name_Len) = '_'
         loop
            Name_Len := Name_Len - 1;
         end loop;

         Name := Name_Find;

         Name := To_Upper (Name);
         RED (E) := New_Node (K_Defining_Identifier);
         Set_Name
           (RED (E), Name);
      end loop;

      --  Apply casing rule for variables, there is no difference
      --  between POK and ARINC653 variables casing rules, so, we
      --  use the same.

      for E in RV_Id loop
         Set_Str_To_Name_Buffer (RV_Id'Image (E));
         Set_Str_To_Name_Buffer (Name_Buffer (4 .. Name_Len));
         Apply_Casing_Rules (Name_Buffer (1 .. Name_Len));

         while Name_Buffer (Name_Len) in '0' .. '9'
           or else Name_Buffer (Name_Len) = '_'
         loop
            Name_Len := Name_Len - 1;
         end loop;

         Name := Name_Find;

         Name := To_Lower (Name);
         RED (E) := New_Node (K_Defining_Identifier);
         Set_Name
           (RED (E), Name);
      end loop;

      --  Apply casing rule for members. The difference
      --  between POK and ARINC653 is made inside the loop.

      for E in MR_Id loop
         Set_Str_To_Name_Buffer (RC_Id'Image (E));
         Set_Str_To_Name_Buffer (Name_Buffer (4 .. Name_Len));
         Apply_Casing_Rules (Name_Buffer (1 .. Name_Len));

         while Name_Buffer (Name_Len) in '0' .. '9'
           or else Name_Buffer (Name_Len) = '_'
         loop
            Name_Len := Name_Len - 1;
         end loop;

         Name := Name_Find;

         --  If we use the ARINC653 backend flavor, the
         --  members are in upper case. Otherwise, we use
         --  lower case.

         if POK_Flavor = ARINC653 then
            Name := To_Upper (Name);
         else
            Name := To_Lower (Name);
         end if;
         RED (E) := New_Node (K_Defining_Identifier);
         Set_Name
           (RED (E), Name);
      end loop;

   end Initialize;

   -----------
   -- Reset --
   -----------

   procedure Reset is
   begin
      RED := (RE_Id'Range => No_Node);
      RHD := (RH_Id'Range => No_Node);
      Rules_Last := 0;

      Initialized := False;
   end Reset;

   --------
   -- RE --
   --------

   function RE (Id : RE_Id) return Node_Id is
   begin
      if RE_Header_Table (Id) /= RH_Null then
         Add_Include (RH (RE_Header_Table (Id)));
      end if;

      return Copy_Node (RED (Id));
   end RE;

   --------
   -- RF --
   --------

   function RF (Id : RF_Id) return Node_Id is
      N : Node_Id;
      R : RE_Id;
   begin
      N := RE (Id);
      R := RF_Define_Table (Id);
      if R /= RE_Null then
         Add_Define_Deployment (RE (R));
      end if;

      --  Add functionnality in the kernel
      --  according to model needs.
      --  WiP functionnality at this time, needs to describe
      --  each function needs.
      if Id = RE_Pok_Blackboard_Read then
         Push_Entity (Entity (Current_Kernel_Unit));
         Push_Entity (Current_Kernel_Unit);

         Add_Define_Deployment (RE (RE_Pok_Needs_Gettick));

         Pop_Entity;
         Pop_Entity;
      end if;

      return N;
   end RF;

   --------
   -- RH --
   --------

   function RH (Id : RH_Id) return Node_Id is
   begin
      return Copy_Node (RHD (Id));
   end RH;

   --------------------------
   -- Register_Casing_Rule --
   --------------------------

   procedure Register_Casing_Rule (S : String) is
   begin
      Rules_Last := Rules_Last + 1;
      Rules (Rules_Last).Size := S'Length;
      Rules (Rules_Last).Into := new String'(S);
      Rules (Rules_Last).From := new String'(S);
      To_Lower (Rules (Rules_Last).From.all);
   end Register_Casing_Rule;

   --------------------------
   -- Update_Headers_Names --
   --------------------------

   procedure Update_Headers_Names is
      Name     : Name_Id;
   begin
      for E in RH_Id loop
         Set_Str_To_Name_Buffer (RH_Id'Image (E));
         Set_Str_To_Name_Buffer (Name_Buffer (4 .. Name_Len));
         Apply_Casing_Rules (Name_Buffer (1 .. Name_Len));

         while Name_Buffer (Name_Len) = '_'
         loop
            Name_Len := Name_Len - 1;
         end loop;

         if RH_Service_Table (E) /= RHS_Generated
            and then RH_Service_Table (E) /= RHS_Null then
            Name := Name_Find;
            Set_Str_To_Name_Buffer (RHS_Id'Image (RH_Service_Table (E)));
            Set_Str_To_Name_Buffer (Name_Buffer (5 .. Name_Len));
            Add_Str_To_Name_Buffer ("/");
            Apply_Casing_Rules (Name_Buffer (1 .. Name_Len));
            Get_Name_String_And_Append (Name);
         end if;

         Name := Name_Find;

         Name := Utils.To_Lower (Name);

         Set_Name (Header_Name (RHD (E)), Name);
      end loop;
   end Update_Headers_Names;

   -----------------
   -- Normal_Mode --
   -----------------

   procedure Normal_Mode is
   begin
      if POK_Flavor = ARINC653 then
         ARINC653_Mode;
      else
         POK_Mode;
      end if;
   end Normal_Mode;

   --------------
   -- POK_Mode --
   --------------

   procedure POK_Mode is
   begin
      RH_Service_Table (RH_Assert)     := RHS_Null;
      RH_Service_Table (RH_Thread)     := RHS_Core;
      RH_Service_Table (RH_Blackboard) := RHS_Middleware;
      RH_Service_Table (RH_Buffer)     := RHS_Middleware;
      RH_Service_Table (RH_Queueing)   := RHS_Middleware;
      RH_Service_Table (RH_Semaphore)  := RHS_Core;
      RH_Service_Table (RH_Event)      := RHS_Core;
      RH_Service_Table (RH_Sampling)   := RHS_Middleware;
      RH_Service_Table (RH_Types)      := RHS_Null;
      RH_Service_Table (RH_Time)       := RHS_Core;
      RH_Service_Table (RH_Partition)  := RHS_Core;

      Update_Headers_Names;
   end POK_Mode;

   -------------------
   -- ARINC653_Mode --
   -------------------

   procedure ARINC653_Mode is
   begin
      RH_Service_Table (RH_Assert)     := RHS_Null;
      RH_Service_Table (RH_Thread)     := RHS_Core;
      RH_Service_Table (RH_Blackboard) := RHS_ARINC653;
      RH_Service_Table (RH_Buffer)     := RHS_ARINC653;
      RH_Service_Table (RH_Queueing)   := RHS_ARINC653;
      RH_Service_Table (RH_Semaphore)  := RHS_ARINC653;
      RH_Service_Table (RH_Event)      := RHS_ARINC653;
      RH_Service_Table (RH_Sampling)   := RHS_ARINC653;
      RH_Service_Table (RH_Types)      := RHS_ARINC653;
      RH_Service_Table (RH_Time)       := RHS_ARINC653;
      RH_Service_Table (RH_Partition)  := RHS_ARINC653;

      Update_Headers_Names;
   end ARINC653_Mode;

   -----------------
   -- Kernel_Mode --
   -----------------

   procedure Kernel_Mode is
   begin
      --  Same as User_Mode but for the kernel
      RH_Service_Table (RH_Types)      := RHS_Null;
      RH_Service_Table (RH_Partition)  := RHS_Core;
      RH_Service_Table (RH_Error)      := RHS_Core;

      Update_Headers_Names;
   end Kernel_Mode;

   ---------------
   -- User_Mode --
   ---------------

   procedure User_Mode is
   begin
      --  Switch to user mode, change header name
      --  locations, change their containing directories.
      if POK_Flavor = ARINC653 then
         RH_Service_Table (RH_Assert)     := RHS_Null;
         RH_Service_Table (RH_Thread)     := RHS_Core;
         RH_Service_Table (RH_Blackboard) := RHS_ARINC653;
         RH_Service_Table (RH_Error)      := RHS_ARINC653;
         RH_Service_Table (RH_Buffer)     := RHS_ARINC653;
         RH_Service_Table (RH_Queueing)   := RHS_ARINC653;
         RH_Service_Table (RH_Sampling)   := RHS_ARINC653;
         RH_Service_Table (RH_Types)      := RHS_ARINC653;
         RH_Service_Table (RH_Time)       := RHS_ARINC653;
         RH_Service_Table (RH_Partition)  := RHS_ARINC653;
      else
         RH_Service_Table (RH_Assert)     := RHS_Null;
         RH_Service_Table (RH_Thread)     := RHS_Core;
         RH_Service_Table (RH_Blackboard) := RHS_Middleware;
         RH_Service_Table (RH_Buffer)     := RHS_Middleware;
         RH_Service_Table (RH_Queueing)   := RHS_Middleware;
         RH_Service_Table (RH_Sampling)   := RHS_Middleware;
         RH_Service_Table (RH_Types)      := RHS_Null;
         RH_Service_Table (RH_Time)       := RHS_Core;
         RH_Service_Table (RH_Partition)  := RHS_Core;
      end if;

      Update_Headers_Names;
   end User_Mode;

   procedure Register_Kernel_Unit (Unit : Node_Id)
   is
   begin
      Current_Kernel_Unit := Unit;
   end Register_Kernel_Unit;

   function Get_Errcode_OK return Node_Id is
   begin
      if POK_Flavor = ARINC653 then
         return RE (RE_No_Error);
      else
         return RE (RE_Pok_Errno_Ok);
      end if;
   end Get_Errcode_OK;

end Ocarina.Backends.POK_C.Runtime;