ocarina-backends.adb 11.3 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                      --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--          Copyright (C) 2008-2011, European Space Agency (ESA).           --
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
--                                                                          --
-- 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.Table;

with Charset; use Charset;
with Namet;   use Namet;
with Output;  use Output;
with Errors;  use Errors;

with Ocarina.Backends.Build_Utils;
with Ocarina.Backends.Messages;
with Ocarina.Backends.PN;
45
46

with Ocarina.Backends.ARINC653_Conf;
47
with Ocarina.Backends.PO_HI_Ada;
48
with Ocarina.Backends.ASN1;
49
50
51
52
53
with Ocarina.Backends.PO_HI_C;
with Ocarina.Backends.PO_HI_RTSJ;
with Ocarina.Backends.Stats;
with Ocarina.Backends.Subprograms;
with Ocarina.Backends.Carts;
54
with Ocarina.Backends.Cheddar;
55
with Ocarina.Backends.Connection_Matrix;
56
with Ocarina.Backends.Functions_Matrix;
57
58
59
60
with Ocarina.Backends.MAST;
with Ocarina.Backends.MAST_Values;
with Ocarina.Backends.MAST_Tree.Nodes;
with Ocarina.Backends.MAST_Tree.Nutils;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
with Ocarina.Backends.POK_C;
with Ocarina.Backends.PO_QoS_Ada;
with Ocarina.Backends.Properties;
with Ocarina.Backends.Ada_Tree.Nodes;
with Ocarina.Backends.Ada_Tree.Nutils;
with Ocarina.Backends.Ada_Values;
with Ocarina.Backends.C_Tree.Nodes;
with Ocarina.Backends.C_Tree.Nutils;
with Ocarina.Backends.C_Values;
with Ocarina.Backends.RTSJ_Tree.Nodes;
with Ocarina.Backends.RTSJ_Tree.Nutils;
with Ocarina.Backends.RTSJ_Values;
with Ocarina.Backends.XML_Values;
with Ocarina.Backends.XML_Tree.Nutils;
with Ocarina.Backends.BoundT;
with Ocarina.Backends.REAL;
77
with Ocarina.Backends.Xtratum_Conf;
78
79
80
with Ocarina.Backends.ASN1_Tree.Nodes;
with Ocarina.Backends.ASN1_Tree.Nutils;
with Ocarina.Backends.ASN1_Values;
81
with Ocarina.Backends.AADL_XML;
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

with Ocarina.Options; use Ocarina.Options;

package body Ocarina.Backends is

   use Ocarina.Backends.Messages;

   Current_Backend_Name : Name_Id      := No_Name;
   Current_Backend_Kind : Backend_Kind := Invalid_Backend;

   type Backend_Record is record
      Name    : Name_Id;
      Process : Backend_Subprogram;
      Kind    : Backend_Kind;
   end record;

   package Backend_Table is
      new GNAT.Table (Backend_Record, Natural, 1, 10, 10);

   procedure Write_Backends (Indent : Natural);
   --  Displays the list of registered generators each one on a new
   --  line indented by 'Indent'.

   -------------------
   -- Generate_Code --
   -------------------

   procedure Generate_Code
     (Root         : Node_Id;
      Backend_Name : Name_Id  := No_Name)
   is
      Current_Backend : Natural := 0;

   begin

      --  FIXME: Select the code generator according to information
      --  given in the instance root system.

      if Backend_Name /= No_Name then
         for B in Backend_Table.First .. Backend_Table.Last loop
            if Backend_Table.Table (B).Name = Backend_Name then
               Current_Backend := B;
               Current_Backend_Kind := Backend_Table.Table (B).Kind;
               exit;
            end if;
         end loop;

      elsif Current_Backend_Name = No_Name then
         Display_Error ("No backend name specified", Fatal => True);

      else
         for B in Backend_Table.First .. Backend_Table.Last loop
            if Backend_Table.Table (B).Name = Current_Backend_Name then
               Current_Backend := B;
               Current_Backend_Kind := Backend_Table.Table (B).Kind;
               exit;
            end if;
         end loop;
      end if;

      if Current_Backend = 0 then
         Ocarina.Backends.Messages.Display_Error
           ("Cannot find backend " & Get_Name_String (Current_Backend_Name),
           Fatal => True);
      end if;

      --  Call the current generator entry point

      Backend_Table.Table (Current_Backend).Process (Root);

   exception
      when E : others =>
         Errors.Display_Bug_Box (E);
   end Generate_Code;

   ------------------------------
   -- Get_Current_Backend_Kind --
   ------------------------------

   function Get_Current_Backend_Kind return Backend_Kind is
   begin
      return Current_Backend_Kind;
   end Get_Current_Backend_Kind;

   ----------
   -- Init --
   ----------

   procedure Init is
   begin
      Backend_Table.Init;
      Properties.Init;
      Ada_Tree.Nutils.Initialize;
      C_Tree.Nutils.Initialize;
      RTSJ_Tree.Nutils.Initialize;
      XML_Tree.Nutils.Initialize;
178
      MAST_Tree.Nutils.Initialize;
179
      ASN1_Tree.Nutils.Initialize;
180
181
182

      --  Register the several code generators

183
      Ocarina.Backends.ARINC653_Conf.Init;
184
185
      PN.Init;
      BoundT.Init;
186
      MAST.Init;
187
188
189
190
191
      PO_HI_Ada.Init;
      PO_QoS_Ada.Init;
      PO_HI_C.Init;
      PO_HI_RTSJ.Init;
      POK_C.Init;
