OSDN Git Service

2007-09-26 Thomas Quinot <quinot@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / back_end.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                            B A C K _ E N D                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Atree;     use Atree;
27 with Debug;     use Debug;
28 with Elists;    use Elists;
29 with Lib;       use Lib;
30 with Osint;     use Osint;
31 with Opt;       use Opt;
32 with Osint.C;   use Osint.C;
33 with Namet;     use Namet;
34 with Nlists;    use Nlists;
35 with Stand;     use Stand;
36 with Sinput;    use Sinput;
37 with Stringt;   use Stringt;
38 with Switch;    use Switch;
39 with Switch.C;  use Switch.C;
40 with System;    use System;
41 with Types;     use Types;
42
43 package body Back_End is
44
45    -------------------
46    -- Call_Back_End --
47    -------------------
48
49    procedure Call_Back_End (Mode : Back_End_Mode_Type) is
50
51       --  The File_Record type has a lot of components that are meaningless
52       --  to the back end, so a new record is created here to contain the
53       --  needed information for each file.
54
55       type Needed_File_Info_Type is record
56          File_Name        : File_Name_Type;
57          First_Sloc       : Source_Ptr;
58          Last_Sloc        : Source_Ptr;
59          Num_Source_Lines : Nat;
60       end record;
61
62       File_Info_Array :
63         array (Main_Unit .. Last_Unit) of Needed_File_Info_Type;
64
65       procedure gigi (
66          gnat_root                     : Int;
67          max_gnat_node                 : Int;
68          number_name                   : Nat;
69          nodes_ptr                     : Address;
70
71          next_node_ptr                 : Address;
72          prev_node_ptr                 : Address;
73          elists_ptr                    : Address;
74          elmts_ptr                     : Address;
75
76          strings_ptr                   : Address;
77          string_chars_ptr              : Address;
78          list_headers_ptr              : Address;
79          number_units                  : Int;
80
81          file_info_ptr                 : Address;
82          gigi_standard_integer         : Entity_Id;
83          gigi_standard_long_long_float : Entity_Id;
84          gigi_standard_exception_type  : Entity_Id;
85          gigi_operating_mode           : Back_End_Mode_Type);
86
87       pragma Import (C, gigi);
88
89       S : Source_File_Index;
90
91    begin
92       --  Skip call if in -gnatdH mode
93
94       if Debug_Flag_HH then
95          return;
96       end if;
97
98       for J in Main_Unit .. Last_Unit loop
99          S := Source_Index (J);
100          File_Info_Array (J).File_Name        := File_Name (S);
101          File_Info_Array (J).First_Sloc       := Source_Text (S)'First;
102          File_Info_Array (J).Last_Sloc        := Source_Text (S)'Last;
103          File_Info_Array (J).Num_Source_Lines := Num_Source_Lines (S);
104       end loop;
105
106       gigi (
107          gnat_root          => Int (Cunit (Main_Unit)),
108          max_gnat_node      => Int (Last_Node_Id - First_Node_Id + 1),
109          number_name        => Name_Entries_Count,
110          nodes_ptr          => Nodes_Address,
111
112          next_node_ptr      => Next_Node_Address,
113          prev_node_ptr      => Prev_Node_Address,
114          elists_ptr         => Elists_Address,
115          elmts_ptr          => Elmts_Address,
116
117          strings_ptr        => Strings_Address,
118          string_chars_ptr   => String_Chars_Address,
119          list_headers_ptr   => Lists_Address,
120          number_units       => Num_Units,
121
122          file_info_ptr                 => File_Info_Array'Address,
123          gigi_standard_integer         => Standard_Integer,
124          gigi_standard_long_long_float => Standard_Long_Long_Float,
125          gigi_standard_exception_type  => Standard_Exception_Type,
126          gigi_operating_mode           => Mode);
127    end Call_Back_End;
128
129    -----------------------------
130    -- Scan_Compiler_Arguments --
131    -----------------------------
132
133    procedure Scan_Compiler_Arguments is
134
135       Next_Arg : Pos := 1;
136
137       subtype Big_String is String (Positive);
138       type BSP is access Big_String;
139
140       type Arg_Array is array (Nat) of BSP;
141       type Arg_Array_Ptr is access Arg_Array;
142
143       --  Import flag_stack_check from toplev.c
144
145       flag_stack_check : Int;
146       pragma Import (C, flag_stack_check); -- Import from toplev.c
147
148       save_argc : Nat;
149       pragma Import (C, save_argc); -- Import from toplev.c
150
151       save_argv : Arg_Array_Ptr;
152       pragma Import (C, save_argv); -- Import from toplev.c
153
154       Output_File_Name_Seen : Boolean := False;
155       --  Set to True after having scanned the file_name for
156       --  switch "-gnatO file_name"
157
158       --  Local functions
159
160       function Len_Arg (Arg : Pos) return Nat;
161       --  Determine length of argument number Arg on the original
162       --  command line from gnat1
163
164       procedure Scan_Back_End_Switches (Switch_Chars : String);
165       --  Procedure to scan out switches stored in Switch_Chars. The first
166       --  character is known to be a valid switch character, and there are no
167       --  blanks or other switch terminator characters in the string, so the
168       --  entire string should consist of valid switch characters, except that
169       --  an optional terminating NUL character is allowed.
170       --
171       --  Back end switches have already been checked and processed by GCC
172       --  in toplev.c, so no errors can occur and control will always return.
173       --  The switches must still be scanned to skip the arguments of the
174       --  "-o" or the (undocumented) "-dumpbase" switch, by incrementing
175       --  the Next_Arg variable. The "-dumpbase" switch is used to set the
176       --  basename for GCC dumpfiles.
177
178       -------------
179       -- Len_Arg --
180       -------------
181
182       function Len_Arg (Arg : Pos) return Nat is
183       begin
184          for J in 1 .. Nat'Last loop
185             if save_argv (Arg).all (Natural (J)) = ASCII.NUL then
186                return J - 1;
187             end if;
188          end loop;
189
190          raise Program_Error;
191       end Len_Arg;
192
193       ----------------------------
194       -- Scan_Back_End_Switches --
195       ----------------------------
196
197       procedure Scan_Back_End_Switches (Switch_Chars : String) is
198          First : constant Positive := Switch_Chars'First + 1;
199          Last  : Natural := Switch_Chars'Last;
200
201       begin
202          if Last >= First
203            and then Switch_Chars (Last) = ASCII.NUL
204          then
205             Last := Last - 1;
206          end if;
207
208          --  For these switches, skip following argument and do not
209          --  store either the switch or the following argument
210
211          if Switch_Chars (First .. Last) = "o"
212             or else Switch_Chars (First .. Last) = "dumpbase"
213             or else Switch_Chars (First .. Last) = "-param"
214
215          then
216             Next_Arg := Next_Arg + 1;
217
218          --  Do not record -quiet switch
219
220          elsif Switch_Chars (First .. Last) = "quiet" then
221             null;
222
223          else
224             --  Store any other GCC switches
225
226             Store_Compilation_Switch (Switch_Chars);
227          end if;
228       end Scan_Back_End_Switches;
229
230    --  Start of processing for Scan_Compiler_Arguments
231
232    begin
233       --  Acquire stack checking mode directly from GCC
234
235       Opt.Stack_Checking_Enabled := (flag_stack_check /= 0);
236
237       --  Loop through command line arguments, storing them for later access
238
239       while Next_Arg < save_argc loop
240          Look_At_Arg : declare
241             Argv_Ptr : constant BSP    := save_argv (Next_Arg);
242             Argv_Len : constant Nat    := Len_Arg (Next_Arg);
243             Argv     : constant String := Argv_Ptr (1 .. Natural (Argv_Len));
244
245          begin
246             --  If the previous switch has set the Output_File_Name_Present
247             --  flag (that is we have seen a -gnatO), then the next argument
248             --  is the name of the output object file.
249
250             if Output_File_Name_Present
251               and then not Output_File_Name_Seen
252             then
253                if Is_Switch (Argv) then
254                   Fail ("Object file name missing after -gnatO");
255
256                else
257                   Set_Output_Object_File_Name (Argv);
258                   Output_File_Name_Seen := True;
259                end if;
260
261                --  If the previous switch has set the Search_Directory_Present
262                --  flag (that is if we have just seen -I), then the next
263                --  argument is a search directory path.
264
265             elsif Search_Directory_Present then
266                if Is_Switch (Argv) then
267                   Fail ("search directory missing after -I");
268                else
269                   Add_Src_Search_Dir (Argv);
270                   Search_Directory_Present := False;
271                end if;
272
273             elsif not Is_Switch (Argv) then -- must be a file name
274                Add_File (Argv);
275
276             --  We must recognize -nostdinc to suppress visibility on the
277             --  standard GNAT RTL sources. This is also a gcc switch.
278
279             elsif Argv (Argv'First + 1 .. Argv'Last) = "nostdinc" then
280                Opt.No_Stdinc := True;
281                Scan_Back_End_Switches (Argv);
282
283             --  We must recognize -nostdlib to suppress visibility on the
284             --  standard GNAT RTL objects.
285
286             elsif Argv (Argv'First + 1 .. Argv'Last) = "nostdlib" then
287                Opt.No_Stdlib := True;
288
289             elsif Is_Front_End_Switch (Argv) then
290                Scan_Front_End_Switches (Argv);
291
292             --  All non-front-end switches are back-end switches
293
294             else
295                Scan_Back_End_Switches (Argv);
296             end if;
297          end Look_At_Arg;
298
299          Next_Arg := Next_Arg + 1;
300       end loop;
301    end Scan_Compiler_Arguments;
302
303 end Back_End;