ocarina-backends.adb 10.4 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                                  --
--                                                                          --
yoogx's avatar
yoogx committed
9
--    Copyright (C) 2008-2009 Telecom ParisTech, 2010-2016 ESA & ISAE.      --
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
--                                                                          --
-- 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.Table;

with Charset; use Charset;
36
37
with Ocarina.Namet;   use Ocarina.Namet;
with Ocarina.Output;  use Ocarina.Output;
38
39
40
41
42
with Errors;  use Errors;

with Ocarina.Backends.Build_Utils;
with Ocarina.Backends.Messages;
with Ocarina.Backends.PN;
43
44

with Ocarina.Backends.ARINC653_Conf;
Julien's avatar
Julien committed
45
with Ocarina.Backends.Deos_Conf;
46
with Ocarina.Backends.Vxworks653_Conf;
47
with Ocarina.Backends.PO_HI_Ada;
48
with Ocarina.Backends.ASN1;
49
50
51
with Ocarina.Backends.PO_HI_C;
with Ocarina.Backends.Stats;
with Ocarina.Backends.Subprograms;
52
with Ocarina.Backends.Cheddar;
53
with Ocarina.Backends.Connection_Matrix;
54
with Ocarina.Backends.Functions_Matrix;
55
56
57
58
with Ocarina.Backends.MAST;
with Ocarina.Backends.MAST_Values;
with Ocarina.Backends.MAST_Tree.Nodes;
with Ocarina.Backends.MAST_Tree.Nutils;
59
60
61
62
63
64
65
66
67
68
69
70
with Ocarina.Backends.POK_C;
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.XML_Values;
with Ocarina.Backends.XML_Tree.Nutils;
with Ocarina.Backends.BoundT;
with Ocarina.Backends.REAL;
71
with Ocarina.Backends.Xtratum_Conf;
72
73
74
with Ocarina.Backends.ASN1_Tree.Nodes;
with Ocarina.Backends.ASN1_Tree.Nutils;
with Ocarina.Backends.ASN1_Values;
75
with Ocarina.Backends.AADL_XML;
yoogx's avatar
yoogx committed
76
with Ocarina.Backends.Alloy;
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

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;

93
94
95
96
97
98
   package Backend_Table is new GNAT.Table
     (Backend_Record,
      Natural,
      1,
      10,
      10);
99
100
101
102
103
104
105

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

   procedure Generate_Code
     (Root         : Node_Id;
106
      Backend_Name : Name_Id := No_Name)
107
108
109
110
111
112
113
114
115
116
117
   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
118
               Current_Backend      := B;
119
120
121
122
123
124
125
126
127
128
129
               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
130
               Current_Backend      := B;
131
132
133
134
135
136
137
138
139
               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),
140
            Fatal => True);
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
      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;
      XML_Tree.Nutils.Initialize;
172
      MAST_Tree.Nutils.Initialize;
173
      ASN1_Tree.Nutils.Initialize;
174

175
176
177
178
179
180
181
      if Generated_Sources_Directory = No_Name then
         Generated_Sources_Directory := Get_String_Name (".");
      end if;

      Compile_Generated_Sources := Compile_Generated_Sources or else
        Do_Regression_Test or else Do_Coverage_Test;

yoogx's avatar
yoogx committed
182
      --  Register the code generators
183

184
      Ocarina.Backends.ARINC653_Conf.Init;
185
      Ocarina.Backends.Vxworks653_Conf.Init;
Julien's avatar
Julien committed
186
      Ocarina.Backends.Deos_Conf.Init;
187
188
      PN.Init;
      BoundT.Init;
189
      MAST.Init;
190
191
192
      PO_HI_Ada.Init;
      PO_HI_C.Init;
      POK_C.Init;
193
      Xtratum_Conf.Init;
194
195
196
      Stats.Init;
      Subprograms.Init;
      REAL.Init;
197
      ASN1.Init;
198
      Cheddar.Init;
199
      Connection_Matrix.Init;
200
      Functions_Matrix.Init;
201
      AADL_XML.Init;
yoogx's avatar
yoogx committed
202
      Alloy.Init;
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
   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
239
240
241
            Display_Error
              ("Cannot register twice backend " & Name,
               Fatal => True);
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
         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;
      POK_C.Reset;
260
      Xtratum_Conf.Reset;
261
      Stats.Reset;
262
      Connection_Matrix.Reset;
263
      Functions_Matrix.Reset;
264
265
266

      Ada_Tree.Nutils.Reset;
      C_Tree.Nutils.Reset;
267
      ASN1_Tree.Nutils.Reset;
268
      MAST_Tree.Nutils.Reset;
269
270
271
272
273
274
275
276
277

      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;

278
279
280
281
      MAST_Tree.Nodes.Entries.Free;
      MAST_Tree.Nodes.Entries.Init;
      MAST_Values.Reset;

282
283
284
285
      XML_Values.Reset;
      BoundT.Reset;
      REAL.Reset;

286
287
288
289
      ASN1_Tree.Nodes.Entries.Free;
      ASN1_Tree.Nodes.Entries.Init;
      ASN1_Values.Reset;

290
291
292
293
      MAST_Tree.Nodes.Entries.Free;
      MAST_Tree.Nodes.Entries.Init;
      MAST_Values.Reset;

yoogx's avatar
yoogx committed
294
295
      Alloy.Reset;

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

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