1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2002 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Atree; use Atree;
30 with Back_End; use Back_End;
32 with Csets; use Csets;
33 with Debug; use Debug;
35 with Errout; use Errout;
36 with Fname; use Fname;
37 with Fname.UF; use Fname.UF;
39 with Gnatvsn; use Gnatvsn;
43 with Lib.Writ; use Lib.Writ;
44 with Namet; use Namet;
47 with Osint; use Osint;
48 with Output; use Output;
49 with Repinfo; use Repinfo;
50 with Restrict; use Restrict;
53 with Sinfo; use Sinfo;
54 with Sinput.L; use Sinput.L;
56 with Sprint; use Sprint;
60 with Treepr; use Treepr;
62 with Types; use Types;
64 with Uname; use Uname;
68 with System.Assertions;
71 Main_Unit_Node : Node_Id;
72 -- Compilation unit node for main unit
74 Main_Unit_Entity : Node_Id;
75 -- Compilation unit entity for main unit
77 Main_Kind : Node_Kind;
78 -- Kind of main compilation unit node.
80 Original_Operating_Mode : Operating_Mode_Type;
81 -- Save operating type specified by options
83 Back_End_Mode : Back_End.Back_End_Mode_Type;
84 -- Record back end mode
87 -- This inner block is set up to catch assertion errors and constraint
88 -- errors. Since the code for handling these errors can cause another
89 -- exception to be raised (namely Unrecoverable_Error), we need two
90 -- nested blocks, so that the outer one handles unrecoverable error.
93 -- Lib.Initialize need to be called before Scan_Compiler_Arguments,
94 -- because it initialize a table that is filled by
95 -- Scan_Compiler_Arguments.
98 Scan_Compiler_Arguments;
99 Osint.Add_Default_Search_Dirs;
113 -- Acquire target parameters and perform required setup
115 Targparm.Get_Target_Parameters;
117 if Targparm.High_Integrity_Mode_On_Target then
118 Set_No_Run_Time_Mode;
121 -- Output copyright notice if full list mode
123 if (Verbose_Mode or Full_List)
124 and then (not Debug_Flag_7)
129 if Targparm.High_Integrity_Mode_On_Target then
130 Write_Str ("Pro High Integrity ");
133 Write_Str (Gnat_Version_String);
134 Write_Str (" Copyright 1992-2002 Free Software Foundation, Inc.");
138 -- Before we do anything else, adjust certain global values for
139 -- debug switches which modify their normal natural settings.
142 Ttypes.Bytes_Big_Endian := not Ttypes.Bytes_Big_Endian;
146 Targparm.OpenVMS_On_Target := True;
147 Hostparm.OpenVMS := True;
150 if Debug_Flag_FF then
151 Targparm.Frontend_Layout_On_Target := True;
154 -- We take the default exception mechanism into account
156 if Targparm.ZCX_By_Default_On_Target then
157 if Targparm.GCC_ZCX_Support_On_Target then
158 Exception_Mechanism := GCC_ZCX;
160 Exception_Mechanism := Front_End_ZCX;
164 -- We take the command line exception mechanism into account
166 if Opt.Zero_Cost_Exceptions_Set then
167 if Opt.Zero_Cost_Exceptions_Val = False then
168 Exception_Mechanism := Setjmp_Longjmp;
170 elsif Targparm.GCC_ZCX_Support_On_Target then
171 Exception_Mechanism := GCC_ZCX;
173 elsif Targparm.Front_End_ZCX_Support_On_Target
174 or else Debug_Flag_XX
176 Exception_Mechanism := Front_End_ZCX;
180 ("Zero Cost Exceptions not supported on this target");
184 -- Set proper status for overflow checks. We turn on overflow checks
185 -- if -gnatp was not specified, and either -gnato is set or the back
186 -- end takes care of overflow checks. Otherwise we suppress overflow
187 -- checks by default (since front end checks are expensive).
189 if not Opt.Suppress_Checks
190 and then (Opt.Enable_Overflow_Checks
192 (Targparm.Backend_Divide_Checks_On_Target
194 Targparm.Backend_Overflow_Checks_On_Target))
196 Suppress_Options.Overflow_Checks := False;
198 Suppress_Options.Overflow_Checks := True;
201 -- Check we have exactly one source file, this happens only in
202 -- the case where the driver is called directly, it cannot happen
203 -- when gnat1 is invoked from gcc in the normal case.
205 if Osint.Number_Of_Files /= 1 then
208 Osint.Fail ("you must provide one source file");
210 elsif Usage_Requested then
214 Original_Operating_Mode := Operating_Mode;
216 Main_Unit_Node := Cunit (Main_Unit);
217 Main_Unit_Entity := Cunit_Entity (Main_Unit);
218 Main_Kind := Nkind (Unit (Main_Unit_Node));
220 -- Check for suspicious or incorrect body present if we are doing
221 -- semantic checking. We omit this check in syntax only mode, because
222 -- in that case we do not know if we need a body or not.
224 if Operating_Mode /= Check_Syntax
226 ((Main_Kind = N_Package_Declaration
227 and then not Body_Required (Main_Unit_Node))
228 or else (Main_Kind = N_Generic_Package_Declaration
229 and then not Body_Required (Main_Unit_Node))
230 or else Main_Kind = N_Package_Renaming_Declaration
231 or else Main_Kind = N_Subprogram_Renaming_Declaration
232 or else Nkind (Original_Node (Unit (Main_Unit_Node)))
233 in N_Generic_Instantiation)
236 Sname : Unit_Name_Type := Unit_Name (Main_Unit);
237 Src_Ind : Source_File_Index;
238 Fname : File_Name_Type;
240 procedure Bad_Body (Msg : String);
241 -- Issue message for bad body found
243 procedure Bad_Body (Msg : String) is
245 Error_Msg_N (Msg, Main_Unit_Node);
246 Error_Msg_Name_1 := Fname;
248 ("remove incorrect body in file{!", Main_Unit_Node);
252 Sname := Unit_Name (Main_Unit);
254 -- If we do not already have a body name, then get the body
255 -- name (but how can we have a body name here ???)
257 if not Is_Body_Name (Sname) then
258 Sname := Get_Body_Name (Sname);
261 Fname := Get_File_Name (Sname, Subunit => False);
262 Src_Ind := Load_Source_File (Fname);
264 -- Case where body is present and it is not a subunit. Exclude
265 -- the subunit case, because it has nothing to do with the
266 -- package we are compiling. It is illegal for a child unit
267 -- and a subunit with the same expanded name (RM 10.2(9)) to
268 -- appear together in a partition, but there is nothing to
269 -- stop a compilation environment from having both, and the
270 -- test here simply allows that. If there is an attempt to
271 -- include both in a partition, this is diagnosed at bind time.
272 -- In Ada 83 mode this is not a warning case.
274 if Src_Ind /= No_Source_File
275 and then not Source_File_Is_Subunit (Src_Ind)
277 Error_Msg_Name_1 := Sname;
279 -- Ada 83 case of a package body being ignored. This is not
280 -- an error as far as the Ada 83 RM is concerned, but it is
281 -- almost certainly not what is wanted so output a warning.
282 -- Give this message only if there were no errors, since
283 -- otherwise it may be incorrect (we may have misinterpreted
284 -- a junk spec as not needing a body when it really does).
286 if Main_Kind = N_Package_Declaration
288 and then Operating_Mode = Generate_Code
289 and then Distribution_Stub_Mode /= Generate_Caller_Stub_Body
290 and then not Compilation_Errors
293 ("package % does not require a body?!", Main_Unit_Node);
294 Error_Msg_Name_1 := Fname;
296 ("body in file{?! will be ignored", Main_Unit_Node);
298 -- Ada 95 cases of a body file present when no body is
299 -- permitted. This we consider to be an error.
302 -- For generic instantiations, we never allow a body
304 if Nkind (Original_Node (Unit (Main_Unit_Node)))
305 in N_Generic_Instantiation
308 ("generic instantiation for % does not allow a body");
310 -- A library unit that is a renaming never allows a body
312 elsif Main_Kind in N_Renaming_Declaration then
314 ("renaming declaration for % does not allow a body!");
316 -- Remaining cases are packages and generic packages.
317 -- Here we only do the test if there are no previous
318 -- errors, because if there are errors, they may lead
319 -- us to incorrectly believe that a package does not
320 -- allow a body when in fact it does.
322 elsif not Compilation_Errors then
323 if Main_Kind = N_Package_Declaration then
324 Bad_Body ("package % does not allow a body!");
326 elsif Main_Kind = N_Generic_Package_Declaration then
327 Bad_Body ("generic package % does not allow a body!");
336 -- Exit if compilation errors detected
338 if Compilation_Errors then
340 Sem_Ch13.Validate_Unchecked_Conversions;
344 -- Generate ALI file if specially requested
346 if Opt.Force_ALI_Tree_File then
347 Write_ALI (Object => False);
351 Exit_Program (E_Errors);
354 -- Set Generate_Code on main unit and its spec. We do this even if
355 -- are not generating code, since Lib-Writ uses this to determine
356 -- which units get written in the ali file.
358 Set_Generate_Code (Main_Unit);
360 -- If we have a corresponding spec, then we need object
361 -- code for the spec unit as well
363 if Nkind (Unit (Main_Unit_Node)) in N_Unit_Body
364 and then not Acts_As_Spec (Main_Unit_Node)
367 (Get_Cunit_Unit_Number (Library_Unit (Main_Unit_Node)));
370 -- Case of no code required to be generated, exit indicating no error
372 if Original_Operating_Mode = Check_Syntax then
377 Exit_Program (E_Success);
379 elsif Original_Operating_Mode = Check_Semantics then
380 Back_End_Mode := Declarations_Only;
382 -- All remaining cases are cases in which the user requested that code
383 -- be generated (i.e. no -gnatc or -gnats switch was used). Check if
384 -- we can in fact satisfy this request.
386 -- Cannot generate code if someone has turned off code generation
387 -- for any reason at all. We will try to figure out a reason below.
389 elsif Operating_Mode /= Generate_Code then
390 Back_End_Mode := Skip;
392 -- We can generate code for a subprogram body unless its corresponding
393 -- subprogram spec is a generic delaration. Note that the check for
394 -- No (Library_Unit) here is a defensive check that should not be
395 -- necessary, since the Library_Unit field should be set properly.
397 elsif Main_Kind = N_Subprogram_Body
398 and then not Subunits_Missing
399 and then (No (Library_Unit (Main_Unit_Node))
400 or else Nkind (Unit (Library_Unit (Main_Unit_Node))) /=
401 N_Generic_Subprogram_Declaration
402 or else Generic_Separately_Compiled (Main_Unit_Entity))
404 Back_End_Mode := Generate_Object;
406 -- We can generate code for a package body unless its corresponding
407 -- package spec is a generic declaration. As described above, the
408 -- check for No (LIbrary_Unit) is a defensive check.
410 elsif Main_Kind = N_Package_Body
411 and then not Subunits_Missing
412 and then (No (Library_Unit (Main_Unit_Node))
413 or else Nkind (Unit (Library_Unit (Main_Unit_Node))) /=
414 N_Generic_Package_Declaration
415 or else Generic_Separately_Compiled (Main_Unit_Entity))
418 Back_End_Mode := Generate_Object;
420 -- We can generate code for a package declaration or a subprogram
421 -- declaration only if it does not required a body.
423 elsif (Main_Kind = N_Package_Declaration
425 Main_Kind = N_Subprogram_Declaration)
427 (not Body_Required (Main_Unit_Node)
429 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
431 Back_End_Mode := Generate_Object;
433 -- We can generate code for a generic package declaration of a generic
434 -- subprogram declaration only if does not require a body, and if it
435 -- is a generic that is separately compiled.
437 elsif (Main_Kind = N_Generic_Package_Declaration
439 Main_Kind = N_Generic_Subprogram_Declaration)
440 and then not Body_Required (Main_Unit_Node)
441 and then Generic_Separately_Compiled (Main_Unit_Entity)
443 Back_End_Mode := Generate_Object;
445 -- Compilation units that are renamings do not require bodies,
446 -- so we can generate code for them.
448 elsif Main_Kind = N_Package_Renaming_Declaration
449 or else Main_Kind = N_Subprogram_Renaming_Declaration
451 Back_End_Mode := Generate_Object;
453 -- Compilation units that are generic renamings do not require bodies
454 -- so we can generate code for them in the separately compiled case
456 elsif Main_Kind in N_Generic_Renaming_Declaration
457 and then Generic_Separately_Compiled (Main_Unit_Entity)
459 Back_End_Mode := Generate_Object;
461 -- In all other cases (specs which have bodies, generics, and bodies
462 -- where subunits are missing), we cannot generate code and we generate
463 -- a warning message. Note that generic instantiations are gone at this
464 -- stage since they have been replaced by their instances.
467 Back_End_Mode := Skip;
470 -- At this stage Call_Back_End is set to indicate if the backend
471 -- should be called to generate code. If it is not set, then code
472 -- generation has been turned off, even though code was requested
473 -- by the original command. This is not an error from the user
474 -- point of view, but it is an error from the point of view of
475 -- the gcc driver, so we must exit with an error status.
477 -- We generate an informative message (from the gcc point of view,
478 -- it is an error message, but from the users point of view this
479 -- is not an error, just a consequence of compiling something that
480 -- cannot generate code).
482 if Back_End_Mode = Skip then
483 Write_Str ("No code generated for ");
485 Write_Name (Unit_File_Name (Main_Unit));
487 if Subunits_Missing then
488 Write_Str (" (missing subunits)");
490 elsif Main_Kind = N_Subunit then
491 Write_Str (" (subunit)");
493 elsif Main_Kind = N_Package_Body
494 or else Main_Kind = N_Subprogram_Body
496 Write_Str (" (generic unit)");
498 elsif Main_Kind = N_Subprogram_Declaration then
499 Write_Str (" (subprogram spec)");
501 -- Only other case is a package spec
504 Write_Str (" (package spec)");
509 Sem_Ch13.Validate_Unchecked_Conversions;
513 Write_ALI (Object => False);
516 -- Exit program with error indication, to kill object file
518 Exit_Program (E_No_Code);
521 -- In -gnatc mode, we only do annotation if -gnatt or -gnatR is also
522 -- set as indicated by Back_Annotate_Rep_Info being set to True.
524 -- We don't call for annotations on a subunit, because to process those
525 -- the back-end requires that the parent(s) be properly compiled.
527 -- Annotation is also suppressed in the case of compiling for
528 -- the Java VM, since representations are largely symbolic there.
530 if Back_End_Mode = Declarations_Only
531 and then (not (Back_Annotate_Rep_Info or Debug_Flag_AA)
532 or else Main_Kind = N_Subunit
533 or else Hostparm.Java_VM)
535 Sem_Ch13.Validate_Unchecked_Conversions;
537 Write_ALI (Object => False);
544 -- Ensure that we properly register a dependency on system.ads,
545 -- since even if we do not semantically depend on this, Targparm
546 -- has read system parameters from the system.ads file.
548 Lib.Writ.Ensure_System_Dependency;
550 -- Back end needs to explicitly unlock tables it needs to touch
563 -- There are cases where the back end emits warnings, e.g. on objects
564 -- that are too large and will cause Storage_Error. If such a warning
565 -- appears in a generic context, then it is always appropriately
566 -- placed on the instance rather than the template, since gigi only
567 -- deals with generated code in instances (in particular the warning
568 -- for oversize objects clearly belongs on the instance).
570 Warn_On_Instance := True;
572 -- Here we call the backend to generate the output code
574 Back_End.Call_Back_End (Back_End_Mode);
576 -- Once the backend is complete, we unlock the names table. This
577 -- call allows a few extra entries, needed for example for the file
578 -- name for the library file output.
582 -- Validate unchecked conversions (using the values for size
583 -- and alignment annotated by the backend where possible).
585 Sem_Ch13.Validate_Unchecked_Conversions;
587 -- Now we complete output of errors, rep info and the tree info.
588 -- These are delayed till now, since it is perfectly possible for
589 -- gigi to generate errors, modify the tree (in particular by setting
590 -- flags indicating that elaboration is required, and also to back
591 -- annotate representation information for List_Rep_Info.
595 if Opt.List_Representation_Info /= 0 or else Debug_Flag_AA then
599 -- Only write the library if the backend did not generate any error
600 -- messages. Otherwise signal errors to the driver program so that
601 -- there will be no attempt to generate an object file.
603 if Compilation_Errors then
605 Exit_Program (E_Errors);
608 Write_ALI (Object => (Back_End_Mode = Generate_Object));
610 -- Generate the ASIS tree after writing the ALI file, since in
611 -- ASIS mode, Write_ALI may in fact result in further tree
612 -- decoration from the original tree file. Note that we dump
613 -- the tree just before generating it, so that the dump will
614 -- exactly reflect what is written out.
619 -- Finalize name table and we are all done
624 -- Handle fatal internal compiler errors
626 when System.Assertions.Assert_Failure =>
627 Comperr.Compiler_Abort ("Assert_Failure");
629 when Constraint_Error =>
630 Comperr.Compiler_Abort ("Constraint_Error");
632 when Program_Error =>
633 Comperr.Compiler_Abort ("Program_Error");
635 when Storage_Error =>
637 -- Assume this is a bug. If it is real, the message will in
638 -- any case say Storage_Error, giving a strong hint!
640 Comperr.Compiler_Abort ("Storage_Error");
643 -- The outer exception handles an unrecoverable error
646 when Unrecoverable_Error =>
650 Write_Str ("compilation abandoned");
656 Exit_Program (E_Errors);