ocarina-backends-po_hi_ada-runtime.adb 12 KB
Newer Older
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--   O C A R I N A . B A C K E N D S . P O _ H I _ A D A . R U N T I M E    --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
--    Copyright (C) 2006-2009 Telecom ParisTech, 2010-2015 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/>.                                          --
26
--                                                                          --
jhugues's avatar
jhugues committed
27
28
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
29
30
31
32
33
34
35
--                                                                          --
------------------------------------------------------------------------------

with GNAT.OS_Lib; use GNAT.OS_Lib;
with GNAT.Case_Util;

with Charset; use Charset;
36
with Ocarina.Namet;   use Ocarina.Namet;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

with Ocarina.Backends.Ada_Tree.Nodes;
with Ocarina.Backends.Ada_Tree.Nutils;

package body Ocarina.Backends.PO_HI_Ada.Runtime is

   use Ocarina.Backends.Ada_Tree.Nodes;
   use Ocarina.Backends.Ada_Tree.Nutils;

   Initialized : Boolean := False;

   RUD : array (RU_Id) of Node_Id := (RU_Id'Range => No_Node);
   RED : array (RE_Id) of Node_Id := (RE_Id'Range => No_Node);
   --  Arrays of runtime entity and unit designators

   REI : array (RE_Id) of Name_Id := (RE_Id'Range => No_Name);
   --  Arrays of custom Images for runtime entity designators

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

61
   Rules      : array (1 .. 64) of Casing_Rule;
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
   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

   procedure Declare_Subunit (N : Node_Id);
   --  Declare the Unit corresponding to the node N as being nested

   function Get_Unit_Internal_Name (U_Name : Name_Id) return Name_Id;
   function Get_Unit_Position (U : Name_Id) return Int;
   procedure Set_Unit_Position (U : Name_Id; Pos : Int);
   --  The three routines below iensure the absence of conflict
   --  amongst different runtimes. The Get_Unit_Internal_Name does not
   --  affect globally the content of the name buffer.

   ----------------------------
   -- Get_Unit_Internal_Name --
   ----------------------------

   function Get_Unit_Internal_Name (U_Name : Name_Id) return Name_Id is
      Old_Name_Len    : constant Integer := Name_Len;
86
      Old_Name_Buffer : constant String  := Name_Buffer;
87
88
89
90
91
92
93
94
      Result          : Name_Id;
   begin
      Set_Str_To_Name_Buffer ("PO_HI_Ada%RU%");
      Get_Name_String_And_Append (U_Name);
      Result := Name_Find;

      --  Restore the name buffer

95
      Name_Len    := Old_Name_Len;
96
97
98
99
100
101
102
103
104
105
      Name_Buffer := Old_Name_Buffer;

      return Result;
   end Get_Unit_Internal_Name;

   -----------------------
   -- Get_Unit_Position --
   -----------------------

   function Get_Unit_Position (U : Name_Id) return Int is
106
      U_Name : constant Name_Id := Get_Unit_Internal_Name (U);
107
108
109
110
111
112
113
114
115
   begin
      return Get_Name_Table_Info (U_Name);
   end Get_Unit_Position;

   -----------------------
   -- Set_Unit_Position --
   -----------------------

   procedure Set_Unit_Position (U : Name_Id; Pos : Int) is
116
      U_Name : constant Name_Id := Get_Unit_Internal_Name (U);
117
118
119
120
121
122
123
124
125
   begin
      Set_Name_Table_Info (U_Name, Pos);
   end Set_Unit_Position;

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

   procedure Apply_Casing_Rules (S : in out String) is
126
127
      New_Word : Boolean         := True;
      Length   : Natural         := S'Length;
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
      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;

   ---------------------
   -- Declare_Subunit --
   ---------------------

   procedure Declare_Subunit (N : Node_Id) is
      S : Node_Id;

   begin
      pragma Assert (Kind (N) = K_Designator);
      S := Corresponding_Node (Defining_Identifier (N));
      pragma Assert (Kind (S) = K_Package_Specification);
      Set_Is_Subunit_Package (S, True);
   end Declare_Subunit;

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

   procedure Initialize is
      Position   : Integer;
      Name       : Name_Id;
      Identifier : Node_Id;
      Length     : Natural;
      Pkg_Spec   : Node_Id;

   begin
      --  Initialize the runtime only once

      if Initialized then
         return;
      end if;

      Initialized := True;

      --  Register the custom casing rules

      Register_Casing_Rule ("AADL");
      Register_Casing_Rule ("ASN1");
      Register_Casing_Rule ("PolyORB_HI");
      Register_Casing_Rule ("GNAT");
      Register_Casing_Rule ("VM");
      Register_Casing_Rule ("char_array");
      Register_Casing_Rule ("nul");

      --  Register the custom runtime entity images

203
204
205
206
207
      REI (RE_Integer_8)   := Get_String_Name ("Integer_8");
      REI (RE_Integer_16)  := Get_String_Name ("Integer_16");
      REI (RE_Integer_32)  := Get_String_Name ("Integer_32");
      REI (RE_Integer_64)  := Get_String_Name ("Integer_64");
      REI (RE_Unsigned_8)  := Get_String_Name ("Unsigned_8");
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
      REI (RE_Unsigned_16) := Get_String_Name ("Unsigned_16");
      REI (RE_Unsigned_32) := Get_String_Name ("Unsigned_32");
      REI (RE_Unsigned_64) := Get_String_Name ("Unsigned_64");
      --  We do this because suffixes of the form _X where X is an
      --  integer are cut by default.

      for U in RU_Id'Succ (RU_Id'First) .. RU_Id'Last loop
         Set_Str_To_Name_Buffer (RU_Id'Image (U));
         Set_Str_To_Name_Buffer (Name_Buffer (4 .. Name_Len));

         RUD (U) := New_Node (K_Designator);

         Position := 0;
         Name     := Name_Find;
         Length   := Name_Len;
         Set_Unit_Position (Name, RU_Id'Pos (U));

         while Name_Len > 0 loop
            if Name_Buffer (Name_Len) = '_' then
               Name_Len := Name_Len - 1;
               Position := Integer (Get_Unit_Position (Name_Find));
               exit when Position > 0;

            else
               Name_Len := Name_Len - 1;
            end if;
         end loop;

         --  When there is a parent, remove parent unit name from
         --  unit name to get real identifier.

         if Position > 0 then
240
            Set_Str_To_Name_Buffer (Name_Buffer (Name_Len + 2 .. Length));
241
242
            Name := Name_Find;
            Set_Homogeneous_Parent_Unit_Name
243
244
              (RUD (U),
               RUD (RU_Id'Val (Position)));
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
         end if;

         Get_Name_String (Name);
         Apply_Casing_Rules (Name_Buffer (1 .. Name_Len));
         Identifier := Make_Defining_Identifier (Name_Find);
         Set_Defining_Identifier (RUD (U), Identifier);
         Pkg_Spec := New_Node (K_Package_Specification);
         Set_Is_Runtime_Package (Pkg_Spec, True);
         Set_Corresponding_Node (Identifier, Pkg_Spec);

         if Position > 0 then
            Set_Homogeneous_Parent_Unit_Name
              (Identifier,
               Defining_Identifier (Parent_Unit_Name (RUD (U))));
         end if;

      end loop;

      --  IMPORTANT : Declare here the subunits. Example
      --    Declare_Subunit (RUD (RU_Subunit_Not_To_Be_Withed);
      Declare_Subunit
        (RUD (RU_Ada_Strings_Wide_Bounded_Generic_Bounded_Length));
      Declare_Subunit (RUD (RU_Ada_Strings_Bounded_Generic_Bounded_Length));
      Declare_Subunit (RUD (RU_PolyORB_HI_Hybrid_Task_Driver_Driver));
yoogx's avatar
yoogx committed
269
      Declare_Subunit (RUD (RU_Ada_Interrupts_Names));
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

      --  Package Standard is not a subunit but it has to be handled
      --  in a specific way as well as subunit.

      Declare_Subunit (RUD (RU_Standard));

      for E in RE_Id loop
         if REI (E) = No_Name then
            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) in '0' .. '9'
              or else Name_Buffer (Name_Len) = '_'
            loop
               Name_Len := Name_Len - 1;
            end loop;

            Name := Name_Find;
         else
            Name := REI (E);
         end if;

         RED (E) := New_Node (K_Designator);
294
         Set_Defining_Identifier (RED (E), Make_Defining_Identifier (Name));
295
296
297
298
299
300
301
302
303
304
         Set_Homogeneous_Parent_Unit_Name (RED (E), RUD (RE_Unit_Table (E)));
      end loop;
   end Initialize;

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

   procedure Reset is
   begin
305
306
      RUD        := (RU_Id'Range => No_Node);
      RED        := (RE_Id'Range => No_Node);
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
      Rules_Last := 0;

      Initialized := False;
   end Reset;

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

   function RE (Id : RE_Id; Withed : Boolean := True) return Node_Id is
   begin
      return Copy_Designator (RED (Id), Withed);
   end RE;

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

   procedure Register_Casing_Rule (S : String) is
   begin
327
      Rules_Last              := Rules_Last + 1;
328
329
330
331
332
333
334
335
336
337
      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;

   --------
   -- RU --
   --------

338
339
340
341
   function RU
     (Id : RU_Id; Withed : Boolean := True; Elaborated : Boolean := False)
     return Node_Id
   is
342
343
344
345
346
347
348
      Result : Node_Id;
   begin
      --  This is a runtime unit and not a runtime entity, so it's
      --  parent unit does not have to be "withed"

      Result := Copy_Designator (RUD (Id), False);
      if Withed then
349
         Add_With_Package (Result, Elaborated => Elaborated);
350
351
352
353
354
      end if;
      return Result;
   end RU;

end Ocarina.Backends.PO_HI_Ada.Runtime;