1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2021, AdaCore --
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. --
21 ------------------------------------------------------------------------------
23 -- This is the version of the Back_End package for back ends written in Ada
25 with Atree
; use Atree
;
26 with Backend_Utils
; use Backend_Utils
;
30 with Output
; use Output
;
31 with Osint
; use Osint
;
32 with Osint
.C
; use Osint
.C
;
33 with Switch
.C
; use Switch
.C
;
34 with Types
; use Types
;
36 with System
.OS_Lib
; use System
.OS_Lib
;
38 package body Adabkend
is
46 procedure Call_Back_End
is
48 if (Opt
.Verbose_Mode
or Opt
.Full_List
)
49 and then not Debug
.Debug_Flag_7
52 Write_Str
(Product_Name
);
53 Write_Str
(", Copyright ");
54 Write_Str
(Copyright_Years
);
55 Write_Str
(" Ada Core Technologies, Inc.");
56 Write_Str
(" (http://www.adacore.com)");
61 -- The front end leaves the Current_Error_Node at a location that is
62 -- meaningless and confusing when emitting bug boxes from the back end.
63 -- Reset the global variable in order to emit "No source file position
64 -- information available" messages on back end crashes.
66 Current_Error_Node
:= Empty
;
68 Driver
(Lib
.Cunit
(Types
.Main_Unit
));
71 -----------------------------
72 -- Scan_Compiler_Arguments --
73 -----------------------------
75 procedure Scan_Compiler_Arguments
is
76 Output_File_Name_Seen
: Boolean := False;
77 -- Set to True after having scanned the file_name for switch
80 Argument_Count
: constant Integer := Arg_Count
- 1;
81 -- Number of arguments (excluding program name)
83 Args
: Argument_List
(1 .. Argument_Count
);
84 Next_Arg
: Positive := 1;
86 procedure Scan_Back_End_Switches
(Switch_Chars
: String);
87 -- Procedure to scan out switches stored in Switch_Chars. The first
88 -- character is known to be a valid switch character, and there are no
89 -- blanks or other switch terminator characters in the string, so the
90 -- entire string should consist of valid switch characters, except that
91 -- an optional terminating NUL character is allowed.
93 -- If the switch is not valid, control will not return. The switches
94 -- must still be scanned to skip the "-o" arguments, or internal GCC
95 -- switches, which may be safely ignored by other back ends.
97 ----------------------------
98 -- Scan_Back_End_Switches --
99 ----------------------------
101 procedure Scan_Back_End_Switches
(Switch_Chars
: String) is
102 First
: constant Positive := Switch_Chars
'First + 1;
103 Last
: constant Natural := Switch_Last
(Switch_Chars
);
106 -- Process any back end switches, returning if the switch does not
107 -- affect code generation or falling through if it does, so the
108 -- switch will get stored.
110 -- Skip -o, -G or internal GCC switches together with their argument.
112 if Switch_Chars
(First
.. Last
) = "o"
113 or else Switch_Chars
(First
.. Last
) = "G"
114 or else Is_Internal_GCC_Switch
(Switch_Chars
)
116 Next_Arg
:= Next_Arg
+ 1;
117 return; -- ignore this switch
119 -- Set optimization indicators appropriately. In gcc-based GNAT this
120 -- is picked up from imported variables set by the gcc driver, but
121 -- for compilers with non-gcc back ends we do it here to allow use of
122 -- these switches by the front end. Allowed optimization switches are
123 -- -Os (optimize for size), -O[0123], -O (same as -O1), -Ofast
124 -- (disregard strict standards compliance), and -Og (optimize
125 -- debugging experience).
127 elsif Switch_Chars
(First
) = 'O' then
129 Optimization_Level
:= 1;
131 elsif Last
- First
= 1 then
132 if Switch_Chars
(Last
) = 's' then
134 Optimization_Level
:= 2; -- Consistent with gcc setting
136 elsif Switch_Chars
(Last
) in '0' .. '3' then
137 Optimization_Level
:=
138 Character'Pos (Switch_Chars
(Last
)) - Character'Pos ('0');
140 -- Switch -Og is between -O0 and -O1 in GCC. Consider it like
141 -- -O0 for other back ends.
143 elsif Switch_Chars
(Last
) = 'g' then
144 Optimization_Level
:= 0;
147 Fail
("invalid switch: " & Switch_Chars
);
150 -- Switch -Ofast enables -O3
152 elsif Switch_Chars
(First
+ 1 .. Last
) = "fast" then
153 Optimization_Level
:= 3;
156 Fail
("invalid switch: " & Switch_Chars
);
159 elsif Switch_Chars
(First
.. Last
) = "quiet" then
160 return; -- ignore this switch
162 elsif Switch_Chars
(First
.. Last
) = "c" then
163 return; -- ignore this switch
165 -- The -x switch and its language name argument will generally be
166 -- ignored by non-gcc back ends. In any case, we save the switch and
167 -- argument in the compilation switches.
169 elsif Switch_Chars
(First
.. Last
) = "x" then
170 Lib
.Store_Compilation_Switch
(Switch_Chars
);
171 Next_Arg
:= Next_Arg
+ 1;
174 Argv
: constant String := Args
(Next_Arg
).all;
177 if Is_Switch
(Argv
) then
178 Fail
("language name missing after -x");
180 Lib
.Store_Compilation_Switch
(Argv
);
186 -- Ignore all other back-end switches
188 elsif Scan_Common_Back_End_Switch
(Switch_Chars
)
189 or else Is_Back_End_Switch
(Switch_Chars
)
193 -- Give error for junk switch
196 Fail
("invalid switch: " & Switch_Chars
);
199 -- Store any other GCC switches
201 Lib
.Store_Compilation_Switch
(Switch_Chars
);
202 end Scan_Back_End_Switches
;
204 -- Start of processing for Scan_Compiler_Args
207 -- Put all the arguments in argument list Args
209 for Arg
in 1 .. Argument_Count
loop
211 Argv
: String (1 .. Len_Arg
(Arg
));
213 Fill_Arg
(Argv
'Address, Arg
);
214 Args
(Arg
) := new String'(Argv);
218 -- Loop through command line arguments, storing them for later access
220 while Next_Arg <= Argument_Count loop
221 Look_At_Arg : declare
222 Argv : constant String := Args (Next_Arg).all;
225 if Argv'Length = 0 then
226 Fail ("Empty argument");
229 -- If the previous switch has set the Output_File_Name_Present
230 -- flag (that is we have seen a -gnatO), then the next argument
231 -- is the name of the output object file.
233 if Opt.Output_File_Name_Present
234 and then not Output_File_Name_Seen
236 if Is_Switch (Argv) then
237 Fail ("Object file name missing after -gnatO");
239 Set_Output_Object_File_Name (Argv);
240 Output_File_Name_Seen := True;
243 -- If the previous switch has set the Search_Directory_Present
244 -- flag (that is if we have just seen -I), then the next
245 -- argument is a search directory path.
247 elsif Search_Directory_Present then
248 if Is_Switch (Argv) then
249 Fail ("search directory missing after -I");
251 Add_Src_Search_Dir (Argv);
253 -- Add directory to lib search so that back end can take as
254 -- input ALI files if needed. Otherwise this won't have any
255 -- impact on the compiler.
257 Add_Lib_Search_Dir (Argv);
259 Search_Directory_Present := False;
262 -- If not a switch, must be a file name
264 elsif not Is_Switch (Argv) then
267 -- We must recognize -nostdinc to suppress visibility on the
268 -- standard GNAT RTL sources.
270 elsif Argv (Argv'First + 1 .. Argv'Last) = "nostdinc" then
271 Opt.No_Stdinc := True;
275 elsif Is_Front_End_Switch (Argv) then
276 Scan_Front_End_Switches (Argv, Args, Next_Arg);
278 -- All non-front-end switches are back-end switches
281 Scan_Back_End_Switches (Argv);
285 Next_Arg := Next_Arg + 1;
287 end Scan_Compiler_Arguments;