2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / gnat_ug_vms.texi
blob89bc0e17b49c3d3cbbb79ac688f36cfad0f2f957
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 oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
25 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
27 @c                           GNAT_UG Style Guide
29 @c  1. Always put a @noindent on the line before the first paragraph
30 @c     after any of these commands:
32 @c          @chapter
33 @c          @section
34 @c          @subsection
35 @c          @subsubsection
36 @c          @subsubsubsection
38 @c          @end smallexample
39 @c          @end itemize
40 @c          @end enumerate
42 @c  2. DO NOT use @example. Use @smallexample instead.
44 @c  3. Each @chapter, @section, @subsection, @subsubsection, etc.
45 @c     command must be preceded by two empty lines
47 @c  4. The @item command must be on a line of its own if it is in an
48 @c     @itemize or @enumerate command.
50 @c  5. When talking about ALI files use "ALI" (all uppercase), not "Ali"
51 @c     or "ali".
53 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
55 @setfilename gnat_ug_vms.info
56 @settitle GNAT User's Guide for OpenVMS Alpha
61 @include gcc-common.texi
63 @setchapternewpage odd
64 @syncodeindex fn cp
65 @c %**end of header
67 @copying
68 Copyright @copyright{} 1995-2003, Free Software Foundation
70 Permission is granted to copy, distribute and/or modify this document
71 under the terms of the GNU Free Documentation License, Version 1.2
72 or any later version published by the Free Software Foundation;
73 with the Invariant Sections being ``GNU Free Documentation License'', with the
74 Front-Cover Texts being
75 ``GNAT User's Guide for OpenVMS Alpha'',
76 and with no Back-Cover Texts.
77 A copy of the license is included in the section entitled ``GNU
78 Free Documentation License''.
79 @end copying
81 @titlepage
83 @title GNAT User's Guide
84 @center @titlefont{for OpenVMS Alpha}
89 @subtitle GNAT, The GNU Ada 95 Compiler
90 @subtitle GNAT Version for GCC @value{version-GCC}
92 @author Ada Core Technologies, Inc.
94 @page
95 @vskip 0pt plus 1filll
97 @insertcopying
99 @end titlepage
101 @ifnottex
102 @node Top, About This Guide, (dir), (dir)
103 @top GNAT User's Guide
105 GNAT User's Guide for OpenVMS Alpha
110 GNAT, The GNU Ada 95 Compiler
112 GNAT Version for GCC @value{version-GCC}
114 Ada Core Technologies, Inc.
116 @insertcopying
118 @menu
119 * About This Guide::
120 * Getting Started with GNAT::
121 * The GNAT Compilation Model::
122 * Compiling Using GNAT COMPILE::
123 * Binding Using GNAT BIND::
124 * Linking Using GNAT LINK::
125 * The GNAT Make Program GNAT MAKE::
126 * Renaming Files Using GNAT CHOP::
127 * Configuration Pragmas::
128 * Handling Arbitrary File Naming Conventions Using gnatname::
129 * GNAT Project Manager::
130 * Elaboration Order Handling in GNAT::
131 * The Cross-Referencing Tools GNAT XREF and GNAT FIND::
132 * File Name Krunching Using GNAT KRUNCH::
133 * Preprocessing Using GNAT PREPROCESS::
134 * The GNAT Run-Time Library Builder GNAT LIBRARY::
135 * The GNAT Library Browser GNAT LIST::
136 * Finding Memory Problems with GNAT Debug Pool::
137 * Creating Sample Bodies Using GNAT STUB::
138 * Reducing the Size of Ada Executables with GNAT ELIM::
139 * Other Utility Programs::
140 * Compatibility with DEC Ada::
141 * Running and Debugging Ada Programs::
142 * Inline Assembler::
143 * Performance Considerations::
144 * GNU Free Documentation License::
145 * Index::
147  --- The Detailed Node Listing ---
149 About This Guide
151 * What This Guide Contains::
152 * What You Should Know before Reading This Guide::
153 * Related Information::
154 * Conventions::
157 Getting Started with GNAT
159 * Running GNAT::
160 * Running a Simple Ada Program::
161 * Running a Program with Multiple Units::
162 * Using the GNAT MAKE Utility::
163 * Editing with EMACS::
165 The GNAT Compilation Model
167 * Source Representation::
168 * Foreign Language Representation::
169 * File Naming Rules::
170 * Using Other File Names::
171 * Alternative File Naming Schemes::
172 * Generating Object Files::
173 * Source Dependencies::
174 * The Ada Library Information Files::
175 * Binding an Ada Program::
176 * Mixed Language Programming::
177 * Building Mixed Ada & C++ Programs::
178 * Comparison between GNAT and C/C++ Compilation Models::
179 * Comparison between GNAT and Conventional Ada Library Models::
181 Foreign Language Representation
183 * Latin-1::
184 * Other 8-Bit Codes::
185 * Wide Character Encodings::
187 Compiling Ada Programs With GNAT COMPILE
189 * Compiling Programs::
190 * Qualifiers for GNAT COMPILE::
191 * Search Paths and the Run-Time Library (RTL)::
192 * Order of Compilation Issues::
193 * Examples::
195 Qualifiers for GNAT COMPILE
197 * Output and Error Message Control::
198 * Debugging and Assertion Control::
199 * Run-Time Checks::
200 * Stack Overflow Checking::
201 * Run-Time Control::
202 * Validity Checking::
203 * Style Checking::
204 * Using GNAT COMPILE for Syntax Checking::
205 * Using GNAT COMPILE for Semantic Checking::
206 * Compiling Ada 83 Programs::
207 * Character Set Control::
208 * File Naming Control::
209 * Subprogram Inlining Control::
210 * Auxiliary Output Control::
211 * Debugging Control::
212 * Units to Sources Mapping Files::
214 Binding Ada Programs With GNAT BIND
216 * Running GNAT BIND::
217 * Generating the Binder Program in C::
218 * Consistency-Checking Modes::
219 * Binder Error Message Control::
220 * Elaboration Control::
221 * Output Control::
222 * Binding with Non-Ada Main Programs::
223 * Binding Programs with No Main Subprogram::
224 * Summary of Binder Qualifiers::
225 * Command-Line Access::
226 * Search Paths for GNAT BIND::
227 * Examples of GNAT BIND Usage::
229 Linking Using GNAT LINK
231 * Running GNAT LINK::
232 * Qualifiers for GNAT LINK::
233 * Setting Stack Size from GNAT LINK::
234 * Setting Heap Size from GNAT LINK::
236 The GNAT Make Program GNAT MAKE
238 * Running GNAT MAKE::
239 * Qualifiers for GNAT MAKE::
240 * Mode Qualifiers for GNAT MAKE::
241 * Notes on the Command Line::
242 * How GNAT MAKE Works::
243 * Examples of GNAT MAKE Usage::
245 Renaming Files Using GNAT CHOP
247 * Handling Files with Multiple Units::
248 * Operating GNAT CHOP in Compilation Mode::
249 * Command Line for GNAT CHOP::
250 * Qualifiers for GNAT CHOP::
251 * Examples of GNAT CHOP Usage::
253 Configuration Pragmas
255 * Handling of Configuration Pragmas::
256 * The Configuration Pragmas Files::
258 Handling Arbitrary File Naming Conventions Using gnatname
260 * Arbitrary File Naming Conventions::
261 * Running gnatname::
262 * Qualifiers for gnatname::
263 * Examples of gnatname Usage::
265 GNAT Project Manager
267 * Introduction::
268 * Examples of Project Files::
269 * Project File Syntax::
270 * Objects and Sources in Project Files::
271 * Importing Projects::
272 * Project Extension::
273 * External References in Project Files::
274 * Packages in Project Files::
275 * Variables from Imported Projects::
276 * Naming Schemes::
277 * Library Projects::
278 * Qualifiers Related to Project Files::
279 * Tools Supporting Project Files::
280 * An Extended Example::
281 * Project File Complete Syntax::
283 Elaboration Order Handling in GNAT
285 * Elaboration Code in Ada 95::
286 * Checking the Elaboration Order in Ada 95::
287 * Controlling the Elaboration Order in Ada 95::
288 * Controlling Elaboration in GNAT - Internal Calls::
289 * Controlling Elaboration in GNAT - External Calls::
290 * Default Behavior in GNAT - Ensuring Safety::
291 * Elaboration Issues for Library Tasks::
292 * Mixing Elaboration Models::
293 * What to Do If the Default Elaboration Behavior Fails::
294 * Elaboration for Access-to-Subprogram Values::
295 * Summary of Procedures for Elaboration Control::
296 * Other Elaboration Order Considerations::
298 The Cross-Referencing Tools GNAT XREF and GNAT FIND
300 * GNAT XREF Qualifiers::
301 * GNAT FIND Qualifiers::
302 * Project Files for GNAT XREF and GNAT FIND::
303 * Regular Expressions in GNAT FIND and GNAT XREF::
304 * Examples of GNAT XREF Usage::
305 * Examples of GNAT FIND Usage::
307 File Name Krunching Using GNAT KRUNCH
309 * About GNAT KRUNCH::
310 * Using GNAT KRUNCH::
311 * Krunching Method::
312 * Examples of GNAT KRUNCH Usage::
314 Preprocessing Using GNAT PREPROCESS
316 * Using GNAT PREPROCESS::
317 * Qualifiers for GNAT PREPROCESS::
318 * Form of Definitions File::
319 * Form of Input Text for GNAT PREPROCESS::
321 The GNAT Run-Time Library Builder GNAT LIBRARY
323 * Running GNAT LIBRARY::
324 * Qualifiers for GNAT LIBRARY::
325 * Examples of GNAT LIBRARY Usage::
327 The GNAT Library Browser GNAT LIST
329 * Running GNAT LIST::
330 * Qualifiers for GNAT LIST::
331 * Examples of GNAT LIST Usage::
334 Finding Memory Problems with GNAT Debug Pool
336 Creating Sample Bodies Using GNAT STUB
338 * Running GNAT STUB::
339 * Qualifiers for GNAT STUB::
341 Reducing the Size of Ada Executables with GNAT ELIM
343 * About GNAT ELIM::
344 * Eliminate Pragma::
345 * Tree Files::
346 * Preparing Tree and Bind Files for GNAT ELIM::
347 * Running GNAT ELIM::
348 * Correcting the List of Eliminate Pragmas::
349 * Making Your Executables Smaller::
350 * Summary of the GNAT ELIM Usage Cycle::
352 Other Utility Programs
354 * Using Other Utility Programs with GNAT::
355 * The GNAT STANDARD Utility Program::
356 * The External Symbol Naming Scheme of GNAT::
357 * Ada Mode for Glide::
358 * Converting Ada Files to html with gnathtml::
359 * LSE::
361 Compatibility with DEC Ada
363 * Ada 95 Compatibility::
364 * Differences in the Definition of Package System::
365 * Language-Related Features::
366 * The Package STANDARD::
367 * The Package SYSTEM::
368 * Tasking and Task-Related Features::
369 * Implementation of Tasks in DEC Ada for OpenVMS Alpha Systems::
370 * Pragmas and Pragma-Related Features::
371 * Library of Predefined Units::
372 * Bindings::
373 * Main Program Definition::
374 * Implementation-Defined Attributes::
375 * Compiler and Run-Time Interfacing::
376 * Program Compilation and Library Management::
377 * Input-Output::
378 * Implementation Limits::
379 * Tools::
381 Language-Related Features
383 * Integer Types and Representations::
384 * Floating-Point Types and Representations::
385 * Pragmas Float_Representation and Long_Float::
386 * Fixed-Point Types and Representations::
387 * Record and Array Component Alignment::
388 * Address Clauses::
389 * Other Representation Clauses::
391 Implementation of Tasks in DEC Ada for OpenVMS Alpha Systems
393 * Assigning Task IDs::
394 * Task IDs and Delays::
395 * Task-Related Pragmas::
396 * Scheduling and Task Priority::
397 * The Task Stack::
398 * External Interrupts::
400 Pragmas and Pragma-Related Features
402 * Restrictions on the Pragma INLINE::
403 * Restrictions on the Pragma INTERFACE::
404 * Restrictions on the Pragma SYSTEM_NAME::
406 Library of Predefined Units
408 * Changes to DECLIB::
410 Bindings
412 * Shared Libraries and Options Files::
413 * Interfaces to C::
415 Running and Debugging Ada Programs
417 * The GNAT Debugger GDB::
418 * Running GDB::
419 * Introduction to GDB Commands::
420 * Using Ada Expressions::
421 * Calling User-Defined Subprograms::
422 * Using the Next Command in a Function::
423 * Ada Exceptions::
424 * Ada Tasks::
425 * Debugging Generic Units::
426 * GNAT Abnormal Termination or Failure to Terminate::
427 * Naming Conventions for GNAT Source Files::
428 * Getting Internal Debugging Information::
429 * Stack Traceback::
431 Inline Assembler
433 * Basic Assembler Syntax::
434 * A Simple Example of Inline Assembler::
435 * Output Variables in Inline Assembler::
436 * Input Variables in Inline Assembler::
437 * Inlining Inline Assembler Code::
438 * Other Asm Functionality::
439 * A Complete Example::
443 Performance Considerations
445 * Controlling Run-Time Checks::
446 * Optimization Levels::
447 * Debugging Optimized Code::
448 * Inlining of Subprograms::
449 * Coverage Analysis::
451 * Index::
452 @end menu
453 @end ifnottex
455 @node About This Guide
456 @unnumbered About This Guide
458 @noindent
459 This guide describes the use of of GNAT, a full language compiler for the Ada
460 95 programming language, implemented on DIGITAL OpenVMS Alpha Systems.
461 It describes the features of the compiler and tools, and details
462 how to use them to build Ada 95 applications.
464 @menu
465 * What This Guide Contains::
466 * What You Should Know before Reading This Guide::
467 * Related Information::
468 * Conventions::
469 @end menu
471 @node What This Guide Contains
472 @unnumberedsec What This Guide Contains
474 @noindent
475 This guide contains the following chapters:
476 @itemize @bullet
477 @item
478 @ref{Getting Started with GNAT}, describes how to get started compiling
479 and running Ada programs with the GNAT Ada programming environment.
480 @item
481 @ref{The GNAT Compilation Model}, describes the compilation model used
482 by GNAT.
483 @item
484 @ref{Compiling Using GNAT COMPILE}, describes how to compile
485 Ada programs with @code{GNAT COMPILE}, the Ada compiler.
486 @item
487 @ref{Binding Using GNAT BIND}, describes how to
488 perform binding of Ada programs with @code{GNAT BIND}, the GNAT binding
489 utility.
490 @item
491 @ref{Linking Using GNAT LINK},
492 describes @code{GNAT LINK}, a
493 program that provides for linking using the GNAT run-time library to
494 construct a program. @code{GNAT LINK} can also incorporate foreign language
495 object units into the executable.
496 @item
497 @ref{The GNAT Make Program GNAT MAKE}, describes @code{GNAT MAKE}, a
498 utility that automatically determines the set of sources
499 needed by an Ada compilation unit, and executes the necessary compilations
500 binding and link.
501 @item
502 @ref{Renaming Files Using GNAT CHOP}, describes
503 @code{GNAT CHOP}, a utility that allows you to preprocess a file that
504 contains Ada source code, and split it into one or more new files, one
505 for each compilation unit.
506 @item
507 @ref{Configuration Pragmas}, describes the configuration pragmas handled by GNAT.
508 @item
509 @ref{Handling Arbitrary File Naming Conventions Using gnatname}, shows how to override
510 the default GNAT file naming conventions, either for an individual unit or globally.
511 @item
512 @ref{GNAT Project Manager}, describes how to use project files to organize large projects.
513 @item
514 @ref{Elaboration Order Handling in GNAT}, describes how GNAT helps you deal with
515 elaboration order issues.
516 @item
517 @ref{The Cross-Referencing Tools GNAT XREF and GNAT FIND}, discusses
518 @code{GNAT XREF} and @code{GNAT FIND}, two tools that provide an easy
519 way to navigate through sources.
520 @item
521 @ref{File Name Krunching Using GNAT KRUNCH}, describes the @code{GNAT KRUNCH}
522 file name krunching utility, used to handle shortened
523 file names on operating systems with a limit on the length of names.
524 @item
525 @ref{Preprocessing Using GNAT PREPROCESS}, describes @code{GNAT PREPROCESS}, a
526 preprocessor utility that allows a single source file to be used to
527 generate multiple or parameterized source files, by means of macro
528 substitution.
529 @item
530 @ref{The GNAT Library Browser GNAT LIST}, describes @code{GNAT LIST}, a
531 utility that displays information about compiled units, including dependences
532 on the corresponding sources files, and consistency of compilations.
533 @item
534 @ref{Finding Memory Problems with GNAT Debug Pool}, describes how to
535 use the GNAT-specific Debug Pool in order to detect as early as possible
536 the use of incorrect memory references.
538 @item
539 @ref{Creating Sample Bodies Using GNAT STUB}, discusses @code{GNAT STUB},
540 a utility that generates empty but compilable bodies for library units.
542 @item
543 @ref{Reducing the Size of Ada Executables with GNAT ELIM}, describes
544 @code{GNAT ELIM}, a tool which detects unused subprograms and helps
545 the compiler to create a smaller executable for the program.
547 @item
548 @ref{Other Utility Programs}, discusses several other GNAT utilities,
549 including @code{GNAT STANDARD}.
551 @item
552 @ref{Running and Debugging Ada Programs}, describes how to run and debug
553 Ada programs.
555 @item
556 @ref{Inline Assembler}, shows how to use the inline assembly facility in an Ada program.
559 @item
560 @ref{Performance Considerations}, reviews the trade offs between using
561 defaults or options in program development.
562 @item
563 @ref{Compatibility with DEC Ada}, details the compatibility of GNAT with
564 DEC Ada 83 for OpenVMS Alpha.
565 @end itemize
567 @node What You Should Know before Reading This Guide
568 @unnumberedsec What You Should Know before Reading This Guide
570 @cindex Ada 95 Language Reference Manual
571 @noindent
572 This user's guide assumes that you are familiar with Ada 95 language, as
573 described in the International Standard ANSI/ISO/IEC-8652:1995, Jan
574 1995.
576 @node Related Information
577 @unnumberedsec Related Information
579 @noindent
580 For further information about related tools, refer to the following
581 documents:
583 @itemize @bullet
584 @item
585 @cite{GNAT Reference Manual}, which contains all reference
586 material for the GNAT implementation of Ada 95.
588 @item
589 @cite{Ada 95 Language Reference Manual}, which contains all reference
590 material for the Ada 95 programming language.
592 @item
593 @cite{Debugging with GDB}
594 , located in the GNU:[DOCS] directory,
595 contains all details on the use of the GNU source-level debugger.
597 @item
598 @cite{GNU EMACS Manual}
599 , located in the GNU:[DOCS] directory if the EMACS kit is installed,
600 contains full information on the extensible editor and programming
601 environment EMACS.
603 @end itemize
605 @node Conventions
606 @unnumberedsec Conventions
607 @cindex Conventions
608 @cindex Typographical conventions
610 @noindent
611 Following are examples of the typographical and graphic conventions used
612 in this guide:
614 @itemize @bullet
615 @item
616 @code{Functions}, @code{utility program names}, @code{standard names},
617 and @code{classes}.
619 @item
620 @samp{Option flags}
622 @item
623 @file{File Names}, @file{button names}, and @file{field names}.
625 @item
626 @var{Variables}.
628 @item
629 @emph{Emphasis}.
631 @item
632 [optional information or parameters]
634 @item
635 Examples are described by text
636 @smallexample
637 and then shown this way.
638 @end smallexample
639 @end itemize
641 @noindent
642 Commands that are entered by the user are preceded in this manual by the
643 characters @w{"@code{$ }"} (dollar sign followed by space). If your system
644 uses this sequence as a prompt, then the commands will appear exactly as
645 you see them in the manual. If your system uses some other prompt, then
646 the command will appear with the @code{$} replaced by whatever prompt
647 character you are using.
650 @node Getting Started with GNAT
651 @chapter Getting Started with GNAT
653 @noindent
654 This chapter describes some simple ways of using GNAT to build
655 executable Ada programs.
657 @menu
658 * Running GNAT::
659 * Running a Simple Ada Program::
661 * Running a Program with Multiple Units::
663 * Using the GNAT MAKE Utility::
664 * Editing with EMACS::
665 @end menu
667 @node Running GNAT
668 @section Running GNAT
670 @noindent
671 Three steps are needed to create an executable file from an Ada source
672 file:
674 @enumerate
675 @item
676 The source file(s) must be compiled.
677 @item
678 The file(s) must be bound using the GNAT binder.
679 @item
680 All appropriate object files must be linked to produce an executable.
681 @end enumerate
683 @noindent
684 All three steps are most commonly handled by using the @code{GNAT MAKE}
685 utility program that, given the name of the main program, automatically
686 performs the necessary compilation, binding and linking steps.
688 @node Running a Simple Ada Program
689 @section Running a Simple Ada Program
691 @noindent
692 Any text editor may be used to prepare an Ada program. If @code{Glide} is
693 used, the optional Ada mode may be helpful in laying out the program. The
694 program text is a normal text file. We will suppose in our initial
695 example that you have used your editor to prepare the following
696 standard format text file:
698 @smallexample
699 @group
700 @cartouche
701 @b{with} Ada.Text_IO; @b{use} Ada.Text_IO;
702 @b{procedure} Hello @b{is}
703 @b{begin}
704    Put_Line ("Hello WORLD!");
705 @b{end} Hello;
706 @end cartouche
707 @end group
708 @end smallexample
710 @noindent
711 This file should be named @file{HELLO.ADB}.
712 With the normal default file naming conventions, GNAT requires
713 that each file
714 contain a single compilation unit whose file name is the
715 unit name,
716 with periods replaced by hyphens; the
717 extension is @file{ads} for a
718 spec and @file{adb} for a body.
719 You can override this default file naming convention by use of the
720 special pragma @code{Source_File_Name} (@pxref{Using Other File Names}).
721 Alternatively, if you want to rename your files according to this default
722 convention, which is probably more convenient if you will be using GNAT
723 for all your compilations, then the @code{GNAT CHOP} utility
724 can be used to generate correctly-named source files
725 (@pxref{Renaming Files Using GNAT CHOP}).
727 You can compile the program using the following command (@code{$} is used
728 as the command prompt in the examples in this document):
730 @smallexample
731 $ GNAT COMPILE HELLO.ADB
732 @end smallexample
735 @noindent
736 @code{GNAT COMPILE} is the command used to run the compiler. This compiler is
737 capable of compiling programs in several languages, including Ada 95 and
738 C. It assumes that you have given it an Ada program if the file extension is
739 either @file{.ADS} or @file{.ADB}, and it will then call the GNAT compiler to compile
740 the specified file.
743 This compile command generates a file
744 @file{HELLO.OBJ}, which is the object
745 file corresponding to your Ada program. It also generates an "Ada Library Information" file
746 @file{HELLO.ALI},
747 which contains additional information used to check
748 that an Ada program is consistent.
749 To build an executable file,
750 use @code{GNAT BIND} to bind the program
751 and @code{GNAT LINK} to link it. The
752 argument to both @code{GNAT BIND} and @code{GNAT LINK} is the name of the
753 @file{ali} file, but the default extension of @file{.ALI} can
754 be omitted. This means that in the most common case, the argument
755 is simply the name of the main program:
757 @smallexample
758 $ GNAT BIND hello
759 $ GNAT LINK hello
760 @end smallexample
763 @noindent
764 A simpler method of carrying out these steps is to use
765 @command{GNAT MAKE},
766 a master program that invokes all the required
767 compilation, binding and linking tools in the correct order. In particular,
768 @command{GNAT MAKE} automatically recompiles any sources that have been modified
769 since they were last compiled, or sources that depend
770 on such modified sources, so that "version skew" is avoided.
771 @cindex Version skew (avoided by @command{GNAT MAKE})
773 @smallexample
774 $ GNAT MAKE HELLO.ADB
775 @end smallexample
778 @noindent
779 The result is an executable program called @file{hello}, which can be
780 run by entering:
782 @c The following should be removed (BMB 2001-01-23)
783 @c @smallexample
784 @c $ $ RUN HELLO
785 @c @end smallexample
787 @smallexample
788 $ hello
789 @end smallexample
791 @noindent
792 assuming that the current directory is on the search path for executable programs.
794 @noindent
795 and, if all has gone well, you will see
797 @smallexample
798 Hello WORLD!
799 @end smallexample
801 @noindent
802 appear in response to this command.
807 @node Running a Program with Multiple Units
808 @section Running a Program with Multiple Units
810 @noindent
811 Consider a slightly more complicated example that has three files: a
812 main program, and the spec and body of a package:
814 @smallexample
815 @cartouche
816 @group
817 @b{package} Greetings @b{is}
818    @b{procedure} Hello;
819    @b{procedure} Goodbye;
820 @b{end} Greetings;
822 @b{with} Ada.Text_IO; @b{use} Ada.Text_IO;
823 @b{package} @b{body} Greetings @b{is}
824    @b{procedure} Hello @b{is}
825    @b{begin}
826       Put_Line ("Hello WORLD!");
827    @b{end} Hello;
829    @b{procedure} Goodbye @b{is}
830    @b{begin}
831       Put_Line ("Goodbye WORLD!");
832    @b{end} Goodbye;
833 @b{end} Greetings;
834 @end group
836 @group
837 @b{with} Greetings;
838 @b{procedure} Gmain @b{is}
839 @b{begin}
840    Greetings.Hello;
841    Greetings.Goodbye;
842 @b{end} Gmain;
843 @end group
844 @end cartouche
845 @end smallexample
847 @noindent
848 Following the one-unit-per-file rule, place this program in the
849 following three separate files:
851 @table @file
852 @item GREETINGS.ADS
853 spec of package @code{Greetings}
855 @item GREETINGS.ADB
856 body of package @code{Greetings}
858 @item GMAIN.ADB
859 body of main program
860 @end table
862 @noindent
863 To build an executable version of
864 this program, we could use four separate steps to compile, bind, and link
865 the program, as follows:
867 @smallexample
868 $ GNAT COMPILE GMAIN.ADB
869 $ GNAT COMPILE GREETINGS.ADB
870 $ GNAT BIND gmain
871 $ GNAT LINK gmain
872 @end smallexample
875 @noindent
876 Note that there is no required order of compilation when using GNAT.
877 In particular it is perfectly fine to compile the main program first.
878 Also, it is not necessary to compile package specs in the case where
879 there is an accompanying body; you only need to compile the body. If you want
880 to submit these files to the compiler for semantic checking and not code generation,
881 then use the
882 @option{/NOLOAD} qualifier:
884 @smallexample
885    $ GNAT COMPILE GREETINGS.ADS /NOLOAD
886 @end smallexample
889 @noindent
890 Although the compilation can be done in separate steps as in the
891 above example, in practice it is almost always more convenient
892 to use the @code{GNAT MAKE} tool. All you need to know in this case
893 is the name of the main program's source file. The effect of the above four
894 commands can be achieved with a single one:
896 @smallexample
897 $ GNAT MAKE GMAIN.ADB
898 @end smallexample
901 @noindent
902 In the next section we discuss the advantages of using @code{GNAT MAKE} in
903 more detail.
905 @node Using the GNAT MAKE Utility
906 @section Using the @command{GNAT MAKE} Utility
908 @noindent
909 If you work on a program by compiling single components at a time using
910 @code{GNAT COMPILE}, you typically keep track of the units you modify. In order to
911 build a consistent system, you compile not only these units, but also any
912 units that depend on the units you have modified.
913 For example, in the preceding case,
914 if you edit @file{GMAIN.ADB}, you only need to recompile that file. But if
915 you edit @file{GREETINGS.ADS}, you must recompile both
916 @file{GREETINGS.ADB} and @file{GMAIN.ADB}, because both files contain
917 units that depend on @file{GREETINGS.ADS}.
919 @code{GNAT BIND} will warn you if you forget one of these compilation
920 steps, so that it is impossible to generate an inconsistent program as a
921 result of forgetting to do a compilation. Nevertheless it is tedious and
922 error-prone to keep track of dependencies among units.
923 One approach to handle the dependency-bookkeeping is to use a
924 makefile. However, makefiles present maintenance problems of their own:
925 if the dependencies change as you change the program, you must make
926 sure that the makefile is kept up-to-date manually, which is also an
927 error-prone process.
929 The @code{GNAT MAKE} utility takes care of these details automatically.
930 Invoke it using either one of the following forms:
932 @smallexample
933 $ GNAT MAKE GMAIN.ADB
934 $ GNAT MAKE GMAIN
935 @end smallexample
938 @noindent
939 The argument is the name of the file containing the main program;
940 you may omit the extension. @code{GNAT MAKE}
941 examines the environment, automatically recompiles any files that need
942 recompiling, and binds and links the resulting set of object files,
943 generating the executable file, @file{GMAIN.EXE}.
944 In a large program, it
945 can be extremely helpful to use @code{GNAT MAKE}, because working out by hand
946 what needs to be recompiled can be difficult.
948 Note that @code{GNAT MAKE}
949 takes into account all the Ada 95 rules that
950 establish dependencies among units. These include dependencies that result
951 from inlining subprogram bodies, and from
952 generic instantiation. Unlike some other
953 Ada make tools, @code{GNAT MAKE} does not rely on the dependencies that were
954 found by the compiler on a previous compilation, which may possibly
955 be wrong when sources change. @code{GNAT MAKE} determines the exact set of
956 dependencies from scratch each time it is run.
958 @node Editing with EMACS
959 @section Editing with EMACS
960 @cindex EMACS
962 @noindent
963 EMACS is an extensible self-documenting text editor that is available in a
964 separate VMSINSTAL kit.
966 Invoke EMACS by typing "EMACS" at the command prompt. To get started,
967 click on the EMACS Help menu and run the EMACS Tutorial.
968 In a character cell terminal, EMACS help is invoked with "Ctrl-h" (also written
969 as "C-h"), and the tutorial by "C-h t".
971 Documentation on EMACS and other tools is available in EMACS under the
972 pull-down menu button: Help - Info. After selecting Info, use the middle
973 mouse button to select a topic (e.g. EMACS).
975 In a character cell terminal, do "C-h i" to invoke info, and then "m"
976 (stands for menu) followed by the menu item desired, as in "m EMACS", to get
977 to the EMACS manual.
978 Help on EMACS is also available by typing "HELP EMACS" at the DCL command
979 prompt.
981 The tutorial is highly recommended in order to learn the intricacies of EMACS,
982 which is sufficiently extensible to provide for a complete programming
983 environment and shell for the sophisticated user.
986 @node The GNAT Compilation Model
987 @chapter The GNAT Compilation Model
988 @cindex GNAT compilation model
989 @cindex Compilation model
991 @menu
992 * Source Representation::
993 * Foreign Language Representation::
994 * File Naming Rules::
995 * Using Other File Names::
996 * Alternative File Naming Schemes::
997 * Generating Object Files::
998 * Source Dependencies::
999 * The Ada Library Information Files::
1000 * Binding an Ada Program::
1001 * Mixed Language Programming::
1002 * Building Mixed Ada & C++ Programs::
1003 * Comparison between GNAT and C/C++ Compilation Models::
1004 * Comparison between GNAT and Conventional Ada Library Models::
1005 @end menu
1007 @noindent
1008 This chapter describes the compilation model used by GNAT. Although
1009 similar to that used by other languages, such as C and C++, this model
1010 is substantially different from the traditional Ada compilation models,
1011 which are based on a library. The model is initially described without
1012 reference to the library-based model. If you have not previously used an
1013 Ada compiler, you need only read the first part of this chapter. The
1014 last section describes and discusses the differences between the GNAT
1015 model and the traditional Ada compiler models. If you have used other
1016 Ada compilers, this section will help you to understand those
1017 differences, and the advantages of the GNAT model.
1019 @node Source Representation
1020 @section Source Representation
1021 @cindex Latin-1
1023 @noindent
1024 Ada source programs are represented in standard text files, using
1025 Latin-1 coding. Latin-1 is an 8-bit code that includes the familiar
1026 7-bit ASCII set, plus additional characters used for
1027 representing foreign languages (@pxref{Foreign Language Representation}
1028 for support of non-USA character sets). The format effector characters
1029 are represented using their standard ASCII encodings, as follows:
1031 @table @code
1032 @item VT
1033 @findex VT
1034 Vertical tab, @code{16#0B#}
1036 @item HT
1037 @findex HT
1038 Horizontal tab, @code{16#09#}
1040 @item CR
1041 @findex CR
1042 Carriage return, @code{16#0D#}
1044 @item LF
1045 @findex LF
1046 Line feed, @code{16#0A#}
1048 @item FF
1049 @findex FF
1050 Form feed, @code{16#0C#}
1051 @end table
1053 @noindent
1054 Source files are in standard text file format. In addition, GNAT will
1055 recognize a wide variety of stream formats, in which the end of physical
1056 physical lines is marked by any of the following sequences:
1057 @code{LF}, @code{CR}, @code{CR-LF}, or @code{LF-CR}. This is useful
1058 in accommodating files that are imported from other operating systems.
1060 @cindex End of source file
1061 @cindex Source file, end
1062 @findex SUB
1063 The end of a source file is normally represented by the physical end of
1064 file. However, the control character @code{16#1A#} (@code{SUB}) is also
1065 recognized as signalling the end of the source file. Again, this is
1066 provided for compatibility with other operating systems where this
1067 code is used to represent the end of file.
1069 Each file contains a single Ada compilation unit, including any pragmas
1070 associated with the unit. For example, this means you must place a
1071 package declaration (a package @dfn{spec}) and the corresponding body in
1072 separate files. An Ada @dfn{compilation} (which is a sequence of
1073 compilation units) is represented using a sequence of files. Similarly,
1074 you will place each subunit or child unit in a separate file.
1076 @node Foreign Language Representation
1077 @section Foreign Language Representation
1079 @noindent
1080 GNAT supports the standard character sets defined in Ada 95 as well as
1081 several other non-standard character sets for use in localized versions
1082 of the compiler (@pxref{Character Set Control}).
1083 @menu
1084 * Latin-1::
1085 * Other 8-Bit Codes::
1086 * Wide Character Encodings::
1087 @end menu
1089 @node Latin-1
1090 @subsection Latin-1
1091 @cindex Latin-1
1093 @noindent
1094 The basic character set is Latin-1. This character set is defined by ISO
1095 standard 8859, part 1. The lower half (character codes @code{16#00#}
1096 ... @code{16#7F#)} is identical to standard ASCII coding, but the upper half is
1097 used to represent additional characters. These include extended letters
1098 used by European languages, such as French accents, the vowels with umlauts
1099 used in German, and the extra letter A-ring used in Swedish.
1101 @findex Ada.Characters.Latin_1
1102 For a complete list of Latin-1 codes and their encodings, see the source
1103 file of library unit @code{Ada.Characters.Latin_1} in file
1104 @file{A-CHLAT1.ADS}.
1105 You may use any of these extended characters freely in character or
1106 string literals. In addition, the extended characters that represent
1107 letters can be used in identifiers.
1109 @node Other 8-Bit Codes
1110 @subsection Other 8-Bit Codes
1112 @noindent
1113 GNAT also supports several other 8-bit coding schemes:
1115 @table @asis
1116 @cindex Latin-2
1117 @item Latin-2
1118 Latin-2 letters allowed in identifiers, with uppercase and lowercase
1119 equivalence.
1121 @item Latin-3
1122 @cindex Latin-3
1123 Latin-3 letters allowed in identifiers, with uppercase and lowercase
1124 equivalence.
1126 @item Latin-4
1127 @cindex Latin-4
1128 Latin-4 letters allowed in identifiers, with uppercase and lowercase
1129 equivalence.
1131 @item Latin-5
1132 @cindex Latin-5
1133 @cindex Cyrillic
1134 Latin-4 letters (Cyrillic) allowed in identifiers, with uppercase and lowercase
1135 equivalence.
1137 @item IBM PC (code page 437)
1138 @cindex code page 437
1139 This code page is the normal default for PCs in the U.S. It corresponds
1140 to the original IBM PC character set. This set has some, but not all, of
1141 the extended Latin-1 letters, but these letters do not have the same
1142 encoding as Latin-1. In this mode, these letters are allowed in
1143 identifiers with uppercase and lowercase equivalence.
1145 @item IBM PC (code page 850)
1146 @cindex code page 850
1147 This code page is a modification of 437 extended to include all the
1148 Latin-1 letters, but still not with the usual Latin-1 encoding. In this
1149 mode, all these letters are allowed in identifiers with uppercase and
1150 lowercase equivalence.
1152 @item Full Upper 8-bit
1153 Any character in the range 80-FF allowed in identifiers, and all are
1154 considered distinct. In other words, there are no uppercase and lowercase
1155 equivalences in this range. This is useful in conjunction with
1156 certain encoding schemes used for some foreign character sets (e.g.
1157 the typical method of representing Chinese characters on the PC).
1159 @item No Upper-Half
1160 No upper-half characters in the range 80-FF are allowed in identifiers.
1161 This gives Ada 83 compatibility for identifier names.
1162 @end table
1164 @noindent
1165 For precise data on the encodings permitted, and the uppercase and lowercase
1166 equivalences that are recognized, see the file @file{CSETS.ADB} in
1167 the GNAT compiler sources. You will need to obtain a full source release
1168 of GNAT to obtain this file.
1170 @node Wide Character Encodings
1171 @subsection Wide Character Encodings
1173 @noindent
1174 GNAT allows wide character codes to appear in character and string
1175 literals, and also optionally in identifiers, by means of the following
1176 possible encoding schemes:
1178 @table @asis
1180 @item Hex Coding
1181 In this encoding, a wide character is represented by the following five
1182 character sequence:
1184 @smallexample
1185 ESC a b c d
1186 @end smallexample
1188 @noindent
1189 Where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1190 characters (using uppercase letters) of the wide character code. For
1191 example, ESC A345 is used to represent the wide character with code
1192 @code{16#A345#}.
1193 This scheme is compatible with use of the full Wide_Character set.
1195 @item Upper-Half Coding
1196 @cindex Upper-Half Coding
1197 The wide character with encoding @code{16#abcd#} where the upper bit is on (in
1198 other words, "a" is in the range 8-F) is represented as two bytes,
1199 @code{16#ab#} and @code{16#cd#}. The second byte cannot be a format control
1200 character, but is not required to be in the upper half. This method can
1201 be also used for shift-JIS or EUC, where the internal coding matches the
1202 external coding.
1204 @item Shift JIS Coding
1205 @cindex Shift JIS Coding
1206 A wide character is represented by a two-character sequence,
1207 @code{16#ab#} and
1208 @code{16#cd#}, with the restrictions described for upper-half encoding as
1209 described above. The internal character code is the corresponding JIS
1210 character according to the standard algorithm for Shift-JIS
1211 conversion. Only characters defined in the JIS code set table can be
1212 used with this encoding method.
1214 @item EUC Coding
1215 @cindex EUC Coding
1216 A wide character is represented by a two-character sequence
1217 @code{16#ab#} and
1218 @code{16#cd#}, with both characters being in the upper half. The internal
1219 character code is the corresponding JIS character according to the EUC
1220 encoding algorithm. Only characters defined in the JIS code set table
1221 can be used with this encoding method.
1223 @item UTF-8 Coding
1224 A wide character is represented using
1225 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
1226 10646-1/Am.2. Depending on the character value, the representation
1227 is a one, two, or three byte sequence:
1228 @smallexample
1229 @iftex
1230 @leftskip=.7cm
1231 @end iftex
1232 16#0000#-16#007f#: 2#0xxxxxxx#
1233 16#0080#-16#07ff#: 2#110xxxxx# 2#10xxxxxx#
1234 16#0800#-16#ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx#
1236 @end smallexample
1238 @noindent
1239 where the xxx bits correspond to the left-padded bits of the
1240 16-bit character value. Note that all lower half ASCII characters
1241 are represented as ASCII bytes and all upper half characters and
1242 other wide characters are represented as sequences of upper-half
1243 (The full UTF-8 scheme allows for encoding 31-bit characters as
1244 6-byte sequences, but in this implementation, all UTF-8 sequences
1245 of four or more bytes length will be treated as illegal).
1246 @item Brackets Coding
1247 In this encoding, a wide character is represented by the following eight
1248 character sequence:
1250 @smallexample
1251 [ " a b c d " ]
1252 @end smallexample
1254 @noindent
1255 Where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1256 characters (using uppercase letters) of the wide character code. For
1257 example, ["A345"] is used to represent the wide character with code
1258 @code{16#A345#}. It is also possible (though not required) to use the
1259 Brackets coding for upper half characters. For example, the code
1260 @code{16#A3#} can be represented as @code{["A3"]}.
1262 This scheme is compatible with use of the full Wide_Character set,
1263 and is also the method used for wide character encoding in the standard
1264 ACVC (Ada Compiler Validation Capability) test suite distributions.
1266 @end table
1268 @noindent
1269 Note: Some of these coding schemes do not permit the full use of the
1270 Ada 95 character set. For example, neither Shift JIS, nor EUC allow the
1271 use of the upper half of the Latin-1 set.
1273 @node File Naming Rules
1274 @section File Naming Rules
1276 @noindent
1277 The default file name is determined by the name of the unit that the
1278 file contains. The name is formed by taking the full expanded name of
1279 the unit and replacing the separating dots with hyphens and using
1280 uppercase for all letters.
1282 An exception arises if the file name generated by the above rules starts
1283 with one of the characters
1284 A,G,I, or S,
1285 and the second character is a
1286 minus. In this case, the character dollar sign is used in place
1287 of the minus. The reason for this special rule is to avoid clashes with
1288 the standard names for child units of the packages System, Ada,
1289 Interfaces, and GNAT, which use the prefixes
1290 S- A- I- and G-
1291 respectively.
1293 The file extension is @file{.ADS} for a spec and
1294 @file{.ADB} for a body. The following list shows some
1295 examples of these rules.
1297 @table @file
1298 @item MAIN.ADS
1299 Main (spec)
1300 @item MAIN.ADB
1301 Main (body)
1302 @item ARITH_FUNCTIONS.ADS
1303 Arith_Functions (package spec)
1304 @item ARITH_FUNCTIONS.ADB
1305 Arith_Functions (package body)
1306 @item FUNC-SPEC.ADS
1307 Func.Spec (child package spec)
1308 @item FUNC-SPEC.ADB
1309 Func.Spec (child package body)
1310 @item MAIN-SUB.ADB
1311 Sub (subunit of Main)
1312 @item A$BAD.ADB
1313 A.Bad (child package body)
1314 @end table
1316 @noindent
1317 Following these rules can result in excessively long
1318 file names if corresponding
1319 unit names are long (for example, if child units or subunits are
1320 heavily nested). An option is available to shorten such long file names
1321 (called file name "krunching"). This may be particularly useful when
1322 programs being developed with GNAT are to be used on operating systems
1323 with limited file name lengths. @xref{Using GNAT KRUNCH}.
1325 Of course, no file shortening algorithm can guarantee uniqueness over
1326 all possible unit names; if file name krunching is used, it is your
1327 responsibility to ensure no name clashes occur. Alternatively you
1328 can specify the exact file names that you want used, as described
1329 in the next section. Finally, if your Ada programs are migrating from a
1330 compiler with a different naming convention, you can use the GNAT CHOP
1331 utility to produce source files that follow the GNAT naming conventions.
1332 (For details @pxref{Renaming Files Using GNAT CHOP}.)
1334 @node Using Other File Names
1335 @section Using Other File Names
1336 @cindex File names
1338 @noindent
1339 In the previous section, we have described the default rules used by
1340 GNAT to determine the file name in which a given unit resides. It is
1341 often convenient to follow these default rules, and if you follow them,
1342 the compiler knows without being explicitly told where to find all
1343 the files it needs.
1345 However, in some cases, particularly when a program is imported from
1346 another Ada compiler environment, it may be more convenient for the
1347 programmer to specify which file names contain which units. GNAT allows
1348 arbitrary file names to be used by means of the Source_File_Name pragma.
1349 The form of this pragma is as shown in the following examples:
1350 @cindex Source_File_Name pragma
1352 @smallexample
1353 @group
1354 @cartouche
1355 @b{pragma} Source_File_Name (My_Utilities.Stacks,
1356   Spec_File_Name => "MYUTILST_A.ADA");
1357 @b{pragma} Source_File_name (My_Utilities.Stacks,
1358   Body_File_Name => "MYUTILST.ADA");
1359 @end cartouche
1360 @end group
1361 @end smallexample
1363 @noindent
1364 As shown in this example, the first argument for the pragma is the unit
1365 name (in this example a child unit). The second argument has the form
1366 of a named association. The identifier
1367 indicates whether the file name is for a spec or a body;
1368 the file name itself is given by a string literal.
1370 The source file name pragma is a configuration pragma, which means that
1371 normally it will be placed in the @file{GNAT.ADC}
1372 file used to hold configuration
1373 pragmas that apply to a complete compilation environment.
1374 For more details on how the @file{GNAT.ADC} file is created and used
1375 @pxref{Handling of Configuration Pragmas}
1376 @cindex @file{GNAT.ADC}
1379 @noindent
1380 @code{GNAT MAKE} handles non-standard file names in the usual manner (the
1381 non-standard file name for the main program is simply used as the
1382 argument to GNAT MAKE). Note that if the extension is also non-standard,
1383 then it must be included in the GNAT MAKE command, it may not be omitted.
1385 @node Alternative File Naming Schemes
1386 @section Alternative File Naming Schemes
1387 @cindex File naming schemes, alternative
1388 @cindex File names
1390 In the previous section, we described the use of the @code{Source_File_Name}
1391 pragma to allow arbitrary names to be assigned to individual source files.
1392 However, this approach requires one pragma for each file, and especially in
1393 large systems can result in very long @file{GNAT.ADC} files, and also create
1394 a maintenance problem.
1396 GNAT also provides a facility for specifying systematic file naming schemes
1397 other than the standard default naming scheme previously described. An
1398 alternative scheme for naming is specified by the use of
1399 @code{Source_File_Name} pragmas having the following format:
1400 @cindex Source_File_Name pragma
1402 @smallexample
1403 pragma Source_File_Name (
1404    Spec_File_Name  => FILE_NAME_PATTERN
1405  [,Casing          => CASING_SPEC]
1406  [,Dot_Replacement => STRING_LITERAL]);
1408 pragma Source_File_Name (
1409    Body_File_Name  => FILE_NAME_PATTERN
1410  [,Casing          => CASING_SPEC]
1411  [,Dot_Replacement => STRING_LITERAL]);
1413 pragma Source_File_Name (
1414    Subunit_File_Name  => FILE_NAME_PATTERN
1415  [,Casing             => CASING_SPEC]
1416  [,Dot_Replacement    => STRING_LITERAL]);
1418 FILE_NAME_PATTERN ::= STRING_LITERAL
1419 CASING_SPEC ::= Lowercase | Uppercase | Mixedcase
1421 @end smallexample
1423 @noindent
1424 The @code{FILE_NAME_PATTERN} string shows how the file name is constructed.
1425 It contains a single asterisk character, and the unit name is substituted
1426 systematically for this asterisk. The optional parameter
1427 @code{Casing} indicates
1428 whether the unit name is to be all upper-case letters, all lower-case letters,
1429 or mixed-case. If no
1430 @code{Casing} parameter is used, then the default is all
1431 upper-case.
1433 The optional @code{Dot_Replacement} string is used to replace any periods
1434 that occur in subunit or child unit names. If no @code{Dot_Replacement}
1435 argument is used then separating dots appear unchanged in the resulting
1436 file name.
1437 Although the above syntax indicates that the
1438 @code{Casing} argument must appear
1439 before the @code{Dot_Replacement} argument, but it
1440 is also permissible to write these arguments in the opposite order.
1442 As indicated, it is possible to specify different naming schemes for
1443 bodies, specs, and subunits. Quite often the rule for subunits is the
1444 same as the rule for bodies, in which case, there is no need to give
1445 a separate @code{Subunit_File_Name} rule, and in this case the
1446 @code{Body_File_name} rule is used for subunits as well.
1448 The separate rule for subunits can also be used to implement the rather
1449 unusual case of a compilation environment (e.g. a single directory) which
1450 contains a subunit and a child unit with the same unit name. Although
1451 both units cannot appear in the same partition, the Ada Reference Manual
1452 allows (but does not require) the possibility of the two units coexisting
1453 in the same environment.
1455 The file name translation works in the following steps:
1457 @itemize @bullet
1459 @item
1460 If there is a specific @code{Source_File_Name} pragma for the given unit,
1461 then this is always used, and any general pattern rules are ignored.
1463 @item
1464 If there is a pattern type @code{Source_File_Name} pragma that applies to
1465 the unit, then the resulting file name will be used if the file exists. If
1466 more than one pattern matches, the latest one will be tried first, and the
1467 first attempt resulting in a reference to a file that exists will be used.
1469 @item
1470 If no pattern type @code{Source_File_Name} pragma that applies to the unit
1471 for which the corresponding file exists, then the standard GNAT default
1472 naming rules are used.
1474 @end itemize
1476 @noindent
1477 As an example of the use of this mechanism, consider a commonly used scheme
1478 in which file names are all lower case, with separating periods copied
1479 unchanged to the resulting file name, and specs end with ".1.ADA", and
1480 bodies end with ".2.ADA". GNAT will follow this scheme if the following
1481 two pragmas appear:
1483 @smallexample
1484 pragma Source_File_Name
1485   (Spec_File_Name => "*.1.ADA");
1486 pragma Source_File_Name
1487   (Body_File_Name => "*.2.ADA");
1488 @end smallexample
1490 @noindent
1491 The default GNAT scheme is actually implemented by providing the following
1492 default pragmas internally:
1494 @smallexample
1495 pragma Source_File_Name
1496   (Spec_File_Name => "*.ADS", Dot_Replacement => "-");
1497 pragma Source_File_Name
1498   (Body_File_Name => "*.ADB", Dot_Replacement => "-");
1499 @end smallexample
1501 @noindent
1502 Our final example implements a scheme typically used with one of the
1503 Ada 83 compilers, where the separator character for subunits was "__"
1504 (two underscores), specs were identified by adding @file{_.ADA}, bodies
1505 by adding @file{.ADA}, and subunits by
1506 adding @file{.SEP}. All file names were
1507 upper case. Child units were not present of course since this was an
1508 Ada 83 compiler, but it seems reasonable to extend this scheme to use
1509 the same double underscore separator for child units.
1511 @smallexample
1512 pragma Source_File_Name
1513   (Spec_File_Name => "*_.ADA",
1514    Dot_Replacement => "__",
1515    Casing = Uppercase);
1516 pragma Source_File_Name
1517   (Body_File_Name => "*.ADA",
1518    Dot_Replacement => "__",
1519    Casing = Uppercase);
1520 pragma Source_File_Name
1521   (Subunit_File_Name => "*.SEP",
1522    Dot_Replacement => "__",
1523    Casing = Uppercase);
1524 @end smallexample
1526 @node Generating Object Files
1527 @section Generating Object Files
1529 @noindent
1530 An Ada program consists of a set of source files, and the first step in
1531 compiling the program is to generate the corresponding object files.
1532 These are generated by compiling a subset of these source files.
1533 The files you need to compile are the following:
1535 @itemize @bullet
1536 @item
1537 If a package spec has no body, compile the package spec to produce the
1538 object file for the package.
1540 @item
1541 If a package has both a spec and a body, compile the body to produce the
1542 object file for the package. The source file for the package spec need
1543 not be compiled in this case because there is only one object file, which
1544 contains the code for both the spec and body of the package.
1546 @item
1547 For a subprogram, compile the subprogram body to produce the object file
1548 for the subprogram. The spec, if one is present, is as usual in a
1549 separate file, and need not be compiled.
1551 @item
1552 @cindex Subunits
1553 In the case of subunits, only compile the parent unit. A single object
1554 file is generated for the entire subunit tree, which includes all the
1555 subunits.
1557 @item
1558 Compile child units independently of their parent units
1559 (though, of course, the spec of all the ancestor unit must be present in order
1560 to compile a child unit).
1562 @item
1563 @cindex Generics
1564 Compile generic units in the same manner as any other units. The object
1565 files in this case are small dummy files that contain at most the
1566 flag used for elaboration checking. This is because GNAT always handles generic
1567 instantiation by means of macro expansion. However, it is still necessary to
1568 compile generic units, for dependency checking and elaboration purposes.
1569 @end itemize
1571 @noindent
1572 The preceding rules describe the set of files that must be compiled to
1573 generate the object files for a program. Each object file has the same
1574 name as the corresponding source file, except that the extension is
1575 @file{.OBJ} as usual.
1577 You may wish to compile other files for the purpose of checking their
1578 syntactic and semantic correctness. For example, in the case where a
1579 package has a separate spec and body, you would not normally compile the
1580 spec. However, it is convenient in practice to compile the spec to make
1581 sure it is error-free before compiling clients of this spec, because such
1582 compilations will fail if there is an error in the spec.
1584 GNAT provides an option for compiling such files purely for the
1585 purposes of checking correctness; such compilations are not required as
1586 part of the process of building a program. To compile a file in this
1587 checking mode, use the @option{/NOLOAD} qualifier.
1589 @node Source Dependencies
1590 @section Source Dependencies
1592 @noindent
1593 A given object file clearly depends on the source file which is compiled
1594 to produce it. Here we are using @dfn{depends} in the sense of a typical
1595 @code{make} utility; in other words, an object file depends on a source
1596 file if changes to the source file require the object file to be
1597 recompiled.
1598 In addition to this basic dependency, a given object may depend on
1599 additional source files as follows:
1601 @itemize @bullet
1602 @item
1603 If a file being compiled @code{with}'s a unit @var{X}, the object file
1604 depends on the file containing the spec of unit @var{X}. This includes
1605 files that are @code{with}'ed implicitly either because they are parents
1606 of @code{with}'ed child units or they are run-time units required by the
1607 language constructs used in a particular unit.
1609 @item
1610 If a file being compiled instantiates a library level generic unit, the
1611 object file depends on both the spec and body files for this generic
1612 unit.
1614 @item
1615 If a file being compiled instantiates a generic unit defined within a
1616 package, the object file depends on the body file for the package as
1617 well as the spec file.
1619 @item
1620 @findex Inline
1621 @cindex @option{/INLINE=PRAGMA} qualifier
1622 If a file being compiled contains a call to a subprogram for which
1623 pragma @code{Inline} applies and inlining is activated with the
1624 @option{/INLINE=PRAGMA} qualifier, the object file depends on the file containing the
1625 body of this subprogram as well as on the file containing the spec. Note
1626 that for inlining to actually occur as a result of the use of this qualifier,
1627 it is necessary to compile in optimizing mode.
1629 @cindex @option{-gnatN} qualifier
1630 The use of @option{-gnatN} activates a more extensive inlining optimization
1631 that is performed by the front end of the compiler. This inlining does
1632 not require that the code generation be optimized. Like @option{/INLINE=PRAGMA},
1633 the use of this qualifier generates additional dependencies.
1635 @item
1636 If an object file O  depends on the proper body of a subunit through inlining
1637 or instantiation, it depends on the parent unit of the subunit. This means that
1638 any modification of the parent unit or one of its subunits affects the
1639 compilation of O.
1641 @item
1642 The object file for a parent unit depends on all its subunit body files.
1644 @item
1645 The previous two rules meant that for purposes of computing dependencies and
1646 recompilation, a body and all its subunits are treated as an indivisible whole.
1648 @noindent
1649 These rules are applied transitively: if unit @code{A} @code{with}'s
1650 unit @code{B}, whose elaboration calls an inlined procedure in package
1651 @code{C}, the object file for unit @code{A} will depend on the body of
1652 @code{C}, in file @file{C.ADB}.
1654 The set of dependent files described by these rules includes all the
1655 files on which the unit is semantically dependent, as described in the
1656 Ada 95 Language Reference Manual. However, it is a superset of what the
1657 ARM describes, because it includes generic, inline, and subunit dependencies.
1659 An object file must be recreated by recompiling the corresponding source
1660 file if any of the source files on which it depends are modified. For
1661 example, if the @code{make} utility is used to control compilation,
1662 the rule for an Ada object file must mention all the source files on
1663 which the object file depends, according to the above definition.
1664 The determination of the necessary
1665 recompilations is done automatically when one uses @code{GNAT MAKE}.
1666 @end itemize
1668 @node The Ada Library Information Files
1669 @section The Ada Library Information Files
1670 @cindex Ada Library Information files
1671 @cindex @file{ali} files
1673 @noindent
1674 Each compilation actually generates two output files. The first of these
1675 is the normal object file that has a @file{.OBJ} extension. The second is a
1676 text file containing full dependency information. It has the same
1677 name as the source file, but an @file{.ALI} extension.
1678 This file is known as the Ada Library Information (@file{ali}) file.
1679 The following information is contained in the @file{ali} file.
1681 @itemize @bullet
1682 @item
1683 Version information (indicates which version of GNAT was used to compile
1684 the unit(s) in question)
1686 @item
1687 Main program information (including priority and time slice settings,
1688 as well as the wide character encoding used during compilation).
1690 @item
1691 List of arguments used in the @code{GNAT COMPILE} command for the compilation
1693 @item
1694 Attributes of the unit, including configuration pragmas used, an indication
1695 of whether the compilation was successful, exception model used etc.
1697 @item
1698 A list of relevant restrictions applying to the unit (used for consistency)
1699 checking.
1701 @item
1702 Categorization information (e.g. use of pragma @code{Pure}).
1704 @item
1705 Information on all @code{with}'ed units, including presence of
1706 @code{Elaborate} or @code{Elaborate_All} pragmas.
1708 @item
1709 Information from any @code{Linker_Options} pragmas used in the unit
1711 @item
1712 Information on the use of @code{Body_Version} or @code{Version}
1713 attributes in the unit.
1715 @item
1716 Dependency information. This is a list of files, together with
1717 time stamp and checksum information. These are files on which
1718 the unit depends in the sense that recompilation is required
1719 if any of these units are modified.
1721 @item
1722 Cross-reference data. Contains information on all entities referenced
1723 in the unit. Used by tools like @code{GNAT XREF} and @code{GNAT FIND} to
1724 provide cross-reference information.
1726 @end itemize
1728 @noindent
1729 For a full detailed description of the format of the @file{ali} file,
1730 see the source of the body of unit @code{Lib.Writ}, contained in file
1731 @file{LIB-WRIT.ADB} in the GNAT compiler sources.
1733 @node Binding an Ada Program
1734 @section Binding an Ada Program
1736 @noindent
1737 When using languages such as C and C++, once the source files have been
1738 compiled the only remaining step in building an executable program
1739 is linking the object modules together. This means that it is possible to
1740 link an inconsistent version of a program, in which two units have
1741 included different versions of the same header.
1743 The rules of Ada do not permit such an inconsistent program to be built.
1744 For example, if two clients have different versions of the same package,
1745 it is illegal to build a program containing these two clients.
1746 These rules are enforced by the GNAT binder, which also determines an
1747 elaboration order consistent with the Ada rules.
1749 The GNAT binder is run after all the object files for a program have
1750 been created. It is given the name of the main program unit, and from
1751 this it determines the set of units required by the program, by reading the
1752 corresponding ALI files. It generates error messages if the program is
1753 inconsistent or if no valid order of elaboration exists.
1755 If no errors are detected, the binder produces a main program, in Ada by
1756 default, that contains calls to the elaboration procedures of those
1757 compilation unit that require them, followed by
1758 a call to the main program. This Ada program is compiled to generate the
1759 object file for the main program. The name of
1760 the Ada file is @file{B$@var{xxx}.ADB} (with the corresponding spec
1761 @file{B$@var{xxx}.ADS}) where @var{xxx} is the name of the
1762 main program unit.
1764 Finally, the linker is used to build the resulting executable program,
1765 using the object from the main program from the bind step as well as the
1766 object files for the Ada units of the program.
1768 @node Mixed Language Programming
1769 @section Mixed Language Programming
1770 @cindex Mixed Language Programming
1772 @menu
1773 * Interfacing to C::
1774 * Calling Conventions::
1775 @end menu
1777 @node Interfacing to C
1778 @subsection Interfacing to C
1779 @noindent
1780 There are two ways to
1781 build a program that contains some Ada files and some other language
1782 files depending on whether the main program is in Ada or not.
1783 If the main program is in Ada, you should proceed as follows:
1785 @enumerate
1786 @item
1787 Compile the other language files to generate object files. For instance:
1788 @smallexample
1789 GNAT COMPILE FILE1.C
1790 GNAT COMPILE FILE2.C
1791 @end smallexample
1793 @item
1794 Compile the Ada units to produce a set of object files and ALI
1795 files. For instance:
1796 @smallexample
1797 GNAT MAKE /ACTIONS=COMPILE MY_MAIN.ADB
1798 @end smallexample
1800 @item
1801 Run the Ada binder on the Ada main program. For instance:
1802 @smallexample
1803 GNAT BIND MY_MAIN.ALI
1804 @end smallexample
1806 @item
1807 Link the Ada main program, the Ada objects and the other language
1808 objects. For instance:
1809 @smallexample
1810 GNAT LINK MY_MAIN.ALI FILE1.OBJ FILE2.OBJ
1811 @end smallexample
1812 @end enumerate
1814 The three last steps can be grouped in a single command:
1815 @smallexample
1816 GNAT MAKE MY_MAIN.ADB /LINKER_QUALIFIERS FILE1.OBJ FILE2.OBJ
1817 @end smallexample
1819 @cindex Binder output file
1820 @noindent
1821 If the main program is in some language other than Ada, you may
1822 have more than one entry point in the Ada subsystem. You must use a
1823 special option of the binder to generate callable routines to initialize
1824 and finalize the Ada units (@pxref{Binding with Non-Ada Main Programs}).
1825 Calls to the initialization and finalization routines must be inserted in
1826 the main program, or some other appropriate point in the code. The call to
1827 initialize the Ada units must occur before the first Ada subprogram is
1828 called, and the call to finalize the Ada units must occur after the last
1829 Ada subprogram returns. You use the same procedure for building the
1830 program as described previously. In this case, however, the binder
1831 only places the initialization and finalization subprograms into file
1832 @file{B$@var{xxx}.ADB} instead of the main program.
1833 So, if the main program is not in Ada, you should proceed as follows:
1835 @enumerate
1836 @item
1837 Compile the other language files to generate object files. For instance:
1838 @smallexample
1839 GNAT COMPILE FILE1.C
1840 GNAT COMPILE FILE2.C
1841 @end smallexample
1843 @item
1844 Compile the Ada units to produce a set of object files and ALI
1845 files. For instance:
1846 @smallexample
1847 GNAT MAKE /ACTIONS=COMPILE ENTRY_POINT1.ADB
1848 GNAT MAKE /ACTIONS=COMPILE ENTRY_POINT2.ADB
1849 @end smallexample
1851 @item
1852 Run the Ada binder on the Ada main program. For instance:
1853 @smallexample
1854 GNAT BIND /NOMAIN ENTRY_POINT1.ALI ENTRY_POINT2.ALI
1855 @end smallexample
1857 @item
1858 Link the Ada main program, the Ada objects and the other language
1859 objects. You only need to give the last entry point here. For instance:
1860 @smallexample
1861 GNAT LINK ENTRY_POINT2.ALI FILE1.OBJ FILE2.OBJ
1862 @end smallexample
1863 @end enumerate
1865 @node Calling Conventions
1866 @subsection Calling Conventions
1867 @cindex Foreign Languages
1868 @cindex Calling Conventions
1869 GNAT follows standard calling sequence conventions and will thus interface
1870 to any other language that also follows these conventions. The following
1871 Convention identifiers are recognized by GNAT:
1873 @itemize @bullet
1874 @cindex Interfacing to Ada
1875 @cindex Other Ada compilers
1876 @cindex Convention Ada
1877 @item
1878 Ada. This indicates that the standard Ada calling sequence will be
1879 used and all Ada data items may be passed without any limitations in the
1880 case where GNAT is used to generate both the caller and callee. It is also
1881 possible to mix GNAT generated code and code generated by another Ada
1882 compiler. In this case, the data types should be restricted to simple
1883 cases, including primitive types. Whether complex data types can be passed
1884 depends on the situation. Probably it is safe to pass simple arrays, such
1885 as arrays of integers or floats. Records may or may not work, depending
1886 on whether both compilers lay them out identically. Complex structures
1887 involving variant records, access parameters, tasks, or protected types,
1888 are unlikely to be able to be passed.
1890 Note that in the case of GNAT running
1891 on a platform that supports DEC Ada 83, a higher degree of compatibility
1892 can be guaranteed, and in particular records are layed out in an identical
1893 manner in the two compilers. Note also that if output from two different
1894 compilers is mixed, the program is responsible for dealing with elaboration
1895 issues. Probably the safest approach is to write the main program in the
1896 version of Ada other than GNAT, so that it takes care of its own elaboration
1897 requirements, and then call the GNAT-generated adainit procedure to ensure
1898 elaboration of the GNAT components. Consult the documentation of the other
1899 Ada compiler for further details on elaboration.
1901 However, it is not possible to mix the tasking run time of GNAT and
1902 DEC Ada 83, All the tasking operations must either be entirely within
1903 GNAT compiled sections of the program, or entirely within DEC Ada 83
1904 compiled sections of the program.
1906 @cindex Interfacing to Assembly
1907 @cindex Convention Assembler
1908 @item
1909 Assembler. Specifies assembler as the convention. In practice this has the
1910 same effect as convention Ada (but is not equivalent in the sense of being
1911 considered the same convention).
1913 @cindex Convention Asm
1914 @findex Asm
1915 @item
1916 Asm. Equivalent to Assembler.
1918 @cindex Convention Asm
1919 @findex Asm
1920 @item
1921 Asm. Equivalent to Assembly.
1923 @cindex Interfacing to COBOL
1924 @cindex Convention COBOL
1925 @findex COBOL
1926 @item
1927 COBOL. Data will be passed according to the conventions described
1928 in section B.4 of the Ada 95 Reference Manual.
1930 @findex C
1931 @cindex Interfacing to C
1932 @cindex Convention C
1933 @item
1934 C. Data will be passed according to the conventions described
1935 in section B.3 of the Ada 95 Reference Manual.
1937 @cindex Convention Default
1938 @findex Default
1939 @item
1940 Default. Equivalent to C.
1942 @cindex Convention External
1943 @findex External
1944 @item
1945 External. Equivalent to C.
1947 @findex C++
1948 @cindex Interfacing to C++
1949 @cindex Convention C++
1950 @item
1951 CPP. This stands for C++. For most purposes this is identical to C.
1952 See the separate description of the specialized GNAT pragmas relating to
1953 C++ interfacing for further details.
1955 @findex Fortran
1956 @cindex Interfacing to Fortran
1957 @cindex Convention Fortran
1958 @item
1959 Fortran. Data will be passed according to the conventions described
1960 in section B.5 of the Ada 95 Reference Manual.
1962 @item
1963 Intrinsic. This applies to an intrinsic operation, as defined in the Ada 95
1964 Reference Manual. If a a pragma Import (Intrinsic) applies to a subprogram,
1965 this means that the body of the subprogram is provided by the compiler itself,
1966 usually by means of an efficient code sequence, and that the user does not
1967 supply an explicit body for it. In an application program, the pragma can only
1968 be applied to the following two sets of names, which the GNAT compiler
1969 recognizes.
1970 @itemize @bullet
1971 @item
1972 Rotate_Left, Rotate_Right, Shift_Left, Shift_Right, Shift_Right_-
1973 Arithmetic.  The corresponding subprogram declaration must have
1974 two formal parameters. The
1975 first one must be a signed integer type or a modular type with a binary
1976 modulus, and the second parameter must be of type Natural.
1977 The return type must be the same as the type of the first argument. The size
1978 of this type can only be 8, 16, 32, or 64.
1979 @item binary arithmetic operators: "+", "-", "*", "/"
1980 The corresponding operator declaration must have parameters and result type
1981 that have the same root numeric type (for example, all three are long_float
1982 types). This simplifies the definition of operations that use type checking
1983 to perform dimensional checks:
1984 @smallexample
1985 type Distance is new Long_Float;
1986 type Time     is new Long_Float;
1987 type Velocity is new Long_Float;
1988 function "/" (D : Distance; T : Time)
1989   return Velocity;
1990 pragma Import (Intrinsic, "/");
1991 @end smallexample
1992 @noindent
1993 This common idiom is often programmed with a generic definition and an explicit
1994 body. The pragma makes it simpler to introduce such declarations. It incurs
1995 no overhead in compilation time or code size, because it is implemented as a
1996 single machine instruction.
1997 @end itemize
1998 @noindent
2000 @findex Stdcall
2001 @cindex Convention Stdcall
2002 @item
2003 Stdcall. This is relevant only to NT/Win95 implementations of GNAT,
2004 and specifies that the Stdcall calling sequence will be used, as defined
2005 by the NT API.
2007 @findex DLL
2008 @cindex Convention DLL
2009 @item
2010 DLL. This is equivalent to Stdcall.
2012 @findex Win32
2013 @cindex Convention Win32
2014 @item
2015 Win32. This is equivalent to Stdcall.
2017 @findex Stubbed
2018 @cindex Convention Stubbed
2019 @item
2020 Stubbed. This is a special convention that indicates that the compiler
2021 should provide a stub body that raises @code{Program_Error}.
2022 @end itemize
2024 @noindent
2025 GNAT additionally provides a useful pragma @code{Convention_Identifier}
2026 that can be used to parametrize conventions and allow additional synonyms
2027 to be specified. For example if you have legacy code in which the convention
2028 identifier Fortran77 was used for Fortran, you can use the configuration
2029 pragma:
2031 @smallexample
2032    pragma Convention_Identifier (Fortran77, Fortran);
2033 @end smallexample
2035 @noindent
2036 And from now on the identifier Fortran77 may be used as a convention
2037 identifier (for example in an @code{Import} pragma) with the same
2038 meaning as Fortran.
2040 @node Building Mixed Ada & C++ Programs
2041 @section Building Mixed Ada & C++ Programs
2043 @noindent
2044 Building a mixed application containing both Ada and C++ code may be a
2045 challenge for the unaware programmer. As a matter of fact, this
2046 interfacing has not been standardized in the Ada 95 reference manual due
2047 to the immaturity and lack of standard of C++ at the time. This
2048 section gives a few hints that should make this task easier. In
2049 particular the first section addresses the differences with
2050 interfacing with C. The second section looks into the delicate problem
2051 of linking the complete application from its Ada and C++ parts. The last
2052 section give some hints on how the GNAT run time can be adapted in order
2053 to allow inter-language dispatching with a new C++ compiler.
2055 @menu
2056 * Interfacing to C++::
2057 * Linking a Mixed C++ & Ada Program::
2058 * A Simple Example::
2059 * Adapting the Run Time to a New C++ Compiler::
2060 @end menu
2062 @node Interfacing to C++
2063 @subsection Interfacing to C++
2065 @noindent
2066 GNAT supports interfacing with C++ compilers generating code that is
2067 compatible with the standard Application Binary Interface of the given
2068 platform.
2070 @noindent
2071 Interfacing can be done at 3 levels: simple data, subprograms and
2072 classes. In the first 2 cases, GNAT offer a specific @var{Convention
2073 CPP} that behaves exactly like @var{Convention C}. Usually C++ mangle
2074 names of subprograms and currently GNAT does not provide any help to
2075 solve the demangling problem. This problem can be addressed in 2 ways:
2076 @itemize @bullet
2077 @item
2078 by modifying the C++ code in order to force a C convention using
2079 the @var{extern "C"} syntax.
2081 @item
2082 by figuring out the mangled name and use it as the Link_Name argument of
2083 the pragma import.
2084 @end itemize
2086 @noindent
2087 Interfacing at the class level can be achieved by using the GNAT specific
2088 pragmas such as @code{CPP_Class} and @code{CPP_Virtual}. See the GNAT
2089 Reference Manual for additional information.
2091 @node Linking a Mixed C++ & Ada Program
2092 @subsection Linking a Mixed C++ & Ada Program
2094 @noindent
2095 Usually the linker of the C++ development system must be used to link
2096 mixed applications because most C++ systems will resolve elaboration
2097 issues (such as calling constructors on global class instances)
2098 transparently during the link phase. GNAT has been adapted to ease the
2099 use of a foreign linker for the last phase. Three cases can be
2100 considered:
2101 @enumerate
2103 @item
2104 Using GNAT and G++ (GNU C++ compiler) from the same GCC
2105 installation. The c++ linker can simply be called by using the c++
2106 specific driver called @code{c++}. Note that this setup is not
2107 very common because it may request recompiling the whole GCC
2108 tree from sources and it does not allow to upgrade easily to a new
2109 version of one compiler for one of the two languages without taking the
2110 risk of destabilizing the other.
2112 @smallexample
2113 $ c++ -c file1.C
2114 $ c++ -c file2.C
2115 $ GNAT MAKE ada_unit /LINKER_QUALIFIERS FILE1.OBJ FILE2.OBJ --LINK=c++
2116 @end smallexample
2118 @item
2119 Using GNAT and G++ from 2 different GCC installations. If both compilers
2120 are on the PATH, the same method can be used. It is important to be
2121 aware that environment variables such as C_INCLUDE_PATH,
2122 GCC_EXEC_PREFIX, BINUTILS_ROOT or GCC_ROOT will affect both compilers at
2123 the same time and thus may make one of the 2 compilers operate
2124 improperly if they are set for the other. In particular it is important
2125 that the link command has access to the proper GNAT COMPILE library @file{libgcc.a},
2126 that is to say the one that is part of the C++ compiler
2127 installation. The implicit link command as suggested in the GNAT MAKE
2128 command from the former example can be replaced by an explicit link
2129 command with full verbosity in order to verify which library is used:
2130 @smallexample
2131 $ GNAT BIND ada_unit
2132 $ GNAT LINK -v -v ada_unit FILE1.OBJ FILE2.OBJ --LINK=c++
2133 @end smallexample
2134 If there is a problem due to interfering environment variables, it can
2135 be workaround by using an intermediate script. The following example
2136 shows the proper script to use when GNAT has not been installed at its
2137 default location and g++ has been installed at its default location:
2139 @smallexample
2140 $ GNAT LINK -v -v ada_unit FILE1.OBJ FILE2.OBJ --LINK=./my_script
2141 $ cat ./my_script
2142 #!/bin/sh
2143 unset BINUTILS_ROOT
2144 unset GCC_ROOT
2145 c++ $*
2146 @end smallexample
2148 @item
2149 Using a non GNU C++ compiler. The same set of command as previously
2150 described can be used to insure that the c++ linker is
2151 used. Nonetheless, you need to add the path to libgcc explicitely, since some
2152 libraries needed by GNAT are located in this directory:
2154 @smallexample
2156 $ GNAT LINK ada_unit FILE1.OBJ FILE2.OBJ --LINK=./my_script
2157 $ cat ./my_script
2158 #!/bin/sh
2159 CC $* `GNAT COMPILE -print-libgcc-file-name`
2161 @end smallexample
2163 Where CC is the name of the non GNU C++ compiler.
2165 @end enumerate
2167 @node A Simple Example
2168 @subsection  A Simple Example
2169 @noindent
2170 The following example, provided as part of the GNAT examples, show how
2171 to achieve procedural interfacing between Ada and C++ in both
2172 directions. The C++ class A has 2 methods. The first method is exported
2173 to Ada by the means of an extern C wrapper function. The second method
2174 calls an Ada subprogram. On the Ada side, The C++ calls is modelized by
2175 a limited record with a layout comparable to the C++ class. The Ada
2176 subprogram, in turn, calls the c++ method. So from the C++ main program
2177 the code goes back and forth between the 2 languages.
2179 @noindent
2180 Here are the compilation commands
2181 for native configurations:
2182 @smallexample
2183 $ GNAT MAKE -c simple_cpp_interface
2184 $ c++ -c cpp_main.C
2185 $ c++ -c ex7.C
2186 $ GNAT BIND -n simple_cpp_interface
2187 $ GNAT LINK simple_cpp_interface -o cpp_main --LINK=$(CPLUSPLUS)
2188       -lstdc++ EX7.OBJ CPP_MAIN.OBJ
2189 @end smallexample
2190 @noindent
2191 Here are the corresponding sources:
2192 @smallexample
2194 //cpp_main.C
2196 #include "ex7.h"
2198 extern "C" @{
2199   void adainit (void);
2200   void adafinal (void);
2201   void method1 (A *t);
2204 void method1 (A *t)
2206   t->method1 ();
2209 int main ()
2211   A obj;
2212   adainit ();
2213   obj.method2 (3030);
2214   adafinal ();
2217 //ex7.h
2219 class Origin @{
2220  public:
2221   int o_value;
2223 class A : public Origin @{
2224  public:
2225   void method1 (void);
2226   virtual void method2 (int v);
2227   A();
2228   int   a_value;
2231 //ex7.C
2233 #include "ex7.h"
2234 #include <stdio.h>
2236 extern "C" @{ void ada_method2 (A *t, int v);@}
2238 void A::method1 (void)
2240   a_value = 2020;
2241   printf ("in A::method1, a_value = %d \n",a_value);
2245 void A::method2 (int v)
2247    ada_method2 (this, v);
2248    printf ("in A::method2, a_value = %d \n",a_value);
2252 A::A(void)
2254    a_value = 1010;
2255   printf ("in A::A, a_value = %d \n",a_value);
2258 -- Ada sources
2259 @b{package} @b{body} Simple_Cpp_Interface @b{is}
2261    @b{procedure} Ada_Method2 (This : @b{in} @b{out} A; V : Integer) @b{is}
2262    @b{begin}
2263       Method1 (This);
2264       This.A_Value := V;
2265    @b{end} Ada_Method2;
2267 @b{end} Simple_Cpp_Interface;
2269 @b{package} Simple_Cpp_Interface @b{is}
2270    @b{type} A @b{is} @b{limited}
2271       @b{record}
2272          O_Value : Integer;
2273          A_Value : Integer;
2274       @b{end} @b{record};
2275    @b{pragma} Convention (C, A);
2277    @b{procedure} Method1 (This : @b{in} @b{out} A);
2278    @b{pragma} Import (C, Method1);
2280    @b{procedure} Ada_Method2 (This : @b{in} @b{out} A; V : Integer);
2281    @b{pragma} Export (C, Ada_Method2);
2283 @b{end} Simple_Cpp_Interface;
2284 @end smallexample
2286 @node Adapting the Run Time to a New C++ Compiler
2287 @subsection Adapting the Run Time to a New C++ Compiler
2288 @noindent
2289 GNAT offers the capability to derive Ada 95 tagged types directly from
2290 preexisting C++ classes and . See "Interfacing with C++" in the GNAT
2291 reference manual. The mechanism used by GNAT for achieving such a goal
2292 has been made user configurable through a GNAT library unit
2293 @code{Interfaces.CPP}. The default version of this file is adapted to
2294 the GNU c++ compiler. Internal knowledge of the virtual
2295 table layout used by the new C++ compiler is needed to configure
2296 properly this unit. The Interface of this unit is known by the compiler
2297 and cannot be changed except for the value of the constants defining the
2298 characteristics of the virtual table: CPP_DT_Prologue_Size, CPP_DT_Entry_Size,
2299 CPP_TSD_Prologue_Size, CPP_TSD_Entry_Size. Read comments in the source
2300 of this unit for more details.
2302 @node Comparison between GNAT and C/C++ Compilation Models
2303 @section Comparison between GNAT and C/C++ Compilation Models
2305 @noindent
2306 The GNAT model of compilation is close to the C and C++ models. You can
2307 think of Ada specs as corresponding to header files in C. As in C, you
2308 don't need to compile specs; they are compiled when they are used. The
2309 Ada @code{with} is similar in effect to the @code{#include} of a C
2310 header.
2312 One notable difference is that, in Ada, you may compile specs separately
2313 to check them for semantic and syntactic accuracy. This is not always
2314 possible with C headers because they are fragments of programs that have
2315 less specific syntactic or semantic rules.
2317 The other major difference is the requirement for running the binder,
2318 which performs two important functions. First, it checks for
2319 consistency. In C or C++, the only defense against assembling
2320 inconsistent programs lies outside the compiler, in a makefile, for
2321 example. The binder satisfies the Ada requirement that it be impossible
2322 to construct an inconsistent program when the compiler is used in normal
2323 mode.
2325 @cindex Elaboration order control
2326 The other important function of the binder is to deal with elaboration
2327 issues. There are also elaboration issues in C++ that are handled
2328 automatically. This automatic handling has the advantage of being
2329 simpler to use, but the C++ programmer has no control over elaboration.
2330 Where @code{GNAT BIND} might complain there was no valid order of
2331 elaboration, a C++ compiler would simply construct a program that
2332 malfunctioned at run time.
2334 @node Comparison between GNAT and Conventional Ada Library Models
2335 @section Comparison between GNAT and Conventional Ada Library Models
2337 @noindent
2338 This section is intended to be useful to Ada programmers who have
2339 previously used an Ada compiler implementing the traditional Ada library
2340 model, as described in the Ada 95 Language Reference Manual. If you
2341 have not used such a system, please go on to the next section.
2343 @cindex GNAT library
2344 In GNAT, there is no @dfn{library} in the normal sense. Instead, the set of
2345 source files themselves acts as the library. Compiling Ada programs does
2346 not generate any centralized information, but rather an object file and
2347 a ALI file, which are of interest only to the binder and linker.
2348 In a traditional system, the compiler reads information not only from
2349 the source file being compiled, but also from the centralized library.
2350 This means that the effect of a compilation depends on what has been
2351 previously compiled. In particular:
2353 @itemize @bullet
2354 @item
2355 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
2356 to the version of the unit most recently compiled into the library.
2358 @item
2359 Inlining is effective only if the necessary body has already been
2360 compiled into the library.
2362 @item
2363 Compiling a unit may obsolete other units in the library.
2364 @end itemize
2366 @noindent
2367 In GNAT, compiling one unit never affects the compilation of any other
2368 units because the compiler reads only source files. Only changes to source
2369 files can affect the results of a compilation. In particular:
2371 @itemize @bullet
2372 @item
2373 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
2374 to the source version of the unit that is currently accessible to the
2375 compiler.
2377 @item
2378 @cindex Inlining
2379 Inlining requires the appropriate source files for the package or
2380 subprogram bodies to be available to the compiler. Inlining is always
2381 effective, independent of the order in which units are complied.
2383 @item
2384 Compiling a unit never affects any other compilations. The editing of
2385 sources may cause previous compilations to be out of date if they
2386 depended on the source file being modified.
2387 @end itemize
2389 @noindent
2390 The most important result of these differences is that order of compilation
2391 is never significant in GNAT. There is no situation in which one is
2392 required to do one compilation before another. What shows up as order of
2393 compilation requirements in the traditional Ada library becomes, in
2394 GNAT, simple source dependencies; in other words, there is only a set
2395 of rules saying what source files must be present when a file is
2396 compiled.
2398 @node Compiling Using GNAT COMPILE
2399 @chapter Compiling Using @code{GNAT COMPILE}
2401 @noindent
2402 This chapter discusses how to compile Ada programs using the @code{GNAT COMPILE}
2403 command. It also describes the set of qualifiers
2404 that can be used to control the behavior of the compiler.
2405 @menu
2406 * Compiling Programs::
2407 * Qualifiers for GNAT COMPILE::
2408 * Search Paths and the Run-Time Library (RTL)::
2409 * Order of Compilation Issues::
2410 * Examples::
2411 @end menu
2413 @node Compiling Programs
2414 @section Compiling Programs
2416 @noindent
2417 The first step in creating an executable program is to compile the units
2418 of the program using the @code{GNAT COMPILE} command. You must compile the
2419 following files:
2421 @itemize @bullet
2422 @item
2423 the body file (@file{.ADB}) for a library level subprogram or generic
2424 subprogram
2426 @item
2427 the spec file (@file{.ADS}) for a library level package or generic
2428 package that has no body
2430 @item
2431 the body file (@file{.ADB}) for a library level package
2432 or generic package that has a body
2434 @end itemize
2436 @noindent
2437 You need @emph{not} compile the following files
2439 @itemize @bullet
2441 @item
2442 the spec of a library unit which has a body
2444 @item
2445 subunits
2446 @end itemize
2448 @noindent
2449 because they are compiled as part of compiling related units. GNAT
2450 package specs
2451 when the corresponding body is compiled, and subunits when the parent is
2452 compiled.
2453 @cindex No code generated
2454 If you attempt to compile any of these files, you will get one of the
2455 following error messages (where fff is the name of the file you compiled):
2457 @smallexample
2458 No code generated for file @var{fff} (@var{package spec})
2459 No code generated for file @var{fff} (@var{subunit})
2460 @end smallexample
2462 @noindent
2463 The basic command for compiling a file containing an Ada unit is
2465 @smallexample
2466 $ GNAT COMPILE [@var{qualifiers}] @file{file name}
2467 @end smallexample
2469 @noindent
2470 where @var{file name} is the name of the Ada file (usually
2471 having an extension
2472 @file{.ADS} for a spec or @file{.ADB} for a body).
2473 The result of a successful compilation is an object file, which has the
2474 same name as the source file but an extension of @file{.OBJ} and an Ada
2475 Library Information (ALI) file, which also has the same name as the
2476 source file, but with @file{.ALI} as the extension. GNAT creates these
2477 two output files in the current directory, but you may specify a source
2478 file in any directory using an absolute or relative path specification
2479 containing the directory information.
2481 @findex GNAT1
2482 @code{GNAT COMPILE} is actually a driver program that looks at the extensions of
2483 the file arguments and loads the appropriate compiler. For example, the
2484 GNU C compiler is @file{CC1}, and the Ada compiler is @file{GNAT1}.
2485 These programs are in directories known to the driver program (in some
2486 configurations via environment variables you set), but need not be in
2487 your path. The @code{GNAT COMPILE} driver also calls the assembler and any other
2488 utilities needed to complete the generation of the required object
2489 files.
2491 It is possible to supply several file names on the same @code{GNAT COMPILE}
2492 command. This causes @code{GNAT COMPILE} to call the appropriate compiler for
2493 each file. For example, the following command lists three separate
2494 files to be compiled:
2496 @smallexample
2497 $ GNAT COMPILE X.ADB Y.ADB Z.C
2498 @end smallexample
2500 @noindent
2501 calls @code{GNAT1} (the Ada compiler) twice to compile @file{X.ADB} and
2502 @file{Y.ADB}, and @code{CC1} (the C compiler) once to compile @file{Z.C}.
2503 The compiler generates three object files @file{X.OBJ}, @file{Y.OBJ} and
2504 @file{Z.OBJ} and the two ALI files @file{X.ALI} and @file{Y.ALI} from the
2505 Ada compilations. Any qualifiers apply to all the files listed.
2507 @node Qualifiers for GNAT COMPILE
2508 @section Qualifiers for @code{GNAT COMPILE}
2510 @noindent
2511 The @code{GNAT COMPILE} command accepts qualifiers that control the
2512 compilation process. These qualifiers are fully described in this section.
2513 First we briefly list all the qualifiers, in alphabetical order, then we
2514 describe the qualifiers in more detail in functionally grouped sections.
2516 @menu
2517 * Output and Error Message Control::
2518 * Debugging and Assertion Control::
2519 * Run-Time Checks::
2520 * Stack Overflow Checking::
2521 * Run-Time Control::
2522 * Validity Checking::
2523 * Style Checking::
2524 * Using GNAT COMPILE for Syntax Checking::
2525 * Using GNAT COMPILE for Semantic Checking::
2526 * Compiling Ada 83 Programs::
2527 * Character Set Control::
2528 * File Naming Control::
2529 * Subprogram Inlining Control::
2530 * Auxiliary Output Control::
2531 * Debugging Control::
2532 * Units to Sources Mapping Files::
2533 @end menu
2535 @table @code
2537 @item /DEBUG
2538 @cindex @code{/DEBUG} (@code{GNAT COMPILE})
2539 Generate debugging information. This information is stored in the object
2540 file and copied from there to the final executable file by the linker,
2541 where it can be read by the debugger. You must use the
2542 @code{/DEBUG} qualifier if you plan on using the debugger.
2544 @item /SEARCH=@var{dir}
2545 @cindex @code{/SEARCH} (@code{GNAT COMPILE})
2546 @cindex RTL
2547 Direct GNAT to search the @var{dir} directory for source files needed by
2548 the current compilation
2549 (@pxref{Search Paths and the Run-Time Library (RTL)}).
2551 @item /NOCURRENT_DIRECTORY
2552 @cindex @code{/NOCURRENT_DIRECTORY} (@code{GNAT COMPILE})
2553 @cindex RTL
2554 Except for the source file named in the command line, do not look for source files
2555 in the directory containing the source file named in the command line
2556 (@pxref{Search Paths and the Run-Time Library (RTL)}).
2560 @item  /NOOPTIMIZE (default)
2561 @itemx /OPTIMIZE[=(keyword[,...])]
2562 Selects the level of optimization for your program. The supported
2563 keywords are as follows:
2564 @table @code
2565 @item   ALL (default)
2566 Perform most optimizations, including those that
2567 be expensive.
2569 @item   NONE
2570 Do not do any optimizations. Same as @code{/NOOPTIMIZE}.
2572 @item SOME
2573 Perform some optimizations, but omit ones that are costly.
2575 @item   DEVELOPMENT
2576 Same as @code{SOME}.
2578 @item   INLINING
2579 Full optimization, and also attempt automatic inlining of small
2580 subprograms within a unit (@pxref{Inlining of Subprograms}).
2582 @item   UNROLL_LOOPS
2583 Try to unroll loops. This keyword may be specified together with
2584 any keyword above other than @code{NONE}. Loop unrolling
2585 usually, but not always, improves the performance of programs.
2586 @end table
2588 @item /RUNTIME_SYSTEM=@var{rts-path}
2589 @cindex @code{/RUNTIME_SYSTEM} (@code{GNAT COMPILE})
2590 Specifies the default location of the runtime library. Same meaning as the
2591 equivalent @code{GNAT MAKE} flag (see @ref{Qualifiers for GNAT MAKE}).
2593 @item /ASM
2594 @cindex @code{/ASM} (@code{GNAT COMPILE})
2595 Used to
2596 cause the assembler source file to be
2597 generated, using @file{.S} as the extension,
2598 instead of the object file.
2599 This may be useful if you need to examine the generated assembly code.
2601 @item /VERBOSE
2602 @cindex @code{/VERBOSE} (@code{GNAT COMPILE})
2603 Show commands generated by the @code{GNAT COMPILE} driver. Normally used only for
2604 debugging purposes or if you need to be sure what version of the
2605 compiler you are executing.
2608 @item /CHECKS=ASSERTIONS
2609 Assertions enabled. @code{Pragma Assert} and @code{pragma Debug} to be
2610 activated.
2612 @item -gnatA
2613 Avoid processing @file{GNAT.ADC}. If a GNAT.ADC file is present, it will be ignored.
2615 @item /WARNINGS=BRIEF
2616 Generate brief messages to @file{SYS$ERROR} even if verbose mode set.
2618 @item /NOLOAD
2619 Check syntax and semantics only (no code generation attempted).
2621 @item /COMPRESS_NAMES
2622 Compress debug information and external symbol name table entries.
2624 @item /XDEBUG
2625 Output expanded source files for source level debugging. This qualifier
2626 also suppress generation of cross-reference information (see /XREF=SUPPRESS).
2628 @item -gnatec@var{path}
2629 Specify a configuration pragma file. (see @ref{The Configuration Pragmas Files})
2631 @item -gnatem@var{path}
2632 Specify a mapping file. (see @ref{Units to Sources Mapping Files})
2634 @item /CHECKS=ELABORATION
2635 Full dynamic elaboration checks.
2637 @item /REPORT_ERRORS=FULL
2638 Full errors. Multiple errors per line, all undefined references.
2640 @item /UPPERCASE_EXTERNALS
2641 Externals names are folded to all uppercase.
2643 @item /STYLE=GNAT
2644 Internal GNAT implementation mode. This should not be used for
2645 applications programs, it is intended only for use by the compiler
2646 and its run-time library. For documentation, see the GNAT sources.
2648 @item /EXPAND_SOURCE
2649 List generated expanded code in source form.
2651 @item /IDENTIFIER_CHARACTER_SET=@var{c}
2652 Identifier character set
2653 For details of the possible selections for @var{c},
2654 see @xref{Character Set Control}.
2656 @item /HELP
2657 Output usage information. The output is written to @file{SYS$OUTPUT}.
2659 @item /FILE_NAME_MAX_LENGTH=@var{n}
2660 Limit file names to @var{n} (1-999) characters .
2662 @item /LIST
2663 Output full source listing with embedded error messages.
2665 @item /ERROR_LIMIT=@var{n}
2666 Limit number of detected errors to @var{n} (1-999).
2668 @item /INLINE=PRAGMA
2669 Activate inlining across unit boundaries for subprograms for which
2670 pragma @code{inline} is specified.
2672 @item -gnatN
2673 Activate front end inlining.
2675 @item /INLINE=SUPPRESS
2676 Suppresses all inlining, even if other optimization or inlining qualifiers
2677 are set.
2680 @item /CHECKS=OVERFLOW
2681 Enable numeric overflow checking (which is not normally enabled by
2682 default). Not that division by zero is a separate check that is not
2683 controlled by this qualifier (division by zero checking is on by default).
2685 @item /CHECKS=SUPPRESS_ALL
2686 Suppress all checks.
2688 @item /TRY_SEMANTICS
2689 Don't quit; try semantics, even if parse errors.
2691 @item /FORCE_ALI
2692 Don't quit; generate @file{ali} and tree files even if illegalities.
2694 @item /POLLING_ENABLE
2695 Enable polling. This is required on some systems (notably Windows NT) to
2696 obtain asynchronous abort and asynchronous transfer of control capability.
2697 See the description of pragma Polling in the GNAT Reference Manual for
2698 full details.
2700 @item /REPRESENTATION_INFO[0/1/2/3][s]
2701 Output representation information for declared types and objects.
2703 @item /SYNTAX_ONLY
2704 Syntax check only.
2706 @item /TREE_OUTPUT
2707 Tree output file to be generated.
2709 @item -gnatT nnn
2710 Set time slice to specified number of microseconds
2712 @item /UNITS_LIST
2713 List units for this compilation.
2715 @item /UNIQUE_ERROR_TAG
2716 Tag all error messages with the unique string "error:"
2718 @item /REPORT_ERRORS=VERBOSE
2719 Verbose mode. Full error output with source lines to @file{SYS$OUTPUT}.
2721 @item /VALIDITY_CHECKING
2722 Control level of validity checking. See separate section describing
2723 this feature.
2725 @item /WARNINGS=@var{xxx}
2726 Warning mode where
2727 @var{xxx} is a string of options describing the exact warnings that
2728 are enabled or disabled. See separate section on warning control.
2730 @item /WIDE_CHARACTER_ENCODING=@var{e}
2731 Wide character encoding method
2732 (@var{e}=@code{BRACKETS, NONE, HEX, UPPER, SHIFT_JIS, EUC, UTF8})
2734 @item /XREF=SUPPRESS
2735 Suppress generation of cross-reference information.
2737 @item /STYLE_CHECKS=(option,option..)
2738 Enable built-in style checks. See separate section describing this feature.
2740 @item /DISTRIBUTION_STUBS=@var{m}
2741 Distribution stub generation and compilation
2742 (@var{m}=@code{RECEIVER} or @code{CALLER} to specify the type of stubs
2743 to be generated and compiled).
2745 @item /83
2746 Enforce Ada 83 restrictions.
2748 @end table
2751 @noindent
2752 The following restrictions apply to the combination of qualifiers
2753 in this manner:
2755 @itemize @bullet
2756 @item
2757 The qualifier @option{/NOLOAD} if combined with other qualifiers must come
2758 first in the string.
2760 @item
2761 The qualifier @option{/SYNTAX_ONLY} if combined with other qualifiers must come
2762 first in the string.
2764 @item
2765 Once a "y" appears in the string (that is a use of the @option{/STYLE=}
2766 qualifier), then all further characters in the qualifier are interpreted
2767 as style modifiers (see description of @option{/STYLE=}).
2769 @item
2770 Once a "d" appears in the string (that is a use of the @option{-gnatd}
2771 qualifier), then all further characters in the qualifier are interpreted
2772 as debug flags (see description of @option{-gnatd}).
2774 @item
2775 Once a "w" appears in the string (that is a use of the @option{-gnatw}
2776 qualifier), then all further characters in the qualifier are interpreted
2777 as warning mode modifiers (see description of @option{-gnatw}).
2779 @item
2780 Once a "V" appears in the string (that is a use of the @option{/VALIDITY_CHECKING}
2781 qualifier), then all further characters in the qualifier are interpreted
2782 as validity checking options (see description of @option{/VALIDITY_CHECKING}).
2784 @end itemize
2786 @node Output and Error Message Control
2787 @subsection Output and Error Message Control
2788 @findex SYS$ERROR
2790 @noindent
2791 The standard default format for error messages is called "brief format."
2792 Brief format messages are written to @file{SYS$ERROR} (the standard error
2793 file) and have the following form:
2795 @smallexample
2796 @iftex
2797 @leftskip=.7cm
2798 @end iftex
2799 E.ADB:3:04: Incorrect spelling of keyword "function"
2800 E.ADB:4:20: ";" should be "is"
2801 @end smallexample
2803 @noindent
2804 The first integer after the file name is the line number in the file,
2805 and the second integer is the column number within the line.
2806 @code{glide} can parse the error messages
2807 and point to the referenced character.
2808 The following qualifiers provide control over the error message
2809 format:
2811 @table @code
2812 @item /REPORT_ERRORS=VERBOSE
2813 @cindex @option{/REPORT_ERRORS=VERBOSE} (@code{GNAT COMPILE})
2814 @findex SYS$OUTPUT
2815 The effect of this setting is to write long-format error
2816 messages to @file{SYS$OUTPUT} (the standard output file.
2817 The same program compiled with the
2818 @option{/REPORT_ERRORS=VERBOSE} qualifier would generate:
2820 @smallexample
2821 @group
2822 @cartouche
2823 3. funcion X (Q : Integer)
2824    |
2825 >>> Incorrect spelling of keyword "function"
2826 4. return Integer;
2827                  |
2828 >>> ";" should be "is"
2829 @end cartouche
2830 @end group
2831 @end smallexample
2833 @noindent
2834 The vertical bar indicates the location of the error, and the @samp{>>>}
2835 prefix can be used to search for error messages. When this qualifier is
2836 used the only source lines output are those with errors.
2838 @item /LIST
2839 @cindex @option{/LIST} (@code{GNAT COMPILE})
2840 This qualifier causes a full listing of
2841 the file to be generated. The output might look as follows:
2843 @smallexample
2844 @group
2845 @cartouche
2846  1. procedure E is
2847  2.    V : Integer;
2848  3.    funcion X (Q : Integer)
2849        |
2850     >>> Incorrect spelling of keyword "function"
2851  4.     return Integer;
2852                       |
2853     >>> ";" should be "is"
2854  5.    begin
2855  6.       return Q + Q;
2856  7.    end;
2857  8. begin
2858  9.    V := X + X;
2859 10.end E;
2860 @end cartouche
2861 @end group
2862 @end smallexample
2864 @noindent
2865 @findex SYS$ERROR
2866 When you specify the @option{/REPORT_ERRORS=VERBOSE} or @option{/LIST} qualifiers and
2867 standard output is redirected, a brief summary is written to
2868 @file{SYS$ERROR} (standard error) giving the number of error messages and
2869 warning messages generated.
2871 @item /UNIQUE_ERROR_TAG
2872 @cindex @option{/UNIQUE_ERROR_TAG} (@code{GNAT COMPILE})
2873 This qualifier forces all error messages to be preceded by the unique
2874 string "error:". This means that error messages take a few more
2875 characters in space, but allows easy searching for and identification
2876 of error messages.
2878 @item /WARNINGS=BRIEF
2879 @cindex @option{/WARNINGS=BRIEF} (@code{GNAT COMPILE})
2880 This qualifier causes GNAT to generate the
2881 brief format error messages to @file{SYS$ERROR} (the standard error
2882 file) as well as the verbose
2883 format message or full listing (which as usual is written to
2884 @file{SYS$OUTPUT} (the standard output file).
2886 @item /ERROR_LIMIT=@var{n}
2887 @cindex @option{/ERROR_LIMIT} (@code{GNAT COMPILE})
2888 @var{n} is a decimal integer in the
2889 range of 1 to 999 and limits the number of error messages to be
2890 generated. For example, using @option{/ERROR_LIMIT=2} might yield
2892 @smallexample
2893 @iftex
2894 @leftskip=.7cm
2895 @end iftex
2896 E.ADB:3:04: Incorrect spelling of keyword "function"
2897 E.ADB:5:35: missing ".."
2898 fatal error: maximum errors reached
2899 compilation abandoned
2900 @end smallexample
2902 @item /REPORT_ERRORS=FULL
2903 @cindex @option{/REPORT_ERRORS=FULL} (@code{GNAT COMPILE})
2904 @cindex Error messages, suppressing
2905 Normally, the compiler suppresses error messages that are likely to be
2906 redundant. This qualifier causes all error
2907 messages to be generated. In particular, in the case of
2908 references to undefined variables. If a given variable is referenced
2909 several times, the normal format of messages is
2910 @smallexample
2911 @iftex
2912 @leftskip=.7cm
2913 @end iftex
2914 E.ADB:7:07: "V" is undefined (more references follow)
2915 @end smallexample
2917 @noindent
2918 where the parenthetical comment warns that there are additional
2919 references to the variable @code{V}. Compiling the same program with the
2920 @option{/REPORT_ERRORS=FULL} qualifier yields
2922 @smallexample
2923 E.ADB:7:07: "V" is undefined
2924 E.ADB:8:07: "V" is undefined
2925 E.ADB:8:12: "V" is undefined
2926 E.ADB:8:16: "V" is undefined
2927 E.ADB:9:07: "V" is undefined
2928 E.ADB:9:12: "V" is undefined
2929 @end smallexample
2931 @item /TRY_SEMANTICS
2932 @cindex @option{/TRY_SEMANTICS} (@code{GNAT COMPILE})
2933 In normal operation mode, the compiler first parses the program and
2934 determines if there are any syntax errors. If there are, appropriate
2935 error messages are generated and compilation is immediately terminated.
2936 This qualifier tells
2937 GNAT to continue with semantic analysis even if syntax errors have been
2938 found. This may enable the detection of more errors in a single run. On
2939 the other hand, the semantic analyzer is more likely to encounter some
2940 internal fatal error when given a syntactically invalid tree.
2942 @item /FORCE_ALI
2943 In normal operation mode, the @file{ali} file is not generated if any
2944 illegalities are detected in the program. The use of @option{/FORCE_ALI} forces
2945 generation of the @file{ali} file. This file is marked as being in
2946 error, so it cannot be used for binding purposes, but it does contain
2947 reasonably complete cross-reference information, and thus may be useful
2948 for use by tools (e.g. semantic browsing tools or integrated development
2949 environments) that are driven from the @file{ali} file.
2951 In addition, if @option{/TREE_OUTPUT} is also specified, then the tree file is
2952 generated even if there are illegalities. It may be useful in this case
2953 to also specify @option{/TRY_SEMANTICS} to ensure that full semantic processing
2954 occurs. The resulting tree file can be processed by ASIS, for the purpose
2955 of providing partial information about illegal units, but if the error
2956 causes the tree to be badly malformed, then ASIS may crash during the
2957 analysis.
2959 @end table
2961 @noindent
2962 In addition to error messages, which correspond to illegalities as defined
2963 in the Ada 95 Reference Manual, the compiler detects two kinds of warning
2964 situations.
2966 @cindex Warning messages
2967 First, the compiler considers some constructs suspicious and generates a
2968 warning message to alert you to a possible error. Second, if the
2969 compiler detects a situation that is sure to raise an exception at
2970 run time, it generates a warning message. The following shows an example
2971 of warning messages:
2972 @smallexample
2973 @iftex
2974 @leftskip=.2cm
2975 @end iftex
2976 E.ADB:4:24: warning: creation of object may raise Storage_Error
2977 E.ADB:10:17: warning: static value out of range
2978 E.ADB:10:17: warning: "Constraint_Error" will be raised at run time
2980 @end smallexample
2982 @noindent
2983 GNAT considers a large number of situations as appropriate
2984 for the generation of warning messages. As always, warnings are not
2985 definite indications of errors. For example, if you do an out-of-range
2986 assignment with the deliberate intention of raising a
2987 @code{Constraint_Error} exception, then the warning that may be
2988 issued does not indicate an error. Some of the situations for which GNAT
2989 issues warnings (at least some of the time) are given in the following
2990 list, which is not necessarily complete.
2992 @itemize @bullet
2993 @item
2994 Possible infinitely recursive calls
2996 @item
2997 Out-of-range values being assigned
2999 @item
3000 Possible order of elaboration problems
3002 @item
3003 Unreachable code
3005 @item
3006 Fixed-point type declarations with a null range
3008 @item
3009 Variables that are never assigned a value
3011 @item
3012 Variables that are referenced before being initialized
3014 @item
3015 Task entries with no corresponding accept statement
3017 @item
3018 Duplicate accepts for the same task entry in a select
3020 @item
3021 Objects that take too much storage
3023 @item
3024 Unchecked conversion between types of differing sizes
3026 @item
3027 Missing return statements along some execution paths in a function
3029 @item
3030 Incorrect (unrecognized) pragmas
3032 @item
3033 Incorrect external names
3035 @item
3036 Allocation from empty storage pool
3038 @item
3039 Potentially blocking operations in protected types
3041 @item
3042 Suspicious parenthesization of expressions
3044 @item
3045 Mismatching bounds in an aggregate
3047 @item
3048 Attempt to return local value by reference
3050 @item
3051 Unrecognized pragmas
3053 @item
3054 Premature instantiation of a generic body
3056 @item
3057 Attempt to pack aliased components
3059 @item
3060 Out of bounds array subscripts
3062 @item
3063 Wrong length on string assignment
3065 @item
3066 Violations of style rules if style checking is enabled
3068 @item
3069 Unused with clauses
3071 @item
3072 Bit_Order usage that does not have any effect
3074 @item
3075 Compile time biased rounding of floating-point constant
3077 @item
3078 Standard.Duration used to resolve universal fixed expression
3080 @item
3081 Dereference of possibly null value
3083 @item
3084 Declaration that is likely to cause storage error
3086 @item
3087 Internal GNAT unit with'ed by application unit
3089 @item
3090 Values known to be out of range at compile time
3092 @item
3093 Unreferenced labels and variables
3095 @item
3096 Address overlays that could clobber memory
3098 @item
3099 Unexpected initialization when address clause present
3101 @item
3102 Bad alignment for address clause
3104 @item
3105 Useless type conversions
3107 @item
3108 Redundant assignment statements
3110 @item
3111 Accidental hiding of name by child unit
3113 @item
3114 Unreachable code
3116 @item
3117 Access before elaboration detected at compile time
3119 @item
3120 A range in a @code{for} loop that is known to be null or might be null
3122 @end itemize
3124 @noindent
3125 The following qualifiers are available to control the handling of
3126 warning messages:
3128 @table @code
3129 @item /WARNINGS=OPTIONAL (activate all optional errors)
3130 @cindex @option{/WARNINGS=OPTIONAL} (@code{GNAT COMPILE})
3131 This qualifier activates most optional warning messages, see remaining list
3132 in this section for details on optional warning messages that can be
3133 individually controlled. The warnings that are not turned on by this
3134 qualifier are @option{/WARNINGS=BIASED_ROUNDING} (biased rounding),
3135 @option{/WARNINGS=IMPLICIT_DEREFERENCE} (implicit dereferencing),
3136 and @option{/WARNINGS=HIDING} (hiding). All other optional warnings are
3137 turned on.
3139 @item /WARNINGS=NOOPTIONAL (suppress all optional errors)
3140 @cindex @option{/WARNINGS=NOOPTIONAL} (@code{GNAT COMPILE})
3141 This qualifier suppresses all optional warning messages, see remaining list
3142 in this section for details on optional warning messages that can be
3143 individually controlled.
3145 @item /WARNINGS=BIASED_ROUNDING (activate warnings on biased rounding)
3146 @cindex @option{/WARNINGS=BIASED_ROUNDING} (@code{GNAT COMPILE})
3147 @cindex Rounding, biased
3148 @cindex Biased rounding
3149 If a static floating-point expression has a value that is exactly half
3150 way between two adjacent machine numbers, then the rules of Ada
3151 (Ada Reference Manual, section 4.9(38)) require that this rounding
3152 be done away from zero, even if the normal unbiased rounding rules
3153 at run time would require rounding towards zero. This warning message
3154 alerts you to such instances where compile-time rounding and run-time
3155 rounding are not equivalent. If it is important to get proper run-time
3156 rounding, then you can force this by making one of the operands into
3157 a variable. The default is that such warnings are not generated.
3158 Note that @option{/WARNINGS=OPTIONAL} does not affect the setting of
3159 this warning option.
3161 @item /WARNINGS=NOBIASED_ROUNDING (suppress warnings on biased rounding)
3162 @cindex @option{/WARNINGS=NOBIASED_ROUNDING} (@code{GNAT COMPILE})
3163 This qualifier disables warnings on biased rounding.
3165 @item /WARNINGS=CONDITIONALS (activate warnings on conditionals)
3166 @cindex @option{/WARNINGS=CONDITIONALS} (@code{GNAT COMPILE})
3167 @cindex Conditionals, constant
3168 This qualifier activates warnings for conditional expressions used in
3169 tests that are known to be True or False at compile time. The default
3170 is that such warnings are not generated.
3171 This warning can also be turned on using @option{/WARNINGS=OPTIONAL}.
3173 @item /WARNINGS=NOCONDITIONALS (suppress warnings on conditionals)
3174 @cindex @option{/WARNINGS=NOCONDITIONALS} (@code{GNAT COMPILE})
3175 This qualifier suppresses warnings for conditional expressions used in
3176 tests that are known to be True or False at compile time.
3178 @item /WARNINGS=IMPLICIT_DEREFERENCE (activate warnings on implicit dereferencing)
3179 @cindex @option{/WARNINGS=IMPLICIT_DEREFERENCE} (@code{GNAT COMPILE})
3180 If this qualifier is set, then the use of a prefix of an access type
3181 in an indexed component, slice, or selected component without an
3182 explicit @code{.all} will generate a warning. With this warning
3183 enabled, access checks occur only at points where an explicit
3184 @code{.all} appears in the source code (assuming no warnings are
3185 generated as a result of this qualifier). The default is that such
3186 warnings are not generated.
3187 Note that @option{/WARNINGS=OPTIONAL} does not affect the setting of
3188 this warning option.
3190 @item /WARNINGS=NOIMPLICIT_DEREFERENCE (suppress warnings on implicit dereferencing)
3191 @cindex @option{/WARNINGS=NOIMPLICIT_DEREFERENCE} (@code{GNAT COMPILE})
3192 @cindex Implicit dereferencing
3193 @cindex Dereferencing, implicit
3194 This qualifier suppresses warnings for implicit deferences in
3195 indexed components, slices, and selected components.
3197 @item /WARNINGS=ERROR (treat warnings as errors)
3198 @cindex @option{/WARNINGS=ERROR} (@code{GNAT COMPILE})
3199 @cindex Warnings, treat as error
3200 This qualifier causes warning messages to be treated as errors.
3201 The warning string still appears, but the warning messages are counted
3202 as errors, and prevent the generation of an object file.
3204 @item /WARNINGS=UNREFERENCED_FORMALS (activate warnings on unreferenced formals)
3205 @cindex @option{/WARNINGS=UNREFERENCED_FORMALS} (@code{GNAT COMPILE})
3206 @cindex Formals, unreferenced
3207 This qualifier causes a warning to be generated if a formal parameter
3208 is not referenced in the body of the subprogram. This warning can
3209 also be turned on using @option{/WARNINGS=OPTIONAL} or @option{/WARNINGS=UNUSED}.
3211 @item /WARNINGS=NOUNREFERENCED_FORMALS (suppress warnings on unreferenced formals)
3212 @cindex @option{/WARNINGS=NOUNREFERENCED_FORMALS} (@code{GNAT COMPILE})
3213 This qualifier suppresses warnings for unreferenced formal
3214 parameters. Note that the
3215 combination @option{/WARNINGS=UNUSED} followed by @option{/WARNINGS=NOUNREFERENCED_FORMALS} has the
3216 effect of warning on unreferenced entities other than subprogram
3217 formals.
3219 @item /WARNINGS=HIDING (activate warnings on hiding)
3220 @cindex @option{/WARNINGS=HIDING} (@code{GNAT COMPILE})
3221 @cindex Hiding of Declarations
3222 This qualifier activates warnings on hiding declarations.
3223 A declaration is considered hiding
3224 if it is for a non-overloadable entity, and it declares an entity with the
3225 same name as some other entity that is directly or use-visible. The default
3226 is that such warnings are not generated.
3227 Note that @option{/WARNINGS=OPTIONAL} does not affect the setting of this warning option.
3229 @item /WARNINGS=NOHIDING (suppress warnings on hiding)
3230 @cindex @option{/WARNINGS=NOHIDING} (@code{GNAT COMPILE})
3231 This qualifier suppresses warnings on hiding declarations.
3233 @item /WARNINGS=IMPLEMENTATION (activate warnings on implementation units).
3234 @cindex @option{/WARNINGS=IMPLEMENTATION} (@code{GNAT COMPILE})
3235 This qualifier activates warnings for a @code{with} of an internal GNAT
3236 implementation unit, defined as any unit from the @code{Ada},
3237 @code{Interfaces}, @code{GNAT},
3238 @code{DEC}, or @code{System}
3239 hierarchies that is not
3240 documented in either the Ada Reference Manual or the GNAT
3241 Programmer's Reference Manual. Such units are intended only
3242 for internal implementation purposes and should not be @code{with}'ed
3243 by user programs. The default is that such warnings are generated
3244 This warning can also be turned on using @option{/WARNINGS=OPTIONAL}.
3246 @item /WARNINGS=NOIMPLEMENTATION (disable warnings on implementation units).
3247 @cindex @option{/WARNINGS=NOIMPLEMENTATION} (@code{GNAT COMPILE})
3248 This qualifier disables warnings for a @code{with} of an internal GNAT
3249 implementation unit.
3251 @item /WARNINGS=ELABORATION (activate warnings on elaboration pragmas)
3252 @cindex @option{/WARNINGS=ELABORATION} (@code{GNAT COMPILE})
3253 @cindex Elaboration, warnings
3254 This qualifier activates warnings on missing pragma Elaborate_All statements.
3255 See the section in this guide on elaboration checking for details on
3256 when such pragma should be used. The default is that such warnings
3257 are not generated.
3258 This warning can also be turned on using @option{/WARNINGS=OPTIONAL}.
3260 @item /WARNINGS=NOELABORATION (suppress warnings on elaboration pragmas)
3261 @cindex @option{/WARNINGS=NOELABORATION} (@code{GNAT COMPILE})
3262 This qualifier suppresses 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.
3266 @item /WARNINGS=OVERLAYS (activate warnings on address clause overlays)
3267 @cindex @option{/WARNINGS=OVERLAYS} (@code{GNAT COMPILE})
3268 @cindex Address Clauses, warnings
3269 This qualifier activates warnings for possibly unintended initialization
3270 effects of defining address clauses that cause one variable to overlap
3271 another. The default is that such warnings are generated.
3272 This warning can also be turned on using @option{/WARNINGS=OPTIONAL}.
3274 @item /WARNINGS=NOOVERLAYS (suppress warnings on address clause overlays)
3275 @cindex @option{/WARNINGS=NOOVERLAYS} (@code{GNAT COMPILE})
3276 This qualifier suppresses warnings on possibly unintended initialization
3277 effects of defining address clauses that cause one variable to overlap
3278 another.
3280 @item -gnatwp (activate warnings on ineffective pragma Inlines)
3281 @cindex @option{-gnatwp} (@code{GNAT COMPILE})
3282 @cindex Inlining, warnings
3283 This qualifier activates warnings for failure of front end inlining
3284 (activated by @option{-gnatN}) to inline a particular call. There are
3285 many reasons for not being able to inline a call, including most
3286 commonly that the call is too complex to inline.
3287 This warning can also be turned on using @option{/WARNINGS=OPTIONAL}.
3289 @item -gnatwP (suppress warnings on ineffective pragma Inlines)
3290 @cindex @option{-gnatwP} (@code{GNAT COMPILE})
3291 This qualifier suppresses warnings on ineffective pragma Inlines. If the
3292 inlining mechanism cannot inline a call, it will simply ignore the
3293 request silently.
3295 @item /WARNINGS=REDUNDANT (activate warnings on redundant constructs)
3296 @cindex @option{/WARNINGS=REDUNDANT} (@code{GNAT COMPILE})
3297 This qualifier activates warnings for redundant constructs. The following
3298 is the current list of constructs regarded as redundant:
3299 This warning can also be turned on using @option{/WARNINGS=OPTIONAL}.
3301 @itemize @bullet
3302 @item
3303 Assignment of an item to itself.
3304 @item
3305 Type conversion that converts an expression to its own type.
3306 @item
3307 Use of the attribute @code{Base} where @code{typ'Base} is the same
3308 as @code{typ}.
3309 @item
3310 Use of pragma @code{Pack} when all components are placed by a record
3311 representation clause.
3312 @end itemize
3314 @item /WARNINGS=NOREDUNDANT (suppress warnings on redundant constructs)
3315 @cindex @option{/WARNINGS=NOREDUNDANT} (@code{GNAT COMPILE})
3316 This qualifier suppresses warnings for redundant constructs.
3318 @item /WARNINGS=SUPPRESS (suppress all warnings)
3319 @cindex @option{/WARNINGS=SUPPRESS} (@code{GNAT COMPILE})
3320 This qualifier completely suppresses the
3321 output of all warning messages from the GNAT front end.
3322 Note that it does not suppress warnings from the @code{GNAT COMPILE} back end.
3323 To suppress these back end warnings as well, use the qualifier @code{-w}
3324 in addition to @option{/WARNINGS=SUPPRESS}.
3326 @item /WARNINGS=UNUSED (activate warnings on unused entities)
3327 @cindex @option{/WARNINGS=UNUSED} (@code{GNAT COMPILE})
3328 This qualifier activates warnings to be generated for entities that
3329 are defined but not referenced, and for units that are @code{with}'ed
3330 and not
3331 referenced. In the case of packages, a warning is also generated if
3332 no entities in the package are referenced. This means that if the package
3333 is referenced but the only references are in @code{use}
3334 clauses or @code{renames}
3335 declarations, a warning is still generated. A warning is also generated
3336 for a generic package that is @code{with}'ed but never instantiated.
3337 In the case where a package or subprogram body is compiled, and there
3338 is a @code{with} on the corresponding spec
3339 that is only referenced in the body,
3340 a warning is also generated, noting that the
3341 @code{with} can be moved to the body. The default is that
3342 such warnings are not generated.
3343 This qualifier also activates warnings on unreferenced formals
3344 (it is includes the effect of @option{/WARNINGS=UNREFERENCED_FORMALS}).
3345 This warning can also be turned on using @option{/WARNINGS=OPTIONAL}.
3347 @item /WARNINGS=NOUNUSED (suppress warnings on unused entities)
3348 @cindex @option{/WARNINGS=NOUNUSED} (@code{GNAT COMPILE})
3349 This qualifier suppresses warnings for unused entities and packages.
3350 It also turns off warnings on unreferenced formals (and thus includes
3351 the effect of @option{/WARNINGS=NOUNREFERENCED_FORMALS}).
3353 @noindent
3354 A string of warning parameters can be used in the same parameter. For example:
3356 @smallexample
3357 -gnatwaLe
3358 @end smallexample
3360 @noindent
3361 Would turn on all optional warnings except for elaboration pragma warnings,
3362 and also specify that warnings should be treated as errors.
3364 @item -w
3365 @cindex @code{-w}
3366 This qualifier suppresses warnings from the @code{GNAT COMPILE} backend. It may be
3367 used in conjunction with @option{/WARNINGS=SUPPRESS} to ensure that all warnings
3368 are suppressed during the entire compilation process.
3370 @end table
3372 @node Debugging and Assertion Control
3373 @subsection Debugging and Assertion Control
3375 @table @code
3376 @item /CHECKS=ASSERTIONS
3377 @cindex @option{/CHECKS=ASSERTIONS} (@code{GNAT COMPILE})
3378 @findex Assert
3379 @findex Debug
3380 @cindex Assertions
3382 @noindent
3383 The pragmas @code{Assert} and @code{Debug} normally have no effect and
3384 are ignored. This qualifier, where @samp{a} stands for assert, causes
3385 @code{Assert} and @code{Debug} pragmas to be activated.
3387 The pragmas have the form:
3389 @smallexample
3390 @group
3391 @cartouche
3392    @b{pragma} Assert (@var{Boolean-expression} [,
3393                       @var{static-string-expression}])
3394    @b{pragma} Debug (@var{procedure call})
3395 @end cartouche
3396 @end group
3397 @end smallexample
3399 @noindent
3400 The @code{Assert} pragma causes @var{Boolean-expression} to be tested.
3401 If the result is @code{True}, the pragma has no effect (other than
3402 possible side effects from evaluating the expression). If the result is
3403 @code{False}, the exception @code{Assert_Failure} declared in the package
3404 @code{System.Assertions} is
3405 raised (passing @var{static-string-expression}, if present, as the
3406 message associated with the exception). If no string expression is
3407 given the default is a string giving the file name and line number
3408 of the pragma.
3410 The @code{Debug} pragma causes @var{procedure} to be called. Note that
3411 @code{pragma Debug} may appear within a declaration sequence, allowing
3412 debugging procedures to be called between declarations.
3414 @item /DEBUG[=debug-level]
3415 @itemx  /NODEBUG
3416 Specifies how much debugging information is to be included in
3417 the resulting object file where 'debug-level' is one of the following:
3418 @table @code
3419 @item   TRACEBACK (default)
3420 Include both debugger symbol records and traceback
3421 the object file.
3422 @item   ALL
3423 Include both debugger symbol records and traceback in
3424 object file.
3425 @item   NONE
3426 Excludes both debugger symbol records and traceback
3427 the object file. Same as /NODEBUG.
3428 @item   SYMBOLS
3429 Includes only debugger symbol records in the object
3430 file. Note that this doesn't include traceback information.
3431 @end table
3432 @end table
3434 @node Validity Checking
3435 @subsection Validity Checking
3436 @findex Validity Checking
3438 @noindent
3439 The Ada 95 Reference Manual has specific requirements for checking
3440 for invalid values. In particular, RM 13.9.1 requires that the
3441 evaluation of invalid values (for example from unchecked conversions),
3442 not result in erroneous execution. In GNAT, the result of such an
3443 evaluation in normal default mode is to either use the value
3444 unmodified, or to raise Constraint_Error in those cases where use
3445 of the unmodified value would cause erroneous execution. The cases
3446 where unmodified values might lead to erroneous execution are case
3447 statements (where a wild jump might result from an invalid value),
3448 and subscripts on the left hand side (where memory corruption could
3449 occur as a result of an invalid value).
3451 The @option{-gnatVx} qualifier allows more control over the validity checking
3452 mode. The @code{x} argument here is a string of letters which control which
3453 validity checks are performed in addition to the default checks described
3454 above.
3456 @itemize @bullet
3457 @item
3458 @option{-gnatVc} Validity checks for copies
3460 The right hand side of assignments, and the initializing values of
3461 object declarations are validity checked.
3463 @item
3464 @option{/VALIDITY_CHECKING=RM} Default (RM) validity checks
3466 Some validity checks are done by default following normal Ada semantics
3467 (RM 13.9.1 (9-11)).
3468 A check is done in case statements that the expression is within the range
3469 of the subtype. If it is not, Constraint_Error is raised.
3470 For assignments to array components, a check is done that the expression used
3471 as index is within the range. If it is not, Constraint_Error is raised.
3472 Both these validity checks may be turned off using qualifier @option{-gnatVD}.
3473 They are turned on by default. If @option{-gnatVD} is specified, a subsequent
3474 qualifier @option{/VALIDITY_CHECKING=RM} will leave the checks turned on.
3475 Qualifier @option{-gnatVD} should be used only if you are sure that all such
3476 expressions have valid values. If you use this qualifier and invalid values
3477 are present, then the program is erroneous, and wild jumps or memory
3478 overwriting may occur.
3480 @item
3481 @option{-gnatVi} Validity checks for @code{in} mode parameters
3483 Arguments for parameters of mode @code{in} are validity checked in function
3484 and procedure calls at the point of call.
3486 @item
3487 @option{-gnatVm} Validity checks for @code{in out} mode parameters
3489 Arguments for parameters of mode @code{in out} are validity checked in
3490 procedure calls at the point of call. The @code{'m'} here stands for
3491 modify, since this concerns parameters that can be modified by the call.
3492 Note that there is no specific option to test @code{out} parameters,
3493 but any reference within the subprogram will be tested in the usual
3494 manner, and if an invalid value is copied back, any reference to it
3495 will be subject to validity checking.
3497 @item
3498 @option{-gnatVo} Validity checks for operator and attribute operands
3500 Arguments for predefined operators and attributes are validity checked.
3501 This includes all operators in package @code{Standard},
3502 the shift operators defined as intrinsic in package @code{Interfaces}
3503 and operands for attributes such as @code{Pos}.
3505 @item
3506 @option{-gnatVr} Validity checks for function returns
3508 The expression in @code{return} statements in functions is validity
3509 checked.
3511 @item
3512 @option{-gnatVs} Validity checks for subscripts
3514 All subscripts expressions are checked for validity, whether they appear
3515 on the right side or left side (in default mode only left side subscripts
3516 are validity checked).
3518 @item
3519 @option{-gnatVt} Validity checks for tests
3521 Expressions used as conditions in @code{if}, @code{while} or @code{exit}
3522 statements are checked, as well as guard expressions in entry calls.
3524 @item
3525 @option{/VALIDITY_CHECKING=FULL} Validity checks for floating-point values
3527 In the absence of this qualifier, validity checking occurs only for discrete
3528 values. If @option{/VALIDITY_CHECKING=FULL} is specified, then validity checking also applies
3529 for floating-point values, and NaN's and infinities are considered invalid,
3530 as well as out of range values for constrained types. Note that this means
3531 that standard @code{IEEE} infinity mode is not allowed. The exact contexts
3532 in which floating-point values are checked depends on the setting of other
3533 options. For example @option{-gnatVif} or @option{-gnatVfi} (the order does
3534 not matter) specifies that floating-point parameters of mode @code{in} should
3535 be validity checked.
3537 @item
3538 @option{-gnatVa} All validity checks
3540 All the above validity checks are turned on. That is @option{-gnatVa} is
3541 equivalent to @code{gnatVcdfimorst}.
3543 @item
3544 @option{-gnatVn} No validity checks
3546 This qualifier turns off all validity checking, including the default checking
3547 for case statements and left hand side subscripts. Note that the use of
3548 the qualifier @option{/CHECKS=SUPPRESS_ALL} supresses all run-time checks, including
3549 validity checks, and thus implies @option{-gnatVn}.
3551 @end itemize
3553 The @option{/VALIDITY_CHECKING} qualifier may be followed by a string of letters to turn on
3554 a series of validity checking options. For example, @option{-gnatVcr} specifies
3555 that in addition to the default validity checking, copies and function
3556 return expressions be validity checked. In order to make it easier to specify
3557 a set of options, the upper case letters @code{CDFIMORST} may be used to turn
3558 off the corresponding lower case option, so for example @option{-gnatVaM} turns
3559 on all validity checking options except for checking of @code{in out}
3560 procedure arguments.
3562 The specification of additional validity checking generates extra code (and
3563 in the case of @option{-gnatva} the code expansion can be substantial. However,
3564 these additional checks can be very useful in smoking out cases of
3565 uninitialized variables, incorrect use of unchecked conversion, and other
3566 errors leading to invalid values. The use of pragma @code{Initialize_Scalars}
3567 is useful in conjunction with the extra validity checking, since this
3568 ensures that wherever possible uninitialized variables have invalid values.
3570 See also the pragma @code{Validity_Checks} which allows modification of
3571 the validity checking mode at the program source level, and also allows for
3572 temporary disabling of validity checks.
3574 @node Style Checking
3575 @subsection Style Checking
3576 @findex Style checking
3578 @noindent
3579 The /STYLE=(@var{option},@var{option},..) qualifier causes the compiler to
3580 enforce specified style rules. A limited set of style rules has been used
3581 in writing the GNAT sources themselves. This qualifier allows user programs
3582 to activate all or some of these checks. If the source program fails a
3583 specified style check, an appropriate warning message is given, preceded by
3584 the character sequence "(style)".
3585 (OPTION,OPTION,..) is a sequence of keywords
3586 indicating the particular style
3587 checks to be performed. The following checks are defined:
3589 @table @code
3590 @item 1-9 (specify indentation level)
3591 If a digit from 1-9 appears in the string after @option{/STYLE=} then proper
3592 indentation is checked, with the digit indicating the indentation level
3593 required. The general style of required indentation is as specified by
3594 the examples in the Ada Reference Manual. Full line comments must be
3595 aligned with the @code{--} starting on a column that is a multiple of
3596 the alignment level.
3598 @item ATTRIBUTE (check attribute casing)
3599 If the word ATTRIBUTE appears in the string after @option{/STYLE=} then
3600 attribute names, including the case of keywords such as @code{digits}
3601 used as attributes names, must be written in mixed case, that is, the
3602 initial letter and any letter following an underscore must be uppercase.
3603 All other letters must be lowercase.
3605 @item BLANKS (blanks not allowed at statement end)
3606 If the word BLANKS appears in the string after @option{/STYLE=} then
3607 trailing blanks are not allowed at the end of statements. The purpose of this
3608 rule, together with h (no horizontal tabs), is to enforce a canonical format
3609 for the use of blanks to separate source tokens.
3611 @item COMMENTS (check comments)
3612 If the word COMMENTS appears in the string after @option{/STYLE=} then
3613 comments must meet the following set of rules:
3615 @itemize @bullet
3617 @item
3618 The "--" that starts the column must either start in column one, or else
3619 at least one blank must precede this sequence.
3621 @item
3622 Comments that follow other tokens on a line must have at least one blank
3623 following the "--" at the start of the comment.
3625 @item
3626 Full line comments must have two blanks following the "--" that starts
3627 the comment, with the following exceptions.
3629 @item
3630 A line consisting only of the "--" characters, possibly preceded by blanks
3631 is permitted.
3633 @item
3634 A comment starting with "--x" where x is a special character is permitted.
3635 This alows proper processing of the output generated by specialized tools
3636 including @code{GNAT PREPROCESS} (where --! is used) and the SPARK annnotation
3637 language (where --# is used). For the purposes of this rule, a special
3638 character is defined as being in one of the ASCII ranges
3639 16#21#..16#2F# or 16#3A#..16#3F#.
3641 @item
3642 A line consisting entirely of minus signs, possibly preceded by blanks, is
3643 permitted. This allows the construction of box comments where lines of minus
3644 signs are used to form the top and bottom of the box.
3646 @item
3647 If a comment starts and ends with "--" is permitted as long as at least
3648 one blank follows the initial "--". Together with the preceding rule,
3649 this allows the construction of box comments, as shown in the following
3650 example:
3651 @smallexample
3652 ---------------------------
3653 -- This is a box comment --
3654 -- with two text lines.  --
3655 ---------------------------
3656 @end smallexample
3657 @end itemize
3659 @item END (check end/exit labels)
3660 If the word END appears in the string after @option{/STYLE=} then
3661 optional labels on @code{end} statements ending subprograms and on
3662 @code{exit} statements exiting named loops, are required to be present.
3664 @item VTABS (no form feeds or vertical tabs)
3665 If the word VTABS appears in the string after @option{/STYLE=} then
3666 neither form feeds nor vertical tab characters are not permitted
3667 in the source text.
3669 @item HTABS (no horizontal tabs)
3670 If the word HTABS appears in the string after @option{/STYLE=} then
3671 horizontal tab characters are not permitted in the source text.
3672 Together with the b (no blanks at end of line) check, this
3673 enforces a canonical form for the use of blanks to separate
3674 source tokens.
3676 @item IF_THEN (check if-then layout)
3677 If the word IF_THEN appears in the string after @option{/STYLE=},
3678 then the keyword @code{then} must appear either on the same
3679 line as corresponding @code{if}, or on a line on its own, lined
3680 up under the @code{if} with at least one non-blank line in between
3681 containing all or part of the condition to be tested.
3683 @item KEYWORD (check keyword casing)
3684 If the word KEYWORD appears in the string after @option{/STYLE=} then
3685 all keywords must be in lower case (with the exception of keywords
3686 such as @code{digits} used as attribute names to which this check
3687 does not apply).
3689 @item LAYOUT (check layout)
3690 If the word LAYOUT appears in the string after @option{/STYLE=} then
3691 layout of statement and declaration constructs must follow the
3692 recommendations in the Ada Reference Manual, as indicated by the
3693 form of the syntax rules. For example an @code{else} keyword must
3694 be lined up with the corresponding @code{if} keyword.
3696 There are two respects in which the style rule enforced by this check
3697 option are more liberal than those in the Ada Reference Manual. First
3698 in the case of record declarations, it is permissible to put the
3699 @code{record} keyword on the same line as the @code{type} keyword, and
3700 then the @code{end} in @code{end record} must line up under @code{type}.
3701 For example, either of the following two layouts is acceptable:
3703 @smallexample
3704 @group
3705 @cartouche
3706 @b{type} q @b{is record}
3707    a : integer;
3708    b : integer;
3709 @b{end record};
3711 @b{type} q @b{is}
3712    @b{record}
3713       a : integer;
3714       b : integer;
3715    @b{end record};
3716 @end cartouche
3717 @end group
3718 @end smallexample
3720 @noindent
3721 Second, in the case of a block statement, a permitted alternative
3722 is to put the block label on the same line as the @code{declare} or
3723 @code{begin} keyword, and then line the @code{end} keyword up under
3724 the block label. For example both the following are permitted:
3726 @smallexample
3727 @group
3728 @cartouche
3729 Block : @b{declare}
3730    A : Integer := 3;
3731 @b{begin}
3732    Proc (A, A);
3733 @b{end} Block;
3735 Block :
3736    @b{declare}
3737       A : Integer := 3;
3738    @b{begin}
3739       Proc (A, A);
3740    @b{end} Block;
3741 @end cartouche
3742 @end group
3743 @end smallexample
3745 @noindent
3746 The same alternative format is allowed for loops. For example, both of
3747 the following are permitted:
3749 @smallexample
3750 @group
3751 @cartouche
3752 Clear : @b{while} J < 10 @b{loop}
3753    A (J) := 0;
3754 @b{end loop} Clear;
3756 Clear :
3757    @b{while} J < 10 @b{loop}
3758       A (J) := 0;
3759    @b{end loop} Clear;
3760 @end cartouche
3761 @end group
3762 @end smallexample
3764 @item LINE_LENGTH (check maximum line length)
3765 If the word LINE_LENGTH appears in the string after @option{/STYLE=}
3766 then the length of source lines must not exceed 79 characters, including
3767 any trailing blanks. The value of 79 allows convenient display on an
3768 80 character wide device or window, allowing for possible special
3769 treatment of 80 character lines.
3771 @item MAX_LENGTH=nnn (set maximum line length)
3772 If the sequence MAX_LENGTH=nnn, where nnn is a decimal number, appears in
3773 the string after @option{/STYLE=} then the length of lines must not exceed the
3774 given value.
3776 @item STANDARD_CASING (check casing of entities in Standard)
3777 If the word STANDARD_CASING appears in the string
3778 after @option{/STYLE=} then any identifier from Standard must be cased
3779 to match the presentation in the Ada Reference Manual (for example,
3780 @code{Integer} and @code{ASCII.NUL}).
3782 @item ORDERED_SUBPROGRAMS (check order of subprogram bodies)
3783 If the word ORDERED_SUBPROGRAMS appears in the string
3784 after @option{/STYLE=} then all subprogram bodies in a given scope
3785 (e.g. a package body) must be in alphabetical order. The ordering
3786 rule uses normal Ada rules for comparing strings, ignoring casing
3787 of letters, except that if there is a trailing numeric suffix, then
3788 the value of this suffix is used in the ordering (e.g. Junk2 comes
3789 before Junk10).
3791 @item PRAGMA (check pragma casing)
3792 If the word PRAGMA appears in the string after @option{/STYLE=} then
3793 pragma names must be written in mixed case, that is, the
3794 initial letter and any letter following an underscore must be uppercase.
3795 All other letters must be lowercase.
3797 @item REFERENCES (check references)
3798 If the word REFERENCES appears in the string after @option{/STYLE=}
3799 then all identifier references must be cased in the same way as the
3800 corresponding declaration. No specific casing style is imposed on
3801 identifiers. The only requirement is for consistency of references
3802 with declarations.
3804 @item SPECS (check separate specs)
3805 If the word SPECS appears in the string after @option{/STYLE=} then
3806 separate declarations ("specs") are required for subprograms (a
3807 body is not allowed to serve as its own declaration). The only
3808 exception is that parameterless library level procedures are
3809 not required to have a separate declaration. This exception covers
3810 the most frequent form of main program procedures.
3812 @item TOKEN (check token spacing)
3813 If the word TOKEN appears in the string after @option{/STYLE=} then
3814 the following token spacing rules are enforced:
3816 @itemize @bullet
3818 @item
3819 The keywords @code{abs} and @code{not} must be followed by a space.
3821 @item
3822 The token @code{=>} must be surrounded by spaces.
3824 @item
3825 The token @code{<>} must be preceded by a space or a left parenthesis.
3827 @item
3828 Binary operators other than @code{**} must be surrounded by spaces.
3829 There is no restriction on the layout of the @code{**} binary operator.
3831 @item
3832 Colon must be surrounded by spaces.
3834 @item
3835 Colon-equal (assignment) must be surrounded by spaces.
3837 @item
3838 Comma must be the first non-blank character on the line, or be
3839 immediately preceded by a non-blank character, and must be followed
3840 by a space.
3842 @item
3843 If the token preceding a left paren ends with a letter or digit, then
3844 a space must separate the two tokens.
3846 @item
3847 A right parenthesis must either be the first non-blank character on
3848 a line, or it must be preceded by a non-blank character.
3850 @item
3851 A semicolon must not be preceded by a space, and must not be followed by
3852 a non-blank character.
3854 @item
3855 A unary plus or minus may not be followed by a space.
3857 @item
3858 A vertical bar must be surrounded by spaces.
3859 @end itemize
3861 @noindent
3862 In the above rules, appearing in column one is always permitted, that is,
3863 counts as meeting either a requirement for a required preceding space,
3864 or as meeting a requirement for no preceding space.
3866 Appearing at the end of a line is also always permitted, that is, counts
3867 as meeting either a requirement for a following space, or as meeting
3868 a requirement for no following space.
3870 @end table
3872 @noindent
3873 If any of these style rules is violated, a message is generated giving
3874 details on the violation. The initial characters of such messages are
3875 always "(style)". Note that these messages are treated as warning
3876 messages, so they normally do not prevent the generation of an object
3877 file. The @option{/WARNINGS=ERROR} qualifier can be used to treat warning messages,
3878 including style messages, as fatal errors.
3880 @noindent
3881 The qualifier
3882 /STYLE_CHECKS=ALL_BUILTIN
3883 is equivalent to  all checking
3884 options  enabled with
3885 the exception of ORDERED_SUBPROGRAMS,
3886 with an indentation level of 3. This is the standard
3887 checking option that is used for the GNAT sources.
3889 @node Run-Time Checks
3890 @subsection Run-Time Checks
3891 @cindex Division by zero
3892 @cindex Access before elaboration
3893 @cindex Checks, division by zero
3894 @cindex Checks, access before elaboration
3896 @noindent
3897 If you compile with the default options, GNAT will insert many run-time
3898 checks into the compiled code, including code that performs range
3899 checking against constraints, but not arithmetic overflow checking for
3900 integer operations (including division by zero) or checks for access
3901 before elaboration on subprogram calls. All other run-time checks, as
3902 required by the Ada 95 Reference Manual, are generated by default.
3903 The following @code{GNAT COMPILE} qualifiers refine this default behavior:
3905 @table @code
3906 @item /CHECKS=SUPPRESS_ALL
3907 @cindex @option{/CHECKS=SUPPRESS_ALL} (@code{GNAT COMPILE})
3908 @cindex Suppressing checks
3909 @cindex Checks, suppressing
3910 @findex Suppress
3911 Suppress all run-time checks as though @code{pragma Suppress (all_checks})
3912 had been present in the source. Validity checks are also suppressed (in
3913 other words @option{/CHECKS=SUPPRESS_ALL} also implies @option{-gnatVn}.
3914 Use this qualifier to improve the performance
3915 of the code at the expense of safety in the presence of invalid data or
3916 program bugs.
3918 @item /CHECKS=OVERFLOW
3919 @cindex @option{/CHECKS=OVERFLOW} (@code{GNAT COMPILE})
3920 @cindex Overflow checks
3921 @cindex Check, overflow
3922 Enables overflow checking for integer operations.
3923 This causes GNAT to generate slower and larger executable
3924 programs by adding code to check for overflow (resulting in raising
3925 @code{Constraint_Error} as required by standard Ada
3926 semantics). These overflow checks correspond to situations in which
3927 the true value of the result of an operation may be outside the base
3928 range of the result type. The following example shows the distinction:
3930 @smallexample
3931 X1 : Integer := Integer'Last;
3932 X2 : Integer range 1 .. 5 := 5;
3934 X1 := X1 + 1;   -- @option{/CHECKS=OVERFLOW} required to catch the Constraint_Error
3935 X2 := X2 + 1;   -- range check, @option{/CHECKS=OVERFLOW} has no effect here
3936 @end smallexample
3938 @noindent
3939 Here the first addition results in a value that is outside the base range
3940 of Integer, and hence requires an overflow check for detection of the
3941 constraint error. The second increment operation results in a violation
3942 of the explicit range constraint, and such range checks are always
3943 performed. Basically the compiler can assume that in the absence of
3944 the @option{/CHECKS=OVERFLOW} qualifier that any value of type @code{xxx} is
3945 in range of the base type of @code{xxx}.
3947 @findex Machine_Overflows
3948 Note that the @option{/CHECKS=OVERFLOW} qualifier does not affect the code generated
3949 for any floating-point operations; it applies only to integer
3950 semantics).
3951 For floating-point, GNAT has the @code{Machine_Overflows}
3952 attribute set to @code{False} and the normal mode of operation is to
3953 generate IEEE NaN and infinite values on overflow or invalid operations
3954 (such as dividing 0.0 by 0.0).
3956 The reason that we distinguish overflow checking from other kinds of
3957 range constraint checking is that a failure of an overflow check can
3958 generate an incorrect value, but cannot cause erroneous behavior. This
3959 is unlike the situation with a constraint check on an array subscript,
3960 where failure to perform the check can result in random memory description,
3961 or the range check on a case statement, where failure to perform the check
3962 can cause a wild jump.
3964 Note again that @option{/CHECKS=OVERFLOW} is off by default, so overflow checking is
3965 not performed in default mode. This means that out of the box, with the
3966 default settings, GNAT does not do all the checks expected from the
3967 language description in the Ada Reference Manual. If you want all constraint
3968 checks to be performed, as described in this Manual, then you must
3969 explicitly use the /CHECKS=OVERFLOW qualifier either on the @code{GNAT MAKE} or
3970 @code{GNAT COMPILE} command.
3972 @item /CHECKS=ELABORATION
3973 @cindex @option{/CHECKS=ELABORATION} (@code{GNAT COMPILE})
3974 @cindex Elaboration checks
3975 @cindex Check, elaboration
3976 Enables dynamic checks for access-before-elaboration
3977 on subprogram calls and generic instantiations.
3978 For full details of the effect and use of this qualifier,
3979 @xref{Compiling Using GNAT COMPILE}.
3980 @end table
3982 @findex Unsuppress
3983 @noindent
3984 The setting of these qualifiers only controls the default setting of the
3985 checks. You may modify them using either @code{Suppress} (to remove
3986 checks) or @code{Unsuppress} (to add back suppressed checks) pragmas in
3987 the program source.
3989 @node Stack Overflow Checking
3990 @subsection Stack Overflow Checking
3991 @cindex Stack Overflow Checking
3992 @cindex -fstack-check
3994 @noindent
3995 For most operating systems, @code{GNAT COMPILE} does not perform stack overflow
3996 checking by default. This means that if the main environment task or
3997 some other task exceeds the available stack space, then unpredictable
3998 behavior will occur.
4000 To activate stack checking, compile all units with the GNAT COMPILE option
4001 @code{-fstack-check}. For example:
4003 @smallexample
4004 GNAT COMPILE -fstack-check PACKAGE1.ADB
4005 @end smallexample
4007 @noindent
4008 Units compiled with this option will generate extra instructions to check
4009 that any use of the stack (for procedure calls or for declaring local
4010 variables in declare blocks) do not exceed the available stack space.
4011 If the space is exceeded, then a @code{Storage_Error} exception is raised.
4013 For declared tasks, the stack size is always controlled by the size
4014 given in an applicable @code{Storage_Size} pragma (or is set to
4015 the default size if no pragma is used.
4017 For the environment task, the stack size depends on
4018 system defaults and is unknown to the compiler. The stack
4019 may even dynamically grow on some systems, precluding the
4020 normal Ada semantics for stack overflow. In the worst case,
4021 unbounded stack usage, causes unbounded stack expansion
4022 resulting in the system running out of virtual memory.
4024 The stack checking may still work correctly if a fixed
4025 size stack is allocated, but this cannot be guaranteed.
4026 To ensure that a clean exception is signalled for stack
4027 overflow, set the environment variable
4028 @code{GNAT_STACK_LIMIT} to indicate the maximum
4029 stack area that can be used, as in:
4030 @cindex GNAT_STACK_LIMIT
4032 @smallexample
4033 SET GNAT_STACK_LIMIT 1600
4034 @end smallexample
4036 @noindent
4037 The limit is given in kilobytes, so the above declaration would
4038 set the stack limit of the environment task to 1.6 megabytes.
4039 Note that the only purpose of this usage is to limit the amount
4040 of stack used by the environment task. If it is necessary to
4041 increase the amount of stack for the environment task, then this
4042 is an operating systems issue, and must be addressed with the
4043 appropriate operating systems commands.
4045 @node Run-Time Control
4046 @subsection Run-Time Control
4048 @table @code
4049 @item -gnatT nnn
4050 @cindex @option{-gnatT} (@code{GNAT COMPILE})
4051 @cindex Time Slicing
4053 @noindent
4054 The @code{gnatT} qualifier can be used to specify the time-slicing value
4055 to be used for task switching between equal priority tasks. The value
4056 @code{nnn} is given in microseconds as a decimal integer.
4058 Setting the time-slicing value is only effective if the underlying thread
4059 control system can accommodate time slicing. Check the documentation of
4060 your operating system for details. Note that the time-slicing value can
4061 also be set by use of pragma @code{Time_Slice} or by use of the
4062 @code{t} qualifier in the GNAT BIND step. The pragma overrides a command
4063 line argument if both are present, and the @code{t} qualifier for GNAT BIND
4064 overrides both the pragma and the @code{GNAT COMPILE} command line qualifier.
4065 @end table
4067 @node Using GNAT COMPILE for Syntax Checking
4068 @subsection Using @code{GNAT COMPILE} for Syntax Checking
4069 @table @code
4070 @item /SYNTAX_ONLY
4071 @cindex @option{/SYNTAX_ONLY} (@code{GNAT COMPILE})
4073 Run GNAT in syntax checking only mode. For
4074 example, the command
4076 @smallexample
4077 $ GNAT COMPILE /SYNTAX_ONLY X.ADB
4078 @end smallexample
4080 @noindent
4081 compiles file @file{X.ADB} in syntax-check-only mode. You can check a
4082 series of files in a single command
4085 You may use other qualifiers in conjunction with @option{/SYNTAX_ONLY}. In
4086 particular, @option{/LIST} and @option{/REPORT_ERRORS=VERBOSE} are useful to control the
4087 format of any generated error messages.
4089 The output is simply the error messages, if any. No object file or ALI
4090 file is generated by a syntax-only compilation. Also, no units other
4091 than the one specified are accessed. For example, if a unit @code{X}
4092 @code{with}'s a unit @code{Y}, compiling unit @code{X} in syntax
4093 check only mode does not access the source file containing unit
4094 @code{Y}.
4096 @cindex Multiple units, syntax checking
4097 Normally, GNAT allows only a single unit in a source file. However, this
4098 restriction does not apply in syntax-check-only mode, and it is possible
4099 to check a file containing multiple compilation units concatenated
4100 together. This is primarily used by the @code{GNAT CHOP} utility
4101 (@pxref{Renaming Files Using GNAT CHOP}).
4102 @end table
4104 @node Using GNAT COMPILE for Semantic Checking
4105 @subsection Using @code{GNAT COMPILE} for Semantic Checking
4106 @table @code
4107 @item /NOLOAD
4108 @cindex @option{/NOLOAD} (@code{GNAT COMPILE})
4110 Causes the compiler to operate in semantic check mode,
4111 with full checking for all illegalities specified in the
4112 Ada 95 Reference Manual, but without generation of any object code
4113 (no object file is generated).
4115 Because dependent files must be accessed, you must follow the GNAT
4116 semantic restrictions on file structuring to operate in this mode:
4118 @itemize @bullet
4119 @item
4120 The needed source files must be accessible
4121 (@pxref{Search Paths and the Run-Time Library (RTL)}).
4123 @item
4124 Each file must contain only one compilation unit.
4126 @item
4127 The file name and unit name must match (@pxref{File Naming Rules}).
4128 @end itemize
4130 The output consists of error messages as appropriate. No object file is
4131 generated. An @file{ALI} file is generated for use in the context of
4132 cross-reference tools, but this file is marked as not being suitable
4133 for binding (since no object file is generated).
4134 The checking corresponds exactly to the notion of
4135 legality in the Ada 95 Reference Manual.
4137 Any unit can be compiled in semantics-checking-only mode, including
4138 units that would not normally be compiled (subunits,
4139 and specifications where a separate body is present).
4140 @end table
4142 @node Compiling Ada 83 Programs
4143 @subsection Compiling Ada 83 Programs
4144 @table @code
4145 @cindex Ada 83 compatibility
4146 @item /83
4147 @cindex @option{/83} (@code{GNAT COMPILE})
4148 @cindex ACVC, Ada 83 tests
4150 @noindent
4151 Although GNAT is primarily an Ada 95 compiler, it accepts this qualifier to
4152 specify that an Ada 83 program is to be compiled in Ada83 mode. If you specify
4153 this qualifier, GNAT rejects most Ada 95 extensions and applies Ada 83 semantics
4154 where this can be done easily.
4155 It is not possible to guarantee this qualifier does a perfect
4156 job; for example, some subtle tests, such as are
4157 found in earlier ACVC tests (that have been removed from the ACVC suite for Ada
4158 95), may not compile correctly. However, for most purposes, using
4159 this qualifier should help to ensure that programs that compile correctly
4160 under the @option{/83} qualifier can be ported easily to an Ada 83
4161 compiler. This is the main use of the qualifier.
4163 With few exceptions (most notably the need to use @code{<>} on
4164 @cindex Generic formal parameters
4165 unconstrained generic formal parameters, the use of the new Ada 95
4166 keywords, and the use of packages
4167 with optional bodies), it is not necessary to use the
4168 @option{/83} qualifier when compiling Ada 83 programs, because, with rare
4169 exceptions, Ada 95 is upwardly compatible with Ada 83. This
4170 means that a correct Ada 83 program is usually also a correct Ada 95
4171 program.
4173 @end table
4175 @node Character Set Control
4176 @subsection Character Set Control
4177 @table @code
4178 @item /IDENTIFIER_CHARACTER_SET=@var{c}
4179 @cindex @code{/IDENTIFIER_CHARACTER_SET} (@code{GNAT COMPILE})
4181 @noindent
4182 Normally GNAT recognizes the Latin-1 character set in source program
4183 identifiers, as described in the Ada 95 Reference Manual.
4184 This qualifier causes
4185 GNAT to recognize alternate character sets in identifiers. @var{c} is a
4186 single character or word indicating the character set, as follows:
4188 @table @code
4189 @item 1
4190 Latin-1 identifiers
4192 @item 2
4193 Latin-2 letters allowed in identifiers
4195 @item 3
4196 Latin-3 letters allowed in identifiers
4198 @item 4
4199 Latin-4 letters allowed in identifiers
4201 @item 5
4202 Latin-5 (Cyrillic) letters allowed in identifiers
4204 @item 9
4205 Latin-9 letters allowed in identifiers
4207 @item PC
4208 IBM PC letters (code page 437) allowed in identifiers
4210 @item PC850
4211 IBM PC letters (code page 850) allowed in identifiers
4213 @item FULL_UPPER
4214 Full upper-half codes allowed in identifiers
4216 @item NO_UPPER
4217 No upper-half codes allowed in identifiers
4219 @item WIDE
4220 Wide-character codes (that is, codes greater than 255)
4221 allowed in identifiers
4222 @end table
4224 @xref{Foreign Language Representation}, for full details on the
4225 implementation of these character sets.
4227 @item /WIDE_CHARACTER_ENCODING=@var{e}
4228 @cindex @code{/WIDE_CHARACTER_ENCODING} (@code{GNAT COMPILE})
4229 Specify the method of encoding for wide characters.
4230 @var{e} is one of the following:
4232 @table @code
4234 @item HEX
4235 Hex encoding (brackets coding also recognized)
4237 @item UPPER
4238 Upper half encoding (brackets encoding also recognized)
4240 @item SHIFT_JIS
4241 Shift/JIS encoding (brackets encoding also recognized)
4243 @item EUC
4244 EUC encoding (brackets encoding also recognized)
4246 @item UTF8
4247 UTF-8 encoding (brackets encoding also recognized)
4249 @item BRACKETS
4250 Brackets encoding only (default value)
4251 @end table
4252 For full details on the these encoding
4253 methods see @xref{Wide Character Encodings}.
4254 Note that brackets coding is always accepted, even if one of the other
4255 options is specified, so for example @option{/WIDE_CHARACTER_ENCODING=UTF8} specifies that both
4256 brackets and @code{UTF-8} encodings will be recognized. The units that are
4257 with'ed directly or indirectly will be scanned using the specified
4258 representation scheme, and so if one of the non-brackets scheme is
4259 used, it must be used consistently throughout the program. However,
4260 since brackets encoding is always recognized, it may be conveniently
4261 used in standard libraries, allowing these libraries to be used with
4262 any of the available coding schemes.
4263 scheme. If no @option{/WIDE_CHARACTER_ENCODING=?} parameter is present, then the default
4264 representation is Brackets encoding only.
4266 Note that the wide character representation that is specified (explicitly
4267 or by default) for the main program also acts as the default encoding used
4268 for Wide_Text_IO files if not specifically overridden by a WCEM form
4269 parameter.
4271 @end table
4272 @node File Naming Control
4273 @subsection File Naming Control
4275 @table @code
4276 @item /FILE_NAME_MAX_LENGTH=@var{n}
4277 @cindex @option{/FILE_NAME_MAX_LENGTH} (@code{GNAT COMPILE})
4278 Activates file name "krunching". @var{n}, a decimal integer in the range
4279 1-999, indicates the maximum allowable length of a file name (not
4280 including the @file{.ADS} or @file{.ADB} extension). The default is not
4281 to enable file name krunching.
4283 For the source file naming rules, @xref{File Naming Rules}.
4284 @end table
4286 @node Subprogram Inlining Control
4287 @subsection Subprogram Inlining Control
4289 @table @code
4290 @item /INLINE=PRAGMA
4291 @cindex @option{/INLINE=PRAGMA} (@code{GNAT COMPILE})
4292 GNAT recognizes and processes @code{Inline} pragmas. However, for the
4293 inlining to actually occur, optimization must be enabled. To enable
4294 inlining across unit boundaries, this is, inlining a call in one unit of
4295 a subprogram declared in a @code{with}'ed unit, you must also specify
4296 this qualifier.
4297 In the absence of this qualifier, GNAT does not attempt
4298 inlining across units and does not need to access the bodies of
4299 subprograms for which @code{pragma Inline} is specified if they are not
4300 in the current unit.
4302 If you specify this qualifier the compiler will access these bodies,
4303 creating an extra source dependency for the resulting object file, and
4304 where possible, the call will be inlined.
4305 For further details on when inlining is possible
4306 see @xref{Inlining of Subprograms}.
4308 @item -gnatN
4309 @cindex @option{-gnatN} (@code{GNAT COMPILE})
4310 The front end inlining activated by this qualifier is generally more extensive,
4311 and quite often more effective than the standard @option{/INLINE=PRAGMA} inlining mode.
4312 It will also generate additional dependencies.
4314 @end table
4316 @node Auxiliary Output Control
4317 @subsection Auxiliary Output Control
4319 @table @code
4320 @item /TREE_OUTPUT
4321 @cindex @option{/TREE_OUTPUT} (@code{GNAT COMPILE})
4322 @cindex Writing internal trees
4323 @cindex Internal trees, writing to file
4324 Causes GNAT to write the internal tree for a unit to a file (with the
4325 extension @file{.adt}.
4326 This not normally required, but is used by separate analysis tools.
4327 Typically
4328 these tools do the necessary compilations automatically, so you should
4329 not have to specify this qualifier in normal operation.
4331 @item /UNITS_LIST
4332 @cindex @option{/UNITS_LIST} (@code{GNAT COMPILE})
4333 Print a list of units required by this compilation on @file{SYS$OUTPUT}.
4334 The listing includes all units on which the unit being compiled depends
4335 either directly or indirectly.
4337 @end table
4339 @node Debugging Control
4340 @subsection Debugging Control
4342 @table @code
4343 @cindex Debugging options
4345 @item /EXPAND_SOURCE
4346 @cindex @option{/EXPAND_SOURCE} (@code{GNAT COMPILE})
4347 This qualifier causes the compiler to generate auxiliary output containing
4348 a pseudo-source listing of the generated expanded code. Like most Ada
4349 compilers, GNAT works by first transforming the high level Ada code into
4350 lower level constructs. For example, tasking operations are transformed
4351 into calls to the tasking run-time routines. A unique capability of GNAT
4352 is to list this expanded code in a form very close to normal Ada source.
4353 This is very useful in understanding the implications of various Ada
4354 usage on the efficiency of the generated code. There are many cases in
4355 Ada (e.g. the use of controlled types), where simple Ada statements can
4356 generate a lot of run-time code. By using @option{/EXPAND_SOURCE} you can identify
4357 these cases, and consider whether it may be desirable to modify the coding
4358 approach to improve efficiency.
4360 The format of the output is very similar to standard Ada source, and is
4361 easily understood by an Ada programmer. The following special syntactic
4362 additions correspond to low level features used in the generated code that
4363 do not have any exact analogies in pure Ada source form. The following
4364 is a partial list of these special constructions. See the specification
4365 of package @code{Sprint} in file @file{SPRINT.ADS} for a full list.
4367 @table @code
4368 @item new @var{xxx} [storage_pool = @var{yyy}]
4369 Shows the storage pool being used for an allocator.
4371 @item at end @var{procedure-name};
4372 Shows the finalization (cleanup) procedure for a scope.
4374 @item (if @var{expr} then @var{expr} else @var{expr})
4375 Conditional expression equivalent to the @code{x?y:z} construction in C.
4377 @item @var{target}^(@var{source})
4378 A conversion with floating-point truncation instead of rounding.
4380 @item @var{target}?(@var{source})
4381 A conversion that bypasses normal Ada semantic checking. In particular
4382 enumeration types and fixed-point types are treated simply as integers.
4384 @item @var{target}?^(@var{source})
4385 Combines the above two cases.
4387 @item @var{x} #/ @var{y}
4388 @itemx @var{x} #mod @var{y}
4389 @itemx @var{x} #* @var{y}
4390 @itemx @var{x} #rem @var{y}
4391 A division or multiplication of fixed-point values which are treated as
4392 integers without any kind of scaling.
4394 @item free @var{expr} [storage_pool = @var{xxx}]
4395 Shows the storage pool associated with a @code{free} statement.
4397 @item freeze @var{typename} [@var{actions}]
4398 Shows the point at which @var{typename} is frozen, with possible
4399 associated actions to be performed at the freeze point.
4401 @item reference @var{itype}
4402 Reference (and hence definition) to internal type @var{itype}.
4404 @item @var{function-name}! (@var{arg}, @var{arg}, @var{arg})
4405 Intrinsic function call.
4407 @item @var{labelname} : label
4408 Declaration of label @var{labelname}.
4410 @item @var{expr} && @var{expr} && @var{expr} ... && @var{expr}
4411 A multiple concatenation (same effect as @var{expr} & @var{expr} &
4412 @var{expr}, but handled more efficiently).
4414 @item [constraint_error]
4415 Raise the @code{Constraint_Error} exception.
4417 @item @var{expression}'reference
4418 A pointer to the result of evaluating @var{expression}.
4420 @item @var{target-type}!(@var{source-expression})
4421 An unchecked conversion of @var{source-expression} to @var{target-type}.
4423 @item [@var{numerator}/@var{denominator}]
4424 Used to represent internal real literals (that) have no exact
4425 representation in base 2-16 (for example, the result of compile time
4426 evaluation of the expression 1.0/27.0).
4428 @item /XDEBUG
4429 @cindex @option{/XDEBUG} (@code{GNAT COMPILE})
4430 This qualifier is used in conjunction with @option{/EXPAND_SOURCE} to cause the expanded
4431 source, as described above to be written to files with names
4432 @file{XXX_DG}, where @file{xxx} is the normal file name,
4433 for example, if the source file name is @file{HELLO.ADB},
4434 then a file @file{HELLO.ADB_DG} will be written.
4435 The debugging information generated
4436 by the @code{GNAT COMPILE} @code{/DEBUG} qualifier will refer to the generated
4437 @file{XXX_DG} file. This allows you to do source level debugging using
4438 the generated code which is sometimes useful for complex code, for example
4439 to find out exactly which part of a complex construction raised an
4440 exception. This qualifier also suppress generation of cross-reference
4441 information (see /XREF=SUPPRESS).
4443 @item /COMPRESS_NAMES
4444 @cindex @option{/CHECKS=ELABORATION} (@code{GNAT COMPILE})
4445 In the generated debugging information, and also in the case of long external
4446 names, the compiler uses a compression mechanism if the name is very long.
4447 This compression method uses a checksum, and avoids trouble on some operating
4448 systems which have difficulty with very long names. The @option{/COMPRESS_NAMES} qualifier
4449 forces this compression approach to be used on all external names and names
4450 in the debugging information tables. This reduces the size of the generated
4451 executable, at the expense of making the naming scheme more complex. The
4452 compression only affects the qualification of the name. Thus a name in
4453 the source:
4455 @smallexample
4456 Very_Long_Package.Very_Long_Inner_Package.Var
4457 @end smallexample
4459 @noindent
4460 would normally appear in these tables as:
4462 @smallexample
4463 very_long_package__very_long_inner_package__var
4464 @end smallexample
4466 @noindent
4467 but if the @option{/COMPRESS_NAMES} qualifier is used, then the name appears as
4469 @smallexample
4470 XCb7e0c705__var
4471 @end smallexample
4473 @noindent
4474 Here b7e0c705 is a compressed encoding of the qualification prefix.
4475 The GNAT Ada aware version of GDB understands these encoded prefixes, so if this
4476 debugger is used, the encoding is largely hidden from the user of the compiler.
4478 @end table
4480 @item /REPRESENTATION_INFO[0|1|2|3][s]
4481 @cindex @option{/REPRESENTATION_INFO} (@code{GNAT COMPILE})
4482 This qualifier controls output from the compiler of a listing showing
4483 representation information for declared types and objects. For
4484 @option{/REPRESENTATION_INFO=NONE}, no information is output (equivalent to omitting
4485 the @option{/REPRESENTATION_INFO} qualifier). For @option{/REPRESENTATION_INFO=ARRAYS} (which is the default,
4486 so @option{/REPRESENTATION_INFO} with no parameter has the same effect), size and alignment
4487 information is listed for declared array and record types. For
4488 @option{/REPRESENTATION_INFO=OBJECTS}, size and alignment information is listed for all
4489 expression information for values that are computed at run time for
4490 variant records. These symbolic expressions have a mostly obvious
4491 format with #n being used to represent the value of the n'th
4492 discriminant. See source files @file{REPINFO.ADS/adb} in the
4493 @code{GNAT} sources for full detalis on the format of @option{/REPRESENTATION_INFO=SYMBOLIC}
4494 output. If the qualifier is followed by an s (e.g. @option{-gnatR2s}), then
4495 the output is to a file with the name @file{file_REP} where
4496 file is the name of the corresponding source file.
4498 @item /XREF=SUPPRESS
4499 @cindex @option{/XREF=SUPPRESS} (@code{GNAT COMPILE})
4500 Normally the compiler generates full cross-referencing information in
4501 the @file{ALI} file. This information is used by a number of tools,
4502 including @code{GNAT FIND} and @code{GNAT XREF}. The /XREF=SUPPRESS qualifier
4503 suppresses this information. This saves some space and may slightly
4504 speed up compilation, but means that these tools cannot be used.
4505 @end table
4507 @node Units to Sources Mapping Files
4508 @subsection Units to Sources Mapping Files
4510 @table @code
4512 @item -gnatem@var{path}
4513 @cindex @option{-gnatem} (@code{GNAT COMPILE})
4514 A mapping file is a way to communicate to the compiler two mappings:
4515 from unit names to file names (without any directory information) and from
4516 file names to path names (with full directory information). These mappings
4517 are used by the compiler to short-circuit the path search.
4519 A mapping file is a sequence of sets of three lines. In each set,
4520 the first line is the unit name, in lower case, with "%s" appended for
4521 specifications and "%b" appended for bodies; the second line is the file
4522 name; and the third line is the path name.
4524 Example:
4525 @smallexample
4526    main%b
4527    main.2.ADA
4528    /gnat/project1/sources/main.2.ADA
4529 @end smallexample
4531 When the qualifier @option{-gnatem} is specified, the compiler will create
4532 in memory the two mappings from the specified file. If there is any problem
4533 (non existent file, truncated file or duplicate entries), no mapping
4534 will be created.
4536 Several @option{-gnatem} qualifiers may be specified; however, only the last
4537 one on the command line will be taken into account.
4539 When using a project file, @code{GNAT MAKE} create a temporary mapping file
4540 and communicates it to the compiler using this qualifier.
4542 @end table
4544 @node Search Paths and the Run-Time Library (RTL)
4545 @section Search Paths and the Run-Time Library (RTL)
4547 @noindent
4548 With the GNAT source-based library system, the compiler must be able to
4549 find source files for units that are needed by the unit being compiled.
4550 Search paths are used to guide this process.
4552 The compiler compiles one source file whose name must be given
4553 explicitly on the command line. In other words, no searching is done
4554 for this file. To find all other source files that are needed (the most
4555 common being the specs of units), the compiler examines the following
4556 directories, in the following order:
4558 @enumerate
4559 @item
4560 The directory containing the source file of the main unit being compiled
4561 (the file name on the command line).
4563 @item
4564 Each directory named by an @code{/SOURCE_SEARCH} qualifier given on the @code{GNAT COMPILE}
4565 command line, in the order given.
4567 @item
4568 @findex ADA_INCLUDE_PATH
4569 Each of the directories listed in the value of the
4570 @code{ADA_INCLUDE_PATH} logical name.
4571 Normally, define this value as a logical name containing a comma separated
4572 list of directory names.
4574 This variable can also be defined by means of an environment string
4575 (an argument to the DEC C exec* set of functions).
4577 Logical Name:
4578 @smallexample
4579 DEFINE ANOTHER_PATH FOO:[BAG]
4580 DEFINE ADA_INCLUDE_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
4581 @end smallexample
4583 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
4584 first, followed by the standard Ada 95
4585 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADAINCLUDE].
4586 If this is not redefined, the user will obtain the DEC Ada83 IO packages
4587 (Text_IO, Sequential_IO, etc)
4588 instead of the Ada95 packages. Thus, in order to get the Ada 95
4589 packages by default, ADA_INCLUDE_PATH must be redefined.
4590 @item
4591 The content of the "ada_source_path" file which is part of the GNAT
4592 installation tree and is used to store standard libraries such as the
4593 GNAT Run Time Library (RTL) source files.
4594 @end enumerate
4596 @noindent
4597 Specifying the qualifier @code{/NOCURRENT_DIRECTORY}
4598 inhibits the use of the directory
4599 containing the source file named in the command line. You can still
4600 have this directory on your search path, but in this case it must be
4601 explicitly requested with a @code{/SOURCE_SEARCH} qualifier.
4603 Specifying the qualifier @code{/NOSTD_INCLUDES}
4604 inhibits the search of the default location for the GNAT Run Time
4605 Library (RTL) source files.
4607 The compiler outputs its object files and ALI files in the current
4608 working directory.
4610 @findex System.IO
4611 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
4612 children make up the GNAT RTL, together with the simple @code{System.IO}
4613 package used in the "Hello World" example. The sources for these units
4614 are needed by the compiler and are kept together in one directory. Not
4615 all of the bodies are needed, but all of the sources are kept together
4616 anyway. In a normal installation, you need not specify these directory
4617 names when compiling or binding. Either the environment variables or
4618 the built-in defaults cause these files to be found.
4620 In addition to the language-defined hierarchies (System, Ada and
4621 Interfaces), the GNAT distribution provides a fourth hierarchy,
4622 consisting of child units of GNAT. This is a collection of generally
4623 useful routines. See the GNAT Reference Manual for further details.
4625 Besides simplifying access to the RTL, a major use of search paths is
4626 in compiling sources from multiple directories. This can make
4627 development environments much more flexible.
4629 @node Order of Compilation Issues
4630 @section Order of Compilation Issues
4632 @noindent
4633 If, in our earlier example, there was a spec for the @code{hello}
4634 procedure, it would be contained in the file @file{HELLO.ADS}; yet this
4635 file would not have to be explicitly compiled. This is the result of the
4636 model we chose to implement library management. Some of the consequences
4637 of this model are as follows:
4639 @itemize @bullet
4640 @item
4641 There is no point in compiling specs (except for package
4642 specs with no bodies) because these are compiled as needed by clients. If
4643 you attempt a useless compilation, you will receive an error message.
4644 It is also useless to compile subunits because they are compiled as needed
4645 by the parent.
4647 @item
4648 There are no order of compilation requirements: performing a
4649 compilation never obsoletes anything. The only way you can obsolete
4650 something and require recompilations is to modify one of the
4651 source files on which it depends.
4653 @item
4654 There is no library as such, apart from the ALI files
4655 (@pxref{The Ada Library Information Files}, for information on the format of these
4656 files). For now we find it convenient to create separate ALI files, but
4657 eventually the information therein may be incorporated into the object
4658 file directly.
4660 @item
4661 When you compile a unit, the source files for the specs of all units
4662 that it @code{with}'s, all its subunits, and the bodies of any generics it
4663 instantiates must be available (reachable by the search-paths mechanism
4664 described above), or you will receive a fatal error message.
4665 @end itemize
4667 @node Examples
4668 @section Examples
4670 @noindent
4671 The following are some typical Ada compilation command line examples:
4673 @table @code
4674 @item $ GNAT COMPILE XYZ.ADB
4675 Compile body in file @file{XYZ.ADB} with all default options.
4677 @item $ GNAT COMPILE /OPTIMIZE=ALL /CHECKS=ASSERTIONS XYZ-DEF.ADB
4679 Compile the child unit package in file @file{XYZ-DEF.ADB} with extensive
4680 optimizations, and pragma @code{Assert}/@code{Debug} statements
4681 enabled.
4683 @item $ GNAT COMPILE /NOLOAD ABC-DEF.ADB
4684 Compile the subunit in file @file{ABC-DEF.ADB} in semantic-checking-only
4685 mode.
4686 @end table
4688 @node Binding Using GNAT BIND
4689 @chapter Binding Using @code{GNAT BIND}
4690 @findex GNAT BIND
4692 @menu
4693 * Running GNAT BIND::
4694 * Generating the Binder Program in C::
4695 * Consistency-Checking Modes::
4696 * Binder Error Message Control::
4697 * Elaboration Control::
4698 * Output Control::
4699 * Binding with Non-Ada Main Programs::
4700 * Binding Programs with No Main Subprogram::
4701 * Summary of Binder Qualifiers::
4702 * Command-Line Access::
4703 * Search Paths for GNAT BIND::
4704 * Examples of GNAT BIND Usage::
4705 @end menu
4707 @noindent
4708 This chapter describes the GNAT binder, @code{GNAT BIND}, which is used
4709 to bind compiled GNAT objects. The @code{GNAT BIND} program performs
4710 four separate functions:
4712 @enumerate
4713 @item
4714 Checks that a program is consistent, in accordance with the rules in
4715 Chapter 10 of the Ada 95 Reference Manual. In particular, error
4716 messages are generated if a program uses inconsistent versions of a
4717 given unit.
4719 @item
4720 Checks that an acceptable order of elaboration exists for the program
4721 and issues an error message if it cannot find an order of elaboration
4722 that satisfies the rules in Chapter 10 of the Ada 95 Language Manual.
4724 @item
4725 Generates a main program incorporating the given elaboration order.
4726 This program is a small Ada package (body and spec) that
4727 must be subsequently compiled
4728 using the GNAT compiler. The necessary compilation step is usually
4729 performed automatically by @code{GNAT LINK}. The two most important
4730 functions of this program
4731 are to call the elaboration routines of units in an appropriate order
4732 and to call the main program.
4734 @item
4735 Determines the set of object files required by the given main program.
4736 This information is output in the forms of comments in the generated program,
4737 to be read by the @code{GNAT LINK} utility used to link the Ada application.
4738 @end enumerate
4740 @node Running GNAT BIND
4741 @section Running @code{GNAT BIND}
4743 @noindent
4744 The form of the @code{GNAT BIND} command is
4746 @smallexample
4747 $ GNAT BIND [@var{qualifiers}] @var{mainprog}[.ALI] [@var{qualifiers}]
4748 @end smallexample
4750 @noindent
4751 where @var{mainprog}.ADB is the Ada file containing the main program
4752 unit body. If no qualifiers are specified, @code{GNAT BIND} constructs an Ada
4753 package in two files which names are
4754 @file{B$@var{ada_main}.ADS}, and @file{B$@var{ada_main}.ADB}.
4755 For example, if given the
4756 parameter @samp{HELLO.ALI}, for a main program contained in file
4757 @file{HELLO.ADB}, the binder output files would be @file{B~HELLO.ADS}
4758 and @file{B~HELLO.ADB}.
4760 When doing consistency checking, the binder takes into consideration
4761 any source files it can locate. For example, if the binder determines
4762 that the given main program requires the package @code{Pack}, whose
4763 @file{.ALI}
4764 file is @file{PACK.ALI} and whose corresponding source spec file is
4765 @file{PACK.ADS}, it attempts to locate the source file @file{PACK.ADS}
4766 (using the same search path conventions as previously described for the
4767 @code{GNAT COMPILE} command). If it can locate this source file, it checks that
4768 the time stamps
4769 or source checksums of the source and its references to in @file{ali} files
4770 match. In other words, any @file{ali} files that mentions this spec must have
4771 resulted from compiling this version of the source file (or in the case
4772 where the source checksums match, a version close enough that the
4773 difference does not matter).
4775 @cindex Source files, use by binder
4776 The effect of this consistency checking, which includes source files, is
4777 that the binder ensures that the program is consistent with the latest
4778 version of the source files that can be located at bind time. Editing a
4779 source file without compiling files that depend on the source file cause
4780 error messages to be generated by the binder.
4782 For example, suppose you have a main program @file{HELLO.ADB} and a
4783 package @code{P}, from file @file{P.ADS} and you perform the following
4784 steps:
4786 @enumerate
4787 @item
4788 Enter @code{GNAT COMPILE HELLO.ADB} to compile the main program.
4790 @item
4791 Enter @code{GNAT COMPILE P.ADS} to compile package @code{P}.
4793 @item
4794 Edit file @file{P.ADS}.
4796 @item
4797 Enter @code{GNAT BIND hello}.
4798 @end enumerate
4800 At this point, the file @file{P.ALI} contains an out-of-date time stamp
4801 because the file @file{P.ADS} has been edited. The attempt at binding
4802 fails, and the binder generates the following error messages:
4804 @smallexample
4805 error: "HELLO.ADB" must be recompiled ("P.ADS" has been modified)
4806 error: "P.ADS" has been modified and must be recompiled
4807 @end smallexample
4809 @noindent
4810 Now both files must be recompiled as indicated, and then the bind can
4811 succeed, generating a main program. You need not normally be concerned
4812 with the contents of this file, but it is similar to the following which
4813 is the binder file generated for a simple "hello world" program.
4815 @smallexample
4816 @iftex
4817 @leftskip=0cm
4818 @end iftex
4819 --  The package is called Ada_Main unless this name is actually used
4820 --  as a unit name in the partition, in which case some other unique
4821 --  name is used.
4823 with System;
4824 package ada_main is
4826    Elab_Final_Code : Integer;
4827    pragma Import (C, Elab_Final_Code, "__gnat_inside_elab_final_code");
4829    --  The main program saves the parameters (argument count,
4830    --  argument values, environment pointer) in global variables
4831    --  for later access by other units including
4832    --  Ada.Command_Line.
4834    gnat_argc : Integer;
4835    gnat_argv : System.Address;
4836    gnat_envp : System.Address;
4838    --  The actual variables are stored in a library routine. This
4839    --  is useful for some shared library situations, where there
4840    --  are problems if variables are not in the library.
4842    pragma Import (C, gnat_argc);
4843    pragma Import (C, gnat_argv);
4844    pragma Import (C, gnat_envp);
4846    --  The exit status is similarly an external location
4848    gnat_exit_status : Integer;
4849    pragma Import (C, gnat_exit_status);
4851    GNAT_Version : constant String :=
4852                     "GNAT Version: 3.15w (20010315)";
4853    pragma Export (C, GNAT_Version, "__gnat_version");
4855    --  This is the generated adafinal routine that performs
4856    --  finalization at the end of execution. In the case where
4857    --  Ada is the main program, this main program makes a call
4858    --  to adafinal at program termination.
4860    procedure adafinal;
4861    pragma Export (C, adafinal, "adafinal");
4863    --  This is the generated adainit routine that performs
4864    --  initialization at the start of execution. In the case
4865    --  where Ada is the main program, this main program makes
4866    --  a call to adainit at program startup.
4868    procedure adainit;
4869    pragma Export (C, adainit, "adainit");
4871    --  This routine is called at the start of execution. It is
4872    --  a dummy routine that is used by the debugger to breakpoint
4873    --  at the start of execution.
4875    procedure Break_Start;
4876    pragma Import (C, Break_Start, "__gnat_break_start");
4878    --  This is the actual generated main program (it would be
4879    --  suppressed if the no main program qualifier were used). As
4880    --  required by standard system conventions, this program has
4881    --  the external name main.
4883    function main
4884      (argc : Integer;
4885       argv : System.Address;
4886       envp : System.Address)
4887       return Integer;
4888    pragma Export (C, main, "main");
4890    --  The following set of constants give the version
4891    --  identification values for every unit in the bound
4892    --  partition. This identification is computed from all
4893    --  dependent semantic units, and corresponds to the
4894    --  string that would be returned by use of the
4895    --  Body_Version or Version attributes.
4897    type Version_32 is mod 2 ** 32;
4898    u00001 : constant Version_32 := 16#7880BEB3#;
4899    u00002 : constant Version_32 := 16#0D24CBD0#;
4900    u00003 : constant Version_32 := 16#3283DBEB#;
4901    u00004 : constant Version_32 := 16#2359F9ED#;
4902    u00005 : constant Version_32 := 16#664FB847#;
4903    u00006 : constant Version_32 := 16#68E803DF#;
4904    u00007 : constant Version_32 := 16#5572E604#;
4905    u00008 : constant Version_32 := 16#46B173D8#;
4906    u00009 : constant Version_32 := 16#156A40CF#;
4907    u00010 : constant Version_32 := 16#033DABE0#;
4908    u00011 : constant Version_32 := 16#6AB38FEA#;
4909    u00012 : constant Version_32 := 16#22B6217D#;
4910    u00013 : constant Version_32 := 16#68A22947#;
4911    u00014 : constant Version_32 := 16#18CC4A56#;
4912    u00015 : constant Version_32 := 16#08258E1B#;
4913    u00016 : constant Version_32 := 16#367D5222#;
4914    u00017 : constant Version_32 := 16#20C9ECA4#;
4915    u00018 : constant Version_32 := 16#50D32CB6#;
4916    u00019 : constant Version_32 := 16#39A8BB77#;
4917    u00020 : constant Version_32 := 16#5CF8FA2B#;
4918    u00021 : constant Version_32 := 16#2F1EB794#;
4919    u00022 : constant Version_32 := 16#31AB6444#;
4920    u00023 : constant Version_32 := 16#1574B6E9#;
4921    u00024 : constant Version_32 := 16#5109C189#;
4922    u00025 : constant Version_32 := 16#56D770CD#;
4923    u00026 : constant Version_32 := 16#02F9DE3D#;
4924    u00027 : constant Version_32 := 16#08AB6B2C#;
4925    u00028 : constant Version_32 := 16#3FA37670#;
4926    u00029 : constant Version_32 := 16#476457A0#;
4927    u00030 : constant Version_32 := 16#731E1B6E#;
4928    u00031 : constant Version_32 := 16#23C2E789#;
4929    u00032 : constant Version_32 := 16#0F1BD6A1#;
4930    u00033 : constant Version_32 := 16#7C25DE96#;
4931    u00034 : constant Version_32 := 16#39ADFFA2#;
4932    u00035 : constant Version_32 := 16#571DE3E7#;
4933    u00036 : constant Version_32 := 16#5EB646AB#;
4934    u00037 : constant Version_32 := 16#4249379B#;
4935    u00038 : constant Version_32 := 16#0357E00A#;
4936    u00039 : constant Version_32 := 16#3784FB72#;
4937    u00040 : constant Version_32 := 16#2E723019#;
4938    u00041 : constant Version_32 := 16#623358EA#;
4939    u00042 : constant Version_32 := 16#107F9465#;
4940    u00043 : constant Version_32 := 16#6843F68A#;
4941    u00044 : constant Version_32 := 16#63305874#;
4942    u00045 : constant Version_32 := 16#31E56CE1#;
4943    u00046 : constant Version_32 := 16#02917970#;
4944    u00047 : constant Version_32 := 16#6CCBA70E#;
4945    u00048 : constant Version_32 := 16#41CD4204#;
4946    u00049 : constant Version_32 := 16#572E3F58#;
4947    u00050 : constant Version_32 := 16#20729FF5#;
4948    u00051 : constant Version_32 := 16#1D4F93E8#;
4949    u00052 : constant Version_32 := 16#30B2EC3D#;
4950    u00053 : constant Version_32 := 16#34054F96#;
4951    u00054 : constant Version_32 := 16#5A199860#;
4952    u00055 : constant Version_32 := 16#0E7F912B#;
4953    u00056 : constant Version_32 := 16#5760634A#;
4954    u00057 : constant Version_32 := 16#5D851835#;
4956    --  The following Export pragmas export the version numbers
4957    --  with symbolic names ending in B (for body) or S
4958    --  (for spec) so that they can be located in a link. The
4959    --  information provided here is sufficient to track down
4960    --  the exact versions of units used in a given build.
4962    pragma Export (C, u00001, "helloB");
4963    pragma Export (C, u00002, "system__standard_libraryB");
4964    pragma Export (C, u00003, "system__standard_libraryS");
4965    pragma Export (C, u00004, "adaS");
4966    pragma Export (C, u00005, "ada__text_ioB");
4967    pragma Export (C, u00006, "ada__text_ioS");
4968    pragma Export (C, u00007, "ada__exceptionsB");
4969    pragma Export (C, u00008, "ada__exceptionsS");
4970    pragma Export (C, u00009, "gnatS");
4971    pragma Export (C, u00010, "gnat__heap_sort_aB");
4972    pragma Export (C, u00011, "gnat__heap_sort_aS");
4973    pragma Export (C, u00012, "systemS");
4974    pragma Export (C, u00013, "system__exception_tableB");
4975    pragma Export (C, u00014, "system__exception_tableS");
4976    pragma Export (C, u00015, "gnat__htableB");
4977    pragma Export (C, u00016, "gnat__htableS");
4978    pragma Export (C, u00017, "system__exceptionsS");
4979    pragma Export (C, u00018, "system__machine_state_operationsB");
4980    pragma Export (C, u00019, "system__machine_state_operationsS");
4981    pragma Export (C, u00020, "system__machine_codeS");
4982    pragma Export (C, u00021, "system__storage_elementsB");
4983    pragma Export (C, u00022, "system__storage_elementsS");
4984    pragma Export (C, u00023, "system__secondary_stackB");
4985    pragma Export (C, u00024, "system__secondary_stackS");
4986    pragma Export (C, u00025, "system__parametersB");
4987    pragma Export (C, u00026, "system__parametersS");
4988    pragma Export (C, u00027, "system__soft_linksB");
4989    pragma Export (C, u00028, "system__soft_linksS");
4990    pragma Export (C, u00029, "system__stack_checkingB");
4991    pragma Export (C, u00030, "system__stack_checkingS");
4992    pragma Export (C, u00031, "system__tracebackB");
4993    pragma Export (C, u00032, "system__tracebackS");
4994    pragma Export (C, u00033, "ada__streamsS");
4995    pragma Export (C, u00034, "ada__tagsB");
4996    pragma Export (C, u00035, "ada__tagsS");
4997    pragma Export (C, u00036, "system__string_opsB");
4998    pragma Export (C, u00037, "system__string_opsS");
4999    pragma Export (C, u00038, "interfacesS");
5000    pragma Export (C, u00039, "interfaces__c_streamsB");
5001    pragma Export (C, u00040, "interfaces__c_streamsS");
5002    pragma Export (C, u00041, "system__file_ioB");
5003    pragma Export (C, u00042, "system__file_ioS");
5004    pragma Export (C, u00043, "ada__finalizationB");
5005    pragma Export (C, u00044, "ada__finalizationS");
5006    pragma Export (C, u00045, "system__finalization_rootB");
5007    pragma Export (C, u00046, "system__finalization_rootS");
5008    pragma Export (C, u00047, "system__finalization_implementationB");
5009    pragma Export (C, u00048, "system__finalization_implementationS");
5010    pragma Export (C, u00049, "system__string_ops_concat_3B");
5011    pragma Export (C, u00050, "system__string_ops_concat_3S");
5012    pragma Export (C, u00051, "system__stream_attributesB");
5013    pragma Export (C, u00052, "system__stream_attributesS");
5014    pragma Export (C, u00053, "ada__io_exceptionsS");
5015    pragma Export (C, u00054, "system__unsigned_typesS");
5016    pragma Export (C, u00055, "system__file_control_blockS");
5017    pragma Export (C, u00056, "ada__finalization__list_controllerB");
5018    pragma Export (C, u00057, "ada__finalization__list_controllerS");
5020    -- BEGIN ELABORATION ORDER
5021    -- ada (spec)
5022    -- gnat (spec)
5023    -- gnat.heap_sort_a (spec)
5024    -- gnat.heap_sort_a (body)
5025    -- gnat.htable (spec)
5026    -- gnat.htable (body)
5027    -- interfaces (spec)
5028    -- system (spec)
5029    -- system.machine_code (spec)
5030    -- system.parameters (spec)
5031    -- system.parameters (body)
5032    -- interfaces.c_streams (spec)
5033    -- interfaces.c_streams (body)
5034    -- system.standard_library (spec)
5035    -- ada.exceptions (spec)
5036    -- system.exception_table (spec)
5037    -- system.exception_table (body)
5038    -- ada.io_exceptions (spec)
5039    -- system.exceptions (spec)
5040    -- system.storage_elements (spec)
5041    -- system.storage_elements (body)
5042    -- system.machine_state_operations (spec)
5043    -- system.machine_state_operations (body)
5044    -- system.secondary_stack (spec)
5045    -- system.stack_checking (spec)
5046    -- system.soft_links (spec)
5047    -- system.soft_links (body)
5048    -- system.stack_checking (body)
5049    -- system.secondary_stack (body)
5050    -- system.standard_library (body)
5051    -- system.string_ops (spec)
5052    -- system.string_ops (body)
5053    -- ada.tags (spec)
5054    -- ada.tags (body)
5055    -- ada.streams (spec)
5056    -- system.finalization_root (spec)
5057    -- system.finalization_root (body)
5058    -- system.string_ops_concat_3 (spec)
5059    -- system.string_ops_concat_3 (body)
5060    -- system.traceback (spec)
5061    -- system.traceback (body)
5062    -- ada.exceptions (body)
5063    -- system.unsigned_types (spec)
5064    -- system.stream_attributes (spec)
5065    -- system.stream_attributes (body)
5066    -- system.finalization_implementation (spec)
5067    -- system.finalization_implementation (body)
5068    -- ada.finalization (spec)
5069    -- ada.finalization (body)
5070    -- ada.finalization.list_controller (spec)
5071    -- ada.finalization.list_controller (body)
5072    -- system.file_control_block (spec)
5073    -- system.file_io (spec)
5074    -- system.file_io (body)
5075    -- ada.text_io (spec)
5076    -- ada.text_io (body)
5077    -- hello (body)
5078    -- END ELABORATION ORDER
5080 end ada_main;
5082 --  The following source file name pragmas allow the generated file
5083 --  names to be unique for different main programs. They are needed
5084 --  since the package name will always be Ada_Main.
5086 pragma Source_File_Name (ada_main, Spec_File_Name => "B~HELLO.ADS");
5087 pragma Source_File_Name (ada_main, Body_File_Name => "B~HELLO.ADB");
5089 --  Generated package body for Ada_Main starts here
5091 package body ada_main is
5093    --  The actual finalization is performed by calling the
5094    --  library routine in System.Standard_Library.Adafinal
5096    procedure Do_Finalize;
5097    pragma Import (C, Do_Finalize, "system__standard_library__adafinal");
5099    -------------
5100    -- adainit --
5101    -------------
5103 @findex adainit
5104    procedure adainit is
5106       --  These booleans are set to True once the associated unit has
5107       --  been elaborated. It is also used to avoid elaborating the
5108       --  same unit twice.
5110       E040 : Boolean; pragma Import (Ada, E040, "interfaces__c_streams_E");
5111       E008 : Boolean; pragma Import (Ada, E008, "ada__exceptions_E");
5112       E014 : Boolean; pragma Import (Ada, E014, "system__exception_table_E");
5113       E053 : Boolean; pragma Import (Ada, E053, "ada__io_exceptions_E");
5114       E017 : Boolean; pragma Import (Ada, E017, "system__exceptions_E");
5115       E024 : Boolean; pragma Import (Ada, E024, "system__secondary_stack_E");
5116       E030 : Boolean; pragma Import (Ada, E030, "system__stack_checking_E");
5117       E028 : Boolean; pragma Import (Ada, E028, "system__soft_links_E");
5118       E035 : Boolean; pragma Import (Ada, E035, "ada__tags_E");
5119       E033 : Boolean; pragma Import (Ada, E033, "ada__streams_E");
5120       E046 : Boolean; pragma Import (Ada, E046, "system__finalization_root_E");
5121       E048 : Boolean; pragma Import (Ada, E048, "system__finalization_implementation_E");
5122       E044 : Boolean; pragma Import (Ada, E044, "ada__finalization_E");
5123       E057 : Boolean; pragma Import (Ada, E057, "ada__finalization__list_controller_E");
5124       E055 : Boolean; pragma Import (Ada, E055, "system__file_control_block_E");
5125       E042 : Boolean; pragma Import (Ada, E042, "system__file_io_E");
5126       E006 : Boolean; pragma Import (Ada, E006, "ada__text_io_E");
5128       --  Set_Globals is a library routine that stores away the
5129       --  value of the indicated set of global values in global
5130       --  variables within the library.
5132       procedure Set_Globals
5133         (Main_Priority            : Integer;
5134          Time_Slice_Value         : Integer;
5135          WC_Encoding              : Character;
5136          Locking_Policy           : Character;
5137          Queuing_Policy           : Character;
5138          Task_Dispatching_Policy  : Character;
5139          Adafinal                 : System.Address;
5140          Unreserve_All_Interrupts : Integer;
5141          Exception_Tracebacks     : Integer);
5142 @findex __gnat_set_globals
5143       pragma Import (C, Set_Globals, "__gnat_set_globals");
5145       --  SDP_Table_Build is a library routine used to build the
5146       --  exception tables. See unit Ada.Exceptions in files
5147       --  A-EXCEPT.ADS/adb for full details of how zero cost
5148       --  exception handling works. This procedure, the call to
5149       --  it, and the two following tables are all omitted if the
5150       --  build is in longjmp/setjump exception mode.
5152 @findex SDP_Table_Build
5153 @findex Zero Cost Exceptions
5154       procedure SDP_Table_Build
5155         (SDP_Addresses   : System.Address;
5156          SDP_Count       : Natural;
5157          Elab_Addresses  : System.Address;
5158          Elab_Addr_Count : Natural);
5159       pragma Import (C, SDP_Table_Build, "__gnat_SDP_Table_Build");
5161       --  Table of Unit_Exception_Table addresses. Used for zero
5162       --  cost exception handling to build the top level table.
5164       ST : aliased constant array (1 .. 23) of System.Address := (
5165         Hello'UET_Address,
5166         Ada.Text_Io'UET_Address,
5167         Ada.Exceptions'UET_Address,
5168         Gnat.Heap_Sort_A'UET_Address,
5169         System.Exception_Table'UET_Address,
5170         System.Machine_State_Operations'UET_Address,
5171         System.Secondary_Stack'UET_Address,
5172         System.Parameters'UET_Address,
5173         System.Soft_Links'UET_Address,
5174         System.Stack_Checking'UET_Address,
5175         System.Traceback'UET_Address,
5176         Ada.Streams'UET_Address,
5177         Ada.Tags'UET_Address,
5178         System.String_Ops'UET_Address,
5179         Interfaces.C_Streams'UET_Address,
5180         System.File_Io'UET_Address,
5181         Ada.Finalization'UET_Address,
5182         System.Finalization_Root'UET_Address,
5183         System.Finalization_Implementation'UET_Address,
5184         System.String_Ops_Concat_3'UET_Address,
5185         System.Stream_Attributes'UET_Address,
5186         System.File_Control_Block'UET_Address,
5187         Ada.Finalization.List_Controller'UET_Address);
5189       --  Table of addresses of elaboration routines. Used for
5190       --  zero cost exception handling to make sure these
5191       --  addresses are included in the top level procedure
5192       --  address table.
5194       EA : aliased constant array (1 .. 23) of System.Address := (
5195         adainit'Code_Address,
5196         Do_Finalize'Code_Address,
5197         Ada.Exceptions'Elab_Spec'Address,
5198         System.Exceptions'Elab_Spec'Address,
5199         Interfaces.C_Streams'Elab_Spec'Address,
5200         System.Exception_Table'Elab_Body'Address,
5201         Ada.Io_Exceptions'Elab_Spec'Address,
5202         System.Stack_Checking'Elab_Spec'Address,
5203         System.Soft_Links'Elab_Body'Address,
5204         System.Secondary_Stack'Elab_Body'Address,
5205         Ada.Tags'Elab_Spec'Address,
5206         Ada.Tags'Elab_Body'Address,
5207         Ada.Streams'Elab_Spec'Address,
5208         System.Finalization_Root'Elab_Spec'Address,
5209         Ada.Exceptions'Elab_Body'Address,
5210         System.Finalization_Implementation'Elab_Spec'Address,
5211         System.Finalization_Implementation'Elab_Body'Address,
5212         Ada.Finalization'Elab_Spec'Address,
5213         Ada.Finalization.List_Controller'Elab_Spec'Address,
5214         System.File_Control_Block'Elab_Spec'Address,
5215         System.File_Io'Elab_Body'Address,
5216         Ada.Text_Io'Elab_Spec'Address,
5217         Ada.Text_Io'Elab_Body'Address);
5219    --  Start of processing for adainit
5221    begin
5223       --  Call SDP_Table_Build to build the top level procedure
5224       --  table for zero cost exception handling (omitted in
5225       --  longjmp/setjump mode).
5227       SDP_Table_Build (ST'Address, 23, EA'Address, 23);
5229       --  Call Set_Globals to record various information for
5230       --  this partition.  The values are derived by the binder
5231       --  from information stored in the ali files by the compiler.
5233 @findex __gnat_set_globals
5234       Set_Globals
5235         (Main_Priority            => -1,
5236          --  Priority of main program, -1 if no pragma Priority used
5238          Time_Slice_Value         => -1,
5239          --  Time slice from Time_Slice pragma, -1 if none used
5241          WC_Encoding              => 'b',
5242          --  Wide_Character encoding used, default is brackets
5244          Locking_Policy           => ' ',
5245          --  Locking_Policy used, default of space means not
5246          --  specified, otherwise it is the first character of
5247          --  the policy name.
5249          Queuing_Policy           => ' ',
5250          --  Queuing_Policy used, default of space means not
5251          --  specified, otherwise it is the first character of
5252          --  the policy name.
5254          Task_Dispatching_Policy  => ' ',
5255          --  Task_Dispatching_Policy used, default of space means
5256          --  not specified, otherwise first character of the
5257          --  policy name.
5259          Adafinal                 => System.Null_Address,
5260          --  Address of Adafinal routine, not used anymore
5262          Unreserve_All_Interrupts => 0,
5263          --  Set true if pragma Unreserve_All_Interrupts was used
5265          Exception_Tracebacks     => 0);
5266          --  Indicates if exception tracebacks are enabled
5268       Elab_Final_Code := 1;
5270       --  Now we have the elaboration calls for all units in the partition.
5271       --  The Elab_Spec and Elab_Body attributes generate references to the
5272       --  implicit elaboration procedures generated by the compiler for
5273       --  each unit that requires elaboration.
5275       if not E040 then
5276          Interfaces.C_Streams'Elab_Spec;
5277       end if;
5278       E040 := True;
5279       if not E008 then
5280          Ada.Exceptions'Elab_Spec;
5281       end if;
5282       if not E014 then
5283          System.Exception_Table'Elab_Body;
5284          E014 := True;
5285       end if;
5286       if not E053 then
5287          Ada.Io_Exceptions'Elab_Spec;
5288          E053 := True;
5289       end if;
5290       if not E017 then
5291          System.Exceptions'Elab_Spec;
5292          E017 := True;
5293       end if;
5294       if not E030 then
5295          System.Stack_Checking'Elab_Spec;
5296       end if;
5297       if not E028 then
5298          System.Soft_Links'Elab_Body;
5299          E028 := True;
5300       end if;
5301       E030 := True;
5302       if not E024 then
5303          System.Secondary_Stack'Elab_Body;
5304          E024 := True;
5305       end if;
5306       if not E035 then
5307          Ada.Tags'Elab_Spec;
5308       end if;
5309       if not E035 then
5310          Ada.Tags'Elab_Body;
5311          E035 := True;
5312       end if;
5313       if not E033 then
5314          Ada.Streams'Elab_Spec;
5315          E033 := True;
5316       end if;
5317       if not E046 then
5318          System.Finalization_Root'Elab_Spec;
5319       end if;
5320       E046 := True;
5321       if not E008 then
5322          Ada.Exceptions'Elab_Body;
5323          E008 := True;
5324       end if;
5325       if not E048 then
5326          System.Finalization_Implementation'Elab_Spec;
5327       end if;
5328       if not E048 then
5329          System.Finalization_Implementation'Elab_Body;
5330          E048 := True;
5331       end if;
5332       if not E044 then
5333          Ada.Finalization'Elab_Spec;
5334       end if;
5335       E044 := True;
5336       if not E057 then
5337          Ada.Finalization.List_Controller'Elab_Spec;
5338       end if;
5339       E057 := True;
5340       if not E055 then
5341          System.File_Control_Block'Elab_Spec;
5342          E055 := True;
5343       end if;
5344       if not E042 then
5345          System.File_Io'Elab_Body;
5346          E042 := True;
5347       end if;
5348       if not E006 then
5349          Ada.Text_Io'Elab_Spec;
5350       end if;
5351       if not E006 then
5352          Ada.Text_Io'Elab_Body;
5353          E006 := True;
5354       end if;
5356       Elab_Final_Code := 0;
5357    end adainit;
5359    --------------
5360    -- adafinal --
5361    --------------
5363 @findex adafinal
5364    procedure adafinal is
5365    begin
5366       Do_Finalize;
5367    end adafinal;
5369    ----------
5370    -- main --
5371    ----------
5373    --  main is actually a function, as in the ANSI C standard,
5374    --  defined to return the exit status. The three parameters
5375    --  are the argument count, argument values and environment
5376    --  pointer.
5378 @findex Main Program
5379    function main
5380      (argc : Integer;
5381       argv : System.Address;
5382       envp : System.Address)
5383       return Integer
5384    is
5385       --  The initialize routine performs low level system
5386       --  initialization using a standard library routine which
5387       --  sets up signal handling and performs any other
5388       --  required setup. The routine can be found in file
5389       --  A-INIT.C.
5391 @findex __gnat_initialize
5392       procedure initialize;
5393       pragma Import (C, initialize, "__gnat_initialize");
5395       --  The finalize routine performs low level system
5396       --  finalization using a standard library routine. The
5397       --  routine is found in file A-FINAL.C and in the standard
5398       --  distribution is a dummy routine that does nothing, so
5399       --  really this is a hook for special user finalization.
5401 @findex __gnat_finalize
5402       procedure finalize;
5403       pragma Import (C, finalize, "__gnat_finalize");
5405       --  We get to the main program of the partition by using
5406       --  pragma Import because if we try to with the unit and
5407       --  call it Ada style, then not only do we waste time
5408       --  recompiling it, but also, we don't really know the right
5409       --  qualifiers (e.g. identifier character set) to be used
5410       --  to compile it.
5412       procedure Ada_Main_Program;
5413       pragma Import (Ada, Ada_Main_Program, "_ada_hello");
5415    --  Start of processing for main
5417    begin
5418       --  Save global variables
5420       gnat_argc := argc;
5421       gnat_argv := argv;
5422       gnat_envp := envp;
5424       --  Call low level system initialization
5426       Initialize;
5428       --  Call our generated Ada initialization routine
5430       adainit;
5432       --  This is the point at which we want the debugger to get
5433       --  control
5435       Break_Start;
5437       --  Now we call the main program of the partition
5439       Ada_Main_Program;
5441       --  Perform Ada finalization
5443       adafinal;
5445       --  Perform low level system finalization
5447       Finalize;
5449       --  Return the proper exit status
5450       return (gnat_exit_status);
5451    end;
5453 --  This section is entirely comments, so it has no effect on the
5454 --  compilation of the Ada_Main package. It provides the list of
5455 --  object files and linker options, as well as some standard
5456 --  libraries needed for the link. The GNAT LINK utility parses
5457 --  this B~HELLO.ADB file to read these comment lines to generate
5458 --  the appropriate command line arguments for the call to the
5459 --  system linker. The BEGIN/END lines are used for sentinels for
5460 --  this parsing operation.
5462 --  The exact file names will of course depend on the environment,
5463 --  host/target and location of files on the host system.
5465 @findex Object file list
5466 -- BEGIN Object file/option list
5467    --   ./HELLO.OBJ
5468    --   -L./
5469    --   -L/usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/
5470    --   /usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a
5471 -- END Object file/option list
5473 end ada_main;
5475 @end smallexample
5477 @noindent
5478 The Ada code in the above example is exactly what is generated by the
5479 binder. We have added comments to more clearly indicate the function
5480 of each part of the generated @code{Ada_Main} package.
5482 The code is standard Ada in all respects, and can be processed by any
5483 tools that handle Ada. In particular, it is possible to use the debugger
5484 in Ada mode to debug the generated Ada_Main package. For example, suppose
5485 that for reasons that you do not understand, your program is blowing up
5486 during elaboration of the body of @code{Ada.Text_IO}. To chase this bug
5487 down, you can place a breakpoint on the call:
5489 @smallexample
5490 Ada.Text_Io'Elab_Body;
5491 @end smallexample
5493 @noindent
5494 and trace the elaboration routine for this package to find out where
5495 the problem might be (more usually of course you would be debugging
5496 elaboration code in your own application).
5498 @node Generating the Binder Program in C
5499 @section Generating the Binder Program in C
5500 @noindent
5501 In most normal usage, the default mode of @code{GNAT BIND} which is to
5502 generate the main package in Ada, as described in the previous section.
5503 In particular, this means that any Ada programmer can read and understand
5504 the generated main program. It can also be debugged just like any other
5505 Ada code provided the @code{-g} qualifier is used for @code{GNAT BIND}
5506 and @code{GNAT LINK}.
5508 However for some purposes it may be convenient to generate the main
5509 program in C rather than Ada. This may for example be helpful when you
5510 are generating a mixed language program with the main program in C. The
5511 GNAT compiler itself is an example. The use of the @code{-C} qualifier
5512 for both @code{GNAT BIND} and @code{GNAT LINK} will cause the program to
5513 be generated in C (and compiled using the gnu C compiler). The
5514 following shows the C code generated for the same "Hello World"
5515 program:
5517 @smallexample
5519 #ifdef __STDC__
5520 #define PARAMS(paramlist) paramlist
5521 #else
5522 #define PARAMS(paramlist) ()
5523 #endif
5525 extern void __gnat_set_globals
5526  PARAMS ((int, int, int, int, int, int,
5527           void (*) PARAMS ((void)), int, int));
5528 extern void adafinal PARAMS ((void));
5529 extern void adainit PARAMS ((void));
5530 extern void system__standard_library__adafinal PARAMS ((void));
5531 extern int main PARAMS ((int, char **, char **));
5532 extern void exit PARAMS ((int));
5533 extern void __gnat_break_start PARAMS ((void));
5534 extern void _ada_hello PARAMS ((void));
5535 extern void __gnat_initialize PARAMS ((void));
5536 extern void __gnat_finalize PARAMS ((void));
5538 extern void ada__exceptions___elabs PARAMS ((void));
5539 extern void system__exceptions___elabs PARAMS ((void));
5540 extern void interfaces__c_streams___elabs PARAMS ((void));
5541 extern void system__exception_table___elabb PARAMS ((void));
5542 extern void ada__io_exceptions___elabs PARAMS ((void));
5543 extern void system__stack_checking___elabs PARAMS ((void));
5544 extern void system__soft_links___elabb PARAMS ((void));
5545 extern void system__secondary_stack___elabb PARAMS ((void));
5546 extern void ada__tags___elabs PARAMS ((void));
5547 extern void ada__tags___elabb PARAMS ((void));
5548 extern void ada__streams___elabs PARAMS ((void));
5549 extern void system__finalization_root___elabs PARAMS ((void));
5550 extern void ada__exceptions___elabb PARAMS ((void));
5551 extern void system__finalization_implementation___elabs PARAMS ((void));
5552 extern void system__finalization_implementation___elabb PARAMS ((void));
5553 extern void ada__finalization___elabs PARAMS ((void));
5554 extern void ada__finalization__list_controller___elabs PARAMS ((void));
5555 extern void system__file_control_block___elabs PARAMS ((void));
5556 extern void system__file_io___elabb PARAMS ((void));
5557 extern void ada__text_io___elabs PARAMS ((void));
5558 extern void ada__text_io___elabb PARAMS ((void));
5560 extern int __gnat_inside_elab_final_code;
5562 extern int gnat_argc;
5563 extern char **gnat_argv;
5564 extern char **gnat_envp;
5565 extern int gnat_exit_status;
5567 char __gnat_version[] = "GNAT Version: 3.15w (20010315)";
5568 void adafinal () @{
5569    system__standard_library__adafinal ();
5572 void adainit ()
5574    extern char ada__exceptions_E;
5575    extern char system__exceptions_E;
5576    extern char interfaces__c_streams_E;
5577    extern char system__exception_table_E;
5578    extern char ada__io_exceptions_E;
5579    extern char system__secondary_stack_E;
5580    extern char system__stack_checking_E;
5581    extern char system__soft_links_E;
5582    extern char ada__tags_E;
5583    extern char ada__streams_E;
5584    extern char system__finalization_root_E;
5585    extern char system__finalization_implementation_E;
5586    extern char ada__finalization_E;
5587    extern char ada__finalization__list_controller_E;
5588    extern char system__file_control_block_E;
5589    extern char system__file_io_E;
5590    extern char ada__text_io_E;
5592    extern void *__gnat_hello__SDP;
5593    extern void *__gnat_ada__text_io__SDP;
5594    extern void *__gnat_ada__exceptions__SDP;
5595    extern void *__gnat_gnat__heap_sort_a__SDP;
5596    extern void *__gnat_system__exception_table__SDP;
5597    extern void *__gnat_system__machine_state_operations__SDP;
5598    extern void *__gnat_system__secondary_stack__SDP;
5599    extern void *__gnat_system__parameters__SDP;
5600    extern void *__gnat_system__soft_links__SDP;
5601    extern void *__gnat_system__stack_checking__SDP;
5602    extern void *__gnat_system__traceback__SDP;
5603    extern void *__gnat_ada__streams__SDP;
5604    extern void *__gnat_ada__tags__SDP;
5605    extern void *__gnat_system__string_ops__SDP;
5606    extern void *__gnat_interfaces__c_streams__SDP;
5607    extern void *__gnat_system__file_io__SDP;
5608    extern void *__gnat_ada__finalization__SDP;
5609    extern void *__gnat_system__finalization_root__SDP;
5610    extern void *__gnat_system__finalization_implementation__SDP;
5611    extern void *__gnat_system__string_ops_concat_3__SDP;
5612    extern void *__gnat_system__stream_attributes__SDP;
5613    extern void *__gnat_system__file_control_block__SDP;
5614    extern void *__gnat_ada__finalization__list_controller__SDP;
5616    void **st[23] = @{
5617      &__gnat_hello__SDP,
5618      &__gnat_ada__text_io__SDP,
5619      &__gnat_ada__exceptions__SDP,
5620      &__gnat_gnat__heap_sort_a__SDP,
5621      &__gnat_system__exception_table__SDP,
5622      &__gnat_system__machine_state_operations__SDP,
5623      &__gnat_system__secondary_stack__SDP,
5624      &__gnat_system__parameters__SDP,
5625      &__gnat_system__soft_links__SDP,
5626      &__gnat_system__stack_checking__SDP,
5627      &__gnat_system__traceback__SDP,
5628      &__gnat_ada__streams__SDP,
5629      &__gnat_ada__tags__SDP,
5630      &__gnat_system__string_ops__SDP,
5631      &__gnat_interfaces__c_streams__SDP,
5632      &__gnat_system__file_io__SDP,
5633      &__gnat_ada__finalization__SDP,
5634      &__gnat_system__finalization_root__SDP,
5635      &__gnat_system__finalization_implementation__SDP,
5636      &__gnat_system__string_ops_concat_3__SDP,
5637      &__gnat_system__stream_attributes__SDP,
5638      &__gnat_system__file_control_block__SDP,
5639      &__gnat_ada__finalization__list_controller__SDP@};
5641    extern void ada__exceptions___elabs ();
5642    extern void system__exceptions___elabs ();
5643    extern void interfaces__c_streams___elabs ();
5644    extern void system__exception_table___elabb ();
5645    extern void ada__io_exceptions___elabs ();
5646    extern void system__stack_checking___elabs ();
5647    extern void system__soft_links___elabb ();
5648    extern void system__secondary_stack___elabb ();
5649    extern void ada__tags___elabs ();
5650    extern void ada__tags___elabb ();
5651    extern void ada__streams___elabs ();
5652    extern void system__finalization_root___elabs ();
5653    extern void ada__exceptions___elabb ();
5654    extern void system__finalization_implementation___elabs ();
5655    extern void system__finalization_implementation___elabb ();
5656    extern void ada__finalization___elabs ();
5657    extern void ada__finalization__list_controller___elabs ();
5658    extern void system__file_control_block___elabs ();
5659    extern void system__file_io___elabb ();
5660    extern void ada__text_io___elabs ();
5661    extern void ada__text_io___elabb ();
5663    void (*ea[23]) () = @{
5664      adainit,
5665      system__standard_library__adafinal,
5666      ada__exceptions___elabs,
5667      system__exceptions___elabs,
5668      interfaces__c_streams___elabs,
5669      system__exception_table___elabb,
5670      ada__io_exceptions___elabs,
5671      system__stack_checking___elabs,
5672      system__soft_links___elabb,
5673      system__secondary_stack___elabb,
5674      ada__tags___elabs,
5675      ada__tags___elabb,
5676      ada__streams___elabs,
5677      system__finalization_root___elabs,
5678      ada__exceptions___elabb,
5679      system__finalization_implementation___elabs,
5680      system__finalization_implementation___elabb,
5681      ada__finalization___elabs,
5682      ada__finalization__list_controller___elabs,
5683      system__file_control_block___elabs,
5684      system__file_io___elabb,
5685      ada__text_io___elabs,
5686      ada__text_io___elabb@};
5688    __gnat_SDP_Table_Build (&st, 23, ea, 23);
5689    __gnat_set_globals (
5690       -1,      /* Main_Priority              */
5691       -1,      /* Time_Slice_Value           */
5692       'b',     /* WC_Encoding                */
5693       ' ',     /* Locking_Policy             */
5694       ' ',     /* Queuing_Policy             */
5695       ' ',     /* Tasking_Dispatching_Policy */
5696       0,       /* Finalization routine address, not used anymore */
5697       0,       /* Unreserve_All_Interrupts */
5698       0);      /* Exception_Tracebacks */
5700    __gnat_inside_elab_final_code = 1;
5702    if (ada__exceptions_E == 0) @{
5703       ada__exceptions___elabs ();
5704    @}
5705    if (system__exceptions_E == 0) @{
5706       system__exceptions___elabs ();
5707       system__exceptions_E++;
5708    @}
5709    if (interfaces__c_streams_E == 0) @{
5710       interfaces__c_streams___elabs ();
5711    @}
5712    interfaces__c_streams_E = 1;
5713    if (system__exception_table_E == 0) @{
5714       system__exception_table___elabb ();
5715       system__exception_table_E++;
5716    @}
5717    if (ada__io_exceptions_E == 0) @{
5718       ada__io_exceptions___elabs ();
5719       ada__io_exceptions_E++;
5720    @}
5721    if (system__stack_checking_E == 0) @{
5722       system__stack_checking___elabs ();
5723    @}
5724    if (system__soft_links_E == 0) @{
5725       system__soft_links___elabb ();
5726       system__soft_links_E++;
5727    @}
5728    system__stack_checking_E = 1;
5729    if (system__secondary_stack_E == 0) @{
5730       system__secondary_stack___elabb ();
5731       system__secondary_stack_E++;
5732    @}
5733    if (ada__tags_E == 0) @{
5734       ada__tags___elabs ();
5735    @}
5736    if (ada__tags_E == 0) @{
5737       ada__tags___elabb ();
5738       ada__tags_E++;
5739    @}
5740    if (ada__streams_E == 0) @{
5741       ada__streams___elabs ();
5742       ada__streams_E++;
5743    @}
5744    if (system__finalization_root_E == 0) @{
5745       system__finalization_root___elabs ();
5746    @}
5747    system__finalization_root_E = 1;
5748    if (ada__exceptions_E == 0) @{
5749       ada__exceptions___elabb ();
5750       ada__exceptions_E++;
5751    @}
5752    if (system__finalization_implementation_E == 0) @{
5753       system__finalization_implementation___elabs ();
5754    @}
5755    if (system__finalization_implementation_E == 0) @{
5756       system__finalization_implementation___elabb ();
5757       system__finalization_implementation_E++;
5758    @}
5759    if (ada__finalization_E == 0) @{
5760       ada__finalization___elabs ();
5761    @}
5762    ada__finalization_E = 1;
5763    if (ada__finalization__list_controller_E == 0) @{
5764       ada__finalization__list_controller___elabs ();
5765    @}
5766    ada__finalization__list_controller_E = 1;
5767    if (system__file_control_block_E == 0) @{
5768       system__file_control_block___elabs ();
5769       system__file_control_block_E++;
5770    @}
5771    if (system__file_io_E == 0) @{
5772       system__file_io___elabb ();
5773       system__file_io_E++;
5774    @}
5775    if (ada__text_io_E == 0) @{
5776       ada__text_io___elabs ();
5777    @}
5778    if (ada__text_io_E == 0) @{
5779       ada__text_io___elabb ();
5780       ada__text_io_E++;
5781    @}
5783    __gnat_inside_elab_final_code = 0;
5785 int main (argc, argv, envp)
5786     int argc;
5787     char **argv;
5788     char **envp;
5790    gnat_argc = argc;
5791    gnat_argv = argv;
5792    gnat_envp = envp;
5794    __gnat_initialize ();
5795    adainit ();
5796    __gnat_break_start ();
5798    _ada_hello ();
5800    system__standard_library__adafinal ();
5801    __gnat_finalize ();
5802    exit (gnat_exit_status);
5804 unsigned helloB = 0x7880BEB3;
5805 unsigned system__standard_libraryB = 0x0D24CBD0;
5806 unsigned system__standard_libraryS = 0x3283DBEB;
5807 unsigned adaS = 0x2359F9ED;
5808 unsigned ada__text_ioB = 0x47C85FC4;
5809 unsigned ada__text_ioS = 0x496FE45C;
5810 unsigned ada__exceptionsB = 0x74F50187;
5811 unsigned ada__exceptionsS = 0x6736945B;
5812 unsigned gnatS = 0x156A40CF;
5813 unsigned gnat__heap_sort_aB = 0x033DABE0;
5814 unsigned gnat__heap_sort_aS = 0x6AB38FEA;
5815 unsigned systemS = 0x0331C6FE;
5816 unsigned system__exceptionsS = 0x20C9ECA4;
5817 unsigned system__exception_tableB = 0x68A22947;
5818 unsigned system__exception_tableS = 0x394BADD5;
5819 unsigned gnat__htableB = 0x08258E1B;
5820 unsigned gnat__htableS = 0x367D5222;
5821 unsigned system__machine_state_operationsB = 0x4F3B7492;
5822 unsigned system__machine_state_operationsS = 0x182F5CF4;
5823 unsigned system__storage_elementsB = 0x2F1EB794;
5824 unsigned system__storage_elementsS = 0x102C83C7;
5825 unsigned system__secondary_stackB = 0x1574B6E9;
5826 unsigned system__secondary_stackS = 0x708E260A;
5827 unsigned system__parametersB = 0x56D770CD;
5828 unsigned system__parametersS = 0x237E39BE;
5829 unsigned system__soft_linksB = 0x08AB6B2C;
5830 unsigned system__soft_linksS = 0x1E2491F3;
5831 unsigned system__stack_checkingB = 0x476457A0;
5832 unsigned system__stack_checkingS = 0x5299FCED;
5833 unsigned system__tracebackB = 0x2971EBDE;
5834 unsigned system__tracebackS = 0x2E9C3122;
5835 unsigned ada__streamsS = 0x7C25DE96;
5836 unsigned ada__tagsB = 0x39ADFFA2;
5837 unsigned ada__tagsS = 0x769A0464;
5838 unsigned system__string_opsB = 0x5EB646AB;
5839 unsigned system__string_opsS = 0x63CED018;
5840 unsigned interfacesS = 0x0357E00A;
5841 unsigned interfaces__c_streamsB = 0x3784FB72;
5842 unsigned interfaces__c_streamsS = 0x2E723019;
5843 unsigned system__file_ioB = 0x623358EA;
5844 unsigned system__file_ioS = 0x31F873E6;
5845 unsigned ada__finalizationB = 0x6843F68A;
5846 unsigned ada__finalizationS = 0x63305874;
5847 unsigned system__finalization_rootB = 0x31E56CE1;
5848 unsigned system__finalization_rootS = 0x23169EF3;
5849 unsigned system__finalization_implementationB = 0x6CCBA70E;
5850 unsigned system__finalization_implementationS = 0x604AA587;
5851 unsigned system__string_ops_concat_3B = 0x572E3F58;
5852 unsigned system__string_ops_concat_3S = 0x01F57876;
5853 unsigned system__stream_attributesB = 0x1D4F93E8;
5854 unsigned system__stream_attributesS = 0x30B2EC3D;
5855 unsigned ada__io_exceptionsS = 0x34054F96;
5856 unsigned system__unsigned_typesS = 0x7B9E7FE3;
5857 unsigned system__file_control_blockS = 0x2FF876A8;
5858 unsigned ada__finalization__list_controllerB = 0x5760634A;
5859 unsigned ada__finalization__list_controllerS = 0x5D851835;
5861 /* BEGIN ELABORATION ORDER
5862 ada (spec)
5863 gnat (spec)
5864 gnat.heap_sort_a (spec)
5865 gnat.htable (spec)
5866 gnat.htable (body)
5867 interfaces (spec)
5868 system (spec)
5869 system.parameters (spec)
5870 system.standard_library (spec)
5871 ada.exceptions (spec)
5872 system.exceptions (spec)
5873 system.parameters (body)
5874 gnat.heap_sort_a (body)
5875 interfaces.c_streams (spec)
5876 interfaces.c_streams (body)
5877 system.exception_table (spec)
5878 system.exception_table (body)
5879 ada.io_exceptions (spec)
5880 system.storage_elements (spec)
5881 system.storage_elements (body)
5882 system.machine_state_operations (spec)
5883 system.machine_state_operations (body)
5884 system.secondary_stack (spec)
5885 system.stack_checking (spec)
5886 system.soft_links (spec)
5887 system.soft_links (body)
5888 system.stack_checking (body)
5889 system.secondary_stack (body)
5890 system.standard_library (body)
5891 system.string_ops (spec)
5892 system.string_ops (body)
5893 ada.tags (spec)
5894 ada.tags (body)
5895 ada.streams (spec)
5896 system.finalization_root (spec)
5897 system.finalization_root (body)
5898 system.string_ops_concat_3 (spec)
5899 system.string_ops_concat_3 (body)
5900 system.traceback (spec)
5901 system.traceback (body)
5902 ada.exceptions (body)
5903 system.unsigned_types (spec)
5904 system.stream_attributes (spec)
5905 system.stream_attributes (body)
5906 system.finalization_implementation (spec)
5907 system.finalization_implementation (body)
5908 ada.finalization (spec)
5909 ada.finalization (body)
5910 ada.finalization.list_controller (spec)
5911 ada.finalization.list_controller (body)
5912 system.file_control_block (spec)
5913 system.file_io (spec)
5914 system.file_io (body)
5915 ada.text_io (spec)
5916 ada.text_io (body)
5917 hello (body)
5918    END ELABORATION ORDER */
5920 /* BEGIN Object file/option list
5921 ./HELLO.OBJ
5922 -L./
5923 -L/usr/local/gnat/lib/gcc-lib/alpha-dec-osf5.1/2.8.1/adalib/
5924 /usr/local/gnat/lib/gcc-lib/alpha-dec-osf5.1/2.8.1/adalib/libgnat.a
5925 -lexc
5926    END Object file/option list */
5928 @end smallexample
5930 @noindent
5931 Here again, the C code is exactly what is generated by the binder. The
5932 functions of the various parts of this code correspond in an obvious
5933 manner with the commented Ada code shown in the example in the previous
5934 section.
5936 @node Consistency-Checking Modes
5937 @section Consistency-Checking Modes
5939 @noindent
5940 As described in the previous section, by default @code{GNAT BIND} checks
5941 that object files are consistent with one another and are consistent
5942 with any source files it can locate. The following qualifiers control binder
5943 access to sources.
5945 @table @code
5946 @item /READ_SOURCES=ALL
5947 @cindex @code{/READ_SOURCES=ALL} (@code{GNAT BIND})
5948 Require source files to be present. In this mode, the binder must be
5949 able to locate all source files that are referenced, in order to check
5950 their consistency. In normal mode, if a source file cannot be located it
5951 is simply ignored. If you specify this qualifier, a missing source
5952 file is an error.
5954 @item /READ_SOURCES=NONE
5955 @cindex @code{/READ_SOURCES=NONE} (@code{GNAT BIND})
5956 Exclude source files. In this mode, the binder only checks that ALI
5957 files are consistent with one another. Source files are not accessed.
5958 The binder runs faster in this mode, and there is still a guarantee that
5959 the resulting program is self-consistent.
5960 If a source file has been edited since it was last compiled, and you
5961 specify this qualifier, the binder will not detect that the object
5962 file is out of date with respect to the source file. Note that this is the
5963 mode that is automatically used by @code{GNAT MAKE} because in this
5964 case the checking against sources has already been performed by
5965 @code{GNAT MAKE} in the course of compilation (i.e. before binding).
5967 @item /READ_SOURCES=AVAILABLE
5968 This is the default mode in which source files are checked if they are
5969 available, and ignored if they are not available.
5970 @end table
5972 @node Binder Error Message Control
5973 @section Binder Error Message Control
5975 @noindent
5976 The following qualifiers provide control over the generation of error
5977 messages from the binder:
5979 @table @code
5980 @item /REPORT_ERRORS=VERBOSE
5981 @cindex @code{/REPORT_ERRORS=VERBOSE} (@code{GNAT BIND})
5982 Verbose mode. In the normal mode, brief error messages are generated to
5983 @file{SYS$ERROR}. If this qualifier is present, a header is written
5984 to @file{SYS$OUTPUT} and any error messages are directed to @file{SYS$OUTPUT}.
5985 All that is written to @file{SYS$ERROR} is a brief summary message.
5987 @item /REPORT_ERRORS=BRIEF
5988 @cindex @code{/REPORT_ERRORS=BRIEF} (@code{GNAT BIND})
5989 Generate brief error messages to @file{SYS$ERROR} even if verbose mode is
5990 specified. This is relevant only when used with the
5991 @code{/REPORT_ERRORS=VERBOSE} qualifier.
5994 @item /WARNINGS=SUPPRESS
5995 @cindex @code{/WARNINGS=SUPPRESS} (@code{GNAT BIND})
5996 @cindex Warnings
5997 Suppress all warning messages.
5999 @item /WARNINGS=ERROR
6000 @cindex @code{/WARNINGS=ERROR} (@code{GNAT BIND})
6001 Treat any warning messages as fatal errors.
6003 @item /WARNINGS=NORMAL
6004 Standard mode with warnings generated, but warnings do not get treated
6005 as errors.
6007 @item /NOTIME_STAMP_CHECK
6008 @cindex @code{/NOTIME_STAMP_CHECK} (@code{GNAT BIND})
6009 @cindex Time stamp checks, in binder
6010 @cindex Binder consistency checks
6011 @cindex Consistency checks, in binder
6012 The binder performs a number of consistency checks including:
6014 @itemize @bullet
6015 @item
6016 Check that time stamps of a given source unit are consistent
6017 @item
6018 Check that checksums of a given source unit are consistent
6019 @item
6020 Check that consistent versions of @code{GNAT} were used for compilation
6021 @item
6022 Check consistency of configuration pragmas as required
6023 @end itemize
6025 @noindent
6026 Normally failure of such checks, in accordance with the consistency
6027 requirements of the Ada Reference Manual, causes error messages to be
6028 generated which abort the binder and prevent the output of a binder
6029 file and subsequent link to obtain an executable.
6031 The @code{/NOTIME_STAMP_CHECK} qualifier converts these error messages
6032 into warnings, so that
6033 binding and linking can continue to completion even in the presence of such
6034 errors. The result may be a failed link (due to missing symbols), or a
6035 non-functional executable which has undefined semantics.
6036 @emph{This means that
6037 @code{/NOTIME_STAMP_CHECK} should be used only in unusual situations,
6038 with extreme care.}
6039 @end table
6041 @node Elaboration Control
6042 @section Elaboration Control
6044 @noindent
6045 The following qualifiers provide additional control over the elaboration
6046 order. For full details see @xref{Elaboration Order Handling in GNAT}.
6048 @table @code
6049 @item /PESSIMISTIC_ELABORATION
6050 @cindex @code{/PESSIMISTIC_ELABORATION} (@code{GNAT BIND})
6051 Normally the binder attempts to choose an elaboration order that is
6052 likely to minimize the likelihood of an elaboration order error resulting
6053 in raising a @code{Program_Error} exception. This qualifier reverses the
6054 action of the binder, and requests that it deliberately choose an order
6055 that is likely to maximize the likelihood of an elaboration error.
6056 This is useful in ensuring portability and avoiding dependence on
6057 accidental fortuitous elaboration ordering.
6059 Normally it only makes sense to use the @code{-p} qualifier if dynamic
6060 elaboration checking is used (@option{/CHECKS=ELABORATION} qualifier used for compilation).
6061 This is because in the default static elaboration mode, all necessary
6062 @code{Elaborate_All} pragmas are implicitly inserted. These implicit
6063 pragmas are still respected by the binder in @code{-p} mode, so a
6064 safe elaboration order is assured.
6065 @end table
6067 @node Output Control
6068 @section Output Control
6070 @noindent
6071 The following qualifiers allow additional control over the output
6072 generated by the binder.
6074 @table @code
6076 @item /BIND_FILE=ADA
6077 @cindex @code{/BIND_FILE=ADA} (@code{GNAT BIND})
6078 Generate binder program in Ada (default). The binder program is named
6079 @file{B$@var{mainprog}.ADB} by default. This can be changed with
6080 @code{-o} @code{GNAT BIND} option.
6082 @item /NOOUTPUT
6083 @cindex @code{/NOOUTPUT} (@code{GNAT BIND})
6084 Check only. Do not generate the binder output file. In this mode the
6085 binder performs all error checks but does not generate an output file.
6087 @item /BIND_FILE=C
6088 @cindex @code{/BIND_FILE=C} (@code{GNAT BIND})
6089 Generate binder program in C. The binder program is named
6090 @file{B_@var{mainprog}.C}. This can be changed with @code{-o} @code{GNAT BIND}
6091 option.
6093 @item /ELABORATION_DEPENDENCIES
6094 @cindex @code{/ELABORATION_DEPENDENCIES} (@code{GNAT BIND})
6095 Output complete list of elaboration-order dependencies, showing the
6096 reason for each dependency. This output can be rather extensive but may
6097 be useful in diagnosing problems with elaboration order. The output is
6098 written to @file{SYS$OUTPUT}.
6100 @item /HELP
6101 @cindex @code{/HELP} (@code{GNAT BIND})
6102 Output usage information. The output is written to @file{SYS$OUTPUT}.
6104 @item /LINKER_OPTION_LIST
6105 @cindex @code{/LINKER_OPTION_LIST} (@code{GNAT BIND})
6106 Output linker options to @file{SYS$OUTPUT}. Includes library search paths,
6107 contents of pragmas Ident and Linker_Options, and libraries added
6108 by @code{GNAT BIND}.
6110 @item /ORDER_OF_ELABORATION
6111 @cindex @code{/ORDER_OF_ELABORATION} (@code{GNAT BIND})
6112 Output chosen elaboration order. The output is written to @file{SYS$OUTPUT}.
6114 @item /OBJECT_LIST
6115 @cindex @code{/OBJECT_LIST} (@code{GNAT BIND})
6116 Output full names of all the object files that must be linked to provide
6117 the Ada component of the program. The output is written to @file{SYS$OUTPUT}.
6118 This list includes the files explicitly supplied and referenced by the user
6119 as well as implicitly referenced run-time unit files. The latter are
6120 omitted if the corresponding units reside in shared libraries. The
6121 directory names for the run-time units depend on the system configuration.
6123 @item /OUTPUT=@var{file}
6124 @cindex @code{/OUTPUT} (@code{GNAT BIND})
6125 Set name of output file to @var{file} instead of the normal
6126 @file{B$@var{mainprog}.ADB} default. Note that @var{file} denote the Ada
6127 binder generated body filename. In C mode you would normally give
6128 @var{file} an extension of @file{.C} because it will be a C source program.
6129 Note that if this option is used, then linking must be done manually.
6130 It is not possible to use GNAT LINK in this case, since it cannot locate
6131 the binder file.
6133 @item /RESTRICTION_LIST
6134 @cindex @code{/RESTRICTION_LIST} (@code{GNAT BIND})
6135 Generate list of @code{pragma Rerstrictions} that could be applied to
6136 the current unit. This is useful for code audit purposes, and also may
6137 be used to improve code generation in some cases.
6139 @end table
6141 @node Binding with Non-Ada Main Programs
6142 @section Binding with Non-Ada Main Programs
6144 @noindent
6145 In our description so far we have assumed that the main
6146 program is in Ada, and that the task of the binder is to generate a
6147 corresponding function @code{main} that invokes this Ada main
6148 program. GNAT also supports the building of executable programs where
6149 the main program is not in Ada, but some of the called routines are
6150 written in Ada and compiled using GNAT (@pxref{Mixed Language Programming}).
6151 The following qualifier is used in this situation:
6153 @table @code
6154 @item /NOMAIN
6155 @cindex @code{/NOMAIN} (@code{GNAT BIND})
6156 No main program. The main program is not in Ada.
6157 @end table
6159 @noindent
6160 In this case, most of the functions of the binder are still required,
6161 but instead of generating a main program, the binder generates a file
6162 containing the following callable routines:
6164 @table @code
6165 @item adainit
6166 @findex adainit
6167 You must call this routine to initialize the Ada part of the program by
6168 calling the necessary elaboration routines. A call to @code{adainit} is
6169 required before the first call to an Ada subprogram.
6171 Note that it is assumed that the basic execution environment must be setup
6172 to be appropriate for Ada execution at the point where the first Ada
6173 subprogram is called. In particular, if the Ada code will do any
6174 floating-point operations, then the FPU must be setup in an appropriate
6175 manner. For the case of the x86, for example, full precision mode is
6176 required. The procedure GNAT.Float_Control.Reset may be used to ensure
6177 that the FPU is in the right state.
6179 @item adafinal
6180 @findex adafinal
6181 You must call this routine to perform any library-level finalization
6182 required by the Ada subprograms. A call to @code{adafinal} is required
6183 after the last call to an Ada subprogram, and before the program
6184 terminates.
6185 @end table
6187 @noindent
6188 If the @code{/NOMAIN} qualifier
6189 @cindex Binder, multiple input files
6190 is given, more than one ALI file may appear on
6191 the command line for @code{GNAT BIND}. The normal @dfn{closure}
6192 calculation is performed for each of the specified units. Calculating
6193 the closure means finding out the set of units involved by tracing
6194 @code{with} references. The reason it is necessary to be able to
6195 specify more than one ALI file is that a given program may invoke two or
6196 more quite separate groups of Ada units.
6198 The binder takes the name of its output file from the last specified ALI
6199 file, unless overridden by the use of the @code{/OUTPUT=file}.
6200 The output is an Ada unit in source form that can
6201 be compiled with GNAT unless the -C qualifier is used in which case the
6202 output is a C source file, which must be compiled using the C compiler.
6203 This compilation occurs automatically as part of the @code{GNAT LINK}
6204 processing.
6206 Currently the GNAT run time requires a FPU using 80 bits mode
6207 precision. Under targets where this is not the default it is required to
6208 call GNAT.Float_Control.Reset before using floating point numbers (this
6209 include float computation, float input and output) in the Ada code. A
6210 side effect is that this could be the wrong mode for the foreign code
6211 where floating point computation could be broken after this call.
6213 @node Binding Programs with No Main Subprogram
6214 @section Binding Programs with No Main Subprogram
6216 @noindent
6217 It is possible to have an Ada program which does not have a main
6218 subprogram. This program will call the elaboration routines of all the
6219 packages, then the finalization routines.
6221 The following qualifier is used to bind programs organized in this manner:
6223 @table @code
6224 @item /ZERO_MAIN
6225 @cindex @code{/ZERO_MAIN} (@code{GNAT BIND})
6226 Normally the binder checks that the unit name given on the command line
6227 corresponds to a suitable main subprogram. When this qualifier is used,
6228 a list of ALI files can be given, and the execution of the program
6229 consists of elaboration of these units in an appropriate order.
6230 @end table
6232 @node Summary of Binder Qualifiers
6233 @section Summary of Binder Qualifiers
6235 @noindent
6236 The following are the qualifiers available with @code{GNAT BIND}:
6238 @table @code
6239 @item /OBJECT_SEARCH
6240 Specify directory to be searched for ALI files.
6242 @item /SOURCE_SEARCH
6243 Specify directory to be searched for source file.
6245 @item /BIND_FILE=ADA
6246 Generate binder program in Ada (default)
6248 @item /REPORT_ERRORS=BRIEF
6249 Generate brief messages to @file{SYS$ERROR} even if verbose mode set.
6251 @item /NOOUTPUT
6252 Check only, no generation of binder output file.
6254 @item /BIND_FILE=C
6255 Generate binder program in C
6257 @item /ELABORATION_DEPENDENCIES
6258 Output complete list of elaboration-order dependencies.
6260 @item -E
6261 Store tracebacks in exception occurrences when the target supports it.
6262 This is the default with the zero cost exception mechanism.
6263 This option is currently supported on the following targets:
6264 all x86 ports, Solaris, Windows, HP-UX, AIX, PowerPC VxWorks and Alpha VxWorks.
6265 See also the packages @code{GNAT.Traceback} and
6266 @code{GNAT.Traceback.Symbolic} for more information.
6267 Note that on x86 ports, you must not use @code{-fomit-frame-pointer}
6268 @code{GNAT COMPILE} option.
6270 @item -h
6271 Output usage (help) information
6273 @item /SEARCH
6274 Specify directory to be searched for source and ALI files.
6276 @item /NOCURRENT_DIRECTORY
6277 Do not look for sources in the current directory where @code{GNAT BIND} was
6278 invoked, and do not look for ALI files in the directory containing the
6279 ALI file named in the @code{GNAT BIND} command line.
6281 @item /ORDER_OF_ELABORATION
6282 Output chosen elaboration order.
6284 @item -Lxxx
6285 Binds the units for library building. In this case the adainit and
6286 adafinal procedures (See @pxref{Binding with Non-Ada Main Programs})
6287 are renamed to xxxinit and xxxfinal. Implies -n.
6289 @item -Mxyz
6290 Rename generated main program from main to xyz
6292 @item /ERROR_LIMIT=@var{n}
6293 Limit number of detected errors to @var{n} (1-999).
6295 @item /NOMAIN
6296 No main program.
6298 @item /NOSTD_INCLUDES
6299 Do not look for sources in the system default directory.
6301 @item /NOSTD_LIBRARIES
6302 Do not look for library files in the system default directory.
6304 @item /RUNTIME_SYSTEM=@var{rts-path}
6305 @cindex @code{/RUNTIME_SYSTEM} (@code{GNAT BIND})
6306 Specifies the default location of the runtime library. Same meaning as the
6307 equivalent @code{GNAT MAKE} flag (see @ref{Qualifiers for GNAT MAKE}).
6309 @item /OUTPUT=@var{file}
6310 Name the output file @var{file} (default is @file{B$@var{xxx}.ADB}).
6311 Note that if this option is used, then linking must be done manually,
6312 GNAT LINK cannot be used.
6314 @item /OBJECT_LIST
6315 Output object list.
6317 @item -p
6318 Pessimistic (worst-case) elaboration order
6320 @item /READ_SOURCES=ALL
6321 Require all source files to be present.
6324 @item /NOTIME_STAMP_CHECK
6325 Tolerate time stamp and other consistency errors
6327 @item -T@var{n}
6328 Set the time slice value to n microseconds. A value of zero means no time
6329 slicing and also indicates to the tasking run time to match as close as
6330 possible to the annex D requirements of the RM.
6332 @item /REPORT_ERRORS=VERBOSE
6333 Verbose mode. Write error messages, header, summary output to
6334 @file{SYS$OUTPUT}.
6337 @item /WARNINGS=NORMAL
6338 Normal warnings mode. Warnings are issued but ignored
6340 @item /WARNINGS=SUPPRESS
6341 All warning messages are suppressed
6343 @item /WARNINGS=ERROR
6344 Warning messages are treated as fatal errors
6346 @item /READ_SOURCES=NONE
6347 Exclude source files (check object consistency only).
6349 @item /READ_SOURCES=AVAILABLE
6350 Default mode, in which sources are checked for consistency only if
6351 they are available.
6353 @item /ZERO_MAIN
6354 No main subprogram.
6356 @end table
6359 @node Command-Line Access
6360 @section Command-Line Access
6362 @noindent
6363 The package @code{Ada.Command_Line} provides access to the command-line
6364 arguments and program name. In order for this interface to operate
6365 correctly, the two variables
6367 @smallexample
6368 @group
6369 @cartouche
6370 int gnat_argc;
6371 char **gnat_argv;
6372 @end cartouche
6373 @end group
6374 @end smallexample
6376 @noindent
6377 @findex gnat_argv
6378 @findex gnat_argc
6379 are declared in one of the GNAT library routines. These variables must
6380 be set from the actual @code{argc} and @code{argv} values passed to the
6381 main program. With no @code{/NOMAIN} present, @code{GNAT BIND}
6382 generates the C main program to automatically set these variables.
6383 If the @code{/NOMAIN} qualifier is used, there is no automatic way to
6384 set these variables. If they are not set, the procedures in
6385 @code{Ada.Command_Line} will not be available, and any attempt to use
6386 them will raise @code{Constraint_Error}. If command line access is
6387 required, your main program must set @code{gnat_argc} and
6388 @code{gnat_argv} from the @code{argc} and @code{argv} values passed to
6391 @node Search Paths for GNAT BIND
6392 @section Search Paths for @code{GNAT BIND}
6394 @noindent
6395 The binder takes the name of an ALI file as its argument and needs to
6396 locate source files as well as other ALI files to verify object consistency.
6398 For source files, it follows exactly the same search rules as @code{GNAT COMPILE}
6399 (@pxref{Search Paths and the Run-Time Library (RTL)}). For ALI files the
6400 directories searched are:
6402 @enumerate
6403 @item
6404 The directory containing the ALI file named in the command line, unless
6405 the qualifier @code{/NOCURRENT_DIRECTORY} is specified.
6407 @item
6408 All directories specified by @code{/SEARCH}
6409 qualifiers on the @code{GNAT BIND}
6410 command line, in the order given.
6412 @item
6413 @findex ADA_OBJECTS_PATH
6414 Each of the directories listed in the value of the
6415 @code{ADA_OBJECTS_PATH} logical name.
6416 Normally, define this value as a logical name containing a comma separated
6417 list of directory names.
6419 This variable can also be defined by means of an environment string
6420 (an argument to the DEC C exec* set of functions).
6422 Logical Name:
6423 @smallexample
6424 DEFINE ANOTHER_PATH FOO:[BAG]
6425 DEFINE ADA_OBJECTS_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
6426 @end smallexample
6428 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
6429 first, followed by the standard Ada 95
6430 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADALIB].
6431 If this is not redefined, the user will obtain the DEC Ada83 IO packages
6432 (Text_IO, Sequential_IO, etc)
6433 instead of the Ada95 packages. Thus, in order to get the Ada 95
6434 packages by default, ADA_OBJECTS_PATH must be redefined.
6436 @item
6437 The content of the "ada_object_path" file which is part of the GNAT
6438 installation tree and is used to store standard libraries such as the
6439 GNAT Run Time Library (RTL) unless the qualifier @code{/NOSTD_LIBRARIES} is
6440 specified.
6441 @end enumerate
6443 @noindent
6444 In the binder the qualifier @code{/SEARCH}
6445 is used to specify both source and
6446 library file paths. Use @code{/SOURCE_SEARCH}
6447 instead if you want to specify
6448 source paths only, and @code{/LIBRARY_SEARCH}
6449 if you want to specify library paths
6450 only. This means that for the binder
6451 @code{/SEARCH=}@var{dir} is equivalent to
6452 @code{/SOURCE_SEARCH=}@var{dir}
6453 @code{/OBJECT_SEARCH=}@var{dir}.
6454 The binder generates the bind file (a C language source file) in the
6455 current working directory.
6457 @findex Ada
6458 @findex System
6459 @findex Interfaces
6460 @findex GNAT
6461 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
6462 children make up the GNAT Run-Time Library, together with the package
6463 GNAT and its children, which contain a set of useful additional
6464 library functions provided by GNAT. The sources for these units are
6465 needed by the compiler and are kept together in one directory. The ALI
6466 files and object files generated by compiling the RTL are needed by the
6467 binder and the linker and are kept together in one directory, typically
6468 different from the directory containing the sources. In a normal
6469 installation, you need not specify these directory names when compiling
6470 or binding. Either the environment variables or the built-in defaults
6471 cause these files to be found.
6473 Besides simplifying access to the RTL, a major use of search paths is
6474 in compiling sources from multiple directories. This can make
6475 development environments much more flexible.
6477 @node Examples of GNAT BIND Usage
6478 @section Examples of @code{GNAT BIND} Usage
6480 @noindent
6481 This section contains a number of examples of using the GNAT binding
6482 utility @code{GNAT BIND}.
6484 @table @code
6485 @item GNAT BIND hello
6486 The main program @code{Hello} (source program in @file{HELLO.ADB}) is
6487 bound using the standard qualifier settings. The generated main program is
6488 @file{B~HELLO.ADB}. This is the normal, default use of the binder.
6490 @item GNAT BIND HELLO.ALI /OUTPUT=Mainprog.ADB
6491 The main program @code{Hello} (source program in @file{HELLO.ADB}) is
6492 bound using the standard qualifier settings. The generated main program is
6493 @file{MAINPROG.ADB} with the associated spec in
6494 @file{MAINPROG.ADS}. Note that you must specify the body here not the
6495 spec, in the case where the output is in Ada. Note that if this option
6496 is used, then linking must be done manually, since GNAT LINK will not
6497 be able to find the generated file.
6499 @item GNAT BIND MAIN.ALI /BIND_FILE=C /OUTPUT=Mainprog.C /READ_SOURCES=NONE
6500 The main program @code{Main} (source program in
6501 @file{MAIN.ADB}) is bound, excluding source files from the
6502 consistency checking, generating
6503 the file @file{MAINPROG.C}.
6506 @item GNAT BIND /NOMAIN math dbase /BIND_FILE=C /OUTPUT=ADA-CONTROL.C
6507 The main program is in a language other than Ada, but calls to
6508 subprograms in packages @code{Math} and @code{Dbase} appear. This call
6509 to @code{GNAT BIND} generates the file @file{ADA-CONTROL.C} containing
6510 the @code{adainit} and @code{adafinal} routines to be called before and
6511 after accessing the Ada units.
6512 @end table
6514 @node Linking Using GNAT LINK
6515 @chapter Linking Using @code{GNAT LINK}
6516 @findex GNAT LINK
6518 @noindent
6519 This chapter discusses @code{GNAT LINK}, a utility program used to link
6520 Ada programs and build an executable file. This is a simple program
6521 that invokes the Unix linker (via the @code{GNAT COMPILE}
6522 command) with a correct list of object files and library references.
6523 @code{GNAT LINK} automatically determines the list of files and
6524 references for the Ada part of a program. It uses the binder file
6525 generated by the binder to determine this list.
6527 @menu
6528 * Running GNAT LINK::
6529 * Qualifiers for GNAT LINK::
6530 * Setting Stack Size from GNAT LINK::
6531 * Setting Heap Size from GNAT LINK::
6532 @end menu
6534 @node Running GNAT LINK
6535 @section Running @code{GNAT LINK}
6537 @noindent
6538 The form of the @code{GNAT LINK} command is
6540 @smallexample
6541 $ GNAT LINK [@var{qualifiers}] @var{mainprog}[.ALI] [@var{non-Ada objects}]
6542       [@var{linker options}]
6543 @end smallexample
6545 @noindent
6546 @file{@var{mainprog}.ALI} references the ALI file of the main program.
6547 The @file{.ALI} extension of this file can be omitted. From this
6548 reference, @code{GNAT LINK} locates the corresponding binder file
6549 @file{B$@var{mainprog}.ADB} and, using the information in this file along
6550 with the list of non-Ada objects and linker options, constructs a Unix
6551 linker command file to create the executable.
6553 The arguments following @file{@var{mainprog}.ALI} are passed to the
6554 linker uninterpreted. They typically include the names of object files
6555 for units written in other languages than Ada and any library references
6556 required to resolve references in any of these foreign language units,
6557 or in @code{pragma Import} statements in any Ada units.
6559 @var{linker options} is an optional list of linker specific
6560 qualifiers. The default linker called by GNAT LINK is @var{GNAT COMPILE} which in
6561 turn calls the appropriate system linker usually called
6562 @var{ld}. Standard options for the linker such as @code{-lmy_lib} or
6563 @code{-Ldir} can be added as is. For options that are not recognized by
6564 @var{GNAT COMPILE} as linker options, the @var{GNAT COMPILE} qualifiers @code{-Xlinker} or
6565 @code{-Wl,} shall be used. Refer to the GCC documentation for
6566 details. Here is an example showing how to generate a linker map
6567 assuming that the underlying linker is GNU ld:
6569 @smallexample
6570 $ GNAT LINK my_prog -Wl,-Map,MAPFILE
6571 @end smallexample
6573 Using @var{linker options} it is possible to set the program stack and
6574 heap size. See @pxref{Setting Stack Size from GNAT LINK} and
6575 @pxref{Setting Heap Size from GNAT LINK}.
6577 @code{GNAT LINK} determines the list of objects required by the Ada
6578 program and prepends them to the list of objects passed to the linker.
6579 @code{GNAT LINK} also gathers any arguments set by the use of
6580 @code{pragma Linker_Options} and adds them to the list of arguments
6581 presented to the linker.
6583 @code{GNAT LINK} accepts the following types of extra files on the command
6584 line: objects (.OBJ), libraries (.OLB), shareable images (.EXE), and
6585 options files (.OPT). These are recognized and handled according to their
6586 extension.
6588 @node Qualifiers for GNAT LINK
6589 @section Qualifiers for @code{GNAT LINK}
6591 @noindent
6592 The following qualifiers are available with the @code{GNAT LINK} utility:
6594 @table @code
6596 @item /BIND_FILE=ADA
6597 @cindex @code{/BIND_FILE=ADA} (@code{GNAT LINK})
6598 The binder has generated code in Ada. This is the default.
6600 @item /BIND_FILE=C
6601 @cindex @code{/BIND_FILE=C} (@code{GNAT LINK})
6602 If instead of generating a file in Ada, the binder has generated one in
6603 C, then the linker needs to know about it. Use this qualifier to signal
6604 to @code{GNAT LINK} that the binder has generated C code rather than
6605 Ada code.
6607 @item -f
6608 @cindex Command line length
6609 @cindex @code{-f} (@code{GNAT LINK})
6610 On some targets, the command line length is limited, and @code{GNAT LINK}
6611 will generate a separate file for the linker if the list of object files
6612 is too long. The @code{-f} flag forces this file to be generated even if
6613 the limit is not exceeded. This is useful in some cases to deal with
6614 special situations where the command line length is exceeded.
6616 @item /DEBUG
6617 @cindex Debugging information, including
6618 @cindex @code{/DEBUG} (@code{GNAT LINK})
6619 The option to include debugging information causes the Ada bind file (in
6620 other words, @file{B$@var{mainprog}.ADB}) to be compiled with
6621 @code{/DEBUG}.
6622 In addition, the binder does not delete the @file{B$@var{mainprog}.ADB},
6623 @file{B$@var{mainprog}.OBJ} and @file{B$@var{mainprog}.ALI} files.
6624 Without @code{/DEBUG}, the binder removes these files by
6625 default. The same procedure apply if a C bind file was generated using
6626 @code{/BIND_FILE=C} @code{GNAT BIND} option, in this case the filenames are
6627 @file{B_@var{mainprog}.C} and @file{B_@var{mainprog}.OBJ}.
6630 @item /VERBOSE
6631 @cindex @code{/VERBOSE} (@code{GNAT LINK})
6632 Causes additional information to be output, including a full list of the
6633 included object files. This qualifier option is most useful when you want
6634 to see what set of object files are being used in the link step.
6637 @item /EXECUTABLE=@var{exec-name}
6638 @cindex @code{/EXECUTABLE} (@code{GNAT LINK})
6639 @var{exec-name} specifies an alternate name for the generated
6640 executable program. If this qualifier is omitted, the executable has the same
6641 name as the main unit. For example, @code{GNAT LINK TRY.ALI} creates
6642 an executable called @file{TRY.EXE}.
6645 @item /DEBUG=TRACEBACK
6646 @cindex @code{/DEBUG=TRACEBACK} (@code{GNAT LINK})
6647 This qualifier causes sufficient information to be included in the
6648 executable file to allow a traceback, but does not include the full
6649 symbol information needed by the debugger.
6651 @item /IDENTIFICATION="<string>"
6652 "<string>" specifies the string to be stored in the image file identification
6653 field in the image header. It overrides any pragma Ident specified string.
6655 @item /NOINHIBIT-EXEC
6656 Generate the executable file even if there are linker warnings.
6658 @item /NOSTART_FILES
6659 Don't link in the object file containing the "main" transfer address.
6660 Used when linking with a foreign language main program compiled with a
6661 Digital compiler.
6663 @item /STATIC
6664 Prefer linking with object libraries over shareable images, even without
6665 /DEBUG.
6667 @end table
6669 @node Setting Stack Size from GNAT LINK
6670 @section Setting Stack Size from @code{GNAT LINK}
6672 @noindent
6673 It is possible to specify the program stack size from @code{GNAT LINK}.
6674 Assuming that the underlying linker is GNU ld there is two ways to do so:
6676 @itemize @bullet
6678 @item using @code{-Xlinker} linker option
6680 @smallexample
6681 $ GNAT LINK hello -Xlinker --stack=0x10000,0x1000
6682 @end smallexample
6684 This set the stack reserve size to 0x10000 bytes and the stack commit
6685 size to 0x1000 bytes.
6687 @item using @code{-Wl} linker option
6689 @smallexample
6690 $ GNAT LINK hello -Wl,--stack=0x1000000
6691 @end smallexample
6693 This set the stack reserve size to 0x1000000 bytes. Note that with
6694 @code{-Wl} option it is not possible to set the stack commit size
6695 because the coma is a separator for this option.
6697 @end itemize
6699 @node Setting Heap Size from GNAT LINK
6700 @section Setting Heap Size from @code{GNAT LINK}
6702 @noindent
6703 It is possible to specify the program heap size from @code{GNAT LINK}.
6704 Assuming that the underlying linker is GNU ld there is two ways to do so:
6706 @itemize @bullet
6708 @item using @code{-Xlinker} linker option
6710 @smallexample
6711 $ GNAT LINK hello -Xlinker --heap=0x10000,0x1000
6712 @end smallexample
6714 This set the heap reserve size to 0x10000 bytes and the heap commit
6715 size to 0x1000 bytes.
6717 @item using @code{-Wl} linker option
6719 @smallexample
6720 $ GNAT LINK hello -Wl,--heap=0x1000000
6721 @end smallexample
6723 This set the heap reserve size to 0x1000000 bytes. Note that with
6724 @code{-Wl} option it is not possible to set the heap commit size
6725 because the coma is a separator for this option.
6727 @end itemize
6729 @node The GNAT Make Program GNAT MAKE
6730 @chapter The GNAT Make Program @code{GNAT MAKE}
6731 @findex GNAT MAKE
6733 @menu
6734 * Running GNAT MAKE::
6735 * Qualifiers for GNAT MAKE::
6736 * Mode Qualifiers for GNAT MAKE::
6737 * Notes on the Command Line::
6738 * How GNAT MAKE Works::
6739 * Examples of GNAT MAKE Usage::
6740 @end menu
6741 @noindent
6742 A typical development cycle when working on an Ada program consists of
6743 the following steps:
6745 @enumerate
6746 @item
6747 Edit some sources to fix bugs.
6749 @item
6750 Add enhancements.
6752 @item
6753 Compile all sources affected.
6755 @item
6756 Rebind and relink.
6758 @item
6759 Test.
6760 @end enumerate
6762 @noindent
6763 The third step can be tricky, because not only do the modified files
6764 @cindex Dependency rules
6765 have to be compiled, but any files depending on these files must also be
6766 recompiled. The dependency rules in Ada can be quite complex, especially
6767 in the presence of overloading, @code{use} clauses, generics and inlined
6768 subprograms.
6770 @code{GNAT MAKE} automatically takes care of the third and fourth steps
6771 of this process. It determines which sources need to be compiled,
6772 compiles them, and binds and links the resulting object files.
6774 Unlike some other Ada make programs, the dependencies are always
6775 accurately recomputed from the new sources. The source based approach of
6776 the GNAT compilation model makes this possible. This means that if
6777 changes to the source program cause corresponding changes in
6778 dependencies, they will always be tracked exactly correctly by
6779 @code{GNAT MAKE}.
6781 @node Running GNAT MAKE
6782 @section Running @code{GNAT MAKE}
6784 @noindent
6785 The usual form of the @code{GNAT MAKE} command is
6787 @smallexample
6788 $ GNAT MAKE [@var{qualifiers}] @var{file_name} [@var{file_names}] [@var{mode_qualifiers}]
6789 @end smallexample
6791 @noindent
6792 The only required argument is one @var{file_name}, which specifies
6793 a compilation unit that is a main program. Several @var{file_names} can be
6794 specified: this will result in several executables being built.
6795 If @code{qualifiers} are present, they can be placed before the first
6796 @var{file_name}, between @var{file_names} or after the last @var{file_name}.
6797 If @var{mode_qualifiers} are present, they must always be placed after
6798 the last @var{file_name} and all @code{qualifiers}.
6800 If you are using standard file extensions (.ADB and .ADS), then the
6801 extension may be omitted from the @var{file_name} arguments. However, if
6802 you are using non-standard extensions, then it is required that the
6803 extension be given. A relative or absolute directory path can be
6804 specified in a @var{file_name}, in which case, the input source file will
6805 be searched for in the specified directory only. Otherwise, the input
6806 source file will first be searched in the directory where
6807 @code{GNAT MAKE} was invoked and if it is not found, it will be search on
6808 the source path of the compiler as described in
6809 @ref{Search Paths and the Run-Time Library (RTL)}.
6811 When several @var{file_names} are specified, if an executable needs to be
6812 rebuilt and relinked, all subsequent executables will be rebuilt and
6813 relinked, even if this would not be absolutely necessary.
6815 All @code{GNAT MAKE} output (except when you specify
6816 @code{/DEPENDENCIES_LIST}) is to
6817 @file{SYS$ERROR}. The output produced by the
6818 @code{/DEPENDENCIES_LIST} qualifier is send to
6819 @file{SYS$OUTPUT}.
6821 @node Qualifiers for GNAT MAKE
6822 @section Qualifiers for @code{GNAT MAKE}
6824 @noindent
6825 You may specify any of the following qualifiers to @code{GNAT MAKE}:
6827 @table @code
6829 @item /ALL_FILES
6830 @cindex @code{/ALL_FILES} (@code{GNAT MAKE})
6831 Consider all files in the make process, even the GNAT internal system
6832 files (for example, the predefined Ada library files), as well as any
6833 locked files. Locked files are files whose ALI file is write-protected.
6834 By default,
6835 @code{GNAT MAKE} does not check these files,
6836 because the assumption is that the GNAT internal files are properly up
6837 to date, and also that any write protected ALI files have been properly
6838 installed. Note that if there is an installation problem, such that one
6839 of these files is not up to date, it will be properly caught by the
6840 binder.
6841 You may have to specify this qualifier if you are working on GNAT
6842 itself. @code{/ALL_FILES} is also useful in conjunction with
6843 @code{/FORCE_COMPILE}
6844 if you need to recompile an entire application,
6845 including run-time files, using special configuration pragma settings,
6846 such as a non-standard @code{Float_Representation} pragma.
6847 By default
6848 @code{GNAT MAKE /ALL_FILES} compiles all GNAT
6849 internal files with
6850 the @code{/CHECKS=SUPPRESS_ALL /STYLE_CHECKS=GNAT} qualifier.
6852 @item /ACTIONS=BIND
6853 @cindex @code{/ACTIONS=BIND} (@code{GNAT MAKE})
6854 Bind only. Can be combined with @code{/ACTIONS=COMPILE} to do compilation
6855 and binding, but no link. Can be combined with @code{/ACTIONS=LINK}
6856 to do binding and linking. When not combined with @code{/ACTIONS=COMPILE}
6857 all the units in the closure of the main program must have been previously
6858 compiled and must be up to date. The root unit specified by @var{file_name}
6859 may be given without extension, with the source extension or, if no GNAT
6860 Project File is specified, with the ALI file extension.
6862 @item /ACTIONS=COMPILE
6863 @cindex @code{/ACTIONS=COMPILE} (@code{GNAT MAKE})
6864 Compile only. Do not perform binding, except when @code{/ACTIONS=BIND}
6865 is also specified. Do not perform linking, except if both
6866 @code{/ACTIONS=BIND} and
6867  @code{/ACTIONS=LINK} are also specified.
6868 If the root unit specified by @var{file_name} is not a main unit, this is the
6869 default. Otherwise @code{GNAT MAKE} will attempt binding and linking
6870 unless all objects are up to date and the executable is more recent than
6871 the objects.
6873 @item /MAPPING
6874 @cindex @code{/MAPPING} (@code{GNAT MAKE})
6875 Use a mapping file. A mapping file is a way to communicate to the compiler
6876 two mappings: from unit names to file names (without any directory information)
6877 and from file names to path names (with full directory information).
6878 These mappings are used by the compiler to short-circuit the path search.
6879 When @code{GNAT MAKE} is invoked with this qualifier, it will create a mapping
6880 file, initially populated by the project manager, if @code{-P} is used,
6881 otherwise initially empty. Each invocation of the compiler will add the newly
6882 accessed sources to the mapping file. This will improve the source search
6883 during the next invocation of the compiler.
6885 @item /FORCE_COMPILE
6886 @cindex @code{/FORCE_COMPILE} (@code{GNAT MAKE})
6887 Force recompilations. Recompile all sources, even though some object
6888 files may be up to date, but don't recompile predefined or GNAT internal
6889 files or locked files (files with a write-protected ALI file),
6890 unless the @code{/ALL_FILES} qualifier is also specified.
6892 @item
6893 @item /IN_PLACE
6894 @cindex @code{/IN_PLACE} (@code{GNAT MAKE})
6895 In normal mode, @code{GNAT MAKE} compiles all object files and ALI files
6896 into the current directory. If the @code{/IN_PLACE} qualifier is used,
6897 then instead object files and ALI files that already exist are overwritten
6898 in place. This means that once a large project is organized into separate
6899 directories in the desired manner, then @code{GNAT MAKE} will automatically
6900 maintain and update this organization. If no ALI files are found on the
6901 Ada object path (@ref{Search Paths and the Run-Time Library (RTL)}),
6902 the new object and ALI files are created in the
6903 directory containing the source being compiled. If another organization
6904 is desired, where objects and sources are kept in different directories,
6905 a useful technique is to create dummy ALI files in the desired directories.
6906 When detecting such a dummy file, @code{GNAT MAKE} will be forced to recompile
6907 the corresponding source file, and it will be put the resulting object
6908 and ALI files in the directory where it found the dummy file.
6910 @item /PROCESSES=@var{n}
6911 @cindex @code{/PROCESSES} (@code{GNAT MAKE})
6912 @cindex Parallel make
6913 Use @var{n} processes to carry out the (re)compilations. On a
6914 multiprocessor machine compilations will occur in parallel. In the
6915 event of compilation errors, messages from various compilations might
6916 get interspersed (but @code{GNAT MAKE} will give you the full ordered
6917 list of failing compiles at the end). If this is problematic, rerun
6918 the make process with n set to 1 to get a clean list of messages.
6920 @item /CONTINUE_ON_ERROR
6921 @cindex @code{/CONTINUE_ON_ERROR} (@code{GNAT MAKE})
6922 Keep going. Continue as much as possible after a compilation error. To
6923 ease the programmer's task in case of compilation errors, the list of
6924 sources for which the compile fails is given when @code{GNAT MAKE}
6925 terminates.
6927 If @code{GNAT MAKE} is invoked with several @file{file_names} and with this
6928 qualifier, if there are compilation errors when building an executable,
6929 @code{GNAT MAKE} will not attempt to build the following executables.
6931 @item /ACTIONS=LINK
6932 @cindex @code{/ACTIONS=LINK} (@code{GNAT MAKE})
6933 Link only. Can be combined with @code{/ACTIONS=BIND} to binding
6934 and linking. Linking will not be performed if combined with
6935 @code{/ACTIONS=COMPILE}
6936 but not with @code{/ACTIONS=BIND}.
6937 When not combined with @code{/ACTIONS=BIND}
6938 all the units in the closure of the main program must have been previously
6939 compiled and must be up to date, and the main program need to have been bound.
6940 The root unit specified by @var{file_name}
6941 may be given without extension, with the source extension or, if no GNAT
6942 Project File is specified, with the ALI file extension.
6944 @item /MINIMAL_RECOMPILATION
6945 @cindex @code{/MINIMAL_RECOMPILATION} (@code{GNAT MAKE})
6946 Specifies that the minimum necessary amount of recompilations
6947 be performed. In this mode @code{GNAT MAKE} ignores time
6948 stamp differences when the only
6949 modifications to a source file consist in adding/removing comments,
6950 empty lines, spaces or tabs. This means that if you have changed the
6951 comments in a source file or have simply reformatted it, using this
6952 qualifier will tell GNAT MAKE not to recompile files that depend on it
6953 (provided other sources on which these files depend have undergone no
6954 semantic modifications). Note that the debugging information may be
6955 out of date with respect to the sources if the @code{-m} qualifier causes
6956 a compilation to be switched, so the use of this qualifier represents a
6957 trade-off between compilation time and accurate debugging information.
6959 @item /DEPENDENCIES_LIST
6960 @cindex Dependencies, producing list
6961 @cindex @code{/DEPENDENCIES_LIST} (@code{GNAT MAKE})
6962 Check if all objects are up to date. If they are, output the object
6963 dependences to @file{SYS$OUTPUT} in a form that can be directly exploited in
6964 a @file{Makefile}. By default, each source file is prefixed with its
6965 (relative or absolute) directory name. This name is whatever you
6966 specified in the various @code{/SOURCE_SEARCH}
6967 and @code{/SEARCH} qualifiers. If you use
6968 @code{GNAT MAKE /DEPENDENCIES_LIST}
6969 @code{/QUIET}
6970 (see below), only the source file names,
6971 without relative paths, are output. If you just specify the
6972 @code{/DEPENDENCIES_LIST}
6973 qualifier, dependencies of the GNAT internal system files are omitted. This
6974 is typically what you want. If you also specify
6975 the @code{/ALL_FILES} qualifier,
6976 dependencies of the GNAT internal files are also listed. Note that
6977 dependencies of the objects in external Ada libraries (see qualifier
6978 @code{/SKIP_MISSING=}@var{dir} in the following list) are never reported.
6980 @item /DO_OBJECT_CHECK
6981 @cindex @code{/DO_OBJECT_CHECK} (@code{GNAT MAKE})
6982 Don't compile, bind, or link. Checks if all objects are up to date.
6983 If they are not, the full name of the first file that needs to be
6984 recompiled is printed.
6985 Repeated use of this option, followed by compiling the indicated source
6986 file, will eventually result in recompiling all required units.
6988 @item /EXECUTABLE=@var{exec_name}
6989 @cindex @code{/EXECUTABLE} (@code{GNAT MAKE})
6990 Output executable name. The name of the final executable program will be
6991 @var{exec_name}. If the @code{/EXECUTABLE} qualifier is omitted the default
6992 name for the executable will be the name of the input file in appropriate form
6993 for an executable file on the host system.
6995 This qualifier cannot be used when invoking @code{GNAT MAKE} with several
6996 @file{file_names}.
6998 @item /QUIET
6999 @cindex @code{/QUIET} (@code{GNAT MAKE})
7000 Quiet. When this flag is not set, the commands carried out by
7001 @code{GNAT MAKE} are displayed.
7003 @item /SWITCH_CHECK/
7004 @cindex @code{/SWITCH_CHECK} (@code{GNAT MAKE})
7005 Recompile if compiler qualifiers have changed since last compilation.
7006 All compiler qualifiers but -I and -o are taken into account in the
7007 following way:
7008 orders between different ``first letter'' qualifiers are ignored, but
7009 orders between same qualifiers are taken into account. For example,
7010 @code{-O /OPTIMIZE=ALL} is different than @code{/OPTIMIZE=ALL -O}, but @code{-g -O} is equivalent
7011 to @code{-O -g}.
7013 @item /UNIQUE
7014 @cindex @code{/UNIQUE} (@code{GNAT MAKE})
7015 Unique. Recompile at most the main file. It implies -c. Combined with
7016 -f, it is equivalent to calling the compiler directly.
7018 @item /REASONS
7019 @cindex @code{/REASONS} (@code{GNAT MAKE})
7020 Verbose. Displays the reason for all recompilations @code{GNAT MAKE}
7021 decides are necessary.
7023 @item /NOMAIN
7024 @cindex @code{/NOMAIN} (@code{GNAT MAKE})
7025 No main subprogram. Bind and link the program even if the unit name
7026 given on the command line is a package name. The resulting executable
7027 will execute the elaboration routines of the package and its closure,
7028 then the finalization routines.
7030 @item @code{GNAT COMPILE} @asis{qualifiers}
7031 Any qualifier that cannot be recognized as a qualifier for @code{GNAT MAKE}
7032 but is recognizable as a valid qualifier for @code{GNAT COMPILE} is
7033 automatically treated as a compiler qualifier, and passed on to all
7034 compilations that are carried out.
7035 @end table
7037 @noindent
7038 Source and library search path qualifiers:
7040 @table @code
7041 @item /SOURCE_SEARCH=@var{dir}
7042 @cindex @code{/SOURCE_SEARCH} (@code{GNAT MAKE})
7043 When looking for source files also look in directory @var{dir}.
7044 The order in which source files search is undertaken is
7045 described in @ref{Search Paths and the Run-Time Library (RTL)}.
7047 @item /SKIP_MISSING=@var{dir}
7048 @cindex @code{/SKIP_MISSING} (@code{GNAT MAKE})
7049 Consider @var{dir} as being an externally provided Ada library.
7050 Instructs @code{GNAT MAKE} to skip compilation units whose @file{.ALI}
7051 files have been located in directory @var{dir}. This allows you to have
7052 missing bodies for the units in @var{dir} and to ignore out of date bodies
7053 for the same units. You still need to specify
7054 the location of the specs for these units by using the qualifiers
7055 @code{/SOURCE_SEARCH=@var{dir}}
7056 or @code{/SEARCH=@var{dir}}.
7057 Note: this qualifier is provided for compatibility with previous versions
7058 of @code{GNAT MAKE}. The easier method of causing standard libraries
7059 to be excluded from consideration is to write-protect the corresponding
7060 ALI files.
7062 @item /OBJECT_SEARCH=@var{dir}
7063 @cindex @code{/OBJECT_SEARCH} (@code{GNAT MAKE})
7064 When searching for library and object files, look in directory
7065 @var{dir}. The order in which library files are searched is described in
7066 @ref{Search Paths for GNAT BIND}.
7068 @item /CONDITIONAL_SOURCE_SEARCH=@var{dir}
7069 @cindex Search paths, for @code{GNAT MAKE}
7070 @cindex @code{/CONDITIONAL_SOURCE_SEARCH} (@code{GNAT MAKE})
7071 Equivalent to @code{/SKIP_MISSING=@var{dir}
7072 /SOURCE_SEARCH=@var{dir}}.
7074 @item /SEARCH=@var{dir}
7075 @cindex @code{/SEARCH} (@code{GNAT MAKE})
7076 Equivalent to @code{/OBJECT_SEARCH=@var{dir}
7077 /SOURCE_SEARCH=@var{dir}}.
7079 @item /NOCURRENT_DIRECTORY
7080 @cindex @code{/NOCURRENT_DIRECTORY} (@code{GNAT MAKE})
7081 @cindex Source files, suppressing search
7082 Do not look for source files in the directory containing the source
7083 file named in the command line.
7084 Do not look for ALI or object files in the directory
7085 where @code{GNAT MAKE} was invoked.
7087 @item /LIBRARY_SEARCH=@var{dir}
7088 @cindex @code{/LIBRARY_SEARCH} (@code{GNAT MAKE})
7089 @cindex Linker libraries
7090 Add directory @var{dir} to the list of directories in which the linker
7091 will search for libraries. This is equivalent to
7092 @code{/LINKER_QUALIFIERS /LIBRARY_SEARCH=}@var{dir}.
7094 @item /NOSTD_INCLUDES
7095 @cindex @code{/NOSTD_INCLUDES} (@code{GNAT MAKE})
7096 Do not look for source files in the system default directory.
7098 @item /NOSTD_LIBRARIES
7099 @cindex @code{/NOSTD_LIBRARIES} (@code{GNAT MAKE})
7100 Do not look for library files in the system default directory.
7102 @item /RUNTIME_SYSTEM=@var{rts-path}
7103 @cindex @code{/RUNTIME_SYSTEM} (@code{GNAT MAKE})
7104 Specifies the default location of the runtime library. We look for the runtime
7105 in the following directories, and stop as soon as a valid runtime is found
7106 ("adainclude" or "ada_source_path", and "adalib" or "ada_object_path" present):
7108 @itemize @bullet
7109 @item <current directory>/$rts_path
7111 @item <default-search-dir>/$rts_path
7113 @item <default-search-dir>/rts-$rts_path
7114 @end itemize
7116 @noindent
7117 The selected path is handled like a normal RTS path.
7119 @end table
7121 @node Mode Qualifiers for GNAT MAKE
7122 @section Mode Qualifiers for @code{GNAT MAKE}
7124 @noindent
7125 The mode qualifiers (referred to as @code{mode_qualifiers}) allow the
7126 inclusion of qualifiers that are to be passed to the compiler itself, the
7127 binder or the linker. The effect of a mode qualifier is to cause all
7128 subsequent qualifiers up to the end of the qualifier list, or up to the next
7129 mode qualifier, to be interpreted as qualifiers to be passed on to the
7130 designated component of GNAT.
7132 @table @code
7133 @item /COMPILER_QUALIFIERS @var{qualifiers}
7134 @cindex @code{/COMPILER_QUALIFIERS} (@code{GNAT MAKE})
7135 Compiler qualifiers. Here @var{qualifiers} is a list of qualifiers
7136 that are valid qualifiers for @code{GNAT COMPILE}. They will be passed on to
7137 all compile steps performed by @code{GNAT MAKE}.
7139 @item /BINDER_QUALIFIERS @var{qualifiers}
7140 @cindex @code{/BINDER_QUALIFIERS} (@code{GNAT MAKE})
7141 Binder qualifiers. Here @var{qualifiers} is a list of qualifiers
7142 that are valid qualifiers for @code{GNAT COMPILE}. They will be passed on to
7143 all bind steps performed by @code{GNAT MAKE}.
7145 @item /LINKER_QUALIFIERS @var{qualifiers}
7146 @cindex @code{/LINKER_QUALIFIERS} (@code{GNAT MAKE})
7147 Linker qualifiers. Here @var{qualifiers} is a list of qualifiers
7148 that are valid qualifiers for @code{GNAT COMPILE}. They will be passed on to
7149 all link steps performed by @code{GNAT MAKE}.
7150 @end table
7152 @node Notes on the Command Line
7153 @section Notes on the Command Line
7155 @noindent
7156 This section contains some additional useful notes on the operation
7157 of the @code{GNAT MAKE} command.
7159 @itemize @bullet
7160 @item
7161 @cindex Recompilation, by @code{GNAT MAKE}
7162 If @code{GNAT MAKE} finds no ALI files, it recompiles the main program
7163 and all other units required by the main program.
7164 This means that @code{GNAT MAKE}
7165 can be used for the initial compile, as well as during subsequent steps of
7166 the development cycle.
7168 @item
7169 If you enter @code{GNAT MAKE @var{file}.ADB}, where @file{@var{file}.ADB}
7170 is a subunit or body of a generic unit, @code{GNAT MAKE} recompiles
7171 @file{@var{file}.ADB} (because it finds no ALI) and stops, issuing a
7172 warning.
7174 @item
7175 In @code{GNAT MAKE} the qualifier @code{/SEARCH}
7176 is used to specify both source and
7177 library file paths. Use @code{/SOURCE_SEARCH}
7178 instead if you just want to specify
7179 source paths only and @code{/OBJECT_SEARCH}
7180 if you want to specify library paths
7181 only.
7183 @item
7184 @code{GNAT MAKE} examines both an ALI file and its corresponding object file
7185 for consistency. If an ALI is more recent than its corresponding object,
7186 or if the object file is missing, the corresponding source will be recompiled.
7187 Note that @code{GNAT MAKE} expects an ALI and the corresponding object file
7188 to be in the same directory.
7190 @item
7191 @code{GNAT MAKE} will ignore any files whose ALI file is write-protected.
7192 This may conveniently be used to exclude standard libraries from
7193 consideration and in particular it means that the use of the
7194 @code{/FORCE_COMPILE} qualifier will not recompile these files
7195 unless @code{/ALL_FILES} is also specified.
7197 @item
7198 @code{GNAT MAKE} has been designed to make the use of Ada libraries
7199 particularly convenient. Assume you have an Ada library organized
7200 as follows: [@var{OBJ_DIR}] contains the objects and ALI files for
7201 of your Ada compilation units,
7202 whereas [@var{INCLUDE_DIR}] contains the
7203 specs of these units, but no bodies. Then to compile a unit
7204 stored in @code{MAIN.ADB}, which uses this Ada library you would just type
7206 @smallexample
7207 $ GNAT MAKE /SOURCE_SEARCH=[@var{INCLUDE_DIR}]
7208            /SKIP_MISSING=[@var{OBJ_DIR}] main
7209 @end smallexample
7211 @item
7212 Using @code{GNAT MAKE} along with the
7213 @code{/MINIMAL_RECOMPILATION}
7214 qualifier provides a mechanism for avoiding unnecessary rcompilations. Using
7215 this qualifier,
7216 you can update the comments/format of your
7217 source files without having to recompile everything. Note, however, that
7218 adding or deleting lines in a source files may render its debugging
7219 info obsolete. If the file in question is a spec, the impact is rather
7220 limited, as that debugging info will only be useful during the
7221 elaboration phase of your program. For bodies the impact can be more
7222 significant. In all events, your debugger will warn you if a source file
7223 is more recent than the corresponding object, and alert you to the fact
7224 that the debugging information may be out of date.
7225 @end itemize
7227 @node How GNAT MAKE Works
7228 @section How @code{GNAT MAKE} Works
7230 @noindent
7231 Generally @code{GNAT MAKE} automatically performs all necessary
7232 recompilations and you don't need to worry about how it works. However,
7233 it may be useful to have some basic understanding of the @code{GNAT MAKE}
7234 approach and in particular to understand how it uses the results of
7235 previous compilations without incorrectly depending on them.
7237 First a definition: an object file is considered @dfn{up to date} if the
7238 corresponding ALI file exists and its time stamp predates that of the
7239 object file and if all the source files listed in the
7240 dependency section of this ALI file have time stamps matching those in
7241 the ALI file. This means that neither the source file itself nor any
7242 files that it depends on have been modified, and hence there is no need
7243 to recompile this file.
7245 @code{GNAT MAKE} works by first checking if the specified main unit is up
7246 to date. If so, no compilations are required for the main unit. If not,
7247 @code{GNAT MAKE} compiles the main program to build a new ALI file that
7248 reflects the latest sources. Then the ALI file of the main unit is
7249 examined to find all the source files on which the main program depends,
7250 and @code{GNAT MAKE} recursively applies the above procedure on all these files.
7252 This process ensures that @code{GNAT MAKE} only trusts the dependencies
7253 in an existing ALI file if they are known to be correct. Otherwise it
7254 always recompiles to determine a new, guaranteed accurate set of
7255 dependencies. As a result the program is compiled "upside down" from what may
7256 be more familiar as the required order of compilation in some other Ada
7257 systems. In particular, clients are compiled before the units on which
7258 they depend. The ability of GNAT to compile in any order is critical in
7259 allowing an order of compilation to be chosen that guarantees that
7260 @code{GNAT MAKE} will recompute a correct set of new dependencies if
7261 necessary.
7263 When invoking @code{GNAT MAKE} with several @var{file_names}, if a unit is
7264 imported by several of the executables, it will be recompiled at most once.
7266 @node Examples of GNAT MAKE Usage
7267 @section Examples of @code{GNAT MAKE} Usage
7269 @table @code
7270 @item GNAT MAKE HELLO.ADB
7271 Compile all files necessary to bind and link the main program
7272 @file{HELLO.ADB} (containing unit @code{Hello}) and bind and link the
7273 resulting object files to generate an executable file @file{HELLO.EXE}.
7275 @item GNAT MAKE main1 main2 main3
7276 Compile all files necessary to bind and link the main programs
7277 @file{MAIN1.ADB} (containing unit @code{Main1}), @file{MAIN2.ADB}
7278 (containing unit @code{Main2}) and @file{MAIN3.ADB}
7279 (containing unit @code{Main3}) and bind and link the resulting object files
7280 to generate three executable files @file{MAIN1.EXE},
7281 @file{MAIN2.EXE}
7282 and @file{MAIN3.EXE}.
7285 @item GNAT MAKE Main_Unit /QUIET /COMPILER_QUALIFIERS /OPTIMIZE=ALL /BINDER_QUALIFIERS /ORDER_OF_ELABORATION
7286 Compile all files necessary to bind and link the main program unit
7287 @code{Main_Unit} (from file @file{MAIN_UNIT.ADB}). All compilations will
7288 be done with optimization level 2 and the order of elaboration will be
7289 listed by the binder. @code{GNAT MAKE} will operate in quiet mode, not
7290 displaying commands it is executing.
7291 @end table
7293 @node Renaming Files Using GNAT CHOP
7294 @chapter Renaming Files Using @code{GNAT CHOP}
7295 @findex GNAT CHOP
7297 @noindent
7298 This chapter discusses how to handle files with multiple units by using
7299 the @code{GNAT CHOP} utility. This utility is also useful in renaming
7300 files to meet the standard GNAT default file naming conventions.
7302 @menu
7303 * Handling Files with Multiple Units::
7304 * Operating GNAT CHOP in Compilation Mode::
7305 * Command Line for GNAT CHOP::
7306 * Qualifiers for GNAT CHOP::
7307 * Examples of GNAT CHOP Usage::
7308 @end menu
7310 @node Handling Files with Multiple Units
7311 @section Handling Files with Multiple Units
7313 @noindent
7314 The basic compilation model of GNAT requires that a file submitted to the
7315 compiler have only one unit and there be a strict correspondence
7316 between the file name and the unit name.
7318 The @code{GNAT CHOP} utility allows both of these rules to be relaxed,
7319 allowing GNAT to process files which contain multiple compilation units
7320 and files with arbitrary file names. @code{GNAT CHOP}
7321 reads the specified file and generates one or more output files,
7322 containing one unit per file. The unit and the file name correspond,
7323 as required by GNAT.
7325 If you want to permanently restructure a set of "foreign" files so that
7326 they match the GNAT rules, and do the remaining development using the
7327 GNAT structure, you can simply use @code{GNAT CHOP} once, generate the
7328 new set of files and work with them from that point on.
7330 Alternatively, if you want to keep your files in the "foreign" format,
7331 perhaps to maintain compatibility with some other Ada compilation
7332 system, you can set up a procedure where you use @code{GNAT CHOP} each
7333 time you compile, regarding the source files that it writes as temporary
7334 files that you throw away.
7336 @node Operating GNAT CHOP in Compilation Mode
7337 @section Operating GNAT CHOP in Compilation Mode
7339 @noindent
7340 The basic function of @code{GNAT CHOP} is to take a file with multiple units
7341 and split it into separate files. The boundary between files is reasonably
7342 clear, except for the issue of comments and pragmas. In default mode, the
7343 rule is that any pragmas between units belong to the previous unit, except
7344 that configuration pragmas always belong to the following unit. Any comments
7345 belong to the following unit. These rules
7346 almost always result in the right choice of
7347 the split point without needing to mark it explicitly and most users will
7348 find this default to be what they want. In this default mode it is incorrect to
7349 submit a file containing only configuration pragmas, or one that ends in
7350 configuration pragmas, to @code{GNAT CHOP}.
7352 However, using a special option to activate "compilation mode",
7353 @code{GNAT CHOP}
7354 can perform another function, which is to provide exactly the semantics
7355 required by the RM for handling of configuration pragmas in a compilation.
7356 In the absence of configuration pragmas (at the main file level), this
7357 option has no effect, but it causes such configuration pragmas to be handled
7358 in a quite different manner.
7360 First, in compilation mode, if @code{GNAT CHOP} is given a file that consists of
7361 only configuration pragmas, then this file is appended to the
7362 @file{GNAT.ADC} file in the current directory. This behavior provides
7363 the required behavior described in the RM for the actions to be taken
7364 on submitting such a file to the compiler, namely that these pragmas
7365 should apply to all subsequent compilations in the same compilation
7366 environment. Using GNAT, the current directory, possibly containing a
7367 @file{GNAT.ADC} file is the representation
7368 of a compilation environment. For more information on the
7369 @file{GNAT.ADC} file, see the section on handling of configuration
7370 pragmas @pxref{Handling of Configuration Pragmas}.
7372 Second, in compilation mode, if @code{GNAT CHOP}
7373 is given a file that starts with
7374 configuration pragmas, and contains one or more units, then these
7375 configuration pragmas are prepended to each of the chopped files. This
7376 behavior provides the required behavior described in the RM for the
7377 actions to be taken on compiling such a file, namely that the pragmas
7378 apply to all units in the compilation, but not to subsequently compiled
7379 units.
7381 Finally, if configuration pragmas appear between units, they are appended
7382 to the previous unit. This results in the previous unit being illegal,
7383 since the compiler does not accept configuration pragmas that follow
7384 a unit. This provides the required RM behavior that forbids configuration
7385 pragmas other than those preceding the first compilation unit of a
7386 compilation.
7388 For most purposes, @code{GNAT CHOP} will be used in default mode. The
7389 compilation mode described above is used only if you need exactly
7390 accurate behavior with respect to compilations, and you have files
7391 that contain multiple units and configuration pragmas. In this
7392 circumstance the use of @code{GNAT CHOP} with the compilation mode
7393 qualifier provides the required behavior, and is for example the mode
7394 in which GNAT processes the ACVC tests.
7396 @node Command Line for GNAT CHOP
7397 @section Command Line for @code{GNAT CHOP}
7399 @noindent
7400 The @code{GNAT CHOP} command has the form:
7402 @smallexample
7403 $ GNAT CHOP qualifiers @var{file name} [@var{file name} @var{file name} ...]
7404       [@var{directory}]
7405 @end smallexample
7407 @noindent
7408 The only required argument is the file name of the file to be chopped.
7409 There are no restrictions on the form of this file name. The file itself
7410 contains one or more Ada units, in normal GNAT format, concatenated
7411 together. As shown, more than one file may be presented to be chopped.
7413 When run in default mode, @code{GNAT CHOP} generates one output file in
7414 the current directory for each unit in each of the files.
7416 @var{directory}, if specified, gives the name of the directory to which
7417 the output files will be written. If it is not specified, all files are
7418 written to the current directory.
7420 For example, given a
7421 file called @file{hellofiles} containing
7423 @smallexample
7424 @group
7425 @cartouche
7426 @b{procedure} hello;
7428 @b{with} Text_IO; @b{use} Text_IO;
7429 @b{procedure} hello @b{is}
7430 @b{begin}
7431    Put_Line ("Hello");
7432 @b{end} hello;
7433 @end cartouche
7434 @end group
7435 @end smallexample
7437 @noindent
7438 the command
7440 @smallexample
7441 $ GNAT CHOP HELLOFILES.
7442 @end smallexample
7444 @noindent
7445 generates two files in the current directory, one called
7446 @file{HELLO.ADS} containing the single line that is the procedure spec,
7447 and the other called @file{HELLO.ADB} containing the remaining text. The
7448 original file is not affected. The generated files can be compiled in
7449 the normal manner.
7451 @node Qualifiers for GNAT CHOP
7452 @section Qualifiers for @code{GNAT CHOP}
7454 @noindent
7455 @code{GNAT CHOP} recognizes the following qualifiers:
7457 @table @code
7459 @item /COMPILATION
7460 @cindex @code{/COMPILATION} (@code{GNAT CHOP})
7461 Causes @code{GNAT CHOP} to operate in compilation mode, in which
7462 configuration pragmas are handled according to strict RM rules. See
7463 previous section for a full description of this mode.
7466 @item /HELP
7467 Causes @code{GNAT CHOP} to generate a brief help summary to the standard
7468 output file showing usage information.
7470 @item /FILE_NAME_MAX_LENGTH=@var{mm}
7471 @cindex @code{/FILE_NAME_MAX_LENGTH} (@code{GNAT CHOP})
7472 Limit generated file names to the specified number @code{mm}
7473 of characters.
7474 This is useful if the
7475 resulting set of files is required to be interoperable with systems
7476 which limit the length of file names.
7477 If no value is given, or
7478 if no @code{/FILE_NAME_MAX_LENGTH} qualifier is given,
7479 a default of 39, suitable for OpenVMS Alpha
7480 Systems, is assumed
7482 @item /PRESERVE
7483 @cindex @code{/PRESERVE} (@code{GNAT CHOP})
7484 Causes the file creation time stamp of the input file to be
7485 preserved and used for the time stamp of the output file(s). This may be
7486 useful for preserving coherency of time stamps in an enviroment where
7487 @code{GNAT CHOP} is used as part of a standard build process.
7489 @item /QUIET
7490 @cindex @code{/QUIET} (@code{GNAT CHOP})
7491 Causes output of informational messages indicating the set of generated
7492 files to be suppressed. Warnings and error messages are unaffected.
7494 @item /REFERENCE
7495 @cindex @code{/REFERENCE} (@code{GNAT CHOP})
7496 @findex Source_Reference
7497 Generate @code{Source_Reference} pragmas. Use this qualifier if the output
7498 files are regarded as temporary and development is to be done in terms
7499 of the original unchopped file. This qualifier causes
7500 @code{Source_Reference} pragmas to be inserted into each of the
7501 generated files to refers back to the original file name and line number.
7502 The result is that all error messages refer back to the original
7503 unchopped file.
7504 In addition, the debugging information placed into the object file (when
7505 the @code{/DEBUG} qualifier of @code{GNAT COMPILE} or @code{GNAT MAKE} is specified) also
7506 refers back to this original file so that tools like profilers and
7507 debuggers will give information in terms of the original unchopped file.
7509 If the original file to be chopped itself contains
7510 a @code{Source_Reference}
7511 pragma referencing a third file, then GNAT CHOP respects
7512 this pragma, and the generated @code{Source_Reference} pragmas
7513 in the chopped file refer to the original file, with appropriate
7514 line numbers. This is particularly useful when @code{GNAT CHOP}
7515 is used in conjunction with @code{GNAT PREPROCESS} to compile files that
7516 contain preprocessing statements and multiple units.
7518 @item /VERBOSE
7519 @cindex @code{/VERBOSE} (@code{GNAT CHOP})
7520 Causes @code{GNAT CHOP} to operate in verbose mode. The version
7521 number and copyright notice are output, as well as exact copies of
7522 the GNAT1 commands spawned to obtain the chop control information.
7524 @item /OVERWRITE
7525 @cindex @code{/OVERWRITE} (@code{GNAT CHOP})
7526 Overwrite existing file names. Normally @code{GNAT CHOP} regards it as a
7527 fatal error if there is already a file with the same name as a
7528 file it would otherwise output, in other words if the files to be
7529 chopped contain duplicated units. This qualifier bypasses this
7530 check, and causes all but the last instance of such duplicated
7531 units to be skipped.
7533 @end table
7535 @node Examples of GNAT CHOP Usage
7536 @section Examples of @code{GNAT CHOP} Usage
7538 @table @code
7539 @item GNAT CHOP /OVERWRITE HELLO_S.ADA [ICHBIAH.FILES]
7541 Chops the source file @file{HELLO_S.ADA}. The output files will be
7542 placed in the directory @file{[ICHBIAH.FILES]},
7543 overwriting any
7544 files with matching names in that directory (no files in the current
7545 directory are modified).
7547 @item GNAT CHOP ARCHIVE.
7548 Chops the source file @file{ARCHIVE.}
7549 into the current directory. One
7550 useful application of @code{GNAT CHOP} is in sending sets of sources
7551 around, for example in email messages. The required sources are simply
7552 concatenated (for example, using a VMS @code{APPEND/NEW}
7553 command), and then
7554 @code{GNAT CHOP} is used at the other end to reconstitute the original
7555 file names.
7557 @item GNAT CHOP file1 file2 file3 direc
7558 Chops all units in files @file{file1}, @file{file2}, @file{file3}, placing
7559 the resulting files in the directory @file{direc}. Note that if any units
7560 occur more than once anywhere within this set of files, an error message
7561 is generated, and no files are written. To override this check, use the
7562 @code{/OVERWRITE} qualifier,
7563 in which case the last occurrence in the last file will
7564 be the one that is output, and earlier duplicate occurrences for a given
7565 unit will be skipped.
7566 @end table
7568 @node Configuration Pragmas
7569 @chapter Configuration Pragmas
7570 @cindex Configuration pragmas
7571 @cindex Pragmas, configuration
7573 @noindent
7574 In Ada 95, configuration pragmas include those pragmas described as
7575 such in the Ada 95 Reference Manual, as well as
7576 implementation-dependent pragmas that are configuration pragmas. See the
7577 individual descriptions of pragmas in the GNAT Reference Manual for
7578 details on these additional GNAT-specific configuration pragmas. Most
7579 notably, the pragma @code{Source_File_Name}, which allows
7580 specifying non-default names for source files, is a configuration
7581 pragma. The following is a complete list of configuration pragmas
7582 recognized by @code{GNAT}:
7584 @smallexample
7585    Ada_83
7586    Ada_95
7587    C_Pass_By_Copy
7588    Component_Alignment
7589    Discard_Names
7590    Elaboration_Checks
7591    Eliminate
7592    Extend_System
7593    Extensions_Allowed
7594    External_Name_Casing
7595    Float_Representation
7596    Initialize_Scalars
7597    License
7598    Locking_Policy
7599    Long_Float
7600    No_Run_Time
7601    Normalize_Scalars
7602    Polling
7603    Propagate_Exceptions
7604    Queuing_Policy
7605    Ravenscar
7606    Restricted_Run_Time
7607    Restrictions
7608    Reviewable
7609    Source_File_Name
7610    Style_Checks
7611    Suppress
7612    Task_Dispatching_Policy
7613    Unsuppress
7614    Use_VADS_Size
7615    Warnings
7616    Validity_Checks
7617 @end smallexample
7619 @menu
7620 * Handling of Configuration Pragmas::
7621 * The Configuration Pragmas Files::
7622 @end menu
7624 @node Handling of Configuration Pragmas
7625 @section Handling of Configuration Pragmas
7627 Configuration pragmas may either appear at the start of a compilation
7628 unit, in which case they apply only to that unit, or they may apply to
7629 all compilations performed in a given compilation environment.
7631 GNAT also provides the @code{GNAT CHOP} utility to provide an automatic
7632 way to handle configuration pragmas following the semantics for
7633 compilations (that is, files with multiple units), described in the RM.
7634 See section @pxref{Operating GNAT CHOP in Compilation Mode} for details.
7635 However, for most purposes, it will be more convenient to edit the
7636 @file{GNAT.ADC} file that contains configuration pragmas directly,
7637 as described in the following section.
7639 @node The Configuration Pragmas Files
7640 @section The Configuration Pragmas Files
7641 @cindex @file{GNAT.ADC}
7643 @noindent
7644 In GNAT a compilation environment is defined by the current
7645 directory at the time that a compile command is given. This current
7646 directory is searched for a file whose name is @file{GNAT.ADC}. If
7647 this file is present, it is expected to contain one or more
7648 configuration pragmas that will be applied to the current compilation.
7649 However, if the qualifier @option{-gnatA} is used, @file{GNAT.ADC} is not
7650 considered.
7652 Configuration pragmas may be entered into the @file{GNAT.ADC} file
7653 either by running @code{GNAT CHOP} on a source file that consists only of
7654 configuration pragmas, or more conveniently  by
7655 direct editing of the @file{GNAT.ADC} file, which is a standard format
7656 source file.
7658 In addition to @file{GNAT.ADC}, one additional file containing configuration
7659 pragmas may be applied to the current compilation using the qualifier
7660 @option{-gnatec}@var{path}. @var{path} must designate an existing file that
7661 contains only configuration pragmas. These configuration pragmas are
7662 in addition to those found in @file{GNAT.ADC} (provided @file{GNAT.ADC}
7663 is present and qualifier @option{-gnatA} is not used).
7665 It is allowed to specify several qualifiers @option{-gnatec}, however only
7666 the last one on the command line will be taken into account.
7668 Of special interest to GNAT OpenVMS Alpha is the following configuration pragma:
7670 @smallexample
7671 @cartouche
7672 @b{pragma} Extend_System (Aux_DEC);
7673 @end cartouche
7674 @end smallexample
7676 @noindent
7677 In the presence of this pragma, GNAT adds to the definition of the
7678 predefined package SYSTEM all the additional types and subprograms that are
7679 defined in DEC Ada. See @pxref{Compatibility with DEC Ada} for details.
7681 @node Handling Arbitrary File Naming Conventions Using gnatname
7682 @chapter Handling Arbitrary File Naming Conventions Using @code{gnatname}
7683 @cindex Arbitrary File Naming Conventions
7685 @menu
7686 * Arbitrary File Naming Conventions::
7687 * Running gnatname::
7688 * Qualifiers for gnatname::
7689 * Examples of gnatname Usage::
7690 @end menu
7692 @node Arbitrary File Naming Conventions
7693 @section Arbitrary File Naming Conventions
7695 @noindent
7696 The GNAT compiler must be able to know the source file name of a compilation unit.
7697 When using the standard GNAT default file naming conventions (@code{.ADS} for specs,
7698 @code{.ADB} for bodies), the GNAT compiler does not need additional information.
7700 @noindent
7701 When the source file names do not follow the standard GNAT default file naming
7702 conventions, the GNAT compiler must be given additional information through
7703 a configuration pragmas file (see @ref{Configuration Pragmas}) or a project file.
7704 When the non standard file naming conventions are well-defined, a small number of
7705 pragmas @code{Source_File_Name} specifying a naming pattern
7706 (see @ref{Alternative File Naming Schemes}) may be sufficient. However,
7707 if the file naming conventions are irregular or arbitrary, a number
7708 of pragma @code{Source_File_Name} for individual compilation units must be defined.
7709 To help maintain the correspondence between compilation unit names and
7710 source file names within the compiler,
7711 GNAT provides a tool @code{gnatname} to generate the required pragmas for a
7712 set of files.
7714 @node Running gnatname
7715 @section Running @code{gnatname}
7717 @noindent
7718 The usual form of the @code{gnatname} command is
7720 @smallexample
7721 $ gnatname [@var{qualifiers}] @var{naming_pattern} [@var{naming_patterns}]
7722 @end smallexample
7724 @noindent
7725 All of the arguments are optional. If invoked without any argument,
7726 @code{gnatname} will display its usage.
7728 @noindent
7729 When used with at least one naming pattern, @code{gnatname} will attempt to
7730 find all the compilation units in files that follow at least one of the
7731 naming patterns. To find these compilation units,
7732 @code{gnatname} will use the GNAT compiler in syntax-check-only mode on all
7733 regular files.
7735 @noindent
7736 One or several Naming Patterns may be given as arguments to @code{gnatname}.
7737 Each Naming Pattern is enclosed between double quotes.
7738 A Naming Pattern is a regular expression similar to the wildcard patterns
7739 used in file names by the Unix shells or the DOS prompt.
7741 @noindent
7742 Examples of Naming Patterns are
7744 @smallexample
7745    "*.[12].ADA"
7746    "*.ad[sb]*"
7747    "body_*"    "spec_*"
7748 @end smallexample
7750 @noindent
7751 For a more complete description of the syntax of Naming Patterns, see the second kind
7752 of regular expressions described in @file{G-REGEXP.ADS} (the "Glob" regular
7753 expressions).
7755 @noindent
7756 When invoked with no qualifiers, @code{gnatname} will create a configuration
7757 pragmas file @file{GNAT.ADC} in the current working directory, with pragmas
7758 @code{Source_File_Name} for each file that contains a valid Ada unit.
7760 @node Qualifiers for gnatname
7761 @section Qualifiers for @code{gnatname}
7763 @noindent
7764 Qualifiers for @code{gnatname} must precede any specified Naming Pattern.
7766 @noindent
7767 You may specify any of the following qualifiers to @code{gnatname}:
7769 @table @code
7771 @item -c@file{file}
7772 @cindex @code{-c} (@code{gnatname})
7773 Create a configuration pragmas file @file{file} (instead of the default
7774 @file{GNAT.ADC}). There may be zero, one or more space between @code{-c} and
7775 @file{file}. @file{file} may include directory information. @file{file} must be
7776 writeable. There may be only one qualifier @code{-c}. When a qualifier @code{-c} is
7777 specified, no qualifier @code{-P} may be specified (see below).
7779 @item -d@file{dir}
7780 @cindex @code{-d} (@code{gnatname})
7781 Look for source files in directory @file{dir}. There may be zero, one or more spaces
7782 between @code{-d} and @file{dir}. When a qualifier @code{-d} is specified,
7783 the current working directory will not be searched for source files, unless it
7784 is explictly
7785 specified with a @code{-d} or @code{-D} qualifier. Several qualifiers @code{-d} may be
7786 specified. If @file{dir} is a relative path, it is relative to the directory of
7787 the configuration pragmas file specified with qualifier @code{-c}, or to the directory
7788 of the project file specified with qualifier @code{-P} or, if neither qualifier @code{-c}
7789 nor qualifier @code{-P} are specified, it is relative to the current working
7790 directory. The directory
7791 specified with qualifier @code{-c} must exist and be readable.
7793 @item -D@file{file}
7794 @cindex @code{-D} (@code{gnatname})
7795 Look for source files in all directories listed in text file @file{file}. There may be
7796 zero, one or more spaces between @code{-d} and @file{dir}. @file{file}
7797 must be an existing, readable text file. Each non empty line in @file{file} must be
7798 a directory. Specifying qualifier @code{-D} is equivalent to specifying as many qualifiers
7799 @code{-d} as there are non empty lines in @file{file}.
7801 @item -h
7802 @cindex @code{-h} (@code{gnatname})
7803 Output usage (help) information. The output is written to @file{SYS$OUTPUT}.
7805 @item -P@file{proj}
7806 @cindex @code{-P} (@code{gnatname})
7807 Create or update project file @file{proj}. There may be zero, one or more space
7808 between @code{-P} and @file{proj}. @file{proj} may include directory information.
7809 @file{proj} must be writeable. There may be only one qualifier @code{-P}.
7810 When a qualifier @code{-P} is specified, no qualifier @code{-c} may be specified.
7812 @item -v
7813 @cindex @code{-v} (@code{gnatname})
7814 Verbose mode. Output detailed explanation of behavior to @file{SYS$OUTPUT}. This includes
7815 name of the file written, the name of the directories to search and, for each file
7816 in those directories whose name matches at least one of the Naming Patterns, an
7817 indication of whether the file contains a unit, and if so the name of the unit.
7819 @item -v -v
7820 Very Verbose mode. In addition to the output produced in verbose mode, for each file
7821 in the searched directories whose name matches none of the Naming Patterns, an
7822 indication is given that there is no match.
7824 @item -x@file{pattern}
7825 Excluded patterns. Using this qualifier, it is possible to exclude some files
7826 that would match the name patterns. For example,
7827 @code{"gnatname -x "*_NT.ADA" "*.ADA"} will look for Ada units in all files
7828 with the @file{.ADA} extension, except those whose names end with
7829 @file{_NT.ADA}.
7831 @end table
7833 @node Examples of gnatname Usage
7834 @section Examples of @code{gnatname} Usage
7836 @smallexample
7837 $ gnatname -c /home/me/NAMES.ADC -d sources "[a-z]*.ADA*"
7838 @end smallexample
7840 In this example, the directory @file{/home/me} must already exist and be
7841 writeable. In addition, the directory @file{/home/me/sources} (specified by
7842 @code{-d sources}) must exist and be readable. Note the optional spaces after
7843 @code{-c} and @code{-d}.
7845 @smallexample
7846 $ gnatname -P/home/me/proj -x "*_NT_BODY.ADA" -dsources -dsources/plus -Dcommon_dirs.txt "body_*" "spec_*"
7847 @end smallexample
7849 Note that several qualifiers @code{-d} may be used, even in conjunction with one
7850 or several qualifiers @code{-D}. Several Naming Patterns and one excluded pattern
7851 are used in this example.
7854 @c *****************************************
7855 @c * G N A T  P r o j e c t  M a n a g e r *
7856 @c *****************************************
7857 @node GNAT Project Manager
7858 @chapter GNAT Project Manager
7860 @menu
7861 * Introduction::
7862 * Examples of Project Files::
7863 * Project File Syntax::
7864 * Objects and Sources in Project Files::
7865 * Importing Projects::
7866 * Project Extension::
7867 * External References in Project Files::
7868 * Packages in Project Files::
7869 * Variables from Imported Projects::
7870 * Naming Schemes::
7871 * Library Projects::
7872 * Qualifiers Related to Project Files::
7873 * Tools Supporting Project Files::
7874 * An Extended Example::
7875 * Project File Complete Syntax::
7876 @end menu
7879 @c ****************
7880 @c * Introduction *
7881 @c ****************
7883 @node Introduction
7884 @section Introduction
7886 @noindent
7887 This chapter describes GNAT's @emph{Project Manager}, a facility that
7888 lets you configure various properties for a collection of source files.  In
7889 particular, you can specify:
7890 @itemize @bullet
7891 @item
7892 The directory or set of directories containing the source files, and/or the
7893 names of the specific source files themselves
7894 @item
7895 The directory in which the compiler's output
7896 (@file{ALI} files, object files, tree files) will be placed
7897 @item
7898 The directory in which the executable programs will be placed
7899 @item
7900 Qualifier settings for any of the project-enabled tools (@command{GNAT MAKE},
7901 compiler, binder, linker, @code{GNAT LIST}, @code{GNAT XREF}, @code{GNAT FIND});
7902 you can apply these settings either globally or to individual units
7903 @item
7904 The source files containing the main subprogram(s) to be built
7905 @item
7906 The source programming language(s) (currently Ada and/or C)
7907 @item
7908 Source file naming conventions; you can specify these either globally or for
7909 individual units
7910 @end itemize
7912 @menu
7913 * Project Files::
7914 @end menu
7916 @node Project Files
7917 @subsection Project Files
7919 @noindent
7920 A @dfn{project} is a specific set of values for these properties.  You can
7921 define a project's settings in a @dfn{project file}, a text file with an
7922 Ada-like syntax; a property value is either a string or a list of strings.
7923 Properties that are not explicitly set receive default values.  A project
7924 file may interrogate the values of @dfn{external variables} (user-defined
7925 command-line qualifiers or environment variables), and it may specify property
7926 settings conditionally, based on the value of such variables.
7928 In simple cases, a project's source files depend only on other source files
7929 in the same project, or on the predefined libraries.  ("Dependence" is in
7930 the technical sense; for example, one Ada unit "with"ing another.)  However,
7931 the Project Manager also allows much more sophisticated arrangements,
7932 with the source files in one project depending on source files in other
7933 projects:
7934 @itemize @bullet
7935 @item
7936 One project can @emph{import} other projects containing needed source files.
7937 @item
7938 You can organize GNAT projects in a hierarchy: a @emph{child} project
7939 can extend a @emph{parent} project, inheriting the parent's source files and
7940 optionally overriding any of them with alternative versions
7941 @end itemize
7943 @noindent
7944 More generally, the Project Manager lets you structure large development
7945 efforts into hierarchical subsystems, with build decisions deferred to the
7946 subsystem level and thus different compilation environments (qualifier settings)
7947 used for different subsystems.
7949 The Project Manager is invoked through the @option{-P@emph{projectfile}}
7950 qualifier to @command{GNAT MAKE} or to the @command{gnat} front driver.
7951 If you want to define (on the command line) an external variable that is
7952 queried by the project file, additionally use the
7953 @option{-X@emph{vbl}=@emph{value}} qualifier.
7954 The Project Manager parses and interprets the project file, and drives the
7955 invoked tool based on the project settings.
7957 The Project Manager supports a wide range of development strategies,
7958 for systems of all sizes.  Some typical practices that are easily handled:
7959 @itemize @bullet
7960 @item
7961 Using a common set of source files, but generating object files in different
7962 directories via different qualifier settings
7963 @item
7964 Using a mostly-shared set of source files, but with different versions of
7965 some unit or units
7966 @end itemize
7968 @noindent
7969 The destination of an executable can be controlled inside a project file
7970 using the @option{-o} qualifier. In the absence of such a qualifier either inside
7971 the project file or on the command line, any executable files generated by
7972 @command{GNAT MAKE} will be placed in the directory @code{Exec_Dir} specified
7973 in the project file. If no @code{Exec_Dir} is specified, they will be placed
7974 in the object directory of the project.
7976 You can use project files to achieve some of the effects of a source
7977 versioning system (for example, defining separate projects for
7978 the different sets of sources that comprise different releases) but the
7979 Project Manager is independent of any source configuration management tools
7980 that might be used by the developers.
7982 The next section introduces the main features of GNAT's project facility
7983 through a sequence of examples; subsequent sections will present the syntax
7984 and semantics in more detail.
7987 @c *****************************
7988 @c * Examples of Project Files *
7989 @c *****************************
7991 @node Examples of Project Files
7992 @section Examples of Project Files
7993 @noindent
7994 This section illustrates some of the typical uses of project files and
7995 explains their basic structure and behavior.
7997 @menu
7998 * Common Sources with Different Qualifiers and Different Output Directories::
7999 * Using External Variables::
8000 * Importing Other Projects::
8001 * Extending a Project::
8002 @end menu
8004 @node Common Sources with Different Qualifiers and Different Output Directories
8005 @subsection Common Sources with Different Qualifiers and Different Output Directories
8007 @menu
8008 * Source Files::
8009 * Specifying the Object Directory::
8010 * Specifying the Exec Directory::
8011 * Project File Packages::
8012 * Specifying Qualifier Settings::
8013 * Main Subprograms::
8014 * Source File Naming Conventions::
8015 * Source Language(s)::
8016 @end menu
8018 @noindent
8019 Assume that the Ada source files @file{PACK.ADS}, @file{PACK.ADB}, and
8020 @file{PROC.ADB} are in the @file{/common} directory.  The file
8021 @file{PROC.ADB} contains an Ada main subprogram @code{Proc} that "with"s
8022 package @code{Pack}.  We want to compile these source files under two sets
8023 of qualifiers:
8024 @itemize @bullet
8025 @item
8026 When debugging, we want to pass the @option{-g} qualifier to @command{GNAT MAKE},
8027 and the @option{/CHECKS=ASSERTIONS}, @option{/CHECKS=OVERFLOW}, and @option{/CHECKS=ELABORATION} qualifiers to the
8028 compiler; the compiler's output is to appear in @file{/common/debug}
8029 @item
8030 When preparing a release version, we want to pass the @option{/OPTIMIZE=ALL} qualifier to
8031 the compiler; the compiler's output is to appear in @file{/common/release}
8032 @end itemize
8034 @noindent
8035 The GNAT project files shown below, respectively @file{debug.gpr} and
8036 @file{release.gpr} in the @file{/common} directory, achieve these effects.
8038 Diagrammatically:
8039 @smallexample
8040 @group
8041 /common
8042   debug.gpr
8043   release.gpr
8044   PACK.ADS
8045   PACK.ADB
8046   PROC.ADB
8047 @end group
8048 @group
8049 /common/debug @{-g, /CHECKS=ASSERTIONS, /CHECKS=OVERFLOW, /CHECKS=ELABORATION@}
8050   PROC.ALI, PROC.OBJ
8051   PACK.ALI, PACK.OBJ
8052 @end group
8053 @group
8054 /common/release @{/OPTIMIZE=ALL@}
8055   PROC.ALI, PROC.OBJ
8056   PACK.ALI, PACK.OBJ
8057 @end group
8058 @end smallexample
8059 Here are the project files:
8060 @smallexample
8061 @group
8062 project Debug is
8063   for Object_Dir use "debug";
8064   for Main use ("proc");
8066   package Builder is
8067     for Default_Qualifiers ("Ada") use ("-g");
8068   end Builder;
8069 @end group
8071 @group
8072   package Compiler is
8073     for Default_Qualifiers ("Ada")
8074        use ("-fstack-check", "/CHECKS=ASSERTIONS", "/CHECKS=OVERFLOW", "/CHECKS=ELABORATION");
8075   end Compiler;
8076 end Debug;
8077 @end group
8078 @end smallexample
8080 @smallexample
8081 @group
8082 project Release is
8083   for Object_Dir use "release";
8084   for Exec_Dir use ".";
8085   for Main use ("proc");
8087   package Compiler is
8088     for Default_Qualifiers ("Ada") use ("/OPTIMIZE=ALL");
8089   end Compiler;
8090 end Release;
8091 @end group
8092 @end smallexample
8094 @noindent
8095 The name of the project defined by @file{debug.gpr} is @code{"Debug"} (case
8096 insensitive), and analogously the project defined by @file{release.gpr} is
8097 @code{"Release"}.  For consistency the file should have the same name as the
8098 project, and the project file's extension should be @code{"gpr"}. These
8099 conventions are not required, but a warning is issued if they are not followed.
8101 If the current directory is @file{/temp}, then the command
8102 @smallexample
8103 GNAT MAKE -P/common/debug.gpr
8104 @end smallexample
8106 @noindent
8107 generates object and ALI files in @file{/common/debug}, and the @code{proc}
8108 executable also in @file{/common/debug}, using the qualifier settings defined in
8109 the project file.
8111 Likewise, the command
8112 @smallexample
8113 GNAT MAKE -P/common/release.gpr
8114 @end smallexample
8116 @noindent
8117 generates object and ALI files in @file{/common/release}, and the @code{proc}
8118 executable in @file{/common}, using the qualifier settings from the project file.
8120 @node Source Files
8121 @unnumberedsubsubsec Source Files
8123 @noindent
8124 If a project file does not explicitly specify a set of source directories or
8125 a set of source files, then by default the project's source files are the
8126 Ada source files in the project file directory.  Thus @file{PACK.ADS},
8127 @file{PACK.ADB}, and @file{PROC.ADB} are the source files for both projects.
8129 @node Specifying the Object Directory
8130 @unnumberedsubsubsec Specifying the Object Directory
8132 @noindent
8133 Several project properties are modeled by Ada-style @emph{attributes};
8134 you define the property by supplying the equivalent of an Ada attribute
8135 definition clause in the project file.
8136 A project's object directory is such a property; the corresponding
8137 attribute is @code{Object_Dir}, and its value is a string expression.  A
8138 directory may be specified either as absolute or as relative; in the latter
8139 case, it is relative to the project file directory.  Thus the compiler's
8140 output is directed to @file{/common/debug} (for the @code{Debug} project)
8141 and to @file{/common/release} (for the @code{Release} project).  If
8142 @code{Object_Dir} is not specified, then the default is the project file
8143 directory.
8145 @node Specifying the Exec Directory
8146 @unnumberedsubsubsec Specifying the Exec Directory
8148 @noindent
8149 A project's exec directory is another property; the corresponding
8150 attribute is @code{Exec_Dir}, and its value is also a string expression,
8151 either specified as relative or absolute. If @code{Exec_Dir} is not specified,
8152 then the default is the object directory (which may also be the project file
8153 directory if attribute @code{Object_Dir} is not specified). Thus the executable
8154 is placed in @file{/common/debug} for the @code{Debug} project (attribute
8155 @code{Exec_Dir} not specified) and in @file{/common} for the @code{Release}
8156 project.
8158 @node Project File Packages
8159 @unnumberedsubsubsec Project File Packages
8161 @noindent
8162 A GNAT tool integrated with the Project Manager is modeled by a
8163 corresponding package in the project file.
8164 The @code{Debug} project defines the packages @code{Builder}
8165 (for @command{GNAT MAKE}) and @code{Compiler};
8166 the @code{Release} project defines only the @code{Compiler} package.
8168 The Ada package syntax is not to be taken literally.  Although packages in
8169 project files bear a surface resemblance to packages in Ada source code, the
8170 notation is simply a way to convey a grouping of properties for a named
8171 entity.  Indeed, the package names permitted in project files are restricted
8172 to a predefined set, corresponding to the project-aware tools, and the contents
8173 of packages are limited to a small set of constructs.
8174 The packages in the example above contain attribute definitions.
8177 @node Specifying Qualifier Settings
8178 @unnumberedsubsubsec Specifying Qualifier Settings
8180 @noindent
8181 Qualifier settings for a project-aware tool can be specified through attributes
8182 in the package corresponding to the tool.
8183 The example above illustrates one of the relevant attributes,
8184 @code{Default_Qualifiers}, defined in the packages in both project files.
8185 Unlike simple attributes like @code{Source_Dirs}, @code{Default_Qualifiers} is
8186 known as an @emph{associative array}.  When you define this attribute, you must
8187 supply an "index" (a literal string), and the effect of the attribute
8188 definition is to set the value of the "array" at the specified "index".
8189 For the @code{Default_Qualifiers} attribute, the index is a programming
8190 language (in our case, Ada) , and the value specified (after @code{use})
8191 must be a list of string expressions.
8193 The attributes permitted in project files are restricted to a predefined set.
8194 Some may appear at project level, others in packages.
8195 For any attribute that is an associate array, the index must always be a
8196 literal string, but the restrictions on this string (e.g., a file name or a
8197 language name) depend on the individual attribute.
8198 Also depending on the attribute, its specified value will need to be either a
8199 string or a string list.
8201 In the @code{Debug} project, we set the qualifiers for two tools,
8202 @command{GNAT MAKE} and the compiler, and thus we include corresponding
8203 packages, with each package defining the @code{Default_Qualifiers} attribute
8204 with index @code{"Ada"}.
8205 Note that the package corresponding to
8206 @command{GNAT MAKE} is named @code{Builder}.  The @code{Release} project is
8207 similar, but with just the @code{Compiler} package.
8209 In project @code{Debug} above the qualifiers starting with @option{-gnat} that
8210 are specified in package @code{Compiler} could have been placed in package
8211 @code{Builder}, since @command{GNAT MAKE} transmits all such qualifiers to the
8212 compiler.
8214 @node Main Subprograms
8215 @unnumberedsubsubsec Main Subprograms
8217 @noindent
8218 One of the properties of a project is its list of main subprograms (actually
8219 a list of names of source files containing main subprograms, with the file
8220 extension optional.  This property is captured in the @code{Main} attribute,
8221 whose value is a list of strings.  If a project defines the @code{Main}
8222 attribute, then you do not need to identify the main subprogram(s) when
8223 invoking @command{GNAT MAKE} (see @ref{GNAT MAKE and Project Files}).
8225 @node Source File Naming Conventions
8226 @unnumberedsubsubsec Source File Naming Conventions
8228 @noindent
8229 Since the project files do not specify any source file naming conventions,
8230 the GNAT defaults are used.  The mechanism for defining source file naming
8231 conventions -- a package named @code{Naming} -- will be described below
8232 (@pxref{Naming Schemes}).
8234 @node Source Language(s)
8235 @unnumberedsubsubsec Source Language(s)
8237 @noindent
8238 Since the project files do not specify a @code{Languages} attribute, by
8239 default the GNAT tools assume that the language of the project file is Ada.
8240 More generally, a project can comprise source files
8241 in Ada, C, and/or other languages.
8243 @node Using External Variables
8244 @subsection Using External Variables
8246 @noindent
8247 Instead of supplying different project files for debug and release, we can
8248 define a single project file that queries an external variable (set either
8249 on the command line or via an environment variable) in order to
8250 conditionally define the appropriate settings.  Again, assume that the
8251 source files @file{PACK.ADS}, @file{PACK.ADB}, and @file{PROC.ADB} are
8252 located in directory @file{/common}.  The following project file,
8253 @file{build.gpr}, queries the external variable named @code{STYLE} and
8254 defines an object directory and qualifier settings based on whether the value
8255 is @code{"deb"} (debug) or @code{"rel"} (release), where the default is
8256 @code{"deb"}.
8258 @smallexample
8259 @group
8260 project Build is
8261   for Main use ("proc");
8263   type Style_Type is ("deb", "rel");
8264   Style : Style_Type := external ("STYLE", "deb");
8266   case Style is
8267     when "deb" =>
8268       for Object_Dir use "debug";
8270     when "rel" =>
8271       for Object_Dir use "release";
8272       for Exec_Dir use ".";
8273   end case;
8274 @end group
8276 @group
8277   package Builder is
8279     case Style is
8280       when "deb" =>
8281         for Default_Qualifiers ("Ada") use ("-g");
8282     end case;
8284   end Builder;
8285 @end group
8287 @group
8288   package Compiler is
8290     case Style is
8291       when "deb" =>
8292         for Default_Qualifiers ("Ada") use ("/CHECKS=ASSERTIONS", "/CHECKS=OVERFLOW", "/CHECKS=ELABORATION");
8294       when "rel" =>
8295         for Default_Qualifiers ("Ada") use ("/OPTIMIZE=ALL");
8296     end case;
8298   end Compiler;
8300 end Build;
8301 @end group
8302 @end smallexample
8304 @noindent
8305 @code{Style_Type} is an example of a @emph{string type}, which is the project
8306 file analog of an Ada enumeration type but containing string literals rather
8307 than identifiers.  @code{Style} is declared as a variable of this type.
8309 The form @code{external("STYLE", "deb")} is known as an
8310 @emph{external reference}; its first argument is the name of an
8311 @emph{external variable}, and the second argument is a default value to be
8312 used if the external variable doesn't exist.  You can define an external
8313 variable on the command line via the @option{-X} qualifier, or you can use an
8314 environment variable as an external variable.
8316 Each @code{case} construct is expanded by the Project Manager based on the
8317 value of @code{Style}. Thus the command
8318 @smallexample
8319 GNAT MAKE -P/common/build.gpr -XSTYLE=deb
8320 @end smallexample
8322 @noindent
8323 is equivalent to the @command{GNAT MAKE} invocation using the project file
8324 @file{debug.gpr} in the earlier example.  So is the command
8325 @smallexample
8326 GNAT MAKE -P/common/build.gpr
8327 @end smallexample
8329 @noindent
8330 since @code{"deb"} is the default for @code{STYLE}.
8332 Analogously,
8333 @smallexample
8334 GNAT MAKE -P/common/build.gpr -XSTYLE=rel
8335 @end smallexample
8337 @noindent
8338 is equivalent to the @command{GNAT MAKE} invocation using the project file
8339 @file{release.gpr} in the earlier example.
8342 @node Importing Other Projects
8343 @subsection Importing Other Projects
8345 @noindent
8346 A compilation unit in a source file in one project may depend on compilation
8347 units in source files in other projects.  To obtain this behavior, the
8348 dependent project must @emph{import} the projects containing the needed source
8349 files.  This effect is embodied in syntax similar to an Ada @code{with} clause,
8350 but the "with"ed entities are strings denoting project files.
8352 As an example, suppose that the two projects @code{GUI_Proj} and
8353 @code{Comm_Proj} are defined in the project files @file{gui_proj.gpr} and
8354 @file{comm_proj.gpr} in directories @file{/gui} and @file{/comm},
8355 respectively.  Assume that the source files for @code{GUI_Proj} are
8356 @file{GUI.ADS} and @file{GUI.ADB}, and that the source files for
8357 @code{Comm_Proj} are @file{COMM.ADS} and @file{COMM.ADB}, with each set of
8358 files located in its respective project file directory.  Diagrammatically:
8360 @smallexample
8361 @group
8362 /gui
8363   gui_proj.gpr
8364   GUI.ADS
8365   GUI.ADB
8366 @end group
8368 @group
8369 /comm
8370   comm_proj.gpr
8371   COMM.ADS
8372   COMM.ADB
8373 @end group
8374 @end smallexample
8376 @noindent
8377 We want to develop an application in directory @file{/app} that "with"s the
8378 packages @code{GUI} and @code{Comm}, using the properties of the
8379 corresponding project files (e.g. the qualifier settings and object directory).
8380 Skeletal code for a main procedure might be something like the following:
8382 @smallexample
8383 @group
8384 with GUI, Comm;
8385 procedure App_Main is
8386    ...
8387 begin
8388    ...
8389 end App_Main;
8390 @end group
8391 @end smallexample
8393 @noindent
8394 Here is a project file, @file{app_proj.gpr}, that achieves the desired
8395 effect:
8397 @smallexample
8398 @group
8399 with "/gui/gui_proj", "/comm/comm_proj";
8400 project App_Proj is
8401    for Main use ("app_main");
8402 end App_Proj;
8403 @end group
8404 @end smallexample
8406 @noindent
8407 Building an executable is achieved through the command:
8408 @smallexample
8409 GNAT MAKE -P/app/app_proj
8410 @end smallexample
8411 @noindent
8412 which will generate the @code{app_main} executable in the directory where
8413 @file{app_proj.gpr} resides.
8415 If an imported project file uses the standard extension (@code{gpr}) then
8416 (as illustrated above) the @code{with} clause can omit the extension.
8418 Our example specified an absolute path for each imported project file.
8419 Alternatively, you can omit the directory if either
8420 @itemize @bullet
8421 @item
8422 The imported project file is in the same directory as the importing project
8423 file, or
8424 @item
8425 You have defined an environment variable @code{ADA_PROJECT_PATH} that
8426 includes the directory containing the needed project file.
8427 @end itemize
8429 @noindent
8430 Thus, if we define @code{ADA_PROJECT_PATH} to include @file{/gui} and
8431 @file{/comm}, then our project file @file{app_proj.gpr} could be written as
8432 follows:
8434 @smallexample
8435 @group
8436 with "gui_proj", "comm_proj";
8437 project App_Proj is
8438    for Main use ("app_main");
8439 end App_Proj;
8440 @end group
8441 @end smallexample
8443 @noindent
8444 Importing other projects raises the possibility of ambiguities.  For
8445 example, the same unit might be present in different imported projects, or
8446 it might be present in both the importing project and an imported project.
8447 Both of these conditions are errors.  Note that in the current version of
8448 the Project Manager, it is illegal to have an ambiguous unit even if the
8449 unit is never referenced by the importing project.  This restriction may be
8450 relaxed in a future release.
8452 @node Extending a Project
8453 @subsection Extending a Project
8455 @noindent
8456 A common situation in large software systems is to have multiple
8457 implementations for a common interface; in Ada terms, multiple versions of a
8458 package body for the same specification.  For example, one implementation
8459 might be safe for use in tasking programs, while another might only be used
8460 in sequential applications.  This can be modeled in GNAT using the concept
8461 of @emph{project extension}.  If one project (the "child") @emph{extends}
8462 another project (the "parent") then by default all source files of the
8463 parent project are inherited by the child, but the child project can
8464 override any of the parent's source files with new versions, and can also
8465 add new files.  This facility is the project analog of extension in
8466 Object-Oriented Programming.  Project hierarchies are permitted (a child
8467 project may be the parent of yet another project), and a project that
8468 inherits one project can also import other projects.
8470 As an example, suppose that directory @file{/seq} contains the project file
8471 @file{seq_proj.gpr} and the source files @file{PACK.ADS}, @file{PACK.ADB},
8472 and @file{PROC.ADB}:
8474 @smallexample
8475 @group
8476 /seq
8477   PACK.ADS
8478   PACK.ADB
8479   PROC.ADB
8480   seq_proj.gpr
8481 @end group
8482 @end smallexample
8484 @noindent
8485 Note that the project file can simply be empty (that is, no attribute or
8486 package is defined):
8488 @smallexample
8489 @group
8490 project Seq_Proj is
8491 end Seq_Proj;
8492 @end group
8493 @end smallexample
8495 @noindent
8496 implying that its source files are all the Ada source files in the project
8497 directory.
8499 Suppose we want to supply an alternate version of @file{PACK.ADB}, in
8500 directory @file{/tasking}, but use the existing versions of @file{PACK.ADS}
8501 and @file{PROC.ADB}.  We can define a project @code{Tasking_Proj} that
8502 inherits @code{Seq_Proj}:
8504 @smallexample
8505 @group
8506 /tasking
8507   PACK.ADB
8508   tasking_proj.gpr
8509 @end group
8511 @group
8512 project Tasking_Proj extends "/seq/seq_proj" is
8513 end Tasking_Proj;
8514 @end group
8515 @end smallexample
8517 @noindent
8518 The version of @file{PACK.ADB} used in a build depends on which project file
8519 is specified.
8521 Note that we could have designed this using project import rather than
8522 project inheritance; a @code{base} project would contain the sources for
8523 @file{PACK.ADS} and @file{PROC.ADB}, a sequential project would import
8524 @code{base} and add @file{PACK.ADB}, and likewise a tasking project would
8525 import @code{base} and add a different version of @file{PACK.ADB}.  The
8526 choice depends on whether other sources in the original project need to be
8527 overridden.  If they do, then project extension is necessary, otherwise,
8528 importing is sufficient.
8531 @c ***********************
8532 @c * Project File Syntax *
8533 @c ***********************
8535 @node Project File Syntax
8536 @section Project File Syntax
8538 @menu
8539 * Basic Syntax::
8540 * Packages::
8541 * Expressions::
8542 * String Types::
8543 * Variables::
8544 * Attributes::
8545 * Associative Array Attributes::
8546 * case Constructions::
8547 @end menu
8549 @noindent
8550 This section describes the structure of project files.
8552 A project may be an @emph{independent project}, entirely defined by a single
8553 project file. Any Ada source file in an independent project depends only
8554 on the predefined library and other Ada source files in the same project.
8556 @noindent
8557 A project may also @dfn{depend on} other projects, in either or both of the following ways:
8558 @itemize @bullet
8559 @item It may import any number of projects
8560 @item It may extend at most one other project
8561 @end itemize
8563 @noindent
8564 The dependence relation is a directed acyclic graph (the subgraph reflecting
8565 the "extends" relation is a tree).
8567 A project's @dfn{immediate sources} are the source files directly defined by
8568 that project, either implicitly by residing in the project file's directory,
8569 or explicitly through any of the source-related attributes described below.
8570 More generally, a project @var{proj}'s @dfn{sources} are the immediate sources
8571 of @var{proj} together with the immediate sources (unless overridden) of any
8572 project on which @var{proj} depends (either directly or indirectly).
8574 @node Basic Syntax
8575 @subsection Basic Syntax
8577 @noindent
8578 As seen in the earlier examples, project files have an Ada-like syntax.
8579 The minimal project file is:
8580 @smallexample
8581 @group
8582 project Empty is
8584 end Empty;
8585 @end group
8586 @end smallexample
8588 @noindent
8589 The identifier @code{Empty} is the name of the project.
8590 This project name must be present after the reserved
8591 word @code{end} at the end of the project file, followed by a semi-colon.
8593 Any name in a project file, such as the project name or a variable name,
8594 has the same syntax as an Ada identifier.
8596 The reserved words of project files are the Ada reserved words plus
8597 @code{extends}, @code{external}, and @code{project}.  Note that the only Ada
8598 reserved words currently used in project file syntax are:
8600 @itemize @bullet
8601 @item
8602 @code{case}
8603 @item
8604 @code{end}
8605 @item
8606 @code{for}
8607 @item
8608 @code{is}
8609 @item
8610 @code{others}
8611 @item
8612 @code{package}
8613 @item
8614 @code{renames}
8615 @item
8616 @code{type}
8617 @item
8618 @code{use}
8619 @item
8620 @code{when}
8621 @item
8622 @code{with}
8623 @end itemize
8625 @noindent
8626 Comments in project files have the same syntax as in Ada, two consecutives
8627 hyphens through the end of the line.
8629 @node Packages
8630 @subsection Packages
8632 @noindent
8633 A project file may contain @emph{packages}. The name of a package must be one
8634 of the identifiers (case insensitive) from a predefined list, and a package
8635 with a given name may only appear once in a project file. The predefined list
8636 includes the following packages:
8638 @itemize @bullet
8639 @item
8640 @code{Naming}
8641 @item
8642 @code{Builder}
8643 @item
8644 @code{Compiler}
8645 @item
8646 @code{Binder}
8647 @item
8648 @code{Linker}
8649 @item
8650 @code{Finder}
8651 @item
8652 @code{Cross_Reference}
8653 @item
8654 @code{GNAT LIST}
8655 @end itemize
8657 @noindent
8658 (The complete list of the package names and their attributes can be found
8659 in file @file{PRJ-ATTR.ADB}).
8661 @noindent
8662 In its simplest form, a package may be empty:
8664 @smallexample
8665 @group
8666 project Simple is
8667   package Builder is
8668   end Builder;
8669 end Simple;
8670 @end group
8671 @end smallexample
8673 @noindent
8674 A package may contain @emph{attribute declarations},
8675 @emph{variable declarations} and @emph{case constructions}, as will be
8676 described below.
8678 When there is ambiguity between a project name and a package name,
8679 the name always designates the project. To avoid possible confusion, it is
8680 always a good idea to avoid naming a project with one of the
8681 names allowed for packages or any name that starts with @code{gnat}.
8684 @node Expressions
8685 @subsection Expressions
8687 @noindent
8688 An @emph{expression} is either a @emph{string expression} or a
8689 @emph{string list expression}.
8691 A @emph{string expression} is either a @emph{simple string expression} or a
8692 @emph{compound string expression}.
8694 A @emph{simple string expression} is one of the following:
8695 @itemize @bullet
8696 @item A literal string; e.g.@code{"comm/my_proj.gpr"}
8697 @item A string-valued variable reference (see @ref{Variables})
8698 @item A string-valued attribute reference (see @ref{Attributes})
8699 @item An external reference (see @ref{External References in Project Files})
8700 @end itemize
8702 @noindent
8703 A @emph{compound string expression} is a concatenation of string expressions,
8704 using @code{"&"}
8705 @smallexample
8706        Path & "/" & File_Name & ".ADS"
8707 @end smallexample
8709 @noindent
8710 A @emph{string list expression} is either a
8711 @emph{simple string list expression} or a
8712 @emph{compound string list expression}.
8714 A @emph{simple string list expression} is one of the following:
8715 @itemize @bullet
8716 @item A parenthesized list of zero or more string expressions, separated by commas
8717 @smallexample
8718    File_Names := (File_Name, "GNAT.ADC", File_Name & ".orig");
8719    Empty_List := ();
8720 @end smallexample
8721 @item A string list-valued variable reference
8722 @item A string list-valued attribute reference
8723 @end itemize
8725 @noindent
8726 A @emph{compound string list expression} is the concatenation (using
8727 @code{"&"}) of a simple string list expression and an expression.  Note that
8728 each term in a compound string list expression, except the first, may be
8729 either a string expression or a string list expression.
8731 @smallexample
8732 @group
8733    File_Name_List := () & File_Name; --  One string in this list
8734    Extended_File_Name_List := File_Name_List & (File_Name & ".orig");
8735    --  Two strings
8736    Big_List := File_Name_List & Extended_File_Name_List;
8737    --  Concatenation of two string lists: three strings
8738    Illegal_List := "GNAT.ADC" & Extended_File_Name_List;
8739    --  Illegal: must start with a string list
8740 @end group
8741 @end smallexample
8744 @node String Types
8745 @subsection String Types
8747 @noindent
8748 The value of a variable may be restricted to a list of string literals.
8749 The restricted list of string literals is given in a
8750 @emph{string type declaration}.
8752 Here is an example of a string type declaration:
8754 @smallexample
8755    type OS is ("NT, "nt", "Unix", "Linux", "other OS");
8756 @end smallexample
8758 @noindent
8759 Variables of a string type are called @emph{typed variables}; all other
8760 variables are called @emph{untyped variables}. Typed variables are
8761 particularly useful in @code{case} constructions
8762 (see @ref{case Constructions}).
8764 A string type declaration starts with the reserved word @code{type}, followed
8765 by the name of the string type (case-insensitive), followed by the reserved
8766 word @code{is}, followed by a parenthesized list of one or more string literals
8767 separated by commas, followed by a semicolon.
8769 The string literals in the list are case sensitive and must all be different.
8770 They may include any graphic characters allowed in Ada, including spaces.
8772 A string type may only be declared at the project level, not inside a package.
8774 A string type may be referenced by its name if it has been declared in the same
8775 project file, or by its project name, followed by a dot,
8776 followed by the string type name.
8779 @node Variables
8780 @subsection Variables
8782 @noindent
8783 A variable may be declared at the project file level, or in a package.
8784 Here are some examples of variable declarations:
8786 @smallexample
8787 @group
8788    This_OS : OS := external ("OS"); --  a typed variable declaration
8789    That_OS := "Linux";              --  an untyped variable declaration
8790 @end group
8791 @end smallexample
8793 @noindent
8794 A @emph{typed variable declaration} includes the variable name, followed by a colon,
8795 followed by the name of a string type, followed by @code{:=}, followed by
8796 a simple string expression.
8798 An @emph{untyped variable declaration} includes the variable name,
8799 followed by @code{:=}, followed by an expression.  Note that, despite the
8800 terminology, this form of "declaration" resembles more an assignment
8801 than a declaration in Ada.  It is a declaration in several senses:
8802 @itemize @bullet
8803 @item
8804 The variable name does not need to be defined previously
8805 @item
8806 The declaration establishes the @emph{kind} (string versus string list) of the
8807 variable, and later declarations of the same variable need to be consistent
8808 with this
8809 @end itemize
8811 @noindent
8812 A string variable declaration (typed or untyped) declares a variable
8813 whose value is a string. This variable may be used as a string expression.
8814 @smallexample
8815    File_Name       := "readme.txt";
8816    Saved_File_Name := File_Name & ".saved";
8817 @end smallexample
8819 @noindent
8820 A string list variable declaration declares a variable whose value is a list
8821 of strings. The list may contain any number (zero or more) of strings.
8823 @smallexample
8824    Empty_List := ();
8825    List_With_One_Element := ("/STYLE=");
8826    List_With_Two_Elements := List_With_One_Element & "/STYLE=GNAT";
8827    Long_List := ("MAIN.ADA", "PACK1_.ADA", "PACK1.ADA", "PACK2_.ADA"
8828                  "PACK2.ADA", "UTIL_.ADA", "UTIL.ADA");
8829 @end smallexample
8831 @noindent
8832 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.
8834 The same untyped variable may be declared several times.
8835 In this case, the new value replaces the old one,
8836 and any subsequent reference to the variable uses the new value.
8837 However, as noted above, if a variable has been declared as a string, all subsequent
8838 declarations must give it a string value. Similarly, if a variable has
8839 been declared as a string list, all subsequent declarations
8840 must give it a string list value.
8842 A @emph{variable reference} may take several forms:
8844 @itemize @bullet
8845 @item The simple variable name, for a variable in the current package (if any) or in the current project
8846 @item A context name, followed by a dot, followed by the variable name.
8847 @end itemize
8849 @noindent
8850 A @emph{context} may be one of the following:
8852 @itemize @bullet
8853 @item The name of an existing package in the current project
8854 @item The name of an imported project of the current project
8855 @item The name of an ancestor project (i.e., a project extended by the current project, either directly or indirectly)
8856 @item An imported/parent project name, followed by a dot, followed by a package name
8857 @end itemize
8859 @noindent
8860 A variable reference may be used in an expression.
8863 @node Attributes
8864 @subsection Attributes
8866 @noindent
8867 A project (and its packages) may have @emph{attributes} that define the project's properties.
8868 Some attributes have values that are strings;
8869 others have values that are string lists.
8871 There are two categories of attributes: @emph{simple attributes} and @emph{associative arrays}
8872 (see @ref{Associative Array Attributes}).
8874 The names of the attributes are restricted; there is a list of project
8875 attributes, and a list of package attributes for each package.
8876 The names are not case sensitive.
8878 The project attributes are as follows (all are simple attributes):
8880 @multitable @columnfractions .4 .3
8881 @item @emph{Attribute Name}
8882 @tab @emph{Value}
8883 @item @code{Source_Files}
8884 @tab string list
8885 @item @code{Source_Dirs}
8886 @tab string list
8887 @item @code{Source_List_File}
8888 @tab string
8889 @item @code{Object_Dir}
8890 @tab string
8891 @item @code{Exec_Dir}
8892 @tab string
8893 @item @code{Main}
8894 @tab string list
8895 @item @code{Languages}
8896 @tab string list
8897 @item @code{Library_Dir}
8898 @tab string
8899 @item @code{Library_Name}
8900 @tab string
8901 @item @code{Library_Kind}
8902 @tab string
8903 @item @code{Library_Elaboration}
8904 @tab string
8905 @item @code{Library_Version}
8906 @tab string
8907 @end multitable
8909 @noindent
8910 The attributes for package @code{Naming} are as follows
8911 (see @ref{Naming Schemes}):
8913 @multitable @columnfractions .4 .2 .2 .2
8914 @item Attribute Name @tab Category @tab Index @tab Value
8915 @item @code{Specification_Suffix}
8916 @tab associative array
8917 @tab language name
8918 @tab string
8919 @item @code{Implementation_Suffix}
8920 @tab associative array
8921 @tab language name
8922 @tab string
8923 @item @code{Separate_Suffix}
8924 @tab simple attribute
8925 @tab n/a
8926 @tab string
8927 @item @code{Casing}
8928 @tab simple attribute
8929 @tab n/a
8930 @tab string
8931 @item @code{Dot_Replacement}
8932 @tab simple attribute
8933 @tab n/a
8934 @tab string
8935 @item @code{Specification}
8936 @tab associative array
8937 @tab Ada unit name
8938 @tab string
8939 @item @code{Implementation}
8940 @tab associative array
8941 @tab Ada unit name
8942 @tab string
8943 @item @code{Specification_Exceptions}
8944 @tab associative array
8945 @tab language name
8946 @tab string list
8947 @item @code{Implementation_Exceptions}
8948 @tab associative array
8949 @tab language name
8950 @tab string list
8951 @end multitable
8953 @noindent
8954 The attributes for package @code{Builder}, @code{Compiler}, @code{Binder},
8955 @code{Linker}, @code{Cross_Reference}, and @code{Finder}
8956 are as follows (see @ref{Qualifiers and Project Files}).
8958 @multitable @columnfractions .4 .2 .2 .2
8959 @item Attribute Name @tab Category @tab Index @tab Value
8960 @item @code{Default_Qualifiers}
8961 @tab associative array
8962 @tab language name
8963 @tab string list
8964 @item @code{Qualifiers}
8965 @tab associative array
8966 @tab file name
8967 @tab string list
8968 @end multitable
8970 @noindent
8971 In addition, package @code{Builder} has a single string attribute
8972 @code{Local_Configuration_Pragmas} and package @code{Builder} has a single
8973 string attribute @code{Global_Configuration_Pragmas}.
8975 @noindent
8976 The attribute for package @code{Glide} are not documented: they are for
8977 internal use only.
8979 @noindent
8980 Each simple attribute has a default value: the empty string (for string-valued
8981 attributes) and the empty list (for string list-valued attributes).
8983 Similar to variable declarations, an attribute declaration defines a new value
8984 for an attribute.
8986 Examples of simple attribute declarations:
8988 @smallexample
8989    for Object_Dir use "objects";
8990    for Source_Dirs use ("units", "test/drivers");
8991 @end smallexample
8993 @noindent
8994 A @dfn{simple attribute declaration} starts with the reserved word @code{for},
8995 followed by the name of the attribute, followed by the reserved word
8996 @code{use}, followed by an expression (whose kind depends on the attribute),
8997 followed by a semicolon.
8999 Attributes may be referenced in expressions.
9000 The general form for such a reference is @code{<entity>'<attribute>}:
9001 the entity for which the attribute is defined,
9002 followed by an apostrophe, followed by the name of the attribute.
9003 For associative array attributes, a litteral string between parentheses
9004 need to be supplied as index.
9006 Examples are:
9008 @smallexample
9009   project'Object_Dir
9010   Naming'Dot_Replacement
9011   Imported_Project'Source_Dirs
9012   Imported_Project.Naming'Casing
9013   Builder'Default_Qualifiers("Ada")
9014 @end smallexample
9016 @noindent
9017 The entity may be:
9018 @itemize @bullet
9019 @item @code{project} for an attribute of the current project
9020 @item The name of an existing package of the current project
9021 @item The name of an imported project
9022 @item The name of a parent project (extended by the current project)
9023 @item An imported/parent project name, followed by a dot,
9024       followed by a package name
9025 @end itemize
9027 @noindent
9028 Example:
9029 @smallexample
9030 @group
9031    project Prj is
9032      for Source_Dirs use project'Source_Dirs & "units";
9033      for Source_Dirs use project'Source_Dirs & "test/drivers"
9034    end Prj;
9035 @end group
9036 @end smallexample
9038 @noindent
9039 In the first attribute declaration, initially the attribute @code{Source_Dirs}
9040 has the default value: an empty string list. After this declaration,
9041 @code{Source_Dirs} is a string list of one element: "units".
9042 After the second attribute declaration @code{Source_Dirs} is a string list of
9043 two elements: "units" and "test/drivers".
9045 Note: this example is for illustration only. In practice,
9046 the project file would contain only one attribute declaration:
9048 @smallexample
9049    for Source_Dirs use ("units", "test/drivers");
9050 @end smallexample
9053 @node Associative Array Attributes
9054 @subsection Associative Array Attributes
9056 @noindent
9057 Some attributes are defined as @emph{associative arrays}. An associative
9058 array may be regarded as a function that takes a string as a parameter
9059 and delivers a string or string list value as its result.
9061 Here are some examples of associative array attribute declarations:
9063 @smallexample
9064    for Implementation ("main") use "MAIN.ADA";
9065    for Qualifiers ("MAIN.ADA") use ("-v", "/REPORT_ERRORS=VERBOSE");
9066    for Qualifiers ("MAIN.ADA") use Builder'Qualifiers ("MAIN.ADA") & "-g";
9067 @end smallexample
9069 @noindent
9070 Like untyped variables and simple attributes, associative array attributes may be declared several times. Each declaration supplies a new value for the
9071 attribute, replacing the previous setting.
9074 @node case Constructions
9075 @subsection @code{case} Constructions
9077 @noindent
9078 A @code{case} construction is used in a project file to effect conditional
9079 behavior.
9080 Here is a typical example:
9082 @smallexample
9083 @group
9084 project MyProj is
9085    type OS_Type is ("Linux", "Unix", "NT", "VMS");
9087    OS : OS_Type := external ("OS", "Linux");
9088 @end group
9090 @group
9091    package Compiler is
9092      case OS is
9093        when "Linux" | "Unix" =>
9094          for Default_Qualifiers ("Ada") use ("-gnath");
9095        when "NT" =>
9096          for Default_Qualifiers ("Ada") use ("/POLLING_ENABLE");
9097        when others =>
9098      end case;
9099    end Compiler;
9100 end MyProj;
9101 @end group
9102 @end smallexample
9104 @noindent
9105 The syntax of a @code{case} construction is based on the Ada case statement
9106 (although there is no @code{null} construction for empty alternatives).
9108 Following the reserved word @code{case} there is the case variable (a typed
9109 string variable), the reserved word @code{is}, and then a sequence of one or
9110 more alternatives.
9111 Each alternative comprises the reserved word @code{when}, either a list of
9112 literal strings separated by the @code{"|"} character or the reserved word
9113 @code{others},  and the @code{"=>"} token.
9114 Each literal string must belong to the string type that is the type of the
9115 case variable.
9116 An @code{others} alternative, if present, must occur last.
9117 The @code{end case;} sequence terminates the case construction.
9119 After each @code{=>}, there are zero or more constructions.  The only
9120 constructions allowed in a case construction are other case constructions and
9121 attribute declarations. String type declarations, variable declarations and
9122 package declarations are not allowed.
9124 The value of the case variable is often given by an external reference
9125 (see @ref{External References in Project Files}).
9128 @c ****************************************
9129 @c * Objects and Sources in Project Files *
9130 @c ****************************************
9132 @node Objects and Sources in Project Files
9133 @section Objects and Sources in Project Files
9135 @menu
9136 * Object Directory::
9137 * Exec Directory::
9138 * Source Directories::
9139 * Source File Names::
9140 @end menu
9142 @noindent
9143 Each project has exactly one object directory and one or more source
9144 directories. The source directories must contain at least one source file,
9145 unless  the project file explicitly specifies that no source files are present
9146 (see @ref{Source File Names}).
9149 @node Object Directory
9150 @subsection Object Directory
9152 @noindent
9153 The object directory for a project is the directory containing the compiler's
9154 output (such as @file{ALI} files and object files) for the project's immediate
9155 sources. Note that for inherited sources (when extending a parent project) the
9156 parent project's object directory is used.
9158 The object directory is given by the value of the attribute @code{Object_Dir}
9159 in the project file.
9161 @smallexample
9162    for Object_Dir use "objects";
9163 @end smallexample
9165 @noindent
9166 The attribute @var{Object_Dir} has a string value, the path name of the object
9167 directory. The path name may be absolute or relative to the directory of the
9168 project file. This directory must already exist, and be readable and writable.
9170 By default, when the attribute @code{Object_Dir} is not given an explicit value
9171 or when its value is the empty string, the object directory is the same as the
9172 directory containing the project file.
9175 @node Exec Directory
9176 @subsection Exec Directory
9178 @noindent
9179 The exec directory for a project is the directory containing the executables
9180 for the project's main subprograms.
9182 The exec directory is given by the value of the attribute @code{Exec_Dir}
9183 in the project file.
9185 @smallexample
9186    for Exec_Dir use "executables";
9187 @end smallexample
9189 @noindent
9190 The attribute @var{Exec_Dir} has a string value, the path name of the exec
9191 directory. The path name may be absolute or relative to the directory of the
9192 project file. This directory must already exist, and be writable.
9194 By default, when the attribute @code{Exec_Dir} is not given an explicit value
9195 or when its value is the empty string, the exec directory is the same as the
9196 object directory of the project file.
9199 @node Source Directories
9200 @subsection Source Directories
9202 @noindent
9203 The source directories of a project are specified by the project file
9204 attribute @code{Source_Dirs}.
9206 This attribute's value is a string list. If the attribute is not given an
9207 explicit value, then there is only one source directory, the one where the
9208 project file resides.
9210 A @code{Source_Dirs} attribute that is explicitly defined to be the empty list,
9211 as in
9213 @smallexample
9214     for Source_Dirs use ();
9215 @end smallexample
9217 @noindent
9218 indicates that the project contains no source files.
9220 Otherwise, each string in the string list designates one or more
9221 source directories.
9223 @smallexample
9224    for Source_Dirs use ("sources", "test/drivers");
9225 @end smallexample
9227 @noindent
9228 If a string in the list ends with @code{"/**"},  then the directory whose path
9229 name precedes the two asterisks, as well as all its subdirectories
9230 (recursively), are source directories.
9232 @smallexample
9233    for Source_Dirs use ("/system/sources/**");
9234 @end smallexample
9236 @noindent
9237 Here the directory @code{/system/sources} and all of its subdirectories
9238 (recursively) are source directories.
9240 To specify that the source directories are the directory of the project file
9241 and all of its subdirectories, you can declare @code{Source_Dirs} as follows:
9242 @smallexample
9243    for Source_Dirs use ("./**");
9244 @end smallexample
9246 @noindent
9247 Each of the source directories must exist and be readable.
9250 @node Source File Names
9251 @subsection Source File Names
9253 @noindent
9254 In a project that contains source files, their names may be specified by the
9255 attributes @code{Source_Files} (a string list) or @code{Source_List_File}
9256 (a string). Source file names never include any directory information.
9258 If the attribute @code{Source_Files} is given an explicit value, then each
9259 element of the list is a source file name.
9261 @smallexample
9262    for Source_Files use ("MAIN.ADB");
9263    for Source_Files use ("MAIN.ADB", "PACK1.ADS", "PACK2.ADB");
9264 @end smallexample
9266 @noindent
9267 If the attribute @code{Source_Files} is not given an explicit value,
9268 but the attribute @code{Source_List_File} is given a string value,
9269 then the source file names are contained in the text file whose path name
9270 (absolute or relative to the directory of the project file) is the
9271 value of the attribute @code{Source_List_File}.
9273 Each line in the file that is not empty or is not a comment
9274 contains a source file name. A comment line starts with two hyphens.
9276 @smallexample
9277    for Source_List_File use "source_list.txt";
9278 @end smallexample
9280 @noindent
9281 By default, if neither the attribute @code{Source_Files} nor the attribute
9282 @code{Source_List_File} is given an explicit value, then each file in the
9283 source directories that conforms to the project's naming scheme
9284 (see @ref{Naming Schemes}) is an immediate source of the project.
9286 A warning is issued if both attributes @code{Source_Files} and
9287 @code{Source_List_File} are given explicit values. In this case, the attribute
9288 @code{Source_Files} prevails.
9290 Each source file name must be the name of one and only one existing source file
9291 in one of the source directories.
9293 A @code{Source_Files} attribute defined with an empty list as its value
9294 indicates that there are no source files in the project.
9296 Except for projects that are clearly specified as containing no Ada source
9297 files (@code{Source_Dirs} or @code{Source_Files} specified as an empty list,
9298 or @code{Languages} specified without @code{"Ada"} in the list)
9299 @smallexample
9300    for Source_Dirs use ();
9301    for Source_Files use ();
9302    for Languages use ("C", "C++");
9303 @end smallexample
9305 @noindent
9306 a project must contain at least one immediate source.
9308 Projects with no source files are useful as template packages
9309 (see @ref{Packages in Project Files}) for other projects; in particular to
9310 define a package @code{Naming} (see @ref{Naming Schemes}).
9313 @c ****************************
9314 @c * Importing Projects *
9315 @c ****************************
9317 @node  Importing Projects
9318 @section Importing Projects
9320 @noindent
9321 An immediate source of a project P may depend on source files that
9322 are neither immediate sources of P nor in the predefined library.
9323 To get this effect, P must @emph{import} the projects that contain the needed
9324 source files.
9326 @smallexample
9327 @group
9328   with "project1", "utilities.gpr";
9329   with "/namings/apex.gpr";
9330   project Main is
9331     ...
9332 @end group
9333 @end smallexample
9335 @noindent
9336 As can be seen in this example, the syntax for importing projects is similar
9337 to the syntax for importing compilation units in Ada. However, project files
9338 use literal strings instead of names, and the @code{with} clause identifies
9339 project files rather than packages.
9341 Each literal string is the file name or path name (absolute or relative) of a
9342 project file. If a string is simply a file name, with no path, then its
9343 location is determined by the @emph{project path}:
9345 @itemize @bullet
9346 @item
9347 If the environment variable @env{ADA_PROJECT_PATH} exists, then the project
9348 path includes all the directories in this environment variable, plus the
9349 directory of the project file.
9351 @item
9352 If the environment variable @env{ADA_PROJECT_PATH} does not exist,
9353 then the project path contains only one directory, namely the one where
9354 the project file is located.
9355 @end itemize
9357 @noindent
9358 If a relative pathname is used as in
9360 @smallexample
9361   with "tests/proj";
9362 @end smallexample
9364 @noindent
9365 then the path is relative to the directory where the importing project file is
9366 located. Any symbolic link will be fully resolved in the directory
9367 of the importing project file before the imported project file is looked up.
9369 When the @code{with}'ed project file name does not have an extension,
9370 the default is @file{.gpr}. If a file with this extension is not found, then
9371 the file name as specified in the @code{with} clause (no extension) will be
9372 used. In the above example, if a file @code{project1.gpr} is found, then it
9373 will be used; otherwise, if a file @code{project1} exists then it will be used;
9374 if neither file exists, this is an error.
9376 A warning is issued if the name of the project file does not match the
9377 name of the project; this check is case insensitive.
9379 Any source file that is an immediate source of the imported project can be
9380 used by the immediate sources of the importing project, and recursively. Thus
9381 if @code{A} imports @code{B}, and @code{B} imports @code{C}, the immediate
9382 sources of @code{A} may depend on the immediate sources of @code{C}, even if
9383 @code{A} does not import @code{C} explicitly. However, this is not recommended,
9384 because if and when @code{B} ceases to import @code{C}, some sources in
9385 @code{A} will no longer compile.
9387 A side effect of this capability is that cyclic dependences are not permitted:
9388 if @code{A} imports @code{B} (directly or indirectly) then @code{B} is not
9389 allowed to import @code{A}.
9392 @c *********************
9393 @c * Project Extension *
9394 @c *********************
9396 @node Project Extension
9397 @section Project Extension
9399 @noindent
9400 During development of a large system, it is sometimes necessary to use
9401 modified versions of some of the source files without changing the original
9402 sources. This can be achieved through a facility known as
9403 @emph{project extension}.
9405 @smallexample
9406    project Modified_Utilities extends "/baseline/utilities.gpr" is ...
9407 @end smallexample
9409 @noindent
9410 The project file for the project being extended (the @emph{parent}) is
9411 identified by the literal string that follows the reserved word @code{extends},
9412 which itself follows the name of the extending project (the @emph{child}).
9414 By default, a child project inherits all the sources of its parent.
9415 However, inherited sources can be overridden: a unit with the same name as one
9416 in the parent will hide the original unit.
9417 Inherited sources are considered to be sources (but not immediate sources)
9418 of the child project; see @ref{Project File Syntax}.
9420 An inherited source file retains any qualifiers specified in the parent project.
9422 For example if the project @code{Utilities} contains the specification and the
9423 body of an Ada package @code{Util_IO}, then the project
9424 @code{Modified_Utilities} can contain a new body for package @code{Util_IO}.
9425 The original body of @code{Util_IO} will not be considered in program builds.
9426 However, the package specification will still be found in the project
9427 @code{Utilities}.
9429 A child project can have only one parent but it may import any number of other
9430 projects.
9432 A project is not allowed to import directly or indirectly at the same time a
9433 child project and any of its ancestors.
9436 @c ****************************************
9437 @c * External References in Project Files *
9438 @c ****************************************
9440 @node  External References in Project Files
9441 @section External References in Project Files
9443 @noindent
9444 A project file may contain references to external variables; such references
9445 are called @emph{external references}.
9447 An external variable is either defined as part of the environment (an
9448 environment variable in Unix, for example) or else specified on the command
9449 line via the @option{-X@emph{vbl}=@emph{value}} qualifier. If both, then the
9450 command line value is used.
9452 An external reference is denoted by the built-in function
9453 @code{external}, which returns a string value.  This function has two forms:
9454 @itemize @bullet
9455 @item @code{external (external_variable_name)}
9456 @item @code{external (external_variable_name, default_value)}
9457 @end itemize
9459 @noindent
9460 Each parameter must be a string literal.  For example:
9462 @smallexample
9463    external ("USER")
9464    external ("OS", "Linux")
9465 @end smallexample
9467 @noindent
9468 In the form with one parameter, the function returns the value of
9469 the external variable given as parameter. If this name is not present in the
9470 environment, then the returned value is an empty string.
9472 In the form with two string parameters, the second parameter is
9473 the value returned when the variable given as the first parameter is not
9474 present in the environment. In the example above, if @code{"OS"} is not
9475 the name of an environment variable and is not passed on the command line,
9476 then the returned value will be @code{"Linux"}.
9478 An external reference may be part of a string expression or of a string
9479 list expression, to define variables or attributes.
9481 @smallexample
9482 @group
9483    type Mode_Type is ("Debug", "Release");
9484    Mode : Mode_Type := external ("MODE");
9485    case Mode is
9486      when "Debug" =>
9487         ...
9488 @end group
9489 @end smallexample
9492 @c *****************************
9493 @c * Packages in Project Files *
9494 @c *****************************
9496 @node  Packages in Project Files
9497 @section Packages in Project Files
9499 @noindent
9500 The @emph{package} is the project file feature that defines the settings for
9501 project-aware tools.
9502 For each such tool you can declare a corresponding package; the names for these
9503 packages are preset (see @ref{Packages}) but are not case sensitive.
9504 A package may contain variable declarations, attribute declarations, and case
9505 constructions.
9507 @smallexample
9508 @group
9509    project Proj is
9510       package Builder is  -- used by GNAT MAKE
9511          for Default_Qualifiers ("Ada") use ("-v", "-g");
9512       end Builder;
9513    end Proj;
9514 @end group
9515 @end smallexample
9517 @noindent
9518 A package declaration starts with the reserved word @code{package},
9519 followed by the package name (case insensitive), followed by the reserved word
9520 @code{is}. It ends with the reserved word @code{end}, followed by the package
9521 name, finally followed by a semi-colon.
9523 Most of the packages have an attribute @code{Default_Qualifiers}.
9524 This attribute is an associative array, and its value is a string list.
9525 The index of the associative array is the name of a programming language (case
9526 insensitive). This attribute indicates the qualifier or qualifiers to be used
9527 with the corresponding tool.
9529 Some packages also have another attribute, @code{Qualifiers}, an associative
9530 array whose value is a string list. The index is the name of a source file.
9531 This attribute indicates the qualifier or qualifiers to be used by the corresponding
9532 tool when dealing with this specific file.
9534 Further information on these qualifier-related attributes is found in
9535 @ref{Qualifiers and Project Files}.
9537 A package may be declared as a @emph{renaming} of another package; e.g., from
9538 the project file for an imported project.
9540 @smallexample
9541 @group
9542   with "/global/apex.gpr";
9543   project Example is
9544     package Naming renames Apex.Naming;
9545     ...
9546   end Example;
9547 @end group
9548 @end smallexample
9550 @noindent
9551 Packages that are renamed in other project files often come from project files
9552 that have no sources: they are just used as templates. Any modification in the
9553 template will be reflected automatically in all the project files that rename
9554 a package from the template.
9556 In addition to the tool-oriented packages, you can also declare a package
9557 named @code{Naming} to establish specialized source file naming conventions
9558 (see @ref{Naming Schemes}).
9561 @c ************************************
9562 @c * Variables from Imported Projects *
9563 @c ************************************
9565 @node Variables from Imported Projects
9566 @section Variables from Imported Projects
9568 @noindent
9569 An attribute or variable defined in an imported or parent project can
9570 be used in expressions in the importing / extending project.
9571 Such an attribute or variable is prefixed with the name of the project
9572 and (if relevant) the name of package where it is defined.
9574 @smallexample
9575 @group
9576   with "imported";
9577   project Main extends "base" is
9578      Var1 := Imported.Var;
9579      Var2 := Base.Var & ".new";
9580 @end group
9582 @group
9583      package Builder is
9584         for Default_Qualifiers ("Ada") use Imported.Builder.Ada_Qualifiers &
9585                          "/STYLE=GNAT" & "-v";
9586      end Builder;
9587 @end group
9589 @group
9590      package Compiler is
9591         for Default_Qualifiers ("Ada") use Base.Compiler.Ada_Qualifiers;
9592      end Compiler;
9593   end Main;
9594 @end group
9595 @end smallexample
9597 @noindent
9598 In this example:
9600 @itemize @bullet
9601 @item
9602 @code{Var1} is a copy of the variable @code{Var} defined in the project file
9603 @file{"imported.gpr"}
9604 @item
9605 the value of @code{Var2} is a copy of the value of variable @code{Var}
9606 defined in the project file @file{base.gpr}, concatenated with @code{".new"}
9607 @item
9608 attribute @code{Default_Qualifiers ("Ada")} in package @code{Builder}
9609 is a string list that includes in its value a copy of variable
9610 @code{Ada_Qualifiers} defined in the @code{Builder} package in project file
9611 @file{imported.gpr} plus two new elements: @option{"/STYLE=GNAT"} and @option{"-v"};
9612 @item
9613 attribute @code{Default_Qualifiers ("Ada")} in package @code{Compiler}
9614 is a copy of the variable @code{Ada_Qualifiers} defined in the @code{Compiler}
9615 package in project file @file{base.gpr}, the project being extended.
9616 @end itemize
9619 @c ******************
9620 @c * Naming Schemes *
9621 @c ******************
9623 @node  Naming Schemes
9624 @section Naming Schemes
9626 @noindent
9627 Sometimes an Ada software system is ported from a foreign compilation
9628 environment to GNAT, with file names that do not use the default GNAT
9629 conventions. Instead of changing all the file names (which for a variety of
9630 reasons might not be possible), you can define the relevant file naming scheme
9631 in the @code{Naming} package in your project file.  For example, the following
9632 package models the Apex file naming rules:
9634 @smallexample
9635 @group
9636   package Naming is
9637     for Casing                        use "lowercase";
9638     for Dot_Replacement               use ".";
9639     for Specification_Suffix ("Ada")  use ".1.ADA";
9640     for Implementation_Suffix ("Ada") use ".2.ADA";
9641   end Naming;
9642 @end group
9643 @end smallexample
9645 @noindent
9646 You can define the following attributes in package @code{Naming}:
9648 @table @code
9650 @item @var{Casing}
9651 This must be a string with one of the three values @code{"lowercase"},
9652 @code{"uppercase"} or @code{"mixedcase"}; these strings are case insensitive.
9654 @noindent
9655 If @var{Casing} is not specified, then the default is @code{"lowercase"}.
9657 @item @var{Dot_Replacement}
9658 This must be a string whose value satisfies the following conditions:
9660 @itemize @bullet
9661 @item It must not be empty
9662 @item It cannot start or end with an alphanumeric character
9663 @item It cannot be a single underscore
9664 @item It cannot start with an underscore followed by an alphanumeric
9665 @item It cannot contain a dot @code{'.'} except if it the entire string is @code{"."}
9666 @end itemize
9668 @noindent
9669 If @code{Dot_Replacement} is not specified, then the default is @code{"-"}.
9671 @item @var{Specification_Suffix}
9672 This is an associative array (indexed by the programming language name, case
9673 insensitive) whose value is a string that must satisfy the following
9674 conditions:
9676 @itemize @bullet
9677 @item It must not be empty
9678 @item It cannot start with an alphanumeric character
9679 @item It cannot start with an underscore followed by an alphanumeric character
9680 @end itemize
9681 @noindent
9682 If @code{Specification_Suffix ("Ada")} is not specified, then the default is
9683 @code{".ADS"}.
9685 @item @var{Implementation_Suffix}
9686 This is an associative array (indexed by the programming language name, case
9687 insensitive) whose value is a string that must satisfy the following
9688 conditions:
9690 @itemize @bullet
9691 @item It must not be empty
9692 @item It cannot start with an alphanumeric character
9693 @item It cannot start with an underscore followed by an alphanumeric character
9694 @item It cannot be a suffix of @code{Specification_Suffix}
9695 @end itemize
9696 @noindent
9697 If @code{Implementation_Suffix ("Ada")} is not specified, then the default is
9698 @code{".ADB"}.
9700 @item @var{Separate_Suffix}
9701 This must be a string whose value satisfies the same conditions as
9702 @code{Implementation_Suffix}.
9704 @noindent
9705 If @code{Separate_Suffix ("Ada")} is not specified, then it defaults to same
9706 value as @code{Implementation_Suffix ("Ada")}.
9708 @item @var{Specification}
9709 @noindent
9710 You can use the @code{Specification} attribute, an associative array, to define
9711 the source file name for an individual Ada compilation unit's spec. The array
9712 index must be a string literal that identifies the Ada unit (case insensitive).
9713 The value of this attribute must be a string that identifies the file that
9714 contains this unit's spec (case sensitive or insensitive depending on the
9715 operating system).
9717 @smallexample
9718    for Specification ("MyPack.MyChild") use "mypack.mychild.spec";
9719 @end smallexample
9721 @item @var{Implementation}
9723 You can use the @code{Implementation} attribute, an associative array, to
9724 define the source file name for an individual Ada compilation unit's body
9725 (possibly a subunit).  The array index must be a string literal that identifies
9726 the Ada unit (case insensitive).  The value of this attribute must be a string
9727 that identifies the file that contains this unit's body or subunit (case
9728 sensitive or insensitive depending on the operating system).
9730 @smallexample
9731    for Implementation ("MyPack.MyChild") use "mypack.mychild.body";
9732 @end smallexample
9733 @end table
9736 @c ********************
9737 @c * Library Projects *
9738 @c ********************
9740 @node Library Projects
9741 @section Library Projects
9743 @noindent
9744 @emph{Library projects} are projects whose object code is placed in a library.
9745 (Note that this facility is not yet supported on all platforms)
9747 To create a library project, you need to define in its project file
9748 two project-level attributes: @code{Library_Name} and @code{Library_Dir}.
9749 Additionally, you may define the library-related attributes
9750 @code{Library_Kind}, @code{Library_Version} and @code{Library_Elaboration}.
9752 The @code{Library_Name} attribute has a string value that must start with a
9753 letter and include only letters and digits.
9755 The @code{Library_Dir} attribute has a string value that designates the path
9756 (absolute or relative) of the directory where the library will reside.
9757 It must designate an existing directory, and this directory needs to be
9758 different from the project's object directory. It also needs to be writable.
9760 If both @code{Library_Name} and @code{Library_Dir} are specified and
9761 are legal, then the project file defines a library project.  The optional
9762 library-related attributes are checked only for such project files.
9764 The @code{Library_Kind} attribute has a string value that must be one of the
9765 following (case insensitive): @code{"static"}, @code{"dynamic"} or
9766 @code{"relocatable"}. If this attribute is not specified, the library is a
9767 static library. Otherwise, the library may be dynamic or relocatable.
9768 Depending on the operating system, there may or may not be a distinction
9769 between dynamic and relocatable libraries. For example, on Unix there is no
9770 such distinction.
9772 The @code{Library_Version} attribute has a string value whose interpretation
9773 is platform dependent. On Unix, it is used only for dynamic/relocatable
9774 libraries as the internal name of the library (the @code{"soname"}). If the
9775 library file name (built from the @code{Library_Name}) is different from the
9776 @code{Library_Version}, then the library file will be a symbolic link to the
9777 actual file whose name will be @code{Library_Version}.
9779 Example (on Unix):
9781 @smallexample
9782 @group
9783 project Plib is
9785    Version := "1";
9787    for Library_Dir use "lib_dir";
9788    for Library_Name use "dummy";
9789    for Library_Kind use "relocatable";
9790    for Library_Version use "libdummy.so." & Version;
9792 end Plib;
9793 @end group
9794 @end smallexample
9796 @noindent
9797 Directory @file{lib_dir} will contain the internal library file whose name
9798 will be @file{libdummy.so.1}, and @file{libdummy.so} will be a symbolic link to
9799 @file{libdummy.so.1}.
9801 When @command{GNAT MAKE} detects that a project file (not the main project file)
9802 is a library project file, it will check all immediate sources of the project
9803 and rebuild the library if any of the sources have been recompiled.
9804 All @file{ALI} files will also be copied from the object directory to the
9805 library directory. To build executables, @command{GNAT MAKE} will use the
9806 library rather than the individual object files.
9809 @c *************************************
9810 @c * Qualifiers Related to Project Files *
9811 @c *************************************
9812 @node Qualifiers Related to Project Files
9813 @section Qualifiers Related to Project Files
9815 @noindent
9816 The following qualifiers are used by GNAT tools that support project files:
9818 @table @code
9820 @item @option{-P@var{project}}
9821 Indicates the name of a project file. This project file will be parsed with
9822 the verbosity indicated by @option{-vP@emph{x}}, if any, and using the external
9823 references indicated by @option{-X} qualifiers, if any.
9825 @noindent
9826 There must be only one @option{-P} qualifier on the command line.
9828 @noindent
9829 Since the Project Manager parses the project file only after all the qualifiers
9830 on the command line are checked, the order of the qualifiers @option{-P},
9831 @option{-Vp@emph{x}} or @option{-X} is not significant.
9833 @item @option{-X@var{name=value}}
9834 Indicates that external variable @var{name} has the value @var{value}.
9835 The Project Manager will use this value for occurrences of
9836 @code{external(name)} when parsing the project file.
9838 @noindent
9839 If @var{name} or @var{value} includes a space, then @var{name=value} should be
9840 put between quotes.
9841 @smallexample
9842   -XOS=NT
9843   -X"user=John Doe"
9844 @end smallexample
9846 @noindent
9847 Several @option{-X} qualifiers can be used simultaneously.
9848 If several @option{-X} qualifiers specify the same @var{name}, only the last one
9849 is used.
9851 @noindent
9852 An external variable specified with a @option{-X} qualifier takes precedence
9853 over the value of the same name in the environment.
9855 @item @option{-vP@emph{x}}
9856 Indicates the verbosity of the parsing of GNAT project files.
9857 @option{-vP0} means Default (no output for syntactically correct project
9858 files);
9859 @option{-vP1} means Medium;
9860 @option{-vP2} means High.
9861 @noindent
9862 The default is Default.
9863 @noindent
9864 If several @option{-vP@emph{x}} qualifiers are present, only the last one is
9865 used.
9867 @end table
9870 @c **********************************
9871 @c * Tools Supporting Project Files *
9872 @c **********************************
9874 @node  Tools Supporting Project Files
9875 @section Tools Supporting Project Files
9877 @menu
9878 * GNAT MAKE and Project Files::
9879 * The GNAT Driver and Project Files::
9880 @end menu
9882 @node GNAT MAKE and Project Files
9883 @subsection GNAT MAKE and Project Files
9885 @noindent
9886 This section covers two topics related to @command{GNAT MAKE} and project files:
9887 defining qualifiers for @command{GNAT MAKE} and for the tools that it invokes;
9888 and the use of the @code{Main} attribute.
9890 @menu
9891 * Qualifiers and Project Files::
9892 * Project Files and Main Subprograms::
9893 @end menu
9895 @node Qualifiers and Project Files
9896 @subsubsection Qualifiers and Project Files
9898 @noindent
9899 For each of the packages @code{Builder}, @code{Compiler}, @code{Binder}, and
9900 @code{Linker}, you can specify a @code{Default_Qualifiers} attribute, a
9901 @code{Qualifiers} attribute, or both; as their names imply, these qualifier-related
9902 attributes affect which qualifiers are used for which files when
9903 @command{GNAT MAKE} is invoked.  As will be explained below, these
9904 package-contributed qualifiers precede the qualifiers passed on the
9905 @command{GNAT MAKE} command line.
9907 The @code{Default_Qualifiers} attribute is an associative array indexed by
9908 language name (case insensitive) and returning a string list.  For example:
9910 @smallexample
9911 @group
9912 package Compiler is
9913   for Default_Qualifiers ("Ada") use ("/STYLE=", "-v");
9914 end Compiler;
9915 @end group
9916 @end smallexample
9918 @noindent
9919 The @code{Qualifiers} attribute is also an associative array, indexed by a file
9920 name (which may or may not be case sensitive, depending on the operating
9921 system) and returning a string list.  For example:
9923 @smallexample
9924 @group
9925 package Builder is
9926    for Qualifiers ("MAIN1.ADB") use ("/OPTIMIZE=ALL");
9927    for Qualifiers ("MAIN2.ADB") use ("-g");
9928 end Builder;
9929 @end group
9930 @end smallexample
9932 @noindent
9933 For the @code{Builder} package, the file names should designate source files
9934 for main subprograms.  For the @code{Binder} and @code{Linker} packages, the
9935 file names should designate @file{ALI} or source files for main subprograms.
9936 In each case just the file name (without explicit extension) is acceptable.
9938 For each tool used in a program build (@command{GNAT MAKE}, the compiler, the
9939 binder, and the linker), its corresponding package @dfn{contributes} a set of
9940 qualifiers for each file on which the tool is invoked, based on the
9941 qualifier-related attributes defined in the package. In particular, the qualifiers
9942 that each of these packages contributes for a given file @var{f} comprise:
9944 @itemize @bullet
9945 @item
9946 the value of attribute @code{Qualifiers (@var{f})}, if it is specified in the
9947 package for the given file,
9948 @item
9949 otherwise, the value of @code{Default_Qualifiers ("Ada")}, if it is specified in
9950 the package.
9951 @end itemize
9953 @noindent
9954 If neither of these attributes is defined in the package, then the package does
9955 not contribute any qualifiers for the given file.
9957 When @command{GNAT MAKE} is invoked on a file, the qualifiers comprise two sets,
9958 in the following order: those contributed for the file by the @code{Builder}
9959 package; and the qualifiers passed on the command line.
9961 When @command{GNAT MAKE} invokes a tool (compiler, binder, linker) on a file,
9962 the qualifiers passed to the tool comprise three sets, in the following order:
9964 @enumerate
9965 @item
9966 the applicable qualifiers contributed for the file by the @code{Builder} package
9967 in the project file supplied on the command line;
9969 @item
9970 those contributed for the file by the package (in the relevant project file --
9971 see below) corresponding to the tool; and
9973 @item
9974 the applicable qualifiers passed on the command line.
9975 @end enumerate
9977 @noindent
9978 The term @emph{applicable qualifiers} reflects the fact that @command{GNAT MAKE}
9979 qualifiers may or may not be passed to individual tools, depending on the
9980 individual qualifier.
9982 @command{GNAT MAKE} may invoke the compiler on source files from different
9983 projects. The Project Manager will use the appropriate project file to
9984 determine the @code{Compiler} package for each source file being compiled.
9985 Likewise for the @code{Binder} and @code{Linker} packages.
9987 As an example, consider the following package in a project file:
9989 @smallexample
9990 @group
9991 project Proj1 is
9992    package Compiler is
9993       for Default_Qualifiers ("Ada") use ("-g");
9994       for Qualifiers ("A.ADB") use ("/OPTIMIZE=SOME");
9995       for Qualifiers ("B.ADB") use ("/OPTIMIZE=ALL", "/STYLE=");
9996    end Compiler;
9997 end Proj1;
9998 @end group
9999 @end smallexample
10001 @noindent
10002 If @command{GNAT MAKE} is invoked with this project file, and it needs to
10003 compile, say, the files @file{A.ADB}, @file{B.ADB}, and @file{C.ADB}, then
10004 @file{A.ADB} will be compiled with the qualifier @option{/OPTIMIZE=SOME}, @file{B.ADB}
10005 with qualifiers @option{/OPTIMIZE=ALL} and @option{/STYLE=}, and @file{C.ADB} with
10006 @option{-g}.
10008 Another example illustrates the ordering of the qualifiers contributed by
10009 different packages:
10011 @smallexample
10012 @group
10013 project Proj2 is
10014    package Builder is
10015       for Qualifiers ("MAIN.ADB") use ("-g", "/OPTIMIZE=SOME", "-f");
10016    end Builder;
10017 @end group
10019 @group
10020    package Compiler is
10021       for Qualifiers ("MAIN.ADB") use ("/OPTIMIZE=ALL");
10022    end Compiler;
10023 end Proj2;
10024 @end group
10025 @end smallexample
10027 @noindent
10028 If you issue the command:
10030 @smallexample
10031     GNAT MAKE -PProj2 /OPTIMIZE=NONE main
10032 @end smallexample
10034 @noindent
10035 then the compiler will be invoked on @file{MAIN.ADB} with the following sequence of qualifiers
10037 @smallexample
10038    -g /OPTIMIZE=SOME /OPTIMIZE=ALL /OPTIMIZE=NONE
10039 @end smallexample
10041 with the last @option{-O} qualifier having precedence over the earlier ones;
10042 several other qualifiers (such as @option{-c}) are added implicitly.
10044 The qualifiers @option{-g} and @option{/OPTIMIZE=SOME} are contributed by package
10045 @code{Builder},  @option{/OPTIMIZE=ALL} is contributed by the package @code{Compiler}
10046 and @option{/OPTIMIZE=NONE} comes from the command line.
10048 The @option{-g} qualifier will also be passed in the invocation of
10049 @command{GNAT LINK.}
10051 A final example illustrates qualifier contributions from packages in different
10052 project files:
10054 @smallexample
10055 @group
10056 project Proj3 is
10057    for Source_Files use ("PACK.ADS", "PACK.ADB");
10058    package Compiler is
10059       for Default_Qualifiers ("Ada") use ("/CHECKS=ASSERTIONS");
10060    end Compiler;
10061 end Proj3;
10062 @end group
10064 @group
10065 with "Proj3";
10066 project Proj4 is
10067    for Source_Files use ("FOO_MAIN.ADB", "BAR_MAIN.ADB");
10068    package Builder is
10069       for Qualifiers ("FOO_MAIN.ADB") use ("-s", "-g");
10070    end Builder;
10071 end Proj4;
10072 @end group
10074 @group
10075 -- Ada source file:
10076 with Pack;
10077 procedure Foo_Main is
10078    ...
10079 end Foo_Main;
10080 @end group
10081 @end smallexample
10083 If the command is
10084 @smallexample
10085 GNAT MAKE -PProj4 FOO_MAIN.ADB /COMPILER_QUALIFIERS /CHECKS=OVERFLOW
10086 @end smallexample
10088 @noindent
10089 then the qualifiers passed to the compiler for @file{FOO_MAIN.ADB} are
10090 @option{-g} (contributed by the package @code{Proj4.Builder}) and
10091 @option{/CHECKS=OVERFLOW} (passed on the command line).
10092 When the imported package @code{Pack} is compiled, the qualifiers used are
10093 @option{-g} from @code{Proj4.Builder}, @option{/CHECKS=ASSERTIONS} (contributed from
10094 package @code{Proj3.Compiler}, and @option{/CHECKS=OVERFLOW} from the command line.
10097 @node Project Files and Main Subprograms
10098 @subsubsection Project Files and Main Subprograms
10100 @noindent
10101 When using a project file, you can invoke @command{GNAT MAKE}
10102 with several main subprograms, by specifying their source files on the command
10103 line.  Each of these needs to be an immediate source file of the project.
10105 @smallexample
10106     GNAT MAKE -Pprj main1 main2 main3
10107 @end smallexample
10109 @noindent
10110 When using a project file, you can also invoke @command{GNAT MAKE} without
10111 explicitly specifying any main, and the effect depends on whether you have
10112 defined the @code{Main} attribute.  This attribute has a string list value,
10113 where each element in the list is the name of a source file (the file
10114 extension is optional) containing a main subprogram.
10116 If the @code{Main} attribute is defined in a project file as a non-empty
10117 string list and the qualifier @option{-u} is not used on the command line, then
10118 invoking @command{GNAT MAKE} with this project file but without any main on the
10119 command line is equivalent to invoking @command{GNAT MAKE} with all the file
10120 names in the @code{Main} attribute on the command line.
10122 Example:
10123 @smallexample
10124 @group
10125    project Prj is
10126       for Main use ("main1", "main2", "main3");
10127    end Prj;
10128 @end group
10129 @end smallexample
10131 @noindent
10132 With this project file, @code{"GNAT MAKE -Pprj"} is equivalent to
10133 @code{"GNAT MAKE -Pprj main1 main2 main3"}.
10135 When the project attribute @code{Main} is not specified, or is specified
10136 as an empty string list, or when the qualifier @option{-u} is used on the command
10137 line, then invoking @command{GNAT MAKE} with no main on the command line will
10138 result in all immediate sources of the project file being checked, and
10139 potentially recompiled. Depending on the presence of the qualifier @option{-u},
10140 sources from other project files on which the immediate sources of the main
10141 project file depend are also checked and potentially recompiled. In other
10142 words, the @option{-u} qualifier is applied to all of the immediate sources of themain project file.
10145 @node The GNAT Driver and Project Files
10146 @subsection The GNAT Driver and Project Files
10148 @noindent
10149 A number of GNAT tools, other than @command{GNAT MAKE} are project-aware:
10150 @command{GNAT BIND}, @command{GNAT FIND}, @command{GNAT LINK}, @command{GNAT LIST}
10151 and @command{GNAT XREF}. However, none of these tools can be invoked directly
10152 with a project file qualifier (@code{-P}). They need to be invoke through the
10153 @command{gnat} driver.
10155 The @command{gnat} driver is a front-end that accepts a number of commands and
10156 call the corresponding tool. It has been designed initially for VMS to convert
10157 VMS style qualifiers to Unix style qualifiers, but it is now available to all
10158 the GNAT supported platforms.
10160 On non VMS platforms, the @command{gnat} driver accepts the following commands
10161 (case insensitive):
10163 @itemize @bullet
10164 @item
10165 BIND to invoke @command{GNAT BIND}
10166 @item
10167 CHOP to invoke @command{GNAT CHOP}
10168 @item
10169 COMP or COMPILE to invoke the compiler
10170 @item
10171 ELIM to invoke @command{GNAT ELIM}
10172 @item
10173 FIND to invoke @command{GNAT FIND}
10174 @item
10175 KR or KRUNCH to invoke @command{GNAT KRUNCH}
10176 @item
10177 LINK to invoke @command{GNAT LINK}
10178 @item
10179 LS or LIST to invoke @command{GNAT LIST}
10180 @item
10181 MAKE to invoke @command{GNAT MAKE}
10182 @item
10183 NAME to invoke @command{gnatname}
10184 @item
10185 PREP or PREPROCESS to invoke @command{GNAT PREPROCESS}
10186 @item
10187 PSTA or STANDARD to invoke @command{GNAT STANDARD}
10188 @item
10189 STUB to invoke @command{GNAT STUB}
10190 @item
10191 XREF to invoke @command{GNAT XREF}
10192 @end itemize
10194 @noindent
10195 Note that the compiler is invoked using the command @command{GNAT MAKE -f -u}.
10197 @noindent
10198 Following the command, you may put qualifiers and arguments for the invoked
10199 tool.
10201 @smallexample
10202   gnat bind -C MAIN.ALI
10203   gnat ls -a main
10204   gnat chop foo.txt
10205 @end smallexample
10207 @noindent
10208 In addition, for command BIND, FIND, LS or LIST, LINK and XREF, the project
10209 file related qualifiers (@code{-P}, @code{-X} and @code{-vPx}) may be used in
10210 addition to the qualifiers of the invoking tool.
10212 @noindent
10213 For each of these command, there is possibly a package in the main project that
10214 corresponds to the invoked tool.
10216 @itemize @bullet
10217 @item
10218 package @code{Binder} for command BIND (invoking @code{GNAT BIND})
10220 @item
10221 package @code{Finder} for command FIND (invoking @code{GNAT FIND})
10223 @item
10224 package @code{GNAT LIST} for command LS or LIST (invoking @code{GNAT LIST})
10226 @item
10227 package @code{Linker} for command LINK (invoking @code{GNAT LINK})
10229 @item
10230 package @code{Cross_Reference} for command XREF (invoking @code{GNAT LINK})
10232 @end itemize
10234 @noindent
10235 Package @code{GNAT LIST} has a unique attribute @code{Qualifiers}, a simple variable
10236 with a string list value. It contains qualifiers for the invocation of
10237 @code{GNAT LIST}.
10239 @smallexample
10240 @group
10241 project Proj1 is
10242    package GNAT LIST is
10243       for Qualifiers use ("-a", "-v");
10244    end GNAT LIST;
10245 end Proj1;
10246 @end group
10247 @end smallexample
10249 @noindent
10250 All other packages contains a qualifier @code{Default_Qualifiers}, an associative
10251 array, indexed by the programming language (case insensitive) and having a
10252 string list value. @code{Default_Qualifiers ("Ada")} contains the qualifiers for
10253 the invocation of the tool corresponding to the package.
10255 @smallexample
10256 @group
10257 project Proj is
10259    for Source_Dirs use ("./**");
10261    package GNAT LIST is
10262       for Qualifiers use ("-a", "-v");
10263    end GNAT LIST;
10264 @end group
10265 @group
10267    package Binder is
10268       for Default_Qualifiers ("Ada") use ("-C", "-e");
10269    end Binder;
10270 @end group
10271 @group
10273    package Linker is
10274       for Default_Qualifiers ("Ada") use ("-C");
10275    end Linker;
10276 @end group
10277 @group
10279    package Finder is
10280       for Default_Qualifiers ("Ada") use ("-a", "-f");
10281    end Finder;
10282 @end group
10283 @group
10285    package Cross_Reference is
10286       for Default_Qualifiers ("Ada") use ("-a", "-f", "-d", "-u");
10287    end Cross_Reference;
10288 end Proj;
10289 @end group
10290 @end smallexample
10292 @noindent
10293 With the above project file, commands such as
10295 @smallexample
10296    gnat ls -Pproj main
10297    gnat xref -Pproj main
10298    gnat bind -Pproj MAIN.ALI
10299 @end smallexample
10301 @noindent
10302 will set up the environment properly and invoke the tool with the qualifiers
10303 found in the package corresponding to the tool.
10308 @node An Extended Example
10309 @section An Extended Example
10311 @noindent
10312 Suppose that we have two programs, @var{prog1} and @var{prog2}, with the sources
10313 in the respective directories. We would like to build them with a single
10314 @command{GNAT MAKE} command, and we would like to place their object files into
10315 @file{.build} subdirectories of the source directories. Furthermore, we would
10316 like to have to have two separate subdirectories in @file{.build}  --
10317 @file{release} and @file{debug} -- which will contain the object files compiled with
10318 different set of compilation flags.
10320 In other words, we have the following structure:
10322 @smallexample
10323 @group
10324    main
10325      |- prog1
10326      |    |- .build
10327      |         | debug
10328      |         | release
10329      |- prog2
10330           |- .build
10331                | debug
10332                | release
10333 @end group
10334 @end smallexample
10336 @noindent
10337 Here are the project files that we need to create in a directory @file{main}
10338 to maintain this structure:
10340 @enumerate
10342 @item We create a @code{Common} project with a package @code{Compiler} that
10343 specifies the compilation qualifiers:
10345 @smallexample
10346 File "common.gpr":
10347 @group
10348 @b{project} Common @b{is}
10350    @b{for} Source_Dirs @b{use} (); -- No source files
10351 @end group
10353 @group
10354    @b{type} Build_Type @b{is} ("release", "debug");
10355    Build : Build_Type := External ("BUILD", "debug");
10356 @end group
10357 @group
10358    @b{package} Compiler @b{is}
10359       @b{case} Build @b{is}
10360          @b{when} "release" =>
10361            @b{for} Default_Qualifiers ("Ada") @b{use} ("/OPTIMIZE=ALL");
10362          @b{when} "debug"   =>
10363            @b{for} Default_Qualifiers ("Ada") @b{use} ("-g");
10364       @b{end case};
10365    @b{end} Compiler;
10367 @b{end} Common;
10368 @end group
10369 @end smallexample
10371 @item We create separate projects for the two programs:
10373 @smallexample
10374 @group
10375 File "prog1.gpr":
10377 @b{with} "common";
10378 @b{project} Prog1 @b{is}
10380     @b{for} Source_Dirs @b{use} ("prog1");
10381     @b{for} Object_Dir  @b{use} "prog1/.build/" & Common.Build;
10383     @b{package} Compiler @b{renames} Common.Compiler;
10385 @b{end} Prog1;
10386 @end group
10387 @end smallexample
10389 @smallexample
10390 @group
10391 File "prog2.gpr":
10393 @b{with} "common";
10394 @b{project} Prog2 @b{is}
10396     @b{for} Source_Dirs @b{use} ("prog2");
10397     @b{for} Object_Dir  @b{use} "prog2/.build/" & Common.Build;
10399     @b{package} Compiler @b{renames} Common.Compiler;
10401 @end group
10402 @b{end} Prog2;
10403 @end smallexample
10405 @item We create a wrapping project @var{Main}:
10407 @smallexample
10408 @group
10409 File "main.gpr":
10411 @b{with} "common";
10412 @b{with} "prog1";
10413 @b{with} "prog2";
10414 @b{project} Main @b{is}
10416    @b{package} Compiler @b{renames} Common.Compiler;
10418 @b{end} Main;
10419 @end group
10420 @end smallexample
10422 @item Finally we need to create a dummy procedure that @code{with}s (either
10423 explicitly or implicitly) all the sources of our two programs.
10425 @end enumerate
10427 @noindent
10428 Now we can build the programs using the command
10430 @smallexample
10431    GNAT MAKE -Pmain dummy
10432 @end smallexample
10434 @noindent
10435 for the Debug mode, or
10437 @smallexample
10438    GNAT MAKE -Pmain -XBUILD=release
10439 @end smallexample
10441 @noindent
10442 for the Release mode.
10445 @c ********************************
10446 @c * Project File Complete Syntax *
10447 @c ********************************
10449 @node Project File Complete Syntax
10450 @section Project File Complete Syntax
10452 @smallexample
10453 project ::=
10454   context_clause project_declaration
10456 context_clause ::=
10457   @{with_clause@}
10459 with_clause ::=
10460   @b{with} literal_string @{ , literal_string @} ;
10462 project_declaration ::=
10463   @b{project} <project_>simple_name [ @b{extends} literal_string ] @b{is}
10464     @{declarative_item@}
10465   @b{end} <project_>simple_name;
10467 declarative_item ::=
10468   package_declaration |
10469   typed_string_declaration |
10470   other_declarative_item
10472 package_declaration ::=
10473   @b{package} <package_>simple_name package_completion
10475 package_completion ::=
10476   package_body | package_renaming
10478 package body ::=
10479   @b{is}
10480     @{other_declarative_item@}
10481   @b{end} <package_>simple_name ;
10483 package_renaming ::==
10484   @b{renames} <project_>simple_name.<package_>simple_name ;
10486 typed_string_declaration ::=
10487   @b{type} <typed_string_>_simple_name @b{is}
10488    ( literal_string @{, literal_string@} );
10490 other_declarative_item ::=
10491   attribute_declaration |
10492   typed_variable_declaration |
10493   variable_declaration |
10494   case_construction
10496 attribute_declaration ::=
10497   @b{for} attribute @b{use} expression ;
10499 attribute ::=
10500   <simple_attribute_>simple_name |
10501   <associative_array_attribute_>simple_name ( literal_string )
10503 typed_variable_declaration ::=
10504   <typed_variable_>simple_name : <typed_string_>name :=  string_expression ;
10506 variable_declaration ::=
10507   <variable_>simple_name := expression;
10509 expression ::=
10510   term @{& term@}
10512 term ::=
10513   literal_string |
10514   string_list |
10515   <variable_>name |
10516   external_value |
10517   attribute_reference
10519 literal_string ::=
10520   (same as Ada)
10522 string_list ::=
10523   ( <string_>expression @{ , <string_>expression @} )
10525 external_value ::=
10526   @b{external} ( literal_string [, literal_string] )
10528 attribute_reference ::=
10529   attribute_parent ' <simple_attribute_>simple_name [ ( literal_string ) ]
10531 attribute_parent ::=
10532   @b{project} |
10533   <project_or_package>simple_name |
10534   <project_>simple_name . <package_>simple_name
10536 case_construction ::=
10537   @b{case} <typed_variable_>name @b{is}
10538     @{case_item@}
10539   @b{end case} ;
10541 case_item ::=
10542   @b{when} discrete_choice_list => @{case_construction | attribute_declaration@}
10544 discrete_choice_list ::=
10545   literal_string @{| literal_string@}
10547 name ::=
10548   simple_name @{. simple_name@}
10550 simple_name ::=
10551   identifier (same as Ada)
10553 @end smallexample
10556 @node Elaboration Order Handling in GNAT
10557 @chapter Elaboration Order Handling in GNAT
10558 @cindex Order of elaboration
10559 @cindex Elaboration control
10561 @menu
10562 * Elaboration Code in Ada 95::
10563 * Checking the Elaboration Order in Ada 95::
10564 * Controlling the Elaboration Order in Ada 95::
10565 * Controlling Elaboration in GNAT - Internal Calls::
10566 * Controlling Elaboration in GNAT - External Calls::
10567 * Default Behavior in GNAT - Ensuring Safety::
10568 * Elaboration Issues for Library Tasks::
10569 * Mixing Elaboration Models::
10570 * What to Do If the Default Elaboration Behavior Fails::
10571 * Elaboration for Access-to-Subprogram Values::
10572 * Summary of Procedures for Elaboration Control::
10573 * Other Elaboration Order Considerations::
10574 @end menu
10576 @noindent
10577 This chapter describes the handling of elaboration code in Ada 95 and
10578 in GNAT, and discusses how the order of elaboration of program units can
10579 be controlled in GNAT, either automatically or with explicit programming
10580 features.
10582 @node Elaboration Code in Ada 95
10583 @section Elaboration Code in Ada 95
10585 @noindent
10586 Ada 95 provides rather general mechanisms for executing code at elaboration
10587 time, that is to say before the main program starts executing. Such code arises
10588 in three contexts:
10590 @table @asis
10591 @item Initializers for variables.
10592 Variables declared at the library level, in package specs or bodies, can
10593 require initialization that is performed at elaboration time, as in:
10594 @smallexample
10595 @cartouche
10596 Sqrt_Half : Float := Sqrt (0.5);
10597 @end cartouche
10598 @end smallexample
10600 @item Package initialization code
10601 Code in a @code{BEGIN-END} section at the outer level of a package body is
10602 executed as part of the package body elaboration code.
10604 @item Library level task allocators
10605 Tasks that are declared using task allocators at the library level
10606 start executing immediately and hence can execute at elaboration time.
10607 @end table
10609 @noindent
10610 Subprogram calls are possible in any of these contexts, which means that
10611 any arbitrary part of the program may be executed as part of the elaboration
10612 code. It is even possible to write a program which does all its work at
10613 elaboration time, with a null main program, although stylistically this
10614 would usually be considered an inappropriate way to structure
10615 a program.
10617 An important concern arises in the context of elaboration code:
10618 we have to be sure that it is executed in an appropriate order. What we
10619 have is a series of elaboration code sections, potentially one section
10620 for each unit in the program. It is important that these execute
10621 in the correct order. Correctness here means that, taking the above
10622 example of the declaration of @code{Sqrt_Half},
10623 if some other piece of
10624 elaboration code references @code{Sqrt_Half},
10625 then it must run after the
10626 section of elaboration code that contains the declaration of
10627 @code{Sqrt_Half}.
10629 There would never be any order of elaboration problem if we made a rule
10630 that whenever you @code{with} a unit, you must elaborate both the spec and body
10631 of that unit before elaborating the unit doing the @code{with}'ing:
10633 @smallexample
10634 @group
10635 @cartouche
10636 @b{with} Unit_1;
10637 @b{package} Unit_2 @b{is} ...
10638 @end cartouche
10639 @end group
10640 @end smallexample
10642 @noindent
10643 would require that both the body and spec of @code{Unit_1} be elaborated
10644 before the spec of @code{Unit_2}. However, a rule like that would be far too
10645 restrictive. In particular, it would make it impossible to have routines
10646 in separate packages that were mutually recursive.
10648 You might think that a clever enough compiler could look at the actual
10649 elaboration code and determine an appropriate correct order of elaboration,
10650 but in the general case, this is not possible. Consider the following
10651 example.
10653 In the body of @code{Unit_1}, we have a procedure @code{Func_1}
10654 that references
10655 the variable @code{Sqrt_1}, which is declared in the elaboration code
10656 of the body of @code{Unit_1}:
10658 @smallexample
10659 @cartouche
10660 Sqrt_1 : Float := Sqrt (0.1);
10661 @end cartouche
10662 @end smallexample
10664 @noindent
10665 The elaboration code of the body of @code{Unit_1} also contains:
10667 @smallexample
10668 @group
10669 @cartouche
10670 @b{if} expression_1 = 1 @b{then}
10671    Q := Unit_2.Func_2;
10672 @b{end if};
10673 @end cartouche
10674 @end group
10675 @end smallexample
10677 @noindent
10678 @code{Unit_2} is exactly parallel,
10679 it has a procedure @code{Func_2} that references
10680 the variable @code{Sqrt_2}, which is declared in the elaboration code of
10681 the body @code{Unit_2}:
10683 @smallexample
10684 @cartouche
10685 Sqrt_2 : Float := Sqrt (0.1);
10686 @end cartouche
10687 @end smallexample
10689 @noindent
10690 The elaboration code of the body of @code{Unit_2} also contains:
10692 @smallexample
10693 @group
10694 @cartouche
10695 @b{if} expression_2 = 2 @b{then}
10696    Q := Unit_1.Func_1;
10697 @b{end if};
10698 @end cartouche
10699 @end group
10700 @end smallexample
10702 @noindent
10703 Now the question is, which of the following orders of elaboration is
10704 acceptable:
10706 @smallexample
10707 @group
10708 Spec of Unit_1
10709 Spec of Unit_2
10710 Body of Unit_1
10711 Body of Unit_2
10712 @end group
10713 @end smallexample
10715 @noindent
10718 @smallexample
10719 @group
10720 Spec of Unit_2
10721 Spec of Unit_1
10722 Body of Unit_2
10723 Body of Unit_1
10724 @end group
10725 @end smallexample
10727 @noindent
10728 If you carefully analyze the flow here, you will see that you cannot tell
10729 at compile time the answer to this question.
10730 If @code{expression_1} is not equal to 1,
10731 and @code{expression_2} is not equal to 2,
10732 then either order is acceptable, because neither of the function calls is
10733 executed. If both tests evaluate to true, then neither order is acceptable
10734 and in fact there is no correct order.
10736 If one of the two expressions is true, and the other is false, then one
10737 of the above orders is correct, and the other is incorrect. For example,
10738 if @code{expression_1} = 1 and @code{expression_2} /= 2,
10739 then the call to @code{Func_2}
10740 will occur, but not the call to @code{Func_1.}
10741 This means that it is essential
10742 to elaborate the body of @code{Unit_1} before
10743 the body of @code{Unit_2}, so the first
10744 order of elaboration is correct and the second is wrong.
10746 By making @code{expression_1} and @code{expression_2}
10747 depend on input data, or perhaps
10748 the time of day, we can make it impossible for the compiler or binder
10749 to figure out which of these expressions will be true, and hence it
10750 is impossible to guarantee a safe order of elaboration at run time.
10752 @node Checking the Elaboration Order in Ada 95
10753 @section Checking the Elaboration Order in Ada 95
10755 @noindent
10756 In some languages that involve the same kind of elaboration problems,
10757 e.g. Java and C++, the programmer is expected to worry about these
10758 ordering problems himself, and it is common to
10759 write a program in which an incorrect elaboration order  gives
10760 surprising results, because it references variables before they
10761 are initialized.
10762 Ada 95 is designed to be a safe language, and a programmer-beware approach is
10763 clearly not sufficient. Consequently, the language provides three lines
10764 of defense:
10766 @table @asis
10767 @item Standard rules
10768 Some standard rules restrict the possible choice of elaboration
10769 order. In particular, if you @code{with} a unit, then its spec is always
10770 elaborated before the unit doing the @code{with}. Similarly, a parent
10771 spec is always elaborated before the child spec, and finally
10772 a spec is always elaborated before its corresponding body.
10774 @item Dynamic elaboration checks
10775 @cindex Elaboration checks
10776 @cindex Checks, elaboration
10777 Dynamic checks are made at run time, so that if some entity is accessed
10778 before it is elaborated (typically  by means of a subprogram call)
10779 then the exception (@code{Program_Error}) is raised.
10781 @item Elaboration control
10782 Facilities are provided for the programmer to specify the desired order
10783 of elaboration.
10784 @end table
10786 Let's look at these facilities in more detail. First, the rules for
10787 dynamic checking. One possible rule would be simply to say that the
10788 exception is raised if you access a variable which has not yet been
10789 elaborated. The trouble with this approach is that it could require
10790 expensive checks on every variable reference. Instead Ada 95 has two
10791 rules which are a little more restrictive, but easier to check, and
10792 easier to state:
10794 @table @asis
10795 @item Restrictions on calls
10796 A subprogram can only be called at elaboration time if its body
10797 has been elaborated. The rules for elaboration given above guarantee
10798 that the spec of the subprogram has been elaborated before the
10799 call, but not the body. If this rule is violated, then the
10800 exception @code{Program_Error} is raised.
10802 @item Restrictions on instantiations
10803 A generic unit can only be instantiated if the body of the generic
10804 unit has been elaborated. Again, the rules for elaboration given above
10805 guarantee that the spec of the generic unit has been elaborated
10806 before the instantiation, but not the body. If this rule is
10807 violated, then the exception @code{Program_Error} is raised.
10808 @end table
10810 @noindent
10811 The idea is that if the body has been elaborated, then any variables
10812 it references must have been elaborated; by checking for the body being
10813 elaborated we guarantee that none of its references causes any
10814 trouble. As we noted above, this is a little too restrictive, because a
10815 subprogram that has no non-local references in its body may in fact be safe
10816 to call. However, it really would be unsafe to rely on this, because
10817 it would mean that the caller was aware of details of the implementation
10818 in the body. This goes against the basic tenets of Ada.
10820 A plausible implementation can be described as follows.
10821 A Boolean variable is associated with each subprogram
10822 and each generic unit. This variable is initialized to False, and is set to
10823 True at the point body is elaborated. Every call or instantiation checks the
10824 variable, and raises @code{Program_Error} if the variable is False.
10826 Note that one might think that it would be good enough to have one Boolean
10827 variable for each package, but that would not deal with cases of trying
10828 to call a body in the same package as the call
10829 that has not been elaborated yet.
10830 Of course a compiler may be able to do enough analysis to optimize away
10831 some of the Boolean variables as unnecessary, and @code{GNAT} indeed
10832 does such optimizations, but still the easiest conceptual model is to
10833 think of there being one variable per subprogram.
10835 @node Controlling the Elaboration Order in Ada 95
10836 @section Controlling the Elaboration Order in Ada 95
10838 @noindent
10839 In the previous section we discussed the rules in Ada 95 which ensure
10840 that @code{Program_Error} is raised if an incorrect elaboration order is
10841 chosen. This prevents erroneous executions, but we need mechanisms to
10842 specify a correct execution and avoid the exception altogether.
10843 To achieve this, Ada 95 provides a number of features for controlling
10844 the order of elaboration. We discuss these features in this section.
10846 First, there are several ways of indicating to the compiler that a given
10847 unit has no elaboration problems:
10849 @table @asis
10850 @item packages that do not require a body
10851 In Ada 95, a library package that does not require a body does not permit
10852 a body. This means that if we have a such a package, as in:
10854 @smallexample
10855 @group
10856 @cartouche
10857 @b{package} Definitions @b{is}
10858    @b{generic}
10859       @b{type} m @b{is new} integer;
10860    @b{package} Subp @b{is}
10861       @b{type} a @b{is array} (1 .. 10) @b{of} m;
10862       @b{type} b @b{is array} (1 .. 20) @b{of} m;
10863    @b{end} Subp;
10864 @b{end} Definitions;
10865 @end cartouche
10866 @end group
10867 @end smallexample
10869 @noindent
10870 A package that @code{with}'s @code{Definitions} may safely instantiate
10871 @code{Definitions.Subp} because the compiler can determine that there
10872 definitely is no package body to worry about in this case
10874 @item pragma Pure
10875 @cindex pragma Pure
10876 @findex Pure
10877 Places sufficient restrictions on a unit to guarantee that
10878 no call to any subprogram in the unit can result in an
10879 elaboration problem. This means that the compiler does not need
10880 to worry about the point of elaboration of such units, and in
10881 particular, does not need to check any calls to any subprograms
10882 in this unit.
10884 @item pragma Preelaborate
10885 @findex Preelaborate
10886 @cindex pragma Preelaborate
10887 This pragma places slightly less stringent restrictions on a unit than
10888 does pragma Pure,
10889 but these restrictions are still sufficient to ensure that there
10890 are no elaboration problems with any calls to the unit.
10892 @item pragma Elaborate_Body
10893 @findex Elaborate_Body
10894 @cindex pragma Elaborate_Body
10895 This pragma requires that the body of a unit be elaborated immediately
10896 after its spec. Suppose a unit @code{A} has such a pragma,
10897 and unit @code{B} does
10898 a @code{with} of unit @code{A}. Recall that the standard rules require
10899 the spec of unit @code{A}
10900 to be elaborated before the @code{with}'ing unit; given the pragma in
10901 @code{A}, we also know that the body of @code{A}
10902 will be elaborated before @code{B}, so
10903 that calls to @code{A} are safe and do not need a check.
10904 @end table
10906 @noindent
10907 Note that,
10908 unlike pragma @code{Pure} and pragma @code{Preelaborate},
10909 the use of
10910 @code{Elaborate_Body} does not guarantee that the program is
10911 free of elaboration problems, because it may not be possible
10912 to satisfy the requested elaboration order.
10913 Let's go back to the example with @code{Unit_1} and @code{Unit_2}.
10914 If a programmer
10915 marks @code{Unit_1} as @code{Elaborate_Body},
10916 and not @code{Unit_2,} then the order of
10917 elaboration will be:
10919 @smallexample
10920 @group
10921 Spec of Unit_2
10922 Spec of Unit_1
10923 Body of Unit_1
10924 Body of Unit_2
10925 @end group
10926 @end smallexample
10928 @noindent
10929 Now that means that the call to @code{Func_1} in @code{Unit_2}
10930 need not be checked,
10931 it must be safe. But the call to @code{Func_2} in
10932 @code{Unit_1} may still fail if
10933 @code{Expression_1} is equal to 1,
10934 and the programmer must still take
10935 responsibility for this not being the case.
10937 If all units carry a pragma @code{Elaborate_Body}, then all problems are
10938 eliminated, except for calls entirely within a body, which are
10939 in any case fully under programmer control. However, using the pragma
10940 everywhere is not always possible.
10941 In particular, for our @code{Unit_1}/@code{Unit_2} example, if
10942 we marked both of them as having pragma @code{Elaborate_Body}, then
10943 clearly there would be no possible elaboration order.
10945 The above pragmas allow a server to guarantee safe use by clients, and
10946 clearly this is the preferable approach. Consequently a good rule in
10947 Ada 95 is to mark units as @code{Pure} or @code{Preelaborate} if possible,
10948 and if this is not possible,
10949 mark them as @code{Elaborate_Body} if possible.
10950 As we have seen, there are situations where neither of these
10951 three pragmas can be used.
10952 So we also provide methods for clients to control the
10953 order of elaboration of the servers on which they depend:
10955 @table @asis
10956 @item pragma Elaborate (unit)
10957 @findex Elaborate
10958 @cindex pragma Elaborate
10959 This pragma is placed in the context clause, after a @code{with} clause,
10960 and it requires that the body of the named unit be elaborated before
10961 the unit in which the pragma occurs. The idea is to use this pragma
10962 if the current unit calls at elaboration time, directly or indirectly,
10963 some subprogram in the named unit.
10965 @item pragma Elaborate_All (unit)
10966 @findex Elaborate_All
10967 @cindex pragma Elaborate_All
10968 This is a stronger version of the Elaborate pragma. Consider the
10969 following example:
10971 @smallexample
10972 Unit A @code{with}'s unit B and calls B.Func in elab code
10973 Unit B @code{with}'s unit C, and B.Func calls C.Func
10974 @end smallexample
10976 @noindent
10977 Now if we put a pragma @code{Elaborate (B)}
10978 in unit @code{A}, this ensures that the
10979 body of @code{B} is elaborated before the call, but not the
10980 body of @code{C}, so
10981 the call to @code{C.Func} could still cause @code{Program_Error} to
10982 be raised.
10984 The effect of a pragma @code{Elaborate_All} is stronger, it requires
10985 not only that the body of the named unit be elaborated before the
10986 unit doing the @code{with}, but also the bodies of all units that the
10987 named unit uses, following @code{with} links transitively. For example,
10988 if we put a pragma @code{Elaborate_All (B)} in unit @code{A},
10989 then it requires
10990 not only that the body of @code{B} be elaborated before @code{A},
10991 but also the
10992 body of @code{C}, because @code{B} @code{with}'s @code{C}.
10993 @end table
10995 @noindent
10996 We are now in a position to give a usage rule in Ada 95 for avoiding
10997 elaboration problems, at least if dynamic dispatching and access to
10998 subprogram values are not used. We will handle these cases separately
10999 later.
11001 The rule is simple. If a unit has elaboration code that can directly or
11002 indirectly make a call to a subprogram in a @code{with}'ed unit, or instantiate
11003 a generic unit in a @code{with}'ed unit,
11004 then if the @code{with}'ed unit does not have
11005 pragma @code{Pure} or @code{Preelaborate}, then the client should have
11006 a pragma @code{Elaborate_All}
11007 for the @code{with}'ed unit. By following this rule a client is
11008 assured that calls can be made without risk of an exception.
11009 If this rule is not followed, then a program may be in one of four
11010 states:
11012 @table @asis
11013 @item No order exists
11014 No order of elaboration exists which follows the rules, taking into
11015 account any @code{Elaborate}, @code{Elaborate_All},
11016 or @code{Elaborate_Body} pragmas. In
11017 this case, an Ada 95 compiler must diagnose the situation at bind
11018 time, and refuse to build an executable program.
11020 @item One or more orders exist, all incorrect
11021 One or more acceptable elaboration orders exists, and all of them
11022 generate an elaboration order problem. In this case, the binder
11023 can build an executable program, but @code{Program_Error} will be raised
11024 when the program is run.
11026 @item Several orders exist, some right, some incorrect
11027 One or more acceptable elaboration orders exists, and some of them
11028 work, and some do not. The programmer has not controlled
11029 the order of elaboration, so the binder may or may not pick one of
11030 the correct orders, and the program may or may not raise an
11031 exception when it is run. This is the worst case, because it means
11032 that the program may fail when moved to another compiler, or even
11033 another version of the same compiler.
11035 @item One or more orders exists, all correct
11036 One ore more acceptable elaboration orders exist, and all of them
11037 work. In this case the program runs successfully. This state of
11038 affairs can be guaranteed by following the rule we gave above, but
11039 may be true even if the rule is not followed.
11040 @end table
11042 @noindent
11043 Note that one additional advantage of following our Elaborate_All rule
11044 is that the program continues to stay in the ideal (all orders OK) state
11045 even if maintenance
11046 changes some bodies of some subprograms. Conversely, if a program that does
11047 not follow this rule happens to be safe at some point, this state of affairs
11048 may deteriorate silently as a result of maintenance changes.
11050 You may have noticed that the above discussion did not mention
11051 the use of @code{Elaborate_Body}. This was a deliberate omission. If you
11052 @code{with} an @code{Elaborate_Body} unit, it still may be the case that
11053 code in the body makes calls to some other unit, so it is still necessary
11054 to use @code{Elaborate_All} on such units.
11056 @node Controlling Elaboration in GNAT - Internal Calls
11057 @section Controlling Elaboration in GNAT - Internal Calls
11059 @noindent
11060 In the case of internal calls, i.e. calls within a single package, the
11061 programmer has full control over the order of elaboration, and it is up
11062 to the programmer to elaborate declarations in an appropriate order. For
11063 example writing:
11065 @smallexample
11066 @group
11067 @cartouche
11068 @b{function} One @b{return} Float;
11070 Q : Float := One;
11072 @b{function} One @b{return} Float @b{is}
11073 @b{begin}
11074      return 1.0;
11075 @b{end} One;
11076 @end cartouche
11077 @end group
11078 @end smallexample
11080 @noindent
11081 will obviously raise @code{Program_Error} at run time, because function
11082 One will be called before its body is elaborated. In this case GNAT will
11083 generate a warning that the call will raise @code{Program_Error}:
11085 @smallexample
11086 @group
11087 @cartouche
11088  1. procedure y is
11089  2.    function One return Float;
11090  3.
11091  4.    Q : Float := One;
11092                     |
11093     >>> warning: cannot call "One" before body is elaborated
11094     >>> warning: Program_Error will be raised at run time
11096  5.
11097  6.    function One return Float is
11098  7.    begin
11099  8.         return 1.0;
11100  9.    end One;
11102 11. begin
11103 12.    null;
11104 13. end;
11105 @end cartouche
11106 @end group
11107 @end smallexample
11109 @noindent
11110 Note that in this particular case, it is likely that the call is safe, because
11111 the function @code{One} does not access any global variables.
11112 Nevertheless in Ada 95, we do not want the validity of the check to depend on
11113 the contents of the body (think about the separate compilation case), so this
11114 is still wrong, as we discussed in the previous sections.
11116 The error is easily corrected by rearranging the declarations so that the
11117 body of One appears before the declaration containing the call
11118 (note that in Ada 95,
11119 declarations can appear in any order, so there is no restriction that
11120 would prevent this reordering, and if we write:
11122 @smallexample
11123 @group
11124 @cartouche
11125 @b{function} One @b{return} Float;
11127 @b{function} One @b{return} Float @b{is}
11128 @b{begin}
11129      return 1.0;
11130 @b{end} One;
11132 Q : Float := One;
11133 @end cartouche
11134 @end group
11135 @end smallexample
11137 @noindent
11138 then all is well, no warning is generated, and no
11139 @code{Program_Error} exception
11140 will be raised.
11141 Things are more complicated when a chain of subprograms is executed:
11143 @smallexample
11144 @group
11145 @cartouche
11146 @b{function} A @b{return} Integer;
11147 @b{function} B @b{return} Integer;
11148 @b{function} C @b{return} Integer;
11150 @b{function} B @b{return} Integer @b{is begin return} A; @b{end};
11151 @b{function} C @b{return} Integer @b{is begin return} B; @b{end};
11153 X : Integer := C;
11155 @b{function} A @b{return} Integer @b{is begin return} 1; @b{end};
11156 @end cartouche
11157 @end group
11158 @end smallexample
11160 @noindent
11161 Now the call to @code{C}
11162 at elaboration time in the declaration of @code{X} is correct, because
11163 the body of @code{C} is already elaborated,
11164 and the call to @code{B} within the body of
11165 @code{C} is correct, but the call
11166 to @code{A} within the body of @code{B} is incorrect, because the body
11167 of @code{A} has not been elaborated, so @code{Program_Error}
11168 will be raised on the call to @code{A}.
11169 In this case GNAT will generate a
11170 warning that @code{Program_Error} may be
11171 raised at the point of the call. Let's look at the warning:
11173 @smallexample
11174 @group
11175 @cartouche
11176  1. procedure x is
11177  2.    function A return Integer;
11178  3.    function B return Integer;
11179  4.    function C return Integer;
11180  5.
11181  6.    function B return Integer is begin return A; end;
11182                                                     |
11183     >>> warning: call to "A" before body is elaborated may
11184                  raise Program_Error
11185     >>> warning: "B" called at line 7
11186     >>> warning: "C" called at line 9
11188  7.    function C return Integer is begin return B; end;
11189  8.
11190  9.    X : Integer := C;
11192 11.    function A return Integer is begin return 1; end;
11194 13. begin
11195 14.    null;
11196 15. end;
11197 @end cartouche
11198 @end group
11199 @end smallexample
11201 @noindent
11202 Note that the message here says "may raise", instead of the direct case,
11203 where the message says "will be raised". That's because whether
11204 @code{A} is
11205 actually called depends in general on run-time flow of control.
11206 For example, if the body of @code{B} said
11208 @smallexample
11209 @group
11210 @cartouche
11211 @b{function} B @b{return} Integer @b{is}
11212 @b{begin}
11213    @b{if} some-condition-depending-on-input-data @b{then}
11214       @b{return} A;
11215    @b{else}
11216       @b{return} 1;
11217    @b{end if};
11218 @b{end} B;
11219 @end cartouche
11220 @end group
11221 @end smallexample
11223 @noindent
11224 then we could not know until run time whether the incorrect call to A would
11225 actually occur, so @code{Program_Error} might
11226 or might not be raised. It is possible for a compiler to
11227 do a better job of analyzing bodies, to
11228 determine whether or not @code{Program_Error}
11229 might be raised, but it certainly
11230 couldn't do a perfect job (that would require solving the halting problem
11231 and is provably impossible), and because this is a warning anyway, it does
11232 not seem worth the effort to do the analysis. Cases in which it
11233 would be relevant are rare.
11235 In practice, warnings of either of the forms given
11236 above will usually correspond to
11237 real errors, and should be examined carefully and eliminated.
11238 In the rare case where a warning is bogus, it can be suppressed by any of
11239 the following methods:
11241 @itemize @bullet
11242 @item
11243 Compile with the @option{/WARNINGS=SUPPRESS} qualifier set
11245 @item
11246 Suppress @code{Elaboration_Checks} for the called subprogram
11248 @item
11249 Use pragma @code{Warnings_Off} to turn warnings off for the call
11250 @end itemize
11252 @noindent
11253 For the internal elaboration check case,
11254 GNAT by default generates the
11255 necessary run-time checks to ensure
11256 that @code{Program_Error} is raised if any
11257 call fails an elaboration check. Of course this can only happen if a
11258 warning has been issued as described above. The use of pragma
11259 @code{Suppress (Elaboration_Checks)} may (but is not guaranteed to) suppress
11260 some of these checks, meaning that it may be possible (but is not
11261 guaranteed) for a program to be able to call a subprogram whose body
11262 is not yet elaborated, without raising a @code{Program_Error} exception.
11264 @node Controlling Elaboration in GNAT - External Calls
11265 @section Controlling Elaboration in GNAT - External Calls
11267 @noindent
11268 The previous section discussed the case in which the execution of a
11269 particular thread of elaboration code occurred entirely within a
11270 single unit. This is the easy case to handle, because a programmer
11271 has direct and total control over the order of elaboration, and
11272 furthermore, checks need only be generated in cases which are rare
11273 and which the compiler can easily detect.
11274 The situation is more complex when separate compilation is taken into account.
11275 Consider the following:
11277 @smallexample
11278 @cartouche
11279 @group
11280 @b{package} Math @b{is}
11281    @b{function} Sqrt (Arg : Float) @b{return} Float;
11282 @b{end} Math;
11284 @b{package body} Math @b{is}
11285    @b{function} Sqrt (Arg : Float) @b{return} Float @b{is}
11286    @b{begin}
11287          ...
11288    @b{end} Sqrt;
11289 @b{end} Math;
11290 @end group
11291 @group
11292 @b{with} Math;
11293 @b{package} Stuff @b{is}
11294    X : Float := Math.Sqrt (0.5);
11295 @b{end} Stuff;
11297 @b{with} Stuff;
11298 @b{procedure} Main @b{is}
11299 @b{begin}
11300    ...
11301 @b{end} Main;
11302 @end group
11303 @end cartouche
11304 @end smallexample
11306 @noindent
11307 where @code{Main} is the main program. When this program is executed, the
11308 elaboration code must first be executed, and one of the jobs of the
11309 binder is to determine the order in which the units of a program are
11310 to be elaborated. In this case we have four units: the spec and body
11311 of @code{Math},
11312 the spec of @code{Stuff} and the body of @code{Main}).
11313 In what order should the four separate sections of elaboration code
11314 be executed?
11316 There are some restrictions in the order of elaboration that the binder
11317 can choose. In particular, if unit U has a @code{with}
11318 for a package @code{X}, then you
11319 are assured that the spec of @code{X}
11320 is elaborated before U , but you are
11321 not assured that the body of @code{X}
11322 is elaborated before U.
11323 This means that in the above case, the binder is allowed to choose the
11324 order:
11326 @smallexample
11327 spec of Math
11328 spec of Stuff
11329 body of Math
11330 body of Main
11331 @end smallexample
11333 @noindent
11334 but that's not good, because now the call to @code{Math.Sqrt}
11335 that happens during
11336 the elaboration of the @code{Stuff}
11337 spec happens before the body of @code{Math.Sqrt} is
11338 elaborated, and hence causes @code{Program_Error} exception to be raised.
11339 At first glance, one might say that the binder is misbehaving, because
11340 obviously you want to elaborate the body of something you @code{with}
11341 first, but
11342 that is not a general rule that can be followed in all cases. Consider
11344 @smallexample
11345 @group
11346 @cartouche
11347 @b{package} X @b{is} ...
11349 @b{package} Y @b{is} ...
11351 @b{with} X;
11352 @b{package body} Y @b{is} ...
11354 @b{with} Y;
11355 @b{package body} X @b{is} ...
11356 @end cartouche
11357 @end group
11358 @end smallexample
11360 @noindent
11361 This is a common arrangement, and, apart from the order of elaboration
11362 problems that might arise in connection with elaboration code, this works fine.
11363 A rule that says that you must first elaborate the body of anything you
11364 @code{with} cannot work in this case:
11365 the body of @code{X} @code{with}'s @code{Y},
11366 which means you would have to
11367 elaborate the body of @code{Y} first, but that @code{with}'s @code{X},
11368 which means
11369 you have to elaborate the body of @code{X} first, but ... and we have a
11370 loop that cannot be broken.
11372 It is true that the binder can in many cases guess an order of elaboration
11373 that is unlikely to cause a @code{Program_Error}
11374 exception to be raised, and it tries to do so (in the
11375 above example of @code{Math/Stuff/Spec}, the GNAT binder will
11376 by default
11377 elaborate the body of @code{Math} right after its spec, so all will be well).
11379 However, a program that blindly relies on the binder to be helpful can
11380 get into trouble, as we discussed in the previous sections, so
11381 GNAT
11382 provides a number of facilities for assisting the programmer in
11383 developing programs that are robust with respect to elaboration order.
11385 @node Default Behavior in GNAT - Ensuring Safety
11386 @section Default Behavior in GNAT - Ensuring Safety
11388 @noindent
11389 The default behavior in GNAT ensures elaboration safety. In its
11390 default mode GNAT implements the
11391 rule we previously described as the right approach. Let's restate it:
11393 @itemize
11394 @item
11395 @emph{If a unit has elaboration code that can directly or indirectly make a
11396 call to a subprogram in a @code{with}'ed unit, or instantiate a generic unit
11397 in a @code{with}'ed unit, then if the @code{with}'ed unit
11398 does not have pragma @code{Pure} or
11399 @code{Preelaborate}, then the client should have an
11400 @code{Elaborate_All} for the @code{with}'ed unit.}
11401 @end itemize
11403 @noindent
11404 By following this rule a client
11405 is assured that calls and instantiations can be made without risk of an exception.
11407 In this mode GNAT traces all calls that are potentially made from
11408 elaboration code, and puts in any missing implicit @code{Elaborate_All}
11409 pragmas.
11410 The advantage of this approach is that no elaboration problems
11411 are possible if the binder can find an elaboration order that is
11412 consistent with these implicit @code{Elaborate_All} pragmas. The
11413 disadvantage of this approach is that no such order may exist.
11415 If the binder does not generate any diagnostics, then it means that it
11416 has found an elaboration order that is guaranteed to be safe. However,
11417 the binder may still be relying on implicitly generated
11418 @code{Elaborate_All} pragmas so portability to other compilers than
11419 GNAT is not guaranteed.
11421 If it is important to guarantee portability, then the compilations should
11422 use the
11423 @option{/WARNINGS=ELABORATION}
11424 (warn on elaboration problems) qualifier. This will cause warning messages
11425 to be generated indicating the missing @code{Elaborate_All} pragmas.
11426 Consider the following source program:
11428 @smallexample
11429 @group
11430 @cartouche
11431 @b{with} k;
11432 @b{package} j @b{is}
11433   m : integer := k.r;
11434 @b{end};
11435 @end cartouche
11436 @end group
11437 @end smallexample
11439 @noindent
11440 where it is clear that there
11441 should be a pragma @code{Elaborate_All}
11442 for unit @code{k}. An implicit pragma will be generated, and it is
11443 likely that the binder will be able to honor it. However,
11444 it is safer to include the pragma explicitly in the source. If this
11445 unit is compiled with the
11446 @option{/WARNINGS=ELABORATION}
11447 qualifier, then the compiler outputs a warning:
11449 @smallexample
11450 @group
11451 @cartouche
11452 1. with k;
11453 2. package j is
11454 3.   m : integer := k.r;
11455                      |
11456    >>> warning: call to "r" may raise Program_Error
11457    >>> warning: missing pragma Elaborate_All for "k"
11459 4. end;
11460 @end cartouche
11461 @end group
11462 @end smallexample
11464 @noindent
11465 and these warnings can be used as a guide for supplying manually
11466 the missing pragmas.
11468 This default mode is more restrictive than the Ada Reference
11469 Manual, and it is possible to construct programs which will compile
11470 using the dynamic model described there, but will run into a
11471 circularity using the safer static model we have described.
11473 Of course any Ada compiler must be able to operate in a mode
11474 consistent with the requirements of the Ada Reference Manual,
11475 and in particular must have the capability of implementing the
11476 standard dynamic model of elaboration with run-time checks.
11478 In GNAT, this standard mode can be achieved either by the use of
11479 the @option{/CHECKS=ELABORATION} qualifier on the compiler (@code{GNAT COMPILE} or @code{GNAT MAKE})
11480 command, or by the use of the configuration pragma:
11482 @smallexample
11483 pragma Elaboration_Checks (RM);
11484 @end smallexample
11486 @noindent
11487 Either approach will cause the unit affected to be compiled using the
11488 standard dynamic run-time elaboration checks described in the Ada
11489 Reference Manual. The static model is generally preferable, since it
11490 is clearly safer to rely on compile and link time checks rather than
11491 run-time checks. However, in the case of legacy code, it may be
11492 difficult to meet the requirements of the static model. This
11493 issue is further discussed in
11494 @ref{What to Do If the Default Elaboration Behavior Fails}.
11496 Note that the static model provides a strict subset of the allowed
11497 behavior and programs of the Ada Reference Manual, so if you do
11498 adhere to the static model and no circularities exist,
11499 then you are assured that your program will
11500 work using the dynamic model.
11502 @node Elaboration Issues for Library Tasks
11503 @section Elaboration Issues for Library Tasks
11504 @cindex Library tasks, elaboration issues
11505 @cindex Elaboration of library tasks
11507 @noindent
11508 In this section we examine special elaboration issues that arise for
11509 programs that declare library level tasks.
11511 Generally the model of execution of an Ada program is that all units are
11512 elaborated, and then execution of the program starts. However, the
11513 declaration of library tasks definitely does not fit this model. The
11514 reason for this is that library tasks start as soon as they are declared
11515 (more precisely, as soon as the statement part of the enclosing package
11516 body is reached), that is to say before elaboration
11517 of the program is complete. This means that if such a task calls a
11518 subprogram, or an entry in another task, the callee may or may not be
11519 elaborated yet, and in the standard
11520 Reference Manual model of dynamic elaboration checks, you can even
11521 get timing dependent Program_Error exceptions, since there can be
11522 a race between the elaboration code and the task code.
11524 The static model of elaboration in GNAT seeks to avoid all such
11525 dynamic behavior, by being conservative, and the conservative
11526 approach in this particular case is to assume that all the code
11527 in a task body is potentially executed at elaboration time if
11528 a task is declared at the library level.
11530 This can definitely result in unexpected circularities. Consider
11531 the following example
11533 @smallexample
11534 package Decls is
11535   task Lib_Task is
11536      entry Start;
11537   end Lib_Task;
11539   type My_Int is new Integer;
11541   function Ident (M : My_Int) return My_Int;
11542 end Decls;
11544 with Utils;
11545 package body Decls is
11546   task body Lib_Task is
11547   begin
11548      accept Start;
11549      Utils.Put_Val (2);
11550   end Lib_Task;
11552   function Ident (M : My_Int) return My_Int is
11553   begin
11554      return M;
11555   end Ident;
11556 end Decls;
11558 with Decls;
11559 package Utils is
11560   procedure Put_Val (Arg : Decls.My_Int);
11561 end Utils;
11563 with Text_IO;
11564 package body Utils is
11565   procedure Put_Val (Arg : Decls.My_Int) is
11566   begin
11567      Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
11568   end Put_Val;
11569 end Utils;
11571 with Decls;
11572 procedure Main is
11573 begin
11574    Decls.Lib_Task.Start;
11575 end;
11576 @end smallexample
11578 @noindent
11579 If the above example is compiled in the default static elaboration
11580 mode, then a circularity occurs. The circularity comes from the call
11581 @code{Utils.Put_Val} in the task body of @code{Decls.Lib_Task}. Since
11582 this call occurs in elaboration code, we need an implicit pragma
11583 @code{Elaborate_All} for @code{Utils}. This means that not only must
11584 the spec and body of @code{Utils} be elaborated before the body
11585 of @code{Decls}, but also the spec and body of any unit that is
11586 @code{with'ed} by the body of @code{Utils} must also be elaborated before
11587 the body of @code{Decls}. This is the transitive implication of
11588 pragma @code{Elaborate_All} and it makes sense, because in general
11589 the body of @code{Put_Val} might have a call to something in a
11590 @code{with'ed} unit.
11592 In this case, the body of Utils (actually its spec) @code{with's}
11593 @code{Decls}. Unfortunately this means that the body of @code{Decls}
11594 must be elaborated before itself, in case there is a call from the
11595 body of @code{Utils}.
11597 Here is the exact chain of events we are worrying about:
11599 @enumerate
11600 @item
11601 In the body of @code{Decls} a call is made from within the body of a library
11602 task to a subprogram in the package @code{Utils}. Since this call may
11603 occur at elaboration time (given that the task is activated at elaboration
11604 time), we have to assume the worst, i.e. that the
11605 call does happen at elaboration time.
11607 @item
11608 This means that the body and spec of @code{Util} must be elaborated before
11609 the body of @code{Decls} so that this call does not cause an access before
11610 elaboration.
11612 @item
11613 Within the body of @code{Util}, specifically within the body of
11614 @code{Util.Put_Val} there may be calls to any unit @code{with}'ed
11615 by this package.
11617 @item
11618 One such @code{with}'ed package is package @code{Decls}, so there
11619 might be a call to a subprogram in @code{Decls} in @code{Put_Val}.
11620 In fact there is such a call in this example, but we would have to
11621 assume that there was such a call even if it were not there, since
11622 we are not supposed to write the body of @code{Decls} knowing what
11623 is in the body of @code{Utils}; certainly in the case of the
11624 static elaboration model, the compiler does not know what is in
11625 other bodies and must assume the worst.
11627 @item
11628 This means that the spec and body of @code{Decls} must also be
11629 elaborated before we elaborate the unit containing the call, but
11630 that unit is @code{Decls}! This means that the body of @code{Decls}
11631 must be elaborated before itself, and that's a circularity.
11632 @end enumerate
11634 @noindent
11635 Indeed, if you add an explicit pragma Elaborate_All for @code{Utils} in
11636 the body of @code{Decls} you will get a true Ada Reference Manual
11637 circularity that makes the program illegal.
11639 In practice, we have found that problems with the static model of
11640 elaboration in existing code often arise from library tasks, so
11641 we must address this particular situation.
11643 Note that if we compile and run the program above, using the dynamic model of
11644 elaboration (that is to say use the @option{/CHECKS=ELABORATION} qualifier),
11645 then it compiles, binds,
11646 links, and runs, printing the expected result of 2. Therefore in some sense
11647 the circularity here is only apparent, and we need to capture
11648 the properties of this program that  distinguish it from other library-level
11649 tasks that have real elaboration problems.
11651 We have four possible answers to this question:
11653 @itemize @bullet
11655 @item
11656 Use the dynamic model of elaboration.
11658 If we use the @option{/CHECKS=ELABORATION} qualifier, then as noted above, the program works.
11659 Why is this? If we examine the task body, it is apparent that the task cannot
11660 proceed past the
11661 @code{accept} statement until after elaboration has been completed, because
11662 the corresponding entry call comes from the main program, not earlier.
11663 This is why the dynamic model works here. But that's really giving
11664 up on a precise analysis, and we prefer to take this approach only if we cannot
11665 solve the
11666 problem in any other manner. So let us examine two ways to reorganize
11667 the program to avoid the potential elaboration problem.
11669 @item
11670 Split library tasks into separate packages.
11672 Write separate packages, so that library tasks are isolated from
11673 other declarations as much as possible. Let us look at a variation on
11674 the above program.
11676 @smallexample
11677 package Decls1 is
11678   task Lib_Task is
11679      entry Start;
11680   end Lib_Task;
11681 end Decls1;
11683 with Utils;
11684 package body Decls1 is
11685   task body Lib_Task is
11686   begin
11687      accept Start;
11688      Utils.Put_Val (2);
11689   end Lib_Task;
11690 end Decls1;
11692 package Decls2 is
11693   type My_Int is new Integer;
11694   function Ident (M : My_Int) return My_Int;
11695 end Decls2;
11697 with Utils;
11698 package body Decls2 is
11699   function Ident (M : My_Int) return My_Int is
11700   begin
11701      return M;
11702   end Ident;
11703 end Decls2;
11705 with Decls2;
11706 package Utils is
11707   procedure Put_Val (Arg : Decls2.My_Int);
11708 end Utils;
11710 with Text_IO;
11711 package body Utils is
11712   procedure Put_Val (Arg : Decls2.My_Int) is
11713   begin
11714      Text_IO.Put_Line (Decls2.My_Int'Image (Decls2.Ident (Arg)));
11715   end Put_Val;
11716 end Utils;
11718 with Decls1;
11719 procedure Main is
11720 begin
11721    Decls1.Lib_Task.Start;
11722 end;
11723 @end smallexample
11725 @noindent
11726 All we have done is to split @code{Decls} into two packages, one
11727 containing the library task, and one containing everything else. Now
11728 there is no cycle, and the program compiles, binds, links and executes
11729 using the default static model of elaboration.
11731 @item
11732 Declare separate task types.
11734 A significant part of the problem arises because of the use of the
11735 single task declaration form. This means that the elaboration of
11736 the task type, and the elaboration of the task itself (i.e. the
11737 creation of the task) happen at the same time. A good rule
11738 of style in Ada 95 is to always create explicit task types. By
11739 following the additional step of placing task objects in separate
11740 packages from the task type declaration, many elaboration problems
11741 are avoided. Here is another modified example of the example program:
11743 @smallexample
11744 package Decls is
11745   task type Lib_Task_Type is
11746      entry Start;
11747   end Lib_Task_Type;
11749   type My_Int is new Integer;
11751   function Ident (M : My_Int) return My_Int;
11752 end Decls;
11754 with Utils;
11755 package body Decls is
11756   task body Lib_Task_Type is
11757   begin
11758      accept Start;
11759      Utils.Put_Val (2);
11760   end Lib_Task_Type;
11762   function Ident (M : My_Int) return My_Int is
11763   begin
11764      return M;
11765   end Ident;
11766 end Decls;
11768 with Decls;
11769 package Utils is
11770   procedure Put_Val (Arg : Decls.My_Int);
11771 end Utils;
11773 with Text_IO;
11774 package body Utils is
11775   procedure Put_Val (Arg : Decls.My_Int) is
11776   begin
11777      Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
11778   end Put_Val;
11779 end Utils;
11781 with Decls;
11782 package Declst is
11783    Lib_Task : Decls.Lib_Task_Type;
11784 end Declst;
11786 with Declst;
11787 procedure Main is
11788 begin
11789    Declst.Lib_Task.Start;
11790 end;
11791 @end smallexample
11793 @noindent
11794 What we have done here is to replace the @code{task} declaration in
11795 package @code{Decls} with a @code{task type} declaration. Then we
11796 introduce a separate package @code{Declst} to contain the actual
11797 task object. This separates the elaboration issues for
11798 the @code{task type}
11799 declaration, which causes no trouble, from the elaboration issues
11800 of the task object, which is also unproblematic, since it is now independent
11801 of the elaboration of  @code{Utils}.
11802 This separation of concerns also corresponds to
11803 a generally sound engineering principle of separating declarations
11804 from instances. This version of the program also compiles, binds, links,
11805 and executes, generating the expected output.
11807 @item
11808 Use No_Entry_Calls_In_Elaboration_Code restriction.
11809 @cindex No_Entry_Calls_In_Elaboration_Code
11811 The previous two approaches described how a program can be restructured
11812 to avoid the special problems caused by library task bodies. in practice,
11813 however, such restructuring may be difficult to apply to existing legacy code,
11814 so we must consider solutions that do not require massive rewriting.
11816 Let us consider more carefully why our original sample program works
11817 under the dynamic model of elaboration. The reason is that the code
11818 in the task body blocks immediately on the @code{accept}
11819 statement. Now of course there is nothing to prohibit elaboration
11820 code from making entry calls (for example from another library level task),
11821 so we cannot tell in isolation that
11822 the task will not execute the accept statement  during elaboration.
11824 However, in practice it is very unusual to see elaboration code
11825 make any entry calls, and the pattern of tasks starting
11826 at elaboration time and then immediately blocking on @code{accept} or
11827 @code{select} statements is very common. What this means is that
11828 the compiler is being too pessimistic when it analyzes the
11829 whole package body as though it might be executed at elaboration
11830 time.
11832 If we know that the elaboration code contains no entry calls, (a very safe
11833 assumption most of the time, that could almost be made the default
11834 behavior), then we can compile all units of the program under control
11835 of the following configuration pragma:
11837 @smallexample
11838 pragma Restrictions (No_Entry_Calls_In_Elaboration_Code);
11839 @end smallexample
11841 @noindent
11842 This pragma can be placed in the @file{GNAT.ADC} file in the usual
11843 manner. If we take our original unmodified program and compile it
11844 in the presence of a @file{GNAT.ADC} containing the above pragma,
11845 then once again, we can compile, bind, link, and execute, obtaining
11846 the expected result. In the presence of this pragma, the compiler does
11847 not trace calls in a task body, that appear after the first @code{accept}
11848 or @code{select} statement, and therefore does not report a potential
11849 circularity in the original program.
11851 The compiler will check to the extent it can that the above
11852 restriction is not violated, but it is not always possible to do a
11853 complete check at compile time, so it is important to use this
11854 pragma only if the stated restriction is in fact met, that is to say
11855 no task receives an entry call before elaboration of all units is completed.
11857 @end itemize
11859 @node Mixing Elaboration Models
11860 @section Mixing Elaboration Models
11861 @noindent
11862 So far, we have assumed that the entire program is either compiled
11863 using the dynamic model or static model, ensuring consistency. It
11864 is possible to mix the two models, but rules have to be followed
11865 if this mixing is done to ensure that elaboration checks are not
11866 omitted.
11868 The basic rule is that @emph{a unit compiled with the static model cannot
11869 be @code{with'ed} by a unit compiled with the dynamic model}. The
11870 reason for this is that in the static model, a unit assumes that
11871 its clients guarantee to use (the equivalent of) pragma
11872 @code{Elaborate_All} so that no elaboration checks are required
11873 in inner subprograms, and this assumption is violated if the
11874 client is compiled with dynamic checks.
11876 The precise rule is as follows. A unit that is compiled with dynamic
11877 checks can only @code{with} a unit that meets at least one of the
11878 following criteria:
11880 @itemize @bullet
11882 @item
11883 The @code{with'ed} unit is itself compiled with dynamic elaboration
11884 checks (that is with the @option{/CHECKS=ELABORATION} qualifier.
11886 @item
11887 The @code{with'ed} unit is an internal GNAT implementation unit from
11888 the System, Interfaces, Ada, or GNAT hierarchies.
11890 @item
11891 The @code{with'ed} unit has pragma Preelaborate or pragma Pure.
11893 @item
11894 The @code{with'ing} unit (that is the client) has an explicit pragma
11895 @code{Elaborate_All} for the @code{with'ed} unit.
11897 @end itemize
11899 @noindent
11900 If this rule is violated, that is if a unit with dynamic elaboration
11901 checks @code{with's} a unit that does not meet one of the above four
11902 criteria, then the binder (@code{GNAT BIND}) will issue a warning
11903 similar to that in the following example:
11905 @smallexample
11906 warning: "X.ADS" has dynamic elaboration checks and with's
11907 warning:   "Y.ADS" which has static elaboration checks
11908 @end smallexample
11910 @noindent
11911 These warnings indicate that the rule has been violated, and that as a result
11912 elaboration checks may be missed in the resulting executable file.
11913 This warning may be suppressed using the @code{-ws} binder qualifier
11914 in the usual manner.
11916 One useful application of this mixing rule is in the case of a subsystem
11917 which does not itself @code{with} units from the remainder of the
11918 application. In this case, the entire subsystem can be compiled with
11919 dynamic checks to resolve a circularity in the subsystem, while
11920 allowing the main application that uses this subsystem to be compiled
11921 using the more reliable default static model.
11923 @node What to Do If the Default Elaboration Behavior Fails
11924 @section What to Do If the Default Elaboration Behavior Fails
11926 @noindent
11927 If the binder cannot find an acceptable order, it outputs detailed
11928 diagnostics. For example:
11929 @smallexample
11930 @group
11931 @iftex
11932 @leftskip=0cm
11933 @end iftex
11934 error: elaboration circularity detected
11935 info:   "proc (body)" must be elaborated before "pack (body)"
11936 info:     reason: Elaborate_All probably needed in unit "pack (body)"
11937 info:     recompile "pack (body)" with /WARNINGS=ELABORATION
11938 info:                             for full details
11939 info:       "proc (body)"
11940 info:         is needed by its spec:
11941 info:       "proc (spec)"
11942 info:         which is withed by:
11943 info:       "pack (body)"
11944 info:  "pack (body)" must be elaborated before "proc (body)"
11945 info:     reason: pragma Elaborate in unit "proc (body)"
11946 @end group
11948 @end smallexample
11950 @noindent
11951 In this case we have a cycle that the binder cannot break. On the one
11952 hand, there is an explicit pragma Elaborate in @code{proc} for
11953 @code{pack}. This means that the body of @code{pack} must be elaborated
11954 before the body of @code{proc}. On the other hand, there is elaboration
11955 code in @code{pack} that calls a subprogram in @code{proc}. This means
11956 that for maximum safety, there should really be a pragma
11957 Elaborate_All in @code{pack} for @code{proc} which would require that
11958 the body of @code{proc} be elaborated before the body of
11959 @code{pack}. Clearly both requirements cannot be satisfied.
11960 Faced with a circularity of this kind, you have three different options.
11962 @table @asis
11963 @item Fix the program
11964 The most desirable option from the point of view of long-term maintenance
11965 is to rearrange the program so that the elaboration problems are avoided.
11966 One useful technique is to place the elaboration code into separate
11967 child packages. Another is to move some of the initialization code to
11968 explicitly called subprograms, where the program controls the order
11969 of initialization explicitly. Although this is the most desirable option,
11970 it may be impractical and involve too much modification, especially in
11971 the case of complex legacy code.
11973 @item Perform dynamic checks
11974 If the compilations are done using the
11975 @option{/CHECKS=ELABORATION}
11976 (dynamic elaboration check) qualifier, then GNAT behaves in
11977 a quite different manner. Dynamic checks are generated for all calls
11978 that could possibly result in raising an exception. With this qualifier,
11979 the compiler does not generate implicit @code{Elaborate_All} pragmas.
11980 The behavior then is exactly as specified in the Ada 95 Reference Manual.
11981 The binder will generate an executable program that may or may not
11982 raise @code{Program_Error}, and then it is the programmer's job to ensure
11983 that it does not raise an exception. Note that it is important to
11984 compile all units with the qualifier, it cannot be used selectively.
11986 @item Suppress checks
11987 The drawback of dynamic checks is that they generate a
11988 significant overhead at run time, both in space and time. If you
11989 are absolutely sure that your program cannot raise any elaboration
11990 exceptions, and you still want to use the dynamic elaboration model,
11991 then you can use the configuration pragma
11992 @code{Suppress (Elaboration_Checks)} to suppress all such checks. For
11993 example this pragma could be placed in the @file{GNAT.ADC} file.
11995 @item Suppress checks selectively
11996 When you know that certain calls in elaboration code cannot possibly
11997 lead to an elaboration error, and the binder nevertheless generates warnings
11998 on those calls and inserts Elaborate_All pragmas that lead to elaboration
11999 circularities, it is possible to remove those warnings locally and obtain
12000 a program that will bind. Clearly this can be unsafe, and it is the
12001 responsibility of the programmer to make sure that the resulting program has
12002 no elaboration anomalies. The pragma @code{Suppress (Elaboration_Check)} can
12003 be used with different granularity to suppress warnings and break
12004 elaboration circularities:
12006 @itemize @bullet
12007 @item
12008 Place the pragma that names the called subprogram in the declarative part
12009 that contains the call.
12011 @item
12012 Place the pragma in the declarative part, without naming an entity. This
12013 disables warnings on all calls in the corresponding  declarative region.
12015 @item
12016 Place the pragma in the package spec that declares the called subprogram,
12017 and name the subprogram. This disables warnings on all elaboration calls to
12018 that subprogram.
12020 @item
12021 Place the pragma in the package spec that declares the called subprogram,
12022 without naming any entity. This disables warnings on all elaboration calls to
12023 all subprograms declared in this spec.
12024 @end itemize
12026 @noindent
12027 These four cases are listed in order of decreasing safety, and therefore
12028 require increasing programmer care in their application. Consider the
12029 following program:
12030 @smallexample
12032 package Pack1 is
12033   function F1 return Integer;
12034   X1 : Integer;
12035 end Pack1;
12037 package Pack2 is
12038   function F2 return Integer;
12039   function Pure (x : integer) return integer;
12040   --  pragma Suppress (Elaboration_Check, On => Pure);  -- (3)
12041   --  pragma Suppress (Elaboration_Check);              -- (4)
12042 end Pack2;
12044 with Pack2;
12045 package body Pack1 is
12046   function F1 return Integer is
12047   begin
12048     return 100;
12049   end F1;
12050   Val : integer := Pack2.Pure (11);    --  Elab. call (1)
12051 begin
12052   declare
12053     --  pragma Suppress(Elaboration_Check, Pack2.F2);   -- (1)
12054     --  pragma Suppress(Elaboration_Check);             -- (2)
12055   begin
12056     X1 := Pack2.F2 + 1;                --  Elab. call (2)
12057   end;
12058 end Pack1;
12060 with Pack1;
12061 package body Pack2 is
12062   function F2 return Integer is
12063   begin
12064      return Pack1.F1;
12065   end F2;
12066   function Pure (x : integer) return integer is
12067   begin
12068      return x ** 3 - 3 * x;
12069   end;
12070 end Pack2;
12072 with Pack1, Ada.Text_IO;
12073 procedure Proc3 is
12074 begin
12075   Ada.Text_IO.Put_Line(Pack1.X1'Img); -- 101
12076 end Proc3;
12077 @end smallexample
12078 In the absence of any pragmas, an attempt to bind this program produces
12079 the following diagnostics:
12080 @smallexample
12081 @group
12082 @iftex
12083 @leftskip=.5cm
12084 @end iftex
12085 error: elaboration circularity detected
12086 info:    "pack1 (body)" must be elaborated before "pack1 (body)"
12087 info:       reason: Elaborate_All probably needed in unit "pack1 (body)"
12088 info:       recompile "pack1 (body)" with /WARNINGS=ELABORATION for full details
12089 info:          "pack1 (body)"
12090 info:             must be elaborated along with its spec:
12091 info:          "pack1 (spec)"
12092 info:             which is withed by:
12093 info:          "pack2 (body)"
12094 info:             which must be elaborated along with its spec:
12095 info:          "pack2 (spec)"
12096 info:             which is withed by:
12097 info:          "pack1 (body)"
12098 @end group
12099 @end smallexample
12100 The sources of the circularity are the two calls to @code{Pack2.Pure} and
12101 @code{Pack2.F2} in the body of @code{Pack1}. We can see that the call to
12102 F2 is safe, even though F2 calls F1, because the call appears after the
12103 elaboration of the body of F1. Therefore the pragma (1) is safe, and will
12104 remove the warning on the call. It is also possible to use pragma (2)
12105 because there are no other potentially unsafe calls in the block.
12107 @noindent
12108 The call to @code{Pure} is safe because this function does not depend on the
12109 state of @code{Pack2}. Therefore any call to this function is safe, and it
12110 is correct to place pragma (3) in the corresponding package spec.
12112 @noindent
12113 Finally, we could place pragma (4) in the spec of @code{Pack2} to disable
12114 warnings on all calls to functions declared therein. Note that this is not
12115 necessarily safe, and requires more detailed examination of the subprogram
12116 bodies involved. In particular, a call to @code{F2} requires that @code{F1}
12117 be already elaborated.
12118 @end table
12120 @noindent
12121 It is hard to generalize on which of these four approaches should be
12122 taken. Obviously if it is possible to fix the program so that the default
12123 treatment works, this is preferable, but this may not always be practical.
12124 It is certainly simple enough to use
12125 @option{/CHECKS=ELABORATION}
12126 but the danger in this case is that, even if the GNAT binder
12127 finds a correct elaboration order, it may not always do so,
12128 and certainly a binder from another Ada compiler might not. A
12129 combination of testing and analysis (for which the warnings generated
12130 with the
12131 @option{/WARNINGS=ELABORATION}
12132 qualifier can be useful) must be used to ensure that the program is free
12133 of errors. One qualifier that is useful in this testing is the
12134 @code{/PESSIMISTIC_ELABORATION_ORDER}
12135 qualifier for
12136 @code{GNAT BIND}.
12137 Normally the binder tries to find an order that has the best chance of
12138 of avoiding elaboration problems. With this qualifier, the binder
12139 plays a devil's advocate role, and tries to choose the order that
12140 has the best chance of failing. If your program works even with this
12141 qualifier, then it has a better chance of being error free, but this is still
12142 not a guarantee.
12144 For an example of this approach in action, consider the C-tests (executable
12145 tests) from the ACVC suite. If these are compiled and run with the default
12146 treatment, then all but one of them succeed without generating any error
12147 diagnostics from the binder. However, there is one test that fails, and
12148 this is not surprising, because the whole point of this test is to ensure
12149 that the compiler can handle cases where it is impossible to determine
12150 a correct order statically, and it checks that an exception is indeed
12151 raised at run time.
12153 This one test must be compiled and run using the
12154 @option{/CHECKS=ELABORATION}
12155 qualifier, and then it passes. Alternatively, the entire suite can
12156 be run using this qualifier. It is never wrong to run with the dynamic
12157 elaboration qualifier if your code is correct, and we assume that the
12158 C-tests are indeed correct (it is less efficient, but efficiency is
12159 not a factor in running the ACVC tests.)
12161 @node Elaboration for Access-to-Subprogram Values
12162 @section Elaboration for Access-to-Subprogram Values
12163 @cindex Access-to-subprogram
12165 @noindent
12166 The introduction of access-to-subprogram types in Ada 95 complicates
12167 the handling of elaboration. The trouble is that it becomes
12168 impossible to tell at compile time which procedure
12169 is being called. This means that it is not possible for the binder
12170 to analyze the elaboration requirements in this case.
12172 If at the point at which the access value is created
12173 (i.e., the evaluation of @code{P'Access} for a subprogram @code{P}),
12174 the body of the subprogram is
12175 known to have been elaborated, then the access value is safe, and its use
12176 does not require a check. This may be achieved by appropriate arrangement
12177 of the order of declarations if the subprogram is in the current unit,
12178 or, if the subprogram is in another unit, by using pragma
12179 @code{Pure}, @code{Preelaborate}, or @code{Elaborate_Body}
12180 on the referenced unit.
12182 If the referenced body is not known to have been elaborated at the point
12183 the access value is created, then any use of the access value must do a
12184 dynamic check, and this dynamic check will fail and raise a
12185 @code{Program_Error} exception if the body has not been elaborated yet.
12186 GNAT will generate the necessary checks, and in addition, if the
12187 @option{/WARNINGS=ELABORATION}
12188 qualifier is set, will generate warnings that such checks are required.
12190 The use of dynamic dispatching for tagged types similarly generates
12191 a requirement for dynamic checks, and premature calls to any primitive
12192 operation of a tagged type before the body of the operation has been elaborated,
12193 will result in the raising of @code{Program_Error}.
12195 @node Summary of Procedures for Elaboration Control
12196 @section Summary of Procedures for Elaboration Control
12197 @cindex Elaboration control
12199 @noindent
12200 First, compile your program with the default options, using none of
12201 the special elaboration control qualifiers. If the binder successfully
12202 binds your program, then you can be confident that, apart from issues
12203 raised by the use of access-to-subprogram types and dynamic dispatching,
12204 the program is free of elaboration errors. If it is important that the
12205 program be portable, then use the
12206 @option{/WARNINGS=ELABORATION}
12207 qualifier to generate warnings about missing @code{Elaborate_All}
12208 pragmas, and supply the missing pragmas.
12210 If the program fails to bind using the default static elaboration
12211 handling, then you can fix the program to eliminate the binder
12212 message, or recompile the entire program with the
12213 @option{/CHECKS=ELABORATION} qualifier to generate dynamic elaboration checks,
12214 and, if you are sure there really are no elaboration problems,
12215 use a global pragma @code{Suppress (Elaboration_Checks)}.
12217 @node Other Elaboration Order Considerations
12218 @section Other Elaboration Order Considerations
12219 @noindent
12220 This section has been entirely concerned with the issue of finding a valid
12221 elaboration order, as defined by the Ada Reference Manual. In a case
12222 where several elaboration orders are valid, the task is to find one
12223 of the possible valid elaboration orders (and the static model in GNAT
12224 will ensure that this is achieved).
12226 The purpose of the elaboration rules in the Ada Reference Manual is to
12227 make sure that no entity is accessed before it has been elaborated. For
12228 a subprogram, this means that the spec and body must have been elaborated
12229 before the subprogram is called. For an object, this means that the object
12230 must have been elaborated before its value is read or written. A violation
12231 of either of these two requirements is an access before elaboration order,
12232 and this section has been all about avoiding such errors.
12234 In the case where more than one order of elaboration is possible, in the
12235 sense that access before elaboration errors are avoided, then any one of
12236 the orders is "correct" in the sense that it meets the requirements of
12237 the Ada Reference Manual, and no such error occurs.
12239 However, it may be the case for a given program, that there are
12240 constraints on the order of elaboration that come not from consideration
12241 of avoiding elaboration errors, but rather from extra-lingual logic
12242 requirements. Consider this example:
12244 @smallexample
12245 with Init_Constants;
12246 package Constants is
12247    X : Integer := 0;
12248    Y : Integer := 0;
12249 end Constants;
12251 package Init_Constants is
12252    procedure Calc;
12253 end Init_Constants;
12255 with Constants;
12256 package body Init_Constants is
12257    procedure Calc is begin null; end;
12258 begin
12259    Constants.X := 3;
12260    Constants.Y := 4;
12261 end Init_Constants;
12263 with Constants;
12264 package Calc is
12265    Z : Integer := Constants.X + Constants.Y;
12266 end Calc;
12268 with Calc;
12269 with Text_IO; use Text_IO;
12270 procedure Main is
12271 begin
12272    Put_Line (Calc.Z'Img);
12273 end Main;
12274 @end smallexample
12276 @noindent
12277 In this example, there is more than one valid order of elaboration. For
12278 example both the following are correct orders:
12280 @smallexample
12281 Init_Constants spec
12282 Constants spec
12283 Calc spec
12284 Main body
12285 Init_Constants body
12287   and
12289 Init_Constants spec
12290 Init_Constants body
12291 Constants spec
12292 Calc spec
12293 Main body
12294 @end smallexample
12296 @noindent
12297 There is no language rule to prefer one or the other, both are correct
12298 from an order of elaboration point of view. But the programmatic effects
12299 of the two orders are very different. In the first, the elaboration routine
12300 of @code{Calc} initializes @code{Z} to zero, and then the main program
12301 runs with this value of zero. But in the second order, the elaboration
12302 routine of @code{Calc} runs after the body of Init_Constants has set
12303 @code{X} and @code{Y} and thus @code{Z} is set to 7 before @code{Main}
12304 runs.
12306 One could perhaps by applying pretty clever non-artificial intelligence
12307 to the situation guess that it is more likely that the second order of
12308 elaboration is the one desired, but there is no formal linguistic reason
12309 to prefer one over the other. In fact in this particular case, GNAT will
12310 prefer the second order, because of the rule that bodies are elaborated
12311 as soon as possible, but it's just luck that this is what was wanted
12312 (if indeed the second order was preferred).
12314 If the program cares about the order of elaboration routines in a case like
12315 this, it is important to specify the order required. In this particular
12316 case, that could have been achieved by adding to the spec of Calc:
12318 @smallexample
12319 pragma Elaborate_All (Constants);
12320 @end smallexample
12322 @noindent
12323 which requires that the body (if any) and spec of @code{Constants},
12324 as well as the body and spec of any unit @code{with}'ed by
12325 @code{Constants} be elaborated before @code{Calc} is elaborated.
12327 Clearly no automatic method can always guess which alternative you require,
12328 and if you are working with legacy code that had constraints of this kind
12329 which were not properly specified by adding @code{Elaborate} or
12330 @code{Elaborate_All} pragmas, then indeed it is possible that two different
12331 compilers can choose different orders.
12333 The @code{GNAT BIND}
12334 @code{/PESSIMISTIC_ELABORATION} qualifier may be useful in smoking
12335 out problems. This qualifier causes bodies to be elaborated as late as possible
12336 instead of as early as possible. In the example above, it would have forced
12337 the choice of the first elaboration order. If you get different results
12338 when using this qualifier, and particularly if one set of results is right,
12339 and one is wrong as far as you are concerned, it shows that you have some
12340 missing @code{Elaborate} pragmas. For the example above, we have the
12341 following output:
12343 @smallexample
12344 GNAT MAKE -f -q main
12345 main
12347 GNAT MAKE -f -q main /BINDER_QUALIFIERS -p
12348 main
12350 @end smallexample
12352 @noindent
12353 It is of course quite unlikely that both these results are correct, so
12354 it is up to you in a case like this to investigate the source of the
12355 difference, by looking at the two elaboration orders that are chosen,
12356 and figuring out which is correct, and then adding the necessary
12357 @code{Elaborate_All} pragmas to ensure the desired order.
12359 @node The Cross-Referencing Tools GNAT XREF and GNAT FIND
12360 @chapter  The Cross-Referencing Tools @code{GNAT XREF} and @code{GNAT FIND}
12361 @findex GNAT XREF
12362 @findex GNAT FIND
12364 @noindent
12365 The compiler generates cross-referencing information (unless
12366 you set the @samp{/XREF=SUPPRESS} qualifier), which are saved in the @file{.ALI} files.
12367 This information indicates where in the source each entity is declared and
12368 referenced. Note that entities in package Standard are not included, but
12369 entities in all other predefined units are included in the output.
12371 Before using any of these two tools, you need to compile successfully your
12372 application, so that GNAT gets a chance to generate the cross-referencing
12373 information.
12375 The two tools @code{GNAT XREF} and @code{GNAT FIND} take advantage of this
12376 information to provide the user with the capability to easily locate the
12377 declaration and references to an entity. These tools are quite similar,
12378 the difference being that @code{GNAT FIND} is intended for locating
12379 definitions and/or references to a specified entity or entities, whereas
12380 @code{GNAT XREF} is oriented to generating a full report of all
12381 cross-references.
12383 To use these tools, you must not compile your application using the
12384 @option{/XREF=SUPPRESS} qualifier on the @file{GNAT MAKE} command line (@inforef{The
12385 GNAT Make Program GNAT MAKE,,gnat_ug}). Otherwise, cross-referencing
12386 information will not be generated.
12388 @menu
12389 * GNAT XREF Qualifiers::
12390 * GNAT FIND Qualifiers::
12391 * Project Files for GNAT XREF and GNAT FIND::
12392 * Regular Expressions in GNAT FIND and GNAT XREF::
12393 * Examples of GNAT XREF Usage::
12394 * Examples of GNAT FIND Usage::
12395 @end menu
12397 @node GNAT XREF Qualifiers
12398 @section @code{GNAT XREF} Qualifiers
12400 @noindent
12401 The command lines for @code{GNAT XREF} is:
12402 @smallexample
12403 $ GNAT XREF [qualifiers] sourcefile1 [sourcefile2 ...]
12404 @end smallexample
12406 @noindent
12407 where
12409 @table @code
12410 @item sourcefile1, sourcefile2
12411 identifies the source files for which a report is to be generated. The
12412 'with'ed units will be processed too. You must provide at least one file.
12414 These file names are considered to be regular expressions, so for instance
12415 specifying 'source*.ADB' is the same as giving every file in the current
12416 directory whose name starts with 'source' and whose extension is 'adb'.
12418 @end table
12420 @noindent
12421 The qualifiers can be :
12422 @table @code
12423 @item /ALL_FILES
12424 If this qualifier is present, @code{GNAT FIND} and @code{GNAT XREF} will parse
12425 the read-only files found in the library search path. Otherwise, these files
12426 will be ignored. This option can be used to protect Gnat sources or your own
12427 libraries from being parsed, thus making @code{GNAT FIND} and @code{GNAT XREF}
12428 much faster, and their output much smaller.
12430 @item /SOURCE_SEARCH=direc
12431 When looking for source files also look in directory DIR. The order in which
12432 source file search is undertaken is the same as for @file{GNAT MAKE}.
12434 @item /OBJECT_SEARCH=direc
12435 When searching for library and object files, look in directory
12436 DIR. The order in which library files are searched is the same as for
12437 @file{GNAT MAKE}.
12439 @item /NOSTD_INCLUDES
12440 Do not look for sources in the system default directory.
12442 @item /NOSTD_LIBRARIES
12443 Do not look for library files in the system default directory.
12445 @item /RUNTIME_SYSTEM=@var{rts-path}
12446 @cindex @code{/RUNTIME_SYSTEM} (@code{GNAT XREF})
12447 Specifies the default location of the runtime library. Same meaning as the
12448 equivalent @code{GNAT MAKE} flag (see @ref{Qualifiers for GNAT MAKE}).
12450 @item -d
12451 If this qualifier is set @code{GNAT XREF} will output the parent type
12452 reference for each matching derived types.
12454 @item /FULL_PATHNAME
12455 If this qualifier is set, the output file names will be preceded by their
12456 directory (if the file was found in the search path). If this qualifier is
12457 not set, the directory will not be printed.
12459 @item /IGNORE_LOCALS
12460 If this qualifier is set, information is output only for library-level
12461 entities, ignoring local entities. The use of this qualifier may accelerate
12462 @code{GNAT FIND} and @code{GNAT XREF}.
12464 @item /SEARCH=direc
12465 Equivalent to @samp{/OBJECT_SEARCH=direc /SOURCE_SEARCH=direc}.
12467 @item /PROJECT=file
12468 Specify a project file to use @xref{Project Files}.
12469 By default, @code{GNAT XREF} and @code{GNAT FIND} will try to locate a
12470 project file in the current directory.
12472 If a project file is either specified or found by the tools, then the content
12473 of the source directory and object directory lines are added as if they
12474 had been specified respectively by @samp{/SOURCE_SEARCH}
12475 and @samp{OBJECT_SEARCH}.
12476 @item /UNUSED
12477 Output only unused symbols. This may be really useful if you give your
12478 main compilation unit on the command line, as @code{GNAT XREF} will then
12479 display every unused entity and 'with'ed package.
12482 @end table
12484 All these qualifiers may be in any order on the command line, and may even
12485 appear after the file names. They need not be separated by spaces, thus
12486 you can say @samp{GNAT XREF /ALL_FILES/IGNORE_LOCALS} instead of
12487 @samp{GNAT XREF /ALL_FILES /IGNORE_LOCALS}.
12489 @node GNAT FIND Qualifiers
12490 @section @code{GNAT FIND} Qualifiers
12492 @noindent
12493 The command line for @code{GNAT FIND} is:
12495 @smallexample
12496 $ GNAT FIND [qualifiers] pattern[:sourcefile[:line[:column]]]
12497       [file1 file2 ...]
12498 @end smallexample
12500 @noindent
12501 where
12503 @table @code
12504 @item pattern
12505 An entity will be output only if it matches the regular expression found
12506 in @samp{pattern}, see @xref{Regular Expressions in GNAT FIND and GNAT XREF}.
12508 Omitting the pattern is equivalent to specifying @samp{*}, which
12509 will match any entity. Note that if you do not provide a pattern, you
12510 have to provide both a sourcefile and a line.
12512 Entity names are given in Latin-1, with uppercase/lowercase equivalence
12513 for matching purposes. At the current time there is no support for
12514 8-bit codes other than Latin-1, or for wide characters in identifiers.
12516 @item sourcefile
12517 @code{GNAT FIND} will look for references, bodies or declarations
12518 of symbols referenced in @file{sourcefile}, at line @samp{line}
12519 and column @samp{column}. See @pxref{Examples of GNAT FIND Usage}
12520 for syntax examples.
12522 @item line
12523 is a decimal integer identifying the line number containing
12524 the reference to the entity (or entities) to be located.
12526 @item column
12527 is a decimal integer identifying the exact location on the
12528 line of the first character of the identifier for the
12529 entity reference. Columns are numbered from 1.
12531 @item file1 file2 ...
12532 The search will be restricted to these files. If none are given, then
12533 the search will be done for every library file in the search path.
12534 These file must appear only after the pattern or sourcefile.
12536 These file names are considered to be regular expressions, so for instance
12537 specifying 'source*.ADB' is the same as giving every file in the current
12538 directory whose name starts with 'source' and whose extension is 'adb'.
12540 Not that if you specify at least one file in this part, @code{GNAT FIND} may
12541 sometimes not be able to find the body of the subprograms...
12543 @end table
12545 At least one of 'sourcefile' or 'pattern' has to be present on
12546 the command line.
12548 The following qualifiers are available:
12549 @table @code
12551 @item /ALL_FILES
12552 If this qualifier is present, @code{GNAT FIND} and @code{GNAT XREF} will parse
12553 the read-only files found in the library search path. Otherwise, these files
12554 will be ignored. This option can be used to protect Gnat sources or your own
12555 libraries from being parsed, thus making @code{GNAT FIND} and @code{GNAT XREF}
12556 much faster, and their output much smaller.
12558 @item /SOURCE_SEARCH=direc
12559 When looking for source files also look in directory DIR. The order in which
12560 source file search is undertaken is the same as for @file{GNAT MAKE}.
12562 @item /OBJECT_SEARCH=direc
12563 When searching for library and object files, look in directory
12564 DIR. The order in which library files are searched is the same as for
12565 @file{GNAT MAKE}.
12567 @item /NOSTD_INCLUDES
12568 Do not look for sources in the system default directory.
12570 @item /NOSTD_LIBRARIES
12571 Do not look for library files in the system default directory.
12573 @item /RUNTIME_SYSTEM=@var{rts-path}
12574 @cindex @code{/RUNTIME_SYSTEM} (@code{GNAT FIND})
12575 Specifies the default location of the runtime library. Same meaning as the
12576 equivalent @code{GNAT MAKE} flag (see @ref{Qualifiers for GNAT MAKE}).
12578 @item -d
12579 If this qualifier is set, then @code{GNAT FIND} will output the parent type
12580 reference for each matching derived types.
12582 @item /EXPRESSIONS
12583 By default, @code{GNAT FIND} accept the simple regular expression set for
12584 @samp{pattern}. If this qualifier is set, then the pattern will be
12585 considered as full Unix-style regular expression.
12587 @item /FULL_PATHNAME
12588 If this qualifier is set, the output file names will be preceded by their
12589 directory (if the file was found in the search path). If this qualifier is
12590 not set, the directory will not be printed.
12592 @item /IGNORE_LOCALS
12593 If this qualifier is set, information is output only for library-level
12594 entities, ignoring local entities. The use of this qualifier may accelerate
12595 @code{GNAT FIND} and @code{GNAT XREF}.
12597 @item /SEARCH=direc
12598 Equivalent to @samp{/OBJECT_SEARCH=direc /SOURCE_SEARCH=direc}.
12600 @item /PROJECT=file
12601 Specify a project file (@pxref{Project Files}) to use.
12602 By default, @code{GNAT XREF} and @code{GNAT FIND} will try to locate a
12603 project file in the current directory.
12605 If a project file is either specified or found by the tools, then the content
12606 of the source directory and object directory lines are added as if they
12607 had been specified respectively by @samp{/SOURCE_SEARCH} and
12608 @samp{/OBJECT_SEARCH}.
12610 @item /REFERENCES
12611 By default, @code{GNAT FIND} will output only the information about the
12612 declaration, body or type completion of the entities. If this qualifier is
12613 set, the @code{GNAT FIND} will locate every reference to the entities in
12614 the files specified on the command line (or in every file in the search
12615 path if no file is given on the command line).
12617 @item /PRINT_LINES
12618 If this qualifier is set, then @code{GNAT FIND} will output the content
12619 of the Ada source file lines were the entity was found.
12621 @item -t
12622 If this qualifier is set, then @code{GNAT FIND} will output the type hierarchy for
12623 the specified type. It act like -d option but recursively from parent
12624 type to parent type. When this qualifier is set it is not possible to
12625 specify more than one file.
12627 @end table
12629 All these qualifiers may be in any order on the command line, and may even
12630 appear after the file names. They need not be separated by spaces, thus
12631 you can say @samp{GNAT XREF /ALL_FILES/IGNORE_LOCALS} instead of
12632 @samp{GNAT XREF /ALL_FILES /IGNORE_LOCALS}.
12634 As stated previously, GNAT FIND will search in every directory in the
12635 search path. You can force it to look only in the current directory if
12636 you specify @code{*} at the end of the command line.
12639 @node Project Files for GNAT XREF and GNAT FIND
12640 @section Project Files for @command{GNAT XREF} and @command{GNAT FIND}
12642 @noindent
12643 Project files allow a programmer to specify how to compile its
12644 application, where to find sources,... These files are used primarily by
12645 the Glide Ada mode, but they can also be used by the two tools
12646 @code{GNAT XREF} and @code{GNAT FIND}.
12648 A project file name must end with @file{.adp}. If a single one is
12649 present in the current directory, then @code{GNAT XREF} and @code{GNAT FIND} will
12650 extract the information from it. If multiple project files are found, none of
12651 them is read, and you have to use the @samp{-p} qualifier to specify the one
12652 you want to use.
12654 The following lines can be included, even though most of them have default
12655 values which can be used in most cases.
12656 The lines can be entered in any order in the file.
12657 Except for @samp{src_dir} and @samp{obj_dir}, you can only have one instance of
12658 each line. If you have multiple instances, only the last one is taken into
12659 account.
12661 @table @code
12662 @item src_dir=DIR         [default: "[]"]
12663 specifies a directory where to look for source files. Multiple src_dir lines
12664 can be specified and they will be searched in the order they
12665 are specified.
12667 @item obj_dir=DIR         [default: "[]"]
12668 specifies a directory where to look for object and library files. Multiple
12669 obj_dir lines can be specified and they will be searched in the order they
12670 are specified
12672 @item comp_opt=SWITCHES   [default: ""]
12673 creates a variable which can be referred to subsequently by using
12674 the @samp{$@{comp_opt@}} notation. This is intended to store the default
12675 qualifiers given to @file{GNAT MAKE} and @file{GNAT COMPILE}.
12677 @item bind_opt=SWITCHES   [default: ""]
12678 creates a variable which can be referred to subsequently by using
12679 the @samp{$@{bind_opt@}} notation. This is intended to store the default
12680 qualifiers given to @file{GNAT BIND}.
12682 @item link_opt=SWITCHES   [default: ""]
12683 creates a variable which can be referred to subsequently by using
12684 the @samp{$@{link_opt@}} notation. This is intended to store the default
12685 qualifiers given to @file{GNAT LINK}.
12687 @item main=EXECUTABLE     [default: ""]
12688 specifies the name of the executable for the application. This variable can
12689 be referred to in the following lines by using the @samp{$@{main@}} notation.
12691 @item comp_cmd=COMMAND    [default: "GNAT COMPILE /SEARCH=$@{src_dir@} /DEBUG /TRY_SEMANTICS"]
12692 specifies the command used to compile a single file in the application.
12694 @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@}"]
12695 specifies the command used to recompile the whole application.
12697 @item run_cmd=COMMAND     [default: "$@{main@}"]
12698 specifies the command used to run the application.
12700 @item debug_cmd=COMMAND   [default: "GDB $@{main@}"]
12701 specifies the command used to debug the application
12703 @end table
12705 @code{GNAT XREF} and @code{GNAT FIND} only take into account the @samp{src_dir}
12706 and @samp{obj_dir} lines, and ignore the others.
12708 @node Regular Expressions in GNAT FIND and GNAT XREF
12709 @section  Regular Expressions in @code{GNAT FIND} and @code{GNAT XREF}
12711 @noindent
12712 As specified in the section about @code{GNAT FIND}, the pattern can be a
12713 regular expression. Actually, there are to set of regular expressions
12714 which are recognized by the program :
12716 @table @code
12717 @item globbing patterns
12718 These are the most usual regular expression. They are the same that you
12719 generally used in a Unix shell command line, or in a DOS session.
12721 Here is a more formal grammar :
12722 @smallexample
12723 @group
12724 @iftex
12725 @leftskip=.5cm
12726 @end iftex
12727 regexp ::= term
12728 term   ::= elmt            -- matches elmt
12729 term   ::= elmt elmt       -- concatenation (elmt then elmt)
12730 term   ::= *               -- any string of 0 or more characters
12731 term   ::= ?               -- matches any character
12732 term   ::= [char @{char@}] -- matches any character listed
12733 term   ::= [char - char]   -- matches any character in range
12734 @end group
12735 @end smallexample
12737 @item full regular expression
12738 The second set of regular expressions is much more powerful. This is the
12739 type of regular expressions recognized by utilities such a @file{grep}.
12741 The following is the form of a regular expression, expressed in Ada
12742 reference manual style BNF is as follows
12744 @smallexample
12745 @iftex
12746 @leftskip=.5cm
12747 @end iftex
12748 @group
12749 regexp ::= term @{| term@} -- alternation (term or term ...)
12751 term ::= item @{item@}     -- concatenation (item then item)
12753 item ::= elmt              -- match elmt
12754 item ::= elmt *            -- zero or more elmt's
12755 item ::= elmt +            -- one or more elmt's
12756 item ::= elmt ?            -- matches elmt or nothing
12757 @end group
12758 @group
12759 elmt ::= nschar            -- matches given character
12760 elmt ::= [nschar @{nschar@}]   -- matches any character listed
12761 elmt ::= [^ nschar @{nschar@}] -- matches any character not listed
12762 elmt ::= [char - char]     -- matches chars in given range
12763 elmt ::= \ char            -- matches given character
12764 elmt ::= .                 -- matches any single character
12765 elmt ::= ( regexp )        -- parens used for grouping
12767 char ::= any character, including special characters
12768 nschar ::= any character except ()[].*+?^
12769 @end group
12770 @end smallexample
12772 Following are a few examples :
12774 @table @samp
12775 @item abcde|fghi
12776 will match any of the two strings 'abcde' and 'fghi'.
12778 @item abc*d
12779 will match any string like 'abd', 'abcd', 'abccd', 'abcccd', and so on
12781 @item [a-z]+
12782 will match any string which has only lowercase characters in it (and at
12783 least one character
12785 @end table
12786 @end table
12788 @node Examples of GNAT XREF Usage
12789 @section Examples of @code{GNAT XREF} Usage
12791 @subsection General Usage
12793 @noindent
12794 For the following examples, we will consider the following units :
12796 @smallexample
12797 @group
12798 @cartouche
12799 MAIN.ADS:
12800 1: @b{with} Bar;
12801 2: @b{package} Main @b{is}
12802 3:     @b{procedure} Foo (B : @b{in} Integer);
12803 4:     C : Integer;
12804 5: @b{private}
12805 6:     D : Integer;
12806 7: @b{end} Main;
12808 MAIN.ADB:
12809 1: @b{package body} Main @b{is}
12810 2:     @b{procedure} Foo (B : @b{in} Integer) @b{is}
12811 3:     @b{begin}
12812 4:        C := B;
12813 5:        D := B;
12814 6:        Bar.Print (B);
12815 7:        Bar.Print (C);
12816 8:     @b{end} Foo;
12817 9: @b{end} Main;
12819 BAR.ADS:
12820 1: @b{package} Bar @b{is}
12821 2:     @b{procedure} Print (B : Integer);
12822 3: @b{end} bar;
12823 @end cartouche
12824 @end group
12825 @end smallexample
12827 @table @code
12829 @noindent
12830 The first thing to do is to recompile your application (for instance, in
12831 that case just by doing a @samp{GNAT MAKE main}, so that GNAT generates
12832 the cross-referencing information.
12833 You can then issue any of the following commands:
12835 @item GNAT XREF MAIN.ADB
12836 @code{GNAT XREF} generates cross-reference information for MAIN.ADB
12837 and every unit 'with'ed by MAIN.ADB.
12839 The output would be:
12840 @smallexample
12841 @iftex
12842 @leftskip=0cm
12843 @end iftex
12844 B                                                      Type: Integer
12845   Decl: BAR.ADS           2:22
12846 B                                                      Type: Integer
12847   Decl: MAIN.ADS          3:20
12848   Body: MAIN.ADB          2:20
12849   Ref:  MAIN.ADB          4:13     5:13     6:19
12850 Bar                                                    Type: Unit
12851   Decl: BAR.ADS           1:9
12852   Ref:  MAIN.ADB          6:8      7:8
12853        MAIN.ADS           1:6
12854 C                                                      Type: Integer
12855   Decl: MAIN.ADS          4:5
12856   Modi: MAIN.ADB          4:8
12857   Ref:  MAIN.ADB          7:19
12858 D                                                      Type: Integer
12859   Decl: MAIN.ADS          6:5
12860   Modi: MAIN.ADB          5:8
12861 Foo                                                    Type: Unit
12862   Decl: MAIN.ADS          3:15
12863   Body: MAIN.ADB          2:15
12864 Main                                                    Type: Unit
12865   Decl: MAIN.ADS          2:9
12866   Body: MAIN.ADB          1:14
12867 Print                                                   Type: Unit
12868   Decl: BAR.ADS           2:15
12869   Ref:  MAIN.ADB          6:12     7:12
12870 @end smallexample
12872 @noindent
12873 that is the entity @code{Main} is declared in MAIN.ADS, line 2, column 9,
12874 its body is in MAIN.ADB, line 1, column 14 and is not referenced any where.
12876 The entity @code{Print} is declared in BAR.ADS, line 2, column 15 and it
12877 it referenced in MAIN.ADB, line 6 column 12 and line 7 column 12.
12879 @item GNAT XREF PACKAGE1.ADB PACKAGE2.ADS
12880 @code{GNAT XREF} will generates cross-reference information for
12881 PACKAGE1.ADB, PACKAGE2.ADS and any other package 'with'ed by any
12882 of these.
12884 @end table
12887 @node Examples of GNAT FIND Usage
12888 @section Examples of @code{GNAT FIND} Usage
12890 @table @code
12892 @item GNAT FIND /FULL_PATHNAME xyz:MAIN.ADB
12893 Find declarations for all entities xyz referenced at least once in
12894 MAIN.ADB. The references are search in every library file in the search
12895 path.
12897 The directories will be printed as well (as the @samp{/FULL_PATHNAME}
12898 qualifier is set)
12900 The output will look like:
12901 @smallexample
12902 [directory]MAIN.ADS:106:14: xyz <= declaration
12903 [directory]MAIN.ADB:24:10: xyz <= body
12904 [directory]FOO.ADS:45:23: xyz <= declaration
12905 @end smallexample
12907 @noindent
12908 that is to say, one of the entities xyz found in MAIN.ADB is declared at
12909 line 12 of MAIN.ADS (and its body is in MAIN.ADB), and another one is
12910 declared at line 45 of FOO.ADS
12912 @item GNAT FIND /FULL_PATHNAME/SOURCE_LINE xyz:MAIN.ADB
12913 This is the same command as the previous one, instead @code{GNAT FIND} will
12914 display the content of the Ada source file lines.
12916 The output will look like:
12918 @smallexample
12919 [directory]MAIN.ADS:106:14: xyz <= declaration
12920    procedure xyz;
12921 [directory]MAIN.ADB:24:10: xyz <= body
12922    procedure xyz is
12923 [directory]FOO.ADS:45:23: xyz <= declaration
12924    xyz : Integer;
12925 @end smallexample
12927 @noindent
12928 This can make it easier to find exactly the location your are looking
12929 for.
12931 @item GNAT FIND /REFERENCES "*x*":MAIN.ADS:123 FOO.ADB
12932 Find references to all entities containing an x that are
12933 referenced on line 123 of MAIN.ADS.
12934 The references will be searched only in MAIN.ADB and FOO.ADB.
12936 @item GNAT FIND MAIN.ADS:123
12937 Find declarations and bodies for all entities that are referenced on
12938 line 123 of MAIN.ADS.
12940 This is the same as @code{GNAT FIND "*":MAIN.ADB:123}.
12942 @item GNAT FIND [mydir]MAIN.ADB:123:45
12943 Find the declaration for the entity referenced at column 45 in
12944 line 123 of file MAIN.ADB in directory mydir. Note that it
12945 is usual to omit the identifier name when the column is given,
12946 since the column position identifies a unique reference.
12948 The column has to be the beginning of the identifier, and should not
12949 point to any character in the middle of the identifier.
12951 @end table
12953 @node File Name Krunching Using GNAT KRUNCH
12954 @chapter File Name Krunching Using @code{GNAT KRUNCH}
12955 @findex GNAT KRUNCH
12957 @noindent
12958 This chapter discusses the method used by the compiler to shorten
12959 the default file names chosen for Ada units so that they do not
12960 exceed the maximum length permitted. It also describes the
12961 @code{GNAT KRUNCH} utility that can be used to determine the result of
12962 applying this shortening.
12963 @menu
12964 * About GNAT KRUNCH::
12965 * Using GNAT KRUNCH::
12966 * Krunching Method::
12967 * Examples of GNAT KRUNCH Usage::
12968 @end menu
12970 @node About GNAT KRUNCH
12971 @section About @code{GNAT KRUNCH}
12973 @noindent
12974 The default file naming rule in GNAT
12975 is that the file name must be derived from
12976 the unit name. The exact default rule is as follows:
12977 @itemize @bullet
12978 @item
12979 Take the unit name and replace all dots by hyphens.
12980 @item
12981 If such a replacement occurs in the
12982 second character position of a name, and the first character is
12983 A, G, S, or I then replace the dot by the character
12984 $ (dollar sign)
12985 instead of a minus.
12986 @end itemize
12987 The reason for this exception is to avoid clashes
12988 with the standard names for children of System, Ada, Interfaces,
12989 and GNAT, which use the prefixes S- A- I- and G-
12990 respectively.
12992 The @code{/FILE_NAME_MAX_LENGTH=@var{nn}}
12993 qualifier of the compiler activates a "krunching"
12994 circuit that limits file names to nn characters (where nn is a decimal
12995 integer). For example, using OpenVMS,
12996 where the maximum file name length is
12997 39, the value of nn is usually set to 39, but if you want to generate
12998 a set of files that would be usable if ported to a system with some
12999 different maximum file length, then a different value can be specified.
13000 The default value of 39 for OpenVMS need not be specified.
13002 The @code{GNAT KRUNCH} utility can be used to determine the krunched name for
13003 a given file, when krunched to a specified maximum length.
13005 @node Using GNAT KRUNCH
13006 @section Using @code{GNAT KRUNCH}
13008 @noindent
13009 The @code{GNAT KRUNCH} command has the form
13012 @smallexample
13013 $ GNAT KRUNCH @var{name} /COUNT=nn
13014 @end smallexample
13016 @noindent
13017 @var{name} can be an Ada name with dots or the GNAT name of the unit,
13018 where the dots representing child units or subunit are replaced by
13019 hyphens. The only confusion arises if a name ends in @code{.ADS} or
13020 @code{.ADB}. @code{GNAT KRUNCH} takes this to be an extension if there are
13021 no other dots in the name.
13023 @var{length} represents the length of the krunched name. The default
13024 when no argument is given is 39 characters. A length of zero stands for
13025 unlimited, in other words do not chop except for system files which are
13026 always 39.
13028 @noindent
13029 The output is the krunched name. The output has an extension only if the
13030 original argument was a file name with an extension.
13032 @node Krunching Method
13033 @section Krunching Method
13035 @noindent
13036 The initial file name is determined by the name of the unit that the file
13037 contains. The name is formed by taking the full expanded name of the
13038 unit and replacing the separating dots with hyphens and
13039 using uppercase
13040 for all letters, except that a hyphen in the second character position is
13041 replaced by a dollar sign if the first character is
13042 A, I, G, or S.
13043 The extension is @code{.ADS} for a
13044 specification and @code{.ADB} for a body.
13045 Krunching does not affect the extension, but the file name is shortened to
13046 the specified length by following these rules:
13048 @itemize @bullet
13049 @item
13050 The name is divided into segments separated by hyphens, tildes or
13051 underscores and all hyphens, tildes, and underscores are
13052 eliminated. If this leaves the name short enough, we are done.
13054 @item
13055 If the name is too long, the longest segment is located (left-most if there are two
13056 of equal length), and shortened by dropping its last character. This is
13057 repeated until the name is short enough.
13059 As an example, consider the krunching of @*@file{OUR-STRINGS-WIDE_FIXED.ADB}
13060 to fit the name into 8 characters as required by some operating systems.
13062 @smallexample
13063 our-strings-wide_fixed 22
13064 our strings wide fixed 19
13065 our string  wide fixed 18
13066 our strin   wide fixed 17
13067 our stri    wide fixed 16
13068 our stri    wide fixe  15
13069 our str     wide fixe  14
13070 our str     wid  fixe  13
13071 our str     wid  fix   12
13072 ou  str     wid  fix   11
13073 ou  st      wid  fix   10
13074 ou  st      wi   fix   9
13075 ou  st      wi   fi    8
13076 Final file name: OUSTWIFI.ADB
13077 @end smallexample
13079 @item
13080 The file names for all predefined units are always krunched to eight
13081 characters. The krunching of these predefined units uses the following
13082 special prefix replacements:
13084 @table @file
13085 @item ada-
13086 replaced by @file{A-}
13088 @item gnat-
13089 replaced by @file{G-}
13091 @item interfaces-
13092 replaced by @file{I-}
13094 @item system-
13095 replaced by @file{S-}
13096 @end table
13098 These system files have a hyphen in the second character position. That
13099 is why normal user files replace such a character with a
13100 dollar sign, to
13101 avoid confusion with system file names.
13103 As an example of this special rule, consider
13104 @*@file{ADA-STRINGS-WIDE_FIXED.ADB}, which gets krunched as follows:
13106 @smallexample
13107 ada-strings-wide_fixed 22
13108 a-  strings wide fixed 18
13109 a-  string  wide fixed 17
13110 a-  strin   wide fixed 16
13111 a-  stri    wide fixed 15
13112 a-  stri    wide fixe  14
13113 a-  str     wide fixe  13
13114 a-  str     wid  fixe  12
13115 a-  str     wid  fix   11
13116 a-  st      wid  fix   10
13117 a-  st      wi   fix   9
13118 a-  st      wi   fi    8
13119 Final file name: A-STWIFI.ADB
13120 @end smallexample
13121 @end itemize
13123 Of course no file shortening algorithm can guarantee uniqueness over all
13124 possible unit names, and if file name krunching is used then it is your
13125 responsibility to ensure that no name clashes occur. The utility
13126 program @code{GNAT KRUNCH} is supplied for conveniently determining the
13127 krunched name of a file.
13129 @node Examples of GNAT KRUNCH Usage
13130 @section Examples of @code{GNAT KRUNCH} Usage
13132 @smallexample
13133 @iftex
13134 @leftskip=0cm
13135 @end iftex
13136 $ GNAT KRUNCH VERY_LONG_UNIT_NAME.ADS/count=6 --> VLUNNA.ADS
13137 $ GNAT KRUNCH VERY_LONG_UNIT_NAME.ADS/count=0 --> VERY_LONG_UNIT_NAME.ADS
13138 @end smallexample
13140 @node Preprocessing Using GNAT PREPROCESS
13141 @chapter Preprocessing Using @code{GNAT PREPROCESS}
13142 @findex GNAT PREPROCESS
13144 @noindent
13145 The @code{GNAT PREPROCESS} utility provides
13146 a simple preprocessing capability for Ada programs.
13147 It is designed for use with GNAT, but is not dependent on any special
13148 features of GNAT.
13150 @menu
13151 * Using GNAT PREPROCESS::
13152 * Qualifiers for GNAT PREPROCESS::
13153 * Form of Definitions File::
13154 * Form of Input Text for GNAT PREPROCESS::
13155 @end menu
13157 @node Using GNAT PREPROCESS
13158 @section Using @code{GNAT PREPROCESS}
13160 @noindent
13161 To call @code{GNAT PREPROCESS} use
13163 @smallexample
13164 $ GNAT PREPROCESS [-bcrsu] [-Dsymbol=value] infile outfile [deffile]
13165 @end smallexample
13167 @noindent
13168 where
13169 @table @code
13170 @item infile
13171 is the full name of the input file, which is an Ada source
13172 file containing preprocessor directives.
13174 @item outfile
13175 is the full name of the output file, which is an Ada source
13176 in standard Ada form. When used with GNAT, this file name will
13177 normally have an ads or adb suffix.
13179 @item deffile
13180 is the full name of a text file containing definitions of
13181 symbols to be referenced by the preprocessor. This argument is
13182 optional, and can be replaced by the use of the @code{-D} qualifier.
13184 @item qualifiers
13185 is an optional sequence of qualifiers as described in the next section.
13186 @end table
13188 @node Qualifiers for GNAT PREPROCESS
13189 @section Qualifiers for @code{GNAT PREPROCESS}
13191 @table @code
13193 @item /BLANK_LINES
13194 Causes both preprocessor lines and the lines deleted by
13195 preprocessing to be replaced by blank lines in the output source file,
13196 preserving line numbers in the output file.
13198 @item /COMMENTS
13199 Causes both preprocessor lines and the lines deleted
13200 by preprocessing to be retained in the output source as comments marked
13201 with the special string "--! ". This option will result in line numbers
13202 being preserved in the output file.
13204 @item -Dsymbol=value
13205 Defines a new symbol, associated with value. If no value is given on the
13206 command line, then symbol is considered to be @code{True}. This qualifier
13207 can be used in place of a definition file.
13209 @item /REMOVE (default)
13210 This is the default setting which causes lines deleted by preprocessing
13211 to be entirely removed from the output file.
13213 @item /REFERENCE
13214 Causes a @code{Source_Reference} pragma to be generated that
13215 references the original input file, so that error messages will use
13216 the file name of this original file. The use of this qualifier implies
13217 that preprocessor lines are not to be removed from the file, so its
13218 use will force @code{/BLANK_LINES} mode if
13219 @code{/COMMENTS}
13220 has not been specified explicitly.
13222 Note that if the file to be preprocessed contains multiple units, then
13223 it will be necessary to @code{GNAT CHOP} the output file from
13224 @code{GNAT PREPROCESS}. If a @code{Source_Reference} pragma is present
13225 in the preprocessed file, it will be respected by
13226 @code{GNAT CHOP /REFERENCE}
13227 so that the final chopped files will correctly refer to the original
13228 input source file for @code{GNAT PREPROCESS}.
13230 @item /SYMBOLS
13231 Causes a sorted list of symbol names and values to be
13232 listed on the standard output file.
13234 @item /UNDEFINED
13235 Causes undefined symbols to be treated as having the value FALSE in the context
13236 of a preprocessor test. In the absence of this option, an undefined symbol in
13237 a @code{#if} or @code{#elsif} test will be treated as an error.
13239 @end table
13242 @node Form of Definitions File
13243 @section Form of Definitions File
13245 @noindent
13246 The definitions file contains lines of the form
13248 @smallexample
13249 symbol := value
13250 @end smallexample
13252 @noindent
13253 where symbol is an identifier, following normal Ada (case-insensitive)
13254 rules for its syntax, and value is one of the following:
13256 @itemize @bullet
13257 @item
13258 Empty, corresponding to a null substitution
13259 @item
13260 A string literal using normal Ada syntax
13261 @item
13262 Any sequence of characters from the set
13263 (letters, digits, period, underline).
13264 @end itemize
13266 @noindent
13267 Comment lines may also appear in the definitions file, starting with
13268 the usual @code{--},
13269 and comments may be added to the definitions lines.
13271 @node Form of Input Text for GNAT PREPROCESS
13272 @section Form of Input Text for @code{GNAT PREPROCESS}
13274 @noindent
13275 The input text may contain preprocessor conditional inclusion lines,
13276 as well as general symbol substitution sequences.
13278 The preprocessor conditional inclusion commands have the form
13280 @smallexample
13281 @group
13282 @cartouche
13283 #if @i{expression} [then]
13284    lines
13285 #elsif @i{expression} [then]
13286    lines
13287 #elsif @i{expression} [then]
13288    lines
13290 #else
13291    lines
13292 #end if;
13293 @end cartouche
13294 @end group
13295 @end smallexample
13297 @noindent
13298 In this example, @i{expression} is defined by the following grammar:
13299 @smallexample
13300 @i{expression} ::=  <symbol>
13301 @i{expression} ::=  <symbol> = "<value>"
13302 @i{expression} ::=  <symbol> = <symbol>
13303 @i{expression} ::=  <symbol> 'Defined
13304 @i{expression} ::=  not @i{expression}
13305 @i{expression} ::=  @i{expression} and @i{expression}
13306 @i{expression} ::=  @i{expression} or @i{expression}
13307 @i{expression} ::=  @i{expression} and then @i{expression}
13308 @i{expression} ::=  @i{expression} or else @i{expression}
13309 @i{expression} ::=  ( @i{expression} )
13310 @end smallexample
13312 @noindent
13313 For the first test (@i{expression} ::= <symbol>) the symbol must have
13314 either the value true or false, that is to say the right-hand of the
13315 symbol definition must be one of the (case-insensitive) literals
13316 @code{True} or @code{False}. If the value is true, then the
13317 corresponding lines are included, and if the value is false, they are
13318 excluded.
13320 The test (@i{expression} ::= <symbol> @code{'Defined}) is true only if
13321 the symbol has been defined in the definition file or by a @code{-D}
13322 qualifier on the command line. Otherwise, the test is false.
13324 The equality tests are case insensitive, as are all the preprocessor lines.
13326 If the symbol referenced is not defined in the symbol definitions file,
13327 then the effect depends on whether or not qualifier @code{-u}
13328 is specified. If so, then the symbol is treated as if it had the value
13329 false and the test fails. If this qualifier is not specified, then
13330 it is an error to reference an undefined symbol. It is also an error to
13331 reference a symbol that is defined with a value other than @code{True}
13332 or @code{False}.
13334 The use of the @code{not} operator inverts the sense of this logical test, so
13335 that the lines are included only if the symbol is not defined.
13336 The @code{then} keyword is optional as shown
13338 The @code{#} must be the first non-blank character on a line, but
13339 otherwise the format is free form. Spaces or tabs may appear between
13340 the @code{#} and the keyword. The keywords and the symbols are case
13341 insensitive as in normal Ada code. Comments may be used on a
13342 preprocessor line, but other than that, no other tokens may appear on a
13343 preprocessor line. Any number of @code{elsif} clauses can be present,
13344 including none at all. The @code{else} is optional, as in Ada.
13346 The @code{#} marking the start of a preprocessor line must be the first
13347 non-blank character on the line, i.e. it must be preceded only by
13348 spaces or horizontal tabs.
13350 Symbol substitution outside of preprocessor lines is obtained by using
13351 the sequence
13353 @smallexample
13354 $symbol
13355 @end smallexample
13357 @noindent
13358 anywhere within a source line, except in a comment or within a
13359 string literal. The identifier
13360 following the @code{$} must match one of the symbols defined in the symbol
13361 definition file, and the result is to substitute the value of the
13362 symbol in place of @code{$symbol} in the output file.
13364 Note that although the substitution of strings within a string literal
13365 is not possible, it is possible to have a symbol whose defined value is
13366 a string literal. So instead of setting XYZ to @code{hello} and writing:
13368 @smallexample
13369 Header : String := "$XYZ";
13370 @end smallexample
13372 @noindent
13373 you should set XYZ to @code{"hello"} and write:
13375 @smallexample
13376 Header : String := $XYZ;
13377 @end smallexample
13379 @noindent
13380 and then the substitution will occur as desired.
13382 @node The GNAT Run-Time Library Builder GNAT LIBRARY
13383 @chapter The GNAT Run-Time Library Builder @code{GNAT LIBRARY}
13384 @findex GNAT LIBRARY
13385 @cindex Library builder
13387 @noindent
13388 @code{GNAT LIBRARY} is a tool for rebuilding the GNAT run time with user
13389 supplied configuration pragmas.
13391 @menu
13392 * Running GNAT LIBRARY::
13393 * Qualifiers for GNAT LIBRARY::
13394 * Examples of GNAT LIBRARY Usage::
13395 @end menu
13397 @node Running GNAT LIBRARY
13398 @section Running @code{GNAT LIBRARY}
13400 @noindent
13401 The @code{GNAT LIBRARY} command has the form
13403 @smallexample
13404 $ GNAT LIBRARY /[CREATE | SET | DELETE]=directory [/CONFIG=file]
13405 @end smallexample
13407 @node Qualifiers for GNAT LIBRARY
13408 @section Qualifiers for @code{GNAT LIBRARY}
13410 @noindent
13411 @code{GNAT LIBRARY} recognizes the following qualifiers:
13413 @table @code
13414 @item /CREATE=directory
13415 @cindex @code{/CREATE=directory} (@code{GNAT LIBRARY})
13416      Create the new run-time library in the specified directory.
13418 @item /SET=directory
13419 @cindex @code{/SET=directory} (@code{GNAT LIBRARY})
13420      Make the library in the specified directory the current run-time
13421      library.
13423 @item /DELETE=directory
13424 @cindex @code{/DELETE=directory} (@code{GNAT LIBRARY})
13425      Delete the run-time library in the specified directory.
13427 @item /CONFIG=file
13428 @cindex @code{/CONFIG=file} (@code{GNAT LIBRARY})
13429      With /CREATE:
13430      Use the configuration pragmas in the specified file when building
13431      the library.
13433      With /SET:
13434      Use the configuration pragmas in the specified file when compiling.
13436 @end table
13438 @node Examples of GNAT LIBRARY Usage
13439 @section Example of @code{GNAT LIBRARY} Usage
13441 @smallexample
13442 Contents of VAXFLOAT.ADC:
13443 pragma Float_Representation (VAX_Float);
13445 $ GNAT LIBRARY /CREATE=[.VAXFLOAT] /CONFIG=VAXFLOAT.ADC
13447 GNAT LIBRARY rebuilds the run-time library in directory [.VAXFLOAT]
13449 @end smallexample
13451 @node The GNAT Library Browser GNAT LIST
13452 @chapter The GNAT Library Browser @code{GNAT LIST}
13453 @findex GNAT LIST
13454 @cindex Library browser
13456 @noindent
13457 @code{GNAT LIST} is a tool that outputs information about compiled
13458 units. It gives the relationship between objects, unit names and source
13459 files. It can also be used to check the source dependencies of a unit
13460 as well as various characteristics.
13462 @menu
13463 * Running GNAT LIST::
13464 * Qualifiers for GNAT LIST::
13465 * Examples of GNAT LIST Usage::
13466 @end menu
13468 @node Running GNAT LIST
13469 @section Running @code{GNAT LIST}
13471 @noindent
13472 The @code{GNAT LIST} command has the form
13474 @smallexample
13475 $ GNAT LIST qualifiers @var{object_or_ali_file}
13476 @end smallexample
13478 @noindent
13479 The main argument is the list of object or @file{ali} files
13480 (@pxref{The Ada Library Information Files})
13481 for which information is requested.
13483 In normal mode, without additional option, @code{GNAT LIST} produces a
13484 four-column listing. Each line represents information for a specific
13485 object. The first column gives the full path of the object, the second
13486 column gives the name of the principal unit in this object, the third
13487 column gives the status of the source and the fourth column gives the
13488 full path of the source representing this unit.
13489 Here is a simple example of use:
13491 @smallexample
13492 $ GNAT LIST *.OBJ
13493 []DEMO1.OBJ            demo1            DIF DEMO1.ADB
13494 []DEMO2.OBJ            demo2             OK DEMO2.ADB
13495 []HELLO.OBJ            h1                OK HELLO.ADB
13496 []INSTR-CHILD.OBJ      instr.child      MOK INSTR-CHILD.ADB
13497 []INSTR.OBJ            instr             OK INSTR.ADB
13498 []TEF.OBJ              tef              DIF TEF.ADB
13499 []TEXT_IO_EXAMPLE.OBJ  text_io_example   OK TEXT_IO_EXAMPLE.ADB
13500 []TGEF.OBJ             tgef             DIF TGEF.ADB
13501 @end smallexample
13503 @noindent
13504 The first line can be interpreted as follows: the main unit which is
13505 contained in
13506 object file @file{DEMO1.OBJ} is demo1, whose main source is in
13507 @file{DEMO1.ADB}. Furthermore, the version of the source used for the
13508 compilation of demo1 has been modified (DIF). Each source file has a status
13509 qualifier which can be:
13511 @table @code
13512 @item OK (unchanged)
13513 The version of the source file used for the compilation of the
13514 specified unit corresponds exactly to the actual source file.
13516 @item MOK (slightly modified)
13517 The version of the source file used for the compilation of the
13518 specified unit differs from the actual source file but not enough to
13519 require recompilation. If you use GNAT MAKE with the qualifier
13520 @code{/MINIMAL_RECOMPILATION}, a file marked
13521 MOK will not be recompiled.
13523 @item DIF (modified)
13524 No version of the source found on the path corresponds to the source
13525 used to build this object.
13527 @item ??? (file not found)
13528 No source file was found for this unit.
13530 @item HID (hidden,  unchanged version not first on PATH)
13531 The version of the source that corresponds exactly to the source used
13532 for compilation has been found on the path but it is hidden by another
13533 version of the same source that has been modified.
13535 @end table
13537 @node Qualifiers for GNAT LIST
13538 @section Qualifiers for @code{GNAT LIST}
13540 @noindent
13541 @code{GNAT LIST} recognizes the following qualifiers:
13543 @table @code
13544 @item /ALL_UNITS
13545 @cindex @code{/ALL_UNITS} (@code{GNAT LIST})
13546 Consider all units, including those of the predefined Ada library.
13547 Especially useful with @code{/DEPENDENCIES}.
13549 @item /DEPENDENCIES
13550 @cindex @code{/DEPENDENCIES} (@code{GNAT LIST})
13551 List sources from which specified units depend on.
13553 @item /OUTPUT=OPTIONS
13554 @cindex @code{/OUTPUT=OPTIONS} (@code{GNAT LIST})
13555 Output the list of options.
13557 @item /OUTPUT=OBJECTS
13558 @cindex @code{/OUTPUT=OBJECTS} (@code{GNAT LIST})
13559 Only output information about object files.
13561 @item /OUTPUT=SOURCES
13562 @cindex @code{/OUTPUT=SOURCES} (@code{GNAT LIST})
13563 Only output information about source files.
13565 @item /OUTPUT=UNITS
13566 @cindex @code{/OUTPUT=UNITS} (@code{GNAT LIST})
13567 Only output information about compilation units.
13569 @item /OBJECT_SEARCH=@var{dir}
13570 @itemx /SOURCE_SEARCH=@var{dir}
13571 @itemx /SEARCH=@var{dir}
13572 @itemx  /NOCURRENT_DIRECTORY
13573 @itemx /NOSTD_INCLUDES
13574 Source path manipulation. Same meaning as the equivalent @code{GNAT MAKE} flags
13575 (see @ref{Qualifiers for GNAT MAKE}).
13577 @item /RUNTIME_SYSTEM=@var{rts-path}
13578 @cindex @code{/RUNTIME_SYSTEM} (@code{GNAT LIST})
13579 Specifies the default location of the runtime library. Same meaning as the
13580 equivalent @code{GNAT MAKE} flag (see @ref{Qualifiers for GNAT MAKE}).
13582 @item /OUTPUT=VERBOSE
13583 @cindex @code{/OUTPUT=VERBOSE} (@code{GNAT LIST})
13584 Verbose mode. Output the complete source and object paths. Do not use
13585 the default column layout but instead use long format giving as much as
13586 information possible on each requested units, including special
13587 characteristics such as:
13589 @table @code
13590 @item  Preelaborable
13591 The unit is preelaborable in the Ada 95 sense.
13593 @item No_Elab_Code
13594 No elaboration code has been produced by the compiler for this unit.
13596 @item Pure
13597 The unit is pure in the Ada 95 sense.
13599 @item Elaborate_Body
13600 The unit contains a pragma Elaborate_Body.
13602 @item Remote_Types
13603 The unit contains a pragma Remote_Types.
13605 @item Shared_Passive
13606 The unit contains a pragma Shared_Passive.
13608 @item Predefined
13609 This unit is part of the predefined environment and cannot be modified
13610 by the user.
13612 @item Remote_Call_Interface
13613 The unit contains a pragma Remote_Call_Interface.
13615 @end table
13617 @end table
13619 @node Examples of GNAT LIST Usage
13620 @section Example of @code{GNAT LIST} Usage
13622 @smallexample
13623 GNAT LIST /DEPENDENCIES /OUTPUT=SOURCES /ALL_UNITS DEMO1.ADB
13625 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]ADA.ADS
13626 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]A-FINALI.ADS
13627 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]A-FILICO.ADS
13628 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]A-STREAM.ADS
13629 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]A-TAGS.ADS
13630 DEMO1.ADB
13631 GEN_LIST.ADS
13632 GEN_LIST.ADB
13633 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]GNAT.ADS
13634 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]G-IO.ADS
13635 INSTR.ADS
13636 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]SYSTEM.ADS
13637 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-EXCTAB.ADS
13638 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-FINIMP.ADS
13639 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-FINROO.ADS
13640 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-SECSTA.ADS
13641 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-STALIB.ADS
13642 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-STOELE.ADS
13643 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-STRATT.ADS
13644 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-TASOLI.ADS
13645 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]S-UNSTYP.ADS
13646 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]UNCHCONV.ADS
13647 @end smallexample
13650 @node Finding Memory Problems with GNAT Debug Pool
13651 @chapter Finding Memory Problems with GNAT Debug Pool
13652 @findex Debug Pool
13653 @cindex storage, pool, memory corruption
13655 @noindent
13656 The use of unchecked deallocation and unchecked conversion can easily
13657 lead to incorrect memory references. The problems generated by such
13658 references are usually difficult to tackle because the symptoms can be
13659 very remote from the origin of the problem. In such cases, it is
13660 very helpful to detect the problem as early as possible. This is the
13661 purpose of the Storage Pool provided by @code{GNAT.Debug_Pools}.
13663 @noindent
13664 In order to use the GNAT specific debugging pool, the user must
13665 associate a debug pool object with each of the access types that may be
13666 related to suspected memory problems. See Ada Reference Manual
13667 13.11.
13668 @smallexample
13669 @b{type} Ptr @b{is} @b{access} Some_Type;
13670 Pool : GNAT.Debug_Pools.Debug_Pool;
13671 @b{for} Ptr'Storage_Pool @b{use} Pool;
13672 @end smallexample
13674 @code{GNAT.Debug_Pools} is derived from of a GNAT-specific kind of
13675 pool: the Checked_Pool. Such pools, like standard Ada storage pools,
13676 allow the user to redefine allocation and deallocation strategies. They
13677 also provide a checkpoint for each dereference, through the use of
13678 the primitive operation @code{Dereference} which is implicitly called at
13679 each dereference of an access value.
13681 Once an access type has been associated with a debug pool, operations on
13682 values of the type may raise four distinct exceptions,
13683 which correspond to four potential kinds of memory corruption:
13684 @itemize @bullet
13685 @item
13686 @code{GNAT.Debug_Pools.Accessing_Not_Allocated_Storage}
13687 @item
13688 @code{GNAT.Debug_Pools.Accessing_Deallocated_Storage}
13689 @item
13690 @code{GNAT.Debug_Pools.Freeing_Not_Allocated_Storage}
13691 @item
13692 @code{GNAT.Debug_Pools.Freeing_Deallocated_Storage }
13693 @end itemize
13695 @noindent
13696 For types associated with a Debug_Pool, dynamic allocation is performed using
13697 the standard
13698 GNAT allocation routine. References to all allocated chunks of memory
13699 are kept in an internal dictionary. The deallocation strategy consists
13700 in not releasing the memory to the underlying system but rather to fill
13701 it with a memory pattern easily recognizable during debugging sessions:
13702 The memory pattern is the old IBM hexadecimal convention: 16#DEADBEEF#.
13703 Upon each dereference, a check is made that the access value denotes a properly
13704 allocated memory location. Here is a complete example of use of
13705 @code{Debug_Pools}, that includes typical instances of  memory corruption:
13706 @smallexample
13707 @iftex
13708 @leftskip=0cm
13709 @end iftex
13710 @b{with} Gnat.Io; @b{use} Gnat.Io;
13711 @b{with} Unchecked_Deallocation;
13712 @b{with} Unchecked_Conversion;
13713 @b{with} GNAT.Debug_Pools;
13714 @b{with} System.Storage_Elements;
13715 @b{with} Ada.Exceptions; @b{use} Ada.Exceptions;
13716 @b{procedure} Debug_Pool_Test @b{is}
13718    @b{type} T @b{is} @b{access} Integer;
13719    @b{type} U @b{is} @b{access} @b{all} T;
13721    P : GNAT.Debug_Pools.Debug_Pool;
13722    @b{for} T'Storage_Pool @b{use} P;
13724    @b{procedure} Free @b{is} @b{new} Unchecked_Deallocation (Integer, T);
13725    @b{function} UC @b{is} @b{new} Unchecked_Conversion (U, T);
13726    A, B : @b{aliased} T;
13728    @b{procedure} Info @b{is} @b{new} GNAT.Debug_Pools.Print_Info(Put_Line);
13730 @b{begin}
13731    Info (P);
13732    A := @b{new} Integer;
13733    B := @b{new} Integer;
13734    B := A;
13735    Info (P);
13736    Free (A);
13737    @b{begin}
13738       Put_Line (Integer'Image(B.@b{all}));
13739    @b{exception}
13740       @b{when} E : @b{others} => Put_Line ("raised: " & Exception_Name (E));
13741    @b{end};
13742    @b{begin}
13743       Free (B);
13744    @b{exception}
13745       @b{when} E : @b{others} => Put_Line ("raised: " & Exception_Name (E));
13746    @b{end};
13747    B := UC(A'Access);
13748    @b{begin}
13749       Put_Line (Integer'Image(B.@b{all}));
13750    @b{exception}
13751       @b{when} E : @b{others} => Put_Line ("raised: " & Exception_Name (E));
13752    @b{end};
13753    @b{begin}
13754       Free (B);
13755    @b{exception}
13756       @b{when} E : @b{others} => Put_Line ("raised: " & Exception_Name (E));
13757    @b{end};
13758    Info (P);
13759 @b{end} Debug_Pool_Test;
13760 @end smallexample
13761 @noindent
13762 The debug pool mechanism provides the following precise diagnostics on the
13763 execution of this erroneous program:
13764 @smallexample
13765 Debug Pool info:
13766   Total allocated bytes :  0
13767   Total deallocated bytes :  0
13768   Current Water Mark:  0
13769   High Water Mark:  0
13771 Debug Pool info:
13772   Total allocated bytes :  8
13773   Total deallocated bytes :  0
13774   Current Water Mark:  8
13775   High Water Mark:  8
13777 raised: GNAT.DEBUG_POOLS.ACCESSING_DEALLOCATED_STORAGE
13778 raised: GNAT.DEBUG_POOLS.FREEING_DEALLOCATED_STORAGE
13779 raised: GNAT.DEBUG_POOLS.ACCESSING_NOT_ALLOCATED_STORAGE
13780 raised: GNAT.DEBUG_POOLS.FREEING_NOT_ALLOCATED_STORAGE
13781 Debug Pool info:
13782   Total allocated bytes :  8
13783   Total deallocated bytes :  4
13784   Current Water Mark:  4
13785   High Water Mark:  8
13787 @end smallexample
13789 @node Creating Sample Bodies Using GNAT STUB
13790 @chapter Creating Sample Bodies Using @code{GNAT STUB}
13791 @findex GNAT STUB
13793 @noindent
13794 @code{GNAT STUB} creates body stubs, that is, empty but compilable bodies
13795 for library unit declarations.
13797 To create a body stub, @code{GNAT STUB} has to compile the library
13798 unit declaration. Therefore, bodies can be created only for legal
13799 library units. Moreover, if a library unit depends semantically upon
13800 units located outside the current directory, you have to provide
13801 the source search path when calling @code{GNAT STUB}, see the description
13802 of @code{GNAT STUB} qualifiers below.
13804 @menu
13805 * Running GNAT STUB::
13806 * Qualifiers for GNAT STUB::
13807 @end menu
13809 @node Running GNAT STUB
13810 @section Running @code{GNAT STUB}
13812 @noindent
13813 @code{GNAT STUB} has the command-line interface of the form
13815 @smallexample
13816 $ GNAT STUB [qualifiers] filename [directory]
13817 @end smallexample
13819 @noindent
13820 where
13821 @table @code
13822 @item filename
13823 is the name of the source file that contains a library unit declaration
13824 for which a body must be created. This name should follow the GNAT file name
13825 conventions. No crunching is allowed for this file name. The file
13826 name may contain the path information.
13828 @item directory
13829 indicates the directory to place a body stub (default is the
13830 current directory)
13832 @item qualifiers
13833 is an optional sequence of qualifiers as described in the next section
13834 @end table
13836 @node Qualifiers for GNAT STUB
13837 @section Qualifiers for @code{GNAT STUB}
13839 @table @code
13841 @item /FULL
13842 If the destination directory already contains a file with a name of the body file
13843 for the argument spec file, replace it with the generated body stub.
13845 @item /HEADER=SPEC
13846 Put the comment header (i.e. all the comments preceding the
13847 compilation unit) from the source of the library unit declaration
13848 into the body stub.
13850 @item /HEADER=GENERAL
13851 Put a sample comment header into the body stub.
13853 @item /SEARCH=direc
13854 @itemx /NOCURRENT_DIRECTORY
13855 These qualifiers have the same meaning as in calls to GNAT COMPILE.
13856 They define the source search path in the call to GNAT COMPILE issued
13857 by @code{GNAT STUB} to compile an argument source file.
13859 @item /INDENTATION=@var{n}
13860 (@var{n} is a decimal natural number). Set the indentation level in the
13861 generated body sample to n, '/INDENTATION=0' means "no indentation",
13862 the default indentation is 3.
13864 @item /TREE_FILE=SAVE
13865 Do not remove the tree file (i.e. the snapshot of the compiler internal
13866 structures used by @code{GNAT STUB}) after creating the body stub.
13868 @item /LINE_LENGTH=@var{n}
13869 (@var{n} is a decimal positive number) Set the maximum line length in the
13870 body stub to n, the default is 78.
13872 @item /QUIET
13873 Quiet mode: do not generate a confirmation when a body is
13874 successfully created or a message when a body is not required for an
13875 argument unit.
13877 @item /TREE_FILE=REUSE
13878 Reuse the tree file (if it exists) instead of creating it: instead of
13879 creating the tree file for the library unit declaration, GNAT STUB
13880 tries to find it in the current directory and use it for creating
13881 a body. If the tree file is not found, no body is created. @code{/REUSE}
13882 also implies @code{/SAVE}, whether or not
13883 @code{/SAVE} is set explicitly.
13885 @item /TREE_FILE=OVERWRITE
13886 Overwrite the existing tree file: if the current directory already
13887 contains the file which, according to the GNAT file name rules should
13888 be considered as a tree file for the argument source file, GNAT STUB
13889 will refuse to create the tree file needed to create a body sampler,
13890 unless @code{-t} option is set
13892 @item /VERBOSE
13893 Verbose mode: generate version information.
13895 @end table
13897 @node Reducing the Size of Ada Executables with GNAT ELIM
13898 @chapter Reducing the Size of Ada Executables with @code{GNAT ELIM}
13899 @findex GNAT ELIM
13901 @menu
13902 * About GNAT ELIM::
13903 * Eliminate Pragma::
13904 * Tree Files::
13905 * Preparing Tree and Bind Files for GNAT ELIM::
13906 * Running GNAT ELIM::
13907 * Correcting the List of Eliminate Pragmas::
13908 * Making Your Executables Smaller::
13909 * Summary of the GNAT ELIM Usage Cycle::
13910 @end menu
13912 @node About GNAT ELIM
13913 @section About @code{GNAT ELIM}
13915 @noindent
13916 When a program shares a set of Ada
13917 packages with other programs, it may happen that this program uses
13918 only a fraction of the subprograms defined in these packages. The code
13919 created for these unused subprograms increases the size of the executable.
13921 @code{GNAT ELIM} tracks unused subprograms in an Ada program and
13922 outputs a list of GNAT-specific @code{Eliminate} pragmas (see next
13923 section) marking all the subprograms that are declared but never called.
13924 By placing the list of @code{Eliminate} pragmas in the GNAT configuration
13925 file @file{GNAT.ADC} and recompiling your program, you may decrease the
13926 size of its executable, because the compiler will not generate the code
13927 for 'eliminated' subprograms.
13929 @code{GNAT ELIM} needs as its input data a set of tree files
13930 (see @ref{Tree Files}) representing all the components of a program to
13931 process and a bind file for a main subprogram (see
13932 @ref{Preparing Tree and Bind Files for GNAT ELIM}).
13934 @node Eliminate Pragma
13935 @section @code{Eliminate} Pragma
13936 @findex Eliminate
13938 @noindent
13939 The simplified syntax of the Eliminate pragma used by @code{GNAT ELIM} is:
13941 @smallexample
13942 @cartouche
13943 @b{pragma} Eliminate (Library_Unit_Name, Subprogram_Name);
13944 @end cartouche
13945 @end smallexample
13947 @noindent
13948 where
13949 @table @code
13950 @item Library_Unit_Name
13951 full expanded Ada name of a library unit
13953 @item Subprogram_Name
13954 a simple or expanded name of a subprogram declared within this
13955 compilation unit
13957 @end table
13959 @noindent
13960 The effect of an @code{Eliminate} pragma placed in the GNAT configuration
13961 file @file{GNAT.ADC} is:
13963 @itemize @bullet
13965 @item
13966 If the subprogram @code{Subprogram_Name} is declared within
13967 the library unit @code{Library_Unit_Name}, the compiler will not generate
13968 code for this subprogram. This applies to all overloaded subprograms denoted
13969 by @code{Subprogram_Name}.
13971 @item
13972 If a subprogram marked by the pragma @code{Eliminate} is used (called)
13973 in a program, the compiler will produce an error message in the place where
13974 it is called.
13975 @end itemize
13977 @node Tree Files
13978 @section Tree Files
13979 @cindex Tree file
13981 @noindent
13982 A tree file stores a snapshot of the compiler internal data
13983 structures at the very end of a successful compilation. It contains all the
13984 syntactic and semantic information for the compiled unit and all the
13985 units upon which it depends semantically.
13986 To use tools that make use of tree files, you
13987 need to first produce the right set of tree files.
13989 GNAT produces correct tree files when /TREE_OUTPUT /NOLOAD options are set
13990 in a GNAT COMPILE call. The tree files have an .adt extension.
13991 Therefore, to produce a tree file for the compilation unit contained in a file
13992 named @file{FOO.ADB}, you must use the command
13994 @smallexample
13995 $ GNAT COMPILE /NOLOAD /TREE_OUTPUT FOO.ADB
13996 @end smallexample
13998 @noindent
13999 and you will get the tree file @file{foo.adt}.
14000 compilation.
14002 @node Preparing Tree and Bind Files for GNAT ELIM
14003 @section Preparing Tree and Bind Files for @code{GNAT ELIM}
14005 @noindent
14006 A set of tree files covering the program to be analyzed with
14007 @code{GNAT ELIM} and
14008 the bind file for the main subprogram does not have to
14009 be in the current directory.
14010 '-T' GNAT ELIM option may be used to provide
14011 the search path for tree files, and '-b'
14012 option may be used to point to the bind
14013 file to process (see @ref{Running GNAT ELIM})
14015 If you do not have the appropriate set of tree
14016 files and the right bind file, you
14017 may create them in the current directory using the following procedure.
14019 Let @code{Main_Prog} be the name of a main subprogram, and suppose
14020 this subprogram is in a file named @file{MAIN_PROG.ADB}.
14022 To create a bind file for @code{GNAT ELIM}, run @code{GNAT BIND} for
14023 the main subprogram. @code{GNAT ELIM} can work with both Ada and C
14024 bind files; when both are present, it uses the Ada bind file.
14025 The following commands will build the program and create the bind file:
14027 @smallexample
14028 $ GNAT MAKE /ACTIONS=COMPILE MAIN_PROG
14029 $ GNAT BIND main_prog
14030 @end smallexample
14032 @noindent
14033 To create a minimal set of tree files covering the whole program, call
14034 @code{GNAT MAKE} for this program as follows:
14036 @smallexample
14037 $ GNAT MAKE /FORCE_COMPILE /ACTIONS=COMPILE /NOLOAD /TREE_OUTPUT MAIN_PROG
14038 @end smallexample
14040 @noindent
14041 The @code{/ACTIONS=COMPILE} GNAT MAKE option turns off the bind and link
14042 steps, that are useless anyway because the sources are compiled with
14043 @option{/NOLOAD} option which turns off code generation.
14045 The @code{/FORCE_COMPILE} GNAT MAKE option forces
14046 recompilation of all the needed sources.
14048 This sequence of actions will create all the data needed by @code{GNAT ELIM}
14049 from scratch and therefore guarantee its consistency. If you would like to
14050 use some existing set of files as @code{GNAT ELIM} output, you must make
14051 sure that the set of files is complete and consistent. You can use the
14052 @code{-m} qualifier to check if there are missed tree files
14054 Note, that @code{GNAT ELIM} needs neither object nor ALI files.
14056 @node Running GNAT ELIM
14057 @section Running @code{GNAT ELIM}
14059 @noindent
14060 @code{GNAT ELIM} has the following command-line interface:
14062 @smallexample
14063 $ GNAT ELIM [options] name
14064 @end smallexample
14066 @noindent
14067 @code{name} should be a full expanded Ada name of a main subprogram
14068 of a program (partition).
14070 @code{GNAT ELIM} options:
14072 @table @code
14073 @item /QUIET
14074 Quiet mode: by default @code{GNAT ELIM} generates to the standard error
14075 stream a trace of the source file names of the compilation units being
14076 processed. This option turns this trace off.
14078 @item /VERBOSE
14079 Verbose mode: @code{GNAT ELIM} version information is printed as Ada
14080 comments to the standard output stream.
14082 @item /ALL
14083 Also look for subprograms from the GNAT run time that can be eliminated.
14085 @item /MISSED
14086 Check if any tree files are missing for an accurate result.
14088 @item /TREE_DIRS=@var{dir}
14089 When looking for tree files also look in directory @var{dir}
14091 @item /BIND_FILE=@var{bind_file}
14092 Specifies @var{bind_file} as the bind file to process. If not set, the name
14093 of the bind file is computed from the full expanded Ada name of a main subprogram.
14095 @item -d@var{x}
14096 Activate internal debugging qualifiers. @var{x} is a letter or digit, or
14097 string of letters or digits, which specifies the type of debugging
14098 mode desired.  Normally these are used only for internal development
14099 or system debugging purposes. You can find full documentation for these
14100 qualifiers in the body of the @code{GNAT ELIM.Options} unit in the compiler
14101 source file @file{GNATELIM-OPTIONS.ADB}.
14102 @end table
14104 @noindent
14105 @code{GNAT ELIM} sends its output to the standard output stream, and all the
14106 tracing and debug information is sent to the standard error stream.
14107 In order to produce a proper GNAT configuration file
14108 @file{GNAT.ADC}, redirection must be used:
14110 @smallexample
14111 $ PIPE GNAT ELIM MAIN_PROG > GNAT.ADC
14112 @end smallexample
14115 @noindent
14116 In order to append the @code{GNAT ELIM} output to the existing contents of
14117 @file{GNAT.ADC}.
14119 @node Correcting the List of Eliminate Pragmas
14120 @section Correcting the List of Eliminate Pragmas
14122 @noindent
14123 In some rare cases it may happen that @code{GNAT ELIM} will try to eliminate
14124 subprograms which are actually called in the program. In this case, the
14125 compiler will generate an error message of the form:
14127 @smallexample
14128 FILE.ADB:106:07: cannot call eliminated subprogram "My_Prog"
14129 @end smallexample
14131 @noindent
14132 You will need to manually remove the wrong @code{Eliminate} pragmas from
14133 the @file{GNAT.ADC} file. It is advised that you recompile your program
14134 from scratch after that because you need a consistent @file{GNAT.ADC} file
14135 during the entire compilation.
14137 @node Making Your Executables Smaller
14138 @section Making Your Executables Smaller
14140 @noindent
14141 In order to get a smaller executable for your program you now have to
14142 recompile the program completely with the new @file{GNAT.ADC} file
14143 created by @code{GNAT ELIM} in your current directory:
14145 @smallexample
14146 $ GNAT MAKE /FORCE_COMPILE MAIN_PROG
14147 @end smallexample
14149 @noindent
14150 (you will need @code{/FORCE_COMPILE} option for GNAT MAKE to
14151 recompile everything
14152 with the set of pragmas @code{Eliminate} you have obtained with
14153 @code{GNAT ELIM}).
14155 Be aware that the set of @code{Eliminate} pragmas is specific to each
14156 program. It is not recommended to merge sets of @code{Eliminate}
14157 pragmas created for different programs in one @file{GNAT.ADC} file.
14159 @node Summary of the GNAT ELIM Usage Cycle
14160 @section Summary of the GNAT ELIM Usage Cycle
14162 @noindent
14163 Here is a quick summary of the steps to be taken in order to reduce
14164 the size of your executables with @code{GNAT ELIM}. You may use
14165 other GNAT options to control the optimization level,
14166 to produce the debugging information, to set search path, etc.
14168 @enumerate
14169 @item
14170 Produce a bind file and a set of tree files
14172 @smallexample
14173 $ GNAT MAKE /ACTIONS=COMPILE MAIN_PROG
14174 $ GNAT BIND main_prog
14175 $ GNAT MAKE /FORCE_COMPILE /NO_LINK /NOLOAD /TREE_OUTPUT MAIN_PROG
14176 @end smallexample
14178 @item
14179 Generate a list of @code{Eliminate} pragmas
14180 @smallexample
14181 $ PIPE GNAT ELIM MAIN_PROG > GNAT.ADC
14182 @end smallexample
14184 @item
14185 Recompile the application
14187 @smallexample
14188 $ GNAT MAKE /FORCE_COMPILE MAIN_PROG
14189 @end smallexample
14191 @end enumerate
14193 @node Other Utility Programs
14194 @chapter Other Utility Programs
14196 @noindent
14197 This chapter discusses some other utility programs available in the Ada
14198 environment.
14200 @menu
14201 * Using Other Utility Programs with GNAT::
14202 * The GNAT STANDARD Utility Program::
14203 * The External Symbol Naming Scheme of GNAT::
14204 * Ada Mode for Glide::
14205 * Converting Ada Files to html with gnathtml::
14206 * Installing gnathtml::
14207 * LSE::
14208 * Profiling::
14209 @end menu
14211 @node Using Other Utility Programs with GNAT
14212 @section Using Other Utility Programs with GNAT
14214 @noindent
14215 The object files generated by GNAT are in standard system format and in
14216 particular the debugging information uses this format. This means
14217 programs generated by GNAT can be used with existing utilities that
14218 depend on these formats.
14221 @node The GNAT STANDARD Utility Program
14222 @section The @code{GNAT STANDARD} Utility Program
14224 @noindent
14225 Many of the definitions in package Standard are implementation-dependent.
14226 However, the source of this package does not exist as an Ada source
14227 file, so these values cannot be determined by inspecting the source.
14228 They can be determined by examining in detail the coding of
14229 @file{CSTAND.ADB} which creates the image of Standard in the compiler,
14230 but this is awkward and requires a great deal of internal knowledge
14231 about the system.
14233 The @code{GNAT STANDARD} utility is designed to deal with this situation.
14234 It is an Ada program that dynamically determines the
14235 values of all the relevant parameters in Standard, and prints them
14236 out in the form of an Ada source listing for Standard, displaying all
14237 the values of interest. This output is generated to
14238 @file{SYS$OUTPUT}.
14240 To determine the value of any parameter in package Standard, simply
14241 run @code{GNAT STANDARD} with no qualifiers or arguments, and examine
14242 the output. This is preferable to consulting documentation, because
14243 you know that the values you are getting are the actual ones provided
14244 by the executing system.
14246 @node The External Symbol Naming Scheme of GNAT
14247 @section The External Symbol Naming Scheme of GNAT
14249 @noindent
14250 In order to interpret the output from GNAT, when using tools that are
14251 originally intended for use with other languages, it is useful to
14252 understand the conventions used to generate link names from the Ada
14253 entity names.
14255 All link names are in all lowercase letters. With the exception of library
14256 procedure names, the mechanism used is simply to use the full expanded
14257 Ada name with dots replaced by double underscores. For example, suppose
14258 we have the following package spec:
14260 @smallexample
14261 @group
14262 @cartouche
14263 @b{package} QRS @b{is}
14264    MN : Integer;
14265 @b{end} QRS;
14266 @end cartouche
14267 @end group
14268 @end smallexample
14270 @noindent
14271 The variable @code{MN} has a full expanded Ada name of @code{QRS.MN}, so
14272 the corresponding link name is @code{qrs__mn}.
14273 @findex Export
14274 Of course if a @code{pragma Export} is used this may be overridden:
14276 @smallexample
14277 @group
14278 @cartouche
14279 @b{package} Exports @b{is}
14280    Var1 : Integer;
14281    @b{pragma} Export (Var1, C, External_Name => "var1_name");
14282    Var2 : Integer;
14283    @b{pragma} Export (Var2, C, Link_Name => "var2_link_name");
14284 @b{end} Exports;
14285 @end cartouche
14286 @end group
14287 @end smallexample
14289 @noindent
14290 In this case, the link name for @var{Var1} is whatever link name the
14291 C compiler would assign for the C function @var{var1_name}. This typically
14292 would be either @var{var1_name} or @var{_var1_name}, depending on operating
14293 system conventions, but other possibilities exist. The link name for
14294 @var{Var2} is @var{var2_link_name}, and this is not operating system
14295 dependent.
14297 @findex _main
14298 One exception occurs for library level procedures. A potential ambiguity
14299 arises between the required name @code{_main} for the C main program,
14300 and the name we would otherwise assign to an Ada library level procedure
14301 called @code{Main} (which might well not be the main program).
14303 To avoid this ambiguity, we attach the prefix @code{_ada_} to such
14304 names. So if we have a library level procedure such as
14306 @smallexample
14307 @group
14308 @cartouche
14309 @b{procedure} Hello (S : String);
14310 @end cartouche
14311 @end group
14312 @end smallexample
14314 @noindent
14315 the external name of this procedure will be @var{_ada_hello}.
14317 @node Ada Mode for Glide
14318 @section Ada Mode for @code{Glide}
14320 @noindent
14321 The Glide mode for programming in Ada (both, Ada83 and Ada95) helps the
14322 user in understanding existing code and facilitates writing new code. It
14323 furthermore provides some utility functions for easier integration of
14324 standard EMACS features when programming in Ada.
14326 @subsection General Features:
14328 @itemize @bullet
14329 @item
14330 Full Integrated Development Environment :
14332 @itemize @bullet
14333 @item
14334 support of 'project files' for the configuration (directories,
14335 compilation options,...)
14337 @item
14338 compiling and stepping through error messages.
14340 @item
14341 running and debugging your applications within Glide.
14342 @end itemize
14344 @item
14345 easy to use for beginners by pull-down menus,
14347 @item
14348 user configurable by many user-option variables.
14349 @end itemize
14351 @subsection Ada Mode Features That Help Understanding Code:
14353 @itemize @bullet
14354 @item
14355 functions for easy and quick stepping through Ada code,
14357 @item
14358 getting cross reference information for identifiers (e.g. find the
14359 defining place by a keystroke),
14361 @item
14362 displaying an index menu of types and subprograms and move point to
14363 the chosen one,
14365 @item
14366 automatic color highlighting of the various entities in Ada code.
14367 @end itemize
14369 @subsection Glide Support for Writing Ada Code:
14371 @itemize @bullet
14372 @item
14373 switching between spec and body files with possible
14374 autogeneration of body files,
14376 @item
14377 automatic formating of subprograms parameter lists.
14379 @item
14380 automatic smart indentation according to Ada syntax,
14382 @item
14383 automatic completion of identifiers,
14385 @item
14386 automatic casing of identifiers, keywords, and attributes,
14388 @item
14389 insertion of statement templates,
14391 @item
14392 filling comment paragraphs like filling normal text,
14393 @end itemize
14395 For more information, please refer to the online Glide documentation
14396 available in the Glide --> Help Menu.
14398 @node Converting Ada Files to html with gnathtml
14399 @section Converting Ada Files to html with @code{gnathtml}
14401 @noindent
14402 This @code{Perl} script allows Ada source files to be browsed using
14403 standard Web browsers. For installation procedure, see the section
14404 @xref{Installing gnathtml}.
14406 Ada reserved keywords are highlighted in a bold font and Ada comments in
14407 a blue font. Unless your program was compiled with the GNAT COMPILE @option{/XREF=SUPPRESS}
14408 qualifier to suppress the generation of cross-referencing information, user
14409 defined variables and types will appear in a different color; you will
14410 be able to click on any identifier and go to its declaration.
14412 The command line is as follow:
14413 @smallexample
14414 $ perl gnathtml.pl [qualifiers] ada-files
14415 @end smallexample
14417 You can pass it as many Ada files as you want. @code{gnathtml} will generate
14418 an html file for every ada file, and a global file called @file{index.htm}.
14419 This file is an index of every identifier defined in the files.
14421 The available qualifiers are the following ones :
14423 @table @code
14424 @item -83
14425 @cindex @code{-83} (@code{gnathtml})
14426 Only the subset on the Ada 83 keywords will be highlighted, not the full
14427 Ada 95 keywords set.
14429 @item -cc @var{color}
14430 This option allows you to change the color used for comments. The default
14431 value is green. The color argument can be any name accepted by html.
14433 @item -d
14434 @cindex @code{-d} (@code{gnathtml})
14435 If the ada files depend on some other files (using for instance the
14436 @code{with} command, the latter will also be converted to html.
14437 Only the files in the user project will be converted to html, not the files
14438 in the run-time library itself.
14440 @item -D
14441 This command is the same as -d above, but @code{gnathtml} will also look
14442 for files in the run-time library, and generate html files for them.
14444 @item -f
14445 @cindex @code{-f} (@code{gnathtml})
14446 By default, gnathtml will generate html links only for global entities
14447 ('with'ed units, global variables and types,...). If you specify the
14448 @code{-f} on the command line, then links will be generated for local
14449 entities too.
14451 @item -l @var{number}
14452 @cindex @code{-l} (@code{gnathtml})
14453 If this qualifier is provided and @var{number} is not 0, then @code{gnathtml}
14454 will number the html files every @var{number} line.
14456 @item -I @var{dir}
14457 @cindex @code{-I} (@code{gnathtml})
14458 Specify a directory to search for library files (@file{.ALI} files) and
14459 source files. You can provide several -I qualifiers on the command line,
14460 and the directories will be parsed in the order of the command line.
14462 @item -o @var{dir}
14463 @cindex @code{-o} (@code{gnathtml})
14464 Specify the output directory for html files. By default, gnathtml will
14465 saved the generated html files in a subdirectory named @file{html/}.
14467 @item -p @var{file}
14468 @cindex @code{-p} (@code{gnathtml})
14469 If you are using EMACS and the most recent EMACS Ada mode, which provides
14470 a full Integrated Development Environment for compiling, checking,
14471 running and debugging applications, you may be using @file{.adp} files
14472 to give the directories where EMACS can find sources and object files.
14474 Using this qualifier, you can tell gnathtml to use these files. This allows
14475 you to get an html version of your application, even if it is spread
14476 over multiple directories.
14478 @item -sc @var{color}
14479 @cindex @code{-sc} (@code{gnathtml})
14480 This option allows you to change the color used for symbol definitions.
14481 The default value is red. The color argument can be any name accepted by html.
14483 @item -t @var{file}
14484 @cindex @code{-t} (@code{gnathtml})
14485 This qualifier provides the name of a file. This file contains a list of
14486 file names to be converted, and the effect is exactly as though they had
14487 appeared explicitly on the command line. This
14488 is the recommended way to work around the command line length limit on some
14489 systems.
14491 @end table
14493 @node Installing gnathtml
14494 @section Installing @code{gnathtml}
14496 @noindent
14497 @code{Perl} needs to be installed on your machine to run this script.
14498 @code{Perl} is freely available for almost every architecture and
14499 Operating System via the Internet.
14501 On Unix systems, you  may want to modify  the  first line of  the script
14502 @code{gnathtml},  to explicitly  tell  the Operating  system  where Perl
14503 is. The syntax of this line is :
14504 @smallexample
14505 #!full_path_name_to_perl
14506 @end smallexample
14508 @noindent
14509 Alternatively, you may run the script using the following command line:
14511 @smallexample
14512 $ perl gnathtml.pl [qualifiers] files
14513 @end smallexample
14515 @node LSE
14516 @section LSE
14517 @findex LSE
14519 @noindent
14520 The GNAT distribution provides an Ada 95 template for the Digital Language
14521 Sensitive Editor (LSE), a component of DECset. In order to
14522 access it, invoke LSE with the qualifier /ENVIRONMENT=GNU:[LIB]ADA95.ENV.
14524 @node Profiling
14525 @section Profiling
14526 @findex PCA
14528 @noindent
14529 GNAT supports The Digital Performance Coverage Analyzer (PCA), a component
14530 of DECset. To use it proceed as outlined under "HELP PCA", except for running
14531 the collection phase with the /DEBUG qualifier.
14533 @smallexample
14534 $ GNAT MAKE /DEBUG <PROGRAM_NAME>
14535 $ DEFINE LIB$DEBUG PCA$COLLECTOR
14536 $ RUN/DEBUG <PROGRAM_NAME>
14537 @end smallexample
14538 @noindent
14540 @node Running and Debugging Ada Programs
14541 @chapter Running and Debugging Ada Programs
14542 @cindex Debugging
14544 @noindent
14545 This chapter discusses how to debug Ada programs. An incorrect Ada program
14546 may be handled in three ways by the GNAT compiler:
14548 @enumerate
14549 @item
14550 The illegality may be a violation of the static semantics of Ada. In
14551 that case GNAT diagnoses the constructs in the program that are illegal.
14552 It is then a straightforward matter for the user to modify those parts of
14553 the program.
14555 @item
14556 The illegality may be a violation of the dynamic semantics of Ada. In
14557 that case the program compiles and executes, but may generate incorrect
14558 results, or may terminate abnormally with some exception.
14560 @item
14561 When presented with a program that contains convoluted errors, GNAT
14562 itself may terminate abnormally without providing full diagnostics on
14563 the incorrect user program.
14564 @end enumerate
14566 @menu
14567 * The GNAT Debugger GDB::
14568 * Running GDB::
14569 * Introduction to GDB Commands::
14570 * Using Ada Expressions::
14571 * Calling User-Defined Subprograms::
14572 * Using the Next Command in a Function::
14573 * Ada Exceptions::
14574 * Ada Tasks::
14575 * Debugging Generic Units::
14576 * GNAT Abnormal Termination or Failure to Terminate::
14577 * Naming Conventions for GNAT Source Files::
14578 * Getting Internal Debugging Information::
14579 * Stack Traceback::
14580 @end menu
14582 @cindex Debugger
14583 @findex GDB
14585 @node The GNAT Debugger GDB
14586 @section The GNAT Debugger GDB
14588 @noindent
14589 @code{GDB} is a general purpose, platform-independent debugger that
14590 can be used to debug mixed-language programs compiled with @code{GCC},
14591 and in particular is capable of debugging Ada programs compiled with
14592 GNAT. The latest versions of @code{GDB} are Ada-aware and can handle
14593 complex Ada data structures.
14595 The manual @cite{Debugging with GDB}
14596 , located in the GNU:[DOCS] directory,
14597 contains full details on the usage of @code{GDB}, including a section on
14598 its usage on programs. This manual should be consulted for full
14599 details. The section that follows is a brief introduction to the
14600 philosophy and use of @code{GDB}.
14602 When GNAT programs are compiled, the compiler optionally writes debugging
14603 information into the generated object file, including information on
14604 line numbers, and on declared types and variables. This information is
14605 separate from the generated code. It makes the object files considerably
14606 larger, but it does not add to the size of the actual executable that
14607 will be loaded into memory, and has no impact on run-time performance. The
14608 generation of debug information is triggered by the use of the
14609 /DEBUG qualifier in the GNAT COMPILE or GNAT MAKE command used to carry out
14610 the compilations. It is important to emphasize that the use of these
14611 options does not change the generated code.
14613 The debugging information is written in standard system formats that
14614 are used by many tools, including debuggers and profilers. The format
14615 of the information is typically designed to describe C types and
14616 semantics, but GNAT implements a translation scheme which allows full
14617 details about Ada types and variables to be encoded into these
14618 standard C formats. Details of this encoding scheme may be found in
14619 the file EXP_DBUG.ADS in the GNAT source distribution. However, the
14620 details of this encoding are, in general, of no interest to a user,
14621 since @code{GDB} automatically performs the necessary decoding.
14623 When a program is bound and linked, the debugging information is
14624 collected from the object files, and stored in the executable image of
14625 the program. Again, this process significantly increases the size of
14626 the generated executable file, but it does not increase the size of
14627 the executable program itself. Furthermore, if this program is run in
14628 the normal manner, it runs exactly as if the debug information were
14629 not present, and takes no more actual memory.
14631 However, if the program is run under control of @code{GDB}, the
14632 debugger is activated.  The image of the program is loaded, at which
14633 point it is ready to run.  If a run command is given, then the program
14634 will run exactly as it would have if @code{GDB} were not present. This
14635 is a crucial part of the @code{GDB} design philosophy.  @code{GDB} is
14636 entirely non-intrusive until a breakpoint is encountered.  If no
14637 breakpoint is ever hit, the program will run exactly as it would if no
14638 debugger were present. When a breakpoint is hit, @code{GDB} accesses
14639 the debugging information and can respond to user commands to inspect
14640 variables, and more generally to report on the state of execution.
14642 @node Running GDB
14643 @section Running GDB
14645 @noindent
14646 The debugger can be launched directly and simply from @code{glide} or
14647 through its graphical interface: @code{gvd}. It can also be used
14648 directly in text mode. Here is described the basic use of @code{GDB}
14649 in text mode. All the commands described below can be used in the
14650 @code{gvd} console window eventhough there is usually other more
14651 graphical ways to achieve the same goals.
14654 @noindent
14655 The command to run @code{GDB} in text mode is
14657 @smallexample
14658 $ $ GDB PROGRAM
14659 @end smallexample
14661 @noindent
14662 where @code{PROGRAM} is the name of the executable file. This
14663 activates the debugger and results in a prompt for debugger commands.
14664 The simplest command is simply @code{run}, which causes the program to run
14665 exactly as if the debugger were not present. The following section
14666 describes some of the additional commands that can be given to @code{GDB}.
14669 @node Introduction to GDB Commands
14670 @section Introduction to GDB Commands
14672 @noindent
14673 @code{GDB} contains a large repertoire of commands. The manual
14674 @cite{Debugging with GDB}
14675 , located in the GNU:[DOCS] directory,
14676 includes extensive documentation on the use
14677 of these commands, together with examples of their use. Furthermore,
14678 the command @var{help} invoked from within @code{GDB} activates a simple help
14679 facility which summarizes the available commands and their options.
14680 In this section we summarize a few of the most commonly
14681 used commands to give an idea of what @code{GDB} is about. You should create
14682 a simple program with debugging information and experiment with the use of
14683 these @code{GDB} commands on the program as you read through the
14684 following section.
14686 @table @code
14687 @item set args @var{arguments}
14688 The @var{arguments} list above is a list of arguments to be passed to
14689 the program on a subsequent run command, just as though the arguments
14690 had been entered on a normal invocation of the program. The @code{set args}
14691 command is not needed if the program does not require arguments.
14693 @item run
14694 The @code{run} command causes execution of the program to start from
14695 the beginning. If the program is already running, that is to say if
14696 you are currently positioned at a breakpoint, then a prompt will ask
14697 for confirmation that you want to abandon the current execution and
14698 restart.
14700 @item breakpoint @var{location}
14701 The breakpoint command sets a breakpoint, that is to say a point at which
14702 execution will halt and @code{GDB} will await further
14703 commands. @var{location} is
14704 either a line number within a file, given in the format @code{file:linenumber},
14705 or it is the name of a subprogram. If you request that a breakpoint be set on
14706 a subprogram that is overloaded, a prompt will ask you to specify on which of
14707 those subprograms you want to breakpoint. You can also
14708 specify that all of them should be breakpointed. If the program is run
14709 and execution encounters the breakpoint, then the program
14710 stops and @code{GDB} signals that the breakpoint was encountered by
14711 printing the line of code before which the program is halted.
14713 @item breakpoint exception @var{name}
14714 A special form of the breakpoint command which breakpoints whenever
14715 exception @var{name} is raised.
14716 If @var{name} is omitted,
14717 then a breakpoint will occur when any exception is raised.
14719 @item print @var{expression}
14720 This will print the value of the given expression. Most simple
14721 Ada expression formats are properly handled by @code{GDB}, so the expression
14722 can contain function calls, variables, operators, and attribute references.
14724 @item continue
14725 Continues execution following a breakpoint, until the next breakpoint or the
14726 termination of the program.
14728 @item step
14729 Executes a single line after a breakpoint. If the next statement is a subprogram
14730 call, execution continues into (the first statement of) the
14731 called subprogram.
14733 @item next
14734 Executes a single line. If this line is a subprogram call, executes and
14735 returns from the call.
14737 @item list
14738 Lists a few lines around the current source location. In practice, it
14739 is usually more convenient to have a separate edit window open with the
14740 relevant source file displayed. Successive applications of this command
14741 print subsequent lines. The command can be given an argument which is a
14742 line number, in which case it displays a few lines around the specified one.
14744 @item backtrace
14745 Displays a backtrace of the call chain. This command is typically
14746 used after a breakpoint has occurred, to examine the sequence of calls that
14747 leads to the current breakpoint. The display includes one line for each
14748 activation record (frame) corresponding to an active subprogram.
14750 @item up
14751 At a breakpoint, @code{GDB} can display the values of variables local
14752 to the current frame. The command @code{up} can be used to
14753 examine the contents of other active frames, by moving the focus up
14754 the stack, that is to say from callee to caller, one frame at a time.
14756 @item down
14757 Moves the focus of @code{GDB} down from the frame currently being
14758 examined to the frame of its callee (the reverse of the previous command),
14760 @item frame @var{n}
14761 Inspect the frame with the given number. The value 0 denotes the frame
14762 of the current breakpoint, that is to say the top of the call stack.
14764 @end table
14766 The above list is a very short introduction to the commands that
14767 @code{GDB} provides. Important additional capabilities, including conditional
14768 breakpoints, the ability to execute command sequences on a breakpoint,
14769 the ability to debug at the machine instruction level and many other
14770 features are described in detail in @cite{Debugging with GDB}.
14771 Note that most commands can be abbreviated
14772 (for example, c for continue, bt for backtrace).
14774 @node Using Ada Expressions
14775 @section Using Ada Expressions
14776 @cindex Ada expressions
14778 @noindent
14779 @code{GDB} supports a fairly large subset of Ada expression syntax, with some
14780 extensions. The philosophy behind the design of this subset is
14782 @itemize @bullet
14783 @item
14784 That @code{GDB} should provide basic literals and access to operations for
14785 arithmetic, dereferencing, field selection, indexing, and subprogram calls,
14786 leaving more sophisticated computations to subprograms written into the
14787 program (which therefore may be called from @code{GDB}).
14789 @item
14790 That type safety and strict adherence to Ada language restrictions
14791 are not particularly important to the @code{GDB} user.
14793 @item
14794 That brevity is important to the @code{GDB} user.
14795 @end itemize
14797 Thus, for brevity, the debugger acts as if there were
14798 implicit @code{with} and @code{use} clauses in effect for all user-written
14799 packages, thus making it unnecessary to fully qualify most names with
14800 their packages, regardless of context. Where this causes ambiguity,
14801 @code{GDB} asks the user's intent.
14803 For details on the supported Ada syntax, see @cite{Debugging with GDB}.
14805 @node Calling User-Defined Subprograms
14806 @section Calling User-Defined Subprograms
14808 @noindent
14809 An important capability of @code{GDB} is the ability to call user-defined
14810 subprograms while debugging. This is achieved simply by entering
14811 a subprogram call statement in the form:
14813 @smallexample
14814 call subprogram-name (parameters)
14815 @end smallexample
14817 @noindent
14818 The keyword @code{call} can be omitted in the normal case where the
14819 @code{subprogram-name} does not coincide with any of the predefined
14820 @code{GDB} commands.
14822 The effect is to invoke the given subprogram, passing it the
14823 list of parameters that is supplied. The parameters can be expressions and
14824 can include variables from the program being debugged. The
14825 subprogram must be defined
14826 at the library level within your program, and @code{GDB} will call the
14827 subprogram within the environment of your program execution (which
14828 means that the subprogram is free to access or even modify variables
14829 within your program).
14831 The most important use of this facility is in allowing the inclusion of
14832 debugging routines that are tailored to particular data structures
14833 in your program. Such debugging routines can be written to provide a suitably
14834 high-level description of an abstract type, rather than a low-level dump
14835 of its physical layout. After all, the standard
14836 @code{GDB print} command only knows the physical layout of your
14837 types, not their abstract meaning. Debugging routines can provide information
14838 at the desired semantic level and are thus enormously useful.
14840 For example, when debugging GNAT itself, it is crucial to have access to
14841 the contents of the tree nodes used to represent the program internally.
14842 But tree nodes are represented simply by an integer value (which in turn
14843 is an index into a table of nodes).
14844 Using the @code{print} command on a tree node would simply print this integer
14845 value, which is not very useful. But the PN routine (defined in file
14846 TREEPR.ADB in the GNAT sources) takes a tree node as input, and displays
14847 a useful high level representation of the tree node, which includes the
14848 syntactic category of the node, its position in the source, the integers
14849 that denote descendant nodes and parent node, as well as varied
14850 semantic information. To study this example in more detail, you might want to
14851 look at the body of the PN procedure in the stated file.
14853 @node Using the Next Command in a Function
14854 @section Using the Next Command in a Function
14856 @noindent
14857 When you use the @code{next} command in a function, the current source
14858 location will advance to the next statement as usual. A special case
14859 arises in the case of a @code{return} statement.
14861 Part of the code for a return statement is the "epilog" of the function.
14862 This is the code that returns to the caller. There is only one copy of
14863 this epilog code, and it is typically associated with the last return
14864 statement in the function if there is more than one return. In some
14865 implementations, this epilog is associated with the first statement
14866 of the function.
14868 The result is that if you use the @code{next} command from a return
14869 statement that is not the last return statement of the function you
14870 may see a strange apparent jump to the last return statement or to
14871 the start of the function. You should simply ignore this odd jump.
14872 The value returned is always that from the first return statement
14873 that was stepped through.
14875 @node Ada Exceptions
14876 @section Breaking on Ada Exceptions
14877 @cindex Exceptions
14879 @noindent
14880 You can set breakpoints that trip when your program raises
14881 selected exceptions.
14883 @table @code
14884 @item break exception
14885 Set a breakpoint that trips whenever (any task in the) program raises
14886 any exception.
14888 @item break exception @var{name}
14889 Set a breakpoint that trips whenever (any task in the) program raises
14890 the exception @var{name}.
14892 @item break exception unhandled
14893 Set a breakpoint that trips whenever (any task in the) program raises an
14894 exception for which there is no handler.
14896 @item info exceptions
14897 @itemx info exceptions @var{regexp}
14898 The @code{info exceptions} command permits the user to examine all defined
14899 exceptions within Ada programs. With a regular expression, @var{regexp}, as
14900 argument, prints out only those exceptions whose name matches @var{regexp}.
14901 @end table
14903 @node Ada Tasks
14904 @section Ada Tasks
14905 @cindex Tasks
14907 @noindent
14908 @code{GDB} allows the following task-related commands:
14910 @table @code
14911 @item info tasks
14912 This command shows a list of current Ada tasks, as in the following example:
14914 @smallexample
14915 @iftex
14916 @leftskip=0cm
14917 @end iftex
14918 (GDB) info tasks
14919   ID       TID P-ID   Thread Pri State                 Name
14920    1   8088000   0   807e000  15 Child Activation Wait main_task
14921    2   80a4000   1   80ae000  15 Accept/Select Wait    b
14922    3   809a800   1   80a4800  15 Child Activation Wait a
14923 *  4   80ae800   3   80b8000  15 Running               c
14924 @end smallexample
14926 @noindent
14927 In this listing, the asterisk before the first task indicates it to be the
14928 currently running task. The first column lists the task ID that is used
14929 to refer to tasks in the following commands.
14931 @item break @var{linespec} task @var{taskid}
14932 @itemx break @var{linespec} task @var{taskid} if @dots{}
14933 @cindex Breakpoints and tasks
14934 These commands are like the @code{break @dots{} thread @dots{}}.
14935 @var{linespec} specifies source lines.
14937 Use the qualifier @samp{task @var{taskid}} with a breakpoint command
14938 to specify that you only want @code{GDB} to stop the program when a
14939 particular Ada task reaches this breakpoint. @var{taskid} is one of the
14940 numeric task identifiers assigned by @code{GDB}, shown in the first
14941 column of the @samp{info tasks} display.
14943 If you do not specify @samp{task @var{taskid}} when you set a
14944 breakpoint, the breakpoint applies to @emph{all} tasks of your
14945 program.
14947 You can use the @code{task} qualifier on conditional breakpoints as
14948 well; in this case, place @samp{task @var{taskid}} before the
14949 breakpoint condition (before the @code{if}).
14951 @item task @var{taskno}
14952 @cindex Task switching
14954 This command allows to qualifier to the task referred by @var{taskno}. In
14955 particular, This allows to browse the backtrace of the specified
14956 task. It is advised to qualifier back to the original task before
14957 continuing execution otherwise the scheduling of the program may be
14958 perturbated.
14959 @end table
14961 @noindent
14962 For more detailed information on the tasking support, see @cite{Debugging with GDB}.
14964 @node Debugging Generic Units
14965 @section Debugging Generic Units
14966 @cindex Debugging Generic Units
14967 @cindex Generics
14969 @noindent
14970 GNAT always uses code expansion for generic instantiation. This means that
14971 each time an instantiation occurs, a complete copy of the original code is
14972 made, with appropriate substitutions of formals by actuals.
14974 It is not possible to refer to the original generic entities in
14975 @code{GDB}, but it is always possible to debug a particular instance of
14976 a generic, by using the appropriate expanded names. For example, if we have
14978 @smallexample
14979 @group
14980 @cartouche
14981 @b{procedure} g @b{is}
14983    @b{generic package} k @b{is}
14984       @b{procedure} kp (v1 : @b{in out} integer);
14985    @b{end} k;
14987    @b{package body} k @b{is}
14988       @b{procedure} kp (v1 : @b{in out} integer) @b{is}
14989       @b{begin}
14990          v1 := v1 + 1;
14991       @b{end} kp;
14992    @b{end} k;
14994    @b{package} k1 @b{is new} k;
14995    @b{package} k2 @b{is new} k;
14997    var : integer := 1;
14999 @b{begin}
15000    k1.kp (var);
15001    k2.kp (var);
15002    k1.kp (var);
15003    k2.kp (var);
15004 @b{end};
15005 @end cartouche
15006 @end group
15007 @end smallexample
15009 @noindent
15010 Then to break on a call to procedure kp in the k2 instance, simply
15011 use the command:
15013 @smallexample
15014 (GDB) break g.k2.kp
15015 @end smallexample
15017 @noindent
15018 When the breakpoint occurs, you can step through the code of the
15019 instance in the normal manner and examine the values of local variables, as for
15020 other units.
15022 @node GNAT Abnormal Termination or Failure to Terminate
15023 @section GNAT Abnormal Termination or Failure to Terminate
15024 @cindex GNAT Abnormal Termination or Failure to Terminate
15026 @noindent
15027 When presented with programs that contain serious errors in syntax
15028 or semantics,
15029 GNAT may on rare occasions  experience problems in operation, such
15030 as aborting with a
15031 segmentation fault or illegal memory access, raising an internal
15032 exception, terminating abnormally, or failing to terminate at all.
15033 In such cases, you can activate
15034 various features of GNAT that can help you pinpoint the construct in your
15035 program that is the likely source of the problem.
15037 The following strategies are presented in increasing order of
15038 difficulty, corresponding to your experience in using GNAT and your
15039 familiarity with compiler internals.
15041 @enumerate
15042 @item
15043 Run @code{GNAT COMPILE} with the @option{/REPORT_ERRORS=FULL}. This first
15044 qualifier causes all errors on a given line to be reported. In its absence,
15045 only the first error on a line is displayed.
15047 The @option{/REPORT_ERRORS=IMMEDIATE} qualifier causes errors to be displayed as soon as they
15048 are encountered, rather than after compilation is terminated. If GNAT
15049 terminates prematurely or goes into an infinite loop, the last error
15050 message displayed may help to pinpoint the culprit.
15052 @item
15053 Run @code{GNAT COMPILE} with the @code{/VERBOSE} qualifier. In this mode,
15054 @code{GNAT COMPILE} produces ongoing information about the progress of the
15055 compilation and provides the name of each procedure as code is
15056 generated. This qualifier allows you to find which Ada procedure was being
15057 compiled when it encountered a code generation problem.
15059 @item
15060 @cindex @option{/TRACE_UNITS} qualifier
15061 Run @code{GNAT COMPILE} with the @option{/TRACE_UNITS} qualifier. This is a GNAT specific
15062 qualifier that does for the front-end what @code{VERBOSE} does for the back end.
15063 The system prints the name of each unit, either a compilation unit or
15064 nested unit, as it is being analyzed.
15065 @item
15066 Finally, you can start
15067 @code{GDB} directly on the @code{GNAT1} executable. @code{GNAT1} is the
15068 front-end of GNAT, and can be run independently (normally it is just
15069 called from @code{GNAT COMPILE}). You can use @code{GDB} on @code{GNAT1} as you
15070 would on a C program (but @pxref{The GNAT Debugger GDB} for caveats). The
15071 @code{where} command is the first line of attack; the variable
15072 @code{lineno} (seen by @code{print lineno}), used by the second phase of
15073 @code{GNAT1} and by the @code{GNAT COMPILE} backend, indicates the source line at
15074 which the execution stopped, and @code{input_file name} indicates the name of
15075 the source file.
15076 @end enumerate
15078 @node Naming Conventions for GNAT Source Files
15079 @section Naming Conventions for GNAT Source Files
15081 @noindent
15082 In order to examine the workings of the GNAT system, the following
15083 brief description of its organization may be helpful:
15085 @itemize @bullet
15086 @item
15087 Files with prefix @file{SC} contain the lexical scanner.
15089 @item
15090 All files prefixed with @file{PAR} are components of the parser. The
15091 numbers correspond to chapters of the Ada 95 Reference Manual. For example,
15092 parsing of select statements can be found in @file{PAR-CH9.ADB}.
15094 @item
15095 All files prefixed with @file{SEM} perform semantic analysis. The
15096 numbers correspond to chapters of the Ada standard. For example, all
15097 issues involving context clauses can be found in @file{SEM_CH10.ADB}. In
15098 addition, some features of the language require sufficient special processing
15099 to justify their own semantic files: sem_aggr for aggregates, sem_disp for
15100 dynamic dispatching, etc.
15102 @item
15103 All files prefixed with @file{EXP} perform normalization and
15104 expansion of the intermediate representation (abstract syntax tree, or AST).
15105 these files use the same numbering scheme as the parser and semantics files.
15106 For example, the construction of record initialization procedures is done in
15107 @file{EXP_CH3.ADB}.
15109 @item
15110 The files prefixed with @file{BIND} implement the binder, which
15111 verifies the consistency of the compilation, determines an order of
15112 elaboration, and generates the bind file.
15114 @item
15115 The files @file{ATREE.ADS} and @file{ATREE.ADB} detail the low-level
15116 data structures used by the front-end.
15118 @item
15119 The files @file{SINFO.ADS} and @file{SINFO.ADB} detail the structure of
15120 the abstract syntax tree as produced by the parser.
15122 @item
15123 The files @file{EINFO.ADS} and @file{EINFO.ADB} detail the attributes of
15124 all entities, computed during semantic analysis.
15126 @item
15127 Library management issues are dealt with in files with prefix
15128 @file{LIB}.
15130 @item
15131 @findex Ada
15132 @cindex Annex A
15133 Ada files with the prefix @file{A-} are children of @code{Ada}, as
15134 defined in Annex A.
15136 @item
15137 @findex Interfaces
15138 @cindex Annex B
15139 Files with prefix @file{I-} are children of @code{Interfaces}, as
15140 defined in Annex B.
15142 @item
15143 @findex System
15144 Files with prefix @file{S-} are children of @code{System}. This includes
15145 both language-defined children and GNAT run-time routines.
15147 @item
15148 @findex GNAT
15149 Files with prefix @file{G-} are children of @code{GNAT}. These are useful
15150 general-purpose packages, fully documented in their specifications. All
15151 the other @file{.C} files are modifications of common @code{GNAT COMPILE} files.
15152 @end itemize
15154 @node Getting Internal Debugging Information
15155 @section Getting Internal Debugging Information
15157 @noindent
15158 Most compilers have internal debugging qualifiers and modes. GNAT
15159 does also, except GNAT internal debugging qualifiers and modes are not
15160 secret. A summary and full description of all the compiler and binder
15161 debug flags are in the file @file{DEBUG.ADB}. You must obtain the
15162 sources of the compiler to see the full detailed effects of these flags.
15164 The qualifiers that print the source of the program (reconstructed from
15165 the internal tree) are of general interest for user programs, as are the
15166 options to print
15167 the full internal tree, and the entity table (the symbol table
15168 information). The reconstructed source provides a readable version of the
15169 program after the front-end has completed analysis and  expansion, and is useful
15170 when studying the performance of specific constructs. For example, constraint
15171 checks are indicated, complex aggregates are replaced with loops and
15172 assignments, and tasking primitives are replaced with run-time calls.
15174 @node Stack Traceback
15175 @section Stack Traceback
15176 @cindex traceback
15177 @cindex stack traceback
15178 @cindex stack unwinding
15180 @noindent
15181 Traceback is a mechanism to display the sequence of subprogram calls that
15182 leads to a specified execution point in a program. Often (but not always)
15183 the execution point is an instruction at which an exception has been raised.
15184 This mechanism is also known as @i{stack unwinding} because it obtains
15185 its information by scanning the run-time stack and recovering the activation
15186 records of all active subprograms. Stack unwinding is one of the most
15187 important tools for program debugging.
15189 @noindent
15190 The first entry stored in traceback corresponds to the deepest calling level,
15191 that is to say the subprogram currently executing the instruction
15192 from which we want to obtain the traceback.
15194 @noindent
15195 Note that there is no runtime performance penalty when stack traceback
15196 is enabled and no exception are raised during program execution.
15198 @menu
15199 * Non-Symbolic Traceback::
15200 * Symbolic Traceback::
15201 @end menu
15203 @node Non-Symbolic Traceback
15204 @subsection Non-Symbolic Traceback
15205 @cindex traceback, non-symbolic
15207 @noindent
15208 Note: this feature is not supported on all platforms. See
15209 @file{GNAT.Traceback spec in G-TRACEB.ADS} for a complete list of supported
15210 platforms.
15212 @menu
15213 * Tracebacks From an Unhandled Exception::
15214 * Tracebacks From Exception Occurrences (non-symbolic)::
15215 * Tracebacks From Anywhere in a Program (non-symbolic)::
15216 @end menu
15218 @node Tracebacks From an Unhandled Exception
15219 @subsubsection Tracebacks From an Unhandled Exception
15221 @noindent
15222 A runtime non-symbolic traceback is a list of addresses of call instructions.
15223 To enable this feature you must use the @code{-E}
15224 @code{GNAT BIND}'s option. With this option a stack traceback is stored as part
15225 of exception information. It is possible to retrieve this information using the
15226 standard @code{Ada.Exception.Exception_Information} routine.
15228 @noindent
15229 Let's have a look at a simple example:
15231 @smallexample
15232 @cartouche
15233 @group
15234 procedure STB is
15236    procedure P1 is
15237    begin
15238       raise Constraint_Error;
15239    end P1;
15241    procedure P2 is
15242    begin
15243       P1;
15244    end P2;
15246 begin
15247    P2;
15248 end STB;
15249 @end group
15250 @end cartouche
15251 @end smallexample
15253 @smallexample
15254 $ GNAT MAKE stb /BINDER_QUALIFIERS -E
15255 $ stb
15257 Execution terminated by unhandled exception
15258 Exception name: CONSTRAINT_ERROR
15259 Message: STB.ADB:5
15260 Call stack traceback locations:
15261 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
15262 @end smallexample
15264 @noindent
15265 As we see the traceback lists a sequence of addresses for the unhandled
15266 exception @code{CONSTAINT_ERROR} raised in procedure P1. It is easy to
15267 guess that this exception come from procedure P1. To translate these
15268 addresses into the source lines where the calls appear, the
15269 @code{addr2line} tool, described below, is invaluable. The use of this tool
15270 requires the program to be compiled with debug information.
15272 @smallexample
15273 $ GNAT MAKE -g stb /BINDER_QUALIFIERS -E
15274 $ stb
15276 Execution terminated by unhandled exception
15277 Exception name: CONSTRAINT_ERROR
15278 Message: STB.ADB:5
15279 Call stack traceback locations:
15280 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
15282 $ addr2line --exe=stb 0x401373 0x40138b 0x40139c 0x401335 0x4011c4
15283    0x4011f1 0x77e892a4
15285 00401373 at d:/stb/STB.ADB:5
15286 0040138B at d:/stb/STB.ADB:10
15287 0040139C at d:/stb/STB.ADB:14
15288 00401335 at d:/stb/B~STB.ADB:104
15289 004011C4 at /build/.../CRT1.C:200
15290 004011F1 at /build/.../CRT1.C:222
15291 77E892A4 in ?? at ??:0
15292 @end smallexample
15294 @noindent
15295 @code{addr2line} has a number of other useful options:
15297 @table @code
15298 @item --functions
15299 to get the function name corresponding to any location
15301 @item --demangle=gnat
15302 to use the @b{gnat} decoding mode for the function names. Note that
15303 for binutils version 2.9.x the option is simply @code{--demangle}.
15304 @end table
15306 @smallexample
15307 $ addr2line --exe=stb --functions --demangle=gnat 0x401373 0x40138b
15308    0x40139c 0x401335 0x4011c4 0x4011f1
15310 00401373 in stb.p1 at d:/stb/STB.ADB:5
15311 0040138B in stb.p2 at d:/stb/STB.ADB:10
15312 0040139C in stb at d:/stb/STB.ADB:14
15313 00401335 in main at d:/stb/B~STB.ADB:104
15314 004011C4 in <__mingw_CRTStartup> at /build/.../CRT1.C:200
15315 004011F1 in <mainCRTStartup> at /build/.../CRT1.C:222
15316 @end smallexample
15318 @noindent
15319 From this traceback we can see that the exception was raised in
15320 @file{STB.ADB} at line 5, which was reached from a procedure call in
15321 @file{STB.ADB} at line 10, and so on. The @file{B~STD.ADB} is the binder file,
15322 which contains the call to the main program.
15323 @pxref{Running GNAT BIND}. The remaining entries are assorted runtime routines,
15324 and the output will vary from platform to platform.
15326 @noindent
15327 It is also possible to use @code{GDB} with these traceback addresses to debug
15328 the program. For example, we can break at a given code location, as reported
15329 in the stack traceback:
15331 @smallexample
15332 $ GDB -nw stb
15334 (GDB) break *0x401373
15335 Breakpoint 1 at 0x401373: file STB.ADB, line 5.
15336 @end smallexample
15338 @noindent
15339 It is important to note that the stack traceback addresses
15340 do not change when debug information is included. This is particularly useful
15341 because it makes it possible to release software without debug information (to
15342 minimize object size), get a field report that includes a stack traceback
15343 whenever an internal bug occurs, and then be able to retrieve the sequence
15344 of calls with the same program compiled with debug information.
15346 @node Tracebacks From Exception Occurrences (non-symbolic)
15347 @subsubsection Tracebacks From Exception Occurrences
15349 @noindent
15350 Non-symbolic tracebacks are obtained by using the @code{-E} binder argument.
15351 The stack traceback is attached to the exception information string, and can
15352 be retrieved in an exception handler within the Ada program, by means of the
15353 Ada95 facilities defined in @code{Ada.Exceptions}. Here is a simple example:
15355 @smallexample
15356 @cartouche
15357 @group
15358 with Ada.Text_IO;
15359 with Ada.Exceptions;
15361 procedure STB is
15363    use Ada;
15364    use Ada.Exceptions;
15366    procedure P1 is
15367       K : Positive := 1;
15368    begin
15369       K := K - 1;
15370    exception
15371       when E : others =>
15372          Text_IO.Put_Line (Exception_Information (E));
15373    end P1;
15375    procedure P2 is
15376    begin
15377       P1;
15378    end P2;
15380 begin
15381    P2;
15382 end STB;
15383 @end group
15384 @end cartouche
15385 @end smallexample
15387 @noindent
15388 This program will output:
15390 @smallexample
15391 $ stb
15393 Exception name: CONSTRAINT_ERROR
15394 Message: STB.ADB:12
15395 Call stack traceback locations:
15396 0x4015e4 0x401633 0x401644 0x401461 0x4011c4 0x4011f1 0x77e892a4
15397 @end smallexample
15399 @node Tracebacks From Anywhere in a Program (non-symbolic)
15400 @subsubsection Tracebacks From Anywhere in a Program
15402 @noindent
15403 It is also possible to retrieve a stack traceback from anywhere in a
15404 program. For this you need to
15405 use the @code{GNAT.Traceback} API. This package includes a procedure called
15406 @code{Call_Chain} that computes a complete stack traceback, as well as useful
15407 display procedures described below. It is not necessary to use the
15408 @code{-E GNAT BIND} option in this case, because the stack traceback mechanism
15409 is invoked explicitly.
15411 @noindent
15412 In the following example we compute a traceback at a specific location in
15413 the program, and we display it using @code{GNAT.Debug_Utilities.Image} to
15414 convert addresses to strings:
15416 @smallexample
15417 @cartouche
15418 @group
15419 with Ada.Text_IO;
15420 with GNAT.Traceback;
15421 with GNAT.Debug_Utilities;
15423 procedure STB is
15425    use Ada;
15426    use GNAT;
15427    use GNAT.Traceback;
15429    procedure P1 is
15430       TB  : Tracebacks_Array (1 .. 10);
15431       --  We are asking for a maximum of 10 stack frames.
15432       Len : Natural;
15433       --  Len will receive the actual number of stack frames returned.
15434    begin
15435       Call_Chain (TB, Len);
15437       Text_IO.Put ("In STB.P1 : ");
15439       for K in 1 .. Len loop
15440          Text_IO.Put (Debug_Utilities.Image (TB (K)));
15441          Text_IO.Put (' ');
15442       end loop;
15444       Text_IO.New_Line;
15445    end P1;
15447    procedure P2 is
15448    begin
15449       P1;
15450    end P2;
15452 begin
15453    P2;
15454 end STB;
15455 @end group
15456 @end cartouche
15457 @end smallexample
15459 @smallexample
15460 $ GNAT MAKE stb
15461 $ stb
15463 In STB.P1 : 16#0040_F1E4# 16#0040_14F2# 16#0040_170B# 16#0040_171C#
15464 16#0040_1461# 16#0040_11C4# 16#0040_11F1# 16#77E8_92A4#
15465 @end smallexample
15467 @node Symbolic Traceback
15468 @subsection Symbolic Traceback
15469 @cindex traceback, symbolic
15471 @noindent
15472 A symbolic traceback is a stack traceback in which procedure names are
15473 associated with each code location.
15475 @noindent
15476 Note that this feature is not supported on all platforms. See
15477 @file{GNAT.Traceback.Symbolic spec in G-TRASYM.ADS} for a complete
15478 list of currently supported platforms.
15480 @noindent
15481 Note that the symbolic traceback requires that the program be compiled
15482 with debug information. If it is not compiled with debug information
15483 only the non-symbolic information will be valid.
15485 @menu
15486 * Tracebacks From Exception Occurrences (symbolic)::
15487 * Tracebacks From Anywhere in a Program (symbolic)::
15488 @end menu
15490 @node Tracebacks From Exception Occurrences (symbolic)
15491 @subsubsection Tracebacks From Exception Occurrences
15493 @smallexample
15494 @cartouche
15495 @group
15496 with Ada.Text_IO;
15497 with GNAT.Traceback.Symbolic;
15499 procedure STB is
15501    procedure P1 is
15502    begin
15503       raise Constraint_Error;
15504    end P1;
15506    procedure P2 is
15507    begin
15508       P1;
15509    end P2;
15511    procedure P3 is
15512    begin
15513       P2;
15514    end P3;
15516 begin
15517    P3;
15518 exception
15519    when E : others =>
15520       Ada.Text_IO.Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
15521 end STB;
15522 @end group
15523 @end cartouche
15524 @end smallexample
15526 @smallexample
15527 $ GNAT MAKE -g stb /BINDER_QUALIFIERS -E /LINKER_QUALIFIERS -lgnat -laddr2line -lintl
15528 $ stb
15530 0040149F in stb.p1 at STB.ADB:8
15531 004014B7 in stb.p2 at STB.ADB:13
15532 004014CF in stb.p3 at STB.ADB:18
15533 004015DD in ada.stb at STB.ADB:22
15534 00401461 in main at B~STB.ADB:168
15535 004011C4 in __mingw_CRTStartup at CRT1.C:200
15536 004011F1 in mainCRTStartup at CRT1.C:222
15537 77E892A4 in ?? at ??:0
15538 @end smallexample
15540 @noindent
15541 The exact sequence of linker options may vary from platform to platform.
15542 The above @code{/LINKER_QUALIFIERS} section is for Windows platforms. By contrast,
15543 under Unix there is no need for the @code{/LINKER_QUALIFIERS} section.
15544 Differences across platforms are due to details of linker implementation.
15546 @node Tracebacks From Anywhere in a Program (symbolic)
15547 @subsubsection Tracebacks From Anywhere in a Program
15549 @noindent
15550 It is possible to get a symbolic stack traceback
15551 from anywhere in a program, just as for non-symbolic tracebacks.
15552 The first step is to obtain a non-symbolic
15553 traceback, and then call @code{Symbolic_Traceback} to compute the symbolic
15554 information. Here is an example:
15556 @smallexample
15557 @cartouche
15558 @group
15559 with Ada.Text_IO;
15560 with GNAT.Traceback;
15561 with GNAT.Traceback.Symbolic;
15563 procedure STB is
15565    use Ada;
15566    use GNAT.Traceback;
15567    use GNAT.Traceback.Symbolic;
15569    procedure P1 is
15570       TB  : Tracebacks_Array (1 .. 10);
15571       --  We are asking for a maximum of 10 stack frames.
15572       Len : Natural;
15573       --  Len will receive the actual number of stack frames returned.
15574    begin
15575       Call_Chain (TB, Len);
15576       Text_IO.Put_Line (Symbolic_Traceback (TB (1 .. Len)));
15577    end P1;
15579    procedure P2 is
15580    begin
15581       P1;
15582    end P2;
15584 begin
15585    P2;
15586 end STB;
15587 @end group
15588 @end cartouche
15589 @end smallexample
15591 @node Compatibility with DEC Ada
15592 @chapter Compatibility with DEC Ada
15593 @cindex Compatibility
15595 @noindent
15596 This section of the manual compares DEC Ada for OpenVMS Alpha and GNAT
15597 OpenVMS Alpha. GNAT achieves a high level of compatibility
15598 with DEC Ada, and it should generally be straightforward to port code
15599 from the DEC Ada environment to GNAT. However, there are a few language
15600 and implementation differences of which the user must be aware. These
15601 differences are discussed in this section. In
15602 addition, the operating environment and command structure for the
15603 compiler are different, and these differences are also discussed.
15605 Note that this discussion addresses specifically the implementation
15606 of Ada 83 for DIGITAL OpenVMS Alpha Systems. In cases where the implementation
15607 of DEC Ada differs between OpenVMS Alpha Systems and OpenVMS VAX Systems, GNAT
15608 always follows the Alpha implementation.
15610 @menu
15611 * Ada 95 Compatibility::
15612 * Differences in the Definition of Package System::
15613 * Language-Related Features::
15614 * The Package STANDARD::
15615 * The Package SYSTEM::
15616 * Tasking and Task-Related Features::
15617 * Implementation of Tasks in DEC Ada for OpenVMS Alpha Systems::
15618 * Pragmas and Pragma-Related Features::
15619 * Library of Predefined Units::
15620 * Bindings::
15621 * Main Program Definition::
15622 * Implementation-Defined Attributes::
15623 * Compiler and Run-Time Interfacing::
15624 * Program Compilation and Library Management::
15625 * Input-Output::
15626 * Implementation Limits::
15627 * Tools::
15628 @end menu
15630 @node Ada 95 Compatibility
15631 @section Ada 95 Compatibility
15633 @noindent
15634 GNAT is an Ada 95 compiler, and DEC Ada is an Ada 83
15635 compiler. Ada 95 is almost completely upwards compatible
15636 with Ada 83, and therefore Ada 83 programs will compile
15637 and run under GNAT with
15638 no changes or only minor changes. The Ada 95 Reference
15639 Manual (ANSI/ISO/IEC-8652:1995) provides details on specific
15640 incompatibilities.
15642 GNAT provides the qualifier /83 on the GNAT COMPILE command,
15643 as well as the pragma ADA_83, to force the compiler to
15644 operate in Ada 83 mode. This mode does not guarantee complete
15645 conformance to Ada 83, but in practice is sufficient to
15646 eliminate most sources of incompatibilities.
15647 In particular, it eliminates the recognition of the
15648 additional Ada 95 keywords, so that their use as identifiers
15649 in Ada83 program is legal, and handles the cases of packages
15650 with optional bodies, and generics that instantiate unconstrained
15651 types without the use of @code{(<>)}.
15653 @node Differences in the Definition of Package System
15654 @section Differences in the Definition of Package System
15656 @noindent
15657 Both the Ada 95 and Ada 83 reference manuals permit a compiler to add
15658 implementation-dependent declarations to package System. In normal mode,
15659 GNAT does not take advantage of this permission, and the version of System
15660 provided by GNAT exactly matches that in the Ada 95 Reference Manual.
15662 However, DEC Ada adds an extensive set of declarations to package System,
15663 as fully documented in the DEC Ada manuals. To minimize changes required
15664 for programs that make use of these extensions, GNAT provides the pragma
15665 Extend_System for extending the definition of package System. By using:
15667 @smallexample
15668 @group
15669 @cartouche
15670 @b{pragma} Extend_System (Aux_DEC);
15671 @end cartouche
15672 @end group
15673 @end smallexample
15675 @noindent
15676 The set of definitions in System is extended to include those in package
15677 @code{System.Aux_DEC}.
15678 These definitions are incorporated directly into package
15679 System, as though they had been declared there in the first place. For a
15680 list of the declarations added, see the specification of this package,
15681 which can be found in the file @code{S-AUXDEC.ADS} in the GNAT library.
15682 The pragma Extend_System is a configuration pragma, which means that
15683 it can be placed in the file @file{GNAT.ADC}, so that it will automatically
15684 apply to all subsequent compilations. See the section on Configuration
15685 Pragmas for further details.
15687 An alternative approach that avoids the use of the non-standard
15688 Extend_System pragma is to add a context clause to the unit that
15689 references these facilities:
15691 @smallexample
15692 @group
15693 @cartouche
15694 @b{with} System.Aux_DEC;
15695 @b{use}  System.Aux_DEC;
15696 @end cartouche
15697 @end group
15698 @end smallexample
15700 @noindent
15701 The effect is not quite semantically identical to incorporating the declarations
15702 directly into package @code{System},
15703 but most programs will not notice a difference
15704 unless they use prefix notation (e.g. @code{System.Integer_8})
15705 to reference the
15706 entities directly in package @code{System}.
15707 For units containing such references,
15708 the prefixes must either be removed, or the pragma @code{Extend_System}
15709 must be used.
15711 @node Language-Related Features
15712 @section Language-Related Features
15714 @noindent
15715 The following sections highlight differences in types,
15716 representations of types, operations, alignment, and
15717 related topics.
15719 @menu
15720 * Integer Types and Representations::
15721 * Floating-Point Types and Representations::
15722 * Pragmas Float_Representation and Long_Float::
15723 * Fixed-Point Types and Representations::
15724 * Record and Array Component Alignment::
15725 * Address Clauses::
15726 * Other Representation Clauses::
15727 @end menu
15729 @node Integer Types and Representations
15730 @subsection Integer Types and Representations
15732 @noindent
15733 The set of predefined integer types is identical in DEC Ada and GNAT.
15734 Furthermore the representation of these integer types is also identical,
15735 including the capability of size clauses forcing biased representation.
15737 In addition,
15738 DEC Ada for OpenVMS Alpha systems has defined the
15739 following additional integer types in package System:
15741 @itemize @bullet
15743 @item
15744 INTEGER_8
15746 @item
15747 INTEGER_16
15749 @item
15750 INTEGER_32
15752 @item
15753 INTEGER_64
15755 @item
15756 LARGEST_INTEGER
15757 @end itemize
15759 @noindent
15760 When using GNAT, the first four of these types may be obtained from the
15761 standard Ada 95 package @code{Interfaces}.
15762 Alternatively, by use of the pragma
15763 @code{Extend_System}, identical
15764 declarations can be referenced directly in package @code{System}.
15765 On both GNAT and DEC Ada, the maximum integer size is 64 bits.
15767 @node Floating-Point Types and Representations
15768 @subsection Floating-Point Types and Representations
15769 @cindex Floating-Point types
15771 @noindent
15772 The set of predefined floating-point types is identical in DEC Ada and GNAT.
15773 Furthermore the representation of these floating-point
15774 types is also identical. One important difference is that the default
15775 representation for DEC Ada is VAX_Float, but the default representation
15776 for GNAT is IEEE.
15778 Specific types may be declared to be VAX_Float or IEEE, using the pragma
15779 @code{Float_Representation} as described in the DEC Ada documentation.
15780 For example, the declarations:
15782 @smallexample
15783 @group
15784 @cartouche
15785 @b{type} F_Float @b{is digits} 6;
15786 @b{pragma} Float_Representation (VAX_Float, F_Float);
15787 @end cartouche
15788 @end group
15789 @end smallexample
15791 @noindent
15792 declare a type F_Float that will be represented in VAX_Float format.
15793 This set of declarations actually appears in System.Aux_DEC, which provides
15794 the full set of additional floating-point declarations provided in
15795 the DEC Ada version of package
15796 System. This and similar declarations may be accessed in a user program by using
15797 pragma @code{Extend_System}. The use of this
15798 pragma, and the related pragma @code{Long_Float} is described in further
15799 detail in the following section.
15801 @node Pragmas Float_Representation and Long_Float
15802 @subsection Pragmas Float_Representation and Long_Float
15804 @noindent
15805 DEC Ada provides the pragma @code{Float_Representation}, which
15806 acts as a program library qualifier to allow control over
15807 the internal representation chosen for the predefined
15808 floating-point types declared in the package @code{Standard}.
15809 The format of this pragma is as follows:
15811 @smallexample
15812 @group
15813 @cartouche
15814 @b{pragma} @code{Float_Representation}(VAX_Float | IEEE_Float);
15815 @end cartouche
15816 @end group
15817 @end smallexample
15819 @noindent
15820 This pragma controls the representation of floating-point
15821 types as follows:
15823 @itemize @bullet
15824 @item
15825 @code{VAX_Float} specifies that floating-point
15826 types are represented by default with the VAX hardware types
15827 F-floating, D-floating, G-floating. Note that the H-floating
15828 type is available only on DIGITAL Vax systems, and is not available
15829 in either DEC Ada or GNAT for Alpha systems.
15831 @item
15832 @code{IEEE_Float} specifies that floating-point
15833 types are represented by default with the IEEE single and
15834 double floating-point types.
15835 @end itemize
15837 @noindent
15838 GNAT provides an identical implementation of the pragma
15839 @code{Float_Representation}, except that it functions as a
15840 configuration pragma, as defined by Ada 95. Note that the
15841 notion of configuration pragma corresponds closely to the
15842 DEC Ada notion of a program library qualifier.
15844 When no pragma is used in GNAT, the default is IEEE_Float, which is different
15845 from DEC Ada 83, where the default is VAX_Float. In addition, the
15846 predefined libraries in GNAT are built using IEEE_Float, so it is not
15847 advisable to change the format of numbers passed to standard library
15848 routines, and if necessary explicit type conversions may be needed.
15850 The use of IEEE_Float is recommended in GNAT since it is more efficient,
15851 and (given that it conforms to an international standard) potentially more
15852 portable. The situation in which VAX_Float may be useful is in interfacing
15853 to existing code and data that expects the use of VAX_Float. There are
15854 two possibilities here. If the requirement for the use of VAX_Float is
15855 localized, then the best approach is to use the predefined VAX_Float
15856 types in package @code{System}, as extended by
15857 @code{Extend_System}. For example, use @code{System.F_Float}
15858 to specify the 32-bit @code{F-Float} format.
15860 Alternatively, if an entire program depends heavily on the use of
15861 the @code{VAX_Float} and in particular assumes that the types in
15862 package @code{Standard} are in @code{Vax_Float} format, then it
15863 may be desirable to reconfigure GNAT to assume Vax_Float by default.
15864 This is done by using the GNAT LIBRARY command to rebuild the library, and
15865 then using the general form of the @code{Float_Representation}
15866 pragma to ensure that this default format is used throughout.
15867 The form of the GNAT LIBRARY command is:
15869 @smallexample
15870 GNAT LIBRARY /CONFIG=@i{file} /CREATE=@i{directory}
15871 @end smallexample
15873 @noindent
15874 where @i{file} contains the new configuration pragmas
15875 and @i{directory} is the directory to be created to contain
15876 the new library.
15878 @noindent
15879 On OpenVMS systems, DEC Ada provides the pragma @code{Long_Float}
15880 to allow control over the internal representation chosen
15881 for the predefined type @code{Long_Float} and for floating-point
15882 type declarations with digits specified in the range 7 .. 15.
15883 The format of this pragma is as follows:
15885 @smallexample
15886 @cartouche
15887 @b{pragma} Long_Float (D_FLOAT | G_FLOAT);
15888 @end cartouche
15889 @end smallexample
15891 @node Fixed-Point Types and Representations
15892 @subsection Fixed-Point Types and Representations
15894 @noindent
15895 On DEC Ada for OpenVMS Alpha systems, rounding is
15896 away from zero for both positive and negative numbers.
15897 Therefore, +0.5 rounds to 1 and -0.5 rounds to -1.
15899 On GNAT for OpenVMS Alpha, the results of operations
15900 on fixed-point types are in accordance with the Ada 95
15901 rules. In particular, results of operations on decimal
15902 fixed-point types are truncated.
15904 @node Record and Array Component Alignment
15905 @subsection Record and Array Component Alignment
15907 @noindent
15908 On DEC Ada for OpenVMS Alpha, all non composite components
15909 are aligned on natural boundaries. For example, 1-byte
15910 components are aligned on byte boundaries, 2-byte
15911 components on 2-byte boundaries, 4-byte components on 4-byte
15912 byte boundaries, and so on. The OpenVMS Alpha hardware
15913 runs more efficiently with naturally aligned data.
15915 ON GNAT for OpenVMS Alpha, alignment rules are compatible
15916 with DEC Ada for OpenVMS Alpha.
15918 @node Address Clauses
15919 @subsection Address Clauses
15921 @noindent
15922 In DEC Ada and GNAT, address clauses are supported for
15923 objects and imported subprograms.
15924 The predefined type @code{System.Address} is a private type
15925 in both compilers, with the same representation (it is simply
15926 a machine pointer). Addition, subtraction, and comparison
15927 operations are available in the standard Ada 95 package
15928 @code{System.Storage_Elements}, or in package @code{System}
15929 if it is extended to include @code{System.Aux_DEC} using a
15930 pragma @code{Extend_System} as previously described.
15932 Note that code that with's both this extended package @code{System}
15933 and the package @code{System.Storage_Elements} should not @code{use}
15934 both packages, or ambiguities will result. In general it is better
15935 not to mix these two sets of facilities. The Ada 95 package was
15936 designed specifically to provide the kind of features that DEC Ada
15937 adds directly to package @code{System}.
15939 GNAT is compatible with DEC Ada in its handling of address
15940 clauses, except for some limitations in
15941 the form of address clauses for composite objects with
15942 initialization. Such address clauses are easily replaced
15943 by the use of an explicitly-defined constant as described
15944 in the Ada 95 Reference Manual (13.1(22)). For example, the sequence
15945 of declarations:
15947 @smallexample
15948 @group
15949 @cartouche
15950 X, Y : Integer := Init_Func;
15951 Q : String (X .. Y) := "abc";
15953 @b{for} Q'Address @b{use} Compute_Address;
15954 @end cartouche
15955 @end group
15956 @end smallexample
15958 @noindent
15959 will be rejected by GNAT, since the address cannot be computed at the time
15960 that Q is declared. To achieve the intended effect, write instead:
15962 @smallexample
15963 @group
15964 @cartouche
15965 X, Y : Integer := Init_Func;
15966 Q_Address : @b{constant} Address := Compute_Address;
15967 Q : String (X .. Y) := "abc";
15969 @b{for} Q'Address @b{use} Q_Address;
15970 @end cartouche
15971 @end group
15972 @end smallexample
15974 @noindent
15975 which will be accepted by GNAT (and other Ada 95 compilers), and is also
15976 backwards compatible with Ada 83. A fuller description of the restrictions
15977 on address specifications is found in the GNAT Reference Manual.
15979 @node Other Representation Clauses
15980 @subsection Other Representation Clauses
15982 @noindent
15983 GNAT supports in a compatible manner all the representation
15984 clauses supported by DEC Ada. In addition, it
15985 supports representation clause forms that are new in Ada 95
15986 including COMPONENT_SIZE and SIZE clauses for objects.
15988 @node The Package STANDARD
15989 @section The Package STANDARD
15991 @noindent
15992 The package STANDARD, as implemented by DEC Ada, is fully
15993 described in the Reference Manual for the Ada Programming
15994 Language (ANSI/MIL-STD-1815A-1983) and in the DEC Ada
15995 Language Reference Manual. As implemented by GNAT, the
15996 package STANDARD is described in the Ada 95 Reference
15997 Manual.
15999 In addition, DEC Ada supports the Latin-1 character set in
16000 the type CHARACTER. GNAT supports the Latin-1 character set
16001 in the type CHARACTER and also Unicode (ISO 10646 BMP) in
16002 the type WIDE_CHARACTER.
16004 The floating-point types supported by GNAT are those
16005 supported by DEC Ada, but defaults are different, and are controlled by
16006 pragmas. See @pxref{Floating-Point Types and Representations} for details.
16008 @node The Package SYSTEM
16009 @section The Package SYSTEM
16011 @noindent
16012 DEC Ada provides a system-specific version of the package
16013 SYSTEM for each platform on which the language ships.
16014 For the complete specification of the package SYSTEM, see
16015 Appendix F of the DEC Ada Language Reference Manual.
16017 On DEC Ada, the package SYSTEM includes the following conversion functions:
16018 @itemize @bullet
16019 @item TO_ADDRESS(INTEGER)
16021 @item  TO_ADDRESS(UNSIGNED_LONGWORD)
16023 @item  TO_ADDRESS(universal_integer)
16025 @item  TO_INTEGER(ADDRESS)
16027 @item  TO_UNSIGNED_LONGWORD(ADDRESS)
16029 @item  Function IMPORT_VALUE return UNSIGNED_LONGWORD and the
16030                  functions IMPORT_ADDRESS and IMPORT_LARGEST_VALUE
16031 @end itemize
16033 @noindent
16034 By default, GNAT supplies a version of SYSTEM that matches
16035 the definition given in the Ada 95 Reference Manual.
16036 This
16037 is a subset of the DIGITAL system definitions, which is as
16038 close as possible to the original definitions. The only difference
16039 is that the definition of SYSTEM_NAME is different:
16041 @smallexample
16042 @group
16043 @cartouche
16044 @b{type} Name @b{is} (SYSTEM_NAME_GNAT);
16045 System_Name : @b{constant} Name := SYSTEM_NAME_GNAT;
16046 @end cartouche
16047 @end group
16048 @end smallexample
16050 @noindent
16051 Also, GNAT adds the new Ada 95 declarations for
16052 BIT_ORDER and DEFAULT_BIT_ORDER.
16054 However, the use of the following pragma causes GNAT
16055 to extend the definition of package SYSTEM so that it
16056 encompasses the full set of DIGITAL-specific extensions,
16057 including the functions listed above:
16059 @smallexample
16060 @cartouche
16061 @b{pragma} Extend_System (Aux_DEC);
16062 @end cartouche
16063 @end smallexample
16065 @noindent
16066 The pragma Extend_System is a configuration pragma that
16067 is most conveniently placed in the @file{GNAT.ADC} file. See the
16068 GNAT Reference Manual for further details.
16070 DEC Ada does not allow the recompilation of the package
16071 SYSTEM. Instead DEC Ada provides several pragmas (SYSTEM_
16072 NAME, STORAGE_UNIT, and MEMORY_SIZE) to modify values in
16073 the package SYSTEM. On OpenVMS Alpha systems, the pragma
16074 SYSTEM_NAME takes the enumeration literal OPENVMS_AXP as
16075 its single argument.
16077 GNAT does permit the recompilation of package SYSTEM using
16078 a special qualifier (/STYLE=GNAT) and this qualifier can be used if
16079 it is necessary to change constants in SYSTEM. GNAT does
16080 not permit the specification of SYSTEM_NAME, STORAGE_UNIT
16081 or MEMORY_SIZE by any other means.
16083 On GNAT systems, the pragma SYSTEM_NAME takes the
16084 enumeration literal SYSTEM_NAME_GNAT.
16086 The definitions provided by the use of
16088 @smallexample
16089 pragma Extend_System (AUX_Dec);
16090 @end smallexample
16092 @noindent
16093 are virtually identical to those provided by the DEC Ada 83 package
16094 System. One important difference is that the name of the TO_ADDRESS
16095 function for type UNSIGNED_LONGWORD is changed to TO_ADDRESS_LONG.
16096 See the GNAT Reference manual for a discussion of why this change was
16097 necessary.
16099 @noindent
16100 The version of TO_ADDRESS taking a universal integer argument is in fact
16101 an extension to Ada 83 not strictly compatible with the reference manual.
16102 In GNAT, we are constrained to be exactly compatible with the standard,
16103 and this means we cannot provide this capability. In DEC Ada 83, the
16104 point of this definition is to deal with a call like:
16106 @smallexample
16107 TO_ADDRESS (16#12777#);
16108 @end smallexample
16110 @noindent
16111 Normally, according to the Ada 83 standard, one would expect this to be
16112 ambiguous, since it matches both the INTEGER and UNSIGNED_LONGWORD forms
16113 of TO_ADDRESS. However, in DEC Ada 83, there is no ambiguity, since the
16114 definition using universal_integer takes precedence.
16116 In GNAT, since the version with universal_integer cannot be supplied, it is
16117 not possible to be 100% compatible. Since there are many programs using
16118 numeric constants for the argument to TO_ADDRESS, the decision in GNAT was
16119 to change the name of the function in the UNSIGNED_LONGWORD case, so the
16120 declarations provided in the GNAT version of AUX_Dec are:
16122 @smallexample
16123 function To_Address (X : Integer) return Address;
16124 pragma Pure_Function (To_Address);
16126 function To_Address_Long (X : Unsigned_Longword) return Address;
16127 pragma Pure_Function (To_Address_Long);
16128 @end smallexample
16130 @noindent
16131 This means that programs using TO_ADDRESS for UNSIGNED_LONGWORD must
16132 change the name to TO_ADDRESS_LONG.
16134 @node Tasking and Task-Related Features
16135 @section Tasking and Task-Related Features
16137 @noindent
16138 The concepts relevant to a comparison of tasking on GNAT
16139 and on DEC Ada for OpenVMS Alpha systems are discussed in
16140 the following sections.
16142 For detailed information on concepts related to tasking in
16143 DEC Ada, see the DEC Ada Language Reference Manual and the
16144 relevant run-time reference manual.
16146 @node Implementation of Tasks in DEC Ada for OpenVMS Alpha Systems
16147 @section Implementation of Tasks in DEC Ada for OpenVMS Alpha Systems
16149 @noindent
16150 On OpenVMS Alpha systems, each Ada task (except a passive
16151 task) is implemented as a single stream of execution
16152 that is created and managed by the kernel. On these
16153 systems, DEC Ada tasking support is based on DECthreads,
16154 an implementation of the POSIX standard for threads.
16156 Although tasks are implemented as threads, all tasks in
16157 an Ada program are part of the same process. As a result,
16158 resources such as open files and virtual memory can be
16159 shared easily among tasks. Having all tasks in one process
16160 allows better integration with the programming environment
16161 (the shell and the debugger, for example).
16163 Also, on OpenVMS Alpha systems, DEC Ada tasks and foreign
16164 code that calls DECthreads routines can be used together.
16165 The interaction between Ada tasks and DECthreads routines
16166 can have some benefits. For example when on OpenVMS Alpha,
16167 DEC Ada can call C code that is already threaded.
16168 GNAT on OpenVMS Alpha uses the facilities of DECthreads,
16169 and Ada tasks are mapped to threads.
16171 @menu
16172 * Assigning Task IDs::
16173 * Task IDs and Delays::
16174 * Task-Related Pragmas::
16175 * Scheduling and Task Priority::
16176 * The Task Stack::
16177 * External Interrupts::
16178 @end menu
16180 @node Assigning Task IDs
16181 @subsection Assigning Task IDs
16183 @noindent
16184 The DEC Ada Run-Time Library always assigns %TASK 1 to
16185 the environment task that executes the main program. On
16186 OpenVMS Alpha systems, %TASK 0 is often used for tasks
16187 that have been created but are not yet activated.
16189 On OpenVMS Alpha systems, task IDs are assigned at
16190 activation. On GNAT systems, task IDs are also assigned at
16191 task creation but do not have the same form or values as
16192 task ID values in DEC Ada. There is no null task, and the
16193 environment task does not have a specific task ID value.
16195 @node Task IDs and Delays
16196 @subsection Task IDs and Delays
16198 @noindent
16199 On OpenVMS Alpha systems, tasking delays are implemented
16200 using Timer System Services. The Task ID is used for the
16201 identification of the timer request (the REQIDT parameter).
16202 If Timers are used in the application take care not to use
16203 0 for the identification, because cancelling such a timer
16204 will cancel all timers and may lead to unpredictable results.
16206 @node Task-Related Pragmas
16207 @subsection Task-Related Pragmas
16209 @noindent
16210 Ada supplies the pragma TASK_STORAGE, which allows
16211 specification of the size of the guard area for a task
16212 stack. (The guard area forms an area of memory that has no
16213 read or write access and thus helps in the detection of
16214 stack overflow.) On OpenVMS Alpha systems, if the pragma
16215 TASK_STORAGE specifies a value of zero, a minimal guard
16216 area is created. In the absence of a pragma TASK_STORAGE, a default guard
16217 area is created.
16219 GNAT supplies the following task-related pragmas:
16221 @itemize @bullet
16222 @item  TASK_INFO
16224               This pragma appears within a task definition and
16225               applies to the task in which it appears. The argument
16226               must be of type SYSTEM.TASK_INFO.TASK_INFO_TYPE.
16228 @item  TASK_STORAGE
16230               GNAT implements pragma TASK_STORAGE in the same way as
16231               DEC Ada.
16232               Both DEC Ada and GNAT supply the pragmas PASSIVE,
16233               SUPPRESS, and VOLATILE.
16234 @end itemize
16235 @node Scheduling and Task Priority
16236 @subsection Scheduling and Task Priority
16238 @noindent
16239 DEC Ada implements the Ada language requirement that
16240 when two tasks are eligible for execution and they have
16241 different priorities, the lower priority task does not
16242 execute while the higher priority task is waiting. The DEC
16243 Ada Run-Time Library keeps a task running until either the
16244 task is suspended or a higher priority task becomes ready.
16246 On OpenVMS Alpha systems, the default strategy is round-
16247 robin with preemption. Tasks of equal priority take turns
16248 at the processor. A task is run for a certain period of
16249 time and then placed at the rear of the ready queue for
16250 its priority level.
16252 DEC Ada provides the implementation-defined pragma TIME_SLICE,
16253 which can be used to enable or disable round-robin
16254 scheduling of tasks with the same priority.
16255 See the relevant DEC Ada run-time reference manual for
16256 information on using the pragmas to control DEC Ada task
16257 scheduling.
16259 GNAT follows the scheduling rules of Annex D (real-time
16260 Annex) of the Ada 95 Reference Manual. In general, this
16261 scheduling strategy is fully compatible with DEC Ada
16262 although it provides some additional constraints (as
16263 fully documented in Annex D).
16264 GNAT implements time slicing control in a manner compatible with
16265 DEC Ada 83, by means of the pragma Time_Slice, whose semantics are identical
16266 to the DEC Ada 83 pragma of the same name.
16267 Note that it is not possible to mix GNAT tasking and
16268 DEC Ada 83 tasking in the same program, since the two run times are
16269 not compatible.
16271 @node The Task Stack
16272 @subsection The Task Stack
16274 @noindent
16275 In DEC Ada, a task stack is allocated each time a
16276 non passive task is activated. As soon as the task is
16277 terminated, the storage for the task stack is deallocated.
16278 If you specify a size of zero (bytes) with T'STORAGE_SIZE,
16279 a default stack size is used. Also, regardless of the size
16280 specified, some additional space is allocated for task
16281 management purposes. On OpenVMS Alpha systems, at least
16282 one page is allocated.
16284 GNAT handles task stacks in a similar manner. According to
16285 the Ada 95 rules, it provides the pragma STORAGE_SIZE as
16286 an alternative method for controlling the task stack size.
16287 The specification of the attribute T'STORAGE_SIZE is also
16288 supported in a manner compatible with DEC Ada.
16290 @node External Interrupts
16291 @subsection External Interrupts
16293 @noindent
16294 On DEC Ada, external interrupts can be associated with task entries.
16295 GNAT is compatible with DEC Ada in its handling of external interrupts.
16297 @node Pragmas and Pragma-Related Features
16298 @section Pragmas and Pragma-Related Features
16300 @noindent
16301 Both DEC Ada and GNAT supply all language-defined pragmas
16302 as specified by the Ada 83 standard. GNAT also supplies all
16303 language-defined pragmas specified in the Ada 95 Reference Manual.
16304 In addition, GNAT implements the implementation-defined pragmas
16305 from DEC Ada 83.
16307 @itemize @bullet
16308 @item  AST_ENTRY
16310 @item  COMMON_OBJECT
16312 @item  COMPONENT_ALIGNMENT
16314 @item  EXPORT_EXCEPTION
16316 @item  EXPORT_FUNCTION
16318 @item  EXPORT_OBJECT
16320 @item  EXPORT_PROCEDURE
16322 @item  EXPORT_VALUED_PROCEDURE
16324 @item  FLOAT_REPRESENTATION
16326 @item  IDENT
16328 @item  IMPORT_EXCEPTION
16330 @item  IMPORT_FUNCTION
16332 @item  IMPORT_OBJECT
16334 @item  IMPORT_PROCEDURE
16336 @item  IMPORT_VALUED_PROCEDURE
16338 @item  INLINE_GENERIC
16340 @item  INTERFACE_NAME
16342 @item  LONG_FLOAT
16344 @item  MAIN_STORAGE
16346 @item  PASSIVE
16348 @item  PSET_OBJECT
16350 @item  SHARE_GENERIC
16352 @item  SUPPRESS_ALL
16354 @item  TASK_STORAGE
16356 @item  TIME_SLICE
16358 @item  TITLE
16359 @end itemize
16361 @noindent
16362 These pragmas are all fully implemented, with the exception of @code{Title},
16363 @code{Passive}, and @code{Share_Generic}, which are
16364 recognized, but which have no
16365 effect in GNAT. The effect of @code{Passive} may be obtained by the
16366 use of protected objects in Ada 95. In GNAT, all generics are inlined.
16368 Unlike DEC Ada, the GNAT 'EXPORT_@i{subprogram}' pragmas require
16369 a separate subprogram specification which must appear before the
16370 subprogram body.
16372 GNAT also supplies a number of implementation-defined pragmas as follows:
16373 @itemize @bullet
16374 @item  C_PASS_BY_COPY
16376 @item  EXTEND_SYSTEM
16378 @item  SOURCE_FILE_NAME
16380 @item  UNSUPPRESS
16382 @item  WARNINGS
16384 @item  ABORT_DEFER
16386 @item  ADA_83
16388 @item  ADA_95
16390 @item  ANNOTATE
16392 @item  ASSERT
16394 @item  CPP_CLASS
16396 @item  CPP_CONSTRUCTOR
16398 @item  CPP_DESTRUCTOR
16400 @item  CPP_VIRTUAL
16402 @item  CP_VTABLE
16404 @item  DEBUG
16406 @item  LINKER_ALIAS
16408 @item  LINKER_SECTION
16410 @item  MACHINE_ATTRIBUTE
16412 @item  NO_RETURN
16414 @item  PURE_FUNCTION
16416 @item  SOURCE_REFERENCE
16418 @item  TASK_INFO
16420 @item  UNCHECKED_UNION
16422 @item  UNIMPLEMENTED_UNIT
16424 @item  WEAK_EXTERNAL
16425 @end itemize
16427 @noindent
16428 For full details on these GNAT implementation-defined pragmas, see
16429 the GNAT Reference Manual.
16431 @menu
16432 * Restrictions on the Pragma INLINE::
16433 * Restrictions on the Pragma INTERFACE::
16434 * Restrictions on the Pragma SYSTEM_NAME::
16435 @end menu
16437 @node Restrictions on the Pragma INLINE
16438 @subsection Restrictions on the Pragma INLINE
16440 @noindent
16441 DEC Ada applies the following restrictions to the pragma INLINE:
16442 @itemize @bullet
16443 @item  Parameters cannot be a task type.
16445 @item  Function results cannot be task types, unconstrained
16446 array types, or unconstrained types with discriminants.
16448 @item  Bodies cannot declare the following:
16449 @itemize @bullet
16450 @item  Subprogram body or stub (imported subprogram is allowed)
16452 @item  Tasks
16454 @item  Generic declarations
16456 @item  Instantiations
16458 @item  Exceptions
16460 @item  Access types (types derived from access types allowed)
16462 @item  Array or record types
16464 @item  Dependent tasks
16466 @item  Direct recursive calls of subprogram or containing
16467 subprogram, directly or via a renaming
16469 @end itemize
16470 @end itemize
16472 @noindent
16473 In GNAT, the only restriction on pragma INLINE is that the
16474 body must occur before the call if both are in the same
16475 unit, and the size must be appropriately small. There are
16476 no other specific restrictions which cause subprograms to
16477 be incapable of being inlined.
16479 @node  Restrictions on the Pragma INTERFACE
16480 @subsection  Restrictions on the Pragma INTERFACE
16482 @noindent
16483 The following lists and describes the restrictions on the
16484 pragma INTERFACE on DEC Ada and GNAT:
16485 @itemize @bullet
16486 @item  Languages accepted: Ada, Bliss, C, Fortran, Default.
16487 Default is the default on OpenVMS Alpha systems.
16489 @item  Parameter passing: Language specifies default
16490 mechanisms but can be overridden with an EXPORT pragma.
16492 @itemize @bullet
16493 @item  Ada: Use internal Ada rules.
16495 @item  Bliss, C: Parameters must be mode @code{in}; cannot be
16496 record or task type. Result cannot be a string, an
16497 array, or a record.
16499 @item  Fortran: Parameters cannot be a task. Result cannot
16500 be a string, an array, or a record.
16501 @end itemize
16502 @end itemize
16504 @noindent
16505 GNAT is entirely upwards compatible with DEC Ada, and in addition allows
16506 record parameters for all languages.
16508 @node  Restrictions on the Pragma SYSTEM_NAME
16509 @subsection  Restrictions on the Pragma SYSTEM_NAME
16511 @noindent
16512 For DEC Ada for OpenVMS Alpha, the enumeration literal
16513 for the type NAME is OPENVMS_AXP. In GNAT, the enumeration
16514 literal for the type NAME is SYSTEM_NAME_GNAT.
16516 @node  Library of Predefined Units
16517 @section  Library of Predefined Units
16519 @noindent
16520 A library of predefined units is provided as part of the
16521 DEC Ada and GNAT implementations. DEC Ada does not provide
16522 the package MACHINE_CODE but instead recommends importing
16523 assembler code.
16525 The GNAT versions of the DEC Ada Run-Time Library (ADA$PREDEFINED:)
16526 units are taken from the OpenVMS Alpha version, not the OpenVMS VAX
16527 version. During GNAT installation, the DEC Ada Predefined
16528 Library units are copied into the GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
16529 (aka DECLIB) directory and patched to remove Ada 95 incompatibilities
16530 and to make them interoperable with GNAT, @pxref{Changes to DECLIB}
16531 for details.
16533 The GNAT RTL is contained in
16534 the GNU:[LIB.OPENVMS7_x.2_8_x.ADALIB] (aka ADALIB) directory and
16535 the default search path is set up to find DECLIB units in preference
16536 to ADALIB units with the same name (TEXT_IO, SEQUENTIAL_IO, and DIRECT_IO,
16537 for example).
16539 However, it is possible to change the default so that the
16540 reverse is true, or even to mix them using child package
16541 notation. The DEC Ada 83 units are available as DEC.xxx where xxx
16542 is the package name, and the Ada units are available in the
16543 standard manner defined for Ada 95, that is to say as Ada.xxx. To
16544 change the default, set ADA_INCLUDE_PATH and ADA_OBJECTS_PATH
16545 appropriately. For example, to change the default to use the Ada95
16546 versions do:
16548 @smallexample
16549 $ DEFINE ADA_INCLUDE_PATH GNU:[LIB.OPENVMS7_1.2_8_1.ADAINCLUDE],-
16550                           GNU:[LIB.OPENVMS7_1.2_8_1.DECLIB]
16551 $ DEFINE ADA_OBJECTS_PATH GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB],-
16552                           GNU:[LIB.OPENVMS7_1.2_8_1.DECLIB]
16553 @end smallexample
16555 @menu
16556 * Changes to DECLIB::
16557 @end menu
16559 @node Changes to DECLIB
16560 @subsection Changes to DECLIB
16562 @noindent
16563 The changes made to the DEC Ada predefined library for GNAT and Ada 95
16564 compatibility are minor and include the following:
16566 @itemize @bullet
16567 @item  Adjusting the location of pragmas and record representation
16568 clauses to obey Ada 95 rules
16570 @item  Adding the proper notation to generic formal parameters
16571 that take unconstrained types in instantiation
16573 @item  Adding pragma ELABORATE_BODY to package specifications
16574 that have package bodies not otherwise allowed
16576 @item  Occurrences of the identifier "PROTECTED" are renamed to "PROTECTD".
16577 Currently these are found only in the STARLET package spec.
16578 @end itemize
16580 @noindent
16581 None of the above changes is visible to users.
16583 @node Bindings
16584 @section Bindings
16586 @noindent
16587 On OpenVMS Alpha, DEC Ada provides the following strongly-typed bindings:
16588 @itemize @bullet
16590 @item  Command Language Interpreter (CLI interface)
16592 @item  DECtalk Run-Time Library (DTK interface)
16594 @item  Librarian utility routines (LBR interface)
16596 @item  General Purpose Run-Time Library (LIB interface)
16598 @item  Math Run-Time Library (MTH interface)
16600 @item  National Character Set Run-Time Library (NCS interface)
16602 @item  Compiled Code Support Run-Time Library (OTS interface)
16604 @item  Parallel Processing Run-Time Library (PPL interface)
16606 @item  Screen Management Run-Time Library (SMG interface)
16608 @item  Sort Run-Time Library (SOR interface)
16610 @item  String Run-Time Library (STR interface)
16612 @item STARLET System Library
16613 @findex Starlet
16615 @item  X Window System Version 11R4 and 11R5 (X, XLIB interface)
16617 @item  X Windows Toolkit (XT interface)
16619 @item  X/Motif Version 1.1.3 and 1.2 (XM interface)
16620 @end itemize
16622 @noindent
16623 GNAT provides implementations of these DEC bindings in the DECLIB directory.
16625 The X/Motif bindings used to build DECLIB are whatever versions are in the
16626 DEC Ada ADA$PREDEFINED directory with extension .ADC. The build script will
16627 automatically add a pragma Linker_Options to packages Xm, Xt, and X_Lib
16628 causing the default X/Motif shareable image libraries to be linked in. This
16629 is done via options files named xm.opt, xt.opt, and x_lib.opt (also located
16630 in the DECLIB directory).
16632 It may be necessary to edit these options files to update or correct the
16633 library names if, for example, the newer X/Motif bindings from ADA$EXAMPLES
16634 had been (previous to installing GNAT) copied and renamed to superseded the
16635 default ADA$PREDEFINED versions.
16637 @menu
16638 * Shared Libraries and Options Files::
16639 * Interfaces to C::
16640 @end menu
16642 @node Shared Libraries and Options Files
16643 @subsection Shared Libraries and Options Files
16645 @noindent
16646 When using the DEC Ada
16647 predefined X and Motif bindings, the linking with their shareable images is
16648 done automatically by GNAT LINK. When using other X and Motif bindings, it
16649 is necessary to add the corresponding shareable images to the command line for
16650 GNAT LINK. When linking with shared libraries, or with .OPT files, it is
16651 also necessary to add them to the command line for GNAT LINK.
16653 A shared library to be used with GNAT is built in the same way as other
16654 libraries under VMS. The VMS Link command can be used in standard fashion.
16656 @node Interfaces to C
16657 @subsection Interfaces to C
16659 @noindent
16660 DEC Ada
16661 provides the following Ada types and operations:
16663 @itemize @bullet
16664 @item C types package (C_TYPES)
16666 @item C strings (C_TYPES.NULL_TERMINATED)
16668 @item Other_types (SHORT_INT)
16669 @end itemize
16671 @noindent
16672 Interfacing to C with GNAT, one can use the above approach
16673 described for DEC Ada or the facilities of Annex B of
16674 the Ada 95 Reference Manual (packages INTERFACES.C,
16675 INTERFACES.C.STRINGS and INTERFACES.C.POINTERS). For more
16676 information, see the section "Interfacing to C" in the
16677 GNAT Reference Manual.
16679 The @option{/UPPERCASE_EXTERNALS} qualifier forces default and explicit
16680 @code{External_Name} parameters in pragmas Import and Export
16681 to be uppercased for compatibility with the default behavior
16682 of DEC C. The qualifier has no effect on @code{Link_Name} parameters.
16684 @node Main Program Definition
16685 @section Main Program Definition
16687 @noindent
16688 The following section discusses differences in the
16689 definition of main programs on DEC Ada and GNAT.
16690 On DEC Ada, main programs are defined to meet the
16691 following conditions:
16692 @itemize @bullet
16693 @item  Procedure with no formal parameters (returns 0 upon
16694        normal completion)
16696 @item  Procedure with no formal parameters (returns 42 when
16697        unhandled exceptions are raised)
16699 @item  Function with no formal parameters whose returned value
16700        is of a discrete type
16702 @item  Procedure with one OUT formal of a discrete type for
16703        which a specification of pragma EXPORT_VALUED_PROCEDURE is given.
16705 @end itemize
16707 @noindent
16708 When declared with the pragma EXPORT_VALUED_PROCEDURE,
16709 a main function or main procedure returns a discrete
16710 value whose size is less than 64 bits (32 on VAX systems),
16711 the value is zero- or sign-extended as appropriate.
16712 On GNAT, main programs are defined as follows:
16713 @itemize @bullet
16714 @item  Must be a non-generic, parameter-less subprogram that
16715 is either a procedure or function returning an Ada
16716 STANDARD.INTEGER (the predefined type)
16718 @item  Cannot be a generic subprogram or an instantiation of a
16719 generic subprogram
16720 @end itemize
16722 @node Implementation-Defined Attributes
16723 @section Implementation-Defined Attributes
16725 @noindent
16726 GNAT provides all DEC Ada implementation-defined
16727 attributes.
16729 @node Compiler and Run-Time Interfacing
16730 @section Compiler and Run-Time Interfacing
16732 @noindent
16733 DEC Ada provides the following ways to pass options to the linker (ACS LINK):
16734 @itemize @bullet
16735 @item  /WAIT and /SUBMIT qualifiers
16737 @item  /COMMAND qualifier
16739 @item  /[NO]MAP qualifier
16741 @item  /OUTPUT=file-spec
16743 @item  /[NO]DEBUG and /[NO]TRACEBACK qualifiers
16744 @end itemize
16746 @noindent
16747 To pass options to the linker, GNAT provides the following
16748 qualifiers:
16750 @itemize @bullet
16751 @item   /EXECUTABLE=exec-name
16753 @item   /VERBOSE qualifier
16755 @item   /[NO]DEBUG and /[NO]TRACEBACK qualifiers
16756 @end itemize
16758 @noindent
16759 For more information on these qualifiers, see the section
16760 "Qualifiers for GNAT LINK" in the corresponding section of this Guide.
16761 In DEC Ada, the command-line qualifier /OPTIMIZE is available
16762 to control optimization. DEC Ada also supplies the
16763 following pragmas:
16764 @itemize @bullet
16765 @item  OPTIMIZE
16767 @item  INLINE
16769 @item  INLINE_GENERIC
16771 @item  SUPPRESS_ALL
16773 @item  PASSIVE
16774 @end itemize
16776 @noindent
16777 In GNAT, optimization is controlled strictly by command
16778 line parameters, as described in the corresponding section of this guide.
16779 The DIGITAL pragmas for control of optimization are
16780 recognized but ignored.
16782 Note that in GNAT, the default is optimization off, whereas in DEC Ada 83,
16783 the default is that optimization is turned on.
16785 @node Program Compilation and Library Management
16786 @section Program Compilation and Library Management
16788 @noindent
16789 DEC Ada and GNAT provide a comparable set of commands to
16790 build programs. DEC Ada also provides a program library,
16791 which is a concept that does not exist on GNAT. Instead,
16792 GNAT provides directories of sources that are compiled as
16793 needed.
16795 The following table summarizes
16796 the DEC Ada commands and provides
16797 equivalent GNAT commands. In this table, some GNAT
16798 equivalents reflect the fact that GNAT does not use the
16799 concept of a program library. Instead, it uses a model
16800 in which collections of source and object files are used
16801 in a manner consistent with other languages like C and
16802 Fortran. Therefore, standard system file commands are used
16803 to manipulate these elements. Those GNAT commands are marked with
16804 an asterisk in the table that follows.
16805 Note that, unlike DEC Ada,  none of the GNAT commands accepts wild cards.
16807 @need 1500
16808 @multitable @columnfractions .31 .30 .39
16810 @item @strong{DEC_Ada_Command}
16811 @tab @strong{GNAT_Equivalent}
16812 @tab @strong{Description}
16814 @item  ADA
16815 @tab     GNAT COMPILE
16816 @tab     Invokes the compiler to compile one or more Ada source files.
16818 @item  ACS ATTACH
16819 @tab     No equivalent
16820 @tab     Qualifiers control of terminal from current process running the program
16821                                                 library manager.
16823 @item   ACS CHECK
16824 @tab      GNAT MAKE /DEPENDENCY_LIST
16825 @tab      Forms the execution closure of one
16826           or more compiled units and checks completeness and currency.
16828 @item   ACS COMPILE
16829 @tab      GNAT MAKE /ACTIONS=COMPILE
16830 @tab      Forms the execution closure of one or
16831           more specified units, checks completeness and currency,
16832           identifies units that have revised source files, compiles same,
16833           and recompiles units that are or will become obsolete.
16834           Also completes incomplete generic instantiations.
16836 @item   ACS COPY FOREIGN
16837 @tab      Copy (*)
16838 @tab      Copies a foreign object file into the program library as a
16839           library unit body.
16841 @item   ACS COPY UNIT
16842 @tab      Copy (*)
16843 @tab      Copies a compiled unit from one program library to another.
16845 @item   ACS CREATE LIBRARY
16846 @tab      Create /directory (*)
16847 @tab      Creates a program library.
16849 @item   ACS CREATE SUBLIBRARY
16850 @tab      Create /directory (*)
16851 @tab      Creates a program sublibrary.
16853 @item   ACS DELETE LIBRARY
16854 @tab
16855 @tab       Deletes a program library and its contents.
16857 @item   ACS DELETE SUBLIBRARY
16858 @tab
16859 @tab      Deletes a program sublibrary and its contents.
16861 @item   ACS DELETE UNIT
16862 @tab      Delete @i{file} (*)
16863 @tab      On OpenVMS systems, deletes one or more compiled units from
16864           the current program library.
16866 @item   ACS DIRECTORY
16867 @tab       Directory (*)
16868 @tab       On OpenVMS systems, lists units contained in the current
16869            program library.
16871 @item   ACS ENTER FOREIGN
16872 @tab      Copy (*)
16873 @tab      Allows the import of a foreign body as an Ada library
16874           specification and enters a reference to a pointer.
16876 @item   ACS ENTER UNIT
16877 @tab      Copy (*)
16878 @tab      Enters a reference (pointer) from the current program library to
16879           a unit compiled into another program library.
16881 @item   ACS EXIT
16882 @tab      No equivalent
16883 @tab      Exits from the program library manager.
16885 @item   ACS EXPORT
16886 @tab      Copy (*)
16887 @tab      Creates an object file that contains system-specific object code
16888           for one or more units. With GNAT, object files can simply be copied
16889           into the desired directory.
16891 @item   ACS EXTRACT SOURCE
16892 @tab      Copy (*)
16893 @tab      Allows access to the copied source file for each Ada compilation unit
16895 @item   ACS HELP
16896 @tab      HELP GNAT
16897 @tab      Provides online help.
16899 @item    ACS LINK
16900 @tab       GNAT LINK
16901 @tab       Links an object file containing Ada units into an executable
16902            file.
16904 @item    ACS LOAD
16905 @tab       Copy (*)
16906 @tab       Loads (partially compiles) Ada units into the program library.
16907            Allows loading a program from a collection of files into a library
16908            without knowing the relationship among units.
16910 @item    ACS MERGE
16911 @tab      Copy (*)
16912 @tab      Merges into the current program library, one or more units from
16913           another library where they were modified.
16915 @item    ACS RECOMPILE
16916 @tab       GNAT MAKE /ACTIONS=COMPILE
16917 @tab       Recompiles from   external or copied source files any obsolete
16918            unit in the closure. Also, completes any incomplete generic
16919            instantiations.
16921 @item    ACS REENTER
16922 @tab       GNAT MAKE
16923 @tab       Reenters current references to units compiled after last entered
16924            with the ACS ENTER UNIT command.
16926 @item    ACS SET LIBRARY
16927 @tab       Set default (*)
16928 @tab       Defines a program library to be the compilation context as well
16929            as the target library for compiler output and commands in general.
16931 @item    ACS SET PRAGMA
16932 @tab       Edit GNAT.ADC (*)
16933 @tab       Redefines specified  values of the library characteristics
16934             LONG_ FLOAT, MEMORY_SIZE, SYSTEM_NAME, and @code{Float_Representation}.
16936 @item    ACS SET SOURCE
16937 @tab       define @* ADA_INCLUDE_PATH @i{path} (*)
16938 @tab       Defines the source file search list for the ACS COMPILE  command.
16940 @item    ACS SHOW LIBRARY
16941 @tab       Directory (*)
16942 @tab        Lists information about one or more program libraries.
16944 @item    ACS SHOW PROGRAM
16945 @tab       No equivalent
16946 @tab       Lists information about the execution closure of one or
16947            more units in the program library.
16949 @item    ACS SHOW SOURCE
16950 @tab       Show logical @* ADA_INCLUDE_PATH
16951 @tab       Shows the source file search used when compiling units.
16953 @item    ACS SHOW VERSION
16954 @tab       Compile with VERBOSE option
16955 @tab       Displays the version number of the compiler and program library
16956            manager used.
16958 @item    ACS SPAWN
16959 @tab        No equivalent
16960 @tab        Creates a subprocess of the current process (same as DCL SPAWN
16961             command).
16963 @item    ACS VERIFY
16964 @tab       No equivalent
16965 @tab       Performs a series of consistency checks on a program library to
16966            determine whether the library structure and library files are in
16967            valid_form.
16969 @end multitable
16971 @noindent
16973 @node Input-Output
16974 @section Input-Output
16976 @noindent
16977 On OpenVMS Alpha systems, DEC Ada uses OpenVMS Record
16978 Management Services (RMS) to perform operations on
16979 external files.
16981 @noindent
16982 DEC Ada and GNAT predefine an identical set of input-
16983 output packages. To make the use of the
16984 generic TEXT_IO operations more convenient, DEC Ada
16985 provides predefined library packages that instantiate the
16986 integer and floating-point operations for the predefined
16987 integer and floating-point types as shown in the following table.
16989 @table @code
16991 @item   Package_Name
16992       Instantiation
16994 @item   INTEGER_TEXT_IO
16995       INTEGER_IO(INTEGER)
16997 @item   SHORT_INTEGER_TEXT_IO
16998      INTEGER_IO(SHORT_INTEGER)
17000 @item   SHORT_SHORT_INTEGER_TEXT_IO
17001      INTEGER_IO(SHORT_SHORT_ INTEGER)
17003 @item   FLOAT_TEXT_IO
17004       FLOAT_IO(FLOAT)
17006 @item   LONG_FLOAT_TEXT_IO
17007       FLOAT_IO(LONG_FLOAT)
17008 @end table
17010 @noindent
17011 The DEC Ada predefined packages and their operations
17012 are implemented using OpenVMS Alpha files and input-
17013 output facilities. DEC Ada supports asynchronous input-
17014 output on OpenVMS Alpha. Familiarity with the following is
17015 recommended:
17016 @itemize @bullet
17017 @item  RMS file organizations and access methods
17019 @item  OpenVMS file specifications and directories
17021 @item  OpenVMS File Definition Language (FDL)
17022 @end itemize
17024 @noindent
17025 GNAT provides I/O facilities that are completely
17026 compatible with DEC Ada. The distribution includes the
17027 standard DEC Ada versions of all I/O packages, operating
17028 in a manner compatible with DEC Ada. In particular, the
17029 following packages are by default the DEC Ada (Ada 83)
17030 versions of these packages rather than the renamings
17031 suggested in annex J of the Ada 95 Reference Manual:
17032 @itemize @bullet
17033 @item  TEXT_IO
17035 @item  SEQUENTIAL_IO
17037 @item  DIRECT_IO
17038 @end itemize
17040 @noindent
17041 The use of the standard Ada 95 syntax for child packages (for
17042 example, ADA.TEXT_IO) retrieves the Ada 95 versions of these
17043 packages, as defined in the Ada 95 Reference Manual.
17044 GNAT provides DIGITAL-compatible predefined instantiations
17045 of the TEXT_IO packages, and also
17046 provides the standard predefined instantiations required
17047 by the Ada 95 Reference Manual.
17049 For further information on how GNAT interfaces to the file
17050 system or how I/O is implemented in programs written in
17051 mixed languages, see the chapter "Implementation of the
17052 Standard I/O" in the GNAT Reference Manual.
17053 This chapter covers the following:
17054 @itemize @bullet
17055 @item  Standard I/O packages
17057 @item  FORM strings
17059 @item  DIRECT_IO
17061 @item  SEQUENTIAL_IO
17063 @item  TEXT_IO
17065 @item  Stream pointer positioning
17067 @item  Reading and writing non-regular files
17069 @item  GET_IMMEDIATE
17071 @item  Treating TEXT_IO files as streams
17073 @item  Shared files
17075 @item  Open modes
17076 @end itemize
17078 @node Implementation Limits
17079 @section Implementation Limits
17081 @noindent
17082 The following table lists implementation limits for DEC Ada and GNAT systems.
17083 @multitable @columnfractions .60 .20 .20
17084 @item  Compilation Parameter
17085 @tab   DEC Ada
17086 @tab   GNAT
17088 @item  In a subprogram or entry  declaration, maximum number of
17089        formal parameters that are of an unconstrained record type
17090 @tab   32
17091 @tab   No set limit
17093 @item  Maximum identifier length (number of characters)
17094 @tab   255
17095 @tab   255
17097 @item  Maximum number of characters in a source line
17098 @tab   255
17099 @tab   255
17101 @item  Maximum collection size   (number of bytes)
17102 @tab   2**31-1
17103 @tab   2**31-1
17105 @item  Maximum number of discriminants for a record type
17106 @tab   245
17107 @tab   No set limit
17109 @item  Maximum number of formal parameters in an entry or
17110        subprogram declaration
17111 @tab   246
17112 @tab    No set limit
17114 @item  Maximum number of dimensions in an array type
17115 @tab   255
17116 @tab   No set limit
17118 @item  Maximum number of library  units and subunits in a compilation.
17119 @tab   4095
17120 @tab   No set limit
17122 @item  Maximum number of library units and subunits in an execution.
17123 @tab   16383
17124 @tab   No set limit
17126 @item  Maximum number of objects declared with the pragma COMMON_OBJECT
17127        or PSECT_OBJECT
17128 @tab   32757
17129 @tab   No set limit
17131 @item  Maximum number of enumeration literals in an enumeration type
17132        definition
17133 @tab   65535
17134 @tab   No set limit
17136 @item  Maximum number of lines in a source file
17137 @tab   65534
17138 @tab   No set limit
17140 @item  Maximum number of bits in any object
17141 @tab   2**31-1
17142 @tab   2**31-1
17144 @item  Maximum size of the static portion of a stack frame (approximate)
17145 @tab   2**31-1
17146 @tab   2**31-1
17147 @end multitable
17149 @node  Tools
17150 @section Tools
17153 @node Inline Assembler
17154 @chapter Inline Assembler
17156 @noindent
17157 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:
17159 @itemize @bullet
17160 @item No need to use non-Ada tools
17161 @item Consistent interface over different targets
17162 @item Automatic usage of the proper calling conventions
17163 @item Access to Ada constants and variables
17164 @item Definition of intrinsic routines
17165 @item Possibility of inlining a subprogram comprising assembler code
17166 @item Code optimizer can take Inline Assembler code into account
17167 @end itemize
17169 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.
17171 @menu
17172 * Basic Assembler Syntax::
17173 * A Simple Example of Inline Assembler::
17174 * Output Variables in Inline Assembler::
17175 * Input Variables in Inline Assembler::
17176 * Inlining Inline Assembler Code::
17177 * Other Asm Functionality::
17178 * A Complete Example::
17179 @end menu
17181 @c ---------------------------------------------------------------------------
17182 @node Basic Assembler Syntax
17183 @section Basic Assembler Syntax
17185 @noindent
17186 The assembler used by GNAT and GNAT COMPILE is based not on the Intel assembly language, but rather on a
17187 language that descends from the AT&T Unix assembler @emph{as} (and which is often
17188 referred to as ``AT&T syntax'').
17189 The following table summarizes the main features of @emph{as} syntax and points out the differences from the Intel conventions.
17190 See the GNAT COMPILE @emph{as} and @emph{gas} (an @emph{as} macro
17191 pre-processor) documentation for further information.
17193 @table @asis
17194 @item Register names
17195 GNAT COMPILE / @emph{as}: Prefix with ``%''; for example @code{%eax}
17197 Intel: No extra punctuation; for example @code{eax}
17199 @item Immediate operand
17200 GNAT COMPILE / @emph{as}: Prefix with ``$''; for example @code{$4}
17202 Intel: No extra punctuation; for example @code{4}
17204 @item Address
17205 GNAT COMPILE / @emph{as}: Prefix with ``$''; for example @code{$loc}
17207 Intel: No extra punctuation; for example @code{loc}
17209 @item Memory contents
17210 GNAT COMPILE / @emph{as}: No extra punctuation; for example @code{loc}
17212 Intel: Square brackets; for example @code{[loc]}
17214 @item Register contents
17215 GNAT COMPILE / @emph{as}: Parentheses; for example @code{(%eax)}
17217 Intel: Square brackets; for example @code{[eax]}
17219 @item Hexadecimal numbers
17220 GNAT COMPILE / @emph{as}: Leading ``0x'' (C language syntax); for example @code{0xA0}
17222 Intel: Trailing ``h''; for example @code{A0h}
17224 @item Operand size
17225 GNAT COMPILE / @emph{as}: Explicit in op code; for example @code{movw} to move a 16-bit word
17227 Intel: Implicit, deduced by assembler; for example @code{mov}
17229 @item Instruction repetition
17230 GNAT COMPILE / @emph{as}: Split into two lines; for example
17232 @code{rep}
17234 @code{stosl}
17236 Intel: Keep on one line; for example @code{rep stosl}
17238 @item Order of operands
17239 GNAT COMPILE / @emph{as}: Source first; for example @code{movw $4, %eax}
17241 Intel: Destination first; for example @code{mov eax, 4}
17242 @end table
17244 @c ---------------------------------------------------------------------------
17245 @node A Simple Example of Inline Assembler
17246 @section A Simple Example of Inline Assembler
17248 @noindent
17249 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.
17251 @smallexample
17252 @group
17253 with System.Machine_Code; use System.Machine_Code;
17254 procedure Nothing is
17255 begin
17256    Asm ("nop");
17257 end Nothing;
17258 @end group
17259 @end smallexample
17261 @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.
17262 @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.
17264 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}.
17266 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:
17267 @smallexample
17268 GNAT MAKE nothing
17269 @end smallexample
17270 However, the interesting aspect of this example is not its run-time behavior but rather the
17271 generated assembly code.  To see this output, invoke the compiler as follows:
17272 @smallexample
17273    GNAT COMPILE -S -fomit-frame-pointer /CHECKS=SUPPRESS_ALL @file{NOTHING.ADB}
17274 @end smallexample
17275 where the options are:
17277 @table @code
17278 @item -c
17279 compile only (no bind or link)
17280 @item -S
17281 generate assembler listing
17282 @item -fomit-frame-pointer
17283 do not set up separate stack frames
17284 @item /CHECKS=SUPPRESS_ALL
17285 do not add runtime checks
17286 @end table
17288 This gives a human-readable assembler version of the code. The resulting
17289 file will have the same name as the Ada source file, but with a @code{.s} extension.
17290 In our example, the file @file{nothing.s} has the following contents:
17292 @smallexample
17293 @group
17294 .file "NOTHING.ADB"
17295 gcc2_compiled.:
17296 ___gnu_compiled_ada:
17297 .text
17298    .align 4
17299 .globl __ada_nothing
17300 __ada_nothing:
17301 #APP
17302    nop
17303 #NO_APP
17304    jmp L1
17305    .align 2,0x90
17307    ret
17308 @end group
17309 @end smallexample
17311 The assembly code you included is clearly indicated by
17312 the compiler, between the @code{#APP} and @code{#NO_APP}
17313 delimiters. The character before the 'APP' and 'NOAPP'
17314 can differ on different targets. For example, Linux uses '#APP' while
17315 on NT you will see '/APP'.
17317 If you make a mistake in your assembler code (such as using the
17318 wrong size modifier, or using a wrong operand for the instruction) GNAT
17319 will report this error in a temporary file, which will be deleted when
17320 the compilation is finished.  Generating an assembler file will help
17321 in such cases, since you can assemble this file separately using the
17322 @emph{as} assembler that comes with GNAT COMPILE.
17324 Assembling the file using the command
17326 @smallexample
17327 as @file{nothing.s}
17328 @end smallexample
17329 @noindent
17330 will give you error messages whose lines correspond to the assembler
17331 input file, so you can easily find and correct any mistakes you made.
17332 If there are no errors, @emph{as} will generate an object file @file{nothing.out}.
17334 @c ---------------------------------------------------------------------------
17335 @node Output Variables in Inline Assembler
17336 @section Output Variables in Inline Assembler
17338 @noindent
17339 The examples in this section, showing how to access the processor flags, illustrate how to specify the destination operands for assembly language statements.
17341 @smallexample
17342 @group
17343 with Interfaces; use Interfaces;
17344 with Ada.Text_IO; use Ada.Text_IO;
17345 with System.Machine_Code; use System.Machine_Code;
17346 procedure Get_Flags is
17347    Flags : Unsigned_32;
17348    use ASCII;
17349 begin
17350    Asm ("pushfl"          & LF & HT & -- push flags on stack
17351         "popl %%eax"      & LF & HT & -- load eax with flags
17352         "movl %%eax, %0",             -- store flags in variable
17353         Outputs => Unsigned_32'Asm_Output ("=g", Flags));
17354    Put_Line ("Flags register:" & Flags'Img);
17355 end Get_Flags;
17356 @end group
17357 @end smallexample
17359 In order to have a nicely aligned assembly listing, we have separated
17360 multiple assembler statements in the Asm template string with linefeed (ASCII.LF)
17361 and horizontal tab (ASCII.HT) characters.  The resulting section of the
17362 assembly output file is:
17364 @smallexample
17365 @group
17366 #APP
17367    pushfl
17368    popl %eax
17369    movl %eax, -40(%ebp)
17370 #NO_APP
17371 @end group
17372 @end smallexample
17374 It would have been legal to write the Asm invocation as:
17376 @smallexample
17377 Asm ("pushfl popl %%eax movl %%eax, %0")
17378 @end smallexample
17380 but in the generated assembler file, this would come out as:
17382 @smallexample
17383 #APP
17384    pushfl popl %eax movl %eax, -40(%ebp)
17385 #NO_APP
17386 @end smallexample
17388 which is not so convenient for the human reader.
17390 We use Ada comments
17391 at the end of each line to explain what the assembler instructions
17392 actually do.  This is a useful convention.
17394 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.
17396 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}.
17397 An output variable is illustrated in
17398 the third statement in the Asm template string:
17399 @smallexample
17400 movl %%eax, %0
17401 @end smallexample
17402 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.
17404 Information about the output variable is supplied in the @code{Outputs} parameter to @code{Asm}:
17405 @smallexample
17406 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
17407 @end smallexample
17409 The output is defined by the @code{Asm_Output} attribute of the target type; the general format is
17410 @smallexample
17411 Type'Asm_Output (constraint_string, variable_name)
17412 @end smallexample
17414 The constraint string directs the compiler how
17415 to store/access the associated variable.  In the example
17416 @smallexample
17417 Unsigned_32'Asm_Output ("=m", Flags);
17418 @end smallexample
17419 the @code{"m"} (memory) constraint tells the compiler that the variable
17420 @code{Flags} should be stored in a memory variable, thus preventing
17421 the optimizer from keeping it in a register.  In contrast,
17422 @smallexample
17423 Unsigned_32'Asm_Output ("=r", Flags);
17424 @end smallexample
17425 uses the @code{"r"} (register) constraint, telling the compiler to
17426 store the variable in a register.
17428 If the constraint is preceded by the equal character (@strong{=}), it tells the
17429 compiler that the variable will be used to store data into it.
17431 In the @code{Get_Flags} example, we used the "g" (global) constraint, allowing the optimizer
17432 to choose whatever it deems best.
17434 There are a fairly large number of constraints, but the ones that are most useful (for the Intel x86 processor) are the following:
17436 @table @code
17437 @item =
17438 output constraint
17439 @item g
17440 global (i.e. can be stored anywhere)
17441 @item m
17442 in memory
17443 @item I
17444 a constant
17445 @item a
17446 use eax
17447 @item b
17448 use ebx
17449 @item c
17450 use ecx
17451 @item d
17452 use edx
17453 @item S
17454 use esi
17455 @item D
17456 use edi
17457 @item r
17458 use one of eax, ebx, ecx or edx
17459 @item q
17460 use one of eax, ebx, ecx, edx, esi or edi
17461 @end table
17463 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.
17465 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
17466 @smallexample
17467 @group
17468 Asm ("pushfl"          & LF & HT & -- push flags on stack
17469      "popl %%eax"      & LF & HT & -- load eax with flags
17470      "movl %%eax, %0",             -- store flags in variable
17471      Outputs => Unsigned_32'Asm_Output ("=g", Flags));
17472 @end group
17473 @end smallexample
17474 @noindent
17475 @code{%0} will be replaced in the expanded code by the appropriate operand,
17476 whatever
17477 the compiler decided for the @code{Flags} variable.
17479 In general, you may have any number of output variables:
17480 @itemize @bullet
17481 @item
17482 Count the operands starting at 0; thus @code{%0}, @code{%1}, etc.
17483 @item
17484 Specify the @code{Outputs} parameter as a parenthesized comma-separated list of @code{Asm_Output} attributes
17485 @end itemize
17487 For example:
17488 @smallexample
17489 @group
17490 Asm ("movl %%eax, %0" & LF & HT &
17491      "movl %%ebx, %1" & LF & HT &
17492      "movl %%ecx, %2",
17493      Outputs => (Unsigned_32'Asm_Output ("=g", Var_A),   --  %0 = Var_A
17494                  Unsigned_32'Asm_Output ("=g", Var_B),   --  %1 = Var_B
17495                  Unsigned_32'Asm_Output ("=g", Var_C))); --  %2 = Var_C
17496 @end group
17497 @end smallexample
17498 @noindent
17499 where @code{Var_A}, @code{Var_B}, and @code{Var_C} are variables in the Ada program.
17501 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:
17503 @smallexample
17504 @group
17505 with Interfaces; use Interfaces;
17506 with Ada.Text_IO; use Ada.Text_IO;
17507 with System.Machine_Code; use System.Machine_Code;
17508 procedure Get_Flags_2 is
17509    Flags : Unsigned_32;
17510    use ASCII;
17511 begin
17512    Asm ("pushfl"      & LF & HT & -- push flags on stack
17513         "popl %%eax",             -- save flags in eax
17514         Outputs => Unsigned_32'Asm_Output ("=a", Flags));
17515    Put_Line ("Flags register:" & Flags'Img);
17516 end Get_Flags_2;
17517 @end group
17518 @end smallexample
17520 @noindent
17521 The @code{"a"} constraint tells the compiler that the @code{Flags}
17522 variable will come from the eax register. Here is the resulting code:
17524 @smallexample
17525 @group
17526 #APP
17527    pushfl
17528    popl %eax
17529 #NO_APP
17530    movl %eax,-40(%ebp)
17531 @end group
17532 @end smallexample
17534 @noindent
17535 The compiler generated the store of eax into Flags after
17536 expanding the assembler code.
17538 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:
17540 @smallexample
17541 @group
17542 with Interfaces; use Interfaces;
17543 with Ada.Text_IO; use Ada.Text_IO;
17544 with System.Machine_Code; use System.Machine_Code;
17545 procedure Get_Flags_3 is
17546    Flags : Unsigned_32;
17547    use ASCII;
17548 begin
17549    Asm ("pushfl"  & LF & HT & -- push flags on stack
17550         "pop %0",             -- save flags in Flags
17551         Outputs => Unsigned_32'Asm_Output ("=g", Flags));
17552    Put_Line ("Flags register:" & Flags'Img);
17553 end Get_Flags_3;
17554 @end group
17555 @end smallexample
17557 @c ---------------------------------------------------------------------------
17558 @node Input Variables in Inline Assembler
17559 @section Input Variables in Inline Assembler
17561 @noindent
17562 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:
17564 @smallexample
17565 @group
17566 with Interfaces; use Interfaces;
17567 with Ada.Text_IO; use Ada.Text_IO;
17568 with System.Machine_Code; use System.Machine_Code;
17569 procedure Increment is
17571    function Incr (Value : Unsigned_32) return Unsigned_32 is
17572       Result : Unsigned_32;
17573    begin
17574       Asm ("incl %0",
17575            Inputs  => Unsigned_32'Asm_Input ("a", Value),
17576            Outputs => Unsigned_32'Asm_Output ("=a", Result));
17577       return Result;
17578    end Incr;
17580    Value : Unsigned_32;
17582 begin
17583    Value := 5;
17584    Put_Line ("Value before is" & Value'Img);
17585    Value := Incr (Value);
17586    Put_Line ("Value after is" & Value'Img);
17587 end Increment;
17588 @end group
17589 @end smallexample
17591 The @code{Outputs} parameter to @code{Asm} specifies
17592 that the result will be in the eax register and that it is to be stored in the @code{Result}
17593 variable.
17595 The @code{Inputs} parameter looks much like the @code{Outputs} parameter, but with an
17596 @code{Asm_Input} attribute. The
17597 @code{"="} constraint, indicating an output value, is not present.
17599 You can have multiple input variables, in the same way that you can have more
17600 than one output variable.
17602 The parameter count (%0, %1) etc, now starts at the first input
17603 statement, and continues with the output statements.
17604 When both parameters use the same variable, the
17605 compiler will treat them as the same %n operand, which is the case here.
17607 Just as the @code{Outputs} parameter causes the register to be stored into the
17608 target variable after execution of the assembler statements, so does the
17609 @code{Inputs} parameter cause its variable to be loaded into the register before execution
17610 of the
17611 assembler statements.
17613 Thus the effect of the @code{Asm} invocation is:
17614 @enumerate
17615 @item load the 32-bit value of @code{Value} into eax
17616 @item execute the @code{incl %eax} instruction
17617 @item store the contents of eax into the @code{Result} variable
17618 @end enumerate
17620 The resulting assembler file (with @code{/OPTIMIZE=ALL} optimization) contains:
17621 @smallexample
17622 @group
17623 _increment__incr.1:
17624    subl $4,%esp
17625    movl 8(%esp),%eax
17626 #APP
17627    incl %eax
17628 #NO_APP
17629    movl %eax,%edx
17630    movl %ecx,(%esp)
17631    addl $4,%esp
17632    ret
17633 @end group
17634 @end smallexample
17636 @c ---------------------------------------------------------------------------
17637 @node Inlining Inline Assembler Code
17638 @section Inlining Inline Assembler Code
17640 @noindent
17641 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)
17642 can be significant, compared to the amount of code in the subprogram body.
17643 A solution is to apply Ada's @code{Inline} pragma to the subprogram,
17644 which directs the compiler to expand invocations of the subprogram at the point(s)
17645 of call, instead of setting up a stack frame for out-of-line calls.
17646 Here is the resulting program:
17648 @smallexample
17649 @group
17650 with Interfaces; use Interfaces;
17651 with Ada.Text_IO; use Ada.Text_IO;
17652 with System.Machine_Code; use System.Machine_Code;
17653 procedure Increment_2 is
17655    function Incr (Value : Unsigned_32) return Unsigned_32 is
17656       Result : Unsigned_32;
17657    begin
17658       Asm ("incl %0",
17659            Inputs  => Unsigned_32'Asm_Input ("a", Value),
17660            Outputs => Unsigned_32'Asm_Output ("=a", Result));
17661       return Result;
17662    end Incr;
17663    pragma Inline (Increment);
17665    Value : Unsigned_32;
17667 begin
17668    Value := 5;
17669    Put_Line ("Value before is" & Value'Img);
17670    Value := Increment (Value);
17671    Put_Line ("Value after is" & Value'Img);
17672 end Increment_2;
17673 @end group
17674 @end smallexample
17676 Compile the program with both optimization (@code{/OPTIMIZE=ALL}) and inlining
17677 enabled (@option{-gnatpn} instead of @option{/CHECKS=SUPPRESS_ALL}).
17679 The @code{Incr} function is still compiled as usual, but at the
17680 point in @code{Increment} where our function used to be called:
17682 @smallexample
17683 @group
17684 pushl %edi
17685 call _increment__incr.1
17686 @end group
17687 @end smallexample
17689 @noindent
17690 the code for the function body directly appears:
17692 @smallexample
17693 @group
17694 movl %esi,%eax
17695 #APP
17696    incl %eax
17697 #NO_APP
17698    movl %eax,%edx
17699 @end group
17700 @end smallexample
17702 @noindent
17703 thus saving the overhead of stack frame setup and an out-of-line call.
17705 @c ---------------------------------------------------------------------------
17706 @node Other Asm Functionality
17707 @section Other @code{Asm} Functionality
17709 @noindent
17710 This section describes two important parameters to the @code{Asm} procedure: @code{Clobber}, which identifies register usage; and @code{Volatile}, which inhibits unwanted optimizations.
17712 @menu
17713 * The Clobber Parameter::
17714 * The Volatile Parameter::
17715 @end menu
17717 @c ---------------------------------------------------------------------------
17718 @node The Clobber Parameter
17719 @subsection The @code{Clobber} Parameter
17721 @noindent
17722 One of the dangers of intermixing assembly language and a compiled language such as Ada is
17723 that the compiler needs to be aware of which registers are being used by the assembly code.
17724 In some cases, such as the earlier examples, the constraint string is sufficient to
17725 indicate register usage (e.g. "a" for the eax register).  But more generally, the
17726 compiler needs an explicit identification of the registers that are used by the Inline
17727 Assembly statements.
17729 Using a register that the compiler doesn't know about
17730 could be a side effect of an instruction (like @code{mull}
17731 storing its result in both eax and edx).
17732 It can also arise from explicit register usage in your
17733 assembly code; for example:
17734 @smallexample
17735 @group
17736 Asm ("movl %0, %%ebx" & LF & HT &
17737      "movl %%ebx, %1",
17738      Inputs  => Unsigned_32'Asm_Input  ("g", Var_In),
17739      Outputs => Unsigned_32'Asm_Output ("=g", Var_Out));
17740 @end group
17741 @end smallexample
17742 @noindent
17743 where the compiler (since it does not analyze the @code{Asm} template string)
17744 does not know you are using the ebx register.
17746 In such cases you need to supply the @code{Clobber} parameter to @code{Asm},
17747 to identify the registers that will be used by your assembly code:
17749 @smallexample
17750 @group
17751 Asm ("movl %0, %%ebx" & LF & HT &
17752      "movl %%ebx, %1",
17753      Inputs  => Unsigned_32'Asm_Input  ("g", Var_In),
17754      Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
17755      Clobber => "ebx");
17756 @end group
17757 @end smallexample
17759 The Clobber parameter is a static string expression specifying the
17760 register(s) you are using.  Note that register names are @emph{not} prefixed by a percent sign.
17761 Also, if more than one register is used then their names are separated by commas; e.g., @code{"eax, ebx"}
17763 The @code{Clobber} parameter has several additional uses:
17764 @enumerate
17765 @item Use the "register" name @code{cc} to indicate that flags might have changed
17766 @item Use the "register" name @code{memory} if you changed a memory location
17767 @end enumerate
17769 @c ---------------------------------------------------------------------------
17770 @node The Volatile Parameter
17771 @subsection The @code{Volatile} Parameter
17772 @cindex Volatile parameter
17774 @noindent
17775 Compiler optimizations in the presence of Inline Assembler may sometimes have unwanted effects.
17776 For example, when
17777 an @code{Asm} invocation with an input variable is inside a loop, the compiler might move
17778 the loading of the input variable outside the loop, regarding it as a
17779 one-time initialization.
17781 If this effect is not desired, you can disable such optimizations by setting the
17782 @code{Volatile} parameter to @code{True}; for example:
17784 @smallexample
17785 @group
17786 Asm ("movl %0, %%ebx" & LF & HT &
17787      "movl %%ebx, %1",
17788      Inputs   => Unsigned_32'Asm_Input  ("g", Var_In),
17789      Outputs  => Unsigned_32'Asm_Output ("=g", Var_Out),
17790      Clobber  => "ebx",
17791      Volatile => True);
17792 @end group
17793 @end smallexample
17795 By default, @code{Volatile} is set to @code{False} unless there is no @code{Outputs}
17796 parameter.
17798 Although setting @code{Volatile} to @code{True} prevents unwanted optimizations,
17799 it will also disable other optimizations that might be important for efficiency.
17800 In general, you should set @code{Volatile} to @code{True} only if the compiler's
17801 optimizations have created problems.
17803 @c ---------------------------------------------------------------------------
17804 @node A Complete Example
17805 @section A Complete Example
17807 @noindent
17808 This section contains a complete program illustrating a realistic usage of GNAT's Inline Assembler
17809 capabilities.  It comprises a main procedure @code{Check_CPU} and a package @code{Intel_CPU}.
17810 The package declares a collection of functions that detect the properties of the 32-bit
17811 x86 processor that is running the program.  The main procedure invokes these functions
17812 and displays the information.
17814 The Intel_CPU package could be enhanced by adding functions to
17815 detect the type of x386 co-processor, the processor caching options and
17816 special operations such as the SIMD extensions.
17818 Although the Intel_CPU package has been written for 32-bit Intel
17819 compatible CPUs, it is OS neutral. It has been tested on DOS,
17820 Windows/NT and Linux.
17822 @menu
17823 * Check_CPU Procedure::
17824 * Intel_CPU Package Specification::
17825 * Intel_CPU Package Body::
17826 @end menu
17828 @c ---------------------------------------------------------------------------
17829 @node Check_CPU Procedure
17830 @subsection @code{Check_CPU} Procedure
17831 @cindex Check_CPU procedure
17833 @smallexample
17834 ---------------------------------------------------------------------
17835 --                                                                 --
17836 --  Uses the Intel_CPU package to identify the CPU the program is  --
17837 --  running on, and some of the features it supports.              --
17838 --                                                                 --
17839 ---------------------------------------------------------------------
17841 with Intel_CPU;                     --  Intel CPU detection functions
17842 with Ada.Text_IO;                   --  Standard text I/O
17843 with Ada.Command_Line;              --  To set the exit status
17845 procedure Check_CPU is
17847    Type_Found : Boolean := False;
17848    --  Flag to indicate that processor was identified
17850    Features   : Intel_CPU.Processor_Features;
17851    --  The processor features
17853    Signature  : Intel_CPU.Processor_Signature;
17854    --  The processor type signature
17856 begin
17858    -----------------------------------
17859    --  Display the program banner.  --
17860    -----------------------------------
17862    Ada.Text_IO.Put_Line (Ada.Command_Line.Command_Name &
17863                          ": check Intel CPU version and features, v1.0");
17864    Ada.Text_IO.Put_Line ("distribute freely, but no warranty whatsoever");
17865    Ada.Text_IO.New_Line;
17867    -----------------------------------------------------------------------
17868    --  We can safely start with the assumption that we are on at least  --
17869    --  a x386 processor. If the CPUID instruction is present, then we   --
17870    --  have a later processor type.                                     --
17871    -----------------------------------------------------------------------
17873    if Intel_CPU.Has_CPUID = False then
17875       --  No CPUID instruction, so we assume this is indeed a x386
17876       --  processor. We can still check if it has a FP co-processor.
17877       if Intel_CPU.Has_FPU then
17878          Ada.Text_IO.Put_Line
17879            ("x386-type processor with a FP co-processor");
17880       else
17881          Ada.Text_IO.Put_Line
17882            ("x386-type processor without a FP co-processor");
17883       end if;  --  check for FPU
17885       --  Program done
17886       Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Success);
17887       return;
17889    end if;  --  check for CPUID
17891    -----------------------------------------------------------------------
17892    --  If CPUID is supported, check if this is a true Intel processor,  --
17893    --  if it is not, display a warning.                                 --
17894    -----------------------------------------------------------------------
17896    if Intel_CPU.Vendor_ID /= Intel_CPU.Intel_Processor then
17897       Ada.Text_IO.Put_Line ("*** This is a Intel compatible processor");
17898       Ada.Text_IO.Put_Line ("*** Some information may be incorrect");
17899    end if;  --  check if Intel
17901    ----------------------------------------------------------------------
17902    --  With the CPUID instruction present, we can assume at least a    --
17903    --  x486 processor. If the CPUID support level is < 1 then we have  --
17904    --  to leave it at that.                                            --
17905    ----------------------------------------------------------------------
17907    if Intel_CPU.CPUID_Level < 1 then
17909       --  Ok, this is a x486 processor. we still can get the Vendor ID
17910       Ada.Text_IO.Put_Line ("x486-type processor");
17911       Ada.Text_IO.Put_Line ("Vendor ID is " & Intel_CPU.Vendor_ID);
17913       --  We can also check if there is a FPU present
17914       if Intel_CPU.Has_FPU then
17915          Ada.Text_IO.Put_Line ("Floating-Point support");
17916       else
17917          Ada.Text_IO.Put_Line ("No Floating-Point support");
17918       end if;  --  check for FPU
17920       --  Program done
17921       Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Success);
17922       return;
17924    end if;  --  check CPUID level
17926    ---------------------------------------------------------------------
17927    --  With a CPUID level of 1 we can use the processor signature to  --
17928    --  determine it's exact type.                                     --
17929    ---------------------------------------------------------------------
17931    Signature := Intel_CPU.Signature;
17933    ----------------------------------------------------------------------
17934    --  Ok, now we go into a lot of messy comparisons to get the        --
17935    --  processor type. For clarity, no attememt to try to optimize the --
17936    --  comparisons has been made. Note that since Intel_CPU does not   --
17937    --  support getting cache info, we cannot distinguish between P5    --
17938    --  and Celeron types yet.                                          --
17939    ----------------------------------------------------------------------
17941    --  x486SL
17942    if Signature.Processor_Type = 2#00#   and
17943      Signature.Family          = 2#0100# and
17944      Signature.Model           = 2#0100# then
17945       Type_Found := True;
17946       Ada.Text_IO.Put_Line ("x486SL processor");
17947    end if;
17949    --  x486DX2 Write-Back
17950    if Signature.Processor_Type = 2#00#   and
17951      Signature.Family          = 2#0100# and
17952      Signature.Model           = 2#0111# then
17953       Type_Found := True;
17954       Ada.Text_IO.Put_Line ("Write-Back Enhanced x486DX2 processor");
17955    end if;
17957    --  x486DX4
17958    if Signature.Processor_Type = 2#00#   and
17959      Signature.Family          = 2#0100# and
17960      Signature.Model           = 2#1000# then
17961       Type_Found := True;
17962       Ada.Text_IO.Put_Line ("x486DX4 processor");
17963    end if;
17965    --  x486DX4 Overdrive
17966    if Signature.Processor_Type = 2#01#   and
17967      Signature.Family          = 2#0100# and
17968      Signature.Model           = 2#1000# then
17969       Type_Found := True;
17970       Ada.Text_IO.Put_Line ("x486DX4 OverDrive processor");
17971    end if;
17973    --  Pentium (60, 66)
17974    if Signature.Processor_Type = 2#00#   and
17975      Signature.Family          = 2#0101# and
17976      Signature.Model           = 2#0001# then
17977       Type_Found := True;
17978       Ada.Text_IO.Put_Line ("Pentium processor (60, 66)");
17979    end if;
17981    --  Pentium (75, 90, 100, 120, 133, 150, 166, 200)
17982    if Signature.Processor_Type = 2#00#   and
17983      Signature.Family          = 2#0101# and
17984      Signature.Model           = 2#0010# then
17985       Type_Found := True;
17986       Ada.Text_IO.Put_Line
17987         ("Pentium processor (75, 90, 100, 120, 133, 150, 166, 200)");
17988    end if;
17990    --  Pentium OverDrive (60, 66)
17991    if Signature.Processor_Type = 2#01#   and
17992      Signature.Family          = 2#0101# and
17993      Signature.Model           = 2#0001# then
17994       Type_Found := True;
17995       Ada.Text_IO.Put_Line ("Pentium OverDrive processor (60, 66)");
17996    end if;
17998    --  Pentium OverDrive (75, 90, 100, 120, 133, 150, 166, 200)
17999    if Signature.Processor_Type = 2#01#   and
18000      Signature.Family          = 2#0101# and
18001      Signature.Model           = 2#0010# then
18002       Type_Found := True;
18003       Ada.Text_IO.Put_Line
18004         ("Pentium OverDrive cpu (75, 90, 100, 120, 133, 150, 166, 200)");
18005    end if;
18007    --  Pentium OverDrive processor for x486 processor-based systems
18008    if Signature.Processor_Type = 2#01#   and
18009      Signature.Family          = 2#0101# and
18010      Signature.Model           = 2#0011# then
18011       Type_Found := True;
18012       Ada.Text_IO.Put_Line
18013         ("Pentium OverDrive processor for x486 processor-based systems");
18014    end if;
18016    --  Pentium processor with MMX technology (166, 200)
18017    if Signature.Processor_Type = 2#00#   and
18018      Signature.Family          = 2#0101# and
18019      Signature.Model           = 2#0100# then
18020       Type_Found := True;
18021       Ada.Text_IO.Put_Line
18022         ("Pentium processor with MMX technology (166, 200)");
18023    end if;
18025    --  Pentium OverDrive with MMX for Pentium (75, 90, 100, 120, 133)
18026    if Signature.Processor_Type = 2#01#   and
18027      Signature.Family          = 2#0101# and
18028      Signature.Model           = 2#0100# then
18029       Type_Found := True;
18030       Ada.Text_IO.Put_Line
18031         ("Pentium OverDrive processor with MMX " &
18032          "technology for Pentium processor (75, 90, 100, 120, 133)");
18033    end if;
18035    --  Pentium Pro processor
18036    if Signature.Processor_Type = 2#00#   and
18037      Signature.Family          = 2#0110# and
18038      Signature.Model           = 2#0001# then
18039       Type_Found := True;
18040       Ada.Text_IO.Put_Line ("Pentium Pro processor");
18041    end if;
18043    --  Pentium II processor, model 3
18044    if Signature.Processor_Type = 2#00#   and
18045      Signature.Family          = 2#0110# and
18046      Signature.Model           = 2#0011# then
18047       Type_Found := True;
18048       Ada.Text_IO.Put_Line ("Pentium II processor, model 3");
18049    end if;
18051    --  Pentium II processor, model 5 or Celeron processor
18052    if Signature.Processor_Type = 2#00#   and
18053      Signature.Family          = 2#0110# and
18054      Signature.Model           = 2#0101# then
18055       Type_Found := True;
18056       Ada.Text_IO.Put_Line
18057         ("Pentium II processor, model 5 or Celeron processor");
18058    end if;
18060    --  Pentium Pro OverDrive processor
18061    if Signature.Processor_Type = 2#01#   and
18062      Signature.Family          = 2#0110# and
18063      Signature.Model           = 2#0011# then
18064       Type_Found := True;
18065       Ada.Text_IO.Put_Line ("Pentium Pro OverDrive processor");
18066    end if;
18068    --  If no type recognized, we have an unknown. Display what
18069    --  we _do_ know
18070    if Type_Found = False then
18071       Ada.Text_IO.Put_Line ("Unknown processor");
18072    end if;
18074    -----------------------------------------
18075    --  Display processor stepping level.  --
18076    -----------------------------------------
18078    Ada.Text_IO.Put_Line ("Stepping level:" & Signature.Stepping'Img);
18080    ---------------------------------
18081    --  Display vendor ID string.  --
18082    ---------------------------------
18084    Ada.Text_IO.Put_Line ("Vendor ID: " & Intel_CPU.Vendor_ID);
18086    ------------------------------------
18087    --  Get the processors features.  --
18088    ------------------------------------
18090    Features := Intel_CPU.Features;
18092    -----------------------------
18093    --  Check for a FPU unit.  --
18094    -----------------------------
18096    if Features.FPU = True then
18097       Ada.Text_IO.Put_Line ("Floating-Point unit available");
18098    else
18099       Ada.Text_IO.Put_Line ("no Floating-Point unit");
18100    end if;  --  check for FPU
18102    --------------------------------
18103    --  List processor features.  --
18104    --------------------------------
18106    Ada.Text_IO.Put_Line ("Supported features: ");
18108    --  Virtual Mode Extension
18109    if Features.VME = True then
18110       Ada.Text_IO.Put_Line ("    VME    - Virtual Mode Extension");
18111    end if;
18113    --  Debugging Extension
18114    if Features.DE = True then
18115       Ada.Text_IO.Put_Line ("    DE     - Debugging Extension");
18116    end if;
18118    --  Page Size Extension
18119    if Features.PSE = True then
18120       Ada.Text_IO.Put_Line ("    PSE    - Page Size Extension");
18121    end if;
18123    --  Time Stamp Counter
18124    if Features.TSC = True then
18125       Ada.Text_IO.Put_Line ("    TSC    - Time Stamp Counter");
18126    end if;
18128    --  Model Specific Registers
18129    if Features.MSR = True then
18130       Ada.Text_IO.Put_Line ("    MSR    - Model Specific Registers");
18131    end if;
18133    --  Physical Address Extension
18134    if Features.PAE = True then
18135       Ada.Text_IO.Put_Line ("    PAE    - Physical Address Extension");
18136    end if;
18138    --  Machine Check Extension
18139    if Features.MCE = True then
18140       Ada.Text_IO.Put_Line ("    MCE    - Machine Check Extension");
18141    end if;
18143    --  CMPXCHG8 instruction supported
18144    if Features.CX8 = True then
18145       Ada.Text_IO.Put_Line ("    CX8    - CMPXCHG8 instruction");
18146    end if;
18148    --  on-chip APIC hardware support
18149    if Features.APIC = True then
18150       Ada.Text_IO.Put_Line ("    APIC   - on-chip APIC hardware support");
18151    end if;
18153    --  Fast System Call
18154    if Features.SEP = True then
18155       Ada.Text_IO.Put_Line ("    SEP    - Fast System Call");
18156    end if;
18158    --  Memory Type Range Registers
18159    if Features.MTRR = True then
18160       Ada.Text_IO.Put_Line ("    MTTR   - Memory Type Range Registers");
18161    end if;
18163    --  Page Global Enable
18164    if Features.PGE = True then
18165       Ada.Text_IO.Put_Line ("    PGE    - Page Global Enable");
18166    end if;
18168    --  Machine Check Architecture
18169    if Features.MCA = True then
18170       Ada.Text_IO.Put_Line ("    MCA    - Machine Check Architecture");
18171    end if;
18173    --  Conditional Move Instruction Supported
18174    if Features.CMOV = True then
18175       Ada.Text_IO.Put_Line
18176         ("    CMOV   - Conditional Move Instruction Supported");
18177    end if;
18179    --  Page Attribute Table
18180    if Features.PAT = True then
18181       Ada.Text_IO.Put_Line ("    PAT    - Page Attribute Table");
18182    end if;
18184    --  36-bit Page Size Extension
18185    if Features.PSE_36 = True then
18186       Ada.Text_IO.Put_Line ("    PSE_36 - 36-bit Page Size Extension");
18187    end if;
18189    --  MMX technology supported
18190    if Features.MMX = True then
18191       Ada.Text_IO.Put_Line ("    MMX    - MMX technology supported");
18192    end if;
18194    --  Fast FP Save and Restore
18195    if Features.FXSR = True then
18196       Ada.Text_IO.Put_Line ("    FXSR   - Fast FP Save and Restore");
18197    end if;
18199    ---------------------
18200    --  Program done.  --
18201    ---------------------
18203    Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Success);
18205 exception
18207    when others =>
18208       Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
18209       raise;
18211 end Check_CPU;
18212 @end smallexample
18214 @c ---------------------------------------------------------------------------
18215 @node Intel_CPU Package Specification
18216 @subsection @code{Intel_CPU} Package Specification
18217 @cindex Intel_CPU package specification
18219 @smallexample
18220 -------------------------------------------------------------------------
18221 --                                                                     --
18222 --  file: INTEL_CPU.ADS                                                --
18223 --                                                                     --
18224 --           *********************************************             --
18225 --           * WARNING: for 32-bit Intel processors only *             --
18226 --           *********************************************             --
18227 --                                                                     --
18228 --  This package contains a number of subprograms that are useful in   --
18229 --  determining the Intel x86 CPU (and the features it supports) on    --
18230 --  which the program is running.                                      --
18231 --                                                                     --
18232 --  The package is based upon the information given in the Intel       --
18233 --  Application Note AP-485: "Intel Processor Identification and the   --
18234 --  CPUID Instruction" as of April 1998. This application note can be  --
18235 --  found on www.intel.com.                                            --
18236 --                                                                     --
18237 --  It currently deals with 32-bit processors only, will not detect    --
18238 --  features added after april 1998, and does not guarantee proper     --
18239 --  results on Intel-compatible processors.                            --
18240 --                                                                     --
18241 --  Cache info and x386 fpu type detection are not supported.          --
18242 --                                                                     --
18243 --  This package does not use any privileged instructions, so should   --
18244 --  work on any OS running on a 32-bit Intel processor.                --
18245 --                                                                     --
18246 -------------------------------------------------------------------------
18248 with Interfaces;             use Interfaces;
18249 --  for using unsigned types
18251 with System.Machine_Code;    use System.Machine_Code;
18252 --  for using inline assembler code
18254 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
18255 --  for inserting control characters
18257 package Intel_CPU is
18259    ----------------------
18260    --  Processor bits  --
18261    ----------------------
18263    subtype Num_Bits is Natural range 0 .. 31;
18264    --  the number of processor bits (32)
18266    --------------------------
18267    --  Processor register  --
18268    --------------------------
18270    --  define a processor register type for easy access to
18271    --  the individual bits
18273    type Processor_Register is array (Num_Bits) of Boolean;
18274    pragma Pack (Processor_Register);
18275    for Processor_Register'Size use 32;
18277    -------------------------
18278    --  Unsigned register  --
18279    -------------------------
18281    --  define a processor register type for easy access to
18282    --  the individual bytes
18284    type Unsigned_Register is
18285       record
18286          L1 : Unsigned_8;
18287          H1 : Unsigned_8;
18288          L2 : Unsigned_8;
18289          H2 : Unsigned_8;
18290       end record;
18292    for Unsigned_Register use
18293       record
18294          L1 at 0 range  0 ..  7;
18295          H1 at 0 range  8 .. 15;
18296          L2 at 0 range 16 .. 23;
18297          H2 at 0 range 24 .. 31;
18298       end record;
18300    for Unsigned_Register'Size use 32;
18302    ---------------------------------
18303    --  Intel processor vendor ID  --
18304    ---------------------------------
18306    Intel_Processor : constant String (1 .. 12) := "GenuineIntel";
18307    --  indicates an Intel manufactured processor
18309    ------------------------------------
18310    --  Processor signature register  --
18311    ------------------------------------
18313    --  a register type to hold the processor signature
18315    type Processor_Signature is
18316       record
18317          Stepping       : Natural range 0 .. 15;
18318          Model          : Natural range 0 .. 15;
18319          Family         : Natural range 0 .. 15;
18320          Processor_Type : Natural range 0 .. 3;
18321          Reserved       : Natural range 0 .. 262143;
18322       end record;
18324    for Processor_Signature use
18325       record
18326          Stepping       at 0 range  0 ..  3;
18327          Model          at 0 range  4 ..  7;
18328          Family         at 0 range  8 .. 11;
18329          Processor_Type at 0 range 12 .. 13;
18330          Reserved       at 0 range 14 .. 31;
18331       end record;
18333    for Processor_Signature'Size use 32;
18335    -----------------------------------
18336    --  Processor features register  --
18337    -----------------------------------
18339    --  a processor register to hold the processor feature flags
18341    type Processor_Features is
18342       record
18343          FPU    : Boolean;                --  floating point unit on chip
18344          VME    : Boolean;                --  virtual mode extension
18345          DE     : Boolean;                --  debugging extension
18346          PSE    : Boolean;                --  page size extension
18347          TSC    : Boolean;                --  time stamp counter
18348          MSR    : Boolean;                --  model specific registers
18349          PAE    : Boolean;                --  physical address extension
18350          MCE    : Boolean;                --  machine check extension
18351          CX8    : Boolean;                --  cmpxchg8 instruction
18352          APIC   : Boolean;                --  on-chip apic hardware
18353          Res_1  : Boolean;                --  reserved for extensions
18354          SEP    : Boolean;                --  fast system call
18355          MTRR   : Boolean;                --  memory type range registers
18356          PGE    : Boolean;                --  page global enable
18357          MCA    : Boolean;                --  machine check architecture
18358          CMOV   : Boolean;                --  conditional move supported
18359          PAT    : Boolean;                --  page attribute table
18360          PSE_36 : Boolean;                --  36-bit page size extension
18361          Res_2  : Natural range 0 .. 31;  --  reserved for extensions
18362          MMX    : Boolean;                --  MMX technology supported
18363          FXSR   : Boolean;                --  fast FP save and restore
18364          Res_3  : Natural range 0 .. 127; --  reserved for extensions
18365       end record;
18367    for Processor_Features use
18368       record
18369          FPU    at 0 range  0 ..  0;
18370          VME    at 0 range  1 ..  1;
18371          DE     at 0 range  2 ..  2;
18372          PSE    at 0 range  3 ..  3;
18373          TSC    at 0 range  4 ..  4;
18374          MSR    at 0 range  5 ..  5;
18375          PAE    at 0 range  6 ..  6;
18376          MCE    at 0 range  7 ..  7;
18377          CX8    at 0 range  8 ..  8;
18378          APIC   at 0 range  9 ..  9;
18379          Res_1  at 0 range 10 .. 10;
18380          SEP    at 0 range 11 .. 11;
18381          MTRR   at 0 range 12 .. 12;
18382          PGE    at 0 range 13 .. 13;
18383          MCA    at 0 range 14 .. 14;
18384          CMOV   at 0 range 15 .. 15;
18385          PAT    at 0 range 16 .. 16;
18386          PSE_36 at 0 range 17 .. 17;
18387          Res_2  at 0 range 18 .. 22;
18388          MMX    at 0 range 23 .. 23;
18389          FXSR   at 0 range 24 .. 24;
18390          Res_3  at 0 range 25 .. 31;
18391       end record;
18393    for Processor_Features'Size use 32;
18395    -------------------
18396    --  Subprograms  --
18397    -------------------
18399    function Has_FPU return Boolean;
18400    --  return True if a FPU is found
18401    --  use only if CPUID is not supported
18403    function Has_CPUID return Boolean;
18404    --  return True if the processor supports the CPUID instruction
18406    function CPUID_Level return Natural;
18407    --  return the CPUID support level (0, 1 or 2)
18408    --  can only be called if the CPUID instruction is supported
18410    function Vendor_ID return String;
18411    --  return the processor vendor identification string
18412    --  can only be called if the CPUID instruction is supported
18414    function Signature return Processor_Signature;
18415    --  return the processor signature
18416    --  can only be called if the CPUID instruction is supported
18418    function Features return Processor_Features;
18419    --  return the processors features
18420    --  can only be called if the CPUID instruction is supported
18422 private
18424    ------------------------
18425    --  EFLAGS bit names  --
18426    ------------------------
18428    ID_Flag : constant Num_Bits := 21;
18429    --  ID flag bit
18431 end Intel_CPU;
18432 @end smallexample
18434 @c ---------------------------------------------------------------------------
18435 @node Intel_CPU Package Body
18436 @subsection @code{Intel_CPU} Package Body
18437 @cindex Intel_CPU package body
18439 @smallexample
18440 package body Intel_CPU is
18442    ---------------------------
18443    --  Detect FPU presence  --
18444    ---------------------------
18446    --  There is a FPU present if we can set values to the FPU Status
18447    --  and Control Words.
18449    function Has_FPU return Boolean is
18451       Register : Unsigned_16;
18452       --  processor register to store a word
18454    begin
18456       --  check if we can change the status word
18457       Asm (
18459            --  the assembler code
18460            "finit"              & LF & HT &    --  reset status word
18461            "movw $0x5A5A, %%ax" & LF & HT &    --  set value status word
18462            "fnstsw %0"          & LF & HT &    --  save status word
18463            "movw %%ax, %0",                    --  store status word
18465            --  output stored in Register
18466            --  register must be a memory location
18467            Outputs => Unsigned_16'Asm_output ("=m", Register),
18469            --  tell compiler that we used eax
18470            Clobber => "eax");
18472       --  if the status word is zero, there is no FPU
18473       if Register = 0 then
18474          return False;   --  no status word
18475       end if;  --  check status word value
18477       --  check if we can get the control word
18478       Asm (
18480            --  the assembler code
18481            "fnstcw %0",   --  save the control word
18483            --  output into Register
18484            --  register must be a memory location
18485            Outputs => Unsigned_16'Asm_output ("=m", Register));
18487       --  check the relevant bits
18488       if (Register and 16#103F#) /= 16#003F# then
18489          return False;   --  no control word
18490       end if;  --  check control word value
18492       --  FPU found
18493       return True;
18495    end Has_FPU;
18497    --------------------------------
18498    --  Detect CPUID instruction  --
18499    --------------------------------
18501    --  The processor supports the CPUID instruction if it is possible
18502    --  to change the value of ID flag bit in the EFLAGS register.
18504    function Has_CPUID return Boolean is
18506       Original_Flags, Modified_Flags : Processor_Register;
18507       --  EFLAG contents before and after changing the ID flag
18509    begin
18511       --  try flipping the ID flag in the EFLAGS register
18512       Asm (
18514            --  the assembler code
18515            "pushfl"               & LF & HT &     --  push EFLAGS on stack
18516            "pop %%eax"            & LF & HT &     --  pop EFLAGS into eax
18517            "movl %%eax, %0"       & LF & HT &     --  save EFLAGS content
18518            "xor $0x200000, %%eax" & LF & HT &     --  flip ID flag
18519            "push %%eax"           & LF & HT &     --  push EFLAGS on stack
18520            "popfl"                & LF & HT &     --  load EFLAGS register
18521            "pushfl"               & LF & HT &     --  push EFLAGS on stack
18522            "pop %1",                              --  save EFLAGS content
18524            --  output values, may be anything
18525            --  Original_Flags is %0
18526            --  Modified_Flags is %1
18527            Outputs =>
18528               (Processor_Register'Asm_output ("=g", Original_Flags),
18529                Processor_Register'Asm_output ("=g", Modified_Flags)),
18531            --  tell compiler eax is destroyed
18532            Clobber => "eax");
18534       --  check if CPUID is supported
18535       if Original_Flags(ID_Flag) /= Modified_Flags(ID_Flag) then
18536          return True;   --  ID flag was modified
18537       else
18538          return False;  --  ID flag unchanged
18539       end if;  --  check for CPUID
18541    end Has_CPUID;
18543    -------------------------------
18544    --  Get CPUID support level  --
18545    -------------------------------
18547    function CPUID_Level return Natural is
18549       Level : Unsigned_32;
18550       --  returned support level
18552    begin
18554       --  execute CPUID, storing the results in the Level register
18555       Asm (
18557            --  the assembler code
18558            "cpuid",    --  execute CPUID
18560            --  zero is stored in eax
18561            --  returning the support level in eax
18562            Inputs => Unsigned_32'Asm_input ("a", 0),
18564            --  eax is stored in Level
18565            Outputs => Unsigned_32'Asm_output ("=a", Level),
18567            --  tell compiler ebx, ecx and edx registers are destroyed
18568            Clobber => "ebx, ecx, edx");
18570       --  return the support level
18571       return Natural (Level);
18573    end CPUID_Level;
18575    --------------------------------
18576    --  Get CPU Vendor ID String  --
18577    --------------------------------
18579    --  The vendor ID string is returned in the ebx, ecx and edx register
18580    --  after executing the CPUID instruction with eax set to zero.
18581    --  In case of a true Intel processor the string returned is
18582    --  "GenuineIntel"
18584    function Vendor_ID return String is
18586       Ebx, Ecx, Edx : Unsigned_Register;
18587       --  registers containing the vendor ID string
18589       Vendor_ID : String (1 .. 12);
18590       -- the vendor ID string
18592    begin
18594       --  execute CPUID, storing the results in the processor registers
18595       Asm (
18597            --  the assembler code
18598            "cpuid",    --  execute CPUID
18600            --  zero stored in eax
18601            --  vendor ID string returned in ebx, ecx and edx
18602            Inputs => Unsigned_32'Asm_input ("a", 0),
18604            --  ebx is stored in Ebx
18605            --  ecx is stored in Ecx
18606            --  edx is stored in Edx
18607            Outputs => (Unsigned_Register'Asm_output ("=b", Ebx),
18608                        Unsigned_Register'Asm_output ("=c", Ecx),
18609                        Unsigned_Register'Asm_output ("=d", Edx)));
18611       --  now build the vendor ID string
18612       Vendor_ID( 1) := Character'Val (Ebx.L1);
18613       Vendor_ID( 2) := Character'Val (Ebx.H1);
18614       Vendor_ID( 3) := Character'Val (Ebx.L2);
18615       Vendor_ID( 4) := Character'Val (Ebx.H2);
18616       Vendor_ID( 5) := Character'Val (Edx.L1);
18617       Vendor_ID( 6) := Character'Val (Edx.H1);
18618       Vendor_ID( 7) := Character'Val (Edx.L2);
18619       Vendor_ID( 8) := Character'Val (Edx.H2);
18620       Vendor_ID( 9) := Character'Val (Ecx.L1);
18621       Vendor_ID(10) := Character'Val (Ecx.H1);
18622       Vendor_ID(11) := Character'Val (Ecx.L2);
18623       Vendor_ID(12) := Character'Val (Ecx.H2);
18625       --  return string
18626       return Vendor_ID;
18628    end Vendor_ID;
18630    -------------------------------
18631    --  Get processor signature  --
18632    -------------------------------
18634    function Signature return Processor_Signature is
18636       Result : Processor_Signature;
18637       --  processor signature returned
18639    begin
18641       --  execute CPUID, storing the results in the Result variable
18642       Asm (
18644            --  the assembler code
18645            "cpuid",    --  execute CPUID
18647            --  one is stored in eax
18648            --  processor signature returned in eax
18649            Inputs => Unsigned_32'Asm_input ("a", 1),
18651            --  eax is stored in Result
18652            Outputs => Processor_Signature'Asm_output ("=a", Result),
18654            --  tell compiler that ebx, ecx and edx are also destroyed
18655            Clobber => "ebx, ecx, edx");
18657       --  return processor signature
18658       return Result;
18660    end Signature;
18662    ------------------------------
18663    --  Get processor features  --
18664    ------------------------------
18666    function Features return Processor_Features is
18668       Result : Processor_Features;
18669       --  processor features returned
18671    begin
18673       --  execute CPUID, storing the results in the Result variable
18674       Asm (
18676            --  the assembler code
18677            "cpuid",    --  execute CPUID
18679            --  one stored in eax
18680            --  processor features returned in edx
18681            Inputs => Unsigned_32'Asm_input ("a", 1),
18683            --  edx is stored in Result
18684            Outputs => Processor_Features'Asm_output ("=d", Result),
18686            --  tell compiler that ebx and ecx are also destroyed
18687            Clobber => "ebx, ecx");
18689       --  return processor signature
18690       return Result;
18692    end Features;
18694 end Intel_CPU;
18695 @end smallexample
18696 @c END OF INLINE ASSEMBLER CHAPTER
18697 @c ===============================
18702 @node Performance Considerations
18703 @chapter Performance Considerations
18704 @cindex Performance
18706 @noindent
18707 The GNAT system provides a number of options that allow a trade-off
18708 between
18710 @itemize @bullet
18711 @item
18712 performance of the generated code
18714 @item
18715 speed of compilation
18717 @item
18718 minimization of dependences and recompilation
18720 @item
18721 the degree of run-time checking.
18722 @end itemize
18724 @noindent
18725 The defaults (if no options are selected) aim at improving the speed
18726 of compilation and minimizing dependences, at the expense of performance
18727 of the generated code:
18729 @itemize @bullet
18730 @item
18731 no optimization
18733 @item
18734 no inlining of subprogram calls
18736 @item
18737 all run-time checks enabled except overflow and elaboration checks
18738 @end itemize
18740 @noindent
18741 These options are suitable for most program development purposes. This
18742 chapter describes how you can modify these choices, and also provides
18743 some guidelines on debugging optimized code.
18745 @menu
18746 * Controlling Run-Time Checks::
18747 * Optimization Levels::
18748 * Debugging Optimized Code::
18749 * Inlining of Subprograms::
18750 * Coverage Analysis::
18751 @end menu
18753 @node Controlling Run-Time Checks
18754 @section Controlling Run-Time Checks
18756 @noindent
18757 By default, GNAT generates all run-time checks, except arithmetic overflow
18758 checking for integer operations and checks for access before elaboration on
18759 subprogram calls. The latter are not required in default mode, because all
18760 necessary checking is done at compile time.
18761 @cindex @option{/CHECKS=SUPPRESS_ALL} (@code{GNAT COMPILE})
18762 @cindex @option{/CHECKS=OVERFLOW} (@code{GNAT COMPILE})
18763 Two gnat qualifiers, @option{/CHECKS=SUPPRESS_ALL} and @option{/CHECKS=OVERFLOW} allow this default to
18764 be modified. @xref{Run-Time Checks}.
18766 Our experience is that the default is suitable for most development
18767 purposes.
18769 We treat integer overflow specially because these
18770 are quite expensive and in our experience are not as important as other
18771 run-time checks in the development process. Note that division by zero
18772 is not considered an overflow check, and divide by zero checks are
18773 generated where required by default.
18775 Elaboration checks are off by default, and also not needed by default, since
18776 GNAT uses a static elaboration analysis approach that avoids the need for
18777 run-time checking. This manual contains a full chapter discussing the issue
18778 of elaboration checks, and if the default is not satisfactory for your use,
18779 you should read this chapter.
18781 For validity checks, the minimal checks required by the Ada Reference
18782 Manual (for case statements and assignments to array elements) are on
18783 by default. These can be suppressed by use of the @option{-gnatVn} qualifier.
18784 Note that in Ada 83, there were no validity checks, so if the Ada 83 mode
18785 is acceptable (or when comparing GNAT performance with an Ada 83 compiler),
18786 it may be reasonable to routinely use @option{-gnatVn}. Validity checks
18787 are also suppressed entirely if @option{/CHECKS=SUPPRESS_ALL} is used.
18789 @cindex Overflow checks
18790 @cindex Checks, overflow
18791 @findex Suppress
18792 @findex Unsuppress
18793 @cindex pragma Suppress
18794 @cindex pragma Unsuppress
18795 Note that the setting of the qualifiers controls the default setting of
18796 the checks. They may be modified using either @code{pragma Suppress} (to
18797 remove checks) or @code{pragma Unsuppress} (to add back suppressed
18798 checks) in the program source.
18800 @node Optimization Levels
18801 @section Optimization Levels
18802 @cindex @code{/OPTIMIZE} (@code{GNAT COMPILE})
18804 @noindent
18805 The default is optimization off. This results in the fastest compile
18806 times, but GNAT makes absolutely no attempt to optimize, and the
18807 generated programs are considerably larger and slower than when
18808 optimization is enabled. You can use the
18809 @code{/OPTIMIZE}
18810 on the @code{GNAT COMPILE} command line to control the optimization level:
18812 @table @code
18813 @item /OPTIMIZE=NONE
18814 no optimization (the default)
18816 @item /OPTIMIZE=SOME
18817 medium level optimization
18819 @item /OPTIMIZE=ALL
18820 full optimization
18822 @item /OPTIMIZE=INLINING
18823 full optimization, and also attempt automatic inlining of small
18824 subprograms within a unit (@pxref{Inlining of Subprograms}).
18825 @end table
18827 Higher optimization levels perform more global transformations on the
18828 program and apply more expensive analysis algorithms in order to generate
18829 faster and more compact code. The price in compilation time, and the
18830 resulting improvement in execution time,
18831 both depend on the particular application and the hardware environment.
18832 You should experiment to find the best level for your application.
18834 Note: Unlike some other compilation systems, @code{GNAT COMPILE} has
18835 been tested extensively at all optimization levels. There are some bugs
18836 which appear only with optimization turned on, but there have also been
18837 bugs which show up only in @emph{unoptimized} code. Selecting a lower
18838 level of optimization does not improve the reliability of the code
18839 generator, which in practice is highly reliable at all optimization
18840 levels.
18842 Note regarding the use of @code{/OPTIMIZE=INLINING}: The use of this optimization level
18843 is generally discouraged with GNAT, since it often results in larger
18844 executables which run more slowly. See further discussion of this point
18845 in @pxref{Inlining of Subprograms}.
18847 @node Debugging Optimized Code
18848 @section Debugging Optimized Code
18850 @noindent
18851 Since the compiler generates debugging tables for a compilation unit before
18852 it performs optimizations, the optimizing transformations may invalidate some
18853 of the debugging data.  You therefore need to anticipate certain
18854 anomalous situations that may arise while debugging optimized code.  This
18855 section describes the most common cases.
18857 @enumerate
18858 @item
18859 @i{The "hopping Program Counter":}  Repeated 'step' or 'next' commands show the PC
18860 bouncing back and forth in the code.  This may result from any of the following
18861 optimizations:
18863 @itemize @bullet
18864 @item
18865 @i{Common subexpression elimination:} using a single instance of code for a
18866 quantity that the source computes several times.  As a result you
18867 may not be able to stop on what looks like a statement.
18869 @item
18870 @i{Invariant code motion:} moving an expression that does not change within a
18871 loop, to the beginning of the loop.
18873 @item
18874 @i{Instruction scheduling:} moving instructions so as to
18875 overlap loads and stores (typically) with other code, or in
18876 general to move computations of values closer to their uses. Often
18877 this causes you to pass an assignment statement without the assignment
18878 happening and then later bounce back to the statement when the
18879 value is actually needed.  Placing a breakpoint on a line of code
18880 and then stepping over it may, therefore, not always cause all the
18881 expected side-effects.
18882 @end itemize
18884 @item
18885 @i{The "big leap":} More commonly known as @i{cross-jumping}, in which two
18886 identical pieces of code are merged and the program counter suddenly
18887 jumps to a statement that is not supposed to be executed, simply because
18888 it (and the code following) translates to the same thing as the code
18889 that @emph{was} supposed to be executed.  This effect is typically seen in
18890 sequences that end in a jump, such as a @code{goto}, a @code{return}, or
18891 a @code{break} in a C @code{qualifier} statement.
18893 @item
18894 @i{The "roving variable":} The symptom is an unexpected value in a variable.
18895 There are various reasons for this effect:
18897 @itemize @bullet
18898 @item
18899 In a subprogram prologue, a parameter may not yet have been moved to its
18900 "home".
18902 @item
18903 A variable may be dead, and its register re-used.  This is
18904 probably the most common cause.
18906 @item
18907 As mentioned above, the assignment of a value to a variable may
18908 have been moved.
18910 @item
18911 A variable may be eliminated entirely by value propagation or
18912 other means.  In this case, GCC may incorrectly generate debugging
18913 information for the variable
18914 @end itemize
18916 @noindent
18917 In general, when an unexpected value appears for a local variable or parameter
18918 you should first ascertain if that value was actually computed by
18919 your program, as opposed to being incorrectly reported by the debugger.
18920 Record fields or
18921 array elements in an object designated by an access value
18922 are generally less of a problem, once you have ascertained that the access value
18923 is sensible.
18924 Typically, this means checking variables in the preceding code and in the
18925 calling subprogram to verify that the value observed is explainable from other
18926 values (one must apply the procedure recursively to those
18927 other values); or re-running the code and stopping a little earlier
18928 (perhaps before the call) and stepping to better see how the variable obtained
18929 the value in question; or continuing to step @emph{from} the point of the
18930 strange value to see if code motion had simply moved the variable's
18931 assignments later.
18932 @end enumerate
18934 @node Inlining of Subprograms
18935 @section Inlining of Subprograms
18937 @noindent
18938 A call to a subprogram in the current unit is inlined if all the
18939 following conditions are met:
18941 @itemize @bullet
18942 @item
18943 The optimization level is at least @code{/OPTIMIZE=SOME}.
18945 @item
18946 The called subprogram is suitable for inlining: It must be small enough
18947 and not contain nested subprograms or anything else that @code{GNAT COMPILE}
18948 cannot support in inlined subprograms.
18950 @item
18951 The call occurs after the definition of the body of the subprogram.
18953 @item
18954 @cindex pragma Inline
18955 @findex Inline
18956 Either @code{pragma Inline} applies to the subprogram or it is
18957 small and automatic inlining (optimization level @code{/OPTIMIZE=INLINING}) is
18958 specified.
18959 @end itemize
18961 @noindent
18962 Calls to subprograms in @code{with}'ed units are normally not inlined.
18963 To achieve this level of inlining, the following conditions must all be
18964 true:
18966 @itemize @bullet
18967 @item
18968 The optimization level is at least @code{/OPTIMIZE=SOME}.
18970 @item
18971 The called subprogram is suitable for inlining: It must be small enough
18972 and not contain nested subprograms or anything else @code{GNAT COMPILE} cannot
18973 support in inlined subprograms.
18975 @item
18976 The call appears in a body (not in a package spec).
18978 @item
18979 There is a @code{pragma Inline} for the subprogram.
18981 @item
18982 @cindex @option{/INLINE=PRAGMA} (@code{GNAT COMPILE})
18983 The @code{/INLINE} qualifier
18984 is used in the @code{GNAT COMPILE} command line
18985 @end itemize
18987 Note that specifying the @option{/INLINE=PRAGMA} qualifier causes additional
18988 compilation dependencies. Consider the following:
18990 @smallexample
18991 @group
18992 @cartouche
18993 @b{package} R @b{is}
18994    @b{procedure} Q;
18995    @b{pragma} Inline (Q);
18996 @b{end} R;
18997 @b{package body} R @b{is}
18998    ...
18999 @b{end} R;
19001 @b{with} R;
19002 @b{procedure} Main @b{is}
19003 @b{begin}
19004    ...
19005    R.Q;
19006 @b{end} Main;
19007 @end cartouche
19008 @end group
19009 @end smallexample
19011 @noindent
19012 With the default behavior (no @option{/INLINE=PRAGMA} qualifier specified), the
19013 compilation of the @code{Main} procedure depends only on its own source,
19014 @file{MAIN.ADB}, and the spec of the package in file @file{R.ADS}. This
19015 means that editing the body of @code{R} does not require recompiling
19016 @code{Main}.
19018 On the other hand, the call @code{R.Q} is not inlined under these
19019 circumstances. If the @option{/INLINE=PRAGMA} qualifier is present when @code{Main}
19020 is compiled, the call will be inlined if the body of @code{Q} is small
19021 enough, but now @code{Main} depends on the body of @code{R} in
19022 @file{R.ADB} as well as on the spec. This means that if this body is edited,
19023 the main program must be recompiled. Note that this extra dependency
19024 occurs whether or not the call is in fact inlined by @code{GNAT COMPILE}.
19026 The use of front end inlining with @option{-gnatN} generates similar
19027 additional dependencies.
19029 @cindex @code{/INLINE=SUPPRESS} (@code{GNAT COMPILE})
19030 Note: The @code{/INLINE=SUPPRESS} qualifier
19031 can be used to prevent
19032 all inlining. This qualifier overrides all other conditions and ensures
19033 that no inlining occurs. The extra dependences resulting from
19034 @option{/INLINE=PRAGMA} will still be active, even if
19035 this qualifier is used to suppress the resulting inlining actions.
19037 Note regarding the use of @code{/OPTIMIZE=INLINING}: There is no difference in inlining
19038 behavior between @code{/OPTIMIZE=ALL} and @code{/OPTIMIZE=INLINING} for subprograms with an explicit
19039 pragma @code{Inline} assuming the use of @option{/INLINE=PRAGMA}
19040 or @option{-gnatN} (the qualifiers that activate inlining). If you have used
19041 pragma @code{Inline} in appropriate cases, then it is usually much better
19042 to use @code{/OPTIMIZE=ALL} and @option{/INLINE=PRAGMA} and avoid the use of @code{/OPTIMIZE=INLINING} which
19043 in this case only has the effect of inlining subprograms you did not
19044 think should be inlined. We often find that the use of @code{/OPTIMIZE=INLINING} slows
19045 down code by performing excessive inlining, leading to increased instruction
19046 cache pressure from the increased code size. So the bottom line here is
19047 that you should not automatically assume that @code{/OPTIMIZE=INLINING} is better than
19048 @code{/OPTIMIZE=ALL}, and indeed you should use @code{/OPTIMIZE=INLINING} only if tests show that
19049 it actually improves performance.
19051 @node Coverage Analysis
19052 @section Coverage Analysis
19054 @noindent
19055 GNAT supports the Digital Performance Coverage Analyzer (PCA), which allows
19056 the user to determine the distribution of execution time across a program,
19057 @pxref{Profiling} for details of usage.
19059 @include fdl.texi
19060 @c GNU Free Documentation License
19062 @node Index,,GNU Free Documentation License, Top
19063 @unnumbered Index
19065 @printindex cp
19067 @contents
19069 @bye