]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/gnat1drv.adb
2017-09-08 Yannick Moy <moy@adacore.com>
[thirdparty/gcc.git] / gcc / ada / gnat1drv.adb
CommitLineData
83cce46b 1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- G N A T 1 D R V --
6-- --
7-- B o d y --
8-- --
739b155e 9-- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
83cce46b 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- --
80df182a 13-- ware Foundation; either version 3, or (at your option) any later ver- --
83cce46b 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 --
80df182a 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. --
83cce46b 20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
e78e8c8e 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
83cce46b 23-- --
24------------------------------------------------------------------------------
25
26with Atree; use Atree;
27with Back_End; use Back_End;
7b8fa048 28with Checks;
83cce46b 29with Comperr;
30with Csets; use Csets;
31with Debug; use Debug;
32with Elists;
33with Errout; use Errout;
e00e091c 34with Exp_CG;
9dfe12ae 35with Fmap;
83cce46b 36with Fname; use Fname;
37with Fname.UF; use Fname.UF;
38with Frontend;
7dbb2cf6 39with Ghost; use Ghost;
83cce46b 40with Gnatvsn; use Gnatvsn;
83cce46b 41with Inline;
42with Lib; use Lib;
43with Lib.Writ; use Lib.Writ;
9dfe12ae 44with Lib.Xref;
83cce46b 45with Namet; use Namet;
46with Nlists;
47with Opt; use Opt;
48with Osint; use Osint;
a0a15971 49with Osint.C; use Osint.C;
83cce46b 50with Output; use Output;
11b376d2 51with Par_SCO;
9dfe12ae 52with Prepcomp;
83cce46b 53with Repinfo; use Repinfo;
9dfe12ae 54with Restrict;
925c2ba1 55with Rident; use Rident;
10084566 56with Rtsfind;
a9958373 57with SCOs;
83cce46b 58with Sem;
9dfe12ae 59with Sem_Ch8;
60with Sem_Ch12;
83cce46b 61with Sem_Ch13;
e7b2d6bc 62with Sem_Elim;
9dfe12ae 63with Sem_Eval;
64with Sem_Type;
cba2ae82 65with Set_Targ;
83cce46b 66with Sinfo; use Sinfo;
67with Sinput.L; use Sinput.L;
68with Snames;
69with Sprint; use Sprint;
70with Stringt;
9af0ddc7 71with Stylesw; use Stylesw;
10084566 72with Targparm; use Targparm;
7cc02797 73with Tbuild;
83cce46b 74with Tree_Gen;
75with Treepr; use Treepr;
76with Ttypes;
77with Types; use Types;
9dfe12ae 78with Uintp; use Uintp;
83cce46b 79with Uname; use Uname;
80with Urealp;
81with Usage;
9af0ddc7 82with Validsw; use Validsw;
83cce46b 83
84with System.Assertions;
82b93248 85with System.OS_Lib;
83cce46b 86
d10a989c 87--------------
88-- Gnat1drv --
89--------------
90
83cce46b 91procedure Gnat1drv is
749c967d 92 procedure Adjust_Global_Switches;
f9906591 93 -- There are various interactions between front-end switch settings,
749c967d 94 -- including debug switch settings and target dependent parameters.
95 -- This procedure takes care of properly handling these interactions.
063ed58b 96 -- We do it after scanning out all the switches, so that we are not
749c967d 97 -- depending on the order in which switches appear.
98
61989dbb 99 procedure Check_Bad_Body (Unit_Node : Node_Id; Unit_Kind : Node_Kind);
100 -- Called to check whether a unit described by its compilation unit node
101 -- and kind has a bad body.
10084566 102
103 procedure Check_Rep_Info;
104 -- Called when we are not generating code, to check if -gnatR was requested
105 -- and if so, explain that we will not be honoring the request.
106
7b8fa048 107 procedure Post_Compilation_Validation_Checks;
108 -- This procedure performs various validation checks that have to be left
109 -- to the end of the compilation process, after generating code but before
110 -- issuing error messages. In particular, these checks generally require
111 -- the information provided by the back end in back annotation of declared
112 -- entities (e.g. actual size and alignment values chosen by the back end).
113
749c967d 114 ----------------------------
115 -- Adjust_Global_Switches --
116 ----------------------------
117
118 procedure Adjust_Global_Switches is
be1bb0b1 119 procedure SPARK_Library_Warning (Kind : String);
120 -- Issue a warning in GNATprove mode if the run-time library does not
121 -- fully support IEEE-754 floating-point semantics.
122
123 ---------------------------
124 -- SPARK_Library_Warning --
125 ---------------------------
126
127 procedure SPARK_Library_Warning (Kind : String) is
128 begin
129 Write_Line
130 ("warning: run-time library may be configured incorrectly");
131 Write_Line
132 ("warning: (SPARK analysis requires support for " & Kind & ')');
133 end SPARK_Library_Warning;
134
135 -- Start of processing for Adjust_Global_Switches
136
749c967d 137 begin
a9cd517c 138 -- -gnatd.M enables Relaxed_RM_Semantics
9af0ddc7 139
a9cd517c 140 if Debug_Flag_Dot_MM then
141 Relaxed_RM_Semantics := True;
9af0ddc7 142 end if;
df45adfd 143
144 -- -gnatd.1 enables unnesting of subprograms
145
146 if Debug_Flag_Dot_1 then
147 Unnest_Subprogram_Mode := True;
148 end if;
749c967d 149
084c3073 150 -- -gnatd.u enables special C expansion mode
27cc5512 151
084c3073 152 if Debug_Flag_Dot_U then
27cc5512 153 Modify_Tree_For_C := True;
154 end if;
155
d1204dd6 156 -- Set all flags required when generating C code
157
158 if Generate_C_Code then
084c3073 159 Modify_Tree_For_C := True;
07c38dd9 160 Unnest_Subprogram_Mode := True;
9c890dc4 161 Minimize_Expression_With_Actions := True;
084c3073 162
084c3073 163 -- Set operating mode to Generate_Code to benefit from full front-end
164 -- expansion (e.g. generics).
165
166 Operating_Mode := Generate_Code;
e9c863fc 167
168 -- Suppress alignment checks since we do not have access to alignment
f90dd1de 169 -- info on the target.
e9c863fc 170
171 Suppress_Options.Suppress (Alignment_Check) := False;
07c38dd9 172 end if;
173
ce9bfeb1 174 -- -gnatd.E sets Error_To_Warning mode, causing selected error messages
175 -- to be treated as warnings instead of errors.
176
177 if Debug_Flag_Dot_EE then
178 Error_To_Warning := True;
179 end if;
180
0d1fd194 181 -- -gnatdJ sets Include_Subprogram_In_Messages, adding the related
182 -- subprogram as part of the error and warning messages.
183
184 if Debug_Flag_JJ then
185 Include_Subprogram_In_Messages := True;
186 end if;
187
f005e79a 188 -- Disable CodePeer_Mode in Check_Syntax, since we need front-end
189 -- expansion.
190
191 if Operating_Mode = Check_Syntax then
192 CodePeer_Mode := False;
193 end if;
194
749c967d 195 -- Set ASIS mode if -gnatt and -gnatc are set
196
197 if Operating_Mode = Check_Semantics and then Tree_Output then
198 ASIS_Mode := True;
199
f74a102b 200 -- Set ASIS GNSA mode if -gnatd.H is set
201
202 if Debug_Flag_Dot_HH then
203 ASIS_GNSA_Mode := True;
204 end if;
205
749c967d 206 -- Turn off inlining in ASIS mode, since ASIS cannot handle the extra
207 -- information in the trees caused by inlining being active.
208
15a0a165 209 -- More specifically, the tree seems to be malformed from the ASIS
210 -- point of view if -gnatc and -gnatn appear together???
749c967d 211
212 Inline_Active := False;
213
f005e79a 214 -- Turn off SCIL generation and CodePeer mode in semantics mode,
215 -- since SCIL requires front-end expansion.
9af0ddc7 216
217 Generate_SCIL := False;
f005e79a 218 CodePeer_Mode := False;
9af0ddc7 219 end if;
220
221 -- SCIL mode needs to disable front-end inlining since the generated
222 -- trees (in particular order and consistency between specs compiled
223 -- as part of a main unit or as part of a with-clause) are causing
224 -- troubles.
749c967d 225
9af0ddc7 226 if Generate_SCIL then
227 Front_End_Inlining := False;
749c967d 228 end if;
229
64427fe6 230 -- Tune settings for optimal SCIL generation in CodePeer mode
9af0ddc7 231
232 if CodePeer_Mode then
233
fbf4d6ef 234 -- Turn off gnatprove mode (which can be set via e.g. -gnatd.F), not
235 -- compatible with CodePeer mode.
58c0ee52 236
237 GNATprove_Mode := False;
fbf4d6ef 238 Debug_Flag_Dot_FF := False;
58c0ee52 239
dad188fe 240 -- Turn off C tree generation, not compatible with CodePeer mode. We
241 -- do not expect this to happen in normal use, since both modes are
242 -- enabled by special tools, but it is useful to turn off these flags
3f716509 243 -- this way when we are doing CodePeer tests on existing test suites
cb388b10 244 -- that may have -gnateg set, to avoid the need for special casing.
dad188fe 245
246 Modify_Tree_For_C := False;
247 Generate_C_Code := False;
248 Unnest_Subprogram_Mode := False;
249
cf563f22 250 -- Turn off inlining, confuses CodePeer output and gains nothing
749c967d 251
749c967d 252 Front_End_Inlining := False;
9af0ddc7 253 Inline_Active := False;
254
43e39b42 255 -- Disable front-end optimizations, to keep the tree as close to the
256 -- source code as possible, and also to avoid inconsistencies between
257 -- trees when using different optimization switches.
258
259 Optimization_Level := 0;
260
925c2ba1 261 -- Enable some restrictions systematically to simplify the generated
262 -- code (and ease analysis). Note that restriction checks are also
ee0d6223 263 -- disabled in CodePeer mode, see Restrict.Check_Restriction, and
264 -- user specified Restrictions pragmas are ignored, see
265 -- Sem_Prag.Process_Restrictions_Or_Restriction_Warnings.
4606d5a9 266
084c3073 267 Restrict.Restrictions.Set (No_Exception_Registration) := True;
ee0d6223 268 Restrict.Restrictions.Set (No_Initialize_Scalars) := True;
64427fe6 269 Restrict.Restrictions.Set (No_Task_Hierarchy) := True;
270 Restrict.Restrictions.Set (No_Abort_Statements) := True;
271 Restrict.Restrictions.Set (Max_Asynchronous_Select_Nesting) := True;
925c2ba1 272 Restrict.Restrictions.Value (Max_Asynchronous_Select_Nesting) := 0;
4606d5a9 273
eba9690d 274 -- Enable pragma Ignore_Pragma (Global) to support legacy code
275
276 Set_Name_Table_Boolean3 (Name_Id'(Name_Find ("global")), True);
277
278 -- Suppress division by zero checks since they are handled
d757af67 279 -- implicitly by CodePeer.
cd534f03 280
281 -- Turn off dynamic elaboration checks: generates inconsistencies in
9af0ddc7 282 -- trees between specs compiled as part of a main unit or as part of
283 -- a with-clause.
284
2f1aac99 285 -- Turn off alignment checks: these cannot be proved statically by
286 -- CodePeer and generate false positives.
287
cd534f03 288 -- Enable all other language checks
9af0ddc7 289
0df9d43f 290 Suppress_Options.Suppress :=
f5a9a481 291 (Alignment_Check => True,
0df9d43f 292 Division_Check => True,
293 Elaboration_Check => True,
294 others => False);
fafc6b97 295
cd534f03 296 Dynamic_Elaboration_Checks := False;
9af0ddc7 297
c412fcee 298 -- Set STRICT mode for overflow checks if not set explicitly. This
299 -- prevents suppressing of overflow checks by default, in code down
300 -- below.
d757af67 301
db415383 302 if Suppress_Options.Overflow_Mode_General = Not_Set then
303 Suppress_Options.Overflow_Mode_General := Strict;
304 Suppress_Options.Overflow_Mode_Assertions := Strict;
d757af67 305 end if;
306
af25d38d 307 -- CodePeer handles division and overflow checks directly, based on
308 -- the marks set by the frontend, hence no special expansion should
309 -- be performed in the frontend for division and overflow checks.
310
311 Backend_Divide_Checks_On_Target := True;
312 Backend_Overflow_Checks_On_Target := True;
313
9af0ddc7 314 -- Kill debug of generated code, since it messes up sloc values
315
316 Debug_Generated_Code := False;
317
e6c83a35 318 -- Ditto for -gnateG which interacts badly with handling of pragma
319 -- Annotate in gnat2scil.
320
321 Generate_Processed_File := False;
322
1ecdfe4b 323 -- Disable Exception_Extra_Info (-gnateE) which generates more
324 -- complex trees with no added value, and may confuse CodePeer.
325
326 Exception_Extra_Info := False;
327
64427fe6 328 -- Turn cross-referencing on in case it was disabled (e.g. by -gnatD)
7241a851 329 -- to support source navigation.
9af0ddc7 330
331 Xref_Active := True;
332
333 -- Polling mode forced off, since it generates confusing junk
334
335 Polling_Required := False;
336
64427fe6 337 -- Set operating mode to Generate_Code to benefit from full front-end
338 -- expansion (e.g. generics).
9af0ddc7 339
34fd8639 340 Operating_Mode := Generate_Code;
9af0ddc7 341
342 -- We need SCIL generation of course
343
344 Generate_SCIL := True;
345
51ea9c94 346 -- Enable assertions, since they give CodePeer valuable extra info
9af0ddc7 347
ed3c9510 348 Assertions_Enabled := True;
9af0ddc7 349
7241a851 350 -- Set normal RM validity checking and checking of copies (to catch
a54e1834 351 -- e.g. wrong values used in unchecked conversions).
7241a851 352 -- All other validity checking is turned off, since this can generate
353 -- very complex trees that only confuse CodePeer and do not bring
354 -- enough useful info.
9af0ddc7 355
356 Reset_Validity_Check_Options;
357 Validity_Check_Default := True;
7241a851 358 Validity_Check_Copies := True;
9af0ddc7 359
8fd43ddd 360 -- Turn off style check options and ignore any style check pragmas
361 -- since we are not interested in any front-end warnings when we are
362 -- getting CodePeer output.
9af0ddc7 363
364 Reset_Style_Check_Options;
8fd43ddd 365 Ignore_Style_Checks_Pragmas := True;
39a79c9e 366
367 -- Always perform semantics and generate ali files in CodePeer mode,
368 -- so that a gnatmake -c -k will proceed further when possible.
369
370 Force_ALI_Tree_File := True;
371 Try_Semantics := True;
a9cd517c 372
f9906591 373 -- Make the Ada front end more liberal so that the compiler will
ce4da1ed 374 -- allow illegal code that is allowed by other compilers. CodePeer
39a0c1d3 375 -- is in the business of finding problems, not enforcing rules.
ce4da1ed 376 -- This is useful when using CodePeer mode with other compilers.
377
a9cd517c 378 Relaxed_RM_Semantics := True;
7241a851 379
380 -- Disable all simple value propagation. This is an optimization
381 -- which is valuable for code optimization, and also for generation
382 -- of compiler warnings, but these are being turned off by default,
383 -- and CodePeer generates better messages (referencing original
384 -- variables) this way.
8b2b45b8 385 -- Do this only if -gnatws is set (the default with -gnatcC), so that
7241a851 386 -- if warnings are enabled, we'll get better messages from GNAT.
387
388 if Warning_Mode = Suppress then
389 Debug_Flag_MM := True;
390 end if;
a9cd517c 391 end if;
392
cf45b231 393 -- Enable some individual switches that are implied by relaxed RM
394 -- semantics mode.
395
a9cd517c 396 if Relaxed_RM_Semantics then
ab78ef7f 397 Opt.Allow_Integer_Address := True;
a9cd517c 398 Overriding_Renamings := True;
778ebf56 399 Treat_Categorization_Errors_As_Warnings := True;
749c967d 400 end if;
401
c39cce40 402 -- Enable GNATprove_Mode when using -gnatd.F switch
67ff7f27 403
175a6969 404 if Debug_Flag_Dot_FF then
c39cce40 405 GNATprove_Mode := True;
175a6969 406 end if;
407
c39cce40 408 -- GNATprove_Mode is also activated by default in the gnat2why
409 -- executable.
175a6969 410
c39cce40 411 if GNATprove_Mode then
c412fcee 412
739b155e 413 -- Turn off CodePeer mode (which can be set via e.g. -gnatC or
414 -- -gnateC), not compatible with GNATprove mode.
415
416 CodePeer_Mode := False;
417 Generate_SCIL := False;
418
419 -- Turn off C tree generation, not compatible with GNATprove mode. We
420 -- do not expect this to happen in normal use, since both modes are
421 -- enabled by special tools, but it is useful to turn off these flags
422 -- this way when we are doing GNATprove tests on existing test suites
423 -- that may have -gnateg set, to avoid the need for special casing.
424
425 Modify_Tree_For_C := False;
426 Generate_C_Code := False;
427 Unnest_Subprogram_Mode := False;
428
c412fcee 429 -- Turn off inlining, which would confuse formal verification output
430 -- and gain nothing.
431
432 Front_End_Inlining := False;
433 Inline_Active := False;
434
72756019 435 -- Issue warnings for failure to inline subprograms, as otherwise
436 -- expected in GNATprove mode for the local subprograms without
437 -- contracts.
438
439 Ineffective_Inline_Warnings := True;
440
c412fcee 441 -- Disable front-end optimizations, to keep the tree as close to the
442 -- source code as possible, and also to avoid inconsistencies between
443 -- trees when using different optimization switches.
444
445 Optimization_Level := 0;
446
447 -- Enable some restrictions systematically to simplify the generated
1240b98d 448 -- code (and ease analysis).
c412fcee 449
450 Restrict.Restrictions.Set (No_Initialize_Scalars) := True;
451
452 -- Note: at this point we used to suppress various checks, but that
453 -- is not what we want. We need the semantic processing for these
454 -- checks (which will set flags like Do_Overflow_Check, showing the
455 -- points at which potential checks are required semantically). We
456 -- don't want the expansion associated with these checks, but that
457 -- happens anyway because this expansion is simply not done in the
b4f636a7 458 -- SPARK version of the expander.
c412fcee 459
0d4674f2 460 -- On the contrary, we need to enable explicitly all language checks,
9c7948d7 461 -- as they may have been suppressed by the use of switch -gnatp.
0d4674f2 462
463 Suppress_Options.Suppress := (others => False);
464
efa86d10 465 -- Detect overflow on unconstrained floating-point types, such as
466 -- the predefined types Float, Long_Float and Long_Long_Float from
22d3a5a3 467 -- package Standard. Not necessary if float overflows are checked
468 -- (Machine_Overflow true), since appropriate Do_Overflow_Check flags
469 -- will be set in any case.
efa86d10 470
22d3a5a3 471 Check_Float_Overflow := not Machine_Overflows_On_Target;
efa86d10 472
c412fcee 473 -- Set STRICT mode for overflow checks if not set explicitly. This
474 -- prevents suppressing of overflow checks by default, in code down
475 -- below.
476
db415383 477 if Suppress_Options.Overflow_Mode_General = Not_Set then
478 Suppress_Options.Overflow_Mode_General := Strict;
479 Suppress_Options.Overflow_Mode_Assertions := Strict;
c412fcee 480 end if;
481
482 -- Kill debug of generated code, since it messes up sloc values
483
484 Debug_Generated_Code := False;
485
486 -- Turn cross-referencing on in case it was disabled (e.g. by -gnatD)
487 -- as it is needed for computing effects of subprograms in the formal
488 -- verification backend.
489
490 Xref_Active := True;
491
492 -- Polling mode forced off, since it generates confusing junk
493
494 Polling_Required := False;
495
c39cce40 496 -- Set operating mode to Check_Semantics, but a light front-end
497 -- expansion is still performed.
c412fcee 498
c39cce40 499 Operating_Mode := Check_Semantics;
c412fcee 500
51ea9c94 501 -- Enable assertions, since they give valuable extra information for
502 -- formal verification.
c412fcee 503
51ea9c94 504 Assertions_Enabled := True;
c412fcee 505
021bbd2b 506 -- Disable validity checks, since it generates code raising
507 -- exceptions for invalid data, which confuses GNATprove. Invalid
508 -- data is directly detected by GNATprove's flow analysis.
509
510 Validity_Checks_On := False;
511
c412fcee 512 -- Turn off style check options since we are not interested in any
b4f636a7 513 -- front-end warnings when we are getting SPARK output.
c412fcee 514
515 Reset_Style_Check_Options;
516
c412fcee 517 -- Suppress the generation of name tables for enumerations, which are
b4f636a7 518 -- not needed for formal verification, and fall outside the SPARK
c412fcee 519 -- subset (use of pointers).
520
521 Global_Discard_Names := True;
522
523 -- Suppress the expansion of tagged types and dispatching calls,
b4f636a7 524 -- which lead to the generation of non-SPARK code (use of pointers),
c412fcee 525 -- which is more complex to formally verify than the original source.
526
527 Tagged_Type_Expansion := False;
52a59c82 528
529 -- Detect that the runtime library support for floating-point numbers
530 -- may not be compatible with SPARK analysis of IEEE-754 floats.
531
be1bb0b1 532 if Denorm_On_Target = False then
533 SPARK_Library_Warning ("float subnormals");
534
535 elsif Machine_Rounds_On_Target = False then
536 SPARK_Library_Warning ("float rounding");
537
538 elsif Signed_Zeros_On_Target = False then
539 SPARK_Library_Warning ("signed zeros");
540 end if;
c412fcee 541 end if;
542
ed3c9510 543 -- Set Configurable_Run_Time mode if system.ads flag set or if the
544 -- special debug flag -gnatdY is set.
749c967d 545
546 if Targparm.Configurable_Run_Time_On_Target or Debug_Flag_YY then
547 Configurable_Run_Time_Mode := True;
548 end if;
549
942051ca 550 -- Set -gnatRm mode if debug flag A set
749c967d 551
552 if Debug_Flag_AA then
553 Back_Annotate_Rep_Info := True;
554 List_Representation_Info := 1;
555 List_Representation_Info_Mechanisms := True;
556 end if;
557
558 -- Force Target_Strict_Alignment true if debug flag -gnatd.a is set
559
560 if Debug_Flag_Dot_A then
561 Ttypes.Target_Strict_Alignment := True;
562 end if;
563
9b8df6be 564 -- Increase size of allocated entities if debug flag -gnatd.N is set
565
566 if Debug_Flag_Dot_NN then
567 Atree.Num_Extension_Nodes := Atree.Num_Extension_Nodes + 1;
568 end if;
569
3a7fe2f3 570 -- Disable static allocation of dispatch tables if -gnatd.t is enabled.
571 -- The front end's layout phase currently treats types that have
572 -- discriminant-dependent arrays as not being static even when a
749c967d 573 -- discriminant constraint on the type is static, and this leads to
574 -- problems with subtypes of type Ada.Tags.Dispatch_Table_Wrapper. ???
575
3a7fe2f3 576 if Debug_Flag_Dot_T then
749c967d 577 Static_Dispatch_Tables := False;
578 end if;
579
580 -- Flip endian mode if -gnatd8 set
581
582 if Debug_Flag_8 then
583 Ttypes.Bytes_Big_Endian := not Ttypes.Bytes_Big_Endian;
584 end if;
585
82c42a95 586 -- Set and check exception mechanism. This is only meaningful when
587 -- compiling, and in particular not meaningful for special modes used
588 -- for program analysis rather than compilation: ASIS mode, CodePeer
589 -- mode and GNATprove mode.
590
591 if Operating_Mode = Generate_Code
592 and then not (ASIS_Mode or CodePeer_Mode or GNATprove_Mode)
593 then
594 case Targparm.Frontend_Exceptions_On_Target is
595 when True =>
596 case Targparm.ZCX_By_Default_On_Target is
597 when True =>
598 Write_Line
599 ("Run-time library configured incorrectly");
600 Write_Line
601 ("(requesting support for Frontend ZCX exceptions)");
602 raise Unrecoverable_Error;
99378362 603
82c42a95 604 when False =>
605 Exception_Mechanism := Front_End_SJLJ;
606 end case;
99378362 607
82c42a95 608 when False =>
609 case Targparm.ZCX_By_Default_On_Target is
610 when True =>
611 Exception_Mechanism := Back_End_ZCX;
612 when False =>
613 Exception_Mechanism := Back_End_SJLJ;
614 end case;
615 end case;
616 end if;
749c967d 617
0df9d43f 618 -- Set proper status for overflow check mechanism
9ea61fdd 619
b4f636a7 620 -- If already set (by -gnato or above in SPARK or CodePeer mode) then we
c412fcee 621 -- have nothing to do.
724d2bd8 622
db415383 623 if Opt.Suppress_Options.Overflow_Mode_General /= Not_Set then
724d2bd8 624 null;
625
0df9d43f 626 -- Otherwise set overflow mode defaults
9ea61fdd 627
0df9d43f 628 else
e14a3829 629 -- Overflow checks are on by default (Suppress set False) except in
630 -- GNAT_Mode, where we want them off by default (we are not ready to
631 -- enable overflow checks in the compiler yet, for one thing the case
632 -- of 64-bit checks needs System.Arith_64 which is not a compiler
633 -- unit and it is a pain to try to include it in the compiler.
b6a8f264 634
e14a3829 635 Suppress_Options.Suppress (Overflow_Check) := GNAT_Mode;
b6a8f264 636
0df9d43f 637 -- Set appropriate default overflow handling mode. Note: at present
638 -- we set STRICT in all three of the following cases. They are
639 -- separated because in the future we may make different choices.
724d2bd8 640
0df9d43f 641 -- By default set STRICT mode if -gnatg in effect
724d2bd8 642
0df9d43f 643 if GNAT_Mode then
db415383 644 Suppress_Options.Overflow_Mode_General := Strict;
645 Suppress_Options.Overflow_Mode_Assertions := Strict;
724d2bd8 646
0df9d43f 647 -- If we have backend divide and overflow checks, then by default
648 -- overflow checks are STRICT. Historically this code used to also
649 -- activate overflow checks, although no target currently has these
650 -- flags set, so this was dead code anyway.
651
652 elsif Targparm.Backend_Divide_Checks_On_Target
e14a3829 653 and
654 Targparm.Backend_Overflow_Checks_On_Target
0df9d43f 655 then
db415383 656 Suppress_Options.Overflow_Mode_General := Strict;
657 Suppress_Options.Overflow_Mode_Assertions := Strict;
0df9d43f 658
659 -- Otherwise for now, default is STRICT mode. This may change in the
660 -- future, but for now this is the compatible behavior with previous
661 -- versions of GNAT.
662
663 else
db415383 664 Suppress_Options.Overflow_Mode_General := Strict;
665 Suppress_Options.Overflow_Mode_Assertions := Strict;
0df9d43f 666 end if;
749c967d 667 end if;
06e076c6 668
50cab70b 669 -- Set default for atomic synchronization. As this synchronization
670 -- between atomic accesses can be expensive, and not typically needed
671 -- on some targets, an optional target parameter can turn the option
672 -- off. Note Atomic Synchronization is implemented as check.
673
fafc6b97 674 Suppress_Options.Suppress (Atomic_Synchronization) :=
5ea6a4ea 675 not Atomic_Sync_Default_On_Target;
50cab70b 676
e556831e 677 -- Set default for Alignment_Check, if we are on a machine with non-
678 -- strict alignment, then we suppress this check, since it is over-
679 -- zealous for such machines.
680
681 if not Ttypes.Target_Strict_Alignment then
682 Suppress_Options.Suppress (Alignment_Check) := True;
683 end if;
684
c9e3ee19 685 -- Set switch indicating if back end can handle limited types, and
686 -- guarantee that no incorrect copies are made (e.g. in the context
92f1631f 687 -- of an if or case expression).
c9e3ee19 688
689 -- Debug flag -gnatd.L decisively sets usage on
690
e977c0cf 691 if Debug_Flag_Dot_LL then
c9e3ee19 692 Back_End_Handles_Limited_Types := True;
693
29d958a7 694 -- If no debug flag, usage off for SCIL cases
c9e3ee19 695
29d958a7 696 elsif Generate_SCIL then
c9e3ee19 697 Back_End_Handles_Limited_Types := False;
698
1f4c5ad8 699 -- Otherwise normal gcc back end, for now still turn flag off by
d8428da5 700 -- default, since there are unresolved problems in the front end.
c9e3ee19 701
702 else
703 Back_End_Handles_Limited_Types := False;
704 end if;
9c714f97 705
6a8773aa 706 -- If the inlining level has not been set by the user, compute it from
707 -- the optimization level: 1 at -O1/-O2 (and -Os), 2 at -O3 and above.
708
709 if Inline_Level = 0 then
710 if Optimization_Level < 3 then
711 Inline_Level := 1;
712 else
713 Inline_Level := 2;
714 end if;
715 end if;
724d2bd8 716
b94a633e 717 -- Treat -gnatn as equivalent to -gnatN for non-GCC targets
718
3ad60f63 719 if Inline_Active and not Front_End_Inlining then
720
721 -- We really should have a tag for this, what if we added a new
722 -- back end some day, it would not be true for this test, but it
723 -- would be non-GCC, so this is a bit troublesome ???
724
29d958a7 725 Front_End_Inlining := Generate_C_Code;
b94a633e 726 end if;
727
f9906591 728 -- Set back-end inlining indication
9fac98bb 729
730 Back_End_Inlining :=
3296750c 731
f9906591 732 -- No back-end inlining available on C generation
41a8d10f 733
29d958a7 734 not Generate_C_Code
41a8d10f 735
f9906591 736 -- No back-end inlining in GNATprove mode, since it just confuses
3296750c 737 -- the formal verification process.
738
739 and then not GNATprove_Mode
740
f9906591 741 -- No back-end inlining if front-end inlining explicitly enabled.
e173b833 742 -- Done to minimize the output differences to customers still using
743 -- this deprecated switch; in addition, this behavior reduces the
744 -- output differences in old tests.
3296750c 745
746 and then not Front_End_Inlining
747
f9906591 748 -- Back-end inlining is disabled if debug flag .z is set
3296750c 749
e173b833 750 and then not Debug_Flag_Dot_Z;
9fac98bb 751
ed3c9510 752 -- Output warning if -gnateE specified and cannot be supported
753
754 if Exception_Extra_Info
755 and then Restrict.No_Exception_Handlers_Set
756 then
757 Set_Standard_Error;
758 Write_Str
759 ("warning: extra exception information (-gnateE) was specified");
760 Write_Eol;
761 Write_Str
762 ("warning: this capability is not available in this configuration");
763 Write_Eol;
764 Set_Standard_Output;
765 end if;
766
724d2bd8 767 -- Finally capture adjusted value of Suppress_Options as the initial
768 -- value for Scope_Suppress, which will be modified as we move from
769 -- scope to scope (by Suppress/Unsuppress/Overflow_Checks pragmas).
770
771 Sem.Scope_Suppress := Opt.Suppress_Options;
749c967d 772 end Adjust_Global_Switches;
773
10084566 774 --------------------
775 -- Check_Bad_Body --
776 --------------------
777
61989dbb 778 procedure Check_Bad_Body (Unit_Node : Node_Id; Unit_Kind : Node_Kind) is
779 Fname : File_Name_Type;
10084566 780
781 procedure Bad_Body_Error (Msg : String);
782 -- Issue message for bad body found
783
784 --------------------
785 -- Bad_Body_Error --
786 --------------------
787
788 procedure Bad_Body_Error (Msg : String) is
789 begin
61989dbb 790 Error_Msg_N (Msg, Unit_Node);
10084566 791 Error_Msg_File_1 := Fname;
61989dbb 792 Error_Msg_N ("remove incorrect body in file{!", Unit_Node);
10084566 793 end Bad_Body_Error;
794
61989dbb 795 -- Local variables
796
797 Sname : Unit_Name_Type;
798 Src_Ind : Source_File_Index;
799
64427fe6 800 -- Start of processing for Check_Bad_Body
10084566 801
802 begin
803 -- Nothing to do if we are only checking syntax, because we don't know
804 -- enough to know if we require or forbid a body in this case.
805
806 if Operating_Mode = Check_Syntax then
807 return;
808 end if;
809
810 -- Check for body not allowed
811
61989dbb 812 if (Unit_Kind = N_Package_Declaration
813 and then not Body_Required (Unit_Node))
814 or else (Unit_Kind = N_Generic_Package_Declaration
815 and then not Body_Required (Unit_Node))
816 or else Unit_Kind = N_Package_Renaming_Declaration
817 or else Unit_Kind = N_Subprogram_Renaming_Declaration
818 or else Nkind (Original_Node (Unit (Unit_Node)))
10084566 819 in N_Generic_Instantiation
820 then
821 Sname := Unit_Name (Main_Unit);
822
823 -- If we do not already have a body name, then get the body name
10084566 824
825 if not Is_Body_Name (Sname) then
826 Sname := Get_Body_Name (Sname);
827 end if;
828
829 Fname := Get_File_Name (Sname, Subunit => False);
830 Src_Ind := Load_Source_File (Fname);
831
69a5377d 832 -- Case where body is present and it is not a subunit. Exclude the
833 -- subunit case, because it has nothing to do with the package we are
834 -- compiling. It is illegal for a child unit and a subunit with the
835 -- same expanded name (RM 10.2(9)) to appear together in a partition,
836 -- but there is nothing to stop a compilation environment from having
837 -- both, and the test here simply allows that. If there is an attempt
838 -- to include both in a partition, this is diagnosed at bind time. In
839 -- Ada 83 mode this is not a warning case.
840
f37968de 841 -- Note that in general we do not give the message if the file in
842 -- question does not look like a body. This includes weird cases,
843 -- but in particular means that if the file is just a No_Body pragma,
844 -- then we won't give the message (that's the whole point of this
845 -- pragma, to be used this way and to cause the body file to be
846 -- ignored in this context).
10084566 847
848 if Src_Ind /= No_Source_File
f37968de 849 and then Source_File_Is_Body (Src_Ind)
10084566 850 then
150564b2 851 Errout.Finalize (Last_Call => False);
10084566 852
853 Error_Msg_Unit_1 := Sname;
854
855 -- Ada 83 case of a package body being ignored. This is not an
856 -- error as far as the Ada 83 RM is concerned, but it is almost
857 -- certainly not what is wanted so output a warning. Give this
858 -- message only if there were no errors, since otherwise it may
859 -- be incorrect (we may have misinterpreted a junk spec as not
860 -- needing a body when it really does).
861
61989dbb 862 if Unit_Kind = N_Package_Declaration
10084566 863 and then Ada_Version = Ada_83
864 and then Operating_Mode = Generate_Code
865 and then Distribution_Stub_Mode /= Generate_Caller_Stub_Body
866 and then not Compilation_Errors
867 then
868 Error_Msg_N
61989dbb 869 ("package $$ does not require a body??", Unit_Node);
10084566 870 Error_Msg_File_1 := Fname;
61989dbb 871 Error_Msg_N ("body in file{ will be ignored??", Unit_Node);
10084566 872
873 -- Ada 95 cases of a body file present when no body is
874 -- permitted. This we consider to be an error.
875
876 else
877 -- For generic instantiations, we never allow a body
878
61989dbb 879 if Nkind (Original_Node (Unit (Unit_Node))) in
f37968de 880 N_Generic_Instantiation
10084566 881 then
882 Bad_Body_Error
883 ("generic instantiation for $$ does not allow a body");
884
61989dbb 885 -- A library unit that is a renaming never allows a body
10084566 886
61989dbb 887 elsif Unit_Kind in N_Renaming_Declaration then
10084566 888 Bad_Body_Error
889 ("renaming declaration for $$ does not allow a body!");
890
891 -- Remaining cases are packages and generic packages. Here
892 -- we only do the test if there are no previous errors,
893 -- because if there are errors, they may lead us to
39a0c1d3 894 -- incorrectly believe that a package does not allow a
895 -- body when in fact it does.
10084566 896
897 elsif not Compilation_Errors then
61989dbb 898 if Unit_Kind = N_Package_Declaration then
10084566 899 Bad_Body_Error
900 ("package $$ does not allow a body!");
901
61989dbb 902 elsif Unit_Kind = N_Generic_Package_Declaration then
10084566 903 Bad_Body_Error
904 ("generic package $$ does not allow a body!");
905 end if;
906 end if;
907
908 end if;
909 end if;
910 end if;
911 end Check_Bad_Body;
912
ed195555 913 --------------------
914 -- Check_Rep_Info --
915 --------------------
916
917 procedure Check_Rep_Info is
918 begin
919 if List_Representation_Info /= 0
920 or else List_Representation_Info_Mechanisms
921 then
922 Set_Standard_Error;
923 Write_Eol;
924 Write_Str
925 ("cannot generate representation information, no code generated");
926 Write_Eol;
927 Write_Eol;
928 Set_Standard_Output;
929 end if;
930 end Check_Rep_Info;
931
7b8fa048 932 ----------------------------------------
933 -- Post_Compilation_Validation_Checks --
934 ----------------------------------------
935
936 procedure Post_Compilation_Validation_Checks is
937 begin
938 -- Validate alignment check warnings. In some cases we generate warnings
939 -- about possible alignment errors because we don't know the alignment
940 -- that will be chosen by the back end. This routine is in charge of
941 -- getting rid of those warnings if we can tell they are not needed.
942
943 Checks.Validate_Alignment_Check_Warnings;
944
76a6b7c7 945 -- Validate compile time warnings and errors (using the values for size
946 -- and alignment annotated by the backend where possible). We need to
947 -- unlock temporarily these tables to reanalyze their expression.
948
e0bb541c 949 Atree.Unlock;
950 Nlists.Unlock;
951 Sem.Unlock;
952 Sem_Ch13.Validate_Compile_Time_Warning_Errors;
953 Sem.Lock;
954 Nlists.Lock;
955 Atree.Lock;
76a6b7c7 956
7b8fa048 957 -- Validate unchecked conversions (using the values for size and
958 -- alignment annotated by the backend where possible).
959
960 Sem_Ch13.Validate_Unchecked_Conversions;
961
962 -- Validate address clauses (again using alignment values annotated
963 -- by the backend where possible).
964
965 Sem_Ch13.Validate_Address_Clauses;
966
dba38d2f 967 -- Validate independence pragmas (again using values annotated by the
968 -- back end for component layout where possible) but only for non-GCC
969 -- back ends, as this is done a priori for GCC back ends.
3a7fe2f3 970 -- ??? We use to test for AAMP_On_Target which is now gone, consider
971 --
972 -- if AAMP_On_Target then
973 -- Sem_Ch13.Validate_Independence;
974 -- end if;
7b8fa048 975 end Post_Compilation_Validation_Checks;
976
61989dbb 977 -- Local variables
978
979 Back_End_Mode : Back_End.Back_End_Mode_Type;
7dbb2cf6 980 Ecode : Exit_Code_Type;
61989dbb 981
982 Main_Unit_Kind : Node_Kind;
983 -- Kind of main compilation unit node
984
985 Main_Unit_Node : Node_Id;
986 -- Compilation unit node for main unit
987
10084566 988-- Start of processing for Gnat1drv
989
83cce46b 990begin
991 -- This inner block is set up to catch assertion errors and constraint
992 -- errors. Since the code for handling these errors can cause another
993 -- exception to be raised (namely Unrecoverable_Error), we need two
994 -- nested blocks, so that the outer one handles unrecoverable error.
995
996 begin
11b376d2 997 -- Initialize all packages. For the most part, these initialization
998 -- calls can be made in any order. Exceptions are as follows:
999
f15731c4 1000 -- Lib.Initialize need to be called before Scan_Compiler_Arguments,
10084566 1001 -- because it initializes a table filled by Scan_Compiler_Arguments.
f15731c4 1002
9dfe12ae 1003 Osint.Initialize;
1004 Fmap.Reset_Tables;
f15731c4 1005 Lib.Initialize;
9dfe12ae 1006 Lib.Xref.Initialize;
83cce46b 1007 Scan_Compiler_Arguments;
1008 Osint.Add_Default_Search_Dirs;
d10a989c 1009 Atree.Initialize;
9dfe12ae 1010 Nlists.Initialize;
83cce46b 1011 Sinput.Initialize;
83cce46b 1012 Sem.Initialize;
e00e091c 1013 Exp_CG.Initialize;
83cce46b 1014 Csets.Initialize;
1015 Uintp.Initialize;
1016 Urealp.Initialize;
1017 Errout.Initialize;
a9958373 1018 SCOs.Initialize;
83cce46b 1019 Snames.Initialize;
1020 Stringt.Initialize;
360b005f 1021 Ghost.Initialize;
83cce46b 1022 Inline.Initialize;
11b376d2 1023 Par_SCO.Initialize;
9dfe12ae 1024 Sem_Ch8.Initialize;
1025 Sem_Ch12.Initialize;
83cce46b 1026 Sem_Ch13.Initialize;
e7b2d6bc 1027 Sem_Elim.Initialize;
9dfe12ae 1028 Sem_Eval.Initialize;
1029 Sem_Type.Init_Interp_Tables;
1030
82b93248 1031 -- Capture compilation date and time
1032
1033 Opt.Compilation_Time := System.OS_Lib.Current_Time_String;
1034
3b514396 1035 -- Get the target parameters only when -gnats is not used, to avoid
1036 -- failing when there is no default runtime.
9dfe12ae 1037
3b514396 1038 if Operating_Mode /= Check_Syntax then
9dfe12ae 1039
3b514396 1040 -- Acquire target parameters from system.ads (package System source)
9dfe12ae 1041
3b514396 1042 Targparm_Acquire : declare
1043 use Sinput;
9dfe12ae 1044
3b514396 1045 S : Source_File_Index;
1046 N : File_Name_Type;
9dfe12ae 1047
3b514396 1048 begin
1049 Name_Buffer (1 .. 10) := "system.ads";
1050 Name_Len := 10;
1051 N := Name_Find;
1052 S := Load_Source_File (N);
9dfe12ae 1053
3b514396 1054 -- Failed to read system.ads, fatal error
83cce46b 1055
3b514396 1056 if S = No_Source_File then
1057 Write_Line
1058 ("fatal error, run-time library not installed correctly");
1059 Write_Line ("cannot locate file system.ads");
1060 raise Unrecoverable_Error;
f15731c4 1061
3b514396 1062 -- Read system.ads successfully, remember its source index
1063
1064 else
1065 System_Source_File_Index := S;
1066 end if;
f15731c4 1067
24c8d764 1068 -- Call to get target parameters. Note that the actual interface
6287ef56 1069 -- routines are in Tbuild. They can't be in this procedure because
1070 -- of accessibility issues.
24c8d764 1071
3b514396 1072 Targparm.Get_Target_Parameters
1073 (System_Text => Source_Text (S),
1074 Source_First => Source_First (S),
1075 Source_Last => Source_Last (S),
1076 Make_Id => Tbuild.Make_Id'Access,
1077 Make_SC => Tbuild.Make_SC'Access,
24c8d764 1078 Set_NOD => Tbuild.Set_NOD'Access,
1079 Set_NSA => Tbuild.Set_NSA'Access,
1080 Set_NUA => Tbuild.Set_NUA'Access,
1081 Set_NUP => Tbuild.Set_NUP'Access);
3b514396 1082
1083 -- Acquire configuration pragma information from Targparm
1084
1085 Restrict.Restrictions := Targparm.Restrictions_On_Target;
1086 end Targparm_Acquire;
1087 end if;
d10a989c 1088
1089 -- Perform various adjustments and settings of global switches
9dfe12ae 1090
749c967d 1091 Adjust_Global_Switches;
150564b2 1092
3a838102 1093 -- Output copyright notice if full list mode unless we have a list
3b514396 1094 -- file, in which case we defer this so that it is output in the file.
83cce46b 1095
10084566 1096 if (Verbose_Mode or else (Full_List and then Full_List_File_Name = null))
3b514396 1097
1098 -- Debug flag gnatd7 suppresses this copyright notice
1099
3a838102 1100 and then not Debug_Flag_7
83cce46b 1101 then
1102 Write_Eol;
1103 Write_Str ("GNAT ");
f15731c4 1104 Write_Str (Gnat_Version_String);
57335d69 1105 Write_Eol;
64427fe6 1106 Write_Str ("Copyright 1992-" & Current_Year
1107 & ", Free Software Foundation, Inc.");
f15731c4 1108 Write_Eol;
83cce46b 1109 end if;
1110
10084566 1111 -- Check we do not have more than one source file, this happens only in
1112 -- the case where the driver is called directly, it cannot happen when
1113 -- gnat1 is invoked from gcc in the normal case.
83cce46b 1114
1115 if Osint.Number_Of_Files /= 1 then
1116 Usage;
1117 Write_Eol;
1118 Osint.Fail ("you must provide one source file");
1119
1120 elsif Usage_Requested then
1121 Usage;
1122 end if;
1123
cba2ae82 1124 -- Generate target dependent output file if requested
1125
5462ac01 1126 if Target_Dependent_Info_Write_Name /= null then
cba2ae82 1127 Set_Targ.Write_Target_Dependent_Values;
1128 end if;
1129
1130 -- Call the front end
1131
83cce46b 1132 Original_Operating_Mode := Operating_Mode;
1133 Frontend;
83cce46b 1134
ca0e899c 1135 -- Exit with errors if the main source could not be parsed
afc48770 1136
10084566 1137 if Sinput.Main_Source_File = No_Source_File then
150564b2 1138 Errout.Finalize (Last_Call => True);
10084566 1139 Errout.Output_Messages;
1140 Exit_Program (E_Errors);
83cce46b 1141 end if;
1142
10084566 1143 Main_Unit_Node := Cunit (Main_Unit);
61989dbb 1144 Main_Unit_Kind := Nkind (Unit (Main_Unit_Node));
1145
1146 Check_Bad_Body (Main_Unit_Node, Main_Unit_Kind);
10084566 1147
43d776b7 1148 -- In CodePeer mode we always delete old SCIL files before regenerating
1149 -- new ones, in case of e.g. errors, and also to remove obsolete scilx
1150 -- files generated by CodePeer itself.
1151
1152 if CodePeer_Mode then
1153 Comperr.Delete_SCIL_Files;
1154 end if;
1155
a0a15971 1156 -- Ditto for old C files before regenerating new ones
1157
1158 if Generate_C_Code then
1159 Delete_C_File;
1160 Delete_H_File;
1161 end if;
1162
83cce46b 1163 -- Exit if compilation errors detected
1164
150564b2 1165 Errout.Finalize (Last_Call => False);
10084566 1166
83cce46b 1167 if Compilation_Errors then
1168 Treepr.Tree_Dump;
7b8fa048 1169 Post_Compilation_Validation_Checks;
10084566 1170 Errout.Output_Messages;
83cce46b 1171 Namet.Finalize;
1172
1173 -- Generate ALI file if specially requested
1174
1175 if Opt.Force_ALI_Tree_File then
1176 Write_ALI (Object => False);
1177 Tree_Gen;
1178 end if;
1179
150564b2 1180 Errout.Finalize (Last_Call => True);
83cce46b 1181 Exit_Program (E_Errors);
1182 end if;
1183
57304b2b 1184 -- Set Generate_Code on main unit and its spec. We do this even if are
1185 -- not generating code, since Lib-Writ uses this to determine which
1186 -- units get written in the ali file.
83cce46b 1187
1188 Set_Generate_Code (Main_Unit);
1189
64427fe6 1190 -- If we have a corresponding spec, and it comes from source or it is
1191 -- not a generated spec for a child subprogram body, then we need object
1192 -- code for the spec unit as well.
83cce46b 1193
1194 if Nkind (Unit (Main_Unit_Node)) in N_Unit_Body
1195 and then not Acts_As_Spec (Main_Unit_Node)
1196 then
035e891d 1197 if Nkind (Unit (Main_Unit_Node)) = N_Subprogram_Body
db317bdb 1198 and then not Comes_From_Source (Library_Unit (Main_Unit_Node))
1199 then
1200 null;
1201 else
1202 Set_Generate_Code
1203 (Get_Cunit_Unit_Number (Library_Unit (Main_Unit_Node)));
1204 end if;
83cce46b 1205 end if;
1206
83cce46b 1207 -- Case of no code required to be generated, exit indicating no error
1208
1209 if Original_Operating_Mode = Check_Syntax then
1210 Treepr.Tree_Dump;
150564b2 1211 Errout.Finalize (Last_Call => True);
10084566 1212 Errout.Output_Messages;
83cce46b 1213 Tree_Gen;
1214 Namet.Finalize;
10084566 1215 Check_Rep_Info;
8c9f4284 1216
1217 -- Use a goto instead of calling Exit_Program so that finalization
1218 -- occurs normally.
1219
1220 goto End_Of_Program;
83cce46b 1221
1222 elsif Original_Operating_Mode = Check_Semantics then
1223 Back_End_Mode := Declarations_Only;
1224
1225 -- All remaining cases are cases in which the user requested that code
64427fe6 1226 -- be generated (i.e. no -gnatc or -gnats switch was used). Check if we
1227 -- can in fact satisfy this request.
83cce46b 1228
57304b2b 1229 -- Cannot generate code if someone has turned off code generation for
1230 -- any reason at all. We will try to figure out a reason below.
83cce46b 1231
1232 elsif Operating_Mode /= Generate_Code then
1233 Back_End_Mode := Skip;
1234
57304b2b 1235 -- We can generate code for a subprogram body unless there were missing
1236 -- subunits. Note that we always generate code for all generic units (a
1237 -- change from some previous versions of GNAT).
83cce46b 1238
61989dbb 1239 elsif Main_Unit_Kind = N_Subprogram_Body
1240 and then not Subunits_Missing
1241 then
83cce46b 1242 Back_End_Mode := Generate_Object;
1243
9dfe12ae 1244 -- We can generate code for a package body unless there are subunits
1245 -- missing (note that we always generate code for generic units, which
1246 -- is a change from some earlier versions of GNAT).
83cce46b 1247
61989dbb 1248 elsif Main_Unit_Kind = N_Package_Body and then not Subunits_Missing then
83cce46b 1249 Back_End_Mode := Generate_Object;
1250
1251 -- We can generate code for a package declaration or a subprogram
1252 -- declaration only if it does not required a body.
1253
61989dbb 1254 elsif Nkind_In (Main_Unit_Kind, N_Package_Declaration,
1255 N_Subprogram_Declaration)
83cce46b 1256 and then
1257 (not Body_Required (Main_Unit_Node)
f74a102b 1258 or else Distribution_Stub_Mode = Generate_Caller_Stub_Body)
83cce46b 1259 then
1260 Back_End_Mode := Generate_Object;
1261
1262 -- We can generate code for a generic package declaration of a generic
9dfe12ae 1263 -- subprogram declaration only if does not require a body.
83cce46b 1264
61989dbb 1265 elsif Nkind_In (Main_Unit_Kind, N_Generic_Package_Declaration,
1266 N_Generic_Subprogram_Declaration)
83cce46b 1267 and then not Body_Required (Main_Unit_Node)
83cce46b 1268 then
1269 Back_End_Mode := Generate_Object;
1270
64427fe6 1271 -- Compilation units that are renamings do not require bodies, so we can
1272 -- generate code for them.
83cce46b 1273
61989dbb 1274 elsif Nkind_In (Main_Unit_Kind, N_Package_Renaming_Declaration,
1275 N_Subprogram_Renaming_Declaration)
83cce46b 1276 then
1277 Back_End_Mode := Generate_Object;
1278
1279 -- Compilation units that are generic renamings do not require bodies
9dfe12ae 1280 -- so we can generate code for them.
83cce46b 1281
61989dbb 1282 elsif Main_Unit_Kind in N_Generic_Renaming_Declaration then
83cce46b 1283 Back_End_Mode := Generate_Object;
1284
6f0d10f7 1285 -- It is not an error to analyze in CodePeer mode a spec which requires
1286 -- a body, in order to generate SCIL for this spec.
da3cad01 1287 -- Ditto for Generate_C_Code mode and generate a C header for a spec.
040277b1 1288
da3cad01 1289 elsif CodePeer_Mode or Generate_C_Code then
040277b1 1290 Back_End_Mode := Generate_Object;
1291
c39cce40 1292 -- It is not an error to analyze in GNATprove mode a spec which requires
1293 -- a body, when the body is not available. During frame condition
6f0d10f7 1294 -- generation, the corresponding ALI file is generated. During
13fa5c6a 1295 -- analysis, the spec is analyzed.
6f0d10f7 1296
c39cce40 1297 elsif GNATprove_Mode then
1298 Back_End_Mode := Declarations_Only;
6f0d10f7 1299
83cce46b 1300 -- In all other cases (specs which have bodies, generics, and bodies
1301 -- where subunits are missing), we cannot generate code and we generate
1302 -- a warning message. Note that generic instantiations are gone at this
1303 -- stage since they have been replaced by their instances.
1304
1305 else
1306 Back_End_Mode := Skip;
1307 end if;
1308
f38c8084 1309 -- At this stage Back_End_Mode is set to indicate if the backend should
1310 -- be called to generate code. If it is Skip, then code generation has
1311 -- been turned off, even though code was requested by the original
57304b2b 1312 -- command. This is not an error from the user point of view, but it is
1313 -- an error from the point of view of the gcc driver, so we must exit
1314 -- with an error status.
83cce46b 1315
57304b2b 1316 -- We generate an informative message (from the gcc point of view, it
1317 -- is an error message, but from the users point of view this is not an
1318 -- error, just a consequence of compiling something that cannot
1319 -- generate code).
83cce46b 1320
1321 if Back_End_Mode = Skip then
83cce46b 1322
61989dbb 1323 -- An ignored Ghost unit is rewritten into a null statement because
1324 -- it must not produce an ALI or object file. Do not emit any errors
1325 -- related to code generation because the unit does not exist.
83cce46b 1326
7dbb2cf6 1327 if Is_Ignored_Ghost_Unit (Main_Unit_Node) then
1328
1329 -- Exit the gnat driver with success, otherwise external builders
1330 -- such as gnatmake and gprbuild will treat the compilation of an
1331 -- ignored Ghost unit as a failure. Note that this will produce
1332 -- an empty object file for the unit.
1333
1334 Ecode := E_Success;
ebce244f 1335
61989dbb 1336 -- Otherwise the unit is missing a crucial piece that prevents code
1337 -- generation.
ebce244f 1338
61989dbb 1339 else
7dbb2cf6 1340 Ecode := E_No_Code;
1341
61989dbb 1342 Set_Standard_Error;
1343 Write_Str ("cannot generate code for file ");
1344 Write_Name (Unit_File_Name (Main_Unit));
83cce46b 1345
61989dbb 1346 if Subunits_Missing then
1347 Write_Str (" (missing subunits)");
1348 Write_Eol;
ebce244f 1349
61989dbb 1350 -- Force generation of ALI file, for backward compatibility
ebce244f 1351
61989dbb 1352 Opt.Force_ALI_Tree_File := True;
9dfe12ae 1353
61989dbb 1354 elsif Main_Unit_Kind = N_Subunit then
1355 Write_Str (" (subunit)");
1356 Write_Eol;
9dfe12ae 1357
78f327e2 1358 -- Do not generate an ALI file in this case, because it would
1359 -- become obsolete when the parent is compiled, and thus
1360 -- confuse tools such as gnatfind.
ebce244f 1361
61989dbb 1362 elsif Main_Unit_Kind = N_Subprogram_Declaration then
1363 Write_Str (" (subprogram spec)");
1364 Write_Eol;
ebce244f 1365
61989dbb 1366 -- Generic package body in GNAT implementation mode
83cce46b 1367
61989dbb 1368 elsif Main_Unit_Kind = N_Package_Body and then GNAT_Mode then
1369 Write_Str (" (predefined generic)");
1370 Write_Eol;
1371
1372 -- Force generation of ALI file, for backward compatibility
1373
1374 Opt.Force_ALI_Tree_File := True;
1375
1376 -- Only other case is a package spec
1377
1378 else
1379 Write_Str (" (package spec)");
1380 Write_Eol;
1381 end if;
9dfe12ae 1382 end if;
1383
c65510c1 1384 Set_Standard_Output;
83cce46b 1385
7b8fa048 1386 Post_Compilation_Validation_Checks;
150564b2 1387 Errout.Finalize (Last_Call => True);
10084566 1388 Errout.Output_Messages;
83cce46b 1389 Treepr.Tree_Dump;
1390 Tree_Gen;
ebce244f 1391
1392 -- Generate ALI file if specially requested, or for missing subunits,
1393 -- subunits or predefined generic.
1394
1395 if Opt.Force_ALI_Tree_File then
1396 Write_ALI (Object => False);
1397 end if;
1398
83cce46b 1399 Namet.Finalize;
10084566 1400 Check_Rep_Info;
83cce46b 1401
7dbb2cf6 1402 -- Exit the driver with an appropriate status indicator. This will
1403 -- generate an empty object file for ignored Ghost units, otherwise
1404 -- no object file will be generated.
83cce46b 1405
7dbb2cf6 1406 Exit_Program (Ecode);
83cce46b 1407 end if;
1408
69a5377d 1409 -- In -gnatc mode, we only do annotation if -gnatt or -gnatR is also set
1410 -- as indicated by Back_Annotate_Rep_Info being set to True.
83cce46b 1411
1412 -- We don't call for annotations on a subunit, because to process those
f9906591 1413 -- the back end requires that the parent(s) be properly compiled.
83cce46b 1414
750b3003 1415 -- Annotation is suppressed for targets where front-end layout is
1416 -- enabled, because the front end determines representations.
1417
f9906591 1418 -- The back end is not invoked in ASIS mode with GNSA because all type
1419 -- representation information will be provided by the GNSA back end, not
f74a102b 1420 -- gigi.
1421
83cce46b 1422 if Back_End_Mode = Declarations_Only
4098232e 1423 and then
1424 (not (Back_Annotate_Rep_Info or Generate_SCIL or GNATprove_Mode)
61989dbb 1425 or else Main_Unit_Kind = N_Subunit
f74a102b 1426 or else ASIS_GNSA_Mode)
83cce46b 1427 then
7b8fa048 1428 Post_Compilation_Validation_Checks;
150564b2 1429 Errout.Finalize (Last_Call => True);
10084566 1430 Errout.Output_Messages;
83cce46b 1431 Write_ALI (Object => False);
1432 Tree_Dump;
1433 Tree_Gen;
1434 Namet.Finalize;
10084566 1435 Check_Rep_Info;
83cce46b 1436 return;
1437 end if;
1438
57304b2b 1439 -- Ensure that we properly register a dependency on system.ads, since
1440 -- even if we do not semantically depend on this, Targparm has read
1441 -- system parameters from the system.ads file.
83cce46b 1442
1443 Lib.Writ.Ensure_System_Dependency;
1444
9dfe12ae 1445 -- Add dependencies, if any, on preprocessing data file and on
1446 -- preprocessing definition file(s).
1447
1448 Prepcomp.Add_Dependencies;
1449
c8a2d809 1450 -- In gnatprove mode we're writing the ALI much earlier than usual
1451 -- as flow analysis needs the file present in order to append its
1452 -- own globals to it.
1453
1454 if GNATprove_Mode then
1455
1456 -- Note: In GNATprove mode, an "object" file is always generated as
1457 -- the result of calling gnat1 or gnat2why, although this is not the
1458 -- same as the object file produced for compilation.
1459
1460 Write_ALI (Object => True);
1461 end if;
1462
a79fe0a3 1463 -- Some back ends (for instance Gigi) are known to rely on SCOs for code
1464 -- generation. Make sure they are available.
1465
1466 if Generate_SCO then
1467 Par_SCO.SCO_Record_Filtered;
1468 end if;
1469
83cce46b 1470 -- Back end needs to explicitly unlock tables it needs to touch
1471
1472 Atree.Lock;
1473 Elists.Lock;
1474 Fname.UF.Lock;
360b005f 1475 Ghost.Lock;
83cce46b 1476 Inline.Lock;
1477 Lib.Lock;
360b005f 1478 Namet.Lock;
83cce46b 1479 Nlists.Lock;
1480 Sem.Lock;
1481 Sinput.Lock;
83cce46b 1482 Stringt.Lock;
1483
9dfe12ae 1484 -- Here we call the back end to generate the output code
83cce46b 1485
10084566 1486 Generating_Code := True;
83cce46b 1487 Back_End.Call_Back_End (Back_End_Mode);
1488
57304b2b 1489 -- Once the backend is complete, we unlock the names table. This call
e9c863fc 1490 -- allows a few extra entries, needed for example for the file name
1491 -- for the library file output.
83cce46b 1492
1493 Namet.Unlock;
1494
e00e091c 1495 -- Generate the call-graph output of dispatching calls
1496
1497 Exp_CG.Generate_CG_Output;
1498
7b8fa048 1499 -- Perform post compilation validation checks
83f8f0a6 1500
7b8fa048 1501 Post_Compilation_Validation_Checks;
7717ea00 1502
57304b2b 1503 -- Now we complete output of errors, rep info and the tree info. These
1504 -- are delayed till now, since it is perfectly possible for gigi to
1505 -- generate errors, modify the tree (in particular by setting flags
1506 -- indicating that elaboration is required, and also to back annotate
9cdeb62a 1507 -- representation information for List_Rep_Info).
83cce46b 1508
150564b2 1509 Errout.Finalize (Last_Call => True);
10084566 1510 Errout.Output_Messages;
3a993333 1511 List_Rep_Info (Ttypes.Bytes_Big_Endian);
fc615d5c 1512 Inline.List_Inlining_Info;
83cce46b 1513
1514 -- Only write the library if the backend did not generate any error
1515 -- messages. Otherwise signal errors to the driver program so that
1516 -- there will be no attempt to generate an object file.
1517
1518 if Compilation_Errors then
1519 Treepr.Tree_Dump;
1520 Exit_Program (E_Errors);
1521 end if;
1522
c8a2d809 1523 if not GNATprove_Mode then
1524 Write_ALI (Object => (Back_End_Mode = Generate_Object));
1525 end if;
83cce46b 1526
8db090bd 1527 if not Compilation_Errors then
0cafb066 1528
8db090bd 1529 -- In case of ada backends, we need to make sure that the generated
0cafb066 1530 -- object file has a timestamp greater than the ALI file. We do this
1531 -- to make gnatmake happy when checking the ALI and obj timestamps,
1532 -- where it expects the object file being written after the ali file.
1533
8db090bd 1534 -- Gnatmake's assumption is true for gcc platforms where the gcc
1535 -- wrapper needs to call the assembler after calling gnat1, but is
1536 -- not true for ada backends, where the object files are created
1537 -- directly by gnat1 (so are created before the ali file).
0cafb066 1538
8db090bd 1539 Back_End.Gen_Or_Update_Object_File;
1540 end if;
1541
69a5377d 1542 -- Generate ASIS tree after writing the ALI file, since in ASIS mode,
1543 -- Write_ALI may in fact result in further tree decoration from the
1544 -- original tree file. Note that we dump the tree just before generating
1545 -- it, so that the dump will exactly reflect what is written out.
83cce46b 1546
1547 Treepr.Tree_Dump;
1548 Tree_Gen;
1549
1550 -- Finalize name table and we are all done
1551
1552 Namet.Finalize;
1553
1554 exception
1555 -- Handle fatal internal compiler errors
1556
10084566 1557 when Rtsfind.RE_Not_Available =>
1558 Comperr.Compiler_Abort ("RE_Not_Available");
1559
83cce46b 1560 when System.Assertions.Assert_Failure =>
1561 Comperr.Compiler_Abort ("Assert_Failure");
1562
1563 when Constraint_Error =>
1564 Comperr.Compiler_Abort ("Constraint_Error");
1565
1566 when Program_Error =>
1567 Comperr.Compiler_Abort ("Program_Error");
1568
61989dbb 1569 -- Assume this is a bug. If it is real, the message will in any case
1570 -- say Storage_Error, giving a strong hint.
83cce46b 1571
61989dbb 1572 when Storage_Error =>
83cce46b 1573 Comperr.Compiler_Abort ("Storage_Error");
f0a120e1 1574
1575 when Unrecoverable_Error =>
1576 raise;
1577
1578 when others =>
1579 Comperr.Compiler_Abort ("exception");
83cce46b 1580 end;
1581
8c9f4284 1582 <<End_Of_Program>>
1583 null;
1584
61989dbb 1585-- The outer exception handler handles an unrecoverable error
83cce46b 1586
1587exception
1588 when Unrecoverable_Error =>
150564b2 1589 Errout.Finalize (Last_Call => True);
10084566 1590 Errout.Output_Messages;
83cce46b 1591
1592 Set_Standard_Error;
1593 Write_Str ("compilation abandoned");
1594 Write_Eol;
1595
1596 Set_Standard_Output;
1597 Source_Dump;
1598 Tree_Dump;
1599 Exit_Program (E_Errors);
1600
1601end Gnat1drv;