| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- A D A B K E N D -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2001-2025, AdaCore -- |
| 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 |
------------------------------------------------------------------------------ |
| 22 |
|
| 23 |
-- This is the version of the Back_End package for back ends written in Ada |
| 24 |
|
| 25 |
with Atree; use Atree; |
| 26 |
with Backend_Utils; use Backend_Utils; |
| 27 |
with Debug; |
| 28 |
with Lib; |
| 29 |
with Opt; use Opt; |
| 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; |
| 35 |
|
| 36 |
with System.OS_Lib; use System.OS_Lib; |
| 37 |
|
| 38 |
package body Adabkend is |
| 39 |
|
| 40 |
use Switch; |
| 41 |
|
| 42 |
------------------- |
| 43 |
-- Call_Back_End -- |
| 44 |
------------------- |
| 45 |
|
| 46 |
procedure Call_Back_End is |
| 47 |
begin |
| 48 |
if (Opt.Verbose_Mode or Opt.Full_List) |
| 49 |
and then not Debug.Debug_Flag_7 |
| 50 |
then |
| 51 |
Write_Eol; |
| 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)"); |
| 57 |
Write_Eol; |
| 58 |
Write_Eol; |
| 59 |
end if; |
| 60 |
|
| 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. |
| 65 |
|
| 66 |
Current_Error_Node := Empty; |
| 67 |
|
| 68 |
Driver (Lib.Cunit (Types.Main_Unit)); |
| 69 |
end Call_Back_End; |
| 70 |
|
| 71 |
----------------------------- |
| 72 |
-- Scan_Compiler_Arguments -- |
| 73 |
----------------------------- |
| 74 |
|
| 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 |
| 78 |
-- "-gnatO file_name" |
| 79 |
|
| 80 |
Argument_Count : constant Integer := Arg_Count - 1; |
| 81 |
-- Number of arguments (excluding program name) |
| 82 |
|
| 83 |
Args : Argument_List (1 .. Argument_Count); |
| 84 |
Next_Arg : Positive := 1; |
| 85 |
|
| 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. |
| 92 |
-- |
| 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. |
| 96 |
|
| 97 |
---------------------------- |
| 98 |
-- Scan_Back_End_Switches -- |
| 99 |
---------------------------- |
| 100 |
|
| 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); |
| 104 |
|
| 105 |
begin |
| 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. |
| 109 |
|
| 110 |
-- Skip -o, -G or internal GCC switches together with their argument. |
| 111 |
|
| 112 |
if Switch_Chars (First .. Last) = "o" |
| 113 |
or else Switch_Chars (First .. Last) = "G" |
| 114 |
or else Is_Internal_GCC_Switch (Switch_Chars) |
| 115 |
then |
| 116 |
Next_Arg := Next_Arg + 1; |
| 117 |
return; -- ignore this switch |
| 118 |
|
| 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). |
| 126 |
|
| 127 |
elsif Switch_Chars (First) = 'O' then |
| 128 |
if First = Last then |
| 129 |
Optimization_Level := 1; |
| 130 |
|
| 131 |
elsif Last - First = 1 then |
| 132 |
if Switch_Chars (Last) = 's' then |
| 133 |
Optimize_Size := 1; |
| 134 |
Optimization_Level := 2; -- Consistent with gcc setting |
| 135 |
|
| 136 |
elsif Switch_Chars (Last) in '0' .. '3' then |
| 137 |
Optimization_Level := |
| 138 |
Character'Pos (Switch_Chars (Last)) - Character'Pos ('0'); |
| 139 |
|
| 140 |
-- Switch -Og is between -O0 and -O1 in GCC. Consider it like |
| 141 |
-- -O0 for other back ends. |
| 142 |
|
| 143 |
elsif Switch_Chars (Last) = 'g' then |
| 144 |
Optimization_Level := 0; |
| 145 |
|
| 146 |
else |
| 147 |
Fail ("invalid switch: " & Switch_Chars); |
| 148 |
end if; |
| 149 |
|
| 150 |
-- Switch -Ofast enables -O3 |
| 151 |
|
| 152 |
elsif Switch_Chars (First + 1 .. Last) = "fast" then |
| 153 |
Optimization_Level := 3; |
| 154 |
|
| 155 |
else |
| 156 |
Fail ("invalid switch: " & Switch_Chars); |
| 157 |
end if; |
| 158 |
|
| 159 |
elsif Switch_Chars (First .. Last) = "quiet" then |
| 160 |
return; -- ignore this switch |
| 161 |
|
| 162 |
elsif Switch_Chars (First .. Last) = "c" then |
| 163 |
return; -- ignore this switch |
| 164 |
|
| 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. |
| 168 |
|
| 169 |
elsif Switch_Chars (First .. Last) = "x" then |
| 170 |
Lib.Store_Compilation_Switch (Switch_Chars); |
| 171 |
Next_Arg := Next_Arg + 1; |
| 172 |
|
| 173 |
declare |
| 174 |
Argv : constant String := Args (Next_Arg).all; |
| 175 |
|
| 176 |
begin |
| 177 |
if Is_Switch (Argv) then |
| 178 |
Fail ("language name missing after -x"); |
| 179 |
else |
| 180 |
Lib.Store_Compilation_Switch (Argv); |
| 181 |
end if; |
| 182 |
end; |
| 183 |
|
| 184 |
return; |
| 185 |
|
| 186 |
-- Ignore all other back-end switches |
| 187 |
|
| 188 |
elsif Scan_Common_Back_End_Switch (Switch_Chars) |
| 189 |
or else Is_Back_End_Switch (Switch_Chars) |
| 190 |
then |
| 191 |
null; |
| 192 |
|
| 193 |
-- Give error for junk switch |
| 194 |
|
| 195 |
else |
| 196 |
Fail ("invalid switch: " & Switch_Chars); |
| 197 |
end if; |
| 198 |
|
| 199 |
-- Store any other GCC switches |
| 200 |
|
| 201 |
Lib.Store_Compilation_Switch (Switch_Chars); |
| 202 |
end Scan_Back_End_Switches; |
| 203 |
|
| 204 |
-- Start of processing for Scan_Compiler_Args |
| 205 |
|
| 206 |
begin |
| 207 |
-- Put all the arguments in argument list Args |
| 208 |
|
| 209 |
for Arg in 1 .. Argument_Count loop |
| 210 |
declare |
| 211 |
Argv : String (1 .. Len_Arg (Arg)); |
| 212 |
begin |
| 213 |
Fill_Arg (Argv'Address, Arg); |
| 214 |
Args (Arg) := new String'(Argv); |
| 215 |
end; |
| 216 |
end loop; |
| 217 |
|
| 218 |
-- Loop through command line arguments, storing them for later access |
| 219 |
|
| 220 |
while Next_Arg <= Argument_Count loop |
| 221 |
Look_At_Arg : declare |
| 222 |
Argv : constant String := Args (Next_Arg).all; |
| 223 |
|
| 224 |
begin |
| 225 |
if Argv'Length = 0 then |
| 226 |
Fail ("Empty argument"); |
| 227 |
end if; |
| 228 |
|
| 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. |
| 232 |
|
| 233 |
if Opt.Output_File_Name_Present |
| 234 |
and then not Output_File_Name_Seen |
| 235 |
then |
| 236 |
if Is_Switch (Argv) then |
| 237 |
Fail ("Object file name missing after -gnatO"); |
| 238 |
else |
| 239 |
Set_Output_Object_File_Name (Argv); |
| 240 |
Output_File_Name_Seen := True; |
| 241 |
end if; |
| 242 |
|
| 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. |
| 246 |
|
| 247 |
elsif Search_Directory_Present then |
| 248 |
if Is_Switch (Argv) then |
| 249 |
Fail ("search directory missing after -I"); |
| 250 |
else |
| 251 |
Add_Src_Search_Dir (Argv); |
| 252 |
|
| 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. |
| 256 |
|
| 257 |
Add_Lib_Search_Dir (Argv); |
| 258 |
|
| 259 |
Search_Directory_Present := False; |
| 260 |
end if; |
| 261 |
|
| 262 |
-- If not a switch, must be a file name |
| 263 |
|
| 264 |
elsif not Is_Switch (Argv) then |
| 265 |
Add_File (Argv); |
| 266 |
|
| 267 |
-- We must recognize -nostdinc to suppress visibility on the |
| 268 |
-- standard GNAT RTL sources. |
| 269 |
|
| 270 |
elsif Argv (Argv'First + 1 .. Argv'Last) = "nostdinc" then |
| 271 |
Opt.No_Stdinc := True; |
| 272 |
|
| 273 |
-- Front end switch |
| 274 |
|
| 275 |
elsif Is_Front_End_Switch (Argv) then |
| 276 |
Scan_Front_End_Switches (Argv, Args, Next_Arg); |
| 277 |
|
| 278 |
-- All non-front-end switches are back-end switches |
| 279 |
|
| 280 |
else |
| 281 |
Scan_Back_End_Switches (Argv); |
| 282 |
end if; |
| 283 |
end Look_At_Arg; |
| 284 |
|
| 285 |
Next_Arg := Next_Arg + 1; |
| 286 |
end loop; |
| 287 |
end Scan_Compiler_Arguments; |
| 288 |
|
| 289 |
end Adabkend; |