192
      Xtratum_Conf.Init;
193
194
195
196
      Stats.Init;
      Subprograms.Init;
      REAL.Init;
      Carts.Init;
197
      ASN1.Init;
198
      Cheddar.Init;
199
      Connection_Matrix.Init;
200
      Functions_Matrix.Init;
201
      AADL_XML.Init;
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
   end Init;

   ----------------------
   -- Register_Backend --
   ----------------------

   procedure Register_Backend
     (Name    : String;
      Process : Backend_Subprogram;
      Kind    : Backend_Kind)
   is
      N : Name_Id;

   begin
      --  If the installation directory is unknown, we do not register
      --  the backend to avoid any future error.

      if Installation_Directory = No_Name then
         return;
      end if;

      Get_Name_String (Installation_Directory);
      Add_Str_To_Name_Buffer ("include" & Directory_Separator);
      Add_Str_To_Name_Buffer ("ocarina" & Directory_Separator);
      Add_Str_To_Name_Buffer ("runtime" & Directory_Separator);

      --  If the runtime is not installed, we do not register the
      --  backend to avoid any future error.

      if not Is_Directory (Name_Buffer (1 .. Name_Len)) then
         return;
      end if;

      N := Get_String_Name (To_Lower (Name));
      for B in Backend_Table.First .. Backend_Table.Last loop
         if Backend_Table.Table (B).Name = N then
            Display_Error ("Cannot register twice backend " & Name,
                           Fatal => True);
         end if;
      end loop;

      Backend_Table.Increment_Last;
      Backend_Table.Table (Backend_Table.Last).Name    := N;
      Backend_Table.Table (Backend_Table.Last).Process := Process;
      Backend_Table.Table (Backend_Table.Last).Kind    := Kind;
   end Register_Backend;

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

   procedure Reset is
   begin
      PO_HI_Ada.Reset;
      PO_HI_C.Reset;
      PO_HI_RTSJ.Reset;
      POK_C.Reset;
259
      Xtratum_Conf.Reset;
260
261
262
      Stats.Reset;
      Carts.Reset;
      PO_QoS_Ada.Reset;
263
      Connection_Matrix.Reset;
264
      Functions_Matrix.Reset;
265
266
267
268

      Ada_Tree.Nutils.Reset;
      C_Tree.Nutils.Reset;
      RTSJ_Tree.Nutils.Reset;
269
      ASN1_Tree.Nutils.Reset;
270
      MAST_Tree.Nutils.Reset;
271
272
273
274
275
276
277
278
279

      Ada_Tree.Nodes.Entries.Free;
      Ada_Tree.Nodes.Entries.Init;
      Ada_Values.Reset;

      C_Tree.Nodes.Entries.Free;
      C_Tree.Nodes.Entries.Init;
      C_Values.Reset;

280
281
282
283
      MAST_Tree.Nodes.Entries.Free;
      MAST_Tree.Nodes.Entries.Init;
      MAST_Values.Reset;

284
285
286
287
288
289
290
291
      RTSJ_Tree.Nodes.Entries.Free;
      RTSJ_Tree.Nodes.Entries.Init;
      RTSJ_Values.Reset;

      XML_Values.Reset;
      BoundT.Reset;
      REAL.Reset;

292
293
294
295
      ASN1_Tree.Nodes.Entries.Free;
      ASN1_Tree.Nodes.Entries.Init;
      ASN1_Values.Reset;

296
297
298
299
      MAST_Tree.Nodes.Entries.Free;
      MAST_Tree.Nodes.Entries.Init;
      MAST_Values.Reset;

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
      Build_Utils.Reset;
   end Reset;

   ------------------------------
   -- Set_Current_Backend_Name --
   ------------------------------

   procedure Set_Current_Backend_Name (Name : String) is
   begin
      Current_Backend_Name := Get_String_Name (To_Lower (Name));
   end Set_Current_Backend_Name;

   ------------------------------
   -- Get_Current_Backend_Name --
   ------------------------------

   function Get_Current_Backend_Name return Name_Id is
   begin
      return Current_Backend_Name;
   end Get_Current_Backend_Name;

   -----------
   -- Usage --
   -----------

   procedure Usage is
   begin
      Write_Line ("   -g  Generate code from the AADL instance tree");
      Ocarina.Backends.Write_Backends (7);
329
      Write_Line ("   -perf  Enable profiling with gprof (PolyORB-HI-C only)");
julien.delange's avatar
julien.delange committed
330
331
      Write_Line
         ("   -asn1  Generate ASN1 deployment file (PolyORB-HI-C only)");
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
      Write_Line
         ("   -arinc653  Generate code for ARINC653 API (POK backend only)");
      Write_Line ("   -b  Generate and build code from the AADL model");
      Write_Line ("   -z  Clean code generated from the AADL model");
   end Usage;

   --------------------
   -- Write_Backends --
   --------------------

   procedure Write_Backends (Indent : Natural) is
   begin
      for Index in 1 .. Indent loop
         Write_Char (' ');
      end loop;

      Write_Line ("Registered backends:");

      for B in Backend_Table.First .. Backend_Table.Last loop
         if Backend_Table.Table (B).Name /= No_Name then
            for Index in 1 .. Indent + 1 loop
               Write_Char (' ');
            end loop;

            Write_Name (Backend_Table.Table (B).Name);
            Write_Eol;
         end if;
      end loop;
   end Write_Backends;

end Ocarina.Backends;