Add hppa-openbsd target
[official-gcc.git] / gcc / ada / gnat_ug_vms.texi
blob4fbf8b8ef945149afa07040203f57ef1ca40c44e
1 \input texinfo   @c -*-texinfo-*-
2 @c %**start of header
4 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
5 @c                                                                            o
6 @c                            GNAT DOCUMENTATION                              o
7 @c                                                                            o
8 @c                              G N A T _ U G                                 o
9 @c                                                                            o
10 @c          Copyright (C) 1992-2002 Ada Core Technologies, Inc.               o
11 @c                                                                            o
12 @c  GNAT is free software;  you can  redistribute it  and/or modify it under  o
13 @c  terms of the  GNU General Public License as published  by the Free Soft-  o
14 @c  ware  Foundation;  either version 2,  or (at your option) any later ver-  o
15 @c  sion.  GNAT is distributed in the hope that it will be useful, but WITH-  o
16 @c  OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY  o
17 @c  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License  o
18 @c  for  more details.  You should have  received  a copy of the GNU General  o
19 @c  Public License  distributed with GNAT;  see file COPYING.  If not, write  o
20 @c  to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston,  o
21 @c  MA 02111-1307, USA.                                                       o
22 @c                                                                            o
23 @c  GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com).    o
24 @c                                                                            o
25 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
27 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
29 @c                           GNAT_UG Style Guide
31 @c  1. Always put a @noindent on the line before the first paragraph
32 @c     after any of these commands:
34 @c          @chapter
35 @c          @section
36 @c          @subsection
37 @c          @subsubsection
38 @c          @subsubsubsection
40 @c          @end smallexample
41 @c          @end itemize
42 @c          @end enumerate
44 @c  2. DO NOT use @example. Use @smallexample instead.
46 @c  3. Each @chapter, @section, @subsection, @subsubsection, etc.
47 @c     command must be preceded by two empty lines
49 @c  4. The @item command must be on a line of its own if it is in an
50 @c     @itemize or @enumerate command.
52 @c  5. When talking about ALI files use "ALI" (all uppercase), not "Ali"
53 @c     or "ali".
55 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
57 @setfilename gnat_ug_vms.info
58 @settitle GNAT User's Guide for OpenVMS Alpha
63 @include gcc-common.texi
65 @setchapternewpage odd
66 @syncodeindex fn cp
67 @c %**end of header
69 @titlepage
71 @title GNAT User's Guide
72 @center @titlefont{for OpenVMS Alpha}
77 @subtitle GNAT, The GNU Ada 95 Compiler
78 @subtitle GNAT Version for GCC @value{version-GCC}
80 @author Ada Core Technologies, Inc.
82 @page
83 @vskip 0pt plus 1filll
85 Copyright @copyright{} 1995-2002, Free Software Foundation
87 Permission is granted to copy, distribute and/or modify this document
88 under the terms of the GNU Free Documentation License, Version 1.1
89 or any later version published by the Free Software Foundation;
90 with the Invariant Sections being ``GNU Free Documentation License'', with the
91 Front-Cover Texts being
92 ``GNAT User's Guide for OpenVMS Alpha'',
93 and with no Back-Cover Texts.
94 A copy of the license is included in the section entitled ``GNU
95 Free Documentation License''.
97 @end titlepage
99 @ifnottex
100 @node Top, About This Guide, (dir), (dir)
101 @top GNAT User's Guide
103 GNAT User's Guide for OpenVMS Alpha
108 GNAT, The GNU Ada 95 Compiler
110 GNAT Version for GCC @value{version-GCC}
112 Ada Core Technologies, Inc.
114 Copyright @copyright{} 1995-2002, Free Software Foundation
116 Permission is granted to copy, distribute and/or modify this document
117 under the terms of the GNU Free Documentation License, Version 1.1
118 or any later version published by the Free Software Foundation;
119 with the Invariant Sections being ``GNU Free Documentation License'', with the
120 Front-Cover Texts being
121 ``GNAT User's Guide for OpenVMS Alpha'',
122 and with no Back-Cover Texts.
123 A copy of the license is included in the section entitled ``GNU
124 Free Documentation License''.
126 @menu
127 * About This Guide::
128 * Getting Started with GNAT::
129 * The GNAT Compilation Model::
130 * Compiling Using GNAT COMPILE::
131 * Binding Using GNAT BIND::
132 * Linking Using GNAT LINK::
133 * The GNAT Make Program GNAT MAKE::
134 * Renaming Files Using GNAT CHOP::
135 * Configuration Pragmas::
136 * Handling Arbitrary File Naming Conventions Using gnatname::
137 * GNAT Project Manager::
138 * Elaboration Order Handling in GNAT::
139 * The Cross-Referencing Tools GNAT XREF and GNAT FIND::
140 * File Name Krunching Using GNAT KRUNCH::
141 * Preprocessing Using GNAT PREPROCESS::
142 * The GNAT Run-Time Library Builder GNAT LIBRARY::
143 * The GNAT Library Browser GNAT LIST::
144 * Finding Memory Problems with GNAT Debug Pool::
145 * Creating Sample Bodies Using GNAT STUB::
146 * Reducing the Size of Ada Executables with GNAT ELIM::
147 * Other Utility Programs::
148 * Compatibility with DEC Ada::
149 * Running and Debugging Ada Programs::
150 * Inline Assembler::
151 * Performance Considerations::
152 * GNU Free Documentation License::
153 * Index::
155  --- The Detailed Node Listing ---
157 About This Guide
159 * What This Guide Contains::
160 * What You Should Know before Reading This Guide::
161 * Related Information::
162 * Conventions::
165 Getting Started with GNAT
167 * Running GNAT::
168 * Running a Simple Ada Program::
169 * Running a Program with Multiple Units::
170 * Using the GNAT MAKE Utility::
171 * Editing with EMACS::
173 The GNAT Compilation Model
175 * Source Representation::
176 * Foreign Language Representation::
177 * File Naming Rules::
178 * Using Other File Names::
179 * Alternative File Naming Schemes::
180 * Generating Object Files::
181 * Source Dependencies::
182 * The Ada Library Information Files::
183 * Binding an Ada Program::
184 * Mixed Language Programming::
185 * Building Mixed Ada & C++ Programs::
186 * Comparison between GNAT and C/C++ Compilation Models::
187 * Comparison between GNAT and Conventional Ada Library Models::
189 Foreign Language Representation
191 * Latin-1::
192 * Other 8-Bit Codes::
193 * Wide Character Encodings::
195 Compiling Ada Programs With GNAT COMPILE
197 * Compiling Programs::
198 * Qualifiers for GNAT COMPILE::
199 * Search Paths and the Run-Time Library (RTL)::
200 * Order of Compilation Issues::
201 * Examples::
203 Qualifiers for GNAT COMPILE
205 * Output and Error Message Control::
206 * Debugging and Assertion Control::
207 * Run-Time Checks::
208 * Stack Overflow Checking::
209 * Run-Time Control::
210 * Validity Checking::
211 * Style Checking::
212 * Using GNAT COMPILE for Syntax Checking::
213 * Using GNAT COMPILE for Semantic Checking::
214 * Compiling Ada 83 Programs::
215 * Character Set Control::
216 * File Naming Control::
217 * Subprogram Inlining Control::
218 * Auxiliary Output Control::
219 * Debugging Control::
220 * Units to Sources Mapping Files::
222 Binding Ada Programs With GNAT BIND
224 * Running GNAT BIND::
225 * Generating the Binder Program in C::
226 * Consistency-Checking Modes::
227 * Binder Error Message Control::
228 * Elaboration Control::
229 * Output Control::
230 * Binding with Non-Ada Main Programs::
231 * Binding Programs with No Main Subprogram::
232 * Summary of Binder Qualifiers::
233 * Command-Line Access::
234 * Search Paths for GNAT BIND::
235 * Examples of GNAT BIND Usage::
237 Linking Using GNAT LINK
239 * Running GNAT LINK::
240 * Qualifiers for GNAT LINK::
241 * Setting Stack Size from GNAT LINK::
242 * Setting Heap Size from GNAT LINK::
244 The GNAT Make Program GNAT MAKE
246 * Running GNAT MAKE::
247 * Qualifiers for GNAT MAKE::
248 * Mode Qualifiers for GNAT MAKE::
249 * Notes on the Command Line::
250 * How GNAT MAKE Works::
251 * Examples of GNAT MAKE Usage::
253 Renaming Files Using GNAT CHOP
255 * Handling Files with Multiple Units::
256 * Operating GNAT CHOP in Compilation Mode::
257 * Command Line for GNAT CHOP::
258 * Qualifiers for GNAT CHOP::
259 * Examples of GNAT CHOP Usage::
261 Configuration Pragmas
263 * Handling of Configuration Pragmas::
264 * The Configuration Pragmas Files::
266 Handling Arbitrary File Naming Conventions Using gnatname
268 * Arbitrary File Naming Conventions::
269 * Running gnatname::
270 * Qualifiers for gnatname::
271 * Examples of gnatname Usage::
273 GNAT Project Manager
275 * Introduction::
276 * Examples of Project Files::
277 * Project File Syntax::
278 * Objects and Sources in Project Files::
279 * Importing Projects::
280 * Project Extension::
281 * External References in Project Files::
282 * Packages in Project Files::
283 * Variables from Imported Projects::
284 * Naming Schemes::
285 * Library Projects::
286 * Qualifiers Related to Project Files::
287 * Tools Supporting Project Files::
288 * An Extended Example::
289 * Project File Complete Syntax::
291 Elaboration Order Handling in GNAT
293 * Elaboration Code in Ada 95::
294 * Checking the Elaboration Order in Ada 95::
295 * Controlling the Elaboration Order in Ada 95::
296 * Controlling Elaboration in GNAT - Internal Calls::
297 * Controlling Elaboration in GNAT - External Calls::
298 * Default Behavior in GNAT - Ensuring Safety::
299 * Elaboration Issues for Library Tasks::
300 * Mixing Elaboration Models::
301 * What to Do If the Default Elaboration Behavior Fails::
302 * Elaboration for Access-to-Subprogram Values::
303 * Summary of Procedures for Elaboration Control::
304 * Other Elaboration Order Considerations::
306 The Cross-Referencing Tools GNAT XREF and GNAT FIND
308 * GNAT XREF Qualifiers::
309 * GNAT FIND Qualifiers::
310 * Project Files for GNAT XREF and GNAT FIND::
311 * Regular Expressions in GNAT FIND and GNAT XREF::
312 * Examples of GNAT XREF Usage::
313 * Examples of GNAT FIND Usage::
315 File Name Krunching Using GNAT KRUNCH
317 * About GNAT KRUNCH::
318 * Using GNAT KRUNCH::
319 * Krunching Method::
320 * Examples of GNAT KRUNCH Usage::
322 Preprocessing Using GNAT PREPROCESS
324 * Using GNAT PREPROCESS::
325 * Qualifiers for GNAT PREPROCESS::
326 * Form of Definitions File::
327 * Form of Input Text for GNAT PREPROCESS::
329 The GNAT Run-Time Library Builder GNAT LIBRARY
331 * Running GNAT LIBRARY::
332 * Qualifiers for GNAT LIBRARY::
333 * Examples of GNAT LIBRARY Usage::
335 The GNAT Library Browser GNAT LIST
337 * Running GNAT LIST::
338 * Qualifiers for GNAT LIST::
339 * Examples of GNAT LIST Usage::
342 Finding Memory Problems with GNAT Debug Pool
344 Creating Sample Bodies Using GNAT STUB
346 * Running GNAT STUB::
347 * Qualifiers for GNAT STUB::
349 Reducing the Size of Ada Executables with GNAT ELIM
351 * About GNAT ELIM::
352 * Eliminate Pragma::
353 * Tree Files::
354 * Preparing Tree and Bind Files for GNAT ELIM::
355 * Running GNAT ELIM::
356 * Correcting the List of Eliminate Pragmas::
357 * Making Your Executables Smaller::
358 * Summary of the GNAT ELIM Usage Cycle::
360 Other Utility Programs
362 * Using Other Utility Programs with GNAT::
363 * The GNAT STANDARD Utility Program::
364 * The External Symbol Naming Scheme of GNAT::
365 * Ada Mode for Glide::
366 * Converting Ada Files to html with gnathtml::
367 * LSE::
369 Compatibility with DEC Ada
371 * Ada 95 Compatibility::
372 * Differences in the Definition of Package System::
373 * Language-Related Features::
374 * The Package STANDARD::
375 * The Package SYSTEM::
376 * Tasking and Task-Related Features::
377 * Implementation of Tasks in DEC Ada for OpenVMS Alpha Systems::
378 * Pragmas and Pragma-Related Features::
379 * Library of Predefined Units::
380 * Bindings::
381 * Main Program Definition::
382 * Implementation-Defined Attributes::
383 * Compiler and Run-Time Interfacing::
384 * Program Compilation and Library Management::
385 * Input-Output::
386 * Implementation Limits::
387 * Tools::
389 Language-Related Features
391 * Integer Types and Representations::
392 * Floating-Point Types and Representations::
393 * Pragmas Float_Representation and Long_Float::
394 * Fixed-Point Types and Representations::
395 * Record and Array Component Alignment::
396 * Address Clauses::
397 * Other Representation Clauses::
399 Implementation of Tasks in DEC Ada for OpenVMS Alpha Systems
401 * Assigning Task IDs::
402 * Task IDs and Delays::
403 * Task-Related Pragmas::
404 * Scheduling and Task Priority::
405 * The Task Stack::
406 * External Interrupts::
408 Pragmas and Pragma-Related Features
410 * Restrictions on the Pragma INLINE::
411 * Restrictions on the Pragma INTERFACE::
412 * Restrictions on the Pragma SYSTEM_NAME::
414 Library of Predefined Units
416 * Changes to DECLIB::
418 Bindings
420 * Shared Libraries and Options Files::
421 * Interfaces to C::
423 Running and Debugging Ada Programs
425 * The GNAT Debugger GDB::
426 * Running GDB::
427 * Introduction to GDB Commands::
428 * Using Ada Expressions::
429 * Calling User-Defined Subprograms::
430 * Using the Next Command in a Function::
431 * Ada Exceptions::
432 * Ada Tasks::
433 * Debugging Generic Units::
434 * GNAT Abnormal Termination or Failure to Terminate::
435 * Naming Conventions for GNAT Source Files::
436 * Getting Internal Debugging Information::
437 * Stack Traceback::
439 Inline Assembler
441 * Basic Assembler Syntax::
442 * A Simple Example of Inline Assembler::
443 * Output Variables in Inline Assembler::
444 * Input Variables in Inline Assembler::
445 * Inlining Inline Assembler Code::
446 * Other Asm Functionality::
447 * A Complete Example::
451 Performance Considerations
453 * Controlling Run-Time Checks::
454 * Optimization Levels::
455 * Debugging Optimized Code::
456 * Inlining of Subprograms::
457 * Coverage Analysis::
459 * Index::
460 @end menu
461 @end ifnottex
463 @node About This Guide
464 @unnumbered About This Guide
466 @noindent
467 This guide describes the use of of GNAT, a full language compiler for the Ada
468 95 programming language, implemented on DIGITAL OpenVMS Alpha Systems.
469 It describes the features of the compiler and tools, and details
470 how to use them to build Ada 95 applications.
472 @menu
473 * What This Guide Contains::
474 * What You Should Know before Reading This Guide::
475 * Related Information::
476 * Conventions::
477 @end menu
479 @node What This Guide Contains
480 @unnumberedsec What This Guide Contains
482 @noindent
483 This guide contains the following chapters:
484 @itemize @bullet
485 @item
486 @ref{Getting Started with GNAT}, describes how to get started compiling
487 and running Ada programs with the GNAT Ada programming environment.
488 @item
489 @ref{The GNAT Compilation Model}, describes the compilation model used
490 by GNAT.
491 @item
492 @ref{Compiling Using GNAT COMPILE}, describes how to compile
493 Ada programs with @code{GNAT COMPILE}, the Ada compiler.
494 @item
495 @ref{Binding Using GNAT BIND}, describes how to
496 perform binding of Ada programs with @code{GNAT BIND}, the GNAT binding
497 utility.
498 @item
499 @ref{Linking Using GNAT LINK},
500 describes @code{GNAT LINK}, a
501 program that provides for linking using the GNAT run-time library to
502 construct a program. @code{GNAT LINK} can also incorporate foreign language
503 object units into the executable.
504 @item
505 @ref{The GNAT Make Program GNAT MAKE}, describes @code{GNAT MAKE}, a
506 utility that automatically determines the set of sources
507 needed by an Ada compilation unit, and executes the necessary compilations
508 binding and link.
509 @item
510 @ref{Renaming Files Using GNAT CHOP}, describes
511 @code{GNAT CHOP}, a utility that allows you to preprocess a file that
512 contains Ada source code, and split it into one or more new files, one
513 for each compilation unit.
514 @item
515 @ref{Configuration Pragmas}, describes the configuration pragmas handled by GNAT.
516 @item
517 @ref{Handling Arbitrary File Naming Conventions Using gnatname}, shows how to override
518 the default GNAT file naming conventions, either for an individual unit or globally.
519 @item
520 @ref{GNAT Project Manager}, describes how to use project files to organize large projects.
521 @item
522 @ref{Elaboration Order Handling in GNAT}, describes how GNAT helps you deal with
523 elaboration order issues.
524 @item
525 @ref{The Cross-Referencing Tools GNAT XREF and GNAT FIND}, discusses
526 @code{GNAT XREF} and @code{GNAT FIND}, two tools that provide an easy
527 way to navigate through sources.
528 @item
529 @ref{File Name Krunching Using GNAT KRUNCH}, describes the @code{GNAT KRUNCH}
530 file name krunching utility, used to handle shortened
531 file names on operating systems with a limit on the length of names.
532 @item
533 @ref{Preprocessing Using GNAT PREPROCESS}, describes @code{GNAT PREPROCESS}, a
534 preprocessor utility that allows a single source file to be used to
535 generate multiple or parameterized source files, by means of macro
536 substitution.
537 @item
538 @ref{The GNAT Library Browser GNAT LIST}, describes @code{GNAT LIST}, a
539 utility that displays information about compiled units, including dependences
540 on the corresponding sources files, and consistency of compilations.
541 @item
542 @ref{Finding Memory Problems with GNAT Debug Pool}, describes how to
543 use the GNAT-specific Debug Pool in order to detect as early as possible
544 the use of incorrect memory references.
546 @item
547 @ref{Creating Sample Bodies Using GNAT STUB}, discusses @code{GNAT STUB},
548 a utility that generates empty but compilable bodies for library units.
550 @item
551 @ref{Reducing the Size of Ada Executables with GNAT ELIM}, describes
552 @code{GNAT ELIM}, a tool which detects unused subprograms and helps
553 the compiler to create a smaller executable for the program.
555 @item
556 @ref{Other Utility Programs}, discusses several other GNAT utilities,
557 including @code{GNAT STANDARD}.
559 @item
560 @ref{Running and Debugging Ada Programs}, describes how to run and debug
561 Ada programs.
563 @item
564 @ref{Inline Assembler}, shows how to use the inline assembly facility in an Ada program.
567 @item
568 @ref{Performance Considerations}, reviews the trade offs between using
569 defaults or options in program development.
570 @item
571 @ref{Compatibility with DEC Ada}, details the compatibility of GNAT with
572 DEC Ada 83 for OpenVMS Alpha.
573 @end itemize
575 @node What You Should Know before Reading This Guide
576 @unnumberedsec What You Should Know before Reading This Guide
578 @cindex Ada 95 Language Reference Manual
579 @noindent
580 This user's guide assumes that you are familiar with Ada 95 language, as
581 described in the International Standard ANSI/ISO/IEC-8652:1995, Jan
582 1995.
584 @node Related Information
585 @unnumberedsec Related Information
587 @noindent
588 For further information about related tools, refer to the following
589 documents:
591 @itemize @bullet
592 @item
593 @cite{GNAT Reference Manual}, which contains all reference
594 material for the GNAT implementation of Ada 95.
596 @item
597 @cite{Ada 95 Language Reference Manual}, which contains all reference
598 material for the Ada 95 programming language.
600 @item
601 @cite{Debugging with GDB}
602 , located in the GNU:[DOCS] directory,
603 contains all details on the use of the GNU source-level debugger.
605 @item
606 @cite{GNU EMACS Manual}
607 , located in the GNU:[DOCS] directory if the EMACS kit is installed,
608 contains full information on the extensible editor and programming
609 environment EMACS.
611 @end itemize
613 @node Conventions
614 @unnumberedsec Conventions
615 @cindex Conventions
616 @cindex Typographical conventions
618 @noindent
619 Following are examples of the typographical and graphic conventions used
620 in this guide:
622 @itemize @bullet
623 @item
624 @code{Functions}, @code{utility program names}, @code{standard names},
625 and @code{classes}.
627 @item
628 @samp{Option flags}
630 @item
631 @file{File Names}, @file{button names}, and @file{field names}.
633 @item
634 @var{Variables}.
636 @item
637 @emph{Emphasis}.
639 @item
640 [optional information or parameters]
642 @item
643 Examples are described by text
644 @smallexample
645 and then shown this way.
646 @end smallexample
647 @end itemize
649 @noindent
650 Commands that are entered by the user are preceded in this manual by the
651 characters @w{"@code{$ }"} (dollar sign followed by space). If your system
652 uses this sequence as a prompt, then the commands will appear exactly as
653 you see them in the manual. If your system uses some other prompt, then
654 the command will appear with the @code{$} replaced by whatever prompt
655 character you are using.
658 @node Getting Started with GNAT
659 @chapter Getting Started with GNAT
661 @noindent
662 This chapter describes some simple ways of using GNAT to build
663 executable Ada programs.
665 @menu
666 * Running GNAT::
667 * Running a Simple Ada Program::
669 * Running a Program with Multiple Units::
671 * Using the GNAT MAKE Utility::
672 * Editing with EMACS::
673 @end menu
675 @node Running GNAT
676 @section Running GNAT
678 @noindent
679 Three steps are needed to create an executable file from an Ada source
680 file:
682 @enumerate
683 @item
684 The source file(s) must be compiled.
685 @item
686 The file(s) must be bound using the GNAT binder.
687 @item
688 All appropriate object files must be linked to produce an executable.
689 @end enumerate
691 @noindent
692 All three steps are most commonly handled by using the @code{GNAT MAKE}
693 utility program that, given the name of the main program, automatically
694 performs the necessary compilation, binding and linking steps.
696 @node Running a Simple Ada Program
697 @section Running a Simple Ada Program
699 @noindent
700 Any text editor may be used to prepare an Ada program. If @code{Glide} is
701 used, the optional Ada mode may be helpful in laying out the program. The
702 program text is a normal text file. We will suppose in our initial
703 example that you have used your editor to prepare the following
704 standard format text file:
706 @smallexample
707 @group
708 @cartouche
709 @b{with} Ada.Text_IO; @b{use} Ada.Text_IO;
710 @b{procedure} Hello @b{is}
711 @b{begin}
712    Put_Line ("Hello WORLD!");
713 @b{end} Hello;
714 @end cartouche
715 @end group
716 @end smallexample
718 @noindent
719 This file should be named @file{HELLO.ADB}.
720 With the normal default file naming conventions, GNAT requires
721 that each file
722 contain a single compilation unit whose file name is the
723 unit name,
724 with periods replaced by hyphens; the
725 extension is @file{ads} for a
726 spec and @file{adb} for a body.
727 You can override this default file naming convention by use of the
728 special pragma @code{Source_File_Name} (@pxref{Using Other File Names}).
729 Alternatively, if you want to rename your files according to this default
730 convention, which is probably more convenient if you will be using GNAT
731 for all your compilations, then the @code{GNAT CHOP} utility
732 can be used to generate correctly-named source files
733 (@pxref{Renaming Files Using GNAT CHOP}).
735 You can compile the program using the following command (@code{$} is used
736 as the command prompt in the examples in this document):
738 @smallexample
739 $ GNAT COMPILE HELLO.ADB
740 @end smallexample
743 @noindent
744 @code{GNAT COMPILE} is the command used to run the compiler. This compiler is
745 capable of compiling programs in several languages, including Ada 95 and
746 C. It assumes that you have given it an Ada program if the file extension is
747 either @file{.ADS} or @file{.ADB}, and it will then call the GNAT compiler to compile
748 the specified file.
751 This compile command generates a file
752 @file{HELLO.OBJ}, which is the object
753 file corresponding to your Ada program. It also generates an "Ada Library Information" file
754 @file{HELLO.ALI},
755 which contains additional information used to check
756 that an Ada program is consistent.
757 To build an executable file,
758 use @code{GNAT BIND} to bind the program
759 and @code{GNAT LINK} to link it. The
760 argument to both @code{GNAT BIND} and @code{GNAT LINK} is the name of the
761 @file{ali} file, but the default extension of @file{.ALI} can
762 be omitted. This means that in the most common case, the argument
763 is simply the name of the main program:
765 @smallexample
766 $ GNAT BIND hello
767 $ GNAT LINK hello
768 @end smallexample
771 @noindent
772 A simpler method of carrying out these steps is to use
773 @command{GNAT MAKE},
774 a master program that invokes all the required
775 compilation, binding and linking tools in the correct order. In particular,
776 @command{GNAT MAKE} automatically recompiles any sources that have been modified
777 since they were last compiled, or sources that depend
778 on such modified sources, so that "version skew" is avoided.
779 @cindex Version skew (avoided by @command{GNAT MAKE})
781 @smallexample
782 $ GNAT MAKE HELLO.ADB
783 @end smallexample
786 @noindent
787 The result is an executable program called @file{hello}, which can be
788 run by entering:
790 @c The following should be removed (BMB 2001-01-23)
791 @c @smallexample
792 @c $ $ RUN HELLO
793 @c @end smallexample
795 @smallexample
796 $ hello
797 @end smallexample
799 @noindent
800 assuming that the current directory is on the search path for executable programs.
802 @noindent
803 and, if all has gone well, you will see
805 @smallexample
806 Hello WORLD!
807 @end smallexample
809 @noindent
810 appear in response to this command.
815 @node Running a Program with Multiple Units
816 @section Running a Program with Multiple Units
818 @noindent
819 Consider a slightly more complicated example that has three files: a
820 main program, and the spec and body of a package:
822 @smallexample
823 @cartouche
824 @group
825 @b{package} Greetings @b{is}
826    @b{procedure} Hello;
827    @b{procedure} Goodbye;
828 @b{end} Greetings;
830 @b{with} Ada.Text_IO; @b{use} Ada.Text_IO;
831 @b{package} @b{body} Greetings @b{is}
832    @b{procedure} Hello @b{is}
833    @b{begin}
834       Put_Line ("Hello WORLD!");
835    @b{end} Hello;
837    @b{procedure} Goodbye @b{is}
838    @b{begin}
839       Put_Line ("Goodbye WORLD!");
840    @b{end} Goodbye;
841 @b{end} Greetings;
842 @end group
844 @group
845 @b{with} Greetings;
846 @b{procedure} Gmain @b{is}
847 @b{begin}
848    Greetings.Hello;
849    Greetings.Goodbye;
850 @b{end} Gmain;
851 @end group
852 @end cartouche
853 @end smallexample
855 @noindent
856 Following the one-unit-per-file rule, place this program in the
857 following three separate files:
859 @table @file
860 @item GREETINGS.ADS
861 spec of package @code{Greetings}
863 @item GREETINGS.ADB
864 body of package @code{Greetings}
866 @item GMAIN.ADB
867 body of main program
868 @end table
870 @noindent
871 To build an executable version of
872 this program, we could use four separate steps to compile, bind, and link
873 the program, as follows:
875 @smallexample
876 $ GNAT COMPILE GMAIN.ADB
877 $ GNAT COMPILE GREETINGS.ADB
878 $ GNAT BIND gmain
879 $ GNAT LINK gmain
880 @end smallexample
883 @noindent
884 Note that there is no required order of compilation when using GNAT.
885 In particular it is perfectly fine to compile the main program first.
886 Also, it is not necessary to compile package specs in the case where
887 there is an accompanying body; you only need to compile the body. If you want
888 to submit these files to the compiler for semantic checking and not code generation,
889 then use the
890 @option{/NOLOAD} qualifier:
892 @smallexample
893    $ GNAT COMPILE GREETINGS.ADS /NOLOAD
894 @end smallexample
897 @noindent
898 Although the compilation can be done in separate steps as in the
899 above example, in practice it is almost always more convenient
900 to use the @code{GNAT MAKE} tool. All you need to know in this case
901 is the name of the main program's source file. The effect of the above four
902 commands can be achieved with a single one:
904 @smallexample
905 $ GNAT MAKE GMAIN.ADB
906 @end smallexample
909 @noindent
910 In the next section we discuss the advantages of using @code{GNAT MAKE} in
911 more detail.
913 @node Using the GNAT MAKE Utility
914 @section Using the @command{GNAT MAKE} Utility
916 @noindent
917 If you work on a program by compiling single components at a time using
918 @code{GNAT COMPILE}, you typically keep track of the units you modify. In order to
919 build a consistent system, you compile not only these units, but also any
920 units that depend on the units you have modified.
921 For example, in the preceding case,
922 if you edit @file{GMAIN.ADB}, you only need to recompile that file. But if
923 you edit @file{GREETINGS.ADS}, you must recompile both
924 @file{GREETINGS.ADB} and @file{GMAIN.ADB}, because both files contain
925 units that depend on @file{GREETINGS.ADS}.
927 @code{GNAT BIND} will warn you if you forget one of these compilation
928 steps, so that it is impossible to generate an inconsistent program as a
929 result of forgetting to do a compilation. Nevertheless it is tedious and
930 error-prone to keep track of dependencies among units.
931 One approach to handle the dependency-bookkeeping is to use a
932 makefile. However, makefiles present maintenance problems of their own:
933 if the dependencies change as you change the program, you must make
934 sure that the makefile is kept up-to-date manually, which is also an
935 error-prone process.
937 The @code{GNAT MAKE} utility takes care of these details automatically.
938 Invoke it using either one of the following forms:
940 @smallexample
941 $ GNAT MAKE GMAIN.ADB
942 $ GNAT MAKE GMAIN
943 @end smallexample
946 @noindent
947 The argument is the name of the file containing the main program;
948 you may omit the extension. @code{GNAT MAKE}
949 examines the environment, automatically recompiles any files that need
950 recompiling, and binds and links the resulting set of object files,
951 generating the executable file, @file{GMAIN.EXE}.
952 In a large program, it
953 can be extremely helpful to use @code{GNAT MAKE}, because working out by hand
954 what needs to be recompiled can be difficult.
956 Note that @code{GNAT MAKE}
957 takes into account all the Ada 95 rules that
958 establish dependencies among units. These include dependencies that result
959 from inlining subprogram bodies, and from
960 generic instantiation. Unlike some other
961 Ada make tools, @code{GNAT MAKE} does not rely on the dependencies that were
962 found by the compiler on a previous compilation, which may possibly
963 be wrong when sources change. @code{GNAT MAKE} determines the exact set of
964 dependencies from scratch each time it is run.
966 @node Editing with EMACS
967 @section Editing with EMACS
968 @cindex EMACS
970 @noindent
971 EMACS is an extensible self-documenting text editor that is available in a
972 separate VMSINSTAL kit.
974 Invoke EMACS by typing "EMACS" at the command prompt. To get started,
975 click on the EMACS Help menu and run the EMACS Tutorial.
976 In a character cell terminal, EMACS help is invoked with "Ctrl-h" (also written
977 as "C-h"), and the tutorial by "C-h t".
979 Documentation on EMACS and other tools is available in EMACS under the
980 pull-down menu button: Help - Info. After selecting Info, use the middle
981 mouse button to select a topic (e.g. EMACS).
983 In a character cell terminal, do "C-h i" to invoke info, and then "m"
984 (stands for menu) followed by the menu item desired, as in "m EMACS", to get
985 to the EMACS manual.
986 Help on EMACS is also available by typing "HELP EMACS" at the DCL command
987 prompt.
989 The tutorial is highly recommended in order to learn the intricacies of EMACS,
990 which is sufficiently extensible to provide for a complete programming
991 environment and shell for the sophisticated user.
994 @node The GNAT Compilation Model
995 @chapter The GNAT Compilation Model
996 @cindex GNAT compilation model
997 @cindex Compilation model
999 @menu
1000 * Source Representation::
1001 * Foreign Language Representation::
1002 * File Naming Rules::
1003 * Using Other File Names::
1004 * Alternative File Naming Schemes::
1005 * Generating Object Files::
1006 * Source Dependencies::
1007 * The Ada Library Information Files::
1008 * Binding an Ada Program::
1009 * Mixed Language Programming::
1010 * Building Mixed Ada & C++ Programs::
1011 * Comparison between GNAT and C/C++ Compilation Models::
1012 * Comparison between GNAT and Conventional Ada Library Models::
1013 @end menu
1015 @noindent
1016 This chapter describes the compilation model used by GNAT. Although
1017 similar to that used by other languages, such as C and C++, this model
1018 is substantially different from the traditional Ada compilation models,
1019 which are based on a library. The model is initially described without
1020 reference to the library-based model. If you have not previously used an
1021 Ada compiler, you need only read the first part of this chapter. The
1022 last section describes and discusses the differences between the GNAT
1023 model and the traditional Ada compiler models. If you have used other
1024 Ada compilers, this section will help you to understand those
1025 differences, and the advantages of the GNAT model.
1027 @node Source Representation
1028 @section Source Representation
1029 @cindex Latin-1
1031 @noindent
1032 Ada source programs are represented in standard text files, using
1033 Latin-1 coding. Latin-1 is an 8-bit code that includes the familiar
1034 7-bit ASCII set, plus additional characters used for
1035 representing foreign languages (@pxref{Foreign Language Representation}
1036 for support of non-USA character sets). The format effector characters
1037 are represented using their standard ASCII encodings, as follows:
1039 @table @code
1040 @item VT
1041 @findex VT
1042 Vertical tab, @code{16#0B#}
1044 @item HT
1045 @findex HT
1046 Horizontal tab, @code{16#09#}
1048 @item CR
1049 @findex CR
1050 Carriage return, @code{16#0D#}
1052 @item LF
1053 @findex LF
1054 Line feed, @code{16#0A#}
1056 @item FF
1057 @findex FF
1058 Form feed, @code{16#0C#}
1059 @end table
1061 @noindent
1062 Source files are in standard text file format. In addition, GNAT will
1063 recognize a wide variety of stream formats, in which the end of physical
1064 physical lines is marked by any of the following sequences:
1065 @code{LF}, @code{CR}, @code{CR-LF}, or @code{LF-CR}. This is useful
1066 in accommodating files that are imported from other operating systems.
1068 @cindex End of source file
1069 @cindex Source file, end
1070 @findex SUB
1071 The end of a source file is normally represented by the physical end of
1072 file. However, the control character @code{16#1A#} (@code{SUB}) is also
1073 recognized as signalling the end of the source file. Again, this is
1074 provided for compatibility with other operating systems where this
1075 code is used to represent the end of file.
1077 Each file contains a single Ada compilation unit, including any pragmas
1078 associated with the unit. For example, this means you must place a
1079 package declaration (a package @dfn{spec}) and the corresponding body in
1080 separate files. An Ada @dfn{compilation} (which is a sequence of
1081 compilation units) is represented using a sequence of files. Similarly,
1082 you will place each subunit or child unit in a separate file.
1084 @node Foreign Language Representation
1085 @section Foreign Language Representation
1087 @noindent
1088 GNAT supports the standard character sets defined in Ada 95 as well as
1089 several other non-standard character sets for use in localized versions
1090 of the compiler (@pxref{Character Set Control}).
1091 @menu
1092 * Latin-1::
1093 * Other 8-Bit Codes::
1094 * Wide Character Encodings::
1095 @end menu
1097 @node Latin-1
1098 @subsection Latin-1
1099 @cindex Latin-1
1101 @noindent
1102 The basic character set is Latin-1. This character set is defined by ISO
1103 standard 8859, part 1. The lower half (character codes @code{16#00#}
1104 ... @code{16#7F#)} is identical to standard ASCII coding, but the upper half is
1105 used to represent additional characters. These include extended letters
1106 used by European languages, such as French accents, the vowels with umlauts
1107 used in German, and the extra letter A-ring used in Swedish.
1109 @findex Ada.Characters.Latin_1
1110 For a complete list of Latin-1 codes and their encodings, see the source
1111 file of library unit @code{Ada.Characters.Latin_1} in file
1112 @file{A-CHLAT1.ADS}.
1113 You may use any of these extended characters freely in character or
1114 string literals. In addition, the extended characters that represent
1115 letters can be used in identifiers.
1117 @node Other 8-Bit Codes
1118 @subsection Other 8-Bit Codes
1120 @noindent
1121 GNAT also supports several other 8-bit coding schemes:
1123 @table @asis
1124 @cindex Latin-2
1125 @item Latin-2
1126 Latin-2 letters allowed in identifiers, with uppercase and lowercase
1127 equivalence.
1129 @item Latin-3
1130 @cindex Latin-3
1131 Latin-3 letters allowed in identifiers, with uppercase and lowercase
1132 equivalence.
1134 @item Latin-4
1135 @cindex Latin-4
1136 Latin-4 letters allowed in identifiers, with uppercase and lowercase
1137 equivalence.
1139 @item Latin-5
1140 @cindex Latin-5
1141 @cindex Cyrillic
1142 Latin-4 letters (Cyrillic) allowed in identifiers, with uppercase and lowercase
1143 equivalence.
1145 @item IBM PC (code page 437)
1146 @cindex code page 437
1147 This code page is the normal default for PCs in the U.S. It corresponds
1148 to the original IBM PC character set. This set has some, but not all, of
1149 the extended Latin-1 letters, but these letters do not have the same
1150 encoding as Latin-1. In this mode, these letters are allowed in
1151 identifiers with uppercase and lowercase equivalence.
1153 @item IBM PC (code page 850)
1154 @cindex code page 850
1155 This code page is a modification of 437 extended to include all the
1156 Latin-1 letters, but still not with the usual Latin-1 encoding. In this
1157 mode, all these letters are allowed in identifiers with uppercase and
1158 lowercase equivalence.
1160 @item Full Upper 8-bit
1161 Any character in the range 80-FF allowed in identifiers, and all are
1162 considered distinct. In other words, there are no uppercase and lowercase
1163 equivalences in this range. This is useful in conjunction with
1164 certain encoding schemes used for some foreign character sets (e.g.
1165 the typical method of representing Chinese characters on the PC).
1167 @item No Upper-Half
1168 No upper-half characters in the range 80-FF are allowed in identifiers.
1169 This gives Ada 83 compatibility for identifier names.
1170 @end table
1172 @noindent
1173 For precise data on the encodings permitted, and the uppercase and lowercase
1174 equivalences that are recognized, see the file @file{CSETS.ADB} in
1175 the GNAT compiler sources. You will need to obtain a full source release
1176 of GNAT to obtain this file.
1178 @node Wide Character Encodings
1179 @subsection Wide Character Encodings
1181 @noindent
1182 GNAT allows wide character codes to appear in character and string
1183 literals, and also optionally in identifiers, by means of the following
1184 possible encoding schemes:
1186 @table @asis
1188 @item Hex Coding
1189 In this encoding, a wide character is represented by the following five
1190 character sequence:
1192 @smallexample
1193 ESC a b c d
1194 @end smallexample
1196 @noindent
1197 Where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1198 characters (using uppercase letters) of the wide character code. For
1199 example, ESC A345 is used to represent the wide character with code
1200 @code{16#A345#}.
1201 This scheme is compatible with use of the full Wide_Character set.
1203 @item Upper-Half Coding
1204 @cindex Upper-Half Coding
1205 The wide character with encoding @code{16#abcd#} where the upper bit is on (in
1206 other words, "a" is in the range 8-F) is represented as two bytes,
1207 @code{16#ab#} and @code{16#cd#}. The second byte cannot be a format control
1208 character, but is not required to be in the upper half. This method can
1209 be also used for shift-JIS or EUC, where the internal coding matches the
1210 external coding.
1212 @item Shift JIS Coding
1213 @cindex Shift JIS Coding
1214 A wide character is represented by a two-character sequence,
1215 @code{16#ab#} and
1216 @code{16#cd#}, with the restrictions described for upper-half encoding as
1217 described above. The internal character code is the corresponding JIS
1218 character according to the standard algorithm for Shift-JIS
1219 conversion. Only characters defined in the JIS code set table can be
1220 used with this encoding method.
1222 @item EUC Coding
1223 @cindex EUC Coding
1224 A wide character is represented by a two-character sequence
1225 @code{16#ab#} and
1226 @code{16#cd#}, with both characters being in the upper half. The internal
1227 character code is the corresponding JIS character according to the EUC
1228 encoding algorithm. Only characters defined in the JIS code set table
1229 can be used with this encoding method.
1231 @item UTF-8 Coding
1232 A wide character is represented using
1233 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
1234 10646-1/Am.2. Depending on the character value, the representation
1235 is a one, two, or three byte sequence:
1236 @smallexample
1237 @iftex
1238 @leftskip=.7cm
1239 @end iftex
1240 16#0000#-16#007f#: 2#0xxxxxxx#
1241 16#0080#-16#07ff#: 2#110xxxxx# 2#10xxxxxx#
1242 16#0800#-16#ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx#
1244 @end smallexample
1246 @noindent
1247 where the xxx bits correspond to the left-padded bits of the
1248 16-bit character value. Note that all lower half ASCII characters
1249 are represented as ASCII bytes and all upper half characters and
1250 other wide characters are represented as sequences of upper-half
1251 (The full UTF-8 scheme allows for encoding 31-bit characters as
1252 6-byte sequences, but in this implementation, all UTF-8 sequences
1253 of four or more bytes length will be treated as illegal).
1254 @item Brackets Coding
1255 In this encoding, a wide character is represented by the following eight
1256 character sequence:
1258 @smallexample
1259 [ " a b c d " ]
1260 @end smallexample
1262 @noindent
1263 Where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1264 characters (using uppercase letters) of the wide character code. For
1265 example, ["A345"] is used to represent the wide character with code
1266 @code{16#A345#}. It is also possible (though not required) to use the
1267 Brackets coding for upper half characters. For example, the code
1268 @code{16#A3#} can be represented as @code{["A3"]}.
1270 This scheme is compatible with use of the full Wide_Character set,
1271 and is also the method used for wide character encoding in the standard
1272 ACVC (Ada Compiler Validation Capability) test suite distributions.
1274 @end table
1276 @noindent
1277 Note: Some of these coding schemes do not permit the full use of the
1278 Ada 95 character set. For example, neither Shift JIS, nor EUC allow the
1279 use of the upper half of the Latin-1 set.
1281 @node File Naming Rules
1282 @section File Naming Rules
1284 @noindent
1285 The default file name is determined by the name of the unit that the
1286 file contains. The name is formed by taking the full expanded name of
1287 the unit and replacing the separating dots with hyphens and using
1288 uppercase for all letters.
1290 An exception arises if the file name generated by the above rules starts
1291 with one of the characters
1292 A,G,I, or S,
1293 and the second character is a
1294 minus. In this case, the character dollar sign is used in place
1295 of the minus. The reason for this special rule is to avoid clashes with
1296 the standard names for child units of the packages System, Ada,
1297 Interfaces, and GNAT, which use the prefixes
1298 S- A- I- and G-
1299 respectively.
1301 The file extension is @file{.ADS} for a spec and
1302 @file{.ADB} for a body. The following list shows some
1303 examples of these rules.
1305 @table @file
1306 @item MAIN.ADS
1307 Main (spec)
1308 @item MAIN.ADB
1309 Main (body)
1310 @item ARITH_FUNCTIONS.ADS
1311 Arith_Functions (package spec)
1312 @item ARITH_FUNCTIONS.ADB
1313 Arith_Functions (package body)
1314 @item FUNC-SPEC.ADS
1315 Func.Spec (child package spec)
1316 @item FUNC-SPEC.ADB
1317 Func.Spec (child package body)
1318 @item MAIN-SUB.ADB
1319 Sub (subunit of Main)
1320 @item A$BAD.ADB
1321 A.Bad (child package body)
1322 @end table
1324 @noindent
1325 Following these rules can result in excessively long
1326 file names if corresponding
1327 unit names are long (for example, if child units or subunits are
1328 heavily nested). An option is available to shorten such long file names
1329 (called file name "krunching"). This may be particularly useful when
1330 programs being developed with GNAT are to be used on operating systems
1331 with limited file name lengths. @xref{Using GNAT KRUNCH}.
1333 Of course, no file shortening algorithm can guarantee uniqueness over
1334 all possible unit names; if file name krunching is used, it is your
1335 responsibility to ensure no name clashes occur. Alternatively you
1336 can specify the exact file names that you want used, as described
1337 in the next section. Finally, if your Ada programs are migrating from a
1338 compiler with a different naming convention, you can use the GNAT CHOP
1339 utility to produce source files that follow the GNAT naming conventions.
1340 (For details @pxref{Renaming Files Using GNAT CHOP}.)
1342 @node Using Other File Names
1343 @section Using Other File Names
1344 @cindex File names
1346 @noindent
1347 In the previous section, we have described the default rules used by
1348 GNAT to determine the file name in which a given unit resides. It is
1349 often convenient to follow these default rules, and if you follow them,
1350 the compiler knows without being explicitly told where to find all
1351 the files it needs.
1353 However, in some cases, particularly when a program is imported from
1354 another Ada compiler environment, it may be more convenient for the
1355 programmer to specify which file names contain which units. GNAT allows
1356 arbitrary file names to be used by means of the Source_File_Name pragma.
1357 The form of this pragma is as shown in the following examples:
1358 @cindex Source_File_Name pragma
1360 @smallexample
1361 @group
1362 @cartouche
1363 @b{pragma} Source_File_Name (My_Utilities.Stacks,
1364   Spec_File_Name => "MYUTILST_A.ADA");
1365 @b{pragma} Source_File_name (My_Utilities.Stacks,
1366   Body_File_Name => "MYUTILST.ADA");
1367 @end cartouche
1368 @end group
1369 @end smallexample
1371 @noindent
1372 As shown in this example, the first argument for the pragma is the unit
1373 name (in this example a child unit). The second argument has the form
1374 of a named association. The identifier
1375 indicates whether the file name is for a spec or a body;
1376 the file name itself is given by a string literal.
1378 The source file name pragma is a configuration pragma, which means that
1379 normally it will be placed in the @file{GNAT.ADC}
1380 file used to hold configuration
1381 pragmas that apply to a complete compilation environment.
1382 For more details on how the @file{GNAT.ADC} file is created and used
1383 @pxref{Handling of Configuration Pragmas}
1384 @cindex @file{GNAT.ADC}
1387 @noindent
1388 @code{GNAT MAKE} handles non-standard file names in the usual manner (the
1389 non-standard file name for the main program is simply used as the
1390 argument to GNAT MAKE). Note that if the extension is also non-standard,
1391 then it must be included in the GNAT MAKE command, it may not be omitted.
1393 @node Alternative File Naming Schemes
1394 @section Alternative File Naming Schemes
1395 @cindex File naming schemes, alternative
1396 @cindex File names
1398 In the previous section, we described the use of the @code{Source_File_Name}
1399 pragma to allow arbitrary names to be assigned to individual source files.
1400 However, this approach requires one pragma for each file, and especially in
1401 large systems can result in very long @file{GNAT.ADC} files, and also create
1402 a maintenance problem.
1404 GNAT also provides a facility for specifying systematic file naming schemes
1405 other than the standard default naming scheme previously described. An
1406 alternative scheme for naming is specified by the use of
1407 @code{Source_File_Name} pragmas having the following format:
1408 @cindex Source_File_Name pragma
1410 @smallexample
1411 pragma Source_File_Name (
1412    Spec_File_Name  => FILE_NAME_PATTERN
1413  [,Casing          => CASING_SPEC]
1414  [,Dot_Replacement => STRING_LITERAL]);
1416 pragma Source_File_Name (
1417    Body_File_Name  => FILE_NAME_PATTERN
1418  [,Casing          => CASING_SPEC]
1419  [,Dot_Replacement => STRING_LITERAL]);
1421 pragma Source_File_Name (
1422    Subunit_File_Name  => FILE_NAME_PATTERN
1423  [,Casing             => CASING_SPEC]
1424  [,Dot_Replacement    => STRING_LITERAL]);
1426 FILE_NAME_PATTERN ::= STRING_LITERAL
1427 CASING_SPEC ::= Lowercase | Uppercase | Mixedcase
1429 @end smallexample
1431 @noindent
1432 The @code{FILE_NAME_PATTERN} string shows how the file name is constructed.
1433 It contains a single asterisk character, and the unit name is substituted
1434 systematically for this asterisk. The optional parameter
1435 @code{Casing} indicates
1436 whether the unit name is to be all upper-case letters, all lower-case letters,
1437 or mixed-case. If no
1438 @code{Casing} parameter is used, then the default is all
1439 upper-case.
1441 The optional @code{Dot_Replacement} string is used to replace any periods
1442 that occur in subunit or child unit names. If no @code{Dot_Replacement}
1443 argument is used then separating dots appear unchanged in the resulting
1444 file name.
1445 Although the above syntax indicates that the
1446 @code{Casing} argument must appear
1447 before the @code{Dot_Replacement} argument, but it
1448 is also permissible to write these arguments in the opposite order.
1450 As indicated, it is possible to specify different naming schemes for
1451 bodies, specs, and subunits. Quite often the rule for subunits is the
1452 same as the rule for bodies, in which case, there is no need to give
1453 a separate @code{Subunit_File_Name} rule, and in this case the
1454 @code{Body_File_name} rule is used for subunits as well.
1456 The separate rule for subunits can also be used to implement the rather
1457 unusual case of a compilation environment (e.g. a single directory) which
1458 contains a subunit and a child unit with the same unit name. Although
1459 both units cannot appear in the same partition, the Ada Reference Manual
1460 allows (but does not require) the possibility of the two units coexisting
1461 in the same environment.
1463 The file name translation works in the following steps:
1465 @itemize @bullet
1467 @item
1468 If there is a specific @code{Source_File_Name} pragma for the given unit,
1469 then this is always used, and any general pattern rules are ignored.
1471 @item
1472 If there is a pattern type @code{Source_File_Name} pragma that applies to
1473 the unit, then the resulting file name will be used if the file exists. If
1474 more than one pattern matches, the latest one will be tried first, and the
1475 first attempt resulting in a reference to a file that exists will be used.
1477 @item
1478 If no pattern type @code{Source_File_Name} pragma that applies to the unit
1479 for which the corresponding file exists, then the standard GNAT default
1480 naming rules are used.
1482 @end itemize
1484 @noindent
1485 As an example of the use of this mechanism, consider a commonly used scheme
1486 in which file names are all lower case, with separating periods copied
1487 unchanged to the resulting file name, and specs end with ".1.ADA", and
1488 bodies end with ".2.ADA". GNAT will follow this scheme if the following
1489 two pragmas appear:
1491 @smallexample
1492 pragma Source_File_Name
1493   (Spec_File_Name => "*.1.ADA");
1494 pragma Source_File_Name
1495   (Body_File_Name => "*.2.ADA");
1496 @end smallexample
1498 @noindent
1499 The default GNAT scheme is actually implemented by providing the following
1500 default pragmas internally:
1502 @smallexample
1503 pragma Source_File_Name
1504   (Spec_File_Name => "*.ADS", Dot_Replacement => "-");
1505 pragma Source_File_Name
1506   (Body_File_Name => "*.ADB", Dot_Replacement => "-");
1507 @end smallexample
1509 @noindent
1510 Our final example implements a scheme typically used with one of the
1511 Ada 83 compilers, where the separator character for subunits was "__"
1512 (two underscores), specs were identified by adding @file{_.ADA}, bodies
1513 by adding @file{.ADA}, and subunits by
1514 adding @file{.SEP}. All file names were
1515 upper case. Child units were not present of course since this was an
1516 Ada 83 compiler, but it seems reasonable to extend this scheme to use
1517 the same double underscore separator for child units.
1519 @smallexample
1520 pragma Source_File_Name
1521   (Spec_File_Name => "*_.ADA",
1522    Dot_Replacement => "__",
1523    Casing = Uppercase);
1524 pragma Source_File_Name
1525   (Body_File_Name => "*.ADA",
1526    Dot_Replacement => "__",
1527    Casing = Uppercase);
1528 pragma Source_File_Name
1529   (Subunit_File_Name => "*.SEP",
1530    Dot_Replacement => "__",
1531    Casing = Uppercase);
1532 @end smallexample
1534 @node Generating Object Files
1535 @section Generating Object Files
1537 @noindent
1538 An Ada program consists of a set of source files, and the first step in
1539 compiling the program is to generate the corresponding object files.
1540 These are generated by compiling a subset of these source files.
1541 The files you need to compile are the following:
1543 @itemize @bullet
1544 @item
1545 If a package spec has no body, compile the package spec to produce the
1546 object file for the package.
1548 @item
1549 If a package has both a spec and a body, compile the body to produce the
1550 object file for the package. The source file for the package spec need
1551 not be compiled in this case because there is only one object file, which
1552 contains the code for both the spec and body of the package.
1554 @item
1555 For a subprogram, compile the subprogram body to produce the object file
1556 for the subprogram. The spec, if one is present, is as usual in a
1557 separate file, and need not be compiled.
1559 @item
1560 @cindex Subunits
1561 In the case of subunits, only compile the parent unit. A single object
1562 file is generated for the entire subunit tree, which includes all the
1563 subunits.
1565 @item
1566 Compile child units independently of their parent units
1567 (though, of course, the spec of all the ancestor unit must be present in order
1568 to compile a child unit).
1570 @item
1571 @cindex Generics
1572 Compile generic units in the same manner as any other units. The object
1573 files in this case are small dummy files that contain at most the
1574 flag used for elaboration checking. This is because GNAT always handles generic
1575 instantiation by means of macro expansion. However, it is still necessary to
1576 compile generic units, for dependency checking and elaboration purposes.
1577 @end itemize
1579 @noindent
1580 The preceding rules describe the set of files that must be compiled to
1581 generate the object files for a program. Each object file has the same
1582 name as the corresponding source file, except that the extension is
1583 @file{.OBJ} as usual.
1585 You may wish to compile other files for the purpose of checking their
1586 syntactic and semantic correctness. For example, in the case where a
1587 package has a separate spec and body, you would not normally compile the
1588 spec. However, it is convenient in practice to compile the spec to make
1589 sure it is error-free before compiling clients of this spec, because such
1590 compilations will fail if there is an error in the spec.
1592 GNAT provides an option for compiling such files purely for the
1593 purposes of checking correctness; such compilations are not required as
1594 part of the process of building a program. To compile a file in this
1595 checking mode, use the @option{/NOLOAD} qualifier.
1597 @node Source Dependencies
1598 @section Source Dependencies
1600 @noindent
1601 A given object file clearly depends on the source file which is compiled
1602 to produce it. Here we are using @dfn{depends} in the sense of a typical
1603 @code{make} utility; in other words, an object file depends on a source
1604 file if changes to the source file require the object file to be
1605 recompiled.
1606 In addition to this basic dependency, a given object may depend on
1607 additional source files as follows:
1609 @itemize @bullet
1610 @item
1611 If a file being compiled @code{with}'s a unit @var{X}, the object file
1612 depends on the file containing the spec of unit @var{X}. This includes
1613 files that are @code{with}'ed implicitly either because they are parents
1614 of @code{with}'ed child units or they are run-time units required by the
1615 language constructs used in a particular unit.
1617 @item
1618 If a file being compiled instantiates a library level generic unit, the
1619 object file depends on both the spec and body files for this generic
1620 unit.
1622 @item
1623 If a file being compiled instantiates a generic unit defined within a
1624 package, the object file depends on the body file for the package as
1625 well as the spec file.
1627 @item
1628 @findex Inline
1629 @cindex @option{/INLINE=PRAGMA} qualifier
1630 If a file being compiled contains a call to a subprogram for which
1631 pragma @code{Inline} applies and inlining is activated with the
1632 @option{/INLINE=PRAGMA} qualifier, the object file depends on the file containing the
1633 body of this subprogram as well as on the file containing the spec. Note
1634 that for inlining to actually occur as a result of the use of this qualifier,
1635 it is necessary to compile in optimizing mode.
1637 @cindex @option{-gnatN} qualifier
1638 The use of @option{-gnatN} activates a more extensive inlining optimization
1639 that is performed by the front end of the compiler. This inlining does
1640 not require that the code generation be optimized. Like @option{/INLINE=PRAGMA},
1641 the use of this qualifier generates additional dependencies.
1643 @item
1644 If an object file O  depends on the proper body of a subunit through inlining
1645 or instantiation, it depends on the parent unit of the subunit. This means that
1646 any modification of the parent unit or one of its subunits affects the
1647 compilation of O.
1649 @item
1650 The object file for a parent unit depends on all its subunit body files.
1652 @item
1653 The previous two rules meant that for purposes of computing dependencies and
1654 recompilation, a body and all its subunits are treated as an indivisible whole.
1656 @noindent
1657 These rules are applied transitively: if unit @code{A} @code{with}'s
1658 unit @code{B}, whose elaboration calls an inlined procedure in package
1659 @code{C}, the object file for unit @code{A} will depend on the body of
1660 @code{C}, in file @file{C.ADB}.
1662 The set of dependent files described by these rules includes all the
1663 files on which the unit is semantically dependent, as described in the
1664 Ada 95 Language Reference Manual. However, it is a superset of what the
1665 ARM describes, because it includes generic, inline, and subunit dependencies.
1667 An object file must be recreated by recompiling the corresponding source
1668 file if any of the source files on which it depends are modified. For
1669 example, if the @code{make} utility is used to control compilation,
1670 the rule for an Ada object file must mention all the source files on
1671 which the object file depends, according to the above definition.
1672 The determination of the necessary
1673 recompilations is done automatically when one uses @code{GNAT MAKE}.
1674 @end itemize
1676 @node The Ada Library Information Files
1677 @section The Ada Library Information Files
1678 @cindex Ada Library Information files
1679 @cindex @file{ali} files
1681 @noindent
1682 Each compilation actually generates two output files. The first of these
1683 is the normal object file that has a @file{.OBJ} extension. The second is a
1684 text file containing full dependency information. It has the same
1685 name as the source file, but an @file{.ALI} extension.
1686 This file is known as the Ada Library Information (@file{ali}) file.
1687 The following information is contained in the @file{ali} file.
1689 @itemize @bullet
1690 @item
1691 Version information (indicates which version of GNAT was used to compile
1692 the unit(s) in question)
1694 @item
1695 Main program information (including priority and time slice settings,
1696 as well as the wide character encoding used during compilation).
1698 @item
1699 List of arguments used in the @code{GNAT COMPILE} command for the compilation
1701 @item
1702 Attributes of the unit, including configuration pragmas used, an indication
1703 of whether the compilation was successful, exception model used etc.
1705 @item
1706 A list of relevant restrictions applying to the unit (used for consistency)
1707 checking.
1709 @item
1710 Categorization information (e.g. use of pragma @code{Pure}).
1712 @item
1713 Information on all @code{with}'ed units, including presence of
1714 @code{Elaborate} or @code{Elaborate_All} pragmas.
1716 @item
1717 Information from any @code{Linker_Options} pragmas used in the unit
1719 @item
1720 Information on the use of @code{Body_Version} or @code{Version}
1721 attributes in the unit.
1723 @item
1724 Dependency information. This is a list of files, together with
1725 time stamp and checksum information. These are files on which
1726 the unit depends in the sense that recompilation is required
1727 if any of these units are modified.
1729 @item
1730 Cross-reference data. Contains information on all entities referenced
1731 in the unit. Used by tools like @code{GNAT XREF} and @code{GNAT FIND} to
1732 provide cross-reference information.
1734 @end itemize
1736 @noindent
1737 For a full detailed description of the format of the @file{ali} file,
1738 see the source of the body of unit @code{Lib.Writ}, contained in file
1739 @file{LIB-WRIT.ADB} in the GNAT compiler sources.
1741 @node Binding an Ada Program
1742 @section Binding an Ada Program
1744 @noindent
1745 When using languages such as C and C++, once the source files have been
1746 compiled the only remaining step in building an executable program
1747 is linking the object modules together. This means that it is possible to
1748 link an inconsistent version of a program, in which two units have
1749 included different versions of the same header.
1751 The rules of Ada do not permit such an inconsistent program to be built.
1752 For example, if two clients have different versions of the same package,
1753 it is illegal to build a program containing these two clients.
1754 These rules are enforced by the GNAT binder, which also determines an
1755 elaboration order consistent with the Ada rules.
1757 The GNAT binder is run after all the object files for a program have
1758 been created. It is given the name of the main program unit, and from
1759 this it determines the set of units required by the program, by reading the
1760 corresponding ALI files. It generates error messages if the program is
1761 inconsistent or if no valid order of elaboration exists.
1763 If no errors are detected, the binder produces a main program, in Ada by
1764 default, that contains calls to the elaboration procedures of those
1765 compilation unit that require them, followed by
1766 a call to the main program. This Ada program is compiled to generate the
1767 object file for the main program. The name of
1768 the Ada file is @file{B$@var{xxx}.ADB} (with the corresponding spec
1769 @file{B$@var{xxx}.ADS}) where @var{xxx} is the name of the
1770 main program unit.
1772 Finally, the linker is used to build the resulting executable program,
1773 using the object from the main program from the bind step as well as the
1774 object files for the Ada units of the program.
1776 @node Mixed Language Programming
1777 @section Mixed Language Programming
1778 @cindex Mixed Language Programming
1780 @menu
1781 * Interfacing to C::
1782 * Calling Conventions::
1783 @end menu
1785 @node Interfacing to C
1786 @subsection Interfacing to C
1787 @noindent
1788 There are two ways to
1789 build a program that contains some Ada files and some other language
1790 files depending on whether the main program is in Ada or not.
1791 If the main program is in Ada, you should proceed as follows:
1793 @enumerate
1794 @item
1795 Compile the other language files to generate object files. For instance:
1796 @smallexample
1797 GNAT COMPILE FILE1.C
1798 GNAT COMPILE FILE2.C
1799 @end smallexample
1801 @item
1802 Compile the Ada units to produce a set of object files and ALI
1803 files. For instance:
1804 @smallexample
1805 GNAT MAKE /ACTIONS=COMPILE MY_MAIN.ADB
1806 @end smallexample
1808 @item
1809 Run the Ada binder on the Ada main program. For instance:
1810 @smallexample
1811 GNAT BIND MY_MAIN.ALI
1812 @end smallexample
1814 @item
1815 Link the Ada main program, the Ada objects and the other language
1816 objects. For instance:
1817 @smallexample
1818 GNAT LINK MY_MAIN.ALI FILE1.OBJ FILE2.OBJ
1819 @end smallexample
1820 @end enumerate
1822 The three last steps can be grouped in a single command:
1823 @smallexample
1824 GNAT MAKE MY_MAIN.ADB /LINKER_QUALIFIERS FILE1.OBJ FILE2.OBJ
1825 @end smallexample
1827 @cindex Binder output file
1828 @noindent
1829 If the main program is in some language other than Ada, Then you may
1830 have more than one entry point in the Ada subsystem. You must use a
1831 special option of the binder to generate callable routines to initialize
1832 and finalize the Ada units (@pxref{Binding with Non-Ada Main Programs}).
1833 Calls to the initialization and finalization routines must be inserted in
1834 the main program, or some other appropriate point in the code. The call to
1835 initialize the Ada units must occur before the first Ada subprogram is
1836 called, and the call to finalize the Ada units must occur after the last
1837 Ada subprogram returns. You use the same procedure for building the
1838 program as described previously. In this case, however, the binder
1839 only places the initialization and finalization subprograms into file
1840 @file{B$@var{xxx}.ADB} instead of the main program.
1841 So, if the main program is not in Ada, you should proceed as follows:
1843 @enumerate
1844 @item
1845 Compile the other language files to generate object files. For instance:
1846 @smallexample
1847 GNAT COMPILE FILE1.C
1848 GNAT COMPILE FILE2.C
1849 @end smallexample
1851 @item
1852 Compile the Ada units to produce a set of object files and ALI
1853 files. For instance:
1854 @smallexample
1855 GNAT MAKE /ACTIONS=COMPILE ENTRY_POINT1.ADB
1856 GNAT MAKE /ACTIONS=COMPILE ENTRY_POINT2.ADB
1857 @end smallexample
1859 @item
1860 Run the Ada binder on the Ada main program. For instance:
1861 @smallexample
1862 GNAT BIND /NOMAIN ENTRY_POINT1.ALI ENTRY_POINT2.ALI
1863 @end smallexample
1865 @item
1866 Link the Ada main program, the Ada objects and the other language
1867 objects. You only need to give the last entry point here. For instance:
1868 @smallexample
1869 GNAT LINK ENTRY_POINT2.ALI FILE1.OBJ FILE2.OBJ
1870 @end smallexample
1871 @end enumerate
1873 @node Calling Conventions
1874 @subsection Calling Conventions
1875 @cindex Foreign Languages
1876 @cindex Calling Conventions
1877 GNAT follows standard calling sequence conventions and will thus interface
1878 to any other language that also follows these conventions. The following
1879 Convention identifiers are recognized by GNAT:
1881 @itemize @bullet
1882 @cindex Interfacing to Ada
1883 @cindex Other Ada compilers
1884 @cindex Convention Ada
1885 @item
1886 Ada. This indicates that the standard Ada calling sequence will be
1887 used and all Ada data items may be passed without any limitations in the
1888 case where GNAT is used to generate both the caller and callee. It is also
1889 possible to mix GNAT generated code and code generated by another Ada
1890 compiler. In this case, the data types should be restricted to simple
1891 cases, including primitive types. Whether complex data types can be passed
1892 depends on the situation. Probably it is safe to pass simple arrays, such
1893 as arrays of integers or floats. Records may or may not work, depending
1894 on whether both compilers lay them out identically. Complex structures
1895 involving variant records, access parameters, tasks, or protected types,
1896 are unlikely to be able to be passed.
1898 Note that in the case of GNAT running
1899 on a platform that supports DEC Ada 83, a higher degree of compatibility
1900 can be guaranteed, and in particular records are layed out in an identical
1901 manner in the two compilers. Note also that if output from two different
1902 compilers is mixed, the program is responsible for dealing with elaboration
1903 issues. Probably the safest approach is to write the main program in the
1904 version of Ada other than GNAT, so that it takes care of its own elaboration
1905 requirements, and then call the GNAT-generated adainit procedure to ensure
1906 elaboration of the GNAT components. Consult the documentation of the other
1907 Ada compiler for further details on elaboration.
1909 However, it is not possible to mix the tasking run time of GNAT and
1910 DEC Ada 83, All the tasking operations must either be entirely within
1911 GNAT compiled sections of the program, or entirely within DEC Ada 83
1912 compiled sections of the program.
1914 @cindex Interfacing to Assembly
1915 @cindex Convention Assembler
1916 @item
1917 Assembler. Specifies assembler as the convention. In practice this has the
1918 same effect as convention Ada (but is not equivalent in the sense of being
1919 considered the same convention).
1921 @cindex Convention Asm
1922 @findex Asm
1923 @item
1924 Asm. Equivalent to Assembler.
1926 @cindex Convention Asm
1927 @findex Asm
1928 @item
1929 Asm. Equivalent to Assembly.
1931 @cindex Interfacing to COBOL
1932 @cindex Convention COBOL
1933 @findex COBOL
1934 @item
1935 COBOL. Data will be passed according to the conventions described
1936 in section B.4 of the Ada 95 Reference Manual.
1938 @findex C
1939 @cindex Interfacing to C
1940 @cindex Convention C
1941 @item
1942 C. Data will be passed according to the conventions described
1943 in section B.3 of the Ada 95 Reference Manual.
1945 @cindex Convention Default
1946 @findex Default
1947 @item
1948 Default. Equivalent to C.
1950 @cindex Convention External
1951 @findex External
1952 @item
1953 External. Equivalent to C.
1955 @findex C++
1956 @cindex Interfacing to C++
1957 @cindex Convention C++
1958 @item
1959 CPP. This stands for C++. For most purposes this is identical to C.
1960 See the separate description of the specialized GNAT pragmas relating to
1961 C++ interfacing for further details.
1963 @findex Fortran
1964 @cindex Interfacing to Fortran
1965 @cindex Convention Fortran
1966 @item
1967 Fortran. Data will be passed according to the conventions described
1968 in section B.5 of the Ada 95 Reference Manual.
1970 @item
1971 Intrinsic. This applies to an intrinsic operation, as defined in the Ada 95
1972 Reference Manual. If a a pragma Import (Intrinsic) applies to a subprogram,
1973 this means that the body of the subprogram is provided by the compiler itself,
1974 usually by means of an efficient code sequence, and that the user does not
1975 supply an explicit body for it. In an application program, the pragma can only
1976 be applied to the following two sets of names, which the GNAT compiler
1977 recognizes.
1978 @itemize @bullet
1979 @item
1980 Rotate_Left, Rotate_Right, Shift_Left, Shift_Right, Shift_Right_-
1981 Arithmetic.  The corresponding subprogram declaration must have
1982 two formal parameters. The
1983 first one must be a signed integer type or a modular type with a binary
1984 modulus, and the second parameter must be of type Natural.
1985 The return type must be the same as the type of the first argument. The size
1986 of this type can only be 8, 16, 32, or 64.
1987 @item binary arithmetic operators: "+", "-", "*", "/"
1988 The corresponding operator declaration must have parameters and result type
1989 that have the same root numeric type (for example, all three are long_float
1990 types). This simplifies the definition of operations that use type checking
1991 to perform dimensional checks:
1992 @smallexample
1993 type Distance is new Long_Float;
1994 type Time     is new Long_Float;
1995 type Velocity is new Long_Float;
1996 function "/" (D : Distance; T : Time)
1997   return Velocity;
1998 pragma Import (Intrinsic, "/");
1999 @end smallexample
2000 @noindent
2001 This common idiom is often programmed with a generic definition and an explicit
2002 body. The pragma makes it simpler to introduce such declarations. It incurs
2003 no overhead in compilation time or code size, because it is implemented as a
2004 single machine instruction.
2005 @end itemize
2006 @noindent
2008 @findex Stdcall
2009 @cindex Convention Stdcall
2010 @item
2011 Stdcall. This is relevant only to NT/Win95 implementations of GNAT,
2012 and specifies that the Stdcall calling sequence will be used, as defined
2013 by the NT API.
2015 @findex DLL
2016 @cindex Convention DLL
2017 @item
2018 DLL. This is equivalent to Stdcall.
2020 @findex Win32
2021 @cindex Convention Win32
2022 @item
2023 Win32. This is equivalent to Stdcall.
2025 @findex Stubbed
2026 @cindex Convention Stubbed
2027 @item
2028 Stubbed. This is a special convention that indicates that the compiler
2029 should provide a stub body that raises @code{Program_Error}.
2030 @end itemize
2032 @noindent
2033 GNAT additionally provides a useful pragma @code{Convention_Identifier}
2034 that can be used to parametrize conventions and allow additional synonyms
2035 to be specified. For example if you have legacy code in which the convention
2036 identifier Fortran77 was used for Fortran, you can use the configuration
2037 pragma:
2039 @smallexample
2040    pragma Convention_Identifier (Fortran77, Fortran);
2041 @end smallexample
2043 @noindent
2044 And from now on the identifier Fortran77 may be used as a convention
2045 identifier (for example in an @code{Import} pragma) with the same
2046 meaning as Fortran.
2048 @node Building Mixed Ada & C++ Programs
2049 @section Building Mixed Ada & C++ Programs
2051 @noindent
2052 Building a mixed application containing both Ada and C++ code may be a
2053 challenge for the unaware programmer. As a matter of fact, this
2054 interfacing has not been standardized in the Ada 95 reference manual due
2055 to the immaturity and lack of standard of C++ at the time. This
2056 section gives a few hints that should make this task easier. In
2057 particular the first section addresses the differences with
2058 interfacing with C. The second section looks into the delicate problem
2059 of linking the complete application from its Ada and C++ parts. The last
2060 section give some hints on how the GNAT run time can be adapted in order
2061 to allow inter-language dispatching with a new C++ compiler.
2063 @menu
2064 * Interfacing to C++::
2065 * Linking a Mixed C++ & Ada Program::
2066 * A Simple Example::
2067 * Adapting the Run Time to a New C++ Compiler::
2068 @end menu
2070 @node Interfacing to C++
2071 @subsection Interfacing to C++
2073 @noindent
2074 GNAT supports interfacing with C++ compilers generating code that is
2075 compatible with the standard Application Binary Interface of the given
2076 platform.
2078 @noindent
2079 Interfacing can be done at 3 levels: simple data, subprograms and
2080 classes. In the first 2 cases, GNAT offer a specific @var{Convention
2081 CPP} that behaves exactly like @var{Convention C}. Usually C++ mangle
2082 names of subprograms and currently GNAT does not provide any help to
2083 solve the demangling problem. This problem can be addressed in 2 ways:
2084 @itemize @bullet
2085 @item
2086 by modifying the C++ code in order to force a C convention using
2087 the @var{extern "C"} syntax.
2089 @item
2090 by figuring out the mangled name and use it as the Link_Name argument of
2091 the pragma import.
2092 @end itemize
2094 @noindent
2095 Interfacing at the class level can be achieved by using the GNAT specific
2096 pragmas such as @code{CPP_Class} and @code{CPP_Virtual}. See the GNAT
2097 Reference Manual for additional information.
2099 @node Linking a Mixed C++ & Ada Program
2100 @subsection Linking a Mixed C++ & Ada Program
2102 @noindent
2103 Usually the linker of the C++ development system must be used to link
2104 mixed applications because most C++ systems will resolve elaboration
2105 issues (such as calling constructors on global class instances)
2106 transparently during the link phase. GNAT has been adapted to ease the
2107 use of a foreign linker for the last phase. Three cases can be
2108 considered:
2109 @enumerate
2111 @item
2112 Using GNAT and G++ (GNU C++ compiler) from the same GCC
2113 installation. The c++ linker can simply be called by using the c++
2114 specific driver called @code{c++}. Note that this setup is not
2115 very common because it may request recompiling the whole GCC
2116 tree from sources and it does not allow to upgrade easily to a new
2117 version of one compiler for one of the two languages without taking the
2118 risk of destabilizing the other.
2120 @smallexample
2121 $ c++ -c file1.C
2122 $ c++ -c file2.C
2123 $ GNAT MAKE ada_unit /LINKER_QUALIFIERS FILE1.OBJ FILE2.OBJ --LINK=c++
2124 @end smallexample
2126 @item
2127 Using GNAT and G++ from 2 different GCC installations. If both compilers
2128 are on the PATH, the same method can be used. It is important to be
2129 aware that environment variables such as C_INCLUDE_PATH,
2130 GCC_EXEC_PREFIX, BINUTILS_ROOT or GCC_ROOT will affect both compilers at
2131 the same time and thus may make one of the 2 compilers operate
2132 improperly if they are set for the other. In particular it is important
2133 that the link command has access to the proper GNAT COMPILE library @file{libgcc.a},
2134 that is to say the one that is part of the C++ compiler
2135 installation. The implicit link command as suggested in the GNAT MAKE
2136 command from the former example can be replaced by an explicit link
2137 command with full verbosity in order to verify which library is used:
2138 @smallexample
2139 $ GNAT BIND ada_unit
2140 $ GNAT LINK -v -v ada_unit FILE1.OBJ FILE2.OBJ --LINK=c++
2141 @end smallexample
2142 If there is a problem due to interfering environment variables, it can
2143 be workaround by using an intermediate script. The following example
2144 shows the proper script to use when GNAT has not been installed at its
2145 default location and g++ has been installed at its default location:
2147 @smallexample
2148 $ GNAT LINK -v -v ada_unit FILE1.OBJ FILE2.OBJ --LINK=./my_script
2149 $ cat ./my_script
2150 #!/bin/sh
2151 unset BINUTILS_ROOT
2152 unset GCC_ROOT
2153 c++ $*
2154 @end smallexample
2156 @item
2157 Using a non GNU C++ compiler. The same set of command as previously
2158 described can be used to insure that the c++ linker is
2159 used. Nonetheless, you need to add the path to libgcc explicitely, since some
2160 libraries needed by GNAT are located in this directory:
2162 @smallexample
2164 $ GNAT LINK ada_unit FILE1.OBJ FILE2.OBJ --LINK=./my_script
2165 $ cat ./my_script
2166 #!/bin/sh
2167 CC $* `GNAT COMPILE -print-libgcc-file-name`
2169 @end smallexample
2171 Where CC is the name of the non GNU C++ compiler.
2173 @end enumerate
2175 @node A Simple Example
2176 @subsection  A Simple Example
2177 @noindent
2178 The following example, provided as part of the GNAT examples, show how
2179 to achieve procedural interfacing between Ada and C++ in both
2180 directions. The C++ class A has 2 methods. The first method is exported
2181 to Ada by the means of an extern C wrapper function. The second method
2182 calls an Ada subprogram. On the Ada side, The C++ calls is modelized by
2183 a limited record with a layout comparable to the C++ class. The Ada
2184 subprogram, in turn, calls the c++ method. So from the C++ main program
2185 the code goes back and forth between the 2 languages.
2187 @noindent
2188 Here are the compilation commands
2189 for native configurations:
2190 @smallexample
2191 $ GNAT MAKE -c simple_cpp_interface
2192 $ c++ -c cpp_main.C
2193 $ c++ -c ex7.C
2194 $ GNAT BIND -n simple_cpp_interface
2195 $ GNAT LINK simple_cpp_interface -o cpp_main --LINK=$(CPLUSPLUS)
2196       -lstdc++ EX7.OBJ CPP_MAIN.OBJ
2197 @end smallexample
2198 @noindent
2199 Here are the corresponding sources:
2200 @smallexample
2202 //cpp_main.C
2204 #include "ex7.h"
2206 extern "C" @{
2207   void adainit (void);
2208   void adafinal (void);
2209   void method1 (A *t);
2212 void method1 (A *t)
2214   t->method1 ();
2217 int main ()
2219   A obj;
2220   adainit ();
2221   obj.method2 (3030);
2222   adafinal ();
2225 //ex7.h
2227 class Origin @{
2228  public:
2229   int o_value;
2231 class A : public Origin @{
2232  public:
2233   void method1 (void);
2234   virtual void method2 (int v);
2235   A();
2236   int   a_value;
2239 //ex7.C
2241 #include "ex7.h"
2242 #include <stdio.h>
2244 extern "C" @{ void ada_method2 (A *t, int v);@}
2246 void A::method1 (void)
2248   a_value = 2020;
2249   printf ("in A::method1, a_value = %d \n",a_value);
2253 void A::method2 (int v)
2255    ada_method2 (this, v);
2256    printf ("in A::method2, a_value = %d \n",a_value);
2260 A::A(void)
2262    a_value = 1010;
2263   printf ("in A::A, a_value = %d \n",a_value);
2266 -- Ada sources
2267 @b{package} @b{body} Simple_Cpp_Interface @b{is}
2269    @b{procedure} Ada_Method2 (This : @b{in} @b{out} A; V : Integer) @b{is}
2270    @b{begin}
2271       Method1 (This);
2272       This.A_Value := V;
2273    @b{end} Ada_Method2;
2275 @b{end} Simple_Cpp_Interface;
2277 @b{package} Simple_Cpp_Interface @b{is}
2278    @b{type} A @b{is} @b{limited}
2279       @b{record}
2280          O_Value : Integer;
2281          A_Value : Integer;
2282       @b{end} @b{record};
2283    @b{pragma} Convention (C, A);
2285    @b{procedure} Method1 (This : @b{in} @b{out} A);
2286    @b{pragma} Import (C, Method1);
2288    @b{procedure} Ada_Method2 (This : @b{in} @b{out} A; V : Integer);
2289    @b{pragma} Export (C, Ada_Method2);
2291 @b{end} Simple_Cpp_Interface;
2292 @end smallexample
2294 @node Adapting the Run Time to a New C++ Compiler
2295 @subsection Adapting the Run Time to a New C++ Compiler
2296 @noindent
2297 GNAT offers the capability to derive Ada 95 tagged types directly from
2298 preexisting C++ classes and . See "Interfacing with C++" in the GNAT
2299 reference manual. The mechanism used by GNAT for achieving such a goal
2300 has been made user configurable through a GNAT library unit
2301 @code{Interfaces.CPP}. The default version of this file is adapted to
2302 the GNU c++ compiler. Internal knowledge of the virtual
2303 table layout used by the new C++ compiler is needed to configure
2304 properly this unit. The Interface of this unit is known by the compiler
2305 and cannot be changed except for the value of the constants defining the
2306 characteristics of the virtual table: CPP_DT_Prologue_Size, CPP_DT_Entry_Size,
2307 CPP_TSD_Prologue_Size, CPP_TSD_Entry_Size. Read comments in the source
2308 of this unit for more details.
2310 @node Comparison between GNAT and C/C++ Compilation Models
2311 @section Comparison between GNAT and C/C++ Compilation Models
2313 @noindent
2314 The GNAT model of compilation is close to the C and C++ models. You can
2315 think of Ada specs as corresponding to header files in C. As in C, you
2316 don't need to compile specs; they are compiled when they are used. The
2317 Ada @code{with} is similar in effect to the @code{#include} of a C
2318 header.
2320 One notable difference is that, in Ada, you may compile specs separately
2321 to check them for semantic and syntactic accuracy. This is not always
2322 possible with C headers because they are fragments of programs that have
2323 less specific syntactic or semantic rules.
2325 The other major difference is the requirement for running the binder,
2326 which performs two important functions. First, it checks for
2327 consistency. In C or C++, the only defense against assembling
2328 inconsistent programs lies outside the compiler, in a makefile, for
2329 example. The binder satisfies the Ada requirement that it be impossible
2330 to construct an inconsistent program when the compiler is used in normal
2331 mode.
2333 @cindex Elaboration order control
2334 The other important function of the binder is to deal with elaboration
2335 issues. There are also elaboration issues in C++ that are handled
2336 automatically. This automatic handling has the advantage of being
2337 simpler to use, but the C++ programmer has no control over elaboration.
2338 Where @code{GNAT BIND} might complain there was no valid order of
2339 elaboration, a C++ compiler would simply construct a program that
2340 malfunctioned at run time.
2342 @node Comparison between GNAT and Conventional Ada Library Models
2343 @section Comparison between GNAT and Conventional Ada Library Models
2345 @noindent
2346 This section is intended to be useful to Ada programmers who have
2347 previously used an Ada compiler implementing the traditional Ada library
2348 model, as described in the Ada 95 Language Reference Manual. If you
2349 have not used such a system, please go on to the next section.
2351 @cindex GNAT library
2352 In GNAT, there is no @dfn{library} in the normal sense. Instead, the set of
2353 source files themselves acts as the library. Compiling Ada programs does
2354 not generate any centralized information, but rather an object file and
2355 a ALI file, which are of interest only to the binder and linker.
2356 In a traditional system, the compiler reads information not only from
2357 the source file being compiled, but also from the centralized library.
2358 This means that the effect of a compilation depends on what has been
2359 previously compiled. In particular:
2361 @itemize @bullet
2362 @item
2363 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
2364 to the version of the unit most recently compiled into the library.
2366 @item
2367 Inlining is effective only if the necessary body has already been
2368 compiled into the library.
2370 @item
2371 Compiling a unit may obsolete other units in the library.
2372 @end itemize
2374 @noindent
2375 In GNAT, compiling one unit never affects the compilation of any other
2376 units because the compiler reads only source files. Only changes to source
2377 files can affect the results of a compilation. In particular:
2379 @itemize @bullet
2380 @item
2381 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
2382 to the source version of the unit that is currently accessible to the
2383 compiler.
2385 @item
2386 @cindex Inlining
2387 Inlining requires the appropriate source files for the package or
2388 subprogram bodies to be available to the compiler. Inlining is always
2389 effective, independent of the order in which units are complied.
2391 @item
2392 Compiling a unit never affects any other compilations. The editing of
2393 sources may cause previous compilations to be out of date if they
2394 depended on the source file being modified.
2395 @end itemize
2397 @noindent
2398 The most important result of these differences is that order of compilation
2399 is never significant in GNAT. There is no situation in which one is
2400 required to do one compilation before another. What shows up as order of
2401 compilation requirements in the traditional Ada library becomes, in
2402 GNAT, simple source dependencies; in other words, there is only a set
2403 of rules saying what source files must be present when a file is
2404 compiled.
2406 @node Compiling Using GNAT COMPILE
2407 @chapter Compiling Using @code{GNAT COMPILE}
2409 @noindent
2410 This chapter discusses how to compile Ada programs using the @code{GNAT COMPILE}
2411 command. It also describes the set of qualifiers
2412 that can be used to control the behavior of the compiler.
2413 @menu
2414 * Compiling Programs::
2415 * Qualifiers for GNAT COMPILE::
2416 * Search Paths and the Run-Time Library (RTL)::
2417 * Order of Compilation Issues::
2418 * Examples::
2419 @end menu
2421 @node Compiling Programs
2422 @section Compiling Programs
2424 @noindent
2425 The first step in creating an executable program is to compile the units
2426 of the program using the @code{GNAT COMPILE} command. You must compile the
2427 following files:
2429 @itemize @bullet
2430 @item
2431 the body file (@file{.ADB}) for a library level subprogram or generic
2432 subprogram
2434 @item
2435 the spec file (@file{.ADS}) for a library level package or generic
2436 package that has no body
2438 @item
2439 the body file (@file{.ADB}) for a library level package
2440 or generic package that has a body
2442 @end itemize
2444 @noindent
2445 You need @emph{not} compile the following files
2447 @itemize @bullet
2449 @item
2450 the spec of a library unit which has a body
2452 @item
2453 subunits
2454 @end itemize
2456 @noindent
2457 because they are compiled as part of compiling related units. GNAT
2458 package specs
2459 when the corresponding body is compiled, and subunits when the parent is
2460 compiled.
2461 @cindex No code generated
2462 If you attempt to compile any of these files, you will get one of the
2463 following error messages (where fff is the name of the file you compiled):
2465 @smallexample
2466 No code generated for file @var{fff} (@var{package spec})
2467 No code generated for file @var{fff} (@var{subunit})
2468 @end smallexample
2470 @noindent
2471 The basic command for compiling a file containing an Ada unit is
2473 @smallexample
2474 $ GNAT COMPILE [@var{qualifiers}] @file{file name}
2475 @end smallexample
2477 @noindent
2478 where @var{file name} is the name of the Ada file (usually
2479 having an extension
2480 @file{.ADS} for a spec or @file{.ADB} for a body).
2481 The result of a successful compilation is an object file, which has the
2482 same name as the source file but an extension of @file{.OBJ} and an Ada
2483 Library Information (ALI) file, which also has the same name as the
2484 source file, but with @file{.ALI} as the extension. GNAT creates these
2485 two output files in the current directory, but you may specify a source
2486 file in any directory using an absolute or relative path specification
2487 containing the directory information.
2489 @findex GNAT1
2490 @code{GNAT COMPILE} is actually a driver program that looks at the extensions of
2491 the file arguments and loads the appropriate compiler. For example, the
2492 GNU C compiler is @file{CC1}, and the Ada compiler is @file{GNAT1}.
2493 These programs are in directories known to the driver program (in some
2494 configurations via environment variables you set), but need not be in
2495 your path. The @code{GNAT COMPILE} driver also calls the assembler and any other
2496 utilities needed to complete the generation of the required object
2497 files.
2499 It is possible to supply several file names on the same @code{GNAT COMPILE}
2500 command. This causes @code{GNAT COMPILE} to call the appropriate compiler for
2501 each file. For example, the following command lists three separate
2502 files to be compiled:
2504 @smallexample
2505 $ GNAT COMPILE X.ADB Y.ADB Z.C
2506 @end smallexample
2508 @noindent
2509 calls @code{GNAT1} (the Ada compiler) twice to compile @file{X.ADB} and
2510 @file{Y.ADB}, and @code{CC1} (the C compiler) once to compile @file{Z.C}.
2511 The compiler generates three object files @file{X.OBJ}, @file{Y.OBJ} and
2512 @file{Z.OBJ} and the two ALI files @file{X.ALI} and @file{Y.ALI} from the
2513 Ada compilations. Any qualifiers apply to all the files listed.
2515 @node Qualifiers for GNAT COMPILE
2516 @section Qualifiers for @code{GNAT COMPILE}
2518 @noindent
2519 The @code{GNAT COMPILE} command accepts qualifiers that control the
2520 compilation process. These qualifiers are fully described in this section.
2521 First we briefly list all the qualifiers, in alphabetical order, then we
2522 describe the qualifiers in more detail in functionally grouped sections.
2524 @menu
2525 * Output and Error Message Control::
2526 * Debugging and Assertion Control::
2527 * Run-Time Checks::
2528 * Stack Overflow Checking::
2529 * Run-Time Control::
2530 * Validity Checking::
2531 * Style Checking::
2532 * Using GNAT COMPILE for Syntax Checking::
2533 * Using GNAT COMPILE for Semantic Checking::
2534 * Compiling Ada 83 Programs::
2535 * Character Set Control::
2536 * File Naming Control::
2537 * Subprogram Inlining Control::
2538 * Auxiliary Output Control::
2539 * Debugging Control::
2540 * Units to Sources Mapping Files::
2541 @end menu
2543 @table @code
2545 @item /DEBUG
2546 @cindex @code{/DEBUG} (@code{GNAT COMPILE})
2547 Generate debugging information. This information is stored in the object
2548 file and copied from there to the final executable file by the linker,
2549 where it can be read by the debugger. You must use the
2550 @code{/DEBUG} qualifier if you plan on using the debugger.
2552 @item /SEARCH=@var{dir}
2553 @cindex @code{/SEARCH} (@code{GNAT COMPILE})
2554 @cindex RTL
2555 Direct GNAT to search the @var{dir} directory for source files needed by
2556 the current compilation
2557 (@pxref{Search Paths and the Run-Time Library (RTL)}).
2559 @item /NOCURRENT_DIRECTORY
2560 @cindex @code{/NOCURRENT_DIRECTORY} (@code{GNAT COMPILE})
2561 @cindex RTL
2562 Except for the source file named in the command line, do not look for source files
2563 in the directory containing the source file named in the command line
2564 (@pxref{Search Paths and the Run-Time Library (RTL)}).
2568 @item  /NOOPTIMIZE (default)
2569 @itemx /OPTIMIZE[=(keyword[,...])]
2570 Selects the level of optimization for your program. The supported
2571 keywords are as follows:
2572 @table @code
2573 @item   ALL (default)
2574 Perform most optimizations, including those that
2575 be expensive.
2577 @item   NONE
2578 Do not do any optimizations. Same as @code{/NOOPTIMIZE}.
2580 @item SOME
2581 Perform some optimizations, but omit ones that are costly.
2583 @item   DEVELOPMENT
2584 Same as @code{SOME}.
2586 @item   INLINING
2587 Full optimization, and also attempt automatic inlining of small
2588 subprograms within a unit (@pxref{Inlining of Subprograms}).
2590 @item   UNROLL_LOOPS
2591 Try to unroll loops. This keyword may be specified together with
2592 any keyword above other than @code{NONE}. Loop unrolling
2593 usually, but not always, improves the performance of programs.
2594 @end table
2596 @item /RUNTIME_SYSTEM=@var{rts-path}
2597 @cindex @code{/RUNTIME_SYSTEM} (@code{GNAT COMPILE})
2598 Specifies the default location of the runtime library. Same meaning as the
2599 equivalent @code{GNAT MAKE} flag (see @ref{Qualifiers for GNAT MAKE}).
2601 @item /ASM
2602 @cindex @code{/ASM} (@code{GNAT COMPILE})
2603 Used to
2604 cause the assembler source file to be
2605 generated, using @file{.S} as the extension,
2606 instead of the object file.
2607 This may be useful if you need to examine the generated assembly code.
2609 @item /VERBOSE
2610 @cindex @code{/VERBOSE} (@code{GNAT COMPILE})
2611 Show commands generated by the @code{GNAT COMPILE} driver. Normally used only for
2612 debugging purposes or if you need to be sure what version of the
2613 compiler you are executing.
2616 @item /CHECKS=ASSERTIONS
2617 Assertions enabled. @code{Pragma Assert} and @code{pragma Debug} to be
2618 activated.
2620 @item -gnatA
2621 Avoid processing @file{GNAT.ADC}. If a GNAT.ADC file is present, it will be ignored.
2623 @item /WARNINGS=BRIEF
2624 Generate brief messages to @file{SYS$ERROR} even if verbose mode set.
2626 @item /NOLOAD
2627 Check syntax and semantics only (no code generation attempted).
2629 @item /COMPRESS_NAMES
2630 Compress debug information and external symbol name table entries.
2632 @item /XDEBUG
2633 Output expanded source files for source level debugging. This qualifier
2634 also suppress generation of cross-reference information (see /XREF=SUPPRESS).
2636 @item -gnatec@var{path}
2637 Specify a configuration pragma file. (see @ref{The Configuration Pragmas Files})
2639 @item -gnatem@var{path}
2640 Specify a mapping file. (see @ref{Units to Sources Mapping Files})
2642 @item /CHECKS=ELABORATION
2643 Full dynamic elaboration checks.
2645 @item /REPORT_ERRORS=FULL
2646 Full errors. Multiple errors per line, all undefined references.
2648 @item /UPPERCASE_EXTERNALS
2649 Externals names are folded to all uppercase.
2651 @item /STYLE=GNAT
2652 Internal GNAT implementation mode. This should not be used for
2653 applications programs, it is intended only for use by the compiler
2654 and its run-time library. For documentation, see the GNAT sources.
2656 @item /EXPAND_SOURCE
2657 List generated expanded code in source form.
2659 @item /IDENTIFIER_CHARACTER_SET=@var{c}
2660 Identifier character set
2661 For details of the possible selections for @var{c},
2662 see @xref{Character Set Control}.
2664 @item /HELP
2665 Output usage information. The output is written to @file{SYS$OUTPUT}.
2667 @item /FILE_NAME_MAX_LENGTH=@var{n}
2668 Limit file names to @var{n} (1-999) characters .
2670 @item /LIST
2671 Output full source listing with embedded error messages.
2673 @item /ERROR_LIMIT=@var{n}
2674 Limit number of detected errors to @var{n} (1-999).
2676 @item /INLINE=PRAGMA
2677 Activate inlining across unit boundaries for subprograms for which
2678 pragma @code{inline} is specified.
2680 @item -gnatN
2681 Activate front end inlining.
2683 @item /INLINE=SUPPRESS
2684 Suppresses all inlining, even if other optimization or inlining qualifiers
2685 are set.
2688 @item /CHECKS=OVERFLOW
2689 Enable numeric overflow checking (which is not normally enabled by
2690 default). Not that division by zero is a separate check that is not
2691 controlled by this qualifier (division by zero checking is on by default).
2693 @item /CHECKS=SUPPRESS_ALL
2694 Suppress all checks.
2696 @item /TRY_SEMANTICS
2697 Don't quit; try semantics, even if parse errors.
2699 @item /FORCE_ALI
2700 Don't quit; generate @file{ali} and tree files even if illegalities.
2702 @item /POLLING_ENABLE
2703 Enable polling. This is required on some systems (notably Windows NT) to
2704 obtain asynchronous abort and asynchronous transfer of control capability.
2705 See the description of pragma Polling in the GNAT Reference Manual for
2706 full details.
2708 @item /REPRESENTATION_INFO[0/1/2/3][s]
2709 Output representation information for declared types and objects.
2711 @item /SYNTAX_ONLY
2712 Syntax check only.
2714 @item /TREE_OUTPUT
2715 Tree output file to be generated.
2717 @item -gnatT nnn
2718 Set time slice to specified number of microseconds
2720 @item /UNITS_LIST
2721 List units for this compilation.
2723 @item /UNIQUE_ERROR_TAG
2724 Tag all error messages with the unique string "error:"
2726 @item /REPORT_ERRORS=VERBOSE
2727 Verbose mode. Full error output with source lines to @file{SYS$OUTPUT}.
2729 @item /VALIDITY_CHECKING
2730 Control level of validity checking. See separate section describing
2731 this feature.
2733 @item /WARNINGS=@var{xxx}
2734 Warning mode where
2735 @var{xxx} is a string of options describing the exact warnings that
2736 are enabled or disabled. See separate section on warning control.
2738 @item /WIDE_CHARACTER_ENCODING=@var{e}
2739 Wide character encoding method
2740 (@var{e}=@code{BRACKETS, NONE, HEX, UPPER, SHIFT_JIS, EUC, UTF8})
2742 @item /XREF=SUPPRESS
2743 Suppress generation of cross-reference information.
2745 @item /STYLE_CHECKS=(option,option..)
2746 Enable built-in style checks. See separate section describing this feature.
2748 @item /DISTRIBUTION_STUBS=@var{m}
2749 Distribution stub generation and compilation
2750 (@var{m}=@code{RECEIVER} or @code{CALLER} to specify the type of stubs
2751 to be generated and compiled).
2753 @item /83
2754 Enforce Ada 83 restrictions.
2756 @end table
2759 @noindent
2760 The following restrictions apply to the combination of qualifiers
2761 in this manner:
2763 @itemize @bullet
2764 @item
2765 The qualifier @option{/NOLOAD} if combined with other qualifiers must come
2766 first in the string.
2768 @item
2769 The qualifier @option{/SYNTAX_ONLY} if combined with other qualifiers must come
2770 first in the string.
2772 @item
2773 Once a "y" appears in the string (that is a use of the @option{/STYLE=}
2774 qualifier), then all further characters in the qualifier are interpreted
2775 as style modifiers (see description of @option{/STYLE=}).
2777 @item
2778 Once a "d" appears in the string (that is a use of the @option{-gnatd}
2779 qualifier), then all further characters in the qualifier are interpreted
2780 as debug flags (see description of @option{-gnatd}).
2782 @item
2783 Once a "w" appears in the string (that is a use of the @option{-gnatw}
2784 qualifier), then all further characters in the qualifier are interpreted
2785 as warning mode modifiers (see description of @option{-gnatw}).
2787 @item
2788 Once a "V" appears in the string (that is a use of the @option{/VALIDITY_CHECKING}
2789 qualifier), then all further characters in the qualifier are interpreted
2790 as validity checking options (see description of @option{/VALIDITY_CHECKING}).
2792 @end itemize
2794 @node Output and Error Message Control
2795 @subsection Output and Error Message Control
2796 @findex SYS$ERROR
2798 @noindent
2799 The standard default format for error messages is called "brief format."
2800 Brief format messages are written to @file{SYS$ERROR} (the standard error
2801 file) and have the following form:
2803 @smallexample
2804 @iftex
2805 @leftskip=.7cm
2806 @end iftex
2807 E.ADB:3:04: Incorrect spelling of keyword "function"
2808 E.ADB:4:20: ";" should be "is"
2809 @end smallexample
2811 @noindent
2812 The first integer after the file name is the line number in the file,
2813 and the second integer is the column number within the line.
2814 @code{glide} can parse the error messages
2815 and point to the referenced character.
2816 The following qualifiers provide control over the error message
2817 format:
2819 @table @code
2820 @item /REPORT_ERRORS=VERBOSE
2821 @cindex @option{/REPORT_ERRORS=VERBOSE} (@code{GNAT COMPILE})
2822 @findex SYS$OUTPUT
2823 The effect of this setting is to write long-format error
2824 messages to @file{SYS$OUTPUT} (the standard output file.
2825 The same program compiled with the
2826 @option{/REPORT_ERRORS=VERBOSE} qualifier would generate:
2828 @smallexample
2829 @group
2830 @cartouche
2831 3. funcion X (Q : Integer)
2832    |
2833 >>> Incorrect spelling of keyword "function"
2834 4. return Integer;
2835                  |
2836 >>> ";" should be "is"
2837 @end cartouche
2838 @end group
2839 @end smallexample
2841 @noindent
2842 The vertical bar indicates the location of the error, and the @samp{>>>}
2843 prefix can be used to search for error messages. When this qualifier is
2844 used the only source lines output are those with errors.
2846 @item /LIST
2847 @cindex @option{/LIST} (@code{GNAT COMPILE})
2848 This qualifier causes a full listing of
2849 the file to be generated. The output might look as follows:
2851 @smallexample
2852 @group
2853 @cartouche
2854  1. procedure E is
2855  2.    V : Integer;
2856  3.    funcion X (Q : Integer)
2857        |
2858     >>> Incorrect spelling of keyword "function"
2859  4.     return Integer;
2860                       |
2861     >>> ";" should be "is"
2862  5.    begin
2863  6.       return Q + Q;
2864  7.    end;
2865  8. begin
2866  9.    V := X + X;
2867 10.end E;
2868 @end cartouche
2869 @end group
2870 @end smallexample
2872 @noindent
2873 @findex SYS$ERROR
2874 When you specify the @option{/REPORT_ERRORS=VERBOSE} or @option{/LIST} qualifiers and
2875 standard output is redirected, a brief summary is written to
2876 @file{SYS$ERROR} (standard error) giving the number of error messages and
2877 warning messages generated.
2879 @item /UNIQUE_ERROR_TAG
2880 @cindex @option{/UNIQUE_ERROR_TAG} (@code{GNAT COMPILE})
2881 This qualifier forces all error messages to be preceded by the unique
2882 string "error:". This means that error messages take a few more
2883 characters in space, but allows easy searching for and identification
2884 of error messages.
2886 @item /WARNINGS=BRIEF
2887 @cindex @option{/WARNINGS=BRIEF} (@code{GNAT COMPILE})
2888 This qualifier causes GNAT to generate the
2889 brief format error messages to @file{SYS$ERROR} (the standard error
2890 file) as well as the verbose
2891 format message or full listing (which as usual is written to
2892 @file{SYS$OUTPUT} (the standard output file).
2894 @item /ERROR_LIMIT=@var{n}
2895 @cindex @option{/ERROR_LIMIT} (@code{GNAT COMPILE})
2896 @var{n} is a decimal integer in the
2897 range of 1 to 999 and limits the number of error messages to be
2898 generated. For example, using @option{/ERROR_LIMIT=2} might yield
2900 @smallexample
2901 @iftex
2902 @leftskip=.7cm
2903 @end iftex
2904 E.ADB:3:04: Incorrect spelling of keyword "function"
2905 E.ADB:5:35: missing ".."
2906 fatal error: maximum errors reached
2907 compilation abandoned
2908 @end smallexample
2910 @item /REPORT_ERRORS=FULL
2911 @cindex @option{/REPORT_ERRORS=FULL} (@code{GNAT COMPILE})
2912 @cindex Error messages, suppressing
2913 Normally, the compiler suppresses error messages that are likely to be
2914 redundant. This qualifier causes all error
2915 messages to be generated. In particular, in the case of
2916 references to undefined variables. If a given variable is referenced
2917 several times, the normal format of messages is
2918 @smallexample
2919 @iftex
2920 @leftskip=.7cm
2921 @end iftex
2922 E.ADB:7:07: "V" is undefined (more references follow)
2923 @end smallexample
2925 @noindent
2926 where the parenthetical comment warns that there are additional
2927 references to the variable @code{V}. Compiling the same program with the
2928 @option{/REPORT_ERRORS=FULL} qualifier yields
2930 @smallexample
2931 E.ADB:7:07: "V" is undefined
2932 E.ADB:8:07: "V" is undefined
2933 E.ADB:8:12: "V" is undefined
2934 E.ADB:8:16: "V" is undefined
2935 E.ADB:9:07: "V" is undefined
2936 E.ADB:9:12: "V" is undefined
2937 @end smallexample
2939 @item /TRY_SEMANTICS
2940 @cindex @option{/TRY_SEMANTICS} (@code{GNAT COMPILE})
2941 In normal operation mode, the compiler first parses the program and
2942 determines if there are any syntax errors. If there are, appropriate
2943 error messages are generated and compilation is immediately terminated.
2944 This qualifier tells
2945 GNAT to continue with semantic analysis even if syntax errors have been
2946 found. This may enable the detection of more errors in a single run. On
2947 the other hand, the semantic analyzer is more likely to encounter some
2948 internal fatal error when given a syntactically invalid tree.
2950 @item /FORCE_ALI
2951 In normal operation mode, the @file{ali} file is not generated if any
2952 illegalities are detected in the program. The use of @option{/FORCE_ALI} forces
2953 generation of the @file{ali} file. This file is marked as being in
2954 error, so it cannot be used for binding purposes, but it does contain
2955 reasonably complete cross-reference information, and thus may be useful
2956 for use by tools (e.g. semantic browsing tools or integrated development
2957 environments) that are driven from the @file{ali} file.
2959 In addition, if @option{/TREE_OUTPUT} is also specified, then the tree file is
2960 generated even if there are illegalities. It may be useful in this case
2961 to also specify @option{/TRY_SEMANTICS} to ensure that full semantic processing
2962 occurs. The resulting tree file can be processed by ASIS, for the purpose
2963 of providing partial information about illegal units, but if the error
2964 causes the tree to be badly malformed, then ASIS may crash during the
2965 analysis.
2967 @end table
2969 @noindent
2970 In addition to error messages, which correspond to illegalities as defined
2971 in the Ada 95 Reference Manual, the compiler detects two kinds of warning
2972 situations.
2974 @cindex Warning messages
2975 First, the compiler considers some constructs suspicious and generates a
2976 warning message to alert you to a possible error. Second, if the
2977 compiler detects a situation that is sure to raise an exception at
2978 run time, it generates a warning message. The following shows an example
2979 of warning messages:
2980 @smallexample
2981 @iftex
2982 @leftskip=.2cm
2983 @end iftex
2984 E.ADB:4:24: warning: creation of object may raise Storage_Error
2985 E.ADB:10:17: warning: static value out of range
2986 E.ADB:10:17: warning: "Constraint_Error" will be raised at run time
2988 @end smallexample
2990 @noindent
2991 GNAT considers a large number of situations as appropriate
2992 for the generation of warning messages. As always, warnings are not
2993 definite indications of errors. For example, if you do an out-of-range
2994 assignment with the deliberate intention of raising a
2995 @code{Constraint_Error} exception, then the warning that may be
2996 issued does not indicate an error. Some of the situations for which GNAT
2997 issues warnings (at least some of the time) are given in the following
2998 list, which is not necessarily complete.
3000 @itemize @bullet
3001 @item
3002 Possible infinitely recursive calls
3004 @item
3005 Out-of-range values being assigned
3007 @item
3008 Possible order of elaboration problems
3010 @item
3011 Unreachable code
3013 @item
3014 Fixed-point type declarations with a null range
3016 @item
3017 Variables that are never assigned a value
3019 @item
3020 Variables that are referenced before being initialized
3022 @item
3023 Task entries with no corresponding accept statement
3025 @item
3026 Duplicate accepts for the same task entry in a select
3028 @item
3029 Objects that take too much storage
3031 @item
3032 Unchecked conversion between types of differing sizes
3034 @item
3035 Missing return statements along some execution paths in a function
3037 @item
3038 Incorrect (unrecognized) pragmas
3040 @item
3041 Incorrect external names
3043 @item
3044 Allocation from empty storage pool
3046 @item
3047 Potentially blocking operations in protected types
3049 @item
3050 Suspicious parenthesization of expressions
3052 @item
3053 Mismatching bounds in an aggregate
3055 @item
3056 Attempt to return local value by reference
3058 @item
3059 Unrecognized pragmas
3061 @item
3062 Premature instantiation of a generic body
3064 @item
3065 Attempt to pack aliased components
3067 @item
3068 Out of bounds array subscripts
3070 @item
3071 Wrong length on string assignment
3073 @item
3074 Violations of style rules if style checking is enabled
3076 @item
3077 Unused with clauses
3079 @item
3080 Bit_Order usage that does not have any effect
3082 @item
3083 Compile time biased rounding of floating-point constant
3085 @item
3086 Standard.Duration used to resolve universal fixed expression
3088 @item
3089 Dereference of possibly null value
3091 @item
3092 Declaration that is likely to cause storage error
3094 @item
3095 Internal GNAT unit with'ed by application unit
3097 @item
3098 Values known to be out of range at compile time
3100 @item
3101 Unreferenced labels and variables
3103 @item
3104 Address overlays that could clobber memory
3106 @item
3107 Unexpected initialization when address clause present
3109 @item
3110 Bad alignment for address clause
3112 @item
3113 Useless type conversions
3115 @item
3116 Redundant assignment statements
3118 @item
3119 Accidental hiding of name by child unit
3121 @item
3122 Unreachable code
3124 @item
3125 Access before elaboration detected at compile time
3127 @item
3128 A range in a @code{for} loop that is known to be null or might be null
3130 @end itemize
3132 @noindent
3133 The following qualifiers are available to control the handling of
3134 warning messages:
3136 @table @code
3137 @item /WARNINGS=OPTIONAL (activate all optional errors)
3138 @cindex @option{/WARNINGS=OPTIONAL} (@code{GNAT COMPILE})
3139 This qualifier activates most optional warning messages, see remaining list
3140 in this section for details on optional warning messages that can be
3141 individually controlled. The warnings that are not turned on by this
3142 qualifier are @option{/WARNINGS=BIASED_ROUNDING} (biased rounding),
3143 @option{/WARNINGS=IMPLICIT_DEREFERENCE} (implicit dereferencing),
3144 and @option{/WARNINGS=HIDING} (hiding). All other optional warnings are
3145 turned on.
3147 @item /WARNINGS=NOOPTIONAL (suppress all optional errors)
3148 @cindex @option{/WARNINGS=NOOPTIONAL} (@code{GNAT COMPILE})
3149 This qualifier suppresses all optional warning messages, see remaining list
3150 in this section for details on optional warning messages that can be
3151 individually controlled.
3153 @item /WARNINGS=BIASED_ROUNDING (activate warnings on biased rounding)
3154 @cindex @option{/WARNINGS=BIASED_ROUNDING} (@code{GNAT COMPILE})
3155 @cindex Rounding, biased
3156 @cindex Biased rounding
3157 If a static floating-point expression has a value that is exactly half
3158 way between two adjacent machine numbers, then the rules of Ada
3159 (Ada Reference Manual, section 4.9(38)) require that this rounding
3160 be done away from zero, even if the normal unbiased rounding rules
3161 at run time would require rounding towards zero. This warning message
3162 alerts you to such instances where compile-time rounding and run-time
3163 rounding are not equivalent. If it is important to get proper run-time
3164 rounding, then you can force this by making one of the operands into
3165 a variable. The default is that such warnings are not generated.
3166 Note that @option{/WARNINGS=OPTIONAL} does not affect the setting of
3167 this warning option.
3169 @item /WARNINGS=NOBIASED_ROUNDING (suppress warnings on biased rounding)
3170 @cindex @option{/WARNINGS=NOBIASED_ROUNDING} (@code{GNAT COMPILE})
3171 This qualifier disables warnings on biased rounding.
3173 @item /WARNINGS=CONDITIONALS (activate warnings on conditionals)
3174 @cindex @option{/WARNINGS=CONDITIONALS} (@code{GNAT COMPILE})
3175 @cindex Conditionals, constant
3176 This qualifier activates warnings for conditional expressions used in
3177 tests that are known to be True or False at compile time. The default
3178 is that such warnings are not generated.
3179 This warning can also be turned on using @option{/WARNINGS=OPTIONAL}.
3181 @item /WARNINGS=NOCONDITIONALS (suppress warnings on conditionals)
3182 @cindex @option{/WARNINGS=NOCONDITIONALS} (@code{GNAT COMPILE})
3183 This qualifier suppresses warnings for conditional expressions used in
3184 tests that are known to be True or False at compile time.
3186 @item /WARNINGS=IMPLICIT_DEREFERENCE (activate warnings on implicit dereferencing)
3187 @cindex @option{/WARNINGS=IMPLICIT_DEREFERENCE} (@code{GNAT COMPILE})
3188 If this qualifier is set, then the use of a prefix of an access type
3189 in an indexed component, slice, or selected component without an
3190 explicit @code{.all} will generate a warning. With this warning
3191 enabled, access checks occur only at points where an explicit
3192 @code{.all} appears in the source code (assuming no warnings are
3193 generated as a result of this qualifier). The default is that such
3194 warnings are not generated.
3195 Note that @option{/WARNINGS=OPTIONAL} does not affect the setting of
3196 this warning option.
3198 @item /WARNINGS=NOIMPLICIT_DEREFERENCE (suppress warnings on implicit dereferencing)
3199 @cindex @option{/WARNINGS=NOIMPLICIT_DEREFERENCE} (@code{GNAT COMPILE})
3200 @cindex Implicit dereferencing
3201 @cindex Dereferencing, implicit
3202 This qualifier suppresses warnings for implicit deferences in
3203 indexed components, slices, and selected components.
3205 @item /WARNINGS=ERROR (treat warnings as errors)
3206 @cindex @option{/WARNINGS=ERROR} (@code{GNAT COMPILE})
3207 @cindex Warnings, treat as error
3208 This qualifier causes warning messages to be treated as errors.
3209 The warning string still appears, but the warning messages are counted
3210 as errors, and prevent the generation of an object file.
3212 @item /WARNINGS=UNREFERENCED_FORMALS (activate warnings on unreferenced formals)
3213 @cindex @option{/WARNINGS=UNREFERENCED_FORMALS} (@code{GNAT COMPILE})
3214 @cindex Formals, unreferenced
3215 This qualifier causes a warning to be generated if a formal parameter
3216 is not referenced in the body of the subprogram. This warning can
3217 also be turned on using @option{/WARNINGS=OPTIONAL} or @option{/WARNINGS=UNUSED}.
3219 @item /WARNINGS=NOUNREFERENCED_FORMALS (suppress warnings on unreferenced formals)
3220 @cindex @option{/WARNINGS=NOUNREFERENCED_FORMALS} (@code{GNAT COMPILE})
3221 This qualifier suppresses warnings for unreferenced formal
3222 parameters. Note that the
3223 combination @option{/WARNINGS=UNUSED} followed by @option{/WARNINGS=NOUNREFERENCED_FORMALS} has the
3224 effect of warning on unreferenced entities other than subprogram
3225 formals.
3227 @item /WARNINGS=HIDING (activate warnings on hiding)
3228 @cindex @option{/WARNINGS=HIDING} (@code{GNAT COMPILE})
3229 @cindex Hiding of Declarations
3230 This qualifier activates warnings on hiding declarations.
3231 A declaration is considered hiding
3232 if it is for a non-overloadable entity, and it declares an entity with the
3233 same name as some other entity that is directly or use-visible. The default
3234 is that such warnings are not generated.
3235 Note that @option{/WARNINGS=OPTIONAL} does not affect the setting of this warning option.
3237 @item /WARNINGS=NOHIDING (suppress warnings on hiding)
3238 @cindex @option{/WARNINGS=NOHIDING} (@code{GNAT COMPILE})
3239 This qualifier suppresses warnings on hiding declarations.
3241 @item /WARNINGS=IMPLEMENTATION (activate warnings on implementation units).
3242 @cindex @option{/WARNINGS=IMPLEMENTATION} (@code{GNAT COMPILE})
3243 This qualifier activates warnings for a @code{with} of an internal GNAT
3244 implementation unit, defined as any unit from the @code{Ada},
3245 @code{Interfaces}, @code{GNAT},
3246 @code{DEC}, or @code{System}
3247 hierarchies that is not
3248 documented in either the Ada Reference Manual or the GNAT
3249 Programmer's Reference Manual. Such units are intended only
3250 for internal implementation purposes and should not be @code{with}'ed
3251 by user programs. The default is that such warnings are generated
3252 This warning can also be turned on using @option{/WARNINGS=OPTIONAL}.
3254 @item /WARNINGS=NOIMPLEMENTATION (disable warnings on implementation units).
3255 @cindex @option{/WARNINGS=NOIMPLEMENTATION} (@code{GNAT COMPILE})
3256 This qualifier disables warnings for a @code{with} of an internal GNAT
3257 implementation unit.
3259 @item /WARNINGS=ELABORATION (activate warnings on elaboration pragmas)
3260 @cindex @option{/WARNINGS=ELABORATION} (@code{GNAT COMPILE})
3261 @cindex Elaboration, warnings
3262 This qualifier activates warnings on missing pragma Elaborate_All statements.
3263 See the section in this guide on elaboration checking for details on
3264 when such pragma should be used. The default is that such warnings
3265 are not generated.
3266 This warning can also be turned on using @option{/WARNINGS=OPTIONAL}.
3268 @item /WARNINGS=NOELABORATION (suppress warnings on elaboration pragmas)
3269 @cindex @option{/WARNINGS=NOELABORATION} (@code{GNAT COMPILE})
3270 This qualifier suppresses warnings on missing pragma Elaborate_All statements.
3271 See the section in this guide on elaboration checking for details on
3272 when such pragma should be used.
3274 @item /WARNINGS=OVERLAYS (activate warnings on address clause overlays)
3275 @cindex @option{/WARNINGS=OVERLAYS} (@code{GNAT COMPILE})
3276 @cindex Address Clauses, warnings
3277 This qualifier activates warnings for possibly unintended initialization
3278 effects of defining address clauses that cause one variable to overlap
3279 another. The default is that such warnings are generated.
3280 This warning can also be turned on using @option{/WARNINGS=OPTIONAL}.
3282 @item /WARNINGS=NOOVERLAYS (suppress warnings on address clause overlays)
3283 @cindex @option{/WARNINGS=NOOVERLAYS} (@code{GNAT COMPILE})
3284 This qualifier suppresses warnings on possibly unintended initialization
3285 effects of defining address clauses that cause one variable to overlap
3286 another.
3288 @item -gnatwp (activate warnings on ineffective pragma Inlines)
3289 @cindex @option{-gnatwp} (@code{GNAT COMPILE})
3290 @cindex Inlining, warnings
3291 This qualifier activates warnings for failure of front end inlining
3292 (activated by @option{-gnatN}) to inline a particular call. There are
3293 many reasons for not being able to inline a call, including most
3294 commonly that the call is too complex to inline.
3295 This warning can also be turned on using @option{/WARNINGS=OPTIONAL}.
3297 @item -gnatwP (suppress warnings on ineffective pragma Inlines)
3298 @cindex @option{-gnatwP} (@code{GNAT COMPILE})
3299 This qualifier suppresses warnings on ineffective pragma Inlines. If the
3300 inlining mechanism cannot inline a call, it will simply ignore the
3301 request silently.
3303 @item /WARNINGS=REDUNDANT (activate warnings on redundant constructs)
3304 @cindex @option{/WARNINGS=REDUNDANT} (@code{GNAT COMPILE})
3305 This qualifier activates warnings for redundant constructs. The following
3306 is the current list of constructs regarded as redundant:
3307 This warning can also be turned on using @option{/WARNINGS=OPTIONAL}.
3309 @itemize @bullet
3310 @item
3311 Assignment of an item to itself.
3312 @item
3313 Type conversion that converts an expression to its own type.
3314 @item
3315 Use of the attribute @code{Base} where @code{typ'Base} is the same
3316 as @code{typ}.
3317 @item
3318 Use of pragma @code{Pack} when all components are placed by a record
3319 representation clause.
3320 @end itemize
3322 @item /WARNINGS=NOREDUNDANT (suppress warnings on redundant constructs)
3323 @cindex @option{/WARNINGS=NOREDUNDANT} (@code{GNAT COMPILE})
3324 This qualifier suppresses warnings for redundant constructs.
3326 @item /WARNINGS=SUPPRESS (suppress all warnings)
3327 @cindex @option{/WARNINGS=SUPPRESS} (@code{GNAT COMPILE})
3328 This qualifier completely suppresses the
3329 output of all warning messages from the GNAT front end.
3330 Note that it does not suppress warnings from the @code{GNAT COMPILE} back end.
3331 To suppress these back end warnings as well, use the qualifier @code{-w}
3332 in addition to @option{/WARNINGS=SUPPRESS}.
3334 @item /WARNINGS=UNUSED (activate warnings on unused entities)
3335 @cindex @option{/WARNINGS=UNUSED} (@code{GNAT COMPILE})
3336 This qualifier activates warnings to be generated for entities that
3337 are defined but not referenced, and for units that are @code{with}'ed
3338 and not
3339 referenced. In the case of packages, a warning is also generated if
3340 no entities in the package are referenced. This means that if the package
3341 is referenced but the only references are in @code{use}
3342 clauses or @code{renames}
3343 declarations, a warning is still generated. A warning is also generated
3344 for a generic package that is @code{with}'ed but never instantiated.
3345 In the case where a package or subprogram body is compiled, and there
3346 is a @code{with} on the corresponding spec
3347 that is only referenced in the body,
3348 a warning is also generated, noting that the
3349 @code{with} can be moved to the body. The default is that
3350 such warnings are not generated.
3351 This qualifier also activates warnings on unreferenced formals
3352 (it is includes the effect of @option{/WARNINGS=UNREFERENCED_FORMALS}).
3353 This warning can also be turned on using @option{/WARNINGS=OPTIONAL}.
3355 @item /WARNINGS=NOUNUSED (suppress warnings on unused entities)
3356 @cindex @option{/WARNINGS=NOUNUSED} (@code{GNAT COMPILE})
3357 This qualifier suppresses warnings for unused entities and packages.
3358 It also turns off warnings on unreferenced formals (and thus includes
3359 the effect of @option{/WARNINGS=NOUNREFERENCED_FORMALS}).
3361 @noindent
3362 A string of warning parameters can be used in the same parameter. For example:
3364 @smallexample
3365 -gnatwaLe
3366 @end smallexample
3368 @noindent
3369 Would turn on all optional warnings except for elaboration pragma warnings,
3370 and also specify that warnings should be treated as errors.
3372 @item -w
3373 @cindex @code{-w}
3374 This qualifier suppresses warnings from the @code{GNAT COMPILE} backend. It may be
3375 used in conjunction with @option{/WARNINGS=SUPPRESS} to ensure that all warnings
3376 are suppressed during the entire compilation process.
3378 @end table
3380 @node Debugging and Assertion Control
3381 @subsection Debugging and Assertion Control
3383 @table @code
3384 @item /CHECKS=ASSERTIONS
3385 @cindex @option{/CHECKS=ASSERTIONS} (@code{GNAT COMPILE})
3386 @findex Assert
3387 @findex Debug
3388 @cindex Assertions
3390 @noindent
3391 The pragmas @code{Assert} and @code{Debug} normally have no effect and
3392 are ignored. This qualifier, where @samp{a} stands for assert, causes
3393 @code{Assert} and @code{Debug} pragmas to be activated.
3395 The pragmas have the form:
3397 @smallexample
3398 @group
3399 @cartouche
3400    @b{pragma} Assert (@var{Boolean-expression} [,
3401                       @var{static-string-expression}])
3402    @b{pragma} Debug (@var{procedure call})
3403 @end cartouche
3404 @end group
3405 @end smallexample
3407 @noindent
3408 The @code{Assert} pragma causes @var{Boolean-expression} to be tested.
3409 If the result is @code{True}, the pragma has no effect (other than
3410 possible side effects from evaluating the expression). If the result is
3411 @code{False}, the exception @code{Assert_Failure} declared in the package
3412 @code{System.Assertions} is
3413 raised (passing @var{static-string-expression}, if present, as the
3414 message associated with the exception). If no string expression is
3415 given the default is a string giving the file name and line number
3416 of the pragma.
3418 The @code{Debug} pragma causes @var{procedure} to be called. Note that
3419 @code{pragma Debug} may appear within a declaration sequence, allowing
3420 debugging procedures to be called between declarations.
3422 @item /DEBUG[=debug-level]
3423 @itemx  /NODEBUG
3424 Specifies how much debugging information is to be included in
3425 the resulting object file where 'debug-level' is one of the following:
3426 @table @code
3427 @item   TRACEBACK (default)
3428 Include both debugger symbol records and traceback
3429 the object file.
3430 @item   ALL
3431 Include both debugger symbol records and traceback in
3432 object file.
3433 @item   NONE
3434 Excludes both debugger symbol records and traceback
3435 the object file. Same as /NODEBUG.
3436 @item   SYMBOLS
3437 Includes only debugger symbol records in the object
3438 file. Note that this doesn't include traceback information.
3439 @end table
3440 @end table
3442 @node Validity Checking
3443 @subsection Validity Checking
3444 @findex Validity Checking
3446 @noindent
3447 The Ada 95 Reference Manual has specific requirements for checking
3448 for invalid values. In particular, RM 13.9.1 requires that the
3449 evaluation of invalid values (for example from unchecked conversions),
3450 not result in erroneous execution. In GNAT, the result of such an
3451 evaluation in normal default mode is to either use the value
3452 unmodified, or to raise Constraint_Error in those cases where use
3453 of the unmodified value would cause erroneous execution. The cases
3454 where unmodified values might lead to erroneous execution are case
3455 statements (where a wild jump might result from an invalid value),
3456 and subscripts on the left hand side (where memory corruption could
3457 occur as a result of an invalid value).
3459 The @option{-gnatVx} qualifier allows more control over the validity checking
3460 mode. The @code{x} argument here is a string of letters which control which
3461 validity checks are performed in addition to the default checks described
3462 above.
3464 @itemize @bullet
3465 @item
3466 @option{-gnatVc} Validity checks for copies
3468 The right hand side of assignments, and the initializing values of
3469 object declarations are validity checked.
3471 @item
3472 @option{/VALIDITY_CHECKING=RM} Default (RM) validity checks
3474 Some validity checks are done by default following normal Ada semantics
3475 (RM 13.9.1 (9-11)).
3476 A check is done in case statements that the expression is within the range
3477 of the subtype. If it is not, Constraint_Error is raised.
3478 For assignments to array components, a check is done that the expression used
3479 as index is within the range. If it is not, Constraint_Error is raised.
3480 Both these validity checks may be turned off using qualifier @option{-gnatVD}.
3481 They are turned on by default. If @option{-gnatVD} is specified, a subsequent
3482 qualifier @option{/VALIDITY_CHECKING=RM} will leave the checks turned on.
3483 Qualifier @option{-gnatVD} should be used only if you are sure that all such
3484 expressions have valid values. If you use this qualifier and invalid values
3485 are present, then the program is erroneous, and wild jumps or memory
3486 overwriting may occur.
3488 @item
3489 @option{-gnatVi} Validity checks for @code{in} mode parameters
3491 Arguments for parameters of mode @code{in} are validity checked in function
3492 and procedure calls at the point of call.
3494 @item
3495 @option{-gnatVm} Validity checks for @code{in out} mode parameters
3497 Arguments for parameters of mode @code{in out} are validity checked in
3498 procedure calls at the point of call. The @code{'m'} here stands for
3499 modify, since this concerns parameters that can be modified by the call.
3500 Note that there is no specific option to test @code{out} parameters,
3501 but any reference within the subprogram will be tested in the usual
3502 manner, and if an invalid value is copied back, any reference to it
3503 will be subject to validity checking.
3505 @item
3506 @option{-gnatVo} Validity checks for operator and attribute operands
3508 Arguments for predefined operators and attributes are validity checked.
3509 This includes all operators in package @code{Standard},
3510 the shift operators defined as intrinsic in package @code{Interfaces}
3511 and operands for attributes such as @code{Pos}.
3513 @item
3514 @option{-gnatVr} Validity checks for function returns
3516 The expression in @code{return} statements in functions is validity
3517 checked.
3519 @item
3520 @option{-gnatVs} Validity checks for subscripts
3522 All subscripts expressions are checked for validity, whether they appear
3523 on the right side or left side (in default mode only left side subscripts
3524 are validity checked).
3526 @item
3527 @option{-gnatVt} Validity checks for tests
3529 Expressions used as conditions in @code{if}, @code{while} or @code{exit}
3530 statements are checked, as well as guard expressions in entry calls.
3532 @item
3533 @option{/VALIDITY_CHECKING=FULL} Validity checks for floating-point values
3535 In the absence of this qualifier, validity checking occurs only for discrete
3536 values. If @option{/VALIDITY_CHECKING=FULL} is specified, then validity checking also applies
3537 for floating-point values, and NaN's and infinities are considered invalid,
3538 as well as out of range values for constrained types. Note that this means
3539 that standard @code{IEEE} infinity mode is not allowed. The exact contexts
3540 in which floating-point values are checked depends on the setting of other
3541 options. For example @option{-gnatVif} or @option{-gnatVfi} (the order does
3542 not matter) specifies that floating-point parameters of mode @code{in} should
3543 be validity checked.
3545 @item
3546 @option{-gnatVa} All validity checks
3548 All the above validity checks are turned on. That is @option{-gnatVa} is
3549 equivalent to @code{gnatVcdfimorst}.
3551 @item
3552 @option{-gnatVn} No validity checks
3554 This qualifier turns off all validity checking, including the default checking
3555 for case statements and left hand side subscripts. Note that the use of
3556 the qualifier @option{/CHECKS=SUPPRESS_ALL} supresses all run-time checks, including
3557 validity checks, and thus implies @option{-gnatVn}.
3559 @end itemize
3561 The @option{/VALIDITY_CHECKING} qualifier may be followed by a string of letters to turn on
3562 a series of validity checking options. For example, @option{-gnatVcr} specifies
3563 that in addition to the default validity checking, copies and function
3564 return expressions be validity checked. In order to make it easier to specify
3565 a set of options, the upper case letters @code{CDFIMORST} may be used to turn
3566 off the corresponding lower case option, so for example @option{-gnatVaM} turns
3567 on all validity checking options except for checking of @code{in out}
3568 procedure arguments.
3570 The specification of additional validity checking generates extra code (and
3571 in the case of @option{-gnatva} the code expansion can be substantial. However,
3572 these additional checks can be very useful in smoking out cases of
3573 uninitialized variables, incorrect use of unchecked conversion, and other
3574 errors leading to invalid values. The use of pragma @code{Initialize_Scalars}
3575 is useful in conjunction with the extra validity checking, since this
3576 ensures that wherever possible uninitialized variables have invalid values.
3578 See also the pragma @code{Validity_Checks} which allows modification of
3579 the validity checking mode at the program source level, and also allows for
3580 temporary disabling of validity checks.
3582 @node Style Checking
3583 @subsection Style Checking
3584 @findex Style checking
3586 @noindent
3587 The /STYLE=@var{(option,option,..)} qualifier causes the compiler to
3588 enforce specified style rules. A limited set of style rules has been used
3589 in writing the GNAT sources themselves. This qualifier allows user programs
3590 to activate all or some of these checks. If the source program fails a
3591 specified style check, an appropriate warning message is given, preceded by
3592 the character sequence "(style)".
3593 (OPTION,OPTION,..) is a sequence of keywords
3594 indicating the particular style
3595 checks to be performed. The following checks are defined:
3597 @table @code
3598 @item 1-9 (specify indentation level)
3599 If a digit from 1-9 appears in the string after @option{/STYLE=} then proper
3600 indentation is checked, with the digit indicating the indentation level
3601 required. The general style of required indentation is as specified by
3602 the examples in the Ada Reference Manual. Full line comments must be
3603 aligned with the @code{--} starting on a column that is a multiple of
3604 the alignment level.
3606 @item ATTRIBUTE (check attribute casing)
3607 If the word ATTRIBUTE appears in the string after @option{/STYLE=} then
3608 attribute names, including the case of keywords such as @code{digits}
3609 used as attributes names, must be written in mixed case, that is, the
3610 initial letter and any letter following an underscore must be uppercase.
3611 All other letters must be lowercase.
3613 @item BLANKS (blanks not allowed at statement end)
3614 If the word BLANKS appears in the string after @option{/STYLE=} then
3615 trailing blanks are not allowed at the end of statements. The purpose of this
3616 rule, together with h (no horizontal tabs), is to enforce a canonical format
3617 for the use of blanks to separate source tokens.
3619 @item COMMENTS (check comments)
3620 If the word COMMENTS appears in the string after @option{/STYLE=} then
3621 comments must meet the following set of rules:
3623 @itemize @bullet
3625 @item
3626 The "--" that starts the column must either start in column one, or else
3627 at least one blank must precede this sequence.
3629 @item
3630 Comments that follow other tokens on a line must have at least one blank
3631 following the "--" at the start of the comment.
3633 @item
3634 Full line comments must have two blanks following the "--" that starts
3635 the comment, with the following exceptions.
3637 @item
3638 A line consisting only of the "--" characters, possibly preceded by blanks
3639 is permitted.
3641 @item
3642 A comment starting with "--x" where x is a special character is permitted.
3643 This alows proper processing of the output generated by specialized tools
3644 including @code{GNAT PREPROCESS} (where --! is used) and the SPARK annnotation
3645 language (where --# is used). For the purposes of this rule, a special
3646 character is defined as being in one of the ASCII ranges
3647 16#21#..16#2F# or 16#3A#..16#3F#.
3649 @item
3650 A line consisting entirely of minus signs, possibly preceded by blanks, is
3651 permitted. This allows the construction of box comments where lines of minus
3652 signs are used to form the top and bottom of the box.
3654 @item
3655 If a comment starts and ends with "--" is permitted as long as at least
3656 one blank follows the initial "--". Together with the preceding rule,
3657 this allows the construction of box comments, as shown in the following
3658 example:
3659 @smallexample
3660 ---------------------------
3661 -- This is a box comment --
3662 -- with two text lines.  --
3663 ---------------------------
3664 @end smallexample
3665 @end itemize
3667 @item END (check end/exit labels)
3668 If the word END appears in the string after @option{/STYLE=} then
3669 optional labels on @code{end} statements ending subprograms and on
3670 @code{exit} statements exiting named loops, are required to be present.
3672 @item VTABS (no form feeds or vertical tabs)
3673 If the word VTABS appears in the string after @option{/STYLE=} then
3674 neither form feeds nor vertical tab characters are not permitted
3675 in the source text.
3677 @item HTABS (no horizontal tabs)
3678 If the word HTABS appears in the string after @option{/STYLE=} then
3679 horizontal tab characters are not permitted in the source text.
3680 Together with the b (no blanks at end of line) check, this
3681 enforces a canonical form for the use of blanks to separate
3682 source tokens.
3684 @item IF_THEN (check if-then layout)
3685 If the word IF_THEN appears in the string after @option{/STYLE=},
3686 then the keyword @code{then} must appear either on the same
3687 line as corresponding @code{if}, or on a line on its own, lined
3688 up under the @code{if} with at least one non-blank line in between
3689 containing all or part of the condition to be tested.
3691 @item KEYWORD (check keyword casing)
3692 If the word KEYWORD appears in the string after @option{/STYLE=} then
3693 all keywords must be in lower case (with the exception of keywords
3694 such as @code{digits} used as attribute names to which this check
3695 does not apply).
3697 @item LAYOUT (check layout)
3698 If the word LAYOUT appears in the string after @option{/STYLE=} then
3699 layout of statement and declaration constructs must follow the
3700 recommendations in the Ada Reference Manual, as indicated by the
3701 form of the syntax rules. For example an @code{else} keyword must
3702 be lined up with the corresponding @code{if} keyword.
3704 There are two respects in which the style rule enforced by this check
3705 option are more liberal than those in the Ada Reference Manual. First
3706 in the case of record declarations, it is permissible to put the
3707 @code{record} keyword on the same line as the @code{type} keyword, and
3708 then the @code{end} in @code{end record} must line up under @code{type}.
3709 For example, either of the following two layouts is acceptable:
3711 @smallexample
3712 @group
3713 @cartouche
3714 @b{type} q @b{is record}
3715    a : integer;
3716    b : integer;
3717 @b{end record};
3719 @b{type} q @b{is}
3720    @b{record}
3721       a : integer;
3722       b : integer;
3723    @b{end record};
3724 @end cartouche
3725 @end group
3726 @end smallexample
3728 @noindent
3729 Second, in the case of a block statement, a permitted alternative
3730 is to put the block label on the same line as the @code{declare} or
3731 @code{begin} keyword, and then line the @code{end} keyword up under
3732 the block label. For example both the following are permitted:
3734 @smallexample
3735 @group
3736 @cartouche
3737 Block : @b{declare}
3738    A : Integer := 3;
3739 @b{begin}
3740    Proc (A, A);
3741 @b{end} Block;
3743 Block :
3744    @b{declare}
3745       A : Integer := 3;
3746    @b{begin}
3747       Proc (A, A);
3748    @b{end} Block;
3749 @end cartouche
3750 @end group
3751 @end smallexample
3753 @noindent
3754 The same alternative format is allowed for loops. For example, both of
3755 the following are permitted:
3757 @smallexample
3758 @group
3759 @cartouche
3760 Clear : @b{while} J < 10 @b{loop}
3761    A (J) := 0;
3762 @b{end loop} Clear;
3764 Clear :
3765    @b{while} J < 10 @b{loop}
3766       A (J) := 0;
3767    @b{end loop} Clear;
3768 @end cartouche
3769 @end group
3770 @end smallexample
3772 @item LINE_LENGTH (check maximum line length)
3773 If the word LINE_LENGTH appears in the string after @option{/STYLE=}
3774 then the length of source lines must not exceed 79 characters, including
3775 any trailing blanks. The value of 79 allows convenient display on an
3776 80 character wide device or window, allowing for possible special
3777 treatment of 80 character lines.
3779 @item MAX_LENGTH=nnn (set maximum line length)
3780 If the sequence MAX_LENGTH=nnn, where nnn is a decimal number, appears in
3781 the string after @option{/STYLE=} then the length of lines must not exceed the
3782 given value.
3784 @item STANDARD_CASING (check casing of entities in Standard)
3785 If the word STANDARD_CASING appears in the string
3786 after @option{/STYLE=} then any identifier from Standard must be cased
3787 to match the presentation in the Ada Reference Manual (for example,
3788 @code{Integer} and @code{ASCII.NUL}).
3790 @item ORDERED_SUBPROGRAMS (check order of subprogram bodies)
3791 If the word ORDERED_SUBPROGRAMS appears in the string
3792 after @option{/STYLE=} then all subprogram bodies in a given scope
3793 (e.g. a package body) must be in alphabetical order. The ordering
3794 rule uses normal Ada rules for comparing strings, ignoring casing
3795 of letters, except that if there is a trailing numeric suffix, then
3796 the value of this suffix is used in the ordering (e.g. Junk2 comes
3797 before Junk10).
3799 @item PRAGMA (check pragma casing)
3800 If the word PRAGMA appears in the string after @option{/STYLE=} then
3801 pragma names must be written in mixed case, that is, the
3802 initial letter and any letter following an underscore must be uppercase.
3803 All other letters must be lowercase.
3805 @item REFERENCES (check references)
3806 If the word REFERENCES appears in the string after @option{/STYLE=}
3807 then all identifier references must be cased in the same way as the
3808 corresponding declaration. No specific casing style is imposed on
3809 identifiers. The only requirement is for consistency of references
3810 with declarations.
3812 @item SPECS (check separate specs)
3813 If the word SPECS appears in the string after @option{/STYLE=} then
3814 separate declarations ("specs") are required for subprograms (a
3815 body is not allowed to serve as its own declaration). The only
3816 exception is that parameterless library level procedures are
3817 not required to have a separate declaration. This exception covers
3818 the most frequent form of main program procedures.
3820 @item TOKEN (check token spacing)
3821 If the word TOKEN appears in the string after @option{/STYLE=} then
3822 the following token spacing rules are enforced:
3824 @itemize @bullet
3826 @item
3827 The keywords @code{abs} and @code{not} must be followed by a space.
3829 @item
3830 The token @code{=>} must be surrounded by spaces.
3832 @item
3833 The token @code{<>} must be preceded by a space or a left parenthesis.
3835 @item
3836 Binary operators other than @code{**} must be surrounded by spaces.
3837 There is no restriction on the layout of the @code{**} binary operator.
3839 @item
3840 Colon must be surrounded by spaces.
3842 @item
3843 Colon-equal (assignment) must be surrounded by spaces.
3845 @item
3846 Comma must be the first non-blank character on the line, or be
3847 immediately preceded by a non-blank character, and must be followed
3848 by a space.
3850 @item
3851 If the token preceding a left paren ends with a letter or digit, then
3852 a space must separate the two tokens.
3854 @item
3855 A right parenthesis must either be the first non-blank character on
3856 a line, or it must be preceded by a non-blank character.
3858 @item
3859 A semicolon must not be preceded by a space, and must not be followed by
3860 a non-blank character.
3862 @item
3863 A unary plus or minus may not be followed by a space.
3865 @item
3866 A vertical bar must be surrounded by spaces.
3867 @end itemize
3869 @noindent
3870 In the above rules, appearing in column one is always permitted, that is,
3871 counts as meeting either a requirement for a required preceding space,
3872 or as meeting a requirement for no preceding space.
3874 Appearing at the end of a line is also always permitted, that is, counts
3875 as meeting either a requirement for a following space, or as meeting
3876 a requirement for no following space.
3878 @end table
3880 @noindent
3881 If any of these style rules is violated, a message is generated giving
3882 details on the violation. The initial characters of such messages are
3883 always "(style)". Note that these messages are treated as warning
3884 messages, so they normally do not prevent the generation of an object
3885 file. The @option{/WARNINGS=ERROR} qualifier can be used to treat warning messages,
3886 including style messages, as fatal errors.
3888 @noindent
3889 The qualifier
3890 /STYLE_CHECKS=ALL_BUILTIN
3891 is equivalent to  all checking
3892 options  enabled with
3893 the exception of ORDERED_SUBPROGRAMS,
3894 with an indentation level of 3. This is the standard
3895 checking option that is used for the GNAT sources.
3897 @node Run-Time Checks
3898 @subsection Run-Time Checks
3899 @cindex Division by zero
3900 @cindex Access before elaboration
3901 @cindex Checks, division by zero
3902 @cindex Checks, access before elaboration
3904 @noindent
3905 If you compile with the default options, GNAT will insert many run-time
3906 checks into the compiled code, including code that performs range
3907 checking against constraints, but not arithmetic overflow checking for
3908 integer operations (including division by zero) or checks for access
3909 before elaboration on subprogram calls. All other run-time checks, as
3910 required by the Ada 95 Reference Manual, are generated by default.
3911 The following @code{GNAT COMPILE} qualifiers refine this default behavior:
3913 @table @code
3914 @item /CHECKS=SUPPRESS_ALL
3915 @cindex @option{/CHECKS=SUPPRESS_ALL} (@code{GNAT COMPILE})
3916 @cindex Suppressing checks
3917 @cindex Checks, suppressing
3918 @findex Suppress
3919 Suppress all run-time checks as though @code{pragma Suppress (all_checks})
3920 had been present in the source. Validity checks are also suppressed (in
3921 other words @option{/CHECKS=SUPPRESS_ALL} also implies @option{-gnatVn}.
3922 Use this qualifier to improve the performance
3923 of the code at the expense of safety in the presence of invalid data or
3924 program bugs.
3926 @item /CHECKS=OVERFLOW
3927 @cindex @option{/CHECKS=OVERFLOW} (@code{GNAT COMPILE})
3928 @cindex Overflow checks
3929 @cindex Check, overflow
3930 Enables overflow checking for integer operations.
3931 This causes GNAT to generate slower and larger executable
3932 programs by adding code to check for overflow (resulting in raising
3933 @code{Constraint_Error} as required by standard Ada
3934 semantics). These overflow checks correspond to situations in which
3935 the true value of the result of an operation may be outside the base
3936 range of the result type. The following example shows the distinction:
3938 @smallexample
3939 X1 : Integer := Integer'Last;
3940 X2 : Integer range 1 .. 5 := 5;
3942 X1 := X1 + 1;   -- @option{/CHECKS=OVERFLOW} required to catch the Constraint_Error
3943 X2 := X2 + 1;   -- range check, @option{/CHECKS=OVERFLOW} has no effect here
3944 @end smallexample
3946 @noindent
3947 Here the first addition results in a value that is outside the base range
3948 of Integer, and hence requires an overflow check for detection of the
3949 constraint error. The second increment operation results in a violation
3950 of the explicit range constraint, and such range checks are always
3951 performed. Basically the compiler can assume that in the absence of
3952 the @option{/CHECKS=OVERFLOW} qualifier that any value of type @code{xxx} is
3953 in range of the base type of @code{xxx}.
3955 @findex Machine_Overflows
3956 Note that the @option{/CHECKS=OVERFLOW} qualifier does not affect the code generated
3957 for any floating-point operations; it applies only to integer
3958 semantics).
3959 For floating-point, GNAT has the @code{Machine_Overflows}
3960 attribute set to @code{False} and the normal mode of operation is to
3961 generate IEEE NaN and infinite values on overflow or invalid operations
3962 (such as dividing 0.0 by 0.0).
3964 The reason that we distinguish overflow checking from other kinds of
3965 range constraint checking is that a failure of an overflow check can
3966 generate an incorrect value, but cannot cause erroneous behavior. This
3967 is unlike the situation with a constraint check on an array subscript,
3968 where failure to perform the check can result in random memory description,
3969 or the range check on a case statement, where failure to perform the check
3970 can cause a wild jump.
3972 Note again that @option{/CHECKS=OVERFLOW} is off by default, so overflow checking is
3973 not performed in default mode. This means that out of the box, with the
3974 default settings, GNAT does not do all the checks expected from the
3975 language description in the Ada Reference Manual. If you want all constraint
3976 checks to be performed, as described in this Manual, then you must
3977 explicitly use the /CHECKS=OVERFLOW qualifier either on the @code{GNAT MAKE} or
3978 @code{GNAT COMPILE} command.
3980 @item /CHECKS=ELABORATION
3981 @cindex @option{/CHECKS=ELABORATION} (@code{GNAT COMPILE})
3982 @cindex Elaboration checks
3983 @cindex Check, elaboration
3984 Enables dynamic checks for access-before-elaboration
3985 on subprogram calls and generic instantiations.
3986 For full details of the effect and use of this qualifier,
3987 @xref{Compiling Using GNAT COMPILE}.
3988 @end table
3990 @findex Unsuppress
3991 @noindent
3992 The setting of these qualifiers only controls the default setting of the
3993 checks. You may modify them using either @code{Suppress} (to remove
3994 checks) or @code{Unsuppress} (to add back suppressed checks) pragmas in
3995 the program source.
3997 @node Stack Overflow Checking
3998 @subsection Stack Overflow Checking
3999 @cindex Stack Overflow Checking
4000 @cindex -fstack-check
4002 @noindent
4003 For most operating systems, @code{GNAT COMPILE} does not perform stack overflow
4004 checking by default. This means that if the main environment task or
4005 some other task exceeds the available stack space, then unpredictable
4006 behavior will occur.
4008 To activate stack checking, compile all units with the GNAT COMPILE option
4009 @code{-fstack-check}. For example:
4011 @smallexample
4012 GNAT COMPILE -fstack-check PACKAGE1.ADB
4013 @end smallexample
4015 @noindent
4016 Units compiled with this option will generate extra instructions to check
4017 that any use of the stack (for procedure calls or for declaring local
4018 variables in declare blocks) do not exceed the available stack space.
4019 If the space is exceeded, then a @code{Storage_Error} exception is raised.
4021 For declared tasks, the stack size is always controlled by the size
4022 given in an applicable @code{Storage_Size} pragma (or is set to
4023 the default size if no pragma is used.
4025 For the environment task, the stack size depends on
4026 system defaults and is unknown to the compiler. The stack
4027 may even dynamically grow on some systems, precluding the
4028 normal Ada semantics for stack overflow. In the worst case,
4029 unbounded stack usage, causes unbounded stack expansion
4030 resulting in the system running out of virtual memory.
4032 The stack checking may still work correctly if a fixed
4033 size stack is allocated, but this cannot be guaranteed.
4034 To ensure that a clean exception is signalled for stack
4035 overflow, set the environment variable
4036 @code{GNAT_STACK_LIMIT} to indicate the maximum
4037 stack area that can be used, as in:
4038 @cindex GNAT_STACK_LIMIT
4040 @smallexample
4041 SET GNAT_STACK_LIMIT 1600
4042 @end smallexample
4044 @noindent
4045 The limit is given in kilobytes, so the above declaration would
4046 set the stack limit of the environment task to 1.6 megabytes.
4047 Note that the only purpose of this usage is to limit the amount
4048 of stack used by the environment task. If it is necessary to
4049 increase the amount of stack for the environment task, then this
4050 is an operating systems issue, and must be addressed with the
4051 appropriate operating systems commands.
4053 @node Run-Time Control
4054 @subsection Run-Time Control
4056 @table @code
4057 @item -gnatT nnn
4058 @cindex @option{-gnatT} (@code{GNAT COMPILE})
4059 @cindex Time Slicing
4061 @noindent
4062 The @code{gnatT} qualifier can be used to specify the time-slicing value
4063 to be used for task switching between equal priority tasks. The value
4064 @code{nnn} is given in microseconds as a decimal integer.
4066 Setting the time-slicing value is only effective if the underlying thread
4067 control system can accommodate time slicing. Check the documentation of
4068 your operating system for details. Note that the time-slicing value can
4069 also be set by use of pragma @code{Time_Slice} or by use of the
4070 @code{t} qualifier in the GNAT BIND step. The pragma overrides a command
4071 line argument if both are present, and the @code{t} qualifier for GNAT BIND
4072 overrides both the pragma and the @code{GNAT COMPILE} command line qualifier.
4073 @end table
4075 @node Using GNAT COMPILE for Syntax Checking
4076 @subsection Using @code{GNAT COMPILE} for Syntax Checking
4077 @table @code
4078 @item /SYNTAX_ONLY
4079 @cindex @option{/SYNTAX_ONLY} (@code{GNAT COMPILE})
4081 Run GNAT in syntax checking only mode. For
4082 example, the command
4084 @smallexample
4085 $ GNAT COMPILE /SYNTAX_ONLY X.ADB
4086 @end smallexample
4088 @noindent
4089 compiles file @file{X.ADB} in syntax-check-only mode. You can check a
4090 series of files in a single command
4093 You may use other qualifiers in conjunction with @option{/SYNTAX_ONLY}. In
4094 particular, @option{/LIST} and @option{/REPORT_ERRORS=VERBOSE} are useful to control the
4095 format of any generated error messages.
4097 The output is simply the error messages, if any. No object file or ALI
4098 file is generated by a syntax-only compilation. Also, no units other
4099 than the one specified are accessed. For example, if a unit @code{X}
4100 @code{with}'s a unit @code{Y}, compiling unit @code{X} in syntax
4101 check only mode does not access the source file containing unit
4102 @code{Y}.
4104 @cindex Multiple units, syntax checking
4105 Normally, GNAT allows only a single unit in a source file. However, this
4106 restriction does not apply in syntax-check-only mode, and it is possible
4107 to check a file containing multiple compilation units concatenated
4108 together. This is primarily used by the @code{GNAT CHOP} utility
4109 (@pxref{Renaming Files Using GNAT CHOP}).
4110 @end table
4112 @node Using GNAT COMPILE for Semantic Checking
4113 @subsection Using @code{GNAT COMPILE} for Semantic Checking
4114 @table @code
4115 @item /NOLOAD
4116 @cindex @option{/NOLOAD} (@code{GNAT COMPILE})
4118 Causes the compiler to operate in semantic check mode,
4119 with full checking for all illegalities specified in the
4120 Ada 95 Reference Manual, but without generation of any object code
4121 (no object file is generated).
4123 Because dependent files must be accessed, you must follow the GNAT
4124 semantic restrictions on file structuring to operate in this mode:
4126 @itemize @bullet
4127 @item
4128 The needed source files must be accessible
4129 (@pxref{Search Paths and the Run-Time Library (RTL)}).
4131 @item
4132 Each file must contain only one compilation unit.
4134 @item
4135 The file name and unit name must match (@pxref{File Naming Rules}).
4136 @end itemize
4138 The output consists of error messages as appropriate. No object file is
4139 generated. An @file{ALI} file is generated for use in the context of
4140 cross-reference tools, but this file is marked as not being suitable
4141 for binding (since no object file is generated).
4142 The checking corresponds exactly to the notion of
4143 legality in the Ada 95 Reference Manual.
4145 Any unit can be compiled in semantics-checking-only mode, including
4146 units that would not normally be compiled (subunits,
4147 and specifications where a separate body is present).
4148 @end table
4150 @node Compiling Ada 83 Programs
4151 @subsection Compiling Ada 83 Programs
4152 @table @code
4153 @cindex Ada 83 compatibility
4154 @item /83
4155 @cindex @option{/83} (@code{GNAT COMPILE})
4156 @cindex ACVC, Ada 83 tests
4158 @noindent
4159 Although GNAT is primarily an Ada 95 compiler, it accepts this qualifier to
4160 specify that an Ada 83 program is to be compiled in Ada83 mode. If you specify
4161 this qualifier, GNAT rejects most Ada 95 extensions and applies Ada 83 semantics
4162 where this can be done easily.
4163 It is not possible to guarantee this qualifier does a perfect
4164 job; for example, some subtle tests, such as are
4165 found in earlier ACVC tests (that have been removed from the ACVC suite for Ada
4166 95), may not compile correctly. However, for most purposes, using
4167 this qualifier should help to ensure that programs that compile correctly
4168 under the @option{/83} qualifier can be ported easily to an Ada 83
4169 compiler. This is the main use of the qualifier.
4171 With few exceptions (most notably the need to use @code{<>} on
4172 @cindex Generic formal parameters
4173 unconstrained generic formal parameters, the use of the new Ada 95
4174 keywords, and the use of packages
4175 with optional bodies), it is not necessary to use the
4176 @option{/83} qualifier when compiling Ada 83 programs, because, with rare
4177 exceptions, Ada 95 is upwardly compatible with Ada 83. This
4178 means that a correct Ada 83 program is usually also a correct Ada 95
4179 program.
4181 @end table
4183 @node Character Set Control
4184 @subsection Character Set Control
4185 @table @code
4186 @item /IDENTIFIER_CHARACTER_SET=@var{c}
4187 @cindex @code{/IDENTIFIER_CHARACTER_SET} (@code{GNAT COMPILE})
4189 @noindent
4190 Normally GNAT recognizes the Latin-1 character set in source program
4191 identifiers, as described in the Ada 95 Reference Manual.
4192 This qualifier causes
4193 GNAT to recognize alternate character sets in identifiers. @var{c} is a
4194 single character or word indicating the character set, as follows:
4196 @table @code
4197 @item 1
4198 Latin-1 identifiers
4200 @item 2
4201 Latin-2 letters allowed in identifiers
4203 @item 3
4204 Latin-3 letters allowed in identifiers
4206 @item 4
4207 Latin-4 letters allowed in identifiers
4209 @item 5
4210 Latin-5 (Cyrillic) letters allowed in identifiers
4212 @item 9
4213 Latin-9 letters allowed in identifiers
4215 @item PC
4216 IBM PC letters (code page 437) allowed in identifiers
4218 @item PC850
4219 IBM PC letters (code page 850) allowed in identifiers
4221 @item FULL_UPPER
4222 Full upper-half codes allowed in identifiers
4224 @item NO_UPPER
4225 No upper-half codes allowed in identifiers
4227 @item WIDE
4228 Wide-character codes (that is, codes greater than 255)
4229 allowed in identifiers
4230 @end table
4232 @xref{Foreign Language Representation}, for full details on the
4233 implementation of these character sets.
4235 @item /WIDE_CHARACTER_ENCODING=@var{e}
4236 @cindex @code{/WIDE_CHARACTER_ENCODING} (@code{GNAT COMPILE})
4237 Specify the method of encoding for wide characters.
4238 @var{e} is one of the following:
4240 @table @code
4242 @item HEX
4243 Hex encoding (brackets coding also recognized)
4245 @item UPPER
4246 Upper half encoding (brackets encoding also recognized)
4248 @item SHIFT_JIS
4249 Shift/JIS encoding (brackets encoding also recognized)
4251 @item EUC
4252 EUC encoding (brackets encoding also recognized)
4254 @item UTF8
4255 UTF-8 encoding (brackets encoding also recognized)
4257 @item BRACKETS
4258 Brackets encoding only (default value)
4259 @end table
4260 For full details on the these encoding
4261 methods see @xref{Wide Character Encodings}.
4262 Note that brackets coding is always accepted, even if one of the other
4263 options is specified, so for example @option{/WIDE_CHARACTER_ENCODING=UTF8} specifies that both
4264 brackets and @code{UTF-8} encodings will be recognized. The units that are
4265 with'ed directly or indirectly will be scanned using the specified
4266 representation scheme, and so if one of the non-brackets scheme is
4267 used, it must be used consistently throughout the program. However,
4268 since brackets encoding is always recognized, it may be conveniently
4269 used in standard libraries, allowing these libraries to be used with
4270 any of the available coding schemes.
4271 scheme. If no @option{/WIDE_CHARACTER_ENCODING=?} parameter is present, then the default
4272 representation is Brackets encoding only.
4274 Note that the wide character representation that is specified (explicitly
4275 or by default) for the main program also acts as the default encoding used
4276 for Wide_Text_IO files if not specifically overridden by a WCEM form
4277 parameter.
4279 @end table
4280 @node File Naming Control
4281 @subsection File Naming Control
4283 @table @code
4284 @item /FILE_NAME_MAX_LENGTH=@var{n}
4285 @cindex @option{/FILE_NAME_MAX_LENGTH} (@code{GNAT COMPILE})
4286 Activates file name "krunching". @var{n}, a decimal integer in the range
4287 1-999, indicates the maximum allowable length of a file name (not
4288 including the @file{.ADS} or @file{.ADB} extension). The default is not
4289 to enable file name krunching.
4291 For the source file naming rules, @xref{File Naming Rules}.
4292 @end table
4294 @node Subprogram Inlining Control
4295 @subsection Subprogram Inlining Control
4297 @table @code
4298 @item /INLINE=PRAGMA
4299 @cindex @option{/INLINE=PRAGMA} (@code{GNAT COMPILE})
4300 GNAT recognizes and processes @code{Inline} pragmas. However, for the
4301 inlining to actually occur, optimization must be enabled. To enable
4302 inlining across unit boundaries, this is, inlining a call in one unit of
4303 a subprogram declared in a @code{with}'ed unit, you must also specify
4304 this qualifier.
4305 In the absence of this qualifier, GNAT does not attempt
4306 inlining across units and does not need to access the bodies of
4307 subprograms for which @code{pragma Inline} is specified if they are not
4308 in the current unit.
4310 If you specify this qualifier the compiler will access these bodies,
4311 creating an extra source dependency for the resulting object file, and
4312 where possible, the call will be inlined.
4313 For further details on when inlining is possible
4314 see @xref{Inlining of Subprograms}.
4316 @item -gnatN
4317 @cindex @option{-gnatN} (@code{GNAT COMPILE})
4318 The front end inlining activated by this qualifier is generally more extensive,
4319 and quite often more effective than the standard @option{/INLINE=PRAGMA} inlining mode.
4320 It will also generate additional dependencies.
4322 @end table
4324 @node Auxiliary Output Control
4325 @subsection Auxiliary Output Control
4327 @table @code
4328 @item /TREE_OUTPUT
4329 @cindex @option{/TREE_OUTPUT} (@code{GNAT COMPILE})
4330 @cindex Writing internal trees
4331 @cindex Internal trees, writing to file
4332 Causes GNAT to write the internal tree for a unit to a file (with the
4333 extension @file{.adt}.
4334 This not normally required, but is used by separate analysis tools.
4335 Typically
4336 these tools do the necessary compilations automatically, so you should
4337 not have to specify this qualifier in normal operation.
4339 @item /UNITS_LIST
4340 @cindex @option{/UNITS_LIST} (@code{GNAT COMPILE})
4341 Print a list of units required by this compilation on @file{SYS$OUTPUT}.
4342 The listing includes all units on which the unit being compiled depends
4343 either directly or indirectly.
4345 @end table
4347 @node Debugging Control
4348 @subsection Debugging Control
4350 @table @code
4351 @cindex Debugging options
4353 @item /EXPAND_SOURCE
4354 @cindex @option{/EXPAND_SOURCE} (@code{GNAT COMPILE})
4355 This qualifier causes the compiler to generate auxiliary output containing
4356 a pseudo-source listing of the generated expanded code. Like most Ada
4357 compilers, GNAT works by first transforming the high level Ada code into
4358 lower level constructs. For example, tasking operations are transformed
4359 into calls to the tasking run-time routines. A unique capability of GNAT
4360 is to list this expanded code in a form very close to normal Ada source.
4361 This is very useful in understanding the implications of various Ada
4362 usage on the efficiency of the generated code. There are many cases in
4363 Ada (e.g. the use of controlled types), where simple Ada statements can
4364 generate a lot of run-time code. By using @option{/EXPAND_SOURCE} you can identify
4365 these cases, and consider whether it may be desirable to modify the coding
4366 approach to improve efficiency.
4368 The format of the output is very similar to standard Ada source, and is
4369 easily understood by an Ada programmer. The following special syntactic
4370 additions correspond to low level features used in the generated code that
4371 do not have any exact analogies in pure Ada source form. The following
4372 is a partial list of these special constructions. See the specification
4373 of package @code{Sprint} in file @file{SPRINT.ADS} for a full list.
4375 @table @code
4376 @item new @var{xxx} [storage_pool = @var{yyy}]
4377 Shows the storage pool being used for an allocator.
4379 @item at end @var{procedure-name};
4380 Shows the finalization (cleanup) procedure for a scope.
4382 @item (if @var{expr} then @var{expr} else @var{expr})
4383 Conditional expression equivalent to the @code{x?y:z} construction in C.
4385 @item @var{target}^(@var{source})
4386 A conversion with floating-point truncation instead of rounding.
4388 @item @var{target}?(@var{source})
4389 A conversion that bypasses normal Ada semantic checking. In particular
4390 enumeration types and fixed-point types are treated simply as integers.
4392 @item @var{target}?^(@var{source})
4393 Combines the above two cases.
4395 @item @var{x} #/ @var{y}
4396 @itemx @var{x} #mod @var{y}
4397 @itemx @var{x} #* @var{y}
4398 @itemx @var{x} #rem @var{y}
4399 A division or multiplication of fixed-point values which are treated as
4400 integers without any kind of scaling.
4402 @item free @var{expr} [storage_pool = @var{xxx}]
4403 Shows the storage pool associated with a @code{free} statement.
4405 @item freeze @var{typename} [@var{actions}]
4406 Shows the point at which @var{typename} is frozen, with possible
4407 associated actions to be performed at the freeze point.
4409 @item reference @var{itype}
4410 Reference (and hence definition) to internal type @var{itype}.
4412 @item @var{function-name}! (@var{arg}, @var{arg}, @var{arg})
4413 Intrinsic function call.
4415 @item @var{labelname} : label
4416 Declaration of label @var{labelname}.
4418 @item @var{expr} && @var{expr} && @var{expr} ... && @var{expr}
4419 A multiple concatenation (same effect as @var{expr} & @var{expr} &
4420 @var{expr}, but handled more efficiently).
4422 @item [constraint_error]
4423 Raise the @code{Constraint_Error} exception.
4425 @item @var{expression}'reference
4426 A pointer to the result of evaluating @var{expression}.
4428 @item @var{target-type}!(@var{source-expression})
4429 An unchecked conversion of @var{source-expression} to @var{target-type}.
4431 @item [@var{numerator}/@var{denominator}]
4432 Used to represent internal real literals (that) have no exact
4433 representation in base 2-16 (for example, the result of compile time
4434 evaluation of the expression 1.0/27.0).
4436 @item /XDEBUG
4437 @cindex @option{/XDEBUG} (@code{GNAT COMPILE})
4438 This qualifier is used in conjunction with @option{/EXPAND_SOURCE} to cause the expanded
4439 source, as described above to be written to files with names
4440 @file{XXX_DG}, where @file{xxx} is the normal file name,
4441 for example, if the source file name is @file{HELLO.ADB},
4442 then a file @file{HELLO.ADB_DG} will be written.
4443 The debugging information generated
4444 by the @code{GNAT COMPILE} @code{/DEBUG} qualifier will refer to the generated
4445 @file{XXX_DG} file. This allows you to do source level debugging using
4446 the generated code which is sometimes useful for complex code, for example
4447 to find out exactly which part of a complex construction raised an
4448 exception. This qualifier also suppress generation of cross-reference
4449 information (see /XREF=SUPPRESS).
4451 @item /COMPRESS_NAMES
4452 @cindex @option{/CHECKS=ELABORATION} (@code{GNAT COMPILE})
4453 In the generated debugging information, and also in the case of long external
4454 names, the compiler uses a compression mechanism if the name is very long.
4455 This compression method uses a checksum, and avoids trouble on some operating
4456 systems which have difficulty with very long names. The @option{/COMPRESS_NAMES} qualifier
4457 forces this compression approach to be used on all external names and names
4458 in the debugging information tables. This reduces the size of the generated
4459 executable, at the expense of making the naming scheme more complex. The
4460 compression only affects the qualification of the name. Thus a name in
4461 the source:
4463 @smallexample
4464 Very_Long_Package.Very_Long_Inner_Package.Var
4465 @end smallexample
4467 @noindent
4468 would normally appear in these tables as:
4470 @smallexample
4471 very_long_package__very_long_inner_package__var
4472 @end smallexample
4474 @noindent
4475 but if the @option{/COMPRESS_NAMES} qualifier is used, then the name appears as
4477 @smallexample
4478 XCb7e0c705__var
4479 @end smallexample
4481 @noindent
4482 Here b7e0c705 is a compressed encoding of the qualification prefix.
4483 The GNAT Ada aware version of GDB understands these encoded prefixes, so if this
4484 debugger is used, the encoding is largely hidden from the user of the compiler.
4486 @end table
4488 @item /REPRESENTATION_INFO[0|1|2|3][s]
4489 @cindex @option{/REPRESENTATION_INFO} (@code{GNAT COMPILE})
4490 This qualifier controls output from the compiler of a listing showing
4491 representation information for declared types and objects. For
4492 @option{/REPRESENTATION_INFO=NONE}, no information is output (equivalent to omitting
4493 the @option{/REPRESENTATION_INFO} qualifier). For @option{/REPRESENTATION_INFO=ARRAYS} (which is the default,
4494 so @option{/REPRESENTATION_INFO} with no parameter has the same effect), size and alignment
4495 information is listed for declared array and record types. For
4496 @option{/REPRESENTATION_INFO=OBJECTS}, size and alignment information is listed for all
4497 expression information for values that are computed at run time for
4498 variant records. These symbolic expressions have a mostly obvious
4499 format with #n being used to represent the value of the n'th
4500 discriminant. See source files @file{REPINFO.ADS/adb} in the
4501 @code{GNAT} sources for full detalis on the format of @option{/REPRESENTATION_INFO=SYMBOLIC}
4502 output. If the qualifier is followed by an s (e.g. @option{-gnatR2s}), then
4503 the output is to a file with the name @file{file_REP} where
4504 file is the name of the corresponding source file.
4506 @item /XREF=SUPPRESS
4507 @cindex @option{/XREF=SUPPRESS} (@code{GNAT COMPILE})
4508 Normally the compiler generates full cross-referencing information in
4509 the @file{ALI} file. This information is used by a number of tools,
4510 including @code{GNAT FIND} and @code{GNAT XREF}. The /XREF=SUPPRESS qualifier
4511 suppresses this information. This saves some space and may slightly
4512 speed up compilation, but means that these tools cannot be used.
4513 @end table
4515 @node Units to Sources Mapping Files
4516 @subsection Units to Sources Mapping Files
4518 @table @code
4520 @item -gnatem@var{path}
4521 @cindex @option{-gnatem} (@code{GNAT COMPILE})
4522 A mapping file is a way to communicate to the compiler two mappings:
4523 from unit names to file names (without any directory information) and from
4524 file names to path names (with full directory information). These mappings
4525 are used by the compiler to short-circuit the path search.
4527 A mapping file is a sequence of sets of three lines. In each set,
4528 the first line is the unit name, in lower case, with "%s" appended for
4529 specifications and "%b" appended for bodies; the second line is the file
4530 name; and the third line is the path name.
4532 Example:
4533 @smallexample
4534    main%b
4535    main.2.ADA
4536    /gnat/project1/sources/main.2.ADA
4537 @end smallexample
4539 When the qualifier @option{-gnatem} is specified, the compiler will create
4540 in memory the two mappings from the specified file. If there is any problem
4541 (non existent file, truncated file or duplicate entries), no mapping
4542 will be created.
4544 Several @option{-gnatem} qualifiers may be specified; however, only the last
4545 one on the command line will be taken into account.
4547 When using a project file, @code{GNAT MAKE} create a temporary mapping file
4548 and communicates it to the compiler using this qualifier.
4550 @end table
4552 @node Search Paths and the Run-Time Library (RTL)
4553 @section Search Paths and the Run-Time Library (RTL)
4555 @noindent
4556 With the GNAT source-based library system, the compiler must be able to
4557 find source files for units that are needed by the unit being compiled.
4558 Search paths are used to guide this process.
4560 The compiler compiles one source file whose name must be given
4561 explicitly on the command line. In other words, no searching is done
4562 for this file. To find all other source files that are needed (the most
4563 common being the specs of units), the compiler examines the following
4564 directories, in the following order:
4566 @enumerate
4567 @item
4568 The directory containing the source file of the main unit being compiled
4569 (the file name on the command line).
4571 @item
4572 Each directory named by an @code{/SOURCE_SEARCH} qualifier given on the @code{GNAT COMPILE}
4573 command line, in the order given.
4575 @item
4576 @findex ADA_INCLUDE_PATH
4577 Each of the directories listed in the value of the
4578 @code{ADA_INCLUDE_PATH} logical name.
4579 Normally, define this value as a logical name containing a comma separated
4580 list of directory names.
4582 This variable can also be defined by means of an environment string
4583 (an argument to the DEC C exec* set of functions).
4585 Logical Name:
4586 @smallexample
4587 DEFINE ANOTHER_PATH FOO:[BAG]
4588 DEFINE ADA_INCLUDE_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
4589 @end smallexample
4591 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
4592 first, followed by the standard Ada 95
4593 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADAINCLUDE].
4594 If this is not redefined, the user will obtain the DEC Ada83 IO packages
4595 (Text_IO, Sequential_IO, etc)
4596 instead of the Ada95 packages. Thus, in order to get the Ada 95
4597 packages by default, ADA_INCLUDE_PATH must be redefined.
4598 @item
4599 The content of the "ada_source_path" file which is part of the GNAT
4600 installation tree and is used to store standard libraries such as the
4601 GNAT Run Time Library (RTL) source files.
4602 @end enumerate
4604 @noindent
4605 Specifying the qualifier @code{/NOCURRENT_DIRECTORY}
4606 inhibits the use of the directory
4607 containing the source file named in the command line. You can still
4608 have this directory on your search path, but in this case it must be
4609 explicitly requested with a @code{/SOURCE_SEARCH} qualifier.
4611 Specifying the qualifier @code{/NOSTD_INCLUDES}
4612 inhibits the search of the default location for the GNAT Run Time
4613 Library (RTL) source files.
4615 The compiler outputs its object files and ALI files in the current
4616 working directory.
4618 @findex System.IO
4619 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
4620 children make up the GNAT RTL, together with the simple @code{System.IO}
4621 package used in the "Hello World" example. The sources for these units
4622 are needed by the compiler and are kept together in one directory. Not
4623 all of the bodies are needed, but all of the sources are kept together
4624 anyway. In a normal installation, you need not specify these directory
4625 names when compiling or binding. Either the environment variables or
4626 the built-in defaults cause these files to be found.
4628 In addition to the language-defined hierarchies (System, Ada and
4629 Interfaces), the GNAT distribution provides a fourth hierarchy,
4630 consisting of child units of GNAT. This is a collection of generally
4631 useful routines. See the GNAT Reference Manual for further details.
4633 Besides simplifying access to the RTL, a major use of search paths is
4634 in compiling sources from multiple directories. This can make
4635 development environments much more flexible.
4637 @node Order of Compilation Issues
4638 @section Order of Compilation Issues
4640 @noindent
4641 If, in our earlier example, there was a spec for the @code{hello}
4642 procedure, it would be contained in the file @file{HELLO.ADS}; yet this
4643 file would not have to be explicitly compiled. This is the result of the
4644 model we chose to implement library management. Some of the consequences
4645 of this model are as follows:
4647 @itemize @bullet
4648 @item
4649 There is no point in compiling specs (except for package
4650 specs with no bodies) because these are compiled as needed by clients. If
4651 you attempt a useless compilation, you will receive an error message.
4652 It is also useless to compile subunits because they are compiled as needed
4653 by the parent.
4655 @item
4656 There are no order of compilation requirements: performing a
4657 compilation never obsoletes anything. The only way you can obsolete
4658 something and require recompilations is to modify one of the
4659 source files on which it depends.
4661 @item
4662 There is no library as such, apart from the ALI files
4663 (@pxref{The Ada Library Information Files}, for information on the format of these
4664 files). For now we find it convenient to create separate ALI files, but
4665 eventually the information therein may be incorporated into the object
4666 file directly.
4668 @item
4669 When you compile a unit, the source files for the specs of all units
4670 that it @code{with}'s, all its subunits, and the bodies of any generics it
4671 instantiates must be available (reachable by the search-paths mechanism
4672 described above), or you will receive a fatal error message.
4673 @end itemize
4675 @node Examples
4676 @section Examples
4678 @noindent
4679 The following are some typical Ada compilation command line examples:
4681 @table @code
4682 @item $ GNAT COMPILE XYZ.ADB
4683 Compile body in file @file{XYZ.ADB} with all default options.
4685 @item $ GNAT COMPILE /OPTIMIZE=ALL /CHECKS=ASSERTIONS XYZ-DEF.ADB
4687 Compile the child unit package in file @file{XYZ-DEF.ADB} with extensive
4688 optimizations, and pragma @code{Assert}/@code{Debug} statements
4689 enabled.
4691 @item $ GNAT COMPILE /NOLOAD ABC-DEF.ADB
4692 Compile the subunit in file @file{ABC-DEF.ADB} in semantic-checking-only
4693 mode.
4694 @end table
4696 @node Binding Using GNAT BIND
4697 @chapter Binding Using @code{GNAT BIND}
4698 @findex GNAT BIND
4700 @menu
4701 * Running GNAT BIND::
4702 * Generating the Binder Program in C::
4703 * Consistency-Checking Modes::
4704 * Binder Error Message Control::
4705 * Elaboration Control::
4706 * Output Control::
4707 * Binding with Non-Ada Main Programs::
4708 * Binding Programs with No Main Subprogram::
4709 * Summary of Binder Qualifiers::
4710 * Command-Line Access::
4711 * Search Paths for GNAT BIND::
4712 * Examples of GNAT BIND Usage::
4713 @end menu
4715 @noindent
4716 This chapter describes the GNAT binder, @code{GNAT BIND}, which is used
4717 to bind compiled GNAT objects. The @code{GNAT BIND} program performs
4718 four separate functions:
4720 @enumerate
4721 @item
4722 Checks that a program is consistent, in accordance with the rules in
4723 Chapter 10 of the Ada 95 Reference Manual. In particular, error
4724 messages are generated if a program uses inconsistent versions of a
4725 given unit.
4727 @item
4728 Checks that an acceptable order of elaboration exists for the program
4729 and issues an error message if it cannot find an order of elaboration
4730 that satisfies the rules in Chapter 10 of the Ada 95 Language Manual.
4732 @item
4733 Generates a main program incorporating the given elaboration order.
4734 This program is a small Ada package (body and spec) that
4735 must be subsequently compiled
4736 using the GNAT compiler. The necessary compilation step is usually
4737 performed automatically by @code{GNAT LINK}. The two most important
4738 functions of this program
4739 are to call the elaboration routines of units in an appropriate order
4740 and to call the main program.
4742 @item
4743 Determines the set of object files required by the given main program.
4744 This information is output in the forms of comments in the generated program,
4745 to be read by the @code{GNAT LINK} utility used to link the Ada application.
4746 @end enumerate
4748 @node Running GNAT BIND
4749 @section Running @code{GNAT BIND}
4751 @noindent
4752 The form of the @code{GNAT BIND} command is
4754 @smallexample
4755 $ GNAT BIND [@var{qualifiers}] @var{mainprog}[.ALI] [@var{qualifiers}]
4756 @end smallexample
4758 @noindent
4759 where @var{mainprog}.ADB is the Ada file containing the main program
4760 unit body. If no qualifiers are specified, @code{GNAT BIND} constructs an Ada
4761 package in two files which names are
4762 @file{B$@var{ada_main}.ADS}, and @file{B$@var{ada_main}.ADB}.
4763 For example, if given the
4764 parameter @samp{HELLO.ALI}, for a main program contained in file
4765 @file{HELLO.ADB}, the binder output files would be @file{B~HELLO.ADS}
4766 and @file{B~HELLO.ADB}.
4768 When doing consistency checking, the binder takes into consideration
4769 any source files it can locate. For example, if the binder determines
4770 that the given main program requires the package @code{Pack}, whose
4771 @file{.ALI}
4772 file is @file{PACK.ALI} and whose corresponding source spec file is
4773 @file{PACK.ADS}, it attempts to locate the source file @file{PACK.ADS}
4774 (using the same search path conventions as previously described for the
4775 @code{GNAT COMPILE} command). If it can locate this source file, it checks that
4776 the time stamps
4777 or source checksums of the source and its references to in @file{ali} files
4778 match. In other words, any @file{ali} files that mentions this spec must have
4779 resulted from compiling this version of the source file (or in the case
4780 where the source checksums match, a version close enough that the
4781 difference does not matter).
4783 @cindex Source files, use by binder
4784 The effect of this consistency checking, which includes source files, is
4785 that the binder ensures that the program is consistent with the latest
4786 version of the source files that can be located at bind time. Editing a
4787 source file without compiling files that depend on the source file cause
4788 error messages to be generated by the binder.
4790 For example, suppose you have a main program @file{HELLO.ADB} and a
4791 package @code{P}, from file @file{P.ADS} and you perform the following
4792 steps:
4794 @enumerate
4795 @item
4796 Enter @code{GNAT COMPILE HELLO.ADB} to compile the main program.
4798 @item
4799 Enter @code{GNAT COMPILE P.ADS} to compile package @code{P}.
4801 @item
4802 Edit file @file{P.ADS}.
4804 @item
4805 Enter @code{GNAT BIND hello}.
4806 @end enumerate
4808 At this point, the file @file{P.ALI} contains an out-of-date time stamp
4809 because the file @file{P.ADS} has been edited. The attempt at binding
4810 fails, and the binder generates the following error messages:
4812 @smallexample
4813 error: "HELLO.ADB" must be recompiled ("P.ADS" has been modified)
4814 error: "P.ADS" has been modified and must be recompiled
4815 @end smallexample
4817 @noindent
4818 Now both files must be recompiled as indicated, and then the bind can
4819 succeed, generating a main program. You need not normally be concerned
4820 with the contents of this file, but it is similar to the following which
4821 is the binder file generated for a simple "hello world" program.
4823 @smallexample
4824 @iftex
4825 @leftskip=0cm
4826 @end iftex
4827 --  The package is called Ada_Main unless this name is actually used
4828 --  as a unit name in the partition, in which case some other unique
4829 --  name is used.
4831 with System;
4832 package ada_main is
4834    Elab_Final_Code : Integer;
4835    pragma Import (C, Elab_Final_Code, "__gnat_inside_elab_final_code");
4837    --  The main program saves the parameters (argument count,
4838    --  argument values, environment pointer) in global variables
4839    --  for later access by other units including
4840    --  Ada.Command_Line.
4842    gnat_argc : Integer;
4843    gnat_argv : System.Address;
4844    gnat_envp : System.Address;
4846    --  The actual variables are stored in a library routine. This
4847    --  is useful for some shared library situations, where there
4848    --  are problems if variables are not in the library.
4850    pragma Import (C, gnat_argc);
4851    pragma Import (C, gnat_argv);
4852    pragma Import (C, gnat_envp);
4854    --  The exit status is similarly an external location
4856    gnat_exit_status : Integer;
4857    pragma Import (C, gnat_exit_status);
4859    GNAT_Version : constant String :=
4860                     "GNAT Version: 3.15w (20010315)";
4861    pragma Export (C, GNAT_Version, "__gnat_version");
4863    --  This is the generated adafinal routine that performs
4864    --  finalization at the end of execution. In the case where
4865    --  Ada is the main program, this main program makes a call
4866    --  to adafinal at program termination.
4868    procedure adafinal;
4869    pragma Export (C, adafinal, "adafinal");
4871    --  This is the generated adainit routine that performs
4872    --  initialization at the start of execution. In the case
4873    --  where Ada is the main program, this main program makes
4874    --  a call to adainit at program startup.
4876    procedure adainit;
4877    pragma Export (C, adainit, "adainit");
4879    --  This routine is called at the start of execution. It is
4880    --  a dummy routine that is used by the debugger to breakpoint
4881    --  at the start of execution.
4883    procedure Break_Start;
4884    pragma Import (C, Break_Start, "__gnat_break_start");
4886    --  This is the actual generated main program (it would be
4887    --  suppressed if the no main program qualifier were used). As
4888    --  required by standard system conventions, this program has
4889    --  the external name main.
4891    function main
4892      (argc : Integer;
4893       argv : System.Address;
4894       envp : System.Address)
4895       return Integer;
4896    pragma Export (C, main, "main");
4898    --  The following set of constants give the version
4899    --  identification values for every unit in the bound
4900    --  partition. This identification is computed from all
4901    --  dependent semantic units, and corresponds to the
4902    --  string that would be returned by use of the
4903    --  Body_Version or Version attributes.
4905    type Version_32 is mod 2 ** 32;
4906    u00001 : constant Version_32 := 16#7880BEB3#;
4907    u00002 : constant Version_32 := 16#0D24CBD0#;
4908    u00003 : constant Version_32 := 16#3283DBEB#;
4909    u00004 : constant Version_32 := 16#2359F9ED#;
4910    u00005 : constant Version_32 := 16#664FB847#;
4911    u00006 : constant Version_32 := 16#68E803DF#;
4912    u00007 : constant Version_32 := 16#5572E604#;
4913    u00008 : constant Version_32 := 16#46B173D8#;
4914    u00009 : constant Version_32 := 16#156A40CF#;
4915    u00010 : constant Version_32 := 16#033DABE0#;
4916    u00011 : constant Version_32 := 16#6AB38FEA#;
4917    u00012 : constant Version_32 := 16#22B6217D#;
4918    u00013 : constant Version_32 := 16#68A22947#;
4919    u00014 : constant Version_32 := 16#18CC4A56#;
4920    u00015 : constant Version_32 := 16#08258E1B#;
4921    u00016 : constant Version_32 := 16#367D5222#;
4922    u00017 : constant Version_32 := 16#20C9ECA4#;
4923    u00018 : constant Version_32 := 16#50D32CB6#;
4924    u00019 : constant Version_32 := 16#39A8BB77#;
4925    u00020 : constant Version_32 := 16#5CF8FA2B#;
4926    u00021 : constant Version_32 := 16#2F1EB794#;
4927    u00022 : constant Version_32 := 16#31AB6444#;
4928    u00023 : constant Version_32 := 16#1574B6E9#;
4929    u00024 : constant Version_32 := 16#5109C189#;
4930    u00025 : constant Version_32 := 16#56D770CD#;
4931    u00026 : constant Version_32 := 16#02F9DE3D#;
4932    u00027 : constant Version_32 := 16#08AB6B2C#;
4933    u00028 : constant Version_32 := 16#3FA37670#;
4934    u00029 : constant Version_32 := 16#476457A0#;
4935    u00030 : constant Version_32 := 16#731E1B6E#;
4936    u00031 : constant Version_32 := 16#23C2E789#;
4937    u00032 : constant Version_32 := 16#0F1BD6A1#;
4938    u00033 : constant Version_32 := 16#7C25DE96#;
4939    u00034 : constant Version_32 := 16#39ADFFA2#;
4940    u00035 : constant Version_32 := 16#571DE3E7#;
4941    u00036 : constant Version_32 := 16#5EB646AB#;
4942    u00037 : constant Version_32 := 16#4249379B#;
4943    u00038 : constant Version_32 := 16#0357E00A#;
4944    u00039 : constant Version_32 := 16#3784FB72#;
4945    u00040 : constant Version_32 := 16#2E723019#;
4946    u00041 : constant Version_32 := 16#623358EA#;
4947    u00042 : constant Version_32 := 16#107F9465#;
4948    u00043 : constant Version_32 := 16#6843F68A#;
4949    u00044 : constant Version_32 := 16#63305874#;
4950    u00045 : constant Version_32 := 16#31E56CE1#;
4951    u00046 : constant Version_32 := 16#02917970#;
4952    u00047 : constant Version_32 := 16#6CCBA70E#;
4953    u00048 : constant Version_32 := 16#41CD4204#;
4954    u00049 : constant Version_32 := 16#572E3F58#;
4955    u00050 : constant Version_32 := 16#20729FF5#;
4956    u00051 : constant Version_32 := 16#1D4F93E8#;
4957    u00052 : constant Version_32 := 16#30B2EC3D#;
4958    u00053 : constant Version_32 := 16#34054F96#;
4959    u00054 : constant Version_32 := 16#5A199860#;
4960    u00055 : constant Version_32 := 16#0E7F912B#;
4961    u00056 : constant Version_32 := 16#5760634A#;
4962    u00057 : constant Version_32 := 16#5D851835#;
4964    --  The following Export pragmas export the version numbers
4965    --  with symbolic names ending in B (for body) or S
4966    --  (for spec) so that they can be located in a link. The
4967    --  information provided here is sufficient to track down
4968    --  the exact versions of units used in a given build.
4970    pragma Export (C, u00001, "helloB");
4971    pragma Export (C, u00002, "system__standard_libraryB");
4972    pragma Export (C, u00003, "system__standard_libraryS");
4973    pragma Export (C, u00004, "adaS");
4974    pragma Export (C, u00005, "ada__text_ioB");
4975    pragma Export (C, u00006, "ada__text_ioS");
4976    pragma Export (C, u00007, "ada__exceptionsB");
4977    pragma Export (C, u00008, "ada__exceptionsS");
4978    pragma Export (C, u00009, "gnatS");
4979    pragma Export (C, u00010, "gnat__heap_sort_aB");
4980    pragma Export (C, u00011, "gnat__heap_sort_aS");
4981    pragma Export (C, u00012, "systemS");
4982    pragma Export (C, u00013, "system__exception_tableB");
4983    pragma Export (C, u00014, "system__exception_tableS");
4984    pragma Export (C, u00015, "gnat__htableB");
4985    pragma Export (C, u00016, "gnat__htableS");
4986    pragma Export (C, u00017, "system__exceptionsS");
4987    pragma Export (C, u00018, "system__machine_state_operationsB");
4988    pragma Export (C, u00019, "system__machine_state_operationsS");
4989    pragma Export (C, u00020, "system__machine_codeS");
4990    pragma Export (C, u00021, "system__storage_elementsB");
4991    pragma Export (C, u00022, "system__storage_elementsS");
4992    pragma Export (C, u00023, "system__secondary_stackB");
4993    pragma Export (C, u00024, "system__secondary_stackS");
4994    pragma Export (C, u00025, "system__parametersB");
4995    pragma Export (C, u00026, "system__parametersS");
4996    pragma Export (C, u00027, "system__soft_linksB");
4997    pragma Export (C, u00028, "system__soft_linksS");
4998    pragma Export (C, u00029, "system__stack_checkingB");
4999    pragma Export (C, u00030, "system__stack_checkingS");
5000    pragma Export (C, u00031, "system__tracebackB");
5001    pragma Export (C, u00032, "system__tracebackS");
5002    pragma Export (C, u00033, "ada__streamsS");
5003    pragma Export (C, u00034, "ada__tagsB");
5004    pragma Export (C, u00035, "ada__tagsS");
5005    pragma Export (C, u00036, "system__string_opsB");
5006    pragma Export (C, u00037, "system__string_opsS");
5007    pragma Export (C, u00038, "interfacesS");
5008    pragma Export (C, u00039, "interfaces__c_streamsB");
5009    pragma Export (C, u00040, "interfaces__c_streamsS");
5010    pragma Export (C, u00041, "system__file_ioB");
5011    pragma Export (C, u00042, "system__file_ioS");
5012    pragma Export (C, u00043, "ada__finalizationB");
5013    pragma Export (C, u00044, "ada__finalizationS");
5014    pragma Export (C, u00045, "system__finalization_rootB");
5015    pragma Export (C, u00046, "system__finalization_rootS");
5016    pragma Export (C, u00047, "system__finalization_implementationB");
5017    pragma Export (C, u00048, "system__finalization_implementationS");
5018    pragma Export (C, u00049, "system__string_ops_concat_3B");
5019    pragma Export (C, u00050, "system__string_ops_concat_3S");
5020    pragma Export (C, u00051, "system__stream_attributesB");
5021    pragma Export (C, u00052, "system__stream_attributesS");
5022    pragma Export (C, u00053, "ada__io_exceptionsS");
5023    pragma Export (C, u00054, "system__unsigned_typesS");
5024    pragma Export (C, u00055, "system__file_control_blockS");
5025    pragma Export (C, u00056, "ada__finalization__list_controllerB");
5026    pragma Export (C, u00057, "ada__finalization__list_controllerS");
5028    -- BEGIN ELABORATION ORDER
5029    -- ada (spec)
5030    -- gnat (spec)
5031    -- gnat.heap_sort_a (spec)
5032    -- gnat.heap_sort_a (body)
5033    -- gnat.htable (spec)
5034    -- gnat.htable (body)
5035    -- interfaces (spec)
5036    -- system (spec)
5037    -- system.machine_code (spec)
5038    -- system.parameters (spec)
5039    -- system.parameters (body)
5040    -- interfaces.c_streams (spec)
5041    -- interfaces.c_streams (body)
5042    -- system.standard_library (spec)
5043    -- ada.exceptions (spec)
5044    -- system.exception_table (spec)
5045    -- system.exception_table (body)
5046    -- ada.io_exceptions (spec)
5047    -- system.exceptions (spec)
5048    -- system.storage_elements (spec)
5049    -- system.storage_elements (body)
5050    -- system.machine_state_operations (spec)
5051    -- system.machine_state_operations (body)
5052    -- system.secondary_stack (spec)
5053    -- system.stack_checking (spec)
5054    -- system.soft_links (spec)
5055    -- system.soft_links (body)
5056    -- system.stack_checking (body)
5057    -- system.secondary_stack (body)
5058    -- system.standard_library (body)
5059    -- system.string_ops (spec)
5060    -- system.string_ops (body)
5061    -- ada.tags (spec)
5062    -- ada.tags (body)
5063    -- ada.streams (spec)
5064    -- system.finalization_root (spec)
5065    -- system.finalization_root (body)
5066    -- system.string_ops_concat_3 (spec)
5067    -- system.string_ops_concat_3 (body)
5068    -- system.traceback (spec)
5069    -- system.traceback (body)
5070    -- ada.exceptions (body)
5071    -- system.unsigned_types (spec)
5072    -- system.stream_attributes (spec)
5073    -- system.stream_attributes (body)
5074    -- system.finalization_implementation (spec)
5075    -- system.finalization_implementation (body)
5076    -- ada.finalization (spec)
5077    -- ada.finalization (body)
5078    -- ada.finalization.list_controller (spec)
5079    -- ada.finalization.list_controller (body)
5080    -- system.file_control_block (spec)
5081    -- system.file_io (spec)
5082    -- system.file_io (body)
5083    -- ada.text_io (spec)
5084    -- ada.text_io (body)
5085    -- hello (body)
5086    -- END ELABORATION ORDER
5088 end ada_main;
5090 --  The following source file name pragmas allow the generated file
5091 --  names to be unique for different main programs. They are needed
5092 --  since the package name will always be Ada_Main.
5094 pragma Source_File_Name (ada_main, Spec_File_Name => "B~HELLO.ADS");
5095 pragma Source_File_Name (ada_main, Body_File_Name => "B~HELLO.ADB");
5097 --  Generated package body for Ada_Main starts here
5099 package body ada_main is
5101    --  The actual finalization is performed by calling the
5102    --  library routine in System.Standard_Library.Adafinal
5104    procedure Do_Finalize;
5105    pragma Import (C, Do_Finalize, "system__standard_library__adafinal");
5107    -------------
5108    -- adainit --
5109    -------------
5111 @findex adainit
5112    procedure adainit is
5114       --  These booleans are set to True once the associated unit has
5115       --  been elaborated. It is also used to avoid elaborating the
5116       --  same unit twice.
5118       E040 : Boolean; pragma Import (Ada, E040, "interfaces__c_streams_E");
5119       E008 : Boolean; pragma Import (Ada, E008, "ada__exceptions_E");
5120       E014 : Boolean; pragma Import (Ada, E014, "system__exception_table_E");
5121       E053 : Boolean; pragma Import (Ada, E053, "ada__io_exceptions_E");
5122       E017 : Boolean; pragma Import (Ada, E017, "system__exceptions_E");
5123       E024 : Boolean; pragma Import (Ada, E024, "system__secondary_stack_E");
5124       E030 : Boolean; pragma Import (Ada, E030, "system__stack_checking_E");
5125       E028 : Boolean; pragma Import (Ada, E028, "system__soft_links_E");
5126       E035 : Boolean; pragma Import (Ada, E035, "ada__tags_E");
5127       E033 : Boolean; pragma Import (Ada, E033, "ada__streams_E");
5128       E046 : Boolean; pragma Import (Ada, E046, "system__finalization_root_E");
5129       E048 : Boolean; pragma Import (Ada, E048, "system__finalization_implementation_E");
5130       E044 : Boolean; pragma Import (Ada, E044, "ada__finalization_E");
5131       E057 : Boolean; pragma Import (Ada, E057, "ada__finalization__list_controller_E");
5132       E055 : Boolean; pragma Import (Ada, E055, "system__file_control_block_E");
5133       E042 : Boolean; pragma Import (Ada, E042, "system__file_io_E");
5134       E006 : Boolean; pragma Import (Ada, E006, "ada__text_io_E");
5136       --  Set_Globals is a library routine that stores away the
5137       --  value of the indicated set of global values in global
5138       --  variables within the library.
5140       procedure Set_Globals
5141         (Main_Priority            : Integer;
5142          Time_Slice_Value         : Integer;
5143          WC_Encoding              : Character;
5144          Locking_Policy           : Character;
5145          Queuing_Policy           : Character;
5146          Task_Dispatching_Policy  : Character;
5147          Adafinal                 : System.Address;
5148          Unreserve_All_Interrupts : Integer;
5149          Exception_Tracebacks     : Integer);
5150 @findex __gnat_set_globals
5151       pragma Import (C, Set_Globals, "__gnat_set_globals");
5153       --  SDP_Table_Build is a library routine used to build the
5154       --  exception tables. See unit Ada.Exceptions in files
5155       --  A-EXCEPT.ADS/adb for full details of how zero cost
5156       --  exception handling works. This procedure, the call to
5157       --  it, and the two following tables are all omitted if the
5158       --  build is in longjmp/setjump exception mode.
5160 @findex SDP_Table_Build
5161 @findex Zero Cost Exceptions
5162       procedure SDP_Table_Build
5163         (SDP_Addresses   : System.Address;
5164          SDP_Count       : Natural;
5165          Elab_Addresses  : System.Address;
5166          Elab_Addr_Count : Natural);
5167       pragma Import (C, SDP_Table_Build, "__gnat_SDP_Table_Build");
5169       --  Table of Unit_Exception_Table addresses. Used for zero
5170       --  cost exception handling to build the top level table.
5172       ST : aliased constant array (1 .. 23) of System.Address := (
5173         Hello'UET_Address,
5174         Ada.Text_Io'UET_Address,
5175         Ada.Exceptions'UET_Address,
5176         Gnat.Heap_Sort_A'UET_Address,
5177         System.Exception_Table'UET_Address,
5178         System.Machine_State_Operations'UET_Address,
5179         System.Secondary_Stack'UET_Address,
5180         System.Parameters'UET_Address,
5181         System.Soft_Links'UET_Address,
5182         System.Stack_Checking'UET_Address,
5183         System.Traceback'UET_Address,
5184         Ada.Streams'UET_Address,
5185         Ada.Tags'UET_Address,
5186         System.String_Ops'UET_Address,
5187         Interfaces.C_Streams'UET_Address,
5188         System.File_Io'UET_Address,
5189         Ada.Finalization'UET_Address,
5190         System.Finalization_Root'UET_Address,
5191         System.Finalization_Implementation'UET_Address,
5192         System.String_Ops_Concat_3'UET_Address,
5193         System.Stream_Attributes'UET_Address,
5194         System.File_Control_Block'UET_Address,
5195         Ada.Finalization.List_Controller'UET_Address);
5197       --  Table of addresses of elaboration routines. Used for
5198       --  zero cost exception handling to make sure these
5199       --  addresses are included in the top level procedure
5200       --  address table.
5202       EA : aliased constant array (1 .. 23) of System.Address := (
5203         adainit'Code_Address,
5204         Do_Finalize'Code_Address,
5205         Ada.Exceptions'Elab_Spec'Address,
5206         System.Exceptions'Elab_Spec'Address,
5207         Interfaces.C_Streams'Elab_Spec'Address,
5208         System.Exception_Table'Elab_Body'Address,
5209         Ada.Io_Exceptions'Elab_Spec'Address,
5210         System.Stack_Checking'Elab_Spec'Address,
5211         System.Soft_Links'Elab_Body'Address,
5212         System.Secondary_Stack'Elab_Body'Address,
5213         Ada.Tags'Elab_Spec'Address,
5214         Ada.Tags'Elab_Body'Address,
5215         Ada.Streams'Elab_Spec'Address,
5216         System.Finalization_Root'Elab_Spec'Address,
5217         Ada.Exceptions'Elab_Body'Address,
5218         System.Finalization_Implementation'Elab_Spec'Address,
5219         System.Finalization_Implementation'Elab_Body'Address,
5220         Ada.Finalization'Elab_Spec'Address,
5221         Ada.Finalization.List_Controller'Elab_Spec'Address,
5222         System.File_Control_Block'Elab_Spec'Address,
5223         System.File_Io'Elab_Body'Address,
5224         Ada.Text_Io'Elab_Spec'Address,
5225         Ada.Text_Io'Elab_Body'Address);
5227    --  Start of processing for adainit
5229    begin
5231       --  Call SDP_Table_Build to build the top level procedure
5232       --  table for zero cost exception handling (omitted in
5233       --  longjmp/setjump mode).
5235       SDP_Table_Build (ST'Address, 23, EA'Address, 23);
5237       --  Call Set_Globals to record various information for
5238       --  this partition.  The values are derived by the binder
5239       --  from information stored in the ali files by the compiler.
5241 @findex __gnat_set_globals
5242       Set_Globals
5243         (Main_Priority            => -1,
5244          --  Priority of main program, -1 if no pragma Priority used
5246          Time_Slice_Value         => -1,
5247          --  Time slice from Time_Slice pragma, -1 if none used
5249          WC_Encoding              => 'b',
5250          --  Wide_Character encoding used, default is brackets
5252          Locking_Policy           => ' ',
5253          --  Locking_Policy used, default of space means not
5254          --  specified, otherwise it is the first character of
5255          --  the policy name.
5257          Queuing_Policy           => ' ',
5258          --  Queuing_Policy used, default of space means not
5259          --  specified, otherwise it is the first character of
5260          --  the policy name.
5262          Task_Dispatching_Policy  => ' ',
5263          --  Task_Dispatching_Policy used, default of space means
5264          --  not specified, otherwise first character of the
5265          --  policy name.
5267          Adafinal                 => System.Null_Address,
5268          --  Address of Adafinal routine, not used anymore
5270          Unreserve_All_Interrupts => 0,
5271          --  Set true if pragma Unreserve_All_Interrupts was used
5273          Exception_Tracebacks     => 0);
5274          --  Indicates if exception tracebacks are enabled
5276       Elab_Final_Code := 1;
5278       --  Now we have the elaboration calls for all units in the partition.
5279       --  The Elab_Spec and Elab_Body attributes generate references to the
5280       --  implicit elaboration procedures generated by the compiler for
5281       --  each unit that requires elaboration.
5283       if not E040 then
5284          Interfaces.C_Streams'Elab_Spec;
5285       end if;
5286       E040 := True;
5287       if not E008 then
5288          Ada.Exceptions'Elab_Spec;
5289       end if;
5290       if not E014 then
5291          System.Exception_Table'Elab_Body;
5292          E014 := True;
5293       end if;
5294       if not E053 then
5295          Ada.Io_Exceptions'Elab_Spec;
5296          E053 := True;
5297       end if;
5298       if not E017 then
5299          System.Exceptions'Elab_Spec;
5300          E017 := True;
5301       end if;
5302       if not E030 then
5303          System.Stack_Checking'Elab_Spec;
5304       end if;
5305       if not E028 then
5306          System.Soft_Links'Elab_Body;
5307          E028 := True;
5308       end if;
5309       E030 := True;
5310       if not E024 then
5311          System.Secondary_Stack'Elab_Body;
5312          E024 := True;
5313       end if;
5314       if not E035 then
5315          Ada.Tags'Elab_Spec;
5316       end if;
5317       if not E035 then
5318          Ada.Tags'Elab_Body;
5319          E035 := True;
5320       end if;
5321       if not E033 then
5322          Ada.Streams'Elab_Spec;
5323          E033 := True;
5324       end if;
5325       if not E046 then
5326          System.Finalization_Root'Elab_Spec;
5327       end if;
5328       E046 := True;
5329       if not E008 then
5330          Ada.Exceptions'Elab_Body;
5331          E008 := True;
5332       end if;
5333       if not E048 then
5334          System.Finalization_Implementation'Elab_Spec;
5335       end if;
5336       if not E048 then
5337          System.Finalization_Implementation'Elab_Body;
5338          E048 := True;
5339       end if;
5340       if not E044 then
5341          Ada.Finalization'Elab_Spec;
5342       end if;
5343       E044 := True;
5344       if not E057 then
5345          Ada.Finalization.List_Controller'Elab_Spec;
5346       end if;
5347       E057 := True;
5348       if not E055 then
5349          System.File_Control_Block'Elab_Spec;
5350          E055 := True;
5351       end if;
5352       if not E042 then
5353          System.File_Io'Elab_Body;
5354          E042 := True;
5355       end if;
5356       if not E006 then
5357          Ada.Text_Io'Elab_Spec;
5358       end if;
5359       if not E006 then
5360          Ada.Text_Io'Elab_Body;
5361          E006 := True;
5362       end if;
5364       Elab_Final_Code := 0;
5365    end adainit;
5367    --------------
5368    -- adafinal --
5369    --------------
5371 @findex adafinal
5372    procedure adafinal is
5373    begin
5374       Do_Finalize;
5375    end adafinal;
5377    ----------
5378    -- main --
5379    ----------
5381    --  main is actually a function, as in the ANSI C standard,
5382    --  defined to return the exit status. The three parameters
5383    --  are the argument count, argument values and environment
5384    --  pointer.
5386 @findex Main Program
5387    function main
5388      (argc : Integer;
5389       argv : System.Address;
5390       envp : System.Address)
5391       return Integer
5392    is
5393       --  The initialize routine performs low level system
5394       --  initialization using a standard library routine which
5395       --  sets up signal handling and performs any other
5396       --  required setup. The routine can be found in file
5397       --  A-INIT.C.
5399 @findex __gnat_initialize
5400       procedure initialize;
5401       pragma Import (C, initialize, "__gnat_initialize");
5403       --  The finalize routine performs low level system
5404       --  finalization using a standard library routine. The
5405       --  routine is found in file A-FINAL.C and in the standard
5406       --  distribution is a dummy routine that does nothing, so
5407       --  really this is a hook for special user finalization.
5409 @findex __gnat_finalize
5410       procedure finalize;
5411       pragma Import (C, finalize, "__gnat_finalize");
5413       --  We get to the main program of the partition by using
5414       --  pragma Import because if we try to with the unit and
5415       --  call it Ada style, then not only do we waste time
5416       --  recompiling it, but also, we don't really know the right
5417       --  qualifiers (e.g. identifier character set) to be used
5418       --  to compile it.
5420       procedure Ada_Main_Program;
5421       pragma Import (Ada, Ada_Main_Program, "_ada_hello");
5423    --  Start of processing for main
5425    begin
5426       --  Save global variables
5428       gnat_argc := argc;
5429       gnat_argv := argv;
5430       gnat_envp := envp;
5432       --  Call low level system initialization
5434       Initialize;
5436       --  Call our generated Ada initialization routine
5438       adainit;
5440       --  This is the point at which we want the debugger to get
5441       --  control
5443       Break_Start;
5445       --  Now we call the main program of the partition
5447       Ada_Main_Program;
5449       --  Perform Ada finalization
5451       adafinal;
5453       --  Perform low level system finalization
5455       Finalize;
5457       --  Return the proper exit status
5458       return (gnat_exit_status);
5459    end;
5461 --  This section is entirely comments, so it has no effect on the
5462 --  compilation of the Ada_Main package. It provides the list of
5463 --  object files and linker options, as well as some standard
5464 --  libraries needed for the link. The GNAT LINK utility parses
5465 --  this B~HELLO.ADB file to read these comment lines to generate
5466 --  the appropriate command line arguments for the call to the
5467 --  system linker. The BEGIN/END lines are used for sentinels for
5468 --  this parsing operation.
5470 --  The exact file names will of course depend on the environment,
5471 --  host/target and location of files on the host system.
5473 @findex Object file list
5474 -- BEGIN Object file/option list
5475    --   ./HELLO.OBJ
5476    --   -L./
5477    --   -L/usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/
5478    --   /usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a
5479 -- END Object file/option list
5481 end ada_main;
5483 @end smallexample
5485 @noindent
5486 The Ada code in the above example is exactly what is generated by the
5487 binder. We have added comments to more clearly indicate the function
5488 of each part of the generated @code{Ada_Main} package.
5490 The code is standard Ada in all respects, and can be processed by any
5491 tools that handle Ada. In particular, it is possible to use the debugger
5492 in Ada mode to debug the generated Ada_Main package. For example, suppose
5493 that for reasons that you do not understand, your program is blowing up
5494 during elaboration of the body of @code{Ada.Text_IO}. To chase this bug
5495 down, you can place a breakpoint on the call:
5497 @smallexample
5498 Ada.Text_Io'Elab_Body;
5499 @end smallexample
5501 @noindent
5502 and trace the elaboration routine for this package to find out where
5503 the problem might be (more usually of course you would be debugging
5504 elaboration code in your own application).
5506 @node Generating the Binder Program in C
5507 @section Generating the Binder Program in C
5508 @noindent
5509 In most normal usage, the default mode of @code{GNAT BIND} which is to
5510 generate the main package in Ada, as described in the previous section.
5511 In particular, this means that any Ada programmer can read and understand
5512 the generated main program. It can also be debugged just like any other
5513 Ada code provided the @code{-g} qualifier is used for @code{GNAT BIND}
5514 and @code{GNAT LINK}.
5516 However for some purposes it may be convenient to generate the main
5517 program in C rather than Ada. This may for example be helpful when you
5518 are generating a mixed language program with the main program in C. The
5519 GNAT compiler itself is an example. The use of the @code{-C} qualifier
5520 for both @code{GNAT BIND} and @code{GNAT LINK} will cause the program to
5521 be generated in C (and compiled using the gnu C compiler). The
5522 following shows the C code generated for the same "Hello World"
5523 program:
5525 @smallexample
5527 #ifdef __STDC__
5528 #define PARAMS(paramlist) paramlist
5529 #else
5530 #define PARAMS(paramlist) ()
5531 #endif
5533 extern void __gnat_set_globals
5534  PARAMS ((int, int, int, int, int, int,
5535           void (*) PARAMS ((void)), int, int));
5536 extern void adafinal PARAMS ((void));
5537 extern void adainit PARAMS ((void));
5538 extern void system__standard_library__adafinal PARAMS ((void));
5539 extern int main PARAMS ((int, char **, char **));
5540 extern void exit PARAMS ((int));
5541 extern void __gnat_break_start PARAMS ((void));
5542 extern void _ada_hello PARAMS ((void));
5543 extern void __gnat_initialize PARAMS ((void));
5544 extern void __gnat_finalize PARAMS ((void));
5546 extern void ada__exceptions___elabs PARAMS ((void));
5547 extern void system__exceptions___elabs PARAMS ((void));
5548 extern void interfaces__c_streams___elabs PARAMS ((void));
5549 extern void system__exception_table___elabb PARAMS ((void));
5550 extern void ada__io_exceptions___elabs PARAMS ((void));
5551 extern void system__stack_checking___elabs PARAMS ((void));
5552 extern void system__soft_links___elabb PARAMS ((void));
5553 extern void system__secondary_stack___elabb PARAMS ((void));
5554 extern void ada__tags___elabs PARAMS ((void));
5555 extern void ada__tags___elabb PARAMS ((void));
5556 extern void ada__streams___elabs PARAMS ((void));
5557 extern void system__finalization_root___elabs PARAMS ((void));
5558 extern void ada__exceptions___elabb PARAMS ((void));
5559 extern void system__finalization_implementation___elabs PARAMS ((void));
5560 extern void system__finalization_implementation___elabb PARAMS ((void));
5561 extern void ada__finalization___elabs PARAMS ((void));
5562 extern void ada__finalization__list_controller___elabs PARAMS ((void));
5563 extern void system__file_control_block___elabs PARAMS ((void));
5564 extern void system__file_io___elabb PARAMS ((void));
5565 extern void ada__text_io___elabs PARAMS ((void));
5566 extern void ada__text_io___elabb PARAMS ((void));
5568 extern int __gnat_inside_elab_final_code;
5570 extern int gnat_argc;
5571 extern char **gnat_argv;
5572 extern char **gnat_envp;
5573 extern int gnat_exit_status;
5575 char __gnat_version[] = "GNAT Version: 3.15w (20010315)";
5576 void adafinal () @{
5577    system__standard_library__adafinal ();
5580 void adainit ()
5582    extern char ada__exceptions_E;
5583    extern char system__exceptions_E;
5584    extern char interfaces__c_streams_E;
5585    extern char system__exception_table_E;
5586    extern char ada__io_exceptions_E;
5587    extern char system__secondary_stack_E;
5588    extern char system__stack_checking_E;
5589    extern char system__soft_links_E;
5590    extern char ada__tags_E;
5591    extern char ada__streams_E;
5592    extern char system__finalization_root_E;
5593    extern char system__finalization_implementation_E;
5594    extern char ada__finalization_E;
5595    extern char ada__finalization__list_controller_E;
5596    extern char system__file_control_block_E;
5597    extern char system__file_io_E;
5598    extern char ada__text_io_E;
5600    extern void *__gnat_hello__SDP;
5601    extern void *__gnat_ada__text_io__SDP;
5602    extern void *__gnat_ada__exceptions__SDP;
5603    extern void *__gnat_gnat__heap_sort_a__SDP;
5604    extern void *__gnat_system__exception_table__SDP;
5605    extern void *__gnat_system__machine_state_operations__SDP;
5606    extern void *__gnat_system__secondary_stack__SDP;
5607    extern void *__gnat_system__parameters__SDP;
5608    extern void *__gnat_system__soft_links__SDP;
5609    extern void *__gnat_system__stack_checking__SDP;
5610    extern void *__gnat_system__traceback__SDP;
5611    extern void *__gnat_ada__streams__SDP;
5612    extern void *__gnat_ada__tags__SDP;
5613    extern void *__gnat_system__string_ops__SDP;
5614    extern void *__gnat_interfaces__c_streams__SDP;
5615    extern void *__gnat_system__file_io__SDP;
5616    extern void *__gnat_ada__finalization__SDP;
5617    extern void *__gnat_system__finalization_root__SDP;
5618    extern void *__gnat_system__finalization_implementation__SDP;
5619    extern void *__gnat_system__string_ops_concat_3__SDP;
5620    extern void *__gnat_system__stream_attributes__SDP;
5621    extern void *__gnat_system__file_control_block__SDP;
5622    extern void *__gnat_ada__finalization__list_controller__SDP;
5624    void **st[23] = @{
5625      &__gnat_hello__SDP,
5626      &__gnat_ada__text_io__SDP,
5627      &__gnat_ada__exceptions__SDP,
5628      &__gnat_gnat__heap_sort_a__SDP,
5629      &__gnat_system__exception_table__SDP,
5630      &__gnat_system__machine_state_operations__SDP,
5631      &__gnat_system__secondary_stack__SDP,
5632      &__gnat_system__parameters__SDP,
5633      &__gnat_system__soft_links__SDP,
5634      &__gnat_system__stack_checking__SDP,
5635      &__gnat_system__traceback__SDP,
5636      &__gnat_ada__streams__SDP,
5637      &__gnat_ada__tags__SDP,
5638      &__gnat_system__string_ops__SDP,
5639      &__gnat_interfaces__c_streams__SDP,
5640      &__gnat_system__file_io__SDP,
5641      &__gnat_ada__finalization__SDP,
5642      &__gnat_system__finalization_root__SDP,
5643      &__gnat_system__finalization_implementation__SDP,
5644      &__gnat_system__string_ops_concat_3__SDP,
5645      &__gnat_system__stream_attributes__SDP,
5646      &__gnat_system__file_control_block__SDP,
5647      &__gnat_ada__finalization__list_controller__SDP@};
5649    extern void ada__exceptions___elabs ();
5650    extern void system__exceptions___elabs ();
5651    extern void interfaces__c_streams___elabs ();
5652    extern void system__exception_table___elabb ();
5653    extern void ada__io_exceptions___elabs ();
5654    extern void system__stack_checking___elabs ();
5655    extern void system__soft_links___elabb ();
5656    extern void system__secondary_stack___elabb ();
5657    extern void ada__tags___elabs ();
5658    extern void ada__tags___elabb ();
5659    extern void ada__streams___elabs ();
5660    extern void system__finalization_root___elabs ();
5661    extern void ada__exceptions___elabb ();
5662    extern void system__finalization_implementation___elabs ();
5663    extern void system__finalization_implementation___elabb ();
5664    extern void ada__finalization___elabs ();
5665    extern void ada__finalization__list_controller___elabs ();
5666    extern void system__file_control_block___elabs ();
5667    extern void system__file_io___elabb ();
5668    extern void ada__text_io___elabs ();
5669    extern void ada__text_io___elabb ();
5671    void (*ea[23]) () = @{
5672      adainit,
5673      system__standard_library__adafinal,
5674      ada__exceptions___elabs,
5675      system__exceptions___elabs,
5676      interfaces__c_streams___elabs,
5677      system__exception_table___elabb,
5678      ada__io_exceptions___elabs,
5679      system__stack_checking___elabs,
5680      system__soft_links___elabb,
5681      system__secondary_stack___elabb,
5682      ada__tags___elabs,
5683      ada__tags___elabb,
5684      ada__streams___elabs,
5685      system__finalization_root___elabs,
5686      ada__exceptions___elabb,
5687      system__finalization_implementation___elabs,
5688      system__finalization_implementation___elabb,
5689      ada__finalization___elabs,
5690      ada__finalization__list_controller___elabs,
5691      system__file_control_block___elabs,
5692      system__file_io___elabb,
5693      ada__text_io___elabs,
5694      ada__text_io___elabb@};
5696    __gnat_SDP_Table_Build (&st, 23, ea, 23);
5697    __gnat_set_globals (
5698       -1,      /* Main_Priority              */
5699       -1,      /* Time_Slice_Value           */
5700       'b',     /* WC_Encoding                */
5701       ' ',     /* Locking_Policy             */
5702       ' ',     /* Queuing_Policy             */
5703       ' ',     /* Tasking_Dispatching_Policy */
5704       0,       /* Finalization routine address, not used anymore */
5705       0,       /* Unreserve_All_Interrupts */
5706       0);      /* Exception_Tracebacks */
5708    __gnat_inside_elab_final_code = 1;
5710    if (ada__exceptions_E == 0) @{
5711       ada__exceptions___elabs ();
5712    @}
5713    if (system__exceptions_E == 0) @{
5714       system__exceptions___elabs ();
5715       system__exceptions_E++;
5716    @}
5717    if (interfaces__c_streams_E == 0) @{
5718       interfaces__c_streams___elabs ();
5719    @}
5720    interfaces__c_streams_E = 1;
5721    if (system__exception_table_E == 0) @{
5722       system__exception_table___elabb ();
5723       system__exception_table_E++;
5724    @}
5725    if (ada__io_exceptions_E == 0) @{
5726       ada__io_exceptions___elabs ();
5727       ada__io_exceptions_E++;
5728    @}
5729    if (system__stack_checking_E == 0) @{
5730       system__stack_checking___elabs ();
5731    @}
5732    if (system__soft_links_E == 0) @{
5733       system__soft_links___elabb ();
5734       system__soft_links_E++;
5735    @}
5736    system__stack_checking_E = 1;
5737    if (system__secondary_stack_E == 0) @{
5738       system__secondary_stack___elabb ();
5739       system__secondary_stack_E++;
5740    @}
5741    if (ada__tags_E == 0) @{
5742       ada__tags___elabs ();
5743    @}
5744    if (ada__tags_E == 0) @{
5745       ada__tags___elabb ();
5746       ada__tags_E++;
5747    @}
5748    if (ada__streams_E == 0) @{
5749       ada__streams___elabs ();
5750       ada__streams_E++;
5751    @}
5752    if (system__finalization_root_E == 0) @{
5753       system__finalization_root___elabs ();
5754    @}
5755    system__finalization_root_E = 1;
5756    if (ada__exceptions_E == 0) @{
5757       ada__exceptions___elabb ();
5758       ada__exceptions_E++;
5759    @}
5760    if (system__finalization_implementation_E == 0) @{
5761       system__finalization_implementation___elabs ();
5762    @}
5763    if (system__finalization_implementation_E == 0) @{
5764       system__finalization_implementation___elabb ();
5765       system__finalization_implementation_E++;
5766    @}
5767    if (ada__finalization_E == 0) @{
5768       ada__finalization___elabs ();
5769    @}
5770    ada__finalization_E = 1;
5771    if (ada__finalization__list_controller_E == 0) @{
5772       ada__finalization__list_controller___elabs ();
5773    @}
5774    ada__finalization__list_controller_E = 1;
5775    if (system__file_control_block_E == 0) @{
5776       system__file_control_block___elabs ();
5777       system__file_control_block_E++;
5778    @}
5779    if (system__file_io_E == 0) @{
5780       system__file_io___elabb ();
5781       system__file_io_E++;
5782    @}
5783    if (ada__text_io_E == 0) @{
5784       ada__text_io___elabs ();
5785    @}
5786    if (ada__text_io_E == 0) @{
5787       ada__text_io___elabb ();
5788       ada__text_io_E++;
5789    @}
5791    __gnat_inside_elab_final_code = 0;
5793 int main (argc, argv, envp)
5794     int argc;
5795     char **argv;
5796     char **envp;
5798    gnat_argc = argc;
5799    gnat_argv = argv;
5800    gnat_envp = envp;
5802    __gnat_initialize ();
5803    adainit ();
5804    __gnat_break_start ();
5806    _ada_hello ();
5808    system__standard_library__adafinal ();
5809    __gnat_finalize ();
5810    exit (gnat_exit_status);
5812 unsigned helloB = 0x7880BEB3;
5813 unsigned system__standard_libraryB = 0x0D24CBD0;
5814 unsigned system__standard_libraryS = 0x3283DBEB;
5815 unsigned adaS = 0x2359F9ED;
5816 unsigned ada__text_ioB = 0x47C85FC4;
5817 unsigned ada__text_ioS = 0x496FE45C;
5818 unsigned ada__exceptionsB = 0x74F50187;
5819 unsigned ada__exceptionsS = 0x6736945B;
5820 unsigned gnatS = 0x156A40CF;
5821 unsigned gnat__heap_sort_aB = 0x033DABE0;
5822 unsigned gnat__heap_sort_aS = 0x6AB38FEA;
5823 unsigned systemS = 0x0331C6FE;
5824 unsigned system__exceptionsS = 0x20C9ECA4;
5825 unsigned system__exception_tableB = 0x68A22947;
5826 unsigned system__exception_tableS = 0x394BADD5;
5827 unsigned gnat__htableB = 0x08258E1B;
5828 unsigned gnat__htableS = 0x367D5222;
5829 unsigned system__machine_state_operationsB = 0x4F3B7492;
5830 unsigned system__machine_state_operationsS = 0x182F5CF4;
5831 unsigned system__storage_elementsB = 0x2F1EB794;
5832 unsigned system__storage_elementsS = 0x102C83C7;
5833 unsigned system__secondary_stackB = 0x1574B6E9;
5834 unsigned system__secondary_stackS = 0x708E260A;
5835 unsigned system__parametersB = 0x56D770CD;
5836 unsigned system__parametersS = 0x237E39BE;
5837 unsigned system__soft_linksB = 0x08AB6B2C;
5838 unsigned system__soft_linksS = 0x1E2491F3;
5839 unsigned system__stack_checkingB = 0x476457A0;
5840 unsigned system__stack_checkingS = 0x5299FCED;
5841 unsigned system__tracebackB = 0x2971EBDE;
5842 unsigned system__tracebackS = 0x2E9C3122;
5843 unsigned ada__streamsS = 0x7C25DE96;
5844 unsigned ada__tagsB = 0x39ADFFA2;
5845 unsigned ada__tagsS = 0x769A0464;
5846 unsigned system__string_opsB = 0x5EB646AB;
5847 unsigned system__string_opsS = 0x63CED018;
5848 unsigned interfacesS = 0x0357E00A;
5849 unsigned interfaces__c_streamsB = 0x3784FB72;
5850 unsigned interfaces__c_streamsS = 0x2E723019;
5851 unsigned system__file_ioB = 0x623358EA;
5852 unsigned system__file_ioS = 0x31F873E6;
5853 unsigned ada__finalizationB = 0x6843F68A;
5854 unsigned ada__finalizationS = 0x63305874;
5855 unsigned system__finalization_rootB = 0x31E56CE1;
5856 unsigned system__finalization_rootS = 0x23169EF3;
5857 unsigned system__finalization_implementationB = 0x6CCBA70E;
5858 unsigned system__finalization_implementationS = 0x604AA587;
5859 unsigned system__string_ops_concat_3B = 0x572E3F58;
5860 unsigned system__string_ops_concat_3S = 0x01F57876;
5861 unsigned system__stream_attributesB = 0x1D4F93E8;
5862 unsigned system__stream_attributesS = 0x30B2EC3D;
5863 unsigned ada__io_exceptionsS = 0x34054F96;
5864 unsigned system__unsigned_typesS = 0x7B9E7FE3;
5865 unsigned system__file_control_blockS = 0x2FF876A8;
5866 unsigned ada__finalization__list_controllerB = 0x5760634A;
5867 unsigned ada__finalization__list_controllerS = 0x5D851835;
5869 /* BEGIN ELABORATION ORDER
5870 ada (spec)
5871 gnat (spec)
5872 gnat.heap_sort_a (spec)
5873 gnat.htable (spec)
5874 gnat.htable (body)
5875 interfaces (spec)
5876 system (spec)
5877 system.parameters (spec)
5878 system.standard_library (spec)
5879 ada.exceptions (spec)
5880 system.exceptions (spec)
5881 system.parameters (body)
5882 gnat.heap_sort_a (body)
5883 interfaces.c_streams (spec)
5884 interfaces.c_streams (body)
5885 system.exception_table (spec)
5886 system.exception_table (body)
5887 ada.io_exceptions (spec)
5888 system.storage_elements (spec)
5889 system.storage_elements (body)
5890 system.machine_state_operations (spec)
5891 system.machine_state_operations (body)
5892 system.secondary_stack (spec)
5893 system.stack_checking (spec)
5894 system.soft_links (spec)
5895 system.soft_links (body)
5896 system.stack_checking (body)
5897 system.secondary_stack (body)
5898 system.standard_library (body)
5899 system.string_ops (spec)
5900 system.string_ops (body)
5901 ada.tags (spec)
5902 ada.tags (body)
5903 ada.streams (spec)
5904 system.finalization_root (spec)
5905 system.finalization_root (body)
5906 system.string_ops_concat_3 (spec)
5907 system.string_ops_concat_3 (body)
5908 system.traceback (spec)
5909 system.traceback (body)
5910 ada.exceptions (body)
5911 system.unsigned_types (spec)
5912 system.stream_attributes (spec)
5913 system.stream_attributes (body)
5914 system.finalization_implementation (spec)
5915 system.finalization_implementation (body)
5916 ada.finalization (spec)
5917 ada.finalization (body)
5918 ada.finalization.list_controller (spec)
5919 ada.finalization.list_controller (body)
5920 system.file_control_block (spec)
5921 system.file_io (spec)
5922 system.file_io (body)
5923 ada.text_io (spec)
5924 ada.text_io (body)
5925 hello (body)
5926    END ELABORATION ORDER */
5928 /* BEGIN Object file/option list
5929 ./HELLO.OBJ
5930 -L./
5931 -L/usr/local/gnat/lib/gcc-lib/alpha-dec-osf5.1/2.8.1/adalib/
5932 /usr/local/gnat/lib/gcc-lib/alpha-dec-osf5.1/2.8.1/adalib/libgnat.a
5933 -lexc
5934    END Object file/option list */
5936 @end smallexample
5938 @noindent
5939 Here again, the C code is exactly what is generated by the binder. The
5940 functions of the various parts of this code correspond in an obvious
5941 manner with the commented Ada code shown in the example in the previous
5942 section.
5944 @node Consistency-Checking Modes
5945 @section Consistency-Checking Modes
5947 @noindent
5948 As described in the previous section, by default @code{GNAT BIND} checks
5949 that object files are consistent with one another and are consistent
5950 with any source files it can locate. The following qualifiers control binder
5951 access to sources.
5953 @table @code
5954 @item /READ_SOURCES=ALL
5955 @cindex @code{/READ_SOURCES=ALL} (@code{GNAT BIND})
5956 Require source files to be present. In this mode, the binder must be
5957 able to locate all source files that are referenced, in order to check
5958 their consistency. In normal mode, if a source file cannot be located it
5959 is simply ignored. If you specify this qualifier, a missing source
5960 file is an error.
5962 @item /READ_SOURCES=NONE
5963 @cindex @code{/READ_SOURCES=NONE} (@code{GNAT BIND})
5964 Exclude source files. In this mode, the binder only checks that ALI
5965 files are consistent with one another. Source files are not accessed.
5966 The binder runs faster in this mode, and there is still a guarantee that
5967 the resulting program is self-consistent.
5968 If a source file has been edited since it was last compiled, and you
5969 specify this qualifier, the binder will not detect that the object
5970 file is out of date with respect to the source file. Note that this is the
5971 mode that is automatically used by @code{GNAT MAKE} because in this
5972 case the checking against sources has already been performed by
5973 @code{GNAT MAKE} in the course of compilation (i.e. before binding).
5975 @item /READ_SOURCES=AVAILABLE
5976 This is the default mode in which source files are checked if they are
5977 available, and ignored if they are not available.
5978 @end table
5980 @node Binder Error Message Control
5981 @section Binder Error Message Control
5983 @noindent
5984 The following qualifiers provide control over the generation of error
5985 messages from the binder:
5987 @table @code
5988 @item /REPORT_ERRORS=VERBOSE
5989 @cindex @code{/REPORT_ERRORS=VERBOSE} (@code{GNAT BIND})
5990 Verbose mode. In the normal mode, brief error messages are generated to
5991 @file{SYS$ERROR}. If this qualifier is present, a header is written
5992 to @file{SYS$OUTPUT} and any error messages are directed to @file{SYS$OUTPUT}.
5993 All that is written to @file{SYS$ERROR} is a brief summary message.
5995 @item /REPORT_ERRORS=BRIEF
5996 @cindex @code{/REPORT_ERRORS=BRIEF} (@code{GNAT BIND})
5997 Generate brief error messages to @file{SYS$ERROR} even if verbose mode is
5998 specified. This is relevant only when used with the
5999 @code{/REPORT_ERRORS=VERBOSE} qualifier.
6002 @item /WARNINGS=SUPPRESS
6003 @cindex @code{/WARNINGS=SUPPRESS} (@code{GNAT BIND})
6004 @cindex Warnings
6005 Suppress all warning messages.
6007 @item /WARNINGS=ERROR
6008 @cindex @code{/WARNINGS=ERROR} (@code{GNAT BIND})
6009 Treat any warning messages as fatal errors.
6011 @item /WARNINGS=NORMAL
6012 Standard mode with warnings generated, but warnings do not get treated
6013 as errors.
6015 @item /NOTIME_STAMP_CHECK
6016 @cindex @code{/NOTIME_STAMP_CHECK} (@code{GNAT BIND})
6017 @cindex Time stamp checks, in binder
6018 @cindex Binder consistency checks
6019 @cindex Consistency checks, in binder
6020 The binder performs a number of consistency checks including:
6022 @itemize @bullet
6023 @item
6024 Check that time stamps of a given source unit are consistent
6025 @item
6026 Check that checksums of a given source unit are consistent
6027 @item
6028 Check that consistent versions of @code{GNAT} were used for compilation
6029 @item
6030 Check consistency of configuration pragmas as required
6031 @end itemize
6033 @noindent
6034 Normally failure of such checks, in accordance with the consistency
6035 requirements of the Ada Reference Manual, causes error messages to be
6036 generated which abort the binder and prevent the output of a binder
6037 file and subsequent link to obtain an executable.
6039 The @code{/NOTIME_STAMP_CHECK} qualifier converts these error messages
6040 into warnings, so that
6041 binding and linking can continue to completion even in the presence of such
6042 errors. The result may be a failed link (due to missing symbols), or a
6043 non-functional executable which has undefined semantics.
6044 @emph{This means that
6045 @code{/NOTIME_STAMP_CHECK} should be used only in unusual situations,
6046 with extreme care.}
6047 @end table
6049 @node Elaboration Control
6050 @section Elaboration Control
6052 @noindent
6053 The following qualifiers provide additional control over the elaboration
6054 order. For full details see @xref{Elaboration Order Handling in GNAT}.
6056 @table @code
6057 @item /PESSIMISTIC_ELABORATION
6058 @cindex @code{/PESSIMISTIC_ELABORATION} (@code{GNAT BIND})
6059 Normally the binder attempts to choose an elaboration order that is
6060 likely to minimize the likelihood of an elaboration order error resulting
6061 in raising a @code{Program_Error} exception. This qualifier reverses the
6062 action of the binder, and requests that it deliberately choose an order
6063 that is likely to maximize the likelihood of an elaboration error.
6064 This is useful in ensuring portability and avoiding dependence on
6065 accidental fortuitous elaboration ordering.
6067 Normally it only makes sense to use the @code{-p} qualifier if dynamic
6068 elaboration checking is used (@option{/CHECKS=ELABORATION} qualifier used for compilation).
6069 This is because in the default static elaboration mode, all necessary
6070 @code{Elaborate_All} pragmas are implicitly inserted. These implicit
6071 pragmas are still respected by the binder in @code{-p} mode, so a
6072 safe elaboration order is assured.
6073 @end table
6075 @node Output Control
6076 @section Output Control
6078 @noindent
6079 The following qualifiers allow additional control over the output
6080 generated by the binder.
6082 @table @code
6084 @item /BIND_FILE=ADA
6085 @cindex @code{/BIND_FILE=ADA} (@code{GNAT BIND})
6086 Generate binder program in Ada (default). The binder program is named
6087 @file{B$@var{mainprog}.ADB} by default. This can be changed with
6088 @code{-o} @code{GNAT BIND} option.
6090 @item /NOOUTPUT
6091 @cindex @code{/NOOUTPUT} (@code{GNAT BIND})
6092 Check only. Do not generate the binder output file. In this mode the
6093 binder performs all error checks but does not generate an output file.
6095 @item /BIND_FILE=C
6096 @cindex @code{/BIND_FILE=C} (@code{GNAT BIND})
6097 Generate binder program in C. The binder program is named
6098 @file{B_@var{mainprog}.C}. This can be changed with @code{-o} @code{GNAT BIND}
6099 option.
6101 @item /ELABORATION_DEPENDENCIES
6102 @cindex @code{/ELABORATION_DEPENDENCIES} (@code{GNAT BIND})
6103 Output complete list of elaboration-order dependencies, showing the
6104 reason for each dependency. This output can be rather extensive but may
6105 be useful in diagnosing problems with elaboration order. The output is
6106 written to @file{SYS$OUTPUT}.
6108 @item /HELP
6109 @cindex @code{/HELP} (@code{GNAT BIND})
6110 Output usage information. The output is written to @file{SYS$OUTPUT}.
6112 @item /LINKER_OPTION_LIST
6113 @cindex @code{/LINKER_OPTION_LIST} (@code{GNAT BIND})
6114 Output linker options to @file{SYS$OUTPUT}. Includes library search paths,
6115 contents of pragmas Ident and Linker_Options, and libraries added
6116 by @code{GNAT BIND}.
6118 @item /ORDER_OF_ELABORATION
6119 @cindex @code{/ORDER_OF_ELABORATION} (@code{GNAT BIND})
6120 Output chosen elaboration order. The output is written to @file{SYS$OUTPUT}.
6122 @item /OBJECT_LIST
6123 @cindex @code{/OBJECT_LIST} (@code{GNAT BIND})
6124 Output full names of all the object files that must be linked to provide
6125 the Ada component of the program. The output is written to @file{SYS$OUTPUT}.
6126 This list includes the files explicitly supplied and referenced by the user
6127 as well as implicitly referenced run-time unit files. The latter are
6128 omitted if the corresponding units reside in shared libraries. The
6129 directory names for the run-time units depend on the system configuration.
6131 @item /OUTPUT=@var{file}
6132 @cindex @code{/OUTPUT} (@code{GNAT BIND})
6133 Set name of output file to @var{file} instead of the normal
6134 @file{B$@var{mainprog}.ADB} default. Note that @var{file} denote the Ada
6135 binder generated body filename. In C mode you would normally give
6136 @var{file} an extension of @file{.C} because it will be a C source program.
6137 Note that if this option is used, then linking must be done manually.
6138 It is not possible to use GNAT LINK in this case, since it cannot locate
6139 the binder file.
6141 @item /RESTRICTION_LIST
6142 @cindex @code{/RESTRICTION_LIST} (@code{GNAT BIND})
6143 Generate list of @code{pragma Rerstrictions} that could be applied to
6144 the current unit. This is useful for code audit purposes, and also may
6145 be used to improve code generation in some cases.
6147 @end table
6149 @node Binding with Non-Ada Main Programs
6150 @section Binding with Non-Ada Main Programs
6152 @noindent
6153 In our description so far we have assumed that the main
6154 program is in Ada, and that the task of the binder is to generate a
6155 corresponding function @code{main} that invokes this Ada main
6156 program. GNAT also supports the building of executable programs where
6157 the main program is not in Ada, but some of the called routines are
6158 written in Ada and compiled using GNAT (@pxref{Mixed Language Programming}).
6159 The following qualifier is used in this situation:
6161 @table @code
6162 @item /NOMAIN
6163 @cindex @code{/NOMAIN} (@code{GNAT BIND})
6164 No main program. The main program is not in Ada.
6165 @end table
6167 @noindent
6168 In this case, most of the functions of the binder are still required,
6169 but instead of generating a main program, the binder generates a file
6170 containing the following callable routines:
6172 @table @code
6173 @item adainit
6174 @findex adainit
6175 You must call this routine to initialize the Ada part of the program by
6176 calling the necessary elaboration routines. A call to @code{adainit} is
6177 required before the first call to an Ada subprogram.
6179 Note that it is assumed that the basic execution environment must be setup
6180 to be appropriate for Ada execution at the point where the first Ada
6181 subprogram is called. In particular, if the Ada code will do any
6182 floating-point operations, then the FPU must be setup in an appropriate
6183 manner. For the case of the x86, for example, full precision mode is
6184 required. The procedure GNAT.Float_Control.Reset may be used to ensure
6185 that the FPU is in the right state.
6187 @item adafinal
6188 @findex adafinal
6189 You must call this routine to perform any library-level finalization
6190 required by the Ada subprograms. A call to @code{adafinal} is required
6191 after the last call to an Ada subprogram, and before the program
6192 terminates.
6193 @end table
6195 @noindent
6196 If the @code{/NOMAIN} qualifier
6197 @cindex Binder, multiple input files
6198 is given, more than one ALI file may appear on
6199 the command line for @code{GNAT BIND}. The normal @dfn{closure}
6200 calculation is performed for each of the specified units. Calculating
6201 the closure means finding out the set of units involved by tracing
6202 @code{with} references. The reason it is necessary to be able to
6203 specify more than one ALI file is that a given program may invoke two or
6204 more quite separate groups of Ada units.
6206 The binder takes the name of its output file from the last specified ALI
6207 file, unless overridden by the use of the @code{/OUTPUT=file}.
6208 The output is an Ada unit in source form that can
6209 be compiled with GNAT unless the -C qualifier is used in which case the
6210 output is a C source file, which must be compiled using the C compiler.
6211 This compilation occurs automatically as part of the @code{GNAT LINK}
6212 processing.
6214 Currently the GNAT run time requires a FPU using 80 bits mode
6215 precision. Under targets where this is not the default it is required to
6216 call GNAT.Float_Control.Reset before using floating point numbers (this
6217 include float computation, float input and output) in the Ada code. A
6218 side effect is that this could be the wrong mode for the foreign code
6219 where floating point computation could be broken after this call.
6221 @node Binding Programs with No Main Subprogram
6222 @section Binding Programs with No Main Subprogram
6224 @noindent
6225 It is possible to have an Ada program which does not have a main
6226 subprogram. This program will call the elaboration routines of all the
6227 packages, then the finalization routines.
6229 The following qualifier is used to bind programs organized in this manner:
6231 @table @code
6232 @item /ZERO_MAIN
6233 @cindex @code{/ZERO_MAIN} (@code{GNAT BIND})
6234 Normally the binder checks that the unit name given on the command line
6235 corresponds to a suitable main subprogram. When this qualifier is used,
6236 a list of ALI files can be given, and the execution of the program
6237 consists of elaboration of these units in an appropriate order.
6238 @end table
6240 @node Summary of Binder Qualifiers
6241 @section Summary of Binder Qualifiers
6243 @noindent
6244 The following are the qualifiers available with @code{GNAT BIND}:
6246 @table @code
6247 @item /OBJECT_SEARCH
6248 Specify directory to be searched for ALI files.
6250 @item /SOURCE_SEARCH
6251 Specify directory to be searched for source file.
6253 @item /BIND_FILE=ADA
6254 Generate binder program in Ada (default)
6256 @item /REPORT_ERRORS=BRIEF
6257 Generate brief messages to @file{SYS$ERROR} even if verbose mode set.
6259 @item /NOOUTPUT
6260 Check only, no generation of binder output file.
6262 @item /BIND_FILE=C
6263 Generate binder program in C
6265 @item /ELABORATION_DEPENDENCIES
6266 Output complete list of elaboration-order dependencies.
6268 @item -E
6269 Store tracebacks in exception occurrences when the target supports it.
6270 This is the default with the zero cost exception mechanism.
6271 This option is currently supported on the following targets:
6272 all x86 ports, Solaris, Windows, HP-UX, AIX, PowerPC VxWorks and Alpha VxWorks.
6273 See also the packages @code{GNAT.Traceback} and
6274 @code{GNAT.Traceback.Symbolic} for more information.
6275 Note that on x86 ports, you must not use @code{-fomit-frame-pointer}
6276 @code{GNAT COMPILE} option.
6278 @item -h
6279 Output usage (help) information
6281 @item /SEARCH
6282 Specify directory to be searched for source and ALI files.
6284 @item /NOCURRENT_DIRECTORY
6285 Do not look for sources in the current directory where @code{GNAT BIND} was
6286 invoked, and do not look for ALI files in the directory containing the
6287 ALI file named in the @code{GNAT BIND} command line.
6289 @item /ORDER_OF_ELABORATION
6290 Output chosen elaboration order.
6292 @item -Lxxx
6293 Binds the units for library building. In this case the adainit and
6294 adafinal procedures (See @pxref{Binding with Non-Ada Main Programs})
6295 are renamed to xxxinit and xxxfinal. Implies -n.
6297 @item -Mxyz
6298 Rename generated main program from main to xyz
6300 @item /ERROR_LIMIT=@var{n}
6301 Limit number of detected errors to @var{n} (1-999).
6303 @item /NOMAIN
6304 No main program.
6306 @item /NOSTD_INCLUDES
6307 Do not look for sources in the system default directory.
6309 @item /NOSTD_LIBRARIES
6310 Do not look for library files in the system default directory.
6312 @item /RUNTIME_SYSTEM=@var{rts-path}
6313 @cindex @code{/RUNTIME_SYSTEM} (@code{GNAT BIND})
6314 Specifies the default location of the runtime library. Same meaning as the
6315 equivalent @code{GNAT MAKE} flag (see @ref{Qualifiers for GNAT MAKE}).
6317 @item /OUTPUT=@var{file}
6318 Name the output file @var{file} (default is @file{B$@var{xxx}.ADB}).
6319 Note that if this option is used, then linking must be done manually,
6320 GNAT LINK cannot be used.
6322 @item /OBJECT_LIST
6323 Output object list.
6325 @item -p
6326 Pessimistic (worst-case) elaboration order
6328 @item /READ_SOURCES=ALL
6329 Require all source files to be present.
6332 @item /NOTIME_STAMP_CHECK
6333 Tolerate time stamp and other consistency errors
6335 @item -T@var{n}
6336 Set the time slice value to n microseconds. A value of zero means no time
6337 slicing and also indicates to the tasking run time to match as close as
6338 possible to the annex D requirements of the RM.
6340 @item /REPORT_ERRORS=VERBOSE
6341 Verbose mode. Write error messages, header, summary output to
6342 @file{SYS$OUTPUT}.
6345 @item /WARNINGS=NORMAL
6346 Normal warnings mode. Warnings are issued but ignored
6348 @item /WARNINGS=SUPPRESS
6349 All warning messages are suppressed
6351 @item /WARNINGS=ERROR
6352 Warning messages are treated as fatal errors
6354 @item /READ_SOURCES=NONE
6355 Exclude source files (check object consistency only).
6357 @item /READ_SOURCES=AVAILABLE
6358 Default mode, in which sources are checked for consistency only if
6359 they are available.
6361 @item /ZERO_MAIN
6362 No main subprogram.
6364 @end table
6367 @node Command-Line Access
6368 @section Command-Line Access
6370 @noindent
6371 The package @code{Ada.Command_Line} provides access to the command-line
6372 arguments and program name. In order for this interface to operate
6373 correctly, the two variables
6375 @smallexample
6376 @group
6377 @cartouche
6378 int gnat_argc;
6379 char **gnat_argv;
6380 @end cartouche
6381 @end group
6382 @end smallexample
6384 @noindent
6385 @findex gnat_argv
6386 @findex gnat_argc
6387 are declared in one of the GNAT library routines. These variables must
6388 be set from the actual @code{argc} and @code{argv} values passed to the
6389 main program. With no @code{/NOMAIN} present, @code{GNAT BIND}
6390 generates the C main program to automatically set these variables.
6391 If the @code{/NOMAIN} qualifier is used, there is no automatic way to
6392 set these variables. If they are not set, the procedures in
6393 @code{Ada.Command_Line} will not be available, and any attempt to use
6394 them will raise @code{Constraint_Error}. If command line access is
6395 required, your main program must set @code{gnat_argc} and
6396 @code{gnat_argv} from the @code{argc} and @code{argv} values passed to
6399 @node Search Paths for GNAT BIND
6400 @section Search Paths for @code{GNAT BIND}
6402 @noindent
6403 The binder takes the name of an ALI file as its argument and needs to
6404 locate source files as well as other ALI files to verify object consistency.
6406 For source files, it follows exactly the same search rules as @code{GNAT COMPILE}
6407 (@pxref{Search Paths and the Run-Time Library (RTL)}). For ALI files the
6408 directories searched are:
6410 @enumerate
6411 @item
6412 The directory containing the ALI file named in the command line, unless
6413 the qualifier @code{/NOCURRENT_DIRECTORY} is specified.
6415 @item
6416 All directories specified by @code{/SEARCH}
6417 qualifiers on the @code{GNAT BIND}
6418 command line, in the order given.
6420 @item
6421 @findex ADA_OBJECTS_PATH
6422 Each of the directories listed in the value of the
6423 @code{ADA_OBJECTS_PATH} logical name.
6424 Normally, define this value as a logical name containing a comma separated
6425 list of directory names.
6427 This variable can also be defined by means of an environment string
6428 (an argument to the DEC C exec* set of functions).
6430 Logical Name:
6431 @smallexample
6432 DEFINE ANOTHER_PATH FOO:[BAG]
6433 DEFINE ADA_OBJECTS_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
6434 @end smallexample
6436 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
6437 first, followed by the standard Ada 95
6438 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADALIB].
6439 If this is not redefined, the user will obtain the DEC Ada83 IO packages
6440 (Text_IO, Sequential_IO, etc)
6441 instead of the Ada95 packages. Thus, in order to get the Ada 95
6442 packages by default, ADA_OBJECTS_PATH must be redefined.
6444 @item
6445 The content of the "ada_object_path" file which is part of the GNAT
6446 installation tree and is used to store standard libraries such as the
6447 GNAT Run Time Library (RTL) unless the qualifier @code{/NOSTD_LIBRARIES} is
6448 specified.
6449 @end enumerate
6451 @noindent
6452 In the binder the qualifier @code{/SEARCH}
6453 is used to specify both source and
6454 library file paths. Use @code{/SOURCE_SEARCH}
6455 instead if you want to specify
6456 source paths only, and @code{/LIBRARY_SEARCH}
6457 if you want to specify library paths
6458 only. This means that for the binder
6459 @code{/SEARCH=}@var{dir} is equivalent to
6460 @code{/SOURCE_SEARCH=}@var{dir}
6461 @code{/OBJECT_SEARCH=}@var{dir}.
6462 The binder generates the bind file (a C language source file) in the
6463 current working directory.
6465 @findex Ada
6466 @findex System
6467 @findex Interfaces
6468 @findex GNAT
6469 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
6470 children make up the GNAT Run-Time Library, together with the package
6471 GNAT and its children, which contain a set of useful additional
6472 library functions provided by GNAT. The sources for these units are
6473 needed by the compiler and are kept together in one directory. The ALI
6474 files and object files generated by compiling the RTL are needed by the
6475 binder and the linker and are kept together in one directory, typically
6476 different from the directory containing the sources. In a normal
6477 installation, you need not specify these directory names when compiling
6478 or binding. Either the environment variables or the built-in defaults
6479 cause these files to be found.
6481 Besides simplifying access to the RTL, a major use of search paths is
6482 in compiling sources from multiple directories. This can make
6483 development environments much more flexible.
6485 @node Examples of GNAT BIND Usage
6486 @section Examples of @code{GNAT BIND} Usage
6488 @noindent
6489 This section contains a number of examples of using the GNAT binding
6490 utility @code{GNAT BIND}.
6492 @table @code
6493 @item GNAT BIND hello
6494 The main program @code{Hello} (source program in @file{HELLO.ADB}) is
6495 bound using the standard qualifier settings. The generated main program is
6496 @file{B~HELLO.ADB}. This is the normal, default use of the binder.
6498 @item GNAT BIND HELLO.ALI /OUTPUT=Mainprog.ADB
6499 The main program @code{Hello} (source program in @file{HELLO.ADB}) is
6500 bound using the standard qualifier settings. The generated main program is
6501 @file{MAINPROG.ADB} with the associated spec in
6502 @file{MAINPROG.ADS}. Note that you must specify the body here not the
6503 spec, in the case where the output is in Ada. Note that if this option
6504 is used, then linking must be done manually, since GNAT LINK will not
6505 be able to find the generated file.
6507 @item GNAT BIND MAIN.ALI /BIND_FILE=C /OUTPUT=Mainprog.C /READ_SOURCES=NONE
6508 The main program @code{Main} (source program in
6509 @file{MAIN.ADB}) is bound, excluding source files from the
6510 consistency checking, generating
6511 the file @file{MAINPROG.C}.
6514 @item GNAT BIND /NOMAIN math dbase /BIND_FILE=C /OUTPUT=ADA-CONTROL.C
6515 The main program is in a language other than Ada, but calls to
6516 subprograms in packages @code{Math} and @code{Dbase} appear. This call
6517 to @code{GNAT BIND} generates the file @file{ADA-CONTROL.C} containing
6518 the @code{adainit} and @code{adafinal} routines to be called before and
6519 after accessing the Ada units.
6520 @end table
6522 @node Linking Using GNAT LINK
6523 @chapter Linking Using @code{GNAT LINK}
6524 @findex GNAT LINK
6526 @noindent
6527 This chapter discusses @code{GNAT LINK}, a utility program used to link
6528 Ada programs and build an executable file. This is a simple program
6529 that invokes the Unix linker (via the @code{GNAT COMPILE}
6530 command) with a correct list of object files and library references.
6531 @code{GNAT LINK} automatically determines the list of files and
6532 references for the Ada part of a program. It uses the binder file
6533 generated by the binder to determine this list.
6535 @menu
6536 * Running GNAT LINK::
6537 * Qualifiers for GNAT LINK::
6538 * Setting Stack Size from GNAT LINK::
6539 * Setting Heap Size from GNAT LINK::
6540 @end menu
6542 @node Running GNAT LINK
6543 @section Running @code{GNAT LINK}
6545 @noindent
6546 The form of the @code{GNAT LINK} command is
6548 @smallexample
6549 $ GNAT LINK [@var{qualifiers}] @var{mainprog}[.ALI] [@var{non-Ada objects}]
6550       [@var{linker options}]
6551 @end smallexample
6553 @noindent
6554 @file{@var{mainprog}.ALI} references the ALI file of the main program.
6555 The @file{.ALI} extension of this file can be omitted. From this
6556 reference, @code{GNAT LINK} locates the corresponding binder file
6557 @file{B$@var{mainprog}.ADB} and, using the information in this file along
6558 with the list of non-Ada objects and linker options, constructs a Unix
6559 linker command file to create the executable.
6561 The arguments following @file{@var{mainprog}.ALI} are passed to the
6562 linker uninterpreted. They typically include the names of object files
6563 for units written in other languages than Ada and any library references
6564 required to resolve references in any of these foreign language units,
6565 or in @code{pragma Import} statements in any Ada units.
6567 @var{linker options} is an optional list of linker specific
6568 qualifiers. The default linker called by GNAT LINK is @var{GNAT COMPILE} which in
6569 turn calls the appropriate system linker usually called
6570 @var{ld}. Standard options for the linker such as @code{-lmy_lib} or
6571 @code{-Ldir} can be added as is. For options that are not recognized by
6572 @var{GNAT COMPILE} as linker options, the @var{GNAT COMPILE} qualifiers @code{-Xlinker} or
6573 @code{-Wl,} shall be used. Refer to the GCC documentation for
6574 details. Here is an example showing how to generate a linker map
6575 assuming that the underlying linker is GNU ld:
6577 @smallexample
6578 $ GNAT LINK my_prog -Wl,-Map,MAPFILE
6579 @end smallexample
6581 Using @var{linker options} it is possible to set the program stack and
6582 heap size. See @pxref{Setting Stack Size from GNAT LINK} and
6583 @pxref{Setting Heap Size from GNAT LINK}.
6585 @code{GNAT LINK} determines the list of objects required by the Ada
6586 program and prepends them to the list of objects passed to the linker.
6587 @code{GNAT LINK} also gathers any arguments set by the use of
6588 @code{pragma Linker_Options} and adds them to the list of arguments
6589 presented to the linker.
6591 @code{GNAT LINK} accepts the following types of extra files on the command
6592 line: objects (.OBJ), libraries (.OLB), shareable images (.EXE), and
6593 options files (.OPT). These are recognized and handled according to their
6594 extension.
6596 @node Qualifiers for GNAT LINK
6597 @section Qualifiers for @code{GNAT LINK}
6599 @noindent
6600 The following qualifiers are available with the @code{GNAT LINK} utility:
6602 @table @code
6604 @item /BIND_FILE=ADA
6605 @cindex @code{/BIND_FILE=ADA} (@code{GNAT LINK})
6606 The binder has generated code in Ada. This is the default.
6608 @item /BIND_FILE=C
6609 @cindex @code{/BIND_FILE=C} (@code{GNAT LINK})
6610 If instead of generating a file in Ada, the binder has generated one in
6611 C, then the linker needs to know about it. Use this qualifier to signal
6612 to @code{GNAT LINK} that the binder has generated C code rather than
6613 Ada code.
6615 @item -f
6616 @cindex Command line length
6617 @cindex @code{-f} (@code{GNAT LINK})
6618 On some targets, the command line length is limited, and @code{GNAT LINK}
6619 will generate a separate file for the linker if the list of object files
6620 is too long. The @code{-f} flag forces this file to be generated even if
6621 the limit is not exceeded. This is useful in some cases to deal with
6622 special situations where the command line length is exceeded.
6624 @item /DEBUG
6625 @cindex Debugging information, including
6626 @cindex @code{/DEBUG} (@code{GNAT LINK})
6627 The option to include debugging information causes the Ada bind file (in
6628 other words, @file{B$@var{mainprog}.ADB}) to be compiled with
6629 @code{/DEBUG}.
6630 In addition, the binder does not delete the @file{B$@var{mainprog}.ADB},
6631 @file{B$@var{mainprog}.OBJ} and @file{B$@var{mainprog}.ALI} files.
6632 Without @code{/DEBUG}, the binder removes these files by
6633 default. The same procedure apply if a C bind file was generated using
6634 @code{/BIND_FILE=C} @code{GNAT BIND} option, in this case the filenames are
6635 @file{B_@var{mainprog}.C} and @file{B_@var{mainprog}.OBJ}.
6638 @item /VERBOSE
6639 @cindex @code{/VERBOSE} (@code{GNAT LINK})
6640 Causes additional information to be output, including a full list of the
6641 included object files. This qualifier option is most useful when you want
6642 to see what set of object files are being used in the link step.
6645 @item /EXECUTABLE=@var{exec-name}
6646 @cindex @code{/EXECUTABLE} (@code{GNAT LINK})
6647 @var{exec-name} specifies an alternate name for the generated
6648 executable program. If this qualifier is omitted, the executable has the same
6649 name as the main unit. For example, @code{GNAT LINK TRY.ALI} creates
6650 an executable called @file{TRY.EXE}.
6653 @item /DEBUG=TRACEBACK
6654 @cindex @code{/DEBUG=TRACEBACK} (@code{GNAT LINK})
6655 This qualifier causes sufficient information to be included in the
6656 executable file to allow a traceback, but does not include the full
6657 symbol information needed by the debugger.
6659 @item /IDENTIFICATION="<string>"
6660 "<string>" specifies the string to be stored in the image file identification
6661 field in the image header. It overrides any pragma Ident specified string.
6663 @item /NOINHIBIT-EXEC
6664 Generate the executable file even if there are linker warnings.
6666 @item /NOSTART_FILES
6667 Don't link in the object file containing the "main" transfer address.
6668 Used when linking with a foreign language main program compiled with a
6669 Digital compiler.
6671 @item /STATIC
6672 Prefer linking with object libraries over shareable images, even without
6673 /DEBUG.
6675 @end table
6677 @node Setting Stack Size from GNAT LINK
6678 @section Setting Stack Size from @code{GNAT LINK}
6680 @noindent
6681 It is possible to specify the program stack size from @code{GNAT LINK}.
6682 Assuming that the underlying linker is GNU ld there is two ways to do so:
6684 @itemize @bullet
6686 @item using @code{-Xlinker} linker option
6688 @smallexample
6689 $ GNAT LINK hello -Xlinker --stack=0x10000,0x1000
6690 @end smallexample
6692 This set the stack reserve size to 0x10000 bytes and the stack commit
6693 size to 0x1000 bytes.
6695 @item using @code{-Wl} linker option
6697 @smallexample
6698 $ GNAT LINK hello -Wl,--stack=0x1000000
6699 @end smallexample
6701 This set the stack reserve size to 0x1000000 bytes. Note that with
6702 @code{-Wl} option it is not possible to set the stack commit size
6703 because the coma is a separator for this option.
6705 @end itemize
6707 @node Setting Heap Size from GNAT LINK
6708 @section Setting Heap Size from @code{GNAT LINK}
6710 @noindent
6711 It is possible to specify the program heap size from @code{GNAT LINK}.
6712 Assuming that the underlying linker is GNU ld there is two ways to do so:
6714 @itemize @bullet
6716 @item using @code{-Xlinker} linker option
6718 @smallexample
6719 $ GNAT LINK hello -Xlinker --heap=0x10000,0x1000
6720 @end smallexample
6722 This set the heap reserve size to 0x10000 bytes and the heap commit
6723 size to 0x1000 bytes.
6725 @item using @code{-Wl} linker option
6727 @smallexample
6728 $ GNAT LINK hello -Wl,--heap=0x1000000
6729 @end smallexample
6731 This set the heap reserve size to 0x1000000 bytes. Note that with
6732 @code{-Wl} option it is not possible to set the heap commit size
6733 because the coma is a separator for this option.
6735 @end itemize
6737 @node The GNAT Make Program GNAT MAKE
6738 @chapter The GNAT Make Program @code{GNAT MAKE}
6739 @findex GNAT MAKE
6741 @menu
6742 * Running GNAT MAKE::
6743 * Qualifiers for GNAT MAKE::
6744 * Mode Qualifiers for GNAT MAKE::
6745 * Notes on the Command Line::
6746 * How GNAT MAKE Works::
6747 * Examples of GNAT MAKE Usage::
6748 @end menu
6749 @noindent
6750 A typical development cycle when working on an Ada program consists of
6751 the following steps:
6753 @enumerate
6754 @item
6755 Edit some sources to fix bugs.
6757 @item
6758 Add enhancements.
6760 @item
6761 Compile all sources affected.
6763 @item
6764 Rebind and relink.
6766 @item
6767 Test.
6768 @end enumerate
6770 @noindent
6771 The third step can be tricky, because not only do the modified files
6772 @cindex Dependency rules
6773 have to be compiled, but any files depending on these files must also be
6774 recompiled. The dependency rules in Ada can be quite complex, especially
6775 in the presence of overloading, @code{use} clauses, generics and inlined
6776 subprograms.
6778 @code{GNAT MAKE} automatically takes care of the third and fourth steps
6779 of this process. It determines which sources need to be compiled,
6780 compiles them, and binds and links the resulting object files.
6782 Unlike some other Ada make programs, the dependencies are always
6783 accurately recomputed from the new sources. The source based approach of
6784 the GNAT compilation model makes this possible. This means that if
6785 changes to the source program cause corresponding changes in
6786 dependencies, they will always be tracked exactly correctly by
6787 @code{GNAT MAKE}.
6789 @node Running GNAT MAKE
6790 @section Running @code{GNAT MAKE}
6792 @noindent
6793 The usual form of the @code{GNAT MAKE} command is
6795 @smallexample
6796 $ GNAT MAKE [@var{qualifiers}] @var{file_name} [@var{file_names}] [@var{mode_qualifiers}]
6797 @end smallexample
6799 @noindent
6800 The only required argument is one @var{file_name}, which specifies
6801 a compilation unit that is a main program. Several @var{file_names} can be
6802 specified: this will result in several executables being built.
6803 If @code{qualifiers} are present, they can be placed before the first
6804 @var{file_name}, between @var{file_names} or after the last @var{file_name}.
6805 If @var{mode_qualifiers} are present, they must always be placed after
6806 the last @var{file_name} and all @code{qualifiers}.
6808 If you are using standard file extensions (.ADB and .ADS), then the
6809 extension may be omitted from the @var{file_name} arguments. However, if
6810 you are using non-standard extensions, then it is required that the
6811 extension be given. A relative or absolute directory path can be
6812 specified in a @var{file_name}, in which case, the input source file will
6813 be searched for in the specified directory only. Otherwise, the input
6814 source file will first be searched in the directory where
6815 @code{GNAT MAKE} was invoked and if it is not found, it will be search on
6816 the source path of the compiler as described in
6817 @ref{Search Paths and the Run-Time Library (RTL)}.
6819 When several @var{file_names} are specified, if an executable needs to be
6820 rebuilt and relinked, all subsequent executables will be rebuilt and
6821 relinked, even if this would not be absolutely necessary.
6823 All @code{GNAT MAKE} output (except when you specify
6824 @code{/DEPENDENCIES_LIST}) is to
6825 @file{SYS$ERROR}. The output produced by the
6826 @code{/DEPENDENCIES_LIST} qualifier is send to
6827 @file{SYS$OUTPUT}.
6829 @node Qualifiers for GNAT MAKE
6830 @section Qualifiers for @code{GNAT MAKE}
6832 @noindent
6833 You may specify any of the following qualifiers to @code{GNAT MAKE}:
6835 @table @code
6837 @item /ALL_FILES
6838 @cindex @code{/ALL_FILES} (@code{GNAT MAKE})
6839 Consider all files in the make process, even the GNAT internal system
6840 files (for example, the predefined Ada library files), as well as any
6841 locked files. Locked files are files whose ALI file is write-protected.
6842 By default,
6843 @code{GNAT MAKE} does not check these files,
6844 because the assumption is that the GNAT internal files are properly up
6845 to date, and also that any write protected ALI files have been properly
6846 installed. Note that if there is an installation problem, such that one
6847 of these files is not up to date, it will be properly caught by the
6848 binder.
6849 You may have to specify this qualifier if you are working on GNAT
6850 itself. @code{/ALL_FILES} is also useful in conjunction with
6851 @code{/FORCE_COMPILE}
6852 if you need to recompile an entire application,
6853 including run-time files, using special configuration pragma settings,
6854 such as a non-standard @code{Float_Representation} pragma.
6855 By default
6856 @code{GNAT MAKE /ALL_FILES} compiles all GNAT
6857 internal files with
6858 the @code{/CHECKS=SUPPRESS_ALL /STYLE_CHECKS=GNAT} qualifier.
6860 @item /ACTIONS=BIND
6861 @cindex @code{/ACTIONS=BIND} (@code{GNAT MAKE})
6862 Bind only. Can be combined with @code{/ACTIONS=COMPILE} to do compilation
6863 and binding, but no link. Can be combined with @code{/ACTIONS=LINK}
6864 to do binding and linking. When not combined with @code{/ACTIONS=COMPILE}
6865 all the units in the closure of the main program must have been previously
6866 compiled and must be up to date. The root unit specified by @var{file_name}
6867 may be given without extension, with the source extension or, if no GNAT
6868 Project File is specified, with the ALI file extension.
6870 @item /ACTIONS=COMPILE
6871 @cindex @code{/ACTIONS=COMPILE} (@code{GNAT MAKE})
6872 Compile only. Do not perform binding, except when @code{/ACTIONS=BIND}
6873 is also specified. Do not perform linking, except if both
6874 @code{/ACTIONS=BIND} and
6875  @code{/ACTIONS=LINK} are also specified.
6876 If the root unit specified by @var{file_name} is not a main unit, this is the
6877 default. Otherwise @code{GNAT MAKE} will attempt binding and linking
6878 unless all objects are up to date and the executable is more recent than
6879 the objects.
6881 @item /MAPPING
6882 @cindex @code{/MAPPING} (@code{GNAT MAKE})
6883 Use a mapping file. A mapping file is a way to communicate to the compiler
6884 two mappings: from unit names to file names (without any directory information)
6885 and from file names to path names (with full directory information).
6886 These mappings are used by the compiler to short-circuit the path search.
6887 When @code{GNAT MAKE} is invoked with this qualifier, it will create a mapping
6888 file, initially populated by the project manager, if @code{-P} is used,
6889 otherwise initially empty. Each invocation of the compiler will add the newly
6890 accessed sources to the mapping file. This will improve the source search
6891 during the next invocation of the compiler.
6893 @item /FORCE_COMPILE
6894 @cindex @code{/FORCE_COMPILE} (@code{GNAT MAKE})
6895 Force recompilations. Recompile all sources, even though some object
6896 files may be up to date, but don't recompile predefined or GNAT internal
6897 files or locked files (files with a write-protected ALI file),
6898 unless the @code{/ALL_FILES} qualifier is also specified.
6900 @item
6901 @item /IN_PLACE
6902 @cindex @code{/IN_PLACE} (@code{GNAT MAKE})
6903 In normal mode, @code{GNAT MAKE} compiles all object files and ALI files
6904 into the current directory. If the @code{/IN_PLACE} qualifier is used,
6905 then instead object files and ALI files that already exist are overwritten
6906 in place. This means that once a large project is organized into separate
6907 directories in the desired manner, then @code{GNAT MAKE} will automatically
6908 maintain and update this organization. If no ALI files are found on the
6909 Ada object path (@ref{Search Paths and the Run-Time Library (RTL)}),
6910 the new object and ALI files are created in the
6911 directory containing the source being compiled. If another organization
6912 is desired, where objects and sources are kept in different directories,
6913 a useful technique is to create dummy ALI files in the desired directories.
6914 When detecting such a dummy file, @code{GNAT MAKE} will be forced to recompile
6915 the corresponding source file, and it will be put the resulting object
6916 and ALI files in the directory where it found the dummy file.
6918 @item /PROCESSES=@var{n}
6919 @cindex @code{/PROCESSES} (@code{GNAT MAKE})
6920 @cindex Parallel make
6921 Use @var{n} processes to carry out the (re)compilations. On a
6922 multiprocessor machine compilations will occur in parallel. In the
6923 event of compilation errors, messages from various compilations might
6924 get interspersed (but @code{GNAT MAKE} will give you the full ordered
6925 list of failing compiles at the end). If this is problematic, rerun
6926 the make process with n set to 1 to get a clean list of messages.
6928 @item /CONTINUE_ON_ERROR
6929 @cindex @code{/CONTINUE_ON_ERROR} (@code{GNAT MAKE})
6930 Keep going. Continue as much as possible after a compilation error. To
6931 ease the programmer's task in case of compilation errors, the list of
6932 sources for which the compile fails is given when @code{GNAT MAKE}
6933 terminates.
6935 If @code{GNAT MAKE} is invoked with several @file{file_names} and with this
6936 qualifier, if there are compilation errors when building an executable,
6937 @code{GNAT MAKE} will not attempt to build the following executables.
6939 @item /ACTIONS=LINK
6940 @cindex @code{/ACTIONS=LINK} (@code{GNAT MAKE})
6941 Link only. Can be combined with @code{/ACTIONS=BIND} to binding
6942 and linking. Linking will not be performed if combined with
6943 @code{/ACTIONS=COMPILE}
6944 but not with @code{/ACTIONS=BIND}.
6945 When not combined with @code{/ACTIONS=BIND}
6946 all the units in the closure of the main program must have been previously
6947 compiled and must be up to date, and the main program need to have been bound.
6948 The root unit specified by @var{file_name}
6949 may be given without extension, with the source extension or, if no GNAT
6950 Project File is specified, with the ALI file extension.
6952 @item /MINIMAL_RECOMPILATION
6953 @cindex @code{/MINIMAL_RECOMPILATION} (@code{GNAT MAKE})
6954 Specifies that the minimum necessary amount of recompilations
6955 be performed. In this mode @code{GNAT MAKE} ignores time
6956 stamp differences when the only
6957 modifications to a source file consist in adding/removing comments,
6958 empty lines, spaces or tabs. This means that if you have changed the
6959 comments in a source file or have simply reformatted it, using this
6960 qualifier will tell GNAT MAKE not to recompile files that depend on it
6961 (provided other sources on which these files depend have undergone no
6962 semantic modifications). Note that the debugging information may be
6963 out of date with respect to the sources if the @code{-m} qualifier causes
6964 a compilation to be switched, so the use of this qualifier represents a
6965 trade-off between compilation time and accurate debugging information.
6967 @item /DEPENDENCIES_LIST
6968 @cindex Dependencies, producing list
6969 @cindex @code{/DEPENDENCIES_LIST} (@code{GNAT MAKE})
6970 Check if all objects are up to date. If they are, output the object
6971 dependences to @file{SYS$OUTPUT} in a form that can be directly exploited in
6972 a @file{Makefile}. By default, each source file is prefixed with its
6973 (relative or absolute) directory name. This name is whatever you
6974 specified in the various @code{/SOURCE_SEARCH}
6975 and @code{/SEARCH} qualifiers. If you use
6976 @code{GNAT MAKE /DEPENDENCIES_LIST}
6977 @code{/QUIET}
6978 (see below), only the source file names,
6979 without relative paths, are output. If you just specify the
6980 @code{/DEPENDENCIES_LIST}
6981 qualifier, dependencies of the GNAT internal system files are omitted. This
6982 is typically what you want. If you also specify
6983 the @code{/ALL_FILES} qualifier,
6984 dependencies of the GNAT internal files are also listed. Note that
6985 dependencies of the objects in external Ada libraries (see qualifier
6986 @code{/SKIP_MISSING=}@var{dir} in the following list) are never reported.
6988 @item /DO_OBJECT_CHECK
6989 @cindex @code{/DO_OBJECT_CHECK} (@code{GNAT MAKE})
6990 Don't compile, bind, or link. Checks if all objects are up to date.
6991 If they are not, the full name of the first file that needs to be
6992 recompiled is printed.
6993 Repeated use of this option, followed by compiling the indicated source
6994 file, will eventually result in recompiling all required units.
6996 @item /EXECUTABLE=@var{exec_name}
6997 @cindex @code{/EXECUTABLE} (@code{GNAT MAKE})
6998 Output executable name. The name of the final executable program will be
6999 @var{exec_name}. If the @code{/EXECUTABLE} qualifier is omitted the default
7000 name for the executable will be the name of the input file in appropriate form
7001 for an executable file on the host system.
7003 This qualifier cannot be used when invoking @code{GNAT MAKE} with several
7004 @file{file_names}.
7006 @item /QUIET
7007 @cindex @code{/QUIET} (@code{GNAT MAKE})
7008 Quiet. When this flag is not set, the commands carried out by
7009 @code{GNAT MAKE} are displayed.
7011 @item /SWITCH_CHECK/
7012 @cindex @code{/SWITCH_CHECK} (@code{GNAT MAKE})
7013 Recompile if compiler qualifiers have changed since last compilation.
7014 All compiler qualifiers but -I and -o are taken into account in the
7015 following way:
7016 orders between different ``first letter'' qualifiers are ignored, but
7017 orders between same qualifiers are taken into account. For example,
7018 @code{-O /OPTIMIZE=ALL} is different than @code{/OPTIMIZE=ALL -O}, but @code{-g -O} is equivalent
7019 to @code{-O -g}.
7021 @item /UNIQUE
7022 @cindex @code{/UNIQUE} (@code{GNAT MAKE})
7023 Unique. Recompile at most the main file. It implies -c. Combined with
7024 -f, it is equivalent to calling the compiler directly.
7026 @item /REASONS
7027 @cindex @code{/REASONS} (@code{GNAT MAKE})
7028 Verbose. Displays the reason for all recompilations @code{GNAT MAKE}
7029 decides are necessary.
7031 @item /NOMAIN
7032 @cindex @code{/NOMAIN} (@code{GNAT MAKE})
7033 No main subprogram. Bind and link the program even if the unit name
7034 given on the command line is a package name. The resulting executable
7035 will execute the elaboration routines of the package and its closure,
7036 then the finalization routines.
7038 @item @code{GNAT COMPILE} @asis{qualifiers}
7039 Any qualifier that cannot be recognized as a qualifier for @code{GNAT MAKE}
7040 but is recognizable as a valid qualifier for @code{GNAT COMPILE} is
7041 automatically treated as a compiler qualifier, and passed on to all
7042 compilations that are carried out.
7043 @end table
7045 @noindent
7046 Source and library search path qualifiers:
7048 @table @code
7049 @item /SOURCE_SEARCH=@var{dir}
7050 @cindex @code{/SOURCE_SEARCH} (@code{GNAT MAKE})
7051 When looking for source files also look in directory @var{dir}.
7052 The order in which source files search is undertaken is
7053 described in @ref{Search Paths and the Run-Time Library (RTL)}.
7055 @item /SKIP_MISSING=@var{dir}
7056 @cindex @code{/SKIP_MISSING} (@code{GNAT MAKE})
7057 Consider @var{dir} as being an externally provided Ada library.
7058 Instructs @code{GNAT MAKE} to skip compilation units whose @file{.ALI}
7059 files have been located in directory @var{dir}. This allows you to have
7060 missing bodies for the units in @var{dir} and to ignore out of date bodies
7061 for the same units. You still need to specify
7062 the location of the specs for these units by using the qualifiers
7063 @code{/SOURCE_SEARCH=@var{dir}}
7064 or @code{/SEARCH=@var{dir}}.
7065 Note: this qualifier is provided for compatibility with previous versions
7066 of @code{GNAT MAKE}. The easier method of causing standard libraries
7067 to be excluded from consideration is to write-protect the corresponding
7068 ALI files.
7070 @item /OBJECT_SEARCH=@var{dir}
7071 @cindex @code{/OBJECT_SEARCH} (@code{GNAT MAKE})
7072 When searching for library and object files, look in directory
7073 @var{dir}. The order in which library files are searched is described in
7074 @ref{Search Paths for GNAT BIND}.
7076 @item /CONDITIONAL_SOURCE_SEARCH=@var{dir}
7077 @cindex Search paths, for @code{GNAT MAKE}
7078 @cindex @code{/CONDITIONAL_SOURCE_SEARCH} (@code{GNAT MAKE})
7079 Equivalent to @code{/SKIP_MISSING=@var{dir}
7080 /SOURCE_SEARCH=@var{dir}}.
7082 @item /SEARCH=@var{dir}
7083 @cindex @code{/SEARCH} (@code{GNAT MAKE})
7084 Equivalent to @code{/OBJECT_SEARCH=@var{dir}
7085 /SOURCE_SEARCH=@var{dir}}.
7087 @item /NOCURRENT_DIRECTORY
7088 @cindex @code{/NOCURRENT_DIRECTORY} (@code{GNAT MAKE})
7089 @cindex Source files, suppressing search
7090 Do not look for source files in the directory containing the source
7091 file named in the command line.
7092 Do not look for ALI or object files in the directory
7093 where @code{GNAT MAKE} was invoked.
7095 @item /LIBRARY_SEARCH=@var{dir}
7096 @cindex @code{/LIBRARY_SEARCH} (@code{GNAT MAKE})
7097 @cindex Linker libraries
7098 Add directory @var{dir} to the list of directories in which the linker
7099 will search for libraries. This is equivalent to
7100 @code{/LINKER_QUALIFIERS /LIBRARY_SEARCH=}@var{dir}.
7102 @item /NOSTD_INCLUDES
7103 @cindex @code{/NOSTD_INCLUDES} (@code{GNAT MAKE})
7104 Do not look for source files in the system default directory.
7106 @item /NOSTD_LIBRARIES
7107 @cindex @code{/NOSTD_LIBRARIES} (@code{GNAT MAKE})
7108 Do not look for library files in the system default directory.
7110 @item /RUNTIME_SYSTEM=@var{rts-path}
7111 @cindex @code{/RUNTIME_SYSTEM} (@code{GNAT MAKE})
7112 Specifies the default location of the runtime library. We look for the runtime
7113 in the following directories, and stop as soon as a valid runtime is found
7114 ("adainclude" or "ada_source_path", and "adalib" or "ada_object_path" present):
7116 @itemize @bullet
7117 @item <current directory>/$rts_path
7119 @item <default-search-dir>/$rts_path
7121 @item <default-search-dir>/rts-$rts_path
7122 @end itemize
7124 @noindent
7125 The selected path is handled like a normal RTS path.
7127 @end table
7129 @node Mode Qualifiers for GNAT MAKE
7130 @section Mode Qualifiers for @code{GNAT MAKE}
7132 @noindent
7133 The mode qualifiers (referred to as @code{mode_qualifiers}) allow the
7134 inclusion of qualifiers that are to be passed to the compiler itself, the
7135 binder or the linker. The effect of a mode qualifier is to cause all
7136 subsequent qualifiers up to the end of the qualifier list, or up to the next
7137 mode qualifier, to be interpreted as qualifiers to be passed on to the
7138 designated component of GNAT.
7140 @table @code
7141 @item /COMPILER_QUALIFIERS @var{qualifiers}
7142 @cindex @code{/COMPILER_QUALIFIERS} (@code{GNAT MAKE})
7143 Compiler qualifiers. Here @var{qualifiers} is a list of qualifiers
7144 that are valid qualifiers for @code{GNAT COMPILE}. They will be passed on to
7145 all compile steps performed by @code{GNAT MAKE}.
7147 @item /BINDER_QUALIFIERS @var{qualifiers}
7148 @cindex @code{/BINDER_QUALIFIERS} (@code{GNAT MAKE})
7149 Binder qualifiers. Here @var{qualifiers} is a list of qualifiers
7150 that are valid qualifiers for @code{GNAT COMPILE}. They will be passed on to
7151 all bind steps performed by @code{GNAT MAKE}.
7153 @item /LINKER_QUALIFIERS @var{qualifiers}
7154 @cindex @code{/LINKER_QUALIFIERS} (@code{GNAT MAKE})
7155 Linker qualifiers. Here @var{qualifiers} is a list of qualifiers
7156 that are valid qualifiers for @code{GNAT COMPILE}. They will be passed on to
7157 all link steps performed by @code{GNAT MAKE}.
7158 @end table
7160 @node Notes on the Command Line
7161 @section Notes on the Command Line
7163 @noindent
7164 This section contains some additional useful notes on the operation
7165 of the @code{GNAT MAKE} command.
7167 @itemize @bullet
7168 @item
7169 @cindex Recompilation, by @code{GNAT MAKE}
7170 If @code{GNAT MAKE} finds no ALI files, it recompiles the main program
7171 and all other units required by the main program.
7172 This means that @code{GNAT MAKE}
7173 can be used for the initial compile, as well as during subsequent steps of
7174 the development cycle.
7176 @item
7177 If you enter @code{GNAT MAKE @var{file}.ADB}, where @file{@var{file}.ADB}
7178 is a subunit or body of a generic unit, @code{GNAT MAKE} recompiles
7179 @file{@var{file}.ADB} (because it finds no ALI) and stops, issuing a
7180 warning.
7182 @item
7183 In @code{GNAT MAKE} the qualifier @code{/SEARCH}
7184 is used to specify both source and
7185 library file paths. Use @code{/SOURCE_SEARCH}
7186 instead if you just want to specify
7187 source paths only and @code{/OBJECT_SEARCH}
7188 if you want to specify library paths
7189 only.
7191 @item
7192 @code{GNAT MAKE} examines both an ALI file and its corresponding object file
7193 for consistency. If an ALI is more recent than its corresponding object,
7194 or if the object file is missing, the corresponding source will be recompiled.
7195 Note that @code{GNAT MAKE} expects an ALI and the corresponding object file
7196 to be in the same directory.
7198 @item
7199 @code{GNAT MAKE} will ignore any files whose ALI file is write-protected.
7200 This may conveniently be used to exclude standard libraries from
7201 consideration and in particular it means that the use of the
7202 @code{/FORCE_COMPILE} qualifier will not recompile these files
7203 unless @code{/ALL_FILES} is also specified.
7205 @item
7206 @code{GNAT MAKE} has been designed to make the use of Ada libraries
7207 particularly convenient. Assume you have an Ada library organized
7208 as follows: @var{[OBJ_DIR]} contains the objects and ALI files for
7209 of your Ada compilation units,
7210 whereas @var{[INCLUDE_DIR]} contains the
7211 specs of these units, but no bodies. Then to compile a unit
7212 stored in @code{MAIN.ADB}, which uses this Ada library you would just type
7214 @smallexample
7215 $ GNAT MAKE /SOURCE_SEARCH=@var{[INCLUDE_DIR]}
7216            /SKIP_MISSING=@var{[OBJ_DIR]} main
7217 @end smallexample
7219 @item
7220 Using @code{GNAT MAKE} along with the
7221 @code{/MINIMAL_RECOMPILATION}
7222 qualifier provides a mechanism for avoiding unnecessary rcompilations. Using
7223 this qualifier,
7224 you can update the comments/format of your
7225 source files without having to recompile everything. Note, however, that
7226 adding or deleting lines in a source files may render its debugging
7227 info obsolete. If the file in question is a spec, the impact is rather
7228 limited, as that debugging info will only be useful during the
7229 elaboration phase of your program. For bodies the impact can be more
7230 significant. In all events, your debugger will warn you if a source file
7231 is more recent than the corresponding object, and alert you to the fact
7232 that the debugging information may be out of date.
7233 @end itemize
7235 @node How GNAT MAKE Works
7236 @section How @code{GNAT MAKE} Works
7238 @noindent
7239 Generally @code{GNAT MAKE} automatically performs all necessary
7240 recompilations and you don't need to worry about how it works. However,
7241 it may be useful to have some basic understanding of the @code{GNAT MAKE}
7242 approach and in particular to understand how it uses the results of
7243 previous compilations without incorrectly depending on them.
7245 First a definition: an object file is considered @dfn{up to date} if the
7246 corresponding ALI file exists and its time stamp predates that of the
7247 object file and if all the source files listed in the
7248 dependency section of this ALI file have time stamps matching those in
7249 the ALI file. This means that neither the source file itself nor any
7250 files that it depends on have been modified, and hence there is no need
7251 to recompile this file.
7253 @code{GNAT MAKE} works by first checking if the specified main unit is up
7254 to date. If so, no compilations are required for the main unit. If not,
7255 @code{GNAT MAKE} compiles the main program to build a new ALI file that
7256 reflects the latest sources. Then the ALI file of the main unit is
7257 examined to find all the source files on which the main program depends,
7258 and @code{GNAT MAKE} recursively applies the above procedure on all these files.
7260 This process ensures that @code{GNAT MAKE} only trusts the dependencies
7261 in an existing ALI file if they are known to be correct. Otherwise it
7262 always recompiles to determine a new, guaranteed accurate set of
7263 dependencies. As a result the program is compiled "upside down" from what may
7264 be more familiar as the required order of compilation in some other Ada
7265 systems. In particular, clients are compiled before the units on which
7266 they depend. The ability of GNAT to compile in any order is critical in
7267 allowing an order of compilation to be chosen that guarantees that
7268 @code{GNAT MAKE} will recompute a correct set of new dependencies if
7269 necessary.
7271 When invoking @code{GNAT MAKE} with several @var{file_names}, if a unit is
7272 imported by several of the executables, it will be recompiled at most once.
7274 @node Examples of GNAT MAKE Usage
7275 @section Examples of @code{GNAT MAKE} Usage
7277 @table @code
7278 @item GNAT MAKE HELLO.ADB
7279 Compile all files necessary to bind and link the main program
7280 @file{HELLO.ADB} (containing unit @code{Hello}) and bind and link the
7281 resulting object files to generate an executable file @file{HELLO.EXE}.
7283 @item GNAT MAKE main1 main2 main3
7284 Compile all files necessary to bind and link the main programs
7285 @file{MAIN1.ADB} (containing unit @code{Main1}), @file{MAIN2.ADB}
7286 (containing unit @code{Main2}) and @file{MAIN3.ADB}
7287 (containing unit @code{Main3}) and bind and link the resulting object files
7288 to generate three executable files @file{MAIN1.EXE},
7289 @file{MAIN2.EXE}
7290 and @file{MAIN3.EXE}.
7293 @item GNAT MAKE Main_Unit /QUIET /COMPILER_QUALIFIERS /OPTIMIZE=ALL /BINDER_QUALIFIERS /ORDER_OF_ELABORATION
7294 Compile all files necessary to bind and link the main program unit
7295 @code{Main_Unit} (from file @file{MAIN_UNIT.ADB}). All compilations will
7296 be done with optimization level 2 and the order of elaboration will be
7297 listed by the binder. @code{GNAT MAKE} will operate in quiet mode, not
7298 displaying commands it is executing.
7299 @end table
7301 @node Renaming Files Using GNAT CHOP
7302 @chapter Renaming Files Using @code{GNAT CHOP}
7303 @findex GNAT CHOP
7305 @noindent
7306 This chapter discusses how to handle files with multiple units by using
7307 the @code{GNAT CHOP} utility. This utility is also useful in renaming
7308 files to meet the standard GNAT default file naming conventions.
7310 @menu
7311 * Handling Files with Multiple Units::
7312 * Operating GNAT CHOP in Compilation Mode::
7313 * Command Line for GNAT CHOP::
7314 * Qualifiers for GNAT CHOP::
7315 * Examples of GNAT CHOP Usage::
7316 @end menu
7318 @node Handling Files with Multiple Units
7319 @section Handling Files with Multiple Units
7321 @noindent
7322 The basic compilation model of GNAT requires that a file submitted to the
7323 compiler have only one unit and there be a strict correspondence
7324 between the file name and the unit name.
7326 The @code{GNAT CHOP} utility allows both of these rules to be relaxed,
7327 allowing GNAT to process files which contain multiple compilation units
7328 and files with arbitrary file names. @code{GNAT CHOP}
7329 reads the specified file and generates one or more output files,
7330 containing one unit per file. The unit and the file name correspond,
7331 as required by GNAT.
7333 If you want to permanently restructure a set of "foreign" files so that
7334 they match the GNAT rules, and do the remaining development using the
7335 GNAT structure, you can simply use @code{GNAT CHOP} once, generate the
7336 new set of files and work with them from that point on.
7338 Alternatively, if you want to keep your files in the "foreign" format,
7339 perhaps to maintain compatibility with some other Ada compilation
7340 system, you can set up a procedure where you use @code{GNAT CHOP} each
7341 time you compile, regarding the source files that it writes as temporary
7342 files that you throw away.
7344 @node Operating GNAT CHOP in Compilation Mode
7345 @section Operating GNAT CHOP in Compilation Mode
7347 @noindent
7348 The basic function of @code{GNAT CHOP} is to take a file with multiple units
7349 and split it into separate files. The boundary between files is reasonably
7350 clear, except for the issue of comments and pragmas. In default mode, the
7351 rule is that any pragmas between units belong to the previous unit, except
7352 that configuration pragmas always belong to the following unit. Any comments
7353 belong to the following unit. These rules
7354 almost always result in the right choice of
7355 the split point without needing to mark it explicitly and most users will
7356 find this default to be what they want. In this default mode it is incorrect to
7357 submit a file containing only configuration pragmas, or one that ends in
7358 configuration pragmas, to @code{GNAT CHOP}.
7360 However, using a special option to activate "compilation mode",
7361 @code{GNAT CHOP}
7362 can perform another function, which is to provide exactly the semantics
7363 required by the RM for handling of configuration pragmas in a compilation.
7364 In the absence of configuration pragmas (at the main file level), this
7365 option has no effect, but it causes such configuration pragmas to be handled
7366 in a quite different manner.
7368 First, in compilation mode, if @code{GNAT CHOP} is given a file that consists of
7369 only configuration pragmas, then this file is appended to the
7370 @file{GNAT.ADC} file in the current directory. This behavior provides
7371 the required behavior described in the RM for the actions to be taken
7372 on submitting such a file to the compiler, namely that these pragmas
7373 should apply to all subsequent compilations in the same compilation
7374 environment. Using GNAT, the current directory, possibly containing a
7375 @file{GNAT.ADC} file is the representation
7376 of a compilation environment. For more information on the
7377 @file{GNAT.ADC} file, see the section on handling of configuration
7378 pragmas @pxref{Handling of Configuration Pragmas}.
7380 Second, in compilation mode, if @code{GNAT CHOP}
7381 is given a file that starts with
7382 configuration pragmas, and contains one or more units, then these
7383 configuration pragmas are prepended to each of the chopped files. This
7384 behavior provides the required behavior described in the RM for the
7385 actions to be taken on compiling such a file, namely that the pragmas
7386 apply to all units in the compilation, but not to subsequently compiled
7387 units.
7389 Finally, if configuration pragmas appear between units, they are appended
7390 to the previous unit. This results in the previous unit being illegal,
7391 since the compiler does not accept configuration pragmas that follow
7392 a unit. This provides the required RM behavior that forbids configuration
7393 pragmas other than those preceding the first compilation unit of a
7394 compilation.
7396 For most purposes, @code{GNAT CHOP} will be used in default mode. The
7397 compilation mode described above is used only if you need exactly
7398 accurate behavior with respect to compilations, and you have files
7399 that contain multiple units and configuration pragmas. In this
7400 circumstance the use of @code{GNAT CHOP} with the compilation mode
7401 qualifier provides the required behavior, and is for example the mode
7402 in which GNAT processes the ACVC tests.
7404 @node Command Line for GNAT CHOP
7405 @section Command Line for @code{GNAT CHOP}
7407 @noindent
7408 The @code{GNAT CHOP} command has the form:
7410 @smallexample
7411 $ GNAT CHOP qualifiers @var{file name} [@var{file name} @var{file name} ...]
7412       [@var{directory}]
7413 @end smallexample
7415 @noindent
7416 The only required argument is the file name of the file to be chopped.
7417 There are no restrictions on the form of this file name. The file itself
7418 contains one or more Ada units, in normal GNAT format, concatenated
7419 together. As shown, more than one file may be presented to be chopped.
7421 When run in default mode, @code{GNAT CHOP} generates one output file in
7422 the current directory for each unit in each of the files.
7424 @var{directory}, if specified, gives the name of the directory to which
7425 the output files will be written. If it is not specified, all files are
7426 written to the current directory.
7428 For example, given a
7429 file called @file{hellofiles} containing
7431 @smallexample
7432 @group
7433 @cartouche
7434 @b{procedure} hello;
7436 @b{with} Text_IO; @b{use} Text_IO;
7437 @b{procedure} hello @b{is}
7438 @b{begin}
7439    Put_Line ("Hello");
7440 @b{end} hello;
7441 @end cartouche
7442 @end group
7443 @end smallexample
7445 @noindent
7446 the command
7448 @smallexample
7449 $ GNAT CHOP HELLOFILES.
7450 @end smallexample
7452 @noindent
7453 generates two files in the current directory, one called
7454 @file{HELLO.ADS} containing the single line that is the procedure spec,
7455 and the other called @file{HELLO.ADB} containing the remaining text. The
7456 original file is not affected. The generated files can be compiled in
7457 the normal manner.
7459 @node Qualifiers for GNAT CHOP
7460 @section Qualifiers for @code{GNAT CHOP}
7462 @noindent
7463 @code{GNAT CHOP} recognizes the following qualifiers:
7465 @table @code
7467 @item /COMPILATION
7468 @cindex @code{/COMPILATION} (@code{GNAT CHOP})
7469 Causes @code{GNAT CHOP} to operate in compilation mode, in which
7470 configuration pragmas are handled according to strict RM rules. See
7471 previous section for a full description of this mode.
7474 @item /HELP
7475 Causes @code{GNAT CHOP} to generate a brief help summary to the standard
7476 output file showing usage information.
7478 @item /FILE_NAME_MAX_LENGTH=@var{mm}
7479 @cindex @code{/FILE_NAME_MAX_LENGTH} (@code{GNAT CHOP})
7480 Limit generated file names to the specified number @code{mm}
7481 of characters.
7482 This is useful if the
7483 resulting set of files is required to be interoperable with systems
7484 which limit the length of file names.
7485 If no value is given, or
7486 if no @code{/FILE_NAME_MAX_LENGTH} qualifier is given,
7487 a default of 39, suitable for OpenVMS Alpha
7488 Systems, is assumed
7490 @item /PRESERVE
7491 @cindex @code{/PRESERVE} (@code{GNAT CHOP})
7492 Causes the file creation time stamp of the input file to be
7493 preserved and used for the time stamp of the output file(s). This may be
7494 useful for preserving coherency of time stamps in an enviroment where
7495 @code{GNAT CHOP} is used as part of a standard build process.
7497 @item /QUIET
7498 @cindex @code{/QUIET} (@code{GNAT CHOP})
7499 Causes output of informational messages indicating the set of generated
7500 files to be suppressed. Warnings and error messages are unaffected.
7502 @item /REFERENCE
7503 @cindex @code{/REFERENCE} (@code{GNAT CHOP})
7504 @findex Source_Reference
7505 Generate @code{Source_Reference} pragmas. Use this qualifier if the output
7506 files are regarded as temporary and development is to be done in terms
7507 of the original unchopped file. This qualifier causes
7508 @code{Source_Reference} pragmas to be inserted into each of the
7509 generated files to refers back to the original file name and line number.
7510 The result is that all error messages refer back to the original
7511 unchopped file.
7512 In addition, the debugging information placed into the object file (when
7513 the @code{/DEBUG} qualifier of @code{GNAT COMPILE} or @code{GNAT MAKE} is specified) also
7514 refers back to this original file so that tools like profilers and
7515 debuggers will give information in terms of the original unchopped file.
7517 If the original file to be chopped itself contains
7518 a @code{Source_Reference}
7519 pragma referencing a third file, then GNAT CHOP respects
7520 this pragma, and the generated @code{Source_Reference} pragmas
7521 in the chopped file refer to the original file, with appropriate
7522 line numbers. This is particularly useful when @code{GNAT CHOP}
7523 is used in conjunction with @code{GNAT PREPROCESS} to compile files that
7524 contain preprocessing statements and multiple units.
7526 @item /VERBOSE
7527 @cindex @code{/VERBOSE} (@code{GNAT CHOP})
7528 Causes @code{GNAT CHOP} to operate in verbose mode. The version
7529 number and copyright notice are output, as well as exact copies of
7530 the GNAT1 commands spawned to obtain the chop control information.
7532 @item /OVERWRITE
7533 @cindex @code{/OVERWRITE} (@code{GNAT CHOP})
7534 Overwrite existing file names. Normally @code{GNAT CHOP} regards it as a
7535 fatal error if there is already a file with the same name as a
7536 file it would otherwise output, in other words if the files to be
7537 chopped contain duplicated units. This qualifier bypasses this
7538 check, and causes all but the last instance of such duplicated
7539 units to be skipped.
7541 @end table
7543 @node Examples of GNAT CHOP Usage
7544 @section Examples of @code{GNAT CHOP} Usage
7546 @table @code
7547 @item GNAT CHOP /OVERWRITE HELLO_S.ADA [ICHBIAH.FILES]
7549 Chops the source file @file{HELLO_S.ADA}. The output files will be
7550 placed in the directory @file{[ICHBIAH.FILES]},
7551 overwriting any
7552 files with matching names in that directory (no files in the current
7553 directory are modified).
7555 @item GNAT CHOP ARCHIVE.
7556 Chops the source file @file{ARCHIVE.}
7557 into the current directory. One
7558 useful application of @code{GNAT CHOP} is in sending sets of sources
7559 around, for example in email messages. The required sources are simply
7560 concatenated (for example, using a VMS @code{APPEND/NEW}
7561 command), and then
7562 @code{GNAT CHOP} is used at the other end to reconstitute the original
7563 file names.
7565 @item GNAT CHOP file1 file2 file3 direc
7566 Chops all units in files @file{file1}, @file{file2}, @file{file3}, placing
7567 the resulting files in the directory @file{direc}. Note that if any units
7568 occur more than once anywhere within this set of files, an error message
7569 is generated, and no files are written. To override this check, use the
7570 @code{/OVERWRITE} qualifier,
7571 in which case the last occurrence in the last file will
7572 be the one that is output, and earlier duplicate occurrences for a given
7573 unit will be skipped.
7574 @end table
7576 @node Configuration Pragmas
7577 @chapter Configuration Pragmas
7578 @cindex Configuration pragmas
7579 @cindex Pragmas, configuration
7581 @noindent
7582 In Ada 95, configuration pragmas include those pragmas described as
7583 such in the Ada 95 Reference Manual, as well as
7584 implementation-dependent pragmas that are configuration pragmas. See the
7585 individual descriptions of pragmas in the GNAT Reference Manual for
7586 details on these additional GNAT-specific configuration pragmas. Most
7587 notably, the pragma @code{Source_File_Name}, which allows
7588 specifying non-default names for source files, is a configuration
7589 pragma. The following is a complete list of configuration pragmas
7590 recognized by @code{GNAT}:
7592 @smallexample
7593    Ada_83
7594    Ada_95
7595    C_Pass_By_Copy
7596    Component_Alignment
7597    Discard_Names
7598    Elaboration_Checks
7599    Eliminate
7600    Extend_System
7601    Extensions_Allowed
7602    External_Name_Casing
7603    Float_Representation
7604    Initialize_Scalars
7605    License
7606    Locking_Policy
7607    Long_Float
7608    No_Run_Time
7609    Normalize_Scalars
7610    Polling
7611    Propagate_Exceptions
7612    Queuing_Policy
7613    Ravenscar
7614    Restricted_Run_Time
7615    Restrictions
7616    Reviewable
7617    Source_File_Name
7618    Style_Checks
7619    Suppress
7620    Task_Dispatching_Policy
7621    Unsuppress
7622    Use_VADS_Size
7623    Warnings
7624    Validity_Checks
7625 @end smallexample
7627 @menu
7628 * Handling of Configuration Pragmas::
7629 * The Configuration Pragmas Files::
7630 @end menu
7632 @node Handling of Configuration Pragmas
7633 @section Handling of Configuration Pragmas
7635 Configuration pragmas may either appear at the start of a compilation
7636 unit, in which case they apply only to that unit, or they may apply to
7637 all compilations performed in a given compilation environment.
7639 GNAT also provides the @code{GNAT CHOP} utility to provide an automatic
7640 way to handle configuration pragmas following the semantics for
7641 compilations (that is, files with multiple units), described in the RM.
7642 See section @pxref{Operating GNAT CHOP in Compilation Mode} for details.
7643 However, for most purposes, it will be more convenient to edit the
7644 @file{GNAT.ADC} file that contains configuration pragmas directly,
7645 as described in the following section.
7647 @node The Configuration Pragmas Files
7648 @section The Configuration Pragmas Files
7649 @cindex @file{GNAT.ADC}
7651 @noindent
7652 In GNAT a compilation environment is defined by the current
7653 directory at the time that a compile command is given. This current
7654 directory is searched for a file whose name is @file{GNAT.ADC}. If
7655 this file is present, it is expected to contain one or more
7656 configuration pragmas that will be applied to the current compilation.
7657 However, if the qualifier @option{-gnatA} is used, @file{GNAT.ADC} is not
7658 considered.
7660 Configuration pragmas may be entered into the @file{GNAT.ADC} file
7661 either by running @code{GNAT CHOP} on a source file that consists only of
7662 configuration pragmas, or more conveniently  by
7663 direct editing of the @file{GNAT.ADC} file, which is a standard format
7664 source file.
7666 In addition to @file{GNAT.ADC}, one additional file containing configuration
7667 pragmas may be applied to the current compilation using the qualifier
7668 @option{-gnatec}@var{path}. @var{path} must designate an existing file that
7669 contains only configuration pragmas. These configuration pragmas are
7670 in addition to those found in @file{GNAT.ADC} (provided @file{GNAT.ADC}
7671 is present and qualifier @option{-gnatA} is not used).
7673 It is allowed to specify several qualifiers @option{-gnatec}, however only
7674 the last one on the command line will be taken into account.
7676 Of special interest to GNAT OpenVMS Alpha is the following configuration pragma:
7678 @smallexample
7679 @cartouche
7680 @b{pragma} Extend_System (Aux_DEC);
7681 @end cartouche
7682 @end smallexample
7684 @noindent
7685 In the presence of this pragma, GNAT adds to the definition of the
7686 predefined package SYSTEM all the additional types and subprograms that are
7687 defined in DEC Ada. See @pxref{Compatibility with DEC Ada} for details.
7689 @node Handling Arbitrary File Naming Conventions Using gnatname
7690 @chapter Handling Arbitrary File Naming Conventions Using @code{gnatname}
7691 @cindex Arbitrary File Naming Conventions
7693 @menu
7694 * Arbitrary File Naming Conventions::
7695 * Running gnatname::
7696 * Qualifiers for gnatname::
7697 * Examples of gnatname Usage::
7698 @end menu
7700 @node Arbitrary File Naming Conventions
7701 @section Arbitrary File Naming Conventions
7703 @noindent
7704 The GNAT compiler must be able to know the source file name of a compilation unit.
7705 When using the standard GNAT default file naming conventions (@code{.ADS} for specs,
7706 @code{.ADB} for bodies), the GNAT compiler does not need additional information.
7708 @noindent
7709 When the source file names do not follow the standard GNAT default file naming
7710 conventions, the GNAT compiler must be given additional information through
7711 a configuration pragmas file (see @ref{Configuration Pragmas}) or a project file.
7712 When the non standard file naming conventions are well-defined, a small number of
7713 pragmas @code{Source_File_Name} specifying a naming pattern
7714 (see @ref{Alternative File Naming Schemes}) may be sufficient. However,
7715 if the file naming conventions are irregular or arbitrary, a number
7716 of pragma @code{Source_File_Name} for individual compilation units must be defined.
7717 To help maintain the correspondence between compilation unit names and
7718 source file names within the compiler,
7719 GNAT provides a tool @code{gnatname} to generate the required pragmas for a
7720 set of files.
7722 @node Running gnatname
7723 @section Running @code{gnatname}
7725 @noindent
7726 The usual form of the @code{gnatname} command is
7728 @smallexample
7729 $ gnatname [@var{qualifiers}] @var{naming_pattern} [@var{naming_patterns}]
7730 @end smallexample
7732 @noindent
7733 All of the arguments are optional. If invoked without any argument,
7734 @code{gnatname} will display its usage.
7736 @noindent
7737 When used with at least one naming pattern, @code{gnatname} will attempt to
7738 find all the compilation units in files that follow at least one of the
7739 naming patterns. To find these compilation units,
7740 @code{gnatname} will use the GNAT compiler in syntax-check-only mode on all
7741 regular files.
7743 @noindent
7744 One or several Naming Patterns may be given as arguments to @code{gnatname}.
7745 Each Naming Pattern is enclosed between double quotes.
7746 A Naming Pattern is a regular expression similar to the wildcard patterns
7747 used in file names by the Unix shells or the DOS prompt.
7749 @noindent
7750 Examples of Naming Patterns are
7752 @smallexample
7753    "*.[12].ADA"
7754    "*.ad[sb]*"
7755    "body_*"    "spec_*"
7756 @end smallexample
7758 @noindent
7759 For a more complete description of the syntax of Naming Patterns, see the second kind
7760 of regular expressions described in @file{G-REGEXP.ADS} (the "Glob" regular
7761 expressions).
7763 @noindent
7764 When invoked with no qualifiers, @code{gnatname} will create a configuration
7765 pragmas file @file{GNAT.ADC} in the current working directory, with pragmas
7766 @code{Source_File_Name} for each file that contains a valid Ada unit.
7768 @node Qualifiers for gnatname
7769 @section Qualifiers for @code{gnatname}
7771 @noindent
7772 Qualifiers for @code{gnatname} must precede any specified Naming Pattern.
7774 @noindent
7775 You may specify any of the following qualifiers to @code{gnatname}:
7777 @table @code
7779 @item -c@file{file}
7780 @cindex @code{-c} (@code{gnatname})
7781 Create a configuration pragmas file @file{file} (instead of the default
7782 @file{GNAT.ADC}). There may be zero, one or more space between @code{-c} and
7783 @file{file}. @file{file} may include directory information. @file{file} must be
7784 writeable. There may be only one qualifier @code{-c}. When a qualifier @code{-c} is
7785 specified, no qualifier @code{-P} may be specified (see below).
7787 @item -d@file{dir}
7788 @cindex @code{-d} (@code{gnatname})
7789 Look for source files in directory @file{dir}. There may be zero, one or more spaces
7790 between @code{-d} and @file{dir}. When a qualifier @code{-d} is specified,
7791 the current working directory will not be searched for source files, unless it
7792 is explictly
7793 specified with a @code{-d} or @code{-D} qualifier. Several qualifiers @code{-d} may be
7794 specified. If @file{dir} is a relative path, it is relative to the directory of
7795 the configuration pragmas file specified with qualifier @code{-c}, or to the directory
7796 of the project file specified with qualifier @code{-P} or, if neither qualifier @code{-c}
7797 nor qualifier @code{-P} are specified, it is relative to the current working
7798 directory. The directory
7799 specified with qualifier @code{-c} must exist and be readable.
7801 @item -D@file{file}
7802 @cindex @code{-D} (@code{gnatname})
7803 Look for source files in all directories listed in text file @file{file}. There may be
7804 zero, one or more spaces between @code{-d} and @file{dir}. @file{file}
7805 must be an existing, readable text file. Each non empty line in @file{file} must be
7806 a directory. Specifying qualifier @code{-D} is equivalent to specifying as many qualifiers
7807 @code{-d} as there are non empty lines in @file{file}.
7809 @item -h
7810 @cindex @code{-h} (@code{gnatname})
7811 Output usage (help) information. The output is written to @file{SYS$OUTPUT}.
7813 @item -P@file{proj}
7814 @cindex @code{-P} (@code{gnatname})
7815 Create or update project file @file{proj}. There may be zero, one or more space
7816 between @code{-P} and @file{proj}. @file{proj} may include directory information.
7817 @file{proj} must be writeable. There may be only one qualifier @code{-P}.
7818 When a qualifier @code{-P} is specified, no qualifier @code{-c} may be specified.
7820 @item -v
7821 @cindex @code{-v} (@code{gnatname})
7822 Verbose mode. Output detailed explanation of behavior to @file{SYS$OUTPUT}. This includes
7823 name of the file written, the name of the directories to search and, for each file
7824 in those directories whose name matches at least one of the Naming Patterns, an
7825 indication of whether the file contains a unit, and if so the name of the unit.
7827 @item -v -v
7828 Very Verbose mode. In addition to the output produced in verbose mode, for each file
7829 in the searched directories whose name matches none of the Naming Patterns, an
7830 indication is given that there is no match.
7832 @item -x@file{pattern}
7833 Excluded patterns. Using this qualifier, it is possible to exclude some files
7834 that would match the name patterns. For example,
7835 @code{"gnatname -x "*_NT.ADA" "*.ADA"} will look for Ada units in all files
7836 with the @file{.ADA} extension, except those whose names end with
7837 @file{_NT.ADA}.
7839 @end table
7841 @node Examples of gnatname Usage
7842 @section Examples of @code{gnatname} Usage
7844 @smallexample
7845 $ gnatname -c /home/me/NAMES.ADC -d sources "[a-z]*.ADA*"
7846 @end smallexample
7848 In this example, the directory @file{/home/me} must already exist and be
7849 writeable. In addition, the directory @file{/home/me/sources} (specified by
7850 @code{-d sources}) must exist and be readable. Note the optional spaces after
7851 @code{-c} and @code{-d}.
7853 @smallexample
7854 $ gnatname -P/home/me/proj -x "*_NT_BODY.ADA" -dsources -dsources/plus -Dcommon_dirs.txt "body_*" "spec_*"
7855 @end smallexample
7857 Note that several qualifiers @code{-d} may be used, even in conjunction with one
7858 or several qualifiers @code{-D}. Several Naming Patterns and one excluded pattern
7859 are used in this example.
7862 @c *****************************************
7863 @c * G N A T  P r o j e c t  M a n a g e r *
7864 @c *****************************************
7865 @node GNAT Project Manager
7866 @chapter GNAT Project Manager
7868 @menu
7869 * Introduction::
7870 * Examples of Project Files::
7871 * Project File Syntax::
7872 * Objects and Sources in Project Files::
7873 * Importing Projects::
7874 * Project Extension::
7875 * External References in Project Files::
7876 * Packages in Project Files::
7877 * Variables from Imported Projects::
7878 * Naming Schemes::
7879 * Library Projects::
7880 * Qualifiers Related to Project Files::
7881 * Tools Supporting Project Files::
7882 * An Extended Example::
7883 * Project File Complete Syntax::
7884 @end menu
7887 @c ****************
7888 @c * Introduction *
7889 @c ****************
7891 @node Introduction
7892 @section Introduction
7894 @noindent
7895 This chapter describes GNAT's @emph{Project Manager}, a facility that
7896 lets you configure various properties for a collection of source files.  In
7897 particular, you can specify:
7898 @itemize @bullet
7899 @item
7900 The directory or set of directories containing the source files, and/or the
7901 names of the specific source files themselves
7902 @item
7903 The directory in which the compiler's output
7904 (@file{ALI} files, object files, tree files) will be placed
7905 @item
7906 The directory in which the executable programs will be placed
7907 @item
7908 Qualifier settings for any of the project-enabled tools (@command{GNAT MAKE},
7909 compiler, binder, linker, @code{GNAT LIST}, @code{GNAT XREF}, @code{GNAT FIND});
7910 you can apply these settings either globally or to individual units
7911 @item
7912 The source files containing the main subprogram(s) to be built
7913 @item
7914 The source programming language(s) (currently Ada and/or C)
7915 @item
7916 Source file naming conventions; you can specify these either globally or for
7917 individual units
7918 @end itemize
7920 @menu
7921 * Project Files::
7922 @end menu
7924 @node Project Files
7925 @subsection Project Files
7927 @noindent
7928 A @dfn{project} is a specific set of values for these properties.  You can
7929 define a project's settings in a @dfn{project file}, a text file with an
7930 Ada-like syntax; a property value is either a string or a list of strings.
7931 Properties that are not explicitly set receive default values.  A project
7932 file may interrogate the values of @dfn{external variables} (user-defined
7933 command-line qualifiers or environment variables), and it may specify property
7934 settings conditionally, based on the value of such variables.
7936 In simple cases, a project's source files depend only on other source files
7937 in the same project, or on the predefined libraries.  ("Dependence" is in
7938 the technical sense; for example, one Ada unit "with"ing another.)  However,
7939 the Project Manager also allows much more sophisticated arrangements,
7940 with the source files in one project depending on source files in other
7941 projects:
7942 @itemize @bullet
7943 @item
7944 One project can @emph{import} other projects containing needed source files.
7945 @item
7946 You can organize GNAT projects in a hierarchy: a @emph{child} project
7947 can extend a @emph{parent} project, inheriting the parent's source files and
7948 optionally overriding any of them with alternative versions
7949 @end itemize
7951 @noindent
7952 More generally, the Project Manager lets you structure large development
7953 efforts into hierarchical subsystems, with build decisions deferred to the
7954 subsystem level and thus different compilation environments (qualifier settings)
7955 used for different subsystems.
7957 The Project Manager is invoked through the @option{-P@emph{projectfile}}
7958 qualifier to @command{GNAT MAKE} or to the @command{gnat} front driver.
7959 If you want to define (on the command line) an external variable that is
7960 queried by the project file, additionally use the
7961 @option{-X@emph{vbl}=@emph{value}} qualifier.
7962 The Project Manager parses and interprets the project file, and drives the
7963 invoked tool based on the project settings.
7965 The Project Manager supports a wide range of development strategies,
7966 for systems of all sizes.  Some typical practices that are easily handled:
7967 @itemize @bullet
7968 @item
7969 Using a common set of source files, but generating object files in different
7970 directories via different qualifier settings
7971 @item
7972 Using a mostly-shared set of source files, but with different versions of
7973 some unit or units
7974 @end itemize
7976 @noindent
7977 The destination of an executable can be controlled inside a project file
7978 using the @option{-o} qualifier. In the absence of such a qualifier either inside
7979 the project file or on the command line, any executable files generated by
7980 @command{GNAT MAKE} will be placed in the directory @code{Exec_Dir} specified
7981 in the project file. If no @code{Exec_Dir} is specified, they will be placed
7982 in the object directory of the project.
7984 You can use project files to achieve some of the effects of a source
7985 versioning system (for example, defining separate projects for
7986 the different sets of sources that comprise different releases) but the
7987 Project Manager is independent of any source configuration management tools
7988 that might be used by the developers.
7990 The next section introduces the main features of GNAT's project facility
7991 through a sequence of examples; subsequent sections will present the syntax
7992 and semantics in more detail.
7995 @c *****************************
7996 @c * Examples of Project Files *
7997 @c *****************************
7999 @node Examples of Project Files
8000 @section Examples of Project Files
8001 @noindent
8002 This section illustrates some of the typical uses of project files and
8003 explains their basic structure and behavior.
8005 @menu
8006 * Common Sources with Different Qualifiers and Different Output Directories::
8007 * Using External Variables::
8008 * Importing Other Projects::
8009 * Extending a Project::
8010 @end menu
8012 @node Common Sources with Different Qualifiers and Different Output Directories
8013 @subsection Common Sources with Different Qualifiers and Different Output Directories
8015 @menu
8016 * Source Files::
8017 * Specifying the Object Directory::
8018 * Specifying the Exec Directory::
8019 * Project File Packages::
8020 * Specifying Qualifier Settings::
8021 * Main Subprograms::
8022 * Source File Naming Conventions::
8023 * Source Language(s)::
8024 @end menu
8026 @noindent
8027 Assume that the Ada source files @file{PACK.ADS}, @file{PACK.ADB}, and
8028 @file{PROC.ADB} are in the @file{/common} directory.  The file
8029 @file{PROC.ADB} contains an Ada main subprogram @code{Proc} that "with"s
8030 package @code{Pack}.  We want to compile these source files under two sets
8031 of qualifiers:
8032 @itemize @bullet
8033 @item
8034 When debugging, we want to pass the @option{-g} qualifier to @command{GNAT MAKE},
8035 and the @option{/CHECKS=ASSERTIONS}, @option{/CHECKS=OVERFLOW}, and @option{/CHECKS=ELABORATION} qualifiers to the
8036 compiler; the compiler's output is to appear in @file{/common/debug}
8037 @item
8038 When preparing a release version, we want to pass the @option{/OPTIMIZE=ALL} qualifier to
8039 the compiler; the compiler's output is to appear in @file{/common/release}
8040 @end itemize
8042 @noindent
8043 The GNAT project files shown below, respectively @file{debug.gpr} and
8044 @file{release.gpr} in the @file{/common} directory, achieve these effects.
8046 Diagrammatically:
8047 @smallexample
8048 @group
8049 /common
8050   debug.gpr
8051   release.gpr
8052   PACK.ADS
8053   PACK.ADB
8054   PROC.ADB
8055 @end group
8056 @group
8057 /common/debug @{-g, /CHECKS=ASSERTIONS, /CHECKS=OVERFLOW, /CHECKS=ELABORATION@}
8058   PROC.ALI, PROC.OBJ
8059   PACK.ALI, PACK.OBJ
8060 @end group
8061 @group
8062 /common/release @{/OPTIMIZE=ALL@}
8063   PROC.ALI, PROC.OBJ
8064   PACK.ALI, PACK.OBJ
8065 @end group
8066 @end smallexample
8067 Here are the project files:
8068 @smallexample
8069 @group
8070 project Debug is
8071   for Object_Dir use "debug";
8072   for Main use ("proc");
8074   package Builder is
8075     for Default_Qualifiers ("Ada") use ("-g");
8076   end Builder;
8077 @end group
8079 @group
8080   package Compiler is
8081     for Default_Qualifiers ("Ada")
8082        use ("-fstack-check", "/CHECKS=ASSERTIONS", "/CHECKS=OVERFLOW", "/CHECKS=ELABORATION");
8083   end Compiler;
8084 end Debug;
8085 @end group
8086 @end smallexample
8088 @smallexample
8089 @group
8090 project Release is
8091   for Object_Dir use "release";
8092   for Exec_Dir use ".";
8093   for Main use ("proc");
8095   package Compiler is
8096     for Default_Qualifiers ("Ada") use ("/OPTIMIZE=ALL");
8097   end Compiler;
8098 end Release;
8099 @end group
8100 @end smallexample
8102 @noindent
8103 The name of the project defined by @file{debug.gpr} is @code{"Debug"} (case
8104 insensitive), and analogously the project defined by @file{release.gpr} is
8105 @code{"Release"}.  For consistency the file should have the same name as the
8106 project, and the project file's extension should be @code{"gpr"}. These
8107 conventions are not required, but a warning is issued if they are not followed.
8109 If the current directory is @file{/temp}, then the command
8110 @smallexample
8111 GNAT MAKE -P/common/debug.gpr
8112 @end smallexample
8114 @noindent
8115 generates object and ALI files in @file{/common/debug}, and the @code{proc}
8116 executable also in @file{/common/debug}, using the qualifier settings defined in
8117 the project file.
8119 Likewise, the command
8120 @smallexample
8121 GNAT MAKE -P/common/release.gpr
8122 @end smallexample
8124 @noindent
8125 generates object and ALI files in @file{/common/release}, and the @code{proc}
8126 executable in @file{/common}, using the qualifier settings from the project file.
8128 @node Source Files
8129 @unnumberedsubsubsec Source Files
8131 @noindent
8132 If a project file does not explicitly specify a set of source directories or
8133 a set of source files, then by default the project's source files are the
8134 Ada source files in the project file directory.  Thus @file{PACK.ADS},
8135 @file{PACK.ADB}, and @file{PROC.ADB} are the source files for both projects.
8137 @node Specifying the Object Directory
8138 @unnumberedsubsubsec Specifying the Object Directory
8140 @noindent
8141 Several project properties are modeled by Ada-style @emph{attributes};
8142 you define the property by supplying the equivalent of an Ada attribute
8143 definition clause in the project file.
8144 A project's object directory is such a property; the corresponding
8145 attribute is @code{Object_Dir}, and its value is a string expression.  A
8146 directory may be specified either as absolute or as relative; in the latter
8147 case, it is relative to the project file directory.  Thus the compiler's
8148 output is directed to @file{/common/debug} (for the @code{Debug} project)
8149 and to @file{/common/release} (for the @code{Release} project).  If
8150 @code{Object_Dir} is not specified, then the default is the project file
8151 directory.
8153 @node Specifying the Exec Directory
8154 @unnumberedsubsubsec Specifying the Exec Directory
8156 @noindent
8157 A project's exec directory is another property; the corresponding
8158 attribute is @code{Exec_Dir}, and its value is also a string expression,
8159 either specified as relative or absolute. If @code{Exec_Dir} is not specified,
8160 then the default is the object directory (which may also be the project file
8161 directory if attribute @code{Object_Dir} is not specified). Thus the executable
8162 is placed in @file{/common/debug} for the @code{Debug} project (attribute
8163 @code{Exec_Dir} not specified) and in @file{/common} for the @code{Release}
8164 project.
8166 @node Project File Packages
8167 @unnumberedsubsubsec Project File Packages
8169 @noindent
8170 A GNAT tool integrated with the Project Manager is modeled by a
8171 corresponding package in the project file.
8172 The @code{Debug} project defines the packages @code{Builder}
8173 (for @command{GNAT MAKE}) and @code{Compiler};
8174 the @code{Release} project defines only the @code{Compiler} package.
8176 The Ada package syntax is not to be taken literally.  Although packages in
8177 project files bear a surface resemblance to packages in Ada source code, the
8178 notation is simply a way to convey a grouping of properties for a named
8179 entity.  Indeed, the package names permitted in project files are restricted
8180 to a predefined set, corresponding to the project-aware tools, and the contents
8181 of packages are limited to a small set of constructs.
8182 The packages in the example above contain attribute definitions.
8185 @node Specifying Qualifier Settings
8186 @unnumberedsubsubsec Specifying Qualifier Settings
8188 @noindent
8189 Qualifier settings for a project-aware tool can be specified through attributes
8190 in the package corresponding to the tool.
8191 The example above illustrates one of the relevant attributes,
8192 @code{Default_Qualifiers}, defined in the packages in both project files.
8193 Unlike simple attributes like @code{Source_Dirs}, @code{Default_Qualifiers} is
8194 known as an @emph{associative array}.  When you define this attribute, you must
8195 supply an "index" (a literal string), and the effect of the attribute
8196 definition is to set the value of the "array" at the specified "index".
8197 For the @code{Default_Qualifiers} attribute, the index is a programming
8198 language (in our case, Ada) , and the value specified (after @code{use})
8199 must be a list of string expressions.
8201 The attributes permitted in project files are restricted to a predefined set.
8202 Some may appear at project level, others in packages.
8203 For any attribute that is an associate array, the index must always be a
8204 literal string, but the restrictions on this string (e.g., a file name or a
8205 language name) depend on the individual attribute.
8206 Also depending on the attribute, its specified value will need to be either a
8207 string or a string list.
8209 In the @code{Debug} project, we set the qualifiers for two tools,
8210 @command{GNAT MAKE} and the compiler, and thus we include corresponding
8211 packages, with each package defining the @code{Default_Qualifiers} attribute
8212 with index @code{"Ada"}.
8213 Note that the package corresponding to
8214 @command{GNAT MAKE} is named @code{Builder}.  The @code{Release} project is
8215 similar, but with just the @code{Compiler} package.
8217 In project @code{Debug} above the qualifiers starting with @option{-gnat} that
8218 are specified in package @code{Compiler} could have been placed in package
8219 @code{Builder}, since @command{GNAT MAKE} transmits all such qualifiers to the
8220 compiler.
8222 @node Main Subprograms
8223 @unnumberedsubsubsec Main Subprograms
8225 @noindent
8226 One of the properties of a project is its list of main subprograms (actually
8227 a list of names of source files containing main subprograms, with the file
8228 extension optional.  This property is captured in the @code{Main} attribute,
8229 whose value is a list of strings.  If a project defines the @code{Main}
8230 attribute, then you do not need to identify the main subprogram(s) when
8231 invoking @command{GNAT MAKE} (see @ref{GNAT MAKE and Project Files}).
8233 @node Source File Naming Conventions
8234 @unnumberedsubsubsec Source File Naming Conventions
8236 @noindent
8237 Since the project files do not specify any source file naming conventions,
8238 the GNAT defaults are used.  The mechanism for defining source file naming
8239 conventions -- a package named @code{Naming} -- will be described below
8240 (@pxref{Naming Schemes}).
8242 @node Source Language(s)
8243 @unnumberedsubsubsec Source Language(s)
8245 @noindent
8246 Since the project files do not specify a @code{Languages} attribute, by
8247 default the GNAT tools assume that the language of the project file is Ada.
8248 More generally, a project can comprise source files
8249 in Ada, C, and/or other languages.
8251 @node Using External Variables
8252 @subsection Using External Variables
8254 @noindent
8255 Instead of supplying different project files for debug and release, we can
8256 define a single project file that queries an external variable (set either
8257 on the command line or via an environment variable) in order to
8258 conditionally define the appropriate settings.  Again, assume that the
8259 source files @file{PACK.ADS}, @file{PACK.ADB}, and @file{PROC.ADB} are
8260 located in directory @file{/common}.  The following project file,
8261 @file{build.gpr}, queries the external variable named @code{STYLE} and
8262 defines an object directory and qualifier settings based on whether the value
8263 is @code{"deb"} (debug) or @code{"rel"} (release), where the default is
8264 @code{"deb"}.
8266 @smallexample
8267 @group
8268 project Build is
8269   for Main use ("proc");
8271   type Style_Type is ("deb", "rel");
8272   Style : Style_Type := external ("STYLE", "deb");
8274   case Style is
8275     when "deb" =>
8276       for Object_Dir use "debug";
8278     when "rel" =>
8279       for Object_Dir use "release";
8280       for Exec_Dir use ".";
8281   end case;
8282 @end group
8284 @group
8285   package Builder is
8287     case Style is
8288       when "deb" =>
8289         for Default_Qualifiers ("Ada") use ("-g");
8290     end case;
8292   end Builder;
8293 @end group
8295 @group
8296   package Compiler is
8298     case Style is
8299       when "deb" =>
8300         for Default_Qualifiers ("Ada") use ("/CHECKS=ASSERTIONS", "/CHECKS=OVERFLOW", "/CHECKS=ELABORATION");
8302       when "rel" =>
8303         for Default_Qualifiers ("Ada") use ("/OPTIMIZE=ALL");
8304     end case;
8306   end Compiler;
8308 end Build;
8309 @end group
8310 @end smallexample
8312 @noindent
8313 @code{Style_Type} is an example of a @emph{string type}, which is the project
8314 file analog of an Ada enumeration type but containing string literals rather
8315 than identifiers.  @code{Style} is declared as a variable of this type.
8317 The form @code{external("STYLE", "deb")} is known as an
8318 @emph{external reference}; its first argument is the name of an
8319 @emph{external variable}, and the second argument is a default value to be
8320 used if the external variable doesn't exist.  You can define an external
8321 variable on the command line via the @option{-X} qualifier, or you can use an
8322 environment variable as an external variable.
8324 Each @code{case} construct is expanded by the Project Manager based on the
8325 value of @code{Style}. Thus the command
8326 @smallexample
8327 GNAT MAKE -P/common/build.gpr -XSTYLE=deb
8328 @end smallexample
8330 @noindent
8331 is equivalent to the @command{GNAT MAKE} invocation using the project file
8332 @file{debug.gpr} in the earlier example.  So is the command
8333 @smallexample
8334 GNAT MAKE -P/common/build.gpr
8335 @end smallexample
8337 @noindent
8338 since @code{"deb"} is the default for @code{STYLE}.
8340 Analogously,
8341 @smallexample
8342 GNAT MAKE -P/common/build.gpr -XSTYLE=rel
8343 @end smallexample
8345 @noindent
8346 is equivalent to the @command{GNAT MAKE} invocation using the project file
8347 @file{release.gpr} in the earlier example.
8350 @node Importing Other Projects
8351 @subsection Importing Other Projects
8353 @noindent
8354 A compilation unit in a source file in one project may depend on compilation
8355 units in source files in other projects.  To obtain this behavior, the
8356 dependent project must @emph{import} the projects containing the needed source
8357 files.  This effect is embodied in syntax similar to an Ada @code{with} clause,
8358 but the "with"ed entities are strings denoting project files.
8360 As an example, suppose that the two projects @code{GUI_Proj} and
8361 @code{Comm_Proj} are defined in the project files @file{gui_proj.gpr} and
8362 @file{comm_proj.gpr} in directories @file{/gui} and @file{/comm},
8363 respectively.  Assume that the source files for @code{GUI_Proj} are
8364 @file{GUI.ADS} and @file{GUI.ADB}, and that the source files for
8365 @code{Comm_Proj} are @file{COMM.ADS} and @file{COMM.ADB}, with each set of
8366 files located in its respective project file directory.  Diagrammatically:
8368 @smallexample
8369 @group
8370 /gui
8371   gui_proj.gpr
8372   GUI.ADS
8373   GUI.ADB
8374 @end group
8376 @group
8377 /comm
8378   comm_proj.gpr
8379   COMM.ADS
8380   COMM.ADB
8381 @end group
8382 @end smallexample
8384 @noindent
8385 We want to develop an application in directory @file{/app} that "with"s the
8386 packages @code{GUI} and @code{Comm}, using the properties of the
8387 corresponding project files (e.g. the qualifier settings and object directory).
8388 Skeletal code for a main procedure might be something like the following:
8390 @smallexample
8391 @group
8392 with GUI, Comm;
8393 procedure App_Main is
8394    ...
8395 begin
8396    ...
8397 end App_Main;
8398 @end group
8399 @end smallexample
8401 @noindent
8402 Here is a project file, @file{app_proj.gpr}, that achieves the desired
8403 effect:
8405 @smallexample
8406 @group
8407 with "/gui/gui_proj", "/comm/comm_proj";
8408 project App_Proj is
8409    for Main use ("app_main");
8410 end App_Proj;
8411 @end group
8412 @end smallexample
8414 @noindent
8415 Building an executable is achieved through the command:
8416 @smallexample
8417 GNAT MAKE -P/app/app_proj
8418 @end smallexample
8419 @noindent
8420 which will generate the @code{app_main} executable in the directory where
8421 @file{app_proj.gpr} resides.
8423 If an imported project file uses the standard extension (@code{gpr}) then
8424 (as illustrated above) the @code{with} clause can omit the extension.
8426 Our example specified an absolute path for each imported project file.
8427 Alternatively, you can omit the directory if either
8428 @itemize @bullet
8429 @item
8430 The imported project file is in the same directory as the importing project
8431 file, or
8432 @item
8433 You have defined an environment variable @code{ADA_PROJECT_PATH} that
8434 includes the directory containing the needed project file.
8435 @end itemize
8437 @noindent
8438 Thus, if we define @code{ADA_PROJECT_PATH} to include @file{/gui} and
8439 @file{/comm}, then our project file @file{app_proj.gpr} could be written as
8440 follows:
8442 @smallexample
8443 @group
8444 with "gui_proj", "comm_proj";
8445 project App_Proj is
8446    for Main use ("app_main");
8447 end App_Proj;
8448 @end group
8449 @end smallexample
8451 @noindent
8452 Importing other projects raises the possibility of ambiguities.  For
8453 example, the same unit might be present in different imported projects, or
8454 it might be present in both the importing project and an imported project.
8455 Both of these conditions are errors.  Note that in the current version of
8456 the Project Manager, it is illegal to have an ambiguous unit even if the
8457 unit is never referenced by the importing project.  This restriction may be
8458 relaxed in a future release.
8460 @node Extending a Project
8461 @subsection Extending a Project
8463 @noindent
8464 A common situation in large software systems is to have multiple
8465 implementations for a common interface; in Ada terms, multiple versions of a
8466 package body for the same specification.  For example, one implementation
8467 might be safe for use in tasking programs, while another might only be used
8468 in sequential applications.  This can be modeled in GNAT using the concept
8469 of @emph{project extension}.  If one project (the "child") @emph{extends}
8470 another project (the "parent") then by default all source files of the
8471 parent project are inherited by the child, but the child project can
8472 override any of the parent's source files with new versions, and can also
8473 add new files.  This facility is the project analog of extension in
8474 Object-Oriented Programming.  Project hierarchies are permitted (a child
8475 project may be the parent of yet another project), and a project that
8476 inherits one project can also import other projects.
8478 As an example, suppose that directory @file{/seq} contains the project file
8479 @file{seq_proj.gpr} and the source files @file{PACK.ADS}, @file{PACK.ADB},
8480 and @file{PROC.ADB}:
8482 @smallexample
8483 @group
8484 /seq
8485   PACK.ADS
8486   PACK.ADB
8487   PROC.ADB
8488   seq_proj.gpr
8489 @end group
8490 @end smallexample
8492 @noindent
8493 Note that the project file can simply be empty (that is, no attribute or
8494 package is defined):
8496 @smallexample
8497 @group
8498 project Seq_Proj is
8499 end Seq_Proj;
8500 @end group
8501 @end smallexample
8503 @noindent
8504 implying that its source files are all the Ada source files in the project
8505 directory.
8507 Suppose we want to supply an alternate version of @file{PACK.ADB}, in
8508 directory @file{/tasking}, but use the existing versions of @file{PACK.ADS}
8509 and @file{PROC.ADB}.  We can define a project @code{Tasking_Proj} that
8510 inherits @code{Seq_Proj}:
8512 @smallexample
8513 @group
8514 /tasking
8515   PACK.ADB
8516   tasking_proj.gpr
8517 @end group
8519 @group
8520 project Tasking_Proj extends "/seq/seq_proj" is
8521 end Tasking_Proj;
8522 @end group
8523 @end smallexample
8525 @noindent
8526 The version of @file{PACK.ADB} used in a build depends on which project file
8527 is specified.
8529 Note that we could have designed this using project import rather than
8530 project inheritance; a @code{base} project would contain the sources for
8531 @file{PACK.ADS} and @file{PROC.ADB}, a sequential project would import
8532 @code{base} and add @file{PACK.ADB}, and likewise a tasking project would
8533 import @code{base} and add a different version of @file{PACK.ADB}.  The
8534 choice depends on whether other sources in the original project need to be
8535 overridden.  If they do, then project extension is necessary, otherwise,
8536 importing is sufficient.
8539 @c ***********************
8540 @c * Project File Syntax *
8541 @c ***********************
8543 @node Project File Syntax
8544 @section Project File Syntax
8546 @menu
8547 * Basic Syntax::
8548 * Packages::
8549 * Expressions::
8550 * String Types::
8551 * Variables::
8552 * Attributes::
8553 * Associative Array Attributes::
8554 * case Constructions::
8555 @end menu
8557 @noindent
8558 This section describes the structure of project files.
8560 A project may be an @emph{independent project}, entirely defined by a single
8561 project file. Any Ada source file in an independent project depends only
8562 on the predefined library and other Ada source files in the same project.
8564 @noindent
8565 A project may also @dfn{depend on} other projects, in either or both of the following ways:
8566 @itemize @bullet
8567 @item It may import any number of projects
8568 @item It may extend at most one other project
8569 @end itemize
8571 @noindent
8572 The dependence relation is a directed acyclic graph (the subgraph reflecting
8573 the "extends" relation is a tree).
8575 A project's @dfn{immediate sources} are the source files directly defined by
8576 that project, either implicitly by residing in the project file's directory,
8577 or explicitly through any of the source-related attributes described below.
8578 More generally, a project @var{proj}'s @dfn{sources} are the immediate sources
8579 of @var{proj} together with the immediate sources (unless overridden) of any
8580 project on which @var{proj} depends (either directly or indirectly).
8582 @node Basic Syntax
8583 @subsection Basic Syntax
8585 @noindent
8586 As seen in the earlier examples, project files have an Ada-like syntax.
8587 The minimal project file is:
8588 @smallexample
8589 @group
8590 project Empty is
8592 end Empty;
8593 @end group
8594 @end smallexample
8596 @noindent
8597 The identifier @code{Empty} is the name of the project.
8598 This project name must be present after the reserved
8599 word @code{end} at the end of the project file, followed by a semi-colon.
8601 Any name in a project file, such as the project name or a variable name,
8602 has the same syntax as an Ada identifier.
8604 The reserved words of project files are the Ada reserved words plus
8605 @code{extends}, @code{external}, and @code{project}.  Note that the only Ada
8606 reserved words currently used in project file syntax are:
8608 @itemize @bullet
8609 @item
8610 @code{case}
8611 @item
8612 @code{end}
8613 @item
8614 @code{for}
8615 @item
8616 @code{is}
8617 @item
8618 @code{others}
8619 @item
8620 @code{package}
8621 @item
8622 @code{renames}
8623 @item
8624 @code{type}
8625 @item
8626 @code{use}
8627 @item
8628 @code{when}
8629 @item
8630 @code{with}
8631 @end itemize
8633 @noindent
8634 Comments in project files have the same syntax as in Ada, two consecutives
8635 hyphens through the end of the line.
8637 @node Packages
8638 @subsection Packages
8640 @noindent
8641 A project file may contain @emph{packages}. The name of a package must be one
8642 of the identifiers (case insensitive) from a predefined list, and a package
8643 with a given name may only appear once in a project file. The predefined list
8644 includes the following packages:
8646 @itemize @bullet
8647 @item
8648 @code{Naming}
8649 @item
8650 @code{Builder}
8651 @item
8652 @code{Compiler}
8653 @item
8654 @code{Binder}
8655 @item
8656 @code{Linker}
8657 @item
8658 @code{Finder}
8659 @item
8660 @code{Cross_Reference}
8661 @item
8662 @code{GNAT LIST}
8663 @end itemize
8665 @noindent
8666 (The complete list of the package names and their attributes can be found
8667 in file @file{PRJ-ATTR.ADB}).
8669 @noindent
8670 In its simplest form, a package may be empty:
8672 @smallexample
8673 @group
8674 project Simple is
8675   package Builder is
8676   end Builder;
8677 end Simple;
8678 @end group
8679 @end smallexample
8681 @noindent
8682 A package may contain @emph{attribute declarations},
8683 @emph{variable declarations} and @emph{case constructions}, as will be
8684 described below.
8686 When there is ambiguity between a project name and a package name,
8687 the name always designates the project. To avoid possible confusion, it is
8688 always a good idea to avoid naming a project with one of the
8689 names allowed for packages or any name that starts with @code{gnat}.
8692 @node Expressions
8693 @subsection Expressions
8695 @noindent
8696 An @emph{expression} is either a @emph{string expression} or a
8697 @emph{string list expression}.
8699 A @emph{string expression} is either a @emph{simple string expression} or a
8700 @emph{compound string expression}.
8702 A @emph{simple string expression} is one of the following:
8703 @itemize @bullet
8704 @item A literal string; e.g.@code{"comm/my_proj.gpr"}
8705 @item A string-valued variable reference (see @ref{Variables})
8706 @item A string-valued attribute reference (see @ref{Attributes})
8707 @item An external reference (see @ref{External References in Project Files})
8708 @end itemize
8710 @noindent
8711 A @emph{compound string expression} is a concatenation of string expressions,
8712 using @code{"&"}
8713 @smallexample
8714        Path & "/" & File_Name & ".ADS"
8715 @end smallexample
8717 @noindent
8718 A @emph{string list expression} is either a
8719 @emph{simple string list expression} or a
8720 @emph{compound string list expression}.
8722 A @emph{simple string list expression} is one of the following:
8723 @itemize @bullet
8724 @item A parenthesized list of zero or more string expressions, separated by commas
8725 @smallexample
8726    File_Names := (File_Name, "GNAT.ADC", File_Name & ".orig");
8727    Empty_List := ();
8728 @end smallexample
8729 @item A string list-valued variable reference
8730 @item A string list-valued attribute reference
8731 @end itemize
8733 @noindent
8734 A @emph{compound string list expression} is the concatenation (using
8735 @code{"&"}) of a simple string list expression and an expression.  Note that
8736 each term in a compound string list expression, except the first, may be
8737 either a string expression or a string list expression.
8739 @smallexample
8740 @group
8741    File_Name_List := () & File_Name; --  One string in this list
8742    Extended_File_Name_List := File_Name_List & (File_Name & ".orig");
8743    --  Two strings
8744    Big_List := File_Name_List & Extended_File_Name_List;
8745    --  Concatenation of two string lists: three strings
8746    Illegal_List := "GNAT.ADC" & Extended_File_Name_List;
8747    --  Illegal: must start with a string list
8748 @end group
8749 @end smallexample
8752 @node String Types
8753 @subsection String Types
8755 @noindent
8756 The value of a variable may be restricted to a list of string literals.
8757 The restricted list of string literals is given in a
8758 @emph{string type declaration}.
8760 Here is an example of a string type declaration:
8762 @smallexample
8763    type OS is ("NT, "nt", "Unix", "Linux", "other OS");
8764 @end smallexample
8766 @noindent
8767 Variables of a string type are called @emph{typed variables}; all other
8768 variables are called @emph{untyped variables}. Typed variables are
8769 particularly useful in @code{case} constructions
8770 (see @ref{case Constructions}).
8772 A string type declaration starts with the reserved word @code{type}, followed
8773 by the name of the string type (case-insensitive), followed by the reserved
8774 word @code{is}, followed by a parenthesized list of one or more string literals
8775 separated by commas, followed by a semicolon.
8777 The string literals in the list are case sensitive and must all be different.
8778 They may include any graphic characters allowed in Ada, including spaces.
8780 A string type may only be declared at the project level, not inside a package.
8782 A string type may be referenced by its name if it has been declared in the same
8783 project file, or by its project name, followed by a dot,
8784 followed by the string type name.
8787 @node Variables
8788 @subsection Variables
8790 @noindent
8791 A variable may be declared at the project file level, or in a package.
8792 Here are some examples of variable declarations:
8794 @smallexample
8795 @group
8796    This_OS : OS := external ("OS"); --  a typed variable declaration
8797    That_OS := "Linux";              --  an untyped variable declaration
8798 @end group
8799 @end smallexample
8801 @noindent
8802 A @emph{typed variable declaration} includes the variable name, followed by a colon,
8803 followed by the name of a string type, followed by @code{:=}, followed by
8804 a simple string expression.
8806 An @emph{untyped variable declaration} includes the variable name,
8807 followed by @code{:=}, followed by an expression.  Note that, despite the
8808 terminology, this form of "declaration" resembles more an assignment
8809 than a declaration in Ada.  It is a declaration in several senses:
8810 @itemize @bullet
8811 @item
8812 The variable name does not need to be defined previously
8813 @item
8814 The declaration establishes the @emph{kind} (string versus string list) of the
8815 variable, and later declarations of the same variable need to be consistent
8816 with this
8817 @end itemize
8819 @noindent
8820 A string variable declaration (typed or untyped) declares a variable
8821 whose value is a string. This variable may be used as a string expression.
8822 @smallexample
8823    File_Name       := "readme.txt";
8824    Saved_File_Name := File_Name & ".saved";
8825 @end smallexample
8827 @noindent
8828 A string list variable declaration declares a variable whose value is a list
8829 of strings. The list may contain any number (zero or more) of strings.
8831 @smallexample
8832    Empty_List := ();
8833    List_With_One_Element := ("/STYLE=");
8834    List_With_Two_Elements := List_With_One_Element & "/STYLE=GNAT";
8835    Long_List := ("MAIN.ADA", "PACK1_.ADA", "PACK1.ADA", "PACK2_.ADA"
8836                  "PACK2.ADA", "UTIL_.ADA", "UTIL.ADA");
8837 @end smallexample
8839 @noindent
8840 The same typed variable may not be declared more than once at project level, and it may not be declared more than once in any package; it is in effect a constant or a readonly variable.
8842 The same untyped variable may be declared several times.
8843 In this case, the new value replaces the old one,
8844 and any subsequent reference to the variable uses the new value.
8845 However, as noted above, if a variable has been declared as a string, all subsequent
8846 declarations must give it a string value. Similarly, if a variable has
8847 been declared as a string list, all subsequent declarations
8848 must give it a string list value.
8850 A @emph{variable reference} may take several forms:
8852 @itemize @bullet
8853 @item The simple variable name, for a variable in the current package (if any) or in the current project
8854 @item A context name, followed by a dot, followed by the variable name.
8855 @end itemize
8857 @noindent
8858 A @emph{context} may be one of the following:
8860 @itemize @bullet
8861 @item The name of an existing package in the current project
8862 @item The name of an imported project of the current project
8863 @item The name of an ancestor project (i.e., a project extended by the current project, either directly or indirectly)
8864 @item An imported/parent project name, followed by a dot, followed by a package name
8865 @end itemize
8867 @noindent
8868 A variable reference may be used in an expression.
8871 @node Attributes
8872 @subsection Attributes
8874 @noindent
8875 A project (and its packages) may have @emph{attributes} that define the project's properties.
8876 Some attributes have values that are strings;
8877 others have values that are string lists.
8879 There are two categories of attributes: @emph{simple attributes} and @emph{associative arrays}
8880 (see @ref{Associative Array Attributes}).
8882 The names of the attributes are restricted; there is a list of project
8883 attributes, and a list of package attributes for each package.
8884 The names are not case sensitive.
8886 The project attributes are as follows (all are simple attributes):
8888 @multitable @columnfractions .4 .3
8889 @item @emph{Attribute Name}
8890 @tab @emph{Value}
8891 @item @code{Source_Files}
8892 @tab string list
8893 @item @code{Source_Dirs}
8894 @tab string list
8895 @item @code{Source_List_File}
8896 @tab string
8897 @item @code{Object_Dir}
8898 @tab string
8899 @item @code{Exec_Dir}
8900 @tab string
8901 @item @code{Main}
8902 @tab string list
8903 @item @code{Languages}
8904 @tab string list
8905 @item @code{Library_Dir}
8906 @tab string
8907 @item @code{Library_Name}
8908 @tab string
8909 @item @code{Library_Kind}
8910 @tab string
8911 @item @code{Library_Elaboration}
8912 @tab string
8913 @item @code{Library_Version}
8914 @tab string
8915 @end multitable
8917 @noindent
8918 The attributes for package @code{Naming} are as follows
8919 (see @ref{Naming Schemes}):
8921 @multitable @columnfractions .4 .2 .2 .2
8922 @item Attribute Name @tab Category @tab Index @tab Value
8923 @item @code{Specification_Suffix}
8924 @tab associative array
8925 @tab language name
8926 @tab string
8927 @item @code{Implementation_Suffix}
8928 @tab associative array
8929 @tab language name
8930 @tab string
8931 @item @code{Separate_Suffix}
8932 @tab simple attribute
8933 @tab n/a
8934 @tab string
8935 @item @code{Casing}
8936 @tab simple attribute
8937 @tab n/a
8938 @tab string
8939 @item @code{Dot_Replacement}
8940 @tab simple attribute
8941 @tab n/a
8942 @tab string
8943 @item @code{Specification}
8944 @tab associative array
8945 @tab Ada unit name
8946 @tab string
8947 @item @code{Implementation}
8948 @tab associative array
8949 @tab Ada unit name
8950 @tab string
8951 @item @code{Specification_Exceptions}
8952 @tab associative array
8953 @tab language name
8954 @tab string list
8955 @item @code{Implementation_Exceptions}
8956 @tab associative array
8957 @tab language name
8958 @tab string list
8959 @end multitable
8961 @noindent
8962 The attributes for package @code{Builder}, @code{Compiler}, @code{Binder},
8963 @code{Linker}, @code{Cross_Reference}, and @code{Finder}
8964 are as follows (see @ref{Qualifiers and Project Files}).
8966 @multitable @columnfractions .4 .2 .2 .2
8967 @item Attribute Name @tab Category @tab Index @tab Value
8968 @item @code{Default_Qualifiers}
8969 @tab associative array
8970 @tab language name
8971 @tab string list
8972 @item @code{Qualifiers}
8973 @tab associative array
8974 @tab file name
8975 @tab string list
8976 @end multitable
8978 @noindent
8979 In addition, package @code{Builder} has a single string attribute
8980 @code{Local_Configuration_Pragmas} and package @code{Builder} has a single
8981 string attribute @code{Global_Configuration_Pragmas}.
8983 @noindent
8984 The attribute for package @code{Glide} are not documented: they are for
8985 internal use only.
8987 @noindent
8988 Each simple attribute has a default value: the empty string (for string-valued
8989 attributes) and the empty list (for string list-valued attributes).
8991 Similar to variable declarations, an attribute declaration defines a new value
8992 for an attribute.
8994 Examples of simple attribute declarations:
8996 @smallexample
8997    for Object_Dir use "objects";
8998    for Source_Dirs use ("units", "test/drivers");
8999 @end smallexample
9001 @noindent
9002 A @dfn{simple attribute declaration} starts with the reserved word @code{for},
9003 followed by the name of the attribute, followed by the reserved word
9004 @code{use}, followed by an expression (whose kind depends on the attribute),
9005 followed by a semicolon.
9007 Attributes may be referenced in expressions.
9008 The general form for such a reference is @code{<entity>'<attribute>}:
9009 the entity for which the attribute is defined,
9010 followed by an apostrophe, followed by the name of the attribute.
9011 For associative array attributes, a litteral string between parentheses
9012 need to be supplied as index.
9014 Examples are:
9016 @smallexample
9017   project'Object_Dir
9018   Naming'Dot_Replacement
9019   Imported_Project'Source_Dirs
9020   Imported_Project.Naming'Casing
9021   Builder'Default_Qualifiers("Ada")
9022 @end smallexample
9024 @noindent
9025 The entity may be:
9026 @itemize @bullet
9027 @item @code{project} for an attribute of the current project
9028 @item The name of an existing package of the current project
9029 @item The name of an imported project
9030 @item The name of a parent project (extended by the current project)
9031 @item An imported/parent project name, followed by a dot,
9032       followed by a package name
9033 @end itemize
9035 @noindent
9036 Example:
9037 @smallexample
9038 @group
9039    project Prj is
9040      for Source_Dirs use project'Source_Dirs & "units";
9041      for Source_Dirs use project'Source_Dirs & "test/drivers"
9042    end Prj;
9043 @end group
9044 @end smallexample
9046 @noindent
9047 In the first attribute declaration, initially the attribute @code{Source_Dirs}
9048 has the default value: an empty string list. After this declaration,
9049 @code{Source_Dirs} is a string list of one element: "units".
9050 After the second attribute declaration @code{Source_Dirs} is a string list of
9051 two elements: "units" and "test/drivers".
9053 Note: this example is for illustration only. In practice,
9054 the project file would contain only one attribute declaration:
9056 @smallexample
9057    for Source_Dirs use ("units", "test/drivers");
9058 @end smallexample
9061 @node Associative Array Attributes
9062 @subsection Associative Array Attributes
9064 @noindent
9065 Some attributes are defined as @emph{associative arrays}. An associative
9066 array may be regarded as a function that takes a string as a parameter
9067 and delivers a string or string list value as its result.
9069 Here are some examples of associative array attribute declarations:
9071 @smallexample
9072    for Implementation ("main") use "MAIN.ADA";
9073    for Qualifiers ("MAIN.ADA") use ("-v", "/REPORT_ERRORS=VERBOSE");
9074    for Qualifiers ("MAIN.ADA") use Builder'Qualifiers ("MAIN.ADA") & "-g";
9075 @end smallexample
9077 @noindent
9078 Like untyped variables and simple attributes, associative array attributes may be declared several times. Each declaration supplies a new value for the
9079 attribute, replacing the previous setting.
9082 @node case Constructions
9083 @subsection @code{case} Constructions
9085 @noindent
9086 A @code{case} construction is used in a project file to effect conditional
9087 behavior.
9088 Here is a typical example:
9090 @smallexample
9091 @group
9092 project MyProj is
9093    type OS_Type is ("Linux", "Unix", "NT", "VMS");
9095    OS : OS_Type := external ("OS", "Linux");
9096 @end group
9098 @group
9099    package Compiler is
9100      case OS is
9101        when "Linux" | "Unix" =>
9102          for Default_Qualifiers ("Ada") use ("-gnath");
9103        when "NT" =>
9104          for Default_Qualifiers ("Ada") use ("/POLLING_ENABLE");
9105        when others =>
9106      end case;
9107    end Compiler;
9108 end MyProj;
9109 @end group
9110 @end smallexample
9112 @noindent
9113 The syntax of a @code{case} construction is based on the Ada case statement
9114 (although there is no @code{null} construction for empty alternatives).
9116 Following the reserved word @code{case} there is the case variable (a typed
9117 string variable), the reserved word @code{is}, and then a sequence of one or
9118 more alternatives.
9119 Each alternative comprises the reserved word @code{when}, either a list of
9120 literal strings separated by the @code{"|"} character or the reserved word
9121 @code{others},  and the @code{"=>"} token.
9122 Each literal string must belong to the string type that is the type of the
9123 case variable.
9124 An @code{others} alternative, if present, must occur last.
9125 The @code{end case;} sequence terminates the case construction.
9127 After each @code{=>}, there are zero or more constructions.  The only
9128 constructions allowed in a case construction are other case constructions and
9129 attribute declarations. String type declarations, variable declarations and
9130 package declarations are not allowed.
9132 The value of the case variable is often given by an external reference
9133 (see @ref{External References in Project Files}).
9136 @c ****************************************
9137 @c * Objects and Sources in Project Files *
9138 @c ****************************************
9140 @node Objects and Sources in Project Files
9141 @section Objects and Sources in Project Files
9143 @menu
9144 * Object Directory::
9145 * Exec Directory::
9146 * Source Directories::
9147 * Source File Names::
9148 @end menu
9150 @noindent
9151 Each project has exactly one object directory and one or more source
9152 directories. The source directories must contain at least one source file,
9153 unless  the project file explicitly specifies that no source files are present
9154 (see @ref{Source File Names}).
9157 @node Object Directory
9158 @subsection Object Directory
9160 @noindent
9161 The object directory for a project is the directory containing the compiler's
9162 output (such as @file{ALI} files and object files) for the project's immediate
9163 sources. Note that for inherited sources (when extending a parent project) the
9164 parent project's object directory is used.
9166 The object directory is given by the value of the attribute @code{Object_Dir}
9167 in the project file.
9169 @smallexample
9170    for Object_Dir use "objects";
9171 @end smallexample
9173 @noindent
9174 The attribute @var{Object_Dir} has a string value, the path name of the object
9175 directory. The path name may be absolute or relative to the directory of the
9176 project file. This directory must already exist, and be readable and writable.
9178 By default, when the attribute @code{Object_Dir} is not given an explicit value
9179 or when its value is the empty string, the object directory is the same as the
9180 directory containing the project file.
9183 @node Exec Directory
9184 @subsection Exec Directory
9186 @noindent
9187 The exec directory for a project is the directory containing the executables
9188 for the project's main subprograms.
9190 The exec directory is given by the value of the attribute @code{Exec_Dir}
9191 in the project file.
9193 @smallexample
9194    for Exec_Dir use "executables";
9195 @end smallexample
9197 @noindent
9198 The attribute @var{Exec_Dir} has a string value, the path name of the exec
9199 directory. The path name may be absolute or relative to the directory of the
9200 project file. This directory must already exist, and be writable.
9202 By default, when the attribute @code{Exec_Dir} is not given an explicit value
9203 or when its value is the empty string, the exec directory is the same as the
9204 object directory of the project file.
9207 @node Source Directories
9208 @subsection Source Directories
9210 @noindent
9211 The source directories of a project are specified by the project file
9212 attribute @code{Source_Dirs}.
9214 This attribute's value is a string list. If the attribute is not given an
9215 explicit value, then there is only one source directory, the one where the
9216 project file resides.
9218 A @code{Source_Dirs} attribute that is explicitly defined to be the empty list,
9219 as in
9221 @smallexample
9222     for Source_Dirs use ();
9223 @end smallexample
9225 @noindent
9226 indicates that the project contains no source files.
9228 Otherwise, each string in the string list designates one or more
9229 source directories.
9231 @smallexample
9232    for Source_Dirs use ("sources", "test/drivers");
9233 @end smallexample
9235 @noindent
9236 If a string in the list ends with @code{"/**"},  then the directory whose path
9237 name precedes the two asterisks, as well as all its subdirectories
9238 (recursively), are source directories.
9240 @smallexample
9241    for Source_Dirs use ("/system/sources/**");
9242 @end smallexample
9244 @noindent
9245 Here the directory @code{/system/sources} and all of its subdirectories
9246 (recursively) are source directories.
9248 To specify that the source directories are the directory of the project file
9249 and all of its subdirectories, you can declare @code{Source_Dirs} as follows:
9250 @smallexample
9251    for Source_Dirs use ("./**");
9252 @end smallexample
9254 @noindent
9255 Each of the source directories must exist and be readable.
9258 @node Source File Names
9259 @subsection Source File Names
9261 @noindent
9262 In a project that contains source files, their names may be specified by the
9263 attributes @code{Source_Files} (a string list) or @code{Source_List_File}
9264 (a string). Source file names never include any directory information.
9266 If the attribute @code{Source_Files} is given an explicit value, then each
9267 element of the list is a source file name.
9269 @smallexample
9270    for Source_Files use ("MAIN.ADB");
9271    for Source_Files use ("MAIN.ADB", "PACK1.ADS", "PACK2.ADB");
9272 @end smallexample
9274 @noindent
9275 If the attribute @code{Source_Files} is not given an explicit value,
9276 but the attribute @code{Source_List_File} is given a string value,
9277 then the source file names are contained in the text file whose path name
9278 (absolute or relative to the directory of the project file) is the
9279 value of the attribute @code{Source_List_File}.
9281 Each line in the file that is not empty or is not a comment
9282 contains a source file name. A comment line starts with two hyphens.
9284 @smallexample
9285    for Source_List_File use "source_list.txt";
9286 @end smallexample
9288 @noindent
9289 By default, if neither the attribute @code{Source_Files} nor the attribute
9290 @code{Source_List_File} is given an explicit value, then each file in the
9291 source directories that conforms to the project's naming scheme
9292 (see @ref{Naming Schemes}) is an immediate source of the project.
9294 A warning is issued if both attributes @code{Source_Files} and
9295 @code{Source_List_File} are given explicit values. In this case, the attribute
9296 @code{Source_Files} prevails.
9298 Each source file name must be the name of one and only one existing source file
9299 in one of the source directories.
9301 A @code{Source_Files} attribute defined with an empty list as its value
9302 indicates that there are no source files in the project.
9304 Except for projects that are clearly specified as containing no Ada source
9305 files (@code{Source_Dirs} or @code{Source_Files} specified as an empty list,
9306 or @code{Languages} specified without @code{"Ada"} in the list)
9307 @smallexample
9308    for Source_Dirs use ();
9309    for Source_Files use ();
9310    for Languages use ("C", "C++");
9311 @end smallexample
9313 @noindent
9314 a project must contain at least one immediate source.
9316 Projects with no source files are useful as template packages
9317 (see @ref{Packages in Project Files}) for other projects; in particular to
9318 define a package @code{Naming} (see @ref{Naming Schemes}).
9321 @c ****************************
9322 @c * Importing Projects *
9323 @c ****************************
9325 @node  Importing Projects
9326 @section Importing Projects
9328 @noindent
9329 An immediate source of a project P may depend on source files that
9330 are neither immediate sources of P nor in the predefined library.
9331 To get this effect, P must @emph{import} the projects that contain the needed
9332 source files.
9334 @smallexample
9335 @group
9336   with "project1", "utilities.gpr";
9337   with "/namings/apex.gpr";
9338   project Main is
9339     ...
9340 @end group
9341 @end smallexample
9343 @noindent
9344 As can be seen in this example, the syntax for importing projects is similar
9345 to the syntax for importing compilation units in Ada. However, project files
9346 use literal strings instead of names, and the @code{with} clause identifies
9347 project files rather than packages.
9349 Each literal string is the file name or path name (absolute or relative) of a
9350 project file. If a string is simply a file name, with no path, then its
9351 location is determined by the @emph{project path}:
9353 @itemize @bullet
9354 @item
9355 If the environment variable @env{ADA_PROJECT_PATH} exists, then the project
9356 path includes all the directories in this environment variable, plus the
9357 directory of the project file.
9359 @item
9360 If the environment variable @env{ADA_PROJECT_PATH} does not exist,
9361 then the project path contains only one directory, namely the one where
9362 the project file is located.
9363 @end itemize
9365 @noindent
9366 If a relative pathname is used as in
9368 @smallexample
9369   with "tests/proj";
9370 @end smallexample
9372 @noindent
9373 then the path is relative to the directory where the importing project file is
9374 located. Any symbolic link will be fully resolved in the directory
9375 of the importing project file before the imported project file is looked up.
9377 When the @code{with}'ed project file name does not have an extension,
9378 the default is @file{.gpr}. If a file with this extension is not found, then
9379 the file name as specified in the @code{with} clause (no extension) will be
9380 used. In the above example, if a file @code{project1.gpr} is found, then it
9381 will be used; otherwise, if a file @code{project1} exists then it will be used;
9382 if neither file exists, this is an error.
9384 A warning is issued if the name of the project file does not match the
9385 name of the project; this check is case insensitive.
9387 Any source file that is an immediate source of the imported project can be
9388 used by the immediate sources of the importing project, and recursively. Thus
9389 if @code{A} imports @code{B}, and @code{B} imports @code{C}, the immediate
9390 sources of @code{A} may depend on the immediate sources of @code{C}, even if
9391 @code{A} does not import @code{C} explicitly. However, this is not recommended,
9392 because if and when @code{B} ceases to import @code{C}, some sources in
9393 @code{A} will no longer compile.
9395 A side effect of this capability is that cyclic dependences are not permitted:
9396 if @code{A} imports @code{B} (directly or indirectly) then @code{B} is not
9397 allowed to import @code{A}.
9400 @c *********************
9401 @c * Project Extension *
9402 @c *********************
9404 @node Project Extension
9405 @section Project Extension
9407 @noindent
9408 During development of a large system, it is sometimes necessary to use
9409 modified versions of some of the source files without changing the original
9410 sources. This can be achieved through a facility known as
9411 @emph{project extension}.
9413 @smallexample
9414    project Modified_Utilities extends "/baseline/utilities.gpr" is ...
9415 @end smallexample
9417 @noindent
9418 The project file for the project being extended (the @emph{parent}) is
9419 identified by the literal string that follows the reserved word @code{extends},
9420 which itself follows the name of the extending project (the @emph{child}).
9422 By default, a child project inherits all the sources of its parent.
9423 However, inherited sources can be overridden: a unit with the same name as one
9424 in the parent will hide the original unit.
9425 Inherited sources are considered to be sources (but not immediate sources)
9426 of the child project; see @ref{Project File Syntax}.
9428 An inherited source file retains any qualifiers specified in the parent project.
9430 For example if the project @code{Utilities} contains the specification and the
9431 body of an Ada package @code{Util_IO}, then the project
9432 @code{Modified_Utilities} can contain a new body for package @code{Util_IO}.
9433 The original body of @code{Util_IO} will not be considered in program builds.
9434 However, the package specification will still be found in the project
9435 @code{Utilities}.
9437 A child project can have only one parent but it may import any number of other
9438 projects.
9440 A project is not allowed to import directly or indirectly at the same time a
9441 child project and any of its ancestors.
9444 @c ****************************************
9445 @c * External References in Project Files *
9446 @c ****************************************
9448 @node  External References in Project Files
9449 @section External References in Project Files
9451 @noindent
9452 A project file may contain references to external variables; such references
9453 are called @emph{external references}.
9455 An external variable is either defined as part of the environment (an
9456 environment variable in Unix, for example) or else specified on the command
9457 line via the @option{-X@emph{vbl}=@emph{value}} qualifier. If both, then the
9458 command line value is used.
9460 An external reference is denoted by the built-in function
9461 @code{external}, which returns a string value.  This function has two forms:
9462 @itemize @bullet
9463 @item @code{external (external_variable_name)}
9464 @item @code{external (external_variable_name, default_value)}
9465 @end itemize
9467 @noindent
9468 Each parameter must be a string literal.  For example:
9470 @smallexample
9471    external ("USER")
9472    external ("OS", "Linux")
9473 @end smallexample
9475 @noindent
9476 In the form with one parameter, the function returns the value of
9477 the external variable given as parameter. If this name is not present in the
9478 environment, then the returned value is an empty string.
9480 In the form with two string parameters, the second parameter is
9481 the value returned when the variable given as the first parameter is not
9482 present in the environment. In the example above, if @code{"OS"} is not
9483 the name of an environment variable and is not passed on the command line,
9484 then the returned value will be @code{"Linux"}.
9486 An external reference may be part of a string expression or of a string
9487 list expression, to define variables or attributes.
9489 @smallexample
9490 @group
9491    type Mode_Type is ("Debug", "Release");
9492    Mode : Mode_Type := external ("MODE");
9493    case Mode is
9494      when "Debug" =>
9495         ...
9496 @end group
9497 @end smallexample
9500 @c *****************************
9501 @c * Packages in Project Files *
9502 @c *****************************
9504 @node  Packages in Project Files
9505 @section Packages in Project Files
9507 @noindent
9508 The @emph{package} is the project file feature that defines the settings for
9509 project-aware tools.
9510 For each such tool you can declare a corresponding package; the names for these
9511 packages are preset (see @ref{Packages}) but are not case sensitive.
9512 A package may contain variable declarations, attribute declarations, and case
9513 constructions.
9515 @smallexample
9516 @group
9517    project Proj is
9518       package Builder is  -- used by GNAT MAKE
9519          for Default_Qualifiers ("Ada") use ("-v", "-g");
9520       end Builder;
9521    end Proj;
9522 @end group
9523 @end smallexample
9525 @noindent
9526 A package declaration starts with the reserved word @code{package},
9527 followed by the package name (case insensitive), followed by the reserved word
9528 @code{is}. It ends with the reserved word @code{end}, followed by the package
9529 name, finally followed by a semi-colon.
9531 Most of the packages have an attribute @code{Default_Qualifiers}.
9532 This attribute is an associative array, and its value is a string list.
9533 The index of the associative array is the name of a programming language (case
9534 insensitive). This attribute indicates the qualifier or qualifiers to be used
9535 with the corresponding tool.
9537 Some packages also have another attribute, @code{Qualifiers}, an associative
9538 array whose value is a string list. The index is the name of a source file.
9539 This attribute indicates the qualifier or qualifiers to be used by the corresponding
9540 tool when dealing with this specific file.
9542 Further information on these qualifier-related attributes is found in
9543 @ref{Qualifiers and Project Files}.
9545 A package may be declared as a @emph{renaming} of another package; e.g., from
9546 the project file for an imported project.
9548 @smallexample
9549 @group
9550   with "/global/apex.gpr";
9551   project Example is
9552     package Naming renames Apex.Naming;
9553     ...
9554   end Example;
9555 @end group
9556 @end smallexample
9558 @noindent
9559 Packages that are renamed in other project files often come from project files
9560 that have no sources: they are just used as templates. Any modification in the
9561 template will be reflected automatically in all the project files that rename
9562 a package from the template.
9564 In addition to the tool-oriented packages, you can also declare a package
9565 named @code{Naming} to establish specialized source file naming conventions
9566 (see @ref{Naming Schemes}).
9569 @c ************************************
9570 @c * Variables from Imported Projects *
9571 @c ************************************
9573 @node Variables from Imported Projects
9574 @section Variables from Imported Projects
9576 @noindent
9577 An attribute or variable defined in an imported or parent project can
9578 be used in expressions in the importing / extending project.
9579 Such an attribute or variable is prefixed with the name of the project
9580 and (if relevant) the name of package where it is defined.
9582 @smallexample
9583 @group
9584   with "imported";
9585   project Main extends "base" is
9586      Var1 := Imported.Var;
9587      Var2 := Base.Var & ".new";
9588 @end group
9590 @group
9591      package Builder is
9592         for Default_Qualifiers ("Ada") use Imported.Builder.Ada_Qualifiers &
9593                          "/STYLE=GNAT" & "-v";
9594      end Builder;
9595 @end group
9597 @group
9598      package Compiler is
9599         for Default_Qualifiers ("Ada") use Base.Compiler.Ada_Qualifiers;
9600      end Compiler;
9601   end Main;
9602 @end group
9603 @end smallexample
9605 @noindent
9606 In this example:
9608 @itemize @bullet
9609 @item
9610 @code{Var1} is a copy of the variable @code{Var} defined in the project file
9611 @file{"imported.gpr"}
9612 @item
9613 the value of @code{Var2} is a copy of the value of variable @code{Var}
9614 defined in the project file @file{base.gpr}, concatenated with @code{".new"}
9615 @item
9616 attribute @code{Default_Qualifiers ("Ada")} in package @code{Builder}
9617 is a string list that includes in its value a copy of variable
9618 @code{Ada_Qualifiers} defined in the @code{Builder} package in project file
9619 @file{imported.gpr} plus two new elements: @option{"/STYLE=GNAT"} and @option{"-v"};
9620 @item
9621 attribute @code{Default_Qualifiers ("Ada")} in package @code{Compiler}
9622 is a copy of the variable @code{Ada_Qualifiers} defined in the @code{Compiler}
9623 package in project file @file{base.gpr}, the project being extended.
9624 @end itemize
9627 @c ******************
9628 @c * Naming Schemes *
9629 @c ******************
9631 @node  Naming Schemes
9632 @section Naming Schemes
9634 @noindent
9635 Sometimes an Ada software system is ported from a foreign compilation
9636 environment to GNAT, with file names that do not use the default GNAT
9637 conventions. Instead of changing all the file names (which for a variety of
9638 reasons might not be possible), you can define the relevant file naming scheme
9639 in the @code{Naming} package in your project file.  For example, the following
9640 package models the Apex file naming rules:
9642 @smallexample
9643 @group
9644   package Naming is
9645     for Casing                        use "lowercase";
9646     for Dot_Replacement               use ".";
9647     for Specification_Suffix ("Ada")  use ".1.ADA";
9648     for Implementation_Suffix ("Ada") use ".2.ADA";
9649   end Naming;
9650 @end group
9651 @end smallexample
9653 @noindent
9654 You can define the following attributes in package @code{Naming}:
9656 @table @code
9658 @item @var{Casing}
9659 This must be a string with one of the three values @code{"lowercase"},
9660 @code{"uppercase"} or @code{"mixedcase"}; these strings are case insensitive.
9662 @noindent
9663 If @var{Casing} is not specified, then the default is @code{"lowercase"}.
9665 @item @var{Dot_Replacement}
9666 This must be a string whose value satisfies the following conditions:
9668 @itemize @bullet
9669 @item It must not be empty
9670 @item It cannot start or end with an alphanumeric character
9671 @item It cannot be a single underscore
9672 @item It cannot start with an underscore followed by an alphanumeric
9673 @item It cannot contain a dot @code{'.'} except if it the entire string is @code{"."}
9674 @end itemize
9676 @noindent
9677 If @code{Dot_Replacement} is not specified, then the default is @code{"-"}.
9679 @item @var{Specification_Suffix}
9680 This is an associative array (indexed by the programming language name, case
9681 insensitive) whose value is a string that must satisfy the following
9682 conditions:
9684 @itemize @bullet
9685 @item It must not be empty
9686 @item It cannot start with an alphanumeric character
9687 @item It cannot start with an underscore followed by an alphanumeric character
9688 @end itemize
9689 @noindent
9690 If @code{Specification_Suffix ("Ada")} is not specified, then the default is
9691 @code{".ADS"}.
9693 @item @var{Implementation_Suffix}
9694 This is an associative array (indexed by the programming language name, case
9695 insensitive) whose value is a string that must satisfy the following
9696 conditions:
9698 @itemize @bullet
9699 @item It must not be empty
9700 @item It cannot start with an alphanumeric character
9701 @item It cannot start with an underscore followed by an alphanumeric character
9702 @item It cannot be a suffix of @code{Specification_Suffix}
9703 @end itemize
9704 @noindent
9705 If @code{Implementation_Suffix ("Ada")} is not specified, then the default is
9706 @code{".ADB"}.
9708 @item @var{Separate_Suffix}
9709 This must be a string whose value satisfies the same conditions as
9710 @code{Implementation_Suffix}.
9712 @noindent
9713 If @code{Separate_Suffix ("Ada")} is not specified, then it defaults to same
9714 value as @code{Implementation_Suffix ("Ada")}.
9716 @item @var{Specification}
9717 @noindent
9718 You can use the @code{Specification} attribute, an associative array, to define
9719 the source file name for an individual Ada compilation unit's spec. The array
9720 index must be a string literal that identifies the Ada unit (case insensitive).
9721 The value of this attribute must be a string that identifies the file that
9722 contains this unit's spec (case sensitive or insensitive depending on the
9723 operating system).
9725 @smallexample
9726    for Specification ("MyPack.MyChild") use "mypack.mychild.spec";
9727 @end smallexample
9729 @item @var{Implementation}
9731 You can use the @code{Implementation} attribute, an associative array, to
9732 define the source file name for an individual Ada compilation unit's body
9733 (possibly a subunit).  The array index must be a string literal that identifies
9734 the Ada unit (case insensitive).  The value of this attribute must be a string
9735 that identifies the file that contains this unit's body or subunit (case
9736 sensitive or insensitive depending on the operating system).
9738 @smallexample
9739    for Implementation ("MyPack.MyChild") use "mypack.mychild.body";
9740 @end smallexample
9741 @end table
9744 @c ********************
9745 @c * Library Projects *
9746 @c ********************
9748 @node Library Projects
9749 @section Library Projects
9751 @noindent
9752 @emph{Library projects} are projects whose object code is placed in a library.
9753 (Note that this facility is not yet supported on all platforms)
9755 To create a library project, you need to define in its project file
9756 two project-level attributes: @code{Library_Name} and @code{Library_Dir}.
9757 Additionally, you may define the library-related attributes
9758 @code{Library_Kind}, @code{Library_Version} and @code{Library_Elaboration}.
9760 The @code{Library_Name} attribute has a string value that must start with a
9761 letter and include only letters and digits.
9763 The @code{Library_Dir} attribute has a string value that designates the path
9764 (absolute or relative) of the directory where the library will reside.
9765 It must designate an existing directory, and this directory needs to be
9766 different from the project's object directory. It also needs to be writable.
9768 If both @code{Library_Name} and @code{Library_Dir} are specified and
9769 are legal, then the project file defines a library project.  The optional
9770 library-related attributes are checked only for such project files.
9772 The @code{Library_Kind} attribute has a string value that must be one of the
9773 following (case insensitive): @code{"static"}, @code{"dynamic"} or
9774 @code{"relocatable"}. If this attribute is not specified, the library is a
9775 static library. Otherwise, the library may be dynamic or relocatable.
9776 Depending on the operating system, there may or may not be a distinction
9777 between dynamic and relocatable libraries. For example, on Unix there is no
9778 such distinction.
9780 The @code{Library_Version} attribute has a string value whose interpretation
9781 is platform dependent. On Unix, it is used only for dynamic/relocatable
9782 libraries as the internal name of the library (the @code{"soname"}). If the
9783 library file name (built from the @code{Library_Name}) is different from the
9784 @code{Library_Version}, then the library file will be a symbolic link to the
9785 actual file whose name will be @code{Library_Version}.
9787 Example (on Unix):
9789 @smallexample
9790 @group
9791 project Plib is
9793    Version := "1";
9795    for Library_Dir use "lib_dir";
9796    for Library_Name use "dummy";
9797    for Library_Kind use "relocatable";
9798    for Library_Version use "libdummy.so." & Version;
9800 end Plib;
9801 @end group
9802 @end smallexample
9804 @noindent
9805 Directory @file{lib_dir} will contain the internal library file whose name
9806 will be @file{libdummy.so.1}, and @file{libdummy.so} will be a symbolic link to
9807 @file{libdummy.so.1}.
9809 When @command{GNAT MAKE} detects that a project file (not the main project file)
9810 is a library project file, it will check all immediate sources of the project
9811 and rebuild the library if any of the sources have been recompiled.
9812 All @file{ALI} files will also be copied from the object directory to the
9813 library directory. To build executables, @command{GNAT MAKE} will use the
9814 library rather than the individual object files.
9817 @c *************************************
9818 @c * Qualifiers Related to Project Files *
9819 @c *************************************
9820 @node Qualifiers Related to Project Files
9821 @section Qualifiers Related to Project Files
9823 @noindent
9824 The following qualifiers are used by GNAT tools that support project files:
9826 @table @code
9828 @item @option{-P@var{project}}
9829 Indicates the name of a project file. This project file will be parsed with
9830 the verbosity indicated by @option{-vP@emph{x}}, if any, and using the external
9831 references indicated by @option{-X} qualifiers, if any.
9833 @noindent
9834 There must be only one @option{-P} qualifier on the command line.
9836 @noindent
9837 Since the Project Manager parses the project file only after all the qualifiers
9838 on the command line are checked, the order of the qualifiers @option{-P},
9839 @option{-Vp@emph{x}} or @option{-X} is not significant.
9841 @item @option{-X@var{name=value}}
9842 Indicates that external variable @var{name} has the value @var{value}.
9843 The Project Manager will use this value for occurrences of
9844 @code{external(name)} when parsing the project file.
9846 @noindent
9847 If @var{name} or @var{value} includes a space, then @var{name=value} should be
9848 put between quotes.
9849 @smallexample
9850   -XOS=NT
9851   -X"user=John Doe"
9852 @end smallexample
9854 @noindent
9855 Several @option{-X} qualifiers can be used simultaneously.
9856 If several @option{-X} qualifiers specify the same @var{name}, only the last one
9857 is used.
9859 @noindent
9860 An external variable specified with a @option{-X} qualifier takes precedence
9861 over the value of the same name in the environment.
9863 @item @option{-vP@emph{x}}
9864 Indicates the verbosity of the parsing of GNAT project files.
9865 @option{-vP0} means Default (no output for syntactically correct project
9866 files);
9867 @option{-vP1} means Medium;
9868 @option{-vP2} means High.
9869 @noindent
9870 The default is Default.
9871 @noindent
9872 If several @option{-vP@emph{x}} qualifiers are present, only the last one is
9873 used.
9875 @end table
9878 @c **********************************
9879 @c * Tools Supporting Project Files *
9880 @c **********************************
9882 @node  Tools Supporting Project Files
9883 @section Tools Supporting Project Files
9885 @menu
9886 * GNAT MAKE and Project Files::
9887 * The GNAT Driver and Project Files::
9888 @end menu
9890 @node GNAT MAKE and Project Files
9891 @subsection GNAT MAKE and Project Files
9893 @noindent
9894 This section covers two topics related to @command{GNAT MAKE} and project files:
9895 defining qualifiers for @command{GNAT MAKE} and for the tools that it invokes;
9896 and the use of the @code{Main} attribute.
9898 @menu
9899 * Qualifiers and Project Files::
9900 * Project Files and Main Subprograms::
9901 @end menu
9903 @node Qualifiers and Project Files
9904 @subsubsection Qualifiers and Project Files
9906 @noindent
9907 For each of the packages @code{Builder}, @code{Compiler}, @code{Binder}, and
9908 @code{Linker}, you can specify a @code{Default_Qualifiers} attribute, a
9909 @code{Qualifiers} attribute, or both; as their names imply, these qualifier-related
9910 attributes affect which qualifiers are used for which files when
9911 @command{GNAT MAKE} is invoked.  As will be explained below, these
9912 package-contributed qualifiers precede the qualifiers passed on the
9913 @command{GNAT MAKE} command line.
9915 The @code{Default_Qualifiers} attribute is an associative array indexed by
9916 language name (case insensitive) and returning a string list.  For example:
9918 @smallexample
9919 @group
9920 package Compiler is
9921   for Default_Qualifiers ("Ada") use ("/STYLE=", "-v");
9922 end Compiler;
9923 @end group
9924 @end smallexample
9926 @noindent
9927 The @code{Qualifiers} attribute is also an associative array, indexed by a file
9928 name (which may or may not be case sensitive, depending on the operating
9929 system) and returning a string list.  For example:
9931 @smallexample
9932 @group
9933 package Builder is
9934    for Qualifiers ("MAIN1.ADB") use ("/OPTIMIZE=ALL");
9935    for Qualifiers ("MAIN2.ADB") use ("-g");
9936 end Builder;
9937 @end group
9938 @end smallexample
9940 @noindent
9941 For the @code{Builder} package, the file names should designate source files
9942 for main subprograms.  For the @code{Binder} and @code{Linker} packages, the
9943 file names should designate @file{ALI} or source files for main subprograms.
9944 In each case just the file name (without explicit extension) is acceptable.
9946 For each tool used in a program build (@command{GNAT MAKE}, the compiler, the
9947 binder, and the linker), its corresponding package @dfn{contributes} a set of
9948 qualifiers for each file on which the tool is invoked, based on the
9949 qualifier-related attributes defined in the package. In particular, the qualifiers
9950 that each of these packages contributes for a given file @var{f} comprise:
9952 @itemize @bullet
9953 @item
9954 the value of attribute @code{Qualifiers (@var{f})}, if it is specified in the
9955 package for the given file,
9956 @item
9957 otherwise, the value of @code{Default_Qualifiers ("Ada")}, if it is specified in
9958 the package.
9959 @end itemize
9961 @noindent
9962 If neither of these attributes is defined in the package, then the package does
9963 not contribute any qualifiers for the given file.
9965 When @command{GNAT MAKE} is invoked on a file, the qualifiers comprise two sets,
9966 in the following order: those contributed for the file by the @code{Builder}
9967 package; and the qualifiers passed on the command line.
9969 When @command{GNAT MAKE} invokes a tool (compiler, binder, linker) on a file,
9970 the qualifiers passed to the tool comprise three sets, in the following order:
9972 @enumerate
9973 @item
9974 the applicable qualifiers contributed for the file by the @code{Builder} package
9975 in the project file supplied on the command line;
9977 @item
9978 those contributed for the file by the package (in the relevant project file --
9979 see below) corresponding to the tool; and
9981 @item
9982 the applicable qualifiers passed on the command line.
9983 @end enumerate
9985 @noindent
9986 The term @emph{applicable qualifiers} reflects the fact that @command{GNAT MAKE}
9987 qualifiers may or may not be passed to individual tools, depending on the
9988 individual qualifier.
9990 @command{GNAT MAKE} may invoke the compiler on source files from different
9991 projects. The Project Manager will use the appropriate project file to
9992 determine the @code{Compiler} package for each source file being compiled.
9993 Likewise for the @code{Binder} and @code{Linker} packages.
9995 As an example, consider the following package in a project file:
9997 @smallexample
9998 @group
9999 project Proj1 is
10000    package Compiler is
10001       for Default_Qualifiers ("Ada") use ("-g");
10002       for Qualifiers ("A.ADB") use ("/OPTIMIZE=SOME");
10003       for Qualifiers ("B.ADB") use ("/OPTIMIZE=ALL", "/STYLE=");
10004    end Compiler;
10005 end Proj1;
10006 @end group
10007 @end smallexample
10009 @noindent
10010 If @command{GNAT MAKE} is invoked with this project file, and it needs to
10011 compile, say, the files @file{A.ADB}, @file{B.ADB}, and @file{C.ADB}, then
10012 @file{A.ADB} will be compiled with the qualifier @option{/OPTIMIZE=SOME}, @file{B.ADB}
10013 with qualifiers @option{/OPTIMIZE=ALL} and @option{/STYLE=}, and @file{C.ADB} with
10014 @option{-g}.
10016 Another example illustrates the ordering of the qualifiers contributed by
10017 different packages:
10019 @smallexample
10020 @group
10021 project Proj2 is
10022    package Builder is
10023       for Qualifiers ("MAIN.ADB") use ("-g", "/OPTIMIZE=SOME", "-f");
10024    end Builder;
10025 @end group
10027 @group
10028    package Compiler is
10029       for Qualifiers ("MAIN.ADB") use ("/OPTIMIZE=ALL");
10030    end Compiler;
10031 end Proj2;
10032 @end group
10033 @end smallexample
10035 @noindent
10036 If you issue the command:
10038 @smallexample
10039     GNAT MAKE -PProj2 /OPTIMIZE=NONE main
10040 @end smallexample
10042 @noindent
10043 then the compiler will be invoked on @file{MAIN.ADB} with the following sequence of qualifiers
10045 @smallexample
10046    -g /OPTIMIZE=SOME /OPTIMIZE=ALL /OPTIMIZE=NONE
10047 @end smallexample
10049 with the last @option{-O} qualifier having precedence over the earlier ones;
10050 several other qualifiers (such as @option{-c}) are added implicitly.
10052 The qualifiers @option{-g} and @option{/OPTIMIZE=SOME} are contributed by package
10053 @code{Builder},  @option{/OPTIMIZE=ALL} is contributed by the package @code{Compiler}
10054 and @option{/OPTIMIZE=NONE} comes from the command line.
10056 The @option{-g} qualifier will also be passed in the invocation of
10057 @command{GNAT LINK.}
10059 A final example illustrates qualifier contributions from packages in different
10060 project files:
10062 @smallexample
10063 @group
10064 project Proj3 is
10065    for Source_Files use ("PACK.ADS", "PACK.ADB");
10066    package Compiler is
10067       for Default_Qualifiers ("Ada") use ("/CHECKS=ASSERTIONS");
10068    end Compiler;
10069 end Proj3;
10070 @end group
10072 @group
10073 with "Proj3";
10074 project Proj4 is
10075    for Source_Files use ("FOO_MAIN.ADB", "BAR_MAIN.ADB");
10076    package Builder is
10077       for Qualifiers ("FOO_MAIN.ADB") use ("-s", "-g");
10078    end Builder;
10079 end Proj4;
10080 @end group
10082 @group
10083 -- Ada source file:
10084 with Pack;
10085 procedure Foo_Main is
10086    ...
10087 end Foo_Main;
10088 @end group
10089 @end smallexample
10091 If the command is
10092 @smallexample
10093 GNAT MAKE -PProj4 FOO_MAIN.ADB /COMPILER_QUALIFIERS /CHECKS=OVERFLOW
10094 @end smallexample
10096 @noindent
10097 then the qualifiers passed to the compiler for @file{FOO_MAIN.ADB} are
10098 @option{-g} (contributed by the package @code{Proj4.Builder}) and
10099 @option{/CHECKS=OVERFLOW} (passed on the command line).
10100 When the imported package @code{Pack} is compiled, the qualifiers used are
10101 @option{-g} from @code{Proj4.Builder}, @option{/CHECKS=ASSERTIONS} (contributed from
10102 package @code{Proj3.Compiler}, and @option{/CHECKS=OVERFLOW} from the command line.
10105 @node Project Files and Main Subprograms
10106 @subsubsection Project Files and Main Subprograms
10108 @noindent
10109 When using a project file, you can invoke @command{GNAT MAKE}
10110 with several main subprograms, by specifying their source files on the command
10111 line.  Each of these needs to be an immediate source file of the project.
10113 @smallexample
10114     GNAT MAKE -Pprj main1 main2 main3
10115 @end smallexample
10117 @noindent
10118 When using a project file, you can also invoke @command{GNAT MAKE} without
10119 explicitly specifying any main, and the effect depends on whether you have
10120 defined the @code{Main} attribute.  This attribute has a string list value,
10121 where each element in the list is the name of a source file (the file
10122 extension is optional) containing a main subprogram.
10124 If the @code{Main} attribute is defined in a project file as a non-empty
10125 string list and the qualifier @option{-u} is not used on the command line, then
10126 invoking @command{GNAT MAKE} with this project file but without any main on the
10127 command line is equivalent to invoking @command{GNAT MAKE} with all the file
10128 names in the @code{Main} attribute on the command line.
10130 Example:
10131 @smallexample
10132 @group
10133    project Prj is
10134       for Main use ("main1", "main2", "main3");
10135    end Prj;
10136 @end group
10137 @end smallexample
10139 @noindent
10140 With this project file, @code{"GNAT MAKE -Pprj"} is equivalent to
10141 @code{"GNAT MAKE -Pprj main1 main2 main3"}.
10143 When the project attribute @code{Main} is not specified, or is specified
10144 as an empty string list, or when the qualifier @option{-u} is used on the command
10145 line, then invoking @command{GNAT MAKE} with no main on the command line will
10146 result in all immediate sources of the project file being checked, and
10147 potentially recompiled. Depending on the presence of the qualifier @option{-u},
10148 sources from other project files on which the immediate sources of the main
10149 project file depend are also checked and potentially recompiled. In other
10150 words, the @option{-u} qualifier is applied to all of the immediate sources of themain project file.
10153 @node The GNAT Driver and Project Files
10154 @subsection The GNAT Driver and Project Files
10156 @noindent
10157 A number of GNAT tools, other than @command{GNAT MAKE} are project-aware:
10158 @command{GNAT BIND}, @command{GNAT FIND}, @command{GNAT LINK}, @command{GNAT LIST}
10159 and @command{GNAT XREF}. However, none of these tools can be invoked directly
10160 with a project file qualifier (@code{-P}). They need to be invoke through the
10161 @command{gnat} driver.
10163 The @command{gnat} driver is a front-end that accepts a number of commands and
10164 call the corresponding tool. It has been designed initially for VMS to convert
10165 VMS style qualifiers to Unix style qualifiers, but it is now available to all
10166 the GNAT supported platforms.
10168 On non VMS platforms, the @command{gnat} driver accepts the following commands
10169 (case insensitive):
10171 @itemize @bullet
10172 @item
10173 BIND to invoke @command{GNAT BIND}
10174 @item
10175 CHOP to invoke @command{GNAT CHOP}
10176 @item
10177 COMP or COMPILE to invoke the compiler
10178 @item
10179 ELIM to invoke @command{GNAT ELIM}
10180 @item
10181 FIND to invoke @command{GNAT FIND}
10182 @item
10183 KR or KRUNCH to invoke @command{GNAT KRUNCH}
10184 @item
10185 LINK to invoke @command{GNAT LINK}
10186 @item
10187 LS or LIST to invoke @command{GNAT LIST}
10188 @item
10189 MAKE to invoke @command{GNAT MAKE}
10190 @item
10191 NAME to invoke @command{gnatname}
10192 @item
10193 PREP or PREPROCESS to invoke @command{GNAT PREPROCESS}
10194 @item
10195 PSTA or STANDARD to invoke @command{GNAT STANDARD}
10196 @item
10197 STUB to invoke @command{GNAT STUB}
10198 @item
10199 XREF to invoke @command{GNAT XREF}
10200 @end itemize
10202 @noindent
10203 Note that the compiler is invoked using the command @command{GNAT MAKE -f -u}.
10205 @noindent
10206 Following the command, you may put qualifiers and arguments for the invoked
10207 tool.
10209 @smallexample
10210   gnat bind -C MAIN.ALI
10211   gnat ls -a main
10212   gnat chop foo.txt
10213 @end smallexample
10215 @noindent
10216 In addition, for command BIND, FIND, LS or LIST, LINK and XREF, the project
10217 file related qualifiers (@code{-P}, @code{-X} and @code{-vPx}) may be used in
10218 addition to the qualifiers of the invoking tool.
10220 @noindent
10221 For each of these command, there is possibly a package in the main project that
10222 corresponds to the invoked tool.
10224 @itemize @bullet
10225 @item
10226 package @code{Binder} for command BIND (invoking @code{GNAT BIND})
10228 @item
10229 package @code{Finder} for command FIND (invoking @code{GNAT FIND})
10231 @item
10232 package @code{GNAT LIST} for command LS or LIST (invoking @code{GNAT LIST})
10234 @item
10235 package @code{Linker} for command LINK (invoking @code{GNAT LINK})
10237 @item
10238 package @code{Cross_Reference} for command XREF (invoking @code{GNAT LINK})
10240 @end itemize
10242 @noindent
10243 Package @code{GNAT LIST} has a unique attribute @code{Qualifiers}, a simple variable
10244 with a string list value. It contains qualifiers for the invocation of
10245 @code{GNAT LIST}.
10247 @smallexample
10248 @group
10249 project Proj1 is
10250    package GNAT LIST is
10251       for Qualifiers use ("-a", "-v");
10252    end GNAT LIST;
10253 end Proj1;
10254 @end group
10255 @end smallexample
10257 @noindent
10258 All other packages contains a qualifier @code{Default_Qualifiers}, an associative
10259 array, indexed by the programming language (case insensitive) and having a
10260 string list value. @code{Default_Qualifiers ("Ada")} contains the qualifiers for
10261 the invocation of the tool corresponding to the package.
10263 @smallexample
10264 @group
10265 project Proj is
10267    for Source_Dirs use ("./**");
10269    package GNAT LIST is
10270       for Qualifiers use ("-a", "-v");
10271    end GNAT LIST;
10272 @end group
10273 @group
10275    package Binder is
10276       for Default_Qualifiers ("Ada") use ("-C", "-e");
10277    end Binder;
10278 @end group
10279 @group
10281    package Linker is
10282       for Default_Qualifiers ("Ada") use ("-C");
10283    end Linker;
10284 @end group
10285 @group
10287    package Finder is
10288       for Default_Qualifiers ("Ada") use ("-a", "-f");
10289    end Finder;
10290 @end group
10291 @group
10293    package Cross_Reference is
10294       for Default_Qualifiers ("Ada") use ("-a", "-f", "-d", "-u");
10295    end Cross_Reference;
10296 end Proj;
10297 @end group
10298 @end smallexample
10300 @noindent
10301 With the above project file, commands such as
10303 @smallexample
10304    gnat ls -Pproj main
10305    gnat xref -Pproj main
10306    gnat bind -Pproj MAIN.ALI
10307 @end smallexample
10309 @noindent
10310 will set up the environment properly and invoke the tool with the qualifiers
10311 found in the package corresponding to the tool.
10316 @node An Extended Example
10317 @section An Extended Example
10319 @noindent
10320 Suppose that we have two programs, @var{prog1} and @var{prog2}, with the sources
10321 in the respective directories. We would like to build them with a single
10322 @command{GNAT MAKE} command, and we would like to place their object files into
10323 @file{.build} subdirectories of the source directories. Furthermore, we would
10324 like to have to have two separate subdirectories in @file{.build}  --
10325 @file{release} and @file{debug} -- which will contain the object files compiled with
10326 different set of compilation flags.
10328 In other words, we have the following structure:
10330 @smallexample
10331 @group
10332    main
10333      |- prog1
10334      |    |- .build
10335      |         | debug
10336      |         | release
10337      |- prog2
10338           |- .build
10339                | debug
10340                | release
10341 @end group
10342 @end smallexample
10344 @noindent
10345 Here are the project files that we need to create in a directory @file{main}
10346 to maintain this structure:
10348 @enumerate
10350 @item We create a @code{Common} project with a package @code{Compiler} that
10351 specifies the compilation qualifiers:
10353 @smallexample
10354 File "common.gpr":
10355 @group
10356 @b{project} Common @b{is}
10358    @b{for} Source_Dirs @b{use} (); -- No source files
10359 @end group
10361 @group
10362    @b{type} Build_Type @b{is} ("release", "debug");
10363    Build : Build_Type := External ("BUILD", "debug");
10364 @end group
10365 @group
10366    @b{package} Compiler @b{is}
10367       @b{case} Build @b{is}
10368          @b{when} "release" =>
10369            @b{for} Default_Qualifiers ("Ada") @b{use} ("/OPTIMIZE=ALL");
10370          @b{when} "debug"   =>
10371            @b{for} Default_Qualifiers ("Ada") @b{use} ("-g");
10372       @b{end case};
10373    @b{end} Compiler;
10375 @b{end} Common;
10376 @end group
10377 @end smallexample
10379 @item We create separate projects for the two programs:
10381 @smallexample
10382 @group
10383 File "prog1.gpr":
10385 @b{with} "common";
10386 @b{project} Prog1 @b{is}
10388     @b{for} Source_Dirs @b{use} ("prog1");
10389     @b{for} Object_Dir  @b{use} "prog1/.build/" & Common.Build;
10391     @b{package} Compiler @b{renames} Common.Compiler;
10393 @b{end} Prog1;
10394 @end group
10395 @end smallexample
10397 @smallexample
10398 @group
10399 File "prog2.gpr":
10401 @b{with} "common";
10402 @b{project} Prog2 @b{is}
10404     @b{for} Source_Dirs @b{use} ("prog2");
10405     @b{for} Object_Dir  @b{use} "prog2/.build/" & Common.Build;
10407     @b{package} Compiler @b{renames} Common.Compiler;
10409 @end group
10410 @b{end} Prog2;
10411 @end smallexample
10413 @item We create a wrapping project @var{Main}:
10415 @smallexample
10416 @group
10417 File "main.gpr":
10419 @b{with} "common";
10420 @b{with} "prog1";
10421 @b{with} "prog2";
10422 @b{project} Main @b{is}
10424    @b{package} Compiler @b{renames} Common.Compiler;
10426 @b{end} Main;
10427 @end group
10428 @end smallexample
10430 @item Finally we need to create a dummy procedure that @code{with}s (either
10431 explicitly or implicitly) all the sources of our two programs.
10433 @end enumerate
10435 @noindent
10436 Now we can build the programs using the command
10438 @smallexample
10439    GNAT MAKE -Pmain dummy
10440 @end smallexample
10442 @noindent
10443 for the Debug mode, or
10445 @smallexample
10446    GNAT MAKE -Pmain -XBUILD=release
10447 @end smallexample
10449 @noindent
10450 for the Release mode.
10453 @c ********************************
10454 @c * Project File Complete Syntax *
10455 @c ********************************
10457 @node Project File Complete Syntax
10458 @section Project File Complete Syntax
10460 @smallexample
10461 project ::=
10462   context_clause project_declaration
10464 context_clause ::=
10465   @{with_clause@}
10467 with_clause ::=
10468   @b{with} literal_string @{ , literal_string @} ;
10470 project_declaration ::=
10471   @b{project} <project_>simple_name [ @b{extends} literal_string ] @b{is}
10472     @{declarative_item@}
10473   @b{end} <project_>simple_name;
10475 declarative_item ::=
10476   package_declaration |
10477   typed_string_declaration |
10478   other_declarative_item
10480 package_declaration ::=
10481   @b{package} <package_>simple_name package_completion
10483 package_completion ::=
10484   package_body | package_renaming
10486 package body ::=
10487   @b{is}
10488     @{other_declarative_item@}
10489   @b{end} <package_>simple_name ;
10491 package_renaming ::==
10492   @b{renames} <project_>simple_name.<package_>simple_name ;
10494 typed_string_declaration ::=
10495   @b{type} <typed_string_>_simple_name @b{is}
10496    ( literal_string @{, literal_string@} );
10498 other_declarative_item ::=
10499   attribute_declaration |
10500   typed_variable_declaration |
10501   variable_declaration |
10502   case_construction
10504 attribute_declaration ::=
10505   @b{for} attribute @b{use} expression ;
10507 attribute ::=
10508   <simple_attribute_>simple_name |
10509   <associative_array_attribute_>simple_name ( literal_string )
10511 typed_variable_declaration ::=
10512   <typed_variable_>simple_name : <typed_string_>name :=  string_expression ;
10514 variable_declaration ::=
10515   <variable_>simple_name := expression;
10517 expression ::=
10518   term @{& term@}
10520 term ::=
10521   literal_string |
10522   string_list |
10523   <variable_>name |
10524   external_value |
10525   attribute_reference
10527 literal_string ::=
10528   (same as Ada)
10530 string_list ::=
10531   ( <string_>expression @{ , <string_>expression @} )
10533 external_value ::=
10534   @b{external} ( literal_string [, literal_string] )
10536 attribute_reference ::=
10537   attribute_parent ' <simple_attribute_>simple_name [ ( literal_string ) ]
10539 attribute_parent ::=
10540   @b{project} |
10541   <project_or_package>simple_name |
10542   <project_>simple_name . <package_>simple_name
10544 case_construction ::=
10545   @b{case} <typed_variable_>name @b{is}
10546     @{case_item@}
10547   @b{end case} ;
10549 case_item ::=
10550   @b{when} discrete_choice_list => @{case_construction | attribute_declaration@}
10552 discrete_choice_list ::=
10553   literal_string @{| literal_string@}
10555 name ::=
10556   simple_name @{. simple_name@}
10558 simple_name ::=
10559   identifier (same as Ada)
10561 @end smallexample
10564 @node Elaboration Order Handling in GNAT
10565 @chapter Elaboration Order Handling in GNAT
10566 @cindex Order of elaboration
10567 @cindex Elaboration control
10569 @menu
10570 * Elaboration Code in Ada 95::
10571 * Checking the Elaboration Order in Ada 95::
10572 * Controlling the Elaboration Order in Ada 95::
10573 * Controlling Elaboration in GNAT - Internal Calls::
10574 * Controlling Elaboration in GNAT - External Calls::
10575 * Default Behavior in GNAT - Ensuring Safety::
10576 * Elaboration Issues for Library Tasks::
10577 * Mixing Elaboration Models::
10578 * What to Do If the Default Elaboration Behavior Fails::
10579 * Elaboration for Access-to-Subprogram Values::
10580 * Summary of Procedures for Elaboration Control::
10581 * Other Elaboration Order Considerations::
10582 @end menu
10584 @noindent
10585 This chapter describes the handling of elaboration code in Ada 95 and
10586 in GNAT, and discusses how the order of elaboration of program units can
10587 be controlled in GNAT, either automatically or with explicit programming
10588 features.
10590 @node Elaboration Code in Ada 95
10591 @section Elaboration Code in Ada 95
10593 @noindent
10594 Ada 95 provides rather general mechanisms for executing code at elaboration
10595 time, that is to say before the main program starts executing. Such code arises
10596 in three contexts:
10598 @table @asis
10599 @item Initializers for variables.
10600 Variables declared at the library level, in package specs or bodies, can
10601 require initialization that is performed at elaboration time, as in:
10602 @smallexample
10603 @cartouche
10604 Sqrt_Half : Float := Sqrt (0.5);
10605 @end cartouche
10606 @end smallexample
10608 @item Package initialization code
10609 Code in a @code{BEGIN-END} section at the outer level of a package body is
10610 executed as part of the package body elaboration code.
10612 @item Library level task allocators
10613 Tasks that are declared using task allocators at the library level
10614 start executing immediately and hence can execute at elaboration time.
10615 @end table
10617 @noindent
10618 Subprogram calls are possible in any of these contexts, which means that
10619 any arbitrary part of the program may be executed as part of the elaboration
10620 code. It is even possible to write a program which does all its work at
10621 elaboration time, with a null main program, although stylistically this
10622 would usually be considered an inappropriate way to structure
10623 a program.
10625 An important concern arises in the context of elaboration code:
10626 we have to be sure that it is executed in an appropriate order. What we
10627 have is a series of elaboration code sections, potentially one section
10628 for each unit in the program. It is important that these execute
10629 in the correct order. Correctness here means that, taking the above
10630 example of the declaration of @code{Sqrt_Half},
10631 if some other piece of
10632 elaboration code references @code{Sqrt_Half},
10633 then it must run after the
10634 section of elaboration code that contains the declaration of
10635 @code{Sqrt_Half}.
10637 There would never be any order of elaboration problem if we made a rule
10638 that whenever you @code{with} a unit, you must elaborate both the spec and body
10639 of that unit before elaborating the unit doing the @code{with}'ing:
10641 @smallexample
10642 @group
10643 @cartouche
10644 @b{with} Unit_1;
10645 @b{package} Unit_2 @b{is} ...
10646 @end cartouche
10647 @end group
10648 @end smallexample
10650 @noindent
10651 would require that both the body and spec of @code{Unit_1} be elaborated
10652 before the spec of @code{Unit_2}. However, a rule like that would be far too
10653 restrictive. In particular, it would make it impossible to have routines
10654 in separate packages that were mutually recursive.
10656 You might think that a clever enough compiler could look at the actual
10657 elaboration code and determine an appropriate correct order of elaboration,
10658 but in the general case, this is not possible. Consider the following
10659 example.
10661 In the body of @code{Unit_1}, we have a procedure @code{Func_1}
10662 that references
10663 the variable @code{Sqrt_1}, which is declared in the elaboration code
10664 of the body of @code{Unit_1}:
10666 @smallexample
10667 @cartouche
10668 Sqrt_1 : Float := Sqrt (0.1);
10669 @end cartouche
10670 @end smallexample
10672 @noindent
10673 The elaboration code of the body of @code{Unit_1} also contains:
10675 @smallexample
10676 @group
10677 @cartouche
10678 @b{if} expression_1 = 1 @b{then}
10679    Q := Unit_2.Func_2;
10680 @b{end if};
10681 @end cartouche
10682 @end group
10683 @end smallexample
10685 @noindent
10686 @code{Unit_2} is exactly parallel,
10687 it has a procedure @code{Func_2} that references
10688 the variable @code{Sqrt_2}, which is declared in the elaboration code of
10689 the body @code{Unit_2}:
10691 @smallexample
10692 @cartouche
10693 Sqrt_2 : Float := Sqrt (0.1);
10694 @end cartouche
10695 @end smallexample
10697 @noindent
10698 The elaboration code of the body of @code{Unit_2} also contains:
10700 @smallexample
10701 @group
10702 @cartouche
10703 @b{if} expression_2 = 2 @b{then}
10704    Q := Unit_1.Func_1;
10705 @b{end if};
10706 @end cartouche
10707 @end group
10708 @end smallexample
10710 @noindent
10711 Now the question is, which of the following orders of elaboration is
10712 acceptable:
10714 @smallexample
10715 @group
10716 Spec of Unit_1
10717 Spec of Unit_2
10718 Body of Unit_1
10719 Body of Unit_2
10720 @end group
10721 @end smallexample
10723 @noindent
10726 @smallexample
10727 @group
10728 Spec of Unit_2
10729 Spec of Unit_1
10730 Body of Unit_2
10731 Body of Unit_1
10732 @end group
10733 @end smallexample
10735 @noindent
10736 If you carefully analyze the flow here, you will see that you cannot tell
10737 at compile time the answer to this question.
10738 If @code{expression_1} is not equal to 1,
10739 and @code{expression_2} is not equal to 2,
10740 then either order is acceptable, because neither of the function calls is
10741 executed. If both tests evaluate to true, then neither order is acceptable
10742 and in fact there is no correct order.
10744 If one of the two expressions is true, and the other is false, then one
10745 of the above orders is correct, and the other is incorrect. For example,
10746 if @code{expression_1} = 1 and @code{expression_2} /= 2,
10747 then the call to @code{Func_2}
10748 will occur, but not the call to @code{Func_1.}
10749 This means that it is essential
10750 to elaborate the body of @code{Unit_1} before
10751 the body of @code{Unit_2}, so the first
10752 order of elaboration is correct and the second is wrong.
10754 By making @code{expression_1} and @code{expression_2}
10755 depend on input data, or perhaps
10756 the time of day, we can make it impossible for the compiler or binder
10757 to figure out which of these expressions will be true, and hence it
10758 is impossible to guarantee a safe order of elaboration at run time.
10760 @node Checking the Elaboration Order in Ada 95
10761 @section Checking the Elaboration Order in Ada 95
10763 @noindent
10764 In some languages that involve the same kind of elaboration problems,
10765 e.g. Java and C++, the programmer is expected to worry about these
10766 ordering problems himself, and it is common to
10767 write a program in which an incorrect elaboration order  gives
10768 surprising results, because it references variables before they
10769 are initialized.
10770 Ada 95 is designed to be a safe language, and a programmer-beware approach is
10771 clearly not sufficient. Consequently, the language provides three lines
10772 of defense:
10774 @table @asis
10775 @item Standard rules
10776 Some standard rules restrict the possible choice of elaboration
10777 order. In particular, if you @code{with} a unit, then its spec is always
10778 elaborated before the unit doing the @code{with}. Similarly, a parent
10779 spec is always elaborated before the child spec, and finally
10780 a spec is always elaborated before its corresponding body.
10782 @item Dynamic elaboration checks
10783 @cindex Elaboration checks
10784 @cindex Checks, elaboration
10785 Dynamic checks are made at run time, so that if some entity is accessed
10786 before it is elaborated (typically  by means of a subprogram call)
10787 then the exception (@code{Program_Error}) is raised.
10789 @item Elaboration control
10790 Facilities are provided for the programmer to specify the desired order
10791 of elaboration.
10792 @end table
10794 Let's look at these facilities in more detail. First, the rules for
10795 dynamic checking. One possible rule would be simply to say that the
10796 exception is raised if you access a variable which has not yet been
10797 elaborated. The trouble with this approach is that it could require
10798 expensive checks on every variable reference. Instead Ada 95 has two
10799 rules which are a little more restrictive, but easier to check, and
10800 easier to state:
10802 @table @asis
10803 @item Restrictions on calls
10804 A subprogram can only be called at elaboration time if its body
10805 has been elaborated. The rules for elaboration given above guarantee
10806 that the spec of the subprogram has been elaborated before the
10807 call, but not the body. If this rule is violated, then the
10808 exception @code{Program_Error} is raised.
10810 @item Restrictions on instantiations
10811 A generic unit can only be instantiated if the body of the generic
10812 unit has been elaborated. Again, the rules for elaboration given above
10813 guarantee that the spec of the generic unit has been elaborated
10814 before the instantiation, but not the body. If this rule is
10815 violated, then the exception @code{Program_Error} is raised.
10816 @end table
10818 @noindent
10819 The idea is that if the body has been elaborated, then any variables
10820 it references must have been elaborated; by checking for the body being
10821 elaborated we guarantee that none of its references causes any
10822 trouble. As we noted above, this is a little too restrictive, because a
10823 subprogram that has no non-local references in its body may in fact be safe
10824 to call. However, it really would be unsafe to rely on this, because
10825 it would mean that the caller was aware of details of the implementation
10826 in the body. This goes against the basic tenets of Ada.
10828 A plausible implementation can be described as follows.
10829 A Boolean variable is associated with each subprogram
10830 and each generic unit. This variable is initialized to False, and is set to
10831 True at the point body is elaborated. Every call or instantiation checks the
10832 variable, and raises @code{Program_Error} if the variable is False.
10834 Note that one might think that it would be good enough to have one Boolean
10835 variable for each package, but that would not deal with cases of trying
10836 to call a body in the same package as the call
10837 that has not been elaborated yet.
10838 Of course a compiler may be able to do enough analysis to optimize away
10839 some of the Boolean variables as unnecessary, and @code{GNAT} indeed
10840 does such optimizations, but still the easiest conceptual model is to
10841 think of there being one variable per subprogram.
10843 @node Controlling the Elaboration Order in Ada 95
10844 @section Controlling the Elaboration Order in Ada 95
10846 @noindent
10847 In the previous section we discussed the rules in Ada 95 which ensure
10848 that @code{Program_Error} is raised if an incorrect elaboration order is
10849 chosen. This prevents erroneous executions, but we need mechanisms to
10850 specify a correct execution and avoid the exception altogether.
10851 To achieve this, Ada 95 provides a number of features for controlling
10852 the order of elaboration. We discuss these features in this section.
10854 First, there are several ways of indicating to the compiler that a given
10855 unit has no elaboration problems:
10857 @table @asis
10858 @item packages that do not require a body
10859 In Ada 95, a library package that does not require a body does not permit
10860 a body. This means that if we have a such a package, as in:
10862 @smallexample
10863 @group
10864 @cartouche
10865 @b{package} Definitions @b{is}
10866    @b{generic}
10867       @b{type} m @b{is new} integer;
10868    @b{package} Subp @b{is}
10869       @b{type} a @b{is array} (1 .. 10) @b{of} m;
10870       @b{type} b @b{is array} (1 .. 20) @b{of} m;
10871    @b{end} Subp;
10872 @b{end} Definitions;
10873 @end cartouche
10874 @end group
10875 @end smallexample
10877 @noindent
10878 A package that @code{with}'s @code{Definitions} may safely instantiate
10879 @code{Definitions.Subp} because the compiler can determine that there
10880 definitely is no package body to worry about in this case
10882 @item pragma Pure
10883 @cindex pragma Pure
10884 @findex Pure
10885 Places sufficient restrictions on a unit to guarantee that
10886 no call to any subprogram in the unit can result in an
10887 elaboration problem. This means that the compiler does not need
10888 to worry about the point of elaboration of such units, and in
10889 particular, does not need to check any calls to any subprograms
10890 in this unit.
10892 @item pragma Preelaborate
10893 @findex Preelaborate
10894 @cindex pragma Preelaborate
10895 This pragma places slightly less stringent restrictions on a unit than
10896 does pragma Pure,
10897 but these restrictions are still sufficient to ensure that there
10898 are no elaboration problems with any calls to the unit.
10900 @item pragma Elaborate_Body
10901 @findex Elaborate_Body
10902 @cindex pragma Elaborate_Body
10903 This pragma requires that the body of a unit be elaborated immediately
10904 after its spec. Suppose a unit @code{A} has such a pragma,
10905 and unit @code{B} does
10906 a @code{with} of unit @code{A}. Recall that the standard rules require
10907 the spec of unit @code{A}
10908 to be elaborated before the @code{with}'ing unit; given the pragma in
10909 @code{A}, we also know that the body of @code{A}
10910 will be elaborated before @code{B}, so
10911 that calls to @code{A} are safe and do not need a check.
10912 @end table
10914 @noindent
10915 Note that,
10916 unlike pragma @code{Pure} and pragma @code{Preelaborate},
10917 the use of
10918 @code{Elaborate_Body} does not guarantee that the program is
10919 free of elaboration problems, because it may not be possible
10920 to satisfy the requested elaboration order.
10921 Let's go back to the example with @code{Unit_1} and @code{Unit_2}.
10922 If a programmer
10923 marks @code{Unit_1} as @code{Elaborate_Body},
10924 and not @code{Unit_2,} then the order of
10925 elaboration will be:
10927 @smallexample
10928 @group
10929 Spec of Unit_2
10930 Spec of Unit_1
10931 Body of Unit_1
10932 Body of Unit_2
10933 @end group
10934 @end smallexample
10936 @noindent
10937 Now that means that the call to @code{Func_1} in @code{Unit_2}
10938 need not be checked,
10939 it must be safe. But the call to @code{Func_2} in
10940 @code{Unit_1} may still fail if
10941 @code{Expression_1} is equal to 1,
10942 and the programmer must still take
10943 responsibility for this not being the case.
10945 If all units carry a pragma @code{Elaborate_Body}, then all problems are
10946 eliminated, except for calls entirely within a body, which are
10947 in any case fully under programmer control. However, using the pragma
10948 everywhere is not always possible.
10949 In particular, for our @code{Unit_1}/@code{Unit_2} example, if
10950 we marked both of them as having pragma @code{Elaborate_Body}, then
10951 clearly there would be no possible elaboration order.
10953 The above pragmas allow a server to guarantee safe use by clients, and
10954 clearly this is the preferable approach. Consequently a good rule in
10955 Ada 95 is to mark units as @code{Pure} or @code{Preelaborate} if possible,
10956 and if this is not possible,
10957 mark them as @code{Elaborate_Body} if possible.
10958 As we have seen, there are situations where neither of these
10959 three pragmas can be used.
10960 So we also provide methods for clients to control the
10961 order of elaboration of the servers on which they depend:
10963 @table @asis
10964 @item pragma Elaborate (unit)
10965 @findex Elaborate
10966 @cindex pragma Elaborate
10967 This pragma is placed in the context clause, after a @code{with} clause,
10968 and it requires that the body of the named unit be elaborated before
10969 the unit in which the pragma occurs. The idea is to use this pragma
10970 if the current unit calls at elaboration time, directly or indirectly,
10971 some subprogram in the named unit.
10973 @item pragma Elaborate_All (unit)
10974 @findex Elaborate_All
10975 @cindex pragma Elaborate_All
10976 This is a stronger version of the Elaborate pragma. Consider the
10977 following example:
10979 @smallexample
10980 Unit A @code{with}'s unit B and calls B.Func in elab code
10981 Unit B @code{with}'s unit C, and B.Func calls C.Func
10982 @end smallexample
10984 @noindent
10985 Now if we put a pragma @code{Elaborate (B)}
10986 in unit @code{A}, this ensures that the
10987 body of @code{B} is elaborated before the call, but not the
10988 body of @code{C}, so
10989 the call to @code{C.Func} could still cause @code{Program_Error} to
10990 be raised.
10992 The effect of a pragma @code{Elaborate_All} is stronger, it requires
10993 not only that the body of the named unit be elaborated before the
10994 unit doing the @code{with}, but also the bodies of all units that the
10995 named unit uses, following @code{with} links transitively. For example,
10996 if we put a pragma @code{Elaborate_All (B)} in unit @code{A},
10997 then it requires
10998 not only that the body of @code{B} be elaborated before @code{A},
10999 but also the
11000 body of @code{C}, because @code{B} @code{with}'s @code{C}.
11001 @end table
11003 @noindent
11004 We are now in a position to give a usage rule in Ada 95 for avoiding
11005 elaboration problems, at least if dynamic dispatching and access to
11006 subprogram values are not used. We will handle these cases separately
11007 later.
11009 The rule is simple. If a unit has elaboration code that can directly or
11010 indirectly make a call to a subprogram in a @code{with}'ed unit, or instantiate
11011 a generic unit in a @code{with}'ed unit,
11012 then if the @code{with}'ed unit does not have
11013 pragma @code{Pure} or @code{Preelaborate}, then the client should have
11014 a pragma @code{Elaborate_All}
11015 for the @code{with}'ed unit. By following this rule a client is
11016 assured that calls can be made without risk of an exception.
11017 If this rule is not followed, then a program may be in one of four
11018 states:
11020 @table @asis
11021 @item No order exists
11022 No order of elaboration exists which follows the rules, taking into
11023 account any @code{Elaborate}, @code{Elaborate_All},
11024 or @code{Elaborate_Body} pragmas. In
11025 this case, an Ada 95 compiler must diagnose the situation at bind
11026 time, and refuse to build an executable program.
11028 @item One or more orders exist, all incorrect
11029 One or more acceptable elaboration orders exists, and all of them
11030 generate an elaboration order problem. In this case, the binder
11031 can build an executable program, but @code{Program_Error} will be raised
11032 when the program is run.
11034 @item Several orders exist, some right, some incorrect
11035 One or more acceptable elaboration orders exists, and some of them
11036 work, and some do not. The programmer has not controlled
11037 the order of elaboration, so the binder may or may not pick one of
11038 the correct orders, and the program may or may not raise an
11039 exception when it is run. This is the worst case, because it means
11040 that the program may fail when moved to another compiler, or even
11041 another version of the same compiler.
11043 @item One or more orders exists, all correct
11044 One ore more acceptable elaboration orders exist, and all of them
11045 work. In this case the program runs successfully. This state of
11046 affairs can be guaranteed by following the rule we gave above, but
11047 may be true even if the rule is not followed.
11048 @end table
11050 @noindent
11051 Note that one additional advantage of following our Elaborate_All rule
11052 is that the program continues to stay in the ideal (all orders OK) state
11053 even if maintenance
11054 changes some bodies of some subprograms. Conversely, if a program that does
11055 not follow this rule happens to be safe at some point, this state of affairs
11056 may deteriorate silently as a result of maintenance changes.
11058 You may have noticed that the above discussion did not mention
11059 the use of @code{Elaborate_Body}. This was a deliberate omission. If you
11060 @code{with} an @code{Elaborate_Body} unit, it still may be the case that
11061 code in the body makes calls to some other unit, so it is still necessary
11062 to use @code{Elaborate_All} on such units.
11064 @node Controlling Elaboration in GNAT - Internal Calls
11065 @section Controlling Elaboration in GNAT - Internal Calls
11067 @noindent
11068 In the case of internal calls, i.e. calls within a single package, the
11069 programmer has full control over the order of elaboration, and it is up
11070 to the programmer to elaborate declarations in an appropriate order. For
11071 example writing:
11073 @smallexample
11074 @group
11075 @cartouche
11076 @b{function} One @b{return} Float;
11078 Q : Float := One;
11080 @b{function} One @b{return} Float @b{is}
11081 @b{begin}
11082      return 1.0;
11083 @b{end} One;
11084 @end cartouche
11085 @end group
11086 @end smallexample
11088 @noindent
11089 will obviously raise @code{Program_Error} at run time, because function
11090 One will be called before its body is elaborated. In this case GNAT will
11091 generate a warning that the call will raise @code{Program_Error}:
11093 @smallexample
11094 @group
11095 @cartouche
11096  1. procedure y is
11097  2.    function One return Float;
11098  3.
11099  4.    Q : Float := One;
11100                     |
11101     >>> warning: cannot call "One" before body is elaborated
11102     >>> warning: Program_Error will be raised at run time
11104  5.
11105  6.    function One return Float is
11106  7.    begin
11107  8.         return 1.0;
11108  9.    end One;
11110 11. begin
11111 12.    null;
11112 13. end;
11113 @end cartouche
11114 @end group
11115 @end smallexample
11117 @noindent
11118 Note that in this particular case, it is likely that the call is safe, because
11119 the function @code{One} does not access any global variables.
11120 Nevertheless in Ada 95, we do not want the validity of the check to depend on
11121 the contents of the body (think about the separate compilation case), so this
11122 is still wrong, as we discussed in the previous sections.
11124 The error is easily corrected by rearranging the declarations so that the
11125 body of One appears before the declaration containing the call
11126 (note that in Ada 95,
11127 declarations can appear in any order, so there is no restriction that
11128 would prevent this reordering, and if we write:
11130 @smallexample
11131 @group
11132 @cartouche
11133 @b{function} One @b{return} Float;
11135 @b{function} One @b{return} Float @b{is}
11136 @b{begin}
11137      return 1.0;
11138 @b{end} One;
11140 Q : Float := One;
11141 @end cartouche
11142 @end group
11143 @end smallexample
11145 @noindent
11146 then all is well, no warning is generated, and no
11147 @code{Program_Error} exception
11148 will be raised.
11149 Things are more complicated when a chain of subprograms is executed:
11151 @smallexample
11152 @group
11153 @cartouche
11154 @b{function} A @b{return} Integer;
11155 @b{function} B @b{return} Integer;
11156 @b{function} C @b{return} Integer;
11158 @b{function} B @b{return} Integer @b{is begin return} A; @b{end};
11159 @b{function} C @b{return} Integer @b{is begin return} B; @b{end};
11161 X : Integer := C;
11163 @b{function} A @b{return} Integer @b{is begin return} 1; @b{end};
11164 @end cartouche
11165 @end group
11166 @end smallexample
11168 @noindent
11169 Now the call to @code{C}
11170 at elaboration time in the declaration of @code{X} is correct, because
11171 the body of @code{C} is already elaborated,
11172 and the call to @code{B} within the body of
11173 @code{C} is correct, but the call
11174 to @code{A} within the body of @code{B} is incorrect, because the body
11175 of @code{A} has not been elaborated, so @code{Program_Error}
11176 will be raised on the call to @code{A}.
11177 In this case GNAT will generate a
11178 warning that @code{Program_Error} may be
11179 raised at the point of the call. Let's look at the warning:
11181 @smallexample
11182 @group
11183 @cartouche
11184  1. procedure x is
11185  2.    function A return Integer;
11186  3.    function B return Integer;
11187  4.    function C return Integer;
11188  5.
11189  6.    function B return Integer is begin return A; end;
11190                                                     |
11191     >>> warning: call to "A" before body is elaborated may
11192                  raise Program_Error
11193     >>> warning: "B" called at line 7
11194     >>> warning: "C" called at line 9
11196  7.    function C return Integer is begin return B; end;
11197  8.
11198  9.    X : Integer := C;
11200 11.    function A return Integer is begin return 1; end;
11202 13. begin
11203 14.    null;
11204 15. end;
11205 @end cartouche
11206 @end group
11207 @end smallexample
11209 @noindent
11210 Note that the message here says "may raise", instead of the direct case,
11211 where the message says "will be raised". That's because whether
11212 @code{A} is
11213 actually called depends in general on run-time flow of control.
11214 For example, if the body of @code{B} said
11216 @smallexample
11217 @group
11218 @cartouche
11219 @b{function} B @b{return} Integer @b{is}
11220 @b{begin}
11221    @b{if} some-condition-depending-on-input-data @b{then}
11222       @b{return} A;
11223    @b{else}
11224       @b{return} 1;
11225    @b{end if};
11226 @b{end} B;
11227 @end cartouche
11228 @end group
11229 @end smallexample
11231 @noindent
11232 then we could not know until run time whether the incorrect call to A would
11233 actually occur, so @code{Program_Error} might
11234 or might not be raised. It is possible for a compiler to
11235 do a better job of analyzing bodies, to
11236 determine whether or not @code{Program_Error}
11237 might be raised, but it certainly
11238 couldn't do a perfect job (that would require solving the halting problem
11239 and is provably impossible), and because this is a warning anyway, it does
11240 not seem worth the effort to do the analysis. Cases in which it
11241 would be relevant are rare.
11243 In practice, warnings of either of the forms given
11244 above will usually correspond to
11245 real errors, and should be examined carefully and eliminated.
11246 In the rare case where a warning is bogus, it can be suppressed by any of
11247 the following methods:
11249 @itemize @bullet
11250 @item
11251 Compile with the @option{/WARNINGS=SUPPRESS} qualifier set
11253 @item
11254 Suppress @code{Elaboration_Checks} for the called subprogram
11256 @item
11257 Use pragma @code{Warnings_Off} to turn warnings off for the call
11258 @end itemize
11260 @noindent
11261 For the internal elaboration check case,
11262 GNAT by default generates the
11263 necessary run-time checks to ensure
11264 that @code{Program_Error} is raised if any
11265 call fails an elaboration check. Of course this can only happen if a
11266 warning has been issued as described above. The use of pragma
11267 @code{Suppress (Elaboration_Checks)} may (but is not guaranteed to) suppress
11268 some of these checks, meaning that it may be possible (but is not
11269 guaranteed) for a program to be able to call a subprogram whose body
11270 is not yet elaborated, without raising a @code{Program_Error} exception.
11272 @node Controlling Elaboration in GNAT - External Calls
11273 @section Controlling Elaboration in GNAT - External Calls
11275 @noindent
11276 The previous section discussed the case in which the execution of a
11277 particular thread of elaboration code occurred entirely within a
11278 single unit. This is the easy case to handle, because a programmer
11279 has direct and total control over the order of elaboration, and
11280 furthermore, checks need only be generated in cases which are rare
11281 and which the compiler can easily detect.
11282 The situation is more complex when separate compilation is taken into account.
11283 Consider the following:
11285 @smallexample
11286 @cartouche
11287 @group
11288 @b{package} Math @b{is}
11289    @b{function} Sqrt (Arg : Float) @b{return} Float;
11290 @b{end} Math;
11292 @b{package body} Math @b{is}
11293    @b{function} Sqrt (Arg : Float) @b{return} Float @b{is}
11294    @b{begin}
11295          ...
11296    @b{end} Sqrt;
11297 @b{end} Math;
11298 @end group
11299 @group
11300 @b{with} Math;
11301 @b{package} Stuff @b{is}
11302    X : Float := Math.Sqrt (0.5);
11303 @b{end} Stuff;
11305 @b{with} Stuff;
11306 @b{procedure} Main @b{is}
11307 @b{begin}
11308    ...
11309 @b{end} Main;
11310 @end group
11311 @end cartouche
11312 @end smallexample
11314 @noindent
11315 where @code{Main} is the main program. When this program is executed, the
11316 elaboration code must first be executed, and one of the jobs of the
11317 binder is to determine the order in which the units of a program are
11318 to be elaborated. In this case we have four units: the spec and body
11319 of @code{Math},
11320 the spec of @code{Stuff} and the body of @code{Main}).
11321 In what order should the four separate sections of elaboration code
11322 be executed?
11324 There are some restrictions in the order of elaboration that the binder
11325 can choose. In particular, if unit U has a @code{with}
11326 for a package @code{X}, then you
11327 are assured that the spec of @code{X}
11328 is elaborated before U , but you are
11329 not assured that the body of @code{X}
11330 is elaborated before U.
11331 This means that in the above case, the binder is allowed to choose the
11332 order:
11334 @smallexample
11335 spec of Math
11336 spec of Stuff
11337 body of Math
11338 body of Main
11339 @end smallexample
11341 @noindent
11342 but that's not good, because now the call to @code{Math.Sqrt}
11343 that happens during
11344 the elaboration of the @code{Stuff}
11345 spec happens before the body of @code{Math.Sqrt} is
11346 elaborated, and hence causes @code{Program_Error} exception to be raised.
11347 At first glance, one might say that the binder is misbehaving, because
11348 obviously you want to elaborate the body of something you @code{with}
11349 first, but
11350 that is not a general rule that can be followed in all cases. Consider
11352 @smallexample
11353 @group
11354 @cartouche
11355 @b{package} X @b{is} ...
11357 @b{package} Y @b{is} ...
11359 @b{with} X;
11360 @b{package body} Y @b{is} ...
11362 @b{with} Y;
11363 @b{package body} X @b{is} ...
11364 @end cartouche
11365 @end group
11366 @end smallexample
11368 @noindent
11369 This is a common arrangement, and, apart from the order of elaboration
11370 problems that might arise in connection with elaboration code, this works fine.
11371 A rule that says that you must first elaborate the body of anything you
11372 @code{with} cannot work in this case:
11373 the body of @code{X} @code{with}'s @code{Y},
11374 which means you would have to
11375 elaborate the body of @code{Y} first, but that @code{with}'s @code{X},
11376 which means
11377 you have to elaborate the body of @code{X} first, but ... and we have a
11378 loop that cannot be broken.
11380 It is true that the binder can in many cases guess an order of elaboration
11381 that is unlikely to cause a @code{Program_Error}
11382 exception to be raised, and it tries to do so (in the
11383 above example of @code{Math/Stuff/Spec}, the GNAT binder will
11384 by default
11385 elaborate the body of @code{Math} right after its spec, so all will be well).
11387 However, a program that blindly relies on the binder to be helpful can
11388 get into trouble, as we discussed in the previous sections, so
11389 GNAT
11390 provides a number of facilities for assisting the programmer in
11391 developing programs that are robust with respect to elaboration order.
11393 @node Default Behavior in GNAT - Ensuring Safety
11394 @section Default Behavior in GNAT - Ensuring Safety
11396 @noindent
11397 The default behavior in GNAT ensures elaboration safety. In its
11398 default mode GNAT implements the
11399 rule we previously described as the right approach. Let's restate it:
11401 @itemize
11402 @item
11403 @emph{If a unit has elaboration code that can directly or indirectly make a
11404 call to a subprogram in a @code{with}'ed unit, or instantiate a generic unit
11405 in a @code{with}'ed unit, then if the @code{with}'ed unit
11406 does not have pragma @code{Pure} or
11407 @code{Preelaborate}, then the client should have an
11408 @code{Elaborate_All} for the @code{with}'ed unit.}
11409 @end itemize
11411 @noindent
11412 By following this rule a client
11413 is assured that calls and instantiations can be made without risk of an exception.
11415 In this mode GNAT traces all calls that are potentially made from
11416 elaboration code, and puts in any missing implicit @code{Elaborate_All}
11417 pragmas.
11418 The advantage of this approach is that no elaboration problems
11419 are possible if the binder can find an elaboration order that is
11420 consistent with these implicit @code{Elaborate_All} pragmas. The
11421 disadvantage of this approach is that no such order may exist.
11423 If the binder does not generate any diagnostics, then it means that it
11424 has found an elaboration order that is guaranteed to be safe. However,
11425 the binder may still be relying on implicitly generated
11426 @code{Elaborate_All} pragmas so portability to other compilers than
11427 GNAT is not guaranteed.
11429 If it is important to guarantee portability, then the compilations should
11430 use the
11431 @option{/WARNINGS=ELABORATION}
11432 (warn on elaboration problems) qualifier. This will cause warning messages
11433 to be generated indicating the missing @code{Elaborate_All} pragmas.
11434 Consider the following source program:
11436 @smallexample
11437 @group
11438 @cartouche
11439 @b{with} k;
11440 @b{package} j @b{is}
11441   m : integer := k.r;
11442 @b{end};
11443 @end cartouche
11444 @end group
11445 @end smallexample
11447 @noindent
11448 where it is clear that there
11449 should be a pragma @code{Elaborate_All}
11450 for unit @code{k}. An implicit pragma will be generated, and it is
11451 likely that the binder will be able to honor it. However,
11452 it is safer to include the pragma explicitly in the source. If this
11453 unit is compiled with the
11454 @option{/WARNINGS=ELABORATION}
11455 qualifier, then the compiler outputs a warning:
11457 @smallexample
11458 @group
11459 @cartouche
11460 1. with k;
11461 2. package j is
11462 3.   m : integer := k.r;
11463                      |
11464    >>> warning: call to "r" may raise Program_Error
11465    >>> warning: missing pragma Elaborate_All for "k"
11467 4. end;
11468 @end cartouche
11469 @end group
11470 @end smallexample
11472 @noindent
11473 and these warnings can be used as a guide for supplying manually
11474 the missing pragmas.
11476 This default mode is more restrictive than the Ada Reference
11477 Manual, and it is possible to construct programs which will compile
11478 using the dynamic model described there, but will run into a
11479 circularity using the safer static model we have described.
11481 Of course any Ada compiler must be able to operate in a mode
11482 consistent with the requirements of the Ada Reference Manual,
11483 and in particular must have the capability of implementing the
11484 standard dynamic model of elaboration with run-time checks.
11486 In GNAT, this standard mode can be achieved either by the use of
11487 the @option{/CHECKS=ELABORATION} qualifier on the compiler (@code{GNAT COMPILE} or @code{GNAT MAKE})
11488 command, or by the use of the configuration pragma:
11490 @smallexample
11491 pragma Elaboration_Checks (RM);
11492 @end smallexample
11494 @noindent
11495 Either approach will cause the unit affected to be compiled using the
11496 standard dynamic run-time elaboration checks described in the Ada
11497 Reference Manual. The static model is generally preferable, since it
11498 is clearly safer to rely on compile and link time checks rather than
11499 run-time checks. However, in the case of legacy code, it may be
11500 difficult to meet the requirements of the static model. This
11501 issue is further discussed in
11502 @ref{What to Do If the Default Elaboration Behavior Fails}.
11504 Note that the static model provides a strict subset of the allowed
11505 behavior and programs of the Ada Reference Manual, so if you do
11506 adhere to the static model and no circularities exist,
11507 then you are assured that your program will
11508 work using the dynamic model.
11510 @node Elaboration Issues for Library Tasks
11511 @section Elaboration Issues for Library Tasks
11512 @cindex Library tasks, elaboration issues
11513 @cindex Elaboration of library tasks
11515 @noindent
11516 In this section we examine special elaboration issues that arise for
11517 programs that declare library level tasks.
11519 Generally the model of execution of an Ada program is that all units are
11520 elaborated, and then execution of the program starts. However, the
11521 declaration of library tasks definitely does not fit this model. The
11522 reason for this is that library tasks start as soon as they are declared
11523 (more precisely, as soon as the statement part of the enclosing package
11524 body is reached), that is to say before elaboration
11525 of the program is complete. This means that if such a task calls a
11526 subprogram, or an entry in another task, the callee may or may not be
11527 elaborated yet, and in the standard
11528 Reference Manual model of dynamic elaboration checks, you can even
11529 get timing dependent Program_Error exceptions, since there can be
11530 a race between the elaboration code and the task code.
11532 The static model of elaboration in GNAT seeks to avoid all such
11533 dynamic behavior, by being conservative, and the conservative
11534 approach in this particular case is to assume that all the code
11535 in a task body is potentially executed at elaboration time if
11536 a task is declared at the library level.
11538 This can definitely result in unexpected circularities. Consider
11539 the following example
11541 @smallexample
11542 package Decls is
11543   task Lib_Task is
11544      entry Start;
11545   end Lib_Task;
11547   type My_Int is new Integer;
11549   function Ident (M : My_Int) return My_Int;
11550 end Decls;
11552 with Utils;
11553 package body Decls is
11554   task body Lib_Task is
11555   begin
11556      accept Start;
11557      Utils.Put_Val (2);
11558   end Lib_Task;
11560   function Ident (M : My_Int) return My_Int is
11561   begin
11562      return M;
11563   end Ident;
11564 end Decls;
11566 with Decls;
11567 package Utils is
11568   procedure Put_Val (Arg : Decls.My_Int);
11569 end Utils;
11571 with Text_IO;
11572 package body Utils is
11573   procedure Put_Val (Arg : Decls.My_Int) is
11574   begin
11575      Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
11576   end Put_Val;
11577 end Utils;
11579 with Decls;
11580 procedure Main is
11581 begin
11582    Decls.Lib_Task.Start;
11583 end;
11584 @end smallexample
11586 @noindent
11587 If the above example is compiled in the default static elaboration
11588 mode, then a circularity occurs. The circularity comes from the call
11589 @code{Utils.Put_Val} in the task body of @code{Decls.Lib_Task}. Since
11590 this call occurs in elaboration code, we need an implicit pragma
11591 @code{Elaborate_All} for @code{Utils}. This means that not only must
11592 the spec and body of @code{Utils} be elaborated before the body
11593 of @code{Decls}, but also the spec and body of any unit that is
11594 @code{with'ed} by the body of @code{Utils} must also be elaborated before
11595 the body of @code{Decls}. This is the transitive implication of
11596 pragma @code{Elaborate_All} and it makes sense, because in general
11597 the body of @code{Put_Val} might have a call to something in a
11598 @code{with'ed} unit.
11600 In this case, the body of Utils (actually its spec) @code{with's}
11601 @code{Decls}. Unfortunately this means that the body of @code{Decls}
11602 must be elaborated before itself, in case there is a call from the
11603 body of @code{Utils}.
11605 Here is the exact chain of events we are worrying about:
11607 @enumerate
11608 @item
11609 In the body of @code{Decls} a call is made from within the body of a library
11610 task to a subprogram in the package @code{Utils}. Since this call may
11611 occur at elaboration time (given that the task is activated at elaboration
11612 time), we have to assume the worst, i.e. that the
11613 call does happen at elaboration time.
11615 @item
11616 This means that the body and spec of @code{Util} must be elaborated before
11617 the body of @code{Decls} so that this call does not cause an access before
11618 elaboration.
11620 @item
11621 Within the body of @code{Util}, specifically within the body of
11622 @code{Util.Put_Val} there may be calls to any unit @code{with}'ed
11623 by this package.
11625 @item
11626 One such @code{with}'ed package is package @code{Decls}, so there
11627 might be a call to a subprogram in @code{Decls} in @code{Put_Val}.
11628 In fact there is such a call in this example, but we would have to
11629 assume that there was such a call even if it were not there, since
11630 we are not supposed to write the body of @code{Decls} knowing what
11631 is in the body of @code{Utils}; certainly in the case of the
11632 static elaboration model, the compiler does not know what is in
11633 other bodies and must assume the worst.
11635 @item
11636 This means that the spec and body of @code{Decls} must also be
11637 elaborated before we elaborate the unit containing the call, but
11638 that unit is @code{Decls}! This means that the body of @code{Decls}
11639 must be elaborated before itself, and that's a circularity.
11640 @end enumerate
11642 @noindent
11643 Indeed, if you add an explicit pragma Elaborate_All for @code{Utils} in
11644 the body of @code{Decls} you will get a true Ada Reference Manual
11645 circularity that makes the program illegal.
11647 In practice, we have found that problems with the static model of
11648 elaboration in existing code often arise from library tasks, so
11649 we must address this particular situation.
11651 Note that if we compile and run the program above, using the dynamic model of
11652 elaboration (that is to say use the @option{/CHECKS=ELABORATION} qualifier),
11653 then it compiles, binds,
11654 links, and runs, printing the expected result of 2. Therefore in some sense
11655 the circularity here is only apparent, and we need to capture
11656 the properties of this program that  distinguish it from other library-level
11657 tasks that have real elaboration problems.
11659 We have four possible answers to this question:
11661 @itemize @bullet
11663 @item
11664 Use the dynamic model of elaboration.
11666 If we use the @option{/CHECKS=ELABORATION} qualifier, then as noted above, the program works.
11667 Why is this? If we examine the task body, it is apparent that the task cannot
11668 proceed past the
11669 @code{accept} statement until after elaboration has been completed, because
11670 the corresponding entry call comes from the main program, not earlier.
11671 This is why the dynamic model works here. But that's really giving
11672 up on a precise analysis, and we prefer to take this approach only if we cannot
11673 solve the
11674 problem in any other manner. So let us examine two ways to reorganize
11675 the program to avoid the potential elaboration problem.
11677 @item
11678 Split library tasks into separate packages.
11680 Write separate packages, so that library tasks are isolated from
11681 other declarations as much as possible. Let us look at a variation on
11682 the above program.
11684 @smallexample
11685 package Decls1 is
11686   task Lib_Task is
11687      entry Start;
11688   end Lib_Task;
11689 end Decls1;
11691 with Utils;
11692 package body Decls1 is
11693   task body Lib_Task is
11694   begin
11695      accept Start;
11696      Utils.Put_Val (2);
11697   end Lib_Task;
11698 end Decls1;
11700 package Decls2 is
11701   type My_Int is new Integer;
11702   function Ident (M : My_Int) return My_Int;
11703 end Decls2;
11705 with Utils;
11706 package body Decls2 is
11707   function Ident (M : My_Int) return My_Int is
11708   begin
11709      return M;
11710   end Ident;
11711 end Decls2;
11713 with Decls2;
11714 package Utils is
11715   procedure Put_Val (Arg : Decls2.My_Int);
11716 end Utils;
11718 with Text_IO;
11719 package body Utils is
11720   procedure Put_Val (Arg : Decls2.My_Int) is
11721   begin
11722      Text_IO.Put_Line (Decls2.My_Int'Image (Decls2.Ident (Arg)));
11723   end Put_Val;
11724 end Utils;
11726 with Decls1;
11727 procedure Main is
11728 begin
11729    Decls1.Lib_Task.Start;
11730 end;
11731 @end smallexample
11733 @noindent
11734 All we have done is to split @code{Decls} into two packages, one
11735 containing the library task, and one containing everything else. Now
11736 there is no cycle, and the program compiles, binds, links and executes
11737 using the default static model of elaboration.
11739 @item
11740 Declare separate task types.
11742 A significant part of the problem arises because of the use of the
11743 single task declaration form. This means that the elaboration of
11744 the task type, and the elaboration of the task itself (i.e. the
11745 creation of the task) happen at the same time. A good rule
11746 of style in Ada 95 is to always create explicit task types. By
11747 following the additional step of placing task objects in separate
11748 packages from the task type declaration, many elaboration problems
11749 are avoided. Here is another modified example of the example program:
11751 @smallexample
11752 package Decls is
11753   task type Lib_Task_Type is
11754      entry Start;
11755   end Lib_Task_Type;
11757   type My_Int is new Integer;
11759   function Ident (M : My_Int) return My_Int;
11760 end Decls;
11762 with Utils;
11763 package body Decls is
11764   task body Lib_Task_Type is
11765   begin
11766      accept Start;
11767      Utils.Put_Val (2);
11768   end Lib_Task_Type;
11770   function Ident (M : My_Int) return My_Int is
11771   begin
11772      return M;
11773   end Ident;
11774 end Decls;
11776 with Decls;
11777 package Utils is
11778   procedure Put_Val (Arg : Decls.My_Int);
11779 end Utils;
11781 with Text_IO;
11782 package body Utils is
11783   procedure Put_Val (Arg : Decls.My_Int) is
11784   begin
11785      Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
11786   end Put_Val;
11787 end Utils;
11789 with Decls;
11790 package Declst is
11791    Lib_Task : Decls.Lib_Task_Type;
11792 end Declst;
11794 with Declst;
11795 procedure Main is
11796 begin
11797    Declst.Lib_Task.Start;
11798 end;
11799 @end smallexample
11801 @noindent
11802 What we have done here is to replace the @code{task} declaration in
11803 package @code{Decls} with a @code{task type} declaration. Then we
11804 introduce a separate package @code{Declst} to contain the actual
11805 task object. This separates the elaboration issues for
11806 the @code{task type}
11807 declaration, which causes no trouble, from the elaboration issues
11808 of the task object, which is also unproblematic, since it is now independent
11809 of the elaboration of  @code{Utils}.
11810 This separation of concerns also corresponds to
11811 a generally sound engineering principle of separating declarations
11812 from instances. This version of the program also compiles, binds, links,
11813 and executes, generating the expected output.
11815 @item
11816 Use No_Entry_Calls_In_Elaboration_Code restriction.
11817 @cindex No_Entry_Calls_In_Elaboration_Code
11819 The previous two approaches described how a program can be restructured
11820 to avoid the special problems caused by library task bodies. in practice,
11821 however, such restructuring may be difficult to apply to existing legacy code,
11822 so we must consider solutions that do not require massive rewriting.
11824 Let us consider more carefully why our original sample program works
11825 under the dynamic model of elaboration. The reason is that the code
11826 in the task body blocks immediately on the @code{accept}
11827 statement. Now of course there is nothing to prohibit elaboration
11828 code from making entry calls (for example from another library level task),
11829 so we cannot tell in isolation that
11830 the task will not execute the accept statement  during elaboration.
11832 However, in practice it is very unusual to see elaboration code
11833 make any entry calls, and the pattern of tasks starting
11834 at elaboration time and then immediately blocking on @code{accept} or
11835 @code{select} statements is very common. What this means is that
11836 the compiler is being too pessimistic when it analyzes the
11837 whole package body as though it might be executed at elaboration
11838 time.
11840 If we know that the elaboration code contains no entry calls, (a very safe
11841 assumption most of the time, that could almost be made the default
11842 behavior), then we can compile all units of the program under control
11843 of the following configuration pragma:
11845 @smallexample
11846 pragma Restrictions (No_Entry_Calls_In_Elaboration_Code);
11847 @end smallexample
11849 @noindent
11850 This pragma can be placed in the @file{GNAT.ADC} file in the usual
11851 manner. If we take our original unmodified program and compile it
11852 in the presence of a @file{GNAT.ADC} containing the above pragma,
11853 then once again, we can compile, bind, link, and execute, obtaining
11854 the expected result. In the presence of this pragma, the compiler does
11855 not trace calls in a task body, that appear after the first @code{accept}
11856 or @code{select} statement, and therefore does not report a potential
11857 circularity in the original program.
11859 The compiler will check to the extent it can that the above
11860 restriction is not violated, but it is not always possible to do a
11861 complete check at compile time, so it is important to use this
11862 pragma only if the stated restriction is in fact met, that is to say
11863 no task receives an entry call before elaboration of all units is completed.
11865 @end itemize
11867 @node Mixing Elaboration Models
11868 @section Mixing Elaboration Models
11869 @noindent
11870 So far, we have assumed that the entire program is either compiled
11871 using the dynamic model or static model, ensuring consistency. It
11872 is possible to mix the two models, but rules have to be followed
11873 if this mixing is done to ensure that elaboration checks are not
11874 omitted.
11876 The basic rule is that @emph{a unit compiled with the static model cannot
11877 be @code{with'ed} by a unit compiled with the dynamic model}. The
11878 reason for this is that in the static model, a unit assumes that
11879 its clients guarantee to use (the equivalent of) pragma
11880 @code{Elaborate_All} so that no elaboration checks are required
11881 in inner subprograms, and this assumption is violated if the
11882 client is compiled with dynamic checks.
11884 The precise rule is as follows. A unit that is compiled with dynamic
11885 checks can only @code{with} a unit that meets at least one of the
11886 following criteria:
11888 @itemize @bullet
11890 @item
11891 The @code{with'ed} unit is itself compiled with dynamic elaboration
11892 checks (that is with the @option{/CHECKS=ELABORATION} qualifier.
11894 @item
11895 The @code{with'ed} unit is an internal GNAT implementation unit from
11896 the System, Interfaces, Ada, or GNAT hierarchies.
11898 @item
11899 The @code{with'ed} unit has pragma Preelaborate or pragma Pure.
11901 @item
11902 The @code{with'ing} unit (that is the client) has an explicit pragma
11903 @code{Elaborate_All} for the @code{with'ed} unit.
11905 @end itemize
11907 @noindent
11908 If this rule is violated, that is if a unit with dynamic elaboration
11909 checks @code{with's} a unit that does not meet one of the above four
11910 criteria, then the binder (@code{GNAT BIND}) will issue a warning
11911 similar to that in the following example:
11913 @smallexample
11914 warning: "X.ADS" has dynamic elaboration checks and with's
11915 warning:   "Y.ADS" which has static elaboration checks
11916 @end smallexample
11918 @noindent
11919 These warnings indicate that the rule has been violated, and that as a result
11920 elaboration checks may be missed in the resulting executable file.
11921 This warning may be suppressed using the @code{-ws} binder qualifier
11922 in the usual manner.
11924 One useful application of this mixing rule is in the case of a subsystem
11925 which does not itself @code{with} units from the remainder of the
11926 application. In this case, the entire subsystem can be compiled with
11927 dynamic checks to resolve a circularity in the subsystem, while
11928 allowing the main application that uses this subsystem to be compiled
11929 using the more reliable default static model.
11931 @node What to Do If the Default Elaboration Behavior Fails
11932 @section What to Do If the Default Elaboration Behavior Fails
11934 @noindent
11935 If the binder cannot find an acceptable order, it outputs detailed
11936 diagnostics. For example:
11937 @smallexample
11938 @group
11939 @iftex
11940 @leftskip=0cm
11941 @end iftex
11942 error: elaboration circularity detected
11943 info:   "proc (body)" must be elaborated before "pack (body)"
11944 info:     reason: Elaborate_All probably needed in unit "pack (body)"
11945 info:     recompile "pack (body)" with /WARNINGS=ELABORATION
11946 info:                             for full details
11947 info:       "proc (body)"
11948 info:         is needed by its spec:
11949 info:       "proc (spec)"
11950 info:         which is withed by:
11951 info:       "pack (body)"
11952 info:  "pack (body)" must be elaborated before "proc (body)"
11953 info:     reason: pragma Elaborate in unit "proc (body)"
11954 @end group
11956 @end smallexample
11958 @noindent
11959 In this case we have a cycle that the binder cannot break. On the one
11960 hand, there is an explicit pragma Elaborate in @code{proc} for
11961 @code{pack}. This means that the body of @code{pack} must be elaborated
11962 before the body of @code{proc}. On the other hand, there is elaboration
11963 code in @code{pack} that calls a subprogram in @code{proc}. This means
11964 that for maximum safety, there should really be a pragma
11965 Elaborate_All in @code{pack} for @code{proc} which would require that
11966 the body of @code{proc} be elaborated before the body of
11967 @code{pack}. Clearly both requirements cannot be satisfied.
11968 Faced with a circularity of this kind, you have three different options.
11970 @table @asis
11971 @item Fix the program
11972 The most desirable option from the point of view of long-term maintenance
11973 is to rearrange the program so that the elaboration problems are avoided.
11974 One useful technique is to place the elaboration code into separate
11975 child packages. Another is to move some of the initialization code to
11976 explicitly called subprograms, where the program controls the order
11977 of initialization explicitly. Although this is the most desirable option,
11978 it may be impractical and involve too much modification, especially in
11979 the case of complex legacy code.
11981 @item Perform dynamic checks
11982 If the compilations are done using the
11983 @option{/CHECKS=ELABORATION}
11984 (dynamic elaboration check) qualifier, then GNAT behaves in
11985 a quite different manner. Dynamic checks are generated for all calls
11986 that could possibly result in raising an exception. With this qualifier,
11987 the compiler does not generate implicit @code{Elaborate_All} pragmas.
11988 The behavior then is exactly as specified in the Ada 95 Reference Manual.
11989 The binder will generate an executable program that may or may not
11990 raise @code{Program_Error}, and then it is the programmer's job to ensure
11991 that it does not raise an exception. Note that it is important to
11992 compile all units with the qualifier, it cannot be used selectively.
11994 @item Suppress checks
11995 The drawback of dynamic checks is that they generate a
11996 significant overhead at run time, both in space and time. If you
11997 are absolutely sure that your program cannot raise any elaboration
11998 exceptions, and you still want to use the dynamic elaboration model,
11999 then you can use the configuration pragma
12000 @code{Suppress (Elaboration_Checks)} to suppress all such checks. For
12001 example this pragma could be placed in the @file{GNAT.ADC} file.
12003 @item Suppress checks selectively
12004 When you know that certain calls in elaboration code cannot possibly
12005 lead to an elaboration error, and the binder nevertheless generates warnings
12006 on those calls and inserts Elaborate_All pragmas that lead to elaboration
12007 circularities, it is possible to remove those warnings locally and obtain
12008 a program that will bind. Clearly this can be unsafe, and it is the
12009 responsibility of the programmer to make sure that the resulting program has
12010 no elaboration anomalies. The pragma @code{Suppress (Elaboration_Check)} can
12011 be used with different granularity to suppress warnings and break
12012 elaboration circularities:
12014 @itemize @bullet
12015 @item
12016 Place the pragma that names the called subprogram in the declarative part
12017 that contains the call.
12019 @item
12020 Place the pragma in the declarative part, without naming an entity. This
12021 disables warnings on all calls in the corresponding  declarative region.
12023 @item
12024 Place the pragma in the package spec that declares the called subprogram,
12025 and name the subprogram. This disables warnings on all elaboration calls to
12026 that subprogram.
12028 @item
12029 Place the pragma in the package spec that declares the called subprogram,
12030 without naming any entity. This disables warnings on all elaboration calls to
12031 all subprograms declared in this spec.
12032 @end itemize
12034 @noindent
12035 These four cases are listed in order of decreasing safety, and therefore
12036 require increasing programmer care in their application. Consider the
12037 following program:
12038 @smallexample
12040 package Pack1 is
12041   function F1 return Integer;
12042   X1 : Integer;
12043 end Pack1;
12045 package Pack2 is
12046   function F2 return Integer;
12047   function Pure (x : integer) return integer;
12048   --  pragma Suppress (Elaboration_Check, On => Pure);  -- (3)
12049   --  pragma Suppress (Elaboration_Check);              -- (4)
12050 end Pack2;
12052 with Pack2;
12053 package body Pack1 is
12054   function F1 return Integer is
12055   begin
12056     return 100;
12057   end F1;
12058   Val : integer := Pack2.Pure (11);    --  Elab. call (1)
12059 begin
12060   declare
12061     --  pragma Suppress(Elaboration_Check, Pack2.F2);   -- (1)
12062     --  pragma Suppress(Elaboration_Check);             -- (2)
12063   begin
12064     X1 := Pack2.F2 + 1;                --  Elab. call (2)
12065   end;
12066 end Pack1;
12068 with Pack1;
12069 package body Pack2 is
12070   function F2 return Integer is
12071   begin
12072      return Pack1.F1;
12073   end F2;
12074   function Pure (x : integer) return integer is
12075   begin
12076      return x ** 3 - 3 * x;
12077   end;
12078 end Pack2;
12080 with Pack1, Ada.Text_IO;
12081 procedure Proc3 is
12082 begin
12083   Ada.Text_IO.Put_Line(Pack1.X1'Img); -- 101
12084 end Proc3;
12085 @end smallexample
12086 In the absence of any pragmas, an attempt to bind this program produces
12087 the following diagnostics:
12088 @smallexample
12089 @group
12090 @iftex
12091 @leftskip=.5cm
12092 @end iftex
12093 error: elaboration circularity detected
12094 info:    "pack1 (body)" must be elaborated before "pack1 (body)"
12095 info:       reason: Elaborate_All probably needed in unit "pack1 (body)"
12096 info:       recompile "pack1 (body)" with /WARNINGS=ELABORATION for full details
12097 info:          "pack1 (body)"
12098 info:             must be elaborated along with its spec:
12099 info:          "pack1 (spec)"
12100 info:             which is withed by:
12101 info:          "pack2 (body)"
12102 info:             which must be elaborated along with its spec:
12103 info:          "pack2 (spec)"
12104 info:             which is withed by:
12105 info:          "pack1 (body)"
12106 @end group
12107 @end smallexample
12108 The sources of the circularity are the two calls to @code{Pack2.Pure} and
12109 @code{Pack2.F2} in the body of @code{Pack1}. We can see that the call to
12110 F2 is safe, even though F2 calls F1, because the call appears after the
12111 elaboration of the body of F1. Therefore the pragma (1) is safe, and will
12112 remove the warning on the call. It is also possible to use pragma (2)
12113 because there are no other potentially unsafe calls in the block.
12115 @noindent
12116 The call to @code{Pure} is safe because this function does not depend on the
12117 state of @code{Pack2}. Therefore any call to this function is safe, and it
12118 is correct to place pragma (3) in the corresponding package spec.
12120 @noindent
12121 Finally, we could place pragma (4) in the spec of @code{Pack2} to disable
12122 warnings on all calls to functions declared therein. Note that this is not
12123 necessarily safe, and requires more detailed examination of the subprogram
12124 bodies involved. In particular, a call to @code{F2} requires that @code{F1}
12125 be already elaborated.
12126 @end table
12128 @noindent
12129 It is hard to generalize on which of these four approaches should be
12130 taken. Obviously if it is possible to fix the program so that the default
12131 treatment works, this is preferable, but this may not always be practical.
12132 It is certainly simple enough to use
12133 @option{/CHECKS=ELABORATION}
12134 but the danger in this case is that, even if the GNAT binder
12135 finds a correct elaboration order, it may not always do so,
12136 and certainly a binder from another Ada compiler might not. A
12137 combination of testing and analysis (for which the warnings generated
12138 with the
12139 @option{/WARNINGS=ELABORATION}
12140 qualifier can be useful) must be used to ensure that the program is free
12141 of errors. One qualifier that is useful in this testing is the
12142 @code{/PESSIMISTIC_ELABORATION_ORDER}
12143 qualifier for
12144 @code{GNAT BIND}.
12145 Normally the binder tries to find an order that has the best chance of
12146 of avoiding elaboration problems. With this qualifier, the binder
12147 plays a devil's advocate role, and tries to choose the order that
12148 has the best chance of failing. If your program works even with this
12149 qualifier, then it has a better chance of being error free, but this is still
12150 not a guarantee.
12152 For an example of this approach in action, consider the C-tests (executable
12153 tests) from the ACVC suite. If these are compiled and run with the default
12154 treatment, then all but one of them succeed without generating any error
12155 diagnostics from the binder. However, there is one test that fails, and
12156 this is not surprising, because the whole point of this test is to ensure
12157 that the compiler can handle cases where it is impossible to determine
12158 a correct order statically, and it checks that an exception is indeed
12159 raised at run time.
12161 This one test must be compiled and run using the
12162 @option{/CHECKS=ELABORATION}
12163 qualifier, and then it passes. Alternatively, the entire suite can
12164 be run using this qualifier. It is never wrong to run with the dynamic
12165 elaboration qualifier if your code is correct, and we assume that the
12166 C-tests are indeed correct (it is less efficient, but efficiency is
12167 not a factor in running the ACVC tests.)
12169 @node Elaboration for Access-to-Subprogram Values
12170 @section Elaboration for Access-to-Subprogram Values
12171 @cindex Access-to-subprogram
12173 @noindent
12174 The introduction of access-to-subprogram types in Ada 95 complicates
12175 the handling of elaboration. The trouble is that it becomes
12176 impossible to tell at compile time which procedure
12177 is being called. This means that it is not possible for the binder
12178 to analyze the elaboration requirements in this case.
12180 If at the point at which the access value is created
12181 (i.e., the evaluation of @code{P'Access} for a subprogram @code{P}),
12182 the body of the subprogram is
12183 known to have been elaborated, then the access value is safe, and its use
12184 does not require a check. This may be achieved by appropriate arrangement
12185 of the order of declarations if the subprogram is in the current unit,
12186 or, if the subprogram is in another unit, by using pragma
12187 @code{Pure}, @code{Preelaborate}, or @code{Elaborate_Body}
12188 on the referenced unit.
12190 If the referenced body is not known to have been elaborated at the point
12191 the access value is created, then any use of the access value must do a
12192 dynamic check, and this dynamic check will fail and raise a
12193 @code{Program_Error} exception if the body has not been elaborated yet.
12194 GNAT will generate the necessary checks, and in addition, if the
12195 @option{/WARNINGS=ELABORATION}
12196 qualifier is set, will generate warnings that such checks are required.
12198 The use of dynamic dispatching for tagged types similarly generates
12199 a requirement for dynamic checks, and premature calls to any primitive
12200 operation of a tagged type before the body of the operation has been elaborated,
12201 will result in the raising of @code{Program_Error}.
12203 @node Summary of Procedures for Elaboration Control
12204 @section Summary of Procedures for Elaboration Control
12205 @cindex Elaboration control
12207 @noindent
12208 First, compile your program with the default options, using none of
12209 the special elaboration control qualifiers. If the binder successfully
12210 binds your program, then you can be confident that, apart from issues
12211 raised by the use of access-to-subprogram types and dynamic dispatching,
12212 the program is free of elaboration errors. If it is important that the
12213 program be portable, then use the
12214 @option{/WARNINGS=ELABORATION}
12215 qualifier to generate warnings about missing @code{Elaborate_All}
12216 pragmas, and supply the missing pragmas.
12218 If the program fails to bind using the default static elaboration
12219 handling, then you can fix the program to eliminate the binder
12220 message, or recompile the entire program with the
12221 @option{/CHECKS=ELABORATION} qualifier to generate dynamic elaboration checks,
12222 and, if you are sure there really are no elaboration problems,
12223 use a global pragma @code{Suppress (Elaboration_Checks)}.
12225 @node Other Elaboration Order Considerations
12226 @section Other Elaboration Order Considerations
12227 @noindent
12228 This section has been entirely concerned with the issue of finding a valid
12229 elaboration order, as defined by the Ada Reference Manual. In a case
12230 where several elaboration orders are valid, the task is to find one
12231 of the possible valid elaboration orders (and the static model in GNAT
12232 will ensure that this is achieved).
12234 The purpose of the elaboration rules in the Ada Reference Manual is to
12235 make sure that no entity is accessed before it has been elaborated. For
12236 a subprogram, this means that the spec and body must have been elaborated
12237 before the subprogram is called. For an object, this means that the object
12238 must have been elaborated before its value is read or written. A violation
12239 of either of these two requirements is an access before elaboration order,
12240 and this section has been all about avoiding such errors.
12242 In the case where more than one order of elaboration is possible, in the
12243 sense that access before elaboration errors are avoided, then any one of
12244 the orders is "correct" in the sense that it meets the requirements of
12245 the Ada Reference Manual, and no such error occurs.
12247 However, it may be the case for a given program, that there are
12248 constraints on the order of elaboration that come not from consideration
12249 of avoiding elaboration errors, but rather from extra-lingual logic
12250 requirements. Consider this example:
12252 @smallexample
12253 with Init_Constants;
12254 package Constants is
12255    X : Integer := 0;
12256    Y : Integer := 0;
12257 end Constants;
12259 package Init_Constants is
12260    procedure Calc;
12261 end Init_Constants;
12263 with Constants;
12264 package body Init_Constants is
12265    procedure Calc is begin null; end;
12266 begin
12267    Constants.X := 3;
12268    Constants.Y := 4;
12269 end Init_Constants;
12271 with Constants;
12272 package Calc is
12273    Z : Integer := Constants.X + Constants.Y;
12274 end Calc;
12276 with Calc;
12277 with Text_IO; use Text_IO;
12278 procedure Main is
12279 begin
12280    Put_Line (Calc.Z'Img);
12281 end Main;
12282 @end smallexample
12284 @noindent
12285 In this example, there is more than one valid order of elaboration. For
12286 example both the following are correct orders:
12288 @smallexample
12289 Init_Constants spec
12290 Constants spec
12291 Calc spec
12292 Main body
12293 Init_Constants body
12295   and
12297 Init_Constants spec
12298 Init_Constants body
12299 Constants spec
12300 Calc spec
12301 Main body
12302 @end smallexample
12304 @noindent
12305 There is no language rule to prefer one or the other, both are correct
12306 from an order of elaboration point of view. But the programmatic effects
12307 of the two orders are very different. In the first, the elaboration routine
12308 of @code{Calc} initializes @code{Z} to zero, and then the main program
12309 runs with this value of zero. But in the second order, the elaboration
12310 routine of @code{Calc} runs after the body of Init_Constants has set
12311 @code{X} and @code{Y} and thus @code{Z} is set to 7 before @code{Main}
12312 runs.
12314 One could perhaps by applying pretty clever non-artificial intelligence
12315 to the situation guess that it is more likely that the second order of
12316 elaboration is the one desired, but there is no formal linguistic reason
12317 to prefer one over the other. In fact in this particular case, GNAT will
12318 prefer the second order, because of the rule that bodies are elaborated
12319 as soon as possible, but it's just luck that this is what was wanted
12320 (if indeed the second order was preferred).
12322 If the program cares about the order of elaboration routines in a case like
12323 this, it is important to specify the order required. In this particular
12324 case, that could have been achieved by adding to the spec of Calc:
12326 @smallexample
12327 pragma Elaborate_All (Constants);
12328 @end smallexample
12330 @noindent
12331 which requires that the body (if any) and spec of @code{Constants},
12332 as well as the body and spec of any unit @code{with}'ed by
12333 @code{Constants} be elaborated before @code{Calc} is elaborated.
12335 Clearly no automatic method can always guess which alternative you require,
12336 and if you are working with legacy code that had constraints of this kind
12337 which were not properly specified by adding @code{Elaborate} or
12338 @code{Elaborate_All} pragmas, then indeed it is possible that two different
12339 compilers can choose different orders.
12341 The @code{GNAT BIND}
12342 @code{/PESSIMISTIC_ELABORATION} qualifier may be useful in smoking
12343 out problems. This qualifier causes bodies to be elaborated as late as possible
12344 instead of as early as possible. In the example above, it would have forced
12345 the choice of the first elaboration order. If you get different results
12346 when using this qualifier, and particularly if one set of results is right,
12347 and one is wrong as far as you are concerned, it shows that you have some
12348 missing @code{Elaborate} pragmas. For the example above, we have the
12349 following output:
12351 @smallexample
12352 GNAT MAKE -f -q main
12353 main
12355 GNAT MAKE -f -q main /BINDER_QUALIFIERS -p
12356 main
12358 @end smallexample
12360 @noindent
12361 It is of course quite unlikely that both these results are correct, so
12362 it is up to you in a case like this to investigate the source of the
12363 difference, by looking at the two elaboration orders that are chosen,
12364 and figuring out which is correct, and then adding the necessary
12365 @code{Elaborate_All} pragmas to ensure the desired order.
12367 @node The Cross-Referencing Tools GNAT XREF and GNAT FIND
12368 @chapter  The Cross-Referencing Tools @code{GNAT XREF} and @code{GNAT FIND}
12369 @findex GNAT XREF
12370 @findex GNAT FIND
12372 @noindent
12373 The compiler generates cross-referencing information (unless
12374 you set the @samp{/XREF=SUPPRESS} qualifier), which are saved in the @file{.ALI} files.
12375 This information indicates where in the source each entity is declared and
12376 referenced. Note that entities in package Standard are not included, but
12377 entities in all other predefined units are included in the output.
12379 Before using any of these two tools, you need to compile successfully your
12380 application, so that GNAT gets a chance to generate the cross-referencing
12381 information.
12383 The two tools @code{GNAT XREF} and @code{GNAT FIND} take advantage of this
12384 information to provide the user with the capability to easily locate the
12385 declaration and references to an entity. These tools are quite similar,
12386 the difference being that @code{GNAT FIND} is intended for locating
12387 definitions and/or references to a specified entity or entities, whereas
12388 @code{GNAT XREF} is oriented to generating a full report of all
12389 cross-references.
12391 To use these tools, you must not compile your application using the
12392 @option{/XREF=SUPPRESS} qualifier on the @file{GNAT MAKE} command line (@inforef{The
12393 GNAT Make Program GNAT MAKE,,gnat_ug}). Otherwise, cross-referencing
12394 information will not be generated.
12396 @menu
12397 * GNAT XREF Qualifiers::
12398 * GNAT FIND Qualifiers::
12399 * Project Files for GNAT XREF and GNAT FIND::
12400 * Regular Expressions in GNAT FIND and GNAT XREF::
12401 * Examples of GNAT XREF Usage::
12402 * Examples of GNAT FIND Usage::
12403 @end menu
12405 @node GNAT XREF Qualifiers
12406 @section @code{GNAT XREF} Qualifiers
12408 @noindent
12409 The command lines for @code{GNAT XREF} is:
12410 @smallexample
12411 $ GNAT XREF [qualifiers] sourcefile1 [sourcefile2 ...]
12412 @end smallexample
12414 @noindent
12415 where
12417 @table @code
12418 @item sourcefile1, sourcefile2
12419 identifies the source files for which a report is to be generated. The
12420 'with'ed units will be processed too. You must provide at least one file.
12422 These file names are considered to be regular expressions, so for instance
12423 specifying 'source*.ADB' is the same as giving every file in the current
12424 directory whose name starts with 'source' and whose extension is 'adb'.
12426 @end table
12428 @noindent
12429 The qualifiers can be :
12430 @table @code
12431 @item /ALL_FILES
12432 If this qualifier is present, @code{GNAT FIND} and @code{GNAT XREF} will parse
12433 the read-only files found in the library search path. Otherwise, these files
12434 will be ignored. This option can be used to protect Gnat sources or your own
12435 libraries from being parsed, thus making @code{GNAT FIND} and @code{GNAT XREF}
12436 much faster, and their output much smaller.
12438 @item /SOURCE_SEARCH=direc
12439 When looking for source files also look in directory DIR. The order in which
12440 source file search is undertaken is the same as for @file{GNAT MAKE}.
12442 @item /OBJECT_SEARCH=direc
12443 When searching for library and object files, look in directory
12444 DIR. The order in which library files are searched is the same as for
12445 @file{GNAT MAKE}.
12447 @item /NOSTD_INCLUDES
12448 Do not look for sources in the system default directory.
12450 @item /NOSTD_LIBRARIES
12451 Do not look for library files in the system default directory.
12453 @item /RUNTIME_SYSTEM=@var{rts-path}
12454 @cindex @code{/RUNTIME_SYSTEM} (@code{GNAT XREF})
12455 Specifies the default location of the runtime library. Same meaning as the
12456 equivalent @code{GNAT MAKE} flag (see @ref{Qualifiers for GNAT MAKE}).
12458 @item -d
12459 If this qualifier is set @code{GNAT XREF} will output the parent type
12460 reference for each matching derived types.
12462 @item /FULL_PATHNAME
12463 If this qualifier is set, the output file names will be preceded by their
12464 directory (if the file was found in the search path). If this qualifier is
12465 not set, the directory will not be printed.
12467 @item /IGNORE_LOCALS
12468 If this qualifier is set, information is output only for library-level
12469 entities, ignoring local entities. The use of this qualifier may accelerate
12470 @code{GNAT FIND} and @code{GNAT XREF}.
12472 @item /SEARCH=direc
12473 Equivalent to @samp{/OBJECT_SEARCH=direc /SOURCE_SEARCH=direc}.
12475 @item /PROJECT=file
12476 Specify a project file to use @xref{Project Files}.
12477 By default, @code{GNAT XREF} and @code{GNAT FIND} will try to locate a
12478 project file in the current directory.
12480 If a project file is either specified or found by the tools, then the content
12481 of the source directory and object directory lines are added as if they
12482 had been specified respectively by @samp{/SOURCE_SEARCH}
12483 and @samp{OBJECT_SEARCH}.
12484 @item /UNUSED
12485 Output only unused symbols. This may be really useful if you give your
12486 main compilation unit on the command line, as @code{GNAT XREF} will then
12487 display every unused entity and 'with'ed package.
12490 @end table
12492 All these qualifiers may be in any order on the command line, and may even
12493 appear after the file names. They need not be separated by spaces, thus
12494 you can say @samp{GNAT XREF /ALL_FILES/IGNORE_LOCALS} instead of
12495 @samp{GNAT XREF /ALL_FILES /IGNORE_LOCALS}.
12497 @node GNAT FIND Qualifiers
12498 @section @code{GNAT FIND} Qualifiers
12500 @noindent
12501 The command line for @code{GNAT FIND} is:
12503 @smallexample
12504 $ GNAT FIND [qualifiers] pattern[:sourcefile[:line[:column]]]
12505       [file1 file2 ...]
12506 @end smallexample
12508 @noindent
12509 where
12511 @table @code
12512 @item pattern
12513 An entity will be output only if it matches the regular expression found
12514 in @samp{pattern}, see @xref{Regular Expressions in GNAT FIND and GNAT XREF}.
12516 Omitting the pattern is equivalent to specifying @samp{*}, which
12517 will match any entity. Note that if you do not provide a pattern, you
12518 have to provide both a sourcefile and a line.
12520 Entity names are given in Latin-1, with uppercase/lowercase equivalence
12521 for matching purposes. At the current time there is no support for
12522 8-bit codes other than Latin-1, or for wide characters in identifiers.
12524 @item sourcefile
12525 @code{GNAT FIND} will look for references, bodies or declarations
12526 of symbols referenced in @file{sourcefile}, at line @samp{line}
12527 and column @samp{column}. See @pxref{Examples of GNAT FIND Usage}
12528 for syntax examples.
12530 @item line
12531 is a decimal integer identifying the line number containing
12532 the reference to the entity (or entities) to be located.
12534 @item column
12535 is a decimal integer identifying the exact location on the
12536 line of the first character of the identifier for the
12537 entity reference. Columns are numbered from 1.
12539 @item file1 file2 ...
12540 The search will be restricted to these files. If none are given, then
12541 the search will be done for every library file in the search path.
12542 These file must appear only after the pattern or sourcefile.
12544 These file names are considered to be regular expressions, so for instance
12545 specifying 'source*.ADB' is the same as giving every file in the current
12546 directory whose name starts with 'source' and whose extension is 'adb'.
12548 Not that if you specify at least one file in this part, @code{GNAT FIND} may
12549 sometimes not be able to find the body of the subprograms...
12551 @end table
12553 At least one of 'sourcefile' or 'pattern' has to be present on
12554 the command line.
12556 The following qualifiers are available:
12557 @table @code
12559 @item /ALL_FILES
12560 If this qualifier is present, @code{GNAT FIND} and @code{GNAT XREF} will parse
12561 the read-only files found in the library search path. Otherwise, these files
12562 will be ignored. This option can be used to protect Gnat sources or your own
12563 libraries from being parsed, thus making @code{GNAT FIND} and @code{GNAT XREF}
12564 much faster, and their output much smaller.
12566 @item /SOURCE_SEARCH=direc
12567 When looking for source files also look in directory DIR. The order in which
12568 source file search is undertaken is the same as for @file{GNAT MAKE}.
12570 @item /OBJECT_SEARCH=direc
12571 When searching for library and object files, look in directory
12572 DIR. The order in which library files are searched is the same as for
12573 @file{GNAT MAKE}.
12575 @item /NOSTD_INCLUDES
12576 Do not look for sources in the system default directory.
12578 @item /NOSTD_LIBRARIES
12579 Do not look for library files in the system default directory.
12581 @item /RUNTIME_SYSTEM=@var{rts-path}
12582 @cindex @code{/RUNTIME_SYSTEM} (@code{GNAT FIND})
12583 Specifies the default location of the runtime library. Same meaning as the
12584 equivalent @code{GNAT MAKE} flag (see @ref{Qualifiers for GNAT MAKE}).
12586 @item -d
12587 If this qualifier is set, then @code{GNAT FIND} will output the parent type
12588 reference for each matching derived types.
12590 @item /EXPRESSIONS
12591 By default, @code{GNAT FIND} accept the simple regular expression set for
12592 @samp{pattern}. If this qualifier is set, then the pattern will be
12593 considered as full Unix-style regular expression.
12595 @item /FULL_PATHNAME
12596 If this qualifier is set, the output file names will be preceded by their
12597 directory (if the file was found in the search path). If this qualifier is
12598 not set, the directory will not be printed.
12600 @item /IGNORE_LOCALS
12601 If this qualifier is set, information is output only for library-level
12602 entities, ignoring local entities. The use of this qualifier may accelerate
12603 @code{GNAT FIND} and @code{GNAT XREF}.
12605 @item /SEARCH=direc
12606 Equivalent to @samp{/OBJECT_SEARCH=direc /SOURCE_SEARCH=direc}.
12608 @item /PROJECT=file
12609 Specify a project file (@pxref{Project Files}) to use.
12610 By default, @code{GNAT XREF} and @code{GNAT FIND} will try to locate a
12611 project file in the current directory.
12613 If a project file is either specified or found by the tools, then the content
12614 of the source directory and object directory lines are added as if they
12615 had been specified respectively by @samp{/SOURCE_SEARCH} and
12616 @samp{/OBJECT_SEARCH}.
12618 @item /REFERENCES
12619 By default, @code{GNAT FIND} will output only the information about the
12620 declaration, body or type completion of the entities. If this qualifier is
12621 set, the @code{GNAT FIND} will locate every reference to the entities in
12622 the files specified on the command line (or in every file in the search
12623 path if no file is given on the command line).
12625 @item /PRINT_LINES
12626 If this qualifier is set, then @code{GNAT FIND} will output the content
12627 of the Ada source file lines were the entity was found.
12629 @item -t
12630 If this qualifier is set, then @code{GNAT FIND} will output the type hierarchy for
12631 the specified type. It act like -d option but recursively from parent
12632 type to parent type. When this qualifier is set it is not possible to
12633 specify more than one file.
12635 @end table
12637 All these qualifiers may be in any order on the command line, and may even
12638 appear after the file names. They need not be separated by spaces, thus
12639 you can say @samp{GNAT XREF /ALL_FILES/IGNORE_LOCALS} instead of
12640 @samp{GNAT XREF /ALL_FILES /IGNORE_LOCALS}.
12642 As stated previously, GNAT FIND will search in every directory in the
12643 search path. You can force it to look only in the current directory if
12644 you specify @code{*} at the end of the command line.
12647 @node Project Files for GNAT XREF and GNAT FIND
12648 @section Project Files for @command{GNAT XREF} and @command{GNAT FIND}
12650 @noindent
12651 Project files allow a programmer to specify how to compile its
12652 application, where to find sources,... These files are used primarily by
12653 the Glide Ada mode, but they can also be used by the two tools
12654 @code{GNAT XREF} and @code{GNAT FIND}.
12656 A project file name must end with @file{.adp}. If a single one is
12657 present in the current directory, then @code{GNAT XREF} and @code{GNAT FIND} will
12658 extract the information from it. If multiple project files are found, none of
12659 them is read, and you have to use the @samp{-p} qualifier to specify the one
12660 you want to use.
12662 The following lines can be included, even though most of them have default
12663 values which can be used in most cases.
12664 The lines can be entered in any order in the file.
12665 Except for @samp{src_dir} and @samp{obj_dir}, you can only have one instance of
12666 each line. If you have multiple instances, only the last one is taken into
12667 account.
12669 @table @code
12670 @item src_dir=DIR         [default: "[]"]
12671 specifies a directory where to look for source files. Multiple src_dir lines
12672 can be specified and they will be searched in the order they
12673 are specified.
12675 @item obj_dir=DIR         [default: "[]"]
12676 specifies a directory where to look for object and library files. Multiple
12677 obj_dir lines can be specified and they will be searched in the order they
12678 are specified
12680 @item comp_opt=SWITCHES   [default: ""]
12681 creates a variable which can be referred to subsequently by using
12682 the @samp{$@{comp_opt@}} notation. This is intended to store the default
12683 qualifiers given to @file{GNAT MAKE} and @file{GNAT COMPILE}.
12685 @item bind_opt=SWITCHES   [default: ""]
12686 creates a variable which can be referred to subsequently by using
12687 the @samp{$@{bind_opt@}} notation. This is intended to store the default
12688 qualifiers given to @file{GNAT BIND}.
12690 @item link_opt=SWITCHES   [default: ""]
12691 creates a variable which can be referred to subsequently by using
12692 the @samp{$@{link_opt@}} notation. This is intended to store the default
12693 qualifiers given to @file{GNAT LINK}.
12695 @item main=EXECUTABLE     [default: ""]
12696 specifies the name of the executable for the application. This variable can
12697 be referred to in the following lines by using the @samp{$@{main@}} notation.
12699 @item comp_cmd=COMMAND    [default: "GNAT COMPILE /SEARCH=$@{src_dir@} /DEBUG /TRY_SEMANTICS"]
12700 specifies the command used to compile a single file in the application.
12702 @item make_cmd=COMMAND    [default: "GNAT MAKE $@{main@} /SOURCE_SEARCH=$@{src_dir@} /OBJECT_SEARCH=$@{obj_dir@} /DEBUG /TRY_SEMANTICS /COMPILER_QUALIFIERS $@{comp_opt@} /BINDER_QUALIFIERS $@{bind_opt@} /LINKER_QUALIFIERS $@{link_opt@}"]
12703 specifies the command used to recompile the whole application.
12705 @item run_cmd=COMMAND     [default: "$@{main@}"]
12706 specifies the command used to run the application.
12708 @item debug_cmd=COMMAND   [default: "GDB $@{main@}"]
12709 specifies the command used to debug the application
12711 @end table
12713 @code{GNAT XREF} and @code{GNAT FIND} only take into account the @samp{src_dir}
12714 and @samp{obj_dir} lines, and ignore the others.
12716 @node Regular Expressions in GNAT FIND and GNAT XREF
12717 @section  Regular Expressions in @code{GNAT FIND} and @code{GNAT XREF}
12719 @noindent
12720 As specified in the section about @code{GNAT FIND}, the pattern can be a
12721 regular expression. Actually, there are to set of regular expressions
12722 which are recognized by the program :
12724 @table @code
12725 @item globbing patterns
12726 These are the most usual regular expression. They are the same that you
12727 generally used in a Unix shell command line, or in a DOS session.
12729 Here is a more formal grammar :
12730 @smallexample
12731 @group
12732 @iftex
12733 @leftskip=.5cm
12734 @end iftex
12735 regexp ::= term
12736 term   ::= elmt            -- matches elmt
12737 term   ::= elmt elmt       -- concatenation (elmt then elmt)
12738 term   ::= *               -- any string of 0 or more characters
12739 term   ::= ?               -- matches any character
12740 term   ::= [char @{char@}] -- matches any character listed
12741 term   ::= [char - char]   -- matches any character in range
12742 @end group
12743 @end smallexample
12745 @item full regular expression
12746 The second set of regular expressions is much more powerful. This is the
12747 type of regular expressions recognized by utilities such a @file{grep}.
12749 The following is the form of a regular expression, expressed in Ada
12750 reference manual style BNF is as follows
12752 @smallexample
12753 @iftex
12754 @leftskip=.5cm
12755 @end iftex
12756 @group
12757 regexp ::= term @{| term@} -- alternation (term or term ...)
12759 term ::= item @{item@}     -- concatenation (item then item)
12761 item ::= elmt              -- match elmt
12762 item ::= elmt *            -- zero or more elmt's
12763 item ::= elmt +            -- one or more elmt's
12764 item ::= elmt ?            -- matches elmt or nothing
12765 @end group
12766 @group
12767 elmt ::= nschar            -- matches given character
12768 elmt ::= [nschar @{nschar@}]   -- matches any character listed
12769 elmt ::= [^ nschar @{nschar@}] -- matches any character not listed
12770 elmt ::= [char - char]     -- matches chars in given range
12771 elmt ::= \ char            -- matches given character
12772 elmt ::= .                 -- matches any single character
12773 elmt ::= ( regexp )        -- parens used for grouping
12775 char ::= any character, including special characters
12776 nschar ::= any character except ()[].*+?^
12777 @end group
12778 @end smallexample
12780 Following are a few examples :
12782 @table @samp
12783 @item abcde|fghi
12784 will match any of the two strings 'abcde' and 'fghi'.
12786 @item abc*d
12787 will match any string like 'abd', 'abcd', 'abccd', 'abcccd', and so on
12789 @item [a-z]+
12790 will match any string which has only lowercase characters in it (and at
12791 least one character
12793 @end table
12794 @end table
12796 @node Examples of GNAT XREF Usage
12797 @section Examples of @code{GNAT XREF} Usage
12799 @subsection General Usage
12801 @noindent
12802 For the following examples, we will consider the following units :
12804 @smallexample
12805 @group
12806 @cartouche
12807 MAIN.ADS:
12808 1: @b{with} Bar;
12809 2: @b{package} Main @b{is}
12810 3:     @b{procedure} Foo (B : @b{in} Integer);
12811 4:     C : Integer;
12812 5: @b{private}
12813 6:     D : Integer;
12814 7: @b{end} Main;
12816 MAIN.ADB:
12817 1: @b{package body} Main @b{is}
12818 2:     @b{procedure} Foo (B : @b{in} Integer) @b{is}
12819 3:     @b{begin}
12820 4:        C := B;
12821 5:        D := B;
12822 6:        Bar.Print (B);
12823 7:        Bar.Print (C);
12824 8:     @b{end} Foo;
12825 9: @b{end} Main;
12827 BAR.ADS:
12828 1: @b{package} Bar @b{is}
12829 2:     @b{procedure} Print (B : Integer);
12830 3: @b{end} bar;
12831 @end cartouche
12832 @end group
12833 @end smallexample
12835 @table @code
12837 @noindent
12838 The first thing to do is to recompile your application (for instance, in
12839 that case just by doing a @samp{GNAT MAKE main}, so that GNAT generates
12840 the cross-referencing information.
12841 You can then issue any of the following commands:
12843 @item GNAT XREF MAIN.ADB
12844 @code{GNAT XREF} generates cross-reference information for MAIN.ADB
12845 and every unit 'with'ed by MAIN.ADB.
12847 The output would be:
12848 @smallexample
12849 @iftex
12850 @leftskip=0cm
12851 @end iftex
12852 B                                                      Type: Integer
12853   Decl: BAR.ADS           2:22
12854 B                                                      Type: Integer
12855   Decl: MAIN.ADS          3:20
12856   Body: MAIN.ADB          2:20
12857   Ref:  MAIN.ADB          4:13     5:13     6:19
12858 Bar                                                    Type: Unit
12859   Decl: BAR.ADS           1:9
12860   Ref:  MAIN.ADB          6:8      7:8
12861        MAIN.ADS           1:6
12862 C                                                      Type: Integer
12863   Decl: MAIN.ADS          4:5
12864   Modi: MAIN.ADB          4:8
12865   Ref:  MAIN.ADB          7:19
12866 D                                                      Type: Integer
12867   Decl: MAIN.ADS          6:5
12868   Modi: MAIN.ADB          5:8
12869 Foo                                                    Type: Unit
12870   Decl: MAIN.ADS          3:15
12871   Body: MAIN.ADB          2:15
12872 Main                                                    Type: Unit
12873   Decl: MAIN.ADS          2:9
12874   Body: MAIN.ADB          1:14
12875 Print                                                   Type: Unit
12876   Decl: BAR.ADS           2:15
12877   Ref:  MAIN.ADB          6:12     7:12
12878 @end smallexample
12880 @noindent
12881 that is the entity @code{Main} is declared in MAIN.ADS, line 2, column 9,
12882 its body is in MAIN.ADB, line 1, column 14 and is not referenced any where.
12884 The entity @code{Print} is declared in BAR.ADS, line 2, column 15 and it
12885 it referenced in MAIN.ADB, line 6 column 12 and line 7 column 12.
12887 @item GNAT XREF PACKAGE1.ADB PACKAGE2.ADS
12888 @code{GNAT XREF} will generates cross-reference information for
12889 PACKAGE1.ADB, PACKAGE2.ADS and any other package 'with'ed by any
12890 of these.
12892 @end table
12895 @node Examples of GNAT FIND Usage
12896 @section Examples of @code{GNAT FIND} Usage
12898 @table @code
12900 @item GNAT FIND /FULL_PATHNAME xyz:MAIN.ADB
12901 Find declarations for all entities xyz referenced at least once in
12902 MAIN.ADB. The references are search in every library file in the search
12903 path.
12905 The directories will be printed as well (as the @samp{/FULL_PATHNAME}
12906 qualifier is set)
12908 The output will look like:
12909 @smallexample
12910 [directory]MAIN.ADS:106:14: xyz <= declaration
12911 [directory]MAIN.ADB:24:10: xyz <= body
12912 [directory]FOO.ADS:45:23: xyz <= declaration
12913 @end smallexample
12915 @noindent
12916 that is to say, one of the entities xyz found in MAIN.ADB is declared at
12917 line 12 of MAIN.ADS (and its body is in MAIN.ADB), and another one is
12918 declared at line 45 of FOO.ADS
12920 @item GNAT FIND /FULL_PATHNAME/SOURCE_LINE xyz:MAIN.ADB
12921 This is the same command as the previous one, instead @code{GNAT FIND} will
12922 display the content of the Ada source file lines.
12924 The output will look like:
12926 @smallexample
12927 [directory]MAIN.ADS:106:14: xyz <= declaration
12928    procedure xyz;
12929 [directory]MAIN.ADB:24:10: xyz <= body
12930    procedure xyz is
12931 [directory]FOO.ADS:45:23: xyz <= declaration
12932    xyz : Integer;
12933 @end smallexample
12935 @noindent
12936 This can make it easier to find exactly the location your are looking
12937 for.
12939 @item GNAT FIND /REFERENCES "*x*":MAIN.ADS:123 FOO.ADB
12940 Find references to all entities containing an x that are
12941 referenced on line 123 of MAIN.ADS.
12942 The references will be searched only in MAIN.ADB and FOO.ADB.
12944 @item GNAT FIND MAIN.ADS:123
12945 Find declarations and bodies for all entities that are referenced on
12946 line 123 of MAIN.ADS.
12948 This is the same as @code{GNAT FIND "*":MAIN.ADB:123}.
12950 @item GNAT FIND [mydir]MAIN.ADB:123:45
12951 Find the declaration for the entity referenced at column 45 in
12952 line 123 of file MAIN.ADB in directory mydir. Note that it
12953 is usual to omit the identifier name when the column is given,
12954 since the column position identifies a unique reference.
12956 The column has to be the beginning of the identifier, and should not
12957 point to any character in the middle of the identifier.
12959 @end table
12961 @node File Name Krunching Using GNAT KRUNCH
12962 @chapter File Name Krunching Using @code{GNAT KRUNCH}
12963 @findex GNAT KRUNCH
12965 @noindent
12966 This chapter discusses the method used by the compiler to shorten
12967 the default file names chosen for Ada units so that they do not
12968 exceed the maximum length permitted. It also describes the
12969 @code{GNAT KRUNCH} utility that can be used to determine the result of
12970 applying this shortening.
12971 @menu
12972 * About GNAT KRUNCH::
12973 * Using GNAT KRUNCH::
12974 * Krunching Method::
12975 * Examples of GNAT KRUNCH Usage::
12976 @end menu
12978 @node About GNAT KRUNCH
12979 @section About @code{GNAT KRUNCH}
12981 @noindent
12982 The default file naming rule in GNAT
12983 is that the file name must be derived from
12984 the unit name. The exact default rule is as follows:
12985 @itemize @bullet
12986 @item
12987 Take the unit name and replace all dots by hyphens.
12988 @item
12989 If such a replacement occurs in the
12990 second character position of a name, and the first character is
12991 A, G, S, or I then replace the dot by the character
12992 $ (dollar sign)
12993 instead of a minus.
12994 @end itemize
12995 The reason for this exception is to avoid clashes
12996 with the standard names for children of System, Ada, Interfaces,
12997 and GNAT, which use the prefixes S- A- I- and G-
12998 respectively.
13000 The @code{/FILE_NAME_MAX_LENGTH=@var{nn}}
13001 qualifier of the compiler activates a "krunching"
13002 circuit that limits file names to nn characters (where nn is a decimal
13003 integer). For example, using OpenVMS,
13004 where the maximum file name length is
13005 39, the value of nn is usually set to 39, but if you want to generate
13006 a set of files that would be usable if ported to a system with some
13007 different maximum file length, then a different value can be specified.
13008 The default value of 39 for OpenVMS need not be specified.
13010 The @code{GNAT KRUNCH} utility can be used to determine the krunched name for
13011 a given file, when krunched to a specified maximum length.
13013 @node Using GNAT KRUNCH
13014 @section Using @code{GNAT KRUNCH}
13016 @noindent
13017 The @code{GNAT KRUNCH} command has the form
13020 @smallexample
13021 $ GNAT KRUNCH @var{name} /COUNT=nn
13022 @end smallexample
13024 @noindent
13025 @var{name} can be an Ada name with dots or the GNAT name of the unit,
13026 where the dots representing child units or subunit are replaced by
13027 hyphens. The only confusion arises if a name ends in @code{.ADS} or
13028 @code{.ADB}. @code{GNAT KRUNCH} takes this to be an extension if there are
13029 no other dots in the name.
13031 @var{length} represents the length of the krunched name. The default
13032 when no argument is given is 39 characters. A length of zero stands for
13033 unlimited, in other words do not chop except for system files which are
13034 always 39.
13036 @noindent
13037 The output is the krunched name. The output has an extension only if the
13038 original argument was a file name with an extension.
13040 @node Krunching Method
13041 @section Krunching Method
13043 @noindent
13044 The initial file name is determined by the name of the unit that the file
13045 contains. The name is formed by taking the full expanded name of the
13046 unit and replacing the separating dots with hyphens and
13047 using uppercase
13048 for all letters, except that a hyphen in the second character position is
13049 replaced by a dollar sign if the first character is
13050 A, I, G, or S.
13051 The extension is @code{.ADS} for a
13052 specification and @code{.ADB} for a body.
13053 Krunching does not affect the extension, but the file name is shortened to
13054 the specified length by following these rules:
13056 @itemize @bullet
13057 @item
13058 The name is divided into segments separated by hyphens, tildes or
13059 underscores and all hyphens, tildes, and underscores are
13060 eliminated. If this leaves the name short enough, we are done.
13062 @item
13063 If the name is too long, the longest segment is located (left-most if there are two
13064 of equal length), and shortened by dropping its last character. This is
13065 repeated until the name is short enough.
13067 As an example, consider the krunching of @*@file{OUR-STRINGS-WIDE_FIXED.ADB}
13068 to fit the name into 8 characters as required by some operating systems.
13070 @smallexample
13071 our-strings-wide_fixed 22
13072 our strings wide fixed 19
13073 our string  wide fixed 18
13074 our strin   wide fixed 17
13075 our stri    wide fixed 16
13076 our stri    wide fixe  15
13077 our str     wide fixe  14
13078 our str     wid  fixe  13
13079 our str     wid  fix   12
13080 ou  str     wid  fix   11
13081 ou  st      wid  fix   10
13082 ou  st      wi   fix   9
13083 ou  st      wi   fi    8
13084 Final file name: OUSTWIFI.ADB
13085 @end smallexample
13087 @item
13088 The file names for all predefined units are always krunched to eight
13089 characters. The krunching of these predefined units uses the following
13090 special prefix replacements:
13092 @table @file
13093 @item ada-
13094 replaced by @file{A-}
13096 @item gnat-
13097 replaced by @file{G-}
13099 @item interfaces-
13100 replaced by @file{I-}
13102 @item system-
13103 replaced by @file{S-}
13104 @end table
13106 These system files have a hyphen in the second character position. That
13107 is why normal user files replace such a character with a
13108 dollar sign, to
13109 avoid confusion with system file names.
13111 As an example of this special rule, consider
13112 @*@file{ADA-STRINGS-WIDE_FIXED.ADB}, which gets krunched as follows:
13114 @smallexample
13115 ada-strings-wide_fixed 22
13116 a-  strings wide fixed 18
13117 a-  string  wide fixed 17
13118 a-  strin   wide fixed 16
13119 a-  stri    wide fixed 15
13120 a-  stri    wide fixe  14
13121 a-  str     wide fixe  13
13122 a-  str     wid  fixe  12
13123 a-  str     wid  fix   11
13124 a-  st      wid  fix   10
13125 a-  st      wi   fix   9
13126 a-  st      wi   fi    8
13127 Final file name: A-STWIFI.ADB
13128 @end smallexample
13129 @end itemize
13131 Of course no file shortening algorithm can guarantee uniqueness over all
13132 possible unit names, and if file name krunching is used then it is your
13133 responsibility to ensure that no name clashes occur. The utility
13134 program @code{GNAT KRUNCH} is supplied for conveniently determining the
13135 krunched name of a file.
13137 @node Examples of GNAT KRUNCH Usage
13138 @section Examples of @code{GNAT KRUNCH} Usage
13140 @smallexample
13141 @iftex
13142 @leftskip=0cm
13143 @end iftex
13144 $ GNAT KRUNCH VERY_LONG_UNIT_NAME.ADS/count=6 --> VLUNNA.ADS
13145 $ GNAT KRUNCH VERY_LONG_UNIT_NAME.ADS/count=0 --> VERY_LONG_UNIT_NAME.ADS
13146 @end smallexample
13148 @node Preprocessing Using GNAT PREPROCESS
13149 @chapter Preprocessing Using @code{GNAT PREPROCESS}
13150 @findex GNAT PREPROCESS
13152 @noindent
13153 The @code{GNAT PREPROCESS} utility provides
13154 a simple preprocessing capability for Ada programs.
13155 It is designed for use with GNAT, but is not dependent on any special
13156 features of GNAT.
13158 @menu
13159 * Using GNAT PREPROCESS::
13160 * Qualifiers for GNAT PREPROCESS::
13161 * Form of Definitions File::
13162 * Form of Input Text for GNAT PREPROCESS::
13163 @end menu
13165 @node Using GNAT PREPROCESS
13166 @section Using @code{GNAT PREPROCESS}
13168 @noindent
13169 To call @code{GNAT PREPROCESS} use
13171 @smallexample
13172 $ GNAT PREPROCESS [-bcrsu] [-Dsymbol=value] infile outfile [deffile]
13173 @end smallexample
13175 @noindent
13176 where
13177 @table @code
13178 @item infile
13179 is the full name of the input file, which is an Ada source
13180 file containing preprocessor directives.
13182 @item outfile
13183 is the full name of the output file, which is an Ada source
13184 in standard Ada form. When used with GNAT, this file name will
13185 normally have an ads or adb suffix.
13187 @item deffile
13188 is the full name of a text file containing definitions of
13189 symbols to be referenced by the preprocessor. This argument is
13190 optional, and can be replaced by the use of the @code{-D} qualifier.
13192 @item qualifiers
13193 is an optional sequence of qualifiers as described in the next section.
13194 @end table
13196 @node Qualifiers for GNAT PREPROCESS
13197 @section Qualifiers for @code{GNAT PREPROCESS}
13199 @table @code
13201 @item /BLANK_LINES
13202 Causes both preprocessor lines and the lines deleted by
13203 preprocessing to be replaced by blank lines in the output source file,
13204 preserving line numbers in the output file.
13206 @item /COMMENTS
13207 Causes both preprocessor lines and the lines deleted
13208 by preprocessing to be retained in the output source as comments marked
13209 with the special string "--! ". This option will result in line numbers
13210 being preserved in the output file.
13212 @item -Dsymbol=value
13213 Defines a new symbol, associated with value. If no value is given on the
13214 command line, then symbol is considered to be @code{True}. This qualifier
13215 can be used in place of a definition file.
13217 @item /REMOVE (default)
13218 This is the default setting which causes lines deleted by preprocessing
13219 to be entirely removed from the output file.
13221 @item /REFERENCE
13222 Causes a @code{Source_Reference} pragma to be generated that
13223 references the original input file, so that error messages will use
13224 the file name of this original file. The use of this qualifier implies
13225 that preprocessor lines are not to be removed from the file, so its
13226 use will force @code{/BLANK_LINES} mode if
13227 @code{/COMMENTS}
13228 has not been specified explicitly.
13230 Note that if the file to be preprocessed contains multiple units, then
13231 it will be necessary to @code{GNAT CHOP} the output file from
13232 @code{GNAT PREPROCESS}. If a @code{Source_Reference} pragma is present
13233 in the preprocessed file, it will be respected by
13234 @code{GNAT CHOP /REFERENCE}
13235 so that the final chopped files will correctly refer to the original
13236 input source file for @code{GNAT PREPROCESS}.
13238 @item /SYMBOLS
13239 Causes a sorted list of symbol names and values to be
13240 listed on the standard output file.
13242 @item /UNDEFINED
13243 Causes undefined symbols to be treated as having the value FALSE in the context
13244 of a preprocessor test. In the absence of this option, an undefined symbol in
13245 a @code{#if} or @code{#elsif} test will be treated as an error.
13247 @end table
13250 @node Form of Definitions File
13251 @section Form of Definitions File
13253 @noindent
13254 The definitions file contains lines of the form
13256 @smallexample
13257 symbol := value
13258 @end smallexample
13260 @noindent
13261 where symbol is an identifier, following normal Ada (case-insensitive)
13262 rules for its syntax, and value is one of the following:
13264 @itemize @bullet
13265 @item
13266 Empty, corresponding to a null substitution
13267 @item
13268 A string literal using normal Ada syntax
13269 @item
13270 Any sequence of characters from the set
13271 (letters, digits, period, underline).
13272 @end itemize
13274 @noindent
13275 Comment lines may also appear in the definitions file, starting with
13276 the usual @code{--},
13277 and comments may be added to the definitions lines.
13279 @node Form of Input Text for GNAT PREPROCESS
13280 @section Form of Input Text for @code{GNAT PREPROCESS}
13282 @noindent
13283 The input text may contain preprocessor conditional inclusion lines,
13284 as well as general symbol substitution sequences.
13286 The preprocessor conditional inclusion commands have the form
13288 @smallexample
13289 @group
13290 @cartouche
13291 #if @i{expression} [then]
13292    lines
13293 #elsif @i{expression} [then]
13294    lines
13295 #elsif @i{expression} [then]
13296    lines
13298 #else
13299    lines
13300 #end if;
13301 @end cartouche
13302 @end group
13303 @end smallexample
13305 @noindent
13306 In this example, @i{expression} is defined by the following grammar:
13307 @smallexample
13308 @i{expression} ::=  <symbol>
13309 @i{expression} ::=  <symbol> = "<value>"
13310 @i{expression} ::=  <symbol> = <symbol>
13311 @i{expression} ::=  <symbol> 'Defined
13312 @i{expression} ::=  not @i{expression}
13313 @i{expression} ::=  @i{expression} and @i{expression}
13314 @i{expression} ::=  @i{expression} or @i{expression}
13315 @i{expression} ::=  @i{expression} and then @i{expression}
13316 @i{expression} ::=  @i{expression} or else @i{expression}
13317 @i{expression} ::=  ( @i{expression} )
13318 @end smallexample
13320 @noindent
13321 For the first test (@i{expression} ::= <symbol>) the symbol must have
13322 either the value true or false, that is to say the right-hand of the
13323 symbol definition must be one of the (case-insensitive) literals
13324 @code{True} or @code{False}. If the value is true, then the
13325 corresponding lines are included, and if the value is false, they are
13326 excluded.
13328 The test (@i{expression} ::= <symbol> @code{'Defined}) is true only if
13329 the symbol has been defined in the definition file or by a @code{-D}
13330 qualifier on the command line. Otherwise, the test is false.
13332 The equality tests are case insensitive, as are all the preprocessor lines.
13334 If the symbol referenced is not defined in the symbol definitions file,
13335 then the effect depends on whether or not qualifier @code{-u}
13336 is specified. If so, then the symbol is treated as if it had the value
13337 false and the test fails. If this qualifier is not specified, then
13338 it is an error to reference an undefined symbol. It is also an error to
13339 reference a symbol that is defined with a value other than @code{True}
13340 or @code{False}.
13342 The use of the @code{not} operator inverts the sense of this logical test, so
13343 that the lines are included only if the symbol is not defined.
13344 The @code{then} keyword is optional as shown
13346 The @code{#} must be the first non-blank character on a line, but
13347 otherwise the format is free form. Spaces or tabs may appear between
13348 the @code{#} and the keyword. The keywords and the symbols are case
13349 insensitive as in normal Ada code. Comments may be used on a
13350 preprocessor line, but other than that, no other tokens may appear on a
13351 preprocessor line. Any number of @code{elsif} clauses can be present,
13352 including none at all. The @code{else} is optional, as in Ada.
13354 The @code{#} marking the start of a preprocessor line must be the first
13355 non-blank character on the line, i.e. it must be preceded only by
13356 spaces or horizontal tabs.
13358 Symbol substitution outside of preprocessor lines is obtained by using
13359 the sequence
13361 @smallexample
13362 $symbol
13363 @end smallexample
13365 @noindent
13366 anywhere within a source line, except in a comment or within a
13367 string literal. The identifier
13368 following the @code{$} must match one of the symbols defined in the symbol
13369 definition file, and the result is to substitute the value of the
13370 symbol in place of @code{$symbol} in the output file.
13372 Note that although the substitution of strings within a string literal
13373 is not possible, it is possible to have a symbol whose defined value is
13374 a string literal. So instead of setting XYZ to @code{hello} and writing:
13376 @smallexample
13377 Header : String := "$XYZ";
13378 @end smallexample
13380 @noindent
13381 you should set XYZ to @code{"hello"} and write:
13383 @smallexample
13384 Header : String := $XYZ;
13385 @end smallexample
13387 @noindent
13388 and then the substitution will occur as desired.
13390 @node The GNAT Run-Time Library Builder GNAT LIBRARY
13391 @chapter The GNAT Run-Time Library Builder @code{GNAT LIBRARY}
13392 @findex GNAT LIBRARY
13393 @cindex Library builder
13395 @noindent
13396 @code{GNAT LIBRARY} is a tool for rebuilding the GNAT run time with user
13397 supplied configuration pragmas.
13399 @menu
13400 * Running GNAT LIBRARY::
13401 * Qualifiers for GNAT LIBRARY::
13402 * Examples of GNAT LIBRARY Usage::
13403 @end menu
13405 @node Running GNAT LIBRARY
13406 @section Running @code{GNAT LIBRARY}
13408 @noindent
13409 The @code{GNAT LIBRARY} command has the form
13411 @smallexample
13412 $ GNAT LIBRARY /[CREATE | SET | DELETE]=directory [/CONFIG=file]
13413 @end smallexample
13415 @node Qualifiers for GNAT LIBRARY
13416 @section Qualifiers for @code{GNAT LIBRARY}
13418 @noindent
13419 @code{GNAT LIBRARY} recognizes the following qualifiers:
13421 @table @code
13422 @item /CREATE=directory
13423 @cindex @code{/CREATE=directory} (@code{GNAT LIBRARY})
13424      Create the new run-time library in the specified directory.
13426 @item /SET=directory
13427 @cindex @code{/SET=directory} (@code{GNAT LIBRARY})
13428      Make the library in the specified directory the current run-time
13429      library.
13431 @item /DELETE=directory
13432 @cindex @code{/DELETE=directory} (@code{GNAT LIBRARY})
13433      Delete the run-time library in the specified directory.
13435 @item /CONFIG=file
13436 @cindex @code{/CONFIG=file} (@code{GNAT LIBRARY})
13437      With /CREATE:
13438      Use the configuration pragmas in the specified file when building
13439      the library.
13441      With /SET:
13442      Use the configuration pragmas in the specified file when compiling.
13444 @end table
13446 @node Examples of GNAT LIBRARY Usage
13447 @section Example of @code{GNAT LIBRARY} Usage
13449 @smallexample
13450 Contents of VAXFLOAT.ADC:
13451 pragma Float_Representation (VAX_Float);
13453 $ GNAT LIBRARY /CREATE=[.VAXFLOAT] /CONFIG=VAXFLOAT.ADC
13455 GNAT LIBRARY rebuilds the run-time library in directory [.VAXFLOAT]
13457 @end smallexample
13459 @node The GNAT Library Browser GNAT LIST
13460 @chapter The GNAT Library Browser @code{GNAT LIST}
13461 @findex GNAT LIST
13462 @cindex Library browser
13464 @noindent
13465 @code{GNAT LIST} is a tool that outputs information about compiled
13466 units. It gives the relationship between objects, unit names and source
13467 files. It can also be used to check the source dependencies of a unit
13468 as well as various characteristics.
13470 @menu
13471 * Running GNAT LIST::
13472 * Qualifiers for GNAT LIST::
13473 * Examples of GNAT LIST Usage::
13474 @end menu
13476 @node Running GNAT LIST
13477 @section Running @code{GNAT LIST}
13479 @noindent
13480 The @code{GNAT LIST} command has the form
13482 @smallexample
13483 $ GNAT LIST qualifiers @var{object_or_ali_file}
13484 @end smallexample
13486 @noindent
13487 The main argument is the list of object or @file{ali} files
13488 (@pxref{The Ada Library Information Files})
13489 for which information is requested.
13491 In normal mode, without additional option, @code{GNAT LIST} produces a
13492 four-column listing. Each line represents information for a specific
13493 object. The first column gives the full path of the object, the second
13494 column gives the name of the principal unit in this object, the third
13495 column gives the status of the source and the fourth column gives the
13496 full path of the source representing this unit.
13497 Here is a simple example of use:
13499 @smallexample
13500 $ GNAT LIST *.OBJ
13501 []DEMO1.OBJ            demo1            DIF DEMO1.ADB
13502 []DEMO2.OBJ            demo2             OK DEMO2.ADB
13503 []HELLO.OBJ            h1                OK HELLO.ADB
13504 []INSTR-CHILD.OBJ      instr.child      MOK INSTR-CHILD.ADB
13505 []INSTR.OBJ            instr             OK INSTR.ADB
13506 []TEF.OBJ              tef              DIF TEF.ADB
13507 []TEXT_IO_EXAMPLE.OBJ  text_io_example   OK TEXT_IO_EXAMPLE.ADB
13508 []TGEF.OBJ             tgef             DIF TGEF.ADB
13509 @end smallexample
13511 @noindent
13512 The first line can be interpreted as follows: the main unit which is
13513 contained in
13514 object file @file{DEMO1.OBJ} is demo1, whose main source is in
13515 @file{DEMO1.ADB}. Furthermore, the version of the source used for the
13516 compilation of demo1 has been modified (DIF). Each source file has a status
13517 qualifier which can be:
13519 @table @code
13520 @item OK (unchanged)
13521 The version of the source file used for the compilation of the
13522 specified unit corresponds exactly to the actual source file.
13524 @item MOK (slightly modified)
13525 The version of the source file used for the compilation of the
13526 specified unit differs from the actual source file but not enough to
13527 require recompilation. If you use GNAT MAKE with the qualifier
13528 @code{/MINIMAL_RECOMPILATION}, a file marked
13529 MOK will not be recompiled.
13531 @item DIF (modified)
13532 No version of the source found on the path corresponds to the source
13533 used to build this object.
13535 @item ??? (file not found)
13536 No source file was found for this unit.
13538 @item HID (hidden,  unchanged version not first on PATH)
13539 The version of the source that corresponds exactly to the source used
13540 for compilation has been found on the path but it is hidden by another
13541 version of the same source that has been modified.
13543 @end table
13545 @node Qualifiers for GNAT LIST
13546 @section Qualifiers for @code{GNAT LIST}
13548 @noindent
13549 @code{GNAT LIST} recognizes the following qualifiers:
13551 @table @code
13552 @item /ALL_UNITS
13553 @cindex @code{/ALL_UNITS} (@code{GNAT LIST})
13554 Consider all units, including those of the predefined Ada library.
13555 Especially useful with @code{/DEPENDENCIES}.
13557 @item /DEPENDENCIES
13558 @cindex @code{/DEPENDENCIES} (@code{GNAT LIST})
13559 List sources from which specified units depend on.
13561 @item /OUTPUT=OPTIONS
13562 @cindex @code{/OUTPUT=OPTIONS} (@code{GNAT LIST})
13563 Output the list of options.
13565 @item /OUTPUT=OBJECTS
13566 @cindex @code{/OUTPUT=OBJECTS} (@code{GNAT LIST})
13567 Only output information about object files.
13569 @item /OUTPUT=SOURCES
13570 @cindex @code{/OUTPUT=SOURCES} (@code{GNAT LIST})
13571 Only output information about source files.
13573 @item /OUTPUT=UNITS
13574 @cindex @code{/OUTPUT=UNITS} (@code{GNAT LIST})
13575 Only output information about compilation units.
13577 @item /OBJECT_SEARCH=@var{dir}
13578 @itemx /SOURCE_SEARCH=@var{dir}
13579 @itemx /SEARCH=@var{dir}
13580 @itemx  /NOCURRENT_DIRECTORY
13581 @itemx /NOSTD_INCLUDES
13582 Source path manipulation. Same meaning as the equivalent @code{GNAT MAKE} flags
13583 (see @ref{Qualifiers for GNAT MAKE}).
13585 @item /RUNTIME_SYSTEM=@var{rts-path}
13586 @cindex @code{/RUNTIME_SYSTEM} (@code{GNAT LIST})
13587 Specifies the default location of the runtime library. Same meaning as the
13588 equivalent @code{GNAT MAKE} flag (see @ref{Qualifiers for GNAT MAKE}).
13590 @item /OUTPUT=VERBOSE
13591 @cindex @code{/OUTPUT=VERBOSE} (@code{GNAT LIST})
13592 Verbose mode. Output the complete source and object paths. Do not use
13593 the default column layout but instead use long format giving as much as
13594 information possible on each requested units, including special
13595 characteristics such as:
13597 @table @code
13598 @item  Preelaborable
13599 The unit is preelaborable in the Ada 95 sense.
13601 @item No_Elab_Code
13602 No elaboration code has been produced by the compiler for this unit.
13604 @item Pure
13605 The unit is pure in the Ada 95 sense.
13607 @item Elaborate_Body
13608 The unit contains a pragma Elaborate_Body.
13610 @item Remote_Types
13611 The unit contains a pragma Remote_Types.
13613 @item Shared_Passive
13614 The unit contains a pragma Shared_Passive.
13616 @item Predefined
13617 This unit is part of the predefined environment and cannot be modified
13618 by the user.
13620 @item Remote_Call_Interface
13621 The unit contains a pragma Remote_Call_Interface.
13623 @end table
13625 @end table
13627 @node Examples of GNAT LIST Usage
13628 @section Example of @code{GNAT LIST} Usage
13630 @smallexample
13631 GNAT LIST /DEPENDENCIES /OUTPUT=SOURCES /ALL_UNITS DEMO1.ADB
13633 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]ADA.ADS
13634 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]A-FINALI.ADS
13635 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]A-FILICO.ADS
13636 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]A-STREAM.ADS
13637 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]A-TAGS.ADS
13638 DEMO1.ADB
13639 GEN_LIST.ADS
13640 GEN_LIST.ADB
13641 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]GNAT.ADS
13642 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]G-IO.ADS
13643 INSTR.ADS
13644 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]SYSTEM.ADS
13645 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-EXCTAB.ADS
13646 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-FINIMP.ADS
13647 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-FINROO.ADS
13648 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-SECSTA.ADS
13649 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-STALIB.ADS
13650 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-STOELE.ADS
13651 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-STRATT.ADS
13652 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-TASOLI.ADS
13653 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-UNSTYP.ADS
13654 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]UNCHCONV.ADS
13655 @end smallexample
13658 @node Finding Memory Problems with GNAT Debug Pool
13659 @chapter Finding Memory Problems with GNAT Debug Pool
13660 @findex Debug Pool
13661 @cindex storage, pool, memory corruption
13663 @noindent
13664 The use of unchecked deallocation and unchecked conversion can easily
13665 lead to incorrect memory references. The problems generated by such
13666 references are usually difficult to tackle because the symptoms can be
13667 very remote from the origin of the problem. In such cases, it is
13668 very helpful to detect the problem as early as possible. This is the
13669 purpose of the Storage Pool provided by @code{GNAT.Debug_Pools}.
13671 @noindent
13672 In order to use the GNAT specific debugging pool, the user must
13673 associate a debug pool object with each of the access types that may be
13674 related to suspected memory problems. See Ada Reference Manual
13675 13.11.
13676 @smallexample
13677 @b{type} Ptr @b{is} @b{access} Some_Type;
13678 Pool : GNAT.Debug_Pools.Debug_Pool;
13679 @b{for} Ptr'Storage_Pool @b{use} Pool;
13680 @end smallexample
13682 @code{GNAT.Debug_Pools} is derived from of a GNAT-specific kind of
13683 pool: the Checked_Pool. Such pools, like standard Ada storage pools,
13684 allow the user to redefine allocation and deallocation strategies. They
13685 also provide a checkpoint for each dereference, through the use of
13686 the primitive operation @code{Dereference} which is implicitly called at
13687 each dereference of an access value.
13689 Once an access type has been associated with a debug pool, operations on
13690 values of the type may raise four distinct exceptions,
13691 which correspond to four potential kinds of memory corruption:
13692 @itemize @bullet
13693 @item
13694 @code{GNAT.Debug_Pools.Accessing_Not_Allocated_Storage}
13695 @item
13696 @code{GNAT.Debug_Pools.Accessing_Deallocated_Storage}
13697 @item
13698 @code{GNAT.Debug_Pools.Freeing_Not_Allocated_Storage}
13699 @item
13700 @code{GNAT.Debug_Pools.Freeing_Deallocated_Storage }
13701 @end itemize
13703 @noindent
13704 For types associated with a Debug_Pool, dynamic allocation is performed using
13705 the standard
13706 GNAT allocation routine. References to all allocated chunks of memory
13707 are kept in an internal dictionary. The deallocation strategy consists
13708 in not releasing the memory to the underlying system but rather to fill
13709 it with a memory pattern easily recognizable during debugging sessions:
13710 The memory pattern is the old IBM hexadecimal convention: 16#DEADBEEF#.
13711 Upon each dereference, a check is made that the access value denotes a properly
13712 allocated memory location. Here is a complete example of use of
13713 @code{Debug_Pools}, that includes typical instances of  memory corruption:
13714 @smallexample
13715 @iftex
13716 @leftskip=0cm
13717 @end iftex
13718 @b{with} Gnat.Io; @b{use} Gnat.Io;
13719 @b{with} Unchecked_Deallocation;
13720 @b{with} Unchecked_Conversion;
13721 @b{with} GNAT.Debug_Pools;
13722 @b{with} System.Storage_Elements;
13723 @b{with} Ada.Exceptions; @b{use} Ada.Exceptions;
13724 @b{procedure} Debug_Pool_Test @b{is}
13726    @b{type} T @b{is} @b{access} Integer;
13727    @b{type} U @b{is} @b{access} @b{all} T;
13729    P : GNAT.Debug_Pools.Debug_Pool;
13730    @b{for} T'Storage_Pool @b{use} P;
13732    @b{procedure} Free @b{is} @b{new} Unchecked_Deallocation (Integer, T);
13733    @b{function} UC @b{is} @b{new} Unchecked_Conversion (U, T);
13734    A, B : @b{aliased} T;
13736    @b{procedure} Info @b{is} @b{new} GNAT.Debug_Pools.Print_Info(Put_Line);
13738 @b{begin}
13739    Info (P);
13740    A := @b{new} Integer;
13741    B := @b{new} Integer;
13742    B := A;
13743    Info (P);
13744    Free (A);
13745    @b{begin}
13746       Put_Line (Integer'Image(B.@b{all}));
13747    @b{exception}
13748       @b{when} E : @b{others} => Put_Line ("raised: " & Exception_Name (E));
13749    @b{end};
13750    @b{begin}
13751       Free (B);
13752    @b{exception}
13753       @b{when} E : @b{others} => Put_Line ("raised: " & Exception_Name (E));
13754    @b{end};
13755    B := UC(A'Access);
13756    @b{begin}
13757       Put_Line (Integer'Image(B.@b{all}));
13758    @b{exception}
13759       @b{when} E : @b{others} => Put_Line ("raised: " & Exception_Name (E));
13760    @b{end};
13761    @b{begin}
13762       Free (B);
13763    @b{exception}
13764       @b{when} E : @b{others} => Put_Line ("raised: " & Exception_Name (E));
13765    @b{end};
13766    Info (P);
13767 @b{end} Debug_Pool_Test;
13768 @end smallexample
13769 @noindent
13770 The debug pool mechanism provides the following precise diagnostics on the
13771 execution of this erroneous program:
13772 @smallexample
13773 Debug Pool info:
13774   Total allocated bytes :  0
13775   Total deallocated bytes :  0
13776   Current Water Mark:  0
13777   High Water Mark:  0
13779 Debug Pool info:
13780   Total allocated bytes :  8
13781   Total deallocated bytes :  0
13782   Current Water Mark:  8
13783   High Water Mark:  8
13785 raised: GNAT.DEBUG_POOLS.ACCESSING_DEALLOCATED_STORAGE
13786 raised: GNAT.DEBUG_POOLS.FREEING_DEALLOCATED_STORAGE
13787 raised: GNAT.DEBUG_POOLS.ACCESSING_NOT_ALLOCATED_STORAGE
13788 raised: GNAT.DEBUG_POOLS.FREEING_NOT_ALLOCATED_STORAGE
13789 Debug Pool info:
13790   Total allocated bytes :  8
13791   Total deallocated bytes :  4
13792   Current Water Mark:  4
13793   High Water Mark:  8
13795 @end smallexample
13797 @node Creating Sample Bodies Using GNAT STUB
13798 @chapter Creating Sample Bodies Using @code{GNAT STUB}
13799 @findex GNAT STUB
13801 @noindent
13802 @code{GNAT STUB} creates body stubs, that is, empty but compilable bodies
13803 for library unit declarations.
13805 To create a body stub, @code{GNAT STUB} has to compile the library
13806 unit declaration. Therefore, bodies can be created only for legal
13807 library units. Moreover, if a library unit depends semantically upon
13808 units located outside the current directory, you have to provide
13809 the source search path when calling @code{GNAT STUB}, see the description
13810 of @code{GNAT STUB} qualifiers below.
13812 @menu
13813 * Running GNAT STUB::
13814 * Qualifiers for GNAT STUB::
13815 @end menu
13817 @node Running GNAT STUB
13818 @section Running @code{GNAT STUB}
13820 @noindent
13821 @code{GNAT STUB} has the command-line interface of the form
13823 @smallexample
13824 $ GNAT STUB [qualifiers] filename [directory]
13825 @end smallexample
13827 @noindent
13828 where
13829 @table @code
13830 @item filename
13831 is the name of the source file that contains a library unit declaration
13832 for which a body must be created. This name should follow the GNAT file name
13833 conventions. No crunching is allowed for this file name. The file
13834 name may contain the path information.
13836 @item directory
13837 indicates the directory to place a body stub (default is the
13838 current directory)
13840 @item qualifiers
13841 is an optional sequence of qualifiers as described in the next section
13842 @end table
13844 @node Qualifiers for GNAT STUB
13845 @section Qualifiers for @code{GNAT STUB}
13847 @table @code
13849 @item /FULL
13850 If the destination directory already contains a file with a name of the body file
13851 for the argument spec file, replace it with the generated body stub.
13853 @item /HEADER=SPEC
13854 Put the comment header (i.e. all the comments preceding the
13855 compilation unit) from the source of the library unit declaration
13856 into the body stub.
13858 @item /HEADER=GENERAL
13859 Put a sample comment header into the body stub.
13861 @item /SEARCH=direc
13862 @itemx /NOCURRENT_DIRECTORY
13863 These qualifiers have the same meaning as in calls to GNAT COMPILE.
13864 They define the source search path in the call to GNAT COMPILE issued
13865 by @code{GNAT STUB} to compile an argument source file.
13867 @item /INDENTATION=@var{n}
13868 (@var{n} is a decimal natural number). Set the indentation level in the
13869 generated body sample to n, '/INDENTATION=0' means "no indentation",
13870 the default indentation is 3.
13872 @item /TREE_FILE=SAVE
13873 Do not remove the tree file (i.e. the snapshot of the compiler internal
13874 structures used by @code{GNAT STUB}) after creating the body stub.
13876 @item /LINE_LENGTH=@var{n}
13877 (@var{n} is a decimal positive number) Set the maximum line length in the
13878 body stub to n, the default is 78.
13880 @item /QUIET
13881 Quiet mode: do not generate a confirmation when a body is
13882 successfully created or a message when a body is not required for an
13883 argument unit.
13885 @item /TREE_FILE=REUSE
13886 Reuse the tree file (if it exists) instead of creating it: instead of
13887 creating the tree file for the library unit declaration, GNAT STUB
13888 tries to find it in the current directory and use it for creating
13889 a body. If the tree file is not found, no body is created. @code{/REUSE}
13890 also implies @code{/SAVE}, whether or not
13891 @code{/SAVE} is set explicitly.
13893 @item /TREE_FILE=OVERWRITE
13894 Overwrite the existing tree file: if the current directory already
13895 contains the file which, according to the GNAT file name rules should
13896 be considered as a tree file for the argument source file, GNAT STUB
13897 will refuse to create the tree file needed to create a body sampler,
13898 unless @code{-t} option is set
13900 @item /VERBOSE
13901 Verbose mode: generate version information.
13903 @end table
13905 @node Reducing the Size of Ada Executables with GNAT ELIM
13906 @chapter Reducing the Size of Ada Executables with @code{GNAT ELIM}
13907 @findex GNAT ELIM
13909 @menu
13910 * About GNAT ELIM::
13911 * Eliminate Pragma::
13912 * Tree Files::
13913 * Preparing Tree and Bind Files for GNAT ELIM::
13914 * Running GNAT ELIM::
13915 * Correcting the List of Eliminate Pragmas::
13916 * Making Your Executables Smaller::
13917 * Summary of the GNAT ELIM Usage Cycle::
13918 @end menu
13920 @node About GNAT ELIM
13921 @section About @code{GNAT ELIM}
13923 @noindent
13924 When a program shares a set of Ada
13925 packages with other programs, it may happen that this program uses
13926 only a fraction of the subprograms defined in these packages. The code
13927 created for these unused subprograms increases the size of the executable.
13929 @code{GNAT ELIM} tracks unused subprograms in an Ada program and
13930 outputs a list of GNAT-specific @code{Eliminate} pragmas (see next
13931 section) marking all the subprograms that are declared but never called.
13932 By placing the list of @code{Eliminate} pragmas in the GNAT configuration
13933 file @file{GNAT.ADC} and recompiling your program, you may decrease the
13934 size of its executable, because the compiler will not generate the code
13935 for 'eliminated' subprograms.
13937 @code{GNAT ELIM} needs as its input data a set of tree files
13938 (see @ref{Tree Files}) representing all the components of a program to
13939 process and a bind file for a main subprogram (see
13940 @ref{Preparing Tree and Bind Files for GNAT ELIM}).
13942 @node Eliminate Pragma
13943 @section @code{Eliminate} Pragma
13944 @findex Eliminate
13946 @noindent
13947 The simplified syntax of the Eliminate pragma used by @code{GNAT ELIM} is:
13949 @smallexample
13950 @cartouche
13951 @b{pragma} Eliminate (Library_Unit_Name, Subprogram_Name);
13952 @end cartouche
13953 @end smallexample
13955 @noindent
13956 where
13957 @table @code
13958 @item Library_Unit_Name
13959 full expanded Ada name of a library unit
13961 @item Subprogram_Name
13962 a simple or expanded name of a subprogram declared within this
13963 compilation unit
13965 @end table
13967 @noindent
13968 The effect of an @code{Eliminate} pragma placed in the GNAT configuration
13969 file @file{GNAT.ADC} is:
13971 @itemize @bullet
13973 @item
13974 If the subprogram @code{Subprogram_Name} is declared within
13975 the library unit @code{Library_Unit_Name}, the compiler will not generate
13976 code for this subprogram. This applies to all overloaded subprograms denoted
13977 by @code{Subprogram_Name}.
13979 @item
13980 If a subprogram marked by the pragma @code{Eliminate} is used (called)
13981 in a program, the compiler will produce an error message in the place where
13982 it is called.
13983 @end itemize
13985 @node Tree Files
13986 @section Tree Files
13987 @cindex Tree file
13989 @noindent
13990 A tree file stores a snapshot of the compiler internal data
13991 structures at the very end of a successful compilation. It contains all the
13992 syntactic and semantic information for the compiled unit and all the
13993 units upon which it depends semantically.
13994 To use tools that make use of tree files, you
13995 need to first produce the right set of tree files.
13997 GNAT produces correct tree files when /TREE_OUTPUT /NOLOAD options are set
13998 in a GNAT COMPILE call. The tree files have an .adt extension.
13999 Therefore, to produce a tree file for the compilation unit contained in a file
14000 named @file{FOO.ADB}, you must use the command
14002 @smallexample
14003 $ GNAT COMPILE /NOLOAD /TREE_OUTPUT FOO.ADB
14004 @end smallexample
14006 @noindent
14007 and you will get the tree file @file{foo.adt}.
14008 compilation.
14010 @node Preparing Tree and Bind Files for GNAT ELIM
14011 @section Preparing Tree and Bind Files for @code{GNAT ELIM}
14013 @noindent
14014 A set of tree files covering the program to be analyzed with
14015 @code{GNAT ELIM} and
14016 the bind file for the main subprogram does not have to
14017 be in the current directory.
14018 '-T' GNAT ELIM option may be used to provide
14019 the search path for tree files, and '-b'
14020 option may be used to point to the bind
14021 file to process (see @ref{Running GNAT ELIM})
14023 If you do not have the appropriate set of tree
14024 files and the right bind file, you
14025 may create them in the current directory using the following procedure.
14027 Let @code{Main_Prog} be the name of a main subprogram, and suppose
14028 this subprogram is in a file named @file{MAIN_PROG.ADB}.
14030 To create a bind file for @code{GNAT ELIM}, run @code{GNAT BIND} for
14031 the main subprogram. @code{GNAT ELIM} can work with both Ada and C
14032 bind files; when both are present, it uses the Ada bind file.
14033 The following commands will build the program and create the bind file:
14035 @smallexample
14036 $ GNAT MAKE /ACTIONS=COMPILE MAIN_PROG
14037 $ GNAT BIND main_prog
14038 @end smallexample
14040 @noindent
14041 To create a minimal set of tree files covering the whole program, call
14042 @code{GNAT MAKE} for this program as follows:
14044 @smallexample
14045 $ GNAT MAKE /FORCE_COMPILE /ACTIONS=COMPILE /NOLOAD /TREE_OUTPUT MAIN_PROG
14046 @end smallexample
14048 @noindent
14049 The @code{/ACTIONS=COMPILE} GNAT MAKE option turns off the bind and link
14050 steps, that are useless anyway because the sources are compiled with
14051 @option{/NOLOAD} option which turns off code generation.
14053 The @code{/FORCE_COMPILE} GNAT MAKE option forces
14054 recompilation of all the needed sources.
14056 This sequence of actions will create all the data needed by @code{GNAT ELIM}
14057 from scratch and therefore guarantee its consistency. If you would like to
14058 use some existing set of files as @code{GNAT ELIM} output, you must make
14059 sure that the set of files is complete and consistent. You can use the
14060 @code{-m} qualifier to check if there are missed tree files
14062 Note, that @code{GNAT ELIM} needs neither object nor ALI files.
14064 @node Running GNAT ELIM
14065 @section Running @code{GNAT ELIM}
14067 @noindent
14068 @code{GNAT ELIM} has the following command-line interface:
14070 @smallexample
14071 $ GNAT ELIM [options] name
14072 @end smallexample
14074 @noindent
14075 @code{name} should be a full expanded Ada name of a main subprogram
14076 of a program (partition).
14078 @code{GNAT ELIM} options:
14080 @table @code
14081 @item /QUIET
14082 Quiet mode: by default @code{GNAT ELIM} generates to the standard error
14083 stream a trace of the source file names of the compilation units being
14084 processed. This option turns this trace off.
14086 @item /VERBOSE
14087 Verbose mode: @code{GNAT ELIM} version information is printed as Ada
14088 comments to the standard output stream.
14090 @item /ALL
14091 Also look for subprograms from the GNAT run time that can be eliminated.
14093 @item /MISSED
14094 Check if any tree files are missing for an accurate result.
14096 @item /TREE_DIRS=@var{dir}
14097 When looking for tree files also look in directory @var{dir}
14099 @item /BIND_FILE=@var{bind_file}
14100 Specifies @var{bind_file} as the bind file to process. If not set, the name
14101 of the bind file is computed from the full expanded Ada name of a main subprogram.
14103 @item -d@var{x}
14104 Activate internal debugging qualifiers. @var{x} is a letter or digit, or
14105 string of letters or digits, which specifies the type of debugging
14106 mode desired.  Normally these are used only for internal development
14107 or system debugging purposes. You can find full documentation for these
14108 qualifiers in the body of the @code{GNAT ELIM.Options} unit in the compiler
14109 source file @file{GNATELIM-OPTIONS.ADB}.
14110 @end table
14112 @noindent
14113 @code{GNAT ELIM} sends its output to the standard output stream, and all the
14114 tracing and debug information is sent to the standard error stream.
14115 In order to produce a proper GNAT configuration file
14116 @file{GNAT.ADC}, redirection must be used:
14118 @smallexample
14119 $ PIPE GNAT ELIM MAIN_PROG > GNAT.ADC
14120 @end smallexample
14123 @noindent
14124 In order to append the @code{GNAT ELIM} output to the existing contents of
14125 @file{GNAT.ADC}.
14127 @node Correcting the List of Eliminate Pragmas
14128 @section Correcting the List of Eliminate Pragmas
14130 @noindent
14131 In some rare cases it may happen that @code{GNAT ELIM} will try to eliminate
14132 subprograms which are actually called in the program. In this case, the
14133 compiler will generate an error message of the form:
14135 @smallexample
14136 FILE.ADB:106:07: cannot call eliminated subprogram "My_Prog"
14137 @end smallexample
14139 @noindent
14140 You will need to manually remove the wrong @code{Eliminate} pragmas from
14141 the @file{GNAT.ADC} file. It is advised that you recompile your program
14142 from scratch after that because you need a consistent @file{GNAT.ADC} file
14143 during the entire compilation.
14145 @node Making Your Executables Smaller
14146 @section Making Your Executables Smaller
14148 @noindent
14149 In order to get a smaller executable for your program you now have to
14150 recompile the program completely with the new @file{GNAT.ADC} file
14151 created by @code{GNAT ELIM} in your current directory:
14153 @smallexample
14154 $ GNAT MAKE /FORCE_COMPILE MAIN_PROG
14155 @end smallexample
14157 @noindent
14158 (you will need @code{/FORCE_COMPILE} option for GNAT MAKE to
14159 recompile everything
14160 with the set of pragmas @code{Eliminate} you have obtained with
14161 @code{GNAT ELIM}).
14163 Be aware that the set of @code{Eliminate} pragmas is specific to each
14164 program. It is not recommended to merge sets of @code{Eliminate}
14165 pragmas created for different programs in one @file{GNAT.ADC} file.
14167 @node Summary of the GNAT ELIM Usage Cycle
14168 @section Summary of the GNAT ELIM Usage Cycle
14170 @noindent
14171 Here is a quick summary of the steps to be taken in order to reduce
14172 the size of your executables with @code{GNAT ELIM}. You may use
14173 other GNAT options to control the optimization level,
14174 to produce the debugging information, to set search path, etc.
14176 @enumerate
14177 @item
14178 Produce a bind file and a set of tree files
14180 @smallexample
14181 $ GNAT MAKE /ACTIONS=COMPILE MAIN_PROG
14182 $ GNAT BIND main_prog
14183 $ GNAT MAKE /FORCE_COMPILE /NO_LINK /NOLOAD /TREE_OUTPUT MAIN_PROG
14184 @end smallexample
14186 @item
14187 Generate a list of @code{Eliminate} pragmas
14188 @smallexample
14189 $ PIPE GNAT ELIM MAIN_PROG > GNAT.ADC
14190 @end smallexample
14192 @item
14193 Recompile the application
14195 @smallexample
14196 $ GNAT MAKE /FORCE_COMPILE MAIN_PROG
14197 @end smallexample
14199 @end enumerate
14201 @node Other Utility Programs
14202 @chapter Other Utility Programs
14204 @noindent
14205 This chapter discusses some other utility programs available in the Ada
14206 environment.
14208 @menu
14209 * Using Other Utility Programs with GNAT::
14210 * The GNAT STANDARD Utility Program::
14211 * The External Symbol Naming Scheme of GNAT::
14212 * Ada Mode for Glide::
14213 * Converting Ada Files to html with gnathtml::
14214 * Installing gnathtml::
14215 * LSE::
14216 * Profiling::
14217 @end menu
14219 @node Using Other Utility Programs with GNAT
14220 @section Using Other Utility Programs with GNAT
14222 @noindent
14223 The object files generated by GNAT are in standard system format and in
14224 particular the debugging information uses this format. This means
14225 programs generated by GNAT can be used with existing utilities that
14226 depend on these formats.
14229 @node The GNAT STANDARD Utility Program
14230 @section The @code{GNAT STANDARD} Utility Program
14232 @noindent
14233 Many of the definitions in package Standard are implementation-dependent.
14234 However, the source of this package does not exist as an Ada source
14235 file, so these values cannot be determined by inspecting the source.
14236 They can be determined by examining in detail the coding of
14237 @file{CSTAND.ADB} which creates the image of Standard in the compiler,
14238 but this is awkward and requires a great deal of internal knowledge
14239 about the system.
14241 The @code{GNAT STANDARD} utility is designed to deal with this situation.
14242 It is an Ada program that dynamically determines the
14243 values of all the relevant parameters in Standard, and prints them
14244 out in the form of an Ada source listing for Standard, displaying all
14245 the values of interest. This output is generated to
14246 @file{SYS$OUTPUT}.
14248 To determine the value of any parameter in package Standard, simply
14249 run @code{GNAT STANDARD} with no qualifiers or arguments, and examine
14250 the output. This is preferable to consulting documentation, because
14251 you know that the values you are getting are the actual ones provided
14252 by the executing system.
14254 @node The External Symbol Naming Scheme of GNAT
14255 @section The External Symbol Naming Scheme of GNAT
14257 @noindent
14258 In order to interpret the output from GNAT, when using tools that are
14259 originally intended for use with other languages, it is useful to
14260 understand the conventions used to generate link names from the Ada
14261 entity names.
14263 All link names are in all lowercase letters. With the exception of library
14264 procedure names, the mechanism used is simply to use the full expanded
14265 Ada name with dots replaced by double underscores. For example, suppose
14266 we have the following package spec:
14268 @smallexample
14269 @group
14270 @cartouche
14271 @b{package} QRS @b{is}
14272    MN : Integer;
14273 @b{end} QRS;
14274 @end cartouche
14275 @end group
14276 @end smallexample
14278 @noindent
14279 The variable @code{MN} has a full expanded Ada name of @code{QRS.MN}, so
14280 the corresponding link name is @code{qrs__mn}.
14281 @findex Export
14282 Of course if a @code{pragma Export} is used this may be overridden:
14284 @smallexample
14285 @group
14286 @cartouche
14287 @b{package} Exports @b{is}
14288    Var1 : Integer;
14289    @b{pragma} Export (Var1, C, External_Name => "var1_name");
14290    Var2 : Integer;
14291    @b{pragma} Export (Var2, C, Link_Name => "var2_link_name");
14292 @b{end} Exports;
14293 @end cartouche
14294 @end group
14295 @end smallexample
14297 @noindent
14298 In this case, the link name for @var{Var1} is whatever link name the
14299 C compiler would assign for the C function @var{var1_name}. This typically
14300 would be either @var{var1_name} or @var{_var1_name}, depending on operating
14301 system conventions, but other possibilities exist. The link name for
14302 @var{Var2} is @var{var2_link_name}, and this is not operating system
14303 dependent.
14305 @findex _main
14306 One exception occurs for library level procedures. A potential ambiguity
14307 arises between the required name @code{_main} for the C main program,
14308 and the name we would otherwise assign to an Ada library level procedure
14309 called @code{Main} (which might well not be the main program).
14311 To avoid this ambiguity, we attach the prefix @code{_ada_} to such
14312 names. So if we have a library level procedure such as
14314 @smallexample
14315 @group
14316 @cartouche
14317 @b{procedure} Hello (S : String);
14318 @end cartouche
14319 @end group
14320 @end smallexample
14322 @noindent
14323 the external name of this procedure will be @var{_ada_hello}.
14325 @node Ada Mode for Glide
14326 @section Ada Mode for @code{Glide}
14328 @noindent
14329 The Glide mode for programming in Ada (both, Ada83 and Ada95) helps the
14330 user in understanding existing code and facilitates writing new code. It
14331 furthermore provides some utility functions for easier integration of
14332 standard EMACS features when programming in Ada.
14334 @subsection General Features:
14336 @itemize @bullet
14337 @item
14338 Full Integrated Development Environment :
14340 @itemize @bullet
14341 @item
14342 support of 'project files' for the configuration (directories,
14343 compilation options,...)
14345 @item
14346 compiling and stepping through error messages.
14348 @item
14349 running and debugging your applications within Glide.
14350 @end itemize
14352 @item
14353 easy to use for beginners by pull-down menus,
14355 @item
14356 user configurable by many user-option variables.
14357 @end itemize
14359 @subsection Ada Mode Features That Help Understanding Code:
14361 @itemize @bullet
14362 @item
14363 functions for easy and quick stepping through Ada code,
14365 @item
14366 getting cross reference information for identifiers (e.g. find the
14367 defining place by a keystroke),
14369 @item
14370 displaying an index menu of types and subprograms and move point to
14371 the chosen one,
14373 @item
14374 automatic color highlighting of the various entities in Ada code.
14375 @end itemize
14377 @subsection Glide Support for Writing Ada Code:
14379 @itemize @bullet
14380 @item
14381 switching between spec and body files with possible
14382 autogeneration of body files,
14384 @item
14385 automatic formating of subprograms parameter lists.
14387 @item
14388 automatic smart indentation according to Ada syntax,
14390 @item
14391 automatic completion of identifiers,
14393 @item
14394 automatic casing of identifiers, keywords, and attributes,
14396 @item
14397 insertion of statement templates,
14399 @item
14400 filling comment paragraphs like filling normal text,
14401 @end itemize
14403 For more information, please refer to the online Glide documentation
14404 available in the Glide --> Help Menu.
14406 @node Converting Ada Files to html with gnathtml
14407 @section Converting Ada Files to html with @code{gnathtml}
14409 @noindent
14410 This @code{Perl} script allows Ada source files to be browsed using
14411 standard Web browsers. For installation procedure, see the section
14412 @xref{Installing gnathtml}.
14414 Ada reserved keywords are highlighted in a bold font and Ada comments in
14415 a blue font. Unless your program was compiled with the GNAT COMPILE @option{/XREF=SUPPRESS}
14416 qualifier to suppress the generation of cross-referencing information, user
14417 defined variables and types will appear in a different color; you will
14418 be able to click on any identifier and go to its declaration.
14420 The command line is as follow:
14421 @smallexample
14422 $ perl gnathtml.pl [qualifiers] ada-files
14423 @end smallexample
14425 You can pass it as many Ada files as you want. @code{gnathtml} will generate
14426 an html file for every ada file, and a global file called @file{index.htm}.
14427 This file is an index of every identifier defined in the files.
14429 The available qualifiers are the following ones :
14431 @table @code
14432 @item -83
14433 @cindex @code{-83} (@code{gnathtml})
14434 Only the subset on the Ada 83 keywords will be highlighted, not the full
14435 Ada 95 keywords set.
14437 @item -cc @var{color}
14438 This option allows you to change the color used for comments. The default
14439 value is green. The color argument can be any name accepted by html.
14441 @item -d
14442 @cindex @code{-d} (@code{gnathtml})
14443 If the ada files depend on some other files (using for instance the
14444 @code{with} command, the latter will also be converted to html.
14445 Only the files in the user project will be converted to html, not the files
14446 in the run-time library itself.
14448 @item -D
14449 This command is the same as -d above, but @code{gnathtml} will also look
14450 for files in the run-time library, and generate html files for them.
14452 @item -f
14453 @cindex @code{-f} (@code{gnathtml})
14454 By default, gnathtml will generate html links only for global entities
14455 ('with'ed units, global variables and types,...). If you specify the
14456 @code{-f} on the command line, then links will be generated for local
14457 entities too.
14459 @item -l @var{number}
14460 @cindex @code{-l} (@code{gnathtml})
14461 If this qualifier is provided and @var{number} is not 0, then @code{gnathtml}
14462 will number the html files every @var{number} line.
14464 @item -I @var{dir}
14465 @cindex @code{-I} (@code{gnathtml})
14466 Specify a directory to search for library files (@file{.ALI} files) and
14467 source files. You can provide several -I qualifiers on the command line,
14468 and the directories will be parsed in the order of the command line.
14470 @item -o @var{dir}
14471 @cindex @code{-o} (@code{gnathtml})
14472 Specify the output directory for html files. By default, gnathtml will
14473 saved the generated html files in a subdirectory named @file{html/}.
14475 @item -p @var{file}
14476 @cindex @code{-p} (@code{gnathtml})
14477 If you are using EMACS and the most recent EMACS Ada mode, which provides
14478 a full Integrated Development Environment for compiling, checking,
14479 running and debugging applications, you may be using @file{.adp} files
14480 to give the directories where EMACS can find sources and object files.
14482 Using this qualifier, you can tell gnathtml to use these files. This allows
14483 you to get an html version of your application, even if it is spread
14484 over multiple directories.
14486 @item -sc @var{color}
14487 @cindex @code{-sc} (@code{gnathtml})
14488 This option allows you to change the color used for symbol definitions.
14489 The default value is red. The color argument can be any name accepted by html.
14491 @item -t @var{file}
14492 @cindex @code{-t} (@code{gnathtml})
14493 This qualifier provides the name of a file. This file contains a list of
14494 file names to be converted, and the effect is exactly as though they had
14495 appeared explicitly on the command line. This
14496 is the recommended way to work around the command line length limit on some
14497 systems.
14499 @end table
14501 @node Installing gnathtml
14502 @section Installing @code{gnathtml}
14504 @noindent
14505 @code{Perl} needs to be installed on your machine to run this script.
14506 @code{Perl} is freely available for almost every architecture and
14507 Operating System via the Internet.
14509 On Unix systems, you  may want to modify  the  first line of  the script
14510 @code{gnathtml},  to explicitly  tell  the Operating  system  where Perl
14511 is. The syntax of this line is :
14512 @smallexample
14513 #!full_path_name_to_perl
14514 @end smallexample
14516 @noindent
14517 Alternatively, you may run the script using the following command line:
14519 @smallexample
14520 $ perl gnathtml.pl [qualifiers] files
14521 @end smallexample
14523 @node LSE
14524 @section LSE
14525 @findex LSE
14527 @noindent
14528 The GNAT distribution provides an Ada 95 template for the Digital Language
14529 Sensitive Editor (LSE), a component of DECset. In order to
14530 access it, invoke LSE with the qualifier /ENVIRONMENT=GNU:[LIB]ADA95.ENV.
14532 @node Profiling
14533 @section Profiling
14534 @findex PCA
14536 @noindent
14537 GNAT supports The Digital Performance Coverage Analyzer (PCA), a component
14538 of DECset. To use it proceed as outlined under "HELP PCA", except for running
14539 the collection phase with the /DEBUG qualifier.
14541 @smallexample
14542 $ GNAT MAKE /DEBUG <PROGRAM_NAME>
14543 $ DEFINE LIB$DEBUG PCA$COLLECTOR
14544 $ RUN/DEBUG <PROGRAM_NAME>
14545 @end smallexample
14546 @noindent
14548 @node Running and Debugging Ada Programs
14549 @chapter Running and Debugging Ada Programs
14550 @cindex Debugging
14552 @noindent
14553 This chapter discusses how to debug Ada programs. An incorrect Ada program
14554 may be handled in three ways by the GNAT compiler:
14556 @enumerate
14557 @item
14558 The illegality may be a violation of the static semantics of Ada. In
14559 that case GNAT diagnoses the constructs in the program that are illegal.
14560 It is then a straightforward matter for the user to modify those parts of
14561 the program.
14563 @item
14564 The illegality may be a violation of the dynamic semantics of Ada. In
14565 that case the program compiles and executes, but may generate incorrect
14566 results, or may terminate abnormally with some exception.
14568 @item
14569 When presented with a program that contains convoluted errors, GNAT
14570 itself may terminate abnormally without providing full diagnostics on
14571 the incorrect user program.
14572 @end enumerate
14574 @menu
14575 * The GNAT Debugger GDB::
14576 * Running GDB::
14577 * Introduction to GDB Commands::
14578 * Using Ada Expressions::
14579 * Calling User-Defined Subprograms::
14580 * Using the Next Command in a Function::
14581 * Ada Exceptions::
14582 * Ada Tasks::
14583 * Debugging Generic Units::
14584 * GNAT Abnormal Termination or Failure to Terminate::
14585 * Naming Conventions for GNAT Source Files::
14586 * Getting Internal Debugging Information::
14587 * Stack Traceback::
14588 @end menu
14590 @cindex Debugger
14591 @findex GDB
14593 @node The GNAT Debugger GDB
14594 @section The GNAT Debugger GDB
14596 @noindent
14597 @code{GDB} is a general purpose, platform-independent debugger that
14598 can be used to debug mixed-language programs compiled with @code{GCC},
14599 and in particular is capable of debugging Ada programs compiled with
14600 GNAT. The latest versions of @code{GDB} are Ada-aware and can handle
14601 complex Ada data structures.
14603 The manual @cite{Debugging with GDB}
14604 , located in the GNU:[DOCS] directory,
14605 contains full details on the usage of @code{GDB}, including a section on
14606 its usage on programs. This manual should be consulted for full
14607 details. The section that follows is a brief introduction to the
14608 philosophy and use of @code{GDB}.
14610 When GNAT programs are compiled, the compiler optionally writes debugging
14611 information into the generated object file, including information on
14612 line numbers, and on declared types and variables. This information is
14613 separate from the generated code. It makes the object files considerably
14614 larger, but it does not add to the size of the actual executable that
14615 will be loaded into memory, and has no impact on run-time performance. The
14616 generation of debug information is triggered by the use of the
14617 /DEBUG qualifier in the GNAT COMPILE or GNAT MAKE command used to carry out
14618 the compilations. It is important to emphasize that the use of these
14619 options does not change the generated code.
14621 The debugging information is written in standard system formats that
14622 are used by many tools, including debuggers and profilers. The format
14623 of the information is typically designed to describe C types and
14624 semantics, but GNAT implements a translation scheme which allows full
14625 details about Ada types and variables to be encoded into these
14626 standard C formats. Details of this encoding scheme may be found in
14627 the file EXP_DBUG.ADS in the GNAT source distribution. However, the
14628 details of this encoding are, in general, of no interest to a user,
14629 since @code{GDB} automatically performs the necessary decoding.
14631 When a program is bound and linked, the debugging information is
14632 collected from the object files, and stored in the executable image of
14633 the program. Again, this process significantly increases the size of
14634 the generated executable file, but it does not increase the size of
14635 the executable program itself. Furthermore, if this program is run in
14636 the normal manner, it runs exactly as if the debug information were
14637 not present, and takes no more actual memory.
14639 However, if the program is run under control of @code{GDB}, the
14640 debugger is activated.  The image of the program is loaded, at which
14641 point it is ready to run.  If a run command is given, then the program
14642 will run exactly as it would have if @code{GDB} were not present. This
14643 is a crucial part of the @code{GDB} design philosophy.  @code{GDB} is
14644 entirely non-intrusive until a breakpoint is encountered.  If no
14645 breakpoint is ever hit, the program will run exactly as it would if no
14646 debugger were present. When a breakpoint is hit, @code{GDB} accesses
14647 the debugging information and can respond to user commands to inspect
14648 variables, and more generally to report on the state of execution.
14650 @node Running GDB
14651 @section Running GDB
14653 @noindent
14654 The debugger can be launched directly and simply from @code{glide} or
14655 through its graphical interface: @code{gvd}. It can also be used
14656 directly in text mode. Here is described the basic use of @code{GDB}
14657 in text mode. All the commands described below can be used in the
14658 @code{gvd} console window eventhough there is usually other more
14659 graphical ways to achieve the same goals.
14662 @noindent
14663 The command to run @code{GDB} in text mode is
14665 @smallexample
14666 $ $ GDB PROGRAM
14667 @end smallexample
14669 @noindent
14670 where @code{PROGRAM} is the name of the executable file. This
14671 activates the debugger and results in a prompt for debugger commands.
14672 The simplest command is simply @code{run}, which causes the program to run
14673 exactly as if the debugger were not present. The following section
14674 describes some of the additional commands that can be given to @code{GDB}.
14677 @node Introduction to GDB Commands
14678 @section Introduction to GDB Commands
14680 @noindent
14681 @code{GDB} contains a large repertoire of commands. The manual
14682 @cite{Debugging with GDB}
14683 , located in the GNU:[DOCS] directory,
14684 includes extensive documentation on the use
14685 of these commands, together with examples of their use. Furthermore,
14686 the command @var{help} invoked from within @code{GDB} activates a simple help
14687 facility which summarizes the available commands and their options.
14688 In this section we summarize a few of the most commonly
14689 used commands to give an idea of what @code{GDB} is about. You should create
14690 a simple program with debugging information and experiment with the use of
14691 these @code{GDB} commands on the program as you read through the
14692 following section.
14694 @table @code
14695 @item set args @var{arguments}
14696 The @var{arguments} list above is a list of arguments to be passed to
14697 the program on a subsequent run command, just as though the arguments
14698 had been entered on a normal invocation of the program. The @code{set args}
14699 command is not needed if the program does not require arguments.
14701 @item run
14702 The @code{run} command causes execution of the program to start from
14703 the beginning. If the program is already running, that is to say if
14704 you are currently positioned at a breakpoint, then a prompt will ask
14705 for confirmation that you want to abandon the current execution and
14706 restart.
14708 @item breakpoint @var{location}
14709 The breakpoint command sets a breakpoint, that is to say a point at which
14710 execution will halt and @code{GDB} will await further
14711 commands. @var{location} is
14712 either a line number within a file, given in the format @code{file:linenumber},
14713 or it is the name of a subprogram. If you request that a breakpoint be set on
14714 a subprogram that is overloaded, a prompt will ask you to specify on which of
14715 those subprograms you want to breakpoint. You can also
14716 specify that all of them should be breakpointed. If the program is run
14717 and execution encounters the breakpoint, then the program
14718 stops and @code{GDB} signals that the breakpoint was encountered by
14719 printing the line of code before which the program is halted.
14721 @item breakpoint exception @var{name}
14722 A special form of the breakpoint command which breakpoints whenever
14723 exception @var{name} is raised.
14724 If @var{name} is omitted,
14725 then a breakpoint will occur when any exception is raised.
14727 @item print @var{expression}
14728 This will print the value of the given expression. Most simple
14729 Ada expression formats are properly handled by @code{GDB}, so the expression
14730 can contain function calls, variables, operators, and attribute references.
14732 @item continue
14733 Continues execution following a breakpoint, until the next breakpoint or the
14734 termination of the program.
14736 @item step
14737 Executes a single line after a breakpoint. If the next statement is a subprogram
14738 call, execution continues into (the first statement of) the
14739 called subprogram.
14741 @item next
14742 Executes a single line. If this line is a subprogram call, executes and
14743 returns from the call.
14745 @item list
14746 Lists a few lines around the current source location. In practice, it
14747 is usually more convenient to have a separate edit window open with the
14748 relevant source file displayed. Successive applications of this command
14749 print subsequent lines. The command can be given an argument which is a
14750 line number, in which case it displays a few lines around the specified one.
14752 @item backtrace
14753 Displays a backtrace of the call chain. This command is typically
14754 used after a breakpoint has occurred, to examine the sequence of calls that
14755 leads to the current breakpoint. The display includes one line for each
14756 activation record (frame) corresponding to an active subprogram.
14758 @item up
14759 At a breakpoint, @code{GDB} can display the values of variables local
14760 to the current frame. The command @code{up} can be used to
14761 examine the contents of other active frames, by moving the focus up
14762 the stack, that is to say from callee to caller, one frame at a time.
14764 @item down
14765 Moves the focus of @code{GDB} down from the frame currently being
14766 examined to the frame of its callee (the reverse of the previous command),
14768 @item frame @var{n}
14769 Inspect the frame with the given number. The value 0 denotes the frame
14770 of the current breakpoint, that is to say the top of the call stack.
14772 @end table
14774 The above list is a very short introduction to the commands that
14775 @code{GDB} provides. Important additional capabilities, including conditional
14776 breakpoints, the ability to execute command sequences on a breakpoint,
14777 the ability to debug at the machine instruction level and many other
14778 features are described in detail in @cite{Debugging with GDB}.
14779 Note that most commands can be abbreviated
14780 (for example, c for continue, bt for backtrace).
14782 @node Using Ada Expressions
14783 @section Using Ada Expressions
14784 @cindex Ada expressions
14786 @noindent
14787 @code{GDB} supports a fairly large subset of Ada expression syntax, with some
14788 extensions. The philosophy behind the design of this subset is
14790 @itemize @bullet
14791 @item
14792 That @code{GDB} should provide basic literals and access to operations for
14793 arithmetic, dereferencing, field selection, indexing, and subprogram calls,
14794 leaving more sophisticated computations to subprograms written into the
14795 program (which therefore may be called from @code{GDB}).
14797 @item
14798 That type safety and strict adherence to Ada language restrictions
14799 are not particularly important to the @code{GDB} user.
14801 @item
14802 That brevity is important to the @code{GDB} user.
14803 @end itemize
14805 Thus, for brevity, the debugger acts as if there were
14806 implicit @code{with} and @code{use} clauses in effect for all user-written
14807 packages, thus making it unnecessary to fully qualify most names with
14808 their packages, regardless of context. Where this causes ambiguity,
14809 @code{GDB} asks the user's intent.
14811 For details on the supported Ada syntax, see @cite{Debugging with GDB}.
14813 @node Calling User-Defined Subprograms
14814 @section Calling User-Defined Subprograms
14816 @noindent
14817 An important capability of @code{GDB} is the ability to call user-defined
14818 subprograms while debugging. This is achieved simply by entering
14819 a subprogram call statement in the form:
14821 @smallexample
14822 call subprogram-name (parameters)
14823 @end smallexample
14825 @noindent
14826 The keyword @code{call} can be omitted in the normal case where the
14827 @code{subprogram-name} does not coincide with any of the predefined
14828 @code{GDB} commands.
14830 The effect is to invoke the given subprogram, passing it the
14831 list of parameters that is supplied. The parameters can be expressions and
14832 can include variables from the program being debugged. The
14833 subprogram must be defined
14834 at the library level within your program, and @code{GDB} will call the
14835 subprogram within the environment of your program execution (which
14836 means that the subprogram is free to access or even modify variables
14837 within your program).
14839 The most important use of this facility is in allowing the inclusion of
14840 debugging routines that are tailored to particular data structures
14841 in your program. Such debugging routines can be written to provide a suitably
14842 high-level description of an abstract type, rather than a low-level dump
14843 of its physical layout. After all, the standard
14844 @code{GDB print} command only knows the physical layout of your
14845 types, not their abstract meaning. Debugging routines can provide information
14846 at the desired semantic level and are thus enormously useful.
14848 For example, when debugging GNAT itself, it is crucial to have access to
14849 the contents of the tree nodes used to represent the program internally.
14850 But tree nodes are represented simply by an integer value (which in turn
14851 is an index into a table of nodes).
14852 Using the @code{print} command on a tree node would simply print this integer
14853 value, which is not very useful. But the PN routine (defined in file
14854 TREEPR.ADB in the GNAT sources) takes a tree node as input, and displays
14855 a useful high level representation of the tree node, which includes the
14856 syntactic category of the node, its position in the source, the integers
14857 that denote descendant nodes and parent node, as well as varied
14858 semantic information. To study this example in more detail, you might want to
14859 look at the body of the PN procedure in the stated file.
14861 @node Using the Next Command in a Function
14862 @section Using the Next Command in a Function
14864 @noindent
14865 When you use the @code{next} command in a function, the current source
14866 location will advance to the next statement as usual. A special case
14867 arises in the case of a @code{return} statement.
14869 Part of the code for a return statement is the "epilog" of the function.
14870 This is the code that returns to the caller. There is only one copy of
14871 this epilog code, and it is typically associated with the last return
14872 statement in the function if there is more than one return. In some
14873 implementations, this epilog is associated with the first statement
14874 of the function.
14876 The result is that if you use the @code{next} command from a return
14877 statement that is not the last return statement of the function you
14878 may see a strange apparent jump to the last return statement or to
14879 the start of the function. You should simply ignore this odd jump.
14880 The value returned is always that from the first return statement
14881 that was stepped through.
14883 @node Ada Exceptions
14884 @section Breaking on Ada Exceptions
14885 @cindex Exceptions
14887 @noindent
14888 You can set breakpoints that trip when your program raises
14889 selected exceptions.
14891 @table @code
14892 @item break exception
14893 Set a breakpoint that trips whenever (any task in the) program raises
14894 any exception.
14896 @item break exception @var{name}
14897 Set a breakpoint that trips whenever (any task in the) program raises
14898 the exception @var{name}.
14900 @item break exception unhandled
14901 Set a breakpoint that trips whenever (any task in the) program raises an
14902 exception for which there is no handler.
14904 @item info exceptions
14905 @itemx info exceptions @var{regexp}
14906 The @code{info exceptions} command permits the user to examine all defined
14907 exceptions within Ada programs. With a regular expression, @var{regexp}, as
14908 argument, prints out only those exceptions whose name matches @var{regexp}.
14909 @end table
14911 @node Ada Tasks
14912 @section Ada Tasks
14913 @cindex Tasks
14915 @noindent
14916 @code{GDB} allows the following task-related commands:
14918 @table @code
14919 @item info tasks
14920 This command shows a list of current Ada tasks, as in the following example:
14922 @smallexample
14923 @iftex
14924 @leftskip=0cm
14925 @end iftex
14926 (GDB) info tasks
14927   ID       TID P-ID   Thread Pri State                 Name
14928    1   8088000   0   807e000  15 Child Activation Wait main_task
14929    2   80a4000   1   80ae000  15 Accept/Select Wait    b
14930    3   809a800   1   80a4800  15 Child Activation Wait a
14931 *  4   80ae800   3   80b8000  15 Running               c
14932 @end smallexample
14934 @noindent
14935 In this listing, the asterisk before the first task indicates it to be the
14936 currently running task. The first column lists the task ID that is used
14937 to refer to tasks in the following commands.
14939 @item break @var{linespec} task @var{taskid}
14940 @itemx break @var{linespec} task @var{taskid} if @dots{}
14941 @cindex Breakpoints and tasks
14942 These commands are like the @code{break @dots{} thread @dots{}}.
14943 @var{linespec} specifies source lines.
14945 Use the qualifier @samp{task @var{taskid}} with a breakpoint command
14946 to specify that you only want @code{GDB} to stop the program when a
14947 particular Ada task reaches this breakpoint. @var{taskid} is one of the
14948 numeric task identifiers assigned by @code{GDB}, shown in the first
14949 column of the @samp{info tasks} display.
14951 If you do not specify @samp{task @var{taskid}} when you set a
14952 breakpoint, the breakpoint applies to @emph{all} tasks of your
14953 program.
14955 You can use the @code{task} qualifier on conditional breakpoints as
14956 well; in this case, place @samp{task @var{taskid}} before the
14957 breakpoint condition (before the @code{if}).
14959 @item task @var{taskno}
14960 @cindex Task switching
14962 This command allows to qualifier to the task referred by @var{taskno}. In
14963 particular, This allows to browse the backtrace of the specified
14964 task. It is advised to qualifier back to the original task before
14965 continuing execution otherwise the scheduling of the program may be
14966 perturbated.
14967 @end table
14969 @noindent
14970 For more detailed information on the tasking support, see @cite{Debugging with GDB}.
14972 @node Debugging Generic Units
14973 @section Debugging Generic Units
14974 @cindex Debugging Generic Units
14975 @cindex Generics
14977 @noindent
14978 GNAT always uses code expansion for generic instantiation. This means that
14979 each time an instantiation occurs, a complete copy of the original code is
14980 made, with appropriate substitutions of formals by actuals.
14982 It is not possible to refer to the original generic entities in
14983 @code{GDB}, but it is always possible to debug a particular instance of
14984 a generic, by using the appropriate expanded names. For example, if we have
14986 @smallexample
14987 @group
14988 @cartouche
14989 @b{procedure} g @b{is}
14991    @b{generic package} k @b{is}
14992       @b{procedure} kp (v1 : @b{in out} integer);
14993    @b{end} k;
14995    @b{package body} k @b{is}
14996       @b{procedure} kp (v1 : @b{in out} integer) @b{is}
14997       @b{begin}
14998          v1 := v1 + 1;
14999       @b{end} kp;
15000    @b{end} k;
15002    @b{package} k1 @b{is new} k;
15003    @b{package} k2 @b{is new} k;
15005    var : integer := 1;
15007 @b{begin}
15008    k1.kp (var);
15009    k2.kp (var);
15010    k1.kp (var);
15011    k2.kp (var);
15012 @b{end};
15013 @end cartouche
15014 @end group
15015 @end smallexample
15017 @noindent
15018 Then to break on a call to procedure kp in the k2 instance, simply
15019 use the command:
15021 @smallexample
15022 (GDB) break g.k2.kp
15023 @end smallexample
15025 @noindent
15026 When the breakpoint occurs, you can step through the code of the
15027 instance in the normal manner and examine the values of local variables, as for
15028 other units.
15030 @node GNAT Abnormal Termination or Failure to Terminate
15031 @section GNAT Abnormal Termination or Failure to Terminate
15032 @cindex GNAT Abnormal Termination or Failure to Terminate
15034 @noindent
15035 When presented with programs that contain serious errors in syntax
15036 or semantics,
15037 GNAT may on rare occasions  experience problems in operation, such
15038 as aborting with a
15039 segmentation fault or illegal memory access, raising an internal
15040 exception, terminating abnormally, or failing to terminate at all.
15041 In such cases, you can activate
15042 various features of GNAT that can help you pinpoint the construct in your
15043 program that is the likely source of the problem.
15045 The following strategies are presented in increasing order of
15046 difficulty, corresponding to your experience in using GNAT and your
15047 familiarity with compiler internals.
15049 @enumerate
15050 @item
15051 Run @code{GNAT COMPILE} with the @option{/REPORT_ERRORS=FULL}. This first
15052 qualifier causes all errors on a given line to be reported. In its absence,
15053 only the first error on a line is displayed.
15055 The @option{/REPORT_ERRORS=IMMEDIATE} qualifier causes errors to be displayed as soon as they
15056 are encountered, rather than after compilation is terminated. If GNAT
15057 terminates prematurely or goes into an infinite loop, the last error
15058 message displayed may help to pinpoint the culprit.
15060 @item
15061 Run @code{GNAT COMPILE} with the @code{/VERBOSE} qualifier. In this mode,
15062 @code{GNAT COMPILE} produces ongoing information about the progress of the
15063 compilation and provides the name of each procedure as code is
15064 generated. This qualifier allows you to find which Ada procedure was being
15065 compiled when it encountered a code generation problem.
15067 @item
15068 @cindex @option{/TRACE_UNITS} qualifier
15069 Run @code{GNAT COMPILE} with the @option{/TRACE_UNITS} qualifier. This is a GNAT specific
15070 qualifier that does for the front-end what @code{VERBOSE} does for the back end.
15071 The system prints the name of each unit, either a compilation unit or
15072 nested unit, as it is being analyzed.
15073 @item
15074 Finally, you can start
15075 @code{GDB} directly on the @code{GNAT1} executable. @code{GNAT1} is the
15076 front-end of GNAT, and can be run independently (normally it is just
15077 called from @code{GNAT COMPILE}). You can use @code{GDB} on @code{GNAT1} as you
15078 would on a C program (but @pxref{The GNAT Debugger GDB} for caveats). The
15079 @code{where} command is the first line of attack; the variable
15080 @code{lineno} (seen by @code{print lineno}), used by the second phase of
15081 @code{GNAT1} and by the @code{GNAT COMPILE} backend, indicates the source line at
15082 which the execution stopped, and @code{input_file name} indicates the name of
15083 the source file.
15084 @end enumerate
15086 @node Naming Conventions for GNAT Source Files
15087 @section Naming Conventions for GNAT Source Files
15089 @noindent
15090 In order to examine the workings of the GNAT system, the following
15091 brief description of its organization may be helpful:
15093 @itemize @bullet
15094 @item
15095 Files with prefix @file{SC} contain the lexical scanner.
15097 @item
15098 All files prefixed with @file{PAR} are components of the parser. The
15099 numbers correspond to chapters of the Ada 95 Reference Manual. For example,
15100 parsing of select statements can be found in @file{PAR-CH9.ADB}.
15102 @item
15103 All files prefixed with @file{SEM} perform semantic analysis. The
15104 numbers correspond to chapters of the Ada standard. For example, all
15105 issues involving context clauses can be found in @file{SEM_CH10.ADB}. In
15106 addition, some features of the language require sufficient special processing
15107 to justify their own semantic files: sem_aggr for aggregates, sem_disp for
15108 dynamic dispatching, etc.
15110 @item
15111 All files prefixed with @file{EXP} perform normalization and
15112 expansion of the intermediate representation (abstract syntax tree, or AST).
15113 these files use the same numbering scheme as the parser and semantics files.
15114 For example, the construction of record initialization procedures is done in
15115 @file{EXP_CH3.ADB}.
15117 @item
15118 The files prefixed with @file{BIND} implement the binder, which
15119 verifies the consistency of the compilation, determines an order of
15120 elaboration, and generates the bind file.
15122 @item
15123 The files @file{ATREE.ADS} and @file{ATREE.ADB} detail the low-level
15124 data structures used by the front-end.
15126 @item
15127 The files @file{SINFO.ADS} and @file{SINFO.ADB} detail the structure of
15128 the abstract syntax tree as produced by the parser.
15130 @item
15131 The files @file{EINFO.ADS} and @file{EINFO.ADB} detail the attributes of
15132 all entities, computed during semantic analysis.
15134 @item
15135 Library management issues are dealt with in files with prefix
15136 @file{LIB}.
15138 @item
15139 @findex Ada
15140 @cindex Annex A
15141 Ada files with the prefix @file{A-} are children of @code{Ada}, as
15142 defined in Annex A.
15144 @item
15145 @findex Interfaces
15146 @cindex Annex B
15147 Files with prefix @file{I-} are children of @code{Interfaces}, as
15148 defined in Annex B.
15150 @item
15151 @findex System
15152 Files with prefix @file{S-} are children of @code{System}. This includes
15153 both language-defined children and GNAT run-time routines.
15155 @item
15156 @findex GNAT
15157 Files with prefix @file{G-} are children of @code{GNAT}. These are useful
15158 general-purpose packages, fully documented in their specifications. All
15159 the other @file{.C} files are modifications of common @code{GNAT COMPILE} files.
15160 @end itemize
15162 @node Getting Internal Debugging Information
15163 @section Getting Internal Debugging Information
15165 @noindent
15166 Most compilers have internal debugging qualifiers and modes. GNAT
15167 does also, except GNAT internal debugging qualifiers and modes are not
15168 secret. A summary and full description of all the compiler and binder
15169 debug flags are in the file @file{DEBUG.ADB}. You must obtain the
15170 sources of the compiler to see the full detailed effects of these flags.
15172 The qualifiers that print the source of the program (reconstructed from
15173 the internal tree) are of general interest for user programs, as are the
15174 options to print
15175 the full internal tree, and the entity table (the symbol table
15176 information). The reconstructed source provides a readable version of the
15177 program after the front-end has completed analysis and  expansion, and is useful
15178 when studying the performance of specific constructs. For example, constraint
15179 checks are indicated, complex aggregates are replaced with loops and
15180 assignments, and tasking primitives are replaced with run-time calls.
15182 @node Stack Traceback
15183 @section Stack Traceback
15184 @cindex traceback
15185 @cindex stack traceback
15186 @cindex stack unwinding
15188 @noindent
15189 Traceback is a mechanism to display the sequence of subprogram calls that
15190 leads to a specified execution point in a program. Often (but not always)
15191 the execution point is an instruction at which an exception has been raised.
15192 This mechanism is also known as @i{stack unwinding} because it obtains
15193 its information by scanning the run-time stack and recovering the activation
15194 records of all active subprograms. Stack unwinding is one of the most
15195 important tools for program debugging.
15197 @noindent
15198 The first entry stored in traceback corresponds to the deepest calling level,
15199 that is to say the subprogram currently executing the instruction
15200 from which we want to obtain the traceback.
15202 @noindent
15203 Note that there is no runtime performance penalty when stack traceback
15204 is enabled and no exception are raised during program execution.
15206 @menu
15207 * Non-Symbolic Traceback::
15208 * Symbolic Traceback::
15209 @end menu
15211 @node Non-Symbolic Traceback
15212 @subsection Non-Symbolic Traceback
15213 @cindex traceback, non-symbolic
15215 @noindent
15216 Note: this feature is not supported on all platforms. See
15217 @file{GNAT.Traceback spec in G-TRACEB.ADS} for a complete list of supported
15218 platforms.
15220 @menu
15221 * Tracebacks From an Unhandled Exception::
15222 * Tracebacks From Exception Occurrences (non-symbolic)::
15223 * Tracebacks From Anywhere in a Program (non-symbolic)::
15224 @end menu
15226 @node Tracebacks From an Unhandled Exception
15227 @subsubsection Tracebacks From an Unhandled Exception
15229 @noindent
15230 A runtime non-symbolic traceback is a list of addresses of call instructions.
15231 To enable this feature you must use the @code{-E}
15232 @code{GNAT BIND}'s option. With this option a stack traceback is stored as part
15233 of exception information. It is possible to retrieve this information using the
15234 standard @code{Ada.Exception.Exception_Information} routine.
15236 @noindent
15237 Let's have a look at a simple example:
15239 @smallexample
15240 @cartouche
15241 @group
15242 procedure STB is
15244    procedure P1 is
15245    begin
15246       raise Constraint_Error;
15247    end P1;
15249    procedure P2 is
15250    begin
15251       P1;
15252    end P2;
15254 begin
15255    P2;
15256 end STB;
15257 @end group
15258 @end cartouche
15259 @end smallexample
15261 @smallexample
15262 $ GNAT MAKE stb /BINDER_QUALIFIERS -E
15263 $ stb
15265 Execution terminated by unhandled exception
15266 Exception name: CONSTRAINT_ERROR
15267 Message: STB.ADB:5
15268 Call stack traceback locations:
15269 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
15270 @end smallexample
15272 @noindent
15273 As we see the traceback lists a sequence of addresses for the unhandled
15274 exception @code{CONSTAINT_ERROR} raised in procedure P1. It is easy to
15275 guess that this exception come from procedure P1. To translate these
15276 addresses into the source lines where the calls appear, the
15277 @code{addr2line} tool, described below, is invaluable. The use of this tool
15278 requires the program to be compiled with debug information.
15280 @smallexample
15281 $ GNAT MAKE -g stb /BINDER_QUALIFIERS -E
15282 $ stb
15284 Execution terminated by unhandled exception
15285 Exception name: CONSTRAINT_ERROR
15286 Message: STB.ADB:5
15287 Call stack traceback locations:
15288 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
15290 $ addr2line --exe=stb 0x401373 0x40138b 0x40139c 0x401335 0x4011c4
15291    0x4011f1 0x77e892a4
15293 00401373 at d:/stb/STB.ADB:5
15294 0040138B at d:/stb/STB.ADB:10
15295 0040139C at d:/stb/STB.ADB:14
15296 00401335 at d:/stb/B~STB.ADB:104
15297 004011C4 at /build/.../CRT1.C:200
15298 004011F1 at /build/.../CRT1.C:222
15299 77E892A4 in ?? at ??:0
15300 @end smallexample
15302 @noindent
15303 @code{addr2line} has a number of other useful options:
15305 @table @code
15306 @item --functions
15307 to get the function name corresponding to any location
15309 @item --demangle=gnat
15310 to use the @b{gnat} decoding mode for the function names. Note that
15311 for binutils version 2.9.x the option is simply @code{--demangle}.
15312 @end table
15314 @smallexample
15315 $ addr2line --exe=stb --functions --demangle=gnat 0x401373 0x40138b
15316    0x40139c 0x401335 0x4011c4 0x4011f1
15318 00401373 in stb.p1 at d:/stb/STB.ADB:5
15319 0040138B in stb.p2 at d:/stb/STB.ADB:10
15320 0040139C in stb at d:/stb/STB.ADB:14
15321 00401335 in main at d:/stb/B~STB.ADB:104
15322 004011C4 in <__mingw_CRTStartup> at /build/.../CRT1.C:200
15323 004011F1 in <mainCRTStartup> at /build/.../CRT1.C:222
15324 @end smallexample
15326 @noindent
15327 From this traceback we can see that the exception was raised in
15328 @file{STB.ADB} at line 5, which was reached from a procedure call in
15329 @file{STB.ADB} at line 10, and so on. The @file{B~STD.ADB} is the binder file,
15330 which contains the call to the main program.
15331 @pxref{Running GNAT BIND}. The remaining entries are assorted runtime routines,
15332 and the output will vary from platform to platform.
15334 @noindent
15335 It is also possible to use @code{GDB} with these traceback addresses to debug
15336 the program. For example, we can break at a given code location, as reported
15337 in the stack traceback:
15339 @smallexample
15340 $ GDB -nw stb
15342 (GDB) break *0x401373
15343 Breakpoint 1 at 0x401373: file STB.ADB, line 5.
15344 @end smallexample
15346 @noindent
15347 It is important to note that the stack traceback addresses
15348 do not change when debug information is included. This is particularly useful
15349 because it makes it possible to release software without debug information (to
15350 minimize object size), get a field report that includes a stack traceback
15351 whenever an internal bug occurs, and then be able to retrieve the sequence
15352 of calls with the same program compiled with debug information.
15354 @node Tracebacks From Exception Occurrences (non-symbolic)
15355 @subsubsection Tracebacks From Exception Occurrences
15357 @noindent
15358 Non-symbolic tracebacks are obtained by using the @code{-E} binder argument.
15359 The stack traceback is attached to the exception information string, and can
15360 be retrieved in an exception handler within the Ada program, by means of the
15361 Ada95 facilities defined in @code{Ada.Exceptions}. Here is a simple example:
15363 @smallexample
15364 @cartouche
15365 @group
15366 with Ada.Text_IO;
15367 with Ada.Exceptions;
15369 procedure STB is
15371    use Ada;
15372    use Ada.Exceptions;
15374    procedure P1 is
15375       K : Positive := 1;
15376    begin
15377       K := K - 1;
15378    exception
15379       when E : others =>
15380          Text_IO.Put_Line (Exception_Information (E));
15381    end P1;
15383    procedure P2 is
15384    begin
15385       P1;
15386    end P2;
15388 begin
15389    P2;
15390 end STB;
15391 @end group
15392 @end cartouche
15393 @end smallexample
15395 @noindent
15396 This program will output:
15398 @smallexample
15399 $ stb
15401 Exception name: CONSTRAINT_ERROR
15402 Message: STB.ADB:12
15403 Call stack traceback locations:
15404 0x4015e4 0x401633 0x401644 0x401461 0x4011c4 0x4011f1 0x77e892a4
15405 @end smallexample
15407 @node Tracebacks From Anywhere in a Program (non-symbolic)
15408 @subsubsection Tracebacks From Anywhere in a Program
15410 @noindent
15411 It is also possible to retrieve a stack traceback from anywhere in a
15412 program. For this you need to
15413 use the @code{GNAT.Traceback} API. This package includes a procedure called
15414 @code{Call_Chain} that computes a complete stack traceback, as well as useful
15415 display procedures described below. It is not necessary to use the
15416 @code{-E GNAT BIND} option in this case, because the stack traceback mechanism
15417 is invoked explicitly.
15419 @noindent
15420 In the following example we compute a traceback at a specific location in
15421 the program, and we display it using @code{GNAT.Debug_Utilities.Image} to
15422 convert addresses to strings:
15424 @smallexample
15425 @cartouche
15426 @group
15427 with Ada.Text_IO;
15428 with GNAT.Traceback;
15429 with GNAT.Debug_Utilities;
15431 procedure STB is
15433    use Ada;
15434    use GNAT;
15435    use GNAT.Traceback;
15437    procedure P1 is
15438       TB  : Tracebacks_Array (1 .. 10);
15439       --  We are asking for a maximum of 10 stack frames.
15440       Len : Natural;
15441       --  Len will receive the actual number of stack frames returned.
15442    begin
15443       Call_Chain (TB, Len);
15445       Text_IO.Put ("In STB.P1 : ");
15447       for K in 1 .. Len loop
15448          Text_IO.Put (Debug_Utilities.Image (TB (K)));
15449          Text_IO.Put (' ');
15450       end loop;
15452       Text_IO.New_Line;
15453    end P1;
15455    procedure P2 is
15456    begin
15457       P1;
15458    end P2;
15460 begin
15461    P2;
15462 end STB;
15463 @end group
15464 @end cartouche
15465 @end smallexample
15467 @smallexample
15468 $ GNAT MAKE stb
15469 $ stb
15471 In STB.P1 : 16#0040_F1E4# 16#0040_14F2# 16#0040_170B# 16#0040_171C#
15472 16#0040_1461# 16#0040_11C4# 16#0040_11F1# 16#77E8_92A4#
15473 @end smallexample
15475 @node Symbolic Traceback
15476 @subsection Symbolic Traceback
15477 @cindex traceback, symbolic
15479 @noindent
15480 A symbolic traceback is a stack traceback in which procedure names are
15481 associated with each code location.
15483 @noindent
15484 Note that this feature is not supported on all platforms. See
15485 @file{GNAT.Traceback.Symbolic spec in G-TRASYM.ADS} for a complete
15486 list of currently supported platforms.
15488 @noindent
15489 Note that the symbolic traceback requires that the program be compiled
15490 with debug information. If it is not compiled with debug information
15491 only the non-symbolic information will be valid.
15493 @menu
15494 * Tracebacks From Exception Occurrences (symbolic)::
15495 * Tracebacks From Anywhere in a Program (symbolic)::
15496 @end menu
15498 @node Tracebacks From Exception Occurrences (symbolic)
15499 @subsubsection Tracebacks From Exception Occurrences
15501 @smallexample
15502 @cartouche
15503 @group
15504 with Ada.Text_IO;
15505 with GNAT.Traceback.Symbolic;
15507 procedure STB is
15509    procedure P1 is
15510    begin
15511       raise Constraint_Error;
15512    end P1;
15514    procedure P2 is
15515    begin
15516       P1;
15517    end P2;
15519    procedure P3 is
15520    begin
15521       P2;
15522    end P3;
15524 begin
15525    P3;
15526 exception
15527    when E : others =>
15528       Ada.Text_IO.Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
15529 end STB;
15530 @end group
15531 @end cartouche
15532 @end smallexample
15534 @smallexample
15535 $ GNAT MAKE -g stb /BINDER_QUALIFIERS -E /LINKER_QUALIFIERS -lgnat -laddr2line -lintl
15536 $ stb
15538 0040149F in stb.p1 at STB.ADB:8
15539 004014B7 in stb.p2 at STB.ADB:13
15540 004014CF in stb.p3 at STB.ADB:18
15541 004015DD in ada.stb at STB.ADB:22
15542 00401461 in main at B~STB.ADB:168
15543 004011C4 in __mingw_CRTStartup at CRT1.C:200
15544 004011F1 in mainCRTStartup at CRT1.C:222
15545 77E892A4 in ?? at ??:0
15546 @end smallexample
15548 @noindent
15549 The exact sequence of linker options may vary from platform to platform.
15550 The above @code{/LINKER_QUALIFIERS} section is for Windows platforms. By contrast,
15551 under Unix there is no need for the @code{/LINKER_QUALIFIERS} section.
15552 Differences across platforms are due to details of linker implementation.
15554 @node Tracebacks From Anywhere in a Program (symbolic)
15555 @subsubsection Tracebacks From Anywhere in a Program
15557 @noindent
15558 It is possible to get a symbolic stack traceback
15559 from anywhere in a program, just as for non-symbolic tracebacks.
15560 The first step is to obtain a non-symbolic
15561 traceback, and then call @code{Symbolic_Traceback} to compute the symbolic
15562 information. Here is an example:
15564 @smallexample
15565 @cartouche
15566 @group
15567 with Ada.Text_IO;
15568 with GNAT.Traceback;
15569 with GNAT.Traceback.Symbolic;
15571 procedure STB is
15573    use Ada;
15574    use GNAT.Traceback;
15575    use GNAT.Traceback.Symbolic;
15577    procedure P1 is
15578       TB  : Tracebacks_Array (1 .. 10);
15579       --  We are asking for a maximum of 10 stack frames.
15580       Len : Natural;
15581       --  Len will receive the actual number of stack frames returned.
15582    begin
15583       Call_Chain (TB, Len);
15584       Text_IO.Put_Line (Symbolic_Traceback (TB (1 .. Len)));
15585    end P1;
15587    procedure P2 is
15588    begin
15589       P1;
15590    end P2;
15592 begin
15593    P2;
15594 end STB;
15595 @end group
15596 @end cartouche
15597 @end smallexample
15599 @node Compatibility with DEC Ada
15600 @chapter Compatibility with DEC Ada
15601 @cindex Compatibility
15603 @noindent
15604 This section of the manual compares DEC Ada for OpenVMS Alpha and GNAT
15605 OpenVMS Alpha. GNAT achieves a high level of compatibility
15606 with DEC Ada, and it should generally be straightforward to port code
15607 from the DEC Ada environment to GNAT. However, there are a few language
15608 and implementation differences of which the user must be aware. These
15609 differences are discussed in this section. In
15610 addition, the operating environment and command structure for the
15611 compiler are different, and these differences are also discussed.
15613 Note that this discussion addresses specifically the implementation
15614 of Ada 83 for DIGITAL OpenVMS Alpha Systems. In cases where the implementation
15615 of DEC Ada differs between OpenVMS Alpha Systems and OpenVMS VAX Systems, GNAT
15616 always follows the Alpha implementation.
15618 @menu
15619 * Ada 95 Compatibility::
15620 * Differences in the Definition of Package System::
15621 * Language-Related Features::
15622 * The Package STANDARD::
15623 * The Package SYSTEM::
15624 * Tasking and Task-Related Features::
15625 * Implementation of Tasks in DEC Ada for OpenVMS Alpha Systems::
15626 * Pragmas and Pragma-Related Features::
15627 * Library of Predefined Units::
15628 * Bindings::
15629 * Main Program Definition::
15630 * Implementation-Defined Attributes::
15631 * Compiler and Run-Time Interfacing::
15632 * Program Compilation and Library Management::
15633 * Input-Output::
15634 * Implementation Limits::
15635 * Tools::
15636 @end menu
15638 @node Ada 95 Compatibility
15639 @section Ada 95 Compatibility
15641 @noindent
15642 GNAT is an Ada 95 compiler, and DEC Ada is an Ada 83
15643 compiler. Ada 95 is almost completely upwards compatible
15644 with Ada 83, and therefore Ada 83 programs will compile
15645 and run under GNAT with
15646 no changes or only minor changes. The Ada 95 Reference
15647 Manual (ANSI/ISO/IEC-8652:1995) provides details on specific
15648 incompatibilities.
15650 GNAT provides the qualifier /83 on the GNAT COMPILE command,
15651 as well as the pragma ADA_83, to force the compiler to
15652 operate in Ada 83 mode. This mode does not guarantee complete
15653 conformance to Ada 83, but in practice is sufficient to
15654 eliminate most sources of incompatibilities.
15655 In particular, it eliminates the recognition of the
15656 additional Ada 95 keywords, so that their use as identifiers
15657 in Ada83 program is legal, and handles the cases of packages
15658 with optional bodies, and generics that instantiate unconstrained
15659 types without the use of @code{(<>)}.
15661 @node Differences in the Definition of Package System
15662 @section Differences in the Definition of Package System
15664 @noindent
15665 Both the Ada 95 and Ada 83 reference manuals permit a compiler to add
15666 implementation-dependent declarations to package System. In normal mode,
15667 GNAT does not take advantage of this permission, and the version of System
15668 provided by GNAT exactly matches that in the Ada 95 Reference Manual.
15670 However, DEC Ada adds an extensive set of declarations to package System,
15671 as fully documented in the DEC Ada manuals. To minimize changes required
15672 for programs that make use of these extensions, GNAT provides the pragma
15673 Extend_System for extending the definition of package System. By using:
15675 @smallexample
15676 @group
15677 @cartouche
15678 @b{pragma} Extend_System (Aux_DEC);
15679 @end cartouche
15680 @end group
15681 @end smallexample
15683 @noindent
15684 The set of definitions in System is extended to include those in package
15685 @code{System.Aux_DEC}.
15686 These definitions are incorporated directly into package
15687 System, as though they had been declared there in the first place. For a
15688 list of the declarations added, see the specification of this package,
15689 which can be found in the file @code{S-AUXDEC.ADS} in the GNAT library.
15690 The pragma Extend_System is a configuration pragma, which means that
15691 it can be placed in the file @file{GNAT.ADC}, so that it will automatically
15692 apply to all subsequent compilations. See the section on Configuration
15693 Pragmas for further details.
15695 An alternative approach that avoids the use of the non-standard
15696 Extend_System pragma is to add a context clause to the unit that
15697 references these facilities:
15699 @smallexample
15700 @group
15701 @cartouche
15702 @b{with} System.Aux_DEC;
15703 @b{use}  System.Aux_DEC;
15704 @end cartouche
15705 @end group
15706 @end smallexample
15708 @noindent
15709 The effect is not quite semantically identical to incorporating the declarations
15710 directly into package @code{System},
15711 but most programs will not notice a difference
15712 unless they use prefix notation (e.g. @code{System.Integer_8})
15713 to reference the
15714 entities directly in package @code{System}.
15715 For units containing such references,
15716 the prefixes must either be removed, or the pragma @code{Extend_System}
15717 must be used.
15719 @node Language-Related Features
15720 @section Language-Related Features
15722 @noindent
15723 The following sections highlight differences in types,
15724 representations of types, operations, alignment, and
15725 related topics.
15727 @menu
15728 * Integer Types and Representations::
15729 * Floating-Point Types and Representations::
15730 * Pragmas Float_Representation and Long_Float::
15731 * Fixed-Point Types and Representations::
15732 * Record and Array Component Alignment::
15733 * Address Clauses::
15734 * Other Representation Clauses::
15735 @end menu
15737 @node Integer Types and Representations
15738 @subsection Integer Types and Representations
15740 @noindent
15741 The set of predefined integer types is identical in DEC Ada and GNAT.
15742 Furthermore the representation of these integer types is also identical,
15743 including the capability of size clauses forcing biased representation.
15745 In addition,
15746 DEC Ada for OpenVMS Alpha systems has defined the
15747 following additional integer types in package System:
15749 @itemize @bullet
15751 @item
15752 INTEGER_8
15754 @item
15755 INTEGER_16
15757 @item
15758 INTEGER_32
15760 @item
15761 INTEGER_64
15763 @item
15764 LARGEST_INTEGER
15765 @end itemize
15767 @noindent
15768 When using GNAT, the first four of these types may be obtained from the
15769 standard Ada 95 package @code{Interfaces}.
15770 Alternatively, by use of the pragma
15771 @code{Extend_System}, identical
15772 declarations can be referenced directly in package @code{System}.
15773 On both GNAT and DEC Ada, the maximum integer size is 64 bits.
15775 @node Floating-Point Types and Representations
15776 @subsection Floating-Point Types and Representations
15777 @cindex Floating-Point types
15779 @noindent
15780 The set of predefined floating-point types is identical in DEC Ada and GNAT.
15781 Furthermore the representation of these floating-point
15782 types is also identical. One important difference is that the default
15783 representation for DEC Ada is VAX_Float, but the default representation
15784 for GNAT is IEEE.
15786 Specific types may be declared to be VAX_Float or IEEE, using the pragma
15787 @code{Float_Representation} as described in the DEC Ada documentation.
15788 For example, the declarations:
15790 @smallexample
15791 @group
15792 @cartouche
15793 @b{type} F_Float @b{is digits} 6;
15794 @b{pragma} Float_Representation (VAX_Float, F_Float);
15795 @end cartouche
15796 @end group
15797 @end smallexample
15799 @noindent
15800 declare a type F_Float that will be represented in VAX_Float format.
15801 This set of declarations actually appears in System.Aux_DEC, which provides
15802 the full set of additional floating-point declarations provided in
15803 the DEC Ada version of package
15804 System. This and similar declarations may be accessed in a user program by using
15805 pragma @code{Extend_System}. The use of this
15806 pragma, and the related pragma @code{Long_Float} is described in further
15807 detail in the following section.
15809 @node Pragmas Float_Representation and Long_Float
15810 @subsection Pragmas Float_Representation and Long_Float
15812 @noindent
15813 DEC Ada provides the pragma @code{Float_Representation}, which
15814 acts as a program library qualifier to allow control over
15815 the internal representation chosen for the predefined
15816 floating-point types declared in the package @code{Standard}.
15817 The format of this pragma is as follows:
15819 @smallexample
15820 @group
15821 @cartouche
15822 @b{pragma} @code{Float_Representation}(VAX_Float | IEEE_Float);
15823 @end cartouche
15824 @end group
15825 @end smallexample
15827 @noindent
15828 This pragma controls the representation of floating-point
15829 types as follows:
15831 @itemize @bullet
15832 @item
15833 @code{VAX_Float} specifies that floating-point
15834 types are represented by default with the VAX hardware types
15835 F-floating, D-floating, G-floating. Note that the H-floating
15836 type is available only on DIGITAL Vax systems, and is not available
15837 in either DEC Ada or GNAT for Alpha systems.
15839 @item
15840 @code{IEEE_Float} specifies that floating-point
15841 types are represented by default with the IEEE single and
15842 double floating-point types.
15843 @end itemize
15845 @noindent
15846 GNAT provides an identical implementation of the pragma
15847 @code{Float_Representation}, except that it functions as a
15848 configuration pragma, as defined by Ada 95. Note that the
15849 notion of configuration pragma corresponds closely to the
15850 DEC Ada notion of a program library qualifier.
15852 When no pragma is used in GNAT, the default is IEEE_Float, which is different
15853 from DEC Ada 83, where the default is VAX_Float. In addition, the
15854 predefined libraries in GNAT are built using IEEE_Float, so it is not
15855 advisable to change the format of numbers passed to standard library
15856 routines, and if necessary explicit type conversions may be needed.
15858 The use of IEEE_Float is recommended in GNAT since it is more efficient,
15859 and (given that it conforms to an international standard) potentially more
15860 portable. The situation in which VAX_Float may be useful is in interfacing
15861 to existing code and data that expects the use of VAX_Float. There are
15862 two possibilities here. If the requirement for the use of VAX_Float is
15863 localized, then the best approach is to use the predefined VAX_Float
15864 types in package @code{System}, as extended by
15865 @code{Extend_System}. For example, use @code{System.F_Float}
15866 to specify the 32-bit @code{F-Float} format.
15868 Alternatively, if an entire program depends heavily on the use of
15869 the @code{VAX_Float} and in particular assumes that the types in
15870 package @code{Standard} are in @code{Vax_Float} format, then it
15871 may be desirable to reconfigure GNAT to assume Vax_Float by default.
15872 This is done by using the GNAT LIBRARY command to rebuild the library, and
15873 then using the general form of the @code{Float_Representation}
15874 pragma to ensure that this default format is used throughout.
15875 The form of the GNAT LIBRARY command is:
15877 @smallexample
15878 GNAT LIBRARY /CONFIG=@i{file} /CREATE=@i{directory}
15879 @end smallexample
15881 @noindent
15882 where @i{file} contains the new configuration pragmas
15883 and @i{directory} is the directory to be created to contain
15884 the new library.
15886 @noindent
15887 On OpenVMS systems, DEC Ada provides the pragma @code{Long_Float}
15888 to allow control over the internal representation chosen
15889 for the predefined type @code{Long_Float} and for floating-point
15890 type declarations with digits specified in the range 7 .. 15.
15891 The format of this pragma is as follows:
15893 @smallexample
15894 @cartouche
15895 @b{pragma} Long_Float (D_FLOAT | G_FLOAT);
15896 @end cartouche
15897 @end smallexample
15899 @node Fixed-Point Types and Representations
15900 @subsection Fixed-Point Types and Representations
15902 @noindent
15903 On DEC Ada for OpenVMS Alpha systems, rounding is
15904 away from zero for both positive and negative numbers.
15905 Therefore, +0.5 rounds to 1 and -0.5 rounds to -1.
15907 On GNAT for OpenVMS Alpha, the results of operations
15908 on fixed-point types are in accordance with the Ada 95
15909 rules. In particular, results of operations on decimal
15910 fixed-point types are truncated.
15912 @node Record and Array Component Alignment
15913 @subsection Record and Array Component Alignment
15915 @noindent
15916 On DEC Ada for OpenVMS Alpha, all non composite components
15917 are aligned on natural boundaries. For example, 1-byte
15918 components are aligned on byte boundaries, 2-byte
15919 components on 2-byte boundaries, 4-byte components on 4-byte
15920 byte boundaries, and so on. The OpenVMS Alpha hardware
15921 runs more efficiently with naturally aligned data.
15923 ON GNAT for OpenVMS Alpha, alignment rules are compatible
15924 with DEC Ada for OpenVMS Alpha.
15926 @node Address Clauses
15927 @subsection Address Clauses
15929 @noindent
15930 In DEC Ada and GNAT, address clauses are supported for
15931 objects and imported subprograms.
15932 The predefined type @code{System.Address} is a private type
15933 in both compilers, with the same representation (it is simply
15934 a machine pointer). Addition, subtraction, and comparison
15935 operations are available in the standard Ada 95 package
15936 @code{System.Storage_Elements}, or in package @code{System}
15937 if it is extended to include @code{System.Aux_DEC} using a
15938 pragma @code{Extend_System} as previously described.
15940 Note that code that with's both this extended package @code{System}
15941 and the package @code{System.Storage_Elements} should not @code{use}
15942 both packages, or ambiguities will result. In general it is better
15943 not to mix these two sets of facilities. The Ada 95 package was
15944 designed specifically to provide the kind of features that DEC Ada
15945 adds directly to package @code{System}.
15947 GNAT is compatible with DEC Ada in its handling of address
15948 clauses, except for some limitations in
15949 the form of address clauses for composite objects with
15950 initialization. Such address clauses are easily replaced
15951 by the use of an explicitly-defined constant as described
15952 in the Ada 95 Reference Manual (13.1(22)). For example, the sequence
15953 of declarations:
15955 @smallexample
15956 @group
15957 @cartouche
15958 X, Y : Integer := Init_Func;
15959 Q : String (X .. Y) := "abc";
15961 @b{for} Q'Address @b{use} Compute_Address;
15962 @end cartouche
15963 @end group
15964 @end smallexample
15966 @noindent
15967 will be rejected by GNAT, since the address cannot be computed at the time
15968 that Q is declared. To achieve the intended effect, write instead:
15970 @smallexample
15971 @group
15972 @cartouche
15973 X, Y : Integer := Init_Func;
15974 Q_Address : @b{constant} Address := Compute_Address;
15975 Q : String (X .. Y) := "abc";
15977 @b{for} Q'Address @b{use} Q_Address;
15978 @end cartouche
15979 @end group
15980 @end smallexample
15982 @noindent
15983 which will be accepted by GNAT (and other Ada 95 compilers), and is also
15984 backwards compatible with Ada 83. A fuller description of the restrictions
15985 on address specifications is found in the GNAT Reference Manual.
15987 @node Other Representation Clauses
15988 @subsection Other Representation Clauses
15990 @noindent
15991 GNAT supports in a compatible manner all the representation
15992 clauses supported by DEC Ada. In addition, it
15993 supports representation clause forms that are new in Ada 95
15994 including COMPONENT_SIZE and SIZE clauses for objects.
15996 @node The Package STANDARD
15997 @section The Package STANDARD
15999 @noindent
16000 The package STANDARD, as implemented by DEC Ada, is fully
16001 described in the Reference Manual for the Ada Programming
16002 Language (ANSI/MIL-STD-1815A-1983) and in the DEC Ada
16003 Language Reference Manual. As implemented by GNAT, the
16004 package STANDARD is described in the Ada 95 Reference
16005 Manual.
16007 In addition, DEC Ada supports the Latin-1 character set in
16008 the type CHARACTER. GNAT supports the Latin-1 character set
16009 in the type CHARACTER and also Unicode (ISO 10646 BMP) in
16010 the type WIDE_CHARACTER.
16012 The floating-point types supported by GNAT are those
16013 supported by DEC Ada, but defaults are different, and are controlled by
16014 pragmas. See @pxref{Floating-Point Types and Representations} for details.
16016 @node The Package SYSTEM
16017 @section The Package SYSTEM
16019 @noindent
16020 DEC Ada provides a system-specific version of the package
16021 SYSTEM for each platform on which the language ships.
16022 For the complete specification of the package SYSTEM, see
16023 Appendix F of the DEC Ada Language Reference Manual.
16025 On DEC Ada, the package SYSTEM includes the following conversion functions:
16026 @itemize @bullet
16027 @item TO_ADDRESS(INTEGER)
16029 @item  TO_ADDRESS(UNSIGNED_LONGWORD)
16031 @item  TO_ADDRESS(universal_integer)
16033 @item  TO_INTEGER(ADDRESS)
16035 @item  TO_UNSIGNED_LONGWORD(ADDRESS)
16037 @item  Function IMPORT_VALUE return UNSIGNED_LONGWORD and the
16038                  functions IMPORT_ADDRESS and IMPORT_LARGEST_VALUE
16039 @end itemize
16041 @noindent
16042 By default, GNAT supplies a version of SYSTEM that matches
16043 the definition given in the Ada 95 Reference Manual.
16044 This
16045 is a subset of the DIGITAL system definitions, which is as
16046 close as possible to the original definitions. The only difference
16047 is that the definition of SYSTEM_NAME is different:
16049 @smallexample
16050 @group
16051 @cartouche
16052 @b{type} Name @b{is} (SYSTEM_NAME_GNAT);
16053 System_Name : @b{constant} Name := SYSTEM_NAME_GNAT;
16054 @end cartouche
16055 @end group
16056 @end smallexample
16058 @noindent
16059 Also, GNAT adds the new Ada 95 declarations for
16060 BIT_ORDER and DEFAULT_BIT_ORDER.
16062 However, the use of the following pragma causes GNAT
16063 to extend the definition of package SYSTEM so that it
16064 encompasses the full set of DIGITAL-specific extensions,
16065 including the functions listed above:
16067 @smallexample
16068 @cartouche
16069 @b{pragma} Extend_System (Aux_DEC);
16070 @end cartouche
16071 @end smallexample
16073 @noindent
16074 The pragma Extend_System is a configuration pragma that
16075 is most conveniently placed in the @file{GNAT.ADC} file. See the
16076 GNAT Reference Manual for further details.
16078 DEC Ada does not allow the recompilation of the package
16079 SYSTEM. Instead DEC Ada provides several pragmas (SYSTEM_
16080 NAME, STORAGE_UNIT, and MEMORY_SIZE) to modify values in
16081 the package SYSTEM. On OpenVMS Alpha systems, the pragma
16082 SYSTEM_NAME takes the enumeration literal OPENVMS_AXP as
16083 its single argument.
16085 GNAT does permit the recompilation of package SYSTEM using
16086 a special qualifier (/STYLE=GNAT) and this qualifier can be used if
16087 it is necessary to change constants in SYSTEM. GNAT does
16088 not permit the specification of SYSTEM_NAME, STORAGE_UNIT
16089 or MEMORY_SIZE by any other means.
16091 On GNAT systems, the pragma SYSTEM_NAME takes the
16092 enumeration literal SYSTEM_NAME_GNAT.
16094 The definitions provided by the use of
16096 @smallexample
16097 pragma Extend_System (AUX_Dec);
16098 @end smallexample
16100 @noindent
16101 are virtually identical to those provided by the DEC Ada 83 package
16102 System. One important difference is that the name of the TO_ADDRESS
16103 function for type UNSIGNED_LONGWORD is changed to TO_ADDRESS_LONG.
16104 See the GNAT Reference manual for a discussion of why this change was
16105 necessary.
16107 @noindent
16108 The version of TO_ADDRESS taking a universal integer argument is in fact
16109 an extension to Ada 83 not strictly compatible with the reference manual.
16110 In GNAT, we are constrained to be exactly compatible with the standard,
16111 and this means we cannot provide this capability. In DEC Ada 83, the
16112 point of this definition is to deal with a call like:
16114 @smallexample
16115 TO_ADDRESS (16#12777#);
16116 @end smallexample
16118 @noindent
16119 Normally, according to the Ada 83 standard, one would expect this to be
16120 ambiguous, since it matches both the INTEGER and UNSIGNED_LONGWORD forms
16121 of TO_ADDRESS. However, in DEC Ada 83, there is no ambiguity, since the
16122 definition using universal_integer takes precedence.
16124 In GNAT, since the version with universal_integer cannot be supplied, it is
16125 not possible to be 100% compatible. Since there are many programs using
16126 numeric constants for the argument to TO_ADDRESS, the decision in GNAT was
16127 to change the name of the function in the UNSIGNED_LONGWORD case, so the
16128 declarations provided in the GNAT version of AUX_Dec are:
16130 @smallexample
16131 function To_Address (X : Integer) return Address;
16132 pragma Pure_Function (To_Address);
16134 function To_Address_Long (X : Unsigned_Longword) return Address;
16135 pragma Pure_Function (To_Address_Long);
16136 @end smallexample
16138 @noindent
16139 This means that programs using TO_ADDRESS for UNSIGNED_LONGWORD must
16140 change the name to TO_ADDRESS_LONG.
16142 @node Tasking and Task-Related Features
16143 @section Tasking and Task-Related Features
16145 @noindent
16146 The concepts relevant to a comparison of tasking on GNAT
16147 and on DEC Ada for OpenVMS Alpha systems are discussed in
16148 the following sections.
16150 For detailed information on concepts related to tasking in
16151 DEC Ada, see the DEC Ada Language Reference Manual and the
16152 relevant run-time reference manual.
16154 @node Implementation of Tasks in DEC Ada for OpenVMS Alpha Systems
16155 @section Implementation of Tasks in DEC Ada for OpenVMS Alpha Systems
16157 @noindent
16158 On OpenVMS Alpha systems, each Ada task (except a passive
16159 task) is implemented as a single stream of execution
16160 that is created and managed by the kernel. On these
16161 systems, DEC Ada tasking support is based on DECthreads,
16162 an implementation of the POSIX standard for threads.
16164 Although tasks are implemented as threads, all tasks in
16165 an Ada program are part of the same process. As a result,
16166 resources such as open files and virtual memory can be
16167 shared easily among tasks. Having all tasks in one process
16168 allows better integration with the programming environment
16169 (the shell and the debugger, for example).
16171 Also, on OpenVMS Alpha systems, DEC Ada tasks and foreign
16172 code that calls DECthreads routines can be used together.
16173 The interaction between Ada tasks and DECthreads routines
16174 can have some benefits. For example when on OpenVMS Alpha,
16175 DEC Ada can call C code that is already threaded.
16176 GNAT on OpenVMS Alpha uses the facilities of DECthreads,
16177 and Ada tasks are mapped to threads.
16179 @menu
16180 * Assigning Task IDs::
16181 * Task IDs and Delays::
16182 * Task-Related Pragmas::
16183 * Scheduling and Task Priority::
16184 * The Task Stack::
16185 * External Interrupts::
16186 @end menu
16188 @node Assigning Task IDs
16189 @subsection Assigning Task IDs
16191 @noindent
16192 The DEC Ada Run-Time Library always assigns %TASK 1 to
16193 the environment task that executes the main program. On
16194 OpenVMS Alpha systems, %TASK 0 is often used for tasks
16195 that have been created but are not yet activated.
16197 On OpenVMS Alpha systems, task IDs are assigned at
16198 activation. On GNAT systems, task IDs are also assigned at
16199 task creation but do not have the same form or values as
16200 task ID values in DEC Ada. There is no null task, and the
16201 environment task does not have a specific task ID value.
16203 @node Task IDs and Delays
16204 @subsection Task IDs and Delays
16206 @noindent
16207 On OpenVMS Alpha systems, tasking delays are implemented
16208 using Timer System Services. The Task ID is used for the
16209 identification of the timer request (the REQIDT parameter).
16210 If Timers are used in the application take care not to use
16211 0 for the identification, because cancelling such a timer
16212 will cancel all timers and may lead to unpredictable results.
16214 @node Task-Related Pragmas
16215 @subsection Task-Related Pragmas
16217 @noindent
16218 Ada supplies the pragma TASK_STORAGE, which allows
16219 specification of the size of the guard area for a task
16220 stack. (The guard area forms an area of memory that has no
16221 read or write access and thus helps in the detection of
16222 stack overflow.) On OpenVMS Alpha systems, if the pragma
16223 TASK_STORAGE specifies a value of zero, a minimal guard
16224 area is created. In the absence of a pragma TASK_STORAGE, a default guard
16225 area is created.
16227 GNAT supplies the following task-related pragmas:
16229 @itemize @bullet
16230 @item  TASK_INFO
16232               This pragma appears within a task definition and
16233               applies to the task in which it appears. The argument
16234               must be of type SYSTEM.TASK_INFO.TASK_INFO_TYPE.
16236 @item  TASK_STORAGE
16238               GNAT implements pragma TASK_STORAGE in the same way as
16239               DEC Ada.
16240               Both DEC Ada and GNAT supply the pragmas PASSIVE,
16241               SUPPRESS, and VOLATILE.
16242 @end itemize
16243 @node Scheduling and Task Priority
16244 @subsection Scheduling and Task Priority
16246 @noindent
16247 DEC Ada implements the Ada language requirement that
16248 when two tasks are eligible for execution and they have
16249 different priorities, the lower priority task does not
16250 execute while the higher priority task is waiting. The DEC
16251 Ada Run-Time Library keeps a task running until either the
16252 task is suspended or a higher priority task becomes ready.
16254 On OpenVMS Alpha systems, the default strategy is round-
16255 robin with preemption. Tasks of equal priority take turns
16256 at the processor. A task is run for a certain period of
16257 time and then placed at the rear of the ready queue for
16258 its priority level.
16260 DEC Ada provides the implementation-defined pragma TIME_SLICE,
16261 which can be used to enable or disable round-robin
16262 scheduling of tasks with the same priority.
16263 See the relevant DEC Ada run-time reference manual for
16264 information on using the pragmas to control DEC Ada task
16265 scheduling.
16267 GNAT follows the scheduling rules of Annex D (real-time
16268 Annex) of the Ada 95 Reference Manual. In general, this
16269 scheduling strategy is fully compatible with DEC Ada
16270 although it provides some additional constraints (as
16271 fully documented in Annex D).
16272 GNAT implements time slicing control in a manner compatible with
16273 DEC Ada 83, by means of the pragma Time_Slice, whose semantics are identical
16274 to the DEC Ada 83 pragma of the same name.
16275 Note that it is not possible to mix GNAT tasking and
16276 DEC Ada 83 tasking in the same program, since the two run times are
16277 not compatible.
16279 @node The Task Stack
16280 @subsection The Task Stack
16282 @noindent
16283 In DEC Ada, a task stack is allocated each time a
16284 non passive task is activated. As soon as the task is
16285 terminated, the storage for the task stack is deallocated.
16286 If you specify a size of zero (bytes) with T'STORAGE_SIZE,
16287 a default stack size is used. Also, regardless of the size
16288 specified, some additional space is allocated for task
16289 management purposes. On OpenVMS Alpha systems, at least
16290 one page is allocated.
16292 GNAT handles task stacks in a similar manner. According to
16293 the Ada 95 rules, it provides the pragma STORAGE_SIZE as
16294 an alternative method for controlling the task stack size.
16295 The specification of the attribute T'STORAGE_SIZE is also
16296 supported in a manner compatible with DEC Ada.
16298 @node External Interrupts
16299 @subsection External Interrupts
16301 @noindent
16302 On DEC Ada, external interrupts can be associated with task entries.
16303 GNAT is compatible with DEC Ada in its handling of external interrupts.
16305 @node Pragmas and Pragma-Related Features
16306 @section Pragmas and Pragma-Related Features
16308 @noindent
16309 Both DEC Ada and GNAT supply all language-defined pragmas
16310 as specified by the Ada 83 standard. GNAT also supplies all
16311 language-defined pragmas specified in the Ada 95 Reference Manual.
16312 In addition, GNAT implements the implementation-defined pragmas
16313 from DEC Ada 83.
16315 @itemize @bullet
16316 @item  AST_ENTRY
16318 @item  COMMON_OBJECT
16320 @item  COMPONENT_ALIGNMENT
16322 @item  EXPORT_EXCEPTION
16324 @item  EXPORT_FUNCTION
16326 @item  EXPORT_OBJECT
16328 @item  EXPORT_PROCEDURE
16330 @item  EXPORT_VALUED_PROCEDURE
16332 @item  FLOAT_REPRESENTATION
16334 @item  IDENT
16336 @item  IMPORT_EXCEPTION
16338 @item  IMPORT_FUNCTION
16340 @item  IMPORT_OBJECT
16342 @item  IMPORT_PROCEDURE
16344 @item  IMPORT_VALUED_PROCEDURE
16346 @item  INLINE_GENERIC
16348 @item  INTERFACE_NAME
16350 @item  LONG_FLOAT
16352 @item  MAIN_STORAGE
16354 @item  PASSIVE
16356 @item  PSET_OBJECT
16358 @item  SHARE_GENERIC
16360 @item  SUPPRESS_ALL
16362 @item  TASK_STORAGE
16364 @item  TIME_SLICE
16366 @item  TITLE
16367 @end itemize
16369 @noindent
16370 These pragmas are all fully implemented, with the exception of @code{Title},
16371 @code{Passive}, and @code{Share_Generic}, which are
16372 recognized, but which have no
16373 effect in GNAT. The effect of @code{Passive} may be obtained by the
16374 use of protected objects in Ada 95. In GNAT, all generics are inlined.
16376 Unlike DEC Ada, the GNAT 'EXPORT_@i{subprogram}' pragmas require
16377 a separate subprogram specification which must appear before the
16378 subprogram body.
16380 GNAT also supplies a number of implementation-defined pragmas as follows:
16381 @itemize @bullet
16382 @item  C_PASS_BY_COPY
16384 @item  EXTEND_SYSTEM
16386 @item  SOURCE_FILE_NAME
16388 @item  UNSUPPRESS
16390 @item  WARNINGS
16392 @item  ABORT_DEFER
16394 @item  ADA_83
16396 @item  ADA_95
16398 @item  ANNOTATE
16400 @item  ASSERT
16402 @item  CPP_CLASS
16404 @item  CPP_CONSTRUCTOR
16406 @item  CPP_DESTRUCTOR
16408 @item  CPP_VIRTUAL
16410 @item  CP_VTABLE
16412 @item  DEBUG
16414 @item  LINKER_ALIAS
16416 @item  LINKER_SECTION
16418 @item  MACHINE_ATTRIBUTE
16420 @item  NO_RETURN
16422 @item  PURE_FUNCTION
16424 @item  SOURCE_REFERENCE
16426 @item  TASK_INFO
16428 @item  UNCHECKED_UNION
16430 @item  UNIMPLEMENTED_UNIT
16432 @item  WEAK_EXTERNAL
16433 @end itemize
16435 @noindent
16436 For full details on these GNAT implementation-defined pragmas, see
16437 the GNAT Reference Manual.
16439 @menu
16440 * Restrictions on the Pragma INLINE::
16441 * Restrictions on the Pragma INTERFACE::
16442 * Restrictions on the Pragma SYSTEM_NAME::
16443 @end menu
16445 @node Restrictions on the Pragma INLINE
16446 @subsection Restrictions on the Pragma INLINE
16448 @noindent
16449 DEC Ada applies the following restrictions to the pragma INLINE:
16450 @itemize @bullet
16451 @item  Parameters cannot be a task type.
16453 @item  Function results cannot be task types, unconstrained
16454 array types, or unconstrained types with discriminants.
16456 @item  Bodies cannot declare the following:
16457 @itemize @bullet
16458 @item  Subprogram body or stub (imported subprogram is allowed)
16460 @item  Tasks
16462 @item  Generic declarations
16464 @item  Instantiations
16466 @item  Exceptions
16468 @item  Access types (types derived from access types allowed)
16470 @item  Array or record types
16472 @item  Dependent tasks
16474 @item  Direct recursive calls of subprogram or containing
16475 subprogram, directly or via a renaming
16477 @end itemize
16478 @end itemize
16480 @noindent
16481 In GNAT, the only restriction on pragma INLINE is that the
16482 body must occur before the call if both are in the same
16483 unit, and the size must be appropriately small. There are
16484 no other specific restrictions which cause subprograms to
16485 be incapable of being inlined.
16487 @node  Restrictions on the Pragma INTERFACE
16488 @subsection  Restrictions on the Pragma INTERFACE
16490 @noindent
16491 The following lists and describes the restrictions on the
16492 pragma INTERFACE on DEC Ada and GNAT:
16493 @itemize @bullet
16494 @item  Languages accepted: Ada, Bliss, C, Fortran, Default.
16495 Default is the default on OpenVMS Alpha systems.
16497 @item  Parameter passing: Language specifies default
16498 mechanisms but can be overridden with an EXPORT pragma.
16500 @itemize @bullet
16501 @item  Ada: Use internal Ada rules.
16503 @item  Bliss, C: Parameters must be mode @code{in}; cannot be
16504 record or task type. Result cannot be a string, an
16505 array, or a record.
16507 @item  Fortran: Parameters cannot be a task. Result cannot
16508 be a string, an array, or a record.
16509 @end itemize
16510 @end itemize
16512 @noindent
16513 GNAT is entirely upwards compatible with DEC Ada, and in addition allows
16514 record parameters for all languages.
16516 @node  Restrictions on the Pragma SYSTEM_NAME
16517 @subsection  Restrictions on the Pragma SYSTEM_NAME
16519 @noindent
16520 For DEC Ada for OpenVMS Alpha, the enumeration literal
16521 for the type NAME is OPENVMS_AXP. In GNAT, the enumeration
16522 literal for the type NAME is SYSTEM_NAME_GNAT.
16524 @node  Library of Predefined Units
16525 @section  Library of Predefined Units
16527 @noindent
16528 A library of predefined units is provided as part of the
16529 DEC Ada and GNAT implementations. DEC Ada does not provide
16530 the package MACHINE_CODE but instead recommends importing
16531 assembler code.
16533 The GNAT versions of the DEC Ada Run-Time Library (ADA$PREDEFINED:)
16534 units are taken from the OpenVMS Alpha version, not the OpenVMS VAX
16535 version. During GNAT installation, the DEC Ada Predefined
16536 Library units are copied into the GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
16537 (aka DECLIB) directory and patched to remove Ada 95 incompatibilities
16538 and to make them interoperable with GNAT, @pxref{Changes to DECLIB}
16539 for details.
16541 The GNAT RTL is contained in
16542 the GNU:[LIB.OPENVMS7_x.2_8_x.ADALIB] (aka ADALIB) directory and
16543 the default search path is set up to find DECLIB units in preference
16544 to ADALIB units with the same name (TEXT_IO, SEQUENTIAL_IO, and DIRECT_IO,
16545 for example).
16547 However, it is possible to change the default so that the
16548 reverse is true, or even to mix them using child package
16549 notation. The DEC Ada 83 units are available as DEC.xxx where xxx
16550 is the package name, and the Ada units are available in the
16551 standard manner defined for Ada 95, that is to say as Ada.xxx. To
16552 change the default, set ADA_INCLUDE_PATH and ADA_OBJECTS_PATH
16553 appropriately. For example, to change the default to use the Ada95
16554 versions do:
16556 @smallexample
16557 $ DEFINE ADA_INCLUDE_PATH GNU:[LIB.OPENVMS7_1.2_8_1.ADAINCLUDE],-
16558                           GNU:[LIB.OPENVMS7_1.2_8_1.DECLIB]
16559 $ DEFINE ADA_OBJECTS_PATH GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB],-
16560                           GNU:[LIB.OPENVMS7_1.2_8_1.DECLIB]
16561 @end smallexample
16563 @menu
16564 * Changes to DECLIB::
16565 @end menu
16567 @node Changes to DECLIB
16568 @subsection Changes to DECLIB
16570 @noindent
16571 The changes made to the DEC Ada predefined library for GNAT and Ada 95
16572 compatibility are minor and include the following:
16574 @itemize @bullet
16575 @item  Adjusting the location of pragmas and record representation
16576 clauses to obey Ada 95 rules
16578 @item  Adding the proper notation to generic formal parameters
16579 that take unconstrained types in instantiation
16581 @item  Adding pragma ELABORATE_BODY to package specifications
16582 that have package bodies not otherwise allowed
16584 @item  Occurrences of the identifier "PROTECTED" are renamed to "PROTECTD".
16585 Currently these are found only in the STARLET package spec.
16586 @end itemize
16588 @noindent
16589 None of the above changes is visible to users.
16591 @node Bindings
16592 @section Bindings
16594 @noindent
16595 On OpenVMS Alpha, DEC Ada provides the following strongly-typed bindings:
16596 @itemize @bullet
16598 @item  Command Language Interpreter (CLI interface)
16600 @item  DECtalk Run-Time Library (DTK interface)
16602 @item  Librarian utility routines (LBR interface)
16604 @item  General Purpose Run-Time Library (LIB interface)
16606 @item  Math Run-Time Library (MTH interface)
16608 @item  National Character Set Run-Time Library (NCS interface)
16610 @item  Compiled Code Support Run-Time Library (OTS interface)
16612 @item  Parallel Processing Run-Time Library (PPL interface)
16614 @item  Screen Management Run-Time Library (SMG interface)
16616 @item  Sort Run-Time Library (SOR interface)
16618 @item  String Run-Time Library (STR interface)
16620 @item STARLET System Library
16621 @findex Starlet
16623 @item  X Window System Version 11R4 and 11R5 (X, XLIB interface)
16625 @item  X Windows Toolkit (XT interface)
16627 @item  X/Motif Version 1.1.3 and 1.2 (XM interface)
16628 @end itemize
16630 @noindent
16631 GNAT provides implementations of these DEC bindings in the DECLIB directory.
16633 The X/Motif bindings used to build DECLIB are whatever versions are in the
16634 DEC Ada ADA$PREDEFINED directory with extension .ADC. The build script will
16635 automatically add a pragma Linker_Options to packages Xm, Xt, and X_Lib
16636 causing the default X/Motif shareable image libraries to be linked in. This
16637 is done via options files named xm.opt, xt.opt, and x_lib.opt (also located
16638 in the DECLIB directory).
16640 It may be necessary to edit these options files to update or correct the
16641 library names if, for example, the newer X/Motif bindings from ADA$EXAMPLES
16642 had been (previous to installing GNAT) copied and renamed to superseded the
16643 default ADA$PREDEFINED versions.
16645 @menu
16646 * Shared Libraries and Options Files::
16647 * Interfaces to C::
16648 @end menu
16650 @node Shared Libraries and Options Files
16651 @subsection Shared Libraries and Options Files
16653 @noindent
16654 When using the DEC Ada
16655 predefined X and Motif bindings, the linking with their shareable images is
16656 done automatically by GNAT LINK. When using other X and Motif bindings, it
16657 is necessary to add the corresponding shareable images to the command line for
16658 GNAT LINK. When linking with shared libraries, or with .OPT files, it is
16659 also necessary to add them to the command line for GNAT LINK.
16661 A shared library to be used with GNAT is built in the same way as other
16662 libraries under VMS. The VMS Link command can be used in standard fashion.
16664 @node Interfaces to C
16665 @subsection Interfaces to C
16667 @noindent
16668 DEC Ada
16669 provides the following Ada types and operations:
16671 @itemize @bullet
16672 @item C types package (C_TYPES)
16674 @item C strings (C_TYPES.NULL_TERMINATED)
16676 @item Other_types (SHORT_INT)
16677 @end itemize
16679 @noindent
16680 Interfacing to C with GNAT, one can use the above approach
16681 described for DEC Ada or the facilities of Annex B of
16682 the Ada 95 Reference Manual (packages INTERFACES.C,
16683 INTERFACES.C.STRINGS and INTERFACES.C.POINTERS). For more
16684 information, see the section "Interfacing to C" in the
16685 GNAT Reference Manual.
16687 The @option{/UPPERCASE_EXTERNALS} qualifier forces default and explicit
16688 @code{External_Name} parameters in pragmas Import and Export
16689 to be uppercased for compatibility with the default behavior
16690 of DEC C. The qualifier has no effect on @code{Link_Name} parameters.
16692 @node Main Program Definition
16693 @section Main Program Definition
16695 @noindent
16696 The following section discusses differences in the
16697 definition of main programs on DEC Ada and GNAT.
16698 On DEC Ada, main programs are defined to meet the
16699 following conditions:
16700 @itemize @bullet
16701 @item  Procedure with no formal parameters (returns 0 upon
16702        normal completion)
16704 @item  Procedure with no formal parameters (returns 42 when
16705        unhandled exceptions are raised)
16707 @item  Function with no formal parameters whose returned value
16708        is of a discrete type
16710 @item  Procedure with one OUT formal of a discrete type for
16711        which a specification of pragma EXPORT_VALUED_PROCEDURE is given.
16713 @end itemize
16715 @noindent
16716 When declared with the pragma EXPORT_VALUED_PROCEDURE,
16717 a main function or main procedure returns a discrete
16718 value whose size is less than 64 bits (32 on VAX systems),
16719 the value is zero- or sign-extended as appropriate.
16720 On GNAT, main programs are defined as follows:
16721 @itemize @bullet
16722 @item  Must be a non-generic, parameter-less subprogram that
16723 is either a procedure or function returning an Ada
16724 STANDARD.INTEGER (the predefined type)
16726 @item  Cannot be a generic subprogram or an instantiation of a
16727 generic subprogram
16728 @end itemize
16730 @node Implementation-Defined Attributes
16731 @section Implementation-Defined Attributes
16733 @noindent
16734 GNAT provides all DEC Ada implementation-defined
16735 attributes.
16737 @node Compiler and Run-Time Interfacing
16738 @section Compiler and Run-Time Interfacing
16740 @noindent
16741 DEC Ada provides the following ways to pass options to the linker (ACS LINK):
16742 @itemize @bullet
16743 @item  /WAIT and /SUBMIT qualifiers
16745 @item  /COMMAND qualifier
16747 @item  /[NO]MAP qualifier
16749 @item  /OUTPUT=file-spec
16751 @item  /[NO]DEBUG and /[NO]TRACEBACK qualifiers
16752 @end itemize
16754 @noindent
16755 To pass options to the linker, GNAT provides the following
16756 qualifiers:
16758 @itemize @bullet
16759 @item   /EXECUTABLE=exec-name
16761 @item   /VERBOSE qualifier
16763 @item   /[NO]DEBUG and /[NO]TRACEBACK qualifiers
16764 @end itemize
16766 @noindent
16767 For more information on these qualifiers, see the section
16768 "Qualifiers for GNAT LINK" in the corresponding section of this Guide.
16769 In DEC Ada, the command-line qualifier /OPTIMIZE is available
16770 to control optimization. DEC Ada also supplies the
16771 following pragmas:
16772 @itemize @bullet
16773 @item  OPTIMIZE
16775 @item  INLINE
16777 @item  INLINE_GENERIC
16779 @item  SUPPRESS_ALL
16781 @item  PASSIVE
16782 @end itemize
16784 @noindent
16785 In GNAT, optimization is controlled strictly by command
16786 line parameters, as described in the corresponding section of this guide.
16787 The DIGITAL pragmas for control of optimization are
16788 recognized but ignored.
16790 Note that in GNAT, the default is optimization off, whereas in DEC Ada 83,
16791 the default is that optimization is turned on.
16793 @node Program Compilation and Library Management
16794 @section Program Compilation and Library Management
16796 @noindent
16797 DEC Ada and GNAT provide a comparable set of commands to
16798 build programs. DEC Ada also provides a program library,
16799 which is a concept that does not exist on GNAT. Instead,
16800 GNAT provides directories of sources that are compiled as
16801 needed.
16803 The following table summarizes
16804 the DEC Ada commands and provides
16805 equivalent GNAT commands. In this table, some GNAT
16806 equivalents reflect the fact that GNAT does not use the
16807 concept of a program library. Instead, it uses a model
16808 in which collections of source and object files are used
16809 in a manner consistent with other languages like C and
16810 Fortran. Therefore, standard system file commands are used
16811 to manipulate these elements. Those GNAT commands are marked with
16812 an asterisk in the table that follows.
16813 Note that, unlike DEC Ada,  none of the GNAT commands accepts wild cards.
16815 @need 1500
16816 @multitable @columnfractions .31 .30 .39
16818 @item @strong{DEC_Ada_Command}
16819 @tab @strong{GNAT_Equivalent}
16820 @tab @strong{Description}
16822 @item  ADA
16823 @tab     GNAT COMPILE
16824 @tab     Invokes the compiler to compile one or more Ada source files.
16826 @item  ACS ATTACH
16827 @tab     No equivalent
16828 @tab     Qualifiers control of terminal from current process running the program
16829                                                 library manager.
16831 @item   ACS CHECK
16832 @tab      GNAT MAKE /DEPENDENCY_LIST
16833 @tab      Forms the execution closure of one
16834           or more compiled units and checks completeness and currency.
16836 @item   ACS COMPILE
16837 @tab      GNAT MAKE /ACTIONS=COMPILE
16838 @tab      Forms the execution closure of one or
16839           more specified units, checks completeness and currency,
16840           identifies units that have revised source files, compiles same,
16841           and recompiles units that are or will become obsolete.
16842           Also completes incomplete generic instantiations.
16844 @item   ACS COPY FOREIGN
16845 @tab      Copy (*)
16846 @tab      Copies a foreign object file into the program library as a
16847           library unit body.
16849 @item   ACS COPY UNIT
16850 @tab      Copy (*)
16851 @tab      Copies a compiled unit from one program library to another.
16853 @item   ACS CREATE LIBRARY
16854 @tab      Create /directory (*)
16855 @tab      Creates a program library.
16857 @item   ACS CREATE SUBLIBRARY
16858 @tab      Create /directory (*)
16859 @tab      Creates a program sublibrary.
16861 @item   ACS DELETE LIBRARY
16862 @tab
16863 @tab       Deletes a program library and its contents.
16865 @item   ACS DELETE SUBLIBRARY
16866 @tab
16867 @tab      Deletes a program sublibrary and its contents.
16869 @item   ACS DELETE UNIT
16870 @tab      Delete @i{file} (*)
16871 @tab      On OpenVMS systems, deletes one or more compiled units from
16872           the current program library.
16874 @item   ACS DIRECTORY
16875 @tab       Directory (*)
16876 @tab       On OpenVMS systems, lists units contained in the current
16877            program library.
16879 @item   ACS ENTER FOREIGN
16880 @tab      Copy (*)
16881 @tab      Allows the import of a foreign body as an Ada library
16882           specification and enters a reference to a pointer.
16884 @item   ACS ENTER UNIT
16885 @tab      Copy (*)
16886 @tab      Enters a reference (pointer) from the current program library to
16887           a unit compiled into another program library.
16889 @item   ACS EXIT
16890 @tab      No equivalent
16891 @tab      Exits from the program library manager.
16893 @item   ACS EXPORT
16894 @tab      Copy (*)
16895 @tab      Creates an object file that contains system-specific object code
16896           for one or more units. With GNAT, object files can simply be copied
16897           into the desired directory.
16899 @item   ACS EXTRACT SOURCE
16900 @tab      Copy (*)
16901 @tab      Allows access to the copied source file for each Ada compilation unit
16903 @item   ACS HELP
16904 @tab      HELP GNAT
16905 @tab      Provides online help.
16907 @item    ACS LINK
16908 @tab       GNAT LINK
16909 @tab       Links an object file containing Ada units into an executable
16910            file.
16912 @item    ACS LOAD
16913 @tab       Copy (*)
16914 @tab       Loads (partially compiles) Ada units into the program library.
16915            Allows loading a program from a collection of files into a library
16916            without knowing the relationship among units.
16918 @item    ACS MERGE
16919 @tab      Copy (*)
16920 @tab      Merges into the current program library, one or more units from
16921           another library where they were modified.
16923 @item    ACS RECOMPILE
16924 @tab       GNAT MAKE /ACTIONS=COMPILE
16925 @tab       Recompiles from   external or copied source files any obsolete
16926            unit in the closure. Also, completes any incomplete generic
16927            instantiations.
16929 @item    ACS REENTER
16930 @tab       GNAT MAKE
16931 @tab       Reenters current references to units compiled after last entered
16932            with the ACS ENTER UNIT command.
16934 @item    ACS SET LIBRARY
16935 @tab       Set default (*)
16936 @tab       Defines a program library to be the compilation context as well
16937            as the target library for compiler output and commands in general.
16939 @item    ACS SET PRAGMA
16940 @tab       Edit GNAT.ADC (*)
16941 @tab       Redefines specified  values of the library characteristics
16942             LONG_ FLOAT, MEMORY_SIZE, SYSTEM_NAME, and @code{Float_Representation}.
16944 @item    ACS SET SOURCE
16945 @tab       define @* ADA_INCLUDE_PATH @i{path} (*)
16946 @tab       Defines the source file search list for the ACS COMPILE  command.
16948 @item    ACS SHOW LIBRARY
16949 @tab       Directory (*)
16950 @tab        Lists information about one or more program libraries.
16952 @item    ACS SHOW PROGRAM
16953 @tab       No equivalent
16954 @tab       Lists information about the execution closure of one or
16955            more units in the program library.
16957 @item    ACS SHOW SOURCE
16958 @tab       Show logical @* ADA_INCLUDE_PATH
16959 @tab       Shows the source file search used when compiling units.
16961 @item    ACS SHOW VERSION
16962 @tab       Compile with VERBOSE option
16963 @tab       Displays the version number of the compiler and program library
16964            manager used.
16966 @item    ACS SPAWN
16967 @tab        No equivalent
16968 @tab        Creates a subprocess of the current process (same as DCL SPAWN
16969             command).
16971 @item    ACS VERIFY
16972 @tab       No equivalent
16973 @tab       Performs a series of consistency checks on a program library to
16974            determine whether the library structure and library files are in
16975            valid_form.
16977 @end multitable
16979 @noindent
16981 @node Input-Output
16982 @section Input-Output
16984 @noindent
16985 On OpenVMS Alpha systems, DEC Ada uses OpenVMS Record
16986 Management Services (RMS) to perform operations on
16987 external files.
16989 @noindent
16990 DEC Ada and GNAT predefine an identical set of input-
16991 output packages. To make the use of the
16992 generic TEXT_IO operations more convenient, DEC Ada
16993 provides predefined library packages that instantiate the
16994 integer and floating-point operations for the predefined
16995 integer and floating-point types as shown in the following table.
16997 @table @code
16999 @item   Package_Name
17000       Instantiation
17002 @item   INTEGER_TEXT_IO
17003       INTEGER_IO(INTEGER)
17005 @item   SHORT_INTEGER_TEXT_IO
17006      INTEGER_IO(SHORT_INTEGER)
17008 @item   SHORT_SHORT_INTEGER_TEXT_IO
17009      INTEGER_IO(SHORT_SHORT_ INTEGER)
17011 @item   FLOAT_TEXT_IO
17012       FLOAT_IO(FLOAT)
17014 @item   LONG_FLOAT_TEXT_IO
17015       FLOAT_IO(LONG_FLOAT)
17016 @end table
17018 @noindent
17019 The DEC Ada predefined packages and their operations
17020 are implemented using OpenVMS Alpha files and input-
17021 output facilities. DEC Ada supports asynchronous input-
17022 output on OpenVMS Alpha. Familiarity with the following is
17023 recommended:
17024 @itemize @bullet
17025 @item  RMS file organizations and access methods
17027 @item  OpenVMS file specifications and directories
17029 @item  OpenVMS File Definition Language (FDL)
17030 @end itemize
17032 @noindent
17033 GNAT provides I/O facilities that are completely
17034 compatible with DEC Ada. The distribution includes the
17035 standard DEC Ada versions of all I/O packages, operating
17036 in a manner compatible with DEC Ada. In particular, the
17037 following packages are by default the DEC Ada (Ada 83)
17038 versions of these packages rather than the renamings
17039 suggested in annex J of the Ada 95 Reference Manual:
17040 @itemize @bullet
17041 @item  TEXT_IO
17043 @item  SEQUENTIAL_IO
17045 @item  DIRECT_IO
17046 @end itemize
17048 @noindent
17049 The use of the standard Ada 95 syntax for child packages (for
17050 example, ADA.TEXT_IO) retrieves the Ada 95 versions of these
17051 packages, as defined in the Ada 95 Reference Manual.
17052 GNAT provides DIGITAL-compatible predefined instantiations
17053 of the TEXT_IO packages, and also
17054 provides the standard predefined instantiations required
17055 by the Ada 95 Reference Manual.
17057 For further information on how GNAT interfaces to the file
17058 system or how I/O is implemented in programs written in
17059 mixed languages, see the chapter "Implementation of the
17060 Standard I/O" in the GNAT Reference Manual.
17061 This chapter covers the following:
17062 @itemize @bullet
17063 @item  Standard I/O packages
17065 @item  FORM strings
17067 @item  DIRECT_IO
17069 @item  SEQUENTIAL_IO
17071 @item  TEXT_IO
17073 @item  Stream pointer positioning
17075 @item  Reading and writing non-regular files
17077 @item  GET_IMMEDIATE
17079 @item  Treating TEXT_IO files as streams
17081 @item  Shared files
17083 @item  Open modes
17084 @end itemize
17086 @node Implementation Limits
17087 @section Implementation Limits
17089 @noindent
17090 The following table lists implementation limits for DEC Ada and GNAT systems.
17091 @multitable @columnfractions .60 .20 .20
17092 @item  Compilation Parameter
17093 @tab   DEC Ada
17094 @tab   GNAT
17096 @item  In a subprogram or entry  declaration, maximum number of
17097        formal parameters that are of an unconstrained record type
17098 @tab   32
17099 @tab   No set limit
17101 @item  Maximum identifier length (number of characters)
17102 @tab   255
17103 @tab   255
17105 @item  Maximum number of characters in a source line
17106 @tab   255
17107 @tab   255
17109 @item  Maximum collection size   (number of bytes)
17110 @tab   2**31-1
17111 @tab   2**31-1
17113 @item  Maximum number of discriminants for a record type
17114 @tab   245
17115 @tab   No set limit
17117 @item  Maximum number of formal parameters in an entry or
17118        subprogram declaration
17119 @tab   246
17120 @tab    No set limit
17122 @item  Maximum number of dimensions in an array type
17123 @tab   255
17124 @tab   No set limit
17126 @item  Maximum number of library  units and subunits in a compilation.
17127 @tab   4095
17128 @tab   No set limit
17130 @item  Maximum number of library units and subunits in an execution.
17131 @tab   16383
17132 @tab   No set limit
17134 @item  Maximum number of objects declared with the pragma COMMON_OBJECT
17135        or PSECT_OBJECT
17136 @tab   32757
17137 @tab   No set limit
17139 @item  Maximum number of enumeration literals in an enumeration type
17140        definition
17141 @tab   65535
17142 @tab   No set limit
17144 @item  Maximum number of lines in a source file
17145 @tab   65534
17146 @tab   No set limit
17148 @item  Maximum number of bits in any object
17149 @tab   2**31-1
17150 @tab   2**31-1
17152 @item  Maximum size of the static portion of a stack frame (approximate)
17153 @tab   2**31-1
17154 @tab   2**31-1
17155 @end multitable
17157 @node  Tools
17158 @section Tools
17161 @node Inline Assembler
17162 @chapter Inline Assembler
17164 @noindent
17165 If you need to write low-level software that interacts directly with the hardware, Ada provides two ways to incorporate assembly language code into your program.  First, you can import and invoke external routines written in assembly language, an Ada feature fully supported by GNAT.  However, for small sections of code it may be simpler or more efficient to include assembly language statements directly in your Ada source program, using the facilities of the implementation-defined package @code{System.Machine_Code}, which incorporates the GNAT COMPILE Inline Assembler.  The Inline Assembler approach offers a number of advantages, including the following:
17167 @itemize @bullet
17168 @item No need to use non-Ada tools
17169 @item Consistent interface over different targets
17170 @item Automatic usage of the proper calling conventions
17171 @item Access to Ada constants and variables
17172 @item Definition of intrinsic routines
17173 @item Possibility of inlining a subprogram comprising assembler code
17174 @item Code optimizer can take Inline Assembler code into account
17175 @end itemize
17177 This chapter presents a series of examples to show you how to use the Inline Assembler.  Although it focuses on the Intel x86, the general approach applies also to other processors.  It is assumed that you are familiar with Ada and with assembly language programming.
17179 @menu
17180 * Basic Assembler Syntax::
17181 * A Simple Example of Inline Assembler::
17182 * Output Variables in Inline Assembler::
17183 * Input Variables in Inline Assembler::
17184 * Inlining Inline Assembler Code::
17185 * Other Asm Functionality::
17186 * A Complete Example::
17187 @end menu
17189 @c ---------------------------------------------------------------------------
17190 @node Basic Assembler Syntax
17191 @section Basic Assembler Syntax
17193 @noindent
17194 The assembler used by GNAT and GNAT COMPILE is based not on the Intel assembly language, but rather on a
17195 language that descends from the AT&T Unix assembler @emph{as} (and which is often
17196 referred to as ``AT&T syntax'').
17197 The following table summarizes the main features of @emph{as} syntax and points out the differences from the Intel conventions.
17198 See the GNAT COMPILE @emph{as} and @emph{gas} (an @emph{as} macro
17199 pre-processor) documentation for further information.
17201 @table @asis
17202 @item Register names
17203 GNAT COMPILE / @emph{as}: Prefix with ``%''; for example @code{%eax}
17205 Intel: No extra punctuation; for example @code{eax}
17207 @item Immediate operand
17208 GNAT COMPILE / @emph{as}: Prefix with ``$''; for example @code{$4}
17210 Intel: No extra punctuation; for example @code{4}
17212 @item Address
17213 GNAT COMPILE / @emph{as}: Prefix with ``$''; for example @code{$loc}
17215 Intel: No extra punctuation; for example @code{loc}
17217 @item Memory contents
17218 GNAT COMPILE / @emph{as}: No extra punctuation; for example @code{loc}
17220 Intel: Square brackets; for example @code{[loc]}
17222 @item Register contents
17223 GNAT COMPILE / @emph{as}: Parentheses; for example @code{(%eax)}
17225 Intel: Square brackets; for example @code{[eax]}
17227 @item Hexadecimal numbers
17228 GNAT COMPILE / @emph{as}: Leading ``0x'' (C language syntax); for example @code{0xA0}
17230 Intel: Trailing ``h''; for example @code{A0h}
17232 @item Operand size
17233 GNAT COMPILE / @emph{as}: Explicit in op code; for example @code{movw} to move a 16-bit word
17235 Intel: Implicit, deduced by assembler; for example @code{mov}
17237 @item Instruction repetition
17238 GNAT COMPILE / @emph{as}: Split into two lines; for example
17240 @code{rep}
17242 @code{stosl}
17244 Intel: Keep on one line; for example @code{rep stosl}
17246 @item Order of operands
17247 GNAT COMPILE / @emph{as}: Source first; for example @code{movw $4, %eax}
17249 Intel: Destination first; for example @code{mov eax, 4}
17250 @end table
17252 @c ---------------------------------------------------------------------------
17253 @node A Simple Example of Inline Assembler
17254 @section A Simple Example of Inline Assembler
17256 @noindent
17257 The following example will generate a single assembly language statement, @code{nop}, which does nothing.  Despite its lack of run-time effect, the example will be useful in illustrating the basics of the Inline Assembler facility.
17259 @smallexample
17260 @group
17261 with System.Machine_Code; use System.Machine_Code;
17262 procedure Nothing is
17263 begin
17264    Asm ("nop");
17265 end Nothing;
17266 @end group
17267 @end smallexample
17269 @code{Asm} is a procedure declared in package @code{System.Machine_Code}; here it takes one parameter, a @emph{template string} that must be a static expression and that will form the generated instruction.
17270 @code{Asm} may be regarded as a compile-time procedure that parses the template string and additional parameters (none here), from which it generates a sequence of assembly language instructions.
17272 The examples in this chapter will illustrate several of the forms for invoking @code{Asm}; a complete specification of the syntax is found in the @cite{GNAT Reference Manual}.
17274 Under the standard GNAT conventions, the @code{Nothing} procedure should be in a file named @file{NOTHING.ADB}.  You can build the executable in the usual way:
17275 @smallexample
17276 GNAT MAKE nothing
17277 @end smallexample
17278 However, the interesting aspect of this example is not its run-time behavior but rather the
17279 generated assembly code.  To see this output, invoke the compiler as follows:
17280 @smallexample
17281    GNAT COMPILE -S -fomit-frame-pointer /CHECKS=SUPPRESS_ALL @file{NOTHING.ADB}
17282 @end smallexample
17283 where the options are:
17285 @table @code
17286 @item -c
17287 compile only (no bind or link)
17288 @item -S
17289 generate assembler listing
17290 @item -fomit-frame-pointer
17291 do not set up separate stack frames
17292 @item /CHECKS=SUPPRESS_ALL
17293 do not add runtime checks
17294 @end table
17296 This gives a human-readable assembler version of the code. The resulting
17297 file will have the same name as the Ada source file, but with a @code{.s} extension.
17298 In our example, the file @file{nothing.s} has the following contents:
17300 @smallexample
17301 @group
17302 .file "NOTHING.ADB"
17303 gcc2_compiled.:
17304 ___gnu_compiled_ada:
17305 .text
17306    .align 4
17307 .globl __ada_nothing
17308 __ada_nothing:
17309 #APP
17310    nop
17311 #NO_APP
17312    jmp L1
17313    .align 2,0x90
17315    ret
17316 @end group
17317 @end smallexample
17319 The assembly code you included is clearly indicated by
17320 the compiler, between the @code{#APP} and @code{#NO_APP}
17321 delimiters. The character before the 'APP' and 'NOAPP'
17322 can differ on different targets. For example, Linux uses '#APP' while
17323 on NT you will see '/APP'.
17325 If you make a mistake in your assembler code (such as using the
17326 wrong size modifier, or using a wrong operand for the instruction) GNAT
17327 will report this error in a temporary file, which will be deleted when
17328 the compilation is finished.  Generating an assembler file will help
17329 in such cases, since you can assemble this file separately using the
17330 @emph{as} assembler that comes with GNAT COMPILE.
17332 Assembling the file using the command
17334 @smallexample
17335 as @file{nothing.s}
17336 @end smallexample
17337 @noindent
17338 will give you error messages whose lines correspond to the assembler
17339 input file, so you can easily find and correct any mistakes you made.
17340 If there are no errors, @emph{as} will generate an object file @file{nothing.out}.
17342 @c ---------------------------------------------------------------------------
17343 @node Output Variables in Inline Assembler
17344 @section Output Variables in Inline Assembler
17346 @noindent
17347 The examples in this section, showing how to access the processor flags, illustrate how to specify the destination operands for assembly language statements.
17349 @smallexample
17350 @group
17351 with Interfaces; use Interfaces;
17352 with Ada.Text_IO; use Ada.Text_IO;
17353 with System.Machine_Code; use System.Machine_Code;
17354 procedure Get_Flags is
17355    Flags : Unsigned_32;
17356    use ASCII;
17357 begin
17358    Asm ("pushfl"          & LF & HT & -- push flags on stack
17359         "popl %%eax"      & LF & HT & -- load eax with flags
17360         "movl %%eax, %0",             -- store flags in variable
17361         Outputs => Unsigned_32'Asm_Output ("=g", Flags));
17362    Put_Line ("Flags register:" & Flags'Img);
17363 end Get_Flags;
17364 @end group
17365 @end smallexample
17367 In order to have a nicely aligned assembly listing, we have separated
17368 multiple assembler statements in the Asm template string with linefeed (ASCII.LF)
17369 and horizontal tab (ASCII.HT) characters.  The resulting section of the
17370 assembly output file is:
17372 @smallexample
17373 @group
17374 #APP
17375    pushfl
17376    popl %eax
17377    movl %eax, -40(%ebp)
17378 #NO_APP
17379 @end group
17380 @end smallexample
17382 It would have been legal to write the Asm invocation as:
17384 @smallexample
17385 Asm ("pushfl popl %%eax movl %%eax, %0")
17386 @end smallexample
17388 but in the generated assembler file, this would come out as:
17390 @smallexample
17391 #APP
17392    pushfl popl %eax movl %eax, -40(%ebp)
17393 #NO_APP
17394 @end smallexample
17396 which is not so convenient for the human reader.
17398 We use Ada comments
17399 at the end of each line to explain what the assembler instructions
17400 actually do.  This is a useful convention.
17402 When writing Inline Assembler instructions, you need to precede each register and variable name with a percent sign.  Since the assembler already requires a percent sign at the beginning of a register name, you need two consecutive percent signs for such names in the Asm template string, thus @code{%%eax}.  In the generated assembly code, one of the percent signs will be stripped off.
17404 Names such as @code{%0}, @code{%1}, @code{%2}, etc., denote input or output variables: operands you later define using @code{Input} or @code{Output} parameters to @code{Asm}.
17405 An output variable is illustrated in
17406 the third statement in the Asm template string:
17407 @smallexample
17408 movl %%eax, %0
17409 @end smallexample
17410 The intent is to store the contents of the eax register in a variable that can be accessed in Ada.  Simply writing @code{movl %%eax, Flags} would not necessarily work, since the compiler might optimize by using a register to hold Flags, and the expansion of the @code{movl} instruction would not be aware of this optimization.  The solution is not to store the result directly but rather to advise the compiler to choose the correct operand form; that is the purpose of the @code{%0} output variable.
17412 Information about the output variable is supplied in the @code{Outputs} parameter to @code{Asm}:
17413 @smallexample
17414 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
17415 @end smallexample
17417 The output is defined by the @code{Asm_Output} attribute of the target type; the general format is
17418 @smallexample
17419 Type'Asm_Output (constraint_string, variable_name)
17420 @end smallexample
17422 The constraint string directs the compiler how
17423 to store/access the associated variable.  In the example
17424 @smallexample
17425 Unsigned_32'Asm_Output ("=m", Flags);
17426 @end smallexample
17427 the @code{"m"} (memory) constraint tells the compiler that the variable
17428 @code{Flags} should be stored in a memory variable, thus preventing
17429 the optimizer from keeping it in a register.  In contrast,
17430 @smallexample
17431 Unsigned_32'Asm_Output ("=r", Flags);
17432 @end smallexample
17433 uses the @code{"r"} (register) constraint, telling the compiler to
17434 store the variable in a register.
17436 If the constraint is preceded by the equal character (@strong{=}), it tells the
17437 compiler that the variable will be used to store data into it.
17439 In the @code{Get_Flags} example, we used the "g" (global) constraint, allowing the optimizer
17440 to choose whatever it deems best.
17442 There are a fairly large number of constraints, but the ones that are most useful (for the Intel x86 processor) are the following:
17444 @table @code
17445 @item =
17446 output constraint
17447 @item g
17448 global (i.e. can be stored anywhere)
17449 @item m
17450 in memory
17451 @item I
17452 a constant
17453 @item a
17454 use eax
17455 @item b
17456 use ebx
17457 @item c
17458 use ecx
17459 @item d
17460 use edx
17461 @item S
17462 use esi
17463 @item D
17464 use edi
17465 @item r
17466 use one of eax, ebx, ecx or edx
17467 @item q
17468 use one of eax, ebx, ecx, edx, esi or edi
17469 @end table
17471 The full set of constraints is described in the GNAT COMPILE and @emph{as} documentation; note that it is possible to combine certain constraints in one constraint string.
17473 You specify the association of an output variable with an assembler operand through the @code{%}@emph{n} notation, where @emph{n} is a non-negative integer.  Thus in
17474 @smallexample
17475 @group
17476 Asm ("pushfl"          & LF & HT & -- push flags on stack
17477      "popl %%eax"      & LF & HT & -- load eax with flags
17478      "movl %%eax, %0",             -- store flags in variable
17479      Outputs => Unsigned_32'Asm_Output ("=g", Flags));
17480 @end group
17481 @end smallexample
17482 @noindent
17483 @code{%0} will be replaced in the expanded code by the appropriate operand,
17484 whatever
17485 the compiler decided for the @code{Flags} variable.
17487 In general, you may have any number of output variables:
17488 @itemize @bullet
17489 @item
17490 Count the operands starting at 0; thus @code{%0}, @code{%1}, etc.
17491 @item
17492 Specify the @code{Outputs} parameter as a parenthesized comma-separated list of @code{Asm_Output} attributes
17493 @end itemize
17495 For example:
17496 @smallexample
17497 @group
17498 Asm ("movl %%eax, %0" & LF & HT &
17499      "movl %%ebx, %1" & LF & HT &
17500      "movl %%ecx, %2",
17501      Outputs => (Unsigned_32'Asm_Output ("=g", Var_A),   --  %0 = Var_A
17502                  Unsigned_32'Asm_Output ("=g", Var_B),   --  %1 = Var_B
17503                  Unsigned_32'Asm_Output ("=g", Var_C))); --  %2 = Var_C
17504 @end group
17505 @end smallexample
17506 @noindent
17507 where @code{Var_A}, @code{Var_B}, and @code{Var_C} are variables in the Ada program.
17509 As a variation on the @code{Get_Flags} example, we can use the constraints string to direct the compiler to store the eax register into the @code{Flags} variable, instead of including the store instruction explicitly in the @code{Asm} template string:
17511 @smallexample
17512 @group
17513 with Interfaces; use Interfaces;
17514 with Ada.Text_IO; use Ada.Text_IO;
17515 with System.Machine_Code; use System.Machine_Code;
17516 procedure Get_Flags_2 is
17517    Flags : Unsigned_32;
17518    use ASCII;
17519 begin
17520    Asm ("pushfl"      & LF & HT & -- push flags on stack
17521         "popl %%eax",             -- save flags in eax
17522         Outputs => Unsigned_32'Asm_Output ("=a", Flags));
17523    Put_Line ("Flags register:" & Flags'Img);
17524 end Get_Flags_2;
17525 @end group
17526 @end smallexample
17528 @noindent
17529 The @code{"a"} constraint tells the compiler that the @code{Flags}
17530 variable will come from the eax register. Here is the resulting code:
17532 @smallexample
17533 @group
17534 #APP
17535    pushfl
17536    popl %eax
17537 #NO_APP
17538    movl %eax,-40(%ebp)
17539 @end group
17540 @end smallexample
17542 @noindent
17543 The compiler generated the store of eax into Flags after
17544 expanding the assembler code.
17546 Actually, there was no need to pop the flags into the eax register; more simply, we could just pop the flags directly into the program variable:
17548 @smallexample
17549 @group
17550 with Interfaces; use Interfaces;
17551 with Ada.Text_IO; use Ada.Text_IO;
17552 with System.Machine_Code; use System.Machine_Code;
17553 procedure Get_Flags_3 is
17554    Flags : Unsigned_32;
17555    use ASCII;
17556 begin
17557    Asm ("pushfl"  & LF & HT & -- push flags on stack
17558         "pop %0",             -- save flags in Flags
17559         Outputs => Unsigned_32'Asm_Output ("=g", Flags));
17560    Put_Line ("Flags register:" & Flags'Img);
17561 end Get_Flags_3;
17562 @end group
17563 @end smallexample
17565 @c ---------------------------------------------------------------------------
17566 @node Input Variables in Inline Assembler
17567 @section Input Variables in Inline Assembler
17569 @noindent
17570 The example in this section illustrates how to specify the source operands for assembly language statements.  The program simply increments its input value by 1:
17572 @smallexample
17573 @group
17574 with Interfaces; use Interfaces;
17575 with Ada.Text_IO; use Ada.Text_IO;
17576 with System.Machine_Code; use System.Machine_Code;
17577 procedure Increment is
17579    function Incr (Value : Unsigned_32) return Unsigned_32 is
17580       Result : Unsigned_32;
17581    begin
17582       Asm ("incl %0",
17583            Inputs  => Unsigned_32'Asm_Input ("a", Value),
17584            Outputs => Unsigned_32'Asm_Output ("=a", Result));
17585       return Result;
17586    end Incr;
17588    Value : Unsigned_32;
17590 begin
17591    Value := 5;
17592    Put_Line ("Value before is" & Value'Img);
17593    Value := Incr (Value);
17594    Put_Line ("Value after is" & Value'Img);
17595 end Increment;
17596 @end group
17597 @end smallexample
17599 The @code{Outputs} parameter to @code{Asm} specifies
17600 that the result will be in the eax register and that it is to be stored in the @code{Result}
17601 variable.
17603 The @code{Inputs} parameter looks much like the @code{Outputs} parameter, but with an
17604 @code{Asm_Input} attribute. The
17605 @code{"="} constraint, indicating an output value, is not present.
17607 You can have multiple input variables, in the same way that you can have more
17608 than one output variable.
17610 The parameter count (%0, %1) etc, now starts at the first input
17611 statement, and continues with the output statements.
17612 When both parameters use the same variable, the
17613 compiler will treat them as the same %n operand, which is the case here.
17615 Just as the @code{Outputs} parameter causes the register to be stored into the
17616 target variable after execution of the assembler statements, so does the
17617 @code{Inputs} parameter cause its variable to be loaded into the register before execution
17618 of the
17619 assembler statements.
17621 Thus the effect of the @code{Asm} invocation is:
17622 @enumerate
17623 @item load the 32-bit value of @code{Value} into eax
17624 @item execute the @code{incl %eax} instruction
17625 @item store the contents of eax into the @code{Result} variable
17626 @end enumerate
17628 The resulting assembler file (with @code{/OPTIMIZE=ALL} optimization) contains:
17629 @smallexample
17630 @group
17631 _increment__incr.1:
17632    subl $4,%esp
17633    movl 8(%esp),%eax
17634 #APP
17635    incl %eax
17636 #NO_APP
17637    movl %eax,%edx
17638    movl %ecx,(%esp)
17639    addl $4,%esp
17640    ret
17641 @end group
17642 @end smallexample
17644 @c ---------------------------------------------------------------------------
17645 @node Inlining Inline Assembler Code
17646 @section Inlining Inline Assembler Code
17648 @noindent
17649 For a short subprogram such as the @code{Incr} function in the previous section, the overhead of the call and return (creating / deleting the stack frame)
17650 can be significant, compared to the amount of code in the subprogram body.
17651 A solution is to apply Ada's @code{Inline} pragma to the subprogram,
17652 which directs the compiler to expand invocations of the subprogram at the point(s)
17653 of call, instead of setting up a stack frame for out-of-line calls.
17654 Here is the resulting program:
17656 @smallexample
17657 @group
17658 with Interfaces; use Interfaces;
17659 with Ada.Text_IO; use Ada.Text_IO;
17660 with System.Machine_Code; use System.Machine_Code;
17661 procedure Increment_2 is
17663    function Incr (Value : Unsigned_32) return Unsigned_32 is
17664       Result : Unsigned_32;
17665    begin
17666       Asm ("incl %0",
17667            Inputs  => Unsigned_32'Asm_Input ("a", Value),
17668            Outputs => Unsigned_32'Asm_Output ("=a", Result));
17669       return Result;
17670    end Incr;
17671    pragma Inline (Increment);
17673    Value : Unsigned_32;
17675 begin
17676    Value := 5;
17677    Put_Line ("Value before is" & Value'Img);
17678    Value := Increment (Value);
17679    Put_Line ("Value after is" & Value'Img);
17680 end Increment_2;
17681 @end group
17682 @end smallexample
17684 Compile the program with both optimization (@code{/OPTIMIZE=ALL}) and inlining
17685 enabled (@option{-gnatpn} instead of @option{/CHECKS=SUPPRESS_ALL}).
17687 The @code{Incr} function is still compiled as usual, but at the
17688 point in @code{Increment} where our function used to be called:
17690 @smallexample
17691 @group
17692 pushl %edi
17693 call _increment__incr.1
17694 @end group
17695 @end smallexample
17697 @noindent
17698 the code for the function body directly appears:
17700 @smallexample
17701 @group
17702 movl %esi,%eax
17703 #APP
17704    incl %eax
17705 #NO_APP
17706    movl %eax,%edx
17707 @end group
17708 @end smallexample
17710 @noindent
17711 thus saving the overhead of stack frame setup and an out-of-line call.
17713 @c ---------------------------------------------------------------------------
17714 @node Other Asm Functionality
17715 @section Other @code{Asm} Functionality
17717 @noindent
17718 This section describes two important parameters to the @code{Asm} procedure: @code{Clobber}, which identifies register usage; and @code{Volatile}, which inhibits unwanted optimizations.
17720 @menu
17721 * The Clobber Parameter::
17722 * The Volatile Parameter::
17723 @end menu
17725 @c ---------------------------------------------------------------------------
17726 @node The Clobber Parameter
17727 @subsection The @code{Clobber} Parameter
17729 @noindent
17730 One of the dangers of intermixing assembly language and a compiled language such as Ada is
17731 that the compiler needs to be aware of which registers are being used by the assembly code.
17732 In some cases, such as the earlier examples, the constraint string is sufficient to
17733 indicate register usage (e.g. "a" for the eax register).  But more generally, the
17734 compiler needs an explicit identification of the registers that are used by the Inline
17735 Assembly statements.
17737 Using a register that the compiler doesn't know about
17738 could be a side effect of an instruction (like @code{mull}
17739 storing its result in both eax and edx).
17740 It can also arise from explicit register usage in your
17741 assembly code; for example:
17742 @smallexample
17743 @group
17744 Asm ("movl %0, %%ebx" & LF & HT &
17745      "movl %%ebx, %1",
17746      Inputs  => Unsigned_32'Asm_Input  ("g", Var_In),
17747      Outputs => Unsigned_32'Asm_Output ("=g", Var_Out));
17748 @end group
17749 @end smallexample
17750 @noindent
17751 where the compiler (since it does not analyze the @code{Asm} template string)
17752 does not know you are using the ebx register.
17754 In such cases you need to supply the @code{Clobber} parameter to @code{Asm},
17755 to identify the registers that will be used by your assembly code:
17757 @smallexample
17758 @group
17759 Asm ("movl %0, %%ebx" & LF & HT &
17760      "movl %%ebx, %1",
17761      Inputs  => Unsigned_32'Asm_Input  ("g", Var_In),
17762      Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
17763      Clobber => "ebx");
17764 @end group
17765 @end smallexample
17767 The Clobber parameter is a static string expression specifying the
17768 register(s) you are using.  Note that register names are @emph{not} prefixed by a percent sign.
17769 Also, if more than one register is used then their names are separated by commas; e.g., @code{"eax, ebx"}
17771 The @code{Clobber} parameter has several additional uses:
17772 @enumerate
17773 @item Use the "register" name @code{cc} to indicate that flags might have changed
17774 @item Use the "register" name @code{memory} if you changed a memory location
17775 @end enumerate
17777 @c ---------------------------------------------------------------------------
17778 @node The Volatile Parameter
17779 @subsection The @code{Volatile} Parameter
17780 @cindex Volatile parameter
17782 @noindent
17783 Compiler optimizations in the presence of Inline Assembler may sometimes have unwanted effects.
17784 For example, when
17785 an @code{Asm} invocation with an input variable is inside a loop, the compiler might move
17786 the loading of the input variable outside the loop, regarding it as a
17787 one-time initialization.
17789 If this effect is not desired, you can disable such optimizations by setting the
17790 @code{Volatile} parameter to @code{True}; for example:
17792 @smallexample
17793 @group
17794 Asm ("movl %0, %%ebx" & LF & HT &
17795      "movl %%ebx, %1",
17796      Inputs   => Unsigned_32'Asm_Input  ("g", Var_In),
17797      Outputs  => Unsigned_32'Asm_Output ("=g", Var_Out),
17798      Clobber  => "ebx",
17799      Volatile => True);
17800 @end group
17801 @end smallexample
17803 By default, @code{Volatile} is set to @code{False} unless there is no @code{Outputs}
17804 parameter.
17806 Although setting @code{Volatile} to @code{True} prevents unwanted optimizations,
17807 it will also disable other optimizations that might be important for efficiency.
17808 In general, you should set @code{Volatile} to @code{True} only if the compiler's
17809 optimizations have created problems.
17811 @c ---------------------------------------------------------------------------
17812 @node A Complete Example
17813 @section A Complete Example
17815 @noindent
17816 This section contains a complete program illustrating a realistic usage of GNAT's Inline Assembler
17817 capabilities.  It comprises a main procedure @code{Check_CPU} and a package @code{Intel_CPU}.
17818 The package declares a collection of functions that detect the properties of the 32-bit
17819 x86 processor that is running the program.  The main procedure invokes these functions
17820 and displays the information.
17822 The Intel_CPU package could be enhanced by adding functions to
17823 detect the type of x386 co-processor, the processor caching options and
17824 special operations such as the SIMD extensions.
17826 Although the Intel_CPU package has been written for 32-bit Intel
17827 compatible CPUs, it is OS neutral. It has been tested on DOS,
17828 Windows/NT and Linux.
17830 @menu
17831 * Check_CPU Procedure::
17832 * Intel_CPU Package Specification::
17833 * Intel_CPU Package Body::
17834 @end menu
17836 @c ---------------------------------------------------------------------------
17837 @node Check_CPU Procedure
17838 @subsection @code{Check_CPU} Procedure
17839 @cindex Check_CPU procedure
17841 @smallexample
17842 ---------------------------------------------------------------------
17843 --                                                                 --
17844 --  Uses the Intel_CPU package to identify the CPU the program is  --
17845 --  running on, and some of the features it supports.              --
17846 --                                                                 --
17847 ---------------------------------------------------------------------
17849 with Intel_CPU;                     --  Intel CPU detection functions
17850 with Ada.Text_IO;                   --  Standard text I/O
17851 with Ada.Command_Line;              --  To set the exit status
17853 procedure Check_CPU is
17855    Type_Found : Boolean := False;
17856    --  Flag to indicate that processor was identified
17858    Features   : Intel_CPU.Processor_Features;
17859    --  The processor features
17861    Signature  : Intel_CPU.Processor_Signature;
17862    --  The processor type signature
17864 begin
17866    -----------------------------------
17867    --  Display the program banner.  --
17868    -----------------------------------
17870    Ada.Text_IO.Put_Line (Ada.Command_Line.Command_Name &
17871                          ": check Intel CPU version and features, v1.0");
17872    Ada.Text_IO.Put_Line ("distribute freely, but no warranty whatsoever");
17873    Ada.Text_IO.New_Line;
17875    -----------------------------------------------------------------------
17876    --  We can safely start with the assumption that we are on at least  --
17877    --  a x386 processor. If the CPUID instruction is present, then we   --
17878    --  have a later processor type.                                     --
17879    -----------------------------------------------------------------------
17881    if Intel_CPU.Has_CPUID = False then
17883       --  No CPUID instruction, so we assume this is indeed a x386
17884       --  processor. We can still check if it has a FP co-processor.
17885       if Intel_CPU.Has_FPU then
17886          Ada.Text_IO.Put_Line
17887            ("x386-type processor with a FP co-processor");
17888       else
17889          Ada.Text_IO.Put_Line
17890            ("x386-type processor without a FP co-processor");
17891       end if;  --  check for FPU
17893       --  Program done
17894       Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Success);
17895       return;
17897    end if;  --  check for CPUID
17899    -----------------------------------------------------------------------
17900    --  If CPUID is supported, check if this is a true Intel processor,  --
17901    --  if it is not, display a warning.                                 --
17902    -----------------------------------------------------------------------
17904    if Intel_CPU.Vendor_ID /= Intel_CPU.Intel_Processor then
17905       Ada.Text_IO.Put_Line ("*** This is a Intel compatible processor");
17906       Ada.Text_IO.Put_Line ("*** Some information may be incorrect");
17907    end if;  --  check if Intel
17909    ----------------------------------------------------------------------
17910    --  With the CPUID instruction present, we can assume at least a    --
17911    --  x486 processor. If the CPUID support level is < 1 then we have  --
17912    --  to leave it at that.                                            --
17913    ----------------------------------------------------------------------
17915    if Intel_CPU.CPUID_Level < 1 then
17917       --  Ok, this is a x486 processor. we still can get the Vendor ID
17918       Ada.Text_IO.Put_Line ("x486-type processor");
17919       Ada.Text_IO.Put_Line ("Vendor ID is " & Intel_CPU.Vendor_ID);
17921       --  We can also check if there is a FPU present
17922       if Intel_CPU.Has_FPU then
17923          Ada.Text_IO.Put_Line ("Floating-Point support");
17924       else
17925          Ada.Text_IO.Put_Line ("No Floating-Point support");
17926       end if;  --  check for FPU
17928       --  Program done
17929       Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Success);
17930       return;
17932    end if;  --  check CPUID level
17934    ---------------------------------------------------------------------
17935    --  With a CPUID level of 1 we can use the processor signature to  --
17936    --  determine it's exact type.                                     --
17937    ---------------------------------------------------------------------
17939    Signature := Intel_CPU.Signature;
17941    ----------------------------------------------------------------------
17942    --  Ok, now we go into a lot of messy comparisons to get the        --
17943    --  processor type. For clarity, no attememt to try to optimize the --
17944    --  comparisons has been made. Note that since Intel_CPU does not   --
17945    --  support getting cache info, we cannot distinguish between P5    --
17946    --  and Celeron types yet.                                          --
17947    ----------------------------------------------------------------------
17949    --  x486SL
17950    if Signature.Processor_Type = 2#00#   and
17951      Signature.Family          = 2#0100# and
17952      Signature.Model           = 2#0100# then
17953       Type_Found := True;
17954       Ada.Text_IO.Put_Line ("x486SL processor");
17955    end if;
17957    --  x486DX2 Write-Back
17958    if Signature.Processor_Type = 2#00#   and
17959      Signature.Family          = 2#0100# and
17960      Signature.Model           = 2#0111# then
17961       Type_Found := True;
17962       Ada.Text_IO.Put_Line ("Write-Back Enhanced x486DX2 processor");
17963    end if;
17965    --  x486DX4
17966    if Signature.Processor_Type = 2#00#   and
17967      Signature.Family          = 2#0100# and
17968      Signature.Model           = 2#1000# then
17969       Type_Found := True;
17970       Ada.Text_IO.Put_Line ("x486DX4 processor");
17971    end if;
17973    --  x486DX4 Overdrive
17974    if Signature.Processor_Type = 2#01#   and
17975      Signature.Family          = 2#0100# and
17976      Signature.Model           = 2#1000# then
17977       Type_Found := True;
17978       Ada.Text_IO.Put_Line ("x486DX4 OverDrive processor");
17979    end if;
17981    --  Pentium (60, 66)
17982    if Signature.Processor_Type = 2#00#   and
17983      Signature.Family          = 2#0101# and
17984      Signature.Model           = 2#0001# then
17985       Type_Found := True;
17986       Ada.Text_IO.Put_Line ("Pentium processor (60, 66)");
17987    end if;
17989    --  Pentium (75, 90, 100, 120, 133, 150, 166, 200)
17990    if Signature.Processor_Type = 2#00#   and
17991      Signature.Family          = 2#0101# and
17992      Signature.Model           = 2#0010# then
17993       Type_Found := True;
17994       Ada.Text_IO.Put_Line
17995         ("Pentium processor (75, 90, 100, 120, 133, 150, 166, 200)");
17996    end if;
17998    --  Pentium OverDrive (60, 66)
17999    if Signature.Processor_Type = 2#01#   and
18000      Signature.Family          = 2#0101# and
18001      Signature.Model           = 2#0001# then
18002       Type_Found := True;
18003       Ada.Text_IO.Put_Line ("Pentium OverDrive processor (60, 66)");
18004    end if;
18006    --  Pentium OverDrive (75, 90, 100, 120, 133, 150, 166, 200)
18007    if Signature.Processor_Type = 2#01#   and
18008      Signature.Family          = 2#0101# and
18009      Signature.Model           = 2#0010# then
18010       Type_Found := True;
18011       Ada.Text_IO.Put_Line
18012         ("Pentium OverDrive cpu (75, 90, 100, 120, 133, 150, 166, 200)");
18013    end if;
18015    --  Pentium OverDrive processor for x486 processor-based systems
18016    if Signature.Processor_Type = 2#01#   and
18017      Signature.Family          = 2#0101# and
18018      Signature.Model           = 2#0011# then
18019       Type_Found := True;
18020       Ada.Text_IO.Put_Line
18021         ("Pentium OverDrive processor for x486 processor-based systems");
18022    end if;
18024    --  Pentium processor with MMX technology (166, 200)
18025    if Signature.Processor_Type = 2#00#   and
18026      Signature.Family          = 2#0101# and
18027      Signature.Model           = 2#0100# then
18028       Type_Found := True;
18029       Ada.Text_IO.Put_Line
18030         ("Pentium processor with MMX technology (166, 200)");
18031    end if;
18033    --  Pentium OverDrive with MMX for Pentium (75, 90, 100, 120, 133)
18034    if Signature.Processor_Type = 2#01#   and
18035      Signature.Family          = 2#0101# and
18036      Signature.Model           = 2#0100# then
18037       Type_Found := True;
18038       Ada.Text_IO.Put_Line
18039         ("Pentium OverDrive processor with MMX " &
18040          "technology for Pentium processor (75, 90, 100, 120, 133)");
18041    end if;
18043    --  Pentium Pro processor
18044    if Signature.Processor_Type = 2#00#   and
18045      Signature.Family          = 2#0110# and
18046      Signature.Model           = 2#0001# then
18047       Type_Found := True;
18048       Ada.Text_IO.Put_Line ("Pentium Pro processor");
18049    end if;
18051    --  Pentium II processor, model 3
18052    if Signature.Processor_Type = 2#00#   and
18053      Signature.Family          = 2#0110# and
18054      Signature.Model           = 2#0011# then
18055       Type_Found := True;
18056       Ada.Text_IO.Put_Line ("Pentium II processor, model 3");
18057    end if;
18059    --  Pentium II processor, model 5 or Celeron processor
18060    if Signature.Processor_Type = 2#00#   and
18061      Signature.Family          = 2#0110# and
18062      Signature.Model           = 2#0101# then
18063       Type_Found := True;
18064       Ada.Text_IO.Put_Line
18065         ("Pentium II processor, model 5 or Celeron processor");
18066    end if;
18068    --  Pentium Pro OverDrive processor
18069    if Signature.Processor_Type = 2#01#   and
18070      Signature.Family          = 2#0110# and
18071      Signature.Model           = 2#0011# then
18072       Type_Found := True;
18073       Ada.Text_IO.Put_Line ("Pentium Pro OverDrive processor");
18074    end if;
18076    --  If no type recognized, we have an unknown. Display what
18077    --  we _do_ know
18078    if Type_Found = False then
18079       Ada.Text_IO.Put_Line ("Unknown processor");
18080    end if;
18082    -----------------------------------------
18083    --  Display processor stepping level.  --
18084    -----------------------------------------
18086    Ada.Text_IO.Put_Line ("Stepping level:" & Signature.Stepping'Img);
18088    ---------------------------------
18089    --  Display vendor ID string.  --
18090    ---------------------------------
18092    Ada.Text_IO.Put_Line ("Vendor ID: " & Intel_CPU.Vendor_ID);
18094    ------------------------------------
18095    --  Get the processors features.  --
18096    ------------------------------------
18098    Features := Intel_CPU.Features;
18100    -----------------------------
18101    --  Check for a FPU unit.  --
18102    -----------------------------
18104    if Features.FPU = True then
18105       Ada.Text_IO.Put_Line ("Floating-Point unit available");
18106    else
18107       Ada.Text_IO.Put_Line ("no Floating-Point unit");
18108    end if;  --  check for FPU
18110    --------------------------------
18111    --  List processor features.  --
18112    --------------------------------
18114    Ada.Text_IO.Put_Line ("Supported features: ");
18116    --  Virtual Mode Extension
18117    if Features.VME = True then
18118       Ada.Text_IO.Put_Line ("    VME    - Virtual Mode Extension");
18119    end if;
18121    --  Debugging Extension
18122    if Features.DE = True then
18123       Ada.Text_IO.Put_Line ("    DE     - Debugging Extension");
18124    end if;
18126    --  Page Size Extension
18127    if Features.PSE = True then
18128       Ada.Text_IO.Put_Line ("    PSE    - Page Size Extension");
18129    end if;
18131    --  Time Stamp Counter
18132    if Features.TSC = True then
18133       Ada.Text_IO.Put_Line ("    TSC    - Time Stamp Counter");
18134    end if;
18136    --  Model Specific Registers
18137    if Features.MSR = True then
18138       Ada.Text_IO.Put_Line ("    MSR    - Model Specific Registers");
18139    end if;
18141    --  Physical Address Extension
18142    if Features.PAE = True then
18143       Ada.Text_IO.Put_Line ("    PAE    - Physical Address Extension");
18144    end if;
18146    --  Machine Check Extension
18147    if Features.MCE = True then
18148       Ada.Text_IO.Put_Line ("    MCE    - Machine Check Extension");
18149    end if;
18151    --  CMPXCHG8 instruction supported
18152    if Features.CX8 = True then
18153       Ada.Text_IO.Put_Line ("    CX8    - CMPXCHG8 instruction");
18154    end if;
18156    --  on-chip APIC hardware support
18157    if Features.APIC = True then
18158       Ada.Text_IO.Put_Line ("    APIC   - on-chip APIC hardware support");
18159    end if;
18161    --  Fast System Call
18162    if Features.SEP = True then
18163       Ada.Text_IO.Put_Line ("    SEP    - Fast System Call");
18164    end if;
18166    --  Memory Type Range Registers
18167    if Features.MTRR = True then
18168       Ada.Text_IO.Put_Line ("    MTTR   - Memory Type Range Registers");
18169    end if;
18171    --  Page Global Enable
18172    if Features.PGE = True then
18173       Ada.Text_IO.Put_Line ("    PGE    - Page Global Enable");
18174    end if;
18176    --  Machine Check Architecture
18177    if Features.MCA = True then
18178       Ada.Text_IO.Put_Line ("    MCA    - Machine Check Architecture");
18179    end if;
18181    --  Conditional Move Instruction Supported
18182    if Features.CMOV = True then
18183       Ada.Text_IO.Put_Line
18184         ("    CMOV   - Conditional Move Instruction Supported");
18185    end if;
18187    --  Page Attribute Table
18188    if Features.PAT = True then
18189       Ada.Text_IO.Put_Line ("    PAT    - Page Attribute Table");
18190    end if;
18192    --  36-bit Page Size Extension
18193    if Features.PSE_36 = True then
18194       Ada.Text_IO.Put_Line ("    PSE_36 - 36-bit Page Size Extension");
18195    end if;
18197    --  MMX technology supported
18198    if Features.MMX = True then
18199       Ada.Text_IO.Put_Line ("    MMX    - MMX technology supported");
18200    end if;
18202    --  Fast FP Save and Restore
18203    if Features.FXSR = True then
18204       Ada.Text_IO.Put_Line ("    FXSR   - Fast FP Save and Restore");
18205    end if;
18207    ---------------------
18208    --  Program done.  --
18209    ---------------------
18211    Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Success);
18213 exception
18215    when others =>
18216       Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
18217       raise;
18219 end Check_CPU;
18220 @end smallexample
18222 @c ---------------------------------------------------------------------------
18223 @node Intel_CPU Package Specification
18224 @subsection @code{Intel_CPU} Package Specification
18225 @cindex Intel_CPU package specification
18227 @smallexample
18228 -------------------------------------------------------------------------
18229 --                                                                     --
18230 --  file: INTEL_CPU.ADS                                                --
18231 --                                                                     --
18232 --           *********************************************             --
18233 --           * WARNING: for 32-bit Intel processors only *             --
18234 --           *********************************************             --
18235 --                                                                     --
18236 --  This package contains a number of subprograms that are useful in   --
18237 --  determining the Intel x86 CPU (and the features it supports) on    --
18238 --  which the program is running.                                      --
18239 --                                                                     --
18240 --  The package is based upon the information given in the Intel       --
18241 --  Application Note AP-485: "Intel Processor Identification and the   --
18242 --  CPUID Instruction" as of April 1998. This application note can be  --
18243 --  found on www.intel.com.                                            --
18244 --                                                                     --
18245 --  It currently deals with 32-bit processors only, will not detect    --
18246 --  features added after april 1998, and does not guarantee proper     --
18247 --  results on Intel-compatible processors.                            --
18248 --                                                                     --
18249 --  Cache info and x386 fpu type detection are not supported.          --
18250 --                                                                     --
18251 --  This package does not use any privileged instructions, so should   --
18252 --  work on any OS running on a 32-bit Intel processor.                --
18253 --                                                                     --
18254 -------------------------------------------------------------------------
18256 with Interfaces;             use Interfaces;
18257 --  for using unsigned types
18259 with System.Machine_Code;    use System.Machine_Code;
18260 --  for using inline assembler code
18262 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
18263 --  for inserting control characters
18265 package Intel_CPU is
18267    ----------------------
18268    --  Processor bits  --
18269    ----------------------
18271    subtype Num_Bits is Natural range 0 .. 31;
18272    --  the number of processor bits (32)
18274    --------------------------
18275    --  Processor register  --
18276    --------------------------
18278    --  define a processor register type for easy access to
18279    --  the individual bits
18281    type Processor_Register is array (Num_Bits) of Boolean;
18282    pragma Pack (Processor_Register);
18283    for Processor_Register'Size use 32;
18285    -------------------------
18286    --  Unsigned register  --
18287    -------------------------
18289    --  define a processor register type for easy access to
18290    --  the individual bytes
18292    type Unsigned_Register is
18293       record
18294          L1 : Unsigned_8;
18295          H1 : Unsigned_8;
18296          L2 : Unsigned_8;
18297          H2 : Unsigned_8;
18298       end record;
18300    for Unsigned_Register use
18301       record
18302          L1 at 0 range  0 ..  7;
18303          H1 at 0 range  8 .. 15;
18304          L2 at 0 range 16 .. 23;
18305          H2 at 0 range 24 .. 31;
18306       end record;
18308    for Unsigned_Register'Size use 32;
18310    ---------------------------------
18311    --  Intel processor vendor ID  --
18312    ---------------------------------
18314    Intel_Processor : constant String (1 .. 12) := "GenuineIntel";
18315    --  indicates an Intel manufactured processor
18317    ------------------------------------
18318    --  Processor signature register  --
18319    ------------------------------------
18321    --  a register type to hold the processor signature
18323    type Processor_Signature is
18324       record
18325          Stepping       : Natural range 0 .. 15;
18326          Model          : Natural range 0 .. 15;
18327          Family         : Natural range 0 .. 15;
18328          Processor_Type : Natural range 0 .. 3;
18329          Reserved       : Natural range 0 .. 262143;
18330       end record;
18332    for Processor_Signature use
18333       record
18334          Stepping       at 0 range  0 ..  3;
18335          Model          at 0 range  4 ..  7;
18336          Family         at 0 range  8 .. 11;
18337          Processor_Type at 0 range 12 .. 13;
18338          Reserved       at 0 range 14 .. 31;
18339       end record;
18341    for Processor_Signature'Size use 32;
18343    -----------------------------------
18344    --  Processor features register  --
18345    -----------------------------------
18347    --  a processor register to hold the processor feature flags
18349    type Processor_Features is
18350       record
18351          FPU    : Boolean;                --  floating point unit on chip
18352          VME    : Boolean;                --  virtual mode extension
18353          DE     : Boolean;                --  debugging extension
18354          PSE    : Boolean;                --  page size extension
18355          TSC    : Boolean;                --  time stamp counter
18356          MSR    : Boolean;                --  model specific registers
18357          PAE    : Boolean;                --  physical address extension
18358          MCE    : Boolean;                --  machine check extension
18359          CX8    : Boolean;                --  cmpxchg8 instruction
18360          APIC   : Boolean;                --  on-chip apic hardware
18361          Res_1  : Boolean;                --  reserved for extensions
18362          SEP    : Boolean;                --  fast system call
18363          MTRR   : Boolean;                --  memory type range registers
18364          PGE    : Boolean;                --  page global enable
18365          MCA    : Boolean;                --  machine check architecture
18366          CMOV   : Boolean;                --  conditional move supported
18367          PAT    : Boolean;                --  page attribute table
18368          PSE_36 : Boolean;                --  36-bit page size extension
18369          Res_2  : Natural range 0 .. 31;  --  reserved for extensions
18370          MMX    : Boolean;                --  MMX technology supported
18371          FXSR   : Boolean;                --  fast FP save and restore
18372          Res_3  : Natural range 0 .. 127; --  reserved for extensions
18373       end record;
18375    for Processor_Features use
18376       record
18377          FPU    at 0 range  0 ..  0;
18378          VME    at 0 range  1 ..  1;
18379          DE     at 0 range  2 ..  2;
18380          PSE    at 0 range  3 ..  3;
18381          TSC    at 0 range  4 ..  4;
18382          MSR    at 0 range  5 ..  5;
18383          PAE    at 0 range  6 ..  6;
18384          MCE    at 0 range  7 ..  7;
18385          CX8    at 0 range  8 ..  8;
18386          APIC   at 0 range  9 ..  9;
18387          Res_1  at 0 range 10 .. 10;
18388          SEP    at 0 range 11 .. 11;
18389          MTRR   at 0 range 12 .. 12;
18390          PGE    at 0 range 13 .. 13;
18391          MCA    at 0 range 14 .. 14;
18392          CMOV   at 0 range 15 .. 15;
18393          PAT    at 0 range 16 .. 16;
18394          PSE_36 at 0 range 17 .. 17;
18395          Res_2  at 0 range 18 .. 22;
18396          MMX    at 0 range 23 .. 23;
18397          FXSR   at 0 range 24 .. 24;
18398          Res_3  at 0 range 25 .. 31;
18399       end record;
18401    for Processor_Features'Size use 32;
18403    -------------------
18404    --  Subprograms  --
18405    -------------------
18407    function Has_FPU return Boolean;
18408    --  return True if a FPU is found
18409    --  use only if CPUID is not supported
18411    function Has_CPUID return Boolean;
18412    --  return True if the processor supports the CPUID instruction
18414    function CPUID_Level return Natural;
18415    --  return the CPUID support level (0, 1 or 2)
18416    --  can only be called if the CPUID instruction is supported
18418    function Vendor_ID return String;
18419    --  return the processor vendor identification string
18420    --  can only be called if the CPUID instruction is supported
18422    function Signature return Processor_Signature;
18423    --  return the processor signature
18424    --  can only be called if the CPUID instruction is supported
18426    function Features return Processor_Features;
18427    --  return the processors features
18428    --  can only be called if the CPUID instruction is supported
18430 private
18432    ------------------------
18433    --  EFLAGS bit names  --
18434    ------------------------
18436    ID_Flag : constant Num_Bits := 21;
18437    --  ID flag bit
18439 end Intel_CPU;
18440 @end smallexample
18442 @c ---------------------------------------------------------------------------
18443 @node Intel_CPU Package Body
18444 @subsection @code{Intel_CPU} Package Body
18445 @cindex Intel_CPU package body
18447 @smallexample
18448 package body Intel_CPU is
18450    ---------------------------
18451    --  Detect FPU presence  --
18452    ---------------------------
18454    --  There is a FPU present if we can set values to the FPU Status
18455    --  and Control Words.
18457    function Has_FPU return Boolean is
18459       Register : Unsigned_16;
18460       --  processor register to store a word
18462    begin
18464       --  check if we can change the status word
18465       Asm (
18467            --  the assembler code
18468            "finit"              & LF & HT &    --  reset status word
18469            "movw $0x5A5A, %%ax" & LF & HT &    --  set value status word
18470            "fnstsw %0"          & LF & HT &    --  save status word
18471            "movw %%ax, %0",                    --  store status word
18473            --  output stored in Register
18474            --  register must be a memory location
18475            Outputs => Unsigned_16'Asm_output ("=m", Register),
18477            --  tell compiler that we used eax
18478            Clobber => "eax");
18480       --  if the status word is zero, there is no FPU
18481       if Register = 0 then
18482          return False;   --  no status word
18483       end if;  --  check status word value
18485       --  check if we can get the control word
18486       Asm (
18488            --  the assembler code
18489            "fnstcw %0",   --  save the control word
18491            --  output into Register
18492            --  register must be a memory location
18493            Outputs => Unsigned_16'Asm_output ("=m", Register));
18495       --  check the relevant bits
18496       if (Register and 16#103F#) /= 16#003F# then
18497          return False;   --  no control word
18498       end if;  --  check control word value
18500       --  FPU found
18501       return True;
18503    end Has_FPU;
18505    --------------------------------
18506    --  Detect CPUID instruction  --
18507    --------------------------------
18509    --  The processor supports the CPUID instruction if it is possible
18510    --  to change the value of ID flag bit in the EFLAGS register.
18512    function Has_CPUID return Boolean is
18514       Original_Flags, Modified_Flags : Processor_Register;
18515       --  EFLAG contents before and after changing the ID flag
18517    begin
18519       --  try flipping the ID flag in the EFLAGS register
18520       Asm (
18522            --  the assembler code
18523            "pushfl"               & LF & HT &     --  push EFLAGS on stack
18524            "pop %%eax"            & LF & HT &     --  pop EFLAGS into eax
18525            "movl %%eax, %0"       & LF & HT &     --  save EFLAGS content
18526            "xor $0x200000, %%eax" & LF & HT &     --  flip ID flag
18527            "push %%eax"           & LF & HT &     --  push EFLAGS on stack
18528            "popfl"                & LF & HT &     --  load EFLAGS register
18529            "pushfl"               & LF & HT &     --  push EFLAGS on stack
18530            "pop %1",                              --  save EFLAGS content
18532            --  output values, may be anything
18533            --  Original_Flags is %0
18534            --  Modified_Flags is %1
18535            Outputs =>
18536               (Processor_Register'Asm_output ("=g", Original_Flags),
18537                Processor_Register'Asm_output ("=g", Modified_Flags)),
18539            --  tell compiler eax is destroyed
18540            Clobber => "eax");
18542       --  check if CPUID is supported
18543       if Original_Flags(ID_Flag) /= Modified_Flags(ID_Flag) then
18544          return True;   --  ID flag was modified
18545       else
18546          return False;  --  ID flag unchanged
18547       end if;  --  check for CPUID
18549    end Has_CPUID;
18551    -------------------------------
18552    --  Get CPUID support level  --
18553    -------------------------------
18555    function CPUID_Level return Natural is
18557       Level : Unsigned_32;
18558       --  returned support level
18560    begin
18562       --  execute CPUID, storing the results in the Level register
18563       Asm (
18565            --  the assembler code
18566            "cpuid",    --  execute CPUID
18568            --  zero is stored in eax
18569            --  returning the support level in eax
18570            Inputs => Unsigned_32'Asm_input ("a", 0),
18572            --  eax is stored in Level
18573            Outputs => Unsigned_32'Asm_output ("=a", Level),
18575            --  tell compiler ebx, ecx and edx registers are destroyed
18576            Clobber => "ebx, ecx, edx");
18578       --  return the support level
18579       return Natural (Level);
18581    end CPUID_Level;
18583    --------------------------------
18584    --  Get CPU Vendor ID String  --
18585    --------------------------------
18587    --  The vendor ID string is returned in the ebx, ecx and edx register
18588    --  after executing the CPUID instruction with eax set to zero.
18589    --  In case of a true Intel processor the string returned is
18590    --  "GenuineIntel"
18592    function Vendor_ID return String is
18594       Ebx, Ecx, Edx : Unsigned_Register;
18595       --  registers containing the vendor ID string
18597       Vendor_ID : String (1 .. 12);
18598       -- the vendor ID string
18600    begin
18602       --  execute CPUID, storing the results in the processor registers
18603       Asm (
18605            --  the assembler code
18606            "cpuid",    --  execute CPUID
18608            --  zero stored in eax
18609            --  vendor ID string returned in ebx, ecx and edx
18610            Inputs => Unsigned_32'Asm_input ("a", 0),
18612            --  ebx is stored in Ebx
18613            --  ecx is stored in Ecx
18614            --  edx is stored in Edx
18615            Outputs => (Unsigned_Register'Asm_output ("=b", Ebx),
18616                        Unsigned_Register'Asm_output ("=c", Ecx),
18617                        Unsigned_Register'Asm_output ("=d", Edx)));
18619       --  now build the vendor ID string
18620       Vendor_ID( 1) := Character'Val (Ebx.L1);
18621       Vendor_ID( 2) := Character'Val (Ebx.H1);
18622       Vendor_ID( 3) := Character'Val (Ebx.L2);
18623       Vendor_ID( 4) := Character'Val (Ebx.H2);
18624       Vendor_ID( 5) := Character'Val (Edx.L1);
18625       Vendor_ID( 6) := Character'Val (Edx.H1);
18626       Vendor_ID( 7) := Character'Val (Edx.L2);
18627       Vendor_ID( 8) := Character'Val (Edx.H2);
18628       Vendor_ID( 9) := Character'Val (Ecx.L1);
18629       Vendor_ID(10) := Character'Val (Ecx.H1);
18630       Vendor_ID(11) := Character'Val (Ecx.L2);
18631       Vendor_ID(12) := Character'Val (Ecx.H2);
18633       --  return string
18634       return Vendor_ID;
18636    end Vendor_ID;
18638    -------------------------------
18639    --  Get processor signature  --
18640    -------------------------------
18642    function Signature return Processor_Signature is
18644       Result : Processor_Signature;
18645       --  processor signature returned
18647    begin
18649       --  execute CPUID, storing the results in the Result variable
18650       Asm (
18652            --  the assembler code
18653            "cpuid",    --  execute CPUID
18655            --  one is stored in eax
18656            --  processor signature returned in eax
18657            Inputs => Unsigned_32'Asm_input ("a", 1),
18659            --  eax is stored in Result
18660            Outputs => Processor_Signature'Asm_output ("=a", Result),
18662            --  tell compiler that ebx, ecx and edx are also destroyed
18663            Clobber => "ebx, ecx, edx");
18665       --  return processor signature
18666       return Result;
18668    end Signature;
18670    ------------------------------
18671    --  Get processor features  --
18672    ------------------------------
18674    function Features return Processor_Features is
18676       Result : Processor_Features;
18677       --  processor features returned
18679    begin
18681       --  execute CPUID, storing the results in the Result variable
18682       Asm (
18684            --  the assembler code
18685            "cpuid",    --  execute CPUID
18687            --  one stored in eax
18688            --  processor features returned in edx
18689            Inputs => Unsigned_32'Asm_input ("a", 1),
18691            --  edx is stored in Result
18692            Outputs => Processor_Features'Asm_output ("=d", Result),
18694            --  tell compiler that ebx and ecx are also destroyed
18695            Clobber => "ebx, ecx");
18697       --  return processor signature
18698       return Result;
18700    end Features;
18702 end Intel_CPU;
18703 @end smallexample
18704 @c END OF INLINE ASSEMBLER CHAPTER
18705 @c ===============================
18710 @node Performance Considerations
18711 @chapter Performance Considerations
18712 @cindex Performance
18714 @noindent
18715 The GNAT system provides a number of options that allow a trade-off
18716 between
18718 @itemize @bullet
18719 @item
18720 performance of the generated code
18722 @item
18723 speed of compilation
18725 @item
18726 minimization of dependences and recompilation
18728 @item
18729 the degree of run-time checking.
18730 @end itemize
18732 @noindent
18733 The defaults (if no options are selected) aim at improving the speed
18734 of compilation and minimizing dependences, at the expense of performance
18735 of the generated code:
18737 @itemize @bullet
18738 @item
18739 no optimization
18741 @item
18742 no inlining of subprogram calls
18744 @item
18745 all run-time checks enabled except overflow and elaboration checks
18746 @end itemize
18748 @noindent
18749 These options are suitable for most program development purposes. This
18750 chapter describes how you can modify these choices, and also provides
18751 some guidelines on debugging optimized code.
18753 @menu
18754 * Controlling Run-Time Checks::
18755 * Optimization Levels::
18756 * Debugging Optimized Code::
18757 * Inlining of Subprograms::
18758 * Coverage Analysis::
18759 @end menu
18761 @node Controlling Run-Time Checks
18762 @section Controlling Run-Time Checks
18764 @noindent
18765 By default, GNAT generates all run-time checks, except arithmetic overflow
18766 checking for integer operations and checks for access before elaboration on
18767 subprogram calls. The latter are not required in default mode, because all
18768 necessary checking is done at compile time.
18769 @cindex @option{/CHECKS=SUPPRESS_ALL} (@code{GNAT COMPILE})
18770 @cindex @option{/CHECKS=OVERFLOW} (@code{GNAT COMPILE})
18771 Two gnat qualifiers, @option{/CHECKS=SUPPRESS_ALL} and @option{/CHECKS=OVERFLOW} allow this default to
18772 be modified. @xref{Run-Time Checks}.
18774 Our experience is that the default is suitable for most development
18775 purposes.
18777 We treat integer overflow specially because these
18778 are quite expensive and in our experience are not as important as other
18779 run-time checks in the development process. Note that division by zero
18780 is not considered an overflow check, and divide by zero checks are
18781 generated where required by default.
18783 Elaboration checks are off by default, and also not needed by default, since
18784 GNAT uses a static elaboration analysis approach that avoids the need for
18785 run-time checking. This manual contains a full chapter discussing the issue
18786 of elaboration checks, and if the default is not satisfactory for your use,
18787 you should read this chapter.
18789 For validity checks, the minimal checks required by the Ada Reference
18790 Manual (for case statements and assignments to array elements) are on
18791 by default. These can be suppressed by use of the @option{-gnatVn} qualifier.
18792 Note that in Ada 83, there were no validity checks, so if the Ada 83 mode
18793 is acceptable (or when comparing GNAT performance with an Ada 83 compiler),
18794 it may be reasonable to routinely use @option{-gnatVn}. Validity checks
18795 are also suppressed entirely if @option{/CHECKS=SUPPRESS_ALL} is used.
18797 @cindex Overflow checks
18798 @cindex Checks, overflow
18799 @findex Suppress
18800 @findex Unsuppress
18801 @cindex pragma Suppress
18802 @cindex pragma Unsuppress
18803 Note that the setting of the qualifiers controls the default setting of
18804 the checks. They may be modified using either @code{pragma Suppress} (to
18805 remove checks) or @code{pragma Unsuppress} (to add back suppressed
18806 checks) in the program source.
18808 @node Optimization Levels
18809 @section Optimization Levels
18810 @cindex @code{/OPTIMIZE} (@code{GNAT COMPILE})
18812 @noindent
18813 The default is optimization off. This results in the fastest compile
18814 times, but GNAT makes absolutely no attempt to optimize, and the
18815 generated programs are considerably larger and slower than when
18816 optimization is enabled. You can use the
18817 @code{/OPTIMIZE}
18818 on the @code{GNAT COMPILE} command line to control the optimization level:
18820 @table @code
18821 @item /OPTIMIZE=NONE
18822 no optimization (the default)
18824 @item /OPTIMIZE=SOME
18825 medium level optimization
18827 @item /OPTIMIZE=ALL
18828 full optimization
18830 @item /OPTIMIZE=INLINING
18831 full optimization, and also attempt automatic inlining of small
18832 subprograms within a unit (@pxref{Inlining of Subprograms}).
18833 @end table
18835 Higher optimization levels perform more global transformations on the
18836 program and apply more expensive analysis algorithms in order to generate
18837 faster and more compact code. The price in compilation time, and the
18838 resulting improvement in execution time,
18839 both depend on the particular application and the hardware environment.
18840 You should experiment to find the best level for your application.
18842 Note: Unlike some other compilation systems, @code{GNAT COMPILE} has
18843 been tested extensively at all optimization levels. There are some bugs
18844 which appear only with optimization turned on, but there have also been
18845 bugs which show up only in @emph{unoptimized} code. Selecting a lower
18846 level of optimization does not improve the reliability of the code
18847 generator, which in practice is highly reliable at all optimization
18848 levels.
18850 Note regarding the use of @code{/OPTIMIZE=INLINING}: The use of this optimization level
18851 is generally discouraged with GNAT, since it often results in larger
18852 executables which run more slowly. See further discussion of this point
18853 in @pxref{Inlining of Subprograms}.
18855 @node Debugging Optimized Code
18856 @section Debugging Optimized Code
18858 @noindent
18859 Since the compiler generates debugging tables for a compilation unit before
18860 it performs optimizations, the optimizing transformations may invalidate some
18861 of the debugging data.  You therefore need to anticipate certain
18862 anomalous situations that may arise while debugging optimized code.  This
18863 section describes the most common cases.
18865 @enumerate
18866 @item
18867 @i{The "hopping Program Counter":}  Repeated 'step' or 'next' commands show the PC
18868 bouncing back and forth in the code.  This may result from any of the following
18869 optimizations:
18871 @itemize @bullet
18872 @item
18873 @i{Common subexpression elimination:} using a single instance of code for a
18874 quantity that the source computes several times.  As a result you
18875 may not be able to stop on what looks like a statement.
18877 @item
18878 @i{Invariant code motion:} moving an expression that does not change within a
18879 loop, to the beginning of the loop.
18881 @item
18882 @i{Instruction scheduling:} moving instructions so as to
18883 overlap loads and stores (typically) with other code, or in
18884 general to move computations of values closer to their uses. Often
18885 this causes you to pass an assignment statement without the assignment
18886 happening and then later bounce back to the statement when the
18887 value is actually needed.  Placing a breakpoint on a line of code
18888 and then stepping over it may, therefore, not always cause all the
18889 expected side-effects.
18890 @end itemize
18892 @item
18893 @i{The "big leap":} More commonly known as @i{cross-jumping}, in which two
18894 identical pieces of code are merged and the program counter suddenly
18895 jumps to a statement that is not supposed to be executed, simply because
18896 it (and the code following) translates to the same thing as the code
18897 that @emph{was} supposed to be executed.  This effect is typically seen in
18898 sequences that end in a jump, such as a @code{goto}, a @code{return}, or
18899 a @code{break} in a C @code{qualifier} statement.
18901 @item
18902 @i{The "roving variable":} The symptom is an unexpected value in a variable.
18903 There are various reasons for this effect:
18905 @itemize @bullet
18906 @item
18907 In a subprogram prologue, a parameter may not yet have been moved to its
18908 "home".
18910 @item
18911 A variable may be dead, and its register re-used.  This is
18912 probably the most common cause.
18914 @item
18915 As mentioned above, the assignment of a value to a variable may
18916 have been moved.
18918 @item
18919 A variable may be eliminated entirely by value propagation or
18920 other means.  In this case, GCC may incorrectly generate debugging
18921 information for the variable
18922 @end itemize
18924 @noindent
18925 In general, when an unexpected value appears for a local variable or parameter
18926 you should first ascertain if that value was actually computed by
18927 your program, as opposed to being incorrectly reported by the debugger.
18928 Record fields or
18929 array elements in an object designated by an access value
18930 are generally less of a problem, once you have ascertained that the access value
18931 is sensible.
18932 Typically, this means checking variables in the preceding code and in the
18933 calling subprogram to verify that the value observed is explainable from other
18934 values (one must apply the procedure recursively to those
18935 other values); or re-running the code and stopping a little earlier
18936 (perhaps before the call) and stepping to better see how the variable obtained
18937 the value in question; or continuing to step @emph{from} the point of the
18938 strange value to see if code motion had simply moved the variable's
18939 assignments later.
18940 @end enumerate
18942 @node Inlining of Subprograms
18943 @section Inlining of Subprograms
18945 @noindent
18946 A call to a subprogram in the current unit is inlined if all the
18947 following conditions are met:
18949 @itemize @bullet
18950 @item
18951 The optimization level is at least @code{/OPTIMIZE=SOME}.
18953 @item
18954 The called subprogram is suitable for inlining: It must be small enough
18955 and not contain nested subprograms or anything else that @code{GNAT COMPILE}
18956 cannot support in inlined subprograms.
18958 @item
18959 The call occurs after the definition of the body of the subprogram.
18961 @item
18962 @cindex pragma Inline
18963 @findex Inline
18964 Either @code{pragma Inline} applies to the subprogram or it is
18965 small and automatic inlining (optimization level @code{/OPTIMIZE=INLINING}) is
18966 specified.
18967 @end itemize
18969 @noindent
18970 Calls to subprograms in @code{with}'ed units are normally not inlined.
18971 To achieve this level of inlining, the following conditions must all be
18972 true:
18974 @itemize @bullet
18975 @item
18976 The optimization level is at least @code{/OPTIMIZE=SOME}.
18978 @item
18979 The called subprogram is suitable for inlining: It must be small enough
18980 and not contain nested subprograms or anything else @code{GNAT COMPILE} cannot
18981 support in inlined subprograms.
18983 @item
18984 The call appears in a body (not in a package spec).
18986 @item
18987 There is a @code{pragma Inline} for the subprogram.
18989 @item
18990 @cindex @option{/INLINE=PRAGMA} (@code{GNAT COMPILE})
18991 The @code{/INLINE} qualifier
18992 is used in the @code{GNAT COMPILE} command line
18993 @end itemize
18995 Note that specifying the @option{/INLINE=PRAGMA} qualifier causes additional
18996 compilation dependencies. Consider the following:
18998 @smallexample
18999 @group
19000 @cartouche
19001 @b{package} R @b{is}
19002    @b{procedure} Q;
19003    @b{pragma} Inline (Q);
19004 @b{end} R;
19005 @b{package body} R @b{is}
19006    ...
19007 @b{end} R;
19009 @b{with} R;
19010 @b{procedure} Main @b{is}
19011 @b{begin}
19012    ...
19013    R.Q;
19014 @b{end} Main;
19015 @end cartouche
19016 @end group
19017 @end smallexample
19019 @noindent
19020 With the default behavior (no @option{/INLINE=PRAGMA} qualifier specified), the
19021 compilation of the @code{Main} procedure depends only on its own source,
19022 @file{MAIN.ADB}, and the spec of the package in file @file{R.ADS}. This
19023 means that editing the body of @code{R} does not require recompiling
19024 @code{Main}.
19026 On the other hand, the call @code{R.Q} is not inlined under these
19027 circumstances. If the @option{/INLINE=PRAGMA} qualifier is present when @code{Main}
19028 is compiled, the call will be inlined if the body of @code{Q} is small
19029 enough, but now @code{Main} depends on the body of @code{R} in
19030 @file{R.ADB} as well as on the spec. This means that if this body is edited,
19031 the main program must be recompiled. Note that this extra dependency
19032 occurs whether or not the call is in fact inlined by @code{GNAT COMPILE}.
19034 The use of front end inlining with @option{-gnatN} generates similar
19035 additional dependencies.
19037 @cindex @code{/INLINE=SUPPRESS} (@code{GNAT COMPILE})
19038 Note: The @code{/INLINE=SUPPRESS} qualifier
19039 can be used to prevent
19040 all inlining. This qualifier overrides all other conditions and ensures
19041 that no inlining occurs. The extra dependences resulting from
19042 @option{/INLINE=PRAGMA} will still be active, even if
19043 this qualifier is used to suppress the resulting inlining actions.
19045 Note regarding the use of @code{/OPTIMIZE=INLINING}: There is no difference in inlining
19046 behavior between @code{/OPTIMIZE=ALL} and @code{/OPTIMIZE=INLINING} for subprograms with an explicit
19047 pragma @code{Inline} assuming the use of @option{/INLINE=PRAGMA}
19048 or @option{-gnatN} (the qualifiers that activate inlining). If you have used
19049 pragma @code{Inline} in appropriate cases, then it is usually much better
19050 to use @code{/OPTIMIZE=ALL} and @option{/INLINE=PRAGMA} and avoid the use of @code{/OPTIMIZE=INLINING} which
19051 in this case only has the effect of inlining subprograms you did not
19052 think should be inlined. We often find that the use of @code{/OPTIMIZE=INLINING} slows
19053 down code by performing excessive inlining, leading to increased instruction
19054 cache pressure from the increased code size. So the bottom line here is
19055 that you should not automatically assume that @code{/OPTIMIZE=INLINING} is better than
19056 @code{/OPTIMIZE=ALL}, and indeed you should use @code{/OPTIMIZE=INLINING} only if tests show that
19057 it actually improves performance.
19059 @node Coverage Analysis
19060 @section Coverage Analysis
19062 @noindent
19063 GNAT supports the Digital Performance Coverage Analyzer (PCA), which allows
19064 the user to determine the distribution of execution time across a program,
19065 @pxref{Profiling} for details of usage.
19067 @include fdl.texi
19068 @c GNU Free Documentation License
19070 @node Index,,GNU Free Documentation License, Top
19071 @unnumbered Index
19073 @printindex cp
19075 @contents
19077 @bye