]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/gnat_ugn.texi
Correct a function pre/postcondition [PR102403].
[thirdparty/gcc.git] / gcc / ada / gnat_ugn.texi
1 \input texinfo @c -*-texinfo-*-
2 @c %**start of header
3 @setfilename gnat_ugn.info
4 @documentencoding UTF-8
5 @ifinfo
6 @*Generated by Sphinx 4.0.2.@*
7 @end ifinfo
8 @settitle GNAT User's Guide for Native Platforms
9 @defindex ge
10 @paragraphindent 0
11 @exampleindent 4
12 @finalout
13 @dircategory GNU Ada Tools
14 @direntry
15 * gnat_ugn: (gnat_ugn.info). gnat_ugn
16 @end direntry
17
18 @definfoenclose strong,`,'
19 @definfoenclose emph,`,'
20 @c %**end of header
21
22 @copying
23 @quotation
24 GNAT User's Guide for Native Platforms , Jun 23, 2021
25
26 AdaCore
27
28 Copyright @copyright{} 2008-2021, Free Software Foundation
29 @end quotation
30
31 @end copying
32
33 @titlepage
34 @title GNAT User's Guide for Native Platforms
35 @insertcopying
36 @end titlepage
37 @contents
38
39 @c %** start of user preamble
40
41 @c %** end of user preamble
42
43 @ifnottex
44 @node Top
45 @top GNAT User's Guide for Native Platforms
46 @insertcopying
47 @end ifnottex
48
49 @c %**start of body
50 @anchor{gnat_ugn doc}@anchor{0}
51 @emph{GNAT, The GNU Ada Development Environment}
52
53
54 @include gcc-common.texi
55 GCC version @value{version-GCC}@*
56 AdaCore
57
58 Permission is granted to copy, distribute and/or modify this document
59 under the terms of the GNU Free Documentation License, Version 1.3 or
60 any later version published by the Free Software Foundation; with no
61 Invariant Sections, with the Front-Cover Texts being
62 “GNAT User’s Guide for Native Platforms”,
63 and with no Back-Cover Texts. A copy of the license is
64 included in the section entitled @ref{1,,GNU Free Documentation License}.
65
66 @menu
67 * About This Guide::
68 * Getting Started with GNAT::
69 * The GNAT Compilation Model::
70 * Building Executable Programs with GNAT::
71 * GNAT Utility Programs::
72 * GNAT and Program Execution::
73 * Platform-Specific Information::
74 * Example of Binder Output File::
75 * Elaboration Order Handling in GNAT::
76 * Inline Assembler::
77 * GNU Free Documentation License::
78 * Index::
79
80 @detailmenu
81 --- The Detailed Node Listing ---
82
83 About This Guide
84
85 * What This Guide Contains::
86 * What You Should Know before Reading This Guide::
87 * Related Information::
88 * Conventions::
89
90 Getting Started with GNAT
91
92 * System Requirements::
93 * Running GNAT::
94 * Running a Simple Ada Program::
95 * Running a Program with Multiple Units::
96
97 The GNAT Compilation Model
98
99 * Source Representation::
100 * Foreign Language Representation::
101 * File Naming Topics and Utilities::
102 * Configuration Pragmas::
103 * Generating Object Files::
104 * Source Dependencies::
105 * The Ada Library Information Files::
106 * Binding an Ada Program::
107 * GNAT and Libraries::
108 * Conditional Compilation::
109 * Mixed Language Programming::
110 * GNAT and Other Compilation Models::
111 * Using GNAT Files with External Tools::
112
113 Foreign Language Representation
114
115 * Latin-1::
116 * Other 8-Bit Codes::
117 * Wide_Character Encodings::
118 * Wide_Wide_Character Encodings::
119
120 File Naming Topics and Utilities
121
122 * File Naming Rules::
123 * Using Other File Names::
124 * Alternative File Naming Schemes::
125 * Handling Arbitrary File Naming Conventions with gnatname::
126 * File Name Krunching with gnatkr::
127 * Renaming Files with gnatchop::
128
129 Handling Arbitrary File Naming Conventions with gnatname
130
131 * Arbitrary File Naming Conventions::
132 * Running gnatname::
133 * Switches for gnatname::
134 * Examples of gnatname Usage::
135
136 File Name Krunching with gnatkr
137
138 * About gnatkr::
139 * Using gnatkr::
140 * Krunching Method::
141 * Examples of gnatkr Usage::
142
143 Renaming Files with gnatchop
144
145 * Handling Files with Multiple Units::
146 * Operating gnatchop in Compilation Mode::
147 * Command Line for gnatchop::
148 * Switches for gnatchop::
149 * Examples of gnatchop Usage::
150
151 Configuration Pragmas
152
153 * Handling of Configuration Pragmas::
154 * The Configuration Pragmas Files::
155
156 GNAT and Libraries
157
158 * Introduction to Libraries in GNAT::
159 * General Ada Libraries::
160 * Stand-alone Ada Libraries::
161 * Rebuilding the GNAT Run-Time Library::
162
163 General Ada Libraries
164
165 * Building a library::
166 * Installing a library::
167 * Using a library::
168
169 Stand-alone Ada Libraries
170
171 * Introduction to Stand-alone Libraries::
172 * Building a Stand-alone Library::
173 * Creating a Stand-alone Library to be used in a non-Ada context::
174 * Restrictions in Stand-alone Libraries::
175
176 Conditional Compilation
177
178 * Modeling Conditional Compilation in Ada::
179 * Preprocessing with gnatprep::
180 * Integrated Preprocessing::
181
182 Modeling Conditional Compilation in Ada
183
184 * Use of Boolean Constants::
185 * Debugging - A Special Case::
186 * Conditionalizing Declarations::
187 * Use of Alternative Implementations::
188 * Preprocessing::
189
190 Preprocessing with gnatprep
191
192 * Preprocessing Symbols::
193 * Using gnatprep::
194 * Switches for gnatprep::
195 * Form of Definitions File::
196 * Form of Input Text for gnatprep::
197
198 Mixed Language Programming
199
200 * Interfacing to C::
201 * Calling Conventions::
202 * Building Mixed Ada and C++ Programs::
203 * Generating Ada Bindings for C and C++ headers::
204 * Generating C Headers for Ada Specifications::
205
206 Building Mixed Ada and C++ Programs
207
208 * Interfacing to C++::
209 * Linking a Mixed C++ & Ada Program::
210 * A Simple Example::
211 * Interfacing with C++ constructors::
212 * Interfacing with C++ at the Class Level::
213
214 Generating Ada Bindings for C and C++ headers
215
216 * Running the Binding Generator::
217 * Generating Bindings for C++ Headers::
218 * Switches::
219
220 Generating C Headers for Ada Specifications
221
222 * Running the C Header Generator::
223
224 GNAT and Other Compilation Models
225
226 * Comparison between GNAT and C/C++ Compilation Models::
227 * Comparison between GNAT and Conventional Ada Library Models::
228
229 Using GNAT Files with External Tools
230
231 * Using Other Utility Programs with GNAT::
232 * The External Symbol Naming Scheme of GNAT::
233
234 Building Executable Programs with GNAT
235
236 * Building with gnatmake::
237 * Compiling with gcc::
238 * Compiler Switches::
239 * Linker Switches::
240 * Binding with gnatbind::
241 * Linking with gnatlink::
242 * Using the GNU make Utility::
243
244 Building with gnatmake
245
246 * Running gnatmake::
247 * Switches for gnatmake::
248 * Mode Switches for gnatmake::
249 * Notes on the Command Line::
250 * How gnatmake Works::
251 * Examples of gnatmake Usage::
252
253 Compiling with gcc
254
255 * Compiling Programs::
256 * Search Paths and the Run-Time Library (RTL): Search Paths and the Run-Time Library RTL.
257 * Order of Compilation Issues::
258 * Examples::
259
260 Compiler Switches
261
262 * Alphabetical List of All Switches::
263 * Output and Error Message Control::
264 * Warning Message Control::
265 * Debugging and Assertion Control::
266 * Validity Checking::
267 * Style Checking::
268 * Run-Time Checks::
269 * Using gcc for Syntax Checking::
270 * Using gcc for Semantic Checking::
271 * Compiling Different Versions of Ada::
272 * Character Set Control::
273 * File Naming Control::
274 * Subprogram Inlining Control::
275 * Auxiliary Output Control::
276 * Debugging Control::
277 * Exception Handling Control::
278 * Units to Sources Mapping Files::
279 * Code Generation Control::
280
281 Binding with gnatbind
282
283 * Running gnatbind::
284 * Switches for gnatbind::
285 * Command-Line Access::
286 * Search Paths for gnatbind::
287 * Examples of gnatbind Usage::
288
289 Switches for gnatbind
290
291 * Consistency-Checking Modes::
292 * Binder Error Message Control::
293 * Elaboration Control::
294 * Output Control::
295 * Dynamic Allocation Control::
296 * Binding with Non-Ada Main Programs::
297 * Binding Programs with No Main Subprogram::
298
299 Linking with gnatlink
300
301 * Running gnatlink::
302 * Switches for gnatlink::
303
304 Using the GNU make Utility
305
306 * Using gnatmake in a Makefile::
307 * Automatically Creating a List of Directories::
308 * Generating the Command Line Switches::
309 * Overcoming Command Line Length Limits::
310
311 GNAT Utility Programs
312
313 * The File Cleanup Utility gnatclean::
314 * The GNAT Library Browser gnatls::
315
316 The File Cleanup Utility gnatclean
317
318 * Running gnatclean::
319 * Switches for gnatclean::
320
321 The GNAT Library Browser gnatls
322
323 * Running gnatls::
324 * Switches for gnatls::
325 * Example of gnatls Usage::
326
327 GNAT and Program Execution
328
329 * Running and Debugging Ada Programs::
330 * Profiling::
331 * Improving Performance::
332 * Overflow Check Handling in GNAT::
333 * Performing Dimensionality Analysis in GNAT::
334 * Stack Related Facilities::
335 * Memory Management Issues::
336
337 Running and Debugging Ada Programs
338
339 * The GNAT Debugger GDB::
340 * Running GDB::
341 * Introduction to GDB Commands::
342 * Using Ada Expressions::
343 * Calling User-Defined Subprograms::
344 * Using the next Command in a Function::
345 * Stopping When Ada Exceptions Are Raised::
346 * Ada Tasks::
347 * Debugging Generic Units::
348 * Remote Debugging with gdbserver::
349 * GNAT Abnormal Termination or Failure to Terminate::
350 * Naming Conventions for GNAT Source Files::
351 * Getting Internal Debugging Information::
352 * Stack Traceback::
353 * Pretty-Printers for the GNAT runtime::
354
355 Stack Traceback
356
357 * Non-Symbolic Traceback::
358 * Symbolic Traceback::
359
360 Profiling
361
362 * Profiling an Ada Program with gprof::
363
364 Profiling an Ada Program with gprof
365
366 * Compilation for profiling::
367 * Program execution::
368 * Running gprof::
369 * Interpretation of profiling results::
370
371 Improving Performance
372
373 * Performance Considerations::
374 * Text_IO Suggestions::
375 * Reducing Size of Executables with Unused Subprogram/Data Elimination::
376
377 Performance Considerations
378
379 * Controlling Run-Time Checks::
380 * Use of Restrictions::
381 * Optimization Levels::
382 * Debugging Optimized Code::
383 * Inlining of Subprograms::
384 * Floating Point Operations::
385 * Vectorization of loops::
386 * Other Optimization Switches::
387 * Optimization and Strict Aliasing::
388 * Aliased Variables and Optimization::
389 * Atomic Variables and Optimization::
390 * Passive Task Optimization::
391
392 Reducing Size of Executables with Unused Subprogram/Data Elimination
393
394 * About unused subprogram/data elimination::
395 * Compilation options::
396 * Example of unused subprogram/data elimination::
397
398 Overflow Check Handling in GNAT
399
400 * Background::
401 * Management of Overflows in GNAT::
402 * Specifying the Desired Mode::
403 * Default Settings::
404 * Implementation Notes::
405
406 Stack Related Facilities
407
408 * Stack Overflow Checking::
409 * Static Stack Usage Analysis::
410 * Dynamic Stack Usage Analysis::
411
412 Memory Management Issues
413
414 * Some Useful Memory Pools::
415 * The GNAT Debug Pool Facility::
416
417 Platform-Specific Information
418
419 * Run-Time Libraries::
420 * Specifying a Run-Time Library::
421 * GNU/Linux Topics::
422 * Microsoft Windows Topics::
423 * Mac OS Topics::
424
425 Run-Time Libraries
426
427 * Summary of Run-Time Configurations::
428
429 Specifying a Run-Time Library
430
431 * Choosing the Scheduling Policy::
432
433 GNU/Linux Topics
434
435 * Required Packages on GNU/Linux::
436
437 Microsoft Windows Topics
438
439 * Using GNAT on Windows::
440 * Using a network installation of GNAT::
441 * CONSOLE and WINDOWS subsystems::
442 * Temporary Files::
443 * Disabling Command Line Argument Expansion::
444 * Windows Socket Timeouts::
445 * Mixed-Language Programming on Windows::
446 * Windows Specific Add-Ons::
447
448 Mixed-Language Programming on Windows
449
450 * Windows Calling Conventions::
451 * Introduction to Dynamic Link Libraries (DLLs): Introduction to Dynamic Link Libraries DLLs.
452 * Using DLLs with GNAT::
453 * Building DLLs with GNAT Project files::
454 * Building DLLs with GNAT::
455 * Building DLLs with gnatdll::
456 * Ada DLLs and Finalization::
457 * Creating a Spec for Ada DLLs::
458 * GNAT and Windows Resources::
459 * Using GNAT DLLs from Microsoft Visual Studio Applications::
460 * Debugging a DLL::
461 * Setting Stack Size from gnatlink::
462 * Setting Heap Size from gnatlink::
463
464 Windows Calling Conventions
465
466 * C Calling Convention::
467 * Stdcall Calling Convention::
468 * Win32 Calling Convention::
469 * DLL Calling Convention::
470
471 Using DLLs with GNAT
472
473 * Creating an Ada Spec for the DLL Services::
474 * Creating an Import Library::
475
476 Building DLLs with gnatdll
477
478 * Limitations When Using Ada DLLs from Ada::
479 * Exporting Ada Entities::
480 * Ada DLLs and Elaboration::
481
482 Creating a Spec for Ada DLLs
483
484 * Creating the Definition File::
485 * Using gnatdll::
486
487 GNAT and Windows Resources
488
489 * Building Resources::
490 * Compiling Resources::
491 * Using Resources::
492
493 Debugging a DLL
494
495 * Program and DLL Both Built with GCC/GNAT::
496 * Program Built with Foreign Tools and DLL Built with GCC/GNAT::
497
498 Windows Specific Add-Ons
499
500 * Win32Ada::
501 * wPOSIX::
502
503 Mac OS Topics
504
505 * Codesigning the Debugger::
506
507 Elaboration Order Handling in GNAT
508
509 * Elaboration Code::
510 * Elaboration Order::
511 * Checking the Elaboration Order::
512 * Controlling the Elaboration Order in Ada::
513 * Controlling the Elaboration Order in GNAT::
514 * Mixing Elaboration Models::
515 * ABE Diagnostics::
516 * SPARK Diagnostics::
517 * Elaboration Circularities::
518 * Resolving Elaboration Circularities::
519 * Elaboration-related Compiler Switches::
520 * Summary of Procedures for Elaboration Control::
521 * Inspecting the Chosen Elaboration Order::
522
523 Inline Assembler
524
525 * Basic Assembler Syntax::
526 * A Simple Example of Inline Assembler::
527 * Output Variables in Inline Assembler::
528 * Input Variables in Inline Assembler::
529 * Inlining Inline Assembler Code::
530 * Other Asm Functionality::
531
532 Other Asm Functionality
533
534 * The Clobber Parameter::
535 * The Volatile Parameter::
536
537 @end detailmenu
538 @end menu
539
540 @node About This Guide,Getting Started with GNAT,Top,Top
541 @anchor{gnat_ugn/about_this_guide doc}@anchor{2}@anchor{gnat_ugn/about_this_guide about-this-guide}@anchor{3}@anchor{gnat_ugn/about_this_guide gnat-user-s-guide-for-native-platforms}@anchor{4}@anchor{gnat_ugn/about_this_guide id1}@anchor{5}
542 @chapter About This Guide
543
544
545
546 This guide describes the use of GNAT,
547 a compiler and software development
548 toolset for the full Ada programming language.
549 It documents the features of the compiler and tools, and explains
550 how to use them to build Ada applications.
551
552 GNAT implements Ada 95, Ada 2005, Ada 2012, and Ada 202x, and it may also be
553 invoked in Ada 83 compatibility mode.
554 By default, GNAT assumes Ada 2012, but you can override with a
555 compiler switch (@ref{6,,Compiling Different Versions of Ada})
556 to explicitly specify the language version.
557 Throughout this manual, references to ‘Ada’ without a year suffix
558 apply to all Ada versions of the language, starting with Ada 95.
559
560 @menu
561 * What This Guide Contains::
562 * What You Should Know before Reading This Guide::
563 * Related Information::
564 * Conventions::
565
566 @end menu
567
568 @node What This Guide Contains,What You Should Know before Reading This Guide,,About This Guide
569 @anchor{gnat_ugn/about_this_guide what-this-guide-contains}@anchor{7}
570 @section What This Guide Contains
571
572
573 This guide contains the following chapters:
574
575
576 @itemize *
577
578 @item
579 @ref{8,,Getting Started with GNAT} describes how to get started compiling
580 and running Ada programs with the GNAT Ada programming environment.
581
582 @item
583 @ref{9,,The GNAT Compilation Model} describes the compilation model used
584 by GNAT.
585
586 @item
587 @ref{a,,Building Executable Programs with GNAT} describes how to use the
588 main GNAT tools to build executable programs, and it also gives examples of
589 using the GNU make utility with GNAT.
590
591 @item
592 @ref{b,,GNAT Utility Programs} explains the various utility programs that
593 are included in the GNAT environment
594
595 @item
596 @ref{c,,GNAT and Program Execution} covers a number of topics related to
597 running, debugging, and tuning the performace of programs developed
598 with GNAT
599 @end itemize
600
601 Appendices cover several additional topics:
602
603
604 @itemize *
605
606 @item
607 @ref{d,,Platform-Specific Information} describes the different run-time
608 library implementations and also presents information on how to use
609 GNAT on several specific platforms
610
611 @item
612 @ref{e,,Example of Binder Output File} shows the source code for the binder
613 output file for a sample program.
614
615 @item
616 @ref{f,,Elaboration Order Handling in GNAT} describes how GNAT helps
617 you deal with elaboration order issues.
618
619 @item
620 @ref{10,,Inline Assembler} shows how to use the inline assembly facility
621 in an Ada program.
622 @end itemize
623
624 @node What You Should Know before Reading This Guide,Related Information,What This Guide Contains,About This Guide
625 @anchor{gnat_ugn/about_this_guide what-you-should-know-before-reading-this-guide}@anchor{11}
626 @section What You Should Know before Reading This Guide
627
628
629 @geindex Ada 95 Language Reference Manual
630
631 @geindex Ada 2005 Language Reference Manual
632
633 This guide assumes a basic familiarity with the Ada 95 language, as
634 described in the International Standard ANSI/ISO/IEC-8652:1995, January
635 1995.
636 Reference manuals for Ada 95, Ada 2005, and Ada 2012 are included in
637 the GNAT documentation package.
638
639 @node Related Information,Conventions,What You Should Know before Reading This Guide,About This Guide
640 @anchor{gnat_ugn/about_this_guide related-information}@anchor{12}
641 @section Related Information
642
643
644 For further information about Ada and related tools, please refer to the
645 following documents:
646
647
648 @itemize *
649
650 @item
651 @cite{Ada 95 Reference Manual}, @cite{Ada 2005 Reference Manual}, and
652 @cite{Ada 2012 Reference Manual}, which contain reference
653 material for the several revisions of the Ada language standard.
654
655 @item
656 @cite{GNAT Reference_Manual}, which contains all reference material for the GNAT
657 implementation of Ada.
658
659 @item
660 @cite{Using GNAT Studio}, which describes the GNAT Studio
661 Integrated Development Environment.
662
663 @item
664 @cite{GNAT Studio Tutorial}, which introduces the
665 main GNAT Studio features through examples.
666
667 @item
668 @cite{Debugging with GDB},
669 for all details on the use of the GNU source-level debugger.
670
671 @item
672 @cite{GNU Emacs Manual},
673 for full information on the extensible editor and programming
674 environment Emacs.
675 @end itemize
676
677 @node Conventions,,Related Information,About This Guide
678 @anchor{gnat_ugn/about_this_guide conventions}@anchor{13}
679 @section Conventions
680
681
682 @geindex Conventions
683 @geindex typographical
684
685 @geindex Typographical conventions
686
687 Following are examples of the typographical and graphic conventions used
688 in this guide:
689
690
691 @itemize *
692
693 @item
694 @code{Functions}, @code{utility program names}, @code{standard names},
695 and @code{classes}.
696
697 @item
698 @code{Option flags}
699
700 @item
701 @code{File names}
702
703 @item
704 @code{Variables}
705
706 @item
707 @emph{Emphasis}
708
709 @item
710 [optional information or parameters]
711
712 @item
713 Examples are described by text
714
715 @example
716 and then shown this way.
717 @end example
718
719 @item
720 Commands that are entered by the user are shown as preceded by a prompt string
721 comprising the @code{$} character followed by a space.
722
723 @item
724 Full file names are shown with the ‘/’ character
725 as the directory separator; e.g., @code{parent-dir/subdir/myfile.adb}.
726 If you are using GNAT on a Windows platform, please note that
727 the ‘' character should be used instead.
728 @end itemize
729
730 @node Getting Started with GNAT,The GNAT Compilation Model,About This Guide,Top
731 @anchor{gnat_ugn/getting_started_with_gnat doc}@anchor{14}@anchor{gnat_ugn/getting_started_with_gnat getting-started-with-gnat}@anchor{8}@anchor{gnat_ugn/getting_started_with_gnat id1}@anchor{15}
732 @chapter Getting Started with GNAT
733
734
735 This chapter describes how to use GNAT’s command line interface to build
736 executable Ada programs.
737 On most platforms a visually oriented Integrated Development Environment
738 is also available: GNAT Studio.
739 GNAT Studio offers a graphical “look and feel”, support for development in
740 other programming languages, comprehensive browsing features, and
741 many other capabilities.
742 For information on GNAT Studio please refer to the
743 @cite{GNAT Studio documentation}.
744
745 @menu
746 * System Requirements::
747 * Running GNAT::
748 * Running a Simple Ada Program::
749 * Running a Program with Multiple Units::
750
751 @end menu
752
753 @node System Requirements,Running GNAT,,Getting Started with GNAT
754 @anchor{gnat_ugn/getting_started_with_gnat id2}@anchor{16}@anchor{gnat_ugn/getting_started_with_gnat system-requirements}@anchor{17}
755 @section System Requirements
756
757
758 Even though any machine can run the GNAT toolset and GNAT Studio IDE, in order
759 to get the best experience, we recommend using a machine with as many cores
760 as possible since all individual compilations can run in parallel.
761 A comfortable setup for a compiler server is a machine with 24 physical cores
762 or more, with at least 48 GB of memory (2 GB per core).
763
764 For a desktop machine, a minimum of 4 cores is recommended (8 preferred),
765 with at least 2GB per core (so 8 to 16GB).
766
767 In addition, for running and navigating sources in GNAT Studio smoothly, we
768 recommend at least 1.5 GB plus 3 GB of RAM per 1 million source line of code.
769 In other words, we recommend at least 3 GB for for 500K lines of code and
770 7.5 GB for 2 million lines of code.
771
772 Note that using local and fast drives will also make a difference in terms of
773 build and link time. Network drives such as NFS, SMB, or worse, configuration
774 management filesystems (such as ClearCase dynamic views) should be avoided as
775 much as possible and will produce very degraded performance (typically 2 to 3
776 times slower than on local fast drives). If such slow drives cannot be avoided
777 for accessing the source code, then you should at least configure your project
778 file so that the result of the compilation is stored on a drive local to the
779 machine performing the run. This can be achieved by setting the @code{Object_Dir}
780 project file attribute.
781
782 @node Running GNAT,Running a Simple Ada Program,System Requirements,Getting Started with GNAT
783 @anchor{gnat_ugn/getting_started_with_gnat id3}@anchor{18}@anchor{gnat_ugn/getting_started_with_gnat running-gnat}@anchor{19}
784 @section Running GNAT
785
786
787 Three steps are needed to create an executable file from an Ada source
788 file:
789
790
791 @itemize *
792
793 @item
794 The source file(s) must be compiled.
795
796 @item
797 The file(s) must be bound using the GNAT binder.
798
799 @item
800 All appropriate object files must be linked to produce an executable.
801 @end itemize
802
803 All three steps are most commonly handled by using the @code{gnatmake}
804 utility program that, given the name of the main program, automatically
805 performs the necessary compilation, binding and linking steps.
806
807 @node Running a Simple Ada Program,Running a Program with Multiple Units,Running GNAT,Getting Started with GNAT
808 @anchor{gnat_ugn/getting_started_with_gnat id4}@anchor{1a}@anchor{gnat_ugn/getting_started_with_gnat running-a-simple-ada-program}@anchor{1b}
809 @section Running a Simple Ada Program
810
811
812 Any text editor may be used to prepare an Ada program.
813 (If Emacs is used, the optional Ada mode may be helpful in laying out the
814 program.)
815 The program text is a normal text file. We will assume in our initial
816 example that you have used your editor to prepare the following
817 standard format text file:
818
819 @example
820 with Ada.Text_IO; use Ada.Text_IO;
821 procedure Hello is
822 begin
823 Put_Line ("Hello WORLD!");
824 end Hello;
825 @end example
826
827 This file should be named @code{hello.adb}.
828 With the normal default file naming conventions, GNAT requires
829 that each file
830 contain a single compilation unit whose file name is the
831 unit name,
832 with periods replaced by hyphens; the
833 extension is @code{ads} for a
834 spec and @code{adb} for a body.
835 You can override this default file naming convention by use of the
836 special pragma @code{Source_File_Name} (for further information please
837 see @ref{1c,,Using Other File Names}).
838 Alternatively, if you want to rename your files according to this default
839 convention, which is probably more convenient if you will be using GNAT
840 for all your compilations, then the @code{gnatchop} utility
841 can be used to generate correctly-named source files
842 (see @ref{1d,,Renaming Files with gnatchop}).
843
844 You can compile the program using the following command (@code{$} is used
845 as the command prompt in the examples in this document):
846
847 @example
848 $ gcc -c hello.adb
849 @end example
850
851 @code{gcc} is the command used to run the compiler. This compiler is
852 capable of compiling programs in several languages, including Ada and
853 C. It assumes that you have given it an Ada program if the file extension is
854 either @code{.ads} or @code{.adb}, and it will then call
855 the GNAT compiler to compile the specified file.
856
857 The @code{-c} switch is required. It tells @code{gcc} to only do a
858 compilation. (For C programs, @code{gcc} can also do linking, but this
859 capability is not used directly for Ada programs, so the @code{-c}
860 switch must always be present.)
861
862 This compile command generates a file
863 @code{hello.o}, which is the object
864 file corresponding to your Ada program. It also generates
865 an ‘Ada Library Information’ file @code{hello.ali},
866 which contains additional information used to check
867 that an Ada program is consistent.
868
869 To build an executable file, use either @code{gnatmake} or gprbuild with
870 the name of the main file: these tools are builders that will take care of
871 all the necessary build steps in the correct order.
872 In particular, these builders automatically recompile any sources that have
873 been modified since they were last compiled, or sources that depend
874 on such modified sources, so that ‘version skew’ is avoided.
875
876 @geindex Version skew (avoided by `@w{`}gnatmake`@w{`})
877
878 @example
879 $ gnatmake hello.adb
880 @end example
881
882 The result is an executable program called @code{hello}, which can be
883 run by entering:
884
885 @example
886 $ hello
887 @end example
888
889 assuming that the current directory is on the search path
890 for executable programs.
891
892 and, if all has gone well, you will see:
893
894 @example
895 Hello WORLD!
896 @end example
897
898 appear in response to this command.
899
900 @node Running a Program with Multiple Units,,Running a Simple Ada Program,Getting Started with GNAT
901 @anchor{gnat_ugn/getting_started_with_gnat id5}@anchor{1e}@anchor{gnat_ugn/getting_started_with_gnat running-a-program-with-multiple-units}@anchor{1f}
902 @section Running a Program with Multiple Units
903
904
905 Consider a slightly more complicated example that has three files: a
906 main program, and the spec and body of a package:
907
908 @example
909 package Greetings is
910 procedure Hello;
911 procedure Goodbye;
912 end Greetings;
913
914 with Ada.Text_IO; use Ada.Text_IO;
915 package body Greetings is
916 procedure Hello is
917 begin
918 Put_Line ("Hello WORLD!");
919 end Hello;
920
921 procedure Goodbye is
922 begin
923 Put_Line ("Goodbye WORLD!");
924 end Goodbye;
925 end Greetings;
926
927 with Greetings;
928 procedure Gmain is
929 begin
930 Greetings.Hello;
931 Greetings.Goodbye;
932 end Gmain;
933 @end example
934
935 Following the one-unit-per-file rule, place this program in the
936 following three separate files:
937
938
939 @table @asis
940
941 @item @emph{greetings.ads}
942
943 spec of package @code{Greetings}
944
945 @item @emph{greetings.adb}
946
947 body of package @code{Greetings}
948
949 @item @emph{gmain.adb}
950
951 body of main program
952 @end table
953
954 Note that there is no required order of compilation when using GNAT.
955 In particular it is perfectly fine to compile the main program first.
956 Also, it is not necessary to compile package specs in the case where
957 there is an accompanying body; you only need to compile the body. If you want
958 to submit these files to the compiler for semantic checking and not code
959 generation, then use the @code{-gnatc} switch:
960
961 @example
962 $ gcc -c greetings.ads -gnatc
963 @end example
964
965 Although the compilation can be done in separate steps, in practice it is
966 almost always more convenient to use the @code{gnatmake} or @code{gprbuild} tools:
967
968 @example
969 $ gnatmake gmain.adb
970 @end example
971
972 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
973
974 @node The GNAT Compilation Model,Building Executable Programs with GNAT,Getting Started with GNAT,Top
975 @anchor{gnat_ugn/the_gnat_compilation_model doc}@anchor{20}@anchor{gnat_ugn/the_gnat_compilation_model id1}@anchor{21}@anchor{gnat_ugn/the_gnat_compilation_model the-gnat-compilation-model}@anchor{9}
976 @chapter The GNAT Compilation Model
977
978
979 @geindex GNAT compilation model
980
981 @geindex Compilation model
982
983 This chapter describes the compilation model used by GNAT. Although
984 similar to that used by other languages such as C and C++, this model
985 is substantially different from the traditional Ada compilation models,
986 which are based on a centralized program library. The chapter covers
987 the following material:
988
989
990 @itemize *
991
992 @item
993 Topics related to source file makeup and naming
994
995
996 @itemize *
997
998 @item
999 @ref{22,,Source Representation}
1000
1001 @item
1002 @ref{23,,Foreign Language Representation}
1003
1004 @item
1005 @ref{24,,File Naming Topics and Utilities}
1006 @end itemize
1007
1008 @item
1009 @ref{25,,Configuration Pragmas}
1010
1011 @item
1012 @ref{26,,Generating Object Files}
1013
1014 @item
1015 @ref{27,,Source Dependencies}
1016
1017 @item
1018 @ref{28,,The Ada Library Information Files}
1019
1020 @item
1021 @ref{29,,Binding an Ada Program}
1022
1023 @item
1024 @ref{2a,,GNAT and Libraries}
1025
1026 @item
1027 @ref{2b,,Conditional Compilation}
1028
1029 @item
1030 @ref{2c,,Mixed Language Programming}
1031
1032 @item
1033 @ref{2d,,GNAT and Other Compilation Models}
1034
1035 @item
1036 @ref{2e,,Using GNAT Files with External Tools}
1037 @end itemize
1038
1039 @menu
1040 * Source Representation::
1041 * Foreign Language Representation::
1042 * File Naming Topics and Utilities::
1043 * Configuration Pragmas::
1044 * Generating Object Files::
1045 * Source Dependencies::
1046 * The Ada Library Information Files::
1047 * Binding an Ada Program::
1048 * GNAT and Libraries::
1049 * Conditional Compilation::
1050 * Mixed Language Programming::
1051 * GNAT and Other Compilation Models::
1052 * Using GNAT Files with External Tools::
1053
1054 @end menu
1055
1056 @node Source Representation,Foreign Language Representation,,The GNAT Compilation Model
1057 @anchor{gnat_ugn/the_gnat_compilation_model id2}@anchor{2f}@anchor{gnat_ugn/the_gnat_compilation_model source-representation}@anchor{22}
1058 @section Source Representation
1059
1060
1061 @geindex Latin-1
1062
1063 @geindex VT
1064 @geindex HT
1065 @geindex CR
1066 @geindex LF
1067 @geindex FF
1068
1069 Ada source programs are represented in standard text files, using
1070 Latin-1 coding. Latin-1 is an 8-bit code that includes the familiar
1071 7-bit ASCII set, plus additional characters used for
1072 representing foreign languages (see @ref{23,,Foreign Language Representation}
1073 for support of non-USA character sets). The format effector characters
1074 are represented using their standard ASCII encodings, as follows:
1075
1076 @quotation
1077
1078
1079 @multitable {xxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxx}
1080 @item
1081
1082 Character
1083
1084 @tab
1085
1086 Effect
1087
1088 @tab
1089
1090 Code
1091
1092 @item
1093
1094 @code{VT}
1095
1096 @tab
1097
1098 Vertical tab
1099
1100 @tab
1101
1102 @code{16#0B#}
1103
1104 @item
1105
1106 @code{HT}
1107
1108 @tab
1109
1110 Horizontal tab
1111
1112 @tab
1113
1114 @code{16#09#}
1115
1116 @item
1117
1118 @code{CR}
1119
1120 @tab
1121
1122 Carriage return
1123
1124 @tab
1125
1126 @code{16#0D#}
1127
1128 @item
1129
1130 @code{LF}
1131
1132 @tab
1133
1134 Line feed
1135
1136 @tab
1137
1138 @code{16#0A#}
1139
1140 @item
1141
1142 @code{FF}
1143
1144 @tab
1145
1146 Form feed
1147
1148 @tab
1149
1150 @code{16#0C#}
1151
1152 @end multitable
1153
1154 @end quotation
1155
1156 Source files are in standard text file format. In addition, GNAT will
1157 recognize a wide variety of stream formats, in which the end of
1158 physical lines is marked by any of the following sequences:
1159 @code{LF}, @code{CR}, @code{CR-LF}, or @code{LF-CR}. This is useful
1160 in accommodating files that are imported from other operating systems.
1161
1162 @geindex End of source file; Source file@comma{} end
1163
1164 @geindex SUB (control character)
1165
1166 The end of a source file is normally represented by the physical end of
1167 file. However, the control character @code{16#1A#} (@code{SUB}) is also
1168 recognized as signalling the end of the source file. Again, this is
1169 provided for compatibility with other operating systems where this
1170 code is used to represent the end of file.
1171
1172 @geindex spec (definition)
1173 @geindex compilation (definition)
1174
1175 Each file contains a single Ada compilation unit, including any pragmas
1176 associated with the unit. For example, this means you must place a
1177 package declaration (a package @emph{spec}) and the corresponding body in
1178 separate files. An Ada @emph{compilation} (which is a sequence of
1179 compilation units) is represented using a sequence of files. Similarly,
1180 you will place each subunit or child unit in a separate file.
1181
1182 @node Foreign Language Representation,File Naming Topics and Utilities,Source Representation,The GNAT Compilation Model
1183 @anchor{gnat_ugn/the_gnat_compilation_model foreign-language-representation}@anchor{23}@anchor{gnat_ugn/the_gnat_compilation_model id3}@anchor{30}
1184 @section Foreign Language Representation
1185
1186
1187 GNAT supports the standard character sets defined in Ada as well as
1188 several other non-standard character sets for use in localized versions
1189 of the compiler (@ref{31,,Character Set Control}).
1190
1191 @menu
1192 * Latin-1::
1193 * Other 8-Bit Codes::
1194 * Wide_Character Encodings::
1195 * Wide_Wide_Character Encodings::
1196
1197 @end menu
1198
1199 @node Latin-1,Other 8-Bit Codes,,Foreign Language Representation
1200 @anchor{gnat_ugn/the_gnat_compilation_model id4}@anchor{32}@anchor{gnat_ugn/the_gnat_compilation_model latin-1}@anchor{33}
1201 @subsection Latin-1
1202
1203
1204 @geindex Latin-1
1205
1206 The basic character set is Latin-1. This character set is defined by ISO
1207 standard 8859, part 1. The lower half (character codes @code{16#00#}
1208 … @code{16#7F#)} is identical to standard ASCII coding, but the upper
1209 half is used to represent additional characters. These include extended letters
1210 used by European languages, such as French accents, the vowels with umlauts
1211 used in German, and the extra letter A-ring used in Swedish.
1212
1213 @geindex Ada.Characters.Latin_1
1214
1215 For a complete list of Latin-1 codes and their encodings, see the source
1216 file of library unit @code{Ada.Characters.Latin_1} in file
1217 @code{a-chlat1.ads}.
1218 You may use any of these extended characters freely in character or
1219 string literals. In addition, the extended characters that represent
1220 letters can be used in identifiers.
1221
1222 @node Other 8-Bit Codes,Wide_Character Encodings,Latin-1,Foreign Language Representation
1223 @anchor{gnat_ugn/the_gnat_compilation_model id5}@anchor{34}@anchor{gnat_ugn/the_gnat_compilation_model other-8-bit-codes}@anchor{35}
1224 @subsection Other 8-Bit Codes
1225
1226
1227 GNAT also supports several other 8-bit coding schemes:
1228
1229 @geindex Latin-2
1230
1231 @geindex ISO 8859-2
1232
1233
1234 @table @asis
1235
1236 @item @emph{ISO 8859-2 (Latin-2)}
1237
1238 Latin-2 letters allowed in identifiers, with uppercase and lowercase
1239 equivalence.
1240 @end table
1241
1242 @geindex Latin-3
1243
1244 @geindex ISO 8859-3
1245
1246
1247 @table @asis
1248
1249 @item @emph{ISO 8859-3 (Latin-3)}
1250
1251 Latin-3 letters allowed in identifiers, with uppercase and lowercase
1252 equivalence.
1253 @end table
1254
1255 @geindex Latin-4
1256
1257 @geindex ISO 8859-4
1258
1259
1260 @table @asis
1261
1262 @item @emph{ISO 8859-4 (Latin-4)}
1263
1264 Latin-4 letters allowed in identifiers, with uppercase and lowercase
1265 equivalence.
1266 @end table
1267
1268 @geindex ISO 8859-5
1269
1270 @geindex Cyrillic
1271
1272
1273 @table @asis
1274
1275 @item @emph{ISO 8859-5 (Cyrillic)}
1276
1277 ISO 8859-5 letters (Cyrillic) allowed in identifiers, with uppercase and
1278 lowercase equivalence.
1279 @end table
1280
1281 @geindex ISO 8859-15
1282
1283 @geindex Latin-9
1284
1285
1286 @table @asis
1287
1288 @item @emph{ISO 8859-15 (Latin-9)}
1289
1290 ISO 8859-15 (Latin-9) letters allowed in identifiers, with uppercase and
1291 lowercase equivalence
1292 @end table
1293
1294 @geindex code page 437 (IBM PC)
1295
1296
1297 @table @asis
1298
1299 @item @emph{IBM PC (code page 437)}
1300
1301 This code page is the normal default for PCs in the U.S. It corresponds
1302 to the original IBM PC character set. This set has some, but not all, of
1303 the extended Latin-1 letters, but these letters do not have the same
1304 encoding as Latin-1. In this mode, these letters are allowed in
1305 identifiers with uppercase and lowercase equivalence.
1306 @end table
1307
1308 @geindex code page 850 (IBM PC)
1309
1310
1311 @table @asis
1312
1313 @item @emph{IBM PC (code page 850)}
1314
1315 This code page is a modification of 437 extended to include all the
1316 Latin-1 letters, but still not with the usual Latin-1 encoding. In this
1317 mode, all these letters are allowed in identifiers with uppercase and
1318 lowercase equivalence.
1319
1320 @item @emph{Full Upper 8-bit}
1321
1322 Any character in the range 80-FF allowed in identifiers, and all are
1323 considered distinct. In other words, there are no uppercase and lowercase
1324 equivalences in this range. This is useful in conjunction with
1325 certain encoding schemes used for some foreign character sets (e.g.,
1326 the typical method of representing Chinese characters on the PC).
1327
1328 @item @emph{No Upper-Half}
1329
1330 No upper-half characters in the range 80-FF are allowed in identifiers.
1331 This gives Ada 83 compatibility for identifier names.
1332 @end table
1333
1334 For precise data on the encodings permitted, and the uppercase and lowercase
1335 equivalences that are recognized, see the file @code{csets.adb} in
1336 the GNAT compiler sources. You will need to obtain a full source release
1337 of GNAT to obtain this file.
1338
1339 @node Wide_Character Encodings,Wide_Wide_Character Encodings,Other 8-Bit Codes,Foreign Language Representation
1340 @anchor{gnat_ugn/the_gnat_compilation_model id6}@anchor{36}@anchor{gnat_ugn/the_gnat_compilation_model wide-character-encodings}@anchor{37}
1341 @subsection Wide_Character Encodings
1342
1343
1344 GNAT allows wide character codes to appear in character and string
1345 literals, and also optionally in identifiers, by means of the following
1346 possible encoding schemes:
1347
1348
1349 @table @asis
1350
1351 @item @emph{Hex Coding}
1352
1353 In this encoding, a wide character is represented by the following five
1354 character sequence:
1355
1356 @example
1357 ESC a b c d
1358 @end example
1359
1360 where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1361 characters (using uppercase letters) of the wide character code. For
1362 example, ESC A345 is used to represent the wide character with code
1363 @code{16#A345#}.
1364 This scheme is compatible with use of the full Wide_Character set.
1365
1366 @item @emph{Upper-Half Coding}
1367
1368 @geindex Upper-Half Coding
1369
1370 The wide character with encoding @code{16#abcd#} where the upper bit is on
1371 (in other words, ‘a’ is in the range 8-F) is represented as two bytes,
1372 @code{16#ab#} and @code{16#cd#}. The second byte cannot be a format control
1373 character, but is not required to be in the upper half. This method can
1374 be also used for shift-JIS or EUC, where the internal coding matches the
1375 external coding.
1376
1377 @item @emph{Shift JIS Coding}
1378
1379 @geindex Shift JIS Coding
1380
1381 A wide character is represented by a two-character sequence,
1382 @code{16#ab#} and
1383 @code{16#cd#}, with the restrictions described for upper-half encoding as
1384 described above. The internal character code is the corresponding JIS
1385 character according to the standard algorithm for Shift-JIS
1386 conversion. Only characters defined in the JIS code set table can be
1387 used with this encoding method.
1388
1389 @item @emph{EUC Coding}
1390
1391 @geindex EUC Coding
1392
1393 A wide character is represented by a two-character sequence
1394 @code{16#ab#} and
1395 @code{16#cd#}, with both characters being in the upper half. The internal
1396 character code is the corresponding JIS character according to the EUC
1397 encoding algorithm. Only characters defined in the JIS code set table
1398 can be used with this encoding method.
1399
1400 @item @emph{UTF-8 Coding}
1401
1402 A wide character is represented using
1403 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
1404 10646-1/Am.2. Depending on the character value, the representation
1405 is a one, two, or three byte sequence:
1406
1407 @example
1408 16#0000#-16#007f#: 2#0xxxxxxx#
1409 16#0080#-16#07ff#: 2#110xxxxx# 2#10xxxxxx#
1410 16#0800#-16#ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx#
1411 @end example
1412
1413 where the @code{xxx} bits correspond to the left-padded bits of the
1414 16-bit character value. Note that all lower half ASCII characters
1415 are represented as ASCII bytes and all upper half characters and
1416 other wide characters are represented as sequences of upper-half
1417 (The full UTF-8 scheme allows for encoding 31-bit characters as
1418 6-byte sequences, and in the following section on wide wide
1419 characters, the use of these sequences is documented).
1420
1421 @item @emph{Brackets Coding}
1422
1423 In this encoding, a wide character is represented by the following eight
1424 character sequence:
1425
1426 @example
1427 [ " a b c d " ]
1428 @end example
1429
1430 where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1431 characters (using uppercase letters) of the wide character code. For
1432 example, [‘A345’] is used to represent the wide character with code
1433 @code{16#A345#}. It is also possible (though not required) to use the
1434 Brackets coding for upper half characters. For example, the code
1435 @code{16#A3#} can be represented as @code{['A3']}.
1436
1437 This scheme is compatible with use of the full Wide_Character set,
1438 and is also the method used for wide character encoding in some standard
1439 ACATS (Ada Conformity Assessment Test Suite) test suite distributions.
1440 @end table
1441
1442 @cartouche
1443 @quotation Note
1444 Some of these coding schemes do not permit the full use of the
1445 Ada character set. For example, neither Shift JIS nor EUC allow the
1446 use of the upper half of the Latin-1 set.
1447 @end quotation
1448 @end cartouche
1449
1450 @node Wide_Wide_Character Encodings,,Wide_Character Encodings,Foreign Language Representation
1451 @anchor{gnat_ugn/the_gnat_compilation_model id7}@anchor{38}@anchor{gnat_ugn/the_gnat_compilation_model wide-wide-character-encodings}@anchor{39}
1452 @subsection Wide_Wide_Character Encodings
1453
1454
1455 GNAT allows wide wide character codes to appear in character and string
1456 literals, and also optionally in identifiers, by means of the following
1457 possible encoding schemes:
1458
1459
1460 @table @asis
1461
1462 @item @emph{UTF-8 Coding}
1463
1464 A wide character is represented using
1465 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
1466 10646-1/Am.2. Depending on the character value, the representation
1467 of character codes with values greater than 16#FFFF# is a
1468 is a four, five, or six byte sequence:
1469
1470 @example
1471 16#01_0000#-16#10_FFFF#: 11110xxx 10xxxxxx 10xxxxxx
1472 10xxxxxx
1473 16#0020_0000#-16#03FF_FFFF#: 111110xx 10xxxxxx 10xxxxxx
1474 10xxxxxx 10xxxxxx
1475 16#0400_0000#-16#7FFF_FFFF#: 1111110x 10xxxxxx 10xxxxxx
1476 10xxxxxx 10xxxxxx 10xxxxxx
1477 @end example
1478
1479 where the @code{xxx} bits correspond to the left-padded bits of the
1480 32-bit character value.
1481
1482 @item @emph{Brackets Coding}
1483
1484 In this encoding, a wide wide character is represented by the following ten or
1485 twelve byte character sequence:
1486
1487 @example
1488 [ " a b c d e f " ]
1489 [ " a b c d e f g h " ]
1490 @end example
1491
1492 where @code{a-h} are the six or eight hexadecimal
1493 characters (using uppercase letters) of the wide wide character code. For
1494 example, [“1F4567”] is used to represent the wide wide character with code
1495 @code{16#001F_4567#}.
1496
1497 This scheme is compatible with use of the full Wide_Wide_Character set,
1498 and is also the method used for wide wide character encoding in some standard
1499 ACATS (Ada Conformity Assessment Test Suite) test suite distributions.
1500 @end table
1501
1502 @node File Naming Topics and Utilities,Configuration Pragmas,Foreign Language Representation,The GNAT Compilation Model
1503 @anchor{gnat_ugn/the_gnat_compilation_model file-naming-topics-and-utilities}@anchor{24}@anchor{gnat_ugn/the_gnat_compilation_model id8}@anchor{3a}
1504 @section File Naming Topics and Utilities
1505
1506
1507 GNAT has a default file naming scheme and also provides the user with
1508 a high degree of control over how the names and extensions of the
1509 source files correspond to the Ada compilation units that they contain.
1510
1511 @menu
1512 * File Naming Rules::
1513 * Using Other File Names::
1514 * Alternative File Naming Schemes::
1515 * Handling Arbitrary File Naming Conventions with gnatname::
1516 * File Name Krunching with gnatkr::
1517 * Renaming Files with gnatchop::
1518
1519 @end menu
1520
1521 @node File Naming Rules,Using Other File Names,,File Naming Topics and Utilities
1522 @anchor{gnat_ugn/the_gnat_compilation_model file-naming-rules}@anchor{3b}@anchor{gnat_ugn/the_gnat_compilation_model id9}@anchor{3c}
1523 @subsection File Naming Rules
1524
1525
1526 The default file name is determined by the name of the unit that the
1527 file contains. The name is formed by taking the full expanded name of
1528 the unit and replacing the separating dots with hyphens and using
1529 lowercase for all letters.
1530
1531 An exception arises if the file name generated by the above rules starts
1532 with one of the characters
1533 @code{a}, @code{g}, @code{i}, or @code{s}, and the second character is a
1534 minus. In this case, the character tilde is used in place
1535 of the minus. The reason for this special rule is to avoid clashes with
1536 the standard names for child units of the packages System, Ada,
1537 Interfaces, and GNAT, which use the prefixes
1538 @code{s-}, @code{a-}, @code{i-}, and @code{g-},
1539 respectively.
1540
1541 The file extension is @code{.ads} for a spec and
1542 @code{.adb} for a body. The following table shows some
1543 examples of these rules.
1544
1545 @quotation
1546
1547
1548 @multitable {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
1549 @item
1550
1551 Source File
1552
1553 @tab
1554
1555 Ada Compilation Unit
1556
1557 @item
1558
1559 @code{main.ads}
1560
1561 @tab
1562
1563 Main (spec)
1564
1565 @item
1566
1567 @code{main.adb}
1568
1569 @tab
1570
1571 Main (body)
1572
1573 @item
1574
1575 @code{arith_functions.ads}
1576
1577 @tab
1578
1579 Arith_Functions (package spec)
1580
1581 @item
1582
1583 @code{arith_functions.adb}
1584
1585 @tab
1586
1587 Arith_Functions (package body)
1588
1589 @item
1590
1591 @code{func-spec.ads}
1592
1593 @tab
1594
1595 Func.Spec (child package spec)
1596
1597 @item
1598
1599 @code{func-spec.adb}
1600
1601 @tab
1602
1603 Func.Spec (child package body)
1604
1605 @item
1606
1607 @code{main-sub.adb}
1608
1609 @tab
1610
1611 Sub (subunit of Main)
1612
1613 @item
1614
1615 @code{a~bad.adb}
1616
1617 @tab
1618
1619 A.Bad (child package body)
1620
1621 @end multitable
1622
1623 @end quotation
1624
1625 Following these rules can result in excessively long
1626 file names if corresponding
1627 unit names are long (for example, if child units or subunits are
1628 heavily nested). An option is available to shorten such long file names
1629 (called file name ‘krunching’). This may be particularly useful when
1630 programs being developed with GNAT are to be used on operating systems
1631 with limited file name lengths. @ref{3d,,Using gnatkr}.
1632
1633 Of course, no file shortening algorithm can guarantee uniqueness over
1634 all possible unit names; if file name krunching is used, it is your
1635 responsibility to ensure no name clashes occur. Alternatively you
1636 can specify the exact file names that you want used, as described
1637 in the next section. Finally, if your Ada programs are migrating from a
1638 compiler with a different naming convention, you can use the gnatchop
1639 utility to produce source files that follow the GNAT naming conventions.
1640 (For details see @ref{1d,,Renaming Files with gnatchop}.)
1641
1642 Note: in the case of Windows or Mac OS operating systems, case is not
1643 significant. So for example on Windows if the canonical name is
1644 @code{main-sub.adb}, you can use the file name @code{Main-Sub.adb} instead.
1645 However, case is significant for other operating systems, so for example,
1646 if you want to use other than canonically cased file names on a Unix system,
1647 you need to follow the procedures described in the next section.
1648
1649 @node Using Other File Names,Alternative File Naming Schemes,File Naming Rules,File Naming Topics and Utilities
1650 @anchor{gnat_ugn/the_gnat_compilation_model id10}@anchor{3e}@anchor{gnat_ugn/the_gnat_compilation_model using-other-file-names}@anchor{1c}
1651 @subsection Using Other File Names
1652
1653
1654 @geindex File names
1655
1656 In the previous section, we have described the default rules used by
1657 GNAT to determine the file name in which a given unit resides. It is
1658 often convenient to follow these default rules, and if you follow them,
1659 the compiler knows without being explicitly told where to find all
1660 the files it needs.
1661
1662 @geindex Source_File_Name pragma
1663
1664 However, in some cases, particularly when a program is imported from
1665 another Ada compiler environment, it may be more convenient for the
1666 programmer to specify which file names contain which units. GNAT allows
1667 arbitrary file names to be used by means of the Source_File_Name pragma.
1668 The form of this pragma is as shown in the following examples:
1669
1670 @example
1671 pragma Source_File_Name (My_Utilities.Stacks,
1672 Spec_File_Name => "myutilst_a.ada");
1673 pragma Source_File_name (My_Utilities.Stacks,
1674 Body_File_Name => "myutilst.ada");
1675 @end example
1676
1677 As shown in this example, the first argument for the pragma is the unit
1678 name (in this example a child unit). The second argument has the form
1679 of a named association. The identifier
1680 indicates whether the file name is for a spec or a body;
1681 the file name itself is given by a string literal.
1682
1683 The source file name pragma is a configuration pragma, which means that
1684 normally it will be placed in the @code{gnat.adc}
1685 file used to hold configuration
1686 pragmas that apply to a complete compilation environment.
1687 For more details on how the @code{gnat.adc} file is created and used
1688 see @ref{3f,,Handling of Configuration Pragmas}.
1689
1690 @geindex gnat.adc
1691
1692 GNAT allows completely arbitrary file names to be specified using the
1693 source file name pragma. However, if the file name specified has an
1694 extension other than @code{.ads} or @code{.adb} it is necessary to use
1695 a special syntax when compiling the file. The name in this case must be
1696 preceded by the special sequence @code{-x} followed by a space and the name
1697 of the language, here @code{ada}, as in:
1698
1699 @example
1700 $ gcc -c -x ada peculiar_file_name.sim
1701 @end example
1702
1703 @code{gnatmake} handles non-standard file names in the usual manner (the
1704 non-standard file name for the main program is simply used as the
1705 argument to gnatmake). Note that if the extension is also non-standard,
1706 then it must be included in the @code{gnatmake} command, it may not
1707 be omitted.
1708
1709 @node Alternative File Naming Schemes,Handling Arbitrary File Naming Conventions with gnatname,Using Other File Names,File Naming Topics and Utilities
1710 @anchor{gnat_ugn/the_gnat_compilation_model alternative-file-naming-schemes}@anchor{40}@anchor{gnat_ugn/the_gnat_compilation_model id11}@anchor{41}
1711 @subsection Alternative File Naming Schemes
1712
1713
1714 @geindex File naming schemes
1715 @geindex alternative
1716
1717 @geindex File names
1718
1719 The previous section described the use of the @code{Source_File_Name}
1720 pragma to allow arbitrary names to be assigned to individual source files.
1721 However, this approach requires one pragma for each file, and especially in
1722 large systems can result in very long @code{gnat.adc} files, and also create
1723 a maintenance problem.
1724
1725 @geindex Source_File_Name pragma
1726
1727 GNAT also provides a facility for specifying systematic file naming schemes
1728 other than the standard default naming scheme previously described. An
1729 alternative scheme for naming is specified by the use of
1730 @code{Source_File_Name} pragmas having the following format:
1731
1732 @example
1733 pragma Source_File_Name (
1734 Spec_File_Name => FILE_NAME_PATTERN
1735 [ , Casing => CASING_SPEC]
1736 [ , Dot_Replacement => STRING_LITERAL ] );
1737
1738 pragma Source_File_Name (
1739 Body_File_Name => FILE_NAME_PATTERN
1740 [ , Casing => CASING_SPEC ]
1741 [ , Dot_Replacement => STRING_LITERAL ] ) ;
1742
1743 pragma Source_File_Name (
1744 Subunit_File_Name => FILE_NAME_PATTERN
1745 [ , Casing => CASING_SPEC ]
1746 [ , Dot_Replacement => STRING_LITERAL ] ) ;
1747
1748 FILE_NAME_PATTERN ::= STRING_LITERAL
1749 CASING_SPEC ::= Lowercase | Uppercase | Mixedcase
1750 @end example
1751
1752 The @code{FILE_NAME_PATTERN} string shows how the file name is constructed.
1753 It contains a single asterisk character, and the unit name is substituted
1754 systematically for this asterisk. The optional parameter
1755 @code{Casing} indicates
1756 whether the unit name is to be all upper-case letters, all lower-case letters,
1757 or mixed-case. If no
1758 @code{Casing} parameter is used, then the default is all
1759 lower-case.
1760
1761 The optional @code{Dot_Replacement} string is used to replace any periods
1762 that occur in subunit or child unit names. If no @code{Dot_Replacement}
1763 argument is used then separating dots appear unchanged in the resulting
1764 file name.
1765 Although the above syntax indicates that the
1766 @code{Casing} argument must appear
1767 before the @code{Dot_Replacement} argument, but it
1768 is also permissible to write these arguments in the opposite order.
1769
1770 As indicated, it is possible to specify different naming schemes for
1771 bodies, specs, and subunits. Quite often the rule for subunits is the
1772 same as the rule for bodies, in which case, there is no need to give
1773 a separate @code{Subunit_File_Name} rule, and in this case the
1774 @code{Body_File_name} rule is used for subunits as well.
1775
1776 The separate rule for subunits can also be used to implement the rather
1777 unusual case of a compilation environment (e.g., a single directory) which
1778 contains a subunit and a child unit with the same unit name. Although
1779 both units cannot appear in the same partition, the Ada Reference Manual
1780 allows (but does not require) the possibility of the two units coexisting
1781 in the same environment.
1782
1783 The file name translation works in the following steps:
1784
1785
1786 @itemize *
1787
1788 @item
1789 If there is a specific @code{Source_File_Name} pragma for the given unit,
1790 then this is always used, and any general pattern rules are ignored.
1791
1792 @item
1793 If there is a pattern type @code{Source_File_Name} pragma that applies to
1794 the unit, then the resulting file name will be used if the file exists. If
1795 more than one pattern matches, the latest one will be tried first, and the
1796 first attempt resulting in a reference to a file that exists will be used.
1797
1798 @item
1799 If no pattern type @code{Source_File_Name} pragma that applies to the unit
1800 for which the corresponding file exists, then the standard GNAT default
1801 naming rules are used.
1802 @end itemize
1803
1804 As an example of the use of this mechanism, consider a commonly used scheme
1805 in which file names are all lower case, with separating periods copied
1806 unchanged to the resulting file name, and specs end with @code{.1.ada}, and
1807 bodies end with @code{.2.ada}. GNAT will follow this scheme if the following
1808 two pragmas appear:
1809
1810 @example
1811 pragma Source_File_Name
1812 (Spec_File_Name => ".1.ada");
1813 pragma Source_File_Name
1814 (Body_File_Name => ".2.ada");
1815 @end example
1816
1817 The default GNAT scheme is actually implemented by providing the following
1818 default pragmas internally:
1819
1820 @example
1821 pragma Source_File_Name
1822 (Spec_File_Name => ".ads", Dot_Replacement => "-");
1823 pragma Source_File_Name
1824 (Body_File_Name => ".adb", Dot_Replacement => "-");
1825 @end example
1826
1827 Our final example implements a scheme typically used with one of the
1828 Ada 83 compilers, where the separator character for subunits was ‘__’
1829 (two underscores), specs were identified by adding @code{_.ADA}, bodies
1830 by adding @code{.ADA}, and subunits by
1831 adding @code{.SEP}. All file names were
1832 upper case. Child units were not present of course since this was an
1833 Ada 83 compiler, but it seems reasonable to extend this scheme to use
1834 the same double underscore separator for child units.
1835
1836 @example
1837 pragma Source_File_Name
1838 (Spec_File_Name => "_.ADA",
1839 Dot_Replacement => "__",
1840 Casing = Uppercase);
1841 pragma Source_File_Name
1842 (Body_File_Name => ".ADA",
1843 Dot_Replacement => "__",
1844 Casing = Uppercase);
1845 pragma Source_File_Name
1846 (Subunit_File_Name => ".SEP",
1847 Dot_Replacement => "__",
1848 Casing = Uppercase);
1849 @end example
1850
1851 @geindex gnatname
1852
1853 @node Handling Arbitrary File Naming Conventions with gnatname,File Name Krunching with gnatkr,Alternative File Naming Schemes,File Naming Topics and Utilities
1854 @anchor{gnat_ugn/the_gnat_compilation_model handling-arbitrary-file-naming-conventions-with-gnatname}@anchor{42}@anchor{gnat_ugn/the_gnat_compilation_model id12}@anchor{43}
1855 @subsection Handling Arbitrary File Naming Conventions with @code{gnatname}
1856
1857
1858 @geindex File Naming Conventions
1859
1860 @menu
1861 * Arbitrary File Naming Conventions::
1862 * Running gnatname::
1863 * Switches for gnatname::
1864 * Examples of gnatname Usage::
1865
1866 @end menu
1867
1868 @node Arbitrary File Naming Conventions,Running gnatname,,Handling Arbitrary File Naming Conventions with gnatname
1869 @anchor{gnat_ugn/the_gnat_compilation_model arbitrary-file-naming-conventions}@anchor{44}@anchor{gnat_ugn/the_gnat_compilation_model id13}@anchor{45}
1870 @subsubsection Arbitrary File Naming Conventions
1871
1872
1873 The GNAT compiler must be able to know the source file name of a compilation
1874 unit. When using the standard GNAT default file naming conventions
1875 (@code{.ads} for specs, @code{.adb} for bodies), the GNAT compiler
1876 does not need additional information.
1877
1878 When the source file names do not follow the standard GNAT default file naming
1879 conventions, the GNAT compiler must be given additional information through
1880 a configuration pragmas file (@ref{25,,Configuration Pragmas})
1881 or a project file.
1882 When the non-standard file naming conventions are well-defined,
1883 a small number of pragmas @code{Source_File_Name} specifying a naming pattern
1884 (@ref{40,,Alternative File Naming Schemes}) may be sufficient. However,
1885 if the file naming conventions are irregular or arbitrary, a number
1886 of pragma @code{Source_File_Name} for individual compilation units
1887 must be defined.
1888 To help maintain the correspondence between compilation unit names and
1889 source file names within the compiler,
1890 GNAT provides a tool @code{gnatname} to generate the required pragmas for a
1891 set of files.
1892
1893 @node Running gnatname,Switches for gnatname,Arbitrary File Naming Conventions,Handling Arbitrary File Naming Conventions with gnatname
1894 @anchor{gnat_ugn/the_gnat_compilation_model id14}@anchor{46}@anchor{gnat_ugn/the_gnat_compilation_model running-gnatname}@anchor{47}
1895 @subsubsection Running @code{gnatname}
1896
1897
1898 The usual form of the @code{gnatname} command is:
1899
1900 @example
1901 $ gnatname [ switches ] naming_pattern [ naming_patterns ]
1902 [--and [ switches ] naming_pattern [ naming_patterns ]]
1903 @end example
1904
1905 All of the arguments are optional. If invoked without any argument,
1906 @code{gnatname} will display its usage.
1907
1908 When used with at least one naming pattern, @code{gnatname} will attempt to
1909 find all the compilation units in files that follow at least one of the
1910 naming patterns. To find these compilation units,
1911 @code{gnatname} will use the GNAT compiler in syntax-check-only mode on all
1912 regular files.
1913
1914 One or several Naming Patterns may be given as arguments to @code{gnatname}.
1915 Each Naming Pattern is enclosed between double quotes (or single
1916 quotes on Windows).
1917 A Naming Pattern is a regular expression similar to the wildcard patterns
1918 used in file names by the Unix shells or the DOS prompt.
1919
1920 @code{gnatname} may be called with several sections of directories/patterns.
1921 Sections are separated by the switch @code{--and}. In each section, there must be
1922 at least one pattern. If no directory is specified in a section, the current
1923 directory (or the project directory if @code{-P} is used) is implied.
1924 The options other that the directory switches and the patterns apply globally
1925 even if they are in different sections.
1926
1927 Examples of Naming Patterns are:
1928
1929 @example
1930 "*.[12].ada"
1931 "*.ad[sb]*"
1932 "body_*" "spec_*"
1933 @end example
1934
1935 For a more complete description of the syntax of Naming Patterns,
1936 see the second kind of regular expressions described in @code{g-regexp.ads}
1937 (the ‘Glob’ regular expressions).
1938
1939 When invoked without the switch @code{-P}, @code{gnatname} will create a
1940 configuration pragmas file @code{gnat.adc} in the current working directory,
1941 with pragmas @code{Source_File_Name} for each file that contains a valid Ada
1942 unit.
1943
1944 @node Switches for gnatname,Examples of gnatname Usage,Running gnatname,Handling Arbitrary File Naming Conventions with gnatname
1945 @anchor{gnat_ugn/the_gnat_compilation_model id15}@anchor{48}@anchor{gnat_ugn/the_gnat_compilation_model switches-for-gnatname}@anchor{49}
1946 @subsubsection Switches for @code{gnatname}
1947
1948
1949 Switches for @code{gnatname} must precede any specified Naming Pattern.
1950
1951 You may specify any of the following switches to @code{gnatname}:
1952
1953 @geindex --version (gnatname)
1954
1955
1956 @table @asis
1957
1958 @item @code{--version}
1959
1960 Display Copyright and version, then exit disregarding all other options.
1961 @end table
1962
1963 @geindex --help (gnatname)
1964
1965
1966 @table @asis
1967
1968 @item @code{--help}
1969
1970 If @code{--version} was not used, display usage, then exit disregarding
1971 all other options.
1972
1973 @item @code{--subdirs=@emph{dir}}
1974
1975 Real object, library or exec directories are subdirectories <dir> of the
1976 specified ones.
1977
1978 @item @code{--no-backup}
1979
1980 Do not create a backup copy of an existing project file.
1981
1982 @item @code{--and}
1983
1984 Start another section of directories/patterns.
1985 @end table
1986
1987 @geindex -c (gnatname)
1988
1989
1990 @table @asis
1991
1992 @item @code{-c@emph{filename}}
1993
1994 Create a configuration pragmas file @code{filename} (instead of the default
1995 @code{gnat.adc}).
1996 There may be zero, one or more space between @code{-c} and
1997 @code{filename}.
1998 @code{filename} may include directory information. @code{filename} must be
1999 writable. There may be only one switch @code{-c}.
2000 When a switch @code{-c} is
2001 specified, no switch @code{-P} may be specified (see below).
2002 @end table
2003
2004 @geindex -d (gnatname)
2005
2006
2007 @table @asis
2008
2009 @item @code{-d@emph{dir}}
2010
2011 Look for source files in directory @code{dir}. There may be zero, one or more
2012 spaces between @code{-d} and @code{dir}.
2013 @code{dir} may end with @code{/**}, that is it may be of the form
2014 @code{root_dir/**}. In this case, the directory @code{root_dir} and all of its
2015 subdirectories, recursively, have to be searched for sources.
2016 When a switch @code{-d}
2017 is specified, the current working directory will not be searched for source
2018 files, unless it is explicitly specified with a @code{-d}
2019 or @code{-D} switch.
2020 Several switches @code{-d} may be specified.
2021 If @code{dir} is a relative path, it is relative to the directory of
2022 the configuration pragmas file specified with switch
2023 @code{-c},
2024 or to the directory of the project file specified with switch
2025 @code{-P} or,
2026 if neither switch @code{-c}
2027 nor switch @code{-P} are specified, it is relative to the
2028 current working directory. The directory
2029 specified with switch @code{-d} must exist and be readable.
2030 @end table
2031
2032 @geindex -D (gnatname)
2033
2034
2035 @table @asis
2036
2037 @item @code{-D@emph{filename}}
2038
2039 Look for source files in all directories listed in text file @code{filename}.
2040 There may be zero, one or more spaces between @code{-D}
2041 and @code{filename}.
2042 @code{filename} must be an existing, readable text file.
2043 Each nonempty line in @code{filename} must be a directory.
2044 Specifying switch @code{-D} is equivalent to specifying as many
2045 switches @code{-d} as there are nonempty lines in
2046 @code{file}.
2047
2048 @item @code{-eL}
2049
2050 Follow symbolic links when processing project files.
2051
2052 @geindex -f (gnatname)
2053
2054 @item @code{-f@emph{pattern}}
2055
2056 Foreign patterns. Using this switch, it is possible to add sources of languages
2057 other than Ada to the list of sources of a project file.
2058 It is only useful if a -P switch is used.
2059 For example,
2060
2061 @example
2062 gnatname -Pprj -f"*.c" "*.ada"
2063 @end example
2064
2065 will look for Ada units in all files with the @code{.ada} extension,
2066 and will add to the list of file for project @code{prj.gpr} the C files
2067 with extension @code{.c}.
2068
2069 @geindex -h (gnatname)
2070
2071 @item @code{-h}
2072
2073 Output usage (help) information. The output is written to @code{stdout}.
2074
2075 @geindex -P (gnatname)
2076
2077 @item @code{-P@emph{proj}}
2078
2079 Create or update project file @code{proj}. There may be zero, one or more space
2080 between @code{-P} and @code{proj}. @code{proj} may include directory
2081 information. @code{proj} must be writable.
2082 There may be only one switch @code{-P}.
2083 When a switch @code{-P} is specified,
2084 no switch @code{-c} may be specified.
2085 On all platforms, except on VMS, when @code{gnatname} is invoked for an
2086 existing project file <proj>.gpr, a backup copy of the project file is created
2087 in the project directory with file name <proj>.gpr.saved_x. ‘x’ is the first
2088 non negative number that makes this backup copy a new file.
2089
2090 @geindex -v (gnatname)
2091
2092 @item @code{-v}
2093
2094 Verbose mode. Output detailed explanation of behavior to @code{stdout}.
2095 This includes name of the file written, the name of the directories to search
2096 and, for each file in those directories whose name matches at least one of
2097 the Naming Patterns, an indication of whether the file contains a unit,
2098 and if so the name of the unit.
2099 @end table
2100
2101 @geindex -v -v (gnatname)
2102
2103
2104 @table @asis
2105
2106 @item @code{-v -v}
2107
2108 Very Verbose mode. In addition to the output produced in verbose mode,
2109 for each file in the searched directories whose name matches none of
2110 the Naming Patterns, an indication is given that there is no match.
2111
2112 @geindex -x (gnatname)
2113
2114 @item @code{-x@emph{pattern}}
2115
2116 Excluded patterns. Using this switch, it is possible to exclude some files
2117 that would match the name patterns. For example,
2118
2119 @example
2120 gnatname -x "*_nt.ada" "*.ada"
2121 @end example
2122
2123 will look for Ada units in all files with the @code{.ada} extension,
2124 except those whose names end with @code{_nt.ada}.
2125 @end table
2126
2127 @node Examples of gnatname Usage,,Switches for gnatname,Handling Arbitrary File Naming Conventions with gnatname
2128 @anchor{gnat_ugn/the_gnat_compilation_model examples-of-gnatname-usage}@anchor{4a}@anchor{gnat_ugn/the_gnat_compilation_model id16}@anchor{4b}
2129 @subsubsection Examples of @code{gnatname} Usage
2130
2131
2132 @example
2133 $ gnatname -c /home/me/names.adc -d sources "[a-z]*.ada*"
2134 @end example
2135
2136 In this example, the directory @code{/home/me} must already exist
2137 and be writable. In addition, the directory
2138 @code{/home/me/sources} (specified by
2139 @code{-d sources}) must exist and be readable.
2140
2141 Note the optional spaces after @code{-c} and @code{-d}.
2142
2143 @example
2144 $ gnatname -P/home/me/proj -x "*_nt_body.ada"
2145 -dsources -dsources/plus -Dcommon_dirs.txt "body_*" "spec_*"
2146 @end example
2147
2148 Note that several switches @code{-d} may be used,
2149 even in conjunction with one or several switches
2150 @code{-D}. Several Naming Patterns and one excluded pattern
2151 are used in this example.
2152
2153 @node File Name Krunching with gnatkr,Renaming Files with gnatchop,Handling Arbitrary File Naming Conventions with gnatname,File Naming Topics and Utilities
2154 @anchor{gnat_ugn/the_gnat_compilation_model file-name-krunching-with-gnatkr}@anchor{4c}@anchor{gnat_ugn/the_gnat_compilation_model id17}@anchor{4d}
2155 @subsection File Name Krunching with @code{gnatkr}
2156
2157
2158 @geindex gnatkr
2159
2160 This section discusses the method used by the compiler to shorten
2161 the default file names chosen for Ada units so that they do not
2162 exceed the maximum length permitted. It also describes the
2163 @code{gnatkr} utility that can be used to determine the result of
2164 applying this shortening.
2165
2166 @menu
2167 * About gnatkr::
2168 * Using gnatkr::
2169 * Krunching Method::
2170 * Examples of gnatkr Usage::
2171
2172 @end menu
2173
2174 @node About gnatkr,Using gnatkr,,File Name Krunching with gnatkr
2175 @anchor{gnat_ugn/the_gnat_compilation_model about-gnatkr}@anchor{4e}@anchor{gnat_ugn/the_gnat_compilation_model id18}@anchor{4f}
2176 @subsubsection About @code{gnatkr}
2177
2178
2179 The default file naming rule in GNAT
2180 is that the file name must be derived from
2181 the unit name. The exact default rule is as follows:
2182
2183
2184 @itemize *
2185
2186 @item
2187 Take the unit name and replace all dots by hyphens.
2188
2189 @item
2190 If such a replacement occurs in the
2191 second character position of a name, and the first character is
2192 @code{a}, @code{g}, @code{s}, or @code{i},
2193 then replace the dot by the character
2194 @code{~} (tilde)
2195 instead of a minus.
2196
2197 The reason for this exception is to avoid clashes
2198 with the standard names for children of System, Ada, Interfaces,
2199 and GNAT, which use the prefixes
2200 @code{s-}, @code{a-}, @code{i-}, and @code{g-},
2201 respectively.
2202 @end itemize
2203
2204 The @code{-gnatk@emph{nn}}
2205 switch of the compiler activates a ‘krunching’
2206 circuit that limits file names to nn characters (where nn is a decimal
2207 integer).
2208
2209 The @code{gnatkr} utility can be used to determine the krunched name for
2210 a given file, when krunched to a specified maximum length.
2211
2212 @node Using gnatkr,Krunching Method,About gnatkr,File Name Krunching with gnatkr
2213 @anchor{gnat_ugn/the_gnat_compilation_model id19}@anchor{50}@anchor{gnat_ugn/the_gnat_compilation_model using-gnatkr}@anchor{3d}
2214 @subsubsection Using @code{gnatkr}
2215
2216
2217 The @code{gnatkr} command has the form:
2218
2219 @example
2220 $ gnatkr name [ length ]
2221 @end example
2222
2223 @code{name} is the uncrunched file name, derived from the name of the unit
2224 in the standard manner described in the previous section (i.e., in particular
2225 all dots are replaced by hyphens). The file name may or may not have an
2226 extension (defined as a suffix of the form period followed by arbitrary
2227 characters other than period). If an extension is present then it will
2228 be preserved in the output. For example, when krunching @code{hellofile.ads}
2229 to eight characters, the result will be hellofil.ads.
2230
2231 Note: for compatibility with previous versions of @code{gnatkr} dots may
2232 appear in the name instead of hyphens, but the last dot will always be
2233 taken as the start of an extension. So if @code{gnatkr} is given an argument
2234 such as @code{Hello.World.adb} it will be treated exactly as if the first
2235 period had been a hyphen, and for example krunching to eight characters
2236 gives the result @code{hellworl.adb}.
2237
2238 Note that the result is always all lower case.
2239 Characters of the other case are folded as required.
2240
2241 @code{length} represents the length of the krunched name. The default
2242 when no argument is given is 8 characters. A length of zero stands for
2243 unlimited, in other words do not chop except for system files where the
2244 implied crunching length is always eight characters.
2245
2246 The output is the krunched name. The output has an extension only if the
2247 original argument was a file name with an extension.
2248
2249 @node Krunching Method,Examples of gnatkr Usage,Using gnatkr,File Name Krunching with gnatkr
2250 @anchor{gnat_ugn/the_gnat_compilation_model id20}@anchor{51}@anchor{gnat_ugn/the_gnat_compilation_model krunching-method}@anchor{52}
2251 @subsubsection Krunching Method
2252
2253
2254 The initial file name is determined by the name of the unit that the file
2255 contains. The name is formed by taking the full expanded name of the
2256 unit and replacing the separating dots with hyphens and
2257 using lowercase
2258 for all letters, except that a hyphen in the second character position is
2259 replaced by a tilde if the first character is
2260 @code{a}, @code{i}, @code{g}, or @code{s}.
2261 The extension is @code{.ads} for a
2262 spec and @code{.adb} for a body.
2263 Krunching does not affect the extension, but the file name is shortened to
2264 the specified length by following these rules:
2265
2266
2267 @itemize *
2268
2269 @item
2270 The name is divided into segments separated by hyphens, tildes or
2271 underscores and all hyphens, tildes, and underscores are
2272 eliminated. If this leaves the name short enough, we are done.
2273
2274 @item
2275 If the name is too long, the longest segment is located (left-most
2276 if there are two of equal length), and shortened by dropping
2277 its last character. This is repeated until the name is short enough.
2278
2279 As an example, consider the krunching of @code{our-strings-wide_fixed.adb}
2280 to fit the name into 8 characters as required by some operating systems:
2281
2282 @example
2283 our-strings-wide_fixed 22
2284 our strings wide fixed 19
2285 our string wide fixed 18
2286 our strin wide fixed 17
2287 our stri wide fixed 16
2288 our stri wide fixe 15
2289 our str wide fixe 14
2290 our str wid fixe 13
2291 our str wid fix 12
2292 ou str wid fix 11
2293 ou st wid fix 10
2294 ou st wi fix 9
2295 ou st wi fi 8
2296 Final file name: oustwifi.adb
2297 @end example
2298
2299 @item
2300 The file names for all predefined units are always krunched to eight
2301 characters. The krunching of these predefined units uses the following
2302 special prefix replacements:
2303
2304
2305 @multitable {xxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxx}
2306 @item
2307
2308 Prefix
2309
2310 @tab
2311
2312 Replacement
2313
2314 @item
2315
2316 @code{ada-}
2317
2318 @tab
2319
2320 @code{a-}
2321
2322 @item
2323
2324 @code{gnat-}
2325
2326 @tab
2327
2328 @code{g-}
2329
2330 @item
2331
2332 @code{interfac es-}
2333
2334 @tab
2335
2336 @code{i-}
2337
2338 @item
2339
2340 @code{system-}
2341
2342 @tab
2343
2344 @code{s-}
2345
2346 @end multitable
2347
2348
2349 These system files have a hyphen in the second character position. That
2350 is why normal user files replace such a character with a
2351 tilde, to avoid confusion with system file names.
2352
2353 As an example of this special rule, consider
2354 @code{ada-strings-wide_fixed.adb}, which gets krunched as follows:
2355
2356 @example
2357 ada-strings-wide_fixed 22
2358 a- strings wide fixed 18
2359 a- string wide fixed 17
2360 a- strin wide fixed 16
2361 a- stri wide fixed 15
2362 a- stri wide fixe 14
2363 a- str wide fixe 13
2364 a- str wid fixe 12
2365 a- str wid fix 11
2366 a- st wid fix 10
2367 a- st wi fix 9
2368 a- st wi fi 8
2369 Final file name: a-stwifi.adb
2370 @end example
2371 @end itemize
2372
2373 Of course no file shortening algorithm can guarantee uniqueness over all
2374 possible unit names, and if file name krunching is used then it is your
2375 responsibility to ensure that no name clashes occur. The utility
2376 program @code{gnatkr} is supplied for conveniently determining the
2377 krunched name of a file.
2378
2379 @node Examples of gnatkr Usage,,Krunching Method,File Name Krunching with gnatkr
2380 @anchor{gnat_ugn/the_gnat_compilation_model examples-of-gnatkr-usage}@anchor{53}@anchor{gnat_ugn/the_gnat_compilation_model id21}@anchor{54}
2381 @subsubsection Examples of @code{gnatkr} Usage
2382
2383
2384 @example
2385 $ gnatkr very_long_unit_name.ads --> velounna.ads
2386 $ gnatkr grandparent-parent-child.ads --> grparchi.ads
2387 $ gnatkr Grandparent.Parent.Child.ads --> grparchi.ads
2388 $ gnatkr grandparent-parent-child --> grparchi
2389 $ gnatkr very_long_unit_name.ads/count=6 --> vlunna.ads
2390 $ gnatkr very_long_unit_name.ads/count=0 --> very_long_unit_name.ads
2391 @end example
2392
2393 @node Renaming Files with gnatchop,,File Name Krunching with gnatkr,File Naming Topics and Utilities
2394 @anchor{gnat_ugn/the_gnat_compilation_model id22}@anchor{55}@anchor{gnat_ugn/the_gnat_compilation_model renaming-files-with-gnatchop}@anchor{1d}
2395 @subsection Renaming Files with @code{gnatchop}
2396
2397
2398 @geindex gnatchop
2399
2400 This section discusses how to handle files with multiple units by using
2401 the @code{gnatchop} utility. This utility is also useful in renaming
2402 files to meet the standard GNAT default file naming conventions.
2403
2404 @menu
2405 * Handling Files with Multiple Units::
2406 * Operating gnatchop in Compilation Mode::
2407 * Command Line for gnatchop::
2408 * Switches for gnatchop::
2409 * Examples of gnatchop Usage::
2410
2411 @end menu
2412
2413 @node Handling Files with Multiple Units,Operating gnatchop in Compilation Mode,,Renaming Files with gnatchop
2414 @anchor{gnat_ugn/the_gnat_compilation_model handling-files-with-multiple-units}@anchor{56}@anchor{gnat_ugn/the_gnat_compilation_model id23}@anchor{57}
2415 @subsubsection Handling Files with Multiple Units
2416
2417
2418 The basic compilation model of GNAT requires that a file submitted to the
2419 compiler have only one unit and there be a strict correspondence
2420 between the file name and the unit name.
2421
2422 If you want to keep your files with multiple units,
2423 perhaps to maintain compatibility with some other Ada compilation system,
2424 you can use @code{gnatname} to generate or update your project files.
2425 Generated or modified project files can be processed by GNAT.
2426
2427 See @ref{42,,Handling Arbitrary File Naming Conventions with gnatname}
2428 for more details on how to use @cite{gnatname}.
2429
2430 Alternatively, if you want to permanently restructure a set of ‘foreign’
2431 files so that they match the GNAT rules, and do the remaining development
2432 using the GNAT structure, you can simply use @code{gnatchop} once, generate the
2433 new set of files and work with them from that point on.
2434
2435 Note that if your file containing multiple units starts with a byte order
2436 mark (BOM) specifying UTF-8 encoding, then the files generated by gnatchop
2437 will each start with a copy of this BOM, meaning that they can be compiled
2438 automatically in UTF-8 mode without needing to specify an explicit encoding.
2439
2440 @node Operating gnatchop in Compilation Mode,Command Line for gnatchop,Handling Files with Multiple Units,Renaming Files with gnatchop
2441 @anchor{gnat_ugn/the_gnat_compilation_model id24}@anchor{58}@anchor{gnat_ugn/the_gnat_compilation_model operating-gnatchop-in-compilation-mode}@anchor{59}
2442 @subsubsection Operating gnatchop in Compilation Mode
2443
2444
2445 The basic function of @code{gnatchop} is to take a file with multiple units
2446 and split it into separate files. The boundary between files is reasonably
2447 clear, except for the issue of comments and pragmas. In default mode, the
2448 rule is that any pragmas between units belong to the previous unit, except
2449 that configuration pragmas always belong to the following unit. Any comments
2450 belong to the following unit. These rules
2451 almost always result in the right choice of
2452 the split point without needing to mark it explicitly and most users will
2453 find this default to be what they want. In this default mode it is incorrect to
2454 submit a file containing only configuration pragmas, or one that ends in
2455 configuration pragmas, to @code{gnatchop}.
2456
2457 However, using a special option to activate ‘compilation mode’,
2458 @code{gnatchop}
2459 can perform another function, which is to provide exactly the semantics
2460 required by the RM for handling of configuration pragmas in a compilation.
2461 In the absence of configuration pragmas (at the main file level), this
2462 option has no effect, but it causes such configuration pragmas to be handled
2463 in a quite different manner.
2464
2465 First, in compilation mode, if @code{gnatchop} is given a file that consists of
2466 only configuration pragmas, then this file is appended to the
2467 @code{gnat.adc} file in the current directory. This behavior provides
2468 the required behavior described in the RM for the actions to be taken
2469 on submitting such a file to the compiler, namely that these pragmas
2470 should apply to all subsequent compilations in the same compilation
2471 environment. Using GNAT, the current directory, possibly containing a
2472 @code{gnat.adc} file is the representation
2473 of a compilation environment. For more information on the
2474 @code{gnat.adc} file, see @ref{3f,,Handling of Configuration Pragmas}.
2475
2476 Second, in compilation mode, if @code{gnatchop}
2477 is given a file that starts with
2478 configuration pragmas, and contains one or more units, then these
2479 configuration pragmas are prepended to each of the chopped files. This
2480 behavior provides the required behavior described in the RM for the
2481 actions to be taken on compiling such a file, namely that the pragmas
2482 apply to all units in the compilation, but not to subsequently compiled
2483 units.
2484
2485 Finally, if configuration pragmas appear between units, they are appended
2486 to the previous unit. This results in the previous unit being illegal,
2487 since the compiler does not accept configuration pragmas that follow
2488 a unit. This provides the required RM behavior that forbids configuration
2489 pragmas other than those preceding the first compilation unit of a
2490 compilation.
2491
2492 For most purposes, @code{gnatchop} will be used in default mode. The
2493 compilation mode described above is used only if you need exactly
2494 accurate behavior with respect to compilations, and you have files
2495 that contain multiple units and configuration pragmas. In this
2496 circumstance the use of @code{gnatchop} with the compilation mode
2497 switch provides the required behavior, and is for example the mode
2498 in which GNAT processes the ACVC tests.
2499
2500 @node Command Line for gnatchop,Switches for gnatchop,Operating gnatchop in Compilation Mode,Renaming Files with gnatchop
2501 @anchor{gnat_ugn/the_gnat_compilation_model command-line-for-gnatchop}@anchor{5a}@anchor{gnat_ugn/the_gnat_compilation_model id25}@anchor{5b}
2502 @subsubsection Command Line for @code{gnatchop}
2503
2504
2505 The @code{gnatchop} command has the form:
2506
2507 @example
2508 $ gnatchop switches file_name [file_name ...]
2509 [directory]
2510 @end example
2511
2512 The only required argument is the file name of the file to be chopped.
2513 There are no restrictions on the form of this file name. The file itself
2514 contains one or more Ada units, in normal GNAT format, concatenated
2515 together. As shown, more than one file may be presented to be chopped.
2516
2517 When run in default mode, @code{gnatchop} generates one output file in
2518 the current directory for each unit in each of the files.
2519
2520 @code{directory}, if specified, gives the name of the directory to which
2521 the output files will be written. If it is not specified, all files are
2522 written to the current directory.
2523
2524 For example, given a
2525 file called @code{hellofiles} containing
2526
2527 @example
2528 procedure Hello;
2529
2530 with Ada.Text_IO; use Ada.Text_IO;
2531 procedure Hello is
2532 begin
2533 Put_Line ("Hello");
2534 end Hello;
2535 @end example
2536
2537 the command
2538
2539 @example
2540 $ gnatchop hellofiles
2541 @end example
2542
2543 generates two files in the current directory, one called
2544 @code{hello.ads} containing the single line that is the procedure spec,
2545 and the other called @code{hello.adb} containing the remaining text. The
2546 original file is not affected. The generated files can be compiled in
2547 the normal manner.
2548
2549 When gnatchop is invoked on a file that is empty or that contains only empty
2550 lines and/or comments, gnatchop will not fail, but will not produce any
2551 new sources.
2552
2553 For example, given a
2554 file called @code{toto.txt} containing
2555
2556 @example
2557 -- Just a comment
2558 @end example
2559
2560 the command
2561
2562 @example
2563 $ gnatchop toto.txt
2564 @end example
2565
2566 will not produce any new file and will result in the following warnings:
2567
2568 @example
2569 toto.txt:1:01: warning: empty file, contains no compilation units
2570 no compilation units found
2571 no source files written
2572 @end example
2573
2574 @node Switches for gnatchop,Examples of gnatchop Usage,Command Line for gnatchop,Renaming Files with gnatchop
2575 @anchor{gnat_ugn/the_gnat_compilation_model id26}@anchor{5c}@anchor{gnat_ugn/the_gnat_compilation_model switches-for-gnatchop}@anchor{5d}
2576 @subsubsection Switches for @code{gnatchop}
2577
2578
2579 @code{gnatchop} recognizes the following switches:
2580
2581 @geindex --version (gnatchop)
2582
2583
2584 @table @asis
2585
2586 @item @code{--version}
2587
2588 Display Copyright and version, then exit disregarding all other options.
2589 @end table
2590
2591 @geindex --help (gnatchop)
2592
2593
2594 @table @asis
2595
2596 @item @code{--help}
2597
2598 If @code{--version} was not used, display usage, then exit disregarding
2599 all other options.
2600 @end table
2601
2602 @geindex -c (gnatchop)
2603
2604
2605 @table @asis
2606
2607 @item @code{-c}
2608
2609 Causes @code{gnatchop} to operate in compilation mode, in which
2610 configuration pragmas are handled according to strict RM rules. See
2611 previous section for a full description of this mode.
2612
2613 @item @code{-gnat@emph{xxx}}
2614
2615 This passes the given @code{-gnat@emph{xxx}} switch to @code{gnat} which is
2616 used to parse the given file. Not all @emph{xxx} options make sense,
2617 but for example, the use of @code{-gnati2} allows @code{gnatchop} to
2618 process a source file that uses Latin-2 coding for identifiers.
2619
2620 @item @code{-h}
2621
2622 Causes @code{gnatchop} to generate a brief help summary to the standard
2623 output file showing usage information.
2624 @end table
2625
2626 @geindex -k (gnatchop)
2627
2628
2629 @table @asis
2630
2631 @item @code{-k@emph{mm}}
2632
2633 Limit generated file names to the specified number @code{mm}
2634 of characters.
2635 This is useful if the
2636 resulting set of files is required to be interoperable with systems
2637 which limit the length of file names.
2638 No space is allowed between the @code{-k} and the numeric value. The numeric
2639 value may be omitted in which case a default of @code{-k8},
2640 suitable for use
2641 with DOS-like file systems, is used. If no @code{-k} switch
2642 is present then
2643 there is no limit on the length of file names.
2644 @end table
2645
2646 @geindex -p (gnatchop)
2647
2648
2649 @table @asis
2650
2651 @item @code{-p}
2652
2653 Causes the file modification time stamp of the input file to be
2654 preserved and used for the time stamp of the output file(s). This may be
2655 useful for preserving coherency of time stamps in an environment where
2656 @code{gnatchop} is used as part of a standard build process.
2657 @end table
2658
2659 @geindex -q (gnatchop)
2660
2661
2662 @table @asis
2663
2664 @item @code{-q}
2665
2666 Causes output of informational messages indicating the set of generated
2667 files to be suppressed. Warnings and error messages are unaffected.
2668 @end table
2669
2670 @geindex -r (gnatchop)
2671
2672 @geindex Source_Reference pragmas
2673
2674
2675 @table @asis
2676
2677 @item @code{-r}
2678
2679 Generate @code{Source_Reference} pragmas. Use this switch if the output
2680 files are regarded as temporary and development is to be done in terms
2681 of the original unchopped file. This switch causes
2682 @code{Source_Reference} pragmas to be inserted into each of the
2683 generated files to refers back to the original file name and line number.
2684 The result is that all error messages refer back to the original
2685 unchopped file.
2686 In addition, the debugging information placed into the object file (when
2687 the @code{-g} switch of @code{gcc} or @code{gnatmake} is
2688 specified)
2689 also refers back to this original file so that tools like profilers and
2690 debuggers will give information in terms of the original unchopped file.
2691
2692 If the original file to be chopped itself contains
2693 a @code{Source_Reference}
2694 pragma referencing a third file, then gnatchop respects
2695 this pragma, and the generated @code{Source_Reference} pragmas
2696 in the chopped file refer to the original file, with appropriate
2697 line numbers. This is particularly useful when @code{gnatchop}
2698 is used in conjunction with @code{gnatprep} to compile files that
2699 contain preprocessing statements and multiple units.
2700 @end table
2701
2702 @geindex -v (gnatchop)
2703
2704
2705 @table @asis
2706
2707 @item @code{-v}
2708
2709 Causes @code{gnatchop} to operate in verbose mode. The version
2710 number and copyright notice are output, as well as exact copies of
2711 the gnat1 commands spawned to obtain the chop control information.
2712 @end table
2713
2714 @geindex -w (gnatchop)
2715
2716
2717 @table @asis
2718
2719 @item @code{-w}
2720
2721 Overwrite existing file names. Normally @code{gnatchop} regards it as a
2722 fatal error if there is already a file with the same name as a
2723 file it would otherwise output, in other words if the files to be
2724 chopped contain duplicated units. This switch bypasses this
2725 check, and causes all but the last instance of such duplicated
2726 units to be skipped.
2727 @end table
2728
2729 @geindex --GCC= (gnatchop)
2730
2731
2732 @table @asis
2733
2734 @item @code{--GCC=@emph{xxxx}}
2735
2736 Specify the path of the GNAT parser to be used. When this switch is used,
2737 no attempt is made to add the prefix to the GNAT parser executable.
2738 @end table
2739
2740 @node Examples of gnatchop Usage,,Switches for gnatchop,Renaming Files with gnatchop
2741 @anchor{gnat_ugn/the_gnat_compilation_model examples-of-gnatchop-usage}@anchor{5e}@anchor{gnat_ugn/the_gnat_compilation_model id27}@anchor{5f}
2742 @subsubsection Examples of @code{gnatchop} Usage
2743
2744
2745 @example
2746 $ gnatchop -w hello_s.ada prerelease/files
2747 @end example
2748
2749 Chops the source file @code{hello_s.ada}. The output files will be
2750 placed in the directory @code{prerelease/files},
2751 overwriting any
2752 files with matching names in that directory (no files in the current
2753 directory are modified).
2754
2755 @example
2756 $ gnatchop archive
2757 @end example
2758
2759 Chops the source file @code{archive}
2760 into the current directory. One
2761 useful application of @code{gnatchop} is in sending sets of sources
2762 around, for example in email messages. The required sources are simply
2763 concatenated (for example, using a Unix @code{cat}
2764 command), and then
2765 @code{gnatchop} is used at the other end to reconstitute the original
2766 file names.
2767
2768 @example
2769 $ gnatchop file1 file2 file3 direc
2770 @end example
2771
2772 Chops all units in files @code{file1}, @code{file2}, @code{file3}, placing
2773 the resulting files in the directory @code{direc}. Note that if any units
2774 occur more than once anywhere within this set of files, an error message
2775 is generated, and no files are written. To override this check, use the
2776 @code{-w} switch,
2777 in which case the last occurrence in the last file will
2778 be the one that is output, and earlier duplicate occurrences for a given
2779 unit will be skipped.
2780
2781 @node Configuration Pragmas,Generating Object Files,File Naming Topics and Utilities,The GNAT Compilation Model
2782 @anchor{gnat_ugn/the_gnat_compilation_model configuration-pragmas}@anchor{25}@anchor{gnat_ugn/the_gnat_compilation_model id28}@anchor{60}
2783 @section Configuration Pragmas
2784
2785
2786 @geindex Configuration pragmas
2787
2788 @geindex Pragmas
2789 @geindex configuration
2790
2791 Configuration pragmas include those pragmas described as
2792 such in the Ada Reference Manual, as well as
2793 implementation-dependent pragmas that are configuration pragmas.
2794 See the @code{Implementation_Defined_Pragmas} chapter in the
2795 @cite{GNAT_Reference_Manual} for details on these
2796 additional GNAT-specific configuration pragmas.
2797 Most notably, the pragma @code{Source_File_Name}, which allows
2798 specifying non-default names for source files, is a configuration
2799 pragma. The following is a complete list of configuration pragmas
2800 recognized by GNAT:
2801
2802 @example
2803 Ada_83
2804 Ada_95
2805 Ada_05
2806 Ada_2005
2807 Ada_12
2808 Ada_2012
2809 Allow_Integer_Address
2810 Annotate
2811 Assertion_Policy
2812 Assume_No_Invalid_Values
2813 C_Pass_By_Copy
2814 Check_Float_Overflow
2815 Check_Name
2816 Check_Policy
2817 Compile_Time_Error
2818 Compile_Time_Warning
2819 Compiler_Unit
2820 Compiler_Unit_Warning
2821 Component_Alignment
2822 Convention_Identifier
2823 Debug_Policy
2824 Detect_Blocking
2825 Default_Scalar_Storage_Order
2826 Default_Storage_Pool
2827 Disable_Atomic_Synchronization
2828 Discard_Names
2829 Elaboration_Checks
2830 Eliminate
2831 Enable_Atomic_Synchronization
2832 Extend_System
2833 Extensions_Allowed
2834 External_Name_Casing
2835 Fast_Math
2836 Favor_Top_Level
2837 Ignore_Pragma
2838 Implicit_Packing
2839 Initialize_Scalars
2840 Interrupt_State
2841 License
2842 Locking_Policy
2843 No_Component_Reordering
2844 No_Heap_Finalization
2845 No_Run_Time
2846 No_Strict_Aliasing
2847 Normalize_Scalars
2848 Optimize_Alignment
2849 Overflow_Mode
2850 Overriding_Renamings
2851 Partition_Elaboration_Policy
2852 Persistent_BSS
2853 Prefix_Exception_Messages
2854 Priority_Specific_Dispatching
2855 Profile
2856 Profile_Warnings
2857 Propagate_Exceptions
2858 Queuing_Policy
2859 Rational
2860 Ravenscar
2861 Rename_Pragma
2862 Restricted_Run_Time
2863 Restrictions
2864 Restrictions_Warnings
2865 Reviewable
2866 Short_Circuit_And_Or
2867 Short_Descriptors
2868 Source_File_Name
2869 Source_File_Name_Project
2870 SPARK_Mode
2871 Style_Checks
2872 Suppress
2873 Suppress_Exception_Locations
2874 Task_Dispatching_Policy
2875 Unevaluated_Use_Of_Old
2876 Universal_Data
2877 Unsuppress
2878 Use_VADS_Size
2879 Validity_Checks
2880 Warning_As_Error
2881 Warnings
2882 Wide_Character_Encoding
2883 @end example
2884
2885 @menu
2886 * Handling of Configuration Pragmas::
2887 * The Configuration Pragmas Files::
2888
2889 @end menu
2890
2891 @node Handling of Configuration Pragmas,The Configuration Pragmas Files,,Configuration Pragmas
2892 @anchor{gnat_ugn/the_gnat_compilation_model handling-of-configuration-pragmas}@anchor{3f}@anchor{gnat_ugn/the_gnat_compilation_model id29}@anchor{61}
2893 @subsection Handling of Configuration Pragmas
2894
2895
2896 Configuration pragmas may either appear at the start of a compilation
2897 unit, or they can appear in a configuration pragma file to apply to
2898 all compilations performed in a given compilation environment.
2899
2900 GNAT also provides the @code{gnatchop} utility to provide an automatic
2901 way to handle configuration pragmas following the semantics for
2902 compilations (that is, files with multiple units), described in the RM.
2903 See @ref{59,,Operating gnatchop in Compilation Mode} for details.
2904 However, for most purposes, it will be more convenient to edit the
2905 @code{gnat.adc} file that contains configuration pragmas directly,
2906 as described in the following section.
2907
2908 In the case of @code{Restrictions} pragmas appearing as configuration
2909 pragmas in individual compilation units, the exact handling depends on
2910 the type of restriction.
2911
2912 Restrictions that require partition-wide consistency (like
2913 @code{No_Tasking}) are
2914 recognized wherever they appear
2915 and can be freely inherited, e.g. from a @emph{with}ed unit to the @emph{with}ing
2916 unit. This makes sense since the binder will in any case insist on seeing
2917 consistent use, so any unit not conforming to any restrictions that are
2918 anywhere in the partition will be rejected, and you might as well find
2919 that out at compile time rather than at bind time.
2920
2921 For restrictions that do not require partition-wide consistency, e.g.
2922 SPARK or No_Implementation_Attributes, in general the restriction applies
2923 only to the unit in which the pragma appears, and not to any other units.
2924
2925 The exception is No_Elaboration_Code which always applies to the entire
2926 object file from a compilation, i.e. to the body, spec, and all subunits.
2927 This restriction can be specified in a configuration pragma file, or it
2928 can be on the body and/or the spec (in eithe case it applies to all the
2929 relevant units). It can appear on a subunit only if it has previously
2930 appeared in the body of spec.
2931
2932 @node The Configuration Pragmas Files,,Handling of Configuration Pragmas,Configuration Pragmas
2933 @anchor{gnat_ugn/the_gnat_compilation_model id30}@anchor{62}@anchor{gnat_ugn/the_gnat_compilation_model the-configuration-pragmas-files}@anchor{63}
2934 @subsection The Configuration Pragmas Files
2935
2936
2937 @geindex gnat.adc
2938
2939 In GNAT a compilation environment is defined by the current
2940 directory at the time that a compile command is given. This current
2941 directory is searched for a file whose name is @code{gnat.adc}. If
2942 this file is present, it is expected to contain one or more
2943 configuration pragmas that will be applied to the current compilation.
2944 However, if the switch @code{-gnatA} is used, @code{gnat.adc} is not
2945 considered. When taken into account, @code{gnat.adc} is added to the
2946 dependencies, so that if @code{gnat.adc} is modified later, an invocation of
2947 @code{gnatmake} will recompile the source.
2948
2949 Configuration pragmas may be entered into the @code{gnat.adc} file
2950 either by running @code{gnatchop} on a source file that consists only of
2951 configuration pragmas, or more conveniently by direct editing of the
2952 @code{gnat.adc} file, which is a standard format source file.
2953
2954 Besides @code{gnat.adc}, additional files containing configuration
2955 pragmas may be applied to the current compilation using the switch
2956 @code{-gnatec=@emph{path}} where @code{path} must designate an existing file that
2957 contains only configuration pragmas. These configuration pragmas are
2958 in addition to those found in @code{gnat.adc} (provided @code{gnat.adc}
2959 is present and switch @code{-gnatA} is not used).
2960
2961 It is allowable to specify several switches @code{-gnatec=}, all of which
2962 will be taken into account.
2963
2964 Files containing configuration pragmas specified with switches
2965 @code{-gnatec=} are added to the dependencies, unless they are
2966 temporary files. A file is considered temporary if its name ends in
2967 @code{.tmp} or @code{.TMP}. Certain tools follow this naming
2968 convention because they pass information to @code{gcc} via
2969 temporary files that are immediately deleted; it doesn’t make sense to
2970 depend on a file that no longer exists. Such tools include
2971 @code{gprbuild}, @code{gnatmake}, and @code{gnatcheck}.
2972
2973 By default, configuration pragma files are stored by their absolute paths in
2974 ALI files. You can use the @code{-gnateb} switch in order to store them by
2975 their basename instead.
2976
2977 If you are using project file, a separate mechanism is provided using
2978 project attributes.
2979
2980 @c --Comment
2981 @c See :ref:`Specifying_Configuration_Pragmas` for more details.
2982
2983 @node Generating Object Files,Source Dependencies,Configuration Pragmas,The GNAT Compilation Model
2984 @anchor{gnat_ugn/the_gnat_compilation_model generating-object-files}@anchor{26}@anchor{gnat_ugn/the_gnat_compilation_model id31}@anchor{64}
2985 @section Generating Object Files
2986
2987
2988 An Ada program consists of a set of source files, and the first step in
2989 compiling the program is to generate the corresponding object files.
2990 These are generated by compiling a subset of these source files.
2991 The files you need to compile are the following:
2992
2993
2994 @itemize *
2995
2996 @item
2997 If a package spec has no body, compile the package spec to produce the
2998 object file for the package.
2999
3000 @item
3001 If a package has both a spec and a body, compile the body to produce the
3002 object file for the package. The source file for the package spec need
3003 not be compiled in this case because there is only one object file, which
3004 contains the code for both the spec and body of the package.
3005
3006 @item
3007 For a subprogram, compile the subprogram body to produce the object file
3008 for the subprogram. The spec, if one is present, is as usual in a
3009 separate file, and need not be compiled.
3010 @end itemize
3011
3012 @geindex Subunits
3013
3014
3015 @itemize *
3016
3017 @item
3018 In the case of subunits, only compile the parent unit. A single object
3019 file is generated for the entire subunit tree, which includes all the
3020 subunits.
3021
3022 @item
3023 Compile child units independently of their parent units
3024 (though, of course, the spec of all the ancestor unit must be present in order
3025 to compile a child unit).
3026
3027 @geindex Generics
3028
3029 @item
3030 Compile generic units in the same manner as any other units. The object
3031 files in this case are small dummy files that contain at most the
3032 flag used for elaboration checking. This is because GNAT always handles generic
3033 instantiation by means of macro expansion. However, it is still necessary to
3034 compile generic units, for dependency checking and elaboration purposes.
3035 @end itemize
3036
3037 The preceding rules describe the set of files that must be compiled to
3038 generate the object files for a program. Each object file has the same
3039 name as the corresponding source file, except that the extension is
3040 @code{.o} as usual.
3041
3042 You may wish to compile other files for the purpose of checking their
3043 syntactic and semantic correctness. For example, in the case where a
3044 package has a separate spec and body, you would not normally compile the
3045 spec. However, it is convenient in practice to compile the spec to make
3046 sure it is error-free before compiling clients of this spec, because such
3047 compilations will fail if there is an error in the spec.
3048
3049 GNAT provides an option for compiling such files purely for the
3050 purposes of checking correctness; such compilations are not required as
3051 part of the process of building a program. To compile a file in this
3052 checking mode, use the @code{-gnatc} switch.
3053
3054 @node Source Dependencies,The Ada Library Information Files,Generating Object Files,The GNAT Compilation Model
3055 @anchor{gnat_ugn/the_gnat_compilation_model id32}@anchor{65}@anchor{gnat_ugn/the_gnat_compilation_model source-dependencies}@anchor{27}
3056 @section Source Dependencies
3057
3058
3059 A given object file clearly depends on the source file which is compiled
3060 to produce it. Here we are using “depends” in the sense of a typical
3061 @code{make} utility; in other words, an object file depends on a source
3062 file if changes to the source file require the object file to be
3063 recompiled.
3064 In addition to this basic dependency, a given object may depend on
3065 additional source files as follows:
3066
3067
3068 @itemize *
3069
3070 @item
3071 If a file being compiled @emph{with}s a unit @code{X}, the object file
3072 depends on the file containing the spec of unit @code{X}. This includes
3073 files that are @emph{with}ed implicitly either because they are parents
3074 of @emph{with}ed child units or they are run-time units required by the
3075 language constructs used in a particular unit.
3076
3077 @item
3078 If a file being compiled instantiates a library level generic unit, the
3079 object file depends on both the spec and body files for this generic
3080 unit.
3081
3082 @item
3083 If a file being compiled instantiates a generic unit defined within a
3084 package, the object file depends on the body file for the package as
3085 well as the spec file.
3086 @end itemize
3087
3088 @geindex Inline
3089
3090 @geindex -gnatn switch
3091
3092
3093 @itemize *
3094
3095 @item
3096 If a file being compiled contains a call to a subprogram for which
3097 pragma @code{Inline} applies and inlining is activated with the
3098 @code{-gnatn} switch, the object file depends on the file containing the
3099 body of this subprogram as well as on the file containing the spec. Note
3100 that for inlining to actually occur as a result of the use of this switch,
3101 it is necessary to compile in optimizing mode.
3102
3103 @geindex -gnatN switch
3104
3105 The use of @code{-gnatN} activates inlining optimization
3106 that is performed by the front end of the compiler. This inlining does
3107 not require that the code generation be optimized. Like @code{-gnatn},
3108 the use of this switch generates additional dependencies.
3109
3110 When using a gcc-based back end, then the use of
3111 @code{-gnatN} is deprecated, and the use of @code{-gnatn} is preferred.
3112 Historically front end inlining was more extensive than the gcc back end
3113 inlining, but that is no longer the case.
3114
3115 @item
3116 If an object file @code{O} depends on the proper body of a subunit through
3117 inlining or instantiation, it depends on the parent unit of the subunit.
3118 This means that any modification of the parent unit or one of its subunits
3119 affects the compilation of @code{O}.
3120
3121 @item
3122 The object file for a parent unit depends on all its subunit body files.
3123
3124 @item
3125 The previous two rules meant that for purposes of computing dependencies and
3126 recompilation, a body and all its subunits are treated as an indivisible whole.
3127
3128 These rules are applied transitively: if unit @code{A} @emph{with}s
3129 unit @code{B}, whose elaboration calls an inlined procedure in package
3130 @code{C}, the object file for unit @code{A} will depend on the body of
3131 @code{C}, in file @code{c.adb}.
3132
3133 The set of dependent files described by these rules includes all the
3134 files on which the unit is semantically dependent, as dictated by the
3135 Ada language standard. However, it is a superset of what the
3136 standard describes, because it includes generic, inline, and subunit
3137 dependencies.
3138
3139 An object file must be recreated by recompiling the corresponding source
3140 file if any of the source files on which it depends are modified. For
3141 example, if the @code{make} utility is used to control compilation,
3142 the rule for an Ada object file must mention all the source files on
3143 which the object file depends, according to the above definition.
3144 The determination of the necessary
3145 recompilations is done automatically when one uses @code{gnatmake}.
3146 @end itemize
3147
3148 @node The Ada Library Information Files,Binding an Ada Program,Source Dependencies,The GNAT Compilation Model
3149 @anchor{gnat_ugn/the_gnat_compilation_model id33}@anchor{66}@anchor{gnat_ugn/the_gnat_compilation_model the-ada-library-information-files}@anchor{28}
3150 @section The Ada Library Information Files
3151
3152
3153 @geindex Ada Library Information files
3154
3155 @geindex ALI files
3156
3157 Each compilation actually generates two output files. The first of these
3158 is the normal object file that has a @code{.o} extension. The second is a
3159 text file containing full dependency information. It has the same
3160 name as the source file, but an @code{.ali} extension.
3161 This file is known as the Ada Library Information (@code{ALI}) file.
3162 The following information is contained in the @code{ALI} file.
3163
3164
3165 @itemize *
3166
3167 @item
3168 Version information (indicates which version of GNAT was used to compile
3169 the unit(s) in question)
3170
3171 @item
3172 Main program information (including priority and time slice settings,
3173 as well as the wide character encoding used during compilation).
3174
3175 @item
3176 List of arguments used in the @code{gcc} command for the compilation
3177
3178 @item
3179 Attributes of the unit, including configuration pragmas used, an indication
3180 of whether the compilation was successful, exception model used etc.
3181
3182 @item
3183 A list of relevant restrictions applying to the unit (used for consistency)
3184 checking.
3185
3186 @item
3187 Categorization information (e.g., use of pragma @code{Pure}).
3188
3189 @item
3190 Information on all @emph{with}ed units, including presence of
3191 @code{Elaborate} or @code{Elaborate_All} pragmas.
3192
3193 @item
3194 Information from any @code{Linker_Options} pragmas used in the unit
3195
3196 @item
3197 Information on the use of @code{Body_Version} or @code{Version}
3198 attributes in the unit.
3199
3200 @item
3201 Dependency information. This is a list of files, together with
3202 time stamp and checksum information. These are files on which
3203 the unit depends in the sense that recompilation is required
3204 if any of these units are modified.
3205
3206 @item
3207 Cross-reference data. Contains information on all entities referenced
3208 in the unit. Used by tools like @code{gnatxref} and @code{gnatfind} to
3209 provide cross-reference information.
3210 @end itemize
3211
3212 For a full detailed description of the format of the @code{ALI} file,
3213 see the source of the body of unit @code{Lib.Writ}, contained in file
3214 @code{lib-writ.adb} in the GNAT compiler sources.
3215
3216 @node Binding an Ada Program,GNAT and Libraries,The Ada Library Information Files,The GNAT Compilation Model
3217 @anchor{gnat_ugn/the_gnat_compilation_model binding-an-ada-program}@anchor{29}@anchor{gnat_ugn/the_gnat_compilation_model id34}@anchor{67}
3218 @section Binding an Ada Program
3219
3220
3221 When using languages such as C and C++, once the source files have been
3222 compiled the only remaining step in building an executable program
3223 is linking the object modules together. This means that it is possible to
3224 link an inconsistent version of a program, in which two units have
3225 included different versions of the same header.
3226
3227 The rules of Ada do not permit such an inconsistent program to be built.
3228 For example, if two clients have different versions of the same package,
3229 it is illegal to build a program containing these two clients.
3230 These rules are enforced by the GNAT binder, which also determines an
3231 elaboration order consistent with the Ada rules.
3232
3233 The GNAT binder is run after all the object files for a program have
3234 been created. It is given the name of the main program unit, and from
3235 this it determines the set of units required by the program, by reading the
3236 corresponding ALI files. It generates error messages if the program is
3237 inconsistent or if no valid order of elaboration exists.
3238
3239 If no errors are detected, the binder produces a main program, in Ada by
3240 default, that contains calls to the elaboration procedures of those
3241 compilation unit that require them, followed by
3242 a call to the main program. This Ada program is compiled to generate the
3243 object file for the main program. The name of
3244 the Ada file is @code{b~xxx}.adb` (with the corresponding spec
3245 @code{b~xxx}.ads`) where @code{xxx} is the name of the
3246 main program unit.
3247
3248 Finally, the linker is used to build the resulting executable program,
3249 using the object from the main program from the bind step as well as the
3250 object files for the Ada units of the program.
3251
3252 @node GNAT and Libraries,Conditional Compilation,Binding an Ada Program,The GNAT Compilation Model
3253 @anchor{gnat_ugn/the_gnat_compilation_model gnat-and-libraries}@anchor{2a}@anchor{gnat_ugn/the_gnat_compilation_model id35}@anchor{68}
3254 @section GNAT and Libraries
3255
3256
3257 @geindex Library building and using
3258
3259 This section describes how to build and use libraries with GNAT, and also shows
3260 how to recompile the GNAT run-time library. You should be familiar with the
3261 Project Manager facility (see the @emph{GNAT_Project_Manager} chapter of the
3262 @emph{GPRbuild User’s Guide}) before reading this chapter.
3263
3264 @menu
3265 * Introduction to Libraries in GNAT::
3266 * General Ada Libraries::
3267 * Stand-alone Ada Libraries::
3268 * Rebuilding the GNAT Run-Time Library::
3269
3270 @end menu
3271
3272 @node Introduction to Libraries in GNAT,General Ada Libraries,,GNAT and Libraries
3273 @anchor{gnat_ugn/the_gnat_compilation_model id36}@anchor{69}@anchor{gnat_ugn/the_gnat_compilation_model introduction-to-libraries-in-gnat}@anchor{6a}
3274 @subsection Introduction to Libraries in GNAT
3275
3276
3277 A library is, conceptually, a collection of objects which does not have its
3278 own main thread of execution, but rather provides certain services to the
3279 applications that use it. A library can be either statically linked with the
3280 application, in which case its code is directly included in the application,
3281 or, on platforms that support it, be dynamically linked, in which case
3282 its code is shared by all applications making use of this library.
3283
3284 GNAT supports both types of libraries.
3285 In the static case, the compiled code can be provided in different ways. The
3286 simplest approach is to provide directly the set of objects resulting from
3287 compilation of the library source files. Alternatively, you can group the
3288 objects into an archive using whatever commands are provided by the operating
3289 system. For the latter case, the objects are grouped into a shared library.
3290
3291 In the GNAT environment, a library has three types of components:
3292
3293
3294 @itemize *
3295
3296 @item
3297 Source files,
3298
3299 @item
3300 @code{ALI} files (see @ref{28,,The Ada Library Information Files}), and
3301
3302 @item
3303 Object files, an archive or a shared library.
3304 @end itemize
3305
3306 A GNAT library may expose all its source files, which is useful for
3307 documentation purposes. Alternatively, it may expose only the units needed by
3308 an external user to make use of the library. That is to say, the specs
3309 reflecting the library services along with all the units needed to compile
3310 those specs, which can include generic bodies or any body implementing an
3311 inlined routine. In the case of @emph{stand-alone libraries} those exposed
3312 units are called @emph{interface units} (@ref{6b,,Stand-alone Ada Libraries}).
3313
3314 All compilation units comprising an application, including those in a library,
3315 need to be elaborated in an order partially defined by Ada’s semantics. GNAT
3316 computes the elaboration order from the @code{ALI} files and this is why they
3317 constitute a mandatory part of GNAT libraries.
3318 @emph{Stand-alone libraries} are the exception to this rule because a specific
3319 library elaboration routine is produced independently of the application(s)
3320 using the library.
3321
3322 @node General Ada Libraries,Stand-alone Ada Libraries,Introduction to Libraries in GNAT,GNAT and Libraries
3323 @anchor{gnat_ugn/the_gnat_compilation_model general-ada-libraries}@anchor{6c}@anchor{gnat_ugn/the_gnat_compilation_model id37}@anchor{6d}
3324 @subsection General Ada Libraries
3325
3326
3327 @menu
3328 * Building a library::
3329 * Installing a library::
3330 * Using a library::
3331
3332 @end menu
3333
3334 @node Building a library,Installing a library,,General Ada Libraries
3335 @anchor{gnat_ugn/the_gnat_compilation_model building-a-library}@anchor{6e}@anchor{gnat_ugn/the_gnat_compilation_model id38}@anchor{6f}
3336 @subsubsection Building a library
3337
3338
3339 The easiest way to build a library is to use the Project Manager,
3340 which supports a special type of project called a @emph{Library Project}
3341 (see the @emph{Library Projects} section in the @emph{GNAT Project Manager}
3342 chapter of the @emph{GPRbuild User’s Guide}).
3343
3344 A project is considered a library project, when two project-level attributes
3345 are defined in it: @code{Library_Name} and @code{Library_Dir}. In order to
3346 control different aspects of library configuration, additional optional
3347 project-level attributes can be specified:
3348
3349
3350 @itemize *
3351
3352 @item
3353
3354 @table @asis
3355
3356 @item @code{Library_Kind}
3357
3358 This attribute controls whether the library is to be static or dynamic
3359 @end table
3360
3361 @item
3362
3363 @table @asis
3364
3365 @item @code{Library_Version}
3366
3367 This attribute specifies the library version; this value is used
3368 during dynamic linking of shared libraries to determine if the currently
3369 installed versions of the binaries are compatible.
3370 @end table
3371
3372 @item
3373 @code{Library_Options}
3374
3375 @item
3376
3377 @table @asis
3378
3379 @item @code{Library_GCC}
3380
3381 These attributes specify additional low-level options to be used during
3382 library generation, and redefine the actual application used to generate
3383 library.
3384 @end table
3385 @end itemize
3386
3387 The GNAT Project Manager takes full care of the library maintenance task,
3388 including recompilation of the source files for which objects do not exist
3389 or are not up to date, assembly of the library archive, and installation of
3390 the library (i.e., copying associated source, object and @code{ALI} files
3391 to the specified location).
3392
3393 Here is a simple library project file:
3394
3395 @example
3396 project My_Lib is
3397 for Source_Dirs use ("src1", "src2");
3398 for Object_Dir use "obj";
3399 for Library_Name use "mylib";
3400 for Library_Dir use "lib";
3401 for Library_Kind use "dynamic";
3402 end My_lib;
3403 @end example
3404
3405 and the compilation command to build and install the library:
3406
3407 @example
3408 $ gnatmake -Pmy_lib
3409 @end example
3410
3411 It is not entirely trivial to perform manually all the steps required to
3412 produce a library. We recommend that you use the GNAT Project Manager
3413 for this task. In special cases where this is not desired, the necessary
3414 steps are discussed below.
3415
3416 There are various possibilities for compiling the units that make up the
3417 library: for example with a Makefile (@ref{70,,Using the GNU make Utility}) or
3418 with a conventional script. For simple libraries, it is also possible to create
3419 a dummy main program which depends upon all the packages that comprise the
3420 interface of the library. This dummy main program can then be given to
3421 @code{gnatmake}, which will ensure that all necessary objects are built.
3422
3423 After this task is accomplished, you should follow the standard procedure
3424 of the underlying operating system to produce the static or shared library.
3425
3426 Here is an example of such a dummy program:
3427
3428 @example
3429 with My_Lib.Service1;
3430 with My_Lib.Service2;
3431 with My_Lib.Service3;
3432 procedure My_Lib_Dummy is
3433 begin
3434 null;
3435 end;
3436 @end example
3437
3438 Here are the generic commands that will build an archive or a shared library.
3439
3440 @example
3441 # compiling the library
3442 $ gnatmake -c my_lib_dummy.adb
3443
3444 # we don't need the dummy object itself
3445 $ rm my_lib_dummy.o my_lib_dummy.ali
3446
3447 # create an archive with the remaining objects
3448 $ ar rc libmy_lib.a *.o
3449 # some systems may require "ranlib" to be run as well
3450
3451 # or create a shared library
3452 $ gcc -shared -o libmy_lib.so *.o
3453 # some systems may require the code to have been compiled with -fPIC
3454
3455 # remove the object files that are now in the library
3456 $ rm *.o
3457
3458 # Make the ALI files read-only so that gnatmake will not try to
3459 # regenerate the objects that are in the library
3460 $ chmod -w *.ali
3461 @end example
3462
3463 Please note that the library must have a name of the form @code{lib@emph{xxx}.a}
3464 or @code{lib@emph{xxx}.so} (or @code{lib@emph{xxx}.dll} on Windows) in order to
3465 be accessed by the directive @code{-l@emph{xxx}} at link time.
3466
3467 @node Installing a library,Using a library,Building a library,General Ada Libraries
3468 @anchor{gnat_ugn/the_gnat_compilation_model id39}@anchor{71}@anchor{gnat_ugn/the_gnat_compilation_model installing-a-library}@anchor{72}
3469 @subsubsection Installing a library
3470
3471
3472 @geindex ADA_PROJECT_PATH
3473
3474 @geindex GPR_PROJECT_PATH
3475
3476 If you use project files, library installation is part of the library build
3477 process (see the @emph{Installing a Library with Project Files} section of the
3478 @emph{GNAT Project Manager} chapter of the @emph{GPRbuild User’s Guide}).
3479
3480 When project files are not an option, it is also possible, but not recommended,
3481 to install the library so that the sources needed to use the library are on the
3482 Ada source path and the ALI files & libraries be on the Ada Object path (see
3483 @ref{73,,Search Paths and the Run-Time Library (RTL)}. Alternatively, the system
3484 administrator can place general-purpose libraries in the default compiler
3485 paths, by specifying the libraries’ location in the configuration files
3486 @code{ada_source_path} and @code{ada_object_path}. These configuration files
3487 must be located in the GNAT installation tree at the same place as the gcc spec
3488 file. The location of the gcc spec file can be determined as follows:
3489
3490 @example
3491 $ gcc -v
3492 @end example
3493
3494 The configuration files mentioned above have a simple format: each line
3495 must contain one unique directory name.
3496 Those names are added to the corresponding path
3497 in their order of appearance in the file. The names can be either absolute
3498 or relative; in the latter case, they are relative to where theses files
3499 are located.
3500
3501 The files @code{ada_source_path} and @code{ada_object_path} might not be
3502 present in a
3503 GNAT installation, in which case, GNAT will look for its run-time library in
3504 the directories @code{adainclude} (for the sources) and @code{adalib} (for the
3505 objects and @code{ALI} files). When the files exist, the compiler does not
3506 look in @code{adainclude} and @code{adalib}, and thus the
3507 @code{ada_source_path} file
3508 must contain the location for the GNAT run-time sources (which can simply
3509 be @code{adainclude}). In the same way, the @code{ada_object_path} file must
3510 contain the location for the GNAT run-time objects (which can simply
3511 be @code{adalib}).
3512
3513 You can also specify a new default path to the run-time library at compilation
3514 time with the switch @code{--RTS=rts-path}. You can thus choose / change
3515 the run-time library you want your program to be compiled with. This switch is
3516 recognized by @code{gcc}, @code{gnatmake}, @code{gnatbind},
3517 @code{gnatls}, @code{gnatfind} and @code{gnatxref}.
3518
3519 It is possible to install a library before or after the standard GNAT
3520 library, by reordering the lines in the configuration files. In general, a
3521 library must be installed before the GNAT library if it redefines
3522 any part of it.
3523
3524 @node Using a library,,Installing a library,General Ada Libraries
3525 @anchor{gnat_ugn/the_gnat_compilation_model id40}@anchor{74}@anchor{gnat_ugn/the_gnat_compilation_model using-a-library}@anchor{75}
3526 @subsubsection Using a library
3527
3528
3529 Once again, the project facility greatly simplifies the use of
3530 libraries. In this context, using a library is just a matter of adding a
3531 @emph{with} clause in the user project. For instance, to make use of the
3532 library @code{My_Lib} shown in examples in earlier sections, you can
3533 write:
3534
3535 @example
3536 with "my_lib";
3537 project My_Proj is
3538 ...
3539 end My_Proj;
3540 @end example
3541
3542 Even if you have a third-party, non-Ada library, you can still use GNAT’s
3543 Project Manager facility to provide a wrapper for it. For example, the
3544 following project, when @emph{with}ed by your main project, will link with the
3545 third-party library @code{liba.a}:
3546
3547 @example
3548 project Liba is
3549 for Externally_Built use "true";
3550 for Source_Files use ();
3551 for Library_Dir use "lib";
3552 for Library_Name use "a";
3553 for Library_Kind use "static";
3554 end Liba;
3555 @end example
3556
3557 This is an alternative to the use of @code{pragma Linker_Options}. It is
3558 especially interesting in the context of systems with several interdependent
3559 static libraries where finding a proper linker order is not easy and best be
3560 left to the tools having visibility over project dependence information.
3561
3562 In order to use an Ada library manually, you need to make sure that this
3563 library is on both your source and object path
3564 (see @ref{73,,Search Paths and the Run-Time Library (RTL)}
3565 and @ref{76,,Search Paths for gnatbind}). Furthermore, when the objects are grouped
3566 in an archive or a shared library, you need to specify the desired
3567 library at link time.
3568
3569 For example, you can use the library @code{mylib} installed in
3570 @code{/dir/my_lib_src} and @code{/dir/my_lib_obj} with the following commands:
3571
3572 @example
3573 $ gnatmake -aI/dir/my_lib_src -aO/dir/my_lib_obj my_appl \\
3574 -largs -lmy_lib
3575 @end example
3576
3577 This can be expressed more simply:
3578
3579 @example
3580 $ gnatmake my_appl
3581 @end example
3582
3583 when the following conditions are met:
3584
3585
3586 @itemize *
3587
3588 @item
3589 @code{/dir/my_lib_src} has been added by the user to the environment
3590 variable
3591 @geindex ADA_INCLUDE_PATH
3592 @geindex environment variable; ADA_INCLUDE_PATH
3593 @code{ADA_INCLUDE_PATH}, or by the administrator to the file
3594 @code{ada_source_path}
3595
3596 @item
3597 @code{/dir/my_lib_obj} has been added by the user to the environment
3598 variable
3599 @geindex ADA_OBJECTS_PATH
3600 @geindex environment variable; ADA_OBJECTS_PATH
3601 @code{ADA_OBJECTS_PATH}, or by the administrator to the file
3602 @code{ada_object_path}
3603
3604 @item
3605 a pragma @code{Linker_Options} has been added to one of the sources.
3606 For example:
3607
3608 @example
3609 pragma Linker_Options ("-lmy_lib");
3610 @end example
3611 @end itemize
3612
3613 Note that you may also load a library dynamically at
3614 run time given its filename, as illustrated in the GNAT @code{plugins} example
3615 in the directory @code{share/examples/gnat/plugins} within the GNAT
3616 install area.
3617
3618 @node Stand-alone Ada Libraries,Rebuilding the GNAT Run-Time Library,General Ada Libraries,GNAT and Libraries
3619 @anchor{gnat_ugn/the_gnat_compilation_model id41}@anchor{77}@anchor{gnat_ugn/the_gnat_compilation_model stand-alone-ada-libraries}@anchor{6b}
3620 @subsection Stand-alone Ada Libraries
3621
3622
3623 @geindex Stand-alone libraries
3624
3625 @menu
3626 * Introduction to Stand-alone Libraries::
3627 * Building a Stand-alone Library::
3628 * Creating a Stand-alone Library to be used in a non-Ada context::
3629 * Restrictions in Stand-alone Libraries::
3630
3631 @end menu
3632
3633 @node Introduction to Stand-alone Libraries,Building a Stand-alone Library,,Stand-alone Ada Libraries
3634 @anchor{gnat_ugn/the_gnat_compilation_model id42}@anchor{78}@anchor{gnat_ugn/the_gnat_compilation_model introduction-to-stand-alone-libraries}@anchor{79}
3635 @subsubsection Introduction to Stand-alone Libraries
3636
3637
3638 A Stand-alone Library (abbreviated ‘SAL’) is a library that contains the
3639 necessary code to
3640 elaborate the Ada units that are included in the library. In contrast with
3641 an ordinary library, which consists of all sources, objects and @code{ALI}
3642 files of the
3643 library, a SAL may specify a restricted subset of compilation units
3644 to serve as a library interface. In this case, the fully
3645 self-sufficient set of files will normally consist of an objects
3646 archive, the sources of interface units’ specs, and the @code{ALI}
3647 files of interface units.
3648 If an interface spec contains a generic unit or an inlined subprogram,
3649 the body’s
3650 source must also be provided; if the units that must be provided in the source
3651 form depend on other units, the source and @code{ALI} files of those must
3652 also be provided.
3653
3654 The main purpose of a SAL is to minimize the recompilation overhead of client
3655 applications when a new version of the library is installed. Specifically,
3656 if the interface sources have not changed, client applications do not need to
3657 be recompiled. If, furthermore, a SAL is provided in the shared form and its
3658 version, controlled by @code{Library_Version} attribute, is not changed,
3659 then the clients do not need to be relinked.
3660
3661 SALs also allow the library providers to minimize the amount of library source
3662 text exposed to the clients. Such ‘information hiding’ might be useful or
3663 necessary for various reasons.
3664
3665 Stand-alone libraries are also well suited to be used in an executable whose
3666 main routine is not written in Ada.
3667
3668 @node Building a Stand-alone Library,Creating a Stand-alone Library to be used in a non-Ada context,Introduction to Stand-alone Libraries,Stand-alone Ada Libraries
3669 @anchor{gnat_ugn/the_gnat_compilation_model building-a-stand-alone-library}@anchor{7a}@anchor{gnat_ugn/the_gnat_compilation_model id43}@anchor{7b}
3670 @subsubsection Building a Stand-alone Library
3671
3672
3673 GNAT’s Project facility provides a simple way of building and installing
3674 stand-alone libraries; see the @emph{Stand-alone Library Projects} section
3675 in the @emph{GNAT Project Manager} chapter of the @emph{GPRbuild User’s Guide}.
3676 To be a Stand-alone Library Project, in addition to the two attributes
3677 that make a project a Library Project (@code{Library_Name} and
3678 @code{Library_Dir}; see the @emph{Library Projects} section in the
3679 @emph{GNAT Project Manager} chapter of the @emph{GPRbuild User’s Guide}),
3680 the attribute @code{Library_Interface} must be defined. For example:
3681
3682 @example
3683 for Library_Dir use "lib_dir";
3684 for Library_Name use "dummy";
3685 for Library_Interface use ("int1", "int1.child");
3686 @end example
3687
3688 Attribute @code{Library_Interface} has a non-empty string list value,
3689 each string in the list designating a unit contained in an immediate source
3690 of the project file.
3691
3692 When a Stand-alone Library is built, first the binder is invoked to build
3693 a package whose name depends on the library name
3694 (@code{b~dummy.ads/b} in the example above).
3695 This binder-generated package includes initialization and
3696 finalization procedures whose
3697 names depend on the library name (@code{dummyinit} and @code{dummyfinal}
3698 in the example
3699 above). The object corresponding to this package is included in the library.
3700
3701 You must ensure timely (e.g., prior to any use of interfaces in the SAL)
3702 calling of these procedures if a static SAL is built, or if a shared SAL
3703 is built
3704 with the project-level attribute @code{Library_Auto_Init} set to
3705 @code{"false"}.
3706
3707 For a Stand-Alone Library, only the @code{ALI} files of the Interface Units
3708 (those that are listed in attribute @code{Library_Interface}) are copied to
3709 the Library Directory. As a consequence, only the Interface Units may be
3710 imported from Ada units outside of the library. If other units are imported,
3711 the binding phase will fail.
3712
3713 It is also possible to build an encapsulated library where not only
3714 the code to elaborate and finalize the library is embedded but also
3715 ensuring that the library is linked only against static
3716 libraries. So an encapsulated library only depends on system
3717 libraries, all other code, including the GNAT runtime, is embedded. To
3718 build an encapsulated library the attribute
3719 @code{Library_Standalone} must be set to @code{encapsulated}:
3720
3721 @example
3722 for Library_Dir use "lib_dir";
3723 for Library_Name use "dummy";
3724 for Library_Kind use "dynamic";
3725 for Library_Interface use ("int1", "int1.child");
3726 for Library_Standalone use "encapsulated";
3727 @end example
3728
3729 The default value for this attribute is @code{standard} in which case
3730 a stand-alone library is built.
3731
3732 The attribute @code{Library_Src_Dir} may be specified for a
3733 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
3734 single string value. Its value must be the path (absolute or relative to the
3735 project directory) of an existing directory. This directory cannot be the
3736 object directory or one of the source directories, but it can be the same as
3737 the library directory. The sources of the Interface
3738 Units of the library that are needed by an Ada client of the library will be
3739 copied to the designated directory, called the Interface Copy directory.
3740 These sources include the specs of the Interface Units, but they may also
3741 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
3742 are used, or when there is a generic unit in the spec. Before the sources
3743 are copied to the Interface Copy directory, an attempt is made to delete all
3744 files in the Interface Copy directory.
3745
3746 Building stand-alone libraries by hand is somewhat tedious, but for those
3747 occasions when it is necessary here are the steps that you need to perform:
3748
3749
3750 @itemize *
3751
3752 @item
3753 Compile all library sources.
3754
3755 @item
3756 Invoke the binder with the switch @code{-n} (No Ada main program),
3757 with all the @code{ALI} files of the interfaces, and
3758 with the switch @code{-L} to give specific names to the @code{init}
3759 and @code{final} procedures. For example:
3760
3761 @example
3762 $ gnatbind -n int1.ali int2.ali -Lsal1
3763 @end example
3764
3765 @item
3766 Compile the binder generated file:
3767
3768 @example
3769 $ gcc -c b~int2.adb
3770 @end example
3771
3772 @item
3773 Link the dynamic library with all the necessary object files,
3774 indicating to the linker the names of the @code{init} (and possibly
3775 @code{final}) procedures for automatic initialization (and finalization).
3776 The built library should be placed in a directory different from
3777 the object directory.
3778
3779 @item
3780 Copy the @code{ALI} files of the interface to the library directory,
3781 add in this copy an indication that it is an interface to a SAL
3782 (i.e., add a word @code{SL} on the line in the @code{ALI} file that starts
3783 with letter ‘P’) and make the modified copy of the @code{ALI} file
3784 read-only.
3785 @end itemize
3786
3787 Using SALs is not different from using other libraries
3788 (see @ref{75,,Using a library}).
3789
3790 @node Creating a Stand-alone Library to be used in a non-Ada context,Restrictions in Stand-alone Libraries,Building a Stand-alone Library,Stand-alone Ada Libraries
3791 @anchor{gnat_ugn/the_gnat_compilation_model creating-a-stand-alone-library-to-be-used-in-a-non-ada-context}@anchor{7c}@anchor{gnat_ugn/the_gnat_compilation_model id44}@anchor{7d}
3792 @subsubsection Creating a Stand-alone Library to be used in a non-Ada context
3793
3794
3795 It is easy to adapt the SAL build procedure discussed above for use of a SAL in
3796 a non-Ada context.
3797
3798 The only extra step required is to ensure that library interface subprograms
3799 are compatible with the main program, by means of @code{pragma Export}
3800 or @code{pragma Convention}.
3801
3802 Here is an example of simple library interface for use with C main program:
3803
3804 @example
3805 package My_Package is
3806
3807 procedure Do_Something;
3808 pragma Export (C, Do_Something, "do_something");
3809
3810 procedure Do_Something_Else;
3811 pragma Export (C, Do_Something_Else, "do_something_else");
3812
3813 end My_Package;
3814 @end example
3815
3816 On the foreign language side, you must provide a ‘foreign’ view of the
3817 library interface; remember that it should contain elaboration routines in
3818 addition to interface subprograms.
3819
3820 The example below shows the content of @code{mylib_interface.h} (note
3821 that there is no rule for the naming of this file, any name can be used)
3822
3823 @example
3824 /* the library elaboration procedure */
3825 extern void mylibinit (void);
3826
3827 /* the library finalization procedure */
3828 extern void mylibfinal (void);
3829
3830 /* the interface exported by the library */
3831 extern void do_something (void);
3832 extern void do_something_else (void);
3833 @end example
3834
3835 Libraries built as explained above can be used from any program, provided
3836 that the elaboration procedures (named @code{mylibinit} in the previous
3837 example) are called before the library services are used. Any number of
3838 libraries can be used simultaneously, as long as the elaboration
3839 procedure of each library is called.
3840
3841 Below is an example of a C program that uses the @code{mylib} library.
3842
3843 @example
3844 #include "mylib_interface.h"
3845
3846 int
3847 main (void)
3848 @{
3849 /* First, elaborate the library before using it */
3850 mylibinit ();
3851
3852 /* Main program, using the library exported entities */
3853 do_something ();
3854 do_something_else ();
3855
3856 /* Library finalization at the end of the program */
3857 mylibfinal ();
3858 return 0;
3859 @}
3860 @end example
3861
3862 Note that invoking any library finalization procedure generated by
3863 @code{gnatbind} shuts down the Ada run-time environment.
3864 Consequently, the
3865 finalization of all Ada libraries must be performed at the end of the program.
3866 No call to these libraries or to the Ada run-time library should be made
3867 after the finalization phase.
3868
3869 Note also that special care must be taken with multi-tasks
3870 applications. The initialization and finalization routines are not
3871 protected against concurrent access. If such requirement is needed it
3872 must be ensured at the application level using a specific operating
3873 system services like a mutex or a critical-section.
3874
3875 @node Restrictions in Stand-alone Libraries,,Creating a Stand-alone Library to be used in a non-Ada context,Stand-alone Ada Libraries
3876 @anchor{gnat_ugn/the_gnat_compilation_model id45}@anchor{7e}@anchor{gnat_ugn/the_gnat_compilation_model restrictions-in-stand-alone-libraries}@anchor{7f}
3877 @subsubsection Restrictions in Stand-alone Libraries
3878
3879
3880 The pragmas listed below should be used with caution inside libraries,
3881 as they can create incompatibilities with other Ada libraries:
3882
3883
3884 @itemize *
3885
3886 @item
3887 pragma @code{Locking_Policy}
3888
3889 @item
3890 pragma @code{Partition_Elaboration_Policy}
3891
3892 @item
3893 pragma @code{Queuing_Policy}
3894
3895 @item
3896 pragma @code{Task_Dispatching_Policy}
3897
3898 @item
3899 pragma @code{Unreserve_All_Interrupts}
3900 @end itemize
3901
3902 When using a library that contains such pragmas, the user must make sure
3903 that all libraries use the same pragmas with the same values. Otherwise,
3904 @code{Program_Error} will
3905 be raised during the elaboration of the conflicting
3906 libraries. The usage of these pragmas and its consequences for the user
3907 should therefore be well documented.
3908
3909 Similarly, the traceback in the exception occurrence mechanism should be
3910 enabled or disabled in a consistent manner across all libraries.
3911 Otherwise, Program_Error will be raised during the elaboration of the
3912 conflicting libraries.
3913
3914 If the @code{Version} or @code{Body_Version}
3915 attributes are used inside a library, then you need to
3916 perform a @code{gnatbind} step that specifies all @code{ALI} files in all
3917 libraries, so that version identifiers can be properly computed.
3918 In practice these attributes are rarely used, so this is unlikely
3919 to be a consideration.
3920
3921 @node Rebuilding the GNAT Run-Time Library,,Stand-alone Ada Libraries,GNAT and Libraries
3922 @anchor{gnat_ugn/the_gnat_compilation_model id46}@anchor{80}@anchor{gnat_ugn/the_gnat_compilation_model rebuilding-the-gnat-run-time-library}@anchor{81}
3923 @subsection Rebuilding the GNAT Run-Time Library
3924
3925
3926 @geindex GNAT Run-Time Library
3927 @geindex rebuilding
3928
3929 @geindex Building the GNAT Run-Time Library
3930
3931 @geindex Rebuilding the GNAT Run-Time Library
3932
3933 @geindex Run-Time Library
3934 @geindex rebuilding
3935
3936 It may be useful to recompile the GNAT library in various debugging or
3937 experimentation contexts. A project file called
3938 @code{libada.gpr} is provided to that effect and can be found in
3939 the directory containing the GNAT library. The location of this
3940 directory depends on the way the GNAT environment has been installed and can
3941 be determined by means of the command:
3942
3943 @example
3944 $ gnatls -v
3945 @end example
3946
3947 The last entry in the source search path usually contains the
3948 gnat library (the @code{adainclude} directory). This project file contains its
3949 own documentation and in particular the set of instructions needed to rebuild a
3950 new library and to use it.
3951
3952 Note that rebuilding the GNAT Run-Time is only recommended for temporary
3953 experiments or debugging, and is not supported.
3954
3955 @geindex Conditional compilation
3956
3957 @node Conditional Compilation,Mixed Language Programming,GNAT and Libraries,The GNAT Compilation Model
3958 @anchor{gnat_ugn/the_gnat_compilation_model conditional-compilation}@anchor{2b}@anchor{gnat_ugn/the_gnat_compilation_model id47}@anchor{82}
3959 @section Conditional Compilation
3960
3961
3962 This section presents some guidelines for modeling conditional compilation in Ada and describes the
3963 gnatprep preprocessor utility.
3964
3965 @geindex Conditional compilation
3966
3967 @menu
3968 * Modeling Conditional Compilation in Ada::
3969 * Preprocessing with gnatprep::
3970 * Integrated Preprocessing::
3971
3972 @end menu
3973
3974 @node Modeling Conditional Compilation in Ada,Preprocessing with gnatprep,,Conditional Compilation
3975 @anchor{gnat_ugn/the_gnat_compilation_model id48}@anchor{83}@anchor{gnat_ugn/the_gnat_compilation_model modeling-conditional-compilation-in-ada}@anchor{84}
3976 @subsection Modeling Conditional Compilation in Ada
3977
3978
3979 It is often necessary to arrange for a single source program
3980 to serve multiple purposes, where it is compiled in different
3981 ways to achieve these different goals. Some examples of the
3982 need for this feature are
3983
3984
3985 @itemize *
3986
3987 @item
3988 Adapting a program to a different hardware environment
3989
3990 @item
3991 Adapting a program to a different target architecture
3992
3993 @item
3994 Turning debugging features on and off
3995
3996 @item
3997 Arranging for a program to compile with different compilers
3998 @end itemize
3999
4000 In C, or C++, the typical approach would be to use the preprocessor
4001 that is defined as part of the language. The Ada language does not
4002 contain such a feature. This is not an oversight, but rather a very
4003 deliberate design decision, based on the experience that overuse of
4004 the preprocessing features in C and C++ can result in programs that
4005 are extremely difficult to maintain. For example, if we have ten
4006 switches that can be on or off, this means that there are a thousand
4007 separate programs, any one of which might not even be syntactically
4008 correct, and even if syntactically correct, the resulting program
4009 might not work correctly. Testing all combinations can quickly become
4010 impossible.
4011
4012 Nevertheless, the need to tailor programs certainly exists, and in
4013 this section we will discuss how this can
4014 be achieved using Ada in general, and GNAT in particular.
4015
4016 @menu
4017 * Use of Boolean Constants::
4018 * Debugging - A Special Case::
4019 * Conditionalizing Declarations::
4020 * Use of Alternative Implementations::
4021 * Preprocessing::
4022
4023 @end menu
4024
4025 @node Use of Boolean Constants,Debugging - A Special Case,,Modeling Conditional Compilation in Ada
4026 @anchor{gnat_ugn/the_gnat_compilation_model id49}@anchor{85}@anchor{gnat_ugn/the_gnat_compilation_model use-of-boolean-constants}@anchor{86}
4027 @subsubsection Use of Boolean Constants
4028
4029
4030 In the case where the difference is simply which code
4031 sequence is executed, the cleanest solution is to use Boolean
4032 constants to control which code is executed.
4033
4034 @example
4035 FP_Initialize_Required : constant Boolean := True;
4036 ...
4037 if FP_Initialize_Required then
4038 ...
4039 end if;
4040 @end example
4041
4042 Not only will the code inside the @code{if} statement not be executed if
4043 the constant Boolean is @code{False}, but it will also be completely
4044 deleted from the program.
4045 However, the code is only deleted after the @code{if} statement
4046 has been checked for syntactic and semantic correctness.
4047 (In contrast, with preprocessors the code is deleted before the
4048 compiler ever gets to see it, so it is not checked until the switch
4049 is turned on.)
4050
4051 @geindex Preprocessors (contrasted with conditional compilation)
4052
4053 Typically the Boolean constants will be in a separate package,
4054 something like:
4055
4056 @example
4057 package Config is
4058 FP_Initialize_Required : constant Boolean := True;
4059 Reset_Available : constant Boolean := False;
4060 ...
4061 end Config;
4062 @end example
4063
4064 The @code{Config} package exists in multiple forms for the various targets,
4065 with an appropriate script selecting the version of @code{Config} needed.
4066 Then any other unit requiring conditional compilation can do a @emph{with}
4067 of @code{Config} to make the constants visible.
4068
4069 @node Debugging - A Special Case,Conditionalizing Declarations,Use of Boolean Constants,Modeling Conditional Compilation in Ada
4070 @anchor{gnat_ugn/the_gnat_compilation_model debugging-a-special-case}@anchor{87}@anchor{gnat_ugn/the_gnat_compilation_model id50}@anchor{88}
4071 @subsubsection Debugging - A Special Case
4072
4073
4074 A common use of conditional code is to execute statements (for example
4075 dynamic checks, or output of intermediate results) under control of a
4076 debug switch, so that the debugging behavior can be turned on and off.
4077 This can be done using a Boolean constant to control whether the code
4078 is active:
4079
4080 @example
4081 if Debugging then
4082 Put_Line ("got to the first stage!");
4083 end if;
4084 @end example
4085
4086 or
4087
4088 @example
4089 if Debugging and then Temperature > 999.0 then
4090 raise Temperature_Crazy;
4091 end if;
4092 @end example
4093
4094 @geindex pragma Assert
4095
4096 Since this is a common case, there are special features to deal with
4097 this in a convenient manner. For the case of tests, Ada 2005 has added
4098 a pragma @code{Assert} that can be used for such tests. This pragma is modeled
4099 on the @code{Assert} pragma that has always been available in GNAT, so this
4100 feature may be used with GNAT even if you are not using Ada 2005 features.
4101 The use of pragma @code{Assert} is described in the
4102 @cite{GNAT_Reference_Manual}, but as an
4103 example, the last test could be written:
4104
4105 @example
4106 pragma Assert (Temperature <= 999.0, "Temperature Crazy");
4107 @end example
4108
4109 or simply
4110
4111 @example
4112 pragma Assert (Temperature <= 999.0);
4113 @end example
4114
4115 In both cases, if assertions are active and the temperature is excessive,
4116 the exception @code{Assert_Failure} will be raised, with the given string in
4117 the first case or a string indicating the location of the pragma in the second
4118 case used as the exception message.
4119
4120 @geindex pragma Assertion_Policy
4121
4122 You can turn assertions on and off by using the @code{Assertion_Policy}
4123 pragma.
4124
4125 @geindex -gnata switch
4126
4127 This is an Ada 2005 pragma which is implemented in all modes by
4128 GNAT. Alternatively, you can use the @code{-gnata} switch
4129 to enable assertions from the command line, which applies to
4130 all versions of Ada.
4131
4132 @geindex pragma Debug
4133
4134 For the example above with the @code{Put_Line}, the GNAT-specific pragma
4135 @code{Debug} can be used:
4136
4137 @example
4138 pragma Debug (Put_Line ("got to the first stage!"));
4139 @end example
4140
4141 If debug pragmas are enabled, the argument, which must be of the form of
4142 a procedure call, is executed (in this case, @code{Put_Line} will be called).
4143 Only one call can be present, but of course a special debugging procedure
4144 containing any code you like can be included in the program and then
4145 called in a pragma @code{Debug} argument as needed.
4146
4147 One advantage of pragma @code{Debug} over the @code{if Debugging then}
4148 construct is that pragma @code{Debug} can appear in declarative contexts,
4149 such as at the very beginning of a procedure, before local declarations have
4150 been elaborated.
4151
4152 @geindex pragma Debug_Policy
4153
4154 Debug pragmas are enabled using either the @code{-gnata} switch that also
4155 controls assertions, or with a separate Debug_Policy pragma.
4156
4157 The latter pragma is new in the Ada 2005 versions of GNAT (but it can be used
4158 in Ada 95 and Ada 83 programs as well), and is analogous to
4159 pragma @code{Assertion_Policy} to control assertions.
4160
4161 @code{Assertion_Policy} and @code{Debug_Policy} are configuration pragmas,
4162 and thus they can appear in @code{gnat.adc} if you are not using a
4163 project file, or in the file designated to contain configuration pragmas
4164 in a project file.
4165 They then apply to all subsequent compilations. In practice the use of
4166 the @code{-gnata} switch is often the most convenient method of controlling
4167 the status of these pragmas.
4168
4169 Note that a pragma is not a statement, so in contexts where a statement
4170 sequence is required, you can’t just write a pragma on its own. You have
4171 to add a @code{null} statement.
4172
4173 @example
4174 if ... then
4175 ... -- some statements
4176 else
4177 pragma Assert (Num_Cases < 10);
4178 null;
4179 end if;
4180 @end example
4181
4182 @node Conditionalizing Declarations,Use of Alternative Implementations,Debugging - A Special Case,Modeling Conditional Compilation in Ada
4183 @anchor{gnat_ugn/the_gnat_compilation_model conditionalizing-declarations}@anchor{89}@anchor{gnat_ugn/the_gnat_compilation_model id51}@anchor{8a}
4184 @subsubsection Conditionalizing Declarations
4185
4186
4187 In some cases it may be necessary to conditionalize declarations to meet
4188 different requirements. For example we might want a bit string whose length
4189 is set to meet some hardware message requirement.
4190
4191 This may be possible using declare blocks controlled
4192 by conditional constants:
4193
4194 @example
4195 if Small_Machine then
4196 declare
4197 X : Bit_String (1 .. 10);
4198 begin
4199 ...
4200 end;
4201 else
4202 declare
4203 X : Large_Bit_String (1 .. 1000);
4204 begin
4205 ...
4206 end;
4207 end if;
4208 @end example
4209
4210 Note that in this approach, both declarations are analyzed by the
4211 compiler so this can only be used where both declarations are legal,
4212 even though one of them will not be used.
4213
4214 Another approach is to define integer constants, e.g., @code{Bits_Per_Word},
4215 or Boolean constants, e.g., @code{Little_Endian}, and then write declarations
4216 that are parameterized by these constants. For example
4217
4218 @example
4219 for Rec use
4220 Field1 at 0 range Boolean'Pos (Little_Endian) * 10 .. Bits_Per_Word;
4221 end record;
4222 @end example
4223
4224 If @code{Bits_Per_Word} is set to 32, this generates either
4225
4226 @example
4227 for Rec use
4228 Field1 at 0 range 0 .. 32;
4229 end record;
4230 @end example
4231
4232 for the big endian case, or
4233
4234 @example
4235 for Rec use record
4236 Field1 at 0 range 10 .. 32;
4237 end record;
4238 @end example
4239
4240 for the little endian case. Since a powerful subset of Ada expression
4241 notation is usable for creating static constants, clever use of this
4242 feature can often solve quite difficult problems in conditionalizing
4243 compilation (note incidentally that in Ada 95, the little endian
4244 constant was introduced as @code{System.Default_Bit_Order}, so you do not
4245 need to define this one yourself).
4246
4247 @node Use of Alternative Implementations,Preprocessing,Conditionalizing Declarations,Modeling Conditional Compilation in Ada
4248 @anchor{gnat_ugn/the_gnat_compilation_model id52}@anchor{8b}@anchor{gnat_ugn/the_gnat_compilation_model use-of-alternative-implementations}@anchor{8c}
4249 @subsubsection Use of Alternative Implementations
4250
4251
4252 In some cases, none of the approaches described above are adequate. This
4253 can occur for example if the set of declarations required is radically
4254 different for two different configurations.
4255
4256 In this situation, the official Ada way of dealing with conditionalizing
4257 such code is to write separate units for the different cases. As long as
4258 this does not result in excessive duplication of code, this can be done
4259 without creating maintenance problems. The approach is to share common
4260 code as far as possible, and then isolate the code and declarations
4261 that are different. Subunits are often a convenient method for breaking
4262 out a piece of a unit that is to be conditionalized, with separate files
4263 for different versions of the subunit for different targets, where the
4264 build script selects the right one to give to the compiler.
4265
4266 @geindex Subunits (and conditional compilation)
4267
4268 As an example, consider a situation where a new feature in Ada 2005
4269 allows something to be done in a really nice way. But your code must be able
4270 to compile with an Ada 95 compiler. Conceptually you want to say:
4271
4272 @example
4273 if Ada_2005 then
4274 ... neat Ada 2005 code
4275 else
4276 ... not quite as neat Ada 95 code
4277 end if;
4278 @end example
4279
4280 where @code{Ada_2005} is a Boolean constant.
4281
4282 But this won’t work when @code{Ada_2005} is set to @code{False},
4283 since the @code{then} clause will be illegal for an Ada 95 compiler.
4284 (Recall that although such unreachable code would eventually be deleted
4285 by the compiler, it still needs to be legal. If it uses features
4286 introduced in Ada 2005, it will be illegal in Ada 95.)
4287
4288 So instead we write
4289
4290 @example
4291 procedure Insert is separate;
4292 @end example
4293
4294 Then we have two files for the subunit @code{Insert}, with the two sets of
4295 code.
4296 If the package containing this is called @code{File_Queries}, then we might
4297 have two files
4298
4299
4300 @itemize *
4301
4302 @item
4303 @code{file_queries-insert-2005.adb}
4304
4305 @item
4306 @code{file_queries-insert-95.adb}
4307 @end itemize
4308
4309 and the build script renames the appropriate file to @code{file_queries-insert.adb} and then carries out the compilation.
4310
4311 This can also be done with project files’ naming schemes. For example:
4312
4313 @example
4314 for body ("File_Queries.Insert") use "file_queries-insert-2005.ada";
4315 @end example
4316
4317 Note also that with project files it is desirable to use a different extension
4318 than @code{ads} / @code{adb} for alternative versions. Otherwise a naming
4319 conflict may arise through another commonly used feature: to declare as part
4320 of the project a set of directories containing all the sources obeying the
4321 default naming scheme.
4322
4323 The use of alternative units is certainly feasible in all situations,
4324 and for example the Ada part of the GNAT run-time is conditionalized
4325 based on the target architecture using this approach. As a specific example,
4326 consider the implementation of the AST feature in VMS. There is one
4327 spec: @code{s-asthan.ads} which is the same for all architectures, and three
4328 bodies:
4329
4330
4331 @itemize *
4332
4333 @item
4334
4335 @table @asis
4336
4337 @item @code{s-asthan.adb}
4338
4339 used for all non-VMS operating systems
4340 @end table
4341
4342 @item
4343
4344 @table @asis
4345
4346 @item @code{s-asthan-vms-alpha.adb}
4347
4348 used for VMS on the Alpha
4349 @end table
4350
4351 @item
4352
4353 @table @asis
4354
4355 @item @code{s-asthan-vms-ia64.adb}
4356
4357 used for VMS on the ia64
4358 @end table
4359 @end itemize
4360
4361 The dummy version @code{s-asthan.adb} simply raises exceptions noting that
4362 this operating system feature is not available, and the two remaining
4363 versions interface with the corresponding versions of VMS to provide
4364 VMS-compatible AST handling. The GNAT build script knows the architecture
4365 and operating system, and automatically selects the right version,
4366 renaming it if necessary to @code{s-asthan.adb} before the run-time build.
4367
4368 Another style for arranging alternative implementations is through Ada’s
4369 access-to-subprogram facility.
4370 In case some functionality is to be conditionally included,
4371 you can declare an access-to-procedure variable @code{Ref} that is initialized
4372 to designate a ‘do nothing’ procedure, and then invoke @code{Ref.all}
4373 when appropriate.
4374 In some library package, set @code{Ref} to @code{Proc'Access} for some
4375 procedure @code{Proc} that performs the relevant processing.
4376 The initialization only occurs if the library package is included in the
4377 program.
4378 The same idea can also be implemented using tagged types and dispatching
4379 calls.
4380
4381 @node Preprocessing,,Use of Alternative Implementations,Modeling Conditional Compilation in Ada
4382 @anchor{gnat_ugn/the_gnat_compilation_model id53}@anchor{8d}@anchor{gnat_ugn/the_gnat_compilation_model preprocessing}@anchor{8e}
4383 @subsubsection Preprocessing
4384
4385
4386 @geindex Preprocessing
4387
4388 Although it is quite possible to conditionalize code without the use of
4389 C-style preprocessing, as described earlier in this section, it is
4390 nevertheless convenient in some cases to use the C approach. Moreover,
4391 older Ada compilers have often provided some preprocessing capability,
4392 so legacy code may depend on this approach, even though it is not
4393 standard.
4394
4395 To accommodate such use, GNAT provides a preprocessor (modeled to a large
4396 extent on the various preprocessors that have been used
4397 with legacy code on other compilers, to enable easier transition).
4398
4399 @geindex gnatprep
4400
4401 The preprocessor may be used in two separate modes. It can be used quite
4402 separately from the compiler, to generate a separate output source file
4403 that is then fed to the compiler as a separate step. This is the
4404 @code{gnatprep} utility, whose use is fully described in
4405 @ref{8f,,Preprocessing with gnatprep}.
4406
4407 The preprocessing language allows such constructs as
4408
4409 @example
4410 #if DEBUG or else (PRIORITY > 4) then
4411 sequence of declarations
4412 #else
4413 completely different sequence of declarations
4414 #end if;
4415 @end example
4416
4417 The values of the symbols @code{DEBUG} and @code{PRIORITY} can be
4418 defined either on the command line or in a separate file.
4419
4420 The other way of running the preprocessor is even closer to the C style and
4421 often more convenient. In this approach the preprocessing is integrated into
4422 the compilation process. The compiler is given the preprocessor input which
4423 includes @code{#if} lines etc, and then the compiler carries out the
4424 preprocessing internally and processes the resulting output.
4425 For more details on this approach, see @ref{90,,Integrated Preprocessing}.
4426
4427 @node Preprocessing with gnatprep,Integrated Preprocessing,Modeling Conditional Compilation in Ada,Conditional Compilation
4428 @anchor{gnat_ugn/the_gnat_compilation_model id54}@anchor{91}@anchor{gnat_ugn/the_gnat_compilation_model preprocessing-with-gnatprep}@anchor{8f}
4429 @subsection Preprocessing with @code{gnatprep}
4430
4431
4432 @geindex gnatprep
4433
4434 @geindex Preprocessing (gnatprep)
4435
4436 This section discusses how to use GNAT’s @code{gnatprep} utility for simple
4437 preprocessing.
4438 Although designed for use with GNAT, @code{gnatprep} does not depend on any
4439 special GNAT features.
4440 For further discussion of conditional compilation in general, see
4441 @ref{2b,,Conditional Compilation}.
4442
4443 @menu
4444 * Preprocessing Symbols::
4445 * Using gnatprep::
4446 * Switches for gnatprep::
4447 * Form of Definitions File::
4448 * Form of Input Text for gnatprep::
4449
4450 @end menu
4451
4452 @node Preprocessing Symbols,Using gnatprep,,Preprocessing with gnatprep
4453 @anchor{gnat_ugn/the_gnat_compilation_model id55}@anchor{92}@anchor{gnat_ugn/the_gnat_compilation_model preprocessing-symbols}@anchor{93}
4454 @subsubsection Preprocessing Symbols
4455
4456
4457 Preprocessing symbols are defined in @emph{definition files} and referenced in the
4458 sources to be preprocessed. A preprocessing symbol is an identifier, following
4459 normal Ada (case-insensitive) rules for its syntax, with the restriction that
4460 all characters need to be in the ASCII set (no accented letters).
4461
4462 @node Using gnatprep,Switches for gnatprep,Preprocessing Symbols,Preprocessing with gnatprep
4463 @anchor{gnat_ugn/the_gnat_compilation_model id56}@anchor{94}@anchor{gnat_ugn/the_gnat_compilation_model using-gnatprep}@anchor{95}
4464 @subsubsection Using @code{gnatprep}
4465
4466
4467 To call @code{gnatprep} use:
4468
4469 @example
4470 $ gnatprep [ switches ] infile outfile [ deffile ]
4471 @end example
4472
4473 where
4474
4475
4476 @itemize *
4477
4478 @item
4479
4480 @table @asis
4481
4482 @item @emph{switches}
4483
4484 is an optional sequence of switches as described in the next section.
4485 @end table
4486
4487 @item
4488
4489 @table @asis
4490
4491 @item @emph{infile}
4492
4493 is the full name of the input file, which is an Ada source
4494 file containing preprocessor directives.
4495 @end table
4496
4497 @item
4498
4499 @table @asis
4500
4501 @item @emph{outfile}
4502
4503 is the full name of the output file, which is an Ada source
4504 in standard Ada form. When used with GNAT, this file name will
4505 normally have an @code{ads} or @code{adb} suffix.
4506 @end table
4507
4508 @item
4509
4510 @table @asis
4511
4512 @item @code{deffile}
4513
4514 is the full name of a text file containing definitions of
4515 preprocessing symbols to be referenced by the preprocessor. This argument is
4516 optional, and can be replaced by the use of the @code{-D} switch.
4517 @end table
4518 @end itemize
4519
4520 @node Switches for gnatprep,Form of Definitions File,Using gnatprep,Preprocessing with gnatprep
4521 @anchor{gnat_ugn/the_gnat_compilation_model id57}@anchor{96}@anchor{gnat_ugn/the_gnat_compilation_model switches-for-gnatprep}@anchor{97}
4522 @subsubsection Switches for @code{gnatprep}
4523
4524
4525 @geindex --version (gnatprep)
4526
4527
4528 @table @asis
4529
4530 @item @code{--version}
4531
4532 Display Copyright and version, then exit disregarding all other options.
4533 @end table
4534
4535 @geindex --help (gnatprep)
4536
4537
4538 @table @asis
4539
4540 @item @code{--help}
4541
4542 If @code{--version} was not used, display usage and then exit disregarding
4543 all other options.
4544 @end table
4545
4546 @geindex -b (gnatprep)
4547
4548
4549 @table @asis
4550
4551 @item @code{-b}
4552
4553 Causes both preprocessor lines and the lines deleted by
4554 preprocessing to be replaced by blank lines in the output source file,
4555 preserving line numbers in the output file.
4556 @end table
4557
4558 @geindex -c (gnatprep)
4559
4560
4561 @table @asis
4562
4563 @item @code{-c}
4564
4565 Causes both preprocessor lines and the lines deleted
4566 by preprocessing to be retained in the output source as comments marked
4567 with the special string @code{"--! "}. This option will result in line numbers
4568 being preserved in the output file.
4569 @end table
4570
4571 @geindex -C (gnatprep)
4572
4573
4574 @table @asis
4575
4576 @item @code{-C}
4577
4578 Causes comments to be scanned. Normally comments are ignored by gnatprep.
4579 If this option is specified, then comments are scanned and any $symbol
4580 substitutions performed as in program text. This is particularly useful
4581 when structured comments are used (e.g., for programs written in a
4582 pre-2014 version of the SPARK Ada subset). Note that this switch is not
4583 available when doing integrated preprocessing (it would be useless in
4584 this context since comments are ignored by the compiler in any case).
4585 @end table
4586
4587 @geindex -D (gnatprep)
4588
4589
4590 @table @asis
4591
4592 @item @code{-D@emph{symbol}[=@emph{value}]}
4593
4594 Defines a new preprocessing symbol with the specified value. If no value is given
4595 on the command line, then symbol is considered to be @code{True}. This switch
4596 can be used in place of a definition file.
4597 @end table
4598
4599 @geindex -r (gnatprep)
4600
4601
4602 @table @asis
4603
4604 @item @code{-r}
4605
4606 Causes a @code{Source_Reference} pragma to be generated that
4607 references the original input file, so that error messages will use
4608 the file name of this original file. The use of this switch implies
4609 that preprocessor lines are not to be removed from the file, so its
4610 use will force @code{-b} mode if @code{-c}
4611 has not been specified explicitly.
4612
4613 Note that if the file to be preprocessed contains multiple units, then
4614 it will be necessary to @code{gnatchop} the output file from
4615 @code{gnatprep}. If a @code{Source_Reference} pragma is present
4616 in the preprocessed file, it will be respected by
4617 @code{gnatchop -r}
4618 so that the final chopped files will correctly refer to the original
4619 input source file for @code{gnatprep}.
4620 @end table
4621
4622 @geindex -s (gnatprep)
4623
4624
4625 @table @asis
4626
4627 @item @code{-s}
4628
4629 Causes a sorted list of symbol names and values to be
4630 listed on the standard output file.
4631 @end table
4632
4633 @geindex -T (gnatprep)
4634
4635
4636 @table @asis
4637
4638 @item @code{-T}
4639
4640 Use LF as line terminators when writing files. By default the line terminator
4641 of the host (LF under unix, CR/LF under Windows) is used.
4642 @end table
4643
4644 @geindex -u (gnatprep)
4645
4646
4647 @table @asis
4648
4649 @item @code{-u}
4650
4651 Causes undefined symbols to be treated as having the value FALSE in the context
4652 of a preprocessor test. In the absence of this option, an undefined symbol in
4653 a @code{#if} or @code{#elsif} test will be treated as an error.
4654 @end table
4655
4656 @geindex -v (gnatprep)
4657
4658
4659 @table @asis
4660
4661 @item @code{-v}
4662
4663 Verbose mode: generates more output about work done.
4664 @end table
4665
4666 Note: if neither @code{-b} nor @code{-c} is present,
4667 then preprocessor lines and
4668 deleted lines are completely removed from the output, unless -r is
4669 specified, in which case -b is assumed.
4670
4671 @node Form of Definitions File,Form of Input Text for gnatprep,Switches for gnatprep,Preprocessing with gnatprep
4672 @anchor{gnat_ugn/the_gnat_compilation_model form-of-definitions-file}@anchor{98}@anchor{gnat_ugn/the_gnat_compilation_model id58}@anchor{99}
4673 @subsubsection Form of Definitions File
4674
4675
4676 The definitions file contains lines of the form:
4677
4678 @example
4679 symbol := value
4680 @end example
4681
4682 where @code{symbol} is a preprocessing symbol, and @code{value} is one of the following:
4683
4684
4685 @itemize *
4686
4687 @item
4688 Empty, corresponding to a null substitution,
4689
4690 @item
4691 A string literal using normal Ada syntax, or
4692
4693 @item
4694 Any sequence of characters from the set @{letters, digits, period, underline@}.
4695 @end itemize
4696
4697 Comment lines may also appear in the definitions file, starting with
4698 the usual @code{--},
4699 and comments may be added to the definitions lines.
4700
4701 @node Form of Input Text for gnatprep,,Form of Definitions File,Preprocessing with gnatprep
4702 @anchor{gnat_ugn/the_gnat_compilation_model form-of-input-text-for-gnatprep}@anchor{9a}@anchor{gnat_ugn/the_gnat_compilation_model id59}@anchor{9b}
4703 @subsubsection Form of Input Text for @code{gnatprep}
4704
4705
4706 The input text may contain preprocessor conditional inclusion lines,
4707 as well as general symbol substitution sequences.
4708
4709 The preprocessor conditional inclusion commands have the form:
4710
4711 @example
4712 #if <expression> [then]
4713 lines
4714 #elsif <expression> [then]
4715 lines
4716 #elsif <expression> [then]
4717 lines
4718 ...
4719 #else
4720 lines
4721 #end if;
4722 @end example
4723
4724 In this example, <expression> is defined by the following grammar:
4725
4726 @example
4727 <expression> ::= <symbol>
4728 <expression> ::= <symbol> = "<value>"
4729 <expression> ::= <symbol> = <symbol>
4730 <expression> ::= <symbol> = <integer>
4731 <expression> ::= <symbol> > <integer>
4732 <expression> ::= <symbol> >= <integer>
4733 <expression> ::= <symbol> < <integer>
4734 <expression> ::= <symbol> <= <integer>
4735 <expression> ::= <symbol> 'Defined
4736 <expression> ::= not <expression>
4737 <expression> ::= <expression> and <expression>
4738 <expression> ::= <expression> or <expression>
4739 <expression> ::= <expression> and then <expression>
4740 <expression> ::= <expression> or else <expression>
4741 <expression> ::= ( <expression> )
4742 @end example
4743
4744 Note the following restriction: it is not allowed to have “and” or “or”
4745 following “not” in the same expression without parentheses. For example, this
4746 is not allowed:
4747
4748 @example
4749 not X or Y
4750 @end example
4751
4752 This can be expressed instead as one of the following forms:
4753
4754 @example
4755 (not X) or Y
4756 not (X or Y)
4757 @end example
4758
4759 For the first test (<expression> ::= <symbol>) the symbol must have
4760 either the value true or false, that is to say the right-hand of the
4761 symbol definition must be one of the (case-insensitive) literals
4762 @code{True} or @code{False}. If the value is true, then the
4763 corresponding lines are included, and if the value is false, they are
4764 excluded.
4765
4766 When comparing a symbol to an integer, the integer is any non negative
4767 literal integer as defined in the Ada Reference Manual, such as 3, 16#FF# or
4768 2#11#. The symbol value must also be a non negative integer. Integer values
4769 in the range 0 .. 2**31-1 are supported.
4770
4771 The test (<expression> ::= <symbol>’Defined) is true only if
4772 the symbol has been defined in the definition file or by a @code{-D}
4773 switch on the command line. Otherwise, the test is false.
4774
4775 The equality tests are case insensitive, as are all the preprocessor lines.
4776
4777 If the symbol referenced is not defined in the symbol definitions file,
4778 then the effect depends on whether or not switch @code{-u}
4779 is specified. If so, then the symbol is treated as if it had the value
4780 false and the test fails. If this switch is not specified, then
4781 it is an error to reference an undefined symbol. It is also an error to
4782 reference a symbol that is defined with a value other than @code{True}
4783 or @code{False}.
4784
4785 The use of the @code{not} operator inverts the sense of this logical test.
4786 The @code{not} operator cannot be combined with the @code{or} or @code{and}
4787 operators, without parentheses. For example, “if not X or Y then” is not
4788 allowed, but “if (not X) or Y then” and “if not (X or Y) then” are.
4789
4790 The @code{then} keyword is optional as shown
4791
4792 The @code{#} must be the first non-blank character on a line, but
4793 otherwise the format is free form. Spaces or tabs may appear between
4794 the @code{#} and the keyword. The keywords and the symbols are case
4795 insensitive as in normal Ada code. Comments may be used on a
4796 preprocessor line, but other than that, no other tokens may appear on a
4797 preprocessor line. Any number of @code{elsif} clauses can be present,
4798 including none at all. The @code{else} is optional, as in Ada.
4799
4800 The @code{#} marking the start of a preprocessor line must be the first
4801 non-blank character on the line, i.e., it must be preceded only by
4802 spaces or horizontal tabs.
4803
4804 Symbol substitution outside of preprocessor lines is obtained by using
4805 the sequence:
4806
4807 @example
4808 $symbol
4809 @end example
4810
4811 anywhere within a source line, except in a comment or within a
4812 string literal. The identifier
4813 following the @code{$} must match one of the symbols defined in the symbol
4814 definition file, and the result is to substitute the value of the
4815 symbol in place of @code{$symbol} in the output file.
4816
4817 Note that although the substitution of strings within a string literal
4818 is not possible, it is possible to have a symbol whose defined value is
4819 a string literal. So instead of setting XYZ to @code{hello} and writing:
4820
4821 @example
4822 Header : String := "$XYZ";
4823 @end example
4824
4825 you should set XYZ to @code{"hello"} and write:
4826
4827 @example
4828 Header : String := $XYZ;
4829 @end example
4830
4831 and then the substitution will occur as desired.
4832
4833 @node Integrated Preprocessing,,Preprocessing with gnatprep,Conditional Compilation
4834 @anchor{gnat_ugn/the_gnat_compilation_model id60}@anchor{9c}@anchor{gnat_ugn/the_gnat_compilation_model integrated-preprocessing}@anchor{90}
4835 @subsection Integrated Preprocessing
4836
4837
4838 As noted above, a file to be preprocessed consists of Ada source code
4839 in which preprocessing lines have been inserted. However,
4840 instead of using @code{gnatprep} to explicitly preprocess a file as a separate
4841 step before compilation, you can carry out the preprocessing implicitly
4842 as part of compilation. Such @emph{integrated preprocessing}, which is the common
4843 style with C, is performed when either or both of the following switches
4844 are passed to the compiler:
4845
4846 @quotation
4847
4848
4849 @itemize *
4850
4851 @item
4852 @code{-gnatep}, which specifies the @emph{preprocessor data file}.
4853 This file dictates how the source files will be preprocessed (e.g., which
4854 symbol definition files apply to which sources).
4855
4856 @item
4857 @code{-gnateD}, which defines values for preprocessing symbols.
4858 @end itemize
4859 @end quotation
4860
4861 Integrated preprocessing applies only to Ada source files, it is
4862 not available for configuration pragma files.
4863
4864 With integrated preprocessing, the output from the preprocessor is not,
4865 by default, written to any external file. Instead it is passed
4866 internally to the compiler. To preserve the result of
4867 preprocessing in a file, either run @code{gnatprep}
4868 in standalone mode or else supply the @code{-gnateG} switch
4869 (described below) to the compiler.
4870
4871 When using project files:
4872
4873 @quotation
4874
4875
4876 @itemize *
4877
4878 @item
4879 the builder switch @code{-x} should be used if any Ada source is
4880 compiled with @code{gnatep=}, so that the compiler finds the
4881 @emph{preprocessor data file}.
4882
4883 @item
4884 the preprocessing data file and the symbol definition files should be
4885 located in the source directories of the project.
4886 @end itemize
4887 @end quotation
4888
4889 Note that the @code{gnatmake} switch @code{-m} will almost
4890 always trigger recompilation for sources that are preprocessed,
4891 because @code{gnatmake} cannot compute the checksum of the source after
4892 preprocessing.
4893
4894 The actual preprocessing function is described in detail in
4895 @ref{8f,,Preprocessing with gnatprep}. This section explains the switches
4896 that relate to integrated preprocessing.
4897
4898 @geindex -gnatep (gcc)
4899
4900
4901 @table @asis
4902
4903 @item @code{-gnatep=@emph{preprocessor_data_file}}
4904
4905 This switch specifies the file name (without directory
4906 information) of the preprocessor data file. Either place this file
4907 in one of the source directories, or, when using project
4908 files, reference the project file’s directory via the
4909 @code{project_name'Project_Dir} project attribute; e.g:
4910
4911 @quotation
4912
4913 @example
4914 project Prj is
4915 package Compiler is
4916 for Switches ("Ada") use
4917 ("-gnatep=" & Prj'Project_Dir & "prep.def");
4918 end Compiler;
4919 end Prj;
4920 @end example
4921 @end quotation
4922
4923 A preprocessor data file is a text file that contains @emph{preprocessor
4924 control lines}. A preprocessor control line directs the preprocessing of
4925 either a particular source file, or, analogous to @code{others} in Ada,
4926 all sources not specified elsewhere in the preprocessor data file.
4927 A preprocessor control line
4928 can optionally identify a @emph{definition file} that assigns values to
4929 preprocessor symbols, as well as a list of switches that relate to
4930 preprocessing.
4931 Empty lines and comments (using Ada syntax) are also permitted, with no
4932 semantic effect.
4933
4934 Here’s an example of a preprocessor data file:
4935
4936 @quotation
4937
4938 @example
4939 "toto.adb" "prep.def" -u
4940 -- Preprocess toto.adb, using definition file prep.def
4941 -- Undefined symbols are treated as False
4942
4943 * -c -DVERSION=V101
4944 -- Preprocess all other sources without using a definition file
4945 -- Suppressed lined are commented
4946 -- Symbol VERSION has the value V101
4947
4948 "tata.adb" "prep2.def" -s
4949 -- Preprocess tata.adb, using definition file prep2.def
4950 -- List all symbols with their values
4951 @end example
4952 @end quotation
4953
4954 A preprocessor control line has the following syntax:
4955
4956 @quotation
4957
4958 @example
4959 <preprocessor_control_line> ::=
4960 <preprocessor_input> [ <definition_file_name> ] @{ <switch> @}
4961
4962 <preprocessor_input> ::= <source_file_name> | '*'
4963
4964 <definition_file_name> ::= <string_literal>
4965
4966 <source_file_name> := <string_literal>
4967
4968 <switch> := (See below for list)
4969 @end example
4970 @end quotation
4971
4972 Thus each preprocessor control line starts with either a literal string or
4973 the character ‘*’:
4974
4975
4976 @itemize *
4977
4978 @item
4979 A literal string is the file name (without directory information) of the source
4980 file that will be input to the preprocessor.
4981
4982 @item
4983 The character ‘*’ is a wild-card indicator; the additional parameters on the line
4984 indicate the preprocessing for all the sources
4985 that are not specified explicitly on other lines (the order of the lines is not
4986 significant).
4987 @end itemize
4988
4989 It is an error to have two lines with the same file name or two
4990 lines starting with the character ‘*’.
4991
4992 After the file name or ‘*’, an optional literal string specifies the name of
4993 the definition file to be used for preprocessing
4994 (@ref{98,,Form of Definitions File}). The definition files are found by the
4995 compiler in one of the source directories. In some cases, when compiling
4996 a source in a directory other than the current directory, if the definition
4997 file is in the current directory, it may be necessary to add the current
4998 directory as a source directory through the @code{-I} switch; otherwise
4999 the compiler would not find the definition file.
5000
5001 Finally, switches similar to those of @code{gnatprep} may optionally appear:
5002
5003
5004 @table @asis
5005
5006 @item @code{-b}
5007
5008 Causes both preprocessor lines and the lines deleted by
5009 preprocessing to be replaced by blank lines, preserving the line number.
5010 This switch is always implied; however, if specified after @code{-c}
5011 it cancels the effect of @code{-c}.
5012
5013 @item @code{-c}
5014
5015 Causes both preprocessor lines and the lines deleted
5016 by preprocessing to be retained as comments marked
5017 with the special string ‘@cite{–!}’.
5018
5019 @item @code{-D@emph{symbol}=@emph{new_value}}
5020
5021 Define or redefine @code{symbol} to have @code{new_value} as its value.
5022 The permitted form for @code{symbol} is either an Ada identifier, or any Ada reserved word
5023 aside from @code{if},
5024 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
5025 The permitted form for @code{new_value} is a literal string, an Ada identifier or any Ada reserved
5026 word. A symbol declared with this switch replaces a symbol with the
5027 same name defined in a definition file.
5028
5029 @item @code{-s}
5030
5031 Causes a sorted list of symbol names and values to be
5032 listed on the standard output file.
5033
5034 @item @code{-u}
5035
5036 Causes undefined symbols to be treated as having the value @code{FALSE}
5037 in the context
5038 of a preprocessor test. In the absence of this option, an undefined symbol in
5039 a @code{#if} or @code{#elsif} test will be treated as an error.
5040 @end table
5041 @end table
5042
5043 @geindex -gnateD (gcc)
5044
5045
5046 @table @asis
5047
5048 @item @code{-gnateD@emph{symbol}[=@emph{new_value}]}
5049
5050 Define or redefine @code{symbol} to have @code{new_value} as its value. If no value
5051 is supplied, then the value of @code{symbol} is @code{True}.
5052 The form of @code{symbol} is an identifier, following normal Ada (case-insensitive)
5053 rules for its syntax, and @code{new_value} is either an arbitrary string between double
5054 quotes or any sequence (including an empty sequence) of characters from the
5055 set (letters, digits, period, underline).
5056 Ada reserved words may be used as symbols, with the exceptions of @code{if},
5057 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
5058
5059 Examples:
5060
5061 @quotation
5062
5063 @example
5064 -gnateDToto=Tata
5065 -gnateDFoo
5066 -gnateDFoo=\"Foo-Bar\"
5067 @end example
5068 @end quotation
5069
5070 A symbol declared with this switch on the command line replaces a
5071 symbol with the same name either in a definition file or specified with a
5072 switch @code{-D} in the preprocessor data file.
5073
5074 This switch is similar to switch @code{-D} of @code{gnatprep}.
5075
5076 @item @code{-gnateG}
5077
5078 When integrated preprocessing is performed on source file @code{filename.extension},
5079 create or overwrite @code{filename.extension.prep} to contain
5080 the result of the preprocessing.
5081 For example if the source file is @code{foo.adb} then
5082 the output file will be @code{foo.adb.prep}.
5083 @end table
5084
5085 @node Mixed Language Programming,GNAT and Other Compilation Models,Conditional Compilation,The GNAT Compilation Model
5086 @anchor{gnat_ugn/the_gnat_compilation_model id61}@anchor{9d}@anchor{gnat_ugn/the_gnat_compilation_model mixed-language-programming}@anchor{2c}
5087 @section Mixed Language Programming
5088
5089
5090 @geindex Mixed Language Programming
5091
5092 This section describes how to develop a mixed-language program,
5093 with a focus on combining Ada with C or C++.
5094
5095 @menu
5096 * Interfacing to C::
5097 * Calling Conventions::
5098 * Building Mixed Ada and C++ Programs::
5099 * Generating Ada Bindings for C and C++ headers::
5100 * Generating C Headers for Ada Specifications::
5101
5102 @end menu
5103
5104 @node Interfacing to C,Calling Conventions,,Mixed Language Programming
5105 @anchor{gnat_ugn/the_gnat_compilation_model id62}@anchor{9e}@anchor{gnat_ugn/the_gnat_compilation_model interfacing-to-c}@anchor{9f}
5106 @subsection Interfacing to C
5107
5108
5109 Interfacing Ada with a foreign language such as C involves using
5110 compiler directives to import and/or export entity definitions in each
5111 language – using @code{extern} statements in C, for instance, and the
5112 @code{Import}, @code{Export}, and @code{Convention} pragmas in Ada.
5113 A full treatment of these topics is provided in Appendix B, section 1
5114 of the Ada Reference Manual.
5115
5116 There are two ways to build a program using GNAT that contains some Ada
5117 sources and some foreign language sources, depending on whether or not
5118 the main subprogram is written in Ada. Here is a source example with
5119 the main subprogram in Ada:
5120
5121 @example
5122 /* file1.c */
5123 #include <stdio.h>
5124
5125 void print_num (int num)
5126 @{
5127 printf ("num is %d.\\n", num);
5128 return;
5129 @}
5130 @end example
5131
5132 @example
5133 /* file2.c */
5134
5135 /* num_from_Ada is declared in my_main.adb */
5136 extern int num_from_Ada;
5137
5138 int get_num (void)
5139 @{
5140 return num_from_Ada;
5141 @}
5142 @end example
5143
5144 @example
5145 -- my_main.adb
5146 procedure My_Main is
5147
5148 -- Declare then export an Integer entity called num_from_Ada
5149 My_Num : Integer := 10;
5150 pragma Export (C, My_Num, "num_from_Ada");
5151
5152 -- Declare an Ada function spec for Get_Num, then use
5153 -- C function get_num for the implementation.
5154 function Get_Num return Integer;
5155 pragma Import (C, Get_Num, "get_num");
5156
5157 -- Declare an Ada procedure spec for Print_Num, then use
5158 -- C function print_num for the implementation.
5159 procedure Print_Num (Num : Integer);
5160 pragma Import (C, Print_Num, "print_num");
5161
5162 begin
5163 Print_Num (Get_Num);
5164 end My_Main;
5165 @end example
5166
5167 To build this example:
5168
5169
5170 @itemize *
5171
5172 @item
5173 First compile the foreign language files to
5174 generate object files:
5175
5176 @example
5177 $ gcc -c file1.c
5178 $ gcc -c file2.c
5179 @end example
5180
5181 @item
5182 Then, compile the Ada units to produce a set of object files and ALI
5183 files:
5184
5185 @example
5186 $ gnatmake -c my_main.adb
5187 @end example
5188
5189 @item
5190 Run the Ada binder on the Ada main program:
5191
5192 @example
5193 $ gnatbind my_main.ali
5194 @end example
5195
5196 @item
5197 Link the Ada main program, the Ada objects and the other language
5198 objects:
5199
5200 @example
5201 $ gnatlink my_main.ali file1.o file2.o
5202 @end example
5203 @end itemize
5204
5205 The last three steps can be grouped in a single command:
5206
5207 @example
5208 $ gnatmake my_main.adb -largs file1.o file2.o
5209 @end example
5210
5211 @geindex Binder output file
5212
5213 If the main program is in a language other than Ada, then you may have
5214 more than one entry point into the Ada subsystem. You must use a special
5215 binder option to generate callable routines that initialize and
5216 finalize the Ada units (@ref{a0,,Binding with Non-Ada Main Programs}).
5217 Calls to the initialization and finalization routines must be inserted
5218 in the main program, or some other appropriate point in the code. The
5219 call to initialize the Ada units must occur before the first Ada
5220 subprogram is called, and the call to finalize the Ada units must occur
5221 after the last Ada subprogram returns. The binder will place the
5222 initialization and finalization subprograms into the
5223 @code{b~xxx.adb} file where they can be accessed by your C
5224 sources. To illustrate, we have the following example:
5225
5226 @example
5227 /* main.c */
5228 extern void adainit (void);
5229 extern void adafinal (void);
5230 extern int add (int, int);
5231 extern int sub (int, int);
5232
5233 int main (int argc, char *argv[])
5234 @{
5235 int a = 21, b = 7;
5236
5237 adainit();
5238
5239 /* Should print "21 + 7 = 28" */
5240 printf ("%d + %d = %d\\n", a, b, add (a, b));
5241
5242 /* Should print "21 - 7 = 14" */
5243 printf ("%d - %d = %d\\n", a, b, sub (a, b));
5244
5245 adafinal();
5246 @}
5247 @end example
5248
5249 @example
5250 -- unit1.ads
5251 package Unit1 is
5252 function Add (A, B : Integer) return Integer;
5253 pragma Export (C, Add, "add");
5254 end Unit1;
5255 @end example
5256
5257 @example
5258 -- unit1.adb
5259 package body Unit1 is
5260 function Add (A, B : Integer) return Integer is
5261 begin
5262 return A + B;
5263 end Add;
5264 end Unit1;
5265 @end example
5266
5267 @example
5268 -- unit2.ads
5269 package Unit2 is
5270 function Sub (A, B : Integer) return Integer;
5271 pragma Export (C, Sub, "sub");
5272 end Unit2;
5273 @end example
5274
5275 @example
5276 -- unit2.adb
5277 package body Unit2 is
5278 function Sub (A, B : Integer) return Integer is
5279 begin
5280 return A - B;
5281 end Sub;
5282 end Unit2;
5283 @end example
5284
5285 The build procedure for this application is similar to the last
5286 example’s:
5287
5288
5289 @itemize *
5290
5291 @item
5292 First, compile the foreign language files to generate object files:
5293
5294 @example
5295 $ gcc -c main.c
5296 @end example
5297
5298 @item
5299 Next, compile the Ada units to produce a set of object files and ALI
5300 files:
5301
5302 @example
5303 $ gnatmake -c unit1.adb
5304 $ gnatmake -c unit2.adb
5305 @end example
5306
5307 @item
5308 Run the Ada binder on every generated ALI file. Make sure to use the
5309 @code{-n} option to specify a foreign main program:
5310
5311 @example
5312 $ gnatbind -n unit1.ali unit2.ali
5313 @end example
5314
5315 @item
5316 Link the Ada main program, the Ada objects and the foreign language
5317 objects. You need only list the last ALI file here:
5318
5319 @example
5320 $ gnatlink unit2.ali main.o -o exec_file
5321 @end example
5322
5323 This procedure yields a binary executable called @code{exec_file}.
5324 @end itemize
5325
5326 Depending on the circumstances (for example when your non-Ada main object
5327 does not provide symbol @code{main}), you may also need to instruct the
5328 GNAT linker not to include the standard startup objects by passing the
5329 @code{-nostartfiles} switch to @code{gnatlink}.
5330
5331 @node Calling Conventions,Building Mixed Ada and C++ Programs,Interfacing to C,Mixed Language Programming
5332 @anchor{gnat_ugn/the_gnat_compilation_model calling-conventions}@anchor{a1}@anchor{gnat_ugn/the_gnat_compilation_model id63}@anchor{a2}
5333 @subsection Calling Conventions
5334
5335
5336 @geindex Foreign Languages
5337
5338 @geindex Calling Conventions
5339
5340 GNAT follows standard calling sequence conventions and will thus interface
5341 to any other language that also follows these conventions. The following
5342 Convention identifiers are recognized by GNAT:
5343
5344 @geindex Interfacing to Ada
5345
5346 @geindex Other Ada compilers
5347
5348 @geindex Convention Ada
5349
5350
5351 @table @asis
5352
5353 @item @code{Ada}
5354
5355 This indicates that the standard Ada calling sequence will be
5356 used and all Ada data items may be passed without any limitations in the
5357 case where GNAT is used to generate both the caller and callee. It is also
5358 possible to mix GNAT generated code and code generated by another Ada
5359 compiler. In this case, the data types should be restricted to simple
5360 cases, including primitive types. Whether complex data types can be passed
5361 depends on the situation. Probably it is safe to pass simple arrays, such
5362 as arrays of integers or floats. Records may or may not work, depending
5363 on whether both compilers lay them out identically. Complex structures
5364 involving variant records, access parameters, tasks, or protected types,
5365 are unlikely to be able to be passed.
5366
5367 Note that in the case of GNAT running
5368 on a platform that supports HP Ada 83, a higher degree of compatibility
5369 can be guaranteed, and in particular records are laid out in an identical
5370 manner in the two compilers. Note also that if output from two different
5371 compilers is mixed, the program is responsible for dealing with elaboration
5372 issues. Probably the safest approach is to write the main program in the
5373 version of Ada other than GNAT, so that it takes care of its own elaboration
5374 requirements, and then call the GNAT-generated adainit procedure to ensure
5375 elaboration of the GNAT components. Consult the documentation of the other
5376 Ada compiler for further details on elaboration.
5377
5378 However, it is not possible to mix the tasking run time of GNAT and
5379 HP Ada 83, All the tasking operations must either be entirely within
5380 GNAT compiled sections of the program, or entirely within HP Ada 83
5381 compiled sections of the program.
5382 @end table
5383
5384 @geindex Interfacing to Assembly
5385
5386 @geindex Convention Assembler
5387
5388
5389 @table @asis
5390
5391 @item @code{Assembler}
5392
5393 Specifies assembler as the convention. In practice this has the
5394 same effect as convention Ada (but is not equivalent in the sense of being
5395 considered the same convention).
5396 @end table
5397
5398 @geindex Convention Asm
5399
5400 @geindex Asm
5401
5402
5403 @table @asis
5404
5405 @item @code{Asm}
5406
5407 Equivalent to Assembler.
5408
5409 @geindex Interfacing to COBOL
5410
5411 @geindex Convention COBOL
5412 @end table
5413
5414 @geindex COBOL
5415
5416
5417 @table @asis
5418
5419 @item @code{COBOL}
5420
5421 Data will be passed according to the conventions described
5422 in section B.4 of the Ada Reference Manual.
5423 @end table
5424
5425 @geindex C
5426
5427 @geindex Interfacing to C
5428
5429 @geindex Convention C
5430
5431
5432 @table @asis
5433
5434 @item @code{C}
5435
5436 Data will be passed according to the conventions described
5437 in section B.3 of the Ada Reference Manual.
5438
5439 A note on interfacing to a C ‘varargs’ function:
5440
5441 @quotation
5442
5443 @geindex C varargs function
5444
5445 @geindex Interfacing to C varargs function
5446
5447 @geindex varargs function interfaces
5448
5449 In C, @code{varargs} allows a function to take a variable number of
5450 arguments. There is no direct equivalent in this to Ada. One
5451 approach that can be used is to create a C wrapper for each
5452 different profile and then interface to this C wrapper. For
5453 example, to print an @code{int} value using @code{printf},
5454 create a C function @code{printfi} that takes two arguments, a
5455 pointer to a string and an int, and calls @code{printf}.
5456 Then in the Ada program, use pragma @code{Import} to
5457 interface to @code{printfi}.
5458
5459 It may work on some platforms to directly interface to
5460 a @code{varargs} function by providing a specific Ada profile
5461 for a particular call. However, this does not work on
5462 all platforms, since there is no guarantee that the
5463 calling sequence for a two argument normal C function
5464 is the same as for calling a @code{varargs} C function with
5465 the same two arguments.
5466 @end quotation
5467 @end table
5468
5469 @geindex Convention Default
5470
5471 @geindex Default
5472
5473
5474 @table @asis
5475
5476 @item @code{Default}
5477
5478 Equivalent to C.
5479 @end table
5480
5481 @geindex Convention External
5482
5483 @geindex External
5484
5485
5486 @table @asis
5487
5488 @item @code{External}
5489
5490 Equivalent to C.
5491 @end table
5492
5493 @geindex C++
5494
5495 @geindex Interfacing to C++
5496
5497 @geindex Convention C++
5498
5499
5500 @table @asis
5501
5502 @item @code{C_Plus_Plus} (or @code{CPP})
5503
5504 This stands for C++. For most purposes this is identical to C.
5505 See the separate description of the specialized GNAT pragmas relating to
5506 C++ interfacing for further details.
5507 @end table
5508
5509 @geindex Fortran
5510
5511 @geindex Interfacing to Fortran
5512
5513 @geindex Convention Fortran
5514
5515
5516 @table @asis
5517
5518 @item @code{Fortran}
5519
5520 Data will be passed according to the conventions described
5521 in section B.5 of the Ada Reference Manual.
5522
5523 @item @code{Intrinsic}
5524
5525 This applies to an intrinsic operation, as defined in the Ada
5526 Reference Manual. If a pragma Import (Intrinsic) applies to a subprogram,
5527 this means that the body of the subprogram is provided by the compiler itself,
5528 usually by means of an efficient code sequence, and that the user does not
5529 supply an explicit body for it. In an application program, the pragma may
5530 be applied to the following sets of names:
5531
5532
5533 @itemize *
5534
5535 @item
5536 Rotate_Left, Rotate_Right, Shift_Left, Shift_Right, Shift_Right_Arithmetic.
5537 The corresponding subprogram declaration must have
5538 two formal parameters. The
5539 first one must be a signed integer type or a modular type with a binary
5540 modulus, and the second parameter must be of type Natural.
5541 The return type must be the same as the type of the first argument. The size
5542 of this type can only be 8, 16, 32, or 64.
5543
5544 @item
5545 Binary arithmetic operators: ‘+’, ‘-‘, ‘*’, ‘/’.
5546 The corresponding operator declaration must have parameters and result type
5547 that have the same root numeric type (for example, all three are long_float
5548 types). This simplifies the definition of operations that use type checking
5549 to perform dimensional checks:
5550 @end itemize
5551
5552 @example
5553 type Distance is new Long_Float;
5554 type Time is new Long_Float;
5555 type Velocity is new Long_Float;
5556 function "/" (D : Distance; T : Time)
5557 return Velocity;
5558 pragma Import (Intrinsic, "/");
5559
5560 This common idiom is often programmed with a generic definition and an
5561 explicit body. The pragma makes it simpler to introduce such declarations.
5562 It incurs no overhead in compilation time or code size, because it is
5563 implemented as a single machine instruction.
5564 @end example
5565
5566
5567 @itemize *
5568
5569 @item
5570 General subprogram entities. This is used to bind an Ada subprogram
5571 declaration to
5572 a compiler builtin by name with back-ends where such interfaces are
5573 available. A typical example is the set of @code{__builtin} functions
5574 exposed by the GCC back-end, as in the following example:
5575
5576 @example
5577 function builtin_sqrt (F : Float) return Float;
5578 pragma Import (Intrinsic, builtin_sqrt, "__builtin_sqrtf");
5579 @end example
5580
5581 Most of the GCC builtins are accessible this way, and as for other
5582 import conventions (e.g. C), it is the user’s responsibility to ensure
5583 that the Ada subprogram profile matches the underlying builtin
5584 expectations.
5585 @end itemize
5586 @end table
5587
5588 @geindex Stdcall
5589
5590 @geindex Convention Stdcall
5591
5592
5593 @table @asis
5594
5595 @item @code{Stdcall}
5596
5597 This is relevant only to Windows implementations of GNAT,
5598 and specifies that the @code{Stdcall} calling sequence will be used,
5599 as defined by the NT API. Nevertheless, to ease building
5600 cross-platform bindings this convention will be handled as a @code{C} calling
5601 convention on non-Windows platforms.
5602 @end table
5603
5604 @geindex DLL
5605
5606 @geindex Convention DLL
5607
5608
5609 @table @asis
5610
5611 @item @code{DLL}
5612
5613 This is equivalent to @code{Stdcall}.
5614 @end table
5615
5616 @geindex Win32
5617
5618 @geindex Convention Win32
5619
5620
5621 @table @asis
5622
5623 @item @code{Win32}
5624
5625 This is equivalent to @code{Stdcall}.
5626 @end table
5627
5628 @geindex Stubbed
5629
5630 @geindex Convention Stubbed
5631
5632
5633 @table @asis
5634
5635 @item @code{Stubbed}
5636
5637 This is a special convention that indicates that the compiler
5638 should provide a stub body that raises @code{Program_Error}.
5639 @end table
5640
5641 GNAT additionally provides a useful pragma @code{Convention_Identifier}
5642 that can be used to parameterize conventions and allow additional synonyms
5643 to be specified. For example if you have legacy code in which the convention
5644 identifier Fortran77 was used for Fortran, you can use the configuration
5645 pragma:
5646
5647 @example
5648 pragma Convention_Identifier (Fortran77, Fortran);
5649 @end example
5650
5651 And from now on the identifier Fortran77 may be used as a convention
5652 identifier (for example in an @code{Import} pragma) with the same
5653 meaning as Fortran.
5654
5655 @node Building Mixed Ada and C++ Programs,Generating Ada Bindings for C and C++ headers,Calling Conventions,Mixed Language Programming
5656 @anchor{gnat_ugn/the_gnat_compilation_model building-mixed-ada-and-c-programs}@anchor{a3}@anchor{gnat_ugn/the_gnat_compilation_model id64}@anchor{a4}
5657 @subsection Building Mixed Ada and C++ Programs
5658
5659
5660 A programmer inexperienced with mixed-language development may find that
5661 building an application containing both Ada and C++ code can be a
5662 challenge. This section gives a few hints that should make this task easier.
5663
5664 @menu
5665 * Interfacing to C++::
5666 * Linking a Mixed C++ & Ada Program::
5667 * A Simple Example::
5668 * Interfacing with C++ constructors::
5669 * Interfacing with C++ at the Class Level::
5670
5671 @end menu
5672
5673 @node Interfacing to C++,Linking a Mixed C++ & Ada Program,,Building Mixed Ada and C++ Programs
5674 @anchor{gnat_ugn/the_gnat_compilation_model id65}@anchor{a5}@anchor{gnat_ugn/the_gnat_compilation_model id66}@anchor{a6}
5675 @subsubsection Interfacing to C++
5676
5677
5678 GNAT supports interfacing with the G++ compiler (or any C++ compiler
5679 generating code that is compatible with the G++ Application Binary
5680 Interface —see @indicateurl{http://www.codesourcery.com/archives/cxx-abi}).
5681
5682 Interfacing can be done at 3 levels: simple data, subprograms, and
5683 classes. In the first two cases, GNAT offers a specific @code{Convention C_Plus_Plus}
5684 (or @code{CPP}) that behaves exactly like @code{Convention C}.
5685 Usually, C++ mangles the names of subprograms. To generate proper mangled
5686 names automatically, see @ref{a7,,Generating Ada Bindings for C and C++ headers}).
5687 This problem can also be addressed manually in two ways:
5688
5689
5690 @itemize *
5691
5692 @item
5693 by modifying the C++ code in order to force a C convention using
5694 the @code{extern "C"} syntax.
5695
5696 @item
5697 by figuring out the mangled name (using e.g. @code{nm}) and using it as the
5698 Link_Name argument of the pragma import.
5699 @end itemize
5700
5701 Interfacing at the class level can be achieved by using the GNAT specific
5702 pragmas such as @code{CPP_Constructor}. See the @cite{GNAT_Reference_Manual} for additional information.
5703
5704 @node Linking a Mixed C++ & Ada Program,A Simple Example,Interfacing to C++,Building Mixed Ada and C++ Programs
5705 @anchor{gnat_ugn/the_gnat_compilation_model linking-a-mixed-c-ada-program}@anchor{a8}@anchor{gnat_ugn/the_gnat_compilation_model linking-a-mixed-c-and-ada-program}@anchor{a9}
5706 @subsubsection Linking a Mixed C++ & Ada Program
5707
5708
5709 Usually the linker of the C++ development system must be used to link
5710 mixed applications because most C++ systems will resolve elaboration
5711 issues (such as calling constructors on global class instances)
5712 transparently during the link phase. GNAT has been adapted to ease the
5713 use of a foreign linker for the last phase. Three cases can be
5714 considered:
5715
5716
5717 @itemize *
5718
5719 @item
5720 Using GNAT and G++ (GNU C++ compiler) from the same GCC installation:
5721 The C++ linker can simply be called by using the C++ specific driver
5722 called @code{g++}.
5723
5724 Note that if the C++ code uses inline functions, you will need to
5725 compile your C++ code with the @code{-fkeep-inline-functions} switch in
5726 order to provide an existing function implementation that the Ada code can
5727 link with.
5728
5729 @example
5730 $ g++ -c -fkeep-inline-functions file1.C
5731 $ g++ -c -fkeep-inline-functions file2.C
5732 $ gnatmake ada_unit -largs file1.o file2.o --LINK=g++
5733 @end example
5734
5735 @item
5736 Using GNAT and G++ from two different GCC installations: If both
5737 compilers are on the :envvar`PATH`, the previous method may be used. It is
5738 important to note that environment variables such as
5739 @geindex C_INCLUDE_PATH
5740 @geindex environment variable; C_INCLUDE_PATH
5741 @code{C_INCLUDE_PATH},
5742 @geindex GCC_EXEC_PREFIX
5743 @geindex environment variable; GCC_EXEC_PREFIX
5744 @code{GCC_EXEC_PREFIX},
5745 @geindex BINUTILS_ROOT
5746 @geindex environment variable; BINUTILS_ROOT
5747 @code{BINUTILS_ROOT}, and
5748 @geindex GCC_ROOT
5749 @geindex environment variable; GCC_ROOT
5750 @code{GCC_ROOT} will affect both compilers
5751 at the same time and may make one of the two compilers operate
5752 improperly if set during invocation of the wrong compiler. It is also
5753 very important that the linker uses the proper @code{libgcc.a} GCC
5754 library – that is, the one from the C++ compiler installation. The
5755 implicit link command as suggested in the @code{gnatmake} command
5756 from the former example can be replaced by an explicit link command with
5757 the full-verbosity option in order to verify which library is used:
5758
5759 @example
5760 $ gnatbind ada_unit
5761 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=c++
5762 @end example
5763
5764 If there is a problem due to interfering environment variables, it can
5765 be worked around by using an intermediate script. The following example
5766 shows the proper script to use when GNAT has not been installed at its
5767 default location and g++ has been installed at its default location:
5768
5769 @example
5770 $ cat ./my_script
5771 #!/bin/sh
5772 unset BINUTILS_ROOT
5773 unset GCC_ROOT
5774 c++ $*
5775 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=./my_script
5776 @end example
5777
5778 @item
5779 Using a non-GNU C++ compiler: The commands previously described can be
5780 used to insure that the C++ linker is used. Nonetheless, you need to add
5781 a few more parameters to the link command line, depending on the exception
5782 mechanism used.
5783
5784 If the @code{setjmp} / @code{longjmp} exception mechanism is used, only the paths
5785 to the @code{libgcc} libraries are required:
5786
5787 @example
5788 $ cat ./my_script
5789 #!/bin/sh
5790 CC $* gcc -print-file-name=libgcc.a gcc -print-file-name=libgcc_eh.a
5791 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
5792 @end example
5793
5794 where CC is the name of the non-GNU C++ compiler.
5795
5796 If the “zero cost” exception mechanism is used, and the platform
5797 supports automatic registration of exception tables (e.g., Solaris),
5798 paths to more objects are required:
5799
5800 @example
5801 $ cat ./my_script
5802 #!/bin/sh
5803 CC gcc -print-file-name=crtbegin.o $* \\
5804 gcc -print-file-name=libgcc.a gcc -print-file-name=libgcc_eh.a \\
5805 gcc -print-file-name=crtend.o
5806 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
5807 @end example
5808
5809 If the “zero cost exception” mechanism is used, and the platform
5810 doesn’t support automatic registration of exception tables (e.g., HP-UX
5811 or AIX), the simple approach described above will not work and
5812 a pre-linking phase using GNAT will be necessary.
5813 @end itemize
5814
5815 Another alternative is to use the @code{gprbuild} multi-language builder
5816 which has a large knowledge base and knows how to link Ada and C++ code
5817 together automatically in most cases.
5818
5819 @node A Simple Example,Interfacing with C++ constructors,Linking a Mixed C++ & Ada Program,Building Mixed Ada and C++ Programs
5820 @anchor{gnat_ugn/the_gnat_compilation_model a-simple-example}@anchor{aa}@anchor{gnat_ugn/the_gnat_compilation_model id67}@anchor{ab}
5821 @subsubsection A Simple Example
5822
5823
5824 The following example, provided as part of the GNAT examples, shows how
5825 to achieve procedural interfacing between Ada and C++ in both
5826 directions. The C++ class A has two methods. The first method is exported
5827 to Ada by the means of an extern C wrapper function. The second method
5828 calls an Ada subprogram. On the Ada side, the C++ calls are modelled by
5829 a limited record with a layout comparable to the C++ class. The Ada
5830 subprogram, in turn, calls the C++ method. So, starting from the C++
5831 main program, the process passes back and forth between the two
5832 languages.
5833
5834 Here are the compilation commands:
5835
5836 @example
5837 $ gnatmake -c simple_cpp_interface
5838 $ g++ -c cpp_main.C
5839 $ g++ -c ex7.C
5840 $ gnatbind -n simple_cpp_interface
5841 $ gnatlink simple_cpp_interface -o cpp_main --LINK=g++ -lstdc++ ex7.o cpp_main.o
5842 @end example
5843
5844 Here are the corresponding sources:
5845
5846 @example
5847 //cpp_main.C
5848
5849 #include "ex7.h"
5850
5851 extern "C" @{
5852 void adainit (void);
5853 void adafinal (void);
5854 void method1 (A *t);
5855 @}
5856
5857 void method1 (A *t)
5858 @{
5859 t->method1 ();
5860 @}
5861
5862 int main ()
5863 @{
5864 A obj;
5865 adainit ();
5866 obj.method2 (3030);
5867 adafinal ();
5868 @}
5869 @end example
5870
5871 @example
5872 //ex7.h
5873
5874 class Origin @{
5875 public:
5876 int o_value;
5877 @};
5878 class A : public Origin @{
5879 public:
5880 void method1 (void);
5881 void method2 (int v);
5882 A();
5883 int a_value;
5884 @};
5885 @end example
5886
5887 @example
5888 //ex7.C
5889
5890 #include "ex7.h"
5891 #include <stdio.h>
5892
5893 extern "C" @{ void ada_method2 (A *t, int v);@}
5894
5895 void A::method1 (void)
5896 @{
5897 a_value = 2020;
5898 printf ("in A::method1, a_value = %d \\n",a_value);
5899 @}
5900
5901 void A::method2 (int v)
5902 @{
5903 ada_method2 (this, v);
5904 printf ("in A::method2, a_value = %d \\n",a_value);
5905 @}
5906
5907 A::A(void)
5908 @{
5909 a_value = 1010;
5910 printf ("in A::A, a_value = %d \\n",a_value);
5911 @}
5912 @end example
5913
5914 @example
5915 -- simple_cpp_interface.ads
5916 with System;
5917 package Simple_Cpp_Interface is
5918 type A is limited
5919 record
5920 Vptr : System.Address;
5921 O_Value : Integer;
5922 A_Value : Integer;
5923 end record;
5924 pragma Convention (C, A);
5925
5926 procedure Method1 (This : in out A);
5927 pragma Import (C, Method1);
5928
5929 procedure Ada_Method2 (This : in out A; V : Integer);
5930 pragma Export (C, Ada_Method2);
5931
5932 end Simple_Cpp_Interface;
5933 @end example
5934
5935 @example
5936 -- simple_cpp_interface.adb
5937 package body Simple_Cpp_Interface is
5938
5939 procedure Ada_Method2 (This : in out A; V : Integer) is
5940 begin
5941 Method1 (This);
5942 This.A_Value := V;
5943 end Ada_Method2;
5944
5945 end Simple_Cpp_Interface;
5946 @end example
5947
5948 @node Interfacing with C++ constructors,Interfacing with C++ at the Class Level,A Simple Example,Building Mixed Ada and C++ Programs
5949 @anchor{gnat_ugn/the_gnat_compilation_model id68}@anchor{ac}@anchor{gnat_ugn/the_gnat_compilation_model interfacing-with-c-constructors}@anchor{ad}
5950 @subsubsection Interfacing with C++ constructors
5951
5952
5953 In order to interface with C++ constructors GNAT provides the
5954 @code{pragma CPP_Constructor} (see the @cite{GNAT_Reference_Manual}
5955 for additional information).
5956 In this section we present some common uses of C++ constructors
5957 in mixed-languages programs in GNAT.
5958
5959 Let us assume that we need to interface with the following
5960 C++ class:
5961
5962 @example
5963 class Root @{
5964 public:
5965 int a_value;
5966 int b_value;
5967 virtual int Get_Value ();
5968 Root(); // Default constructor
5969 Root(int v); // 1st non-default constructor
5970 Root(int v, int w); // 2nd non-default constructor
5971 @};
5972 @end example
5973
5974 For this purpose we can write the following package spec (further
5975 information on how to build this spec is available in
5976 @ref{ae,,Interfacing with C++ at the Class Level} and
5977 @ref{a7,,Generating Ada Bindings for C and C++ headers}).
5978
5979 @example
5980 with Interfaces.C; use Interfaces.C;
5981 package Pkg_Root is
5982 type Root is tagged limited record
5983 A_Value : int;
5984 B_Value : int;
5985 end record;
5986 pragma Import (CPP, Root);
5987
5988 function Get_Value (Obj : Root) return int;
5989 pragma Import (CPP, Get_Value);
5990
5991 function Constructor return Root;
5992 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ev");
5993
5994 function Constructor (v : Integer) return Root;
5995 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ei");
5996
5997 function Constructor (v, w : Integer) return Root;
5998 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Eii");
5999 end Pkg_Root;
6000 @end example
6001
6002 On the Ada side the constructor is represented by a function (whose
6003 name is arbitrary) that returns the classwide type corresponding to
6004 the imported C++ class. Although the constructor is described as a
6005 function, it is typically a procedure with an extra implicit argument
6006 (the object being initialized) at the implementation level. GNAT
6007 issues the appropriate call, whatever it is, to get the object
6008 properly initialized.
6009
6010 Constructors can only appear in the following contexts:
6011
6012
6013 @itemize *
6014
6015 @item
6016 On the right side of an initialization of an object of type @code{T}.
6017
6018 @item
6019 On the right side of an initialization of a record component of type @code{T}.
6020
6021 @item
6022 In an Ada 2005 limited aggregate.
6023
6024 @item
6025 In an Ada 2005 nested limited aggregate.
6026
6027 @item
6028 In an Ada 2005 limited aggregate that initializes an object built in
6029 place by an extended return statement.
6030 @end itemize
6031
6032 In a declaration of an object whose type is a class imported from C++,
6033 either the default C++ constructor is implicitly called by GNAT, or
6034 else the required C++ constructor must be explicitly called in the
6035 expression that initializes the object. For example:
6036
6037 @example
6038 Obj1 : Root;
6039 Obj2 : Root := Constructor;
6040 Obj3 : Root := Constructor (v => 10);
6041 Obj4 : Root := Constructor (30, 40);
6042 @end example
6043
6044 The first two declarations are equivalent: in both cases the default C++
6045 constructor is invoked (in the former case the call to the constructor is
6046 implicit, and in the latter case the call is explicit in the object
6047 declaration). @code{Obj3} is initialized by the C++ non-default constructor
6048 that takes an integer argument, and @code{Obj4} is initialized by the
6049 non-default C++ constructor that takes two integers.
6050
6051 Let us derive the imported C++ class in the Ada side. For example:
6052
6053 @example
6054 type DT is new Root with record
6055 C_Value : Natural := 2009;
6056 end record;
6057 @end example
6058
6059 In this case the components DT inherited from the C++ side must be
6060 initialized by a C++ constructor, and the additional Ada components
6061 of type DT are initialized by GNAT. The initialization of such an
6062 object is done either by default, or by means of a function returning
6063 an aggregate of type DT, or by means of an extension aggregate.
6064
6065 @example
6066 Obj5 : DT;
6067 Obj6 : DT := Function_Returning_DT (50);
6068 Obj7 : DT := (Constructor (30,40) with C_Value => 50);
6069 @end example
6070
6071 The declaration of @code{Obj5} invokes the default constructors: the
6072 C++ default constructor of the parent type takes care of the initialization
6073 of the components inherited from Root, and GNAT takes care of the default
6074 initialization of the additional Ada components of type DT (that is,
6075 @code{C_Value} is initialized to value 2009). The order of invocation of
6076 the constructors is consistent with the order of elaboration required by
6077 Ada and C++. That is, the constructor of the parent type is always called
6078 before the constructor of the derived type.
6079
6080 Let us now consider a record that has components whose type is imported
6081 from C++. For example:
6082
6083 @example
6084 type Rec1 is limited record
6085 Data1 : Root := Constructor (10);
6086 Value : Natural := 1000;
6087 end record;
6088
6089 type Rec2 (D : Integer := 20) is limited record
6090 Rec : Rec1;
6091 Data2 : Root := Constructor (D, 30);
6092 end record;
6093 @end example
6094
6095 The initialization of an object of type @code{Rec2} will call the
6096 non-default C++ constructors specified for the imported components.
6097 For example:
6098
6099 @example
6100 Obj8 : Rec2 (40);
6101 @end example
6102
6103 Using Ada 2005 we can use limited aggregates to initialize an object
6104 invoking C++ constructors that differ from those specified in the type
6105 declarations. For example:
6106
6107 @example
6108 Obj9 : Rec2 := (Rec => (Data1 => Constructor (15, 16),
6109 others => <>),
6110 others => <>);
6111 @end example
6112
6113 The above declaration uses an Ada 2005 limited aggregate to
6114 initialize @code{Obj9}, and the C++ constructor that has two integer
6115 arguments is invoked to initialize the @code{Data1} component instead
6116 of the constructor specified in the declaration of type @code{Rec1}. In
6117 Ada 2005 the box in the aggregate indicates that unspecified components
6118 are initialized using the expression (if any) available in the component
6119 declaration. That is, in this case discriminant @code{D} is initialized
6120 to value @code{20}, @code{Value} is initialized to value 1000, and the
6121 non-default C++ constructor that handles two integers takes care of
6122 initializing component @code{Data2} with values @code{20,30}.
6123
6124 In Ada 2005 we can use the extended return statement to build the Ada
6125 equivalent to C++ non-default constructors. For example:
6126
6127 @example
6128 function Constructor (V : Integer) return Rec2 is
6129 begin
6130 return Obj : Rec2 := (Rec => (Data1 => Constructor (V, 20),
6131 others => <>),
6132 others => <>) do
6133 -- Further actions required for construction of
6134 -- objects of type Rec2
6135 ...
6136 end record;
6137 end Constructor;
6138 @end example
6139
6140 In this example the extended return statement construct is used to
6141 build in place the returned object whose components are initialized
6142 by means of a limited aggregate. Any further action associated with
6143 the constructor can be placed inside the construct.
6144
6145 @node Interfacing with C++ at the Class Level,,Interfacing with C++ constructors,Building Mixed Ada and C++ Programs
6146 @anchor{gnat_ugn/the_gnat_compilation_model id69}@anchor{af}@anchor{gnat_ugn/the_gnat_compilation_model interfacing-with-c-at-the-class-level}@anchor{ae}
6147 @subsubsection Interfacing with C++ at the Class Level
6148
6149
6150 In this section we demonstrate the GNAT features for interfacing with
6151 C++ by means of an example making use of Ada 2005 abstract interface
6152 types. This example consists of a classification of animals; classes
6153 have been used to model our main classification of animals, and
6154 interfaces provide support for the management of secondary
6155 classifications. We first demonstrate a case in which the types and
6156 constructors are defined on the C++ side and imported from the Ada
6157 side, and latter the reverse case.
6158
6159 The root of our derivation will be the @code{Animal} class, with a
6160 single private attribute (the @code{Age} of the animal), a constructor,
6161 and two public primitives to set and get the value of this attribute.
6162
6163 @example
6164 class Animal @{
6165 public:
6166 virtual void Set_Age (int New_Age);
6167 virtual int Age ();
6168 Animal() @{Age_Count = 0;@};
6169 private:
6170 int Age_Count;
6171 @};
6172 @end example
6173
6174 Abstract interface types are defined in C++ by means of classes with pure
6175 virtual functions and no data members. In our example we will use two
6176 interfaces that provide support for the common management of @code{Carnivore}
6177 and @code{Domestic} animals:
6178
6179 @example
6180 class Carnivore @{
6181 public:
6182 virtual int Number_Of_Teeth () = 0;
6183 @};
6184
6185 class Domestic @{
6186 public:
6187 virtual void Set_Owner (char* Name) = 0;
6188 @};
6189 @end example
6190
6191 Using these declarations, we can now say that a @code{Dog} is an animal that is
6192 both Carnivore and Domestic, that is:
6193
6194 @example
6195 class Dog : Animal, Carnivore, Domestic @{
6196 public:
6197 virtual int Number_Of_Teeth ();
6198 virtual void Set_Owner (char* Name);
6199
6200 Dog(); // Constructor
6201 private:
6202 int Tooth_Count;
6203 char *Owner;
6204 @};
6205 @end example
6206
6207 In the following examples we will assume that the previous declarations are
6208 located in a file named @code{animals.h}. The following package demonstrates
6209 how to import these C++ declarations from the Ada side:
6210
6211 @example
6212 with Interfaces.C.Strings; use Interfaces.C.Strings;
6213 package Animals is
6214 type Carnivore is limited interface;
6215 pragma Convention (C_Plus_Plus, Carnivore);
6216 function Number_Of_Teeth (X : Carnivore)
6217 return Natural is abstract;
6218
6219 type Domestic is limited interface;
6220 pragma Convention (C_Plus_Plus, Domestic);
6221 procedure Set_Owner
6222 (X : in out Domestic;
6223 Name : Chars_Ptr) is abstract;
6224
6225 type Animal is tagged limited record
6226 Age : Natural;
6227 end record;
6228 pragma Import (C_Plus_Plus, Animal);
6229
6230 procedure Set_Age (X : in out Animal; Age : Integer);
6231 pragma Import (C_Plus_Plus, Set_Age);
6232
6233 function Age (X : Animal) return Integer;
6234 pragma Import (C_Plus_Plus, Age);
6235
6236 function New_Animal return Animal;
6237 pragma CPP_Constructor (New_Animal);
6238 pragma Import (CPP, New_Animal, "_ZN6AnimalC1Ev");
6239
6240 type Dog is new Animal and Carnivore and Domestic with record
6241 Tooth_Count : Natural;
6242 Owner : Chars_Ptr;
6243 end record;
6244 pragma Import (C_Plus_Plus, Dog);
6245
6246 function Number_Of_Teeth (A : Dog) return Natural;
6247 pragma Import (C_Plus_Plus, Number_Of_Teeth);
6248
6249 procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
6250 pragma Import (C_Plus_Plus, Set_Owner);
6251
6252 function New_Dog return Dog;
6253 pragma CPP_Constructor (New_Dog);
6254 pragma Import (CPP, New_Dog, "_ZN3DogC2Ev");
6255 end Animals;
6256 @end example
6257
6258 Thanks to the compatibility between GNAT run-time structures and the C++ ABI,
6259 interfacing with these C++ classes is easy. The only requirement is that all
6260 the primitives and components must be declared exactly in the same order in
6261 the two languages.
6262
6263 Regarding the abstract interfaces, we must indicate to the GNAT compiler by
6264 means of a @code{pragma Convention (C_Plus_Plus)}, the convention used to pass
6265 the arguments to the called primitives will be the same as for C++. For the
6266 imported classes we use @code{pragma Import} with convention @code{C_Plus_Plus}
6267 to indicate that they have been defined on the C++ side; this is required
6268 because the dispatch table associated with these tagged types will be built
6269 in the C++ side and therefore will not contain the predefined Ada primitives
6270 which Ada would otherwise expect.
6271
6272 As the reader can see there is no need to indicate the C++ mangled names
6273 associated with each subprogram because it is assumed that all the calls to
6274 these primitives will be dispatching calls. The only exception is the
6275 constructor, which must be registered with the compiler by means of
6276 @code{pragma CPP_Constructor} and needs to provide its associated C++
6277 mangled name because the Ada compiler generates direct calls to it.
6278
6279 With the above packages we can now declare objects of type Dog on the Ada side
6280 and dispatch calls to the corresponding subprograms on the C++ side. We can
6281 also extend the tagged type Dog with further fields and primitives, and
6282 override some of its C++ primitives on the Ada side. For example, here we have
6283 a type derivation defined on the Ada side that inherits all the dispatching
6284 primitives of the ancestor from the C++ side.
6285
6286 @example
6287 with Animals; use Animals;
6288 package Vaccinated_Animals is
6289 type Vaccinated_Dog is new Dog with null record;
6290 function Vaccination_Expired (A : Vaccinated_Dog) return Boolean;
6291 end Vaccinated_Animals;
6292 @end example
6293
6294 It is important to note that, because of the ABI compatibility, the programmer
6295 does not need to add any further information to indicate either the object
6296 layout or the dispatch table entry associated with each dispatching operation.
6297
6298 Now let us define all the types and constructors on the Ada side and export
6299 them to C++, using the same hierarchy of our previous example:
6300
6301 @example
6302 with Interfaces.C.Strings;
6303 use Interfaces.C.Strings;
6304 package Animals is
6305 type Carnivore is limited interface;
6306 pragma Convention (C_Plus_Plus, Carnivore);
6307 function Number_Of_Teeth (X : Carnivore)
6308 return Natural is abstract;
6309
6310 type Domestic is limited interface;
6311 pragma Convention (C_Plus_Plus, Domestic);
6312 procedure Set_Owner
6313 (X : in out Domestic;
6314 Name : Chars_Ptr) is abstract;
6315
6316 type Animal is tagged record
6317 Age : Natural;
6318 end record;
6319 pragma Convention (C_Plus_Plus, Animal);
6320
6321 procedure Set_Age (X : in out Animal; Age : Integer);
6322 pragma Export (C_Plus_Plus, Set_Age);
6323
6324 function Age (X : Animal) return Integer;
6325 pragma Export (C_Plus_Plus, Age);
6326
6327 function New_Animal return Animal'Class;
6328 pragma Export (C_Plus_Plus, New_Animal);
6329
6330 type Dog is new Animal and Carnivore and Domestic with record
6331 Tooth_Count : Natural;
6332 Owner : String (1 .. 30);
6333 end record;
6334 pragma Convention (C_Plus_Plus, Dog);
6335
6336 function Number_Of_Teeth (A : Dog) return Natural;
6337 pragma Export (C_Plus_Plus, Number_Of_Teeth);
6338
6339 procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
6340 pragma Export (C_Plus_Plus, Set_Owner);
6341
6342 function New_Dog return Dog'Class;
6343 pragma Export (C_Plus_Plus, New_Dog);
6344 end Animals;
6345 @end example
6346
6347 Compared with our previous example the only differences are the use of
6348 @code{pragma Convention} (instead of @code{pragma Import}), and the use of
6349 @code{pragma Export} to indicate to the GNAT compiler that the primitives will
6350 be available to C++. Thanks to the ABI compatibility, on the C++ side there is
6351 nothing else to be done; as explained above, the only requirement is that all
6352 the primitives and components are declared in exactly the same order.
6353
6354 For completeness, let us see a brief C++ main program that uses the
6355 declarations available in @code{animals.h} (presented in our first example) to
6356 import and use the declarations from the Ada side, properly initializing and
6357 finalizing the Ada run-time system along the way:
6358
6359 @example
6360 #include "animals.h"
6361 #include <iostream>
6362 using namespace std;
6363
6364 void Check_Carnivore (Carnivore *obj) @{...@}
6365 void Check_Domestic (Domestic *obj) @{...@}
6366 void Check_Animal (Animal *obj) @{...@}
6367 void Check_Dog (Dog *obj) @{...@}
6368
6369 extern "C" @{
6370 void adainit (void);
6371 void adafinal (void);
6372 Dog* new_dog ();
6373 @}
6374
6375 void test ()
6376 @{
6377 Dog *obj = new_dog(); // Ada constructor
6378 Check_Carnivore (obj); // Check secondary DT
6379 Check_Domestic (obj); // Check secondary DT
6380 Check_Animal (obj); // Check primary DT
6381 Check_Dog (obj); // Check primary DT
6382 @}
6383
6384 int main ()
6385 @{
6386 adainit (); test(); adafinal ();
6387 return 0;
6388 @}
6389 @end example
6390
6391 @node Generating Ada Bindings for C and C++ headers,Generating C Headers for Ada Specifications,Building Mixed Ada and C++ Programs,Mixed Language Programming
6392 @anchor{gnat_ugn/the_gnat_compilation_model generating-ada-bindings-for-c-and-c-headers}@anchor{a7}@anchor{gnat_ugn/the_gnat_compilation_model id70}@anchor{b0}
6393 @subsection Generating Ada Bindings for C and C++ headers
6394
6395
6396 @geindex Binding generation (for C and C++ headers)
6397
6398 @geindex C headers (binding generation)
6399
6400 @geindex C++ headers (binding generation)
6401
6402 GNAT includes a binding generator for C and C++ headers which is
6403 intended to do 95% of the tedious work of generating Ada specs from C
6404 or C++ header files.
6405
6406 Note that this capability is not intended to generate 100% correct Ada specs,
6407 and will is some cases require manual adjustments, although it can often
6408 be used out of the box in practice.
6409
6410 Some of the known limitations include:
6411
6412
6413 @itemize *
6414
6415 @item
6416 only very simple character constant macros are translated into Ada
6417 constants. Function macros (macros with arguments) are partially translated
6418 as comments, to be completed manually if needed.
6419
6420 @item
6421 some extensions (e.g. vector types) are not supported
6422
6423 @item
6424 pointers to pointers or complex structures are mapped to System.Address
6425
6426 @item
6427 identifiers with identical name (except casing) will generate compilation
6428 errors (e.g. @code{shm_get} vs @code{SHM_GET}).
6429 @end itemize
6430
6431 The code is generated using Ada 2012 syntax, which makes it easier to interface
6432 with other languages. In most cases you can still use the generated binding
6433 even if your code is compiled using earlier versions of Ada (e.g. @code{-gnat95}).
6434
6435 @menu
6436 * Running the Binding Generator::
6437 * Generating Bindings for C++ Headers::
6438 * Switches::
6439
6440 @end menu
6441
6442 @node Running the Binding Generator,Generating Bindings for C++ Headers,,Generating Ada Bindings for C and C++ headers
6443 @anchor{gnat_ugn/the_gnat_compilation_model id71}@anchor{b1}@anchor{gnat_ugn/the_gnat_compilation_model running-the-binding-generator}@anchor{b2}
6444 @subsubsection Running the Binding Generator
6445
6446
6447 The binding generator is part of the @code{gcc} compiler and can be
6448 invoked via the @code{-fdump-ada-spec} switch, which will generate Ada
6449 spec files for the header files specified on the command line, and all
6450 header files needed by these files transitively. For example:
6451
6452 @example
6453 $ g++ -c -fdump-ada-spec -C /usr/include/time.h
6454 $ gcc -c *.ads
6455 @end example
6456
6457 will generate, under GNU/Linux, the following files: @code{time_h.ads},
6458 @code{bits_time_h.ads}, @code{stddef_h.ads}, @code{bits_types_h.ads} which
6459 correspond to the files @code{/usr/include/time.h},
6460 @code{/usr/include/bits/time.h}, etc…, and will then compile these Ada specs
6461 in Ada 2005 mode.
6462
6463 The @code{-C} switch tells @code{gcc} to extract comments from headers,
6464 and will attempt to generate corresponding Ada comments.
6465
6466 If you want to generate a single Ada file and not the transitive closure, you
6467 can use instead the @code{-fdump-ada-spec-slim} switch.
6468
6469 You can optionally specify a parent unit, of which all generated units will
6470 be children, using @code{-fada-spec-parent=@emph{unit}}.
6471
6472 Note that we recommend when possible to use the @emph{g++} driver to
6473 generate bindings, even for most C headers, since this will in general
6474 generate better Ada specs. For generating bindings for C++ headers, it is
6475 mandatory to use the @emph{g++} command, or @emph{gcc -x c++} which
6476 is equivalent in this case. If @emph{g++} cannot work on your C headers
6477 because of incompatibilities between C and C++, then you can fallback to
6478 @code{gcc} instead.
6479
6480 For an example of better bindings generated from the C++ front-end,
6481 the name of the parameters (when available) are actually ignored by the C
6482 front-end. Consider the following C header:
6483
6484 @example
6485 extern void foo (int variable);
6486 @end example
6487
6488 with the C front-end, @code{variable} is ignored, and the above is handled as:
6489
6490 @example
6491 extern void foo (int);
6492 @end example
6493
6494 generating a generic:
6495
6496 @example
6497 procedure foo (param1 : int);
6498 @end example
6499
6500 with the C++ front-end, the name is available, and we generate:
6501
6502 @example
6503 procedure foo (variable : int);
6504 @end example
6505
6506 In some cases, the generated bindings will be more complete or more meaningful
6507 when defining some macros, which you can do via the @code{-D} switch. This
6508 is for example the case with @code{Xlib.h} under GNU/Linux:
6509
6510 @example
6511 $ g++ -c -fdump-ada-spec -DXLIB_ILLEGAL_ACCESS -C /usr/include/X11/Xlib.h
6512 @end example
6513
6514 The above will generate more complete bindings than a straight call without
6515 the @code{-DXLIB_ILLEGAL_ACCESS} switch.
6516
6517 In other cases, it is not possible to parse a header file in a stand-alone
6518 manner, because other include files need to be included first. In this
6519 case, the solution is to create a small header file including the needed
6520 @code{#include} and possible @code{#define} directives. For example, to
6521 generate Ada bindings for @code{readline/readline.h}, you need to first
6522 include @code{stdio.h}, so you can create a file with the following two
6523 lines in e.g. @code{readline1.h}:
6524
6525 @example
6526 #include <stdio.h>
6527 #include <readline/readline.h>
6528 @end example
6529
6530 and then generate Ada bindings from this file:
6531
6532 @example
6533 $ g++ -c -fdump-ada-spec readline1.h
6534 @end example
6535
6536 @node Generating Bindings for C++ Headers,Switches,Running the Binding Generator,Generating Ada Bindings for C and C++ headers
6537 @anchor{gnat_ugn/the_gnat_compilation_model generating-bindings-for-c-headers}@anchor{b3}@anchor{gnat_ugn/the_gnat_compilation_model id72}@anchor{b4}
6538 @subsubsection Generating Bindings for C++ Headers
6539
6540
6541 Generating bindings for C++ headers is done using the same options, always
6542 with the @emph{g++} compiler. Note that generating Ada spec from C++ headers is a
6543 much more complex job and support for C++ headers is much more limited that
6544 support for C headers. As a result, you will need to modify the resulting
6545 bindings by hand more extensively when using C++ headers.
6546
6547 In this mode, C++ classes will be mapped to Ada tagged types, constructors
6548 will be mapped using the @code{CPP_Constructor} pragma, and when possible,
6549 multiple inheritance of abstract classes will be mapped to Ada interfaces
6550 (see the @emph{Interfacing to C++} section in the @cite{GNAT Reference Manual}
6551 for additional information on interfacing to C++).
6552
6553 For example, given the following C++ header file:
6554
6555 @example
6556 class Carnivore @{
6557 public:
6558 virtual int Number_Of_Teeth () = 0;
6559 @};
6560
6561 class Domestic @{
6562 public:
6563 virtual void Set_Owner (char* Name) = 0;
6564 @};
6565
6566 class Animal @{
6567 public:
6568 int Age_Count;
6569 virtual void Set_Age (int New_Age);
6570 @};
6571
6572 class Dog : Animal, Carnivore, Domestic @{
6573 public:
6574 int Tooth_Count;
6575 char *Owner;
6576
6577 virtual int Number_Of_Teeth ();
6578 virtual void Set_Owner (char* Name);
6579
6580 Dog();
6581 @};
6582 @end example
6583
6584 The corresponding Ada code is generated:
6585
6586 @example
6587 package Class_Carnivore is
6588 type Carnivore is limited interface;
6589 pragma Import (CPP, Carnivore);
6590
6591 function Number_Of_Teeth (this : access Carnivore) return int is abstract;
6592 end;
6593 use Class_Carnivore;
6594
6595 package Class_Domestic is
6596 type Domestic is limited interface;
6597 pragma Import (CPP, Domestic);
6598
6599 procedure Set_Owner
6600 (this : access Domestic;
6601 Name : Interfaces.C.Strings.chars_ptr) is abstract;
6602 end;
6603 use Class_Domestic;
6604
6605 package Class_Animal is
6606 type Animal is tagged limited record
6607 Age_Count : aliased int;
6608 end record;
6609 pragma Import (CPP, Animal);
6610
6611 procedure Set_Age (this : access Animal; New_Age : int);
6612 pragma Import (CPP, Set_Age, "_ZN6Animal7Set_AgeEi");
6613 end;
6614 use Class_Animal;
6615
6616 package Class_Dog is
6617 type Dog is new Animal and Carnivore and Domestic with record
6618 Tooth_Count : aliased int;
6619 Owner : Interfaces.C.Strings.chars_ptr;
6620 end record;
6621 pragma Import (CPP, Dog);
6622
6623 function Number_Of_Teeth (this : access Dog) return int;
6624 pragma Import (CPP, Number_Of_Teeth, "_ZN3Dog15Number_Of_TeethEv");
6625
6626 procedure Set_Owner
6627 (this : access Dog; Name : Interfaces.C.Strings.chars_ptr);
6628 pragma Import (CPP, Set_Owner, "_ZN3Dog9Set_OwnerEPc");
6629
6630 function New_Dog return Dog;
6631 pragma CPP_Constructor (New_Dog);
6632 pragma Import (CPP, New_Dog, "_ZN3DogC1Ev");
6633 end;
6634 use Class_Dog;
6635 @end example
6636
6637 @node Switches,,Generating Bindings for C++ Headers,Generating Ada Bindings for C and C++ headers
6638 @anchor{gnat_ugn/the_gnat_compilation_model switches}@anchor{b5}@anchor{gnat_ugn/the_gnat_compilation_model switches-for-ada-binding-generation}@anchor{b6}
6639 @subsubsection Switches
6640
6641
6642 @geindex -fdump-ada-spec (gcc)
6643
6644
6645 @table @asis
6646
6647 @item @code{-fdump-ada-spec}
6648
6649 Generate Ada spec files for the given header files transitively (including
6650 all header files that these headers depend upon).
6651 @end table
6652
6653 @geindex -fdump-ada-spec-slim (gcc)
6654
6655
6656 @table @asis
6657
6658 @item @code{-fdump-ada-spec-slim}
6659
6660 Generate Ada spec files for the header files specified on the command line
6661 only.
6662 @end table
6663
6664 @geindex -fada-spec-parent (gcc)
6665
6666
6667 @table @asis
6668
6669 @item @code{-fada-spec-parent=@emph{unit}}
6670
6671 Specifies that all files generated by @code{-fdump-ada-spec} are
6672 to be child units of the specified parent unit.
6673 @end table
6674
6675 @geindex -C (gcc)
6676
6677
6678 @table @asis
6679
6680 @item @code{-C}
6681
6682 Extract comments from headers and generate Ada comments in the Ada spec files.
6683 @end table
6684
6685 @node Generating C Headers for Ada Specifications,,Generating Ada Bindings for C and C++ headers,Mixed Language Programming
6686 @anchor{gnat_ugn/the_gnat_compilation_model generating-c-headers-for-ada-specifications}@anchor{b7}@anchor{gnat_ugn/the_gnat_compilation_model id73}@anchor{b8}
6687 @subsection Generating C Headers for Ada Specifications
6688
6689
6690 @geindex Binding generation (for Ada specs)
6691
6692 @geindex C headers (binding generation)
6693
6694 GNAT includes a C header generator for Ada specifications which supports
6695 Ada types that have a direct mapping to C types. This includes in particular
6696 support for:
6697
6698
6699 @itemize *
6700
6701 @item
6702 Scalar types
6703
6704 @item
6705 Constrained arrays
6706
6707 @item
6708 Records (untagged)
6709
6710 @item
6711 Composition of the above types
6712
6713 @item
6714 Constant declarations
6715
6716 @item
6717 Object declarations
6718
6719 @item
6720 Subprogram declarations
6721 @end itemize
6722
6723 @menu
6724 * Running the C Header Generator::
6725
6726 @end menu
6727
6728 @node Running the C Header Generator,,,Generating C Headers for Ada Specifications
6729 @anchor{gnat_ugn/the_gnat_compilation_model running-the-c-header-generator}@anchor{b9}
6730 @subsubsection Running the C Header Generator
6731
6732
6733 The C header generator is part of the GNAT compiler and can be invoked via
6734 the @code{-gnatceg} combination of switches, which will generate a @code{.h}
6735 file corresponding to the given input file (Ada spec or body). Note that
6736 only spec files are processed in any case, so giving a spec or a body file
6737 as input is equivalent. For example:
6738
6739 @example
6740 $ gcc -c -gnatceg pack1.ads
6741 @end example
6742
6743 will generate a self-contained file called @code{pack1.h} including
6744 common definitions from the Ada Standard package, followed by the
6745 definitions included in @code{pack1.ads}, as well as all the other units
6746 withed by this file.
6747
6748 For instance, given the following Ada files:
6749
6750 @example
6751 package Pack2 is
6752 type Int is range 1 .. 10;
6753 end Pack2;
6754 @end example
6755
6756 @example
6757 with Pack2;
6758
6759 package Pack1 is
6760 type Rec is record
6761 Field1, Field2 : Pack2.Int;
6762 end record;
6763
6764 Global : Rec := (1, 2);
6765
6766 procedure Proc1 (R : Rec);
6767 procedure Proc2 (R : in out Rec);
6768 end Pack1;
6769 @end example
6770
6771 The above @code{gcc} command will generate the following @code{pack1.h} file:
6772
6773 @example
6774 /* Standard definitions skipped */
6775 #ifndef PACK2_ADS
6776 #define PACK2_ADS
6777 typedef short_short_integer pack2__TintB;
6778 typedef pack2__TintB pack2__int;
6779 #endif /* PACK2_ADS */
6780
6781 #ifndef PACK1_ADS
6782 #define PACK1_ADS
6783 typedef struct _pack1__rec @{
6784 pack2__int field1;
6785 pack2__int field2;
6786 @} pack1__rec;
6787 extern pack1__rec pack1__global;
6788 extern void pack1__proc1(const pack1__rec r);
6789 extern void pack1__proc2(pack1__rec *r);
6790 #endif /* PACK1_ADS */
6791 @end example
6792
6793 You can then @code{include} @code{pack1.h} from a C source file and use the types,
6794 call subprograms, reference objects, and constants.
6795
6796 @node GNAT and Other Compilation Models,Using GNAT Files with External Tools,Mixed Language Programming,The GNAT Compilation Model
6797 @anchor{gnat_ugn/the_gnat_compilation_model gnat-and-other-compilation-models}@anchor{2d}@anchor{gnat_ugn/the_gnat_compilation_model id74}@anchor{ba}
6798 @section GNAT and Other Compilation Models
6799
6800
6801 This section compares the GNAT model with the approaches taken in
6802 other environents, first the C/C++ model and then the mechanism that
6803 has been used in other Ada systems, in particular those traditionally
6804 used for Ada 83.
6805
6806 @menu
6807 * Comparison between GNAT and C/C++ Compilation Models::
6808 * Comparison between GNAT and Conventional Ada Library Models::
6809
6810 @end menu
6811
6812 @node Comparison between GNAT and C/C++ Compilation Models,Comparison between GNAT and Conventional Ada Library Models,,GNAT and Other Compilation Models
6813 @anchor{gnat_ugn/the_gnat_compilation_model comparison-between-gnat-and-c-c-compilation-models}@anchor{bb}@anchor{gnat_ugn/the_gnat_compilation_model id75}@anchor{bc}
6814 @subsection Comparison between GNAT and C/C++ Compilation Models
6815
6816
6817 The GNAT model of compilation is close to the C and C++ models. You can
6818 think of Ada specs as corresponding to header files in C. As in C, you
6819 don’t need to compile specs; they are compiled when they are used. The
6820 Ada @emph{with} is similar in effect to the @code{#include} of a C
6821 header.
6822
6823 One notable difference is that, in Ada, you may compile specs separately
6824 to check them for semantic and syntactic accuracy. This is not always
6825 possible with C headers because they are fragments of programs that have
6826 less specific syntactic or semantic rules.
6827
6828 The other major difference is the requirement for running the binder,
6829 which performs two important functions. First, it checks for
6830 consistency. In C or C++, the only defense against assembling
6831 inconsistent programs lies outside the compiler, in a makefile, for
6832 example. The binder satisfies the Ada requirement that it be impossible
6833 to construct an inconsistent program when the compiler is used in normal
6834 mode.
6835
6836 @geindex Elaboration order control
6837
6838 The other important function of the binder is to deal with elaboration
6839 issues. There are also elaboration issues in C++ that are handled
6840 automatically. This automatic handling has the advantage of being
6841 simpler to use, but the C++ programmer has no control over elaboration.
6842 Where @code{gnatbind} might complain there was no valid order of
6843 elaboration, a C++ compiler would simply construct a program that
6844 malfunctioned at run time.
6845
6846 @node Comparison between GNAT and Conventional Ada Library Models,,Comparison between GNAT and C/C++ Compilation Models,GNAT and Other Compilation Models
6847 @anchor{gnat_ugn/the_gnat_compilation_model comparison-between-gnat-and-conventional-ada-library-models}@anchor{bd}@anchor{gnat_ugn/the_gnat_compilation_model id76}@anchor{be}
6848 @subsection Comparison between GNAT and Conventional Ada Library Models
6849
6850
6851 This section is intended for Ada programmers who have
6852 used an Ada compiler implementing the traditional Ada library
6853 model, as described in the Ada Reference Manual.
6854
6855 @geindex GNAT library
6856
6857 In GNAT, there is no ‘library’ in the normal sense. Instead, the set of
6858 source files themselves acts as the library. Compiling Ada programs does
6859 not generate any centralized information, but rather an object file and
6860 a ALI file, which are of interest only to the binder and linker.
6861 In a traditional system, the compiler reads information not only from
6862 the source file being compiled, but also from the centralized library.
6863 This means that the effect of a compilation depends on what has been
6864 previously compiled. In particular:
6865
6866
6867 @itemize *
6868
6869 @item
6870 When a unit is @emph{with}ed, the unit seen by the compiler corresponds
6871 to the version of the unit most recently compiled into the library.
6872
6873 @item
6874 Inlining is effective only if the necessary body has already been
6875 compiled into the library.
6876
6877 @item
6878 Compiling a unit may obsolete other units in the library.
6879 @end itemize
6880
6881 In GNAT, compiling one unit never affects the compilation of any other
6882 units because the compiler reads only source files. Only changes to source
6883 files can affect the results of a compilation. In particular:
6884
6885
6886 @itemize *
6887
6888 @item
6889 When a unit is @emph{with}ed, the unit seen by the compiler corresponds
6890 to the source version of the unit that is currently accessible to the
6891 compiler.
6892
6893 @geindex Inlining
6894
6895 @item
6896 Inlining requires the appropriate source files for the package or
6897 subprogram bodies to be available to the compiler. Inlining is always
6898 effective, independent of the order in which units are compiled.
6899
6900 @item
6901 Compiling a unit never affects any other compilations. The editing of
6902 sources may cause previous compilations to be out of date if they
6903 depended on the source file being modified.
6904 @end itemize
6905
6906 The most important result of these differences is that order of compilation
6907 is never significant in GNAT. There is no situation in which one is
6908 required to do one compilation before another. What shows up as order of
6909 compilation requirements in the traditional Ada library becomes, in
6910 GNAT, simple source dependencies; in other words, there is only a set
6911 of rules saying what source files must be present when a file is
6912 compiled.
6913
6914 @node Using GNAT Files with External Tools,,GNAT and Other Compilation Models,The GNAT Compilation Model
6915 @anchor{gnat_ugn/the_gnat_compilation_model id77}@anchor{bf}@anchor{gnat_ugn/the_gnat_compilation_model using-gnat-files-with-external-tools}@anchor{2e}
6916 @section Using GNAT Files with External Tools
6917
6918
6919 This section explains how files that are produced by GNAT may be
6920 used with tools designed for other languages.
6921
6922 @menu
6923 * Using Other Utility Programs with GNAT::
6924 * The External Symbol Naming Scheme of GNAT::
6925
6926 @end menu
6927
6928 @node Using Other Utility Programs with GNAT,The External Symbol Naming Scheme of GNAT,,Using GNAT Files with External Tools
6929 @anchor{gnat_ugn/the_gnat_compilation_model id78}@anchor{c0}@anchor{gnat_ugn/the_gnat_compilation_model using-other-utility-programs-with-gnat}@anchor{c1}
6930 @subsection Using Other Utility Programs with GNAT
6931
6932
6933 The object files generated by GNAT are in standard system format and in
6934 particular the debugging information uses this format. This means
6935 programs generated by GNAT can be used with existing utilities that
6936 depend on these formats.
6937
6938 In general, any utility program that works with C will also often work with
6939 Ada programs generated by GNAT. This includes software utilities such as
6940 gprof (a profiling program), gdb (the FSF debugger), and utilities such
6941 as Purify.
6942
6943 @node The External Symbol Naming Scheme of GNAT,,Using Other Utility Programs with GNAT,Using GNAT Files with External Tools
6944 @anchor{gnat_ugn/the_gnat_compilation_model id79}@anchor{c2}@anchor{gnat_ugn/the_gnat_compilation_model the-external-symbol-naming-scheme-of-gnat}@anchor{c3}
6945 @subsection The External Symbol Naming Scheme of GNAT
6946
6947
6948 In order to interpret the output from GNAT, when using tools that are
6949 originally intended for use with other languages, it is useful to
6950 understand the conventions used to generate link names from the Ada
6951 entity names.
6952
6953 All link names are in all lowercase letters. With the exception of library
6954 procedure names, the mechanism used is simply to use the full expanded
6955 Ada name with dots replaced by double underscores. For example, suppose
6956 we have the following package spec:
6957
6958 @example
6959 package QRS is
6960 MN : Integer;
6961 end QRS;
6962 @end example
6963
6964 @geindex pragma Export
6965
6966 The variable @code{MN} has a full expanded Ada name of @code{QRS.MN}, so
6967 the corresponding link name is @code{qrs__mn}.
6968 Of course if a @code{pragma Export} is used this may be overridden:
6969
6970 @example
6971 package Exports is
6972 Var1 : Integer;
6973 pragma Export (Var1, C, External_Name => "var1_name");
6974 Var2 : Integer;
6975 pragma Export (Var2, C, Link_Name => "var2_link_name");
6976 end Exports;
6977 @end example
6978
6979 In this case, the link name for @code{Var1} is whatever link name the
6980 C compiler would assign for the C function @code{var1_name}. This typically
6981 would be either @code{var1_name} or @code{_var1_name}, depending on operating
6982 system conventions, but other possibilities exist. The link name for
6983 @code{Var2} is @code{var2_link_name}, and this is not operating system
6984 dependent.
6985
6986 One exception occurs for library level procedures. A potential ambiguity
6987 arises between the required name @code{_main} for the C main program,
6988 and the name we would otherwise assign to an Ada library level procedure
6989 called @code{Main} (which might well not be the main program).
6990
6991 To avoid this ambiguity, we attach the prefix @code{_ada_} to such
6992 names. So if we have a library level procedure such as:
6993
6994 @example
6995 procedure Hello (S : String);
6996 @end example
6997
6998 the external name of this procedure will be @code{_ada_hello}.
6999
7000 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
7001
7002 @node Building Executable Programs with GNAT,GNAT Utility Programs,The GNAT Compilation Model,Top
7003 @anchor{gnat_ugn/building_executable_programs_with_gnat doc}@anchor{c4}@anchor{gnat_ugn/building_executable_programs_with_gnat building-executable-programs-with-gnat}@anchor{a}@anchor{gnat_ugn/building_executable_programs_with_gnat id1}@anchor{c5}
7004 @chapter Building Executable Programs with GNAT
7005
7006
7007 This chapter describes first the gnatmake tool
7008 (@ref{c6,,Building with gnatmake}),
7009 which automatically determines the set of sources
7010 needed by an Ada compilation unit and executes the necessary
7011 (re)compilations, binding and linking.
7012 It also explains how to use each tool individually: the
7013 compiler (gcc, see @ref{c7,,Compiling with gcc}),
7014 binder (gnatbind, see @ref{c8,,Binding with gnatbind}),
7015 and linker (gnatlink, see @ref{c9,,Linking with gnatlink})
7016 to build executable programs.
7017 Finally, this chapter provides examples of
7018 how to make use of the general GNU make mechanism
7019 in a GNAT context (see @ref{70,,Using the GNU make Utility}).
7020
7021
7022 @menu
7023 * Building with gnatmake::
7024 * Compiling with gcc::
7025 * Compiler Switches::
7026 * Linker Switches::
7027 * Binding with gnatbind::
7028 * Linking with gnatlink::
7029 * Using the GNU make Utility::
7030
7031 @end menu
7032
7033 @node Building with gnatmake,Compiling with gcc,,Building Executable Programs with GNAT
7034 @anchor{gnat_ugn/building_executable_programs_with_gnat building-with-gnatmake}@anchor{ca}@anchor{gnat_ugn/building_executable_programs_with_gnat the-gnat-make-program-gnatmake}@anchor{c6}
7035 @section Building with @code{gnatmake}
7036
7037
7038 @geindex gnatmake
7039
7040 A typical development cycle when working on an Ada program consists of
7041 the following steps:
7042
7043
7044 @enumerate
7045
7046 @item
7047 Edit some sources to fix bugs;
7048
7049 @item
7050 Add enhancements;
7051
7052 @item
7053 Compile all sources affected;
7054
7055 @item
7056 Rebind and relink; and
7057
7058 @item
7059 Test.
7060 @end enumerate
7061
7062 @geindex Dependency rules (compilation)
7063
7064 The third step in particular can be tricky, because not only do the modified
7065 files have to be compiled, but any files depending on these files must also be
7066 recompiled. The dependency rules in Ada can be quite complex, especially
7067 in the presence of overloading, @code{use} clauses, generics and inlined
7068 subprograms.
7069
7070 @code{gnatmake} automatically takes care of the third and fourth steps
7071 of this process. It determines which sources need to be compiled,
7072 compiles them, and binds and links the resulting object files.
7073
7074 Unlike some other Ada make programs, the dependencies are always
7075 accurately recomputed from the new sources. The source based approach of
7076 the GNAT compilation model makes this possible. This means that if
7077 changes to the source program cause corresponding changes in
7078 dependencies, they will always be tracked exactly correctly by
7079 @code{gnatmake}.
7080
7081 Note that for advanced forms of project structure, we recommend creating
7082 a project file as explained in the @emph{GNAT_Project_Manager} chapter in the
7083 @emph{GPRbuild User’s Guide}, and using the
7084 @code{gprbuild} tool which supports building with project files and works similarly
7085 to @code{gnatmake}.
7086
7087 @menu
7088 * Running gnatmake::
7089 * Switches for gnatmake::
7090 * Mode Switches for gnatmake::
7091 * Notes on the Command Line::
7092 * How gnatmake Works::
7093 * Examples of gnatmake Usage::
7094
7095 @end menu
7096
7097 @node Running gnatmake,Switches for gnatmake,,Building with gnatmake
7098 @anchor{gnat_ugn/building_executable_programs_with_gnat id2}@anchor{cb}@anchor{gnat_ugn/building_executable_programs_with_gnat running-gnatmake}@anchor{cc}
7099 @subsection Running @code{gnatmake}
7100
7101
7102 The usual form of the @code{gnatmake} command is
7103
7104 @example
7105 $ gnatmake [<switches>] <file_name> [<file_names>] [<mode_switches>]
7106 @end example
7107
7108 The only required argument is one @code{file_name}, which specifies
7109 a compilation unit that is a main program. Several @code{file_names} can be
7110 specified: this will result in several executables being built.
7111 If @code{switches} are present, they can be placed before the first
7112 @code{file_name}, between @code{file_names} or after the last @code{file_name}.
7113 If @code{mode_switches} are present, they must always be placed after
7114 the last @code{file_name} and all @code{switches}.
7115
7116 If you are using standard file extensions (@code{.adb} and
7117 @code{.ads}), then the
7118 extension may be omitted from the @code{file_name} arguments. However, if
7119 you are using non-standard extensions, then it is required that the
7120 extension be given. A relative or absolute directory path can be
7121 specified in a @code{file_name}, in which case, the input source file will
7122 be searched for in the specified directory only. Otherwise, the input
7123 source file will first be searched in the directory where
7124 @code{gnatmake} was invoked and if it is not found, it will be search on
7125 the source path of the compiler as described in
7126 @ref{73,,Search Paths and the Run-Time Library (RTL)}.
7127
7128 All @code{gnatmake} output (except when you specify @code{-M}) is sent to
7129 @code{stderr}. The output produced by the
7130 @code{-M} switch is sent to @code{stdout}.
7131
7132 @node Switches for gnatmake,Mode Switches for gnatmake,Running gnatmake,Building with gnatmake
7133 @anchor{gnat_ugn/building_executable_programs_with_gnat id3}@anchor{cd}@anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gnatmake}@anchor{ce}
7134 @subsection Switches for @code{gnatmake}
7135
7136
7137 You may specify any of the following switches to @code{gnatmake}:
7138
7139 @geindex --version (gnatmake)
7140
7141
7142 @table @asis
7143
7144 @item @code{--version}
7145
7146 Display Copyright and version, then exit disregarding all other options.
7147 @end table
7148
7149 @geindex --help (gnatmake)
7150
7151
7152 @table @asis
7153
7154 @item @code{--help}
7155
7156 If @code{--version} was not used, display usage, then exit disregarding
7157 all other options.
7158 @end table
7159
7160 @geindex --GCC=compiler_name (gnatmake)
7161
7162
7163 @table @asis
7164
7165 @item @code{--GCC=@emph{compiler_name}}
7166
7167 Program used for compiling. The default is @code{gcc}. You need to use
7168 quotes around @code{compiler_name} if @code{compiler_name} contains
7169 spaces or other separator characters.
7170 As an example @code{--GCC="foo -x -y"}
7171 will instruct @code{gnatmake} to use @code{foo -x -y} as your
7172 compiler. A limitation of this syntax is that the name and path name of
7173 the executable itself must not include any embedded spaces. Note that
7174 switch @code{-c} is always inserted after your command name. Thus in the
7175 above example the compiler command that will be used by @code{gnatmake}
7176 will be @code{foo -c -x -y}. If several @code{--GCC=compiler_name} are
7177 used, only the last @code{compiler_name} is taken into account. However,
7178 all the additional switches are also taken into account. Thus,
7179 @code{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
7180 @code{--GCC="bar -x -y -z -t"}.
7181 @end table
7182
7183 @geindex --GNATBIND=binder_name (gnatmake)
7184
7185
7186 @table @asis
7187
7188 @item @code{--GNATBIND=@emph{binder_name}}
7189
7190 Program used for binding. The default is @code{gnatbind}. You need to
7191 use quotes around @code{binder_name} if @code{binder_name} contains spaces
7192 or other separator characters.
7193 As an example @code{--GNATBIND="bar -x -y"}
7194 will instruct @code{gnatmake} to use @code{bar -x -y} as your
7195 binder. Binder switches that are normally appended by @code{gnatmake}
7196 to @code{gnatbind} are now appended to the end of @code{bar -x -y}.
7197 A limitation of this syntax is that the name and path name of the executable
7198 itself must not include any embedded spaces.
7199 @end table
7200
7201 @geindex --GNATLINK=linker_name (gnatmake)
7202
7203
7204 @table @asis
7205
7206 @item @code{--GNATLINK=@emph{linker_name}}
7207
7208 Program used for linking. The default is @code{gnatlink}. You need to
7209 use quotes around @code{linker_name} if @code{linker_name} contains spaces
7210 or other separator characters.
7211 As an example @code{--GNATLINK="lan -x -y"}
7212 will instruct @code{gnatmake} to use @code{lan -x -y} as your
7213 linker. Linker switches that are normally appended by @code{gnatmake} to
7214 @code{gnatlink} are now appended to the end of @code{lan -x -y}.
7215 A limitation of this syntax is that the name and path name of the executable
7216 itself must not include any embedded spaces.
7217
7218 @item @code{--create-map-file}
7219
7220 When linking an executable, create a map file. The name of the map file
7221 has the same name as the executable with extension “.map”.
7222
7223 @item @code{--create-map-file=@emph{mapfile}}
7224
7225 When linking an executable, create a map file with the specified name.
7226 @end table
7227
7228 @geindex --create-missing-dirs (gnatmake)
7229
7230
7231 @table @asis
7232
7233 @item @code{--create-missing-dirs}
7234
7235 When using project files (@code{-P@emph{project}}), automatically create
7236 missing object directories, library directories and exec
7237 directories.
7238
7239 @item @code{--single-compile-per-obj-dir}
7240
7241 Disallow simultaneous compilations in the same object directory when
7242 project files are used.
7243
7244 @item @code{--subdirs=@emph{subdir}}
7245
7246 Actual object directory of each project file is the subdirectory subdir of the
7247 object directory specified or defaulted in the project file.
7248
7249 @item @code{--unchecked-shared-lib-imports}
7250
7251 By default, shared library projects are not allowed to import static library
7252 projects. When this switch is used on the command line, this restriction is
7253 relaxed.
7254
7255 @item @code{--source-info=@emph{source info file}}
7256
7257 Specify a source info file. This switch is active only when project files
7258 are used. If the source info file is specified as a relative path, then it is
7259 relative to the object directory of the main project. If the source info file
7260 does not exist, then after the Project Manager has successfully parsed and
7261 processed the project files and found the sources, it creates the source info
7262 file. If the source info file already exists and can be read successfully,
7263 then the Project Manager will get all the needed information about the sources
7264 from the source info file and will not look for them. This reduces the time
7265 to process the project files, especially when looking for sources that take a
7266 long time. If the source info file exists but cannot be parsed successfully,
7267 the Project Manager will attempt to recreate it. If the Project Manager fails
7268 to create the source info file, a message is issued, but gnatmake does not
7269 fail. @code{gnatmake} “trusts” the source info file. This means that
7270 if the source files have changed (addition, deletion, moving to a different
7271 source directory), then the source info file need to be deleted and recreated.
7272 @end table
7273
7274 @geindex -a (gnatmake)
7275
7276
7277 @table @asis
7278
7279 @item @code{-a}
7280
7281 Consider all files in the make process, even the GNAT internal system
7282 files (for example, the predefined Ada library files), as well as any
7283 locked files. Locked files are files whose ALI file is write-protected.
7284 By default,
7285 @code{gnatmake} does not check these files,
7286 because the assumption is that the GNAT internal files are properly up
7287 to date, and also that any write protected ALI files have been properly
7288 installed. Note that if there is an installation problem, such that one
7289 of these files is not up to date, it will be properly caught by the
7290 binder.
7291 You may have to specify this switch if you are working on GNAT
7292 itself. The switch @code{-a} is also useful
7293 in conjunction with @code{-f}
7294 if you need to recompile an entire application,
7295 including run-time files, using special configuration pragmas,
7296 such as a @code{Normalize_Scalars} pragma.
7297
7298 By default
7299 @code{gnatmake -a} compiles all GNAT
7300 internal files with
7301 @code{gcc -c -gnatpg} rather than @code{gcc -c}.
7302 @end table
7303
7304 @geindex -b (gnatmake)
7305
7306
7307 @table @asis
7308
7309 @item @code{-b}
7310
7311 Bind only. Can be combined with @code{-c} to do
7312 compilation and binding, but no link.
7313 Can be combined with @code{-l}
7314 to do binding and linking. When not combined with
7315 @code{-c}
7316 all the units in the closure of the main program must have been previously
7317 compiled and must be up to date. The root unit specified by @code{file_name}
7318 may be given without extension, with the source extension or, if no GNAT
7319 Project File is specified, with the ALI file extension.
7320 @end table
7321
7322 @geindex -c (gnatmake)
7323
7324
7325 @table @asis
7326
7327 @item @code{-c}
7328
7329 Compile only. Do not perform binding, except when @code{-b}
7330 is also specified. Do not perform linking, except if both
7331 @code{-b} and
7332 @code{-l} are also specified.
7333 If the root unit specified by @code{file_name} is not a main unit, this is the
7334 default. Otherwise @code{gnatmake} will attempt binding and linking
7335 unless all objects are up to date and the executable is more recent than
7336 the objects.
7337 @end table
7338
7339 @geindex -C (gnatmake)
7340
7341
7342 @table @asis
7343
7344 @item @code{-C}
7345
7346 Use a temporary mapping file. A mapping file is a way to communicate
7347 to the compiler two mappings: from unit names to file names (without
7348 any directory information) and from file names to path names (with
7349 full directory information). A mapping file can make the compiler’s
7350 file searches faster, especially if there are many source directories,
7351 or the sources are read over a slow network connection. If
7352 @code{-P} is used, a mapping file is always used, so
7353 @code{-C} is unnecessary; in this case the mapping file
7354 is initially populated based on the project file. If
7355 @code{-C} is used without
7356 @code{-P},
7357 the mapping file is initially empty. Each invocation of the compiler
7358 will add any newly accessed sources to the mapping file.
7359 @end table
7360
7361 @geindex -C= (gnatmake)
7362
7363
7364 @table @asis
7365
7366 @item @code{-C=@emph{file}}
7367
7368 Use a specific mapping file. The file, specified as a path name (absolute or
7369 relative) by this switch, should already exist, otherwise the switch is
7370 ineffective. The specified mapping file will be communicated to the compiler.
7371 This switch is not compatible with a project file
7372 (-P`file`) or with multiple compiling processes
7373 (-jnnn, when nnn is greater than 1).
7374 @end table
7375
7376 @geindex -d (gnatmake)
7377
7378
7379 @table @asis
7380
7381 @item @code{-d}
7382
7383 Display progress for each source, up to date or not, as a single line:
7384
7385 @example
7386 completed x out of y (zz%)
7387 @end example
7388
7389 If the file needs to be compiled this is displayed after the invocation of
7390 the compiler. These lines are displayed even in quiet output mode.
7391 @end table
7392
7393 @geindex -D (gnatmake)
7394
7395
7396 @table @asis
7397
7398 @item @code{-D @emph{dir}}
7399
7400 Put all object files and ALI file in directory @code{dir}.
7401 If the @code{-D} switch is not used, all object files
7402 and ALI files go in the current working directory.
7403
7404 This switch cannot be used when using a project file.
7405 @end table
7406
7407 @geindex -eI (gnatmake)
7408
7409
7410 @table @asis
7411
7412 @item @code{-eI@emph{nnn}}
7413
7414 Indicates that the main source is a multi-unit source and the rank of the unit
7415 in the source file is nnn. nnn needs to be a positive number and a valid
7416 index in the source. This switch cannot be used when @code{gnatmake} is
7417 invoked for several mains.
7418 @end table
7419
7420 @geindex -eL (gnatmake)
7421
7422 @geindex symbolic links
7423
7424
7425 @table @asis
7426
7427 @item @code{-eL}
7428
7429 Follow all symbolic links when processing project files.
7430 This should be used if your project uses symbolic links for files or
7431 directories, but is not needed in other cases.
7432
7433 @geindex naming scheme
7434
7435 This also assumes that no directory matches the naming scheme for files (for
7436 instance that you do not have a directory called “sources.ads” when using the
7437 default GNAT naming scheme).
7438
7439 When you do not have to use this switch (i.e., by default), gnatmake is able to
7440 save a lot of system calls (several per source file and object file), which
7441 can result in a significant speed up to load and manipulate a project file,
7442 especially when using source files from a remote system.
7443 @end table
7444
7445 @geindex -eS (gnatmake)
7446
7447
7448 @table @asis
7449
7450 @item @code{-eS}
7451
7452 Output the commands for the compiler, the binder and the linker
7453 on standard output,
7454 instead of standard error.
7455 @end table
7456
7457 @geindex -f (gnatmake)
7458
7459
7460 @table @asis
7461
7462 @item @code{-f}
7463
7464 Force recompilations. Recompile all sources, even though some object
7465 files may be up to date, but don’t recompile predefined or GNAT internal
7466 files or locked files (files with a write-protected ALI file),
7467 unless the @code{-a} switch is also specified.
7468 @end table
7469
7470 @geindex -F (gnatmake)
7471
7472
7473 @table @asis
7474
7475 @item @code{-F}
7476
7477 When using project files, if some errors or warnings are detected during
7478 parsing and verbose mode is not in effect (no use of switch
7479 -v), then error lines start with the full path name of the project
7480 file, rather than its simple file name.
7481 @end table
7482
7483 @geindex -g (gnatmake)
7484
7485
7486 @table @asis
7487
7488 @item @code{-g}
7489
7490 Enable debugging. This switch is simply passed to the compiler and to the
7491 linker.
7492 @end table
7493
7494 @geindex -i (gnatmake)
7495
7496
7497 @table @asis
7498
7499 @item @code{-i}
7500
7501 In normal mode, @code{gnatmake} compiles all object files and ALI files
7502 into the current directory. If the @code{-i} switch is used,
7503 then instead object files and ALI files that already exist are overwritten
7504 in place. This means that once a large project is organized into separate
7505 directories in the desired manner, then @code{gnatmake} will automatically
7506 maintain and update this organization. If no ALI files are found on the
7507 Ada object path (see @ref{73,,Search Paths and the Run-Time Library (RTL)}),
7508 the new object and ALI files are created in the
7509 directory containing the source being compiled. If another organization
7510 is desired, where objects and sources are kept in different directories,
7511 a useful technique is to create dummy ALI files in the desired directories.
7512 When detecting such a dummy file, @code{gnatmake} will be forced to
7513 recompile the corresponding source file, and it will be put the resulting
7514 object and ALI files in the directory where it found the dummy file.
7515 @end table
7516
7517 @geindex -j (gnatmake)
7518
7519 @geindex Parallel make
7520
7521
7522 @table @asis
7523
7524 @item @code{-j@emph{n}}
7525
7526 Use @code{n} processes to carry out the (re)compilations. On a multiprocessor
7527 machine compilations will occur in parallel. If @code{n} is 0, then the
7528 maximum number of parallel compilations is the number of core processors
7529 on the platform. In the event of compilation errors, messages from various
7530 compilations might get interspersed (but @code{gnatmake} will give you the
7531 full ordered list of failing compiles at the end). If this is problematic,
7532 rerun the make process with n set to 1 to get a clean list of messages.
7533 @end table
7534
7535 @geindex -k (gnatmake)
7536
7537
7538 @table @asis
7539
7540 @item @code{-k}
7541
7542 Keep going. Continue as much as possible after a compilation error. To
7543 ease the programmer’s task in case of compilation errors, the list of
7544 sources for which the compile fails is given when @code{gnatmake}
7545 terminates.
7546
7547 If @code{gnatmake} is invoked with several @code{file_names} and with this
7548 switch, if there are compilation errors when building an executable,
7549 @code{gnatmake} will not attempt to build the following executables.
7550 @end table
7551
7552 @geindex -l (gnatmake)
7553
7554
7555 @table @asis
7556
7557 @item @code{-l}
7558
7559 Link only. Can be combined with @code{-b} to binding
7560 and linking. Linking will not be performed if combined with
7561 @code{-c}
7562 but not with @code{-b}.
7563 When not combined with @code{-b}
7564 all the units in the closure of the main program must have been previously
7565 compiled and must be up to date, and the main program needs to have been bound.
7566 The root unit specified by @code{file_name}
7567 may be given without extension, with the source extension or, if no GNAT
7568 Project File is specified, with the ALI file extension.
7569 @end table
7570
7571 @geindex -m (gnatmake)
7572
7573
7574 @table @asis
7575
7576 @item @code{-m}
7577
7578 Specify that the minimum necessary amount of recompilations
7579 be performed. In this mode @code{gnatmake} ignores time
7580 stamp differences when the only
7581 modifications to a source file consist in adding/removing comments,
7582 empty lines, spaces or tabs. This means that if you have changed the
7583 comments in a source file or have simply reformatted it, using this
7584 switch will tell @code{gnatmake} not to recompile files that depend on it
7585 (provided other sources on which these files depend have undergone no
7586 semantic modifications). Note that the debugging information may be
7587 out of date with respect to the sources if the @code{-m} switch causes
7588 a compilation to be switched, so the use of this switch represents a
7589 trade-off between compilation time and accurate debugging information.
7590 @end table
7591
7592 @geindex Dependencies
7593 @geindex producing list
7594
7595 @geindex -M (gnatmake)
7596
7597
7598 @table @asis
7599
7600 @item @code{-M}
7601
7602 Check if all objects are up to date. If they are, output the object
7603 dependences to @code{stdout} in a form that can be directly exploited in
7604 a @code{Makefile}. By default, each source file is prefixed with its
7605 (relative or absolute) directory name. This name is whatever you
7606 specified in the various @code{-aI}
7607 and @code{-I} switches. If you use
7608 @code{gnatmake -M} @code{-q}
7609 (see below), only the source file names,
7610 without relative paths, are output. If you just specify the @code{-M}
7611 switch, dependencies of the GNAT internal system files are omitted. This
7612 is typically what you want. If you also specify
7613 the @code{-a} switch,
7614 dependencies of the GNAT internal files are also listed. Note that
7615 dependencies of the objects in external Ada libraries (see
7616 switch @code{-aL@emph{dir}} in the following list)
7617 are never reported.
7618 @end table
7619
7620 @geindex -n (gnatmake)
7621
7622
7623 @table @asis
7624
7625 @item @code{-n}
7626
7627 Don’t compile, bind, or link. Checks if all objects are up to date.
7628 If they are not, the full name of the first file that needs to be
7629 recompiled is printed.
7630 Repeated use of this option, followed by compiling the indicated source
7631 file, will eventually result in recompiling all required units.
7632 @end table
7633
7634 @geindex -o (gnatmake)
7635
7636
7637 @table @asis
7638
7639 @item @code{-o @emph{exec_name}}
7640
7641 Output executable name. The name of the final executable program will be
7642 @code{exec_name}. If the @code{-o} switch is omitted the default
7643 name for the executable will be the name of the input file in appropriate form
7644 for an executable file on the host system.
7645
7646 This switch cannot be used when invoking @code{gnatmake} with several
7647 @code{file_names}.
7648 @end table
7649
7650 @geindex -p (gnatmake)
7651
7652
7653 @table @asis
7654
7655 @item @code{-p}
7656
7657 Same as @code{--create-missing-dirs}
7658 @end table
7659
7660 @geindex -P (gnatmake)
7661
7662
7663 @table @asis
7664
7665 @item @code{-P@emph{project}}
7666
7667 Use project file @code{project}. Only one such switch can be used.
7668 @end table
7669
7670 @c -- Comment:
7671 @c :ref:`gnatmake_and_Project_Files`.
7672
7673 @geindex -q (gnatmake)
7674
7675
7676 @table @asis
7677
7678 @item @code{-q}
7679
7680 Quiet. When this flag is not set, the commands carried out by
7681 @code{gnatmake} are displayed.
7682 @end table
7683
7684 @geindex -s (gnatmake)
7685
7686
7687 @table @asis
7688
7689 @item @code{-s}
7690
7691 Recompile if compiler switches have changed since last compilation.
7692 All compiler switches but -I and -o are taken into account in the
7693 following way:
7694 orders between different ‘first letter’ switches are ignored, but
7695 orders between same switches are taken into account. For example,
7696 @code{-O -O2} is different than @code{-O2 -O}, but @code{-g -O}
7697 is equivalent to @code{-O -g}.
7698
7699 This switch is recommended when Integrated Preprocessing is used.
7700 @end table
7701
7702 @geindex -u (gnatmake)
7703
7704
7705 @table @asis
7706
7707 @item @code{-u}
7708
7709 Unique. Recompile at most the main files. It implies -c. Combined with
7710 -f, it is equivalent to calling the compiler directly. Note that using
7711 -u with a project file and no main has a special meaning.
7712 @end table
7713
7714 @c --Comment
7715 @c (See :ref:`Project_Files_and_Main_Subprograms`.)
7716
7717 @geindex -U (gnatmake)
7718
7719
7720 @table @asis
7721
7722 @item @code{-U}
7723
7724 When used without a project file or with one or several mains on the command
7725 line, is equivalent to -u. When used with a project file and no main
7726 on the command line, all sources of all project files are checked and compiled
7727 if not up to date, and libraries are rebuilt, if necessary.
7728 @end table
7729
7730 @geindex -v (gnatmake)
7731
7732
7733 @table @asis
7734
7735 @item @code{-v}
7736
7737 Verbose. Display the reason for all recompilations @code{gnatmake}
7738 decides are necessary, with the highest verbosity level.
7739 @end table
7740
7741 @geindex -vl (gnatmake)
7742
7743
7744 @table @asis
7745
7746 @item @code{-vl}
7747
7748 Verbosity level Low. Display fewer lines than in verbosity Medium.
7749 @end table
7750
7751 @geindex -vm (gnatmake)
7752
7753
7754 @table @asis
7755
7756 @item @code{-vm}
7757
7758 Verbosity level Medium. Potentially display fewer lines than in verbosity High.
7759 @end table
7760
7761 @geindex -vm (gnatmake)
7762
7763
7764 @table @asis
7765
7766 @item @code{-vh}
7767
7768 Verbosity level High. Equivalent to -v.
7769
7770 @item @code{-vP@emph{x}}
7771
7772 Indicate the verbosity of the parsing of GNAT project files.
7773 See @ref{cf,,Switches Related to Project Files}.
7774 @end table
7775
7776 @geindex -x (gnatmake)
7777
7778
7779 @table @asis
7780
7781 @item @code{-x}
7782
7783 Indicate that sources that are not part of any Project File may be compiled.
7784 Normally, when using Project Files, only sources that are part of a Project
7785 File may be compile. When this switch is used, a source outside of all Project
7786 Files may be compiled. The ALI file and the object file will be put in the
7787 object directory of the main Project. The compilation switches used will only
7788 be those specified on the command line. Even when
7789 @code{-x} is used, mains specified on the
7790 command line need to be sources of a project file.
7791
7792 @item @code{-X@emph{name}=@emph{value}}
7793
7794 Indicate that external variable @code{name} has the value @code{value}.
7795 The Project Manager will use this value for occurrences of
7796 @code{external(name)} when parsing the project file.
7797 @ref{cf,,Switches Related to Project Files}.
7798 @end table
7799
7800 @geindex -z (gnatmake)
7801
7802
7803 @table @asis
7804
7805 @item @code{-z}
7806
7807 No main subprogram. Bind and link the program even if the unit name
7808 given on the command line is a package name. The resulting executable
7809 will execute the elaboration routines of the package and its closure,
7810 then the finalization routines.
7811 @end table
7812
7813 @subsubheading GCC switches
7814
7815
7816 Any uppercase or multi-character switch that is not a @code{gnatmake} switch
7817 is passed to @code{gcc} (e.g., @code{-O}, @code{-gnato,} etc.)
7818
7819 @subsubheading Source and library search path switches
7820
7821
7822 @geindex -aI (gnatmake)
7823
7824
7825 @table @asis
7826
7827 @item @code{-aI@emph{dir}}
7828
7829 When looking for source files also look in directory @code{dir}.
7830 The order in which source files search is undertaken is
7831 described in @ref{73,,Search Paths and the Run-Time Library (RTL)}.
7832 @end table
7833
7834 @geindex -aL (gnatmake)
7835
7836
7837 @table @asis
7838
7839 @item @code{-aL@emph{dir}}
7840
7841 Consider @code{dir} as being an externally provided Ada library.
7842 Instructs @code{gnatmake} to skip compilation units whose @code{.ALI}
7843 files have been located in directory @code{dir}. This allows you to have
7844 missing bodies for the units in @code{dir} and to ignore out of date bodies
7845 for the same units. You still need to specify
7846 the location of the specs for these units by using the switches
7847 @code{-aI@emph{dir}} or @code{-I@emph{dir}}.
7848 Note: this switch is provided for compatibility with previous versions
7849 of @code{gnatmake}. The easier method of causing standard libraries
7850 to be excluded from consideration is to write-protect the corresponding
7851 ALI files.
7852 @end table
7853
7854 @geindex -aO (gnatmake)
7855
7856
7857 @table @asis
7858
7859 @item @code{-aO@emph{dir}}
7860
7861 When searching for library and object files, look in directory
7862 @code{dir}. The order in which library files are searched is described in
7863 @ref{76,,Search Paths for gnatbind}.
7864 @end table
7865
7866 @geindex Search paths
7867 @geindex for gnatmake
7868
7869 @geindex -A (gnatmake)
7870
7871
7872 @table @asis
7873
7874 @item @code{-A@emph{dir}}
7875
7876 Equivalent to @code{-aL@emph{dir}} @code{-aI@emph{dir}}.
7877
7878 @geindex -I (gnatmake)
7879
7880 @item @code{-I@emph{dir}}
7881
7882 Equivalent to @code{-aO@emph{dir} -aI@emph{dir}}.
7883 @end table
7884
7885 @geindex -I- (gnatmake)
7886
7887 @geindex Source files
7888 @geindex suppressing search
7889
7890
7891 @table @asis
7892
7893 @item @code{-I-}
7894
7895 Do not look for source files in the directory containing the source
7896 file named in the command line.
7897 Do not look for ALI or object files in the directory
7898 where @code{gnatmake} was invoked.
7899 @end table
7900
7901 @geindex -L (gnatmake)
7902
7903 @geindex Linker libraries
7904
7905
7906 @table @asis
7907
7908 @item @code{-L@emph{dir}}
7909
7910 Add directory @code{dir} to the list of directories in which the linker
7911 will search for libraries. This is equivalent to
7912 @code{-largs} @code{-L@emph{dir}}.
7913 Furthermore, under Windows, the sources pointed to by the libraries path
7914 set in the registry are not searched for.
7915 @end table
7916
7917 @geindex -nostdinc (gnatmake)
7918
7919
7920 @table @asis
7921
7922 @item @code{-nostdinc}
7923
7924 Do not look for source files in the system default directory.
7925 @end table
7926
7927 @geindex -nostdlib (gnatmake)
7928
7929
7930 @table @asis
7931
7932 @item @code{-nostdlib}
7933
7934 Do not look for library files in the system default directory.
7935 @end table
7936
7937 @geindex --RTS (gnatmake)
7938
7939
7940 @table @asis
7941
7942 @item @code{--RTS=@emph{rts-path}}
7943
7944 Specifies the default location of the run-time library. GNAT looks for the
7945 run-time
7946 in the following directories, and stops as soon as a valid run-time is found
7947 (@code{adainclude} or @code{ada_source_path}, and @code{adalib} or
7948 @code{ada_object_path} present):
7949
7950
7951 @itemize *
7952
7953 @item
7954 @emph{<current directory>/$rts_path}
7955
7956 @item
7957 @emph{<default-search-dir>/$rts_path}
7958
7959 @item
7960 @emph{<default-search-dir>/rts-$rts_path}
7961
7962 @item
7963 The selected path is handled like a normal RTS path.
7964 @end itemize
7965 @end table
7966
7967 @node Mode Switches for gnatmake,Notes on the Command Line,Switches for gnatmake,Building with gnatmake
7968 @anchor{gnat_ugn/building_executable_programs_with_gnat id4}@anchor{d0}@anchor{gnat_ugn/building_executable_programs_with_gnat mode-switches-for-gnatmake}@anchor{d1}
7969 @subsection Mode Switches for @code{gnatmake}
7970
7971
7972 The mode switches (referred to as @code{mode_switches}) allow the
7973 inclusion of switches that are to be passed to the compiler itself, the
7974 binder or the linker. The effect of a mode switch is to cause all
7975 subsequent switches up to the end of the switch list, or up to the next
7976 mode switch, to be interpreted as switches to be passed on to the
7977 designated component of GNAT.
7978
7979 @geindex -cargs (gnatmake)
7980
7981
7982 @table @asis
7983
7984 @item @code{-cargs @emph{switches}}
7985
7986 Compiler switches. Here @code{switches} is a list of switches
7987 that are valid switches for @code{gcc}. They will be passed on to
7988 all compile steps performed by @code{gnatmake}.
7989 @end table
7990
7991 @geindex -bargs (gnatmake)
7992
7993
7994 @table @asis
7995
7996 @item @code{-bargs @emph{switches}}
7997
7998 Binder switches. Here @code{switches} is a list of switches
7999 that are valid switches for @code{gnatbind}. They will be passed on to
8000 all bind steps performed by @code{gnatmake}.
8001 @end table
8002
8003 @geindex -largs (gnatmake)
8004
8005
8006 @table @asis
8007
8008 @item @code{-largs @emph{switches}}
8009
8010 Linker switches. Here @code{switches} is a list of switches
8011 that are valid switches for @code{gnatlink}. They will be passed on to
8012 all link steps performed by @code{gnatmake}.
8013 @end table
8014
8015 @geindex -margs (gnatmake)
8016
8017
8018 @table @asis
8019
8020 @item @code{-margs @emph{switches}}
8021
8022 Make switches. The switches are directly interpreted by @code{gnatmake},
8023 regardless of any previous occurrence of @code{-cargs}, @code{-bargs}
8024 or @code{-largs}.
8025 @end table
8026
8027 @node Notes on the Command Line,How gnatmake Works,Mode Switches for gnatmake,Building with gnatmake
8028 @anchor{gnat_ugn/building_executable_programs_with_gnat id5}@anchor{d2}@anchor{gnat_ugn/building_executable_programs_with_gnat notes-on-the-command-line}@anchor{d3}
8029 @subsection Notes on the Command Line
8030
8031
8032 This section contains some additional useful notes on the operation
8033 of the @code{gnatmake} command.
8034
8035 @geindex Recompilation (by gnatmake)
8036
8037
8038 @itemize *
8039
8040 @item
8041 If @code{gnatmake} finds no ALI files, it recompiles the main program
8042 and all other units required by the main program.
8043 This means that @code{gnatmake}
8044 can be used for the initial compile, as well as during subsequent steps of
8045 the development cycle.
8046
8047 @item
8048 If you enter @code{gnatmake foo.adb}, where @code{foo}
8049 is a subunit or body of a generic unit, @code{gnatmake} recompiles
8050 @code{foo.adb} (because it finds no ALI) and stops, issuing a
8051 warning.
8052
8053 @item
8054 In @code{gnatmake} the switch @code{-I}
8055 is used to specify both source and
8056 library file paths. Use @code{-aI}
8057 instead if you just want to specify
8058 source paths only and @code{-aO}
8059 if you want to specify library paths
8060 only.
8061
8062 @item
8063 @code{gnatmake} will ignore any files whose ALI file is write-protected.
8064 This may conveniently be used to exclude standard libraries from
8065 consideration and in particular it means that the use of the
8066 @code{-f} switch will not recompile these files
8067 unless @code{-a} is also specified.
8068
8069 @item
8070 @code{gnatmake} has been designed to make the use of Ada libraries
8071 particularly convenient. Assume you have an Ada library organized
8072 as follows: @emph{obj-dir} contains the objects and ALI files for
8073 of your Ada compilation units,
8074 whereas @emph{include-dir} contains the
8075 specs of these units, but no bodies. Then to compile a unit
8076 stored in @code{main.adb}, which uses this Ada library you would just type:
8077
8078 @example
8079 $ gnatmake -aI`include-dir` -aL`obj-dir` main
8080 @end example
8081
8082 @item
8083 Using @code{gnatmake} along with the @code{-m (minimal recompilation)}
8084 switch provides a mechanism for avoiding unnecessary recompilations. Using
8085 this switch,
8086 you can update the comments/format of your
8087 source files without having to recompile everything. Note, however, that
8088 adding or deleting lines in a source files may render its debugging
8089 info obsolete. If the file in question is a spec, the impact is rather
8090 limited, as that debugging info will only be useful during the
8091 elaboration phase of your program. For bodies the impact can be more
8092 significant. In all events, your debugger will warn you if a source file
8093 is more recent than the corresponding object, and alert you to the fact
8094 that the debugging information may be out of date.
8095 @end itemize
8096
8097 @node How gnatmake Works,Examples of gnatmake Usage,Notes on the Command Line,Building with gnatmake
8098 @anchor{gnat_ugn/building_executable_programs_with_gnat how-gnatmake-works}@anchor{d4}@anchor{gnat_ugn/building_executable_programs_with_gnat id6}@anchor{d5}
8099 @subsection How @code{gnatmake} Works
8100
8101
8102 Generally @code{gnatmake} automatically performs all necessary
8103 recompilations and you don’t need to worry about how it works. However,
8104 it may be useful to have some basic understanding of the @code{gnatmake}
8105 approach and in particular to understand how it uses the results of
8106 previous compilations without incorrectly depending on them.
8107
8108 First a definition: an object file is considered @emph{up to date} if the
8109 corresponding ALI file exists and if all the source files listed in the
8110 dependency section of this ALI file have time stamps matching those in
8111 the ALI file. This means that neither the source file itself nor any
8112 files that it depends on have been modified, and hence there is no need
8113 to recompile this file.
8114
8115 @code{gnatmake} works by first checking if the specified main unit is up
8116 to date. If so, no compilations are required for the main unit. If not,
8117 @code{gnatmake} compiles the main program to build a new ALI file that
8118 reflects the latest sources. Then the ALI file of the main unit is
8119 examined to find all the source files on which the main program depends,
8120 and @code{gnatmake} recursively applies the above procedure on all these
8121 files.
8122
8123 This process ensures that @code{gnatmake} only trusts the dependencies
8124 in an existing ALI file if they are known to be correct. Otherwise it
8125 always recompiles to determine a new, guaranteed accurate set of
8126 dependencies. As a result the program is compiled ‘upside down’ from what may
8127 be more familiar as the required order of compilation in some other Ada
8128 systems. In particular, clients are compiled before the units on which
8129 they depend. The ability of GNAT to compile in any order is critical in
8130 allowing an order of compilation to be chosen that guarantees that
8131 @code{gnatmake} will recompute a correct set of new dependencies if
8132 necessary.
8133
8134 When invoking @code{gnatmake} with several @code{file_names}, if a unit is
8135 imported by several of the executables, it will be recompiled at most once.
8136
8137 Note: when using non-standard naming conventions
8138 (@ref{1c,,Using Other File Names}), changing through a configuration pragmas
8139 file the version of a source and invoking @code{gnatmake} to recompile may
8140 have no effect, if the previous version of the source is still accessible
8141 by @code{gnatmake}. It may be necessary to use the switch
8142 -f.
8143
8144 @node Examples of gnatmake Usage,,How gnatmake Works,Building with gnatmake
8145 @anchor{gnat_ugn/building_executable_programs_with_gnat examples-of-gnatmake-usage}@anchor{d6}@anchor{gnat_ugn/building_executable_programs_with_gnat id7}@anchor{d7}
8146 @subsection Examples of @code{gnatmake} Usage
8147
8148
8149
8150 @table @asis
8151
8152 @item @emph{gnatmake hello.adb}
8153
8154 Compile all files necessary to bind and link the main program
8155 @code{hello.adb} (containing unit @code{Hello}) and bind and link the
8156 resulting object files to generate an executable file @code{hello}.
8157
8158 @item @emph{gnatmake main1 main2 main3}
8159
8160 Compile all files necessary to bind and link the main programs
8161 @code{main1.adb} (containing unit @code{Main1}), @code{main2.adb}
8162 (containing unit @code{Main2}) and @code{main3.adb}
8163 (containing unit @code{Main3}) and bind and link the resulting object files
8164 to generate three executable files @code{main1},
8165 @code{main2} and @code{main3}.
8166
8167 @item @emph{gnatmake -q Main_Unit -cargs -O2 -bargs -l}
8168
8169 Compile all files necessary to bind and link the main program unit
8170 @code{Main_Unit} (from file @code{main_unit.adb}). All compilations will
8171 be done with optimization level 2 and the order of elaboration will be
8172 listed by the binder. @code{gnatmake} will operate in quiet mode, not
8173 displaying commands it is executing.
8174 @end table
8175
8176 @node Compiling with gcc,Compiler Switches,Building with gnatmake,Building Executable Programs with GNAT
8177 @anchor{gnat_ugn/building_executable_programs_with_gnat compiling-with-gcc}@anchor{c7}@anchor{gnat_ugn/building_executable_programs_with_gnat id8}@anchor{d8}
8178 @section Compiling with @code{gcc}
8179
8180
8181 This section discusses how to compile Ada programs using the @code{gcc}
8182 command. It also describes the set of switches
8183 that can be used to control the behavior of the compiler.
8184
8185 @menu
8186 * Compiling Programs::
8187 * Search Paths and the Run-Time Library (RTL): Search Paths and the Run-Time Library RTL.
8188 * Order of Compilation Issues::
8189 * Examples::
8190
8191 @end menu
8192
8193 @node Compiling Programs,Search Paths and the Run-Time Library RTL,,Compiling with gcc
8194 @anchor{gnat_ugn/building_executable_programs_with_gnat compiling-programs}@anchor{d9}@anchor{gnat_ugn/building_executable_programs_with_gnat id9}@anchor{da}
8195 @subsection Compiling Programs
8196
8197
8198 The first step in creating an executable program is to compile the units
8199 of the program using the @code{gcc} command. You must compile the
8200 following files:
8201
8202
8203 @itemize *
8204
8205 @item
8206 the body file (@code{.adb}) for a library level subprogram or generic
8207 subprogram
8208
8209 @item
8210 the spec file (@code{.ads}) for a library level package or generic
8211 package that has no body
8212
8213 @item
8214 the body file (@code{.adb}) for a library level package
8215 or generic package that has a body
8216 @end itemize
8217
8218 You need @emph{not} compile the following files
8219
8220
8221 @itemize *
8222
8223 @item
8224 the spec of a library unit which has a body
8225
8226 @item
8227 subunits
8228 @end itemize
8229
8230 because they are compiled as part of compiling related units. GNAT
8231 package specs
8232 when the corresponding body is compiled, and subunits when the parent is
8233 compiled.
8234
8235 @geindex cannot generate code
8236
8237 If you attempt to compile any of these files, you will get one of the
8238 following error messages (where @code{fff} is the name of the file you
8239 compiled):
8240
8241 @quotation
8242
8243 @example
8244 cannot generate code for file `@w{`}fff`@w{`} (package spec)
8245 to check package spec, use -gnatc
8246
8247 cannot generate code for file `@w{`}fff`@w{`} (missing subunits)
8248 to check parent unit, use -gnatc
8249
8250 cannot generate code for file `@w{`}fff`@w{`} (subprogram spec)
8251 to check subprogram spec, use -gnatc
8252
8253 cannot generate code for file `@w{`}fff`@w{`} (subunit)
8254 to check subunit, use -gnatc
8255 @end example
8256 @end quotation
8257
8258 As indicated by the above error messages, if you want to submit
8259 one of these files to the compiler to check for correct semantics
8260 without generating code, then use the @code{-gnatc} switch.
8261
8262 The basic command for compiling a file containing an Ada unit is:
8263
8264 @example
8265 $ gcc -c [switches] <file name>
8266 @end example
8267
8268 where @code{file name} is the name of the Ada file (usually
8269 having an extension @code{.ads} for a spec or @code{.adb} for a body).
8270 You specify the
8271 @code{-c} switch to tell @code{gcc} to compile, but not link, the file.
8272 The result of a successful compilation is an object file, which has the
8273 same name as the source file but an extension of @code{.o} and an Ada
8274 Library Information (ALI) file, which also has the same name as the
8275 source file, but with @code{.ali} as the extension. GNAT creates these
8276 two output files in the current directory, but you may specify a source
8277 file in any directory using an absolute or relative path specification
8278 containing the directory information.
8279
8280 TESTING: the @code{--foobar@emph{NN}} switch
8281
8282 @geindex gnat1
8283
8284 @code{gcc} is actually a driver program that looks at the extensions of
8285 the file arguments and loads the appropriate compiler. For example, the
8286 GNU C compiler is @code{cc1}, and the Ada compiler is @code{gnat1}.
8287 These programs are in directories known to the driver program (in some
8288 configurations via environment variables you set), but need not be in
8289 your path. The @code{gcc} driver also calls the assembler and any other
8290 utilities needed to complete the generation of the required object
8291 files.
8292
8293 It is possible to supply several file names on the same @code{gcc}
8294 command. This causes @code{gcc} to call the appropriate compiler for
8295 each file. For example, the following command lists two separate
8296 files to be compiled:
8297
8298 @example
8299 $ gcc -c x.adb y.adb
8300 @end example
8301
8302 calls @code{gnat1} (the Ada compiler) twice to compile @code{x.adb} and
8303 @code{y.adb}.
8304 The compiler generates two object files @code{x.o} and @code{y.o}
8305 and the two ALI files @code{x.ali} and @code{y.ali}.
8306
8307 Any switches apply to all the files listed, see @ref{db,,Compiler Switches} for a
8308 list of available @code{gcc} switches.
8309
8310 @node Search Paths and the Run-Time Library RTL,Order of Compilation Issues,Compiling Programs,Compiling with gcc
8311 @anchor{gnat_ugn/building_executable_programs_with_gnat id10}@anchor{dc}@anchor{gnat_ugn/building_executable_programs_with_gnat search-paths-and-the-run-time-library-rtl}@anchor{73}
8312 @subsection Search Paths and the Run-Time Library (RTL)
8313
8314
8315 With the GNAT source-based library system, the compiler must be able to
8316 find source files for units that are needed by the unit being compiled.
8317 Search paths are used to guide this process.
8318
8319 The compiler compiles one source file whose name must be given
8320 explicitly on the command line. In other words, no searching is done
8321 for this file. To find all other source files that are needed (the most
8322 common being the specs of units), the compiler examines the following
8323 directories, in the following order:
8324
8325
8326 @itemize *
8327
8328 @item
8329 The directory containing the source file of the main unit being compiled
8330 (the file name on the command line).
8331
8332 @item
8333 Each directory named by an @code{-I} switch given on the @code{gcc}
8334 command line, in the order given.
8335
8336 @geindex ADA_PRJ_INCLUDE_FILE
8337
8338 @item
8339 Each of the directories listed in the text file whose name is given
8340 by the
8341 @geindex ADA_PRJ_INCLUDE_FILE
8342 @geindex environment variable; ADA_PRJ_INCLUDE_FILE
8343 @code{ADA_PRJ_INCLUDE_FILE} environment variable.
8344 @geindex ADA_PRJ_INCLUDE_FILE
8345 @geindex environment variable; ADA_PRJ_INCLUDE_FILE
8346 @code{ADA_PRJ_INCLUDE_FILE} is normally set by gnatmake or by the gnat
8347 driver when project files are used. It should not normally be set
8348 by other means.
8349
8350 @geindex ADA_INCLUDE_PATH
8351
8352 @item
8353 Each of the directories listed in the value of the
8354 @geindex ADA_INCLUDE_PATH
8355 @geindex environment variable; ADA_INCLUDE_PATH
8356 @code{ADA_INCLUDE_PATH} environment variable.
8357 Construct this value
8358 exactly as the
8359 @geindex PATH
8360 @geindex environment variable; PATH
8361 @code{PATH} environment variable: a list of directory
8362 names separated by colons (semicolons when working with the NT version).
8363
8364 @item
8365 The content of the @code{ada_source_path} file which is part of the GNAT
8366 installation tree and is used to store standard libraries such as the
8367 GNAT Run Time Library (RTL) source files.
8368 @ref{72,,Installing a library}
8369 @end itemize
8370
8371 Specifying the switch @code{-I-}
8372 inhibits the use of the directory
8373 containing the source file named in the command line. You can still
8374 have this directory on your search path, but in this case it must be
8375 explicitly requested with a @code{-I} switch.
8376
8377 Specifying the switch @code{-nostdinc}
8378 inhibits the search of the default location for the GNAT Run Time
8379 Library (RTL) source files.
8380
8381 The compiler outputs its object files and ALI files in the current
8382 working directory.
8383 Caution: The object file can be redirected with the @code{-o} switch;
8384 however, @code{gcc} and @code{gnat1} have not been coordinated on this
8385 so the @code{ALI} file will not go to the right place. Therefore, you should
8386 avoid using the @code{-o} switch.
8387
8388 @geindex System.IO
8389
8390 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
8391 children make up the GNAT RTL, together with the simple @code{System.IO}
8392 package used in the @code{"Hello World"} example. The sources for these units
8393 are needed by the compiler and are kept together in one directory. Not
8394 all of the bodies are needed, but all of the sources are kept together
8395 anyway. In a normal installation, you need not specify these directory
8396 names when compiling or binding. Either the environment variables or
8397 the built-in defaults cause these files to be found.
8398
8399 In addition to the language-defined hierarchies (@code{System}, @code{Ada} and
8400 @code{Interfaces}), the GNAT distribution provides a fourth hierarchy,
8401 consisting of child units of @code{GNAT}. This is a collection of generally
8402 useful types, subprograms, etc. See the @cite{GNAT_Reference_Manual}
8403 for further details.
8404
8405 Besides simplifying access to the RTL, a major use of search paths is
8406 in compiling sources from multiple directories. This can make
8407 development environments much more flexible.
8408
8409 @node Order of Compilation Issues,Examples,Search Paths and the Run-Time Library RTL,Compiling with gcc
8410 @anchor{gnat_ugn/building_executable_programs_with_gnat id11}@anchor{dd}@anchor{gnat_ugn/building_executable_programs_with_gnat order-of-compilation-issues}@anchor{de}
8411 @subsection Order of Compilation Issues
8412
8413
8414 If, in our earlier example, there was a spec for the @code{hello}
8415 procedure, it would be contained in the file @code{hello.ads}; yet this
8416 file would not have to be explicitly compiled. This is the result of the
8417 model we chose to implement library management. Some of the consequences
8418 of this model are as follows:
8419
8420
8421 @itemize *
8422
8423 @item
8424 There is no point in compiling specs (except for package
8425 specs with no bodies) because these are compiled as needed by clients. If
8426 you attempt a useless compilation, you will receive an error message.
8427 It is also useless to compile subunits because they are compiled as needed
8428 by the parent.
8429
8430 @item
8431 There are no order of compilation requirements: performing a
8432 compilation never obsoletes anything. The only way you can obsolete
8433 something and require recompilations is to modify one of the
8434 source files on which it depends.
8435
8436 @item
8437 There is no library as such, apart from the ALI files
8438 (@ref{28,,The Ada Library Information Files}, for information on the format
8439 of these files). For now we find it convenient to create separate ALI files,
8440 but eventually the information therein may be incorporated into the object
8441 file directly.
8442
8443 @item
8444 When you compile a unit, the source files for the specs of all units
8445 that it @emph{with}s, all its subunits, and the bodies of any generics it
8446 instantiates must be available (reachable by the search-paths mechanism
8447 described above), or you will receive a fatal error message.
8448 @end itemize
8449
8450 @node Examples,,Order of Compilation Issues,Compiling with gcc
8451 @anchor{gnat_ugn/building_executable_programs_with_gnat examples}@anchor{df}@anchor{gnat_ugn/building_executable_programs_with_gnat id12}@anchor{e0}
8452 @subsection Examples
8453
8454
8455 The following are some typical Ada compilation command line examples:
8456
8457 @example
8458 $ gcc -c xyz.adb
8459 @end example
8460
8461 Compile body in file @code{xyz.adb} with all default options.
8462
8463 @example
8464 $ gcc -c -O2 -gnata xyz-def.adb
8465 @end example
8466
8467 Compile the child unit package in file @code{xyz-def.adb} with extensive
8468 optimizations, and pragma @code{Assert}/@cite{Debug} statements
8469 enabled.
8470
8471 @example
8472 $ gcc -c -gnatc abc-def.adb
8473 @end example
8474
8475 Compile the subunit in file @code{abc-def.adb} in semantic-checking-only
8476 mode.
8477
8478 @node Compiler Switches,Linker Switches,Compiling with gcc,Building Executable Programs with GNAT
8479 @anchor{gnat_ugn/building_executable_programs_with_gnat compiler-switches}@anchor{e1}@anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gcc}@anchor{db}
8480 @section Compiler Switches
8481
8482
8483 The @code{gcc} command accepts switches that control the
8484 compilation process. These switches are fully described in this section:
8485 first an alphabetical listing of all switches with a brief description,
8486 and then functionally grouped sets of switches with more detailed
8487 information.
8488
8489 More switches exist for GCC than those documented here, especially
8490 for specific targets. However, their use is not recommended as
8491 they may change code generation in ways that are incompatible with
8492 the Ada run-time library, or can cause inconsistencies between
8493 compilation units.
8494
8495 @menu
8496 * Alphabetical List of All Switches::
8497 * Output and Error Message Control::
8498 * Warning Message Control::
8499 * Debugging and Assertion Control::
8500 * Validity Checking::
8501 * Style Checking::
8502 * Run-Time Checks::
8503 * Using gcc for Syntax Checking::
8504 * Using gcc for Semantic Checking::
8505 * Compiling Different Versions of Ada::
8506 * Character Set Control::
8507 * File Naming Control::
8508 * Subprogram Inlining Control::
8509 * Auxiliary Output Control::
8510 * Debugging Control::
8511 * Exception Handling Control::
8512 * Units to Sources Mapping Files::
8513 * Code Generation Control::
8514
8515 @end menu
8516
8517 @node Alphabetical List of All Switches,Output and Error Message Control,,Compiler Switches
8518 @anchor{gnat_ugn/building_executable_programs_with_gnat alphabetical-list-of-all-switches}@anchor{e2}@anchor{gnat_ugn/building_executable_programs_with_gnat id13}@anchor{e3}
8519 @subsection Alphabetical List of All Switches
8520
8521
8522 @geindex -b (gcc)
8523
8524
8525 @table @asis
8526
8527 @item @code{-b @emph{target}}
8528
8529 Compile your program to run on @code{target}, which is the name of a
8530 system configuration. You must have a GNAT cross-compiler built if
8531 @code{target} is not the same as your host system.
8532 @end table
8533
8534 @geindex -B (gcc)
8535
8536
8537 @table @asis
8538
8539 @item @code{-B@emph{dir}}
8540
8541 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
8542 from @code{dir} instead of the default location. Only use this switch
8543 when multiple versions of the GNAT compiler are available.
8544 See the “Options for Directory Search” section in the
8545 @cite{Using the GNU Compiler Collection (GCC)} manual for further details.
8546 You would normally use the @code{-b} or @code{-V} switch instead.
8547 @end table
8548
8549 @geindex -c (gcc)
8550
8551
8552 @table @asis
8553
8554 @item @code{-c}
8555
8556 Compile. Always use this switch when compiling Ada programs.
8557
8558 Note: for some other languages when using @code{gcc}, notably in
8559 the case of C and C++, it is possible to use
8560 use @code{gcc} without a @code{-c} switch to
8561 compile and link in one step. In the case of GNAT, you
8562 cannot use this approach, because the binder must be run
8563 and @code{gcc} cannot be used to run the GNAT binder.
8564 @end table
8565
8566 @geindex -fcallgraph-info (gcc)
8567
8568
8569 @table @asis
8570
8571 @item @code{-fcallgraph-info[=su,da]}
8572
8573 Makes the compiler output callgraph information for the program, on a
8574 per-file basis. The information is generated in the VCG format. It can
8575 be decorated with additional, per-node and/or per-edge information, if a
8576 list of comma-separated markers is additionally specified. When the
8577 @code{su} marker is specified, the callgraph is decorated with stack usage
8578 information; it is equivalent to @code{-fstack-usage}. When the @code{da}
8579 marker is specified, the callgraph is decorated with information about
8580 dynamically allocated objects.
8581 @end table
8582
8583 @geindex -fdiagnostics-format (gcc)
8584
8585
8586 @table @asis
8587
8588 @item @code{-fdiagnostics-format=json}
8589
8590 Makes GNAT emit warning and error messages as JSON. Inhibits printing of
8591 text warning and errors messages except if @code{-gnatv} or
8592 @code{-gnatl} are present.
8593 @end table
8594
8595 @geindex -fdump-scos (gcc)
8596
8597
8598 @table @asis
8599
8600 @item @code{-fdump-scos}
8601
8602 Generates SCO (Source Coverage Obligation) information in the ALI file.
8603 This information is used by advanced coverage tools. See unit @code{SCOs}
8604 in the compiler sources for details in files @code{scos.ads} and
8605 @code{scos.adb}.
8606 @end table
8607
8608 @geindex -fgnat-encodings (gcc)
8609
8610
8611 @table @asis
8612
8613 @item @code{-fgnat-encodings=[all|gdb|minimal]}
8614
8615 This switch controls the balance between GNAT encodings and standard DWARF
8616 emitted in the debug information.
8617 @end table
8618
8619 @geindex -flto (gcc)
8620
8621
8622 @table @asis
8623
8624 @item @code{-flto[=@emph{n}]}
8625
8626 Enables Link Time Optimization. This switch must be used in conjunction
8627 with the @code{-Ox} switches (but not with the @code{-gnatn} switch
8628 since it is a full replacement for the latter) and instructs the compiler
8629 to defer most optimizations until the link stage. The advantage of this
8630 approach is that the compiler can do a whole-program analysis and choose
8631 the best interprocedural optimization strategy based on a complete view
8632 of the program, instead of a fragmentary view with the usual approach.
8633 This can also speed up the compilation of big programs and reduce the
8634 size of the executable, compared with a traditional per-unit compilation
8635 with inlining across units enabled by the @code{-gnatn} switch.
8636 The drawback of this approach is that it may require more memory and that
8637 the debugging information generated by -g with it might be hardly usable.
8638 The switch, as well as the accompanying @code{-Ox} switches, must be
8639 specified both for the compilation and the link phases.
8640 If the @code{n} parameter is specified, the optimization and final code
8641 generation at link time are executed using @code{n} parallel jobs by
8642 means of an installed @code{make} program.
8643 @end table
8644
8645 @geindex -fno-inline (gcc)
8646
8647
8648 @table @asis
8649
8650 @item @code{-fno-inline}
8651
8652 Suppresses all inlining, unless requested with pragma @code{Inline_Always}. The
8653 effect is enforced regardless of other optimization or inlining switches.
8654 Note that inlining can also be suppressed on a finer-grained basis with
8655 pragma @code{No_Inline}.
8656 @end table
8657
8658 @geindex -fno-inline-functions (gcc)
8659
8660
8661 @table @asis
8662
8663 @item @code{-fno-inline-functions}
8664
8665 Suppresses automatic inlining of subprograms, which is enabled
8666 if @code{-O3} is used.
8667 @end table
8668
8669 @geindex -fno-inline-small-functions (gcc)
8670
8671
8672 @table @asis
8673
8674 @item @code{-fno-inline-small-functions}
8675
8676 Suppresses automatic inlining of small subprograms, which is enabled
8677 if @code{-O2} is used.
8678 @end table
8679
8680 @geindex -fno-inline-functions-called-once (gcc)
8681
8682
8683 @table @asis
8684
8685 @item @code{-fno-inline-functions-called-once}
8686
8687 Suppresses inlining of subprograms local to the unit and called once
8688 from within it, which is enabled if @code{-O1} is used.
8689 @end table
8690
8691 @geindex -fno-ivopts (gcc)
8692
8693
8694 @table @asis
8695
8696 @item @code{-fno-ivopts}
8697
8698 Suppresses high-level loop induction variable optimizations, which are
8699 enabled if @code{-O1} is used. These optimizations are generally
8700 profitable but, for some specific cases of loops with numerous uses
8701 of the iteration variable that follow a common pattern, they may end
8702 up destroying the regularity that could be exploited at a lower level
8703 and thus producing inferior code.
8704 @end table
8705
8706 @geindex -fno-strict-aliasing (gcc)
8707
8708
8709 @table @asis
8710
8711 @item @code{-fno-strict-aliasing}
8712
8713 Causes the compiler to avoid assumptions regarding non-aliasing
8714 of objects of different types. See
8715 @ref{e4,,Optimization and Strict Aliasing} for details.
8716 @end table
8717
8718 @geindex -fno-strict-overflow (gcc)
8719
8720
8721 @table @asis
8722
8723 @item @code{-fno-strict-overflow}
8724
8725 Causes the compiler to avoid assumptions regarding the rules of signed
8726 integer overflow. These rules specify that signed integer overflow will
8727 result in a Constraint_Error exception at run time and are enforced in
8728 default mode by the compiler, so this switch should not be necessary in
8729 normal operating mode. It might be useful in conjunction with @code{-gnato0}
8730 for very peculiar cases of low-level programming.
8731 @end table
8732
8733 @geindex -fstack-check (gcc)
8734
8735
8736 @table @asis
8737
8738 @item @code{-fstack-check}
8739
8740 Activates stack checking.
8741 See @ref{e5,,Stack Overflow Checking} for details.
8742 @end table
8743
8744 @geindex -fstack-usage (gcc)
8745
8746
8747 @table @asis
8748
8749 @item @code{-fstack-usage}
8750
8751 Makes the compiler output stack usage information for the program, on a
8752 per-subprogram basis. See @ref{e6,,Static Stack Usage Analysis} for details.
8753 @end table
8754
8755 @geindex -g (gcc)
8756
8757
8758 @table @asis
8759
8760 @item @code{-g}
8761
8762 Generate debugging information. This information is stored in the object
8763 file and copied from there to the final executable file by the linker,
8764 where it can be read by the debugger. You must use the
8765 @code{-g} switch if you plan on using the debugger.
8766 @end table
8767
8768 @geindex -gnat05 (gcc)
8769
8770
8771 @table @asis
8772
8773 @item @code{-gnat05}
8774
8775 Allow full Ada 2005 features.
8776 @end table
8777
8778 @geindex -gnat12 (gcc)
8779
8780
8781 @table @asis
8782
8783 @item @code{-gnat12}
8784
8785 Allow full Ada 2012 features.
8786 @end table
8787
8788 @geindex -gnat83 (gcc)
8789
8790 @geindex -gnat2005 (gcc)
8791
8792
8793 @table @asis
8794
8795 @item @code{-gnat2005}
8796
8797 Allow full Ada 2005 features (same as @code{-gnat05})
8798 @end table
8799
8800 @geindex -gnat2012 (gcc)
8801
8802
8803 @table @asis
8804
8805 @item @code{-gnat2012}
8806
8807 Allow full Ada 2012 features (same as @code{-gnat12})
8808 @end table
8809
8810 @geindex -gnat2022 (gcc)
8811
8812
8813 @table @asis
8814
8815 @item @code{-gnat2022}
8816
8817 Allow full Ada 2022 features
8818
8819 @item @code{-gnat83}
8820
8821 Enforce Ada 83 restrictions.
8822 @end table
8823
8824 @geindex -gnat95 (gcc)
8825
8826
8827 @table @asis
8828
8829 @item @code{-gnat95}
8830
8831 Enforce Ada 95 restrictions.
8832
8833 Note: for compatibility with some Ada 95 compilers which support only
8834 the @code{overriding} keyword of Ada 2005, the @code{-gnatd.D} switch can
8835 be used along with @code{-gnat95} to achieve a similar effect with GNAT.
8836
8837 @code{-gnatd.D} instructs GNAT to consider @code{overriding} as a keyword
8838 and handle its associated semantic checks, even in Ada 95 mode.
8839 @end table
8840
8841 @geindex -gnata (gcc)
8842
8843
8844 @table @asis
8845
8846 @item @code{-gnata}
8847
8848 Assertions enabled. @code{Pragma Assert} and @code{pragma Debug} to be
8849 activated. Note that these pragmas can also be controlled using the
8850 configuration pragmas @code{Assertion_Policy} and @code{Debug_Policy}.
8851 It also activates pragmas @code{Check}, @code{Precondition}, and
8852 @code{Postcondition}. Note that these pragmas can also be controlled
8853 using the configuration pragma @code{Check_Policy}. In Ada 2012, it
8854 also activates all assertions defined in the RM as aspects: preconditions,
8855 postconditions, type invariants and (sub)type predicates. In all Ada modes,
8856 corresponding pragmas for type invariants and (sub)type predicates are
8857 also activated. The default is that all these assertions are disabled,
8858 and have no effect, other than being checked for syntactic validity, and
8859 in the case of subtype predicates, constructions such as membership tests
8860 still test predicates even if assertions are turned off.
8861 @end table
8862
8863 @geindex -gnatA (gcc)
8864
8865
8866 @table @asis
8867
8868 @item @code{-gnatA}
8869
8870 Avoid processing @code{gnat.adc}. If a @code{gnat.adc} file is present,
8871 it will be ignored.
8872 @end table
8873
8874 @geindex -gnatb (gcc)
8875
8876
8877 @table @asis
8878
8879 @item @code{-gnatb}
8880
8881 Generate brief messages to @code{stderr} even if verbose mode set.
8882 @end table
8883
8884 @geindex -gnatB (gcc)
8885
8886
8887 @table @asis
8888
8889 @item @code{-gnatB}
8890
8891 Assume no invalid (bad) values except for ‘Valid attribute use
8892 (@ref{e7,,Validity Checking}).
8893 @end table
8894
8895 @geindex -gnatc (gcc)
8896
8897
8898 @table @asis
8899
8900 @item @code{-gnatc}
8901
8902 Check syntax and semantics only (no code generation attempted). When the
8903 compiler is invoked by @code{gnatmake}, if the switch @code{-gnatc} is
8904 only given to the compiler (after @code{-cargs} or in package Compiler of
8905 the project file, @code{gnatmake} will fail because it will not find the
8906 object file after compilation. If @code{gnatmake} is called with
8907 @code{-gnatc} as a builder switch (before @code{-cargs} or in package
8908 Builder of the project file) then @code{gnatmake} will not fail because
8909 it will not look for the object files after compilation, and it will not try
8910 to build and link.
8911 @end table
8912
8913 @geindex -gnatC (gcc)
8914
8915
8916 @table @asis
8917
8918 @item @code{-gnatC}
8919
8920 Generate CodePeer intermediate format (no code generation attempted).
8921 This switch will generate an intermediate representation suitable for
8922 use by CodePeer (@code{.scil} files). This switch is not compatible with
8923 code generation (it will, among other things, disable some switches such
8924 as -gnatn, and enable others such as -gnata).
8925 @end table
8926
8927 @geindex -gnatd (gcc)
8928
8929
8930 @table @asis
8931
8932 @item @code{-gnatd}
8933
8934 Specify debug options for the compiler. The string of characters after
8935 the @code{-gnatd} specifies the specific debug options. The possible
8936 characters are 0-9, a-z, A-Z, optionally preceded by a dot or underscore.
8937 See compiler source file @code{debug.adb} for details of the implemented
8938 debug options. Certain debug options are relevant to applications
8939 programmers, and these are documented at appropriate points in this
8940 users guide.
8941 @end table
8942
8943 @geindex -gnatD[nn] (gcc)
8944
8945
8946 @table @asis
8947
8948 @item @code{-gnatD}
8949
8950 Create expanded source files for source level debugging. This switch
8951 also suppresses generation of cross-reference information
8952 (see @code{-gnatx}). Note that this switch is not allowed if a previous
8953 -gnatR switch has been given, since these two switches are not compatible.
8954 @end table
8955
8956 @geindex -gnateA (gcc)
8957
8958
8959 @table @asis
8960
8961 @item @code{-gnateA}
8962
8963 Check that the actual parameters of a subprogram call are not aliases of one
8964 another. To qualify as aliasing, the actuals must denote objects of a composite
8965 type, their memory locations must be identical or overlapping, and at least one
8966 of the corresponding formal parameters must be of mode OUT or IN OUT.
8967
8968 @example
8969 type Rec_Typ is record
8970 Data : Integer := 0;
8971 end record;
8972
8973 function Self (Val : Rec_Typ) return Rec_Typ is
8974 begin
8975 return Val;
8976 end Self;
8977
8978 procedure Detect_Aliasing (Val_1 : in out Rec_Typ; Val_2 : Rec_Typ) is
8979 begin
8980 null;
8981 end Detect_Aliasing;
8982
8983 Obj : Rec_Typ;
8984
8985 Detect_Aliasing (Obj, Obj);
8986 Detect_Aliasing (Obj, Self (Obj));
8987 @end example
8988
8989 In the example above, the first call to @code{Detect_Aliasing} fails with a
8990 @code{Program_Error} at run time because the actuals for @code{Val_1} and
8991 @code{Val_2} denote the same object. The second call executes without raising
8992 an exception because @code{Self(Obj)} produces an anonymous object which does
8993 not share the memory location of @code{Obj}.
8994 @end table
8995
8996 @geindex -gnateb (gcc)
8997
8998
8999 @table @asis
9000
9001 @item @code{-gnateb}
9002
9003 Store configuration files by their basename in ALI files. This switch is
9004 used for instance by gprbuild for distributed builds in order to prevent
9005 issues where machine-specific absolute paths could end up being stored in
9006 ALI files.
9007 @end table
9008
9009 @geindex -gnatec (gcc)
9010
9011
9012 @table @asis
9013
9014 @item @code{-gnatec=@emph{path}}
9015
9016 Specify a configuration pragma file
9017 (the equal sign is optional)
9018 (@ref{63,,The Configuration Pragmas Files}).
9019 @end table
9020
9021 @geindex -gnateC (gcc)
9022
9023
9024 @table @asis
9025
9026 @item @code{-gnateC}
9027
9028 Generate CodePeer messages in a compiler-like format. This switch is only
9029 effective if @code{-gnatcC} is also specified and requires an installation
9030 of CodePeer.
9031 @end table
9032
9033 @geindex -gnated (gcc)
9034
9035
9036 @table @asis
9037
9038 @item @code{-gnated}
9039
9040 Disable atomic synchronization
9041 @end table
9042
9043 @geindex -gnateD (gcc)
9044
9045
9046 @table @asis
9047
9048 @item @code{-gnateDsymbol[=@emph{value}]}
9049
9050 Defines a symbol, associated with @code{value}, for preprocessing.
9051 (@ref{90,,Integrated Preprocessing}).
9052 @end table
9053
9054 @geindex -gnateE (gcc)
9055
9056
9057 @table @asis
9058
9059 @item @code{-gnateE}
9060
9061 Generate extra information in exception messages. In particular, display
9062 extra column information and the value and range associated with index and
9063 range check failures, and extra column information for access checks.
9064 In cases where the compiler is able to determine at compile time that
9065 a check will fail, it gives a warning, and the extra information is not
9066 produced at run time.
9067 @end table
9068
9069 @geindex -gnatef (gcc)
9070
9071
9072 @table @asis
9073
9074 @item @code{-gnatef}
9075
9076 Display full source path name in brief error messages.
9077 @end table
9078
9079 @geindex -gnateF (gcc)
9080
9081
9082 @table @asis
9083
9084 @item @code{-gnateF}
9085
9086 Check for overflow on all floating-point operations, including those
9087 for unconstrained predefined types. See description of pragma
9088 @code{Check_Float_Overflow} in GNAT RM.
9089 @end table
9090
9091 @geindex -gnateg (gcc)
9092
9093 @code{-gnateg}
9094 @code{-gnatceg}
9095
9096 @quotation
9097
9098 The @code{-gnatc} switch must always be specified before this switch, e.g.
9099 @code{-gnatceg}. Generate a C header from the Ada input file. See
9100 @ref{b7,,Generating C Headers for Ada Specifications} for more
9101 information.
9102 @end quotation
9103
9104 @geindex -gnateG (gcc)
9105
9106
9107 @table @asis
9108
9109 @item @code{-gnateG}
9110
9111 Save result of preprocessing in a text file.
9112 @end table
9113
9114 @geindex -gnatei (gcc)
9115
9116
9117 @table @asis
9118
9119 @item @code{-gnatei@emph{nnn}}
9120
9121 Set maximum number of instantiations during compilation of a single unit to
9122 @code{nnn}. This may be useful in increasing the default maximum of 8000 for
9123 the rare case when a single unit legitimately exceeds this limit.
9124 @end table
9125
9126 @geindex -gnateI (gcc)
9127
9128
9129 @table @asis
9130
9131 @item @code{-gnateI@emph{nnn}}
9132
9133 Indicates that the source is a multi-unit source and that the index of the
9134 unit to compile is @code{nnn}. @code{nnn} needs to be a positive number and need
9135 to be a valid index in the multi-unit source.
9136 @end table
9137
9138 @geindex -gnatel (gcc)
9139
9140
9141 @table @asis
9142
9143 @item @code{-gnatel}
9144
9145 This switch can be used with the static elaboration model to issue info
9146 messages showing
9147 where implicit @code{pragma Elaborate} and @code{pragma Elaborate_All}
9148 are generated. This is useful in diagnosing elaboration circularities
9149 caused by these implicit pragmas when using the static elaboration
9150 model. See See the section in this guide on elaboration checking for
9151 further details. These messages are not generated by default, and are
9152 intended only for temporary use when debugging circularity problems.
9153 @end table
9154
9155 @geindex -gnatel (gcc)
9156
9157
9158 @table @asis
9159
9160 @item @code{-gnateL}
9161
9162 This switch turns off the info messages about implicit elaboration pragmas.
9163 @end table
9164
9165 @geindex -gnatem (gcc)
9166
9167
9168 @table @asis
9169
9170 @item @code{-gnatem=@emph{path}}
9171
9172 Specify a mapping file
9173 (the equal sign is optional)
9174 (@ref{e8,,Units to Sources Mapping Files}).
9175 @end table
9176
9177 @geindex -gnatep (gcc)
9178
9179
9180 @table @asis
9181
9182 @item @code{-gnatep=@emph{file}}
9183
9184 Specify a preprocessing data file
9185 (the equal sign is optional)
9186 (@ref{90,,Integrated Preprocessing}).
9187 @end table
9188
9189 @geindex -gnateP (gcc)
9190
9191
9192 @table @asis
9193
9194 @item @code{-gnateP}
9195
9196 Turn categorization dependency errors into warnings.
9197 Ada requires that units that WITH one another have compatible categories, for
9198 example a Pure unit cannot WITH a Preelaborate unit. If this switch is used,
9199 these errors become warnings (which can be ignored, or suppressed in the usual
9200 manner). This can be useful in some specialized circumstances such as the
9201 temporary use of special test software.
9202 @end table
9203
9204 @geindex -gnateS (gcc)
9205
9206
9207 @table @asis
9208
9209 @item @code{-gnateS}
9210
9211 Synonym of @code{-fdump-scos}, kept for backwards compatibility.
9212 @end table
9213
9214 @geindex -gnatet=file (gcc)
9215
9216
9217 @table @asis
9218
9219 @item @code{-gnatet=@emph{path}}
9220
9221 Generate target dependent information. The format of the output file is
9222 described in the section about switch @code{-gnateT}.
9223 @end table
9224
9225 @geindex -gnateT (gcc)
9226
9227
9228 @table @asis
9229
9230 @item @code{-gnateT=@emph{path}}
9231
9232 Read target dependent information, such as endianness or sizes and alignments
9233 of base type. If this switch is passed, the default target dependent
9234 information of the compiler is replaced by the one read from the input file.
9235 This is used by tools other than the compiler, e.g. to do
9236 semantic analysis of programs that will run on some other target than
9237 the machine on which the tool is run.
9238
9239 The following target dependent values should be defined,
9240 where @code{Nat} denotes a natural integer value, @code{Pos} denotes a
9241 positive integer value, and fields marked with a question mark are
9242 boolean fields, where a value of 0 is False, and a value of 1 is True:
9243
9244 @example
9245 Bits_BE : Nat; -- Bits stored big-endian?
9246 Bits_Per_Unit : Pos; -- Bits in a storage unit
9247 Bits_Per_Word : Pos; -- Bits in a word
9248 Bytes_BE : Nat; -- Bytes stored big-endian?
9249 Char_Size : Pos; -- Standard.Character'Size
9250 Double_Float_Alignment : Nat; -- Alignment of double float
9251 Double_Scalar_Alignment : Nat; -- Alignment of double length scalar
9252 Double_Size : Pos; -- Standard.Long_Float'Size
9253 Float_Size : Pos; -- Standard.Float'Size
9254 Float_Words_BE : Nat; -- Float words stored big-endian?
9255 Int_Size : Pos; -- Standard.Integer'Size
9256 Long_Double_Size : Pos; -- Standard.Long_Long_Float'Size
9257 Long_Long_Size : Pos; -- Standard.Long_Long_Integer'Size
9258 Long_Size : Pos; -- Standard.Long_Integer'Size
9259 Maximum_Alignment : Pos; -- Maximum permitted alignment
9260 Max_Unaligned_Field : Pos; -- Maximum size for unaligned bit field
9261 Pointer_Size : Pos; -- System.Address'Size
9262 Short_Enums : Nat; -- Foreign enums use short size?
9263 Short_Size : Pos; -- Standard.Short_Integer'Size
9264 Strict_Alignment : Nat; -- Strict alignment?
9265 System_Allocator_Alignment : Nat; -- Alignment for malloc calls
9266 Wchar_T_Size : Pos; -- Interfaces.C.wchar_t'Size
9267 Words_BE : Nat; -- Words stored big-endian?
9268 @end example
9269
9270 @code{Bits_Per_Unit} is the number of bits in a storage unit, the equivalent of
9271 GCC macro @code{BITS_PER_UNIT} documented as follows: @cite{Define this macro to be the number of bits in an addressable storage unit (byte); normally 8.}
9272
9273 @code{Bits_Per_Word} is the number of bits in a machine word, the equivalent of
9274 GCC macro @code{BITS_PER_WORD} documented as follows: @cite{Number of bits in a word; normally 32.}
9275
9276 @code{Double_Float_Alignment}, if not zero, is the maximum alignment that the
9277 compiler can choose by default for a 64-bit floating-point type or object.
9278
9279 @code{Double_Scalar_Alignment}, if not zero, is the maximum alignment that the
9280 compiler can choose by default for a 64-bit or larger scalar type or object.
9281
9282 @code{Maximum_Alignment} is the maximum alignment that the compiler can choose
9283 by default for a type or object, which is also the maximum alignment that can
9284 be specified in GNAT. It is computed for GCC backends as @code{BIGGEST_ALIGNMENT
9285 / BITS_PER_UNIT} where GCC macro @code{BIGGEST_ALIGNMENT} is documented as
9286 follows: @cite{Biggest alignment that any data type can require on this machine@comma{} in bits.}
9287
9288 @code{Max_Unaligned_Field} is the maximum size for unaligned bit field, which is
9289 64 for the majority of GCC targets (but can be different on some targets).
9290
9291 @code{Strict_Alignment} is the equivalent of GCC macro @code{STRICT_ALIGNMENT}
9292 documented as follows: @cite{Define this macro to be the value 1 if instructions will fail to work if given data not on the nominal alignment. If instructions will merely go slower in that case@comma{} define this macro as 0.}
9293
9294 @code{System_Allocator_Alignment} is the guaranteed alignment of data returned
9295 by calls to @code{malloc}.
9296
9297 The format of the input file is as follows. First come the values of
9298 the variables defined above, with one line per value:
9299
9300 @example
9301 name value
9302 @end example
9303
9304 where @code{name} is the name of the parameter, spelled out in full,
9305 and cased as in the above list, and @code{value} is an unsigned decimal
9306 integer. Two or more blanks separates the name from the value.
9307
9308 All the variables must be present, in alphabetical order (i.e. the
9309 same order as the list above).
9310
9311 Then there is a blank line to separate the two parts of the file. Then
9312 come the lines showing the floating-point types to be registered, with
9313 one line per registered mode:
9314
9315 @example
9316 name digs float_rep size alignment
9317 @end example
9318
9319 where @code{name} is the string name of the type (which can have
9320 single spaces embedded in the name (e.g. long double), @code{digs} is
9321 the number of digits for the floating-point type, @code{float_rep} is
9322 the float representation (I for IEEE-754-Binary, which is
9323 the only one supported at this time),
9324 @code{size} is the size in bits, @code{alignment} is the
9325 alignment in bits. The name is followed by at least two blanks, fields
9326 are separated by at least one blank, and a LF character immediately
9327 follows the alignment field.
9328
9329 Here is an example of a target parameterization file:
9330
9331 @example
9332 Bits_BE 0
9333 Bits_Per_Unit 8
9334 Bits_Per_Word 64
9335 Bytes_BE 0
9336 Char_Size 8
9337 Double_Float_Alignment 0
9338 Double_Scalar_Alignment 0
9339 Double_Size 64
9340 Float_Size 32
9341 Float_Words_BE 0
9342 Int_Size 64
9343 Long_Double_Size 128
9344 Long_Long_Size 64
9345 Long_Size 64
9346 Maximum_Alignment 16
9347 Max_Unaligned_Field 64
9348 Pointer_Size 64
9349 Short_Size 16
9350 Strict_Alignment 0
9351 System_Allocator_Alignment 16
9352 Wchar_T_Size 32
9353 Words_BE 0
9354
9355 float 15 I 64 64
9356 double 15 I 64 64
9357 long double 18 I 80 128
9358 TF 33 I 128 128
9359 @end example
9360 @end table
9361
9362 @geindex -gnateu (gcc)
9363
9364
9365 @table @asis
9366
9367 @item @code{-gnateu}
9368
9369 Ignore unrecognized validity, warning, and style switches that
9370 appear after this switch is given. This may be useful when
9371 compiling sources developed on a later version of the compiler
9372 with an earlier version. Of course the earlier version must
9373 support this switch.
9374 @end table
9375
9376 @geindex -gnateV (gcc)
9377
9378
9379 @table @asis
9380
9381 @item @code{-gnateV}
9382
9383 Check that all actual parameters of a subprogram call are valid according to
9384 the rules of validity checking (@ref{e7,,Validity Checking}).
9385 @end table
9386
9387 @geindex -gnateY (gcc)
9388
9389
9390 @table @asis
9391
9392 @item @code{-gnateY}
9393
9394 Ignore all STYLE_CHECKS pragmas. Full legality checks
9395 are still carried out, but the pragmas have no effect
9396 on what style checks are active. This allows all style
9397 checking options to be controlled from the command line.
9398 @end table
9399
9400 @geindex -gnatE (gcc)
9401
9402
9403 @table @asis
9404
9405 @item @code{-gnatE}
9406
9407 Dynamic elaboration checking mode enabled. For further details see
9408 @ref{f,,Elaboration Order Handling in GNAT}.
9409 @end table
9410
9411 @geindex -gnatf (gcc)
9412
9413
9414 @table @asis
9415
9416 @item @code{-gnatf}
9417
9418 Full errors. Multiple errors per line, all undefined references, do not
9419 attempt to suppress cascaded errors.
9420 @end table
9421
9422 @geindex -gnatF (gcc)
9423
9424
9425 @table @asis
9426
9427 @item @code{-gnatF}
9428
9429 Externals names are folded to all uppercase.
9430 @end table
9431
9432 @geindex -gnatg (gcc)
9433
9434
9435 @table @asis
9436
9437 @item @code{-gnatg}
9438
9439 Internal GNAT implementation mode. This should not be used for applications
9440 programs, it is intended only for use by the compiler and its run-time
9441 library. For documentation, see the GNAT sources. Note that @code{-gnatg}
9442 implies @code{-gnatw.ge} and @code{-gnatyg} so that all standard
9443 warnings and all standard style options are turned on. All warnings and style
9444 messages are treated as errors.
9445 @end table
9446
9447 @geindex -gnatG[nn] (gcc)
9448
9449
9450 @table @asis
9451
9452 @item @code{-gnatG=nn}
9453
9454 List generated expanded code in source form.
9455 @end table
9456
9457 @geindex -gnath (gcc)
9458
9459
9460 @table @asis
9461
9462 @item @code{-gnath}
9463
9464 Output usage information. The output is written to @code{stdout}.
9465 @end table
9466
9467 @geindex -gnatH (gcc)
9468
9469
9470 @table @asis
9471
9472 @item @code{-gnatH}
9473
9474 Legacy elaboration-checking mode enabled. When this switch is in effect,
9475 the pre-18.x access-before-elaboration model becomes the de facto model.
9476 For further details see @ref{f,,Elaboration Order Handling in GNAT}.
9477 @end table
9478
9479 @geindex -gnati (gcc)
9480
9481
9482 @table @asis
9483
9484 @item @code{-gnati@emph{c}}
9485
9486 Identifier character set (@code{c} = 1/2/3/4/5/9/p/8/f/n/w).
9487 For details of the possible selections for @code{c},
9488 see @ref{31,,Character Set Control}.
9489 @end table
9490
9491 @geindex -gnatI (gcc)
9492
9493
9494 @table @asis
9495
9496 @item @code{-gnatI}
9497
9498 Ignore representation clauses. When this switch is used,
9499 representation clauses are treated as comments. This is useful
9500 when initially porting code where you want to ignore rep clause
9501 problems, and also for compiling foreign code (particularly
9502 for use with ASIS). The representation clauses that are ignored
9503 are: enumeration_representation_clause, record_representation_clause,
9504 and attribute_definition_clause for the following attributes:
9505 Address, Alignment, Bit_Order, Component_Size, Machine_Radix,
9506 Object_Size, Scalar_Storage_Order, Size, Small, Stream_Size,
9507 and Value_Size. Pragma Default_Scalar_Storage_Order is also ignored.
9508 Note that this option should be used only for compiling – the
9509 code is likely to malfunction at run time.
9510 @end table
9511
9512 @geindex -gnatjnn (gcc)
9513
9514
9515 @table @asis
9516
9517 @item @code{-gnatj@emph{nn}}
9518
9519 Reformat error messages to fit on @code{nn} character lines
9520 @end table
9521
9522 @geindex -gnatJ (gcc)
9523
9524
9525 @table @asis
9526
9527 @item @code{-gnatJ}
9528
9529 Permissive elaboration-checking mode enabled. When this switch is in effect,
9530 the post-18.x access-before-elaboration model ignores potential issues with:
9531
9532
9533 @itemize -
9534
9535 @item
9536 Accept statements
9537
9538 @item
9539 Activations of tasks defined in instances
9540
9541 @item
9542 Assertion pragmas
9543
9544 @item
9545 Calls from within an instance to its enclosing context
9546
9547 @item
9548 Calls through generic formal parameters
9549
9550 @item
9551 Calls to subprograms defined in instances
9552
9553 @item
9554 Entry calls
9555
9556 @item
9557 Indirect calls using ‘Access
9558
9559 @item
9560 Requeue statements
9561
9562 @item
9563 Select statements
9564
9565 @item
9566 Synchronous task suspension
9567 @end itemize
9568
9569 and does not emit compile-time diagnostics or run-time checks. For further
9570 details see @ref{f,,Elaboration Order Handling in GNAT}.
9571 @end table
9572
9573 @geindex -gnatk (gcc)
9574
9575
9576 @table @asis
9577
9578 @item @code{-gnatk=@emph{n}}
9579
9580 Limit file names to @code{n} (1-999) characters (@code{k} = krunch).
9581 @end table
9582
9583 @geindex -gnatl (gcc)
9584
9585
9586 @table @asis
9587
9588 @item @code{-gnatl}
9589
9590 Output full source listing with embedded error messages.
9591 @end table
9592
9593 @geindex -gnatL (gcc)
9594
9595
9596 @table @asis
9597
9598 @item @code{-gnatL}
9599
9600 Used in conjunction with -gnatG or -gnatD to intersperse original
9601 source lines (as comment lines with line numbers) in the expanded
9602 source output.
9603 @end table
9604
9605 @geindex -gnatm (gcc)
9606
9607
9608 @table @asis
9609
9610 @item @code{-gnatm=@emph{n}}
9611
9612 Limit number of detected error or warning messages to @code{n}
9613 where @code{n} is in the range 1..999999. The default setting if
9614 no switch is given is 9999. If the number of warnings reaches this
9615 limit, then a message is output and further warnings are suppressed,
9616 but the compilation is continued. If the number of error messages
9617 reaches this limit, then a message is output and the compilation
9618 is abandoned. The equal sign here is optional. A value of zero
9619 means that no limit applies.
9620 @end table
9621
9622 @geindex -gnatn (gcc)
9623
9624
9625 @table @asis
9626
9627 @item @code{-gnatn[12]}
9628
9629 Activate inlining across units for subprograms for which pragma @code{Inline}
9630 is specified. This inlining is performed by the GCC back-end. An optional
9631 digit sets the inlining level: 1 for moderate inlining across units
9632 or 2 for full inlining across units. If no inlining level is specified,
9633 the compiler will pick it based on the optimization level.
9634 @end table
9635
9636 @geindex -gnatN (gcc)
9637
9638
9639 @table @asis
9640
9641 @item @code{-gnatN}
9642
9643 Activate front end inlining for subprograms for which
9644 pragma @code{Inline} is specified. This inlining is performed
9645 by the front end and will be visible in the
9646 @code{-gnatG} output.
9647
9648 When using a gcc-based back end, then the use of
9649 @code{-gnatN} is deprecated, and the use of @code{-gnatn} is preferred.
9650 Historically front end inlining was more extensive than the gcc back end
9651 inlining, but that is no longer the case.
9652 @end table
9653
9654 @geindex -gnato0 (gcc)
9655
9656
9657 @table @asis
9658
9659 @item @code{-gnato0}
9660
9661 Suppresses overflow checking. This causes the behavior of the compiler to
9662 match the default for older versions where overflow checking was suppressed
9663 by default. This is equivalent to having
9664 @code{pragma Suppress (Overflow_Check)} in a configuration pragma file.
9665 @end table
9666
9667 @geindex -gnato?? (gcc)
9668
9669
9670 @table @asis
9671
9672 @item @code{-gnato??}
9673
9674 Set default mode for handling generation of code to avoid intermediate
9675 arithmetic overflow. Here @code{??} is two digits, a
9676 single digit, or nothing. Each digit is one of the digits @code{1}
9677 through @code{3}:
9678
9679
9680 @multitable {xxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
9681 @item
9682
9683 Digit
9684
9685 @tab
9686
9687 Interpretation
9688
9689 @item
9690
9691 @emph{1}
9692
9693 @tab
9694
9695 All intermediate overflows checked against base type (@code{STRICT})
9696
9697 @item
9698
9699 @emph{2}
9700
9701 @tab
9702
9703 Minimize intermediate overflows (@code{MINIMIZED})
9704
9705 @item
9706
9707 @emph{3}
9708
9709 @tab
9710
9711 Eliminate intermediate overflows (@code{ELIMINATED})
9712
9713 @end multitable
9714
9715
9716 If only one digit appears, then it applies to all
9717 cases; if two digits are given, then the first applies outside
9718 assertions, pre/postconditions, and type invariants, and the second
9719 applies within assertions, pre/postconditions, and type invariants.
9720
9721 If no digits follow the @code{-gnato}, then it is equivalent to
9722 @code{-gnato11},
9723 causing all intermediate overflows to be handled in strict
9724 mode.
9725
9726 This switch also causes arithmetic overflow checking to be performed
9727 (as though @code{pragma Unsuppress (Overflow_Check)} had been specified).
9728
9729 The default if no option @code{-gnato} is given is that overflow handling
9730 is in @code{STRICT} mode (computations done using the base type), and that
9731 overflow checking is enabled.
9732
9733 Note that division by zero is a separate check that is not
9734 controlled by this switch (divide-by-zero checking is on by default).
9735
9736 See also @ref{e9,,Specifying the Desired Mode}.
9737 @end table
9738
9739 @geindex -gnatp (gcc)
9740
9741
9742 @table @asis
9743
9744 @item @code{-gnatp}
9745
9746 Suppress all checks. See @ref{ea,,Run-Time Checks} for details. This switch
9747 has no effect if cancelled by a subsequent @code{-gnat-p} switch.
9748 @end table
9749
9750 @geindex -gnat-p (gcc)
9751
9752
9753 @table @asis
9754
9755 @item @code{-gnat-p}
9756
9757 Cancel effect of previous @code{-gnatp} switch.
9758 @end table
9759
9760 @geindex -gnatq (gcc)
9761
9762
9763 @table @asis
9764
9765 @item @code{-gnatq}
9766
9767 Don’t quit. Try semantics, even if parse errors.
9768 @end table
9769
9770 @geindex -gnatQ (gcc)
9771
9772
9773 @table @asis
9774
9775 @item @code{-gnatQ}
9776
9777 Don’t quit. Generate @code{ALI} and tree files even if illegalities.
9778 Note that code generation is still suppressed in the presence of any
9779 errors, so even with @code{-gnatQ} no object file is generated.
9780 @end table
9781
9782 @geindex -gnatr (gcc)
9783
9784
9785 @table @asis
9786
9787 @item @code{-gnatr}
9788
9789 Treat pragma Restrictions as Restriction_Warnings.
9790 @end table
9791
9792 @geindex -gnatR (gcc)
9793
9794
9795 @table @asis
9796
9797 @item @code{-gnatR[0|1|2|3|4][e][j][m][s]}
9798
9799 Output representation information for declared types, objects and
9800 subprograms. Note that this switch is not allowed if a previous
9801 @code{-gnatD} switch has been given, since these two switches
9802 are not compatible.
9803 @end table
9804
9805 @geindex -gnats (gcc)
9806
9807
9808 @table @asis
9809
9810 @item @code{-gnats}
9811
9812 Syntax check only.
9813 @end table
9814
9815 @geindex -gnatS (gcc)
9816
9817
9818 @table @asis
9819
9820 @item @code{-gnatS}
9821
9822 Print package Standard.
9823 @end table
9824
9825 @geindex -gnatT (gcc)
9826
9827
9828 @table @asis
9829
9830 @item @code{-gnatT@emph{nnn}}
9831
9832 All compiler tables start at @code{nnn} times usual starting size.
9833 @end table
9834
9835 @geindex -gnatu (gcc)
9836
9837
9838 @table @asis
9839
9840 @item @code{-gnatu}
9841
9842 List units for this compilation.
9843 @end table
9844
9845 @geindex -gnatU (gcc)
9846
9847
9848 @table @asis
9849
9850 @item @code{-gnatU}
9851
9852 Tag all error messages with the unique string ‘error:’
9853 @end table
9854
9855 @geindex -gnatv (gcc)
9856
9857
9858 @table @asis
9859
9860 @item @code{-gnatv}
9861
9862 Verbose mode. Full error output with source lines to @code{stdout}.
9863 @end table
9864
9865 @geindex -gnatV (gcc)
9866
9867
9868 @table @asis
9869
9870 @item @code{-gnatV}
9871
9872 Control level of validity checking (@ref{e7,,Validity Checking}).
9873 @end table
9874
9875 @geindex -gnatw (gcc)
9876
9877
9878 @table @asis
9879
9880 @item @code{-gnatw@emph{xxx}}
9881
9882 Warning mode where
9883 @code{xxx} is a string of option letters that denotes
9884 the exact warnings that
9885 are enabled or disabled (@ref{eb,,Warning Message Control}).
9886 @end table
9887
9888 @geindex -gnatW (gcc)
9889
9890
9891 @table @asis
9892
9893 @item @code{-gnatW@emph{e}}
9894
9895 Wide character encoding method
9896 (@code{e}=n/h/u/s/e/8).
9897 @end table
9898
9899 @geindex -gnatx (gcc)
9900
9901
9902 @table @asis
9903
9904 @item @code{-gnatx}
9905
9906 Suppress generation of cross-reference information.
9907 @end table
9908
9909 @geindex -gnatX (gcc)
9910
9911
9912 @table @asis
9913
9914 @item @code{-gnatX}
9915
9916 Enable GNAT implementation extensions and latest Ada version.
9917 @end table
9918
9919 @geindex -gnaty (gcc)
9920
9921
9922 @table @asis
9923
9924 @item @code{-gnaty}
9925
9926 Enable built-in style checks (@ref{ec,,Style Checking}).
9927 @end table
9928
9929 @geindex -gnatz (gcc)
9930
9931
9932 @table @asis
9933
9934 @item @code{-gnatz@emph{m}}
9935
9936 Distribution stub generation and compilation
9937 (@code{m}=r/c for receiver/caller stubs).
9938 @end table
9939
9940 @geindex -I (gcc)
9941
9942
9943 @table @asis
9944
9945 @item @code{-I@emph{dir}}
9946
9947 @geindex RTL
9948
9949 Direct GNAT to search the @code{dir} directory for source files needed by
9950 the current compilation
9951 (see @ref{73,,Search Paths and the Run-Time Library (RTL)}).
9952 @end table
9953
9954 @geindex -I- (gcc)
9955
9956
9957 @table @asis
9958
9959 @item @code{-I-}
9960
9961 @geindex RTL
9962
9963 Except for the source file named in the command line, do not look for source
9964 files in the directory containing the source file named in the command line
9965 (see @ref{73,,Search Paths and the Run-Time Library (RTL)}).
9966 @end table
9967
9968 @geindex -o (gcc)
9969
9970
9971 @table @asis
9972
9973 @item @code{-o @emph{file}}
9974
9975 This switch is used in @code{gcc} to redirect the generated object file
9976 and its associated ALI file. Beware of this switch with GNAT, because it may
9977 cause the object file and ALI file to have different names which in turn
9978 may confuse the binder and the linker.
9979 @end table
9980
9981 @geindex -nostdinc (gcc)
9982
9983
9984 @table @asis
9985
9986 @item @code{-nostdinc}
9987
9988 Inhibit the search of the default location for the GNAT Run Time
9989 Library (RTL) source files.
9990 @end table
9991
9992 @geindex -nostdlib (gcc)
9993
9994
9995 @table @asis
9996
9997 @item @code{-nostdlib}
9998
9999 Inhibit the search of the default location for the GNAT Run Time
10000 Library (RTL) ALI files.
10001 @end table
10002
10003 @geindex -O (gcc)
10004
10005
10006 @table @asis
10007
10008 @item @code{-O[@emph{n}]}
10009
10010 @code{n} controls the optimization level:
10011
10012
10013 @multitable {xxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
10014 @item
10015
10016 @emph{n}
10017
10018 @tab
10019
10020 Effect
10021
10022 @item
10023
10024 @emph{0}
10025
10026 @tab
10027
10028 No optimization, the default setting if no @code{-O} appears
10029
10030 @item
10031
10032 @emph{1}
10033
10034 @tab
10035
10036 Normal optimization, the default if you specify @code{-O} without an
10037 operand. A good compromise between code quality and compilation
10038 time.
10039
10040 @item
10041
10042 @emph{2}
10043
10044 @tab
10045
10046 Extensive optimization, may improve execution time, possibly at
10047 the cost of substantially increased compilation time.
10048
10049 @item
10050
10051 @emph{3}
10052
10053 @tab
10054
10055 Same as @code{-O2}, and also includes inline expansion for small
10056 subprograms in the same unit.
10057
10058 @item
10059
10060 @emph{s}
10061
10062 @tab
10063
10064 Optimize space usage
10065
10066 @end multitable
10067
10068
10069 See also @ref{ed,,Optimization Levels}.
10070 @end table
10071
10072 @geindex -pass-exit-codes (gcc)
10073
10074
10075 @table @asis
10076
10077 @item @code{-pass-exit-codes}
10078
10079 Catch exit codes from the compiler and use the most meaningful as
10080 exit status.
10081 @end table
10082
10083 @geindex --RTS (gcc)
10084
10085
10086 @table @asis
10087
10088 @item @code{--RTS=@emph{rts-path}}
10089
10090 Specifies the default location of the run-time library. Same meaning as the
10091 equivalent @code{gnatmake} flag (@ref{ce,,Switches for gnatmake}).
10092 @end table
10093
10094 @geindex -S (gcc)
10095
10096
10097 @table @asis
10098
10099 @item @code{-S}
10100
10101 Used in place of @code{-c} to
10102 cause the assembler source file to be
10103 generated, using @code{.s} as the extension,
10104 instead of the object file.
10105 This may be useful if you need to examine the generated assembly code.
10106 @end table
10107
10108 @geindex -fverbose-asm (gcc)
10109
10110
10111 @table @asis
10112
10113 @item @code{-fverbose-asm}
10114
10115 Used in conjunction with @code{-S}
10116 to cause the generated assembly code file to be annotated with variable
10117 names, making it significantly easier to follow.
10118 @end table
10119
10120 @geindex -v (gcc)
10121
10122
10123 @table @asis
10124
10125 @item @code{-v}
10126
10127 Show commands generated by the @code{gcc} driver. Normally used only for
10128 debugging purposes or if you need to be sure what version of the
10129 compiler you are executing.
10130 @end table
10131
10132 @geindex -V (gcc)
10133
10134
10135 @table @asis
10136
10137 @item @code{-V @emph{ver}}
10138
10139 Execute @code{ver} version of the compiler. This is the @code{gcc}
10140 version, not the GNAT version.
10141 @end table
10142
10143 @geindex -w (gcc)
10144
10145
10146 @table @asis
10147
10148 @item @code{-w}
10149
10150 Turn off warnings generated by the back end of the compiler. Use of
10151 this switch also causes the default for front end warnings to be set
10152 to suppress (as though @code{-gnatws} had appeared at the start of
10153 the options).
10154 @end table
10155
10156 @geindex Combining GNAT switches
10157
10158 You may combine a sequence of GNAT switches into a single switch. For
10159 example, the combined switch
10160
10161 @quotation
10162
10163 @example
10164 -gnatofi3
10165 @end example
10166 @end quotation
10167
10168 is equivalent to specifying the following sequence of switches:
10169
10170 @quotation
10171
10172 @example
10173 -gnato -gnatf -gnati3
10174 @end example
10175 @end quotation
10176
10177 The following restrictions apply to the combination of switches
10178 in this manner:
10179
10180
10181 @itemize *
10182
10183 @item
10184 The switch @code{-gnatc} if combined with other switches must come
10185 first in the string.
10186
10187 @item
10188 The switch @code{-gnats} if combined with other switches must come
10189 first in the string.
10190
10191 @item
10192 The switches
10193 @code{-gnatzc} and @code{-gnatzr} may not be combined with any other
10194 switches, and only one of them may appear in the command line.
10195
10196 @item
10197 The switch @code{-gnat-p} may not be combined with any other switch.
10198
10199 @item
10200 Once a ‘y’ appears in the string (that is a use of the @code{-gnaty}
10201 switch), then all further characters in the switch are interpreted
10202 as style modifiers (see description of @code{-gnaty}).
10203
10204 @item
10205 Once a ‘d’ appears in the string (that is a use of the @code{-gnatd}
10206 switch), then all further characters in the switch are interpreted
10207 as debug flags (see description of @code{-gnatd}).
10208
10209 @item
10210 Once a ‘w’ appears in the string (that is a use of the @code{-gnatw}
10211 switch), then all further characters in the switch are interpreted
10212 as warning mode modifiers (see description of @code{-gnatw}).
10213
10214 @item
10215 Once a ‘V’ appears in the string (that is a use of the @code{-gnatV}
10216 switch), then all further characters in the switch are interpreted
10217 as validity checking options (@ref{e7,,Validity Checking}).
10218
10219 @item
10220 Option ‘em’, ‘ec’, ‘ep’, ‘l=’ and ‘R’ must be the last options in
10221 a combined list of options.
10222 @end itemize
10223
10224 @node Output and Error Message Control,Warning Message Control,Alphabetical List of All Switches,Compiler Switches
10225 @anchor{gnat_ugn/building_executable_programs_with_gnat id14}@anchor{ee}@anchor{gnat_ugn/building_executable_programs_with_gnat output-and-error-message-control}@anchor{ef}
10226 @subsection Output and Error Message Control
10227
10228
10229 @geindex stderr
10230
10231 The standard default format for error messages is called ‘brief format’.
10232 Brief format messages are written to @code{stderr} (the standard error
10233 file) and have the following form:
10234
10235 @example
10236 e.adb:3:04: Incorrect spelling of keyword "function"
10237 e.adb:4:20: ";" should be "is"
10238 @end example
10239
10240 The first integer after the file name is the line number in the file,
10241 and the second integer is the column number within the line.
10242 @code{GNAT Studio} can parse the error messages
10243 and point to the referenced character.
10244 The following switches provide control over the error message
10245 format:
10246
10247 @geindex -gnatv (gcc)
10248
10249
10250 @table @asis
10251
10252 @item @code{-gnatv}
10253
10254 The @code{v} stands for verbose.
10255 The effect of this setting is to write long-format error
10256 messages to @code{stdout} (the standard output file.
10257 The same program compiled with the
10258 @code{-gnatv} switch would generate:
10259
10260 @example
10261 3. funcion X (Q : Integer)
10262 |
10263 >>> Incorrect spelling of keyword "function"
10264 4. return Integer;
10265 |
10266 >>> ";" should be "is"
10267 @end example
10268
10269 The vertical bar indicates the location of the error, and the @code{>>>}
10270 prefix can be used to search for error messages. When this switch is
10271 used the only source lines output are those with errors.
10272 @end table
10273
10274 @geindex -gnatl (gcc)
10275
10276
10277 @table @asis
10278
10279 @item @code{-gnatl}
10280
10281 The @code{l} stands for list.
10282 This switch causes a full listing of
10283 the file to be generated. In the case where a body is
10284 compiled, the corresponding spec is also listed, along
10285 with any subunits. Typical output from compiling a package
10286 body @code{p.adb} might look like:
10287
10288 @example
10289 Compiling: p.adb
10290
10291 1. package body p is
10292 2. procedure a;
10293 3. procedure a is separate;
10294 4. begin
10295 5. null
10296 |
10297 >>> missing ";"
10298
10299 6. end;
10300
10301 Compiling: p.ads
10302
10303 1. package p is
10304 2. pragma Elaborate_Body
10305 |
10306 >>> missing ";"
10307
10308 3. end p;
10309
10310 Compiling: p-a.adb
10311
10312 1. separate p
10313 |
10314 >>> missing "("
10315
10316 2. procedure a is
10317 3. begin
10318 4. null
10319 |
10320 >>> missing ";"
10321
10322 5. end;
10323 @end example
10324
10325 When you specify the @code{-gnatv} or @code{-gnatl} switches and
10326 standard output is redirected, a brief summary is written to
10327 @code{stderr} (standard error) giving the number of error messages and
10328 warning messages generated.
10329 @end table
10330
10331 @geindex -gnatl=fname (gcc)
10332
10333
10334 @table @asis
10335
10336 @item @code{-gnatl=@emph{fname}}
10337
10338 This has the same effect as @code{-gnatl} except that the output is
10339 written to a file instead of to standard output. If the given name
10340 @code{fname} does not start with a period, then it is the full name
10341 of the file to be written. If @code{fname} is an extension, it is
10342 appended to the name of the file being compiled. For example, if
10343 file @code{xyz.adb} is compiled with @code{-gnatl=.lst},
10344 then the output is written to file xyz.adb.lst.
10345 @end table
10346
10347 @geindex -gnatU (gcc)
10348
10349
10350 @table @asis
10351
10352 @item @code{-gnatU}
10353
10354 This switch forces all error messages to be preceded by the unique
10355 string ‘error:’. This means that error messages take a few more
10356 characters in space, but allows easy searching for and identification
10357 of error messages.
10358 @end table
10359
10360 @geindex -gnatb (gcc)
10361
10362
10363 @table @asis
10364
10365 @item @code{-gnatb}
10366
10367 The @code{b} stands for brief.
10368 This switch causes GNAT to generate the
10369 brief format error messages to @code{stderr} (the standard error
10370 file) as well as the verbose
10371 format message or full listing (which as usual is written to
10372 @code{stdout} (the standard output file).
10373 @end table
10374
10375 @geindex -gnatm (gcc)
10376
10377
10378 @table @asis
10379
10380 @item @code{-gnatm=@emph{n}}
10381
10382 The @code{m} stands for maximum.
10383 @code{n} is a decimal integer in the
10384 range of 1 to 999999 and limits the number of error or warning
10385 messages to be generated. For example, using
10386 @code{-gnatm2} might yield
10387
10388 @example
10389 e.adb:3:04: Incorrect spelling of keyword "function"
10390 e.adb:5:35: missing ".."
10391 fatal error: maximum number of errors detected
10392 compilation abandoned
10393 @end example
10394
10395 The default setting if
10396 no switch is given is 9999. If the number of warnings reaches this
10397 limit, then a message is output and further warnings are suppressed,
10398 but the compilation is continued. If the number of error messages
10399 reaches this limit, then a message is output and the compilation
10400 is abandoned. A value of zero means that no limit applies.
10401
10402 Note that the equal sign is optional, so the switches
10403 @code{-gnatm2} and @code{-gnatm=2} are equivalent.
10404 @end table
10405
10406 @geindex -gnatf (gcc)
10407
10408
10409 @table @asis
10410
10411 @item @code{-gnatf}
10412
10413 @geindex Error messages
10414 @geindex suppressing
10415
10416 The @code{f} stands for full.
10417 Normally, the compiler suppresses error messages that are likely to be
10418 redundant. This switch causes all error
10419 messages to be generated. In particular, in the case of
10420 references to undefined variables. If a given variable is referenced
10421 several times, the normal format of messages is
10422
10423 @example
10424 e.adb:7:07: "V" is undefined (more references follow)
10425 @end example
10426
10427 where the parenthetical comment warns that there are additional
10428 references to the variable @code{V}. Compiling the same program with the
10429 @code{-gnatf} switch yields
10430
10431 @example
10432 e.adb:7:07: "V" is undefined
10433 e.adb:8:07: "V" is undefined
10434 e.adb:8:12: "V" is undefined
10435 e.adb:8:16: "V" is undefined
10436 e.adb:9:07: "V" is undefined
10437 e.adb:9:12: "V" is undefined
10438 @end example
10439
10440 The @code{-gnatf} switch also generates additional information for
10441 some error messages. Some examples are:
10442
10443
10444 @itemize *
10445
10446 @item
10447 Details on possibly non-portable unchecked conversion
10448
10449 @item
10450 List possible interpretations for ambiguous calls
10451
10452 @item
10453 Additional details on incorrect parameters
10454 @end itemize
10455 @end table
10456
10457 @geindex -gnatjnn (gcc)
10458
10459
10460 @table @asis
10461
10462 @item @code{-gnatjnn}
10463
10464 In normal operation mode (or if @code{-gnatj0} is used), then error messages
10465 with continuation lines are treated as though the continuation lines were
10466 separate messages (and so a warning with two continuation lines counts as
10467 three warnings, and is listed as three separate messages).
10468
10469 If the @code{-gnatjnn} switch is used with a positive value for nn, then
10470 messages are output in a different manner. A message and all its continuation
10471 lines are treated as a unit, and count as only one warning or message in the
10472 statistics totals. Furthermore, the message is reformatted so that no line
10473 is longer than nn characters.
10474 @end table
10475
10476 @geindex -gnatq (gcc)
10477
10478
10479 @table @asis
10480
10481 @item @code{-gnatq}
10482
10483 The @code{q} stands for quit (really ‘don’t quit’).
10484 In normal operation mode, the compiler first parses the program and
10485 determines if there are any syntax errors. If there are, appropriate
10486 error messages are generated and compilation is immediately terminated.
10487 This switch tells
10488 GNAT to continue with semantic analysis even if syntax errors have been
10489 found. This may enable the detection of more errors in a single run. On
10490 the other hand, the semantic analyzer is more likely to encounter some
10491 internal fatal error when given a syntactically invalid tree.
10492 @end table
10493
10494 @geindex -gnatQ (gcc)
10495
10496
10497 @table @asis
10498
10499 @item @code{-gnatQ}
10500
10501 In normal operation mode, the @code{ALI} file is not generated if any
10502 illegalities are detected in the program. The use of @code{-gnatQ} forces
10503 generation of the @code{ALI} file. This file is marked as being in
10504 error, so it cannot be used for binding purposes, but it does contain
10505 reasonably complete cross-reference information, and thus may be useful
10506 for use by tools (e.g., semantic browsing tools or integrated development
10507 environments) that are driven from the @code{ALI} file. This switch
10508 implies @code{-gnatq}, since the semantic phase must be run to get a
10509 meaningful ALI file.
10510
10511 When @code{-gnatQ} is used and the generated @code{ALI} file is marked as
10512 being in error, @code{gnatmake} will attempt to recompile the source when it
10513 finds such an @code{ALI} file, including with switch @code{-gnatc}.
10514
10515 Note that @code{-gnatQ} has no effect if @code{-gnats} is specified,
10516 since ALI files are never generated if @code{-gnats} is set.
10517 @end table
10518
10519 @node Warning Message Control,Debugging and Assertion Control,Output and Error Message Control,Compiler Switches
10520 @anchor{gnat_ugn/building_executable_programs_with_gnat id15}@anchor{f0}@anchor{gnat_ugn/building_executable_programs_with_gnat warning-message-control}@anchor{eb}
10521 @subsection Warning Message Control
10522
10523
10524 @geindex Warning messages
10525
10526 In addition to error messages, which correspond to illegalities as defined
10527 in the Ada Reference Manual, the compiler detects two kinds of warning
10528 situations.
10529
10530 First, the compiler considers some constructs suspicious and generates a
10531 warning message to alert you to a possible error. Second, if the
10532 compiler detects a situation that is sure to raise an exception at
10533 run time, it generates a warning message. The following shows an example
10534 of warning messages:
10535
10536 @example
10537 e.adb:4:24: warning: creation of object may raise Storage_Error
10538 e.adb:10:17: warning: static value out of range
10539 e.adb:10:17: warning: "Constraint_Error" will be raised at run time
10540 @end example
10541
10542 GNAT considers a large number of situations as appropriate
10543 for the generation of warning messages. As always, warnings are not
10544 definite indications of errors. For example, if you do an out-of-range
10545 assignment with the deliberate intention of raising a
10546 @code{Constraint_Error} exception, then the warning that may be
10547 issued does not indicate an error. Some of the situations for which GNAT
10548 issues warnings (at least some of the time) are given in the following
10549 list. This list is not complete, and new warnings are often added to
10550 subsequent versions of GNAT. The list is intended to give a general idea
10551 of the kinds of warnings that are generated.
10552
10553
10554 @itemize *
10555
10556 @item
10557 Possible infinitely recursive calls
10558
10559 @item
10560 Out-of-range values being assigned
10561
10562 @item
10563 Possible order of elaboration problems
10564
10565 @item
10566 Size not a multiple of alignment for a record type
10567
10568 @item
10569 Assertions (pragma Assert) that are sure to fail
10570
10571 @item
10572 Unreachable code
10573
10574 @item
10575 Address clauses with possibly unaligned values, or where an attempt is
10576 made to overlay a smaller variable with a larger one.
10577
10578 @item
10579 Fixed-point type declarations with a null range
10580
10581 @item
10582 Direct_IO or Sequential_IO instantiated with a type that has access values
10583
10584 @item
10585 Variables that are never assigned a value
10586
10587 @item
10588 Variables that are referenced before being initialized
10589
10590 @item
10591 Task entries with no corresponding @code{accept} statement
10592
10593 @item
10594 Duplicate accepts for the same task entry in a @code{select}
10595
10596 @item
10597 Objects that take too much storage
10598
10599 @item
10600 Unchecked conversion between types of differing sizes
10601
10602 @item
10603 Missing @code{return} statement along some execution path in a function
10604
10605 @item
10606 Incorrect (unrecognized) pragmas
10607
10608 @item
10609 Incorrect external names
10610
10611 @item
10612 Allocation from empty storage pool
10613
10614 @item
10615 Potentially blocking operation in protected type
10616
10617 @item
10618 Suspicious parenthesization of expressions
10619
10620 @item
10621 Mismatching bounds in an aggregate
10622
10623 @item
10624 Attempt to return local value by reference
10625
10626 @item
10627 Premature instantiation of a generic body
10628
10629 @item
10630 Attempt to pack aliased components
10631
10632 @item
10633 Out of bounds array subscripts
10634
10635 @item
10636 Wrong length on string assignment
10637
10638 @item
10639 Violations of style rules if style checking is enabled
10640
10641 @item
10642 Unused @emph{with} clauses
10643
10644 @item
10645 @code{Bit_Order} usage that does not have any effect
10646
10647 @item
10648 @code{Standard.Duration} used to resolve universal fixed expression
10649
10650 @item
10651 Dereference of possibly null value
10652
10653 @item
10654 Declaration that is likely to cause storage error
10655
10656 @item
10657 Internal GNAT unit @emph{with}ed by application unit
10658
10659 @item
10660 Values known to be out of range at compile time
10661
10662 @item
10663 Unreferenced or unmodified variables. Note that a special
10664 exemption applies to variables which contain any of the substrings
10665 @code{DISCARD, DUMMY, IGNORE, JUNK, UNUSED}, in any casing. Such variables
10666 are considered likely to be intentionally used in a situation where
10667 otherwise a warning would be given, so warnings of this kind are
10668 always suppressed for such variables.
10669
10670 @item
10671 Address overlays that could clobber memory
10672
10673 @item
10674 Unexpected initialization when address clause present
10675
10676 @item
10677 Bad alignment for address clause
10678
10679 @item
10680 Useless type conversions
10681
10682 @item
10683 Redundant assignment statements and other redundant constructs
10684
10685 @item
10686 Useless exception handlers
10687
10688 @item
10689 Accidental hiding of name by child unit
10690
10691 @item
10692 Access before elaboration detected at compile time
10693
10694 @item
10695 A range in a @code{for} loop that is known to be null or might be null
10696 @end itemize
10697
10698 The following section lists compiler switches that are available
10699 to control the handling of warning messages. It is also possible
10700 to exercise much finer control over what warnings are issued and
10701 suppressed using the GNAT pragma Warnings (see the description
10702 of the pragma in the @cite{GNAT_Reference_manual}).
10703
10704 @geindex -gnatwa (gcc)
10705
10706
10707 @table @asis
10708
10709 @item @code{-gnatwa}
10710
10711 @emph{Activate most optional warnings.}
10712
10713 This switch activates most optional warning messages. See the remaining list
10714 in this section for details on optional warning messages that can be
10715 individually controlled. The warnings that are not turned on by this
10716 switch are:
10717
10718
10719 @itemize *
10720
10721 @item
10722 @code{-gnatwd} (implicit dereferencing)
10723
10724 @item
10725 @code{-gnatw.d} (tag warnings with -gnatw switch)
10726
10727 @item
10728 @code{-gnatwh} (hiding)
10729
10730 @item
10731 @code{-gnatw.h} (holes in record layouts)
10732
10733 @item
10734 @code{-gnatw.j} (late primitives of tagged types)
10735
10736 @item
10737 @code{-gnatw.k} (redefinition of names in standard)
10738
10739 @item
10740 @code{-gnatwl} (elaboration warnings)
10741
10742 @item
10743 @code{-gnatw.l} (inherited aspects)
10744
10745 @item
10746 @code{-gnatw.n} (atomic synchronization)
10747
10748 @item
10749 @code{-gnatwo} (address clause overlay)
10750
10751 @item
10752 @code{-gnatw.o} (values set by out parameters ignored)
10753
10754 @item
10755 @code{-gnatw.q} (questionable layout of record types)
10756
10757 @item
10758 @code{-gnatw_r} (out-of-order record representation clauses)
10759
10760 @item
10761 @code{-gnatw.s} (overridden size clause)
10762
10763 @item
10764 @code{-gnatwt} (tracking of deleted conditional code)
10765
10766 @item
10767 @code{-gnatw.u} (unordered enumeration)
10768
10769 @item
10770 @code{-gnatw.w} (use of Warnings Off)
10771
10772 @item
10773 @code{-gnatw.y} (reasons for package needing body)
10774 @end itemize
10775
10776 All other optional warnings are turned on.
10777 @end table
10778
10779 @geindex -gnatwA (gcc)
10780
10781
10782 @table @asis
10783
10784 @item @code{-gnatwA}
10785
10786 @emph{Suppress all optional errors.}
10787
10788 This switch suppresses all optional warning messages, see remaining list
10789 in this section for details on optional warning messages that can be
10790 individually controlled. Note that unlike switch @code{-gnatws}, the
10791 use of switch @code{-gnatwA} does not suppress warnings that are
10792 normally given unconditionally and cannot be individually controlled
10793 (for example, the warning about a missing exit path in a function).
10794 Also, again unlike switch @code{-gnatws}, warnings suppressed by
10795 the use of switch @code{-gnatwA} can be individually turned back
10796 on. For example the use of switch @code{-gnatwA} followed by
10797 switch @code{-gnatwd} will suppress all optional warnings except
10798 the warnings for implicit dereferencing.
10799 @end table
10800
10801 @geindex -gnatw.a (gcc)
10802
10803
10804 @table @asis
10805
10806 @item @code{-gnatw.a}
10807
10808 @emph{Activate warnings on failing assertions.}
10809
10810 @geindex Assert failures
10811
10812 This switch activates warnings for assertions where the compiler can tell at
10813 compile time that the assertion will fail. Note that this warning is given
10814 even if assertions are disabled. The default is that such warnings are
10815 generated.
10816 @end table
10817
10818 @geindex -gnatw.A (gcc)
10819
10820
10821 @table @asis
10822
10823 @item @code{-gnatw.A}
10824
10825 @emph{Suppress warnings on failing assertions.}
10826
10827 @geindex Assert failures
10828
10829 This switch suppresses warnings for assertions where the compiler can tell at
10830 compile time that the assertion will fail.
10831 @end table
10832
10833 @geindex -gnatw_a
10834
10835
10836 @table @asis
10837
10838 @item @code{-gnatw_a}
10839
10840 @emph{Activate warnings on anonymous allocators.}
10841
10842 @geindex Anonymous allocators
10843
10844 This switch activates warnings for allocators of anonymous access types,
10845 which can involve run-time accessibility checks and lead to unexpected
10846 accessibility violations. For more details on the rules involved, see
10847 RM 3.10.2 (14).
10848 @end table
10849
10850 @geindex -gnatw_A
10851
10852
10853 @table @asis
10854
10855 @item @code{-gnatw_A}
10856
10857 @emph{Supress warnings on anonymous allocators.}
10858
10859 @geindex Anonymous allocators
10860
10861 This switch suppresses warnings for anonymous access type allocators.
10862 @end table
10863
10864 @geindex -gnatwb (gcc)
10865
10866
10867 @table @asis
10868
10869 @item @code{-gnatwb}
10870
10871 @emph{Activate warnings on bad fixed values.}
10872
10873 @geindex Bad fixed values
10874
10875 @geindex Fixed-point Small value
10876
10877 @geindex Small value
10878
10879 This switch activates warnings for static fixed-point expressions whose
10880 value is not an exact multiple of Small. Such values are implementation
10881 dependent, since an implementation is free to choose either of the multiples
10882 that surround the value. GNAT always chooses the closer one, but this is not
10883 required behavior, and it is better to specify a value that is an exact
10884 multiple, ensuring predictable execution. The default is that such warnings
10885 are not generated.
10886 @end table
10887
10888 @geindex -gnatwB (gcc)
10889
10890
10891 @table @asis
10892
10893 @item @code{-gnatwB}
10894
10895 @emph{Suppress warnings on bad fixed values.}
10896
10897 This switch suppresses warnings for static fixed-point expressions whose
10898 value is not an exact multiple of Small.
10899 @end table
10900
10901 @geindex -gnatw.b (gcc)
10902
10903
10904 @table @asis
10905
10906 @item @code{-gnatw.b}
10907
10908 @emph{Activate warnings on biased representation.}
10909
10910 @geindex Biased representation
10911
10912 This switch activates warnings when a size clause, value size clause, component
10913 clause, or component size clause forces the use of biased representation for an
10914 integer type (e.g. representing a range of 10..11 in a single bit by using 0/1
10915 to represent 10/11). The default is that such warnings are generated.
10916 @end table
10917
10918 @geindex -gnatwB (gcc)
10919
10920
10921 @table @asis
10922
10923 @item @code{-gnatw.B}
10924
10925 @emph{Suppress warnings on biased representation.}
10926
10927 This switch suppresses warnings for representation clauses that force the use
10928 of biased representation.
10929 @end table
10930
10931 @geindex -gnatwc (gcc)
10932
10933
10934 @table @asis
10935
10936 @item @code{-gnatwc}
10937
10938 @emph{Activate warnings on conditionals.}
10939
10940 @geindex Conditionals
10941 @geindex constant
10942
10943 This switch activates warnings for conditional expressions used in
10944 tests that are known to be True or False at compile time. The default
10945 is that such warnings are not generated.
10946 Note that this warning does
10947 not get issued for the use of boolean variables or constants whose
10948 values are known at compile time, since this is a standard technique
10949 for conditional compilation in Ada, and this would generate too many
10950 false positive warnings.
10951
10952 This warning option also activates a special test for comparisons using
10953 the operators ‘>=’ and’ <=’.
10954 If the compiler can tell that only the equality condition is possible,
10955 then it will warn that the ‘>’ or ‘<’ part of the test
10956 is useless and that the operator could be replaced by ‘=’.
10957 An example would be comparing a @code{Natural} variable <= 0.
10958
10959 This warning option also generates warnings if
10960 one or both tests is optimized away in a membership test for integer
10961 values if the result can be determined at compile time. Range tests on
10962 enumeration types are not included, since it is common for such tests
10963 to include an end point.
10964
10965 This warning can also be turned on using @code{-gnatwa}.
10966 @end table
10967
10968 @geindex -gnatwC (gcc)
10969
10970
10971 @table @asis
10972
10973 @item @code{-gnatwC}
10974
10975 @emph{Suppress warnings on conditionals.}
10976
10977 This switch suppresses warnings for conditional expressions used in
10978 tests that are known to be True or False at compile time.
10979 @end table
10980
10981 @geindex -gnatw.c (gcc)
10982
10983
10984 @table @asis
10985
10986 @item @code{-gnatw.c}
10987
10988 @emph{Activate warnings on missing component clauses.}
10989
10990 @geindex Component clause
10991 @geindex missing
10992
10993 This switch activates warnings for record components where a record
10994 representation clause is present and has component clauses for the
10995 majority, but not all, of the components. A warning is given for each
10996 component for which no component clause is present.
10997 @end table
10998
10999 @geindex -gnatw.C (gcc)
11000
11001
11002 @table @asis
11003
11004 @item @code{-gnatw.C}
11005
11006 @emph{Suppress warnings on missing component clauses.}
11007
11008 This switch suppresses warnings for record components that are
11009 missing a component clause in the situation described above.
11010 @end table
11011
11012 @geindex -gnatw_c (gcc)
11013
11014
11015 @table @asis
11016
11017 @item @code{-gnatw_c}
11018
11019 @emph{Activate warnings on unknown condition in Compile_Time_Warning.}
11020
11021 @geindex Compile_Time_Warning
11022
11023 @geindex Compile_Time_Error
11024
11025 This switch activates warnings on a pragma Compile_Time_Warning
11026 or Compile_Time_Error whose condition has a value that is not
11027 known at compile time.
11028 The default is that such warnings are generated.
11029 @end table
11030
11031 @geindex -gnatw_C (gcc)
11032
11033
11034 @table @asis
11035
11036 @item @code{-gnatw_C}
11037
11038 @emph{Suppress warnings on unknown condition in Compile_Time_Warning.}
11039
11040 This switch supresses warnings on a pragma Compile_Time_Warning
11041 or Compile_Time_Error whose condition has a value that is not
11042 known at compile time.
11043 @end table
11044
11045 @geindex -gnatwd (gcc)
11046
11047
11048 @table @asis
11049
11050 @item @code{-gnatwd}
11051
11052 @emph{Activate warnings on implicit dereferencing.}
11053
11054 If this switch is set, then the use of a prefix of an access type
11055 in an indexed component, slice, or selected component without an
11056 explicit @code{.all} will generate a warning. With this warning
11057 enabled, access checks occur only at points where an explicit
11058 @code{.all} appears in the source code (assuming no warnings are
11059 generated as a result of this switch). The default is that such
11060 warnings are not generated.
11061 @end table
11062
11063 @geindex -gnatwD (gcc)
11064
11065
11066 @table @asis
11067
11068 @item @code{-gnatwD}
11069
11070 @emph{Suppress warnings on implicit dereferencing.}
11071
11072 @geindex Implicit dereferencing
11073
11074 @geindex Dereferencing
11075 @geindex implicit
11076
11077 This switch suppresses warnings for implicit dereferences in
11078 indexed components, slices, and selected components.
11079 @end table
11080
11081 @geindex -gnatw.d (gcc)
11082
11083
11084 @table @asis
11085
11086 @item @code{-gnatw.d}
11087
11088 @emph{Activate tagging of warning and info messages.}
11089
11090 If this switch is set, then warning messages are tagged, with one of the
11091 following strings:
11092
11093 @quotation
11094
11095
11096 @itemize -
11097
11098 @item
11099 @emph{[-gnatw?]}
11100 Used to tag warnings controlled by the switch @code{-gnatwx} where x
11101 is a letter a-z.
11102
11103 @item
11104 @emph{[-gnatw.?]}
11105 Used to tag warnings controlled by the switch @code{-gnatw.x} where x
11106 is a letter a-z.
11107
11108 @item
11109 @emph{[-gnatel]}
11110 Used to tag elaboration information (info) messages generated when the
11111 static model of elaboration is used and the @code{-gnatel} switch is set.
11112
11113 @item
11114 @emph{[restriction warning]}
11115 Used to tag warning messages for restriction violations, activated by use
11116 of the pragma @code{Restriction_Warnings}.
11117
11118 @item
11119 @emph{[warning-as-error]}
11120 Used to tag warning messages that have been converted to error messages by
11121 use of the pragma Warning_As_Error. Note that such warnings are prefixed by
11122 the string “error: ” rather than “warning: “.
11123
11124 @item
11125 @emph{[enabled by default]}
11126 Used to tag all other warnings that are always given by default, unless
11127 warnings are completely suppressed using pragma @emph{Warnings(Off)} or
11128 the switch @code{-gnatws}.
11129 @end itemize
11130 @end quotation
11131 @end table
11132
11133 @geindex -gnatw.d (gcc)
11134
11135
11136 @table @asis
11137
11138 @item @code{-gnatw.D}
11139
11140 @emph{Deactivate tagging of warning and info messages messages.}
11141
11142 If this switch is set, then warning messages return to the default
11143 mode in which warnings and info messages are not tagged as described above for
11144 @code{-gnatw.d}.
11145 @end table
11146
11147 @geindex -gnatwe (gcc)
11148
11149 @geindex Warnings
11150 @geindex treat as error
11151
11152
11153 @table @asis
11154
11155 @item @code{-gnatwe}
11156
11157 @emph{Treat warnings and style checks as errors.}
11158
11159 This switch causes warning messages and style check messages to be
11160 treated as errors.
11161 The warning string still appears, but the warning messages are counted
11162 as errors, and prevent the generation of an object file. Note that this
11163 is the only -gnatw switch that affects the handling of style check messages.
11164 Note also that this switch has no effect on info (information) messages, which
11165 are not treated as errors if this switch is present.
11166 @end table
11167
11168 @geindex -gnatw.e (gcc)
11169
11170
11171 @table @asis
11172
11173 @item @code{-gnatw.e}
11174
11175 @emph{Activate every optional warning.}
11176
11177 @geindex Warnings
11178 @geindex activate every optional warning
11179
11180 This switch activates all optional warnings, including those which
11181 are not activated by @code{-gnatwa}. The use of this switch is not
11182 recommended for normal use. If you turn this switch on, it is almost
11183 certain that you will get large numbers of useless warnings. The
11184 warnings that are excluded from @code{-gnatwa} are typically highly
11185 specialized warnings that are suitable for use only in code that has
11186 been specifically designed according to specialized coding rules.
11187 @end table
11188
11189 @geindex -gnatwE (gcc)
11190
11191 @geindex Warnings
11192 @geindex treat as error
11193
11194
11195 @table @asis
11196
11197 @item @code{-gnatwE}
11198
11199 @emph{Treat all run-time exception warnings as errors.}
11200
11201 This switch causes warning messages regarding errors that will be raised
11202 during run-time execution to be treated as errors.
11203 @end table
11204
11205 @geindex -gnatwf (gcc)
11206
11207
11208 @table @asis
11209
11210 @item @code{-gnatwf}
11211
11212 @emph{Activate warnings on unreferenced formals.}
11213
11214 @geindex Formals
11215 @geindex unreferenced
11216
11217 This switch causes a warning to be generated if a formal parameter
11218 is not referenced in the body of the subprogram. This warning can
11219 also be turned on using @code{-gnatwu}. The
11220 default is that these warnings are not generated.
11221 @end table
11222
11223 @geindex -gnatwF (gcc)
11224
11225
11226 @table @asis
11227
11228 @item @code{-gnatwF}
11229
11230 @emph{Suppress warnings on unreferenced formals.}
11231
11232 This switch suppresses warnings for unreferenced formal
11233 parameters. Note that the
11234 combination @code{-gnatwu} followed by @code{-gnatwF} has the
11235 effect of warning on unreferenced entities other than subprogram
11236 formals.
11237 @end table
11238
11239 @geindex -gnatwg (gcc)
11240
11241
11242 @table @asis
11243
11244 @item @code{-gnatwg}
11245
11246 @emph{Activate warnings on unrecognized pragmas.}
11247
11248 @geindex Pragmas
11249 @geindex unrecognized
11250
11251 This switch causes a warning to be generated if an unrecognized
11252 pragma is encountered. Apart from issuing this warning, the
11253 pragma is ignored and has no effect. The default
11254 is that such warnings are issued (satisfying the Ada Reference
11255 Manual requirement that such warnings appear).
11256 @end table
11257
11258 @geindex -gnatwG (gcc)
11259
11260
11261 @table @asis
11262
11263 @item @code{-gnatwG}
11264
11265 @emph{Suppress warnings on unrecognized pragmas.}
11266
11267 This switch suppresses warnings for unrecognized pragmas.
11268 @end table
11269
11270 @geindex -gnatw.g (gcc)
11271
11272
11273 @table @asis
11274
11275 @item @code{-gnatw.g}
11276
11277 @emph{Warnings used for GNAT sources.}
11278
11279 This switch sets the warning categories that are used by the standard
11280 GNAT style. Currently this is equivalent to
11281 @code{-gnatwAao.q.s.CI.V.X.Z}
11282 but more warnings may be added in the future without advanced notice.
11283 @end table
11284
11285 @geindex -gnatwh (gcc)
11286
11287
11288 @table @asis
11289
11290 @item @code{-gnatwh}
11291
11292 @emph{Activate warnings on hiding.}
11293
11294 @geindex Hiding of Declarations
11295
11296 This switch activates warnings on hiding declarations that are considered
11297 potentially confusing. Not all cases of hiding cause warnings; for example an
11298 overriding declaration hides an implicit declaration, which is just normal
11299 code. The default is that warnings on hiding are not generated.
11300 @end table
11301
11302 @geindex -gnatwH (gcc)
11303
11304
11305 @table @asis
11306
11307 @item @code{-gnatwH}
11308
11309 @emph{Suppress warnings on hiding.}
11310
11311 This switch suppresses warnings on hiding declarations.
11312 @end table
11313
11314 @geindex -gnatw.h (gcc)
11315
11316
11317 @table @asis
11318
11319 @item @code{-gnatw.h}
11320
11321 @emph{Activate warnings on holes/gaps in records.}
11322
11323 @geindex Record Representation (gaps)
11324
11325 This switch activates warnings on component clauses in record
11326 representation clauses that leave holes (gaps) in the record layout.
11327 If this warning option is active, then record representation clauses
11328 should specify a contiguous layout, adding unused fill fields if needed.
11329 @end table
11330
11331 @geindex -gnatw.H (gcc)
11332
11333
11334 @table @asis
11335
11336 @item @code{-gnatw.H}
11337
11338 @emph{Suppress warnings on holes/gaps in records.}
11339
11340 This switch suppresses warnings on component clauses in record
11341 representation clauses that leave holes (haps) in the record layout.
11342 @end table
11343
11344 @geindex -gnatwi (gcc)
11345
11346
11347 @table @asis
11348
11349 @item @code{-gnatwi}
11350
11351 @emph{Activate warnings on implementation units.}
11352
11353 This switch activates warnings for a @emph{with} of an internal GNAT
11354 implementation unit, defined as any unit from the @code{Ada},
11355 @code{Interfaces}, @code{GNAT},
11356 or @code{System}
11357 hierarchies that is not
11358 documented in either the Ada Reference Manual or the GNAT
11359 Programmer’s Reference Manual. Such units are intended only
11360 for internal implementation purposes and should not be @emph{with}ed
11361 by user programs. The default is that such warnings are generated
11362 @end table
11363
11364 @geindex -gnatwI (gcc)
11365
11366
11367 @table @asis
11368
11369 @item @code{-gnatwI}
11370
11371 @emph{Disable warnings on implementation units.}
11372
11373 This switch disables warnings for a @emph{with} of an internal GNAT
11374 implementation unit.
11375 @end table
11376
11377 @geindex -gnatw.i (gcc)
11378
11379
11380 @table @asis
11381
11382 @item @code{-gnatw.i}
11383
11384 @emph{Activate warnings on overlapping actuals.}
11385
11386 This switch enables a warning on statically detectable overlapping actuals in
11387 a subprogram call, when one of the actuals is an in-out parameter, and the
11388 types of the actuals are not by-copy types. This warning is off by default.
11389 @end table
11390
11391 @geindex -gnatw.I (gcc)
11392
11393
11394 @table @asis
11395
11396 @item @code{-gnatw.I}
11397
11398 @emph{Disable warnings on overlapping actuals.}
11399
11400 This switch disables warnings on overlapping actuals in a call.
11401 @end table
11402
11403 @geindex -gnatwj (gcc)
11404
11405
11406 @table @asis
11407
11408 @item @code{-gnatwj}
11409
11410 @emph{Activate warnings on obsolescent features (Annex J).}
11411
11412 @geindex Features
11413 @geindex obsolescent
11414
11415 @geindex Obsolescent features
11416
11417 If this warning option is activated, then warnings are generated for
11418 calls to subprograms marked with @code{pragma Obsolescent} and
11419 for use of features in Annex J of the Ada Reference Manual. In the
11420 case of Annex J, not all features are flagged. In particular use
11421 of the renamed packages (like @code{Text_IO}) and use of package
11422 @code{ASCII} are not flagged, since these are very common and
11423 would generate many annoying positive warnings. The default is that
11424 such warnings are not generated.
11425
11426 In addition to the above cases, warnings are also generated for
11427 GNAT features that have been provided in past versions but which
11428 have been superseded (typically by features in the new Ada standard).
11429 For example, @code{pragma Ravenscar} will be flagged since its
11430 function is replaced by @code{pragma Profile(Ravenscar)}, and
11431 @code{pragma Interface_Name} will be flagged since its function
11432 is replaced by @code{pragma Import}.
11433
11434 Note that this warning option functions differently from the
11435 restriction @code{No_Obsolescent_Features} in two respects.
11436 First, the restriction applies only to annex J features.
11437 Second, the restriction does flag uses of package @code{ASCII}.
11438 @end table
11439
11440 @geindex -gnatwJ (gcc)
11441
11442
11443 @table @asis
11444
11445 @item @code{-gnatwJ}
11446
11447 @emph{Suppress warnings on obsolescent features (Annex J).}
11448
11449 This switch disables warnings on use of obsolescent features.
11450 @end table
11451
11452 @geindex -gnatw.j (gcc)
11453
11454
11455 @table @asis
11456
11457 @item @code{-gnatw.j}
11458
11459 @emph{Activate warnings on late declarations of tagged type primitives.}
11460
11461 This switch activates warnings on visible primitives added to a
11462 tagged type after deriving a private extension from it.
11463 @end table
11464
11465 @geindex -gnatw.J (gcc)
11466
11467
11468 @table @asis
11469
11470 @item @code{-gnatw.J}
11471
11472 @emph{Suppress warnings on late declarations of tagged type primitives.}
11473
11474 This switch suppresses warnings on visible primitives added to a
11475 tagged type after deriving a private extension from it.
11476 @end table
11477
11478 @geindex -gnatwk (gcc)
11479
11480
11481 @table @asis
11482
11483 @item @code{-gnatwk}
11484
11485 @emph{Activate warnings on variables that could be constants.}
11486
11487 This switch activates warnings for variables that are initialized but
11488 never modified, and then could be declared constants. The default is that
11489 such warnings are not given.
11490 @end table
11491
11492 @geindex -gnatwK (gcc)
11493
11494
11495 @table @asis
11496
11497 @item @code{-gnatwK}
11498
11499 @emph{Suppress warnings on variables that could be constants.}
11500
11501 This switch disables warnings on variables that could be declared constants.
11502 @end table
11503
11504 @geindex -gnatw.k (gcc)
11505
11506
11507 @table @asis
11508
11509 @item @code{-gnatw.k}
11510
11511 @emph{Activate warnings on redefinition of names in standard.}
11512
11513 This switch activates warnings for declarations that declare a name that
11514 is defined in package Standard. Such declarations can be confusing,
11515 especially since the names in package Standard continue to be directly
11516 visible, meaning that use visibiliy on such redeclared names does not
11517 work as expected. Names of discriminants and components in records are
11518 not included in this check.
11519 @end table
11520
11521 @geindex -gnatwK (gcc)
11522
11523
11524 @table @asis
11525
11526 @item @code{-gnatw.K}
11527
11528 @emph{Suppress warnings on redefinition of names in standard.}
11529
11530 This switch disables warnings for declarations that declare a name that
11531 is defined in package Standard.
11532 @end table
11533
11534 @geindex -gnatwl (gcc)
11535
11536
11537 @table @asis
11538
11539 @item @code{-gnatwl}
11540
11541 @emph{Activate warnings for elaboration pragmas.}
11542
11543 @geindex Elaboration
11544 @geindex warnings
11545
11546 This switch activates warnings for possible elaboration problems,
11547 including suspicious use
11548 of @code{Elaborate} pragmas, when using the static elaboration model, and
11549 possible situations that may raise @code{Program_Error} when using the
11550 dynamic elaboration model.
11551 See the section in this guide on elaboration checking for further details.
11552 The default is that such warnings
11553 are not generated.
11554 @end table
11555
11556 @geindex -gnatwL (gcc)
11557
11558
11559 @table @asis
11560
11561 @item @code{-gnatwL}
11562
11563 @emph{Suppress warnings for elaboration pragmas.}
11564
11565 This switch suppresses warnings for possible elaboration problems.
11566 @end table
11567
11568 @geindex -gnatw.l (gcc)
11569
11570
11571 @table @asis
11572
11573 @item @code{-gnatw.l}
11574
11575 @emph{List inherited aspects.}
11576
11577 This switch causes the compiler to list inherited invariants,
11578 preconditions, and postconditions from Type_Invariant’Class, Invariant’Class,
11579 Pre’Class, and Post’Class aspects. Also list inherited subtype predicates.
11580 @end table
11581
11582 @geindex -gnatw.L (gcc)
11583
11584
11585 @table @asis
11586
11587 @item @code{-gnatw.L}
11588
11589 @emph{Suppress listing of inherited aspects.}
11590
11591 This switch suppresses listing of inherited aspects.
11592 @end table
11593
11594 @geindex -gnatwm (gcc)
11595
11596
11597 @table @asis
11598
11599 @item @code{-gnatwm}
11600
11601 @emph{Activate warnings on modified but unreferenced variables.}
11602
11603 This switch activates warnings for variables that are assigned (using
11604 an initialization value or with one or more assignment statements) but
11605 whose value is never read. The warning is suppressed for volatile
11606 variables and also for variables that are renamings of other variables
11607 or for which an address clause is given.
11608 The default is that these warnings are not given.
11609 @end table
11610
11611 @geindex -gnatwM (gcc)
11612
11613
11614 @table @asis
11615
11616 @item @code{-gnatwM}
11617
11618 @emph{Disable warnings on modified but unreferenced variables.}
11619
11620 This switch disables warnings for variables that are assigned or
11621 initialized, but never read.
11622 @end table
11623
11624 @geindex -gnatw.m (gcc)
11625
11626
11627 @table @asis
11628
11629 @item @code{-gnatw.m}
11630
11631 @emph{Activate warnings on suspicious modulus values.}
11632
11633 This switch activates warnings for modulus values that seem suspicious.
11634 The cases caught are where the size is the same as the modulus (e.g.
11635 a modulus of 7 with a size of 7 bits), and modulus values of 32 or 64
11636 with no size clause. The guess in both cases is that 2**x was intended
11637 rather than x. In addition expressions of the form 2*x for small x
11638 generate a warning (the almost certainly accurate guess being that
11639 2**x was intended). This switch also activates warnings for negative
11640 literal values of a modular type, which are interpreted as large positive
11641 integers after wrap-around. The default is that these warnings are given.
11642 @end table
11643
11644 @geindex -gnatw.M (gcc)
11645
11646
11647 @table @asis
11648
11649 @item @code{-gnatw.M}
11650
11651 @emph{Disable warnings on suspicious modulus values.}
11652
11653 This switch disables warnings for suspicious modulus values.
11654 @end table
11655
11656 @geindex -gnatwn (gcc)
11657
11658
11659 @table @asis
11660
11661 @item @code{-gnatwn}
11662
11663 @emph{Set normal warnings mode.}
11664
11665 This switch sets normal warning mode, in which enabled warnings are
11666 issued and treated as warnings rather than errors. This is the default
11667 mode. the switch @code{-gnatwn} can be used to cancel the effect of
11668 an explicit @code{-gnatws} or
11669 @code{-gnatwe}. It also cancels the effect of the
11670 implicit @code{-gnatwe} that is activated by the
11671 use of @code{-gnatg}.
11672 @end table
11673
11674 @geindex -gnatw.n (gcc)
11675
11676 @geindex Atomic Synchronization
11677 @geindex warnings
11678
11679
11680 @table @asis
11681
11682 @item @code{-gnatw.n}
11683
11684 @emph{Activate warnings on atomic synchronization.}
11685
11686 This switch actives warnings when an access to an atomic variable
11687 requires the generation of atomic synchronization code. These
11688 warnings are off by default.
11689 @end table
11690
11691 @geindex -gnatw.N (gcc)
11692
11693
11694 @table @asis
11695
11696 @item @code{-gnatw.N}
11697
11698 @emph{Suppress warnings on atomic synchronization.}
11699
11700 @geindex Atomic Synchronization
11701 @geindex warnings
11702
11703 This switch suppresses warnings when an access to an atomic variable
11704 requires the generation of atomic synchronization code.
11705 @end table
11706
11707 @geindex -gnatwo (gcc)
11708
11709 @geindex Address Clauses
11710 @geindex warnings
11711
11712
11713 @table @asis
11714
11715 @item @code{-gnatwo}
11716
11717 @emph{Activate warnings on address clause overlays.}
11718
11719 This switch activates warnings for possibly unintended initialization
11720 effects of defining address clauses that cause one variable to overlap
11721 another. The default is that such warnings are generated.
11722 @end table
11723
11724 @geindex -gnatwO (gcc)
11725
11726
11727 @table @asis
11728
11729 @item @code{-gnatwO}
11730
11731 @emph{Suppress warnings on address clause overlays.}
11732
11733 This switch suppresses warnings on possibly unintended initialization
11734 effects of defining address clauses that cause one variable to overlap
11735 another.
11736 @end table
11737
11738 @geindex -gnatw.o (gcc)
11739
11740
11741 @table @asis
11742
11743 @item @code{-gnatw.o}
11744
11745 @emph{Activate warnings on modified but unreferenced out parameters.}
11746
11747 This switch activates warnings for variables that are modified by using
11748 them as actuals for a call to a procedure with an out mode formal, where
11749 the resulting assigned value is never read. It is applicable in the case
11750 where there is more than one out mode formal. If there is only one out
11751 mode formal, the warning is issued by default (controlled by -gnatwu).
11752 The warning is suppressed for volatile
11753 variables and also for variables that are renamings of other variables
11754 or for which an address clause is given.
11755 The default is that these warnings are not given.
11756 @end table
11757
11758 @geindex -gnatw.O (gcc)
11759
11760
11761 @table @asis
11762
11763 @item @code{-gnatw.O}
11764
11765 @emph{Disable warnings on modified but unreferenced out parameters.}
11766
11767 This switch suppresses warnings for variables that are modified by using
11768 them as actuals for a call to a procedure with an out mode formal, where
11769 the resulting assigned value is never read.
11770 @end table
11771
11772 @geindex -gnatwp (gcc)
11773
11774 @geindex Inlining
11775 @geindex warnings
11776
11777
11778 @table @asis
11779
11780 @item @code{-gnatwp}
11781
11782 @emph{Activate warnings on ineffective pragma Inlines.}
11783
11784 This switch activates warnings for failure of front end inlining
11785 (activated by @code{-gnatN}) to inline a particular call. There are
11786 many reasons for not being able to inline a call, including most
11787 commonly that the call is too complex to inline. The default is
11788 that such warnings are not given.
11789 Warnings on ineffective inlining by the gcc back-end can be activated
11790 separately, using the gcc switch -Winline.
11791 @end table
11792
11793 @geindex -gnatwP (gcc)
11794
11795
11796 @table @asis
11797
11798 @item @code{-gnatwP}
11799
11800 @emph{Suppress warnings on ineffective pragma Inlines.}
11801
11802 This switch suppresses warnings on ineffective pragma Inlines. If the
11803 inlining mechanism cannot inline a call, it will simply ignore the
11804 request silently.
11805 @end table
11806
11807 @geindex -gnatw.p (gcc)
11808
11809 @geindex Parameter order
11810 @geindex warnings
11811
11812
11813 @table @asis
11814
11815 @item @code{-gnatw.p}
11816
11817 @emph{Activate warnings on parameter ordering.}
11818
11819 This switch activates warnings for cases of suspicious parameter
11820 ordering when the list of arguments are all simple identifiers that
11821 match the names of the formals, but are in a different order. The
11822 warning is suppressed if any use of named parameter notation is used,
11823 so this is the appropriate way to suppress a false positive (and
11824 serves to emphasize that the “misordering” is deliberate). The
11825 default is that such warnings are not given.
11826 @end table
11827
11828 @geindex -gnatw.P (gcc)
11829
11830
11831 @table @asis
11832
11833 @item @code{-gnatw.P}
11834
11835 @emph{Suppress warnings on parameter ordering.}
11836
11837 This switch suppresses warnings on cases of suspicious parameter
11838 ordering.
11839 @end table
11840
11841 @geindex -gnatwq (gcc)
11842
11843 @geindex Parentheses
11844 @geindex warnings
11845
11846
11847 @table @asis
11848
11849 @item @code{-gnatwq}
11850
11851 @emph{Activate warnings on questionable missing parentheses.}
11852
11853 This switch activates warnings for cases where parentheses are not used and
11854 the result is potential ambiguity from a readers point of view. For example
11855 (not a > b) when a and b are modular means ((not a) > b) and very likely the
11856 programmer intended (not (a > b)). Similarly (-x mod 5) means (-(x mod 5)) and
11857 quite likely ((-x) mod 5) was intended. In such situations it seems best to
11858 follow the rule of always parenthesizing to make the association clear, and
11859 this warning switch warns if such parentheses are not present. The default
11860 is that these warnings are given.
11861 @end table
11862
11863 @geindex -gnatwQ (gcc)
11864
11865
11866 @table @asis
11867
11868 @item @code{-gnatwQ}
11869
11870 @emph{Suppress warnings on questionable missing parentheses.}
11871
11872 This switch suppresses warnings for cases where the association is not
11873 clear and the use of parentheses is preferred.
11874 @end table
11875
11876 @geindex -gnatw.q (gcc)
11877
11878 @geindex Layout
11879 @geindex warnings
11880
11881
11882 @table @asis
11883
11884 @item @code{-gnatw.q}
11885
11886 @emph{Activate warnings on questionable layout of record types.}
11887
11888 This switch activates warnings for cases where the default layout of
11889 a record type, that is to say the layout of its components in textual
11890 order of the source code, would very likely cause inefficiencies in
11891 the code generated by the compiler, both in terms of space and speed
11892 during execution. One warning is issued for each problematic component
11893 without representation clause in the nonvariant part and then in each
11894 variant recursively, if any.
11895
11896 The purpose of these warnings is neither to prescribe an optimal layout
11897 nor to force the use of representation clauses, but rather to get rid of
11898 the most blatant inefficiencies in the layout. Therefore, the default
11899 layout is matched against the following synthetic ordered layout and
11900 the deviations are flagged on a component-by-component basis:
11901
11902
11903 @itemize *
11904
11905 @item
11906 first all components or groups of components whose length is fixed
11907 and a multiple of the storage unit,
11908
11909 @item
11910 then the remaining components whose length is fixed and not a multiple
11911 of the storage unit,
11912
11913 @item
11914 then the remaining components whose length doesn’t depend on discriminants
11915 (that is to say, with variable but uniform length for all objects),
11916
11917 @item
11918 then all components whose length depends on discriminants,
11919
11920 @item
11921 finally the variant part (if any),
11922 @end itemize
11923
11924 for the nonvariant part and for each variant recursively, if any.
11925
11926 The exact wording of the warning depends on whether the compiler is allowed
11927 to reorder the components in the record type or precluded from doing it by
11928 means of pragma @code{No_Component_Reordering}.
11929
11930 The default is that these warnings are not given.
11931 @end table
11932
11933 @geindex -gnatw.Q (gcc)
11934
11935
11936 @table @asis
11937
11938 @item @code{-gnatw.Q}
11939
11940 @emph{Suppress warnings on questionable layout of record types.}
11941
11942 This switch suppresses warnings for cases where the default layout of
11943 a record type would very likely cause inefficiencies.
11944 @end table
11945
11946 @geindex -gnatwr (gcc)
11947
11948
11949 @table @asis
11950
11951 @item @code{-gnatwr}
11952
11953 @emph{Activate warnings on redundant constructs.}
11954
11955 This switch activates warnings for redundant constructs. The following
11956 is the current list of constructs regarded as redundant:
11957
11958
11959 @itemize *
11960
11961 @item
11962 Assignment of an item to itself.
11963
11964 @item
11965 Type conversion that converts an expression to its own type.
11966
11967 @item
11968 Use of the attribute @code{Base} where @code{typ'Base} is the same
11969 as @code{typ}.
11970
11971 @item
11972 Use of pragma @code{Pack} when all components are placed by a record
11973 representation clause.
11974
11975 @item
11976 Exception handler containing only a reraise statement (raise with no
11977 operand) which has no effect.
11978
11979 @item
11980 Use of the operator abs on an operand that is known at compile time
11981 to be non-negative
11982
11983 @item
11984 Comparison of an object or (unary or binary) operation of boolean type to
11985 an explicit True value.
11986 @end itemize
11987
11988 The default is that warnings for redundant constructs are not given.
11989 @end table
11990
11991 @geindex -gnatwR (gcc)
11992
11993
11994 @table @asis
11995
11996 @item @code{-gnatwR}
11997
11998 @emph{Suppress warnings on redundant constructs.}
11999
12000 This switch suppresses warnings for redundant constructs.
12001 @end table
12002
12003 @geindex -gnatw.r (gcc)
12004
12005
12006 @table @asis
12007
12008 @item @code{-gnatw.r}
12009
12010 @emph{Activate warnings for object renaming function.}
12011
12012 This switch activates warnings for an object renaming that renames a
12013 function call, which is equivalent to a constant declaration (as
12014 opposed to renaming the function itself). The default is that these
12015 warnings are given.
12016 @end table
12017
12018 @geindex -gnatw.R (gcc)
12019
12020
12021 @table @asis
12022
12023 @item @code{-gnatw.R}
12024
12025 @emph{Suppress warnings for object renaming function.}
12026
12027 This switch suppresses warnings for object renaming function.
12028 @end table
12029
12030 @geindex -gnatw_r (gcc)
12031
12032
12033 @table @asis
12034
12035 @item @code{-gnatw_r}
12036
12037 @emph{Activate warnings for out-of-order record representation clauses.}
12038
12039 This switch activates warnings for record representation clauses,
12040 if the order of component declarations, component clauses,
12041 and bit-level layout do not all agree.
12042 The default is that these warnings are not given.
12043 @end table
12044
12045 @geindex -gnatw_R (gcc)
12046
12047
12048 @table @asis
12049
12050 @item @code{-gnatw_R}
12051
12052 @emph{Suppress warnings for out-of-order record representation clauses.}
12053 @end table
12054
12055 @geindex -gnatws (gcc)
12056
12057
12058 @table @asis
12059
12060 @item @code{-gnatws}
12061
12062 @emph{Suppress all warnings.}
12063
12064 This switch completely suppresses the
12065 output of all warning messages from the GNAT front end, including
12066 both warnings that can be controlled by switches described in this
12067 section, and those that are normally given unconditionally. The
12068 effect of this suppress action can only be cancelled by a subsequent
12069 use of the switch @code{-gnatwn}.
12070
12071 Note that switch @code{-gnatws} does not suppress
12072 warnings from the @code{gcc} back end.
12073 To suppress these back end warnings as well, use the switch @code{-w}
12074 in addition to @code{-gnatws}. Also this switch has no effect on the
12075 handling of style check messages.
12076 @end table
12077
12078 @geindex -gnatw.s (gcc)
12079
12080 @geindex Record Representation (component sizes)
12081
12082
12083 @table @asis
12084
12085 @item @code{-gnatw.s}
12086
12087 @emph{Activate warnings on overridden size clauses.}
12088
12089 This switch activates warnings on component clauses in record
12090 representation clauses where the length given overrides that
12091 specified by an explicit size clause for the component type. A
12092 warning is similarly given in the array case if a specified
12093 component size overrides an explicit size clause for the array
12094 component type.
12095 @end table
12096
12097 @geindex -gnatw.S (gcc)
12098
12099
12100 @table @asis
12101
12102 @item @code{-gnatw.S}
12103
12104 @emph{Suppress warnings on overridden size clauses.}
12105
12106 This switch suppresses warnings on component clauses in record
12107 representation clauses that override size clauses, and similar
12108 warnings when an array component size overrides a size clause.
12109 @end table
12110
12111 @geindex -gnatwt (gcc)
12112
12113 @geindex Deactivated code
12114 @geindex warnings
12115
12116 @geindex Deleted code
12117 @geindex warnings
12118
12119
12120 @table @asis
12121
12122 @item @code{-gnatwt}
12123
12124 @emph{Activate warnings for tracking of deleted conditional code.}
12125
12126 This switch activates warnings for tracking of code in conditionals (IF and
12127 CASE statements) that is detected to be dead code which cannot be executed, and
12128 which is removed by the front end. This warning is off by default. This may be
12129 useful for detecting deactivated code in certified applications.
12130 @end table
12131
12132 @geindex -gnatwT (gcc)
12133
12134
12135 @table @asis
12136
12137 @item @code{-gnatwT}
12138
12139 @emph{Suppress warnings for tracking of deleted conditional code.}
12140
12141 This switch suppresses warnings for tracking of deleted conditional code.
12142 @end table
12143
12144 @geindex -gnatw.t (gcc)
12145
12146
12147 @table @asis
12148
12149 @item @code{-gnatw.t}
12150
12151 @emph{Activate warnings on suspicious contracts.}
12152
12153 This switch activates warnings on suspicious contracts. This includes
12154 warnings on suspicious postconditions (whether a pragma @code{Postcondition} or a
12155 @code{Post} aspect in Ada 2012) and suspicious contract cases (pragma or aspect
12156 @code{Contract_Cases}). A function postcondition or contract case is suspicious
12157 when no postcondition or contract case for this function mentions the result
12158 of the function. A procedure postcondition or contract case is suspicious
12159 when it only refers to the pre-state of the procedure, because in that case
12160 it should rather be expressed as a precondition. This switch also controls
12161 warnings on suspicious cases of expressions typically found in contracts like
12162 quantified expressions and uses of Update attribute. The default is that such
12163 warnings are generated.
12164 @end table
12165
12166 @geindex -gnatw.T (gcc)
12167
12168
12169 @table @asis
12170
12171 @item @code{-gnatw.T}
12172
12173 @emph{Suppress warnings on suspicious contracts.}
12174
12175 This switch suppresses warnings on suspicious contracts.
12176 @end table
12177
12178 @geindex -gnatwu (gcc)
12179
12180
12181 @table @asis
12182
12183 @item @code{-gnatwu}
12184
12185 @emph{Activate warnings on unused entities.}
12186
12187 This switch activates warnings to be generated for entities that
12188 are declared but not referenced, and for units that are @emph{with}ed
12189 and not
12190 referenced. In the case of packages, a warning is also generated if
12191 no entities in the package are referenced. This means that if a with’ed
12192 package is referenced but the only references are in @code{use}
12193 clauses or @code{renames}
12194 declarations, a warning is still generated. A warning is also generated
12195 for a generic package that is @emph{with}ed but never instantiated.
12196 In the case where a package or subprogram body is compiled, and there
12197 is a @emph{with} on the corresponding spec
12198 that is only referenced in the body,
12199 a warning is also generated, noting that the
12200 @emph{with} can be moved to the body. The default is that
12201 such warnings are not generated.
12202 This switch also activates warnings on unreferenced formals
12203 (it includes the effect of @code{-gnatwf}).
12204 @end table
12205
12206 @geindex -gnatwU (gcc)
12207
12208
12209 @table @asis
12210
12211 @item @code{-gnatwU}
12212
12213 @emph{Suppress warnings on unused entities.}
12214
12215 This switch suppresses warnings for unused entities and packages.
12216 It also turns off warnings on unreferenced formals (and thus includes
12217 the effect of @code{-gnatwF}).
12218 @end table
12219
12220 @geindex -gnatw.u (gcc)
12221
12222
12223 @table @asis
12224
12225 @item @code{-gnatw.u}
12226
12227 @emph{Activate warnings on unordered enumeration types.}
12228
12229 This switch causes enumeration types to be considered as conceptually
12230 unordered, unless an explicit pragma @code{Ordered} is given for the type.
12231 The effect is to generate warnings in clients that use explicit comparisons
12232 or subranges, since these constructs both treat objects of the type as
12233 ordered. (A @emph{client} is defined as a unit that is other than the unit in
12234 which the type is declared, or its body or subunits.) Please refer to
12235 the description of pragma @code{Ordered} in the
12236 @cite{GNAT Reference Manual} for further details.
12237 The default is that such warnings are not generated.
12238 @end table
12239
12240 @geindex -gnatw.U (gcc)
12241
12242
12243 @table @asis
12244
12245 @item @code{-gnatw.U}
12246
12247 @emph{Deactivate warnings on unordered enumeration types.}
12248
12249 This switch causes all enumeration types to be considered as ordered, so
12250 that no warnings are given for comparisons or subranges for any type.
12251 @end table
12252
12253 @geindex -gnatwv (gcc)
12254
12255 @geindex Unassigned variable warnings
12256
12257
12258 @table @asis
12259
12260 @item @code{-gnatwv}
12261
12262 @emph{Activate warnings on unassigned variables.}
12263
12264 This switch activates warnings for access to variables which
12265 may not be properly initialized. The default is that
12266 such warnings are generated. This switch will also be emitted when
12267 initializing an array or record object via the following aggregate:
12268
12269 @example
12270 Array_Or_Record : XXX := (others => <>);
12271 @end example
12272
12273 unless the relevant type fully initializes all components.
12274 @end table
12275
12276 @geindex -gnatwV (gcc)
12277
12278
12279 @table @asis
12280
12281 @item @code{-gnatwV}
12282
12283 @emph{Suppress warnings on unassigned variables.}
12284
12285 This switch suppresses warnings for access to variables which
12286 may not be properly initialized.
12287 @end table
12288
12289 @geindex -gnatw.v (gcc)
12290
12291 @geindex bit order warnings
12292
12293
12294 @table @asis
12295
12296 @item @code{-gnatw.v}
12297
12298 @emph{Activate info messages for non-default bit order.}
12299
12300 This switch activates messages (labeled “info”, they are not warnings,
12301 just informational messages) about the effects of non-default bit-order
12302 on records to which a component clause is applied. The effect of specifying
12303 non-default bit ordering is a bit subtle (and changed with Ada 2005), so
12304 these messages, which are given by default, are useful in understanding the
12305 exact consequences of using this feature.
12306 @end table
12307
12308 @geindex -gnatw.V (gcc)
12309
12310
12311 @table @asis
12312
12313 @item @code{-gnatw.V}
12314
12315 @emph{Suppress info messages for non-default bit order.}
12316
12317 This switch suppresses information messages for the effects of specifying
12318 non-default bit order on record components with component clauses.
12319 @end table
12320
12321 @geindex -gnatww (gcc)
12322
12323 @geindex String indexing warnings
12324
12325
12326 @table @asis
12327
12328 @item @code{-gnatww}
12329
12330 @emph{Activate warnings on wrong low bound assumption.}
12331
12332 This switch activates warnings for indexing an unconstrained string parameter
12333 with a literal or S’Length. This is a case where the code is assuming that the
12334 low bound is one, which is in general not true (for example when a slice is
12335 passed). The default is that such warnings are generated.
12336 @end table
12337
12338 @geindex -gnatwW (gcc)
12339
12340
12341 @table @asis
12342
12343 @item @code{-gnatwW}
12344
12345 @emph{Suppress warnings on wrong low bound assumption.}
12346
12347 This switch suppresses warnings for indexing an unconstrained string parameter
12348 with a literal or S’Length. Note that this warning can also be suppressed
12349 in a particular case by adding an assertion that the lower bound is 1,
12350 as shown in the following example:
12351
12352 @example
12353 procedure K (S : String) is
12354 pragma Assert (S'First = 1);
12355 ...
12356 @end example
12357 @end table
12358
12359 @geindex -gnatw.w (gcc)
12360
12361 @geindex Warnings Off control
12362
12363
12364 @table @asis
12365
12366 @item @code{-gnatw.w}
12367
12368 @emph{Activate warnings on Warnings Off pragmas.}
12369
12370 This switch activates warnings for use of @code{pragma Warnings (Off, entity)}
12371 where either the pragma is entirely useless (because it suppresses no
12372 warnings), or it could be replaced by @code{pragma Unreferenced} or
12373 @code{pragma Unmodified}.
12374 Also activates warnings for the case of
12375 Warnings (Off, String), where either there is no matching
12376 Warnings (On, String), or the Warnings (Off) did not suppress any warning.
12377 The default is that these warnings are not given.
12378 @end table
12379
12380 @geindex -gnatw.W (gcc)
12381
12382
12383 @table @asis
12384
12385 @item @code{-gnatw.W}
12386
12387 @emph{Suppress warnings on unnecessary Warnings Off pragmas.}
12388
12389 This switch suppresses warnings for use of @code{pragma Warnings (Off, ...)}.
12390 @end table
12391
12392 @geindex -gnatwx (gcc)
12393
12394 @geindex Export/Import pragma warnings
12395
12396
12397 @table @asis
12398
12399 @item @code{-gnatwx}
12400
12401 @emph{Activate warnings on Export/Import pragmas.}
12402
12403 This switch activates warnings on Export/Import pragmas when
12404 the compiler detects a possible conflict between the Ada and
12405 foreign language calling sequences. For example, the use of
12406 default parameters in a convention C procedure is dubious
12407 because the C compiler cannot supply the proper default, so
12408 a warning is issued. The default is that such warnings are
12409 generated.
12410 @end table
12411
12412 @geindex -gnatwX (gcc)
12413
12414
12415 @table @asis
12416
12417 @item @code{-gnatwX}
12418
12419 @emph{Suppress warnings on Export/Import pragmas.}
12420
12421 This switch suppresses warnings on Export/Import pragmas.
12422 The sense of this is that you are telling the compiler that
12423 you know what you are doing in writing the pragma, and it
12424 should not complain at you.
12425 @end table
12426
12427 @geindex -gnatwm (gcc)
12428
12429
12430 @table @asis
12431
12432 @item @code{-gnatw.x}
12433
12434 @emph{Activate warnings for No_Exception_Propagation mode.}
12435
12436 This switch activates warnings for exception usage when pragma Restrictions
12437 (No_Exception_Propagation) is in effect. Warnings are given for implicit or
12438 explicit exception raises which are not covered by a local handler, and for
12439 exception handlers which do not cover a local raise. The default is that
12440 these warnings are given for units that contain exception handlers.
12441
12442 @item @code{-gnatw.X}
12443
12444 @emph{Disable warnings for No_Exception_Propagation mode.}
12445
12446 This switch disables warnings for exception usage when pragma Restrictions
12447 (No_Exception_Propagation) is in effect.
12448 @end table
12449
12450 @geindex -gnatwy (gcc)
12451
12452 @geindex Ada compatibility issues warnings
12453
12454
12455 @table @asis
12456
12457 @item @code{-gnatwy}
12458
12459 @emph{Activate warnings for Ada compatibility issues.}
12460
12461 For the most part, newer versions of Ada are upwards compatible
12462 with older versions. For example, Ada 2005 programs will almost
12463 always work when compiled as Ada 2012.
12464 However there are some exceptions (for example the fact that
12465 @code{some} is now a reserved word in Ada 2012). This
12466 switch activates several warnings to help in identifying
12467 and correcting such incompatibilities. The default is that
12468 these warnings are generated. Note that at one point Ada 2005
12469 was called Ada 0Y, hence the choice of character.
12470 @end table
12471
12472 @geindex -gnatwY (gcc)
12473
12474 @geindex Ada compatibility issues warnings
12475
12476
12477 @table @asis
12478
12479 @item @code{-gnatwY}
12480
12481 @emph{Disable warnings for Ada compatibility issues.}
12482
12483 This switch suppresses the warnings intended to help in identifying
12484 incompatibilities between Ada language versions.
12485 @end table
12486
12487 @geindex -gnatw.y (gcc)
12488
12489 @geindex Package spec needing body
12490
12491
12492 @table @asis
12493
12494 @item @code{-gnatw.y}
12495
12496 @emph{Activate information messages for why package spec needs body.}
12497
12498 There are a number of cases in which a package spec needs a body.
12499 For example, the use of pragma Elaborate_Body, or the declaration
12500 of a procedure specification requiring a completion. This switch
12501 causes information messages to be output showing why a package
12502 specification requires a body. This can be useful in the case of
12503 a large package specification which is unexpectedly requiring a
12504 body. The default is that such information messages are not output.
12505 @end table
12506
12507 @geindex -gnatw.Y (gcc)
12508
12509 @geindex No information messages for why package spec needs body
12510
12511
12512 @table @asis
12513
12514 @item @code{-gnatw.Y}
12515
12516 @emph{Disable information messages for why package spec needs body.}
12517
12518 This switch suppresses the output of information messages showing why
12519 a package specification needs a body.
12520 @end table
12521
12522 @geindex -gnatwz (gcc)
12523
12524 @geindex Unchecked_Conversion warnings
12525
12526
12527 @table @asis
12528
12529 @item @code{-gnatwz}
12530
12531 @emph{Activate warnings on unchecked conversions.}
12532
12533 This switch activates warnings for unchecked conversions
12534 where the types are known at compile time to have different
12535 sizes. The default is that such warnings are generated. Warnings are also
12536 generated for subprogram pointers with different conventions.
12537 @end table
12538
12539 @geindex -gnatwZ (gcc)
12540
12541
12542 @table @asis
12543
12544 @item @code{-gnatwZ}
12545
12546 @emph{Suppress warnings on unchecked conversions.}
12547
12548 This switch suppresses warnings for unchecked conversions
12549 where the types are known at compile time to have different
12550 sizes or conventions.
12551 @end table
12552
12553 @geindex -gnatw.z (gcc)
12554
12555 @geindex Size/Alignment warnings
12556
12557
12558 @table @asis
12559
12560 @item @code{-gnatw.z}
12561
12562 @emph{Activate warnings for size not a multiple of alignment.}
12563
12564 This switch activates warnings for cases of array and record types
12565 with specified @code{Size} and @code{Alignment} attributes where the
12566 size is not a multiple of the alignment, resulting in an object
12567 size that is greater than the specified size. The default
12568 is that such warnings are generated.
12569 @end table
12570
12571 @geindex -gnatw.Z (gcc)
12572
12573 @geindex Size/Alignment warnings
12574
12575
12576 @table @asis
12577
12578 @item @code{-gnatw.Z}
12579
12580 @emph{Suppress warnings for size not a multiple of alignment.}
12581
12582 This switch suppresses warnings for cases of array and record types
12583 with specified @code{Size} and @code{Alignment} attributes where the
12584 size is not a multiple of the alignment, resulting in an object
12585 size that is greater than the specified size. The warning can also
12586 be suppressed by giving an explicit @code{Object_Size} value.
12587 @end table
12588
12589 @geindex -Wunused (gcc)
12590
12591
12592 @table @asis
12593
12594 @item @code{-Wunused}
12595
12596 The warnings controlled by the @code{-gnatw} switch are generated by
12597 the front end of the compiler. The GCC back end can provide
12598 additional warnings and they are controlled by the @code{-W} switch.
12599 For example, @code{-Wunused} activates back end
12600 warnings for entities that are declared but not referenced.
12601 @end table
12602
12603 @geindex -Wuninitialized (gcc)
12604
12605
12606 @table @asis
12607
12608 @item @code{-Wuninitialized}
12609
12610 Similarly, @code{-Wuninitialized} activates
12611 the back end warning for uninitialized variables. This switch must be
12612 used in conjunction with an optimization level greater than zero.
12613 @end table
12614
12615 @geindex -Wstack-usage (gcc)
12616
12617
12618 @table @asis
12619
12620 @item @code{-Wstack-usage=@emph{len}}
12621
12622 Warn if the stack usage of a subprogram might be larger than @code{len} bytes.
12623 See @ref{e6,,Static Stack Usage Analysis} for details.
12624 @end table
12625
12626 @geindex -Wall (gcc)
12627
12628
12629 @table @asis
12630
12631 @item @code{-Wall}
12632
12633 This switch enables most warnings from the GCC back end.
12634 The code generator detects a number of warning situations that are missed
12635 by the GNAT front end, and this switch can be used to activate them.
12636 The use of this switch also sets the default front end warning mode to
12637 @code{-gnatwa}, that is, most front end warnings activated as well.
12638 @end table
12639
12640 @geindex -w (gcc)
12641
12642
12643 @table @asis
12644
12645 @item @code{-w}
12646
12647 Conversely, this switch suppresses warnings from the GCC back end.
12648 The use of this switch also sets the default front end warning mode to
12649 @code{-gnatws}, that is, front end warnings suppressed as well.
12650 @end table
12651
12652 @geindex -Werror (gcc)
12653
12654
12655 @table @asis
12656
12657 @item @code{-Werror}
12658
12659 This switch causes warnings from the GCC back end to be treated as
12660 errors. The warning string still appears, but the warning messages are
12661 counted as errors, and prevent the generation of an object file.
12662 @end table
12663
12664 A string of warning parameters can be used in the same parameter. For example:
12665
12666 @example
12667 -gnatwaGe
12668 @end example
12669
12670 will turn on all optional warnings except for unrecognized pragma warnings,
12671 and also specify that warnings should be treated as errors.
12672
12673 When no switch @code{-gnatw} is used, this is equivalent to:
12674
12675 @quotation
12676
12677
12678 @itemize *
12679
12680 @item
12681 @code{-gnatw.a}
12682
12683 @item
12684 @code{-gnatwB}
12685
12686 @item
12687 @code{-gnatw.b}
12688
12689 @item
12690 @code{-gnatwC}
12691
12692 @item
12693 @code{-gnatw.C}
12694
12695 @item
12696 @code{-gnatwD}
12697
12698 @item
12699 @code{-gnatw.D}
12700
12701 @item
12702 @code{-gnatwF}
12703
12704 @item
12705 @code{-gnatw.F}
12706
12707 @item
12708 @code{-gnatwg}
12709
12710 @item
12711 @code{-gnatwH}
12712
12713 @item
12714 @code{-gnatw.H}
12715
12716 @item
12717 @code{-gnatwi}
12718
12719 @item
12720 @code{-gnatwJ}
12721
12722 @item
12723 @code{-gnatw.J}
12724
12725 @item
12726 @code{-gnatwK}
12727
12728 @item
12729 @code{-gnatw.K}
12730
12731 @item
12732 @code{-gnatwL}
12733
12734 @item
12735 @code{-gnatw.L}
12736
12737 @item
12738 @code{-gnatwM}
12739
12740 @item
12741 @code{-gnatw.m}
12742
12743 @item
12744 @code{-gnatwn}
12745
12746 @item
12747 @code{-gnatw.N}
12748
12749 @item
12750 @code{-gnatwo}
12751
12752 @item
12753 @code{-gnatw.O}
12754
12755 @item
12756 @code{-gnatwP}
12757
12758 @item
12759 @code{-gnatw.P}
12760
12761 @item
12762 @code{-gnatwq}
12763
12764 @item
12765 @code{-gnatw.Q}
12766
12767 @item
12768 @code{-gnatwR}
12769
12770 @item
12771 @code{-gnatw.R}
12772
12773 @item
12774 @code{-gnatw.S}
12775
12776 @item
12777 @code{-gnatwT}
12778
12779 @item
12780 @code{-gnatw.t}
12781
12782 @item
12783 @code{-gnatwU}
12784
12785 @item
12786 @code{-gnatw.U}
12787
12788 @item
12789 @code{-gnatwv}
12790
12791 @item
12792 @code{-gnatw.v}
12793
12794 @item
12795 @code{-gnatww}
12796
12797 @item
12798 @code{-gnatw.W}
12799
12800 @item
12801 @code{-gnatwx}
12802
12803 @item
12804 @code{-gnatw.X}
12805
12806 @item
12807 @code{-gnatwy}
12808
12809 @item
12810 @code{-gnatw.Y}
12811
12812 @item
12813 @code{-gnatwz}
12814
12815 @item
12816 @code{-gnatw.z}
12817 @end itemize
12818 @end quotation
12819
12820 @node Debugging and Assertion Control,Validity Checking,Warning Message Control,Compiler Switches
12821 @anchor{gnat_ugn/building_executable_programs_with_gnat debugging-and-assertion-control}@anchor{f1}@anchor{gnat_ugn/building_executable_programs_with_gnat id16}@anchor{f2}
12822 @subsection Debugging and Assertion Control
12823
12824
12825 @geindex -gnata (gcc)
12826
12827
12828 @table @asis
12829
12830 @item @code{-gnata}
12831
12832 @geindex Assert
12833
12834 @geindex Debug
12835
12836 @geindex Assertions
12837
12838 @geindex Precondition
12839
12840 @geindex Postcondition
12841
12842 @geindex Type invariants
12843
12844 @geindex Subtype predicates
12845
12846 The @code{-gnata} option is equivalent to the following @code{Assertion_Policy} pragma:
12847
12848 @example
12849 pragma Assertion_Policy (Check);
12850 @end example
12851
12852 Which is a shorthand for:
12853
12854 @example
12855 pragma Assertion_Policy
12856 (Assert => Check,
12857 Static_Predicate => Check,
12858 Dynamic_Predicate => Check,
12859 Pre => Check,
12860 Pre'Class => Check,
12861 Post => Check,
12862 Post'Class => Check,
12863 Type_Invariant => Check,
12864 Type_Invariant'Class => Check);
12865 @end example
12866
12867 The pragmas @code{Assert} and @code{Debug} normally have no effect and
12868 are ignored. This switch, where @code{a} stands for ‘assert’, causes
12869 pragmas @code{Assert} and @code{Debug} to be activated. This switch also
12870 causes preconditions, postconditions, subtype predicates, and
12871 type invariants to be activated.
12872
12873 The pragmas have the form:
12874
12875 @example
12876 pragma Assert (<Boolean-expression> [, <static-string-expression>])
12877 pragma Debug (<procedure call>)
12878 pragma Type_Invariant (<type-local-name>, <Boolean-expression>)
12879 pragma Predicate (<type-local-name>, <Boolean-expression>)
12880 pragma Precondition (<Boolean-expression>, <string-expression>)
12881 pragma Postcondition (<Boolean-expression>, <string-expression>)
12882 @end example
12883
12884 The aspects have the form:
12885
12886 @example
12887 with [Pre|Post|Type_Invariant|Dynamic_Predicate|Static_Predicate]
12888 => <Boolean-expression>;
12889 @end example
12890
12891 The @code{Assert} pragma causes @code{Boolean-expression} to be tested.
12892 If the result is @code{True}, the pragma has no effect (other than
12893 possible side effects from evaluating the expression). If the result is
12894 @code{False}, the exception @code{Assert_Failure} declared in the package
12895 @code{System.Assertions} is raised (passing @code{static-string-expression}, if
12896 present, as the message associated with the exception). If no string
12897 expression is given, the default is a string containing the file name and
12898 line number of the pragma.
12899
12900 The @code{Debug} pragma causes @code{procedure} to be called. Note that
12901 @code{pragma Debug} may appear within a declaration sequence, allowing
12902 debugging procedures to be called between declarations.
12903
12904 For the aspect specification, the @code{Boolean-expression} is evaluated.
12905 If the result is @code{True}, the aspect has no effect. If the result
12906 is @code{False}, the exception @code{Assert_Failure} is raised.
12907 @end table
12908
12909 @node Validity Checking,Style Checking,Debugging and Assertion Control,Compiler Switches
12910 @anchor{gnat_ugn/building_executable_programs_with_gnat id17}@anchor{f3}@anchor{gnat_ugn/building_executable_programs_with_gnat validity-checking}@anchor{e7}
12911 @subsection Validity Checking
12912
12913
12914 @geindex Validity Checking
12915
12916 The Ada Reference Manual defines the concept of invalid values (see
12917 RM 13.9.1). The primary source of invalid values is uninitialized
12918 variables. A scalar variable that is left uninitialized may contain
12919 an invalid value; the concept of invalid does not apply to access or
12920 composite types.
12921
12922 It is an error to read an invalid value, but the RM does not require
12923 run-time checks to detect such errors, except for some minimal
12924 checking to prevent erroneous execution (i.e. unpredictable
12925 behavior). This corresponds to the @code{-gnatVd} switch below,
12926 which is the default. For example, by default, if the expression of a
12927 case statement is invalid, it will raise Constraint_Error rather than
12928 causing a wild jump, and if an array index on the left-hand side of an
12929 assignment is invalid, it will raise Constraint_Error rather than
12930 overwriting an arbitrary memory location.
12931
12932 The @code{-gnatVa} may be used to enable additional validity checks,
12933 which are not required by the RM. These checks are often very
12934 expensive (which is why the RM does not require them). These checks
12935 are useful in tracking down uninitialized variables, but they are
12936 not usually recommended for production builds, and in particular
12937 we do not recommend using these extra validity checking options in
12938 combination with optimization, since this can confuse the optimizer.
12939 If performance is a consideration, leading to the need to optimize,
12940 then the validity checking options should not be used.
12941
12942 The other @code{-gnatV@emph{x}} switches below allow finer-grained
12943 control; you can enable whichever validity checks you desire. However,
12944 for most debugging purposes, @code{-gnatVa} is sufficient, and the
12945 default @code{-gnatVd} (i.e. standard Ada behavior) is usually
12946 sufficient for non-debugging use.
12947
12948 The @code{-gnatB} switch tells the compiler to assume that all
12949 values are valid (that is, within their declared subtype range)
12950 except in the context of a use of the Valid attribute. This means
12951 the compiler can generate more efficient code, since the range
12952 of values is better known at compile time. However, an uninitialized
12953 variable can cause wild jumps and memory corruption in this mode.
12954
12955 The @code{-gnatV@emph{x}} switch allows control over the validity
12956 checking mode as described below.
12957 The @code{x} argument is a string of letters that
12958 indicate validity checks that are performed or not performed in addition
12959 to the default checks required by Ada as described above.
12960
12961 @geindex -gnatVa (gcc)
12962
12963
12964 @table @asis
12965
12966 @item @code{-gnatVa}
12967
12968 @emph{All validity checks.}
12969
12970 All validity checks are turned on.
12971 That is, @code{-gnatVa} is
12972 equivalent to @code{gnatVcdfimoprst}.
12973 @end table
12974
12975 @geindex -gnatVc (gcc)
12976
12977
12978 @table @asis
12979
12980 @item @code{-gnatVc}
12981
12982 @emph{Validity checks for copies.}
12983
12984 The right hand side of assignments, and the initializing values of
12985 object declarations are validity checked.
12986 @end table
12987
12988 @geindex -gnatVd (gcc)
12989
12990
12991 @table @asis
12992
12993 @item @code{-gnatVd}
12994
12995 @emph{Default (RM) validity checks.}
12996
12997 Some validity checks are done by default following normal Ada semantics
12998 (RM 13.9.1 (9-11)).
12999 A check is done in case statements that the expression is within the range
13000 of the subtype. If it is not, Constraint_Error is raised.
13001 For assignments to array components, a check is done that the expression used
13002 as index is within the range. If it is not, Constraint_Error is raised.
13003 Both these validity checks may be turned off using switch @code{-gnatVD}.
13004 They are turned on by default. If @code{-gnatVD} is specified, a subsequent
13005 switch @code{-gnatVd} will leave the checks turned on.
13006 Switch @code{-gnatVD} should be used only if you are sure that all such
13007 expressions have valid values. If you use this switch and invalid values
13008 are present, then the program is erroneous, and wild jumps or memory
13009 overwriting may occur.
13010 @end table
13011
13012 @geindex -gnatVe (gcc)
13013
13014
13015 @table @asis
13016
13017 @item @code{-gnatVe}
13018
13019 @emph{Validity checks for elementary components.}
13020
13021 In the absence of this switch, assignments to record or array components are
13022 not validity checked, even if validity checks for assignments generally
13023 (@code{-gnatVc}) are turned on. In Ada, assignment of composite values do not
13024 require valid data, but assignment of individual components does. So for
13025 example, there is a difference between copying the elements of an array with a
13026 slice assignment, compared to assigning element by element in a loop. This
13027 switch allows you to turn off validity checking for components, even when they
13028 are assigned component by component.
13029 @end table
13030
13031 @geindex -gnatVf (gcc)
13032
13033
13034 @table @asis
13035
13036 @item @code{-gnatVf}
13037
13038 @emph{Validity checks for floating-point values.}
13039
13040 In the absence of this switch, validity checking occurs only for discrete
13041 values. If @code{-gnatVf} is specified, then validity checking also applies
13042 for floating-point values, and NaNs and infinities are considered invalid,
13043 as well as out of range values for constrained types. Note that this means
13044 that standard IEEE infinity mode is not allowed. The exact contexts
13045 in which floating-point values are checked depends on the setting of other
13046 options. For example, @code{-gnatVif} or @code{-gnatVfi}
13047 (the order does not matter) specifies that floating-point parameters of mode
13048 @code{in} should be validity checked.
13049 @end table
13050
13051 @geindex -gnatVi (gcc)
13052
13053
13054 @table @asis
13055
13056 @item @code{-gnatVi}
13057
13058 @emph{Validity checks for `@w{`}in`@w{`} mode parameters.}
13059
13060 Arguments for parameters of mode @code{in} are validity checked in function
13061 and procedure calls at the point of call.
13062 @end table
13063
13064 @geindex -gnatVm (gcc)
13065
13066
13067 @table @asis
13068
13069 @item @code{-gnatVm}
13070
13071 @emph{Validity checks for `@w{`}in out`@w{`} mode parameters.}
13072
13073 Arguments for parameters of mode @code{in out} are validity checked in
13074 procedure calls at the point of call. The @code{'m'} here stands for
13075 modify, since this concerns parameters that can be modified by the call.
13076 Note that there is no specific option to test @code{out} parameters,
13077 but any reference within the subprogram will be tested in the usual
13078 manner, and if an invalid value is copied back, any reference to it
13079 will be subject to validity checking.
13080 @end table
13081
13082 @geindex -gnatVn (gcc)
13083
13084
13085 @table @asis
13086
13087 @item @code{-gnatVn}
13088
13089 @emph{No validity checks.}
13090
13091 This switch turns off all validity checking, including the default checking
13092 for case statements and left hand side subscripts. Note that the use of
13093 the switch @code{-gnatp} suppresses all run-time checks, including
13094 validity checks, and thus implies @code{-gnatVn}. When this switch
13095 is used, it cancels any other @code{-gnatV} previously issued.
13096 @end table
13097
13098 @geindex -gnatVo (gcc)
13099
13100
13101 @table @asis
13102
13103 @item @code{-gnatVo}
13104
13105 @emph{Validity checks for operator and attribute operands.}
13106
13107 Arguments for predefined operators and attributes are validity checked.
13108 This includes all operators in package @code{Standard},
13109 the shift operators defined as intrinsic in package @code{Interfaces}
13110 and operands for attributes such as @code{Pos}. Checks are also made
13111 on individual component values for composite comparisons, and on the
13112 expressions in type conversions and qualified expressions. Checks are
13113 also made on explicit ranges using @code{..} (e.g., slices, loops etc).
13114 @end table
13115
13116 @geindex -gnatVp (gcc)
13117
13118
13119 @table @asis
13120
13121 @item @code{-gnatVp}
13122
13123 @emph{Validity checks for parameters.}
13124
13125 This controls the treatment of parameters within a subprogram (as opposed
13126 to @code{-gnatVi} and @code{-gnatVm} which control validity testing
13127 of parameters on a call. If either of these call options is used, then
13128 normally an assumption is made within a subprogram that the input arguments
13129 have been validity checking at the point of call, and do not need checking
13130 again within a subprogram). If @code{-gnatVp} is set, then this assumption
13131 is not made, and parameters are not assumed to be valid, so their validity
13132 will be checked (or rechecked) within the subprogram.
13133 @end table
13134
13135 @geindex -gnatVr (gcc)
13136
13137
13138 @table @asis
13139
13140 @item @code{-gnatVr}
13141
13142 @emph{Validity checks for function returns.}
13143
13144 The expression in @code{return} statements in functions is validity
13145 checked.
13146 @end table
13147
13148 @geindex -gnatVs (gcc)
13149
13150
13151 @table @asis
13152
13153 @item @code{-gnatVs}
13154
13155 @emph{Validity checks for subscripts.}
13156
13157 All subscripts expressions are checked for validity, whether they appear
13158 on the right side or left side (in default mode only left side subscripts
13159 are validity checked).
13160 @end table
13161
13162 @geindex -gnatVt (gcc)
13163
13164
13165 @table @asis
13166
13167 @item @code{-gnatVt}
13168
13169 @emph{Validity checks for tests.}
13170
13171 Expressions used as conditions in @code{if}, @code{while} or @code{exit}
13172 statements are checked, as well as guard expressions in entry calls.
13173 @end table
13174
13175 The @code{-gnatV} switch may be followed by a string of letters
13176 to turn on a series of validity checking options.
13177 For example, @code{-gnatVcr}
13178 specifies that in addition to the default validity checking, copies and
13179 function return expressions are to be validity checked.
13180 In order to make it easier to specify the desired combination of effects,
13181 the upper case letters @code{CDFIMORST} may
13182 be used to turn off the corresponding lower case option.
13183 Thus @code{-gnatVaM} turns on all validity checking options except for
13184 checking of @code{in out} parameters.
13185
13186 The specification of additional validity checking generates extra code (and
13187 in the case of @code{-gnatVa} the code expansion can be substantial).
13188 However, these additional checks can be very useful in detecting
13189 uninitialized variables, incorrect use of unchecked conversion, and other
13190 errors leading to invalid values. The use of pragma @code{Initialize_Scalars}
13191 is useful in conjunction with the extra validity checking, since this
13192 ensures that wherever possible uninitialized variables have invalid values.
13193
13194 See also the pragma @code{Validity_Checks} which allows modification of
13195 the validity checking mode at the program source level, and also allows for
13196 temporary disabling of validity checks.
13197
13198 @node Style Checking,Run-Time Checks,Validity Checking,Compiler Switches
13199 @anchor{gnat_ugn/building_executable_programs_with_gnat id18}@anchor{f4}@anchor{gnat_ugn/building_executable_programs_with_gnat style-checking}@anchor{ec}
13200 @subsection Style Checking
13201
13202
13203 @geindex Style checking
13204
13205 @geindex -gnaty (gcc)
13206
13207 The @code{-gnatyx} switch causes the compiler to
13208 enforce specified style rules. A limited set of style rules has been used
13209 in writing the GNAT sources themselves. This switch allows user programs
13210 to activate all or some of these checks. If the source program fails a
13211 specified style check, an appropriate message is given, preceded by
13212 the character sequence ‘(style)’. This message does not prevent
13213 successful compilation (unless the @code{-gnatwe} switch is used).
13214
13215 Note that this is by no means intended to be a general facility for
13216 checking arbitrary coding standards. It is simply an embedding of the
13217 style rules we have chosen for the GNAT sources. If you are starting
13218 a project which does not have established style standards, you may
13219 find it useful to adopt the entire set of GNAT coding standards, or
13220 some subset of them.
13221
13222
13223 The string @code{x} is a sequence of letters or digits
13224 indicating the particular style
13225 checks to be performed. The following checks are defined:
13226
13227 @geindex -gnaty[0-9] (gcc)
13228
13229
13230 @table @asis
13231
13232 @item @code{-gnaty0}
13233
13234 @emph{Specify indentation level.}
13235
13236 If a digit from 1-9 appears
13237 in the string after @code{-gnaty}
13238 then proper indentation is checked, with the digit indicating the
13239 indentation level required. A value of zero turns off this style check.
13240 The rule checks that the following constructs start on a column that is
13241 a multiple of the alignment level:
13242
13243
13244 @itemize *
13245
13246 @item
13247 beginnings of declarations (except record component declarations)
13248 and statements;
13249
13250 @item
13251 beginnings of the structural components of compound statements;
13252
13253 @item
13254 @code{end} keyword that completes the declaration of a program unit declaration
13255 or body or that completes a compound statement.
13256 @end itemize
13257
13258 Full line comments must be
13259 aligned with the @code{--} starting on a column that is a multiple of
13260 the alignment level, or they may be aligned the same way as the following
13261 non-blank line (this is useful when full line comments appear in the middle
13262 of a statement, or they may be aligned with the source line on the previous
13263 non-blank line.
13264 @end table
13265
13266 @geindex -gnatya (gcc)
13267
13268
13269 @table @asis
13270
13271 @item @code{-gnatya}
13272
13273 @emph{Check attribute casing.}
13274
13275 Attribute names, including the case of keywords such as @code{digits}
13276 used as attributes names, must be written in mixed case, that is, the
13277 initial letter and any letter following an underscore must be uppercase.
13278 All other letters must be lowercase.
13279 @end table
13280
13281 @geindex -gnatyA (gcc)
13282
13283
13284 @table @asis
13285
13286 @item @code{-gnatyA}
13287
13288 @emph{Use of array index numbers in array attributes.}
13289
13290 When using the array attributes First, Last, Range,
13291 or Length, the index number must be omitted for one-dimensional arrays
13292 and is required for multi-dimensional arrays.
13293 @end table
13294
13295 @geindex -gnatyb (gcc)
13296
13297
13298 @table @asis
13299
13300 @item @code{-gnatyb}
13301
13302 @emph{Blanks not allowed at statement end.}
13303
13304 Trailing blanks are not allowed at the end of statements. The purpose of this
13305 rule, together with h (no horizontal tabs), is to enforce a canonical format
13306 for the use of blanks to separate source tokens.
13307 @end table
13308
13309 @geindex -gnatyB (gcc)
13310
13311
13312 @table @asis
13313
13314 @item @code{-gnatyB}
13315
13316 @emph{Check Boolean operators.}
13317
13318 The use of AND/OR operators is not permitted except in the cases of modular
13319 operands, array operands, and simple stand-alone boolean variables or
13320 boolean constants. In all other cases @code{and then}/@cite{or else} are
13321 required.
13322 @end table
13323
13324 @geindex -gnatyc (gcc)
13325
13326
13327 @table @asis
13328
13329 @item @code{-gnatyc}
13330
13331 @emph{Check comments, double space.}
13332
13333 Comments must meet the following set of rules:
13334
13335
13336 @itemize *
13337
13338 @item
13339 The @code{--} that starts the column must either start in column one,
13340 or else at least one blank must precede this sequence.
13341
13342 @item
13343 Comments that follow other tokens on a line must have at least one blank
13344 following the @code{--} at the start of the comment.
13345
13346 @item
13347 Full line comments must have at least two blanks following the
13348 @code{--} that starts the comment, with the following exceptions.
13349
13350 @item
13351 A line consisting only of the @code{--} characters, possibly preceded
13352 by blanks is permitted.
13353
13354 @item
13355 A comment starting with @code{--x} where @code{x} is a special character
13356 is permitted.
13357 This allows proper processing of the output from specialized tools
13358 such as @code{gnatprep} (where @code{--!} is used) and in earlier versions of the SPARK
13359 annotation
13360 language (where @code{--#} is used). For the purposes of this rule, a
13361 special character is defined as being in one of the ASCII ranges
13362 @code{16#21#...16#2F#} or @code{16#3A#...16#3F#}.
13363 Note that this usage is not permitted
13364 in GNAT implementation units (i.e., when @code{-gnatg} is used).
13365
13366 @item
13367 A line consisting entirely of minus signs, possibly preceded by blanks, is
13368 permitted. This allows the construction of box comments where lines of minus
13369 signs are used to form the top and bottom of the box.
13370
13371 @item
13372 A comment that starts and ends with @code{--} is permitted as long as at
13373 least one blank follows the initial @code{--}. Together with the preceding
13374 rule, this allows the construction of box comments, as shown in the following
13375 example:
13376
13377 @example
13378 ---------------------------
13379 -- This is a box comment --
13380 -- with two text lines. --
13381 ---------------------------
13382 @end example
13383 @end itemize
13384 @end table
13385
13386 @geindex -gnatyC (gcc)
13387
13388
13389 @table @asis
13390
13391 @item @code{-gnatyC}
13392
13393 @emph{Check comments, single space.}
13394
13395 This is identical to @code{c} except that only one space
13396 is required following the @code{--} of a comment instead of two.
13397 @end table
13398
13399 @geindex -gnatyd (gcc)
13400
13401
13402 @table @asis
13403
13404 @item @code{-gnatyd}
13405
13406 @emph{Check no DOS line terminators present.}
13407
13408 All lines must be terminated by a single ASCII.LF
13409 character (in particular the DOS line terminator sequence CR/LF is not
13410 allowed).
13411 @end table
13412
13413 @geindex -gnatyD (gcc)
13414
13415
13416 @table @asis
13417
13418 @item @code{-gnatyD}
13419
13420 @emph{Check declared identifiers in mixed case.}
13421
13422 Declared identifiers must be in mixed case, as in
13423 This_Is_An_Identifier. Use -gnatyr in addition to ensure
13424 that references match declarations.
13425 @end table
13426
13427 @geindex -gnatye (gcc)
13428
13429
13430 @table @asis
13431
13432 @item @code{-gnatye}
13433
13434 @emph{Check end/exit labels.}
13435
13436 Optional labels on @code{end} statements ending subprograms and on
13437 @code{exit} statements exiting named loops, are required to be present.
13438 @end table
13439
13440 @geindex -gnatyf (gcc)
13441
13442
13443 @table @asis
13444
13445 @item @code{-gnatyf}
13446
13447 @emph{No form feeds or vertical tabs.}
13448
13449 Neither form feeds nor vertical tab characters are permitted
13450 in the source text.
13451 @end table
13452
13453 @geindex -gnatyg (gcc)
13454
13455
13456 @table @asis
13457
13458 @item @code{-gnatyg}
13459
13460 @emph{GNAT style mode.}
13461
13462 The set of style check switches is set to match that used by the GNAT sources.
13463 This may be useful when developing code that is eventually intended to be
13464 incorporated into GNAT. Currently this is equivalent to @code{-gnatyydISux})
13465 but additional style switches may be added to this set in the future without
13466 advance notice.
13467 @end table
13468
13469 @geindex -gnatyh (gcc)
13470
13471
13472 @table @asis
13473
13474 @item @code{-gnatyh}
13475
13476 @emph{No horizontal tabs.}
13477
13478 Horizontal tab characters are not permitted in the source text.
13479 Together with the b (no blanks at end of line) check, this
13480 enforces a canonical form for the use of blanks to separate
13481 source tokens.
13482 @end table
13483
13484 @geindex -gnatyi (gcc)
13485
13486
13487 @table @asis
13488
13489 @item @code{-gnatyi}
13490
13491 @emph{Check if-then layout.}
13492
13493 The keyword @code{then} must appear either on the same
13494 line as corresponding @code{if}, or on a line on its own, lined
13495 up under the @code{if}.
13496 @end table
13497
13498 @geindex -gnatyI (gcc)
13499
13500
13501 @table @asis
13502
13503 @item @code{-gnatyI}
13504
13505 @emph{check mode IN keywords.}
13506
13507 Mode @code{in} (the default mode) is not
13508 allowed to be given explicitly. @code{in out} is fine,
13509 but not @code{in} on its own.
13510 @end table
13511
13512 @geindex -gnatyk (gcc)
13513
13514
13515 @table @asis
13516
13517 @item @code{-gnatyk}
13518
13519 @emph{Check keyword casing.}
13520
13521 All keywords must be in lower case (with the exception of keywords
13522 such as @code{digits} used as attribute names to which this check
13523 does not apply). A single error is reported for each line breaking
13524 this rule even if multiple casing issues exist on a same line.
13525 @end table
13526
13527 @geindex -gnatyl (gcc)
13528
13529
13530 @table @asis
13531
13532 @item @code{-gnatyl}
13533
13534 @emph{Check layout.}
13535
13536 Layout of statement and declaration constructs must follow the
13537 recommendations in the Ada Reference Manual, as indicated by the
13538 form of the syntax rules. For example an @code{else} keyword must
13539 be lined up with the corresponding @code{if} keyword.
13540
13541 There are two respects in which the style rule enforced by this check
13542 option are more liberal than those in the Ada Reference Manual. First
13543 in the case of record declarations, it is permissible to put the
13544 @code{record} keyword on the same line as the @code{type} keyword, and
13545 then the @code{end} in @code{end record} must line up under @code{type}.
13546 This is also permitted when the type declaration is split on two lines.
13547 For example, any of the following three layouts is acceptable:
13548
13549 @example
13550 type q is record
13551 a : integer;
13552 b : integer;
13553 end record;
13554
13555 type q is
13556 record
13557 a : integer;
13558 b : integer;
13559 end record;
13560
13561 type q is
13562 record
13563 a : integer;
13564 b : integer;
13565 end record;
13566 @end example
13567
13568 Second, in the case of a block statement, a permitted alternative
13569 is to put the block label on the same line as the @code{declare} or
13570 @code{begin} keyword, and then line the @code{end} keyword up under
13571 the block label. For example both the following are permitted:
13572
13573 @example
13574 Block : declare
13575 A : Integer := 3;
13576 begin
13577 Proc (A, A);
13578 end Block;
13579
13580 Block :
13581 declare
13582 A : Integer := 3;
13583 begin
13584 Proc (A, A);
13585 end Block;
13586 @end example
13587
13588 The same alternative format is allowed for loops. For example, both of
13589 the following are permitted:
13590
13591 @example
13592 Clear : while J < 10 loop
13593 A (J) := 0;
13594 end loop Clear;
13595
13596 Clear :
13597 while J < 10 loop
13598 A (J) := 0;
13599 end loop Clear;
13600 @end example
13601 @end table
13602
13603 @geindex -gnatyLnnn (gcc)
13604
13605
13606 @table @asis
13607
13608 @item @code{-gnatyL}
13609
13610 @emph{Set maximum nesting level.}
13611
13612 The maximum level of nesting of constructs (including subprograms, loops,
13613 blocks, packages, and conditionals) may not exceed the given value
13614 @emph{nnn}. A value of zero disconnects this style check.
13615 @end table
13616
13617 @geindex -gnatym (gcc)
13618
13619
13620 @table @asis
13621
13622 @item @code{-gnatym}
13623
13624 @emph{Check maximum line length.}
13625
13626 The length of source lines must not exceed 79 characters, including
13627 any trailing blanks. The value of 79 allows convenient display on an
13628 80 character wide device or window, allowing for possible special
13629 treatment of 80 character lines. Note that this count is of
13630 characters in the source text. This means that a tab character counts
13631 as one character in this count and a wide character sequence counts as
13632 a single character (however many bytes are needed in the encoding).
13633 @end table
13634
13635 @geindex -gnatyMnnn (gcc)
13636
13637
13638 @table @asis
13639
13640 @item @code{-gnatyM}
13641
13642 @emph{Set maximum line length.}
13643
13644 The length of lines must not exceed the
13645 given value @emph{nnn}. The maximum value that can be specified is 32767.
13646 If neither style option for setting the line length is used, then the
13647 default is 255. This also controls the maximum length of lexical elements,
13648 where the only restriction is that they must fit on a single line.
13649 @end table
13650
13651 @geindex -gnatyn (gcc)
13652
13653
13654 @table @asis
13655
13656 @item @code{-gnatyn}
13657
13658 @emph{Check casing of entities in Standard.}
13659
13660 Any identifier from Standard must be cased
13661 to match the presentation in the Ada Reference Manual (for example,
13662 @code{Integer} and @code{ASCII.NUL}).
13663 @end table
13664
13665 @geindex -gnatyN (gcc)
13666
13667
13668 @table @asis
13669
13670 @item @code{-gnatyN}
13671
13672 @emph{Turn off all style checks.}
13673
13674 All style check options are turned off.
13675 @end table
13676
13677 @geindex -gnatyo (gcc)
13678
13679
13680 @table @asis
13681
13682 @item @code{-gnatyo}
13683
13684 @emph{Check order of subprogram bodies.}
13685
13686 All subprogram bodies in a given scope
13687 (e.g., a package body) must be in alphabetical order. The ordering
13688 rule uses normal Ada rules for comparing strings, ignoring casing
13689 of letters, except that if there is a trailing numeric suffix, then
13690 the value of this suffix is used in the ordering (e.g., Junk2 comes
13691 before Junk10).
13692 @end table
13693
13694 @geindex -gnatyO (gcc)
13695
13696
13697 @table @asis
13698
13699 @item @code{-gnatyO}
13700
13701 @emph{Check that overriding subprograms are explicitly marked as such.}
13702
13703 This applies to all subprograms of a derived type that override a primitive
13704 operation of the type, for both tagged and untagged types. In particular,
13705 the declaration of a primitive operation of a type extension that overrides
13706 an inherited operation must carry an overriding indicator. Another case is
13707 the declaration of a function that overrides a predefined operator (such
13708 as an equality operator).
13709 @end table
13710
13711 @geindex -gnatyp (gcc)
13712
13713
13714 @table @asis
13715
13716 @item @code{-gnatyp}
13717
13718 @emph{Check pragma casing.}
13719
13720 Pragma names must be written in mixed case, that is, the
13721 initial letter and any letter following an underscore must be uppercase.
13722 All other letters must be lowercase. An exception is that SPARK_Mode is
13723 allowed as an alternative for Spark_Mode.
13724 @end table
13725
13726 @geindex -gnatyr (gcc)
13727
13728
13729 @table @asis
13730
13731 @item @code{-gnatyr}
13732
13733 @emph{Check references.}
13734
13735 All identifier references must be cased in the same way as the
13736 corresponding declaration. No specific casing style is imposed on
13737 identifiers. The only requirement is for consistency of references
13738 with declarations.
13739 @end table
13740
13741 @geindex -gnatys (gcc)
13742
13743
13744 @table @asis
13745
13746 @item @code{-gnatys}
13747
13748 @emph{Check separate specs.}
13749
13750 Separate declarations (‘specs’) are required for subprograms (a
13751 body is not allowed to serve as its own declaration). The only
13752 exception is that parameterless library level procedures are
13753 not required to have a separate declaration. This exception covers
13754 the most frequent form of main program procedures.
13755 @end table
13756
13757 @geindex -gnatyS (gcc)
13758
13759
13760 @table @asis
13761
13762 @item @code{-gnatyS}
13763
13764 @emph{Check no statements after then/else.}
13765
13766 No statements are allowed
13767 on the same line as a @code{then} or @code{else} keyword following the
13768 keyword in an @code{if} statement. @code{or else} and @code{and then} are not
13769 affected, and a special exception allows a pragma to appear after @code{else}.
13770 @end table
13771
13772 @geindex -gnatyt (gcc)
13773
13774
13775 @table @asis
13776
13777 @item @code{-gnatyt}
13778
13779 @emph{Check token spacing.}
13780
13781 The following token spacing rules are enforced:
13782
13783
13784 @itemize *
13785
13786 @item
13787 The keywords @code{abs} and @code{not} must be followed by a space.
13788
13789 @item
13790 The token @code{=>} must be surrounded by spaces.
13791
13792 @item
13793 The token @code{<>} must be preceded by a space or a left parenthesis.
13794
13795 @item
13796 Binary operators other than @code{**} must be surrounded by spaces.
13797 There is no restriction on the layout of the @code{**} binary operator.
13798
13799 @item
13800 Colon must be surrounded by spaces.
13801
13802 @item
13803 Colon-equal (assignment, initialization) must be surrounded by spaces.
13804
13805 @item
13806 Comma must be the first non-blank character on the line, or be
13807 immediately preceded by a non-blank character, and must be followed
13808 by a space.
13809
13810 @item
13811 If the token preceding a left parenthesis ends with a letter or digit, then
13812 a space must separate the two tokens.
13813
13814 @item
13815 If the token following a right parenthesis starts with a letter or digit, then
13816 a space must separate the two tokens.
13817
13818 @item
13819 A right parenthesis must either be the first non-blank character on
13820 a line, or it must be preceded by a non-blank character.
13821
13822 @item
13823 A semicolon must not be preceded by a space, and must not be followed by
13824 a non-blank character.
13825
13826 @item
13827 A unary plus or minus may not be followed by a space.
13828
13829 @item
13830 A vertical bar must be surrounded by spaces.
13831 @end itemize
13832
13833 Exactly one blank (and no other white space) must appear between
13834 a @code{not} token and a following @code{in} token.
13835 @end table
13836
13837 @geindex -gnatyu (gcc)
13838
13839
13840 @table @asis
13841
13842 @item @code{-gnatyu}
13843
13844 @emph{Check unnecessary blank lines.}
13845
13846 Unnecessary blank lines are not allowed. A blank line is considered
13847 unnecessary if it appears at the end of the file, or if more than
13848 one blank line occurs in sequence.
13849 @end table
13850
13851 @geindex -gnatyx (gcc)
13852
13853
13854 @table @asis
13855
13856 @item @code{-gnatyx}
13857
13858 @emph{Check extra parentheses.}
13859
13860 Unnecessary extra level of parentheses (C-style) are not allowed
13861 around conditions in @code{if} statements, @code{while} statements and
13862 @code{exit} statements.
13863 @end table
13864
13865 @geindex -gnatyy (gcc)
13866
13867
13868 @table @asis
13869
13870 @item @code{-gnatyy}
13871
13872 @emph{Set all standard style check options.}
13873
13874 This is equivalent to @code{gnaty3aAbcefhiklmnprst}, that is all checking
13875 options enabled with the exception of @code{-gnatyB}, @code{-gnatyd},
13876 @code{-gnatyI}, @code{-gnatyLnnn}, @code{-gnatyo}, @code{-gnatyO},
13877 @code{-gnatyS}, @code{-gnatyu}, and @code{-gnatyx}.
13878 @end table
13879
13880 @geindex -gnaty- (gcc)
13881
13882
13883 @table @asis
13884
13885 @item @code{-gnaty-}
13886
13887 @emph{Remove style check options.}
13888
13889 This causes any subsequent options in the string to act as canceling the
13890 corresponding style check option. To cancel maximum nesting level control,
13891 use the @code{L} parameter without any integer value after that, because any
13892 digit following @emph{-} in the parameter string of the @code{-gnaty}
13893 option will be treated as canceling the indentation check. The same is true
13894 for the @code{M} parameter. @code{y} and @code{N} parameters are not
13895 allowed after @emph{-}.
13896 @end table
13897
13898 @geindex -gnaty+ (gcc)
13899
13900
13901 @table @asis
13902
13903 @item @code{-gnaty+}
13904
13905 @emph{Enable style check options.}
13906
13907 This causes any subsequent options in the string to enable the corresponding
13908 style check option. That is, it cancels the effect of a previous -,
13909 if any.
13910 @end table
13911
13912 @c end of switch description (leave this comment to ease automatic parsing for
13913
13914 @c GNAT Studio
13915
13916 In the above rules, appearing in column one is always permitted, that is,
13917 counts as meeting either a requirement for a required preceding space,
13918 or as meeting a requirement for no preceding space.
13919
13920 Appearing at the end of a line is also always permitted, that is, counts
13921 as meeting either a requirement for a following space, or as meeting
13922 a requirement for no following space.
13923
13924 If any of these style rules is violated, a message is generated giving
13925 details on the violation. The initial characters of such messages are
13926 always ‘@cite{(style)}’. Note that these messages are treated as warning
13927 messages, so they normally do not prevent the generation of an object
13928 file. The @code{-gnatwe} switch can be used to treat warning messages,
13929 including style messages, as fatal errors.
13930
13931 The switch @code{-gnaty} on its own (that is not
13932 followed by any letters or digits) is equivalent
13933 to the use of @code{-gnatyy} as described above, that is all
13934 built-in standard style check options are enabled.
13935
13936 The switch @code{-gnatyN} clears any previously set style checks.
13937
13938 @node Run-Time Checks,Using gcc for Syntax Checking,Style Checking,Compiler Switches
13939 @anchor{gnat_ugn/building_executable_programs_with_gnat id19}@anchor{f5}@anchor{gnat_ugn/building_executable_programs_with_gnat run-time-checks}@anchor{ea}
13940 @subsection Run-Time Checks
13941
13942
13943 @geindex Division by zero
13944
13945 @geindex Access before elaboration
13946
13947 @geindex Checks
13948 @geindex division by zero
13949
13950 @geindex Checks
13951 @geindex access before elaboration
13952
13953 @geindex Checks
13954 @geindex stack overflow checking
13955
13956 By default, the following checks are suppressed: stack overflow
13957 checks, and checks for access before elaboration on subprogram
13958 calls. All other checks, including overflow checks, range checks and
13959 array bounds checks, are turned on by default. The following @code{gcc}
13960 switches refine this default behavior.
13961
13962 @geindex -gnatp (gcc)
13963
13964
13965 @table @asis
13966
13967 @item @code{-gnatp}
13968
13969 @geindex Suppressing checks
13970
13971 @geindex Checks
13972 @geindex suppressing
13973
13974 This switch causes the unit to be compiled
13975 as though @code{pragma Suppress (All_checks)}
13976 had been present in the source. Validity checks are also eliminated (in
13977 other words @code{-gnatp} also implies @code{-gnatVn}.
13978 Use this switch to improve the performance
13979 of the code at the expense of safety in the presence of invalid data or
13980 program bugs.
13981
13982 Note that when checks are suppressed, the compiler is allowed, but not
13983 required, to omit the checking code. If the run-time cost of the
13984 checking code is zero or near-zero, the compiler will generate it even
13985 if checks are suppressed. In particular, if the compiler can prove
13986 that a certain check will necessarily fail, it will generate code to
13987 do an unconditional ‘raise’, even if checks are suppressed. The
13988 compiler warns in this case. Another case in which checks may not be
13989 eliminated is when they are embedded in certain run-time routines such
13990 as math library routines.
13991
13992 Of course, run-time checks are omitted whenever the compiler can prove
13993 that they will not fail, whether or not checks are suppressed.
13994
13995 Note that if you suppress a check that would have failed, program
13996 execution is erroneous, which means the behavior is totally
13997 unpredictable. The program might crash, or print wrong answers, or
13998 do anything else. It might even do exactly what you wanted it to do
13999 (and then it might start failing mysteriously next week or next
14000 year). The compiler will generate code based on the assumption that
14001 the condition being checked is true, which can result in erroneous
14002 execution if that assumption is wrong.
14003
14004 The checks subject to suppression include all the checks defined by the Ada
14005 standard, the additional implementation defined checks @code{Alignment_Check},
14006 @code{Duplicated_Tag_Check}, @code{Predicate_Check}, @code{Container_Checks}, @code{Tampering_Check},
14007 and @code{Validity_Check}, as well as any checks introduced using @code{pragma Check_Name}.
14008 Note that @code{Atomic_Synchronization} is not automatically suppressed by use of this option.
14009
14010 If the code depends on certain checks being active, you can use
14011 pragma @code{Unsuppress} either as a configuration pragma or as
14012 a local pragma to make sure that a specified check is performed
14013 even if @code{gnatp} is specified.
14014
14015 The @code{-gnatp} switch has no effect if a subsequent
14016 @code{-gnat-p} switch appears.
14017 @end table
14018
14019 @geindex -gnat-p (gcc)
14020
14021 @geindex Suppressing checks
14022
14023 @geindex Checks
14024 @geindex suppressing
14025
14026 @geindex Suppress
14027
14028
14029 @table @asis
14030
14031 @item @code{-gnat-p}
14032
14033 This switch cancels the effect of a previous @code{gnatp} switch.
14034 @end table
14035
14036 @geindex -gnato?? (gcc)
14037
14038 @geindex Overflow checks
14039
14040 @geindex Overflow mode
14041
14042 @geindex Check
14043 @geindex overflow
14044
14045
14046 @table @asis
14047
14048 @item @code{-gnato??}
14049
14050 This switch controls the mode used for computing intermediate
14051 arithmetic integer operations, and also enables overflow checking.
14052 For a full description of overflow mode and checking control, see
14053 the ‘Overflow Check Handling in GNAT’ appendix in this
14054 User’s Guide.
14055
14056 Overflow checks are always enabled by this switch. The argument
14057 controls the mode, using the codes
14058
14059
14060 @table @asis
14061
14062 @item @emph{1 = STRICT}
14063
14064 In STRICT mode, intermediate operations are always done using the
14065 base type, and overflow checking ensures that the result is within
14066 the base type range.
14067
14068 @item @emph{2 = MINIMIZED}
14069
14070 In MINIMIZED mode, overflows in intermediate operations are avoided
14071 where possible by using a larger integer type for the computation
14072 (typically @code{Long_Long_Integer}). Overflow checking ensures that
14073 the result fits in this larger integer type.
14074
14075 @item @emph{3 = ELIMINATED}
14076
14077 In ELIMINATED mode, overflows in intermediate operations are avoided
14078 by using multi-precision arithmetic. In this case, overflow checking
14079 has no effect on intermediate operations (since overflow is impossible).
14080 @end table
14081
14082 If two digits are present after @code{-gnato} then the first digit
14083 sets the mode for expressions outside assertions, and the second digit
14084 sets the mode for expressions within assertions. Here assertions is used
14085 in the technical sense (which includes for example precondition and
14086 postcondition expressions).
14087
14088 If one digit is present, the corresponding mode is applicable to both
14089 expressions within and outside assertion expressions.
14090
14091 If no digits are present, the default is to enable overflow checks
14092 and set STRICT mode for both kinds of expressions. This is compatible
14093 with the use of @code{-gnato} in previous versions of GNAT.
14094
14095 @geindex Machine_Overflows
14096
14097 Note that the @code{-gnato??} switch does not affect the code generated
14098 for any floating-point operations; it applies only to integer semantics.
14099 For floating-point, GNAT has the @code{Machine_Overflows}
14100 attribute set to @code{False} and the normal mode of operation is to
14101 generate IEEE NaN and infinite values on overflow or invalid operations
14102 (such as dividing 0.0 by 0.0).
14103
14104 The reason that we distinguish overflow checking from other kinds of
14105 range constraint checking is that a failure of an overflow check, unlike
14106 for example the failure of a range check, can result in an incorrect
14107 value, but cannot cause random memory destruction (like an out of range
14108 subscript), or a wild jump (from an out of range case value). Overflow
14109 checking is also quite expensive in time and space, since in general it
14110 requires the use of double length arithmetic.
14111
14112 Note again that the default is @code{-gnato11} (equivalent to @code{-gnato1}),
14113 so overflow checking is performed in STRICT mode by default.
14114 @end table
14115
14116 @geindex -gnatE (gcc)
14117
14118 @geindex Elaboration checks
14119
14120 @geindex Check
14121 @geindex elaboration
14122
14123
14124 @table @asis
14125
14126 @item @code{-gnatE}
14127
14128 Enables dynamic checks for access-before-elaboration
14129 on subprogram calls and generic instantiations.
14130 Note that @code{-gnatE} is not necessary for safety, because in the
14131 default mode, GNAT ensures statically that the checks would not fail.
14132 For full details of the effect and use of this switch,
14133 @ref{c7,,Compiling with gcc}.
14134 @end table
14135
14136 @geindex -fstack-check (gcc)
14137
14138 @geindex Stack Overflow Checking
14139
14140 @geindex Checks
14141 @geindex stack overflow checking
14142
14143
14144 @table @asis
14145
14146 @item @code{-fstack-check}
14147
14148 Activates stack overflow checking. For full details of the effect and use of
14149 this switch see @ref{e5,,Stack Overflow Checking}.
14150 @end table
14151
14152 @geindex Unsuppress
14153
14154 The setting of these switches only controls the default setting of the
14155 checks. You may modify them using either @code{Suppress} (to remove
14156 checks) or @code{Unsuppress} (to add back suppressed checks) pragmas in
14157 the program source.
14158
14159 @node Using gcc for Syntax Checking,Using gcc for Semantic Checking,Run-Time Checks,Compiler Switches
14160 @anchor{gnat_ugn/building_executable_programs_with_gnat id20}@anchor{f6}@anchor{gnat_ugn/building_executable_programs_with_gnat using-gcc-for-syntax-checking}@anchor{f7}
14161 @subsection Using @code{gcc} for Syntax Checking
14162
14163
14164 @geindex -gnats (gcc)
14165
14166
14167 @table @asis
14168
14169 @item @code{-gnats}
14170
14171 The @code{s} stands for ‘syntax’.
14172
14173 Run GNAT in syntax checking only mode. For
14174 example, the command
14175
14176 @example
14177 $ gcc -c -gnats x.adb
14178 @end example
14179
14180 compiles file @code{x.adb} in syntax-check-only mode. You can check a
14181 series of files in a single command
14182 , and can use wildcards to specify such a group of files.
14183 Note that you must specify the @code{-c} (compile
14184 only) flag in addition to the @code{-gnats} flag.
14185
14186 You may use other switches in conjunction with @code{-gnats}. In
14187 particular, @code{-gnatl} and @code{-gnatv} are useful to control the
14188 format of any generated error messages.
14189
14190 When the source file is empty or contains only empty lines and/or comments,
14191 the output is a warning:
14192
14193 @example
14194 $ gcc -c -gnats -x ada toto.txt
14195 toto.txt:1:01: warning: empty file, contains no compilation units
14196 $
14197 @end example
14198
14199 Otherwise, the output is simply the error messages, if any. No object file or
14200 ALI file is generated by a syntax-only compilation. Also, no units other
14201 than the one specified are accessed. For example, if a unit @code{X}
14202 @emph{with}s a unit @code{Y}, compiling unit @code{X} in syntax
14203 check only mode does not access the source file containing unit
14204 @code{Y}.
14205
14206 @geindex Multiple units
14207 @geindex syntax checking
14208
14209 Normally, GNAT allows only a single unit in a source file. However, this
14210 restriction does not apply in syntax-check-only mode, and it is possible
14211 to check a file containing multiple compilation units concatenated
14212 together. This is primarily used by the @code{gnatchop} utility
14213 (@ref{1d,,Renaming Files with gnatchop}).
14214 @end table
14215
14216 @node Using gcc for Semantic Checking,Compiling Different Versions of Ada,Using gcc for Syntax Checking,Compiler Switches
14217 @anchor{gnat_ugn/building_executable_programs_with_gnat id21}@anchor{f8}@anchor{gnat_ugn/building_executable_programs_with_gnat using-gcc-for-semantic-checking}@anchor{f9}
14218 @subsection Using @code{gcc} for Semantic Checking
14219
14220
14221 @geindex -gnatc (gcc)
14222
14223
14224 @table @asis
14225
14226 @item @code{-gnatc}
14227
14228 The @code{c} stands for ‘check’.
14229 Causes the compiler to operate in semantic check mode,
14230 with full checking for all illegalities specified in the
14231 Ada Reference Manual, but without generation of any object code
14232 (no object file is generated).
14233
14234 Because dependent files must be accessed, you must follow the GNAT
14235 semantic restrictions on file structuring to operate in this mode:
14236
14237
14238 @itemize *
14239
14240 @item
14241 The needed source files must be accessible
14242 (see @ref{73,,Search Paths and the Run-Time Library (RTL)}).
14243
14244 @item
14245 Each file must contain only one compilation unit.
14246
14247 @item
14248 The file name and unit name must match (@ref{3b,,File Naming Rules}).
14249 @end itemize
14250
14251 The output consists of error messages as appropriate. No object file is
14252 generated. An @code{ALI} file is generated for use in the context of
14253 cross-reference tools, but this file is marked as not being suitable
14254 for binding (since no object file is generated).
14255 The checking corresponds exactly to the notion of
14256 legality in the Ada Reference Manual.
14257
14258 Any unit can be compiled in semantics-checking-only mode, including
14259 units that would not normally be compiled (subunits,
14260 and specifications where a separate body is present).
14261 @end table
14262
14263 @node Compiling Different Versions of Ada,Character Set Control,Using gcc for Semantic Checking,Compiler Switches
14264 @anchor{gnat_ugn/building_executable_programs_with_gnat compiling-different-versions-of-ada}@anchor{6}@anchor{gnat_ugn/building_executable_programs_with_gnat id22}@anchor{fa}
14265 @subsection Compiling Different Versions of Ada
14266
14267
14268 The switches described in this section allow you to explicitly specify
14269 the version of the Ada language that your programs are written in.
14270 The default mode is Ada 2012,
14271 but you can also specify Ada 95, Ada 2005 mode, or
14272 indicate Ada 83 compatibility mode.
14273
14274 @geindex Compatibility with Ada 83
14275
14276 @geindex -gnat83 (gcc)
14277
14278 @geindex ACVC
14279 @geindex Ada 83 tests
14280
14281 @geindex Ada 83 mode
14282
14283
14284 @table @asis
14285
14286 @item @code{-gnat83} (Ada 83 Compatibility Mode)
14287
14288 Although GNAT is primarily an Ada 95 / Ada 2005 compiler, this switch
14289 specifies that the program is to be compiled in Ada 83 mode. With
14290 @code{-gnat83}, GNAT rejects most post-Ada 83 extensions and applies Ada 83
14291 semantics where this can be done easily.
14292 It is not possible to guarantee this switch does a perfect
14293 job; some subtle tests, such as are
14294 found in earlier ACVC tests (and that have been removed from the ACATS suite
14295 for Ada 95), might not compile correctly.
14296 Nevertheless, this switch may be useful in some circumstances, for example
14297 where, due to contractual reasons, existing code needs to be maintained
14298 using only Ada 83 features.
14299
14300 With few exceptions (most notably the need to use @code{<>} on
14301 unconstrained
14302 @geindex Generic formal parameters
14303 generic formal parameters,
14304 the use of the new Ada 95 / Ada 2005
14305 reserved words, and the use of packages
14306 with optional bodies), it is not necessary to specify the
14307 @code{-gnat83} switch when compiling Ada 83 programs, because, with rare
14308 exceptions, Ada 95 and Ada 2005 are upwardly compatible with Ada 83. Thus
14309 a correct Ada 83 program is usually also a correct program
14310 in these later versions of the language standard. For further information
14311 please refer to the @emph{Compatibility and Porting Guide} chapter in the
14312 @cite{GNAT Reference Manual}.
14313 @end table
14314
14315 @geindex -gnat95 (gcc)
14316
14317 @geindex Ada 95 mode
14318
14319
14320 @table @asis
14321
14322 @item @code{-gnat95} (Ada 95 mode)
14323
14324 This switch directs the compiler to implement the Ada 95 version of the
14325 language.
14326 Since Ada 95 is almost completely upwards
14327 compatible with Ada 83, Ada 83 programs may generally be compiled using
14328 this switch (see the description of the @code{-gnat83} switch for further
14329 information about Ada 83 mode).
14330 If an Ada 2005 program is compiled in Ada 95 mode,
14331 uses of the new Ada 2005 features will cause error
14332 messages or warnings.
14333
14334 This switch also can be used to cancel the effect of a previous
14335 @code{-gnat83}, @code{-gnat05/2005}, or @code{-gnat12/2012}
14336 switch earlier in the command line.
14337 @end table
14338
14339 @geindex -gnat05 (gcc)
14340
14341 @geindex -gnat2005 (gcc)
14342
14343 @geindex Ada 2005 mode
14344
14345
14346 @table @asis
14347
14348 @item @code{-gnat05} or @code{-gnat2005} (Ada 2005 mode)
14349
14350 This switch directs the compiler to implement the Ada 2005 version of the
14351 language, as documented in the official Ada standards document.
14352 Since Ada 2005 is almost completely upwards
14353 compatible with Ada 95 (and thus also with Ada 83), Ada 83 and Ada 95 programs
14354 may generally be compiled using this switch (see the description of the
14355 @code{-gnat83} and @code{-gnat95} switches for further
14356 information).
14357 @end table
14358
14359 @geindex -gnat12 (gcc)
14360
14361 @geindex -gnat2012 (gcc)
14362
14363 @geindex Ada 2012 mode
14364
14365
14366 @table @asis
14367
14368 @item @code{-gnat12} or @code{-gnat2012} (Ada 2012 mode)
14369
14370 This switch directs the compiler to implement the Ada 2012 version of the
14371 language (also the default).
14372 Since Ada 2012 is almost completely upwards
14373 compatible with Ada 2005 (and thus also with Ada 83, and Ada 95),
14374 Ada 83 and Ada 95 programs
14375 may generally be compiled using this switch (see the description of the
14376 @code{-gnat83}, @code{-gnat95}, and @code{-gnat05/2005} switches
14377 for further information).
14378 @end table
14379
14380 @geindex -gnat2022 (gcc)
14381
14382 @geindex Ada 2022 mode
14383
14384
14385 @table @asis
14386
14387 @item @code{-gnat2022} (Ada 2022 mode)
14388
14389 This switch directs the compiler to implement the Ada 2022 version of the
14390 language.
14391 @end table
14392
14393 @geindex -gnatX (gcc)
14394
14395 @geindex Ada language extensions
14396
14397 @geindex GNAT extensions
14398
14399
14400 @table @asis
14401
14402 @item @code{-gnatX} (Enable GNAT Extensions)
14403
14404 This switch directs the compiler to implement the latest version of the
14405 language (currently Ada 2022) and also to enable certain GNAT implementation
14406 extensions that are not part of any Ada standard. For a full list of these
14407 extensions, see the GNAT reference manual, @code{Pragma Extensions_Allowed}.
14408 @end table
14409
14410 @node Character Set Control,File Naming Control,Compiling Different Versions of Ada,Compiler Switches
14411 @anchor{gnat_ugn/building_executable_programs_with_gnat character-set-control}@anchor{31}@anchor{gnat_ugn/building_executable_programs_with_gnat id23}@anchor{fb}
14412 @subsection Character Set Control
14413
14414
14415 @geindex -gnati (gcc)
14416
14417
14418 @table @asis
14419
14420 @item @code{-gnati@emph{c}}
14421
14422 Normally GNAT recognizes the Latin-1 character set in source program
14423 identifiers, as described in the Ada Reference Manual.
14424 This switch causes
14425 GNAT to recognize alternate character sets in identifiers. @code{c} is a
14426 single character indicating the character set, as follows:
14427
14428
14429 @multitable {xxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
14430 @item
14431
14432 @emph{1}
14433
14434 @tab
14435
14436 ISO 8859-1 (Latin-1) identifiers
14437
14438 @item
14439
14440 @emph{2}
14441
14442 @tab
14443
14444 ISO 8859-2 (Latin-2) letters allowed in identifiers
14445
14446 @item
14447
14448 @emph{3}
14449
14450 @tab
14451
14452 ISO 8859-3 (Latin-3) letters allowed in identifiers
14453
14454 @item
14455
14456 @emph{4}
14457
14458 @tab
14459
14460 ISO 8859-4 (Latin-4) letters allowed in identifiers
14461
14462 @item
14463
14464 @emph{5}
14465
14466 @tab
14467
14468 ISO 8859-5 (Cyrillic) letters allowed in identifiers
14469
14470 @item
14471
14472 @emph{9}
14473
14474 @tab
14475
14476 ISO 8859-15 (Latin-9) letters allowed in identifiers
14477
14478 @item
14479
14480 @emph{p}
14481
14482 @tab
14483
14484 IBM PC letters (code page 437) allowed in identifiers
14485
14486 @item
14487
14488 @emph{8}
14489
14490 @tab
14491
14492 IBM PC letters (code page 850) allowed in identifiers
14493
14494 @item
14495
14496 @emph{f}
14497
14498 @tab
14499
14500 Full upper-half codes allowed in identifiers
14501
14502 @item
14503
14504 @emph{n}
14505
14506 @tab
14507
14508 No upper-half codes allowed in identifiers
14509
14510 @item
14511
14512 @emph{w}
14513
14514 @tab
14515
14516 Wide-character codes (that is, codes greater than 255)
14517 allowed in identifiers
14518
14519 @end multitable
14520
14521
14522 See @ref{23,,Foreign Language Representation} for full details on the
14523 implementation of these character sets.
14524 @end table
14525
14526 @geindex -gnatW (gcc)
14527
14528
14529 @table @asis
14530
14531 @item @code{-gnatW@emph{e}}
14532
14533 Specify the method of encoding for wide characters.
14534 @code{e} is one of the following:
14535
14536
14537 @multitable {xxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
14538 @item
14539
14540 @emph{h}
14541
14542 @tab
14543
14544 Hex encoding (brackets coding also recognized)
14545
14546 @item
14547
14548 @emph{u}
14549
14550 @tab
14551
14552 Upper half encoding (brackets encoding also recognized)
14553
14554 @item
14555
14556 @emph{s}
14557
14558 @tab
14559
14560 Shift/JIS encoding (brackets encoding also recognized)
14561
14562 @item
14563
14564 @emph{e}
14565
14566 @tab
14567
14568 EUC encoding (brackets encoding also recognized)
14569
14570 @item
14571
14572 @emph{8}
14573
14574 @tab
14575
14576 UTF-8 encoding (brackets encoding also recognized)
14577
14578 @item
14579
14580 @emph{b}
14581
14582 @tab
14583
14584 Brackets encoding only (default value)
14585
14586 @end multitable
14587
14588
14589 For full details on these encoding
14590 methods see @ref{37,,Wide_Character Encodings}.
14591 Note that brackets coding is always accepted, even if one of the other
14592 options is specified, so for example @code{-gnatW8} specifies that both
14593 brackets and UTF-8 encodings will be recognized. The units that are
14594 with’ed directly or indirectly will be scanned using the specified
14595 representation scheme, and so if one of the non-brackets scheme is
14596 used, it must be used consistently throughout the program. However,
14597 since brackets encoding is always recognized, it may be conveniently
14598 used in standard libraries, allowing these libraries to be used with
14599 any of the available coding schemes.
14600
14601 Note that brackets encoding only applies to program text. Within comments,
14602 brackets are considered to be normal graphic characters, and bracket sequences
14603 are never recognized as wide characters.
14604
14605 If no @code{-gnatW?} parameter is present, then the default
14606 representation is normally Brackets encoding only. However, if the
14607 first three characters of the file are 16#EF# 16#BB# 16#BF# (the standard
14608 byte order mark or BOM for UTF-8), then these three characters are
14609 skipped and the default representation for the file is set to UTF-8.
14610
14611 Note that the wide character representation that is specified (explicitly
14612 or by default) for the main program also acts as the default encoding used
14613 for Wide_Text_IO files if not specifically overridden by a WCEM form
14614 parameter.
14615 @end table
14616
14617 When no @code{-gnatW?} is specified, then characters (other than wide
14618 characters represented using brackets notation) are treated as 8-bit
14619 Latin-1 codes. The codes recognized are the Latin-1 graphic characters,
14620 and ASCII format effectors (CR, LF, HT, VT). Other lower half control
14621 characters in the range 16#00#..16#1F# are not accepted in program text
14622 or in comments. Upper half control characters (16#80#..16#9F#) are rejected
14623 in program text, but allowed and ignored in comments. Note in particular
14624 that the Next Line (NEL) character whose encoding is 16#85# is not recognized
14625 as an end of line in this default mode. If your source program contains
14626 instances of the NEL character used as a line terminator,
14627 you must use UTF-8 encoding for the whole
14628 source program. In default mode, all lines must be ended by a standard
14629 end of line sequence (CR, CR/LF, or LF).
14630
14631 Note that the convention of simply accepting all upper half characters in
14632 comments means that programs that use standard ASCII for program text, but
14633 UTF-8 encoding for comments are accepted in default mode, providing that the
14634 comments are ended by an appropriate (CR, or CR/LF, or LF) line terminator.
14635 This is a common mode for many programs with foreign language comments.
14636
14637 @node File Naming Control,Subprogram Inlining Control,Character Set Control,Compiler Switches
14638 @anchor{gnat_ugn/building_executable_programs_with_gnat file-naming-control}@anchor{fc}@anchor{gnat_ugn/building_executable_programs_with_gnat id24}@anchor{fd}
14639 @subsection File Naming Control
14640
14641
14642 @geindex -gnatk (gcc)
14643
14644
14645 @table @asis
14646
14647 @item @code{-gnatk@emph{n}}
14648
14649 Activates file name ‘krunching’. @code{n}, a decimal integer in the range
14650 1-999, indicates the maximum allowable length of a file name (not
14651 including the @code{.ads} or @code{.adb} extension). The default is not
14652 to enable file name krunching.
14653
14654 For the source file naming rules, @ref{3b,,File Naming Rules}.
14655 @end table
14656
14657 @node Subprogram Inlining Control,Auxiliary Output Control,File Naming Control,Compiler Switches
14658 @anchor{gnat_ugn/building_executable_programs_with_gnat id25}@anchor{fe}@anchor{gnat_ugn/building_executable_programs_with_gnat subprogram-inlining-control}@anchor{ff}
14659 @subsection Subprogram Inlining Control
14660
14661
14662 @geindex -gnatn (gcc)
14663
14664
14665 @table @asis
14666
14667 @item @code{-gnatn[12]}
14668
14669 The @code{n} here is intended to suggest the first syllable of the word ‘inline’.
14670 GNAT recognizes and processes @code{Inline} pragmas. However, for inlining to
14671 actually occur, optimization must be enabled and, by default, inlining of
14672 subprograms across units is not performed. If you want to additionally
14673 enable inlining of subprograms specified by pragma @code{Inline} across units,
14674 you must also specify this switch.
14675
14676 In the absence of this switch, GNAT does not attempt inlining across units
14677 and does not access the bodies of subprograms for which @code{pragma Inline} is
14678 specified if they are not in the current unit.
14679
14680 You can optionally specify the inlining level: 1 for moderate inlining across
14681 units, which is a good compromise between compilation times and performances
14682 at run time, or 2 for full inlining across units, which may bring about
14683 longer compilation times. If no inlining level is specified, the compiler will
14684 pick it based on the optimization level: 1 for @code{-O1}, @code{-O2} or
14685 @code{-Os} and 2 for @code{-O3}.
14686
14687 If you specify this switch the compiler will access these bodies,
14688 creating an extra source dependency for the resulting object file, and
14689 where possible, the call will be inlined.
14690 For further details on when inlining is possible
14691 see @ref{100,,Inlining of Subprograms}.
14692 @end table
14693
14694 @geindex -gnatN (gcc)
14695
14696
14697 @table @asis
14698
14699 @item @code{-gnatN}
14700
14701 This switch activates front-end inlining which also
14702 generates additional dependencies.
14703
14704 When using a gcc-based back end, then the use of
14705 @code{-gnatN} is deprecated, and the use of @code{-gnatn} is preferred.
14706 Historically front end inlining was more extensive than the gcc back end
14707 inlining, but that is no longer the case.
14708 @end table
14709
14710 @node Auxiliary Output Control,Debugging Control,Subprogram Inlining Control,Compiler Switches
14711 @anchor{gnat_ugn/building_executable_programs_with_gnat auxiliary-output-control}@anchor{101}@anchor{gnat_ugn/building_executable_programs_with_gnat id26}@anchor{102}
14712 @subsection Auxiliary Output Control
14713
14714
14715 @geindex -gnatu (gcc)
14716
14717
14718 @table @asis
14719
14720 @item @code{-gnatu}
14721
14722 Print a list of units required by this compilation on @code{stdout}.
14723 The listing includes all units on which the unit being compiled depends
14724 either directly or indirectly.
14725 @end table
14726
14727 @geindex -pass-exit-codes (gcc)
14728
14729
14730 @table @asis
14731
14732 @item @code{-pass-exit-codes}
14733
14734 If this switch is not used, the exit code returned by @code{gcc} when
14735 compiling multiple files indicates whether all source files have
14736 been successfully used to generate object files or not.
14737
14738 When @code{-pass-exit-codes} is used, @code{gcc} exits with an extended
14739 exit status and allows an integrated development environment to better
14740 react to a compilation failure. Those exit status are:
14741
14742
14743 @multitable {xxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
14744 @item
14745
14746 @emph{5}
14747
14748 @tab
14749
14750 There was an error in at least one source file.
14751
14752 @item
14753
14754 @emph{3}
14755
14756 @tab
14757
14758 At least one source file did not generate an object file.
14759
14760 @item
14761
14762 @emph{2}
14763
14764 @tab
14765
14766 The compiler died unexpectedly (internal error for example).
14767
14768 @item
14769
14770 @emph{0}
14771
14772 @tab
14773
14774 An object file has been generated for every source file.
14775
14776 @end multitable
14777
14778 @end table
14779
14780 @node Debugging Control,Exception Handling Control,Auxiliary Output Control,Compiler Switches
14781 @anchor{gnat_ugn/building_executable_programs_with_gnat debugging-control}@anchor{103}@anchor{gnat_ugn/building_executable_programs_with_gnat id27}@anchor{104}
14782 @subsection Debugging Control
14783
14784
14785 @quotation
14786
14787 @geindex Debugging options
14788 @end quotation
14789
14790 @geindex -gnatd (gcc)
14791
14792
14793 @table @asis
14794
14795 @item @code{-gnatd@emph{x}}
14796
14797 Activate internal debugging switches. @code{x} is a letter or digit, or
14798 string of letters or digits, which specifies the type of debugging
14799 outputs desired. Normally these are used only for internal development
14800 or system debugging purposes. You can find full documentation for these
14801 switches in the body of the @code{Debug} unit in the compiler source
14802 file @code{debug.adb}.
14803 @end table
14804
14805 @geindex -gnatG (gcc)
14806
14807
14808 @table @asis
14809
14810 @item @code{-gnatG[=@emph{nn}]}
14811
14812 This switch causes the compiler to generate auxiliary output containing
14813 a pseudo-source listing of the generated expanded code. Like most Ada
14814 compilers, GNAT works by first transforming the high level Ada code into
14815 lower level constructs. For example, tasking operations are transformed
14816 into calls to the tasking run-time routines. A unique capability of GNAT
14817 is to list this expanded code in a form very close to normal Ada source.
14818 This is very useful in understanding the implications of various Ada
14819 usage on the efficiency of the generated code. There are many cases in
14820 Ada (e.g., the use of controlled types), where simple Ada statements can
14821 generate a lot of run-time code. By using @code{-gnatG} you can identify
14822 these cases, and consider whether it may be desirable to modify the coding
14823 approach to improve efficiency.
14824
14825 The optional parameter @code{nn} if present after -gnatG specifies an
14826 alternative maximum line length that overrides the normal default of 72.
14827 This value is in the range 40-999999, values less than 40 being silently
14828 reset to 40. The equal sign is optional.
14829
14830 The format of the output is very similar to standard Ada source, and is
14831 easily understood by an Ada programmer. The following special syntactic
14832 additions correspond to low level features used in the generated code that
14833 do not have any exact analogies in pure Ada source form. The following
14834 is a partial list of these special constructions. See the spec
14835 of package @code{Sprint} in file @code{sprint.ads} for a full list.
14836
14837 @geindex -gnatL (gcc)
14838
14839 If the switch @code{-gnatL} is used in conjunction with
14840 @code{-gnatG}, then the original source lines are interspersed
14841 in the expanded source (as comment lines with the original line number).
14842
14843
14844 @table @asis
14845
14846 @item @code{new @emph{xxx} [storage_pool = @emph{yyy}]}
14847
14848 Shows the storage pool being used for an allocator.
14849
14850 @item @code{at end @emph{procedure-name};}
14851
14852 Shows the finalization (cleanup) procedure for a scope.
14853
14854 @item @code{(if @emph{expr} then @emph{expr} else @emph{expr})}
14855
14856 Conditional expression equivalent to the @code{x?y:z} construction in C.
14857
14858 @item @code{@emph{target}^(@emph{source})}
14859
14860 A conversion with floating-point truncation instead of rounding.
14861
14862 @item @code{@emph{target}?(@emph{source})}
14863
14864 A conversion that bypasses normal Ada semantic checking. In particular
14865 enumeration types and fixed-point types are treated simply as integers.
14866
14867 @item @code{@emph{target}?^(@emph{source})}
14868
14869 Combines the above two cases.
14870 @end table
14871
14872 @code{@emph{x} #/ @emph{y}}
14873
14874 @code{@emph{x} #mod @emph{y}}
14875
14876 @code{@emph{x} # @emph{y}}
14877
14878
14879 @table @asis
14880
14881 @item @code{@emph{x} #rem @emph{y}}
14882
14883 A division or multiplication of fixed-point values which are treated as
14884 integers without any kind of scaling.
14885
14886 @item @code{free @emph{expr} [storage_pool = @emph{xxx}]}
14887
14888 Shows the storage pool associated with a @code{free} statement.
14889
14890 @item @code{[subtype or type declaration]}
14891
14892 Used to list an equivalent declaration for an internally generated
14893 type that is referenced elsewhere in the listing.
14894
14895 @item @code{freeze @emph{type-name} [@emph{actions}]}
14896
14897 Shows the point at which @code{type-name} is frozen, with possible
14898 associated actions to be performed at the freeze point.
14899
14900 @item @code{reference @emph{itype}}
14901
14902 Reference (and hence definition) to internal type @code{itype}.
14903
14904 @item @code{@emph{function-name}! (@emph{arg}, @emph{arg}, @emph{arg})}
14905
14906 Intrinsic function call.
14907
14908 @item @code{@emph{label-name} : label}
14909
14910 Declaration of label @code{labelname}.
14911
14912 @item @code{#$ @emph{subprogram-name}}
14913
14914 An implicit call to a run-time support routine
14915 (to meet the requirement of H.3.1(9) in a
14916 convenient manner).
14917
14918 @item @code{@emph{expr} && @emph{expr} && @emph{expr} ... && @emph{expr}}
14919
14920 A multiple concatenation (same effect as @code{expr} & @code{expr} &
14921 @code{expr}, but handled more efficiently).
14922
14923 @item @code{[constraint_error]}
14924
14925 Raise the @code{Constraint_Error} exception.
14926
14927 @item @code{@emph{expression}'reference}
14928
14929 A pointer to the result of evaluating @{expression@}.
14930
14931 @item @code{@emph{target-type}!(@emph{source-expression})}
14932
14933 An unchecked conversion of @code{source-expression} to @code{target-type}.
14934
14935 @item @code{[@emph{numerator}/@emph{denominator}]}
14936
14937 Used to represent internal real literals (that) have no exact
14938 representation in base 2-16 (for example, the result of compile time
14939 evaluation of the expression 1.0/27.0).
14940 @end table
14941 @end table
14942
14943 @geindex -gnatD (gcc)
14944
14945
14946 @table @asis
14947
14948 @item @code{-gnatD[=nn]}
14949
14950 When used in conjunction with @code{-gnatG}, this switch causes
14951 the expanded source, as described above for
14952 @code{-gnatG} to be written to files with names
14953 @code{xxx.dg}, where @code{xxx} is the normal file name,
14954 instead of to the standard output file. For
14955 example, if the source file name is @code{hello.adb}, then a file
14956 @code{hello.adb.dg} will be written. The debugging
14957 information generated by the @code{gcc} @code{-g} switch
14958 will refer to the generated @code{xxx.dg} file. This allows
14959 you to do source level debugging using the generated code which is
14960 sometimes useful for complex code, for example to find out exactly
14961 which part of a complex construction raised an exception. This switch
14962 also suppresses generation of cross-reference information (see
14963 @code{-gnatx}) since otherwise the cross-reference information
14964 would refer to the @code{.dg} file, which would cause
14965 confusion since this is not the original source file.
14966
14967 Note that @code{-gnatD} actually implies @code{-gnatG}
14968 automatically, so it is not necessary to give both options.
14969 In other words @code{-gnatD} is equivalent to @code{-gnatDG}).
14970
14971 @geindex -gnatL (gcc)
14972
14973 If the switch @code{-gnatL} is used in conjunction with
14974 @code{-gnatDG}, then the original source lines are interspersed
14975 in the expanded source (as comment lines with the original line number).
14976
14977 The optional parameter @code{nn} if present after -gnatD specifies an
14978 alternative maximum line length that overrides the normal default of 72.
14979 This value is in the range 40-999999, values less than 40 being silently
14980 reset to 40. The equal sign is optional.
14981 @end table
14982
14983 @geindex -gnatr (gcc)
14984
14985 @geindex pragma Restrictions
14986
14987
14988 @table @asis
14989
14990 @item @code{-gnatr}
14991
14992 This switch causes pragma Restrictions to be treated as Restriction_Warnings
14993 so that violation of restrictions causes warnings rather than illegalities.
14994 This is useful during the development process when new restrictions are added
14995 or investigated. The switch also causes pragma Profile to be treated as
14996 Profile_Warnings, and pragma Restricted_Run_Time and pragma Ravenscar set
14997 restriction warnings rather than restrictions.
14998 @end table
14999
15000 @geindex -gnatR (gcc)
15001
15002
15003 @table @asis
15004
15005 @item @code{-gnatR[0|1|2|3|4][e][j][m][s]}
15006
15007 This switch controls output from the compiler of a listing showing
15008 representation information for declared types, objects and subprograms.
15009 For @code{-gnatR0}, no information is output (equivalent to omitting
15010 the @code{-gnatR} switch). For @code{-gnatR1} (which is the default,
15011 so @code{-gnatR} with no parameter has the same effect), size and
15012 alignment information is listed for declared array and record types.
15013
15014 For @code{-gnatR2}, size and alignment information is listed for all
15015 declared types and objects. The @code{Linker_Section} is also listed for any
15016 entity for which the @code{Linker_Section} is set explicitly or implicitly (the
15017 latter case occurs for objects of a type for which a @code{Linker_Section}
15018 is set).
15019
15020 For @code{-gnatR3}, symbolic expressions for values that are computed
15021 at run time for records are included. These symbolic expressions have
15022 a mostly obvious format with #n being used to represent the value of the
15023 n’th discriminant. See source files @code{repinfo.ads/adb} in the
15024 GNAT sources for full details on the format of @code{-gnatR3} output.
15025
15026 For @code{-gnatR4}, information for relevant compiler-generated types
15027 is also listed, i.e. when they are structurally part of other declared
15028 types and objects.
15029
15030 If the switch is followed by an @code{e} (e.g. @code{-gnatR2e}), then
15031 extended representation information for record sub-components of records
15032 is included.
15033
15034 If the switch is followed by an @code{m} (e.g. @code{-gnatRm}), then
15035 subprogram conventions and parameter passing mechanisms for all the
15036 subprograms are included.
15037
15038 If the switch is followed by a @code{j} (e.g., @code{-gnatRj}), then
15039 the output is in the JSON data interchange format specified by the
15040 ECMA-404 standard. The semantic description of this JSON output is
15041 available in the specification of the Repinfo unit present in the
15042 compiler sources.
15043
15044 If the switch is followed by an @code{s} (e.g., @code{-gnatR3s}), then
15045 the output is to a file with the name @code{file.rep} where @code{file} is
15046 the name of the corresponding source file, except if @code{j} is also
15047 specified, in which case the file name is @code{file.json}.
15048
15049 Note that it is possible for record components to have zero size. In
15050 this case, the component clause uses an obvious extension of permitted
15051 Ada syntax, for example @code{at 0 range 0 .. -1}.
15052 @end table
15053
15054 @geindex -gnatS (gcc)
15055
15056
15057 @table @asis
15058
15059 @item @code{-gnatS}
15060
15061 The use of the switch @code{-gnatS} for an
15062 Ada compilation will cause the compiler to output a
15063 representation of package Standard in a form very
15064 close to standard Ada. It is not quite possible to
15065 do this entirely in standard Ada (since new
15066 numeric base types cannot be created in standard
15067 Ada), but the output is easily
15068 readable to any Ada programmer, and is useful to
15069 determine the characteristics of target dependent
15070 types in package Standard.
15071 @end table
15072
15073 @geindex -gnatx (gcc)
15074
15075
15076 @table @asis
15077
15078 @item @code{-gnatx}
15079
15080 Normally the compiler generates full cross-referencing information in
15081 the @code{ALI} file. This information is used by a number of tools,
15082 including @code{gnatfind} and @code{gnatxref}. The @code{-gnatx} switch
15083 suppresses this information. This saves some space and may slightly
15084 speed up compilation, but means that these tools cannot be used.
15085 @end table
15086
15087 @geindex -fgnat-encodings (gcc)
15088
15089
15090 @table @asis
15091
15092 @item @code{-fgnat-encodings=[all|gdb|minimal]}
15093
15094 This switch controls the balance between GNAT encodings and standard DWARF
15095 emitted in the debug information.
15096
15097 Historically, old debug formats like stabs were not powerful enough to
15098 express some Ada types (for instance, variant records or fixed-point types).
15099 To work around this, GNAT introduced proprietary encodings that embed the
15100 missing information (“GNAT encodings”).
15101
15102 Recent versions of the DWARF debug information format are now able to
15103 correctly describe most of these Ada constructs (“standard DWARF”). As
15104 third-party tools started to use this format, GNAT has been enhanced to
15105 generate it. However, most tools (including GDB) are still relying on GNAT
15106 encodings.
15107
15108 To support all tools, GNAT needs to be versatile about the balance between
15109 generation of GNAT encodings and standard DWARF. This is what
15110 @code{-fgnat-encodings} is about.
15111
15112
15113 @itemize *
15114
15115 @item
15116 @code{=all}: Emit all GNAT encodings, and then emit as much standard DWARF as
15117 possible so it does not conflict with GNAT encodings.
15118
15119 @item
15120 @code{=gdb}: Emit as much standard DWARF as possible as long as the current
15121 GDB handles it. Emit GNAT encodings for the rest.
15122
15123 @item
15124 @code{=minimal}: Emit as much standard DWARF as possible and emit GNAT
15125 encodings for the rest.
15126 @end itemize
15127 @end table
15128
15129 @node Exception Handling Control,Units to Sources Mapping Files,Debugging Control,Compiler Switches
15130 @anchor{gnat_ugn/building_executable_programs_with_gnat exception-handling-control}@anchor{105}@anchor{gnat_ugn/building_executable_programs_with_gnat id28}@anchor{106}
15131 @subsection Exception Handling Control
15132
15133
15134 GNAT uses two methods for handling exceptions at run time. The
15135 @code{setjmp/longjmp} method saves the context when entering
15136 a frame with an exception handler. Then when an exception is
15137 raised, the context can be restored immediately, without the
15138 need for tracing stack frames. This method provides very fast
15139 exception propagation, but introduces significant overhead for
15140 the use of exception handlers, even if no exception is raised.
15141
15142 The other approach is called ‘zero cost’ exception handling.
15143 With this method, the compiler builds static tables to describe
15144 the exception ranges. No dynamic code is required when entering
15145 a frame containing an exception handler. When an exception is
15146 raised, the tables are used to control a back trace of the
15147 subprogram invocation stack to locate the required exception
15148 handler. This method has considerably poorer performance for
15149 the propagation of exceptions, but there is no overhead for
15150 exception handlers if no exception is raised. Note that in this
15151 mode and in the context of mixed Ada and C/C++ programming,
15152 to propagate an exception through a C/C++ code, the C/C++ code
15153 must be compiled with the @code{-funwind-tables} GCC’s
15154 option.
15155
15156 The following switches may be used to control which of the
15157 two exception handling methods is used.
15158
15159 @geindex --RTS=sjlj (gnatmake)
15160
15161
15162 @table @asis
15163
15164 @item @code{--RTS=sjlj}
15165
15166 This switch causes the setjmp/longjmp run-time (when available) to be used
15167 for exception handling. If the default
15168 mechanism for the target is zero cost exceptions, then
15169 this switch can be used to modify this default, and must be
15170 used for all units in the partition.
15171 This option is rarely used. One case in which it may be
15172 advantageous is if you have an application where exception
15173 raising is common and the overall performance of the
15174 application is improved by favoring exception propagation.
15175 @end table
15176
15177 @geindex --RTS=zcx (gnatmake)
15178
15179 @geindex Zero Cost Exceptions
15180
15181
15182 @table @asis
15183
15184 @item @code{--RTS=zcx}
15185
15186 This switch causes the zero cost approach to be used
15187 for exception handling. If this is the default mechanism for the
15188 target (see below), then this switch is unneeded. If the default
15189 mechanism for the target is setjmp/longjmp exceptions, then
15190 this switch can be used to modify this default, and must be
15191 used for all units in the partition.
15192 This option can only be used if the zero cost approach
15193 is available for the target in use, otherwise it will generate an error.
15194 @end table
15195
15196 The same option @code{--RTS} must be used both for @code{gcc}
15197 and @code{gnatbind}. Passing this option to @code{gnatmake}
15198 (@ref{ce,,Switches for gnatmake}) will ensure the required consistency
15199 through the compilation and binding steps.
15200
15201 @node Units to Sources Mapping Files,Code Generation Control,Exception Handling Control,Compiler Switches
15202 @anchor{gnat_ugn/building_executable_programs_with_gnat id29}@anchor{107}@anchor{gnat_ugn/building_executable_programs_with_gnat units-to-sources-mapping-files}@anchor{e8}
15203 @subsection Units to Sources Mapping Files
15204
15205
15206 @geindex -gnatem (gcc)
15207
15208
15209 @table @asis
15210
15211 @item @code{-gnatem=@emph{path}}
15212
15213 A mapping file is a way to communicate to the compiler two mappings:
15214 from unit names to file names (without any directory information) and from
15215 file names to path names (with full directory information). These mappings
15216 are used by the compiler to short-circuit the path search.
15217
15218 The use of mapping files is not required for correct operation of the
15219 compiler, but mapping files can improve efficiency, particularly when
15220 sources are read over a slow network connection. In normal operation,
15221 you need not be concerned with the format or use of mapping files,
15222 and the @code{-gnatem} switch is not a switch that you would use
15223 explicitly. It is intended primarily for use by automatic tools such as
15224 @code{gnatmake} running under the project file facility. The
15225 description here of the format of mapping files is provided
15226 for completeness and for possible use by other tools.
15227
15228 A mapping file is a sequence of sets of three lines. In each set, the
15229 first line is the unit name, in lower case, with @code{%s} appended
15230 for specs and @code{%b} appended for bodies; the second line is the
15231 file name; and the third line is the path name.
15232
15233 Example:
15234
15235 @example
15236 main%b
15237 main.2.ada
15238 /gnat/project1/sources/main.2.ada
15239 @end example
15240
15241 When the switch @code{-gnatem} is specified, the compiler will
15242 create in memory the two mappings from the specified file. If there is
15243 any problem (nonexistent file, truncated file or duplicate entries),
15244 no mapping will be created.
15245
15246 Several @code{-gnatem} switches may be specified; however, only the
15247 last one on the command line will be taken into account.
15248
15249 When using a project file, @code{gnatmake} creates a temporary
15250 mapping file and communicates it to the compiler using this switch.
15251 @end table
15252
15253 @node Code Generation Control,,Units to Sources Mapping Files,Compiler Switches
15254 @anchor{gnat_ugn/building_executable_programs_with_gnat code-generation-control}@anchor{108}@anchor{gnat_ugn/building_executable_programs_with_gnat id30}@anchor{109}
15255 @subsection Code Generation Control
15256
15257
15258 The GCC technology provides a wide range of target dependent
15259 @code{-m} switches for controlling
15260 details of code generation with respect to different versions of
15261 architectures. This includes variations in instruction sets (e.g.,
15262 different members of the power pc family), and different requirements
15263 for optimal arrangement of instructions (e.g., different members of
15264 the x86 family). The list of available @code{-m} switches may be
15265 found in the GCC documentation.
15266
15267 Use of these @code{-m} switches may in some cases result in improved
15268 code performance.
15269
15270 The GNAT technology is tested and qualified without any
15271 @code{-m} switches,
15272 so generally the most reliable approach is to avoid the use of these
15273 switches. However, we generally expect most of these switches to work
15274 successfully with GNAT, and many customers have reported successful
15275 use of these options.
15276
15277 Our general advice is to avoid the use of @code{-m} switches unless
15278 special needs lead to requirements in this area. In particular,
15279 there is no point in using @code{-m} switches to improve performance
15280 unless you actually see a performance improvement.
15281
15282 @node Linker Switches,Binding with gnatbind,Compiler Switches,Building Executable Programs with GNAT
15283 @anchor{gnat_ugn/building_executable_programs_with_gnat id31}@anchor{10a}@anchor{gnat_ugn/building_executable_programs_with_gnat linker-switches}@anchor{10b}
15284 @section Linker Switches
15285
15286
15287 Linker switches can be specified after @code{-largs} builder switch.
15288
15289 @geindex -fuse-ld=name
15290
15291
15292 @table @asis
15293
15294 @item @code{-fuse-ld=@emph{name}}
15295
15296 Linker to be used. The default is @code{bfd} for @code{ld.bfd},
15297 the alternative being @code{gold} for @code{ld.gold}. The later is
15298 a more recent and faster linker, but only available on GNU/Linux
15299 platforms.
15300 @end table
15301
15302 @node Binding with gnatbind,Linking with gnatlink,Linker Switches,Building Executable Programs with GNAT
15303 @anchor{gnat_ugn/building_executable_programs_with_gnat binding-with-gnatbind}@anchor{c8}@anchor{gnat_ugn/building_executable_programs_with_gnat id32}@anchor{10c}
15304 @section Binding with @code{gnatbind}
15305
15306
15307 @geindex gnatbind
15308
15309 This chapter describes the GNAT binder, @code{gnatbind}, which is used
15310 to bind compiled GNAT objects.
15311
15312 The @code{gnatbind} program performs four separate functions:
15313
15314
15315 @itemize *
15316
15317 @item
15318 Checks that a program is consistent, in accordance with the rules in
15319 Chapter 10 of the Ada Reference Manual. In particular, error
15320 messages are generated if a program uses inconsistent versions of a
15321 given unit.
15322
15323 @item
15324 Checks that an acceptable order of elaboration exists for the program
15325 and issues an error message if it cannot find an order of elaboration
15326 that satisfies the rules in Chapter 10 of the Ada Language Manual.
15327
15328 @item
15329 Generates a main program incorporating the given elaboration order.
15330 This program is a small Ada package (body and spec) that
15331 must be subsequently compiled
15332 using the GNAT compiler. The necessary compilation step is usually
15333 performed automatically by @code{gnatlink}. The two most important
15334 functions of this program
15335 are to call the elaboration routines of units in an appropriate order
15336 and to call the main program.
15337
15338 @item
15339 Determines the set of object files required by the given main program.
15340 This information is output in the forms of comments in the generated program,
15341 to be read by the @code{gnatlink} utility used to link the Ada application.
15342 @end itemize
15343
15344 @menu
15345 * Running gnatbind::
15346 * Switches for gnatbind::
15347 * Command-Line Access::
15348 * Search Paths for gnatbind::
15349 * Examples of gnatbind Usage::
15350
15351 @end menu
15352
15353 @node Running gnatbind,Switches for gnatbind,,Binding with gnatbind
15354 @anchor{gnat_ugn/building_executable_programs_with_gnat id33}@anchor{10d}@anchor{gnat_ugn/building_executable_programs_with_gnat running-gnatbind}@anchor{10e}
15355 @subsection Running @code{gnatbind}
15356
15357
15358 The form of the @code{gnatbind} command is
15359
15360 @example
15361 $ gnatbind [ switches ] mainprog[.ali] [ switches ]
15362 @end example
15363
15364 where @code{mainprog.adb} is the Ada file containing the main program
15365 unit body. @code{gnatbind} constructs an Ada
15366 package in two files whose names are
15367 @code{b~mainprog.ads}, and @code{b~mainprog.adb}.
15368 For example, if given the
15369 parameter @code{hello.ali}, for a main program contained in file
15370 @code{hello.adb}, the binder output files would be @code{b~hello.ads}
15371 and @code{b~hello.adb}.
15372
15373 When doing consistency checking, the binder takes into consideration
15374 any source files it can locate. For example, if the binder determines
15375 that the given main program requires the package @code{Pack}, whose
15376 @code{.ALI}
15377 file is @code{pack.ali} and whose corresponding source spec file is
15378 @code{pack.ads}, it attempts to locate the source file @code{pack.ads}
15379 (using the same search path conventions as previously described for the
15380 @code{gcc} command). If it can locate this source file, it checks that
15381 the time stamps
15382 or source checksums of the source and its references to in @code{ALI} files
15383 match. In other words, any @code{ALI} files that mentions this spec must have
15384 resulted from compiling this version of the source file (or in the case
15385 where the source checksums match, a version close enough that the
15386 difference does not matter).
15387
15388 @geindex Source files
15389 @geindex use by binder
15390
15391 The effect of this consistency checking, which includes source files, is
15392 that the binder ensures that the program is consistent with the latest
15393 version of the source files that can be located at bind time. Editing a
15394 source file without compiling files that depend on the source file cause
15395 error messages to be generated by the binder.
15396
15397 For example, suppose you have a main program @code{hello.adb} and a
15398 package @code{P}, from file @code{p.ads} and you perform the following
15399 steps:
15400
15401
15402 @itemize *
15403
15404 @item
15405 Enter @code{gcc -c hello.adb} to compile the main program.
15406
15407 @item
15408 Enter @code{gcc -c p.ads} to compile package @code{P}.
15409
15410 @item
15411 Edit file @code{p.ads}.
15412
15413 @item
15414 Enter @code{gnatbind hello}.
15415 @end itemize
15416
15417 At this point, the file @code{p.ali} contains an out-of-date time stamp
15418 because the file @code{p.ads} has been edited. The attempt at binding
15419 fails, and the binder generates the following error messages:
15420
15421 @example
15422 error: "hello.adb" must be recompiled ("p.ads" has been modified)
15423 error: "p.ads" has been modified and must be recompiled
15424 @end example
15425
15426 Now both files must be recompiled as indicated, and then the bind can
15427 succeed, generating a main program. You need not normally be concerned
15428 with the contents of this file, but for reference purposes a sample
15429 binder output file is given in @ref{e,,Example of Binder Output File}.
15430
15431 In most normal usage, the default mode of @code{gnatbind} which is to
15432 generate the main package in Ada, as described in the previous section.
15433 In particular, this means that any Ada programmer can read and understand
15434 the generated main program. It can also be debugged just like any other
15435 Ada code provided the @code{-g} switch is used for
15436 @code{gnatbind} and @code{gnatlink}.
15437
15438 @node Switches for gnatbind,Command-Line Access,Running gnatbind,Binding with gnatbind
15439 @anchor{gnat_ugn/building_executable_programs_with_gnat id34}@anchor{10f}@anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gnatbind}@anchor{110}
15440 @subsection Switches for @code{gnatbind}
15441
15442
15443 The following switches are available with @code{gnatbind}; details will
15444 be presented in subsequent sections.
15445
15446 @geindex --version (gnatbind)
15447
15448
15449 @table @asis
15450
15451 @item @code{--version}
15452
15453 Display Copyright and version, then exit disregarding all other options.
15454 @end table
15455
15456 @geindex --help (gnatbind)
15457
15458
15459 @table @asis
15460
15461 @item @code{--help}
15462
15463 If @code{--version} was not used, display usage, then exit disregarding
15464 all other options.
15465 @end table
15466
15467 @geindex -a (gnatbind)
15468
15469
15470 @table @asis
15471
15472 @item @code{-a}
15473
15474 Indicates that, if supported by the platform, the adainit procedure should
15475 be treated as an initialisation routine by the linker (a constructor). This
15476 is intended to be used by the Project Manager to automatically initialize
15477 shared Stand-Alone Libraries.
15478 @end table
15479
15480 @geindex -aO (gnatbind)
15481
15482
15483 @table @asis
15484
15485 @item @code{-aO}
15486
15487 Specify directory to be searched for ALI files.
15488 @end table
15489
15490 @geindex -aI (gnatbind)
15491
15492
15493 @table @asis
15494
15495 @item @code{-aI}
15496
15497 Specify directory to be searched for source file.
15498 @end table
15499
15500 @geindex -A (gnatbind)
15501
15502
15503 @table @asis
15504
15505 @item @code{-A[=@emph{filename}]}
15506
15507 Output ALI list (to standard output or to the named file).
15508 @end table
15509
15510 @geindex -b (gnatbind)
15511
15512
15513 @table @asis
15514
15515 @item @code{-b}
15516
15517 Generate brief messages to @code{stderr} even if verbose mode set.
15518 @end table
15519
15520 @geindex -c (gnatbind)
15521
15522
15523 @table @asis
15524
15525 @item @code{-c}
15526
15527 Check only, no generation of binder output file.
15528 @end table
15529
15530 @geindex -dnn[k|m] (gnatbind)
15531
15532
15533 @table @asis
15534
15535 @item @code{-d@emph{nn}[k|m]}
15536
15537 This switch can be used to change the default task stack size value
15538 to a specified size @code{nn}, which is expressed in bytes by default, or
15539 in kilobytes when suffixed with @code{k} or in megabytes when suffixed
15540 with @code{m}.
15541 In the absence of a @code{[k|m]} suffix, this switch is equivalent,
15542 in effect, to completing all task specs with
15543
15544 @example
15545 pragma Storage_Size (nn);
15546 @end example
15547
15548 When they do not already have such a pragma.
15549 @end table
15550
15551 @geindex -D (gnatbind)
15552
15553
15554 @table @asis
15555
15556 @item @code{-D@emph{nn}[k|m]}
15557
15558 Set the default secondary stack size to @code{nn}. The suffix indicates whether
15559 the size is in bytes (no suffix), kilobytes (@code{k} suffix) or megabytes
15560 (@code{m} suffix).
15561
15562 The secondary stack holds objects of unconstrained types that are returned by
15563 functions, for example unconstrained Strings. The size of the secondary stack
15564 can be dynamic or fixed depending on the target.
15565
15566 For most targets, the secondary stack grows on demand and is implemented as
15567 a chain of blocks in the heap. In this case, the default secondary stack size
15568 determines the initial size of the secondary stack for each task and the
15569 smallest amount the secondary stack can grow by.
15570
15571 For Ravenscar, ZFP, and Cert run-times the size of the secondary stack is
15572 fixed. This switch can be used to change the default size of these stacks.
15573 The default secondary stack size can be overridden on a per-task basis if
15574 individual tasks have different secondary stack requirements. This is
15575 achieved through the Secondary_Stack_Size aspect that takes the size of the
15576 secondary stack in bytes.
15577 @end table
15578
15579 @geindex -e (gnatbind)
15580
15581
15582 @table @asis
15583
15584 @item @code{-e}
15585
15586 Output complete list of elaboration-order dependencies.
15587 @end table
15588
15589 @geindex -Ea (gnatbind)
15590
15591
15592 @table @asis
15593
15594 @item @code{-Ea}
15595
15596 Store tracebacks in exception occurrences when the target supports it.
15597 The “a” is for “address”; tracebacks will contain hexadecimal addresses,
15598 unless symbolic tracebacks are enabled.
15599
15600 See also the packages @code{GNAT.Traceback} and
15601 @code{GNAT.Traceback.Symbolic} for more information.
15602 Note that on x86 ports, you must not use @code{-fomit-frame-pointer}
15603 @code{gcc} option.
15604 @end table
15605
15606 @geindex -Es (gnatbind)
15607
15608
15609 @table @asis
15610
15611 @item @code{-Es}
15612
15613 Store tracebacks in exception occurrences when the target supports it.
15614 The “s” is for “symbolic”; symbolic tracebacks are enabled.
15615 @end table
15616
15617 @geindex -E (gnatbind)
15618
15619
15620 @table @asis
15621
15622 @item @code{-E}
15623
15624 Currently the same as @code{-Ea}.
15625 @end table
15626
15627 @geindex -f (gnatbind)
15628
15629
15630 @table @asis
15631
15632 @item @code{-f@emph{elab-order}}
15633
15634 Force elaboration order. For further details see @ref{111,,Elaboration Control}
15635 and @ref{f,,Elaboration Order Handling in GNAT}.
15636 @end table
15637
15638 @geindex -F (gnatbind)
15639
15640
15641 @table @asis
15642
15643 @item @code{-F}
15644
15645 Force the checks of elaboration flags. @code{gnatbind} does not normally
15646 generate checks of elaboration flags for the main executable, except when
15647 a Stand-Alone Library is used. However, there are cases when this cannot be
15648 detected by gnatbind. An example is importing an interface of a Stand-Alone
15649 Library through a pragma Import and only specifying through a linker switch
15650 this Stand-Alone Library. This switch is used to guarantee that elaboration
15651 flag checks are generated.
15652 @end table
15653
15654 @geindex -h (gnatbind)
15655
15656
15657 @table @asis
15658
15659 @item @code{-h}
15660
15661 Output usage (help) information.
15662 @end table
15663
15664 @geindex -H (gnatbind)
15665
15666
15667 @table @asis
15668
15669 @item @code{-H}
15670
15671 Legacy elaboration order model enabled. For further details see
15672 @ref{f,,Elaboration Order Handling in GNAT}.
15673 @end table
15674
15675 @geindex -H32 (gnatbind)
15676
15677
15678 @table @asis
15679
15680 @item @code{-H32}
15681
15682 Use 32-bit allocations for @code{__gnat_malloc} (and thus for access types).
15683 For further details see @ref{112,,Dynamic Allocation Control}.
15684 @end table
15685
15686 @geindex -H64 (gnatbind)
15687
15688 @geindex __gnat_malloc
15689
15690
15691 @table @asis
15692
15693 @item @code{-H64}
15694
15695 Use 64-bit allocations for @code{__gnat_malloc} (and thus for access types).
15696 For further details see @ref{112,,Dynamic Allocation Control}.
15697
15698 @geindex -I (gnatbind)
15699
15700 @item @code{-I}
15701
15702 Specify directory to be searched for source and ALI files.
15703
15704 @geindex -I- (gnatbind)
15705
15706 @item @code{-I-}
15707
15708 Do not look for sources in the current directory where @code{gnatbind} was
15709 invoked, and do not look for ALI files in the directory containing the
15710 ALI file named in the @code{gnatbind} command line.
15711
15712 @geindex -l (gnatbind)
15713
15714 @item @code{-l}
15715
15716 Output chosen elaboration order.
15717
15718 @geindex -L (gnatbind)
15719
15720 @item @code{-L@emph{xxx}}
15721
15722 Bind the units for library building. In this case the @code{adainit} and
15723 @code{adafinal} procedures (@ref{a0,,Binding with Non-Ada Main Programs})
15724 are renamed to @code{@emph{xxx}init} and
15725 @code{@emph{xxx}final}.
15726 Implies -n.
15727 (@ref{2a,,GNAT and Libraries}, for more details.)
15728
15729 @geindex -M (gnatbind)
15730
15731 @item @code{-M@emph{xyz}}
15732
15733 Rename generated main program from main to xyz. This option is
15734 supported on cross environments only.
15735
15736 @geindex -m (gnatbind)
15737
15738 @item @code{-m@emph{n}}
15739
15740 Limit number of detected errors or warnings to @code{n}, where @code{n} is
15741 in the range 1..999999. The default value if no switch is
15742 given is 9999. If the number of warnings reaches this limit, then a
15743 message is output and further warnings are suppressed, the bind
15744 continues in this case. If the number of errors reaches this
15745 limit, then a message is output and the bind is abandoned.
15746 A value of zero means that no limit is enforced. The equal
15747 sign is optional.
15748
15749 @geindex -minimal (gnatbind)
15750
15751 @item @code{-minimal}
15752
15753 Generate a binder file suitable for space-constrained applications. When
15754 active, binder-generated objects not required for program operation are no
15755 longer generated. @strong{Warning:} this option comes with the following
15756 limitations:
15757
15758
15759 @itemize *
15760
15761 @item
15762 Starting the program’s execution in the debugger will cause it to
15763 stop at the start of the @code{main} function instead of the main subprogram.
15764 This can be worked around by manually inserting a breakpoint on that
15765 subprogram and resuming the program’s execution until reaching that breakpoint.
15766
15767 @item
15768 Programs using GNAT.Compiler_Version will not link.
15769 @end itemize
15770
15771 @geindex -n (gnatbind)
15772
15773 @item @code{-n}
15774
15775 No main program.
15776
15777 @geindex -nostdinc (gnatbind)
15778
15779 @item @code{-nostdinc}
15780
15781 Do not look for sources in the system default directory.
15782
15783 @geindex -nostdlib (gnatbind)
15784
15785 @item @code{-nostdlib}
15786
15787 Do not look for library files in the system default directory.
15788
15789 @geindex --RTS (gnatbind)
15790
15791 @item @code{--RTS=@emph{rts-path}}
15792
15793 Specifies the default location of the run-time library. Same meaning as the
15794 equivalent @code{gnatmake} flag (@ref{ce,,Switches for gnatmake}).
15795
15796 @geindex -o (gnatbind)
15797
15798 @item @code{-o @emph{file}}
15799
15800 Name the output file @code{file} (default is @code{b~`xxx}.adb`).
15801 Note that if this option is used, then linking must be done manually,
15802 gnatlink cannot be used.
15803
15804 @geindex -O (gnatbind)
15805
15806 @item @code{-O[=@emph{filename}]}
15807
15808 Output object list (to standard output or to the named file).
15809
15810 @geindex -p (gnatbind)
15811
15812 @item @code{-p}
15813
15814 Pessimistic (worst-case) elaboration order.
15815
15816 @geindex -P (gnatbind)
15817
15818 @item @code{-P}
15819
15820 Generate binder file suitable for CodePeer.
15821
15822 @geindex -R (gnatbind)
15823
15824 @item @code{-R}
15825
15826 Output closure source list, which includes all non-run-time units that are
15827 included in the bind.
15828
15829 @geindex -Ra (gnatbind)
15830
15831 @item @code{-Ra}
15832
15833 Like @code{-R} but the list includes run-time units.
15834
15835 @geindex -s (gnatbind)
15836
15837 @item @code{-s}
15838
15839 Require all source files to be present.
15840
15841 @geindex -S (gnatbind)
15842
15843 @item @code{-S@emph{xxx}}
15844
15845 Specifies the value to be used when detecting uninitialized scalar
15846 objects with pragma Initialize_Scalars.
15847 The @code{xxx} string specified with the switch is one of:
15848
15849
15850 @itemize *
15851
15852 @item
15853 @code{in} for an invalid value.
15854
15855 If zero is invalid for the discrete type in question,
15856 then the scalar value is set to all zero bits.
15857 For signed discrete types, the largest possible negative value of
15858 the underlying scalar is set (i.e. a one bit followed by all zero bits).
15859 For unsigned discrete types, the underlying scalar value is set to all
15860 one bits. For floating-point types, a NaN value is set
15861 (see body of package System.Scalar_Values for exact values).
15862
15863 @item
15864 @code{lo} for low value.
15865
15866 If zero is invalid for the discrete type in question,
15867 then the scalar value is set to all zero bits.
15868 For signed discrete types, the largest possible negative value of
15869 the underlying scalar is set (i.e. a one bit followed by all zero bits).
15870 For unsigned discrete types, the underlying scalar value is set to all
15871 zero bits. For floating-point, a small value is set
15872 (see body of package System.Scalar_Values for exact values).
15873
15874 @item
15875 @code{hi} for high value.
15876
15877 If zero is invalid for the discrete type in question,
15878 then the scalar value is set to all one bits.
15879 For signed discrete types, the largest possible positive value of
15880 the underlying scalar is set (i.e. a zero bit followed by all one bits).
15881 For unsigned discrete types, the underlying scalar value is set to all
15882 one bits. For floating-point, a large value is set
15883 (see body of package System.Scalar_Values for exact values).
15884
15885 @item
15886 @code{xx} for hex value (two hex digits).
15887
15888 The underlying scalar is set to a value consisting of repeated bytes, whose
15889 value corresponds to the given value. For example if @code{BF} is given,
15890 then a 32-bit scalar value will be set to the bit patterm @code{16#BFBFBFBF#}.
15891 @end itemize
15892
15893 @geindex GNAT_INIT_SCALARS
15894
15895 In addition, you can specify @code{-Sev} to indicate that the value is
15896 to be set at run time. In this case, the program will look for an environment
15897 variable of the form @code{GNAT_INIT_SCALARS=@emph{yy}}, where @code{yy} is one
15898 of @code{in/lo/hi/@emph{xx}} with the same meanings as above.
15899 If no environment variable is found, or if it does not have a valid value,
15900 then the default is @code{in} (invalid values).
15901 @end table
15902
15903 @geindex -static (gnatbind)
15904
15905
15906 @table @asis
15907
15908 @item @code{-static}
15909
15910 Link against a static GNAT run-time.
15911
15912 @geindex -shared (gnatbind)
15913
15914 @item @code{-shared}
15915
15916 Link against a shared GNAT run-time when available.
15917
15918 @geindex -t (gnatbind)
15919
15920 @item @code{-t}
15921
15922 Tolerate time stamp and other consistency errors.
15923
15924 @geindex -T (gnatbind)
15925
15926 @item @code{-T@emph{n}}
15927
15928 Set the time slice value to @code{n} milliseconds. If the system supports
15929 the specification of a specific time slice value, then the indicated value
15930 is used. If the system does not support specific time slice values, but
15931 does support some general notion of round-robin scheduling, then any
15932 nonzero value will activate round-robin scheduling.
15933
15934 A value of zero is treated specially. It turns off time
15935 slicing, and in addition, indicates to the tasking run-time that the
15936 semantics should match as closely as possible the Annex D
15937 requirements of the Ada RM, and in particular sets the default
15938 scheduling policy to @code{FIFO_Within_Priorities}.
15939
15940 @geindex -u (gnatbind)
15941
15942 @item @code{-u@emph{n}}
15943
15944 Enable dynamic stack usage, with @code{n} results stored and displayed
15945 at program termination. A result is generated when a task
15946 terminates. Results that can’t be stored are displayed on the fly, at
15947 task termination. This option is currently not supported on Itanium
15948 platforms. (See @ref{113,,Dynamic Stack Usage Analysis} for details.)
15949
15950 @geindex -v (gnatbind)
15951
15952 @item @code{-v}
15953
15954 Verbose mode. Write error messages, header, summary output to
15955 @code{stdout}.
15956
15957 @geindex -V (gnatbind)
15958
15959 @item @code{-V@emph{key}=@emph{value}}
15960
15961 Store the given association of @code{key} to @code{value} in the bind environment.
15962 Values stored this way can be retrieved at run time using
15963 @code{GNAT.Bind_Environment}.
15964
15965 @geindex -w (gnatbind)
15966
15967 @item @code{-w@emph{x}}
15968
15969 Warning mode; @code{x} = s/e for suppress/treat as error.
15970
15971 @geindex -Wx (gnatbind)
15972
15973 @item @code{-Wx@emph{e}}
15974
15975 Override default wide character encoding for standard Text_IO files.
15976
15977 @geindex -x (gnatbind)
15978
15979 @item @code{-x}
15980
15981 Exclude source files (check object consistency only).
15982
15983 @geindex -xdr (gnatbind)
15984
15985 @item @code{-xdr}
15986
15987 Use the target-independent XDR protocol for stream oriented attributes
15988 instead of the default implementation which is based on direct binary
15989 representations and is therefore target-and endianness-dependent.
15990 However it does not support 128-bit integer types and the exception
15991 @code{Ada.IO_Exceptions.Device_Error} is raised if any attempt is made
15992 at streaming 128-bit integer types with it.
15993
15994 @geindex -Xnnn (gnatbind)
15995
15996 @item @code{-X@emph{nnn}}
15997
15998 Set default exit status value, normally 0 for POSIX compliance.
15999
16000 @geindex -y (gnatbind)
16001
16002 @item @code{-y}
16003
16004 Enable leap seconds support in @code{Ada.Calendar} and its children.
16005
16006 @geindex -z (gnatbind)
16007
16008 @item @code{-z}
16009
16010 No main subprogram.
16011 @end table
16012
16013 You may obtain this listing of switches by running @code{gnatbind} with
16014 no arguments.
16015
16016 @menu
16017 * Consistency-Checking Modes::
16018 * Binder Error Message Control::
16019 * Elaboration Control::
16020 * Output Control::
16021 * Dynamic Allocation Control::
16022 * Binding with Non-Ada Main Programs::
16023 * Binding Programs with No Main Subprogram::
16024
16025 @end menu
16026
16027 @node Consistency-Checking Modes,Binder Error Message Control,,Switches for gnatbind
16028 @anchor{gnat_ugn/building_executable_programs_with_gnat consistency-checking-modes}@anchor{114}@anchor{gnat_ugn/building_executable_programs_with_gnat id35}@anchor{115}
16029 @subsubsection Consistency-Checking Modes
16030
16031
16032 As described earlier, by default @code{gnatbind} checks
16033 that object files are consistent with one another and are consistent
16034 with any source files it can locate. The following switches control binder
16035 access to sources.
16036
16037 @quotation
16038
16039 @geindex -s (gnatbind)
16040 @end quotation
16041
16042
16043 @table @asis
16044
16045 @item @code{-s}
16046
16047 Require source files to be present. In this mode, the binder must be
16048 able to locate all source files that are referenced, in order to check
16049 their consistency. In normal mode, if a source file cannot be located it
16050 is simply ignored. If you specify this switch, a missing source
16051 file is an error.
16052
16053 @geindex -Wx (gnatbind)
16054
16055 @item @code{-Wx@emph{e}}
16056
16057 Override default wide character encoding for standard Text_IO files.
16058 Normally the default wide character encoding method used for standard
16059 [Wide_[Wide_]]Text_IO files is taken from the encoding specified for
16060 the main source input (see description of switch
16061 @code{-gnatWx} for the compiler). The
16062 use of this switch for the binder (which has the same set of
16063 possible arguments) overrides this default as specified.
16064
16065 @geindex -x (gnatbind)
16066
16067 @item @code{-x}
16068
16069 Exclude source files. In this mode, the binder only checks that ALI
16070 files are consistent with one another. Source files are not accessed.
16071 The binder runs faster in this mode, and there is still a guarantee that
16072 the resulting program is self-consistent.
16073 If a source file has been edited since it was last compiled, and you
16074 specify this switch, the binder will not detect that the object
16075 file is out of date with respect to the source file. Note that this is the
16076 mode that is automatically used by @code{gnatmake} because in this
16077 case the checking against sources has already been performed by
16078 @code{gnatmake} in the course of compilation (i.e., before binding).
16079 @end table
16080
16081 @node Binder Error Message Control,Elaboration Control,Consistency-Checking Modes,Switches for gnatbind
16082 @anchor{gnat_ugn/building_executable_programs_with_gnat binder-error-message-control}@anchor{116}@anchor{gnat_ugn/building_executable_programs_with_gnat id36}@anchor{117}
16083 @subsubsection Binder Error Message Control
16084
16085
16086 The following switches provide control over the generation of error
16087 messages from the binder:
16088
16089 @quotation
16090
16091 @geindex -v (gnatbind)
16092 @end quotation
16093
16094
16095 @table @asis
16096
16097 @item @code{-v}
16098
16099 Verbose mode. In the normal mode, brief error messages are generated to
16100 @code{stderr}. If this switch is present, a header is written
16101 to @code{stdout} and any error messages are directed to @code{stdout}.
16102 All that is written to @code{stderr} is a brief summary message.
16103
16104 @geindex -b (gnatbind)
16105
16106 @item @code{-b}
16107
16108 Generate brief error messages to @code{stderr} even if verbose mode is
16109 specified. This is relevant only when used with the
16110 @code{-v} switch.
16111
16112 @geindex -m (gnatbind)
16113
16114 @item @code{-m@emph{n}}
16115
16116 Limits the number of error messages to @code{n}, a decimal integer in the
16117 range 1-999. The binder terminates immediately if this limit is reached.
16118
16119 @geindex -M (gnatbind)
16120
16121 @item @code{-M@emph{xxx}}
16122
16123 Renames the generated main program from @code{main} to @code{xxx}.
16124 This is useful in the case of some cross-building environments, where
16125 the actual main program is separate from the one generated
16126 by @code{gnatbind}.
16127
16128 @geindex -ws (gnatbind)
16129
16130 @geindex Warnings
16131
16132 @item @code{-ws}
16133
16134 Suppress all warning messages.
16135
16136 @geindex -we (gnatbind)
16137
16138 @item @code{-we}
16139
16140 Treat any warning messages as fatal errors.
16141
16142 @geindex -t (gnatbind)
16143
16144 @geindex Time stamp checks
16145 @geindex in binder
16146
16147 @geindex Binder consistency checks
16148
16149 @geindex Consistency checks
16150 @geindex in binder
16151
16152 @item @code{-t}
16153
16154 The binder performs a number of consistency checks including:
16155
16156
16157 @itemize *
16158
16159 @item
16160 Check that time stamps of a given source unit are consistent
16161
16162 @item
16163 Check that checksums of a given source unit are consistent
16164
16165 @item
16166 Check that consistent versions of @code{GNAT} were used for compilation
16167
16168 @item
16169 Check consistency of configuration pragmas as required
16170 @end itemize
16171
16172 Normally failure of such checks, in accordance with the consistency
16173 requirements of the Ada Reference Manual, causes error messages to be
16174 generated which abort the binder and prevent the output of a binder
16175 file and subsequent link to obtain an executable.
16176
16177 The @code{-t} switch converts these error messages
16178 into warnings, so that
16179 binding and linking can continue to completion even in the presence of such
16180 errors. The result may be a failed link (due to missing symbols), or a
16181 non-functional executable which has undefined semantics.
16182
16183 @cartouche
16184 @quotation Note
16185 This means that @code{-t} should be used only in unusual situations,
16186 with extreme care.
16187 @end quotation
16188 @end cartouche
16189 @end table
16190
16191 @node Elaboration Control,Output Control,Binder Error Message Control,Switches for gnatbind
16192 @anchor{gnat_ugn/building_executable_programs_with_gnat elaboration-control}@anchor{111}@anchor{gnat_ugn/building_executable_programs_with_gnat id37}@anchor{118}
16193 @subsubsection Elaboration Control
16194
16195
16196 The following switches provide additional control over the elaboration
16197 order. For further details see @ref{f,,Elaboration Order Handling in GNAT}.
16198
16199 @geindex -f (gnatbind)
16200
16201
16202 @table @asis
16203
16204 @item @code{-f@emph{elab-order}}
16205
16206 Force elaboration order.
16207
16208 @code{elab-order} should be the name of a “forced elaboration order file”, that
16209 is, a text file containing library item names, one per line. A name of the
16210 form “some.unit%s” or “some.unit (spec)” denotes the spec of Some.Unit. A
16211 name of the form “some.unit%b” or “some.unit (body)” denotes the body of
16212 Some.Unit. Each pair of lines is taken to mean that there is an elaboration
16213 dependence of the second line on the first. For example, if the file
16214 contains:
16215
16216 @example
16217 this (spec)
16218 this (body)
16219 that (spec)
16220 that (body)
16221 @end example
16222
16223 then the spec of This will be elaborated before the body of This, and the
16224 body of This will be elaborated before the spec of That, and the spec of That
16225 will be elaborated before the body of That. The first and last of these three
16226 dependences are already required by Ada rules, so this file is really just
16227 forcing the body of This to be elaborated before the spec of That.
16228
16229 The given order must be consistent with Ada rules, or else @code{gnatbind} will
16230 give elaboration cycle errors. For example, if you say x (body) should be
16231 elaborated before x (spec), there will be a cycle, because Ada rules require
16232 x (spec) to be elaborated before x (body); you can’t have the spec and body
16233 both elaborated before each other.
16234
16235 If you later add “with That;” to the body of This, there will be a cycle, in
16236 which case you should erase either “this (body)” or “that (spec)” from the
16237 above forced elaboration order file.
16238
16239 Blank lines and Ada-style comments are ignored. Unit names that do not exist
16240 in the program are ignored. Units in the GNAT predefined library are also
16241 ignored.
16242 @end table
16243
16244 @geindex -p (gnatbind)
16245
16246
16247 @table @asis
16248
16249 @item @code{-p}
16250
16251 Pessimistic elaboration order
16252
16253 This switch is only applicable to the pre-20.x legacy elaboration models.
16254 The post-20.x elaboration model uses a more informed approach of ordering
16255 the units.
16256
16257 Normally the binder attempts to choose an elaboration order that is likely to
16258 minimize the likelihood of an elaboration order error resulting in raising a
16259 @code{Program_Error} exception. This switch reverses the action of the binder,
16260 and requests that it deliberately choose an order that is likely to maximize
16261 the likelihood of an elaboration error. This is useful in ensuring
16262 portability and avoiding dependence on accidental fortuitous elaboration
16263 ordering.
16264
16265 Normally it only makes sense to use the @code{-p} switch if dynamic
16266 elaboration checking is used (@code{-gnatE} switch used for compilation).
16267 This is because in the default static elaboration mode, all necessary
16268 @code{Elaborate} and @code{Elaborate_All} pragmas are implicitly inserted.
16269 These implicit pragmas are still respected by the binder in @code{-p}
16270 mode, so a safe elaboration order is assured.
16271
16272 Note that @code{-p} is not intended for production use; it is more for
16273 debugging/experimental use.
16274 @end table
16275
16276 @node Output Control,Dynamic Allocation Control,Elaboration Control,Switches for gnatbind
16277 @anchor{gnat_ugn/building_executable_programs_with_gnat id38}@anchor{119}@anchor{gnat_ugn/building_executable_programs_with_gnat output-control}@anchor{11a}
16278 @subsubsection Output Control
16279
16280
16281 The following switches allow additional control over the output
16282 generated by the binder.
16283
16284 @quotation
16285
16286 @geindex -c (gnatbind)
16287 @end quotation
16288
16289
16290 @table @asis
16291
16292 @item @code{-c}
16293
16294 Check only. Do not generate the binder output file. In this mode the
16295 binder performs all error checks but does not generate an output file.
16296
16297 @geindex -e (gnatbind)
16298
16299 @item @code{-e}
16300
16301 Output complete list of elaboration-order dependencies, showing the
16302 reason for each dependency. This output can be rather extensive but may
16303 be useful in diagnosing problems with elaboration order. The output is
16304 written to @code{stdout}.
16305
16306 @geindex -h (gnatbind)
16307
16308 @item @code{-h}
16309
16310 Output usage information. The output is written to @code{stdout}.
16311
16312 @geindex -K (gnatbind)
16313
16314 @item @code{-K}
16315
16316 Output linker options to @code{stdout}. Includes library search paths,
16317 contents of pragmas Ident and Linker_Options, and libraries added
16318 by @code{gnatbind}.
16319
16320 @geindex -l (gnatbind)
16321
16322 @item @code{-l}
16323
16324 Output chosen elaboration order. The output is written to @code{stdout}.
16325
16326 @geindex -O (gnatbind)
16327
16328 @item @code{-O}
16329
16330 Output full names of all the object files that must be linked to provide
16331 the Ada component of the program. The output is written to @code{stdout}.
16332 This list includes the files explicitly supplied and referenced by the user
16333 as well as implicitly referenced run-time unit files. The latter are
16334 omitted if the corresponding units reside in shared libraries. The
16335 directory names for the run-time units depend on the system configuration.
16336
16337 @geindex -o (gnatbind)
16338
16339 @item @code{-o @emph{file}}
16340
16341 Set name of output file to @code{file} instead of the normal
16342 @code{b~`mainprog}.adb` default. Note that @code{file} denote the Ada
16343 binder generated body filename.
16344 Note that if this option is used, then linking must be done manually.
16345 It is not possible to use gnatlink in this case, since it cannot locate
16346 the binder file.
16347
16348 @geindex -r (gnatbind)
16349
16350 @item @code{-r}
16351
16352 Generate list of @code{pragma Restrictions} that could be applied to
16353 the current unit. This is useful for code audit purposes, and also may
16354 be used to improve code generation in some cases.
16355 @end table
16356
16357 @node Dynamic Allocation Control,Binding with Non-Ada Main Programs,Output Control,Switches for gnatbind
16358 @anchor{gnat_ugn/building_executable_programs_with_gnat dynamic-allocation-control}@anchor{112}@anchor{gnat_ugn/building_executable_programs_with_gnat id39}@anchor{11b}
16359 @subsubsection Dynamic Allocation Control
16360
16361
16362 The heap control switches – @code{-H32} and @code{-H64} –
16363 determine whether dynamic allocation uses 32-bit or 64-bit memory.
16364 They only affect compiler-generated allocations via @code{__gnat_malloc};
16365 explicit calls to @code{malloc} and related functions from the C
16366 run-time library are unaffected.
16367
16368
16369 @table @asis
16370
16371 @item @code{-H32}
16372
16373 Allocate memory on 32-bit heap
16374
16375 @item @code{-H64}
16376
16377 Allocate memory on 64-bit heap. This is the default
16378 unless explicitly overridden by a @code{'Size} clause on the access type.
16379 @end table
16380
16381 These switches are only effective on VMS platforms.
16382
16383 @node Binding with Non-Ada Main Programs,Binding Programs with No Main Subprogram,Dynamic Allocation Control,Switches for gnatbind
16384 @anchor{gnat_ugn/building_executable_programs_with_gnat binding-with-non-ada-main-programs}@anchor{a0}@anchor{gnat_ugn/building_executable_programs_with_gnat id40}@anchor{11c}
16385 @subsubsection Binding with Non-Ada Main Programs
16386
16387
16388 The description so far has assumed that the main
16389 program is in Ada, and that the task of the binder is to generate a
16390 corresponding function @code{main} that invokes this Ada main
16391 program. GNAT also supports the building of executable programs where
16392 the main program is not in Ada, but some of the called routines are
16393 written in Ada and compiled using GNAT (@ref{2c,,Mixed Language Programming}).
16394 The following switch is used in this situation:
16395
16396 @quotation
16397
16398 @geindex -n (gnatbind)
16399 @end quotation
16400
16401
16402 @table @asis
16403
16404 @item @code{-n}
16405
16406 No main program. The main program is not in Ada.
16407 @end table
16408
16409 In this case, most of the functions of the binder are still required,
16410 but instead of generating a main program, the binder generates a file
16411 containing the following callable routines:
16412
16413 @quotation
16414
16415 @geindex adainit
16416
16417
16418 @table @asis
16419
16420 @item @code{adainit}
16421
16422 You must call this routine to initialize the Ada part of the program by
16423 calling the necessary elaboration routines. A call to @code{adainit} is
16424 required before the first call to an Ada subprogram.
16425
16426 Note that it is assumed that the basic execution environment must be setup
16427 to be appropriate for Ada execution at the point where the first Ada
16428 subprogram is called. In particular, if the Ada code will do any
16429 floating-point operations, then the FPU must be setup in an appropriate
16430 manner. For the case of the x86, for example, full precision mode is
16431 required. The procedure GNAT.Float_Control.Reset may be used to ensure
16432 that the FPU is in the right state.
16433 @end table
16434
16435 @geindex adafinal
16436
16437
16438 @table @asis
16439
16440 @item @code{adafinal}
16441
16442 You must call this routine to perform any library-level finalization
16443 required by the Ada subprograms. A call to @code{adafinal} is required
16444 after the last call to an Ada subprogram, and before the program
16445 terminates.
16446 @end table
16447 @end quotation
16448
16449 @geindex -n (gnatbind)
16450
16451 @geindex Binder
16452 @geindex multiple input files
16453
16454 If the @code{-n} switch
16455 is given, more than one ALI file may appear on
16456 the command line for @code{gnatbind}. The normal @code{closure}
16457 calculation is performed for each of the specified units. Calculating
16458 the closure means finding out the set of units involved by tracing
16459 @emph{with} references. The reason it is necessary to be able to
16460 specify more than one ALI file is that a given program may invoke two or
16461 more quite separate groups of Ada units.
16462
16463 The binder takes the name of its output file from the last specified ALI
16464 file, unless overridden by the use of the @code{-o file}.
16465
16466 @geindex -o (gnatbind)
16467
16468 The output is an Ada unit in source form that can be compiled with GNAT.
16469 This compilation occurs automatically as part of the @code{gnatlink}
16470 processing.
16471
16472 Currently the GNAT run-time requires a FPU using 80 bits mode
16473 precision. Under targets where this is not the default it is required to
16474 call GNAT.Float_Control.Reset before using floating point numbers (this
16475 include float computation, float input and output) in the Ada code. A
16476 side effect is that this could be the wrong mode for the foreign code
16477 where floating point computation could be broken after this call.
16478
16479 @node Binding Programs with No Main Subprogram,,Binding with Non-Ada Main Programs,Switches for gnatbind
16480 @anchor{gnat_ugn/building_executable_programs_with_gnat binding-programs-with-no-main-subprogram}@anchor{11d}@anchor{gnat_ugn/building_executable_programs_with_gnat id41}@anchor{11e}
16481 @subsubsection Binding Programs with No Main Subprogram
16482
16483
16484 It is possible to have an Ada program which does not have a main
16485 subprogram. This program will call the elaboration routines of all the
16486 packages, then the finalization routines.
16487
16488 The following switch is used to bind programs organized in this manner:
16489
16490 @quotation
16491
16492 @geindex -z (gnatbind)
16493 @end quotation
16494
16495
16496 @table @asis
16497
16498 @item @code{-z}
16499
16500 Normally the binder checks that the unit name given on the command line
16501 corresponds to a suitable main subprogram. When this switch is used,
16502 a list of ALI files can be given, and the execution of the program
16503 consists of elaboration of these units in an appropriate order. Note
16504 that the default wide character encoding method for standard Text_IO
16505 files is always set to Brackets if this switch is set (you can use
16506 the binder switch
16507 @code{-Wx} to override this default).
16508 @end table
16509
16510 @node Command-Line Access,Search Paths for gnatbind,Switches for gnatbind,Binding with gnatbind
16511 @anchor{gnat_ugn/building_executable_programs_with_gnat command-line-access}@anchor{11f}@anchor{gnat_ugn/building_executable_programs_with_gnat id42}@anchor{120}
16512 @subsection Command-Line Access
16513
16514
16515 The package @code{Ada.Command_Line} provides access to the command-line
16516 arguments and program name. In order for this interface to operate
16517 correctly, the two variables
16518
16519 @example
16520 int gnat_argc;
16521 char **gnat_argv;
16522 @end example
16523
16524 @geindex gnat_argv
16525
16526 @geindex gnat_argc
16527
16528 are declared in one of the GNAT library routines. These variables must
16529 be set from the actual @code{argc} and @code{argv} values passed to the
16530 main program. With no @emph{n} present, @code{gnatbind}
16531 generates the C main program to automatically set these variables.
16532 If the @emph{n} switch is used, there is no automatic way to
16533 set these variables. If they are not set, the procedures in
16534 @code{Ada.Command_Line} will not be available, and any attempt to use
16535 them will raise @code{Constraint_Error}. If command line access is
16536 required, your main program must set @code{gnat_argc} and
16537 @code{gnat_argv} from the @code{argc} and @code{argv} values passed to
16538 it.
16539
16540 @node Search Paths for gnatbind,Examples of gnatbind Usage,Command-Line Access,Binding with gnatbind
16541 @anchor{gnat_ugn/building_executable_programs_with_gnat id43}@anchor{121}@anchor{gnat_ugn/building_executable_programs_with_gnat search-paths-for-gnatbind}@anchor{76}
16542 @subsection Search Paths for @code{gnatbind}
16543
16544
16545 The binder takes the name of an ALI file as its argument and needs to
16546 locate source files as well as other ALI files to verify object consistency.
16547
16548 For source files, it follows exactly the same search rules as @code{gcc}
16549 (see @ref{73,,Search Paths and the Run-Time Library (RTL)}). For ALI files the
16550 directories searched are:
16551
16552
16553 @itemize *
16554
16555 @item
16556 The directory containing the ALI file named in the command line, unless
16557 the switch @code{-I-} is specified.
16558
16559 @item
16560 All directories specified by @code{-I}
16561 switches on the @code{gnatbind}
16562 command line, in the order given.
16563
16564 @geindex ADA_PRJ_OBJECTS_FILE
16565
16566 @item
16567 Each of the directories listed in the text file whose name is given
16568 by the
16569 @geindex ADA_PRJ_OBJECTS_FILE
16570 @geindex environment variable; ADA_PRJ_OBJECTS_FILE
16571 @code{ADA_PRJ_OBJECTS_FILE} environment variable.
16572
16573 @geindex ADA_PRJ_OBJECTS_FILE
16574 @geindex environment variable; ADA_PRJ_OBJECTS_FILE
16575 @code{ADA_PRJ_OBJECTS_FILE} is normally set by gnatmake or by the gnat
16576 driver when project files are used. It should not normally be set
16577 by other means.
16578
16579 @geindex ADA_OBJECTS_PATH
16580
16581 @item
16582 Each of the directories listed in the value of the
16583 @geindex ADA_OBJECTS_PATH
16584 @geindex environment variable; ADA_OBJECTS_PATH
16585 @code{ADA_OBJECTS_PATH} environment variable.
16586 Construct this value
16587 exactly as the
16588 @geindex PATH
16589 @geindex environment variable; PATH
16590 @code{PATH} environment variable: a list of directory
16591 names separated by colons (semicolons when working with the NT version
16592 of GNAT).
16593
16594 @item
16595 The content of the @code{ada_object_path} file which is part of the GNAT
16596 installation tree and is used to store standard libraries such as the
16597 GNAT Run-Time Library (RTL) unless the switch @code{-nostdlib} is
16598 specified. See @ref{72,,Installing a library}
16599 @end itemize
16600
16601 @geindex -I (gnatbind)
16602
16603 @geindex -aI (gnatbind)
16604
16605 @geindex -aO (gnatbind)
16606
16607 In the binder the switch @code{-I}
16608 is used to specify both source and
16609 library file paths. Use @code{-aI}
16610 instead if you want to specify
16611 source paths only, and @code{-aO}
16612 if you want to specify library paths
16613 only. This means that for the binder
16614 @code{-I@emph{dir}} is equivalent to
16615 @code{-aI@emph{dir}}
16616 @code{-aO`@emph{dir}}.
16617 The binder generates the bind file (a C language source file) in the
16618 current working directory.
16619
16620 @geindex Ada
16621
16622 @geindex System
16623
16624 @geindex Interfaces
16625
16626 @geindex GNAT
16627
16628 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
16629 children make up the GNAT Run-Time Library, together with the package
16630 GNAT and its children, which contain a set of useful additional
16631 library functions provided by GNAT. The sources for these units are
16632 needed by the compiler and are kept together in one directory. The ALI
16633 files and object files generated by compiling the RTL are needed by the
16634 binder and the linker and are kept together in one directory, typically
16635 different from the directory containing the sources. In a normal
16636 installation, you need not specify these directory names when compiling
16637 or binding. Either the environment variables or the built-in defaults
16638 cause these files to be found.
16639
16640 Besides simplifying access to the RTL, a major use of search paths is
16641 in compiling sources from multiple directories. This can make
16642 development environments much more flexible.
16643
16644 @node Examples of gnatbind Usage,,Search Paths for gnatbind,Binding with gnatbind
16645 @anchor{gnat_ugn/building_executable_programs_with_gnat examples-of-gnatbind-usage}@anchor{122}@anchor{gnat_ugn/building_executable_programs_with_gnat id44}@anchor{123}
16646 @subsection Examples of @code{gnatbind} Usage
16647
16648
16649 Here are some examples of @code{gnatbind} invovations:
16650
16651 @quotation
16652
16653 @example
16654 gnatbind hello
16655 @end example
16656
16657 The main program @code{Hello} (source program in @code{hello.adb}) is
16658 bound using the standard switch settings. The generated main program is
16659 @code{b~hello.adb}. This is the normal, default use of the binder.
16660
16661 @example
16662 gnatbind hello -o mainprog.adb
16663 @end example
16664
16665 The main program @code{Hello} (source program in @code{hello.adb}) is
16666 bound using the standard switch settings. The generated main program is
16667 @code{mainprog.adb} with the associated spec in
16668 @code{mainprog.ads}. Note that you must specify the body here not the
16669 spec. Note that if this option is used, then linking must be done manually,
16670 since gnatlink will not be able to find the generated file.
16671 @end quotation
16672
16673 @node Linking with gnatlink,Using the GNU make Utility,Binding with gnatbind,Building Executable Programs with GNAT
16674 @anchor{gnat_ugn/building_executable_programs_with_gnat id45}@anchor{124}@anchor{gnat_ugn/building_executable_programs_with_gnat linking-with-gnatlink}@anchor{c9}
16675 @section Linking with @code{gnatlink}
16676
16677
16678 @geindex gnatlink
16679
16680 This chapter discusses @code{gnatlink}, a tool that links
16681 an Ada program and builds an executable file. This utility
16682 invokes the system linker (via the @code{gcc} command)
16683 with a correct list of object files and library references.
16684 @code{gnatlink} automatically determines the list of files and
16685 references for the Ada part of a program. It uses the binder file
16686 generated by the @code{gnatbind} to determine this list.
16687
16688 @menu
16689 * Running gnatlink::
16690 * Switches for gnatlink::
16691
16692 @end menu
16693
16694 @node Running gnatlink,Switches for gnatlink,,Linking with gnatlink
16695 @anchor{gnat_ugn/building_executable_programs_with_gnat id46}@anchor{125}@anchor{gnat_ugn/building_executable_programs_with_gnat running-gnatlink}@anchor{126}
16696 @subsection Running @code{gnatlink}
16697
16698
16699 The form of the @code{gnatlink} command is
16700
16701 @example
16702 $ gnatlink [ switches ] mainprog [.ali]
16703 [ non-Ada objects ] [ linker options ]
16704 @end example
16705
16706 The arguments of @code{gnatlink} (switches, main @code{ALI} file,
16707 non-Ada objects
16708 or linker options) may be in any order, provided that no non-Ada object may
16709 be mistaken for a main @code{ALI} file.
16710 Any file name @code{F} without the @code{.ali}
16711 extension will be taken as the main @code{ALI} file if a file exists
16712 whose name is the concatenation of @code{F} and @code{.ali}.
16713
16714 @code{mainprog.ali} references the ALI file of the main program.
16715 The @code{.ali} extension of this file can be omitted. From this
16716 reference, @code{gnatlink} locates the corresponding binder file
16717 @code{b~mainprog.adb} and, using the information in this file along
16718 with the list of non-Ada objects and linker options, constructs a
16719 linker command file to create the executable.
16720
16721 The arguments other than the @code{gnatlink} switches and the main
16722 @code{ALI} file are passed to the linker uninterpreted.
16723 They typically include the names of
16724 object files for units written in other languages than Ada and any library
16725 references required to resolve references in any of these foreign language
16726 units, or in @code{Import} pragmas in any Ada units.
16727
16728 @code{linker options} is an optional list of linker specific
16729 switches.
16730 The default linker called by gnatlink is @code{gcc} which in
16731 turn calls the appropriate system linker.
16732
16733 One useful option for the linker is @code{-s}: it reduces the size of the
16734 executable by removing all symbol table and relocation information from the
16735 executable.
16736
16737 Standard options for the linker such as @code{-lmy_lib} or
16738 @code{-Ldir} can be added as is.
16739 For options that are not recognized by
16740 @code{gcc} as linker options, use the @code{gcc} switches
16741 @code{-Xlinker} or @code{-Wl,}.
16742
16743 Refer to the GCC documentation for
16744 details.
16745
16746 Here is an example showing how to generate a linker map:
16747
16748 @example
16749 $ gnatlink my_prog -Wl,-Map,MAPFILE
16750 @end example
16751
16752 Using @code{linker options} it is possible to set the program stack and
16753 heap size.
16754 See @ref{127,,Setting Stack Size from gnatlink} and
16755 @ref{128,,Setting Heap Size from gnatlink}.
16756
16757 @code{gnatlink} determines the list of objects required by the Ada
16758 program and prepends them to the list of objects passed to the linker.
16759 @code{gnatlink} also gathers any arguments set by the use of
16760 @code{pragma Linker_Options} and adds them to the list of arguments
16761 presented to the linker.
16762
16763 @node Switches for gnatlink,,Running gnatlink,Linking with gnatlink
16764 @anchor{gnat_ugn/building_executable_programs_with_gnat id47}@anchor{129}@anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gnatlink}@anchor{12a}
16765 @subsection Switches for @code{gnatlink}
16766
16767
16768 The following switches are available with the @code{gnatlink} utility:
16769
16770 @geindex --version (gnatlink)
16771
16772
16773 @table @asis
16774
16775 @item @code{--version}
16776
16777 Display Copyright and version, then exit disregarding all other options.
16778 @end table
16779
16780 @geindex --help (gnatlink)
16781
16782
16783 @table @asis
16784
16785 @item @code{--help}
16786
16787 If @code{--version} was not used, display usage, then exit disregarding
16788 all other options.
16789 @end table
16790
16791 @geindex Command line length
16792
16793 @geindex -f (gnatlink)
16794
16795
16796 @table @asis
16797
16798 @item @code{-f}
16799
16800 On some targets, the command line length is limited, and @code{gnatlink}
16801 will generate a separate file for the linker if the list of object files
16802 is too long.
16803 The @code{-f} switch forces this file
16804 to be generated even if
16805 the limit is not exceeded. This is useful in some cases to deal with
16806 special situations where the command line length is exceeded.
16807 @end table
16808
16809 @geindex Debugging information
16810 @geindex including
16811
16812 @geindex -g (gnatlink)
16813
16814
16815 @table @asis
16816
16817 @item @code{-g}
16818
16819 The option to include debugging information causes the Ada bind file (in
16820 other words, @code{b~mainprog.adb}) to be compiled with @code{-g}.
16821 In addition, the binder does not delete the @code{b~mainprog.adb},
16822 @code{b~mainprog.o} and @code{b~mainprog.ali} files.
16823 Without @code{-g}, the binder removes these files by default.
16824 @end table
16825
16826 @geindex -n (gnatlink)
16827
16828
16829 @table @asis
16830
16831 @item @code{-n}
16832
16833 Do not compile the file generated by the binder. This may be used when
16834 a link is rerun with different options, but there is no need to recompile
16835 the binder file.
16836 @end table
16837
16838 @geindex -v (gnatlink)
16839
16840
16841 @table @asis
16842
16843 @item @code{-v}
16844
16845 Verbose mode. Causes additional information to be output, including a full
16846 list of the included object files.
16847 This switch option is most useful when you want
16848 to see what set of object files are being used in the link step.
16849 @end table
16850
16851 @geindex -v -v (gnatlink)
16852
16853
16854 @table @asis
16855
16856 @item @code{-v -v}
16857
16858 Very verbose mode. Requests that the compiler operate in verbose mode when
16859 it compiles the binder file, and that the system linker run in verbose mode.
16860 @end table
16861
16862 @geindex -o (gnatlink)
16863
16864
16865 @table @asis
16866
16867 @item @code{-o @emph{exec-name}}
16868
16869 @code{exec-name} specifies an alternate name for the generated
16870 executable program. If this switch is omitted, the executable has the same
16871 name as the main unit. For example, @code{gnatlink try.ali} creates
16872 an executable called @code{try}.
16873 @end table
16874
16875 @geindex -B (gnatlink)
16876
16877
16878 @table @asis
16879
16880 @item @code{-B@emph{dir}}
16881
16882 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
16883 from @code{dir} instead of the default location. Only use this switch
16884 when multiple versions of the GNAT compiler are available.
16885 See the @code{Directory Options} section in @cite{The_GNU_Compiler_Collection}
16886 for further details. You would normally use the @code{-b} or
16887 @code{-V} switch instead.
16888 @end table
16889
16890 @geindex -M (gnatlink)
16891
16892
16893 @table @asis
16894
16895 @item @code{-M}
16896
16897 When linking an executable, create a map file. The name of the map file
16898 has the same name as the executable with extension “.map”.
16899 @end table
16900
16901 @geindex -M= (gnatlink)
16902
16903
16904 @table @asis
16905
16906 @item @code{-M=@emph{mapfile}}
16907
16908 When linking an executable, create a map file. The name of the map file is
16909 @code{mapfile}.
16910 @end table
16911
16912 @geindex --GCC=compiler_name (gnatlink)
16913
16914
16915 @table @asis
16916
16917 @item @code{--GCC=@emph{compiler_name}}
16918
16919 Program used for compiling the binder file. The default is
16920 @code{gcc}. You need to use quotes around @code{compiler_name} if
16921 @code{compiler_name} contains spaces or other separator characters.
16922 As an example @code{--GCC="foo -x -y"} will instruct @code{gnatlink} to
16923 use @code{foo -x -y} as your compiler. Note that switch @code{-c} is always
16924 inserted after your command name. Thus in the above example the compiler
16925 command that will be used by @code{gnatlink} will be @code{foo -c -x -y}.
16926 A limitation of this syntax is that the name and path name of the executable
16927 itself must not include any embedded spaces. If the compiler executable is
16928 different from the default one (gcc or <prefix>-gcc), then the back-end
16929 switches in the ALI file are not used to compile the binder generated source.
16930 For example, this is the case with @code{--GCC="foo -x -y"}. But the back end
16931 switches will be used for @code{--GCC="gcc -gnatv"}. If several
16932 @code{--GCC=compiler_name} are used, only the last @code{compiler_name}
16933 is taken into account. However, all the additional switches are also taken
16934 into account. Thus,
16935 @code{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
16936 @code{--GCC="bar -x -y -z -t"}.
16937 @end table
16938
16939 @geindex --LINK= (gnatlink)
16940
16941
16942 @table @asis
16943
16944 @item @code{--LINK=@emph{name}}
16945
16946 @code{name} is the name of the linker to be invoked. This is especially
16947 useful in mixed language programs since languages such as C++ require
16948 their own linker to be used. When this switch is omitted, the default
16949 name for the linker is @code{gcc}. When this switch is used, the
16950 specified linker is called instead of @code{gcc} with exactly the same
16951 parameters that would have been passed to @code{gcc} so if the desired
16952 linker requires different parameters it is necessary to use a wrapper
16953 script that massages the parameters before invoking the real linker. It
16954 may be useful to control the exact invocation by using the verbose
16955 switch.
16956 @end table
16957
16958 @node Using the GNU make Utility,,Linking with gnatlink,Building Executable Programs with GNAT
16959 @anchor{gnat_ugn/building_executable_programs_with_gnat id48}@anchor{12b}@anchor{gnat_ugn/building_executable_programs_with_gnat using-the-gnu-make-utility}@anchor{70}
16960 @section Using the GNU @code{make} Utility
16961
16962
16963 @geindex make (GNU)
16964 @geindex GNU make
16965
16966 This chapter offers some examples of makefiles that solve specific
16967 problems. It does not explain how to write a makefile, nor does it try to replace the
16968 @code{gnatmake} utility (@ref{c6,,Building with gnatmake}).
16969
16970 All the examples in this section are specific to the GNU version of
16971 make. Although @code{make} is a standard utility, and the basic language
16972 is the same, these examples use some advanced features found only in
16973 @code{GNU make}.
16974
16975 @menu
16976 * Using gnatmake in a Makefile::
16977 * Automatically Creating a List of Directories::
16978 * Generating the Command Line Switches::
16979 * Overcoming Command Line Length Limits::
16980
16981 @end menu
16982
16983 @node Using gnatmake in a Makefile,Automatically Creating a List of Directories,,Using the GNU make Utility
16984 @anchor{gnat_ugn/building_executable_programs_with_gnat id49}@anchor{12c}@anchor{gnat_ugn/building_executable_programs_with_gnat using-gnatmake-in-a-makefile}@anchor{12d}
16985 @subsection Using gnatmake in a Makefile
16986
16987
16988 @c index makefile (GNU make)
16989
16990 Complex project organizations can be handled in a very powerful way by
16991 using GNU make combined with gnatmake. For instance, here is a Makefile
16992 which allows you to build each subsystem of a big project into a separate
16993 shared library. Such a makefile allows you to significantly reduce the link
16994 time of very big applications while maintaining full coherence at
16995 each step of the build process.
16996
16997 The list of dependencies are handled automatically by
16998 @code{gnatmake}. The Makefile is simply used to call gnatmake in each of
16999 the appropriate directories.
17000
17001 Note that you should also read the example on how to automatically
17002 create the list of directories
17003 (@ref{12e,,Automatically Creating a List of Directories})
17004 which might help you in case your project has a lot of subdirectories.
17005
17006 @example
17007 ## This Makefile is intended to be used with the following directory
17008 ## configuration:
17009 ## - The sources are split into a series of csc (computer software components)
17010 ## Each of these csc is put in its own directory.
17011 ## Their name are referenced by the directory names.
17012 ## They will be compiled into shared library (although this would also work
17013 ## with static libraries
17014 ## - The main program (and possibly other packages that do not belong to any
17015 ## csc is put in the top level directory (where the Makefile is).
17016 ## toplevel_dir __ first_csc (sources) __ lib (will contain the library)
17017 ## \\_ second_csc (sources) __ lib (will contain the library)
17018 ## \\_ ...
17019 ## Although this Makefile is build for shared library, it is easy to modify
17020 ## to build partial link objects instead (modify the lines with -shared and
17021 ## gnatlink below)
17022 ##
17023 ## With this makefile, you can change any file in the system or add any new
17024 ## file, and everything will be recompiled correctly (only the relevant shared
17025 ## objects will be recompiled, and the main program will be re-linked).
17026
17027 # The list of computer software component for your project. This might be
17028 # generated automatically.
17029 CSC_LIST=aa bb cc
17030
17031 # Name of the main program (no extension)
17032 MAIN=main
17033
17034 # If we need to build objects with -fPIC, uncomment the following line
17035 #NEED_FPIC=-fPIC
17036
17037 # The following variable should give the directory containing libgnat.so
17038 # You can get this directory through 'gnatls -v'. This is usually the last
17039 # directory in the Object_Path.
17040 GLIB=...
17041
17042 # The directories for the libraries
17043 # (This macro expands the list of CSC to the list of shared libraries, you
17044 # could simply use the expanded form:
17045 # LIB_DIR=aa/lib/libaa.so bb/lib/libbb.so cc/lib/libcc.so
17046 LIB_DIR=$@{foreach dir,$@{CSC_LIST@},$@{dir@}/lib/lib$@{dir@}.so@}
17047
17048 $@{MAIN@}: objects $@{LIB_DIR@}
17049 gnatbind $@{MAIN@} $@{CSC_LIST:%=-aO%/lib@} -shared
17050 gnatlink $@{MAIN@} $@{CSC_LIST:%=-l%@}
17051
17052 objects::
17053 # recompile the sources
17054 gnatmake -c -i $@{MAIN@}.adb $@{NEED_FPIC@} $@{CSC_LIST:%=-I%@}
17055
17056 # Note: In a future version of GNAT, the following commands will be simplified
17057 # by a new tool, gnatmlib
17058 $@{LIB_DIR@}:
17059 mkdir -p $@{dir $@@ @}
17060 cd $@{dir $@@ @} && gcc -shared -o $@{notdir $@@ @} ../*.o -L$@{GLIB@} -lgnat
17061 cd $@{dir $@@ @} && cp -f ../*.ali .
17062
17063 # The dependencies for the modules
17064 # Note that we have to force the expansion of *.o, since in some cases
17065 # make won't be able to do it itself.
17066 aa/lib/libaa.so: $@{wildcard aa/*.o@}
17067 bb/lib/libbb.so: $@{wildcard bb/*.o@}
17068 cc/lib/libcc.so: $@{wildcard cc/*.o@}
17069
17070 # Make sure all of the shared libraries are in the path before starting the
17071 # program
17072 run::
17073 LD_LIBRARY_PATH=`pwd`/aa/lib:`pwd`/bb/lib:`pwd`/cc/lib ./$@{MAIN@}
17074
17075 clean::
17076 $@{RM@} -rf $@{CSC_LIST:%=%/lib@}
17077 $@{RM@} $@{CSC_LIST:%=%/*.ali@}
17078 $@{RM@} $@{CSC_LIST:%=%/*.o@}
17079 $@{RM@} *.o *.ali $@{MAIN@}
17080 @end example
17081
17082 @node Automatically Creating a List of Directories,Generating the Command Line Switches,Using gnatmake in a Makefile,Using the GNU make Utility
17083 @anchor{gnat_ugn/building_executable_programs_with_gnat automatically-creating-a-list-of-directories}@anchor{12e}@anchor{gnat_ugn/building_executable_programs_with_gnat id50}@anchor{12f}
17084 @subsection Automatically Creating a List of Directories
17085
17086
17087 In most makefiles, you will have to specify a list of directories, and
17088 store it in a variable. For small projects, it is often easier to
17089 specify each of them by hand, since you then have full control over what
17090 is the proper order for these directories, which ones should be
17091 included.
17092
17093 However, in larger projects, which might involve hundreds of
17094 subdirectories, it might be more convenient to generate this list
17095 automatically.
17096
17097 The example below presents two methods. The first one, although less
17098 general, gives you more control over the list. It involves wildcard
17099 characters, that are automatically expanded by @code{make}. Its
17100 shortcoming is that you need to explicitly specify some of the
17101 organization of your project, such as for instance the directory tree
17102 depth, whether some directories are found in a separate tree, etc.
17103
17104 The second method is the most general one. It requires an external
17105 program, called @code{find}, which is standard on all Unix systems. All
17106 the directories found under a given root directory will be added to the
17107 list.
17108
17109 @example
17110 # The examples below are based on the following directory hierarchy:
17111 # All the directories can contain any number of files
17112 # ROOT_DIRECTORY -> a -> aa -> aaa
17113 # -> ab
17114 # -> ac
17115 # -> b -> ba -> baa
17116 # -> bb
17117 # -> bc
17118 # This Makefile creates a variable called DIRS, that can be reused any time
17119 # you need this list (see the other examples in this section)
17120
17121 # The root of your project's directory hierarchy
17122 ROOT_DIRECTORY=.
17123
17124 ####
17125 # First method: specify explicitly the list of directories
17126 # This allows you to specify any subset of all the directories you need.
17127 ####
17128
17129 DIRS := a/aa/ a/ab/ b/ba/
17130
17131 ####
17132 # Second method: use wildcards
17133 # Note that the argument(s) to wildcard below should end with a '/'.
17134 # Since wildcards also return file names, we have to filter them out
17135 # to avoid duplicate directory names.
17136 # We thus use make's `@w{`}dir`@w{`} and `@w{`}sort`@w{`} functions.
17137 # It sets DIRs to the following value (note that the directories aaa and baa
17138 # are not given, unless you change the arguments to wildcard).
17139 # DIRS= ./a/a/ ./b/ ./a/aa/ ./a/ab/ ./a/ac/ ./b/ba/ ./b/bb/ ./b/bc/
17140 ####
17141
17142 DIRS := $@{sort $@{dir $@{wildcard $@{ROOT_DIRECTORY@}/*/
17143 $@{ROOT_DIRECTORY@}/*/*/@}@}@}
17144
17145 ####
17146 # Third method: use an external program
17147 # This command is much faster if run on local disks, avoiding NFS slowdowns.
17148 # This is the most complete command: it sets DIRs to the following value:
17149 # DIRS= ./a ./a/aa ./a/aa/aaa ./a/ab ./a/ac ./b ./b/ba ./b/ba/baa ./b/bb ./b/bc
17150 ####
17151
17152 DIRS := $@{shell find $@{ROOT_DIRECTORY@} -type d -print@}
17153 @end example
17154
17155 @node Generating the Command Line Switches,Overcoming Command Line Length Limits,Automatically Creating a List of Directories,Using the GNU make Utility
17156 @anchor{gnat_ugn/building_executable_programs_with_gnat generating-the-command-line-switches}@anchor{130}@anchor{gnat_ugn/building_executable_programs_with_gnat id51}@anchor{131}
17157 @subsection Generating the Command Line Switches
17158
17159
17160 Once you have created the list of directories as explained in the
17161 previous section (@ref{12e,,Automatically Creating a List of Directories}),
17162 you can easily generate the command line arguments to pass to gnatmake.
17163
17164 For the sake of completeness, this example assumes that the source path
17165 is not the same as the object path, and that you have two separate lists
17166 of directories.
17167
17168 @example
17169 # see "Automatically creating a list of directories" to create
17170 # these variables
17171 SOURCE_DIRS=
17172 OBJECT_DIRS=
17173
17174 GNATMAKE_SWITCHES := $@{patsubst %,-aI%,$@{SOURCE_DIRS@}@}
17175 GNATMAKE_SWITCHES += $@{patsubst %,-aO%,$@{OBJECT_DIRS@}@}
17176
17177 all:
17178 gnatmake $@{GNATMAKE_SWITCHES@} main_unit
17179 @end example
17180
17181 @node Overcoming Command Line Length Limits,,Generating the Command Line Switches,Using the GNU make Utility
17182 @anchor{gnat_ugn/building_executable_programs_with_gnat id52}@anchor{132}@anchor{gnat_ugn/building_executable_programs_with_gnat overcoming-command-line-length-limits}@anchor{133}
17183 @subsection Overcoming Command Line Length Limits
17184
17185
17186 One problem that might be encountered on big projects is that many
17187 operating systems limit the length of the command line. It is thus hard to give
17188 gnatmake the list of source and object directories.
17189
17190 This example shows how you can set up environment variables, which will
17191 make @code{gnatmake} behave exactly as if the directories had been
17192 specified on the command line, but have a much higher length limit (or
17193 even none on most systems).
17194
17195 It assumes that you have created a list of directories in your Makefile,
17196 using one of the methods presented in
17197 @ref{12e,,Automatically Creating a List of Directories}.
17198 For the sake of completeness, we assume that the object
17199 path (where the ALI files are found) is different from the sources patch.
17200
17201 Note a small trick in the Makefile below: for efficiency reasons, we
17202 create two temporary variables (SOURCE_LIST and OBJECT_LIST), that are
17203 expanded immediately by @code{make}. This way we overcome the standard
17204 make behavior which is to expand the variables only when they are
17205 actually used.
17206
17207 On Windows, if you are using the standard Windows command shell, you must
17208 replace colons with semicolons in the assignments to these variables.
17209
17210 @example
17211 # In this example, we create both ADA_INCLUDE_PATH and ADA_OBJECTS_PATH.
17212 # This is the same thing as putting the -I arguments on the command line.
17213 # (the equivalent of using -aI on the command line would be to define
17214 # only ADA_INCLUDE_PATH, the equivalent of -aO is ADA_OBJECTS_PATH).
17215 # You can of course have different values for these variables.
17216 #
17217 # Note also that we need to keep the previous values of these variables, since
17218 # they might have been set before running 'make' to specify where the GNAT
17219 # library is installed.
17220
17221 # see "Automatically creating a list of directories" to create these
17222 # variables
17223 SOURCE_DIRS=
17224 OBJECT_DIRS=
17225
17226 empty:=
17227 space:=$@{empty@} $@{empty@}
17228 SOURCE_LIST := $@{subst $@{space@},:,$@{SOURCE_DIRS@}@}
17229 OBJECT_LIST := $@{subst $@{space@},:,$@{OBJECT_DIRS@}@}
17230 ADA_INCLUDE_PATH += $@{SOURCE_LIST@}
17231 ADA_OBJECTS_PATH += $@{OBJECT_LIST@}
17232 export ADA_INCLUDE_PATH
17233 export ADA_OBJECTS_PATH
17234
17235 all:
17236 gnatmake main_unit
17237 @end example
17238
17239 @node GNAT Utility Programs,GNAT and Program Execution,Building Executable Programs with GNAT,Top
17240 @anchor{gnat_ugn/gnat_utility_programs doc}@anchor{134}@anchor{gnat_ugn/gnat_utility_programs gnat-utility-programs}@anchor{b}@anchor{gnat_ugn/gnat_utility_programs id1}@anchor{135}
17241 @chapter GNAT Utility Programs
17242
17243
17244 This chapter describes a number of utility programs:
17245
17246
17247
17248 @itemize *
17249
17250 @item
17251 @ref{136,,The File Cleanup Utility gnatclean}
17252
17253 @item
17254 @ref{137,,The GNAT Library Browser gnatls}
17255 @end itemize
17256
17257 Other GNAT utilities are described elsewhere in this manual:
17258
17259
17260 @itemize *
17261
17262 @item
17263 @ref{42,,Handling Arbitrary File Naming Conventions with gnatname}
17264
17265 @item
17266 @ref{4c,,File Name Krunching with gnatkr}
17267
17268 @item
17269 @ref{1d,,Renaming Files with gnatchop}
17270
17271 @item
17272 @ref{8f,,Preprocessing with gnatprep}
17273 @end itemize
17274
17275 @menu
17276 * The File Cleanup Utility gnatclean::
17277 * The GNAT Library Browser gnatls::
17278
17279 @end menu
17280
17281 @node The File Cleanup Utility gnatclean,The GNAT Library Browser gnatls,,GNAT Utility Programs
17282 @anchor{gnat_ugn/gnat_utility_programs id2}@anchor{138}@anchor{gnat_ugn/gnat_utility_programs the-file-cleanup-utility-gnatclean}@anchor{136}
17283 @section The File Cleanup Utility @code{gnatclean}
17284
17285
17286 @geindex File cleanup tool
17287
17288 @geindex gnatclean
17289
17290 @code{gnatclean} is a tool that allows the deletion of files produced by the
17291 compiler, binder and linker, including ALI files, object files, tree files,
17292 expanded source files, library files, interface copy source files, binder
17293 generated files and executable files.
17294
17295 @menu
17296 * Running gnatclean::
17297 * Switches for gnatclean::
17298
17299 @end menu
17300
17301 @node Running gnatclean,Switches for gnatclean,,The File Cleanup Utility gnatclean
17302 @anchor{gnat_ugn/gnat_utility_programs id3}@anchor{139}@anchor{gnat_ugn/gnat_utility_programs running-gnatclean}@anchor{13a}
17303 @subsection Running @code{gnatclean}
17304
17305
17306 The @code{gnatclean} command has the form:
17307
17308 @quotation
17309
17310 @example
17311 $ gnatclean switches names
17312 @end example
17313 @end quotation
17314
17315 where @code{names} is a list of source file names. Suffixes @code{.ads} and
17316 @code{adb} may be omitted. If a project file is specified using switch
17317 @code{-P}, then @code{names} may be completely omitted.
17318
17319 In normal mode, @code{gnatclean} delete the files produced by the compiler and,
17320 if switch @code{-c} is not specified, by the binder and
17321 the linker. In informative-only mode, specified by switch
17322 @code{-n}, the list of files that would have been deleted in
17323 normal mode is listed, but no file is actually deleted.
17324
17325 @node Switches for gnatclean,,Running gnatclean,The File Cleanup Utility gnatclean
17326 @anchor{gnat_ugn/gnat_utility_programs id4}@anchor{13b}@anchor{gnat_ugn/gnat_utility_programs switches-for-gnatclean}@anchor{13c}
17327 @subsection Switches for @code{gnatclean}
17328
17329
17330 @code{gnatclean} recognizes the following switches:
17331
17332 @geindex --version (gnatclean)
17333
17334
17335 @table @asis
17336
17337 @item @code{--version}
17338
17339 Display copyright and version, then exit disregarding all other options.
17340 @end table
17341
17342 @geindex --help (gnatclean)
17343
17344
17345 @table @asis
17346
17347 @item @code{--help}
17348
17349 If @code{--version} was not used, display usage, then exit disregarding
17350 all other options.
17351
17352 @item @code{--subdirs=@emph{subdir}}
17353
17354 Actual object directory of each project file is the subdirectory subdir of the
17355 object directory specified or defaulted in the project file.
17356
17357 @item @code{--unchecked-shared-lib-imports}
17358
17359 By default, shared library projects are not allowed to import static library
17360 projects. When this switch is used on the command line, this restriction is
17361 relaxed.
17362 @end table
17363
17364 @geindex -c (gnatclean)
17365
17366
17367 @table @asis
17368
17369 @item @code{-c}
17370
17371 Only attempt to delete the files produced by the compiler, not those produced
17372 by the binder or the linker. The files that are not to be deleted are library
17373 files, interface copy files, binder generated files and executable files.
17374 @end table
17375
17376 @geindex -D (gnatclean)
17377
17378
17379 @table @asis
17380
17381 @item @code{-D @emph{dir}}
17382
17383 Indicate that ALI and object files should normally be found in directory @code{dir}.
17384 @end table
17385
17386 @geindex -F (gnatclean)
17387
17388
17389 @table @asis
17390
17391 @item @code{-F}
17392
17393 When using project files, if some errors or warnings are detected during
17394 parsing and verbose mode is not in effect (no use of switch
17395 -v), then error lines start with the full path name of the project
17396 file, rather than its simple file name.
17397 @end table
17398
17399 @geindex -h (gnatclean)
17400
17401
17402 @table @asis
17403
17404 @item @code{-h}
17405
17406 Output a message explaining the usage of @code{gnatclean}.
17407 @end table
17408
17409 @geindex -n (gnatclean)
17410
17411
17412 @table @asis
17413
17414 @item @code{-n}
17415
17416 Informative-only mode. Do not delete any files. Output the list of the files
17417 that would have been deleted if this switch was not specified.
17418 @end table
17419
17420 @geindex -P (gnatclean)
17421
17422
17423 @table @asis
17424
17425 @item @code{-P@emph{project}}
17426
17427 Use project file @code{project}. Only one such switch can be used.
17428 When cleaning a project file, the files produced by the compilation of the
17429 immediate sources or inherited sources of the project files are to be
17430 deleted. This is not depending on the presence or not of executable names
17431 on the command line.
17432 @end table
17433
17434 @geindex -q (gnatclean)
17435
17436
17437 @table @asis
17438
17439 @item @code{-q}
17440
17441 Quiet output. If there are no errors, do not output anything, except in
17442 verbose mode (switch -v) or in informative-only mode
17443 (switch -n).
17444 @end table
17445
17446 @geindex -r (gnatclean)
17447
17448
17449 @table @asis
17450
17451 @item @code{-r}
17452
17453 When a project file is specified (using switch -P),
17454 clean all imported and extended project files, recursively. If this switch
17455 is not specified, only the files related to the main project file are to be
17456 deleted. This switch has no effect if no project file is specified.
17457 @end table
17458
17459 @geindex -v (gnatclean)
17460
17461
17462 @table @asis
17463
17464 @item @code{-v}
17465
17466 Verbose mode.
17467 @end table
17468
17469 @geindex -vP (gnatclean)
17470
17471
17472 @table @asis
17473
17474 @item @code{-vP@emph{x}}
17475
17476 Indicates the verbosity of the parsing of GNAT project files.
17477 @ref{cf,,Switches Related to Project Files}.
17478 @end table
17479
17480 @geindex -X (gnatclean)
17481
17482
17483 @table @asis
17484
17485 @item @code{-X@emph{name}=@emph{value}}
17486
17487 Indicates that external variable @code{name} has the value @code{value}.
17488 The Project Manager will use this value for occurrences of
17489 @code{external(name)} when parsing the project file.
17490 See @ref{cf,,Switches Related to Project Files}.
17491 @end table
17492
17493 @geindex -aO (gnatclean)
17494
17495
17496 @table @asis
17497
17498 @item @code{-aO@emph{dir}}
17499
17500 When searching for ALI and object files, look in directory @code{dir}.
17501 @end table
17502
17503 @geindex -I (gnatclean)
17504
17505
17506 @table @asis
17507
17508 @item @code{-I@emph{dir}}
17509
17510 Equivalent to @code{-aO@emph{dir}}.
17511 @end table
17512
17513 @geindex -I- (gnatclean)
17514
17515 @geindex Source files
17516 @geindex suppressing search
17517
17518
17519 @table @asis
17520
17521 @item @code{-I-}
17522
17523 Do not look for ALI or object files in the directory
17524 where @code{gnatclean} was invoked.
17525 @end table
17526
17527 @node The GNAT Library Browser gnatls,,The File Cleanup Utility gnatclean,GNAT Utility Programs
17528 @anchor{gnat_ugn/gnat_utility_programs id5}@anchor{13d}@anchor{gnat_ugn/gnat_utility_programs the-gnat-library-browser-gnatls}@anchor{137}
17529 @section The GNAT Library Browser @code{gnatls}
17530
17531
17532 @geindex Library browser
17533
17534 @geindex gnatls
17535
17536 @code{gnatls} is a tool that outputs information about compiled
17537 units. It gives the relationship between objects, unit names and source
17538 files. It can also be used to check the source dependencies of a unit
17539 as well as various characteristics.
17540
17541 @menu
17542 * Running gnatls::
17543 * Switches for gnatls::
17544 * Example of gnatls Usage::
17545
17546 @end menu
17547
17548 @node Running gnatls,Switches for gnatls,,The GNAT Library Browser gnatls
17549 @anchor{gnat_ugn/gnat_utility_programs id6}@anchor{13e}@anchor{gnat_ugn/gnat_utility_programs running-gnatls}@anchor{13f}
17550 @subsection Running @code{gnatls}
17551
17552
17553 The @code{gnatls} command has the form
17554
17555 @quotation
17556
17557 @example
17558 $ gnatls switches object_or_ali_file
17559 @end example
17560 @end quotation
17561
17562 The main argument is the list of object or @code{ali} files
17563 (see @ref{28,,The Ada Library Information Files})
17564 for which information is requested.
17565
17566 In normal mode, without additional option, @code{gnatls} produces a
17567 four-column listing. Each line represents information for a specific
17568 object. The first column gives the full path of the object, the second
17569 column gives the name of the principal unit in this object, the third
17570 column gives the status of the source and the fourth column gives the
17571 full path of the source representing this unit.
17572 Here is a simple example of use:
17573
17574 @quotation
17575
17576 @example
17577 $ gnatls *.o
17578 ./demo1.o demo1 DIF demo1.adb
17579 ./demo2.o demo2 OK demo2.adb
17580 ./hello.o h1 OK hello.adb
17581 ./instr-child.o instr.child MOK instr-child.adb
17582 ./instr.o instr OK instr.adb
17583 ./tef.o tef DIF tef.adb
17584 ./text_io_example.o text_io_example OK text_io_example.adb
17585 ./tgef.o tgef DIF tgef.adb
17586 @end example
17587 @end quotation
17588
17589 The first line can be interpreted as follows: the main unit which is
17590 contained in
17591 object file @code{demo1.o} is demo1, whose main source is in
17592 @code{demo1.adb}. Furthermore, the version of the source used for the
17593 compilation of demo1 has been modified (DIF). Each source file has a status
17594 qualifier which can be:
17595
17596
17597 @table @asis
17598
17599 @item @emph{OK (unchanged)}
17600
17601 The version of the source file used for the compilation of the
17602 specified unit corresponds exactly to the actual source file.
17603
17604 @item @emph{MOK (slightly modified)}
17605
17606 The version of the source file used for the compilation of the
17607 specified unit differs from the actual source file but not enough to
17608 require recompilation. If you use gnatmake with the option
17609 @code{-m} (minimal recompilation), a file marked
17610 MOK will not be recompiled.
17611
17612 @item @emph{DIF (modified)}
17613
17614 No version of the source found on the path corresponds to the source
17615 used to build this object.
17616
17617 @item @emph{??? (file not found)}
17618
17619 No source file was found for this unit.
17620
17621 @item @emph{HID (hidden, unchanged version not first on PATH)}
17622
17623 The version of the source that corresponds exactly to the source used
17624 for compilation has been found on the path but it is hidden by another
17625 version of the same source that has been modified.
17626 @end table
17627
17628 @node Switches for gnatls,Example of gnatls Usage,Running gnatls,The GNAT Library Browser gnatls
17629 @anchor{gnat_ugn/gnat_utility_programs id7}@anchor{140}@anchor{gnat_ugn/gnat_utility_programs switches-for-gnatls}@anchor{141}
17630 @subsection Switches for @code{gnatls}
17631
17632
17633 @code{gnatls} recognizes the following switches:
17634
17635 @geindex --version (gnatls)
17636
17637
17638 @table @asis
17639
17640 @item @code{--version}
17641
17642 Display copyright and version, then exit disregarding all other options.
17643 @end table
17644
17645 @geindex --help (gnatls)
17646
17647
17648 @table @asis
17649
17650 @item @code{--help}
17651
17652 If @code{--version} was not used, display usage, then exit disregarding
17653 all other options.
17654 @end table
17655
17656 @geindex -a (gnatls)
17657
17658
17659 @table @asis
17660
17661 @item @code{-a}
17662
17663 Consider all units, including those of the predefined Ada library.
17664 Especially useful with @code{-d}.
17665 @end table
17666
17667 @geindex -d (gnatls)
17668
17669
17670 @table @asis
17671
17672 @item @code{-d}
17673
17674 List sources from which specified units depend on.
17675 @end table
17676
17677 @geindex -h (gnatls)
17678
17679
17680 @table @asis
17681
17682 @item @code{-h}
17683
17684 Output the list of options.
17685 @end table
17686
17687 @geindex -o (gnatls)
17688
17689
17690 @table @asis
17691
17692 @item @code{-o}
17693
17694 Only output information about object files.
17695 @end table
17696
17697 @geindex -s (gnatls)
17698
17699
17700 @table @asis
17701
17702 @item @code{-s}
17703
17704 Only output information about source files.
17705 @end table
17706
17707 @geindex -u (gnatls)
17708
17709
17710 @table @asis
17711
17712 @item @code{-u}
17713
17714 Only output information about compilation units.
17715 @end table
17716
17717 @geindex -files (gnatls)
17718
17719
17720 @table @asis
17721
17722 @item @code{-files=@emph{file}}
17723
17724 Take as arguments the files listed in text file @code{file}.
17725 Text file @code{file} may contain empty lines that are ignored.
17726 Each nonempty line should contain the name of an existing file.
17727 Several such switches may be specified simultaneously.
17728 @end table
17729
17730 @geindex -aO (gnatls)
17731
17732 @geindex -aI (gnatls)
17733
17734 @geindex -I (gnatls)
17735
17736 @geindex -I- (gnatls)
17737
17738
17739 @table @asis
17740
17741 @item @code{-aO@emph{dir}}, @code{-aI@emph{dir}}, @code{-I@emph{dir}}, @code{-I-}, @code{-nostdinc}
17742
17743 Source path manipulation. Same meaning as the equivalent @code{gnatmake}
17744 flags (@ref{ce,,Switches for gnatmake}).
17745 @end table
17746
17747 @geindex -aP (gnatls)
17748
17749
17750 @table @asis
17751
17752 @item @code{-aP@emph{dir}}
17753
17754 Add @code{dir} at the beginning of the project search dir.
17755 @end table
17756
17757 @geindex --RTS (gnatls)
17758
17759
17760 @table @asis
17761
17762 @item @code{--RTS=@emph{rts-path}}
17763
17764 Specifies the default location of the runtime library. Same meaning as the
17765 equivalent @code{gnatmake} flag (@ref{ce,,Switches for gnatmake}).
17766 @end table
17767
17768 @geindex -v (gnatls)
17769
17770
17771 @table @asis
17772
17773 @item @code{-v}
17774
17775 Verbose mode. Output the complete source, object and project paths. Do not use
17776 the default column layout but instead use long format giving as much as
17777 information possible on each requested units, including special
17778 characteristics such as:
17779
17780
17781 @itemize *
17782
17783 @item
17784 @emph{Preelaborable}: The unit is preelaborable in the Ada sense.
17785
17786 @item
17787 @emph{No_Elab_Code}: No elaboration code has been produced by the compiler for this unit.
17788
17789 @item
17790 @emph{Pure}: The unit is pure in the Ada sense.
17791
17792 @item
17793 @emph{Elaborate_Body}: The unit contains a pragma Elaborate_Body.
17794
17795 @item
17796 @emph{Remote_Types}: The unit contains a pragma Remote_Types.
17797
17798 @item
17799 @emph{Shared_Passive}: The unit contains a pragma Shared_Passive.
17800
17801 @item
17802 @emph{Predefined}: This unit is part of the predefined environment and cannot be modified
17803 by the user.
17804
17805 @item
17806 @emph{Remote_Call_Interface}: The unit contains a pragma Remote_Call_Interface.
17807 @end itemize
17808 @end table
17809
17810 @node Example of gnatls Usage,,Switches for gnatls,The GNAT Library Browser gnatls
17811 @anchor{gnat_ugn/gnat_utility_programs example-of-gnatls-usage}@anchor{142}@anchor{gnat_ugn/gnat_utility_programs id8}@anchor{143}
17812 @subsection Example of @code{gnatls} Usage
17813
17814
17815 Example of using the verbose switch. Note how the source and
17816 object paths are affected by the -I switch.
17817
17818 @quotation
17819
17820 @example
17821 $ gnatls -v -I.. demo1.o
17822
17823 GNATLS 5.03w (20041123-34)
17824 Copyright 1997-2004 Free Software Foundation, Inc.
17825
17826 Source Search Path:
17827 <Current_Directory>
17828 ../
17829 /home/comar/local/adainclude/
17830
17831 Object Search Path:
17832 <Current_Directory>
17833 ../
17834 /home/comar/local/lib/gcc-lib/x86-linux/3.4.3/adalib/
17835
17836 Project Search Path:
17837 <Current_Directory>
17838 /home/comar/local/lib/gnat/
17839
17840 ./demo1.o
17841 Unit =>
17842 Name => demo1
17843 Kind => subprogram body
17844 Flags => No_Elab_Code
17845 Source => demo1.adb modified
17846 @end example
17847 @end quotation
17848
17849 The following is an example of use of the dependency list.
17850 Note the use of the -s switch
17851 which gives a straight list of source files. This can be useful for
17852 building specialized scripts.
17853
17854 @quotation
17855
17856 @example
17857 $ gnatls -d demo2.o
17858 ./demo2.o demo2 OK demo2.adb
17859 OK gen_list.ads
17860 OK gen_list.adb
17861 OK instr.ads
17862 OK instr-child.ads
17863
17864 $ gnatls -d -s -a demo1.o
17865 demo1.adb
17866 /home/comar/local/adainclude/ada.ads
17867 /home/comar/local/adainclude/a-finali.ads
17868 /home/comar/local/adainclude/a-filico.ads
17869 /home/comar/local/adainclude/a-stream.ads
17870 /home/comar/local/adainclude/a-tags.ads
17871 gen_list.ads
17872 gen_list.adb
17873 /home/comar/local/adainclude/gnat.ads
17874 /home/comar/local/adainclude/g-io.ads
17875 instr.ads
17876 /home/comar/local/adainclude/system.ads
17877 /home/comar/local/adainclude/s-exctab.ads
17878 /home/comar/local/adainclude/s-finimp.ads
17879 /home/comar/local/adainclude/s-finroo.ads
17880 /home/comar/local/adainclude/s-secsta.ads
17881 /home/comar/local/adainclude/s-stalib.ads
17882 /home/comar/local/adainclude/s-stoele.ads
17883 /home/comar/local/adainclude/s-stratt.ads
17884 /home/comar/local/adainclude/s-tasoli.ads
17885 /home/comar/local/adainclude/s-unstyp.ads
17886 /home/comar/local/adainclude/unchconv.ads
17887 @end example
17888 @end quotation
17889
17890
17891
17892
17893
17894
17895
17896
17897 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
17898
17899 @node GNAT and Program Execution,Platform-Specific Information,GNAT Utility Programs,Top
17900 @anchor{gnat_ugn/gnat_and_program_execution doc}@anchor{144}@anchor{gnat_ugn/gnat_and_program_execution gnat-and-program-execution}@anchor{c}@anchor{gnat_ugn/gnat_and_program_execution id1}@anchor{145}
17901 @chapter GNAT and Program Execution
17902
17903
17904 This chapter covers several topics:
17905
17906
17907 @itemize *
17908
17909 @item
17910 @ref{146,,Running and Debugging Ada Programs}
17911
17912 @item
17913 @ref{147,,Profiling}
17914
17915 @item
17916 @ref{148,,Improving Performance}
17917
17918 @item
17919 @ref{149,,Overflow Check Handling in GNAT}
17920
17921 @item
17922 @ref{14a,,Performing Dimensionality Analysis in GNAT}
17923
17924 @item
17925 @ref{14b,,Stack Related Facilities}
17926
17927 @item
17928 @ref{14c,,Memory Management Issues}
17929 @end itemize
17930
17931 @menu
17932 * Running and Debugging Ada Programs::
17933 * Profiling::
17934 * Improving Performance::
17935 * Overflow Check Handling in GNAT::
17936 * Performing Dimensionality Analysis in GNAT::
17937 * Stack Related Facilities::
17938 * Memory Management Issues::
17939
17940 @end menu
17941
17942 @node Running and Debugging Ada Programs,Profiling,,GNAT and Program Execution
17943 @anchor{gnat_ugn/gnat_and_program_execution id2}@anchor{146}@anchor{gnat_ugn/gnat_and_program_execution running-and-debugging-ada-programs}@anchor{14d}
17944 @section Running and Debugging Ada Programs
17945
17946
17947 @geindex Debugging
17948
17949 This section discusses how to debug Ada programs.
17950
17951 An incorrect Ada program may be handled in three ways by the GNAT compiler:
17952
17953
17954 @itemize *
17955
17956 @item
17957 The illegality may be a violation of the static semantics of Ada. In
17958 that case GNAT diagnoses the constructs in the program that are illegal.
17959 It is then a straightforward matter for the user to modify those parts of
17960 the program.
17961
17962 @item
17963 The illegality may be a violation of the dynamic semantics of Ada. In
17964 that case the program compiles and executes, but may generate incorrect
17965 results, or may terminate abnormally with some exception.
17966
17967 @item
17968 When presented with a program that contains convoluted errors, GNAT
17969 itself may terminate abnormally without providing full diagnostics on
17970 the incorrect user program.
17971 @end itemize
17972
17973 @geindex Debugger
17974
17975 @geindex gdb
17976
17977 @menu
17978 * The GNAT Debugger GDB::
17979 * Running GDB::
17980 * Introduction to GDB Commands::
17981 * Using Ada Expressions::
17982 * Calling User-Defined Subprograms::
17983 * Using the next Command in a Function::
17984 * Stopping When Ada Exceptions Are Raised::
17985 * Ada Tasks::
17986 * Debugging Generic Units::
17987 * Remote Debugging with gdbserver::
17988 * GNAT Abnormal Termination or Failure to Terminate::
17989 * Naming Conventions for GNAT Source Files::
17990 * Getting Internal Debugging Information::
17991 * Stack Traceback::
17992 * Pretty-Printers for the GNAT runtime::
17993
17994 @end menu
17995
17996 @node The GNAT Debugger GDB,Running GDB,,Running and Debugging Ada Programs
17997 @anchor{gnat_ugn/gnat_and_program_execution id3}@anchor{14e}@anchor{gnat_ugn/gnat_and_program_execution the-gnat-debugger-gdb}@anchor{14f}
17998 @subsection The GNAT Debugger GDB
17999
18000
18001 @code{GDB} is a general purpose, platform-independent debugger that
18002 can be used to debug mixed-language programs compiled with @code{gcc},
18003 and in particular is capable of debugging Ada programs compiled with
18004 GNAT. The latest versions of @code{GDB} are Ada-aware and can handle
18005 complex Ada data structures.
18006
18007 See @cite{Debugging with GDB},
18008 for full details on the usage of @code{GDB}, including a section on
18009 its usage on programs. This manual should be consulted for full
18010 details. The section that follows is a brief introduction to the
18011 philosophy and use of @code{GDB}.
18012
18013 When GNAT programs are compiled, the compiler optionally writes debugging
18014 information into the generated object file, including information on
18015 line numbers, and on declared types and variables. This information is
18016 separate from the generated code. It makes the object files considerably
18017 larger, but it does not add to the size of the actual executable that
18018 will be loaded into memory, and has no impact on run-time performance. The
18019 generation of debug information is triggered by the use of the
18020 @code{-g} switch in the @code{gcc} or @code{gnatmake} command
18021 used to carry out the compilations. It is important to emphasize that
18022 the use of these options does not change the generated code.
18023
18024 The debugging information is written in standard system formats that
18025 are used by many tools, including debuggers and profilers. The format
18026 of the information is typically designed to describe C types and
18027 semantics, but GNAT implements a translation scheme which allows full
18028 details about Ada types and variables to be encoded into these
18029 standard C formats. Details of this encoding scheme may be found in
18030 the file exp_dbug.ads in the GNAT source distribution. However, the
18031 details of this encoding are, in general, of no interest to a user,
18032 since @code{GDB} automatically performs the necessary decoding.
18033
18034 When a program is bound and linked, the debugging information is
18035 collected from the object files, and stored in the executable image of
18036 the program. Again, this process significantly increases the size of
18037 the generated executable file, but it does not increase the size of
18038 the executable program itself. Furthermore, if this program is run in
18039 the normal manner, it runs exactly as if the debug information were
18040 not present, and takes no more actual memory.
18041
18042 However, if the program is run under control of @code{GDB}, the
18043 debugger is activated. The image of the program is loaded, at which
18044 point it is ready to run. If a run command is given, then the program
18045 will run exactly as it would have if @code{GDB} were not present. This
18046 is a crucial part of the @code{GDB} design philosophy. @code{GDB} is
18047 entirely non-intrusive until a breakpoint is encountered. If no
18048 breakpoint is ever hit, the program will run exactly as it would if no
18049 debugger were present. When a breakpoint is hit, @code{GDB} accesses
18050 the debugging information and can respond to user commands to inspect
18051 variables, and more generally to report on the state of execution.
18052
18053 @node Running GDB,Introduction to GDB Commands,The GNAT Debugger GDB,Running and Debugging Ada Programs
18054 @anchor{gnat_ugn/gnat_and_program_execution id4}@anchor{150}@anchor{gnat_ugn/gnat_and_program_execution running-gdb}@anchor{151}
18055 @subsection Running GDB
18056
18057
18058 This section describes how to initiate the debugger.
18059
18060 The debugger can be launched from a @code{GNAT Studio} menu or
18061 directly from the command line. The description below covers the latter use.
18062 All the commands shown can be used in the @code{GNAT Studio} debug console window,
18063 but there are usually more GUI-based ways to achieve the same effect.
18064
18065 The command to run @code{GDB} is
18066
18067 @quotation
18068
18069 @example
18070 $ gdb program
18071 @end example
18072 @end quotation
18073
18074 where @code{program} is the name of the executable file. This
18075 activates the debugger and results in a prompt for debugger commands.
18076 The simplest command is simply @code{run}, which causes the program to run
18077 exactly as if the debugger were not present. The following section
18078 describes some of the additional commands that can be given to @code{GDB}.
18079
18080 @node Introduction to GDB Commands,Using Ada Expressions,Running GDB,Running and Debugging Ada Programs
18081 @anchor{gnat_ugn/gnat_and_program_execution id5}@anchor{152}@anchor{gnat_ugn/gnat_and_program_execution introduction-to-gdb-commands}@anchor{153}
18082 @subsection Introduction to GDB Commands
18083
18084
18085 @code{GDB} contains a large repertoire of commands.
18086 See @cite{Debugging with GDB} for extensive documentation on the use
18087 of these commands, together with examples of their use. Furthermore,
18088 the command @emph{help} invoked from within GDB activates a simple help
18089 facility which summarizes the available commands and their options.
18090 In this section we summarize a few of the most commonly
18091 used commands to give an idea of what @code{GDB} is about. You should create
18092 a simple program with debugging information and experiment with the use of
18093 these @code{GDB} commands on the program as you read through the
18094 following section.
18095
18096
18097 @itemize *
18098
18099 @item
18100
18101 @table @asis
18102
18103 @item @code{set args @emph{arguments}}
18104
18105 The @emph{arguments} list above is a list of arguments to be passed to
18106 the program on a subsequent run command, just as though the arguments
18107 had been entered on a normal invocation of the program. The @code{set args}
18108 command is not needed if the program does not require arguments.
18109 @end table
18110
18111 @item
18112
18113 @table @asis
18114
18115 @item @code{run}
18116
18117 The @code{run} command causes execution of the program to start from
18118 the beginning. If the program is already running, that is to say if
18119 you are currently positioned at a breakpoint, then a prompt will ask
18120 for confirmation that you want to abandon the current execution and
18121 restart.
18122 @end table
18123
18124 @item
18125
18126 @table @asis
18127
18128 @item @code{breakpoint @emph{location}}
18129
18130 The breakpoint command sets a breakpoint, that is to say a point at which
18131 execution will halt and @code{GDB} will await further
18132 commands. @emph{location} is
18133 either a line number within a file, given in the format @code{file:linenumber},
18134 or it is the name of a subprogram. If you request that a breakpoint be set on
18135 a subprogram that is overloaded, a prompt will ask you to specify on which of
18136 those subprograms you want to breakpoint. You can also
18137 specify that all of them should be breakpointed. If the program is run
18138 and execution encounters the breakpoint, then the program
18139 stops and @code{GDB} signals that the breakpoint was encountered by
18140 printing the line of code before which the program is halted.
18141 @end table
18142
18143 @item
18144
18145 @table @asis
18146
18147 @item @code{catch exception @emph{name}}
18148
18149 This command causes the program execution to stop whenever exception
18150 @code{name} is raised. If @code{name} is omitted, then the execution is
18151 suspended when any exception is raised.
18152 @end table
18153
18154 @item
18155
18156 @table @asis
18157
18158 @item @code{print @emph{expression}}
18159
18160 This will print the value of the given expression. Most simple
18161 Ada expression formats are properly handled by @code{GDB}, so the expression
18162 can contain function calls, variables, operators, and attribute references.
18163 @end table
18164
18165 @item
18166
18167 @table @asis
18168
18169 @item @code{continue}
18170
18171 Continues execution following a breakpoint, until the next breakpoint or the
18172 termination of the program.
18173 @end table
18174
18175 @item
18176
18177 @table @asis
18178
18179 @item @code{step}
18180
18181 Executes a single line after a breakpoint. If the next statement
18182 is a subprogram call, execution continues into (the first statement of)
18183 the called subprogram.
18184 @end table
18185
18186 @item
18187
18188 @table @asis
18189
18190 @item @code{next}
18191
18192 Executes a single line. If this line is a subprogram call, executes and
18193 returns from the call.
18194 @end table
18195
18196 @item
18197
18198 @table @asis
18199
18200 @item @code{list}
18201
18202 Lists a few lines around the current source location. In practice, it
18203 is usually more convenient to have a separate edit window open with the
18204 relevant source file displayed. Successive applications of this command
18205 print subsequent lines. The command can be given an argument which is a
18206 line number, in which case it displays a few lines around the specified one.
18207 @end table
18208
18209 @item
18210
18211 @table @asis
18212
18213 @item @code{backtrace}
18214
18215 Displays a backtrace of the call chain. This command is typically
18216 used after a breakpoint has occurred, to examine the sequence of calls that
18217 leads to the current breakpoint. The display includes one line for each
18218 activation record (frame) corresponding to an active subprogram.
18219 @end table
18220
18221 @item
18222
18223 @table @asis
18224
18225 @item @code{up}
18226
18227 At a breakpoint, @code{GDB} can display the values of variables local
18228 to the current frame. The command @code{up} can be used to
18229 examine the contents of other active frames, by moving the focus up
18230 the stack, that is to say from callee to caller, one frame at a time.
18231 @end table
18232
18233 @item
18234
18235 @table @asis
18236
18237 @item @code{down}
18238
18239 Moves the focus of @code{GDB} down from the frame currently being
18240 examined to the frame of its callee (the reverse of the previous command),
18241 @end table
18242
18243 @item
18244
18245 @table @asis
18246
18247 @item @code{frame @emph{n}}
18248
18249 Inspect the frame with the given number. The value 0 denotes the frame
18250 of the current breakpoint, that is to say the top of the call stack.
18251 @end table
18252
18253 @item
18254
18255 @table @asis
18256
18257 @item @code{kill}
18258
18259 Kills the child process in which the program is running under GDB.
18260 This may be useful for several purposes:
18261
18262
18263 @itemize *
18264
18265 @item
18266 It allows you to recompile and relink your program, since on many systems
18267 you cannot regenerate an executable file while it is running in a process.
18268
18269 @item
18270 You can run your program outside the debugger, on systems that do not
18271 permit executing a program outside GDB while breakpoints are set
18272 within GDB.
18273
18274 @item
18275 It allows you to debug a core dump rather than a running process.
18276 @end itemize
18277 @end table
18278 @end itemize
18279
18280 The above list is a very short introduction to the commands that
18281 @code{GDB} provides. Important additional capabilities, including conditional
18282 breakpoints, the ability to execute command sequences on a breakpoint,
18283 the ability to debug at the machine instruction level and many other
18284 features are described in detail in @cite{Debugging with GDB}.
18285 Note that most commands can be abbreviated
18286 (for example, c for continue, bt for backtrace).
18287
18288 @node Using Ada Expressions,Calling User-Defined Subprograms,Introduction to GDB Commands,Running and Debugging Ada Programs
18289 @anchor{gnat_ugn/gnat_and_program_execution id6}@anchor{154}@anchor{gnat_ugn/gnat_and_program_execution using-ada-expressions}@anchor{155}
18290 @subsection Using Ada Expressions
18291
18292
18293 @geindex Ada expressions (in gdb)
18294
18295 @code{GDB} supports a fairly large subset of Ada expression syntax, with some
18296 extensions. The philosophy behind the design of this subset is
18297
18298 @quotation
18299
18300
18301 @itemize *
18302
18303 @item
18304 That @code{GDB} should provide basic literals and access to operations for
18305 arithmetic, dereferencing, field selection, indexing, and subprogram calls,
18306 leaving more sophisticated computations to subprograms written into the
18307 program (which therefore may be called from @code{GDB}).
18308
18309 @item
18310 That type safety and strict adherence to Ada language restrictions
18311 are not particularly relevant in a debugging context.
18312
18313 @item
18314 That brevity is important to the @code{GDB} user.
18315 @end itemize
18316 @end quotation
18317
18318 Thus, for brevity, the debugger acts as if there were
18319 implicit @code{with} and @code{use} clauses in effect for all user-written
18320 packages, thus making it unnecessary to fully qualify most names with
18321 their packages, regardless of context. Where this causes ambiguity,
18322 @code{GDB} asks the user’s intent.
18323
18324 For details on the supported Ada syntax, see @cite{Debugging with GDB}.
18325
18326 @node Calling User-Defined Subprograms,Using the next Command in a Function,Using Ada Expressions,Running and Debugging Ada Programs
18327 @anchor{gnat_ugn/gnat_and_program_execution calling-user-defined-subprograms}@anchor{156}@anchor{gnat_ugn/gnat_and_program_execution id7}@anchor{157}
18328 @subsection Calling User-Defined Subprograms
18329
18330
18331 An important capability of @code{GDB} is the ability to call user-defined
18332 subprograms while debugging. This is achieved simply by entering
18333 a subprogram call statement in the form:
18334
18335 @quotation
18336
18337 @example
18338 call subprogram-name (parameters)
18339 @end example
18340 @end quotation
18341
18342 The keyword @code{call} can be omitted in the normal case where the
18343 @code{subprogram-name} does not coincide with any of the predefined
18344 @code{GDB} commands.
18345
18346 The effect is to invoke the given subprogram, passing it the
18347 list of parameters that is supplied. The parameters can be expressions and
18348 can include variables from the program being debugged. The
18349 subprogram must be defined
18350 at the library level within your program, and @code{GDB} will call the
18351 subprogram within the environment of your program execution (which
18352 means that the subprogram is free to access or even modify variables
18353 within your program).
18354
18355 The most important use of this facility is in allowing the inclusion of
18356 debugging routines that are tailored to particular data structures
18357 in your program. Such debugging routines can be written to provide a suitably
18358 high-level description of an abstract type, rather than a low-level dump
18359 of its physical layout. After all, the standard
18360 @code{GDB print} command only knows the physical layout of your
18361 types, not their abstract meaning. Debugging routines can provide information
18362 at the desired semantic level and are thus enormously useful.
18363
18364 For example, when debugging GNAT itself, it is crucial to have access to
18365 the contents of the tree nodes used to represent the program internally.
18366 But tree nodes are represented simply by an integer value (which in turn
18367 is an index into a table of nodes).
18368 Using the @code{print} command on a tree node would simply print this integer
18369 value, which is not very useful. But the PN routine (defined in file
18370 treepr.adb in the GNAT sources) takes a tree node as input, and displays
18371 a useful high level representation of the tree node, which includes the
18372 syntactic category of the node, its position in the source, the integers
18373 that denote descendant nodes and parent node, as well as varied
18374 semantic information. To study this example in more detail, you might want to
18375 look at the body of the PN procedure in the stated file.
18376
18377 Another useful application of this capability is to deal with situations of
18378 complex data which are not handled suitably by GDB. For example, if you specify
18379 Convention Fortran for a multi-dimensional array, GDB does not know that
18380 the ordering of array elements has been switched and will not properly
18381 address the array elements. In such a case, instead of trying to print the
18382 elements directly from GDB, you can write a callable procedure that prints
18383 the elements in the desired format.
18384
18385 @node Using the next Command in a Function,Stopping When Ada Exceptions Are Raised,Calling User-Defined Subprograms,Running and Debugging Ada Programs
18386 @anchor{gnat_ugn/gnat_and_program_execution id8}@anchor{158}@anchor{gnat_ugn/gnat_and_program_execution using-the-next-command-in-a-function}@anchor{159}
18387 @subsection Using the @emph{next} Command in a Function
18388
18389
18390 When you use the @code{next} command in a function, the current source
18391 location will advance to the next statement as usual. A special case
18392 arises in the case of a @code{return} statement.
18393
18394 Part of the code for a return statement is the ‘epilogue’ of the function.
18395 This is the code that returns to the caller. There is only one copy of
18396 this epilogue code, and it is typically associated with the last return
18397 statement in the function if there is more than one return. In some
18398 implementations, this epilogue is associated with the first statement
18399 of the function.
18400
18401 The result is that if you use the @code{next} command from a return
18402 statement that is not the last return statement of the function you
18403 may see a strange apparent jump to the last return statement or to
18404 the start of the function. You should simply ignore this odd jump.
18405 The value returned is always that from the first return statement
18406 that was stepped through.
18407
18408 @node Stopping When Ada Exceptions Are Raised,Ada Tasks,Using the next Command in a Function,Running and Debugging Ada Programs
18409 @anchor{gnat_ugn/gnat_and_program_execution id9}@anchor{15a}@anchor{gnat_ugn/gnat_and_program_execution stopping-when-ada-exceptions-are-raised}@anchor{15b}
18410 @subsection Stopping When Ada Exceptions Are Raised
18411
18412
18413 @geindex Exceptions (in gdb)
18414
18415 You can set catchpoints that stop the program execution when your program
18416 raises selected exceptions.
18417
18418
18419 @itemize *
18420
18421 @item
18422
18423 @table @asis
18424
18425 @item @code{catch exception}
18426
18427 Set a catchpoint that stops execution whenever (any task in the) program
18428 raises any exception.
18429 @end table
18430
18431 @item
18432
18433 @table @asis
18434
18435 @item @code{catch exception @emph{name}}
18436
18437 Set a catchpoint that stops execution whenever (any task in the) program
18438 raises the exception @emph{name}.
18439 @end table
18440
18441 @item
18442
18443 @table @asis
18444
18445 @item @code{catch exception unhandled}
18446
18447 Set a catchpoint that stops executing whenever (any task in the) program
18448 raises an exception for which there is no handler.
18449 @end table
18450
18451 @item
18452
18453 @table @asis
18454
18455 @item @code{info exceptions}, @code{info exceptions @emph{regexp}}
18456
18457 The @code{info exceptions} command permits the user to examine all defined
18458 exceptions within Ada programs. With a regular expression, @emph{regexp}, as
18459 argument, prints out only those exceptions whose name matches @emph{regexp}.
18460 @end table
18461 @end itemize
18462
18463 @geindex Tasks (in gdb)
18464
18465 @node Ada Tasks,Debugging Generic Units,Stopping When Ada Exceptions Are Raised,Running and Debugging Ada Programs
18466 @anchor{gnat_ugn/gnat_and_program_execution ada-tasks}@anchor{15c}@anchor{gnat_ugn/gnat_and_program_execution id10}@anchor{15d}
18467 @subsection Ada Tasks
18468
18469
18470 @code{GDB} allows the following task-related commands:
18471
18472
18473 @itemize *
18474
18475 @item
18476
18477 @table @asis
18478
18479 @item @code{info tasks}
18480
18481 This command shows a list of current Ada tasks, as in the following example:
18482
18483 @example
18484 (gdb) info tasks
18485 ID TID P-ID Thread Pri State Name
18486 1 8088000 0 807e000 15 Child Activation Wait main_task
18487 2 80a4000 1 80ae000 15 Accept/Select Wait b
18488 3 809a800 1 80a4800 15 Child Activation Wait a
18489 * 4 80ae800 3 80b8000 15 Running c
18490 @end example
18491
18492 In this listing, the asterisk before the first task indicates it to be the
18493 currently running task. The first column lists the task ID that is used
18494 to refer to tasks in the following commands.
18495 @end table
18496 @end itemize
18497
18498 @geindex Breakpoints and tasks
18499
18500
18501 @itemize *
18502
18503 @item
18504 @code{break`@w{`}*linespec* `@w{`}task} @emph{taskid}, @code{break} @emph{linespec} @code{task} @emph{taskid} @code{if} …
18505
18506 @quotation
18507
18508 These commands are like the @code{break ... thread ...}.
18509 @emph{linespec} specifies source lines.
18510
18511 Use the qualifier @code{task @emph{taskid}} with a breakpoint command
18512 to specify that you only want @code{GDB} to stop the program when a
18513 particular Ada task reaches this breakpoint. @emph{taskid} is one of the
18514 numeric task identifiers assigned by @code{GDB}, shown in the first
18515 column of the @code{info tasks} display.
18516
18517 If you do not specify @code{task @emph{taskid}} when you set a
18518 breakpoint, the breakpoint applies to @emph{all} tasks of your
18519 program.
18520
18521 You can use the @code{task} qualifier on conditional breakpoints as
18522 well; in this case, place @code{task @emph{taskid}} before the
18523 breakpoint condition (before the @code{if}).
18524 @end quotation
18525 @end itemize
18526
18527 @geindex Task switching (in gdb)
18528
18529
18530 @itemize *
18531
18532 @item
18533 @code{task @emph{taskno}}
18534
18535 @quotation
18536
18537 This command allows switching to the task referred by @emph{taskno}. In
18538 particular, this allows browsing of the backtrace of the specified
18539 task. It is advisable to switch back to the original task before
18540 continuing execution otherwise the scheduling of the program may be
18541 perturbed.
18542 @end quotation
18543 @end itemize
18544
18545 For more detailed information on the tasking support,
18546 see @cite{Debugging with GDB}.
18547
18548 @geindex Debugging Generic Units
18549
18550 @geindex Generics
18551
18552 @node Debugging Generic Units,Remote Debugging with gdbserver,Ada Tasks,Running and Debugging Ada Programs
18553 @anchor{gnat_ugn/gnat_and_program_execution debugging-generic-units}@anchor{15e}@anchor{gnat_ugn/gnat_and_program_execution id11}@anchor{15f}
18554 @subsection Debugging Generic Units
18555
18556
18557 GNAT always uses code expansion for generic instantiation. This means that
18558 each time an instantiation occurs, a complete copy of the original code is
18559 made, with appropriate substitutions of formals by actuals.
18560
18561 It is not possible to refer to the original generic entities in
18562 @code{GDB}, but it is always possible to debug a particular instance of
18563 a generic, by using the appropriate expanded names. For example, if we have
18564
18565 @quotation
18566
18567 @example
18568 procedure g is
18569
18570 generic package k is
18571 procedure kp (v1 : in out integer);
18572 end k;
18573
18574 package body k is
18575 procedure kp (v1 : in out integer) is
18576 begin
18577 v1 := v1 + 1;
18578 end kp;
18579 end k;
18580
18581 package k1 is new k;
18582 package k2 is new k;
18583
18584 var : integer := 1;
18585
18586 begin
18587 k1.kp (var);
18588 k2.kp (var);
18589 k1.kp (var);
18590 k2.kp (var);
18591 end;
18592 @end example
18593 @end quotation
18594
18595 Then to break on a call to procedure kp in the k2 instance, simply
18596 use the command:
18597
18598 @quotation
18599
18600 @example
18601 (gdb) break g.k2.kp
18602 @end example
18603 @end quotation
18604
18605 When the breakpoint occurs, you can step through the code of the
18606 instance in the normal manner and examine the values of local variables, as for
18607 other units.
18608
18609 @geindex Remote Debugging with gdbserver
18610
18611 @node Remote Debugging with gdbserver,GNAT Abnormal Termination or Failure to Terminate,Debugging Generic Units,Running and Debugging Ada Programs
18612 @anchor{gnat_ugn/gnat_and_program_execution id12}@anchor{160}@anchor{gnat_ugn/gnat_and_program_execution remote-debugging-with-gdbserver}@anchor{161}
18613 @subsection Remote Debugging with gdbserver
18614
18615
18616 On platforms where gdbserver is supported, it is possible to use this tool
18617 to debug your application remotely. This can be useful in situations
18618 where the program needs to be run on a target host that is different
18619 from the host used for development, particularly when the target has
18620 a limited amount of resources (either CPU and/or memory).
18621
18622 To do so, start your program using gdbserver on the target machine.
18623 gdbserver then automatically suspends the execution of your program
18624 at its entry point, waiting for a debugger to connect to it. The
18625 following commands starts an application and tells gdbserver to
18626 wait for a connection with the debugger on localhost port 4444.
18627
18628 @quotation
18629
18630 @example
18631 $ gdbserver localhost:4444 program
18632 Process program created; pid = 5685
18633 Listening on port 4444
18634 @end example
18635 @end quotation
18636
18637 Once gdbserver has started listening, we can tell the debugger to establish
18638 a connection with this gdbserver, and then start the same debugging session
18639 as if the program was being debugged on the same host, directly under
18640 the control of GDB.
18641
18642 @quotation
18643
18644 @example
18645 $ gdb program
18646 (gdb) target remote targethost:4444
18647 Remote debugging using targethost:4444
18648 0x00007f29936d0af0 in ?? () from /lib64/ld-linux-x86-64.so.
18649 (gdb) b foo.adb:3
18650 Breakpoint 1 at 0x401f0c: file foo.adb, line 3.
18651 (gdb) continue
18652 Continuing.
18653
18654 Breakpoint 1, foo () at foo.adb:4
18655 4 end foo;
18656 @end example
18657 @end quotation
18658
18659 It is also possible to use gdbserver to attach to an already running
18660 program, in which case the execution of that program is simply suspended
18661 until the connection between the debugger and gdbserver is established.
18662
18663 For more information on how to use gdbserver, see the @emph{Using the gdbserver Program}
18664 section in @cite{Debugging with GDB}.
18665 GNAT provides support for gdbserver on x86-linux, x86-windows and x86_64-linux.
18666
18667 @geindex Abnormal Termination or Failure to Terminate
18668
18669 @node GNAT Abnormal Termination or Failure to Terminate,Naming Conventions for GNAT Source Files,Remote Debugging with gdbserver,Running and Debugging Ada Programs
18670 @anchor{gnat_ugn/gnat_and_program_execution gnat-abnormal-termination-or-failure-to-terminate}@anchor{162}@anchor{gnat_ugn/gnat_and_program_execution id13}@anchor{163}
18671 @subsection GNAT Abnormal Termination or Failure to Terminate
18672
18673
18674 When presented with programs that contain serious errors in syntax
18675 or semantics,
18676 GNAT may on rare occasions experience problems in operation, such
18677 as aborting with a
18678 segmentation fault or illegal memory access, raising an internal
18679 exception, terminating abnormally, or failing to terminate at all.
18680 In such cases, you can activate
18681 various features of GNAT that can help you pinpoint the construct in your
18682 program that is the likely source of the problem.
18683
18684 The following strategies are presented in increasing order of
18685 difficulty, corresponding to your experience in using GNAT and your
18686 familiarity with compiler internals.
18687
18688
18689 @itemize *
18690
18691 @item
18692 Run @code{gcc} with the @code{-gnatf}. This first
18693 switch causes all errors on a given line to be reported. In its absence,
18694 only the first error on a line is displayed.
18695
18696 The @code{-gnatdO} switch causes errors to be displayed as soon as they
18697 are encountered, rather than after compilation is terminated. If GNAT
18698 terminates prematurely or goes into an infinite loop, the last error
18699 message displayed may help to pinpoint the culprit.
18700
18701 @item
18702 Run @code{gcc} with the @code{-v} (verbose) switch. In this
18703 mode, @code{gcc} produces ongoing information about the progress of the
18704 compilation and provides the name of each procedure as code is
18705 generated. This switch allows you to find which Ada procedure was being
18706 compiled when it encountered a code generation problem.
18707 @end itemize
18708
18709 @geindex -gnatdc switch
18710
18711
18712 @itemize *
18713
18714 @item
18715 Run @code{gcc} with the @code{-gnatdc} switch. This is a GNAT specific
18716 switch that does for the front-end what @code{-v} does
18717 for the back end. The system prints the name of each unit,
18718 either a compilation unit or nested unit, as it is being analyzed.
18719
18720 @item
18721 Finally, you can start
18722 @code{gdb} directly on the @code{gnat1} executable. @code{gnat1} is the
18723 front-end of GNAT, and can be run independently (normally it is just
18724 called from @code{gcc}). You can use @code{gdb} on @code{gnat1} as you
18725 would on a C program (but @ref{14f,,The GNAT Debugger GDB} for caveats). The
18726 @code{where} command is the first line of attack; the variable
18727 @code{lineno} (seen by @code{print lineno}), used by the second phase of
18728 @code{gnat1} and by the @code{gcc} backend, indicates the source line at
18729 which the execution stopped, and @code{input_file name} indicates the name of
18730 the source file.
18731 @end itemize
18732
18733 @node Naming Conventions for GNAT Source Files,Getting Internal Debugging Information,GNAT Abnormal Termination or Failure to Terminate,Running and Debugging Ada Programs
18734 @anchor{gnat_ugn/gnat_and_program_execution id14}@anchor{164}@anchor{gnat_ugn/gnat_and_program_execution naming-conventions-for-gnat-source-files}@anchor{165}
18735 @subsection Naming Conventions for GNAT Source Files
18736
18737
18738 In order to examine the workings of the GNAT system, the following
18739 brief description of its organization may be helpful:
18740
18741
18742 @itemize *
18743
18744 @item
18745 Files with prefix @code{sc} contain the lexical scanner.
18746
18747 @item
18748 All files prefixed with @code{par} are components of the parser. The
18749 numbers correspond to chapters of the Ada Reference Manual. For example,
18750 parsing of select statements can be found in @code{par-ch9.adb}.
18751
18752 @item
18753 All files prefixed with @code{sem} perform semantic analysis. The
18754 numbers correspond to chapters of the Ada standard. For example, all
18755 issues involving context clauses can be found in @code{sem_ch10.adb}. In
18756 addition, some features of the language require sufficient special processing
18757 to justify their own semantic files: sem_aggr for aggregates, sem_disp for
18758 dynamic dispatching, etc.
18759
18760 @item
18761 All files prefixed with @code{exp} perform normalization and
18762 expansion of the intermediate representation (abstract syntax tree, or AST).
18763 these files use the same numbering scheme as the parser and semantics files.
18764 For example, the construction of record initialization procedures is done in
18765 @code{exp_ch3.adb}.
18766
18767 @item
18768 The files prefixed with @code{bind} implement the binder, which
18769 verifies the consistency of the compilation, determines an order of
18770 elaboration, and generates the bind file.
18771
18772 @item
18773 The files @code{atree.ads} and @code{atree.adb} detail the low-level
18774 data structures used by the front-end.
18775
18776 @item
18777 The files @code{sinfo.ads} and @code{sinfo.adb} detail the structure of
18778 the abstract syntax tree as produced by the parser.
18779
18780 @item
18781 The files @code{einfo.ads} and @code{einfo.adb} detail the attributes of
18782 all entities, computed during semantic analysis.
18783
18784 @item
18785 Library management issues are dealt with in files with prefix
18786 @code{lib}.
18787
18788 @geindex Annex A (in Ada Reference Manual)
18789
18790 @item
18791 Ada files with the prefix @code{a-} are children of @code{Ada}, as
18792 defined in Annex A.
18793
18794 @geindex Annex B (in Ada reference Manual)
18795
18796 @item
18797 Files with prefix @code{i-} are children of @code{Interfaces}, as
18798 defined in Annex B.
18799
18800 @geindex System (package in Ada Reference Manual)
18801
18802 @item
18803 Files with prefix @code{s-} are children of @code{System}. This includes
18804 both language-defined children and GNAT run-time routines.
18805
18806 @geindex GNAT (package)
18807
18808 @item
18809 Files with prefix @code{g-} are children of @code{GNAT}. These are useful
18810 general-purpose packages, fully documented in their specs. All
18811 the other @code{.c} files are modifications of common @code{gcc} files.
18812 @end itemize
18813
18814 @node Getting Internal Debugging Information,Stack Traceback,Naming Conventions for GNAT Source Files,Running and Debugging Ada Programs
18815 @anchor{gnat_ugn/gnat_and_program_execution getting-internal-debugging-information}@anchor{166}@anchor{gnat_ugn/gnat_and_program_execution id15}@anchor{167}
18816 @subsection Getting Internal Debugging Information
18817
18818
18819 Most compilers have internal debugging switches and modes. GNAT
18820 does also, except GNAT internal debugging switches and modes are not
18821 secret. A summary and full description of all the compiler and binder
18822 debug flags are in the file @code{debug.adb}. You must obtain the
18823 sources of the compiler to see the full detailed effects of these flags.
18824
18825 The switches that print the source of the program (reconstructed from
18826 the internal tree) are of general interest for user programs, as are the
18827 options to print
18828 the full internal tree, and the entity table (the symbol table
18829 information). The reconstructed source provides a readable version of the
18830 program after the front-end has completed analysis and expansion,
18831 and is useful when studying the performance of specific constructs.
18832 For example, constraint checks are indicated, complex aggregates
18833 are replaced with loops and assignments, and tasking primitives
18834 are replaced with run-time calls.
18835
18836 @geindex traceback
18837
18838 @geindex stack traceback
18839
18840 @geindex stack unwinding
18841
18842 @node Stack Traceback,Pretty-Printers for the GNAT runtime,Getting Internal Debugging Information,Running and Debugging Ada Programs
18843 @anchor{gnat_ugn/gnat_and_program_execution id16}@anchor{168}@anchor{gnat_ugn/gnat_and_program_execution stack-traceback}@anchor{169}
18844 @subsection Stack Traceback
18845
18846
18847 Traceback is a mechanism to display the sequence of subprogram calls that
18848 leads to a specified execution point in a program. Often (but not always)
18849 the execution point is an instruction at which an exception has been raised.
18850 This mechanism is also known as @emph{stack unwinding} because it obtains
18851 its information by scanning the run-time stack and recovering the activation
18852 records of all active subprograms. Stack unwinding is one of the most
18853 important tools for program debugging.
18854
18855 The first entry stored in traceback corresponds to the deepest calling level,
18856 that is to say the subprogram currently executing the instruction
18857 from which we want to obtain the traceback.
18858
18859 Note that there is no runtime performance penalty when stack traceback
18860 is enabled, and no exception is raised during program execution.
18861
18862 @geindex traceback
18863 @geindex non-symbolic
18864
18865 @menu
18866 * Non-Symbolic Traceback::
18867 * Symbolic Traceback::
18868
18869 @end menu
18870
18871 @node Non-Symbolic Traceback,Symbolic Traceback,,Stack Traceback
18872 @anchor{gnat_ugn/gnat_and_program_execution id17}@anchor{16a}@anchor{gnat_ugn/gnat_and_program_execution non-symbolic-traceback}@anchor{16b}
18873 @subsubsection Non-Symbolic Traceback
18874
18875
18876 Note: this feature is not supported on all platforms. See
18877 @code{GNAT.Traceback} spec in @code{g-traceb.ads}
18878 for a complete list of supported platforms.
18879
18880 @subsubheading Tracebacks From an Unhandled Exception
18881
18882
18883 A runtime non-symbolic traceback is a list of addresses of call instructions.
18884 To enable this feature you must use the @code{-E}
18885 @code{gnatbind} option. With this option a stack traceback is stored as part
18886 of exception information. You can retrieve this information using the
18887 @code{addr2line} tool.
18888
18889 Here is a simple example:
18890
18891 @quotation
18892
18893 @example
18894 procedure STB is
18895
18896 procedure P1 is
18897 begin
18898 raise Constraint_Error;
18899 end P1;
18900
18901 procedure P2 is
18902 begin
18903 P1;
18904 end P2;
18905
18906 begin
18907 P2;
18908 end STB;
18909 @end example
18910
18911 @example
18912 $ gnatmake stb -bargs -E
18913 $ stb
18914
18915 Execution terminated by unhandled exception
18916 Exception name: CONSTRAINT_ERROR
18917 Message: stb.adb:5
18918 Call stack traceback locations:
18919 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
18920 @end example
18921 @end quotation
18922
18923 As we see the traceback lists a sequence of addresses for the unhandled
18924 exception @code{CONSTRAINT_ERROR} raised in procedure P1. It is easy to
18925 guess that this exception come from procedure P1. To translate these
18926 addresses into the source lines where the calls appear, the
18927 @code{addr2line} tool, described below, is invaluable. The use of this tool
18928 requires the program to be compiled with debug information.
18929
18930 @quotation
18931
18932 @example
18933 $ gnatmake -g stb -bargs -E
18934 $ stb
18935
18936 Execution terminated by unhandled exception
18937 Exception name: CONSTRAINT_ERROR
18938 Message: stb.adb:5
18939 Call stack traceback locations:
18940 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
18941
18942 $ addr2line --exe=stb 0x401373 0x40138b 0x40139c 0x401335 0x4011c4
18943 0x4011f1 0x77e892a4
18944
18945 00401373 at d:/stb/stb.adb:5
18946 0040138B at d:/stb/stb.adb:10
18947 0040139C at d:/stb/stb.adb:14
18948 00401335 at d:/stb/b~stb.adb:104
18949 004011C4 at /build/.../crt1.c:200
18950 004011F1 at /build/.../crt1.c:222
18951 77E892A4 in ?? at ??:0
18952 @end example
18953 @end quotation
18954
18955 The @code{addr2line} tool has several other useful options:
18956
18957 @quotation
18958
18959
18960 @multitable {xxxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
18961 @item
18962
18963 @code{--functions}
18964
18965 @tab
18966
18967 to get the function name corresponding to any location
18968
18969 @item
18970
18971 @code{--demangle=gnat}
18972
18973 @tab
18974
18975 to use the gnat decoding mode for the function names.
18976 Note that for binutils version 2.9.x the option is
18977 simply @code{--demangle}.
18978
18979 @end multitable
18980
18981
18982 @example
18983 $ addr2line --exe=stb --functions --demangle=gnat 0x401373 0x40138b
18984 0x40139c 0x401335 0x4011c4 0x4011f1
18985
18986 00401373 in stb.p1 at d:/stb/stb.adb:5
18987 0040138B in stb.p2 at d:/stb/stb.adb:10
18988 0040139C in stb at d:/stb/stb.adb:14
18989 00401335 in main at d:/stb/b~stb.adb:104
18990 004011C4 in <__mingw_CRTStartup> at /build/.../crt1.c:200
18991 004011F1 in <mainCRTStartup> at /build/.../crt1.c:222
18992 @end example
18993 @end quotation
18994
18995 From this traceback we can see that the exception was raised in
18996 @code{stb.adb} at line 5, which was reached from a procedure call in
18997 @code{stb.adb} at line 10, and so on. The @code{b~std.adb} is the binder file,
18998 which contains the call to the main program.
18999 @ref{10e,,Running gnatbind}. The remaining entries are assorted runtime routines,
19000 and the output will vary from platform to platform.
19001
19002 It is also possible to use @code{GDB} with these traceback addresses to debug
19003 the program. For example, we can break at a given code location, as reported
19004 in the stack traceback:
19005
19006 @quotation
19007
19008 @example
19009 $ gdb -nw stb
19010 @end example
19011 @end quotation
19012
19013 Furthermore, this feature is not implemented inside Windows DLL. Only
19014 the non-symbolic traceback is reported in this case.
19015
19016 @quotation
19017
19018 @example
19019 (gdb) break *0x401373
19020 Breakpoint 1 at 0x401373: file stb.adb, line 5.
19021 @end example
19022 @end quotation
19023
19024 It is important to note that the stack traceback addresses
19025 do not change when debug information is included. This is particularly useful
19026 because it makes it possible to release software without debug information (to
19027 minimize object size), get a field report that includes a stack traceback
19028 whenever an internal bug occurs, and then be able to retrieve the sequence
19029 of calls with the same program compiled with debug information.
19030
19031 @subsubheading Tracebacks From Exception Occurrences
19032
19033
19034 Non-symbolic tracebacks are obtained by using the @code{-E} binder argument.
19035 The stack traceback is attached to the exception information string, and can
19036 be retrieved in an exception handler within the Ada program, by means of the
19037 Ada facilities defined in @code{Ada.Exceptions}. Here is a simple example:
19038
19039 @quotation
19040
19041 @example
19042 with Ada.Text_IO;
19043 with Ada.Exceptions;
19044
19045 procedure STB is
19046
19047 use Ada;
19048 use Ada.Exceptions;
19049
19050 procedure P1 is
19051 K : Positive := 1;
19052 begin
19053 K := K - 1;
19054 exception
19055 when E : others =>
19056 Text_IO.Put_Line (Exception_Information (E));
19057 end P1;
19058
19059 procedure P2 is
19060 begin
19061 P1;
19062 end P2;
19063
19064 begin
19065 P2;
19066 end STB;
19067 @end example
19068 @end quotation
19069
19070 This program will output:
19071
19072 @quotation
19073
19074 @example
19075 $ stb
19076
19077 Exception name: CONSTRAINT_ERROR
19078 Message: stb.adb:12
19079 Call stack traceback locations:
19080 0x4015e4 0x401633 0x401644 0x401461 0x4011c4 0x4011f1 0x77e892a4
19081 @end example
19082 @end quotation
19083
19084 @subsubheading Tracebacks From Anywhere in a Program
19085
19086
19087 It is also possible to retrieve a stack traceback from anywhere in a
19088 program. For this you need to
19089 use the @code{GNAT.Traceback} API. This package includes a procedure called
19090 @code{Call_Chain} that computes a complete stack traceback, as well as useful
19091 display procedures described below. It is not necessary to use the
19092 @code{-E} @code{gnatbind} option in this case, because the stack traceback mechanism
19093 is invoked explicitly.
19094
19095 In the following example we compute a traceback at a specific location in
19096 the program, and we display it using @code{GNAT.Debug_Utilities.Image} to
19097 convert addresses to strings:
19098
19099 @quotation
19100
19101 @example
19102 with Ada.Text_IO;
19103 with GNAT.Traceback;
19104 with GNAT.Debug_Utilities;
19105
19106 procedure STB is
19107
19108 use Ada;
19109 use GNAT;
19110 use GNAT.Traceback;
19111
19112 procedure P1 is
19113 TB : Tracebacks_Array (1 .. 10);
19114 -- We are asking for a maximum of 10 stack frames.
19115 Len : Natural;
19116 -- Len will receive the actual number of stack frames returned.
19117 begin
19118 Call_Chain (TB, Len);
19119
19120 Text_IO.Put ("In STB.P1 : ");
19121
19122 for K in 1 .. Len loop
19123 Text_IO.Put (Debug_Utilities.Image (TB (K)));
19124 Text_IO.Put (' ');
19125 end loop;
19126
19127 Text_IO.New_Line;
19128 end P1;
19129
19130 procedure P2 is
19131 begin
19132 P1;
19133 end P2;
19134
19135 begin
19136 P2;
19137 end STB;
19138 @end example
19139
19140 @example
19141 $ gnatmake -g stb
19142 $ stb
19143
19144 In STB.P1 : 16#0040_F1E4# 16#0040_14F2# 16#0040_170B# 16#0040_171C#
19145 16#0040_1461# 16#0040_11C4# 16#0040_11F1# 16#77E8_92A4#
19146 @end example
19147 @end quotation
19148
19149 You can then get further information by invoking the @code{addr2line}
19150 tool as described earlier (note that the hexadecimal addresses
19151 need to be specified in C format, with a leading ‘0x’).
19152
19153 @geindex traceback
19154 @geindex symbolic
19155
19156 @node Symbolic Traceback,,Non-Symbolic Traceback,Stack Traceback
19157 @anchor{gnat_ugn/gnat_and_program_execution id18}@anchor{16c}@anchor{gnat_ugn/gnat_and_program_execution symbolic-traceback}@anchor{16d}
19158 @subsubsection Symbolic Traceback
19159
19160
19161 A symbolic traceback is a stack traceback in which procedure names are
19162 associated with each code location.
19163
19164 Note that this feature is not supported on all platforms. See
19165 @code{GNAT.Traceback.Symbolic} spec in @code{g-trasym.ads} for a complete
19166 list of currently supported platforms.
19167
19168 Note that the symbolic traceback requires that the program be compiled
19169 with debug information. If it is not compiled with debug information
19170 only the non-symbolic information will be valid.
19171
19172 @subsubheading Tracebacks From Exception Occurrences
19173
19174
19175 Here is an example:
19176
19177 @quotation
19178
19179 @example
19180 with Ada.Text_IO;
19181 with GNAT.Traceback.Symbolic;
19182
19183 procedure STB is
19184
19185 procedure P1 is
19186 begin
19187 raise Constraint_Error;
19188 end P1;
19189
19190 procedure P2 is
19191 begin
19192 P1;
19193 end P2;
19194
19195 procedure P3 is
19196 begin
19197 P2;
19198 end P3;
19199
19200 begin
19201 P3;
19202 exception
19203 when E : others =>
19204 Ada.Text_IO.Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
19205 end STB;
19206 @end example
19207
19208 @example
19209 $ gnatmake -g .\stb -bargs -E
19210 $ stb
19211
19212 0040149F in stb.p1 at stb.adb:8
19213 004014B7 in stb.p2 at stb.adb:13
19214 004014CF in stb.p3 at stb.adb:18
19215 004015DD in ada.stb at stb.adb:22
19216 00401461 in main at b~stb.adb:168
19217 004011C4 in __mingw_CRTStartup at crt1.c:200
19218 004011F1 in mainCRTStartup at crt1.c:222
19219 77E892A4 in ?? at ??:0
19220 @end example
19221 @end quotation
19222
19223 In the above example the @code{.\} syntax in the @code{gnatmake} command
19224 is currently required by @code{addr2line} for files that are in
19225 the current working directory.
19226 Moreover, the exact sequence of linker options may vary from platform
19227 to platform.
19228 The above @code{-largs} section is for Windows platforms. By contrast,
19229 under Unix there is no need for the @code{-largs} section.
19230 Differences across platforms are due to details of linker implementation.
19231
19232 @subsubheading Tracebacks From Anywhere in a Program
19233
19234
19235 It is possible to get a symbolic stack traceback
19236 from anywhere in a program, just as for non-symbolic tracebacks.
19237 The first step is to obtain a non-symbolic
19238 traceback, and then call @code{Symbolic_Traceback} to compute the symbolic
19239 information. Here is an example:
19240
19241 @quotation
19242
19243 @example
19244 with Ada.Text_IO;
19245 with GNAT.Traceback;
19246 with GNAT.Traceback.Symbolic;
19247
19248 procedure STB is
19249
19250 use Ada;
19251 use GNAT.Traceback;
19252 use GNAT.Traceback.Symbolic;
19253
19254 procedure P1 is
19255 TB : Tracebacks_Array (1 .. 10);
19256 -- We are asking for a maximum of 10 stack frames.
19257 Len : Natural;
19258 -- Len will receive the actual number of stack frames returned.
19259 begin
19260 Call_Chain (TB, Len);
19261 Text_IO.Put_Line (Symbolic_Traceback (TB (1 .. Len)));
19262 end P1;
19263
19264 procedure P2 is
19265 begin
19266 P1;
19267 end P2;
19268
19269 begin
19270 P2;
19271 end STB;
19272 @end example
19273 @end quotation
19274
19275 @subsubheading Automatic Symbolic Tracebacks
19276
19277
19278 Symbolic tracebacks may also be enabled by using the -Es switch to gnatbind (as
19279 in @code{gprbuild -g ... -bargs -Es}).
19280 This will cause the Exception_Information to contain a symbolic traceback,
19281 which will also be printed if an unhandled exception terminates the
19282 program.
19283
19284 @node Pretty-Printers for the GNAT runtime,,Stack Traceback,Running and Debugging Ada Programs
19285 @anchor{gnat_ugn/gnat_and_program_execution id19}@anchor{16e}@anchor{gnat_ugn/gnat_and_program_execution pretty-printers-for-the-gnat-runtime}@anchor{16f}
19286 @subsection Pretty-Printers for the GNAT runtime
19287
19288
19289 As discussed in @cite{Calling User-Defined Subprograms}, GDB’s
19290 @code{print} command only knows about the physical layout of program data
19291 structures and therefore normally displays only low-level dumps, which
19292 are often hard to understand.
19293
19294 An example of this is when trying to display the contents of an Ada
19295 standard container, such as @code{Ada.Containers.Ordered_Maps.Map}:
19296
19297 @quotation
19298
19299 @example
19300 with Ada.Containers.Ordered_Maps;
19301
19302 procedure PP is
19303 package Int_To_Nat is
19304 new Ada.Containers.Ordered_Maps (Integer, Natural);
19305
19306 Map : Int_To_Nat.Map;
19307 begin
19308 Map.Insert (1, 10);
19309 Map.Insert (2, 20);
19310 Map.Insert (3, 30);
19311
19312 Map.Clear; -- BREAK HERE
19313 end PP;
19314 @end example
19315 @end quotation
19316
19317 When this program is built with debugging information and run under
19318 GDB up to the @code{Map.Clear} statement, trying to print @code{Map} will
19319 yield information that is only relevant to the developers of our standard
19320 containers:
19321
19322 @quotation
19323
19324 @example
19325 (gdb) print map
19326 $1 = (
19327 tree => (
19328 first => 0x64e010,
19329 last => 0x64e070,
19330 root => 0x64e040,
19331 length => 3,
19332 tc => (
19333 busy => 0,
19334 lock => 0
19335 )
19336 )
19337 )
19338 @end example
19339 @end quotation
19340
19341 Fortunately, GDB has a feature called pretty-printers@footnote{http://docs.adacore.com/gdb-docs/html/gdb.html#Pretty_002dPrinter-Introduction},
19342 which allows customizing how GDB displays data structures. The GDB
19343 shipped with GNAT embeds such pretty-printers for the most common
19344 containers in the standard library. To enable them, either run the
19345 following command manually under GDB or add it to your @code{.gdbinit} file:
19346
19347 @quotation
19348
19349 @example
19350 python import gnatdbg; gnatdbg.setup()
19351 @end example
19352 @end quotation
19353
19354 Once this is done, GDB’s @code{print} command will automatically use
19355 these pretty-printers when appropriate. Using the previous example:
19356
19357 @quotation
19358
19359 @example
19360 (gdb) print map
19361 $1 = pp.int_to_nat.map of length 3 = @{
19362 [1] = 10,
19363 [2] = 20,
19364 [3] = 30
19365 @}
19366 @end example
19367 @end quotation
19368
19369 Pretty-printers are invoked each time GDB tries to display a value,
19370 including when displaying the arguments of a called subprogram (in
19371 GDB’s @code{backtrace} command) or when printing the value returned by a
19372 function (in GDB’s @code{finish} command).
19373
19374 To display a value without involving pretty-printers, @code{print} can be
19375 invoked with its @code{/r} option:
19376
19377 @quotation
19378
19379 @example
19380 (gdb) print/r map
19381 $1 = (
19382 tree => (...
19383 @end example
19384 @end quotation
19385
19386 Finer control of pretty-printers is also possible: see GDB's online documentation@footnote{http://docs.adacore.com/gdb-docs/html/gdb.html#Pretty_002dPrinter-Commands}
19387 for more information.
19388
19389 @geindex Profiling
19390
19391 @node Profiling,Improving Performance,Running and Debugging Ada Programs,GNAT and Program Execution
19392 @anchor{gnat_ugn/gnat_and_program_execution id20}@anchor{170}@anchor{gnat_ugn/gnat_and_program_execution profiling}@anchor{147}
19393 @section Profiling
19394
19395
19396 This section describes how to use the @code{gprof} profiler tool on Ada programs.
19397
19398 @geindex gprof
19399
19400 @geindex Profiling
19401
19402 @menu
19403 * Profiling an Ada Program with gprof::
19404
19405 @end menu
19406
19407 @node Profiling an Ada Program with gprof,,,Profiling
19408 @anchor{gnat_ugn/gnat_and_program_execution id21}@anchor{171}@anchor{gnat_ugn/gnat_and_program_execution profiling-an-ada-program-with-gprof}@anchor{172}
19409 @subsection Profiling an Ada Program with gprof
19410
19411
19412 This section is not meant to be an exhaustive documentation of @code{gprof}.
19413 Full documentation for it can be found in the @cite{GNU Profiler User’s Guide}
19414 documentation that is part of this GNAT distribution.
19415
19416 Profiling a program helps determine the parts of a program that are executed
19417 most often, and are therefore the most time-consuming.
19418
19419 @code{gprof} is the standard GNU profiling tool; it has been enhanced to
19420 better handle Ada programs and multitasking.
19421 It is currently supported on the following platforms
19422
19423
19424 @itemize *
19425
19426 @item
19427 linux x86/x86_64
19428
19429 @item
19430 windows x86
19431 @end itemize
19432
19433 In order to profile a program using @code{gprof}, several steps are needed:
19434
19435
19436 @enumerate
19437
19438 @item
19439 Instrument the code, which requires a full recompilation of the project with the
19440 proper switches.
19441
19442 @item
19443 Execute the program under the analysis conditions, i.e. with the desired
19444 input.
19445
19446 @item
19447 Analyze the results using the @code{gprof} tool.
19448 @end enumerate
19449
19450 The following sections detail the different steps, and indicate how
19451 to interpret the results.
19452
19453 @menu
19454 * Compilation for profiling::
19455 * Program execution::
19456 * Running gprof::
19457 * Interpretation of profiling results::
19458
19459 @end menu
19460
19461 @node Compilation for profiling,Program execution,,Profiling an Ada Program with gprof
19462 @anchor{gnat_ugn/gnat_and_program_execution compilation-for-profiling}@anchor{173}@anchor{gnat_ugn/gnat_and_program_execution id22}@anchor{174}
19463 @subsubsection Compilation for profiling
19464
19465
19466 @geindex -pg (gcc)
19467 @geindex for profiling
19468
19469 @geindex -pg (gnatlink)
19470 @geindex for profiling
19471
19472 In order to profile a program the first step is to tell the compiler
19473 to generate the necessary profiling information. The compiler switch to be used
19474 is @code{-pg}, which must be added to other compilation switches. This
19475 switch needs to be specified both during compilation and link stages, and can
19476 be specified once when using gnatmake:
19477
19478 @quotation
19479
19480 @example
19481 $ gnatmake -f -pg -P my_project
19482 @end example
19483 @end quotation
19484
19485 Note that only the objects that were compiled with the @code{-pg} switch will
19486 be profiled; if you need to profile your whole project, use the @code{-f}
19487 gnatmake switch to force full recompilation.
19488
19489 @node Program execution,Running gprof,Compilation for profiling,Profiling an Ada Program with gprof
19490 @anchor{gnat_ugn/gnat_and_program_execution id23}@anchor{175}@anchor{gnat_ugn/gnat_and_program_execution program-execution}@anchor{176}
19491 @subsubsection Program execution
19492
19493
19494 Once the program has been compiled for profiling, you can run it as usual.
19495
19496 The only constraint imposed by profiling is that the program must terminate
19497 normally. An interrupted program (via a Ctrl-C, kill, etc.) will not be
19498 properly analyzed.
19499
19500 Once the program completes execution, a data file called @code{gmon.out} is
19501 generated in the directory where the program was launched from. If this file
19502 already exists, it will be overwritten.
19503
19504 @node Running gprof,Interpretation of profiling results,Program execution,Profiling an Ada Program with gprof
19505 @anchor{gnat_ugn/gnat_and_program_execution id24}@anchor{177}@anchor{gnat_ugn/gnat_and_program_execution running-gprof}@anchor{178}
19506 @subsubsection Running gprof
19507
19508
19509 The @code{gprof} tool is called as follow:
19510
19511 @quotation
19512
19513 @example
19514 $ gprof my_prog gmon.out
19515 @end example
19516 @end quotation
19517
19518 or simply:
19519
19520 @quotation
19521
19522 @example
19523 $ gprof my_prog
19524 @end example
19525 @end quotation
19526
19527 The complete form of the gprof command line is the following:
19528
19529 @quotation
19530
19531 @example
19532 $ gprof [switches] [executable [data-file]]
19533 @end example
19534 @end quotation
19535
19536 @code{gprof} supports numerous switches. The order of these
19537 switch does not matter. The full list of options can be found in
19538 the GNU Profiler User’s Guide documentation that comes with this documentation.
19539
19540 The following is the subset of those switches that is most relevant:
19541
19542 @geindex --demangle (gprof)
19543
19544
19545 @table @asis
19546
19547 @item @code{--demangle[=@emph{style}]}, @code{--no-demangle}
19548
19549 These options control whether symbol names should be demangled when
19550 printing output. The default is to demangle C++ symbols. The
19551 @code{--no-demangle} option may be used to turn off demangling. Different
19552 compilers have different mangling styles. The optional demangling style
19553 argument can be used to choose an appropriate demangling style for your
19554 compiler, in particular Ada symbols generated by GNAT can be demangled using
19555 @code{--demangle=gnat}.
19556 @end table
19557
19558 @geindex -e (gprof)
19559
19560
19561 @table @asis
19562
19563 @item @code{-e @emph{function_name}}
19564
19565 The @code{-e @emph{function}} option tells @code{gprof} not to print
19566 information about the function @code{function_name} (and its
19567 children…) in the call graph. The function will still be listed
19568 as a child of any functions that call it, but its index number will be
19569 shown as @code{[not printed]}. More than one @code{-e} option may be
19570 given; only one @code{function_name} may be indicated with each @code{-e}
19571 option.
19572 @end table
19573
19574 @geindex -E (gprof)
19575
19576
19577 @table @asis
19578
19579 @item @code{-E @emph{function_name}}
19580
19581 The @code{-E @emph{function}} option works like the @code{-e} option, but
19582 execution time spent in the function (and children who were not called from
19583 anywhere else), will not be used to compute the percentages-of-time for
19584 the call graph. More than one @code{-E} option may be given; only one
19585 @code{function_name} may be indicated with each @code{-E`} option.
19586 @end table
19587
19588 @geindex -f (gprof)
19589
19590
19591 @table @asis
19592
19593 @item @code{-f @emph{function_name}}
19594
19595 The @code{-f @emph{function}} option causes @code{gprof} to limit the
19596 call graph to the function @code{function_name} and its children (and
19597 their children…). More than one @code{-f} option may be given;
19598 only one @code{function_name} may be indicated with each @code{-f}
19599 option.
19600 @end table
19601
19602 @geindex -F (gprof)
19603
19604
19605 @table @asis
19606
19607 @item @code{-F @emph{function_name}}
19608
19609 The @code{-F @emph{function}} option works like the @code{-f} option, but
19610 only time spent in the function and its children (and their
19611 children…) will be used to determine total-time and
19612 percentages-of-time for the call graph. More than one @code{-F} option
19613 may be given; only one @code{function_name} may be indicated with each
19614 @code{-F} option. The @code{-F} option overrides the @code{-E} option.
19615 @end table
19616
19617 @node Interpretation of profiling results,,Running gprof,Profiling an Ada Program with gprof
19618 @anchor{gnat_ugn/gnat_and_program_execution id25}@anchor{179}@anchor{gnat_ugn/gnat_and_program_execution interpretation-of-profiling-results}@anchor{17a}
19619 @subsubsection Interpretation of profiling results
19620
19621
19622 The results of the profiling analysis are represented by two arrays: the
19623 ‘flat profile’ and the ‘call graph’. Full documentation of those outputs
19624 can be found in the GNU Profiler User’s Guide.
19625
19626 The flat profile shows the time spent in each function of the program, and how
19627 many time it has been called. This allows you to locate easily the most
19628 time-consuming functions.
19629
19630 The call graph shows, for each subprogram, the subprograms that call it,
19631 and the subprograms that it calls. It also provides an estimate of the time
19632 spent in each of those callers/called subprograms.
19633
19634 @node Improving Performance,Overflow Check Handling in GNAT,Profiling,GNAT and Program Execution
19635 @anchor{gnat_ugn/gnat_and_program_execution id26}@anchor{148}@anchor{gnat_ugn/gnat_and_program_execution improving-performance}@anchor{17b}
19636 @section Improving Performance
19637
19638
19639 @geindex Improving performance
19640
19641 This section presents several topics related to program performance.
19642 It first describes some of the tradeoffs that need to be considered
19643 and some of the techniques for making your program run faster.
19644
19645 It then documents the unused subprogram/data elimination feature,
19646 which can reduce the size of program executables.
19647
19648 @menu
19649 * Performance Considerations::
19650 * Text_IO Suggestions::
19651 * Reducing Size of Executables with Unused Subprogram/Data Elimination::
19652
19653 @end menu
19654
19655 @node Performance Considerations,Text_IO Suggestions,,Improving Performance
19656 @anchor{gnat_ugn/gnat_and_program_execution id27}@anchor{17c}@anchor{gnat_ugn/gnat_and_program_execution performance-considerations}@anchor{17d}
19657 @subsection Performance Considerations
19658
19659
19660 The GNAT system provides a number of options that allow a trade-off
19661 between
19662
19663
19664 @itemize *
19665
19666 @item
19667 performance of the generated code
19668
19669 @item
19670 speed of compilation
19671
19672 @item
19673 minimization of dependences and recompilation
19674
19675 @item
19676 the degree of run-time checking.
19677 @end itemize
19678
19679 The defaults (if no options are selected) aim at improving the speed
19680 of compilation and minimizing dependences, at the expense of performance
19681 of the generated code:
19682
19683
19684 @itemize *
19685
19686 @item
19687 no optimization
19688
19689 @item
19690 no inlining of subprogram calls
19691
19692 @item
19693 all run-time checks enabled except overflow and elaboration checks
19694 @end itemize
19695
19696 These options are suitable for most program development purposes. This
19697 section describes how you can modify these choices, and also provides
19698 some guidelines on debugging optimized code.
19699
19700 @menu
19701 * Controlling Run-Time Checks::
19702 * Use of Restrictions::
19703 * Optimization Levels::
19704 * Debugging Optimized Code::
19705 * Inlining of Subprograms::
19706 * Floating Point Operations::
19707 * Vectorization of loops::
19708 * Other Optimization Switches::
19709 * Optimization and Strict Aliasing::
19710 * Aliased Variables and Optimization::
19711 * Atomic Variables and Optimization::
19712 * Passive Task Optimization::
19713
19714 @end menu
19715
19716 @node Controlling Run-Time Checks,Use of Restrictions,,Performance Considerations
19717 @anchor{gnat_ugn/gnat_and_program_execution controlling-run-time-checks}@anchor{17e}@anchor{gnat_ugn/gnat_and_program_execution id28}@anchor{17f}
19718 @subsubsection Controlling Run-Time Checks
19719
19720
19721 By default, GNAT generates all run-time checks, except stack overflow
19722 checks, and checks for access before elaboration on subprogram
19723 calls. The latter are not required in default mode, because all
19724 necessary checking is done at compile time.
19725
19726 @geindex -gnatp (gcc)
19727
19728 @geindex -gnato (gcc)
19729
19730 The gnat switch, @code{-gnatp} allows this default to be modified. See
19731 @ref{ea,,Run-Time Checks}.
19732
19733 Our experience is that the default is suitable for most development
19734 purposes.
19735
19736 Elaboration checks are off by default, and also not needed by default, since
19737 GNAT uses a static elaboration analysis approach that avoids the need for
19738 run-time checking. This manual contains a full chapter discussing the issue
19739 of elaboration checks, and if the default is not satisfactory for your use,
19740 you should read this chapter.
19741
19742 For validity checks, the minimal checks required by the Ada Reference
19743 Manual (for case statements and assignments to array elements) are on
19744 by default. These can be suppressed by use of the @code{-gnatVn} switch.
19745 Note that in Ada 83, there were no validity checks, so if the Ada 83 mode
19746 is acceptable (or when comparing GNAT performance with an Ada 83 compiler),
19747 it may be reasonable to routinely use @code{-gnatVn}. Validity checks
19748 are also suppressed entirely if @code{-gnatp} is used.
19749
19750 @geindex Overflow checks
19751
19752 @geindex Checks
19753 @geindex overflow
19754
19755 @geindex Suppress
19756
19757 @geindex Unsuppress
19758
19759 @geindex pragma Suppress
19760
19761 @geindex pragma Unsuppress
19762
19763 Note that the setting of the switches controls the default setting of
19764 the checks. They may be modified using either @code{pragma Suppress} (to
19765 remove checks) or @code{pragma Unsuppress} (to add back suppressed
19766 checks) in the program source.
19767
19768 @node Use of Restrictions,Optimization Levels,Controlling Run-Time Checks,Performance Considerations
19769 @anchor{gnat_ugn/gnat_and_program_execution id29}@anchor{180}@anchor{gnat_ugn/gnat_and_program_execution use-of-restrictions}@anchor{181}
19770 @subsubsection Use of Restrictions
19771
19772
19773 The use of pragma Restrictions allows you to control which features are
19774 permitted in your program. Apart from the obvious point that if you avoid
19775 relatively expensive features like finalization (enforceable by the use
19776 of pragma Restrictions (No_Finalization), the use of this pragma does not
19777 affect the generated code in most cases.
19778
19779 One notable exception to this rule is that the possibility of task abort
19780 results in some distributed overhead, particularly if finalization or
19781 exception handlers are used. The reason is that certain sections of code
19782 have to be marked as non-abortable.
19783
19784 If you use neither the @code{abort} statement, nor asynchronous transfer
19785 of control (@code{select ... then abort}), then this distributed overhead
19786 is removed, which may have a general positive effect in improving
19787 overall performance. Especially code involving frequent use of tasking
19788 constructs and controlled types will show much improved performance.
19789 The relevant restrictions pragmas are
19790
19791 @quotation
19792
19793 @example
19794 pragma Restrictions (No_Abort_Statements);
19795 pragma Restrictions (Max_Asynchronous_Select_Nesting => 0);
19796 @end example
19797 @end quotation
19798
19799 It is recommended that these restriction pragmas be used if possible. Note
19800 that this also means that you can write code without worrying about the
19801 possibility of an immediate abort at any point.
19802
19803 @node Optimization Levels,Debugging Optimized Code,Use of Restrictions,Performance Considerations
19804 @anchor{gnat_ugn/gnat_and_program_execution id30}@anchor{182}@anchor{gnat_ugn/gnat_and_program_execution optimization-levels}@anchor{ed}
19805 @subsubsection Optimization Levels
19806
19807
19808 @geindex -O (gcc)
19809
19810 Without any optimization option,
19811 the compiler’s goal is to reduce the cost of
19812 compilation and to make debugging produce the expected results.
19813 Statements are independent: if you stop the program with a breakpoint between
19814 statements, you can then assign a new value to any variable or change
19815 the program counter to any other statement in the subprogram and get exactly
19816 the results you would expect from the source code.
19817
19818 Turning on optimization makes the compiler attempt to improve the
19819 performance and/or code size at the expense of compilation time and
19820 possibly the ability to debug the program.
19821
19822 If you use multiple
19823 -O options, with or without level numbers,
19824 the last such option is the one that is effective.
19825
19826 The default is optimization off. This results in the fastest compile
19827 times, but GNAT makes absolutely no attempt to optimize, and the
19828 generated programs are considerably larger and slower than when
19829 optimization is enabled. You can use the
19830 @code{-O} switch (the permitted forms are @code{-O0}, @code{-O1}
19831 @code{-O2}, @code{-O3}, and @code{-Os})
19832 to @code{gcc} to control the optimization level:
19833
19834
19835 @itemize *
19836
19837 @item
19838
19839 @table @asis
19840
19841 @item @code{-O0}
19842
19843 No optimization (the default);
19844 generates unoptimized code but has
19845 the fastest compilation time.
19846
19847 Note that many other compilers do substantial optimization even
19848 if ‘no optimization’ is specified. With gcc, it is very unusual
19849 to use @code{-O0} for production if execution time is of any concern,
19850 since @code{-O0} means (almost) no optimization. This difference
19851 between gcc and other compilers should be kept in mind when
19852 doing performance comparisons.
19853 @end table
19854
19855 @item
19856
19857 @table @asis
19858
19859 @item @code{-O1}
19860
19861 Moderate optimization;
19862 optimizes reasonably well but does not
19863 degrade compilation time significantly.
19864 @end table
19865
19866 @item
19867
19868 @table @asis
19869
19870 @item @code{-O2}
19871
19872 Full optimization;
19873 generates highly optimized code and has
19874 the slowest compilation time.
19875 @end table
19876
19877 @item
19878
19879 @table @asis
19880
19881 @item @code{-O3}
19882
19883 Full optimization as in @code{-O2};
19884 also uses more aggressive automatic inlining of subprograms within a unit
19885 (@ref{100,,Inlining of Subprograms}) and attempts to vectorize loops.
19886 @end table
19887
19888 @item
19889
19890 @table @asis
19891
19892 @item @code{-Os}
19893
19894 Optimize space usage (code and data) of resulting program.
19895 @end table
19896 @end itemize
19897
19898 Higher optimization levels perform more global transformations on the
19899 program and apply more expensive analysis algorithms in order to generate
19900 faster and more compact code. The price in compilation time, and the
19901 resulting improvement in execution time,
19902 both depend on the particular application and the hardware environment.
19903 You should experiment to find the best level for your application.
19904
19905 Since the precise set of optimizations done at each level will vary from
19906 release to release (and sometime from target to target), it is best to think
19907 of the optimization settings in general terms.
19908 See the @emph{Options That Control Optimization} section in
19909 @cite{Using the GNU Compiler Collection (GCC)}
19910 for details about
19911 the @code{-O} settings and a number of @code{-f} options that
19912 individually enable or disable specific optimizations.
19913
19914 Unlike some other compilation systems, @code{gcc} has
19915 been tested extensively at all optimization levels. There are some bugs
19916 which appear only with optimization turned on, but there have also been
19917 bugs which show up only in @emph{unoptimized} code. Selecting a lower
19918 level of optimization does not improve the reliability of the code
19919 generator, which in practice is highly reliable at all optimization
19920 levels.
19921
19922 Note regarding the use of @code{-O3}: The use of this optimization level
19923 ought not to be automatically preferred over that of level @code{-O2},
19924 since it often results in larger executables which may run more slowly.
19925 See further discussion of this point in @ref{100,,Inlining of Subprograms}.
19926
19927 @node Debugging Optimized Code,Inlining of Subprograms,Optimization Levels,Performance Considerations
19928 @anchor{gnat_ugn/gnat_and_program_execution debugging-optimized-code}@anchor{183}@anchor{gnat_ugn/gnat_and_program_execution id31}@anchor{184}
19929 @subsubsection Debugging Optimized Code
19930
19931
19932 @geindex Debugging optimized code
19933
19934 @geindex Optimization and debugging
19935
19936 Although it is possible to do a reasonable amount of debugging at
19937 nonzero optimization levels,
19938 the higher the level the more likely that
19939 source-level constructs will have been eliminated by optimization.
19940 For example, if a loop is strength-reduced, the loop
19941 control variable may be completely eliminated and thus cannot be
19942 displayed in the debugger.
19943 This can only happen at @code{-O2} or @code{-O3}.
19944 Explicit temporary variables that you code might be eliminated at
19945 level @code{-O1} or higher.
19946
19947 @geindex -g (gcc)
19948
19949 The use of the @code{-g} switch,
19950 which is needed for source-level debugging,
19951 affects the size of the program executable on disk,
19952 and indeed the debugging information can be quite large.
19953 However, it has no effect on the generated code (and thus does not
19954 degrade performance)
19955
19956 Since the compiler generates debugging tables for a compilation unit before
19957 it performs optimizations, the optimizing transformations may invalidate some
19958 of the debugging data. You therefore need to anticipate certain
19959 anomalous situations that may arise while debugging optimized code.
19960 These are the most common cases:
19961
19962
19963 @itemize *
19964
19965 @item
19966 @emph{The ‘hopping Program Counter’:} Repeated @code{step} or @code{next}
19967 commands show
19968 the PC bouncing back and forth in the code. This may result from any of
19969 the following optimizations:
19970
19971
19972 @itemize -
19973
19974 @item
19975 @emph{Common subexpression elimination:} using a single instance of code for a
19976 quantity that the source computes several times. As a result you
19977 may not be able to stop on what looks like a statement.
19978
19979 @item
19980 @emph{Invariant code motion:} moving an expression that does not change within a
19981 loop, to the beginning of the loop.
19982
19983 @item
19984 @emph{Instruction scheduling:} moving instructions so as to
19985 overlap loads and stores (typically) with other code, or in
19986 general to move computations of values closer to their uses. Often
19987 this causes you to pass an assignment statement without the assignment
19988 happening and then later bounce back to the statement when the
19989 value is actually needed. Placing a breakpoint on a line of code
19990 and then stepping over it may, therefore, not always cause all the
19991 expected side-effects.
19992 @end itemize
19993
19994 @item
19995 @emph{The ‘big leap’:} More commonly known as @emph{cross-jumping}, in which
19996 two identical pieces of code are merged and the program counter suddenly
19997 jumps to a statement that is not supposed to be executed, simply because
19998 it (and the code following) translates to the same thing as the code
19999 that @emph{was} supposed to be executed. This effect is typically seen in
20000 sequences that end in a jump, such as a @code{goto}, a @code{return}, or
20001 a @code{break} in a C @code{switch} statement.
20002
20003 @item
20004 @emph{The ‘roving variable’:} The symptom is an unexpected value in a variable.
20005 There are various reasons for this effect:
20006
20007
20008 @itemize -
20009
20010 @item
20011 In a subprogram prologue, a parameter may not yet have been moved to its
20012 ‘home’.
20013
20014 @item
20015 A variable may be dead, and its register re-used. This is
20016 probably the most common cause.
20017
20018 @item
20019 As mentioned above, the assignment of a value to a variable may
20020 have been moved.
20021
20022 @item
20023 A variable may be eliminated entirely by value propagation or
20024 other means. In this case, GCC may incorrectly generate debugging
20025 information for the variable
20026 @end itemize
20027
20028 In general, when an unexpected value appears for a local variable or parameter
20029 you should first ascertain if that value was actually computed by
20030 your program, as opposed to being incorrectly reported by the debugger.
20031 Record fields or
20032 array elements in an object designated by an access value
20033 are generally less of a problem, once you have ascertained that the access
20034 value is sensible.
20035 Typically, this means checking variables in the preceding code and in the
20036 calling subprogram to verify that the value observed is explainable from other
20037 values (one must apply the procedure recursively to those
20038 other values); or re-running the code and stopping a little earlier
20039 (perhaps before the call) and stepping to better see how the variable obtained
20040 the value in question; or continuing to step @emph{from} the point of the
20041 strange value to see if code motion had simply moved the variable’s
20042 assignments later.
20043 @end itemize
20044
20045 In light of such anomalies, a recommended technique is to use @code{-O0}
20046 early in the software development cycle, when extensive debugging capabilities
20047 are most needed, and then move to @code{-O1} and later @code{-O2} as
20048 the debugger becomes less critical.
20049 Whether to use the @code{-g} switch in the release version is
20050 a release management issue.
20051 Note that if you use @code{-g} you can then use the @code{strip} program
20052 on the resulting executable,
20053 which removes both debugging information and global symbols.
20054
20055 @node Inlining of Subprograms,Floating Point Operations,Debugging Optimized Code,Performance Considerations
20056 @anchor{gnat_ugn/gnat_and_program_execution id32}@anchor{185}@anchor{gnat_ugn/gnat_and_program_execution inlining-of-subprograms}@anchor{100}
20057 @subsubsection Inlining of Subprograms
20058
20059
20060 A call to a subprogram in the current unit is inlined if all the
20061 following conditions are met:
20062
20063
20064 @itemize *
20065
20066 @item
20067 The optimization level is at least @code{-O1}.
20068
20069 @item
20070 The called subprogram is suitable for inlining: It must be small enough
20071 and not contain something that @code{gcc} cannot support in inlined
20072 subprograms.
20073
20074 @geindex pragma Inline
20075
20076 @geindex Inline
20077
20078 @item
20079 Any one of the following applies: @code{pragma Inline} is applied to the
20080 subprogram; the subprogram is local to the unit and called once from
20081 within it; the subprogram is small and optimization level @code{-O2} is
20082 specified; optimization level @code{-O3} is specified.
20083 @end itemize
20084
20085 Calls to subprograms in @emph{with}ed units are normally not inlined.
20086 To achieve actual inlining (that is, replacement of the call by the code
20087 in the body of the subprogram), the following conditions must all be true:
20088
20089
20090 @itemize *
20091
20092 @item
20093 The optimization level is at least @code{-O1}.
20094
20095 @item
20096 The called subprogram is suitable for inlining: It must be small enough
20097 and not contain something that @code{gcc} cannot support in inlined
20098 subprograms.
20099
20100 @item
20101 There is a @code{pragma Inline} for the subprogram.
20102
20103 @item
20104 The @code{-gnatn} switch is used on the command line.
20105 @end itemize
20106
20107 Even if all these conditions are met, it may not be possible for
20108 the compiler to inline the call, due to the length of the body,
20109 or features in the body that make it impossible for the compiler
20110 to do the inlining.
20111
20112 Note that specifying the @code{-gnatn} switch causes additional
20113 compilation dependencies. Consider the following:
20114
20115 @quotation
20116
20117 @example
20118 package R is
20119 procedure Q;
20120 pragma Inline (Q);
20121 end R;
20122 package body R is
20123 ...
20124 end R;
20125
20126 with R;
20127 procedure Main is
20128 begin
20129 ...
20130 R.Q;
20131 end Main;
20132 @end example
20133 @end quotation
20134
20135 With the default behavior (no @code{-gnatn} switch specified), the
20136 compilation of the @code{Main} procedure depends only on its own source,
20137 @code{main.adb}, and the spec of the package in file @code{r.ads}. This
20138 means that editing the body of @code{R} does not require recompiling
20139 @code{Main}.
20140
20141 On the other hand, the call @code{R.Q} is not inlined under these
20142 circumstances. If the @code{-gnatn} switch is present when @code{Main}
20143 is compiled, the call will be inlined if the body of @code{Q} is small
20144 enough, but now @code{Main} depends on the body of @code{R} in
20145 @code{r.adb} as well as on the spec. This means that if this body is edited,
20146 the main program must be recompiled. Note that this extra dependency
20147 occurs whether or not the call is in fact inlined by @code{gcc}.
20148
20149 The use of front end inlining with @code{-gnatN} generates similar
20150 additional dependencies.
20151
20152 @geindex -fno-inline (gcc)
20153
20154 Note: The @code{-fno-inline} switch overrides all other conditions and ensures that
20155 no inlining occurs, unless requested with pragma Inline_Always for @code{gcc}
20156 back-ends. The extra dependences resulting from @code{-gnatn} will still be active,
20157 even if this switch is used to suppress the resulting inlining actions.
20158
20159 @geindex -fno-inline-functions (gcc)
20160
20161 Note: The @code{-fno-inline-functions} switch can be used to prevent
20162 automatic inlining of subprograms if @code{-O3} is used.
20163
20164 @geindex -fno-inline-small-functions (gcc)
20165
20166 Note: The @code{-fno-inline-small-functions} switch can be used to prevent
20167 automatic inlining of small subprograms if @code{-O2} is used.
20168
20169 @geindex -fno-inline-functions-called-once (gcc)
20170
20171 Note: The @code{-fno-inline-functions-called-once} switch
20172 can be used to prevent inlining of subprograms local to the unit
20173 and called once from within it if @code{-O1} is used.
20174
20175 Note regarding the use of @code{-O3}: @code{-gnatn} is made up of two
20176 sub-switches @code{-gnatn1} and @code{-gnatn2} that can be directly
20177 specified in lieu of it, @code{-gnatn} being translated into one of them
20178 based on the optimization level. With @code{-O2} or below, @code{-gnatn}
20179 is equivalent to @code{-gnatn1} which activates pragma @code{Inline} with
20180 moderate inlining across modules. With @code{-O3}, @code{-gnatn} is
20181 equivalent to @code{-gnatn2} which activates pragma @code{Inline} with
20182 full inlining across modules. If you have used pragma @code{Inline} in
20183 appropriate cases, then it is usually much better to use @code{-O2}
20184 and @code{-gnatn} and avoid the use of @code{-O3} which has the additional
20185 effect of inlining subprograms you did not think should be inlined. We have
20186 found that the use of @code{-O3} may slow down the compilation and increase
20187 the code size by performing excessive inlining, leading to increased
20188 instruction cache pressure from the increased code size and thus minor
20189 performance improvements. So the bottom line here is that you should not
20190 automatically assume that @code{-O3} is better than @code{-O2}, and
20191 indeed you should use @code{-O3} only if tests show that it actually
20192 improves performance for your program.
20193
20194 @node Floating Point Operations,Vectorization of loops,Inlining of Subprograms,Performance Considerations
20195 @anchor{gnat_ugn/gnat_and_program_execution floating-point-operations}@anchor{186}@anchor{gnat_ugn/gnat_and_program_execution id33}@anchor{187}
20196 @subsubsection Floating Point Operations
20197
20198
20199 @geindex Floating-Point Operations
20200
20201 On almost all targets, GNAT maps Float and Long_Float to the 32-bit and
20202 64-bit standard IEEE floating-point representations, and operations will
20203 use standard IEEE arithmetic as provided by the processor. On most, but
20204 not all, architectures, the attribute Machine_Overflows is False for these
20205 types, meaning that the semantics of overflow is implementation-defined.
20206 In the case of GNAT, these semantics correspond to the normal IEEE
20207 treatment of infinities and NaN (not a number) values. For example,
20208 1.0 / 0.0 yields plus infinitiy and 0.0 / 0.0 yields a NaN. By
20209 avoiding explicit overflow checks, the performance is greatly improved
20210 on many targets. However, if required, floating-point overflow can be
20211 enabled by the use of the pragma Check_Float_Overflow.
20212
20213 Another consideration that applies specifically to x86 32-bit
20214 architectures is which form of floating-point arithmetic is used.
20215 By default the operations use the old style x86 floating-point,
20216 which implements an 80-bit extended precision form (on these
20217 architectures the type Long_Long_Float corresponds to that form).
20218 In addition, generation of efficient code in this mode means that
20219 the extended precision form will be used for intermediate results.
20220 This may be helpful in improving the final precision of a complex
20221 expression. However it means that the results obtained on the x86
20222 will be different from those on other architectures, and for some
20223 algorithms, the extra intermediate precision can be detrimental.
20224
20225 In addition to this old-style floating-point, all modern x86 chips
20226 implement an alternative floating-point operation model referred
20227 to as SSE2. In this model there is no extended form, and furthermore
20228 execution performance is significantly enhanced. To force GNAT to use
20229 this more modern form, use both of the switches:
20230
20231 @quotation
20232
20233 -msse2 -mfpmath=sse
20234 @end quotation
20235
20236 A unit compiled with these switches will automatically use the more
20237 efficient SSE2 instruction set for Float and Long_Float operations.
20238 Note that the ABI has the same form for both floating-point models,
20239 so it is permissible to mix units compiled with and without these
20240 switches.
20241
20242 @node Vectorization of loops,Other Optimization Switches,Floating Point Operations,Performance Considerations
20243 @anchor{gnat_ugn/gnat_and_program_execution id34}@anchor{188}@anchor{gnat_ugn/gnat_and_program_execution vectorization-of-loops}@anchor{189}
20244 @subsubsection Vectorization of loops
20245
20246
20247 @geindex Optimization Switches
20248
20249 You can take advantage of the auto-vectorizer present in the @code{gcc}
20250 back end to vectorize loops with GNAT. The corresponding command line switch
20251 is @code{-ftree-vectorize} but, as it is enabled by default at @code{-O3}
20252 and other aggressive optimizations helpful for vectorization also are enabled
20253 by default at this level, using @code{-O3} directly is recommended.
20254
20255 You also need to make sure that the target architecture features a supported
20256 SIMD instruction set. For example, for the x86 architecture, you should at
20257 least specify @code{-msse2} to get significant vectorization (but you don’t
20258 need to specify it for x86-64 as it is part of the base 64-bit architecture).
20259 Similarly, for the PowerPC architecture, you should specify @code{-maltivec}.
20260
20261 The preferred loop form for vectorization is the @code{for} iteration scheme.
20262 Loops with a @code{while} iteration scheme can also be vectorized if they are
20263 very simple, but the vectorizer will quickly give up otherwise. With either
20264 iteration scheme, the flow of control must be straight, in particular no
20265 @code{exit} statement may appear in the loop body. The loop may however
20266 contain a single nested loop, if it can be vectorized when considered alone:
20267
20268 @quotation
20269
20270 @example
20271 A : array (1..4, 1..4) of Long_Float;
20272 S : array (1..4) of Long_Float;
20273
20274 procedure Sum is
20275 begin
20276 for I in A'Range(1) loop
20277 for J in A'Range(2) loop
20278 S (I) := S (I) + A (I, J);
20279 end loop;
20280 end loop;
20281 end Sum;
20282 @end example
20283 @end quotation
20284
20285 The vectorizable operations depend on the targeted SIMD instruction set, but
20286 the adding and some of the multiplying operators are generally supported, as
20287 well as the logical operators for modular types. Note that compiling
20288 with @code{-gnatp} might well reveal cases where some checks do thwart
20289 vectorization.
20290
20291 Type conversions may also prevent vectorization if they involve semantics that
20292 are not directly supported by the code generator or the SIMD instruction set.
20293 A typical example is direct conversion from floating-point to integer types.
20294 The solution in this case is to use the following idiom:
20295
20296 @quotation
20297
20298 @example
20299 Integer (S'Truncation (F))
20300 @end example
20301 @end quotation
20302
20303 if @code{S} is the subtype of floating-point object @code{F}.
20304
20305 In most cases, the vectorizable loops are loops that iterate over arrays.
20306 All kinds of array types are supported, i.e. constrained array types with
20307 static bounds:
20308
20309 @quotation
20310
20311 @example
20312 type Array_Type is array (1 .. 4) of Long_Float;
20313 @end example
20314 @end quotation
20315
20316 constrained array types with dynamic bounds:
20317
20318 @quotation
20319
20320 @example
20321 type Array_Type is array (1 .. Q.N) of Long_Float;
20322
20323 type Array_Type is array (Q.K .. 4) of Long_Float;
20324
20325 type Array_Type is array (Q.K .. Q.N) of Long_Float;
20326 @end example
20327 @end quotation
20328
20329 or unconstrained array types:
20330
20331 @quotation
20332
20333 @example
20334 type Array_Type is array (Positive range <>) of Long_Float;
20335 @end example
20336 @end quotation
20337
20338 The quality of the generated code decreases when the dynamic aspect of the
20339 array type increases, the worst code being generated for unconstrained array
20340 types. This is so because, the less information the compiler has about the
20341 bounds of the array, the more fallback code it needs to generate in order to
20342 fix things up at run time.
20343
20344 It is possible to specify that a given loop should be subject to vectorization
20345 preferably to other optimizations by means of pragma @code{Loop_Optimize}:
20346
20347 @quotation
20348
20349 @example
20350 pragma Loop_Optimize (Vector);
20351 @end example
20352 @end quotation
20353
20354 placed immediately within the loop will convey the appropriate hint to the
20355 compiler for this loop.
20356
20357 It is also possible to help the compiler generate better vectorized code
20358 for a given loop by asserting that there are no loop-carried dependencies
20359 in the loop. Consider for example the procedure:
20360
20361 @quotation
20362
20363 @example
20364 type Arr is array (1 .. 4) of Long_Float;
20365
20366 procedure Add (X, Y : not null access Arr; R : not null access Arr) is
20367 begin
20368 for I in Arr'Range loop
20369 R(I) := X(I) + Y(I);
20370 end loop;
20371 end;
20372 @end example
20373 @end quotation
20374
20375 By default, the compiler cannot unconditionally vectorize the loop because
20376 assigning to a component of the array designated by R in one iteration could
20377 change the value read from the components of the array designated by X or Y
20378 in a later iteration. As a result, the compiler will generate two versions
20379 of the loop in the object code, one vectorized and the other not vectorized,
20380 as well as a test to select the appropriate version at run time. This can
20381 be overcome by another hint:
20382
20383 @quotation
20384
20385 @example
20386 pragma Loop_Optimize (Ivdep);
20387 @end example
20388 @end quotation
20389
20390 placed immediately within the loop will tell the compiler that it can safely
20391 omit the non-vectorized version of the loop as well as the run-time test.
20392
20393 @node Other Optimization Switches,Optimization and Strict Aliasing,Vectorization of loops,Performance Considerations
20394 @anchor{gnat_ugn/gnat_and_program_execution id35}@anchor{18a}@anchor{gnat_ugn/gnat_and_program_execution other-optimization-switches}@anchor{18b}
20395 @subsubsection Other Optimization Switches
20396
20397
20398 @geindex Optimization Switches
20399
20400 Since GNAT uses the @code{gcc} back end, all the specialized
20401 @code{gcc} optimization switches are potentially usable. These switches
20402 have not been extensively tested with GNAT but can generally be expected
20403 to work. Examples of switches in this category are @code{-funroll-loops}
20404 and the various target-specific @code{-m} options (in particular, it has
20405 been observed that @code{-march=xxx} can significantly improve performance
20406 on appropriate machines). For full details of these switches, see
20407 the @emph{Submodel Options} section in the @emph{Hardware Models and Configurations}
20408 chapter of @cite{Using the GNU Compiler Collection (GCC)}.
20409
20410 @node Optimization and Strict Aliasing,Aliased Variables and Optimization,Other Optimization Switches,Performance Considerations
20411 @anchor{gnat_ugn/gnat_and_program_execution id36}@anchor{18c}@anchor{gnat_ugn/gnat_and_program_execution optimization-and-strict-aliasing}@anchor{e4}
20412 @subsubsection Optimization and Strict Aliasing
20413
20414
20415 @geindex Aliasing
20416
20417 @geindex Strict Aliasing
20418
20419 @geindex No_Strict_Aliasing
20420
20421 The strong typing capabilities of Ada allow an optimizer to generate
20422 efficient code in situations where other languages would be forced to
20423 make worst case assumptions preventing such optimizations. Consider
20424 the following example:
20425
20426 @quotation
20427
20428 @example
20429 procedure R is
20430 type Int1 is new Integer;
20431 type Int2 is new Integer;
20432 type Int1A is access Int1;
20433 type Int2A is access Int2;
20434 Int1V : Int1A;
20435 Int2V : Int2A;
20436 ...
20437
20438 begin
20439 ...
20440 for J in Data'Range loop
20441 if Data (J) = Int1V.all then
20442 Int2V.all := Int2V.all + 1;
20443 end if;
20444 end loop;
20445 ...
20446 end R;
20447 @end example
20448 @end quotation
20449
20450 In this example, since the variable @code{Int1V} can only access objects
20451 of type @code{Int1}, and @code{Int2V} can only access objects of type
20452 @code{Int2}, there is no possibility that the assignment to
20453 @code{Int2V.all} affects the value of @code{Int1V.all}. This means that
20454 the compiler optimizer can “know” that the value @code{Int1V.all} is constant
20455 for all iterations of the loop and avoid the extra memory reference
20456 required to dereference it each time through the loop.
20457
20458 This kind of optimization, called strict aliasing analysis, is
20459 triggered by specifying an optimization level of @code{-O2} or
20460 higher or @code{-Os} and allows GNAT to generate more efficient code
20461 when access values are involved.
20462
20463 However, although this optimization is always correct in terms of
20464 the formal semantics of the Ada Reference Manual, difficulties can
20465 arise if features like @code{Unchecked_Conversion} are used to break
20466 the typing system. Consider the following complete program example:
20467
20468 @quotation
20469
20470 @example
20471 package p1 is
20472 type int1 is new integer;
20473 type int2 is new integer;
20474 type a1 is access int1;
20475 type a2 is access int2;
20476 end p1;
20477
20478 with p1; use p1;
20479 package p2 is
20480 function to_a2 (Input : a1) return a2;
20481 end p2;
20482
20483 with Unchecked_Conversion;
20484 package body p2 is
20485 function to_a2 (Input : a1) return a2 is
20486 function to_a2u is
20487 new Unchecked_Conversion (a1, a2);
20488 begin
20489 return to_a2u (Input);
20490 end to_a2;
20491 end p2;
20492
20493 with p2; use p2;
20494 with p1; use p1;
20495 with Text_IO; use Text_IO;
20496 procedure m is
20497 v1 : a1 := new int1;
20498 v2 : a2 := to_a2 (v1);
20499 begin
20500 v1.all := 1;
20501 v2.all := 0;
20502 put_line (int1'image (v1.all));
20503 end;
20504 @end example
20505 @end quotation
20506
20507 This program prints out 0 in @code{-O0} or @code{-O1}
20508 mode, but it prints out 1 in @code{-O2} mode. That’s
20509 because in strict aliasing mode, the compiler can and
20510 does assume that the assignment to @code{v2.all} could not
20511 affect the value of @code{v1.all}, since different types
20512 are involved.
20513
20514 This behavior is not a case of non-conformance with the standard, since
20515 the Ada RM specifies that an unchecked conversion where the resulting
20516 bit pattern is not a correct value of the target type can result in an
20517 abnormal value and attempting to reference an abnormal value makes the
20518 execution of a program erroneous. That’s the case here since the result
20519 does not point to an object of type @code{int2}. This means that the
20520 effect is entirely unpredictable.
20521
20522 However, although that explanation may satisfy a language
20523 lawyer, in practice an applications programmer expects an
20524 unchecked conversion involving pointers to create true
20525 aliases and the behavior of printing 1 seems plain wrong.
20526 In this case, the strict aliasing optimization is unwelcome.
20527
20528 Indeed the compiler recognizes this possibility, and the
20529 unchecked conversion generates a warning:
20530
20531 @quotation
20532
20533 @example
20534 p2.adb:5:07: warning: possible aliasing problem with type "a2"
20535 p2.adb:5:07: warning: use -fno-strict-aliasing switch for references
20536 p2.adb:5:07: warning: or use "pragma No_Strict_Aliasing (a2);"
20537 @end example
20538 @end quotation
20539
20540 Unfortunately the problem is recognized when compiling the body of
20541 package @code{p2}, but the actual “bad” code is generated while
20542 compiling the body of @code{m} and this latter compilation does not see
20543 the suspicious @code{Unchecked_Conversion}.
20544
20545 As implied by the warning message, there are approaches you can use to
20546 avoid the unwanted strict aliasing optimization in a case like this.
20547
20548 One possibility is to simply avoid the use of @code{-O2}, but
20549 that is a bit drastic, since it throws away a number of useful
20550 optimizations that do not involve strict aliasing assumptions.
20551
20552 A less drastic approach is to compile the program using the
20553 option @code{-fno-strict-aliasing}. Actually it is only the
20554 unit containing the dereferencing of the suspicious pointer
20555 that needs to be compiled. So in this case, if we compile
20556 unit @code{m} with this switch, then we get the expected
20557 value of zero printed. Analyzing which units might need
20558 the switch can be painful, so a more reasonable approach
20559 is to compile the entire program with options @code{-O2}
20560 and @code{-fno-strict-aliasing}. If the performance is
20561 satisfactory with this combination of options, then the
20562 advantage is that the entire issue of possible “wrong”
20563 optimization due to strict aliasing is avoided.
20564
20565 To avoid the use of compiler switches, the configuration
20566 pragma @code{No_Strict_Aliasing} with no parameters may be
20567 used to specify that for all access types, the strict
20568 aliasing optimization should be suppressed.
20569
20570 However, these approaches are still overkill, in that they causes
20571 all manipulations of all access values to be deoptimized. A more
20572 refined approach is to concentrate attention on the specific
20573 access type identified as problematic.
20574
20575 First, if a careful analysis of uses of the pointer shows
20576 that there are no possible problematic references, then
20577 the warning can be suppressed by bracketing the
20578 instantiation of @code{Unchecked_Conversion} to turn
20579 the warning off:
20580
20581 @quotation
20582
20583 @example
20584 pragma Warnings (Off);
20585 function to_a2u is
20586 new Unchecked_Conversion (a1, a2);
20587 pragma Warnings (On);
20588 @end example
20589 @end quotation
20590
20591 Of course that approach is not appropriate for this particular
20592 example, since indeed there is a problematic reference. In this
20593 case we can take one of two other approaches.
20594
20595 The first possibility is to move the instantiation of unchecked
20596 conversion to the unit in which the type is declared. In
20597 this example, we would move the instantiation of
20598 @code{Unchecked_Conversion} from the body of package
20599 @code{p2} to the spec of package @code{p1}. Now the
20600 warning disappears. That’s because any use of the
20601 access type knows there is a suspicious unchecked
20602 conversion, and the strict aliasing optimization
20603 is automatically suppressed for the type.
20604
20605 If it is not practical to move the unchecked conversion to the same unit
20606 in which the destination access type is declared (perhaps because the
20607 source type is not visible in that unit), you may use pragma
20608 @code{No_Strict_Aliasing} for the type. This pragma must occur in the
20609 same declarative sequence as the declaration of the access type:
20610
20611 @quotation
20612
20613 @example
20614 type a2 is access int2;
20615 pragma No_Strict_Aliasing (a2);
20616 @end example
20617 @end quotation
20618
20619 Here again, the compiler now knows that the strict aliasing optimization
20620 should be suppressed for any reference to type @code{a2} and the
20621 expected behavior is obtained.
20622
20623 Finally, note that although the compiler can generate warnings for
20624 simple cases of unchecked conversions, there are tricker and more
20625 indirect ways of creating type incorrect aliases which the compiler
20626 cannot detect. Examples are the use of address overlays and unchecked
20627 conversions involving composite types containing access types as
20628 components. In such cases, no warnings are generated, but there can
20629 still be aliasing problems. One safe coding practice is to forbid the
20630 use of address clauses for type overlaying, and to allow unchecked
20631 conversion only for primitive types. This is not really a significant
20632 restriction since any possible desired effect can be achieved by
20633 unchecked conversion of access values.
20634
20635 The aliasing analysis done in strict aliasing mode can certainly
20636 have significant benefits. We have seen cases of large scale
20637 application code where the time is increased by up to 5% by turning
20638 this optimization off. If you have code that includes significant
20639 usage of unchecked conversion, you might want to just stick with
20640 @code{-O1} and avoid the entire issue. If you get adequate
20641 performance at this level of optimization level, that’s probably
20642 the safest approach. If tests show that you really need higher
20643 levels of optimization, then you can experiment with @code{-O2}
20644 and @code{-O2 -fno-strict-aliasing} to see how much effect this
20645 has on size and speed of the code. If you really need to use
20646 @code{-O2} with strict aliasing in effect, then you should
20647 review any uses of unchecked conversion of access types,
20648 particularly if you are getting the warnings described above.
20649
20650 @node Aliased Variables and Optimization,Atomic Variables and Optimization,Optimization and Strict Aliasing,Performance Considerations
20651 @anchor{gnat_ugn/gnat_and_program_execution aliased-variables-and-optimization}@anchor{18d}@anchor{gnat_ugn/gnat_and_program_execution id37}@anchor{18e}
20652 @subsubsection Aliased Variables and Optimization
20653
20654
20655 @geindex Aliasing
20656
20657 There are scenarios in which programs may
20658 use low level techniques to modify variables
20659 that otherwise might be considered to be unassigned. For example,
20660 a variable can be passed to a procedure by reference, which takes
20661 the address of the parameter and uses the address to modify the
20662 variable’s value, even though it is passed as an IN parameter.
20663 Consider the following example:
20664
20665 @quotation
20666
20667 @example
20668 procedure P is
20669 Max_Length : constant Natural := 16;
20670 type Char_Ptr is access all Character;
20671
20672 procedure Get_String(Buffer: Char_Ptr; Size : Integer);
20673 pragma Import (C, Get_String, "get_string");
20674
20675 Name : aliased String (1 .. Max_Length) := (others => ' ');
20676 Temp : Char_Ptr;
20677
20678 function Addr (S : String) return Char_Ptr is
20679 function To_Char_Ptr is
20680 new Ada.Unchecked_Conversion (System.Address, Char_Ptr);
20681 begin
20682 return To_Char_Ptr (S (S'First)'Address);
20683 end;
20684
20685 begin
20686 Temp := Addr (Name);
20687 Get_String (Temp, Max_Length);
20688 end;
20689 @end example
20690 @end quotation
20691
20692 where Get_String is a C function that uses the address in Temp to
20693 modify the variable @code{Name}. This code is dubious, and arguably
20694 erroneous, and the compiler would be entitled to assume that
20695 @code{Name} is never modified, and generate code accordingly.
20696
20697 However, in practice, this would cause some existing code that
20698 seems to work with no optimization to start failing at high
20699 levels of optimzization.
20700
20701 What the compiler does for such cases is to assume that marking
20702 a variable as aliased indicates that some “funny business” may
20703 be going on. The optimizer recognizes the aliased keyword and
20704 inhibits optimizations that assume the value cannot be assigned.
20705 This means that the above example will in fact “work” reliably,
20706 that is, it will produce the expected results.
20707
20708 @node Atomic Variables and Optimization,Passive Task Optimization,Aliased Variables and Optimization,Performance Considerations
20709 @anchor{gnat_ugn/gnat_and_program_execution atomic-variables-and-optimization}@anchor{18f}@anchor{gnat_ugn/gnat_and_program_execution id38}@anchor{190}
20710 @subsubsection Atomic Variables and Optimization
20711
20712
20713 @geindex Atomic
20714
20715 There are two considerations with regard to performance when
20716 atomic variables are used.
20717
20718 First, the RM only guarantees that access to atomic variables
20719 be atomic, it has nothing to say about how this is achieved,
20720 though there is a strong implication that this should not be
20721 achieved by explicit locking code. Indeed GNAT will never
20722 generate any locking code for atomic variable access (it will
20723 simply reject any attempt to make a variable or type atomic
20724 if the atomic access cannot be achieved without such locking code).
20725
20726 That being said, it is important to understand that you cannot
20727 assume that the entire variable will always be accessed. Consider
20728 this example:
20729
20730 @quotation
20731
20732 @example
20733 type R is record
20734 A,B,C,D : Character;
20735 end record;
20736 for R'Size use 32;
20737 for R'Alignment use 4;
20738
20739 RV : R;
20740 pragma Atomic (RV);
20741 X : Character;
20742 ...
20743 X := RV.B;
20744 @end example
20745 @end quotation
20746
20747 You cannot assume that the reference to @code{RV.B}
20748 will read the entire 32-bit
20749 variable with a single load instruction. It is perfectly legitimate if
20750 the hardware allows it to do a byte read of just the B field. This read
20751 is still atomic, which is all the RM requires. GNAT can and does take
20752 advantage of this, depending on the architecture and optimization level.
20753 Any assumption to the contrary is non-portable and risky. Even if you
20754 examine the assembly language and see a full 32-bit load, this might
20755 change in a future version of the compiler.
20756
20757 If your application requires that all accesses to @code{RV} in this
20758 example be full 32-bit loads, you need to make a copy for the access
20759 as in:
20760
20761 @quotation
20762
20763 @example
20764 declare
20765 RV_Copy : constant R := RV;
20766 begin
20767 X := RV_Copy.B;
20768 end;
20769 @end example
20770 @end quotation
20771
20772 Now the reference to RV must read the whole variable.
20773 Actually one can imagine some compiler which figures
20774 out that the whole copy is not required (because only
20775 the B field is actually accessed), but GNAT
20776 certainly won’t do that, and we don’t know of any
20777 compiler that would not handle this right, and the
20778 above code will in practice work portably across
20779 all architectures (that permit the Atomic declaration).
20780
20781 The second issue with atomic variables has to do with
20782 the possible requirement of generating synchronization
20783 code. For more details on this, consult the sections on
20784 the pragmas Enable/Disable_Atomic_Synchronization in the
20785 GNAT Reference Manual. If performance is critical, and
20786 such synchronization code is not required, it may be
20787 useful to disable it.
20788
20789 @node Passive Task Optimization,,Atomic Variables and Optimization,Performance Considerations
20790 @anchor{gnat_ugn/gnat_and_program_execution id39}@anchor{191}@anchor{gnat_ugn/gnat_and_program_execution passive-task-optimization}@anchor{192}
20791 @subsubsection Passive Task Optimization
20792
20793
20794 @geindex Passive Task
20795
20796 A passive task is one which is sufficiently simple that
20797 in theory a compiler could recognize it an implement it
20798 efficiently without creating a new thread. The original design
20799 of Ada 83 had in mind this kind of passive task optimization, but
20800 only a few Ada 83 compilers attempted it. The problem was that
20801 it was difficult to determine the exact conditions under which
20802 the optimization was possible. The result is a very fragile
20803 optimization where a very minor change in the program can
20804 suddenly silently make a task non-optimizable.
20805
20806 With the revisiting of this issue in Ada 95, there was general
20807 agreement that this approach was fundamentally flawed, and the
20808 notion of protected types was introduced. When using protected
20809 types, the restrictions are well defined, and you KNOW that the
20810 operations will be optimized, and furthermore this optimized
20811 performance is fully portable.
20812
20813 Although it would theoretically be possible for GNAT to attempt to
20814 do this optimization, but it really doesn’t make sense in the
20815 context of Ada 95, and none of the Ada 95 compilers implement
20816 this optimization as far as we know. In particular GNAT never
20817 attempts to perform this optimization.
20818
20819 In any new Ada 95 code that is written, you should always
20820 use protected types in place of tasks that might be able to
20821 be optimized in this manner.
20822 Of course this does not help if you have legacy Ada 83 code
20823 that depends on this optimization, but it is unusual to encounter
20824 a case where the performance gains from this optimization
20825 are significant.
20826
20827 Your program should work correctly without this optimization. If
20828 you have performance problems, then the most practical
20829 approach is to figure out exactly where these performance problems
20830 arise, and update those particular tasks to be protected types. Note
20831 that typically clients of the tasks who call entries, will not have
20832 to be modified, only the task definition itself.
20833
20834 @node Text_IO Suggestions,Reducing Size of Executables with Unused Subprogram/Data Elimination,Performance Considerations,Improving Performance
20835 @anchor{gnat_ugn/gnat_and_program_execution id40}@anchor{193}@anchor{gnat_ugn/gnat_and_program_execution text-io-suggestions}@anchor{194}
20836 @subsection @code{Text_IO} Suggestions
20837
20838
20839 @geindex Text_IO and performance
20840
20841 The @code{Ada.Text_IO} package has fairly high overheads due in part to
20842 the requirement of maintaining page and line counts. If performance
20843 is critical, a recommendation is to use @code{Stream_IO} instead of
20844 @code{Text_IO} for volume output, since this package has less overhead.
20845
20846 If @code{Text_IO} must be used, note that by default output to the standard
20847 output and standard error files is unbuffered (this provides better
20848 behavior when output statements are used for debugging, or if the
20849 progress of a program is observed by tracking the output, e.g. by
20850 using the Unix @emph{tail -f} command to watch redirected output.
20851
20852 If you are generating large volumes of output with @code{Text_IO} and
20853 performance is an important factor, use a designated file instead
20854 of the standard output file, or change the standard output file to
20855 be buffered using @code{Interfaces.C_Streams.setvbuf}.
20856
20857 @node Reducing Size of Executables with Unused Subprogram/Data Elimination,,Text_IO Suggestions,Improving Performance
20858 @anchor{gnat_ugn/gnat_and_program_execution id41}@anchor{195}@anchor{gnat_ugn/gnat_and_program_execution reducing-size-of-executables-with-unused-subprogram-data-elimination}@anchor{196}
20859 @subsection Reducing Size of Executables with Unused Subprogram/Data Elimination
20860
20861
20862 @geindex Uunused subprogram/data elimination
20863
20864 This section describes how you can eliminate unused subprograms and data from
20865 your executable just by setting options at compilation time.
20866
20867 @menu
20868 * About unused subprogram/data elimination::
20869 * Compilation options::
20870 * Example of unused subprogram/data elimination::
20871
20872 @end menu
20873
20874 @node About unused subprogram/data elimination,Compilation options,,Reducing Size of Executables with Unused Subprogram/Data Elimination
20875 @anchor{gnat_ugn/gnat_and_program_execution about-unused-subprogram-data-elimination}@anchor{197}@anchor{gnat_ugn/gnat_and_program_execution id42}@anchor{198}
20876 @subsubsection About unused subprogram/data elimination
20877
20878
20879 By default, an executable contains all code and data of its composing objects
20880 (directly linked or coming from statically linked libraries), even data or code
20881 never used by this executable.
20882
20883 This feature will allow you to eliminate such unused code from your
20884 executable, making it smaller (in disk and in memory).
20885
20886 This functionality is available on all Linux platforms except for the IA-64
20887 architecture and on all cross platforms using the ELF binary file format.
20888 In both cases GNU binutils version 2.16 or later are required to enable it.
20889
20890 @node Compilation options,Example of unused subprogram/data elimination,About unused subprogram/data elimination,Reducing Size of Executables with Unused Subprogram/Data Elimination
20891 @anchor{gnat_ugn/gnat_and_program_execution compilation-options}@anchor{199}@anchor{gnat_ugn/gnat_and_program_execution id43}@anchor{19a}
20892 @subsubsection Compilation options
20893
20894
20895 The operation of eliminating the unused code and data from the final executable
20896 is directly performed by the linker.
20897
20898 @geindex -ffunction-sections (gcc)
20899
20900 @geindex -fdata-sections (gcc)
20901
20902 In order to do this, it has to work with objects compiled with the
20903 following options:
20904 @code{-ffunction-sections} @code{-fdata-sections}.
20905
20906 These options are usable with C and Ada files.
20907 They will place respectively each
20908 function or data in a separate section in the resulting object file.
20909
20910 Once the objects and static libraries are created with these options, the
20911 linker can perform the dead code elimination. You can do this by setting
20912 the @code{-Wl,--gc-sections} option to gcc command or in the
20913 @code{-largs} section of @code{gnatmake}. This will perform a
20914 garbage collection of code and data never referenced.
20915
20916 If the linker performs a partial link (@code{-r} linker option), then you
20917 will need to provide the entry point using the @code{-e} / @code{--entry}
20918 linker option.
20919
20920 Note that objects compiled without the @code{-ffunction-sections} and
20921 @code{-fdata-sections} options can still be linked with the executable.
20922 However, no dead code elimination will be performed on those objects (they will
20923 be linked as is).
20924
20925 The GNAT static library is now compiled with -ffunction-sections and
20926 -fdata-sections on some platforms. This allows you to eliminate the unused code
20927 and data of the GNAT library from your executable.
20928
20929 @node Example of unused subprogram/data elimination,,Compilation options,Reducing Size of Executables with Unused Subprogram/Data Elimination
20930 @anchor{gnat_ugn/gnat_and_program_execution example-of-unused-subprogram-data-elimination}@anchor{19b}@anchor{gnat_ugn/gnat_and_program_execution id44}@anchor{19c}
20931 @subsubsection Example of unused subprogram/data elimination
20932
20933
20934 Here is a simple example:
20935
20936 @quotation
20937
20938 @example
20939 with Aux;
20940
20941 procedure Test is
20942 begin
20943 Aux.Used (10);
20944 end Test;
20945
20946 package Aux is
20947 Used_Data : Integer;
20948 Unused_Data : Integer;
20949
20950 procedure Used (Data : Integer);
20951 procedure Unused (Data : Integer);
20952 end Aux;
20953
20954 package body Aux is
20955 procedure Used (Data : Integer) is
20956 begin
20957 Used_Data := Data;
20958 end Used;
20959
20960 procedure Unused (Data : Integer) is
20961 begin
20962 Unused_Data := Data;
20963 end Unused;
20964 end Aux;
20965 @end example
20966 @end quotation
20967
20968 @code{Unused} and @code{Unused_Data} are never referenced in this code
20969 excerpt, and hence they may be safely removed from the final executable.
20970
20971 @quotation
20972
20973 @example
20974 $ gnatmake test
20975
20976 $ nm test | grep used
20977 020015f0 T aux__unused
20978 02005d88 B aux__unused_data
20979 020015cc T aux__used
20980 02005d84 B aux__used_data
20981
20982 $ gnatmake test -cargs -fdata-sections -ffunction-sections \\
20983 -largs -Wl,--gc-sections
20984
20985 $ nm test | grep used
20986 02005350 T aux__used
20987 0201ffe0 B aux__used_data
20988 @end example
20989 @end quotation
20990
20991 It can be observed that the procedure @code{Unused} and the object
20992 @code{Unused_Data} are removed by the linker when using the
20993 appropriate options.
20994
20995 @geindex Overflow checks
20996
20997 @geindex Checks (overflow)
20998
20999 @node Overflow Check Handling in GNAT,Performing Dimensionality Analysis in GNAT,Improving Performance,GNAT and Program Execution
21000 @anchor{gnat_ugn/gnat_and_program_execution id45}@anchor{149}@anchor{gnat_ugn/gnat_and_program_execution overflow-check-handling-in-gnat}@anchor{19d}
21001 @section Overflow Check Handling in GNAT
21002
21003
21004 This section explains how to control the handling of overflow checks.
21005
21006 @menu
21007 * Background::
21008 * Management of Overflows in GNAT::
21009 * Specifying the Desired Mode::
21010 * Default Settings::
21011 * Implementation Notes::
21012
21013 @end menu
21014
21015 @node Background,Management of Overflows in GNAT,,Overflow Check Handling in GNAT
21016 @anchor{gnat_ugn/gnat_and_program_execution background}@anchor{19e}@anchor{gnat_ugn/gnat_and_program_execution id46}@anchor{19f}
21017 @subsection Background
21018
21019
21020 Overflow checks are checks that the compiler may make to ensure
21021 that intermediate results are not out of range. For example:
21022
21023 @quotation
21024
21025 @example
21026 A : Integer;
21027 ...
21028 A := A + 1;
21029 @end example
21030 @end quotation
21031
21032 If @code{A} has the value @code{Integer'Last}, then the addition may cause
21033 overflow since the result is out of range of the type @code{Integer}.
21034 In this case @code{Constraint_Error} will be raised if checks are
21035 enabled.
21036
21037 A trickier situation arises in examples like the following:
21038
21039 @quotation
21040
21041 @example
21042 A, C : Integer;
21043 ...
21044 A := (A + 1) + C;
21045 @end example
21046 @end quotation
21047
21048 where @code{A} is @code{Integer'Last} and @code{C} is @code{-1}.
21049 Now the final result of the expression on the right hand side is
21050 @code{Integer'Last} which is in range, but the question arises whether the
21051 intermediate addition of @code{(A + 1)} raises an overflow error.
21052
21053 The (perhaps surprising) answer is that the Ada language
21054 definition does not answer this question. Instead it leaves
21055 it up to the implementation to do one of two things if overflow
21056 checks are enabled.
21057
21058
21059 @itemize *
21060
21061 @item
21062 raise an exception (@code{Constraint_Error}), or
21063
21064 @item
21065 yield the correct mathematical result which is then used in
21066 subsequent operations.
21067 @end itemize
21068
21069 If the compiler chooses the first approach, then the assignment of this
21070 example will indeed raise @code{Constraint_Error} if overflow checking is
21071 enabled, or result in erroneous execution if overflow checks are suppressed.
21072
21073 But if the compiler
21074 chooses the second approach, then it can perform both additions yielding
21075 the correct mathematical result, which is in range, so no exception
21076 will be raised, and the right result is obtained, regardless of whether
21077 overflow checks are suppressed.
21078
21079 Note that in the first example an
21080 exception will be raised in either case, since if the compiler
21081 gives the correct mathematical result for the addition, it will
21082 be out of range of the target type of the assignment, and thus
21083 fails the range check.
21084
21085 This lack of specified behavior in the handling of overflow for
21086 intermediate results is a source of non-portability, and can thus
21087 be problematic when programs are ported. Most typically this arises
21088 in a situation where the original compiler did not raise an exception,
21089 and then the application is moved to a compiler where the check is
21090 performed on the intermediate result and an unexpected exception is
21091 raised.
21092
21093 Furthermore, when using Ada 2012’s preconditions and other
21094 assertion forms, another issue arises. Consider:
21095
21096 @quotation
21097
21098 @example
21099 procedure P (A, B : Integer) with
21100 Pre => A + B <= Integer'Last;
21101 @end example
21102 @end quotation
21103
21104 One often wants to regard arithmetic in a context like this from
21105 a mathematical point of view. So for example, if the two actual parameters
21106 for a call to @code{P} are both @code{Integer'Last}, then
21107 the precondition should be regarded as False. If we are executing
21108 in a mode with run-time checks enabled for preconditions, then we would
21109 like this precondition to fail, rather than raising an exception
21110 because of the intermediate overflow.
21111
21112 However, the language definition leaves the specification of
21113 whether the above condition fails (raising @code{Assert_Error}) or
21114 causes an intermediate overflow (raising @code{Constraint_Error})
21115 up to the implementation.
21116
21117 The situation is worse in a case such as the following:
21118
21119 @quotation
21120
21121 @example
21122 procedure Q (A, B, C : Integer) with
21123 Pre => A + B + C <= Integer'Last;
21124 @end example
21125 @end quotation
21126
21127 Consider the call
21128
21129 @quotation
21130
21131 @example
21132 Q (A => Integer'Last, B => 1, C => -1);
21133 @end example
21134 @end quotation
21135
21136 From a mathematical point of view the precondition
21137 is True, but at run time we may (but are not guaranteed to) get an
21138 exception raised because of the intermediate overflow (and we really
21139 would prefer this precondition to be considered True at run time).
21140
21141 @node Management of Overflows in GNAT,Specifying the Desired Mode,Background,Overflow Check Handling in GNAT
21142 @anchor{gnat_ugn/gnat_and_program_execution id47}@anchor{1a0}@anchor{gnat_ugn/gnat_and_program_execution management-of-overflows-in-gnat}@anchor{1a1}
21143 @subsection Management of Overflows in GNAT
21144
21145
21146 To deal with the portability issue, and with the problem of
21147 mathematical versus run-time interpretation of the expressions in
21148 assertions, GNAT provides comprehensive control over the handling
21149 of intermediate overflow. GNAT can operate in three modes, and
21150 furthemore, permits separate selection of operating modes for
21151 the expressions within assertions (here the term ‘assertions’
21152 is used in the technical sense, which includes preconditions and so forth)
21153 and for expressions appearing outside assertions.
21154
21155 The three modes are:
21156
21157
21158 @itemize *
21159
21160 @item
21161 @emph{Use base type for intermediate operations} (@code{STRICT})
21162
21163 In this mode, all intermediate results for predefined arithmetic
21164 operators are computed using the base type, and the result must
21165 be in range of the base type. If this is not the
21166 case then either an exception is raised (if overflow checks are
21167 enabled) or the execution is erroneous (if overflow checks are suppressed).
21168 This is the normal default mode.
21169
21170 @item
21171 @emph{Most intermediate overflows avoided} (@code{MINIMIZED})
21172
21173 In this mode, the compiler attempts to avoid intermediate overflows by
21174 using a larger integer type, typically @code{Long_Long_Integer},
21175 as the type in which arithmetic is
21176 performed for predefined arithmetic operators. This may be slightly more
21177 expensive at
21178 run time (compared to suppressing intermediate overflow checks), though
21179 the cost is negligible on modern 64-bit machines. For the examples given
21180 earlier, no intermediate overflows would have resulted in exceptions,
21181 since the intermediate results are all in the range of
21182 @code{Long_Long_Integer} (typically 64-bits on nearly all implementations
21183 of GNAT). In addition, if checks are enabled, this reduces the number of
21184 checks that must be made, so this choice may actually result in an
21185 improvement in space and time behavior.
21186
21187 However, there are cases where @code{Long_Long_Integer} is not large
21188 enough, consider the following example:
21189
21190 @quotation
21191
21192 @example
21193 procedure R (A, B, C, D : Integer) with
21194 Pre => (A**2 * B**2) / (C**2 * D**2) <= 10;
21195 @end example
21196 @end quotation
21197
21198 where @code{A} = @code{B} = @code{C} = @code{D} = @code{Integer'Last}.
21199 Now the intermediate results are
21200 out of the range of @code{Long_Long_Integer} even though the final result
21201 is in range and the precondition is True (from a mathematical point
21202 of view). In such a case, operating in this mode, an overflow occurs
21203 for the intermediate computation (which is why this mode
21204 says @emph{most} intermediate overflows are avoided). In this case,
21205 an exception is raised if overflow checks are enabled, and the
21206 execution is erroneous if overflow checks are suppressed.
21207
21208 @item
21209 @emph{All intermediate overflows avoided} (@code{ELIMINATED})
21210
21211 In this mode, the compiler avoids all intermediate overflows
21212 by using arbitrary precision arithmetic as required. In this
21213 mode, the above example with @code{A**2 * B**2} would
21214 not cause intermediate overflow, because the intermediate result
21215 would be evaluated using sufficient precision, and the result
21216 of evaluating the precondition would be True.
21217
21218 This mode has the advantage of avoiding any intermediate
21219 overflows, but at the expense of significant run-time overhead,
21220 including the use of a library (included automatically in this
21221 mode) for multiple-precision arithmetic.
21222
21223 This mode provides cleaner semantics for assertions, since now
21224 the run-time behavior emulates true arithmetic behavior for the
21225 predefined arithmetic operators, meaning that there is never a
21226 conflict between the mathematical view of the assertion, and its
21227 run-time behavior.
21228
21229 Note that in this mode, the behavior is unaffected by whether or
21230 not overflow checks are suppressed, since overflow does not occur.
21231 It is possible for gigantic intermediate expressions to raise
21232 @code{Storage_Error} as a result of attempting to compute the
21233 results of such expressions (e.g. @code{Integer'Last ** Integer'Last})
21234 but overflow is impossible.
21235 @end itemize
21236
21237 Note that these modes apply only to the evaluation of predefined
21238 arithmetic, membership, and comparison operators for signed integer
21239 arithmetic.
21240
21241 For fixed-point arithmetic, checks can be suppressed. But if checks
21242 are enabled
21243 then fixed-point values are always checked for overflow against the
21244 base type for intermediate expressions (that is such checks always
21245 operate in the equivalent of @code{STRICT} mode).
21246
21247 For floating-point, on nearly all architectures, @code{Machine_Overflows}
21248 is False, and IEEE infinities are generated, so overflow exceptions
21249 are never raised. If you want to avoid infinities, and check that
21250 final results of expressions are in range, then you can declare a
21251 constrained floating-point type, and range checks will be carried
21252 out in the normal manner (with infinite values always failing all
21253 range checks).
21254
21255 @node Specifying the Desired Mode,Default Settings,Management of Overflows in GNAT,Overflow Check Handling in GNAT
21256 @anchor{gnat_ugn/gnat_and_program_execution id48}@anchor{1a2}@anchor{gnat_ugn/gnat_and_program_execution specifying-the-desired-mode}@anchor{e9}
21257 @subsection Specifying the Desired Mode
21258
21259
21260 @geindex pragma Overflow_Mode
21261
21262 The desired mode of for handling intermediate overflow can be specified using
21263 either the @code{Overflow_Mode} pragma or an equivalent compiler switch.
21264 The pragma has the form
21265
21266 @quotation
21267
21268 @example
21269 pragma Overflow_Mode ([General =>] MODE [, [Assertions =>] MODE]);
21270 @end example
21271 @end quotation
21272
21273 where @code{MODE} is one of
21274
21275
21276 @itemize *
21277
21278 @item
21279 @code{STRICT}: intermediate overflows checked (using base type)
21280
21281 @item
21282 @code{MINIMIZED}: minimize intermediate overflows
21283
21284 @item
21285 @code{ELIMINATED}: eliminate intermediate overflows
21286 @end itemize
21287
21288 The case is ignored, so @code{MINIMIZED}, @code{Minimized} and
21289 @code{minimized} all have the same effect.
21290
21291 If only the @code{General} parameter is present, then the given @code{MODE} applies
21292 to expressions both within and outside assertions. If both arguments
21293 are present, then @code{General} applies to expressions outside assertions,
21294 and @code{Assertions} applies to expressions within assertions. For example:
21295
21296 @quotation
21297
21298 @example
21299 pragma Overflow_Mode
21300 (General => Minimized, Assertions => Eliminated);
21301 @end example
21302 @end quotation
21303
21304 specifies that general expressions outside assertions be evaluated
21305 in ‘minimize intermediate overflows’ mode, and expressions within
21306 assertions be evaluated in ‘eliminate intermediate overflows’ mode.
21307 This is often a reasonable choice, avoiding excessive overhead
21308 outside assertions, but assuring a high degree of portability
21309 when importing code from another compiler, while incurring
21310 the extra overhead for assertion expressions to ensure that
21311 the behavior at run time matches the expected mathematical
21312 behavior.
21313
21314 The @code{Overflow_Mode} pragma has the same scoping and placement
21315 rules as pragma @code{Suppress}, so it can occur either as a
21316 configuration pragma, specifying a default for the whole
21317 program, or in a declarative scope, where it applies to the
21318 remaining declarations and statements in that scope.
21319
21320 Note that pragma @code{Overflow_Mode} does not affect whether
21321 overflow checks are enabled or suppressed. It only controls the
21322 method used to compute intermediate values. To control whether
21323 overflow checking is enabled or suppressed, use pragma @code{Suppress}
21324 or @code{Unsuppress} in the usual manner.
21325
21326 @geindex -gnato? (gcc)
21327
21328 @geindex -gnato?? (gcc)
21329
21330 Additionally, a compiler switch @code{-gnato?} or @code{-gnato??}
21331 can be used to control the checking mode default (which can be subsequently
21332 overridden using pragmas).
21333
21334 Here @code{?} is one of the digits @code{1} through @code{3}:
21335
21336 @quotation
21337
21338
21339 @multitable {xxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
21340 @item
21341
21342 @code{1}
21343
21344 @tab
21345
21346 use base type for intermediate operations (@code{STRICT})
21347
21348 @item
21349
21350 @code{2}
21351
21352 @tab
21353
21354 minimize intermediate overflows (@code{MINIMIZED})
21355
21356 @item
21357
21358 @code{3}
21359
21360 @tab
21361
21362 eliminate intermediate overflows (@code{ELIMINATED})
21363
21364 @end multitable
21365
21366 @end quotation
21367
21368 As with the pragma, if only one digit appears then it applies to all
21369 cases; if two digits are given, then the first applies outside
21370 assertions, and the second within assertions. Thus the equivalent
21371 of the example pragma above would be
21372 @code{-gnato23}.
21373
21374 If no digits follow the @code{-gnato}, then it is equivalent to
21375 @code{-gnato11},
21376 causing all intermediate operations to be computed using the base
21377 type (@code{STRICT} mode).
21378
21379 @node Default Settings,Implementation Notes,Specifying the Desired Mode,Overflow Check Handling in GNAT
21380 @anchor{gnat_ugn/gnat_and_program_execution default-settings}@anchor{1a3}@anchor{gnat_ugn/gnat_and_program_execution id49}@anchor{1a4}
21381 @subsection Default Settings
21382
21383
21384 The default mode for overflow checks is
21385
21386 @quotation
21387
21388 @example
21389 General => Strict
21390 @end example
21391 @end quotation
21392
21393 which causes all computations both inside and outside assertions to use
21394 the base type.
21395
21396 This retains compatibility with previous versions of
21397 GNAT which suppressed overflow checks by default and always
21398 used the base type for computation of intermediate results.
21399
21400 @c Sphinx allows no emphasis within :index: role. As a workaround we
21401 @c point the index to "switch" and use emphasis for "-gnato".
21402
21403 The
21404 @geindex -gnato (gcc)
21405 switch @code{-gnato} (with no digits following)
21406 is equivalent to
21407
21408 @quotation
21409
21410 @example
21411 General => Strict
21412 @end example
21413 @end quotation
21414
21415 which causes overflow checking of all intermediate overflows
21416 both inside and outside assertions against the base type.
21417
21418 The pragma @code{Suppress (Overflow_Check)} disables overflow
21419 checking, but it has no effect on the method used for computing
21420 intermediate results.
21421
21422 The pragma @code{Unsuppress (Overflow_Check)} enables overflow
21423 checking, but it has no effect on the method used for computing
21424 intermediate results.
21425
21426 @node Implementation Notes,,Default Settings,Overflow Check Handling in GNAT
21427 @anchor{gnat_ugn/gnat_and_program_execution id50}@anchor{1a5}@anchor{gnat_ugn/gnat_and_program_execution implementation-notes}@anchor{1a6}
21428 @subsection Implementation Notes
21429
21430
21431 In practice on typical 64-bit machines, the @code{MINIMIZED} mode is
21432 reasonably efficient, and can be generally used. It also helps
21433 to ensure compatibility with code imported from some other
21434 compiler to GNAT.
21435
21436 Setting all intermediate overflows checking (@code{CHECKED} mode)
21437 makes sense if you want to
21438 make sure that your code is compatible with any other possible
21439 Ada implementation. This may be useful in ensuring portability
21440 for code that is to be exported to some other compiler than GNAT.
21441
21442 The Ada standard allows the reassociation of expressions at
21443 the same precedence level if no parentheses are present. For
21444 example, @code{A+B+C} parses as though it were @code{(A+B)+C}, but
21445 the compiler can reintepret this as @code{A+(B+C)}, possibly
21446 introducing or eliminating an overflow exception. The GNAT
21447 compiler never takes advantage of this freedom, and the
21448 expression @code{A+B+C} will be evaluated as @code{(A+B)+C}.
21449 If you need the other order, you can write the parentheses
21450 explicitly @code{A+(B+C)} and GNAT will respect this order.
21451
21452 The use of @code{ELIMINATED} mode will cause the compiler to
21453 automatically include an appropriate arbitrary precision
21454 integer arithmetic package. The compiler will make calls
21455 to this package, though only in cases where it cannot be
21456 sure that @code{Long_Long_Integer} is sufficient to guard against
21457 intermediate overflows. This package does not use dynamic
21458 allocation, but it does use the secondary stack, so an
21459 appropriate secondary stack package must be present (this
21460 is always true for standard full Ada, but may require
21461 specific steps for restricted run times such as ZFP).
21462
21463 Although @code{ELIMINATED} mode causes expressions to use arbitrary
21464 precision arithmetic, avoiding overflow, the final result
21465 must be in an appropriate range. This is true even if the
21466 final result is of type @code{[Long_[Long_]]Integer'Base}, which
21467 still has the same bounds as its associated constrained
21468 type at run-time.
21469
21470 Currently, the @code{ELIMINATED} mode is only available on target
21471 platforms for which @code{Long_Long_Integer} is 64-bits (nearly all GNAT
21472 platforms).
21473
21474 @node Performing Dimensionality Analysis in GNAT,Stack Related Facilities,Overflow Check Handling in GNAT,GNAT and Program Execution
21475 @anchor{gnat_ugn/gnat_and_program_execution id51}@anchor{14a}@anchor{gnat_ugn/gnat_and_program_execution performing-dimensionality-analysis-in-gnat}@anchor{1a7}
21476 @section Performing Dimensionality Analysis in GNAT
21477
21478
21479 @geindex Dimensionality analysis
21480
21481 The GNAT compiler supports dimensionality checking. The user can
21482 specify physical units for objects, and the compiler will verify that uses
21483 of these objects are compatible with their dimensions, in a fashion that is
21484 familiar to engineering practice. The dimensions of algebraic expressions
21485 (including powers with static exponents) are computed from their constituents.
21486
21487 @geindex Dimension_System aspect
21488
21489 @geindex Dimension aspect
21490
21491 This feature depends on Ada 2012 aspect specifications, and is available from
21492 version 7.0.1 of GNAT onwards.
21493 The GNAT-specific aspect @code{Dimension_System}
21494 allows you to define a system of units; the aspect @code{Dimension}
21495 then allows the user to declare dimensioned quantities within a given system.
21496 (These aspects are described in the @emph{Implementation Defined Aspects}
21497 chapter of the @emph{GNAT Reference Manual}).
21498
21499 The major advantage of this model is that it does not require the declaration of
21500 multiple operators for all possible combinations of types: it is only necessary
21501 to use the proper subtypes in object declarations.
21502
21503 @geindex System.Dim.Mks package (GNAT library)
21504
21505 @geindex MKS_Type type
21506
21507 The simplest way to impose dimensionality checking on a computation is to make
21508 use of one of the instantiations of the package @code{System.Dim.Generic_Mks}, which
21509 are part of the GNAT library. This generic package defines a floating-point
21510 type @code{MKS_Type}, for which a sequence of dimension names are specified,
21511 together with their conventional abbreviations. The following should be read
21512 together with the full specification of the package, in file
21513 @code{s-digemk.ads}.
21514
21515 @quotation
21516
21517 @geindex s-digemk.ads file
21518
21519 @example
21520 type Mks_Type is new Float_Type
21521 with
21522 Dimension_System => (
21523 (Unit_Name => Meter, Unit_Symbol => 'm', Dim_Symbol => 'L'),
21524 (Unit_Name => Kilogram, Unit_Symbol => "kg", Dim_Symbol => 'M'),
21525 (Unit_Name => Second, Unit_Symbol => 's', Dim_Symbol => 'T'),
21526 (Unit_Name => Ampere, Unit_Symbol => 'A', Dim_Symbol => 'I'),
21527 (Unit_Name => Kelvin, Unit_Symbol => 'K', Dim_Symbol => "Theta"),
21528 (Unit_Name => Mole, Unit_Symbol => "mol", Dim_Symbol => 'N'),
21529 (Unit_Name => Candela, Unit_Symbol => "cd", Dim_Symbol => 'J'));
21530 @end example
21531 @end quotation
21532
21533 The package then defines a series of subtypes that correspond to these
21534 conventional units. For example:
21535
21536 @quotation
21537
21538 @example
21539 subtype Length is Mks_Type
21540 with
21541 Dimension => (Symbol => 'm', Meter => 1, others => 0);
21542 @end example
21543 @end quotation
21544
21545 and similarly for @code{Mass}, @code{Time}, @code{Electric_Current},
21546 @code{Thermodynamic_Temperature}, @code{Amount_Of_Substance}, and
21547 @code{Luminous_Intensity} (the standard set of units of the SI system).
21548
21549 The package also defines conventional names for values of each unit, for
21550 example:
21551
21552 @quotation
21553
21554 @example
21555 m : constant Length := 1.0;
21556 kg : constant Mass := 1.0;
21557 s : constant Time := 1.0;
21558 A : constant Electric_Current := 1.0;
21559 @end example
21560 @end quotation
21561
21562 as well as useful multiples of these units:
21563
21564 @quotation
21565
21566 @example
21567 cm : constant Length := 1.0E-02;
21568 g : constant Mass := 1.0E-03;
21569 min : constant Time := 60.0;
21570 day : constant Time := 60.0 * 24.0 * min;
21571 ...
21572 @end example
21573 @end quotation
21574
21575 There are three instantiations of @code{System.Dim.Generic_Mks} defined in the
21576 GNAT library:
21577
21578
21579 @itemize *
21580
21581 @item
21582 @code{System.Dim.Float_Mks} based on @code{Float} defined in @code{s-diflmk.ads}.
21583
21584 @item
21585 @code{System.Dim.Long_Mks} based on @code{Long_Float} defined in @code{s-dilomk.ads}.
21586
21587 @item
21588 @code{System.Dim.Mks} based on @code{Long_Long_Float} defined in @code{s-dimmks.ads}.
21589 @end itemize
21590
21591 Using one of these packages, you can then define a derived unit by providing
21592 the aspect that specifies its dimensions within the MKS system, as well as the
21593 string to be used for output of a value of that unit:
21594
21595 @quotation
21596
21597 @example
21598 subtype Acceleration is Mks_Type
21599 with Dimension => ("m/sec^2",
21600 Meter => 1,
21601 Second => -2,
21602 others => 0);
21603 @end example
21604 @end quotation
21605
21606 Here is a complete example of use:
21607
21608 @quotation
21609
21610 @example
21611 with System.Dim.MKS; use System.Dim.Mks;
21612 with System.Dim.Mks_IO; use System.Dim.Mks_IO;
21613 with Text_IO; use Text_IO;
21614 procedure Free_Fall is
21615 subtype Acceleration is Mks_Type
21616 with Dimension => ("m/sec^2", 1, 0, -2, others => 0);
21617 G : constant acceleration := 9.81 * m / (s ** 2);
21618 T : Time := 10.0*s;
21619 Distance : Length;
21620
21621 begin
21622 Put ("Gravitational constant: ");
21623 Put (G, Aft => 2, Exp => 0); Put_Line ("");
21624 Distance := 0.5 * G * T ** 2;
21625 Put ("distance travelled in 10 seconds of free fall ");
21626 Put (Distance, Aft => 2, Exp => 0);
21627 Put_Line ("");
21628 end Free_Fall;
21629 @end example
21630 @end quotation
21631
21632 Execution of this program yields:
21633
21634 @quotation
21635
21636 @example
21637 Gravitational constant: 9.81 m/sec^2
21638 distance travelled in 10 seconds of free fall 490.50 m
21639 @end example
21640 @end quotation
21641
21642 However, incorrect assignments such as:
21643
21644 @quotation
21645
21646 @example
21647 Distance := 5.0;
21648 Distance := 5.0 * kg;
21649 @end example
21650 @end quotation
21651
21652 are rejected with the following diagnoses:
21653
21654 @quotation
21655
21656 @example
21657 Distance := 5.0;
21658 >>> dimensions mismatch in assignment
21659 >>> left-hand side has dimension [L]
21660 >>> right-hand side is dimensionless
21661
21662 Distance := 5.0 * kg:
21663 >>> dimensions mismatch in assignment
21664 >>> left-hand side has dimension [L]
21665 >>> right-hand side has dimension [M]
21666 @end example
21667 @end quotation
21668
21669 The dimensions of an expression are properly displayed, even if there is
21670 no explicit subtype for it. If we add to the program:
21671
21672 @quotation
21673
21674 @example
21675 Put ("Final velocity: ");
21676 Put (G * T, Aft =>2, Exp =>0);
21677 Put_Line ("");
21678 @end example
21679 @end quotation
21680
21681 then the output includes:
21682
21683 @quotation
21684
21685 @example
21686 Final velocity: 98.10 m.s**(-1)
21687 @end example
21688
21689 @geindex Dimensionable type
21690
21691 @geindex Dimensioned subtype
21692 @end quotation
21693
21694 The type @code{Mks_Type} is said to be a @emph{dimensionable type} since it has a
21695 @code{Dimension_System} aspect, and the subtypes @code{Length}, @code{Mass}, etc.,
21696 are said to be @emph{dimensioned subtypes} since each one has a @code{Dimension}
21697 aspect.
21698
21699 @quotation
21700
21701 @geindex Dimension Vector (for a dimensioned subtype)
21702
21703 @geindex Dimension aspect
21704
21705 @geindex Dimension_System aspect
21706 @end quotation
21707
21708 The @code{Dimension} aspect of a dimensioned subtype @code{S} defines a mapping
21709 from the base type’s Unit_Names to integer (or, more generally, rational)
21710 values. This mapping is the @emph{dimension vector} (also referred to as the
21711 @emph{dimensionality}) for that subtype, denoted by @code{DV(S)}, and thus for each
21712 object of that subtype. Intuitively, the value specified for each
21713 @code{Unit_Name} is the exponent associated with that unit; a zero value
21714 means that the unit is not used. For example:
21715
21716 @quotation
21717
21718 @example
21719 declare
21720 Acc : Acceleration;
21721 ...
21722 begin
21723 ...
21724 end;
21725 @end example
21726 @end quotation
21727
21728 Here @code{DV(Acc)} = @code{DV(Acceleration)} =
21729 @code{(Meter=>1, Kilogram=>0, Second=>-2, Ampere=>0, Kelvin=>0, Mole=>0, Candela=>0)}.
21730 Symbolically, we can express this as @code{Meter / Second**2}.
21731
21732 The dimension vector of an arithmetic expression is synthesized from the
21733 dimension vectors of its components, with compile-time dimensionality checks
21734 that help prevent mismatches such as using an @code{Acceleration} where a
21735 @code{Length} is required.
21736
21737 The dimension vector of the result of an arithmetic expression @emph{expr}, or
21738 @code{DV(@emph{expr})}, is defined as follows, assuming conventional
21739 mathematical definitions for the vector operations that are used:
21740
21741
21742 @itemize *
21743
21744 @item
21745 If @emph{expr} is of the type @emph{universal_real}, or is not of a dimensioned subtype,
21746 then @emph{expr} is dimensionless; @code{DV(@emph{expr})} is the empty vector.
21747
21748 @item
21749 @code{DV(@emph{op expr})}, where @emph{op} is a unary operator, is @code{DV(@emph{expr})}
21750
21751 @item
21752 @code{DV(@emph{expr1 op expr2})} where @emph{op} is “+” or “-” is @code{DV(@emph{expr1})}
21753 provided that @code{DV(@emph{expr1})} = @code{DV(@emph{expr2})}.
21754 If this condition is not met then the construct is illegal.
21755
21756 @item
21757 @code{DV(@emph{expr1} * @emph{expr2})} is @code{DV(@emph{expr1})} + @code{DV(@emph{expr2})},
21758 and @code{DV(@emph{expr1} / @emph{expr2})} = @code{DV(@emph{expr1})} - @code{DV(@emph{expr2})}.
21759 In this context if one of the @emph{expr}s is dimensionless then its empty
21760 dimension vector is treated as @code{(others => 0)}.
21761
21762 @item
21763 @code{DV(@emph{expr} ** @emph{power})} is @emph{power} * @code{DV(@emph{expr})},
21764 provided that @emph{power} is a static rational value. If this condition is not
21765 met then the construct is illegal.
21766 @end itemize
21767
21768 Note that, by the above rules, it is illegal to use binary “+” or “-” to
21769 combine a dimensioned and dimensionless value. Thus an expression such as
21770 @code{acc-10.0} is illegal, where @code{acc} is an object of subtype
21771 @code{Acceleration}.
21772
21773 The dimensionality checks for relationals use the same rules as
21774 for “+” and “-“, except when comparing to a literal; thus
21775
21776 @quotation
21777
21778 @example
21779 acc > len
21780 @end example
21781 @end quotation
21782
21783 is equivalent to
21784
21785 @quotation
21786
21787 @example
21788 acc-len > 0.0
21789 @end example
21790 @end quotation
21791
21792 and is thus illegal, but
21793
21794 @quotation
21795
21796 @example
21797 acc > 10.0
21798 @end example
21799 @end quotation
21800
21801 is accepted with a warning. Analogously a conditional expression requires the
21802 same dimension vector for each branch (with no exception for literals).
21803
21804 The dimension vector of a type conversion @code{T(@emph{expr})} is defined
21805 as follows, based on the nature of @code{T}:
21806
21807
21808 @itemize *
21809
21810 @item
21811 If @code{T} is a dimensioned subtype then @code{DV(T(@emph{expr}))} is @code{DV(T)}
21812 provided that either @emph{expr} is dimensionless or
21813 @code{DV(T)} = @code{DV(@emph{expr})}. The conversion is illegal
21814 if @emph{expr} is dimensioned and @code{DV(@emph{expr})} /= @code{DV(T)}.
21815 Note that vector equality does not require that the corresponding
21816 Unit_Names be the same.
21817
21818 As a consequence of the above rule, it is possible to convert between
21819 different dimension systems that follow the same international system
21820 of units, with the seven physical components given in the standard order
21821 (length, mass, time, etc.). Thus a length in meters can be converted to
21822 a length in inches (with a suitable conversion factor) but cannot be
21823 converted, for example, to a mass in pounds.
21824
21825 @item
21826 If @code{T} is the base type for @emph{expr} (and the dimensionless root type of
21827 the dimension system), then @code{DV(T(@emph{expr}))} is @code{DV(expr)}.
21828 Thus, if @emph{expr} is of a dimensioned subtype of @code{T}, the conversion may
21829 be regarded as a “view conversion” that preserves dimensionality.
21830
21831 This rule makes it possible to write generic code that can be instantiated
21832 with compatible dimensioned subtypes. The generic unit will contain
21833 conversions that will consequently be present in instantiations, but
21834 conversions to the base type will preserve dimensionality and make it
21835 possible to write generic code that is correct with respect to
21836 dimensionality.
21837
21838 @item
21839 Otherwise (i.e., @code{T} is neither a dimensioned subtype nor a dimensionable
21840 base type), @code{DV(T(@emph{expr}))} is the empty vector. Thus a dimensioned
21841 value can be explicitly converted to a non-dimensioned subtype, which
21842 of course then escapes dimensionality analysis.
21843 @end itemize
21844
21845 The dimension vector for a type qualification @code{T'(@emph{expr})} is the same
21846 as for the type conversion @code{T(@emph{expr})}.
21847
21848 An assignment statement
21849
21850 @quotation
21851
21852 @example
21853 Source := Target;
21854 @end example
21855 @end quotation
21856
21857 requires @code{DV(Source)} = @code{DV(Target)}, and analogously for parameter
21858 passing (the dimension vector for the actual parameter must be equal to the
21859 dimension vector for the formal parameter).
21860
21861 @node Stack Related Facilities,Memory Management Issues,Performing Dimensionality Analysis in GNAT,GNAT and Program Execution
21862 @anchor{gnat_ugn/gnat_and_program_execution id52}@anchor{14b}@anchor{gnat_ugn/gnat_and_program_execution stack-related-facilities}@anchor{1a8}
21863 @section Stack Related Facilities
21864
21865
21866 This section describes some useful tools associated with stack
21867 checking and analysis. In
21868 particular, it deals with dynamic and static stack usage measurements.
21869
21870 @menu
21871 * Stack Overflow Checking::
21872 * Static Stack Usage Analysis::
21873 * Dynamic Stack Usage Analysis::
21874
21875 @end menu
21876
21877 @node Stack Overflow Checking,Static Stack Usage Analysis,,Stack Related Facilities
21878 @anchor{gnat_ugn/gnat_and_program_execution id53}@anchor{1a9}@anchor{gnat_ugn/gnat_and_program_execution stack-overflow-checking}@anchor{e5}
21879 @subsection Stack Overflow Checking
21880
21881
21882 @geindex Stack Overflow Checking
21883
21884 @geindex -fstack-check (gcc)
21885
21886 For most operating systems, @code{gcc} does not perform stack overflow
21887 checking by default. This means that if the main environment task or
21888 some other task exceeds the available stack space, then unpredictable
21889 behavior will occur. Most native systems offer some level of protection by
21890 adding a guard page at the end of each task stack. This mechanism is usually
21891 not enough for dealing properly with stack overflow situations because
21892 a large local variable could “jump” above the guard page.
21893 Furthermore, when the
21894 guard page is hit, there may not be any space left on the stack for executing
21895 the exception propagation code. Enabling stack checking avoids
21896 such situations.
21897
21898 To activate stack checking, compile all units with the @code{gcc} option
21899 @code{-fstack-check}. For example:
21900
21901 @quotation
21902
21903 @example
21904 $ gcc -c -fstack-check package1.adb
21905 @end example
21906 @end quotation
21907
21908 Units compiled with this option will generate extra instructions to check
21909 that any use of the stack (for procedure calls or for declaring local
21910 variables in declare blocks) does not exceed the available stack space.
21911 If the space is exceeded, then a @code{Storage_Error} exception is raised.
21912
21913 For declared tasks, the default stack size is defined by the GNAT runtime,
21914 whose size may be modified at bind time through the @code{-d} bind switch
21915 (@ref{110,,Switches for gnatbind}). Task specific stack sizes may be set using the
21916 @code{Storage_Size} pragma.
21917
21918 For the environment task, the stack size is determined by the operating system.
21919 Consequently, to modify the size of the environment task please refer to your
21920 operating system documentation.
21921
21922 @node Static Stack Usage Analysis,Dynamic Stack Usage Analysis,Stack Overflow Checking,Stack Related Facilities
21923 @anchor{gnat_ugn/gnat_and_program_execution id54}@anchor{1aa}@anchor{gnat_ugn/gnat_and_program_execution static-stack-usage-analysis}@anchor{e6}
21924 @subsection Static Stack Usage Analysis
21925
21926
21927 @geindex Static Stack Usage Analysis
21928
21929 @geindex -fstack-usage
21930
21931 A unit compiled with @code{-fstack-usage} will generate an extra file
21932 that specifies
21933 the maximum amount of stack used, on a per-function basis.
21934 The file has the same
21935 basename as the target object file with a @code{.su} extension.
21936 Each line of this file is made up of three fields:
21937
21938
21939 @itemize *
21940
21941 @item
21942 The name of the function.
21943
21944 @item
21945 A number of bytes.
21946
21947 @item
21948 One or more qualifiers: @code{static}, @code{dynamic}, @code{bounded}.
21949 @end itemize
21950
21951 The second field corresponds to the size of the known part of the function
21952 frame.
21953
21954 The qualifier @code{static} means that the function frame size
21955 is purely static.
21956 It usually means that all local variables have a static size.
21957 In this case, the second field is a reliable measure of the function stack
21958 utilization.
21959
21960 The qualifier @code{dynamic} means that the function frame size is not static.
21961 It happens mainly when some local variables have a dynamic size. When this
21962 qualifier appears alone, the second field is not a reliable measure
21963 of the function stack analysis. When it is qualified with @code{bounded}, it
21964 means that the second field is a reliable maximum of the function stack
21965 utilization.
21966
21967 A unit compiled with @code{-Wstack-usage} will issue a warning for each
21968 subprogram whose stack usage might be larger than the specified amount of
21969 bytes. The wording is in keeping with the qualifier documented above.
21970
21971 @node Dynamic Stack Usage Analysis,,Static Stack Usage Analysis,Stack Related Facilities
21972 @anchor{gnat_ugn/gnat_and_program_execution dynamic-stack-usage-analysis}@anchor{113}@anchor{gnat_ugn/gnat_and_program_execution id55}@anchor{1ab}
21973 @subsection Dynamic Stack Usage Analysis
21974
21975
21976 It is possible to measure the maximum amount of stack used by a task, by
21977 adding a switch to @code{gnatbind}, as:
21978
21979 @quotation
21980
21981 @example
21982 $ gnatbind -u0 file
21983 @end example
21984 @end quotation
21985
21986 With this option, at each task termination, its stack usage is output on
21987 @code{stderr}.
21988 Note that this switch is not compatible with tools like
21989 Valgrind and DrMemory; they will report errors.
21990
21991 It is not always convenient to output the stack usage when the program
21992 is still running. Hence, it is possible to delay this output until program
21993 termination. for a given number of tasks specified as the argument of the
21994 @code{-u} option. For instance:
21995
21996 @quotation
21997
21998 @example
21999 $ gnatbind -u100 file
22000 @end example
22001 @end quotation
22002
22003 will buffer the stack usage information of the first 100 tasks to terminate and
22004 output this info at program termination. Results are displayed in four
22005 columns:
22006
22007 @quotation
22008
22009 @example
22010 Index | Task Name | Stack Size | Stack Usage
22011 @end example
22012 @end quotation
22013
22014 where:
22015
22016
22017 @itemize *
22018
22019 @item
22020 @emph{Index} is a number associated with each task.
22021
22022 @item
22023 @emph{Task Name} is the name of the task analyzed.
22024
22025 @item
22026 @emph{Stack Size} is the maximum size for the stack.
22027
22028 @item
22029 @emph{Stack Usage} is the measure done by the stack analyzer.
22030 In order to prevent overflow, the stack
22031 is not entirely analyzed, and it’s not possible to know exactly how
22032 much has actually been used.
22033 @end itemize
22034
22035 By default the environment task stack, the stack that contains the main unit,
22036 is not processed. To enable processing of the environment task stack, the
22037 environment variable GNAT_STACK_LIMIT needs to be set to the maximum size of
22038 the environment task stack. This amount is given in kilobytes. For example:
22039
22040 @quotation
22041
22042 @example
22043 $ set GNAT_STACK_LIMIT 1600
22044 @end example
22045 @end quotation
22046
22047 would specify to the analyzer that the environment task stack has a limit
22048 of 1.6 megabytes. Any stack usage beyond this will be ignored by the analysis.
22049
22050 The package @code{GNAT.Task_Stack_Usage} provides facilities to get
22051 stack-usage reports at run time. See its body for the details.
22052
22053 @node Memory Management Issues,,Stack Related Facilities,GNAT and Program Execution
22054 @anchor{gnat_ugn/gnat_and_program_execution id56}@anchor{14c}@anchor{gnat_ugn/gnat_and_program_execution memory-management-issues}@anchor{1ac}
22055 @section Memory Management Issues
22056
22057
22058 This section describes some useful memory pools provided in the GNAT library
22059 and in particular the GNAT Debug Pool facility, which can be used to detect
22060 incorrect uses of access values (including ‘dangling references’).
22061
22062
22063 @menu
22064 * Some Useful Memory Pools::
22065 * The GNAT Debug Pool Facility::
22066
22067 @end menu
22068
22069 @node Some Useful Memory Pools,The GNAT Debug Pool Facility,,Memory Management Issues
22070 @anchor{gnat_ugn/gnat_and_program_execution id57}@anchor{1ad}@anchor{gnat_ugn/gnat_and_program_execution some-useful-memory-pools}@anchor{1ae}
22071 @subsection Some Useful Memory Pools
22072
22073
22074 @geindex Memory Pool
22075
22076 @geindex storage
22077 @geindex pool
22078
22079 The @code{System.Pool_Global} package offers the Unbounded_No_Reclaim_Pool
22080 storage pool. Allocations use the standard system call @code{malloc} while
22081 deallocations use the standard system call @code{free}. No reclamation is
22082 performed when the pool goes out of scope. For performance reasons, the
22083 standard default Ada allocators/deallocators do not use any explicit storage
22084 pools but if they did, they could use this storage pool without any change in
22085 behavior. That is why this storage pool is used when the user
22086 manages to make the default implicit allocator explicit as in this example:
22087
22088 @quotation
22089
22090 @example
22091 type T1 is access Something;
22092 -- no Storage pool is defined for T2
22093
22094 type T2 is access Something_Else;
22095 for T2'Storage_Pool use T1'Storage_Pool;
22096 -- the above is equivalent to
22097 for T2'Storage_Pool use System.Pool_Global.Global_Pool_Object;
22098 @end example
22099 @end quotation
22100
22101 The @code{System.Pool_Local} package offers the @code{Unbounded_Reclaim_Pool} storage
22102 pool. The allocation strategy is similar to @code{Pool_Local}
22103 except that the all
22104 storage allocated with this pool is reclaimed when the pool object goes out of
22105 scope. This pool provides a explicit mechanism similar to the implicit one
22106 provided by several Ada 83 compilers for allocations performed through a local
22107 access type and whose purpose was to reclaim memory when exiting the
22108 scope of a given local access. As an example, the following program does not
22109 leak memory even though it does not perform explicit deallocation:
22110
22111 @quotation
22112
22113 @example
22114 with System.Pool_Local;
22115 procedure Pooloc1 is
22116 procedure Internal is
22117 type A is access Integer;
22118 X : System.Pool_Local.Unbounded_Reclaim_Pool;
22119 for A'Storage_Pool use X;
22120 v : A;
22121 begin
22122 for I in 1 .. 50 loop
22123 v := new Integer;
22124 end loop;
22125 end Internal;
22126 begin
22127 for I in 1 .. 100 loop
22128 Internal;
22129 end loop;
22130 end Pooloc1;
22131 @end example
22132 @end quotation
22133
22134 The @code{System.Pool_Size} package implements the @code{Stack_Bounded_Pool} used when
22135 @code{Storage_Size} is specified for an access type.
22136 The whole storage for the pool is
22137 allocated at once, usually on the stack at the point where the access type is
22138 elaborated. It is automatically reclaimed when exiting the scope where the
22139 access type is defined. This package is not intended to be used directly by the
22140 user and it is implicitly used for each such declaration:
22141
22142 @quotation
22143
22144 @example
22145 type T1 is access Something;
22146 for T1'Storage_Size use 10_000;
22147 @end example
22148 @end quotation
22149
22150 @node The GNAT Debug Pool Facility,,Some Useful Memory Pools,Memory Management Issues
22151 @anchor{gnat_ugn/gnat_and_program_execution id58}@anchor{1af}@anchor{gnat_ugn/gnat_and_program_execution the-gnat-debug-pool-facility}@anchor{1b0}
22152 @subsection The GNAT Debug Pool Facility
22153
22154
22155 @geindex Debug Pool
22156
22157 @geindex storage
22158 @geindex pool
22159 @geindex memory corruption
22160
22161 The use of unchecked deallocation and unchecked conversion can easily
22162 lead to incorrect memory references. The problems generated by such
22163 references are usually difficult to tackle because the symptoms can be
22164 very remote from the origin of the problem. In such cases, it is
22165 very helpful to detect the problem as early as possible. This is the
22166 purpose of the Storage Pool provided by @code{GNAT.Debug_Pools}.
22167
22168 In order to use the GNAT specific debugging pool, the user must
22169 associate a debug pool object with each of the access types that may be
22170 related to suspected memory problems. See Ada Reference Manual 13.11.
22171
22172 @quotation
22173
22174 @example
22175 type Ptr is access Some_Type;
22176 Pool : GNAT.Debug_Pools.Debug_Pool;
22177 for Ptr'Storage_Pool use Pool;
22178 @end example
22179 @end quotation
22180
22181 @code{GNAT.Debug_Pools} is derived from a GNAT-specific kind of
22182 pool: the @code{Checked_Pool}. Such pools, like standard Ada storage pools,
22183 allow the user to redefine allocation and deallocation strategies. They
22184 also provide a checkpoint for each dereference, through the use of
22185 the primitive operation @code{Dereference} which is implicitly called at
22186 each dereference of an access value.
22187
22188 Once an access type has been associated with a debug pool, operations on
22189 values of the type may raise four distinct exceptions,
22190 which correspond to four potential kinds of memory corruption:
22191
22192
22193 @itemize *
22194
22195 @item
22196 @code{GNAT.Debug_Pools.Accessing_Not_Allocated_Storage}
22197
22198 @item
22199 @code{GNAT.Debug_Pools.Accessing_Deallocated_Storage}
22200
22201 @item
22202 @code{GNAT.Debug_Pools.Freeing_Not_Allocated_Storage}
22203
22204 @item
22205 @code{GNAT.Debug_Pools.Freeing_Deallocated_Storage}
22206 @end itemize
22207
22208 For types associated with a Debug_Pool, dynamic allocation is performed using
22209 the standard GNAT allocation routine. References to all allocated chunks of
22210 memory are kept in an internal dictionary. Several deallocation strategies are
22211 provided, whereupon the user can choose to release the memory to the system,
22212 keep it allocated for further invalid access checks, or fill it with an easily
22213 recognizable pattern for debug sessions. The memory pattern is the old IBM
22214 hexadecimal convention: @code{16#DEADBEEF#}.
22215
22216 See the documentation in the file g-debpoo.ads for more information on the
22217 various strategies.
22218
22219 Upon each dereference, a check is made that the access value denotes a
22220 properly allocated memory location. Here is a complete example of use of
22221 @code{Debug_Pools}, that includes typical instances of memory corruption:
22222
22223 @quotation
22224
22225 @example
22226 with Gnat.Io; use Gnat.Io;
22227 with Unchecked_Deallocation;
22228 with Unchecked_Conversion;
22229 with GNAT.Debug_Pools;
22230 with System.Storage_Elements;
22231 with Ada.Exceptions; use Ada.Exceptions;
22232 procedure Debug_Pool_Test is
22233
22234 type T is access Integer;
22235 type U is access all T;
22236
22237 P : GNAT.Debug_Pools.Debug_Pool;
22238 for T'Storage_Pool use P;
22239
22240 procedure Free is new Unchecked_Deallocation (Integer, T);
22241 function UC is new Unchecked_Conversion (U, T);
22242 A, B : aliased T;
22243
22244 procedure Info is new GNAT.Debug_Pools.Print_Info(Put_Line);
22245
22246 begin
22247 Info (P);
22248 A := new Integer;
22249 B := new Integer;
22250 B := A;
22251 Info (P);
22252 Free (A);
22253 begin
22254 Put_Line (Integer'Image(B.all));
22255 exception
22256 when E : others => Put_Line ("raised: " & Exception_Name (E));
22257 end;
22258 begin
22259 Free (B);
22260 exception
22261 when E : others => Put_Line ("raised: " & Exception_Name (E));
22262 end;
22263 B := UC(A'Access);
22264 begin
22265 Put_Line (Integer'Image(B.all));
22266 exception
22267 when E : others => Put_Line ("raised: " & Exception_Name (E));
22268 end;
22269 begin
22270 Free (B);
22271 exception
22272 when E : others => Put_Line ("raised: " & Exception_Name (E));
22273 end;
22274 Info (P);
22275 end Debug_Pool_Test;
22276 @end example
22277 @end quotation
22278
22279 The debug pool mechanism provides the following precise diagnostics on the
22280 execution of this erroneous program:
22281
22282 @quotation
22283
22284 @example
22285 Debug Pool info:
22286 Total allocated bytes : 0
22287 Total deallocated bytes : 0
22288 Current Water Mark: 0
22289 High Water Mark: 0
22290
22291 Debug Pool info:
22292 Total allocated bytes : 8
22293 Total deallocated bytes : 0
22294 Current Water Mark: 8
22295 High Water Mark: 8
22296
22297 raised: GNAT.DEBUG_POOLS.ACCESSING_DEALLOCATED_STORAGE
22298 raised: GNAT.DEBUG_POOLS.FREEING_DEALLOCATED_STORAGE
22299 raised: GNAT.DEBUG_POOLS.ACCESSING_NOT_ALLOCATED_STORAGE
22300 raised: GNAT.DEBUG_POOLS.FREEING_NOT_ALLOCATED_STORAGE
22301 Debug Pool info:
22302 Total allocated bytes : 8
22303 Total deallocated bytes : 4
22304 Current Water Mark: 4
22305 High Water Mark: 8
22306 @end example
22307 @end quotation
22308
22309
22310 @c -- Non-breaking space in running text
22311 @c -- E.g. Ada |nbsp| 95
22312
22313 @node Platform-Specific Information,Example of Binder Output File,GNAT and Program Execution,Top
22314 @anchor{gnat_ugn/platform_specific_information doc}@anchor{1b1}@anchor{gnat_ugn/platform_specific_information id1}@anchor{1b2}@anchor{gnat_ugn/platform_specific_information platform-specific-information}@anchor{d}
22315 @chapter Platform-Specific Information
22316
22317
22318 This appendix contains information relating to the implementation
22319 of run-time libraries on various platforms and also covers
22320 topics related to the GNAT implementation on Windows and Mac OS.
22321
22322 @menu
22323 * Run-Time Libraries::
22324 * Specifying a Run-Time Library::
22325 * GNU/Linux Topics::
22326 * Microsoft Windows Topics::
22327 * Mac OS Topics::
22328
22329 @end menu
22330
22331 @node Run-Time Libraries,Specifying a Run-Time Library,,Platform-Specific Information
22332 @anchor{gnat_ugn/platform_specific_information id2}@anchor{1b3}@anchor{gnat_ugn/platform_specific_information run-time-libraries}@anchor{1b4}
22333 @section Run-Time Libraries
22334
22335
22336 @geindex Tasking and threads libraries
22337
22338 @geindex Threads libraries and tasking
22339
22340 @geindex Run-time libraries (platform-specific information)
22341
22342 The GNAT run-time implementation may vary with respect to both the
22343 underlying threads library and the exception-handling scheme.
22344 For threads support, the default run-time will bind to the thread
22345 package of the underlying operating system.
22346
22347 For exception handling, either or both of two models are supplied:
22348
22349 @quotation
22350
22351 @geindex Zero-Cost Exceptions
22352
22353 @geindex ZCX (Zero-Cost Exceptions)
22354 @end quotation
22355
22356
22357 @itemize *
22358
22359 @item
22360 @strong{Zero-Cost Exceptions} (“ZCX”),
22361 which uses binder-generated tables that
22362 are interrogated at run time to locate a handler.
22363
22364 @geindex setjmp/longjmp Exception Model
22365
22366 @geindex SJLJ (setjmp/longjmp Exception Model)
22367
22368 @item
22369 @strong{setjmp / longjmp} (‘SJLJ’),
22370 which uses dynamically-set data to establish
22371 the set of handlers
22372 @end itemize
22373
22374 Most programs should experience a substantial speed improvement by
22375 being compiled with a ZCX run-time.
22376 This is especially true for
22377 tasking applications or applications with many exception handlers.
22378 Note however that the ZCX run-time does not support asynchronous abort
22379 of tasks (@code{abort} and @code{select-then-abort} constructs) and will instead
22380 implement abort by polling points in the runtime. You can also add additional
22381 polling points explicitly if needed in your application via @code{pragma
22382 Abort_Defer}.
22383
22384 This section summarizes which combinations of threads and exception support
22385 are supplied on various GNAT platforms.
22386
22387 @menu
22388 * Summary of Run-Time Configurations::
22389
22390 @end menu
22391
22392 @node Summary of Run-Time Configurations,,,Run-Time Libraries
22393 @anchor{gnat_ugn/platform_specific_information id3}@anchor{1b5}@anchor{gnat_ugn/platform_specific_information summary-of-run-time-configurations}@anchor{1b6}
22394 @subsection Summary of Run-Time Configurations
22395
22396
22397
22398 @multitable {xxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxx}
22399 @headitem
22400
22401 Platform
22402
22403 @tab
22404
22405 Run-Time
22406
22407 @tab
22408
22409 Tasking
22410
22411 @tab
22412
22413 Exceptions
22414
22415 @item
22416
22417 GNU/Linux
22418
22419 @tab
22420
22421 rts-native
22422 (default)
22423
22424 @tab
22425
22426 pthread library
22427
22428 @tab
22429
22430 ZCX
22431
22432 @item
22433
22434 rts-sjlj
22435
22436 @tab
22437
22438 pthread library
22439
22440 @tab
22441
22442 SJLJ
22443
22444 @item
22445
22446 Windows
22447
22448 @tab
22449
22450 rts-native
22451 (default)
22452
22453 @tab
22454
22455 native Win32 threads
22456
22457 @tab
22458
22459 ZCX
22460
22461 @item
22462
22463 rts-sjlj
22464
22465 @tab
22466
22467 native Win32 threads
22468
22469 @tab
22470
22471 SJLJ
22472
22473 @item
22474
22475 Mac OS
22476
22477 @tab
22478
22479 rts-native
22480
22481 @tab
22482
22483 pthread library
22484
22485 @tab
22486
22487 ZCX
22488
22489 @end multitable
22490
22491
22492 @node Specifying a Run-Time Library,GNU/Linux Topics,Run-Time Libraries,Platform-Specific Information
22493 @anchor{gnat_ugn/platform_specific_information id4}@anchor{1b7}@anchor{gnat_ugn/platform_specific_information specifying-a-run-time-library}@anchor{1b8}
22494 @section Specifying a Run-Time Library
22495
22496
22497 The @code{adainclude} subdirectory containing the sources of the GNAT
22498 run-time library, and the @code{adalib} subdirectory containing the
22499 @code{ALI} files and the static and/or shared GNAT library, are located
22500 in the gcc target-dependent area:
22501
22502 @quotation
22503
22504 @example
22505 target=$prefix/lib/gcc/gcc-*dumpmachine*/gcc-*dumpversion*/
22506 @end example
22507 @end quotation
22508
22509 As indicated above, on some platforms several run-time libraries are supplied.
22510 These libraries are installed in the target dependent area and
22511 contain a complete source and binary subdirectory. The detailed description
22512 below explains the differences between the different libraries in terms of
22513 their thread support.
22514
22515 The default run-time library (when GNAT is installed) is @emph{rts-native}.
22516 This default run-time is selected by the means of soft links.
22517 For example on x86-linux:
22518
22519 @c --
22520 @c -- $(target-dir)
22521 @c -- |
22522 @c -- +--- adainclude----------+
22523 @c -- | |
22524 @c -- +--- adalib-----------+ |
22525 @c -- | | |
22526 @c -- +--- rts-native | |
22527 @c -- | | | |
22528 @c -- | +--- adainclude <---+
22529 @c -- | | |
22530 @c -- | +--- adalib <----+
22531 @c -- |
22532 @c -- +--- rts-sjlj
22533 @c -- |
22534 @c -- +--- adainclude
22535 @c -- |
22536 @c -- +--- adalib
22537
22538
22539 @example
22540 $(target-dir)
22541 __/ / \ \___
22542 _______/ / \ \_________________
22543 / / \ \
22544 / / \ \
22545 ADAINCLUDE ADALIB rts-native rts-sjlj
22546 : : / \ / \
22547 : : / \ / \
22548 : : / \ / \
22549 : : / \ / \
22550 +-------------> adainclude adalib adainclude adalib
22551 : ^
22552 : :
22553 +---------------------+
22554
22555 Run-Time Library Directory Structure
22556 (Upper-case names and dotted/dashed arrows represent soft links)
22557 @end example
22558
22559 If the @emph{rts-sjlj} library is to be selected on a permanent basis,
22560 these soft links can be modified with the following commands:
22561
22562 @quotation
22563
22564 @example
22565 $ cd $target
22566 $ rm -f adainclude adalib
22567 $ ln -s rts-sjlj/adainclude adainclude
22568 $ ln -s rts-sjlj/adalib adalib
22569 @end example
22570 @end quotation
22571
22572 Alternatively, you can specify @code{rts-sjlj/adainclude} in the file
22573 @code{$target/ada_source_path} and @code{rts-sjlj/adalib} in
22574 @code{$target/ada_object_path}.
22575
22576 @geindex --RTS option
22577
22578 Selecting another run-time library temporarily can be
22579 achieved by using the @code{--RTS} switch, e.g., @code{--RTS=sjlj}
22580 @anchor{gnat_ugn/platform_specific_information choosing-the-scheduling-policy}@anchor{1b9}
22581 @geindex SCHED_FIFO scheduling policy
22582
22583 @geindex SCHED_RR scheduling policy
22584
22585 @geindex SCHED_OTHER scheduling policy
22586
22587 @menu
22588 * Choosing the Scheduling Policy::
22589
22590 @end menu
22591
22592 @node Choosing the Scheduling Policy,,,Specifying a Run-Time Library
22593 @anchor{gnat_ugn/platform_specific_information id5}@anchor{1ba}
22594 @subsection Choosing the Scheduling Policy
22595
22596
22597 When using a POSIX threads implementation, you have a choice of several
22598 scheduling policies: @code{SCHED_FIFO}, @code{SCHED_RR} and @code{SCHED_OTHER}.
22599
22600 Typically, the default is @code{SCHED_OTHER}, while using @code{SCHED_FIFO}
22601 or @code{SCHED_RR} requires special (e.g., root) privileges.
22602
22603 @geindex pragma Time_Slice
22604
22605 @geindex -T0 option
22606
22607 @geindex pragma Task_Dispatching_Policy
22608
22609 By default, GNAT uses the @code{SCHED_OTHER} policy. To specify
22610 @code{SCHED_FIFO},
22611 you can use one of the following:
22612
22613
22614 @itemize *
22615
22616 @item
22617 @code{pragma Time_Slice (0.0)}
22618
22619 @item
22620 the corresponding binder option @code{-T0}
22621
22622 @item
22623 @code{pragma Task_Dispatching_Policy (FIFO_Within_Priorities)}
22624 @end itemize
22625
22626 To specify @code{SCHED_RR},
22627 you should use @code{pragma Time_Slice} with a
22628 value greater than 0.0, or else use the corresponding @code{-T}
22629 binder option.
22630
22631 To make sure a program is running as root, you can put something like
22632 this in a library package body in your application:
22633
22634 @quotation
22635
22636 @example
22637 function geteuid return Integer;
22638 pragma Import (C, geteuid, "geteuid");
22639 Ignore : constant Boolean :=
22640 (if geteuid = 0 then True else raise Program_Error with "must be root");
22641 @end example
22642 @end quotation
22643
22644 It gets the effective user id, and if it’s not 0 (i.e. root), it raises
22645 Program_Error.
22646
22647 @geindex Linux
22648
22649 @geindex GNU/Linux
22650
22651 @node GNU/Linux Topics,Microsoft Windows Topics,Specifying a Run-Time Library,Platform-Specific Information
22652 @anchor{gnat_ugn/platform_specific_information gnu-linux-topics}@anchor{1bb}@anchor{gnat_ugn/platform_specific_information id6}@anchor{1bc}
22653 @section GNU/Linux Topics
22654
22655
22656 This section describes topics that are specific to GNU/Linux platforms.
22657
22658 @menu
22659 * Required Packages on GNU/Linux::
22660
22661 @end menu
22662
22663 @node Required Packages on GNU/Linux,,,GNU/Linux Topics
22664 @anchor{gnat_ugn/platform_specific_information id7}@anchor{1bd}@anchor{gnat_ugn/platform_specific_information required-packages-on-gnu-linux}@anchor{1be}
22665 @subsection Required Packages on GNU/Linux
22666
22667
22668 GNAT requires the C library developer’s package to be installed.
22669 The name of of that package depends on your GNU/Linux distribution:
22670
22671
22672 @itemize *
22673
22674 @item
22675 RedHat, SUSE: @code{glibc-devel};
22676
22677 @item
22678 Debian, Ubuntu: @code{libc6-dev} (normally installed by default).
22679 @end itemize
22680
22681 If using the 32-bit version of GNAT on a 64-bit version of GNU/Linux,
22682 you’ll need the 32-bit version of the following packages:
22683
22684
22685 @itemize *
22686
22687 @item
22688 RedHat, SUSE: @code{glibc.i686}, @code{glibc-devel.i686}, @code{ncurses-libs.i686}
22689
22690 @item
22691 Debian, Ubuntu: @code{libc6:i386}, @code{libc6-dev:i386}, @code{lib32ncursesw5}
22692 @end itemize
22693
22694 Other GNU/Linux distributions might be choosing a different name
22695 for those packages.
22696
22697 @geindex Windows
22698
22699 @node Microsoft Windows Topics,Mac OS Topics,GNU/Linux Topics,Platform-Specific Information
22700 @anchor{gnat_ugn/platform_specific_information id8}@anchor{1bf}@anchor{gnat_ugn/platform_specific_information microsoft-windows-topics}@anchor{1c0}
22701 @section Microsoft Windows Topics
22702
22703
22704 This section describes topics that are specific to the Microsoft Windows
22705 platforms.
22706
22707
22708 @menu
22709 * Using GNAT on Windows::
22710 * Using a network installation of GNAT::
22711 * CONSOLE and WINDOWS subsystems::
22712 * Temporary Files::
22713 * Disabling Command Line Argument Expansion::
22714 * Windows Socket Timeouts::
22715 * Mixed-Language Programming on Windows::
22716 * Windows Specific Add-Ons::
22717
22718 @end menu
22719
22720 @node Using GNAT on Windows,Using a network installation of GNAT,,Microsoft Windows Topics
22721 @anchor{gnat_ugn/platform_specific_information id9}@anchor{1c1}@anchor{gnat_ugn/platform_specific_information using-gnat-on-windows}@anchor{1c2}
22722 @subsection Using GNAT on Windows
22723
22724
22725 One of the strengths of the GNAT technology is that its tool set
22726 (@code{gcc}, @code{gnatbind}, @code{gnatlink}, @code{gnatmake}, the
22727 @code{gdb} debugger, etc.) is used in the same way regardless of the
22728 platform.
22729
22730 On Windows this tool set is complemented by a number of Microsoft-specific
22731 tools that have been provided to facilitate interoperability with Windows
22732 when this is required. With these tools:
22733
22734
22735 @itemize *
22736
22737 @item
22738 You can build applications using the @code{CONSOLE} or @code{WINDOWS}
22739 subsystems.
22740
22741 @item
22742 You can use any Dynamically Linked Library (DLL) in your Ada code (both
22743 relocatable and non-relocatable DLLs are supported).
22744
22745 @item
22746 You can build Ada DLLs for use in other applications. These applications
22747 can be written in a language other than Ada (e.g., C, C++, etc). Again both
22748 relocatable and non-relocatable Ada DLLs are supported.
22749
22750 @item
22751 You can include Windows resources in your Ada application.
22752
22753 @item
22754 You can use or create COM/DCOM objects.
22755 @end itemize
22756
22757 Immediately below are listed all known general GNAT-for-Windows restrictions.
22758 Other restrictions about specific features like Windows Resources and DLLs
22759 are listed in separate sections below.
22760
22761
22762 @itemize *
22763
22764 @item
22765 It is not possible to use @code{GetLastError} and @code{SetLastError}
22766 when tasking, protected records, or exceptions are used. In these
22767 cases, in order to implement Ada semantics, the GNAT run-time system
22768 calls certain Win32 routines that set the last error variable to 0 upon
22769 success. It should be possible to use @code{GetLastError} and
22770 @code{SetLastError} when tasking, protected record, and exception
22771 features are not used, but it is not guaranteed to work.
22772
22773 @item
22774 It is not possible to link against Microsoft C++ libraries except for
22775 import libraries. Interfacing must be done by the mean of DLLs.
22776
22777 @item
22778 It is possible to link against Microsoft C libraries. Yet the preferred
22779 solution is to use C/C++ compiler that comes with GNAT, since it
22780 doesn’t require having two different development environments and makes the
22781 inter-language debugging experience smoother.
22782
22783 @item
22784 When the compilation environment is located on FAT32 drives, users may
22785 experience recompilations of the source files that have not changed if
22786 Daylight Saving Time (DST) state has changed since the last time files
22787 were compiled. NTFS drives do not have this problem.
22788
22789 @item
22790 No components of the GNAT toolset use any entries in the Windows
22791 registry. The only entries that can be created are file associations and
22792 PATH settings, provided the user has chosen to create them at installation
22793 time, as well as some minimal book-keeping information needed to correctly
22794 uninstall or integrate different GNAT products.
22795 @end itemize
22796
22797 @node Using a network installation of GNAT,CONSOLE and WINDOWS subsystems,Using GNAT on Windows,Microsoft Windows Topics
22798 @anchor{gnat_ugn/platform_specific_information id10}@anchor{1c3}@anchor{gnat_ugn/platform_specific_information using-a-network-installation-of-gnat}@anchor{1c4}
22799 @subsection Using a network installation of GNAT
22800
22801
22802 Make sure the system on which GNAT is installed is accessible from the
22803 current machine, i.e., the install location is shared over the network.
22804 Shared resources are accessed on Windows by means of UNC paths, which
22805 have the format @code{\\\\server\\sharename\\path}
22806
22807 In order to use such a network installation, simply add the UNC path of the
22808 @code{bin} directory of your GNAT installation in front of your PATH. For
22809 example, if GNAT is installed in @code{\GNAT} directory of a share location
22810 called @code{c-drive} on a machine @code{LOKI}, the following command will
22811 make it available:
22812
22813 @quotation
22814
22815 @example
22816 $ path \\loki\c-drive\gnat\bin;%path%`
22817 @end example
22818 @end quotation
22819
22820 Be aware that every compilation using the network installation results in the
22821 transfer of large amounts of data across the network and will likely cause
22822 serious performance penalty.
22823
22824 @node CONSOLE and WINDOWS subsystems,Temporary Files,Using a network installation of GNAT,Microsoft Windows Topics
22825 @anchor{gnat_ugn/platform_specific_information console-and-windows-subsystems}@anchor{1c5}@anchor{gnat_ugn/platform_specific_information id11}@anchor{1c6}
22826 @subsection CONSOLE and WINDOWS subsystems
22827
22828
22829 @geindex CONSOLE Subsystem
22830
22831 @geindex WINDOWS Subsystem
22832
22833 @geindex -mwindows
22834
22835 There are two main subsystems under Windows. The @code{CONSOLE} subsystem
22836 (which is the default subsystem) will always create a console when
22837 launching the application. This is not something desirable when the
22838 application has a Windows GUI. To get rid of this console the
22839 application must be using the @code{WINDOWS} subsystem. To do so
22840 the @code{-mwindows} linker option must be specified.
22841
22842 @quotation
22843
22844 @example
22845 $ gnatmake winprog -largs -mwindows
22846 @end example
22847 @end quotation
22848
22849 @node Temporary Files,Disabling Command Line Argument Expansion,CONSOLE and WINDOWS subsystems,Microsoft Windows Topics
22850 @anchor{gnat_ugn/platform_specific_information id12}@anchor{1c7}@anchor{gnat_ugn/platform_specific_information temporary-files}@anchor{1c8}
22851 @subsection Temporary Files
22852
22853
22854 @geindex Temporary files
22855
22856 It is possible to control where temporary files gets created by setting
22857 the
22858 @geindex TMP
22859 @geindex environment variable; TMP
22860 @code{TMP} environment variable. The file will be created:
22861
22862
22863 @itemize *
22864
22865 @item
22866 Under the directory pointed to by the
22867 @geindex TMP
22868 @geindex environment variable; TMP
22869 @code{TMP} environment variable if
22870 this directory exists.
22871
22872 @item
22873 Under @code{c:\temp}, if the
22874 @geindex TMP
22875 @geindex environment variable; TMP
22876 @code{TMP} environment variable is not
22877 set (or not pointing to a directory) and if this directory exists.
22878
22879 @item
22880 Under the current working directory otherwise.
22881 @end itemize
22882
22883 This allows you to determine exactly where the temporary
22884 file will be created. This is particularly useful in networked
22885 environments where you may not have write access to some
22886 directories.
22887
22888 @node Disabling Command Line Argument Expansion,Windows Socket Timeouts,Temporary Files,Microsoft Windows Topics
22889 @anchor{gnat_ugn/platform_specific_information disabling-command-line-argument-expansion}@anchor{1c9}
22890 @subsection Disabling Command Line Argument Expansion
22891
22892
22893 @geindex Command Line Argument Expansion
22894
22895 By default, an executable compiled for the Windows platform will do
22896 the following postprocessing on the arguments passed on the command
22897 line:
22898
22899
22900 @itemize *
22901
22902 @item
22903 If the argument contains the characters @code{*} and/or @code{?}, then
22904 file expansion will be attempted. For example, if the current directory
22905 contains @code{a.txt} and @code{b.txt}, then when calling:
22906
22907 @example
22908 $ my_ada_program *.txt
22909 @end example
22910
22911 The following arguments will effectively be passed to the main program
22912 (for example when using @code{Ada.Command_Line.Argument}):
22913
22914 @example
22915 Ada.Command_Line.Argument (1) -> "a.txt"
22916 Ada.Command_Line.Argument (2) -> "b.txt"
22917 @end example
22918
22919 @item
22920 Filename expansion can be disabled for a given argument by using single
22921 quotes. Thus, calling:
22922
22923 @example
22924 $ my_ada_program '*.txt'
22925 @end example
22926
22927 will result in:
22928
22929 @example
22930 Ada.Command_Line.Argument (1) -> "*.txt"
22931 @end example
22932 @end itemize
22933
22934 Note that if the program is launched from a shell such as Cygwin Bash
22935 then quote removal might be performed by the shell.
22936
22937 In some contexts it might be useful to disable this feature (for example if
22938 the program performs its own argument expansion). In order to do this, a C
22939 symbol needs to be defined and set to @code{0}. You can do this by
22940 adding the following code fragment in one of your Ada units:
22941
22942 @example
22943 Do_Argv_Expansion : Integer := 0;
22944 pragma Export (C, Do_Argv_Expansion, "__gnat_do_argv_expansion");
22945 @end example
22946
22947 The results of previous examples will be respectively:
22948
22949 @example
22950 Ada.Command_Line.Argument (1) -> "*.txt"
22951 @end example
22952
22953 and:
22954
22955 @example
22956 Ada.Command_Line.Argument (1) -> "'*.txt'"
22957 @end example
22958
22959 @node Windows Socket Timeouts,Mixed-Language Programming on Windows,Disabling Command Line Argument Expansion,Microsoft Windows Topics
22960 @anchor{gnat_ugn/platform_specific_information windows-socket-timeouts}@anchor{1ca}
22961 @subsection Windows Socket Timeouts
22962
22963
22964 Microsoft Windows desktops older than @code{8.0} and Microsoft Windows Servers
22965 older than @code{2019} set a socket timeout 500 milliseconds longer than the value
22966 set by setsockopt with @code{SO_RCVTIMEO} and @code{SO_SNDTIMEO} options. The GNAT
22967 runtime makes a correction for the difference in the corresponding Windows
22968 versions. For Windows Server starting with version @code{2019}, the user must
22969 provide a manifest file for the GNAT runtime to be able to recognize that
22970 the Windows version does not need the timeout correction. The manifest file
22971 should be located in the same directory as the executable file, and its file
22972 name must match the executable name suffixed by @code{.manifest}. For example,
22973 if the executable name is @code{sock_wto.exe}, then the manifest file name
22974 has to be @code{sock_wto.exe.manifest}. The manifest file must contain at
22975 least the following data:
22976
22977 @example
22978 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
22979 <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
22980 <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
22981 <application>
22982 <!-- Windows Vista -->
22983 <supportedOS Id="@{e2011457-1546-43c5-a5fe-008deee3d3f0@}"/>
22984 <!-- Windows 7 -->
22985 <supportedOS Id="@{35138b9a-5d96-4fbd-8e2d-a2440225f93a@}"/>
22986 <!-- Windows 8 -->
22987 <supportedOS Id="@{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38@}"/>
22988 <!-- Windows 8.1 -->
22989 <supportedOS Id="@{1f676c76-80e1-4239-95bb-83d0f6d0da78@}"/>
22990 <!-- Windows 10 -->
22991 <supportedOS Id="@{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a@}"/>
22992 </application>
22993 </compatibility>
22994 </assembly>
22995 @end example
22996
22997 Without the manifest file, the socket timeout is going to be overcorrected on
22998 these Windows Server versions and the actual time is going to be 500
22999 milliseconds shorter than what was set with GNAT.Sockets.Set_Socket_Option.
23000 Note that on Microsoft Windows versions where correction is necessary, there
23001 is no way to set a socket timeout shorter than 500 ms. If a socket timeout
23002 shorter than 500 ms is needed on these Windows versions, a call to
23003 Check_Selector should be added before any socket read or write operations.
23004
23005 @node Mixed-Language Programming on Windows,Windows Specific Add-Ons,Windows Socket Timeouts,Microsoft Windows Topics
23006 @anchor{gnat_ugn/platform_specific_information id13}@anchor{1cb}@anchor{gnat_ugn/platform_specific_information mixed-language-programming-on-windows}@anchor{1cc}
23007 @subsection Mixed-Language Programming on Windows
23008
23009
23010 Developing pure Ada applications on Windows is no different than on
23011 other GNAT-supported platforms. However, when developing or porting an
23012 application that contains a mix of Ada and C/C++, the choice of your
23013 Windows C/C++ development environment conditions your overall
23014 interoperability strategy.
23015
23016 If you use @code{gcc} or Microsoft C to compile the non-Ada part of
23017 your application, there are no Windows-specific restrictions that
23018 affect the overall interoperability with your Ada code. If you do want
23019 to use the Microsoft tools for your C++ code, you have two choices:
23020
23021
23022 @itemize *
23023
23024 @item
23025 Encapsulate your C++ code in a DLL to be linked with your Ada
23026 application. In this case, use the Microsoft or whatever environment to
23027 build the DLL and use GNAT to build your executable
23028 (@ref{1cd,,Using DLLs with GNAT}).
23029
23030 @item
23031 Or you can encapsulate your Ada code in a DLL to be linked with the
23032 other part of your application. In this case, use GNAT to build the DLL
23033 (@ref{1ce,,Building DLLs with GNAT Project files}) and use the Microsoft
23034 or whatever environment to build your executable.
23035 @end itemize
23036
23037 In addition to the description about C main in
23038 @ref{2c,,Mixed Language Programming} section, if the C main uses a
23039 stand-alone library it is required on x86-windows to
23040 setup the SEH context. For this the C main must looks like this:
23041
23042 @quotation
23043
23044 @example
23045 /* main.c */
23046 extern void adainit (void);
23047 extern void adafinal (void);
23048 extern void __gnat_initialize(void*);
23049 extern void call_to_ada (void);
23050
23051 int main (int argc, char *argv[])
23052 @{
23053 int SEH [2];
23054
23055 /* Initialize the SEH context */
23056 __gnat_initialize (&SEH);
23057
23058 adainit();
23059
23060 /* Then call Ada services in the stand-alone library */
23061
23062 call_to_ada();
23063
23064 adafinal();
23065 @}
23066 @end example
23067 @end quotation
23068
23069 Note that this is not needed on x86_64-windows where the Windows
23070 native SEH support is used.
23071
23072 @menu
23073 * Windows Calling Conventions::
23074 * Introduction to Dynamic Link Libraries (DLLs): Introduction to Dynamic Link Libraries DLLs.
23075 * Using DLLs with GNAT::
23076 * Building DLLs with GNAT Project files::
23077 * Building DLLs with GNAT::
23078 * Building DLLs with gnatdll::
23079 * Ada DLLs and Finalization::
23080 * Creating a Spec for Ada DLLs::
23081 * GNAT and Windows Resources::
23082 * Using GNAT DLLs from Microsoft Visual Studio Applications::
23083 * Debugging a DLL::
23084 * Setting Stack Size from gnatlink::
23085 * Setting Heap Size from gnatlink::
23086
23087 @end menu
23088
23089 @node Windows Calling Conventions,Introduction to Dynamic Link Libraries DLLs,,Mixed-Language Programming on Windows
23090 @anchor{gnat_ugn/platform_specific_information id14}@anchor{1cf}@anchor{gnat_ugn/platform_specific_information windows-calling-conventions}@anchor{1d0}
23091 @subsubsection Windows Calling Conventions
23092
23093
23094 @geindex Stdcall
23095
23096 @geindex APIENTRY
23097
23098 This section pertain only to Win32. On Win64 there is a single native
23099 calling convention. All convention specifiers are ignored on this
23100 platform.
23101
23102 When a subprogram @code{F} (caller) calls a subprogram @code{G}
23103 (callee), there are several ways to push @code{G}’s parameters on the
23104 stack and there are several possible scenarios to clean up the stack
23105 upon @code{G}’s return. A calling convention is an agreed upon software
23106 protocol whereby the responsibilities between the caller (@code{F}) and
23107 the callee (@code{G}) are clearly defined. Several calling conventions
23108 are available for Windows:
23109
23110
23111 @itemize *
23112
23113 @item
23114 @code{C} (Microsoft defined)
23115
23116 @item
23117 @code{Stdcall} (Microsoft defined)
23118
23119 @item
23120 @code{Win32} (GNAT specific)
23121
23122 @item
23123 @code{DLL} (GNAT specific)
23124 @end itemize
23125
23126 @menu
23127 * C Calling Convention::
23128 * Stdcall Calling Convention::
23129 * Win32 Calling Convention::
23130 * DLL Calling Convention::
23131
23132 @end menu
23133
23134 @node C Calling Convention,Stdcall Calling Convention,,Windows Calling Conventions
23135 @anchor{gnat_ugn/platform_specific_information c-calling-convention}@anchor{1d1}@anchor{gnat_ugn/platform_specific_information id15}@anchor{1d2}
23136 @subsubsection @code{C} Calling Convention
23137
23138
23139 This is the default calling convention used when interfacing to C/C++
23140 routines compiled with either @code{gcc} or Microsoft Visual C++.
23141
23142 In the @code{C} calling convention subprogram parameters are pushed on the
23143 stack by the caller from right to left. The caller itself is in charge of
23144 cleaning up the stack after the call. In addition, the name of a routine
23145 with @code{C} calling convention is mangled by adding a leading underscore.
23146
23147 The name to use on the Ada side when importing (or exporting) a routine
23148 with @code{C} calling convention is the name of the routine. For
23149 instance the C function:
23150
23151 @quotation
23152
23153 @example
23154 int get_val (long);
23155 @end example
23156 @end quotation
23157
23158 should be imported from Ada as follows:
23159
23160 @quotation
23161
23162 @example
23163 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
23164 pragma Import (C, Get_Val, External_Name => "get_val");
23165 @end example
23166 @end quotation
23167
23168 Note that in this particular case the @code{External_Name} parameter could
23169 have been omitted since, when missing, this parameter is taken to be the
23170 name of the Ada entity in lower case. When the @code{Link_Name} parameter
23171 is missing, as in the above example, this parameter is set to be the
23172 @code{External_Name} with a leading underscore.
23173
23174 When importing a variable defined in C, you should always use the @code{C}
23175 calling convention unless the object containing the variable is part of a
23176 DLL (in which case you should use the @code{Stdcall} calling
23177 convention, @ref{1d3,,Stdcall Calling Convention}).
23178
23179 @node Stdcall Calling Convention,Win32 Calling Convention,C Calling Convention,Windows Calling Conventions
23180 @anchor{gnat_ugn/platform_specific_information id16}@anchor{1d4}@anchor{gnat_ugn/platform_specific_information stdcall-calling-convention}@anchor{1d3}
23181 @subsubsection @code{Stdcall} Calling Convention
23182
23183
23184 This convention, which was the calling convention used for Pascal
23185 programs, is used by Microsoft for all the routines in the Win32 API for
23186 efficiency reasons. It must be used to import any routine for which this
23187 convention was specified.
23188
23189 In the @code{Stdcall} calling convention subprogram parameters are pushed
23190 on the stack by the caller from right to left. The callee (and not the
23191 caller) is in charge of cleaning the stack on routine exit. In addition,
23192 the name of a routine with @code{Stdcall} calling convention is mangled by
23193 adding a leading underscore (as for the @code{C} calling convention) and a
23194 trailing @code{@@@emph{nn}}, where @code{nn} is the overall size (in
23195 bytes) of the parameters passed to the routine.
23196
23197 The name to use on the Ada side when importing a C routine with a
23198 @code{Stdcall} calling convention is the name of the C routine. The leading
23199 underscore and trailing @code{@@@emph{nn}} are added automatically by
23200 the compiler. For instance the Win32 function:
23201
23202 @quotation
23203
23204 @example
23205 APIENTRY int get_val (long);
23206 @end example
23207 @end quotation
23208
23209 should be imported from Ada as follows:
23210
23211 @quotation
23212
23213 @example
23214 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
23215 pragma Import (Stdcall, Get_Val);
23216 -- On the x86 a long is 4 bytes, so the Link_Name is "_get_val@@4"
23217 @end example
23218 @end quotation
23219
23220 As for the @code{C} calling convention, when the @code{External_Name}
23221 parameter is missing, it is taken to be the name of the Ada entity in lower
23222 case. If instead of writing the above import pragma you write:
23223
23224 @quotation
23225
23226 @example
23227 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
23228 pragma Import (Stdcall, Get_Val, External_Name => "retrieve_val");
23229 @end example
23230 @end quotation
23231
23232 then the imported routine is @code{_retrieve_val@@4}. However, if instead
23233 of specifying the @code{External_Name} parameter you specify the
23234 @code{Link_Name} as in the following example:
23235
23236 @quotation
23237
23238 @example
23239 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
23240 pragma Import (Stdcall, Get_Val, Link_Name => "retrieve_val");
23241 @end example
23242 @end quotation
23243
23244 then the imported routine is @code{retrieve_val}, that is, there is no
23245 decoration at all. No leading underscore and no Stdcall suffix
23246 @code{@@@emph{nn}}.
23247
23248 This is especially important as in some special cases a DLL’s entry
23249 point name lacks a trailing @code{@@@emph{nn}} while the exported
23250 name generated for a call has it.
23251
23252 It is also possible to import variables defined in a DLL by using an
23253 import pragma for a variable. As an example, if a DLL contains a
23254 variable defined as:
23255
23256 @quotation
23257
23258 @example
23259 int my_var;
23260 @end example
23261 @end quotation
23262
23263 then, to access this variable from Ada you should write:
23264
23265 @quotation
23266
23267 @example
23268 My_Var : Interfaces.C.int;
23269 pragma Import (Stdcall, My_Var);
23270 @end example
23271 @end quotation
23272
23273 Note that to ease building cross-platform bindings this convention
23274 will be handled as a @code{C} calling convention on non-Windows platforms.
23275
23276 @node Win32 Calling Convention,DLL Calling Convention,Stdcall Calling Convention,Windows Calling Conventions
23277 @anchor{gnat_ugn/platform_specific_information id17}@anchor{1d5}@anchor{gnat_ugn/platform_specific_information win32-calling-convention}@anchor{1d6}
23278 @subsubsection @code{Win32} Calling Convention
23279
23280
23281 This convention, which is GNAT-specific is fully equivalent to the
23282 @code{Stdcall} calling convention described above.
23283
23284 @node DLL Calling Convention,,Win32 Calling Convention,Windows Calling Conventions
23285 @anchor{gnat_ugn/platform_specific_information dll-calling-convention}@anchor{1d7}@anchor{gnat_ugn/platform_specific_information id18}@anchor{1d8}
23286 @subsubsection @code{DLL} Calling Convention
23287
23288
23289 This convention, which is GNAT-specific is fully equivalent to the
23290 @code{Stdcall} calling convention described above.
23291
23292 @node Introduction to Dynamic Link Libraries DLLs,Using DLLs with GNAT,Windows Calling Conventions,Mixed-Language Programming on Windows
23293 @anchor{gnat_ugn/platform_specific_information id19}@anchor{1d9}@anchor{gnat_ugn/platform_specific_information introduction-to-dynamic-link-libraries-dlls}@anchor{1da}
23294 @subsubsection Introduction to Dynamic Link Libraries (DLLs)
23295
23296
23297 @geindex DLL
23298
23299 A Dynamically Linked Library (DLL) is a library that can be shared by
23300 several applications running under Windows. A DLL can contain any number of
23301 routines and variables.
23302
23303 One advantage of DLLs is that you can change and enhance them without
23304 forcing all the applications that depend on them to be relinked or
23305 recompiled. However, you should be aware than all calls to DLL routines are
23306 slower since, as you will understand below, such calls are indirect.
23307
23308 To illustrate the remainder of this section, suppose that an application
23309 wants to use the services of a DLL @code{API.dll}. To use the services
23310 provided by @code{API.dll} you must statically link against the DLL or
23311 an import library which contains a jump table with an entry for each
23312 routine and variable exported by the DLL. In the Microsoft world this
23313 import library is called @code{API.lib}. When using GNAT this import
23314 library is called either @code{libAPI.dll.a}, @code{libapi.dll.a},
23315 @code{libAPI.a} or @code{libapi.a} (names are case insensitive).
23316
23317 After you have linked your application with the DLL or the import library
23318 and you run your application, here is what happens:
23319
23320
23321 @itemize *
23322
23323 @item
23324 Your application is loaded into memory.
23325
23326 @item
23327 The DLL @code{API.dll} is mapped into the address space of your
23328 application. This means that:
23329
23330
23331 @itemize -
23332
23333 @item
23334 The DLL will use the stack of the calling thread.
23335
23336 @item
23337 The DLL will use the virtual address space of the calling process.
23338
23339 @item
23340 The DLL will allocate memory from the virtual address space of the calling
23341 process.
23342
23343 @item
23344 Handles (pointers) can be safely exchanged between routines in the DLL
23345 routines and routines in the application using the DLL.
23346 @end itemize
23347
23348 @item
23349 The entries in the jump table (from the import library @code{libAPI.dll.a}
23350 or @code{API.lib} or automatically created when linking against a DLL)
23351 which is part of your application are initialized with the addresses
23352 of the routines and variables in @code{API.dll}.
23353
23354 @item
23355 If present in @code{API.dll}, routines @code{DllMain} or
23356 @code{DllMainCRTStartup} are invoked. These routines typically contain
23357 the initialization code needed for the well-being of the routines and
23358 variables exported by the DLL.
23359 @end itemize
23360
23361 There is an additional point which is worth mentioning. In the Windows
23362 world there are two kind of DLLs: relocatable and non-relocatable
23363 DLLs. Non-relocatable DLLs can only be loaded at a very specific address
23364 in the target application address space. If the addresses of two
23365 non-relocatable DLLs overlap and these happen to be used by the same
23366 application, a conflict will occur and the application will run
23367 incorrectly. Hence, when possible, it is always preferable to use and
23368 build relocatable DLLs. Both relocatable and non-relocatable DLLs are
23369 supported by GNAT. Note that the @code{-s} linker option (see GNU Linker
23370 User’s Guide) removes the debugging symbols from the DLL but the DLL can
23371 still be relocated.
23372
23373 As a side note, an interesting difference between Microsoft DLLs and
23374 Unix shared libraries, is the fact that on most Unix systems all public
23375 routines are exported by default in a Unix shared library, while under
23376 Windows it is possible (but not required) to list exported routines in
23377 a definition file (see @ref{1db,,The Definition File}).
23378
23379 @node Using DLLs with GNAT,Building DLLs with GNAT Project files,Introduction to Dynamic Link Libraries DLLs,Mixed-Language Programming on Windows
23380 @anchor{gnat_ugn/platform_specific_information id20}@anchor{1dc}@anchor{gnat_ugn/platform_specific_information using-dlls-with-gnat}@anchor{1cd}
23381 @subsubsection Using DLLs with GNAT
23382
23383
23384 To use the services of a DLL, say @code{API.dll}, in your Ada application
23385 you must have:
23386
23387
23388 @itemize *
23389
23390 @item
23391 The Ada spec for the routines and/or variables you want to access in
23392 @code{API.dll}. If not available this Ada spec must be built from the C/C++
23393 header files provided with the DLL.
23394
23395 @item
23396 The import library (@code{libAPI.dll.a} or @code{API.lib}). As previously
23397 mentioned an import library is a statically linked library containing the
23398 import table which will be filled at load time to point to the actual
23399 @code{API.dll} routines. Sometimes you don’t have an import library for the
23400 DLL you want to use. The following sections will explain how to build
23401 one. Note that this is optional.
23402
23403 @item
23404 The actual DLL, @code{API.dll}.
23405 @end itemize
23406
23407 Once you have all the above, to compile an Ada application that uses the
23408 services of @code{API.dll} and whose main subprogram is @code{My_Ada_App},
23409 you simply issue the command
23410
23411 @quotation
23412
23413 @example
23414 $ gnatmake my_ada_app -largs -lAPI
23415 @end example
23416 @end quotation
23417
23418 The argument @code{-largs -lAPI} at the end of the @code{gnatmake} command
23419 tells the GNAT linker to look for an import library. The linker will
23420 look for a library name in this specific order:
23421
23422
23423 @itemize *
23424
23425 @item
23426 @code{libAPI.dll.a}
23427
23428 @item
23429 @code{API.dll.a}
23430
23431 @item
23432 @code{libAPI.a}
23433
23434 @item
23435 @code{API.lib}
23436
23437 @item
23438 @code{libAPI.dll}
23439
23440 @item
23441 @code{API.dll}
23442 @end itemize
23443
23444 The first three are the GNU style import libraries. The third is the
23445 Microsoft style import libraries. The last two are the actual DLL names.
23446
23447 Note that if the Ada package spec for @code{API.dll} contains the
23448 following pragma
23449
23450 @quotation
23451
23452 @example
23453 pragma Linker_Options ("-lAPI");
23454 @end example
23455 @end quotation
23456
23457 you do not have to add @code{-largs -lAPI} at the end of the
23458 @code{gnatmake} command.
23459
23460 If any one of the items above is missing you will have to create it
23461 yourself. The following sections explain how to do so using as an
23462 example a fictitious DLL called @code{API.dll}.
23463
23464 @menu
23465 * Creating an Ada Spec for the DLL Services::
23466 * Creating an Import Library::
23467
23468 @end menu
23469
23470 @node Creating an Ada Spec for the DLL Services,Creating an Import Library,,Using DLLs with GNAT
23471 @anchor{gnat_ugn/platform_specific_information creating-an-ada-spec-for-the-dll-services}@anchor{1dd}@anchor{gnat_ugn/platform_specific_information id21}@anchor{1de}
23472 @subsubsection Creating an Ada Spec for the DLL Services
23473
23474
23475 A DLL typically comes with a C/C++ header file which provides the
23476 definitions of the routines and variables exported by the DLL. The Ada
23477 equivalent of this header file is a package spec that contains definitions
23478 for the imported entities. If the DLL you intend to use does not come with
23479 an Ada spec you have to generate one such spec yourself. For example if
23480 the header file of @code{API.dll} is a file @code{api.h} containing the
23481 following two definitions:
23482
23483 @quotation
23484
23485 @example
23486 int some_var;
23487 int get (char *);
23488 @end example
23489 @end quotation
23490
23491 then the equivalent Ada spec could be:
23492
23493 @quotation
23494
23495 @example
23496 with Interfaces.C.Strings;
23497 package API is
23498 use Interfaces;
23499
23500 Some_Var : C.int;
23501 function Get (Str : C.Strings.Chars_Ptr) return C.int;
23502
23503 private
23504 pragma Import (C, Get);
23505 pragma Import (DLL, Some_Var);
23506 end API;
23507 @end example
23508 @end quotation
23509
23510 @node Creating an Import Library,,Creating an Ada Spec for the DLL Services,Using DLLs with GNAT
23511 @anchor{gnat_ugn/platform_specific_information creating-an-import-library}@anchor{1df}@anchor{gnat_ugn/platform_specific_information id22}@anchor{1e0}
23512 @subsubsection Creating an Import Library
23513
23514
23515 @geindex Import library
23516
23517 If a Microsoft-style import library @code{API.lib} or a GNAT-style
23518 import library @code{libAPI.dll.a} or @code{libAPI.a} is available
23519 with @code{API.dll} you can skip this section. You can also skip this
23520 section if @code{API.dll} or @code{libAPI.dll} is built with GNU tools
23521 as in this case it is possible to link directly against the
23522 DLL. Otherwise read on.
23523
23524 @geindex Definition file
23525 @anchor{gnat_ugn/platform_specific_information the-definition-file}@anchor{1db}
23526 @subsubheading The Definition File
23527
23528
23529 As previously mentioned, and unlike Unix systems, the list of symbols
23530 that are exported from a DLL must be provided explicitly in Windows.
23531 The main goal of a definition file is precisely that: list the symbols
23532 exported by a DLL. A definition file (usually a file with a @code{.def}
23533 suffix) has the following structure:
23534
23535 @quotation
23536
23537 @example
23538 [LIBRARY `@w{`}name`@w{`}]
23539 [DESCRIPTION `@w{`}string`@w{`}]
23540 EXPORTS
23541 `@w{`}symbol1`@w{`}
23542 `@w{`}symbol2`@w{`}
23543 ...
23544 @end example
23545 @end quotation
23546
23547
23548 @table @asis
23549
23550 @item @emph{LIBRARY name}
23551
23552 This section, which is optional, gives the name of the DLL.
23553
23554 @item @emph{DESCRIPTION string}
23555
23556 This section, which is optional, gives a description string that will be
23557 embedded in the import library.
23558
23559 @item @emph{EXPORTS}
23560
23561 This section gives the list of exported symbols (procedures, functions or
23562 variables). For instance in the case of @code{API.dll} the @code{EXPORTS}
23563 section of @code{API.def} looks like:
23564
23565 @example
23566 EXPORTS
23567 some_var
23568 get
23569 @end example
23570 @end table
23571
23572 Note that you must specify the correct suffix (@code{@@@emph{nn}})
23573 (see @ref{1d0,,Windows Calling Conventions}) for a Stdcall
23574 calling convention function in the exported symbols list.
23575
23576 There can actually be other sections in a definition file, but these
23577 sections are not relevant to the discussion at hand.
23578 @anchor{gnat_ugn/platform_specific_information create-def-file-automatically}@anchor{1e1}
23579 @subsubheading Creating a Definition File Automatically
23580
23581
23582 You can automatically create the definition file @code{API.def}
23583 (see @ref{1db,,The Definition File}) from a DLL.
23584 For that use the @code{dlltool} program as follows:
23585
23586 @quotation
23587
23588 @example
23589 $ dlltool API.dll -z API.def --export-all-symbols
23590 @end example
23591
23592 Note that if some routines in the DLL have the @code{Stdcall} convention
23593 (@ref{1d0,,Windows Calling Conventions}) with stripped @code{@@@emph{nn}}
23594 suffix then you’ll have to edit @code{api.def} to add it, and specify
23595 @code{-k} to @code{gnatdll} when creating the import library.
23596
23597 Here are some hints to find the right @code{@@@emph{nn}} suffix.
23598
23599
23600 @itemize -
23601
23602 @item
23603 If you have the Microsoft import library (.lib), it is possible to get
23604 the right symbols by using Microsoft @code{dumpbin} tool (see the
23605 corresponding Microsoft documentation for further details).
23606
23607 @example
23608 $ dumpbin /exports api.lib
23609 @end example
23610
23611 @item
23612 If you have a message about a missing symbol at link time the compiler
23613 tells you what symbol is expected. You just have to go back to the
23614 definition file and add the right suffix.
23615 @end itemize
23616 @end quotation
23617 @anchor{gnat_ugn/platform_specific_information gnat-style-import-library}@anchor{1e2}
23618 @subsubheading GNAT-Style Import Library
23619
23620
23621 To create a static import library from @code{API.dll} with the GNAT tools
23622 you should create the .def file, then use @code{gnatdll} tool
23623 (see @ref{1e3,,Using gnatdll}) as follows:
23624
23625 @quotation
23626
23627 @example
23628 $ gnatdll -e API.def -d API.dll
23629 @end example
23630
23631 @code{gnatdll} takes as input a definition file @code{API.def} and the
23632 name of the DLL containing the services listed in the definition file
23633 @code{API.dll}. The name of the static import library generated is
23634 computed from the name of the definition file as follows: if the
23635 definition file name is @code{xyz.def}, the import library name will
23636 be @code{libxyz.a}. Note that in the previous example option
23637 @code{-e} could have been removed because the name of the definition
23638 file (before the @code{.def} suffix) is the same as the name of the
23639 DLL (@ref{1e3,,Using gnatdll} for more information about @code{gnatdll}).
23640 @end quotation
23641 @anchor{gnat_ugn/platform_specific_information msvs-style-import-library}@anchor{1e4}
23642 @subsubheading Microsoft-Style Import Library
23643
23644
23645 A Microsoft import library is needed only if you plan to make an
23646 Ada DLL available to applications developed with Microsoft
23647 tools (@ref{1cc,,Mixed-Language Programming on Windows}).
23648
23649 To create a Microsoft-style import library for @code{API.dll} you
23650 should create the .def file, then build the actual import library using
23651 Microsoft’s @code{lib} utility:
23652
23653 @quotation
23654
23655 @example
23656 $ lib -machine:IX86 -def:API.def -out:API.lib
23657 @end example
23658
23659 If you use the above command the definition file @code{API.def} must
23660 contain a line giving the name of the DLL:
23661
23662 @example
23663 LIBRARY "API"
23664 @end example
23665
23666 See the Microsoft documentation for further details about the usage of
23667 @code{lib}.
23668 @end quotation
23669
23670 @node Building DLLs with GNAT Project files,Building DLLs with GNAT,Using DLLs with GNAT,Mixed-Language Programming on Windows
23671 @anchor{gnat_ugn/platform_specific_information building-dlls-with-gnat-project-files}@anchor{1ce}@anchor{gnat_ugn/platform_specific_information id23}@anchor{1e5}
23672 @subsubsection Building DLLs with GNAT Project files
23673
23674
23675 @geindex DLLs
23676 @geindex building
23677
23678 There is nothing specific to Windows in the build process.
23679 See the @emph{Library Projects} section in the @emph{GNAT Project Manager}
23680 chapter of the @emph{GPRbuild User’s Guide}.
23681
23682 Due to a system limitation, it is not possible under Windows to create threads
23683 when inside the @code{DllMain} routine which is used for auto-initialization
23684 of shared libraries, so it is not possible to have library level tasks in SALs.
23685
23686 @node Building DLLs with GNAT,Building DLLs with gnatdll,Building DLLs with GNAT Project files,Mixed-Language Programming on Windows
23687 @anchor{gnat_ugn/platform_specific_information building-dlls-with-gnat}@anchor{1e6}@anchor{gnat_ugn/platform_specific_information id24}@anchor{1e7}
23688 @subsubsection Building DLLs with GNAT
23689
23690
23691 @geindex DLLs
23692 @geindex building
23693
23694 This section explain how to build DLLs using the GNAT built-in DLL
23695 support. With the following procedure it is straight forward to build
23696 and use DLLs with GNAT.
23697
23698
23699 @itemize *
23700
23701 @item
23702 Building object files.
23703 The first step is to build all objects files that are to be included
23704 into the DLL. This is done by using the standard @code{gnatmake} tool.
23705
23706 @item
23707 Building the DLL.
23708 To build the DLL you must use the @code{gcc} @code{-shared} and
23709 @code{-shared-libgcc} options. It is quite simple to use this method:
23710
23711 @example
23712 $ gcc -shared -shared-libgcc -o api.dll obj1.o obj2.o ...
23713 @end example
23714
23715 It is important to note that in this case all symbols found in the
23716 object files are automatically exported. It is possible to restrict
23717 the set of symbols to export by passing to @code{gcc} a definition
23718 file (see @ref{1db,,The Definition File}).
23719 For example:
23720
23721 @example
23722 $ gcc -shared -shared-libgcc -o api.dll api.def obj1.o obj2.o ...
23723 @end example
23724
23725 If you use a definition file you must export the elaboration procedures
23726 for every package that required one. Elaboration procedures are named
23727 using the package name followed by “_E”.
23728
23729 @item
23730 Preparing DLL to be used.
23731 For the DLL to be used by client programs the bodies must be hidden
23732 from it and the .ali set with read-only attribute. This is very important
23733 otherwise GNAT will recompile all packages and will not actually use
23734 the code in the DLL. For example:
23735
23736 @example
23737 $ mkdir apilib
23738 $ copy *.ads *.ali api.dll apilib
23739 $ attrib +R apilib\\*.ali
23740 @end example
23741 @end itemize
23742
23743 At this point it is possible to use the DLL by directly linking
23744 against it. Note that you must use the GNAT shared runtime when using
23745 GNAT shared libraries. This is achieved by using the @code{-shared} binder
23746 option.
23747
23748 @quotation
23749
23750 @example
23751 $ gnatmake main -Iapilib -bargs -shared -largs -Lapilib -lAPI
23752 @end example
23753 @end quotation
23754
23755 @node Building DLLs with gnatdll,Ada DLLs and Finalization,Building DLLs with GNAT,Mixed-Language Programming on Windows
23756 @anchor{gnat_ugn/platform_specific_information building-dlls-with-gnatdll}@anchor{1e8}@anchor{gnat_ugn/platform_specific_information id25}@anchor{1e9}
23757 @subsubsection Building DLLs with gnatdll
23758
23759
23760 @geindex DLLs
23761 @geindex building
23762
23763 Note that it is preferred to use GNAT Project files
23764 (@ref{1ce,,Building DLLs with GNAT Project files}) or the built-in GNAT
23765 DLL support (@ref{1e6,,Building DLLs with GNAT}) or to build DLLs.
23766
23767 This section explains how to build DLLs containing Ada code using
23768 @code{gnatdll}. These DLLs will be referred to as Ada DLLs in the
23769 remainder of this section.
23770
23771 The steps required to build an Ada DLL that is to be used by Ada as well as
23772 non-Ada applications are as follows:
23773
23774
23775 @itemize *
23776
23777 @item
23778 You need to mark each Ada entity exported by the DLL with a @code{C} or
23779 @code{Stdcall} calling convention to avoid any Ada name mangling for the
23780 entities exported by the DLL
23781 (see @ref{1ea,,Exporting Ada Entities}). You can
23782 skip this step if you plan to use the Ada DLL only from Ada applications.
23783
23784 @item
23785 Your Ada code must export an initialization routine which calls the routine
23786 @code{adainit} generated by @code{gnatbind} to perform the elaboration of
23787 the Ada code in the DLL (@ref{1eb,,Ada DLLs and Elaboration}). The initialization
23788 routine exported by the Ada DLL must be invoked by the clients of the DLL
23789 to initialize the DLL.
23790
23791 @item
23792 When useful, the DLL should also export a finalization routine which calls
23793 routine @code{adafinal} generated by @code{gnatbind} to perform the
23794 finalization of the Ada code in the DLL (@ref{1ec,,Ada DLLs and Finalization}).
23795 The finalization routine exported by the Ada DLL must be invoked by the
23796 clients of the DLL when the DLL services are no further needed.
23797
23798 @item
23799 You must provide a spec for the services exported by the Ada DLL in each
23800 of the programming languages to which you plan to make the DLL available.
23801
23802 @item
23803 You must provide a definition file listing the exported entities
23804 (@ref{1db,,The Definition File}).
23805
23806 @item
23807 Finally you must use @code{gnatdll} to produce the DLL and the import
23808 library (@ref{1e3,,Using gnatdll}).
23809 @end itemize
23810
23811 Note that a relocatable DLL stripped using the @code{strip}
23812 binutils tool will not be relocatable anymore. To build a DLL without
23813 debug information pass @code{-largs -s} to @code{gnatdll}. This
23814 restriction does not apply to a DLL built using a Library Project.
23815 See the @emph{Library Projects} section in the @emph{GNAT Project Manager}
23816 chapter of the @emph{GPRbuild User’s Guide}.
23817
23818 @c Limitations_When_Using_Ada_DLLs_from Ada:
23819
23820 @menu
23821 * Limitations When Using Ada DLLs from Ada::
23822 * Exporting Ada Entities::
23823 * Ada DLLs and Elaboration::
23824
23825 @end menu
23826
23827 @node Limitations When Using Ada DLLs from Ada,Exporting Ada Entities,,Building DLLs with gnatdll
23828 @anchor{gnat_ugn/platform_specific_information limitations-when-using-ada-dlls-from-ada}@anchor{1ed}
23829 @subsubsection Limitations When Using Ada DLLs from Ada
23830
23831
23832 When using Ada DLLs from Ada applications there is a limitation users
23833 should be aware of. Because on Windows the GNAT run-time is not in a DLL of
23834 its own, each Ada DLL includes a part of the GNAT run-time. Specifically,
23835 each Ada DLL includes the services of the GNAT run-time that are necessary
23836 to the Ada code inside the DLL. As a result, when an Ada program uses an
23837 Ada DLL there are two independent GNAT run-times: one in the Ada DLL and
23838 one in the main program.
23839
23840 It is therefore not possible to exchange GNAT run-time objects between the
23841 Ada DLL and the main Ada program. Example of GNAT run-time objects are file
23842 handles (e.g., @code{Text_IO.File_Type}), tasks types, protected objects
23843 types, etc.
23844
23845 It is completely safe to exchange plain elementary, array or record types,
23846 Windows object handles, etc.
23847
23848 @node Exporting Ada Entities,Ada DLLs and Elaboration,Limitations When Using Ada DLLs from Ada,Building DLLs with gnatdll
23849 @anchor{gnat_ugn/platform_specific_information exporting-ada-entities}@anchor{1ea}@anchor{gnat_ugn/platform_specific_information id26}@anchor{1ee}
23850 @subsubsection Exporting Ada Entities
23851
23852
23853 @geindex Export table
23854
23855 Building a DLL is a way to encapsulate a set of services usable from any
23856 application. As a result, the Ada entities exported by a DLL should be
23857 exported with the @code{C} or @code{Stdcall} calling conventions to avoid
23858 any Ada name mangling. As an example here is an Ada package
23859 @code{API}, spec and body, exporting two procedures, a function, and a
23860 variable:
23861
23862 @quotation
23863
23864 @example
23865 with Interfaces.C; use Interfaces;
23866 package API is
23867 Count : C.int := 0;
23868 function Factorial (Val : C.int) return C.int;
23869
23870 procedure Initialize_API;
23871 procedure Finalize_API;
23872 -- Initialization & Finalization routines. More in the next section.
23873 private
23874 pragma Export (C, Initialize_API);
23875 pragma Export (C, Finalize_API);
23876 pragma Export (C, Count);
23877 pragma Export (C, Factorial);
23878 end API;
23879 @end example
23880
23881 @example
23882 package body API is
23883 function Factorial (Val : C.int) return C.int is
23884 Fact : C.int := 1;
23885 begin
23886 Count := Count + 1;
23887 for K in 1 .. Val loop
23888 Fact := Fact * K;
23889 end loop;
23890 return Fact;
23891 end Factorial;
23892
23893 procedure Initialize_API is
23894 procedure Adainit;
23895 pragma Import (C, Adainit);
23896 begin
23897 Adainit;
23898 end Initialize_API;
23899
23900 procedure Finalize_API is
23901 procedure Adafinal;
23902 pragma Import (C, Adafinal);
23903 begin
23904 Adafinal;
23905 end Finalize_API;
23906 end API;
23907 @end example
23908 @end quotation
23909
23910 If the Ada DLL you are building will only be used by Ada applications
23911 you do not have to export Ada entities with a @code{C} or @code{Stdcall}
23912 convention. As an example, the previous package could be written as
23913 follows:
23914
23915 @quotation
23916
23917 @example
23918 package API is
23919 Count : Integer := 0;
23920 function Factorial (Val : Integer) return Integer;
23921
23922 procedure Initialize_API;
23923 procedure Finalize_API;
23924 -- Initialization and Finalization routines.
23925 end API;
23926 @end example
23927
23928 @example
23929 package body API is
23930 function Factorial (Val : Integer) return Integer is
23931 Fact : Integer := 1;
23932 begin
23933 Count := Count + 1;
23934 for K in 1 .. Val loop
23935 Fact := Fact * K;
23936 end loop;
23937 return Fact;
23938 end Factorial;
23939
23940 ...
23941 -- The remainder of this package body is unchanged.
23942 end API;
23943 @end example
23944 @end quotation
23945
23946 Note that if you do not export the Ada entities with a @code{C} or
23947 @code{Stdcall} convention you will have to provide the mangled Ada names
23948 in the definition file of the Ada DLL
23949 (@ref{1ef,,Creating the Definition File}).
23950
23951 @node Ada DLLs and Elaboration,,Exporting Ada Entities,Building DLLs with gnatdll
23952 @anchor{gnat_ugn/platform_specific_information ada-dlls-and-elaboration}@anchor{1eb}@anchor{gnat_ugn/platform_specific_information id27}@anchor{1f0}
23953 @subsubsection Ada DLLs and Elaboration
23954
23955
23956 @geindex DLLs and elaboration
23957
23958 The DLL that you are building contains your Ada code as well as all the
23959 routines in the Ada library that are needed by it. The first thing a
23960 user of your DLL must do is elaborate the Ada code
23961 (@ref{f,,Elaboration Order Handling in GNAT}).
23962
23963 To achieve this you must export an initialization routine
23964 (@code{Initialize_API} in the previous example), which must be invoked
23965 before using any of the DLL services. This elaboration routine must call
23966 the Ada elaboration routine @code{adainit} generated by the GNAT binder
23967 (@ref{a0,,Binding with Non-Ada Main Programs}). See the body of
23968 @code{Initialize_Api} for an example. Note that the GNAT binder is
23969 automatically invoked during the DLL build process by the @code{gnatdll}
23970 tool (@ref{1e3,,Using gnatdll}).
23971
23972 When a DLL is loaded, Windows systematically invokes a routine called
23973 @code{DllMain}. It would therefore be possible to call @code{adainit}
23974 directly from @code{DllMain} without having to provide an explicit
23975 initialization routine. Unfortunately, it is not possible to call
23976 @code{adainit} from the @code{DllMain} if your program has library level
23977 tasks because access to the @code{DllMain} entry point is serialized by
23978 the system (that is, only a single thread can execute ‘through’ it at a
23979 time), which means that the GNAT run-time will deadlock waiting for the
23980 newly created task to complete its initialization.
23981
23982 @node Ada DLLs and Finalization,Creating a Spec for Ada DLLs,Building DLLs with gnatdll,Mixed-Language Programming on Windows
23983 @anchor{gnat_ugn/platform_specific_information ada-dlls-and-finalization}@anchor{1ec}@anchor{gnat_ugn/platform_specific_information id28}@anchor{1f1}
23984 @subsubsection Ada DLLs and Finalization
23985
23986
23987 @geindex DLLs and finalization
23988
23989 When the services of an Ada DLL are no longer needed, the client code should
23990 invoke the DLL finalization routine, if available. The DLL finalization
23991 routine is in charge of releasing all resources acquired by the DLL. In the
23992 case of the Ada code contained in the DLL, this is achieved by calling
23993 routine @code{adafinal} generated by the GNAT binder
23994 (@ref{a0,,Binding with Non-Ada Main Programs}).
23995 See the body of @code{Finalize_Api} for an
23996 example. As already pointed out the GNAT binder is automatically invoked
23997 during the DLL build process by the @code{gnatdll} tool
23998 (@ref{1e3,,Using gnatdll}).
23999
24000 @node Creating a Spec for Ada DLLs,GNAT and Windows Resources,Ada DLLs and Finalization,Mixed-Language Programming on Windows
24001 @anchor{gnat_ugn/platform_specific_information creating-a-spec-for-ada-dlls}@anchor{1f2}@anchor{gnat_ugn/platform_specific_information id29}@anchor{1f3}
24002 @subsubsection Creating a Spec for Ada DLLs
24003
24004
24005 To use the services exported by the Ada DLL from another programming
24006 language (e.g., C), you have to translate the specs of the exported Ada
24007 entities in that language. For instance in the case of @code{API.dll},
24008 the corresponding C header file could look like:
24009
24010 @quotation
24011
24012 @example
24013 extern int *_imp__count;
24014 #define count (*_imp__count)
24015 int factorial (int);
24016 @end example
24017 @end quotation
24018
24019 It is important to understand that when building an Ada DLL to be used by
24020 other Ada applications, you need two different specs for the packages
24021 contained in the DLL: one for building the DLL and the other for using
24022 the DLL. This is because the @code{DLL} calling convention is needed to
24023 use a variable defined in a DLL, but when building the DLL, the variable
24024 must have either the @code{Ada} or @code{C} calling convention. As an
24025 example consider a DLL comprising the following package @code{API}:
24026
24027 @quotation
24028
24029 @example
24030 package API is
24031 Count : Integer := 0;
24032 ...
24033 -- Remainder of the package omitted.
24034 end API;
24035 @end example
24036 @end quotation
24037
24038 After producing a DLL containing package @code{API}, the spec that
24039 must be used to import @code{API.Count} from Ada code outside of the
24040 DLL is:
24041
24042 @quotation
24043
24044 @example
24045 package API is
24046 Count : Integer;
24047 pragma Import (DLL, Count);
24048 end API;
24049 @end example
24050 @end quotation
24051
24052 @menu
24053 * Creating the Definition File::
24054 * Using gnatdll::
24055
24056 @end menu
24057
24058 @node Creating the Definition File,Using gnatdll,,Creating a Spec for Ada DLLs
24059 @anchor{gnat_ugn/platform_specific_information creating-the-definition-file}@anchor{1ef}@anchor{gnat_ugn/platform_specific_information id30}@anchor{1f4}
24060 @subsubsection Creating the Definition File
24061
24062
24063 The definition file is the last file needed to build the DLL. It lists
24064 the exported symbols. As an example, the definition file for a DLL
24065 containing only package @code{API} (where all the entities are exported
24066 with a @code{C} calling convention) is:
24067
24068 @quotation
24069
24070 @example
24071 EXPORTS
24072 count
24073 factorial
24074 finalize_api
24075 initialize_api
24076 @end example
24077 @end quotation
24078
24079 If the @code{C} calling convention is missing from package @code{API},
24080 then the definition file contains the mangled Ada names of the above
24081 entities, which in this case are:
24082
24083 @quotation
24084
24085 @example
24086 EXPORTS
24087 api__count
24088 api__factorial
24089 api__finalize_api
24090 api__initialize_api
24091 @end example
24092 @end quotation
24093
24094 @node Using gnatdll,,Creating the Definition File,Creating a Spec for Ada DLLs
24095 @anchor{gnat_ugn/platform_specific_information id31}@anchor{1f5}@anchor{gnat_ugn/platform_specific_information using-gnatdll}@anchor{1e3}
24096 @subsubsection Using @code{gnatdll}
24097
24098
24099 @geindex gnatdll
24100
24101 @code{gnatdll} is a tool to automate the DLL build process once all the Ada
24102 and non-Ada sources that make up your DLL have been compiled.
24103 @code{gnatdll} is actually in charge of two distinct tasks: build the
24104 static import library for the DLL and the actual DLL. The form of the
24105 @code{gnatdll} command is
24106
24107 @quotation
24108
24109 @example
24110 $ gnatdll [ switches ] list-of-files [ -largs opts ]
24111 @end example
24112 @end quotation
24113
24114 where @code{list-of-files} is a list of ALI and object files. The object
24115 file list must be the exact list of objects corresponding to the non-Ada
24116 sources whose services are to be included in the DLL. The ALI file list
24117 must be the exact list of ALI files for the corresponding Ada sources
24118 whose services are to be included in the DLL. If @code{list-of-files} is
24119 missing, only the static import library is generated.
24120
24121 You may specify any of the following switches to @code{gnatdll}:
24122
24123 @quotation
24124
24125 @geindex -a (gnatdll)
24126 @end quotation
24127
24128
24129 @table @asis
24130
24131 @item @code{-a[@emph{address}]}
24132
24133 Build a non-relocatable DLL at @code{address}. If @code{address} is not
24134 specified the default address @code{0x11000000} will be used. By default,
24135 when this switch is missing, @code{gnatdll} builds relocatable DLL. We
24136 advise the reader to build relocatable DLL.
24137
24138 @geindex -b (gnatdll)
24139
24140 @item @code{-b @emph{address}}
24141
24142 Set the relocatable DLL base address. By default the address is
24143 @code{0x11000000}.
24144
24145 @geindex -bargs (gnatdll)
24146
24147 @item @code{-bargs @emph{opts}}
24148
24149 Binder options. Pass @code{opts} to the binder.
24150
24151 @geindex -d (gnatdll)
24152
24153 @item @code{-d @emph{dllfile}}
24154
24155 @code{dllfile} is the name of the DLL. This switch must be present for
24156 @code{gnatdll} to do anything. The name of the generated import library is
24157 obtained algorithmically from @code{dllfile} as shown in the following
24158 example: if @code{dllfile} is @code{xyz.dll}, the import library name is
24159 @code{libxyz.dll.a}. The name of the definition file to use (if not specified
24160 by option @code{-e}) is obtained algorithmically from @code{dllfile}
24161 as shown in the following example:
24162 if @code{dllfile} is @code{xyz.dll}, the definition
24163 file used is @code{xyz.def}.
24164
24165 @geindex -e (gnatdll)
24166
24167 @item @code{-e @emph{deffile}}
24168
24169 @code{deffile} is the name of the definition file.
24170
24171 @geindex -g (gnatdll)
24172
24173 @item @code{-g}
24174
24175 Generate debugging information. This information is stored in the object
24176 file and copied from there to the final DLL file by the linker,
24177 where it can be read by the debugger. You must use the
24178 @code{-g} switch if you plan on using the debugger or the symbolic
24179 stack traceback.
24180
24181 @geindex -h (gnatdll)
24182
24183 @item @code{-h}
24184
24185 Help mode. Displays @code{gnatdll} switch usage information.
24186
24187 @geindex -I (gnatdll)
24188
24189 @item @code{-I@emph{dir}}
24190
24191 Direct @code{gnatdll} to search the @code{dir} directory for source and
24192 object files needed to build the DLL.
24193 (@ref{73,,Search Paths and the Run-Time Library (RTL)}).
24194
24195 @geindex -k (gnatdll)
24196
24197 @item @code{-k}
24198
24199 Removes the @code{@@@emph{nn}} suffix from the import library’s exported
24200 names, but keeps them for the link names. You must specify this
24201 option if you want to use a @code{Stdcall} function in a DLL for which
24202 the @code{@@@emph{nn}} suffix has been removed. This is the case for most
24203 of the Windows NT DLL for example. This option has no effect when
24204 @code{-n} option is specified.
24205
24206 @geindex -l (gnatdll)
24207
24208 @item @code{-l @emph{file}}
24209
24210 The list of ALI and object files used to build the DLL are listed in
24211 @code{file}, instead of being given in the command line. Each line in
24212 @code{file} contains the name of an ALI or object file.
24213
24214 @geindex -n (gnatdll)
24215
24216 @item @code{-n}
24217
24218 No Import. Do not create the import library.
24219
24220 @geindex -q (gnatdll)
24221
24222 @item @code{-q}
24223
24224 Quiet mode. Do not display unnecessary messages.
24225
24226 @geindex -v (gnatdll)
24227
24228 @item @code{-v}
24229
24230 Verbose mode. Display extra information.
24231
24232 @geindex -largs (gnatdll)
24233
24234 @item @code{-largs @emph{opts}}
24235
24236 Linker options. Pass @code{opts} to the linker.
24237 @end table
24238
24239 @subsubheading @code{gnatdll} Example
24240
24241
24242 As an example the command to build a relocatable DLL from @code{api.adb}
24243 once @code{api.adb} has been compiled and @code{api.def} created is
24244
24245 @quotation
24246
24247 @example
24248 $ gnatdll -d api.dll api.ali
24249 @end example
24250 @end quotation
24251
24252 The above command creates two files: @code{libapi.dll.a} (the import
24253 library) and @code{api.dll} (the actual DLL). If you want to create
24254 only the DLL, just type:
24255
24256 @quotation
24257
24258 @example
24259 $ gnatdll -d api.dll -n api.ali
24260 @end example
24261 @end quotation
24262
24263 Alternatively if you want to create just the import library, type:
24264
24265 @quotation
24266
24267 @example
24268 $ gnatdll -d api.dll
24269 @end example
24270 @end quotation
24271
24272 @subsubheading @code{gnatdll} behind the Scenes
24273
24274
24275 This section details the steps involved in creating a DLL. @code{gnatdll}
24276 does these steps for you. Unless you are interested in understanding what
24277 goes on behind the scenes, you should skip this section.
24278
24279 We use the previous example of a DLL containing the Ada package @code{API},
24280 to illustrate the steps necessary to build a DLL. The starting point is a
24281 set of objects that will make up the DLL and the corresponding ALI
24282 files. In the case of this example this means that @code{api.o} and
24283 @code{api.ali} are available. To build a relocatable DLL, @code{gnatdll} does
24284 the following:
24285
24286
24287 @itemize *
24288
24289 @item
24290 @code{gnatdll} builds the base file (@code{api.base}). A base file gives
24291 the information necessary to generate relocation information for the
24292 DLL.
24293
24294 @example
24295 $ gnatbind -n api
24296 $ gnatlink api -o api.jnk -mdll -Wl,--base-file,api.base
24297 @end example
24298
24299 In addition to the base file, the @code{gnatlink} command generates an
24300 output file @code{api.jnk} which can be discarded. The @code{-mdll} switch
24301 asks @code{gnatlink} to generate the routines @code{DllMain} and
24302 @code{DllMainCRTStartup} that are called by the Windows loader when the DLL
24303 is loaded into memory.
24304
24305 @item
24306 @code{gnatdll} uses @code{dlltool} (see @ref{1f6,,Using dlltool}) to build the
24307 export table (@code{api.exp}). The export table contains the relocation
24308 information in a form which can be used during the final link to ensure
24309 that the Windows loader is able to place the DLL anywhere in memory.
24310
24311 @example
24312 $ dlltool --dllname api.dll --def api.def --base-file api.base \\
24313 --output-exp api.exp
24314 @end example
24315
24316 @item
24317 @code{gnatdll} builds the base file using the new export table. Note that
24318 @code{gnatbind} must be called once again since the binder generated file
24319 has been deleted during the previous call to @code{gnatlink}.
24320
24321 @example
24322 $ gnatbind -n api
24323 $ gnatlink api -o api.jnk api.exp -mdll
24324 -Wl,--base-file,api.base
24325 @end example
24326
24327 @item
24328 @code{gnatdll} builds the new export table using the new base file and
24329 generates the DLL import library @code{libAPI.dll.a}.
24330
24331 @example
24332 $ dlltool --dllname api.dll --def api.def --base-file api.base \\
24333 --output-exp api.exp --output-lib libAPI.a
24334 @end example
24335
24336 @item
24337 Finally @code{gnatdll} builds the relocatable DLL using the final export
24338 table.
24339
24340 @example
24341 $ gnatbind -n api
24342 $ gnatlink api api.exp -o api.dll -mdll
24343 @end example
24344 @end itemize
24345 @anchor{gnat_ugn/platform_specific_information using-dlltool}@anchor{1f6}
24346 @subsubheading Using @code{dlltool}
24347
24348
24349 @code{dlltool} is the low-level tool used by @code{gnatdll} to build
24350 DLLs and static import libraries. This section summarizes the most
24351 common @code{dlltool} switches. The form of the @code{dlltool} command
24352 is
24353
24354 @quotation
24355
24356 @example
24357 $ dlltool [`switches`]
24358 @end example
24359 @end quotation
24360
24361 @code{dlltool} switches include:
24362
24363 @geindex --base-file (dlltool)
24364
24365
24366 @table @asis
24367
24368 @item @code{--base-file @emph{basefile}}
24369
24370 Read the base file @code{basefile} generated by the linker. This switch
24371 is used to create a relocatable DLL.
24372 @end table
24373
24374 @geindex --def (dlltool)
24375
24376
24377 @table @asis
24378
24379 @item @code{--def @emph{deffile}}
24380
24381 Read the definition file.
24382 @end table
24383
24384 @geindex --dllname (dlltool)
24385
24386
24387 @table @asis
24388
24389 @item @code{--dllname @emph{name}}
24390
24391 Gives the name of the DLL. This switch is used to embed the name of the
24392 DLL in the static import library generated by @code{dlltool} with switch
24393 @code{--output-lib}.
24394 @end table
24395
24396 @geindex -k (dlltool)
24397
24398
24399 @table @asis
24400
24401 @item @code{-k}
24402
24403 Kill @code{@@@emph{nn}} from exported names
24404 (@ref{1d0,,Windows Calling Conventions}
24405 for a discussion about @code{Stdcall}-style symbols.
24406 @end table
24407
24408 @geindex --help (dlltool)
24409
24410
24411 @table @asis
24412
24413 @item @code{--help}
24414
24415 Prints the @code{dlltool} switches with a concise description.
24416 @end table
24417
24418 @geindex --output-exp (dlltool)
24419
24420
24421 @table @asis
24422
24423 @item @code{--output-exp @emph{exportfile}}
24424
24425 Generate an export file @code{exportfile}. The export file contains the
24426 export table (list of symbols in the DLL) and is used to create the DLL.
24427 @end table
24428
24429 @geindex --output-lib (dlltool)
24430
24431
24432 @table @asis
24433
24434 @item @code{--output-lib @emph{libfile}}
24435
24436 Generate a static import library @code{libfile}.
24437 @end table
24438
24439 @geindex -v (dlltool)
24440
24441
24442 @table @asis
24443
24444 @item @code{-v}
24445
24446 Verbose mode.
24447 @end table
24448
24449 @geindex --as (dlltool)
24450
24451
24452 @table @asis
24453
24454 @item @code{--as @emph{assembler-name}}
24455
24456 Use @code{assembler-name} as the assembler. The default is @code{as}.
24457 @end table
24458
24459 @node GNAT and Windows Resources,Using GNAT DLLs from Microsoft Visual Studio Applications,Creating a Spec for Ada DLLs,Mixed-Language Programming on Windows
24460 @anchor{gnat_ugn/platform_specific_information gnat-and-windows-resources}@anchor{1f7}@anchor{gnat_ugn/platform_specific_information id32}@anchor{1f8}
24461 @subsubsection GNAT and Windows Resources
24462
24463
24464 @geindex Resources
24465 @geindex windows
24466
24467 Resources are an easy way to add Windows specific objects to your
24468 application. The objects that can be added as resources include:
24469
24470
24471 @itemize *
24472
24473 @item
24474 menus
24475
24476 @item
24477 accelerators
24478
24479 @item
24480 dialog boxes
24481
24482 @item
24483 string tables
24484
24485 @item
24486 bitmaps
24487
24488 @item
24489 cursors
24490
24491 @item
24492 icons
24493
24494 @item
24495 fonts
24496
24497 @item
24498 version information
24499 @end itemize
24500
24501 For example, a version information resource can be defined as follow and
24502 embedded into an executable or DLL:
24503
24504 A version information resource can be used to embed information into an
24505 executable or a DLL. These information can be viewed using the file properties
24506 from the Windows Explorer. Here is an example of a version information
24507 resource:
24508
24509 @quotation
24510
24511 @example
24512 1 VERSIONINFO
24513 FILEVERSION 1,0,0,0
24514 PRODUCTVERSION 1,0,0,0
24515 BEGIN
24516 BLOCK "StringFileInfo"
24517 BEGIN
24518 BLOCK "080904E4"
24519 BEGIN
24520 VALUE "CompanyName", "My Company Name"
24521 VALUE "FileDescription", "My application"
24522 VALUE "FileVersion", "1.0"
24523 VALUE "InternalName", "my_app"
24524 VALUE "LegalCopyright", "My Name"
24525 VALUE "OriginalFilename", "my_app.exe"
24526 VALUE "ProductName", "My App"
24527 VALUE "ProductVersion", "1.0"
24528 END
24529 END
24530
24531 BLOCK "VarFileInfo"
24532 BEGIN
24533 VALUE "Translation", 0x809, 1252
24534 END
24535 END
24536 @end example
24537 @end quotation
24538
24539 The value @code{0809} (langID) is for the U.K English language and
24540 @code{04E4} (charsetID), which is equal to @code{1252} decimal, for
24541 multilingual.
24542
24543 This section explains how to build, compile and use resources. Note that this
24544 section does not cover all resource objects, for a complete description see
24545 the corresponding Microsoft documentation.
24546
24547 @menu
24548 * Building Resources::
24549 * Compiling Resources::
24550 * Using Resources::
24551
24552 @end menu
24553
24554 @node Building Resources,Compiling Resources,,GNAT and Windows Resources
24555 @anchor{gnat_ugn/platform_specific_information building-resources}@anchor{1f9}@anchor{gnat_ugn/platform_specific_information id33}@anchor{1fa}
24556 @subsubsection Building Resources
24557
24558
24559 @geindex Resources
24560 @geindex building
24561
24562 A resource file is an ASCII file. By convention resource files have an
24563 @code{.rc} extension.
24564 The easiest way to build a resource file is to use Microsoft tools
24565 such as @code{imagedit.exe} to build bitmaps, icons and cursors and
24566 @code{dlgedit.exe} to build dialogs.
24567 It is always possible to build an @code{.rc} file yourself by writing a
24568 resource script.
24569
24570 It is not our objective to explain how to write a resource file. A
24571 complete description of the resource script language can be found in the
24572 Microsoft documentation.
24573
24574 @node Compiling Resources,Using Resources,Building Resources,GNAT and Windows Resources
24575 @anchor{gnat_ugn/platform_specific_information compiling-resources}@anchor{1fb}@anchor{gnat_ugn/platform_specific_information id34}@anchor{1fc}
24576 @subsubsection Compiling Resources
24577
24578
24579 @geindex rc
24580
24581 @geindex windres
24582
24583 @geindex Resources
24584 @geindex compiling
24585
24586 This section describes how to build a GNAT-compatible (COFF) object file
24587 containing the resources. This is done using the Resource Compiler
24588 @code{windres} as follows:
24589
24590 @quotation
24591
24592 @example
24593 $ windres -i myres.rc -o myres.o
24594 @end example
24595 @end quotation
24596
24597 By default @code{windres} will run @code{gcc} to preprocess the @code{.rc}
24598 file. You can specify an alternate preprocessor (usually named
24599 @code{cpp.exe}) using the @code{windres} @code{--preprocessor}
24600 parameter. A list of all possible options may be obtained by entering
24601 the command @code{windres} @code{--help}.
24602
24603 It is also possible to use the Microsoft resource compiler @code{rc.exe}
24604 to produce a @code{.res} file (binary resource file). See the
24605 corresponding Microsoft documentation for further details. In this case
24606 you need to use @code{windres} to translate the @code{.res} file to a
24607 GNAT-compatible object file as follows:
24608
24609 @quotation
24610
24611 @example
24612 $ windres -i myres.res -o myres.o
24613 @end example
24614 @end quotation
24615
24616 @node Using Resources,,Compiling Resources,GNAT and Windows Resources
24617 @anchor{gnat_ugn/platform_specific_information id35}@anchor{1fd}@anchor{gnat_ugn/platform_specific_information using-resources}@anchor{1fe}
24618 @subsubsection Using Resources
24619
24620
24621 @geindex Resources
24622 @geindex using
24623
24624 To include the resource file in your program just add the
24625 GNAT-compatible object file for the resource(s) to the linker
24626 arguments. With @code{gnatmake} this is done by using the @code{-largs}
24627 option:
24628
24629 @quotation
24630
24631 @example
24632 $ gnatmake myprog -largs myres.o
24633 @end example
24634 @end quotation
24635
24636 @node Using GNAT DLLs from Microsoft Visual Studio Applications,Debugging a DLL,GNAT and Windows Resources,Mixed-Language Programming on Windows
24637 @anchor{gnat_ugn/platform_specific_information using-gnat-dll-from-msvs}@anchor{1ff}@anchor{gnat_ugn/platform_specific_information using-gnat-dlls-from-microsoft-visual-studio-applications}@anchor{200}
24638 @subsubsection Using GNAT DLLs from Microsoft Visual Studio Applications
24639
24640
24641 @geindex Microsoft Visual Studio
24642 @geindex use with GNAT DLLs
24643
24644 This section describes a common case of mixed GNAT/Microsoft Visual Studio
24645 application development, where the main program is developed using MSVS, and
24646 is linked with a DLL developed using GNAT. Such a mixed application should
24647 be developed following the general guidelines outlined above; below is the
24648 cookbook-style sequence of steps to follow:
24649
24650
24651 @enumerate
24652
24653 @item
24654 First develop and build the GNAT shared library using a library project
24655 (let’s assume the project is @code{mylib.gpr}, producing the library @code{libmylib.dll}):
24656 @end enumerate
24657
24658 @quotation
24659
24660 @example
24661 $ gprbuild -p mylib.gpr
24662 @end example
24663 @end quotation
24664
24665
24666 @enumerate 2
24667
24668 @item
24669 Produce a .def file for the symbols you need to interface with, either by
24670 hand or automatically with possibly some manual adjustments
24671 (see @ref{1e1,,Creating Definition File Automatically}):
24672 @end enumerate
24673
24674 @quotation
24675
24676 @example
24677 $ dlltool libmylib.dll -z libmylib.def --export-all-symbols
24678 @end example
24679 @end quotation
24680
24681
24682 @enumerate 3
24683
24684 @item
24685 Make sure that MSVS command-line tools are accessible on the path.
24686
24687 @item
24688 Create the Microsoft-style import library (see @ref{1e4,,MSVS-Style Import Library}):
24689 @end enumerate
24690
24691 @quotation
24692
24693 @example
24694 $ lib -machine:IX86 -def:libmylib.def -out:libmylib.lib
24695 @end example
24696 @end quotation
24697
24698 If you are using a 64-bit toolchain, the above becomes…
24699
24700 @quotation
24701
24702 @example
24703 $ lib -machine:X64 -def:libmylib.def -out:libmylib.lib
24704 @end example
24705 @end quotation
24706
24707
24708 @enumerate 5
24709
24710 @item
24711 Build the C main
24712 @end enumerate
24713
24714 @quotation
24715
24716 @example
24717 $ cl /O2 /MD main.c libmylib.lib
24718 @end example
24719 @end quotation
24720
24721
24722 @enumerate 6
24723
24724 @item
24725 Before running the executable, make sure you have set the PATH to the DLL,
24726 or copy the DLL into into the directory containing the .exe.
24727 @end enumerate
24728
24729 @node Debugging a DLL,Setting Stack Size from gnatlink,Using GNAT DLLs from Microsoft Visual Studio Applications,Mixed-Language Programming on Windows
24730 @anchor{gnat_ugn/platform_specific_information debugging-a-dll}@anchor{201}@anchor{gnat_ugn/platform_specific_information id36}@anchor{202}
24731 @subsubsection Debugging a DLL
24732
24733
24734 @geindex DLL debugging
24735
24736 Debugging a DLL is similar to debugging a standard program. But
24737 we have to deal with two different executable parts: the DLL and the
24738 program that uses it. We have the following four possibilities:
24739
24740
24741 @itemize *
24742
24743 @item
24744 The program and the DLL are built with GCC/GNAT.
24745
24746 @item
24747 The program is built with foreign tools and the DLL is built with
24748 GCC/GNAT.
24749
24750 @item
24751 The program is built with GCC/GNAT and the DLL is built with
24752 foreign tools.
24753 @end itemize
24754
24755 In this section we address only cases one and two above.
24756 There is no point in trying to debug
24757 a DLL with GNU/GDB, if there is no GDB-compatible debugging
24758 information in it. To do so you must use a debugger compatible with the
24759 tools suite used to build the DLL.
24760
24761 @menu
24762 * Program and DLL Both Built with GCC/GNAT::
24763 * Program Built with Foreign Tools and DLL Built with GCC/GNAT::
24764
24765 @end menu
24766
24767 @node Program and DLL Both Built with GCC/GNAT,Program Built with Foreign Tools and DLL Built with GCC/GNAT,,Debugging a DLL
24768 @anchor{gnat_ugn/platform_specific_information id37}@anchor{203}@anchor{gnat_ugn/platform_specific_information program-and-dll-both-built-with-gcc-gnat}@anchor{204}
24769 @subsubsection Program and DLL Both Built with GCC/GNAT
24770
24771
24772 This is the simplest case. Both the DLL and the program have @code{GDB}
24773 compatible debugging information. It is then possible to break anywhere in
24774 the process. Let’s suppose here that the main procedure is named
24775 @code{ada_main} and that in the DLL there is an entry point named
24776 @code{ada_dll}.
24777
24778 The DLL (@ref{1da,,Introduction to Dynamic Link Libraries (DLLs)}) and
24779 program must have been built with the debugging information (see GNAT -g
24780 switch). Here are the step-by-step instructions for debugging it:
24781
24782
24783 @itemize *
24784
24785 @item
24786 Launch @code{GDB} on the main program.
24787
24788 @example
24789 $ gdb -nw ada_main
24790 @end example
24791
24792 @item
24793 Start the program and stop at the beginning of the main procedure
24794
24795 @example
24796 (gdb) start
24797 @end example
24798
24799 This step is required to be able to set a breakpoint inside the DLL. As long
24800 as the program is not run, the DLL is not loaded. This has the
24801 consequence that the DLL debugging information is also not loaded, so it is not
24802 possible to set a breakpoint in the DLL.
24803
24804 @item
24805 Set a breakpoint inside the DLL
24806
24807 @example
24808 (gdb) break ada_dll
24809 (gdb) cont
24810 @end example
24811 @end itemize
24812
24813 At this stage a breakpoint is set inside the DLL. From there on
24814 you can use the standard approach to debug the whole program
24815 (@ref{14d,,Running and Debugging Ada Programs}).
24816
24817 @node Program Built with Foreign Tools and DLL Built with GCC/GNAT,,Program and DLL Both Built with GCC/GNAT,Debugging a DLL
24818 @anchor{gnat_ugn/platform_specific_information id38}@anchor{205}@anchor{gnat_ugn/platform_specific_information program-built-with-foreign-tools-and-dll-built-with-gcc-gnat}@anchor{206}
24819 @subsubsection Program Built with Foreign Tools and DLL Built with GCC/GNAT
24820
24821
24822 In this case things are slightly more complex because it is not possible to
24823 start the main program and then break at the beginning to load the DLL and the
24824 associated DLL debugging information. It is not possible to break at the
24825 beginning of the program because there is no @code{GDB} debugging information,
24826 and therefore there is no direct way of getting initial control. This
24827 section addresses this issue by describing some methods that can be used
24828 to break somewhere in the DLL to debug it.
24829
24830 First suppose that the main procedure is named @code{main} (this is for
24831 example some C code built with Microsoft Visual C) and that there is a
24832 DLL named @code{test.dll} containing an Ada entry point named
24833 @code{ada_dll}.
24834
24835 The DLL (see @ref{1da,,Introduction to Dynamic Link Libraries (DLLs)}) must have
24836 been built with debugging information (see the GNAT @code{-g} option).
24837
24838 @subsubheading Debugging the DLL Directly
24839
24840
24841
24842 @itemize *
24843
24844 @item
24845 Find out the executable starting address
24846
24847 @example
24848 $ objdump --file-header main.exe
24849 @end example
24850
24851 The starting address is reported on the last line. For example:
24852
24853 @example
24854 main.exe: file format pei-i386
24855 architecture: i386, flags 0x0000010a:
24856 EXEC_P, HAS_DEBUG, D_PAGED
24857 start address 0x00401010
24858 @end example
24859
24860 @item
24861 Launch the debugger on the executable.
24862
24863 @example
24864 $ gdb main.exe
24865 @end example
24866
24867 @item
24868 Set a breakpoint at the starting address, and launch the program.
24869
24870 @example
24871 $ (gdb) break *0x00401010
24872 $ (gdb) run
24873 @end example
24874
24875 The program will stop at the given address.
24876
24877 @item
24878 Set a breakpoint on a DLL subroutine.
24879
24880 @example
24881 (gdb) break ada_dll.adb:45
24882 @end example
24883
24884 Or if you want to break using a symbol on the DLL, you need first to
24885 select the Ada language (language used by the DLL).
24886
24887 @example
24888 (gdb) set language ada
24889 (gdb) break ada_dll
24890 @end example
24891
24892 @item
24893 Continue the program.
24894
24895 @example
24896 (gdb) cont
24897 @end example
24898
24899 This will run the program until it reaches the breakpoint that has been
24900 set. From that point you can use the standard way to debug a program
24901 as described in (@ref{14d,,Running and Debugging Ada Programs}).
24902 @end itemize
24903
24904 It is also possible to debug the DLL by attaching to a running process.
24905
24906 @subsubheading Attaching to a Running Process
24907
24908
24909 @geindex DLL debugging
24910 @geindex attach to process
24911
24912 With @code{GDB} it is always possible to debug a running process by
24913 attaching to it. It is possible to debug a DLL this way. The limitation
24914 of this approach is that the DLL must run long enough to perform the
24915 attach operation. It may be useful for instance to insert a time wasting
24916 loop in the code of the DLL to meet this criterion.
24917
24918
24919 @itemize *
24920
24921 @item
24922 Launch the main program @code{main.exe}.
24923
24924 @example
24925 $ main
24926 @end example
24927
24928 @item
24929 Use the Windows @emph{Task Manager} to find the process ID. Let’s say
24930 that the process PID for @code{main.exe} is 208.
24931
24932 @item
24933 Launch gdb.
24934
24935 @example
24936 $ gdb
24937 @end example
24938
24939 @item
24940 Attach to the running process to be debugged.
24941
24942 @example
24943 (gdb) attach 208
24944 @end example
24945
24946 @item
24947 Load the process debugging information.
24948
24949 @example
24950 (gdb) symbol-file main.exe
24951 @end example
24952
24953 @item
24954 Break somewhere in the DLL.
24955
24956 @example
24957 (gdb) break ada_dll
24958 @end example
24959
24960 @item
24961 Continue process execution.
24962
24963 @example
24964 (gdb) cont
24965 @end example
24966 @end itemize
24967
24968 This last step will resume the process execution, and stop at
24969 the breakpoint we have set. From there you can use the standard
24970 approach to debug a program as described in
24971 @ref{14d,,Running and Debugging Ada Programs}.
24972
24973 @node Setting Stack Size from gnatlink,Setting Heap Size from gnatlink,Debugging a DLL,Mixed-Language Programming on Windows
24974 @anchor{gnat_ugn/platform_specific_information id39}@anchor{207}@anchor{gnat_ugn/platform_specific_information setting-stack-size-from-gnatlink}@anchor{127}
24975 @subsubsection Setting Stack Size from @code{gnatlink}
24976
24977
24978 It is possible to specify the program stack size at link time. On modern
24979 versions of Windows, starting with XP, this is mostly useful to set the size of
24980 the main stack (environment task). The other task stacks are set with pragma
24981 Storage_Size or with the @emph{gnatbind -d} command.
24982
24983 Since older versions of Windows (2000, NT4, etc.) do not allow setting the
24984 reserve size of individual tasks, the link-time stack size applies to all
24985 tasks, and pragma Storage_Size has no effect.
24986 In particular, Stack Overflow checks are made against this
24987 link-time specified size.
24988
24989 This setting can be done with @code{gnatlink} using either of the following:
24990
24991
24992 @itemize *
24993
24994 @item
24995 @code{-Xlinker} linker option
24996
24997 @example
24998 $ gnatlink hello -Xlinker --stack=0x10000,0x1000
24999 @end example
25000
25001 This sets the stack reserve size to 0x10000 bytes and the stack commit
25002 size to 0x1000 bytes.
25003
25004 @item
25005 @code{-Wl} linker option
25006
25007 @example
25008 $ gnatlink hello -Wl,--stack=0x1000000
25009 @end example
25010
25011 This sets the stack reserve size to 0x1000000 bytes. Note that with
25012 @code{-Wl} option it is not possible to set the stack commit size
25013 because the comma is a separator for this option.
25014 @end itemize
25015
25016 @node Setting Heap Size from gnatlink,,Setting Stack Size from gnatlink,Mixed-Language Programming on Windows
25017 @anchor{gnat_ugn/platform_specific_information id40}@anchor{208}@anchor{gnat_ugn/platform_specific_information setting-heap-size-from-gnatlink}@anchor{128}
25018 @subsubsection Setting Heap Size from @code{gnatlink}
25019
25020
25021 Under Windows systems, it is possible to specify the program heap size from
25022 @code{gnatlink} using either of the following:
25023
25024
25025 @itemize *
25026
25027 @item
25028 @code{-Xlinker} linker option
25029
25030 @example
25031 $ gnatlink hello -Xlinker --heap=0x10000,0x1000
25032 @end example
25033
25034 This sets the heap reserve size to 0x10000 bytes and the heap commit
25035 size to 0x1000 bytes.
25036
25037 @item
25038 @code{-Wl} linker option
25039
25040 @example
25041 $ gnatlink hello -Wl,--heap=0x1000000
25042 @end example
25043
25044 This sets the heap reserve size to 0x1000000 bytes. Note that with
25045 @code{-Wl} option it is not possible to set the heap commit size
25046 because the comma is a separator for this option.
25047 @end itemize
25048
25049 @node Windows Specific Add-Ons,,Mixed-Language Programming on Windows,Microsoft Windows Topics
25050 @anchor{gnat_ugn/platform_specific_information win32-specific-addons}@anchor{209}@anchor{gnat_ugn/platform_specific_information windows-specific-add-ons}@anchor{20a}
25051 @subsection Windows Specific Add-Ons
25052
25053
25054 This section describes the Windows specific add-ons.
25055
25056 @menu
25057 * Win32Ada::
25058 * wPOSIX::
25059
25060 @end menu
25061
25062 @node Win32Ada,wPOSIX,,Windows Specific Add-Ons
25063 @anchor{gnat_ugn/platform_specific_information id41}@anchor{20b}@anchor{gnat_ugn/platform_specific_information win32ada}@anchor{20c}
25064 @subsubsection Win32Ada
25065
25066
25067 Win32Ada is a binding for the Microsoft Win32 API. This binding can be
25068 easily installed from the provided installer. To use the Win32Ada
25069 binding you need to use a project file, and adding a single with_clause
25070 will give you full access to the Win32Ada binding sources and ensure
25071 that the proper libraries are passed to the linker.
25072
25073 @quotation
25074
25075 @example
25076 with "win32ada";
25077 project P is
25078 for Sources use ...;
25079 end P;
25080 @end example
25081 @end quotation
25082
25083 To build the application you just need to call gprbuild for the
25084 application’s project, here p.gpr:
25085
25086 @quotation
25087
25088 @example
25089 gprbuild p.gpr
25090 @end example
25091 @end quotation
25092
25093 @node wPOSIX,,Win32Ada,Windows Specific Add-Ons
25094 @anchor{gnat_ugn/platform_specific_information id42}@anchor{20d}@anchor{gnat_ugn/platform_specific_information wposix}@anchor{20e}
25095 @subsubsection wPOSIX
25096
25097
25098 wPOSIX is a minimal POSIX binding whose goal is to help with building
25099 cross-platforms applications. This binding is not complete though, as
25100 the Win32 API does not provide the necessary support for all POSIX APIs.
25101
25102 To use the wPOSIX binding you need to use a project file, and adding
25103 a single with_clause will give you full access to the wPOSIX binding
25104 sources and ensure that the proper libraries are passed to the linker.
25105
25106 @quotation
25107
25108 @example
25109 with "wposix";
25110 project P is
25111 for Sources use ...;
25112 end P;
25113 @end example
25114 @end quotation
25115
25116 To build the application you just need to call gprbuild for the
25117 application’s project, here p.gpr:
25118
25119 @quotation
25120
25121 @example
25122 gprbuild p.gpr
25123 @end example
25124 @end quotation
25125
25126 @node Mac OS Topics,,Microsoft Windows Topics,Platform-Specific Information
25127 @anchor{gnat_ugn/platform_specific_information id43}@anchor{20f}@anchor{gnat_ugn/platform_specific_information mac-os-topics}@anchor{210}
25128 @section Mac OS Topics
25129
25130
25131 @geindex OS X
25132
25133 This section describes topics that are specific to Apple’s OS X
25134 platform.
25135
25136 @menu
25137 * Codesigning the Debugger::
25138
25139 @end menu
25140
25141 @node Codesigning the Debugger,,,Mac OS Topics
25142 @anchor{gnat_ugn/platform_specific_information codesigning-the-debugger}@anchor{211}
25143 @subsection Codesigning the Debugger
25144
25145
25146 The Darwin Kernel requires the debugger to have special permissions
25147 before it is allowed to control other processes. These permissions
25148 are granted by codesigning the GDB executable. Without these
25149 permissions, the debugger will report error messages such as:
25150
25151 @example
25152 Starting program: /x/y/foo
25153 Unable to find Mach task port for process-id 28885: (os/kern) failure (0x5).
25154 (please check gdb is codesigned - see taskgated(8))
25155 @end example
25156
25157 Codesigning requires a certificate. The following procedure explains
25158 how to create one:
25159
25160
25161 @itemize *
25162
25163 @item
25164 Start the Keychain Access application (in
25165 /Applications/Utilities/Keychain Access.app)
25166
25167 @item
25168 Select the Keychain Access -> Certificate Assistant ->
25169 Create a Certificate… menu
25170
25171 @item
25172 Then:
25173
25174
25175 @itemize *
25176
25177 @item
25178 Choose a name for the new certificate (this procedure will use
25179 “gdb-cert” as an example)
25180
25181 @item
25182 Set “Identity Type” to “Self Signed Root”
25183
25184 @item
25185 Set “Certificate Type” to “Code Signing”
25186
25187 @item
25188 Activate the “Let me override defaults” option
25189 @end itemize
25190
25191 @item
25192 Click several times on “Continue” until the “Specify a Location
25193 For The Certificate” screen appears, then set “Keychain” to “System”
25194
25195 @item
25196 Click on “Continue” until the certificate is created
25197
25198 @item
25199 Finally, in the view, double-click on the new certificate,
25200 and set “When using this certificate” to “Always Trust”
25201
25202 @item
25203 Exit the Keychain Access application and restart the computer
25204 (this is unfortunately required)
25205 @end itemize
25206
25207 Once a certificate has been created, the debugger can be codesigned
25208 as follow. In a Terminal, run the following command:
25209
25210 @quotation
25211
25212 @example
25213 $ codesign -f -s "gdb-cert" <gnat_install_prefix>/bin/gdb
25214 @end example
25215 @end quotation
25216
25217 where “gdb-cert” should be replaced by the actual certificate
25218 name chosen above, and <gnat_install_prefix> should be replaced by
25219 the location where you installed GNAT. Also, be sure that users are
25220 in the Unix group @code{_developer}.
25221
25222 @node Example of Binder Output File,Elaboration Order Handling in GNAT,Platform-Specific Information,Top
25223 @anchor{gnat_ugn/example_of_binder_output doc}@anchor{212}@anchor{gnat_ugn/example_of_binder_output example-of-binder-output-file}@anchor{e}@anchor{gnat_ugn/example_of_binder_output id1}@anchor{213}
25224 @chapter Example of Binder Output File
25225
25226
25227 @geindex Binder output (example)
25228
25229 This Appendix displays the source code for the output file
25230 generated by @emph{gnatbind} for a simple ‘Hello World’ program.
25231 Comments have been added for clarification purposes.
25232
25233 @example
25234 -- The package is called Ada_Main unless this name is actually used
25235 -- as a unit name in the partition, in which case some other unique
25236 -- name is used.
25237
25238 pragma Ada_95;
25239 with System;
25240 package ada_main is
25241 pragma Warnings (Off);
25242
25243 -- The main program saves the parameters (argument count,
25244 -- argument values, environment pointer) in global variables
25245 -- for later access by other units including
25246 -- Ada.Command_Line.
25247
25248 gnat_argc : Integer;
25249 gnat_argv : System.Address;
25250 gnat_envp : System.Address;
25251
25252 -- The actual variables are stored in a library routine. This
25253 -- is useful for some shared library situations, where there
25254 -- are problems if variables are not in the library.
25255
25256 pragma Import (C, gnat_argc);
25257 pragma Import (C, gnat_argv);
25258 pragma Import (C, gnat_envp);
25259
25260 -- The exit status is similarly an external location
25261
25262 gnat_exit_status : Integer;
25263 pragma Import (C, gnat_exit_status);
25264
25265 GNAT_Version : constant String :=
25266 "GNAT Version: Pro 7.4.0w (20141119-49)" & ASCII.NUL;
25267 pragma Export (C, GNAT_Version, "__gnat_version");
25268
25269 Ada_Main_Program_Name : constant String := "_ada_hello" & ASCII.NUL;
25270 pragma Export (C, Ada_Main_Program_Name, "__gnat_ada_main_program_name");
25271
25272 -- This is the generated adainit routine that performs
25273 -- initialization at the start of execution. In the case
25274 -- where Ada is the main program, this main program makes
25275 -- a call to adainit at program startup.
25276
25277 procedure adainit;
25278 pragma Export (C, adainit, "adainit");
25279
25280 -- This is the generated adafinal routine that performs
25281 -- finalization at the end of execution. In the case where
25282 -- Ada is the main program, this main program makes a call
25283 -- to adafinal at program termination.
25284
25285 procedure adafinal;
25286 pragma Export (C, adafinal, "adafinal");
25287
25288 -- This routine is called at the start of execution. It is
25289 -- a dummy routine that is used by the debugger to breakpoint
25290 -- at the start of execution.
25291
25292 -- This is the actual generated main program (it would be
25293 -- suppressed if the no main program switch were used). As
25294 -- required by standard system conventions, this program has
25295 -- the external name main.
25296
25297 function main
25298 (argc : Integer;
25299 argv : System.Address;
25300 envp : System.Address)
25301 return Integer;
25302 pragma Export (C, main, "main");
25303
25304 -- The following set of constants give the version
25305 -- identification values for every unit in the bound
25306 -- partition. This identification is computed from all
25307 -- dependent semantic units, and corresponds to the
25308 -- string that would be returned by use of the
25309 -- Body_Version or Version attributes.
25310
25311 -- The following Export pragmas export the version numbers
25312 -- with symbolic names ending in B (for body) or S
25313 -- (for spec) so that they can be located in a link. The
25314 -- information provided here is sufficient to track down
25315 -- the exact versions of units used in a given build.
25316
25317 type Version_32 is mod 2 ** 32;
25318 u00001 : constant Version_32 := 16#8ad6e54a#;
25319 pragma Export (C, u00001, "helloB");
25320 u00002 : constant Version_32 := 16#fbff4c67#;
25321 pragma Export (C, u00002, "system__standard_libraryB");
25322 u00003 : constant Version_32 := 16#1ec6fd90#;
25323 pragma Export (C, u00003, "system__standard_libraryS");
25324 u00004 : constant Version_32 := 16#3ffc8e18#;
25325 pragma Export (C, u00004, "adaS");
25326 u00005 : constant Version_32 := 16#28f088c2#;
25327 pragma Export (C, u00005, "ada__text_ioB");
25328 u00006 : constant Version_32 := 16#f372c8ac#;
25329 pragma Export (C, u00006, "ada__text_ioS");
25330 u00007 : constant Version_32 := 16#2c143749#;
25331 pragma Export (C, u00007, "ada__exceptionsB");
25332 u00008 : constant Version_32 := 16#f4f0cce8#;
25333 pragma Export (C, u00008, "ada__exceptionsS");
25334 u00009 : constant Version_32 := 16#a46739c0#;
25335 pragma Export (C, u00009, "ada__exceptions__last_chance_handlerB");
25336 u00010 : constant Version_32 := 16#3aac8c92#;
25337 pragma Export (C, u00010, "ada__exceptions__last_chance_handlerS");
25338 u00011 : constant Version_32 := 16#1d274481#;
25339 pragma Export (C, u00011, "systemS");
25340 u00012 : constant Version_32 := 16#a207fefe#;
25341 pragma Export (C, u00012, "system__soft_linksB");
25342 u00013 : constant Version_32 := 16#467d9556#;
25343 pragma Export (C, u00013, "system__soft_linksS");
25344 u00014 : constant Version_32 := 16#b01dad17#;
25345 pragma Export (C, u00014, "system__parametersB");
25346 u00015 : constant Version_32 := 16#630d49fe#;
25347 pragma Export (C, u00015, "system__parametersS");
25348 u00016 : constant Version_32 := 16#b19b6653#;
25349 pragma Export (C, u00016, "system__secondary_stackB");
25350 u00017 : constant Version_32 := 16#b6468be8#;
25351 pragma Export (C, u00017, "system__secondary_stackS");
25352 u00018 : constant Version_32 := 16#39a03df9#;
25353 pragma Export (C, u00018, "system__storage_elementsB");
25354 u00019 : constant Version_32 := 16#30e40e85#;
25355 pragma Export (C, u00019, "system__storage_elementsS");
25356 u00020 : constant Version_32 := 16#41837d1e#;
25357 pragma Export (C, u00020, "system__stack_checkingB");
25358 u00021 : constant Version_32 := 16#93982f69#;
25359 pragma Export (C, u00021, "system__stack_checkingS");
25360 u00022 : constant Version_32 := 16#393398c1#;
25361 pragma Export (C, u00022, "system__exception_tableB");
25362 u00023 : constant Version_32 := 16#b33e2294#;
25363 pragma Export (C, u00023, "system__exception_tableS");
25364 u00024 : constant Version_32 := 16#ce4af020#;
25365 pragma Export (C, u00024, "system__exceptionsB");
25366 u00025 : constant Version_32 := 16#75442977#;
25367 pragma Export (C, u00025, "system__exceptionsS");
25368 u00026 : constant Version_32 := 16#37d758f1#;
25369 pragma Export (C, u00026, "system__exceptions__machineS");
25370 u00027 : constant Version_32 := 16#b895431d#;
25371 pragma Export (C, u00027, "system__exceptions_debugB");
25372 u00028 : constant Version_32 := 16#aec55d3f#;
25373 pragma Export (C, u00028, "system__exceptions_debugS");
25374 u00029 : constant Version_32 := 16#570325c8#;
25375 pragma Export (C, u00029, "system__img_intB");
25376 u00030 : constant Version_32 := 16#1ffca443#;
25377 pragma Export (C, u00030, "system__img_intS");
25378 u00031 : constant Version_32 := 16#b98c3e16#;
25379 pragma Export (C, u00031, "system__tracebackB");
25380 u00032 : constant Version_32 := 16#831a9d5a#;
25381 pragma Export (C, u00032, "system__tracebackS");
25382 u00033 : constant Version_32 := 16#9ed49525#;
25383 pragma Export (C, u00033, "system__traceback_entriesB");
25384 u00034 : constant Version_32 := 16#1d7cb2f1#;
25385 pragma Export (C, u00034, "system__traceback_entriesS");
25386 u00035 : constant Version_32 := 16#8c33a517#;
25387 pragma Export (C, u00035, "system__wch_conB");
25388 u00036 : constant Version_32 := 16#065a6653#;
25389 pragma Export (C, u00036, "system__wch_conS");
25390 u00037 : constant Version_32 := 16#9721e840#;
25391 pragma Export (C, u00037, "system__wch_stwB");
25392 u00038 : constant Version_32 := 16#2b4b4a52#;
25393 pragma Export (C, u00038, "system__wch_stwS");
25394 u00039 : constant Version_32 := 16#92b797cb#;
25395 pragma Export (C, u00039, "system__wch_cnvB");
25396 u00040 : constant Version_32 := 16#09eddca0#;
25397 pragma Export (C, u00040, "system__wch_cnvS");
25398 u00041 : constant Version_32 := 16#6033a23f#;
25399 pragma Export (C, u00041, "interfacesS");
25400 u00042 : constant Version_32 := 16#ece6fdb6#;
25401 pragma Export (C, u00042, "system__wch_jisB");
25402 u00043 : constant Version_32 := 16#899dc581#;
25403 pragma Export (C, u00043, "system__wch_jisS");
25404 u00044 : constant Version_32 := 16#10558b11#;
25405 pragma Export (C, u00044, "ada__streamsB");
25406 u00045 : constant Version_32 := 16#2e6701ab#;
25407 pragma Export (C, u00045, "ada__streamsS");
25408 u00046 : constant Version_32 := 16#db5c917c#;
25409 pragma Export (C, u00046, "ada__io_exceptionsS");
25410 u00047 : constant Version_32 := 16#12c8cd7d#;
25411 pragma Export (C, u00047, "ada__tagsB");
25412 u00048 : constant Version_32 := 16#ce72c228#;
25413 pragma Export (C, u00048, "ada__tagsS");
25414 u00049 : constant Version_32 := 16#c3335bfd#;
25415 pragma Export (C, u00049, "system__htableB");
25416 u00050 : constant Version_32 := 16#99e5f76b#;
25417 pragma Export (C, u00050, "system__htableS");
25418 u00051 : constant Version_32 := 16#089f5cd0#;
25419 pragma Export (C, u00051, "system__string_hashB");
25420 u00052 : constant Version_32 := 16#3bbb9c15#;
25421 pragma Export (C, u00052, "system__string_hashS");
25422 u00053 : constant Version_32 := 16#807fe041#;
25423 pragma Export (C, u00053, "system__unsigned_typesS");
25424 u00054 : constant Version_32 := 16#d27be59e#;
25425 pragma Export (C, u00054, "system__val_lluB");
25426 u00055 : constant Version_32 := 16#fa8db733#;
25427 pragma Export (C, u00055, "system__val_lluS");
25428 u00056 : constant Version_32 := 16#27b600b2#;
25429 pragma Export (C, u00056, "system__val_utilB");
25430 u00057 : constant Version_32 := 16#b187f27f#;
25431 pragma Export (C, u00057, "system__val_utilS");
25432 u00058 : constant Version_32 := 16#d1060688#;
25433 pragma Export (C, u00058, "system__case_utilB");
25434 u00059 : constant Version_32 := 16#392e2d56#;
25435 pragma Export (C, u00059, "system__case_utilS");
25436 u00060 : constant Version_32 := 16#84a27f0d#;
25437 pragma Export (C, u00060, "interfaces__c_streamsB");
25438 u00061 : constant Version_32 := 16#8bb5f2c0#;
25439 pragma Export (C, u00061, "interfaces__c_streamsS");
25440 u00062 : constant Version_32 := 16#6db6928f#;
25441 pragma Export (C, u00062, "system__crtlS");
25442 u00063 : constant Version_32 := 16#4e6a342b#;
25443 pragma Export (C, u00063, "system__file_ioB");
25444 u00064 : constant Version_32 := 16#ba56a5e4#;
25445 pragma Export (C, u00064, "system__file_ioS");
25446 u00065 : constant Version_32 := 16#b7ab275c#;
25447 pragma Export (C, u00065, "ada__finalizationB");
25448 u00066 : constant Version_32 := 16#19f764ca#;
25449 pragma Export (C, u00066, "ada__finalizationS");
25450 u00067 : constant Version_32 := 16#95817ed8#;
25451 pragma Export (C, u00067, "system__finalization_rootB");
25452 u00068 : constant Version_32 := 16#52d53711#;
25453 pragma Export (C, u00068, "system__finalization_rootS");
25454 u00069 : constant Version_32 := 16#769e25e6#;
25455 pragma Export (C, u00069, "interfaces__cB");
25456 u00070 : constant Version_32 := 16#4a38bedb#;
25457 pragma Export (C, u00070, "interfaces__cS");
25458 u00071 : constant Version_32 := 16#07e6ee66#;
25459 pragma Export (C, u00071, "system__os_libB");
25460 u00072 : constant Version_32 := 16#d7b69782#;
25461 pragma Export (C, u00072, "system__os_libS");
25462 u00073 : constant Version_32 := 16#1a817b8e#;
25463 pragma Export (C, u00073, "system__stringsB");
25464 u00074 : constant Version_32 := 16#639855e7#;
25465 pragma Export (C, u00074, "system__stringsS");
25466 u00075 : constant Version_32 := 16#e0b8de29#;
25467 pragma Export (C, u00075, "system__file_control_blockS");
25468 u00076 : constant Version_32 := 16#b5b2aca1#;
25469 pragma Export (C, u00076, "system__finalization_mastersB");
25470 u00077 : constant Version_32 := 16#69316dc1#;
25471 pragma Export (C, u00077, "system__finalization_mastersS");
25472 u00078 : constant Version_32 := 16#57a37a42#;
25473 pragma Export (C, u00078, "system__address_imageB");
25474 u00079 : constant Version_32 := 16#bccbd9bb#;
25475 pragma Export (C, u00079, "system__address_imageS");
25476 u00080 : constant Version_32 := 16#7268f812#;
25477 pragma Export (C, u00080, "system__img_boolB");
25478 u00081 : constant Version_32 := 16#e8fe356a#;
25479 pragma Export (C, u00081, "system__img_boolS");
25480 u00082 : constant Version_32 := 16#d7aac20c#;
25481 pragma Export (C, u00082, "system__ioB");
25482 u00083 : constant Version_32 := 16#8365b3ce#;
25483 pragma Export (C, u00083, "system__ioS");
25484 u00084 : constant Version_32 := 16#6d4d969a#;
25485 pragma Export (C, u00084, "system__storage_poolsB");
25486 u00085 : constant Version_32 := 16#e87cc305#;
25487 pragma Export (C, u00085, "system__storage_poolsS");
25488 u00086 : constant Version_32 := 16#e34550ca#;
25489 pragma Export (C, u00086, "system__pool_globalB");
25490 u00087 : constant Version_32 := 16#c88d2d16#;
25491 pragma Export (C, u00087, "system__pool_globalS");
25492 u00088 : constant Version_32 := 16#9d39c675#;
25493 pragma Export (C, u00088, "system__memoryB");
25494 u00089 : constant Version_32 := 16#445a22b5#;
25495 pragma Export (C, u00089, "system__memoryS");
25496 u00090 : constant Version_32 := 16#6a859064#;
25497 pragma Export (C, u00090, "system__storage_pools__subpoolsB");
25498 u00091 : constant Version_32 := 16#e3b008dc#;
25499 pragma Export (C, u00091, "system__storage_pools__subpoolsS");
25500 u00092 : constant Version_32 := 16#63f11652#;
25501 pragma Export (C, u00092, "system__storage_pools__subpools__finalizationB");
25502 u00093 : constant Version_32 := 16#fe2f4b3a#;
25503 pragma Export (C, u00093, "system__storage_pools__subpools__finalizationS");
25504
25505 -- BEGIN ELABORATION ORDER
25506 -- ada%s
25507 -- interfaces%s
25508 -- system%s
25509 -- system.case_util%s
25510 -- system.case_util%b
25511 -- system.htable%s
25512 -- system.img_bool%s
25513 -- system.img_bool%b
25514 -- system.img_int%s
25515 -- system.img_int%b
25516 -- system.io%s
25517 -- system.io%b
25518 -- system.parameters%s
25519 -- system.parameters%b
25520 -- system.crtl%s
25521 -- interfaces.c_streams%s
25522 -- interfaces.c_streams%b
25523 -- system.standard_library%s
25524 -- system.exceptions_debug%s
25525 -- system.exceptions_debug%b
25526 -- system.storage_elements%s
25527 -- system.storage_elements%b
25528 -- system.stack_checking%s
25529 -- system.stack_checking%b
25530 -- system.string_hash%s
25531 -- system.string_hash%b
25532 -- system.htable%b
25533 -- system.strings%s
25534 -- system.strings%b
25535 -- system.os_lib%s
25536 -- system.traceback_entries%s
25537 -- system.traceback_entries%b
25538 -- ada.exceptions%s
25539 -- system.soft_links%s
25540 -- system.unsigned_types%s
25541 -- system.val_llu%s
25542 -- system.val_util%s
25543 -- system.val_util%b
25544 -- system.val_llu%b
25545 -- system.wch_con%s
25546 -- system.wch_con%b
25547 -- system.wch_cnv%s
25548 -- system.wch_jis%s
25549 -- system.wch_jis%b
25550 -- system.wch_cnv%b
25551 -- system.wch_stw%s
25552 -- system.wch_stw%b
25553 -- ada.exceptions.last_chance_handler%s
25554 -- ada.exceptions.last_chance_handler%b
25555 -- system.address_image%s
25556 -- system.exception_table%s
25557 -- system.exception_table%b
25558 -- ada.io_exceptions%s
25559 -- ada.tags%s
25560 -- ada.streams%s
25561 -- ada.streams%b
25562 -- interfaces.c%s
25563 -- system.exceptions%s
25564 -- system.exceptions%b
25565 -- system.exceptions.machine%s
25566 -- system.finalization_root%s
25567 -- system.finalization_root%b
25568 -- ada.finalization%s
25569 -- ada.finalization%b
25570 -- system.storage_pools%s
25571 -- system.storage_pools%b
25572 -- system.finalization_masters%s
25573 -- system.storage_pools.subpools%s
25574 -- system.storage_pools.subpools.finalization%s
25575 -- system.storage_pools.subpools.finalization%b
25576 -- system.memory%s
25577 -- system.memory%b
25578 -- system.standard_library%b
25579 -- system.pool_global%s
25580 -- system.pool_global%b
25581 -- system.file_control_block%s
25582 -- system.file_io%s
25583 -- system.secondary_stack%s
25584 -- system.file_io%b
25585 -- system.storage_pools.subpools%b
25586 -- system.finalization_masters%b
25587 -- interfaces.c%b
25588 -- ada.tags%b
25589 -- system.soft_links%b
25590 -- system.os_lib%b
25591 -- system.secondary_stack%b
25592 -- system.address_image%b
25593 -- system.traceback%s
25594 -- ada.exceptions%b
25595 -- system.traceback%b
25596 -- ada.text_io%s
25597 -- ada.text_io%b
25598 -- hello%b
25599 -- END ELABORATION ORDER
25600
25601 end ada_main;
25602 @end example
25603
25604 @example
25605 pragma Ada_95;
25606 -- The following source file name pragmas allow the generated file
25607 -- names to be unique for different main programs. They are needed
25608 -- since the package name will always be Ada_Main.
25609
25610 pragma Source_File_Name (ada_main, Spec_File_Name => "b~hello.ads");
25611 pragma Source_File_Name (ada_main, Body_File_Name => "b~hello.adb");
25612
25613 pragma Suppress (Overflow_Check);
25614 with Ada.Exceptions;
25615
25616 -- Generated package body for Ada_Main starts here
25617
25618 package body ada_main is
25619 pragma Warnings (Off);
25620
25621 -- These values are reference counter associated to units which have
25622 -- been elaborated. It is also used to avoid elaborating the
25623 -- same unit twice.
25624
25625 E72 : Short_Integer; pragma Import (Ada, E72, "system__os_lib_E");
25626 E13 : Short_Integer; pragma Import (Ada, E13, "system__soft_links_E");
25627 E23 : Short_Integer; pragma Import (Ada, E23, "system__exception_table_E");
25628 E46 : Short_Integer; pragma Import (Ada, E46, "ada__io_exceptions_E");
25629 E48 : Short_Integer; pragma Import (Ada, E48, "ada__tags_E");
25630 E45 : Short_Integer; pragma Import (Ada, E45, "ada__streams_E");
25631 E70 : Short_Integer; pragma Import (Ada, E70, "interfaces__c_E");
25632 E25 : Short_Integer; pragma Import (Ada, E25, "system__exceptions_E");
25633 E68 : Short_Integer; pragma Import (Ada, E68, "system__finalization_root_E");
25634 E66 : Short_Integer; pragma Import (Ada, E66, "ada__finalization_E");
25635 E85 : Short_Integer; pragma Import (Ada, E85, "system__storage_pools_E");
25636 E77 : Short_Integer; pragma Import (Ada, E77, "system__finalization_masters_E");
25637 E91 : Short_Integer; pragma Import (Ada, E91, "system__storage_pools__subpools_E");
25638 E87 : Short_Integer; pragma Import (Ada, E87, "system__pool_global_E");
25639 E75 : Short_Integer; pragma Import (Ada, E75, "system__file_control_block_E");
25640 E64 : Short_Integer; pragma Import (Ada, E64, "system__file_io_E");
25641 E17 : Short_Integer; pragma Import (Ada, E17, "system__secondary_stack_E");
25642 E06 : Short_Integer; pragma Import (Ada, E06, "ada__text_io_E");
25643
25644 Local_Priority_Specific_Dispatching : constant String := "";
25645 Local_Interrupt_States : constant String := "";
25646
25647 Is_Elaborated : Boolean := False;
25648
25649 procedure finalize_library is
25650 begin
25651 E06 := E06 - 1;
25652 declare
25653 procedure F1;
25654 pragma Import (Ada, F1, "ada__text_io__finalize_spec");
25655 begin
25656 F1;
25657 end;
25658 E77 := E77 - 1;
25659 E91 := E91 - 1;
25660 declare
25661 procedure F2;
25662 pragma Import (Ada, F2, "system__file_io__finalize_body");
25663 begin
25664 E64 := E64 - 1;
25665 F2;
25666 end;
25667 declare
25668 procedure F3;
25669 pragma Import (Ada, F3, "system__file_control_block__finalize_spec");
25670 begin
25671 E75 := E75 - 1;
25672 F3;
25673 end;
25674 E87 := E87 - 1;
25675 declare
25676 procedure F4;
25677 pragma Import (Ada, F4, "system__pool_global__finalize_spec");
25678 begin
25679 F4;
25680 end;
25681 declare
25682 procedure F5;
25683 pragma Import (Ada, F5, "system__storage_pools__subpools__finalize_spec");
25684 begin
25685 F5;
25686 end;
25687 declare
25688 procedure F6;
25689 pragma Import (Ada, F6, "system__finalization_masters__finalize_spec");
25690 begin
25691 F6;
25692 end;
25693 declare
25694 procedure Reraise_Library_Exception_If_Any;
25695 pragma Import (Ada, Reraise_Library_Exception_If_Any, "__gnat_reraise_library_exception_if_any");
25696 begin
25697 Reraise_Library_Exception_If_Any;
25698 end;
25699 end finalize_library;
25700
25701 -------------
25702 -- adainit --
25703 -------------
25704
25705 procedure adainit is
25706
25707 Main_Priority : Integer;
25708 pragma Import (C, Main_Priority, "__gl_main_priority");
25709 Time_Slice_Value : Integer;
25710 pragma Import (C, Time_Slice_Value, "__gl_time_slice_val");
25711 WC_Encoding : Character;
25712 pragma Import (C, WC_Encoding, "__gl_wc_encoding");
25713 Locking_Policy : Character;
25714 pragma Import (C, Locking_Policy, "__gl_locking_policy");
25715 Queuing_Policy : Character;
25716 pragma Import (C, Queuing_Policy, "__gl_queuing_policy");
25717 Task_Dispatching_Policy : Character;
25718 pragma Import (C, Task_Dispatching_Policy, "__gl_task_dispatching_policy");
25719 Priority_Specific_Dispatching : System.Address;
25720 pragma Import (C, Priority_Specific_Dispatching, "__gl_priority_specific_dispatching");
25721 Num_Specific_Dispatching : Integer;
25722 pragma Import (C, Num_Specific_Dispatching, "__gl_num_specific_dispatching");
25723 Main_CPU : Integer;
25724 pragma Import (C, Main_CPU, "__gl_main_cpu");
25725 Interrupt_States : System.Address;
25726 pragma Import (C, Interrupt_States, "__gl_interrupt_states");
25727 Num_Interrupt_States : Integer;
25728 pragma Import (C, Num_Interrupt_States, "__gl_num_interrupt_states");
25729 Unreserve_All_Interrupts : Integer;
25730 pragma Import (C, Unreserve_All_Interrupts, "__gl_unreserve_all_interrupts");
25731 Detect_Blocking : Integer;
25732 pragma Import (C, Detect_Blocking, "__gl_detect_blocking");
25733 Default_Stack_Size : Integer;
25734 pragma Import (C, Default_Stack_Size, "__gl_default_stack_size");
25735 Leap_Seconds_Support : Integer;
25736 pragma Import (C, Leap_Seconds_Support, "__gl_leap_seconds_support");
25737
25738 procedure Runtime_Initialize;
25739 pragma Import (C, Runtime_Initialize, "__gnat_runtime_initialize");
25740
25741 Finalize_Library_Objects : No_Param_Proc;
25742 pragma Import (C, Finalize_Library_Objects, "__gnat_finalize_library_objects");
25743
25744 -- Start of processing for adainit
25745
25746 begin
25747
25748 -- Record various information for this partition. The values
25749 -- are derived by the binder from information stored in the ali
25750 -- files by the compiler.
25751
25752 if Is_Elaborated then
25753 return;
25754 end if;
25755 Is_Elaborated := True;
25756 Main_Priority := -1;
25757 Time_Slice_Value := -1;
25758 WC_Encoding := 'b';
25759 Locking_Policy := ' ';
25760 Queuing_Policy := ' ';
25761 Task_Dispatching_Policy := ' ';
25762 Priority_Specific_Dispatching :=
25763 Local_Priority_Specific_Dispatching'Address;
25764 Num_Specific_Dispatching := 0;
25765 Main_CPU := -1;
25766 Interrupt_States := Local_Interrupt_States'Address;
25767 Num_Interrupt_States := 0;
25768 Unreserve_All_Interrupts := 0;
25769 Detect_Blocking := 0;
25770 Default_Stack_Size := -1;
25771 Leap_Seconds_Support := 0;
25772
25773 Runtime_Initialize;
25774
25775 Finalize_Library_Objects := finalize_library'access;
25776
25777 -- Now we have the elaboration calls for all units in the partition.
25778 -- The Elab_Spec and Elab_Body attributes generate references to the
25779 -- implicit elaboration procedures generated by the compiler for
25780 -- each unit that requires elaboration. Increment a counter of
25781 -- reference for each unit.
25782
25783 System.Soft_Links'Elab_Spec;
25784 System.Exception_Table'Elab_Body;
25785 E23 := E23 + 1;
25786 Ada.Io_Exceptions'Elab_Spec;
25787 E46 := E46 + 1;
25788 Ada.Tags'Elab_Spec;
25789 Ada.Streams'Elab_Spec;
25790 E45 := E45 + 1;
25791 Interfaces.C'Elab_Spec;
25792 System.Exceptions'Elab_Spec;
25793 E25 := E25 + 1;
25794 System.Finalization_Root'Elab_Spec;
25795 E68 := E68 + 1;
25796 Ada.Finalization'Elab_Spec;
25797 E66 := E66 + 1;
25798 System.Storage_Pools'Elab_Spec;
25799 E85 := E85 + 1;
25800 System.Finalization_Masters'Elab_Spec;
25801 System.Storage_Pools.Subpools'Elab_Spec;
25802 System.Pool_Global'Elab_Spec;
25803 E87 := E87 + 1;
25804 System.File_Control_Block'Elab_Spec;
25805 E75 := E75 + 1;
25806 System.File_Io'Elab_Body;
25807 E64 := E64 + 1;
25808 E91 := E91 + 1;
25809 System.Finalization_Masters'Elab_Body;
25810 E77 := E77 + 1;
25811 E70 := E70 + 1;
25812 Ada.Tags'Elab_Body;
25813 E48 := E48 + 1;
25814 System.Soft_Links'Elab_Body;
25815 E13 := E13 + 1;
25816 System.Os_Lib'Elab_Body;
25817 E72 := E72 + 1;
25818 System.Secondary_Stack'Elab_Body;
25819 E17 := E17 + 1;
25820 Ada.Text_Io'Elab_Spec;
25821 Ada.Text_Io'Elab_Body;
25822 E06 := E06 + 1;
25823 end adainit;
25824
25825 --------------
25826 -- adafinal --
25827 --------------
25828
25829 procedure adafinal is
25830 procedure s_stalib_adafinal;
25831 pragma Import (C, s_stalib_adafinal, "system__standard_library__adafinal");
25832
25833 procedure Runtime_Finalize;
25834 pragma Import (C, Runtime_Finalize, "__gnat_runtime_finalize");
25835
25836 begin
25837 if not Is_Elaborated then
25838 return;
25839 end if;
25840 Is_Elaborated := False;
25841 Runtime_Finalize;
25842 s_stalib_adafinal;
25843 end adafinal;
25844
25845 -- We get to the main program of the partition by using
25846 -- pragma Import because if we try to with the unit and
25847 -- call it Ada style, then not only do we waste time
25848 -- recompiling it, but also, we don't really know the right
25849 -- switches (e.g.@@: identifier character set) to be used
25850 -- to compile it.
25851
25852 procedure Ada_Main_Program;
25853 pragma Import (Ada, Ada_Main_Program, "_ada_hello");
25854
25855 ----------
25856 -- main --
25857 ----------
25858
25859 -- main is actually a function, as in the ANSI C standard,
25860 -- defined to return the exit status. The three parameters
25861 -- are the argument count, argument values and environment
25862 -- pointer.
25863
25864 function main
25865 (argc : Integer;
25866 argv : System.Address;
25867 envp : System.Address)
25868 return Integer
25869 is
25870 -- The initialize routine performs low level system
25871 -- initialization using a standard library routine which
25872 -- sets up signal handling and performs any other
25873 -- required setup. The routine can be found in file
25874 -- a-init.c.
25875
25876 procedure initialize;
25877 pragma Import (C, initialize, "__gnat_initialize");
25878
25879 -- The finalize routine performs low level system
25880 -- finalization using a standard library routine. The
25881 -- routine is found in file a-final.c and in the standard
25882 -- distribution is a dummy routine that does nothing, so
25883 -- really this is a hook for special user finalization.
25884
25885 procedure finalize;
25886 pragma Import (C, finalize, "__gnat_finalize");
25887
25888 -- The following is to initialize the SEH exceptions
25889
25890 SEH : aliased array (1 .. 2) of Integer;
25891
25892 Ensure_Reference : aliased System.Address := Ada_Main_Program_Name'Address;
25893 pragma Volatile (Ensure_Reference);
25894
25895 -- Start of processing for main
25896
25897 begin
25898 -- Save global variables
25899
25900 gnat_argc := argc;
25901 gnat_argv := argv;
25902 gnat_envp := envp;
25903
25904 -- Call low level system initialization
25905
25906 Initialize (SEH'Address);
25907
25908 -- Call our generated Ada initialization routine
25909
25910 adainit;
25911
25912 -- Now we call the main program of the partition
25913
25914 Ada_Main_Program;
25915
25916 -- Perform Ada finalization
25917
25918 adafinal;
25919
25920 -- Perform low level system finalization
25921
25922 Finalize;
25923
25924 -- Return the proper exit status
25925 return (gnat_exit_status);
25926 end;
25927
25928 -- This section is entirely comments, so it has no effect on the
25929 -- compilation of the Ada_Main package. It provides the list of
25930 -- object files and linker options, as well as some standard
25931 -- libraries needed for the link. The gnatlink utility parses
25932 -- this b~hello.adb file to read these comment lines to generate
25933 -- the appropriate command line arguments for the call to the
25934 -- system linker. The BEGIN/END lines are used for sentinels for
25935 -- this parsing operation.
25936
25937 -- The exact file names will of course depend on the environment,
25938 -- host/target and location of files on the host system.
25939
25940 -- BEGIN Object file/option list
25941 -- ./hello.o
25942 -- -L./
25943 -- -L/usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/
25944 -- /usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a
25945 -- END Object file/option list
25946
25947 end ada_main;
25948 @end example
25949
25950 The Ada code in the above example is exactly what is generated by the
25951 binder. We have added comments to more clearly indicate the function
25952 of each part of the generated @code{Ada_Main} package.
25953
25954 The code is standard Ada in all respects, and can be processed by any
25955 tools that handle Ada. In particular, it is possible to use the debugger
25956 in Ada mode to debug the generated @code{Ada_Main} package. For example,
25957 suppose that for reasons that you do not understand, your program is crashing
25958 during elaboration of the body of @code{Ada.Text_IO}. To locate this bug,
25959 you can place a breakpoint on the call:
25960
25961 @quotation
25962
25963 @example
25964 Ada.Text_Io'Elab_Body;
25965 @end example
25966 @end quotation
25967
25968 and trace the elaboration routine for this package to find out where
25969 the problem might be (more usually of course you would be debugging
25970 elaboration code in your own application).
25971
25972 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
25973
25974 @node Elaboration Order Handling in GNAT,Inline Assembler,Example of Binder Output File,Top
25975 @anchor{gnat_ugn/elaboration_order_handling_in_gnat doc}@anchor{214}@anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-order-handling-in-gnat}@anchor{f}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id1}@anchor{215}
25976 @chapter Elaboration Order Handling in GNAT
25977
25978
25979 @geindex Order of elaboration
25980
25981 @geindex Elaboration control
25982
25983 This appendix describes the handling of elaboration code in Ada and GNAT, and
25984 discusses how the order of elaboration of program units can be controlled in
25985 GNAT, either automatically or with explicit programming features.
25986
25987 @menu
25988 * Elaboration Code::
25989 * Elaboration Order::
25990 * Checking the Elaboration Order::
25991 * Controlling the Elaboration Order in Ada::
25992 * Controlling the Elaboration Order in GNAT::
25993 * Mixing Elaboration Models::
25994 * ABE Diagnostics::
25995 * SPARK Diagnostics::
25996 * Elaboration Circularities::
25997 * Resolving Elaboration Circularities::
25998 * Elaboration-related Compiler Switches::
25999 * Summary of Procedures for Elaboration Control::
26000 * Inspecting the Chosen Elaboration Order::
26001
26002 @end menu
26003
26004 @node Elaboration Code,Elaboration Order,,Elaboration Order Handling in GNAT
26005 @anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-code}@anchor{216}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id2}@anchor{217}
26006 @section Elaboration Code
26007
26008
26009 Ada defines the term @emph{execution} as the process by which a construct achieves
26010 its run-time effect. This process is also referred to as @strong{elaboration} for
26011 declarations and @emph{evaluation} for expressions.
26012
26013 The execution model in Ada allows for certain sections of an Ada program to be
26014 executed prior to execution of the program itself, primarily with the intent of
26015 initializing data. These sections are referred to as @strong{elaboration code}.
26016 Elaboration code is executed as follows:
26017
26018
26019 @itemize *
26020
26021 @item
26022 All partitions of an Ada program are executed in parallel with one another,
26023 possibly in a separate address space, and possibly on a separate computer.
26024
26025 @item
26026 The execution of a partition involves running the environment task for that
26027 partition.
26028
26029 @item
26030 The environment task executes all elaboration code (if available) for all
26031 units within that partition. This code is said to be executed at
26032 @strong{elaboration time}.
26033
26034 @item
26035 The environment task executes the Ada program (if available) for that
26036 partition.
26037 @end itemize
26038
26039 In addition to the Ada terminology, this appendix defines the following terms:
26040
26041
26042 @itemize *
26043
26044 @item
26045 @emph{Invocation}
26046
26047 The act of calling a subprogram, instantiating a generic, or activating a
26048 task.
26049
26050 @item
26051 @emph{Scenario}
26052
26053 A construct that is elaborated or invoked by elaboration code is referred to
26054 as an @emph{elaboration scenario} or simply a @strong{scenario}. GNAT recognizes the
26055 following scenarios:
26056
26057
26058 @itemize -
26059
26060 @item
26061 @code{'Access} of entries, operators, and subprograms
26062
26063 @item
26064 Activation of tasks
26065
26066 @item
26067 Calls to entries, operators, and subprograms
26068
26069 @item
26070 Instantiations of generic templates
26071 @end itemize
26072
26073 @item
26074 @emph{Target}
26075
26076 A construct elaborated by a scenario is referred to as @emph{elaboration target}
26077 or simply @strong{target}. GNAT recognizes the following targets:
26078
26079
26080 @itemize -
26081
26082 @item
26083 For @code{'Access} of entries, operators, and subprograms, the target is the
26084 entry, operator, or subprogram being aliased.
26085
26086 @item
26087 For activation of tasks, the target is the task body
26088
26089 @item
26090 For calls to entries, operators, and subprograms, the target is the entry,
26091 operator, or subprogram being invoked.
26092
26093 @item
26094 For instantiations of generic templates, the target is the generic template
26095 being instantiated.
26096 @end itemize
26097 @end itemize
26098
26099 Elaboration code may appear in two distinct contexts:
26100
26101
26102 @itemize *
26103
26104 @item
26105 @emph{Library level}
26106
26107 A scenario appears at the library level when it is encapsulated by a package
26108 [body] compilation unit, ignoring any other package [body] declarations in
26109 between.
26110
26111 @example
26112 with Server;
26113 package Client is
26114 procedure Proc;
26115
26116 package Nested is
26117 Val : ... := Server.Func;
26118 end Nested;
26119 end Client;
26120 @end example
26121
26122 In the example above, the call to @code{Server.Func} is an elaboration scenario
26123 because it appears at the library level of package @code{Client}. Note that the
26124 declaration of package @code{Nested} is ignored according to the definition
26125 given above. As a result, the call to @code{Server.Func} will be invoked when
26126 the spec of unit @code{Client} is elaborated.
26127
26128 @item
26129 @emph{Package body statements}
26130
26131 A scenario appears within the statement sequence of a package body when it is
26132 bounded by the region starting from the @code{begin} keyword of the package body
26133 and ending at the @code{end} keyword of the package body.
26134
26135 @example
26136 package body Client is
26137 procedure Proc is
26138 begin
26139 ...
26140 end Proc;
26141 begin
26142 Proc;
26143 end Client;
26144 @end example
26145
26146 In the example above, the call to @code{Proc} is an elaboration scenario because
26147 it appears within the statement sequence of package body @code{Client}. As a
26148 result, the call to @code{Proc} will be invoked when the body of @code{Client} is
26149 elaborated.
26150 @end itemize
26151
26152 @node Elaboration Order,Checking the Elaboration Order,Elaboration Code,Elaboration Order Handling in GNAT
26153 @anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-order}@anchor{218}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id3}@anchor{219}
26154 @section Elaboration Order
26155
26156
26157 The sequence by which the elaboration code of all units within a partition is
26158 executed is referred to as @strong{elaboration order}.
26159
26160 Within a single unit, elaboration code is executed in sequential order.
26161
26162 @quotation
26163
26164 @example
26165 package body Client is
26166 Result : ... := Server.Func;
26167
26168 procedure Proc is
26169 package Inst is new Server.Gen;
26170 begin
26171 Inst.Eval (Result);
26172 end Proc;
26173 begin
26174 Proc;
26175 end Client;
26176 @end example
26177 @end quotation
26178
26179 In the example above, the elaboration order within package body @code{Client} is
26180 as follows:
26181
26182
26183 @enumerate
26184
26185 @item
26186 The object declaration of @code{Result} is elaborated.
26187
26188
26189 @itemize *
26190
26191 @item
26192 Function @code{Server.Func} is invoked.
26193 @end itemize
26194
26195 @item
26196 The subprogram body of @code{Proc} is elaborated.
26197
26198 @item
26199 Procedure @code{Proc} is invoked.
26200
26201
26202 @itemize *
26203
26204 @item
26205 Generic unit @code{Server.Gen} is instantiated as @code{Inst}.
26206
26207 @item
26208 Instance @code{Inst} is elaborated.
26209
26210 @item
26211 Procedure @code{Inst.Eval} is invoked.
26212 @end itemize
26213 @end enumerate
26214
26215 The elaboration order of all units within a partition depends on the following
26216 factors:
26217
26218
26219 @itemize *
26220
26221 @item
26222 @emph{with}ed units
26223
26224 @item
26225 parent units
26226
26227 @item
26228 purity of units
26229
26230 @item
26231 preelaborability of units
26232
26233 @item
26234 presence of elaboration-control pragmas
26235
26236 @item
26237 invocations performed in elaboration code
26238 @end itemize
26239
26240 A program may have several elaboration orders depending on its structure.
26241
26242 @quotation
26243
26244 @example
26245 package Server is
26246 function Func (Index : Integer) return Integer;
26247 end Server;
26248 @end example
26249
26250 @example
26251 package body Server is
26252 Results : array (1 .. 5) of Integer := (1, 2, 3, 4, 5);
26253
26254 function Func (Index : Integer) return Integer is
26255 begin
26256 return Results (Index);
26257 end Func;
26258 end Server;
26259 @end example
26260
26261 @example
26262 with Server;
26263 package Client is
26264 Val : constant Integer := Server.Func (3);
26265 end Client;
26266 @end example
26267
26268 @example
26269 with Client;
26270 procedure Main is begin null; end Main;
26271 @end example
26272 @end quotation
26273
26274 The following elaboration order exhibits a fundamental problem referred to as
26275 @emph{access-before-elaboration} or simply @strong{ABE}.
26276
26277 @quotation
26278
26279 @example
26280 spec of Server
26281 spec of Client
26282 body of Server
26283 body of Main
26284 @end example
26285 @end quotation
26286
26287 The elaboration of @code{Server}’s spec materializes function @code{Func}, making it
26288 callable. The elaboration of @code{Client}’s spec elaborates the declaration of
26289 @code{Val}. This invokes function @code{Server.Func}, however the body of
26290 @code{Server.Func} has not been elaborated yet because @code{Server}’s body comes
26291 after @code{Client}’s spec in the elaboration order. As a result, the value of
26292 constant @code{Val} is now undefined.
26293
26294 Without any guarantees from the language, an undetected ABE problem may hinder
26295 proper initialization of data, which in turn may lead to undefined behavior at
26296 run time. To prevent such ABE problems, Ada employs dynamic checks in the same
26297 vein as index or null exclusion checks. A failed ABE check raises exception
26298 @code{Program_Error}.
26299
26300 The following elaboration order avoids the ABE problem and the program can be
26301 successfully elaborated.
26302
26303 @quotation
26304
26305 @example
26306 spec of Server
26307 body of Server
26308 spec of Client
26309 body of Main
26310 @end example
26311 @end quotation
26312
26313 Ada states that a total elaboration order must exist, but it does not define
26314 what this order is. A compiler is thus tasked with choosing a suitable
26315 elaboration order which satisfies the dependencies imposed by @emph{with} clauses,
26316 unit categorization, elaboration-control pragmas, and invocations performed in
26317 elaboration code. Ideally an order that avoids ABE problems should be chosen,
26318 however a compiler may not always find such an order due to complications with
26319 respect to control and data flow.
26320
26321 @node Checking the Elaboration Order,Controlling the Elaboration Order in Ada,Elaboration Order,Elaboration Order Handling in GNAT
26322 @anchor{gnat_ugn/elaboration_order_handling_in_gnat checking-the-elaboration-order}@anchor{21a}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id4}@anchor{21b}
26323 @section Checking the Elaboration Order
26324
26325
26326 To avoid placing the entire elaboration-order burden on the programmer, Ada
26327 provides three lines of defense:
26328
26329
26330 @itemize *
26331
26332 @item
26333 @emph{Static semantics}
26334
26335 Static semantic rules restrict the possible choice of elaboration order. For
26336 instance, if unit Client @emph{with}s unit Server, then the spec of Server is
26337 always elaborated prior to Client. The same principle applies to child units
26338 - the spec of a parent unit is always elaborated prior to the child unit.
26339
26340 @item
26341 @emph{Dynamic semantics}
26342
26343 Dynamic checks are performed at run time, to ensure that a target is
26344 elaborated prior to a scenario that invokes it, thus avoiding ABE problems.
26345 A failed run-time check raises exception @code{Program_Error}. The following
26346 restrictions apply:
26347
26348
26349 @itemize -
26350
26351 @item
26352 @emph{Restrictions on calls}
26353
26354 An entry, operator, or subprogram can be called from elaboration code only
26355 when the corresponding body has been elaborated.
26356
26357 @item
26358 @emph{Restrictions on instantiations}
26359
26360 A generic unit can be instantiated by elaboration code only when the
26361 corresponding body has been elaborated.
26362
26363 @item
26364 @emph{Restrictions on task activation}
26365
26366 A task can be activated by elaboration code only when the body of the
26367 associated task type has been elaborated.
26368 @end itemize
26369
26370 The restrictions above can be summarized by the following rule:
26371
26372 @emph{If a target has a body, then this body must be elaborated prior to the
26373 scenario that invokes the target.}
26374
26375 @item
26376 @emph{Elaboration control}
26377
26378 Pragmas are provided for the programmer to specify the desired elaboration
26379 order.
26380 @end itemize
26381
26382 @node Controlling the Elaboration Order in Ada,Controlling the Elaboration Order in GNAT,Checking the Elaboration Order,Elaboration Order Handling in GNAT
26383 @anchor{gnat_ugn/elaboration_order_handling_in_gnat controlling-the-elaboration-order-in-ada}@anchor{21c}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id5}@anchor{21d}
26384 @section Controlling the Elaboration Order in Ada
26385
26386
26387 Ada provides several idioms and pragmas to aid the programmer with specifying
26388 the desired elaboration order and avoiding ABE problems altogether.
26389
26390
26391 @itemize *
26392
26393 @item
26394 @emph{Packages without a body}
26395
26396 A library package which does not require a completing body does not suffer
26397 from ABE problems.
26398
26399 @example
26400 package Pack is
26401 generic
26402 type Element is private;
26403 package Containers is
26404 type Element_Array is array (1 .. 10) of Element;
26405 end Containers;
26406 end Pack;
26407 @end example
26408
26409 In the example above, package @code{Pack} does not require a body because it
26410 does not contain any constructs which require completion in a body. As a
26411 result, generic @code{Pack.Containers} can be instantiated without encountering
26412 any ABE problems.
26413 @end itemize
26414
26415 @geindex pragma Pure
26416
26417
26418 @itemize *
26419
26420 @item
26421 @emph{pragma Pure}
26422
26423 Pragma @code{Pure} places sufficient restrictions on a unit to guarantee that no
26424 scenario within the unit can result in an ABE problem.
26425 @end itemize
26426
26427 @geindex pragma Preelaborate
26428
26429
26430 @itemize *
26431
26432 @item
26433 @emph{pragma Preelaborate}
26434
26435 Pragma @code{Preelaborate} is slightly less restrictive than pragma @code{Pure},
26436 but still strong enough to prevent ABE problems within a unit.
26437 @end itemize
26438
26439 @geindex pragma Elaborate_Body
26440
26441
26442 @itemize *
26443
26444 @item
26445 @emph{pragma Elaborate_Body}
26446
26447 Pragma @code{Elaborate_Body} requires that the body of a unit is elaborated
26448 immediately after its spec. This restriction guarantees that no client
26449 scenario can invoke a server target before the target body has been
26450 elaborated because the spec and body are effectively “glued” together.
26451
26452 @example
26453 package Server is
26454 pragma Elaborate_Body;
26455
26456 function Func return Integer;
26457 end Server;
26458 @end example
26459
26460 @example
26461 package body Server is
26462 function Func return Integer is
26463 begin
26464 ...
26465 end Func;
26466 end Server;
26467 @end example
26468
26469 @example
26470 with Server;
26471 package Client is
26472 Val : constant Integer := Server.Func;
26473 end Client;
26474 @end example
26475
26476 In the example above, pragma @code{Elaborate_Body} guarantees the following
26477 elaboration order:
26478
26479 @example
26480 spec of Server
26481 body of Server
26482 spec of Client
26483 @end example
26484
26485 because the spec of @code{Server} must be elaborated prior to @code{Client} by
26486 virtue of the @emph{with} clause, and in addition the body of @code{Server} must be
26487 elaborated immediately after the spec of @code{Server}.
26488
26489 Removing pragma @code{Elaborate_Body} could result in the following incorrect
26490 elaboration order:
26491
26492 @example
26493 spec of Server
26494 spec of Client
26495 body of Server
26496 @end example
26497
26498 where @code{Client} invokes @code{Server.Func}, but the body of @code{Server.Func} has
26499 not been elaborated yet.
26500 @end itemize
26501
26502 The pragmas outlined above allow a server unit to guarantee safe elaboration
26503 use by client units. Thus it is a good rule to mark units as @code{Pure} or
26504 @code{Preelaborate}, and if this is not possible, mark them as @code{Elaborate_Body}.
26505
26506 There are however situations where @code{Pure}, @code{Preelaborate}, and
26507 @code{Elaborate_Body} are not applicable. Ada provides another set of pragmas for
26508 use by client units to help ensure the elaboration safety of server units they
26509 depend on.
26510
26511 @geindex pragma Elaborate (Unit)
26512
26513
26514 @itemize *
26515
26516 @item
26517 @emph{pragma Elaborate (Unit)}
26518
26519 Pragma @code{Elaborate} can be placed in the context clauses of a unit, after a
26520 @emph{with} clause. It guarantees that both the spec and body of its argument will
26521 be elaborated prior to the unit with the pragma. Note that other unrelated
26522 units may be elaborated in between the spec and the body.
26523
26524 @example
26525 package Server is
26526 function Func return Integer;
26527 end Server;
26528 @end example
26529
26530 @example
26531 package body Server is
26532 function Func return Integer is
26533 begin
26534 ...
26535 end Func;
26536 end Server;
26537 @end example
26538
26539 @example
26540 with Server;
26541 pragma Elaborate (Server);
26542 package Client is
26543 Val : constant Integer := Server.Func;
26544 end Client;
26545 @end example
26546
26547 In the example above, pragma @code{Elaborate} guarantees the following
26548 elaboration order:
26549
26550 @example
26551 spec of Server
26552 body of Server
26553 spec of Client
26554 @end example
26555
26556 Removing pragma @code{Elaborate} could result in the following incorrect
26557 elaboration order:
26558
26559 @example
26560 spec of Server
26561 spec of Client
26562 body of Server
26563 @end example
26564
26565 where @code{Client} invokes @code{Server.Func}, but the body of @code{Server.Func}
26566 has not been elaborated yet.
26567 @end itemize
26568
26569 @geindex pragma Elaborate_All (Unit)
26570
26571
26572 @itemize *
26573
26574 @item
26575 @emph{pragma Elaborate_All (Unit)}
26576
26577 Pragma @code{Elaborate_All} is placed in the context clauses of a unit, after
26578 a @emph{with} clause. It guarantees that both the spec and body of its argument
26579 will be elaborated prior to the unit with the pragma, as well as all units
26580 @emph{with}ed by the spec and body of the argument, recursively. Note that other
26581 unrelated units may be elaborated in between the spec and the body.
26582
26583 @example
26584 package Math is
26585 function Factorial (Val : Natural) return Natural;
26586 end Math;
26587 @end example
26588
26589 @example
26590 package body Math is
26591 function Factorial (Val : Natural) return Natural is
26592 begin
26593 ...;
26594 end Factorial;
26595 end Math;
26596 @end example
26597
26598 @example
26599 package Computer is
26600 type Operation_Kind is (None, Op_Factorial);
26601
26602 function Compute
26603 (Val : Natural;
26604 Op : Operation_Kind) return Natural;
26605 end Computer;
26606 @end example
26607
26608 @example
26609 with Math;
26610 package body Computer is
26611 function Compute
26612 (Val : Natural;
26613 Op : Operation_Kind) return Natural
26614 is
26615 if Op = Op_Factorial then
26616 return Math.Factorial (Val);
26617 end if;
26618
26619 return 0;
26620 end Compute;
26621 end Computer;
26622 @end example
26623
26624 @example
26625 with Computer;
26626 pragma Elaborate_All (Computer);
26627 package Client is
26628 Val : constant Natural :=
26629 Computer.Compute (123, Computer.Op_Factorial);
26630 end Client;
26631 @end example
26632
26633 In the example above, pragma @code{Elaborate_All} can result in the following
26634 elaboration order:
26635
26636 @example
26637 spec of Math
26638 body of Math
26639 spec of Computer
26640 body of Computer
26641 spec of Client
26642 @end example
26643
26644 Note that there are several allowable suborders for the specs and bodies of
26645 @code{Math} and @code{Computer}, but the point is that these specs and bodies will
26646 be elaborated prior to @code{Client}.
26647
26648 Removing pragma @code{Elaborate_All} could result in the following incorrect
26649 elaboration order:
26650
26651 @example
26652 spec of Math
26653 spec of Computer
26654 body of Computer
26655 spec of Client
26656 body of Math
26657 @end example
26658
26659 where @code{Client} invokes @code{Computer.Compute}, which in turn invokes
26660 @code{Math.Factorial}, but the body of @code{Math.Factorial} has not been
26661 elaborated yet.
26662 @end itemize
26663
26664 All pragmas shown above can be summarized by the following rule:
26665
26666 @emph{If a client unit elaborates a server target directly or indirectly, then if
26667 the server unit requires a body and does not have pragma Pure, Preelaborate,
26668 or Elaborate_Body, then the client unit should have pragma Elaborate or
26669 Elaborate_All for the server unit.}
26670
26671 If the rule outlined above is not followed, then a program may fall in one of
26672 the following states:
26673
26674
26675 @itemize *
26676
26677 @item
26678 @emph{No elaboration order exists}
26679
26680 In this case a compiler must diagnose the situation, and refuse to build an
26681 executable program.
26682
26683 @item
26684 @emph{One or more incorrect elaboration orders exist}
26685
26686 In this case a compiler can build an executable program, but
26687 @code{Program_Error} will be raised when the program is run.
26688
26689 @item
26690 @emph{Several elaboration orders exist, some correct, some incorrect}
26691
26692 In this case the programmer has not controlled the elaboration order. As a
26693 result, a compiler may or may not pick one of the correct orders, and the
26694 program may or may not raise @code{Program_Error} when it is run. This is the
26695 worst possible state because the program may fail on another compiler, or
26696 even another version of the same compiler.
26697
26698 @item
26699 @emph{One or more correct orders exist}
26700
26701 In this case a compiler can build an executable program, and the program is
26702 run successfully. This state may be guaranteed by following the outlined
26703 rules, or may be the result of good program architecture.
26704 @end itemize
26705
26706 Note that one additional advantage of using @code{Elaborate} and @code{Elaborate_All}
26707 is that the program continues to stay in the last state (one or more correct
26708 orders exist) even if maintenance changes the bodies of targets.
26709
26710 @node Controlling the Elaboration Order in GNAT,Mixing Elaboration Models,Controlling the Elaboration Order in Ada,Elaboration Order Handling in GNAT
26711 @anchor{gnat_ugn/elaboration_order_handling_in_gnat controlling-the-elaboration-order-in-gnat}@anchor{21e}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id6}@anchor{21f}
26712 @section Controlling the Elaboration Order in GNAT
26713
26714
26715 In addition to Ada semantics and rules synthesized from them, GNAT offers
26716 three elaboration models to aid the programmer with specifying the correct
26717 elaboration order and to diagnose elaboration problems.
26718
26719 @geindex Dynamic elaboration model
26720
26721
26722 @itemize *
26723
26724 @item
26725 @emph{Dynamic elaboration model}
26726
26727 This is the most permissive of the three elaboration models and emulates the
26728 behavior specified by the Ada Reference Manual. When the dynamic model is in
26729 effect, GNAT makes the following assumptions:
26730
26731
26732 @itemize -
26733
26734 @item
26735 All code within all units in a partition is considered to be elaboration
26736 code.
26737
26738 @item
26739 Some of the invocations in elaboration code may not take place at run time
26740 due to conditional execution.
26741 @end itemize
26742
26743 GNAT performs extensive diagnostics on a unit-by-unit basis for all scenarios
26744 that invoke internal targets. In addition, GNAT generates run-time checks for
26745 all external targets and for all scenarios that may exhibit ABE problems.
26746
26747 The elaboration order is obtained by honoring all @emph{with} clauses, purity and
26748 preelaborability of units, and elaboration-control pragmas. The dynamic model
26749 attempts to take all invocations in elaboration code into account. If an
26750 invocation leads to a circularity, GNAT ignores the invocation based on the
26751 assumptions stated above. An order obtained using the dynamic model may fail
26752 an ABE check at run time when GNAT ignored an invocation.
26753
26754 The dynamic model is enabled with compiler switch @code{-gnatE}.
26755 @end itemize
26756
26757 @geindex Static elaboration model
26758
26759
26760 @itemize *
26761
26762 @item
26763 @emph{Static elaboration model}
26764
26765 This is the middle ground of the three models. When the static model is in
26766 effect, GNAT makes the following assumptions:
26767
26768
26769 @itemize -
26770
26771 @item
26772 Only code at the library level and in package body statements within all
26773 units in a partition is considered to be elaboration code.
26774
26775 @item
26776 All invocations in elaboration will take place at run time, regardless of
26777 conditional execution.
26778 @end itemize
26779
26780 GNAT performs extensive diagnostics on a unit-by-unit basis for all scenarios
26781 that invoke internal targets. In addition, GNAT generates run-time checks for
26782 all external targets and for all scenarios that may exhibit ABE problems.
26783
26784 The elaboration order is obtained by honoring all @emph{with} clauses, purity and
26785 preelaborability of units, presence of elaboration-control pragmas, and all
26786 invocations in elaboration code. An order obtained using the static model is
26787 guaranteed to be ABE problem-free, excluding dispatching calls and
26788 access-to-subprogram types.
26789
26790 The static model is the default model in GNAT.
26791 @end itemize
26792
26793 @geindex SPARK elaboration model
26794
26795
26796 @itemize *
26797
26798 @item
26799 @emph{SPARK elaboration model}
26800
26801 This is the most conservative of the three models and enforces the SPARK
26802 rules of elaboration as defined in the SPARK Reference Manual, section 7.7.
26803 The SPARK model is in effect only when a scenario and a target reside in a
26804 region subject to @code{SPARK_Mode On}, otherwise the dynamic or static model
26805 is in effect.
26806
26807 The SPARK model is enabled with compiler switch @code{-gnatd.v}.
26808 @end itemize
26809
26810 @geindex Legacy elaboration models
26811
26812
26813 @itemize *
26814
26815 @item
26816 @emph{Legacy elaboration models}
26817
26818 In addition to the three elaboration models outlined above, GNAT provides the
26819 following legacy models:
26820
26821
26822 @itemize -
26823
26824 @item
26825 @cite{Legacy elaboration-checking model} available in pre-18.x versions of GNAT.
26826 This model is enabled with compiler switch @code{-gnatH}.
26827
26828 @item
26829 @cite{Legacy elaboration-order model} available in pre-20.x versions of GNAT.
26830 This model is enabled with binder switch @code{-H}.
26831 @end itemize
26832 @end itemize
26833
26834 @geindex Relaxed elaboration mode
26835
26836 The dynamic, legacy, and static models can be relaxed using compiler switch
26837 @code{-gnatJ}, making them more permissive. Note that in this mode, GNAT
26838 may not diagnose certain elaboration issues or install run-time checks.
26839
26840 @node Mixing Elaboration Models,ABE Diagnostics,Controlling the Elaboration Order in GNAT,Elaboration Order Handling in GNAT
26841 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id7}@anchor{220}@anchor{gnat_ugn/elaboration_order_handling_in_gnat mixing-elaboration-models}@anchor{221}
26842 @section Mixing Elaboration Models
26843
26844
26845 It is possible to mix units compiled with a different elaboration model,
26846 however the following rules must be observed:
26847
26848
26849 @itemize *
26850
26851 @item
26852 A client unit compiled with the dynamic model can only @emph{with} a server unit
26853 that meets at least one of the following criteria:
26854
26855
26856 @itemize -
26857
26858 @item
26859 The server unit is compiled with the dynamic model.
26860
26861 @item
26862 The server unit is a GNAT implementation unit from the @code{Ada}, @code{GNAT},
26863 @code{Interfaces}, or @code{System} hierarchies.
26864
26865 @item
26866 The server unit has pragma @code{Pure} or @code{Preelaborate}.
26867
26868 @item
26869 The client unit has an explicit @code{Elaborate_All} pragma for the server
26870 unit.
26871 @end itemize
26872 @end itemize
26873
26874 These rules ensure that elaboration checks are not omitted. If the rules are
26875 violated, the binder emits a warning:
26876
26877 @quotation
26878
26879 @example
26880 warning: "x.ads" has dynamic elaboration checks and with's
26881 warning: "y.ads" which has static elaboration checks
26882 @end example
26883 @end quotation
26884
26885 The warnings can be suppressed by binder switch @code{-ws}.
26886
26887 @node ABE Diagnostics,SPARK Diagnostics,Mixing Elaboration Models,Elaboration Order Handling in GNAT
26888 @anchor{gnat_ugn/elaboration_order_handling_in_gnat abe-diagnostics}@anchor{222}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id8}@anchor{223}
26889 @section ABE Diagnostics
26890
26891
26892 GNAT performs extensive diagnostics on a unit-by-unit basis for all scenarios
26893 that invoke internal targets, regardless of whether the dynamic, SPARK, or
26894 static model is in effect.
26895
26896 Note that GNAT emits warnings rather than hard errors whenever it encounters an
26897 elaboration problem. This is because the elaboration model in effect may be too
26898 conservative, or a particular scenario may not be invoked due conditional
26899 execution. The warnings can be suppressed selectively with @code{pragma Warnings
26900 (Off)} or globally with compiler switch @code{-gnatwL}.
26901
26902 A @emph{guaranteed ABE} arises when the body of a target is not elaborated early
26903 enough, and causes @emph{all} scenarios that directly invoke the target to fail.
26904
26905 @quotation
26906
26907 @example
26908 package body Guaranteed_ABE is
26909 function ABE return Integer;
26910
26911 Val : constant Integer := ABE;
26912
26913 function ABE return Integer is
26914 begin
26915 ...
26916 end ABE;
26917 end Guaranteed_ABE;
26918 @end example
26919 @end quotation
26920
26921 In the example above, the elaboration of @code{Guaranteed_ABE}’s body elaborates
26922 the declaration of @code{Val}. This invokes function @code{ABE}, however the body of
26923 @code{ABE} has not been elaborated yet. GNAT emits the following diagnostic:
26924
26925 @quotation
26926
26927 @example
26928 4. Val : constant Integer := ABE;
26929 |
26930 >>> warning: cannot call "ABE" before body seen
26931 >>> warning: Program_Error will be raised at run time
26932 @end example
26933 @end quotation
26934
26935 A @emph{conditional ABE} arises when the body of a target is not elaborated early
26936 enough, and causes @emph{some} scenarios that directly invoke the target to fail.
26937
26938 @quotation
26939
26940 @example
26941 1. package body Conditional_ABE is
26942 2. procedure Force_Body is null;
26943 3.
26944 4. generic
26945 5. with function Func return Integer;
26946 6. package Gen is
26947 7. Val : constant Integer := Func;
26948 8. end Gen;
26949 9.
26950 10. function ABE return Integer;
26951 11.
26952 12. function Cause_ABE return Boolean is
26953 13. package Inst is new Gen (ABE);
26954 14. begin
26955 15. ...
26956 16. end Cause_ABE;
26957 17.
26958 18. Val : constant Boolean := Cause_ABE;
26959 19.
26960 20. function ABE return Integer is
26961 21. begin
26962 22. ...
26963 23. end ABE;
26964 24.
26965 25. Safe : constant Boolean := Cause_ABE;
26966 26. end Conditional_ABE;
26967 @end example
26968 @end quotation
26969
26970 In the example above, the elaboration of package body @code{Conditional_ABE}
26971 elaborates the declaration of @code{Val}. This invokes function @code{Cause_ABE},
26972 which instantiates generic unit @code{Gen} as @code{Inst}. The elaboration of
26973 @code{Inst} invokes function @code{ABE}, however the body of @code{ABE} has not been
26974 elaborated yet. GNAT emits the following diagnostic:
26975
26976 @quotation
26977
26978 @example
26979 13. package Inst is new Gen (ABE);
26980 |
26981 >>> warning: in instantiation at line 7
26982 >>> warning: cannot call "ABE" before body seen
26983 >>> warning: Program_Error may be raised at run time
26984 >>> warning: body of unit "Conditional_ABE" elaborated
26985 >>> warning: function "Cause_ABE" called at line 18
26986 >>> warning: function "ABE" called at line 7, instance at line 13
26987 @end example
26988 @end quotation
26989
26990 Note that the same ABE problem does not occur with the elaboration of
26991 declaration @code{Safe} because the body of function @code{ABE} has already been
26992 elaborated at that point.
26993
26994 @node SPARK Diagnostics,Elaboration Circularities,ABE Diagnostics,Elaboration Order Handling in GNAT
26995 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id9}@anchor{224}@anchor{gnat_ugn/elaboration_order_handling_in_gnat spark-diagnostics}@anchor{225}
26996 @section SPARK Diagnostics
26997
26998
26999 GNAT enforces the SPARK rules of elaboration as defined in the SPARK Reference
27000 Manual section 7.7 when compiler switch @code{-gnatd.v} is in effect. Note
27001 that GNAT emits hard errors whenever it encounters a violation of the SPARK
27002 rules.
27003
27004 @quotation
27005
27006 @example
27007 1. with Server;
27008 2. package body SPARK_Diagnostics with SPARK_Mode is
27009 3. Val : constant Integer := Server.Func;
27010 |
27011 >>> call to "Func" during elaboration in SPARK
27012 >>> unit "SPARK_Diagnostics" requires pragma "Elaborate_All" for "Server"
27013 >>> body of unit "SPARK_Model" elaborated
27014 >>> function "Func" called at line 3
27015
27016 4. end SPARK_Diagnostics;
27017 @end example
27018 @end quotation
27019
27020 @node Elaboration Circularities,Resolving Elaboration Circularities,SPARK Diagnostics,Elaboration Order Handling in GNAT
27021 @anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-circularities}@anchor{226}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id10}@anchor{227}
27022 @section Elaboration Circularities
27023
27024
27025 An @strong{elaboration circularity} occurs whenever the elaboration of a set of
27026 units enters a deadlocked state, where each unit is waiting for another unit
27027 to be elaborated. This situation may be the result of improper use of @emph{with}
27028 clauses, elaboration-control pragmas, or invocations in elaboration code.
27029
27030 The following example exhibits an elaboration circularity.
27031
27032 @quotation
27033
27034 @example
27035 with B; pragma Elaborate (B);
27036 package A is
27037 end A;
27038 @end example
27039
27040 @example
27041 package B is
27042 procedure Force_Body;
27043 end B;
27044 @end example
27045
27046 @example
27047 with C;
27048 package body B is
27049 procedure Force_Body is null;
27050
27051 Elab : constant Integer := C.Func;
27052 end B;
27053 @end example
27054
27055 @example
27056 package C is
27057 function Func return Integer;
27058 end C;
27059 @end example
27060
27061 @example
27062 with A;
27063 package body C is
27064 function Func return Integer is
27065 begin
27066 ...
27067 end Func;
27068 end C;
27069 @end example
27070 @end quotation
27071
27072 The binder emits the following diagnostic:
27073
27074 @quotation
27075
27076 @example
27077 error: Elaboration circularity detected
27078 info:
27079 info: Reason:
27080 info:
27081 info: unit "a (spec)" depends on its own elaboration
27082 info:
27083 info: Circularity:
27084 info:
27085 info: unit "a (spec)" has with clause and pragma Elaborate for unit "b (spec)"
27086 info: unit "b (body)" is in the closure of pragma Elaborate
27087 info: unit "b (body)" invokes a construct of unit "c (body)" at elaboration time
27088 info: unit "c (body)" has with clause for unit "a (spec)"
27089 info:
27090 info: Suggestions:
27091 info:
27092 info: remove pragma Elaborate for unit "b (body)" in unit "a (spec)"
27093 info: use the dynamic elaboration model (compiler switch -gnatE)
27094 @end example
27095 @end quotation
27096
27097 The diagnostic consist of the following sections:
27098
27099
27100 @itemize *
27101
27102 @item
27103 Reason
27104
27105 This section provides a short explanation describing why the set of units
27106 could not be ordered.
27107
27108 @item
27109 Circularity
27110
27111 This section enumerates the units comprising the deadlocked set, along with
27112 their interdependencies.
27113
27114 @item
27115 Suggestions
27116
27117 This section enumerates various tactics for eliminating the circularity.
27118 @end itemize
27119
27120 @node Resolving Elaboration Circularities,Elaboration-related Compiler Switches,Elaboration Circularities,Elaboration Order Handling in GNAT
27121 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id11}@anchor{228}@anchor{gnat_ugn/elaboration_order_handling_in_gnat resolving-elaboration-circularities}@anchor{229}
27122 @section Resolving Elaboration Circularities
27123
27124
27125 The most desirable option from the point of view of long-term maintenance is to
27126 rearrange the program so that the elaboration problems are avoided. One useful
27127 technique is to place the elaboration code into separate child packages.
27128 Another is to move some of the initialization code to explicitly invoked
27129 subprograms, where the program controls the order of initialization explicitly.
27130 Although this is the most desirable option, it may be impractical and involve
27131 too much modification, especially in the case of complex legacy code.
27132
27133 When faced with an elaboration circularity, the programmer should also consider
27134 the tactics given in the suggestions section of the circularity diagnostic.
27135 Depending on the units involved in the circularity, their @emph{with} clauses,
27136 purity, preelaborability, presence of elaboration-control pragmas and
27137 invocations at elaboration time, the binder may suggest one or more of the
27138 following tactics to eliminate the circularity:
27139
27140
27141 @itemize *
27142
27143 @item
27144 Pragma Elaborate elimination
27145
27146 @example
27147 remove pragma Elaborate for unit "..." in unit "..."
27148 @end example
27149
27150 This tactic is suggested when the binder has determined that pragma
27151 @code{Elaborate}:
27152
27153
27154 @itemize -
27155
27156 @item
27157 Prevents a set of units from being elaborated.
27158
27159 @item
27160 The removal of the pragma will not eliminate the semantic effects of the
27161 pragma. In other words, the argument of the pragma will still be elaborated
27162 prior to the unit containing the pragma.
27163
27164 @item
27165 The removal of the pragma will enable the successful ordering of the units.
27166 @end itemize
27167
27168 The programmer should remove the pragma as advised, and rebuild the program.
27169
27170 @item
27171 Pragma Elaborate_All elimination
27172
27173 @example
27174 remove pragma Elaborate_All for unit "..." in unit "..."
27175 @end example
27176
27177 This tactic is suggested when the binder has determined that pragma
27178 @code{Elaborate_All}:
27179
27180
27181 @itemize -
27182
27183 @item
27184 Prevents a set of units from being elaborated.
27185
27186 @item
27187 The removal of the pragma will not eliminate the semantic effects of the
27188 pragma. In other words, the argument of the pragma along with its @emph{with}
27189 closure will still be elaborated prior to the unit containing the pragma.
27190
27191 @item
27192 The removal of the pragma will enable the successful ordering of the units.
27193 @end itemize
27194
27195 The programmer should remove the pragma as advised, and rebuild the program.
27196
27197 @item
27198 Pragma Elaborate_All downgrade
27199
27200 @example
27201 change pragma Elaborate_All for unit "..." to Elaborate in unit "..."
27202 @end example
27203
27204 This tactic is always suggested with the pragma @code{Elaborate_All} elimination
27205 tactic. It offers a different alernative of guaranteeing that the argument of
27206 the pragma will still be elaborated prior to the unit containing the pragma.
27207
27208 The programmer should update the pragma as advised, and rebuild the program.
27209
27210 @item
27211 Pragma Elaborate_Body elimination
27212
27213 @example
27214 remove pragma Elaborate_Body in unit "..."
27215 @end example
27216
27217 This tactic is suggested when the binder has determined that pragma
27218 @code{Elaborate_Body}:
27219
27220
27221 @itemize -
27222
27223 @item
27224 Prevents a set of units from being elaborated.
27225
27226 @item
27227 The removal of the pragma will enable the successful ordering of the units.
27228 @end itemize
27229
27230 Note that the binder cannot determine whether the pragma is required for
27231 other purposes, such as guaranteeing the initialization of a variable
27232 declared in the spec by elaboration code in the body.
27233
27234 The programmer should remove the pragma as advised, and rebuild the program.
27235
27236 @item
27237 Use of pragma Restrictions
27238
27239 @example
27240 use pragma Restrictions (No_Entry_Calls_In_Elaboration_Code)
27241 @end example
27242
27243 This tactic is suggested when the binder has determined that a task
27244 activation at elaboration time:
27245
27246
27247 @itemize -
27248
27249 @item
27250 Prevents a set of units from being elaborated.
27251 @end itemize
27252
27253 Note that the binder cannot determine with certainty whether the task will
27254 block at elaboration time.
27255
27256 The programmer should create a configuration file, place the pragma within,
27257 update the general compilation arguments, and rebuild the program.
27258
27259 @item
27260 Use of dynamic elaboration model
27261
27262 @example
27263 use the dynamic elaboration model (compiler switch -gnatE)
27264 @end example
27265
27266 This tactic is suggested when the binder has determined that an invocation at
27267 elaboration time:
27268
27269
27270 @itemize -
27271
27272 @item
27273 Prevents a set of units from being elaborated.
27274
27275 @item
27276 The use of the dynamic model will enable the successful ordering of the
27277 units.
27278 @end itemize
27279
27280 The programmer has two options:
27281
27282
27283 @itemize -
27284
27285 @item
27286 Determine the units involved in the invocation using the detailed
27287 invocation information, and add compiler switch @code{-gnatE} to the
27288 compilation arguments of selected files only. This approach will yield
27289 safer elaboration orders compared to the other option because it will
27290 minimize the opportunities presented to the dynamic model for ignoring
27291 invocations.
27292
27293 @item
27294 Add compiler switch @code{-gnatE} to the general compilation arguments.
27295 @end itemize
27296
27297 @item
27298 Use of detailed invocation information
27299
27300 @example
27301 use detailed invocation information (compiler switch -gnatd_F)
27302 @end example
27303
27304 This tactic is always suggested with the use of the dynamic model tactic. It
27305 causes the circularity section of the circularity diagnostic to describe the
27306 flow of elaboration code from a unit to a unit, enumerating all such paths in
27307 the process.
27308
27309 The programmer should analyze this information to determine which units
27310 should be compiled with the dynamic model.
27311
27312 @item
27313 Forced-dependency elimination
27314
27315 @example
27316 remove the dependency of unit "..." on unit "..." from the argument of switch -f
27317 @end example
27318
27319 This tactic is suggested when the binder has determined that a dependency
27320 present in the forced-elaboration-order file indicated by binder switch
27321 @code{-f}:
27322
27323
27324 @itemize -
27325
27326 @item
27327 Prevents a set of units from being elaborated.
27328
27329 @item
27330 The removal of the dependency will enable the successful ordering of the
27331 units.
27332 @end itemize
27333
27334 The programmer should edit the forced-elaboration-order file, remove the
27335 dependency, and rebind the program.
27336
27337 @item
27338 All forced-dependency elimination
27339
27340 @example
27341 remove switch -f
27342 @end example
27343
27344 This tactic is suggested in case editing the forced-elaboration-order file is
27345 not an option.
27346
27347 The programmer should remove binder switch @code{-f} from the binder
27348 arguments, and rebind.
27349
27350 @item
27351 Multiple-circularities diagnostic
27352
27353 @example
27354 diagnose all circularities (binder switch -d_C)
27355 @end example
27356
27357 By default, the binder will diagnose only the highest-precedence circularity.
27358 If the program contains multiple circularities, the binder will suggest the
27359 use of binder switch @code{-d_C} in order to obtain the diagnostics of all
27360 circularities.
27361
27362 The programmer should add binder switch @code{-d_C} to the binder
27363 arguments, and rebind.
27364 @end itemize
27365
27366 If none of the tactics suggested by the binder eliminate the elaboration
27367 circularity, the programmer should consider using one of the legacy elaboration
27368 models, in the following order:
27369
27370
27371 @itemize *
27372
27373 @item
27374 Use the pre-20.x legacy elaboration-order model, with binder switch
27375 @code{-H}.
27376
27377 @item
27378 Use both pre-18.x and pre-20.x legacy elaboration models, with compiler
27379 switch @code{-gnatH} and binder switch @code{-H}.
27380
27381 @item
27382 Use the relaxed static-elaboration model, with compiler switches
27383 @code{-gnatH} @code{-gnatJ} and binder switch @code{-H}.
27384
27385 @item
27386 Use the relaxed dynamic-elaboration model, with compiler switches
27387 @code{-gnatH} @code{-gnatJ} @code{-gnatE} and binder switch
27388 @code{-H}.
27389 @end itemize
27390
27391 @node Elaboration-related Compiler Switches,Summary of Procedures for Elaboration Control,Resolving Elaboration Circularities,Elaboration Order Handling in GNAT
27392 @anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-related-compiler-switches}@anchor{22a}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id12}@anchor{22b}
27393 @section Elaboration-related Compiler Switches
27394
27395
27396 GNAT has several switches that affect the elaboration model and consequently
27397 the elaboration order chosen by the binder.
27398
27399 @geindex -gnatE (gnat)
27400
27401
27402 @table @asis
27403
27404 @item @code{-gnatE}
27405
27406 Dynamic elaboration checking mode enabled
27407
27408 When this switch is in effect, GNAT activates the dynamic model.
27409 @end table
27410
27411 @geindex -gnatel (gnat)
27412
27413
27414 @table @asis
27415
27416 @item @code{-gnatel}
27417
27418 Turn on info messages on generated Elaborate[_All] pragmas
27419
27420 This switch is only applicable to the pre-20.x legacy elaboration models.
27421 The post-20.x elaboration model no longer relies on implicitly generated
27422 @code{Elaborate} and @code{Elaborate_All} pragmas to order units.
27423
27424 When this switch is in effect, GNAT will emit the following supplementary
27425 information depending on the elaboration model in effect.
27426
27427
27428 @itemize -
27429
27430 @item
27431 @emph{Dynamic model}
27432
27433 GNAT will indicate missing @code{Elaborate} and @code{Elaborate_All} pragmas for
27434 all library-level scenarios within the partition.
27435
27436 @item
27437 @emph{Static model}
27438
27439 GNAT will indicate all scenarios invoked during elaboration. In addition,
27440 it will provide detailed traceback when an implicit @code{Elaborate} or
27441 @code{Elaborate_All} pragma is generated.
27442
27443 @item
27444 @emph{SPARK model}
27445
27446 GNAT will indicate how an elaboration requirement is met by the context of
27447 a unit. This diagnostic requires compiler switch @code{-gnatd.v}.
27448
27449 @example
27450 1. with Server; pragma Elaborate_All (Server);
27451 2. package Client with SPARK_Mode is
27452 3. Val : constant Integer := Server.Func;
27453 |
27454 >>> info: call to "Func" during elaboration in SPARK
27455 >>> info: "Elaborate_All" requirement for unit "Server" met by pragma at line 1
27456
27457 4. end Client;
27458 @end example
27459 @end itemize
27460 @end table
27461
27462 @geindex -gnatH (gnat)
27463
27464
27465 @table @asis
27466
27467 @item @code{-gnatH}
27468
27469 Legacy elaboration checking mode enabled
27470
27471 When this switch is in effect, GNAT will utilize the pre-18.x elaboration
27472 model.
27473 @end table
27474
27475 @geindex -gnatJ (gnat)
27476
27477
27478 @table @asis
27479
27480 @item @code{-gnatJ}
27481
27482 Relaxed elaboration checking mode enabled
27483
27484 When this switch is in effect, GNAT will not process certain scenarios,
27485 resulting in a more permissive elaboration model. Note that this may
27486 eliminate some diagnostics and run-time checks.
27487 @end table
27488
27489 @geindex -gnatw.f (gnat)
27490
27491
27492 @table @asis
27493
27494 @item @code{-gnatw.f}
27495
27496 Turn on warnings for suspicious Subp’Access
27497
27498 When this switch is in effect, GNAT will treat @code{'Access} of an entry,
27499 operator, or subprogram as a potential call to the target and issue warnings:
27500
27501 @example
27502 1. package body Attribute_Call is
27503 2. function Func return Integer;
27504 3. type Func_Ptr is access function return Integer;
27505 4.
27506 5. Ptr : constant Func_Ptr := Func'Access;
27507 |
27508 >>> warning: "Access" attribute of "Func" before body seen
27509 >>> warning: possible Program_Error on later references
27510 >>> warning: body of unit "Attribute_Call" elaborated
27511 >>> warning: "Access" of "Func" taken at line 5
27512
27513 6.
27514 7. function Func return Integer is
27515 8. begin
27516 9. ...
27517 10. end Func;
27518 11. end Attribute_Call;
27519 @end example
27520
27521 In the example above, the elaboration of declaration @code{Ptr} is assigned
27522 @code{Func'Access} before the body of @code{Func} has been elaborated.
27523 @end table
27524
27525 @geindex -gnatwl (gnat)
27526
27527
27528 @table @asis
27529
27530 @item @code{-gnatwl}
27531
27532 Turn on warnings for elaboration problems
27533
27534 When this switch is in effect, GNAT emits diagnostics in the form of warnings
27535 concerning various elaboration problems. The warnings are enabled by default.
27536 The switch is provided in case all warnings are suppressed, but elaboration
27537 warnings are still desired.
27538
27539 @item @code{-gnatwL}
27540
27541 Turn off warnings for elaboration problems
27542
27543 When this switch is in effect, GNAT no longer emits any diagnostics in the
27544 form of warnings. Selective suppression of elaboration problems is possible
27545 using @code{pragma Warnings (Off)}.
27546
27547 @example
27548 1. package body Selective_Suppression is
27549 2. function ABE return Integer;
27550 3.
27551 4. Val_1 : constant Integer := ABE;
27552 |
27553 >>> warning: cannot call "ABE" before body seen
27554 >>> warning: Program_Error will be raised at run time
27555
27556 5.
27557 6. pragma Warnings (Off);
27558 7. Val_2 : constant Integer := ABE;
27559 8. pragma Warnings (On);
27560 9.
27561 10. function ABE return Integer is
27562 11. begin
27563 12. ...
27564 13. end ABE;
27565 14. end Selective_Suppression;
27566 @end example
27567
27568 Note that suppressing elaboration warnings does not eliminate run-time
27569 checks. The example above will still fail at run time with an ABE.
27570 @end table
27571
27572 @node Summary of Procedures for Elaboration Control,Inspecting the Chosen Elaboration Order,Elaboration-related Compiler Switches,Elaboration Order Handling in GNAT
27573 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id13}@anchor{22c}@anchor{gnat_ugn/elaboration_order_handling_in_gnat summary-of-procedures-for-elaboration-control}@anchor{22d}
27574 @section Summary of Procedures for Elaboration Control
27575
27576
27577 A programmer should first compile the program with the default options, using
27578 none of the binder or compiler switches. If the binder succeeds in finding an
27579 elaboration order, then apart from possible cases involing dispatching calls
27580 and access-to-subprogram types, the program is free of elaboration errors.
27581
27582 If it is important for the program to be portable to compilers other than GNAT,
27583 then the programmer should use compiler switch @code{-gnatel} and consider
27584 the messages about missing or implicitly created @code{Elaborate} and
27585 @code{Elaborate_All} pragmas.
27586
27587 If the binder reports an elaboration circularity, the programmer has several
27588 options:
27589
27590
27591 @itemize *
27592
27593 @item
27594 Ensure that elaboration warnings are enabled. This will allow the static
27595 model to output trace information of elaboration issues. The trace
27596 information could shed light on previously unforeseen dependencies, as well
27597 as their origins. Elaboration warnings are enabled with compiler switch
27598 @code{-gnatwl}.
27599
27600 @item
27601 Cosider the tactics given in the suggestions section of the circularity
27602 diagnostic.
27603
27604 @item
27605 If none of the steps outlined above resolve the circularity, use a more
27606 permissive elaboration model, in the following order:
27607
27608
27609 @itemize -
27610
27611 @item
27612 Use the pre-20.x legacy elaboration-order model, with binder switch
27613 @code{-H}.
27614
27615 @item
27616 Use both pre-18.x and pre-20.x legacy elaboration models, with compiler
27617 switch @code{-gnatH} and binder switch @code{-H}.
27618
27619 @item
27620 Use the relaxed static elaboration model, with compiler switches
27621 @code{-gnatH} @code{-gnatJ} and binder switch @code{-H}.
27622
27623 @item
27624 Use the relaxed dynamic elaboration model, with compiler switches
27625 @code{-gnatH} @code{-gnatJ} @code{-gnatE} and binder switch
27626 @code{-H}.
27627 @end itemize
27628 @end itemize
27629
27630 @node Inspecting the Chosen Elaboration Order,,Summary of Procedures for Elaboration Control,Elaboration Order Handling in GNAT
27631 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id14}@anchor{22e}@anchor{gnat_ugn/elaboration_order_handling_in_gnat inspecting-the-chosen-elaboration-order}@anchor{22f}
27632 @section Inspecting the Chosen Elaboration Order
27633
27634
27635 To see the elaboration order chosen by the binder, inspect the contents of file
27636 @cite{b~xxx.adb}. On certain targets, this file appears as @cite{b_xxx.adb}. The
27637 elaboration order appears as a sequence of calls to @code{Elab_Body} and
27638 @code{Elab_Spec}, interspersed with assignments to @cite{Exxx} which indicates that a
27639 particular unit is elaborated. For example:
27640
27641 @quotation
27642
27643 @example
27644 System.Soft_Links'Elab_Body;
27645 E14 := True;
27646 System.Secondary_Stack'Elab_Body;
27647 E18 := True;
27648 System.Exception_Table'Elab_Body;
27649 E24 := True;
27650 Ada.Io_Exceptions'Elab_Spec;
27651 E67 := True;
27652 Ada.Tags'Elab_Spec;
27653 Ada.Streams'Elab_Spec;
27654 E43 := True;
27655 Interfaces.C'Elab_Spec;
27656 E69 := True;
27657 System.Finalization_Root'Elab_Spec;
27658 E60 := True;
27659 System.Os_Lib'Elab_Body;
27660 E71 := True;
27661 System.Finalization_Implementation'Elab_Spec;
27662 System.Finalization_Implementation'Elab_Body;
27663 E62 := True;
27664 Ada.Finalization'Elab_Spec;
27665 E58 := True;
27666 Ada.Finalization.List_Controller'Elab_Spec;
27667 E76 := True;
27668 System.File_Control_Block'Elab_Spec;
27669 E74 := True;
27670 System.File_Io'Elab_Body;
27671 E56 := True;
27672 Ada.Tags'Elab_Body;
27673 E45 := True;
27674 Ada.Text_Io'Elab_Spec;
27675 Ada.Text_Io'Elab_Body;
27676 E07 := True;
27677 @end example
27678 @end quotation
27679
27680 Note also binder switch @code{-l}, which outputs the chosen elaboration
27681 order and provides a more readable form of the above:
27682
27683 @quotation
27684
27685 @example
27686 ada (spec)
27687 interfaces (spec)
27688 system (spec)
27689 system.case_util (spec)
27690 system.case_util (body)
27691 system.concat_2 (spec)
27692 system.concat_2 (body)
27693 system.concat_3 (spec)
27694 system.concat_3 (body)
27695 system.htable (spec)
27696 system.parameters (spec)
27697 system.parameters (body)
27698 system.crtl (spec)
27699 interfaces.c_streams (spec)
27700 interfaces.c_streams (body)
27701 system.restrictions (spec)
27702 system.restrictions (body)
27703 system.standard_library (spec)
27704 system.exceptions (spec)
27705 system.exceptions (body)
27706 system.storage_elements (spec)
27707 system.storage_elements (body)
27708 system.secondary_stack (spec)
27709 system.stack_checking (spec)
27710 system.stack_checking (body)
27711 system.string_hash (spec)
27712 system.string_hash (body)
27713 system.htable (body)
27714 system.strings (spec)
27715 system.strings (body)
27716 system.traceback (spec)
27717 system.traceback (body)
27718 system.traceback_entries (spec)
27719 system.traceback_entries (body)
27720 ada.exceptions (spec)
27721 ada.exceptions.last_chance_handler (spec)
27722 system.soft_links (spec)
27723 system.soft_links (body)
27724 ada.exceptions.last_chance_handler (body)
27725 system.secondary_stack (body)
27726 system.exception_table (spec)
27727 system.exception_table (body)
27728 ada.io_exceptions (spec)
27729 ada.tags (spec)
27730 ada.streams (spec)
27731 interfaces.c (spec)
27732 interfaces.c (body)
27733 system.finalization_root (spec)
27734 system.finalization_root (body)
27735 system.memory (spec)
27736 system.memory (body)
27737 system.standard_library (body)
27738 system.os_lib (spec)
27739 system.os_lib (body)
27740 system.unsigned_types (spec)
27741 system.stream_attributes (spec)
27742 system.stream_attributes (body)
27743 system.finalization_implementation (spec)
27744 system.finalization_implementation (body)
27745 ada.finalization (spec)
27746 ada.finalization (body)
27747 ada.finalization.list_controller (spec)
27748 ada.finalization.list_controller (body)
27749 system.file_control_block (spec)
27750 system.file_io (spec)
27751 system.file_io (body)
27752 system.val_uns (spec)
27753 system.val_util (spec)
27754 system.val_util (body)
27755 system.val_uns (body)
27756 system.wch_con (spec)
27757 system.wch_con (body)
27758 system.wch_cnv (spec)
27759 system.wch_jis (spec)
27760 system.wch_jis (body)
27761 system.wch_cnv (body)
27762 system.wch_stw (spec)
27763 system.wch_stw (body)
27764 ada.tags (body)
27765 ada.exceptions (body)
27766 ada.text_io (spec)
27767 ada.text_io (body)
27768 text_io (spec)
27769 gdbstr (body)
27770 @end example
27771 @end quotation
27772
27773 @node Inline Assembler,GNU Free Documentation License,Elaboration Order Handling in GNAT,Top
27774 @anchor{gnat_ugn/inline_assembler doc}@anchor{230}@anchor{gnat_ugn/inline_assembler id1}@anchor{231}@anchor{gnat_ugn/inline_assembler inline-assembler}@anchor{10}
27775 @chapter Inline Assembler
27776
27777
27778 @geindex Inline Assembler
27779
27780 If you need to write low-level software that interacts directly
27781 with the hardware, Ada provides two ways to incorporate assembly
27782 language code into your program. First, you can import and invoke
27783 external routines written in assembly language, an Ada feature fully
27784 supported by GNAT. However, for small sections of code it may be simpler
27785 or more efficient to include assembly language statements directly
27786 in your Ada source program, using the facilities of the implementation-defined
27787 package @code{System.Machine_Code}, which incorporates the gcc
27788 Inline Assembler. The Inline Assembler approach offers a number of advantages,
27789 including the following:
27790
27791
27792 @itemize *
27793
27794 @item
27795 No need to use non-Ada tools
27796
27797 @item
27798 Consistent interface over different targets
27799
27800 @item
27801 Automatic usage of the proper calling conventions
27802
27803 @item
27804 Access to Ada constants and variables
27805
27806 @item
27807 Definition of intrinsic routines
27808
27809 @item
27810 Possibility of inlining a subprogram comprising assembler code
27811
27812 @item
27813 Code optimizer can take Inline Assembler code into account
27814 @end itemize
27815
27816 This appendix presents a series of examples to show you how to use
27817 the Inline Assembler. Although it focuses on the Intel x86,
27818 the general approach applies also to other processors.
27819 It is assumed that you are familiar with Ada
27820 and with assembly language programming.
27821
27822 @menu
27823 * Basic Assembler Syntax::
27824 * A Simple Example of Inline Assembler::
27825 * Output Variables in Inline Assembler::
27826 * Input Variables in Inline Assembler::
27827 * Inlining Inline Assembler Code::
27828 * Other Asm Functionality::
27829
27830 @end menu
27831
27832 @node Basic Assembler Syntax,A Simple Example of Inline Assembler,,Inline Assembler
27833 @anchor{gnat_ugn/inline_assembler basic-assembler-syntax}@anchor{232}@anchor{gnat_ugn/inline_assembler id2}@anchor{233}
27834 @section Basic Assembler Syntax
27835
27836
27837 The assembler used by GNAT and gcc is based not on the Intel assembly
27838 language, but rather on a language that descends from the AT&T Unix
27839 assembler @code{as} (and which is often referred to as ‘AT&T syntax’).
27840 The following table summarizes the main features of @code{as} syntax
27841 and points out the differences from the Intel conventions.
27842 See the gcc @code{as} and @code{gas} (an @code{as} macro
27843 pre-processor) documentation for further information.
27844
27845
27846 @display
27847 @emph{Register names}@w{ }
27848 @display
27849 gcc / @code{as}: Prefix with ‘%’; for example @code{%eax}@w{ }
27850 Intel: No extra punctuation; for example @code{eax}@w{ }
27851 @end display
27852 @end display
27853
27854
27855
27856
27857 @display
27858 @emph{Immediate operand}@w{ }
27859 @display
27860 gcc / @code{as}: Prefix with ‘$’; for example @code{$4}@w{ }
27861 Intel: No extra punctuation; for example @code{4}@w{ }
27862 @end display
27863 @end display
27864
27865
27866
27867
27868 @display
27869 @emph{Address}@w{ }
27870 @display
27871 gcc / @code{as}: Prefix with ‘$’; for example @code{$loc}@w{ }
27872 Intel: No extra punctuation; for example @code{loc}@w{ }
27873 @end display
27874 @end display
27875
27876
27877
27878
27879 @display
27880 @emph{Memory contents}@w{ }
27881 @display
27882 gcc / @code{as}: No extra punctuation; for example @code{loc}@w{ }
27883 Intel: Square brackets; for example @code{[loc]}@w{ }
27884 @end display
27885 @end display
27886
27887
27888
27889
27890 @display
27891 @emph{Register contents}@w{ }
27892 @display
27893 gcc / @code{as}: Parentheses; for example @code{(%eax)}@w{ }
27894 Intel: Square brackets; for example @code{[eax]}@w{ }
27895 @end display
27896 @end display
27897
27898
27899
27900
27901 @display
27902 @emph{Hexadecimal numbers}@w{ }
27903 @display
27904 gcc / @code{as}: Leading ‘0x’ (C language syntax); for example @code{0xA0}@w{ }
27905 Intel: Trailing ‘h’; for example @code{A0h}@w{ }
27906 @end display
27907 @end display
27908
27909
27910
27911
27912 @display
27913 @emph{Operand size}@w{ }
27914 @display
27915 gcc / @code{as}: Explicit in op code; for example @code{movw} to move a 16-bit word@w{ }
27916 Intel: Implicit, deduced by assembler; for example @code{mov}@w{ }
27917 @end display
27918 @end display
27919
27920
27921
27922
27923 @display
27924 @emph{Instruction repetition}@w{ }
27925 @display
27926 gcc / @code{as}: Split into two lines; for example@w{ }
27927 @display
27928 @code{rep}@w{ }
27929 @code{stosl}@w{ }
27930 @end display
27931 Intel: Keep on one line; for example @code{rep stosl}@w{ }
27932 @end display
27933 @end display
27934
27935
27936
27937
27938 @display
27939 @emph{Order of operands}@w{ }
27940 @display
27941 gcc / @code{as}: Source first; for example @code{movw $4, %eax}@w{ }
27942 Intel: Destination first; for example @code{mov eax, 4}@w{ }
27943 @end display
27944 @end display
27945
27946
27947
27948 @node A Simple Example of Inline Assembler,Output Variables in Inline Assembler,Basic Assembler Syntax,Inline Assembler
27949 @anchor{gnat_ugn/inline_assembler a-simple-example-of-inline-assembler}@anchor{234}@anchor{gnat_ugn/inline_assembler id3}@anchor{235}
27950 @section A Simple Example of Inline Assembler
27951
27952
27953 The following example will generate a single assembly language statement,
27954 @code{nop}, which does nothing. Despite its lack of run-time effect,
27955 the example will be useful in illustrating the basics of
27956 the Inline Assembler facility.
27957
27958 @quotation
27959
27960 @example
27961 with System.Machine_Code; use System.Machine_Code;
27962 procedure Nothing is
27963 begin
27964 Asm ("nop");
27965 end Nothing;
27966 @end example
27967 @end quotation
27968
27969 @code{Asm} is a procedure declared in package @code{System.Machine_Code};
27970 here it takes one parameter, a @emph{template string} that must be a static
27971 expression and that will form the generated instruction.
27972 @code{Asm} may be regarded as a compile-time procedure that parses
27973 the template string and additional parameters (none here),
27974 from which it generates a sequence of assembly language instructions.
27975
27976 The examples in this chapter will illustrate several of the forms
27977 for invoking @code{Asm}; a complete specification of the syntax
27978 is found in the @code{Machine_Code_Insertions} section of the
27979 @cite{GNAT Reference Manual}.
27980
27981 Under the standard GNAT conventions, the @code{Nothing} procedure
27982 should be in a file named @code{nothing.adb}.
27983 You can build the executable in the usual way:
27984
27985 @quotation
27986
27987 @example
27988 $ gnatmake nothing
27989 @end example
27990 @end quotation
27991
27992 However, the interesting aspect of this example is not its run-time behavior
27993 but rather the generated assembly code.
27994 To see this output, invoke the compiler as follows:
27995
27996 @quotation
27997
27998 @example
27999 $ gcc -c -S -fomit-frame-pointer -gnatp nothing.adb
28000 @end example
28001 @end quotation
28002
28003 where the options are:
28004
28005
28006 @itemize *
28007
28008 @item
28009
28010 @table @asis
28011
28012 @item @code{-c}
28013
28014 compile only (no bind or link)
28015 @end table
28016
28017 @item
28018
28019 @table @asis
28020
28021 @item @code{-S}
28022
28023 generate assembler listing
28024 @end table
28025
28026 @item
28027
28028 @table @asis
28029
28030 @item @code{-fomit-frame-pointer}
28031
28032 do not set up separate stack frames
28033 @end table
28034
28035 @item
28036
28037 @table @asis
28038
28039 @item @code{-gnatp}
28040
28041 do not add runtime checks
28042 @end table
28043 @end itemize
28044
28045 This gives a human-readable assembler version of the code. The resulting
28046 file will have the same name as the Ada source file, but with a @code{.s}
28047 extension. In our example, the file @code{nothing.s} has the following
28048 contents:
28049
28050 @quotation
28051
28052 @example
28053 .file "nothing.adb"
28054 gcc2_compiled.:
28055 ___gnu_compiled_ada:
28056 .text
28057 .align 4
28058 .globl __ada_nothing
28059 __ada_nothing:
28060 #APP
28061 nop
28062 #NO_APP
28063 jmp L1
28064 .align 2,0x90
28065 L1:
28066 ret
28067 @end example
28068 @end quotation
28069
28070 The assembly code you included is clearly indicated by
28071 the compiler, between the @code{#APP} and @code{#NO_APP}
28072 delimiters. The character before the ‘APP’ and ‘NOAPP’
28073 can differ on different targets. For example, GNU/Linux uses ‘#APP’ while
28074 on NT you will see ‘/APP’.
28075
28076 If you make a mistake in your assembler code (such as using the
28077 wrong size modifier, or using a wrong operand for the instruction) GNAT
28078 will report this error in a temporary file, which will be deleted when
28079 the compilation is finished. Generating an assembler file will help
28080 in such cases, since you can assemble this file separately using the
28081 @code{as} assembler that comes with gcc.
28082
28083 Assembling the file using the command
28084
28085 @quotation
28086
28087 @example
28088 $ as nothing.s
28089 @end example
28090 @end quotation
28091
28092 will give you error messages whose lines correspond to the assembler
28093 input file, so you can easily find and correct any mistakes you made.
28094 If there are no errors, @code{as} will generate an object file
28095 @code{nothing.out}.
28096
28097 @node Output Variables in Inline Assembler,Input Variables in Inline Assembler,A Simple Example of Inline Assembler,Inline Assembler
28098 @anchor{gnat_ugn/inline_assembler id4}@anchor{236}@anchor{gnat_ugn/inline_assembler output-variables-in-inline-assembler}@anchor{237}
28099 @section Output Variables in Inline Assembler
28100
28101
28102 The examples in this section, showing how to access the processor flags,
28103 illustrate how to specify the destination operands for assembly language
28104 statements.
28105
28106 @quotation
28107
28108 @example
28109 with Interfaces; use Interfaces;
28110 with Ada.Text_IO; use Ada.Text_IO;
28111 with System.Machine_Code; use System.Machine_Code;
28112 procedure Get_Flags is
28113 Flags : Unsigned_32;
28114 use ASCII;
28115 begin
28116 Asm ("pushfl" & LF & HT & -- push flags on stack
28117 "popl %%eax" & LF & HT & -- load eax with flags
28118 "movl %%eax, %0", -- store flags in variable
28119 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
28120 Put_Line ("Flags register:" & Flags'Img);
28121 end Get_Flags;
28122 @end example
28123 @end quotation
28124
28125 In order to have a nicely aligned assembly listing, we have separated
28126 multiple assembler statements in the Asm template string with linefeed
28127 (ASCII.LF) and horizontal tab (ASCII.HT) characters.
28128 The resulting section of the assembly output file is:
28129
28130 @quotation
28131
28132 @example
28133 #APP
28134 pushfl
28135 popl %eax
28136 movl %eax, -40(%ebp)
28137 #NO_APP
28138 @end example
28139 @end quotation
28140
28141 It would have been legal to write the Asm invocation as:
28142
28143 @quotation
28144
28145 @example
28146 Asm ("pushfl popl %%eax movl %%eax, %0")
28147 @end example
28148 @end quotation
28149
28150 but in the generated assembler file, this would come out as:
28151
28152 @quotation
28153
28154 @example
28155 #APP
28156 pushfl popl %eax movl %eax, -40(%ebp)
28157 #NO_APP
28158 @end example
28159 @end quotation
28160
28161 which is not so convenient for the human reader.
28162
28163 We use Ada comments
28164 at the end of each line to explain what the assembler instructions
28165 actually do. This is a useful convention.
28166
28167 When writing Inline Assembler instructions, you need to precede each register
28168 and variable name with a percent sign. Since the assembler already requires
28169 a percent sign at the beginning of a register name, you need two consecutive
28170 percent signs for such names in the Asm template string, thus @code{%%eax}.
28171 In the generated assembly code, one of the percent signs will be stripped off.
28172
28173 Names such as @code{%0}, @code{%1}, @code{%2}, etc., denote input or output
28174 variables: operands you later define using @code{Input} or @code{Output}
28175 parameters to @code{Asm}.
28176 An output variable is illustrated in
28177 the third statement in the Asm template string:
28178
28179 @quotation
28180
28181 @example
28182 movl %%eax, %0
28183 @end example
28184 @end quotation
28185
28186 The intent is to store the contents of the eax register in a variable that can
28187 be accessed in Ada. Simply writing @code{movl %%eax, Flags} would not
28188 necessarily work, since the compiler might optimize by using a register
28189 to hold Flags, and the expansion of the @code{movl} instruction would not be
28190 aware of this optimization. The solution is not to store the result directly
28191 but rather to advise the compiler to choose the correct operand form;
28192 that is the purpose of the @code{%0} output variable.
28193
28194 Information about the output variable is supplied in the @code{Outputs}
28195 parameter to @code{Asm}:
28196
28197 @quotation
28198
28199 @example
28200 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
28201 @end example
28202 @end quotation
28203
28204 The output is defined by the @code{Asm_Output} attribute of the target type;
28205 the general format is
28206
28207 @quotation
28208
28209 @example
28210 Type'Asm_Output (constraint_string, variable_name)
28211 @end example
28212 @end quotation
28213
28214 The constraint string directs the compiler how
28215 to store/access the associated variable. In the example
28216
28217 @quotation
28218
28219 @example
28220 Unsigned_32'Asm_Output ("=m", Flags);
28221 @end example
28222 @end quotation
28223
28224 the @code{"m"} (memory) constraint tells the compiler that the variable
28225 @code{Flags} should be stored in a memory variable, thus preventing
28226 the optimizer from keeping it in a register. In contrast,
28227
28228 @quotation
28229
28230 @example
28231 Unsigned_32'Asm_Output ("=r", Flags);
28232 @end example
28233 @end quotation
28234
28235 uses the @code{"r"} (register) constraint, telling the compiler to
28236 store the variable in a register.
28237
28238 If the constraint is preceded by the equal character ‘=’, it tells
28239 the compiler that the variable will be used to store data into it.
28240
28241 In the @code{Get_Flags} example, we used the @code{"g"} (global) constraint,
28242 allowing the optimizer to choose whatever it deems best.
28243
28244 There are a fairly large number of constraints, but the ones that are
28245 most useful (for the Intel x86 processor) are the following:
28246
28247 @quotation
28248
28249
28250 @multitable {xxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
28251 @item
28252
28253 @emph{=}
28254
28255 @tab
28256
28257 output constraint
28258
28259 @item
28260
28261 @emph{g}
28262
28263 @tab
28264
28265 global (i.e., can be stored anywhere)
28266
28267 @item
28268
28269 @emph{m}
28270
28271 @tab
28272
28273 in memory
28274
28275 @item
28276
28277 @emph{I}
28278
28279 @tab
28280
28281 a constant
28282
28283 @item
28284
28285 @emph{a}
28286
28287 @tab
28288
28289 use eax
28290
28291 @item
28292
28293 @emph{b}
28294
28295 @tab
28296
28297 use ebx
28298
28299 @item
28300
28301 @emph{c}
28302
28303 @tab
28304
28305 use ecx
28306
28307 @item
28308
28309 @emph{d}
28310
28311 @tab
28312
28313 use edx
28314
28315 @item
28316
28317 @emph{S}
28318
28319 @tab
28320
28321 use esi
28322
28323 @item
28324
28325 @emph{D}
28326
28327 @tab
28328
28329 use edi
28330
28331 @item
28332
28333 @emph{r}
28334
28335 @tab
28336
28337 use one of eax, ebx, ecx or edx
28338
28339 @item
28340
28341 @emph{q}
28342
28343 @tab
28344
28345 use one of eax, ebx, ecx, edx, esi or edi
28346
28347 @end multitable
28348
28349 @end quotation
28350
28351 The full set of constraints is described in the gcc and @code{as}
28352 documentation; note that it is possible to combine certain constraints
28353 in one constraint string.
28354
28355 You specify the association of an output variable with an assembler operand
28356 through the @code{%@emph{n}} notation, where @emph{n} is a non-negative
28357 integer. Thus in
28358
28359 @quotation
28360
28361 @example
28362 Asm ("pushfl" & LF & HT & -- push flags on stack
28363 "popl %%eax" & LF & HT & -- load eax with flags
28364 "movl %%eax, %0", -- store flags in variable
28365 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
28366 @end example
28367 @end quotation
28368
28369 @code{%0} will be replaced in the expanded code by the appropriate operand,
28370 whatever
28371 the compiler decided for the @code{Flags} variable.
28372
28373 In general, you may have any number of output variables:
28374
28375
28376 @itemize *
28377
28378 @item
28379 Count the operands starting at 0; thus @code{%0}, @code{%1}, etc.
28380
28381 @item
28382 Specify the @code{Outputs} parameter as a parenthesized comma-separated list
28383 of @code{Asm_Output} attributes
28384 @end itemize
28385
28386 For example:
28387
28388 @quotation
28389
28390 @example
28391 Asm ("movl %%eax, %0" & LF & HT &
28392 "movl %%ebx, %1" & LF & HT &
28393 "movl %%ecx, %2",
28394 Outputs => (Unsigned_32'Asm_Output ("=g", Var_A), -- %0 = Var_A
28395 Unsigned_32'Asm_Output ("=g", Var_B), -- %1 = Var_B
28396 Unsigned_32'Asm_Output ("=g", Var_C))); -- %2 = Var_C
28397 @end example
28398 @end quotation
28399
28400 where @code{Var_A}, @code{Var_B}, and @code{Var_C} are variables
28401 in the Ada program.
28402
28403 As a variation on the @code{Get_Flags} example, we can use the constraints
28404 string to direct the compiler to store the eax register into the @code{Flags}
28405 variable, instead of including the store instruction explicitly in the
28406 @code{Asm} template string:
28407
28408 @quotation
28409
28410 @example
28411 with Interfaces; use Interfaces;
28412 with Ada.Text_IO; use Ada.Text_IO;
28413 with System.Machine_Code; use System.Machine_Code;
28414 procedure Get_Flags_2 is
28415 Flags : Unsigned_32;
28416 use ASCII;
28417 begin
28418 Asm ("pushfl" & LF & HT & -- push flags on stack
28419 "popl %%eax", -- save flags in eax
28420 Outputs => Unsigned_32'Asm_Output ("=a", Flags));
28421 Put_Line ("Flags register:" & Flags'Img);
28422 end Get_Flags_2;
28423 @end example
28424 @end quotation
28425
28426 The @code{"a"} constraint tells the compiler that the @code{Flags}
28427 variable will come from the eax register. Here is the resulting code:
28428
28429 @quotation
28430
28431 @example
28432 #APP
28433 pushfl
28434 popl %eax
28435 #NO_APP
28436 movl %eax,-40(%ebp)
28437 @end example
28438 @end quotation
28439
28440 The compiler generated the store of eax into Flags after
28441 expanding the assembler code.
28442
28443 Actually, there was no need to pop the flags into the eax register;
28444 more simply, we could just pop the flags directly into the program variable:
28445
28446 @quotation
28447
28448 @example
28449 with Interfaces; use Interfaces;
28450 with Ada.Text_IO; use Ada.Text_IO;
28451 with System.Machine_Code; use System.Machine_Code;
28452 procedure Get_Flags_3 is
28453 Flags : Unsigned_32;
28454 use ASCII;
28455 begin
28456 Asm ("pushfl" & LF & HT & -- push flags on stack
28457 "pop %0", -- save flags in Flags
28458 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
28459 Put_Line ("Flags register:" & Flags'Img);
28460 end Get_Flags_3;
28461 @end example
28462 @end quotation
28463
28464 @node Input Variables in Inline Assembler,Inlining Inline Assembler Code,Output Variables in Inline Assembler,Inline Assembler
28465 @anchor{gnat_ugn/inline_assembler id5}@anchor{238}@anchor{gnat_ugn/inline_assembler input-variables-in-inline-assembler}@anchor{239}
28466 @section Input Variables in Inline Assembler
28467
28468
28469 The example in this section illustrates how to specify the source operands
28470 for assembly language statements.
28471 The program simply increments its input value by 1:
28472
28473 @quotation
28474
28475 @example
28476 with Interfaces; use Interfaces;
28477 with Ada.Text_IO; use Ada.Text_IO;
28478 with System.Machine_Code; use System.Machine_Code;
28479 procedure Increment is
28480
28481 function Incr (Value : Unsigned_32) return Unsigned_32 is
28482 Result : Unsigned_32;
28483 begin
28484 Asm ("incl %0",
28485 Outputs => Unsigned_32'Asm_Output ("=a", Result),
28486 Inputs => Unsigned_32'Asm_Input ("a", Value));
28487 return Result;
28488 end Incr;
28489
28490 Value : Unsigned_32;
28491
28492 begin
28493 Value := 5;
28494 Put_Line ("Value before is" & Value'Img);
28495 Value := Incr (Value);
28496 Put_Line ("Value after is" & Value'Img);
28497 end Increment;
28498 @end example
28499 @end quotation
28500
28501 The @code{Outputs} parameter to @code{Asm} specifies
28502 that the result will be in the eax register and that it is to be stored
28503 in the @code{Result} variable.
28504
28505 The @code{Inputs} parameter looks much like the @code{Outputs} parameter,
28506 but with an @code{Asm_Input} attribute.
28507 The @code{"="} constraint, indicating an output value, is not present.
28508
28509 You can have multiple input variables, in the same way that you can have more
28510 than one output variable.
28511
28512 The parameter count (%0, %1) etc, still starts at the first output statement,
28513 and continues with the input statements.
28514
28515 Just as the @code{Outputs} parameter causes the register to be stored into the
28516 target variable after execution of the assembler statements, so does the
28517 @code{Inputs} parameter cause its variable to be loaded into the register
28518 before execution of the assembler statements.
28519
28520 Thus the effect of the @code{Asm} invocation is:
28521
28522
28523 @itemize *
28524
28525 @item
28526 load the 32-bit value of @code{Value} into eax
28527
28528 @item
28529 execute the @code{incl %eax} instruction
28530
28531 @item
28532 store the contents of eax into the @code{Result} variable
28533 @end itemize
28534
28535 The resulting assembler file (with @code{-O2} optimization) contains:
28536
28537 @quotation
28538
28539 @example
28540 _increment__incr.1:
28541 subl $4,%esp
28542 movl 8(%esp),%eax
28543 #APP
28544 incl %eax
28545 #NO_APP
28546 movl %eax,%edx
28547 movl %ecx,(%esp)
28548 addl $4,%esp
28549 ret
28550 @end example
28551 @end quotation
28552
28553 @node Inlining Inline Assembler Code,Other Asm Functionality,Input Variables in Inline Assembler,Inline Assembler
28554 @anchor{gnat_ugn/inline_assembler id6}@anchor{23a}@anchor{gnat_ugn/inline_assembler inlining-inline-assembler-code}@anchor{23b}
28555 @section Inlining Inline Assembler Code
28556
28557
28558 For a short subprogram such as the @code{Incr} function in the previous
28559 section, the overhead of the call and return (creating / deleting the stack
28560 frame) can be significant, compared to the amount of code in the subprogram
28561 body. A solution is to apply Ada’s @code{Inline} pragma to the subprogram,
28562 which directs the compiler to expand invocations of the subprogram at the
28563 point(s) of call, instead of setting up a stack frame for out-of-line calls.
28564 Here is the resulting program:
28565
28566 @quotation
28567
28568 @example
28569 with Interfaces; use Interfaces;
28570 with Ada.Text_IO; use Ada.Text_IO;
28571 with System.Machine_Code; use System.Machine_Code;
28572 procedure Increment_2 is
28573
28574 function Incr (Value : Unsigned_32) return Unsigned_32 is
28575 Result : Unsigned_32;
28576 begin
28577 Asm ("incl %0",
28578 Outputs => Unsigned_32'Asm_Output ("=a", Result),
28579 Inputs => Unsigned_32'Asm_Input ("a", Value));
28580 return Result;
28581 end Incr;
28582 pragma Inline (Increment);
28583
28584 Value : Unsigned_32;
28585
28586 begin
28587 Value := 5;
28588 Put_Line ("Value before is" & Value'Img);
28589 Value := Increment (Value);
28590 Put_Line ("Value after is" & Value'Img);
28591 end Increment_2;
28592 @end example
28593 @end quotation
28594
28595 Compile the program with both optimization (@code{-O2}) and inlining
28596 (@code{-gnatn}) enabled.
28597
28598 The @code{Incr} function is still compiled as usual, but at the
28599 point in @code{Increment} where our function used to be called:
28600
28601 @quotation
28602
28603 @example
28604 pushl %edi
28605 call _increment__incr.1
28606 @end example
28607 @end quotation
28608
28609 the code for the function body directly appears:
28610
28611 @quotation
28612
28613 @example
28614 movl %esi,%eax
28615 #APP
28616 incl %eax
28617 #NO_APP
28618 movl %eax,%edx
28619 @end example
28620 @end quotation
28621
28622 thus saving the overhead of stack frame setup and an out-of-line call.
28623
28624 @node Other Asm Functionality,,Inlining Inline Assembler Code,Inline Assembler
28625 @anchor{gnat_ugn/inline_assembler id7}@anchor{23c}@anchor{gnat_ugn/inline_assembler other-asm-functionality}@anchor{23d}
28626 @section Other @code{Asm} Functionality
28627
28628
28629 This section describes two important parameters to the @code{Asm}
28630 procedure: @code{Clobber}, which identifies register usage;
28631 and @code{Volatile}, which inhibits unwanted optimizations.
28632
28633 @menu
28634 * The Clobber Parameter::
28635 * The Volatile Parameter::
28636
28637 @end menu
28638
28639 @node The Clobber Parameter,The Volatile Parameter,,Other Asm Functionality
28640 @anchor{gnat_ugn/inline_assembler id8}@anchor{23e}@anchor{gnat_ugn/inline_assembler the-clobber-parameter}@anchor{23f}
28641 @subsection The @code{Clobber} Parameter
28642
28643
28644 One of the dangers of intermixing assembly language and a compiled language
28645 such as Ada is that the compiler needs to be aware of which registers are
28646 being used by the assembly code. In some cases, such as the earlier examples,
28647 the constraint string is sufficient to indicate register usage (e.g.,
28648 @code{"a"} for
28649 the eax register). But more generally, the compiler needs an explicit
28650 identification of the registers that are used by the Inline Assembly
28651 statements.
28652
28653 Using a register that the compiler doesn’t know about
28654 could be a side effect of an instruction (like @code{mull}
28655 storing its result in both eax and edx).
28656 It can also arise from explicit register usage in your
28657 assembly code; for example:
28658
28659 @quotation
28660
28661 @example
28662 Asm ("movl %0, %%ebx" & LF & HT &
28663 "movl %%ebx, %1",
28664 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
28665 Inputs => Unsigned_32'Asm_Input ("g", Var_In));
28666 @end example
28667 @end quotation
28668
28669 where the compiler (since it does not analyze the @code{Asm} template string)
28670 does not know you are using the ebx register.
28671
28672 In such cases you need to supply the @code{Clobber} parameter to @code{Asm},
28673 to identify the registers that will be used by your assembly code:
28674
28675 @quotation
28676
28677 @example
28678 Asm ("movl %0, %%ebx" & LF & HT &
28679 "movl %%ebx, %1",
28680 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
28681 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
28682 Clobber => "ebx");
28683 @end example
28684 @end quotation
28685
28686 The Clobber parameter is a static string expression specifying the
28687 register(s) you are using. Note that register names are @emph{not} prefixed
28688 by a percent sign. Also, if more than one register is used then their names
28689 are separated by commas; e.g., @code{"eax, ebx"}
28690
28691 The @code{Clobber} parameter has several additional uses:
28692
28693
28694 @itemize *
28695
28696 @item
28697 Use ‘register’ name @code{cc} to indicate that flags might have changed
28698
28699 @item
28700 Use ‘register’ name @code{memory} if you changed a memory location
28701 @end itemize
28702
28703 @node The Volatile Parameter,,The Clobber Parameter,Other Asm Functionality
28704 @anchor{gnat_ugn/inline_assembler id9}@anchor{240}@anchor{gnat_ugn/inline_assembler the-volatile-parameter}@anchor{241}
28705 @subsection The @code{Volatile} Parameter
28706
28707
28708 @geindex Volatile parameter
28709
28710 Compiler optimizations in the presence of Inline Assembler may sometimes have
28711 unwanted effects. For example, when an @code{Asm} invocation with an input
28712 variable is inside a loop, the compiler might move the loading of the input
28713 variable outside the loop, regarding it as a one-time initialization.
28714
28715 If this effect is not desired, you can disable such optimizations by setting
28716 the @code{Volatile} parameter to @code{True}; for example:
28717
28718 @quotation
28719
28720 @example
28721 Asm ("movl %0, %%ebx" & LF & HT &
28722 "movl %%ebx, %1",
28723 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
28724 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
28725 Clobber => "ebx",
28726 Volatile => True);
28727 @end example
28728 @end quotation
28729
28730 By default, @code{Volatile} is set to @code{False} unless there is no
28731 @code{Outputs} parameter.
28732
28733 Although setting @code{Volatile} to @code{True} prevents unwanted
28734 optimizations, it will also disable other optimizations that might be
28735 important for efficiency. In general, you should set @code{Volatile}
28736 to @code{True} only if the compiler’s optimizations have created
28737 problems.
28738
28739 @node GNU Free Documentation License,Index,Inline Assembler,Top
28740 @anchor{share/gnu_free_documentation_license doc}@anchor{242}@anchor{share/gnu_free_documentation_license gnu-fdl}@anchor{1}@anchor{share/gnu_free_documentation_license gnu-free-documentation-license}@anchor{243}
28741 @chapter GNU Free Documentation License
28742
28743
28744 Version 1.3, 3 November 2008
28745
28746 Copyright 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc
28747 @indicateurl{http://fsf.org/}
28748
28749 Everyone is permitted to copy and distribute verbatim copies of this
28750 license document, but changing it is not allowed.
28751
28752 @strong{Preamble}
28753
28754 The purpose of this License is to make a manual, textbook, or other
28755 functional and useful document “free” in the sense of freedom: to
28756 assure everyone the effective freedom to copy and redistribute it,
28757 with or without modifying it, either commercially or noncommercially.
28758 Secondarily, this License preserves for the author and publisher a way
28759 to get credit for their work, while not being considered responsible
28760 for modifications made by others.
28761
28762 This License is a kind of “copyleft”, which means that derivative
28763 works of the document must themselves be free in the same sense. It
28764 complements the GNU General Public License, which is a copyleft
28765 license designed for free software.
28766
28767 We have designed this License in order to use it for manuals for free
28768 software, because free software needs free documentation: a free
28769 program should come with manuals providing the same freedoms that the
28770 software does. But this License is not limited to software manuals;
28771 it can be used for any textual work, regardless of subject matter or
28772 whether it is published as a printed book. We recommend this License
28773 principally for works whose purpose is instruction or reference.
28774
28775 @strong{1. APPLICABILITY AND DEFINITIONS}
28776
28777 This License applies to any manual or other work, in any medium, that
28778 contains a notice placed by the copyright holder saying it can be
28779 distributed under the terms of this License. Such a notice grants a
28780 world-wide, royalty-free license, unlimited in duration, to use that
28781 work under the conditions stated herein. The @strong{Document}, below,
28782 refers to any such manual or work. Any member of the public is a
28783 licensee, and is addressed as “@strong{you}”. You accept the license if you
28784 copy, modify or distribute the work in a way requiring permission
28785 under copyright law.
28786
28787 A “@strong{Modified Version}” of the Document means any work containing the
28788 Document or a portion of it, either copied verbatim, or with
28789 modifications and/or translated into another language.
28790
28791 A “@strong{Secondary Section}” is a named appendix or a front-matter section of
28792 the Document that deals exclusively with the relationship of the
28793 publishers or authors of the Document to the Document’s overall subject
28794 (or to related matters) and contains nothing that could fall directly
28795 within that overall subject. (Thus, if the Document is in part a
28796 textbook of mathematics, a Secondary Section may not explain any
28797 mathematics.) The relationship could be a matter of historical
28798 connection with the subject or with related matters, or of legal,
28799 commercial, philosophical, ethical or political position regarding
28800 them.
28801
28802 The “@strong{Invariant Sections}” are certain Secondary Sections whose titles
28803 are designated, as being those of Invariant Sections, in the notice
28804 that says that the Document is released under this License. If a
28805 section does not fit the above definition of Secondary then it is not
28806 allowed to be designated as Invariant. The Document may contain zero
28807 Invariant Sections. If the Document does not identify any Invariant
28808 Sections then there are none.
28809
28810 The “@strong{Cover Texts}” are certain short passages of text that are listed,
28811 as Front-Cover Texts or Back-Cover Texts, in the notice that says that
28812 the Document is released under this License. A Front-Cover Text may
28813 be at most 5 words, and a Back-Cover Text may be at most 25 words.
28814
28815 A “@strong{Transparent}” copy of the Document means a machine-readable copy,
28816 represented in a format whose specification is available to the
28817 general public, that is suitable for revising the document
28818 straightforwardly with generic text editors or (for images composed of
28819 pixels) generic paint programs or (for drawings) some widely available
28820 drawing editor, and that is suitable for input to text formatters or
28821 for automatic translation to a variety of formats suitable for input
28822 to text formatters. A copy made in an otherwise Transparent file
28823 format whose markup, or absence of markup, has been arranged to thwart
28824 or discourage subsequent modification by readers is not Transparent.
28825 An image format is not Transparent if used for any substantial amount
28826 of text. A copy that is not “Transparent” is called @strong{Opaque}.
28827
28828 Examples of suitable formats for Transparent copies include plain
28829 ASCII without markup, Texinfo input format, LaTeX input format, SGML
28830 or XML using a publicly available DTD, and standard-conforming simple
28831 HTML, PostScript or PDF designed for human modification. Examples of
28832 transparent image formats include PNG, XCF and JPG. Opaque formats
28833 include proprietary formats that can be read and edited only by
28834 proprietary word processors, SGML or XML for which the DTD and/or
28835 processing tools are not generally available, and the
28836 machine-generated HTML, PostScript or PDF produced by some word
28837 processors for output purposes only.
28838
28839 The “@strong{Title Page}” means, for a printed book, the title page itself,
28840 plus such following pages as are needed to hold, legibly, the material
28841 this License requires to appear in the title page. For works in
28842 formats which do not have any title page as such, “Title Page” means
28843 the text near the most prominent appearance of the work’s title,
28844 preceding the beginning of the body of the text.
28845
28846 The “@strong{publisher}” means any person or entity that distributes
28847 copies of the Document to the public.
28848
28849 A section “@strong{Entitled XYZ}” means a named subunit of the Document whose
28850 title either is precisely XYZ or contains XYZ in parentheses following
28851 text that translates XYZ in another language. (Here XYZ stands for a
28852 specific section name mentioned below, such as “@strong{Acknowledgements}”,
28853 “@strong{Dedications}”, “@strong{Endorsements}”, or “@strong{History}”.)
28854 To “@strong{Preserve the Title}”
28855 of such a section when you modify the Document means that it remains a
28856 section “Entitled XYZ” according to this definition.
28857
28858 The Document may include Warranty Disclaimers next to the notice which
28859 states that this License applies to the Document. These Warranty
28860 Disclaimers are considered to be included by reference in this
28861 License, but only as regards disclaiming warranties: any other
28862 implication that these Warranty Disclaimers may have is void and has
28863 no effect on the meaning of this License.
28864
28865 @strong{2. VERBATIM COPYING}
28866
28867 You may copy and distribute the Document in any medium, either
28868 commercially or noncommercially, provided that this License, the
28869 copyright notices, and the license notice saying this License applies
28870 to the Document are reproduced in all copies, and that you add no other
28871 conditions whatsoever to those of this License. You may not use
28872 technical measures to obstruct or control the reading or further
28873 copying of the copies you make or distribute. However, you may accept
28874 compensation in exchange for copies. If you distribute a large enough
28875 number of copies you must also follow the conditions in section 3.
28876
28877 You may also lend copies, under the same conditions stated above, and
28878 you may publicly display copies.
28879
28880 @strong{3. COPYING IN QUANTITY}
28881
28882 If you publish printed copies (or copies in media that commonly have
28883 printed covers) of the Document, numbering more than 100, and the
28884 Document’s license notice requires Cover Texts, you must enclose the
28885 copies in covers that carry, clearly and legibly, all these Cover
28886 Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on
28887 the back cover. Both covers must also clearly and legibly identify
28888 you as the publisher of these copies. The front cover must present
28889 the full title with all words of the title equally prominent and
28890 visible. You may add other material on the covers in addition.
28891 Copying with changes limited to the covers, as long as they preserve
28892 the title of the Document and satisfy these conditions, can be treated
28893 as verbatim copying in other respects.
28894
28895 If the required texts for either cover are too voluminous to fit
28896 legibly, you should put the first ones listed (as many as fit
28897 reasonably) on the actual cover, and continue the rest onto adjacent
28898 pages.
28899
28900 If you publish or distribute Opaque copies of the Document numbering
28901 more than 100, you must either include a machine-readable Transparent
28902 copy along with each Opaque copy, or state in or with each Opaque copy
28903 a computer-network location from which the general network-using
28904 public has access to download using public-standard network protocols
28905 a complete Transparent copy of the Document, free of added material.
28906 If you use the latter option, you must take reasonably prudent steps,
28907 when you begin distribution of Opaque copies in quantity, to ensure
28908 that this Transparent copy will remain thus accessible at the stated
28909 location until at least one year after the last time you distribute an
28910 Opaque copy (directly or through your agents or retailers) of that
28911 edition to the public.
28912
28913 It is requested, but not required, that you contact the authors of the
28914 Document well before redistributing any large number of copies, to give
28915 them a chance to provide you with an updated version of the Document.
28916
28917 @strong{4. MODIFICATIONS}
28918
28919 You may copy and distribute a Modified Version of the Document under
28920 the conditions of sections 2 and 3 above, provided that you release
28921 the Modified Version under precisely this License, with the Modified
28922 Version filling the role of the Document, thus licensing distribution
28923 and modification of the Modified Version to whoever possesses a copy
28924 of it. In addition, you must do these things in the Modified Version:
28925
28926
28927 @enumerate A
28928
28929 @item
28930 Use in the Title Page (and on the covers, if any) a title distinct
28931 from that of the Document, and from those of previous versions
28932 (which should, if there were any, be listed in the History section
28933 of the Document). You may use the same title as a previous version
28934 if the original publisher of that version gives permission.
28935
28936 @item
28937 List on the Title Page, as authors, one or more persons or entities
28938 responsible for authorship of the modifications in the Modified
28939 Version, together with at least five of the principal authors of the
28940 Document (all of its principal authors, if it has fewer than five),
28941 unless they release you from this requirement.
28942
28943 @item
28944 State on the Title page the name of the publisher of the
28945 Modified Version, as the publisher.
28946
28947 @item
28948 Preserve all the copyright notices of the Document.
28949
28950 @item
28951 Add an appropriate copyright notice for your modifications
28952 adjacent to the other copyright notices.
28953
28954 @item
28955 Include, immediately after the copyright notices, a license notice
28956 giving the public permission to use the Modified Version under the
28957 terms of this License, in the form shown in the Addendum below.
28958
28959 @item
28960 Preserve in that license notice the full lists of Invariant Sections
28961 and required Cover Texts given in the Document’s license notice.
28962
28963 @item
28964 Include an unaltered copy of this License.
28965
28966 @item
28967 Preserve the section Entitled “History”, Preserve its Title, and add
28968 to it an item stating at least the title, year, new authors, and
28969 publisher of the Modified Version as given on the Title Page. If
28970 there is no section Entitled “History” in the Document, create one
28971 stating the title, year, authors, and publisher of the Document as
28972 given on its Title Page, then add an item describing the Modified
28973 Version as stated in the previous sentence.
28974
28975 @item
28976 Preserve the network location, if any, given in the Document for
28977 public access to a Transparent copy of the Document, and likewise
28978 the network locations given in the Document for previous versions
28979 it was based on. These may be placed in the “History” section.
28980 You may omit a network location for a work that was published at
28981 least four years before the Document itself, or if the original
28982 publisher of the version it refers to gives permission.
28983
28984 @item
28985 For any section Entitled “Acknowledgements” or “Dedications”,
28986 Preserve the Title of the section, and preserve in the section all
28987 the substance and tone of each of the contributor acknowledgements
28988 and/or dedications given therein.
28989
28990 @item
28991 Preserve all the Invariant Sections of the Document,
28992 unaltered in their text and in their titles. Section numbers
28993 or the equivalent are not considered part of the section titles.
28994
28995 @item
28996 Delete any section Entitled “Endorsements”. Such a section
28997 may not be included in the Modified Version.
28998
28999 @item
29000 Do not retitle any existing section to be Entitled “Endorsements”
29001 or to conflict in title with any Invariant Section.
29002
29003 @item
29004 Preserve any Warranty Disclaimers.
29005 @end enumerate
29006
29007 If the Modified Version includes new front-matter sections or
29008 appendices that qualify as Secondary Sections and contain no material
29009 copied from the Document, you may at your option designate some or all
29010 of these sections as invariant. To do this, add their titles to the
29011 list of Invariant Sections in the Modified Version’s license notice.
29012 These titles must be distinct from any other section titles.
29013
29014 You may add a section Entitled “Endorsements”, provided it contains
29015 nothing but endorsements of your Modified Version by various
29016 parties—for example, statements of peer review or that the text has
29017 been approved by an organization as the authoritative definition of a
29018 standard.
29019
29020 You may add a passage of up to five words as a Front-Cover Text, and a
29021 passage of up to 25 words as a Back-Cover Text, to the end of the list
29022 of Cover Texts in the Modified Version. Only one passage of
29023 Front-Cover Text and one of Back-Cover Text may be added by (or
29024 through arrangements made by) any one entity. If the Document already
29025 includes a cover text for the same cover, previously added by you or
29026 by arrangement made by the same entity you are acting on behalf of,
29027 you may not add another; but you may replace the old one, on explicit
29028 permission from the previous publisher that added the old one.
29029
29030 The author(s) and publisher(s) of the Document do not by this License
29031 give permission to use their names for publicity for or to assert or
29032 imply endorsement of any Modified Version.
29033
29034 @strong{5. COMBINING DOCUMENTS}
29035
29036 You may combine the Document with other documents released under this
29037 License, under the terms defined in section 4 above for modified
29038 versions, provided that you include in the combination all of the
29039 Invariant Sections of all of the original documents, unmodified, and
29040 list them all as Invariant Sections of your combined work in its
29041 license notice, and that you preserve all their Warranty Disclaimers.
29042
29043 The combined work need only contain one copy of this License, and
29044 multiple identical Invariant Sections may be replaced with a single
29045 copy. If there are multiple Invariant Sections with the same name but
29046 different contents, make the title of each such section unique by
29047 adding at the end of it, in parentheses, the name of the original
29048 author or publisher of that section if known, or else a unique number.
29049 Make the same adjustment to the section titles in the list of
29050 Invariant Sections in the license notice of the combined work.
29051
29052 In the combination, you must combine any sections Entitled “History”
29053 in the various original documents, forming one section Entitled
29054 “History”; likewise combine any sections Entitled “Acknowledgements”,
29055 and any sections Entitled “Dedications”. You must delete all sections
29056 Entitled “Endorsements”.
29057
29058 @strong{6. COLLECTIONS OF DOCUMENTS}
29059
29060 You may make a collection consisting of the Document and other documents
29061 released under this License, and replace the individual copies of this
29062 License in the various documents with a single copy that is included in
29063 the collection, provided that you follow the rules of this License for
29064 verbatim copying of each of the documents in all other respects.
29065
29066 You may extract a single document from such a collection, and distribute
29067 it individually under this License, provided you insert a copy of this
29068 License into the extracted document, and follow this License in all
29069 other respects regarding verbatim copying of that document.
29070
29071 @strong{7. AGGREGATION WITH INDEPENDENT WORKS}
29072
29073 A compilation of the Document or its derivatives with other separate
29074 and independent documents or works, in or on a volume of a storage or
29075 distribution medium, is called an “aggregate” if the copyright
29076 resulting from the compilation is not used to limit the legal rights
29077 of the compilation’s users beyond what the individual works permit.
29078 When the Document is included in an aggregate, this License does not
29079 apply to the other works in the aggregate which are not themselves
29080 derivative works of the Document.
29081
29082 If the Cover Text requirement of section 3 is applicable to these
29083 copies of the Document, then if the Document is less than one half of
29084 the entire aggregate, the Document’s Cover Texts may be placed on
29085 covers that bracket the Document within the aggregate, or the
29086 electronic equivalent of covers if the Document is in electronic form.
29087 Otherwise they must appear on printed covers that bracket the whole
29088 aggregate.
29089
29090 @strong{8. TRANSLATION}
29091
29092 Translation is considered a kind of modification, so you may
29093 distribute translations of the Document under the terms of section 4.
29094 Replacing Invariant Sections with translations requires special
29095 permission from their copyright holders, but you may include
29096 translations of some or all Invariant Sections in addition to the
29097 original versions of these Invariant Sections. You may include a
29098 translation of this License, and all the license notices in the
29099 Document, and any Warranty Disclaimers, provided that you also include
29100 the original English version of this License and the original versions
29101 of those notices and disclaimers. In case of a disagreement between
29102 the translation and the original version of this License or a notice
29103 or disclaimer, the original version will prevail.
29104
29105 If a section in the Document is Entitled “Acknowledgements”,
29106 “Dedications”, or “History”, the requirement (section 4) to Preserve
29107 its Title (section 1) will typically require changing the actual
29108 title.
29109
29110 @strong{9. TERMINATION}
29111
29112 You may not copy, modify, sublicense, or distribute the Document
29113 except as expressly provided under this License. Any attempt
29114 otherwise to copy, modify, sublicense, or distribute it is void, and
29115 will automatically terminate your rights under this License.
29116
29117 However, if you cease all violation of this License, then your license
29118 from a particular copyright holder is reinstated (a) provisionally,
29119 unless and until the copyright holder explicitly and finally
29120 terminates your license, and (b) permanently, if the copyright holder
29121 fails to notify you of the violation by some reasonable means prior to
29122 60 days after the cessation.
29123
29124 Moreover, your license from a particular copyright holder is
29125 reinstated permanently if the copyright holder notifies you of the
29126 violation by some reasonable means, this is the first time you have
29127 received notice of violation of this License (for any work) from that
29128 copyright holder, and you cure the violation prior to 30 days after
29129 your receipt of the notice.
29130
29131 Termination of your rights under this section does not terminate the
29132 licenses of parties who have received copies or rights from you under
29133 this License. If your rights have been terminated and not permanently
29134 reinstated, receipt of a copy of some or all of the same material does
29135 not give you any rights to use it.
29136
29137 @strong{10. FUTURE REVISIONS OF THIS LICENSE}
29138
29139 The Free Software Foundation may publish new, revised versions
29140 of the GNU Free Documentation License from time to time. Such new
29141 versions will be similar in spirit to the present version, but may
29142 differ in detail to address new problems or concerns. See
29143 @indicateurl{http://www.gnu.org/copyleft/}.
29144
29145 Each version of the License is given a distinguishing version number.
29146 If the Document specifies that a particular numbered version of this
29147 License “or any later version” applies to it, you have the option of
29148 following the terms and conditions either of that specified version or
29149 of any later version that has been published (not as a draft) by the
29150 Free Software Foundation. If the Document does not specify a version
29151 number of this License, you may choose any version ever published (not
29152 as a draft) by the Free Software Foundation. If the Document
29153 specifies that a proxy can decide which future versions of this
29154 License can be used, that proxy’s public statement of acceptance of a
29155 version permanently authorizes you to choose that version for the
29156 Document.
29157
29158 @strong{11. RELICENSING}
29159
29160 “Massive Multiauthor Collaboration Site” (or “MMC Site”) means any
29161 World Wide Web server that publishes copyrightable works and also
29162 provides prominent facilities for anybody to edit those works. A
29163 public wiki that anybody can edit is an example of such a server. A
29164 “Massive Multiauthor Collaboration” (or “MMC”) contained in the
29165 site means any set of copyrightable works thus published on the MMC
29166 site.
29167
29168 “CC-BY-SA” means the Creative Commons Attribution-Share Alike 3.0
29169 license published by Creative Commons Corporation, a not-for-profit
29170 corporation with a principal place of business in San Francisco,
29171 California, as well as future copyleft versions of that license
29172 published by that same organization.
29173
29174 “Incorporate” means to publish or republish a Document, in whole or
29175 in part, as part of another Document.
29176
29177 An MMC is “eligible for relicensing” if it is licensed under this
29178 License, and if all works that were first published under this License
29179 somewhere other than this MMC, and subsequently incorporated in whole
29180 or in part into the MMC, (1) had no cover texts or invariant sections,
29181 and (2) were thus incorporated prior to November 1, 2008.
29182
29183 The operator of an MMC Site may republish an MMC contained in the site
29184 under CC-BY-SA on the same site at any time before August 1, 2009,
29185 provided the MMC is eligible for relicensing.
29186
29187 @strong{ADDENDUM: How to use this License for your documents}
29188
29189 To use this License in a document you have written, include a copy of
29190 the License in the document and put the following copyright and
29191 license notices just after the title page:
29192
29193 @quotation
29194
29195 Copyright © YEAR YOUR NAME.
29196 Permission is granted to copy, distribute and/or modify this document
29197 under the terms of the GNU Free Documentation License, Version 1.3
29198 or any later version published by the Free Software Foundation;
29199 with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
29200 A copy of the license is included in the section entitled “GNU
29201 Free Documentation License”.
29202 @end quotation
29203
29204 If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts,
29205 replace the “with … Texts.” line with this:
29206
29207 @quotation
29208
29209 with the Invariant Sections being LIST THEIR TITLES, with the
29210 Front-Cover Texts being LIST, and with the Back-Cover Texts being LIST.
29211 @end quotation
29212
29213 If you have Invariant Sections without Cover Texts, or some other
29214 combination of the three, merge those two alternatives to suit the
29215 situation.
29216
29217 If your document contains nontrivial examples of program code, we
29218 recommend releasing these examples in parallel under your choice of
29219 free software license, such as the GNU General Public License,
29220 to permit their use in free software.
29221
29222 @node Index,,GNU Free Documentation License,Top
29223 @unnumbered Index
29224
29225
29226 @printindex ge
29227
29228 @anchor{cf}@w{ }
29229 @anchor{gnat_ugn/gnat_utility_programs switches-related-to-project-files}@w{ }
29230
29231 @c %**end of body
29232 @bye